Uploaded image for project: 'Embedded Software & Tools'
  1. Embedded Software & Tools
  2. EXT_EP-11874

assignment to a bit field mistakenly overwrites entire bit field structure

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Unresolved
    • Icon: Medium Medium
    • Code Generation Tools
    • CODEGEN-12762
    • Hide
      C2000_18.1.0.LTS
      C6000_8.3.0
      MSP430_20.2.0.LTS
      C7000_2.1.0.LTS
      C2000_22.6.1.LTS
      ARM_18.12.0.LTS
      MSP430_21.6.0.LTS
      C2000_21.6.0.LTS
      PRU_2.3.0
      C7000_3.1.0.LTS
      PRU_2.2.0
      ARM_20.2.0.LTS
      C7000_4.1.0.LTS
      C2000_20.2.0.LTS
      C2000_15.12.0.LTS
      C2000_6.4.0
      C2000_18.12.0.LTS
      Show
      C2000_18.1.0.LTS C6000_8.3.0 MSP430_20.2.0.LTS C7000_2.1.0.LTS C2000_22.6.1.LTS ARM_18.12.0.LTS MSP430_21.6.0.LTS C2000_21.6.0.LTS PRU_2.3.0 C7000_3.1.0.LTS PRU_2.2.0 ARM_20.2.0.LTS C7000_4.1.0.LTS C2000_20.2.0.LTS C2000_15.12.0.LTS C2000_6.4.0 C2000_18.12.0.LTS
    • Hide
      C2000_25.3.0.LTS*
      C7000_2.1.3.LTS*
      MSP430_21.6.2.LTS*
      C7000_5.0.0.LTS*
      C7000_4.1.1.LTS*
      C7000_3.1.3.LTS*
      C2000_21.6.2.LTS*
      MSP430_20.2.8.LTS*
      C2000_22.6.2.LTS*
      ARM_20.2.8.LTS*
      PRU_2.4.0*
      C6000_8.3.14*
      PRU_2.3.4*
      Show
      C2000_25.3.0.LTS* C7000_2.1.3.LTS* MSP430_21.6.2.LTS* C7000_5.0.0.LTS* C7000_4.1.1.LTS* C7000_3.1.3.LTS* C2000_21.6.2.LTS* MSP430_20.2.8.LTS* C2000_22.6.2.LTS* ARM_20.2.8.LTS* PRU_2.4.0* C6000_8.3.14* PRU_2.3.4*
    • 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;

            syncuser TI User
            syncuser TI User
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated: