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