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

Useless Moves

Is there a way to change the DEFINE statement to get rid of the two useless mov(s)?

C51 COMPILER V6.02, COMPILATION OF MODULE MAIN
OBJECT MODULE PLACED IN .\MAIN.OBJ
COMPILER INVOKED BY: C:\PROGRAM FILES\KEIL\C51\BIN\C51.EXE .\MAIN.C OPTIMIZE(7,SPEED) NOINTPROMOTE MODDP2 DEBUG OBJECTEX
                    -TEND CODE SYMBOLS NOCOND

stmt level    source

   1          typedef unsigned char BYTE;
   2          typedef unsigned int  WORD;
   3          
   4          typedef pdata struct 
   5          {
   6            BYTE PC0           :1;
   7            BYTE PC1           :1;
   8            BYTE nFifoPend     :1; //PC2
   9            BYTE PC3           :1;
  10            BYTE FpgaReset     :1; //PC4
  11            BYTE PC5           :1;
  12            BYTE PC6           :1;
  13            BYTE PC7           :1; 
  14          } CType;
  15          
  16          extern volatile BYTE pdata xOUTC;
  17          
  18          #define IoPins_FpgaReset ( ((CType pdata*)(&xOUTC))->FpgaReset )
  19          
  20          void main(void)
  21          {
  22   1        IoPins_FpgaReset = 1;
  23   1      }
  24          
  25          


             ; FUNCTION main (BEGIN)
                                           ; SOURCE LINE # 20
                                           ; SOURCE LINE # 21
                                           ; SOURCE LINE # 22
0000 7A00        E     MOV     R2,#HIGH xOUTC
0002 7900        E     MOV     R1,#LOW xOUTC
0004 7800        E     MOV     R0,#LOW xOUTC
0006 E2                MOVX    A,@R0
0007 4410              ORL     A,#010H
0009 F2                MOVX    @R0,A
                                           ; SOURCE LINE # 23
000A 22                RET     
             ; FUNCTION main (END)

Parents
  • However, I disagree that 'The volatile on the "extern BYTE pdata xOUTC" is not necessary'.

    It is not necessary because C ignores it on extern'd variable declarations. It only matters on the definition which is in some other translation unit for your project. If you don't put volatile on the definition then, yes, xOUT = xOUT will be optimized out.

    Remember definitions allocate storage, declarations do not have to (a definition is an implicit declaration).

    So I agree with your bit-fields being efficient after playing with them. What did you think of my other example using

    volatile CType pdata r_portC _at_ 0x8000;
    Just trying to help.

    - Mark

Reply
  • However, I disagree that 'The volatile on the "extern BYTE pdata xOUTC" is not necessary'.

    It is not necessary because C ignores it on extern'd variable declarations. It only matters on the definition which is in some other translation unit for your project. If you don't put volatile on the definition then, yes, xOUT = xOUT will be optimized out.

    Remember definitions allocate storage, declarations do not have to (a definition is an implicit declaration).

    So I agree with your bit-fields being efficient after playing with them. What did you think of my other example using

    volatile CType pdata r_portC _at_ 0x8000;
    Just trying to help.

    - Mark

Children
  • I am sorry, I am quite wrong. Obviously since the compiler cannot know that the definition might have the volatile keyword if the declaration does not then it will optimize out the xOUT = xOUT statement. You do need to make the definition and extern declaration volatile if the memory "location" is actually a memory mapped device or can be changed by an ISR or other task.

    Please excuse my whilst I eat this large crow.

    - Mark

  • I think the real problem here comes from the fact that the memory location that is referenced is an unsigned char type. However, the actual data is a structure. So, to access the structure you must take the address of the unsigned char, cast it to a structure pointer, and dereference an element of the structure.

    I think it would be much more clear if the original variable were declared as a structure of the appropriate type. Then you don't have to do all of this explicit type casting and you won't run into that sneaky volatile problem.

    Jon