Wrap more stuff, move some things around.
[cl-llvm.git] / src / llvm-extras.cpp
blob2348bb6e32981b06e5e0d641155dbebd29ff8612
1 #include <llvm-c/Target.h>
2 #include <llvm/Target/TargetData.h>
3 #include <llvm/ModuleProvider.h>
4 #include <llvm/Support/SourceMgr.h>
5 #include <llvm/Support/raw_ostream.h>
6 #include <llvm/Assembly/Parser.h>
8 #ifndef SWIG
9 using namespace llvm;
10 #endif
12 extern "C" {
13 // Declare here so the inline definition gets into the lib. Why is
14 // there an inline function in a binding header anyways. :(
15 int CLLLVM_LLVMInitializeNativeTarget() {
16 LLVMInitializeNativeTarget();
19 // Functions missing in the LLVM C API
23 LLVMModuleRef CLLLVM_LLVMModuleProviderGetModule(LLVMModuleProviderRef modprovider) {
24 return wrap(unwrap(modprovider)->getModule());
27 LLVMModuleRef CLLLVM_LLVMParseAssemblyString(const char *AsmString,
28 LLVMModuleRef M,
29 LLVMContextRef Context) {
30 class SMDiagnostic Error;
31 LLVMModuleRef res =
32 wrap(ParseAssemblyString(AsmString, unwrap(M), Error, *unwrap(Context)));
33 Error.Print("sbcl", errs());
36 LLVMTypeRef CLLLVM_LLVMIntPtrTypeInContext(LLVMContextRef Context, LLVMTargetDataRef TD) {
37 return wrap(unwrap(TD)->getIntPtrType(*unwrap(Context)));
40 char *CLLLVM_LLVMDumpModuleToString(LLVMModuleRef module)
42 std::string s;
43 raw_string_ostream buf(s);
44 unwrap(module)->print(buf, NULL);
45 return strdup(buf.str().c_str());
48 char *CLLLVM_LLVMDumpTypeToString(LLVMTypeRef type)
50 std::string s;
51 raw_string_ostream buf(s);
52 unwrap(type)->print(buf);
53 return strdup(buf.str().c_str());
56 char *CLLLVM_LLVMDumpValueToString(LLVMValueRef value)
58 std::string s;
59 raw_string_ostream buf(s);
60 unwrap(value)->print(buf);
61 return strdup(buf.str().c_str());
64 // These are buggy in LLVM: they affect the return value attributes,
65 // not the fn attributes.
66 void CLLLVM_LLVMAddFunctionAttr(LLVMValueRef Fn, LLVMAttribute PA) {
67 Function *Func = unwrap<Function>(Fn);
68 const AttrListPtr PAL = Func->getAttributes();
69 const AttrListPtr PALnew = PAL.addAttr(~0, PA);
70 Func->setAttributes(PALnew);
73 void CLLLVM_LLVMRemoveFunctionAttr(LLVMValueRef Fn, LLVMAttribute PA) {
74 Function *Func = unwrap<Function>(Fn);
75 const AttrListPtr PAL = Func->getAttributes();
76 const AttrListPtr PALnew = PAL.removeAttr(~0, PA);
77 Func->setAttributes(PALnew);
80 void CLLLVM_LLVMAddRetAttr(LLVMValueRef Fn, LLVMAttribute PA) {
81 Function *Func = unwrap<Function>(Fn);
82 const AttrListPtr PAL = Func->getAttributes();
83 const AttrListPtr PALnew = PAL.addAttr(0, PA);
84 Func->setAttributes(PALnew);
87 void CLLLVM_LLVMRemoveRetAttr(LLVMValueRef Fn, LLVMAttribute PA) {
88 Function *Func = unwrap<Function>(Fn);
89 const AttrListPtr PAL = Func->getAttributes();
90 const AttrListPtr PALnew = PAL.removeAttr(0, PA);
91 Func->setAttributes(PALnew);
94 LLVMAttribute CLLLVM_LLVMGetRetAttr(LLVMValueRef Fn) {
95 Function *Func = unwrap<Function>(Fn);
96 const AttrListPtr PAL = Func->getAttributes();
97 Attributes attr = PAL.getRetAttributes();
98 return (LLVMAttribute)attr;