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
use super::{AbstractOp, Expression, Imm};
use std::convert::From;
use std::fmt;
#[derive(Debug, Clone, Eq, PartialEq)]
pub enum MacroDefinition {
Instruction(InstructionMacroDefinition),
Expression(ExpressionMacroDefinition),
}
impl MacroDefinition {
pub fn name(&self) -> &String {
match self {
Self::Instruction(m) => &m.name,
Self::Expression(m) => &m.name,
}
}
pub fn parameters(&self) -> &[String] {
match self {
Self::Instruction(m) => &m.parameters,
Self::Expression(m) => &m.parameters,
}
}
pub fn unwrap_expression(&self) -> &ExpressionMacroDefinition {
match self {
Self::Instruction(_) => {
panic!("unwrapped expression macro, but found instruction macro")
}
Self::Expression(m) => m,
}
}
}
impl fmt::Display for MacroDefinition {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
Self::Instruction(m) => write!(f, "%{}({})", m.name, m.parameters.join(", ")),
Self::Expression(m) => write!(f, "{}({})", m.name, m.parameters.join(", ")),
}
}
}
impl From<InstructionMacroDefinition> for MacroDefinition {
fn from(item: InstructionMacroDefinition) -> Self {
MacroDefinition::Instruction(item)
}
}
impl From<ExpressionMacroDefinition> for MacroDefinition {
fn from(item: ExpressionMacroDefinition) -> Self {
MacroDefinition::Expression(item)
}
}
#[derive(Debug, Default, Clone, Eq, PartialEq)]
pub struct InstructionMacroDefinition {
pub name: String,
pub parameters: Vec<String>,
pub contents: Vec<AbstractOp>,
}
#[derive(Debug, Clone, Eq, PartialEq)]
pub struct InstructionMacroInvocation {
pub name: String,
pub parameters: Vec<Expression>,
}
impl InstructionMacroInvocation {
pub fn with_zero_parameters(name: String) -> Self {
Self {
name,
parameters: vec![],
}
}
}
impl fmt::Display for InstructionMacroInvocation {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(
f,
"%{}({})",
self.name,
self.parameters
.iter()
.map(Expression::to_string)
.collect::<Vec<String>>()
.join(", ")
)
}
}
#[derive(Debug, Clone, Eq, PartialEq)]
pub struct ExpressionMacroDefinition {
pub name: String,
pub parameters: Vec<String>,
pub content: Imm,
}
#[derive(Clone, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
pub struct ExpressionMacroInvocation {
pub name: String,
pub parameters: Vec<Expression>,
}
impl fmt::Display for ExpressionMacroInvocation {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(
f,
"{}({})",
self.name,
self.parameters
.iter()
.map(Expression::to_string)
.collect::<Vec<String>>()
.join(", ")
)
}
}