fixes for latest zig

This commit is contained in:
Luna 2019-12-07 20:28:55 -03:00
parent 9a9008247f
commit ebf4af6537
3 changed files with 20 additions and 21 deletions

View file

@ -30,5 +30,5 @@ fn add(a: i32, b: i32) i32 {
} }
fn main() i32 { fn main() i32 {
return 1; return 123;
} }

View file

@ -4,8 +4,7 @@ const llvm = @import("../llvm.zig");
const comp = @import("../comp_ctx.zig"); const comp = @import("../comp_ctx.zig");
const CompileError = @import("../codegen.zig").CompileError; const CompileError = @import("../codegen.zig").CompileError;
fn sliceify(non_slice: ?[*]const u8) []const u8 { fn sliceify(non_slice: ?[*:0]const u8) []const u8 {
if (non_slice == null) return "";
return non_slice.?[0..std.mem.len(u8, non_slice.?)]; return non_slice.?[0..std.mem.len(u8, non_slice.?)];
} }
@ -126,8 +125,8 @@ pub const Codegen = struct {
var right = try self.emitExpr(builder, unary.right); var right = try self.emitExpr(builder, unary.right);
return switch (unary.op) { return switch (unary.op) {
.Negate => llvm.LLVMBuildNeg(builder, right, c"neg_tmp"), .Negate => llvm.LLVMBuildNeg(builder, right, "neg_tmp"),
.Not => llvm.LLVMBuildNot(builder, right, c"neg_tmp"), .Not => llvm.LLVMBuildNot(builder, right, "neg_tmp"),
}; };
}, },
@ -136,13 +135,13 @@ pub const Codegen = struct {
var right = try self.emitExpr(builder, binary.right); var right = try self.emitExpr(builder, binary.right);
return switch (binary.op) { return switch (binary.op) {
.Add => llvm.LLVMBuildAdd(builder, left, right, c"addtmp"), .Add => llvm.LLVMBuildAdd(builder, left, right, "addtmp"),
.Sub => llvm.LLVMBuildSub(builder, left, right, c"subtmp"), .Sub => llvm.LLVMBuildSub(builder, left, right, "subtmp"),
.Mul => llvm.LLVMBuildMul(builder, left, right, c"multmp"), .Mul => llvm.LLVMBuildMul(builder, left, right, "multmp"),
//.Div => llvm.LLVMBuildDiv(builder, left, right, c"divtmp"), //.Div => llvm.LLVMBuildDiv(builder, left, right, "divtmp"),
.And => llvm.LLVMBuildAnd(builder, left, right, c"andtmp"), .And => llvm.LLVMBuildAnd(builder, left, right, "andtmp"),
.Or => llvm.LLVMBuildOr(builder, left, right, c"ortmp"), .Or => llvm.LLVMBuildOr(builder, left, right, "ortmp"),
else => { else => {
std.debug.warn("Unexpected binary operator: '{}'\n", binary.op); std.debug.warn("Unexpected binary operator: '{}'\n", binary.op);
@ -199,7 +198,7 @@ pub const Codegen = struct {
llvm_func.?.value, llvm_func.?.value,
args_slice.ptr, args_slice.ptr,
@intCast(c_uint, args_slice.len), @intCast(c_uint, args_slice.len),
c"call", "call",
); );
}, },
@ -276,14 +275,14 @@ pub const Codegen = struct {
.If => |ifstmt| { .If => |ifstmt| {
var cond = try self.emitExpr(builder, ifstmt.condition); var cond = try self.emitExpr(builder, ifstmt.condition);
var zero = mkLLVMBool(false); var zero = mkLLVMBool(false);
var icmp = llvm.LLVMBuildICmp(builder, llvm.LLVMIntPredicate.LLVMIntNE, cond, zero, c"ifcond"); var icmp = llvm.LLVMBuildICmp(builder, llvm.LLVMIntPredicate.LLVMIntNE, cond, zero, "ifcond");
var insert = llvm.LLVMGetInsertBlock(builder); var insert = llvm.LLVMGetInsertBlock(builder);
var function = llvm.LLVMGetBasicBlockParent(insert); var function = llvm.LLVMGetBasicBlockParent(insert);
var then_bb = llvm.LLVMAppendBasicBlock(function, c"then"); var then_bb = llvm.LLVMAppendBasicBlock(function, "then");
var else_bb = llvm.LLVMAppendBasicBlock(function, c"else"); var else_bb = llvm.LLVMAppendBasicBlock(function, "else");
var merge_bb = llvm.LLVMAppendBasicBlock(function, c"ifcont"); var merge_bb = llvm.LLVMAppendBasicBlock(function, "ifcont");
var condbr = llvm.LLVMBuildCondBr(builder, icmp, then_bb, else_bb); var condbr = llvm.LLVMBuildCondBr(builder, icmp, then_bb, else_bb);
@ -514,7 +513,7 @@ pub const Codegen = struct {
std.debug.warn("cgen: start gen\n"); std.debug.warn("cgen: start gen\n");
_ = llvm.LLVMInitializeNativeTarget(); _ = llvm.LLVMInitializeNativeTarget();
var mod = llvm.LLVMModuleCreateWithName(c"awoo").?; var mod = llvm.LLVMModuleCreateWithName("awoo").?;
defer llvm.LLVMDisposeModule(mod); defer llvm.LLVMDisposeModule(mod);
var root_slice = root.Root.toSlice(); var root_slice = root.Root.toSlice();
@ -522,15 +521,15 @@ pub const Codegen = struct {
try self.genNode(mod, &root_slice[idx]); try self.genNode(mod, &root_slice[idx]);
} }
var err: ?[*]u8 = null; var err: ?[*:0]u8 = null;
defer llvm.LLVMDisposeMessage(err); defer llvm.LLVMDisposeMessage(err);
if (llvm.LLVMPrintModuleToFile(mod, c"output.ll", &err) != 0) { if (llvm.LLVMPrintModuleToFile(mod, "output.ll", &err) != 0) {
std.debug.warn("error printing module to file: {}\n", sliceify(err)); std.debug.warn("error printing module to file: {}\n", sliceify(err));
return CompileError.BackendError; return CompileError.BackendError;
} }
//if (llvm.LLVMWriteBitcodeToFile(mod, c"awoo.bc") != 0) { //if (llvm.LLVMWriteBitcodeToFile(mod, "awoo.bc") != 0) {
// std.debug.warn("error writing bitcode to file: {}\n", sliceify(err)); // std.debug.warn("error writing bitcode to file: {}\n", sliceify(err));
// return CompileError.BackendError; // return CompileError.BackendError;
//} //}

View file

@ -60,7 +60,7 @@ pub fn run(allocator: *std.mem.Allocator, slice: []const u8) !Result {
try cgen.gen(root); try cgen.gen(root);
var child = try std.ChildProcess.init( var child = try std.ChildProcess.init(
[_][]const u8{ "gcc", "src/entry.c", "outpath.o", "-o", "a.out" }, &[_][]const u8{ "gcc", "src/entry.c", "outpath.o", "-o", "a.out" },
allocator, allocator,
); );
try child.spawn(); try child.spawn();