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::aarch64);
33 SupportedArchs.push_back(Triple::arm);
34 SupportedArchs.push_back(Triple::mips);
35 SupportedArchs.push_back(Triple::x86);
36 SupportedArchs.push_back(Triple::x86_64);
38 // Some architectures have sub-architectures in which tests will fail, like
39 // ARM. These two vectors will define if they do have sub-archs (to avoid
40 // extra work for those who don't), and if so, if they are listed to work
41 HasSubArchs.push_back(Triple::arm);
42 SupportedSubArchs.push_back("armv6");
43 SupportedSubArchs.push_back("armv7");
45 // The operating systems below are known to be sufficiently incompatible
46 // that they will fail the MCJIT C API tests.
47 UnsupportedOSs.push_back(Triple::Cygwin);
51 TEST_F(MCJITCAPITest, simple_function) {
52 SKIP_UNSUPPORTED_PLATFORM;
56 // Creates a function that returns 42, compiles it, and runs it.
58 LLVMModuleRef module = LLVMModuleCreateWithName("simple_module");
60 LLVMSetTarget(module, HostTriple.c_str());
62 LLVMValueRef function = LLVMAddFunction(
63 module, "simple_function", LLVMFunctionType(LLVMInt32Type(), 0, 0, 0));
64 LLVMSetFunctionCallConv(function, LLVMCCallConv);
66 LLVMBasicBlockRef entry = LLVMAppendBasicBlock(function, "entry");
67 LLVMBuilderRef builder = LLVMCreateBuilder();
68 LLVMPositionBuilderAtEnd(builder, entry);
69 LLVMBuildRet(builder, LLVMConstInt(LLVMInt32Type(), 42, 0));
71 LLVMVerifyModule(module, LLVMAbortProcessAction, &error);
72 LLVMDisposeMessage(error);
74 LLVMDisposeBuilder(builder);
76 LLVMMCJITCompilerOptions options;
77 LLVMInitializeMCJITCompilerOptions(&options, sizeof(options));
80 // Just ensure that this field still exists.
81 options.NoFramePointerElim = false;
83 LLVMExecutionEngineRef engine;
85 0, LLVMCreateMCJITCompilerForModule(&engine, module, &options,
86 sizeof(options), &error));
88 LLVMPassManagerRef pass = LLVMCreatePassManager();
89 LLVMAddTargetData(LLVMGetExecutionEngineTargetData(engine), pass);
90 LLVMAddConstantPropagationPass(pass);
91 LLVMAddInstructionCombiningPass(pass);
92 LLVMRunPassManager(pass, module);
93 LLVMDisposePassManager(pass);
99 functionPointer.raw = LLVMGetPointerToGlobal(engine, function);
101 EXPECT_EQ(42, functionPointer.usable());
103 LLVMDisposeExecutionEngine(engine);