This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

[参考译文] C60-CGT:C60-CGT 8.3.13:使用枚举类错误编译开关盒

Guru**** 2470720 points


请注意,本文内容源自机器翻译,可能存在语法或其它翻译错误,仅供参考。如需获取准确内容,请参阅链接中的英语原文或自行翻译。

https://e2e.ti.com/support/processors-group/processors/f/processors-forum/1467094/c6000-cgt-c6000-cgt-8-3-13-miscompilation-of-swtich-case-with-enum-class

器件型号:C60-CGT

工具与软件:

编译器:C60-CGT 8.3.13

如果未传递优化设置、则以下代码会被错误编译:

例如、"cl6x.exe enumClass.cpp"

enum class A : unsigned short
{
    A1 = 0xFFFFu
};

int test(A a)
{
    switch (a)
    {
        case A::A1:
            return 42;
        default:
            return 0;
    }
}

反汇编:

Disassembly of enumClass.obj:

TEXT Section .text (Little Endian), 0x40 bytes at 0x00000000
00000000            _Z4test1A:
00000000   07bf005b           SUB.L2        B15,0x8,B15
00000004   000c1fd8 ||        MV.L1X        B3,A0
00000008   023c42d4           STH.D2T1      A4,*+B15[2]
0000000c   000ca120           BNOP.S1       $C$L3 (PC+24 = 0x00000018),5
00000010            $C$L1:
00000010       858a           BNOP.S1       $C$L4 (PC+44 = 0x0000002c),4
00000012       4a32           MVK.S1        42,A4
00000014            $C$L2:
00000014       858a           BNOP.S1       $C$L4 (PC+44 = 0x0000002c),4
00000016       0626           MVK.L1        0,A4
00000018            $C$L3:
00000018   023c4286           LDHU.D2T2     *+B15[2],B4
0000001c   e6108000           .fphead       p, l, W, BU, br, nosat, 0110000b
00000020   0013ea5a           CMPEQ.L2      -1,B4,B0
00000024   2ffca120    [ B0]  BNOP.S1       $C$L1 (PC-16 = 0x00000010),5
00000028   0ffda120           BNOP.S1       $C$L2 (PC-12 = 0x00000014),5
0000002c            $C$L4:
0000002c   00809362           BNOP.S2X      A0,4
00000030   07bd005a           ADD.L2        8,B15,B15
00000034   00000000           NOP
00000038   00000000           NOP
0000003c   00000000           NOP

损坏的零件:

00000018  023c4286           LDHU.D2T2    *+B15[2]、B4
0000001c  e6108000          .fphead      p、l、W、BU、br nosat、0110000b
00000020  0013ea5a          CMPEG.L2      -1、B4、B0

LDHU (从存储器加载半字-无符号扩展)与-1的比较的组合显然是不正确的。

如果设置了枚举值中的最高位、则会触发错误。 也就是说、如果相同大小的有符号值为负。

如果使用比"-Ooff"更高的"-O"设置、则代码被正确编译。

如果枚举类更改为(未定义范围的)枚举、错误合规性也会消失。

类似的枚举定义避免了不合规情况:

enum A : unsigned short
{
    A1 = 0xFFFFu
};

  • 请注意,本文内容源自机器翻译,可能存在语法或其它翻译错误,仅供参考。如需获取准确内容,请参阅链接中的英语原文或自行翻译。

    感谢您通知我们此问题并提交一个简短的测试用例。  我能够重现相同的行为。  我提交了 EXT_EP-12185条目 、以对此进行调查。  我们欢迎您通过这个链接来了解。

    谢谢。此致、

    -George.