English 中文(简体)
当优化在IAR MSP430编译器中设置为高时,会发生指令的疯狂执行
原标题:Wild execution of instructions happen when optimization set to high in IAR MSP430 compiler
  • 时间:2011-06-01 11:22:59
  •  标签:
  • c

请参阅下面的代码。当IAR MSP430编译器中的优化设置为高时,我会遇到以下问题。当优化程度较低时,代码运行良好。

问题:如果(B)处的条件语句返回false,则执行语句(A)而不是语句(C)。


int16_t cpu_flash_read_setting (void * setting, const uint8_t offset, const uint8_t num_of_bytes)
{
    int16_t returnable_status = PASS;
    uint16_t flash_copy_one_address =  FLASH_INFO_SEG_C_ADDR + offset;
    uint16_t flash_copy_two_address =  FLASH_INFO_SEG_D_ADDR + offset;
    if (0U == (num_of_bytes % sizeof(uint16_t))) 
    {
        uint16_t *setting_copy_one = (uint16_t *) flash_copy_one_address;
        uint16_t *setting_copy_two = (uint16_t *) flash_copy_two_address;  
        if (*setting_copy_one == *setting_copy_two)
        {
            setting = setting_copy_one;       
        }
        else
        {
(A)         returnable_status = FAIL;         
        }
    }
    else if (0U == (num_of_bytes % sizeof(uint8_t))) 
    {
        uint8_t *setting_copy_one = (uint8_t *) flash_copy_one_address;
        uint8_t *setting_copy_two = (uint8_t *) flash_copy_two_address;  
(B)     if (*setting_copy_one == *setting_copy_two)
        {
            setting = setting_copy_one;       
        }
        else
        {
(C)        returnable_status = FAIL;         
        }      
    }
    else
    {
        /* No Action */
    }
    return returnable_status;    
}
最佳回答

这在我看来是完全合理的。当优化率很高时,编译器可以而且通常会疯狂地重新排序语句。除了类型之外,您的两个主要子句是相同的,因此编译器完全可以合并执行路径,并仅在实际重要的地方使它们不同。

只有当实际可观察到的效果与预期不同时,这才是一个问题。

在任何情况下,优化的代码总是很难用调试器执行,这正是因为重新排序的影响。

顺便说一句,如果您的代码与实际硬件对话,您可能需要将flash_copy_*_address变量声明为volatile。这向编译器暗示,他们指向的内存不一定以正常方式运行,并迫使它在优化时更加保守。

问题回答

代码AC的两行是相同的,执行路径在这两行之后合并(在这两种情况下要执行的下一行是return returnable_status;

因此,编译器正在执行尾部合并优化,对两个源代码行使用相同的汇编代码块。这种优化是意料之中的,完全有效,不应造成问题。





相关问题
Fastest method for running a binary search on a file in C?

For example, let s say I want to find a particular word or number in a file. The contents are in sorted order (obviously). Since I want to run a binary search on the file, it seems like a real waste ...

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

Tips for debugging a made-for-linux application on windows?

I m trying to find the source of a bug I have found in an open-source application. I have managed to get a build up and running on my Windows machine, but I m having trouble finding the spot in the ...

Trying to split by two delimiters and it doesn t work - C

I wrote below code to readin line by line from stdin ex. city=Boston;city=New York;city=Chicago and then split each line by ; delimiter and print each record. Then in yet another loop I try to ...

Good, free, easy-to-use C graphics libraries? [closed]

I was wondering if there were any good free graphics libraries for C that are easy to use? It s for plotting 2d and 3d graphs and then saving to a file. It s on a Linux system and there s no gnuplot ...

Encoding, decoding an integer to a char array

Please note that this is not homework and i did search before starting this new thread. I got Store an int in a char array? I was looking for an answer but didn t get any satisfactory answer in the ...

热门标签