John Regehr
2010-Mar-06 04:47 UTC
[LLVMdev] expression statements, volatiles, and C vs. C++
The question is, what should C and C++ compilers do with this code? volatile int x; void foo (void) { x; } This question is not totally stupid: embedded systems use code like this when reading a hardware register has a useful side effect (usually clearing the register). It is reasonably clear that a C compiler should load from x and throw away the value. clang and llvm-gcc do this, as do most other good C compilers. However, clang++ and llvm-g++ also load from x and this does not appear to be supported by the 1998 C++ standard. In 6.2, it is explicitly stated that for an expression statement, no conversion from lvalue to rvalue occurs. If there's no rvalue, there should not be a load from x. Anyway, I'm curious: is the C-like interpretation of a volatile expression statement considered to be a feature by the LLVM maintainers? If so, what is the rationale? I haven't done extensive testing, but there do exist compiler families (such as those from IAR and Intel) where the C compiler loads from x and the C++ compiler does not. [I just sent a very message very much like this one to the GCC mailing list.] Thanks, John Regehr
Conrado Miranda
2010-Mar-06 15:12 UTC
[LLVMdev] expression statements, volatiles, and C vs. C++
LLVM doesn't know about any of this C/C++ specific stuff. If g++ do something (such as the load), so will llvm-g++. I'd suggest you send this to the clang mailing list. BTW, this issue is very interesting. I've never used C++ for embedded systems so never realised this difference. On Sat, Mar 6, 2010 at 1:47 AM, John Regehr <regehr at cs.utah.edu> wrote:> The question is, what should C and C++ compilers do with this code? > > volatile int x; > > void foo (void) { > x; > } > > This question is not totally stupid: embedded systems use code like this > when reading a hardware register has a useful side effect (usually > clearing the register). > > It is reasonably clear that a C compiler should load from x and throw > away the value. clang and llvm-gcc do this, as do most other good C > compilers. > > However, clang++ and llvm-g++ also load from x and this does not appear > to be supported by the 1998 C++ standard. In 6.2, it is explicitly > stated that for an expression statement, no conversion from lvalue to > rvalue occurs. If there's no rvalue, there should not be a load from x. > > Anyway, I'm curious: is the C-like interpretation of a volatile > expression statement considered to be a feature by the LLVM maintainers? > If so, what is the rationale? > > I haven't done extensive testing, but there do exist compiler families > (such as those from IAR and Intel) where the C compiler loads from x and > the C++ compiler does not. > > [I just sent a very message very much like this one to the GCC mailing > list.] > > Thanks, > > John Regehr > _______________________________________________ > LLVM Developers mailing list > LLVMdev at cs.uiuc.edu http://llvm.cs.uiuc.edu > http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev >