ironwork-rt 0.9.0

ironwork for COBOL: the runtime a compiled program links, under the runtime exception
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
//! Operands, expressions and FUNCTION, evaluated as the walker's `operand`, `expr_value`,
//! `eval_fixed`, `eval_float` and `function` evaluate them (lir.md §6, §9.9).

use super::{Code, Facts, R, Vm, not_yet};
use crate::abend::Abend;
use crate::arith;
use crate::fixed::places_of;
use crate::intrinsic::function::{self as intrinsic, Evaluator};
use crate::lir::{AbendId, Argument, Base, Comparand, Const, Count, Expr, ExprId, Func, FunctionId, FunctionPlan, IntExpr, Mode, Operand, PlaceId, RefMod, SenderCheck};
use crate::storage::{Kind, Loc, Val};
use crate::store::{self, ProgramFacts};
use crate::unit::{ADDRESS_BASE, Loader};
use crate::vocab::{BinOp, Figurative, Pos};
use numeric::Arith;
use numeric::precision::{Fixed, Places, carried, product_places, quotient_places, sum_places};
use std::cmp::Ordering;
use std::rc::Rc;
use zarch::hfp::{Hfp, Precision};

/// A constant's value, or the abend reading a refused one gives.
pub(super) fn constant(c: &Const) -> Result<Val, AbendId> {
    Ok(match c {
        Const::Bytes(b) => Val::Bytes(b.clone()),
        Const::National(b) => Val::National(b.clone()),
        Const::Number(f) => Val::Num(*f),
        Const::Figurative(f) => Val::Fig(*f),
        Const::All(b) => Val::All(b.clone()),
        Const::AllNational(b) => Val::AllNational(b.clone()),
        Const::Dbcs(b) => Val::Dbcs(b.clone()),
        Const::Refused(abend) => return Err(*abend),
    })
}

impl<'p, L: Loader<Rc<Code>>> Vm<'p, '_, '_, L> {
    pub(super) fn read(&self, loc: Loc, pos: Pos) -> R<Val> {
        Ok(store::read(&self.facts(), &self.unit.mem, loc, pos)?)
    }

    /// NUMCHECK's test of an item once located and before it is read, under the option.
    pub(super) fn numcheck(&mut self, loc: Loc, check: SenderCheck, pos: Pos) -> R<()> {
        if self.p.options.options.numcheck.is_none() {
            return Ok(());
        }
        let facts = self.facts();
        Ok(store::numcheck_sender(&facts, self.unit, loc, check, self.sym(self.p.id), pos)?)
    }

    /// Place `p`, located at `loc`, tested and read as `Machine::operand` reads a data item.
    pub(super) fn read_tested(&mut self, p: PlaceId, loc: Loc) -> R<Val> {
        let pos = self.pos(self.p.places[p as usize].at);
        self.numcheck(loc, SenderCheck::Item, pos)?;
        self.read(loc, pos)
    }

    /// `Machine::operand`: a data item located and read, a literal, LENGTH OF, ADDRESS OF or FUNCTION.
    pub(super) fn value(&mut self, o: Operand) -> R<Val> {
        match o {
            Operand::Load(p) => {
                let loc = self.loc(p)?;
                self.read_tested(p, loc)
            }
            Operand::Const(c) => constant(&self.p.consts[c as usize]).map_err(|a| self.abend(a, None).into()),
            Operand::LengthOf(p) => {
                // A LINKAGE item's length no OCCURS DEPENDING ON or reference modification varies is
                // the compile's, read without its address (Language Reference, LENGTH OF).
                let q = &self.p.places[p as usize];
                let fixed = matches!(q.base, Base::Linkage(_)) && q.odo.is_empty() && q.refmod.is_none();
                let len = if fixed { q.len as usize } else { self.loc(p)?.len };
                Ok(Val::Num(Fixed::new(len as i128, Places::new(9, 0))))
            }
            Operand::AddressOf(p) => {
                if let Base::Linkage(record) = self.p.places[p as usize].base
                    && self.linkage[record as usize].is_none()
                {
                    return Ok(Val::Address(0));
                }
                let loc = self.loc(p)?;
                Ok(Val::Address(ADDRESS_BASE + loc.offset as u32))
            }
            Operand::Function(f) => self.function(f),
            Operand::UserFunction(f) => self.user_function(f),
        }
    }

    /// `Machine::operand_with_loc`: a data item's value and its `Loc`, anything else's value.
    pub(super) fn value_with_loc(&mut self, o: Operand) -> R<(Val, Option<Loc>)> {
        if let Operand::Load(p) = o {
            let loc = self.loc(p)?;
            return Ok((self.read_tested(p, loc)?, Some(loc)));
        }
        Ok((self.value(o)?, None))
    }

    /// `Machine::expr_value`: an operand as it reads, an expression after its locate passes.
    pub(super) fn comparand(&mut self, c: &Comparand, pos: Pos) -> R<Val> {
        match c {
            Comparand::Operand(o) => self.value(*o),
            Comparand::Expr { expr, dmax, mode, prepass } => {
                for &q in prepass {
                    self.loc(q)?;
                }
                Ok(match mode {
                    Mode::Float(p) => Val::Float(self.eval_float(*expr, *p, pos)?),
                    Mode::Fixed => Val::Num(self.eval_fixed(*expr, *dmax, pos)?),
                })
            }
        }
    }

    /// `Machine::comparand`: an operand keeps its location for a comparison; an expression does not.
    pub(super) fn comparand_with_loc(&mut self, c: &Comparand, pos: Pos) -> R<(Val, Option<Loc>)> {
        match c {
            Comparand::Operand(o) => self.value_with_loc(*o),
            Comparand::Expr { .. } => Ok((self.comparand(c, pos)?, None)),
        }
    }

    pub(super) fn eval_fixed(&mut self, e: ExprId, dmax: u32, pos: Pos) -> R<Fixed> {
        self.eval_fixed_at(e, dmax, dmax, pos)
    }

    /// `Machine::eval_fixed_at`: `e`'s top operation at `last` places and every operation below
    /// it at `inner`.
    pub(super) fn eval_fixed_at(&mut self, e: ExprId, last: u32, inner: u32, pos: Pos) -> R<Fixed> {
        Ok(self.eval_number_at(e, last, inner, pos)?.fixed())
    }

    /// `eval_fixed`, its value held as an integer while it is one.
    pub(super) fn eval_number(&mut self, e: ExprId, dmax: u32, pos: Pos) -> R<Number> {
        self.eval_number_at(e, dmax, dmax, pos)
    }

    /// `eval_fixed_at`, its value held as an integer while it is one.
    pub(super) fn eval_number_at(&mut self, e: ExprId, last: u32, inner: u32, pos: Pos) -> R<Number> {
        let arith = self.p.options.options.arith;
        match &self.p.exprs[e as usize] {
            Expr::Operand(o) => self.operand_number(*o, last, pos),
            Expr::Neg(operand) => Ok(match self.eval_number_at(*operand, inner, inner, pos)? {
                Number::Int(n, places) if n != i64::MIN => Number::Int(-n, places),
                v => Number::Fixed(arith::fixed_neg(v.fixed())),
            }),
            Expr::Bin(a, op, b) => {
                let x = self.eval_number_at(*a, inner, inner, pos)?;
                let y = self.eval_number_at(*b, inner, inner, pos)?;
                if let Some(r) = int_binop(x, *op, y, last, arith) {
                    return Ok(r);
                }
                let (x, y) = (x.fixed(), y.fixed());
                if arith::divides_by_zero(*op, &y) {
                    let binary = self.binary_division(*a, *b)?;
                    return Err(arith::zero_divide(binary, pos).into());
                }
                Ok(Number::Fixed(arith::fixed_binop(x, *op, y, last, arith, pos)?))
            }
            Expr::Pow(base, exponent) => {
                let x = self.eval_number_at(*base, inner, inner, pos)?.fixed();
                let n = self.int(exponent, pos)?;
                Ok(Number::Fixed(arith::pow(x, n, last, arith, pos)?))
            }
        }
    }

    /// An operand as `value` reads it, then `fixed_operand` takes it.
    pub(super) fn operand_number(&mut self, o: Operand, dmax: u32, pos: Pos) -> R<Number> {
        match o {
            Operand::Load(p) => {
                if let Some(n) = self.static_number(p) {
                    return Ok(n);
                }
                let place = &self.p.places[p as usize];
                let loc = self.loc(p)?;
                let at = self.pos(place.at);
                self.numcheck(loc, SenderCheck::Item, at)?;
                if super::place::plain(place)
                    && let Some(n) = store::read_digits(&self.facts(), &self.unit.mem, loc)
                {
                    return Ok(Number::Int(n, places_of(loc.kind)));
                }
                let val = self.read(loc, at)?;
                Ok(Number::Fixed(arith::fixed_operand(val, dmax, pos)?))
            }
            Operand::Function(f) => match self.function_count(f)? {
                Some(n) => Ok(n),
                None => {
                    let val = self.value(o)?;
                    Ok(Number::Fixed(arith::fixed_operand(val, dmax, pos)?))
                }
            },
            Operand::Const(c) => match self.code.literals[c as usize] {
                Some((n, places)) => Ok(Number::Int(n, places)),
                None => {
                    let val = self.value(o)?;
                    Ok(Number::Fixed(arith::fixed_operand(val, dmax, pos)?))
                }
            },
            _ => {
                let val = self.value(o)?;
                Ok(Number::Fixed(arith::fixed_operand(val, dmax, pos)?))
            }
        }
    }

    /// FUNCTION MOD of two fixed-point arguments, each evaluated as `comparand` evaluates it and
    /// held as a count, the intrinsic taking the two values only where `count_mod` cannot; None,
    /// with nothing evaluated, for any other function or argument.
    fn function_count(&mut self, id: FunctionId) -> R<Option<Number>> {
        let plan = &self.p.plans.function[id as usize];
        let (Func::Mod, None, [Argument::Value(a), Argument::Value(b)]) = (plan.func, &plan.refmod, plan.args.as_slice()) else { return Ok(None) };
        if !(self.countable(a) && self.countable(b)) {
            return Ok(None);
        }
        let pos = self.pos(plan.at);
        let x = self.comparand_number(a, pos)?;
        let y = self.comparand_number(b, pos)?;
        if let Some(n) = count_mod(x, y) {
            return Ok(Some(n));
        }
        let mut args = vec![Val::Num(x.fixed()), Val::Num(y.fixed())];
        let result = intrinsic::evaluate(&mut Call { vm: self, plan }, plan.func.name(), plan.side, &mut args, pos);
        match self.settle(result)? {
            Val::Num(f) => Ok(Some(Number::of(f))),
            _ => Err(not_yet("FUNCTION MOD of numbers that is not a number")),
        }
    }

    /// A function argument `comparand` reads as a fixed-point number: an arithmetic expression, a
    /// numeric literal or a binary, packed or zoned item.
    fn countable(&self, c: &Comparand) -> bool {
        match c {
            Comparand::Expr { mode: Mode::Fixed, .. } => true,
            Comparand::Expr { .. } => false,
            Comparand::Operand(Operand::Const(k)) => self.code.literals[*k as usize].is_some(),
            Comparand::Operand(Operand::Load(p)) => matches!(self.p.places[*p as usize].kind, Kind::Binary { .. } | Kind::Packed { .. } | Kind::Zoned { .. }),
            Comparand::Operand(_) => false,
        }
    }

    /// `comparand` of an argument `countable` takes, held as a `Number`.
    fn comparand_number(&mut self, c: &Comparand, pos: Pos) -> R<Number> {
        match c {
            Comparand::Expr { expr, dmax, prepass, .. } => {
                for &q in prepass {
                    self.loc(q)?;
                }
                self.eval_number(*expr, *dmax, pos)
            }
            Comparand::Operand(o) => self.operand_number(*o, 0, pos),
        }
    }

    /// `Machine::eval_float`: an exponent is evaluated as a float, without its dmax pass.
    pub(super) fn eval_float(&mut self, e: ExprId, p: Precision, pos: Pos) -> R<Hfp> {
        match &self.p.exprs[e as usize] {
            Expr::Operand(o) => {
                let val = self.value(*o)?;
                Ok(arith::float_operand(val, p, pos)?)
            }
            Expr::Neg(inner) => Ok(arith::float_neg(self.eval_float(*inner, p, pos)?)),
            Expr::Bin(a, op, b) => {
                let x = self.eval_float(*a, p, pos)?;
                let y = self.eval_float(*b, p, pos)?;
                Ok(arith::float_binop(x, *op, y, p, pos)?)
            }
            Expr::Pow(base, exponent) => {
                let x = self.eval_float(*base, p, pos)?;
                let y = match exponent {
                    IntExpr::Const(n) => Hfp::from_integer(i128::from(*n), p),
                    IntExpr::Item(q) => {
                        let val = self.value(Operand::Load(*q))?;
                        arith::float_operand(val, p, pos)?
                    }
                    IntExpr::Fixed { expr, .. } => self.eval_float(*expr, p, pos)?,
                    IntExpr::Walk(_) => return Err(not_yet("a JSON walk subscript as an exponent")),
                };
                Ok(arith::float_binop(x, BinOp::Pow, y, p, pos)?)
            }
        }
    }

    /// `Machine::binary_division`: whether a zero divisor is the fixed-point divide's (S0C9),
    /// every operand of both sides an integer binary item or integer literal and one an item.
    pub(super) fn binary_division(&mut self, a: ExprId, b: ExprId) -> R<bool> {
        let mut walk = Division::default();
        let all = self.binary_operands(a, &mut walk)? && self.binary_operands(b, &mut walk)?;
        let undecided = || not_yet("a zero divisor beside ZERO or an integer exponent, which may have been written ALL ZERO or with a decimal point");
        match all {
            false if walk.undecided && self.p.options.options.numcheck.is_some() => Err(undecided()),
            false => Ok(false),
            true if walk.items == 0 => Ok(false),
            true if walk.undecided => Err(undecided()),
            true => Ok(true),
        }
    }

    /// Whether each operand, left to right, is one the fixed-point divide takes, each item located
    /// again, until one is not. An operand the LIR does not keep enough of to decide is taken to be
    /// one and noted: ZERO, which may have been written ALL ZERO, and an integer exponent, which
    /// may have been written with a decimal point.
    fn binary_operands(&mut self, e: ExprId, walk: &mut Division) -> R<bool> {
        Ok(match &self.p.exprs[e as usize] {
            Expr::Operand(Operand::Const(c)) => match &self.p.consts[*c as usize] {
                Const::Number(f) => f.places.dec == 0,
                Const::Figurative(Figurative::Zero) => {
                    walk.undecided = true;
                    true
                }
                _ => false,
            },
            Expr::Operand(Operand::LengthOf(_)) => {
                walk.items += 1;
                true
            }
            Expr::Operand(Operand::Load(place)) => self.binary_item(*place, walk)?,
            Expr::Operand(_) => false,
            Expr::Neg(inner) => self.binary_operands(*inner, walk)?,
            Expr::Bin(x, _, y) => self.binary_operands(*x, walk)? && self.binary_operands(*y, walk)?,
            Expr::Pow(x, exponent) => {
                self.binary_operands(*x, walk)?
                    && match exponent {
                        IntExpr::Const(_) => {
                            walk.undecided = true;
                            true
                        }
                        IntExpr::Item(place) => self.binary_item(*place, walk)?,
                        IntExpr::Fixed { expr, .. } => self.binary_operands(*expr, walk)?,
                        IntExpr::Walk(_) => return Err(not_yet("a JSON walk subscript as an exponent")),
                    }
            }
        })
    }

    fn binary_item(&mut self, place: PlaceId, walk: &mut Division) -> R<bool> {
        let binary = matches!(self.loc(place)?.kind, Kind::Binary { scale: 0, .. } | Kind::Index);
        walk.items += usize::from(binary);
        Ok(binary)
    }

    /// `Machine::function`: HEX-OF, BIT-OF and BYTE-LENGTH read an argument's storage; any other
    /// evaluates its arguments, then the function; then reference modification of the result.
    pub(super) fn function(&mut self, id: FunctionId) -> R<Val> {
        let plan = &self.p.plans.function[id as usize];
        let pos = self.pos(plan.at);
        let facts = self.facts();
        let value = if matches!(plan.func, Func::HexOf | Func::BitOf | Func::ByteLength) {
            if let Some(abend) = plan.arity {
                return Err(self.abend(abend, Some(plan.at)).into());
            }
            let [Argument::Value(arg)] = plan.args.as_slice() else { return Err(not_yet("a storage FUNCTION without its one argument")) };
            let bytes = match arg {
                Comparand::Operand(Operand::Load(p)) => {
                    let loc = self.loc(*p)?;
                    store::bytes(&self.unit.mem, loc).to_vec()
                }
                other => {
                    let val = self.comparand(other, pos)?;
                    intrinsic::stored_bytes(&facts, val, pos)?
                }
            };
            intrinsic::storage(&facts, plan.func.name(), &bytes, pos)?
        } else {
            let mut args = std::mem::take(&mut self.spare.args);
            args.clear();
            for a in &plan.args {
                match a {
                    Argument::Value(c) => args.push(self.comparand(c, pos)?),
                    Argument::All { element, all } => self.all_elements(*element, all, &mut args)?,
                }
            }
            match plan.func {
                Func::Uuid4 => return Err(not_yet("FUNCTION UUID4, which gives another value on every run")),
                Func::Random if self.locating > 0 => return Err(not_yet("FUNCTION RANDOM in a subscript, reference modification or OCCURS DEPENDING ON")),
                _ => {}
            }
            let counted = match (plan.func, args.as_slice()) {
                (Func::Mod, [Val::Num(a), Val::Num(b)]) => count_mod(Number::of(*a), Number::of(*b)),
                _ => None,
            };
            let result = match counted {
                Some(n) => Ok(Val::Num(n.fixed())),
                None => intrinsic::evaluate(&mut Call { vm: self, plan }, plan.func.name(), plan.side, &mut args, pos),
            };
            args.clear();
            self.spare.args = args;
            self.settle(result)?
        };
        self.refmodded(value, plan.refmod.as_ref(), pos)
    }

    /// A function's value reference-modified by `refmod`, its start and length evaluated now.
    pub(super) fn refmodded(&mut self, value: Val, refmod: Option<&RefMod>, pos: Pos) -> R<Val> {
        let Some(rm) = refmod else { return Ok(value) };
        let result = intrinsic::refmod(value, pos, || {
            let start = self.int(&rm.start, pos);
            let start = self.lift(start, pos)?;
            let length = match &rm.length {
                Some(l) => {
                    let length = self.int(l, pos);
                    Some(self.lift(length, pos)?)
                }
                None => None,
            };
            Ok((start, length))
        });
        self.settle(result)
    }

    /// `Machine::all_elements`: the counts of the ALL dimensions, left to right, then each
    /// element, the rightmost ALL subscript varying fastest, located and read.
    fn all_elements(&mut self, element: PlaceId, all: &[(u32, Count)], out: &mut Vec<Val>) -> R<()> {
        let pos = self.pos(self.p.places[element as usize].at);
        let mut counts = Vec::with_capacity(all.len());
        for (_, count) in all {
            counts.push(i64::from(self.count(count, pos)?));
        }
        if counts.contains(&0) {
            return Ok(());
        }
        let mut current: Vec<(u32, i64)> = all.iter().map(|&(at, _)| (at, 1)).collect();
        loop {
            let loc = self.loc_with(element, &current)?;
            out.push(self.read_tested(element, loc)?);
            let mut k = current.len();
            loop {
                if k == 0 {
                    return Ok(());
                }
                k -= 1;
                if current[k].1 < counts[k] {
                    current[k].1 += 1;
                    break;
                }
                current[k].1 = 1;
            }
        }
    }

    /// `Machine::program_name`: an alphanumeric value, decoded, trimmed and upper-cased.
    pub(super) fn program_name(&mut self, o: Operand, pos: Pos) -> R<String> {
        match self.value(o)? {
            Val::Bytes(b) => Ok(self.facts().page().decode(&b).trim().to_ascii_uppercase()),
            _ => Err(Abend::ironwork("a program name must be alphanumeric", pos).into()),
        }
    }
}

/// A constant as `operand_number` takes it while it fits an `i64` count of its last decimal place,
/// other than a negative zero.
pub(super) fn const_number(c: &Const) -> Option<(i64, Places)> {
    match c {
        Const::Number(f) => match Number::of(*f) {
            Number::Int(n, places) => Some((n, places)),
            Number::Fixed(_) => None,
        },
        _ => None,
    }
}

/// A fixed-point value, held as a count of its last decimal place with its places while that fits
/// an `i64`.
#[derive(Clone, Copy, Debug, PartialEq)]
pub(super) enum Number {
    Int(i64, Places),
    Fixed(Fixed),
}

impl Number {
    /// `f` as a count of its last decimal place where that fits an `i64`.
    pub(super) fn of(f: Fixed) -> Self {
        match f.to_i128().and_then(|n| i64::try_from(n).ok()) {
            Some(n) if !(f.negative && f.magnitude.is_zero()) => Self::Int(n, f.places),
            _ => Self::Fixed(f),
        }
    }

    pub(super) fn fixed(self) -> Fixed {
        match self {
            Self::Int(n, places) => Fixed::new(i128::from(n), places),
            Self::Fixed(f) => f,
        }
    }
}

/// ADD, SUBTRACT, MULTIPLY or DIVIDE of two counts as `Fixed` gives them: the exact result, or the
/// truncated quotient, kept to the places carried; None where a step leaves `i128`, the result does
/// not fit an `i64`, or the divisor is zero, which `Fixed` reports.
pub(super) fn int_binop(x: Number, op: BinOp, y: Number, dmax: u32, arith: Arith) -> Option<Number> {
    let (Number::Int(x, px), Number::Int(y, py)) = (x, y) else { return None };
    let (x, y) = (i128::from(x), i128::from(y));
    let (exact, ir) = match op {
        BinOp::Add | BinOp::Sub => {
            let (x, y) = aligned(x, px.dec, y, py.dec)?;
            let exact = if op == BinOp::Add { x.checked_add(y)? } else { x.checked_sub(y)? };
            (exact, sum_places(px, py))
        }
        BinOp::Mul => (x * y, product_places(px, py)),
        BinOp::Div if y != 0 => {
            let to = carried(quotient_places(px, py, dmax), dmax, arith);
            let (numerator, denominator) = aligned(x, px.dec, y, py.dec + to.dec)?;
            return kept(numerator / denominator, to, to);
        }
        BinOp::Div | BinOp::Pow => return None,
    };
    kept(exact, ir, carried(ir, dmax, arith))
}

/// FUNCTION MOD of two counts as the intrinsic gives it: a - b * FLOOR(a / b) at the arguments'
/// greater decimal places, with the integer places of the shorter argument, high-order digits
/// dropped; None where either is not a count, or `b` is zero, which the intrinsic reports.
pub(super) fn count_mod(a: Number, b: Number) -> Option<Number> {
    let (Number::Int(a, pa), Number::Int(b, pb)) = (a, b) else { return None };
    let (a, b) = aligned(i128::from(a), pa.dec, i128::from(b), pb.dec)?;
    if b == 0 {
        return None;
    }
    let r = a % b;
    let r = if r != 0 && (r < 0) != (b < 0) { r + b } else { r };
    let places = Places::new(pa.int.min(pb.int), pa.dec.max(pb.dec));
    kept(r, places, places)
}

/// Ten to each power an `i128` holds.
const POW10: [i128; 39] = {
    let mut table = [1; 39];
    let mut k = 1;
    while k < table.len() {
        table[k] = table[k - 1] * 10;
        k += 1;
    }
    table
};

#[inline]
fn pow10(n: u32) -> Option<i128> {
    POW10.get(n as usize).copied()
}

/// Counts `x` of `dx` and `y` of `dy` decimal places, held at the greater; None past `i128`.
#[inline]
pub(super) fn aligned(x: i128, dx: u32, y: i128, dy: u32) -> Option<(i128, i128)> {
    match dx.cmp(&dy) {
        Ordering::Equal => Some((x, y)),
        Ordering::Less => Some((x.checked_mul(pow10(dy - dx)?)?, y)),
        Ordering::Greater => Some((x, y.checked_mul(pow10(dx - dy)?)?)),
    }
}

/// `Fixed::fit`: `exact`, held at `from`'s decimal places, kept to `to.dec` decimal places, the rest
/// truncated, and `to.int` integer places, the high-order digits dropped.
fn kept(exact: i128, from: Places, to: Places) -> Option<Number> {
    let mut magnitude = exact.unsigned_abs();
    if to.dec < from.dec {
        magnitude = pow10(from.dec - to.dec).map_or(0, |d| quotient(magnitude, d.unsigned_abs()));
    }
    if let Some(cap) = pow10(to.int + from.dec.min(to.dec)).map(i128::unsigned_abs)
        && magnitude >= cap
    {
        magnitude -= quotient(magnitude, cap) * cap;
    }
    let kept = i64::try_from(magnitude).ok()?.checked_mul(i64::try_from(pow10(to.dec.saturating_sub(from.dec))?).ok()?)?;
    Some(Number::Int(if exact < 0 { -kept } else { kept }, to))
}

/// `m / d`, by the 64-bit divide where both fit it.
fn quotient(m: u128, d: u128) -> u128 {
    match (u64::try_from(m), u64::try_from(d)) {
        (Ok(m), Ok(d)) => u128::from(m / d),
        _ => m / d,
    }
}

/// What `binary_operands` has found so far: the items, and whether an operand it took to be one
/// the fixed-point divide takes may not have been.
#[derive(Default)]
struct Division {
    items: usize,
    undecided: bool,
}

/// What a FUNCTION reads beyond its arguments' values: its plan's integer argument again, the run
/// unit's clock and RANDOM state, and the program's compile time and NUMVAL-C currency.
struct Call<'a, 'p, 'u, 'w, L: Loader<Rc<Code>>> {
    vm: &'a mut Vm<'p, 'u, 'w, L>,
    plan: &'p FunctionPlan,
}

impl<'p, L: Loader<Rc<Code>>> Evaluator for Call<'_, 'p, '_, '_, L> {
    type Facts = Facts<'p>;

    fn facts(&self) -> Facts<'p> {
        self.vm.facts()
    }

    fn integer(&mut self, _k: usize, pos: Pos) -> Result<i64, Abend> {
        let value = match &self.plan.integer {
            Some(e) => self.vm.int(e, pos),
            None => Err(not_yet("a FUNCTION argument read again without its plan")),
        };
        self.vm.lift(value, pos)
    }

    fn written(&self) -> usize {
        self.plan.args.len()
    }

    fn now(&self) -> (i64, u32) {
        self.vm.unit.now()
    }

    fn compiled(&self) -> (i64, u32) {
        self.vm.p.options.when_compiled.map_or((0, 0), |t| (t.seconds, t.hundredths))
    }

    fn random(&mut self) -> &mut Option<u32> {
        &mut self.vm.unit.random
    }

    fn currency(&self) -> String {
        self.vm.p.options.numval_currency.clone()
    }

    fn caller(&mut self) -> Option<String> {
        let unit = &self.vm.unit;
        unit.caller_of(self.vm.me).map(|p| unit.programs[p].name.clone())
    }

    fn crt_status(&mut self) -> u16 {
        self.vm.unit.crt.as_ref().and_then(|c| c.borrow().last_key).map_or(0, crate::crt::crt_status)
    }

    fn run_argument(&mut self, position: usize) -> Option<String> {
        position.checked_sub(1).and_then(|k| self.vm.unit.arguments.words.get(k)).cloned()
    }

    fn heap_allocate(&mut self, size: usize) -> u32 {
        self.vm.unit.heap_allocate(size)
    }

    fn heap_free(&mut self, address: u32) -> u32 {
        self.vm.unit.heap_free(address)
    }

    fn argument_length(&mut self, position: usize) -> usize {
        self.vm.unit.argument_length_of(self.vm.me, position)
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn integers_add_subtract_and_multiply_as_fixed_point_does() {
        let mut state = 0x2545_F491_4F6C_DD1Du64;
        let mut next = move || {
            state ^= state >> 12;
            state ^= state << 25;
            state ^= state >> 27;
            state.wrapping_mul(0x9E37_79B9_7F4A_7C15)
        };
        let mut fast = 0;
        for _ in 0..20_000 {
            let mut operand = || {
                let digits = 1 + (next() % 31) as u32;
                let bits = next() % 64;
                let n = (next() >> (63 - bits)) as i64;
                (if next() % 2 == 0 { -n } else { n }, Places::new(digits, 0))
            };
            let ((x, px), (y, py)) = (operand(), operand());
            let (dmax, arith) = ((next() % 4) as u32, if next() % 2 == 0 { Arith::Compat } else { Arith::Extend });
            for op in [BinOp::Add, BinOp::Sub, BinOp::Mul] {
                let (a, b) = (Number::Int(x, px), Number::Int(y, py));
                let expected = arith::fixed_binop(a.fixed(), op, b.fixed(), dmax, arith, Pos::default()).unwrap();
                if let Some(r) = int_binop(a, op, b, dmax, arith) {
                    fast += 1;
                    assert_eq!(r.fixed(), expected, "{x} {op:?} {y} at {px:?} {py:?}, dmax {dmax}, {arith:?}");
                }
            }
        }
        assert!(fast > 40_000, "only {fast} operations were integers");
    }

    #[test]
    fn scaled_operands_add_subtract_multiply_and_divide_as_fixed_point_does() {
        let mut state = 0x9E37_79B9_7F4A_7C15u64;
        let mut next = move || {
            state ^= state >> 12;
            state ^= state << 25;
            state ^= state >> 27;
            state.wrapping_mul(0x2545_F491_4F6C_DD1D)
        };
        let mut fast = [0; 4];
        for _ in 0..40_000 {
            let mut operand = || {
                let digits = 1 + (next() % 18) as u32;
                let dec = (next() % u64::from(digits + 1)) as u32;
                let n = (next() % 10u64.pow(digits)) as i64;
                (if next() % 2 == 0 { -n } else { n }, Places::new(digits - dec, dec))
            };
            let ((x, px), (y, py)) = (operand(), operand());
            let (dmax, arith) = ((next() % 10) as u32, if next() % 2 == 0 { Arith::Compat } else { Arith::Extend });
            for (k, op) in [BinOp::Add, BinOp::Sub, BinOp::Mul, BinOp::Div].into_iter().enumerate() {
                let (a, b) = (Number::Int(x, px), Number::Int(y, py));
                let expected = arith::fixed_binop(a.fixed(), op, b.fixed(), dmax, arith, Pos::default());
                match (int_binop(a, op, b, dmax, arith), expected) {
                    (Some(r), Ok(expected)) => {
                        fast[k] += 1;
                        assert_eq!(r.fixed(), expected, "{x} {op:?} {y} at {px:?} {py:?}, dmax {dmax}, {arith:?}");
                    }
                    (Some(r), Err(e)) => panic!("{x} {op:?} {y} gave {r:?} where fixed point gives {e:?}"),
                    (None, _) => {}
                }
            }
        }
        assert!(fast.iter().all(|&n| n > 20_000), "operations taken as counts: {fast:?}");
    }

    #[derive(Clone, Copy)]
    struct Plain;

    impl ProgramFacts for Plain {
        fn options(&self) -> numeric::Options {
            numeric::Options::default()
        }
        fn page(&self) -> &'static zarch::ebcdic::CodePage {
            numeric::Options::default().code_page()
        }
        fn figurative(&self, _: Figurative) -> u8 {
            0
        }
        fn collation(&self) -> &zarch::ebcdic::Collation {
            &zarch::ebcdic::Collation::Native
        }
        fn ordinal(&self, byte: u8) -> u16 {
            u16::from(byte) + 1
        }
        fn character(&self, _: i64) -> Option<u8> {
            None
        }
        fn characters(&self) -> usize {
            256
        }
        fn decimal_point(&self) -> char {
            '.'
        }
        fn edit(&self, _: u32) -> (&[crate::picture::Sym], &str) {
            (&[], "")
        }
        fn scaling(&self, _: usize) -> u32 {
            0
        }
        fn item_name(&self, _: usize) -> String {
            String::new()
        }
    }

    struct Nothing(Option<u32>);

    impl Evaluator for Nothing {
        type Facts = Plain;
        fn facts(&self) -> Plain {
            Plain
        }
        fn integer(&mut self, _: usize, pos: Pos) -> Result<i64, Abend> {
            Err(Abend::ironwork("no integer argument", pos))
        }
        fn written(&self) -> usize {
            2
        }
        fn now(&self) -> (i64, u32) {
            (0, 0)
        }
        fn compiled(&self) -> (i64, u32) {
            (0, 0)
        }
        fn random(&mut self) -> &mut Option<u32> {
            &mut self.0
        }
        fn currency(&self) -> String {
            String::new()
        }
        fn caller(&mut self) -> Option<String> {
            None
        }
        fn argument_length(&mut self, _: usize) -> usize {
            0
        }

        fn run_argument(&mut self, _: usize) -> Option<String> {
            None
        }

        fn crt_status(&mut self) -> u16 {
            0
        }

        fn heap_allocate(&mut self, _: usize) -> u32 {
            0
        }

        fn heap_free(&mut self, address: u32) -> u32 {
            address
        }
    }

    #[test]
    fn mod_of_two_counts_is_the_intrinsic_s() {
        let mut state = 0x2545_F491_4F6C_DD1Du64;
        let mut next = move || {
            state ^= state >> 12;
            state ^= state << 25;
            state ^= state >> 27;
            state.wrapping_mul(0x9E37_79B9_7F4A_7C15)
        };
        let mut fast = 0;
        for _ in 0..40_000 {
            let mut operand = || {
                let digits = 1 + (next() % 18) as u32;
                let dec = (next() % u64::from(digits + 1)) as u32;
                let n = (next() % 10u64.pow(digits)) as i64;
                let n = if next() % 4 == 0 { n % 10 } else { n };
                (if next() % 2 == 0 { -n } else { n }, Places::new(digits - dec, dec))
            };
            let ((x, px), (y, py)) = (operand(), operand());
            let (a, b) = (Number::Int(x, px), Number::Int(y, py));
            let mut args = vec![Val::Num(a.fixed()), Val::Num(b.fixed())];
            let general = intrinsic::evaluate(&mut Nothing(None), "MOD", None, &mut args, Pos::default());
            match (count_mod(a, b), general) {
                (Some(r), Ok(Val::Num(general))) => {
                    fast += 1;
                    assert_eq!(r.fixed(), general, "MOD({x} at {px:?}, {y} at {py:?})");
                }
                (Some(r), other) => panic!("MOD({x} at {px:?}, {y} at {py:?}) gave {r:?} where the intrinsic gives {other:?}"),
                (None, _) => assert!(y == 0, "MOD({x} at {px:?}, {y} at {py:?}) left the counts"),
            }
        }
        assert!(fast > 35_000, "only {fast} MODs taken as counts");
    }
}