neyuki 0.6.3

A small Lua-like scripting language implemented in Rust
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
//! Inlines small straight-line leaf functions.
//
// Soundness rules (all enforced below; anything else stays a call):
// - the body holds only frame-independent ops plus one trailing Return of
//   exactly one value: no calls, loops, branches, labels, varargs,
//   upvalues, spreads, or nested functions
// - the call site passes exactly `num_params` arguments and takes exactly
//   one value with a fixed `retc` (never MULTRET, which flows through the
//   stack top and cannot be spliced)
// - the callee resolves to a single Closure definition: directly, through
//   single-definition Move aliases, or through a single SetGlobal/GetGlobal
//   pair for one name

use std::collections::{HashMap, HashSet};

use crate::compiler::ir::block::{IrFunction, IrModule, UpvalSource};
use crate::compiler::ir::inst::IrInst;
use crate::compiler::ir::types::{IrLabel, IrVar};

const MAX_INLINE_INSTRUCTIONS: usize = 32;

/// Frame-independent ops: relocating them into the caller preserves meaning.
fn is_inlinable_body_inst(inst: &IrInst) -> bool {
    matches!(
        inst,
        IrInst::Move { .. }
            | IrInst::BinOp { .. }
            | IrInst::UnOp { .. }
            | IrInst::LoadConst { .. }
            | IrInst::LoadNil { .. }
            | IrInst::NewTable { .. }
            | IrInst::GetTable { .. }
            | IrInst::GetImport { .. }
            | IrInst::SetTable { .. }
            | IrInst::GetGlobal { .. }
            | IrInst::SetGlobal { .. }
            | IrInst::AppendArray { .. }
            | IrInst::GetUpval { .. }
            | IrInst::SetUpval { .. }
            | IrInst::Label(_)
            | IrInst::Jump(_)
            | IrInst::JumpIfFalse { .. }
    )
}

fn build_candidates(func: &IrFunction) -> HashMap<usize, IrFunction> {
    func.protos
        .iter()
        .enumerate()
        .filter_map(|(idx, f)| {
            if is_inline_candidate(f) {
                Some((idx, f.clone()))
            } else {
                None
            }
        })
        .collect()
}

fn inline_func_tree(func: &mut IrFunction) -> bool {
    let mut changed = false;
    for proto in &mut func.protos {
        if inline_func_tree(proto) {
            changed = true;
        }
    }
    let candidates = build_candidates(func);
    if !candidates.is_empty() && inline_in_func(func, &candidates) {
        changed = true;
    }
    changed
}

pub fn inline_functions(module: &mut IrModule) -> bool {
    let mut any_changed = false;
    const MAX_INLINE_ROUNDS: usize = 2;

    for _ in 0..MAX_INLINE_ROUNDS {
        let mut round_changed = false;
        if inline_func_tree(&mut module.main) {
            round_changed = true;
            any_changed = true;
        }

        if !round_changed {
            break;
        }
    }

    any_changed
}

fn inline_in_func(func: &mut IrFunction, candidates: &HashMap<usize, IrFunction>) -> bool {
    let mut def_count: HashMap<IrVar, usize> = HashMap::new();
    let mut move_alias: HashMap<IrVar, IrVar> = HashMap::new();
    let mut closure_map: HashMap<IrVar, usize> = HashMap::new();
    let mut getglobal_name: HashMap<IrVar, String> = HashMap::new();
    let mut setglobal_srcs: HashMap<String, Vec<IrVar>> = HashMap::new();
    for inst in &func.instructions {
        for d in inst.def_vars() {
            *def_count.entry(d).or_insert(0) += 1;
        }
        match inst {
            IrInst::Closure { dst, proto_idx, .. } => {
                closure_map.insert(*dst, *proto_idx as usize);
            }
            IrInst::Move { dst, src } => {
                move_alias.insert(*dst, *src);
            }
            IrInst::GetGlobal { dst, name } => {
                getglobal_name.insert(*dst, name.clone());
            }
            IrInst::SetGlobal { name, src } => {
                setglobal_srcs.entry(name.clone()).or_default().push(*src);
            }
            _ => {}
        }
    }

    let mut max_var_id = 0u32;
    let mut max_label_id = 0usize;
    for inst in &func.instructions {
        if let Some(d) = inst.def_var() {
            max_var_id = max_var_id.max(d.0);
        }
        for u in inst.use_vars() {
            max_var_id = max_var_id.max(u.0);
        }
        for l in inst.jump_targets() {
            max_label_id = max_label_id.max(l.0);
        }
        if let IrInst::Label(l) = inst {
            max_label_id = max_label_id.max(l.0);
        }
    }

    let mut changed = false;
    let mut new_instructions = Vec::new();

    for inst in func.instructions.drain(..) {
        let inlined = match &inst {
            IrInst::Call {
                dsts,
                callee,
                args,
                retc,
            } if dsts.len() == 1 && *retc == 1 => resolve_callee(
                *callee,
                &closure_map,
                &move_alias,
                &getglobal_name,
                &setglobal_srcs,
                &def_count,
            )
            .and_then(|idx| candidates.get(&idx))
            .filter(|f| f.num_params as usize == args.len())
            .map(|target| (target, dsts[0], args.clone())),
            _ => None,
        };

        if let Some((target_func, out_dst, call_args)) = inlined
            && splice_call(
                &mut new_instructions,
                target_func,
                out_dst,
                &call_args,
                &mut max_var_id,
                &mut max_label_id,
            )
        {
            changed = true;
        } else {
            new_instructions.push(inst);
        }
    }

    func.instructions = new_instructions;
    changed
}

/// Follows a variable to the Closure definition it must hold at runtime:
/// directly, through single-definition Move aliases, or through a
/// GetGlobal fed by a single SetGlobal of the same name. `visited` guards
/// against alias cycles. Returns the child proto index on success.
fn resolve_callee(
    mut var: IrVar,
    closure_map: &HashMap<IrVar, usize>,
    move_alias: &HashMap<IrVar, IrVar>,
    getglobal_name: &HashMap<IrVar, String>,
    setglobal_srcs: &HashMap<String, Vec<IrVar>>,
    def_count: &HashMap<IrVar, usize>,
) -> Option<usize> {
    let mut visited: HashSet<IrVar> = HashSet::new();
    loop {
        if let Some(&proto_idx) = closure_map.get(&var) {
            return Some(proto_idx);
        }
        if !visited.insert(var) {
            return None;
        }
        // Single-definition Move alias.
        if def_count.get(&var).copied().unwrap_or(0) == 1
            && let Some(&src) = move_alias.get(&var)
        {
            var = src;
            continue;
        }
        // GetGlobal fed by exactly one SetGlobal of the same name.
        if def_count.get(&var).copied().unwrap_or(0) == 1
            && let Some(name) = getglobal_name.get(&var)
            && let Some(srcs) = setglobal_srcs.get(name)
            && let [src] = srcs.as_slice()
        {
            var = *src;
            continue;
        }
        return None;
    }
}

/// Splices one inlined call into `out`: parameters map to the call's
/// arguments, the single returned value moves to `out_dst`. Returns false
/// if any body instruction cannot be remapped, in which case the caller
/// keeps the original call and `out` is left with a partial splice that
/// must be discarded.
fn splice_call(
    out: &mut Vec<IrInst>,
    target_func: &IrFunction,
    out_dst: IrVar,
    call_args: &[IrVar],
    max_var_id: &mut u32,
    max_label_id: &mut usize,
) -> bool {
    let checkpoint = out.len();
    *max_label_id += 1;
    let exit_label = IrLabel(*max_label_id);

    let mut label_map: HashMap<IrLabel, IrLabel> = HashMap::new();
    for inst in &target_func.instructions {
        if let IrInst::Label(l) = inst {
            *max_label_id += 1;
            label_map.insert(*l, IrLabel(*max_label_id));
        }
    }

    let mut var_map: HashMap<IrVar, IrVar> = HashMap::new();
    for (i, &arg) in call_args.iter().enumerate() {
        var_map.insert(IrVar(i as u32), arg);
    }

    // The callee's line and local markers would misattribute the rest of
    // the caller's statement, so they are left behind.
    for body_inst in target_func
        .instructions
        .iter()
        .filter(|inst| !inst.is_debug_marker())
    {
        match body_inst {
            IrInst::Return(ret_vars) => {
                match ret_vars.first() {
                    Some(&ret_v) => {
                        let Some(&resolved_ret) = var_map.get(&ret_v) else {
                            out.truncate(checkpoint);
                            return false;
                        };
                        out.push(IrInst::Move {
                            dst: out_dst,
                            src: resolved_ret,
                        });
                    }
                    None => {
                        out.push(IrInst::LoadNil { dst: out_dst });
                    }
                }
                out.push(IrInst::Jump(exit_label));
            }
            IrInst::Label(l) => {
                let mapped = label_map.get(l).copied().unwrap_or(*l);
                out.push(IrInst::Label(mapped));
            }
            IrInst::Jump(target) => {
                let Some(&mapped) = label_map.get(target) else {
                    out.truncate(checkpoint);
                    return false;
                };
                out.push(IrInst::Jump(mapped));
            }
            IrInst::JumpIfFalse { cond, target } => {
                let mut c = *cond;
                if !use_mapped(&mut c, &var_map) {
                    out.truncate(checkpoint);
                    return false;
                }
                let Some(&mapped) = label_map.get(target) else {
                    out.truncate(checkpoint);
                    return false;
                };
                out.push(IrInst::JumpIfFalse {
                    cond: c,
                    target: mapped,
                });
            }
            IrInst::GetUpval { dst, index } => {
                if let Some(UpvalSource::ParentLocal(parent_var)) =
                    target_func.upvalue_vars.get(*index as usize)
                {
                    *max_var_id += 1;
                    let fresh = IrVar(*max_var_id);
                    var_map.insert(*dst, fresh);
                    out.push(IrInst::Move {
                        dst: fresh,
                        src: *parent_var,
                    });
                } else {
                    out.truncate(checkpoint);
                    return false;
                }
            }
            IrInst::SetUpval { src, index } => {
                if let Some(UpvalSource::ParentLocal(parent_var)) =
                    target_func.upvalue_vars.get(*index as usize)
                {
                    let mut s = *src;
                    if !use_mapped(&mut s, &var_map) {
                        out.truncate(checkpoint);
                        return false;
                    }
                    out.push(IrInst::Move {
                        dst: *parent_var,
                        src: s,
                    });
                } else {
                    out.truncate(checkpoint);
                    return false;
                }
            }
            _ => {
                let mut cloned = body_inst.clone();
                if !remap_inline_vars(&mut cloned, &mut var_map, max_var_id) {
                    out.truncate(checkpoint);
                    return false;
                }
                out.push(cloned);
            }
        }
    }
    out.push(IrInst::Label(exit_label));
    true
}

fn is_inline_candidate(func: &IrFunction) -> bool {
    let body: Vec<&IrInst> = func
        .instructions
        .iter()
        .filter(|inst| !inst.is_debug_marker())
        .collect();
    if body.len() > MAX_INLINE_INSTRUCTIONS {
        return false;
    }
    if !crate::compiler::cost_model::CostModel::default().should_inline_ir(func) {
        return false;
    }
    if func.is_vararg || !func.protos.is_empty() {
        return false;
    }
    for uv in &func.upvalue_vars {
        if !matches!(uv, UpvalSource::ParentLocal(_)) {
            return false;
        }
    }
    for inst in &body {
        match inst {
            IrInst::Return(_) => {}
            _ if is_inlinable_body_inst(inst) => {}
            _ => return false,
        }
    }
    if !body.iter().any(|inst| matches!(inst, IrInst::Return(_))) {
        return false;
    }

    // Reject backward jumps (loops): linear splice cannot reconstruct loop SSA.
    let mut label_indices = HashMap::new();
    for (idx, inst) in body.iter().enumerate() {
        if let IrInst::Label(l) = inst {
            label_indices.insert(*l, idx);
        }
    }
    for (idx, inst) in body.iter().enumerate() {
        for target in inst.jump_targets() {
            if let Some(&target_idx) = label_indices.get(&target) {
                if target_idx <= idx {
                    return false;
                }
            } else {
                return false;
            }
        }
    }

    // If branches/labels exist, mutable variables (assigned > 1 time) cannot be reconciled
    // across divergent paths by linear variable remapping without phi-nodes.
    let has_branches = body.iter().any(|inst| {
        matches!(
            inst,
            IrInst::Jump(_) | IrInst::JumpIfFalse { .. } | IrInst::Label(_)
        )
    });
    if has_branches {
        let mut def_counts = HashMap::new();
        for i in 0..func.num_params {
            *def_counts.entry(IrVar(i as u32)).or_insert(0) += 1;
        }
        for inst in &body {
            for d in inst.def_vars() {
                *def_counts.entry(d).or_insert(0) += 1;
            }
        }
        if def_counts.values().any(|&c| c > 1) {
            return false;
        }
    }

    true
}

/// Maps a use to its replacement; bails on uses that are neither parameters
/// nor remapped definitions (the body would not be self-contained).
fn use_mapped(v: &mut IrVar, var_map: &HashMap<IrVar, IrVar>) -> bool {
    match var_map.get(v).copied() {
        Some(mapped) => {
            *v = mapped;
            true
        }
        None => false,
    }
}

/// Mints a fresh variable for a definition so spliced code cannot collide
/// with caller variables.
fn fresh_def(v: IrVar, var_map: &mut HashMap<IrVar, IrVar>, max_id: &mut u32) -> IrVar {
    *max_id += 1;
    let fresh = IrVar(*max_id);
    var_map.insert(v, fresh);
    fresh
}

fn remap_inline_vars(
    inst: &mut IrInst,
    var_map: &mut HashMap<IrVar, IrVar>,
    max_id: &mut u32,
) -> bool {
    match inst {
        IrInst::Move { dst, src } => {
            if !use_mapped(src, var_map) {
                return false;
            }
            *dst = fresh_def(*dst, var_map, max_id);
        }
        IrInst::BinOp { dst, lhs, rhs, .. } => {
            if !use_mapped(lhs, var_map) || !use_mapped(rhs, var_map) {
                return false;
            }
            *dst = fresh_def(*dst, var_map, max_id);
        }
        IrInst::UnOp { dst, src, .. } => {
            if !use_mapped(src, var_map) {
                return false;
            }
            *dst = fresh_def(*dst, var_map, max_id);
        }
        IrInst::LoadConst { dst, .. } | IrInst::LoadNil { dst } => {
            *dst = fresh_def(*dst, var_map, max_id);
        }
        IrInst::NewTable { dst, .. } => {
            *dst = fresh_def(*dst, var_map, max_id);
        }
        IrInst::GetTable { dst, table, key } => {
            if !use_mapped(table, var_map) || !use_mapped(key, var_map) {
                return false;
            }
            *dst = fresh_def(*dst, var_map, max_id);
        }
        IrInst::SetTable { table, key, val } => {
            if !use_mapped(table, var_map) || !use_mapped(key, var_map) || !use_mapped(val, var_map)
            {
                return false;
            }
        }
        IrInst::GetGlobal { dst, .. } => {
            *dst = fresh_def(*dst, var_map, max_id);
        }
        IrInst::SetGlobal { src, .. } => {
            if !use_mapped(src, var_map) {
                return false;
            }
        }
        IrInst::AppendArray { table, src } => {
            if !use_mapped(table, var_map) || !use_mapped(src, var_map) {
                return false;
            }
        }
        _ => return false,
    }
    true
}