Bug 5922 – inline assembler - referencing immutable variable values fails

Status
RESOLVED
Resolution
FIXED
Severity
normal
Priority
P2
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2011-05-03T03:04:41Z
Last change time
2020-08-22T11:35:46Z
Keywords
iasm, pull
Assigned to
No Owner
Creator
bearophile_hugs
See also
https://issues.dlang.org/show_bug.cgi?id=21186

Comments

Comment #0 by bearophile_hugs — 2011-05-03T03:04:41Z
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.
Comment #1 by verylonglogin.reg — 2012-03-02T06:37:11Z
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.
Comment #2 by yebblies — 2013-01-16T05:04:57Z
For some reason x's initializer is "x = 10" instead of just "10". Probably a side effect of some hack.
Comment #3 by bugzilla — 2020-08-22T00:18:54Z
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.
Comment #4 by dlang-bot — 2020-08-22T05:32:54Z
@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
Comment #5 by dlang-bot — 2020-08-22T11:35:46Z
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