I am not sure about the errors shown here (so no keywords). Please close this bug report if things here are working as expected. D2 code: void main() { immutable size_t x = 10; asm { mov EDI, x; } } DMD 2.052 shows the error: test.d(2): Error: Integer constant expression expected instead of x = 10u -------------------------- D2 code, static (thread-local) variables can't be used in ASM: void main() { static size_t x = 10; asm { mov EDI, x; } } It produces: object.Error: Access Violation They work using __gshared, this gives no errors: void main() { __gshared static size_t x = 10; asm { mov EDI, x; } } My suggestion is to remove this source of errors. One solution may be to disallow the direct access to static variables from asm code, avoiding this bug.
The first one is definetly a bug with const/immutable local variables: --- void f() { const size_t a = 1; // Error: Integer constant expression expected instead of a = 1u version (D_InlineAsm_X86_64) asm { mov RAX, a; } else version (D_InlineAsm_X86) asm { mov EAX, a; } else static assert(1); } --- The second one looks like expected behaviour because with current inline assembler for non-stack varable `someVar` instruction `mov EAX, someVar;` is equal to `mov EAX, [someVar];` and `mov EAX, [n];` where `n` is a displacement of `someVar` from some initial addres which depends on `someVar` type (global, shared, or TLS). Inline assembler behaves in this way but I've never seen any documentation and reasons *why* does it behave this way because it's really unobvious.
For some reason x's initializer is "x = 10" instead of just "10". Probably a side effect of some hack.
In reply to bearophile_hugs from comment #0) > -------------------------- > > D2 code, static (thread-local) variables can't be used in ASM: > > [...] Moved to https://issues.dlang.org/show_bug.cgi?id=21186 as it is a different bug.
@WalterBright created dlang/dmd pull request #11605 "fix Issue 5922 - inline assembler - referencing immutable variable va…" fixing this issue: - fix Issue 5922 - inline assembler - referencing immutable variable values fails https://github.com/dlang/dmd/pull/11605
dlang/dmd pull request #11605 "fix Issue 5922 - inline assembler - referencing immutable variable va…" was merged into master: - 45303db7ab4f9ed2beb1a5f3d33cb8a89f47f388 by Walter Bright: fix Issue 5922 - inline assembler - referencing immutable variable values fails https://github.com/dlang/dmd/pull/11605