<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>Classic/Legacy CodeWarriorのトピックBug report: HC12 Compiler 5.0.35 will erroneously inline routines with assembly code</title>
    <link>https://community.nxp.com/t5/Classic-Legacy-CodeWarrior/Bug-report-HC12-Compiler-5-0-35-will-erroneously-inline-routines/m-p/150870#M2064</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;HC12 Compiler 5.0.35 will erroneously inline routines with assembly code.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;We switched from version 5.0.21&amp;nbsp; to 5.0.35 of the HC12 compiler.&amp;nbsp; A C routine that had assembly code in it was not inlined by compiler 5.0.21.&amp;nbsp; However, 5.0.35 did inline the routine, and did so poorly, resulting in a bug (stack mixup causing variable values to be written in the wrong place).&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Here's the C code:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;#pragma INLINE&lt;BR /&gt;char SpiByteExchange(register unsigned char cDataToSend)&lt;BR /&gt;{&lt;BR /&gt;&amp;nbsp;&amp;nbsp; while (SPI1SR_SPTEF == 0);&lt;BR /&gt;&amp;nbsp;&amp;nbsp; asm STAB SPI1DR;&lt;BR /&gt;&amp;nbsp;&amp;nbsp; while (SPI1SR_SPIF == 0);&lt;BR /&gt;&amp;nbsp;&amp;nbsp; return SPI1DR;&lt;BR /&gt;}&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Here's the assembly from compiler 5.0.35:&lt;/P&gt;&lt;P&gt;Options : -C++f -Cf -Cni -CpEPAGE -CpGPAGE -CpPPAGE -CpRPAGE -CPUHCS12X -DSDRX -F2 -Lasm=%n.lst -Lasmc=aepv -Ldf=predef.h -Lm=d:\SDRX\text\SDRX.dep -Ml -Onu -Onf -Oi=c0 -OnB -Onbt -Onca -Oncn -One -OnP=abcdefghijklmnpqrtu -Ont= -TuCD4LD4LLD4uE -ViewHidden -W1 -WErrFileOff -WmsgNu=abcde -WmsgSd1106 -WmsgSd1435 -WOutFileOn&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp; 513:&amp;nbsp;&amp;nbsp;&amp;nbsp; SpiByteExchange(0xff);&lt;BR /&gt;&amp;nbsp; c6ff&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; [1]&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; LDAB&amp;nbsp; #255&lt;BR /&gt;&amp;nbsp; 6b8c&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; [2]&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; STAB&amp;nbsp; 12,SP&lt;BR /&gt;&amp;nbsp; 4f0020fc&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; [4]&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; BRCLR _SPI1SR,#32,*+0 ;abs = 0012&lt;BR /&gt;&amp;nbsp; 5b00&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; [2]&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; STAB&amp;nbsp; _SPI1DR&lt;BR /&gt;&amp;nbsp; 4f0080fc&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; [4]&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; BRCLR _SPI1SR,#128,*+0 ;abs = 0018&lt;BR /&gt;&amp;nbsp; d600&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; [3]&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; LDAB&amp;nbsp; _SPI1DR&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Sat, 25 Sep 2010 03:53:15 GMT</pubDate>
    <dc:creator>adiroot</dc:creator>
    <dc:date>2010-09-25T03:53:15Z</dc:date>
    <item>
      <title>Bug report: HC12 Compiler 5.0.35 will erroneously inline routines with assembly code</title>
      <link>https://community.nxp.com/t5/Classic-Legacy-CodeWarrior/Bug-report-HC12-Compiler-5-0-35-will-erroneously-inline-routines/m-p/150870#M2064</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;HC12 Compiler 5.0.35 will erroneously inline routines with assembly code.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;We switched from version 5.0.21&amp;nbsp; to 5.0.35 of the HC12 compiler.&amp;nbsp; A C routine that had assembly code in it was not inlined by compiler 5.0.21.&amp;nbsp; However, 5.0.35 did inline the routine, and did so poorly, resulting in a bug (stack mixup causing variable values to be written in the wrong place).&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Here's the C code:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;#pragma INLINE&lt;BR /&gt;char SpiByteExchange(register unsigned char cDataToSend)&lt;BR /&gt;{&lt;BR /&gt;&amp;nbsp;&amp;nbsp; while (SPI1SR_SPTEF == 0);&lt;BR /&gt;&amp;nbsp;&amp;nbsp; asm STAB SPI1DR;&lt;BR /&gt;&amp;nbsp;&amp;nbsp; while (SPI1SR_SPIF == 0);&lt;BR /&gt;&amp;nbsp;&amp;nbsp; return SPI1DR;&lt;BR /&gt;}&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Here's the assembly from compiler 5.0.35:&lt;/P&gt;&lt;P&gt;Options : -C++f -Cf -Cni -CpEPAGE -CpGPAGE -CpPPAGE -CpRPAGE -CPUHCS12X -DSDRX -F2 -Lasm=%n.lst -Lasmc=aepv -Ldf=predef.h -Lm=d:\SDRX\text\SDRX.dep -Ml -Onu -Onf -Oi=c0 -OnB -Onbt -Onca -Oncn -One -OnP=abcdefghijklmnpqrtu -Ont= -TuCD4LD4LLD4uE -ViewHidden -W1 -WErrFileOff -WmsgNu=abcde -WmsgSd1106 -WmsgSd1435 -WOutFileOn&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp; 513:&amp;nbsp;&amp;nbsp;&amp;nbsp; SpiByteExchange(0xff);&lt;BR /&gt;&amp;nbsp; c6ff&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; [1]&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; LDAB&amp;nbsp; #255&lt;BR /&gt;&amp;nbsp; 6b8c&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; [2]&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; STAB&amp;nbsp; 12,SP&lt;BR /&gt;&amp;nbsp; 4f0020fc&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; [4]&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; BRCLR _SPI1SR,#32,*+0 ;abs = 0012&lt;BR /&gt;&amp;nbsp; 5b00&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; [2]&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; STAB&amp;nbsp; _SPI1DR&lt;BR /&gt;&amp;nbsp; 4f0080fc&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; [4]&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; BRCLR _SPI1SR,#128,*+0 ;abs = 0018&lt;BR /&gt;&amp;nbsp; d600&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; [3]&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; LDAB&amp;nbsp; _SPI1DR&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 25 Sep 2010 03:53:15 GMT</pubDate>
      <guid>https://community.nxp.com/t5/Classic-Legacy-CodeWarrior/Bug-report-HC12-Compiler-5-0-35-will-erroneously-inline-routines/m-p/150870#M2064</guid>
      <dc:creator>adiroot</dc:creator>
      <dc:date>2010-09-25T03:53:15Z</dc:date>
    </item>
    <item>
      <title>Re: Bug report: HC12 Compiler 5.0.35 will erroneously inline routines with assembly code</title>
      <link>https://community.nxp.com/t5/Classic-Legacy-CodeWarrior/Bug-report-HC12-Compiler-5-0-35-will-erroneously-inline-routines/m-p/150871#M2065</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;&amp;gt;However, 5.0.35 did inline the routine, and did so poorly, resulting in a&lt;/P&gt;&lt;P&gt;&amp;gt; bug (stack mixup causing variable values to be written in the wrong place).&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;When looking at the code I see a store to SP+12. Are you sure that is not just where the (never read)&amp;nbsp;cDataToSend is stored to? &amp;nbsp;As -Onu is used (don't optimize unused stores) this seems reasonable to me.&lt;/P&gt;&lt;P&gt;Just looking at the code I'm not sure it is actually wrong.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The shown&amp;nbsp;SpiByteExchange is on the other hand&amp;nbsp;explicitly&amp;nbsp;marked with a #pragma INLINE. I well would think inlining it is the right thing to do. Not inlining it is probably as easy as not using that pragma...&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;BTW: The routine assumes that the B register is not modified by the C code before. I don't think this is guaranteed. While the existing simple while loop might not touch B, I don't think compiler would preserve it if some other C patter would write to B. So that STAB is a bit on the dangerous side, especially with inlining where the store to the unused local variable is a bit pointless....&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Daniel&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 25 Sep 2010 11:01:59 GMT</pubDate>
      <guid>https://community.nxp.com/t5/Classic-Legacy-CodeWarrior/Bug-report-HC12-Compiler-5-0-35-will-erroneously-inline-routines/m-p/150871#M2065</guid>
      <dc:creator>CompilerGuru</dc:creator>
      <dc:date>2010-09-25T11:01:59Z</dc:date>
    </item>
    <item>
      <title>Re: Bug report: HC12 Compiler 5.0.35 will erroneously inline routines with assembly code</title>
      <link>https://community.nxp.com/t5/Classic-Legacy-CodeWarrior/Bug-report-HC12-Compiler-5-0-35-will-erroneously-inline-routines/m-p/150872#M2066</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hi Daniel and thanks for your reply.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I think that you are correct in saying that the bug caused by the combination of the STAB instruction and the #pragma INLINE.&amp;nbsp; I should not be doing one or the other.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;However The issue that I am reporting is this:&lt;/P&gt;&lt;P&gt;"Compiler is inlining routines that have assembly code in them".&lt;/P&gt;&lt;P&gt;&amp;nbsp;Previous versions of the compiler did not have this bug and did not inline the code.&amp;nbsp; That was easy enough to check by looking at the assembly listing.&amp;nbsp; The manual says that functions with inline assembly statements are not inlined (pg 239)&lt;/P&gt;&lt;P&gt;HC(S)12 Build Tools Reference Manual Revised: 3 April 2008&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I am using -Oi=0 and a pragma inline, so mea culpa in this case for trying to inline the routine.&amp;nbsp;&amp;nbsp; However, other programmers that are using the -Oi option (which inlines all routines of less than 40 bytes) and switching to the newer version of the compiler may run into the same bug.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I think that a fix is needed.&amp;nbsp; Or an update of the manual.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 29 Sep 2010 03:55:31 GMT</pubDate>
      <guid>https://community.nxp.com/t5/Classic-Legacy-CodeWarrior/Bug-report-HC12-Compiler-5-0-35-will-erroneously-inline-routines/m-p/150872#M2066</guid>
      <dc:creator>adiroot</dc:creator>
      <dc:date>2010-09-29T03:55:31Z</dc:date>
    </item>
    <item>
      <title>Re: Bug report: HC12 Compiler 5.0.35 will erroneously inline routines with assembly code</title>
      <link>https://community.nxp.com/t5/Classic-Legacy-CodeWarrior/Bug-report-HC12-Compiler-5-0-35-will-erroneously-inline-routines/m-p/150873#M2067</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Reporting the issue as documentation bug makes sence, absolutely.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;gt;I am using -Oi=0 and a pragma inline, so mea culpa in this case for trying to inline the routine. &amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;gt; However, other programmers that are using the -Oi option (which inlines all routines of less than 40 bytes)&lt;/P&gt;&lt;P&gt;&amp;gt; and switching to the newer version of the compiler may run into the same bug.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Just using&amp;nbsp;-Oi=40 will not inline functions with HLI. Functions containing HLI code are only inlined if they contain an explicit &amp;nbsp;#pragma INLINE.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Daniel&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 29 Sep 2010 10:26:27 GMT</pubDate>
      <guid>https://community.nxp.com/t5/Classic-Legacy-CodeWarrior/Bug-report-HC12-Compiler-5-0-35-will-erroneously-inline-routines/m-p/150873#M2067</guid>
      <dc:creator>CompilerGuru</dc:creator>
      <dc:date>2010-09-29T10:26:27Z</dc:date>
    </item>
  </channel>
</rss>

