Fixes IRBytecodeGenerator >> #pushLiteral and IRBuilder >> #jumpAheadTo:
authorJan Vrany <jan.vrany@fit.cvut.cz>
Mon, 03 Nov 2008 17:02:40 +0000
changeset 3 c9845c180bd4
parent 2 6e1de7f85d59
child 4 7195866510e1
Fixes IRBytecodeGenerator >> #pushLiteral and IRBuilder >> #jumpAheadTo:
IRBuilder.st
IRBuilderTest.st
IRBytecodeGenerator.st
IRMethod.st
IRPrinter.st
IRTranslator.st
extensions.st
stx_goodies_newcompiler.st
--- a/IRBuilder.st	Wed Sep 17 10:30:02 2008 +0000
+++ b/IRBuilder.st	Mon Nov 03 17:02:40 2008 +0000
@@ -67,16 +67,18 @@
 !
 
 initialize
-	ir := IRMethod new.
-	tempMap := Dictionary new.
-	jumpAheadStacks := IdentityDictionary new.
-	jumpBackTargetStacks := IdentityDictionary new.
-	sourceMapNodes := OrderedCollection new.	"stack"
+        ir := IRMethod new.
+        tempMap := Dictionary new.
+        jumpAheadStacks := IdentityDictionary new.
+        jumpBackTargetStacks := IdentityDictionary new.
+        sourceMapNodes := OrderedCollection new.        "stack"
 
-	"Leave an empty sequence up front (guaranteed not to be in loop)"
-	ir  startSequence:((IRSequence new  orderNumber:0)  method:ir).
-	currentSequence := (IRSequence new  orderNumber:1)  method:ir.
-	ir startSequence  add:(IRJump new  destination: currentSequence)
+        "Leave an empty sequence up front (guaranteed not to be in loop)"
+        ir  startSequence:((IRSequence new  orderNumber:0)  method:ir).
+        currentSequence := (IRSequence new  orderNumber:1)  method:ir.
+        ir startSequence  add:(IRJump new  destination: currentSequence)
+
+    "Modified: / 03-11-2008 / 11:52:48 / Jan Vrany <vranyj1@fel.cvut.cz>"
 !
 
 numRargs: n
@@ -117,13 +119,16 @@
 !IRBuilder methodsFor:'instructions'!
 
 jumpAheadTarget: labelSymbol
-	"Pop latest jumpAheadTo: with this labelSymbol and have it point to this new instruction sequence"
+        "Pop latest jumpAheadTo: with this labelSymbol and have it point to this new instruction sequence"
 
-	| jumpInstr |
-	self startNewSequence.
-	jumpInstr := (jumpAheadStacks at: labelSymbol ifAbsent: [
-			self error: 'Missing jumpAheadTo: ', labelSymbol printString]) removeLast.
-	jumpInstr destination: currentSequence.
+        | jumpInstrs |
+        self startNewSequence.
+        jumpInstrs := (jumpAheadStacks at: labelSymbol ifAbsent: [
+                        self error: 'Missing jumpAheadTo: ', labelSymbol printString]).
+        jumpInstrs do:[:jumpInstr | jumpInstr destination: currentSequence].
+        jumpInstrs removeLast.
+
+    "Modified: / 03-11-2008 / 12:00:44 / Jan Vrany <vranyj1@fel.cvut.cz>"
 !
 
 jumpAheadTo: labelSymbol
--- a/IRBuilderTest.st	Wed Sep 17 10:30:02 2008 +0000
+++ b/IRBuilderTest.st	Mon Nov 03 17:02:40 2008 +0000
@@ -206,21 +206,21 @@
 
 testLiteralFloat
 
-	| iRMethod aCompiledMethod |
-	iRMethod := IRBuilder new
-		numRargs: 1;
-		addTemps: #(self);		"receiver and args declarations"
-			
-		pushLiteral: 2.0; 	
-		returnTop;
-		ir.
+        | iRMethod aCompiledMethod |
+        iRMethod := IRBuilder new
+                numRargs: 1;
+                addTemps: #(self);              "receiver and args declarations"
+                        
+                pushLiteral: 2.0;       
+                returnTop;
+                ir.
 
-	aCompiledMethod := iRMethod compiledMethod.
+        aCompiledMethod := iRMethod compiledMethod.
 
-	self assert: (aCompiledMethod isKindOf: CompiledMethod).
+        self assert: (aCompiledMethod isKindOf: CompiledMethod).
      self assert: ((aCompiledMethod valueWithReceiver: nil arguments: #() ) = 2.0).
 
-	
+    "Modified: / 03-11-2008 / 08:39:52 / Jan Vrany <vranyj1@fel.cvut.cz>"
 !
 
 testLiteralInteger
@@ -599,6 +599,31 @@
      self assert: ((aCompiledMethod valueWithReceiver: nil arguments: #() ) isKindOf: ClosureEnvironment).
 
     "Modified: / 11-06-2008 / 14:47:51 / Jan Vrany <vranyj1@fel.cvut.cz>"
+!
+
+testTwoJumpAheadToIfsToSameTarget
+
+        | iRMethod aCompiledMethod |
+        iRMethod := IRBuilder new
+                numRargs: 1;
+                addTemps: #(self);              "receiver and args declarations"
+                pushTemp: #self         ;
+                pushLiteral: false;
+                jumpAheadTo: #end if: true;
+                pushLiteral: true;
+                jumpAheadTo: #end if: true;
+                pushLiteral: 3;
+                jumpAheadTarget: #end;  
+                
+                returnTop;
+                ir.
+
+        aCompiledMethod := iRMethod compiledMethod.
+
+        self assert: (aCompiledMethod isKindOf: CompiledMethod).
+     self assert: ((aCompiledMethod valueWithReceiver: nil arguments: #() ) = nil).
+
+    "Created: / 03-11-2008 / 13:34:50 / Jan Vrany <vranyj1@fel.cvut.cz>"
 ! !
 
 !IRBuilderTest class methodsFor:'documentation'!
--- a/IRBytecodeGenerator.st	Wed Sep 17 10:30:02 2008 +0000
+++ b/IRBytecodeGenerator.st	Mon Nov 03 17:02:40 2008 +0000
@@ -170,10 +170,10 @@
         stack push.
 
         self 
-            nextPut: #pushLit;
+            nextPut: #pushLitS;
             nextPut: (self addLiteral: object).
 
-    "Modified: / 11-06-2008 / 14:04:41 / Jan Vrany <vranyj1@fel.cvut.cz>"
+    "Modified: / 03-11-2008 / 14:32:04 / Jan Vrany <vranyj1@fel.cvut.cz>"
 !
 
 pushLiteralVariable: object
@@ -403,11 +403,11 @@
 
 addLiteral: object
 
-    (literals includes: object)
+    (literals includesIdentical: object)
         ifFalse:[literals add: object].
     ^ literals identityIndexOf: object
 
-    "Modified: / 11-06-2008 / 13:49:00 / Jan Vrany <vranyj1@fel.cvut.cz>"
+    "Modified: / 03-11-2008 / 13:49:25 / Jan Vrany <vranyj1@fel.cvut.cz>"
 !
 
 from: fromSeqId goto: toSeqId
@@ -528,13 +528,19 @@
 
 !IRBytecodeGenerator methodsFor:'results'!
 
-bytecodes
+bytecodesAndLiteralArray
+
+    | byteCodeCompiler |
+    byteCodeCompiler := ByteCodeCompiler new.
 
-    ^ByteCodeCompiler new
-        genByteCodeFrom: self symboliccodes;
-        code.
+    byteCodeCompiler literalArray: literals.
+    byteCodeCompiler genByteCodeFrom: self symboliccodes.
 
-    "Modified: / 11-06-2008 / 14:01:32 / Jan Vrany <vranyj1@fel.cvut.cz>"
+    ^Array 
+        with: byteCodeCompiler code
+        with: byteCodeCompiler literalArray.
+
+    "Created: / 03-11-2008 / 14:20:32 / Jan Vrany <vranyj1@fel.cvut.cz>"
 !
 
 compiledMethod
@@ -546,23 +552,37 @@
 
 compiledMethodUsing: aCompiledMethodClass
 
+    "
+        self symboliccodes
+    " 
+
+    | bytecodesAndLiteralArray |
+    bytecodesAndLiteralArray := self bytecodesAndLiteralArray.
+
     ^(aCompiledMethodClass new: literals size)
         numberOfArgs: numArgs;
         numberOfVars: maxTemp - numArgs ;
-        byteCode: self bytecodes;
-        literals: literals asArray;
+        byteCode: bytecodesAndLiteralArray first;
+        literals: bytecodesAndLiteralArray second asArray;
         yourself
 
     "Created: / 11-06-2008 / 14:02:03 / Jan Vrany <vranyj1@fel.cvut.cz>"
+    "Modified: / 03-11-2008 / 14:22:52 / Jan Vrany <vranyj1@fel.cvut.cz>"
 !
 
 literals
 
-	literals := literals asArray copyWith: MethodProperties new.
+    ^literals asArray
+
+
+    " old "
+    "    literals := literals asArray copyWith: MethodProperties new.
 
-	^ lastLiteral 
-		ifNil: [literals copyWith: nil ] 
-		ifNotNil: [literals copyWith: lastLiteral]
+        ^ lastLiteral 
+                ifNil: [literals copyWith: nil ] 
+                ifNotNil: [literals copyWith: lastLiteral]"
+
+    "Modified: / 03-11-2008 / 13:41:33 / Jan Vrany <vranyj1@fel.cvut.cz>"
 !
 
 numArgs
--- a/IRMethod.st	Wed Sep 17 10:30:02 2008 +0000
+++ b/IRMethod.st	Mon Nov 03 17:02:40 2008 +0000
@@ -315,6 +315,19 @@
 
 !IRMethod methodsFor:'translating'!
 
+bytecodes
+
+        ^ compiledMethod 
+            ifNotNil: 
+                [compiledMethod byteCode]                
+            ifNil:
+                [IRTranslator new
+                    interpret: self;
+                    bytecodes]
+
+    "Created: / 03-11-2008 / 08:38:02 / Jan Vrany <vranyj1@fel.cvut.cz>"
+!
+
 compiledMethod
 
         ^ compiledMethod ifNil: [self compiledMethodUsing: Method]
@@ -331,6 +344,15 @@
     "Created: / 11-06-2008 / 11:06:28 / Jan Vrany <vranyj1@fel.cvut.cz>"
 !
 
+literals
+
+        ^(IRTranslator new
+            interpret: self;
+            literals)
+
+    "Created: / 03-11-2008 / 09:08:23 / Jan Vrany <vranyj1@fel.cvut.cz>"
+!
+
 setCompiledMethod:aCompiledMethod 
     compiledMethod := aCompiledMethod
 
--- a/IRPrinter.st	Wed Sep 17 10:30:02 2008 +0000
+++ b/IRPrinter.st	Mon Nov 03 17:02:40 2008 +0000
@@ -95,16 +95,19 @@
 
 pushLiteral: object
 
-	stream nextPutAll: 'pushLiteral: '.
-	object isVariableBinding ifTrue: [^ stream nextPutAll: object key].
-	object printOn: stream.
+        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].
+        ((object isKindOf: BlockClosure) or: [object isKindOf: CompiledMethod])
+                ifTrue: [
+                        IRPrinter new
+                                indent: indent + 1;
+                                stream: stream;
+                                interpret: object method ir removeEmptyStart].
+
+    "Modified: / 03-11-2008 / 17:59:13 / Jan Vrany <vranyj1@fel.cvut.cz>"
 !
 
 pushLiteralVariable: object
--- a/IRTranslator.st	Wed Sep 17 10:30:02 2008 +0000
+++ b/IRTranslator.st	Mon Nov 03 17:02:40 2008 +0000
@@ -309,9 +309,18 @@
 
 !IRTranslator methodsFor:'results'!
 
+bytecodes
+
+    ^gen bytecodes
+
+    "Created: / 03-11-2008 / 09:23:01 / Jan Vrany <vranyj1@fel.cvut.cz>"
+!
+
 compiledMethod
 
-	^ gen compiledMethodWith: trailerBytes
+        ^ gen compiledMethod
+
+    "Modified: / 03-11-2008 / 09:22:36 / Jan Vrany <vranyj1@fel.cvut.cz>"
 !
 
 compiledMethodUsing: aCompiledMethodClass
@@ -321,14 +330,11 @@
     "Modified: / 17-09-2008 / 12:18:43 / Jan Vrany <vranyj1@fel.cvut.cz>"
 !
 
-compiledMethodWith: trailer
+literals
 
-	^ gen compiledMethodWith: trailer
-!
+    ^gen literals
 
-compiledMethodWith: trailer using: aCompiledMethodClass
-
-	^ gen compiledMethodWith: trailer using: aCompiledMethodClass
+    "Created: / 03-11-2008 / 09:23:08 / Jan Vrany <vranyj1@fel.cvut.cz>"
 ! !
 
 !IRTranslator class methodsFor:'documentation'!
--- a/extensions.st	Wed Sep 17 10:30:02 2008 +0000
+++ b/extensions.st	Mon Nov 03 17:02:40 2008 +0000
@@ -2,6 +2,15 @@
 
 !
 
+!ByteCodeCompiler methodsFor:'accessing'!
+
+literalArray: anArray
+    "return the literal array - only valid after parsing"
+
+    litArray := anArray
+
+    "Created: / 03-11-2008 / 14:09:33 / Jan Vrany <vranyj1@fel.cvut.cz>"
+! !
 !Class methodsFor:'accessing'!
 
 binding
--- a/stx_goodies_newcompiler.st	Wed Sep 17 10:30:02 2008 +0000
+++ b/stx_goodies_newcompiler.st	Mon Nov 03 17:02:40 2008 +0000
@@ -21,7 +21,7 @@
         #'stx:libwidg'    "ScrollableView - referenced by IRMethod>>inspector2TabIRCode "
     )
 
-    "Modified: / 17-09-2008 / 12:22:36 / Jan Vrany <vranyj1@fel.cvut.cz>"
+    "Modified: / 03-11-2008 / 18:00:51 / Jan Vrany <vranyj1@fel.cvut.cz>"
 ! !
 
 !stx_goodies_newcompiler class methodsFor:'description - contents'!
@@ -63,16 +63,17 @@
         IRTempStore
     )
 
-    "Modified: / 17-09-2008 / 12:22:35 / Jan Vrany <vranyj1@fel.cvut.cz>"
+    "Modified: / 03-11-2008 / 18:00:51 / Jan Vrany <vranyj1@fel.cvut.cz>"
 !
 
 extensionMethodNames
     ^ #(
+        ByteCodeCompiler literalArray:
         Class binding
         Class bindingOf:
     )
 
-    "Modified: / 17-09-2008 / 12:22:35 / Jan Vrany <vranyj1@fel.cvut.cz>"
+    "Modified: / 03-11-2008 / 18:00:51 / Jan Vrany <vranyj1@fel.cvut.cz>"
 ! !
 
 !stx_goodies_newcompiler class methodsFor:'description - project information'!