From f108d57f9b12ec176a87faa46464057d51063d65 Mon Sep 17 00:00:00 2001 From: Robert Widmann Date: Fri, 6 Apr 2018 02:31:29 +0000 Subject: [PATCH] [LLVM-C] Audit Inline Assembly APIs for Consistency Summary: - Add a missing getter for module-level inline assembly - Add a missing append function for module-level inline assembly - Deprecate LLVMSetModuleInlineAsm and replace it with LLVMSetModuleInlineAsm2 which takes an explicit length parameter - Deprecate LLVMConstInlineAsm and replace it with LLVMGetInlineAsm, a function that allows passing a dialect and is not mis-classified as a constant operation Reviewers: whitequark, deadalnix Reviewed By: whitequark Subscribers: llvm-commits Differential Revision: https://reviews.llvm.org/D45346 llvm-svn: 329369 --- llvm/include/llvm-c/Core.h | 39 +++++++++++++++++++++++++++++++++++++-- llvm/lib/IR/Core.cpp | 32 ++++++++++++++++++++++++++++++++ llvm/test/Bindings/llvm-c/echo.ll | 2 ++ llvm/tools/llvm-c-test/echo.cpp | 4 ++++ 4 files changed, 75 insertions(+), 2 deletions(-) diff --git a/llvm/include/llvm-c/Core.h b/llvm/include/llvm-c/Core.h index ddc7fe7..f449429 100644 --- a/llvm/include/llvm-c/Core.h +++ b/llvm/include/llvm-c/Core.h @@ -341,6 +341,11 @@ typedef enum { LLVMDSNote } LLVMDiagnosticSeverity; +typedef enum { + LLVMInlineAsmDialectATT, + LLVMInlineAsmDialectIntel +} LLVMInlineAsmDialect; + /** * Attribute index are either LLVMAttributeReturnIndex, * LLVMAttributeFunctionIndex or a parameter number from 1 to N. @@ -650,11 +655,36 @@ LLVMBool LLVMPrintModuleToFile(LLVMModuleRef M, const char *Filename, char *LLVMPrintModuleToString(LLVMModuleRef M); /** + * Get inline assembly for a module. + * + * @see Module::getModuleInlineAsm() + */ +const char *LLVMGetModuleInlineAsm(LLVMModuleRef M, size_t *Len); + +/** * Set inline assembly for a module. * * @see Module::setModuleInlineAsm() */ -void LLVMSetModuleInlineAsm(LLVMModuleRef M, const char *Asm); +void LLVMSetModuleInlineAsm2(LLVMModuleRef M, const char *Asm, size_t Len); + +/** + * Append inline assembly to a module. + * + * @see Module::appendModuleInlineAsm() + */ +void LLVMAppendModuleInlineAsm(LLVMModuleRef M, const char *Asm, size_t Len); + +/** + * Create the specified uniqued inline asm string. + * + * @see InlineAsm::get() + */ +LLVMValueRef LLVMGetInlineAsm(LLVMTypeRef Ty, + char *AsmString, size_t AsmStringSize, + char *Constraints, size_t ConstraintsSize, + LLVMBool HasSideEffects, LLVMBool IsAlignStack, + LLVMInlineAsmDialect Dialect); /** * Obtain the context to which this module is associated. @@ -745,6 +775,9 @@ LLVMValueRef LLVMGetNextFunction(LLVMValueRef Fn); */ LLVMValueRef LLVMGetPreviousFunction(LLVMValueRef Fn); +/** Deprecated: Use LLVMSetModuleInlineAsm2 instead. */ +void LLVMSetModuleInlineAsm(LLVMModuleRef M, const char *Asm); + /** * @} */ @@ -1820,10 +1853,12 @@ LLVMValueRef LLVMConstExtractValue(LLVMValueRef AggConstant, unsigned *IdxList, LLVMValueRef LLVMConstInsertValue(LLVMValueRef AggConstant, LLVMValueRef ElementValueConstant, unsigned *IdxList, unsigned NumIdx); +LLVMValueRef LLVMBlockAddress(LLVMValueRef F, LLVMBasicBlockRef BB); + +/** Deprecated: Use LLVMGetInlineAsm instead. */ LLVMValueRef LLVMConstInlineAsm(LLVMTypeRef Ty, const char *AsmString, const char *Constraints, LLVMBool HasSideEffects, LLVMBool IsAlignStack); -LLVMValueRef LLVMBlockAddress(LLVMValueRef F, LLVMBasicBlockRef BB); /** * @} diff --git a/llvm/lib/IR/Core.cpp b/llvm/lib/IR/Core.cpp index 29a82ec..439c34b 100644 --- a/llvm/lib/IR/Core.cpp +++ b/llvm/lib/IR/Core.cpp @@ -304,10 +304,42 @@ char *LLVMPrintModuleToString(LLVMModuleRef M) { } /*--.. Operations on inline assembler ......................................--*/ +void LLVMSetModuleInlineAsm2(LLVMModuleRef M, const char *Asm, size_t Len) { + unwrap(M)->setModuleInlineAsm(StringRef(Asm, Len)); +} + void LLVMSetModuleInlineAsm(LLVMModuleRef M, const char *Asm) { unwrap(M)->setModuleInlineAsm(StringRef(Asm)); } +void LLVMAppendModuleInlineAsm(LLVMModuleRef M, const char *Asm, size_t Len) { + unwrap(M)->appendModuleInlineAsm(StringRef(Asm, Len)); +} + +const char *LLVMGetModuleInlineAsm(LLVMModuleRef M, size_t *Len) { + auto &Str = unwrap(M)->getModuleInlineAsm(); + *Len = Str.length(); + return Str.c_str(); +} + +LLVMValueRef LLVMGetInlineAsm(LLVMTypeRef Ty, + char *AsmString, size_t AsmStringSize, + char *Constraints, size_t ConstraintsSize, + LLVMBool HasSideEffects, LLVMBool IsAlignStack, + LLVMInlineAsmDialect Dialect) { + InlineAsm::AsmDialect AD; + switch (Dialect) { + case LLVMInlineAsmDialectATT: + AD = InlineAsm::AD_ATT; + case LLVMInlineAsmDialectIntel: + AD = InlineAsm::AD_Intel; + } + return wrap(InlineAsm::get(unwrap(Ty), + StringRef(AsmString, AsmStringSize), + StringRef(Constraints, ConstraintsSize), + HasSideEffects, IsAlignStack, AD)); +} + /*--.. Operations on module contexts ......................................--*/ LLVMContextRef LLVMGetModuleContext(LLVMModuleRef M) { diff --git a/llvm/test/Bindings/llvm-c/echo.ll b/llvm/test/Bindings/llvm-c/echo.ll index 8c9cd7b..958aba0 100644 --- a/llvm/test/Bindings/llvm-c/echo.ll +++ b/llvm/test/Bindings/llvm-c/echo.ll @@ -6,6 +6,8 @@ source_filename = "/test/Bindings/echo.ll" target datalayout = "e-m:o-i64:64-f80:128-n8:16:32:64-S128" target triple = "x86_64-apple-macosx10.11.0" +module asm "classical GAS" + %S = type { i64, %S* } @var = global i32 42 diff --git a/llvm/tools/llvm-c-test/echo.cpp b/llvm/tools/llvm-c-test/echo.cpp index 4b6423d..19ad5bd 100644 --- a/llvm/tools/llvm-c-test/echo.cpp +++ b/llvm/tools/llvm-c-test/echo.cpp @@ -1002,6 +1002,10 @@ int llvm_echo(void) { if (strcmp(LLVMGetDataLayoutStr(M), LLVMGetDataLayoutStr(Src))) report_fatal_error("Inconsistent DataLayout string representation"); + size_t ModuleInlineAsmLen; + const char *ModuleAsm = LLVMGetModuleInlineAsm(Src, &ModuleInlineAsmLen); + LLVMSetModuleInlineAsm2(M, ModuleAsm, ModuleInlineAsmLen); + declare_symbols(Src, M); clone_symbols(Src, M); char *Str = LLVMPrintModuleToString(M); -- 2.7.4