仅包含标识符的表达式是否会读取易失性变量?

发布于 2024-10-30 19:12:29 字数 351 浏览 1 评论 0原文

我正在使用 Microchip 的编译器为微控制器(dsPIC24)编写代码。外围设备都是内存映射变量。有时需要通过读取缓冲区来清除缓冲区,我通常这样做:

SPI1BUF;

...其中 SPI1BUF 在 Microchip 的标头中声明为 volatile char。它对我有用,但现在我很好奇:这是标准化行为吗?我记得如果我执行一个赋值:

unsigned char x = SPI1BUF;

...标准规定必须读取易失性变量。但是对于标识符作为整体表达式的情况也是如此吗?

I'm writing for a microcontroller (dsPIC24) using Microchip's compiler. The peripheral devices are all memory-mapped variables. Occasionally it is necessary to clear a buffer by reading from it, which I usually do with just:

SPI1BUF;

...where SPI1BUF is declared volatile char in Microchip's headers. It works for me, but now I'm curious: is it standardised behaviour? I recall that if I perform an assignment:

unsigned char x = SPI1BUF;

...the standard says that the volatile variable must be read. But is this also true for the identifier-as-the-whole-expression case?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

旧人 2024-11-06 19:12:29

这种裸表达式是C标准中称为“表达式语句”的语句类型。标准(6.8.3)的相关部分说:

表达式中的表达式
语句被评估为无效
表达其副作用。

读取 volatile 限定变量的值被认为是一种副作用,因此根据第 §5.1.2.3 节,它不能被忽略:

访问易失性对象、修改对象、修改文件,或者
调用执行任何这些操作的函数都是侧面的
效果,即执行环境状态的变化。

假设 SPI1BUF 是左值(如果它是标识符,或者扩展为调用一元 * 运算符的宏,则都是如此),§6.3.2.1有这样说:

除非它是 sizeof 运算符的操作数,一元 &
运算符、++ 运算符、-- 运算符或左操作数
. 运算符或赋值运算符,左值不
将数组类型转换为指定值存储
对象(并且不再是左值)。

由于没有任何异常适用,这确保了底层(易失性)对象是“已访问”的,因为左值已转换为存储在对象中的值,这显然需要访问那个物体。

总之:是的,这是标准行为。

Such a bare expression is a statement type called an "expression statement" in the C standard. The relevant section of the standard (6.8.3) says:

The expression in an expression
statement is evaluated as a void
expression for its side effects.

Reading the value of a volatile-qualified variable is considered such a side effect, so it can't be elided, per §5.1.2.3:

Accessing a volatile object, modifying an object, modifying a file, or
calling a function that does any of those operations are all side
effects, which are changes in the state of the execution environment.

Assuming that SPI1BUF is an lvalue (this is true both if it is an identifier, or a macro that expands to an invocation of the unary * operator), §6.3.2.1 has this to say:

Except when it is the operand of the sizeof operator, the unary &
operator, the ++ operator, the -- operator, or the left operand of
the . operator or an assignment operator, an lvalue that does not
have array type is converted to the value stored in the designated
object (and is no longer an lvalue).

Since none of the exceptions apply, this assures us that the underlying (volatile) object is "Accessed", because the lvalue has been converted to the value stored in the object, which obviously necessitates accessing the value of that object.

In summary: yes, this is standard behaviour.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文