"
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.
"
"{ Package: 'stx:libcomp' }"
PrimaryNode subclass:#ConstantNode
instanceVariableNames:''
classVariableNames:'TrueNode FalseNode NilNode Const0Node Const1Node Float0Node'
poolDictionaries:''
category:'System-Compiler-Support'
!
!ConstantNode 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.
"
!
documentation
"
node for parse-trees, representing literal constants
This is a helper class for the compiler.
[author:]
Claus Gittinger
"
! !
!ConstantNode class methodsFor:'instance creation'!
type:t value:val
"some constant nodes are used so often, its worth caching them"
(t == #True) ifTrue:[
TrueNode isNil ifTrue:[
TrueNode := (self basicNew) type:t value:val
].
^ TrueNode
].
(t == #False) ifTrue:[
FalseNode isNil ifTrue:[
FalseNode := (self basicNew) type:t value:val
].
^ FalseNode
].
(t == #Nil) ifTrue:[
NilNode isNil ifTrue:[
NilNode := (self basicNew) type:t value:val
].
^ NilNode
].
(t == #Integer) ifTrue:[
(val == 0) ifTrue:[
Const0Node isNil ifTrue:[
Const0Node := (self basicNew) type:t value:val
].
^ Const0Node
].
(val == 1) ifTrue:[
Const1Node isNil ifTrue:[
Const1Node := (self basicNew) type:t value:val
].
^ Const1Node
]
].
(t == #Float) ifTrue:[
(val = 0.0) ifTrue:[
"/ care for negative0 (which compares = to 0.0)
val isNegativeZero ifFalse:[
Float0Node isNil ifTrue:[
Float0Node := (self basicNew) type:t value:val
].
^ Float0Node
]
]
].
^ (self basicNew) type:t value:val
!
value:val
^ self type:(self typeOfConstant:val) value:val
! !
!ConstantNode class methodsFor:'queries'!
typeOfConstant:anObject
"return the constantNode type for an object"
"the most common case first ..."
(anObject isMemberOf:SmallInteger) ifTrue:[
^ #Integer
].
anObject isNil ifTrue:[
^ #Nil
].
anObject isNumber ifTrue:[
"the most common case first ..."
(anObject isMemberOf:Float) ifTrue:[
^ #Float
].
anObject isInteger ifTrue:[
^ #Integer
].
].
(anObject == true) ifTrue:[
^ #True
].
(anObject == false) ifTrue:[
^ #False
].
^ #Literal
! !
!ConstantNode methodsFor:'accessing'!
lineNumber:ignoredLineNumber
"Created: / 14.5.1998 / 19:31:48 / cg"
!
type:t value:val
type := t.
value := val
! !
!ConstantNode methodsFor:'code generation'!
codeForSimpleReturnOn:aStream inBlock:b lineNumber:lineNrOrNil for:aCompiler
lineNrOrNil notNil ifTrue:[
self codeLineNumber:lineNrOrNil on:aStream for:aCompiler
].
(type == #Nil) ifTrue:[
aStream nextPut:#retNil.
^self
].
(type == #True) ifTrue:[
aStream nextPut:#retTrue.
^self
].
(type == #False) ifTrue:[
aStream nextPut:#retFalse.
^self
].
(type == #Integer) ifTrue:[
(value between: -128 and:127) ifTrue:[
(value == 0) ifTrue:[
aStream nextPut:#ret0.
^ self.
].
aStream nextPut:#retNum; nextPut:value.
^ self
].
].
"/ anything else must be pushed, then top returned
self codeOn:aStream inBlock:b for:aCompiler.
aStream nextPut:#retTop
!
codeOn:aStream inBlock:b for:aCompiler
"generate code for the constant"
|code index|
(type == #Integer) ifTrue:[
(value between: -128 and:127) ifTrue:[
(value == 0) ifTrue:[
code := #push0
].
(value == 1) ifTrue:[
code := #push1.
].
(value == 2) ifTrue:[
code := #push2.
].
(value == -1) ifTrue:[
code := #pushMinus1.
].
code notNil ifTrue:[
aStream nextPut:code. ^ self
].
aStream nextPut:#pushNum; nextPut:value.
^ self
].
(value between:16r-8000 and:16r7FFF) ifTrue:[
aStream nextPut:#pushNum16; nextPut:value; nextPut:0.
^ self
]
].
(type == #Nil) ifTrue:[
code := #pushNil.
].
(type == #True) ifTrue:[
code := #pushTrue.
].
(type == #False) ifTrue:[
code := #pushFalse.
].
code notNil ifTrue:[
aStream nextPut:code. ^ self
].
"/ kludge for backward compatibility
aCompiler isNil ifTrue:[
aStream nextPut:#pushLit; nextPut:value.
^ self.
].
index := aCompiler addLiteral:value.
index <= 8 ifTrue:[
aStream nextPut:(#(pushLit1 pushLit2 pushLit3 pushLit4
pushLit5 pushLit6 pushLit7 pushLit8) at:index).
] ifFalse:[
index < 256 ifTrue:[
aStream nextPut:#pushLitS; nextPut:index
] ifFalse:[
aStream nextPut:#pushLitL; nextPut:index; nextPut:0
].
].
!
codeStoreOn:aStream inBlock:codeBlock valueNeeded:valueNeeded for:aCompiler
"not sent - parser checks for this"
^ self error:'assignment to literals not allowed'
! !
!ConstantNode methodsFor:'enumerating'!
nodeDo:anEnumerator
"helper for parse tree walking"
^ anEnumerator doLiteral:self value:value
"Modified: 19.6.1997 / 16:40:59 / cg"
! !
!ConstantNode methodsFor:'evaluation'!
evaluate
"exists for performance only"
^ value
!
evaluateIn:anEnvironment
^ value
!
store:aValue
"not reached - parser checks for this"
self error:'store not allowed'.
^ aValue
! !
!ConstantNode methodsFor:'printing & storing'!
displayString
"return a printed representation of the receiver for displaying"
^ value displayString
"Modified: 20.9.1997 / 11:39:07 / cg"
!
printOn:aStream indent:i
value storeOn:aStream
! !
!ConstantNode methodsFor:'queries'!
canReuseAsArg:anotherNode
|otherValue|
anotherNode isConstant ifTrue:[
anotherNode type ~~ type ifTrue:[^ false].
otherValue := anotherNode evaluate.
(value isMemberOf:SmallInteger) ifTrue:[
(value == 0) ifTrue:[^ false].
(value == 1) ifTrue:[^ false].
(value == 2) ifTrue:[^ false].
(value == -1) ifTrue:[^ false].
^ otherValue == value
].
(value isMemberOf:Float) ifTrue:[
^ (otherValue isMemberOf:Float)
and:[otherValue = value]
].
(value isMemberOf:Symbol) ifTrue:[
^ otherValue == value
].
(value isMemberOf:String) ifTrue:[
^ (otherValue isMemberOf:String)
and:[otherValue = value]
].
].
^ false
"Created: 14.4.1996 / 00:43:14 / cg"
"Modified: 14.4.1996 / 01:00:29 / cg"
!
isConstant
^ true
! !
!ConstantNode class methodsFor:'documentation'!
version
^ '$Header: /cvs/stx/stx/libcomp/ConstantNode.st,v 1.33 2003-04-25 16:40:04 stefan Exp $'
! !