blob: 07ea1afe1a85f69f1746bb7e3c05140a38d0da04 [file] [log] [blame]
Andrew Kaylord2755af2013-04-29 17:49:40 +00001//===- 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
24using namespace llvm;
25
26class MCJITCAPITest : public testing::Test, public MCJITTestAPICommon {
27protected:
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
43TEST_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");
Andrew Kaylor50be71d2013-05-10 17:58:41 +000051
52 LLVMSetTarget(module, HostTriple.c_str());
Andrew Kaylord2755af2013-04-29 17:49:40 +000053
54 LLVMValueRef function = LLVMAddFunction(
55 module, "simple_function", LLVMFunctionType(LLVMInt32Type(), 0, 0, 0));
56 LLVMSetFunctionCallConv(function, LLVMCCallConv);
57
58 LLVMBasicBlockRef entry = LLVMAppendBasicBlock(function, "entry");
59 LLVMBuilderRef builder = LLVMCreateBuilder();
60 LLVMPositionBuilderAtEnd(builder, entry);
61 LLVMBuildRet(builder, LLVMConstInt(LLVMInt32Type(), 42, 0));
62
63 LLVMVerifyModule(module, LLVMAbortProcessAction, &error);
64 LLVMDisposeMessage(error);
65
66 LLVMDisposeBuilder(builder);
67
68 LLVMMCJITCompilerOptions options;
Filip Pizlo0e1327e2013-05-01 22:58:00 +000069 LLVMInitializeMCJITCompilerOptions(&options, sizeof(options));
Andrew Kaylord2755af2013-04-29 17:49:40 +000070 options.OptLevel = 2;
Filip Pizloa4fa74e2013-05-01 06:46:59 +000071
72 // Just ensure that this field still exists.
73 options.NoFramePointerElim = false;
Andrew Kaylord2755af2013-04-29 17:49:40 +000074
75 LLVMExecutionEngineRef engine;
76 ASSERT_EQ(
Filip Pizloa4fa74e2013-05-01 06:46:59 +000077 0, LLVMCreateMCJITCompilerForModule(&engine, module, &options,
78 sizeof(options), &error));
Andrew Kaylord2755af2013-04-29 17:49:40 +000079
80 LLVMPassManagerRef pass = LLVMCreatePassManager();
81 LLVMAddTargetData(LLVMGetExecutionEngineTargetData(engine), pass);
82 LLVMAddConstantPropagationPass(pass);
83 LLVMAddInstructionCombiningPass(pass);
84 LLVMRunPassManager(pass, module);
85 LLVMDisposePassManager(pass);
86
87 union {
88 void *raw;
89 int (*usable)();
90 } functionPointer;
91 functionPointer.raw = LLVMGetPointerToGlobal(engine, function);
92
93 EXPECT_EQ(42, functionPointer.usable());
94
95 LLVMDisposeExecutionEngine(engine);
96}
97