See code blow :
static uint globalThreadLocal = 123456;
uint foo() {
asm {
naked;
mov EAX, globalThreadLocal;
ret;
}
}
void main() {
uint x = foo();
import std.stdio;
writeln(x);
}
Output garbage. This code should generate an error and not compile as if it was correct.
Comment #1 by yebblies — 2012-03-17T17:21:41Z
It's not that simple. IIRC "mov EAX, globalThreadLocal" moves the offset of globalThreadLocal into EAX. While this obviously isn't what you expected, this is valid and useful. How else would you get the offset?
After all, you're using the inline assembler. The compiler will never be able to protect you against writing incorrect assembly.
Comment #2 by deadalnix — 2012-03-18T06:04:39Z
(In reply to comment #1)
> It's not that simple. IIRC "mov EAX, globalThreadLocal" moves the offset of
> globalThreadLocal into EAX. While this obviously isn't what you expected, this
> is valid and useful. How else would you get the offset?
>
> After all, you're using the inline assembler. The compiler will never be able
> to protect you against writing incorrect assembly.
In this case, this behavior should be documented.
But this is confusing, because it is not what happen with other variables (variable value is copied into the register).
Comment #3 by k.hara.pg — 2015-01-06T15:09:15Z
See also issue 13938.
Comment #4 by dlang-bugzilla — 2017-07-18T13:47:25Z
No longer compiles since https://github.com/dlang/dmd/pull/4260, with error message "cannot directly load TLS variable 'globalThreadLocal'".
*** This issue has been marked as a duplicate of issue 13938 ***