ConstantNode.st
author Claus Gittinger <cg@exept.de>
Thu, 14 May 1998 19:24:30 +0200
changeset 710 fc74f135494d
parent 612 2748896a66c8
child 711 25b9a501b97d
permissions -rw-r--r--
ignore lineNumber assignment

"
 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.
"

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:[
	    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'!

lineNr:ignoredLineNumber

    "Created: / 14.5.1998 / 19:24:17 / cg"
!

type:t value:val
    type := t.
    value := val
! !

!ConstantNode methodsFor:'code generation'!

codeOn:aStream inBlock:b for:aCompiler
    "generated 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:'enumeration'!

nodeDo:anEnumerator
    "helper for parse tree walking"

    ^ anEnumerator doLiteral:self value:value

    "Modified: 19.6.1997 / 16:40:59 / cg"
! !

!ConstantNode methodsFor:'evaluating'!

evaluate
    ^ value
!

store:aValue
    "not reached - parser checks for this"

    self error:'store not allowed'.
    ^ aValue
! !

!ConstantNode methodsFor:'printing'!

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.25 1998-05-14 17:24:30 cg Exp $'
! !