digitalmars.D - Automatic void initialization
- Fractal (6/6) May 31 2009 Hello
- Jason House (6/17) May 31 2009 D will always initialize variables unless you explicitly tell it not to....
- Walter Bright (2/4) Jun 02 2009 Dead assignment elimination is compiler technology from the 70's !
- bearophile (28/29) Jun 02 2009 I'd like to see this technology used for arrays too. So in the following...
- Jarrett Billingsley (5/32) Jun 02 2009 two adjacent lines of code "a" doesn't get initialized to zero before be...
- Frits van Bommel (8/20) Jun 02 2009 It doesn't. The runtime call that allocates the array initializes it (to...
- Jarrett Billingsley (9/36) Jun 02 2009 g
- Frits van Bommel (5/7) Jun 02 2009 Oh, the function's there. It's just not used for that purpose :P.
- bearophile (9/11) Jun 02 2009 Splitting the array allocation and the memset32, a successive optimizati...
- Walter Bright (4/9) Jun 02 2009 Although the technology didn't make it to the PC until 1985 with the
- Denis Koroskin (2/10) Jun 02 2009
- Walter Bright (2/6) Jun 02 2009 Yes.
- BCS (4/15) May 31 2009 int.init unless the compiler rewrites it as
- Walter Bright (15/21) Jun 02 2009 Here's the easy way to find out:
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
May 31 2009
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? ThanksD 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;
May 31 2009
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 !
Jun 02 2009
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
Jun 02 2009
On Tue, Jun 2, 2009 at 8:10 AM, bearophile <bearophileHUGS lycos.com> wrote= :Walter Bright:two adjacent lines of code "a" doesn't get initialized to zero before being= initialized again to 5: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 =void main() { =A0 =A0auto a =3D new int[10]; =A0 =A0a[] =3D 5; =A0 =A0printf("%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.
Jun 02 2009
Jarrett Billingsley wrote:On Tue, Jun 2, 2009 at 8:10 AM, bearophile <bearophileHUGS lycos.com> wrote: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.Walter Bright:As far as I know, LDC already does this.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]); }
Jun 02 2009
On Tue, Jun 2, 2009 at 10:41 AM, Frits van Bommel <fvbommel remwovexcapss.nl> wrote:Jarrett Billingsley wrote:gOn 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 followin=eingtwo adjacent lines of code "a" doesn't get initialized to zero before b=0It doesn't. The runtime call that allocates the array initializes it (to =initialized again to 5: void main() { =A0 auto a =3D new int[10]; =A0 a[] =3D 5; =A0 printf("%d\n", a[3]); }As far as I know, LDC already does this.in this case), and then the array assignment overwrites that initializati=on.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" t=hecall 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?
Jun 02 2009
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.
Jun 02 2009
Jarrett Billingsley:I thought I remember seeing a runtime function to allocate an array without initializing it.. maybe it's just not used yet?Splitting the array allocation and the memset32, a successive optimization pass of the compiles may be able to simplify code like: auto a = new int[n]; a[] = 10; a[] = 20; In a single allocation + one memset32, instead of allocation + 3 memsets32. But in most situations this isn't a critical optimization, so it's low priority. Bye, bearophile
Jun 02 2009
Walter Bright wrote:Jason House wrote:Although the technology didn't make it to the PC until 1985 with the release of Datalight Optimum C. But still, that's 24 years ago!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 !
Jun 02 2009
On Tue, 02 Jun 2009 23:20:06 +0400, Walter Bright <newshound1 digitalmars.com> wrote:Walter Bright wrote:Which became Zortech C++ which became Symantec C++?Jason House wrote:Although the technology didn't make it to the PC until 1985 with the release of Datalight Optimum C.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 !But still, that's 24 years ago!
Jun 02 2009
Denis Koroskin wrote:On Tue, 02 Jun 2009 23:20:06 +0400, Walter Bright <newshound1 digitalmars.com> wrote:Yes.Although the technology didn't make it to the PC until 1985 with the release of Datalight Optimum C.Which became Zortech C++ which became Symantec C++?
Jun 02 2009
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? Thanksint.init unless the compiler rewrites it as int foo = 5; and then 5.
May 31 2009
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
Jun 02 2009