1 //===- MCJITTest.cpp - Unit tests for the MCJIT ---------------------------===//
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 test suite verifies basic MCJIT functionality when invoked form the C
13 //===----------------------------------------------------------------------===//
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"
26 class MCJITCAPITest : public testing::Test, public MCJITTestAPICommon {
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
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);
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);
43 TEST_F(MCJITCAPITest, simple_function) {
44 SKIP_UNSUPPORTED_PLATFORM;
48 // Creates a function that returns 42, compiles it, and runs it.
50 LLVMModuleRef module = LLVMModuleCreateWithName("simple_module");
52 LLVMSetTarget(module, HostTriple.c_str());
54 LLVMValueRef function = LLVMAddFunction(
55 module, "simple_function", LLVMFunctionType(LLVMInt32Type(), 0, 0, 0));
56 LLVMSetFunctionCallConv(function, LLVMCCallConv);
58 LLVMBasicBlockRef entry = LLVMAppendBasicBlock(function, "entry");
59 LLVMBuilderRef builder = LLVMCreateBuilder();
60 LLVMPositionBuilderAtEnd(builder, entry);
61 LLVMBuildRet(builder, LLVMConstInt(LLVMInt32Type(), 42, 0));
63 LLVMVerifyModule(module, LLVMAbortProcessAction, &error);
64 LLVMDisposeMessage(error);
66 LLVMDisposeBuilder(builder);
68 LLVMMCJITCompilerOptions options;
69 LLVMInitializeMCJITCompilerOptions(&options, sizeof(options));
72 // Just ensure that this field still exists.
73 options.NoFramePointerElim = false;
75 LLVMExecutionEngineRef engine;
77 0, LLVMCreateMCJITCompilerForModule(&engine, module, &options,
78 sizeof(options), &error));
80 LLVMPassManagerRef pass = LLVMCreatePassManager();
81 LLVMAddTargetData(LLVMGetExecutionEngineTargetData(engine), pass);
82 LLVMAddConstantPropagationPass(pass);
83 LLVMAddInstructionCombiningPass(pass);
84 LLVMRunPassManager(pass, module);
85 LLVMDisposePassManager(pass);
91 functionPointer.raw = LLVMGetPointerToGlobal(engine, function);
93 EXPECT_EQ(42, functionPointer.usable());
95 LLVMDisposeExecutionEngine(engine);