D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 7720 - asm silent wrong code generation
Summary: asm silent wrong code generation
Status: RESOLVED DUPLICATE of issue 13938
Alias: None
Product: D
Classification: Unclassified
Component: dmd (show other issues)
Version: D2
Hardware: All All
: P2 normal
Assignee: No Owner
URL:
Keywords: iasm
Depends on:
Blocks:
 
Reported: 2012-03-17 12:56 UTC by deadalnix
Modified: 2017-07-18 13:47 UTC (History)
1 user (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description deadalnix 2012-03-17 12:56:07 UTC
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 yebblies 2012-03-17 17:21:41 UTC
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 deadalnix 2012-03-18 06:04:39 UTC
(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 Kenji Hara 2015-01-06 15:09:15 UTC
See also issue 13938.
Comment 4 Vladimir Panteleev 2017-07-18 13:47:25 UTC
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 ***