compiler/PPCMethod.st
changeset 438 20598d7ce9fa
parent 422 116d2b2af905
child 448 02db0b67ed3f
child 452 9f4558b3be66
equal deleted inserted replaced
437:54b3bc9e3987 438:20598d7ce9fa
     1 "{ Package: 'stx:goodies/petitparser/compiler' }"
     1 "{ Package: 'stx:goodies/petitparser/compiler' }"
     2 
     2 
     3 "{ NameSpace: Smalltalk }"
     3 "{ NameSpace: Smalltalk }"
     4 
     4 
     5 Object subclass:#PPCMethod
     5 Object subclass:#PPCMethod
     6 	instanceVariableNames:'buffer variables indentation id profile canInline'
     6 	instanceVariableNames:'buffer variables indentation id profile variableForReturn'
     7 	classVariableNames:''
     7 	classVariableNames:''
     8 	poolDictionaries:''
     8 	poolDictionaries:''
     9 	category:'PetitCompiler-Core'
     9 	category:'PetitCompiler-Core'
    10 !
    10 !
    11 
    11 
    20 
    20 
    21 !PPCMethod methodsFor:'as yet unclassified'!
    21 !PPCMethod methodsFor:'as yet unclassified'!
    22 
    22 
    23 add: string
    23 add: string
    24 	self nl.
    24 	self nl.
    25 	indentation timesRepeat: [ buffer nextPutAll: '  ' ].
    25 	indentation timesRepeat: [ buffer nextPut: Character tab  ].
    26 	self addOnLine: string.
    26 	self addOnLine: string.
    27 !
    27 !
    28 
    28 
    29 addOnLine: string
    29 addOnLine: string
    30 	buffer nextPutAll: string.
    30 	buffer nextPutAll: string.
    31 !
    31 !
    32 
    32 
    33 addVariable: name
    33 addVariable: name
       
    34 	(variables includes: name) ifTrue:[ 
       
    35 	    self error:'Duplicate variable name, must rename'.
       
    36 	].
    34 	variables add: name.
    37 	variables add: name.
    35 !
       
    36 
    38 
    37 allowInline
    39     "Modified: / 23-04-2015 / 12:29:58 / Jan Vrany <jan.vrany@fit.cvut.cz>"
    38 	canInline := true
       
    39 !
    40 !
    40 
    41 
    41 body
    42 body
    42 	^ buffer contents
    43 	^ buffer contents
    43 !
    44 !
    49 call
    50 call
    50 	^ 'self ', self methodName, '.'.
    51 	^ 'self ', self methodName, '.'.
    51 !
    52 !
    52 
    53 
    53 code
    54 code
    54 	^ self methodName, String cr,  
    55 	^ self methodName, Character cr asString,  
    55 		self variables, String cr,
    56 		self variables, Character cr asString,
    56 		self profilingBegin, String cr,
    57 		self profilingBegin, Character cr asString,
    57 		self body, String cr
    58 		self body, Character cr asString
    58 "		self profilingEnd"
    59 "               self profilingEnd"
       
    60 
       
    61     "Modified: / 23-04-2015 / 19:26:39 / Jan Vrany <jan.vrany@fit.cvut.cz>"
    59 !
    62 !
    60 
    63 
    61 dedent
    64 dedent
    62 	indentation := indentation - 1
    65 	indentation := indentation - 1
    63 !
    66 !
    66 	id := value
    69 	id := value
    67 !
    70 !
    68 
    71 
    69 indent 
    72 indent 
    70 	indentation := indentation + 1
    73 	indentation := indentation + 1
       
    74 !
       
    75 
       
    76 isInline
       
    77 	^ false
    71 !
    78 !
    72 
    79 
    73 isMethod
    80 isMethod
    74 	^ true
    81 	^ true
    75 !
    82 !
   102  		^ '  context methodFinished: #', id, '.'	
   109  		^ '  context methodFinished: #', id, '.'	
   103 	].
   110 	].
   104 	^ ''
   111 	^ ''
   105 !
   112 !
   106 
   113 
       
   114 returnVariable
       
   115     ^  variableForReturn
       
   116 
       
   117     "Created: / 23-04-2015 / 20:50:50 / Jan Vrany <jan.vrany@fit.cvut.cz>"
       
   118 !
       
   119 
       
   120 returnVariable: aString
       
   121     ^ variableForReturn := aString
       
   122 
       
   123     "Created: / 23-04-2015 / 18:23:47 / Jan Vrany <jan.vrany@fit.cvut.cz>"
       
   124     "Modified: / 23-04-2015 / 21:08:54 / Jan Vrany <jan.vrany@fit.cvut.cz>"
       
   125 !
       
   126 
   107 variables
   127 variables
   108 	^ '  | ', (variables inject: '' into: [ :s :e | s, ' ', e]), ' |'
   128 	^ '  | ', (variables inject: '' into: [ :s :e | s, ' ', e]), ' |'
       
   129 ! !
       
   130 
       
   131 !PPCMethod methodsFor:'code generation - variables'!
       
   132 
       
   133 allocateReturnVariable
       
   134     
       
   135 	^ variableForReturn isNil ifTrue:[ 
       
   136 		variableForReturn := self allocateTemporaryVariableNamed: 'retval'  
       
   137 	] ifFalse:[ 
       
   138 		variableForReturn
       
   139 	].
       
   140 
       
   141     "Created: / 23-04-2015 / 18:03:40 / Jan Vrany <jan.vrany@fit.cvut.cz>"
       
   142 !
       
   143 
       
   144 allocateTemporaryVariableNamed:preferredName 
       
   145     "Allocate a new variable with (preferably) given name.
       
   146      Returns a real variable name that should be used."
       
   147     
       
   148     (variables includes:preferredName) ifFalse:[
       
   149 	variables add:preferredName.
       
   150 	^ preferredName
       
   151     ] ifTrue:[
       
   152 	| name |
       
   153 
       
   154 	name := preferredName , '_' , (variables size + 1) printString.
       
   155 	variables add:name.
       
   156 	^ name
       
   157     ].
       
   158 
       
   159     "Created: / 23-04-2015 / 17:37:55 / Jan Vrany <jan.vrany@fit.cvut.cz>"
   109 ! !
   160 ! !
   110 
   161 
   111 !PPCMethod methodsFor:'initialization'!
   162 !PPCMethod methodsFor:'initialization'!
   112 
   163 
   113 initialize
   164 initialize
   114 	buffer := WriteStream on: ''.
   165 	buffer := WriteStream on: ''.
   115 	indentation := 1.
   166 	indentation := 1.
   116 	variables := OrderedCollection new.
   167 	variables := OrderedCollection new.
   117 	canInline := false
   168 ! !
       
   169 
       
   170 !PPCMethod methodsFor:'printing & storing'!
       
   171 
       
   172 printOn:aStream
       
   173     "append a printed representation if the receiver to the argument, aStream"
       
   174 
       
   175     super printOn:aStream.
       
   176     aStream nextPutAll:' id: '.
       
   177     id printOn:aStream.
       
   178 
       
   179     "Modified: / 23-04-2015 / 12:32:30 / Jan Vrany <jan.vrany@fit.cvut.cz>"
   118 ! !
   180 ! !
   119 
   181 
   120 !PPCMethod class methodsFor:'documentation'!
   182 !PPCMethod class methodsFor:'documentation'!
   121 
   183 
   122 version_HG
   184 version_HG