This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

compiler optimizations

Hello,

1. What is this different style of value assigning? What is the use?

Unsigned char const event_strings[11] = {
~0x3f, ~0x06, ~0x5b, ~0x4f, ~0x66, ~0x6d, ~0x7d, ~0x07, ~0x7f, ~0x6f, ~0x00};

2. What are the drawbacks / overheads on the controller if we use volatile type? Can we assign volatile type to structure members and bdata variables as shown below:

Eg 1:

typedef struct dtmf_scan_block{
  unsigned char state;
  unsigned char user;
  unsigned char s_r;
  unsigned char r_id;
}DTMF_SCAN_BLOCK;

DTMF_SCAN_BLOCK volatile xdata dtmf[NO_DTMF];

Eg2:

unsigned char volatile bdata dtvar;
sbit dt0        = dtvar^0;
sbit dt1        = dtvar^1;
sbit dt2        = dtvar^2;
sbit dt3        = dtvar^3;

Eg3:

typedef struct extention_data
{
        unsigned char volatile dgt_buf[40];
        unsigned char volatile how[16];
        unsigned char call_privacy[3];
        unsigned char id;
}EXTN_DATA;

3. I am comparing bit type variable with unsigned char variable at many places in my code. Should I follow typecasting?

4. Is it necessary to turn off compiler optimizations {Level 9} for hardware driver initialization code in my project? I am initializing drivers for switch array MT8816, GM16C550 - UART, RTC - DS1380, DTMF IC MT8888,etc

please advise.

Parents
  • ~x3f is a bitwise negation of the value. For an 8-bit data type, this would assign the value 0xc0 since (0xc0 + 0x3f = 0xff).

    The volatile keyword definitely affects code optimization (as intended) since it forces the compiler to always read fresh values from the memory location instead of caching the value in a register.

    Why do you ask where you can place the volatile keyword? If you want the formal answer, the language standard would be the place to look. For quick and dirty - just test what the compiler accepts.

    If a compiler generates correct code - and you have rememberd to set all registers/memory locations of the hw as volatile - then code optimization should normally not affect your code. However, some hardware are timing-sensitive so high optimization levels have a larger probability of making your code run too fast. But hardware timing is something you have to think about - and verify - whatever optimiztion you use. Also, if you forget the volatile keyword, some optimization levels (possibly all) can result in a failed program because the compiler optimizes away what it thinks is a "redundant" memory access.

Reply
  • ~x3f is a bitwise negation of the value. For an 8-bit data type, this would assign the value 0xc0 since (0xc0 + 0x3f = 0xff).

    The volatile keyword definitely affects code optimization (as intended) since it forces the compiler to always read fresh values from the memory location instead of caching the value in a register.

    Why do you ask where you can place the volatile keyword? If you want the formal answer, the language standard would be the place to look. For quick and dirty - just test what the compiler accepts.

    If a compiler generates correct code - and you have rememberd to set all registers/memory locations of the hw as volatile - then code optimization should normally not affect your code. However, some hardware are timing-sensitive so high optimization levels have a larger probability of making your code run too fast. But hardware timing is something you have to think about - and verify - whatever optimiztion you use. Also, if you forget the volatile keyword, some optimization levels (possibly all) can result in a failed program because the compiler optimizes away what it thinks is a "redundant" memory access.

Children
No data