2019-09-21 02:40:21 +00:00
|
|
|
const std = @import("std");
|
|
|
|
const ast = @import("ast.zig");
|
|
|
|
const llvm = @import("llvm.zig");
|
2019-09-25 14:29:47 +00:00
|
|
|
const comp = @import("comp_ctx.zig");
|
2019-09-27 03:00:06 +00:00
|
|
|
// const analysis = @import("analysis.zig");
|
2019-09-21 02:40:21 +00:00
|
|
|
|
2019-09-22 02:56:22 +00:00
|
|
|
fn sliceify(non_slice: ?[*]const u8) []const u8 {
|
|
|
|
if (non_slice == null) return "";
|
|
|
|
return non_slice.?[0..std.mem.len(u8, non_slice.?)];
|
|
|
|
}
|
|
|
|
|
2019-09-24 01:07:19 +00:00
|
|
|
pub const CompileError = error{
|
|
|
|
LLVMError,
|
|
|
|
EmitError,
|
2019-09-24 16:23:23 +00:00
|
|
|
TypeError,
|
2019-09-24 01:07:19 +00:00
|
|
|
};
|
2019-09-22 21:26:00 +00:00
|
|
|
|
2019-09-24 17:12:37 +00:00
|
|
|
fn mkLLVMBool(val: bool) llvm.LLVMValueRef {
|
|
|
|
if (val) {
|
|
|
|
return llvm.LLVMConstInt(llvm.LLVMInt1Type(), 1, 1);
|
|
|
|
} else {
|
|
|
|
return llvm.LLVMConstInt(llvm.LLVMInt1Type(), 0, 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-25 21:05:56 +00:00
|
|
|
pub const LLVMTable = std.StringHashMap(llvm.LLVMValueRef);
|
|
|
|
pub const LLVMValueList = std.ArrayList(llvm.LLVMValueRef);
|
|
|
|
|
2019-09-21 02:40:21 +00:00
|
|
|
pub const Codegen = struct {
|
|
|
|
allocator: *std.mem.Allocator,
|
2019-09-25 14:29:47 +00:00
|
|
|
ctx: *comp.CompilationContext,
|
2019-09-25 21:05:56 +00:00
|
|
|
llvm_table: LLVMTable,
|
2019-09-25 14:29:47 +00:00
|
|
|
|
2019-09-27 23:39:16 +00:00
|
|
|
current_function_name: ?[]const u8 = null,
|
|
|
|
|
2019-09-25 14:29:47 +00:00
|
|
|
pub fn init(allocator: *std.mem.Allocator, ctx: *comp.CompilationContext) Codegen {
|
2019-09-25 21:05:56 +00:00
|
|
|
return Codegen{
|
|
|
|
.allocator = allocator,
|
|
|
|
.ctx = ctx,
|
|
|
|
.llvm_table = LLVMTable.init(allocator),
|
|
|
|
};
|
2019-09-25 14:29:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn typeToLLVM(self: *@This(), typ: comp.SymbolUnderlyingType) !llvm.LLVMTypeRef {
|
|
|
|
return switch (typ) {
|
|
|
|
.Integer32 => llvm.LLVMInt32Type(),
|
|
|
|
.Integer64 => llvm.LLVMInt64Type(),
|
|
|
|
.Bool => llvm.LLVMInt1Type(),
|
|
|
|
|
|
|
|
.OpaqueType => |val| {
|
|
|
|
std.debug.warn("Invalid return type: {}\n", val);
|
|
|
|
return CompileError.TypeError;
|
|
|
|
},
|
|
|
|
|
|
|
|
.Struct, .Enum => |lex| blk: {
|
|
|
|
var sym_data = self.ctx.symbol_table.get(lex).?.value;
|
2019-09-28 16:17:11 +00:00
|
|
|
break :blk switch (sym_data.*) {
|
2019-09-25 14:29:47 +00:00
|
|
|
.Struct => unreachable,
|
|
|
|
.Enum => llvm.LLVMInt32Type(),
|
|
|
|
else => {
|
|
|
|
std.debug.warn("Function {} is not a type\n", lex);
|
|
|
|
return CompileError.TypeError;
|
|
|
|
},
|
|
|
|
};
|
|
|
|
},
|
2019-09-21 02:40:21 +00:00
|
|
|
|
2019-09-25 14:29:47 +00:00
|
|
|
else => {
|
|
|
|
std.debug.warn("TODO handle {}\n", typ);
|
|
|
|
return CompileError.TypeError;
|
|
|
|
},
|
|
|
|
};
|
2019-09-21 02:40:21 +00:00
|
|
|
}
|
|
|
|
|
2019-09-27 20:18:38 +00:00
|
|
|
fn emitForVariableType(self: *@This(), vari: var, get: var, kv: var) !llvm.LLVMValueRef {
|
|
|
|
var sym = kv.value;
|
|
|
|
|
2019-09-28 16:17:11 +00:00
|
|
|
switch (sym.*) {
|
2019-09-27 20:18:38 +00:00
|
|
|
.Enum => |map| {
|
|
|
|
var val = map.get(get.name.lexeme);
|
|
|
|
if (val == null) {
|
|
|
|
std.debug.warn(
|
|
|
|
"enum {} does not have field {}\n",
|
|
|
|
vari.lexeme,
|
|
|
|
get.name.lexeme,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
return llvm.LLVMConstInt(llvm.LLVMInt32Type(), val.?.value, 1);
|
|
|
|
},
|
|
|
|
|
|
|
|
.Struct => @panic("TODO handle struct"),
|
|
|
|
else => {
|
2019-09-28 16:17:11 +00:00
|
|
|
std.debug.warn("Invalid get target: {}\n", comp.SymbolType(sym.*));
|
2019-09-27 20:18:38 +00:00
|
|
|
return CompileError.EmitError;
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-25 21:05:56 +00:00
|
|
|
fn emitExpr(
|
|
|
|
self: *Codegen,
|
|
|
|
builder: var,
|
|
|
|
expr: *const ast.Expr,
|
|
|
|
) anyerror!llvm.LLVMValueRef {
|
2019-09-23 21:33:30 +00:00
|
|
|
// TODO if expr is Variable, we should do a variable lookup
|
|
|
|
// in a symbol table, going up in scope, etc.
|
|
|
|
|
|
|
|
// TODO Assign modify symbol table
|
2019-09-24 01:07:19 +00:00
|
|
|
return switch (expr.*) {
|
2019-09-23 21:33:30 +00:00
|
|
|
|
|
|
|
// TODO handle all literals, construct llvm values for them
|
2019-09-24 01:07:19 +00:00
|
|
|
.Literal => |literal| blk: {
|
|
|
|
break :blk switch (literal) {
|
|
|
|
// TODO other literals
|
2019-09-27 15:07:20 +00:00
|
|
|
.Integer32 => |val| llvm.LLVMConstInt(
|
|
|
|
llvm.LLVMInt32Type(),
|
|
|
|
@intCast(c_ulonglong, val),
|
|
|
|
10,
|
|
|
|
),
|
|
|
|
.Integer64 => |val| llvm.LLVMConstInt(
|
|
|
|
llvm.LLVMInt64Type(),
|
|
|
|
@intCast(c_ulonglong, val),
|
|
|
|
10,
|
|
|
|
),
|
|
|
|
|
2019-09-24 01:07:19 +00:00
|
|
|
.Float => |val| blk2: {
|
|
|
|
var val_cstr = try std.cstr.addNullByte(self.allocator, val);
|
|
|
|
break :blk2 llvm.LLVMConstRealOfString(llvm.LLVMDoubleType(), val_cstr.ptr);
|
|
|
|
},
|
2019-09-24 16:23:23 +00:00
|
|
|
.Bool => |val| blk2: {
|
2019-09-24 17:12:37 +00:00
|
|
|
break :blk2 mkLLVMBool(val);
|
2019-09-24 16:23:23 +00:00
|
|
|
},
|
2019-09-24 01:07:19 +00:00
|
|
|
else => unreachable,
|
|
|
|
};
|
|
|
|
},
|
2019-09-24 16:23:23 +00:00
|
|
|
|
2019-09-23 21:33:30 +00:00
|
|
|
.Binary => |binary| {
|
2019-09-24 01:07:19 +00:00
|
|
|
var left = try self.emitExpr(builder, binary.left);
|
|
|
|
var right = try self.emitExpr(builder, binary.right);
|
2019-09-23 21:33:30 +00:00
|
|
|
|
2019-09-24 02:18:31 +00:00
|
|
|
return switch (binary.op) {
|
|
|
|
.Add => llvm.LLVMBuildAdd(builder, left, right, c"addtmp"),
|
|
|
|
.Sub => llvm.LLVMBuildSub(builder, left, right, c"subtmp"),
|
|
|
|
.Mul => llvm.LLVMBuildMul(builder, left, right, c"multmp"),
|
|
|
|
|
|
|
|
//.Div => llvm.LLVMBuildDiv(builder, left, right, c"divtmp"),
|
2019-09-24 16:23:23 +00:00
|
|
|
.And => llvm.LLVMBuildAnd(builder, left, right, c"andtmp"),
|
|
|
|
.Or => llvm.LLVMBuildOr(builder, left, right, c"ortmp"),
|
2019-09-23 21:33:30 +00:00
|
|
|
|
2019-09-24 01:07:19 +00:00
|
|
|
else => {
|
2019-09-24 02:18:31 +00:00
|
|
|
std.debug.warn("Unexpected binary operator: '{}'\n", binary.op);
|
2019-09-24 01:07:19 +00:00
|
|
|
return CompileError.EmitError;
|
|
|
|
},
|
2019-09-23 21:33:30 +00:00
|
|
|
};
|
|
|
|
},
|
|
|
|
|
2019-09-25 15:28:22 +00:00
|
|
|
.Get => |get| {
|
2019-09-26 21:03:39 +00:00
|
|
|
var target = get.target.*;
|
2019-09-25 15:28:22 +00:00
|
|
|
|
2019-09-26 21:03:39 +00:00
|
|
|
switch (target) {
|
2019-09-25 15:28:22 +00:00
|
|
|
.Variable => |vari| {
|
|
|
|
// first, we must check if the target is a type
|
|
|
|
// and emit accordingly
|
|
|
|
var kv_sym_opt = self.ctx.symbol_table.get(vari.lexeme);
|
|
|
|
if (kv_sym_opt) |kv| {
|
2019-09-27 20:18:38 +00:00
|
|
|
return try self.emitForVariableType(vari, get, kv);
|
2019-09-25 15:28:22 +00:00
|
|
|
}
|
|
|
|
|
2019-09-27 20:18:38 +00:00
|
|
|
// if not, its likely a variable, we should handle it accordingly
|
2019-09-25 15:28:22 +00:00
|
|
|
// as well
|
|
|
|
@panic("TODO handle variables");
|
|
|
|
},
|
|
|
|
|
|
|
|
else => {
|
2019-09-26 21:03:39 +00:00
|
|
|
std.debug.warn("Invalid get target: {}\n", ast.ExprType(target));
|
2019-09-25 15:28:22 +00:00
|
|
|
return CompileError.EmitError;
|
|
|
|
},
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2019-09-25 21:05:56 +00:00
|
|
|
.Call => |call| {
|
|
|
|
const name = call.callee.*.Variable.lexeme;
|
|
|
|
|
|
|
|
var llvm_func = self.llvm_table.get(name);
|
|
|
|
if (llvm_func == null) {
|
|
|
|
std.debug.warn("Function '{}' not found\n", name);
|
|
|
|
return CompileError.EmitError;
|
|
|
|
}
|
|
|
|
|
|
|
|
var args = LLVMValueList.init(self.allocator);
|
|
|
|
errdefer args.deinit();
|
|
|
|
|
|
|
|
for (call.arguments.toSlice()) |arg_expr| {
|
|
|
|
var arg_val = try self.emitExpr(builder, &arg_expr);
|
|
|
|
try args.append(arg_val);
|
|
|
|
}
|
|
|
|
|
|
|
|
var args_slice = args.toSlice();
|
|
|
|
|
|
|
|
return llvm.LLVMBuildCall(
|
|
|
|
builder,
|
|
|
|
llvm_func.?.value,
|
|
|
|
args_slice.ptr,
|
|
|
|
@intCast(c_uint, args_slice.len),
|
|
|
|
c"call",
|
|
|
|
);
|
|
|
|
},
|
|
|
|
|
2019-09-26 03:11:48 +00:00
|
|
|
// TODO finish this
|
|
|
|
.Assign => |assign| {
|
|
|
|
// TODO find assign.name on the "parent context", we should have
|
|
|
|
// a way to do name resolution that is completely relative to
|
|
|
|
// where we currently are, and go up in scope. so that we find
|
|
|
|
// the LLVMValueRef.
|
|
|
|
|
|
|
|
// we will also need to repeat the step for the type resolver
|
|
|
|
|
|
|
|
//var typ = self.findCurrent(assign.name);
|
|
|
|
var assign_expr = try self.emitExpr(builder, assign.value);
|
|
|
|
|
|
|
|
// TODO rm null
|
|
|
|
return llvm.LLVMBuildStore(builder, null, assign_expr);
|
|
|
|
},
|
|
|
|
|
2019-09-27 23:39:16 +00:00
|
|
|
.Variable => |vari| {
|
|
|
|
var kv_opt = self.ctx.metadata_map.get(expr);
|
|
|
|
|
|
|
|
if (kv_opt == null) {
|
|
|
|
std.debug.warn("variable {} not fully analyzed\n", vari);
|
|
|
|
return CompileError.EmitError;
|
|
|
|
}
|
|
|
|
|
|
|
|
// we have metadata, which means we can check if the variable
|
|
|
|
// is coming from the scope or from the function
|
|
|
|
|
|
|
|
var metadata = kv_opt.?.value;
|
|
|
|
|
|
|
|
return switch (metadata.using) {
|
|
|
|
.Function => blk: {
|
|
|
|
var param = metadata.from_function.?.parameters.get(vari.lexeme).?.value;
|
2019-09-28 15:46:23 +00:00
|
|
|
// var llvm_func = self.llvm_table.get(self.current_function_name.?).?.value;
|
|
|
|
// break :blk llvm.LLVMGetParam(llvm_func, @intCast(c_uint, param.idx));
|
2019-09-27 23:39:16 +00:00
|
|
|
|
2019-09-28 16:17:11 +00:00
|
|
|
std.debug.warn("fn param alloca {} {}\n", param.name, param.llvm_alloca);
|
|
|
|
break :blk param.llvm_alloca.?;
|
2019-09-27 23:39:16 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
.Scope => @panic("TODO local variables"),
|
|
|
|
};
|
|
|
|
},
|
2019-09-27 20:18:38 +00:00
|
|
|
|
2019-09-24 01:07:19 +00:00
|
|
|
else => {
|
2019-09-25 15:28:22 +00:00
|
|
|
std.debug.warn("Got unexpected expr {}\n", ast.ExprType(expr.*));
|
2019-09-24 01:07:19 +00:00
|
|
|
return CompileError.EmitError;
|
|
|
|
},
|
|
|
|
};
|
2019-09-23 21:33:30 +00:00
|
|
|
}
|
|
|
|
|
2019-09-28 01:49:33 +00:00
|
|
|
fn emitStmt(self: *Codegen, builder: var, stmt: *ast.Stmt) anyerror!void {
|
2019-09-23 21:33:30 +00:00
|
|
|
switch (stmt.*) {
|
2019-09-24 01:07:19 +00:00
|
|
|
.Expr => |expr| _ = try self.emitExpr(builder, expr),
|
2019-09-24 17:12:37 +00:00
|
|
|
|
2019-09-24 01:07:19 +00:00
|
|
|
.Return => |ret| {
|
|
|
|
var ret_expr = try self.emitExpr(builder, ret.value);
|
|
|
|
_ = llvm.LLVMBuildRet(builder, ret_expr);
|
|
|
|
},
|
2019-09-24 17:12:37 +00:00
|
|
|
|
|
|
|
.If => |ifstmt| {
|
|
|
|
var cond = try self.emitExpr(builder, ifstmt.condition);
|
|
|
|
var zero = mkLLVMBool(false);
|
|
|
|
var icmp = llvm.LLVMBuildICmp(builder, llvm.LLVMIntPredicate.LLVMIntNE, cond, zero, c"ifcond");
|
2019-09-24 20:47:17 +00:00
|
|
|
|
|
|
|
var insert = llvm.LLVMGetInsertBlock(builder);
|
|
|
|
var function = llvm.LLVMGetBasicBlockParent(insert);
|
|
|
|
|
|
|
|
var then_bb = llvm.LLVMAppendBasicBlock(function, c"then");
|
2019-09-24 21:49:35 +00:00
|
|
|
var else_bb = llvm.LLVMAppendBasicBlock(function, c"else");
|
|
|
|
var merge_bb = llvm.LLVMAppendBasicBlock(function, c"ifcont");
|
2019-09-24 20:47:17 +00:00
|
|
|
|
|
|
|
var condbr = llvm.LLVMBuildCondBr(builder, icmp, then_bb, else_bb);
|
|
|
|
|
|
|
|
llvm.LLVMPositionBuilderAtEnd(builder, then_bb);
|
|
|
|
|
|
|
|
// roughly translating to kaleidoscope's
|
|
|
|
// 'Value *ThenV = Then->codegen();'
|
2019-09-24 21:49:35 +00:00
|
|
|
var then_rets = false;
|
|
|
|
var else_rets = false;
|
|
|
|
|
2019-09-28 01:36:35 +00:00
|
|
|
self.ctx.setScope(self.ctx.current_scope.?.nextChild());
|
|
|
|
|
2019-09-28 01:49:33 +00:00
|
|
|
var then_branch = ifstmt.then_branch.toSlice();
|
|
|
|
for (then_branch) |_, idx| {
|
2019-09-24 21:49:35 +00:00
|
|
|
// keep emitting until branch has ret
|
2019-09-28 16:17:11 +00:00
|
|
|
var then_stmt = &then_branch[idx];
|
|
|
|
|
2019-09-24 21:49:35 +00:00
|
|
|
if (!then_rets)
|
2019-09-28 16:17:11 +00:00
|
|
|
try self.emitStmt(builder, then_stmt);
|
2019-09-24 21:49:35 +00:00
|
|
|
|
2019-09-28 16:17:11 +00:00
|
|
|
// TODO break? lol
|
|
|
|
switch (then_stmt.*) {
|
2019-09-24 21:49:35 +00:00
|
|
|
.Return => then_rets = true,
|
|
|
|
else => {},
|
|
|
|
}
|
2019-09-24 20:47:17 +00:00
|
|
|
}
|
|
|
|
|
2019-09-28 01:36:35 +00:00
|
|
|
self.ctx.dumpScope();
|
|
|
|
|
2019-09-24 21:49:35 +00:00
|
|
|
// only build the br instruction if we didn't ret, because
|
|
|
|
// there can't be any instruction after a terminator
|
|
|
|
// same applies for the else branch
|
|
|
|
if (!then_rets)
|
|
|
|
_ = llvm.LLVMBuildBr(builder, merge_bb);
|
|
|
|
|
2019-09-24 20:47:17 +00:00
|
|
|
then_bb = llvm.LLVMGetInsertBlock(builder);
|
|
|
|
|
|
|
|
llvm.LLVMPositionBuilderAtEnd(builder, else_bb);
|
|
|
|
|
|
|
|
// roughly translating to kaleidoscope's
|
|
|
|
// 'Else *ElseV = Else->codegen();'
|
|
|
|
if (ifstmt.else_branch) |else_block| {
|
2019-09-28 01:36:35 +00:00
|
|
|
self.ctx.setScope(self.ctx.current_scope.?.nextChild());
|
2019-09-28 01:49:33 +00:00
|
|
|
|
|
|
|
var else_slice = else_block.toSlice();
|
|
|
|
for (else_slice) |_, idx| {
|
2019-09-24 21:49:35 +00:00
|
|
|
// keep emitting until branch has ret
|
2019-09-28 16:17:11 +00:00
|
|
|
var else_stmt = &else_slice[idx];
|
|
|
|
|
2019-09-24 21:49:35 +00:00
|
|
|
if (!else_rets)
|
2019-09-28 16:17:11 +00:00
|
|
|
try self.emitStmt(builder, else_stmt);
|
2019-09-24 21:49:35 +00:00
|
|
|
|
2019-09-28 16:17:11 +00:00
|
|
|
switch (else_stmt.*) {
|
2019-09-24 21:49:35 +00:00
|
|
|
.Return => else_rets = true,
|
|
|
|
else => {},
|
|
|
|
}
|
2019-09-24 20:47:17 +00:00
|
|
|
}
|
2019-09-28 01:36:35 +00:00
|
|
|
|
|
|
|
self.ctx.dumpScope();
|
2019-09-24 20:47:17 +00:00
|
|
|
}
|
|
|
|
|
2019-09-24 21:49:35 +00:00
|
|
|
if (!else_rets)
|
|
|
|
_ = llvm.LLVMBuildBr(builder, merge_bb);
|
|
|
|
|
2019-09-24 20:47:17 +00:00
|
|
|
else_bb = llvm.LLVMGetInsertBlock(builder);
|
|
|
|
|
|
|
|
llvm.LLVMPositionBuilderAtEnd(builder, merge_bb);
|
|
|
|
|
2019-09-28 01:49:33 +00:00
|
|
|
// if both of the branches return, we should put
|
|
|
|
// the merge branch as unreachable.
|
2019-09-24 21:49:35 +00:00
|
|
|
if (then_rets and else_rets)
|
|
|
|
_ = llvm.LLVMBuildUnreachable(builder);
|
2019-09-24 17:12:37 +00:00
|
|
|
},
|
|
|
|
|
2019-09-28 01:36:35 +00:00
|
|
|
.VarDecl => |vardecl| {
|
|
|
|
// we alaready inferred the type of the variable in the
|
|
|
|
// analyze pass and the current scope contains the variable's
|
|
|
|
// type(hopefully), so we resolve it
|
|
|
|
const name = vardecl.name.lexeme;
|
|
|
|
var var_metadata = try self.ctx.resolveVarType(name);
|
|
|
|
|
|
|
|
var name_cstr = try std.cstr.addNullByte(self.allocator, name);
|
|
|
|
errdefer self.allocator.free(name_cstr);
|
|
|
|
|
|
|
|
var fn_symbol = self.getFnSymbol(self.current_function_name.?);
|
|
|
|
|
2019-09-28 01:49:33 +00:00
|
|
|
var variable = llvm.LLVMBuildAlloca(
|
2019-09-28 01:36:35 +00:00
|
|
|
builder,
|
|
|
|
try self.typeToLLVM(var_metadata.typ),
|
|
|
|
name_cstr.ptr,
|
|
|
|
);
|
|
|
|
|
2019-09-28 16:17:11 +00:00
|
|
|
stmt.*.VarDecl.llvm_alloca = variable;
|
2019-09-28 01:49:33 +00:00
|
|
|
|
|
|
|
var llvm_expr = try self.emitExpr(builder, vardecl.value);
|
|
|
|
_ = llvm.LLVMBuildStore(builder, llvm_expr, variable);
|
2019-09-28 01:36:35 +00:00
|
|
|
},
|
2019-09-27 20:18:38 +00:00
|
|
|
|
2019-09-24 01:07:19 +00:00
|
|
|
else => {
|
2019-09-26 03:11:48 +00:00
|
|
|
std.debug.warn("Got unexpected stmt {}\n", stmt.*);
|
2019-09-24 01:07:19 +00:00
|
|
|
return CompileError.EmitError;
|
|
|
|
},
|
2019-09-23 21:33:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-28 16:17:11 +00:00
|
|
|
fn getFnSymbol(self: *@This(), name: []const u8) *comp.FunctionSymbol {
|
2019-09-28 01:36:35 +00:00
|
|
|
var fn_sym_search = self.ctx.symbol_table.get(name).?.value;
|
2019-09-28 16:17:11 +00:00
|
|
|
std.debug.assert(comp.SymbolType(fn_sym_search.*) == .Function);
|
|
|
|
return &fn_sym_search.Function;
|
2019-09-28 01:36:35 +00:00
|
|
|
}
|
|
|
|
|
2019-09-25 14:29:47 +00:00
|
|
|
/// Emit LLVM ir for the given node.
|
2019-09-21 02:40:21 +00:00
|
|
|
fn genNode(
|
|
|
|
self: *Codegen,
|
|
|
|
mod: llvm.LLVMModuleRef,
|
2019-09-28 14:40:56 +00:00
|
|
|
node: *ast.Node,
|
2019-09-21 02:40:21 +00:00
|
|
|
) !void {
|
|
|
|
switch (node.*) {
|
|
|
|
.Root => @panic("Should not have gotten Root"),
|
|
|
|
.FnDecl => |decl| {
|
|
|
|
const name = decl.func_name.lexeme;
|
2019-09-27 23:39:16 +00:00
|
|
|
self.current_function_name = name;
|
2019-09-28 01:36:35 +00:00
|
|
|
std.debug.warn("cgen: genning function '{}'\n", name);
|
2019-09-25 14:29:47 +00:00
|
|
|
|
2019-09-28 01:36:35 +00:00
|
|
|
var fn_sym = self.getFnSymbol(name);
|
2019-09-25 14:29:47 +00:00
|
|
|
|
2019-09-21 02:40:21 +00:00
|
|
|
const name_cstr = try std.cstr.addNullByte(self.allocator, name);
|
|
|
|
errdefer self.allocator.free(name_cstr);
|
|
|
|
|
|
|
|
var param_types = llvm.LLVMTypeList.init(self.allocator);
|
|
|
|
errdefer param_types.deinit();
|
|
|
|
|
|
|
|
for (decl.params.toSlice()) |param| {
|
2019-09-25 14:29:47 +00:00
|
|
|
try param_types.append(try self.typeToLLVM(fn_sym.parameters.get(
|
|
|
|
param.name.lexeme,
|
2019-09-27 23:39:16 +00:00
|
|
|
).?.value.typ));
|
2019-09-21 02:40:21 +00:00
|
|
|
}
|
|
|
|
|
2019-09-24 16:23:23 +00:00
|
|
|
var llvm_ret_type = llvm.LLVMFunctionType(
|
2019-09-25 14:29:47 +00:00
|
|
|
try self.typeToLLVM(fn_sym.return_type),
|
2019-09-21 02:40:21 +00:00
|
|
|
param_types.toSlice().ptr,
|
|
|
|
@intCast(c_uint, param_types.len),
|
|
|
|
0,
|
|
|
|
);
|
|
|
|
|
2019-09-24 16:23:23 +00:00
|
|
|
var func = llvm.LLVMAddFunction(mod, name_cstr.ptr, llvm_ret_type);
|
2019-09-25 21:05:56 +00:00
|
|
|
_ = try self.llvm_table.put(name, func);
|
2019-09-24 17:12:37 +00:00
|
|
|
|
|
|
|
var buf = try self.allocator.alloc(u8, 512);
|
|
|
|
var entry_lbl = try std.fmt.bufPrint(buf, "fn_{}_entry", name);
|
|
|
|
var entry_lbl_cstr = try std.cstr.addNullByte(self.allocator, entry_lbl);
|
|
|
|
var entry = llvm.LLVMAppendBasicBlock(func, entry_lbl_cstr.ptr);
|
2019-09-21 02:40:21 +00:00
|
|
|
|
|
|
|
var builder = llvm.LLVMCreateBuilder();
|
|
|
|
llvm.LLVMPositionBuilderAtEnd(builder, entry);
|
|
|
|
|
2019-09-28 14:40:56 +00:00
|
|
|
// to have the ability to mutate parameters, we must allocate them on
|
|
|
|
// the stack
|
2019-09-28 16:17:11 +00:00
|
|
|
var params_slice = decl.params.toSlice();
|
2019-09-28 14:40:56 +00:00
|
|
|
for (params_slice) |param_node, idx| {
|
|
|
|
var param = fn_sym.parameters.get(param_node.name.lexeme).?.value;
|
|
|
|
|
2019-09-28 16:17:11 +00:00
|
|
|
const param_name_cstr = try std.cstr.addNullByte(self.allocator, param_node.name.lexeme);
|
|
|
|
errdefer self.allocator.free(param_name_cstr);
|
|
|
|
|
|
|
|
var alloca = llvm.LLVMBuildAlloca(builder, try self.typeToLLVM(param.typ), param_name_cstr.ptr);
|
2019-09-28 14:40:56 +00:00
|
|
|
|
2019-09-28 16:17:11 +00:00
|
|
|
std.debug.warn("SET PARAM LLVM ALLOCA {} to {}\n", param_node.name.lexeme, alloca);
|
2019-09-28 14:40:56 +00:00
|
|
|
param.llvm_alloca = alloca;
|
|
|
|
|
|
|
|
// TODO store register into stack param
|
2019-09-28 15:46:23 +00:00
|
|
|
// llvm.LLVMBuildStore(builder, null, assign_expr);
|
2019-09-28 14:40:56 +00:00
|
|
|
}
|
|
|
|
|
2019-09-28 01:36:35 +00:00
|
|
|
self.ctx.setScope(fn_sym.scope);
|
|
|
|
defer self.ctx.dumpScope();
|
|
|
|
|
2019-09-28 01:49:33 +00:00
|
|
|
var body_slice = decl.body.toSlice();
|
|
|
|
for (body_slice) |_, idx| {
|
|
|
|
try self.emitStmt(builder, &body_slice[idx]);
|
2019-09-23 21:33:30 +00:00
|
|
|
}
|
|
|
|
|
2019-09-24 17:12:37 +00:00
|
|
|
std.debug.warn("cgen: generated function '{}'\n", name);
|
2019-09-21 02:40:21 +00:00
|
|
|
},
|
2019-09-24 20:47:17 +00:00
|
|
|
|
2019-09-25 14:59:36 +00:00
|
|
|
// NOTE: enums don't have specific llvm ir code generated for them
|
|
|
|
.Enum => {},
|
|
|
|
|
2019-09-25 20:05:37 +00:00
|
|
|
.ConstDecl => |constdecls| {
|
|
|
|
for (constdecls.toSlice()) |constdecl| {
|
|
|
|
const name = constdecl.name.lexeme;
|
|
|
|
|
|
|
|
var const_type = self.ctx.symbol_table.get(name).?.value;
|
|
|
|
var const_llvm_type = try self.typeToLLVM(const_type.Const);
|
|
|
|
|
|
|
|
const const_name = try std.cstr.addNullByte(self.allocator, name);
|
|
|
|
errdefer self.allocator.free(const_name);
|
|
|
|
|
|
|
|
var global = llvm.LLVMAddGlobal(mod, const_llvm_type, const_name.ptr);
|
|
|
|
|
2019-09-25 21:10:17 +00:00
|
|
|
// TODO maybe put builder at main function so we can still
|
|
|
|
// call other functions inside consts?
|
|
|
|
|
2019-09-25 20:05:37 +00:00
|
|
|
var builder = llvm.LLVMCreateBuilder();
|
|
|
|
var expr_llvm_val = try self.emitExpr(builder, constdecl.expr);
|
|
|
|
|
|
|
|
llvm.LLVMSetInitializer(global, expr_llvm_val);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2019-09-21 02:40:21 +00:00
|
|
|
else => {
|
2019-09-25 03:26:30 +00:00
|
|
|
std.debug.warn("TODO handle node type {}\n", @tagName(node.*));
|
2019-09-25 14:29:47 +00:00
|
|
|
return;
|
2019-09-21 02:40:21 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn gen(self: *Codegen, root: *ast.Node) !void {
|
|
|
|
std.debug.warn("cgen: start gen\n");
|
2019-09-22 02:56:22 +00:00
|
|
|
_ = llvm.LLVMInitializeNativeTarget();
|
2019-09-21 15:31:47 +00:00
|
|
|
|
|
|
|
var mod = llvm.LLVMModuleCreateWithName(c"awoo").?;
|
2019-09-21 02:40:21 +00:00
|
|
|
defer llvm.LLVMDisposeModule(mod);
|
|
|
|
|
2019-09-28 14:40:56 +00:00
|
|
|
var root_slice = root.Root.toSlice();
|
|
|
|
for (root_slice) |_, idx| {
|
|
|
|
try self.genNode(mod, &root_slice[idx]);
|
2019-09-21 02:40:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var err: ?[*]u8 = null;
|
2019-09-22 02:56:22 +00:00
|
|
|
defer llvm.LLVMDisposeMessage(err);
|
|
|
|
|
2019-09-24 21:59:00 +00:00
|
|
|
if (llvm.LLVMPrintModuleToFile(mod, c"output.ll", &err) != 0) {
|
2019-09-24 21:49:35 +00:00
|
|
|
std.debug.warn("error printing module to file: {}\n", sliceify(err));
|
|
|
|
return CompileError.LLVMError;
|
|
|
|
}
|
2019-09-21 15:37:41 +00:00
|
|
|
|
2019-09-25 21:05:56 +00:00
|
|
|
//if (llvm.LLVMWriteBitcodeToFile(mod, c"awoo.bc") != 0) {
|
|
|
|
// std.debug.warn("error writing bitcode to file: {}\n", sliceify(err));
|
|
|
|
// return CompileError.LLVMError;
|
|
|
|
//}
|
2019-09-22 02:56:22 +00:00
|
|
|
|
2019-09-24 21:49:35 +00:00
|
|
|
std.debug.warn("cgen: verify llvm module\n");
|
|
|
|
_ = llvm.LLVMVerifyModule(
|
|
|
|
mod,
|
|
|
|
llvm.LLVMVerifierFailureAction.LLVMAbortProcessAction,
|
|
|
|
&err,
|
|
|
|
);
|
|
|
|
|
2019-09-22 21:40:15 +00:00
|
|
|
llvm.LLVMInitializeAllTargetInfos();
|
|
|
|
llvm.LLVMInitializeAllTargets();
|
|
|
|
llvm.LLVMInitializeAllTargetMCs();
|
|
|
|
llvm.LLVMInitializeAllAsmParsers();
|
|
|
|
llvm.LLVMInitializeAllAsmPrinters();
|
2019-09-22 02:56:22 +00:00
|
|
|
|
|
|
|
var engine: llvm.LLVMExecutionEngineRef = undefined;
|
|
|
|
if (llvm.LLVMCreateExecutionEngineForModule(&engine, mod, &err) != 0) {
|
|
|
|
std.debug.warn("failed to create execution engine: {}\n", sliceify(err));
|
2019-09-22 21:26:00 +00:00
|
|
|
return CompileError.LLVMError;
|
2019-09-22 02:56:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var machine = llvm.LLVMGetExecutionEngineTargetMachine(engine);
|
|
|
|
defer llvm.LLVMDisposeTargetMachine(machine);
|
|
|
|
|
|
|
|
var target = llvm.LLVMGetTargetMachineTarget(machine);
|
|
|
|
var target_data = llvm.LLVMCreateTargetDataLayout(machine);
|
|
|
|
var data_layout = llvm.LLVMCopyStringRepOfTargetData(target_data);
|
|
|
|
llvm.LLVMSetDataLayout(mod, data_layout);
|
|
|
|
|
2019-09-24 01:07:19 +00:00
|
|
|
var outpath_cstr = try std.cstr.addNullByte(self.allocator, "outpath.o");
|
2019-09-22 02:56:22 +00:00
|
|
|
|
2019-09-24 01:07:19 +00:00
|
|
|
//var asmpath_cstr = try std.cstr.addNullByte(self.allocator, "output.S");
|
2019-09-22 21:40:15 +00:00
|
|
|
|
2019-09-22 02:56:22 +00:00
|
|
|
var desc = llvm.LLVMGetTargetDescription(target);
|
|
|
|
var features = llvm.LLVMGetTargetMachineFeatureString(machine);
|
|
|
|
var triple = llvm.LLVMGetTargetMachineTriple(machine);
|
|
|
|
|
|
|
|
std.debug.warn("target: {}\n", sliceify(desc));
|
|
|
|
std.debug.warn("triple: {}\n", sliceify(triple));
|
|
|
|
std.debug.warn("features: {}\n", sliceify(features));
|
|
|
|
|
2019-09-22 21:40:15 +00:00
|
|
|
//if (llvm.LLVMTargetMachineEmitToFile(
|
|
|
|
// machine,
|
|
|
|
// mod,
|
|
|
|
// asmpath_cstr.ptr,
|
|
|
|
// llvm.LLVMCodeGenFileType.LLVMAssemblyFile,
|
|
|
|
// &err,
|
|
|
|
//) != 0) {
|
|
|
|
// std.debug.warn("failed to emit to assembly file: {}\n", sliceify(err));
|
|
|
|
// return CompileError.LLVMError;
|
|
|
|
//}
|
|
|
|
|
2019-09-22 02:56:22 +00:00
|
|
|
if (llvm.LLVMTargetMachineEmitToFile(
|
|
|
|
machine,
|
|
|
|
mod,
|
|
|
|
outpath_cstr.ptr,
|
|
|
|
llvm.LLVMCodeGenFileType.LLVMObjectFile,
|
|
|
|
&err,
|
|
|
|
) != 0) {
|
|
|
|
std.debug.warn("failed to emit to file: {}\n", sliceify(err));
|
2019-09-22 21:26:00 +00:00
|
|
|
return CompileError.LLVMError;
|
2019-09-21 15:37:41 +00:00
|
|
|
}
|
2019-09-21 02:40:21 +00:00
|
|
|
}
|
|
|
|
};
|