compiler/TLLVMCodeGenerator.st
changeset 16 17a2d1d9f205
parent 15 10a95d798b36
equal deleted inserted replaced
15:10a95d798b36 16:17a2d1d9f205
       
     1 "
       
     2     Copyright (C) 2015-now Jan Vrany
       
     3 
       
     4     This code is not an open-source (yet). You may use this code
       
     5     for your own experiments and projects, given that:
       
     6 
       
     7     * all modification to the code will be sent to the
       
     8       original author for inclusion in future releases
       
     9     * this is not used in any commercial software
       
    10 
       
    11     This license is provisional and may (will) change in
       
    12     a future.
       
    13 "
     1 "{ Package: 'jv:tea/compiler' }"
    14 "{ Package: 'jv:tea/compiler' }"
     2 
    15 
     3 "{ NameSpace: Smalltalk }"
    16 "{ NameSpace: Smalltalk }"
     4 
    17 
     5 TCompilerPass subclass:#TLLVMCodeGenerator
    18 TCompilerPass subclass:#TLLVMCodeGenerator
     6 	instanceVariableNames:'function asm'
    19 	instanceVariableNames:'function asm'
     7 	classVariableNames:'SelectorSpecialCharMappingTable'
    20 	classVariableNames:'SelectorSpecialCharMappingTable'
     8 	poolDictionaries:''
    21 	poolDictionaries:'LLVMAtomicOrdering LLVMAtomicRMWBinOp LLVMAttribute
       
    22 		LLVMByteOrdering LLVMCallConv LLVMDLLStorageClass
       
    23 		LLVMIntPredicate'
     9 	category:'Languages-Tea-Compiler-Internals'
    24 	category:'Languages-Tea-Compiler-Internals'
    10 !
    25 !
    11 
    26 
       
    27 !TLLVMCodeGenerator class methodsFor:'documentation'!
       
    28 
       
    29 copyright
       
    30 "
       
    31     Copyright (C) 2015-now Jan Vrany
       
    32 
       
    33     This code is not an open-source (yet). You may use this code
       
    34     for your own experiments and projects, given that:
       
    35 
       
    36     * all modification to the code will be sent to the
       
    37       original author for inclusion in future releases
       
    38     * this is not used in any commercial software
       
    39 
       
    40     This license is provisional and may (will) change in
       
    41     a future.
       
    42 "
       
    43 ! !
    12 
    44 
    13 !TLLVMCodeGenerator class methodsFor:'initialization'!
    45 !TLLVMCodeGenerator class methodsFor:'initialization'!
    14 
    46 
    15 initialize
    47 initialize
    16     "Invoked at system start or when the class is dynamically loaded."
    48     "Invoked at system start or when the class is dynamically loaded."
    90         | block allocas |
   122         | block allocas |
    91 
   123 
    92         allocas := anRBVariableNode scope llvmAllocas.
   124         allocas := anRBVariableNode scope llvmAllocas.
    93         block := asm block.
   125         block := asm block.
    94         asm block: allocas.
   126         asm block: allocas.
    95         value := asm alloca: (binding type asLLVMTypeInModule: context module)  as: binding name.
   127         value := asm alloca: (binding type asLLVMTypeInModule: context llvmModule)  as: binding name.
    96         asm block: block.
   128         asm block: block.
    97     ].
   129     ].
    98     binding llvmValue: value.
   130     binding llvmValue: value.
    99 
   131 
   100     "Created: / 02-09-2015 / 08:43:00 / Jan Vrany <jan.vrany@fit.cvut.cz>"
   132     "Created: / 02-09-2015 / 08:43:00 / Jan Vrany <jan.vrany@fit.cvut.cz>"
   101     "Modified: / 23-09-2015 / 21:47:38 / Jan Vrany <jan.vrany@fit.cvut.cz>"
   133     "Modified: / 24-09-2015 / 16:18:03 / Jan Vrany <jan.vrany@fit.cvut.cz>"
   102 ! !
   134 ! !
   103 
   135 
   104 !TLLVMCodeGenerator methodsFor:'visitor-double dispatching'!
   136 !TLLVMCodeGenerator methodsFor:'visitor-double dispatching'!
   105 
   137 
   106 acceptArrayNode: anArrayNode
   138 acceptArrayNode: anArrayNode
   189 
   221 
   190     "Created: / 23-09-2015 / 21:57:31 / Jan Vrany <jan.vrany@fit.cvut.cz>"
   222     "Created: / 23-09-2015 / 21:57:31 / Jan Vrany <jan.vrany@fit.cvut.cz>"
   191 !
   223 !
   192 
   224 
   193 acceptLiteralNode: aLiteralNode
   225 acceptLiteralNode: aLiteralNode
   194     ^ aLiteralNode binding asLLVMValueInModule: context module.
   226     ^ aLiteralNode binding asLLVMValueInModule: context llvmModule.
   195 
   227 
   196     "Created: / 31-08-2015 / 10:13:55 / Jan Vrany <jan.vrany@fit.cvut.cz>"
   228     "Created: / 31-08-2015 / 10:13:55 / Jan Vrany <jan.vrany@fit.cvut.cz>"
   197     "Modified: / 31-08-2015 / 12:20:52 / Jan Vrany <jan.vrany@fit.cvut.cz>"
   229     "Modified: / 24-09-2015 / 16:17:51 / Jan Vrany <jan.vrany@fit.cvut.cz>"
   198 !
   230 !
   199 
   231 
   200 acceptMessageNode: aMessageNode
   232 acceptMessageNode: aMessageNode
   201     | receiver arguments methodFunction |
   233     | receiver arguments methodFunction |
   202 
   234 
   203     receiver := self visitNode: aMessageNode receiver.
   235     receiver := self visitNode: aMessageNode receiver.
   204     arguments := aMessageNode arguments collect: [:argument | self visitNode: argument ].
   236     arguments := aMessageNode arguments collect: [:argument | self visitNode: argument ].
   205     methodFunction := aMessageNode binding asLLVMValueInModule: context module.  
   237     methodFunction := aMessageNode binding asLLVMValueInModule: context llvmModule.  
   206 
   238 
   207     ^ asm call: methodFunction _: { receiver } , arguments
   239     ^ asm call: methodFunction _: { receiver } , arguments
   208 
   240 
   209     "Created: / 31-08-2015 / 10:13:51 / Jan Vrany <jan.vrany@fit.cvut.cz>"
   241     "Created: / 31-08-2015 / 10:13:51 / Jan Vrany <jan.vrany@fit.cvut.cz>"
   210     "Modified (format): / 15-09-2015 / 07:14:45 / Jan Vrany <jan.vrany@fit.cvut.cz>"
   242     "Modified: / 24-09-2015 / 16:17:54 / Jan Vrany <jan.vrany@fit.cvut.cz>"
   211 !
   243 !
   212 
   244 
   213 acceptMethodNode: aMethodNode 
   245 acceptMethodNode: aMethodNode 
   214     | allocas entry |
   246     | allocas entry |
   215 
   247 
   216     function := aMethodNode binding asLLVMValueInModule: context module.
   248     function := aMethodNode binding asLLVMValueInModule: context llvmModule.
   217     allocas := function addBasicBlockNamed: 'allocas'.
   249     allocas := function addBasicBlockNamed: 'allocas'.
   218     entry := function addBasicBlockNamed: 'entry'.
   250     entry := function addBasicBlockNamed: 'entry'.
   219     aMethodNode scope llvmAllocas: allocas.
   251     aMethodNode scope llvmAllocas: allocas.
   220     asm := entry builder.
   252     asm := entry builder.
   221     super acceptMethodNode: aMethodNode.
   253     super acceptMethodNode: aMethodNode.
   222     "/ Finally, link allocas to entry.
   254     "/ Finally, link allocas to entry.
   223     asm block: allocas.
   255     asm block: allocas.
   224     asm br: entry.
   256     asm br: entry.
   225 
   257 
   226     "Created: / 31-08-2015 / 09:42:58 / Jan Vrany <jan.vrany@fit.cvut.cz>"
   258     "Created: / 31-08-2015 / 09:42:58 / Jan Vrany <jan.vrany@fit.cvut.cz>"
   227     "Modified: / 23-09-2015 / 22:32:04 / Jan Vrany <jan.vrany@fit.cvut.cz>"
   259     "Modified: / 24-09-2015 / 16:17:59 / Jan Vrany <jan.vrany@fit.cvut.cz>"
   228 !
   260 !
   229 
   261 
   230 acceptOptimizedNode: anOptimizedNode 
   262 acceptOptimizedNode: anOptimizedNode 
   231     self notYetImplemented
   263     self notYetImplemented
   232 
   264 
   233     "Created: / 31-08-2015 / 10:13:46 / Jan Vrany <jan.vrany@fit.cvut.cz>"
   265     "Created: / 31-08-2015 / 10:13:46 / Jan Vrany <jan.vrany@fit.cvut.cz>"
   234 !
   266 !
   235 
   267 
   236 acceptPragmaNode: aPragmaNode
   268 acceptPragmaNode: aPragmaNode
   237     self notYetImplemented
   269     aPragmaNode selector = 'primitive:' ifTrue:[ 
       
   270         self acceptPrimitiveNode: aPragmaNode  
       
   271     ].
   238 
   272 
   239     "Created: / 31-08-2015 / 10:13:41 / Jan Vrany <jan.vrany@fit.cvut.cz>"
   273     "Created: / 31-08-2015 / 10:13:41 / Jan Vrany <jan.vrany@fit.cvut.cz>"
       
   274     "Modified: / 24-09-2015 / 18:53:54 / Jan Vrany <jan.vrany@fit.cvut.cz>"
   240 !
   275 !
   241 
   276 
   242 acceptPrimitiveNode: aPragmaNode
   277 acceptPrimitiveNode: aPragmaNode
   243     | primitiveBlock methodIsForMeta emitMethodNode emitMethod |
   278     | primitiveBlock methodIsForMeta emitMethodNode emitMethod |
   244 
   279 
   254         variableNode name = 'self' ifTrue:[ 
   289         variableNode name = 'self' ifTrue:[ 
   255             variableNode name: 'zelf'.
   290             variableNode name: 'zelf'.
   256         ].
   291         ].
   257     ].
   292     ].
   258     emitMethodNode selector:(String streamContents: [ :s | emitMethodNode arguments size timesRepeat:[s nextPutAll:'_:'] ]).
   293     emitMethodNode selector:(String streamContents: [ :s | emitMethodNode arguments size timesRepeat:[s nextPutAll:'_:'] ]).
   259     emitMethod := Compiler compile: emitMethodNode formattedCode forClass: UndefinedObject install: false.
   294     emitMethod := Compiler compile: emitMethodNode formattedCode forClass: self class install: false.
   260     emitMethod
   295     emitMethod
   261         valueWithReceiver: nil
   296         valueWithReceiver: nil
   262         arguments: { asm } , ((1 to: function numArgs) collect: [ :i | function parameterAt: i ])
   297         arguments: { asm } , ((1 to: function numArgs) collect: [ :i | function parameterAt: i ])
   263 
   298 
   264     "Created: / 22-09-2015 / 18:03:33 / Jan Vrany <jan.vrany@fit.cvut.cz>"
   299     "Created: / 22-09-2015 / 18:03:33 / Jan Vrany <jan.vrany@fit.cvut.cz>"
   265     "Modified: / 23-09-2015 / 06:27:56 / Jan Vrany <jan.vrany@fit.cvut.cz>"
   300     "Modified: / 24-09-2015 / 18:58:42 / Jan Vrany <jan.vrany@fit.cvut.cz>"
   266 !
   301 !
   267 
   302 
   268 acceptReturnNode: aReturnNode 
   303 acceptReturnNode: aReturnNode 
   269     | value |
   304     | value |
   270 
   305