aboutsummaryrefslogtreecommitdiff
path: root/src/codegen/c.zig
diff options
context:
space:
mode:
authorDavid Rubin <87927264+Rexicon226@users.noreply.github.com>2024-05-22 08:51:16 -0700
committerGitHub <noreply@github.com>2024-05-22 10:51:16 -0500
commita7de02e05216db9a04e438703ddf1b6b12f3fbef (patch)
treebf70046bbcf24c00bf201c0638f2f90543a62df5 /src/codegen/c.zig
parented75f62568f64c0d3859aab35aaf5289c3f07026 (diff)
downloadzig-a7de02e05216db9a04e438703ddf1b6b12f3fbef.tar.gz
zig-a7de02e05216db9a04e438703ddf1b6b12f3fbef.zip
implement `@expect` builtin (#19658)
* implement `@expect` * add docs * add a second arg for expected bool * fix typo * move `expect` to use BinOp * update to newer langref format
Diffstat (limited to 'src/codegen/c.zig')
-rw-r--r--src/codegen/c.zig23
1 files changed, 23 insertions, 0 deletions
diff --git a/src/codegen/c.zig b/src/codegen/c.zig
index 9514b826ea..f3ff666802 100644
--- a/src/codegen/c.zig
+++ b/src/codegen/c.zig
@@ -3343,6 +3343,8 @@ fn genBodyInner(f: *Function, body: []const Air.Inst.Index) error{ AnalysisFail,
.@"try" => try airTry(f, inst),
.try_ptr => try airTryPtr(f, inst),
+
+ .expect => try airExpect(f, inst),
.dbg_stmt => try airDbgStmt(f, inst),
.dbg_inline_block => try airDbgInlineBlock(f, inst),
@@ -4704,6 +4706,27 @@ fn airTryPtr(f: *Function, inst: Air.Inst.Index) !CValue {
return lowerTry(f, inst, extra.data.ptr, body, err_union_ty, true);
}
+fn airExpect(f: *Function, inst: Air.Inst.Index) !CValue {
+ const bin_op = f.air.instructions.items(.data)[@intFromEnum(inst)].bin_op;
+ const operand = try f.resolveInst(bin_op.lhs);
+ const expected = try f.resolveInst(bin_op.rhs);
+
+ const writer = f.object.writer();
+ const local = try f.allocLocal(inst, Type.bool);
+ const a = try Assignment.start(f, writer, CType.bool);
+ try f.writeCValue(writer, local, .Other);
+ try a.assign(f, writer);
+
+ try writer.writeAll("zig_expect(");
+ try f.writeCValue(writer, operand, .FunctionArgument);
+ try writer.writeAll(", ");
+ try f.writeCValue(writer, expected, .FunctionArgument);
+ try writer.writeAll(")");
+
+ try a.end(f, writer);
+ return local;
+}
+
fn lowerTry(
f: *Function,
inst: Air.Inst.Index,