Fix MCJITCAPITest.cpp unit test on Windows.
[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   LLVMSetTarget(module, HostTriple.c_str());
53   
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;
69   LLVMInitializeMCJITCompilerOptions(&options, sizeof(options));
70   options.OptLevel = 2;
71   
72   // Just ensure that this field still exists.
73   options.NoFramePointerElim = false;
74   
75   LLVMExecutionEngineRef engine;
76   ASSERT_EQ(
77     0, LLVMCreateMCJITCompilerForModule(&engine, module, &options,
78                                         sizeof(options), &error));
79   
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