This repository was archived by the owner on Jan 8, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathBuiltin.cpp
More file actions
144 lines (131 loc) · 7.11 KB
/
Builtin.cpp
File metadata and controls
144 lines (131 loc) · 7.11 KB
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
132
133
134
135
136
137
138
139
140
141
142
143
144
#include "CodeGen/Builtin.h"
void Builtin::initialize(LLVMModule &module, LLVMContext &context, const llvm::Twine &libDirArg) {
llvm::SmallString<256> libPath;
if (!libDirArg.str().empty()) {
libDirArg.toVector(libPath);
if (!llvm::sys::path::is_absolute(libPath)) {
if (llvm::sys::fs::make_absolute(libPath)) {
reportLinkError("Parse the specified library directory failed");
}
}
if (!llvm::sys::fs::exists(libPath)) {
reportLinkError("The specified library directory does not exist");
}
if (!llvm::sys::fs::is_directory(libPath)) {
reportLinkError("The specified library directory is not a directory");
}
libDir = libPath.str();
} else {
// 在当前执行目录下搜索lib目录
llvm::sys::fs::current_path(libPath);
llvm::sys::path::append(libPath, "lib");
if (llvm::sys::fs::exists(libPath) && llvm::sys::fs::is_directory(libPath)) {
libDir = libPath.str();
}
}
if (!llvm::sys::fs::exists(libDir)) {
reportLinkError("The library directory '" + libDir + "'does not exist");
}
BuiltinError::linkModule(module, context);
BuiltinString::linkModule(module, context);
BuiltinArray::linkModule(module, context);
BuiltinIO::linkModule(module, context);
BuiltinError::getTypeAndFunction(module);
BuiltinString::getTypeAndFunction(module);
BuiltinArray::getTypeAndFunction(module);
BuiltinIO::getTypeAndFunction(module);
}
#define BUILTIN_LINK_MODULE(moduleName) \
llvm::SMDiagnostic error; \
String bitcodeFilename = Builtin::libDir + "/ss_"#moduleName".bc"; \
bool bitcodeExist = llvm::sys::fs::exists(bitcodeFilename); \
reportOnLinkError(!bitcodeExist, "Not found " + bitcodeFilename); \
std::unique_ptr<LLVMModule> bitcodeModule = llvm::parseIRFile(bitcodeFilename, error, context); \
reportOnLinkError(!bitcodeModule, "Parse bitcode file of the "#moduleName" module failed"); \
bool linkFailed = llvm::Linker::linkModules(module, std::move(bitcodeModule)); \
reportOnLinkError(linkFailed, "Link "#moduleName" module failed");
void BuiltinError::linkModule(LLVMModule &module, LLVMContext &context) {
BUILTIN_LINK_MODULE(error)
}
void BuiltinError::getTypeAndFunction(LLVMModule &module) {
llvm::StructType *errStructType = llvm::StructType::getTypeByName(module.getContext(), "struct.ss_error");
type = errStructType->getPointerTo();
exitIfErrorFunc = module.getFunction("ss_exit_if_error");
assertFunc = module.getFunction("ss_assert");
}
void BuiltinString::linkModule(LLVMModule &module, LLVMContext &context) {
BUILTIN_LINK_MODULE(string)
}
void BuiltinString::getTypeAndFunction(LLVMModule &module) {
llvm::StructType *strStructType = llvm::StructType::getTypeByName(module.getContext(), "struct.ss_string");
type = strStructType->getPointerTo();
createFunc = module.getFunction("ss_string_create");
deleteFunc = module.getFunction("ss_string_delete");
getSizeFunc = module.getFunction("ss_string_get_size");
concatFunc = module.getFunction("ss_string_concat");
appendFunc = module.getFunction("ss_string_append");
prependFunc = module.getFunction("ss_string_prepend");
sliceFunc = module.getFunction("ss_string_slice");
equalsFunc = module.getFunction("ss_string_equals");
indexOfFunc = module.getFunction("ss_string_index_of");
trimLeftFunc = module.getFunction("ss_string_trim_left");
trimRightFunc = module.getFunction("ss_string_trim_right");
trimFunc = module.getFunction("ss_string_trim");
}
void BuiltinArray::linkModule(LLVMModule &module, LLVMContext &context) {
BUILTIN_LINK_MODULE(array)
}
void BuiltinArray::getTypeAndFunction(LLVMModule &module) {
llvm::StructType *arrStructType = llvm::StructType::getTypeByName(module.getContext(), "struct.ss_array");
type = arrStructType->getPointerTo();
createIntegerArrayFunc = module.getFunction("ss_array_create_integer_array");
createFloatArrayFunc = module.getFunction("ss_array_create_float_array");
createBooleanArrayFunc = module.getFunction("ss_array_create_boolean_array");
createStringArrayFunc = module.getFunction("ss_array_create_string_array");
createArrayArrayFunc = module.getFunction("ss_array_create_array_array");
createIntegerArrayWithLiteralFunc = module.getFunction("ss_array_create_integer_array_with_literal");
createFloatArrayWithLiteralFunc = module.getFunction("ss_array_create_float_array_with_literal");
createBooleanArrayWithLiteralFunc = module.getFunction("ss_array_create_boolean_array_with_literal");
createStringArrayWithLiteralFunc = module.getFunction("ss_array_create_string_array_with_literal");
createArrayArrayWithLiteralFunc = module.getFunction("ss_array_create_array_array_with_literal");
deleteFunc = module.getFunction("ss_array_delete");
getSizeFunc = module.getFunction("ss_array_get_size");
isNDArrayFunc = module.getFunction("ss_array_is_nd_array");
isFloatArrayFunc = module.getFunction("ss_array_is_float_array");
pushIntegerFunc = module.getFunction("ss_array_push_integer");
pushFloatFunc = module.getFunction("ss_array_push_float");
pushBooleanFunc = module.getFunction("ss_array_push_boolean");
pushStringFunc = module.getFunction("ss_array_push_string");
pushArrayFunc = module.getFunction("ss_array_push_array");
popIntegerFunc = module.getFunction("ss_array_pop_integer");
popFloatFunc = module.getFunction("ss_array_pop_float");
popBooleanFunc = module.getFunction("ss_array_pop_boolean");
popStringFunc = module.getFunction("ss_array_pop_string");
popArrayFunc = module.getFunction("ss_array_pop_array");
getIntegerFunc = module.getFunction("ss_array_get_integer");
getFloatFunc = module.getFunction("ss_array_get_float");
getBooleanFunc = module.getFunction("ss_array_get_boolean");
getStringFunc = module.getFunction("ss_array_get_string");
getArrayFunc = module.getFunction("ss_array_get_array");
setIntegerFunc = module.getFunction("ss_array_set_integer");
setFloatFunc = module.getFunction("ss_array_set_float");
setBooleanFunc = module.getFunction("ss_array_set_boolean");
setStringFunc = module.getFunction("ss_array_set_string");
setArrayFunc = module.getFunction("ss_array_set_array");
sliceFunc = module.getFunction("ss_array_slice");
}
void BuiltinIO::linkModule(LLVMModule &module, LLVMContext &context) {
BUILTIN_LINK_MODULE(io)
}
void BuiltinIO::getTypeAndFunction(LLVMModule &module) {
integer2stringFunc = module.getFunction("ss_integer2string");
string2integerFunc = module.getFunction("ss_string2integer");
float2stringFunc = module.getFunction("ss_float2string");
string2floatFunc = module.getFunction("ss_string2float");
printIntegerFunc = module.getFunction("ss_print_integer");
printlnIntegerFunc = module.getFunction("ss_println_integer");
printFloatFunc = module.getFunction("ss_print_float");
printlnFloatFunc = module.getFunction("ss_println_float");
printStringFunc = module.getFunction("ss_print_string");
printlnStringFunc = module.getFunction("ss_println_string");
}