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
use std::fmt;

use crate::{
    ir::{
        function::IsIRStatement,
        quantity::{self, local, Quantity},
        RegisterName,
    },
    utility::{
        data_type,
        data_type::Type,
        parsing::{self, in_multispace},
    },
};
use nom::{
    bytes::complete::tag,
    character::complete::space1,
    combinator::map,
    multi::separated_list1,
    sequence::{delimited, tuple},
    IResult,
};
use serde::{Deserialize, Serialize};
/// [`SetField`] instruction.
#[derive(Debug, Eq, PartialEq, Clone, Hash, Serialize, Deserialize)]
pub struct SetField {
    /// Where to store the result.
    pub target: RegisterName,
    /// What value to set.
    pub source: Quantity,
    /// Which value to set.
    pub origin_root: RegisterName,
    /// Access `.0`th field of the struct, which is `.1` type.
    pub field_chain: Vec<(Type, usize)>,
    /// `source`'s type.
    pub final_type: Type,
}

impl IsIRStatement for SetField {
    fn on_register_change(&mut self, from: &RegisterName, to: Quantity) {
        if &self.target == from {
            self.target = to.clone().unwrap_local();
        }
        if let Quantity::RegisterName(local) = &mut self.source {
            if local == from {
                *local = to.clone().unwrap_local();
            }
        }
        if &self.origin_root == from {
            self.origin_root = to.unwrap_local();
        }
    }
    fn generate_register(&self) -> Option<(RegisterName, Type)> {
        Some((self.target.clone(), self.field_chain[0].0.clone()))
    }
    fn use_register(&self) -> Vec<RegisterName> {
        if let Quantity::RegisterName(register) = &self.source {
            vec![register.clone()]
        } else {
            vec![]
        }
    }
}

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

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 [`SetField`] instruction.
pub fn parse(code: &str) -> IResult<&str, SetField> {
    map(
        tuple((
            local::parse,
            space1,
            tag("="),
            space1,
            tag("setfield"),
            space1,
            data_type::parse,
            space1,
            local::parse,
            tag("."),
            delimited(
                tag("["),
                separated_list1(tag(","), in_multispace(parse_field)),
                tag("]"),
            ),
            space1,
            quantity::parse,
        )),
        |(
            target,
            _,
            _eq,
            _,
            _setfield,
            _,
            final_type,
            _,
            origin_root,
            _dot,
            field_chain,
            _,
            source,
        )| SetField {
            target,
            source,
            origin_root,
            field_chain,
            final_type,
        },
    )(code)
}

#[cfg(test)]
mod tests {
    #![allow(clippy::borrow_interior_mutable_const)]

    use super::*;

    #[test]
    fn test_parse() {
        let code = "%2 = setfield i32 %1.[SS.1, S.0] %0";
        let (_, set_field) = parse(code).unwrap();
        assert_eq!(
            set_field,
            SetField {
                source: RegisterName("0".to_string()).into(),
                origin_root: RegisterName("1".to_string()),
                field_chain: vec![
                    (Type::StructRef("SS".to_string()), 1),
                    (Type::StructRef("S".to_string()), 0),
                ],
                final_type: data_type::I32.clone(),
                target: RegisterName("2".to_string())
            }
        );
    }
}