From 7bfc61dfe3ade3762dd0cbc9dc1879c0eb9abec2 Mon Sep 17 00:00:00 2001 From: Peter Zotov Date: Mon, 22 Dec 2014 18:53:11 +0000 Subject: [PATCH] [C API] Expose LLVMGetGlobalValueAddress and LLVMGetFunctionAddress. Patch by Ramkumar Ramachandra git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@224720 91177308-0d34-0410-b5e6-96231b3b80d8 --- include/llvm-c/ExecutionEngine.h | 4 ++ .../ExecutionEngineBindings.cpp | 8 ++++ .../ExecutionEngine/MCJIT/MCJITCAPITest.cpp | 38 +++++++++++++++++++ 3 files changed, 50 insertions(+) diff --git a/include/llvm-c/ExecutionEngine.h b/include/llvm-c/ExecutionEngine.h index f1f4cadec34..eb3ecabfa8a 100644 --- a/include/llvm-c/ExecutionEngine.h +++ b/include/llvm-c/ExecutionEngine.h @@ -170,6 +170,10 @@ void LLVMAddGlobalMapping(LLVMExecutionEngineRef EE, LLVMValueRef Global, void *LLVMGetPointerToGlobal(LLVMExecutionEngineRef EE, LLVMValueRef Global); +uint64_t LLVMGetGlobalValueAddress(LLVMExecutionEngineRef EE, const char *Name); + +uint64_t LLVMGetFunctionAddress(LLVMExecutionEngineRef EE, const char *Name); + /*===-- Operations on memory managers -------------------------------------===*/ typedef uint8_t *(*LLVMMemoryManagerAllocateCodeSectionCallback)( diff --git a/lib/ExecutionEngine/ExecutionEngineBindings.cpp b/lib/ExecutionEngine/ExecutionEngineBindings.cpp index 7fc72ae19c5..aaa53f0c695 100644 --- a/lib/ExecutionEngine/ExecutionEngineBindings.cpp +++ b/lib/ExecutionEngine/ExecutionEngineBindings.cpp @@ -328,6 +328,14 @@ void *LLVMGetPointerToGlobal(LLVMExecutionEngineRef EE, LLVMValueRef Global) { return unwrap(EE)->getPointerToGlobal(unwrap(Global)); } +uint64_t LLVMGetGlobalValueAddress(LLVMExecutionEngineRef EE, const char *Name) { + return unwrap(EE)->getGlobalValueAddress(Name); +} + +uint64_t LLVMGetFunctionAddress(LLVMExecutionEngineRef EE, const char *Name) { + return unwrap(EE)->getFunctionAddress(Name); +} + /*===-- Operations on memory managers -------------------------------------===*/ namespace { diff --git a/unittests/ExecutionEngine/MCJIT/MCJITCAPITest.cpp b/unittests/ExecutionEngine/MCJIT/MCJITCAPITest.cpp index c80b88b8c2b..62967bdd327 100644 --- a/unittests/ExecutionEngine/MCJIT/MCJITCAPITest.cpp +++ b/unittests/ExecutionEngine/MCJIT/MCJITCAPITest.cpp @@ -347,6 +347,44 @@ TEST_F(MCJITCAPITest, simple_function) { EXPECT_EQ(42, functionPointer.usable()); } +TEST_F(MCJITCAPITest, gva) { + SKIP_UNSUPPORTED_PLATFORM; + + Module = LLVMModuleCreateWithName("simple_module"); + LLVMSetTarget(Module, HostTriple.c_str()); + LLVMValueRef GlobalVar = LLVMAddGlobal(Module, LLVMInt32Type(), "simple_value"); + LLVMSetInitializer(GlobalVar, LLVMConstInt(LLVMInt32Type(), 42, 0)); + + buildMCJITOptions(); + buildMCJITEngine(); + buildAndRunPasses(); + + union { + uint64_t raw; + int32_t *usable; + } valuePointer; + valuePointer.raw = LLVMGetGlobalValueAddress(Engine, "simple_value"); + + EXPECT_EQ(42, *valuePointer.usable); +} + +TEST_F(MCJITCAPITest, gfa) { + SKIP_UNSUPPORTED_PLATFORM; + + buildSimpleFunction(); + buildMCJITOptions(); + buildMCJITEngine(); + buildAndRunPasses(); + + union { + uint64_t raw; + int (*usable)(); + } functionPointer; + functionPointer.raw = LLVMGetFunctionAddress(Engine, "simple_function"); + + EXPECT_EQ(42, functionPointer.usable()); +} + TEST_F(MCJITCAPITest, custom_memory_manager) { SKIP_UNSUPPORTED_PLATFORM;