 |
|
 |
RENESAS TOOL NEWS on March 1, 2004: RSO-M3T-NC308WA_2-040301D
A Note on Using C Compilers M3T-NC308WA and M3T-NC30WA
--On Referencing Members in a Union--
|
Please take note of the following problem in using C compilers (with an assembler and integrated development environment) M3T-NC308WA and M3T-NC30WA
- On referencing members in a union
- Products and Versions Concerned
M3T-NC308WA V.5.00 Release 1 and V.5.10 Release 1
for the M32C/80 and M16C/80 series MCUs
M3T-NC30WA V.5.10 Release 1 and V.5.20 Release 1
for the M16C/60, M16C/30, M16C/Tiny, M16C/20, M16C/10, and R8C/Tiny series MCUs
- Description
References to members in a union may not properly be made.
- Conditions
This problem occurs if the following six conditions are satisfied:
| (1) |
Any of the optimizing options -OR, -OS, -O, -O1, -O2, -O3, -O4, and -O5 is selected.
|
| (2) |
A union is defined as auto variable A.
|
| (3) |
A constant is assigned to a member B of the union defined in (2).
|
| (4) |
A write is made to another member C of the union above. (This write also affects the value of member B.)
|
| (5) |
The addresses of B and C are separated from each other by the number of bytes shown below.
a. 2 bytes or more if the union is in the near area.
b. 4 bytes or more if the union is in the far area.
|
| (6) |
Member B is referenced after the write in (4) is made.
|
- Example
----------------------------------------------------------------
union UNION1 {
unsigned long long m64;
unsigned long m32[2];
};
extern unsigned long long gi64;
void func(void)
{
union UNION1 au; /* Condition (2) */
au.m64 = 0x1234567887654321ull; /* Condition (3) */
au.m32[1] = 0x44444444ul; /* Conditions (4) and (5) */
gi64 = au.m64; /* Condition (6) */
}
----------------------------------------------------------------
- Workaround
This problem can be circumvented in either of the following ways:
| (1) |
Don't use any of the optimizing options -OR, -OS, -O, -O1, -O2, -O3, -O4, and -O5.
|
| (2) |
Place a dummy asm function immediately after assigning a constant to member B of the union.
|
|
Example:
----------------------------------------------------------------
union UNION1 {
unsigned long long m64;
unsigned long m32[2];
};
extern unsigned long long gi64;
void func(void)
{
union UNION1 au;
au.m64 = 0x1234567887654321ull;
asm(); /* Dummy asm function */
au.m32[1] = 0x44444444ul;
gi64 = au.m64;
}
----------------------------------------------------------------
|
- Schedule of Fixing the Problem
We plan to fix this problem in our next release of the products.
|
 |