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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
use std::fmt;

use paste;

use crate::tokens::expression::*;
use crate::tokens::registers::*;

#[derive(Debug, PartialEq, Eq, Clone)]
/// Encode the way mnemonics access to data
#[allow(missing_docs)]
pub enum DataAccess {
    /// We are using an indexed register associated to its index
    IndexRegister16WithIndex(IndexRegister16, Expr),
    IndexRegister16(IndexRegister16),
    IndexRegister8(IndexRegister8),
    /// Represents a standard 16 bits register
    Register16(Register16),
    /// Represents a standard 8 bits register
    Register8(Register8),
    /// Represents a memory access indexed by a register
    MemoryRegister16(Register16),
    MemoryIndexRegister16(IndexRegister16),
    /// Represents any expression
    Expression(Expr),
    /// Represents an address
    Memory(Expr),
    /// Represnts the test of bit flag
    FlagTest(FlagTest),
    /// Special register I
    SpecialRegisterI,
    /// Special register R
    SpecialRegisterR,
    /// Used for in/out instructions
    PortC,
    /// Used for in/out instructions
    PortN(Expr)
}

impl From<u8> for DataAccess {
    fn from(val: u8) -> Self {
        DataAccess::Expression(Expr::from(val))
    }
}

impl From<Expr> for DataAccess {
    fn from(exp: Expr) -> Self {
        DataAccess::Expression(exp)
    }
}

impl From<&str> for DataAccess {
    fn from(txt: &str) -> Self {
        DataAccess::Expression(Expr::from(txt))
    }
}

impl From<Register8> for DataAccess {
    fn from(reg: Register8) -> Self {
        DataAccess::Register8(reg)
    }
}

impl From<Register16> for DataAccess {
    fn from(reg: Register16) -> Self {
        DataAccess::Register16(reg)
    }
}

impl From<IndexRegister8> for DataAccess {
    fn from(reg: IndexRegister8) -> Self {
        DataAccess::IndexRegister8(reg)
    }
}

impl From<IndexRegister16> for DataAccess {
    fn from(reg: IndexRegister16) -> Self {
        DataAccess::IndexRegister16(reg)
    }
}

impl From<FlagTest> for DataAccess {
    fn from(test: FlagTest) -> Self {
        DataAccess::FlagTest(test)
    }
}

impl fmt::Display for DataAccess {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            DataAccess::IndexRegister16WithIndex(ref reg, ref delta) => {
                if delta.is_negated() {
                    write!(f, "({} - {})", reg, delta)
                }
                else {
                    write!(f, "({} + {})", reg, delta)
                }
            }
            DataAccess::IndexRegister16(ref reg) => write!(f, "{}", reg),
            DataAccess::Register16(ref reg) => write!(f, "{}", reg),
            DataAccess::IndexRegister8(ref reg) => write!(f, "{}", reg),
            DataAccess::Register8(ref reg) => write!(f, "{}", reg),
            DataAccess::MemoryRegister16(ref reg) => write!(f, "({})", reg),
            DataAccess::MemoryIndexRegister16(ref reg) => write!(f, "({})", reg),
            DataAccess::Expression(ref exp) => write!(f, "{}", exp.to_simplified_string()),
            DataAccess::Memory(ref exp) => write!(f, "({})", exp.to_simplified_string()),
            DataAccess::FlagTest(ref test) => write!(f, "{}", test),
            DataAccess::SpecialRegisterI => write!(f, "I"),
            DataAccess::SpecialRegisterR => write!(f, "R"),
            DataAccess::PortC => write!(f, "(C)"),
            DataAccess::PortN(n) => write!(f, "({})", n)
        }
    }
}

impl DataAccess {
    // Rename the local labels used in macros (with @)
    // pub fn fix_local_macro_labels_with_seed(&mut self, seed: usize) {
    // match self {
    // Self::Expression(e)
    // | Self::IndexRegister16WithIndex(_, e)
    // | Self::Memory(e)
    // | Self::PortN(e) => {
    // e.fix_local_macro_labels_with_seed(seed);
    // }
    // _ => {
    // nothing to do there
    // }
    // }
    // }
}

#[allow(missing_docs)]
impl DataAccess {
    pub fn expr(&self) -> Option<&Expr> {
        match self {
            DataAccess::Expression(ref expr) => Some(expr),
            _ => None
        }
    }

    pub fn is_portc(&self) -> bool {
        match self {
            DataAccess::PortC => true,
            _ => false
        }
    }

    pub fn is_register_i(&self) -> bool {
        match self {
            DataAccess::SpecialRegisterI => true,
            _ => false
        }
    }

    pub fn is_register_r(&self) -> bool {
        match self {
            DataAccess::SpecialRegisterR => true,
            _ => false
        }
    }

    pub fn is_register8(&self) -> bool {
        match self {
            DataAccess::Register8(_) => true,
            _ => false
        }
    }

    pub fn is_register16(&self) -> bool {
        match self {
            DataAccess::Register16(_) => true,
            _ => false
        }
    }

    pub fn is_indexregister8(&self) -> bool {
        match self {
            DataAccess::IndexRegister8(_) => true,
            _ => false
        }
    }

    pub fn is_indexregister16(&self) -> bool {
        match self {
            DataAccess::IndexRegister16(_) => true,
            _ => false
        }
    }

    pub fn is_indexregister_with_index(&self) -> bool {
        match self {
            DataAccess::IndexRegister16WithIndex(..) => true,
            _ => false
        }
    }

    pub fn is_memory(&self) -> bool {
        match self {
            DataAccess::Memory(_) => true,
            _ => false
        }
    }

    pub fn is_address_in_register16(&self) -> bool {
        match self {
            DataAccess::MemoryRegister16(_) => true,
            _ => false
        }
    }

    pub fn is_address_in_indexregister16(&self) -> bool {
        match self {
            DataAccess::MemoryIndexRegister16(_) => true,
            _ => false
        }
    }

    pub fn get_register16(&self) -> Option<Register16> {
        match self {
            DataAccess::Register16(ref reg) => Some(*reg),
            DataAccess::MemoryRegister16(ref reg) => Some(*reg),
            _ => None
        }
    }

    pub fn get_indexregister16(&self) -> Option<IndexRegister16> {
        match self {
            DataAccess::IndexRegister16(ref reg) => Some(*reg),
            DataAccess::MemoryIndexRegister16(ref reg) => Some(*reg),
            DataAccess::IndexRegister16WithIndex(reg, _) => Some(*reg),
            _ => None
        }
    }

    pub fn to_data_access_for_low_register(&self) -> Option<DataAccess> {
        match self {
            DataAccess::IndexRegister16(ref reg) => Some(reg.low().into()),
            DataAccess::Register16(ref reg) => Some(reg.low().unwrap().into()),
            _ => None
        }
    }

    pub fn to_data_access_for_high_register(&self) -> Option<DataAccess> {
        match self {
            DataAccess::IndexRegister16(ref reg) => Some(reg.high().into()),
            DataAccess::Register16(ref reg) => Some(reg.high().unwrap().into()),
            _ => None
        }
    }

    pub fn get_register8(&self) -> Option<Register8> {
        match self {
            DataAccess::Register8(ref reg) => Some(*reg),
            _ => None
        }
    }

    pub fn get_expression(&self) -> Option<&Expr> {
        match self {
            DataAccess::Expression(ref exp) => Some(exp),
            _ => None
        }
    }

    pub fn expression_mut(&mut self) -> Option<&mut Expr> {
        match self {
            DataAccess::Expression(ref mut exp) => Some(exp),
            _ => None
        }
    }
}

macro_rules! is_any_indexregister8 {
    ($($reg:ident)*) => {$(
        paste::paste! {
            impl DataAccess {
                /// Check if this DataAccess corresonds to $reg
                pub fn [<is_register_ $reg:lower>] (&self) -> bool {
                    match self {
                        DataAccess::IndexRegister8(IndexRegister8::$reg) => true,
                        _ => false,
                    }
                }
            }
        }
    )*}
}
is_any_indexregister8!(Ixh Ixl Iyh Iyl);

macro_rules! is_any_register8 {
    ($($reg:ident)*) => {$(
        paste::paste! {
            impl DataAccess {
                /// Check if this DataAccess corresonds to $reg
                pub fn [<is_register_ $reg:lower>] (&self) -> bool {
                    match self {
                        DataAccess::Register8(Register8::$reg) => true,
                        _ => false,
                    }
                }
            }
        }
    )*}
}
is_any_register8!(A B C D E H L);

macro_rules! is_any_register16 {
    ($($reg:ident)*) => {$(
        paste::paste! {
            impl DataAccess {
                /// Check if this DataAccess corresonds to $reg
                pub fn [<is_register_ $reg:lower>] (&self) -> bool {
                    match self {
                        DataAccess::Register16(Register16::$reg) => true,
                        _ => false,
                    }
                }
            }
        }
    )*}
}
is_any_register16!(Af Bc De Hl Sp);

macro_rules! is_any_indexregister16 {
    ($($reg:ident)*) => {$(
        paste::paste! {
            impl DataAccess {
                /// Check if this DataAccess corresonds to $reg
                pub fn [<is_register_ $reg:lower>] (&self) -> bool {
                    match self {
                        DataAccess::IndexRegister16(IndexRegister16::$reg) => true,
                        _ => false,
                    }
                }
            }
        }
    )*}
}
is_any_indexregister16!(Ix Iy);

#[cfg(test)]
mod test {
    use super::*;

    #[test]
    fn is_indexregister8() {
        assert!(DataAccess::IndexRegister8(IndexRegister8::Ixl).is_indexregister8());
        assert!(DataAccess::IndexRegister8(IndexRegister8::Ixl).is_register_ixl());
        assert!(!DataAccess::IndexRegister8(IndexRegister8::Ixl).is_register_iyl());
        assert!(!DataAccess::Register8(Register8::A).is_register_iyl());
    }
}