kecc/ir/
visualize.rs

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
//! Visualize IR.

use std::collections::HashMap;

use crate::Translate;
use crate::ir::*;

#[derive(Default, Debug)]
pub struct Visualizer {
    /// First instruction in the function.
    function_first_instruction: HashMap<String, String>,

    /// First instruction in the block.
    block_first_instruction: HashMap<(String, BlockId), String>,
}

impl Translate<TranslationUnit> for Visualizer {
    type Target = String;
    type Error = ();

    fn translate(&mut self, source: &TranslationUnit) -> Result<Self::Target, Self::Error> {
        let mut subgraphs = Vec::new();

        // TODO: Add variables and structs information
        for (name, decl) in &source.decls {
            if let Declaration::Function {
                signature,
                definition: Some(definition),
            } = decl
            {
                let subgraph = self.translate_function(name, signature, definition)?;
                subgraphs.push(subgraph);
            }
        }

        let mut edges = Vec::new();

        // Add edges between subgraphs
        for (name, decl) in &source.decls {
            if let Declaration::Function {
                definition: Some(definition),
                ..
            } = decl
            {
                for (bid, block) in &definition.blocks {
                    for (iid, instruction) in block.instructions.iter().enumerate() {
                        if let Instruction::Call { callee, .. } = &instruction.inner {
                            let from = self.translate_instruction_node(name, *bid, iid);
                            let to = self.translate_callee(name, callee)?;

                            edges.push(format!("{from} -> {to};"));
                        }
                    }
                }
            }
        }

        let inner = [subgraphs, edges].concat().join("\n");

        Ok(format!("digraph G {{\n{inner}\n}}"))
    }
}

impl Visualizer {
    #[inline]
    fn get_function_first_instruction(&self, name: &str) -> String {
        self.function_first_instruction
            .get(name)
            .expect("failed to get first instruction from function")
            .clone()
    }

    #[inline]
    fn get_block_first_instruction(&self, name: &str, bid: BlockId) -> String {
        self.block_first_instruction
            .get(&(name.to_string(), bid))
            .expect("failed to get first instruction from block")
            .clone()
    }

    #[inline]
    fn translate_instruction_node(&self, name: &str, bid: BlockId, iid: usize) -> String {
        format!("\"{name}:{bid}:i{iid}\"")
    }

    #[inline]
    fn translate_block_exit_node(&self, name: &str, bid: BlockId) -> String {
        format!("\"{name}:{bid}:exit\"")
    }

    #[inline]
    fn translate_callee(&mut self, name: &str, callee: &Operand) -> Result<String, ()> {
        match callee {
            Operand::Constant(_constant @ Constant::GlobalVariable { name, .. }) => {
                Ok(self.get_function_first_instruction(name))
            }
            Operand::Register {
                rid: _rid @ RegisterId::Temp { bid, iid },
                ..
            } => Ok(self.translate_instruction_node(name, *bid, *iid)),
            _ => todo!("translate_callee: operand {:?}", callee),
        }
    }

    fn translate_function(
        &mut self,
        name: &str,
        signature: &FunctionSignature,
        definition: &FunctionDefinition,
    ) -> Result<String, ()> {
        let mut subgraphs = Vec::new();

        for (bid, block) in &definition.blocks {
            let subgraph = self.translate_block(name, bid, block)?;
            subgraphs.push(subgraph);
        }

        let _unused = self.function_first_instruction.insert(
            name.to_string(),
            self.get_block_first_instruction(name, definition.bid_init),
        );

        let params = signature
            .params
            .iter()
            .map(|p| p.to_string())
            .collect::<Vec<_>>()
            .join(", ");
        let label = format!("label=\"fun {} @{} ({})\";", signature.ret, name, params);

        let mut edges = Vec::new();

        // Add edges for block exit
        for (bid, block) in &definition.blocks {
            match &block.exit {
                BlockExit::Jump { arg } => {
                    edges.push(format!(
                        "{} -> {};",
                        self.translate_block_exit_node(name, *bid),
                        self.get_block_first_instruction(name, arg.bid)
                    ));
                }
                BlockExit::ConditionalJump {
                    arg_then, arg_else, ..
                } => {
                    edges.push(format!(
                        "{} -> {} [label=\"true\"];",
                        self.translate_block_exit_node(name, *bid),
                        self.get_block_first_instruction(name, arg_then.bid)
                    ));
                    edges.push(format!(
                        "{} -> {} [label=\"false\"];",
                        self.translate_block_exit_node(name, *bid),
                        self.get_block_first_instruction(name, arg_else.bid)
                    ));
                }
                BlockExit::Switch { default, cases, .. } => {
                    edges.push(format!(
                        "{} -> {} [label=\"default\"];",
                        self.translate_block_exit_node(name, *bid),
                        self.get_block_first_instruction(name, default.bid)
                    ));
                    for (constant, arg) in cases {
                        edges.push(format!(
                            "{} -> {} [label=\"{}\"];",
                            self.translate_block_exit_node(name, *bid),
                            self.get_block_first_instruction(name, arg.bid),
                            constant,
                        ));
                    }
                }
                _ => {}
            }
        }

        // TODO: Add init information (bid_init, allocations)
        let inner = [subgraphs, vec![label], edges].concat().join("\n");

        Ok(format!("subgraph \"cluster.{name}\" {{\n{inner}\n}}"))
    }

    fn translate_block(&mut self, name: &str, bid: &BlockId, block: &Block) -> Result<String, ()> {
        let mut header = Vec::new();
        header.push("style=filled;".to_string());
        header.push("color=lightgrey;".to_string());
        header.push("node [shape=record];".to_string());
        header.push(format!("label=\"{bid}\";"));

        let mut nodes = Vec::new();

        // Add nodes for instructions
        for (iid, instruction) in block.instructions.iter().enumerate() {
            nodes.push(format!(
                "{} [label=\"{}\"]",
                self.translate_instruction_node(name, *bid, iid),
                instruction
            ));
        }

        // Add node for block exit
        nodes.push(format!(
            "{} [label=\"{}\"]",
            self.translate_block_exit_node(name, *bid),
            block.exit
        ));

        let edges = (0..block.instructions.len())
            .map(|iid| self.translate_instruction_node(name, *bid, iid))
            .chain([self.translate_block_exit_node(name, *bid)])
            .collect::<Vec<String>>()
            .join(" -> ");

        let first_instruction = if block.instructions.is_empty() {
            self.translate_block_exit_node(name, *bid)
        } else {
            self.translate_instruction_node(name, *bid, 0)
        };
        let _unused = self
            .block_first_instruction
            .insert((name.to_string(), *bid), first_instruction);

        let inner = [header, nodes, vec![edges]].concat().join("\n");

        Ok(format!("subgraph \"cluster.{name}.{bid}\" {{\n{inner}\n}}"))
    }
}