authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2021-04-19 23:23:24-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2021-04-19 23:23:24-07:00
loge315120b79f98163dd6413e62684d6ad148295ee
treeabc748fa6b802d4007741ba1ef0fbf1fef1ef152
parent693dbeeef2a35737cec893b50a4fed11b248dd6a

AstGen: implement array initialization expressions


5 files changed, 401 insertions(+), 120 deletions(-)

lib/std/zig/render.zig-1
...@@ -1590,7 +1590,6 @@ fn renderStructInit(...@@ -1590,7 +1590,6 @@ fn renderStructInit(
1590 return renderToken(ais, tree, rbrace, space);1590 return renderToken(ais, tree, rbrace, space);
1591}1591}
15921592
1593// TODO: handle comments between elements
1594fn renderArrayInit(1593fn renderArrayInit(
1595 gpa: *Allocator,1594 gpa: *Allocator,
1596 ais: *Ais,1595 ais: *Ais,
src/AstGen.zig+218-28
...@@ -822,15 +822,20 @@ pub fn expr(gz: *GenZir, scope: *Scope, rl: ResultLoc, node: ast.Node.Index) Inn...@@ -822,15 +822,20 @@ pub fn expr(gz: *GenZir, scope: *Scope, rl: ResultLoc, node: ast.Node.Index) Inn
822 .@"errdefer" => return astgen.failNode(node, "TODO implement astgen.expr for .errdefer", .{}),822 .@"errdefer" => return astgen.failNode(node, "TODO implement astgen.expr for .errdefer", .{}),
823 .@"try" => return astgen.failNode(node, "TODO implement astgen.expr for .Try", .{}),823 .@"try" => return astgen.failNode(node, "TODO implement astgen.expr for .Try", .{}),
824824
825 .array_init_one,825 .array_init_one, .array_init_one_comma => {
826 .array_init_one_comma,826 var elements: [1]ast.Node.Index = undefined;
827 .array_init_dot_two,827 return arrayInitExpr(gz, scope, rl, node, tree.arrayInitOne(&elements, node));
828 .array_init_dot_two_comma,828 },
829 .array_init_dot_two, .array_init_dot_two_comma => {
830 var elements: [2]ast.Node.Index = undefined;
831 return arrayInitExpr(gz, scope, rl, node, tree.arrayInitDotTwo(&elements, node));
832 },
829 .array_init_dot,833 .array_init_dot,
830 .array_init_dot_comma,834 .array_init_dot_comma,
835 => return arrayInitExpr(gz, scope, rl, node, tree.arrayInitDot(node)),
831 .array_init,836 .array_init,
832 .array_init_comma,837 .array_init_comma,
833 => return astgen.failNode(node, "TODO implement astgen.expr for array literals", .{}),838 => return arrayInitExpr(gz, scope, rl, node, tree.arrayInit(node)),
834839
835 .struct_init_one, .struct_init_one_comma => {840 .struct_init_one, .struct_init_one_comma => {
836 var fields: [1]ast.Node.Index = undefined;841 var fields: [1]ast.Node.Index = undefined;
...@@ -856,6 +861,182 @@ pub fn expr(gz: *GenZir, scope: *Scope, rl: ResultLoc, node: ast.Node.Index) Inn...@@ -856,6 +861,182 @@ pub fn expr(gz: *GenZir, scope: *Scope, rl: ResultLoc, node: ast.Node.Index) Inn
856 }861 }
857}862}
858863
864pub fn arrayInitExpr(
865 gz: *GenZir,
866 scope: *Scope,
867 rl: ResultLoc,
868 node: ast.Node.Index,
869 array_init: ast.full.ArrayInit,
870) InnerError!Zir.Inst.Ref {
871 const astgen = gz.astgen;
872 const tree = &astgen.file.tree;
873 const gpa = astgen.gpa;
874 const node_tags = tree.nodes.items(.tag);
875 const main_tokens = tree.nodes.items(.main_token);
876
877 assert(array_init.ast.elements.len != 0); // Otherwise it would be struct init.
878
879 const types: struct {
880 array: Zir.Inst.Ref,
881 elem: Zir.Inst.Ref,
882 } = inst: {
883 if (array_init.ast.type_expr == 0) break :inst .{
884 .array = .none,
885 .elem = .none,
886 };
887
888 infer: {
889 const array_type: ast.full.ArrayType = switch (node_tags[array_init.ast.type_expr]) {
890 .array_type => tree.arrayType(array_init.ast.type_expr),
891 .array_type_sentinel => tree.arrayTypeSentinel(array_init.ast.type_expr),
892 else => break :infer,
893 };
894 // This intentionally does not support `@"_"` syntax.
895 if (node_tags[array_type.ast.elem_count] == .identifier and
896 mem.eql(u8, tree.tokenSlice(main_tokens[array_type.ast.elem_count]), "_"))
897 {
898 const tag: Zir.Inst.Tag = switch (node_tags[array_init.ast.type_expr]) {
899 .array_type => .array_type,
900 .array_type_sentinel => .array_type_sentinel,
901 else => unreachable,
902 };
903 const len_inst = try gz.addInt(array_init.ast.elements.len);
904 const elem_type = try typeExpr(gz, scope, array_type.ast.elem_type);
905 const array_type_inst = try gz.addBin(tag, len_inst, elem_type);
906 break :inst .{
907 .array = array_type_inst,
908 .elem = elem_type,
909 };
910 }
911 }
912 const array_type_inst = try typeExpr(gz, scope, array_init.ast.type_expr);
913 const elem_type = try gz.addUnNode(.elem_type, array_type_inst, array_init.ast.type_expr);
914 break :inst .{
915 .array = array_type_inst,
916 .elem = elem_type,
917 };
918 };
919
920 switch (rl) {
921 .discard => {
922 for (array_init.ast.elements) |elem_init| {
923 _ = try expr(gz, scope, .discard, elem_init);
924 }
925 return Zir.Inst.Ref.void_value;
926 },
927 .ref => {
928 if (types.array != .none) {
929 return arrayInitExprRlTy(gz, scope, rl, node, array_init.ast.elements, types.array, types.elem, .array_init_ref);
930 } else {
931 return arrayInitExprRlNone(gz, scope, rl, node, array_init.ast.elements, .array_init_anon_ref);
932 }
933 },
934 .none, .none_or_ref => {
935 if (types.array != .none) {
936 return arrayInitExprRlTy(gz, scope, rl, node, array_init.ast.elements, types.array, types.elem, .array_init);
937 } else {
938 return arrayInitExprRlNone(gz, scope, rl, node, array_init.ast.elements, .array_init_anon);
939 }
940 },
941 .ty => |ty_inst| {
942 if (types.array != .none) {
943 const result = try arrayInitExprRlTy(gz, scope, rl, node, array_init.ast.elements, types.array, types.elem, .array_init);
944 return rvalue(gz, scope, rl, result, node);
945 } else {
946 const elem_type = try gz.addUnNode(.elem_type, ty_inst, node);
947 return arrayInitExprRlTy(gz, scope, rl, node, array_init.ast.elements, ty_inst, elem_type, .array_init);
948 }
949 },
950 .ptr, .inferred_ptr => |ptr_inst| {
951 return arrayInitExprRlPtr(gz, scope, rl, node, array_init.ast.elements, ptr_inst);
952 },
953 .block_ptr => |block_gz| {
954 return arrayInitExprRlPtr(gz, scope, rl, node, array_init.ast.elements, block_gz.rl_ptr);
955 },
956 }
957}
958
959pub fn arrayInitExprRlNone(
960 gz: *GenZir,
961 scope: *Scope,
962 rl: ResultLoc,
963 node: ast.Node.Index,
964 elements: []const ast.Node.Index,
965 tag: Zir.Inst.Tag,
966) InnerError!Zir.Inst.Ref {
967 const astgen = gz.astgen;
968 const gpa = astgen.gpa;
969 const elem_list = try gpa.alloc(Zir.Inst.Ref, elements.len);
970 defer gpa.free(elem_list);
971
972 for (elements) |elem_init, i| {
973 elem_list[i] = try expr(gz, scope, .none, elem_init);
974 }
975 const init_inst = try gz.addPlNode(tag, node, Zir.Inst.MultiOp{
976 .operands_len = @intCast(u32, elem_list.len),
977 });
978 try astgen.appendRefs(elem_list);
979 return init_inst;
980}
981
982pub fn arrayInitExprRlTy(
983 gz: *GenZir,
984 scope: *Scope,
985 rl: ResultLoc,
986 node: ast.Node.Index,
987 elements: []const ast.Node.Index,
988 array_ty_inst: Zir.Inst.Ref,
989 elem_ty_inst: Zir.Inst.Ref,
990 tag: Zir.Inst.Tag,
991) InnerError!Zir.Inst.Ref {
992 const astgen = gz.astgen;
993 const gpa = astgen.gpa;
994
995 const elem_list = try gpa.alloc(Zir.Inst.Ref, elements.len);
996 defer gpa.free(elem_list);
997
998 const elem_rl: ResultLoc = .{ .ty = elem_ty_inst };
999
1000 for (elements) |elem_init, i| {
1001 elem_list[i] = try expr(gz, scope, elem_rl, elem_init);
1002 }
1003 const init_inst = try gz.addPlNode(tag, node, Zir.Inst.MultiOp{
1004 .operands_len = @intCast(u32, elem_list.len),
1005 });
1006 try astgen.appendRefs(elem_list);
1007 return init_inst;
1008}
1009
1010pub fn arrayInitExprRlPtr(
1011 gz: *GenZir,
1012 scope: *Scope,
1013 rl: ResultLoc,
1014 node: ast.Node.Index,
1015 elements: []const ast.Node.Index,
1016 result_ptr: Zir.Inst.Ref,
1017) InnerError!Zir.Inst.Ref {
1018 const astgen = gz.astgen;
1019 const gpa = astgen.gpa;
1020
1021 const elem_ptr_list = try gpa.alloc(Zir.Inst.Index, elements.len);
1022 defer gpa.free(elem_ptr_list);
1023
1024 for (elements) |elem_init, i| {
1025 const index_inst = try gz.addInt(i);
1026 const elem_ptr = try gz.addPlNode(.elem_ptr_node, elem_init, Zir.Inst.Bin{
1027 .lhs = result_ptr,
1028 .rhs = index_inst,
1029 });
1030 elem_ptr_list[i] = gz.refToIndex(elem_ptr).?;
1031 _ = try expr(gz, scope, .{ .ptr = elem_ptr }, elem_init);
1032 }
1033 _ = try gz.addPlNode(.validate_array_init_ptr, node, Zir.Inst.Block{
1034 .body_len = @intCast(u32, elem_ptr_list.len),
1035 });
1036 try astgen.extra.appendSlice(gpa, elem_ptr_list);
1037 return .void_value;
1038}
1039
859pub fn structInitExpr(1040pub fn structInitExpr(
860 gz: *GenZir,1041 gz: *GenZir,
861 scope: *Scope,1042 scope: *Scope,
...@@ -911,7 +1092,14 @@ pub fn structInitExpr(...@@ -911,7 +1092,14 @@ pub fn structInitExpr(
911 return init_inst;1092 return init_inst;
912 },1093 },
913 .ref => unreachable, // struct literal not valid as l-value1094 .ref => unreachable, // struct literal not valid as l-value
914 .ty => |ty_inst| return structInitExprRlTy(gz, scope, rl, node, struct_init, ty_inst),1095 .ty => |ty_inst| {
1096 if (struct_init.ast.type_expr == 0) {
1097 return structInitExprRlTy(gz, scope, rl, node, struct_init, ty_inst);
1098 }
1099 const inner_ty_inst = try typeExpr(gz, scope, struct_init.ast.type_expr);
1100 const result = try structInitExprRlTy(gz, scope, rl, node, struct_init, inner_ty_inst);
1101 return rvalue(gz, scope, rl, result, node);
1102 },
915 .ptr, .inferred_ptr => |ptr_inst| return structInitExprRlPtr(gz, scope, rl, node, struct_init, ptr_inst),1103 .ptr, .inferred_ptr => |ptr_inst| return structInitExprRlPtr(gz, scope, rl, node, struct_init, ptr_inst),
916 .block_ptr => |block_gz| return structInitExprRlPtr(gz, scope, rl, node, struct_init, block_gz.rl_ptr),1104 .block_ptr => |block_gz| return structInitExprRlPtr(gz, scope, rl, node, struct_init, block_gz.rl_ptr),
917 }1105 }
...@@ -942,11 +1130,11 @@ pub fn structInitExprRlPtr(...@@ -942,11 +1130,11 @@ pub fn structInitExprRlPtr(
942 field_ptr_list[i] = gz.refToIndex(field_ptr).?;1130 field_ptr_list[i] = gz.refToIndex(field_ptr).?;
943 _ = try expr(gz, scope, .{ .ptr = field_ptr }, field_init);1131 _ = try expr(gz, scope, .{ .ptr = field_ptr }, field_init);
944 }1132 }
945 const validate_inst = try gz.addPlNode(.validate_struct_init_ptr, node, Zir.Inst.Block{1133 _ = try gz.addPlNode(.validate_struct_init_ptr, node, Zir.Inst.Block{
946 .body_len = @intCast(u32, field_ptr_list.len),1134 .body_len = @intCast(u32, field_ptr_list.len),
947 });1135 });
948 try astgen.extra.appendSlice(gpa, field_ptr_list);1136 try astgen.extra.appendSlice(gpa, field_ptr_list);
949 return validate_inst;1137 return .void_value;
950}1138}
9511139
952pub fn structInitExprRlTy(1140pub fn structInitExprRlTy(
...@@ -1336,6 +1524,7 @@ fn blockExprStmts(...@@ -1336,6 +1524,7 @@ fn blockExprStmts(
1336 .array_mul,1524 .array_mul,
1337 .array_type,1525 .array_type,
1338 .array_type_sentinel,1526 .array_type_sentinel,
1527 .elem_type,
1339 .indexable_ptr_len,1528 .indexable_ptr_len,
1340 .as,1529 .as,
1341 .as_node,1530 .as_node,
...@@ -1393,8 +1582,6 @@ fn blockExprStmts(...@@ -1393,8 +1582,6 @@ fn blockExprStmts(
1393 .param_type,1582 .param_type,
1394 .ptrtoint,1583 .ptrtoint,
1395 .ref,1584 .ref,
1396 .ret_ptr,
1397 .ret_type,
1398 .shl,1585 .shl,
1399 .shr,1586 .shr,
1400 .str,1587 .str,
...@@ -1453,6 +1640,10 @@ fn blockExprStmts(...@@ -1453,6 +1640,10 @@ fn blockExprStmts(
1453 .struct_init_empty,1640 .struct_init_empty,
1454 .struct_init,1641 .struct_init,
1455 .struct_init_anon,1642 .struct_init_anon,
1643 .array_init,
1644 .array_init_anon,
1645 .array_init_ref,
1646 .array_init_anon_ref,
1456 .union_init_ptr,1647 .union_init_ptr,
1457 .field_type,1648 .field_type,
1458 .field_type_ref,1649 .field_type_ref,
...@@ -1469,18 +1660,12 @@ fn blockExprStmts(...@@ -1469,18 +1660,12 @@ fn blockExprStmts(
1469 .type_info,1660 .type_info,
1470 .size_of,1661 .size_of,
1471 .bit_size_of,1662 .bit_size_of,
1472 .this,
1473 .ret_addr,
1474 .builtin_src,
1475 .add_with_overflow,1663 .add_with_overflow,
1476 .sub_with_overflow,1664 .sub_with_overflow,
1477 .mul_with_overflow,1665 .mul_with_overflow,
1478 .shl_with_overflow,1666 .shl_with_overflow,
1479 .log2_int_type,1667 .log2_int_type,
1480 .typeof_log2_int_type,1668 .typeof_log2_int_type,
1481 .error_return_trace,
1482 .frame,
1483 .frame_address,
1484 .ptr_to_int,1669 .ptr_to_int,
1485 .align_of,1670 .align_of,
1486 .bool_to_int,1671 .bool_to_int,
...@@ -1575,6 +1760,7 @@ fn blockExprStmts(...@@ -1575,6 +1760,7 @@ fn blockExprStmts(
1575 .repeat,1760 .repeat,
1576 .repeat_inline,1761 .repeat_inline,
1577 .validate_struct_init_ptr,1762 .validate_struct_init_ptr,
1763 .validate_array_init_ptr,
1578 .panic,1764 .panic,
1579 .set_align_stack,1765 .set_align_stack,
1580 .set_cold,1766 .set_cold,
...@@ -2572,13 +2758,17 @@ fn structDeclInner(...@@ -2572,13 +2758,17 @@ fn structDeclInner(
25722758
2573 field_index += 1;2759 field_index += 1;
2574 }2760 }
2575 if (field_index != 0) {2761 {
2576 const empty_slot_count = 16 - (field_index % 16);2762 const empty_slot_count = 16 - (field_index % 16);
2577 cur_bit_bag >>= @intCast(u5, empty_slot_count * 2);2763 if (empty_slot_count < 16) {
2764 cur_bit_bag >>= @intCast(u5, empty_slot_count * 2);
2765 }
2578 }2766 }
2579 if (wip_decls.decl_index != 0) {2767 {
2580 const empty_slot_count = 16 - (wip_decls.decl_index % 16);2768 const empty_slot_count = 16 - (wip_decls.decl_index % 16);
2581 wip_decls.cur_bit_bag >>= @intCast(u5, empty_slot_count * 2);2769 if (empty_slot_count < 16) {
2770 wip_decls.cur_bit_bag >>= @intCast(u5, empty_slot_count * 2);
2771 }
2582 }2772 }
25832773
2584 const decl_inst = try gz.addBlock(tag, node);2774 const decl_inst = try gz.addBlock(tag, node);
...@@ -4609,9 +4799,9 @@ fn ret(gz: *GenZir, scope: *Scope, node: ast.Node.Index) InnerError!Zir.Inst.Ref...@@ -4609,9 +4799,9 @@ fn ret(gz: *GenZir, scope: *Scope, node: ast.Node.Index) InnerError!Zir.Inst.Ref
4609 const operand_node = node_datas[node].lhs;4799 const operand_node = node_datas[node].lhs;
4610 const operand: Zir.Inst.Ref = if (operand_node != 0) operand: {4800 const operand: Zir.Inst.Ref = if (operand_node != 0) operand: {
4611 const rl: ResultLoc = if (nodeMayNeedMemoryLocation(tree, operand_node)) .{4801 const rl: ResultLoc = if (nodeMayNeedMemoryLocation(tree, operand_node)) .{
4612 .ptr = try gz.addNode(.ret_ptr, node),4802 .ptr = try gz.addNodeExtended(.ret_ptr, node),
4613 } else .{4803 } else .{
4614 .ty = try gz.addNode(.ret_type, node),4804 .ty = try gz.addNodeExtended(.ret_type, node),
4615 };4805 };
4616 break :operand try expr(gz, scope, rl, operand_node);4806 break :operand try expr(gz, scope, rl, operand_node);
4617 } else .void_value;4807 } else .void_value;
...@@ -5203,12 +5393,12 @@ fn builtinCall(...@@ -5203,12 +5393,12 @@ fn builtinCall(
5203 .breakpoint => return simpleNoOpVoid(gz, scope, rl, node, .breakpoint),5393 .breakpoint => return simpleNoOpVoid(gz, scope, rl, node, .breakpoint),
5204 .fence => return simpleNoOpVoid(gz, scope, rl, node, .fence),5394 .fence => return simpleNoOpVoid(gz, scope, rl, node, .fence),
52055395
5206 .This => return rvalue(gz, scope, rl, try gz.addNode(.this, node), node),5396 .This => return rvalue(gz, scope, rl, try gz.addNodeExtended(.this, node), node),
5207 .return_address => return rvalue(gz, scope, rl, try gz.addNode(.ret_addr, node), node),5397 .return_address => return rvalue(gz, scope, rl, try gz.addNodeExtended(.ret_addr, node), node),
5208 .src => return rvalue(gz, scope, rl, try gz.addNode(.builtin_src, node), node),5398 .src => return rvalue(gz, scope, rl, try gz.addNodeExtended(.builtin_src, node), node),
5209 .error_return_trace => return rvalue(gz, scope, rl, try gz.addNode(.error_return_trace, node), node),5399 .error_return_trace => return rvalue(gz, scope, rl, try gz.addNodeExtended(.error_return_trace, node), node),
5210 .frame => return rvalue(gz, scope, rl, try gz.addNode(.frame, node), node),5400 .frame => return rvalue(gz, scope, rl, try gz.addNodeExtended(.frame, node), node),
5211 .frame_address => return rvalue(gz, scope, rl, try gz.addNode(.frame_address, node), node),5401 .frame_address => return rvalue(gz, scope, rl, try gz.addNodeExtended(.frame_address, node), node),
52125402
5213 .type_info => return simpleUnOpType(gz, scope, rl, node, params[0], .type_info),5403 .type_info => return simpleUnOpType(gz, scope, rl, node, params[0], .type_info),
5214 .size_of => return simpleUnOpType(gz, scope, rl, node, params[0], .size_of),5404 .size_of => return simpleUnOpType(gz, scope, rl, node, params[0], .size_of),
src/Module.zig+17
...@@ -1622,6 +1622,22 @@ pub const Scope = struct {...@@ -1622,6 +1622,22 @@ pub const Scope = struct {
1622 });1622 });
1623 }1623 }
16241624
1625 pub fn addNodeExtended(
1626 gz: *GenZir,
1627 opcode: Zir.Inst.Extended,
1628 /// Absolute node index. This function does the conversion to offset from Decl.
1629 src_node: ast.Node.Index,
1630 ) !Zir.Inst.Ref {
1631 return gz.add(.{
1632 .tag = .extended,
1633 .data = .{ .extended = .{
1634 .opcode = opcode,
1635 .small = undefined,
1636 .operand = @bitCast(u32, gz.nodeIndexToRelative(src_node)),
1637 } },
1638 });
1639 }
1640
1625 /// Asserts that `str` is 8 or fewer bytes.1641 /// Asserts that `str` is 8 or fewer bytes.
1626 pub fn addSmallStr(1642 pub fn addSmallStr(
1627 gz: *GenZir,1643 gz: *GenZir,
...@@ -2583,6 +2599,7 @@ pub fn astGenFile(mod: *Module, file: *Scope.File, prog_node: *std.Progress.Node...@@ -2583,6 +2599,7 @@ pub fn astGenFile(mod: *Module, file: *Scope.File, prog_node: *std.Progress.Node
2583 return error.AnalysisFail;2599 return error.AnalysisFail;
2584 }2600 }
25852601
2602 log.debug("AstGen success: {s}", .{file.sub_file_path});
2586 file.status = .success;2603 file.status = .success;
2587}2604}
25882605
src/Sema.zig+105-50
...@@ -175,6 +175,7 @@ pub fn analyzeBody(...@@ -175,6 +175,7 @@ pub fn analyzeBody(
175 .elem_ptr_node => try sema.zirElemPtrNode(block, inst),175 .elem_ptr_node => try sema.zirElemPtrNode(block, inst),
176 .elem_val => try sema.zirElemVal(block, inst),176 .elem_val => try sema.zirElemVal(block, inst),
177 .elem_val_node => try sema.zirElemValNode(block, inst),177 .elem_val_node => try sema.zirElemValNode(block, inst),
178 .elem_type => try sema.zirElemType(block, inst),
178 .enum_literal => try sema.zirEnumLiteral(block, inst),179 .enum_literal => try sema.zirEnumLiteral(block, inst),
179 .enum_literal_small => try sema.zirEnumLiteralSmall(block, inst),180 .enum_literal_small => try sema.zirEnumLiteralSmall(block, inst),
180 .enum_to_int => try sema.zirEnumToInt(block, inst),181 .enum_to_int => try sema.zirEnumToInt(block, inst),
...@@ -223,8 +224,6 @@ pub fn analyzeBody(...@@ -223,8 +224,6 @@ pub fn analyzeBody(
223 .ptr_type_simple => try sema.zirPtrTypeSimple(block, inst),224 .ptr_type_simple => try sema.zirPtrTypeSimple(block, inst),
224 .ptrtoint => try sema.zirPtrtoint(block, inst),225 .ptrtoint => try sema.zirPtrtoint(block, inst),
225 .ref => try sema.zirRef(block, inst),226 .ref => try sema.zirRef(block, inst),
226 .ret_ptr => try sema.zirRetPtr(block, inst),
227 .ret_type => try sema.zirRetType(block, inst),
228 .shl => try sema.zirShl(block, inst),227 .shl => try sema.zirShl(block, inst),
229 .shr => try sema.zirShr(block, inst),228 .shr => try sema.zirShr(block, inst),
230 .slice_end => try sema.zirSliceEnd(block, inst),229 .slice_end => try sema.zirSliceEnd(block, inst),
...@@ -252,9 +251,6 @@ pub fn analyzeBody(...@@ -252,9 +251,6 @@ pub fn analyzeBody(
252 .type_info => try sema.zirTypeInfo(block, inst),251 .type_info => try sema.zirTypeInfo(block, inst),
253 .size_of => try sema.zirSizeOf(block, inst),252 .size_of => try sema.zirSizeOf(block, inst),
254 .bit_size_of => try sema.zirBitSizeOf(block, inst),253 .bit_size_of => try sema.zirBitSizeOf(block, inst),
255 .this => try sema.zirThis(block, inst),
256 .ret_addr => try sema.zirRetAddr(block, inst),
257 .builtin_src => try sema.zirBuiltinSrc(block, inst),
258 .typeof => try sema.zirTypeof(block, inst),254 .typeof => try sema.zirTypeof(block, inst),
259 .typeof_elem => try sema.zirTypeofElem(block, inst),255 .typeof_elem => try sema.zirTypeofElem(block, inst),
260 .typeof_peer => try sema.zirTypeofPeer(block, inst),256 .typeof_peer => try sema.zirTypeofPeer(block, inst),
...@@ -264,12 +260,13 @@ pub fn analyzeBody(...@@ -264,12 +260,13 @@ pub fn analyzeBody(
264 .struct_init_empty => try sema.zirStructInitEmpty(block, inst),260 .struct_init_empty => try sema.zirStructInitEmpty(block, inst),
265 .struct_init => try sema.zirStructInit(block, inst),261 .struct_init => try sema.zirStructInit(block, inst),
266 .struct_init_anon => try sema.zirStructInitAnon(block, inst),262 .struct_init_anon => try sema.zirStructInitAnon(block, inst),
263 .array_init => try sema.zirArrayInit(block, inst, false),
264 .array_init_anon => try sema.zirArrayInitAnon(block, inst, false),
265 .array_init_ref => try sema.zirArrayInit(block, inst, true),
266 .array_init_anon_ref => try sema.zirArrayInitAnon(block, inst, true),
267 .union_init_ptr => try sema.zirUnionInitPtr(block, inst),267 .union_init_ptr => try sema.zirUnionInitPtr(block, inst),
268 .field_type => try sema.zirFieldType(block, inst),268 .field_type => try sema.zirFieldType(block, inst),
269 .field_type_ref => try sema.zirFieldTypeRef(block, inst),269 .field_type_ref => try sema.zirFieldTypeRef(block, inst),
270 .error_return_trace => try sema.zirErrorReturnTrace(block, inst),
271 .frame => try sema.zirFrame(block, inst),
272 .frame_address => try sema.zirFrameAddress(block, inst),
273 .ptr_to_int => try sema.zirPtrToInt(block, inst),270 .ptr_to_int => try sema.zirPtrToInt(block, inst),
274 .align_of => try sema.zirAlignOf(block, inst),271 .align_of => try sema.zirAlignOf(block, inst),
275 .bool_to_int => try sema.zirBoolToInt(block, inst),272 .bool_to_int => try sema.zirBoolToInt(block, inst),
...@@ -435,6 +432,10 @@ pub fn analyzeBody(...@@ -435,6 +432,10 @@ pub fn analyzeBody(
435 try sema.zirValidateStructInitPtr(block, inst);432 try sema.zirValidateStructInitPtr(block, inst);
436 continue;433 continue;
437 },434 },
435 .validate_array_init_ptr => {
436 try sema.zirValidateArrayInitPtr(block, inst);
437 continue;
438 },
438 .@"export" => {439 .@"export" => {
439 try sema.zirExport(block, inst);440 try sema.zirExport(block, inst);
440 continue;441 continue;
...@@ -499,6 +500,28 @@ pub fn analyzeBody(...@@ -499,6 +500,28 @@ pub fn analyzeBody(
499 }500 }
500}501}
501502
503fn zirExtended(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) InnerError!*Inst {
504 const extended = sema.code.instructions.items(.data)[inst].extended;
505 switch (extended.opcode) {
506 // zig fmt: off
507 .func => return sema.zirFuncExtended( block, extended),
508 .ret_ptr => return sema.zirRetPtr( block, extended),
509 .ret_type => return sema.zirRetType( block, extended),
510 .this => return sema.zirThis( block, extended),
511 .ret_addr => return sema.zirRetAddr( block, extended),
512 .builtin_src => return sema.zirBuiltinSrc( block, extended),
513 .error_return_trace => return sema.zirErrorReturnTrace(block, extended),
514 .frame => return sema.zirFrame( block, extended),
515 .frame_address => return sema.zirFrameAddress( block, extended),
516 .c_undef => return sema.zirCUndef( block, extended),
517 .c_include => return sema.zirCInclude( block, extended),
518 .c_define => return sema.zirCDefine( block, extended),
519 .wasm_memory_size => return sema.zirWasmMemorySize( block, extended),
520 .wasm_memory_grow => return sema.zirWasmMemoryGrow( block, extended),
521 // zig fmt: on
522 }
523}
524
502/// TODO when we rework TZIR memory layout, this function will no longer have a possible error.525/// TODO when we rework TZIR memory layout, this function will no longer have a possible error.
503pub fn resolveInst(sema: *Sema, zir_ref: Zir.Inst.Ref) error{OutOfMemory}!*ir.Inst {526pub fn resolveInst(sema: *Sema, zir_ref: Zir.Inst.Ref) error{OutOfMemory}!*ir.Inst {
504 var i: usize = @enumToInt(zir_ref);527 var i: usize = @enumToInt(zir_ref);
...@@ -990,11 +1013,15 @@ fn zirErrorSetDecl(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) Inner...@@ -990,11 +1013,15 @@ fn zirErrorSetDecl(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) Inner
990 return sema.mod.fail(&block.base, sema.src, "TODO implement zirErrorSetDecl", .{});1013 return sema.mod.fail(&block.base, sema.src, "TODO implement zirErrorSetDecl", .{});
991}1014}
9921015
993fn zirRetPtr(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) InnerError!*Inst {1016fn zirRetPtr(
1017 sema: *Sema,
1018 block: *Scope.Block,
1019 extended: Zir.Inst.Extended.InstData,
1020) InnerError!*Inst {
994 const tracy = trace(@src());1021 const tracy = trace(@src());
995 defer tracy.end();1022 defer tracy.end();
9961023
997 const src: LazySrcLoc = .unneeded;1024 const src: LazySrcLoc = .{ .node_offset = @bitCast(i32, extended.operand) };
998 try sema.requireFunctionBlock(block, src);1025 try sema.requireFunctionBlock(block, src);
999 const fn_ty = sema.func.?.owner_decl.typed_value.most_recent.typed_value.ty;1026 const fn_ty = sema.func.?.owner_decl.typed_value.most_recent.typed_value.ty;
1000 const ret_type = fn_ty.fnReturnType();1027 const ret_type = fn_ty.fnReturnType();
...@@ -1011,11 +1038,15 @@ fn zirRef(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) InnerError!*In...@@ -1011,11 +1038,15 @@ fn zirRef(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) InnerError!*In
1011 return sema.analyzeRef(block, inst_data.src(), operand);1038 return sema.analyzeRef(block, inst_data.src(), operand);
1012}1039}
10131040
1014fn zirRetType(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) InnerError!*Inst {1041fn zirRetType(
1042 sema: *Sema,
1043 block: *Scope.Block,
1044 extended: Zir.Inst.Extended.InstData,
1045) InnerError!*Inst {
1015 const tracy = trace(@src());1046 const tracy = trace(@src());
1016 defer tracy.end();1047 defer tracy.end();
10171048
1018 const src: LazySrcLoc = .unneeded;1049 const src: LazySrcLoc = .{ .node_offset = @bitCast(i32, extended.operand) };
1019 try sema.requireFunctionBlock(block, src);1050 try sema.requireFunctionBlock(block, src);
1020 const fn_ty = sema.func.?.owner_decl.typed_value.most_recent.typed_value.ty;1051 const fn_ty = sema.func.?.owner_decl.typed_value.most_recent.typed_value.ty;
1021 const ret_type = fn_ty.fnReturnType();1052 const ret_type = fn_ty.fnReturnType();
...@@ -1247,6 +1278,12 @@ fn zirValidateStructInitPtr(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Ind...@@ -1247,6 +1278,12 @@ fn zirValidateStructInitPtr(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Ind
1247 }1278 }
1248}1279}
12491280
1281fn zirValidateArrayInitPtr(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) InnerError!void {
1282 const inst_data = sema.code.instructions.items(.data)[inst].pl_node;
1283 const src = inst_data.src();
1284 return sema.mod.fail(&block.base, src, "TODO implement Sema.zirValidateArrayInitPtr", .{});
1285}
1286
1250fn failWithBadFieldAccess(1287fn failWithBadFieldAccess(
1251 sema: *Sema,1288 sema: *Sema,
1252 block: *Scope.Block,1289 block: *Scope.Block,
...@@ -2064,6 +2101,14 @@ fn zirOptionalTypeFromPtrElem(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.I...@@ -2064,6 +2101,14 @@ fn zirOptionalTypeFromPtrElem(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.I
2064 return sema.mod.constType(sema.arena, inst_data.src(), opt_ty);2101 return sema.mod.constType(sema.arena, inst_data.src(), opt_ty);
2065}2102}
20662103
2104fn zirElemType(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) InnerError!*Inst {
2105 const inst_data = sema.code.instructions.items(.data)[inst].un_node;
2106 const src = inst_data.src();
2107 const array_type = try sema.resolveType(block, src, inst_data.operand);
2108 const elem_type = array_type.elemType();
2109 return sema.mod.constType(sema.arena, src, elem_type);
2110}
2111
2067fn zirArrayType(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) InnerError!*Inst {2112fn zirArrayType(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) InnerError!*Inst {
2068 const tracy = trace(@src());2113 const tracy = trace(@src());
2069 defer tracy.end();2114 defer tracy.end();
...@@ -4512,21 +4557,30 @@ fn zirBitSizeOf(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) InnerErr...@@ -4512,21 +4557,30 @@ fn zirBitSizeOf(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) InnerErr
4512 return sema.mod.constIntUnsigned(sema.arena, src, Type.initTag(.comptime_int), bit_size);4557 return sema.mod.constIntUnsigned(sema.arena, src, Type.initTag(.comptime_int), bit_size);
4513}4558}
45144559
4515fn zirThis(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) InnerError!*Inst {4560fn zirThis(
4516 const src_node = sema.code.instructions.items(.data)[inst].node;4561 sema: *Sema,
4517 const src: LazySrcLoc = .{ .node_offset = src_node };4562 block: *Scope.Block,
4563 extended: Zir.Inst.Extended.InstData,
4564) InnerError!*Inst {
4565 const src: LazySrcLoc = .{ .node_offset = @bitCast(i32, extended.operand) };
4518 return sema.mod.fail(&block.base, src, "TODO: implement Sema.zirThis", .{});4566 return sema.mod.fail(&block.base, src, "TODO: implement Sema.zirThis", .{});
4519}4567}
45204568
4521fn zirRetAddr(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) InnerError!*Inst {4569fn zirRetAddr(
4522 const src_node = sema.code.instructions.items(.data)[inst].node;4570 sema: *Sema,
4523 const src: LazySrcLoc = .{ .node_offset = src_node };4571 block: *Scope.Block,
4572 extended: Zir.Inst.Extended.InstData,
4573) InnerError!*Inst {
4574 const src: LazySrcLoc = .{ .node_offset = @bitCast(i32, extended.operand) };
4524 return sema.mod.fail(&block.base, src, "TODO: implement Sema.zirRetAddr", .{});4575 return sema.mod.fail(&block.base, src, "TODO: implement Sema.zirRetAddr", .{});
4525}4576}
45264577
4527fn zirBuiltinSrc(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) InnerError!*Inst {4578fn zirBuiltinSrc(
4528 const src_node = sema.code.instructions.items(.data)[inst].node;4579 sema: *Sema,
4529 const src: LazySrcLoc = .{ .node_offset = src_node };4580 block: *Scope.Block,
4581 extended: Zir.Inst.Extended.InstData,
4582) InnerError!*Inst {
4583 const src: LazySrcLoc = .{ .node_offset = @bitCast(i32, extended.operand) };
4530 return sema.mod.fail(&block.base, src, "TODO: implement Sema.zirBuiltinSrc", .{});4584 return sema.mod.fail(&block.base, src, "TODO: implement Sema.zirBuiltinSrc", .{});
4531}4585}
45324586
...@@ -4983,6 +5037,18 @@ fn zirStructInitAnon(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) Inn...@@ -4983,6 +5037,18 @@ fn zirStructInitAnon(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) Inn
4983 return sema.mod.fail(&block.base, src, "TODO: Sema.zirStructInitAnon", .{});5037 return sema.mod.fail(&block.base, src, "TODO: Sema.zirStructInitAnon", .{});
4984}5038}
49855039
5040fn zirArrayInit(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index, is_ref: bool) InnerError!*Inst {
5041 const inst_data = sema.code.instructions.items(.data)[inst].pl_node;
5042 const src = inst_data.src();
5043 return sema.mod.fail(&block.base, src, "TODO: Sema.zirArrayInit", .{});
5044}
5045
5046fn zirArrayInitAnon(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index, is_ref: bool) InnerError!*Inst {
5047 const inst_data = sema.code.instructions.items(.data)[inst].pl_node;
5048 const src = inst_data.src();
5049 return sema.mod.fail(&block.base, src, "TODO: Sema.zirArrayInitAnon", .{});
5050}
5051
4986fn zirFieldTypeRef(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) InnerError!*Inst {5052fn zirFieldTypeRef(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) InnerError!*Inst {
4987 const inst_data = sema.code.instructions.items(.data)[inst].pl_node;5053 const inst_data = sema.code.instructions.items(.data)[inst].pl_node;
4988 const src = inst_data.src();5054 const src = inst_data.src();
...@@ -4995,21 +5061,30 @@ fn zirFieldType(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) InnerErr...@@ -4995,21 +5061,30 @@ fn zirFieldType(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) InnerErr
4995 return sema.mod.fail(&block.base, src, "TODO: Sema.zirFieldType", .{});5061 return sema.mod.fail(&block.base, src, "TODO: Sema.zirFieldType", .{});
4996}5062}
49975063
4998fn zirErrorReturnTrace(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) InnerError!*Inst {5064fn zirErrorReturnTrace(
4999 const inst_data = sema.code.instructions.items(.data)[inst].un_node;5065 sema: *Sema,
5000 const src = inst_data.src();5066 block: *Scope.Block,
5067 extended: Zir.Inst.Extended.InstData,
5068) InnerError!*Inst {
5069 const src: LazySrcLoc = .{ .node_offset = @bitCast(i32, extended.operand) };
5001 return sema.mod.fail(&block.base, src, "TODO: Sema.zirErrorReturnTrace", .{});5070 return sema.mod.fail(&block.base, src, "TODO: Sema.zirErrorReturnTrace", .{});
5002}5071}
50035072
5004fn zirFrame(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) InnerError!*Inst {5073fn zirFrame(
5005 const inst_data = sema.code.instructions.items(.data)[inst].un_node;5074 sema: *Sema,
5006 const src = inst_data.src();5075 block: *Scope.Block,
5076 extended: Zir.Inst.Extended.InstData,
5077) InnerError!*Inst {
5078 const src: LazySrcLoc = .{ .node_offset = @bitCast(i32, extended.operand) };
5007 return sema.mod.fail(&block.base, src, "TODO: Sema.zirFrame", .{});5079 return sema.mod.fail(&block.base, src, "TODO: Sema.zirFrame", .{});
5008}5080}
50095081
5010fn zirFrameAddress(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) InnerError!*Inst {5082fn zirFrameAddress(
5011 const inst_data = sema.code.instructions.items(.data)[inst].un_node;5083 sema: *Sema,
5012 const src = inst_data.src();5084 block: *Scope.Block,
5085 extended: Zir.Inst.Extended.InstData,
5086) InnerError!*Inst {
5087 const src: LazySrcLoc = .{ .node_offset = @bitCast(i32, extended.operand) };
5013 return sema.mod.fail(&block.base, src, "TODO: Sema.zirFrameAddress", .{});5088 return sema.mod.fail(&block.base, src, "TODO: Sema.zirFrameAddress", .{});
5014}5089}
50155090
...@@ -5295,24 +5370,9 @@ fn zirBuiltinAsyncCall(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) I...@@ -5295,24 +5370,9 @@ fn zirBuiltinAsyncCall(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) I
5295 return sema.mod.fail(&block.base, src, "TODO: Sema.zirBuiltinAsyncCall", .{});5370 return sema.mod.fail(&block.base, src, "TODO: Sema.zirBuiltinAsyncCall", .{});
5296}5371}
52975372
5298fn zirExtended(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) InnerError!*Inst {
5299 const extended = sema.code.instructions.items(.data)[inst].extended;
5300 switch (extended.opcode) {
5301 // zig fmt: off
5302 .func => return sema.zirFuncExtended( block, inst, extended),
5303 .c_undef => return sema.zirCUndef( block, inst, extended),
5304 .c_include => return sema.zirCInclude( block, inst, extended),
5305 .c_define => return sema.zirCDefine( block, inst, extended),
5306 .wasm_memory_size => return sema.zirWasmMemorySize(block, inst, extended),
5307 .wasm_memory_grow => return sema.zirWasmMemoryGrow(block, inst, extended),
5308 // zig fmt: on
5309 }
5310}
5311
5312fn zirFuncExtended(5373fn zirFuncExtended(
5313 sema: *Sema,5374 sema: *Sema,
5314 block: *Scope.Block,5375 block: *Scope.Block,
5315 inst: Zir.Inst.Index,
5316 extended: Zir.Inst.Extended.InstData,5376 extended: Zir.Inst.Extended.InstData,
5317) InnerError!*Inst {5377) InnerError!*Inst {
5318 const tracy = trace(@src());5378 const tracy = trace(@src());
...@@ -5364,7 +5424,6 @@ fn zirFuncExtended(...@@ -5364,7 +5424,6 @@ fn zirFuncExtended(
5364fn zirCUndef(5424fn zirCUndef(
5365 sema: *Sema,5425 sema: *Sema,
5366 block: *Scope.Block,5426 block: *Scope.Block,
5367 inst: Zir.Inst.Index,
5368 extended: Zir.Inst.Extended.InstData,5427 extended: Zir.Inst.Extended.InstData,
5369) InnerError!*Inst {5428) InnerError!*Inst {
5370 const extra = sema.code.extraData(Zir.Inst.UnNode, extended.operand).data;5429 const extra = sema.code.extraData(Zir.Inst.UnNode, extended.operand).data;
...@@ -5375,7 +5434,6 @@ fn zirCUndef(...@@ -5375,7 +5434,6 @@ fn zirCUndef(
5375fn zirCInclude(5434fn zirCInclude(
5376 sema: *Sema,5435 sema: *Sema,
5377 block: *Scope.Block,5436 block: *Scope.Block,
5378 inst: Zir.Inst.Index,
5379 extended: Zir.Inst.Extended.InstData,5437 extended: Zir.Inst.Extended.InstData,
5380) InnerError!*Inst {5438) InnerError!*Inst {
5381 const extra = sema.code.extraData(Zir.Inst.UnNode, extended.operand).data;5439 const extra = sema.code.extraData(Zir.Inst.UnNode, extended.operand).data;
...@@ -5386,7 +5444,6 @@ fn zirCInclude(...@@ -5386,7 +5444,6 @@ fn zirCInclude(
5386fn zirCDefine(5444fn zirCDefine(
5387 sema: *Sema,5445 sema: *Sema,
5388 block: *Scope.Block,5446 block: *Scope.Block,
5389 inst: Zir.Inst.Index,
5390 extended: Zir.Inst.Extended.InstData,5447 extended: Zir.Inst.Extended.InstData,
5391) InnerError!*Inst {5448) InnerError!*Inst {
5392 const extra = sema.code.extraData(Zir.Inst.BinNode, extended.operand).data;5449 const extra = sema.code.extraData(Zir.Inst.BinNode, extended.operand).data;
...@@ -5397,7 +5454,6 @@ fn zirCDefine(...@@ -5397,7 +5454,6 @@ fn zirCDefine(
5397fn zirWasmMemorySize(5454fn zirWasmMemorySize(
5398 sema: *Sema,5455 sema: *Sema,
5399 block: *Scope.Block,5456 block: *Scope.Block,
5400 inst: Zir.Inst.Index,
5401 extended: Zir.Inst.Extended.InstData,5457 extended: Zir.Inst.Extended.InstData,
5402) InnerError!*Inst {5458) InnerError!*Inst {
5403 const extra = sema.code.extraData(Zir.Inst.UnNode, extended.operand).data;5459 const extra = sema.code.extraData(Zir.Inst.UnNode, extended.operand).data;
...@@ -5408,7 +5464,6 @@ fn zirWasmMemorySize(...@@ -5408,7 +5464,6 @@ fn zirWasmMemorySize(
5408fn zirWasmMemoryGrow(5464fn zirWasmMemoryGrow(
5409 sema: *Sema,5465 sema: *Sema,
5410 block: *Scope.Block,5466 block: *Scope.Block,
5411 inst: Zir.Inst.Index,
5412 extended: Zir.Inst.Extended.InstData,5467 extended: Zir.Inst.Extended.InstData,
5413) InnerError!*Inst {5468) InnerError!*Inst {
5414 const extra = sema.code.extraData(Zir.Inst.BinNode, extended.operand).data;5469 const extra = sema.code.extraData(Zir.Inst.BinNode, extended.operand).data;
src/Zir.zig+61-41
...@@ -169,6 +169,9 @@ pub const Inst = struct {...@@ -169,6 +169,9 @@ pub const Inst = struct {
169 /// `[N:S]T` syntax. No source location provided.169 /// `[N:S]T` syntax. No source location provided.
170 /// Uses the `array_type_sentinel` field.170 /// Uses the `array_type_sentinel` field.
171 array_type_sentinel,171 array_type_sentinel,
172 /// Given an array type, returns the element type.
173 /// Uses the `un_node` union field.
174 elem_type,
172 /// Given a pointer to an indexable object, returns the len property. This is175 /// Given a pointer to an indexable object, returns the len property. This is
173 /// used by for loops. This instruction also emits a for-loop specific compile176 /// used by for loops. This instruction also emits a for-loop specific compile
174 /// error if the indexable object is not indexable.177 /// error if the indexable object is not indexable.
...@@ -457,12 +460,6 @@ pub const Inst = struct {...@@ -457,12 +460,6 @@ pub const Inst = struct {
457 /// instruction.460 /// instruction.
458 /// Uses the `un_tok` union field.461 /// Uses the `un_tok` union field.
459 ref,462 ref,
460 /// Obtains a pointer to the return value.
461 /// Uses the `node` union field.
462 ret_ptr,
463 /// Obtains the return type of the in-scope function.
464 /// Uses the `node` union field.
465 ret_type,
466 /// Sends control flow back to the function's callee.463 /// Sends control flow back to the function's callee.
467 /// Includes an operand as the return value.464 /// Includes an operand as the return value.
468 /// Includes an AST node source location.465 /// Includes an AST node source location.
...@@ -674,6 +671,13 @@ pub const Inst = struct {...@@ -674,6 +671,13 @@ pub const Inst = struct {
674 /// because it must use one of them to find out the struct type.671 /// because it must use one of them to find out the struct type.
675 /// Uses the `pl_node` field. Payload is `Block`.672 /// Uses the `pl_node` field. Payload is `Block`.
676 validate_struct_init_ptr,673 validate_struct_init_ptr,
674 /// Given a set of `elem_ptr_node` instructions, assumes they are all part of an
675 /// array initialization expression, and emits a compile error if the number of
676 /// elements does not match the array type.
677 /// This instruction asserts that there is at least one elem_ptr_node instruction,
678 /// because it must use one of them to find out the array type.
679 /// Uses the `pl_node` field. Payload is `Block`.
680 validate_array_init_ptr,
677 /// A struct literal with a specified type, with no fields.681 /// A struct literal with a specified type, with no fields.
678 /// Uses the `un_node` field.682 /// Uses the `un_node` field.
679 struct_init_empty,683 struct_init_empty,
...@@ -690,6 +694,18 @@ pub const Inst = struct {...@@ -690,6 +694,18 @@ pub const Inst = struct {
690 /// Struct initialization without a type.694 /// Struct initialization without a type.
691 /// Uses the `pl_node` field. Payload is `StructInitAnon`.695 /// Uses the `pl_node` field. Payload is `StructInitAnon`.
692 struct_init_anon,696 struct_init_anon,
697 /// Array initialization syntax.
698 /// Uses the `pl_node` field. Payload is `MultiOp`.
699 array_init,
700 /// Anonymous array initialization syntax.
701 /// Uses the `pl_node` field. Payload is `MultiOp`.
702 array_init_anon,
703 /// Array initialization syntax, make the result a pointer.
704 /// Uses the `pl_node` field. Payload is `MultiOp`.
705 array_init_ref,
706 /// Anonymous array initialization syntax, make the result a pointer.
707 /// Uses the `pl_node` field. Payload is `MultiOp`.
708 array_init_anon_ref,
693 /// Given a pointer to a union and a comptime known field name, activates that field709 /// Given a pointer to a union and a comptime known field name, activates that field
694 /// and returns a pointer to it.710 /// and returns a pointer to it.
695 /// Uses the `pl_node` field. Payload is `UnionInitPtr`.711 /// Uses the `pl_node` field. Payload is `UnionInitPtr`.
...@@ -700,14 +716,8 @@ pub const Inst = struct {...@@ -700,14 +716,8 @@ pub const Inst = struct {
700 size_of,716 size_of,
701 /// Implements the `@bitSizeOf` builtin. Uses `un_node`.717 /// Implements the `@bitSizeOf` builtin. Uses `un_node`.
702 bit_size_of,718 bit_size_of,
703 /// Implements the `@This` builtin. Uses `node`.719 /// Implements the `@fence` builtin. Uses `node`.
704 this,
705 /// Implements the `@fence` builtin. Uses `un_node`.
706 fence,720 fence,
707 /// Implements the `@returnAddress` builtin. Uses `un_node`.
708 ret_addr,
709 /// Implements the `@src` builtin. Uses `un_node`.
710 builtin_src,
711 /// Implements the `@addWithOverflow` builtin. Uses `pl_node` with `OverflowArithmetic`.721 /// Implements the `@addWithOverflow` builtin. Uses `pl_node` with `OverflowArithmetic`.
712 add_with_overflow,722 add_with_overflow,
713 /// Implements the `@subWithOverflow` builtin. Uses `pl_node` with `OverflowArithmetic`.723 /// Implements the `@subWithOverflow` builtin. Uses `pl_node` with `OverflowArithmetic`.
...@@ -717,16 +727,6 @@ pub const Inst = struct {...@@ -717,16 +727,6 @@ pub const Inst = struct {
717 /// Implements the `@shlWithOverflow` builtin. Uses `pl_node` with `OverflowArithmetic`.727 /// Implements the `@shlWithOverflow` builtin. Uses `pl_node` with `OverflowArithmetic`.
718 shl_with_overflow,728 shl_with_overflow,
719729
720 /// Implements the `@errorReturnTrace` builtin.
721 /// Uses the `un_node` field.
722 error_return_trace,
723 /// Implements the `@frame` builtin.
724 /// Uses the `un_node` field.
725 frame,
726 /// Implements the `@frameAddress` builtin.
727 /// Uses the `un_node` field.
728 frame_address,
729
730 /// Implement builtin `@ptrToInt`. Uses `un_node`.730 /// Implement builtin `@ptrToInt`. Uses `un_node`.
731 ptr_to_int,731 ptr_to_int,
732 /// Implement builtin `@errToInt`. Uses `un_node`.732 /// Implement builtin `@errToInt`. Uses `un_node`.
...@@ -951,6 +951,7 @@ pub const Inst = struct {...@@ -951,6 +951,7 @@ pub const Inst = struct {
951 .array_mul,951 .array_mul,
952 .array_type,952 .array_type,
953 .array_type_sentinel,953 .array_type_sentinel,
954 .elem_type,
954 .indexable_ptr_len,955 .indexable_ptr_len,
955 .as,956 .as,
956 .as_node,957 .as_node,
...@@ -970,6 +971,7 @@ pub const Inst = struct {...@@ -970,6 +971,7 @@ pub const Inst = struct {
970 .bool_and,971 .bool_and,
971 .bool_or,972 .bool_or,
972 .breakpoint,973 .breakpoint,
974 .fence,
973 .call,975 .call,
974 .call_chkused,976 .call_chkused,
975 .call_compile_time,977 .call_compile_time,
...@@ -1026,8 +1028,6 @@ pub const Inst = struct {...@@ -1026,8 +1028,6 @@ pub const Inst = struct {
1026 .param_type,1028 .param_type,
1027 .ptrtoint,1029 .ptrtoint,
1028 .ref,1030 .ref,
1029 .ret_ptr,
1030 .ret_type,
1031 .shl,1031 .shl,
1032 .shr,1032 .shr,
1033 .store,1033 .store,
...@@ -1094,9 +1094,14 @@ pub const Inst = struct {...@@ -1094,9 +1094,14 @@ pub const Inst = struct {
1094 .switch_block_ref_under,1094 .switch_block_ref_under,
1095 .switch_block_ref_under_multi,1095 .switch_block_ref_under_multi,
1096 .validate_struct_init_ptr,1096 .validate_struct_init_ptr,
1097 .validate_array_init_ptr,
1097 .struct_init_empty,1098 .struct_init_empty,
1098 .struct_init,1099 .struct_init,
1099 .struct_init_anon,1100 .struct_init_anon,
1101 .array_init,
1102 .array_init_anon,
1103 .array_init_ref,
1104 .array_init_anon_ref,
1100 .union_init_ptr,1105 .union_init_ptr,
1101 .field_type,1106 .field_type,
1102 .field_type_ref,1107 .field_type_ref,
...@@ -1105,17 +1110,10 @@ pub const Inst = struct {...@@ -1105,17 +1110,10 @@ pub const Inst = struct {
1105 .type_info,1110 .type_info,
1106 .size_of,1111 .size_of,
1107 .bit_size_of,1112 .bit_size_of,
1108 .this,
1109 .fence,
1110 .ret_addr,
1111 .builtin_src,
1112 .add_with_overflow,1113 .add_with_overflow,
1113 .sub_with_overflow,1114 .sub_with_overflow,
1114 .mul_with_overflow,1115 .mul_with_overflow,
1115 .shl_with_overflow,1116 .shl_with_overflow,
1116 .error_return_trace,
1117 .frame,
1118 .frame_address,
1119 .ptr_to_int,1117 .ptr_to_int,
1120 .align_of,1118 .align_of,
1121 .bool_to_int,1119 .bool_to_int,
...@@ -1211,6 +1209,30 @@ pub const Inst = struct {...@@ -1211,6 +1209,30 @@ pub const Inst = struct {
1211 /// `operand` is payload index to `ExtendedFunc`.1209 /// `operand` is payload index to `ExtendedFunc`.
1212 /// `small` is `ExtendedFunc.Small`.1210 /// `small` is `ExtendedFunc.Small`.
1213 func,1211 func,
1212 /// Obtains a pointer to the return value.
1213 /// `operand` is `src_node: i32`.
1214 ret_ptr,
1215 /// Obtains the return type of the in-scope function.
1216 /// `operand` is `src_node: i32`.
1217 ret_type,
1218 /// Implements the `@This` builtin.
1219 /// `operand` is `src_node: i32`.
1220 this,
1221 /// Implements the `@returnAddress` builtin.
1222 /// `operand` is `src_node: i32`.
1223 ret_addr,
1224 /// Implements the `@src` builtin.
1225 /// `operand` is `src_node: i32`.
1226 builtin_src,
1227 /// Implements the `@errorReturnTrace` builtin.
1228 /// `operand` is `src_node: i32`.
1229 error_return_trace,
1230 /// Implements the `@frame` builtin.
1231 /// `operand` is `src_node: i32`.
1232 frame,
1233 /// Implements the `@frameAddress` builtin.
1234 /// `operand` is `src_node: i32`.
1235 frame_address,
1214 /// `operand` is payload index to `UnNode`.1236 /// `operand` is payload index to `UnNode`.
1215 c_undef,1237 c_undef,
1216 /// `operand` is payload index to `UnNode`.1238 /// `operand` is payload index to `UnNode`.
...@@ -2281,6 +2303,7 @@ const Writer = struct {...@@ -2281,6 +2303,7 @@ const Writer = struct {
2281 .pop_count,2303 .pop_count,
2282 .byte_swap,2304 .byte_swap,
2283 .bit_reverse,2305 .bit_reverse,
2306 .elem_type,
2284 => try self.writeUnNode(stream, inst),2307 => try self.writeUnNode(stream, inst),
22852308
2286 .ref,2309 .ref,
...@@ -2317,6 +2340,10 @@ const Writer = struct {...@@ -2317,6 +2340,10 @@ const Writer = struct {
2317 .union_decl,2340 .union_decl,
2318 .struct_init,2341 .struct_init,
2319 .struct_init_anon,2342 .struct_init_anon,
2343 .array_init,
2344 .array_init_anon,
2345 .array_init_ref,
2346 .array_init_anon_ref,
2320 .union_init_ptr,2347 .union_init_ptr,
2321 .field_type,2348 .field_type,
2322 .field_type_ref,2349 .field_type_ref,
...@@ -2409,6 +2436,7 @@ const Writer = struct {...@@ -2409,6 +2436,7 @@ const Writer = struct {
2409 .block_inline_var,2436 .block_inline_var,
2410 .loop,2437 .loop,
2411 .validate_struct_init_ptr,2438 .validate_struct_init_ptr,
2439 .validate_array_init_ptr,
2412 .c_import,2440 .c_import,
2413 => try self.writePlNodeBlock(stream, inst),2441 => try self.writePlNodeBlock(stream, inst),
24142442
...@@ -2450,21 +2478,13 @@ const Writer = struct {...@@ -2450,21 +2478,13 @@ const Writer = struct {
2450 .as_node => try self.writeAs(stream, inst),2478 .as_node => try self.writeAs(stream, inst),
24512479
2452 .breakpoint,2480 .breakpoint,
2481 .fence,
2453 .opaque_decl,2482 .opaque_decl,
2454 .dbg_stmt_node,2483 .dbg_stmt_node,
2455 .ret_ptr,
2456 .ret_type,
2457 .repeat,2484 .repeat,
2458 .repeat_inline,2485 .repeat_inline,
2459 .alloc_inferred,2486 .alloc_inferred,
2460 .alloc_inferred_mut,2487 .alloc_inferred_mut,
2461 .this,
2462 .fence,
2463 .ret_addr,
2464 .builtin_src,
2465 .error_return_trace,
2466 .frame,
2467 .frame_address,
2468 => try self.writeNode(stream, inst),2488 => try self.writeNode(stream, inst),
24692489
2470 .error_value,2490 .error_value,