-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathllvm.cc
97 lines (87 loc) · 3.18 KB
/
llvm.cc
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
#include "llvm.h"
using namespace std;
using namespace llvm;
void MakeErrorInfo(SMDiagnostic& err, std::string &output) {
std::ostringstream stringStream;
std::string errMsg = err.getMessage();
std::string errLineContents = err.getLineContents();
int errLineNo = err.getLineNo(), errColNo = err.getColumnNo();
stringStream << "Error Message: " << errMsg << std::endl;
stringStream << "Error Line number: " << errLineNo << ". Column number: " << errColNo << std::endl;
output = stringStream.str();
}
void llvm_init() {
InitializeNativeTarget();
llvm_start_multithreaded();
}
LLVMRESOURCE llvm_newResource() {
LLVMResource *Resource = new LLVMResource();
return (LLVMRESOURCE) Resource;
}
void llvm_freeResource(LLVMRESOURCE Resource) {
delete (LLVMResource *) Resource;
}
void *llvm_callFunc(LLVMRESOURCE _Resource, const char *name, size_t name_length, void **argv, int argc, char *errormsg, size_t errormsg_size) {
fCall_t call;
call = llvm_getFunc(_Resource, name);
if(call == NULL){
return false;
}
return call(argv, argc);
}
fCall_t llvm_getFunc(LLVMRESOURCE _Resource, const char *name){
std::string funcName, errorMessage;
Module *m;
// funcName.assign(name, strlen(name));
LLVMResource *Resource = (LLVMResource *) _Resource;
ExecutionEngine *ee = Resource->getExecutionEngine();
//funcName.c_str()
Function* func = ee->FindFunctionNamed(name);
if (func == NULL) {
return NULL;
}
return (fCall_t) ee->getPointerToFunction(func);
}
size_t llvm_compileAssembly(LLVMRESOURCE _Resource, char *Buffer, size_t size, char *Output, size_t output_size, char *errormsg, size_t errormsg_size, int optimize_level) {
std::string str, bitcodeString, errorMessage;
SMDiagnostic error;
Module *m;
LLVMResource *Resource = (LLVMResource *) _Resource;
MemoryBuffer *Mem = MemoryBuffer::getMemBuffer(StringRef(Buffer, size), "", false);
m = ParseAssembly(Mem, 0, error, Resource->getContext());
if (m == NULL) {
MakeErrorInfo(error, errorMessage);
errorMessage.copy(errormsg, errormsg_size);
return 0;
}
if (optimize_level) {
optimizeModule(m, optimize_level);
}
raw_string_ostream stream(str);
WriteBitcodeToFile(m, stream);
bitcodeString = stream.str();
if (bitcodeString.length() > output_size) {
errorMessage = "output buffer is too small!";
errorMessage.copy(errormsg, errormsg_size);
return 0;
}
bitcodeString.copy(Output, bitcodeString.length());
return bitcodeString.length();
}
int llvm_loadBitcode(LLVMRESOURCE _Resource, char *Buffer, size_t size, char *errormsg, size_t errormsg_size) {
string error;
Module *m;
LLVMResource *Resource = (LLVMResource *) _Resource;
// ExecutionEngine *ee = Resource->getExecutionEngine();
MemoryBuffer *Mem = MemoryBuffer::getMemBuffer(StringRef(Buffer, size), "", false);
m = ParseBitcodeFile(Mem, Resource->getContext(), &error);
if (m == NULL) {
error.copy(errormsg, errormsg_size);
return 0;
}
if (Resource->LinkModule(m, &error)) {
error.copy(errormsg, errormsg_size);
return 0;
}
return 1;
}