summaryrefslogtreecommitdiff
path: root/src/codegen.zig
diff options
context:
space:
mode:
Diffstat (limited to 'src/codegen.zig')
-rw-r--r--src/codegen.zig54
1 files changed, 49 insertions, 5 deletions
diff --git a/src/codegen.zig b/src/codegen.zig
index 8f3160e..7aff0b6 100644
--- a/src/codegen.zig
+++ b/src/codegen.zig
@@ -81,6 +81,7 @@ pub const Generator = struct {
const table = stmt.symtable;
const symbol = table.getValue(nodeVar.ident.ident).?;
const value = try self.genExpr(nodeVar.expr);
+ // const ptr = try self.genAlloc(toLLVMtype(nodeVar.expr.typ.?, table).?, nodeVar.ident.ident);
const ptr = try self.genAlloc(
toLLVMtype(
nodeVar.expr.typ orelse try nodeVar.expr.symtable.getValue(nodeVar.ident.ident).?.typ.toTypeIdent(self.allocator),
@@ -98,9 +99,11 @@ pub const Generator = struct {
const table = stmt.symtable;
const symbol = table.getValue(nodeVar.ident.ident).?;
+ // const ptr = try self.genAlloc(toLLVMtype(nodeVar.expr.typ.?, table), nodeVar.ident.ident);
const ptr = try self.genAlloc(toLLVMtype(nodeVar.expr.typ orelse try nodeVar.expr.inferType(self.allocator, table), table, nodeVar.expr), nodeVar.ident.ident);
const value = try self.genExpr(nodeVar.expr);
_ = core.LLVMBuildStore(self.builder, value, ptr);
+ std.debug.print("\t\t\tGenerated Value {s}\n", .{nodeVar.ident.ident});
try self.references.put(symbol.id, ptr);
}
@@ -120,9 +123,9 @@ pub const Generator = struct {
return core.LLVMBuildAlloca(builder, typ, str);
}
- fn asBasicType(typ: symb.SymbType) ?types.LLVMTypeKind {
+ fn asBasicType(typ: symb.SymbType) ?types.LLVMTypeRef {
return switch (typ) {
- .Integer => types.LLVMTypeKind.LLVMIntegerTypeKind,
+ .Integer => core.LLVMInt32Type(),
else => null,
};
}
@@ -186,13 +189,39 @@ pub const Generator = struct {
}
}
+ fn genIf(self: *Generator, stmt: parse.NodeStmt) CodegenError!void {
+ const ifkind = stmt.kind.ifstmt;
+ const block = ifkind.body;
+ const expr = ifkind.expr;
+
+ const condition = try self.genExpr(expr);
+ const func = self.currentFunc.?;
+
+ const then = core.LLVMAppendBasicBlock(func, "then");
+ const elsebb = core.LLVMAppendBasicBlock(func, "elsebb");
+ const cont = core.LLVMAppendBasicBlock(func, "continue");
+
+ _ = core.LLVMBuildCondBr(self.builder, condition, then, elsebb);
+
+ _ = core.LLVMPositionBuilderAtEnd(self.builder, then);
+ try self.genStmt(block.*);
+ _ = core.LLVMBuildBr(self.builder, cont);
+
+ _ = core.LLVMPositionBuilderAtEnd(self.builder, elsebb);
+ _ = core.LLVMBuildBr(self.builder, cont);
+
+ _ = core.LLVMPositionBuilderAtEnd(self.builder, cont);
+ }
+
fn genStmt(self: *Generator, stmt: parse.NodeStmt) !void {
+ std.debug.print("======\n\tStmt: {any}\n======\n", .{stmt.kind});
try switch (stmt.kind) {
.exit => |expr| self.genExit(expr),
.function => self.genFunc(stmt),
.defValue => self.genValue(stmt),
.defVar => self.genVar(stmt),
.assignVar => self.genAssign(stmt),
+ .ifstmt => self.genIf(stmt),
.expr => |expression| {
_ = try self.genExpr(expression);
},
@@ -230,6 +259,20 @@ pub const Generator = struct {
);
},
.intLit => |int| core.LLVMConstInt(core.LLVMInt32TypeInContext(self.context), @intCast(int.intLit), 1),
+ .binaryOp => |exp| blk: {
+ const lhs = try self.genExpr(exp.lhs.*);
+ const rhs = try self.genExpr(exp.rhs.*);
+ std.debug.print("\n\n\tLHS: {any}\n\tRHS: {any}\n\tOP: {any}\n\n", .{ lhs, rhs, exp.op });
+
+ break :blk switch (exp.op) {
+ .plus => core.LLVMBuildAdd(self.builder, lhs, rhs, "add"),
+ .minus => core.LLVMBuildSub(self.builder, lhs, rhs, "sub"),
+ .star => core.LLVMBuildMul(self.builder, lhs, rhs, "mul"),
+ .slash => core.LLVMBuildSDiv(self.builder, lhs, rhs, "div"),
+ .eqleql => core.LLVMBuildICmp(self.builder, types.LLVMIntPredicate.LLVMIntEQ, lhs, rhs, "eql"),
+ else => core.LLVMBuildICmp(self.builder, types.LLVMIntPredicate.LLVMIntEQ, lhs, rhs, "eql"),
+ };
+ },
.stringLit => |str| blk: {
const vref = core.LLVMAddGlobal(
self.module,
@@ -254,7 +297,7 @@ pub const Generator = struct {
const ident = try self.allocator.dupeZ(u8, call.ident.ident);
const function = core.LLVMGetNamedFunction(self.module, ident);
var args = std.ArrayList(types.LLVMValueRef).init(self.allocator);
- for (call.args, functype.input) |arg, intype| {
+ for (call.args.items, functype.input) |arg, intype| {
if (!std.meta.eql(expr.symtable.getType(arg.typ.?).?, intype)) return CodegenError.IncorrectType;
try args.append(try self.genExpr(arg));
}
@@ -266,7 +309,7 @@ pub const Generator = struct {
funcType,
function,
@ptrCast(args.items),
- @intCast(call.args.len),
+ @intCast(call.args.items.len),
ident,
);
// std.debug.print("CALL\n", .{});
@@ -279,8 +322,9 @@ pub const Generator = struct {
pub fn generate(self: *Generator) ![]const u8 {
try switch (self.root.kind) {
.block => |b| {
- for (b) |stmt|
+ for (b) |stmt| {
try self.genStmt(stmt);
+ }
},
else => error.InvalidTop,
};