strange bit variable code generation

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

strange bit variable code generation

Jump to solution
3,434 Views
rg
Contributor I

hello
I have C code like this
#pragma DATA_SEG __SHORT_SEG MY_ZEROPAGE
...
struct {
u8 one:1;
u8 two:1
}alm;
#pragma DATA_SEG DEFAULT_RAM

void fun1(void)
{
...
alm.one = alm.two;
alm.one = PTAD_PTAD0;
...
}
and CW translate fun1 to:

343: alm.one = alm.two;
0000 020003 [5] BRSET 1,alm,L6 ;abs = 0006
0003 1100 [5] BCLR 0,alm
0005 65 [3] SKIP2 L8 ;abs = 0008
0006 L6:
0006 1000 [5] BSET 0,alm
0008 L8:
344: alm.one = PTAD_PTAD0;
0008 b600 [3] LDA _PTAD
000a a401 [2] AND #1
000c 2603 [3] BNE L11 ;abs = 0011
000e 1100 [5] BCLR 0,alm
0010 65 [3] SKIP2 L13 ;abs = 0013
0011 L11:
0011 1000 [5] BSET 0,alm
0013 L13:

my question is why PTAD_PTAD0 is not same as alm.two?

Labels (1)
Tags (1)
0 Kudos
Reply
1 Solution
1,667 Views
CompilerGuru
NXP Employee
NXP Employee
There are a lot of things which might cause different code patterns to be generated for bitfields.
Just the ones which come to my mind now:
- the number of bits (both times the same in your snippet)
- the bit offset, especially bitoffsets 0 and 7 (least/most significant bits) may have special patterns
- if the bitfield is volatile (I guess the PTAD_PTAD0 is)
- how the bitfield is used, especially tests, assignments, ...
- note that all your assignments have two bitfield accesses.
- how to access the variables (DIRECT,...)
- surrounding code (CSE, reuse of previously used computed subexpressions, available constants, common code opti, ...)

Daniel

View solution in original post

0 Kudos
Reply
6 Replies
1,668 Views
CompilerGuru
NXP Employee
NXP Employee
There are a lot of things which might cause different code patterns to be generated for bitfields.
Just the ones which come to my mind now:
- the number of bits (both times the same in your snippet)
- the bit offset, especially bitoffsets 0 and 7 (least/most significant bits) may have special patterns
- if the bitfield is volatile (I guess the PTAD_PTAD0 is)
- how the bitfield is used, especially tests, assignments, ...
- note that all your assignments have two bitfield accesses.
- how to access the variables (DIRECT,...)
- surrounding code (CSE, reuse of previously used computed subexpressions, available constants, common code opti, ...)

Daniel

0 Kudos
Reply
1,667 Views
Sten
Contributor IV
Do I misunderstand something ("my question is why PTAD_PTAD0 is not same as alm.two" )? This code do not try to set the port pin equal to alm.two, only sets alm.one equal to the port pin!
0 Kudos
Reply
1,667 Views
allawtterb
Contributor IV


Sten wrote:
Do I misunderstand something ("my question is why PTAD_PTAD0 is not same as alm.two" )? This code do not try to set the port pin equal to alm.two, only sets alm.one equal to the port pin!



No, you are right.  They are putting putting the value in alm.two into alm.one and then overwriting it with the status of PTAD_PTAD0.  If they want PTAD_PTAD0 to be equal to alm.two they have to either:
1) Have PTAD_PTAD0 as an output then change the second line to
PTAD_PTAD0 = alm.one;  // Set Port D Bit 0 to alm.one
or 2) Do the following:
alm.one = PTAD_PTAD0; // Set Port D Bit 0 to alm.one
alm.two = alm.one;           // Set alm.two to alm.one
 


Message Edited by allawtterb on 2008-01-29 08:25 PM
0 Kudos
Reply
1,667 Views
CompilerGuru
NXP Employee
NXP Employee
I did understand the OP's question as why the compiler would generate different code for the two assignments, and not why the values of the port and the alm.two bitfield member were not the same. The latter are of course not the same as they are completely independent.

rg, can you clarify?

Daniel
0 Kudos
Reply
1,667 Views
rg
Contributor I
thanks replies.
 
this snippet is for testing OPTION of compiler,
and dont have real effect.
in fact,I want asm code like this:
 BRSET 1,alm,L6
 BCLR 0,alm
 BRA L8
L6:
 BSET 0,alm
L8:
 BRSET 0,_PTAD,L61
 BCLR 0,alm
 BRA L81
L61:
 BSET 0,alm
L81:
 
0 Kudos
Reply
1,667 Views
BlackNight
NXP Employee
NXP Employee
Hello,
could you check/publish here the the declaration of PTAD_PTAD0?
Maybe this is not a bitfield, or signed?

BK
0 Kudos
Reply