Skip to main content

ironwork_rt/lir/
sort.rs

1//! SORT, MERGE, RELEASE and RETURN (lir.md ยง9.6), which `rt::sort` runs. `FileSort` is generic
2//! over the handles the executor resolves as the statement runs: the LIR's by default, the walker's
3//! own references in the interpreter.
4
5use super::{Count, FromMove, MovePlan, PlaceId, RangeId, SymId};
6use crate::storage::Kind;
7use crate::{codec_enum, codec_struct};
8
9#[derive(Clone, Debug, PartialEq, Eq)]
10pub enum SortPlan {
11    File(FileSort),
12    Table(TableSort),
13}
14
15/// A SORT or MERGE of SD `sd`. `sort_return` and `sort_control` are SORT-RETURN and SORT-CONTROL;
16/// an input or output that is None fails the statement when the sort reaches it.
17#[derive(Clone, Debug, PartialEq, Eq)]
18pub struct FileSort<R = PlaceId, Q = RangeId, K = SortKeys, F = u16> {
19    pub sd: u16,
20    pub merge: bool,
21    pub keys: K,
22    pub input: Option<SortIo<Q, F>>,
23    pub output: Option<SortIo<Q, F>>,
24    pub sort_return: R,
25    pub sort_control: R,
26}
27
28/// USING or GIVING files, or an INPUT or OUTPUT PROCEDURE.
29#[derive(Clone, Debug, PartialEq, Eq)]
30pub enum SortIo<Q = RangeId, F = u16> {
31    Files(Vec<F>),
32    Procedure(Q),
33}
34
35/// The keys, most significant first, and the collating sequence of those marked `collated` when it
36/// is not EBCDIC: each byte's position.
37#[derive(Clone, Debug, PartialEq, Eq)]
38pub struct SortKeys {
39    pub keys: Vec<SortKey>,
40    pub collating: Option<Box<[u8; 256]>>,
41}
42
43/// A key at `offset` in the record or table element, read as item `item` of `kind`.
44#[derive(Clone, Copy, Debug, PartialEq, Eq)]
45pub struct SortKey {
46    pub ascending: bool,
47    pub offset: u32,
48    pub len: u32,
49    pub kind: Kind,
50    pub item: u32,
51    pub collated: bool,
52}
53
54/// A table SORT: `first` is the table's first element and `count` how many it holds now.
55#[derive(Clone, Debug, PartialEq, Eq)]
56pub struct TableSort {
57    pub first: PlaceId,
58    pub count: Count,
59    pub stride: u32,
60    pub keys: SortKeys,
61    pub name: SymId,
62}
63
64/// RELEASE of `record`, a record of file `file`, located after FROM has moved into `FromMove.to`,
65/// the record as a receiving item. `name` is the record as written.
66#[derive(Clone, Copy, Debug, PartialEq, Eq)]
67pub struct ReleasePlan {
68    pub record: PlaceId,
69    pub file: Option<u16>,
70    pub from: Option<FromMove>,
71    pub sort_return: PlaceId,
72    pub name: SymId,
73}
74
75/// RETURN of file `file`, named `name` as written, and INTO's MOVE of the record's bytes. The op
76/// returns Arm(0) at end and Arm(1) when a record came.
77#[derive(Clone, Copy, Debug, PartialEq, Eq)]
78pub struct ReturnPlan {
79    pub file: Option<u16>,
80    pub into: Option<(PlaceId, MovePlan)>,
81    pub sort_return: PlaceId,
82    pub name: SymId,
83}
84
85codec_enum!(SortPlan { File(sort) = 0, Table(sort) = 1 });
86codec_struct!(FileSort { sd, merge, keys, input, output, sort_return, sort_control });
87codec_enum!(SortIo { Files(files) = 0, Procedure(range) = 1 });
88codec_struct!(SortKeys { keys, collating });
89codec_struct!(SortKey { ascending, offset, len, kind, item, collated });
90codec_struct!(TableSort { first, count, stride, keys, name });
91codec_struct!(ReleasePlan { record, file, from, sort_return, name });
92codec_struct!(ReturnPlan { file, into, sort_return, name });