LLVMIRBuilder.st
changeset 28 97013ae2abae
parent 24 7e7ddd55174c
child 33 feabf14b6c1d
equal deleted inserted replaced
27:b26354bbff25 28:97013ae2abae
       
     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 "
       
    14 "{ Package: 'jv:llvm_s' }"
       
    15 
       
    16 "{ NameSpace: Smalltalk }"
       
    17 
       
    18 LLVMDisposableObject subclass:#LLVMIRBuilder
       
    19 	instanceVariableNames:''
       
    20 	classVariableNames:''
       
    21 	poolDictionaries:'LLVMIntPredicate LLVMRealPredicate LLVMTypeKind'
       
    22 	category:'LLVM-S-Core'
       
    23 !
       
    24 
       
    25 !LLVMIRBuilder class methodsFor:'documentation'!
       
    26 
       
    27 copyright
       
    28 "
       
    29     Copyright (C) 2015-now Jan Vrany
       
    30 
       
    31     This code is not an open-source (yet). You may use this code
       
    32     for your own experiments and projects, given that:
       
    33 
       
    34     * all modification to the code will be sent to the
       
    35       original author for inclusion in future releases
       
    36     * this is not used in any commercial software
       
    37 
       
    38     This license is provisional and may (will) change in
       
    39     a future.
       
    40 "
       
    41 ! !
       
    42 
       
    43 !LLVMIRBuilder class methodsFor:'instance creation'!
       
    44 
       
    45 new
       
    46     ^ LLVM CreateBuilder
       
    47 
       
    48     "Created: / 07-07-2015 / 22:38:15 / Jan Vrany <jan.vrany@fit.cvut.cz>"
       
    49 ! !
       
    50 
       
    51 !LLVMIRBuilder class methodsFor:'generators'!
       
    52 
       
    53 instructions
       
    54     ^ #(
       
    55         add:to: (isIntegerOrVector isIntegerOrVector)
       
    56         lsrh:by: (isIntegerOrVector isIntegerOrVector)
       
    57     )
       
    58 
       
    59     "Created: / 11-07-2015 / 13:05:29 / Jan Vrany <jan.vrany@fit.cvut.cz>"
       
    60 ! !
       
    61 
       
    62 !LLVMIRBuilder methodsFor:'accessing'!
       
    63 
       
    64 block: anLLVMBasicBlock
       
    65     "Sets the 'current' basic block to `anLLVMBasicBlock` and
       
    66      position to it's end so that instructions will be generated
       
    67      at the end of the block."
       
    68 
       
    69     ^ self positionAtEnd: anLLVMBasicBlock
       
    70 
       
    71     "Created: / 10-08-2015 / 09:03:12 / Jan Vrany <jan.vrany@fit.cvut.cz>"
       
    72 ! !
       
    73 
       
    74 !LLVMIRBuilder methodsFor:'initialization & release'!
       
    75 
       
    76 dispose
       
    77     ^ LLVM DisposeBuilder: self.
       
    78 
       
    79     "Modified (comment): / 08-07-2015 / 22:39:43 / Jan Vrany <jan.vrany@fit.cvut.cz>"
       
    80 ! !
       
    81 
       
    82 !LLVMIRBuilder methodsFor:'instructions - aggregates'!
       
    83 
       
    84 extractvalue: value at: index
       
    85     ^ self extractvalue: value at: index as: ''
       
    86 
       
    87     "Created: / 10-08-2015 / 17:38:44 / Jan Vrany <jan.vrany@fit.cvut.cz>"
       
    88 !
       
    89 
       
    90 extractvalue: value at: index as: name
       
    91 
       
    92 
       
    93     self assertIsValue: value.
       
    94     self assert: ((value type kind == LLVMStructTypeKind) or:[ value type kind == LLVMArrayTypeKind ]) message: 'value is not a struct or an array'.
       
    95     self assert: index isInteger message: 'index is not an integer'.
       
    96 
       
    97     ^ LLVM BuildExtractValue: self _: value _: index _: name.
       
    98 
       
    99 
       
   100     "Created: / 10-08-2015 / 17:39:25 / Jan Vrany <jan.vrany@fit.cvut.cz>"
       
   101 ! !
       
   102 
       
   103 !LLVMIRBuilder methodsFor:'instructions - binary'!
       
   104 
       
   105 add:value1 _:value2 
       
   106     ^ self add:value1 _:value2 as:''
       
   107 
       
   108     "Created: / 07-07-2015 / 22:52:39 / Jan Vrany <jan.vrany@fit.cvut.cz>"
       
   109     "Modified (format): / 10-08-2015 / 09:42:20 / Jan Vrany <jan.vrany@fit.cvut.cz>"
       
   110 !
       
   111 
       
   112 add:value1 _:value2 as:name 
       
   113     self assertIsIntegerOrIntegerVectorValue: value1.  
       
   114     self assertIsIntegerOrIntegerVectorValue: value2.
       
   115     self assertIsValueOfSameType: value1  as: value2. 
       
   116     self assertIsString: name.  
       
   117 
       
   118     ^ LLVM BuildAdd:self _:value1 _:value2 _:name
       
   119 
       
   120     "Created: / 07-07-2015 / 22:52:39 / Jan Vrany <jan.vrany@fit.cvut.cz>"
       
   121     "Modified: / 10-08-2015 / 09:41:47 / Jan Vrany <jan.vrany@fit.cvut.cz>"
       
   122 !
       
   123 
       
   124 and:value1 _:value2 
       
   125     ^ self and:value1 _:value2 as:''
       
   126 
       
   127     "Created: / 07-08-2015 / 16:51:53 / Jan Vrany <jan.vrany@fit.cvut.cz>"
       
   128     "Modified: / 07-08-2015 / 17:56:21 / Jan Vrany <jan.vrany@fit.cvut.cz>"
       
   129 !
       
   130 
       
   131 and:value1 _:value2 as: name
       
   132     self assert: (value1 isKindOf: LLVMValue).
       
   133     self assert: value1 isIntegerOrIntegerVectorValue.
       
   134     self assert: (value2 isKindOf: LLVMValue).
       
   135     self assert: value2 isIntegerOrIntegerVectorValue.
       
   136     self assert: (name isSingleByteString).
       
   137     ^LLVM BuildAnd: self  _: value1 _: value2 _: name
       
   138 
       
   139     "Created: / 07-08-2015 / 17:56:42 / Jan Vrany <jan.vrany@fit.cvut.cz>"
       
   140 !
       
   141 
       
   142 ashr:value1 _:value2 
       
   143     ^ self 
       
   144             ashr:value1
       
   145             _:value2
       
   146             as:''
       
   147 
       
   148     "Created: / 11-07-2015 / 16:46:53 / Jan Vrany <jan.vrany@fit.cvut.cz>"
       
   149 !
       
   150 
       
   151 ashr:value1 _:value2 as:name 
       
   152     self assert:(value1 isKindOf:LLVMValue).
       
   153     self assert:value1 isIntegerOrIntegerVectorValue.
       
   154     self assert:(value2 isKindOf:LLVMValue).
       
   155     self assert:value2 isIntegerOrIntegerVectorValue.
       
   156     self assert:(name isSingleByteString).
       
   157     ^ LLVM 
       
   158         BuildAShr:self
       
   159         _:value1
       
   160         _:value2
       
   161         _:name
       
   162 
       
   163     "Created: / 11-07-2015 / 16:46:43 / Jan Vrany <jan.vrany@fit.cvut.cz>"
       
   164 !
       
   165 
       
   166 lshr:value1 _:value2 
       
   167     ^ self 
       
   168             lshr:value1
       
   169             _:value2
       
   170             as:''
       
   171 
       
   172     "Created: / 11-07-2015 / 13:02:47 / Jan Vrany <jan.vrany@fit.cvut.cz>"
       
   173 !
       
   174 
       
   175 lshr:value1 _:value2 as:name 
       
   176     self assert:(value1 isKindOf:LLVMValue).
       
   177     self assert:value1 isIntegerOrIntegerVectorValue.
       
   178     self assert:(value2 isKindOf:LLVMValue).
       
   179     self assert:value2 isIntegerOrIntegerVectorValue.
       
   180     self assert:(name isSingleByteString).
       
   181     ^ LLVM 
       
   182         BuildLShr:self
       
   183         _:value1
       
   184         _:value2
       
   185         _:name
       
   186 
       
   187     "Created: / 11-07-2015 / 14:49:13 / Jan Vrany <jan.vrany@fit.cvut.cz>"
       
   188 !
       
   189 
       
   190 mul:value1 _:value2 
       
   191     ^ self mul:value1 _:value2 as:''
       
   192 
       
   193     "Created: / 10-08-2015 / 09:42:32 / Jan Vrany <jan.vrany@fit.cvut.cz>"
       
   194 !
       
   195 
       
   196 mul:value1 _:value2 as:name 
       
   197     self assertIsIntegerOrIntegerVectorValue: value1.  
       
   198     self assertIsIntegerOrIntegerVectorValue: value2.
       
   199     self assertIsValueOfSameType: value1  as: value2. 
       
   200     self assertIsString: name.  
       
   201 
       
   202     ^ LLVM BuildMul:self _:value1 _:value2 _:name
       
   203 
       
   204     "Created: / 10-08-2015 / 09:41:56 / Jan Vrany <jan.vrany@fit.cvut.cz>"
       
   205 !
       
   206 
       
   207 or:value1 _:value2 
       
   208     ^ self 
       
   209             or:value1
       
   210             _:value2
       
   211             as:''
       
   212 
       
   213     "Created: / 11-07-2015 / 17:17:07 / Jan Vrany <jan.vrany@fit.cvut.cz>"
       
   214 !
       
   215 
       
   216 or:value1 _:value2 as:name 
       
   217     self assert:(value1 isKindOf:LLVMValue).
       
   218     self assert:value1 isIntegerOrIntegerVectorValue.
       
   219     self assert:(value2 isKindOf:LLVMValue).
       
   220     self assert:value2 isIntegerOrIntegerVectorValue.
       
   221     self assert:(name isSingleByteString).
       
   222     ^ LLVM 
       
   223         BuildOr:self
       
   224         _:value1
       
   225         _:value2
       
   226         _:name
       
   227 
       
   228     "Created: / 11-07-2015 / 17:16:44 / Jan Vrany <jan.vrany@fit.cvut.cz>"
       
   229 !
       
   230 
       
   231 shl:value1 _:value2
       
   232     ^ self shl:value1 _:value2 as:''
       
   233 
       
   234     "Created: / 11-07-2015 / 16:37:17 / Jan Vrany <jan.vrany@fit.cvut.cz>"
       
   235     "Modified (format): / 07-08-2015 / 17:58:34 / Jan Vrany <jan.vrany@fit.cvut.cz>"
       
   236 !
       
   237 
       
   238 shl:value1 _:value2 as:name 
       
   239     self assert:(value1 isKindOf:LLVMValue).
       
   240     self assert:value1 isIntegerOrIntegerVectorValue.
       
   241     self assert:(value2 isKindOf:LLVMValue).
       
   242     self assert:value2 isIntegerOrIntegerVectorValue.
       
   243     self assert:(name isSingleByteString).
       
   244     ^ LLVM 
       
   245         BuildShl:self
       
   246         _:value1
       
   247         _:value2
       
   248         _:name
       
   249 
       
   250     "Created: / 11-07-2015 / 16:37:05 / Jan Vrany <jan.vrany@fit.cvut.cz>"
       
   251 !
       
   252 
       
   253 sub:value1 _:value2 
       
   254     ^ self sub:value1 _:value2 as:''
       
   255 
       
   256     "Created: / 10-08-2015 / 09:42:25 / Jan Vrany <jan.vrany@fit.cvut.cz>"
       
   257 !
       
   258 
       
   259 sub:value1 _:value2 as:name 
       
   260     self assertIsIntegerOrIntegerVectorValue: value1.  
       
   261     self assertIsIntegerOrIntegerVectorValue: value2.
       
   262     self assertIsValueOfSameType: value1  as: value2. 
       
   263     self assertIsString: name.  
       
   264 
       
   265     ^ LLVM BuildSub:self _:value1 _:value2 _:name
       
   266 
       
   267     "Created: / 10-08-2015 / 09:42:04 / Jan Vrany <jan.vrany@fit.cvut.cz>"
       
   268 ! !
       
   269 
       
   270 !LLVMIRBuilder methodsFor:'instructions - binary - compare'!
       
   271 
       
   272 icmp:value1 _:value2 cond: cond
       
   273     ^ self icmp:value1 _:value2 cond: cond as: ''
       
   274 
       
   275     "Created: / 07-08-2015 / 18:39:45 / Jan Vrany <jan.vrany@fit.cvut.cz>"
       
   276 !
       
   277 
       
   278 icmp:value1 _:value2 cond: cond as: name
       
   279 
       
   280     self assertIsIntegerOrIntegerVectorValue:value1.      
       
   281     self assertIsIntegerOrIntegerVectorValue:value2.      
       
   282     self assertIsValueOfSameType:value1 as:value2.
       
   283     self assertIsString:name.      
       
   284     ^ LLVM BuildICmp: self  _: cond _:  value1 _: value2 _: name
       
   285 
       
   286     "Created: / 07-08-2015 / 18:18:39 / Jan Vrany <jan.vrany@fit.cvut.cz>"
       
   287 ! !
       
   288 
       
   289 !LLVMIRBuilder methodsFor:'instructions - memory'!
       
   290 
       
   291 alloca: type
       
   292     ^ self alloca: type as: ''
       
   293 
       
   294     "Created: / 10-08-2015 / 06:33:18 / Jan Vrany <jan.vrany@fit.cvut.cz>"
       
   295 !
       
   296 
       
   297 alloca: type as: name
       
   298     self assertIsType: type.
       
   299     self assertIsString: name.
       
   300 
       
   301     ^ LLVM BuildAlloca: self _: type _: name
       
   302 
       
   303     "Created: / 10-08-2015 / 06:26:37 / Jan Vrany <jan.vrany@fit.cvut.cz>"
       
   304 !
       
   305 
       
   306 gep: pointer at: integerOrArrayOfIntegers
       
   307     ^ self gep: pointer at: integerOrArrayOfIntegers as: ''
       
   308 
       
   309     "Created: / 05-08-2015 / 20:58:42 / Jan Vrany <jan.vrany@fit.cvut.cz>"
       
   310 !
       
   311 
       
   312 gep: pointer at: integerOrArrayOfIntegers as: name
       
   313     | indices |
       
   314 
       
   315     self assertIsValue: pointer ofKind: LLVMPointerTypeKind.  
       
   316     self assert: (integerOrArrayOfIntegers isInteger 
       
   317                     or:[ integerOrArrayOfIntegers isSequenceable and:[ integerOrArrayOfIntegers allSatisfy:[:e|e isInteger] ] ]).
       
   318     self assertIsString: name.  
       
   319     integerOrArrayOfIntegers isInteger ifTrue:[ 
       
   320         indices := LLVMObjectArray with: (LLVMConstant uint32: integerOrArrayOfIntegers)
       
   321     ] ifFalse:[ 
       
   322         indices := LLVMObjectArray new: integerOrArrayOfIntegers size.
       
   323         1 to: indices size do:[:i |
       
   324             indices at: i put: (LLVMConstant uint32: (integerOrArrayOfIntegers at: i)).
       
   325         ].
       
   326     ].
       
   327     ^ LLVM BuildGEP: self _: pointer _: indices _: indices size _: name.
       
   328 
       
   329     "Created: / 05-08-2015 / 20:58:23 / Jan Vrany <jan.vrany@fit.cvut.cz>"
       
   330     "Modified: / 10-08-2015 / 17:41:47 / Jan Vrany <jan.vrany@fit.cvut.cz>"
       
   331 !
       
   332 
       
   333 load: pointer
       
   334     ^ self load: pointer as: ''
       
   335 
       
   336     "Created: / 10-08-2015 / 06:45:29 / Jan Vrany <jan.vrany@fit.cvut.cz>"
       
   337 !
       
   338 
       
   339 load: pointer as: name
       
   340     self assertIsValue: pointer.
       
   341     self assertIsString: name.
       
   342 
       
   343     ^ LLVM BuildLoad: self _: pointer _: name
       
   344 
       
   345     "Created: / 10-08-2015 / 06:45:16 / Jan Vrany <jan.vrany@fit.cvut.cz>"
       
   346 !
       
   347 
       
   348 store: value _: pointer
       
   349     self assertIsValue: value.
       
   350     self assertIsValue: pointer.
       
   351 
       
   352     ^ LLVM BuildStore: self  _: value _: pointer
       
   353 
       
   354     "Created: / 10-08-2015 / 06:45:41 / Jan Vrany <jan.vrany@fit.cvut.cz>"
       
   355 ! !
       
   356 
       
   357 !LLVMIRBuilder methodsFor:'instructions - other'!
       
   358 
       
   359 call: function _: args
       
   360     ^ self call: function _: args as: ''.
       
   361 
       
   362     "Created: / 10-08-2015 / 18:53:42 / Jan Vrany <jan.vrany@fit.cvut.cz>"
       
   363 !
       
   364 
       
   365 call: function _: arguments as: name
       
   366     | argumentsArray argumentsSize |
       
   367 
       
   368     self assertIsFunctionValue: function.
       
   369     self assertIsValueArray: arguments.  
       
   370     self assertIsString: name.
       
   371 
       
   372     argumentsSize := arguments size.
       
   373     argumentsArray := arguments asLLVMObjectArray.
       
   374     ^ LLVM BuildCall: self _: function _: argumentsArray _: argumentsSize _: name
       
   375 
       
   376     "Created: / 10-08-2015 / 18:53:52 / Jan Vrany <jan.vrany@fit.cvut.cz>"
       
   377 ! !
       
   378 
       
   379 !LLVMIRBuilder methodsFor:'instructions - terminators'!
       
   380 
       
   381 br: target
       
   382     | targetAsValue  |
       
   383 
       
   384     targetAsValue := target asLLVMValue.
       
   385 
       
   386     self assertIsBasicBlockValue: targetAsValue.
       
   387 
       
   388     ^ LLVM BuildBr: self _: targetAsValue
       
   389 
       
   390     "Created: / 08-08-2015 / 02:59:25 / Jan Vrany <jan.vrany@fit.cvut.cz>"
       
   391 !
       
   392 
       
   393 if: cond then: then else: else
       
   394     ^ self if: cond then: then else: else as: ''
       
   395 
       
   396     "Created: / 08-08-2015 / 04:15:44 / Jan Vrany <jan.vrany@fit.cvut.cz>"
       
   397 !
       
   398 
       
   399 if: cond then: then else: else as: name
       
   400     | thenAsValue elseAsValue |
       
   401 
       
   402     thenAsValue := then asLLVMValue.
       
   403     elseAsValue := else asLLVMValue.
       
   404 
       
   405     self assertIsBasicBlockValue: thenAsValue.
       
   406     self assertIsBasicBlockValue: elseAsValue.
       
   407     self assertIsValue: cond ofType: LLVMType int1.
       
   408 
       
   409     ^ LLVM BuildCondBr: self _: cond _: thenAsValue _: elseAsValue
       
   410 
       
   411     "Created: / 07-08-2015 / 18:42:28 / Jan Vrany <jan.vrany@fit.cvut.cz>"
       
   412     "Modified: / 08-08-2015 / 02:58:39 / Jan Vrany <jan.vrany@fit.cvut.cz>"
       
   413 !
       
   414 
       
   415 ret
       
   416     ^ LLVM BuildRetVoid: self
       
   417 
       
   418     "Created: / 07-08-2015 / 18:14:37 / Jan Vrany <jan.vrany@fit.cvut.cz>"
       
   419 !
       
   420 
       
   421 ret:value1
       
   422 
       
   423     self assertIsValue: value1.
       
   424     ^ LLVM BuildRet: self _: value1
       
   425 
       
   426     "Created: / 07-07-2015 / 22:55:27 / Jan Vrany <jan.vrany@fit.cvut.cz>"
       
   427     "Modified: / 08-08-2015 / 03:11:16 / Jan Vrany <jan.vrany@fit.cvut.cz>"
       
   428 ! !
       
   429 
       
   430 !LLVMIRBuilder methodsFor:'positioning'!
       
   431 
       
   432 positionAtEnd: basicBlock
       
   433     LLVM PositionBuilderAtEnd: self  _: basicBlock
       
   434 
       
   435     "Created: / 07-07-2015 / 22:45:49 / Jan Vrany <jan.vrany@fit.cvut.cz>"
       
   436 ! !
       
   437