Make LLVMConstInt transparently work with bignums.
[cl-llvm.git] / src / llvm-extras.cpp
blob1b368957b0f07462634da6fa5f5b67805e30a568
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>
7 #include <llvm/PassManager.h>
8 #include <llvm/CodeGen/Passes.h>
10 #ifndef SWIG
11 using namespace llvm;
12 #endif
14 extern "C" {
15 // Declare here so the inline definition gets into the lib. Why is
16 // there an inline function in a binding header anyways. :(
17 int CLLLVM_LLVMInitializeNativeTarget() {
18 LLVMInitializeNativeTarget();
21 // Functions missing in the LLVM C API
25 LLVMModuleRef CLLLVM_LLVMModuleProviderGetModule(LLVMModuleProviderRef modprovider) {
26 return wrap(unwrap(modprovider)->getModule());
29 LLVMModuleRef CLLLVM_LLVMParseAssemblyString(const char *AsmString,
30 LLVMModuleRef M,
31 LLVMContextRef Context) {
32 class SMDiagnostic Error;
33 LLVMModuleRef res =
34 wrap(ParseAssemblyString(AsmString, unwrap(M), Error, *unwrap(Context)));
35 Error.Print("sbcl", errs());
38 LLVMTypeRef CLLLVM_LLVMIntPtrTypeInContext(LLVMContextRef Context, LLVMTargetDataRef TD) {
39 return wrap(unwrap(TD)->getIntPtrType(*unwrap(Context)));
42 char *CLLLVM_LLVMDumpModuleToString(LLVMModuleRef module)
44 std::string s;
45 raw_string_ostream buf(s);
46 unwrap(module)->print(buf, NULL);
47 return strdup(buf.str().c_str());
50 char *CLLLVM_LLVMDumpTypeToString(LLVMTypeRef type)
52 std::string s;
53 raw_string_ostream buf(s);
54 unwrap(type)->print(buf);
55 return strdup(buf.str().c_str());
58 char *CLLLVM_LLVMDumpValueToString(LLVMValueRef value)
60 std::string s;
61 raw_string_ostream buf(s);
62 unwrap(value)->print(buf);
63 return strdup(buf.str().c_str());
66 // These are buggy in LLVM: they affect the return value attributes,
67 // not the fn attributes.
68 void CLLLVM_LLVMAddFunctionAttr(LLVMValueRef Fn, LLVMAttribute PA) {
69 Function *Func = unwrap<Function>(Fn);
70 const AttrListPtr PAL = Func->getAttributes();
71 const AttrListPtr PALnew = PAL.addAttr(~0, PA);
72 Func->setAttributes(PALnew);
75 void CLLLVM_LLVMRemoveFunctionAttr(LLVMValueRef Fn, LLVMAttribute PA) {
76 Function *Func = unwrap<Function>(Fn);
77 const AttrListPtr PAL = Func->getAttributes();
78 const AttrListPtr PALnew = PAL.removeAttr(~0, PA);
79 Func->setAttributes(PALnew);
82 void CLLLVM_LLVMAddRetAttr(LLVMValueRef Fn, LLVMAttribute PA) {
83 Function *Func = unwrap<Function>(Fn);
84 const AttrListPtr PAL = Func->getAttributes();
85 const AttrListPtr PALnew = PAL.addAttr(0, PA);
86 Func->setAttributes(PALnew);
89 void CLLLVM_LLVMRemoveRetAttr(LLVMValueRef Fn, LLVMAttribute PA) {
90 Function *Func = unwrap<Function>(Fn);
91 const AttrListPtr PAL = Func->getAttributes();
92 const AttrListPtr PALnew = PAL.removeAttr(0, PA);
93 Func->setAttributes(PALnew);
96 LLVMAttribute CLLLVM_LLVMGetRetAttr(LLVMValueRef Fn) {
97 Function *Func = unwrap<Function>(Fn);
98 const AttrListPtr PAL = Func->getAttributes();
99 Attributes attr = PAL.getRetAttributes();
100 return (LLVMAttribute)attr;
103 LLVMValueRef CLLLVM_LLVMConstIntOfBigVal(LLVMTypeRef IntTy,
104 unsigned numWords,
105 const uint64_t bigVal[]) {
106 IntegerType *Ty = unwrap<IntegerType>(IntTy);
107 return wrap(ConstantInt::get(Ty->getContext(),
108 APInt(Ty->getBitWidth(), numWords, bigVal)));
111 void CLLLVM_AddPrintAsmPass(LLVMPassManagerRef PM, char *Banner) {
112 unwrap(PM)->add(createMachineFunctionPrinterPass(errs(), Banner));