IRPrinter.st
author Jan Vrany <jan.vrany@fit.cvut.cz>
Wed, 11 Jun 2008 14:54:42 +0000
changeset 1 0dd36941955f
child 3 c9845c180bd4
permissions -rw-r--r--
Initial revision. All tests pass.

"{ Package: 'stx:goodies/newcompiler' }"

IRInterpreter subclass:#IRPrinter
	instanceVariableNames:'stream indent'
	classVariableNames:''
	poolDictionaries:''
	category:'NewCompiler-IR'
!

IRPrinter comment:'I interpret IRMethod instructions and write them out to a print stream.'
!


!IRPrinter methodsFor:'initialize'!

indent: tabs

	indent _ tabs
!

stream: stringWriteStream

	stream _ stringWriteStream
! !

!IRPrinter methodsFor:'instructions'!

blockReturnTop

	stream nextPutAll: 'blockReturnTop'.
!

goto: seqNum

	stream nextPutAll: 'goto: '.
	seqNum printOn: stream.
!

if: bool goto: seqNum1 otherwise: seqNum2

	stream nextPutAll: 'if: '.
	bool printOn: stream.
	stream nextPutAll: ' goto: '.
	seqNum1 printOn: stream.
	stream nextPutAll: ' else: '.
	seqNum2 printOn: stream.
!

jumpOverBlock: blockSeq to: dest

	stream nextPutAll: 'jumpOverBlock: '.
	stream nextPutAll: ' block '.
	blockSeq  printOn: stream.
	stream nextPutAll: ' cont: '.
	dest  printOn: stream.
!

label: seqNum

	"add tab and cr since this does not get called within interpretInstruction:"
	stream cr.  "extra cr just to space out sequences"
	indent timesRepeat: [stream tab].
	stream nextPutAll: 'label: '.
	seqNum printOn: stream.
	stream cr.
!

popTop

	stream nextPutAll: 'popTop'
!

pushBlock: irMethod

	stream nextPutAll: 'pushBlock:'.
	IRPrinter new
		indent: indent + 1;
		stream: stream;
		interpret: irMethod removeEmptyStart.
!

pushBlockMethod: irMethod

	stream nextPutAll: 'pushBlockMethod:'.
	IRPrinter new
		indent: indent + 1;
		stream: stream;
		interpret: irMethod removeEmptyStart.
!

pushDup

	stream nextPutAll: 'pushDup'
!

pushLiteral: object

	stream nextPutAll: 'pushLiteral: '.
	object isVariableBinding ifTrue: [^ stream nextPutAll: object key].
	object printOn: stream.

	((object isKindOf: BlockClosure) or: [object isKindOf: CompiledMethod])
		ifTrue: [
			IRPrinter new
				indent: indent + 1;
				stream: stream;
				interpret: object method ir removeEmptyStart].
!

pushLiteralVariable: object

	stream nextPutAll: 'pushLiteralVariable: '.
	object isVariableBinding ifTrue: [^ stream nextPutAll: object key].
	object printOn: stream.
!

pushTemp: index

	stream nextPutAll: 'pushTemp: '.
	index printOn: stream.
	index = 0 ifTrue: [stream nextPutAll: ' "receiver"'].
	index = -1 ifTrue: [stream nextPutAll: ' "thisEnv"'].
	index = -2 ifTrue: [stream nextPutAll: ' "thisContext"'].
!

remoteReturn

	stream nextPutAll: 'remoteReturn'.
!

returnTop

	stream nextPutAll: 'returnTop'.
!

send: selector

	stream nextPutAll: 'send: '.
	selector printOn: stream.
!

send: selector toSuperOf: behavior

	stream nextPutAll: 'send: '.
	selector printOn: stream.
	stream nextPutAll: ' toSuperOf: '.
	behavior printOn: stream.
!

storeTemp: index

	stream nextPutAll: 'storeTemp: '.
	index printOn: stream.
	index = -1 ifTrue: [stream nextPutAll: ' "thisEnv"'].
! !

!IRPrinter methodsFor:'interpret'!

interpretInstruction: irInstruction

	indent timesRepeat: [stream tab].
	super interpretInstruction: irInstruction.
	stream cr.
! !

!IRPrinter class methodsFor:'documentation'!

version
    ^'$Id$'
! !