This repository has been archived by the owner on Jan 12, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcompiler.rs
410 lines (357 loc) · 13.2 KB
/
compiler.rs
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
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
use deno_ast::swc::ast::{
CallExpr, Callee, Decl, Expr, Lit, MemberExpr, ModuleItem, Pat, Program, Script, Stmt, VarDecl,
};
use inkwell::builder::Builder;
use inkwell::context::Context;
use inkwell::module::Module;
use inkwell::types::AnyTypeEnum;
use inkwell::values::{AnyValue, AnyValueEnum, BasicValue, FunctionValue, PointerValue};
use std::cell::RefCell;
use std::collections::HashMap;
use std::error::Error;
use std::fmt::{Debug, Display, Formatter};
use std::path::Path;
#[derive(Debug)]
pub struct CompilerError {
message: String,
}
impl Display for CompilerError {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
write!(f, "An error occurred while compiling: '{}'", self.message)
}
}
impl Error for CompilerError {}
enum VariableKind {
Const,
Let,
Var,
}
impl VariableKind {
fn from_string(string: String) -> Self {
match string.as_str() {
"const" => VariableKind::Const,
"let" => VariableKind::Let,
"var" => VariableKind::Var,
_ => panic!("Invalid variable kind"),
}
}
}
struct Variable<'ctx> {
pointer: PointerValue<'ctx>,
kind: VariableKind,
}
pub(crate) struct Compiler<'a, 'ctx> {
context: &'a Context,
module: Module<'ctx>,
builder: Builder<'ctx>,
variables: RefCell<HashMap<String, Variable<'ctx>>>,
main_fn: Option<FunctionValue<'ctx>>,
}
impl<'a: 'ctx, 'ctx> Compiler<'a, 'ctx> {
pub fn new(context: &'a Context, source_path: &str) -> Self {
let file = Path::new(source_path);
let filename = file.file_name().unwrap().to_str().unwrap();
Self {
context,
module: context.create_module(filename),
builder: context.create_builder(),
variables: RefCell::new(HashMap::new()),
main_fn: None,
}
}
pub fn get_llvm_ir(&self) -> String {
self.module.print_to_string().to_string()
}
fn add_variable(&self, name: String, pointer: PointerValue<'ctx>, kind: VariableKind) {
self.variables
.borrow_mut()
.insert(name, Variable { pointer, kind });
}
pub fn show_variables(&self) {
println!("Variables:");
for (_name, variable) in self.variables.borrow().iter() {
println!("{}", variable.pointer.print_to_string().to_string());
}
println!();
}
pub fn write_to_file(&self, path: &str) -> Result<(), Box<dyn Error>> {
self.module.print_to_file(path)?;
Ok(())
}
#[inline]
fn main_fn(&self) -> FunctionValue<'ctx> {
self.main_fn.unwrap()
}
fn null_pointer(&self) -> PointerValue<'ctx> {
self.context
.i8_type()
.ptr_type(inkwell::AddressSpace::Generic)
.const_null()
}
pub fn compile(&mut self, program: &Program) -> Result<(), CompilerError> {
if self.main_fn.is_none() {
return Err(CompilerError {
message: "No main function found".to_string(),
});
}
match program {
Program::Module(module) => self.compile_module(module)?,
Program::Script(script) => self.compile_script(script)?,
}
self.compile_main_function_return();
Ok(())
}
fn compile_main_function_return(&self) {
self.builder
.build_return(Some(&self.context.i32_type().const_zero()));
}
pub fn compile_main_function(&mut self) -> Result<(), CompilerError> {
let i32_type = self.context.i32_type();
let fn_type = i32_type.fn_type(&[], false);
let main_fn = self.module.add_function("main", fn_type, None);
let entry = self.context.append_basic_block(main_fn, "entry");
self.builder.position_at_end(entry);
self.main_fn = Some(main_fn);
Ok(())
}
fn compile_script(&mut self, script: &Script) -> Result<(), CompilerError> {
(&script.body)
.into_iter()
.try_for_each(|stmt| self.compile_statement(stmt))
}
fn compile_module(&mut self, module: &deno_ast::swc::ast::Module) -> Result<(), CompilerError> {
(&module.body)
.into_iter()
.try_for_each(|item| self.compile_module_item(item))
}
fn compile_module_item(&mut self, item: &ModuleItem) -> Result<(), CompilerError> {
match item {
ModuleItem::ModuleDecl(_) => {
return Err(CompilerError {
message: "Module declarations are not supported".to_string(),
});
}
ModuleItem::Stmt(stmt) => self.compile_statement(stmt),
}
}
fn compile_statement(&self, statement: &Stmt) -> Result<(), CompilerError> {
match statement {
Stmt::Expr(expr) => self.compile_expression(expr.expr.as_ref()).map(|_| ()),
Stmt::Decl(decl) => self.compile_declaration(decl),
_ => {
return Err(CompilerError {
message: "Unsupported statement".to_string(),
});
}
}
}
fn compile_expression(&self, expression: &Expr) -> Result<AnyValueEnum<'ctx>, CompilerError> {
match expression {
Expr::Call(call) => self.compile_call_expression(call),
Expr::Member(member) => self.compile_member_expression(member),
Expr::Lit(lit) => self.compile_literal(lit),
_ => {
return Err(CompilerError {
message: "Unsupported expression".to_string(),
});
}
}
}
fn compile_call_expression(
&self,
call_expression: &CallExpr,
) -> Result<AnyValueEnum<'ctx>, CompilerError> {
let callee = &call_expression.callee;
let callee = match callee {
Callee::Expr(expr) => self.compile_expression(expr),
_ => {
return Err(CompilerError {
message: "Unsupported callee".to_string(),
});
}
};
if let Err(err) = callee {
return Err(err);
}
let callee = callee.unwrap();
match callee {
AnyValueEnum::FunctionValue(_) => {
let arguments = (&call_expression.args)
.into_iter()
.map(|argument| self.compile_expression(argument.expr.as_ref()))
.collect::<Result<Vec<_>, _>>()?;
let argument = arguments.first().unwrap();
match argument {
AnyValueEnum::PointerValue(pointer) => {
let bitcast = self.builder.build_bitcast(
pointer.as_basic_value_enum(),
self.context
.i8_type()
.ptr_type(inkwell::AddressSpace::Generic),
"console_log_argument_1",
);
self.builder.build_call(
callee.into_function_value(),
&[bitcast.into()],
"console_log",
);
}
_ => {
return Err(CompilerError {
message: "Unsupported argument".to_string(),
});
}
}
}
_ => {
return Err(CompilerError {
message: "Unsupported callee".to_string(),
});
}
}
// returns null pointer
Ok(self.null_pointer().into())
}
fn compile_member_expression(
&self,
member_expression: &MemberExpr,
) -> Result<AnyValueEnum<'ctx>, CompilerError> {
// TODO: should not be hardcoded
// let object = self.compile_expression(member_expression.obj.as_ref())?;
let object = member_expression.obj.as_ident().unwrap();
let prop = &member_expression.prop;
let prop = prop.as_ident().unwrap();
let is_console_log = object.sym.to_string() == "console" && prop.sym.to_string() == "log";
if is_console_log {
let i32_type = self.context.i32_type();
let puts_type = i32_type.fn_type(
&[self
.context
.i8_type()
.ptr_type(inkwell::AddressSpace::Generic)
.into()],
true,
);
let puts = self.module.add_function("puts", puts_type, None);
return Ok(puts.into());
}
Err(CompilerError {
message: "Unsupported expression".to_string(),
})
}
fn compile_literal(&self, literal: &Lit) -> Result<AnyValueEnum<'ctx>, CompilerError> {
match literal {
Lit::Str(string) => {
let string_pointer = self.context.const_string((&string.value).as_bytes(), false);
Ok(string_pointer.as_any_value_enum())
}
Lit::Num(number) => {
let number_value = number.value;
let f64_type = self.context.f64_type();
let const_float = f64_type.const_float(number_value);
Ok(const_float.as_any_value_enum())
}
Lit::Bool(boolean) => {
let bool_type = self.context.bool_type();
let const_bool = bool_type.const_int(boolean.value.into(), false);
Ok(const_bool.as_any_value_enum())
}
Lit::Null(_) => Ok(self.null_pointer().into()),
_ => {
return Err(CompilerError {
message: "Unsupported literal".to_string(),
});
}
}
}
fn compile_declaration(&self, declaration: &Decl) -> Result<(), CompilerError> {
match declaration {
Decl::Var(var) => self.compile_variable_declaration(var),
_ => {
return Err(CompilerError {
message: "Unsupported declaration".to_string(),
});
}
}
}
fn compile_variable_declaration(
&self,
variable_declaration: &Box<VarDecl>,
) -> Result<(), CompilerError> {
let declarations = &variable_declaration.decls;
let kind = &variable_declaration.kind;
for declaration in declarations {
let name = self.compile_pattern(&declaration.name)?;
let init = self.compile_expression(&declaration.init.as_ref().unwrap())?;
let kind = VariableKind::from_string(kind.to_string());
let pointer = self.allocate_variable(&name, &init, &kind)?;
self.add_variable(name, pointer, kind);
}
Ok(())
}
fn allocate_variable(
&self,
variable_name: &String,
init_value: &AnyValueEnum<'ctx>,
_variable_kind: &VariableKind,
) -> Result<PointerValue<'ctx>, CompilerError> {
let variable_type = init_value.get_type();
let variable_value = match variable_type {
AnyTypeEnum::ArrayType(array) => {
let pointer = self.builder.build_alloca(array, &variable_name);
self.builder
.build_store(pointer, init_value.into_array_value());
pointer
}
AnyTypeEnum::FloatType(float) => {
let pointer = self.builder.build_alloca(float, &variable_name);
self.builder
.build_store(pointer, init_value.into_float_value());
pointer
}
AnyTypeEnum::IntType(int) => {
let pointer = self.builder.build_alloca(int, &variable_name);
self.builder
.build_store(pointer, init_value.into_int_value());
pointer
}
AnyTypeEnum::PointerType(pointer) => {
let pointer = self.builder.build_alloca(pointer, &variable_name);
self.builder
.build_store(pointer, init_value.into_pointer_value());
pointer
}
AnyTypeEnum::StructType(structt) => {
let pointer = self.builder.build_alloca(structt, &variable_name);
self.builder
.build_store(pointer, init_value.into_struct_value());
pointer
}
AnyTypeEnum::VectorType(vector) => {
let pointer = self.builder.build_alloca(vector, &variable_name);
self.builder
.build_store(pointer, init_value.into_vector_value());
pointer
}
_ => {
return Err(CompilerError {
message: "Unsupported variable type".to_string(),
});
}
};
Ok(variable_value)
}
fn compile_pattern(&self, pattern: &Pat) -> Result<String, CompilerError> {
match pattern {
Pat::Ident(ident) => {
let name = ident.sym.to_string();
Ok(name)
}
_ => {
return Err(CompilerError {
message: "Unsupported pattern".to_string(),
});
}
}
}
}