authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2020-12-31 02:42:48-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2020-12-31 02:42:48-07:00
log7deb1f4f6c038eaef815d5d179633683a9574ff0
treefcfabab97a321af087a1bf03a54db3d4108a8a5a
parenta46d24af1cf2885991c67bf39a2e639891c16121

stage2: type inference for local var


4 files changed, 20 insertions(+), 7 deletions(-)

BRANCH_TODO created+2
......@@ -0,0 +1,2 @@
1 * no need for payload on inferred_alloc for the type
2 * compile error for "variable of type '{}' must be const or comptime" after resolving types
src/astgen.zig+3-3
......@@ -625,9 +625,9 @@ fn varDecl(
625625 const alloc = try addZIRUnOp(mod, scope, name_src, .alloc_mut, type_inst);
626626 break :a .{ .alloc = alloc, .result_loc = .{ .ptr = alloc } };
627627 } else a: {
628 const alloc = try addZIRNoOp(mod, scope, name_src, .alloc_inferred_mut);
629 resolve_inferred_alloc = alloc;
630 break :a .{ .alloc = alloc, .result_loc = .{ .inferred_ptr = alloc.castTag(.alloc_inferred_mut).? } };
628 const alloc = try addZIRNoOpT(mod, scope, name_src, .alloc_inferred);
629 resolve_inferred_alloc = &alloc.base;
630 break :a .{ .alloc = &alloc.base, .result_loc = .{ .inferred_ptr = alloc } };
631631 };
632632 const init_inst = try expr(mod, scope, var_data.result_loc, init_node);
633633 if (resolve_inferred_alloc) |inst| {
src/codegen/c.zig+11-2
......@@ -275,7 +275,6 @@ pub fn generate(file: *C, module: *Module, decl: *Decl) !void {
275275 try writer.writeAll(" {");
276276
277277 const func: *Module.Fn = func_payload.data;
278 //func.dump(module.*);
279278 const instructions = func.analysis.success.instructions;
280279 if (instructions.len > 0) {
281280 try writer.writeAll("\n");
......@@ -297,6 +296,7 @@ pub fn generate(file: *C, module: *Module, decl: *Decl) !void {
297296 .cmp_neq => try genBinOp(&ctx, file, inst.castTag(.cmp_neq).?, "!="),
298297 .dbg_stmt => try genDbgStmt(&ctx, inst.castTag(.dbg_stmt).?),
299298 .intcast => try genIntCast(&ctx, file, inst.castTag(.intcast).?),
299 .load => try genLoad(&ctx, file, inst.castTag(.load).?),
300300 .ret => try genRet(&ctx, file, inst.castTag(.ret).?),
301301 .retvoid => try genRetVoid(file),
302302 .store => try genStore(&ctx, file, inst.castTag(.store).?),
......@@ -431,6 +431,16 @@ fn genRetVoid(file: *C) !?[]u8 {
431431 return null;
432432}
433433
434fn genLoad(ctx: *Context, file: *C, inst: *Inst.UnOp) !?[]u8 {
435 const operand = try ctx.resolveInst(inst.operand);
436 const writer = file.main.writer();
437 try indent(file);
438 const local_name = try ctx.name();
439 try renderTypeAndName(ctx, writer, inst.base.ty, local_name, .Const);
440 try writer.print(" = *{s};\n", .{operand});
441 return local_name;
442}
443
434444fn genRet(ctx: *Context, file: *C, inst: *Inst.UnOp) !?[]u8 {
435445 try indent(file);
436446 const writer = file.main.writer();
......@@ -442,7 +452,6 @@ fn genIntCast(ctx: *Context, file: *C, inst: *Inst.UnOp) !?[]u8 {
442452 if (inst.base.isUnused())
443453 return null;
444454 try indent(file);
445 const op = inst.operand;
446455 const writer = file.main.writer();
447456 const name = try ctx.name();
448457 const from = try ctx.resolveInst(inst.operand);
test/stage2/cbe.zig+4-2
......@@ -52,7 +52,7 @@ pub fn addCases(ctx: *TestContext) !void {
5252 }
5353
5454 {
55 var case = ctx.exeFromCompiledC("inferred local const", .{});
55 var case = ctx.exeFromCompiledC("inferred local const and var", .{});
5656
5757 case.addCompareOutput(
5858 \\fn add(a: i32, b: i32) i32 {
......@@ -61,7 +61,9 @@ pub fn addCases(ctx: *TestContext) !void {
6161 \\
6262 \\export fn main() c_int {
6363 \\ const x = add(1, 2);
64 \\ return x - 3;
64 \\ var y = add(3, 0);
65 \\ y -= x;
66 \\ return y;
6567 \\}
6668 , "");
6769 }