compiler/PPCCompiler.st
changeset 465 f729f6cd3c76
parent 460 87a3d30ab570
parent 464 f6d77fee9811
child 474 62b5330d8b23
equal deleted inserted replaced
463:d4014e0a47a0 465:f729f6cd3c76
     6 	instanceVariableNames:'compilerStack compiledParser cache currentMethod ids constants
     6 	instanceVariableNames:'compilerStack compiledParser cache currentMethod ids constants
     7 		compiledParserName compiledParserSuperclass returnVariable
     7 		compiledParserName compiledParserSuperclass returnVariable
     8 		arguments'
     8 		arguments'
     9 	classVariableNames:''
     9 	classVariableNames:''
    10 	poolDictionaries:''
    10 	poolDictionaries:''
    11 	category:'PetitCompiler-Core'
    11 	category:'PetitCompiler-Compiler'
    12 !
    12 !
    13 
    13 
    14 
    14 
    15 !PPCCompiler class methodsFor:'instance creation'!
    15 !PPCCompiler class methodsFor:'instance creation'!
    16 
    16 
   176     
   176     
   177     "TODO JK: Hack alert, whatever is magic constant!!"
   177     "TODO JK: Hack alert, whatever is magic constant!!"
   178     (variable == #whatever) ifFalse: [ 
   178     (variable == #whatever) ifFalse: [ 
   179         "Do not assign, if somebody does not care!!"
   179         "Do not assign, if somebody does not care!!"
   180         self add: variable ,' := ', code.
   180         self add: variable ,' := ', code.
   181  		] ifTrue: [ 
       
   182         "In case code hava a side effect"
       
   183  				self add: code	
       
   184     ]
   181     ]
   185 !
   182 !
   186 
   183 
   187 codeClearError
   184 codeClearError
   188     self add: 'self clearError.'.
   185     self add: 'self clearError.'.
   189 !
   186 !
   190 
   187 
       
   188 codeError
       
   189     self add: 'self error: ''message notspecified''.'.
       
   190 !
       
   191 
   191 codeError: errorMessage
   192 codeError: errorMessage
   192     self add: 'self error: ''', errorMessage, '''.'
   193     self add: 'self error: ''', errorMessage, '''.'
       
   194 !
       
   195 
       
   196 codeError: errorMessage at: position
       
   197     self add: 'self error: ''', errorMessage, ''' at: ', position asString, '.'
       
   198 !
       
   199 
       
   200 codeEvaluate: selector argument: argument on: variable
       
   201     self assert: variable isNil not.
       
   202     
       
   203     "TODO JK: Hack alert, whatever is magic constant!!"
       
   204     (variable == #whatever) ifFalse: [ 
       
   205         "Do not assign, if somebody does not care!!"
       
   206         self add: variable, ' ', selector,' ', argument.
       
   207  	] ifTrue: [ 
       
   208         "In case argument has a side effect"
       
   209  		self add: argument	
       
   210     ]
       
   211 !
       
   212 
       
   213 codeEvaluateAndAssign: argument to: variable
       
   214     self assert: variable isNil not.
       
   215     
       
   216     "TODO JK: Hack alert, whatever is magic constant!!"
       
   217     (variable == #whatever) ifFalse: [ 
       
   218         "Do not assign, if somebody does not care!!"
       
   219         self add: variable ,' := ', argument.
       
   220     ] ifTrue: [ 
       
   221         "In case an argument has a side effect"
       
   222  		self add: argument.	
       
   223     ]
   193 !
   224 !
   194 
   225 
   195 codeHalt
   226 codeHalt
   196     self add: 'self halt. '
   227     self add: 'self halt. '
   197 !
   228 !
   226 
   257 
   227 codeReturn: code
   258 codeReturn: code
   228     " - returns whatever is in code OR
   259     " - returns whatever is in code OR
   229       - assigns whatever is in code into the returnVariable"
   260       - assigns whatever is in code into the returnVariable"
   230    currentMethod isInline ifTrue:[ 
   261    currentMethod isInline ifTrue:[ 
   231         self codeAssign: code to: currentMethod returnVariable. 
   262         self codeEvaluateAndAssign: code to: currentMethod returnVariable. 
   232    ] ifFalse: [ 
   263    ] ifFalse: [ 
   233         self add: '^ ', code 		
   264         self add: '^ ', code 		
   234     ]
   265     ]
   235 
   266 
   236     "Created: / 23-04-2015 / 18:01:05 / Jan Vrany <jan.vrany@fit.cvut.cz>"
   267     "Created: / 23-04-2015 / 18:01:05 / Jan Vrany <jan.vrany@fit.cvut.cz>"
   251     ].
   282     ].
   252     
   283     
   253     method isInline ifTrue: [ 
   284     method isInline ifTrue: [ 
   254         self callOnLine: method 
   285         self callOnLine: method 
   255     ] ifFalse: [ 
   286     ] ifFalse: [ 
   256         self codeAssign: (method call) to: aString.
   287         self codeEvaluateAndAssign: (method call) to: aString.
   257     ]	
   288     ]	
   258     
   289     
   259     "Created: / 23-04-2015 / 18:21:51 / Jan Vrany <jan.vrany@fit.cvut.cz>"
   290     "Created: / 23-04-2015 / 18:21:51 / Jan Vrany <jan.vrany@fit.cvut.cz>"
       
   291 !
       
   292 
       
   293 codeTokenGuard: node ifFalse: codeBlock
       
   294     | guard id |
       
   295     guard := PPCTokenGuard on: node.
       
   296     (guard makesSense) ifTrue: [ 
       
   297         id := self idFor: guard firstToken.
       
   298 
       
   299         self add: 'self ', id asString, ' ifFalse: ['.
       
   300             self indent.
       
   301             codeBlock value.
       
   302             self dedent.
       
   303         self add: '].'.
       
   304     ]
   260 !
   305 !
   261 
   306 
   262 codeTranscriptShow: text
   307 codeTranscriptShow: text
   263     (arguments profile) ifTrue: [ 
   308     (arguments profile) ifTrue: [ 
   264         self add: 'Transcript show: ', text storeString, '; cr.'.
   309         self add: 'Transcript show: ', text storeString, '; cr.'.
   300 idFor: object prefixed: prefix suffixed: suffix effect: effect
   345 idFor: object prefixed: prefix suffixed: suffix effect: effect
   301     | name id |
   346     | name id |
   302     ^ ids at: object ifAbsentPut: [ 
   347     ^ ids at: object ifAbsentPut: [ 
   303         ((object isKindOf: PPCNode) and: [object name isNotNil]) ifTrue: [ 
   348         ((object isKindOf: PPCNode) and: [object name isNotNil]) ifTrue: [ 
   304             "Do not use prefix, if there is a name"
   349             "Do not use prefix, if there is a name"
   305             name := self asSelector: object name.
   350             name := self asSelector: (object name asString).
   306             id := (name, suffix) asSymbol.
   351             id := (name, suffix) asSymbol.
   307             
   352             
   308             "Make sure, that the generated ID is uniqe!!"
   353             "Make sure, that the generated ID is uniqe!!"
   309             (ids includes: id) ifTrue: [ 
   354             (ids includes: id) ifTrue: [ 
   310                 (id, '_', ids size asString) asSymbol 
   355                 (id, '_', ids size asString) asSymbol 
   320 idFor: object suffixed: suffix
   365 idFor: object suffixed: suffix
   321     self assert: (object isKindOf: PPCNode) description: 'Shold use PPCNode for ids'.
   366     self assert: (object isKindOf: PPCNode) description: 'Shold use PPCNode for ids'.
   322     ^ self idFor: object prefixed: object prefix suffixed: suffix effect: #none
   367     ^ self idFor: object prefixed: object prefix suffixed: suffix effect: #none
   323 ! !
   368 ! !
   324 
   369 
       
   370 !PPCCompiler methodsFor:'code generation - profiling'!
       
   371 
       
   372 profileTokenRead: tokenName
       
   373     arguments profile ifTrue: [ 
       
   374         self add: 'context tokenRead: ', tokenName storeString, '.'
       
   375     ]
       
   376 ! !
       
   377 
   325 !PPCCompiler methodsFor:'code generation - support'!
   378 !PPCCompiler methodsFor:'code generation - support'!
   326 
   379 
   327 cache: id as: value
   380 cache: id as: value
   328     cache at: id put: value.
   381     cache at: id put: value.
   329 !
   382 !
   395 !
   448 !
   396 
   449 
   397 stopMethod
   450 stopMethod
   398     self cache: currentMethod methodName as: currentMethod.
   451     self cache: currentMethod methodName as: currentMethod.
   399     
   452     
   400     arguments profile ifTrue: [ Transcript show: currentMethod code; cr. ].
   453     "arguments profile ifTrue: [ Transcript show: currentMethod code; cr. ]."
   401     ^ self pop.
   454     ^ self pop.
   402 
   455 
   403     "Modified: / 01-05-2015 / 14:18:07 / Jan Vrany <jan.vrany@fit.cvut.cz>"
   456     "Modified: / 01-05-2015 / 14:18:07 / Jan Vrany <jan.vrany@fit.cvut.cz>"
   404 !
   457 !
   405 
   458