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