Should this always work?

Ola Fosheim Grøstad ola.fosheim.grostad at gmail.com
Tue May 4 15:59:21 UTC 2021


On Tuesday, 4 May 2021 at 14:18:30 UTC, Ola Fosheim Grøstad wrote:
> On Tuesday, 4 May 2021 at 13:58:59 UTC, Steven Schveighoffer 
> wrote:
>> Yeah, I wasn't aware of the more general usage, I thought it 
>> was always a pointer adjustment. But I also am not steeped in 
>> the terminology, just parroting what I've heard.
>
> My understanding is that a thunk is the code object that 
> fetches the value for you, in this case there might not be a 
> thunk involved as the interface code can just apply a fixed 
> offset?

I guess in D terms it can be best explained as a compiler 
internal "delegate". Let's take the example of getting the 
this-pointer:

Say, you want to write generic code gen, then you can let that 
code gen take a "delegate" that computes a this-pointer rather 
than writing many different code gens for different layouts. Then 
you trust the optimizer to get rid of it, or keep it if need be. 
That delegate would be a thunk. It may or may not exist at 
run-time based on the optimizer/language semantics.

You could also do something similar with an offset or switch 
statement. The point is, the code gen have no idea of how to 
obtain the this-pointer, the thunk does. It is a separate 
external code piece that obtains the value of the this-pointer.


More information about the Digitalmars-d-learn mailing list