From 69156b4c20249e7800cc09e0eef0beb3d15ac1ad Mon Sep 17 00:00:00 2001 From: Dimitry Andric Date: Sun, 6 Sep 2015 18:34:38 +0000 Subject: Import llvm 3.7.0 release (r246257). --- unittests/ExecutionEngine/MCJIT/MCJITCAPITest.cpp | 109 +++++++++++++--------- 1 file changed, 64 insertions(+), 45 deletions(-) (limited to 'unittests/ExecutionEngine') diff --git a/unittests/ExecutionEngine/MCJIT/MCJITCAPITest.cpp b/unittests/ExecutionEngine/MCJIT/MCJITCAPITest.cpp index c7d4dd757cf8..85cabdbd41a4 100644 --- a/unittests/ExecutionEngine/MCJIT/MCJITCAPITest.cpp +++ b/unittests/ExecutionEngine/MCJIT/MCJITCAPITest.cpp @@ -339,14 +339,11 @@ TEST_F(MCJITCAPITest, simple_function) { buildMCJITOptions(); buildMCJITEngine(); buildAndRunPasses(); - - union { - void *raw; - int (*usable)(); - } functionPointer; - functionPointer.raw = LLVMGetPointerToGlobal(Engine, Function); - - EXPECT_EQ(42, functionPointer.usable()); + + auto *functionPointer = reinterpret_cast( + reinterpret_cast(LLVMGetPointerToGlobal(Engine, Function))); + + EXPECT_EQ(42, functionPointer()); } TEST_F(MCJITCAPITest, gva) { @@ -389,14 +386,11 @@ TEST_F(MCJITCAPITest, custom_memory_manager) { useRoundTripSectionMemoryManager(); buildMCJITEngine(); buildAndRunPasses(); - - union { - void *raw; - int (*usable)(); - } functionPointer; - functionPointer.raw = LLVMGetPointerToGlobal(Engine, Function); - - EXPECT_EQ(42, functionPointer.usable()); + + auto *functionPointer = reinterpret_cast( + reinterpret_cast(LLVMGetPointerToGlobal(Engine, Function))); + + EXPECT_EQ(42, functionPointer()); EXPECT_TRUE(didCallAllocateCodeSection); } @@ -412,14 +406,11 @@ TEST_F(MCJITCAPITest, stackmap_creates_compact_unwind_on_darwin) { useRoundTripSectionMemoryManager(); buildMCJITEngine(); buildAndRunOptPasses(); - - union { - void *raw; - int (*usable)(); - } functionPointer; - functionPointer.raw = LLVMGetPointerToGlobal(Engine, Function); - - EXPECT_EQ(42, functionPointer.usable()); + + auto *functionPointer = reinterpret_cast( + reinterpret_cast(LLVMGetPointerToGlobal(Engine, Function))); + + EXPECT_EQ(42, functionPointer()); EXPECT_TRUE(didCallAllocateCodeSection); // Up to this point, the test is specific only to X86-64. But this next @@ -446,21 +437,15 @@ TEST_F(MCJITCAPITest, reserve_allocation_space) { Options.MCJMM = wrap(MM); buildMCJITEngine(); buildAndRunPasses(); - - union { - void *raw; - int (*usable)(); - } GetGlobalFct; - GetGlobalFct.raw = LLVMGetPointerToGlobal(Engine, Function); - - union { - void *raw; - void (*usable)(int); - } SetGlobalFct; - SetGlobalFct.raw = LLVMGetPointerToGlobal(Engine, Function2); - - SetGlobalFct.usable(789); - EXPECT_EQ(789, GetGlobalFct.usable()); + + auto GetGlobalFct = reinterpret_cast( + reinterpret_cast(LLVMGetPointerToGlobal(Engine, Function))); + + auto SetGlobalFct = reinterpret_cast( + reinterpret_cast(LLVMGetPointerToGlobal(Engine, Function2))); + + SetGlobalFct(789); + EXPECT_EQ(789, GetGlobalFct()); EXPECT_LE(MM->UsedCodeSize, MM->ReservedCodeSize); EXPECT_LE(MM->UsedDataSizeRO, MM->ReservedDataSizeRO); EXPECT_LE(MM->UsedDataSizeRW, MM->ReservedDataSizeRW); @@ -478,13 +463,47 @@ TEST_F(MCJITCAPITest, yield) { LLVMContextSetYieldCallback(C, yield, nullptr); buildAndRunPasses(); - union { - void *raw; - int (*usable)(); - } functionPointer; - functionPointer.raw = LLVMGetPointerToGlobal(Engine, Function); + auto *functionPointer = reinterpret_cast( + reinterpret_cast(LLVMGetPointerToGlobal(Engine, Function))); - EXPECT_EQ(42, functionPointer.usable()); + EXPECT_EQ(42, functionPointer()); EXPECT_TRUE(didCallYield); } +static int localTestFunc() { + return 42; +} + +TEST_F(MCJITCAPITest, addGlobalMapping) { + SKIP_UNSUPPORTED_PLATFORM; + + Module = LLVMModuleCreateWithName("testModule"); + LLVMSetTarget(Module, HostTriple.c_str()); + LLVMTypeRef FunctionType = LLVMFunctionType(LLVMInt32Type(), NULL, 0, 0); + LLVMValueRef MappedFn = LLVMAddFunction(Module, "mapped_fn", FunctionType); + + Function = LLVMAddFunction(Module, "test_fn", FunctionType); + LLVMBasicBlockRef Entry = LLVMAppendBasicBlock(Function, ""); + LLVMBuilderRef Builder = LLVMCreateBuilder(); + LLVMPositionBuilderAtEnd(Builder, Entry); + LLVMValueRef RetVal = LLVMBuildCall(Builder, MappedFn, NULL, 0, ""); + LLVMBuildRet(Builder, RetVal); + LLVMDisposeBuilder(Builder); + + LLVMVerifyModule(Module, LLVMAbortProcessAction, &Error); + LLVMDisposeMessage(Error); + + buildMCJITOptions(); + buildMCJITEngine(); + + LLVMAddGlobalMapping( + Engine, MappedFn, + reinterpret_cast(reinterpret_cast(&localTestFunc))); + + buildAndRunPasses(); + + uint64_t raw = LLVMGetFunctionAddress(Engine, "test_fn"); + int (*usable)() = (int (*)()) raw; + + EXPECT_EQ(42, usable()); +} -- cgit v1.2.3