ab-riscv-interpreter 0.0.3

Composable and generic RISC-V 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
//! Zve64x permutation instructions

#[cfg(test)]
mod tests;
pub mod zve64x_perm_helpers;

use crate::v::vector_registers::VectorRegistersExt;
use crate::v::zve64x::zve64x_helpers;
use crate::{
    ExecutableInstruction, ExecutionError, InterpreterState, ProgramCounter, VirtualMemory,
};
use ab_riscv_macros::instruction_execution;
use ab_riscv_primitives::prelude::*;
use core::fmt;
use core::ops::ControlFlow;

#[instruction_execution]
impl<Reg, ExtState, Memory, PC, InstructionHandler, CustomError>
    ExecutableInstruction<
        InterpreterState<Reg, ExtState, Memory, PC, InstructionHandler, CustomError>,
        CustomError,
    > for Zve64xPermInstruction<Reg>
where
    Reg: Register,
    [(); Reg::N]:,
    ExtState: VectorRegistersExt<Reg, CustomError>,
    [(); ExtState::ELEN as usize]:,
    [(); ExtState::VLEN as usize]:,
    [(); ExtState::VLENB as usize]:,
    Memory: VirtualMemory,
    PC: ProgramCounter<Reg::Type, Memory, CustomError>,
    CustomError: fmt::Debug,
{
    #[inline(always)]
    fn execute(
        self,
        state: &mut InterpreterState<Reg, ExtState, Memory, PC, InstructionHandler, CustomError>,
    ) -> Result<ControlFlow<()>, ExecutionError<Reg::Type, CustomError>> {
        match self {
            // vmv.x.s rd, vs2
            // Copies sign-extended element 0 of vs2 (at current SEW) to GPR rd.
            // Requires valid vtype (needs SEW to know element width).
            // Does not use vl or masking; always reads element 0.
            // Resets vstart per spec §6.3.
            Self::VmvXS { rd, vs2 } => {
                if !state.ext_state.vector_instructions_allowed() {
                    Err(ExecutionError::IllegalInstruction {
                        address: state
                            .instruction_fetcher
                            .old_pc(zve64x_helpers::INSTRUCTION_SIZE),
                    })?;
                }
                let vtype = state
                    .ext_state
                    .vtype()
                    .ok_or(ExecutionError::IllegalInstruction {
                        address: state
                            .instruction_fetcher
                            .old_pc(zve64x_helpers::INSTRUCTION_SIZE),
                    })?;
                let sew = vtype.vsew();
                // SAFETY: element 0 is always within register v(vs2_base), byte offset 0;
                // VLENB >= sew.bytes() for all legal vtype configurations.
                let raw = unsafe {
                    zve64x_perm_helpers::read_element_0_u64(
                        state.ext_state.read_vreg(),
                        vs2.bits(),
                        sew,
                    )
                };
                let sign_extended = zve64x_perm_helpers::sign_extend_to_reg::<Reg>(raw, sew);
                state.regs.write(rd, sign_extended);
                state.ext_state.mark_vs_dirty();
                state.ext_state.reset_vstart();
            }
            // vmv.s.x vd, rs1
            // Copies scalar GPR rs1 (zero-extended / truncated to SEW) into element 0 of vd.
            // When vl == 0, the write is suppressed but vstart is still reset.
            // Resets vstart per spec §6.3.
            Self::VmvSX { vd, rs1 } => {
                if !state.ext_state.vector_instructions_allowed() {
                    Err(ExecutionError::IllegalInstruction {
                        address: state
                            .instruction_fetcher
                            .old_pc(zve64x_helpers::INSTRUCTION_SIZE),
                    })?;
                }
                let vtype = state
                    .ext_state
                    .vtype()
                    .ok_or(ExecutionError::IllegalInstruction {
                        address: state
                            .instruction_fetcher
                            .old_pc(zve64x_helpers::INSTRUCTION_SIZE),
                    })?;
                let sew = vtype.vsew();
                let vl = state.ext_state.vl();
                let vstart = u32::from(state.ext_state.vstart());
                // Per spec §16.1: update only when vstart < vl.
                if vstart < vl {
                    let scalar = state.regs.read(rs1).as_u64();
                    // SAFETY: element 0 always fits.
                    unsafe {
                        zve64x_perm_helpers::write_element_0_u64(
                            state.ext_state.write_vreg(),
                            vd.bits(),
                            sew,
                            scalar,
                        );
                    }
                }
                state.ext_state.mark_vs_dirty();
                state.ext_state.reset_vstart();
            }
            // vslideup.vx vd, vs2, rs1, vm
            // Slides elements of vs2 up by the scalar offset in rs1.
            // Elements vd[0..offset] are unchanged (tail-undisturbed for those positions).
            // Elements vd[i] for offset <= i < vl get vs2[i - offset].
            // Per spec §16.3.1: vd must not overlap vs2.
            Self::VslideupVx { vd, vs2, rs1, vm } => {
                if !state.ext_state.vector_instructions_allowed() {
                    Err(ExecutionError::IllegalInstruction {
                        address: state
                            .instruction_fetcher
                            .old_pc(zve64x_helpers::INSTRUCTION_SIZE),
                    })?;
                }
                let vtype = state
                    .ext_state
                    .vtype()
                    .ok_or(ExecutionError::IllegalInstruction {
                        address: state
                            .instruction_fetcher
                            .old_pc(zve64x_helpers::INSTRUCTION_SIZE),
                    })?;
                let group_regs = vtype.vlmul().register_count();
                zve64x_perm_helpers::check_vreg_group_alignment(state, vd, group_regs)?;
                zve64x_perm_helpers::check_vreg_group_alignment(state, vs2, group_regs)?;
                // vd must not overlap vs2
                zve64x_perm_helpers::check_no_overlap(state, vd, vs2, group_regs)?;
                if !vm && vd.bits() == 0 {
                    Err(ExecutionError::IllegalInstruction {
                        address: state
                            .instruction_fetcher
                            .old_pc(zve64x_helpers::INSTRUCTION_SIZE),
                    })?;
                }
                let sew = vtype.vsew();
                let vl = state.ext_state.vl();
                let vstart = u32::from(state.ext_state.vstart());
                let offset = state.regs.read(rs1).as_u64();
                // SAFETY: alignment and no-overlap verified above; vl <= VLMAX.
                unsafe {
                    zve64x_perm_helpers::execute_slideup(
                        state, vd, vs2, vm, vl, vstart, sew, offset,
                    );
                }
            }
            // vslideup.vi vd, vs2, uimm, vm
            // Same as vslideup.vx but offset is a 5-bit unsigned immediate.
            Self::VslideupVi { vd, vs2, uimm, vm } => {
                if !state.ext_state.vector_instructions_allowed() {
                    Err(ExecutionError::IllegalInstruction {
                        address: state
                            .instruction_fetcher
                            .old_pc(zve64x_helpers::INSTRUCTION_SIZE),
                    })?;
                }
                let vtype = state
                    .ext_state
                    .vtype()
                    .ok_or(ExecutionError::IllegalInstruction {
                        address: state
                            .instruction_fetcher
                            .old_pc(zve64x_helpers::INSTRUCTION_SIZE),
                    })?;
                let group_regs = vtype.vlmul().register_count();
                zve64x_perm_helpers::check_vreg_group_alignment(state, vd, group_regs)?;
                zve64x_perm_helpers::check_vreg_group_alignment(state, vs2, group_regs)?;
                zve64x_perm_helpers::check_no_overlap(state, vd, vs2, group_regs)?;
                if !vm && vd.bits() == 0 {
                    Err(ExecutionError::IllegalInstruction {
                        address: state
                            .instruction_fetcher
                            .old_pc(zve64x_helpers::INSTRUCTION_SIZE),
                    })?;
                }
                let sew = vtype.vsew();
                let vl = state.ext_state.vl();
                let vstart = u32::from(state.ext_state.vstart());
                let offset = u64::from(uimm);
                // SAFETY: same as VslideupVx.
                unsafe {
                    zve64x_perm_helpers::execute_slideup(
                        state, vd, vs2, vm, vl, vstart, sew, offset,
                    );
                }
            }
            // vslidedown.vx vd, vs2, rs1, vm
            // Element vd[i] = vs2[i + offset] if i + offset < VLMAX, else 0.
            // vd may overlap vs2 for slidedown.
            Self::VslidedownVx { vd, vs2, rs1, vm } => {
                if !state.ext_state.vector_instructions_allowed() {
                    Err(ExecutionError::IllegalInstruction {
                        address: state
                            .instruction_fetcher
                            .old_pc(zve64x_helpers::INSTRUCTION_SIZE),
                    })?;
                }
                let vtype = state
                    .ext_state
                    .vtype()
                    .ok_or(ExecutionError::IllegalInstruction {
                        address: state
                            .instruction_fetcher
                            .old_pc(zve64x_helpers::INSTRUCTION_SIZE),
                    })?;
                let group_regs = vtype.vlmul().register_count();
                zve64x_perm_helpers::check_vreg_group_alignment(state, vd, group_regs)?;
                zve64x_perm_helpers::check_vreg_group_alignment(state, vs2, group_regs)?;
                if !vm && vd.bits() == 0 {
                    Err(ExecutionError::IllegalInstruction {
                        address: state
                            .instruction_fetcher
                            .old_pc(zve64x_helpers::INSTRUCTION_SIZE),
                    })?;
                }
                let sew = vtype.vsew();
                let vl = state.ext_state.vl();
                let vstart = u32::from(state.ext_state.vstart());
                let vlmax = state.ext_state.vlmax_for_vtype(vtype);
                let offset = state.regs.read(rs1).as_u64();
                // SAFETY: alignment verified above; vl <= VLMAX; offset clamped in helper.
                unsafe {
                    zve64x_perm_helpers::execute_slidedown(
                        state, vd, vs2, vm, vl, vstart, sew, vlmax, offset,
                    );
                }
            }
            // vslidedown.vi vd, vs2, uimm, vm
            // Same as vslidedown.vx but offset is a 5-bit unsigned immediate.
            Self::VslidedownVi { vd, vs2, uimm, vm } => {
                if !state.ext_state.vector_instructions_allowed() {
                    Err(ExecutionError::IllegalInstruction {
                        address: state
                            .instruction_fetcher
                            .old_pc(zve64x_helpers::INSTRUCTION_SIZE),
                    })?;
                }
                let vtype = state
                    .ext_state
                    .vtype()
                    .ok_or(ExecutionError::IllegalInstruction {
                        address: state
                            .instruction_fetcher
                            .old_pc(zve64x_helpers::INSTRUCTION_SIZE),
                    })?;
                let group_regs = vtype.vlmul().register_count();
                zve64x_perm_helpers::check_vreg_group_alignment(state, vd, group_regs)?;
                zve64x_perm_helpers::check_vreg_group_alignment(state, vs2, group_regs)?;
                if !vm && vd.bits() == 0 {
                    Err(ExecutionError::IllegalInstruction {
                        address: state
                            .instruction_fetcher
                            .old_pc(zve64x_helpers::INSTRUCTION_SIZE),
                    })?;
                }
                let sew = vtype.vsew();
                let vl = state.ext_state.vl();
                let vstart = u32::from(state.ext_state.vstart());
                let vlmax = state.ext_state.vlmax_for_vtype(vtype);
                let offset = u64::from(uimm);
                // SAFETY: same as VslidedownVx.
                unsafe {
                    zve64x_perm_helpers::execute_slidedown(
                        state, vd, vs2, vm, vl, vstart, sew, vlmax, offset,
                    );
                }
            }
            // vslide1up.vx vd, vs2, rs1, vm
            // Element 0 of vd gets the scalar value rs1 (written at SEW width).
            // Elements vd[i] for 1 <= i < vl get vs2[i - 1].
            // vd must not overlap vs2.
            Self::Vslide1upVx { vd, vs2, rs1, vm } => {
                if !state.ext_state.vector_instructions_allowed() {
                    Err(ExecutionError::IllegalInstruction {
                        address: state
                            .instruction_fetcher
                            .old_pc(zve64x_helpers::INSTRUCTION_SIZE),
                    })?;
                }
                let vtype = state
                    .ext_state
                    .vtype()
                    .ok_or(ExecutionError::IllegalInstruction {
                        address: state
                            .instruction_fetcher
                            .old_pc(zve64x_helpers::INSTRUCTION_SIZE),
                    })?;
                let group_regs = vtype.vlmul().register_count();
                zve64x_perm_helpers::check_vreg_group_alignment(state, vd, group_regs)?;
                zve64x_perm_helpers::check_vreg_group_alignment(state, vs2, group_regs)?;
                zve64x_perm_helpers::check_no_overlap(state, vd, vs2, group_regs)?;
                if !vm && vd.bits() == 0 {
                    Err(ExecutionError::IllegalInstruction {
                        address: state
                            .instruction_fetcher
                            .old_pc(zve64x_helpers::INSTRUCTION_SIZE),
                    })?;
                }
                let sew = vtype.vsew();
                let vl = state.ext_state.vl();
                let vstart = u32::from(state.ext_state.vstart());
                let scalar = state.regs.read(rs1).as_u64();
                // SAFETY: alignment and no-overlap verified; vl <= VLMAX.
                unsafe {
                    zve64x_perm_helpers::execute_slide1up(
                        state, vd, vs2, vm, vl, vstart, sew, scalar,
                    );
                }
            }
            // vslide1down.vx vd, vs2, rs1, vm
            // Element vd[i] = vs2[i + 1] for 0 <= i < vl - 1.
            // Element vd[vl - 1] gets the scalar value rs1.
            // vd may overlap vs2 for slide1down.
            Self::Vslide1downVx { vd, vs2, rs1, vm } => {
                if !state.ext_state.vector_instructions_allowed() {
                    Err(ExecutionError::IllegalInstruction {
                        address: state
                            .instruction_fetcher
                            .old_pc(zve64x_helpers::INSTRUCTION_SIZE),
                    })?;
                }
                let vtype = state
                    .ext_state
                    .vtype()
                    .ok_or(ExecutionError::IllegalInstruction {
                        address: state
                            .instruction_fetcher
                            .old_pc(zve64x_helpers::INSTRUCTION_SIZE),
                    })?;
                let group_regs = vtype.vlmul().register_count();
                zve64x_perm_helpers::check_vreg_group_alignment(state, vd, group_regs)?;
                zve64x_perm_helpers::check_vreg_group_alignment(state, vs2, group_regs)?;
                if !vm && vd.bits() == 0 {
                    Err(ExecutionError::IllegalInstruction {
                        address: state
                            .instruction_fetcher
                            .old_pc(zve64x_helpers::INSTRUCTION_SIZE),
                    })?;
                }
                let sew = vtype.vsew();
                let vl = state.ext_state.vl();
                let vstart = u32::from(state.ext_state.vstart());
                let scalar = state.regs.read(rs1).as_u64();
                // SAFETY: alignment verified; vl <= VLMAX; overlap permitted by spec.
                unsafe {
                    zve64x_perm_helpers::execute_slide1down(
                        state, vd, vs2, vm, vl, vstart, sew, scalar,
                    );
                }
            }
            // vrgather.vv vd, vs2, vs1, vm
            // vd[i] = (vs1[i] < VLMAX) ? vs2[vs1[i]] : 0
            // vd must not overlap vs1 or vs2.
            Self::VrgatherVv { vd, vs2, vs1, vm } => {
                if !state.ext_state.vector_instructions_allowed() {
                    Err(ExecutionError::IllegalInstruction {
                        address: state
                            .instruction_fetcher
                            .old_pc(zve64x_helpers::INSTRUCTION_SIZE),
                    })?;
                }
                let vtype = state
                    .ext_state
                    .vtype()
                    .ok_or(ExecutionError::IllegalInstruction {
                        address: state
                            .instruction_fetcher
                            .old_pc(zve64x_helpers::INSTRUCTION_SIZE),
                    })?;
                let group_regs = vtype.vlmul().register_count();
                zve64x_perm_helpers::check_vreg_group_alignment(state, vd, group_regs)?;
                zve64x_perm_helpers::check_vreg_group_alignment(state, vs2, group_regs)?;
                zve64x_perm_helpers::check_vreg_group_alignment(state, vs1, group_regs)?;
                zve64x_perm_helpers::check_no_overlap(state, vd, vs2, group_regs)?;
                zve64x_perm_helpers::check_no_overlap(state, vd, vs1, group_regs)?;
                if !vm && vd.bits() == 0 {
                    Err(ExecutionError::IllegalInstruction {
                        address: state
                            .instruction_fetcher
                            .old_pc(zve64x_helpers::INSTRUCTION_SIZE),
                    })?;
                }
                let sew = vtype.vsew();
                let vl = state.ext_state.vl();
                let vstart = u32::from(state.ext_state.vstart());
                let vlmax = state.ext_state.vlmax_for_vtype(vtype);
                // SAFETY: all alignment and overlap constraints verified above; vl <= VLMAX.
                unsafe {
                    zve64x_perm_helpers::execute_rgather_vv(
                        state, vd, vs2, vs1, vm, vl, vstart, sew, vlmax,
                    );
                }
            }
            // vrgather.vx vd, vs2, rs1, vm
            // All active elements of vd get vs2[rs1] if rs1 < VLMAX, else 0.
            // vd must not overlap vs2.
            Self::VrgatherVx { vd, vs2, rs1, vm } => {
                if !state.ext_state.vector_instructions_allowed() {
                    Err(ExecutionError::IllegalInstruction {
                        address: state
                            .instruction_fetcher
                            .old_pc(zve64x_helpers::INSTRUCTION_SIZE),
                    })?;
                }
                let vtype = state
                    .ext_state
                    .vtype()
                    .ok_or(ExecutionError::IllegalInstruction {
                        address: state
                            .instruction_fetcher
                            .old_pc(zve64x_helpers::INSTRUCTION_SIZE),
                    })?;
                let group_regs = vtype.vlmul().register_count();
                zve64x_perm_helpers::check_vreg_group_alignment(state, vd, group_regs)?;
                zve64x_perm_helpers::check_vreg_group_alignment(state, vs2, group_regs)?;
                zve64x_perm_helpers::check_no_overlap(state, vd, vs2, group_regs)?;
                if !vm && vd.bits() == 0 {
                    Err(ExecutionError::IllegalInstruction {
                        address: state
                            .instruction_fetcher
                            .old_pc(zve64x_helpers::INSTRUCTION_SIZE),
                    })?;
                }
                let sew = vtype.vsew();
                let vl = state.ext_state.vl();
                let vstart = u32::from(state.ext_state.vstart());
                let vlmax = state.ext_state.vlmax_for_vtype(vtype);
                let index = state.regs.read(rs1).as_u64();
                // SAFETY: alignment and no-overlap verified; vl <= VLMAX.
                unsafe {
                    zve64x_perm_helpers::execute_rgather_scalar(
                        state, vd, vs2, vm, vl, vstart, sew, vlmax, index,
                    );
                }
            }
            // vrgather.vi vd, vs2, uimm, vm
            // Same as vrgather.vx but index is a 5-bit unsigned immediate.
            Self::VrgatherVi { vd, vs2, uimm, vm } => {
                if !state.ext_state.vector_instructions_allowed() {
                    Err(ExecutionError::IllegalInstruction {
                        address: state
                            .instruction_fetcher
                            .old_pc(zve64x_helpers::INSTRUCTION_SIZE),
                    })?;
                }
                let vtype = state
                    .ext_state
                    .vtype()
                    .ok_or(ExecutionError::IllegalInstruction {
                        address: state
                            .instruction_fetcher
                            .old_pc(zve64x_helpers::INSTRUCTION_SIZE),
                    })?;
                let group_regs = vtype.vlmul().register_count();
                zve64x_perm_helpers::check_vreg_group_alignment(state, vd, group_regs)?;
                zve64x_perm_helpers::check_vreg_group_alignment(state, vs2, group_regs)?;
                zve64x_perm_helpers::check_no_overlap(state, vd, vs2, group_regs)?;
                if !vm && vd.bits() == 0 {
                    Err(ExecutionError::IllegalInstruction {
                        address: state
                            .instruction_fetcher
                            .old_pc(zve64x_helpers::INSTRUCTION_SIZE),
                    })?;
                }
                let sew = vtype.vsew();
                let vl = state.ext_state.vl();
                let vstart = u32::from(state.ext_state.vstart());
                let vlmax = state.ext_state.vlmax_for_vtype(vtype);
                let index = u64::from(uimm);
                // SAFETY: same as VrgatherVx.
                unsafe {
                    zve64x_perm_helpers::execute_rgather_scalar(
                        state, vd, vs2, vm, vl, vstart, sew, vlmax, index,
                    );
                }
            }
            // vrgatherei16.vv vd, vs2, vs1, vm
            // Like vrgather.vv but vs1 always uses EEW=16 (regardless of SEW).
            // EMUL_vs1 = (16 / SEW) * LMUL; must be in [1/8, 8] else illegal.
            // vd must not overlap vs1 or vs2.
            Self::Vrgatherei16Vv { vd, vs2, vs1, vm } => {
                if !state.ext_state.vector_instructions_allowed() {
                    Err(ExecutionError::IllegalInstruction {
                        address: state
                            .instruction_fetcher
                            .old_pc(zve64x_helpers::INSTRUCTION_SIZE),
                    })?;
                }
                let vtype = state
                    .ext_state
                    .vtype()
                    .ok_or(ExecutionError::IllegalInstruction {
                        address: state
                            .instruction_fetcher
                            .old_pc(zve64x_helpers::INSTRUCTION_SIZE),
                    })?;
                let group_regs = vtype.vlmul().register_count();
                zve64x_perm_helpers::check_vreg_group_alignment(state, vd, group_regs)?;
                zve64x_perm_helpers::check_vreg_group_alignment(state, vs2, group_regs)?;
                // Compute EMUL for vs1 index register (EEW=16).
                let index_group_regs = vtype
                    .vlmul()
                    .index_register_count(
                        ab_riscv_primitives::instructions::v::Eew::E16,
                        vtype.vsew(),
                    )
                    .ok_or(ExecutionError::IllegalInstruction {
                        address: state
                            .instruction_fetcher
                            .old_pc(zve64x_helpers::INSTRUCTION_SIZE),
                    })?;
                zve64x_perm_helpers::check_vreg_group_alignment(state, vs1, index_group_regs)?;
                zve64x_perm_helpers::check_no_overlap(state, vd, vs2, group_regs)?;
                // vd and vs1 have different group sizes (group_regs vs index_group_regs),
                // so the symmetric helper would use the wrong size for one of the intervals.
                zve64x_perm_helpers::check_no_overlap_asymmetric(
                    state,
                    vd,
                    group_regs,
                    vs1,
                    index_group_regs,
                )?;
                if !vm && vd.bits() == 0 {
                    Err(ExecutionError::IllegalInstruction {
                        address: state
                            .instruction_fetcher
                            .old_pc(zve64x_helpers::INSTRUCTION_SIZE),
                    })?;
                }
                let sew = vtype.vsew();
                let vl = state.ext_state.vl();
                let vstart = u32::from(state.ext_state.vstart());
                let vlmax = state.ext_state.vlmax_for_vtype(vtype);
                // SAFETY: all alignment and overlap constraints verified; vl <= VLMAX;
                // vs1 uses EEW=16 with computed index_group_regs.
                unsafe {
                    zve64x_perm_helpers::execute_rgatherei16(
                        state,
                        vd,
                        vs2,
                        vs1,
                        vm,
                        vl,
                        vstart,
                        sew,
                        vlmax,
                        index_group_regs,
                    );
                }
            }
            // vmerge.vvm / vmv.v.v
            // When vm=true: vmv.v.v vd, vs1 - broadcast all active elements from vs1.
            //   vs2 is ignored; no overlap restriction on vd/vs2.
            // When vm=false: vmerge.vvm vd, vs2, vs1, v0
            //   vd[i] = v0[i] ? vs1[i] : vs2[i]
            //   vd must not overlap v0 (mask source).
            Self::VmergeVvm { vd, vs2, vs1, vm } => {
                if !state.ext_state.vector_instructions_allowed() {
                    Err(ExecutionError::IllegalInstruction {
                        address: state
                            .instruction_fetcher
                            .old_pc(zve64x_helpers::INSTRUCTION_SIZE),
                    })?;
                }
                let vtype = state
                    .ext_state
                    .vtype()
                    .ok_or(ExecutionError::IllegalInstruction {
                        address: state
                            .instruction_fetcher
                            .old_pc(zve64x_helpers::INSTRUCTION_SIZE),
                    })?;
                let group_regs = vtype.vlmul().register_count();
                zve64x_perm_helpers::check_vreg_group_alignment(state, vd, group_regs)?;
                zve64x_perm_helpers::check_vreg_group_alignment(state, vs1, group_regs)?;
                if !vm {
                    // vmerge: vs2 is read, vd must not overlap v0
                    zve64x_perm_helpers::check_vreg_group_alignment(state, vs2, group_regs)?;
                    zve64x_perm_helpers::check_no_overlap(state, vd, VReg::V0, group_regs)?;
                }
                let sew = vtype.vsew();
                let vl = state.ext_state.vl();
                let vstart = u32::from(state.ext_state.vstart());
                // SAFETY: alignment and overlap verified above; vl <= VLMAX.
                unsafe {
                    zve64x_perm_helpers::execute_merge_vv(state, vd, vs2, vs1, vm, vl, vstart, sew);
                }
            }
            // vmerge.vxm / vmv.v.x
            // When vm=true: vmv.v.x vd, rs1 - broadcast scalar to all active elements.
            // When vm=false: vmerge.vxm - vd[i] = v0[i] ? rs1 : vs2[i]
            Self::VmergeVxm { vd, vs2, rs1, vm } => {
                if !state.ext_state.vector_instructions_allowed() {
                    Err(ExecutionError::IllegalInstruction {
                        address: state
                            .instruction_fetcher
                            .old_pc(zve64x_helpers::INSTRUCTION_SIZE),
                    })?;
                }
                let vtype = state
                    .ext_state
                    .vtype()
                    .ok_or(ExecutionError::IllegalInstruction {
                        address: state
                            .instruction_fetcher
                            .old_pc(zve64x_helpers::INSTRUCTION_SIZE),
                    })?;
                let group_regs = vtype.vlmul().register_count();
                zve64x_perm_helpers::check_vreg_group_alignment(state, vd, group_regs)?;
                if !vm {
                    zve64x_perm_helpers::check_vreg_group_alignment(state, vs2, group_regs)?;
                    zve64x_perm_helpers::check_no_overlap(state, vd, VReg::V0, group_regs)?;
                }
                let sew = vtype.vsew();
                let vl = state.ext_state.vl();
                let vstart = u32::from(state.ext_state.vstart());
                let scalar = state.regs.read(rs1).as_u64();
                // SAFETY: alignment and overlap verified above; vl <= VLMAX.
                unsafe {
                    zve64x_perm_helpers::execute_merge_scalar(
                        state, vd, vs2, vm, vl, vstart, sew, scalar,
                    );
                }
            }
            // vmerge.vim / vmv.v.i
            // When vm=true: vmv.v.i vd, simm5 - broadcast sign-extended immediate.
            // When vm=false: vmerge.vim - vd[i] = v0[i] ? simm5 : vs2[i]
            Self::VmergeVim { vd, vs2, simm5, vm } => {
                if !state.ext_state.vector_instructions_allowed() {
                    Err(ExecutionError::IllegalInstruction {
                        address: state
                            .instruction_fetcher
                            .old_pc(zve64x_helpers::INSTRUCTION_SIZE),
                    })?;
                }
                let vtype = state
                    .ext_state
                    .vtype()
                    .ok_or(ExecutionError::IllegalInstruction {
                        address: state
                            .instruction_fetcher
                            .old_pc(zve64x_helpers::INSTRUCTION_SIZE),
                    })?;
                let group_regs = vtype.vlmul().register_count();
                zve64x_perm_helpers::check_vreg_group_alignment(state, vd, group_regs)?;
                if !vm {
                    zve64x_perm_helpers::check_vreg_group_alignment(state, vs2, group_regs)?;
                    zve64x_perm_helpers::check_no_overlap(state, vd, VReg::V0, group_regs)?;
                }
                let sew = vtype.vsew();
                let vl = state.ext_state.vl();
                let vstart = u32::from(state.ext_state.vstart());
                // Sign-extend imm to u64 so the low sew_bytes are correct for all SEW.
                let scalar = i64::from(simm5).cast_unsigned();
                // SAFETY: alignment and overlap verified above; vl <= VLMAX.
                unsafe {
                    zve64x_perm_helpers::execute_merge_scalar(
                        state, vd, vs2, vm, vl, vstart, sew, scalar,
                    );
                }
            }
            // vcompress.vm vd, vs2, vs1
            // Packs active elements of vs2 (where vs1 mask bit is set) sequentially into vd.
            // Always unmasked (vm=1 in encoding); vs1 is the explicit mask operand.
            // vd must not overlap vs1 or vs2.
            Self::VcompressVm { vd, vs2, vs1 } => {
                if !state.ext_state.vector_instructions_allowed() {
                    Err(ExecutionError::IllegalInstruction {
                        address: state
                            .instruction_fetcher
                            .old_pc(zve64x_helpers::INSTRUCTION_SIZE),
                    })?;
                }
                let vtype = state
                    .ext_state
                    .vtype()
                    .ok_or(ExecutionError::IllegalInstruction {
                        address: state
                            .instruction_fetcher
                            .old_pc(zve64x_helpers::INSTRUCTION_SIZE),
                    })?;
                // Spec §16.5: vstart must be zero.
                if state.ext_state.vstart() != 0 {
                    Err(ExecutionError::IllegalInstruction {
                        address: state
                            .instruction_fetcher
                            .old_pc(zve64x_helpers::INSTRUCTION_SIZE),
                    })?;
                }
                let group_regs = vtype.vlmul().register_count();
                zve64x_perm_helpers::check_vreg_group_alignment(state, vd, group_regs)?;
                zve64x_perm_helpers::check_vreg_group_alignment(state, vs2, group_regs)?;
                // vs1 is always a single mask register (no LMUL grouping)
                zve64x_perm_helpers::check_no_overlap(state, vd, vs2, group_regs)?;
                // vs1 is a mask register; check it doesn't overlap vd
                zve64x_perm_helpers::check_no_overlap(state, vd, vs1, 1)?;
                let sew = vtype.vsew();
                let vl = state.ext_state.vl();
                unsafe {
                    zve64x_perm_helpers::execute_compress(state, vd, vs2, vs1, vl, sew);
                }
            }
            // vmv1r.v vd, vs2
            // Whole register move: copies 1 register.
            // No masking, no vtype/vl dependency.
            Self::Vmv1rV { vd, vs2 } => {
                if !state.ext_state.vector_instructions_allowed() {
                    Err(ExecutionError::IllegalInstruction {
                        address: state
                            .instruction_fetcher
                            .old_pc(zve64x_helpers::INSTRUCTION_SIZE),
                    })?;
                }
                // SAFETY: both vd.bits() and vs2.bits() are always in [0, 32) by VReg invariant;
                // copying 1 register always fits.
                unsafe {
                    zve64x_perm_helpers::execute_whole_reg_move(
                        state.ext_state.write_vreg(),
                        vd.bits(),
                        vs2.bits(),
                        1,
                    );
                }
                state.ext_state.mark_vs_dirty();
                state.ext_state.reset_vstart();
            }
            // vmv2r.v vd, vs2
            // Whole register move: copies 2 registers.
            // vd and vs2 must be aligned to 2 (checked here per spec §17.6).
            Self::Vmv2rV { vd, vs2 } => {
                if !state.ext_state.vector_instructions_allowed() {
                    Err(ExecutionError::IllegalInstruction {
                        address: state
                            .instruction_fetcher
                            .old_pc(zve64x_helpers::INSTRUCTION_SIZE),
                    })?;
                }
                if !vd.bits().is_multiple_of(2) || !vs2.bits().is_multiple_of(2) {
                    Err(ExecutionError::IllegalInstruction {
                        address: state
                            .instruction_fetcher
                            .old_pc(zve64x_helpers::INSTRUCTION_SIZE),
                    })?;
                }
                // SAFETY: alignment verified; 2 registers from aligned base always stay in [0, 32).
                unsafe {
                    zve64x_perm_helpers::execute_whole_reg_move(
                        state.ext_state.write_vreg(),
                        vd.bits(),
                        vs2.bits(),
                        2,
                    );
                }
                state.ext_state.mark_vs_dirty();
                state.ext_state.reset_vstart();
            }
            // vmv4r.v vd, vs2
            // Whole register move: copies 4 registers.
            Self::Vmv4rV { vd, vs2 } => {
                if !state.ext_state.vector_instructions_allowed() {
                    Err(ExecutionError::IllegalInstruction {
                        address: state
                            .instruction_fetcher
                            .old_pc(zve64x_helpers::INSTRUCTION_SIZE),
                    })?;
                }
                if !vd.bits().is_multiple_of(4) || !vs2.bits().is_multiple_of(4) {
                    Err(ExecutionError::IllegalInstruction {
                        address: state
                            .instruction_fetcher
                            .old_pc(zve64x_helpers::INSTRUCTION_SIZE),
                    })?;
                }
                // SAFETY: alignment verified; 4 registers from aligned base always stay in [0, 32).
                unsafe {
                    zve64x_perm_helpers::execute_whole_reg_move(
                        state.ext_state.write_vreg(),
                        vd.bits(),
                        vs2.bits(),
                        4,
                    );
                }
                state.ext_state.mark_vs_dirty();
                state.ext_state.reset_vstart();
            }
            // vmv8r.v vd, vs2
            // Whole register move: copies 8 registers.
            Self::Vmv8rV { vd, vs2 } => {
                if !state.ext_state.vector_instructions_allowed() {
                    Err(ExecutionError::IllegalInstruction {
                        address: state
                            .instruction_fetcher
                            .old_pc(zve64x_helpers::INSTRUCTION_SIZE),
                    })?;
                }
                if !vd.bits().is_multiple_of(8) || !vs2.bits().is_multiple_of(8) {
                    Err(ExecutionError::IllegalInstruction {
                        address: state
                            .instruction_fetcher
                            .old_pc(zve64x_helpers::INSTRUCTION_SIZE),
                    })?;
                }
                // SAFETY: alignment verified; 8 registers from aligned base always stay in [0, 32).
                unsafe {
                    zve64x_perm_helpers::execute_whole_reg_move(
                        state.ext_state.write_vreg(),
                        vd.bits(),
                        vs2.bits(),
                        8,
                    );
                }
                state.ext_state.mark_vs_dirty();
                state.ext_state.reset_vstart();
            }
        }

        Ok(ControlFlow::Continue(()))
    }
}