<s>
The	O
SECD	B-Application
machine	I-Application
is	O
a	O
highly	O
influential	O
(	O
see	O
:	O
)	O
virtual	B-Architecture
machine	I-Architecture
and	O
abstract	B-Application
machine	I-Application
intended	O
as	O
a	O
target	O
for	O
functional	B-Language
programming	I-Language
language	I-Language
compilers	B-Language
.	O
</s>
<s>
The	O
letters	O
stand	O
for	O
Stack	B-Application
,	O
Environment	O
,	O
Control	O
,	O
Dump	O
—	O
the	O
internal	O
registers	O
of	O
the	O
machine	O
.	O
</s>
<s>
The	O
registers	O
Stack	B-Application
,	O
Control	O
,	O
and	O
Dump	O
point	O
to	O
(	O
some	O
realisations	O
of	O
)	O
stacks	B-Application
,	O
and	O
Environment	O
points	O
to	O
(	O
some	O
realisation	O
of	O
)	O
an	O
associative	B-Application
array	I-Application
.	O
</s>
<s>
The	O
machine	O
was	O
the	O
first	O
to	O
be	O
specifically	O
designed	O
to	O
evaluate	O
lambda	B-Language
calculus	I-Language
expressions	O
.	O
</s>
<s>
Lispkit	B-Language
Lisp	I-Language
was	O
an	O
influential	O
compiler	B-Language
based	O
on	O
the	O
SECD	B-Application
machine	I-Application
,	O
and	O
the	O
SECD	B-Application
machine	I-Application
has	O
been	O
used	O
as	O
the	O
target	O
for	O
other	O
systems	O
such	O
as	O
Lisp/370	O
.	O
</s>
<s>
This	O
method	O
works	O
in	O
the	O
Algol	O
60	O
implementation	O
,	O
but	O
in	O
a	O
functional	B-Language
programming	I-Language
language	I-Language
where	O
functions	O
are	O
first-class	O
citizens	O
,	O
a	O
free	O
variable	O
on	O
a	O
call	O
stack	B-Application
might	O
be	O
dereferenced	O
in	O
error	O
.	O
</s>
<s>
Turner	O
notes	O
that	O
Landin	O
solved	O
this	O
with	O
his	O
SECD	B-Application
machine	I-Application
,	O
in	O
which	O
a	O
function	O
is	O
represented	O
by	O
a	O
closure	B-Language
in	O
the	O
heap	O
instead	O
.	O
</s>
<s>
When	O
evaluation	O
of	O
an	O
expression	O
begins	O
,	O
the	O
expression	O
is	O
loaded	O
as	O
the	O
only	O
element	O
of	O
control	O
C	O
.	O
The	O
environment	O
E	O
,	O
stack	B-Application
S	O
and	O
dump	O
D	O
begin	O
empty	O
.	O
</s>
<s>
If	O
the	O
first	O
item	O
in	O
C	O
is	O
a	O
value	O
,	O
it	O
is	O
pushed	O
onto	O
the	O
stack	B-Application
S	O
.	O
More	O
exactly	O
,	O
if	O
the	O
item	O
is	O
an	O
identifier	O
,	O
the	O
value	O
pushed	O
onto	O
the	O
stack	B-Application
will	O
be	O
the	O
binding	O
for	O
that	O
identifier	O
in	O
the	O
current	O
environment	O
E	O
.	O
If	O
the	O
item	O
is	O
an	O
abstraction	O
,	O
a	O
closure	B-Language
is	O
constructed	O
to	O
preserve	O
the	O
bindings	O
of	O
its	O
free	O
variables	O
(	O
which	O
are	O
in	O
E	O
)	O
,	O
and	O
it	O
is	O
this	O
closure	B-Language
which	O
is	O
pushed	O
onto	O
the	O
stack	B-Application
.	O
</s>
<s>
If	O
the	O
item	O
is	O
ap	O
,	O
two	O
values	O
are	O
popped	O
off	O
the	O
stack	B-Application
and	O
the	O
application	O
done	O
(	O
first	O
applied	O
to	O
second	O
)	O
.	O
</s>
<s>
If	O
the	O
result	O
of	O
the	O
application	O
is	O
a	O
value	O
,	O
it	O
is	O
pushed	O
onto	O
the	O
stack	B-Application
.	O
</s>
<s>
If	O
the	O
application	O
is	O
of	O
an	O
abstraction	O
to	O
a	O
value	O
,	O
however	O
,	O
it	O
will	O
result	O
in	O
a	O
lambda	B-Language
calculus	I-Language
expression	O
that	O
may	O
itself	O
be	O
an	O
application	O
(	O
rather	O
than	O
a	O
value	O
)	O
,	O
and	O
so	O
cannot	O
be	O
pushed	O
onto	O
the	O
stack	B-Application
.	O
</s>
<s>
In	O
this	O
case	O
,	O
the	O
current	O
contents	O
of	O
S	O
,	O
E	O
,	O
and	O
C	O
are	O
pushed	O
onto	O
the	O
dump	O
D	O
(	O
which	O
is	O
a	O
stack	B-Application
of	O
these	O
triples	O
)	O
,	O
S	O
is	O
reinitialised	O
to	O
empty	O
,	O
and	O
C	O
is	O
reinitialised	O
to	O
the	O
application	O
result	O
with	O
E	O
containing	O
the	O
environment	O
for	O
the	O
free	O
variables	O
of	O
this	O
expression	O
,	O
augmented	O
with	O
the	O
binding	O
that	O
resulted	O
from	O
the	O
application	O
.	O
</s>
<s>
Completed	O
evaluation	O
is	O
indicated	O
by	O
C	O
being	O
empty	O
,	O
in	O
which	O
case	O
the	O
result	O
will	O
be	O
on	O
the	O
stack	B-Application
S	O
.	O
The	O
last	O
saved	O
evaluation	O
state	O
on	O
D	O
is	O
then	O
popped	O
,	O
and	O
the	O
result	O
of	O
the	O
completed	O
evaluation	O
is	O
pushed	O
onto	O
the	O
stack	B-Application
contents	O
restored	O
from	O
D	O
.	O
Evaluation	O
of	O
the	O
restored	O
state	O
then	O
continues	O
as	O
above	O
.	O
</s>
<s>
If	O
C	O
and	O
D	O
are	O
both	O
empty	O
,	O
overall	O
evaluation	O
has	O
completed	O
with	O
the	O
result	O
on	O
the	O
stack	B-Application
S	O
.	O
</s>
<s>
The	O
SECD	B-Application
machine	I-Application
is	O
stack-based	B-Application
.	O
</s>
<s>
Functions	O
take	O
their	O
arguments	O
from	O
the	O
stack	B-Application
.	O
</s>
<s>
Like	O
all	O
internal	O
data-structures	O
,	O
the	O
stack	B-Application
is	O
a	O
list	O
,	O
with	O
the	O
S	O
register	O
pointing	O
at	O
the	O
list	O
's	O
head	O
or	O
beginning	O
.	O
</s>
<s>
Due	O
to	O
the	O
list	O
structure	O
,	O
the	O
stack	B-Application
need	O
not	O
be	O
a	O
continuous	O
block	O
of	O
memory	O
,	O
so	O
stack	B-Application
space	O
is	O
available	O
as	O
long	O
as	O
there	O
is	O
a	O
single	O
free	O
memory	O
cell	O
.	O
</s>
<s>
Even	O
when	O
all	O
cells	O
have	O
been	O
used	O
,	O
garbage	B-General_Concept
collection	I-General_Concept
may	O
yield	O
additional	O
free	O
memory	O
.	O
</s>
<s>
Obviously	O
,	O
specific	O
implementations	O
of	O
the	O
SECD	B-Application
structure	O
can	O
implement	O
the	O
stack	B-Application
as	O
a	O
canonical	O
stack	B-Application
structure	O
,	O
thus	O
improving	O
the	O
overall	O
efficiency	O
of	O
the	O
virtual	B-Architecture
machine	I-Architecture
,	O
provided	O
that	O
a	O
strict	O
bound	O
be	O
put	O
on	O
the	O
dimension	O
of	O
the	O
stack	B-Application
.	O
</s>
<s>
Once	O
the	O
instruction	O
there	O
has	O
been	O
executed	O
,	O
the	O
C	O
is	O
pointed	O
at	O
the	O
next	B-General_Concept
instruction	I-General_Concept
in	O
the	O
list	O
—	O
it	O
is	O
similar	O
to	O
an	O
instruction	B-General_Concept
pointer	I-General_Concept
(	O
or	O
program	B-General_Concept
counter	I-General_Concept
)	O
in	O
conventional	O
machines	O
,	O
except	O
that	O
subsequent	O
instructions	O
are	O
always	O
specified	O
during	O
execution	O
and	O
are	O
not	O
by	O
default	O
contained	O
in	O
subsequent	O
memory	O
locations	O
,	O
as	O
is	O
the	O
case	O
with	O
the	O
conventional	O
machines	O
.	O
</s>
<s>
It	O
can	O
be	O
likened	O
to	O
the	O
return	O
stack	B-Application
of	O
other	O
machines	O
.	O
</s>
<s>
The	O
memory	O
organization	O
of	O
the	O
SECD	B-Application
machine	I-Application
is	O
similar	O
to	O
the	O
model	O
used	O
by	O
most	O
functional	B-Language
language	I-Language
interpreters	B-Application
:	O
a	O
number	O
of	O
memory	O
cells	O
,	O
each	O
of	O
which	O
can	O
hold	O
either	O
an	O
atom	O
(	O
a	O
simple	O
value	O
,	O
for	O
example	O
13	O
)	O
,	O
or	O
represent	O
an	O
empty	O
or	O
non-empty	O
list	O
.	O
</s>
<s>
The	O
two	O
pointers	O
are	O
traditionally	O
named	O
car	B-Language
and	I-Language
cdr	I-Language
respectively	O
—	O
but	O
the	O
more	O
modern	O
terms	O
head	O
and	O
tail	O
are	O
often	O
used	O
instead	O
.	O
</s>
<s>
The	O
different	O
types	O
of	O
values	O
that	O
a	O
cell	O
can	O
hold	O
are	O
distinguished	O
by	O
a	O
tag	B-Architecture
.	O
</s>
<s>
In	O
the	O
SECD	B-Application
machine	I-Application
,	O
cell	O
0	O
always	O
implicitly	O
represents	O
the	O
empty	O
list	O
,	O
so	O
no	O
special	O
tag	B-Architecture
value	O
is	O
needed	O
to	O
signal	O
an	O
empty	O
list	O
(	O
everything	O
needing	O
that	O
can	O
simply	O
point	O
to	O
cell	O
0	O
)	O
.	O
</s>
<s>
If	O
both	O
car	B-Language
and	I-Language
cdr	I-Language
point	O
at	O
atoms	O
,	O
that	O
will	O
yield	O
a	O
pair	O
,	O
usually	O
written	O
like	O
(	O
1	O
.	O
</s>
<s>
ld	O
pushes	O
the	O
value	O
of	O
a	O
variable	O
onto	O
the	O
stack	B-Application
.	O
</s>
<s>
sel	O
expects	O
two	O
list	O
arguments	O
,	O
and	O
pops	O
a	O
value	O
from	O
the	O
stack	B-Application
.	O
</s>
<s>
It	O
constructs	O
a	O
closure	B-Language
(	O
a	O
pair	O
containing	O
the	O
function	O
and	O
the	O
current	O
environment	O
)	O
and	O
pushes	O
that	O
onto	O
the	O
stack	B-Application
.	O
</s>
<s>
ap	O
pops	O
a	O
closure	B-Language
and	O
a	O
list	O
of	O
parameter	O
values	O
from	O
the	O
stack	B-Application
.	O
</s>
<s>
The	O
closure	B-Language
is	O
applied	O
to	O
the	O
parameters	O
by	O
installing	O
its	O
environment	O
as	O
the	O
current	O
one	O
,	O
pushing	O
the	O
parameter	O
list	O
in	O
front	O
of	O
that	O
,	O
clearing	O
the	O
stack	B-Application
,	O
and	O
setting	O
C	O
to	O
the	O
closure	B-Language
's	O
function	O
pointer	O
.	O
</s>
<s>
ret	O
pops	O
one	O
return	O
value	O
from	O
the	O
stack	B-Application
,	O
restores	O
S	O
,	O
E	O
,	O
and	O
C	O
from	O
the	O
dump	O
,	O
and	O
pushes	O
the	O
return	O
value	O
onto	O
the	O
now-current	O
stack	B-Application
.	O
</s>
<s>
They	O
all	O
take	O
any	O
necessary	O
parameters	O
from	O
the	O
stack	B-Application
.	O
</s>
