Does avr compiler optimization affect registers?

Go To Last Post
8 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hello.

Does avr compiler optimization affect registers like PORTC, OCR0A, EICRA etc etc? 

I think it does, since they are part of the ram, just like variables. 

 

Since i dont know so much about gcc optimization, Is there a source where i can read which parts of an atmega is affected by compiler optimizations? I would prefer a simple explanation from a tutorial, rather than a official documentation since it will be hard to understand for me, but both will do.

 

Thanks

This topic has a solution.
Last Edited: Wed. Sep 11, 2019 - 05:56 AM
This reply has been marked as the solution. 
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Basilisvirus wrote:

Does avr compiler optimization affect registers like PORTC, OCR0A, EICRA etc etc? 

I think it does, since they are part of the ram, just like variables. 

No.

 

The whole point of those all been defined as "volatile" is for the very reason that volatile means "do not optimize access to this". For example:

#include <avr/io.h>

int main() {
	PORTB;
}

Notice I am not even doing anything with "PORTB". I simply mention it. If it were optimized the C compiler would realise that this reference does nothing so would optimize it away. But:

00000080 <main>:
#include <avr/io.h>

int main() {
	PORTB;
  80:	85 b1       	in	r24, 0x05	; 5

So the compiler HAS generated an "in" to read the location because it was volatile. You can see this in the code after pre-processing:

# 3 ".././main.c"
int main() {
 
# 4 ".././main.c" 3
(*(volatile uint8_t *)((0x05) + 0x20))
# 4 ".././main.c"
     ;

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Basilisvirus wrote:

Does avr compiler optimization affect registers like PORTC, OCR0A, EICRA etc etc? 

"Optimization" is an extremely broad concept. What exactly are you talking about? What king of effect do you have in mind when you say "affect registers"?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

clawson's reply was good for me, but i dont know exactly the 'nature' of my question, i understand your doubt. I never studied about compiler optimization. I just know that it exists, and that tries to make your code faster, sometimes with undesirable way.

The question is more like "do these registers get optimized like any other variable?, or they have different treatment from the compiler?"

Clawson solved my question for now, but im still learning, i will sure do a more precise question in the future

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 1

Basilisvirus wrote:
sometimes with undesirable way.
If that happens then you aren't using the compiler right. I guess you are talking about the classic case of a main/ISR shared variable?

int flag;

int main(void) {
    enable_some_vect_ISR();

    flag = 0;
    while (1) {
        if (flag != 0) {
            PORTB = 0x55;
        }
    }
}

ISR(SOME_vect) {
    flag = 1;
}

If you are the C compiler and you look at this sequence:

    flag = 0;
    while (1) {
        if (flag != 0) {
            PORTB = 0x55;
        }
    }

You can see flag is set to 0, there's apparently no way it could change to 1 in this sequence so:

        if (flag != 0) {
            PORTB = 0x55;
        }

could never be true. As such there's no point generating code for either the if() test or the consequences.

 

The correct solution is "volatile" which says "do not optimise this - always read/write it - you can't see it right now but there is a way this may change".

 

Other than that optimisation should only do good things to your code (make it smaller and faster).

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

clawson wrote:

Basilisvirus wrote:

 

Does avr compiler optimization affect registers like PORTC, OCR0A, EICRA etc etc? 

I think it does, since they are part of the ram, just like variables. 

No.

Actually, the answer is: "Yes, but".

 

"Yes" - they are handled exactly the same as other variables;

 

"But" - as clawson says, the definitions of  PORTC, OCR0A, EICRA etc etc  include 'volatile' - exactly as required for any other variable.

 

This really isn't anything special to AVR or microcontrollers - it is all standard 'C' stuff.

 

Here are some 'C' learning & reference materials:

 

http://blog.antronics.co.uk/2011...

 

 

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

thanks, i will check this link as well.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Yes, i remember you helped me on that 'volatile' declaration, on another post i made here. 

 

Well while i managed to fix the while(){} thing, another issue came up this time.

 

I will soon make another post here, regarding the issues can occur from the compiler, but i am currently trying to look it up and figure it out myself before i make a new thread. Usually the answer is out there, but sometimes you dont know the right question.

Thanks again