authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2021-03-20 17:09:06-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2021-03-20 17:09:06-07:00
log50010447bde42ca96aff63e0a620f941464f2eae
tree620e963139761ede5900e91f7ebbbe4373a7b864
parent907142a03627f170ef1e721e996a8759952fb58f

astgen: implement function calls


4 files changed, 96 insertions(+), 46 deletions(-)

src/Module.zig+68-28
......@@ -237,12 +237,20 @@ pub const Decl = struct {
237237 }
238238 }
239239
240 pub fn tokSrcLoc(decl: *Decl, token_index: ast.TokenIndex) LazySrcLoc {
240 pub fn relativeToNodeIndex(decl: Decl, offset: i32) ast.Node.Index {
241 return @bitCast(ast.Node.Index, offset + @bitCast(i32, decl.srcNode()));
242 }
243
244 pub fn nodeIndexToRelative(decl: Decl, node_index: ast.Node.Index) i32 {
245 return @bitCast(i32, node_index) - @bitCast(i32, decl.srcNode());
246 }
247
248 pub fn tokSrcLoc(decl: Decl, token_index: ast.TokenIndex) LazySrcLoc {
241249 return .{ .token_offset = token_index - decl.srcToken() };
242250 }
243251
244 pub fn nodeSrcLoc(decl: *Decl, node_index: ast.Node.Index) LazySrcLoc {
245 return .{ .node_offset = node_index - decl.srcNode() };
252 pub fn nodeSrcLoc(decl: Decl, node_index: ast.Node.Index) LazySrcLoc {
253 return .{ .node_offset = decl.nodeIndexToRelative(node_index) };
246254 }
247255
248256 pub fn srcLoc(decl: *Decl) SrcLoc {
......@@ -1076,6 +1084,40 @@ pub const Scope = struct {
10761084 return new_index + gz.zir_code.ref_start_index;
10771085 }
10781086
1087 pub fn addCall(
1088 gz: *GenZir,
1089 tag: zir.Inst.Tag,
1090 callee: zir.Inst.Ref,
1091 args: []const zir.Inst.Ref,
1092 /// Absolute node index. This function does the conversion to offset from Decl.
1093 abs_node_index: ast.Node.Index,
1094 ) !zir.Inst.Index {
1095 assert(callee != 0);
1096 assert(abs_node_index != 0);
1097 const gpa = gz.zir_code.gpa;
1098 try gz.instructions.ensureCapacity(gpa, gz.instructions.items.len + 1);
1099 try gz.zir_code.instructions.ensureCapacity(gpa, gz.zir_code.instructions.len + 1);
1100 try gz.zir_code.extra.ensureCapacity(gpa, gz.zir_code.extra.items.len +
1101 @typeInfo(zir.Inst.Call).Struct.fields.len + args.len);
1102
1103 const payload_index = gz.zir_code.addExtra(zir.Inst.Call{
1104 .callee = callee,
1105 .args_len = @intCast(u32, args.len),
1106 }) catch unreachable; // Capacity is ensured above.
1107 gz.zir_code.extra.appendSliceAssumeCapacity(args);
1108
1109 const new_index = @intCast(zir.Inst.Index, gz.zir_code.instructions.len);
1110 gz.zir_code.instructions.appendAssumeCapacity(.{
1111 .tag = tag,
1112 .data = .{ .pl_node = .{
1113 .src_node = gz.zir_code.decl.nodeIndexToRelative(abs_node_index),
1114 .payload_index = payload_index,
1115 } },
1116 });
1117 gz.instructions.appendAssumeCapacity(new_index);
1118 return new_index + gz.zir_code.ref_start_index;
1119 }
1120
10791121 pub fn addInt(gz: *GenZir, integer: u64) !zir.Inst.Ref {
10801122 return gz.add(.{
10811123 .tag = .int,
......@@ -1095,7 +1137,7 @@ pub const Scope = struct {
10951137 .tag = tag,
10961138 .data = .{ .un_node = .{
10971139 .operand = operand,
1098 .src_node = abs_node_index - gz.zir_code.decl.srcNode(),
1140 .src_node = gz.zir_code.decl.nodeIndexToRelative(abs_node_index),
10991141 } },
11001142 });
11011143 }
......@@ -1116,7 +1158,7 @@ pub const Scope = struct {
11161158 gz.zir_code.instructions.appendAssumeCapacity(.{
11171159 .tag = tag,
11181160 .data = .{ .pl_node = .{
1119 .src_node = gz.zir_code.decl.srcNode() - abs_node_index,
1161 .src_node = gz.zir_code.decl.nodeIndexToRelative(abs_node_index),
11201162 .payload_index = payload_index,
11211163 } },
11221164 });
......@@ -1177,7 +1219,7 @@ pub const Scope = struct {
11771219 ) !zir.Inst.Ref {
11781220 return gz.add(.{
11791221 .tag = tag,
1180 .data = .{ .node = abs_node_index - gz.zir_code.decl.srcNode() },
1222 .data = .{ .node = gz.zir_code.decl.nodeIndexToRelative(abs_node_index) },
11811223 });
11821224 }
11831225
......@@ -1205,14 +1247,14 @@ pub const Scope = struct {
12051247 try gz.zir_code.instructions.append(gz.zir_code.gpa, .{
12061248 .tag = tag,
12071249 .data = .{ .pl_node = .{
1208 .src_node = node - gz.zir_code.decl.srcNode(),
1250 .src_node = gz.zir_code.decl.nodeIndexToRelative(node),
12091251 .payload_index = undefined,
12101252 } },
12111253 });
12121254 return new_index;
12131255 }
12141256
1215 fn add(gz: *GenZir, inst: zir.Inst) !zir.Inst.Ref {
1257 pub fn add(gz: *GenZir, inst: zir.Inst) !zir.Inst.Ref {
12161258 const gpa = gz.zir_code.gpa;
12171259 try gz.instructions.ensureCapacity(gpa, gz.instructions.items.len + 1);
12181260 try gz.zir_code.instructions.ensureCapacity(gpa, gz.zir_code.instructions.len + 1);
......@@ -1593,7 +1635,7 @@ pub const SrcLoc = struct {
15931635 },
15941636 .node_offset => |node_off| {
15951637 const decl = src_loc.container.decl;
1596 const node_index = decl.srcNode() + node_off;
1638 const node_index = decl.relativeToNodeIndex(node_off);
15971639 const tree = decl.container.file_scope.base.tree();
15981640 const tok_index = tree.firstToken(node_index);
15991641 const token_starts = tree.tokens.items(.start);
......@@ -1659,84 +1701,84 @@ pub const LazySrcLoc = union(enum) {
16591701 /// The source location points to an AST node, which is this value offset
16601702 /// from its containing Decl node AST index.
16611703 /// The Decl is determined contextually.
1662 node_offset: u32,
1704 node_offset: i32,
16631705 /// The source location points to a variable declaration type expression,
16641706 /// found by taking this AST node index offset from the containing
16651707 /// Decl AST node, which points to a variable declaration AST node. Next, navigate
16661708 /// to the type expression.
16671709 /// The Decl is determined contextually.
1668 node_offset_var_decl_ty: u32,
1710 node_offset_var_decl_ty: i32,
16691711 /// The source location points to a for loop condition expression,
16701712 /// found by taking this AST node index offset from the containing
16711713 /// Decl AST node, which points to a for loop AST node. Next, navigate
16721714 /// to the condition expression.
16731715 /// The Decl is determined contextually.
1674 node_offset_for_cond: u32,
1716 node_offset_for_cond: i32,
16751717 /// The source location points to the first parameter of a builtin
16761718 /// function call, found by taking this AST node index offset from the containing
16771719 /// Decl AST node, which points to a builtin call AST node. Next, navigate
16781720 /// to the first parameter.
16791721 /// The Decl is determined contextually.
1680 node_offset_builtin_call_arg0: u32,
1722 node_offset_builtin_call_arg0: i32,
16811723 /// Same as `node_offset_builtin_call_arg0` except arg index 1.
1682 node_offset_builtin_call_arg1: u32,
1724 node_offset_builtin_call_arg1: i32,
16831725 /// Same as `node_offset_builtin_call_arg0` except the arg index is contextually
16841726 /// determined.
1685 node_offset_builtin_call_argn: u32,
1727 node_offset_builtin_call_argn: i32,
16861728 /// The source location points to the index expression of an array access
16871729 /// expression, found by taking this AST node index offset from the containing
16881730 /// Decl AST node, which points to an array access AST node. Next, navigate
16891731 /// to the index expression.
16901732 /// The Decl is determined contextually.
1691 node_offset_array_access_index: u32,
1733 node_offset_array_access_index: i32,
16921734 /// The source location points to the sentinel expression of a slice
16931735 /// expression, found by taking this AST node index offset from the containing
16941736 /// Decl AST node, which points to a slice AST node. Next, navigate
16951737 /// to the sentinel expression.
16961738 /// The Decl is determined contextually.
1697 node_offset_slice_sentinel: u32,
1739 node_offset_slice_sentinel: i32,
16981740 /// The source location points to the callee expression of a function
16991741 /// call expression, found by taking this AST node index offset from the containing
17001742 /// Decl AST node, which points to a function call AST node. Next, navigate
17011743 /// to the callee expression.
17021744 /// The Decl is determined contextually.
1703 node_offset_call_func: u32,
1745 node_offset_call_func: i32,
17041746 /// The source location points to the field name of a field access expression,
17051747 /// found by taking this AST node index offset from the containing
17061748 /// Decl AST node, which points to a field access AST node. Next, navigate
17071749 /// to the field name token.
17081750 /// The Decl is determined contextually.
1709 node_offset_field_name: u32,
1751 node_offset_field_name: i32,
17101752 /// The source location points to the pointer of a pointer deref expression,
17111753 /// found by taking this AST node index offset from the containing
17121754 /// Decl AST node, which points to a pointer deref AST node. Next, navigate
17131755 /// to the pointer expression.
17141756 /// The Decl is determined contextually.
1715 node_offset_deref_ptr: u32,
1757 node_offset_deref_ptr: i32,
17161758 /// The source location points to the assembly source code of an inline assembly
17171759 /// expression, found by taking this AST node index offset from the containing
17181760 /// Decl AST node, which points to inline assembly AST node. Next, navigate
17191761 /// to the asm template source code.
17201762 /// The Decl is determined contextually.
1721 node_offset_asm_source: u32,
1763 node_offset_asm_source: i32,
17221764 /// The source location points to the return type of an inline assembly
17231765 /// expression, found by taking this AST node index offset from the containing
17241766 /// Decl AST node, which points to inline assembly AST node. Next, navigate
17251767 /// to the return type expression.
17261768 /// The Decl is determined contextually.
1727 node_offset_asm_ret_ty: u32,
1769 node_offset_asm_ret_ty: i32,
17281770 /// The source location points to the condition expression of an if
17291771 /// expression, found by taking this AST node index offset from the containing
17301772 /// Decl AST node, which points to an if expression AST node. Next, navigate
17311773 /// to the condition expression.
17321774 /// The Decl is determined contextually.
1733 node_offset_if_cond: u32,
1775 node_offset_if_cond: i32,
17341776 /// The source location points to the type expression of an `anyframe->T`
17351777 /// expression, found by taking this AST node index offset from the containing
17361778 /// Decl AST node, which points to a `anyframe->T` expression AST node. Next, navigate
17371779 /// to the type expression.
17381780 /// The Decl is determined contextually.
1739 node_offset_anyframe_type: u32,
1781 node_offset_anyframe_type: i32,
17401782
17411783 /// Upgrade to a `SrcLoc` based on the `Decl` or file in the provided scope.
17421784 pub fn toSrcLoc(lazy: LazySrcLoc, scope: *Scope) SrcLoc {
......@@ -3678,8 +3720,7 @@ pub fn failTok(
36783720 comptime format: []const u8,
36793721 args: anytype,
36803722) InnerError {
3681 const decl_token = scope.srcDecl().?.srcToken();
3682 const src: LazySrcLoc = .{ .token_offset = token_index - decl_token };
3723 const src = scope.srcDecl().?.tokSrcLoc(token_index);
36833724 return mod.fail(scope, src, format, args);
36843725}
36853726
......@@ -3692,8 +3733,7 @@ pub fn failNode(
36923733 comptime format: []const u8,
36933734 args: anytype,
36943735) InnerError {
3695 const decl_node = scope.srcDecl().?.srcNode();
3696 const src: LazySrcLoc = .{ .node_offset = decl_node - node_index };
3736 const src = scope.srcDecl().?.nodeSrcLoc(node_index);
36973737 return mod.fail(scope, src, format, args);
36983738}
36993739
src/Sema.zig+1-1
......@@ -618,7 +618,7 @@ fn zirParamType(sema: *Sema, block: *Scope.Block, inst: zir.Inst.Index) InnerErr
618618 const tracy = trace(@src());
619619 defer tracy.end();
620620
621 const src: LazySrcLoc = .todo;
621 const src: LazySrcLoc = .unneeded;
622622 const inst_data = sema.code.instructions.items(.data)[inst].param_type;
623623 const fn_inst = try sema.resolveInst(inst_data.callee);
624624 const param_index = inst_data.param_index;
src/astgen.zig+10-11
......@@ -3402,10 +3402,6 @@ fn callExpr(
34023402 node: ast.Node.Index,
34033403 call: ast.full.Call,
34043404) InnerError!zir.Inst.Ref {
3405 if (true) {
3406 @panic("TODO update for zir-memory-layout branch");
3407 }
3408
34093405 if (call.async_token) |async_token| {
34103406 return mod.failTok(scope, async_token, "TODO implement async fn call", .{});
34113407 }
......@@ -3414,11 +3410,14 @@ fn callExpr(
34143410 const args = try mod.gpa.alloc(zir.Inst.Index, call.ast.params.len);
34153411 defer mod.gpa.free(args);
34163412
3417 const gen_zir = scope.getGenZir();
3413 const gz = scope.getGenZir();
34183414 for (call.ast.params) |param_node, i| {
3419 const param_type = try gen_zir.addParamType(.{
3420 .callee = lhs,
3421 .param_index = i,
3415 const param_type = try gz.add(.{
3416 .tag = .param_type,
3417 .data = .{ .param_type = .{
3418 .callee = lhs,
3419 .param_index = @intCast(u32, i),
3420 } },
34223421 });
34233422 args[i] = try expr(mod, scope, .{ .ty = param_type }, param_node);
34243423 }
......@@ -3430,7 +3429,7 @@ fn callExpr(
34303429 const result: zir.Inst.Index = res: {
34313430 const tag: zir.Inst.Tag = switch (modifier) {
34323431 .auto => switch (args.len == 0) {
3433 true => break :res try gen_zir.addCallNone(lhs, node),
3432 true => break :res try gz.addUnNode(.call_none, lhs, node),
34343433 false => .call,
34353434 },
34363435 .async_kw => .call_async_kw,
......@@ -3441,9 +3440,9 @@ fn callExpr(
34413440 .always_inline => unreachable,
34423441 .compile_time => .call_compile_time,
34433442 };
3444 break :res try gen_zir.addCall(tag, lhs, args, node);
3443 break :res try gz.addCall(tag, lhs, args, node);
34453444 };
3446 return rvalue(mod, scope, rl, result); // TODO function call with result location
3445 return rvalue(mod, scope, rl, result, node); // TODO function call with result location
34473446}
34483447
34493448fn suspendExpr(mod: *Module, scope: *Scope, node: ast.Node.Index) InnerError!zir.Inst.Ref {
src/zir.zig+17-6
......@@ -1019,7 +1019,7 @@ pub const Inst = struct {
10191019 /// Used for unary operators, with an AST node source location.
10201020 un_node: struct {
10211021 /// Offset from Decl AST node index.
1022 src_node: ast.Node.Index,
1022 src_node: i32,
10231023 /// The meaning of this operand depends on the corresponding `Tag`.
10241024 operand: Ref,
10251025
......@@ -1041,7 +1041,7 @@ pub const Inst = struct {
10411041 pl_node: struct {
10421042 /// Offset from Decl AST node index.
10431043 /// `Tag` determines which kind of AST node this points to.
1044 src_node: ast.Node.Index,
1044 src_node: i32,
10451045 /// index into extra.
10461046 /// `Tag` determines what lives there.
10471047 payload_index: u32,
......@@ -1092,7 +1092,7 @@ pub const Inst = struct {
10921092 /// Offset from Decl AST token index.
10931093 tok: ast.TokenIndex,
10941094 /// Offset from Decl AST node index.
1095 node: ast.Node.Index,
1095 node: i32,
10961096 int: u64,
10971097 array_type_sentinel: struct {
10981098 len: Ref,
......@@ -1400,9 +1400,10 @@ const Writer = struct {
14001400 .slice_sentinel,
14011401 .typeof_peer,
14021402 .suspend_block,
1403 .as_node,
14041403 => try self.writePlNode(stream, inst),
14051404
1405 .as_node => try self.writeAs(stream, inst),
1406
14061407 .breakpoint,
14071408 .dbg_stmt_node,
14081409 .ret_ptr,
......@@ -1544,6 +1545,16 @@ const Writer = struct {
15441545 try self.writeSrc(stream, inst_data.src());
15451546 }
15461547
1548 fn writeAs(self: *Writer, stream: anytype, inst: Inst.Index) !void {
1549 const inst_data = self.code.instructions.items(.data)[inst].pl_node;
1550 const extra = self.code.extraData(Inst.As, inst_data.payload_index).data;
1551 try self.writeInstRef(stream, extra.dest_type);
1552 try stream.writeAll(", ");
1553 try self.writeInstRef(stream, extra.operand);
1554 try stream.writeAll(") ");
1555 try self.writeSrc(stream, inst_data.src());
1556 }
1557
15471558 fn writeNode(
15481559 self: *Writer,
15491560 stream: anytype,
......@@ -1560,8 +1571,8 @@ const Writer = struct {
15601571 stream: anytype,
15611572 inst: Inst.Index,
15621573 ) (@TypeOf(stream).Error || error{OutOfMemory})!void {
1563 const inst_data = self.code.instructions.items(.data)[inst].decl;
1564 try stream.writeAll("TODO)");
1574 const decl = self.code.instructions.items(.data)[inst].decl;
1575 try stream.print("{s})", .{decl.name});
15651576 }
15661577
15671578 fn writeStrTok(