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    /// The dmax of every operation below a step's top one, which `dmax` is for: lower than `dmax`
24    /// only under --dialect gnucobol, where a ROUNDED receiver's extra place counts in the top
25    /// operation alone (assumption C101).
26    pub inner_dmax: u32,
27}
28
29/// `probe` holds the places the walker's float test locates after the receiver, static ones left out.
30#[derive(Clone, Debug, PartialEq, Eq)]
31pub struct ArithStep {
32    pub target: PlaceId,
33    pub expr: ExprId,
34    pub mode: Mode,
35    pub store: StorePlan,
36    pub rounded: bool,
37    pub probe: Vec<PlaceId>,
38}
39
40#[derive(Clone, Copy, Debug, PartialEq, Eq)]
41pub enum Mode {
42    Fixed,
43    Float(Precision),
44}
45
46#[derive(Clone, Copy, Debug, PartialEq, Eq)]
47pub enum StorePlan {
48    Zoned { digits: u32, scale: u32, signed: bool, sign: Option<SignClause> },
49    Packed { digits: u32, scale: u32, signed: bool },
50    /// `name` labels a TRUNC(OPT) report.
51    Binary { digits: u32, scale: u32, signed: bool, native: bool, name: SymId },
52    NumericEdited { edit: u32, digits: u32, scale: u32, blank_when_zero: bool },
53    Float(Precision),
54    Index,
55    Refused(AbendId),
56}
57
58#[derive(Clone, Copy, Debug, PartialEq, Eq)]
59pub struct RemainderPlan {
60    pub target: PlaceId,
61    pub dividend: ExprId,
62    pub divisor: ExprId,
63    pub quotient_scale: u32,
64    pub store: StorePlan,
65}
66
67/// PERFORM VARYING's step, SET UP and DOWN BY, and the TALLYING adds: no ROUNDED, no size error.
68#[derive(Clone, Copy, Debug, PartialEq, Eq)]
69pub struct StepPlan {
70    pub dmax: u32,
71    pub store: StorePlan,
72}
73
74/// SET UP BY or DOWN BY on one receiver, by what reading it gives: an address moved by the step,
75/// a number the step is added to (no ROUNDED, no size error), or a value the walker refuses once
76/// it has read it.
77#[derive(Clone, Copy, Debug, PartialEq, Eq)]
78pub enum UpDown {
79    Pointer,
80    Number(StepPlan),
81    Refused(AbendId),
82}
83
84codec_struct!(ArithPlan { dmax, arith, prepass, steps, remainder, handled, per_receiver, inner_dmax });
85codec_struct!(ArithStep { target, expr, mode, store, rounded, probe });
86codec_enum!(Mode { Fixed = 0, Float(precision) = 1 });
87codec_enum!(StorePlan {
88    Zoned { digits, scale, signed, sign } = 0,
89    Packed { digits, scale, signed } = 1,
90    Binary { digits, scale, signed, native, name } = 2,
91    NumericEdited { edit, digits, scale, blank_when_zero } = 3,
92    Float(precision) = 4,
93    Index = 5,
94    Refused(abend) = 6,
95});
96codec_struct!(RemainderPlan { target, dividend, divisor, quotient_scale, store });
97codec_struct!(StepPlan { dmax, store });
98codec_enum!(UpDown { Pointer = 0, Number(plan) = 1, Refused(abend) = 2 });