Skip to main content

luaur_code_gen/functions/
to_dot.rs

1extern crate alloc;
2
3use crate::enums::ir_block_kind::IrBlockKind;
4use crate::enums::ir_op_kind::IrOpKind;
5use crate::functions::append::append;
6use crate::functions::append_blocks::append_blocks;
7use crate::records::ir_function::IrFunction;
8use crate::records::ir_to_string_context::ir_to_string_context;
9use alloc::string::String;
10use core::ffi::c_void;
11
12pub fn to_dot(function: &IrFunction, include_inst: bool) -> String {
13    let mut result = String::new();
14
15    {
16        let mut ctx = ir_to_string_context {
17            result: &mut result,
18            blocks: &function.blocks,
19            constants: &function.constants,
20            cfg: &function.cfg,
21            vm_exit_info: &function.vm_exit_info,
22            proto: function.proto as *mut c_void,
23        };
24
25        ctx.result.push_str("digraph CFG {\n");
26        ctx.result.push_str("node[shape=record]\n");
27
28        append_blocks(
29            &mut ctx,
30            function,
31            include_inst,
32            /* include_in */ true,
33            /* include_out */ true,
34            /* include_def */ true,
35        );
36
37        for i in 0..function.blocks.len() {
38            let block = function.blocks[i];
39
40            if block.start == !0u32 {
41                continue;
42            }
43
44            let mut inst_idx = block.start;
45            while inst_idx != !0u32 && inst_idx <= block.finish {
46                let inst = &function.instructions[inst_idx as usize];
47
48                for op in inst.ops.as_slice() {
49                    if op.kind() == IrOpKind::Block {
50                        if function.blocks[op.index() as usize].kind != IrBlockKind::Fallback {
51                            append(
52                                &mut ctx.result,
53                                format_args!("b{} -> b{} [weight=10];\n", i as u32, op.index()),
54                            );
55                        } else {
56                            append(
57                                &mut ctx.result,
58                                format_args!("b{} -> b{};\n", i as u32, op.index()),
59                            );
60                        }
61                    }
62                }
63
64                inst_idx += 1;
65            }
66        }
67
68        ctx.result.push_str("}\n");
69    }
70
71    result
72}