Compiler Error?

(instructions, reset, WDT, specifications...) PIC17Cxx, PIC18Fxxx

Compiler Error?

Postby SLTom992 » Mon Aug 11, 2014 7:05 pm

I have a piece of code that works with the lines in a specific order but not otherwise. Could you offer a reason for this?

The code reads the on-board 10 bit ADC which monitors the battery power.

If the final two lines are in the reverse order of what they presently are - that is: if they are the same as the others - the code (with good battery voltage) will step down six lines and then hop to line 13 (the last line). This means that with good voltage the light will be a red "low voltage".

If I reverse the order of the bottom two lines the code operates normally and works fine.

This appears to be some sort of optimization but this is the free version which I understood to not have any optimizations in it.

Code: Select all
           if (ad_flag == TRUE && ADCON0bits.GODONE == DONE) {
                int_ADC_rd = ADRESH;
                int_ADC_rd = int_ADC_rd << 8;
                int_ADC_rd |= ADRESL;
                if (int_ADC_rd >= 0x30E) {           // More than 8.8 Volts turn Green
                        led_mode[1] = OFF;            // Red LED
                        led_mode[2] = LED_ON;       // Grn LED
                }
                else if (int_ADC_rd <= 0x2E6) {    // Less than 8.4 turn Red
                        led_mode[1] = LED_ON;       // Red LED
                        led_mode[2] = OFF;             // Grn LED
                }
                else {                                          // In between turn Orange
                        led_mode[2] = LED_ON;       // Grn LED
                        led_mode[1] = LED_ON;       // Red LED
                }
 
SLTom992
 
Posts: 58
Joined: Tue Jun 10, 2014 8:59 pm
PIC experience: Professional 1+ years with MCHP products

Re: Compiler Error?

Postby tunelabguy » Mon Aug 11, 2014 7:56 pm

Off hand I see no obvious problem with the code. But answer a few questions if could.

1. How did you verify that the program will "step down six lines and then hop to line 13"? Did you single-step the program with a debugger to see it happen? Or did you reach that conclusion from observing the end result?

2. Since line numbers are not displayed in your posting, it is not clear what line #13 is. Can you clarify? It is also not clear which lines were executed in the "step down six lines".

3. Assuming you are deducing the program flow from from the end result, how does the setting of led_mode[] affect the green and red LED? Does it use the LATx registers or the PORTx registers?
User avatar
tunelabguy
Verified identity
 
Posts: 29
Joined: Sun Jul 20, 2014 9:41 pm
PIC experience: Professional 5+ years with MCHP products

Re: Compiler Error?

Postby ric » Tue Aug 12, 2014 12:39 am

The free version does have some minimal optimisations allowed.
Plainly it's just re-using the "led_mode[2] = LED_ON; // Grn LED" code, as both the first and last conditions do that instruction second when you've reversed the last one.
i.e. if I am following your explanation correctly, then it is working just as it should.
Latest test project, an LED matrix display made from one reel of addressable LEDs. here
User avatar
ric
Verified identity
 
Posts: 659
Joined: Sat May 24, 2014 2:35 pm
Location: Melbourne, Australia
PIC experience: Professional 5+ years with MCHP products

Re: Compiler Error?

Postby SLTom992 » Wed Aug 13, 2014 9:53 pm

The device is a PIC18F45K22. The program is edited on MPLAB X IDE and the compiler is XC-8. The program is single stepped though the function using an ICD 3. The lines are numbered as any line with code on them and not the empty lines with only brackets.

Ric, indeed the function works with the order of the final two steps reversed but this would appear to be a compiler error.

I have found some others that are a great deal more strange and I will post them as soon as I completely understand what is going on.
SLTom992
 
Posts: 58
Joined: Tue Jun 10, 2014 8:59 pm
PIC experience: Professional 1+ years with MCHP products

Re: Compiler Error?

Postby ric » Thu Aug 14, 2014 12:34 am

SLTom992 wrote:...
Ric, indeed the function works with the order of the final two steps reversed but this would appear to be a compiler error.
...

Why?
It is functionally doing exactly what you have asked it to.
There is never a guarantee of a 1:1 match between C source lines and assembler instructions.
Latest test project, an LED matrix display made from one reel of addressable LEDs. here
User avatar
ric
Verified identity
 
Posts: 659
Joined: Sat May 24, 2014 2:35 pm
Location: Melbourne, Australia
PIC experience: Professional 5+ years with MCHP products

Re: Compiler Error?

Postby SLTom992 » Thu Aug 14, 2014 3:23 am

But there is SUPPOSED to be a 1:1 match between the operation of the code regardless of the order it is written, in cases like that. There has been several times when I have had code that looks correct that simply won't work and I change the order of some perfectly straightforward code and it began working. It should not make a different if you initialize "j" first or "k" first to get code to work.

This is why I am hesitating to get the "Pro" version of the compiler. I'd hate to have to learn a bunch of new bugs.
SLTom992
 
Posts: 58
Joined: Tue Jun 10, 2014 8:59 pm
PIC experience: Professional 1+ years with MCHP products

Re: Compiler Error?

Postby jtemples » Thu Aug 14, 2014 7:32 am

SLTom992 wrote:But there is SUPPOSED to be a 1:1 match between the operation of the code regardless of the order it is written, in cases like that.

No, there isn't. The only thing that the compiler has to do in the specific order you dictated is accesses to the A/D registers. The rest of the code is just maniplating intermediate results; the compiler is free to do those operations in any order it likes, or not at all if it sees that it can reach the same conclusion more efficiently.

It sounds like what you're complaining about is the collision of optimized code with debugging. Make sure all optimizations are disabled during debugging if you want to try avoid this.
jtemples
Verified identity
 
Posts: 195
Joined: Sun May 25, 2014 2:23 am
Location: The 805
PIC experience: Professional 5+ years with MCHP products

Re: Compiler Error?

Postby ric » Thu Aug 14, 2014 12:45 pm

SLTom992 wrote: There has been several times when I have had code that looks correct that simply won't work and I change the order of some perfectly straightforward code and it began working. It should not make a different if you initialize "j" first or "k" first to get code to work.

This is why I am hesitating to get the "Pro" version of the compiler. I'd hate to have to learn a bunch of new bugs.

We seem to be talking at cross purposes.
There is nothing wrong with what the compiler has done, as reported by you above. The end result is exactly the same, and it saved some program space by reusing some sections of identical code.
As John says, you can turn off ALL optimisations if you want to keep the code inefficient. You can program in assembler if you want to specify the exact instruction sequence it should use.
You would save yourself a lot of aggravation if you just let the compiler do its job, and only check that the end result is correct.
Latest test project, an LED matrix display made from one reel of addressable LEDs. here
User avatar
ric
Verified identity
 
Posts: 659
Joined: Sat May 24, 2014 2:35 pm
Location: Melbourne, Australia
PIC experience: Professional 5+ years with MCHP products

Re: Compiler Error?

Postby SLTom992 » Thu Aug 14, 2014 11:09 pm

I'm not sure I understand you. I have the proper battery voltage measured. In the one case it lights the green LED and with those two instructions revered it lights the red LED instead.

I can't quite figure out how that is "nothing wrong with what the compiler has done". Perhaps I'm missing something?

I cannot see any C allowed difference in two non-interactive instructions operating in a different manner when their order is reversed in the program.
SLTom992
 
Posts: 58
Joined: Tue Jun 10, 2014 8:59 pm
PIC experience: Professional 1+ years with MCHP products

Re: Compiler Error?

Postby jtemples » Thu Aug 14, 2014 11:18 pm

I guess I don't understand what you're reporting. Ignoring the behavior in the debugger, does the code behave incorrectly? Can you post the code that works and the code that doesn't work?
jtemples
Verified identity
 
Posts: 195
Joined: Sun May 25, 2014 2:23 am
Location: The 805
PIC experience: Professional 5+ years with MCHP products

Next

Return to 16-Bit Core

Who is online

Users browsing this forum: No registered users and 7 guests

cron