Notes |
|
|
Does the variable appear amongst the list of exported symbols?
What version of gcc are you using? (Recent ones do perform some LTO, so maybe
that's the problem?) |
|
|
|
The problem is that the static variable MR_grade that we emit at the end of every generated .c file is being optimised away (since it is not referenced by anything else). GCC and clang seem quite happy to do this even if the variable is also declared to be volatile. The will however retain the variable if it is declared with
the variable attribute __attribute__((used)). For other C compilers I'm not yet sure what to do (or even if this is a problem). |
|
|
(0000893)
|
zs
|
2016-07-07 20:27
|
|
We could generate an exported function that returns the value of MR_grade.
Since it could be called from somewhere else, the compiler wouldn't be able
to optimize away the reference. This should be more portable than __attribute__. |
|
|
|
We can hide __attribute__ behind a macro, just as we already do with other such attributes, e.g.
#if defined(MR_GNUC) || defined(MR_CLANG)
#define MR_CONSIDER_USED __attribute__((used))
#else
#define MR_CONSIDER_USED volatile /* .. and hope for the best! */
#endif
I've implemented this and tested it with clang. (I'm not even sure that this is an issue with
MSVC; I'll take a look tomorrow when I have access to a machine with that compiler installed.)
Given that C compilers used with Mercury are nearly always GCC or clang I think this is probably
good enough. |
|
|
|
|