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 {
15991599 const token_starts = tree.tokens.items(.start);
16001600 return token_starts[tok_index];
16011601 },
1602 .node_offset => |node_off| {
1602 .node_offset, .node_offset_bin_op => |node_off| {
16031603 const decl = src_loc.container.decl;
16041604 const node = decl.relativeToNodeIndex(node_off);
16051605 const tree = decl.container.file_scope.base.tree();
......@@ -1660,9 +1660,28 @@ pub const SrcLoc = struct {
16601660 const token_starts = tree.tokens.items(.start);
16611661 return token_starts[tok_index];
16621662 },
1663 .node_offset_bin_op => @panic("TODO"),
1664 .node_offset_bin_lhs => @panic("TODO"),
1665 .node_offset_bin_rhs => @panic("TODO"),
1663 .node_offset_bin_lhs => |node_off| {
1664 const decl = src_loc.container.decl;
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 },
16661685 }
16671686 }
16681687};
src/Sema.zig+41-35
......@@ -133,9 +133,9 @@ pub fn analyzeBody(
133133 .as_node => try sema.zirAsNode(block, inst),
134134 .@"asm" => try sema.zirAsm(block, inst, false),
135135 .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),
137137 .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),
139139 .bitcast => try sema.zirBitcast(block, inst),
140140 .bitcast_ref => try sema.zirBitcastRef(block, inst),
141141 .bitcast_result_ptr => try sema.zirBitcastResultPtr(block, inst),
......@@ -227,7 +227,7 @@ pub fn analyzeBody(
227227 .subwrap => try sema.zirArithmetic(block, inst),
228228 .typeof => try sema.zirTypeof(block, inst),
229229 .typeof_peer => try sema.zirTypeofPeer(block, inst),
230 .xor => try sema.zirBitwise(block, inst),
230 .xor => try sema.zirBitwise(block, inst, .xor),
231231 // TODO
232232 //.switchbr => try sema.zirSwitchBr(block, inst, false),
233233 //.switchbr_ref => try sema.zirSwitchBr(block, inst, true),
......@@ -1390,23 +1390,28 @@ fn zirErrorUnionType(sema: *Sema, block: *Scope.Block, inst: zir.Inst.Index) Inn
13901390 const tracy = trace(@src());
13911391 defer tracy.end();
13921392
1393 const bin_inst = sema.code.instructions.items(.data)[inst].bin;
1394 const error_union = try sema.resolveType(block, .unneeded, bin_inst.lhs);
1395 const payload = try sema.resolveType(block, .unneeded, bin_inst.rhs);
1393 const inst_data = sema.code.instructions.items(.data)[inst].pl_node;
1394 const extra = sema.code.extraData(zir.Inst.Bin, inst_data.payload_index).data;
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
13971401 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 });
13991405 }
14001406 const err_union_ty = try sema.mod.errorUnionType(sema.arena, error_union, payload);
1401
1402 return sema.mod.constType(sema.arena, .unneeded, err_union_ty);
1407 return sema.mod.constType(sema.arena, src, err_union_ty);
14031408}
14041409
14051410fn zirErrorSet(sema: *Sema, block: *Scope.Block, inst: zir.Inst.Index) InnerError!*Inst {
14061411 const tracy = trace(@src());
14071412 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
14111416 // The owner Decl arena will store the hashmap.
14121417 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
14591464 const tracy = trace(@src());
14601465 defer tracy.end();
14611466
1462 if (true) @panic("TODO update zirMergeErrorSets in zir-memory-layout branch");
1463
1464 const bin_inst = sema.code.instructions.items(.data)[inst].bin;
1465 const lhs_ty = try sema.resolveType(block, .unneeded, bin_inst.lhs);
1466 const rhs_ty = try sema.resolveType(block, .unneeded, bin_inst.rhs);
1467 const inst_data = sema.code.instructions.items(.data)[inst].pl_node;
1468 const extra = sema.code.extraData(zir.Inst.Bin, inst_data.payload_index).data;
1469 const src: LazySrcLoc = .{ .node_offset_bin_op = inst_data.src_node };
1470 const lhs_src: LazySrcLoc = .{ .node_offset_bin_lhs = inst_data.src_node };
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);
14671474 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});
14691476 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
14721479 // anything merged with anyerror is anyerror
14731480 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, .{
14751482 .ty = Type.initTag(.type),
14761483 .val = Value.initTag(.anyerror_type),
14771484 });
......@@ -1533,7 +1540,7 @@ fn zirMergeErrorSets(sema: *Sema, block: *Scope.Block, inst: zir.Inst.Index) Inn
15331540 });
15341541 payload.data.decl = new_decl;
15351542
1536 return sema.analyzeDeclVal(block, inst.base.src, new_decl);
1543 return sema.analyzeDeclVal(block, src, new_decl);
15371544}
15381545
15391546fn 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
24422449 return sema.mod.fail(&block.base, sema.src, "TODO implement zirShr", .{});
24432450}
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 {
24462458 const tracy = trace(@src());
24472459 defer tracy.end();
24482460
2449 if (true) @panic("TODO rework with zir-memory-layout in mind");
2450
2451 const bin_inst = sema.code.instructions.items(.data)[inst].bin;
2452 const src: LazySrcLoc = .todo;
2453 const lhs = try sema.resolveInst(bin_inst.lhs);
2454 const rhs = try sema.resolveInst(bin_inst.rhs);
2461 const inst_data = sema.code.instructions.items(.data)[inst].pl_node;
2462 const src: LazySrcLoc = .{ .node_offset_bin_op = inst_data.src_node };
2463 const lhs_src: LazySrcLoc = .{ .node_offset_bin_lhs = inst_data.src_node };
2464 const rhs_src: LazySrcLoc = .{ .node_offset_bin_rhs = inst_data.src_node };
2465 const extra = sema.code.extraData(zir.Inst.Bin, inst_data.payload_index).data;
2466 const lhs = try sema.resolveInst(extra.lhs);
2467 const rhs = try sema.resolveInst(extra.rhs);
24552468
24562469 const instructions = &[_]*Inst{ lhs, rhs };
24572470 const resolved_type = try sema.resolvePeerTypes(block, src, instructions);
2458 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);
2471 const casted_lhs = try sema.coerce(block, resolved_type, lhs, lhs_src);
2472 const casted_rhs = try sema.coerce(block, resolved_type, rhs, rhs_src);
24602473
24612474 const scalar_type = if (resolved_type.zigTypeTag() == .Vector)
24622475 resolved_type.elemType()
......@@ -2499,13 +2512,6 @@ fn zirBitwise(sema: *Sema, block: *Scope.Block, inst: zir.Inst.Index) InnerError
24992512 }
25002513
25012514 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
25092515 return block.addBinOp(src, scalar_type, ir_tag, casted_lhs, casted_rhs);
25102516}
25112517
src/astgen.zig+25-34
......@@ -367,6 +367,9 @@ pub fn expr(mod: *Module, scope: *Scope, rl: ResultLoc, node: ast.Node.Index) In
367367 .array_cat => return simpleBinOp(mod, scope, rl, node, .array_cat),
368368 .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
370373 .bool_and => return boolBinOp(mod, scope, rl, node, .bool_br_and),
371374 .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
515518 const statements = tree.extra_data[node_datas[node].lhs..node_datas[node].rhs];
516519 return blockExpr(mod, scope, rl, node, statements);
517520 },
518 .enum_literal => {
519 const ident_token = main_tokens[node];
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 },
521 .enum_literal => return simpleStrTok(mod, scope, rl, main_tokens[node], node, .enum_literal),
522 .error_value => return simpleStrTok(mod, scope, rl, node_datas[node].rhs, node, .error_value),
548523 .anyframe_literal => return mod.failNode(scope, node, "async and related features are not yet supported", .{}),
549524 .anyframe_type => return mod.failNode(scope, node, "async and related features are not yet supported", .{}),
550525 .@"catch" => {
551 if (true) @panic("TODO update for zir-memory-layout");
552526 const catch_token = main_tokens[node];
553527 const payload_token: ?ast.TokenIndex = if (token_tags[catch_token + 1] == .pipe)
554528 catch_token + 2
......@@ -1597,7 +1571,6 @@ fn containerDecl(
15971571 rl: ResultLoc,
15981572 container_decl: ast.full.ContainerDecl,
15991573) InnerError!zir.Inst.Ref {
1600 if (true) @panic("TODO update for zir-memory-layout");
16011574 return mod.failTok(scope, container_decl.ast.main_token, "TODO implement container decls", .{});
16021575}
16031576
......@@ -1607,8 +1580,9 @@ fn errorSetDecl(
16071580 rl: ResultLoc,
16081581 node: ast.Node.Index,
16091582) InnerError!zir.Inst.Ref {
1610 if (true) @panic("TODO update for zir-memory-layout");
1611 const tree = scope.tree();
1583 if (true) @panic("TODO update for zir-memory-layout branch");
1584 const gz = scope.getGenZir();
1585 const tree = gz.tree();
16121586 const main_tokens = tree.nodes.items(.main_token);
16131587 const token_tags = tree.tokens.items(.tag);
16141588
......@@ -1905,6 +1879,23 @@ fn simpleBinOp(
19051879 return rvalue(mod, scope, rl, result, node);
19061880}
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
19081899fn boolBinOp(
19091900 mod: *Module,
19101901 scope: *Scope,
src/zir.zig+10-5
......@@ -312,8 +312,12 @@ pub const Inst = struct {
312312 /// Uses the `un_node` field.
313313 ensure_result_non_error,
314314 /// Create a `E!T` type.
315 /// Uses the `pl_node` field with `Bin` payload.
315316 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.
317321 error_set,
318322 /// `error.Foo` syntax. Uses the `str_tok` field of the Data union.
319323 error_value,
......@@ -393,6 +397,7 @@ pub const Inst = struct {
393397 /// Uses the `node` field.
394398 repeat_inline,
395399 /// Merge two error sets into one, `E1 || E2`.
400 /// Uses the `pl_node` field with payload `Bin`.
396401 merge_error_sets,
397402 /// Ambiguously remainder division or modulus. If the computation would possibly have
398403 /// a different value depending on whether the operation is remainder division or modulus,
......@@ -1368,14 +1373,11 @@ const Writer = struct {
13681373 try stream.print("= {s}(", .{@tagName(tags[inst])});
13691374 switch (tag) {
13701375 .array_type,
1371 .bit_and,
1372 .bit_or,
13731376 .as,
13741377 .coerce_result_ptr,
13751378 .elem_ptr,
13761379 .elem_val,
13771380 .intcast,
1378 .merge_error_sets,
13791381 .store,
13801382 .store_to_block_ptr,
13811383 => try self.writeBin(stream, inst),
......@@ -1481,6 +1483,10 @@ const Writer = struct {
14811483 .shr,
14821484 .xor,
14831485 .store_node,
1486 .error_union_type,
1487 .merge_error_sets,
1488 .bit_and,
1489 .bit_or,
14841490 => try self.writePlNodeBin(stream, inst),
14851491
14861492 .call,
......@@ -1530,7 +1536,6 @@ const Writer = struct {
15301536 .bitcast,
15311537 .bitcast_ref,
15321538 .bitcast_result_ptr,
1533 .error_union_type,
15341539 .error_set,
15351540 .store_to_inferred_ptr,
15361541 => try stream.writeAll("TODO)"),
test/stage2/arm.zig+93-93
......@@ -184,103 +184,103 @@ pub fn addCases(ctx: *TestContext) !void {
184184 );
185185
186186 // Bitwise And
187 //case.addCompareOutput(
188 // \\export fn _start() noreturn {
189 // \\ print(8, 9);
190 // \\ print(3, 7);
191 // \\ exit();
192 // \\}
193 // \\
194 // \\fn print(a: u32, b: u32) void {
195 // \\ asm volatile ("svc #0"
196 // \\ :
197 // \\ : [number] "{r7}" (4),
198 // \\ [arg3] "{r2}" (a & b),
199 // \\ [arg1] "{r0}" (1),
200 // \\ [arg2] "{r1}" (@ptrToInt("123456789"))
201 // \\ : "memory"
202 // \\ );
203 // \\ return;
204 // \\}
205 // \\
206 // \\fn exit() noreturn {
207 // \\ asm volatile ("svc #0"
208 // \\ :
209 // \\ : [number] "{r7}" (1),
210 // \\ [arg1] "{r0}" (0)
211 // \\ : "memory"
212 // \\ );
213 // \\ unreachable;
214 // \\}
215 //,
216 // "12345678123",
217 //);
187 case.addCompareOutput(
188 \\export fn _start() noreturn {
189 \\ print(8, 9);
190 \\ print(3, 7);
191 \\ exit();
192 \\}
193 \\
194 \\fn print(a: u32, b: u32) void {
195 \\ asm volatile ("svc #0"
196 \\ :
197 \\ : [number] "{r7}" (4),
198 \\ [arg3] "{r2}" (a & b),
199 \\ [arg1] "{r0}" (1),
200 \\ [arg2] "{r1}" (@ptrToInt("123456789"))
201 \\ : "memory"
202 \\ );
203 \\ return;
204 \\}
205 \\
206 \\fn exit() noreturn {
207 \\ asm volatile ("svc #0"
208 \\ :
209 \\ : [number] "{r7}" (1),
210 \\ [arg1] "{r0}" (0)
211 \\ : "memory"
212 \\ );
213 \\ unreachable;
214 \\}
215 ,
216 "12345678123",
217 );
218218
219219 // Bitwise Or
220 //case.addCompareOutput(
221 // \\export fn _start() noreturn {
222 // \\ print(4, 2);
223 // \\ print(3, 7);
224 // \\ exit();
225 // \\}
226 // \\
227 // \\fn print(a: u32, b: u32) void {
228 // \\ asm volatile ("svc #0"
229 // \\ :
230 // \\ : [number] "{r7}" (4),
231 // \\ [arg3] "{r2}" (a | b),
232 // \\ [arg1] "{r0}" (1),
233 // \\ [arg2] "{r1}" (@ptrToInt("123456789"))
234 // \\ : "memory"
235 // \\ );
236 // \\ return;
237 // \\}
238 // \\
239 // \\fn exit() noreturn {
240 // \\ asm volatile ("svc #0"
241 // \\ :
242 // \\ : [number] "{r7}" (1),
243 // \\ [arg1] "{r0}" (0)
244 // \\ : "memory"
245 // \\ );
246 // \\ unreachable;
247 // \\}
248 //,
249 // "1234561234567",
250 //);
220 case.addCompareOutput(
221 \\export fn _start() noreturn {
222 \\ print(4, 2);
223 \\ print(3, 7);
224 \\ exit();
225 \\}
226 \\
227 \\fn print(a: u32, b: u32) void {
228 \\ asm volatile ("svc #0"
229 \\ :
230 \\ : [number] "{r7}" (4),
231 \\ [arg3] "{r2}" (a | b),
232 \\ [arg1] "{r0}" (1),
233 \\ [arg2] "{r1}" (@ptrToInt("123456789"))
234 \\ : "memory"
235 \\ );
236 \\ return;
237 \\}
238 \\
239 \\fn exit() noreturn {
240 \\ asm volatile ("svc #0"
241 \\ :
242 \\ : [number] "{r7}" (1),
243 \\ [arg1] "{r0}" (0)
244 \\ : "memory"
245 \\ );
246 \\ unreachable;
247 \\}
248 ,
249 "1234561234567",
250 );
251251
252252 // Bitwise Xor
253 //case.addCompareOutput(
254 // \\export fn _start() noreturn {
255 // \\ print(42, 42);
256 // \\ print(3, 5);
257 // \\ exit();
258 // \\}
259 // \\
260 // \\fn print(a: u32, b: u32) void {
261 // \\ asm volatile ("svc #0"
262 // \\ :
263 // \\ : [number] "{r7}" (4),
264 // \\ [arg3] "{r2}" (a ^ b),
265 // \\ [arg1] "{r0}" (1),
266 // \\ [arg2] "{r1}" (@ptrToInt("123456789"))
267 // \\ : "memory"
268 // \\ );
269 // \\ return;
270 // \\}
271 // \\
272 // \\fn exit() noreturn {
273 // \\ asm volatile ("svc #0"
274 // \\ :
275 // \\ : [number] "{r7}" (1),
276 // \\ [arg1] "{r0}" (0)
277 // \\ : "memory"
278 // \\ );
279 // \\ unreachable;
280 // \\}
281 //,
282 // "123456",
283 //);
253 case.addCompareOutput(
254 \\export fn _start() noreturn {
255 \\ print(42, 42);
256 \\ print(3, 5);
257 \\ exit();
258 \\}
259 \\
260 \\fn print(a: u32, b: u32) void {
261 \\ asm volatile ("svc #0"
262 \\ :
263 \\ : [number] "{r7}" (4),
264 \\ [arg3] "{r2}" (a ^ b),
265 \\ [arg1] "{r0}" (1),
266 \\ [arg2] "{r1}" (@ptrToInt("123456789"))
267 \\ : "memory"
268 \\ );
269 \\ return;
270 \\}
271 \\
272 \\fn exit() noreturn {
273 \\ asm volatile ("svc #0"
274 \\ :
275 \\ : [number] "{r7}" (1),
276 \\ [arg1] "{r0}" (0)
277 \\ : "memory"
278 \\ );
279 \\ unreachable;
280 \\}
281 ,
282 "123456",
283 );
284284 }
285285
286286 {
test/stage2/test.zig+158-154
......@@ -1356,53 +1356,53 @@ pub fn addCases(ctx: *TestContext) !void {
13561356 \\}
13571357 , &[_][]const u8{":8:21: error: evaluation exceeded 1000 backwards branches"});
13581358 }
1359 //{
1360 // var case = ctx.exe("orelse at comptime", linux_x64);
1361 // case.addCompareOutput(
1362 // \\export fn _start() noreturn {
1363 // \\ const i: ?u64 = 0;
1364 // \\ const orelsed = i orelse 5;
1365 // \\ assert(orelsed == 0);
1366 // \\ exit();
1367 // \\}
1368 // \\fn assert(b: bool) void {
1369 // \\ if (!b) unreachable;
1370 // \\}
1371 // \\fn exit() noreturn {
1372 // \\ asm volatile ("syscall"
1373 // \\ :
1374 // \\ : [number] "{rax}" (231),
1375 // \\ [arg1] "{rdi}" (0)
1376 // \\ : "rcx", "r11", "memory"
1377 // \\ );
1378 // \\ unreachable;
1379 // \\}
1380 // ,
1381 // "",
1382 // );
1383 // case.addCompareOutput(
1384 // \\export fn _start() noreturn {
1385 // \\ const i: ?u64 = null;
1386 // \\ const orelsed = i orelse 5;
1387 // \\ assert(orelsed == 5);
1388 // \\ exit();
1389 // \\}
1390 // \\fn assert(b: bool) void {
1391 // \\ if (!b) unreachable;
1392 // \\}
1393 // \\fn exit() noreturn {
1394 // \\ asm volatile ("syscall"
1395 // \\ :
1396 // \\ : [number] "{rax}" (231),
1397 // \\ [arg1] "{rdi}" (0)
1398 // \\ : "rcx", "r11", "memory"
1399 // \\ );
1400 // \\ unreachable;
1401 // \\}
1402 // ,
1403 // "",
1404 // );
1405 //}
1359 {
1360 var case = ctx.exe("orelse at comptime", linux_x64);
1361 case.addCompareOutput(
1362 \\export fn _start() noreturn {
1363 \\ const i: ?u64 = 0;
1364 \\ const orelsed = i orelse 5;
1365 \\ assert(orelsed == 0);
1366 \\ exit();
1367 \\}
1368 \\fn assert(b: bool) void {
1369 \\ if (!b) unreachable;
1370 \\}
1371 \\fn exit() noreturn {
1372 \\ asm volatile ("syscall"
1373 \\ :
1374 \\ : [number] "{rax}" (231),
1375 \\ [arg1] "{rdi}" (0)
1376 \\ : "rcx", "r11", "memory"
1377 \\ );
1378 \\ unreachable;
1379 \\}
1380 ,
1381 "",
1382 );
1383 case.addCompareOutput(
1384 \\export fn _start() noreturn {
1385 \\ const i: ?u64 = null;
1386 \\ const orelsed = i orelse 5;
1387 \\ assert(orelsed == 5);
1388 \\ exit();
1389 \\}
1390 \\fn assert(b: bool) void {
1391 \\ if (!b) unreachable;
1392 \\}
1393 \\fn exit() noreturn {
1394 \\ asm volatile ("syscall"
1395 \\ :
1396 \\ : [number] "{rax}" (231),
1397 \\ [arg1] "{rdi}" (0)
1398 \\ : "rcx", "r11", "memory"
1399 \\ );
1400 \\ unreachable;
1401 \\}
1402 ,
1403 "",
1404 );
1405 }
14061406
14071407 {
14081408 var case = ctx.exe("only 1 function and it gets updated", linux_x64);
......@@ -1454,113 +1454,117 @@ pub fn addCases(ctx: *TestContext) !void {
14541454 "",
14551455 );
14561456 }
1457 //{
1458 // var case = ctx.exe("catch at comptime", linux_x64);
1459 // case.addCompareOutput(
1460 // \\export fn _start() noreturn {
1461 // \\ const i: anyerror!u64 = 0;
1462 // \\ const caught = i catch 5;
1463 // \\ assert(caught == 0);
1464 // \\ exit();
1465 // \\}
1466 // \\fn assert(b: bool) void {
1467 // \\ if (!b) unreachable;
1468 // \\}
1469 // \\fn exit() noreturn {
1470 // \\ asm volatile ("syscall"
1471 // \\ :
1472 // \\ : [number] "{rax}" (231),
1473 // \\ [arg1] "{rdi}" (0)
1474 // \\ : "rcx", "r11", "memory"
1475 // \\ );
1476 // \\ unreachable;
1477 // \\}
1478 // ,
1479 // "",
1480 // );
1481 // case.addCompareOutput(
1482 // \\export fn _start() noreturn {
1483 // \\ const i: anyerror!u64 = error.B;
1484 // \\ const caught = i catch 5;
1485 // \\ assert(caught == 5);
1486 // \\ exit();
1487 // \\}
1488 // \\fn assert(b: bool) void {
1489 // \\ if (!b) unreachable;
1490 // \\}
1491 // \\fn exit() noreturn {
1492 // \\ asm volatile ("syscall"
1493 // \\ :
1494 // \\ : [number] "{rax}" (231),
1495 // \\ [arg1] "{rdi}" (0)
1496 // \\ : "rcx", "r11", "memory"
1497 // \\ );
1498 // \\ unreachable;
1499 // \\}
1500 // ,
1501 // "",
1502 // );
1503 // case.addCompareOutput(
1504 // \\export fn _start() noreturn {
1505 // \\ const a: anyerror!comptime_int = 42;
1506 // \\ const b: *const comptime_int = &(a catch unreachable);
1507 // \\ assert(b.* == 42);
1508 // \\
1509 // \\ exit();
1510 // \\}
1511 // \\fn assert(b: bool) void {
1512 // \\ if (!b) unreachable; // assertion failure
1513 // \\}
1514 // \\fn exit() noreturn {
1515 // \\ asm volatile ("syscall"
1516 // \\ :
1517 // \\ : [number] "{rax}" (231),
1518 // \\ [arg1] "{rdi}" (0)
1519 // \\ : "rcx", "r11", "memory"
1520 // \\ );
1521 // \\ unreachable;
1522 // \\}
1523 // , "");
1524 // case.addCompareOutput(
1525 // \\export fn _start() noreturn {
1526 // \\const a: anyerror!u32 = error.B;
1527 // \\_ = &(a catch |err| assert(err == error.B));
1528 // \\exit();
1529 // \\}
1530 // \\fn assert(b: bool) void {
1531 // \\ if (!b) unreachable;
1532 // \\}
1533 // \\fn exit() noreturn {
1534 // \\ asm volatile ("syscall"
1535 // \\ :
1536 // \\ : [number] "{rax}" (231),
1537 // \\ [arg1] "{rdi}" (0)
1538 // \\ : "rcx", "r11", "memory"
1539 // \\ );
1540 // \\ unreachable;
1541 // \\}
1542 // , "");
1543 // case.addCompareOutput(
1544 // \\export fn _start() noreturn {
1545 // \\ const a: anyerror!u32 = error.Bar;
1546 // \\ a catch |err| assert(err == error.Bar);
1547 // \\
1548 // \\ exit();
1549 // \\}
1550 // \\fn assert(b: bool) void {
1551 // \\ if (!b) unreachable;
1552 // \\}
1553 // \\fn exit() noreturn {
1554 // \\ asm volatile ("syscall"
1555 // \\ :
1556 // \\ : [number] "{rax}" (231),
1557 // \\ [arg1] "{rdi}" (0)
1558 // \\ : "rcx", "r11", "memory"
1559 // \\ );
1560 // \\ unreachable;
1561 // \\}
1562 // , "");
1563 //}
1457 {
1458 var case = ctx.exe("catch at comptime", linux_x64);
1459 case.addCompareOutput(
1460 \\export fn _start() noreturn {
1461 \\ const i: anyerror!u64 = 0;
1462 \\ const caught = i catch 5;
1463 \\ assert(caught == 0);
1464 \\ exit();
1465 \\}
1466 \\fn assert(b: bool) void {
1467 \\ if (!b) unreachable;
1468 \\}
1469 \\fn exit() noreturn {
1470 \\ asm volatile ("syscall"
1471 \\ :
1472 \\ : [number] "{rax}" (231),
1473 \\ [arg1] "{rdi}" (0)
1474 \\ : "rcx", "r11", "memory"
1475 \\ );
1476 \\ unreachable;
1477 \\}
1478 ,
1479 "",
1480 );
1481
1482 case.addCompareOutput(
1483 \\export fn _start() noreturn {
1484 \\ const i: anyerror!u64 = error.B;
1485 \\ const caught = i catch 5;
1486 \\ assert(caught == 5);
1487 \\ exit();
1488 \\}
1489 \\fn assert(b: bool) void {
1490 \\ if (!b) unreachable;
1491 \\}
1492 \\fn exit() noreturn {
1493 \\ asm volatile ("syscall"
1494 \\ :
1495 \\ : [number] "{rax}" (231),
1496 \\ [arg1] "{rdi}" (0)
1497 \\ : "rcx", "r11", "memory"
1498 \\ );
1499 \\ unreachable;
1500 \\}
1501 ,
1502 "",
1503 );
1504
1505 //case.addCompareOutput(
1506 // \\export fn _start() noreturn {
1507 // \\ const a: anyerror!comptime_int = 42;
1508 // \\ const b: *const comptime_int = &(a catch unreachable);
1509 // \\ assert(b.* == 42);
1510 // \\
1511 // \\ exit();
1512 // \\}
1513 // \\fn assert(b: bool) void {
1514 // \\ if (!b) unreachable; // assertion failure
1515 // \\}
1516 // \\fn exit() noreturn {
1517 // \\ asm volatile ("syscall"
1518 // \\ :
1519 // \\ : [number] "{rax}" (231),
1520 // \\ [arg1] "{rdi}" (0)
1521 // \\ : "rcx", "r11", "memory"
1522 // \\ );
1523 // \\ unreachable;
1524 // \\}
1525 //, "");
1526
1527 case.addCompareOutput(
1528 \\export fn _start() noreturn {
1529 \\ const a: anyerror!u32 = error.B;
1530 \\ _ = &(a catch |err| assert(err == error.B));
1531 \\ exit();
1532 \\}
1533 \\fn assert(b: bool) void {
1534 \\ if (!b) unreachable;
1535 \\}
1536 \\fn exit() noreturn {
1537 \\ asm volatile ("syscall"
1538 \\ :
1539 \\ : [number] "{rax}" (231),
1540 \\ [arg1] "{rdi}" (0)
1541 \\ : "rcx", "r11", "memory"
1542 \\ );
1543 \\ unreachable;
1544 \\}
1545 , "");
1546
1547 case.addCompareOutput(
1548 \\export fn _start() noreturn {
1549 \\ const a: anyerror!u32 = error.Bar;
1550 \\ a catch |err| assert(err == error.Bar);
1551 \\
1552 \\ exit();
1553 \\}
1554 \\fn assert(b: bool) void {
1555 \\ if (!b) unreachable;
1556 \\}
1557 \\fn exit() noreturn {
1558 \\ asm volatile ("syscall"
1559 \\ :
1560 \\ : [number] "{rax}" (231),
1561 \\ [arg1] "{rdi}" (0)
1562 \\ : "rcx", "r11", "memory"
1563 \\ );
1564 \\ unreachable;
1565 \\}
1566 , "");
1567 }
15641568 //{
15651569 // var case = ctx.exe("merge error sets", linux_x64);
15661570