Skip to main content

ironwork_rt/lir/
codec.rs

1//! Codecs for the types the LIR borrows from `rt`, `numeric` and `zarch`, with load-module.md's tags.
2
3use crate::abend::{AbendCode, Ending, FileStatus, Signal};
4use crate::files::Format;
5use crate::module::ModuleError;
6use crate::module::codec::{Decode, Encode, Reader, Writer};
7use crate::picture::Sym;
8use crate::sql::HostType;
9use crate::storage::Kind;
10use crate::vocab::{AcceptFrom, BinOp, Closing, Figurative, InspectMode, OpenMode, Pos, RelOp, SignClause, SignPosition};
11use crate::{codec_enum, codec_struct};
12use numeric::precision::{Fixed, Places};
13use numeric::options::{Compile, FastsrtAdvPrint, Invdata, Stop, Warnings};
14use numeric::{
15    Arith, BinCheck, CicsReturnWarning, Currency, DispSign, Initcheck, IntDate, Nsymbol, Numcheck, Numproc, Options, Parmcheck, Qualify, Quote, SortKeys, Trunc,
16    TruncCheck, Vlr, VsamOpenFs, ZonCheck,
17};
18use zarch::check::ProgramCheck;
19use zarch::ebcdic::CodePage;
20use zarch::hfp::Precision;
21use zarch::wide::U256;
22
23codec_enum!(Kind {
24    Group = 0,
25    Alnum { justified } = 1,
26    National = 2,
27    Zoned { digits, scale, signed, sign } = 3,
28    Packed { digits, scale, signed } = 4,
29    Binary { digits, scale, signed, native } = 5,
30    Float(precision) = 6,
31    NumericEdited { edit, digits, scale, blank_when_zero } = 7,
32    AlnumEdited { edit } = 8,
33    Pointer = 9,
34    Index = 10,
35    ObjectReference = 11,
36    ProgramPointer = 12,
37});
38codec_struct!(SignClause { position, separate });
39codec_struct!(Pos { file, line, col });
40codec_enum!(SignPosition { Leading = 0, Trailing = 1 });
41codec_enum!(Figurative { Zero = 0, Space = 1, HighValue = 2, LowValue = 3, Quote = 4, Null = 5 });
42codec_enum!(BinOp { Add = 0, Sub = 1, Mul = 2, Div = 3, Pow = 4 });
43codec_enum!(RelOp { Eq = 0, Ne = 1, Lt = 2, Le = 3, Gt = 4, Ge = 5 });
44codec_enum!(AcceptFrom {
45    Sysin = 0,
46    Date { four_digit_year } = 1,
47    Day { four_digit_year } = 2,
48    DayOfWeek = 3,
49    Time = 4,
50});
51codec_enum!(InspectMode { Characters = 0, All = 1, Leading = 2, First = 3 });
52codec_enum!(OpenMode { Input = 0, Output = 1, Extend = 2, InputOutput = 3 });
53codec_enum!(Closing { Volume = 0, NoRewind = 1, Lock = 2 });
54codec_enum!(Format { Fixed = 0, Variable = 1, Text = 2 });
55codec_enum!(HostType {
56    SmallInt { signed } = 0,
57    Integer { signed } = 1,
58    BigInt { signed } = 2,
59    Decimal { digits, scale, signed } = 3,
60    Zoned { digits, scale, signed, sign } = 4,
61    Real = 5,
62    Double = 6,
63    Char(len) = 7,
64    VarChar(max) = 8,
65    Structure(members) = 9,
66});
67codec_enum!(Sym {
68    Nine = 0,
69    Z = 1,
70    Star = 2,
71    FloatLead(c) = 3,
72    Float(c) = 4,
73    Sign(c) = 5,
74    Currency = 6,
75    Cr = 7,
76    Db = 8,
77    Point = 9,
78    Implied = 10,
79    Insert(c) = 11,
80    Char = 12,
81});
82codec_enum!(Precision { Short = 0, Long = 1, Extended = 2 });
83codec_enum!(ProgramCheck {
84    Specification = 0,
85    Data = 1,
86    FixedPointOverflow = 2,
87    FixedPointDivide = 3,
88    DecimalOverflow = 4,
89    DecimalDivide = 5,
90    HfpExponentOverflow = 6,
91    HfpExponentUnderflow = 7,
92    HfpSignificance = 8,
93    HfpDivide = 9,
94});
95
96codec_enum!(Ending { Goback = 0, StopRun = 1, EndOfProgram = 2 });
97codec_enum!(AbendCode {
98    Check(check) = 0,
99    Protection = 1,
100    ModuleNotFound = 2,
101    Io(status) = 3,
102    Cics(code) = 4,
103    User(code) = 5,
104    Ironwork = 6,
105    Exec = 7,
106    Sql = 8,
107    SqlReplay = 9,
108    Java = 10,
109    Signal(signal) = 11,
110});
111codec_enum!(Signal { StopRun = 0, GoBack = 1, SortStopped = 2, ClosedOutput = 3, DeclarativeExit = 4 });
112codec_enum!(FileStatus {
113    Success = 0,
114    SuccessDuplicate = 1,
115    SuccessWrongLength = 2,
116    SuccessOptional = 3,
117    AtEnd = 4,
118    RelativeKeyOverflow = 5,
119    SequenceError = 6,
120    DuplicateKey = 7,
121    NotFound = 8,
122    BoundaryViolation = 9,
123    PermanentError = 10,
124    FileNotFound = 11,
125    OpenModeUnsupported = 12,
126    AlreadyOpen = 13,
127    NotOpen = 14,
128    NoPriorRead = 15,
129    RecordLengthChanged = 16,
130    NoNextRecord = 17,
131    NotOpenInput = 18,
132    NotOpenOutput = 19,
133    NotOpenInputOutput = 20,
134    SuccessNonReel = 21,
135    ClosedWithLock = 22,
136});
137
138codec_struct!(Options {
139    arith, trunc, numproc, codepage, trunc_check, fastsrt, fastsrt_adv_print, sort_keys, adv, thread, dll, rent, dbcs,
140    warnings, compile, dynam, debug, cics_return_warning, invdata, zwb, quote, currency, nsymbol, dispsign, intdate, qualify, initial,
141    vlr, vsamopenfs, numcheck, parmcheck, initcheck,
142} check options_valid);
143codec_struct!(Invdata { forcenumcmp, cleansign });
144codec_enum!(Arith { Compat = 0, Extend = 1 });
145codec_enum!(Trunc { Std = 0, Opt = 1, Bin = 2 });
146codec_enum!(Numproc { Nopfd = 0, Pfd = 1 });
147codec_enum!(TruncCheck { Report = 0, Silent = 1 });
148codec_enum!(SortKeys { Dfsort = 0, Strict = 1 });
149codec_enum!(FastsrtAdvPrint { Exclude = 0, Include = 1 });
150codec_enum!(Warnings { Proceed = 0, Block = 1 });
151codec_enum!(Compile { Full = 0, Until(stop) = 1, SyntaxOnly = 2 });
152codec_enum!(Stop { W = 0, E = 1, S = 2 });
153codec_enum!(CicsReturnWarning { Once = 0, Always = 1, Never = 2 });
154codec_enum!(Quote { Quote = 0, Apost = 1 });
155codec_enum!(Currency { Char(c) = 0, Hex(b) = 1 });
156codec_enum!(Nsymbol { National = 0, Dbcs = 1 });
157codec_enum!(DispSign { Compat = 0, Sep = 1 });
158codec_enum!(IntDate { Ansi = 0, Lilian = 1 });
159codec_enum!(Qualify { Compat = 0, Extend = 1 });
160codec_enum!(Vlr { Standard = 0, Compat = 1 });
161codec_enum!(VsamOpenFs { Compat = 0, Succ = 1 });
162codec_struct!(Numcheck { zon, pac, bin, abd });
163codec_struct!(ZonCheck { alphnum, lax });
164codec_struct!(BinCheck { truncbin });
165codec_struct!(Parmcheck { abd, bytes });
166codec_enum!(Initcheck { Lax = 0, Strict = 1 });
167
168/// `Options::code_page` panics on a CCSID the tables do not carry.
169fn options_valid(options: &Options) -> Result<(), String> {
170    match CodePage::by_ccsid(options.codepage) {
171        Some(_) => Ok(()),
172        None => Err(format!("CODEPAGE({}) is not a page the tables carry", options.codepage)),
173    }
174}
175
176codec_struct!(Fixed { negative, magnitude, places });
177codec_struct!(Places { int, dec });
178
179/// Four 64-bit limbs, low first.
180impl Encode for U256 {
181    fn encode(&self, w: &mut Writer) {
182        [self.lo as u64, (self.lo >> 64) as u64, self.hi as u64, (self.hi >> 64) as u64].encode(w);
183    }
184}
185
186impl Decode for U256 {
187    fn decode(r: &mut Reader<'_>) -> Result<Self, ModuleError> {
188        let [a, b, c, d] = <[u64; 4]>::decode(r)?;
189        let limbs = |low: u64, high: u64| u128::from(low) | u128::from(high) << 64;
190        Ok(U256 { hi: limbs(c, d), lo: limbs(a, b) })
191    }
192}