run-rs 0.6.30

Run a subset of Rust as an interpreted script
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
//! Resolves every `Own` into a move or a copy. A register that is read again after the `Own` is
//! still live, and `rustc` only accepts that for a `Copy` type, so the value is copied. A dead
//! register is moved and cleared. No types are needed, the program passed the borrow checker.
//!
//! The same pass decides which captures a `move` closure takes instead of copies.

use std::collections::HashSet;

use super::FnState;
use crate::interpreter::bytecode::{CapSource, NO_ROOT, Op, Reg};

struct Bits {
    words: Vec<u64>,
}

impl Bits {
    fn new(regs: usize) -> Bits {
        Bits {
            words: vec![0; regs.div_ceil(64)],
        }
    }

    /// `DISCARD` and the other sentinels sit past the frame, they are no register.
    fn in_frame(&self, reg: usize) -> bool {
        reg < self.words.len() * 64
    }

    fn get(&self, reg: Reg) -> bool {
        let reg = usize::from(reg);
        self.in_frame(reg) && self.words[reg / 64] & (1 << (reg % 64)) != 0
    }

    fn set(&mut self, reg: Reg) {
        let reg = usize::from(reg);
        if self.in_frame(reg) {
            self.words[reg / 64] |= 1 << (reg % 64);
        }
    }

    fn clear(&mut self, reg: Reg) {
        let reg = usize::from(reg);
        if self.in_frame(reg) {
            self.words[reg / 64] &= !(1 << (reg % 64));
        }
    }

    /// true when something changed
    fn union(&mut self, other: &Bits) -> bool {
        let mut changed = false;
        for (mine, theirs) in self.words.iter_mut().zip(&other.words) {
            let next = *mine | theirs;
            changed |= next != *mine;
            *mine = next;
        }
        changed
    }
}

fn window(reads: &mut Vec<Reg>, base: Reg, count: usize) {
    for i in 0..count {
        reads.push(base + u16::try_from(i).expect("window fits u16"));
    }
}

/// The ops with an argument window or a side table behind them. A `DropScope` is not a read, a
/// dropped register was never read by the program, it is only cleaned up.
fn table_effects(f: &FnState, op: &Op, reads: &mut Vec<Reg>, writes: &mut Vec<Reg>) -> bool {
    match op {
        Op::CallFn {
            dst, base, argc, ..
        }
        | Op::CallPath {
            dst, base, argc, ..
        } => {
            window(reads, *base, usize::from(*argc));
            writes.push(*dst);
        }
        Op::CallValue {
            dst,
            callee,
            base,
            argc,
        } => {
            reads.push(*callee);
            window(reads, *base, usize::from(*argc));
            writes.push(*dst);
        }
        Op::Method {
            dst,
            recv,
            base,
            argc,
            ..
        } => {
            reads.push(*recv);
            window(reads, *base, usize::from(*argc));
            writes.push(*dst);
        }
        Op::MakeVec { dst, base, count }
        | Op::MakeTuple { dst, base, count }
        | Op::MakeEnum {
            dst, base, count, ..
        }
        | Op::Dbg {
            dst,
            base,
            argc: count,
        } => {
            window(reads, *base, usize::from(*count));
            writes.push(*dst);
        }
        Op::MakeStruct { dst, info, base } => {
            let lit = &f.struct_lits[usize::from(*info)];
            window(
                reads,
                *base,
                lit.shape.fields.len() + usize::from(lit.has_rest),
            );
            writes.push(*dst);
        }
        Op::MakeClosure { dst, child } | Op::Spawn { dst, child } => {
            for cap in &f.child_caps[usize::from(*child)] {
                if let CapSource::Local(reg) | CapSource::MutableLocal(reg) = cap {
                    reads.push(*reg);
                }
            }
            writes.push(*dst);
        }
        Op::DropScope { list } | Op::DropParams { list } => {
            writes.extend(f.drop_lists[usize::from(*list)].iter());
        }
        Op::TestBind { val, pat, dst } => {
            reads.push(*val);
            let info = &f.pats[usize::from(*pat)];
            reads.extend(info.consts.iter());
            writes.extend(info.binds.iter().map(|(_, reg)| *reg));
            writes.push(*dst);
        }
        Op::Fmt { dst, spec } | Op::MacroCall { dst, spec, .. } => {
            let spec = &f.fmts[usize::from(*spec)];
            reads.extend(spec.positional.iter());
            reads.extend(spec.named.iter().map(|(_, reg)| *reg));
            writes.push(*dst);
        }
        _ => return false,
    }
    true
}

/// The field, element and deref ops.
fn place_effects(op: &Op, reads: &mut Vec<Reg>, writes: &mut Vec<Reg>) {
    match op {
        Op::Index { dst, base, key } | Op::RefIndex { dst, base, key } => {
            reads.push(*base);
            reads.push(*key);
            writes.push(*dst);
        }
        Op::SetIndex { base, key, val } => {
            reads.push(*base);
            reads.push(*key);
            reads.push(*val);
        }
        Op::SetDeref { target, val } | Op::DerefBinAssign { target, val, .. } => {
            reads.push(*target);
            reads.push(*val);
        }
        Op::SetDerefParam { target, val } => {
            reads.push(*val);
            writes.push(*target);
        }
        Op::GetField { dst, base, .. }
        | Op::RefField { dst, base, .. }
        | Op::TakeField { dst, base, .. } => {
            reads.push(*base);
            writes.push(*dst);
        }
        Op::SetField { base, val, .. } => {
            reads.push(*base);
            reads.push(*val);
        }
        _ => unreachable!("not a place op"),
    }
}

/// The arithmetic and the compare jumps.
fn scalar_effects(op: &Op, reads: &mut Vec<Reg>, writes: &mut Vec<Reg>) {
    match op {
        Op::Take { dst, src } => {
            reads.push(*src);
            writes.push(*src);
            writes.push(*dst);
        }
        Op::Bin { dst, a, b, .. }
        | Op::BinInt { dst, a, b, .. }
        | Op::BinFloat { dst, a, b, .. } => {
            reads.push(*a);
            reads.push(*b);
            writes.push(*dst);
        }
        Op::JumpIfFalse { cond, .. } | Op::JumpIfTrue { cond, .. } => reads.push(*cond),
        Op::CmpJump { a, b, .. } | Op::CmpJumpInt { a, b, .. } => {
            reads.push(*a);
            reads.push(*b);
        }
        Op::CmpJumpImm { a, .. } | Op::CmpJumpIntImm { a, .. } => reads.push(*a),
        _ => unreachable!("not a scalar op"),
    }
}

/// `reads` and `writes` of one op.
fn effects(f: &FnState, op: &Op, reads: &mut Vec<Reg>, writes: &mut Vec<Reg>) {
    if table_effects(f, op, reads, writes) {
        return;
    }
    match op {
        Op::LoadConst { dst, .. }
        | Op::LoadInt { dst, .. }
        | Op::LoadIntW { dst, .. }
        | Op::LoadBool { dst, .. }
        | Op::LoadUnit { dst }
        | Op::LoadUpvalue { dst, .. }
        | Op::LoadGlobal { dst, .. }
        | Op::PathValue { dst, .. }
        | Op::MakeMap { dst, .. }
        | Op::LoadEnum { dst, .. }
        | Op::BuildDefault { dst, .. } => writes.push(*dst),
        Op::LoadCell { dst, cell } => {
            reads.push(*cell);
            writes.push(*dst);
        }
        Op::StoreCell { cell, src } => {
            reads.push(*src);
            writes.push(*cell);
        }
        Op::DropCell { .. } | Op::Jump { .. } => {}
        Op::StoreUpvalue { src, .. } | Op::Ret { src } => reads.push(*src),
        Op::Move { dst, src }
        | Op::Copy { dst, src }
        | Op::IterInit { dst, src, .. }
        | Op::Deref { dst, src }
        | Op::MakeBorrow { dst, src }
        | Op::DefaultOf { dst, src }
        | Op::Try { dst, src, .. }
        | Op::TryJump { dst, src, .. }
        | Op::Cast { dst, src, .. }
        | Op::Coerce { dst, src, .. }
        | Op::Await { dst, src }
        | Op::Un { dst, a: src, .. }
        | Op::BinImm { dst, a: src, .. }
        | Op::BinIntImm { dst, a: src, .. }
        | Op::Own { dst, src, .. } => {
            reads.push(*src);
            writes.push(*dst);
        }
        Op::Take { .. }
        | Op::Bin { .. }
        | Op::BinInt { .. }
        | Op::BinFloat { .. }
        | Op::JumpIfFalse { .. }
        | Op::JumpIfTrue { .. }
        | Op::CmpJump { .. }
        | Op::CmpJumpInt { .. }
        | Op::CmpJumpImm { .. }
        | Op::CmpJumpIntImm { .. } => scalar_effects(op, reads, writes),
        Op::GetOrDefault {
            dst,
            recv,
            key,
            default,
        } => {
            reads.push(*recv);
            reads.push(*key);
            reads.push(*default);
            writes.push(*dst);
        }
        Op::MakeArrayRepeat { dst, val, count } => {
            reads.push(*val);
            reads.push(*count);
            writes.push(*dst);
        }
        Op::MakeRange {
            dst, start, end, ..
        } => {
            reads.push(*start);
            reads.push(*end);
            writes.push(*dst);
        }
        Op::ForNext { iter, idx, val, .. } => {
            reads.push(*iter);
            reads.push(*idx);
            writes.push(*idx);
            writes.push(*val);
        }
        Op::Index { .. }
        | Op::RefIndex { .. }
        | Op::SetIndex { .. }
        | Op::SetDeref { .. }
        | Op::DerefBinAssign { .. }
        | Op::SetDerefParam { .. }
        | Op::GetField { .. }
        | Op::RefField { .. }
        | Op::TakeField { .. }
        | Op::SetField { .. } => place_effects(op, reads, writes),
        _ => unreachable!("handled by `table_effects`"),
    }
}

fn successors(op: &Op, at: usize, out: &mut Vec<usize>) {
    match op {
        Op::Jump { to } => out.push(*to as usize),
        Op::Ret { .. } => {}
        Op::JumpIfFalse { to, .. }
        | Op::JumpIfTrue { to, .. }
        | Op::CmpJump { to, .. }
        | Op::CmpJumpImm { to, .. }
        | Op::CmpJumpInt { to, .. }
        | Op::CmpJumpIntImm { to, .. }
        | Op::ForNext { to, .. }
        | Op::TryJump { to, .. } => {
            out.push(at + 1);
            out.push(*to as usize);
        }
        _ => out.push(at + 1),
    }
}

/// Live-in per op, to a fixpoint.
struct Liveness {
    succ: Vec<Vec<usize>>,
    writes: Vec<Vec<Reg>>,
    live_in: Vec<Bits>,
    /// never dead, see `Liveness::of`
    pinned: HashSet<Reg>,
}

impl Liveness {
    fn of(func: &FnState) -> Liveness {
        let regs = usize::from(func.max_reg).max(1);
        let count = func.code.len();
        // A register a closure captured is read by every later call of that closure, so it is
        // never dead. A cell promoted local is shared the same way.
        let mut pinned: HashSet<Reg> = func.mutable_locals.iter().copied().collect();
        for caps in &func.child_caps {
            for cap in caps {
                if let CapSource::Local(reg) | CapSource::MutableLocal(reg) = cap {
                    pinned.insert(*reg);
                }
            }
        }
        let mut reads: Vec<Vec<Reg>> = Vec::with_capacity(count);
        let mut writes: Vec<Vec<Reg>> = Vec::with_capacity(count);
        let mut succ: Vec<Vec<usize>> = Vec::with_capacity(count);
        for (at, op) in func.code.iter().enumerate() {
            let (mut op_reads, mut op_writes, mut op_succ) = (Vec::new(), Vec::new(), Vec::new());
            effects(func, op, &mut op_reads, &mut op_writes);
            successors(op, at, &mut op_succ);
            reads.push(op_reads);
            writes.push(op_writes);
            succ.push(op_succ);
        }
        let mut live_in: Vec<Bits> = (0..count).map(|_| Bits::new(regs)).collect();
        let mut changed = true;
        while changed {
            changed = false;
            for at in (0..count).rev() {
                let mut out = Bits::new(regs);
                for &next in &succ[at] {
                    if next < count {
                        out.union(&live_in[next]);
                    }
                }
                for &w in &writes[at] {
                    out.clear(w);
                }
                for &r in &reads[at] {
                    out.set(r);
                }
                changed |= live_in[at].union(&out);
            }
        }
        Liveness {
            succ,
            writes,
            live_in,
            pinned,
        }
    }

    fn live_out(&self, at: usize, reg: Reg) -> bool {
        self.pinned.contains(&reg)
            || self.succ[at]
                .iter()
                .any(|&s| s < self.live_in.len() && self.live_in[s].get(reg))
    }
}

impl FnState {
    /// Every `Own` becomes a `Take` or a `Copy`, and `child_moves` is filled.
    pub(super) fn resolve_owns(&mut self) {
        let n = self.code.len();
        let live = Liveness::of(self);
        let live_out = |at: usize, reg: Reg| live.live_out(at, reg);
        for at in 0..n {
            let Op::Own { dst, src, root } = self.code[at] else {
                if let Op::MakeClosure { child, .. } | Op::Spawn { child, .. } = self.code[at] {
                    let child = usize::from(child);
                    let moves = self.children[child].moves;
                    let takes: Vec<bool> = self.child_caps[child]
                        .iter()
                        .map(|cap| match cap {
                            CapSource::Local(reg) | CapSource::MutableLocal(reg) => {
                                moves && !live_out(at, *reg)
                            }
                            CapSource::Upvalue(_) | CapSource::MutableUpvalue(_) => false,
                        })
                        .collect();
                    self.child_moves[child] = takes.into();
                }
                continue;
            };
            self.code[at] = if root == NO_ROOT || live_out(at, root) {
                Op::Copy { dst, src }
            } else if dst == src {
                if self.mutable_locals.contains(&root) {
                    // a value moved out of a capture cell, the cell is cleared so the closure
                    // can't drop the moved part again
                    Op::LoadUnit { dst: root }
                } else {
                    // A field or element read out of a dead local. A move of a non copy field
                    // left a tombstone in the local already, see `compile_owned_into`, so what
                    // reaches here copies and the local drops its rest at scope end.
                    Op::Copy { dst, src }
                }
            } else {
                Op::Take { dst, src }
            };
        }
    }

    /// The `LoadUnit` stores nobody reads, the value of a statement position `if` or of a
    /// block that ends in a `;`. Only a register no other op writes qualifies, so it holds
    /// unit or nothing either way and the store can go without a trace.
    pub(super) fn dead_unit_loads(&self) -> Vec<bool> {
        let live = Liveness::of(self);
        let regs = usize::from(self.max_reg).max(1);
        let mut unit_only = vec![true; regs];
        for slot in unit_only.iter_mut().take(self.num_params) {
            *slot = false;
        }
        for (at, op) in self.code.iter().enumerate() {
            if matches!(op, Op::LoadUnit { .. }) {
                continue;
            }
            for &w in &live.writes[at] {
                if let Some(slot) = unit_only.get_mut(usize::from(w)) {
                    *slot = false;
                }
            }
        }
        self.code
            .iter()
            .enumerate()
            .map(|(at, op)| match op {
                Op::LoadUnit { dst } => {
                    unit_only.get(usize::from(*dst)).copied().unwrap_or(false)
                        && !live.live_out(at, *dst)
                }
                _ => false,
            })
            .collect()
    }

    /// A `Jump` to the op right after it, the end of a `then` branch with no `else` once the
    /// unit stores around it are gone.
    pub(super) fn dead_jumps(&self) -> Vec<bool> {
        self.code
            .iter()
            .enumerate()
            .map(|(at, op)| match op {
                Op::Jump { to } => usize::try_from(*to).is_ok_and(|to| to == at + 1),
                _ => false,
            })
            .collect()
    }
}