authorgravatar for joachim.schmidt557@outlook.comJoachim Schmidt <joachim.schmidt557@outlook.com> 2022-03-25 09:30:53+01:00
committergravatar for joachim.schmidt557@outlook.comJoachim Schmidt <joachim.schmidt557@outlook.com> 2022-03-26 17:21:39+01:00
log1803167db2d5045e71015498b8e5b184c53edf07
treef41994dd5eecaf7d62e10f68fe2dc48b934b9cd5
parentaf844931b2600e50e586436dee0d607d67ed9ff2
signaturelock-open Commit is signed but in an unrecognized format.

AIR: change signature of overflow arithmetic instructions

add_with_overflow and similar functions now have the ty_pl data attached. The Payload will now be a binary operation and the inst is expected to return a tuple consisting of the destination integer type and an overflow bit (u1). Co-authored-by: Jan Philipp Hafer <jan.hafer@rwth-aachen.de>

2 files changed, 20 insertions(+), 28 deletions(-)

src/Air.zig+16-22
......@@ -134,28 +134,24 @@ pub const Inst = struct {
134134 /// Uses the `bin_op` field.
135135 min,
136136 /// Integer addition with overflow. Both operands are guaranteed to be the same type,
137 /// and the result is bool. The wrapped value is written to the pointer given by the in
138 /// operand of the `pl_op` field. Payload is `Bin` with `lhs` and `rhs` the relevant types
139 /// of the operation.
140 /// Uses the `pl_op` field with payload `Bin`.
137 /// and the result is a tuple with .{res, ov}. The wrapped value is written to res
138 /// and if an overflow happens, ov is 1. Otherwise ov is 0.
139 /// Uses the `ty_pl` field. Payload is `Bin`.
141140 add_with_overflow,
142141 /// Integer subtraction with overflow. Both operands are guaranteed to be the same type,
143 /// and the result is bool. The wrapped value is written to the pointer given by the in
144 /// operand of the `pl_op` field. Payload is `Bin` with `lhs` and `rhs` the relevant types
145 /// of the operation.
146 /// Uses the `pl_op` field with payload `Bin`.
142 /// and the result is a tuple with .{res, ov}. The wrapped value is written to res
143 /// and if an overflow happens, ov is 1. Otherwise ov is 0.
144 /// Uses the `ty_pl` field. Payload is `Bin`.
147145 sub_with_overflow,
148146 /// Integer multiplication with overflow. Both operands are guaranteed to be the same type,
149 /// and the result is bool. The wrapped value is written to the pointer given by the in
150 /// operand of the `pl_op` field. Payload is `Bin` with `lhs` and `rhs` the relevant types
151 /// of the operation.
152 /// Uses the `pl_op` field with payload `Bin`.
147 /// and the result is a tuple with .{res, ov}. The wrapped value is written to res
148 /// and if an overflow happens, ov is 1. Otherwise ov is 0.
149 /// Uses the `ty_pl` field. Payload is `Bin`.
153150 mul_with_overflow,
154151 /// Integer left-shift with overflow. Both operands are guaranteed to be the same type,
155 /// and the result is bool. The wrapped value is written to the pointer given by the in
156 /// operand of the `pl_op` field. Payload is `Bin` with `lhs` and `rhs` the relevant types
157 /// of the operation.
158 /// Uses the `pl_op` field with payload `Bin`.
152 /// and the result is a tuple with .{res, ov}. The wrapped value is written to res
153 /// and if an overflow happens, ov is 1. Otherwise ov is 0.
154 /// Uses the `ty_pl` field. Payload is `Bin`.
159155 shl_with_overflow,
160156 /// Allocates stack local memory.
161157 /// Uses the `ty` field.
......@@ -964,6 +960,10 @@ pub fn typeOfIndex(air: Air, inst: Air.Inst.Index) Type {
964960 .union_init,
965961 .field_parent_ptr,
966962 .cmp_vector,
963 .add_with_overflow,
964 .sub_with_overflow,
965 .mul_with_overflow,
966 .shl_with_overflow,
967967 => return air.getRefType(datas[inst].ty_pl.ty),
968968
969969 .not,
......@@ -1074,12 +1074,6 @@ pub fn typeOfIndex(air: Air, inst: Air.Inst.Index) Type {
10741074 const extra = air.extraData(Air.Bin, datas[inst].pl_op.payload).data;
10751075 return air.typeOf(extra.lhs);
10761076 },
1077
1078 .add_with_overflow,
1079 .sub_with_overflow,
1080 .mul_with_overflow,
1081 .shl_with_overflow,
1082 => return Type.bool,
10831077 }
10841078}
10851079
src/print_air.zig+4-6
......@@ -473,14 +473,12 @@ const Writer = struct {
473473 }
474474
475475 fn writeOverflow(w: *Writer, s: anytype, inst: Air.Inst.Index) @TypeOf(s).Error!void {
476 const pl_op = w.air.instructions.items(.data)[inst].pl_op;
477 const extra = w.air.extraData(Air.Bin, pl_op.payload).data;
476 const ty_pl = w.air.instructions.items(.data)[inst].ty_pl;
477 const extra = w.air.extraData(Air.Bin, ty_pl.payload).data;
478478
479 try w.writeOperand(s, inst, 0, pl_op.operand);
480 try s.writeAll(", ");
481 try w.writeOperand(s, inst, 1, extra.lhs);
479 try w.writeOperand(s, inst, 0, extra.lhs);
482480 try s.writeAll(", ");
483 try w.writeOperand(s, inst, 2, extra.rhs);
481 try w.writeOperand(s, inst, 1, extra.rhs);
484482 }
485483
486484 fn writeMemset(w: *Writer, s: anytype, inst: Air.Inst.Index) @TypeOf(s).Error!void {