Thread overview
[dmd-internals] How does TypeInfo.init work?
Jun 11, 2015
Mike Franklin
Jun 11, 2015
Mike Franklin
Jun 12, 2015
Mike Franklin
June 11, 2015
There is a comment [1] in object.d for TypeInfo.init reading:

"Return default initializer.  If the type should be initialized to all zeros, an array with a null ptr and a length equal to the type size will be returned."

Also, I see where the null pointer is set in typinf.c [2].


Using int as an example, how does the compiler go from a null pointer and a size to a 32-bit 0?  Where is that done in the compiler?


[1] https://github.com/D-Programming-Language/druntime/blob/master/src/object.d#L286-L287 [2] https://github.com/D-Programming-Language/dmd/blob/master/src/typinf.c#L460


Thanks for the help,
Mike
_______________________________________________
dmd-internals mailing list
dmd-internals@puremagic.com
http://lists.puremagic.com/mailman/listinfo/dmd-internals
June 11, 2015
I don’t know that the compiler cares what’s in the TypeInfo, it uses it’s own internal representation I think. This is a communication to druntime of how to initialize the type. The idea is not to take up data in the static section with zero bytes.

You will see it used in the runtime, for example: https://github.com/D-Programming-Language/druntime/blob/master/src/object.d#L2592 <https://github.com/D-Programming-Language/druntime/blob/master/src/object.d#L2592>

It’s probably used elsewhere too, but I’m not sure where to look.

-Steve

> On Jun 11, 2015, at 8:02 AM, Mike Franklin via dmd-internals <dmd-internals@puremagic.com> wrote:
> 
> There is a comment [1] in object.d for TypeInfo.init reading:
> 
> "Return default initializer.  If the type should be initialized to all zeros, an array with a null ptr and a length equal to the type size will be returned."
> 
> Also, I see where the null pointer is set in typinf.c [2].
> 
> 
> Using int as an example, how does the compiler go from a null pointer and a size to a 32-bit 0?  Where is that done in the compiler?
> 
> 
> [1] https://github.com/D-Programming-Language/druntime/blob/master/src/object.d#L286-L287 [2] https://github.com/D-Programming-Language/dmd/blob/master/src/typinf.c#L460
> 
> 
> Thanks for the help,
> Mike
> _______________________________________________
> dmd-internals mailing list
> dmd-internals@puremagic.com
> http://lists.puremagic.com/mailman/listinfo/dmd-internals



June 11, 2015
Ok, but if you look at the TypeInfo class in object.d [1], you'll see init() returns null.  Then if you look at the TypeInfo_i class for integers [2], it doesn't override init().  I, therefore, suspect some compiler special treatment.  So, how (and where in the source code) does an integer get it's 32-bit 0 initial value?

[1] https://github.com/D-Programming-Language/druntime/blob/master/src/object.d#L289[2] https://github.com/D-Programming-Language/druntime/blob/master/src/rt/typeinfo/ti_int.d#L18
 Mike


     On Friday, June 12, 2015 3:58 AM, Steven Schveighoffer via dmd-internals <dmd-internals@puremagic.com> wrote:


 I don’t know that the compiler cares what’s in the TypeInfo, it uses it’s own internal representation I think. This is a communication to druntime of how to initialize the type. The idea is not to take up data in the static section with zero bytes.
You will see it used in the runtime, for example: https://github.com/D-Programming-Language/druntime/blob/master/src/object.d#L2592
It’s probably used elsewhere too, but I’m not sure where to look.
-Steve

On Jun 11, 2015, at 8:02 AM, Mike Franklin via dmd-internals <dmd-internals@puremagic.com> wrote: There is a comment [1] in object.d for TypeInfo.init reading:

"Return default initializer.  If the type should be initialized to all zeros, an array with a null ptr and a length equal to the type size will be returned."

Also, I see where the null pointer is set in typinf.c [2].


Using int as an example, how does the compiler go from a null pointer and a size to a 32-bit 0?  Where is that done in the compiler?


[1] https://github.com/D-Programming-Language/druntime/blob/master/src/object.d#L286-L287 [2] https://github.com/D-Programming-Language/dmd/blob/master/src/typinf.c#L460


Thanks for the help,
Mike
_______________________________________________
dmd-internals mailing list
dmd-internals@puremagic.com
http://lists.puremagic.com/mailman/listinfo/dmd-internals


_______________________________________________
dmd-internals mailing list
dmd-internals@puremagic.com
http://lists.puremagic.com/mailman/listinfo/dmd-internals



June 11, 2015
And in fact, that is what it does :)

void main()
{
    import std.stdio;
    auto x = typeid(int);
    writefln("%s, %s", x.init.length, x.init.ptr);
}

output:

0, null

Doing some research, I found this tidbit in history:

https://issues.dlang.org/show_bug.cgi?id=2990 <https://issues.dlang.org/show_bug.cgi?id=2990>

The only basic types that have init() defined are the ones that have non-zero initializers. I suppose we have 3 options:

1. Revise the documentation so it says “if this returns null with zero length, it means the type is initialized with all zeros, and use tsize to get the size of the type”
2. Add the appropriate overrides to all the druntime types.
3. We could make TypeInfo.init return (cast(void *)null)[0..tsize] by default, defaulting all inits to a sane value.

My personal preference is for 3, since it adds no new functions, and will fix any omissions that we haven’t discovered or that would be added in the future.

Clearly, though, this identifies how unused that function really is :)

I recall now, the compiler only sets up static data inside the typeinfo for things like classes and structs, and the TypeInfo_Struct init() function reads that when determining what to return.

Everything else is defined in druntime.

-Steve

> On Jun 11, 2015, at 7:48 PM, Mike Franklin <slavo5150@yahoo.com> wrote:
> 
> Ok, but if you look at the TypeInfo class in object.d [1], you'll see init() returns null.  Then if you look at the TypeInfo_i class for integers [2], it doesn't override init().  I, therefore, suspect some compiler special treatment.  So, how (and where in the source code) does an integer get it's 32-bit 0 initial value?
> 
> [1] https://github.com/D-Programming-Language/druntime/blob/master/src/object.d#L289 <https://github.com/D-Programming-Language/druntime/blob/master/src/object.d#L289>
> [2] https://github.com/D-Programming-Language/druntime/blob/master/src/rt/typeinfo/ti_int.d#L18 <https://github.com/D-Programming-Language/druntime/blob/master/src/rt/typeinfo/ti_int.d#L18>
> 
> Mike
> 



June 12, 2015
(I hope this formatting comes out right)

>And in fact, that is what it does :)>
>void main()
>{
>    import std.stdio;
>    auto x = typeid(int);
>    writefln("%s, %s", x.init.length, x.init.ptr);
>}
>
>output:
>
>0, null


Actually, I think it is only half right.  Shouldn't x.init.length return 4?

>Doing some research, I found this tidbit in history:
>
>https://issues.dlang.org/show_bug.cgi?id=2990
>
>The only basic types that have init() defined are the ones that have non-zero initializers. I suppose we have 3 options:
>
>1. Revise the documentation so it says “if this returns null with zero length, it means the type is initialized with all zeros, and use tsize to get the size of the type”
>2. Add the appropriate overrides to all the druntime types.
>3. We could make TypeInfo.init return (cast(void *)null)[0..tsize] by default, defaulting all inits to a sane value.
>
>My personal preference is for 3, since it adds no new functions, and will fix any omissions that we haven’t discovered or that would be added in the future.


I'm inclined to agree with you.  I think I'll put this on my todo list for pull request when the time is right.  I think now is not it.  Perhaps after 2.068 is released.

>Clearly, though, this identifies how unused that function really is :)
>
>I recall now, the compiler only sets up static data inside the typeinfo for things like classes and structs, and the TypeInfo_Struct init() function reads that when determining what to return.
>
>Everything else is defined in druntime.

Thanks Steve,  You've been very helpful.  Things are starting to come together for me now.

_______________________________________________
dmd-internals mailing list
dmd-internals@puremagic.com
http://lists.puremagic.com/mailman/listinfo/dmd-internals