immutable, static, __gshared, TLS, and compile time allocation

Andrej Mitrovic andrej.mitrovich at gmail.com
Fri Apr 20 15:17:48 PDT 2012


On 4/20/12, Manu <turkeyman at gmail.com> wrote:
> I need to clarify some things that have confused me a few times.

When in doubt you can either do a few write calls:

import std.stdio;
import std.concurrency;

struct FooStruct
{
    static int FooX;
    __gshared int FooY;
}

void test()
{
    writeln(FooStruct.FooX);
    writeln(FooStruct.FooY);
}

void main()
{
    writeln(FooStruct.FooX);
    writeln(FooStruct.FooY);
    FooStruct.FooX = 1;
    FooStruct.FooY = 1;

    spawn(&test);
}

Or better yet look at the disassembly:

Here's FooX:
SECTION .tls$   align=16 noexecute                      ; section number 8, data
_D4test9FooStruct4FooXi:                                ; dword
        dd 00000000H                                    ; 0000 _ 0

Here's how it's loaded:
mov     eax, dword [__tls_index]                ; 0003 _ A1, 00000000(segrel)
mov     ecx, dword [fs:__tls_array]             ; 0008 _ 64: 8B. 0D,
00000000(segrel)
mov     edx, dword [ecx+eax*4]                  ; 000F _ 8B. 14 81
mov     eax, dword [_D4test9FooStruct4FooXi+edx]; 0012 _ 8B. 82, 00000000

And here's FooY:
SECTION .BSS    align=16 noexecute                      ; section number 4, bss
_D4test9FooStruct4FooYi:                                ; dword
        resd    1                                       ; 0004

And here's how it's loaded (notice the lack of having to calculate the
address based on TLS:
mov     eax, dword [_D4test9FooStruct4FooYi]    ; 001D _ A1, 00000004(segrel)

Adding static before __gshared doesn't seem to have any effect. DMD
ignores a lot of nonsense keyword combinations. Maybe this will
improve one day..


More information about the Digitalmars-d mailing list