MessageNode.st
author claus
Thu, 17 Nov 1994 15:23:23 +0100
changeset 49 02660b790c3e
parent 48 86595b70333e
child 52 d80ec10c3321
permissions -rw-r--r--
*** empty log message ***

"
 COPYRIGHT (c) 1989 by Claus Gittinger
	      All Rights Reserved

 This software is furnished under a license and may be used
 only in accordance with the terms of that license and with the
 inclusion of the above copyright notice.   This software may not
 be provided or otherwise made available to, or used by, any
 other person.  No title to or ownership of the software is
 hereby transferred.
"

ParseNode subclass:#MessageNode
       instanceVariableNames:'receiver selector argArray lineNr'
       classVariableNames:''
       poolDictionaries:''
       category:'System-Compiler-Support'
!

MessageNode comment:'
COPYRIGHT (c) 1989 by Claus Gittinger
	      All Rights Reserved

$Header: /cvs/stx/stx/libcomp/MessageNode.st,v 1.14 1994-11-17 14:22:46 claus Exp $
'!

!MessageNode class methodsFor:'documentation'!

copyright
"
 COPYRIGHT (c) 1989 by Claus Gittinger
	      All Rights Reserved

 This software is furnished under a license and may be used
 only in accordance with the terms of that license and with the
 inclusion of the above copyright notice.   This software may not
 be provided or otherwise made available to, or used by, any
 other person.  No title to or ownership of the software is
 hereby transferred.
"
!

version
"
$Header: /cvs/stx/stx/libcomp/MessageNode.st,v 1.14 1994-11-17 14:22:46 claus Exp $
"
!

documentation
"
    node for parse-trees, representing message sends
"
! !

!MessageNode class methodsFor:'instance creation'!

receiver:recNode selector:selectorString 
    ^ (self basicNew) receiver:recNode selector:selectorString args:nil lineno:0
!

receiver:recNode selector:selectorString arg:argNode
    ^ self receiver:recNode selector:selectorString arg:argNode fold:true
!

receiver:recNode selector:selectorString arg:argNode fold:folding
    |result recVal argVal selector|

   "
     The constant folding code can usually not optimize much
     - this may change when some kind of constant/macro declaration is added to smalltalk.
    "
    folding ifTrue:[
	"do constant folding ..."
	(recNode isConstant and:[argNode isConstant]) ifTrue:[
	    "check if we can do it ..."
	    selectorString knownAsSymbol ifTrue:[
		selector := selectorString asSymbol.
		recVal := recNode evaluate.
		(recVal respondsTo:selector) ifTrue:[
		    "
		     we could do much more here - but then, we need a dependency from
		     the folded selectors method to the method we generate code for ...
		     limit optimizations to those that will never change
		     (or - if you change them - you will crash so bad ...)
		    "
		    argVal := argNode evaluate.
		    (recVal respondsToArithmetic and:[argVal respondsToArithmetic]) ifTrue:[
			(#( @ + - * / // \\ min: max: quo:) includes:selector) ifTrue:[
			    (#( / // \\ ) includes:selector) ifTrue:[
				argVal = 0 ifTrue:[
				    ^ 'division by zero in constant expression'
				].
			    ].
			    (SignalSet anySignal catch:[
				result := recVal perform:selector with:argVal.
			    ]) ifTrue:[
				^ 'error in constant expression'
			    ].
			    ^ ConstantNode type:(ConstantNode typeOfConstant:result) value:result
			]
		    ].
		    (recVal isMemberOf:String) ifTrue:[
			argVal isInteger ifTrue:[
			    (selector == #at:) ifTrue:[
				(SignalSet anySignal catch:[
				    result := recVal perform:selector with:argVal.
				]) ifTrue:[
				    ^ 'error in constant expression'
				].
				^ ConstantNode type:(ConstantNode typeOfConstant:result) value:result
			    ]
			].
			(argVal isMemberOf:String) ifTrue:[
			    (selector == #',') ifTrue:[
				(SignalSet anySignal catch:[
				    result := recVal perform:selector with:argVal.
				]) ifTrue:[
				    ^ 'error in constant expression'
				].
				^ ConstantNode type:(ConstantNode typeOfConstant:result) value:result
			    ]
			]
		    ]
		]
	    ]
	]
    ].
    ^ (self basicNew) receiver:recNode selector:selectorString args:(Array with:argNode) lineno:0
!

receiver:recNode selector:selectorString arg1:argNode1 arg2:argNode2 fold:folding
    |result recVal argVal selector|

    "
     This is just a demonstration - of how complex constants can be folded.
     This was inspired by some discussion in c.l.s about enhancing the language - I prefer
     enhancing the compiler ....
     The following optimization will convert '#(...) with:#(...) collect:[...]' into an array constant,
     allowing a constant arrays of complex objects.

     Notice: this method is normally disabled - its just a demo after all.
    "
    folding ifTrue:[
	"do constant folding ..."
	(recNode isConstant and:[argNode1 isConstant]) ifTrue:[
	    "check if we can do it ..."
	    selectorString knownAsSymbol ifTrue:[
		selector := selectorString asSymbol.
		recVal := recNode evaluate.
		(recVal respondsTo:selector) ifTrue:[
		    "
		     we could do much more here - but then, we need a dependency from
		     the folded selectors method to the method we generate code for ...
		     limit optimizations to those that will never change
		     (or - if you change them - you will crash so bad ...)
		    "
		    argVal := argNode1 evaluate.
		    ((recVal isMemberOf:Array) and:[argVal isMemberOf:Array]) ifTrue:[
			(selector == #with:collect:) ifTrue:[
			    (argNode2 isMemberOf:BlockNode) ifTrue:[
				(SignalSet anySignal catch:[
				    result := recVal perform:selector with:argVal with:(argNode2 evaluate).
				]) ifTrue:[
				    ^ 'error in constant expression'
				].
				^ ConstantNode type:(ConstantNode typeOfConstant:result) value:result
			    ]
			]
		    ]
		]
	    ]
	]
    ].
    ^ (self basicNew) receiver:recNode selector:selectorString args:(Array with:argNode1 with:argNode2) lineno:0
!

receiver:recNode selector:selectorString args:anArray
    ^ self receiver:recNode selector:selectorString args:anArray fold:true
!

receiver:recNode selector:selectorString args:argArray fold:folding
    folding ifTrue:[
	(argArray size == 1) ifTrue:[
	    ^ self receiver:recNode selector:selectorString arg:(argArray at:1) fold:true 
	].

	"uncomment the follwoing for a nice array initializer optimization ..."
"/        (argArray size == 2) ifTrue:[
"/            ^ self receiver:recNode selector:selectorString arg1:(argArray at:1) arg2:(argArray at:2) fold:true 
"/        ].
    ].
    ^ (self basicNew) receiver:recNode selector:selectorString args:argArray lineno:0
! !

!MessageNode methodsFor:'accessing'!

receiver:r selector:s args:a lineno:l
    receiver := r.
    selector := s asSymbol.
    argArray := a.
    lineNr := l
!

receiver
    ^ receiver
!

selector
    ^ selector
!

args
    ^ argArray
!

arg1
    ^ argArray at:1
!

lineNumber
     ^ lineNr
!

lineNumber:num
     lineNr := num
! !

!MessageNode methodsFor:'queries'!

isMessage
    ^ true
! !

!MessageNode class methodsFor:'queries'!

hasLineNumber:sel
    "return true, if special send code needs lineNr"

    (sel == #==) ifTrue:[^ false].
    (sel == #~~) ifTrue:[^ false].
    (sel == #class) ifTrue:[^ false].
    ^ true
!

isBuiltInUnarySelector:sel
    "return true, if unary selector sel is built-in. 
     (i.e. there is a single bytecode for it)"

    (sel == #peek) ifTrue:[^ true].
    (sel == #value) ifTrue:[^ true].
    (sel == #next) ifTrue:[^ true].
    (sel == #class) ifTrue:[^ true].
    (sel == #size) ifTrue:[^ true].
    (sel == #x) ifTrue:[^ true].
    (sel == #y) ifTrue:[^ true].
    (sel == #width) ifTrue:[^ true].
    (sel == #height) ifTrue:[^ true].
    (sel == #origin) ifTrue:[^ true].
    (sel == #extent) ifTrue:[^ true].
    (sel == #asInteger) ifTrue:[^ true].
    (sel == #rounded) ifTrue:[^ true].
    ^ false
!

isBuiltIn1ArgSelector:sel
    "return true, if selector sel is built-in.
     (i.e. there is a single bytecode for it)"

    (sel == #at:) ifTrue:[^ true].
    (sel == #value:) ifTrue:[^ true].
    (sel == #bitAnd:) ifTrue:[^ true].
    (sel == #bitOr:) ifTrue:[^ true].
    ^ false
!

isBuiltIn2ArgSelector:sel
    "return true, if selector sel is built-in.
     (i.e. there is a single bytecode for it)"

    (sel == #at:put:) ifTrue:[^ true].
    ^ false
! !

!MessageNode methodsFor:'printing'!

printOn:aStream indent:i
    |needParen selectorParts index index2 arg|

    (#(whileTrue: whileFalse:) includes:selector) ifTrue:[
	(receiver isKindOf:BlockNode) ifTrue:[
	    ^ self printWhileOn:aStream indent:i
	].
    ].

    index := 1.
    selectorParts := OrderedCollection new.
    [index == 0] whileFalse:[
	index2 := selector indexOf:$: startingAt:index.
	index2 ~~ 0 ifTrue:[
	    selectorParts add:(selector copyFrom:index to:index2).
	    index2 := index2 + 1
	].
	index := index2
    ].

    needParen := false.
    receiver isMessage ifTrue:[
	receiver isUnaryMessage ifFalse:[
	    receiver isBinaryMessage ifFalse:[
		needParen := true
	    ].
	].
    ].
    needParen ifTrue:[
	aStream nextPutAll:'('
    ].
    receiver printOn:aStream indent:i.
    needParen ifTrue:[
	aStream nextPutAll:')'
    ].

    1 to:(argArray size) do:[:argIndex |
	aStream space.
	(selectorParts at:argIndex) printOn:aStream.
	aStream space.
	arg := argArray at:argIndex.
	needParen := false.
	arg isMessage ifTrue:[
	    arg isBinaryMessage ifFalse:[
		arg isUnaryMessage ifFalse:[
		    needParen := true
		]
	    ].
	].
	needParen ifTrue:[
	    aStream nextPutAll:'('
	].
	arg printOn:aStream indent:i.
	needParen ifTrue:[
	    aStream nextPutAll:') '
	].
    ]
!

printWhileOn:aStream indent:i
    |needParen arg|

    "special handling of whileTrue/whileFalse"

    aStream nextPutAll:'['.
    receiver statements printOn:aStream indent:i.
    aStream nextPutAll:'] whileTrue: '.

    arg := argArray at:1.
    needParen := false.
    arg isMessage ifTrue:[
	arg isBinaryMessage ifFalse:[
	    arg isUnaryMessage ifFalse:[
		needParen := true
	    ]
	].
    ].
    needParen ifTrue:[
	aStream nextPutAll:'('
    ].
    arg printOn:aStream indent:i.
    needParen ifTrue:[
	aStream nextPutAll:') '
    ].
! !

!MessageNode methodsFor:'checks'!

plausibilityCheck
    |rec arg operand|

    "
     it once took me almost an hour, to find a '==' which
     should have been an '=' (you cannot compare floats with ==)
     (well, I looked at the '==' at least 50 times -
      - but didn't think about it ...).
     thats reason enough to add this check here.
     I will add more as heuristic knowledge increases ...
     (send me comments on common programming errors ...)
    "

    "
     check #== appled to Floats, Strings or Fractions
    "
    ((selector == #==) or:[selector == #~~]) ifTrue:[
	receiver isConstant ifTrue:[
	    rec := receiver evaluate.
	    ((rec isMemberOf:String) or:[
	     (rec isMemberOf:Float) or:[
	     (rec isMemberOf:Fraction)]]) ifTrue:[
		operand := rec
	    ].
	].
	(argArray at:1) isConstant ifTrue:[
	    arg := (argArray at:1) evaluate.
	    ((arg isMemberOf:String) or:[
	     (arg isMemberOf:Float) or:[
	     (arg isMemberOf:Fraction)]]) ifTrue:[
		operand := arg
	    ].
	].
	operand notNil ifTrue:[
	    (selector == #==) ifTrue:[
		^ 'identity compare is unsafe here'
	    ].
	    ^ 'identity compare will usually return true here'
	]
    ].

    "
     [...] ifTrue:...
     an error often occuring when you are a beginner ...
    "
    ((selector == #ifTrue:) or:[selector == #ifFalse:]) ifTrue:[
	receiver isBlock ifTrue:[
	    (Block canUnderstand:selector) ifFalse:[
		^ 'blocks usually do not respond to ' , selector , ' messages'
	    ].
	].
	(argArray at:1) isBlock ifFalse:[
	    ^ 'will fail at runtime, if argument to ' , selector , ' does not evaluate to a block'
	]
    ].
    ((selector == #ifTrue:ifFalse:) or:[selector == #ifFalse:ifTrue:]) ifTrue:[
	receiver isBlock ifTrue:[
	    (Block canUnderstand:selector) ifFalse:[
		^ 'blocks usually do not respond to ' , selector , ' messages'
	    ].
	].
	(argArray at:1) isBlock ifFalse:[
	    ^ 'will fail at runtime, if 1st. argument to ' , selector , ' does not evaluate to a block'
	].
	(argArray at:2) isBlock ifFalse:[
	    ^ 'will fail at runtime, if 2nd. argument to ' , selector , ' does not evaluate to a block'
	]
    ].

    "
     (...) whileTrue:[
    "
    ((selector == #whileTrue:) or:[selector == #whileFalse:]) ifTrue:[
	receiver isBlock ifFalse:[
	    "
	     only warn, if code was originally parenthized
	    "
	    receiver parenthized ifTrue:[
		^ 'will fail at runtime, if receiver of ' , selector , ' does not evaluate to a block'
	    ]
	].
	(argArray at:1) isBlock ifFalse:[
	    ^ 'will fail at runtime, if argument to ' , selector , ' does not evaluate to a block'
	].
    ].
    ^ nil
! !

!MessageNode methodsFor:'evaluating'!

evaluate
    |r nargs argValueArray class|

    receiver isSuper ifTrue:[
	r := receiver value.
	receiver isHere ifTrue:[
	    class := receiver definingClass.
	] ifFalse:[
	    class := receiver definingClass superclass.
	].
	argArray notNil ifTrue:[
	    argValueArray := argArray collect:[:arg | arg evaluate].
	] ifFalse:[
	    argValueArray := #()
	].
	^ r perform:selector inClass:class withArguments:argValueArray
    ].


    argArray isNil ifTrue:[
	^ (receiver evaluate) perform:selector
    ].
    nargs := argArray size.
    (nargs == 1) ifTrue:[
	^ (receiver evaluate) perform:selector with:(argArray at:1) evaluate
    ].
    (nargs == 2) ifTrue:[
	^ (receiver evaluate) perform:selector
				 with:(argArray at:1) evaluate
				 with:(argArray at:2) evaluate
    ].
    (nargs == 3) ifTrue:[
	^ (receiver evaluate) perform:selector
				 with:(argArray at:1) evaluate
				 with:(argArray at:2) evaluate
				 with:(argArray at:3) evaluate
    ].
    r := receiver evaluate.
    argValueArray := argArray collect:[:arg | arg evaluate].
    ^ r perform:selector withArguments:argValueArray
!

evaluateForCascade
    |r nargs argValueArray class|

    receiver isSuper ifTrue:[
	r := receiver value.
	receiver isHere ifTrue:[
	    class := receiver definingClass.
	] ifFalse:[
	    class := receiver definingClass superclass.
	].
	argArray notNil ifTrue:[
	    argValueArray := argArray collect:[:arg | arg evaluate].
	] ifFalse:[
	    argValueArray := #()
	].
	r perform:selector inClass:class withArguments:argValueArray.
	^ r
    ].

    r := receiver evaluate.
    argArray isNil ifTrue:[
	r perform:selector.
	^ r
    ].
    nargs := argArray size.
    (nargs == 1) ifTrue:[
	r perform:selector with:(argArray at:1) evaluate.
	^ r
    ].
    (nargs == 2) ifTrue:[
	r perform:selector with:(argArray at:1) evaluate
			   with:(argArray at:2) evaluate.
	^ r
    ].
    (nargs == 3) ifTrue:[
	r perform:selector with:(argArray at:1) evaluate
			   with:(argArray at:2) evaluate
			   with:(argArray at:3) evaluate.
	^ r
    ].
    argValueArray := argArray collect:[:arg | arg evaluate].
    r perform:selector withArguments:argValueArray.
    ^ r
! !

!MessageNode methodsFor:'code generation'!

codeForSideEffectOn:aStream inBlock:b
    self codeOn:aStream inBlock:b valueNeeded:false
!

codeOn:aStream inBlock:b
    self codeOn:aStream inBlock:b valueNeeded:true
!

optimizedConditionFor:aReceiver with:aByteCode
    |rec sel|

    rec := aReceiver.
    (rec isBlock "class == BlockNode") ifTrue:[
	rec statements nextStatement isNil ifTrue:[
	    rec := rec statements expression
	]
    ].
    (rec isUnaryMessage "class == UnaryNode") ifTrue:[
	sel := rec selector.
	(sel == #isNil) ifTrue:[
	    (aByteCode == #trueJump) ifTrue:[^ #nilJump].
	    (aByteCode == #falseJump) ifTrue:[^ #notNilJump]
	].
	(sel == #notNil) ifTrue:[
	    (aByteCode == #trueJump) ifTrue:[^ #notNilJump].
	    (aByteCode == #falseJump) ifTrue:[^ #nilJump]
	].
	(sel == #not) ifTrue:[
	    (aByteCode == #trueJump) ifTrue:[^ #falseJump].
	    (aByteCode == #falseJump) ifTrue:[^ #trueJump]
	].
	^ nil
    ].
    (rec isBinaryMessage "class == BinaryNode") ifTrue:[
	sel := rec selector.
	rec arg1 isConstant ifTrue:[
	    (rec arg1 value == 0) ifTrue:[
		(sel == #==) ifTrue:[
		    (aByteCode == #trueJump) ifTrue:[^ #zeroJump].
		    (aByteCode == #falseJump) ifTrue:[^ #notZeroJump]
		].
		(sel == #~~) ifTrue:[
		    (aByteCode == #falseJump) ifTrue:[^ #zeroJump].
		    (aByteCode == #trueJump) ifTrue:[^ #notZeroJump]
		].
		^ nil
	    ]
	].
	(sel == #==) ifTrue:[
	    (aByteCode == #trueJump) ifTrue:[^ #eqJump].
	    (aByteCode == #falseJump) ifTrue:[^ #notEqJump]
	].
	(sel == #~~) ifTrue:[
	    (aByteCode == #falseJump) ifTrue:[^ #eqJump].
	    (aByteCode == #trueJump) ifTrue:[^ #notEqJump]
	]
    ].
    ^ nil
!

codeWhileOn:aStream inBlock:b valueNeeded:valueNeeded
    "generate code for [...] whilexxx:[ ... ]"

    |pos pos2 theReceiver theArg theByteCode optByteCode|

    (selector == #whileTrue:) ifTrue:[
	theByteCode := #falseJump
    ] ifFalse:[
	theByteCode := #trueJump
    ].

    theReceiver := receiver.
    optByteCode := self optimizedConditionFor:theReceiver with:theByteCode.
    optByteCode notNil ifTrue:[
	((optByteCode == #eqJump) or:[optByteCode == #notEqJump]) ifTrue:[
	    theArg := receiver statements expression arg1
	].
	theReceiver := receiver statements expression receiver.
	theByteCode := optByteCode
    ].

    valueNeeded ifTrue:[aStream nextPut:#pushNil].
    pos := aStream position.
    optByteCode notNil ifTrue:[
	theReceiver codeOn:aStream inBlock:b.
	theArg notNil ifTrue:[
	    theArg codeOn:aStream inBlock:b
	]
    ] ifFalse:[
	theReceiver codeInlineOn:aStream inBlock:b
    ].
    aStream nextPut:theByteCode.
    pos2 := aStream position.
    aStream nextPut:0.
    valueNeeded ifTrue:[aStream nextPut:#drop].
    (argArray at:1) codeInlineOn:aStream inBlock:b valueNeeded:valueNeeded.
    aStream nextPut:#jump.
    aStream nextPut:pos.
    (aStream contents) at:pos2 put:(aStream position)
!

codeTimesRepeatOn:aStream inBlock:b valueNeeded:valueNeeded
    "generate code for n timesRepeat:[ ... ]"

    |pos pos2 theReceiver|

    theReceiver := receiver.
    theReceiver codeOn:aStream inBlock:b.
    valueNeeded ifTrue:[aStream nextPut:#dup].

    pos := aStream position.
    aStream nextPut:#dup.
    aStream nextPut:#push0.
    aStream nextPut:#>.
    aStream nextPut:lineNr.
    aStream nextPut:#falseJump.
    pos2 := aStream position.
    aStream nextPut:0.

    (argArray at:1) codeInlineOn:aStream inBlock:b valueNeeded:false.
    aStream nextPut:#minus1.
    aStream nextPut:lineNr.
    aStream nextPut:#jump.
    aStream nextPut:pos.

    (aStream contents) at:pos2 put:(aStream position)
!

codeIfElseOn:aStream inBlock:b valueNeeded:valueNeeded
    "generate code for x ifxxx:[ ... ] yyy:[ ...]"

    |pos pos2 theReceiver theArg theByteCode optByteCode|

    theReceiver := receiver.
    (selector == #ifTrue:ifFalse:) ifTrue:[
	theByteCode := #falseJump
    ] ifFalse:[
	(selector == #ifFalse:ifTrue:) ifTrue:[
	    theByteCode := #trueJump
	]
    ].
    optByteCode := self optimizedConditionFor:theReceiver
					 with:theByteCode.
    optByteCode notNil ifTrue:[
	((optByteCode == #eqJump) or:[optByteCode == #notEqJump]) ifTrue:[
	    theArg := theReceiver arg1
	].
	theReceiver := theReceiver receiver.
	theByteCode := optByteCode
    ].
    theByteCode notNil ifTrue:[
	theReceiver codeOn:aStream inBlock:b.
	theArg notNil ifTrue:[
	    theArg codeOn:aStream inBlock:b
	].
	aStream nextPut:theByteCode.
	pos := aStream position.
	aStream nextPut:0.
	(argArray at:1) codeInlineOn:aStream inBlock:b valueNeeded:valueNeeded.
	aStream nextPut:#jump.
	pos2 := aStream position.
	aStream nextPut:0.
	(aStream contents) at:pos put:(aStream position).
	(argArray at:2) codeInlineOn:aStream inBlock:b valueNeeded:valueNeeded.
	(aStream contents) at:pos2 put:(aStream position)
    ]
!

codeIfOn:aStream inBlock:b valueNeeded:valueNeeded
    "generate code for x ifxxx:[ ... ]"

    |pos pos2 theReceiver theArg theByteCode optByteCode subsel|

    theReceiver := receiver.

    (theReceiver isMessage "class == MessageNode") ifTrue:[
	subsel := theReceiver selector.
	(subsel == #and:) ifTrue:[
	    self codeAndIfOn:aStream inBlock:b valueNeeded:valueNeeded.
	    ^ self
	].
	(subsel == #or:) ifTrue:[
	    self codeOrIfOn:aStream inBlock:b valueNeeded:valueNeeded.
	    ^ self
	]
    ].
    (selector == #ifTrue:) ifTrue:[
	theByteCode := #falseJump
    ] ifFalse:[
	theByteCode := #trueJump
    ].
    optByteCode := self optimizedConditionFor:theReceiver
					 with:theByteCode.
    optByteCode notNil ifTrue:[
	((optByteCode == #eqJump) or:[optByteCode == #notEqJump]) ifTrue:[
	    theArg := theReceiver arg1
	].
	theReceiver := theReceiver receiver.
	theByteCode := optByteCode
    ].

    theReceiver codeOn:aStream inBlock:b.
    theArg notNil ifTrue:[
	theArg codeOn:aStream inBlock:b
    ].
    aStream nextPut:theByteCode.
    pos := aStream position.
    aStream nextPut:0.
    (argArray at: 1) codeInlineOn:aStream inBlock:b valueNeeded:valueNeeded.
    valueNeeded ifTrue:[
	aStream nextPut:#jump.
	pos2 := aStream position.
	aStream nextPut:0.
	(aStream contents) at:pos put:(aStream position).
	aStream nextPut:#pushNil.
	(aStream contents) at:pos2 put:(aStream position)
    ] ifFalse:[
	(aStream contents) at:pos put:(aStream position)
    ]
!

codeAndIfOn:aStream inBlock:b valueNeeded:valueNeeded
    "generate code for (x and:[y]) ifxxx:[ ... ]"

    |theByteCode optByteCode theReceiver theArg pos1 pos2 pos3|


    theByteCode := #falseJump.
    theReceiver := receiver receiver.

    optByteCode := self optimizedConditionFor:theReceiver
					 with:theByteCode.
    optByteCode notNil ifTrue:[
	((optByteCode == #eqJump) or:[optByteCode == #notEqJump]) ifTrue:[
	    theArg := theReceiver arg1
	].
	theReceiver := theReceiver receiver.
	theByteCode := optByteCode
    ].
    theReceiver codeOn:aStream inBlock:b.
    theArg notNil ifTrue:[
	theArg codeOn:aStream inBlock:b
    ].
    aStream nextPut:theByteCode.
    pos1 := aStream position.
    aStream nextPut:0.

    theReceiver := receiver arg1.
    theReceiver codeInlineOn:aStream inBlock:b.
    (selector == #ifTrue:) ifTrue:[
	aStream nextPut:#falseJump
    ] ifFalse:[
	aStream nextPut:#trueJump
    ].
    pos2 := aStream position.
    aStream nextPut:0.
    (selector == #ifFalse:) ifTrue:[
	(aStream contents) at:pos1 put:(aStream position)
    ].
    (argArray at: 1) codeInlineOn:aStream inBlock:b valueNeeded:valueNeeded.
    valueNeeded ifTrue:[
	aStream nextPut:#jump.
	pos3 := aStream position.
	aStream nextPut:0.
	(selector == #ifTrue:) ifTrue:[
	    (aStream contents) at:pos1 put:(aStream position)
	].
	(aStream contents) at:pos2 put:(aStream position).
	aStream nextPut:#pushNil.
	(aStream contents) at:pos3 put:(aStream position)
    ] ifFalse:[
	(selector == #ifTrue:) ifTrue:[
	    (aStream contents) at:pos1 put:(aStream position)
	].
	(aStream contents) at:pos2 put:(aStream position)
    ]
!

codeOrIfOn:aStream inBlock:b valueNeeded:valueNeeded
    "generate code for (x or:[y]) ifxxx:[ ... ]"

    |theByteCode optByteCode theReceiver theArg pos1 pos2 pos3|

    theByteCode := #trueJump.
    theReceiver := receiver receiver.

    optByteCode := self optimizedConditionFor:theReceiver with:theByteCode.
    optByteCode notNil ifTrue:[
	((optByteCode == #eqJump) or:[optByteCode == #notEqJump]) ifTrue:[
	    theArg := theReceiver arg1
	].
	theReceiver := theReceiver receiver.
	theByteCode := optByteCode
    ].
    theReceiver codeOn:aStream inBlock:b.
    theArg notNil ifTrue:[
	theArg codeOn:aStream inBlock:b
    ].
    aStream nextPut:theByteCode.
    pos1 := aStream position.
    aStream nextPut:0.


    theReceiver := receiver arg1.

"new:"
    (selector == #ifTrue:) ifTrue:[
	theByteCode := #falseJump
    ] ifFalse:[
	theByteCode := #trueJump
    ].
    optByteCode := self optimizedConditionFor:theReceiver with:theByteCode.
    optByteCode notNil ifTrue:[
	theReceiver isBlock ifTrue:[
	    theReceiver := theReceiver statements expression
	].
	((optByteCode == #eqJump) or:[optByteCode == #notEqJump]) ifTrue:[
	    theArg := theReceiver arg1
	].
	theReceiver := theReceiver receiver.
	theByteCode := optByteCode.

	theReceiver codeOn:aStream inBlock:b.
	theArg notNil ifTrue:[
	    theArg codeOn:aStream inBlock:b
	].
	aStream nextPut:theByteCode.

    ] ifFalse:[
"org"
	theReceiver codeInlineOn:aStream inBlock:b.
	(selector == #ifTrue:) ifTrue:[
	    aStream nextPut:#falseJump
	] ifFalse:[
	    aStream nextPut:#trueJump
	].
    ].
    pos2 := aStream position.
    aStream nextPut:0.
    (selector == #ifTrue:) ifTrue:[
	(aStream contents) at:pos1 put:(aStream position)
    ].
    (argArray at: 1) codeInlineOn:aStream inBlock:b valueNeeded:valueNeeded.
    valueNeeded ifTrue:[
	aStream nextPut:#jump.
	pos3 := aStream position.
	aStream nextPut:0.
	(selector == #ifFalse:) ifTrue:[
	    (aStream contents) at:pos1 put:(aStream position)
	].
	(aStream contents) at:pos2 put:(aStream position).
	aStream nextPut:#pushNil.
	(aStream contents) at:pos3 put:(aStream position)
    ] ifFalse:[
	(selector == #ifFalse:) ifTrue:[
	    (aStream contents) at:pos1 put:(aStream position)
	].
	(aStream contents) at:pos2 put:(aStream position)
    ]
!

codeAndOrOn:aStream inBlock:b valueNeeded:valueNeeded
    "generate code for x and/or:[y] - but not in an if"

    |pos theReceiver theByteCode|

self halt.
    theReceiver := receiver.
    (selector == #and:) ifTrue:[
	theByteCode := #falseJump
    ] ifFalse:[
	theByteCode := #trueJump
    ].
"
    (self canOptimizeConditionFor:receiver) ifTrue:[
	theByteCode := self optimizedConditionFor:theReceiver
					     with:theByteCode.
	theReceiver := theReceiver receiver
    ].
"
    theReceiver codeOn:aStream inBlock:b.
    aStream nextPut:theByteCode.
    pos := aStream position.
    aStream nextPut:0.
    (argArray at: 1) codeInlineOn:aStream inBlock:b.
    (aStream contents) at:pos put:(aStream position).
    valueNeeded ifFalse:[aStream nextPut:#drop]
!

codeOn:aStream inBlock:b valueNeeded:valueNeeded
    |nargs isBuiltIn|

    argArray isNil ifTrue:[
	nargs := 0
    ] ifFalse:[
	nargs := argArray size
    ].

    isBuiltIn := false.

    (nargs == 0) ifTrue:[
	(receiver type == #ThisContext) ifTrue:[
	    valueNeeded ifFalse:[
		"for now, only do it in methods"
		b isNil ifTrue:[
		    (selector == #restart) ifTrue:[
			aStream nextPut:#jump.      "jump to start"
			aStream nextPut:1.
			^ self
		    ].
		].
		(selector == #return) ifTrue:[  "^ nil"
		    aStream nextPut:#retNil.
		    ^ self
		].
	    ]
	].
	isBuiltIn := self class isBuiltInUnarySelector:selector
    ].

    (nargs == 1) ifTrue:[
	(receiver type == #ThisContext) ifTrue:[
	    valueNeeded ifFalse:[
		(selector == #return:) ifTrue:[
		    (argArray at:1) codeOn:aStream inBlock:b.  "^ value"
		    aStream nextPut:#retTop.
		    ^ self
		].
	     ].
	].

	((argArray at:1) isBlock) ifTrue:[
	    ((selector == #ifTrue:) or:[selector == #ifFalse:]) ifTrue:[
		self codeIfOn:aStream inBlock:b valueNeeded:valueNeeded.
		^ self
	    ].
"
	    ((selector == #and:) or:[selector == #or:]) ifTrue:[
		self codeAndOrOn:aStream inBlock:b valueNeeded:valueNeeded.
		^ self
	    ].
"
	    (selector == #timesRepeat:) ifTrue:[
		(receiver isConstant and:[receiver evaluate isNumber]) ifTrue:[
		    self codeTimesRepeatOn:aStream inBlock:b valueNeeded:valueNeeded.
		    ^ self
		]
	    ].

	    ((selector == #whileTrue:) or:[selector == #whileFalse:]) ifTrue:[
		(receiver isBlock) ifTrue:[
		    self codeWhileOn:aStream inBlock:b valueNeeded:valueNeeded.
		    ^ self
		]
	    ]
	].
	isBuiltIn := self class isBuiltIn1ArgSelector:selector
    ].

    (nargs == 2) ifTrue:[
	((selector == #ifTrue:ifFalse:) or:[selector == #ifFalse:ifTrue:]) ifTrue:[
	    (argArray at:1) isBlock ifTrue:[
		(argArray at:2) isBlock ifTrue:[
		    self codeIfElseOn:aStream inBlock:b valueNeeded:valueNeeded.
		    ^ self
		]
	    ]
	].
	isBuiltIn := self class isBuiltIn2ArgSelector:selector
    ].

    "can we use a send-bytecode ?"
    isBuiltIn ifTrue:[
	receiver isSuper ifFalse:[
	    receiver codeOn:aStream inBlock:b.
	    (nargs > 0) ifTrue:[
		(argArray at:1) codeOn:aStream inBlock:b.
		(nargs > 1) ifTrue:[
		    (argArray at:2) codeOn:aStream inBlock:b
		]
	    ].
	    aStream nextPut:selector.
	    (self class hasLineNumber:selector) ifTrue:[
		aStream nextPut:lineNr.
	    ].
	    valueNeeded ifFalse:[
		aStream nextPut:#drop
	    ].
	    ^ self
	]
    ].

    ((nargs == 0) and:[selector == #yourself]) ifTrue:[
	"yourself is often added to get the receiver -
	 we get it without the yourself-message"

	valueNeeded ifTrue:[
	    receiver codeOn:aStream inBlock:b
	].
	^ self
    ].

    "no - generate a send"
    ((receiver type ~~ #Self)
    or:[nargs > 3]) ifTrue:[
	receiver codeOn:aStream inBlock:b
    ].
    argArray notNil ifTrue:[
	argArray do:[:arg |
	    arg codeOn:aStream inBlock:b
	]
    ].
    receiver isSuper ifTrue:[
	receiver isHere ifTrue:[
	    aStream nextPut:#hereSend
	] ifFalse:[
	    aStream nextPut:#superSend.
	].
	aStream nextPut:lineNr.
	aStream nextPut:selector.
	aStream nextPut:nargs.
	aStream nextPut:nil.
	valueNeeded ifFalse:[
	    aStream nextPut:#drop
	].
	^ self
    ].

    (nargs <= 3) ifTrue:[
	|codes|

	valueNeeded ifTrue:[
	    (receiver type == #Self) ifTrue:[
		codes := #(sendSelf0 sendSelf1 sendSelf2 sendSelf3)
	    ] ifFalse:[
		codes := #(send0 send1 send2 send3)
	    ]
	] ifFalse:[
	    (receiver type == #Self) ifTrue:[
		codes := #(sendSelfDrop0 sendSelfDrop1 sendSelfDrop2 sendSelfDrop3)
	    ] ifFalse:[
		codes := #(sendDrop0 sendDrop1 sendDrop2 sendDrop3)
	    ]
	].
	aStream nextPut:(codes at:nargs + 1).
	aStream nextPut:lineNr.
	aStream nextPut:selector.
	^ self
    ].

    valueNeeded ifTrue:[
	aStream nextPut:#send
    ] ifFalse:[
	aStream nextPut:#sendDrop
    ].
    aStream nextPut:lineNr.
    aStream nextPut:selector.
    aStream nextPut:nargs
!

codeSendOn:aStream inBlock:b valueNeeded:valueNeeded
    "like code on, but assumes that receiver has already been
     coded onto stack - needed for cascade"

    |nargs isBuiltIn|

    argArray isNil ifTrue:[
	nargs := 0
    ] ifFalse:[
	nargs := argArray size
    ].

    isBuiltIn := false.

    (nargs == 0) ifTrue:[
	isBuiltIn := self class isBuiltInUnarySelector:selector
    ].
    (nargs == 1) ifTrue:[
	isBuiltIn := self class isBuiltIn1ArgSelector:selector
    ].
    (nargs == 2) ifTrue:[
	isBuiltIn := self class isBuiltIn2ArgSelector:selector
    ].

    "can we use a send-bytecode ?"
    isBuiltIn ifTrue:[
	receiver isSuper ifFalse:[
	    (nargs > 0) ifTrue:[
		(argArray at:1) codeOn:aStream inBlock:b.
		(nargs > 1) ifTrue:[
		    (argArray at:2) codeOn:aStream inBlock:b
		]
	    ].
	    aStream nextPut:selector.
	    (self class hasLineNumber:selector) ifTrue:[
		aStream nextPut:lineNr.
	    ].
	    valueNeeded ifFalse:[
		aStream nextPut:#drop
	    ].
	    ^ self
	]
    ].

    argArray notNil ifTrue:[
	argArray do:[:arg |
	    arg codeOn:aStream inBlock:b
	]
    ].

    receiver isSuper ifTrue:[
	receiver isHere ifTrue:[
	    aStream nextPut:#hereSend
	] ifFalse:[
	    aStream nextPut:#superSend.
	].
	aStream nextPut:lineNr.
	aStream nextPut:selector.
	aStream nextPut:nargs.
	aStream nextPut:nil.
	valueNeeded ifFalse:[
	    aStream nextPut:#drop
	].
	^ self
    ].
    (nargs == 0) ifTrue:[
	(selector == #yourself) ifTrue:[
	    "yourself is often added to get the receiver -
	     we get it without the yourself-message"

	    valueNeeded ifFalse:[
		aStream nextPut:#drop
	    ].
	    ^ self
	].
    ].

    (nargs <= 3) ifTrue:[
	valueNeeded ifTrue:[
	    aStream nextPut:( #( send0 send1 send2 send3) at:(nargs+1) ).
	] ifFalse:[
	    aStream nextPut:( #( sendDrop0 sendDrop1 sendDrop2 sendDrop3) at:(nargs+1) ).
	].
	aStream nextPut:lineNr.
	aStream nextPut:selector.
	^ self
    ].

    valueNeeded ifTrue:[
	aStream nextPut:#send
    ] ifFalse:[
	aStream nextPut:#sendDrop
    ].
    aStream nextPut:lineNr.
    aStream nextPut:selector.
    aStream nextPut:nargs
!

codeForCascadeOn:aStream inBlock:b
    "like codeOn, but always leave the receiver instead of the result"
    |nargs isBuiltIn|

    argArray isNil ifTrue:[
	nargs := 0
    ] ifFalse:[
	nargs := argArray size
    ].

    isBuiltIn := false.

    (nargs == 0) ifTrue:[
	isBuiltIn := self class isBuiltInUnarySelector:selector
    ].
    (nargs == 1) ifTrue:[
	isBuiltIn := self class isBuiltIn1ArgSelector:selector
    ].
    (nargs == 2) ifTrue:[
	isBuiltIn := self class isBuiltIn2ArgSelector:selector
    ].

    receiver codeOn:aStream inBlock:b.
    aStream nextPut:#dup.

    "can we use a send-bytecode ?"
    isBuiltIn ifTrue:[
	receiver isSuper ifFalse:[
	    (nargs > 0) ifTrue:[
		(argArray at:1) codeOn:aStream inBlock:b.
		(nargs > 1) ifTrue:[
		    (argArray at:2) codeOn:aStream inBlock:b
		]
	    ].
	    aStream nextPut:selector.
	    aStream nextPut:lineNr.
	    aStream nextPut:#drop.
	    ^ self
	]
    ].

    "no - generate a send"
    argArray notNil ifTrue:[
	argArray do:[:arg |
	    arg codeOn:aStream inBlock:b
	]
    ].
    receiver isSuper ifTrue:[
	receiver isHere ifTrue:[
	    aStream nextPut:#hereSend
	] ifFalse:[
	    aStream nextPut:#superSend.
	].
	aStream nextPut:lineNr.
	aStream nextPut:selector.
	aStream nextPut:nargs.
	aStream nextPut:nil.
	aStream nextPut:#drop.
	^ self
    ].
    (nargs <= 3) ifTrue:[
	aStream nextPut:( #( sendDrop0 sendDrop1 sendDrop2 sendDrop3) at:(nargs+1) ).
	aStream nextPut:lineNr.
	aStream nextPut:selector.
	^ self
    ].

    aStream nextPut:#sendDrop.
    aStream nextPut:lineNr.
    aStream nextPut:selector.
    aStream nextPut:nargs
! !