Nested blocks works
authorJan Vrany <jan.vrany@fit.cvut.cz>
Tue, 12 May 2009 11:10:22 +0000
changeset 17 df6f26442169
parent 16 f325defc9c7e
child 18 e6921b3a5d5b
Nested blocks works
IRBuilderTest.st
IRBytecodeGenerator.st
IRInterpreter.st
IRPrinter.st
--- a/IRBuilderTest.st	Mon May 11 20:20:05 2009 +0000
+++ b/IRBuilderTest.st	Tue May 12 11:10:22 2009 +0000
@@ -156,6 +156,32 @@
     ^([:arg| arg ] value: 22)
 
     "Created: / 30-03-2009 / 19:21:48 / Jan Vrany <vranyj1@fel.cvut.cz>"
+!
+
+mock9
+
+    ^[:barg1 | [:barg2 | barg2 ] value: barg1 ]
+        value: 22
+
+    "
+    decompiling IRBuilderTest>>mock9
+    nA: 0 nV: 0 nT: 3
+    
+     1: 37 0C 00 01             makeBlock 12 (15) nv=0 na=1
+     5: 37 04 00 01             makeBlock 4 (11) nv=0 na=1
+     9: 8C                      pushBlockArg1
+    10: 00                      retTop
+    11: 8C                      pushBlockArg1
+    12: 98 03                   send1 #value:[3]
+    14: 00                      retTop
+    15: 10 16                   pushNum 22
+    17: 98 04                   send1 #value:[4]
+    19: 08 03                   LINE[3]
+    21: 00 
+    
+    "
+
+    "Created: / 11-05-2009 / 23:09:57 / Jan Vrany <vranyj1@fel.cvut.cz>"
 ! !
 
 !IRBuilderTest methodsFor:'testing'!
@@ -739,6 +765,42 @@
 
 !IRBuilderTest methodsFor:'testing - blocks'!
 
+testBlock_blockNesting_1
+    |aCompiledMethod irBuilder|
+
+    irBuilder := (IRBuilder new)
+                numRargs:1;
+                addTemps:#( #self );
+                pushBlockUsingBuilder:[:builder | 
+                        builder
+                            numRargs:1;
+                            addTemps:#( #barg1 );
+                            pushBlockUsingBuilder:[:builder|
+                                builder
+                                    numRargs: 1;
+                                    addTemps: #( #barg2 );
+                                    pushTemp: #barg2;
+                                    returnTop
+                            ];
+                            pushTemp: #barg1;
+                            send: #value:;
+                            returnTop
+                    ];
+                pushLiteral: 22;
+                send:#value;
+                returnTop;
+                ir.
+    "
+        irBuilder ir
+    "
+
+    aCompiledMethod := irBuilder compiledCode.
+    self assert:(aCompiledMethod isKindOf:CompiledMethod).
+    self assert:((aCompiledMethod valueWithReceiver:1 arguments:#()) = 22).
+
+    "Created: / 11-05-2009 / 23:04:13 / Jan Vrany <vranyj1@fel.cvut.cz>"
+!
+
 testBlock_blockTempArg
     |aCompiledMethod irBuilder|
 
--- a/IRBytecodeGenerator.st	Mon May 11 20:20:05 2009 +0000
+++ b/IRBytecodeGenerator.st	Tue May 12 11:10:22 2009 +0000
@@ -190,7 +190,7 @@
 
     self 
         nextPut: #makeBlock;
-        nextPut: nil "Number of block bytecodes. Patched later";
+        nextPut: nil "Index of block bytecode end. Patched later";
         nextPut: irClosure numVars;
         nextPut: irClosure numArgs.
 
@@ -199,13 +199,23 @@
     closureCode := translator
                         interpret: irClosure;
                         getCode.
+
+    "Patch makeBlock offsets"
+    closureCode withIndexDo:
+        [:instr :index|
+        instr == #makeBlock ifTrue:
+            [closureCode 
+                at: index + 1 
+                put: (closureCode at: index + 1) + pos + 4]].
+
+
     code addAll: closureCode.
 
     "Patch number of closure bytecodes"
     code at: pos + 2 put: code size + 1.
 
     "Created: / 30-03-2009 / 18:16:10 / Jan Vrany <vranyj1@fel.cvut.cz>"
-    "Modified: / 01-04-2009 / 19:43:22 / Jan Vrany <vranyj1@fel.cvut.cz>"
+    "Modified: / 12-05-2009 / 08:58:11 / Jan Vrany <vranyj1@fel.cvut.cz>"
 !
 
 popTop
--- a/IRInterpreter.st	Mon May 11 20:20:05 2009 +0000
+++ b/IRInterpreter.st	Tue May 12 11:10:22 2009 +0000
@@ -75,7 +75,9 @@
     "Modified: / 01-12-2008 / 19:45:19 / Jan Vrany <vranyj1@fel.cvut.cz>"
 !
 
-storeLiteralVariable: index
+storeIntoLiteralVariable: index
+
+    "Created: / 11-05-2009 / 22:38:37 / Jan Vrany <vranyj1@fel.cvut.cz>"
 !
 
 storeTemp: index kind: kind level: level
--- a/IRPrinter.st	Mon May 11 20:20:05 2009 +0000
+++ b/IRPrinter.st	Tue May 12 11:10:22 2009 +0000
@@ -173,6 +173,14 @@
     "Modified: / 30-03-2009 / 18:44:04 / Jan Vrany <vranyj1@fel.cvut.cz>"
 !
 
+storeIntoLiteralVariable: name
+        stream 
+            nextPutAll: 'storeLiteralVariable: ';
+            nextPutAll: name
+
+    "Created: / 11-05-2009 / 22:39:23 / Jan Vrany <vranyj1@fel.cvut.cz>"
+!
+
 storeTemp: index kind: kind level: level
 
         stream