IRDecompiler.st
changeset 45 04a50b0d540a
parent 43 c8afb8e4c3cc
--- a/IRDecompiler.st	Thu Oct 30 22:27:09 2014 +0000
+++ b/IRDecompiler.st	Thu Oct 30 22:42:40 2014 +0000
@@ -49,61 +49,65 @@
 !
 
 decompileIR: ir 
-	| sequenceNode temps args goto seq value method |
-	scope isBlockScope 
-		ifTrue:[(scope addTemp: 'parent env') markArg]
-		ifFalse:[(scope addTemp: 'self') markArg].
-	ir tempKeys do: [:temp | scope tempVarAt: temp].
-	0 to: ir numRargs - 1 do: [:i | (scope tempVarAt: i) markArg].
-	self interpret: ir.
-	
-	self addTempToScope: ir.
-	self label: #return.
-	self Label: #return.
-	(self endCase: #lastReturn) ifFalse:[self Label: #return.].
-	goto := self Goto.
-	value := self ValueOrNone.
-	seq := self Sequence.
-	self removeClosureCreation: seq.
-	sp = 1 ifFalse: [stack explore. self error: 'error'].
-	value ifNotNil: [seq addNode: value].
-	sequenceNode := (self newBlock: seq return: goto) body.
-	temps := scope compactIndexTemps asArray.
-	ir tempKeys: temps.
-	args := (temps first: ir numRargs) allButFirst.
-	args := args collect: [:var | self newVar: var].
-	temps := temps allButFirst: ir numRargs.
-	sequenceNode temporaries: (temps collect: [:var | self newVar: var]), 
-		((scope capturedVars select:[:var | var name ~= 'self' and: [var sourceTemp == nil]]) 
-			collect:[:var | self newVar: var]).
-	method := (RBMethodNode new)
-				selectorParts: (self 
-							newSelectorParts: (self class dummySelector: args size));
-				arguments: args;
-				body: sequenceNode;
-				primitiveNode: ir primitiveNode;
-				scope: scope.
-	sequenceNode parent: method.
-	Preferences compileBlocksAsClosures 
-		ifFalse: [ASTFixDecompileBlockScope new visitNode: method].
-	^ method
+"/        | sequenceNode temps args goto seq value method |
+"/        scope isBlockScope 
+"/                ifTrue:[(scope addTemp: 'parent env') markArg]
+"/                ifFalse:[(scope addTemp: 'self') markArg].
+"/        ir tempKeys do: [:temp | scope tempVarAt: temp].
+"/        0 to: ir numRargs - 1 do: [:i | (scope tempVarAt: i) markArg].
+"/        self interpret: ir.
+"/        
+"/        self addTempToScope: ir.
+"/        self label: #return.
+"/        self Label: #return.
+"/        (self endCase: #lastReturn) ifFalse:[self Label: #return.].
+"/        goto := self Goto.
+"/        value := self ValueOrNone.
+"/        seq := self Sequence.
+"/        self removeClosureCreation: seq.
+"/        sp = 1 ifFalse: [stack explore. self error: 'error'].
+"/        value ifNotNil: [seq addNode: value].
+"/        sequenceNode := (self newBlock: seq return: goto) body.
+"/        temps := scope compactIndexTemps asArray.
+"/        ir tempKeys: temps.
+"/        args := (temps first: ir numRargs) allButFirst.
+"/        args := args collect: [:var | self newVar: var].
+"/        temps := temps allButFirst: ir numRargs.
+"/        sequenceNode temporaries: (temps collect: [:var | self newVar: var]), 
+"/                ((scope capturedVars select:[:var | var name ~= 'self' and: [var sourceTemp == nil]]) 
+"/                        collect:[:var | self newVar: var]).
+"/        method := (RBMethodNode new)
+"/                                selectorParts: (self 
+"/                                                        newSelectorParts: (self class dummySelector: args size));
+"/                                arguments: args;
+"/                                body: sequenceNode;
+"/                                primitiveNode: ir primitiveNode;
+"/                                scope: scope.
+"/        sequenceNode parent: method.
+"/        Preferences compileBlocksAsClosures 
+"/                ifFalse: [ASTFixDecompileBlockScope new visitNode: method].
+"/        ^ method
+
+    "Modified: / 30-10-2014 / 22:36:32 / Jan Vrany <jan.vrany@fit.cvut.cz>"
 !
 
 removeClosureCreation: seq 
-	(Preferences compileBlocksAsClosures 
-		and: [seq statements size > 0]
-		and: [seq statements first isClosureEnvironmentCreation]) ifTrue: [
-			seq statements removeFirst.
-			(seq statements size > 0
-				and: [seq statements first isClosureEnvironmentRegistration])
-				ifTrue: [seq statements removeFirst]].
-			
-	[Preferences compileBlocksAsClosures
-		and: [seq statements size > 0]
-		and: [seq statements first isClosureRegistrationAndCreation
-			or: [seq statements first isSelfClosureRegistration]
-			or: [seq statements first isTempClosureRegistration]]]
-					whileTrue: [seq statements removeFirst]
+        ("Preferences compileBlocksAsClosures"true
+                and: [seq statements size > 0]
+                and: [seq statements first isClosureEnvironmentCreation]) ifTrue: [
+                        seq statements removeFirst.
+                        (seq statements size > 0
+                                and: [seq statements first isClosureEnvironmentRegistration])
+                                ifTrue: [seq statements removeFirst]].
+                        
+        ["Preferences compileBlocksAsClosures"true
+                and: [seq statements size > 0]
+                and: [seq statements first isClosureRegistrationAndCreation
+                        or: [seq statements first isSelfClosureRegistration]
+                        or: [seq statements first isTempClosureRegistration]]]
+                                        whileTrue: [seq statements removeFirst]
+
+    "Modified: / 30-10-2014 / 22:37:55 / Jan Vrany <jan.vrany@fit.cvut.cz>"
 !
 
 scope: aLexicalScope
@@ -235,7 +239,7 @@
                 ^self cascade] on: Abort do:[^false]
         ].
 
-        Preferences compileBlocksAsClosures 
+        "Preferences compileBlocksAsClosures"true
                         ifTrue: [ (rcvr isLiteral and: [selector = #createBlock:]) ifTrue: [
                                          ^ self block: rcvr value env: args first]]
                         ifFalse: [ (selector = #blockCopy:) ifTrue: [
@@ -247,6 +251,7 @@
                 arguments: args)).
 
     "Created: / 01-12-2008 / 19:40:52 / Jan Vrany <vranyj1@fel.cvut.cz>"
+    "Modified: / 30-10-2014 / 22:38:11 / Jan Vrany <jan.vrany@fit.cvut.cz>"
 !
 
 send: selector numArgs: numArgs toSuperOf: behavior
@@ -383,14 +388,17 @@
 !IRDecompiler methodsFor:'priv instructions'!
 
 addReturn: statements from: goto
+"/    |  |
 
-		| ret |
-		statements last isReturn ifTrue:[^self].
-		ret := RBReturnNode value: statements last.
-		Preferences compileBlocksAsClosures ifTrue:[
-			scope isHome ifFalse: [ret homeBinding: scope outerEnvScope thisEnvVar]].
-		goto mapInstr sourceNode: ret.
-		statements atLast: 1 put: ret.
+    self error: 'IRDecompiler is an unfinished code'.    
+"/    statements last isReturn ifTrue:[^self].
+"/    ret := RBReturnNode value: statements last.
+"/    Preferences compileBlocksAsClosures ifTrue:[
+"/            scope isHome ifFalse: [ret homeBinding: scope outerEnvScope thisEnvVar]].
+"/    goto mapInstr sourceNode: ret.
+"/    statements atLast: 1 put: ret.
+
+    "Modified: / 30-10-2014 / 22:35:15 / Jan Vrany <jan.vrany@fit.cvut.cz>"
 !
 
 block: method env: envRefNode
@@ -779,55 +787,57 @@
 
 endToDo: seqNum
 
-	| start limit incr iter step loopBlock o if test limitExpr init |
-	[
-		start := self Goto destination.
-		limit := self Value.
-		incr := self Assignment.
-		iter := incr variable.
-		(incr value isMessage and:
-		 [incr value selector == #+ and:
-		  [incr value receiver isVariable and: 
-		   [incr value receiver binding == iter binding]]]
-		) ifFalse: [self abort].
-		step := incr value arguments first.
-		loopBlock := self Sequence.
-		o := self Label destination.
-		if := self IfGoto: seqNum otherwise: o.
-		test := self Value.
-		(test isMessage and:
-		 [(test selector == #<= or: [test selector == #>=]) and:
-		  [(valueLabelMap at: test arguments first ifAbsent: [self abort]) destination = start]]
-		) ifFalse: [self abort].
-		limitExpr := test arguments first.
-		limitExpr isAssignment ifTrue: [
-			(limitExpr variable binding index == limit binding index 
-				and:[limitExpr variable binding scope == limit binding scope]) ifFalse: [self abort].
-			limitExpr := limitExpr value.
-		].
-		init := test receiver.
-		(init isAssignment and: [init variable binding == iter binding])
-			ifFalse: [self abort].
-	] on: Abort do: [^ false].
-	limit isVariable 
-		ifTrue:[scope 
-			removeTemp: limit binding 
-			ifAbsent:[Preferences compileBlocksAsClosures 
-				ifFalse:[scope removeTempFromOldBlock: limit]]].
-	loopBlock := self newBlock: loopBlock.
-	loopBlock arguments: {iter}.
-	self stackPush: ((step isLiteral: [:c | c = 1])
-		ifTrue: [RBMessageNode
-				receiver: init value
-				selector: #to:do:
-				arguments: {limitExpr. loopBlock}]
-		ifFalse: [RBMessageNode
-				receiver: init value
-				selector: #to:by:do:
-				arguments: {limitExpr. step. loopBlock}]).
-	self popTop.
-	self goto: seqNum.
-	^ true
+        | start limit incr iter step loopBlock o if test limitExpr init |
+        [
+                start := self Goto destination.
+                limit := self Value.
+                incr := self Assignment.
+                iter := incr variable.
+                (incr value isMessage and:
+                 [incr value selector == #+ and:
+                  [incr value receiver isVariable and: 
+                   [incr value receiver binding == iter binding]]]
+                ) ifFalse: [self abort].
+                step := incr value arguments first.
+                loopBlock := self Sequence.
+                o := self Label destination.
+                if := self IfGoto: seqNum otherwise: o.
+                test := self Value.
+                (test isMessage and:
+                 [(test selector == #<= or: [test selector == #>=]) and:
+                  [(valueLabelMap at: test arguments first ifAbsent: [self abort]) destination = start]]
+                ) ifFalse: [self abort].
+                limitExpr := test arguments first.
+                limitExpr isAssignment ifTrue: [
+                        (limitExpr variable binding index == limit binding index 
+                                and:[limitExpr variable binding scope == limit binding scope]) ifFalse: [self abort].
+                        limitExpr := limitExpr value.
+                ].
+                init := test receiver.
+                (init isAssignment and: [init variable binding == iter binding])
+                        ifFalse: [self abort].
+        ] on: Abort do: [^ false].
+        limit isVariable 
+                ifTrue:[scope 
+                        removeTemp: limit binding 
+                        ifAbsent:["Preferences compileBlocksAsClosures"true
+                                ifFalse:[scope removeTempFromOldBlock: limit]]].
+        loopBlock := self newBlock: loopBlock.
+        loopBlock arguments: {iter}.
+        self stackPush: ((step isLiteral: [:c | c = 1])
+                ifTrue: [RBMessageNode
+                                receiver: init value
+                                selector: #to:do:
+                                arguments: {limitExpr. loopBlock}]
+                ifFalse: [RBMessageNode
+                                receiver: init value
+                                selector: #to:by:do:
+                                arguments: {limitExpr. step. loopBlock}]).
+        self popTop.
+        self goto: seqNum.
+        ^ true
+
+    "Modified: / 30-10-2014 / 22:37:21 / Jan Vrany <jan.vrany@fit.cvut.cz>"
 !
 
 endWhile2: seqNum
@@ -923,11 +933,13 @@
 
 isExplicitReturn: goto
 
-	Preferences compileBlocksAsClosures 
-		ifTrue:[^ goto isRet 
-			and: [goto mapInstr notNil] 
-			and: [goto mapInstr isRemote or: [scope isBlockScope not]]]
-		ifFalse: [^goto isRet and: [goto mapInstr isBlockReturnTop not]]
+        "Preferences compileBlocksAsClosures"true
+                ifTrue:[^ goto isRet 
+                        and: [goto mapInstr notNil] 
+                        and: [goto mapInstr isRemote or: [scope isBlockScope not]]]
+                ifFalse: [^goto isRet and: [goto mapInstr isBlockReturnTop not]]
+
+    "Modified: / 30-10-2014 / 22:37:29 / Jan Vrany <jan.vrany@fit.cvut.cz>"
 !
 
 mapNode: node
@@ -945,16 +957,18 @@
 
 newBlock: sequence return: goto
 
-	| statements block |
-	statements := sequence statements.
-	(goto notNil and: [self isExplicitReturn: goto]) ifTrue: [
-		self addReturn: statements from: goto
-	].
-	sequence statements: statements.
-	block := RBBlockNode body: sequence.
-	sequence parent: block.
-	Preferences compileBlocksAsClosures ifFalse: [block scope: scope].
-	^block
+        | statements block |
+        statements := sequence statements.
+        (goto notNil and: [self isExplicitReturn: goto]) ifTrue: [
+                self addReturn: statements from: goto
+        ].
+        sequence statements: statements.
+        block := RBBlockNode body: sequence.
+        sequence parent: block.
+        "Preferences compileBlocksAsClosures"true ifFalse: [block scope: scope].
+        ^block
+
+    "Modified: / 30-10-2014 / 22:37:39 / Jan Vrany <jan.vrany@fit.cvut.cz>"
 !
 
 newLiteral: literal