-
Notifications
You must be signed in to change notification settings - Fork 0
/
codegen.cpp
201 lines (155 loc) · 7.12 KB
/
codegen.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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
#include "codegen.h"
namespace Tobsterlang {
auto Codegen::get_function_by_name(llvm::Module& module,
const std::string& name) {
auto function = module.getFunction(name);
if (function) {
return function;
}
auto lib_func_it = m_lib_funcs.find(name);
if (lib_func_it != m_lib_funcs.end()) {
return lib_func_it->second(module);
}
throw std::runtime_error("unknown function: " + name);
}
auto Codegen::get_type_by_name(const std::string& name) {
auto type = type_by_name[name];
if (type) {
return type(m_llvm_context);
}
throw std::runtime_error("unknown type: " + name);
}
auto Codegen::generate(ASTNode const& tree) -> std::unique_ptr<llvm::Module> {
auto root_node = tree.get_child("Root");
auto module_name = root_node.get_child("<xmlattr>.module").data();
auto module = std::make_unique<llvm::Module>(module_name, m_llvm_context);
std::function<std::vector<llvm::Value*>(Tobsterlang::ASTNode const&)>
recurse_tree =
[&](Tobsterlang::ASTNode const& tree) -> std::vector<llvm::Value*> {
if (tree.empty()) {
return {};
}
std::vector<llvm::Value*> ret;
for (auto& [node_name, subtree] : tree) {
if (node_name == "Func") {
std::string func_name;
llvm::Type* return_type = llvm::Type::getVoidTy(m_llvm_context);
std::vector<std::string> argument_names;
std::vector<llvm::Type*> argument_types;
for (auto& [argument_name, argument_node] :
subtree.get_child("<xmlattr>")) {
auto argument_value = argument_node.data();
if (argument_name == "name") {
func_name = argument_value;
continue;
}
if (argument_name == "returns") {
return_type = get_type_by_name(argument_value);
continue;
}
auto argument_type = get_type_by_name(argument_value);
argument_names.push_back(argument_name);
argument_types.push_back(argument_type);
}
if (func_name == "ZdraveitePriqteliAzSumTobstera") {
func_name = "main";
}
auto func_type =
llvm::FunctionType::get(return_type, argument_types, false);
auto func = llvm::Function::Create(
func_type, llvm::Function::ExternalLinkage, func_name,
module.get());
for (auto i = 0; i < argument_names.size(); ++i) {
func->getArg(i)->setName(argument_names[i]);
}
auto func_body =
llvm::BasicBlock::Create(m_llvm_context, "entry", func);
m_builder.SetInsertPoint(func_body);
m_named_values.clear();
for (auto& arg : func->args()) {
auto arg_mem = m_builder.CreateAlloca(arg.getType());
m_builder.CreateStore(&arg, arg_mem);
m_named_values[arg.getName().str()] = arg_mem;
}
auto children = recurse_tree(subtree);
auto has_return = subtree.rbegin()->first == "Return";
if (!has_return) {
if (return_type->isVoidTy() || children.empty()) {
m_builder.CreateRet(nullptr);
} else {
m_builder.CreateRet(children.back());
}
}
ret.push_back(func);
} else if (node_name == "Var") {
// TODO: Global variables
auto name = subtree.get_child("<xmlattr>.name").data();
auto type_node = subtree.get_child("<xmlattr>.type");
auto type = get_type_by_name(type_node.data());
auto var = m_builder.CreateAlloca(type);
// TODO: We should handle variable shadowing in the future
m_named_values[name] = var;
} else if (node_name == "Value") {
auto type_name = subtree.get_child("<xmlattr>.type").data();
auto value = Utils::unescape(subtree.data());
auto type = get_type_by_name(type_name);
if (type->isIntegerTy()) {
ret.push_back(llvm::ConstantInt::get(
m_llvm_context,
llvm::APInt(type->getIntegerBitWidth(), value, 10)));
} else if (type_name == "String") {
ret.push_back(m_builder.CreateGlobalStringPtr(value));
} else {
throw std::runtime_error("unknown type: " + type_name);
}
} else if (node_name == "Store") {
auto name = subtree.get_child("<xmlattr>.name").data();
auto var = m_named_values[name];
auto children = recurse_tree(subtree);
assert(children.size() == 1);
m_builder.CreateStore(children[0], var);
} else if (node_name == "Load") {
auto name = subtree.get_child("<xmlattr>.name").data();
auto var = m_named_values[name];
ret.push_back(m_builder.CreateLoad(var));
} else if (node_name == "Ref") {
auto name = subtree.get_child("<xmlattr>.name").data();
auto var = m_named_values[name];
ret.push_back(var);
} else if (node_name == "Add") {
auto values = recurse_tree(subtree);
assert(values.size() >= 2);
llvm::Value* sum = m_builder.CreateAdd(values[0], values[1]);
for (auto i = 2; i < values.size(); ++i) {
sum = m_builder.CreateAdd(sum, values[i]);
}
ret.push_back(sum);
} else if (node_name == "Sub") {
auto values = recurse_tree(subtree);
assert(values.size() >= 2);
llvm::Value* difference =
m_builder.CreateSub(values[0], values[1]);
for (auto i = 2; i < values.size(); ++i) {
difference = m_builder.CreateSub(difference, values[i]);
}
ret.push_back(difference);
} else if (node_name == "Return") {
auto values = recurse_tree(subtree);
if (values.size() == 1) {
m_builder.CreateRet(values[0]);
} else {
m_builder.CreateRet(nullptr);
}
} else if (node_name == "Call") {
auto name = subtree.get_child("<xmlattr>.name").data();
auto func = get_function_by_name(*module, name);
auto args = recurse_tree(subtree);
ret.push_back(m_builder.CreateCall(func, args));
}
}
return ret;
};
recurse_tree(root_node);
return module;
}
} // namespace Tobsterlang