authorgravatar for 58830309+g-w1@users.noreply.github.comg-w1 <58830309+g-w1@users.noreply.github.com> 2020-12-28 08:42:22-05:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2020-12-28 15:42:22+02:00
logc234761eddc2fe932046767756b9e6df82a73b65
tree959a1873c66019485873a425fd5a915b490871ed
parentfffb0904f898d47800aa418085dc6064d02c32fe
signaturebadge-question-mark Signed by PGP key 4AEE18F83AFDEB23

stage2: make Alloc(Inferred) have mutabality info (#7570)


3 files changed, 26 insertions(+), 6 deletions(-)

src/astgen.zig+3-3
...@@ -616,11 +616,11 @@ fn varDecl(...@@ -616,11 +616,11 @@ fn varDecl(
616 .Keyword_var => {616 .Keyword_var => {
617 const var_data: struct { result_loc: ResultLoc, alloc: *zir.Inst } = if (node.getTypeNode()) |type_node| a: {617 const var_data: struct { result_loc: ResultLoc, alloc: *zir.Inst } = if (node.getTypeNode()) |type_node| a: {
618 const type_inst = try typeExpr(mod, scope, type_node);618 const type_inst = try typeExpr(mod, scope, type_node);
619 const alloc = try addZIRUnOp(mod, scope, name_src, .alloc, type_inst);619 const alloc = try addZIRUnOp(mod, scope, name_src, .alloc_mut, type_inst);
620 break :a .{ .alloc = alloc, .result_loc = .{ .ptr = alloc } };620 break :a .{ .alloc = alloc, .result_loc = .{ .ptr = alloc } };
621 } else a: {621 } else a: {
622 const alloc = try addZIRNoOp(mod, scope, name_src, .alloc_inferred);622 const alloc = try addZIRNoOp(mod, scope, name_src, .alloc_inferred_mut);
623 break :a .{ .alloc = alloc, .result_loc = .{ .inferred_ptr = alloc.castTag(.alloc_inferred).? } };623 break :a .{ .alloc = alloc, .result_loc = .{ .inferred_ptr = alloc.castTag(.alloc_inferred_mut).? } };
624 };624 };
625 const init_inst = try expr(mod, scope, var_data.result_loc, init_node);625 const init_inst = try expr(mod, scope, var_data.result_loc, init_node);
626 const sub_scope = try block_arena.create(Scope.LocalPtr);626 const sub_scope = try block_arena.create(Scope.LocalPtr);
src/zir.zig+10-2
...@@ -41,8 +41,12 @@ pub const Inst = struct {...@@ -41,8 +41,12 @@ pub const Inst = struct {
41 /// Allocates stack local memory. Its lifetime ends when the block ends that contains41 /// Allocates stack local memory. Its lifetime ends when the block ends that contains
42 /// this instruction. The operand is the type of the allocated object.42 /// this instruction. The operand is the type of the allocated object.
43 alloc,43 alloc,
44 /// Same as `alloc` except mutable.
45 alloc_mut,
44 /// Same as `alloc` except the type is inferred.46 /// Same as `alloc` except the type is inferred.
45 alloc_inferred,47 alloc_inferred,
48 /// Same as `alloc_inferred` except mutable.
49 alloc_inferred_mut,
46 /// Create an `anyframe->T`.50 /// Create an `anyframe->T`.
47 anyframe_type,51 anyframe_type,
48 /// Array concatenation. `a ++ b`52 /// Array concatenation. `a ++ b`
...@@ -289,16 +293,19 @@ pub const Inst = struct {...@@ -289,16 +293,19 @@ pub const Inst = struct {
289293
290 pub fn Type(tag: Tag) type {294 pub fn Type(tag: Tag) type {
291 return switch (tag) {295 return switch (tag) {
296 .alloc_inferred,
297 .alloc_inferred_mut,
292 .breakpoint,298 .breakpoint,
293 .dbg_stmt,299 .dbg_stmt,
294 .returnvoid,300 .returnvoid,
295 .alloc_inferred,
296 .ret_ptr,301 .ret_ptr,
297 .ret_type,302 .ret_type,
298 .unreach_nocheck,303 .unreach_nocheck,
299 .@"unreachable",304 .@"unreachable",
300 => NoOp,305 => NoOp,
301306
307 .alloc,
308 .alloc_mut,
302 .boolnot,309 .boolnot,
303 .compileerror,310 .compileerror,
304 .deref,311 .deref,
...@@ -307,7 +314,6 @@ pub const Inst = struct {...@@ -307,7 +314,6 @@ pub const Inst = struct {
307 .isnonnull,314 .isnonnull,
308 .iserr,315 .iserr,
309 .ptrtoint,316 .ptrtoint,
310 .alloc,
311 .ensure_result_used,317 .ensure_result_used,
312 .ensure_result_non_error,318 .ensure_result_non_error,
313 .ensure_indexable,319 .ensure_indexable,
...@@ -419,7 +425,9 @@ pub const Inst = struct {...@@ -419,7 +425,9 @@ pub const Inst = struct {
419 .add,425 .add,
420 .addwrap,426 .addwrap,
421 .alloc,427 .alloc,
428 .alloc_mut,
422 .alloc_inferred,429 .alloc_inferred,
430 .alloc_inferred_mut,
423 .array_cat,431 .array_cat,
424 .array_mul,432 .array_mul,
425 .array_type,433 .array_type,
src/zir_sema.zig+13-1
...@@ -27,7 +27,9 @@ const Decl = Module.Decl;...@@ -27,7 +27,9 @@ const Decl = Module.Decl;
27pub fn analyzeInst(mod: *Module, scope: *Scope, old_inst: *zir.Inst) InnerError!*Inst {27pub fn analyzeInst(mod: *Module, scope: *Scope, old_inst: *zir.Inst) InnerError!*Inst {
28 switch (old_inst.tag) {28 switch (old_inst.tag) {
29 .alloc => return analyzeInstAlloc(mod, scope, old_inst.castTag(.alloc).?),29 .alloc => return analyzeInstAlloc(mod, scope, old_inst.castTag(.alloc).?),
30 .alloc_mut => return analyzeInstAllocMut(mod, scope, old_inst.castTag(.alloc_mut).?),
30 .alloc_inferred => return analyzeInstAllocInferred(mod, scope, old_inst.castTag(.alloc_inferred).?),31 .alloc_inferred => return analyzeInstAllocInferred(mod, scope, old_inst.castTag(.alloc_inferred).?),
32 .alloc_inferred_mut => return analyzeInstAllocInferredMut(mod, scope, old_inst.castTag(.alloc_inferred_mut).?),
31 .arg => return analyzeInstArg(mod, scope, old_inst.castTag(.arg).?),33 .arg => return analyzeInstArg(mod, scope, old_inst.castTag(.arg).?),
32 .bitcast_ref => return analyzeInstBitCastRef(mod, scope, old_inst.castTag(.bitcast_ref).?),34 .bitcast_ref => return analyzeInstBitCastRef(mod, scope, old_inst.castTag(.bitcast_ref).?),
33 .bitcast_result_ptr => return analyzeInstBitCastResultPtr(mod, scope, old_inst.castTag(.bitcast_result_ptr).?),35 .bitcast_result_ptr => return analyzeInstBitCastResultPtr(mod, scope, old_inst.castTag(.bitcast_result_ptr).?),
...@@ -410,7 +412,13 @@ fn analyzeInstEnsureIndexable(mod: *Module, scope: *Scope, inst: *zir.Inst.UnOp)...@@ -410,7 +412,13 @@ fn analyzeInstEnsureIndexable(mod: *Module, scope: *Scope, inst: *zir.Inst.UnOp)
410412
411fn analyzeInstAlloc(mod: *Module, scope: *Scope, inst: *zir.Inst.UnOp) InnerError!*Inst {413fn analyzeInstAlloc(mod: *Module, scope: *Scope, inst: *zir.Inst.UnOp) InnerError!*Inst {
412 const var_type = try resolveType(mod, scope, inst.positionals.operand);414 const var_type = try resolveType(mod, scope, inst.positionals.operand);
413 // TODO this should happen only for var allocs415 const ptr_type = try mod.simplePtrType(scope, inst.base.src, var_type, true, .One);
416 const b = try mod.requireRuntimeBlock(scope, inst.base.src);
417 return mod.addNoOp(b, inst.base.src, ptr_type, .alloc);
418}
419
420fn analyzeInstAllocMut(mod: *Module, scope: *Scope, inst: *zir.Inst.UnOp) InnerError!*Inst {
421 const var_type = try resolveType(mod, scope, inst.positionals.operand);
414 if (!var_type.isValidVarType(false)) {422 if (!var_type.isValidVarType(false)) {
415 return mod.fail(scope, inst.base.src, "variable of type '{}' must be const or comptime", .{var_type});423 return mod.fail(scope, inst.base.src, "variable of type '{}' must be const or comptime", .{var_type});
416 }424 }
...@@ -423,6 +431,10 @@ fn analyzeInstAllocInferred(mod: *Module, scope: *Scope, inst: *zir.Inst.NoOp) I...@@ -423,6 +431,10 @@ fn analyzeInstAllocInferred(mod: *Module, scope: *Scope, inst: *zir.Inst.NoOp) I
423 return mod.fail(scope, inst.base.src, "TODO implement analyzeInstAllocInferred", .{});431 return mod.fail(scope, inst.base.src, "TODO implement analyzeInstAllocInferred", .{});
424}432}
425433
434fn analyzeInstAllocInferredMut(mod: *Module, scope: *Scope, inst: *zir.Inst.NoOp) InnerError!*Inst {
435 return mod.fail(scope, inst.base.src, "TODO implement analyzeInstAllocInferredMut", .{});
436}
437
426fn analyzeInstStore(mod: *Module, scope: *Scope, inst: *zir.Inst.BinOp) InnerError!*Inst {438fn analyzeInstStore(mod: *Module, scope: *Scope, inst: *zir.Inst.BinOp) InnerError!*Inst {
427 const ptr = try resolveInst(mod, scope, inst.positionals.lhs);439 const ptr = try resolveInst(mod, scope, inst.positionals.lhs);
428 const value = try resolveInst(mod, scope, inst.positionals.rhs);440 const value = try resolveInst(mod, scope, inst.positionals.rhs);