首页 > 解决方案 > Why is it OK to initialize global variable with result from inline assembly code?

问题描述

Consider this code which would compile on PIC32 (source: openscope-mz)

static  uint32_t    tSLoop              = ReadCoreTimer();

static inline uint32_t ReadCoreTimer(void)
{
    uint32_t coreTimerCount;
    __asm__ __volatile__("mfc0 %0,$9" : "=r" (coreTimerCount));
    return(coreTimerCount);
}

Please explain why this is valid C code.

标签: c++inline-assembly

解决方案


不,这是没有实现定义扩展的无效 C 代码。静态变量的初始化必须是常量表达式。它指定了什么是常量表达式 - 函数的结果不在该列表中。C 标准允许实现接受其他形式的常量表达式,因此通过实现定义的扩展来接受此类语句,它可能是有效的 C 代码。无论如何,我怀疑情况并非如此。

线

static  uint32_t    tSLoop              = ReadCoreTimer();

来自LoopStats.cpp,它是一个 C++ 文件。


推荐阅读