From 9aea1d086521b297ead0b27b36497c01a6860b23 Mon Sep 17 00:00:00 2001 From: James Y Knight Date: Wed, 30 Dec 2009 13:51:12 -0500 Subject: [PATCH] Wrap more stuff, move some things around. --- cl-llvm.asd | 4 ++- src/Makefile | 12 ++++---- src/core.i | 5 ++++ src/generated/core.lisp | 8 ----- src/generated/llvm-extras.lisp | 52 +++++++++++++++++++++++++++++++++ src/generated/transforms-ipo.lisp | 52 +++++++++++++++++++++++++++++++++ src/llvm-extras.cpp | 61 ++++++++++++++++++++++++++++++++------- src/llvm-extras.i | 9 ++++++ src/stuff.lisp | 14 --------- src/transforms-ipo.i | 3 ++ src/typemaps.i | 17 +++++++++++ 11 files changed, 198 insertions(+), 39 deletions(-) create mode 100644 src/generated/llvm-extras.lisp create mode 100644 src/generated/transforms-ipo.lisp create mode 100644 src/llvm-extras.i create mode 100644 src/transforms-ipo.i diff --git a/cl-llvm.asd b/cl-llvm.asd index c385548..8d7b9ee 100644 --- a/cl-llvm.asd +++ b/cl-llvm.asd @@ -15,7 +15,9 @@ (:file "analysis") (:file "target") (:file "execution-engine") - (:file "transforms-scalar"))) + (:file "transforms-scalar") + (:file "transforms-ipo") + (:file "llvm-extras"))) (:file "stuff") (:file "packages-post"))))) diff --git a/src/Makefile b/src/Makefile index 379978e..5bf54b7 100644 --- a/src/Makefile +++ b/src/Makefile @@ -9,11 +9,13 @@ LINKER=g++ LLVM_LIBS=-Wl,--whole-archive $(shell llvm-config --libs core jit interpreter native asmparser) -Wl,--no-whole-archive BINDINGS_FILES=$(addprefix generated/,\ - core.lisp \ - analysis.lisp \ - execution-engine.lisp \ - target.lisp \ - transforms-scalar.lisp) + core.lisp \ + analysis.lisp \ + execution-engine.lisp \ + target.lisp \ + transforms-scalar.lisp \ + transforms-ipo.lisp \ + llvm-extras.lisp) all: build diff --git a/src/core.i b/src/core.i index baabe7a..fec951d 100644 --- a/src/core.i +++ b/src/core.i @@ -72,6 +72,11 @@ typedef unsigned char uint8_t; %ignore LLVMCreateBuilder; %rename LLVMCreateBuilderInContext "%%LLVMCreateBuilderInContext"; + +// Discard broken functions, in favor of those defined in llvm-extras.cpp +%ignore LLVMAddFunctionAttr; +%ignore LLVMRemoveFunctionAttr; + %include "llvm-c/Core.h" diff --git a/src/generated/core.lisp b/src/generated/core.lisp index f745d19..17fb9a8 100644 --- a/src/generated/core.lisp +++ b/src/generated/core.lisp @@ -1029,17 +1029,9 @@ (Fn LLVMValueRef) (Name :string)) -(cffi:defcfun ("LLVMAddFunctionAttr" LLVMAddFunctionAttr) :void - (Fn LLVMValueRef) - (PA LLVMAttribute)) - (cffi:defcfun ("LLVMGetFunctionAttr" LLVMGetFunctionAttr) LLVMAttribute (Fn LLVMValueRef)) -(cffi:defcfun ("LLVMRemoveFunctionAttr" LLVMRemoveFunctionAttr) :void - (Fn LLVMValueRef) - (PA LLVMAttribute)) - (cffi:defcfun ("LLVMCountParams" LLVMCountParams) :unsigned-int (Fn LLVMValueRef)) diff --git a/src/generated/llvm-extras.lisp b/src/generated/llvm-extras.lisp new file mode 100644 index 0000000..30d484c --- /dev/null +++ b/src/generated/llvm-extras.lisp @@ -0,0 +1,52 @@ +;;; This file was automatically generated by SWIG (http://www.swig.org). +;;; Version 1.3.40 +;;; +;;; Do not make changes to this file unless you know what you are doing--modify +;;; the SWIG interface file instead. + +(in-package :llvm) + + +(cffi:defcfun ("CLLLVM_LLVMInitializeNativeTarget" CLLLVM_LLVMInitializeNativeTarget) :int) + +(cffi:defcfun ("CLLLVM_LLVMModuleProviderGetModule" CLLLVM_LLVMModuleProviderGetModule) LLVMModuleRef + (modprovider LLVMModuleProviderRef)) + +(cffi:defcfun ("CLLLVM_LLVMParseAssemblyString" CLLLVM_LLVMParseAssemblyString) LLVMModuleRef + (AsmString :string) + (M LLVMModuleRef) + (Context LLVMContextRef)) + +(cffi:defcfun ("CLLLVM_LLVMIntPtrTypeInContext" CLLLVM_LLVMIntPtrTypeInContext) LLVMTypeRef + (Context LLVMContextRef) + (TD LLVMTargetDataRef)) + +(cffi:defcfun ("CLLLVM_LLVMDumpModuleToString" CLLLVM_LLVMDumpModuleToString) :string + (module LLVMModuleRef)) + +(cffi:defcfun ("CLLLVM_LLVMDumpTypeToString" CLLLVM_LLVMDumpTypeToString) :string + (type LLVMTypeRef)) + +(cffi:defcfun ("CLLLVM_LLVMDumpValueToString" CLLLVM_LLVMDumpValueToString) :string + (value LLVMValueRef)) + +(cffi:defcfun ("CLLLVM_LLVMAddFunctionAttr" LLVMAddFunctionAttr) :void + (Fn LLVMValueRef) + (PA LLVMAttribute)) + +(cffi:defcfun ("CLLLVM_LLVMRemoveFunctionAttr" LLVMRemoveFunctionAttr) :void + (Fn LLVMValueRef) + (PA LLVMAttribute)) + +(cffi:defcfun ("CLLLVM_LLVMAddRetAttr" LLVMAddRetAttr) :void + (Fn LLVMValueRef) + (PA LLVMAttribute)) + +(cffi:defcfun ("CLLLVM_LLVMRemoveRetAttr" LLVMRemoveRetAttr) :void + (Fn LLVMValueRef) + (PA LLVMAttribute)) + +(cffi:defcfun ("CLLLVM_LLVMGetRetAttr" LLVMGetRetAttr) LLVMAttribute + (Fn LLVMValueRef)) + + diff --git a/src/generated/transforms-ipo.lisp b/src/generated/transforms-ipo.lisp new file mode 100644 index 0000000..e074137 --- /dev/null +++ b/src/generated/transforms-ipo.lisp @@ -0,0 +1,52 @@ +;;; This file was automatically generated by SWIG (http://www.swig.org). +;;; Version 1.3.40 +;;; +;;; Do not make changes to this file unless you know what you are doing--modify +;;; the SWIG interface file instead. + +(in-package :llvm) + + +(cffi:defcfun ("LLVMAddArgumentPromotionPass" LLVMAddArgumentPromotionPass) :void + (PM LLVMPassManagerRef)) + +(cffi:defcfun ("LLVMAddConstantMergePass" LLVMAddConstantMergePass) :void + (PM LLVMPassManagerRef)) + +(cffi:defcfun ("LLVMAddDeadArgEliminationPass" LLVMAddDeadArgEliminationPass) :void + (PM LLVMPassManagerRef)) + +(cffi:defcfun ("LLVMAddDeadTypeEliminationPass" LLVMAddDeadTypeEliminationPass) :void + (PM LLVMPassManagerRef)) + +(cffi:defcfun ("LLVMAddFunctionAttrsPass" LLVMAddFunctionAttrsPass) :void + (PM LLVMPassManagerRef)) + +(cffi:defcfun ("LLVMAddFunctionInliningPass" LLVMAddFunctionInliningPass) :void + (PM LLVMPassManagerRef)) + +(cffi:defcfun ("LLVMAddGlobalDCEPass" LLVMAddGlobalDCEPass) :void + (PM LLVMPassManagerRef)) + +(cffi:defcfun ("LLVMAddGlobalOptimizerPass" LLVMAddGlobalOptimizerPass) :void + (PM LLVMPassManagerRef)) + +(cffi:defcfun ("LLVMAddIPConstantPropagationPass" LLVMAddIPConstantPropagationPass) :void + (PM LLVMPassManagerRef)) + +(cffi:defcfun ("LLVMAddLowerSetJmpPass" LLVMAddLowerSetJmpPass) :void + (PM LLVMPassManagerRef)) + +(cffi:defcfun ("LLVMAddPruneEHPass" LLVMAddPruneEHPass) :void + (PM LLVMPassManagerRef)) + +(cffi:defcfun ("LLVMAddRaiseAllocationsPass" LLVMAddRaiseAllocationsPass) :void + (PM LLVMPassManagerRef)) + +(cffi:defcfun ("LLVMAddStripDeadPrototypesPass" LLVMAddStripDeadPrototypesPass) :void + (PM LLVMPassManagerRef)) + +(cffi:defcfun ("LLVMAddStripSymbolsPass" LLVMAddStripSymbolsPass) :void + (PM LLVMPassManagerRef)) + + diff --git a/src/llvm-extras.cpp b/src/llvm-extras.cpp index b9643ce..2348bb6 100644 --- a/src/llvm-extras.cpp +++ b/src/llvm-extras.cpp @@ -5,6 +5,10 @@ #include #include +#ifndef SWIG +using namespace llvm; +#endif + extern "C" { // Declare here so the inline definition gets into the lib. Why is // there an inline function in a binding header anyways. :( @@ -17,46 +21,81 @@ int CLLLVM_LLVMInitializeNativeTarget() { LLVMModuleRef CLLLVM_LLVMModuleProviderGetModule(LLVMModuleProviderRef modprovider) { - return llvm::wrap(llvm::unwrap(modprovider)->getModule()); + return wrap(unwrap(modprovider)->getModule()); } LLVMModuleRef CLLLVM_LLVMParseAssemblyString(const char *AsmString, LLVMModuleRef M, LLVMContextRef Context) { - class llvm::SMDiagnostic Error; + class SMDiagnostic Error; LLVMModuleRef res = - llvm::wrap(llvm::ParseAssemblyString(AsmString, llvm::unwrap(M), Error, *llvm::unwrap(Context))); - Error.Print("sbcl", llvm::errs()); + wrap(ParseAssemblyString(AsmString, unwrap(M), Error, *unwrap(Context))); + Error.Print("sbcl", errs()); } LLVMTypeRef CLLLVM_LLVMIntPtrTypeInContext(LLVMContextRef Context, LLVMTargetDataRef TD) { - return llvm::wrap(llvm::unwrap(TD)->getIntPtrType(*llvm::unwrap(Context))); + return wrap(unwrap(TD)->getIntPtrType(*unwrap(Context))); } char *CLLLVM_LLVMDumpModuleToString(LLVMModuleRef module) { std::string s; - llvm::raw_string_ostream buf(s); - llvm::unwrap(module)->print(buf, NULL); + raw_string_ostream buf(s); + unwrap(module)->print(buf, NULL); return strdup(buf.str().c_str()); } char *CLLLVM_LLVMDumpTypeToString(LLVMTypeRef type) { std::string s; - llvm::raw_string_ostream buf(s); - llvm::unwrap(type)->print(buf); + raw_string_ostream buf(s); + unwrap(type)->print(buf); return strdup(buf.str().c_str()); } char *CLLLVM_LLVMDumpValueToString(LLVMValueRef value) { std::string s; - llvm::raw_string_ostream buf(s); - llvm::unwrap(value)->print(buf); + raw_string_ostream buf(s); + unwrap(value)->print(buf); return strdup(buf.str().c_str()); } + +// These are buggy in LLVM: they affect the return value attributes, +// not the fn attributes. +void CLLLVM_LLVMAddFunctionAttr(LLVMValueRef Fn, LLVMAttribute PA) { + Function *Func = unwrap(Fn); + const AttrListPtr PAL = Func->getAttributes(); + const AttrListPtr PALnew = PAL.addAttr(~0, PA); + Func->setAttributes(PALnew); } +void CLLLVM_LLVMRemoveFunctionAttr(LLVMValueRef Fn, LLVMAttribute PA) { + Function *Func = unwrap(Fn); + const AttrListPtr PAL = Func->getAttributes(); + const AttrListPtr PALnew = PAL.removeAttr(~0, PA); + Func->setAttributes(PALnew); +} +void CLLLVM_LLVMAddRetAttr(LLVMValueRef Fn, LLVMAttribute PA) { + Function *Func = unwrap(Fn); + const AttrListPtr PAL = Func->getAttributes(); + const AttrListPtr PALnew = PAL.addAttr(0, PA); + Func->setAttributes(PALnew); +} +void CLLLVM_LLVMRemoveRetAttr(LLVMValueRef Fn, LLVMAttribute PA) { + Function *Func = unwrap(Fn); + const AttrListPtr PAL = Func->getAttributes(); + const AttrListPtr PALnew = PAL.removeAttr(0, PA); + Func->setAttributes(PALnew); +} + +LLVMAttribute CLLLVM_LLVMGetRetAttr(LLVMValueRef Fn) { + Function *Func = unwrap(Fn); + const AttrListPtr PAL = Func->getAttributes(); + Attributes attr = PAL.getRetAttributes(); + return (LLVMAttribute)attr; +} + +} diff --git a/src/llvm-extras.i b/src/llvm-extras.i new file mode 100644 index 0000000..f6248da --- /dev/null +++ b/src/llvm-extras.i @@ -0,0 +1,9 @@ +%include "typemaps.i" + +%rename CLLLVM_LLVMAddFunctionAttr "LLVMAddFunctionAttr"; +%rename CLLLVM_LLVMRemoveFunctionAttr "LLVMRemoveFunctionAttr"; +%rename CLLLVM_LLVMAddRetAttr "LLVMAddRetAttr"; +%rename CLLLVM_LLVMRemoveRetAttr "LLVMRemoveRetAttr"; +%rename CLLLVM_LLVMGetRetAttr "LLVMGetRetAttr"; + +%include "./llvm-extras.cpp" diff --git a/src/stuff.lisp b/src/stuff.lisp index b5c0540..266deef 100644 --- a/src/stuff.lisp +++ b/src/stuff.lisp @@ -1,19 +1,5 @@ (in-package :llvm) ;; Load up the native codegen. -(cffi:defcfun ("CLLLVM_LLVMInitializeNativeTarget" CLLLVM_LLVMInitializeNativeTarget) :int) -(cffi:defcfun ("CLLLVM_LLVMModuleProviderGetModule" CLLLVM_LLVMModuleProviderGetModule) :pointer - (modprovider :pointer)) -(cffi:defcfun ("CLLLVM_LLVMParseAssemblyString" CLLLVM_LLVMParseAssemblyString) :pointer - (asm-string :string) - (module :pointer) - (context :pointer)) -(cffi:defcfun ("CLLLVM_LLVMDumpModuleToString" CLLLVM_LLVMDumpModuleToString) :string - (module :pointer)) -(cffi:defcfun ("CLLLVM_LLVMDumpTypeToString" CLLLVM_LLVMDumpTypeToString) :string - (module :pointer)) -(cffi:defcfun ("CLLLVM_LLVMDumpValueToString" CLLLVM_LLVMDumpValueToString) :string - (module :pointer)) - (CLLLVM_LLVMInitializeNativeTarget) ;; A global context. Most of LLVM is only thread-safe within a single "context". There is an diff --git a/src/transforms-ipo.i b/src/transforms-ipo.i new file mode 100644 index 0000000..3deaabc --- /dev/null +++ b/src/transforms-ipo.i @@ -0,0 +1,3 @@ +%include "typemaps.i" + +%include "llvm-c/Transforms/IPO.h" diff --git a/src/typemaps.i b/src/typemaps.i index 4f14467..6bcbde6 100644 --- a/src/typemaps.i +++ b/src/typemaps.i @@ -11,6 +11,23 @@ // :pointer, in any case, via defctype. // For Core.h +%typemap(cin) LLVMAttribute "LLVMAttribute"; +%typemap(cout) LLVMAttribute "LLVMAttribute"; +%typemap(cin) LLVMOpcode "LLVMOpcode"; +%typemap(cout) LLVMOpcode "LLVMOpcode"; +%typemap(cin) LLVMTypeKind "LLVMTypeKind"; +%typemap(cout) LLVMTypeKind "LLVMTypeKind"; +%typemap(cin) LLVMLinkage "LLVMLinkage"; +%typemap(cout) LLVMLinkage "LLVMLinkage"; +%typemap(cin) LLVMVisibility "LLVMVisibility"; +%typemap(cout) LLVMVisibility "LLVMVisibility"; +%typemap(cin) LLVMCallConv "LLVMCallConv"; +%typemap(cout) LLVMCallConv "LLVMCallConv"; +%typemap(cin) LLVMIntPredicate "LLVMIntPredicate"; +%typemap(cout) LLVMIntPredicate "LLVMIntPredicate"; +%typemap(cin) LLVMRealPredicate "LLVMRealPredicate"; +%typemap(cout) LLVMRealPredicate "LLVMRealPredicate"; + %typemap(cin) LLVMContextRef "LLVMContextRef"; %typemap(cout) LLVMContextRef "LLVMContextRef"; %typemap(cin) LLVMModuleRef "LLVMModuleRef"; -- 2.11.4.GIT