In this example, we illustrate how to generate a routine via LLVM which calls a routine that exists in the R executable. This shows how we can make use of existing functions.
library(Rllvm) InitializeNativeTarget() mod = Module("dotC")
Now we create the routine.
fun = Function("tossCoin", Int32Type, module = mod)
b = Block(fun, "entry") head = Block(fun) tail = Block(fun) ret = Block(fun) ir = IRBuilder(b)
zero = createIntegerConstant( 0L) one = createIntegerConstant( 1L) ans = createLocalVariable(ir, Int32Type, "ans") runif = Function("runif", DoubleType, module = mod) setLinkage(runif, ExternalLinkage) toss = ir$createCall(runif) point5 = createDoubleConstant(0.5) gt = ir$createFCmp(FCMP_ULT, toss, point5) ir$createCondBr(gt, head, tail)
ir$setInsertPoint(head) ir$createStore(one, ans) ir$createBr(ret)
ir$setInsertPoint(tail) ir$createStore(zero, ans) ir$createBr(ret)
ir$setInsertPoint(ret) ans = ir$createLoad(ans) ir$createRet(ans)
llvmAddSymbol(runif = getNativeSymbolInfo("runif")$address) print(run(fun))
llvmLoadDLL(system.file("libs", "Rllvm.so", package = "Rllvm")) print(run(fun))