LLVMExamples.st
changeset 61 c2e287d54de5
parent 57 4ca7c3a327a2
child 62 2936ec426df6
--- a/LLVMExamples.st	Thu Apr 21 09:50:02 2016 +0100
+++ b/LLVMExamples.st	Thu Apr 21 22:17:02 2016 +0100
@@ -231,7 +231,7 @@
 !
 
 example5_factorial
-    "A simple factorial using recursive algorithm.
+    "A simple factorial using iterative algorithm.
      No overflow or negative value checks"
     
     | module functionType function asm  
@@ -306,11 +306,67 @@
 
     "Created: / 10-08-2015 / 09:46:29 / Jan Vrany <jan.vrany@fit.cvut.cz>"
     "Modified: / 17-08-2015 / 07:39:41 / Jan Vrany <jan.vrany@fit.cvut.cz>"
+    "Modified (comment): / 21-04-2016 / 21:37:16 / Jan Vrany <jan.vrany@fit.cvut.cz>"
+!
+
+example5_factorial_phi
+    "A simple factorial using recursive algorithm.
+     No overflow or negative value checks. 
+     Demonstrates usage of phi node"
+    
+    | module functionType function asm  
+    "Values"       paramIsOneVal paramIsNotOneVal
+    "Blocks" entry paramIsOne    paramIsNotOne exit jit externalFunction |
+
+    module := LLVMModule newWithName:testSelector.
+    functionType := LLVMType function:{ LLVMType intptr } returning:LLVMType intptr.
+    function := module addFunctionNamed:'factorial' type:functionType.
+    asm := LLVMIRBuilder new.
+    entry := function entry.
+    paramIsOne := function addBasicBlockNamed:'paramIsOne'.
+    paramIsNotOne := function addBasicBlockNamed:'paramIsNotOne'.
+    exit := function addBasicBlockNamed:'exit'.
+     
+    "/ Generate function setup
+    "/ 
+    "/ 03   function f(v) {
+    "/ 04    return (v == 1
+    "/ 05             ?  1
+    "/ 07             :  v * f(v - 1)
+    "/ 08    )
+    
+    asm block:entry.
+    asm if: (asm icmp:(function parameterAt: 1) _:(LLVMConstant sintptr:1) cond:LLVMIntEQ)
+      then: paramIsOne
+      else: paramIsNotOne.
+
+    asm block: paramIsOne.
+    paramIsOneVal := LLVMConstant sintptr: 1.
+    asm br: exit.
+    asm block: paramIsNotOne.
+    paramIsNotOneVal := asm mul: (asm call: function _: { asm sub: (function parameterAt: 1) _: (LLVMConstant sintptr: 1) })
+                              _: (function parameterAt: 1).           
+    asm br: exit.
+    asm block: exit.
+    asm ret: (asm phi: { paramIsOne -> paramIsOneVal . paramIsNotOneVal }).
+
+
+    jit := LLVMExecutionEngine newForModule:module.
+    externalFunction := jit externalOfFunction:function.
+    self assert:(externalFunction callWith:5) == 120.
+    self assert:(externalFunction callWith:1) == 1.
+
+    "
+        LLVMExamples run: example5_factorial_with_debug_info
+    "
+
+    "Created: / 21-04-2016 / 22:14:30 / Jan Vrany <jan.vrany@fit.cvut.cz>"
+    "Modified: / 22-04-2016 / 09:10:21 / Jan Vrany <jan.vrany@fit.cvut.cz>"
 !
 
 example6_factorial_with_overflow
     "
-     Simple factorial using recursive algorithm.
+     Simple factorial using iterative algorithm.
      This one checks for overflow, if overflow happens,
      return -1"
     
@@ -411,10 +467,11 @@
 
     "Created: / 14-08-2015 / 06:46:31 / Jan Vrany <jan.vrany@fit.cvut.cz>"
     "Modified: / 17-08-2015 / 07:40:02 / Jan Vrany <jan.vrany@fit.cvut.cz>"
+    "Modified (comment): / 21-04-2016 / 21:37:25 / Jan Vrany <jan.vrany@fit.cvut.cz>"
 !
 
 example7_factorial_with_debug_info
-    "A simple factorial using recursive algorithm
+    "A simple factorial using iterative algorithm
      with debug info attached. 
     "     
 
@@ -562,6 +619,7 @@
 
     "Created: / 14-08-2015 / 06:46:39 / Jan Vrany <jan.vrany@fit.cvut.cz>"
     "Modified: / 06-02-2016 / 15:28:25 / Jan Vrany <jan.vrany@fit.cvut.cz>"
+    "Modified (comment): / 21-04-2016 / 21:37:35 / Jan Vrany <jan.vrany@fit.cvut.cz>"
 !
 
 example8_data_at_fixed_address