Exposing MCJIT through C API
[oota-llvm.git] / unittests / ExecutionEngine / MCJIT / MCJITCAPITest.cpp
1 //===- MCJITTest.cpp - Unit tests for the MCJIT ---------------------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This test suite verifies basic MCJIT functionality when invoked form the C
11 // API.
12 //
13 //===----------------------------------------------------------------------===//
14
15 #include "llvm-c/Analysis.h"
16 #include "llvm-c/Core.h"
17 #include "llvm-c/ExecutionEngine.h"
18 #include "llvm-c/Target.h"
19 #include "llvm-c/Transforms/Scalar.h"
20 #include "llvm/Support/Host.h"
21 #include "MCJITTestAPICommon.h"
22 #include "gtest/gtest.h"
23
24 using namespace llvm;
25
26 class MCJITCAPITest : public testing::Test, public MCJITTestAPICommon {
27 protected:
28   MCJITCAPITest() {
29     // The architectures below are known to be compatible with MCJIT as they
30     // are copied from test/ExecutionEngine/MCJIT/lit.local.cfg and should be
31     // kept in sync.
32     SupportedArchs.push_back(Triple::arm);
33     SupportedArchs.push_back(Triple::mips);
34     SupportedArchs.push_back(Triple::x86);
35     SupportedArchs.push_back(Triple::x86_64);
36
37     // The operating systems below are known to be sufficiently incompatible
38     // that they will fail the MCJIT C API tests.
39     UnsupportedOSs.push_back(Triple::Cygwin);
40   }
41 };
42
43 TEST_F(MCJITCAPITest, simple_function) {
44   SKIP_UNSUPPORTED_PLATFORM;
45   
46   char *error = 0;
47   
48   // Creates a function that returns 42, compiles it, and runs it.
49   
50   LLVMModuleRef module = LLVMModuleCreateWithName("simple_module");
51   
52   LLVMValueRef function = LLVMAddFunction(
53     module, "simple_function", LLVMFunctionType(LLVMInt32Type(), 0, 0, 0));
54   LLVMSetFunctionCallConv(function, LLVMCCallConv);
55   
56   LLVMBasicBlockRef entry = LLVMAppendBasicBlock(function, "entry");
57   LLVMBuilderRef builder = LLVMCreateBuilder();
58   LLVMPositionBuilderAtEnd(builder, entry);
59   LLVMBuildRet(builder, LLVMConstInt(LLVMInt32Type(), 42, 0));
60   
61   LLVMVerifyModule(module, LLVMAbortProcessAction, &error);
62   LLVMDisposeMessage(error);
63   
64   LLVMDisposeBuilder(builder);
65   
66   LLVMMCJITCompilerOptions options;
67   memset(&options, 0, sizeof(options));
68   options.OptLevel = 2;
69   options.NoFramePointerElim = false; // Just ensure that this field still exists.
70   
71   LLVMExecutionEngineRef engine;
72   ASSERT_EQ(
73     0, LLVMCreateMCJITCompilerForModule(&engine, module, &options, sizeof(options),
74                                         &error));
75   
76   LLVMPassManagerRef pass = LLVMCreatePassManager();
77   LLVMAddTargetData(LLVMGetExecutionEngineTargetData(engine), pass);
78   LLVMAddConstantPropagationPass(pass);
79   LLVMAddInstructionCombiningPass(pass);
80   LLVMRunPassManager(pass, module);
81   LLVMDisposePassManager(pass);
82   
83   union {
84     void *raw;
85     int (*usable)();
86   } functionPointer;
87   functionPointer.raw = LLVMGetPointerToGlobal(engine, function);
88   
89   EXPECT_EQ(42, functionPointer.usable());
90   
91   LLVMDisposeExecutionEngine(engine);
92 }
93