dazzle-core 0.4.6

Core library for Dazzle: Scheme interpreter (ported from OpenJade) + DSSSL primitives + Grove/FOT traits
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
# VM Architecture Documentation

## Overview

Dazzle's bytecode VM implements OpenJade's instruction-based execution model, replacing tree-walking interpretation with a compile-once, execute-many-times approach. This document describes the architecture, key optimizations, and performance characteristics.

## Architecture Comparison

### OpenJade Pattern

```cpp
class Identifier {
    Owner<Expression> def_;   // Parsed AST
    InsnPtr insn_;            // Compiled instructions (cached!)
};

class VM {
    void eval() {
        // Flat while loop - no recursion
        while (ip < instructions.size()) {
            executeInstruction(instructions[ip++]);
        }
    }
};
```

### Dazzle Implementation

```rust
// Instruction definitions (instruction.rs)
pub enum Instruction {
    Constant { value_id: ValueId },
    Variable { depth: usize, offset: usize },
    Apply { n_args: usize },
    Test { else_ip: usize },
    Jump { target_ip: usize },
    MakeClosure { params, body_ip, n_free, ... },
    Return,
    // ... 18 total instruction types
}

// VM executor (vm.rs)
pub struct VM<'a> {
    arena: &'a mut Arena,
    stack: Vec<ValueId>,
    frames: Vec<Frame>,
    primitives: HashMap<String, PrimitiveFn>,
}

// Cached instructions in ConstructionRule
pub struct ConstructionRule {
    pub pattern: String,
    pub expr: Value,
    pub cached_instructions: RefCell<Option<(Vec<Instruction>, usize)>>,
}
```

## Key Components

### 1. Instruction Set (instruction.rs)

18 bytecode instructions organized by category:

**Stack Operations:**
- `Constant { value_id }` - Push constant value
- `Dup` - Duplicate top of stack
- `Pop` - Discard top of stack

**Variables:**
- `Variable { depth, offset }` - Lexical variable lookup (display-based)
- `GlobalVariable { name }` - Global variable lookup
- `SetVariable { depth, offset }` - Lexical assignment
- `SetGlobalVariable { name }` - Global assignment
- `DefineGlobal { name }` - Global definition

**Control Flow:**
- `Test { else_ip }` - Conditional jump
- `Jump { target_ip }` - Unconditional jump
- `Apply { n_args }` - Function application
- `Return` - Return from function

**Closures:**
- `MakeClosure { params, body_ip, n_free, ... }` - Create closure

**List Operations:**
- `Cons` - Create pair
- `Car` - Get first element
- `Cdr` - Get rest
- `IsNull` - Test for nil
- `MakeList { n }` - Create list from stack values

**Arithmetic:**
- `Add`, `Subtract`, `Multiply`, `Divide` - Numeric operations
- `Equal`, `NumLt`, `NumGt` - Comparisons

### 2. Compiler (compiler.rs)

Converts Value expressions to bytecode instructions:

```rust
pub struct Compiler<'a> {
    arena: &'a Arena,
    program: Program,
    lexical_env: Vec<Vec<String>>,
}

impl<'a> Compiler<'a> {
    pub fn compile(&mut self, expr: ValueId) -> Result<usize, String> {
        match self.arena.get(expr) {
            ValueData::Symbol(name) => self.compile_variable(name),
            ValueData::Pair { car, .. } => self.compile_application(expr),
            _ => self.compile_constant(expr),
        }
    }
}
```

**Compilation Examples:**

```scheme
;; (+ 1 2)
0: Constant 1
1: Constant 2
2: Primitive "+"
3: Apply 2

;; (if #t 1 2)
0: Constant #t
1: Test 4
2: Constant 1
3: Jump 5
4: Constant 2
5: ...

;; (lambda (x) (* x x))
0: MakeClosure params=[x] body_ip=2 n_free=0
1: Jump 5
2: Variable depth=0 offset=0
3: Variable depth=0 offset=0
4: Primitive "*"
5: Apply 2
6: Return
```

### 3. VM Executor (vm.rs)

Stack-based execution engine with display-based lexical scoping:

```rust
pub struct VM<'a> {
    arena: &'a mut Arena,
    stack: Vec<ValueId>,           // Value stack
    frames: Vec<Frame>,             // Call frames
    primitives: HashMap<...>,       // ~220 registered primitives
}

pub struct Frame {
    return_ip: usize,               // Where to return
    frame_pointer: usize,           // Stack position at call
    display: Vec<Vec<ValueId>>,     // Lexical environment (depth → frame)
}

impl<'a> VM<'a> {
    pub fn run(&mut self, instructions: &[Instruction], start_ip: usize)
        -> Result<ValueId, String>
    {
        let mut ip = start_ip;

        // Flat while loop - no recursion
        while ip < instructions.len() {
            match &instructions[ip] {
                Instruction::Constant { value_id } => {
                    self.stack.push(*value_id);
                    ip += 1;
                }
                Instruction::Apply { n_args } => {
                    // ... procedure application
                }
                Instruction::Return => {
                    if let Some(frame) = self.frames.pop() {
                        ip = frame.return_ip;
                    } else {
                        break; // Top-level return
                    }
                }
                // ... handle all 18 instructions
            }
        }

        Ok(self.stack.pop().unwrap_or(NIL_ID))
    }
}
```

**Display-Based Environment:**

OpenJade's display-based lexical scoping maps each variable reference to `(depth, offset)`:

```scheme
(let ((x 1))           ; depth=0, offset=0
  (let ((y 2))         ; depth=0, offset=1
    (lambda (z)        ; depth=0, offset=0 (in lambda's frame)
      (+ x y z))))     ; x: depth=1, offset=0
                       ; y: depth=1, offset=1
                       ; z: depth=0, offset=0
```

The display is a vector of frames: `Vec<Vec<ValueId>>`
- `display[0]` = current frame
- `display[1]` = parent frame
- `display[2]` = grandparent frame
- etc.

Variable lookup: `display[depth][offset]`

### 4. Instruction Caching (evaluator.rs)

OpenJade's key optimization - compile once, execute many times:

```rust
pub struct ConstructionRule {
    pub pattern: String,
    pub expr: Value,
    // Cached compiled instructions (OpenJade's InsnPtr)
    pub cached_instructions: RefCell<Option<(Vec<Instruction>, usize)>>,
}

impl Evaluator {
    fn eval_rule_with_cache(
        &mut self,
        rule_expr: Value,
        rule_cached: RefCell<Option<(Vec<Instruction>, usize)>>,
        _env: Gc<Environment>
    ) -> EvalResult {
        // Check cache
        let cached_data = rule_cached.borrow().clone();

        if let Some((instructions, start_ip)) = cached_data {
            // Cache hit! Execute directly
            let mut vm = VM::with_primitives(&mut self.arena);
            let result_id = vm.run(&instructions, start_ip)?;
            return Ok(arena_to_value(&self.arena, result_id));
        }

        // Cache miss - compile and cache
        let expr_id = value_to_arena(&mut self.arena, &rule_expr);
        let mut compiler = Compiler::new(&self.arena);
        let start_ip = compiler.compile(expr_id)?;

        let mut program = compiler.into_program();
        program.emit(Instruction::Return);

        // Cache for next time
        let instructions = program.instructions.clone();
        *rule_cached.borrow_mut() = Some((instructions.clone(), start_ip));

        // Execute
        let mut vm = VM::with_primitives(&mut self.arena);
        let result_id = vm.run(&instructions, start_ip)?;
        Ok(arena_to_value(&self.arena, result_id))
    }
}
```

**How Caching Works:**

1. First evaluation of a ConstructionRule:
   - Compile `expr` to bytecode
   - Store in `cached_instructions`
   - Execute bytecode

2. Subsequent evaluations:
   - Retrieve cached bytecode
   - Execute directly (skip compilation)

3. Performance Impact:
   - Eliminates repeated AST traversal
   - Eliminates repeated compilation
   - Critical for DSSSL rules that fire thousands of times

### 5. Bridge (bridge.rs)

Bidirectional conversion between Gc-based `Value` and arena-based `ValueId`:

```rust
// Gc-based system (tree-walker)
pub enum Value {
    Integer(i64),
    String(Gc<String>),
    Pair(Gc<RefCell<PairData>>),
    Procedure(Procedure),
    // ...
}

// Arena-based system (VM)
pub enum ValueData {
    Integer(i64),
    String(String),
    Pair { car: ValueId, cdr: ValueId },
    Procedure(ProcedureData),
    // ...
}

// Conversion functions
pub fn value_to_arena(arena: &mut Arena, value: &Value) -> ValueId;
pub fn arena_to_value(arena: &Arena, value_id: ValueId) -> Value;
```

**Migration Strategy:**

1. Parse to `Value` (existing parser)
2. Convert `Value``ValueId` (bridge.rs)
3. Compile `ValueId` to bytecode (compiler.rs)
4. Execute bytecode (vm.rs)
5. Convert result `ValueId``Value` (bridge.rs)
6. Return to tree-walker code

This allows gradual migration while keeping all existing code working.

## Performance Characteristics

### Benchmark Results

Test: Complex recursive Fibonacci calculation with multiple let bindings

```
Tree-walker: 725.52 μs
VM:          111.13 μs
Speedup:     6.53x
```

**Why the speedup?**

1. **No recursive eval() calls** - Flat while loop
2. **Instruction caching** - Compile once, execute many
3. **No AST traversal** - Direct instruction execution
4. **Efficient variable lookup** - Direct indexing via depth/offset
5. **Stack-based operations** - Cache-friendly data locality

### Realistic Workload Performance

Expected performance on DSSSL templates (not yet measured):

- **Instruction caching benefit**: 10-50x for rules that fire frequently
- **Overall speedup**: 10-30x on real DocBook processing
- **Memory**: Lower GC pressure due to arena allocation

Construction rules in DSSSL fire thousands of times on large documents. Instruction caching eliminates repeated compilation, making this the critical optimization.

## Integration Points

### Evaluator Integration

The VM is integrated into `Evaluator` via the `use_vm` flag:

```rust
pub struct Evaluator {
    use_vm: bool,           // Enable VM execution
    arena: Arena,           // Shared arena for VM
    // ... existing tree-walker state
}

impl Evaluator {
    pub fn eval(&mut self, expr: Value, env: Gc<Environment>) -> EvalResult {
        if self.use_vm {
            self.eval_with_vm(expr, env)
        } else {
            self.eval_tree_walker(expr, env)
        }
    }

    fn process_node(&mut self, node: &dyn Node, mode: &str) -> Value {
        // ... find matching rule

        let result = if self.use_vm {
            self.eval_rule_with_cache(rule_expr, rule_cached, env)
        } else {
            self.eval(rule_expr, env)
        };

        // ...
    }
}
```

### Primitive Registration

220+ primitives are registered in the VM at startup:

```rust
impl VM<'_> {
    pub fn with_primitives(arena: &mut Arena) -> VM {
        let mut vm = VM::new(arena);

        // Register all primitives
        register_list_primitives(&mut vm);      // cons, car, cdr, etc.
        register_string_primitives(&mut vm);    // string-append, substring, etc.
        register_numeric_primitives(&mut vm);   // +, -, *, /, <, >, etc.
        register_grove_primitives(&mut vm);     // gi, data, children, etc.
        // ... ~220 total primitives

        vm
    }
}
```

Primitives have signature: `fn(arena: &mut Arena, args: &[ValueId]) -> Result<ValueId, String>`

## Instruction Reference

### Stack Manipulation

**Constant { value_id }**
- Push a constant value onto the stack
- Example: `Constant 42``[42]`

**Dup**
- Duplicate top of stack
- Example: `[1 2] → [1 2 2]`

**Pop**
- Discard top of stack
- Example: `[1 2] → [1]`

### Variables

**Variable { depth, offset }**
- Look up lexical variable in display
- `depth`: How many frames up (0 = current)
- `offset`: Index within that frame
- Example: `Variable { depth: 0, offset: 0 }` → first variable in current frame

**GlobalVariable { name }**
- Look up global variable by name
- Example: `GlobalVariable { name: "+" }` → primitive add function

**SetVariable { depth, offset }**
- Set lexical variable (pop value, assign, push value back)
- Returns the assigned value

**SetGlobalVariable { name }**
- Set global variable (pop value, assign, push value back)
- Returns the assigned value

**DefineGlobal { name }**
- Define global variable (pop value, define, push unspecified)
- Used by `define` form

### Control Flow

**Test { else_ip }**
- Pop condition, jump to `else_ip` if false
- Example:
  ```
  0: Test 3
  1: <then-branch>
  2: Jump 4
  3: <else-branch>
  4: ...
  ```

**Jump { target_ip }**
- Unconditional jump
- Used for if, cond, and sequencing

**Apply { n_args }**
- Pop procedure and n arguments, apply, push result
- Handles both primitives and compiled lambdas
- For lambdas: pushes call frame and jumps to body

**Return**
- Pop call frame and return to caller
- If no frames, end execution

### Closures

**MakeClosure { params, required_count, body_ip, n_free }**
- Create a closure capturing free variables
- Pops `n_free` values from stack (captured environment)
- Pushes closure value
- Example:
  ```scheme
  (let ((x 1))
    (lambda (y) (+ x y)))

  ; Compiles to:
  0: Constant 1                    ; x value
  1: MakeClosure params=[y] body_ip=3 n_free=1
  2: Jump 7
  3: Variable depth=1 offset=0     ; x (from captured env)
  4: Variable depth=0 offset=0     ; y (from parameter)
  5: Primitive "+"
  6: Apply 2
  7: Return
  ```

### List Operations

**Cons**
- Pop two values (car, cdr), create pair, push result
- Example: `[1 2] → [(1 . 2)]`

**Car**
- Pop pair, push car
- Example: `[(1 . 2)] → [1]`

**Cdr**
- Pop pair, push cdr
- Example: `[(1 . 2)] → [2]`

**IsNull**
- Pop value, push #t if nil, #f otherwise
- Example: `[nil] → [#t]`

**MakeList { n }**
- Pop n values, create list, push result
- Example: `[1 2 3] MakeList{3} → [(1 2 3)]`

### Arithmetic

**Add, Subtract, Multiply, Divide**
- Pop two numbers, perform operation, push result
- Example: `[2 3] Add → [5]`

**Equal**
- Pop two values, compare for equality, push boolean
- Example: `[1 1] Equal → [#t]`

**NumLt, NumGt**
- Pop two numbers, compare, push boolean
- Example: `[2 3] NumLt → [#t]`

## Future Optimizations

### 1. Tail-Call Optimization (TCO)

**Current Behavior:**
```rust
Instruction::Apply { n_args } => {
    // Always pushes new call frame
    self.frames.push(Frame { ... });
    ip = body_ip;
}
```

**Proposed Optimization:**

Add `TailApply` instruction:
```rust
enum Instruction {
    TailApply { n_args: usize },  // New instruction
    // ...
}
```

Compiler detects tail position:
```rust
fn compile_application(&mut self, expr: ValueId, in_tail_position: bool) {
    // ... compile procedure and arguments

    if in_tail_position {
        self.emit(Instruction::TailApply { n_args });
    } else {
        self.emit(Instruction::Apply { n_args });
    }
}
```

VM reuses current frame:
```rust
Instruction::TailApply { n_args } => {
    // Reuse current frame instead of pushing new one
    let current_frame = self.frames.last_mut().unwrap();
    // ... update display, reuse frame
    ip = body_ip;
}
```

**Benefit:** Enables constant-space recursion for tail-recursive functions.

### 2. Specialized Instructions

Add domain-specific instructions for common DSSSL operations:

```rust
enum Instruction {
    // Current generic instructions
    Apply { n_args: usize },

    // Proposed specialized instructions
    ProcessChildren,     // Inline process-children
    MakeEntity { system_id_reg: usize },  // Inline entity creation
    FormatNumber { style: NumberStyle },   // Inline number formatting
    // ...
}
```

**Benefit:** Eliminate VM ↔ primitive call overhead for hot operations.

### 3. Constant Folding

Optimize constant expressions at compile time:

```scheme
(+ 2 3)  ; Compile to: Constant 5
(* 2 2)  ; Compile to: Constant 4
```

**Implementation:**
```rust
fn compile_application(&mut self, expr: ValueId) {
    if self.is_constant_foldable(expr) {
        let result = self.fold_constants(expr);
        return self.emit(Instruction::Constant { value_id: result });
    }
    // ... normal compilation
}
```

### 4. Inline Caching

Cache type information for polymorphic primitives:

```rust
struct InlineCache {
    last_type: Option<TypeTag>,
    specialized_impl: Option<PrimitiveFn>,
}

Instruction::Apply { n_args, cache: InlineCache }
```

**Benefit:** Faster dispatch for primitives that handle multiple types.

### 5. Register-Based VM

Current VM is stack-based. Consider register-based alternative:

```rust
enum Instruction {
    LoadConst { dest_reg: u8, value_id: ValueId },
    LoadVar { dest_reg: u8, depth: usize, offset: usize },
    Add { dest_reg: u8, src1: u8, src2: u8 },
    Call { proc_reg: u8, args: Vec<u8> },
    // ...
}
```

**Benefit:** Fewer stack manipulations, better register allocation.

**Trade-off:** More complex compiler, larger instruction encoding.

## Testing Strategy

### Unit Tests

Each component has comprehensive tests:

**instruction.rs:**
- Instruction emission and patching
- Program construction

**compiler.rs:**
- Constant compilation
- Variable lookup compilation
- Application compilation
- Lambda compilation with closures

**vm.rs:**
- Individual instruction execution
- Primitive registration
- Frame management
- Stack operations

**bridge.rs:**
- Value ↔ ValueId conversion
- Round-trip conversion
- Complex data structure handling

### Integration Tests

**evaluator.rs:**
- VM vs tree-walker correctness
- `test_vm_vs_tree_walker()` - Ensures identical results
- Performance benchmarks

**Real-world Tests:**
- DSSSL template execution
- DocBook processing
- Large document handling

### Performance Tests

Benchmarks in `integration_tests.rs`:

```rust
#[test]
fn benchmark_vm_vs_tree_walker() {
    let complex_expr = /* ... */;

    let tree_time = benchmark_tree_walker(expr);
    let vm_time = benchmark_vm(expr);

    println!("Tree-walker: {:.2} μs", tree_time);
    println!("VM:          {:.2} μs", vm_time);
    println!("Speedup:     {:.2}x", tree_time / vm_time);
}
```

## Debugging

### VM Trace Mode

Enable instruction tracing:

```rust
impl VM<'_> {
    pub fn run_with_trace(&mut self, instructions: &[Instruction], start_ip: usize) {
        while ip < instructions.len() {
            eprintln!("IP={} Stack={:?} Insn={:?}",
                      ip, self.stack, instructions[ip]);
            // ... execute instruction
        }
    }
}
```

### Disassembler

Pretty-print compiled bytecode:

```rust
pub fn disassemble(program: &Program) {
    for (ip, insn) in program.instructions.iter().enumerate() {
        println!("{:04}: {:?}", ip, insn);
    }
}
```

Example output:
```
0000: Constant { value_id: 3 }
0001: Variable { depth: 0, offset: 0 }
0002: Apply { n_args: 1 }
0003: Return
```

## Conclusion

Dazzle's bytecode VM achieves 6.5x speedup over tree-walking interpretation through:

1. **Instruction-based execution** - Flat loop, no recursion
2. **Instruction caching** - Compile once, execute many times
3. **Display-based scoping** - Fast lexical variable access
4. **Arena allocation** - Reduced GC pressure

The architecture faithfully ports OpenJade's proven design while leveraging Rust's type safety and memory management. Future optimizations (TCO, specialized instructions, constant folding) can build on this solid foundation.

The VM is production-ready and fully integrated with the evaluator, supporting all 260 language features and 220+ primitives.