blob: 8dd5795dc554efe60f462ee25e7e9e436202e5ac [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");
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;
Filip Pizloa4fa74e2013-05-01 06:46:59 +000069
70 // Just ensure that this field still exists.
71 options.NoFramePointerElim = false;
Andrew Kaylord2755af2013-04-29 17:49:40 +000072
73 LLVMExecutionEngineRef engine;
74 ASSERT_EQ(
Filip Pizloa4fa74e2013-05-01 06:46:59 +000075 0, LLVMCreateMCJITCompilerForModule(&engine, module, &options,
76 sizeof(options), &error));
Andrew Kaylord2755af2013-04-29 17:49:40 +000077
78 LLVMPassManagerRef pass = LLVMCreatePassManager();
79 LLVMAddTargetData(LLVMGetExecutionEngineTargetData(engine), pass);
80 LLVMAddConstantPropagationPass(pass);
81 LLVMAddInstructionCombiningPass(pass);
82 LLVMRunPassManager(pass, module);
83 LLVMDisposePassManager(pass);
84
85 union {
86 void *raw;
87 int (*usable)();
88 } functionPointer;
89 functionPointer.raw = LLVMGetPointerToGlobal(engine, function);
90
91 EXPECT_EQ(42, functionPointer.usable());
92
93 LLVMDisposeExecutionEngine(engine);
94}
95