neyuki 0.1.1

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
// Static Single Assignment (SSA) form construction and destruction.

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

use crate::compiler::ir::block::ControlFlowGraph;
use crate::compiler::ir::dom::DominatorTree;
use crate::compiler::ir::inst::IrInst;
use crate::compiler::ir::types::{IrLabel, IrVar};

pub struct SsaContext {
    var_counter: u32,
}

impl SsaContext {
    pub fn new(max_existing_var: u32) -> Self {
        Self {
            var_counter: max_existing_var + 1,
        }
    }

    pub fn fresh_var(&mut self) -> IrVar {
        let v = IrVar(self.var_counter);
        self.var_counter += 1;
        v
    }
}

pub fn construct_ssa(cfg: &mut ControlFlowGraph, dom: &DominatorTree) {
    // 1. Find all variables defined in CFG and their defining blocks
    let mut max_var_id = 0u32;
    let mut def_blocks: HashMap<IrVar, HashSet<IrLabel>> = HashMap::new();

    for block in &cfg.blocks {
        for inst in &block.instructions {
            if let Some(d) = inst.def_var() {
                max_var_id = max_var_id.max(d.0);
                def_blocks.entry(d).or_default().insert(block.label);
            }
            for u in inst.use_vars() {
                max_var_id = max_var_id.max(u.0);
            }
        }
    }

    let mut ctx = SsaContext::new(max_var_id);

    // 2. Insert Phi nodes at dominance frontiers
    for (var, blocks) in def_blocks {
        let mut worklist: Vec<IrLabel> = blocks.iter().copied().collect();
        let mut has_phi: HashSet<IrLabel> = HashSet::new();

        while let Some(blk) = worklist.pop() {
            if let Some(frontier) = dom.frontiers.get(&blk) {
                for &f_label in frontier {
                    if has_phi.insert(f_label) {
                        if let Some(target_block) = cfg.find_block_mut(f_label) {
                            let phi = IrInst::Phi {
                                dst: var,
                                incoming: Vec::new(),
                            };
                            target_block.instructions.insert(0, phi);
                        }
                        worklist.push(f_label);
                    }
                }
            }
        }
    }

    // 3. Rename variables via dominator tree traversal
    let mut var_stacks: HashMap<IrVar, Vec<IrVar>> = HashMap::new();
    rename_block(cfg.entry_label, cfg, dom, &mut var_stacks, &mut ctx);
}

fn rename_block(
    block_label: IrLabel,
    cfg: &mut ControlFlowGraph,
    dom: &DominatorTree,
    stacks: &mut HashMap<IrVar, Vec<IrVar>>,
    ctx: &mut SsaContext,
) {
    let mut defs_in_block: Vec<IrVar> = Vec::new();

    // Process instructions in this block
    if let Some(block) = cfg.find_block_mut(block_label) {
        for inst in &mut block.instructions {
            match inst {
                IrInst::Phi { dst, .. } => {
                    let old_var = *dst;
                    let new_var = ctx.fresh_var();
                    *dst = new_var;
                    stacks.entry(old_var).or_default().push(new_var);
                    defs_in_block.push(old_var);
                }
                _ => {
                    // Rename uses
                    rename_inst_uses(inst, stacks);

                    // Rename def
                    if let Some(old_dst) = inst.def_var() {
                        let new_dst = ctx.fresh_var();
                        set_inst_def(inst, new_dst);
                        stacks.entry(old_dst).or_default().push(new_dst);
                        defs_in_block.push(old_dst);
                    }
                }
            }
        }
    }

    // Fill Phi nodes in successors
    let succs = cfg
        .find_block(block_label)
        .map(|b| b.successors.clone())
        .unwrap_or_default();

    for succ_label in succs {
        if let Some(succ_block) = cfg.find_block_mut(succ_label) {
            for inst in &mut succ_block.instructions {
                if let IrInst::Phi { incoming, .. } = inst {
                    // Look up current value for the phi's variable
                    if let Some(&top) = defs_in_block
                        .last()
                        .and_then(|last_def| stacks.get(last_def))
                        .and_then(|stack| stack.last())
                    {
                        incoming.push((block_label, top));
                    }
                }
            }
        }
    }

    // Traverse dominator tree children
    if let Some(children) = dom.children.get(&block_label).cloned() {
        for child in children {
            rename_block(child, cfg, dom, stacks, ctx);
        }
    }

    // Pop definitions made in this block
    for var in defs_in_block {
        if let Some(stack) = stacks.get_mut(&var) {
            stack.pop();
        }
    }
}

fn rename_inst_uses(inst: &mut IrInst, stacks: &HashMap<IrVar, Vec<IrVar>>) {
    let rename = |v: &mut IrVar| {
        if let Some(&top) = stacks.get(v).and_then(|s| s.last()) {
            *v = top;
        }
    };

    match inst {
        IrInst::Move { src, .. } | IrInst::UnOp { src, .. } | IrInst::AppendArray { src, .. } => {
            rename(src);
        }
        IrInst::BinOp { lhs, rhs, .. } => {
            rename(lhs);
            rename(rhs);
        }
        IrInst::GetTable { table, key, .. } => {
            rename(table);
            rename(key);
        }
        IrInst::SetTable { table, key, val } => {
            rename(table);
            rename(key);
            rename(val);
        }
        IrInst::SetGlobal { src, .. } | IrInst::SetUpval { src, .. } => {
            rename(src);
        }
        IrInst::Call { callee, args, .. } => {
            rename(callee);
            for arg in args {
                rename(arg);
            }
        }
        IrInst::Return(vars) => {
            for v in vars {
                rename(v);
            }
        }
        IrInst::JumpIfFalse { cond, .. } => {
            rename(cond);
        }
        IrInst::ForPrep {
            base, limit, step, ..
        } => {
            rename(base);
            rename(limit);
            rename(step);
        }
        IrInst::ForLoop { base, .. } | IrInst::TForLoop { base, .. } => {
            rename(base);
        }
        IrInst::TForCall {
            base, state, ctrl, ..
        } => {
            rename(base);
            rename(state);
            rename(ctrl);
        }
        _ => {}
    }
}

fn set_inst_def(inst: &mut IrInst, new_def: IrVar) {
    match inst {
        IrInst::LoadConst { dst, .. }
        | IrInst::LoadNil { dst }
        | IrInst::Move { dst, .. }
        | IrInst::BinOp { dst, .. }
        | IrInst::UnOp { dst, .. }
        | IrInst::NewTable { dst }
        | IrInst::GetTable { dst, .. }
        | IrInst::GetGlobal { dst, .. }
        | IrInst::GetUpval { dst, .. }
        | IrInst::Closure { dst, .. }
        | IrInst::Vararg { dst, .. }
        | IrInst::Phi { dst, .. } => {
            *dst = new_def;
        }
        IrInst::Call { dsts, .. } => {
            // Only the first result is renamed; SSA does not model the rest.
            if let Some(first) = dsts.first_mut() {
                *first = new_def;
            }
        }
        _ => {}
    }
}

// Lowers SSA form back to conventional 3-Address Code by converting Phi nodes to parallel copies
pub fn destruct_ssa(cfg: &mut ControlFlowGraph) {
    let mut copies: Vec<(IrLabel, IrInst)> = Vec::new();

    for block in &mut cfg.blocks {
        let mut non_phi = Vec::new();

        for inst in block.instructions.drain(..) {
            if let IrInst::Phi { dst, incoming } = inst {
                for (pred_label, src_var) in incoming {
                    copies.push((pred_label, IrInst::Move { dst, src: src_var }));
                }
            } else {
                non_phi.push(inst);
            }
        }

        block.instructions = non_phi;
    }

    // Insert copies into predecessor blocks before their terminators
    for (pred_label, copy_inst) in copies {
        if let Some(pred_block) = cfg.find_block_mut(pred_label) {
            let term_pos = pred_block
                .instructions
                .iter()
                .position(|i| i.is_terminator());

            if let Some(pos) = term_pos {
                pred_block.instructions.insert(pos, copy_inst);
            } else {
                pred_block.instructions.push(copy_inst);
            }
        }
    }
}

use crate::compiler::ir::types::IrBinaryOp;
use crate::compiler::ir::types::IrConstant;

/// Sparse Constant & Copy Propagation pass directly operating on SSA form.
/// Folds constant Phi nodes, resolves compile-time binary operations, and propagates values.
pub fn ssa_constant_propagation(cfg: &mut ControlFlowGraph) -> bool {
    let mut const_values: HashMap<IrVar, IrConstant> = HashMap::new();
    let mut copy_values: HashMap<IrVar, IrVar> = HashMap::new();
    let mut changed = false;

    let mut passes = 0;
    const MAX_PASSES: usize = 16;

    while passes < MAX_PASSES {
        passes += 1;
        let mut pass_changed = false;

        for block in &mut cfg.blocks {
            for inst in &mut block.instructions {
                match inst {
                    IrInst::LoadConst { dst, val } => {
                        if const_values.insert(*dst, val.clone()).is_none() {
                            pass_changed = true;
                        }
                    }
                    IrInst::Move { dst, src } => {
                        let resolved_src = copy_values.get(src).copied().unwrap_or(*src);
                        if let Some(c) = const_values.get(&resolved_src).cloned() {
                            if const_values.insert(*dst, c).is_none() {
                                pass_changed = true;
                            }
                        } else if copy_values.insert(*dst, resolved_src).is_none() {
                            pass_changed = true;
                        }
                    }
                    IrInst::Phi { dst, incoming } => {
                        // Check if all incoming values are identical constants or the dst itself
                        let mut common_const: Option<IrConstant> = None;
                        let mut all_same_const = !incoming.is_empty();

                        for (_, src_var) in incoming.iter() {
                            if *src_var == *dst {
                                continue;
                            }
                            let resolved = copy_values.get(src_var).copied().unwrap_or(*src_var);
                            if let Some(c) = const_values.get(&resolved) {
                                if let Some(ref prev) = common_const {
                                    if prev != c {
                                        all_same_const = false;
                                        break;
                                    }
                                } else {
                                    common_const = Some(c.clone());
                                }
                            } else {
                                all_same_const = false;
                                break;
                            }
                        }

                        if let (true, Some(c)) = (all_same_const, common_const) {
                            if const_values.insert(*dst, c.clone()).is_none() {
                                pass_changed = true;
                            }
                            *inst = IrInst::LoadConst { dst: *dst, val: c };
                            continue;
                        }

                        // Check if all incoming values are the exact same variable
                        let mut common_var: Option<IrVar> = None;
                        let mut all_same_var = !incoming.is_empty();
                        for (_, src_var) in incoming.iter() {
                            if *src_var == *dst {
                                continue;
                            }
                            let resolved = copy_values.get(src_var).copied().unwrap_or(*src_var);
                            if let Some(prev) = common_var {
                                if prev != resolved {
                                    all_same_var = false;
                                    break;
                                }
                            } else {
                                common_var = Some(resolved);
                            }
                        }

                        if let (true, Some(src)) = (all_same_var, common_var) {
                            copy_values.insert(*dst, src);
                            *inst = IrInst::Move { dst: *dst, src };
                            pass_changed = true;
                        }
                    }
                    IrInst::BinOp { dst, op, lhs, rhs } => {
                        let c_lhs = const_values.get(lhs);
                        let c_rhs = const_values.get(rhs);

                        if let (Some(IrConstant::Int(a)), Some(IrConstant::Int(b))) = (c_lhs, c_rhs)
                        {
                            let folded = match op {
                                IrBinaryOp::Add => Some(IrConstant::Int(a + b)),
                                IrBinaryOp::Sub => Some(IrConstant::Int(a - b)),
                                IrBinaryOp::Mul => Some(IrConstant::Int(a * b)),
                                IrBinaryOp::Eq => Some(IrConstant::Bool(a == b)),
                                IrBinaryOp::Ne => Some(IrConstant::Bool(a != b)),
                                IrBinaryOp::Lt => Some(IrConstant::Bool(a < b)),
                                IrBinaryOp::Le => Some(IrConstant::Bool(a <= b)),
                                IrBinaryOp::Gt => Some(IrConstant::Bool(a > b)),
                                IrBinaryOp::Ge => Some(IrConstant::Bool(a >= b)),
                                _ => None,
                            };
                            if let Some(val) = folded {
                                const_values.insert(*dst, val.clone());
                                *inst = IrInst::LoadConst { dst: *dst, val };
                                pass_changed = true;
                            }
                        }
                    }
                    _ => {}
                }
            }
        }

        if !pass_changed {
            break;
        }
        changed = true;
    }

    changed
}