<s>
In	O
computer	B-General_Concept
programming	I-General_Concept
,	O
a	O
thunk	B-Application
is	O
a	O
subroutine	O
used	O
to	O
inject	O
a	O
calculation	O
into	O
another	O
subroutine	O
.	O
</s>
<s>
Thunks	B-Application
are	O
primarily	O
used	O
to	O
delay	O
a	O
calculation	O
until	O
its	O
result	O
is	O
needed	O
,	O
or	O
to	O
insert	O
operations	O
at	O
the	O
beginning	O
or	O
end	O
of	O
the	O
other	O
subroutine	O
.	O
</s>
<s>
They	O
have	O
many	O
other	O
applications	O
in	O
compiler	B-Application
code	I-Application
generation	I-Application
and	O
modular	B-Architecture
programming	I-Architecture
.	O
</s>
<s>
It	O
refers	O
to	O
the	O
original	O
use	O
of	O
thunks	B-Application
in	O
ALGOL	B-Language
60	I-Language
compilers	B-Language
,	O
which	O
required	O
special	O
analysis	O
(	O
thought	O
)	O
to	O
determine	O
what	O
type	O
of	O
routine	O
to	O
generate	O
.	O
</s>
<s>
The	O
early	O
years	O
of	O
compiler	B-Language
research	O
saw	O
broad	O
experimentation	O
with	O
different	O
evaluation	B-Language
strategies	I-Language
.	O
</s>
<s>
A	O
key	O
question	O
was	O
how	O
to	O
compile	B-Language
a	O
subroutine	O
call	O
if	O
the	O
arguments	O
can	O
be	O
arbitrary	O
mathematical	O
expressions	O
rather	O
than	O
constants	O
.	O
</s>
<s>
As	O
an	O
improvement	O
,	O
the	O
compiler	B-Language
can	O
generate	O
a	O
helper	O
subroutine	O
,	O
called	O
a	O
thunk	B-Application
,	O
that	O
calculates	O
the	O
value	O
of	O
the	O
argument	O
.	O
</s>
<s>
Peter	O
Ingerman	O
first	O
described	O
thunks	B-Application
in	O
reference	O
to	O
the	O
ALGOL	B-Language
60	I-Language
programming	I-Language
language	I-Language
,	O
which	O
supports	O
call-by-name	O
evaluation	O
.	O
</s>
<s>
Although	O
the	O
software	O
industry	O
largely	O
standardized	O
on	O
call-by-value	O
and	O
call-by-reference	O
evaluation	O
,	O
active	O
study	O
of	O
call-by-name	O
continued	O
in	O
the	O
functional	B-Language
programming	I-Language
community	O
.	O
</s>
<s>
This	O
research	O
produced	O
a	O
series	O
of	O
lazy	O
evaluation	O
programming	O
languages	O
in	O
which	O
some	O
variant	O
of	O
call-by-name	O
is	O
the	O
standard	O
evaluation	B-Language
strategy	I-Language
.	O
</s>
<s>
Compilers	B-Language
for	O
these	O
languages	O
,	O
such	O
as	O
the	B-Application
Glasgow	I-Application
Haskell	I-Application
Compiler	I-Application
,	O
have	O
relied	O
heavily	O
on	O
thunks	B-Application
,	O
with	O
the	O
added	O
feature	O
that	O
the	O
thunks	B-Application
save	O
their	O
initial	O
result	O
so	O
that	O
they	O
can	O
avoid	O
recalculating	O
it	O
;	O
this	O
is	O
known	O
as	O
memoization	O
or	O
call-by-need	O
.	O
</s>
<s>
Functional	B-Language
programming	I-Language
languages	I-Language
have	O
also	O
allowed	O
programmers	O
to	O
explicitly	O
generate	O
thunks	B-Application
.	O
</s>
<s>
This	O
is	O
done	O
in	O
source	O
code	O
by	O
wrapping	O
an	O
argument	O
expression	O
in	O
an	O
anonymous	B-General_Concept
function	I-General_Concept
that	O
has	O
no	O
parameters	O
of	O
its	O
own	O
.	O
</s>
<s>
This	O
prevents	O
the	O
expression	O
from	O
being	O
evaluated	O
until	O
a	O
receiving	O
function	O
calls	O
the	O
anonymous	B-General_Concept
function	I-General_Concept
,	O
thereby	O
achieving	O
the	O
same	O
effect	O
as	O
call-by-name	O
.	O
</s>
<s>
The	O
adoption	O
of	O
anonymous	B-General_Concept
functions	I-General_Concept
into	O
other	O
programming	O
languages	O
has	O
made	O
this	O
capability	O
widely	O
available	O
.	O
</s>
<s>
Thunks	B-Application
are	O
useful	O
in	O
object-oriented	B-Language
programming	I-Language
platforms	O
that	O
allow	O
a	O
class	O
to	O
inherit	B-Application
multiple	I-Application
interfaces	I-Application
,	O
leading	O
to	O
situations	O
where	O
the	O
same	O
method	B-Language
might	O
be	O
called	O
via	O
any	O
of	O
several	O
interfaces	O
.	O
</s>
<s>
The	O
following	O
code	O
illustrates	O
such	O
a	O
situation	O
in	O
C++	B-Language
.	O
</s>
<s>
If	O
it	O
refers	O
to	O
an	O
object	O
of	O
type	O
C	O
,	O
the	O
compiler	B-Language
must	O
ensure	O
that	O
C	O
's	O
implementation	O
receives	O
an	O
instance	B-Application
address	I-Application
for	O
the	O
entire	O
C	O
object	O
,	O
rather	O
than	O
the	O
inherited	O
B	O
part	O
of	O
that	O
object	O
.	O
</s>
<s>
As	O
a	O
direct	O
approach	O
to	O
this	B-Application
pointer	I-Application
adjustment	O
problem	O
,	O
the	O
compiler	B-Language
can	O
include	O
an	O
integer	O
offset	O
in	O
each	O
dispatch	O
table	O
entry	O
.	O
</s>
<s>
This	O
offset	O
is	O
the	O
difference	O
between	O
the	O
reference	O
's	O
address	O
and	O
the	O
address	O
required	O
by	O
the	O
method	B-Language
implementation	O
.	O
</s>
<s>
The	O
code	O
generated	O
for	O
each	O
call	O
through	O
these	O
dispatch	O
tables	O
must	O
then	O
retrieve	O
the	O
offset	O
and	O
use	O
it	O
to	O
adjust	O
the	O
instance	B-Application
address	I-Application
before	O
calling	O
the	O
method	B-Language
.	O
</s>
<s>
The	O
solution	O
just	O
described	O
has	O
problems	O
similar	O
to	O
the	O
naïve	O
implementation	O
of	O
call-by-name	O
described	O
earlier	O
:	O
the	O
compiler	B-Language
generates	O
several	O
copies	O
of	O
code	O
to	O
calculate	O
an	O
argument	O
(	O
the	O
instance	B-Application
address	I-Application
)	O
,	O
while	O
also	O
increasing	O
the	O
dispatch	O
table	O
sizes	O
to	O
hold	O
the	O
offsets	O
.	O
</s>
<s>
As	O
an	O
alternative	O
,	O
the	O
compiler	B-Language
can	O
generate	O
an	O
adjustor	O
thunk	B-Application
along	O
with	O
C	O
's	O
implementation	O
of	O
that	O
adjusts	O
the	O
instance	B-Application
address	I-Application
by	O
the	O
required	O
amount	O
and	O
then	O
calls	O
the	O
method	B-Language
.	O
</s>
<s>
The	O
thunk	B-Application
can	O
appear	O
in	O
C	O
's	O
dispatch	O
table	O
for	O
B	O
,	O
thereby	O
eliminating	O
the	O
need	O
for	O
callers	O
to	O
adjust	O
the	O
address	O
themselves	O
.	O
</s>
<s>
Call	O
by	O
name	O
was	O
used	O
for	O
this	O
purpose	O
in	O
languages	O
that	O
did	O
n't	O
support	O
closures	B-Language
or	O
procedure	O
parameters	O
.	O
</s>
<s>
Thunks	B-Application
have	O
been	O
widely	O
used	O
to	O
provide	O
interoperability	O
between	O
software	O
modules	B-Architecture
whose	O
routines	O
cannot	O
call	O
each	O
other	O
directly	O
.	O
</s>
<s>
This	O
may	O
occur	O
because	O
the	O
routines	O
have	O
different	O
calling	O
conventions	O
,	O
run	O
in	O
different	O
CPU	B-General_Concept
modes	I-General_Concept
or	O
address	B-General_Concept
spaces	I-General_Concept
,	O
or	O
at	O
least	O
one	O
runs	O
in	O
a	O
virtual	B-Architecture
machine	I-Architecture
.	O
</s>
<s>
A	O
compiler	B-Language
(	O
or	O
other	O
tool	O
)	O
can	O
solve	O
this	O
problem	O
by	O
generating	O
a	O
thunk	B-Application
that	O
automates	O
the	O
additional	O
steps	O
needed	O
to	O
call	O
the	O
target	O
routine	O
,	O
whether	O
that	O
is	O
transforming	O
arguments	O
,	O
copying	O
them	O
to	O
another	O
location	O
,	O
or	O
switching	O
the	O
CPU	B-General_Concept
mode	I-General_Concept
.	O
</s>
<s>
A	O
successful	O
thunk	B-Application
minimizes	O
the	O
extra	O
work	O
the	O
caller	O
must	O
do	O
compared	O
to	O
a	O
normal	O
call	O
.	O
</s>
<s>
Much	O
of	O
the	O
literature	O
on	O
interoperability	O
thunks	B-Application
relates	O
to	O
various	O
Wintel	B-Device
platforms	O
,	O
including	O
MS-DOS	B-Application
,	O
OS/2	B-Application
,	O
Windows	B-Application
and	O
.NET	B-Application
,	O
and	O
to	O
the	O
transition	O
from	O
16-bit	B-Device
to	O
32-bit	O
memory	O
addressing	O
.	O
</s>
<s>
As	O
customers	O
have	O
migrated	O
from	O
one	O
platform	O
to	O
another	O
,	O
thunks	B-Application
have	O
been	O
essential	O
to	O
support	O
legacy	B-Device
software	I-Device
written	O
for	O
the	O
older	O
platforms	O
.	O
</s>
<s>
The	O
transition	O
from	O
32-bit	O
to	O
64-bit	O
code	O
on	O
x86	O
also	O
uses	O
a	O
form	O
of	O
thunking	B-Application
(	O
WoW64	B-Application
)	O
.	O
</s>
<s>
However	O
,	O
because	O
the	O
x86-64	O
address	B-General_Concept
space	I-General_Concept
is	O
larger	O
than	O
the	O
one	O
available	O
to	O
32-bit	O
code	O
,	O
the	O
old	O
"	O
generic	O
thunk	B-Application
"	O
mechanism	O
could	O
not	O
be	O
used	O
to	O
call	O
64-bit	O
code	O
from	O
32-bit	O
code	O
.	O
</s>
<s>
The	O
only	O
case	O
of	O
32-bit	O
code	O
calling	O
64-bit	O
code	O
is	O
in	O
the	O
WoW64	B-Application
's	O
thunking	B-Application
of	O
Windows	B-Application
APIs	O
to	O
32-bit	O
.	O
</s>
<s>
On	O
systems	O
that	O
lack	O
automatic	O
virtual	B-Architecture
memory	I-Architecture
hardware	O
,	O
thunks	B-Application
can	O
implement	O
a	O
limited	O
form	O
of	O
virtual	B-Architecture
memory	I-Architecture
known	O
as	O
overlays	B-General_Concept
.	O
</s>
<s>
With	O
overlays	B-General_Concept
,	O
a	O
developer	O
divides	O
a	O
program	O
's	O
code	O
into	O
segments	O
that	O
can	O
be	O
loaded	O
and	O
unloaded	O
independently	O
,	O
and	O
identifies	O
the	O
entry	B-Language
points	I-Language
into	O
each	O
segment	O
.	O
</s>
<s>
When	O
a	O
segment	O
is	O
unloaded	O
,	O
its	O
entries	O
are	O
replaced	O
with	O
"	O
reload	O
thunks	B-Application
"	O
that	O
can	O
reload	O
it	O
on	O
demand	O
.	O
</s>
<s>
Similarly	O
,	O
systems	O
that	O
dynamically	B-Application
link	I-Application
modules	B-Architecture
of	O
a	O
program	O
together	O
at	O
run-time	O
can	O
use	O
thunks	B-Application
to	O
connect	O
the	O
modules	B-Architecture
.	O
</s>
<s>
Each	O
module	B-Architecture
can	O
call	O
the	O
others	O
through	O
a	O
table	O
of	O
thunks	B-Application
that	O
the	O
linker	O
fills	O
in	O
when	O
it	O
loads	O
the	O
module	B-Architecture
.	O
</s>
<s>
This	O
way	O
the	O
modules	B-Architecture
can	O
interact	O
without	O
prior	O
knowledge	O
of	O
where	O
they	O
are	O
located	O
in	O
memory	O
.	O
</s>
