Skip to main content

ironwork_rt/lir/
arith.rs

1//! Arithmetic plans (lir.md ยง7): what the walker works out on each execution, fixed at lowering.
2
3use super::{AbendId, ExprId, PlaceId, SymId};
4use crate::vocab::SignClause;
5use crate::{codec_enum, codec_struct};
6use numeric::Arith;
7use zarch::hfp::Precision;
8
9#[derive(Clone, Debug, PartialEq, Eq)]
10pub struct ArithPlan {
11    pub dmax: u32,
12    pub arith: Arith,
13    /// The places the walker's dmax pre-pass locates, in its order, static ones left out.
14    pub prepass: Vec<PlaceId>,
15    pub steps: Vec<ArithStep>,
16    pub remainder: Option<RemainderPlan>,
17    /// ON or NOT ON SIZE ERROR is written.
18    pub handled: bool,
19    /// ADD, SUBTRACT, MULTIPLY or DIVIDE rather than COMPUTE: a step whose expression has its own
20    /// receiver as an operand of its top operation evaluates the other operand with every step's,
21    /// and reads the receiver only when it stores.
22    pub per_receiver: bool,
23}
24
25/// `probe` holds the places the walker's float test locates after the receiver, static ones left out.
26#[derive(Clone, Debug, PartialEq, Eq)]
27pub struct ArithStep {
28    pub target: PlaceId,
29    pub expr: ExprId,
30    pub mode: Mode,
31    pub store: StorePlan,
32    pub rounded: bool,
33    pub probe: Vec<PlaceId>,
34}
35
36#[derive(Clone, Copy, Debug, PartialEq, Eq)]
37pub enum Mode {
38    Fixed,
39    Float(Precision),
40}
41
42#[derive(Clone, Copy, Debug, PartialEq, Eq)]
43pub enum StorePlan {
44    Zoned { digits: u32, scale: u32, signed: bool, sign: Option<SignClause> },
45    Packed { digits: u32, scale: u32, signed: bool },
46    /// `name` labels a TRUNC(OPT) report.
47    Binary { digits: u32, scale: u32, signed: bool, native: bool, name: SymId },
48    NumericEdited { edit: u32, digits: u32, scale: u32, blank_when_zero: bool },
49    Float(Precision),
50    Index,
51    Refused(AbendId),
52}
53
54#[derive(Clone, Copy, Debug, PartialEq, Eq)]
55pub struct RemainderPlan {
56    pub target: PlaceId,
57    pub dividend: ExprId,
58    pub divisor: ExprId,
59    pub quotient_scale: u32,
60    pub store: StorePlan,
61}
62
63/// PERFORM VARYING's step, SET UP and DOWN BY, and the TALLYING adds: no ROUNDED, no size error.
64#[derive(Clone, Copy, Debug, PartialEq, Eq)]
65pub struct StepPlan {
66    pub dmax: u32,
67    pub store: StorePlan,
68}
69
70/// SET UP BY or DOWN BY on one receiver, by what reading it gives: an address moved by the step,
71/// a number the step is added to (no ROUNDED, no size error), or a value the walker refuses once
72/// it has read it.
73#[derive(Clone, Copy, Debug, PartialEq, Eq)]
74pub enum UpDown {
75    Pointer,
76    Number(StepPlan),
77    Refused(AbendId),
78}
79
80codec_struct!(ArithPlan { dmax, arith, prepass, steps, remainder, handled, per_receiver });
81codec_struct!(ArithStep { target, expr, mode, store, rounded, probe });
82codec_enum!(Mode { Fixed = 0, Float(precision) = 1 });
83codec_enum!(StorePlan {
84    Zoned { digits, scale, signed, sign } = 0,
85    Packed { digits, scale, signed } = 1,
86    Binary { digits, scale, signed, native, name } = 2,
87    NumericEdited { edit, digits, scale, blank_when_zero } = 3,
88    Float(precision) = 4,
89    Index = 5,
90    Refused(abend) = 6,
91});
92codec_struct!(RemainderPlan { target, dividend, divisor, quotient_scale, store });
93codec_struct!(StepPlan { dmax, store });
94codec_enum!(UpDown { Pointer = 0, Number(plan) = 1, Refused(abend) = 2 });