1 //===- executionengine_test.go - Tests for executionengine ----------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file tests bindings for the executionengine component.
12 //===----------------------------------------------------------------------===//
20 func TestFactorial(t *testing.T) {
22 InitializeNativeTarget()
23 InitializeNativeAsmPrinter()
25 mod := NewModule("fac_module")
27 fac_args := []Type{Int32Type()}
28 fac_type := FunctionType(Int32Type(), fac_args, false)
29 fac := AddFunction(mod, "fac", fac_type)
30 fac.SetFunctionCallConv(CCallConv)
33 entry := AddBasicBlock(fac, "entry")
34 iftrue := AddBasicBlock(fac, "iftrue")
35 iffalse := AddBasicBlock(fac, "iffalse")
36 end := AddBasicBlock(fac, "end")
38 builder := NewBuilder()
39 defer builder.Dispose()
41 builder.SetInsertPointAtEnd(entry)
42 If := builder.CreateICmp(IntEQ, n, ConstInt(Int32Type(), 0, false), "cmptmp")
43 builder.CreateCondBr(If, iftrue, iffalse)
45 builder.SetInsertPointAtEnd(iftrue)
46 res_iftrue := ConstInt(Int32Type(), 1, false)
49 builder.SetInsertPointAtEnd(iffalse)
50 n_minus := builder.CreateSub(n, ConstInt(Int32Type(), 1, false), "subtmp")
51 call_fac_args := []Value{n_minus}
52 call_fac := builder.CreateCall(fac, call_fac_args, "calltmp")
53 res_iffalse := builder.CreateMul(n, call_fac, "multmp")
56 builder.SetInsertPointAtEnd(end)
57 res := builder.CreatePHI(Int32Type(), "result")
58 phi_vals := []Value{res_iftrue, res_iffalse}
59 phi_blocks := []BasicBlock{iftrue, iffalse}
60 res.AddIncoming(phi_vals, phi_blocks)
61 builder.CreateRet(res)
63 err := VerifyModule(mod, ReturnStatusAction)
65 t.Errorf("Error verifying module: %s", err)
69 options := NewMCJITCompilerOptions()
70 options.SetMCJITOptimizationLevel(2)
71 engine, err := NewMCJITCompiler(mod, options)
73 t.Errorf("Error creating JIT: %s", err)
76 defer engine.Dispose()
78 pass := NewPassManager()
81 pass.Add(engine.TargetData())
82 pass.AddConstantPropagationPass()
83 pass.AddInstructionCombiningPass()
84 pass.AddPromoteMemoryToRegisterPass()
86 pass.AddCFGSimplificationPass()
89 exec_args := []GenericValue{NewGenericValueFromInt(Int32Type(), 10, false)}
90 exec_res := engine.RunFunction(fac, exec_args)
91 var fac10 uint64 = 10 * 9 * 8 * 7 * 6 * 5 * 4 * 3 * 2 * 1
92 if exec_res.Int(false) != fac10 {
93 t.Errorf("Expected %d, got %d", fac10, exec_res.Int(false))