runmat-vm 0.5.0

RunMat virtual machine and bytecode interpreter
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
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
use crate::bytecode::EndExpr;
use crate::indexing::selectors::{index_scalar_from_value, SliceSelector};
use crate::interpreter::errors::mex;
use runmat_builtins::Value;
use runmat_runtime::{builtins::common::shape::is_scalar_shape, RuntimeError};
use std::future::Future;

pub type VmResult<T> = Result<T, RuntimeError>;

#[derive(Debug, Clone, Default)]
pub struct IndexPlanProperties {
    pub full_row: Option<usize>,
    pub full_column: Option<usize>,
}

#[derive(Debug, Clone)]
pub struct IndexPlan {
    pub indices: Vec<u32>,
    pub output_shape: Vec<usize>,
    pub selection_lengths: Vec<usize>,
    pub dims: usize,
    pub base_shape: Vec<usize>,
    pub properties: IndexPlanProperties,
}

impl IndexPlan {
    pub fn new(
        indices: Vec<u32>,
        output_shape: Vec<usize>,
        selection_lengths: Vec<usize>,
        dims: usize,
        base_shape: Vec<usize>,
    ) -> Self {
        let properties = derive_plan_properties(&indices, dims, &base_shape);
        Self {
            indices,
            output_shape,
            selection_lengths,
            dims,
            base_shape,
            properties,
        }
    }
}

fn derive_plan_properties(
    indices: &[u32],
    dims: usize,
    base_shape: &[usize],
) -> IndexPlanProperties {
    let mut properties = IndexPlanProperties {
        full_row: None,
        full_column: None,
    };
    if dims != 2 || indices.is_empty() {
        return properties;
    }
    let rows = base_shape.first().copied().unwrap_or(1);
    let cols = base_shape.get(1).copied().unwrap_or(1);
    if indices.len() == rows {
        let first = indices[0] as usize;
        if first.is_multiple_of(rows) {
            let col = first / rows;
            if col < cols
                && indices
                    .iter()
                    .enumerate()
                    .all(|(r, &idx)| idx as usize == col * rows + r)
            {
                properties.full_column = Some(col);
            }
        }
    }
    if indices.len() == cols {
        let first = indices[0] as usize;
        let row = first % rows;
        if row < rows
            && indices
                .iter()
                .enumerate()
                .all(|(c, &idx)| idx as usize == row + c * rows)
        {
            properties.full_row = Some(row);
        }
    }
    properties
}

fn cartesian_product<F: FnMut(&[usize])>(lists: &[Vec<usize>], mut f: F) {
    let dims = lists.len();
    if dims == 0 {
        return;
    }
    let mut idx = vec![0usize; dims];
    loop {
        let current: Vec<usize> = (0..dims).map(|d| lists[d][idx[d]]).collect();
        f(&current);
        let mut d = 0usize;
        while d < dims {
            idx[d] += 1;
            if idx[d] < lists[d].len() {
                break;
            }
            idx[d] = 0;
            d += 1;
        }
        if d == dims {
            break;
        }
    }
}

pub fn total_len_from_shape(shape: &[usize]) -> usize {
    if is_scalar_shape(shape) {
        1
    } else {
        shape.iter().copied().product()
    }
}

fn matlab_squeezed_shape(selection_lengths: &[usize], scalar_mask: &[bool]) -> Vec<usize> {
    let mut dims: Vec<(usize, usize, bool)> = selection_lengths
        .iter()
        .enumerate()
        .map(|(d, &len)| (d, len, scalar_mask.get(d).copied().unwrap_or(false)))
        .collect();
    while dims.len() > 2
        && dims
            .last()
            .map(|&(_, len, is_scalar)| len == 1 && is_scalar)
            .unwrap_or(false)
    {
        dims.pop();
    }
    let out: Vec<usize> = dims.into_iter().map(|(_, len, _)| len).collect();
    if out.is_empty() {
        vec![1, 1]
    } else {
        out
    }
}

fn exact_index_from_f64(value: f64) -> Option<i64> {
    if !value.is_finite() {
        return None;
    }
    let rounded = value.round();
    if (rounded - value).abs() > f64::EPSILON {
        return None;
    }
    if rounded < i64::MIN as f64 || rounded > i64::MAX as f64 {
        return None;
    }
    Some(rounded as i64)
}

pub fn build_index_plan(
    selectors: &[SliceSelector],
    dims: usize,
    base_shape: &[usize],
) -> VmResult<IndexPlan> {
    let total_len = total_len_from_shape(base_shape);
    if dims == 1 {
        let list = selectors
            .first()
            .cloned()
            .unwrap_or(SliceSelector::Indices(Vec::new()));
        let indices = match &list {
            SliceSelector::Colon => (1..=total_len).collect::<Vec<usize>>(),
            SliceSelector::Scalar(i) => vec![*i],
            SliceSelector::Indices(v) => v.clone(),
            SliceSelector::LinearIndices { values, .. } => values.clone(),
        };
        if indices.iter().any(|&i| i == 0 || i > total_len) {
            return Err(mex("IndexOutOfBounds", "Index out of bounds"));
        }
        let zero_based: Vec<u32> = indices.iter().map(|&i| (i - 1) as u32).collect();
        let count = zero_based.len();
        let base_is_row_vector = base_shape.first().copied().unwrap_or(1) == 1
            && base_shape.get(1).copied().unwrap_or(1) > 1;
        let shape = match list {
            SliceSelector::Colon => vec![count, 1],
            SliceSelector::LinearIndices { output_shape, .. } => output_shape,
            _ if count == 0 => vec![0, 1],
            _ if count <= 1 => vec![1, 1],
            _ if base_is_row_vector => vec![1, count],
            _ => vec![count, 1],
        };
        return Ok(IndexPlan::new(
            zero_based,
            shape,
            vec![count],
            dims,
            base_shape.to_vec(),
        ));
    }

    let mut selection_lengths = Vec::with_capacity(dims);
    let mut per_dim_lists: Vec<Vec<usize>> = Vec::with_capacity(dims);
    let mut scalar_mask: Vec<bool> = Vec::with_capacity(dims);
    for (d, sel) in selectors.iter().enumerate().take(dims) {
        let dim_len = base_shape.get(d).copied().unwrap_or(1);
        let idxs = match sel {
            SliceSelector::Colon => (1..=dim_len).collect::<Vec<usize>>(),
            SliceSelector::Scalar(i) => vec![*i],
            SliceSelector::Indices(v) => v.clone(),
            SliceSelector::LinearIndices { values: v, .. } => v.clone(),
        };
        if idxs.iter().any(|&i| i == 0 || i > dim_len) {
            return Err(mex("IndexOutOfBounds", "Index out of bounds"));
        }
        selection_lengths.push(idxs.len());
        per_dim_lists.push(idxs);
        scalar_mask.push(matches!(sel, SliceSelector::Scalar(_)));
    }

    let mut out_shape = matlab_squeezed_shape(&selection_lengths, &scalar_mask);
    if selection_lengths.contains(&0) {
        let selection_lengths = out_shape.clone();
        return Ok(IndexPlan::new(
            Vec::new(),
            out_shape,
            selection_lengths,
            dims,
            base_shape.to_vec(),
        ));
    }

    let mut base_norm = base_shape.to_vec();
    if base_norm.len() < dims {
        base_norm.resize(dims, 1);
    }
    let mut strides = vec![1usize; dims];
    for d in 1..dims {
        strides[d] = strides[d - 1] * base_norm[d - 1].max(1);
    }

    let mut indices = Vec::new();
    cartesian_product(&per_dim_lists, |multi| {
        let mut lin = 0usize;
        for d in 0..dims {
            let idx = multi[d] - 1;
            lin += idx * strides[d];
        }
        indices.push(lin as u32);
    });

    let total_out: usize = selection_lengths.iter().product();
    if total_out == 1 {
        out_shape = vec![1, 1];
    }
    let selection_lengths = out_shape.clone();
    Ok(IndexPlan::new(
        indices,
        out_shape,
        selection_lengths,
        dims,
        base_shape.to_vec(),
    ))
}

#[derive(Clone)]
enum ExprSel {
    Colon,
    Scalar(usize),
    Indices(Vec<usize>),
    Range {
        start: i64,
        step: i64,
        end_off: EndExpr,
    },
}

pub struct ExprPlanSpec<'a> {
    pub dims: usize,
    pub colon_mask: u32,
    pub end_mask: u32,
    pub range_dims: &'a [usize],
    pub range_params: &'a [(f64, f64)],
    pub range_start_exprs: &'a [Option<EndExpr>],
    pub range_step_exprs: &'a [Option<EndExpr>],
    pub range_end_exprs: &'a [EndExpr],
    pub numeric: &'a [Value],
    pub shape: &'a [usize],
}

fn selector_mask_has_dim(mask: u32, dim: usize) -> bool {
    dim < u32::BITS as usize && (mask & (1u32 << dim)) != 0
}

fn validate_expr_range_selector_plan(
    spec: &ExprPlanSpec<'_>,
) -> Result<Vec<Option<usize>>, RuntimeError> {
    let range_len = spec.range_dims.len();
    if spec.range_params.len() != range_len
        || spec.range_start_exprs.len() != range_len
        || spec.range_step_exprs.len() != range_len
        || spec.range_end_exprs.len() != range_len
    {
        return Err(mex(
            "InvalidRangeSelectorPlan",
            "inconsistent range selector metadata",
        ));
    }

    let mut by_dim = vec![None; spec.dims];
    for (pos, &dim) in spec.range_dims.iter().enumerate() {
        if dim >= spec.dims {
            return Err(mex(
                "InvalidRangeSelectorDim",
                "range selector dimension is out of bounds",
            ));
        }
        let conflicts_with_colon = selector_mask_has_dim(spec.colon_mask, dim);
        let conflicts_with_end = selector_mask_has_dim(spec.end_mask, dim);
        if conflicts_with_colon || conflicts_with_end {
            return Err(mex(
                "InvalidRangeSelectorPlan",
                "range selector conflicts with colon/end selector masks",
            ));
        }
        if by_dim[dim].replace(pos).is_some() {
            return Err(mex(
                "InvalidRangeSelectorPlan",
                "range selector dimension appears more than once",
            ));
        }
    }
    Ok(by_dim)
}

pub async fn build_expr_index_plan<ResolveEnd, Fut>(
    spec: ExprPlanSpec<'_>,
    mut resolve_end: ResolveEnd,
) -> Result<IndexPlan, RuntimeError>
where
    ResolveEnd: FnMut(usize, &EndExpr) -> Fut,
    Fut: Future<Output = Result<f64, RuntimeError>>,
{
    let rank = spec.shape.len();
    let full_shape: Vec<usize> = if spec.dims == 1 {
        vec![total_len_from_shape(spec.shape)]
    } else if rank < spec.dims {
        let mut s = spec.shape.to_vec();
        s.resize(spec.dims, 1);
        s
    } else {
        spec.shape.to_vec()
    };

    let range_pos_by_dim = validate_expr_range_selector_plan(&spec)?;
    let mut selectors: Vec<ExprSel> = Vec::with_capacity(spec.dims);
    let mut linear_output_shape: Option<Vec<usize>> = None;
    let mut num_iter = 0usize;
    for (d, range_pos) in range_pos_by_dim.iter().enumerate().take(spec.dims) {
        let is_colon = selector_mask_has_dim(spec.colon_mask, d);
        let is_end = selector_mask_has_dim(spec.end_mask, d);
        if is_colon {
            selectors.push(ExprSel::Colon);
        } else if is_end {
            selectors.push(ExprSel::Scalar(*full_shape.get(d).unwrap_or(&1)));
        } else if let Some(pos) = *range_pos {
            let (raw_st, raw_sp) = spec.range_params[pos];
            let dim_len = *full_shape.get(d).unwrap_or(&1);
            let st = if let Some(expr) = &spec.range_start_exprs[pos] {
                resolve_end(dim_len, expr).await? as f64
            } else {
                raw_st
            };
            let sp = if let Some(expr) = &spec.range_step_exprs[pos] {
                resolve_end(dim_len, expr).await? as f64
            } else {
                raw_sp
            };
            let start = exact_index_from_f64(st).ok_or_else(|| {
                mex(
                    "UnsupportedIndexType",
                    "Index values must be positive integers or logical values",
                )
            })?;
            let step = exact_index_from_f64(sp).ok_or_else(|| {
                mex(
                    "UnsupportedIndexType",
                    "Index values must be positive integers or logical values",
                )
            })?;
            let off = spec.range_end_exprs[pos].clone();
            selectors.push(ExprSel::Range {
                start,
                step,
                end_off: off,
            });
        } else {
            let v = spec
                .numeric
                .get(num_iter)
                .ok_or_else(|| mex("MissingNumericIndex", "missing numeric index"))?;
            num_iter += 1;
            if let Some(idx) = index_scalar_from_value(v).await? {
                if idx < 1 {
                    return Err(mex("IndexOutOfBounds", "Index out of bounds"));
                }
                selectors.push(ExprSel::Scalar(idx as usize));
            } else {
                match v {
                    Value::Bool(b) => {
                        selectors.push(if *b {
                            ExprSel::Indices(vec![1])
                        } else {
                            ExprSel::Indices(Vec::new())
                        });
                    }
                    Value::LogicalArray(la) => {
                        if la.data.len() == 1 && is_scalar_shape(&la.shape) {
                            selectors.push(if la.data[0] != 0 {
                                ExprSel::Indices(vec![1])
                            } else {
                                ExprSel::Indices(Vec::new())
                            });
                        } else {
                            let dim_len = *full_shape.get(d).unwrap_or(&1);
                            if la.data.len() != dim_len {
                                return Err(mex(
                                    "IndexShape",
                                    "Logical mask length mismatch for dimension",
                                ));
                            }
                            let mut vv = Vec::new();
                            for (i, &bit) in la.data.iter().enumerate() {
                                if bit != 0 {
                                    vv.push(i + 1);
                                }
                            }
                            if spec.dims == 1 {
                                // MATLAB-style linear logical indexing returns a column vector.
                                linear_output_shape = Some(vec![vv.len(), 1]);
                            }
                            selectors.push(ExprSel::Indices(vv));
                        }
                    }
                    Value::Tensor(idx_t) => {
                        let len = idx_t.shape.iter().product::<usize>();
                        if spec.dims == 1 {
                            linear_output_shape = Some(idx_t.shape.clone());
                        }
                        let mut vv = Vec::with_capacity(len);
                        for &val in &idx_t.data {
                            let idx = exact_index_from_f64(val).ok_or_else(|| {
                                mex(
                                    "UnsupportedIndexType",
                                    "Index values must be positive integers or logical values",
                                )
                            })?;
                            if idx < 1 {
                                return Err(mex("IndexOutOfBounds", "Index out of bounds"));
                            }
                            vv.push(idx as usize);
                        }
                        selectors.push(ExprSel::Indices(vv));
                    }
                    _ => return Err(mex("UnsupportedIndexType", "Unsupported index type")),
                }
            }
        }
    }

    let mut per_dim_indices: Vec<Vec<usize>> = Vec::with_capacity(spec.dims);
    let mut selection_lengths: Vec<usize> = Vec::with_capacity(spec.dims);
    let mut scalar_mask: Vec<bool> = Vec::with_capacity(spec.dims);
    let base_is_row_vector = spec.dims == 1
        && spec.shape.first().copied().unwrap_or(1) == 1
        && spec.shape.get(1).copied().unwrap_or(1) > 1;
    let linear_selector_is_colon = matches!(selectors.first(), Some(ExprSel::Colon));
    for (d, sel) in selectors.iter().enumerate().take(spec.dims) {
        let dim_len = full_shape[d] as i64;
        let idxs: Vec<usize> = match sel {
            ExprSel::Colon => (1..=full_shape[d]).collect(),
            ExprSel::Scalar(i) => vec![*i],
            ExprSel::Indices(v) => v.clone(),
            ExprSel::Range {
                start,
                step,
                end_off,
            } => {
                let mut v = Vec::new();
                let mut cur = *start;
                let stp = *step;
                let end_bound = resolve_end(dim_len as usize, end_off).await?;
                if stp == 0 {
                    return Err(mex("IndexStepZero", "Index step cannot be zero"));
                }
                if !end_bound.is_finite() {
                    return Err(mex(
                        "UnsupportedIndexType",
                        "Index values must be positive integers or logical values",
                    ));
                }
                let end_i = if stp > 0 {
                    end_bound.floor()
                } else {
                    end_bound.ceil()
                };
                if end_i < i64::MIN as f64 || end_i > i64::MAX as f64 {
                    return Err(mex(
                        "UnsupportedIndexType",
                        "Index values must be positive integers or logical values",
                    ));
                }
                let end_i = end_i as i64;
                if stp > 0 {
                    while cur <= end_i {
                        if cur < 1 || cur > dim_len {
                            return Err(mex("IndexOutOfBounds", "Index out of bounds"));
                        }
                        v.push(cur as usize);
                        cur += stp;
                    }
                } else {
                    while cur >= end_i {
                        if cur < 1 || cur > dim_len {
                            return Err(mex("IndexOutOfBounds", "Index out of bounds"));
                        }
                        v.push(cur as usize);
                        cur += stp;
                    }
                }
                v
            }
        };
        if idxs.iter().any(|&i| i == 0 || i > full_shape[d]) {
            return Err(mex("IndexOutOfBounds", "Index out of bounds"));
        }
        selection_lengths.push(idxs.len());
        per_dim_indices.push(idxs);
        scalar_mask.push(matches!(sel, ExprSel::Scalar(_)));
    }

    let mut strides: Vec<usize> = vec![0; spec.dims];
    let mut acc = 1usize;
    for (d, stride) in strides.iter_mut().enumerate().take(spec.dims) {
        *stride = acc;
        acc *= full_shape[d];
    }
    let total_out: usize = per_dim_indices.iter().map(|v| v.len()).product();
    if total_out == 0 {
        let output_shape = if spec.dims == 1 {
            if let Some(shape) = linear_output_shape.clone() {
                shape
            } else if linear_selector_is_colon {
                vec![0, 1]
            } else if base_is_row_vector {
                vec![1, 0]
            } else {
                vec![0, 1]
            }
        } else {
            let mut dims_out: Vec<(usize, usize, bool)> = selection_lengths
                .iter()
                .enumerate()
                .map(|(d, &len)| (d, len, scalar_mask.get(d).copied().unwrap_or(false)))
                .collect();
            while dims_out.len() > 2
                && dims_out
                    .last()
                    .map(|&(_, len, is_scalar)| len == 1 && is_scalar)
                    .unwrap_or(false)
            {
                dims_out.pop();
            }
            if dims_out.is_empty() {
                vec![1, 1]
            } else if dims_out.len() == 1 {
                let (dim, len, _) = dims_out[0];
                if dim == 1 {
                    vec![1, len]
                } else {
                    vec![len, 1]
                }
            } else {
                dims_out.into_iter().map(|(_, len, _)| len).collect()
            }
        };
        return Ok(IndexPlan::new(
            Vec::new(),
            output_shape,
            selection_lengths,
            spec.dims,
            spec.shape.to_vec(),
        ));
    }

    let mut indices: Vec<u32> = Vec::with_capacity(total_out);
    let mut idx = vec![0usize; spec.dims];
    loop {
        let mut lin = 0usize;
        for d in 0..spec.dims {
            let i0 = per_dim_indices[d][idx[d]] - 1;
            lin += i0 * strides[d];
        }
        indices.push(lin as u32);
        let mut d = 0usize;
        while d < spec.dims {
            idx[d] += 1;
            if idx[d] < per_dim_indices[d].len() {
                break;
            }
            idx[d] = 0;
            d += 1;
        }
        if d == spec.dims {
            break;
        }
    }

    let output_shape = if spec.dims == 1 {
        if let Some(shape) = linear_output_shape {
            shape
        } else if total_out <= 1 {
            vec![1, 1]
        } else if linear_selector_is_colon {
            vec![total_out, 1]
        } else if base_is_row_vector {
            vec![1, total_out]
        } else {
            vec![total_out, 1]
        }
    } else {
        let mut dims_out: Vec<(usize, usize, bool)> = selection_lengths
            .iter()
            .enumerate()
            .map(|(d, &len)| (d, len, scalar_mask.get(d).copied().unwrap_or(false)))
            .collect();
        while dims_out.len() > 2
            && dims_out
                .last()
                .map(|&(_, len, is_scalar)| len == 1 && is_scalar)
                .unwrap_or(false)
        {
            dims_out.pop();
        }
        if dims_out.is_empty() {
            vec![1, 1]
        } else if dims_out.len() == 1 {
            let (dim, len, _) = dims_out[0];
            if dim == 1 {
                vec![1, len]
            } else {
                vec![len, 1]
            }
        } else {
            dims_out.into_iter().map(|(_, len, _)| len).collect()
        }
    };
    Ok(IndexPlan::new(
        indices,
        output_shape,
        selection_lengths,
        spec.dims,
        spec.shape.to_vec(),
    ))
}

#[cfg(test)]
mod tests {
    use super::{build_expr_index_plan, build_index_plan, ExprPlanSpec};
    use crate::bytecode::EndExpr;
    use crate::indexing::selectors::{build_slice_selectors, SliceSelector};
    use runmat_builtins::{LogicalArray, Tensor, Value};

    #[test]
    fn plain_and_expr_linear_range_plans_match() {
        futures::executor::block_on(async {
            let shape = vec![1, 10];
            let numeric = vec![Value::Tensor(
                Tensor::new(vec![2.0, 4.0, 6.0, 8.0], vec![1, 4]).unwrap(),
            )];
            let plain_selectors = build_slice_selectors(1, 0, 0, &numeric, &shape)
                .await
                .unwrap();
            let plain = build_index_plan(&plain_selectors, 1, &shape).unwrap();
            let expr = build_expr_index_plan(
                ExprPlanSpec {
                    dims: 1,
                    colon_mask: 0,
                    end_mask: 0,
                    range_dims: &[0],
                    range_params: &[(2.0, 2.0)],
                    range_start_exprs: &[None],
                    range_step_exprs: &[None],
                    range_end_exprs: &[EndExpr::Sub(
                        Box::new(EndExpr::End),
                        Box::new(EndExpr::Const(1.0)),
                    )],
                    numeric: &[],
                    shape: &shape,
                },
                |dim_len, expr| {
                    let expr = expr.clone();
                    async move {
                        Ok(match &expr {
                            EndExpr::End => dim_len as f64,
                            EndExpr::Const(value) => *value,
                            EndExpr::Sub(lhs, rhs) => {
                                let lhs_val = match lhs.as_ref() {
                                    EndExpr::End => dim_len as f64,
                                    EndExpr::Const(value) => *value,
                                    other => panic!("unsupported lhs expr: {other:?}"),
                                };
                                let rhs_val = match rhs.as_ref() {
                                    EndExpr::Const(value) => *value,
                                    other => panic!("unsupported rhs expr: {other:?}"),
                                };
                                lhs_val - rhs_val
                            }
                            other => panic!("unsupported expr: {other:?}"),
                        })
                    }
                },
            )
            .await
            .unwrap();
            assert_eq!(plain.indices, expr.indices);
            assert_eq!(plain.output_shape, expr.output_shape);
            assert_eq!(plain.selection_lengths, expr.selection_lengths);
            assert_eq!(plain.properties.full_row, expr.properties.full_row);
            assert_eq!(plain.properties.full_column, expr.properties.full_column);
        })
    }

    #[test]
    fn plain_and_expr_column_plans_match_properties() {
        futures::executor::block_on(async {
            let shape = vec![3, 4];
            let numeric = vec![Value::Num(3.0)];
            let plain_selectors = build_slice_selectors(2, 1, 0, &numeric, &shape)
                .await
                .unwrap();
            let plain = build_index_plan(&plain_selectors, 2, &shape).unwrap();
            let expr = build_expr_index_plan(
                ExprPlanSpec {
                    dims: 2,
                    colon_mask: 1,
                    end_mask: 0,
                    range_dims: &[],
                    range_params: &[],
                    range_start_exprs: &[],
                    range_step_exprs: &[],
                    range_end_exprs: &[],
                    numeric: &numeric,
                    shape: &shape,
                },
                |_dim_len, _expr| async move { unreachable!() },
            )
            .await
            .unwrap();
            assert_eq!(plain.indices, expr.indices);
            assert_eq!(plain.properties.full_column, Some(2));
            assert_eq!(plain.properties.full_column, expr.properties.full_column);
            assert_eq!(plain.properties.full_row, expr.properties.full_row);
        })
    }

    #[test]
    fn expr_plan_rejects_range_dim_conflicting_with_colon_mask() {
        futures::executor::block_on(async {
            let err = build_expr_index_plan(
                ExprPlanSpec {
                    dims: 2,
                    colon_mask: 0b01,
                    end_mask: 0,
                    range_dims: &[0],
                    range_params: &[(1.0, 1.0)],
                    range_start_exprs: &[None],
                    range_step_exprs: &[None],
                    range_end_exprs: &[EndExpr::End],
                    numeric: &[Value::Num(1.0)],
                    shape: &[3, 3],
                },
                |_dim_len, _expr| async move { unreachable!() },
            )
            .await
            .expect_err("range/colon conflict should fail");
            assert_eq!(err.identifier(), Some("RunMat:InvalidRangeSelectorPlan"));
        })
    }

    #[test]
    fn expr_plan_rejects_range_dim_conflicting_with_end_mask() {
        futures::executor::block_on(async {
            let err = build_expr_index_plan(
                ExprPlanSpec {
                    dims: 2,
                    colon_mask: 0,
                    end_mask: 0b10,
                    range_dims: &[1],
                    range_params: &[(1.0, 1.0)],
                    range_start_exprs: &[None],
                    range_step_exprs: &[None],
                    range_end_exprs: &[EndExpr::End],
                    numeric: &[Value::Num(1.0)],
                    shape: &[3, 3],
                },
                |_dim_len, _expr| async move { unreachable!() },
            )
            .await
            .expect_err("range/end conflict should fail");
            assert_eq!(err.identifier(), Some("RunMat:InvalidRangeSelectorPlan"));
        })
    }

    #[test]
    fn expr_plan_rejects_duplicate_range_dims() {
        futures::executor::block_on(async {
            let err = build_expr_index_plan(
                ExprPlanSpec {
                    dims: 2,
                    colon_mask: 0,
                    end_mask: 0,
                    range_dims: &[1, 1],
                    range_params: &[(1.0, 1.0), (1.0, 1.0)],
                    range_start_exprs: &[None, None],
                    range_step_exprs: &[None, None],
                    range_end_exprs: &[EndExpr::End, EndExpr::End],
                    numeric: &[Value::Num(1.0)],
                    shape: &[3, 3],
                },
                |_dim_len, _expr| async move { unreachable!() },
            )
            .await
            .expect_err("duplicate range dims should fail");
            assert_eq!(err.identifier(), Some("RunMat:InvalidRangeSelectorPlan"));
        })
    }

    #[test]
    fn expr_plan_rejects_out_of_bounds_range_dim() {
        futures::executor::block_on(async {
            let err = build_expr_index_plan(
                ExprPlanSpec {
                    dims: 2,
                    colon_mask: 0,
                    end_mask: 0,
                    range_dims: &[2],
                    range_params: &[(1.0, 1.0)],
                    range_start_exprs: &[None],
                    range_step_exprs: &[None],
                    range_end_exprs: &[EndExpr::End],
                    numeric: &[Value::Num(1.0), Value::Num(1.0)],
                    shape: &[3, 3],
                },
                |_dim_len, _expr| async move { unreachable!() },
            )
            .await
            .expect_err("out-of-bounds range dim should fail");
            assert_eq!(err.identifier(), Some("RunMat:InvalidRangeSelectorDim"));
        })
    }

    #[test]
    fn expr_plan_rejects_inconsistent_range_metadata_lengths() {
        futures::executor::block_on(async {
            let err = build_expr_index_plan(
                ExprPlanSpec {
                    dims: 2,
                    colon_mask: 0,
                    end_mask: 0,
                    range_dims: &[1],
                    range_params: &[],
                    range_start_exprs: &[None],
                    range_step_exprs: &[None],
                    range_end_exprs: &[EndExpr::End],
                    numeric: &[Value::Num(1.0)],
                    shape: &[3, 3],
                },
                |_dim_len, _expr| async move { unreachable!() },
            )
            .await
            .expect_err("inconsistent range metadata should fail");
            assert_eq!(err.identifier(), Some("RunMat:InvalidRangeSelectorPlan"));
        })
    }

    #[test]
    fn expr_plan_supports_dims_beyond_mask_width() {
        futures::executor::block_on(async {
            let numeric = vec![Value::Num(1.0); 31];
            let shape = vec![1usize; 33];
            let plan = build_expr_index_plan(
                ExprPlanSpec {
                    dims: 33,
                    colon_mask: 0b1,
                    end_mask: 0b10,
                    range_dims: &[],
                    range_params: &[],
                    range_start_exprs: &[],
                    range_step_exprs: &[],
                    range_end_exprs: &[],
                    numeric: &numeric,
                    shape: &shape,
                },
                |_dim_len, _expr| async move { unreachable!() },
            )
            .await
            .expect("expr plan for dims beyond mask width");
            assert_eq!(plan.dims, 33);
            assert!(!plan.indices.is_empty());
        })
    }

    #[test]
    fn expr_plan_tensor_selector_length_match_uses_numeric_indices() {
        futures::executor::block_on(async {
            let shape = vec![3, 2];
            let numeric = vec![Value::Tensor(
                Tensor::new(vec![2.0, 1.0, 3.0], vec![1, 3]).expect("selector tensor"),
            )];
            let plain_selectors = build_slice_selectors(2, 0b10, 0, &numeric, &shape)
                .await
                .unwrap();
            let plain = build_index_plan(&plain_selectors, 2, &shape).unwrap();
            let expr = build_expr_index_plan(
                ExprPlanSpec {
                    dims: 2,
                    colon_mask: 0b10,
                    end_mask: 0,
                    range_dims: &[],
                    range_params: &[],
                    range_start_exprs: &[],
                    range_step_exprs: &[],
                    range_end_exprs: &[],
                    numeric: &numeric,
                    shape: &shape,
                },
                |_dim_len, _expr| async move { unreachable!() },
            )
            .await
            .unwrap();
            assert_eq!(plain.indices, expr.indices);
            assert_eq!(plain.output_shape, expr.output_shape);
            assert_eq!(plain.selection_lengths, expr.selection_lengths);
        })
    }

    #[test]
    fn expr_plan_logical_selector_remains_logical_mask() {
        futures::executor::block_on(async {
            let shape = vec![3, 2];
            let numeric = vec![Value::LogicalArray(
                LogicalArray::new(vec![0, 1, 1], vec![1, 3]).expect("logical selector"),
            )];
            let plain_selectors = build_slice_selectors(2, 0b10, 0, &numeric, &shape)
                .await
                .unwrap();
            let plain = build_index_plan(&plain_selectors, 2, &shape).unwrap();
            let expr = build_expr_index_plan(
                ExprPlanSpec {
                    dims: 2,
                    colon_mask: 0b10,
                    end_mask: 0,
                    range_dims: &[],
                    range_params: &[],
                    range_start_exprs: &[],
                    range_step_exprs: &[],
                    range_end_exprs: &[],
                    numeric: &numeric,
                    shape: &shape,
                },
                |_dim_len, _expr| async move { unreachable!() },
            )
            .await
            .unwrap();
            assert_eq!(plain.indices, expr.indices);
            assert_eq!(plain.output_shape, expr.output_shape);
            assert_eq!(plain.selection_lengths, expr.selection_lengths);
        })
    }

    #[test]
    fn expr_plan_linear_tensor_selector_preserves_tensor_shape() {
        futures::executor::block_on(async {
            let shape = vec![1, 10];
            let numeric = vec![Value::Tensor(
                Tensor::new(vec![2.0, 4.0], vec![2, 1]).expect("selector tensor"),
            )];
            let plain_selectors = build_slice_selectors(1, 0, 0, &numeric, &shape)
                .await
                .unwrap();
            let plain = build_index_plan(&plain_selectors, 1, &shape).unwrap();
            let expr = build_expr_index_plan(
                ExprPlanSpec {
                    dims: 1,
                    colon_mask: 0,
                    end_mask: 0,
                    range_dims: &[],
                    range_params: &[],
                    range_start_exprs: &[],
                    range_step_exprs: &[],
                    range_end_exprs: &[],
                    numeric: &numeric,
                    shape: &shape,
                },
                |_dim_len, _expr| async move { unreachable!() },
            )
            .await
            .unwrap();
            assert_eq!(plain.indices, expr.indices);
            assert_eq!(plain.output_shape, expr.output_shape);
            assert_eq!(plain.selection_lengths, expr.selection_lengths);
        })
    }

    #[test]
    fn expr_plan_linear_colon_selector_matches_plain_shape() {
        futures::executor::block_on(async {
            let shape = vec![1, 5];
            let plain = build_index_plan(&[SliceSelector::Colon], 1, &shape).unwrap();
            let expr = build_expr_index_plan(
                ExprPlanSpec {
                    dims: 1,
                    colon_mask: 0b1,
                    end_mask: 0,
                    range_dims: &[],
                    range_params: &[],
                    range_start_exprs: &[],
                    range_step_exprs: &[],
                    range_end_exprs: &[],
                    numeric: &[],
                    shape: &shape,
                },
                |_dim_len, _expr| async move { unreachable!() },
            )
            .await
            .unwrap();
            assert_eq!(plain.indices, expr.indices);
            assert_eq!(plain.output_shape, expr.output_shape);
            assert_eq!(plain.selection_lengths, expr.selection_lengths);
        })
    }

    #[test]
    fn expr_plan_linear_logical_mask_matches_plain_shape() {
        futures::executor::block_on(async {
            let shape = vec![1, 5];
            let numeric = vec![Value::LogicalArray(
                LogicalArray::new(vec![1, 0, 1, 0, 1], vec![1, 5]).expect("logical selector"),
            )];
            let plain_selectors = build_slice_selectors(1, 0, 0, &numeric, &shape)
                .await
                .unwrap();
            let plain = build_index_plan(&plain_selectors, 1, &shape).unwrap();
            let expr = build_expr_index_plan(
                ExprPlanSpec {
                    dims: 1,
                    colon_mask: 0,
                    end_mask: 0,
                    range_dims: &[],
                    range_params: &[],
                    range_start_exprs: &[],
                    range_step_exprs: &[],
                    range_end_exprs: &[],
                    numeric: &numeric,
                    shape: &shape,
                },
                |_dim_len, _expr| async move { unreachable!() },
            )
            .await
            .unwrap();
            assert_eq!(plain.indices, expr.indices);
            assert_eq!(plain.output_shape, expr.output_shape);
            assert_eq!(plain.selection_lengths, expr.selection_lengths);
        })
    }

    #[test]
    fn linear_empty_selector_uses_empty_column_shape() {
        let plan = build_index_plan(&[SliceSelector::Indices(Vec::new())], 1, &[1, 5])
            .expect("empty linear selector should build");
        assert!(plan.indices.is_empty());
        assert_eq!(plan.output_shape, vec![0, 1]);
    }

    #[test]
    fn expr_plan_linear_empty_logical_mask_matches_plain_shape() {
        futures::executor::block_on(async {
            let shape = vec![1, 5];
            let numeric = vec![Value::LogicalArray(
                LogicalArray::new(vec![0, 0, 0, 0, 0], vec![1, 5]).expect("logical selector"),
            )];
            let plain_selectors = build_slice_selectors(1, 0, 0, &numeric, &shape)
                .await
                .unwrap();
            let plain = build_index_plan(&plain_selectors, 1, &shape).unwrap();
            let expr = build_expr_index_plan(
                ExprPlanSpec {
                    dims: 1,
                    colon_mask: 0,
                    end_mask: 0,
                    range_dims: &[],
                    range_params: &[],
                    range_start_exprs: &[],
                    range_step_exprs: &[],
                    range_end_exprs: &[],
                    numeric: &numeric,
                    shape: &shape,
                },
                |_dim_len, _expr| async move { unreachable!() },
            )
            .await
            .unwrap();
            assert!(plain.indices.is_empty());
            assert!(expr.indices.is_empty());
            assert_eq!(plain.output_shape, vec![0, 1]);
            assert_eq!(plain.output_shape, expr.output_shape);
            assert_eq!(plain.selection_lengths, expr.selection_lengths);
        })
    }
}