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