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
use super::Load;
use crate::{
    ast::{
        self,
        expression::{FieldAccess, LValue},
    },
    ir::{
        function::{ir_generator::IRGeneratingContext, IsIRStatement},
        quantity::{local, Quantity, RegisterName},
    },
    utility::{
        data_type,
        data_type::Type,
        parsing::{self, in_multispace},
    },
};
use nom::{
    bytes::complete::tag,
    character::complete::{space0, space1},
    combinator::map,
    multi::separated_list1,
    sequence::{delimited, tuple},
    IResult,
};
use serde::{Deserialize, Serialize};
use std::fmt;

/// [`LoadField`] instruction.
#[derive(Debug, Eq, PartialEq, Clone, Hash, Serialize, Deserialize)]
pub struct LoadField {
    /// Where to store the result of the load.
    pub target: RegisterName,
    /// Where to load from.
    pub source: RegisterName,
    /// Access `.0`th field of the struct, which is `.1` type.
    pub field_chain: Vec<(Type, usize)>,
    /// `to`'s type.
    pub leaf_type: Type,
}

impl fmt::Display for LoadField {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(
            f,
            "{} = load_field {} {}.[{}]",
            self.target,
            self.leaf_type,
            self.source,
            self.field_chain
                .iter()
                .map(|(t, i)| format!("{t}.{i}"))
                .collect::<Vec<_>>()
                .join(", "),
        )
    }
}

impl IsIRStatement for LoadField {
    fn on_register_change(&mut self, from: &RegisterName, to: Quantity) {
        if &self.target == from {
            self.target = to.clone().unwrap_local();
        }
        if &self.source == from {
            self.source = to.unwrap_local();
        }
    }
    fn generate_register(&self) -> Option<(RegisterName, Type)> {
        Some((self.target.clone(), self.leaf_type.clone()))
    }
    fn use_register(&self) -> Vec<RegisterName> {
        vec![self.source.clone()]
    }
}

fn parse_field(code: &str) -> IResult<&str, (Type, usize)> {
    map(
        tuple((data_type::parse, tag("."), parsing::integer)),
        |(t, _, i)| (t, i),
    )(code)
}

/// Parse ir code to get a [`LoadField`] instruction.
pub fn parse(code: &str) -> IResult<&str, LoadField> {
    map(
        tuple((
            local::parse,
            space0,
            tag("="),
            space0,
            tag("loadfield"),
            space1,
            data_type::parse,
            space1,
            local::parse,
            tag("."),
            delimited(
                tag("["),
                separated_list1(tag(","), in_multispace(parse_field)),
                tag("]"),
            ),
        )),
        |(to, _, _equal, _, _loadfield, _space, final_type, _, source, _dot, field_chain)| {
            LoadField {
                target: to,
                leaf_type: final_type,
                source,
                field_chain,
            }
        },
    )(code)
}

/// Generate IR from an [`ast::expression::FieldAccess`] AST node.
pub fn from_ast(ast: &ast::expression::FieldAccess, ctx: &mut IRGeneratingContext) -> RegisterName {
    let ast::expression::FieldAccess { from, name } = ast;
    let mut current = *from.clone();
    let mut field_chain_rev = vec![name.clone()];
    while let LValue::FieldAccess(field_access) = current {
        let FieldAccess { from, name } = field_access;
        field_chain_rev.push(name);
        current = *from.clone();
    }
    let root = if let LValue::VariableRef(root) = from.as_ref() {
        root
    } else {
        unreachable!()
    };
    let mut current_type = ctx.symbol_table.type_of_variable(root);
    let mut field_chain = vec![];
    for field in field_chain_rev.into_iter().rev() {
        let current_type_name = if let Type::StructRef(name) = &current_type {
            name
        } else {
            unreachable!()
        };
        let mapping = ctx
            .parent_context
            .type_definitions
            .get(current_type_name)
            .unwrap();
        let index = mapping.field_names.get(&field).unwrap();
        let data_type = mapping.field_types[*index].clone();
        field_chain.push((current_type, *index));
        current_type = data_type;
    }
    let root_variable_addr = ctx.symbol_table.current_variable_address_register(root);
    let load_to = ctx.next_register_with_type(&field_chain[0].0);
    ctx.current_basic_block.append_statement(Load {
        to: load_to.clone(),
        data_type: field_chain[0].0.clone(),
        from: root_variable_addr.into(),
    });
    let target = ctx.next_register_with_type(&field_chain[0].0);
    ctx.current_basic_block.append_statement(LoadField {
        target: target.clone(),
        source: load_to,
        field_chain,
        leaf_type: current_type,
    });
    target
}

#[cfg(test)]
mod tests {
    #![allow(clippy::borrow_interior_mutable_const)]
    use super::*;

    #[test]
    fn test_parse() {
        let result = parse("%1 = loadfield i32 %0.[S.0]").unwrap().1;
        assert_eq!(
            result,
            LoadField {
                target: RegisterName("1".to_string()),
                source: RegisterName("0".to_string()),
                field_chain: vec![(Type::StructRef("S".to_string()), 0)],
                leaf_type: data_type::I32.clone()
            },
        );

        let result = parse("%1 = loadfield i32 %0.[SS.1, S.0]").unwrap().1;
        assert_eq!(
            result,
            LoadField {
                target: RegisterName("1".to_string()),
                source: RegisterName("0".to_string()),
                field_chain: vec![
                    (Type::StructRef("SS".to_string()), 1),
                    (Type::StructRef("S".to_string()), 0)
                ],
                leaf_type: data_type::I32.clone()
            },
        );
    }
}