Skip to main content

luaur_code_gen/functions/
to_dot_cfg.rs

1extern crate alloc;
2
3use crate::functions::append::append;
4use crate::functions::append_blocks::append_blocks;
5use crate::functions::successors::successors;
6use crate::records::ir_function::IrFunction;
7use crate::records::ir_to_string_context::ir_to_string_context;
8use alloc::string::String;
9use core::ffi::c_void;
10
11pub fn to_dot_cfg(function: &IrFunction) -> String {
12    let mut result = String::new();
13
14    {
15        let mut ctx = ir_to_string_context {
16            result: &mut result,
17            blocks: &function.blocks,
18            constants: &function.constants,
19            cfg: &function.cfg,
20            vm_exit_info: &function.vm_exit_info,
21            proto: function.proto as *mut c_void,
22        };
23
24        ctx.result.push_str("digraph CFG {\n");
25        ctx.result.push_str("node[shape=record]\n");
26
27        append_blocks(
28            &mut ctx, function, /* include_inst */ false, /* include_in */ false,
29            /* include_out */ false, /* include_def */ true,
30        );
31
32        let cfg = ctx.cfg;
33
34        let mut i = 0usize;
35        while i < function.blocks.len() && i < cfg.successors_offsets.len() {
36            let succ = successors(cfg, i as u32);
37
38            for target in succ {
39                append(
40                    &mut ctx.result,
41                    format_args!("b{} -> b{};\n", i as u32, target),
42                );
43            }
44
45            i += 1;
46        }
47
48        ctx.result.push_str("}\n");
49    }
50
51    result
52}