Beta Draft - Do not distribute
© 2001, By Randall Hyde
This chapter discusses thunks which are special types of procedures and procedure calls you can use to
defer the e
ecution of some procedure call.
Although the use of thunks is not commonplace in standard
assembly code, their semantics are quite useful in
cial intelligence) and other programs.
use of thunks can dramatically impro
e the performance of certain programs.
Perhaps a reason thunks do
e use in assembly code is because most assembly language programmers are una
This chapter will solv
e that problem by presenting the defi
nition of thunks and describe
w to use them in your assembly programs.
First Class Objects
The actual lo
implementation of a thunk, and the in
ocation of a thunk, is rather simple.
, to understand wh
y you w
ant to use a thunk in an assembly language program we need to jump to
a higher le
el of abstraction and discuss the concept of
st Class Objects
rst class object is one you can treat lik
e a normal scalar data v
ou can pass it as a parameter
y arbitrary parameter passing mechanism), you can return it as a function result, you can change the
alue via certain le
al operations, you can retrie
e its v
alue, and you can assign one instance of a
rst class object to another
ariable is a good e
xample of a fi
rst class object.
w consider an array
y languages, arrays are not fi
rst class objects.
Oh, you can pass them as
parameters and operate on them,
ut you can’
t assign one array to another nor can you return an array as a
function result in man
In other languages, ho
, all these operations are permissible on
arrays so the
y are fi
rst class objects (in such languages).
A statement sequence (especially one in
olving procedure calls) is generally not a fi
rst class object in
y programming languages.
xample, in C/C++ or P
ascal/Delphi you cannot pass a sequence of
statements as a parameter
, assign them to a v
ariable, return them as a function result, or otherwise operate on
them as though the
y were data.
ou cannot create arbitrary arrays of statements nor can you ask a sequence
of statements to e
xcept at their point of declaration.
er used a language that allo
ws you to treat e
ecutable statements as data, you’
ant to do this.
There are, ho
, some v
ery good reasons for w
to treat statements as data and e
ecute them on demand.
amiliar with the C/C++ programming
language, consider the C/C++
expr ? Texpr: Fexpr
or those who are unfamiliar with the "?" operator, it evaluates the first expression (
) and then returns
the value of