0

Is it possible to have volatile labels? Something like this:

volatile coroutine:

or

coroutine volatile:

or maybe even

coroutine: volatile

I want a label that won't be touched by optimizations. Is that possible? GCC extensions are acceptable.

Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574
hauzer
  • 258
  • 3
  • 12
  • 3
    Interesting [XY](http://meta.stackexchange.com/questions/66377/what-is-the-xy-problem) problem. The real question is is how to make "a label that won't be touched by optimizations". OP poses a solution, which is being knocked down, maybe rightful so, but the real question itself has merit. – chux - Reinstate Monica Oct 09 '13 at 17:53
  • 1
    Can you provide an example with a label which is touched by optimizations? Because it looks impossible to me.. – Mihai Maruseac Oct 09 '13 at 17:58
  • 2
    In my opinion, the [revision 2](https://stackoverflow.com/revisions/19278400/2) edit of the question should not have been approved, because it invalidates existing answers. Therefore, I believe it was correct to rollback to revision 1. – Andreas Wenzel Jun 04 '22 at 11:39
  • (the question is discussed on meta - https://meta.stackoverflow.com/questions/418618/re-open-closed-question-with-incorrect-answer) – Alexei Levenkov Jun 09 '22 at 20:48

2 Answers2

0

The meaning of the volatile qualifier is specified in C 2011 (N1570) 5.1.2.3 6: “Accesses to volatile objects are evaluated strictly according to the rules of the abstract machine.… This is the observable behavior of the program.”

This specification has no applicability to labels, since labels are not objects, nor are labels accessed.

In theory, the language could be extended so that volatile could apply to labels. To do this, you would need to specify what it means. Saying it means they “are not to be tampered by compiler optimizations” is insufficient because it is unclear, uses undefined terms (“tampered”), and uses inapplicable terms (The C standard applies to C implementations, not to compilers. The word “compiler” does not even appear in the normative part of the standard.).

Eric Postpischil
  • 195,579
  • 13
  • 168
  • 312
  • Thanks for a standard-complying answer. I *have* mentioned possible GCC extensions as being acceptable, obviously not expecting answers that draw their inspiration from the standard. There's no need for nitpicking, we all know what "compilers", "optimizations", and “are not to be tampered by compiler optimizations” mean in the real world. – hauzer Oct 09 '13 at 17:47
  • @hauzer: These are not nits. Programming computers requires being precise. A working optimizer does not “tamper” with labels; it implements their semantics fully and completely as specified by the standard. What you want to do is prohibit some optimizations. But we do not know which ones. You must specify what semantics you want. You could start by giving specific examples of behaviors that would constitute “tampering” with a label that would otherwise be conforming behavior in C. – Eric Postpischil Oct 09 '13 at 18:21
-1

Short answer: NO.

Longer answer: labels are there only to specify jump targets. They are not to be kept in registers or in cache memory (the thing where volatile is involved).

Mihai Maruseac
  • 20,967
  • 7
  • 57
  • 109
  • As far as I understand, `volatile` marks something as essential; not to be tampered by compiler optimizations. Why is that not applicable to labels? They may not be stored in registers or memory, but they do *reference* memory. – hauzer Oct 09 '13 at 17:16
  • They do reference memroy from the *text* segment (read-only). You cannot change them. Volatile is used to allow changes to be propagated as fast as possible to all readers of the same value. – Mihai Maruseac Oct 09 '13 at 17:18
  • Why does the compiler care? If I want something marked as volatile, I should be able to make it so. That's the whole point of it; the compiler shouldn't interfere. But alright, if nothing else comes up, I'll accept your answer. – hauzer Oct 09 '13 at 17:21
  • 1
    @hauzer: declaring an address `extern` comes down to making it untouchable to compiler's machinations -- i.e., I don't believe extern functions can be inlined or optimized away. So as long as you are mixing C keywords in any way you *feel* "should be able", `extern` would make a far better choice than `volatile`. $64,000 question follows: can you declare a *label* "extern"? .. If you really, really wanted to? – Jongware Oct 09 '13 at 19:45
  • 2
    @Jongware: External functions can be inlined as long as an implementation exists that is visible externally. (That is, the compiler may provide both an inlined version and an external version.) Furthermore, externally visible functions may be eliminated at link time if they are not used. The result may be a function declared external that has only inline implementations. – Eric Postpischil Oct 10 '13 at 15:33