LLVMExamples.st
changeset 62 2936ec426df6
parent 61 c2e287d54de5
child 70 ced2a5c16e70
equal deleted inserted replaced
61:c2e287d54de5 62:2936ec426df6
   226     "
   226     "
   227     LLVMExamples example3_cond
   227     LLVMExamples example3_cond
   228     "
   228     "
   229 
   229 
   230     "Created: / 08-08-2015 / 04:16:25 / Jan Vrany <jan.vrany@fit.cvut.cz>"
   230     "Created: / 08-08-2015 / 04:16:25 / Jan Vrany <jan.vrany@fit.cvut.cz>"
       
   231 !
       
   232 
       
   233 example4_cond_easy
       
   234     "
       
   235     Creates a function @even(intpr) which returns
       
   236     1 if the parameter is even or 0 if not.
       
   237 
       
   238     This demonstrate usage of blocks with
       
   239     LLVMIRBuilder>>if:then:else: which is easier to use
       
   240     than fiddling about basic blocks manually.
       
   241     "    
       
   242 
       
   243     | module 
       
   244      functionType function asm isOdd jit externalFunction |
       
   245 
       
   246     module := LLVMModule newWithName: testSelector.
       
   247 
       
   248     functionType := LLVMType function: { LLVMType intptr } returning: LLVMType intptr.
       
   249     function := module addFunctionNamed: 'even' type: functionType.
       
   250 
       
   251     asm := function builder.
       
   252     isOdd := asm icmp: (asm and: (function parameterAt: 1) _: (LLVMConstant uintptr: 1)) _: (LLVMConstant uintptr: 1) cond: LLVMIntEQ.
       
   253     asm if: isOdd then: [ 
       
   254         asm ret: (LLVMConstant uintptr: 0).
       
   255     ] else: [ 
       
   256         asm ret: (LLVMConstant uintptr: 1).
       
   257     ].
       
   258 
       
   259     jit := LLVMExecutionEngine newForModule: module.
       
   260     externalFunction := jit externalOfFunction: function.
       
   261 
       
   262     self assert: (externalFunction callWith: 10) == 1.
       
   263     self assert: (externalFunction callWith: 11) == 0.
       
   264 
       
   265     "
       
   266     LLVMExamples example3_cond
       
   267     "
       
   268 
       
   269     "Created: / 22-04-2016 / 10:22:05 / Jan Vrany <jan.vrany@fit.cvut.cz>"
   231 !
   270 !
   232 
   271 
   233 example5_factorial
   272 example5_factorial
   234     "A simple factorial using iterative algorithm.
   273     "A simple factorial using iterative algorithm.
   235      No overflow or negative value checks"
   274      No overflow or negative value checks"