Skip to main content

luaur_code_gen/functions/
append_blocks.rs

1use crate::enums::ir_block_kind::IrBlockKind;
2use crate::functions::append::append;
3use crate::functions::append_label_regset::append_label_regset;
4use crate::functions::is_pseudo::is_pseudo;
5use crate::functions::to_string_ir_dump::to_string as to_string_inst;
6use crate::functions::to_string_ir_dump_alt_c::to_string_ir_to_string_context_ir_block_u32 as to_string_block;
7use crate::records::ir_function::IrFunction;
8use crate::records::ir_to_string_context::IrToStringContext;
9
10pub fn append_blocks(
11    ctx: &mut IrToStringContext,
12    function: &IrFunction,
13    include_inst: bool,
14    include_in: bool,
15    include_out: bool,
16    include_def: bool,
17) {
18    // Launder the shared CfgInfo reference so the register-set reads don't alias
19    // the &mut borrow of ctx.
20    let cfg = ctx.cfg;
21
22    for i in 0..function.blocks.len() {
23        let block = function.blocks[i];
24
25        append(&mut ctx.result, format_args!("b{} [", i as u32));
26
27        if block.kind == IrBlockKind::Fallback {
28            append(
29                &mut ctx.result,
30                format_args!("style=filled;fillcolor=salmon;"),
31            );
32        } else if block.kind == IrBlockKind::Bytecode {
33            append(
34                &mut ctx.result,
35                format_args!("style=filled;fillcolor=palegreen;"),
36            );
37        }
38
39        ctx.result.push_str("label=\"{");
40        to_string_block(ctx, &block, i as u32);
41
42        if include_in {
43            append_label_regset(ctx, &cfg.r#in, i, "in");
44        }
45
46        if include_inst && block.start != !0u32 {
47            let mut inst_idx = block.start;
48            while inst_idx <= block.finish {
49                let inst = &function.instructions[inst_idx as usize];
50
51                // Skip pseudo instructions unless they are still referenced
52                if is_pseudo(inst.cmd) && inst.use_count == 0 {
53                    inst_idx += 1;
54                    continue;
55                }
56
57                ctx.result.push_str("|");
58                to_string_inst(ctx, inst, inst_idx);
59
60                inst_idx += 1;
61            }
62        }
63
64        if include_def {
65            append_label_regset(ctx, &cfg.def, i, "def");
66        }
67
68        if include_out {
69            append_label_regset(ctx, &cfg.out, i, "out");
70        }
71
72        ctx.result.push_str("}\"];\n");
73    }
74}