authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2021-03-26 23:46:37-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2021-03-26 23:46:37-07:00
log1f5617ac078041532dd5933ae1cf0ccb13c3cd8a
treeab72fff671634323a6993788dab8ba8812ef22f9
parentda731e18c94fdd985812ec27cfdacff5199e55d2

stage2: implement bitwise expr and error literals


6 files changed, 350 insertions(+), 325 deletions(-)

src/Module.zig+23-4
...@@ -1599,7 +1599,7 @@ pub const SrcLoc = struct {...@@ -1599,7 +1599,7 @@ pub const SrcLoc = struct {
1599 const token_starts = tree.tokens.items(.start);1599 const token_starts = tree.tokens.items(.start);
1600 return token_starts[tok_index];1600 return token_starts[tok_index];
1601 },1601 },
1602 .node_offset => |node_off| {1602 .node_offset, .node_offset_bin_op => |node_off| {
1603 const decl = src_loc.container.decl;1603 const decl = src_loc.container.decl;
1604 const node = decl.relativeToNodeIndex(node_off);1604 const node = decl.relativeToNodeIndex(node_off);
1605 const tree = decl.container.file_scope.base.tree();1605 const tree = decl.container.file_scope.base.tree();
...@@ -1660,9 +1660,28 @@ pub const SrcLoc = struct {...@@ -1660,9 +1660,28 @@ pub const SrcLoc = struct {
1660 const token_starts = tree.tokens.items(.start);1660 const token_starts = tree.tokens.items(.start);
1661 return token_starts[tok_index];1661 return token_starts[tok_index];
1662 },1662 },
1663 .node_offset_bin_op => @panic("TODO"),1663 .node_offset_bin_lhs => |node_off| {
1664 .node_offset_bin_lhs => @panic("TODO"),1664 const decl = src_loc.container.decl;
1665 .node_offset_bin_rhs => @panic("TODO"),1665 const node = decl.relativeToNodeIndex(node_off);
1666 const tree = decl.container.file_scope.base.tree();
1667 const node_datas = tree.nodes.items(.data);
1668 const src_node = node_datas[node].lhs;
1669 const main_tokens = tree.nodes.items(.main_token);
1670 const tok_index = main_tokens[src_node];
1671 const token_starts = tree.tokens.items(.start);
1672 return token_starts[tok_index];
1673 },
1674 .node_offset_bin_rhs => |node_off| {
1675 const decl = src_loc.container.decl;
1676 const node = decl.relativeToNodeIndex(node_off);
1677 const tree = decl.container.file_scope.base.tree();
1678 const node_datas = tree.nodes.items(.data);
1679 const src_node = node_datas[node].rhs;
1680 const main_tokens = tree.nodes.items(.main_token);
1681 const tok_index = main_tokens[src_node];
1682 const token_starts = tree.tokens.items(.start);
1683 return token_starts[tok_index];
1684 },
1666 }1685 }
1667 }1686 }
1668};1687};
src/Sema.zig+41-35
...@@ -133,9 +133,9 @@ pub fn analyzeBody(...@@ -133,9 +133,9 @@ pub fn analyzeBody(
133 .as_node => try sema.zirAsNode(block, inst),133 .as_node => try sema.zirAsNode(block, inst),
134 .@"asm" => try sema.zirAsm(block, inst, false),134 .@"asm" => try sema.zirAsm(block, inst, false),
135 .asm_volatile => try sema.zirAsm(block, inst, true),135 .asm_volatile => try sema.zirAsm(block, inst, true),
136 .bit_and => try sema.zirBitwise(block, inst),136 .bit_and => try sema.zirBitwise(block, inst, .bit_and),
137 .bit_not => try sema.zirBitNot(block, inst),137 .bit_not => try sema.zirBitNot(block, inst),
138 .bit_or => try sema.zirBitwise(block, inst),138 .bit_or => try sema.zirBitwise(block, inst, .bit_or),
139 .bitcast => try sema.zirBitcast(block, inst),139 .bitcast => try sema.zirBitcast(block, inst),
140 .bitcast_ref => try sema.zirBitcastRef(block, inst),140 .bitcast_ref => try sema.zirBitcastRef(block, inst),
141 .bitcast_result_ptr => try sema.zirBitcastResultPtr(block, inst),141 .bitcast_result_ptr => try sema.zirBitcastResultPtr(block, inst),
...@@ -227,7 +227,7 @@ pub fn analyzeBody(...@@ -227,7 +227,7 @@ pub fn analyzeBody(
227 .subwrap => try sema.zirArithmetic(block, inst),227 .subwrap => try sema.zirArithmetic(block, inst),
228 .typeof => try sema.zirTypeof(block, inst),228 .typeof => try sema.zirTypeof(block, inst),
229 .typeof_peer => try sema.zirTypeofPeer(block, inst),229 .typeof_peer => try sema.zirTypeofPeer(block, inst),
230 .xor => try sema.zirBitwise(block, inst),230 .xor => try sema.zirBitwise(block, inst, .xor),
231 // TODO231 // TODO
232 //.switchbr => try sema.zirSwitchBr(block, inst, false),232 //.switchbr => try sema.zirSwitchBr(block, inst, false),
233 //.switchbr_ref => try sema.zirSwitchBr(block, inst, true),233 //.switchbr_ref => try sema.zirSwitchBr(block, inst, true),
...@@ -1390,23 +1390,28 @@ fn zirErrorUnionType(sema: *Sema, block: *Scope.Block, inst: zir.Inst.Index) Inn...@@ -1390,23 +1390,28 @@ fn zirErrorUnionType(sema: *Sema, block: *Scope.Block, inst: zir.Inst.Index) Inn
1390 const tracy = trace(@src());1390 const tracy = trace(@src());
1391 defer tracy.end();1391 defer tracy.end();
13921392
1393 const bin_inst = sema.code.instructions.items(.data)[inst].bin;1393 const inst_data = sema.code.instructions.items(.data)[inst].pl_node;
1394 const error_union = try sema.resolveType(block, .unneeded, bin_inst.lhs);1394 const extra = sema.code.extraData(zir.Inst.Bin, inst_data.payload_index).data;
1395 const payload = try sema.resolveType(block, .unneeded, bin_inst.rhs);1395 const src: LazySrcLoc = .{ .node_offset_bin_op = inst_data.src_node };
1396 const lhs_src: LazySrcLoc = .{ .node_offset_bin_lhs = inst_data.src_node };
1397 const rhs_src: LazySrcLoc = .{ .node_offset_bin_rhs = inst_data.src_node };
1398 const error_union = try sema.resolveType(block, lhs_src, extra.lhs);
1399 const payload = try sema.resolveType(block, rhs_src, extra.rhs);
13961400
1397 if (error_union.zigTypeTag() != .ErrorSet) {1401 if (error_union.zigTypeTag() != .ErrorSet) {
1398 return sema.mod.fail(&block.base, .todo, "expected error set type, found {}", .{error_union.elemType()});1402 return sema.mod.fail(&block.base, lhs_src, "expected error set type, found {}", .{
1403 error_union.elemType(),
1404 });
1399 }1405 }
1400 const err_union_ty = try sema.mod.errorUnionType(sema.arena, error_union, payload);1406 const err_union_ty = try sema.mod.errorUnionType(sema.arena, error_union, payload);
14011407 return sema.mod.constType(sema.arena, src, err_union_ty);
1402 return sema.mod.constType(sema.arena, .unneeded, err_union_ty);
1403}1408}
14041409
1405fn zirErrorSet(sema: *Sema, block: *Scope.Block, inst: zir.Inst.Index) InnerError!*Inst {1410fn zirErrorSet(sema: *Sema, block: *Scope.Block, inst: zir.Inst.Index) InnerError!*Inst {
1406 const tracy = trace(@src());1411 const tracy = trace(@src());
1407 defer tracy.end();1412 defer tracy.end();
14081413
1409 if (true) @panic("TODO update zirErrorSet in zir-memory-layout branch");1414 if (true) @panic("TODO update for zir-memory-layout branch");
14101415
1411 // The owner Decl arena will store the hashmap.1416 // The owner Decl arena will store the hashmap.
1412 var new_decl_arena = std.heap.ArenaAllocator.init(sema.gpa);1417 var new_decl_arena = std.heap.ArenaAllocator.init(sema.gpa);
...@@ -1459,19 +1464,21 @@ fn zirMergeErrorSets(sema: *Sema, block: *Scope.Block, inst: zir.Inst.Index) Inn...@@ -1459,19 +1464,21 @@ fn zirMergeErrorSets(sema: *Sema, block: *Scope.Block, inst: zir.Inst.Index) Inn
1459 const tracy = trace(@src());1464 const tracy = trace(@src());
1460 defer tracy.end();1465 defer tracy.end();
14611466
1462 if (true) @panic("TODO update zirMergeErrorSets in zir-memory-layout branch");1467 const inst_data = sema.code.instructions.items(.data)[inst].pl_node;
14631468 const extra = sema.code.extraData(zir.Inst.Bin, inst_data.payload_index).data;
1464 const bin_inst = sema.code.instructions.items(.data)[inst].bin;1469 const src: LazySrcLoc = .{ .node_offset_bin_op = inst_data.src_node };
1465 const lhs_ty = try sema.resolveType(block, .unneeded, bin_inst.lhs);1470 const lhs_src: LazySrcLoc = .{ .node_offset_bin_lhs = inst_data.src_node };
1466 const rhs_ty = try sema.resolveType(block, .unneeded, bin_inst.rhs);1471 const rhs_src: LazySrcLoc = .{ .node_offset_bin_rhs = inst_data.src_node };
1472 const lhs_ty = try sema.resolveType(block, lhs_src, extra.lhs);
1473 const rhs_ty = try sema.resolveType(block, rhs_src, extra.rhs);
1467 if (rhs_ty.zigTypeTag() != .ErrorSet)1474 if (rhs_ty.zigTypeTag() != .ErrorSet)
1468 return sema.mod.fail(&block.base, inst.positionals.rhs.src, "expected error set type, found {}", .{rhs_ty});1475 return sema.mod.fail(&block.base, rhs_src, "expected error set type, found {}", .{rhs_ty});
1469 if (lhs_ty.zigTypeTag() != .ErrorSet)1476 if (lhs_ty.zigTypeTag() != .ErrorSet)
1470 return sema.mod.fail(&block.base, inst.positionals.lhs.src, "expected error set type, found {}", .{lhs_ty});1477 return sema.mod.fail(&block.base, lhs_src, "expected error set type, found {}", .{lhs_ty});
14711478
1472 // anything merged with anyerror is anyerror1479 // anything merged with anyerror is anyerror
1473 if (lhs_ty.tag() == .anyerror or rhs_ty.tag() == .anyerror)1480 if (lhs_ty.tag() == .anyerror or rhs_ty.tag() == .anyerror)
1474 return sema.mod.constInst(sema.arena, inst.base.src, .{1481 return sema.mod.constInst(sema.arena, src, .{
1475 .ty = Type.initTag(.type),1482 .ty = Type.initTag(.type),
1476 .val = Value.initTag(.anyerror_type),1483 .val = Value.initTag(.anyerror_type),
1477 });1484 });
...@@ -1533,7 +1540,7 @@ fn zirMergeErrorSets(sema: *Sema, block: *Scope.Block, inst: zir.Inst.Index) Inn...@@ -1533,7 +1540,7 @@ fn zirMergeErrorSets(sema: *Sema, block: *Scope.Block, inst: zir.Inst.Index) Inn
1533 });1540 });
1534 payload.data.decl = new_decl;1541 payload.data.decl = new_decl;
15351542
1536 return sema.analyzeDeclVal(block, inst.base.src, new_decl);1543 return sema.analyzeDeclVal(block, src, new_decl);
1537}1544}
15381545
1539fn zirEnumLiteral(sema: *Sema, block: *Scope.Block, inst: zir.Inst.Index) InnerError!*Inst {1546fn zirEnumLiteral(sema: *Sema, block: *Scope.Block, inst: zir.Inst.Index) InnerError!*Inst {
...@@ -2442,21 +2449,27 @@ fn zirShr(sema: *Sema, block: *Scope.Block, inst: zir.Inst.Index) InnerError!*In...@@ -2442,21 +2449,27 @@ fn zirShr(sema: *Sema, block: *Scope.Block, inst: zir.Inst.Index) InnerError!*In
2442 return sema.mod.fail(&block.base, sema.src, "TODO implement zirShr", .{});2449 return sema.mod.fail(&block.base, sema.src, "TODO implement zirShr", .{});
2443}2450}
24442451
2445fn zirBitwise(sema: *Sema, block: *Scope.Block, inst: zir.Inst.Index) InnerError!*Inst {2452fn zirBitwise(
2453 sema: *Sema,
2454 block: *Scope.Block,
2455 inst: zir.Inst.Index,
2456 ir_tag: ir.Inst.Tag,
2457) InnerError!*Inst {
2446 const tracy = trace(@src());2458 const tracy = trace(@src());
2447 defer tracy.end();2459 defer tracy.end();
24482460
2449 if (true) @panic("TODO rework with zir-memory-layout in mind");2461 const inst_data = sema.code.instructions.items(.data)[inst].pl_node;
24502462 const src: LazySrcLoc = .{ .node_offset_bin_op = inst_data.src_node };
2451 const bin_inst = sema.code.instructions.items(.data)[inst].bin;2463 const lhs_src: LazySrcLoc = .{ .node_offset_bin_lhs = inst_data.src_node };
2452 const src: LazySrcLoc = .todo;2464 const rhs_src: LazySrcLoc = .{ .node_offset_bin_rhs = inst_data.src_node };
2453 const lhs = try sema.resolveInst(bin_inst.lhs);2465 const extra = sema.code.extraData(zir.Inst.Bin, inst_data.payload_index).data;
2454 const rhs = try sema.resolveInst(bin_inst.rhs);2466 const lhs = try sema.resolveInst(extra.lhs);
2467 const rhs = try sema.resolveInst(extra.rhs);
24552468
2456 const instructions = &[_]*Inst{ lhs, rhs };2469 const instructions = &[_]*Inst{ lhs, rhs };
2457 const resolved_type = try sema.resolvePeerTypes(block, src, instructions);2470 const resolved_type = try sema.resolvePeerTypes(block, src, instructions);
2458 const casted_lhs = try sema.coerce(block, resolved_type, lhs, lhs.src);2471 const casted_lhs = try sema.coerce(block, resolved_type, lhs, lhs_src);
2459 const casted_rhs = try sema.coerce(block, resolved_type, rhs, rhs.src);2472 const casted_rhs = try sema.coerce(block, resolved_type, rhs, rhs_src);
24602473
2461 const scalar_type = if (resolved_type.zigTypeTag() == .Vector)2474 const scalar_type = if (resolved_type.zigTypeTag() == .Vector)
2462 resolved_type.elemType()2475 resolved_type.elemType()
...@@ -2499,13 +2512,6 @@ fn zirBitwise(sema: *Sema, block: *Scope.Block, inst: zir.Inst.Index) InnerError...@@ -2499,13 +2512,6 @@ fn zirBitwise(sema: *Sema, block: *Scope.Block, inst: zir.Inst.Index) InnerError
2499 }2512 }
25002513
2501 try sema.requireRuntimeBlock(block, src);2514 try sema.requireRuntimeBlock(block, src);
2502 const ir_tag = switch (inst.base.tag) {
2503 .bit_and => Inst.Tag.bit_and,
2504 .bit_or => Inst.Tag.bit_or,
2505 .xor => Inst.Tag.xor,
2506 else => unreachable,
2507 };
2508
2509 return block.addBinOp(src, scalar_type, ir_tag, casted_lhs, casted_rhs);2515 return block.addBinOp(src, scalar_type, ir_tag, casted_lhs, casted_rhs);
2510}2516}
25112517
src/astgen.zig+25-34
...@@ -367,6 +367,9 @@ pub fn expr(mod: *Module, scope: *Scope, rl: ResultLoc, node: ast.Node.Index) In...@@ -367,6 +367,9 @@ pub fn expr(mod: *Module, scope: *Scope, rl: ResultLoc, node: ast.Node.Index) In
367 .array_cat => return simpleBinOp(mod, scope, rl, node, .array_cat),367 .array_cat => return simpleBinOp(mod, scope, rl, node, .array_cat),
368 .array_mult => return simpleBinOp(mod, scope, rl, node, .array_mul),368 .array_mult => return simpleBinOp(mod, scope, rl, node, .array_mul),
369369
370 .error_union => return simpleBinOp(mod, scope, rl, node, .error_union_type),
371 .merge_error_sets => return simpleBinOp(mod, scope, rl, node, .merge_error_sets),
372
370 .bool_and => return boolBinOp(mod, scope, rl, node, .bool_br_and),373 .bool_and => return boolBinOp(mod, scope, rl, node, .bool_br_and),
371 .bool_or => return boolBinOp(mod, scope, rl, node, .bool_br_or),374 .bool_or => return boolBinOp(mod, scope, rl, node, .bool_br_or),
372375
...@@ -515,40 +518,11 @@ pub fn expr(mod: *Module, scope: *Scope, rl: ResultLoc, node: ast.Node.Index) In...@@ -515,40 +518,11 @@ pub fn expr(mod: *Module, scope: *Scope, rl: ResultLoc, node: ast.Node.Index) In
515 const statements = tree.extra_data[node_datas[node].lhs..node_datas[node].rhs];518 const statements = tree.extra_data[node_datas[node].lhs..node_datas[node].rhs];
516 return blockExpr(mod, scope, rl, node, statements);519 return blockExpr(mod, scope, rl, node, statements);
517 },520 },
518 .enum_literal => {521 .enum_literal => return simpleStrTok(mod, scope, rl, main_tokens[node], node, .enum_literal),
519 const ident_token = main_tokens[node];522 .error_value => return simpleStrTok(mod, scope, rl, node_datas[node].rhs, node, .error_value),
520 const string_bytes = &gz.zir_code.string_bytes;
521 const str_index = @intCast(u32, string_bytes.items.len);
522 try mod.appendIdentStr(scope, ident_token, string_bytes);
523 try string_bytes.append(mod.gpa, 0);
524 const result = try gz.addStrTok(.enum_literal, str_index, ident_token);
525 return rvalue(mod, scope, rl, result, node);
526 },
527 .error_value => {
528 if (true) @panic("TODO update for zir-memory-layout");
529 const ident_token = node_datas[node].rhs;
530 const name = try mod.identifierTokenString(scope, ident_token);
531 const result = try addZirInstTag(mod, scope, src, .error_value, .{ .name = name });
532 return rvalue(mod, scope, rl, result);
533 },
534 .error_union => {
535 if (true) @panic("TODO update for zir-memory-layout");
536 const error_set = try typeExpr(mod, scope, node_datas[node].lhs);
537 const payload = try typeExpr(mod, scope, node_datas[node].rhs);
538 const result = try addZIRBinOp(mod, scope, src, .error_union_type, error_set, payload);
539 return rvalue(mod, scope, rl, result);
540 },
541 .merge_error_sets => {
542 if (true) @panic("TODO update for zir-memory-layout");
543 const lhs = try typeExpr(mod, scope, node_datas[node].lhs);
544 const rhs = try typeExpr(mod, scope, node_datas[node].rhs);
545 const result = try addZIRBinOp(mod, scope, src, .merge_error_sets, lhs, rhs);
546 return rvalue(mod, scope, rl, result);
547 },
548 .anyframe_literal => return mod.failNode(scope, node, "async and related features are not yet supported", .{}),523 .anyframe_literal => return mod.failNode(scope, node, "async and related features are not yet supported", .{}),
549 .anyframe_type => return mod.failNode(scope, node, "async and related features are not yet supported", .{}),524 .anyframe_type => return mod.failNode(scope, node, "async and related features are not yet supported", .{}),
550 .@"catch" => {525 .@"catch" => {
551 if (true) @panic("TODO update for zir-memory-layout");
552 const catch_token = main_tokens[node];526 const catch_token = main_tokens[node];
553 const payload_token: ?ast.TokenIndex = if (token_tags[catch_token + 1] == .pipe)527 const payload_token: ?ast.TokenIndex = if (token_tags[catch_token + 1] == .pipe)
554 catch_token + 2528 catch_token + 2
...@@ -1597,7 +1571,6 @@ fn containerDecl(...@@ -1597,7 +1571,6 @@ fn containerDecl(
1597 rl: ResultLoc,1571 rl: ResultLoc,
1598 container_decl: ast.full.ContainerDecl,1572 container_decl: ast.full.ContainerDecl,
1599) InnerError!zir.Inst.Ref {1573) InnerError!zir.Inst.Ref {
1600 if (true) @panic("TODO update for zir-memory-layout");
1601 return mod.failTok(scope, container_decl.ast.main_token, "TODO implement container decls", .{});1574 return mod.failTok(scope, container_decl.ast.main_token, "TODO implement container decls", .{});
1602}1575}
16031576
...@@ -1607,8 +1580,9 @@ fn errorSetDecl(...@@ -1607,8 +1580,9 @@ fn errorSetDecl(
1607 rl: ResultLoc,1580 rl: ResultLoc,
1608 node: ast.Node.Index,1581 node: ast.Node.Index,
1609) InnerError!zir.Inst.Ref {1582) InnerError!zir.Inst.Ref {
1610 if (true) @panic("TODO update for zir-memory-layout");1583 if (true) @panic("TODO update for zir-memory-layout branch");
1611 const tree = scope.tree();1584 const gz = scope.getGenZir();
1585 const tree = gz.tree();
1612 const main_tokens = tree.nodes.items(.main_token);1586 const main_tokens = tree.nodes.items(.main_token);
1613 const token_tags = tree.tokens.items(.tag);1587 const token_tags = tree.tokens.items(.tag);
16141588
...@@ -1905,6 +1879,23 @@ fn simpleBinOp(...@@ -1905,6 +1879,23 @@ fn simpleBinOp(
1905 return rvalue(mod, scope, rl, result, node);1879 return rvalue(mod, scope, rl, result, node);
1906}1880}
19071881
1882fn simpleStrTok(
1883 mod: *Module,
1884 scope: *Scope,
1885 rl: ResultLoc,
1886 ident_token: ast.TokenIndex,
1887 node: ast.Node.Index,
1888 op_inst_tag: zir.Inst.Tag,
1889) InnerError!zir.Inst.Ref {
1890 const gz = scope.getGenZir();
1891 const string_bytes = &gz.zir_code.string_bytes;
1892 const str_index = @intCast(u32, string_bytes.items.len);
1893 try mod.appendIdentStr(scope, ident_token, string_bytes);
1894 try string_bytes.append(mod.gpa, 0);
1895 const result = try gz.addStrTok(op_inst_tag, str_index, ident_token);
1896 return rvalue(mod, scope, rl, result, node);
1897}
1898
1908fn boolBinOp(1899fn boolBinOp(
1909 mod: *Module,1900 mod: *Module,
1910 scope: *Scope,1901 scope: *Scope,
src/zir.zig+10-5
...@@ -312,8 +312,12 @@ pub const Inst = struct {...@@ -312,8 +312,12 @@ pub const Inst = struct {
312 /// Uses the `un_node` field.312 /// Uses the `un_node` field.
313 ensure_result_non_error,313 ensure_result_non_error,
314 /// Create a `E!T` type.314 /// Create a `E!T` type.
315 /// Uses the `pl_node` field with `Bin` payload.
315 error_union_type,316 error_union_type,
316 /// Create an error set. extra[lhs..rhs]. The values are token index offsets.317 /// Create an error set. TODO can't we just do this in astgen? reconsider
318 /// memory layout of error sets. if astgen wants to make Sema do the work,
319 /// this ZIR instruction could just be an AST node index. If astgen wants to
320 /// do the work, it could use a const instruction.
317 error_set,321 error_set,
318 /// `error.Foo` syntax. Uses the `str_tok` field of the Data union.322 /// `error.Foo` syntax. Uses the `str_tok` field of the Data union.
319 error_value,323 error_value,
...@@ -393,6 +397,7 @@ pub const Inst = struct {...@@ -393,6 +397,7 @@ pub const Inst = struct {
393 /// Uses the `node` field.397 /// Uses the `node` field.
394 repeat_inline,398 repeat_inline,
395 /// Merge two error sets into one, `E1 || E2`.399 /// Merge two error sets into one, `E1 || E2`.
400 /// Uses the `pl_node` field with payload `Bin`.
396 merge_error_sets,401 merge_error_sets,
397 /// Ambiguously remainder division or modulus. If the computation would possibly have402 /// Ambiguously remainder division or modulus. If the computation would possibly have
398 /// a different value depending on whether the operation is remainder division or modulus,403 /// a different value depending on whether the operation is remainder division or modulus,
...@@ -1368,14 +1373,11 @@ const Writer = struct {...@@ -1368,14 +1373,11 @@ const Writer = struct {
1368 try stream.print("= {s}(", .{@tagName(tags[inst])});1373 try stream.print("= {s}(", .{@tagName(tags[inst])});
1369 switch (tag) {1374 switch (tag) {
1370 .array_type,1375 .array_type,
1371 .bit_and,
1372 .bit_or,
1373 .as,1376 .as,
1374 .coerce_result_ptr,1377 .coerce_result_ptr,
1375 .elem_ptr,1378 .elem_ptr,
1376 .elem_val,1379 .elem_val,
1377 .intcast,1380 .intcast,
1378 .merge_error_sets,
1379 .store,1381 .store,
1380 .store_to_block_ptr,1382 .store_to_block_ptr,
1381 => try self.writeBin(stream, inst),1383 => try self.writeBin(stream, inst),
...@@ -1481,6 +1483,10 @@ const Writer = struct {...@@ -1481,6 +1483,10 @@ const Writer = struct {
1481 .shr,1483 .shr,
1482 .xor,1484 .xor,
1483 .store_node,1485 .store_node,
1486 .error_union_type,
1487 .merge_error_sets,
1488 .bit_and,
1489 .bit_or,
1484 => try self.writePlNodeBin(stream, inst),1490 => try self.writePlNodeBin(stream, inst),
14851491
1486 .call,1492 .call,
...@@ -1530,7 +1536,6 @@ const Writer = struct {...@@ -1530,7 +1536,6 @@ const Writer = struct {
1530 .bitcast,1536 .bitcast,
1531 .bitcast_ref,1537 .bitcast_ref,
1532 .bitcast_result_ptr,1538 .bitcast_result_ptr,
1533 .error_union_type,
1534 .error_set,1539 .error_set,
1535 .store_to_inferred_ptr,1540 .store_to_inferred_ptr,
1536 => try stream.writeAll("TODO)"),1541 => try stream.writeAll("TODO)"),
test/stage2/arm.zig+93-93
...@@ -184,103 +184,103 @@ pub fn addCases(ctx: *TestContext) !void {...@@ -184,103 +184,103 @@ pub fn addCases(ctx: *TestContext) !void {
184 );184 );
185185
186 // Bitwise And186 // Bitwise And
187 //case.addCompareOutput(187 case.addCompareOutput(
188 // \\export fn _start() noreturn {188 \\export fn _start() noreturn {
189 // \\ print(8, 9);189 \\ print(8, 9);
190 // \\ print(3, 7);190 \\ print(3, 7);
191 // \\ exit();191 \\ exit();
192 // \\}192 \\}
193 // \\193 \\
194 // \\fn print(a: u32, b: u32) void {194 \\fn print(a: u32, b: u32) void {
195 // \\ asm volatile ("svc #0"195 \\ asm volatile ("svc #0"
196 // \\ :196 \\ :
197 // \\ : [number] "{r7}" (4),197 \\ : [number] "{r7}" (4),
198 // \\ [arg3] "{r2}" (a & b),198 \\ [arg3] "{r2}" (a & b),
199 // \\ [arg1] "{r0}" (1),199 \\ [arg1] "{r0}" (1),
200 // \\ [arg2] "{r1}" (@ptrToInt("123456789"))200 \\ [arg2] "{r1}" (@ptrToInt("123456789"))
201 // \\ : "memory"201 \\ : "memory"
202 // \\ );202 \\ );
203 // \\ return;203 \\ return;
204 // \\}204 \\}
205 // \\205 \\
206 // \\fn exit() noreturn {206 \\fn exit() noreturn {
207 // \\ asm volatile ("svc #0"207 \\ asm volatile ("svc #0"
208 // \\ :208 \\ :
209 // \\ : [number] "{r7}" (1),209 \\ : [number] "{r7}" (1),
210 // \\ [arg1] "{r0}" (0)210 \\ [arg1] "{r0}" (0)
211 // \\ : "memory"211 \\ : "memory"
212 // \\ );212 \\ );
213 // \\ unreachable;213 \\ unreachable;
214 // \\}214 \\}
215 //,215 ,
216 // "12345678123",216 "12345678123",
217 //);217 );
218218
219 // Bitwise Or219 // Bitwise Or
220 //case.addCompareOutput(220 case.addCompareOutput(
221 // \\export fn _start() noreturn {221 \\export fn _start() noreturn {
222 // \\ print(4, 2);222 \\ print(4, 2);
223 // \\ print(3, 7);223 \\ print(3, 7);
224 // \\ exit();224 \\ exit();
225 // \\}225 \\}
226 // \\226 \\
227 // \\fn print(a: u32, b: u32) void {227 \\fn print(a: u32, b: u32) void {
228 // \\ asm volatile ("svc #0"228 \\ asm volatile ("svc #0"
229 // \\ :229 \\ :
230 // \\ : [number] "{r7}" (4),230 \\ : [number] "{r7}" (4),
231 // \\ [arg3] "{r2}" (a | b),231 \\ [arg3] "{r2}" (a | b),
232 // \\ [arg1] "{r0}" (1),232 \\ [arg1] "{r0}" (1),
233 // \\ [arg2] "{r1}" (@ptrToInt("123456789"))233 \\ [arg2] "{r1}" (@ptrToInt("123456789"))
234 // \\ : "memory"234 \\ : "memory"
235 // \\ );235 \\ );
236 // \\ return;236 \\ return;
237 // \\}237 \\}
238 // \\238 \\
239 // \\fn exit() noreturn {239 \\fn exit() noreturn {
240 // \\ asm volatile ("svc #0"240 \\ asm volatile ("svc #0"
241 // \\ :241 \\ :
242 // \\ : [number] "{r7}" (1),242 \\ : [number] "{r7}" (1),
243 // \\ [arg1] "{r0}" (0)243 \\ [arg1] "{r0}" (0)
244 // \\ : "memory"244 \\ : "memory"
245 // \\ );245 \\ );
246 // \\ unreachable;246 \\ unreachable;
247 // \\}247 \\}
248 //,248 ,
249 // "1234561234567",249 "1234561234567",
250 //);250 );
251251
252 // Bitwise Xor252 // Bitwise Xor
253 //case.addCompareOutput(253 case.addCompareOutput(
254 // \\export fn _start() noreturn {254 \\export fn _start() noreturn {
255 // \\ print(42, 42);255 \\ print(42, 42);
256 // \\ print(3, 5);256 \\ print(3, 5);
257 // \\ exit();257 \\ exit();
258 // \\}258 \\}
259 // \\259 \\
260 // \\fn print(a: u32, b: u32) void {260 \\fn print(a: u32, b: u32) void {
261 // \\ asm volatile ("svc #0"261 \\ asm volatile ("svc #0"
262 // \\ :262 \\ :
263 // \\ : [number] "{r7}" (4),263 \\ : [number] "{r7}" (4),
264 // \\ [arg3] "{r2}" (a ^ b),264 \\ [arg3] "{r2}" (a ^ b),
265 // \\ [arg1] "{r0}" (1),265 \\ [arg1] "{r0}" (1),
266 // \\ [arg2] "{r1}" (@ptrToInt("123456789"))266 \\ [arg2] "{r1}" (@ptrToInt("123456789"))
267 // \\ : "memory"267 \\ : "memory"
268 // \\ );268 \\ );
269 // \\ return;269 \\ return;
270 // \\}270 \\}
271 // \\271 \\
272 // \\fn exit() noreturn {272 \\fn exit() noreturn {
273 // \\ asm volatile ("svc #0"273 \\ asm volatile ("svc #0"
274 // \\ :274 \\ :
275 // \\ : [number] "{r7}" (1),275 \\ : [number] "{r7}" (1),
276 // \\ [arg1] "{r0}" (0)276 \\ [arg1] "{r0}" (0)
277 // \\ : "memory"277 \\ : "memory"
278 // \\ );278 \\ );
279 // \\ unreachable;279 \\ unreachable;
280 // \\}280 \\}
281 //,281 ,
282 // "123456",282 "123456",
283 //);283 );
284 }284 }
285285
286 {286 {
test/stage2/test.zig+158-154
...@@ -1356,53 +1356,53 @@ pub fn addCases(ctx: *TestContext) !void {...@@ -1356,53 +1356,53 @@ pub fn addCases(ctx: *TestContext) !void {
1356 \\}1356 \\}
1357 , &[_][]const u8{":8:21: error: evaluation exceeded 1000 backwards branches"});1357 , &[_][]const u8{":8:21: error: evaluation exceeded 1000 backwards branches"});
1358 }1358 }
1359 //{1359 {
1360 // var case = ctx.exe("orelse at comptime", linux_x64);1360 var case = ctx.exe("orelse at comptime", linux_x64);
1361 // case.addCompareOutput(1361 case.addCompareOutput(
1362 // \\export fn _start() noreturn {1362 \\export fn _start() noreturn {
1363 // \\ const i: ?u64 = 0;1363 \\ const i: ?u64 = 0;
1364 // \\ const orelsed = i orelse 5;1364 \\ const orelsed = i orelse 5;
1365 // \\ assert(orelsed == 0);1365 \\ assert(orelsed == 0);
1366 // \\ exit();1366 \\ exit();
1367 // \\}1367 \\}
1368 // \\fn assert(b: bool) void {1368 \\fn assert(b: bool) void {
1369 // \\ if (!b) unreachable;1369 \\ if (!b) unreachable;
1370 // \\}1370 \\}
1371 // \\fn exit() noreturn {1371 \\fn exit() noreturn {
1372 // \\ asm volatile ("syscall"1372 \\ asm volatile ("syscall"
1373 // \\ :1373 \\ :
1374 // \\ : [number] "{rax}" (231),1374 \\ : [number] "{rax}" (231),
1375 // \\ [arg1] "{rdi}" (0)1375 \\ [arg1] "{rdi}" (0)
1376 // \\ : "rcx", "r11", "memory"1376 \\ : "rcx", "r11", "memory"
1377 // \\ );1377 \\ );
1378 // \\ unreachable;1378 \\ unreachable;
1379 // \\}1379 \\}
1380 // ,1380 ,
1381 // "",1381 "",
1382 // );1382 );
1383 // case.addCompareOutput(1383 case.addCompareOutput(
1384 // \\export fn _start() noreturn {1384 \\export fn _start() noreturn {
1385 // \\ const i: ?u64 = null;1385 \\ const i: ?u64 = null;
1386 // \\ const orelsed = i orelse 5;1386 \\ const orelsed = i orelse 5;
1387 // \\ assert(orelsed == 5);1387 \\ assert(orelsed == 5);
1388 // \\ exit();1388 \\ exit();
1389 // \\}1389 \\}
1390 // \\fn assert(b: bool) void {1390 \\fn assert(b: bool) void {
1391 // \\ if (!b) unreachable;1391 \\ if (!b) unreachable;
1392 // \\}1392 \\}
1393 // \\fn exit() noreturn {1393 \\fn exit() noreturn {
1394 // \\ asm volatile ("syscall"1394 \\ asm volatile ("syscall"
1395 // \\ :1395 \\ :
1396 // \\ : [number] "{rax}" (231),1396 \\ : [number] "{rax}" (231),
1397 // \\ [arg1] "{rdi}" (0)1397 \\ [arg1] "{rdi}" (0)
1398 // \\ : "rcx", "r11", "memory"1398 \\ : "rcx", "r11", "memory"
1399 // \\ );1399 \\ );
1400 // \\ unreachable;1400 \\ unreachable;
1401 // \\}1401 \\}
1402 // ,1402 ,
1403 // "",1403 "",
1404 // );1404 );
1405 //}1405 }
14061406
1407 {1407 {
1408 var case = ctx.exe("only 1 function and it gets updated", linux_x64);1408 var case = ctx.exe("only 1 function and it gets updated", linux_x64);
...@@ -1454,113 +1454,117 @@ pub fn addCases(ctx: *TestContext) !void {...@@ -1454,113 +1454,117 @@ pub fn addCases(ctx: *TestContext) !void {
1454 "",1454 "",
1455 );1455 );
1456 }1456 }
1457 //{1457 {
1458 // var case = ctx.exe("catch at comptime", linux_x64);1458 var case = ctx.exe("catch at comptime", linux_x64);
1459 // case.addCompareOutput(1459 case.addCompareOutput(
1460 // \\export fn _start() noreturn {1460 \\export fn _start() noreturn {
1461 // \\ const i: anyerror!u64 = 0;1461 \\ const i: anyerror!u64 = 0;
1462 // \\ const caught = i catch 5;1462 \\ const caught = i catch 5;
1463 // \\ assert(caught == 0);1463 \\ assert(caught == 0);
1464 // \\ exit();1464 \\ exit();
1465 // \\}1465 \\}
1466 // \\fn assert(b: bool) void {1466 \\fn assert(b: bool) void {
1467 // \\ if (!b) unreachable;1467 \\ if (!b) unreachable;
1468 // \\}1468 \\}
1469 // \\fn exit() noreturn {1469 \\fn exit() noreturn {
1470 // \\ asm volatile ("syscall"1470 \\ asm volatile ("syscall"
1471 // \\ :1471 \\ :
1472 // \\ : [number] "{rax}" (231),1472 \\ : [number] "{rax}" (231),
1473 // \\ [arg1] "{rdi}" (0)1473 \\ [arg1] "{rdi}" (0)
1474 // \\ : "rcx", "r11", "memory"1474 \\ : "rcx", "r11", "memory"
1475 // \\ );1475 \\ );
1476 // \\ unreachable;1476 \\ unreachable;
1477 // \\}1477 \\}
1478 // ,1478 ,
1479 // "",1479 "",
1480 // );1480 );
1481 // case.addCompareOutput(1481
1482 // \\export fn _start() noreturn {1482 case.addCompareOutput(
1483 // \\ const i: anyerror!u64 = error.B;1483 \\export fn _start() noreturn {
1484 // \\ const caught = i catch 5;1484 \\ const i: anyerror!u64 = error.B;
1485 // \\ assert(caught == 5);1485 \\ const caught = i catch 5;
1486 // \\ exit();1486 \\ assert(caught == 5);
1487 // \\}1487 \\ exit();
1488 // \\fn assert(b: bool) void {1488 \\}
1489 // \\ if (!b) unreachable;1489 \\fn assert(b: bool) void {
1490 // \\}1490 \\ if (!b) unreachable;
1491 // \\fn exit() noreturn {1491 \\}
1492 // \\ asm volatile ("syscall"1492 \\fn exit() noreturn {
1493 // \\ :1493 \\ asm volatile ("syscall"
1494 // \\ : [number] "{rax}" (231),1494 \\ :
1495 // \\ [arg1] "{rdi}" (0)1495 \\ : [number] "{rax}" (231),
1496 // \\ : "rcx", "r11", "memory"1496 \\ [arg1] "{rdi}" (0)
1497 // \\ );1497 \\ : "rcx", "r11", "memory"
1498 // \\ unreachable;1498 \\ );
1499 // \\}1499 \\ unreachable;
1500 // ,1500 \\}
1501 // "",1501 ,
1502 // );1502 "",
1503 // case.addCompareOutput(1503 );
1504 // \\export fn _start() noreturn {1504
1505 // \\ const a: anyerror!comptime_int = 42;1505 //case.addCompareOutput(
1506 // \\ const b: *const comptime_int = &(a catch unreachable);1506 // \\export fn _start() noreturn {
1507 // \\ assert(b.* == 42);1507 // \\ const a: anyerror!comptime_int = 42;
1508 // \\1508 // \\ const b: *const comptime_int = &(a catch unreachable);
1509 // \\ exit();1509 // \\ assert(b.* == 42);
1510 // \\}1510 // \\
1511 // \\fn assert(b: bool) void {1511 // \\ exit();
1512 // \\ if (!b) unreachable; // assertion failure1512 // \\}
1513 // \\}1513 // \\fn assert(b: bool) void {
1514 // \\fn exit() noreturn {1514 // \\ if (!b) unreachable; // assertion failure
1515 // \\ asm volatile ("syscall"1515 // \\}
1516 // \\ :1516 // \\fn exit() noreturn {
1517 // \\ : [number] "{rax}" (231),1517 // \\ asm volatile ("syscall"
1518 // \\ [arg1] "{rdi}" (0)1518 // \\ :
1519 // \\ : "rcx", "r11", "memory"1519 // \\ : [number] "{rax}" (231),
1520 // \\ );1520 // \\ [arg1] "{rdi}" (0)
1521 // \\ unreachable;1521 // \\ : "rcx", "r11", "memory"
1522 // \\}1522 // \\ );
1523 // , "");1523 // \\ unreachable;
1524 // case.addCompareOutput(1524 // \\}
1525 // \\export fn _start() noreturn {1525 //, "");
1526 // \\const a: anyerror!u32 = error.B;1526
1527 // \\_ = &(a catch |err| assert(err == error.B));1527 case.addCompareOutput(
1528 // \\exit();1528 \\export fn _start() noreturn {
1529 // \\}1529 \\ const a: anyerror!u32 = error.B;
1530 // \\fn assert(b: bool) void {1530 \\ _ = &(a catch |err| assert(err == error.B));
1531 // \\ if (!b) unreachable;1531 \\ exit();
1532 // \\}1532 \\}
1533 // \\fn exit() noreturn {1533 \\fn assert(b: bool) void {
1534 // \\ asm volatile ("syscall"1534 \\ if (!b) unreachable;
1535 // \\ :1535 \\}
1536 // \\ : [number] "{rax}" (231),1536 \\fn exit() noreturn {
1537 // \\ [arg1] "{rdi}" (0)1537 \\ asm volatile ("syscall"
1538 // \\ : "rcx", "r11", "memory"1538 \\ :
1539 // \\ );1539 \\ : [number] "{rax}" (231),
1540 // \\ unreachable;1540 \\ [arg1] "{rdi}" (0)
1541 // \\}1541 \\ : "rcx", "r11", "memory"
1542 // , "");1542 \\ );
1543 // case.addCompareOutput(1543 \\ unreachable;
1544 // \\export fn _start() noreturn {1544 \\}
1545 // \\ const a: anyerror!u32 = error.Bar;1545 , "");
1546 // \\ a catch |err| assert(err == error.Bar);1546
1547 // \\1547 case.addCompareOutput(
1548 // \\ exit();1548 \\export fn _start() noreturn {
1549 // \\}1549 \\ const a: anyerror!u32 = error.Bar;
1550 // \\fn assert(b: bool) void {1550 \\ a catch |err| assert(err == error.Bar);
1551 // \\ if (!b) unreachable;1551 \\
1552 // \\}1552 \\ exit();
1553 // \\fn exit() noreturn {1553 \\}
1554 // \\ asm volatile ("syscall"1554 \\fn assert(b: bool) void {
1555 // \\ :1555 \\ if (!b) unreachable;
1556 // \\ : [number] "{rax}" (231),1556 \\}
1557 // \\ [arg1] "{rdi}" (0)1557 \\fn exit() noreturn {
1558 // \\ : "rcx", "r11", "memory"1558 \\ asm volatile ("syscall"
1559 // \\ );1559 \\ :
1560 // \\ unreachable;1560 \\ : [number] "{rax}" (231),
1561 // \\}1561 \\ [arg1] "{rdi}" (0)
1562 // , "");1562 \\ : "rcx", "r11", "memory"
1563 //}1563 \\ );
1564 \\ unreachable;
1565 \\}
1566 , "");
1567 }
1564 //{1568 //{
1565 // var case = ctx.exe("merge error sets", linux_x64);1569 // var case = ctx.exe("merge error sets", linux_x64);
15661570