| Thread overview | ||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
June 01, 2009 Automatic void initialization | ||||
|---|---|---|---|---|
| ||||
Hello if I have the following code: int foo; foo = 5; When the variable foo is declared, it is initialized to int.init, or has garbage contents until it is assigned? Thanks | ||||
June 01, 2009 Re: Automatic void initialization | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Fractal | Fractal wrote:
> Hello
>
> if I have the following code:
>
> int foo;
> foo = 5;
>
> When the variable foo is declared, it is initialized to int.init, or has garbage contents until it is assigned?
>
> Thanks
D will always initialize variables unless you explicitly tell it not to. (although a smart compiler may optimize certain cases)
Here's how to get foo initialized to garbage:
int foo = void;
foo = 5;
| |||
June 01, 2009 Re: Automatic void initialization | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Fractal | Hello Fractal,
> Hello
>
> if I have the following code:
>
> int foo;
> foo = 5;
> When the variable foo is declared, it is initialized to int.init, or
> has garbage contents until it is assigned?
>
> Thanks
>
int.init unless the compiler rewrites it as
int foo = 5;
and then 5.
| |||
June 02, 2009 Re: Automatic void initialization | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Fractal | Fractal wrote:
> if I have the following code:
>
> int foo; foo = 5;
>
> When the variable foo is declared, it is initialized to int.init, or
> has garbage contents until it is assigned?
Here's the easy way to find out:
int bar()
{ int foo;
foo = 5;
return foo;
}
compile with:
dmd -c test -O -release
and disassemble with:
obj2asm test.obj
to show the generated code is:
_D4test3barFZi comdat
mov EAX,5
ret
| |||
June 02, 2009 Re: Automatic void initialization | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Jason House | Jason House wrote:
> D will always initialize variables unless you explicitly tell it not to. (although a smart compiler may optimize certain cases)
Dead assignment elimination is compiler technology from the 70's !
| |||
June 02, 2009 Re: Automatic void initialization | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Walter Bright | Walter Bright:
>Dead assignment elimination is compiler technology from the 70's !<
I'd like to see this technology used for arrays too. So in the following two adjacent lines of code "a" doesn't get initialized to zero before being initialized again to 5:
void main() {
auto a = new int[10];
a[] = 5;
printf("%d\n", a[3]);
}
The ASM:
sub ESP,01Ch
mov EAX,offset FLAT:_D11TypeInfo_Ai6__initZ
push 0Ah
push EAX
call near ptr __d_newarrayT
mov 0Ch[ESP],EAX
mov 010h[ESP],EDX
push dword ptr 0Ch[ESP]
push 5
push EDX
mov 014h[ESP],EDX
call near ptr __memset32
mov ECX,014h[ESP]
mov EDX,offset FLAT:_DATA
push dword ptr 0Ch[ECX]
push EDX
call near ptr _printf
[...]
Bye,
bearophile
| |||
June 02, 2009 Re: Automatic void initialization | ||||
|---|---|---|---|---|
| ||||
Posted in reply to bearophile | On Tue, Jun 2, 2009 at 8:10 AM, bearophile <bearophileHUGS@lycos.com> wrote:
> Walter Bright:
>>Dead assignment elimination is compiler technology from the 70's !<
>
> I'd like to see this technology used for arrays too. So in the following two adjacent lines of code "a" doesn't get initialized to zero before being initialized again to 5:
>
> void main() {
> auto a = new int[10];
> a[] = 5;
> printf("%d\n", a[3]);
> }
>
> The ASM:
>
> sub ESP,01Ch
> mov EAX,offset FLAT:_D11TypeInfo_Ai6__initZ
> push 0Ah
> push EAX
> call near ptr __d_newarrayT
> mov 0Ch[ESP],EAX
> mov 010h[ESP],EDX
> push dword ptr 0Ch[ESP]
> push 5
> push EDX
> mov 014h[ESP],EDX
> call near ptr __memset32
> mov ECX,014h[ESP]
> mov EDX,offset FLAT:_DATA
> push dword ptr 0Ch[ECX]
> push EDX
> call near ptr _printf
> [...]
As far as I know, LDC already does this.
| |||
June 02, 2009 Re: Automatic void initialization | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Jarrett Billingsley | Jarrett Billingsley wrote:
> On Tue, Jun 2, 2009 at 8:10 AM, bearophile <bearophileHUGS@lycos.com> wrote:
>> Walter Bright:
>>> Dead assignment elimination is compiler technology from the 70's !<
>> I'd like to see this technology used for arrays too. So in the following two adjacent lines of code "a" doesn't get initialized to zero before being initialized again to 5:
>>
>> void main() {
>> auto a = new int[10];
>> a[] = 5;
>> printf("%d\n", a[3]);
>> }
>
> As far as I know, LDC already does this.
It doesn't. The runtime call that allocates the array initializes it (to 0 in this case), and then the array assignment overwrites that initialization.
In this case the array doesn't escape the function, so it does get stack-allocated by a custom optimization pass (and the initialization is turned into an LLVM memset intrinsic), but LLVM still can't "look into" the call that does the array assignment to know it will always overwrite the entire array so the memset isn't necessary.
| |||
June 02, 2009 Re: Automatic void initialization | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Frits van Bommel | On Tue, Jun 2, 2009 at 10:41 AM, Frits van Bommel <fvbommel@remwovexcapss.nl> wrote:
> Jarrett Billingsley wrote:
>>
>> On Tue, Jun 2, 2009 at 8:10 AM, bearophile <bearophileHUGS@lycos.com> wrote:
>>>
>>> Walter Bright:
>>>>
>>>> Dead assignment elimination is compiler technology from the 70's !<
>>>
>>> I'd like to see this technology used for arrays too. So in the following two adjacent lines of code "a" doesn't get initialized to zero before being initialized again to 5:
>>>
>>> void main() {
>>> auto a = new int[10];
>>> a[] = 5;
>>> printf("%d\n", a[3]);
>>> }
>>
>> As far as I know, LDC already does this.
>
> It doesn't. The runtime call that allocates the array initializes it (to 0 in this case), and then the array assignment overwrites that initialization.
>
> In this case the array doesn't escape the function, so it does get stack-allocated by a custom optimization pass (and the initialization is turned into an LLVM memset intrinsic), but LLVM still can't "look into" the call that does the array assignment to know it will always overwrite the entire array so the memset isn't necessary.
I thought I remember seeing a runtime function to allocate an array without initializing it.. maybe it's just not used yet?
| |||
June 02, 2009 Re: Automatic void initialization | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Jarrett Billingsley | Jarrett Billingsley wrote:
> I thought I remember seeing a runtime function to allocate an array
> without initializing it.. maybe it's just not used yet?
Oh, the function's there. It's just not used for that purpose :P.
It's used for array concatenations and array literals, where the compiler can tell from a single expression that the default initialization will always be useless since it'll be overwritten immediately.
| |||
Copyright © 1999-2021 by the D Language Foundation
Permalink
Reply