compiler/PPCCodeGenerator.st
changeset 488 19a9c25960ef
parent 487 602215b19135
child 489 0ca7a70db0f5
equal deleted inserted replaced
487:602215b19135 488:19a9c25960ef
   156     compiler add: 'ifTrue: [ context next ].'.
   156     compiler add: 'ifTrue: [ context next ].'.
   157     compiler dedent.	
   157     compiler dedent.	
   158 !
   158 !
   159 
   159 
   160 retvalVar
   160 retvalVar
   161     ^ compiler currentReturnVariable 
   161     ^ compiler currentReturnVariable
       
   162 
       
   163     "Modified: / 15-06-2015 / 18:20:37 / Jan Vrany <jan.vrany@fit.cvut.cz>"
   162 !
   164 !
   163 
   165 
   164 startMethodForNode:node
   166 startMethodForNode:node
   165     node isMarkedForInline ifTrue:[ 
   167     node isMarkedForInline ifTrue:[ 
   166 		compiler startInline: (compiler idFor: node).
   168         compiler startInline: (compiler idFor: node).
   167 		compiler addComment: 'BEGIN inlined code of ' , node printString.
   169         compiler addComment: 'BEGIN inlined code of ' , node printString.
   168 		compiler indent.
   170         compiler indent.
   169     ] ifFalse:[ 
   171     ] ifFalse:[ 
   170 		compiler startMethod: (compiler idFor: node).
   172         compiler startMethod: (compiler idFor: node).
   171 		compiler addComment: 'GENERATED by ' , node printString.
   173         compiler addComment: 'GENERATED by ' , node printString.
   172 		compiler allocateReturnVariable.
   174         compiler allocateReturnVariable.
   173     ].
   175     ].
   174 
   176 
   175     "Created: / 23-04-2015 / 15:51:06 / Jan Vrany <jan.vrany@fit.cvut.cz>"
   177     "Created: / 23-04-2015 / 15:51:06 / Jan Vrany <jan.vrany@fit.cvut.cz>"
   176     "Modified: / 23-04-2015 / 19:13:25 / Jan Vrany <jan.vrany@fit.cvut.cz>"
   178     "Modified: / 23-04-2015 / 19:13:25 / Jan Vrany <jan.vrany@fit.cvut.cz>"
   177     "Modified (comment): / 23-04-2015 / 21:31:24 / Jan Vrany <jan.vrany@fit.cvut.cz>"
   179     "Modified (format): / 15-06-2015 / 18:03:07 / Jan Vrany <jan.vrany@fit.cvut.cz>"
   178 !
   180 !
   179 
   181 
   180 stopMethodForNode:aPPCNode
   182 stopMethodForNode:aPPCNode
   181     ^ aPPCNode isMarkedForInline ifTrue:[ 
   183     ^ aPPCNode isMarkedForInline ifTrue:[ 
   182 		compiler dedent.
   184 		compiler dedent.
   416     compiler dedent.
   418     compiler dedent.
   417     compiler add: '].'.
   419     compiler add: '].'.
   418 !
   420 !
   419 
   421 
   420 visitMappedActionNode: node
   422 visitMappedActionNode: node
   421     | blockNode blockBody |
   423     | child blockNode blockBody |
   422 
   424 
       
   425     child := node child.
   423     blockNode := node block ast copy.
   426     blockNode := node block ast copy.
   424     blockBody := blockNode body.
   427     blockBody := blockNode body.
   425 
   428 
   426     "Block return value is return value of last statement.
   429     "Block return value is return value of last statement.
   427      So if the method is not inline, make last statement a return.
   430      So if the method is not inline, make last statement a return.
   436 
   439 
   437         return := RBReturnNode value: blockBody statements last.
   440         return := RBReturnNode value: blockBody statements last.
   438         blockBody replaceNode: blockBody statements last withNode: return.
   441         blockBody replaceNode: blockBody statements last withNode: return.
   439     ].
   442     ].
   440 
   443 
   441     node child preferredChildrenVariableNames: blockNode argumentNames.
   444     child isSequenceNode ifTrue:[  
   442     node child isMarkedForInline ifTrue:[ 
   445         child isMarkedForInline ifTrue:[ 
   443         node child returnParsedObjectsAsCollection: false.
   446             child preferredChildrenVariableNames: blockNode argumentNames.
   444     ].
   447             child returnParsedObjectsAsCollection: false.
   445 
   448         ].
   446     compiler 
   449     ] ifFalse:[ 
   447           codeAssignParsedValueOf:[ self visit:node child ]
   450         "Child is not a sequence so it 'returns' only one object.
   448           to:self retvalVar.
   451          Therefore the block takes only one argument and it's value
       
   452          is value of child's retval.
       
   453          In the block, replace all references to block argument to
       
   454          my retvalVar. "
       
   455         | blockArg |
       
   456 
       
   457         blockArg := blockNode arguments first.
       
   458         blockBody variableNodesDo:[:variableNode| 
       
   459             variableNode name = blockArg name ifTrue:[ 
       
   460                 variableNode token value: self retvalVar.
       
   461             ].
       
   462         ]. 
       
   463     ].
       
   464 
       
   465     compiler codeAssignParsedValueOf: [ self visit: child ] to: self retvalVar.
   449     compiler codeIf: 'error' then: [ 
   466     compiler codeIf: 'error' then: [ 
   450         compiler codeReturn: 'failure'. 
   467         compiler codeReturn: 'failure'. 
   451     ] else: [
   468     ] else: [
   452         "First, extract mapped elements to variable..."
   469         "If the child is sequence and not inlined, extract
   453         blockNode arguments withIndexDo:[ :arg :idx |
   470          nodes from returned collection into used-to-be block variables"
   454             node child isMarkedForInline ifFalse:[ 
   471         (child isSequenceNode and:[ child returnParsedObjectsAsCollection ]) ifTrue:[
   455                 compiler allocateTemporaryVariableNamed: arg name.
   472             blockNode arguments withIndexDo:[ :arg :idx |
   456                 compiler codeAssign: (self retvalVar , ' at: ', idx printString) to: arg name.
   473                 node child isMarkedForInline ifFalse:[ 
       
   474                     compiler allocateTemporaryVariableNamed: arg name.
       
   475                     compiler codeAssign: (self retvalVar , ' at: ', idx printString) to: arg name.
       
   476                 ].
       
   477                 compiler addOnLine: '.'; nl.
   457             ].
   478             ].
   458             compiler add: '.'.
       
   459         ].
   479         ].
   460         compiler code: blockBody.    
   480         compiler code: blockBody.    
   461     ]
   481     ]
   462 
   482 
   463     "Created: / 02-06-2015 / 17:28:55 / Jan Vrany <jan.vrany@fit.cvut.cz>"
   483     "Created: / 02-06-2015 / 17:28:55 / Jan Vrany <jan.vrany@fit.cvut.cz>"
   464     "Modified: / 04-06-2015 / 23:46:41 / Jan Vrany <jan.vrany@fit.cvut.cz>"
   484     "Modified: / 15-06-2015 / 19:03:57 / Jan Vrany <jan.vrany@fit.cvut.cz>"
   465 !
   485 !
   466 
   486 
   467 visitMessagePredicateNode: node
   487 visitMessagePredicateNode: node
   468     compiler add: '(context peek ', node message, ') ifFalse: ['.
   488     compiler add: '(context peek ', node message, ') ifFalse: ['.
   469     compiler codeError: 'predicate not found'.
   489     compiler codeError: 'predicate not found'.
   719 !
   739 !
   720 
   740 
   721 visitStarAnyNode: node
   741 visitStarAnyNode: node
   722     | retvalVar sizeVar |
   742     | retvalVar sizeVar |
   723 
   743 
   724     retvalVar := compiler allocateReturnVariable.
   744     retvalVar := self retvalVar.
   725     sizeVar := compiler allocateTemporaryVariableNamed: 'size'.  
   745     sizeVar := compiler allocateTemporaryVariableNamed: 'size'.  
   726     compiler add: sizeVar , ' := context size - context position.'.
   746     compiler add: sizeVar , ' := context size - context position.'.
   727     compiler add: retvalVar,' := Array new: ',sizeVar,'.'.
   747     compiler add: retvalVar,' := Array new: ',sizeVar,'.'.
   728     compiler add: '(1 to: ',sizeVar,') do: [ :e | ',retvalVar,' at: e put: context next ].'.
   748     compiler add: '(1 to: ',sizeVar,') do: [ :e | ',retvalVar,' at: e put: context next ].'.
   729     compiler codeReturn.
   749     compiler codeReturn.
   730     
   750 
   731     "Modified: / 05-05-2015 / 14:13:52 / Jan Vrany <jan.vrany@fit.cvut.cz>"
   751     "Modified: / 15-06-2015 / 18:53:58 / Jan Vrany <jan.vrany@fit.cvut.cz>"
   732 !
   752 !
   733 
   753 
   734 visitStarCharSetPredicateNode: node
   754 visitStarCharSetPredicateNode: node
   735     | classification classificationId |
   755     | classification classificationId |
   736     
   756