This code works in DMD v1.047 and fails in DMD v2.035
import std.stdio;
static int value1;
void main() {
value1 = 2;
asm {
mov EAX,1;
mov [value1],EAX;
}
writefln("value1 %d", value1);
}
The code generated in each case is:
Compiled under DMD v 1.047:
> u __Dmain l39
TEST03!__Dmain+0x0:
0x00402010 55 push ebp
0x00402011 8bec mov ebp,esp
0x00402013 c705801445000200
0000 mov dword ptr [_D6test036value1i (00451480)],00000002
0x0040201d b801000000 mov eax,00000001
0x00402022 a380144500 mov dword ptr [_D6test036value1i (00451480)],eax
0x00402027 ff3580144500 push dword ptr [_D6test036value1i (00451480)]
0x0040202d ff3594b04400 push dword ptr [_TMP0+00000004 (0044b094)]
0x00402033 ff3590b04400 push dword ptr [_TMP0 (0044b090)]
0x00402039 b8f0b04400 mov eax,0044b0f0
0x0040203e 50 push eax
0x0040203f e830170000 call _D3std5stdio8writeflnFYv (00403774)
0x00402044 83c410 add esp,10
0x00402047 5d pop ebp
0x00402048 c3 retn
>
Compiled under DMD v 2.0035:
> u __Dmain l43
TEST03!__Dmain+0x0:
0x00402010 55 push ebp
0x00402011 8bec mov ebp,esp
0x00402013 53 push ebx
0x00402014 64a12c000000 mov eax,dword ptr fs:[0000002c]
0x0040201a 8b08 mov ecx,dword ptr [eax]
0x0040201c c781040000000200
0000 mov dword ptr [ecx+00000004],00000002
0x00402026 b801000000 mov eax,00000001
0x0040202b a304000000 mov dword ptr [00000004],eax
0x00402030 ff3594904200 push dword ptr [_TMP0+00000004 (00429094)]
0x00402036 ff3590904200 push dword ptr [_TMP0 (00429090)]
0x0040203c 648b152c000000 mov edx,dword ptr fs:[0000002c]
0x00402043 8b1a mov ebx,dword ptr [edx]
0x00402045 8b8304000000 mov eax,dword ptr [ebx+00000004]
0x0040204b e804000000 call _D3std5stdio19__T8writeflnTAyaTiZ8writeflnFAyaiZv (00402054)
0x00402050 5b pop ebx
0x00402051 5d pop ebp
0x00402052 c3 retn
>
The "D" code accesses 'value1' as dword ptr [ecx+00000004] and the inline assember as dword ptr [00000004]
Is this a change in the language runtime specification for how module static variables are accessed or it this a bug ?
Comment #1 by clugdbug — 2009-10-19T08:37:32Z
It's a change. statics are now thread-local by default. You need to mark as __gshared if you want the D1 behaviour.
Comment #2 by grahamc001uk — 2009-10-20T15:29:29Z
Thanks.
Is there any way to put __gshared or immutable into a typedef or alias so that conditional compilation can allow code to compile with both DMD v1 and DMD v2 ?
I thought something like this would do it:
import std.stdio;
static __gshared uint value1 = 3;
static immutable uint value2 = 4;
version(D_Version2) {
mixin("typedef static __gshared uint __gshared_uint;");
mixin("typedef static immutable uint immutable_uint;");
}
else {
typedef static uint __gshared_uint;
typedef static uint immutable_uint;
}
__gshared_uint value3 = 5;
immutable_uint value4 = 6;
void main() {
uint save1, save2, save3, save4;
asm {
mov EAX,value1;
mov save1,EAX;
mov EAX,value2;
mov save2,EAX;
mov EAX,value3;
mov save3,EAX;
mov EAX,value4;
mov save4,EAX;
}
writefln("save1 %d save2 %d save3 %d save4 %d", save1, save2, save3, save4);
}
but it does not:
dmd -vtls test03
test03.d(15): value3 is thread local
test03.d(16): value4 is thread local
Comment #3 by clugdbug — 2010-01-31T12:17:37Z
Marking this as invalid, since it is an intentional change between D1 and D2.