-
Notifications
You must be signed in to change notification settings - Fork 0
/
compiler.cpp
327 lines (271 loc) · 9.72 KB
/
compiler.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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
// Copyright (c) 2009, Nicholas "Indy" Ray. All rights reserved.
// See the LICENSE file for usage, modification, and distribution terms.
#include "compiler_private.h"
#include "types.h"
#include "typeinfo.h"
#include "error.h"
#include <stdint.h>
#include <llvm/Support/raw_ostream.h>
#include <llvm/Support/raw_os_ostream.h>
#include <llvm/LLVMContext.h>
#include <fstream>
using namespace llvm;
extern symbol_table* sym_tbl;
compiler* init_compiler(symbol_table* table)
{
compiler* compile = new compiler();
compile->sym_table = table;
init_function_table(compile);
return compile;
}
void destroy_compiler(compiler* compile)
{
delete compile;
}
void compiler_error(const char* Error, ...)
{
va_list ap;
va_start(ap, Error);
printf("Error: Compiling: ");
vprintf(Error, ap);
putc('\n', stdout);
va_end(ap);
}
void compiler_error(pointer P, const char* Error, ...)
{
va_list ap;
va_start(ap, Error);
printf("Error Compiling at:\n");
print_object(P, sym_tbl);
printf("\nError: ");
putc('\n', stdout);
vprintf(Error, ap);
putc('\n', stdout);
va_end(ap);
}
llvm::Value* compiler_resolve_expression(compiler* compile, compile_block* block, pointer P);
llvm::Value* compiler_resolve_expression_list(compiler* compile, compile_block* block, pointer P);
compiler_scope* compiler_create_empty_scope(compiler_scope* parent_scope)
{
compiler_scope* scope = new compiler_scope();
scope->parent_scope = parent_scope;
return scope;
}
void compiler_destroy_scope(compiler_scope* scope)
{
delete scope;
}
void compiler_add_to_scope(compile_block* block, symbol sym, llvm::Value* val)
{
// Error on redef of variable at same scope... not dynamically bound in compiliation mode.
block->current_scope->scope_map[sym] = val;
}
llvm::Value* compiler_find_in_scope(compile_block* block, symbol sym)
{
compiler_scope* scope = block->current_scope;
while(scope != NULL)
{
std::map<symbol, llvm::Value*>::iterator it = scope->scope_map.find(sym);
if(it != scope->scope_map.end())
return it->second;
scope = scope->parent_scope;
}
return NULL;
}
std::vector<const Type*> compiler_populate_param_types(compiler* compile, pointer P)
{
std::vector<const Type*> ParamTypes;
while(P != NIL)
{
assert_cerror(is_type(P, DT_Pair) && is_type(pair_car(P), DT_Symbol) && is_type(cadr(P), DT_TypeInfo),
P, "Parameter list not well formed.");
ParamTypes.push_back(typeinfo_get_llvm_type(cadr(P)));
P = cddr(P);
}
return ParamTypes;
}
compile_block* compiler_create_function_block(compiler* compile, const char* Name, const llvm::Type* RetType, pointer Params, compile_block* parent_block)
{
std::vector<const Type*> ParamTypes = compiler_populate_param_types(compile, Params);
compile_block* block = new compile_block();
LLVMContext* Context;
if(parent_block)
Context = &parent_block->function->getContext();
else
Context = &compile->module->getContext();
if(!RetType)
RetType = llvm::Type::getVoidTy(*Context);
FunctionType* ft = FunctionType::get(RetType, ParamTypes, false);
Function* f;
Constant* c = compile->module->getFunction(Name);
if(c)
{
assert_cerror(isa<Function>(c), Params, "Constant '%s' already declared.", Name);
f = cast<Function>(c);
assert_cerror(f->arg_size() == ParamTypes.size(), Params, "Function '%s' already declared, mismatched params.", Name);
}
else
f = Function::Create(ft, Function::ExternalLinkage, Name, compile->module);
block->function = f;
block->function->setCallingConv(CallingConv::C);
compiler_scope* parent_scope = NULL;
if(parent_block)
parent_scope = parent_block->current_scope;
block->current_scope = compiler_create_empty_scope(parent_scope);
Function::arg_iterator args = block->function->arg_begin();
pointer P = Params;
while(P != NIL)
{
Value* v = args++;
assert_cerror(is_type(P, DT_Pair) && is_type(pair_car(P), DT_Symbol) && is_type(cadr(P), DT_TypeInfo),
P, "Parameter list for function '%s' not well formed.", Name);
symbol S = *get_symbol(pair_car(P));
v->setName(string_from_symbol(compile->sym_table, S));
compiler_add_to_scope(block, S, v);
P = cddr(P);
}
f->deleteBody();
block->block = BasicBlock::Create(*Context, "entry", block->function);
block->builder = new IRBuilder<>(block->block);
return block;
}
void compiler_destroy_function_block(compile_block* block)
{
// llvm should maintain it's own types, we just want to get rid of the block we pass around to build it.
compiler_destroy_scope(block->current_scope);
delete block->builder;
delete block;
}
compile_block* compiler_init_module(compiler* compile, const char* entry)
{
compile->module = new Module("", getGlobalContext());
return compiler_create_function_block(compile, entry);
}
llvm::Value* compiler_resolve_variable(compiler* compile, compile_block* block, symbol S)
{
Value* var = compiler_find_in_scope(block, S);
if(!var)
compiler_error("Undefined variable '%s'.", string_from_symbol(compile->sym_table, S));
return var;
}
llvm::Value* compiler_resolve_expression(compiler* compile, compile_block* block, pointer P)
{
switch(get_type_id(P))
{
case DT_Pair:
{
Function* function = NULL;
if(is_type(pair_car(P), DT_Symbol))
{
// Try to resolve special forms first!
symbol S = *get_symbol(pair_car(P));
compiler_special_form* fun = &compile->form_table[S];
if(fun && fun->special_form)
return (*fun->special_form)(compile, block, P);
}
Value* var = compiler_resolve_expression(compile, block, pair_car(P));
if(var && isa<Function>(var))
function = cast<Function>(var);
else
compiler_error(P, "Function not defined.");
SmallVector<Value*, 4> Params;
P = pair_cdr(P);
while(P != NIL)
{
Params.push_back(compiler_resolve_expression(compile, block, pair_car(P)));
if(is_type(P, DT_Pair))
P = pair_cdr(P);
else
P = NIL;
}
return block->builder->CreateCall(function, Params.begin(), Params.end());
}
case DT_Symbol:
{
// Resolve variables
Value* var = compiler_resolve_variable(compile, block, *get_symbol(P));
assert_cerror(var, P, "Undefined variable");
return var;
}
case DT_Int:
return ConstantInt::get(Type::getInt32Ty(block->function->getContext()), APInt(32, get_int(P), true));
case DT_Real:
return ConstantFP::get(block->function->getContext(), APFloat(get_real(P)));
case DT_String:
{
// The Parser currentlly only supports ASCII, but use UTF-8.
const char* Curr = get_string(P);
size_t strlength = strlen(Curr);
std::vector<Constant*> StringList;
StringList.reserve(strlength+1);
do
{
// TODO: Either convert from extended ascii to UTF-8, or use UTF-8 all through
// the parser.40
assert(*Curr >= 0 && "Conversion from extended ascii and UTF-8 not supported");
StringList.push_back(
ConstantInt::get(Type::getInt8Ty(block->function->getContext()), (uint64_t)*Curr, false));
}
while(*(Curr++) != '\0');
llvm::ArrayType* ArrType = ArrayType::get(Type::getInt8Ty(block->function->getContext()),
StringList.size());
Constant* Arr = ConstantArray::get(
ArrType,
StringList);
GlobalVariable* gv = new GlobalVariable(*compile->module,
ArrType,
true,
GlobalValue::InternalLinkage,
Arr,
llvm::Twine(""),
0,
false);
llvm::Value* V = block->builder->CreateConstInBoundsGEP2_64(gv, 0, 0);
V->getType()->dump();
return V;
}
case DT_Char:
return ConstantInt::get(Type::getInt8Ty(block->function->getContext()), APInt(8, get_char(P), false));
default:
compiler_error(P, "Uncompilable expression in AST.");
break;
}
}
llvm::Value* compiler_resolve_expression_list(compiler* compile, compile_block* block, pointer P)
{
llvm::Value* LastExp = NULL;
while(P != NIL)
{
if(is_type(P, DT_Pair))
{
LastExp = compiler_resolve_expression(compile, block, pair_car(P));
P = pair_cdr(P);
}
else
P = NIL;
}
return LastExp;
}
void compiler_compile_expression(compiler* compile, pointer P, const char* entry)
{
compile_block* block = compiler_init_module(compile, entry);
compiler_resolve_expression_list(compile, block, P);
block->builder->CreateRetVoid();
compiler_destroy_function_block(block);
// Seperate the rest of this into pass and run modules.
verifyModule(*compile->module, PrintMessageAction);
}
void compiler_print_module(compiler* compile)
{
PassManager PM;
PM.add(createPrintModulePass(&llvm::outs()));
PM.run(*compile->module);
}
void compiler_write_asm_file(compiler* compile, const char* output_filename)
{
std::ofstream out_file(output_filename);
llvm::raw_os_ostream raw_out(out_file);
PassManager PM;
PM.add(createPrintModulePass(&raw_out));
PM.run(*compile->module);
}