Skip to content

Commit

Permalink
add typed ir insts for unwrap error union err and unwrap error union …
Browse files Browse the repository at this point in the history
…payload
  • Loading branch information
g-w1 committed Dec 31, 2020
1 parent 44fd783 commit 49d9b19
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 1 deletion.
19 changes: 19 additions & 0 deletions src/codegen.zig
Original file line number Diff line number Diff line change
Expand Up @@ -799,6 +799,8 @@ fn Function(comptime arch: std.Target.Cpu.Arch) type {
.switchbr => return self.genSwitch(inst.castTag(.switchbr).?),
.unreach => return MCValue{ .unreach = {} },
.unwrap_optional => return self.genUnwrapOptional(inst.castTag(.unwrap_optional).?),
.unwrap_err_err => return self.genUnwrapErrErr(inst.castTag(.unwrap_err_err).?),
.unwrap_err_payload => return self.genUnwrapErrPayload(inst.castTag(.unwrap_err_payload).?),
.wrap_optional => return self.genWrapOptional(inst.castTag(.wrap_optional).?),
.varptr => return self.genVarPtr(inst.castTag(.varptr).?),
.xor => return self.genXor(inst.castTag(.xor).?),
Expand Down Expand Up @@ -1051,6 +1053,23 @@ fn Function(comptime arch: std.Target.Cpu.Arch) type {
}
}

fn genUnwrapErrErr(self: *Self, inst: *ir.Inst.UnOp) !MCValue {
// No side effects, so if it's unreferenced, do nothing.
if (inst.base.isUnused())
return MCValue.dead;
switch (arch) {
else => return self.fail(inst.base.src, "TODO implement unwrap error union error for {}", .{self.target.cpu.arch}),
}
}

fn genUnwrapErrPayload(self: *Self, inst: *ir.Inst.UnOp) !MCValue {
// No side effects, so if it's unreferenced, do nothing.
if (inst.base.isUnused())
return MCValue.dead;
switch (arch) {
else => return self.fail(inst.base.src, "TODO implement unwrap error union payload for {}", .{self.target.cpu.arch}),
}
}
fn genWrapOptional(self: *Self, inst: *ir.Inst.UnOp) !MCValue {
const optional_ty = inst.base.ty;

Expand Down
6 changes: 6 additions & 0 deletions src/ir.zig
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,10 @@ pub const Inst = struct {
intcast,
unwrap_optional,
wrap_optional,
/// gets the payload of an error union
unwrap_err_payload,
/// gets the error from an error union
unwrap_err_err,
xor,
switchbr,

Expand All @@ -120,6 +124,8 @@ pub const Inst = struct {
.load,
.unwrap_optional,
.wrap_optional,
.unwrap_err_payload,
.unwrap_err_err,
=> UnOp,

.add,
Expand Down
4 changes: 3 additions & 1 deletion src/zir.zig
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,7 @@ pub const Inst = struct {
unwrap_optional_unsafe,
/// Gets the payload of an error union
unwrap_err_safe,
/// Same as previous, but without safety checks. Used for orelse, if and while
/// Same as previous, but without safety checks. Used for if, try, catch and while
unwrap_err_unsafe,
/// Gets the error code value of an error union
unwrap_err_code,
Expand Down Expand Up @@ -2371,6 +2371,8 @@ const EmitZIR = struct {
.iserr => try self.emitUnOp(inst.src, new_body, inst.castTag(.iserr).?, .iserr),
.load => try self.emitUnOp(inst.src, new_body, inst.castTag(.load).?, .deref),
.ref => try self.emitUnOp(inst.src, new_body, inst.castTag(.ref).?, .ref),
.unwrap_err_payload => try self.emitUnOp(inst.src, new_body, inst.castTag(.unwrap_err_payload).?, .unwrap_err_unsafe),
.unwrap_err_err => try self.emitUnOp(inst.src, new_body, inst.castTag(.unwrap_err_err).?, .unwrap_err_code),
.unwrap_optional => try self.emitUnOp(inst.src, new_body, inst.castTag(.unwrap_optional).?, .unwrap_optional_unsafe),
.wrap_optional => try self.emitCast(inst.src, new_body, inst.castTag(.wrap_optional).?, .as),

Expand Down

0 comments on commit 49d9b19

Please sign in to comment.