compiler/PPCMethod.st
changeset 438 20598d7ce9fa
parent 422 116d2b2af905
child 448 02db0b67ed3f
child 452 9f4558b3be66
--- a/compiler/PPCMethod.st	Tue Apr 21 17:20:11 2015 +0100
+++ b/compiler/PPCMethod.st	Thu Apr 30 23:43:14 2015 +0200
@@ -3,7 +3,7 @@
 "{ NameSpace: Smalltalk }"
 
 Object subclass:#PPCMethod
-	instanceVariableNames:'buffer variables indentation id profile canInline'
+	instanceVariableNames:'buffer variables indentation id profile variableForReturn'
 	classVariableNames:''
 	poolDictionaries:''
 	category:'PetitCompiler-Core'
@@ -22,7 +22,7 @@
 
 add: string
 	self nl.
-	indentation timesRepeat: [ buffer nextPutAll: '  ' ].
+	indentation timesRepeat: [ buffer nextPut: Character tab  ].
 	self addOnLine: string.
 !
 
@@ -31,11 +31,12 @@
 !
 
 addVariable: name
+	(variables includes: name) ifTrue:[ 
+	    self error:'Duplicate variable name, must rename'.
+	].
 	variables add: name.
-!
 
-allowInline
-	canInline := true
+    "Modified: / 23-04-2015 / 12:29:58 / Jan Vrany <jan.vrany@fit.cvut.cz>"
 !
 
 body
@@ -51,11 +52,13 @@
 !
 
 code
-	^ self methodName, String cr,  
-		self variables, String cr,
-		self profilingBegin, String cr,
-		self body, String cr
-"		self profilingEnd"
+	^ self methodName, Character cr asString,  
+		self variables, Character cr asString,
+		self profilingBegin, Character cr asString,
+		self body, Character cr asString
+"               self profilingEnd"
+
+    "Modified: / 23-04-2015 / 19:26:39 / Jan Vrany <jan.vrany@fit.cvut.cz>"
 !
 
 dedent
@@ -70,6 +73,10 @@
 	indentation := indentation + 1
 !
 
+isInline
+	^ false
+!
+
 isMethod
 	^ true
 !
@@ -104,17 +111,72 @@
 	^ ''
 !
 
+returnVariable
+    ^  variableForReturn
+
+    "Created: / 23-04-2015 / 20:50:50 / Jan Vrany <jan.vrany@fit.cvut.cz>"
+!
+
+returnVariable: aString
+    ^ variableForReturn := aString
+
+    "Created: / 23-04-2015 / 18:23:47 / Jan Vrany <jan.vrany@fit.cvut.cz>"
+    "Modified: / 23-04-2015 / 21:08:54 / Jan Vrany <jan.vrany@fit.cvut.cz>"
+!
+
 variables
 	^ '  | ', (variables inject: '' into: [ :s :e | s, ' ', e]), ' |'
 ! !
 
+!PPCMethod methodsFor:'code generation - variables'!
+
+allocateReturnVariable
+    
+	^ variableForReturn isNil ifTrue:[ 
+		variableForReturn := self allocateTemporaryVariableNamed: 'retval'  
+	] ifFalse:[ 
+		variableForReturn
+	].
+
+    "Created: / 23-04-2015 / 18:03:40 / Jan Vrany <jan.vrany@fit.cvut.cz>"
+!
+
+allocateTemporaryVariableNamed:preferredName 
+    "Allocate a new variable with (preferably) given name.
+     Returns a real variable name that should be used."
+    
+    (variables includes:preferredName) ifFalse:[
+	variables add:preferredName.
+	^ preferredName
+    ] ifTrue:[
+	| name |
+
+	name := preferredName , '_' , (variables size + 1) printString.
+	variables add:name.
+	^ name
+    ].
+
+    "Created: / 23-04-2015 / 17:37:55 / Jan Vrany <jan.vrany@fit.cvut.cz>"
+! !
+
 !PPCMethod methodsFor:'initialization'!
 
 initialize
 	buffer := WriteStream on: ''.
 	indentation := 1.
 	variables := OrderedCollection new.
-	canInline := false
+! !
+
+!PPCMethod methodsFor:'printing & storing'!
+
+printOn:aStream
+    "append a printed representation if the receiver to the argument, aStream"
+
+    super printOn:aStream.
+    aStream nextPutAll:' id: '.
+    id printOn:aStream.
+
+    "Modified: / 23-04-2015 / 12:32:30 / Jan Vrany <jan.vrany@fit.cvut.cz>"
 ! !
 
 !PPCMethod class methodsFor:'documentation'!