[comp.lang.smalltalk] How does the Smalltalk compiler work?

adamk@mit-amt.MEDIA.MIT.EDU (Adam Kao) (01/08/90)

I'm trying to understand the Smalltalk compiler and Goldberg and
Robson are almost no help.  Is the Smalltalk compiler a traditional
compiler with a yaccable grammar?  If not, what steps does it go
through in translating source to bytecodes?  How do object-orientation
and message passing change the way expressions are parsed?

In a yacc grammar for C I would find separate rules for if-then,
while, etc.  I notice in the back of Goldberg and Robson there is only
one track diagram for blocks.  Where is the information on how to
generate code for control structures?

Basically I just need to know how the Smalltalk compiler is similar to
(for example) an ordinary C compiler, and how it is different.  I also
would like to know which differences are directly caused by object
oriented features (inheritance, etc.) and which are more incidental.

Please excuse my naive questions; I don't have access to a Smalltalk
system and learning from the book is frustrating.

Thank you for your time,
Adam

schang@netcom.UUCP (Sehyo Chang) (01/09/90)

In article <1340@mit-amt.MEDIA.MIT.EDU> adamk@media-lab.media.mit.edu.UUCP (Adam Kao) writes:
>I'm trying to understand the Smalltalk compiler and Goldberg and
>Robson are almost no help.  Is the Smalltalk compiler a traditional
>compiler with a yaccable grammar?  If not, what steps does it go
Smalltalk is recursive-descent parser(at least in the VM).

>through in translating source to bytecodes?  How do object-orientation
>and message passing change the way expressions are parsed?
Smalltalk expression directly compiled one-to-one into bytecodes.
For example, in the back of the blue book, under section "The Implementation"
smalltalk expression "origin + corner / 2" would translated into something
like following bytecodes,
      0		"push value of 'origin' instance value onto stack
      1        "push value of 'corner' instance value onto stack
      176      "send binary message with the selector +"
      119      "push the SmallInteger 2 onto the stack"
      185      "push a binary message with the selector /"
      124      "return the object on the top of the stack"
Remember, Smalltalk VM is stack-orienteted architecture interpterter,thus
all operation is done through stack manipulation and it's language syntax
reflects through left-right expression syntax(with some sugar coating).

>
>In a yacc grammar for C I would find separate rules for if-then,
>while, etc.  I notice in the back of Goldberg and Robson there is only
>one track diagram for blocks.  Where is the information on how to
>generate code for control structures?
because, there are not control structure built into smalltalk, all condition
evaluation are done through polymorphosim(hard to spell, yeh?) evaluation.
Obviously, you havn't fully understood basic concepts of smalltalk.  
For example, expression 'x > 1 ifTrue: [exp1] ifFalse: [exp2]' are normal
smalltalk expression with no control structure.  Both 'ifTrue' and 'ifFalse'
methods are in implemented in 'Boolean' class, but differently in 'True'
and 'False' class.  For reference, read 1981 Byte articles on smalltalk.

>Basically I just need to know how the Smalltalk compiler is similar to
>(for example) an ordinary C compiler, and how it is different.  I also
It is not matter of difference in compliler, it is matter of difference
between compiler(source-host machine) to interpreter(source-VM bytecodes),
In Smalltalk, you don't just consider language, you have to consider
more than 300 library classes(some are more important than others).

>
>Please excuse my naive questions; I don't have access to a Smalltalk
>system and learning from the book is frustrating.
There are no naive questions, we all learn from simple questions. But you
should play with smalltalk to really appreciate power and simplicity of 
Smalltalk system.  There are two cheap ways to do it. (1) Get "little Smalltalk
", or (2) Smalltalk/V from Digitalk.  But if you have some money, get  a
ParcPlace's Smalltalk, they are more true to the original smalltalk-80.

--------------
Sehyo Chang
Ascent Logic Corp.
180 Rose Orchard Way
San Jose, CA 95134
(408) 943-0630
schang@netcom.uucp

new@udel.edu (Darren New) (01/09/90)

In article <5478@netcom.UUCP> schang@netcom.UUCP (Sehyo Chang) writes:
>In article <1340@mit-amt.MEDIA.MIT.EDU> adamk@media-lab.media.mit.edu.UUCP (Adam Kao) writes:
>For example, expression 'x > 1 ifTrue: [exp1] ifFalse: [exp2]' are normal
>smalltalk expression with no control structure.  Both 'ifTrue' and 'ifFalse'
>methods are in implemented in 'Boolean' class, but differently in 'True'
>and 'False' class.  For reference, read 1981 Byte articles on smalltalk.

True up to a point, but then did you ever look at the whileTrue: message
works?  In actuallity, the compiler (at least the original ST-80 compiler)
had special-case code for ifTrue:, whileTrue:, et al, and generated
inline jump instructions (primarily for efficiency).  Otherwise,
such common stuff would be difficult to do efficiently and while-loops
would require recursive implementation, quickly running you out
of stack space. 


>>Basically I just need to know how the Smalltalk compiler is similar to
>>(for example) an ordinary C compiler, and how it is different.  I also
>It is not matter of difference in compliler, it is matter of difference
>between compiler(source-host machine) to interpreter(source-VM bytecodes),

All the Smalltalk compilers I have studied the internals of use 
recursive-descent parsing; this topic should be findable in any
reasonable compiler book.  No tables are used -- basically the 
program-counter encodes the major state of the parsing.

>...  There are two cheap ways to do it. (1) Get "little Smalltalk
>", or (2) Smalltalk/V from Digitalk.  But if you have some money, get  a
>ParcPlace's Smalltalk, they are more true to the original smalltalk-80.

Actually, Smalltalk-80 is only about $200 for Macs and IBMclones and
about $400 for Suns if you can get the educational discount; e.g.,
if your school buys it.  I think it is well worth it, myself, and their
support is super.  Don't quote me on prices. Send mail to
info@parcplace.com or info@parcplace.uucp.   LossaLuck -- Darren

adamk@mit-amt.MEDIA.MIT.EDU (Adam Kao) (01/12/90)

Thanks to all who replied.

Adam