authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2021-12-21 21:06:08-08:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2021-12-21 21:06:08-08:00
log5d6380b38d7c83055fe29eb7f8a19b1424276cd7
tree59ff7df2562eb47b34cd5dd3551742200a0f85f0
parent88be5bd81decab792cdb5b749b4bb5cf6d71e877
parentc55f58d8bb38ef356282e43fa010b5e3f8da8a00
signaturebadge-question-mark Signed by PGP key 4AEE18F83AFDEB23

Merge pull request #10384 from joachimschmidt557/stage2-arm-optionals

stage2 ARM: basic implementation of optionals and error unions

2 files changed, 176 insertions(+), 51 deletions(-)

src/arch/arm/CodeGen.zig+107-51
......@@ -1104,7 +1104,13 @@ fn airUnwrapErrErr(self: *Self, inst: Air.Inst.Index) !void {
11041104
11051105fn airUnwrapErrPayload(self: *Self, inst: Air.Inst.Index) !void {
11061106 const ty_op = self.air.instructions.items(.data)[inst].ty_op;
1107 const result: MCValue = if (self.liveness.isUnused(inst)) .dead else return self.fail("TODO implement unwrap error union payload for {}", .{self.target.cpu.arch});
1107 const result: MCValue = if (self.liveness.isUnused(inst)) .dead else result: {
1108 const err_ty = self.air.typeOf(ty_op.operand);
1109 const payload_ty = err_ty.errorUnionPayload();
1110 if (!payload_ty.hasCodeGenBits()) break :result MCValue.none;
1111
1112 return self.fail("TODO implement unwrap error union payload for non-empty payloads", .{});
1113 };
11081114 return self.finishAir(inst, result, .{ ty_op.operand, .none, .none });
11091115}
11101116
......@@ -2334,39 +2340,81 @@ fn airCondBr(self: *Self, inst: Air.Inst.Index) !void {
23342340 return self.finishAir(inst, .unreach, .{ .none, .none, .none });
23352341}
23362342
2337fn isNull(self: *Self, operand: MCValue) !MCValue {
2338 _ = operand;
2339 // Here you can specialize this instruction if it makes sense to, otherwise the default
2340 // will call isNonNull and invert the result.
2341 return self.fail("TODO call isNonNull and invert the result", .{});
2343fn isNull(self: *Self, ty: Type, operand: MCValue) !MCValue {
2344 if (ty.isPtrLikeOptional()) {
2345 assert(ty.abiSize(self.target.*) == 4);
2346
2347 const reg_mcv: MCValue = switch (operand) {
2348 .register => operand,
2349 else => .{ .register = try self.copyToTmpRegister(ty, operand) },
2350 };
2351
2352 try self.genArmBinOpCode(undefined, reg_mcv, .{ .immediate = 0 }, false, .cmp_eq, undefined);
2353
2354 return MCValue{ .compare_flags_unsigned = .eq };
2355 } else {
2356 return self.fail("TODO implement non-pointer optionals", .{});
2357 }
23422358}
23432359
2344fn isNonNull(self: *Self, operand: MCValue) !MCValue {
2345 _ = operand;
2346 // Here you can specialize this instruction if it makes sense to, otherwise the default
2347 // will call isNull and invert the result.
2348 return self.fail("TODO call isNull and invert the result", .{});
2360fn isNonNull(self: *Self, ty: Type, operand: MCValue) !MCValue {
2361 const is_null_result = try self.isNull(ty, operand);
2362 assert(is_null_result.compare_flags_unsigned == .eq);
2363
2364 return MCValue{ .compare_flags_unsigned = .neq };
23492365}
23502366
2351fn isErr(self: *Self, operand: MCValue) !MCValue {
2367fn isErr(self: *Self, ty: Type, operand: MCValue) !MCValue {
23522368 _ = operand;
2353 // Here you can specialize this instruction if it makes sense to, otherwise the default
2354 // will call isNonNull and invert the result.
2355 return self.fail("TODO call isNonErr and invert the result", .{});
2369
2370 const error_type = ty.errorUnionSet();
2371 const payload_type = ty.errorUnionPayload();
2372
2373 if (!error_type.hasCodeGenBits()) {
2374 return MCValue{ .immediate = 0 }; // always false
2375 } else if (!payload_type.hasCodeGenBits()) {
2376 if (error_type.abiSize(self.target.*) <= 4) {
2377 const reg_mcv: MCValue = switch (operand) {
2378 .register => operand,
2379 else => .{ .register = try self.copyToTmpRegister(error_type, operand) },
2380 };
2381
2382 try self.genArmBinOpCode(undefined, reg_mcv, .{ .immediate = 0 }, false, .cmp_eq, undefined);
2383
2384 return MCValue{ .compare_flags_unsigned = .gt };
2385 } else {
2386 return self.fail("TODO isErr for errors with size > 4", .{});
2387 }
2388 } else {
2389 return self.fail("TODO isErr for non-empty payloads", .{});
2390 }
23562391}
23572392
2358fn isNonErr(self: *Self, operand: MCValue) !MCValue {
2359 _ = operand;
2360 // Here you can specialize this instruction if it makes sense to, otherwise the default
2361 // will call isNull and invert the result.
2362 return self.fail("TODO call isErr and invert the result", .{});
2393fn isNonErr(self: *Self, ty: Type, operand: MCValue) !MCValue {
2394 const is_err_result = try self.isErr(ty, operand);
2395 switch (is_err_result) {
2396 .compare_flags_unsigned => |op| {
2397 assert(op == .gt);
2398 return MCValue{ .compare_flags_unsigned = .lte };
2399 },
2400 .immediate => |imm| {
2401 assert(imm == 0);
2402 return MCValue{ .immediate = 1 };
2403 },
2404 else => unreachable,
2405 }
23632406}
23642407
23652408fn airIsNull(self: *Self, inst: Air.Inst.Index) !void {
23662409 const un_op = self.air.instructions.items(.data)[inst].un_op;
2410
2411 try self.spillCompareFlagsIfOccupied();
2412 self.compare_flags_inst = inst;
2413
23672414 const result: MCValue = if (self.liveness.isUnused(inst)) .dead else result: {
23682415 const operand = try self.resolveInst(un_op);
2369 break :result try self.isNull(operand);
2416 const ty = self.air.typeOf(un_op);
2417 break :result try self.isNull(ty, operand);
23702418 };
23712419 return self.finishAir(inst, result, .{ un_op, .none, .none });
23722420}
......@@ -2375,6 +2423,7 @@ fn airIsNullPtr(self: *Self, inst: Air.Inst.Index) !void {
23752423 const un_op = self.air.instructions.items(.data)[inst].un_op;
23762424 const result: MCValue = if (self.liveness.isUnused(inst)) .dead else result: {
23772425 const operand_ptr = try self.resolveInst(un_op);
2426 const ptr_ty = self.air.typeOf(un_op);
23782427 const operand: MCValue = blk: {
23792428 if (self.reuseOperand(inst, un_op, 0, operand_ptr)) {
23802429 // The MCValue that holds the pointer can be re-used as the value.
......@@ -2383,8 +2432,8 @@ fn airIsNullPtr(self: *Self, inst: Air.Inst.Index) !void {
23832432 break :blk try self.allocRegOrMem(inst, true);
23842433 }
23852434 };
2386 try self.load(operand, operand_ptr, self.air.typeOf(un_op));
2387 break :result try self.isNull(operand);
2435 try self.load(operand, operand_ptr, ptr_ty);
2436 break :result try self.isNull(ptr_ty.elemType(), operand);
23882437 };
23892438 return self.finishAir(inst, result, .{ un_op, .none, .none });
23902439}
......@@ -2393,7 +2442,8 @@ fn airIsNonNull(self: *Self, inst: Air.Inst.Index) !void {
23932442 const un_op = self.air.instructions.items(.data)[inst].un_op;
23942443 const result: MCValue = if (self.liveness.isUnused(inst)) .dead else result: {
23952444 const operand = try self.resolveInst(un_op);
2396 break :result try self.isNonNull(operand);
2445 const ty = self.air.typeOf(un_op);
2446 break :result try self.isNonNull(ty, operand);
23972447 };
23982448 return self.finishAir(inst, result, .{ un_op, .none, .none });
23992449}
......@@ -2402,6 +2452,7 @@ fn airIsNonNullPtr(self: *Self, inst: Air.Inst.Index) !void {
24022452 const un_op = self.air.instructions.items(.data)[inst].un_op;
24032453 const result: MCValue = if (self.liveness.isUnused(inst)) .dead else result: {
24042454 const operand_ptr = try self.resolveInst(un_op);
2455 const ptr_ty = self.air.typeOf(un_op);
24052456 const operand: MCValue = blk: {
24062457 if (self.reuseOperand(inst, un_op, 0, operand_ptr)) {
24072458 // The MCValue that holds the pointer can be re-used as the value.
......@@ -2410,8 +2461,8 @@ fn airIsNonNullPtr(self: *Self, inst: Air.Inst.Index) !void {
24102461 break :blk try self.allocRegOrMem(inst, true);
24112462 }
24122463 };
2413 try self.load(operand, operand_ptr, self.air.typeOf(un_op));
2414 break :result try self.isNonNull(operand);
2464 try self.load(operand, operand_ptr, ptr_ty);
2465 break :result try self.isNonNull(ptr_ty.elemType(), operand);
24152466 };
24162467 return self.finishAir(inst, result, .{ un_op, .none, .none });
24172468}
......@@ -2420,7 +2471,8 @@ fn airIsErr(self: *Self, inst: Air.Inst.Index) !void {
24202471 const un_op = self.air.instructions.items(.data)[inst].un_op;
24212472 const result: MCValue = if (self.liveness.isUnused(inst)) .dead else result: {
24222473 const operand = try self.resolveInst(un_op);
2423 break :result try self.isErr(operand);
2474 const ty = self.air.typeOf(un_op);
2475 break :result try self.isErr(ty, operand);
24242476 };
24252477 return self.finishAir(inst, result, .{ un_op, .none, .none });
24262478}
......@@ -2429,6 +2481,7 @@ fn airIsErrPtr(self: *Self, inst: Air.Inst.Index) !void {
24292481 const un_op = self.air.instructions.items(.data)[inst].un_op;
24302482 const result: MCValue = if (self.liveness.isUnused(inst)) .dead else result: {
24312483 const operand_ptr = try self.resolveInst(un_op);
2484 const ptr_ty = self.air.typeOf(un_op);
24322485 const operand: MCValue = blk: {
24332486 if (self.reuseOperand(inst, un_op, 0, operand_ptr)) {
24342487 // The MCValue that holds the pointer can be re-used as the value.
......@@ -2437,8 +2490,8 @@ fn airIsErrPtr(self: *Self, inst: Air.Inst.Index) !void {
24372490 break :blk try self.allocRegOrMem(inst, true);
24382491 }
24392492 };
2440 try self.load(operand, operand_ptr, self.air.typeOf(un_op));
2441 break :result try self.isErr(operand);
2493 try self.load(operand, operand_ptr, ptr_ty);
2494 break :result try self.isErr(ptr_ty.elemType(), operand);
24422495 };
24432496 return self.finishAir(inst, result, .{ un_op, .none, .none });
24442497}
......@@ -2447,7 +2500,8 @@ fn airIsNonErr(self: *Self, inst: Air.Inst.Index) !void {
24472500 const un_op = self.air.instructions.items(.data)[inst].un_op;
24482501 const result: MCValue = if (self.liveness.isUnused(inst)) .dead else result: {
24492502 const operand = try self.resolveInst(un_op);
2450 break :result try self.isNonErr(operand);
2503 const ty = self.air.typeOf(un_op);
2504 break :result try self.isNonErr(ty, operand);
24512505 };
24522506 return self.finishAir(inst, result, .{ un_op, .none, .none });
24532507}
......@@ -2456,6 +2510,7 @@ fn airIsNonErrPtr(self: *Self, inst: Air.Inst.Index) !void {
24562510 const un_op = self.air.instructions.items(.data)[inst].un_op;
24572511 const result: MCValue = if (self.liveness.isUnused(inst)) .dead else result: {
24582512 const operand_ptr = try self.resolveInst(un_op);
2513 const ptr_ty = self.air.typeOf(un_op);
24592514 const operand: MCValue = blk: {
24602515 if (self.reuseOperand(inst, un_op, 0, operand_ptr)) {
24612516 // The MCValue that holds the pointer can be re-used as the value.
......@@ -2464,8 +2519,8 @@ fn airIsNonErrPtr(self: *Self, inst: Air.Inst.Index) !void {
24642519 break :blk try self.allocRegOrMem(inst, true);
24652520 }
24662521 };
2467 try self.load(operand, operand_ptr, self.air.typeOf(un_op));
2468 break :result try self.isNonErr(operand);
2522 try self.load(operand, operand_ptr, ptr_ty);
2523 break :result try self.isNonErr(ptr_ty.elemType(), operand);
24692524 };
24702525 return self.finishAir(inst, result, .{ un_op, .none, .none });
24712526}
......@@ -3365,31 +3420,32 @@ fn genTypedValue(self: *Self, typed_value: TypedValue) InnerError!MCValue {
33653420 }
33663421 },
33673422 .ErrorSet => {
3368 switch (typed_value.val.tag()) {
3369 .@"error" => {
3370 const err_name = typed_value.val.castTag(.@"error").?.data.name;
3371 const module = self.bin_file.options.module.?;
3372 const global_error_set = module.global_error_set;
3373 const error_index = global_error_set.get(err_name).?;
3374 return MCValue{ .immediate = error_index };
3375 },
3376 else => {
3377 // In this case we are rendering an error union which has a 0 bits payload.
3378 return MCValue{ .immediate = 0 };
3379 },
3380 }
3423 const err_name = typed_value.val.castTag(.@"error").?.data.name;
3424 const module = self.bin_file.options.module.?;
3425 const global_error_set = module.global_error_set;
3426 const error_index = global_error_set.get(err_name).?;
3427 return MCValue{ .immediate = error_index };
33813428 },
33823429 .ErrorUnion => {
33833430 const error_type = typed_value.ty.errorUnionSet();
33843431 const payload_type = typed_value.ty.errorUnionPayload();
3385 const sub_val = typed_value.val.castTag(.eu_payload).?.data;
33863432
3387 if (!payload_type.hasCodeGenBits()) {
3388 // We use the error type directly as the type.
3389 return self.genTypedValue(.{ .ty = error_type, .val = sub_val });
3390 }
3433 if (typed_value.val.castTag(.eu_payload)) |pl| {
3434 if (!payload_type.hasCodeGenBits()) {
3435 // We use the error type directly as the type.
3436 return MCValue{ .immediate = 0 };
3437 }
33913438
3392 return self.fail("TODO implement error union const of type '{}'", .{typed_value.ty});
3439 _ = pl;
3440 return self.fail("TODO implement error union const of type '{}' (non-error)", .{typed_value.ty});
3441 } else {
3442 if (!payload_type.hasCodeGenBits()) {
3443 // We use the error type directly as the type.
3444 return self.genTypedValue(.{ .ty = error_type, .val = typed_value.val });
3445 }
3446
3447 return self.fail("TODO implement error union const of type '{}' (error)", .{typed_value.ty});
3448 }
33933449 },
33943450 else => return self.fail("TODO implement const of type '{}'", .{typed_value.ty}),
33953451 }
test/stage2/arm.zig+69
......@@ -568,4 +568,73 @@ pub fn addCases(ctx: *TestContext) !void {
568568 "",
569569 );
570570 }
571
572 {
573 var case = ctx.exe("optionals", linux_arm);
574 case.addCompareOutput(
575 \\var x: u32 = 42;
576 \\
577 \\pub fn main() void {
578 \\ var p: ?*u32 = null;
579 \\ assert(p == null);
580 \\ p = &x;
581 \\ assert(p != null);
582 \\}
583 \\
584 \\fn assert(ok: bool) void {
585 \\ if (!ok) unreachable;
586 \\}
587 ,
588 "",
589 );
590 }
591
592 {
593 var case = ctx.exe("errors", linux_arm);
594 case.addCompareOutput(
595 \\pub fn main() void {
596 \\ foo() catch print();
597 \\}
598 \\
599 \\fn foo() anyerror!void {}
600 \\
601 \\fn print() void {
602 \\ asm volatile ("svc #0"
603 \\ :
604 \\ : [number] "{r7}" (4),
605 \\ [arg1] "{r0}" (1),
606 \\ [arg2] "{r1}" (@ptrToInt("Hello, World!\n")),
607 \\ [arg3] "{r2}" ("Hello, World!\n".len),
608 \\ : "memory"
609 \\ );
610 \\ return;
611 \\}
612 ,
613 "",
614 );
615
616 case.addCompareOutput(
617 \\pub fn main() void {
618 \\ foo() catch print();
619 \\}
620 \\
621 \\fn foo() anyerror!void {
622 \\ return error.Test;
623 \\}
624 \\
625 \\fn print() void {
626 \\ asm volatile ("svc #0"
627 \\ :
628 \\ : [number] "{r7}" (4),
629 \\ [arg1] "{r0}" (1),
630 \\ [arg2] "{r1}" (@ptrToInt("Hello, World!\n")),
631 \\ [arg3] "{r2}" ("Hello, World!\n".len),
632 \\ : "memory"
633 \\ );
634 \\ return;
635 \\}
636 ,
637 "Hello, World!\n",
638 );
639 }
571640}