LLVMExamples.st
changeset 62 2936ec426df6
parent 61 c2e287d54de5
child 70 ced2a5c16e70
--- a/LLVMExamples.st	Thu Apr 21 22:17:02 2016 +0100
+++ b/LLVMExamples.st	Fri Apr 22 14:45:34 2016 +0100
@@ -230,6 +230,45 @@
     "Created: / 08-08-2015 / 04:16:25 / Jan Vrany <jan.vrany@fit.cvut.cz>"
 !
 
+example4_cond_easy
+    "
+    Creates a function @even(intpr) which returns
+    1 if the parameter is even or 0 if not.
+
+    This demonstrate usage of blocks with
+    LLVMIRBuilder>>if:then:else: which is easier to use
+    than fiddling about basic blocks manually.
+    "    
+
+    | module 
+     functionType function asm isOdd jit externalFunction |
+
+    module := LLVMModule newWithName: testSelector.
+
+    functionType := LLVMType function: { LLVMType intptr } returning: LLVMType intptr.
+    function := module addFunctionNamed: 'even' type: functionType.
+
+    asm := function builder.
+    isOdd := asm icmp: (asm and: (function parameterAt: 1) _: (LLVMConstant uintptr: 1)) _: (LLVMConstant uintptr: 1) cond: LLVMIntEQ.
+    asm if: isOdd then: [ 
+        asm ret: (LLVMConstant uintptr: 0).
+    ] else: [ 
+        asm ret: (LLVMConstant uintptr: 1).
+    ].
+
+    jit := LLVMExecutionEngine newForModule: module.
+    externalFunction := jit externalOfFunction: function.
+
+    self assert: (externalFunction callWith: 10) == 1.
+    self assert: (externalFunction callWith: 11) == 0.
+
+    "
+    LLVMExamples example3_cond
+    "
+
+    "Created: / 22-04-2016 / 10:22:05 / Jan Vrany <jan.vrany@fit.cvut.cz>"
+!
+
 example5_factorial
     "A simple factorial using iterative algorithm.
      No overflow or negative value checks"