Hi,
For SAoC 2022, I want to translate DRuntime hooks to templates. I am working on _d_newitem{U,T,iT}
. The compiler uses them to lower new S(args)
expressions, where S
is a struct
.
To instantiate the new template hooks, the lowering has to be moved from the glue layer to the semantic analysis. However, when the constructed struct
is nested, the lowering logic also needs to copy the struct
's context pointer(s). This is currently handled by the glue layer in e2ir.d
. Now the same logic needs to be replicated in the semantic analysis, where there is no machinery to find the right frame pointer, such as getEthis()
in toir.d
.
After discussing with my mentors, we have come up with 2 possible approaches:
-
Add a new AST node for copying context pointers to
expression.d
:ContextPointerExp
. The new lowering ofnew S(args)
in the semantic phase will insert aContextPointerExp
node and then the glue layer will visit it and copy the correct context pointer to the newly createdstruct
. This approach is efficient from a runtime perspective, as it doesn't add any unnecessary computation, but has the disadvantage of requiring changes beyond the frontend. Thus it won't be transparent for LDC and GDC, who will have to handleContextPointerExp
in their own glue layers. -
Change the lowering to something like this:
S* s = new S();
// lowering:
S* s = (S tmp, _d_newitem(tmp));
This way, tmp
's context pointer will be set by default and _d_newitem
will be able to simply copy it from tmp
to s
in a way similar to copyEmplace()
. This solution is not without faults, though, as it requires creating and initialising tmp
only to get its context pointer.
void
-initialising tmp
from the snippet above is not a solution, as that also leaves the context pointer uninitialised. The code below either seg faults, or prints a random value for s.y
because the context pointer of s
is null
:
void main()
{
int x = 3;
struct S
{
int y;
void bar()
{
y = x;
}
}
S s = void;
s.bar();
writeln(s);
}
Do you see a better approach than 2? How should we handle context pointers in the semantic phase?
Thanks,
Teo