Skip to main content

ironwork_rt/lir/
payload.rs

1//! Statement payloads from lir.md §9: MOVE, INITIALIZE, DISPLAY, SEARCH ALL, FUNCTION and INVOKE.
2//! DISPLAY's, SEARCH ALL's, INVOKE's and those of `text`, `call` and `sql` are generic over the
3//! handles they name (semantics-library.md §9, C6): the LIR's ids by default, the walker's own
4//! references in the interpreter.
5
6use super::{AbendId, Comparand, Compare, Count, DebugId, IntExpr, Operand, PlaceId, RefMod, StorePlan, SymId};
7use crate::vocab::Figurative;
8use crate::{codec_enum, codec_struct};
9use zarch::hfp::Precision;
10
11#[derive(Clone, Copy, Debug, PartialEq, Eq)]
12pub enum MovePlan {
13    Alnum { image: Image, justified: bool },
14    AlnumEdited { image: Image, edit: u32, positions: u32 },
15    National(NationalFrom),
16    Numeric { from: NumericFrom, store: StorePlan },
17    Float { from: FloatFrom, precision: Precision },
18    Address,
19    Index,
20    Refused(AbendId),
21}
22
23#[derive(Clone, Copy, Debug, PartialEq, Eq)]
24pub enum Image {
25    Bytes,
26    All,
27    Figurative,
28    Digits { digits: u32 },
29    /// A numeric, floating-point or pointer sender's own bytes as stored, once it has been read.
30    Stored,
31}
32
33#[derive(Clone, Copy, Debug, PartialEq, Eq)]
34pub enum NationalFrom {
35    Units,
36    Decoded,
37    Figurative,
38}
39
40#[derive(Clone, Copy, Debug, PartialEq, Eq)]
41pub enum NumericFrom {
42    /// The sender as a number; a zoned or packed sender through `store::move_sender`, which gives
43    /// digits that are not decimal as bytes to carry unchecked (C260).
44    Value,
45    /// NUMPROC(PFD), packed to packed of the same kind and scale: the bytes.
46    PackedCopy,
47    Float,
48    Zero,
49    /// A figurative other than ZERO, or an ALL literal: bytes filled, not converted.
50    Fill,
51    /// Alphanumeric bytes read as an unsigned zoned integer of their length; to a zoned or packed
52    /// integer without P scaling, the low halves of their last bytes, stored unchecked (C240).
53    Zoned,
54    DeEdit { edit: u32, digits: u32, scale: u32 },
55}
56
57#[derive(Clone, Copy, Debug, PartialEq, Eq)]
58pub enum FloatFrom {
59    Float,
60    Fixed,
61    Zero,
62}
63
64/// INITIALIZE of one item: each elementary item the walk reaches, every occurrence listed.
65#[derive(Clone, Debug, PartialEq, Eq)]
66pub struct InitPlan {
67    pub fields: Vec<InitField>,
68}
69
70/// SPACE, ZERO or NULL, as the walker gives the item's kind, moved into `len` bytes at `offset` from
71/// the target's start.
72#[derive(Clone, Copy, Debug, PartialEq, Eq)]
73pub struct InitField {
74    pub offset: u32,
75    pub len: u32,
76    pub value: Figurative,
77    pub store: MovePlan,
78}
79
80/// DISPLAY's items, each shown as the walker shows its kind, then a newline unless NO ADVANCING.
81#[derive(Clone, Debug, PartialEq, Eq)]
82pub struct DisplayPlan<P = PlaceId, O = Operand> {
83    pub items: Vec<DisplayItem<P, O>>,
84    pub no_advancing: bool,
85}
86
87#[derive(Clone, Copy, Debug, PartialEq, Eq)]
88pub enum DisplayItem<P = PlaceId, O = Operand> {
89    /// Groups, alphanumeric, zoned and edited items: the storage in the program's code page.
90    Bytes(P),
91    National(P),
92    /// Packed and binary items: the value's last `digits` digits, which for COMP-5 or TRUNC(BIN)
93    /// are as many as the item's halfword, fullword or doubleword holds.
94    Digits { place: P, digits: u32, signed: bool },
95    /// Floating-point, pointer, index and object-reference items: the place, then the abend.
96    Refused { place: P, abend: AbendId },
97    /// A literal or figurative constant as DISPLAY shows it; a numeric literal as written, its decimal
98    /// point the program's.
99    Text(SymId),
100    /// FUNCTION, LENGTH OF or ADDRESS OF, by the value's kind.
101    Value(O),
102}
103
104/// SEARCH ALL's binary search. The op returns Arm(0) on an occurrence whose keys equal their WHEN
105/// terms, for a Branch on the whole condition, and Arm(1) when the search ends without one.
106#[derive(Clone, Debug, PartialEq, Eq)]
107pub struct SearchAllPlan<P = PlaceId, V = Comparand, N = Count> {
108    /// The table's first index, stored by `store` at each occurrence tried.
109    pub index: P,
110    pub store: StorePlan,
111    pub count: N,
112    /// The keys the WHEN condition tests for equality, in the table's KEY order.
113    pub keys: Vec<SearchKey<V>>,
114}
115
116/// `key` as the WHEN term names it, and the `value` it must equal.
117#[derive(Clone, Copy, Debug, PartialEq, Eq)]
118pub struct SearchKey<V = Comparand> {
119    pub ascending: bool,
120    pub key: V,
121    pub value: V,
122    pub how: Compare,
123}
124
125/// FUNCTION, as the walker evaluates it: each argument in turn, as a comparison evaluates an
126/// operand or expression; then, when the values the arguments give number outside `func.arity()`,
127/// `arity`'s abend (lowering leaves `arity` None where they cannot); then the function, which for
128/// CHAR, INTEGER-OF-DATE, DATE-OF-INTEGER and RANDOM evaluates its first argument again as
129/// `integer`, and for NATIONAL-OF with two arguments its second; last, on an alphanumeric result,
130/// `refmod`'s start and length. `refmod.check` is always false: the walker checks a function's
131/// reference modification against its result whatever SSRANGE says.
132///
133/// HEX-OF, BIT-OF and BYTE-LENGTH read a `Load` argument's bytes as stored, and any other
134/// argument's value as DISPLAY would hold it. WHEN-COMPILED reads `ProgramOptions.when_compiled`.
135#[derive(Clone, Debug, PartialEq, Eq)]
136pub struct FunctionPlan {
137    pub func: Func,
138    pub args: Vec<Argument>,
139    pub integer: Option<IntExpr>,
140    /// TRIM's LEADING or TRAILING.
141    pub side: Option<TrimSide>,
142    pub refmod: Option<RefMod>,
143    pub arity: Option<AbendId>,
144    pub at: DebugId,
145}
146
147/// A FUNCTION argument: one value, or the elements of a table written with ALL subscripts.
148#[derive(Clone, Debug, PartialEq, Eq)]
149pub enum Argument {
150    Value(Comparand),
151    /// `element` is the table as written, each ALL subscript 1. Each `(position, count)` is an ALL
152    /// subscript's position among the place's subscripts and its occurrences. The counts are
153    /// evaluated first, left to right; then, unless one is zero, each element with its ALL
154    /// subscripts set, the rightmost varying fastest, is evaluated and read as `Load` reads it.
155    All { element: PlaceId, all: Vec<(u32, Count)> },
156}
157
158/// One row per intrinsic function: its variant, its tag in a load module, its name, and the
159/// fewest and most arguments it takes. Adding a function is adding its row.
160macro_rules! functions {
161    ($($variant:ident = $tag:literal, $name:literal, $min:literal ..= $max:expr;)*) => {
162        #[derive(Clone, Copy, Debug, PartialEq, Eq)]
163        pub enum Func {
164            $($variant,)*
165        }
166
167        impl Func {
168            pub const ALL: &'static [Func] = &[$(Func::$variant,)*];
169
170            pub fn name(self) -> &'static str {
171                match self {
172                    $(Func::$variant => $name,)*
173                }
174            }
175
176            pub fn named(name: &str) -> Option<Func> {
177                match name {
178                    $($name => Some(Func::$variant),)*
179                    _ => None,
180                }
181            }
182
183            /// The argument counts the function takes.
184            pub fn arity(self) -> ::core::ops::RangeInclusive<usize> {
185                match self {
186                    $(Func::$variant => $min..=$max,)*
187                }
188            }
189        }
190
191        codec_enum!(Func { $($variant = $tag,)* });
192    };
193}
194
195functions! {
196    Char = 0, "CHAR", 1..=1;
197    Ord = 1, "ORD", 1..=1;
198    NationalOf = 2, "NATIONAL-OF", 1..=2;
199    Length = 3, "LENGTH", 1..=1;
200    UpperCase = 4, "UPPER-CASE", 1..=1;
201    LowerCase = 5, "LOWER-CASE", 1..=1;
202    Reverse = 6, "REVERSE", 1..=1;
203    CurrentDate = 7, "CURRENT-DATE", 0..=0;
204    Numval = 8, "NUMVAL", 1..=2;
205    NumvalC = 9, "NUMVAL-C", 1..=2;
206    Trim = 10, "TRIM", 1..=1;
207    Mod = 11, "MOD", 2..=2;
208    Rem = 12, "REM", 2..=2;
209    Integer = 13, "INTEGER", 1..=1;
210    IntegerPart = 14, "INTEGER-PART", 1..=1;
211    Abs = 15, "ABS", 1..=1;
212    Min = 16, "MIN", 1..=usize::MAX;
213    Max = 17, "MAX", 1..=usize::MAX;
214    IntegerOfDate = 18, "INTEGER-OF-DATE", 1..=1;
215    DateOfInteger = 19, "DATE-OF-INTEGER", 1..=1;
216    Random = 20, "RANDOM", 0..=1;
217    Acos = 21, "ACOS", 1..=1;
218    Annuity = 22, "ANNUITY", 2..=2;
219    Asin = 23, "ASIN", 1..=1;
220    Atan = 24, "ATAN", 1..=1;
221    BitOf = 25, "BIT-OF", 1..=1;
222    BitToChar = 26, "BIT-TO-CHAR", 1..=1;
223    ByteLength = 27, "BYTE-LENGTH", 1..=1;
224    Cos = 28, "COS", 1..=1;
225    DateToYyyymmdd = 29, "DATE-TO-YYYYMMDD", 1..=2;
226    DayOfInteger = 30, "DAY-OF-INTEGER", 1..=1;
227    DayToYyyyddd = 31, "DAY-TO-YYYYDDD", 1..=2;
228    DisplayOf = 32, "DISPLAY-OF", 1..=2;
229    E = 33, "E", 0..=0;
230    Exp = 34, "EXP", 1..=1;
231    Exp10 = 35, "EXP10", 1..=1;
232    Factorial = 36, "FACTORIAL", 1..=1;
233    FormattedCurrentDate = 37, "FORMATTED-CURRENT-DATE", 1..=1;
234    FormattedDate = 38, "FORMATTED-DATE", 2..=2;
235    FormattedDatetime = 39, "FORMATTED-DATETIME", 3..=4;
236    FormattedTime = 40, "FORMATTED-TIME", 2..=3;
237    HexOf = 41, "HEX-OF", 1..=1;
238    HexToChar = 42, "HEX-TO-CHAR", 1..=1;
239    IntegerOfDay = 43, "INTEGER-OF-DAY", 1..=1;
240    IntegerOfFormattedDate = 44, "INTEGER-OF-FORMATTED-DATE", 2..=2;
241    Log = 45, "LOG", 1..=1;
242    Log10 = 46, "LOG10", 1..=1;
243    Mean = 47, "MEAN", 1..=usize::MAX;
244    Median = 48, "MEDIAN", 1..=usize::MAX;
245    Midrange = 49, "MIDRANGE", 1..=usize::MAX;
246    NumvalF = 50, "NUMVAL-F", 1..=1;
247    OrdMax = 51, "ORD-MAX", 1..=usize::MAX;
248    OrdMin = 52, "ORD-MIN", 1..=usize::MAX;
249    Pi = 53, "PI", 0..=0;
250    PresentValue = 54, "PRESENT-VALUE", 2..=usize::MAX;
251    Range = 55, "RANGE", 1..=usize::MAX;
252    SecondsFromFormattedTime = 56, "SECONDS-FROM-FORMATTED-TIME", 2..=2;
253    SecondsPastMidnight = 57, "SECONDS-PAST-MIDNIGHT", 0..=0;
254    Sign = 58, "SIGN", 1..=1;
255    Sin = 59, "SIN", 1..=1;
256    Sqrt = 60, "SQRT", 1..=1;
257    StandardDeviation = 61, "STANDARD-DEVIATION", 1..=usize::MAX;
258    Sum = 62, "SUM", 1..=usize::MAX;
259    Tan = 63, "TAN", 1..=1;
260    TestDateYyyymmdd = 64, "TEST-DATE-YYYYMMDD", 1..=1;
261    TestDayYyyyddd = 65, "TEST-DAY-YYYYDDD", 1..=1;
262    TestFormattedDatetime = 66, "TEST-FORMATTED-DATETIME", 2..=2;
263    TestNumval = 67, "TEST-NUMVAL", 1..=1;
264    TestNumvalC = 68, "TEST-NUMVAL-C", 1..=2;
265    TestNumvalF = 69, "TEST-NUMVAL-F", 1..=1;
266    Uuid4 = 70, "UUID4", 0..=0;
267    Variance = 71, "VARIANCE", 1..=usize::MAX;
268    YearToYyyy = 72, "YEAR-TO-YYYY", 1..=2;
269    WhenCompiled = 73, "WHEN-COMPILED", 0..=0;
270    Ulength = 74, "ULENGTH", 1..=1;
271    Upos = 75, "UPOS", 2..=2;
272    Usubstr = 76, "USUBSTR", 3..=3;
273    Usupplementary = 77, "USUPPLEMENTARY", 1..=1;
274    Uvalid = 78, "UVALID", 1..=1;
275    Uwidth = 79, "UWIDTH", 2..=2;
276    CombinedDatetime = 80, "COMBINED-DATETIME", 2..=2;
277    ContentOf = 81, "CONTENT-OF", 1..=1;
278}
279
280#[derive(Clone, Copy, Debug, PartialEq, Eq)]
281pub enum TrimSide {
282    Leading,
283    Trailing,
284}
285
286/// `args` and `returning` carry each Java type signature. The op evaluates the method name, then the
287/// receiver, then each argument, and returns Arm(1) when no method matches and ON EXCEPTION is
288/// written, Arm(0) otherwise, or Next when neither phrase is written. `returning` is moved by the
289/// kind of the value the method returns.
290#[derive(Clone, Debug, PartialEq, Eq)]
291pub struct InvokePlan<P = PlaceId, O = Operand, S = SymId> {
292    pub receiver: Receiver<P, S>,
293    pub method: MethodName<P, S>,
294    pub args: Vec<(O, S)>,
295    pub returning: Option<(P, S)>,
296    pub on_exception: bool,
297    pub not_on_exception: bool,
298}
299
300/// `Class` is a REPOSITORY class-name: `name` as written, which messages give, and `external`, which
301/// finds the class.
302#[derive(Clone, Copy, Debug, PartialEq, Eq)]
303pub enum Receiver<P = PlaceId, S = SymId> {
304    SelfRef,
305    Super,
306    Class { name: S, external: S },
307    Object(P),
308}
309
310#[derive(Clone, Copy, Debug, PartialEq, Eq)]
311pub enum MethodName<P = PlaceId, S = SymId> {
312    New,
313    Named(S),
314    Dynamic(P),
315}
316
317codec_enum!(MovePlan {
318    Alnum { image, justified } = 0,
319    AlnumEdited { image, edit, positions } = 1,
320    National(from) = 2,
321    Numeric { from, store } = 3,
322    Float { from, precision } = 4,
323    Address = 5,
324    Index = 6,
325    Refused(abend) = 7,
326});
327codec_enum!(Image { Bytes = 0, All = 1, Figurative = 2, Digits { digits } = 3, Stored = 4 });
328codec_enum!(NationalFrom { Units = 0, Decoded = 1, Figurative = 2 });
329codec_enum!(NumericFrom {
330    Value = 0,
331    PackedCopy = 1,
332    Float = 2,
333    Zero = 3,
334    Fill = 4,
335    Zoned = 5,
336    DeEdit { edit, digits, scale } = 6,
337});
338codec_enum!(FloatFrom { Float = 0, Fixed = 1, Zero = 2 });
339codec_struct!(InitPlan { fields });
340codec_struct!(InitField { offset, len, value, store });
341codec_struct!(DisplayPlan { items, no_advancing });
342codec_enum!(DisplayItem {
343    Bytes(place) = 0,
344    National(place) = 1,
345    Digits { place, digits, signed } = 2,
346    Refused { place, abend } = 3,
347    Text(text) = 4,
348    Value(value) = 5,
349});
350codec_struct!(SearchAllPlan { index, store, count, keys });
351codec_struct!(SearchKey { ascending, key, value, how });
352codec_struct!(FunctionPlan { func, args, integer, side, refmod, arity, at });
353codec_enum!(Argument { Value(value) = 0, All { element, all } = 1 });
354codec_enum!(TrimSide { Leading = 0, Trailing = 1 });
355codec_struct!(InvokePlan { receiver, method, args, returning, on_exception, not_on_exception });
356codec_enum!(Receiver { SelfRef = 0, Super = 1, Class { name, external } = 2, Object(place) = 3 });
357codec_enum!(MethodName { New = 0, Named(name) = 1, Dynamic(place) = 2 });