authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2021-04-18 22:38:41-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2021-04-18 22:38:41-07:00
logae495de54d6aed6efbfc727f66154216b15225af
tree875de98aa0bcca806fb0ccc901203cb53ec0656a
parent5a3045b5de03fade87ccbb4191344861374ea0a4

AstGen: implement all the builtin functions


7 files changed, 2185 insertions(+), 430 deletions(-)

BRANCH_TODO-2
......@@ -737,5 +737,3 @@ fn errorSetDecl(
737737 try mod.analyzeExport(&decl_scope.base, export_src, name, decl);
738738 }
739739 }
740
741
src/AstGen.zig+670-227
......@@ -51,6 +51,7 @@ pub fn addExtraAssumeCapacity(astgen: *AstGen, extra: anytype) u32 {
5151 astgen.extra.appendAssumeCapacity(switch (field.field_type) {
5252 u32 => @field(extra, field.name),
5353 Zir.Inst.Ref => @enumToInt(@field(extra, field.name)),
54 i32 => @bitCast(u32, @field(extra, field.name)),
5455 else => @compileError("bad field type"),
5556 });
5657 }
......@@ -237,6 +238,9 @@ pub const ResultLoc = union(enum) {
237238 }
238239};
239240
241pub const align_rl: ResultLoc = .{ .ty = .u16_type };
242pub const bool_rl: ResultLoc = .{ .ty = .bool_type };
243
240244pub fn typeExpr(gz: *GenZir, scope: *Scope, type_node: ast.Node.Index) InnerError!Zir.Inst.Ref {
241245 return expr(gz, scope, .{ .ty = .type_type }, type_node);
242246}
......@@ -469,20 +473,22 @@ pub fn expr(gz: *GenZir, scope: *Scope, rl: ResultLoc, node: ast.Node.Index) Inn
469473 try assign(gz, scope, node);
470474 return rvalue(gz, scope, rl, .void_value, node);
471475 },
472 .assign_bit_and => {
473 try assignOp(gz, scope, node, .bit_and);
476
477 .assign_bit_shift_left => {
478 try assignShift(gz, scope, node, .shl);
474479 return rvalue(gz, scope, rl, .void_value, node);
475480 },
476 .assign_bit_or => {
477 try assignOp(gz, scope, node, .bit_or);
481 .assign_bit_shift_right => {
482 try assignShift(gz, scope, node, .shr);
478483 return rvalue(gz, scope, rl, .void_value, node);
479484 },
480 .assign_bit_shift_left => {
481 try assignOp(gz, scope, node, .shl);
485
486 .assign_bit_and => {
487 try assignOp(gz, scope, node, .bit_and);
482488 return rvalue(gz, scope, rl, .void_value, node);
483489 },
484 .assign_bit_shift_right => {
485 try assignOp(gz, scope, node, .shr);
490 .assign_bit_or => {
491 try assignOp(gz, scope, node, .bit_or);
486492 return rvalue(gz, scope, rl, .void_value, node);
487493 },
488494 .assign_bit_xor => {
......@@ -522,51 +528,54 @@ pub fn expr(gz: *GenZir, scope: *Scope, rl: ResultLoc, node: ast.Node.Index) Inn
522528 return rvalue(gz, scope, rl, .void_value, node);
523529 },
524530
525 .add => return simpleBinOp(gz, scope, rl, node, .add),
531 // zig fmt: off
532 .bit_shift_left => return shiftOp(gz, scope, rl, node, node_datas[node].lhs, node_datas[node].rhs, .shl),
533 .bit_shift_right => return shiftOp(gz, scope, rl, node, node_datas[node].lhs, node_datas[node].rhs, .shr),
534
535 .add => return simpleBinOp(gz, scope, rl, node, .add),
526536 .add_wrap => return simpleBinOp(gz, scope, rl, node, .addwrap),
527 .sub => return simpleBinOp(gz, scope, rl, node, .sub),
537 .sub => return simpleBinOp(gz, scope, rl, node, .sub),
528538 .sub_wrap => return simpleBinOp(gz, scope, rl, node, .subwrap),
529 .mul => return simpleBinOp(gz, scope, rl, node, .mul),
539 .mul => return simpleBinOp(gz, scope, rl, node, .mul),
530540 .mul_wrap => return simpleBinOp(gz, scope, rl, node, .mulwrap),
531 .div => return simpleBinOp(gz, scope, rl, node, .div),
532 .mod => return simpleBinOp(gz, scope, rl, node, .mod_rem),
533 .bit_and => return simpleBinOp(gz, scope, rl, node, .bit_and),
534 .bit_or => return simpleBinOp(gz, scope, rl, node, .bit_or),
535 .bit_shift_left => return simpleBinOp(gz, scope, rl, node, .shl),
536 .bit_shift_right => return simpleBinOp(gz, scope, rl, node, .shr),
537 .bit_xor => return simpleBinOp(gz, scope, rl, node, .xor),
538
539 .bang_equal => return simpleBinOp(gz, scope, rl, node, .cmp_neq),
540 .equal_equal => return simpleBinOp(gz, scope, rl, node, .cmp_eq),
541 .greater_than => return simpleBinOp(gz, scope, rl, node, .cmp_gt),
541 .div => return simpleBinOp(gz, scope, rl, node, .div),
542 .mod => return simpleBinOp(gz, scope, rl, node, .mod_rem),
543 .bit_and => return simpleBinOp(gz, scope, rl, node, .bit_and),
544 .bit_or => return simpleBinOp(gz, scope, rl, node, .bit_or),
545 .bit_xor => return simpleBinOp(gz, scope, rl, node, .xor),
546
547 .bang_equal => return simpleBinOp(gz, scope, rl, node, .cmp_neq),
548 .equal_equal => return simpleBinOp(gz, scope, rl, node, .cmp_eq),
549 .greater_than => return simpleBinOp(gz, scope, rl, node, .cmp_gt),
542550 .greater_or_equal => return simpleBinOp(gz, scope, rl, node, .cmp_gte),
543 .less_than => return simpleBinOp(gz, scope, rl, node, .cmp_lt),
544 .less_or_equal => return simpleBinOp(gz, scope, rl, node, .cmp_lte),
551 .less_than => return simpleBinOp(gz, scope, rl, node, .cmp_lt),
552 .less_or_equal => return simpleBinOp(gz, scope, rl, node, .cmp_lte),
545553
546 .array_cat => return simpleBinOp(gz, scope, rl, node, .array_cat),
547 .array_mult => return simpleBinOp(gz, scope, rl, node, .array_mul),
554 .array_cat => return simpleBinOp(gz, scope, rl, node, .array_cat),
555 .array_mult => return simpleBinOp(gz, scope, rl, node, .array_mul),
548556
549 .error_union => return simpleBinOp(gz, scope, rl, node, .error_union_type),
557 .error_union => return simpleBinOp(gz, scope, rl, node, .error_union_type),
550558 .merge_error_sets => return simpleBinOp(gz, scope, rl, node, .merge_error_sets),
551559
552560 .bool_and => return boolBinOp(gz, scope, rl, node, .bool_br_and),
553 .bool_or => return boolBinOp(gz, scope, rl, node, .bool_br_or),
561 .bool_or => return boolBinOp(gz, scope, rl, node, .bool_br_or),
554562
555563 .bool_not => return boolNot(gz, scope, rl, node),
556 .bit_not => return bitNot(gz, scope, rl, node),
564 .bit_not => return bitNot(gz, scope, rl, node),
557565
558 .negation => return negation(gz, scope, rl, node, .negate),
566 .negation => return negation(gz, scope, rl, node, .negate),
559567 .negation_wrap => return negation(gz, scope, rl, node, .negate_wrap),
560568
561569 .identifier => return identifier(gz, scope, rl, node),
562570
563571 .asm_simple => return asmExpr(gz, scope, rl, node, tree.asmSimple(node)),
564 .@"asm" => return asmExpr(gz, scope, rl, node, tree.asmFull(node)),
572 .@"asm" => return asmExpr(gz, scope, rl, node, tree.asmFull(node)),
565573
566 .string_literal => return stringLiteral(gz, scope, rl, node),
574 .string_literal => return stringLiteral(gz, scope, rl, node),
567575 .multiline_string_literal => return multilineStringLiteral(gz, scope, rl, node),
568576
569577 .integer_literal => return integerLiteral(gz, scope, rl, node),
578 // zig fmt: on
570579
571580 .builtin_call_two, .builtin_call_two_comma => {
572581 if (node_datas[node].lhs == 0) {
......@@ -1181,13 +1190,14 @@ fn labeledBlockExpr(
11811190 // All break operands are values that did not use the result location pointer.
11821191 if (strat.elide_store_to_block_ptr_instructions) {
11831192 for (block_scope.labeled_store_to_block_ptr_list.items) |inst| {
1184 zir_tags[inst] = .elided;
1185 zir_datas[inst] = undefined;
1193 // Mark as elided for removal below.
1194 assert(zir_tags[inst] == .store_to_block_ptr);
1195 zir_datas[inst].bin.lhs = .none;
11861196 }
1187 // TODO technically not needed since we changed the tag to elided but
1188 // would be better still to elide the ones that are in this list.
1197 try block_scope.setBlockBodyEliding(block_inst);
1198 } else {
1199 try block_scope.setBlockBody(block_inst);
11891200 }
1190 try block_scope.setBlockBody(block_inst);
11911201 const block_ref = gz.indexToRef(block_inst);
11921202 switch (rl) {
11931203 .ref => return block_ref,
......@@ -1222,20 +1232,24 @@ fn blockExprStmts(
12221232 .simple_var_decl => scope = try varDecl(gz, scope, statement, &block_arena.allocator, tree.simpleVarDecl(statement)),
12231233 .aligned_var_decl => scope = try varDecl(gz, scope, statement, &block_arena.allocator, tree.alignedVarDecl(statement)),
12241234
1235 // zig fmt: off
12251236 .assign => try assign(gz, scope, statement),
1226 .assign_bit_and => try assignOp(gz, scope, statement, .bit_and),
1227 .assign_bit_or => try assignOp(gz, scope, statement, .bit_or),
1228 .assign_bit_shift_left => try assignOp(gz, scope, statement, .shl),
1229 .assign_bit_shift_right => try assignOp(gz, scope, statement, .shr),
1230 .assign_bit_xor => try assignOp(gz, scope, statement, .xor),
1231 .assign_div => try assignOp(gz, scope, statement, .div),
1232 .assign_sub => try assignOp(gz, scope, statement, .sub),
1237
1238 .assign_bit_shift_left => try assignShift(gz, scope, statement, .shl),
1239 .assign_bit_shift_right => try assignShift(gz, scope, statement, .shr),
1240
1241 .assign_bit_and => try assignOp(gz, scope, statement, .bit_and),
1242 .assign_bit_or => try assignOp(gz, scope, statement, .bit_or),
1243 .assign_bit_xor => try assignOp(gz, scope, statement, .xor),
1244 .assign_div => try assignOp(gz, scope, statement, .div),
1245 .assign_sub => try assignOp(gz, scope, statement, .sub),
12331246 .assign_sub_wrap => try assignOp(gz, scope, statement, .subwrap),
1234 .assign_mod => try assignOp(gz, scope, statement, .mod_rem),
1235 .assign_add => try assignOp(gz, scope, statement, .add),
1247 .assign_mod => try assignOp(gz, scope, statement, .mod_rem),
1248 .assign_add => try assignOp(gz, scope, statement, .add),
12361249 .assign_add_wrap => try assignOp(gz, scope, statement, .addwrap),
1237 .assign_mul => try assignOp(gz, scope, statement, .mul),
1250 .assign_mul => try assignOp(gz, scope, statement, .mul),
12381251 .assign_mul_wrap => try assignOp(gz, scope, statement, .mulwrap),
1252 // zig fmt: on
12391253
12401254 else => {
12411255 // We need to emit an error if the result is not `noreturn` or `void`, but
......@@ -1313,7 +1327,6 @@ fn blockExprStmts(
13131327 .func_var_args,
13141328 .func_extra,
13151329 .func_extra_var_args,
1316 .has_decl,
13171330 .int,
13181331 .float,
13191332 .float128,
......@@ -1390,6 +1403,7 @@ fn blockExprStmts(
13901403 .switch_capture_else_ref,
13911404 .struct_init_empty,
13921405 .struct_init,
1406 .union_init_ptr,
13931407 .field_type,
13941408 .struct_decl,
13951409 .struct_decl_packed,
......@@ -1404,7 +1418,6 @@ fn blockExprStmts(
14041418 .size_of,
14051419 .bit_size_of,
14061420 .this,
1407 .fence,
14081421 .ret_addr,
14091422 .builtin_src,
14101423 .add_with_overflow,
......@@ -1412,10 +1425,80 @@ fn blockExprStmts(
14121425 .mul_with_overflow,
14131426 .shl_with_overflow,
14141427 .log2_int_type,
1428 .typeof_log2_int_type,
1429 .error_return_trace,
1430 .frame,
1431 .frame_address,
1432 .ptr_to_int,
1433 .align_of,
1434 .bool_to_int,
1435 .embed_file,
1436 .error_name,
1437 .sqrt,
1438 .sin,
1439 .cos,
1440 .exp,
1441 .exp2,
1442 .log,
1443 .log2,
1444 .log10,
1445 .fabs,
1446 .floor,
1447 .ceil,
1448 .trunc,
1449 .round,
1450 .tag_name,
1451 .reify,
1452 .type_name,
1453 .frame_type,
1454 .frame_size,
1455 .float_to_int,
1456 .int_to_float,
1457 .int_to_ptr,
1458 .float_cast,
1459 .int_cast,
1460 .err_set_cast,
1461 .ptr_cast,
1462 .truncate,
1463 .align_cast,
1464 .has_decl,
1465 .has_field,
1466 .clz,
1467 .ctz,
1468 .pop_count,
1469 .byte_swap,
1470 .bit_reverse,
1471 .div_exact,
1472 .div_floor,
1473 .div_trunc,
1474 .mod,
1475 .rem,
1476 .shl_exact,
1477 .shr_exact,
1478 .bit_offset_of,
1479 .byte_offset_of,
1480 .cmpxchg_strong,
1481 .cmpxchg_weak,
1482 .splat,
1483 .reduce,
1484 .shuffle,
1485 .atomic_load,
1486 .atomic_rmw,
1487 .atomic_store,
1488 .mul_add,
1489 .builtin_call,
1490 .field_ptr_type,
1491 .field_parent_ptr,
1492 .memcpy,
1493 .memset,
1494 .builtin_async_call,
1495 .c_import,
1496 .extended,
14151497 => break :b false,
14161498
14171499 // ZIR instructions that are always either `noreturn` or `void`.
14181500 .breakpoint,
1501 .fence,
14191502 .dbg_stmt_node,
14201503 .ensure_result_used,
14211504 .ensure_result_non_error,
......@@ -1432,7 +1515,6 @@ fn blockExprStmts(
14321515 .ret_tok,
14331516 .ret_coerce,
14341517 .@"unreachable",
1435 .elided,
14361518 .store,
14371519 .store_node,
14381520 .store_to_block_ptr,
......@@ -1441,6 +1523,11 @@ fn blockExprStmts(
14411523 .repeat,
14421524 .repeat_inline,
14431525 .validate_struct_init_ptr,
1526 .panic,
1527 .set_align_stack,
1528 .set_cold,
1529 .set_float_mode,
1530 .set_runtime_safety,
14441531 => break :b true,
14451532 }
14461533 } else switch (maybe_unused_result) {
......@@ -1702,12 +1789,34 @@ fn assignOp(
17021789 _ = try gz.addBin(.store, lhs_ptr, result);
17031790}
17041791
1792fn assignShift(
1793 gz: *GenZir,
1794 scope: *Scope,
1795 infix_node: ast.Node.Index,
1796 op_inst_tag: Zir.Inst.Tag,
1797) InnerError!void {
1798 const astgen = gz.astgen;
1799 const tree = &astgen.file.tree;
1800 const node_datas = tree.nodes.items(.data);
1801
1802 const lhs_ptr = try lvalExpr(gz, scope, node_datas[infix_node].lhs);
1803 const lhs = try gz.addUnNode(.load, lhs_ptr, infix_node);
1804 const rhs_type = try gz.addUnNode(.typeof_log2_int_type, lhs, infix_node);
1805 const rhs = try expr(gz, scope, .{ .ty = rhs_type }, node_datas[infix_node].rhs);
1806
1807 const result = try gz.addPlNode(op_inst_tag, infix_node, Zir.Inst.Bin{
1808 .lhs = lhs,
1809 .rhs = rhs,
1810 });
1811 _ = try gz.addBin(.store, lhs_ptr, result);
1812}
1813
17051814fn boolNot(gz: *GenZir, scope: *Scope, rl: ResultLoc, node: ast.Node.Index) InnerError!Zir.Inst.Ref {
17061815 const astgen = gz.astgen;
17071816 const tree = &astgen.file.tree;
17081817 const node_datas = tree.nodes.items(.data);
17091818
1710 const operand = try expr(gz, scope, .{ .ty = .bool_type }, node_datas[node].lhs);
1819 const operand = try expr(gz, scope, bool_rl, node_datas[node].lhs);
17111820 const result = try gz.addUnNode(.bool_not, operand, node);
17121821 return rvalue(gz, scope, rl, result, node);
17131822}
......@@ -1778,7 +1887,7 @@ fn ptrType(
17781887 trailing_count += 1;
17791888 }
17801889 if (ptr_info.ast.align_node != 0) {
1781 align_ref = try expr(gz, scope, .none, ptr_info.ast.align_node);
1890 align_ref = try expr(gz, scope, align_rl, ptr_info.ast.align_node);
17821891 trailing_count += 1;
17831892 }
17841893 if (ptr_info.ast.bit_range_start != 0) {
......@@ -1978,19 +2087,16 @@ fn fnDecl(
19782087 );
19792088
19802089 const cc: Zir.Inst.Ref = if (fn_proto.ast.callconv_expr != 0)
1981 // TODO instead of enum literal type, this needs to be the
1982 // std.builtin.CallingConvention enum. We need to implement importing other files
1983 // and enums in order to fix this.
19842090 try AstGen.expr(
19852091 &decl_gz,
19862092 &decl_gz.base,
1987 .{ .ty = .enum_literal_type },
2093 .{ .ty = .calling_convention_type },
19882094 fn_proto.ast.callconv_expr,
19892095 )
19902096 else if (is_extern) // note: https://github.com/ziglang/zig/issues/5269
1991 try decl_gz.addSmallStr(.enum_literal_small, "C")
2097 Zir.Inst.Ref.calling_convention_c
19922098 else
1993 .none;
2099 Zir.Inst.Ref.none;
19942100
19952101 const func_inst: Zir.Inst.Ref = if (body_node == 0) func: {
19962102 if (is_extern) {
......@@ -3079,7 +3185,7 @@ fn boolBinOp(
30793185) InnerError!Zir.Inst.Ref {
30803186 const node_datas = gz.tree().nodes.items(.data);
30813187
3082 const lhs = try expr(gz, scope, .{ .ty = .bool_type }, node_datas[node].lhs);
3188 const lhs = try expr(gz, scope, bool_rl, node_datas[node].lhs);
30833189 const bool_br = try gz.addBoolBr(zir_tag, lhs);
30843190
30853191 var rhs_scope: GenZir = .{
......@@ -3089,7 +3195,7 @@ fn boolBinOp(
30893195 .force_comptime = gz.force_comptime,
30903196 };
30913197 defer rhs_scope.instructions.deinit(gz.astgen.gpa);
3092 const rhs = try expr(&rhs_scope, &rhs_scope.base, .{ .ty = .bool_type }, node_datas[node].rhs);
3198 const rhs = try expr(&rhs_scope, &rhs_scope.base, bool_rl, node_datas[node].rhs);
30933199 _ = try rhs_scope.addBreak(.break_inline, bool_br, rhs);
30943200 try rhs_scope.setBoolBrBody(bool_br);
30953201
......@@ -3122,7 +3228,7 @@ fn ifExpr(
31223228 } else if (if_full.payload_token) |payload_token| {
31233229 return astgen.failTok(payload_token, "TODO implement if optional", .{});
31243230 } else {
3125 break :c try expr(&block_scope, &block_scope.base, .{ .ty = .bool_type }, if_full.ast.cond_expr);
3231 break :c try expr(&block_scope, &block_scope.base, bool_rl, if_full.ast.cond_expr);
31263232 }
31273233 };
31283234
......@@ -3291,8 +3397,7 @@ fn whileExpr(
32913397 } else if (while_full.payload_token) |payload_token| {
32923398 return astgen.failTok(payload_token, "TODO implement while optional", .{});
32933399 } else {
3294 const bool_type_rl: ResultLoc = .{ .ty = .bool_type };
3295 break :c try expr(&continue_scope, &continue_scope.base, bool_type_rl, while_full.ast.cond_expr);
3400 break :c try expr(&continue_scope, &continue_scope.base, bool_rl, while_full.ast.cond_expr);
32963401 }
32973402 };
32983403
......@@ -4758,37 +4863,8 @@ fn builtinCall(
47584863 }
47594864 }
47604865
4866 // zig fmt: off
47614867 switch (info.tag) {
4762 .ptr_to_int => {
4763 const operand = try expr(gz, scope, .none, params[0]);
4764 const result = try gz.addUnNode(.ptrtoint, operand, node);
4765 return rvalue(gz, scope, rl, result, node);
4766 },
4767 .float_cast => {
4768 const dest_type = try typeExpr(gz, scope, params[0]);
4769 const rhs = try expr(gz, scope, .none, params[1]);
4770 const result = try gz.addPlNode(.floatcast, node, Zir.Inst.Bin{
4771 .lhs = dest_type,
4772 .rhs = rhs,
4773 });
4774 return rvalue(gz, scope, rl, result, node);
4775 },
4776 .int_cast => {
4777 const dest_type = try typeExpr(gz, scope, params[0]);
4778 const rhs = try expr(gz, scope, .none, params[1]);
4779 const result = try gz.addPlNode(.intcast, node, Zir.Inst.Bin{
4780 .lhs = dest_type,
4781 .rhs = rhs,
4782 });
4783 return rvalue(gz, scope, rl, result, node);
4784 },
4785 .breakpoint => {
4786 _ = try gz.add(.{
4787 .tag = .breakpoint,
4788 .data = .{ .node = gz.nodeIndexToRelative(node) },
4789 });
4790 return rvalue(gz, scope, rl, .void_value, node);
4791 },
47924868 .import => {
47934869 const node_tags = tree.nodes.items(.tag);
47944870 const node_datas = tree.nodes.items(.data);
......@@ -4804,26 +4880,6 @@ fn builtinCall(
48044880 const result = try gz.addStrTok(.import, str.index, str_lit_token);
48054881 return rvalue(gz, scope, rl, result, node);
48064882 },
4807 .error_to_int => {
4808 const target = try expr(gz, scope, .none, params[0]);
4809 const result = try gz.addUnNode(.error_to_int, target, node);
4810 return rvalue(gz, scope, rl, result, node);
4811 },
4812 .int_to_error => {
4813 const target = try expr(gz, scope, .{ .ty = .u16_type }, params[0]);
4814 const result = try gz.addUnNode(.int_to_error, target, node);
4815 return rvalue(gz, scope, rl, result, node);
4816 },
4817 .compile_error => {
4818 const target = try expr(gz, scope, .none, params[0]);
4819 const result = try gz.addUnNode(.compile_error, target, node);
4820 return rvalue(gz, scope, rl, result, node);
4821 },
4822 .set_eval_branch_quota => {
4823 const quota = try expr(gz, scope, .{ .ty = .u32_type }, params[0]);
4824 const result = try gz.addUnNode(.set_eval_branch_quota, quota, node);
4825 return rvalue(gz, scope, rl, result, node);
4826 },
48274883 .compile_log => {
48284884 const arg_refs = try astgen.gpa.alloc(Zir.Inst.Ref, params.len);
48294885 defer astgen.gpa.free(arg_refs);
......@@ -4850,23 +4906,11 @@ fn builtinCall(
48504906 });
48514907 return rvalue(gz, scope, rl, result, node);
48524908 },
4853 .as => return as(gz, scope, rl, node, params[0], params[1]),
4854 .bit_cast => return bitCast(gz, scope, rl, node, params[0], params[1]),
4855 .TypeOf => return typeOf(gz, scope, rl, node, params),
4856
4857 .int_to_enum => {
4858 const result = try gz.addPlNode(.int_to_enum, node, Zir.Inst.Bin{
4859 .lhs = try typeExpr(gz, scope, params[0]),
4860 .rhs = try expr(gz, scope, .none, params[1]),
4861 });
4862 return rvalue(gz, scope, rl, result, node);
4863 },
4864
4865 .enum_to_int => {
4866 const operand = try expr(gz, scope, .none, params[0]);
4867 const result = try gz.addUnNode(.enum_to_int, operand, node);
4868 return rvalue(gz, scope, rl, result, node);
4869 },
4909 .as => return as( gz, scope, rl, node, params[0], params[1]),
4910 .bit_cast => return bitCast( gz, scope, rl, node, params[0], params[1]),
4911 .TypeOf => return typeOf( gz, scope, rl, node, params),
4912 .union_init => return unionInit(gz, scope, rl, node, params),
4913 .c_import => return cImport( gz, scope, rl, node, params[0]),
48704914
48714915 .@"export" => {
48724916 // TODO: @export is supposed to be able to export things other than functions.
......@@ -4882,38 +4926,147 @@ fn builtinCall(
48824926 return rvalue(gz, scope, rl, .void_value, node);
48834927 },
48844928
4885 .has_decl => {
4886 const container_type = try typeExpr(gz, scope, params[0]);
4887 const name = try comptimeExpr(gz, scope, .{ .ty = .const_slice_u8_type }, params[1]);
4888 const result = try gz.addPlNode(.has_decl, node, Zir.Inst.Bin{
4889 .lhs = container_type,
4890 .rhs = name,
4929 .breakpoint => return simpleNoOpVoid(gz, scope, rl, node, .breakpoint),
4930 .fence => return simpleNoOpVoid(gz, scope, rl, node, .fence),
4931
4932 .This => return rvalue(gz, scope, rl, try gz.addNode(.this, node), node),
4933 .return_address => return rvalue(gz, scope, rl, try gz.addNode(.ret_addr, node), node),
4934 .src => return rvalue(gz, scope, rl, try gz.addNode(.builtin_src, node), node),
4935 .error_return_trace => return rvalue(gz, scope, rl, try gz.addNode(.error_return_trace, node), node),
4936 .frame => return rvalue(gz, scope, rl, try gz.addNode(.frame, node), node),
4937 .frame_address => return rvalue(gz, scope, rl, try gz.addNode(.frame_address, node), node),
4938
4939 .type_info => return simpleUnOpType(gz, scope, rl, node, params[0], .type_info),
4940 .size_of => return simpleUnOpType(gz, scope, rl, node, params[0], .size_of),
4941 .bit_size_of => return simpleUnOpType(gz, scope, rl, node, params[0], .bit_size_of),
4942 .align_of => return simpleUnOpType(gz, scope, rl, node, params[0], .align_of),
4943
4944 .ptr_to_int => return simpleUnOp(gz, scope, rl, node, .none, params[0], .ptr_to_int),
4945 .error_to_int => return simpleUnOp(gz, scope, rl, node, .none, params[0], .error_to_int),
4946 .int_to_error => return simpleUnOp(gz, scope, rl, node, .{ .ty = .u16_type }, params[0], .int_to_error),
4947 .compile_error => return simpleUnOp(gz, scope, rl, node, .{ .ty = .const_slice_u8_type }, params[0], .compile_error),
4948 .set_eval_branch_quota => return simpleUnOp(gz, scope, rl, node, .{ .ty = .u32_type }, params[0], .set_eval_branch_quota),
4949 .enum_to_int => return simpleUnOp(gz, scope, rl, node, .none, params[0], .enum_to_int),
4950 .bool_to_int => return simpleUnOp(gz, scope, rl, node, bool_rl, params[0], .bool_to_int),
4951 .embed_file => return simpleUnOp(gz, scope, rl, node, .{ .ty = .const_slice_u8_type }, params[0], .embed_file),
4952 .error_name => return simpleUnOp(gz, scope, rl, node, .{ .ty = .anyerror_type }, params[0], .error_name),
4953 .panic => return simpleUnOp(gz, scope, rl, node, .{ .ty = .const_slice_u8_type }, params[0], .panic),
4954 .set_align_stack => return simpleUnOp(gz, scope, rl, node, align_rl, params[0], .set_align_stack),
4955 .set_cold => return simpleUnOp(gz, scope, rl, node, bool_rl, params[0], .set_cold),
4956 .set_float_mode => return simpleUnOp(gz, scope, rl, node, .{ .ty = .float_mode_type }, params[0], .set_float_mode),
4957 .set_runtime_safety => return simpleUnOp(gz, scope, rl, node, bool_rl, params[0], .set_runtime_safety),
4958 .sqrt => return simpleUnOp(gz, scope, rl, node, .none, params[0], .sqrt),
4959 .sin => return simpleUnOp(gz, scope, rl, node, .none, params[0], .sin),
4960 .cos => return simpleUnOp(gz, scope, rl, node, .none, params[0], .cos),
4961 .exp => return simpleUnOp(gz, scope, rl, node, .none, params[0], .exp),
4962 .exp2 => return simpleUnOp(gz, scope, rl, node, .none, params[0], .exp2),
4963 .log => return simpleUnOp(gz, scope, rl, node, .none, params[0], .log),
4964 .log2 => return simpleUnOp(gz, scope, rl, node, .none, params[0], .log2),
4965 .log10 => return simpleUnOp(gz, scope, rl, node, .none, params[0], .log10),
4966 .fabs => return simpleUnOp(gz, scope, rl, node, .none, params[0], .fabs),
4967 .floor => return simpleUnOp(gz, scope, rl, node, .none, params[0], .floor),
4968 .ceil => return simpleUnOp(gz, scope, rl, node, .none, params[0], .ceil),
4969 .trunc => return simpleUnOp(gz, scope, rl, node, .none, params[0], .trunc),
4970 .round => return simpleUnOp(gz, scope, rl, node, .none, params[0], .round),
4971 .tag_name => return simpleUnOp(gz, scope, rl, node, .none, params[0], .tag_name),
4972 .Type => return simpleUnOp(gz, scope, rl, node, .none, params[0], .reify),
4973 .type_name => return simpleUnOp(gz, scope, rl, node, .none, params[0], .type_name),
4974 .Frame => return simpleUnOp(gz, scope, rl, node, .none, params[0], .frame_type),
4975 .frame_size => return simpleUnOp(gz, scope, rl, node, .none, params[0], .frame_size),
4976
4977 .float_to_int => return typeCast(gz, scope, rl, node, params[0], params[1], .float_to_int),
4978 .int_to_float => return typeCast(gz, scope, rl, node, params[0], params[1], .int_to_float),
4979 .int_to_ptr => return typeCast(gz, scope, rl, node, params[0], params[1], .int_to_ptr),
4980 .int_to_enum => return typeCast(gz, scope, rl, node, params[0], params[1], .int_to_enum),
4981 .float_cast => return typeCast(gz, scope, rl, node, params[0], params[1], .float_cast),
4982 .int_cast => return typeCast(gz, scope, rl, node, params[0], params[1], .int_cast),
4983 .err_set_cast => return typeCast(gz, scope, rl, node, params[0], params[1], .err_set_cast),
4984 .ptr_cast => return typeCast(gz, scope, rl, node, params[0], params[1], .ptr_cast),
4985 .truncate => return typeCast(gz, scope, rl, node, params[0], params[1], .truncate),
4986 .align_cast => {
4987 const dest_align = try comptimeExpr(gz, scope, align_rl, params[0]);
4988 const rhs = try expr(gz, scope, .none, params[1]);
4989 const result = try gz.addPlNode(.align_cast, node, Zir.Inst.Bin{
4990 .lhs = dest_align,
4991 .rhs = rhs,
48914992 });
48924993 return rvalue(gz, scope, rl, result, node);
48934994 },
48944995
4895 .type_info => {
4896 const operand = try typeExpr(gz, scope, params[0]);
4897 const result = try gz.addUnNode(.type_info, operand, node);
4996 .has_decl => return hasDeclOrField(gz, scope, rl, node, params[0], params[1], .has_decl),
4997 .has_field => return hasDeclOrField(gz, scope, rl, node, params[0], params[1], .has_field),
4998
4999 .clz => return bitBuiltin(gz, scope, rl, node, params[0], params[1], .clz),
5000 .ctz => return bitBuiltin(gz, scope, rl, node, params[0], params[1], .ctz),
5001 .pop_count => return bitBuiltin(gz, scope, rl, node, params[0], params[1], .pop_count),
5002 .byte_swap => return bitBuiltin(gz, scope, rl, node, params[0], params[1], .byte_swap),
5003 .bit_reverse => return bitBuiltin(gz, scope, rl, node, params[0], params[1], .bit_reverse),
5004
5005 .div_exact => return divBuiltin(gz, scope, rl, node, params[0], params[1], .div_exact),
5006 .div_floor => return divBuiltin(gz, scope, rl, node, params[0], params[1], .div_floor),
5007 .div_trunc => return divBuiltin(gz, scope, rl, node, params[0], params[1], .div_trunc),
5008 .mod => return divBuiltin(gz, scope, rl, node, params[0], params[1], .mod),
5009 .rem => return divBuiltin(gz, scope, rl, node, params[0], params[1], .rem),
5010
5011 .shl_exact => return shiftOp(gz, scope, rl, node, params[0], params[1], .shl_exact),
5012 .shr_exact => return shiftOp(gz, scope, rl, node, params[0], params[1], .shr_exact),
5013
5014 .bit_offset_of => return offsetOf(gz, scope, rl, node, params[0], params[1], .bit_offset_of),
5015 .byte_offset_of => return offsetOf(gz, scope, rl, node, params[0], params[1], .byte_offset_of),
5016
5017 .c_undef => return simpleCBuiltin(gz, scope, rl, node, params[0], .c_undef),
5018 .c_include => return simpleCBuiltin(gz, scope, rl, node, params[0], .c_include),
5019
5020 .cmpxchg_strong => return cmpxchg(gz, scope, rl, node, params, .cmpxchg_strong),
5021 .cmpxchg_weak => return cmpxchg(gz, scope, rl, node, params, .cmpxchg_weak),
5022
5023 .wasm_memory_size => {
5024 const operand = try expr(gz, scope, .{ .ty = .u32_type }, params[0]);
5025 const result = try gz.addExtendedPayload(.wasm_memory_size, Zir.Inst.UnNode{
5026 .node = gz.nodeIndexToRelative(node),
5027 .operand = operand,
5028 });
48985029 return rvalue(gz, scope, rl, result, node);
48995030 },
4900
4901 .size_of => {
4902 const operand = try typeExpr(gz, scope, params[0]);
4903 const result = try gz.addUnNode(.size_of, operand, node);
5031 .wasm_memory_grow => {
5032 const index_arg = try expr(gz, scope, .{ .ty = .u32_type }, params[0]);
5033 const delta_arg = try expr(gz, scope, .{ .ty = .u32_type }, params[1]);
5034 const result = try gz.addExtendedPayload(.wasm_memory_grow, Zir.Inst.BinNode{
5035 .node = gz.nodeIndexToRelative(node),
5036 .lhs = index_arg,
5037 .rhs = delta_arg,
5038 });
49045039 return rvalue(gz, scope, rl, result, node);
49055040 },
4906
4907 .bit_size_of => {
4908 const operand = try typeExpr(gz, scope, params[0]);
4909 const result = try gz.addUnNode(.bit_size_of, operand, node);
5041 .c_define => {
5042 const name = try comptimeExpr(gz, scope, .{ .ty = .const_slice_u8_type }, params[0]);
5043 const value = try comptimeExpr(gz, scope, .none, params[1]);
5044 const result = try gz.addExtendedPayload(.c_define, Zir.Inst.BinNode{
5045 .node = gz.nodeIndexToRelative(node),
5046 .lhs = name,
5047 .rhs = value,
5048 });
49105049 return rvalue(gz, scope, rl, result, node);
49115050 },
49125051
4913 .This => return rvalue(gz, scope, rl, try gz.addNode(.this, node), node),
4914 .fence => return rvalue(gz, scope, rl, try gz.addNode(.fence, node), node),
4915 .return_address => return rvalue(gz, scope, rl, try gz.addNode(.ret_addr, node), node),
4916 .src => return rvalue(gz, scope, rl, try gz.addNode(.builtin_src, node), node),
5052 .splat => {
5053 const len = try expr(gz, scope, .{ .ty = .u32_type }, params[0]);
5054 const scalar = try expr(gz, scope, .none, params[1]);
5055 const result = try gz.addPlNode(.splat, node, Zir.Inst.Bin{
5056 .lhs = len,
5057 .rhs = scalar,
5058 });
5059 return rvalue(gz, scope, rl, result, node);
5060 },
5061 .reduce => {
5062 const op = try expr(gz, scope, .{ .ty = .reduce_op_type }, params[0]);
5063 const scalar = try expr(gz, scope, .none, params[1]);
5064 const result = try gz.addPlNode(.reduce, node, Zir.Inst.Bin{
5065 .lhs = op,
5066 .rhs = scalar,
5067 });
5068 return rvalue(gz, scope, rl, result, node);
5069 },
49175070
49185071 .add_with_overflow => return overflowArithmetic(gz, scope, rl, node, params, .add_with_overflow),
49195072 .sub_with_overflow => return overflowArithmetic(gz, scope, rl, node, params, .sub_with_overflow),
......@@ -4941,83 +5094,373 @@ fn builtinCall(
49415094 return rvalue(gz, scope, rl, result, node);
49425095 },
49435096
4944 .align_cast,
4945 .align_of,
4946 .atomic_load,
4947 .atomic_rmw,
4948 .atomic_store,
4949 .bit_offset_of,
4950 .bool_to_int,
4951 .mul_add,
4952 .byte_swap,
4953 .bit_reverse,
4954 .byte_offset_of,
4955 .call,
4956 .c_define,
4957 .c_import,
4958 .c_include,
4959 .clz,
4960 .cmpxchg_strong,
4961 .cmpxchg_weak,
4962 .ctz,
4963 .c_undef,
4964 .div_exact,
4965 .div_floor,
4966 .div_trunc,
4967 .embed_file,
4968 .error_name,
4969 .error_return_trace,
4970 .err_set_cast,
4971 .field_parent_ptr,
4972 .float_to_int,
4973 .has_field,
4974 .int_to_float,
4975 .int_to_ptr,
4976 .memcpy,
4977 .memset,
4978 .wasm_memory_size,
4979 .wasm_memory_grow,
4980 .mod,
4981 .panic,
4982 .pop_count,
4983 .ptr_cast,
4984 .rem,
4985 .set_align_stack,
4986 .set_cold,
4987 .set_float_mode,
4988 .set_runtime_safety,
4989 .shl_exact,
4990 .shr_exact,
4991 .shuffle,
4992 .splat,
4993 .reduce,
4994 .sqrt,
4995 .sin,
4996 .cos,
4997 .exp,
4998 .exp2,
4999 .log,
5000 .log2,
5001 .log10,
5002 .fabs,
5003 .floor,
5004 .ceil,
5005 .trunc,
5006 .round,
5007 .tag_name,
5008 .truncate,
5009 .Type,
5010 .type_name,
5011 .union_init,
5012 .async_call,
5013 .frame,
5014 .Frame,
5015 .frame_address,
5016 .frame_size,
5017 => return astgen.failNode(node, "TODO: implement builtin function {s}", .{
5018 builtin_name,
5019 }),
5097 .atomic_load => {
5098 const int_type = try typeExpr(gz, scope, params[0]);
5099 const ptr_type = try gz.add(.{ .tag = .ptr_type_simple, .data = .{
5100 .ptr_type_simple = .{
5101 .is_allowzero = false,
5102 .is_mutable = false,
5103 .is_volatile = false,
5104 .size = .One,
5105 .elem_type = int_type,
5106 },
5107 } });
5108 const ptr = try expr(gz, scope, .{ .ty = ptr_type }, params[1]);
5109 const ordering = try expr(gz, scope, .{ .ty = .atomic_ordering_type }, params[2]);
5110 const result = try gz.addPlNode(.atomic_load, node, Zir.Inst.Bin{
5111 .lhs = ptr,
5112 .rhs = ordering,
5113 });
5114 return rvalue(gz, scope, rl, result, node);
5115 },
5116 .atomic_rmw => {
5117 const int_type = try typeExpr(gz, scope, params[0]);
5118 const ptr_type = try gz.add(.{ .tag = .ptr_type_simple, .data = .{
5119 .ptr_type_simple = .{
5120 .is_allowzero = false,
5121 .is_mutable = true,
5122 .is_volatile = false,
5123 .size = .One,
5124 .elem_type = int_type,
5125 },
5126 } });
5127 const ptr = try expr(gz, scope, .{ .ty = ptr_type }, params[1]);
5128 const operation = try expr(gz, scope, .{ .ty = .atomic_rmw_op_type }, params[2]);
5129 const operand = try expr(gz, scope, .{ .ty = int_type }, params[3]);
5130 const ordering = try expr(gz, scope, .{ .ty = .atomic_ordering_type }, params[4]);
5131 const result = try gz.addPlNode(.atomic_rmw, node, Zir.Inst.AtomicRmw{
5132 .ptr = ptr,
5133 .operation = operation,
5134 .operand = operand,
5135 .ordering = ordering,
5136 });
5137 return rvalue(gz, scope, rl, result, node);
5138 },
5139 .atomic_store => {
5140 const int_type = try typeExpr(gz, scope, params[0]);
5141 const ptr_type = try gz.add(.{ .tag = .ptr_type_simple, .data = .{
5142 .ptr_type_simple = .{
5143 .is_allowzero = false,
5144 .is_mutable = true,
5145 .is_volatile = false,
5146 .size = .One,
5147 .elem_type = int_type,
5148 },
5149 } });
5150 const ptr = try expr(gz, scope, .{ .ty = ptr_type }, params[1]);
5151 const operand = try expr(gz, scope, .{ .ty = int_type }, params[2]);
5152 const ordering = try expr(gz, scope, .{ .ty = .atomic_ordering_type }, params[3]);
5153 const result = try gz.addPlNode(.atomic_store, node, Zir.Inst.AtomicStore{
5154 .ptr = ptr,
5155 .operand = operand,
5156 .ordering = ordering,
5157 });
5158 return rvalue(gz, scope, rl, result, node);
5159 },
5160 .mul_add => {
5161 const float_type = try typeExpr(gz, scope, params[0]);
5162 const mulend1 = try expr(gz, scope, .{ .ty = float_type }, params[1]);
5163 const mulend2 = try expr(gz, scope, .{ .ty = float_type }, params[2]);
5164 const addend = try expr(gz, scope, .{ .ty = float_type }, params[3]);
5165 const result = try gz.addPlNode(.mul_add, node, Zir.Inst.MulAdd{
5166 .mulend1 = mulend1,
5167 .mulend2 = mulend2,
5168 .addend = addend,
5169 });
5170 return rvalue(gz, scope, rl, result, node);
5171 },
5172 .call => {
5173 const options = try comptimeExpr(gz, scope, .{ .ty = .call_options_type }, params[0]);
5174 const callee = try expr(gz, scope, .none, params[1]);
5175 const args = try expr(gz, scope, .none, params[2]);
5176 const result = try gz.addPlNode(.builtin_call, node, Zir.Inst.BuiltinCall{
5177 .options = options,
5178 .callee = callee,
5179 .args = args,
5180 });
5181 return rvalue(gz, scope, rl, result, node);
5182 },
5183 .field_parent_ptr => {
5184 const parent_type = try typeExpr(gz, scope, params[0]);
5185 const field_name = try comptimeExpr(gz, scope, .{ .ty = .const_slice_u8_type }, params[1]);
5186 const field_ptr_type = try gz.addBin(.field_ptr_type, parent_type, field_name);
5187 const result = try gz.addPlNode(.field_parent_ptr, node, Zir.Inst.FieldParentPtr{
5188 .parent_type = parent_type,
5189 .field_name = field_name,
5190 .field_ptr = try expr(gz, scope, .{ .ty = field_ptr_type }, params[2]),
5191 });
5192 return rvalue(gz, scope, rl, result, node);
5193 },
5194 .memcpy => {
5195 const result = try gz.addPlNode(.memcpy, node, Zir.Inst.Memcpy{
5196 .dest = try expr(gz, scope, .{ .ty = .manyptr_u8_type }, params[0]),
5197 .source = try expr(gz, scope, .{ .ty = .manyptr_const_u8_type }, params[1]),
5198 .byte_count = try expr(gz, scope, .{ .ty = .usize_type }, params[2]),
5199 });
5200 return rvalue(gz, scope, rl, result, node);
5201 },
5202 .memset => {
5203 const result = try gz.addPlNode(.memset, node, Zir.Inst.Memset{
5204 .dest = try expr(gz, scope, .{ .ty = .manyptr_u8_type }, params[0]),
5205 .byte = try expr(gz, scope, .{ .ty = .u8_type }, params[1]),
5206 .byte_count = try expr(gz, scope, .{ .ty = .usize_type }, params[2]),
5207 });
5208 return rvalue(gz, scope, rl, result, node);
5209 },
5210 .shuffle => {
5211 const result = try gz.addPlNode(.shuffle, node, Zir.Inst.Shuffle{
5212 .elem_type = try typeExpr(gz, scope, params[0]),
5213 .a = try expr(gz, scope, .none, params[1]),
5214 .b = try expr(gz, scope, .none, params[2]),
5215 .mask = try comptimeExpr(gz, scope, .none, params[3]),
5216 });
5217 return rvalue(gz, scope, rl, result, node);
5218 },
5219 .async_call => {
5220 const result = try gz.addPlNode(.builtin_async_call, node, Zir.Inst.AsyncCall{
5221 .frame_buffer = try expr(gz, scope, .none, params[0]),
5222 .result_ptr = try expr(gz, scope, .none, params[1]),
5223 .fn_ptr = try expr(gz, scope, .none, params[2]),
5224 .args = try expr(gz, scope, .none, params[3]),
5225 });
5226 return rvalue(gz, scope, rl, result, node);
5227 },
50205228 }
5229 // zig fmt: on
5230}
5231
5232fn simpleNoOpVoid(
5233 gz: *GenZir,
5234 scope: *Scope,
5235 rl: ResultLoc,
5236 node: ast.Node.Index,
5237 tag: Zir.Inst.Tag,
5238) InnerError!Zir.Inst.Ref {
5239 _ = try gz.addNode(tag, node);
5240 return rvalue(gz, scope, rl, .void_value, node);
5241}
5242
5243fn hasDeclOrField(
5244 gz: *GenZir,
5245 scope: *Scope,
5246 rl: ResultLoc,
5247 node: ast.Node.Index,
5248 lhs_node: ast.Node.Index,
5249 rhs_node: ast.Node.Index,
5250 tag: Zir.Inst.Tag,
5251) InnerError!Zir.Inst.Ref {
5252 const container_type = try typeExpr(gz, scope, lhs_node);
5253 const name = try comptimeExpr(gz, scope, .{ .ty = .const_slice_u8_type }, rhs_node);
5254 const result = try gz.addPlNode(tag, node, Zir.Inst.Bin{
5255 .lhs = container_type,
5256 .rhs = name,
5257 });
5258 return rvalue(gz, scope, rl, result, node);
5259}
5260
5261fn typeCast(
5262 gz: *GenZir,
5263 scope: *Scope,
5264 rl: ResultLoc,
5265 node: ast.Node.Index,
5266 lhs_node: ast.Node.Index,
5267 rhs_node: ast.Node.Index,
5268 tag: Zir.Inst.Tag,
5269) InnerError!Zir.Inst.Ref {
5270 const result = try gz.addPlNode(tag, node, Zir.Inst.Bin{
5271 .lhs = try typeExpr(gz, scope, lhs_node),
5272 .rhs = try expr(gz, scope, .none, rhs_node),
5273 });
5274 return rvalue(gz, scope, rl, result, node);
5275}
5276
5277fn simpleUnOpType(
5278 gz: *GenZir,
5279 scope: *Scope,
5280 rl: ResultLoc,
5281 node: ast.Node.Index,
5282 operand_node: ast.Node.Index,
5283 tag: Zir.Inst.Tag,
5284) InnerError!Zir.Inst.Ref {
5285 const operand = try typeExpr(gz, scope, operand_node);
5286 const result = try gz.addUnNode(tag, operand, node);
5287 return rvalue(gz, scope, rl, result, node);
5288}
5289
5290fn simpleUnOp(
5291 gz: *GenZir,
5292 scope: *Scope,
5293 rl: ResultLoc,
5294 node: ast.Node.Index,
5295 operand_rl: ResultLoc,
5296 operand_node: ast.Node.Index,
5297 tag: Zir.Inst.Tag,
5298) InnerError!Zir.Inst.Ref {
5299 const operand = try expr(gz, scope, operand_rl, operand_node);
5300 const result = try gz.addUnNode(tag, operand, node);
5301 return rvalue(gz, scope, rl, result, node);
5302}
5303
5304fn cmpxchg(
5305 gz: *GenZir,
5306 scope: *Scope,
5307 rl: ResultLoc,
5308 node: ast.Node.Index,
5309 params: []const ast.Node.Index,
5310 tag: Zir.Inst.Tag,
5311) InnerError!Zir.Inst.Ref {
5312 const int_type = try typeExpr(gz, scope, params[0]);
5313 const ptr_type = try gz.add(.{ .tag = .ptr_type_simple, .data = .{
5314 .ptr_type_simple = .{
5315 .is_allowzero = false,
5316 .is_mutable = true,
5317 .is_volatile = false,
5318 .size = .One,
5319 .elem_type = int_type,
5320 },
5321 } });
5322 const result = try gz.addPlNode(tag, node, Zir.Inst.Cmpxchg{
5323 // zig fmt: off
5324 .ptr = try expr(gz, scope, .{ .ty = ptr_type }, params[1]),
5325 .expected_value = try expr(gz, scope, .{ .ty = int_type }, params[2]),
5326 .new_value = try expr(gz, scope, .{ .ty = int_type }, params[3]),
5327 .success_order = try expr(gz, scope, .{ .ty = .atomic_ordering_type }, params[4]),
5328 .fail_order = try expr(gz, scope, .{ .ty = .atomic_ordering_type }, params[5]),
5329 // zig fmt: on
5330 });
5331 return rvalue(gz, scope, rl, result, node);
5332}
5333
5334fn bitBuiltin(
5335 gz: *GenZir,
5336 scope: *Scope,
5337 rl: ResultLoc,
5338 node: ast.Node.Index,
5339 int_type_node: ast.Node.Index,
5340 operand_node: ast.Node.Index,
5341 tag: Zir.Inst.Tag,
5342) InnerError!Zir.Inst.Ref {
5343 const int_type = try typeExpr(gz, scope, int_type_node);
5344 const operand = try expr(gz, scope, .{ .ty = int_type }, operand_node);
5345 const result = try gz.addUnNode(tag, operand, node);
5346 return rvalue(gz, scope, rl, result, node);
5347}
5348
5349fn divBuiltin(
5350 gz: *GenZir,
5351 scope: *Scope,
5352 rl: ResultLoc,
5353 node: ast.Node.Index,
5354 lhs_node: ast.Node.Index,
5355 rhs_node: ast.Node.Index,
5356 tag: Zir.Inst.Tag,
5357) InnerError!Zir.Inst.Ref {
5358 const result = try gz.addPlNode(tag, node, Zir.Inst.Bin{
5359 .lhs = try expr(gz, scope, .none, lhs_node),
5360 .rhs = try expr(gz, scope, .none, rhs_node),
5361 });
5362 return rvalue(gz, scope, rl, result, node);
5363}
5364
5365fn simpleCBuiltin(
5366 gz: *GenZir,
5367 scope: *Scope,
5368 rl: ResultLoc,
5369 node: ast.Node.Index,
5370 operand_node: ast.Node.Index,
5371 tag: Zir.Inst.Extended,
5372) InnerError!Zir.Inst.Ref {
5373 const operand = try comptimeExpr(gz, scope, .{ .ty = .const_slice_u8_type }, operand_node);
5374 _ = try gz.addExtendedPayload(tag, Zir.Inst.UnNode{
5375 .node = gz.nodeIndexToRelative(node),
5376 .operand = operand,
5377 });
5378 return rvalue(gz, scope, rl, .void_value, node);
5379}
5380
5381fn offsetOf(
5382 gz: *GenZir,
5383 scope: *Scope,
5384 rl: ResultLoc,
5385 node: ast.Node.Index,
5386 lhs_node: ast.Node.Index,
5387 rhs_node: ast.Node.Index,
5388 tag: Zir.Inst.Tag,
5389) InnerError!Zir.Inst.Ref {
5390 const type_inst = try typeExpr(gz, scope, lhs_node);
5391 const field_name = try comptimeExpr(gz, scope, .{ .ty = .const_slice_u8_type }, rhs_node);
5392 const result = try gz.addPlNode(tag, node, Zir.Inst.Bin{
5393 .lhs = type_inst,
5394 .rhs = field_name,
5395 });
5396 return rvalue(gz, scope, rl, result, node);
5397}
5398
5399fn shiftOp(
5400 gz: *GenZir,
5401 scope: *Scope,
5402 rl: ResultLoc,
5403 node: ast.Node.Index,
5404 lhs_node: ast.Node.Index,
5405 rhs_node: ast.Node.Index,
5406 tag: Zir.Inst.Tag,
5407) InnerError!Zir.Inst.Ref {
5408 const lhs = try expr(gz, scope, .none, lhs_node);
5409 const log2_int_type = try gz.addUnNode(.typeof_log2_int_type, lhs, lhs_node);
5410 const rhs = try expr(gz, scope, .{ .ty = log2_int_type }, rhs_node);
5411 const result = try gz.addPlNode(tag, node, Zir.Inst.Bin{
5412 .lhs = lhs,
5413 .rhs = rhs,
5414 });
5415 return rvalue(gz, scope, rl, result, node);
5416}
5417
5418fn cImport(
5419 gz: *GenZir,
5420 scope: *Scope,
5421 rl: ResultLoc,
5422 node: ast.Node.Index,
5423 body_node: ast.Node.Index,
5424) InnerError!Zir.Inst.Ref {
5425 const astgen = gz.astgen;
5426 const gpa = astgen.gpa;
5427
5428 var block_scope: GenZir = .{
5429 .parent = scope,
5430 .decl_node_index = gz.decl_node_index,
5431 .astgen = astgen,
5432 .force_comptime = true,
5433 .instructions = .{},
5434 };
5435 defer block_scope.instructions.deinit(gpa);
5436
5437 const block_inst = try gz.addBlock(.c_import, node);
5438 const block_result = try expr(&block_scope, &block_scope.base, .none, body_node);
5439 if (!gz.refIsNoReturn(block_result)) {
5440 _ = try block_scope.addBreak(.break_inline, block_inst, .void_value);
5441 }
5442 try block_scope.setBlockBody(block_inst);
5443 try gz.instructions.append(gpa, block_inst);
5444
5445 return rvalue(gz, scope, rl, .void_value, node);
5446}
5447
5448fn unionInit(
5449 gz: *GenZir,
5450 scope: *Scope,
5451 rl: ResultLoc,
5452 node: ast.Node.Index,
5453 params: []const ast.Node.Index,
5454) InnerError!Zir.Inst.Ref {
5455 const union_type = try typeExpr(gz, scope, params[0]);
5456 const field_name = try comptimeExpr(gz, scope, .{ .ty = .const_slice_u8_type }, params[1]);
5457 const union_init_ptr = try gz.addPlNode(.union_init_ptr, node, Zir.Inst.UnionInitPtr{
5458 .union_type = union_type,
5459 .field_name = field_name,
5460 });
5461 // TODO: set up a store_to_block_ptr elision thing here
5462 const result = try expr(gz, scope, .{ .ptr = union_init_ptr }, params[2]);
5463 return rvalue(gz, scope, rl, result, node);
50215464}
50225465
50235466fn overflowArithmetic(
src/Module.zig+48
......@@ -1198,6 +1198,30 @@ pub const Scope = struct {
11981198 gz.astgen.extra.appendSliceAssumeCapacity(gz.instructions.items);
11991199 }
12001200
1201 /// Same as `setBlockBody` except we don't copy instructions which are
1202 /// `store_to_block_ptr` instructions with lhs set to .none.
1203 pub fn setBlockBodyEliding(gz: GenZir, inst: Zir.Inst.Index) !void {
1204 const gpa = gz.astgen.gpa;
1205 try gz.astgen.extra.ensureCapacity(gpa, gz.astgen.extra.items.len +
1206 @typeInfo(Zir.Inst.Block).Struct.fields.len + gz.instructions.items.len);
1207 const zir_datas = gz.astgen.instructions.items(.data);
1208 const zir_tags = gz.astgen.instructions.items(.tag);
1209 const block_pl_index = gz.astgen.addExtraAssumeCapacity(Zir.Inst.Block{
1210 .body_len = @intCast(u32, gz.instructions.items.len),
1211 });
1212 zir_datas[inst].pl_node.payload_index = block_pl_index;
1213 for (gz.instructions.items) |sub_inst| {
1214 if (zir_tags[sub_inst] == .store_to_block_ptr and
1215 zir_datas[sub_inst].bin.lhs == .none)
1216 {
1217 // Decrement `body_len`.
1218 gz.astgen.extra.items[block_pl_index] -= 1;
1219 continue;
1220 }
1221 gz.astgen.extra.appendAssumeCapacity(sub_inst);
1222 }
1223 }
1224
12011225 pub fn identAsString(gz: *GenZir, ident_token: ast.TokenIndex) !u32 {
12021226 const astgen = gz.astgen;
12031227 const gpa = astgen.gpa;
......@@ -1445,6 +1469,30 @@ pub const Scope = struct {
14451469 return gz.indexToRef(new_index);
14461470 }
14471471
1472 pub fn addExtendedPayload(
1473 gz: *GenZir,
1474 opcode: Zir.Inst.Extended,
1475 extra: anytype,
1476 ) !Zir.Inst.Ref {
1477 const gpa = gz.astgen.gpa;
1478
1479 try gz.instructions.ensureCapacity(gpa, gz.instructions.items.len + 1);
1480 try gz.astgen.instructions.ensureCapacity(gpa, gz.astgen.instructions.len + 1);
1481
1482 const payload_index = try gz.astgen.addExtra(extra);
1483 const new_index = @intCast(Zir.Inst.Index, gz.astgen.instructions.len);
1484 gz.astgen.instructions.appendAssumeCapacity(.{
1485 .tag = .extended,
1486 .data = .{ .extended = .{
1487 .opcode = opcode,
1488 .small = undefined,
1489 .operand = payload_index,
1490 } },
1491 });
1492 gz.instructions.appendAssumeCapacity(new_index);
1493 return gz.indexToRef(new_index);
1494 }
1495
14481496 pub fn addArrayTypeSentinel(
14491497 gz: *GenZir,
14501498 len: Zir.Inst.Ref,
src/Sema.zig+687-162
......@@ -131,159 +131,227 @@ pub fn analyzeBody(
131131 while (true) : (i += 1) {
132132 const inst = body[i];
133133 map[inst] = switch (tags[inst]) {
134 .elided => continue,
135
136 .alloc => try sema.zirAlloc(block, inst),
137 .alloc_inferred => try sema.zirAllocInferred(block, inst, Type.initTag(.inferred_alloc_const)),
138 .alloc_inferred_mut => try sema.zirAllocInferred(block, inst, Type.initTag(.inferred_alloc_mut)),
139 .alloc_mut => try sema.zirAllocMut(block, inst),
140 .array_cat => try sema.zirArrayCat(block, inst),
141 .array_mul => try sema.zirArrayMul(block, inst),
142 .array_type => try sema.zirArrayType(block, inst),
143 .array_type_sentinel => try sema.zirArrayTypeSentinel(block, inst),
144 .as => try sema.zirAs(block, inst),
145 .as_node => try sema.zirAsNode(block, inst),
146 .@"asm" => try sema.zirAsm(block, inst, false),
147 .asm_volatile => try sema.zirAsm(block, inst, true),
148 .bit_and => try sema.zirBitwise(block, inst, .bit_and),
149 .bit_not => try sema.zirBitNot(block, inst),
150 .bit_or => try sema.zirBitwise(block, inst, .bit_or),
151 .bitcast => try sema.zirBitcast(block, inst),
152 .bitcast_result_ptr => try sema.zirBitcastResultPtr(block, inst),
153 .block => try sema.zirBlock(block, inst),
154 .bool_not => try sema.zirBoolNot(block, inst),
155 .bool_and => try sema.zirBoolOp(block, inst, false),
156 .bool_or => try sema.zirBoolOp(block, inst, true),
157 .bool_br_and => try sema.zirBoolBr(block, inst, false),
158 .bool_br_or => try sema.zirBoolBr(block, inst, true),
159 .call => try sema.zirCall(block, inst, .auto, false),
160 .call_chkused => try sema.zirCall(block, inst, .auto, true),
161 .call_compile_time => try sema.zirCall(block, inst, .compile_time, false),
162 .call_none => try sema.zirCallNone(block, inst, false),
163 .call_none_chkused => try sema.zirCallNone(block, inst, true),
164 .cmp_eq => try sema.zirCmp(block, inst, .eq),
165 .cmp_gt => try sema.zirCmp(block, inst, .gt),
166 .cmp_gte => try sema.zirCmp(block, inst, .gte),
167 .cmp_lt => try sema.zirCmp(block, inst, .lt),
168 .cmp_lte => try sema.zirCmp(block, inst, .lte),
169 .cmp_neq => try sema.zirCmp(block, inst, .neq),
170 .coerce_result_ptr => try sema.zirCoerceResultPtr(block, inst),
171 .decl_ref => try sema.zirDeclRef(block, inst),
172 .decl_val => try sema.zirDeclVal(block, inst),
173 .load => try sema.zirLoad(block, inst),
174 .elem_ptr => try sema.zirElemPtr(block, inst),
175 .elem_ptr_node => try sema.zirElemPtrNode(block, inst),
176 .elem_val => try sema.zirElemVal(block, inst),
177 .elem_val_node => try sema.zirElemValNode(block, inst),
178 .enum_literal => try sema.zirEnumLiteral(block, inst),
179 .enum_literal_small => try sema.zirEnumLiteralSmall(block, inst),
180 .enum_to_int => try sema.zirEnumToInt(block, inst),
181 .int_to_enum => try sema.zirIntToEnum(block, inst),
182 .err_union_code => try sema.zirErrUnionCode(block, inst),
183 .err_union_code_ptr => try sema.zirErrUnionCodePtr(block, inst),
184 .err_union_payload_safe => try sema.zirErrUnionPayload(block, inst, true),
185 .err_union_payload_safe_ptr => try sema.zirErrUnionPayloadPtr(block, inst, true),
186 .err_union_payload_unsafe => try sema.zirErrUnionPayload(block, inst, false),
134 // zig fmt: off
135 .alloc => try sema.zirAlloc(block, inst),
136 .alloc_inferred => try sema.zirAllocInferred(block, inst, Type.initTag(.inferred_alloc_const)),
137 .alloc_inferred_mut => try sema.zirAllocInferred(block, inst, Type.initTag(.inferred_alloc_mut)),
138 .alloc_mut => try sema.zirAllocMut(block, inst),
139 .array_cat => try sema.zirArrayCat(block, inst),
140 .array_mul => try sema.zirArrayMul(block, inst),
141 .array_type => try sema.zirArrayType(block, inst),
142 .array_type_sentinel => try sema.zirArrayTypeSentinel(block, inst),
143 .as => try sema.zirAs(block, inst),
144 .as_node => try sema.zirAsNode(block, inst),
145 .@"asm" => try sema.zirAsm(block, inst, false),
146 .asm_volatile => try sema.zirAsm(block, inst, true),
147 .bit_and => try sema.zirBitwise(block, inst, .bit_and),
148 .bit_not => try sema.zirBitNot(block, inst),
149 .bit_or => try sema.zirBitwise(block, inst, .bit_or),
150 .bitcast => try sema.zirBitcast(block, inst),
151 .bitcast_result_ptr => try sema.zirBitcastResultPtr(block, inst),
152 .block => try sema.zirBlock(block, inst),
153 .bool_not => try sema.zirBoolNot(block, inst),
154 .bool_and => try sema.zirBoolOp(block, inst, false),
155 .bool_or => try sema.zirBoolOp(block, inst, true),
156 .bool_br_and => try sema.zirBoolBr(block, inst, false),
157 .bool_br_or => try sema.zirBoolBr(block, inst, true),
158 .c_import => try sema.zirCImport(block, inst),
159 .call => try sema.zirCall(block, inst, .auto, false),
160 .call_chkused => try sema.zirCall(block, inst, .auto, true),
161 .call_compile_time => try sema.zirCall(block, inst, .compile_time, false),
162 .call_none => try sema.zirCallNone(block, inst, false),
163 .call_none_chkused => try sema.zirCallNone(block, inst, true),
164 .cmp_eq => try sema.zirCmp(block, inst, .eq),
165 .cmp_gt => try sema.zirCmp(block, inst, .gt),
166 .cmp_gte => try sema.zirCmp(block, inst, .gte),
167 .cmp_lt => try sema.zirCmp(block, inst, .lt),
168 .cmp_lte => try sema.zirCmp(block, inst, .lte),
169 .cmp_neq => try sema.zirCmp(block, inst, .neq),
170 .coerce_result_ptr => try sema.zirCoerceResultPtr(block, inst),
171 .decl_ref => try sema.zirDeclRef(block, inst),
172 .decl_val => try sema.zirDeclVal(block, inst),
173 .load => try sema.zirLoad(block, inst),
174 .elem_ptr => try sema.zirElemPtr(block, inst),
175 .elem_ptr_node => try sema.zirElemPtrNode(block, inst),
176 .elem_val => try sema.zirElemVal(block, inst),
177 .elem_val_node => try sema.zirElemValNode(block, inst),
178 .enum_literal => try sema.zirEnumLiteral(block, inst),
179 .enum_literal_small => try sema.zirEnumLiteralSmall(block, inst),
180 .enum_to_int => try sema.zirEnumToInt(block, inst),
181 .int_to_enum => try sema.zirIntToEnum(block, inst),
182 .err_union_code => try sema.zirErrUnionCode(block, inst),
183 .err_union_code_ptr => try sema.zirErrUnionCodePtr(block, inst),
184 .err_union_payload_safe => try sema.zirErrUnionPayload(block, inst, true),
185 .err_union_payload_safe_ptr => try sema.zirErrUnionPayloadPtr(block, inst, true),
186 .err_union_payload_unsafe => try sema.zirErrUnionPayload(block, inst, false),
187187 .err_union_payload_unsafe_ptr => try sema.zirErrUnionPayloadPtr(block, inst, false),
188 .error_union_type => try sema.zirErrorUnionType(block, inst),
189 .error_value => try sema.zirErrorValue(block, inst),
190 .error_to_int => try sema.zirErrorToInt(block, inst),
191 .int_to_error => try sema.zirIntToError(block, inst),
192 .field_ptr => try sema.zirFieldPtr(block, inst),
193 .field_ptr_named => try sema.zirFieldPtrNamed(block, inst),
194 .field_val => try sema.zirFieldVal(block, inst),
195 .field_val_named => try sema.zirFieldValNamed(block, inst),
196 .floatcast => try sema.zirFloatcast(block, inst),
197 .func => try sema.zirFunc(block, inst, false),
198 .func_extra => try sema.zirFuncExtra(block, inst, false),
199 .func_extra_var_args => try sema.zirFuncExtra(block, inst, true),
200 .func_var_args => try sema.zirFunc(block, inst, true),
201 .has_decl => try sema.zirHasDecl(block, inst),
202 .import => try sema.zirImport(block, inst),
203 .indexable_ptr_len => try sema.zirIndexablePtrLen(block, inst),
204 .int => try sema.zirInt(block, inst),
205 .float => try sema.zirFloat(block, inst),
206 .float128 => try sema.zirFloat128(block, inst),
207 .int_type => try sema.zirIntType(block, inst),
208 .intcast => try sema.zirIntcast(block, inst),
209 .is_err => try sema.zirIsErr(block, inst),
210 .is_err_ptr => try sema.zirIsErrPtr(block, inst),
211 .is_non_null => try sema.zirIsNull(block, inst, true),
212 .is_non_null_ptr => try sema.zirIsNullPtr(block, inst, true),
213 .is_null => try sema.zirIsNull(block, inst, false),
214 .is_null_ptr => try sema.zirIsNullPtr(block, inst, false),
215 .loop => try sema.zirLoop(block, inst),
216 .merge_error_sets => try sema.zirMergeErrorSets(block, inst),
217 .negate => try sema.zirNegate(block, inst, .sub),
218 .negate_wrap => try sema.zirNegate(block, inst, .subwrap),
219 .optional_payload_safe => try sema.zirOptionalPayload(block, inst, true),
220 .optional_payload_safe_ptr => try sema.zirOptionalPayloadPtr(block, inst, true),
221 .optional_payload_unsafe => try sema.zirOptionalPayload(block, inst, false),
222 .optional_payload_unsafe_ptr => try sema.zirOptionalPayloadPtr(block, inst, false),
223 .optional_type => try sema.zirOptionalType(block, inst),
224 .optional_type_from_ptr_elem => try sema.zirOptionalTypeFromPtrElem(block, inst),
225 .param_type => try sema.zirParamType(block, inst),
226 .ptr_type => try sema.zirPtrType(block, inst),
227 .ptr_type_simple => try sema.zirPtrTypeSimple(block, inst),
228 .ptrtoint => try sema.zirPtrtoint(block, inst),
229 .ref => try sema.zirRef(block, inst),
230 .ret_ptr => try sema.zirRetPtr(block, inst),
231 .ret_type => try sema.zirRetType(block, inst),
232 .shl => try sema.zirShl(block, inst),
233 .shr => try sema.zirShr(block, inst),
234 .slice_end => try sema.zirSliceEnd(block, inst),
235 .slice_sentinel => try sema.zirSliceSentinel(block, inst),
236 .slice_start => try sema.zirSliceStart(block, inst),
237 .str => try sema.zirStr(block, inst),
238 .switch_block => try sema.zirSwitchBlock(block, inst, false, .none),
239 .switch_block_multi => try sema.zirSwitchBlockMulti(block, inst, false, .none),
240 .switch_block_else => try sema.zirSwitchBlock(block, inst, false, .@"else"),
241 .switch_block_else_multi => try sema.zirSwitchBlockMulti(block, inst, false, .@"else"),
242 .switch_block_under => try sema.zirSwitchBlock(block, inst, false, .under),
243 .switch_block_under_multi => try sema.zirSwitchBlockMulti(block, inst, false, .under),
244 .switch_block_ref => try sema.zirSwitchBlock(block, inst, true, .none),
245 .switch_block_ref_multi => try sema.zirSwitchBlockMulti(block, inst, true, .none),
246 .switch_block_ref_else => try sema.zirSwitchBlock(block, inst, true, .@"else"),
247 .switch_block_ref_else_multi => try sema.zirSwitchBlockMulti(block, inst, true, .@"else"),
248 .switch_block_ref_under => try sema.zirSwitchBlock(block, inst, true, .under),
188 .error_union_type => try sema.zirErrorUnionType(block, inst),
189 .error_value => try sema.zirErrorValue(block, inst),
190 .error_to_int => try sema.zirErrorToInt(block, inst),
191 .int_to_error => try sema.zirIntToError(block, inst),
192 .field_ptr => try sema.zirFieldPtr(block, inst),
193 .field_ptr_named => try sema.zirFieldPtrNamed(block, inst),
194 .field_val => try sema.zirFieldVal(block, inst),
195 .field_val_named => try sema.zirFieldValNamed(block, inst),
196 .floatcast => try sema.zirFloatcast(block, inst),
197 .func => try sema.zirFunc(block, inst, false),
198 .func_extra => try sema.zirFuncExtra(block, inst, false),
199 .func_extra_var_args => try sema.zirFuncExtra(block, inst, true),
200 .func_var_args => try sema.zirFunc(block, inst, true),
201 .import => try sema.zirImport(block, inst),
202 .indexable_ptr_len => try sema.zirIndexablePtrLen(block, inst),
203 .int => try sema.zirInt(block, inst),
204 .float => try sema.zirFloat(block, inst),
205 .float128 => try sema.zirFloat128(block, inst),
206 .int_type => try sema.zirIntType(block, inst),
207 .intcast => try sema.zirIntcast(block, inst),
208 .is_err => try sema.zirIsErr(block, inst),
209 .is_err_ptr => try sema.zirIsErrPtr(block, inst),
210 .is_non_null => try sema.zirIsNull(block, inst, true),
211 .is_non_null_ptr => try sema.zirIsNullPtr(block, inst, true),
212 .is_null => try sema.zirIsNull(block, inst, false),
213 .is_null_ptr => try sema.zirIsNullPtr(block, inst, false),
214 .loop => try sema.zirLoop(block, inst),
215 .merge_error_sets => try sema.zirMergeErrorSets(block, inst),
216 .negate => try sema.zirNegate(block, inst, .sub),
217 .negate_wrap => try sema.zirNegate(block, inst, .subwrap),
218 .optional_payload_safe => try sema.zirOptionalPayload(block, inst, true),
219 .optional_payload_safe_ptr => try sema.zirOptionalPayloadPtr(block, inst, true),
220 .optional_payload_unsafe => try sema.zirOptionalPayload(block, inst, false),
221 .optional_payload_unsafe_ptr => try sema.zirOptionalPayloadPtr(block, inst, false),
222 .optional_type => try sema.zirOptionalType(block, inst),
223 .optional_type_from_ptr_elem => try sema.zirOptionalTypeFromPtrElem(block, inst),
224 .param_type => try sema.zirParamType(block, inst),
225 .ptr_type => try sema.zirPtrType(block, inst),
226 .ptr_type_simple => try sema.zirPtrTypeSimple(block, inst),
227 .ptrtoint => try sema.zirPtrtoint(block, inst),
228 .ref => try sema.zirRef(block, inst),
229 .ret_ptr => try sema.zirRetPtr(block, inst),
230 .ret_type => try sema.zirRetType(block, inst),
231 .shl => try sema.zirShl(block, inst),
232 .shr => try sema.zirShr(block, inst),
233 .slice_end => try sema.zirSliceEnd(block, inst),
234 .slice_sentinel => try sema.zirSliceSentinel(block, inst),
235 .slice_start => try sema.zirSliceStart(block, inst),
236 .str => try sema.zirStr(block, inst),
237 .switch_block => try sema.zirSwitchBlock(block, inst, false, .none),
238 .switch_block_multi => try sema.zirSwitchBlockMulti(block, inst, false, .none),
239 .switch_block_else => try sema.zirSwitchBlock(block, inst, false, .@"else"),
240 .switch_block_else_multi => try sema.zirSwitchBlockMulti(block, inst, false, .@"else"),
241 .switch_block_under => try sema.zirSwitchBlock(block, inst, false, .under),
242 .switch_block_under_multi => try sema.zirSwitchBlockMulti(block, inst, false, .under),
243 .switch_block_ref => try sema.zirSwitchBlock(block, inst, true, .none),
244 .switch_block_ref_multi => try sema.zirSwitchBlockMulti(block, inst, true, .none),
245 .switch_block_ref_else => try sema.zirSwitchBlock(block, inst, true, .@"else"),
246 .switch_block_ref_else_multi => try sema.zirSwitchBlockMulti(block, inst, true, .@"else"),
247 .switch_block_ref_under => try sema.zirSwitchBlock(block, inst, true, .under),
249248 .switch_block_ref_under_multi => try sema.zirSwitchBlockMulti(block, inst, true, .under),
250 .switch_capture => try sema.zirSwitchCapture(block, inst, false, false),
251 .switch_capture_ref => try sema.zirSwitchCapture(block, inst, false, true),
252 .switch_capture_multi => try sema.zirSwitchCapture(block, inst, true, false),
253 .switch_capture_multi_ref => try sema.zirSwitchCapture(block, inst, true, true),
254 .switch_capture_else => try sema.zirSwitchCaptureElse(block, inst, false),
255 .switch_capture_else_ref => try sema.zirSwitchCaptureElse(block, inst, true),
256 .type_info => try sema.zirTypeInfo(block, inst),
257 .size_of => try sema.zirSizeOf(block, inst),
258 .bit_size_of => try sema.zirBitSizeOf(block, inst),
259 .this => try sema.zirThis(block, inst),
260 .fence => try sema.zirFence(block, inst),
261 .ret_addr => try sema.zirRetAddr(block, inst),
262 .builtin_src => try sema.zirBuiltinSrc(block, inst),
263 .typeof => try sema.zirTypeof(block, inst),
264 .typeof_elem => try sema.zirTypeofElem(block, inst),
265 .typeof_peer => try sema.zirTypeofPeer(block, inst),
266 .log2_int_type => try sema.zirLog2IntType(block, inst),
267 .xor => try sema.zirBitwise(block, inst, .xor),
268 .struct_init_empty => try sema.zirStructInitEmpty(block, inst),
269 .struct_init => try sema.zirStructInit(block, inst),
270 .field_type => try sema.zirFieldType(block, inst),
271
272 .struct_decl => try sema.zirStructDecl(block, inst, .Auto),
273 .struct_decl_packed => try sema.zirStructDecl(block, inst, .Packed),
274 .struct_decl_extern => try sema.zirStructDecl(block, inst, .Extern),
275 .enum_decl => try sema.zirEnumDecl(block, inst, false),
249 .switch_capture => try sema.zirSwitchCapture(block, inst, false, false),
250 .switch_capture_ref => try sema.zirSwitchCapture(block, inst, false, true),
251 .switch_capture_multi => try sema.zirSwitchCapture(block, inst, true, false),
252 .switch_capture_multi_ref => try sema.zirSwitchCapture(block, inst, true, true),
253 .switch_capture_else => try sema.zirSwitchCaptureElse(block, inst, false),
254 .switch_capture_else_ref => try sema.zirSwitchCaptureElse(block, inst, true),
255 .type_info => try sema.zirTypeInfo(block, inst),
256 .size_of => try sema.zirSizeOf(block, inst),
257 .bit_size_of => try sema.zirBitSizeOf(block, inst),
258 .this => try sema.zirThis(block, inst),
259 .ret_addr => try sema.zirRetAddr(block, inst),
260 .builtin_src => try sema.zirBuiltinSrc(block, inst),
261 .typeof => try sema.zirTypeof(block, inst),
262 .typeof_elem => try sema.zirTypeofElem(block, inst),
263 .typeof_peer => try sema.zirTypeofPeer(block, inst),
264 .log2_int_type => try sema.zirLog2IntType(block, inst),
265 .typeof_log2_int_type => try sema.zirTypeofLog2IntType(block, inst),
266 .xor => try sema.zirBitwise(block, inst, .xor),
267 .struct_init_empty => try sema.zirStructInitEmpty(block, inst),
268 .struct_init => try sema.zirStructInit(block, inst),
269 .union_init_ptr => try sema.zirUnionInitPtr(block, inst),
270 .field_type => try sema.zirFieldType(block, inst),
271 .error_return_trace => try sema.zirErrorReturnTrace(block, inst),
272 .frame => try sema.zirFrame(block, inst),
273 .frame_address => try sema.zirFrameAddress(block, inst),
274 .ptr_to_int => try sema.zirPtrToInt(block, inst),
275 .align_of => try sema.zirAlignOf(block, inst),
276 .bool_to_int => try sema.zirBoolToInt(block, inst),
277 .embed_file => try sema.zirEmbedFile(block, inst),
278 .error_name => try sema.zirErrorName(block, inst),
279 .tag_name => try sema.zirTagName(block, inst),
280 .reify => try sema.zirReify(block, inst),
281 .type_name => try sema.zirTypeName(block, inst),
282 .frame_type => try sema.zirFrameType(block, inst),
283 .frame_size => try sema.zirFrameSize(block, inst),
284 .float_to_int => try sema.zirFloatToInt(block, inst),
285 .int_to_float => try sema.zirIntToFloat(block, inst),
286 .int_to_ptr => try sema.zirIntToPtr(block, inst),
287 .float_cast => try sema.zirFloatCast(block, inst),
288 .int_cast => try sema.zirIntCast(block, inst),
289 .err_set_cast => try sema.zirErrSetCast(block, inst),
290 .ptr_cast => try sema.zirPtrCast(block, inst),
291 .truncate => try sema.zirTruncate(block, inst),
292 .align_cast => try sema.zirAlignCast(block, inst),
293 .has_decl => try sema.zirHasDecl(block, inst),
294 .has_field => try sema.zirHasField(block, inst),
295 .clz => try sema.zirClz(block, inst),
296 .ctz => try sema.zirCtz(block, inst),
297 .pop_count => try sema.zirPopCount(block, inst),
298 .byte_swap => try sema.zirByteSwap(block, inst),
299 .bit_reverse => try sema.zirBitReverse(block, inst),
300 .div_exact => try sema.zirDivExact(block, inst),
301 .div_floor => try sema.zirDivFloor(block, inst),
302 .div_trunc => try sema.zirDivTrunc(block, inst),
303 .mod => try sema.zirMod(block, inst),
304 .rem => try sema.zirRem(block, inst),
305 .shl_exact => try sema.zirShlExact(block, inst),
306 .shr_exact => try sema.zirShrExact(block, inst),
307 .bit_offset_of => try sema.zirBitOffsetOf(block, inst),
308 .byte_offset_of => try sema.zirByteOffsetOf(block, inst),
309 .cmpxchg_strong => try sema.zirCmpxchg(block, inst),
310 .cmpxchg_weak => try sema.zirCmpxchg(block, inst),
311 .splat => try sema.zirSplat(block, inst),
312 .reduce => try sema.zirReduce(block, inst),
313 .shuffle => try sema.zirShuffle(block, inst),
314 .atomic_load => try sema.zirAtomicLoad(block, inst),
315 .atomic_rmw => try sema.zirAtomicRmw(block, inst),
316 .atomic_store => try sema.zirAtomicStore(block, inst),
317 .mul_add => try sema.zirMulAdd(block, inst),
318 .builtin_call => try sema.zirBuiltinCall(block, inst),
319 .field_ptr_type => try sema.zirFieldPtrType(block, inst),
320 .field_parent_ptr => try sema.zirFieldParentPtr(block, inst),
321 .memcpy => try sema.zirMemcpy(block, inst),
322 .memset => try sema.zirMemset(block, inst),
323 .builtin_async_call => try sema.zirBuiltinAsyncCall(block, inst),
324 .extended => try sema.zirExtended(block, inst),
325
326 .sqrt => try sema.zirUnaryMath(block, inst),
327 .sin => try sema.zirUnaryMath(block, inst),
328 .cos => try sema.zirUnaryMath(block, inst),
329 .exp => try sema.zirUnaryMath(block, inst),
330 .exp2 => try sema.zirUnaryMath(block, inst),
331 .log => try sema.zirUnaryMath(block, inst),
332 .log2 => try sema.zirUnaryMath(block, inst),
333 .log10 => try sema.zirUnaryMath(block, inst),
334 .fabs => try sema.zirUnaryMath(block, inst),
335 .floor => try sema.zirUnaryMath(block, inst),
336 .ceil => try sema.zirUnaryMath(block, inst),
337 .trunc => try sema.zirUnaryMath(block, inst),
338 .round => try sema.zirUnaryMath(block, inst),
339
340 .struct_decl => try sema.zirStructDecl(block, inst, .Auto),
341 .struct_decl_packed => try sema.zirStructDecl(block, inst, .Packed),
342 .struct_decl_extern => try sema.zirStructDecl(block, inst, .Extern),
343 .enum_decl => try sema.zirEnumDecl(block, inst, false),
276344 .enum_decl_nonexhaustive => try sema.zirEnumDecl(block, inst, true),
277 .union_decl => try sema.zirUnionDecl(block, inst),
278 .opaque_decl => try sema.zirOpaqueDecl(block, inst),
345 .union_decl => try sema.zirUnionDecl(block, inst),
346 .opaque_decl => try sema.zirOpaqueDecl(block, inst),
279347
280 .add => try sema.zirArithmetic(block, inst),
348 .add => try sema.zirArithmetic(block, inst),
281349 .addwrap => try sema.zirArithmetic(block, inst),
282 .div => try sema.zirArithmetic(block, inst),
350 .div => try sema.zirArithmetic(block, inst),
283351 .mod_rem => try sema.zirArithmetic(block, inst),
284 .mul => try sema.zirArithmetic(block, inst),
352 .mul => try sema.zirArithmetic(block, inst),
285353 .mulwrap => try sema.zirArithmetic(block, inst),
286 .sub => try sema.zirArithmetic(block, inst),
354 .sub => try sema.zirArithmetic(block, inst),
287355 .subwrap => try sema.zirArithmetic(block, inst),
288356
289357 .add_with_overflow => try sema.zirOverflowArithmetic(block, inst),
......@@ -294,15 +362,17 @@ pub fn analyzeBody(
294362 // Instructions that we know to *always* be noreturn based solely on their tag.
295363 // These functions match the return type of analyzeBody so that we can
296364 // tail call them here.
297 .condbr => return sema.zirCondbr(block, inst),
298 .@"break" => return sema.zirBreak(block, inst),
299 .break_inline => return inst,
300 .compile_error => return sema.zirCompileError(block, inst),
301 .ret_coerce => return sema.zirRetTok(block, inst, true),
302 .ret_node => return sema.zirRetNode(block, inst),
303 .ret_tok => return sema.zirRetTok(block, inst, false),
365 .break_inline => return inst,
366 .condbr => return sema.zirCondbr(block, inst),
367 .@"break" => return sema.zirBreak(block, inst),
368 .compile_error => return sema.zirCompileError(block, inst),
369 .ret_coerce => return sema.zirRetTok(block, inst, true),
370 .ret_node => return sema.zirRetNode(block, inst),
371 .ret_tok => return sema.zirRetTok(block, inst, false),
304372 .@"unreachable" => return sema.zirUnreachable(block, inst),
305 .repeat => return sema.zirRepeat(block, inst),
373 .repeat => return sema.zirRepeat(block, inst),
374 .panic => return sema.zirPanic(block, inst),
375 // zig fmt: on
306376
307377 // Instructions that we know can *never* be noreturn based solely on
308378 // their tag. We avoid needlessly checking if they are noreturn and
......@@ -313,6 +383,10 @@ pub fn analyzeBody(
313383 try sema.zirBreakpoint(block, inst);
314384 continue;
315385 },
386 .fence => {
387 try sema.zirFence(block, inst);
388 continue;
389 },
316390 .dbg_stmt_node => {
317391 try sema.zirDbgStmtNode(block, inst);
318392 continue;
......@@ -365,6 +439,22 @@ pub fn analyzeBody(
365439 try sema.zirExport(block, inst);
366440 continue;
367441 },
442 .set_align_stack => {
443 try sema.zirSetAlignStack(block, inst);
444 continue;
445 },
446 .set_cold => {
447 try sema.zirSetAlignStack(block, inst);
448 continue;
449 },
450 .set_float_mode => {
451 try sema.zirSetFloatMode(block, inst);
452 continue;
453 },
454 .set_runtime_safety => {
455 try sema.zirSetRuntimeSafety(block, inst);
456 continue;
457 },
368458
369459 // Special case instructions to handle comptime control flow.
370460 .repeat_inline => {
......@@ -1382,6 +1472,13 @@ fn zirRepeat(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) InnerError!
13821472 return always_noreturn;
13831473}
13841474
1475fn zirPanic(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) InnerError!Zir.Inst.Index {
1476 const inst_data = sema.code.instructions.items(.data)[inst].un_node;
1477 const src: LazySrcLoc = inst_data.src();
1478 return sema.mod.fail(&block.base, src, "TODO: implement Sema.zirPanic", .{});
1479 //return always_noreturn;
1480}
1481
13851482fn zirLoop(sema: *Sema, parent_block: *Scope.Block, inst: Zir.Inst.Index) InnerError!*Inst {
13861483 const tracy = trace(@src());
13871484 defer tracy.end();
......@@ -1443,6 +1540,16 @@ fn zirLoop(sema: *Sema, parent_block: *Scope.Block, inst: Zir.Inst.Index) InnerE
14431540 return sema.analyzeBlockBody(parent_block, src, &child_block, merges);
14441541}
14451542
1543fn zirCImport(sema: *Sema, parent_block: *Scope.Block, inst: Zir.Inst.Index) InnerError!*Inst {
1544 const tracy = trace(@src());
1545 defer tracy.end();
1546
1547 const inst_data = sema.code.instructions.items(.data)[inst].pl_node;
1548 const src = inst_data.src();
1549
1550 return sema.mod.fail(&parent_block.base, src, "TODO: implement Sema.zirCImport", .{});
1551}
1552
14461553fn zirBlock(sema: *Sema, parent_block: *Scope.Block, inst: Zir.Inst.Index) InnerError!*Inst {
14471554 const tracy = trace(@src());
14481555 defer tracy.end();
......@@ -1597,6 +1704,30 @@ fn zirExport(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) InnerError!
15971704 try sema.mod.analyzeExport(&block.base, src, export_name, actual_fn.owner_decl);
15981705}
15991706
1707fn zirSetAlignStack(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) InnerError!void {
1708 const inst_data = sema.code.instructions.items(.data)[inst].un_node;
1709 const src: LazySrcLoc = inst_data.src();
1710 return sema.mod.fail(&block.base, src, "TODO: implement Sema.zirSetAlignStack", .{});
1711}
1712
1713fn zirSetCold(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) InnerError!void {
1714 const inst_data = sema.code.instructions.items(.data)[inst].un_node;
1715 const src: LazySrcLoc = inst_data.src();
1716 return sema.mod.fail(&block.base, src, "TODO: implement Sema.zirSetCold", .{});
1717}
1718
1719fn zirSetFloatMode(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) InnerError!void {
1720 const inst_data = sema.code.instructions.items(.data)[inst].un_node;
1721 const src: LazySrcLoc = inst_data.src();
1722 return sema.mod.fail(&block.base, src, "TODO: implement Sema.zirSetFloatMode", .{});
1723}
1724
1725fn zirSetRuntimeSafety(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) InnerError!void {
1726 const inst_data = sema.code.instructions.items(.data)[inst].un_node;
1727 const src: LazySrcLoc = inst_data.src();
1728 return sema.mod.fail(&block.base, src, "TODO: implement Sema.zirSetRuntimeSafety", .{});
1729}
1730
16001731fn zirBreakpoint(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) InnerError!void {
16011732 const tracy = trace(@src());
16021733 defer tracy.end();
......@@ -1607,6 +1738,12 @@ fn zirBreakpoint(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) InnerEr
16071738 _ = try block.addNoOp(src, Type.initTag(.void), .breakpoint);
16081739}
16091740
1741fn zirFence(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) InnerError!void {
1742 const src_node = sema.code.instructions.items(.data)[inst].node;
1743 const src: LazySrcLoc = .{ .node_offset = src_node };
1744 return sema.mod.fail(&block.base, src, "TODO: implement Sema.zirFence", .{});
1745}
1746
16101747fn zirBreak(sema: *Sema, start_block: *Scope.Block, inst: Zir.Inst.Index) InnerError!Zir.Inst.Index {
16111748 const tracy = trace(@src());
16121749 defer tracy.end();
......@@ -3874,10 +4011,15 @@ fn validateSwitchNoRange(
38744011 return sema.mod.failWithOwnedErrorMsg(&block.base, msg);
38754012}
38764013
3877fn zirHasDecl(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) InnerError!*Inst {
3878 const tracy = trace(@src());
3879 defer tracy.end();
4014fn zirHasField(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) InnerError!*Inst {
4015 const inst_data = sema.code.instructions.items(.data)[inst].pl_node;
4016 const extra = sema.code.extraData(Zir.Inst.Bin, inst_data.payload_index).data;
4017 const src = inst_data.src();
4018
4019 return sema.mod.fail(&block.base, src, "TODO implement zirHasField", .{});
4020}
38804021
4022fn zirHasDecl(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) InnerError!*Inst {
38814023 const inst_data = sema.code.instructions.items(.data)[inst].pl_node;
38824024 const extra = sema.code.extraData(Zir.Inst.Bin, inst_data.payload_index).data;
38834025 const src = inst_data.src();
......@@ -4382,16 +4524,13 @@ fn zirThis(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) InnerError!*I
43824524 const src: LazySrcLoc = .{ .node_offset = src_node };
43834525 return sema.mod.fail(&block.base, src, "TODO: implement Sema.zirThis", .{});
43844526}
4385fn zirFence(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) InnerError!*Inst {
4386 const src_node = sema.code.instructions.items(.data)[inst].node;
4387 const src: LazySrcLoc = .{ .node_offset = src_node };
4388 return sema.mod.fail(&block.base, src, "TODO: implement Sema.zirFence", .{});
4389}
4527
43904528fn zirRetAddr(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) InnerError!*Inst {
43914529 const src_node = sema.code.instructions.items(.data)[inst].node;
43924530 const src: LazySrcLoc = .{ .node_offset = src_node };
43934531 return sema.mod.fail(&block.base, src, "TODO: implement Sema.zirRetAddr", .{});
43944532}
4533
43954534fn zirBuiltinSrc(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) InnerError!*Inst {
43964535 const src_node = sema.code.instructions.items(.data)[inst].node;
43974536 const src: LazySrcLoc = .{ .node_offset = src_node };
......@@ -4419,6 +4558,12 @@ fn zirTypeofElem(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) InnerEr
44194558 return sema.mod.constType(sema.arena, src, elem_ty);
44204559}
44214560
4561fn zirTypeofLog2IntType(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) InnerError!*Inst {
4562 const inst_data = sema.code.instructions.items(.data)[inst].un_node;
4563 const src = inst_data.src();
4564 return sema.mod.fail(&block.base, src, "TODO: implement Sema.zirTypeofLog2IntType", .{});
4565}
4566
44224567fn zirLog2IntType(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) InnerError!*Inst {
44234568 const inst_data = sema.code.instructions.items(.data)[inst].un_node;
44244569 const src = inst_data.src();
......@@ -4827,6 +4972,12 @@ fn zirStructInitEmpty(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) In
48274972 });
48284973}
48294974
4975fn zirUnionInitPtr(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) InnerError!*Inst {
4976 const inst_data = sema.code.instructions.items(.data)[inst].pl_node;
4977 const src = inst_data.src();
4978 return sema.mod.fail(&block.base, src, "TODO: Sema.zirUnionInitPtr", .{});
4979}
4980
48304981fn zirStructInit(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) InnerError!*Inst {
48314982 const inst_data = sema.code.instructions.items(.data)[inst].pl_node;
48324983 const src = inst_data.src();
......@@ -4839,6 +4990,380 @@ fn zirFieldType(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) InnerErr
48394990 return sema.mod.fail(&block.base, src, "TODO: Sema.zirFieldType", .{});
48404991}
48414992
4993fn zirErrorReturnTrace(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) InnerError!*Inst {
4994 const inst_data = sema.code.instructions.items(.data)[inst].un_node;
4995 const src = inst_data.src();
4996 return sema.mod.fail(&block.base, src, "TODO: Sema.zirErrorReturnTrace", .{});
4997}
4998
4999fn zirFrame(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) InnerError!*Inst {
5000 const inst_data = sema.code.instructions.items(.data)[inst].un_node;
5001 const src = inst_data.src();
5002 return sema.mod.fail(&block.base, src, "TODO: Sema.zirFrame", .{});
5003}
5004
5005fn zirFrameAddress(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) InnerError!*Inst {
5006 const inst_data = sema.code.instructions.items(.data)[inst].un_node;
5007 const src = inst_data.src();
5008 return sema.mod.fail(&block.base, src, "TODO: Sema.zirFrameAddress", .{});
5009}
5010
5011fn zirPtrToInt(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) InnerError!*Inst {
5012 const inst_data = sema.code.instructions.items(.data)[inst].un_node;
5013 const src = inst_data.src();
5014 return sema.mod.fail(&block.base, src, "TODO: Sema.zirPtrToInt", .{});
5015}
5016
5017fn zirAlignOf(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) InnerError!*Inst {
5018 const inst_data = sema.code.instructions.items(.data)[inst].un_node;
5019 const src = inst_data.src();
5020 return sema.mod.fail(&block.base, src, "TODO: Sema.zirAlignOf", .{});
5021}
5022
5023fn zirBoolToInt(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) InnerError!*Inst {
5024 const inst_data = sema.code.instructions.items(.data)[inst].un_node;
5025 const src = inst_data.src();
5026 return sema.mod.fail(&block.base, src, "TODO: Sema.zirBoolToInt", .{});
5027}
5028
5029fn zirEmbedFile(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) InnerError!*Inst {
5030 const inst_data = sema.code.instructions.items(.data)[inst].un_node;
5031 const src = inst_data.src();
5032 return sema.mod.fail(&block.base, src, "TODO: Sema.zirEmbedFile", .{});
5033}
5034
5035fn zirErrorName(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) InnerError!*Inst {
5036 const inst_data = sema.code.instructions.items(.data)[inst].un_node;
5037 const src = inst_data.src();
5038 return sema.mod.fail(&block.base, src, "TODO: Sema.zirErrorName", .{});
5039}
5040
5041fn zirUnaryMath(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) InnerError!*Inst {
5042 const inst_data = sema.code.instructions.items(.data)[inst].un_node;
5043 const src = inst_data.src();
5044 return sema.mod.fail(&block.base, src, "TODO: Sema.zirUnaryMath", .{});
5045}
5046
5047fn zirTagName(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) InnerError!*Inst {
5048 const inst_data = sema.code.instructions.items(.data)[inst].un_node;
5049 const src = inst_data.src();
5050 return sema.mod.fail(&block.base, src, "TODO: Sema.zirTagName", .{});
5051}
5052
5053fn zirReify(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) InnerError!*Inst {
5054 const inst_data = sema.code.instructions.items(.data)[inst].un_node;
5055 const src = inst_data.src();
5056 return sema.mod.fail(&block.base, src, "TODO: Sema.zirReify", .{});
5057}
5058
5059fn zirTypeName(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) InnerError!*Inst {
5060 const inst_data = sema.code.instructions.items(.data)[inst].un_node;
5061 const src = inst_data.src();
5062 return sema.mod.fail(&block.base, src, "TODO: Sema.zirTypeName", .{});
5063}
5064
5065fn zirFrameType(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) InnerError!*Inst {
5066 const inst_data = sema.code.instructions.items(.data)[inst].un_node;
5067 const src = inst_data.src();
5068 return sema.mod.fail(&block.base, src, "TODO: Sema.zirFrameType", .{});
5069}
5070
5071fn zirFrameSize(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) InnerError!*Inst {
5072 const inst_data = sema.code.instructions.items(.data)[inst].un_node;
5073 const src = inst_data.src();
5074 return sema.mod.fail(&block.base, src, "TODO: Sema.zirFrameSize", .{});
5075}
5076
5077fn zirFloatToInt(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) InnerError!*Inst {
5078 const inst_data = sema.code.instructions.items(.data)[inst].pl_node;
5079 const src = inst_data.src();
5080 return sema.mod.fail(&block.base, src, "TODO: Sema.zirFloatToInt", .{});
5081}
5082
5083fn zirIntToFloat(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) InnerError!*Inst {
5084 const inst_data = sema.code.instructions.items(.data)[inst].pl_node;
5085 const src = inst_data.src();
5086 return sema.mod.fail(&block.base, src, "TODO: Sema.zirIntToFloat", .{});
5087}
5088
5089fn zirIntToPtr(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) InnerError!*Inst {
5090 const inst_data = sema.code.instructions.items(.data)[inst].pl_node;
5091 const src = inst_data.src();
5092 return sema.mod.fail(&block.base, src, "TODO: Sema.zirIntToPtr", .{});
5093}
5094
5095fn zirFloatCast(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) InnerError!*Inst {
5096 const inst_data = sema.code.instructions.items(.data)[inst].pl_node;
5097 const src = inst_data.src();
5098 return sema.mod.fail(&block.base, src, "TODO: Sema.zirFloatCast", .{});
5099}
5100
5101fn zirIntCast(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) InnerError!*Inst {
5102 const inst_data = sema.code.instructions.items(.data)[inst].pl_node;
5103 const src = inst_data.src();
5104 return sema.mod.fail(&block.base, src, "TODO: Sema.zirIntCast", .{});
5105}
5106
5107fn zirErrSetCast(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) InnerError!*Inst {
5108 const inst_data = sema.code.instructions.items(.data)[inst].pl_node;
5109 const src = inst_data.src();
5110 return sema.mod.fail(&block.base, src, "TODO: Sema.zirErrSetCast", .{});
5111}
5112
5113fn zirPtrCast(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) InnerError!*Inst {
5114 const inst_data = sema.code.instructions.items(.data)[inst].pl_node;
5115 const src = inst_data.src();
5116 return sema.mod.fail(&block.base, src, "TODO: Sema.zirPtrCast", .{});
5117}
5118
5119fn zirTruncate(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) InnerError!*Inst {
5120 const inst_data = sema.code.instructions.items(.data)[inst].pl_node;
5121 const src = inst_data.src();
5122 return sema.mod.fail(&block.base, src, "TODO: Sema.zirTruncate", .{});
5123}
5124
5125fn zirAlignCast(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) InnerError!*Inst {
5126 const inst_data = sema.code.instructions.items(.data)[inst].pl_node;
5127 const src = inst_data.src();
5128 return sema.mod.fail(&block.base, src, "TODO: Sema.zirAlignCast", .{});
5129}
5130
5131fn zirClz(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) InnerError!*Inst {
5132 const inst_data = sema.code.instructions.items(.data)[inst].un_node;
5133 const src = inst_data.src();
5134 return sema.mod.fail(&block.base, src, "TODO: Sema.zirClz", .{});
5135}
5136
5137fn zirCtz(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) InnerError!*Inst {
5138 const inst_data = sema.code.instructions.items(.data)[inst].un_node;
5139 const src = inst_data.src();
5140 return sema.mod.fail(&block.base, src, "TODO: Sema.zirCtz", .{});
5141}
5142
5143fn zirPopCount(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) InnerError!*Inst {
5144 const inst_data = sema.code.instructions.items(.data)[inst].un_node;
5145 const src = inst_data.src();
5146 return sema.mod.fail(&block.base, src, "TODO: Sema.zirPopCount", .{});
5147}
5148
5149fn zirByteSwap(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) InnerError!*Inst {
5150 const inst_data = sema.code.instructions.items(.data)[inst].un_node;
5151 const src = inst_data.src();
5152 return sema.mod.fail(&block.base, src, "TODO: Sema.zirByteSwap", .{});
5153}
5154
5155fn zirBitReverse(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) InnerError!*Inst {
5156 const inst_data = sema.code.instructions.items(.data)[inst].un_node;
5157 const src = inst_data.src();
5158 return sema.mod.fail(&block.base, src, "TODO: Sema.zirBitReverse", .{});
5159}
5160
5161fn zirDivExact(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) InnerError!*Inst {
5162 const inst_data = sema.code.instructions.items(.data)[inst].pl_node;
5163 const src = inst_data.src();
5164 return sema.mod.fail(&block.base, src, "TODO: Sema.zirDivExact", .{});
5165}
5166
5167fn zirDivFloor(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) InnerError!*Inst {
5168 const inst_data = sema.code.instructions.items(.data)[inst].pl_node;
5169 const src = inst_data.src();
5170 return sema.mod.fail(&block.base, src, "TODO: Sema.zirDivFloor", .{});
5171}
5172
5173fn zirDivTrunc(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) InnerError!*Inst {
5174 const inst_data = sema.code.instructions.items(.data)[inst].pl_node;
5175 const src = inst_data.src();
5176 return sema.mod.fail(&block.base, src, "TODO: Sema.zirDivTrunc", .{});
5177}
5178
5179fn zirMod(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) InnerError!*Inst {
5180 const inst_data = sema.code.instructions.items(.data)[inst].pl_node;
5181 const src = inst_data.src();
5182 return sema.mod.fail(&block.base, src, "TODO: Sema.zirMod", .{});
5183}
5184
5185fn zirRem(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) InnerError!*Inst {
5186 const inst_data = sema.code.instructions.items(.data)[inst].pl_node;
5187 const src = inst_data.src();
5188 return sema.mod.fail(&block.base, src, "TODO: Sema.zirRem", .{});
5189}
5190
5191fn zirShlExact(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) InnerError!*Inst {
5192 const inst_data = sema.code.instructions.items(.data)[inst].pl_node;
5193 const src = inst_data.src();
5194 return sema.mod.fail(&block.base, src, "TODO: Sema.zirShlExact", .{});
5195}
5196
5197fn zirShrExact(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) InnerError!*Inst {
5198 const inst_data = sema.code.instructions.items(.data)[inst].pl_node;
5199 const src = inst_data.src();
5200 return sema.mod.fail(&block.base, src, "TODO: Sema.zirShrExact", .{});
5201}
5202
5203fn zirBitOffsetOf(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) InnerError!*Inst {
5204 const inst_data = sema.code.instructions.items(.data)[inst].pl_node;
5205 const src = inst_data.src();
5206 return sema.mod.fail(&block.base, src, "TODO: Sema.zirBitOffsetOf", .{});
5207}
5208
5209fn zirByteOffsetOf(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) InnerError!*Inst {
5210 const inst_data = sema.code.instructions.items(.data)[inst].pl_node;
5211 const src = inst_data.src();
5212 return sema.mod.fail(&block.base, src, "TODO: Sema.zirByteOffsetOf", .{});
5213}
5214
5215fn zirCmpxchg(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) InnerError!*Inst {
5216 const inst_data = sema.code.instructions.items(.data)[inst].pl_node;
5217 const src = inst_data.src();
5218 return sema.mod.fail(&block.base, src, "TODO: Sema.zirCmpxchg", .{});
5219}
5220
5221fn zirSplat(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) InnerError!*Inst {
5222 const inst_data = sema.code.instructions.items(.data)[inst].pl_node;
5223 const src = inst_data.src();
5224 return sema.mod.fail(&block.base, src, "TODO: Sema.zirSplat", .{});
5225}
5226
5227fn zirReduce(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) InnerError!*Inst {
5228 const inst_data = sema.code.instructions.items(.data)[inst].pl_node;
5229 const src = inst_data.src();
5230 return sema.mod.fail(&block.base, src, "TODO: Sema.zirReduce", .{});
5231}
5232
5233fn zirShuffle(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) InnerError!*Inst {
5234 const inst_data = sema.code.instructions.items(.data)[inst].pl_node;
5235 const src = inst_data.src();
5236 return sema.mod.fail(&block.base, src, "TODO: Sema.zirShuffle", .{});
5237}
5238
5239fn zirAtomicLoad(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) InnerError!*Inst {
5240 const inst_data = sema.code.instructions.items(.data)[inst].pl_node;
5241 const src = inst_data.src();
5242 return sema.mod.fail(&block.base, src, "TODO: Sema.zirAtomicLoad", .{});
5243}
5244
5245fn zirAtomicRmw(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) InnerError!*Inst {
5246 const inst_data = sema.code.instructions.items(.data)[inst].pl_node;
5247 const src = inst_data.src();
5248 return sema.mod.fail(&block.base, src, "TODO: Sema.zirAtomicRmw", .{});
5249}
5250
5251fn zirAtomicStore(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) InnerError!*Inst {
5252 const inst_data = sema.code.instructions.items(.data)[inst].pl_node;
5253 const src = inst_data.src();
5254 return sema.mod.fail(&block.base, src, "TODO: Sema.zirAtomicStore", .{});
5255}
5256
5257fn zirMulAdd(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) InnerError!*Inst {
5258 const inst_data = sema.code.instructions.items(.data)[inst].pl_node;
5259 const src = inst_data.src();
5260 return sema.mod.fail(&block.base, src, "TODO: Sema.zirMulAdd", .{});
5261}
5262
5263fn zirBuiltinCall(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) InnerError!*Inst {
5264 const inst_data = sema.code.instructions.items(.data)[inst].pl_node;
5265 const src = inst_data.src();
5266 return sema.mod.fail(&block.base, src, "TODO: Sema.zirBuiltinCall", .{});
5267}
5268
5269fn zirFieldPtrType(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) InnerError!*Inst {
5270 const inst_data = sema.code.instructions.items(.data)[inst].pl_node;
5271 const src = inst_data.src();
5272 return sema.mod.fail(&block.base, src, "TODO: Sema.zirFieldPtrType", .{});
5273}
5274
5275fn zirFieldParentPtr(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) InnerError!*Inst {
5276 const inst_data = sema.code.instructions.items(.data)[inst].pl_node;
5277 const src = inst_data.src();
5278 return sema.mod.fail(&block.base, src, "TODO: Sema.zirFieldParentPtr", .{});
5279}
5280
5281fn zirMemcpy(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) InnerError!*Inst {
5282 const inst_data = sema.code.instructions.items(.data)[inst].pl_node;
5283 const src = inst_data.src();
5284 return sema.mod.fail(&block.base, src, "TODO: Sema.zirMemcpy", .{});
5285}
5286
5287fn zirMemset(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) InnerError!*Inst {
5288 const inst_data = sema.code.instructions.items(.data)[inst].pl_node;
5289 const src = inst_data.src();
5290 return sema.mod.fail(&block.base, src, "TODO: Sema.zirMemset", .{});
5291}
5292
5293fn zirBuiltinAsyncCall(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) InnerError!*Inst {
5294 const inst_data = sema.code.instructions.items(.data)[inst].pl_node;
5295 const src = inst_data.src();
5296 return sema.mod.fail(&block.base, src, "TODO: Sema.zirBuiltinAsyncCall", .{});
5297}
5298
5299fn zirExtended(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) InnerError!*Inst {
5300 const extended = sema.code.instructions.items(.data)[inst].extended;
5301 switch (extended.opcode) {
5302 // zig fmt: off
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 zirCUndef(
5313 sema: *Sema,
5314 block: *Scope.Block,
5315 inst: Zir.Inst.Index,
5316 extended: Zir.Inst.Extended.InstData,
5317) InnerError!*Inst {
5318 const extra = sema.code.extraData(Zir.Inst.UnNode, extended.operand).data;
5319 const src: LazySrcLoc = .{ .node_offset = extra.node };
5320 return sema.mod.fail(&block.base, src, "TODO: implement Sema.zirCUndef", .{});
5321}
5322
5323fn zirCInclude(
5324 sema: *Sema,
5325 block: *Scope.Block,
5326 inst: Zir.Inst.Index,
5327 extended: Zir.Inst.Extended.InstData,
5328) InnerError!*Inst {
5329 const extra = sema.code.extraData(Zir.Inst.UnNode, extended.operand).data;
5330 const src: LazySrcLoc = .{ .node_offset = extra.node };
5331 return sema.mod.fail(&block.base, src, "TODO: implement Sema.zirCInclude", .{});
5332}
5333
5334fn zirCDefine(
5335 sema: *Sema,
5336 block: *Scope.Block,
5337 inst: Zir.Inst.Index,
5338 extended: Zir.Inst.Extended.InstData,
5339) InnerError!*Inst {
5340 const extra = sema.code.extraData(Zir.Inst.BinNode, extended.operand).data;
5341 const src: LazySrcLoc = .{ .node_offset = extra.node };
5342 return sema.mod.fail(&block.base, src, "TODO: implement Sema.zirCDefine", .{});
5343}
5344
5345fn zirWasmMemorySize(
5346 sema: *Sema,
5347 block: *Scope.Block,
5348 inst: Zir.Inst.Index,
5349 extended: Zir.Inst.Extended.InstData,
5350) InnerError!*Inst {
5351 const extra = sema.code.extraData(Zir.Inst.UnNode, extended.operand).data;
5352 const src: LazySrcLoc = .{ .node_offset = extra.node };
5353 return sema.mod.fail(&block.base, src, "TODO: implement Sema.zirWasmMemorySize", .{});
5354}
5355
5356fn zirWasmMemoryGrow(
5357 sema: *Sema,
5358 block: *Scope.Block,
5359 inst: Zir.Inst.Index,
5360 extended: Zir.Inst.Extended.InstData,
5361) InnerError!*Inst {
5362 const extra = sema.code.extraData(Zir.Inst.BinNode, extended.operand).data;
5363 const src: LazySrcLoc = .{ .node_offset = extra.node };
5364 return sema.mod.fail(&block.base, src, "TODO: implement Sema.zirWasmMemoryGrow", .{});
5365}
5366
48425367fn requireFunctionBlock(sema: *Sema, block: *Scope.Block, src: LazySrcLoc) !void {
48435368 if (sema.func == null) {
48445369 return sema.mod.fail(&block.base, src, "instruction illegal outside function body", .{});
src/Zir.zig+537-36
......@@ -274,9 +274,6 @@ pub const Inst = struct {
274274 /// Uses the `bin` union field.
275275 /// LHS is destination element type, RHS is result pointer.
276276 coerce_result_ptr,
277 /// Emit an error message and fail compilation.
278 /// Uses the `un_node` field.
279 compile_error,
280277 /// Log compile time variables and emit an error message.
281278 /// Uses the `pl_node` union field. The AST node is the compile log builtin call.
282279 /// The payload is `MultiOp`.
......@@ -339,9 +336,6 @@ pub const Inst = struct {
339336 /// Same as `elem_val` except also stores a source location node.
340337 /// Uses the `pl_node` union field. AST node is a[b] syntax. Payload is `Bin`.
341338 elem_val_node,
342 /// This instruction has been deleted late in the astgen phase. It must
343 /// be ignored, and the corresponding `Data` is undefined.
344 elided,
345339 /// Emits a compile error if the operand is not `void`.
346340 /// Uses the `un_node` field.
347341 ensure_result_used,
......@@ -391,9 +385,6 @@ pub const Inst = struct {
391385 func_extra,
392386 /// Same as `func_extra` but the function is variadic.
393387 func_extra_var_args,
394 /// Implements the `@hasDecl` builtin.
395 /// Uses the `pl_node` union field. Payload is `Bin`.
396 has_decl,
397388 /// Implements the `@import` builtin.
398389 /// Uses the `str_tok` field.
399390 import,
......@@ -412,10 +403,6 @@ pub const Inst = struct {
412403 /// Make an integer type out of signedness and bit count.
413404 /// Payload is `int_type`
414405 int_type,
415 /// Convert an error type to `u16`
416 error_to_int,
417 /// Convert a `u16` to `anyerror`
418 int_to_error,
419406 /// Return a boolean false if an optional is null. `x != null`
420407 /// Uses the `un_node` field.
421408 is_non_null,
......@@ -498,16 +485,6 @@ pub const Inst = struct {
498485 /// Same as `ret_tok` except the operand needs to get coerced to the function's
499486 /// return type.
500487 ret_coerce,
501 /// Changes the maximum number of backwards branches that compile-time
502 /// code execution can use before giving up and making a compile error.
503 /// Uses the `un_node` union field.
504 set_eval_branch_quota,
505 /// Integer shift-left. Zeroes are shifted in from the right hand side.
506 /// Uses the `pl_node` union field. Payload is `Bin`.
507 shl,
508 /// Integer shift-right. Arithmetic or logical depending on the signedness of the integer type.
509 /// Uses the `pl_node` union field. Payload is `Bin`.
510 shr,
511488 /// Create a pointer type that does not have a sentinel, alignment, or bit range specified.
512489 /// Uses the `ptr_type_simple` union field.
513490 ptr_type_simple,
......@@ -573,6 +550,10 @@ pub const Inst = struct {
573550 /// of one or more params.
574551 /// Uses the `pl_node` field. AST node is the `@TypeOf` call. Payload is `MultiOp`.
575552 typeof_peer,
553 /// Given a value, look at the type of it, which must be an integer type.
554 /// Returns the integer type for the RHS of a shift operation.
555 /// Uses the `un_node` field.
556 typeof_log2_int_type,
576557 /// Given an integer type, returns the integer type for the RHS of a shift operation.
577558 /// Uses the `un_node` field.
578559 log2_int_type,
......@@ -712,12 +693,10 @@ pub const Inst = struct {
712693 /// struct value.
713694 /// Uses the `pl_node` field. Payload is `StructInit`.
714695 struct_init,
715 /// Converts an integer into an enum value.
716 /// Uses `pl_node` with payload `Bin`. `lhs` is enum type, `rhs` is operand.
717 int_to_enum,
718 /// Converts an enum value into an integer. Resulting type will be the tag type
719 /// of the enum. Uses `un_node`.
720 enum_to_int,
696 /// Given a pointer to a union and a comptime known field name, activates that field
697 /// and returns a pointer to it.
698 /// Uses the `pl_node` field. Payload is `UnionInitPtr`.
699 union_init_ptr,
721700 /// Implements the `@typeInfo` builtin. Uses `un_node`.
722701 type_info,
723702 /// Implements the `@sizeOf` builtin. Uses `un_node`.
......@@ -741,6 +720,224 @@ pub const Inst = struct {
741720 /// Implements the `@shlWithOverflow` builtin. Uses `pl_node` with `OverflowArithmetic`.
742721 shl_with_overflow,
743722
723 /// Implements the `@errorReturnTrace` builtin.
724 /// Uses the `un_node` field.
725 error_return_trace,
726 /// Implements the `@frame` builtin.
727 /// Uses the `un_node` field.
728 frame,
729 /// Implements the `@frameAddress` builtin.
730 /// Uses the `un_node` field.
731 frame_address,
732
733 /// Implement builtin `@ptrToInt`. Uses `un_node`.
734 ptr_to_int,
735 /// Implement builtin `@errToInt`. Uses `un_node`.
736 error_to_int,
737 /// Implement builtin `@intToError`. Uses `un_node`.
738 int_to_error,
739 /// Emit an error message and fail compilation.
740 /// Uses the `un_node` field.
741 compile_error,
742 /// Changes the maximum number of backwards branches that compile-time
743 /// code execution can use before giving up and making a compile error.
744 /// Uses the `un_node` union field.
745 set_eval_branch_quota,
746 /// Converts an enum value into an integer. Resulting type will be the tag type
747 /// of the enum. Uses `un_node`.
748 enum_to_int,
749 /// Implement builtin `@alignOf`. Uses `un_node`.
750 align_of,
751 /// Implement builtin `@boolToInt`. Uses `un_node`.
752 bool_to_int,
753 /// Implement builtin `@embedFile`. Uses `un_node`.
754 embed_file,
755 /// Implement builtin `@errorName`. Uses `un_node`.
756 error_name,
757 /// Implement builtin `@panic`. Uses `un_node`.
758 panic,
759 /// Implement builtin `@setAlignStack`. Uses `un_node`.
760 set_align_stack,
761 /// Implement builtin `@setCold`. Uses `un_node`.
762 set_cold,
763 /// Implement builtin `@setFloatMode`. Uses `un_node`.
764 set_float_mode,
765 /// Implement builtin `@setRuntimeSafety`. Uses `un_node`.
766 set_runtime_safety,
767 /// Implement builtin `@sqrt`. Uses `un_node`.
768 sqrt,
769 /// Implement builtin `@sin`. Uses `un_node`.
770 sin,
771 /// Implement builtin `@cos`. Uses `un_node`.
772 cos,
773 /// Implement builtin `@exp`. Uses `un_node`.
774 exp,
775 /// Implement builtin `@exp2`. Uses `un_node`.
776 exp2,
777 /// Implement builtin `@log`. Uses `un_node`.
778 log,
779 /// Implement builtin `@log2`. Uses `un_node`.
780 log2,
781 /// Implement builtin `@log10`. Uses `un_node`.
782 log10,
783 /// Implement builtin `@fabs`. Uses `un_node`.
784 fabs,
785 /// Implement builtin `@floor`. Uses `un_node`.
786 floor,
787 /// Implement builtin `@ceil`. Uses `un_node`.
788 ceil,
789 /// Implement builtin `@trunc`. Uses `un_node`.
790 trunc,
791 /// Implement builtin `@round`. Uses `un_node`.
792 round,
793 /// Implement builtin `@tagName`. Uses `un_node`.
794 tag_name,
795 /// Implement builtin `@Type`. Uses `un_node`.
796 reify,
797 /// Implement builtin `@typeName`. Uses `un_node`.
798 type_name,
799 /// Implement builtin `@Frame`. Uses `un_node`.
800 frame_type,
801 /// Implement builtin `@frameSize`. Uses `un_node`.
802 frame_size,
803
804 /// Implements the `@floatToInt` builtin.
805 /// Uses `pl_node` with payload `Bin`. `lhs` is dest type, `rhs` is operand.
806 float_to_int,
807 /// Implements the `@intToFloat` builtin.
808 /// Uses `pl_node` with payload `Bin`. `lhs` is dest type, `rhs` is operand.
809 int_to_float,
810 /// Implements the `@intToPtr` builtin.
811 /// Uses `pl_node` with payload `Bin`. `lhs` is dest type, `rhs` is operand.
812 int_to_ptr,
813 /// Converts an integer into an enum value.
814 /// Uses `pl_node` with payload `Bin`. `lhs` is dest type, `rhs` is operand.
815 int_to_enum,
816 /// Implements the `@floatCast` builtin.
817 /// Uses `pl_node` with payload `Bin`. `lhs` is dest type, `rhs` is operand.
818 float_cast,
819 /// Implements the `@intCast` builtin.
820 /// Uses `pl_node` with payload `Bin`. `lhs` is dest type, `rhs` is operand.
821 int_cast,
822 /// Implements the `@errSetCast` builtin.
823 /// Uses `pl_node` with payload `Bin`. `lhs` is dest type, `rhs` is operand.
824 err_set_cast,
825 /// Implements the `@ptrCast` builtin.
826 /// Uses `pl_node` with payload `Bin`. `lhs` is dest type, `rhs` is operand.
827 ptr_cast,
828 /// Implements the `@truncate` builtin.
829 /// Uses `pl_node` with payload `Bin`. `lhs` is dest type, `rhs` is operand.
830 truncate,
831 /// Implements the `@alignCast` builtin.
832 /// Uses `pl_node` with payload `Bin`. `lhs` is dest alignment, `rhs` is operand.
833 align_cast,
834
835 /// Implements the `@hasDecl` builtin.
836 /// Uses the `pl_node` union field. Payload is `Bin`.
837 has_decl,
838 /// Implements the `@hasField` builtin.
839 /// Uses the `pl_node` union field. Payload is `Bin`.
840 has_field,
841
842 /// Implements the `@clz` builtin. Uses the `un_node` union field.
843 clz,
844 /// Implements the `@ctz` builtin. Uses the `un_node` union field.
845 ctz,
846 /// Implements the `@popCount` builtin. Uses the `un_node` union field.
847 pop_count,
848 /// Implements the `@byteSwap` builtin. Uses the `un_node` union field.
849 byte_swap,
850 /// Implements the `@bitReverse` builtin. Uses the `un_node` union field.
851 bit_reverse,
852
853 /// Implements the `@divExact` builtin.
854 /// Uses the `pl_node` union field with payload `Bin`.
855 div_exact,
856 /// Implements the `@divFloor` builtin.
857 /// Uses the `pl_node` union field with payload `Bin`.
858 div_floor,
859 /// Implements the `@divTrunc` builtin.
860 /// Uses the `pl_node` union field with payload `Bin`.
861 div_trunc,
862 /// Implements the `@mod` builtin.
863 /// Uses the `pl_node` union field with payload `Bin`.
864 mod,
865 /// Implements the `@rem` builtin.
866 /// Uses the `pl_node` union field with payload `Bin`.
867 rem,
868
869 /// Integer shift-left. Zeroes are shifted in from the right hand side.
870 /// Uses the `pl_node` union field. Payload is `Bin`.
871 shl,
872 /// Implements the `@shlExact` builtin.
873 /// Uses the `pl_node` union field with payload `Bin`.
874 shl_exact,
875 /// Integer shift-right. Arithmetic or logical depending on the signedness of the integer type.
876 /// Uses the `pl_node` union field. Payload is `Bin`.
877 shr,
878 /// Implements the `@shrExact` builtin.
879 /// Uses the `pl_node` union field with payload `Bin`.
880 shr_exact,
881
882 /// Implements the `@bitOffsetOf` builtin.
883 /// Uses the `pl_node` union field with payload `Bin`.
884 bit_offset_of,
885 /// Implements the `@byteOffsetOf` builtin.
886 /// Uses the `pl_node` union field with payload `Bin`.
887 byte_offset_of,
888 /// Implements the `@cmpxchgStrong` builtin.
889 /// Uses the `pl_node` union field with payload `Cmpxchg`.
890 cmpxchg_strong,
891 /// Implements the `@cmpxchgWeak` builtin.
892 /// Uses the `pl_node` union field with payload `Cmpxchg`.
893 cmpxchg_weak,
894 /// Implements the `@splat` builtin.
895 /// Uses the `pl_node` union field with payload `Bin`.
896 splat,
897 /// Implements the `@reduce` builtin.
898 /// Uses the `pl_node` union field with payload `Bin`.
899 reduce,
900 /// Implements the `@shuffle` builtin.
901 /// Uses the `pl_node` union field with payload `Shuffle`.
902 shuffle,
903 /// Implements the `@atomicLoad` builtin.
904 /// Uses the `pl_node` union field with payload `Bin`.
905 atomic_load,
906 /// Implements the `@atomicRmw` builtin.
907 /// Uses the `pl_node` union field with payload `AtomicRmw`.
908 atomic_rmw,
909 /// Implements the `@atomicStore` builtin.
910 /// Uses the `pl_node` union field with payload `AtomicStore`.
911 atomic_store,
912 /// Implements the `@mulAdd` builtin.
913 /// Uses the `pl_node` union field with payload `MulAdd`.
914 mul_add,
915 /// Implements the `@call` builtin.
916 /// Uses the `pl_node` union field with payload `BuiltinCall`.
917 builtin_call,
918 /// Given a type and a field name, returns a pointer to the field type.
919 /// Assumed to be part of a `@fieldParentPtr` builtin call.
920 /// Uses the `bin` union field. LHS is type, RHS is field name.
921 field_ptr_type,
922 /// Implements the `@fieldParentPtr` builtin.
923 /// Uses the `pl_node` union field with payload `FieldParentPtr`.
924 field_parent_ptr,
925 /// Implements the `@memcpy` builtin.
926 /// Uses the `pl_node` union field with payload `Memcpy`.
927 memcpy,
928 /// Implements the `@memset` builtin.
929 /// Uses the `pl_node` union field with payload `Memset`.
930 memset,
931 /// Implements the `@asyncCall` builtin.
932 /// Uses the `pl_node` union field with payload `AsyncCall`.
933 builtin_async_call,
934 /// Implements the `@cImport` builtin.
935 /// Uses the `pl_node` union field with payload `Block`.
936 c_import,
937 /// The ZIR instruction tag is one of the `Extended` ones.
938 /// Uses the `extended` union field.
939 extended,
940
744941 /// Returns whether the instruction is one of the control flow "noreturn" types.
745942 /// Function calls do not count.
746943 pub fn isNoReturn(tag: Tag) bool {
......@@ -876,11 +1073,11 @@ pub const Inst = struct {
8761073 .slice_sentinel,
8771074 .import,
8781075 .typeof_peer,
1076 .typeof_log2_int_type,
8791077 .log2_int_type,
8801078 .resolve_inferred_alloc,
8811079 .set_eval_branch_quota,
8821080 .compile_log,
883 .elided,
8841081 .switch_capture,
8851082 .switch_capture_ref,
8861083 .switch_capture_multi,
......@@ -902,6 +1099,7 @@ pub const Inst = struct {
9021099 .validate_struct_init_ptr,
9031100 .struct_init_empty,
9041101 .struct_init,
1102 .union_init_ptr,
9051103 .field_type,
9061104 .int_to_enum,
9071105 .enum_to_int,
......@@ -916,6 +1114,77 @@ pub const Inst = struct {
9161114 .sub_with_overflow,
9171115 .mul_with_overflow,
9181116 .shl_with_overflow,
1117 .error_return_trace,
1118 .frame,
1119 .frame_address,
1120 .ptr_to_int,
1121 .align_of,
1122 .bool_to_int,
1123 .embed_file,
1124 .error_name,
1125 .set_align_stack,
1126 .set_cold,
1127 .set_float_mode,
1128 .set_runtime_safety,
1129 .sqrt,
1130 .sin,
1131 .cos,
1132 .exp,
1133 .exp2,
1134 .log,
1135 .log2,
1136 .log10,
1137 .fabs,
1138 .floor,
1139 .ceil,
1140 .trunc,
1141 .round,
1142 .tag_name,
1143 .reify,
1144 .type_name,
1145 .frame_type,
1146 .frame_size,
1147 .float_to_int,
1148 .int_to_float,
1149 .int_to_ptr,
1150 .float_cast,
1151 .int_cast,
1152 .err_set_cast,
1153 .ptr_cast,
1154 .truncate,
1155 .align_cast,
1156 .has_field,
1157 .clz,
1158 .ctz,
1159 .pop_count,
1160 .byte_swap,
1161 .bit_reverse,
1162 .div_exact,
1163 .div_floor,
1164 .div_trunc,
1165 .mod,
1166 .rem,
1167 .shl_exact,
1168 .shr_exact,
1169 .bit_offset_of,
1170 .byte_offset_of,
1171 .cmpxchg_strong,
1172 .cmpxchg_weak,
1173 .splat,
1174 .reduce,
1175 .shuffle,
1176 .atomic_load,
1177 .atomic_rmw,
1178 .atomic_store,
1179 .mul_add,
1180 .builtin_call,
1181 .field_ptr_type,
1182 .field_parent_ptr,
1183 .memcpy,
1184 .memset,
1185 .builtin_async_call,
1186 .c_import,
1187 .extended,
9191188 => false,
9201189
9211190 .@"break",
......@@ -929,11 +1198,33 @@ pub const Inst = struct {
9291198 .@"unreachable",
9301199 .repeat,
9311200 .repeat_inline,
1201 .panic,
9321202 => true,
9331203 };
9341204 }
9351205 };
9361206
1207 /// Rarer instructions are here; ones that do not fit in the 8-bit `Tag` enum.
1208 /// `noreturn` instructions may not go here; they must be part of the main `Tag` enum.
1209 pub const Extended = enum(u16) {
1210 /// `operand` is payload index to `UnNode`.
1211 c_undef,
1212 /// `operand` is payload index to `UnNode`.
1213 c_include,
1214 /// `operand` is payload index to `BinNode`.
1215 c_define,
1216 /// `operand` is payload index to `UnNode`.
1217 wasm_memory_size,
1218 /// `operand` is payload index to `BinNode`.
1219 wasm_memory_grow,
1220
1221 pub const InstData = struct {
1222 opcode: Extended,
1223 small: u16,
1224 operand: u32,
1225 };
1226 };
1227
9371228 /// The position of a ZIR instruction within the `Zir` instructions array.
9381229 pub const Index = u32;
9391230
......@@ -1002,6 +1293,14 @@ pub const Inst = struct {
10021293 single_const_pointer_to_comptime_int_type,
10031294 const_slice_u8_type,
10041295 enum_literal_type,
1296 manyptr_u8_type,
1297 manyptr_const_u8_type,
1298 atomic_ordering_type,
1299 atomic_rmw_op_type,
1300 calling_convention_type,
1301 float_mode_type,
1302 reduce_op_type,
1303 call_options_type,
10051304
10061305 /// `undefined` (untyped)
10071306 undef,
......@@ -1025,6 +1324,8 @@ pub const Inst = struct {
10251324 zero_usize,
10261325 /// `1` (usize)
10271326 one_usize,
1327 /// `std.builtin.CallingConvention.C`
1328 calling_convention_c,
10281329
10291330 _,
10301331
......@@ -1191,6 +1492,38 @@ pub const Inst = struct {
11911492 .ty = Type.initTag(.type),
11921493 .val = Value.initTag(.enum_literal_type),
11931494 },
1495 .manyptr_u8_type = .{
1496 .ty = Type.initTag(.type),
1497 .val = Value.initTag(.manyptr_u8_type),
1498 },
1499 .manyptr_const_u8_type = .{
1500 .ty = Type.initTag(.type),
1501 .val = Value.initTag(.manyptr_const_u8_type),
1502 },
1503 .atomic_ordering_type = .{
1504 .ty = Type.initTag(.type),
1505 .val = Value.initTag(.atomic_ordering_type),
1506 },
1507 .atomic_rmw_op_type = .{
1508 .ty = Type.initTag(.type),
1509 .val = Value.initTag(.atomic_rmw_op_type),
1510 },
1511 .calling_convention_type = .{
1512 .ty = Type.initTag(.type),
1513 .val = Value.initTag(.calling_convention_type),
1514 },
1515 .float_mode_type = .{
1516 .ty = Type.initTag(.type),
1517 .val = Value.initTag(.float_mode_type),
1518 },
1519 .reduce_op_type = .{
1520 .ty = Type.initTag(.type),
1521 .val = Value.initTag(.reduce_op_type),
1522 },
1523 .call_options_type = .{
1524 .ty = Type.initTag(.type),
1525 .val = Value.initTag(.call_options_type),
1526 },
11941527
11951528 .undef = .{
11961529 .ty = Type.initTag(.@"undefined"),
......@@ -1236,13 +1569,27 @@ pub const Inst = struct {
12361569 .ty = Type.initTag(.empty_struct_literal),
12371570 .val = Value.initTag(.empty_struct_value),
12381571 },
1572 .calling_convention_c = .{
1573 .ty = Type.initTag(.calling_convention),
1574 .val = .{ .ptr_otherwise = &calling_convention_c_payload.base },
1575 },
12391576 });
12401577 };
12411578
1579 /// We would like this to be const but `Value` wants a mutable pointer for
1580 /// its payload field. Nothing should mutate this though.
1581 var calling_convention_c_payload: Value.Payload.U32 = .{
1582 .base = .{ .tag = .enum_field_index },
1583 .data = @enumToInt(std.builtin.CallingConvention.C),
1584 };
1585
12421586 /// All instructions have an 8-byte payload, which is contained within
12431587 /// this union. `Tag` determines which union field is active, as well as
12441588 /// how to interpret the data within.
12451589 pub const Data = union {
1590 /// Used for `Tag.extended`. The extended opcode determines the meaning
1591 /// of the `small` and `operand` fields.
1592 extended: Extended.InstData,
12461593 /// Used for unary operators, with an AST node source location.
12471594 un_node: struct {
12481595 /// Offset from Decl AST node index.
......@@ -1462,6 +1809,12 @@ pub const Inst = struct {
14621809 args_len: u32,
14631810 };
14641811
1812 pub const BuiltinCall = struct {
1813 options: Ref,
1814 callee: Ref,
1815 args: Ref,
1816 };
1817
14651818 /// This data is stored inside extra, with two sets of trailing `Ref`:
14661819 /// * 0. the then body, according to `then_body_len`.
14671820 /// * 1. the else body, according to `else_body_len`.
......@@ -1510,6 +1863,17 @@ pub const Inst = struct {
15101863 rhs: Ref,
15111864 };
15121865
1866 pub const BinNode = struct {
1867 node: i32,
1868 lhs: Ref,
1869 rhs: Ref,
1870 };
1871
1872 pub const UnNode = struct {
1873 node: i32,
1874 operand: Ref,
1875 };
1876
15131877 /// This form is supported when there are no ranges, and exactly 1 item per block.
15141878 /// Depending on zir tag and len fields, extra fields trail
15151879 /// this one in the extra array.
......@@ -1679,6 +2043,70 @@ pub const Inst = struct {
16792043 ptr: Ref,
16802044 };
16812045
2046 pub const Cmpxchg = struct {
2047 ptr: Ref,
2048 expected_value: Ref,
2049 new_value: Ref,
2050 success_order: Ref,
2051 fail_order: Ref,
2052 };
2053
2054 pub const AtomicRmw = struct {
2055 ptr: Ref,
2056 operation: Ref,
2057 operand: Ref,
2058 ordering: Ref,
2059 };
2060
2061 pub const UnionInitPtr = struct {
2062 union_type: Ref,
2063 field_name: Ref,
2064 };
2065
2066 pub const AtomicStore = struct {
2067 ptr: Ref,
2068 operand: Ref,
2069 ordering: Ref,
2070 };
2071
2072 pub const MulAdd = struct {
2073 mulend1: Ref,
2074 mulend2: Ref,
2075 addend: Ref,
2076 };
2077
2078 pub const FieldParentPtr = struct {
2079 parent_type: Ref,
2080 field_name: Ref,
2081 field_ptr: Ref,
2082 };
2083
2084 pub const Memcpy = struct {
2085 dest: Ref,
2086 source: Ref,
2087 byte_count: Ref,
2088 };
2089
2090 pub const Memset = struct {
2091 dest: Ref,
2092 byte: Ref,
2093 byte_count: Ref,
2094 };
2095
2096 pub const Shuffle = struct {
2097 elem_type: Ref,
2098 a: Ref,
2099 b: Ref,
2100 mask: Ref,
2101 };
2102
2103 pub const AsyncCall = struct {
2104 frame_buffer: Ref,
2105 result_ptr: Ref,
2106 fn_ptr: Ref,
2107 args: Ref,
2108 };
2109
16822110 /// Trailing: `CompileErrors.Item` for each `items_len`.
16832111 pub const CompileErrors = struct {
16842112 items_len: u32,
......@@ -1749,13 +2177,11 @@ const Writer = struct {
17492177 .negate_wrap,
17502178 .call_none,
17512179 .call_none_chkused,
1752 .compile_error,
17532180 .load,
17542181 .ensure_result_used,
17552182 .ensure_result_non_error,
17562183 .ptrtoint,
17572184 .ret_node,
1758 .set_eval_branch_quota,
17592185 .resolve_inferred_alloc,
17602186 .optional_type,
17612187 .optional_type_from_ptr_elem,
......@@ -1769,8 +2195,6 @@ const Writer = struct {
17692195 .err_union_payload_unsafe_ptr,
17702196 .err_union_code,
17712197 .err_union_code_ptr,
1772 .int_to_error,
1773 .error_to_int,
17742198 .is_non_null,
17752199 .is_null,
17762200 .is_non_null_ptr,
......@@ -1780,11 +2204,49 @@ const Writer = struct {
17802204 .typeof,
17812205 .typeof_elem,
17822206 .struct_init_empty,
1783 .enum_to_int,
17842207 .type_info,
17852208 .size_of,
17862209 .bit_size_of,
2210 .typeof_log2_int_type,
17872211 .log2_int_type,
2212 .ptr_to_int,
2213 .error_to_int,
2214 .int_to_error,
2215 .compile_error,
2216 .set_eval_branch_quota,
2217 .enum_to_int,
2218 .align_of,
2219 .bool_to_int,
2220 .embed_file,
2221 .error_name,
2222 .panic,
2223 .set_align_stack,
2224 .set_cold,
2225 .set_float_mode,
2226 .set_runtime_safety,
2227 .sqrt,
2228 .sin,
2229 .cos,
2230 .exp,
2231 .exp2,
2232 .log,
2233 .log2,
2234 .log10,
2235 .fabs,
2236 .floor,
2237 .ceil,
2238 .trunc,
2239 .round,
2240 .tag_name,
2241 .reify,
2242 .type_name,
2243 .frame_type,
2244 .frame_size,
2245 .clz,
2246 .ctz,
2247 .pop_count,
2248 .byte_swap,
2249 .bit_reverse,
17882250 => try self.writeUnNode(stream, inst),
17892251
17902252 .ref,
......@@ -1805,7 +2267,6 @@ const Writer = struct {
18052267 .float => try self.writeFloat(stream, inst),
18062268 .float128 => try self.writeFloat128(stream, inst),
18072269 .str => try self.writeStr(stream, inst),
1808 .elided => try stream.writeAll(")"),
18092270 .int_type => try self.writeIntType(stream, inst),
18102271
18112272 .@"break",
......@@ -1824,7 +2285,20 @@ const Writer = struct {
18242285 .slice_sentinel,
18252286 .union_decl,
18262287 .struct_init,
2288 .union_init_ptr,
18272289 .field_type,
2290 .cmpxchg_strong,
2291 .cmpxchg_weak,
2292 .shuffle,
2293 .atomic_rmw,
2294 .atomic_store,
2295 .mul_add,
2296 .builtin_call,
2297 .field_ptr_type,
2298 .field_parent_ptr,
2299 .memcpy,
2300 .memset,
2301 .builtin_async_call,
18282302 => try self.writePlNode(stream, inst),
18292303
18302304 .add_with_overflow,
......@@ -1851,9 +2325,12 @@ const Writer = struct {
18512325 .cmp_neq,
18522326 .div,
18532327 .has_decl,
2328 .has_field,
18542329 .mod_rem,
18552330 .shl,
2331 .shl_exact,
18562332 .shr,
2333 .shr_exact,
18572334 .xor,
18582335 .store_node,
18592336 .error_union_type,
......@@ -1861,7 +2338,26 @@ const Writer = struct {
18612338 .merge_error_sets,
18622339 .bit_and,
18632340 .bit_or,
2341 .float_to_int,
2342 .int_to_float,
2343 .int_to_ptr,
18642344 .int_to_enum,
2345 .float_cast,
2346 .int_cast,
2347 .err_set_cast,
2348 .ptr_cast,
2349 .truncate,
2350 .align_cast,
2351 .div_exact,
2352 .div_floor,
2353 .div_trunc,
2354 .mod,
2355 .rem,
2356 .bit_offset_of,
2357 .byte_offset_of,
2358 .splat,
2359 .reduce,
2360 .atomic_load,
18652361 => try self.writePlNodeBin(stream, inst),
18662362
18672363 .call,
......@@ -1874,6 +2370,7 @@ const Writer = struct {
18742370 .block_inline_var,
18752371 .loop,
18762372 .validate_struct_init_ptr,
2373 .c_import,
18772374 => try self.writePlNodeBlock(stream, inst),
18782375
18792376 .condbr,
......@@ -1926,6 +2423,9 @@ const Writer = struct {
19262423 .fence,
19272424 .ret_addr,
19282425 .builtin_src,
2426 .error_return_trace,
2427 .frame,
2428 .frame_address,
19292429 => try self.writeNode(stream, inst),
19302430
19312431 .error_value,
......@@ -1954,6 +2454,7 @@ const Writer = struct {
19542454
19552455 .bitcast,
19562456 .bitcast_result_ptr,
2457 .extended,
19572458 => try stream.writeAll("TODO)"),
19582459 }
19592460 }
src/type.zig+185-3
......@@ -83,6 +83,8 @@ pub const Type = extern union {
8383 .pointer,
8484 .inferred_alloc_const,
8585 .inferred_alloc_mut,
86 .manyptr_u8,
87 .manyptr_const_u8,
8688 => return .Pointer,
8789
8890 .optional,
......@@ -96,11 +98,17 @@ pub const Type = extern union {
9698 .empty_struct,
9799 .empty_struct_literal,
98100 .@"struct",
101 .call_options,
99102 => return .Struct,
100103
101104 .enum_full,
102105 .enum_nonexhaustive,
103106 .enum_simple,
107 .atomic_ordering,
108 .atomic_rmw_op,
109 .calling_convention,
110 .float_mode,
111 .reduce_op,
104112 => return .Enum,
105113
106114 .var_args_param => unreachable, // can be any type
......@@ -205,6 +213,8 @@ pub const Type = extern union {
205213 .mut_slice,
206214 .optional_single_const_pointer,
207215 .optional_single_mut_pointer,
216 .manyptr_u8,
217 .manyptr_const_u8,
208218 => self.cast(Payload.ElemType),
209219
210220 .inferred_alloc_const => unreachable,
......@@ -271,6 +281,17 @@ pub const Type = extern union {
271281 .@"volatile" = false,
272282 .size = .Many,
273283 } },
284 .manyptr_const_u8 => return .{ .data = .{
285 .pointee_type = Type.initTag(.u8),
286 .sentinel = null,
287 .@"align" = 0,
288 .bit_offset = 0,
289 .host_size = 0,
290 .@"allowzero" = false,
291 .mutable = false,
292 .@"volatile" = false,
293 .size = .Many,
294 } },
274295 .many_mut_pointer => return .{ .data = .{
275296 .pointee_type = self.castPointer().?.data,
276297 .sentinel = null,
......@@ -282,6 +303,17 @@ pub const Type = extern union {
282303 .@"volatile" = false,
283304 .size = .Many,
284305 } },
306 .manyptr_u8 => return .{ .data = .{
307 .pointee_type = Type.initTag(.u8),
308 .sentinel = null,
309 .@"align" = 0,
310 .bit_offset = 0,
311 .host_size = 0,
312 .@"allowzero" = false,
313 .mutable = true,
314 .@"volatile" = false,
315 .size = .Many,
316 } },
285317 .c_const_pointer => return .{ .data = .{
286318 .pointee_type = self.castPointer().?.data,
287319 .sentinel = null,
......@@ -576,6 +608,14 @@ pub const Type = extern union {
576608 .inferred_alloc_mut,
577609 .var_args_param,
578610 .empty_struct_literal,
611 .manyptr_u8,
612 .manyptr_const_u8,
613 .atomic_ordering,
614 .atomic_rmw_op,
615 .calling_convention,
616 .float_mode,
617 .reduce_op,
618 .call_options,
579619 => unreachable,
580620
581621 .array_u8,
......@@ -746,6 +786,14 @@ pub const Type = extern union {
746786 .fn_naked_noreturn_no_args => return writer.writeAll("fn() callconv(.Naked) noreturn"),
747787 .fn_ccc_void_no_args => return writer.writeAll("fn() callconv(.C) void"),
748788 .single_const_pointer_to_comptime_int => return writer.writeAll("*const comptime_int"),
789 .manyptr_u8 => return writer.writeAll("[*]u8"),
790 .manyptr_const_u8 => return writer.writeAll("[*]const u8"),
791 .atomic_ordering => return writer.writeAll("std.builtin.AtomicOrdering"),
792 .atomic_rmw_op => return writer.writeAll("std.builtin.AtomicRmwOp"),
793 .calling_convention => return writer.writeAll("std.builtin.CallingConvention"),
794 .float_mode => return writer.writeAll("std.builtin.FloatMode"),
795 .reduce_op => return writer.writeAll("std.builtin.ReduceOp"),
796 .call_options => return writer.writeAll("std.builtin.CallOptions"),
749797 .function => {
750798 const payload = ty.castTag(.function).?.data;
751799 try writer.writeAll("fn(");
......@@ -952,6 +1000,14 @@ pub const Type = extern union {
9521000 .single_const_pointer_to_comptime_int => return Value.initTag(.single_const_pointer_to_comptime_int_type),
9531001 .const_slice_u8 => return Value.initTag(.const_slice_u8_type),
9541002 .enum_literal => return Value.initTag(.enum_literal_type),
1003 .manyptr_u8 => return Value.initTag(.manyptr_u8_type),
1004 .manyptr_const_u8 => return Value.initTag(.manyptr_const_u8_type),
1005 .atomic_ordering => return Value.initTag(.atomic_ordering_type),
1006 .atomic_rmw_op => return Value.initTag(.atomic_rmw_op_type),
1007 .calling_convention => return Value.initTag(.calling_convention_type),
1008 .float_mode => return Value.initTag(.float_mode_type),
1009 .reduce_op => return Value.initTag(.reduce_op_type),
1010 .call_options => return Value.initTag(.call_options_type),
9551011 .inferred_alloc_const => unreachable,
9561012 .inferred_alloc_mut => unreachable,
9571013 else => return Value.Tag.ty.create(allocator, self),
......@@ -1001,6 +1057,14 @@ pub const Type = extern union {
10011057 .anyerror_void_error_union,
10021058 .error_set,
10031059 .error_set_single,
1060 .manyptr_u8,
1061 .manyptr_const_u8,
1062 .atomic_ordering,
1063 .atomic_rmw_op,
1064 .calling_convention,
1065 .float_mode,
1066 .reduce_op,
1067 .call_options,
10041068 => true,
10051069
10061070 .@"struct" => {
......@@ -1079,7 +1143,10 @@ pub const Type = extern union {
10791143 .optional_single_mut_pointer,
10801144 => return self.cast(Payload.ElemType).?.data.abiAlignment(target),
10811145
1082 .const_slice_u8 => return 1,
1146 .manyptr_u8,
1147 .manyptr_const_u8,
1148 .const_slice_u8,
1149 => return 1,
10831150
10841151 .pointer => {
10851152 const ptr_info = self.castTag(.pointer).?.data;
......@@ -1102,6 +1169,12 @@ pub const Type = extern union {
11021169 .bool,
11031170 .array_u8_sentinel_0,
11041171 .array_u8,
1172 .atomic_ordering,
1173 .atomic_rmw_op,
1174 .calling_convention,
1175 .float_mode,
1176 .reduce_op,
1177 .call_options,
11051178 => return 1,
11061179
11071180 .fn_noreturn_no_args, // represents machine code; not a pointer
......@@ -1136,6 +1209,8 @@ pub const Type = extern union {
11361209 .optional_single_const_pointer,
11371210 .optional_single_mut_pointer,
11381211 .pointer,
1212 .manyptr_u8,
1213 .manyptr_const_u8,
11391214 => return @divExact(target.cpu.arch.ptrBitWidth(), 8),
11401215
11411216 .c_short => return @divExact(CType.short.sizeInBits(target), 8),
......@@ -1271,6 +1346,12 @@ pub const Type = extern union {
12711346 .u8,
12721347 .i8,
12731348 .bool,
1349 .atomic_ordering,
1350 .atomic_rmw_op,
1351 .calling_convention,
1352 .float_mode,
1353 .reduce_op,
1354 .call_options,
12741355 => return 1,
12751356
12761357 .array_u8 => self.castTag(.array_u8).?.data,
......@@ -1322,6 +1403,10 @@ pub const Type = extern union {
13221403 return @divExact(target.cpu.arch.ptrBitWidth(), 8);
13231404 },
13241405
1406 .manyptr_u8,
1407 .manyptr_const_u8,
1408 => return @divExact(target.cpu.arch.ptrBitWidth(), 8),
1409
13251410 .c_short => return @divExact(CType.short.sizeInBits(target), 8),
13261411 .c_ushort => return @divExact(CType.ushort.sizeInBits(target), 8),
13271412 .c_int => return @divExact(CType.int.sizeInBits(target), 8),
......@@ -1475,6 +1560,10 @@ pub const Type = extern union {
14751560 }
14761561 },
14771562
1563 .manyptr_u8,
1564 .manyptr_const_u8,
1565 => return target.cpu.arch.ptrBitWidth(),
1566
14781567 .c_short => return CType.short.sizeInBits(target),
14791568 .c_ushort => return CType.ushort.sizeInBits(target),
14801569 .c_int => return CType.int.sizeInBits(target),
......@@ -1517,8 +1606,16 @@ pub const Type = extern union {
15171606 } else if (!payload.payload.hasCodeGenBits()) {
15181607 return payload.error_set.bitSize(target);
15191608 }
1520 @panic("TODO abiSize error union");
1609 @panic("TODO bitSize error union");
15211610 },
1611
1612 .atomic_ordering,
1613 .atomic_rmw_op,
1614 .calling_convention,
1615 .float_mode,
1616 .reduce_op,
1617 .call_options,
1618 => @panic("TODO at some point we gotta resolve builtin types"),
15221619 };
15231620 }
15241621
......@@ -1564,6 +1661,8 @@ pub const Type = extern union {
15641661
15651662 .many_const_pointer,
15661663 .many_mut_pointer,
1664 .manyptr_u8,
1665 .manyptr_const_u8,
15671666 => .Many,
15681667
15691668 .c_const_pointer,
......@@ -1604,6 +1703,7 @@ pub const Type = extern union {
16041703 .single_const_pointer_to_comptime_int,
16051704 .const_slice_u8,
16061705 .const_slice,
1706 .manyptr_const_u8,
16071707 => true,
16081708
16091709 .pointer => !self.castTag(.pointer).?.data.mutable,
......@@ -1718,7 +1818,13 @@ pub const Type = extern union {
17181818 .mut_slice,
17191819 => self.castPointer().?.data,
17201820
1721 .array_u8, .array_u8_sentinel_0, .const_slice_u8 => Type.initTag(.u8),
1821 .array_u8,
1822 .array_u8_sentinel_0,
1823 .const_slice_u8,
1824 .manyptr_u8,
1825 .manyptr_const_u8,
1826 => Type.initTag(.u8),
1827
17221828 .single_const_pointer_to_comptime_int => Type.initTag(.comptime_int),
17231829 .pointer => self.castTag(.pointer).?.data.pointee_type,
17241830
......@@ -1811,6 +1917,8 @@ pub const Type = extern union {
18111917 .single_const_pointer_to_comptime_int,
18121918 .array,
18131919 .array_u8,
1920 .manyptr_u8,
1921 .manyptr_const_u8,
18141922 => return null,
18151923
18161924 .pointer => return self.castTag(.pointer).?.data.sentinel,
......@@ -2122,6 +2230,14 @@ pub const Type = extern union {
21222230 .error_set_single,
21232231 .@"opaque",
21242232 .var_args_param,
2233 .manyptr_u8,
2234 .manyptr_const_u8,
2235 .atomic_ordering,
2236 .atomic_rmw_op,
2237 .calling_convention,
2238 .float_mode,
2239 .reduce_op,
2240 .call_options,
21252241 => return null,
21262242
21272243 .@"struct" => {
......@@ -2281,6 +2397,14 @@ pub const Type = extern union {
22812397 const enum_simple = ty.castTag(.enum_simple).?.data;
22822398 return enum_simple.fields.count();
22832399 },
2400 .atomic_ordering,
2401 .atomic_rmw_op,
2402 .calling_convention,
2403 .float_mode,
2404 .reduce_op,
2405 .call_options,
2406 => @panic("TODO resolve std.builtin types"),
2407
22842408 else => unreachable,
22852409 }
22862410 }
......@@ -2295,6 +2419,13 @@ pub const Type = extern union {
22952419 const enum_simple = ty.castTag(.enum_simple).?.data;
22962420 return enum_simple.fields.entries.items[field_index].key;
22972421 },
2422 .atomic_ordering,
2423 .atomic_rmw_op,
2424 .calling_convention,
2425 .float_mode,
2426 .reduce_op,
2427 .call_options,
2428 => @panic("TODO resolve std.builtin types"),
22982429 else => unreachable,
22992430 }
23002431 }
......@@ -2309,6 +2440,13 @@ pub const Type = extern union {
23092440 const enum_simple = ty.castTag(.enum_simple).?.data;
23102441 return enum_simple.fields.getIndex(field_name);
23112442 },
2443 .atomic_ordering,
2444 .atomic_rmw_op,
2445 .calling_convention,
2446 .float_mode,
2447 .reduce_op,
2448 .call_options,
2449 => @panic("TODO resolve std.builtin types"),
23122450 else => unreachable,
23132451 }
23142452 }
......@@ -2345,6 +2483,13 @@ pub const Type = extern union {
23452483 const enum_simple = ty.castTag(.enum_simple).?.data;
23462484 return S.fieldWithRange(enum_tag, enum_simple.fields.count());
23472485 },
2486 .atomic_ordering,
2487 .atomic_rmw_op,
2488 .calling_convention,
2489 .float_mode,
2490 .reduce_op,
2491 .call_options,
2492 => @panic("TODO resolve std.builtin types"),
23482493 else => unreachable,
23492494 }
23502495 }
......@@ -2367,6 +2512,13 @@ pub const Type = extern union {
23672512 const error_set = ty.castTag(.error_set).?.data;
23682513 return error_set.srcLoc();
23692514 },
2515 .atomic_ordering,
2516 .atomic_rmw_op,
2517 .calling_convention,
2518 .float_mode,
2519 .reduce_op,
2520 .call_options,
2521 => @panic("TODO resolve std.builtin types"),
23702522 else => unreachable,
23712523 }
23722524 }
......@@ -2390,6 +2542,13 @@ pub const Type = extern union {
23902542 return error_set.owner_decl;
23912543 },
23922544 .@"opaque" => @panic("TODO"),
2545 .atomic_ordering,
2546 .atomic_rmw_op,
2547 .calling_convention,
2548 .float_mode,
2549 .reduce_op,
2550 .call_options,
2551 => @panic("TODO resolve std.builtin types"),
23932552 else => unreachable,
23942553 }
23952554 }
......@@ -2422,6 +2581,13 @@ pub const Type = extern union {
24222581 const enum_simple = ty.castTag(.enum_simple).?.data;
24232582 return S.intInRange(int, enum_simple.fields.count());
24242583 },
2584 .atomic_ordering,
2585 .atomic_rmw_op,
2586 .calling_convention,
2587 .float_mode,
2588 .reduce_op,
2589 .call_options,
2590 => @panic("TODO resolve std.builtin types"),
24252591
24262592 else => unreachable,
24272593 }
......@@ -2469,6 +2635,14 @@ pub const Type = extern union {
24692635 comptime_float,
24702636 noreturn,
24712637 enum_literal,
2638 manyptr_u8,
2639 manyptr_const_u8,
2640 atomic_ordering,
2641 atomic_rmw_op,
2642 calling_convention,
2643 float_mode,
2644 reduce_op,
2645 call_options,
24722646 @"null",
24732647 @"undefined",
24742648 fn_noreturn_no_args,
......@@ -2572,6 +2746,14 @@ pub const Type = extern union {
25722746 .inferred_alloc_mut,
25732747 .var_args_param,
25742748 .empty_struct_literal,
2749 .manyptr_u8,
2750 .manyptr_const_u8,
2751 .atomic_ordering,
2752 .atomic_rmw_op,
2753 .calling_convention,
2754 .float_mode,
2755 .reduce_op,
2756 .call_options,
25752757 => @compileError("Type Tag " ++ @tagName(t) ++ " has no payload"),
25762758
25772759 .array_u8,
src/value.zig+58
......@@ -64,6 +64,14 @@ pub const Value = extern union {
6464 single_const_pointer_to_comptime_int_type,
6565 const_slice_u8_type,
6666 enum_literal_type,
67 manyptr_u8_type,
68 manyptr_const_u8_type,
69 atomic_ordering_type,
70 atomic_rmw_op_type,
71 calling_convention_type,
72 float_mode_type,
73 reduce_op_type,
74 call_options_type,
6775
6876 undef,
6977 zero,
......@@ -169,6 +177,14 @@ pub const Value = extern union {
169177 .bool_true,
170178 .bool_false,
171179 .abi_align_default,
180 .manyptr_u8_type,
181 .manyptr_const_u8_type,
182 .atomic_ordering_type,
183 .atomic_rmw_op_type,
184 .calling_convention_type,
185 .float_mode_type,
186 .reduce_op_type,
187 .call_options_type,
172188 => @compileError("Value Tag " ++ @tagName(t) ++ " has no payload"),
173189
174190 .int_big_positive,
......@@ -327,6 +343,14 @@ pub const Value = extern union {
327343 .bool_false,
328344 .empty_struct_value,
329345 .abi_align_default,
346 .manyptr_u8_type,
347 .manyptr_const_u8_type,
348 .atomic_ordering_type,
349 .atomic_rmw_op_type,
350 .calling_convention_type,
351 .float_mode_type,
352 .reduce_op_type,
353 .call_options_type,
330354 => unreachable,
331355
332356 .ty => {
......@@ -474,6 +498,14 @@ pub const Value = extern union {
474498 .single_const_pointer_to_comptime_int_type => return out_stream.writeAll("*const comptime_int"),
475499 .const_slice_u8_type => return out_stream.writeAll("[]const u8"),
476500 .enum_literal_type => return out_stream.writeAll("@Type(.EnumLiteral)"),
501 .manyptr_u8_type => return out_stream.writeAll("[*]u8"),
502 .manyptr_const_u8_type => return out_stream.writeAll("[*]const u8"),
503 .atomic_ordering_type => return out_stream.writeAll("std.builtin.AtomicOrdering"),
504 .atomic_rmw_op_type => return out_stream.writeAll("std.builtin.AtomicRmwOp"),
505 .calling_convention_type => return out_stream.writeAll("std.builtin.CallingConvention"),
506 .float_mode_type => return out_stream.writeAll("std.builtin.FloatMode"),
507 .reduce_op_type => return out_stream.writeAll("std.builtin.ReduceOp"),
508 .call_options_type => return out_stream.writeAll("std.builtin.CallOptions"),
477509 .abi_align_default => return out_stream.writeAll("(default ABI alignment)"),
478510
479511 .empty_struct_value => return out_stream.writeAll("struct {}{}"),
......@@ -595,6 +627,14 @@ pub const Value = extern union {
595627 .single_const_pointer_to_comptime_int_type => Type.initTag(.single_const_pointer_to_comptime_int),
596628 .const_slice_u8_type => Type.initTag(.const_slice_u8),
597629 .enum_literal_type => Type.initTag(.enum_literal),
630 .manyptr_u8_type => Type.initTag(.manyptr_u8),
631 .manyptr_const_u8_type => Type.initTag(.manyptr_const_u8),
632 .atomic_ordering_type => Type.initTag(.atomic_ordering),
633 .atomic_rmw_op_type => Type.initTag(.atomic_rmw_op),
634 .calling_convention_type => Type.initTag(.calling_convention),
635 .float_mode_type => Type.initTag(.float_mode),
636 .reduce_op_type => Type.initTag(.reduce_op),
637 .call_options_type => Type.initTag(.call_options),
598638
599639 .int_type => {
600640 const payload = self.castTag(.int_type).?.data;
......@@ -1132,6 +1172,16 @@ pub const Value = extern union {
11321172 std.hash.autoHash(&hasher, payload.hash());
11331173 },
11341174 .inferred_alloc => unreachable,
1175
1176 .manyptr_u8_type,
1177 .manyptr_const_u8_type,
1178 .atomic_ordering_type,
1179 .atomic_rmw_op_type,
1180 .calling_convention_type,
1181 .float_mode_type,
1182 .reduce_op_type,
1183 .call_options_type,
1184 => @panic("TODO this hash function looks pretty broken. audit it"),
11351185 }
11361186 return hasher.final();
11371187 }
......@@ -1278,6 +1328,14 @@ pub const Value = extern union {
12781328 .single_const_pointer_to_comptime_int_type,
12791329 .const_slice_u8_type,
12801330 .enum_literal_type,
1331 .manyptr_u8_type,
1332 .manyptr_const_u8_type,
1333 .atomic_ordering_type,
1334 .atomic_rmw_op_type,
1335 .calling_convention_type,
1336 .float_mode_type,
1337 .reduce_op_type,
1338 .call_options_type,
12811339 => true,
12821340
12831341 .zero,