xqvm 0.2.0

X-Quadratic Virtual Machine — bytecode interpreter for the XQuad Toolchain
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
// Copyright (C) 2026 Postquant Labs Incorporated
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program.  If not, see <https://www.gnu.org/licenses/>.
//
// SPDX-License-Identifier: AGPL-3.0-or-later

use crate::bytecode::codec;
use crate::{Instruction, InstructionBuilder, Program, Register};

use super::error::VerifierError;
use super::jump_target::JumpTargetPhase;
use super::loop_nesting::LoopNestingPhase;
use super::phase::{Phase, Verifier};
use super::register_type::RegisterTypePhase;
use super::stack_depth::StackDepthPhase;
use super::structural::StructuralPhase;
use super::verify;

fn bytes(instrs: &[Instruction]) -> Vec<u8> {
    instrs.iter().flat_map(codec::encode).collect()
}

// --- StructuralPhase ---

#[test]
fn structural_accepts_valid_stream() {
    let mut b = InstructionBuilder::new();
    let _ = b.emit_push(1).emit_push(2).emit_add().emit_halt();
    assert!(StructuralPhase.run(&b.build().unwrap()).is_ok());
}

#[test]
fn structural_rejects_truncated_push2() {
    // PUSH2 (0x12) needs 2 operand bytes; supply only 1.
    let prog = Program::new(vec![0x12, 0x00]);
    let err = StructuralPhase.run(&prog).unwrap_err();
    assert_eq!(err.variant_name(), "TruncatedInstruction");
}

#[test]
fn structural_rejects_reserved_opcode() {
    // 0x0D is the reserved gap in the opcode table.
    let err = StructuralPhase.run(&Program::new(vec![0x0D])).unwrap_err();
    assert_eq!(err.variant_name(), "BadOpcode");
}

// --- JumpTargetPhase ---

#[test]
fn jump_target_accepts_valid_label() {
    // TARGET(.0) then JUMP1 0.
    let code = bytes(&[
        Instruction::Target {},
        Instruction::Jump1 { label: 0 },
        Instruction::Halt {},
    ]);
    assert!(JumpTargetPhase.run(&Program::new(code)).is_ok());
}

#[test]
fn jump1_to_nonexistent_label() {
    // No TARGETs; label 0 is undefined.
    let code = bytes(&[Instruction::Jump1 { label: 0 }, Instruction::Halt {}]);
    let err = JumpTargetPhase.run(&Program::new(code)).unwrap_err();
    assert_eq!(err.variant_name(), "UndefinedJumpTarget");
}

#[test]
fn jumpi1_to_nonexistent_label() {
    let code = bytes(&[Instruction::JumpI1 { label: 0 }, Instruction::Halt {}]);
    let err = JumpTargetPhase.run(&Program::new(code)).unwrap_err();
    assert_eq!(err.variant_name(), "UndefinedJumpTarget");
}

#[test]
fn jump2_to_nonexistent_label() {
    let code = bytes(&[Instruction::Jump2 { label: 0 }, Instruction::Halt {}]);
    let err = JumpTargetPhase.run(&Program::new(code)).unwrap_err();
    assert_eq!(err.variant_name(), "UndefinedJumpTarget");
}

#[test]
fn jump2_label_out_of_range_with_one_target() {
    // One TARGET (id 0); JUMP2 label=1 is out of range.
    let code = bytes(&[
        Instruction::Target {},
        Instruction::Jump2 { label: 1 },
        Instruction::Halt {},
    ]);
    let err = JumpTargetPhase.run(&Program::new(code)).unwrap_err();
    assert_eq!(err.variant_name(), "UndefinedJumpTarget");
}

// --- LoopNestingPhase ---

#[test]
fn loop_nesting_accepts_balanced_range() {
    let mut b = InstructionBuilder::new();
    let _ = b
        .emit_push(0)
        .emit_push(3)
        .emit_range()
        .emit_next()
        .emit_halt();
    assert!(LoopNestingPhase.run(&b.build().unwrap()).is_ok());
}

#[test]
fn unmatched_range_at_eof() {
    let code = bytes(&[Instruction::Range {}, Instruction::Halt {}]);
    let err = LoopNestingPhase.run(&Program::new(code)).unwrap_err();
    assert_eq!(err.variant_name(), "UnmatchedLoop");
}

#[test]
fn next_outside_any_loop() {
    let code = bytes(&[Instruction::Next {}, Instruction::Halt {}]);
    let err = LoopNestingPhase.run(&Program::new(code)).unwrap_err();
    assert_eq!(err.variant_name(), "NoActiveLoop");
}

#[test]
fn lval_outside_loop() {
    let code = bytes(&[Instruction::LVal { reg: Register(0) }, Instruction::Halt {}]);
    let err = LoopNestingPhase.run(&Program::new(code)).unwrap_err();
    assert_eq!(err.variant_name(), "NoActiveLoop");
}

#[test]
fn lidx_outside_loop() {
    let code = bytes(&[Instruction::Lidx { reg: Register(0) }, Instruction::Halt {}]);
    let err = LoopNestingPhase.run(&Program::new(code)).unwrap_err();
    assert_eq!(err.variant_name(), "NoActiveLoop");
}

#[test]
fn nested_loops_balanced() {
    // RANGE { RANGE { NEXT } NEXT } HALT
    let code = bytes(&[
        Instruction::Range {},
        Instruction::Range {},
        Instruction::Next {},
        Instruction::Next {},
        Instruction::Halt {},
    ]);
    assert!(LoopNestingPhase.run(&Program::new(code)).is_ok());
}

#[test]
fn nested_loops_outer_unmatched() {
    // RANGE { RANGE { NEXT } HALT -- outer RANGE has no NEXT
    let code = bytes(&[
        Instruction::Range {},
        Instruction::Range {},
        Instruction::Next {},
        Instruction::Halt {},
    ]);
    let err = LoopNestingPhase.run(&Program::new(code)).unwrap_err();
    assert_eq!(err.variant_name(), "UnmatchedLoop");
    // Outermost opener is at offset 0.
    assert!(matches!(
        err,
        VerifierError::UnmatchedLoop {
            offset: 0,
            depth: 1
        }
    ));
}

// --- RegisterTypePhase ---

#[test]
fn reg_type_stow_then_load_ok() {
    let code = bytes(&[
        Instruction::Push1 { val: [7] },
        Instruction::Stow { reg: Register(0) },
        Instruction::Load { reg: Register(0) },
        Instruction::Halt {},
    ]);
    assert!(RegisterTypePhase.run(&Program::new(code)).is_ok());
}

#[test]
fn reg_type_load_unset_register_is_error() {
    let code = bytes(&[Instruction::Load { reg: Register(1) }, Instruction::Halt {}]);
    let err = RegisterTypePhase.run(&Program::new(code)).unwrap_err();
    assert_eq!(err.variant_name(), "ReadUnsetRegister");
}

#[test]
fn reg_type_output_unset_register_is_error() {
    let code = bytes(&[
        Instruction::Push1 { val: [0] }, // output slot index
        Instruction::Output { reg: Register(2) },
        Instruction::Halt {},
    ]);
    let err = RegisterTypePhase.run(&Program::new(code)).unwrap_err();
    assert_eq!(err.variant_name(), "ReadUnsetRegister");
}

#[test]
fn reg_type_drop_clears_register() {
    // STOW r0, DROP r0, LOAD r0 -- Load after Drop should error.
    let code = bytes(&[
        Instruction::Push1 { val: [1] },
        Instruction::Stow { reg: Register(0) },
        Instruction::Drop { reg: Register(0) },
        Instruction::Load { reg: Register(0) },
        Instruction::Halt {},
    ]);
    let err = RegisterTypePhase.run(&Program::new(code)).unwrap_err();
    assert_eq!(err.variant_name(), "ReadUnsetRegister");
}

#[test]
fn reg_type_wrong_type_for_load() {
    // BQMX writes Model to r0; LOAD expects Int.
    let code = bytes(&[
        Instruction::Push1 { val: [4] }, // size
        Instruction::Bqmx { reg: Register(0) },
        Instruction::Load { reg: Register(0) },
        Instruction::Halt {},
    ]);
    let err = RegisterTypePhase.run(&Program::new(code)).unwrap_err();
    assert_eq!(err.variant_name(), "RegisterTypeMismatch");
}

#[test]
fn reg_type_input_satisfies_any_read() {
    // INPUT writes Any; subsequent LOAD should not error.
    let code = bytes(&[
        Instruction::Push1 { val: [0] }, // calldata index
        Instruction::Input { reg: Register(0) },
        Instruction::Load { reg: Register(0) },
        Instruction::Halt {},
    ]);
    assert!(RegisterTypePhase.run(&Program::new(code)).is_ok());
}

#[test]
fn reg_type_bqmx_then_getline_ok() {
    let code = bytes(&[
        Instruction::Push1 { val: [4] },
        Instruction::Bqmx { reg: Register(0) },
        Instruction::Push1 { val: [0] },
        Instruction::GetLine { reg: Register(0) },
        Instruction::Halt {},
    ]);
    assert!(RegisterTypePhase.run(&Program::new(code)).is_ok());
}

#[test]
fn reg_type_veci_then_getline_mismatch() {
    // VECI writes VecInt to r0; GETLINE expects Model.
    let code = bytes(&[
        Instruction::VecI { reg: Register(0) },
        Instruction::Push1 { val: [0] },
        Instruction::GetLine { reg: Register(0) },
        Instruction::Halt {},
    ]);
    let err = RegisterTypePhase.run(&Program::new(code)).unwrap_err();
    assert_eq!(err.variant_name(), "RegisterTypeMismatch");
}

#[test]
fn reg_type_energy_wrong_sample_slot() {
    // BQMX r0=Model, BSMX r1=Sample, ENERGY with model=r1, sample=r0.
    // r1 is Sample (ok for model slot? no -- model slot requires Model).
    let code = bytes(&[
        Instruction::Push1 { val: [2] },
        Instruction::Bqmx { reg: Register(0) }, // r0 = Model
        Instruction::Push1 { val: [2] },
        Instruction::Bsmx { reg: Register(1) }, // r1 = Sample
        // Energy with model=r1(Sample) -- should fail
        Instruction::Energy {
            model: Register(1),
            sample: Register(0),
        },
        Instruction::Halt {},
    ]);
    let err = RegisterTypePhase.run(&Program::new(code)).unwrap_err();
    assert_eq!(err.variant_name(), "RegisterTypeMismatch");
}

#[test]
fn reg_type_correct_energy_program() {
    let code = bytes(&[
        Instruction::Push1 { val: [2] },
        Instruction::Bqmx { reg: Register(0) }, // r0 = Model
        Instruction::Push1 { val: [2] },
        Instruction::Bsmx { reg: Register(1) }, // r1 = Sample
        Instruction::Energy {
            model: Register(0),
            sample: Register(1),
        },
        Instruction::Halt {},
    ]);
    assert!(RegisterTypePhase.run(&Program::new(code)).is_ok());
}

#[test]
fn reg_type_veclen_accepts_vec_xqmx() {
    let code = bytes(&[
        Instruction::VecX { reg: Register(0) }, // r0 = VecXqmx
        Instruction::VecLen { reg: Register(0) },
        Instruction::Halt {},
    ]);
    assert!(RegisterTypePhase.run(&Program::new(code)).is_ok());
}

#[test]
fn reg_type_resize_accepts_model_or_sample() {
    // RESIZE accepts both Model and Sample via the Grid requirement.
    let code_model = bytes(&[
        Instruction::Push1 { val: [4] },
        Instruction::Bqmx { reg: Register(0) },
        Instruction::Push1 { val: [8] },
        Instruction::Push1 { val: [8] },
        Instruction::Resize { reg: Register(0) },
        Instruction::Halt {},
    ]);
    assert!(RegisterTypePhase.run(&Program::new(code_model)).is_ok());

    let code_sample = bytes(&[
        Instruction::Push1 { val: [4] },
        Instruction::Bsmx { reg: Register(0) },
        Instruction::Push1 { val: [8] },
        Instruction::Push1 { val: [8] },
        Instruction::Resize { reg: Register(0) },
        Instruction::Halt {},
    ]);
    assert!(RegisterTypePhase.run(&Program::new(code_sample)).is_ok());
}

// --- Verifier composition ---

#[test]
fn default_verifier_passes_valid_program() {
    let mut b = InstructionBuilder::new();
    let _ = b.emit_push(10).emit_push(32).emit_add().emit_halt();
    assert!(verify(&b.build().unwrap()).is_ok());
}

#[test]
fn custom_verifier_only_jump_phase() {
    // An unmatched RANGE is not caught if LoopNestingPhase is not included.
    let code = bytes(&[Instruction::Range {}, Instruction::Halt {}]);
    let result = Verifier::new()
        .with_phase(JumpTargetPhase)
        .run(&Program::new(code));
    assert!(
        result.is_ok(),
        "jump-only verifier should not catch loop errors"
    );
}

#[test]
fn custom_verifier_structural_then_loop() {
    // A bad opcode is caught by StructuralPhase before LoopNestingPhase runs.
    let prog = Program::new(vec![0x0D]);
    let err = Verifier::new()
        .with_phase(StructuralPhase)
        .with_phase(LoopNestingPhase)
        .run(&prog)
        .unwrap_err();
    assert_eq!(err.variant_name(), "BadOpcode");
}

// --- StackDepthPhase ---

#[test]
fn stack_depth_mismatch_at_conditional_join() {
    // PUSH1(1); JUMPI1(label=0); PUSH1(2); TARGET(label=0); HALT
    // Taken (JUMPI1 fires, depth 0) vs fall-through (depth 1) at TARGET.
    let mut b = InstructionBuilder::new();
    let label = b.label();
    let _ = b
        .emit_push(1)
        .emit_jump_if(label) // taken: depth 0
        .emit_push(2) // fall-through only: depth 1
        .place(label)
        .unwrap()
        .emit_halt();
    let err = StackDepthPhase.run(&b.build().unwrap()).unwrap_err();
    assert_eq!(err.variant_name(), "StackDepthMismatch");
}

#[test]
fn stack_effect_underflow_on_pop_empty() {
    // POP with nothing on the stack.
    let code = bytes(&[Instruction::Pop {}, Instruction::Halt {}]);
    let err = StackDepthPhase.run(&Program::new(code)).unwrap_err();
    assert_eq!(err.variant_name(), "StackUnderflow");
}

#[test]
fn stack_effect_underflow_on_binary_op() {
    // ADD on an empty stack: delta = -1, depth = 0 → depth + delta < 0 → underflow.
    // Note: the conservative scan uses net delta, so ADD with depth=1 (one item) would
    // not be caught here -- that is a documented limitation of the linear scan.
    let code = bytes(&[Instruction::Add {}, Instruction::Halt {}]);
    let err = StackDepthPhase.run(&Program::new(code)).unwrap_err();
    assert_eq!(err.variant_name(), "StackUnderflow");
}

#[test]
fn stack_effect_valid_push_pop() {
    let code = bytes(&[
        Instruction::Push1 { val: [42] },
        Instruction::Pop {},
        Instruction::Halt {},
    ]);
    assert!(StackDepthPhase.run(&Program::new(code)).is_ok());
}

#[test]
fn stack_effect_sclr_resets_depth() {
    // PUSH, PUSH, SCLR, then POP would underflow — SCLR drops both items.
    let code = bytes(&[
        Instruction::Push1 { val: [1] },
        Instruction::Push1 { val: [2] },
        Instruction::Sclr {},
        Instruction::Pop {},
        Instruction::Halt {},
    ]);
    let err = StackDepthPhase.run(&Program::new(code)).unwrap_err();
    assert_eq!(err.variant_name(), "StackUnderflow");
}

#[test]
fn stack_effect_sclr_leaves_clean_stack() {
    // PUSH, PUSH, SCLR -- depth is 0 afterwards; HALT is fine.
    let code = bytes(&[
        Instruction::Push1 { val: [1] },
        Instruction::Push1 { val: [2] },
        Instruction::Sclr {},
        Instruction::Halt {},
    ]);
    assert!(StackDepthPhase.run(&Program::new(code)).is_ok());
}

// SCLR inside a loop resets the depth counter to 0 unconditionally. If the
// depth at loop body entry was N > 0, the exit depth will be 0 != N and
// LoopStackImbalance is raised. The error message says "loop has non-zero
// stack effect", which is technically correct but the root cause is a global
// stack reset mid-iteration rather than an unmatched push or pop.
#[test]
fn sclr_inside_loop_body_causes_imbalance() {
    let code = bytes(&[
        Instruction::Push1 { val: [1] }, // extra item -- entry depth before RANGE = 3
        Instruction::Push1 { val: [0] }, // start
        Instruction::Push1 { val: [3] }, // count
        Instruction::Range {},           // pops 2; entry depth recorded = 1
        Instruction::Sclr {},            // resets depth to 0; exit depth = 0 != 1
        Instruction::Next {},
        Instruction::Halt {},
    ]);
    let err = StackDepthPhase.run(&Program::new(code)).unwrap_err();
    assert_eq!(err.variant_name(), "LoopStackImbalance");
}

#[test]
fn loop_body_neutral_passes() {
    // PUSH start, PUSH count, RANGE, NEXT, HALT -- body is empty, net effect = 0.
    let code = bytes(&[
        Instruction::Push1 { val: [0] },
        Instruction::Push1 { val: [3] },
        Instruction::Range {},
        Instruction::Next {},
        Instruction::Halt {},
    ]);
    assert!(StackDepthPhase.run(&Program::new(code)).is_ok());
}

#[test]
fn loop_body_push_causes_imbalance() {
    // Loop body does a PUSH but no matching POP: each iteration leaks one item.
    let code = bytes(&[
        Instruction::Push1 { val: [0] },
        Instruction::Push1 { val: [3] },
        Instruction::Range {},
        Instruction::Push1 { val: [99] }, // unmatched push inside loop
        Instruction::Next {},
        Instruction::Halt {},
    ]);
    let err = StackDepthPhase.run(&Program::new(code)).unwrap_err();
    assert_eq!(err.variant_name(), "LoopStackImbalance");
}

#[test]
fn loop_body_pop_causes_imbalance() {
    // Loop body pops the loop's own operands off the stack.
    // Entry depth after RANGE's -2 = 0. POP inside would underflow -- caught as underflow.
    // Instead use a loop that has 1 extra item before RANGE.
    // depth before RANGE = 3 (start=0, count=3, extra=1 → no, RANGE pops 2 → depth=1)
    // body pops 1 → depth=0, NEXT expects depth=1 → LoopStackImbalance
    let code = bytes(&[
        Instruction::Push1 { val: [1] }, // extra item
        Instruction::Push1 { val: [0] }, // start
        Instruction::Push1 { val: [3] }, // count
        Instruction::Range {},           // pops 2, depth = 1
        Instruction::Pop {},             // pops extra, depth = 0
        Instruction::Next {},            // expects depth = 1
        Instruction::Halt {},
    ]);
    let err = StackDepthPhase.run(&Program::new(code)).unwrap_err();
    assert_eq!(err.variant_name(), "LoopStackImbalance");
}

#[test]
fn nested_loops_both_neutral() {
    let code = bytes(&[
        Instruction::Push1 { val: [0] },
        Instruction::Push1 { val: [2] },
        Instruction::Range {},
        Instruction::Push1 { val: [0] },
        Instruction::Push1 { val: [2] },
        Instruction::Range {},
        Instruction::Next {},
        Instruction::Next {},
        Instruction::Halt {},
    ]);
    assert!(StackDepthPhase.run(&Program::new(code)).is_ok());
}

#[test]
fn stack_effect_on_all_variants_does_not_panic() {
    // Smoke test: stack_effect() must not panic for any instruction variant.
    use crate::bytecode::types::Register;
    let instrs: &[Instruction] = &[
        Instruction::Target {},
        Instruction::Jump1 { label: 0 },
        Instruction::JumpI1 { label: 0 },
        Instruction::Nop {},
        Instruction::Halt {},
        Instruction::Sclr {},
        Instruction::Add {},
        Instruction::Energy {
            model: Register(0),
            sample: Register(1),
        },
    ];
    for instr in instrs {
        let _ = instr.stack_effect();
    }
}