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!...@@ -1078,6 +1078,9 @@ Happy writing!
1078 case "void": {1078 case "void": {
1079 return "void";1079 return "void";
1080 }1080 }
1081 case "unreachable": {
1082 return "unreachable";
1083 }
1081 case "slice": {1084 case "slice": {
1082 let payloadHtml = "";1085 let payloadHtml = "";
1083 const lhsExpr = zigAnalysis.exprs[expr.slice.lhs];1086 const lhsExpr = zigAnalysis.exprs[expr.slice.lhs];
...@@ -1386,6 +1389,9 @@ Happy writing!...@@ -1386,6 +1389,9 @@ Happy writing!
1386 case "bit_not": {1389 case "bit_not": {
1387 return "~" + param;1390 return "~" + param;
1388 }1391 }
1392 case "bool_not": {
1393 return "!" + param;
1394 }
1389 case "clz": {1395 case "clz": {
1390 return "@clz(T" + ", " + param + ")";1396 return "@clz(T" + ", " + param + ")";
1391 }1397 }
...@@ -1657,6 +1663,14 @@ Happy writing!...@@ -1657,6 +1663,14 @@ Happy writing!
1657 operator += "<=";1663 operator += "<=";
1658 break;1664 break;
1659 }1665 }
1666 case "bool_br_and": {
1667 operator += "and";
1668 break;
1669 }
1670 case "bool_br_or": {
1671 operator += "or";
1672 break;
1673 }
1660 default:1674 default:
1661 console.log("operator not handled yet or doesn't exist!");1675 console.log("operator not handled yet or doesn't exist!");
1662 }1676 }
src/Autodoc.zig+52-1
...@@ -797,7 +797,7 @@ const DocData = struct {...@@ -797,7 +797,7 @@ const DocData = struct {
797 }797 }
798 };798 };
799799
800 /// An Expr represents the (untyped) result of analizing instructions.800 /// An Expr represents the (untyped) result of analyzing instructions.
801 /// The data is normalized, which means that an Expr that results in a801 /// The data is normalized, which means that an Expr that results in a
802 /// type definition will hold an index into `self.types`.802 /// type definition will hold an index into `self.types`.
803 pub const Expr = union(enum) {803 pub const Expr = union(enum) {
...@@ -1262,6 +1262,12 @@ fn walkInstruction(...@@ -1262,6 +1262,12 @@ fn walkInstruction(
1262 .expr = .{ .int_big = .{ .value = as_string } },1262 .expr = .{ .int_big = .{ .value = as_string } },
1263 };1263 };
1264 },1264 },
1265 .@"unreachable" => {
1266 return DocData.WalkResult{
1267 .typeRef = .{ .type = @intFromEnum(Ref.noreturn_type) },
1268 .expr = .{ .@"unreachable" = .{} },
1269 };
1270 },
12651271
1266 .slice_start => {1272 .slice_start => {
1267 const pl_node = data[inst_index].pl_node;1273 const pl_node = data[inst_index].pl_node;
...@@ -1580,6 +1586,7 @@ fn walkInstruction(...@@ -1580,6 +1586,7 @@ fn walkInstruction(
1580 .frame_size,1586 .frame_size,
1581 .int_from_ptr,1587 .int_from_ptr,
1582 .bit_not,1588 .bit_not,
1589 .bool_not,
1583 // @check1590 // @check
1584 .clz,1591 .clz,
1585 .ctz,1592 .ctz,
...@@ -1602,6 +1609,40 @@ fn walkInstruction(...@@ -1602,6 +1609,40 @@ fn walkInstruction(
1602 .expr = .{ .builtinIndex = bin_index },1609 .expr = .{ .builtinIndex = bin_index },
1603 };1610 };
1604 },1611 },
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 },
1605 .truncate => {1646 .truncate => {
1606 // in the ZIR this node is a builtin `bin` but we want send it as a `un` builtin1647 // in the ZIR this node is a builtin `bin` but we want send it as a `un` builtin
1607 const pl_node = data[inst_index].pl_node;1648 const pl_node = data[inst_index].pl_node;
...@@ -2359,6 +2400,16 @@ fn walkInstruction(...@@ -2359,6 +2400,16 @@ fn walkInstruction(
2359 need_type,2400 need_type,
2360 );2401 );
2361 },2402 },
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 },
2362 .struct_init => {2413 .struct_init => {
2363 const pl_node = data[inst_index].pl_node;2414 const pl_node = data[inst_index].pl_node;
2364 const extra = file.zir.extraData(Zir.Inst.StructInit, pl_node.payload_index);2415 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 {...@@ -255,7 +255,7 @@ pub const Inst = struct {
255 /// Uses the pl_node field with payload `Bin`.255 /// Uses the pl_node field with payload `Bin`.
256 bitcast,256 bitcast,
257 /// Bitwise NOT. `~`257 /// Bitwise NOT. `~`
258 /// Uses `un_tok`.258 /// Uses `un_node`.
259 bit_not,259 bit_not,
260 /// Bitwise OR. `|`260 /// Bitwise OR. `|`
261 bit_or,261 bit_or,
...@@ -274,7 +274,7 @@ pub const Inst = struct {...@@ -274,7 +274,7 @@ pub const Inst = struct {
274 /// Uses the `pl_node` union field. Payload is `Block`.274 /// Uses the `pl_node` union field. Payload is `Block`.
275 suspend_block,275 suspend_block,
276 /// Boolean NOT. See also `bit_not`.276 /// Boolean NOT. See also `bit_not`.
277 /// Uses the `un_tok` field.277 /// Uses the `un_node` field.
278 bool_not,278 bool_not,
279 /// Short-circuiting boolean `and`. `lhs` is a boolean `Ref` and the other operand279 /// Short-circuiting boolean `and`. `lhs` is a boolean `Ref` and the other operand
280 /// is a block, which is evaluated if `lhs` is `true`.280 /// is a block, which is evaluated if `lhs` is `true`.