D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 5922 - inline assembler - referencing immutable variable values fails
Summary: inline assembler - referencing immutable variable values fails
Status: RESOLVED FIXED
Alias: None
Product: D
Classification: Unclassified
Component: dmd (show other issues)
Version: D2
Hardware: All All
: P2 normal
Assignee: No Owner
URL:
Keywords: iasm, pull
Depends on:
Blocks:
 
Reported: 2011-05-03 03:04 UTC by bearophile_hugs
Modified: 2020-08-22 11:35 UTC (History)
3 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description bearophile_hugs 2011-05-03 03:04:41 UTC
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 Denis Shelomovskii 2012-03-02 06:37:11 UTC
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 yebblies 2013-01-16 05:04:57 UTC
For some reason x's initializer is "x = 10" instead of just "10".  Probably a side effect of some hack.
Comment 3 Walter Bright 2020-08-22 00:18:54 UTC
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 Dlang Bot 2020-08-22 05:32:54 UTC
@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 Dlang Bot 2020-08-22 11:35:46 UTC
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