compiler/PPCMethod.st
changeset 505 19d830b74322
parent 503 ff58cd9f1f3c
child 516 3b81c9e53352
equal deleted inserted replaced
504:0fb1f0799fc1 505:19d830b74322
     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 id variableForReturn category profile'
     6 	instanceVariableNames:'selector source category variableForReturn'
     7 	classVariableNames:''
     7 	classVariableNames:''
     8 	poolDictionaries:''
     8 	poolDictionaries:''
     9 	category:'PetitCompiler-Compiler-Codegen'
     9 	category:'PetitCompiler-Compiler-Codegen'
    10 !
    10 !
    11 
    11 
    17 
    17 
    18     ^ self basicNew initialize.
    18     ^ self basicNew initialize.
    19 ! !
    19 ! !
    20 
    20 
    21 !PPCMethod methodsFor:'accessing'!
    21 !PPCMethod methodsFor:'accessing'!
    22 
       
    23 body
       
    24     ^ buffer contents
       
    25 !
       
    26 
    22 
    27 bridge
    23 bridge
    28     ^ PPCBridge on: self methodName.
    24     ^ PPCBridge on: self methodName.
    29 !
    25 !
    30 
    26 
    37 
    33 
    38 category: value
    34 category: value
    39     category := value
    35     category := value
    40 !
    36 !
    41 
    37 
    42 code
       
    43     ^ String streamContents: [ :s |
       
    44         s nextPutAll: self methodName; cr.
       
    45         buffer codeOn: s.    
       
    46     ]
       
    47 
       
    48     "Modified: / 01-06-2015 / 21:24:47 / Jan Vrany <jan.vrany@fit.cvut.cz>"
       
    49 !
       
    50 
       
    51 id: value
    38 id: value
    52     id := value
    39     selector := value
    53 !
    40 !
    54 
    41 
    55 indentationLevel
    42 indentationLevel
    56     ^ buffer indentationLevel
    43     ^ source indentationLevel
    57 
    44 
    58     "Created: / 01-06-2015 / 21:38:31 / Jan Vrany <jan.vrany@fit.cvut.cz>"
    45     "Created: / 01-06-2015 / 21:38:31 / Jan Vrany <jan.vrany@fit.cvut.cz>"
    59 !
    46 !
    60 
    47 
    61 indentationLevel: anInteger
    48 indentationLevel: anInteger
    62     buffer indentationLevel: anInteger
    49     source indentationLevel: anInteger
    63 
    50 
    64     "Created: / 01-06-2015 / 21:38:58 / Jan Vrany <jan.vrany@fit.cvut.cz>"
    51     "Created: / 01-06-2015 / 21:38:58 / Jan Vrany <jan.vrany@fit.cvut.cz>"
    65 !
    52 !
    66 
    53 
    67 methodName
    54 methodName
    68     ^ id
    55     ^ selector
    69 !
    56 !
    70 
    57 
    71 profile
    58 source
    72     ^ profile
    59     ^ source isString ifTrue:[ 
    73 !
    60         source
    74 
    61     ] ifFalse:[ 
    75 profile: aBoolean
    62         String streamContents: [ :s |
    76     profile := aBoolean 
    63             s nextPutAll: self methodName; cr.
       
    64             source sourceOn:s.    
       
    65         ]
       
    66     ].
       
    67 
       
    68     "Created: / 24-07-2015 / 19:46:09 / Jan Vrany <jan.vrany@fit.cvut.cz>"
       
    69 !
       
    70 
       
    71 source: aString
       
    72     source := aString
       
    73 
       
    74     "Created: / 24-07-2015 / 19:48:05 / Jan Vrany <jan.vrany@fit.cvut.cz>"
    77 ! !
    75 ! !
    78 
    76 
    79 !PPCMethod methodsFor:'as yet unclassified'!
    77 !PPCMethod methodsFor:'as yet unclassified'!
    80 
    78 
    81 add: string
    79 add: string
    82     buffer add: string
    80     source add: string
    83 
    81 
    84     "Modified: / 01-06-2015 / 21:09:06 / Jan Vrany <jan.vrany@fit.cvut.cz>"
    82     "Modified: / 01-06-2015 / 21:09:06 / Jan Vrany <jan.vrany@fit.cvut.cz>"
    85 !
    83 !
    86 
    84 
    87 addOnLine: string
    85 addOnLine: string
    88     buffer addOnLine: string
    86     source addOnLine: string
    89 
    87 
    90     "Modified: / 01-06-2015 / 21:09:20 / Jan Vrany <jan.vrany@fit.cvut.cz>"
    88     "Modified: / 01-06-2015 / 21:09:20 / Jan Vrany <jan.vrany@fit.cvut.cz>"
    91 !
    89 !
    92 
    90 
    93 call
    91 call
    94     ^ 'self ', self methodName, '.'.
    92     ^ 'self ', self methodName, '.'.
    95 !
    93 !
    96 
    94 
    97 profilingBegin
    95 profilingBegin
    98     self profile ifTrue: [ 
    96     self profile ifTrue: [ 
    99  								^ '  context methodInvoked: #', id, '.'	
    97  								^ '  context methodInvoked: #', selector, '.'	
   100     ].
    98     ].
   101     ^ ''
    99     ^ ''
   102 !
   100 !
   103 
   101 
   104 profilingEnd
   102 profilingEnd
   105     self profile ifTrue: [ 
   103     self profile ifTrue: [ 
   106  								^ '  context methodFinished: #', id, '.'	
   104  								^ '  context methodFinished: #', selector, '.'	
   107     ].
   105     ].
   108     ^ ''
   106     ^ ''
   109 ! !
   107 ! !
   110 
   108 
   111 !PPCMethod methodsFor:'code generation'!
   109 !PPCMethod methodsFor:'code generation'!
   112 
   110 
   113 code: aStringOrBlockOrRBParseNode
   111 code: aStringOrBlockOrRBParseNode
   114     buffer code: aStringOrBlockOrRBParseNode.
   112     source code: aStringOrBlockOrRBParseNode.
   115 
   113 
   116     "Created: / 01-06-2015 / 22:31:16 / Jan Vrany <jan.vrany@fit.cvut.cz>"
   114     "Created: / 01-06-2015 / 22:31:16 / Jan Vrany <jan.vrany@fit.cvut.cz>"
   117     "Modified (format): / 01-06-2015 / 23:50:26 / Jan Vrany <jan.vrany@fit.cvut.cz>"
   115     "Modified (format): / 01-06-2015 / 23:50:26 / Jan Vrany <jan.vrany@fit.cvut.cz>"
   118 !
   116 !
   119 
   117 
   120 codeBlock: contents
   118 codeBlock: contents
   121     | outerBlock innerBlock |
   119     | outerBlock innerBlock |
   122 
   120 
   123     outerBlock := buffer.
   121     outerBlock := source.
   124     innerBlock := PPCCodeBlock new.
   122     innerBlock := PPCCodeBlock new.
   125     innerBlock indentationLevel: outerBlock indentationLevel + 1.  
   123     innerBlock indentationLevel: outerBlock indentationLevel + 1.  
   126     [ 
   124     [ 
   127         outerBlock addOnLine:'['.
   125         outerBlock addOnLine:'['.
   128         buffer := innerBlock.
   126         source := innerBlock.
   129         self code: contents.
   127         self code: contents.
   130     ] ensure:[
   128     ] ensure:[
   131         outerBlock 
   129         outerBlock 
   132             code: (String streamContents:[:s | innerBlock codeOn: s]);
   130             code: (String streamContents:[:s | innerBlock sourceOn:s]);
   133             add:']'.
   131             add:']'.
   134         buffer := outerBlock.
   132         source := outerBlock.
   135     ]
   133     ]
   136 
   134 
   137     "Created: / 01-06-2015 / 22:33:21 / Jan Vrany <jan.vrany@fit.cvut.cz>"
   135     "Created: / 01-06-2015 / 22:33:21 / Jan Vrany <jan.vrany@fit.cvut.cz>"
   138     "Modified: / 03-06-2015 / 06:11:32 / Jan Vrany <jan.vrany@fit.cvut.cz>"
   136     "Modified: / 03-06-2015 / 06:11:32 / Jan Vrany <jan.vrany@fit.cvut.cz>"
   139 ! !
   137 ! !
   140 
   138 
   141 !PPCMethod methodsFor:'code generation - indenting'!
   139 !PPCMethod methodsFor:'code generation - indenting'!
   142 
   140 
   143 dedent
   141 dedent
   144     buffer dedent
   142     source dedent
   145 
   143 
   146     "Created: / 01-06-2015 / 21:32:28 / Jan Vrany <jan.vrany@fit.cvut.cz>"
   144     "Created: / 01-06-2015 / 21:32:28 / Jan Vrany <jan.vrany@fit.cvut.cz>"
   147 !
   145 !
   148 
   146 
   149 indent
   147 indent
   150     buffer indent
   148     source indent
   151 
   149 
   152     "Created: / 01-06-2015 / 21:32:22 / Jan Vrany <jan.vrany@fit.cvut.cz>"
   150     "Created: / 01-06-2015 / 21:32:22 / Jan Vrany <jan.vrany@fit.cvut.cz>"
   153 !
   151 !
   154 
   152 
   155 nl
   153 nl
   156 
   154 
   157     buffer nl
   155     source nl
   158 
   156 
   159     "Created: / 01-06-2015 / 21:52:31 / Jan Vrany <jan.vrany@fit.cvut.cz>"
   157     "Created: / 01-06-2015 / 21:52:31 / Jan Vrany <jan.vrany@fit.cvut.cz>"
   160 ! !
   158 ! !
   161 
   159 
   162 !PPCMethod methodsFor:'code generation - variables'!
   160 !PPCMethod methodsFor:'code generation - variables'!
   187 
   185 
   188 allocateTemporaryVariableNamed:preferredName 
   186 allocateTemporaryVariableNamed:preferredName 
   189     "Allocate a new variable with (preferably) given name.
   187     "Allocate a new variable with (preferably) given name.
   190      Returns a real variable name that should be used."
   188      Returns a real variable name that should be used."
   191 
   189 
   192     ^ buffer allocateTemporaryVariableNamed: preferredName
   190     ^ source allocateTemporaryVariableNamed: preferredName
   193 
   191 
   194     "Created: / 23-04-2015 / 17:37:55 / Jan Vrany <jan.vrany@fit.cvut.cz>"
   192     "Created: / 23-04-2015 / 17:37:55 / Jan Vrany <jan.vrany@fit.cvut.cz>"
   195     "Modified: / 01-06-2015 / 21:04:02 / Jan Vrany <jan.vrany@fit.cvut.cz>"
   193     "Modified: / 01-06-2015 / 21:04:02 / Jan Vrany <jan.vrany@fit.cvut.cz>"
   196 !
   194 !
   197 
   195 
   213 ! !
   211 ! !
   214 
   212 
   215 !PPCMethod methodsFor:'initialization'!
   213 !PPCMethod methodsFor:'initialization'!
   216 
   214 
   217 initialize
   215 initialize
   218     buffer := PPCCodeBlock new.
   216     source := PPCCodeBlock new.
   219 
   217 
   220     "Modified: / 01-06-2015 / 21:33:36 / Jan Vrany <jan.vrany@fit.cvut.cz>"
   218     "Modified: / 01-06-2015 / 21:33:36 / Jan Vrany <jan.vrany@fit.cvut.cz>"
   221 ! !
   219 ! !
   222 
   220 
   223 !PPCMethod methodsFor:'printing'!
   221 !PPCMethod methodsFor:'printing'!
   225 printOn:aStream
   223 printOn:aStream
   226     "append a printed representation if the receiver to the argument, aStream"
   224     "append a printed representation if the receiver to the argument, aStream"
   227 
   225 
   228     super printOn:aStream.
   226     super printOn:aStream.
   229     aStream nextPutAll:' id: '.
   227     aStream nextPutAll:' id: '.
   230     id printOn:aStream.
   228     selector printOn:aStream.
   231 
   229 
   232     "Modified: / 23-04-2015 / 12:32:30 / Jan Vrany <jan.vrany@fit.cvut.cz>"
   230     "Modified: / 23-04-2015 / 12:32:30 / Jan Vrany <jan.vrany@fit.cvut.cz>"
   233 ! !
   231 ! !
   234 
   232 
   235 !PPCMethod methodsFor:'testing'!
   233 !PPCMethod methodsFor:'testing'!