/
llvm-racket.cpp
131 lines (97 loc) · 3.28 KB
/
llvm-racket.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
#include "llvm-c/Core.h"
#include "llvm/IR/Type.h"
#include "llvm/IR/Value.h"
#include "llvm/PassManager.h"
//#include "llvm/IntrinsicInst.h"
#include "llvm/Support/TargetSelect.h"
#include "llvm/Support/raw_ostream.h"
#include "llvm/Target/TargetLibraryInfo.h"
#include "llvm/IR/DataLayout.h"
#include "llvm/IR/Intrinsics.h"
#include "llvm/IR/Module.h"
#include "llvm/IR/Verifier.h"
#include "llvm/Transforms/IPO.h"
#include "llvm/Transforms/IPO/PassManagerBuilder.h"
#include "llvm/ADT/Triple.h"
#ifdef __cplusplus
extern "C" {
#endif
using namespace llvm;
/*===-- Error handling ----------------------------------------------------===*/
const char* LLVMGetTypeDescription(LLVMTypeRef Ty) {
std::string DescStr;
raw_string_ostream DescOS(DescStr);
unwrap(Ty)->print(DescOS);
std::string str = DescOS.str();
char* cstr = (char*) malloc(str.length()+1);
strcpy(cstr,str.c_str());
return cstr;
}
char* LLVMGetValueDescription(LLVMValueRef V) {
std::string DescStr;
raw_string_ostream DescOS(DescStr);
unwrap(V)->print(DescOS);
std::string str = DescOS.str();
char* cstr = (char*) malloc(str.length()+1);
strcpy(cstr,str.c_str());
return cstr;
}
char* LLVMGetModuleDescription(LLVMModuleRef M) {
std::string DescStr;
raw_string_ostream DescOS(DescStr);
unwrap(M)->print(DescOS,NULL);
std::string str = DescOS.str();
char* cstr = (char*) malloc(str.length()+1);
strcpy(cstr,str.c_str());
return cstr;
}
bool LLVMIsValidTypeIndex(LLVMTypeRef Ty, LLVMValueRef Idx) {
CompositeType* Comp = cast<CompositeType>(unwrap(Ty));
const Value* V = unwrap(Idx);
return Comp->indexValid(V);
}
LLVMTypeRef LLVMGetTypeAtIndex(LLVMTypeRef Ty, LLVMValueRef Idx) {
CompositeType* Comp = cast<CompositeType>(unwrap(Ty));
const Value* V = unwrap(Idx);
return wrap(Comp->getTypeAtIndex(V));
}
bool LLVMIsTerminatorInstruction(LLVMValueRef V) {
return cast<Instruction>(unwrap(V))->isTerminator();
}
void LLVMInitializeRacket() {
InitializeNativeTarget();
}
bool LLVMOptimizeModule(LLVMModuleRef Mod) {
Module* M = unwrap(Mod);
// Create a PassManager to hold and optimize the collection of passes we are
// about to build.
PassManager Passes;
// Add an appropriate TargetLibraryInfo pass for the module's triple.
TargetLibraryInfo *TLI = new TargetLibraryInfo(Triple(M->getTargetTriple()));
// Add an appropriate DataLayout instance for this module.
// const std::string &ModuleDataLayout = M->getDataLayout();
// if (!ModuleDataLayout.empty()) {
// DataLayout *TD = NULL; // new DataLayout(ModuleDataLayout);
// Passes.add(TD);
// }
Passes.add(createVerifierPass()); // Verify that input is correct
// -std-compile-opts adds the same module passes as -O3.
PassManagerBuilder Builder;
Builder.Inliner = createFunctionInliningPass();
Builder.OptLevel = 3;
Builder.populateModulePassManager(Passes);
// Now that we have all of the passes ready, run them.
bool change = Passes.run(*M);
return change;
}
LLVMValueRef LLVMGetIntrinsic(
LLVMModuleRef M,
Intrinsic::ID id,
LLVMTypeRef *ParamTypes,
unsigned ParamCount) {
ArrayRef<Type*> Tys(unwrap(ParamTypes), ParamCount);
return wrap(Intrinsic::getDeclaration(unwrap(M), id, Tys));
}
#ifdef __cplusplus
}
#endif /* !defined(__cplusplus) */