gbf_core/decompiler/
region.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
227
228
229
230
231
232
233
#![deny(missing_docs)]

use crate::cfg_dot::RenderableNode;
use crate::decompiler::ast::visitors::emit_context::{EmitContextBuilder, EmitVerbosity};
use crate::decompiler::ast::visitors::emitter::Gs2Emitter;
use crate::decompiler::ast::visitors::AstVisitor;
use crate::decompiler::ast::AstKind;
use crate::utils::GBF_YELLOW;
use serde::{Deserialize, Serialize};
use std::fmt::Write;
use std::slice::Iter;

/// Represents the type of control-flow region.
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize, Copy)]
pub enum RegionType {
    /// Simply moves on to the next region without control flow
    Linear,
    /// Control flow construct (e.g. for, while, if, switch, etc.)
    ControlFlow,
    /// A tail (e.g, return)
    Tail,
}

/// Describes a region
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize, Copy)]
pub struct RegionId {
    /// The index of the region
    pub index: usize,
    /// The region type
    pub region_type: RegionType,
}

impl RegionId {
    /// Create a new `BasicBlockId`.
    ///
    /// # Arguments
    /// - `index`: The index of the basic block in the function.
    ///
    /// # Returns
    /// - A new `BasicBlockId` instance.
    ///
    /// Example
    /// ```
    /// use gbf_core::decompiler::region::RegionId;
    /// use gbf_core::decompiler::region::RegionType;
    ///
    /// let block = RegionId::new(0, RegionType::Linear);
    /// ```
    pub fn new(index: usize, region_type: RegionType) -> Self {
        Self { index, region_type }
    }
}

/// Represents a region in the control-flow graph.
#[derive(Debug, Clone)]
pub struct Region {
    id: RegionId,
    nodes: Vec<AstKind>,
}

impl Region {
    /// Creates a new region with the specified type and initializes with no statements.
    ///
    /// # Arguments
    /// * `id` - The id of the region.
    pub fn new(id: RegionId) -> Self {
        Self {
            id,
            nodes: Vec::new(),
        }
    }

    /// Returns the type of the region.
    pub fn region_type(&self) -> &RegionType {
        &self.id.region_type
    }

    /// Adds a statement to the region.
    ///
    /// # Arguments
    ///
    /// * `node` - The AST node to add.
    pub fn push_node(&mut self, node: AstKind) {
        self.nodes.push(node);
    }

    /// Returns an iterator over the statements in the region.
    pub fn iter_statements(&self) -> Iter<AstKind> {
        self.nodes.iter()
    }
}

// === Other Implementations ===

/// Allows iterating over the statements in a region.
impl<'a> IntoIterator for &'a Region {
    type Item = &'a AstKind;
    type IntoIter = Iter<'a, AstKind>;

    fn into_iter(self) -> Self::IntoIter {
        self.nodes.iter()
    }
}

/// Allows iterating over the statements in a region (mutable).
impl<'a> IntoIterator for &'a mut Region {
    type Item = &'a mut AstKind;
    type IntoIter = std::slice::IterMut<'a, AstKind>;

    fn into_iter(self) -> Self::IntoIter {
        self.nodes.iter_mut()
    }
}

impl RenderableNode for Region {
    /// Render the region's node representation for Graphviz with customizable padding.
    ///
    /// # Arguments
    /// * `padding` - The number of spaces to use for indentation.
    ///
    /// # Return
    /// The rendered node
    fn render_node(&self, padding: usize) -> String {
        let mut label = String::new();
        let indent = " ".repeat(padding);

        // Start the HTML-like table for Graphviz.
        writeln!(
            &mut label,
            r#"{indent}<TABLE BORDER="0" CELLBORDER="0" CELLSPACING="0" CELLPADDING="0">"#,
            indent = indent
        )
        .unwrap();

        // Render each statement as a table row with indentation.
        for node in &self.nodes {
            // Build a new EmitContext with debug
            let context = EmitContextBuilder::default()
                .verbosity(EmitVerbosity::Debug)
                .include_ssa_versions(true)
                .build();
            let mut emitter = Gs2Emitter::new(context);
            emitter.visit_node(node);
            let result = emitter.output();

            writeln!(
                &mut label,
                r##"{indent}    <TR>
{indent}        <TD ALIGN="LEFT"><FONT COLOR="{GBF_YELLOW}">{}</FONT></TD>
{indent}    </TR>"##,
                result,
                indent = indent
            )
            .unwrap();
        }

        // Close the HTML-like table.
        writeln!(&mut label, "{indent}</TABLE>", indent = indent).unwrap();

        label
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::decompiler::ast::assignable::AssignableKind;
    use crate::decompiler::ast::bin_op::{BinOpType, BinaryOperationNode};
    use crate::decompiler::ast::expr::ExprKind;
    use crate::decompiler::ast::identifier::IdentifierNode;
    use crate::decompiler::ast::literal::LiteralNode;
    use crate::decompiler::ast::statement::StatementNode;

    fn create_identifier(id: &str) -> Box<AssignableKind> {
        Box::new(AssignableKind::Identifier(IdentifierNode::new(
            id.to_string(),
        )))
    }

    fn create_integer_literal(value: i32) -> Box<ExprKind> {
        Box::new(ExprKind::Literal(LiteralNode::Number(value)))
    }

    fn create_addition(lhs: Box<ExprKind>, rhs: Box<ExprKind>) -> Box<ExprKind> {
        Box::new(ExprKind::BinOp(
            BinaryOperationNode::new(lhs, rhs, BinOpType::Add).unwrap(),
        ))
    }

    fn create_subtraction(lhs: Box<ExprKind>, rhs: Box<ExprKind>) -> Box<ExprKind> {
        Box::new(ExprKind::BinOp(
            BinaryOperationNode::new(lhs, rhs, BinOpType::Sub).unwrap(),
        ))
    }

    fn create_statement(lhs: Box<AssignableKind>, rhs: Box<ExprKind>) -> StatementNode {
        StatementNode::new(lhs, rhs).unwrap()
    }

    #[test]
    fn test_region_creation_and_instruction_addition() {
        let region_id = RegionId::new(0, RegionType::Linear);
        let mut region = Region::new(region_id);

        assert_eq!(region.region_type(), &RegionType::Linear);
        assert_eq!(region.iter_statements().count(), 0);

        let ast_node1 = create_statement(
            create_identifier("x"),
            create_addition(create_integer_literal(1), create_integer_literal(2)),
        );

        let ast_node2 = create_statement(
            create_identifier("y"),
            create_subtraction(create_integer_literal(3), create_integer_literal(4)),
        );

        region.push_node(AstKind::Statement(ast_node1.clone()));
        region.push_node(AstKind::Statement(ast_node2.clone()));

        let mut iter = region.iter_statements();
        assert_eq!(iter.next(), Some(&AstKind::Statement(ast_node1)));
        assert_eq!(iter.next(), Some(&AstKind::Statement(ast_node2)));
    }

    #[test]
    fn test_region_into_iter() {
        let region_id = RegionId::new(0, RegionType::Linear);
        let region = Region::new(region_id);
        let mut iter = region.into_iter();
        assert_eq!(iter.next(), None);
    }
}