diff -r 146527d8dd66 -r c2e287d54de5 LLVMExamples.st --- 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 " "Modified: / 17-08-2015 / 07:39:41 / Jan Vrany " + "Modified (comment): / 21-04-2016 / 21:37:16 / Jan Vrany " +! + +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 " + "Modified: / 22-04-2016 / 09:10:21 / Jan Vrany " ! 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 " "Modified: / 17-08-2015 / 07:40:02 / Jan Vrany " + "Modified (comment): / 21-04-2016 / 21:37:25 / Jan Vrany " ! 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 " "Modified: / 06-02-2016 / 15:28:25 / Jan Vrany " + "Modified (comment): / 21-04-2016 / 21:37:35 / Jan Vrany " ! example8_data_at_fixed_address