-
Bug
-
Resolution: Unresolved
-
Medium
-
Code Generation Tools
-
CODEGEN-12762
-
-
-
default
-
Use --opt_level=off,0
The compiler incorrectly optimizes an assignment to a bit field where
left hand side is a bit field with the same size as "int", and is within a struct with multiple bit fields
right hand side is either "int" or cast to "int"
and the generated code incorrectly does not mask the right hand side before assigning over the entire struct.
Above occurs for --opt_level=1,2,3,4 (and not --opt_level=off,0)
NOTE: below example is for C28 which has 16bit "int", however the same bug can occur on architectures with 32bit "int" and 32bit bit fields with 64bit double assign cast through int32_t.
NOTE: below test case only triggers the bug with little endian due to the sizes of types being assigned, however, the bug occurs for both big/little endian.
The attached source file has these lines ...
struct
{ uint32_t a:16; uint32_t b:16; }instance;
float getA()
{ return -10.f; }int16_t getB()
{ return 10; }void fxn()
{ instance.b = getB(); instance.a = (int16_t)getA(); }Build it ...
% cl2000 -@options.txt -s file.c
Inspect the assembly code in file.asm. These are the comments added by the compiler.
;*** 14 ------ *&instance &= 0xffffuL;
;*** 14 ------ *&instance |= (unsigned long)getB()<<16;
;*** 15 ------ *&instance &= 0xffff0000uL;
;*** 15 ------ *&instance |= (unsigned long)(int)getA();
The second |= expression is wrong. It is missing a 16bit mask.
It instead overwrites the entire structure.
The correct code generated by the compiler is shown below with missing mask for line 15:
;*** 14 ------- *&instance &= 0xffffuL;
;*** 14 ------- *&instance |= (unsigned long)getB()<<16;
;*** 15 ------- *&instance &= 0xffff0000uL;
;*** 15 ------- *&instance |= (unsigned long)(int)getA()&0xffffuL;