authorgravatar for ian@ianjohnson.devIan Johnson <ian@ianjohnson.dev> 2023-07-04 00:11:06-04:00
committergravatar for kappaloris@gmail.comLoris Cro <kappaloris@gmail.com> 2023-07-06 17:28:11+02:00
log91daf1c8d8a64133f18dcec2b96e9f9f4326fe36
tree582dae6a965ca3e7d61899ae8ec745b8a2e5598f
parent8ce68e5f411b63fd989a1deb202286f7711c7e4e

Autodoc: implement boolean operations


3 files changed, 68 insertions(+), 3 deletions(-)

lib/docs/main.js+14
......@@ -1078,6 +1078,9 @@ Happy writing!
10781078 case "void": {
10791079 return "void";
10801080 }
1081 case "unreachable": {
1082 return "unreachable";
1083 }
10811084 case "slice": {
10821085 let payloadHtml = "";
10831086 const lhsExpr = zigAnalysis.exprs[expr.slice.lhs];
......@@ -1386,6 +1389,9 @@ Happy writing!
13861389 case "bit_not": {
13871390 return "~" + param;
13881391 }
1392 case "bool_not": {
1393 return "!" + param;
1394 }
13891395 case "clz": {
13901396 return "@clz(T" + ", " + param + ")";
13911397 }
......@@ -1657,6 +1663,14 @@ Happy writing!
16571663 operator += "<=";
16581664 break;
16591665 }
1666 case "bool_br_and": {
1667 operator += "and";
1668 break;
1669 }
1670 case "bool_br_or": {
1671 operator += "or";
1672 break;
1673 }
16601674 default:
16611675 console.log("operator not handled yet or doesn't exist!");
16621676 }
src/Autodoc.zig+52-1
......@@ -797,7 +797,7 @@ const DocData = struct {
797797 }
798798 };
799799
800 /// An Expr represents the (untyped) result of analizing instructions.
800 /// An Expr represents the (untyped) result of analyzing instructions.
801801 /// The data is normalized, which means that an Expr that results in a
802802 /// type definition will hold an index into `self.types`.
803803 pub const Expr = union(enum) {
......@@ -1262,6 +1262,12 @@ fn walkInstruction(
12621262 .expr = .{ .int_big = .{ .value = as_string } },
12631263 };
12641264 },
1265 .@"unreachable" => {
1266 return DocData.WalkResult{
1267 .typeRef = .{ .type = @intFromEnum(Ref.noreturn_type) },
1268 .expr = .{ .@"unreachable" = .{} },
1269 };
1270 },
12651271
12661272 .slice_start => {
12671273 const pl_node = data[inst_index].pl_node;
......@@ -1580,6 +1586,7 @@ fn walkInstruction(
15801586 .frame_size,
15811587 .int_from_ptr,
15821588 .bit_not,
1589 .bool_not,
15831590 // @check
15841591 .clz,
15851592 .ctz,
......@@ -1602,6 +1609,40 @@ fn walkInstruction(
16021609 .expr = .{ .builtinIndex = bin_index },
16031610 };
16041611 },
1612 .bool_br_and, .bool_br_or => {
1613 const bool_br = data[inst_index].bool_br;
1614
1615 const bin_index = self.exprs.items.len;
1616 try self.exprs.append(self.arena, .{ .binOp = .{ .lhs = 0, .rhs = 0 } });
1617
1618 const lhs = try self.walkRef(
1619 file,
1620 parent_scope,
1621 parent_src,
1622 bool_br.lhs,
1623 false,
1624 );
1625 const lhs_index = self.exprs.items.len;
1626 try self.exprs.append(self.arena, lhs.expr);
1627
1628 const extra = file.zir.extraData(Zir.Inst.Block, bool_br.payload_index);
1629 const rhs = try self.walkInstruction(
1630 file,
1631 parent_scope,
1632 parent_src,
1633 file.zir.extra[extra.end..][extra.data.body_len - 1],
1634 false,
1635 );
1636 const rhs_index = self.exprs.items.len;
1637 try self.exprs.append(self.arena, rhs.expr);
1638
1639 self.exprs.items[bin_index] = .{ .binOp = .{ .name = @tagName(tags[inst_index]), .lhs = lhs_index, .rhs = rhs_index } };
1640
1641 return DocData.WalkResult{
1642 .typeRef = .{ .type = @intFromEnum(Ref.bool_type) },
1643 .expr = .{ .binOpIndex = bin_index },
1644 };
1645 },
16051646 .truncate => {
16061647 // in the ZIR this node is a builtin `bin` but we want send it as a `un` builtin
16071648 const pl_node = data[inst_index].pl_node;
......@@ -2359,6 +2400,16 @@ fn walkInstruction(
23592400 need_type,
23602401 );
23612402 },
2403 .break_inline => {
2404 const @"break" = data[inst_index].@"break";
2405 return try self.walkRef(
2406 file,
2407 parent_scope,
2408 parent_src,
2409 @"break".operand,
2410 need_type,
2411 );
2412 },
23622413 .struct_init => {
23632414 const pl_node = data[inst_index].pl_node;
23642415 const extra = file.zir.extraData(Zir.Inst.StructInit, pl_node.payload_index);
src/Zir.zig+2-2
......@@ -255,7 +255,7 @@ pub const Inst = struct {
255255 /// Uses the pl_node field with payload `Bin`.
256256 bitcast,
257257 /// Bitwise NOT. `~`
258 /// Uses `un_tok`.
258 /// Uses `un_node`.
259259 bit_not,
260260 /// Bitwise OR. `|`
261261 bit_or,
......@@ -274,7 +274,7 @@ pub const Inst = struct {
274274 /// Uses the `pl_node` union field. Payload is `Block`.
275275 suspend_block,
276276 /// Boolean NOT. See also `bit_not`.
277 /// Uses the `un_tok` field.
277 /// Uses the `un_node` field.
278278 bool_not,
279279 /// Short-circuiting boolean `and`. `lhs` is a boolean `Ref` and the other operand
280280 /// is a block, which is evaluated if `lhs` is `true`.