zub 0.3.14

A fast, stack-based virtual machine for dynamic languages, with an intuitive IR-builder, garbage collection and NaN-tagging.
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
use super::chunk::{ Chunk, Op };
use super::*;

#[derive(Debug, Clone)]
pub struct Local {
    pub name: String,
    pub depth: usize,
    pub captured: bool,
    pub reserved: bool,
}

#[derive(Debug, Clone)]
struct UpValue {
    pub index: u8,
    pub is_local: bool,
}

#[derive(Debug)]
pub struct CompileState {
    line: usize,
    pub locals: Vec<Local>,
    upvalues: Vec<UpValue>,
    function: FunctionBuilder,
    scope_depth: usize,
    breaks: Vec<usize>,
    method: bool,
}

impl CompileState {
    pub fn new(method: bool, reserved: &str, function: FunctionBuilder, scope_depth: usize) -> Self {
        let locals = vec![
            Local {
                name: reserved.into(),
                depth: 1,
                captured: false,
                reserved: true
            }
        ];

        CompileState {
            line: 0,
            locals,
            upvalues: Vec::new(),
            function,
            scope_depth,
            breaks: Vec::new(),
            method,
        }
    }

    fn capture_local(&mut self, var: &str) -> Option<u8> {
        for (i, local) in self.locals.iter_mut().enumerate().rev() {
            if local.name == var {
                local.captured = true;

                return Some(i as u8)
            }
        }

        None
    }

    fn add_local(&mut self, var: &str, depth: usize) -> u8 {
        let depth = self.scope_depth - depth;

        if self.locals.len() == std::u8::MAX as usize {
            panic!("local variable overflow")
        }

        self.locals.push(
            Local {
                name: var.into(),
                depth,
                captured: false,
                reserved: false,
            }
        );

        (self.locals.len() - 1) as u8
    }

    fn resolve_local(&mut self, var: &str) -> u8 {
        for (i, local) in self.locals.iter().enumerate().rev() {
            if local.name == var {
                return i as u8
            }
        }

        panic!("TODO: unresolved var: {} in {:#?}", var, self.locals)
    }

    fn add_upvalue(&mut self, index: u8, is_local: bool) -> u8 {
        for (i, upval) in self.upvalues.iter().enumerate() {
            if upval.index == index && upval.is_local == is_local {
                return i as u8
            }
        }

        if self.upvalues.len() == std::u8::MAX as usize {
            panic!("too many upvalues, not cool")
        } else {
            self.upvalues.push(
                UpValue {
                    index,
                    is_local
                }
            );

            (self.upvalues.len() - 1) as u8
        }
    }

    fn begin_scope(&mut self) {
        self.scope_depth += 1;
    }

    fn end_scope(&mut self) {
        let last = self.scope_depth;

        self.scope_depth -= 1;

        let mut ops = Vec::new();

        self.locals.retain(|local| {
            if local.depth < last || local.reserved {
                return true
            }

            if local.captured {
                ops.push(Op::CloseUpValue)
            } else {
                ops.push(Op::Pop)
            }

            false
        });

        ops.into_iter().rev().for_each(|op| self.emit(op))
    }

    fn emit(&mut self, op: Op) {
        self.function.chunk_mut().write(op, self.line);
    }

    fn add_break(&mut self, jmp: usize) {
        self.breaks.push(jmp);
    }

    fn breaks(&mut self) -> Vec<usize> {
        let bs = self.breaks.clone();
        self.breaks.clear();

        bs
    }
}


pub struct Compiler<'g> {
    heap: &'g mut Heap<Object>,
    pub states: Vec<CompileState>,
    pub locals_cache: Vec<Local>,
}

impl<'g> Compiler<'g> {
    pub fn new(heap: &'g mut Heap<Object>) -> Self {
        Compiler {
            heap,
            states: Vec::new(),
            locals_cache: Vec::new(),
        }
    }

    pub fn compile(&mut self, exprs: &[ExprNode]) -> Function {
        self.start_function(false, "<zub>", 0, 0);

        for expr in exprs.iter() {
            self.compile_expr(expr)
        }

        self.emit_return(None);
        self.end_function()
    }

    pub fn compile_from(&mut self, exprs: &[ExprNode], locals: Vec<Local>) -> Function {
        self.start_function(false, "<zub>", 0, 0);
        self.states.last_mut().unwrap().locals = locals;

        for expr in exprs.iter() {
            self.compile_expr(expr)
        }

        self.emit_return(None);
        self.end_function()
    }

    fn compile_expr(&mut self, expr: &ExprNode) {
        use self::Expr::*;

        match expr.inner() {
            Literal(ref lit) => self.emit_constant(lit),
            Unary(ref op, ref node) => {
                self.compile_expr(node);

                use self::UnaryOp::*;

                match op {
                    Neg => self.emit(Op::Neg),
                    Not => self.emit(Op::Not)
                }
            },

            Var(ref var) => self.var_get(var),
            Mutate(ref lhs, ref rhs) => {
                // Currently just handling Var
                if let Var(ref var) = lhs.inner() {
                    self.compile_expr(rhs);

                    if var.is_upvalue() {
                        let idx = self.resolve_upvalue(var.name());

                        self.emit(Op::SetUpValue);
                        self.emit_byte(idx)
                    } else {
                        if var.depth.is_none() { // Global
                            self.set_global(var.name())
                        } else {
                            let idx = self.state_mut().resolve_local(var.name());

                            self.emit(Op::SetLocal);
                            self.emit_byte(idx)
                        }
                    }
                } else {
                    // When classes are a thing, this is where we handle setting properties
                    panic!("can't mutate non-variable")
                }
            },

            Return(val) => self.emit_return((*val).clone()),

            Function(ref ir_func) => {
                self.var_define(&ir_func.var, None);

                self.function_decl(ir_func);
            },

            AnonFunction(ref ir_func) => {
                self.function_decl(ir_func);
            }

            Not(ref expr) => {
                self.compile_expr(expr);
                self.emit(Op::Not)
            }

            Neg(ref expr) => {
                self.compile_expr(expr);
                self.emit(Op::Neg)
            }

            Call(ref call) => {
                let arity = call.args.len();

                if arity > 8 {
                    panic!("That's a lot of arguments. But I will fix this limitation asap.")
                }

                self.compile_expr(&call.callee);

                for arg in call.args.iter() {
                    self.compile_expr(arg)
                }

                self.emit(Op::Call(arity as u8))
            },

            List(ref content) => {
                for el in content.iter().rev() {
                    self.compile_expr(el)
                }

                self.emit(Op::List);
                self.emit_byte(content.len() as u8)
            },

            SetElement(ref list, ref index, ref value) => {
                self.compile_expr(value);
                self.compile_expr(index);
                self.compile_expr(list);

                self.emit(Op::SetElement);
            },

            Dict(keys, values) => {
                for (key, val) in keys.iter().zip(values.iter()) {
                    self.compile_expr(key);
                    self.compile_expr(val);
                }

                self.emit(Op::Dict);
                self.emit_byte(keys.len() as u8);
            },

            If(ref cond, ref then, ref els) => {
                self.compile_expr(cond);

                let else_jmp = self.emit_jze();

                self.emit(Op::Pop);
                self.compile_expr(then);

                let end_jmp = self.emit_jmp();

                self.patch_jmp(else_jmp);
                self.emit(Op::Pop);

                if let &Some(ref els) = els {
                    self.compile_expr(els)
                }

                self.patch_jmp(end_jmp)
            },

            While(ref cond, ref body) => {
                let ip = self.ip();

                self.compile_expr(cond);

                let end_jmp = self.emit_jze();

                self.emit(Op::Pop);
                self.compile_expr(body);

                self.emit_loop(ip);
                self.patch_jmp(end_jmp);

                self.emit(Op::Pop);

                for b in self.state_mut().breaks() {
                    self.patch_jmp(b)
                }
            },

            Break => {
                let jmp = self.emit_jmp();
                self.state_mut().add_break(jmp)
            },

            Pop => {
                self.emit(Op::Pop)
            }

            Binary(lhs, op, rhs) => {
                use self::BinaryOp::*;

                match op {
                    And => {
                        self.compile_expr(lhs);

                        let short_circuit_jmp = self.emit_jze();

                        self.emit(Op::Pop);
                        self.compile_expr(rhs);

                        self.patch_jmp(short_circuit_jmp);
                    },

                    Or => {
                        self.compile_expr(lhs);

                        let else_jmp = self.emit_jze();
                        let end_jmp = self.emit_jmp();

                        self.patch_jmp(else_jmp);
                        self.emit(Op::Pop);

                        self.compile_expr(rhs);

                        self.patch_jmp(end_jmp)
                    },

                    Index => {
                        self.compile_expr(rhs);
                        self.compile_expr(lhs);
        
                        self.emit(Op::Index);
                    }

                    _ => {
                        // This looks kinda funny, but it's an ok way of matching I guess

                        self.compile_expr(lhs); // will handle type in the future :)
                        self.compile_expr(rhs);

                        match op {
                            Add => self.emit(Op::Add),
                            Sub => self.emit(Op::Sub),
                            Rem => self.emit(Op::Rem),
                            Mul => self.emit(Op::Mul),
                            Div => self.emit(Op::Div),

                            Equal => self.emit(Op::Equal),
                            Gt => self.emit(Op::Greater),
                            Lt => self.emit(Op::Less),
                            Pow => self.emit(Op::Pow),

                            GtEqual => {
                                self.emit(Op::Less);
                                self.emit(Op::Not)
                            },

                            LtEqual => {
                                self.emit(Op::Greater);
                                self.emit(Op::Not)
                            },

                            NEqual => {
                                self.emit(Op::Equal);
                                self.emit(Op::Not)
                            },

                            _ => {}
                        }
                    }
                }
            },

            Bind(ref var, ref init) => {
                self.compile_expr(init);
                self.var_define(var, None);
            },

            BindGlobal(ref var, ref init) => {
                self.compile_expr(init);
                self.var_define(var, None)
            },

            Block(ref body) => for node in body {
                self.compile_expr(node)
            },

            _ => todo!()
        }
    }



    fn var_get(&mut self, var: &Binding) {
        if var.is_upvalue() {
            let idx = self.resolve_upvalue(var.name());

            self.emit(Op::GetUpValue);
            self.emit_byte(idx);
        } else {
            // local time B)
            if var.depth.is_none() {
                self.emit(Op::GetGlobal);
                let idx = self.string_constant(var.name());
                self.emit_byte(idx)
            } else {
                let idx = self.state_mut().resolve_local(var.name());

                self.emit(Op::GetLocal);
                self.emit_byte(idx)
            }
        }
    }

    fn var_define(&mut self, var: &Binding, constant: Option<u8>) {
        // If there's depth, it's a local
        if let Some(depth) = var.depth {
            self.state_mut().add_local(var.name(), depth);
            self.state_mut().resolve_local(var.name());
        } else {
            self.emit(Op::DefineGlobal);

            let idx = constant.unwrap_or_else(|| {
                self.string_constant(var.name())
            });

            self.emit_byte(idx)
        }
    }

    fn set_global(&mut self, name: &str) {
        self.emit(Op::SetGlobal);

        let idx = {
            let chunk = self.states.last_mut()
                .unwrap()
                .function
                .chunk_mut();

            chunk.string_constant(self.heap, name)
        };

        self.emit_byte(idx)
    }

    fn function_decl(&mut self, f: &IrFunction) {
        let name = f.var.name();
        let decl = f.body.borrow();

        let params = &decl.params;
        let body = &decl.inner;
        let arity = params.len() as u8;

        self.start_function(decl.method, name, arity, 1);

        for p in params {
            self.state_mut().add_local(p.name(), 0);
            self.state_mut().resolve_local(p.name());
        }

        for expr in body.iter() {
            self.compile_expr(expr)
        }

        self.state_mut().end_scope();

        let upvalues = self.state_mut().upvalues.clone();

        let function = self.end_function(); // Might delete later, felt cute
        let handle = self.heap.insert(Object::Function(function)).into_handle();

        let value = Value::object(handle);
        let idx = self.chunk_mut().add_constant(value);

        self.emit(Op::Closure);
        self.emit_byte(idx);

        for upvalue in upvalues {
            self.emit_byte(
                if upvalue.is_local {
                    1
                } else {
                    0
                }
            );

            self.emit_byte(upvalue.index)
        }
    }

    fn start_function(&mut self, method: bool, name: &str, arity: u8, scope: usize) {
        let next_function = FunctionBuilder::new(name, arity);
        let reserved_var = if method { "self" } else { "" };
        let state = CompileState::new(method, reserved_var, next_function, scope);

        self.states.push(state)
    }

    fn end_function(&mut self) -> Function {
        // self.emit_return(None);

        let mut state: CompileState = self.states.pop().expect("states can't be empty");

        self.locals_cache.extend(state.locals.clone());

        state.function.set_upvalue_count(state.upvalues.len());
        state.function.build()
    }

    fn resolve_upvalue(&mut self, name: &str) -> u8 {
        let end = self.states.len() - 1;

        let (scope, mut index) =
            self.states[..end].iter_mut()
                .enumerate()
                .rev()
                .filter_map(|(i, enclosing)| {
                    enclosing.capture_local(name).map(|local| (i, local))
                })
                .next()
                .expect(&format!("upvalue marked during resolution, but wasn't found: {}", name));


        index = self.states[scope + 1].add_upvalue(index, true);

        if scope >= self.states.len() - 2 {
            // if we're one scope from current function
            index
        } else {
            for enclosing in &mut self.states[scope + 2..] {
                index = enclosing.add_upvalue(index, false)
            }

            index
        }
    }

    fn emit_return(&mut self, ret: Option<ExprNode>) {
        let state = self.state_mut();
        let initializer = state.function.name() == "init" && state.method;

        if initializer {
            self.emit(Op::GetLocal);
            self.emit_byte(0)
        } else if let Some(ref expr) = ret {
            self.compile_expr(expr)
        } else {
            self.emit(Op::Nil)
        }

        self.emit(Op::Return)
    }

    fn state_mut(&mut self) -> &mut CompileState {
        self.states.last_mut().expect("states can't be empty")
    }

    fn chunk_mut(&mut self) -> &mut Chunk {
        self.states.last_mut()
            .expect("states to be non-empty")
            .function
            .chunk_mut()
    }

    fn chunk(&self) -> &Chunk {
        &self.states.last()
            .expect("states to be non-empty")
            .function
            .chunk
    }

    fn line(&mut self) -> usize {
        self.states.last_mut()
            .expect("states to be non-empty")
            .line
    }

    fn string_constant(&mut self, s: &str) -> u8 {
        let chunk = self.states.last_mut().unwrap().function.chunk_mut();

        chunk.string_constant(self.heap, s)
    }

    fn emit(&mut self, op: Op) {
        let line = self.line();
        self.chunk_mut().write(op, line);
    }

    fn emit_byte(&mut self, byte: u8) {
        self.chunk_mut().write_byte(byte);
    }

    fn emit_constant(&mut self, lit: &Literal) {
        use self::Literal::*;

        match *lit {
            Nil     => self.emit(Op::Nil),
            Boolean(b) => self.emit(if b { Op::True} else { Op::False } ),
            Number(n) => self.emit_number_literal(n),
            String(ref s) => {
                let idx = {
                    let chunk = self.states.last_mut().unwrap().function.chunk_mut();
                    chunk.string_constant(self.heap, s)
                };

                self.emit(Op::Constant(idx))
            },

            _ => panic!("not a constant")
        }
    }

    fn emit_number_literal(&mut self, n: f64) {
        self.emit(Op::Immediate);

        let value = Value::float(n).to_raw();
        let chunk = self.chunk_mut();

        chunk.write_u64(value)
    }

    fn emit_jze(&mut self) -> usize {
        let line = self.line();
        let chunk = self.chunk_mut();

        chunk.write(Op::JumpIfFalse, line);
        chunk.write_byte(0xff);
        chunk.write_byte(0xff);

        chunk.len() - 2
    }

    fn emit_jmp(&mut self) -> usize {
        let line = self.line();
        let chunk = self.chunk_mut();

        chunk.write(Op::Jump, line);
        chunk.write_byte(0xff);
        chunk.write_byte(0xff);
        chunk.len() - 2
    }

    fn emit_loop(&mut self, ip: usize) {
        let line = self.line();
        let chunk = self.chunk_mut();
        let sub = chunk.len() - ip + 3;

        let lo = (sub & 0xff) as u8;
        let hi = ((sub >> 8) & 0xff) as u8;

        chunk.write(Op::Loop, line);
        chunk.write_byte(lo);
        chunk.write_byte(hi);
    }

    fn ip(&self) -> usize {
        self.chunk().len()
    }

    fn patch_jmp(&mut self, idx: usize) {
        let jmp = self.ip();
        let lo = (jmp & 0xff) as u8;
        let hi = ((jmp >> 8) & 0xff) as u8;

        self.chunk_mut().write_byte_at(idx, lo);
        self.chunk_mut().write_byte_at(idx + 1, hi);
    }
}