RIT Scholar Works
Theses
Thesis/Dissertation Collections
1984
Smalltalk-80 virtual machine
Ashraful Huq
Follow this and additional works at:
http://scholarworks.rit.edu/theses
This Thesis is brought to you for free and open access by the Thesis/Dissertation Collections at RIT Scholar Works. It has been accepted for inclusion
in Theses by an authorized administrator of RIT Scholar Works. For more information, please contact
Recommended Citation
School of Computer Science and Technology
SMALLTALK-80
VIRTUAL MACHINE
by
Ashraful Huq
Thesis submitted to the Faculty of the
School of Computer Science and Technology
in partial fulfillment of the requirements for the degree of
MASTER OF SCIENCE
in
Computer Science
APPROJIED :
Guy Johnson
j'fofe:sri07 Guy Johnson
Lawrence Coon
Professor
L~re~
Coqn_
Warren R. Carithers
1.2
Smalltalk-80
System
Implementation
5
1.3
Thesis Description
7
2.0
IMPLEMENTATION
OF
THE
SMALLTALK-80
VIRTUAL MACHINE
.9
3.0
THE
INTERPRETER
IMPLEMENTATION
11
3.1
Bytecodes
14
3.2
Objects
Used
By
The
Interpreter
16
3.2.1
Compiled
Method
16
3.2.2
Contexts
21
3.2.3
Classes
25
3.2.4
Primitive
Subroutines
27
4.0
METHOD EXECUTION EXAMPLE
30
5.0
MEMORY MANAGEMENT OVERVIEW
49
5.1
Object
Memory
Implementation
54
5.1.1
Object
Table
56
5.1.2
Object
Space
60
5.1.3
Allocation
62
5.1.4
Deallocation
64
5.1.5
Compaction
64
5.2
GARBAGE COLLECTION
65
5.2.1
Reference
Counting
65
5.2.2
Marking
66
6.0
CONCLUSION
68
7.0
FUTURE WORK
72
8.0
APPENDIX
A
74
8.1
The
Smalltalk-80
Bytecodes
74
9.0
APPENDIX B
76
9.1
Interface
Between The
Interpreter
And
Object
Memory
76
10.0
APPENDIX
C
81
10.1
Program
Execution
Statistics
81
11.0
APPENDIX
D
83
ACKNOWLEDGEMENTS
I
dedicate
this
thesis
to
my
parents.The
writing
andprogramming
ofthis
thesis
has
been
an arduoustask.
My
mother and
my
late
father,
gave me patientunderstanding
andlove
without whichthis
work could nothave
been
completed.Both my brothers have
spoken words of encouragementto
meall along.
I
wishto
thank Professor
Guy
Johnson
for
the
encouragementand guidance
he
has
given me.I
amvery
gratefulto
him
for
the
endlesshours
he
spentdirecting
my
effort onthis
project.
I
amindebted
to
Professor
Lawrence
Coon
for
giving
methe
mainidea
for
the
thesis.
I
also wishto
thank
Professor
Warren R.
Carithers
for
his
cooperation andfor
time
spent onmy
committee.I
am gratefulto
both
ofthem
for
their
diligent
patience and support.I
would alsolike
to
thank
Professor
John
McCarthy
for
his
invaluable
help
and encouragementto
cometo
the
United
States
for
my
degree.
I
also oweDr.
R.
Giles
specialthanks
for
his
support andhelp.
Lastly,
my
sincerethanks
to
allmy
friends
andfellow
1.0
INTRODUCTION AND BACKGROUND
Smalltalk-80
is
aninteractive,
object orientedprogramming
language
developed
by
the
XEROX Palo Alto
Research
Center,
California
to
demonstrate
the
feasibility
of a usableobject-oriented
programming
environment.The
challenge ofthe
system(Goldberg,
Adele, Robson,
etcCI,
5,
63)
is
to
provide acceptable performance without
compromising
the
simplicity
ofthe
object-orientedprogramming
environment.The
goals ofthe
system areto
provide:1.
A
powerful andflexible
userfriendly
object-orientedprogramming
language
and agraphics-based,
interactive
programdevelopment
environment.The
system shouldbe
powerful enoughfor
expertusers,
but
alsoeasy
to
useby
novice users.2.
A
powerfulinformation
system,
onein
whichthe
user canstore, access and manipulate
information
sothat
the
system can grow asthe
user'sideas
grow,
and provideincreased
flexibility
in
terms
of user programmability.3.
A
language
ofdescription
which serves as aninterface
between
the
modelsin
the
human
mind andthose
in
4.
A modular, flexible
and consistent system which cancater
to
a widevariety
ofinformation
needs.5.
A
system whose performanceis
to
be
comparable withexisting
interactive
information
systems.Smalltalk-80
is
an outgrowth of several experimentalobject-oriented
systems,
Smalltalk-72,
Smalltalk-76
andSmalltalk-78
C5,
6,
73.
They
wereintended
as a vehiclefor
research
in
object-orientedprogramming
environment;
eventually
Smalltalk-80
evolvedinto
afully
operationalprogramming
language
product.1. 1
Overview Of
The
Smalltalk-80
System
A
completeSmalltalk-80
systemis
madeup
ofthe
following
components; a) automatic storage
management;
b)
file
system; c)
display
handling;
d)
text
and pictureediting;
e)
keyboard
andpointing
device
input;
f )
debuggers;
g)performance profiler;
h)
processorscheduling
andi)
compilation and
decompilation.
In
Smalltalk:
1.
Every
entity, no matter whatits
name orfunction,
is
anobject.
For
example, objectsrepresent;
numbers,
character strings, queues,
programs,
compilers,
2.
Every
objectis
aninstance
of aclass,
andbehaves
in
a manner prescribedby
that
class.3.
Each
objectmay
maintainits
ownlocal
state,
ormemory
-4.
Objects
communicate with each otherby
sending
messages.Thus,
defining
a class consists ofenumerating
the
validmessages which
it
might receive, anddefining
the
appropriate response associated with each message.In
Smalltalk
the
userinterface
component andthe
language
interface
component are object-oriented, as opposedto
the
procedure oriented slant of most older userinterfaces
andlanguages.
Object-oriented
programming
replaces conventional operator and operand concepts with messages andobjects.
Object
are privatedata,
andthe
operations supported onthat
data;
a messageis
a requestfor
an objectto
perform one ofits
operations.For
examplethe
expression
1+9
canbe
viewed asthe
object1
being
sentthe
message +9.Recognizing
the
message'+',
it
then
asksfor
the
nextvalue,
whichis
9;
the
additionis
performed,
andthe
resultis
returned.In
a conventionalprogramming
language
when procedure andfunction
calls are made, such asexecuting
a specific piece of code appropriateto
somedata
type,
the
decision
asto
object-oriented
model when a messageis
sentto
perform someoperation,
the
messageis
responsibleonly
for
deciding
whatshould
be
done;
the
objectsdecide
how to do
it.
Here
the
semantics are
found
in
the
classdefinition;
the
interpretation
of a message(an
operator)is
intrinsic
orinternal
to
that
class,
and not external orlodged
in
a setof external routines.
For
exampleadding
complex numbersto
a compiler requires
changing
allthe
routinesfor
addition,
subtraction etc.
Adding
it
to
Smalltalk merely
requiresdefining
appropriate responsesto
the
desired
set ofmessages and
defining
complex number objects.1. 2
Smalltalk-80
System
Implementation
The
Smalltalk-80
systemis
divided
into
two
layers:
1.
Virtual
Image
-This
image
consists of allthe
objectsin
the
system.2.
Virtual
Machine
-This
consists ofthe
hardware
andsoftware which give
dynamics
to
the
objectsin
the
Virtual
Image.
In
Smalltalk,
written programs aretranslated
into
sequencesof eight-bit
instructions
(called
bytecodes)
by
the
Smalltalk-80
compiler.These
bytecodes
are usedby
the
interpreter
to
manipulate objectsin
the
Virtual
Image.
objects which make
up
the
Virtual
Image.
The
Virtual
Image
is
providedby
XEROX,
andthe
task
ofany
system
implementor
is
to
create aVirtual
Machine.
The
Virtual
Image
then
canbe
loaded
into
the
Virtual
Machine
and
the
Smalltalk-80
systembecomes
operational.The
external
behaviour
ofthe
Virtual
Machine
must conformto
the
Smalltalk-80
specification,
eventhough
the
underlying
implementation
ofthe
Virtual
Machine
may
be
different
than
that
presentedby
the
Smalltalk-80
implementors
C13.
The
current size of
the
Virtual
Image
requires atleast
half
megabyte of memory.
As
mentionedearlier,
the
Smalltalk-80
systemis
divided
into
two
distinct
parts,
in
presenting
the
details
ofthe
Smalltalk-80
implementation,
it
is
assumedthat
the
readeris
generally
familiar
withthe
Smalltalk-80
system,
particularly
Objects,
Instance
Variables,
Classes
etc.It
is
beyond
the
scope ofthis
thesis
to
present a completeoverview of
the
Smalltalk-80
system;
rather,
the
emphasisis
onimplementation.
Readers
unfamiliar withthe
Smalltalk-80
system should referto
materials presentedin
1.3
Thesi3 Description
The
Smalltalk-80
System
is
divided
into
two
parts: a)Smalltalk
Virtual
Image;
andb)
Smalltalk-80
Virtual
Machine
.This
thesis
is
going
to
be
based
on a partialimplementation
of
the
Smalltalk-80
Virtual
Machine.
Specifically:
1.
Implement
aninterpreter
to
simulatethe
Virtual
Machine
2.
Implement
allnecessary memory
management routines usedby
the
interpreter.
The
following
areas will notbe
addressed/implemented:
1.
Graphics
support2.
File
System
3.
Multiple
Processes
The
thesis
is
divided
into
7
chapters.Chapter
1
gives abrief
overview ofthe
Smalltalk-80
system.Chapter
2
describes
the
implementation
ofthe
system on atime-sharing
system.
Chapter
3
describes
the
interpreter;
data
stuctures,
objects,
bytecodes,
messages are explained.Chapter
4
includes
a completeSmalltalk-80
method execution.It
lists
in
detail
how
source statements gets executedby
the
interpreter.
Chapter
5
describes
the
objectmemory
structure of
the
objectmemory
is
described,
andmemory
management routines explained.
Chapter
6
details
the
conclusions reached.
Chapter
7
explains what needsto
be
2.0
IMPLEMENTATION OF THE SMALLTALK-80
VIRTUAL
MACHINE
The
Smalltalk-80
Virtual
Machine
is
Implemented
ontop
ofthe
4.2
BSD
UNIXAOperating
System.
The
systemis
implemented
as prescribedby
the
book
"SMALLTALK-80
:The
language
andits
implementation"C13.
There
aretwo
distinct
part ofthe Virtual
Machine:
1.
Smalltalk-80
Interpreter
2.
Smalltalk-80
Object
Memory
Manager
The
Smalltalk-80 Virtual
Machine
wasdeveloped using
the
VAX
11/780
UNIX
system.The
entireVirtual
Machine
is
writtenin
C.
VAX
11/780
is
a32
bit
wordmachine,
andthe
standardSmalltalk-80
implementation
uses16
bit
word.So
to
be
consistent with
the
Smalltalk-80
implementation
C13,
in
allour
C
programs, a wordis
treated
as16
bits
long.
Even
though
the
System
is
being
developed
on aVAX
11/780,
the
system canbe
portedto
avariety
ofhardware
whichhas
support
for
C
andUNIX.
The
only
modification requiredin
the
programs willbe
to
changethe
macrodefinitions
usedto
define
integer.
The
Virtual
Machine
implementation
described
in
the
book
is
written
in
Smalltalk-80
C13.
This
source code wastranslated
into
C
code and modifiedto
producethe
samebehaviour
as prescribedby
the
Smalltalk
source code.The
choice ofC
as animplementation
language
was made onthe
basis
ofthe
strength ofC
as a systemprogramming
language.
UNIX
softwaretools
proved quite usefulfor
3-0
THE
INTERPRETER
IMPLEMENTATION
The
Smalltalk-80
Virtual
Machine
andcorresponding
bytecode
set are
stack-oriented.
Object
pointers are pushed onto andpopped
from
astack,
and when a messageis
sent,
the
top
few
elements of
the
stack are used as receiver and arguments ofthe
method(methods
correspondto
programs,
subroutines, orprocedures
in
procedure orientedlanguages).
These
are replacedby
the
object returned asthe
value ofthat
method.Some
examples are givenlater
on.The
State
ofthe
interpreter
consists of:1.
The
Compiled
Method
whosebytecodes
arebeing
executed.
Compiled
Method
is
anObject
whichcontains
the
bytecodes
to
be
executed.The
data
structure of
the
Compiled
Method
is
described
later.
2.
The
location
ofthe
nextbytecode
to
be
executedin
that
Compiled
Method.
This
is
the
interpreter's
instruction
pointer.3.
The
receiver and arguments ofthe
messagethat
invoked
the
Compiled
Method.
4.
Any
temporary
variables neededby
the
Compiled
5.
A
stackThe
interpreter
usesthe
abovefive
pieces ofinformation
and
repeatedly
performs athree
step
cycleC13:
1.
Fetch
the
bytecode
from
the
Compiled
Method
indicated
by
the
instruction
pointer.2.
Increment
the
instruction
pointer.3.
Perform
the
function
specifiedby
the
bytecode.
Figure
1
thru
4
show some examples:The
bytecodes
aredescribed,
the
action performedby
the
bytecodes
andthe
contents of
the
stack are explained.The
stack mentionedin
some of
the
bytecodes
is
usedfor
several purposes.It
holds
the
receiver,
arguments,
and results ofthe
two
messages
that
are sent.The
stackis
also used asthe
source of
the
resultto
be
returnedby
a message.The
stackis
maintainedby
the
interpreter.
Bytecode
Action
Stack
Contents
After Execution
(Top
of stackto
Right)
32
Push 3
(3)
33
Push
4
(3
4)
34
Push
5
(3
4
5)
176
Send
+(3
9)
184
Send
*(27)
Bytecode
Action
Stack
Contents After
Execution
(Top
of stackto
Right)
32
Push
3
(3)
105
storeinto
a(3)
135
Pop
(
)
33
Push 4
(4)
106
storeinto
b
(4)
Figure
2:
Bytecodes
for
the
expression a <-3.
b
<-4.
Bytecode
Action
Stack
Contents
After
Execution
(Top
of stackto
Right)
32
Push
3
(3)
105
Store
into
a(3)
135
Pop
(3)
128
Push
a(3)
124
Return
top
(
)
Figure
3:
Bytecodes
for
the
expression a <-3.
|a
Bytecode
Action
32
33
176
105
Stack
Contents
After
Execution
(Top
of stackto Right)
Push 3
(3)
Push
4
(3
4)
Send
+(3
9)
Store
into
a(7)
3.1
Bytecodes
The
interpreter
understands256
bytecode
instructions
that
fall
into
five
categories:pushes,
stores,
sends, returnsand
jumps.
Appendix A
describes
the
exactmeaning
of eachbytecodeC13.
Since
morethan
256
instructions
for
the
interpreter
are needed(bytecodes
are8
bits
long)
, some ofthe
bytecodes
take
extensions.An
extensionis
one ortwo
bytes
following
the
bytecode
that
further
specify
the
instruction.
An
extensionis
not aninstruction
onits
own;
it
is
only
part of aninstruction.
The
bytecodes
in Appendix A
arelisted
in
rangesthat
have
similar
function.
Each
range ofbytecode
is
listed
with abit
pattern and a comment aboutthe
function
ofthe
bytecodes.
The
bit
pattern showsthe
binary
representationof
the
bytecodes
in
the
range.Push Bytecodes
:A
pushbytecode
indicates
the
source of anobject
to
be
pushed onthe
top
ofthe
interpreter's
stack.The
sourcesinclude:
a)the
receiver ofthe
messagethat
invoked
the
Compiled
Method;
b)
the
instance
variables ofthe
receiver; c)the
temporary
frame
(the
arguments ofthe
message and
the
temporary
variables);d)
the
literal
frame
of
the
Compiled
Method;
e)the
top
ofthe
stack(i.e.,
this
bytecode
duplicates
the
top
ofthe
stack).expression end with a store
bytecode.
The
bytecodes
before
the
storebytecode
computethe
new value of a variable andleave
it
ontop
ofthe
stack.A
storebytecode
indicates
the
variable whose value shouldbe
changed.Send
Bytecodes
:The
sendbytecode
causes a messageto
be
sent.
The
object pointersfor
the
receiver andthe
arguments ofthe
message arefound
onthe
active contextsstack.
The
sendbytecode
determines
the
selector ofthe
message andhow many
argumentsto
take
from
the
stack.A
set of
32
sendbytecodes
referdirectly
to
the
special selectors(described
later;
Figure
8),
the
other sendbytecodes
referto
their
selectorsin
the
literal
frame.
Return
Bytecodes
:There
are sixbytecodes
that
returncontrol,
and a valuefrom
a context.The
valueis
usually
found
onthe
top
ofthe
stack.Four
ofthese
are specialbytecodes.
These
are self(message
receiver),
true,
false,
nil.
Jump
Bytecodes
:The
jump
bytecodes
changethe
activecontext's
instruction
pointerby
a specified amount.Unconditional
jumps
changethe
instruction
pointer wheneverthey
are encountered.Conditional
jumps
only
changethe
instruction
pointerif
the
object pointer onthe
top
ofthe
stack
is
a specifiedboolean
object,
eithertrue
orfalse.
The
jump
bytecodes
are usedto
implement
efficient control3.2
Objects
Used By
The
Interpreter
When
the
Interpreter
is
running
it
needsto have
accessto
certain objects.
These
objects are :1.
Compiled Method.
2.
Contexts.
3.
Classes.
3.2.1
Compiled Method
When
the
interpreter
is
running
it
fetches
bytecodes
from
and object called
the
Compiled
Method
(Figure
6).
+ +
|
Size
|
+ +
|
Class
|
+ +
|
Header
|
+ +
I
I
+ +
|
literal
frame
|
+ +
I
I
+ +
I
I
+ +
|
bytecodes
|
+ +
I
I
+ +
In
the
Smalltalk-80
Virtual
Image
when a programmer writessource
statements,
these
aretranslated
by
the
Smalltalk-80
compiler
into
bytecodes
and storesthem
in
an object calledCompiled
Method
(figure
6).
For
examplethe bytecodes
for
the
source statement:extent:newExtent
corner <- origin + newExtent
would
be
0,
16,
176,
97,
120.
The
bytecodes
are stored as8
bit
values,
withtwo
bytecodes
to
a word.In
addition aCompiled Method
also contains some object pointers.The
first
ofthese
object pointersis
calledthe
methodheader
and
the
rest ofthe
object pointers makeup
the
method'sliteral
frame.
Header:
The
header
field
ofthe
Compiled Method
is
encodedas an object pointer.
It
contains a smallinteger
withthe
low
orderbit
setto
1
to
indicate
it
is
not an objectpointer.
The
header
includes
four
fields
as showin
figure
7.
+ + + + + +
I
I
I
I
I
I
I
I
I
I
I
I
I
I
I
H
+ + + + + +
flag
temporary
large
literal
value count context count
flag
Flag
value-This
field
is
3
bits
long
andis
usedto
indicate
the
number of arguments aCompiled Method
takes
andwhether or not
it
has
an associated primitive routine.Temporary
Count
-This
field
is
5
bits
long.
It
indicates
to
the
Interpreter
the
number oftemporary
variablesincluding
the
number of arguments usedby
the
Compiled
Method
.Large
Context
Flag
-This
field
is
singlebit.
It
indicates
which of
the
two
sizes of stack are needed.Smaller
Compiled
Method have
roomfor
12
andlarger
have
roomfor
32.
Literal
Count
-This
field
is
5
bits
long
andholds
the
sizeof
the
literal
frame
(Figure
6).
Special
Primitive
Methods:
Smalltalk
methodsthat
only
return
the
receiver ofthe
message(self)
produceCompiled
Methods
that
have
noliterals
orbytecodes,
only
aheader
with a
flag
value of5.
Similarly
Smalltalk
methodsthat
only
returnthe
value of one ofthe
receiver'sinstance
variables produce
Compiled
Methods
that
containonly headers
with a
flag
value of6.
All
other methods produceCompiled
Methods
withbytecodes.
When
the
flag
valueis
6,
the
index
of
the
instance
variableto
returnis
found
in
the header
in
the
bit
field
ordinarily
usedto
indicate
the
number ofMethod Header Extensions:
If
the
flag
valueis
7,
the
nextto
last
literal
is
aheader
extension.The header
extensionincludes
two bit
fields
that
encodethe
argument count andprimitive
index
ofthe
Compiled
Method.
Figure
7.1
shows anexample.
+ + + +--+
I
0|
0|
|
|
|
|
|
|
|
|
|
|
|
|
1|
+ + + + +
argument primitive
count
index
Figure
7.1
Literal
Frame:
Here
object pointers are storedfor
objectsthat
the
Interpreter
cannot referdirectly.
These
include
the
selectors of messagesthat
the
methodsends,
plus sharedvariables and constants
to
whichthe
method refers.Figure
8
showsthe
objects andthe
message selectorsthat
canbe
directly
referredby
bytecodes.
All
other objects areentered
in
the
literal
frame.
Objects:
-
the
receiver and arguments of
the
invoking
message-
the
-the
values ofany
temporary
variables requiredby
the
method
-seven special constants
(true,
false,
nil, -1,0,
1,
2)
-32
special message selectors.The
32
selectors are:+
-<
>
<
=>
= = +**
/
\
@
bitShift
\\
bit
And
bitOr
at: at:put: size next
nextPut: atEnd = = class
blockCopy
value value:do:
new new: X
Y
Figure
8
For
examplein
the
Compiled
Method
for
the
method"Rectangle
intersects"(Figure
9)C13.
intersects
: aRectangle|
(origin
max:aRectangle origin)<
(corner
min: aRectanglecorner)
The
four
message selectors,"max:",
"origin:",
"min:",
and "corner"by
bytecodes
(Figure
8).
These
selector areincluded
in
the
literal
frame
andthe
sendbytecode
accessthese
selectorsby
their
positionin
the
literal
frame.
Rectangle
intersects:
0
pushthe
value ofthe
receiver'sfirst
instance
variable
(origin)
ontothe
stack16
pushthe
argument(aRectangle)
209
send aunary
message withthe
selectorin
the
second
literal
frame
location
(origin)
224
send a single argument message withthe
selectorin
the
first
literal
frame
location
(max:)
1
pushthe
value ofthe
receiver's secondinstance
variable(corner)
ontothe
stack16
pushthe
argument(aRectangle)
ontothe
stack211
send aunary
message withthe
selectorin
the
fourth
literal
frame
location
(corner)
226
send a single argument message withthe
selectorin
the
third
literal
frame
location
(min:
178
send abinary
message withthe
selector<
124
returnthe
object ontop
ofthe
stack asthe
value of
the
message(intersects:)
Literal
Frame
max:
origin
min:
corner
Figure
9
3.2.2
Contexts
The
interpreter
uses contextsto
representthe
state ofPush,
store,
andjump
bytecodes
requireonly
small changesto
the
state ofthe
interpreter.
Objects
may be
movedto
orfrom
the
stack,
andthe
instruction
pointeris
alwayschanged;
but
most ofthe
state remainsthe
same.Send
andreturn
bytecodes
may
require muchlarger
changesto
the
interpreter's
state.When
a messageis
sentthe
interpreter's
statemay
have
to
change
in
orderto
execute adifferent
Compiled Method.
Before
switching,the
interpreter's
old stateis
savedin
objects called context.
This
is
known
as "contextswitching".
A
context canbe
aMethod
Context
orBlock
Context.
A
Method
Context
representsthe
execution of aCompiled
Method
in
responseto
a message.A
Block
Context
represents a
block
encounteredin
aCompiled
Method.
A
Block
Context
refersto
the
Method
Context
whoseCompiled
Method
containedthe
block
it
represents.This
is
calledthe
Block
Context's
home
Blocks
are objects usedin many
ofthe
control structuresin
the
Smalltalk-80
System.
A block
represents a
deferred
sequence of actions.A
block
expression consists of a sequence of expressions separated
by
periods anddelimited
by
squarebrackets.
For
example:C index
<-index
+13.
Figure
9.1
shows aMethod
Context
andits
Compiled
Method
and
figure
9.2
shows aBlock
Context
andits
Compiled Method
+ +
|
sender|
+ +
|
ins.
pointer|
+ +
-|
stack pointer|
+ +
/
|
method|
/
+ +
|
(unused)
|
+ +
|
receiver|
+ +
I
I
+ arguments +
I
I
+ +
I
I
+ +
I
I
+
temporaries
+I
I
+
I
+I
.>+ +
I
I
+ stack +
|
contents|
+ +
AAA/\AAAAAAAAAAAA
/->
|
header
/
++
I
+>
I
I
+ +
I
I
+
literal
frame+I
I
+ +
I
I
+ +
I
I
I
+ + +
I
I
I
+
bytecodes
+I
I
I
+ + +
Figure
9.1
Both kinds
of contextshave
sixfields
corresponding
to
sixnamed instance variables.
These
fixed
fields
arefollowed
by
some indexablefields.
The
indexable
fields
are usedto
store
the
temporary
frame
whichincludes
arguments andtemporary
variables.These
arefollowed
by
the
contents of+ +
I
sender|
+ +
|
ins.
pointer|
+ +
--|stackpointer
|
+ +
/
|
method|
/
+ +
|
(unused)
|
+ +
I
receiver|
+ +
I
I
+ arguments +
I
I
+ +I
I
+temporaries
+I
I
->+ +I
I
+ stack +
|
contents|
+ +
I
I
AAAAAAAAAAAAAA
->l
header
1
/+-1
1
1
+ 1 11
4-|
1
1
1
+1
1
literal
frame
1
1
+|
1
1
1
+1
1
1 >
-*--+
|
1
~l1
+ 11
+ i~+
1
1
1
1
1
+<"
1
1 i 1 +1
+-1
bytecodes
1
1
1
+<--1
-+ callerins.
ptr -+I
-+|
stack ptr|
+ +
|
arg. count|
+ +
-|
initial
i.p|
+ +
|
home
|
<
--+ +
I
I
+ +
I
I
+ stack +
|
contents|
+ +
I
AAAAAAAAAAAAAA
Figure
9.2
The
contextthat
representsthe
Compiled
Method
orblock
currently
being
executedis
calledthe
active context.When
the
active context changesthe
stack pointer andthe
instruction
pointer are savedin
the
active contextbefore
a3.2.3
Classes
The
interpreter
finds
the
Compiled Method
whichis
to be
to
executed
in
responseto
a messageby
searching
a messagedictionary.
The
dictionary
is
found
in
the
class ofthe
message receiver or one of
the
superclasses ofthat
class.A
classis
an objectthat
describes
the
implementation
of aset of similar
objects,
and a superclassis
the
parent ofthe
current classC13.
When
a messageis
sent,
the
receiver and arguments areidentified,
andthe
interpreter
searchesfor
the
appropriatemethod
(Figure
10).
+ + + + + +
|
superclass|
/->
|
tally
|
->||
+ +/ + +
/
+ +|
messagediet
|
|
methodarray
|/
|
|
+ + + + + +
|
instance
spec|
I
II
I
4. 4. 4. 4-4-
4-|
||
||
methods|
4. 4- 4.
4-4-+
|
|
|
selectors|
|
I
4. 4. 4. 4- + +
I
II
II
I
A A AAAA AAAAAAAAAAAAA A. +. J I.
I
4"The
technique
usedin
Smalltalk-80
is
to
include
in
eachclass-describing
object adictionary,
calledthe
methoddictionary,
that
associates
selectors with methods.Pointers
to
the
selectorsthat
willbe
sentby
any
methodare
kept
in
the
method(along
with global variable pointersand
bytecodes).
The
bytecodes
that
tell
the
Interpreter
to
send a message encode a
field
offsetin
the
literal
frame
where
the
selectoris
found,
plusthe
number of argumentsthat
the
method needs.By
convention,
the
top
elements ofthe
stack arethe
arguments,
andthe
elementsbelow the
last
argument
is
the
receiver.For
example,
the
sendbytecode
for
the
expression3*4
will stand
for
"sendthe
selectorin
field
A
ofthe
method(which
willbe
*),
andit
takes
one argument".The
interpreter
will askthe
Memory
Manager
for
the A
field
ofthe
method,
will getthe
top
ofthe
stack(4)
asthe
argument,
and will getthe
next elementdown
(3)
asthe
receiver.
It
willlocate
the
receiver'sclass,
its
methoddictionary,
searchit
for
an association ofthe
* selectorwith some method and, when
found,
executethe
method.If
no such associationis
found,
the
searching does
not end.The
receiver's classmay
be
a subclass of anotherclass,
called
its
superclass.If
this
is
the
case,
the
methodfor
check
there.
This
meansthat
each class musthave
afield
that
refersto
its
superclass.The
interpreter
searchesthe
method
dictionary
ofthe
superclass,
an soon,
until eitheran appropriate method
is
found
orit
runs out ofsuperclasses,
in
which case an error occurs.3.2.4
Primitive
Subroutines
Since
methodlookup
alwaysinvolves
searching
the
methoddictionary,
it
is
desirable
to
implement
certainfunctions
in
machine code withoutany
dictionary
lookup
to
reduceoverhead.
These
are:1.
Input
/Output:
connecting
the
Smalltalk-80
systemto
actual
hardware.
2.
Arithmetic:
basic
arithmeticfor
integers.
3.
Subscripting
indexable
objects:fetching
andstoring
indexable
instance
variables(Arrays).
4.
Screen
Graphics:
drawing
andmoving
areas ofthe
screenbitmap
quickly
-5.
Object
allocation:Creation
of new objects.In
Smalltalk-80
these
set of subroutines are calledprimitive subroutines.
These
routines are representedin
that
saysto
runthe
corresponding
subroutine ratherthan
the
Smalltalk-80
bytecodes.
A
few
ofthese
primitivemethods are executed so often
that
eventhe
cost oflooking
them
up
in
their
classes methoddictionary
wouldbe
excessive.
These
methods are represented as special versionof
the
Send
Message
type
ofbytecodes.
For
example,
the
messages
(4-,
-,*,
/)
are representedthis
way.When
the
bytecode
is
executed andthe
top
two
elements ofthe
stackare small
integers,
the
primitive methodis
called as asubroutine.
When
these bytecodes
are executed andthe
top
two
elements are not smallintegers,
the
(4-,
-,*,
/)
messages are sent normally.
Figure
11
shows an examplefor
the
4- primitive method.4-addend
<primitive:l>
|
super4-addend
Small
integer
4-associated with primitive
#1
112
Push
the
receiver(self)
onto stack16
Push
the
contents ofthe
first
temporary
frame
location
(the
argument addend) onto stack.133,32
sendto
super a single argument message withthe
selector
in
the
first
literal
frame
location
(4-)
124
Return
the
object ontop
ofthe
stack asthe
value of
the
message(4-)
Literal
Frame
When
the
interpreter
is
executing
the
bytecode
from
the
Compiled
Method
to
send a message andfinds
one ofthese
flags
set,
it
callsthe
subroutinedirectly
and usesthe
value returned
from
it
asthe
value ofthe
method.If
the
flag
is
not set a newMethod
Context
is
created and madeactive.
Even
when a primitive methodis
indicated
in
the
Compiled
Method,
it
is
possiblefor
the
interpreter
notto
respondsuccessfully.
An
example couldbe
that
the
arguments ofthe
+
may
notbe
smallintegers.
If
the
interpreter
cannotexecute
the
primitive,the
primitivefails
andthe
a message4.0
METHOD
EXECUTION
EXAMPLE
Below
an exampleimplementation
is
described.
A
completetrace
is
included
ofthe
methodexecution,
contextswitching
and responsesto
primitive methods andthe
results returnedby
each message.The
implementation
describes
three
parts(Figure
12).
1.
a class name2.
adeclaration
ofthe
variables availableto
the
instances
3.
the
methods usedby
the
instances
to
respondto
messages.The
example givenbelow
wasdefined
in
the
"Smalltalk-80
The
Language
andits
Implementation"book
C13.
Some
slight modificationhas
been
madeto
the
example.There
are six methods associated withFinancial
History
Class
andthey
describe
sometransaction
recording,
and some queries.Financial
History
is
the
newclass,
whose superclassis
Object.
cashOnHand,
incomes,
expenditures,
moneySpent,
moneyReceived are all
instance
variables ofFinancial
History
and are sharedby
all methodsin
the
Financial
History
Class.
The
six methods"someDemo",
"initialBalance:
" ,"receive
:from:"
, "spend:
for
: ",
"totalReceivedFrom:
", and
"totalSpentFor:
"class
superclass
instance
variable namesFinancialHi story
Object
cashOnHand
incomes
expenditures
moneySpent
moneyReceived
instance
methodssomeDemo
self
initialBalance:
16
self receive:
5
from:
1
self receive:
6
from:
10
self spend:
3
for:
10
self spend:
4
for:
1
moneyReceived <- self totalReceivedFrom:
10
moneySpent <- self totalSpentFor:
1
initialBalance:amount
cashOnHand <- amount
incomes
<-Array
new:10
expenditures <-
Array
new:10
receive: amount
from:
sourceincomes
at: source put: amountcashOnHand <- cashOnHand 4- amount
spend: amount
for:
reasonexpenditures at: reason put:amount
cashOnHand <- cashOnHand
-amount
totalReceivedFrom: source
Incomes
at: sourcetotalSpentFor: reason
Expenditures at: reason
Method
"initialBalance:"initializes
the
Financial
History
class.
It
setsthe
cashOnHand variableto
the
initial
amount(16
dollars).
Then
two
messages are sentto
the
Array
class,
to
createtwo
instances
ofindexed
variables ofdimension
10
each.The
method's(initialBalance:)
data
structure
is
shownin
figure
13.
+-I
4--I
4~12
34
263
|
Size
ofObject
4-|
Object's
Class
4-|
1
argument and3
selectors.(Header)
|
Array
oop
16
|
Array
object pointer4.
4-|
new:(11)
I
Selector
11
for
new:
4-I
10
I
-4-I
-4-I
4-I
4-I
4-I
4-
-4-I
4.
4-Method
initialBalance:Object
Pointer
=76
Argument
10
<-I
literal
frame
<-|
16
|
96
|
64
|
34
|
225
|
97
|
64
I
34
|
225
|
98
1
120
|
bytecodes
I
<-Figure
13
The
263
whichis
in
the
header
field
specifiesthat
there
in
Figure
13.1,
4. 4. 4.__
+ +__
+
I
0|
0|
0|
0|
0|
0|
0|
1|
0|
0|
0|
0|
0|
1|
1|
1|
4. 4. 4___+
+__+
flag
temporary
literal
value count count
Figure
13.1
Method
"receive:
amountfrom:
source"describes
the
methodwhich
handles
income.
The
Compiled
Method
for
"receive
:from: "is
shownin
figure
14:
4-I
-4-I
-4-9
34
|
4.
I
515
|
4. 4.
|
at:put:(13)|
4. 4.
I
4. 4.
|
16
|
240
|
4. 4.
I
4. 4.
I
4-4. 4.
Method
receive:from:
Object
Pointer
=78
Size
Object's
class2
arguments and1
selectorSelector
1
17
16
240
0
16
176
96
120
When
this
methodis
invoked
withthe
amount and sourcearguments,
the
amountis
putinto
the
incomes array
in
the
field
specifiedby
the
sourceindex.
The
amountis
alsoadded
to
cashOnHandinstance
variable.
Method
"spend:
amountfor:
reason"describes
how
the
expenditures are
to be
handled.
When
this
methodis
invoked
with
the
amount and reasonarguments,
the
amountis
putinto
the
expenditures
array
in
the
field
specifiedby
the
reasonindex.
The
amountis
deducted
from
cashOnHand.The
Compiled
Method for
"spend:for:"is
shownin
figure
15:
4.
+
I
9
|
Size
4. 4.
I
34
|
Object's
class4.
+
I
515
|
2
arguments and1
selector4. 4.
|
at:put:(13)
|
Selector
4. 4.
I
2
|
17
|
4.
+
I
16
|
240
|
4. 4.
10
|
16
|
4. 4.
I
177
|
96
|
4. 4.
I
120
|
|
4. 4.
Method
spend:for:
Object
Pointer
=80
Method
"totalReceivedFrom:
source"queries
the
incomes
object
for
a particularitem
(Figure
16).
The
sourceargument specifies
the
index
into
the
incomes
indexed
fields.
The
income
amountfrom
the
specified sourceis
returned.
Here
at: methodis
executedprimitively
by
the
interpreter,
however
the
selector associated with at:is
still
looked
by
in
the
dictionary.
The
method associatedwith
the
selectoris
then
executedby
the
interpreter.
No
context switch occurs.
4-
4-|
6
|
Size
4-
4-|
34
|
Class
4. 4.
|
259
|
1
argument and1
Selector
4-
4-|
at:(12)
|
Selector
4. 4.
I
1
I
16
I
+ 4.
|
224
|
124
|
4. 4.
Method
totalReceivedFrom
Object
Pointer
=82
Figure
16
Method
"totalSpentFor:reason"
queries
the
expenditureobject
for
a particularitem
(Figure
17).
The
reasonargument specifies
the
index
into
the
expendituresindexed
fields.
The
expenditures amountfor
the
specified reasonis
4-
4-I
6
|
Size
4. 4,
I
34
|
Class
4. 4.
I
259
|
1
argument and1
Selector
4. 4.
|
at:(12)
|
Selector
4-
4-I
2
I
16
|
4. 4.
|
224
|
124
|
4. 4.
Method
totalSpentFor:
Object
Pointer
=84
Figure
17
Figure
18
(continued
next page)4-
4-|
33
|
Size
4-
4-|
34
|
Object's
class4. 4.
|
544
|
4. 4.
I
16
|
4. 4.
|
(14)
|
Selector
for
initialBalance:
4. 4.
I
5
|
4. 4.
I
1
I
4-
4-I
6
I
4. 4.
I
2
|
4.
4-|
(15)
|
Selector
for
receive:from:4. 4.
I
3
|
4.
4-I
10
|
4.
4-I
4
|
4.
(16)
+-I
4-I
4--I
+-I
4-2
(17)
1
(18)
|
112
4-|
225
4-|
112
4-|
35
4-|
199
4-|
36
4-|
246
4-I
112
-4-I
-4-I
-4-I
-4-I
-4-I
-4-<Selector
for
spend:for:
Selector
for
totalReceivedFrom:
Selector
for
totalSpentFor:
32
199
34
246
112
37
199
39
251
112
42
199
237
|
40
4-|
199
4-I
41
4-I
251
4-|
44
4-|
99
4-|
46
4.|
100
4|
200
4. 4-<
-Method
someDemoMethod
"someDemo"shown
in
figure
18,
sends various messagesto
each ofthe
above methods(described
earlier)to
performactions.
Here
In
all casesthe
receiveris
someDemo.Appendix
D
showsdumps
by
the
interpreter
ofthe
contents ofvarious
methods,
the
receivervariables,
etc.Bytecodes
199
and
200
are usedfor
dumping
the
receiver's objects andits
contents,
otherthan
that
it
has
no significancein
these
routines.
The
receiver objectinitially
looks
like
(Figure
19)
:4. +
|
7
|
Size
4-
4-|
62
|
Class
(Financial
History)
4-
4-|
|
cashOnHand+
4-|
|
incomes
4. +
|
|
expenditures4-
4-|
|
moneySpent+
4-|
|
moneyReceived4.
4-Receiver's
instance
variablesObject
Pointer
=86
Figure
19
All
its
instance
variablefields
"cashOnHand",
"incomes"
"expenditures",
"moneySpent",
and"moneyReceived"
are set
to
The
dictionary
associated withthe
Financial
History
Class
is
shownin
figure
21.
4--I
4--I
+-I
4--5
16
64
4-4-I
">l -4-I
4-I
-4-1/
-4-4I
4-I
4~I
4I
4I
4I
4--I
4--I
4--I
4-I
4-12
16
66
11
12
13
14
15
16
17
18
-4-4-I
">l-4-I
-4-I
/
-4-I
-4-I
-4-I
-4-I
-4-I
-4-I
-4-I
-4-I
-4-10
I
16
1
68(oop)
1
70(oop)
1
72(oop)
|
76(oop)
1
78(oop)
1
80(oop)
1
82(oop)
1
84(oop)
1
4-Financial
History
Class
Dictionary
Oop
=62
Selectors
Oop
=64
Figure
21
Methods
Oop
=66
The
selectors are chosenarbitrarily
andin
the
Methods
the
object pointer of each of
the
methods are stored.These
objects were created
during
the
initialization ofthe
The
selectors andits
associated numbers usedin
the
dictionary lookup
are shownin
Figure
22:
Selector
Selector
No
new:
11
at:
12
at:put:
13
initialBalance
:14
receive:from:
15
spent:
for
:16
totalReceivedFrom
:17
totalSpentFor
:18
Figure
22
The
object pointersfor
the
methods are shownin
figure
23.
These
object pointers areheld
in
a methodarray
(see
figure
21).
When
the
selectoris
found
in
the
selectors array,the
associated method's object pointer
is
returned.methods
oop
new:
68
at:
70
at:put:
72
initialBalance
:76
receive:from:
78
spend:
for
:80
totalReceivedFrom
:82
totalSpentFor
:84
The
methodsfor
"initialBalance:",
"receive
.-from: "
,
"spent:
for",
"totalReceivedFrom:"and "totalSpentFor:" were
described
earlier.The
selectors"new:",
"at:",
"at:put:"are primitive methods and
the
interpreter
do
not performcontext switch
to
executethese
methods.The
methods(Figure
24)
for
these
objectshave
a specialflag
set whichindicates
to
the
interpreter
that
it
couldrespond
directly.
Usually
these
3
selectors are storedin
aspecial selector array.
However
in
our examplethese
areput
in
the
dictionary
associated withFinancial
History
Class.
4--I
4-I
4-I
4--34
57349
143
new:Object
Pointer
=68
-4-I
-4-I
-4-I
-4-I
-4-4--I
4--I
4--4
34
57349
121
at:Object
Pointer
-4-I
-4-I
-4-I
-4-I
4--I
4--4
34
57349
123
-4-I
-4-I
-4-I
-4-70
at:put:
Object
Pointer
=72
Figure
24
The
header
field
ofthe
Compiled
Method
"new:" shownin
part),
indicates
to
the
interpreter
that
this
method canbe
executed primitively.
The
example showsthe
header
information
ofthe
"new:"method.
The
field
which contains143
(actually
the
header
extension part)
indicates
the
primitiveindex
number.|
1|
1|
1|
0|
0|
0|
0|
0|
0|
0|
0|
0|
0|
1|
0|
1|
4- 4- 4- 4- 4-
4-flag
temporary
literal
value count count
The
header
extensionfor
the
primitiveindex
71
is:
+ 4- 4- +--+
|
0|
0|
0|
0|
0|
0|
0|
0|
1|
0|
0|
0|
1|
1|
1|
1|
4. 4. 4-
44-argument primitive
count
index
Figure
25
The
interpreter checksthe
methodheader
first
to
seeif
aprimitive method
is
indicated.In
this
casethe
flag
valuecontains
7,
which meansthat
a primitive method exists.From
the
header
extension partthe
primitive numberis
fetched.
In
this
exampleit
is
71,
whichis
the
primitiveindex
for
the
method"new:".
From
this
methodthe
When
the
interpreter
is
executing
the
bytecodes
in
the
current method
(Its
objectpointer
is
88
andthe
receiversobject pointer
is
86)
i.e
in
showDemo,
the
Compiled
Method
(also
known
asthe
home
context as well asthe
activecontext)
looks
like
(Figure
25.1):
4-I
4-I
4-I
+I
4-I
4-I
4-I
4-I
4-I
4-I
I
AAAAA/\/\/\AAAAAAAA1
20
1
22
1
0
1
71
1
5
1
88
1
unused1
86
1
Class
(Compiled
Method)
Sender
instruction
pointer stack pointer method\
t-33
receiverCompiled
Method
Object
Pointer
=90
4--I
4--I
4-I
4--I
4-34
544
16
(14)
5
1
-4-I
-4-I
-4-I
-4-I
-4-I
-+I
-4-I
-4-AAAAAAAAAAAAAAAAA method(someDemo)
Object
Pointer
=88
Intitally
the
home
context andthe
active context arethe
same
(they
both
have
the
same object pointers).Bytecode
112
instructs
the
interpreter
to
pushthe
receiver(object
pointer
is
86)
into
the
stack.Bytecode
32
pushesthe
argument
16
(initialBalance: 16)
into
the
stack.When