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
/*
* Copyright (c) Peter Bjorklund. All rights reserved. https://github.com/swamp/swamp
* Licensed under the MIT License. See LICENSE in the project root for license information.
*/
use crate::code_bld::CodeBuilder;
use crate::ctx::Context;
use swamp_semantic::{Match, NormalPattern, Pattern, PatternElement};
use swamp_vm_isa::MemoryOffset;
use swamp_vm_types::MemoryLocation;
use swamp_vm_types::types::{BasicTypeKind, Place, VmType, b8_type, u8_type};
impl CodeBuilder<'_> {
#[allow(clippy::too_many_lines)]
pub(crate) fn emit_match_enum(
&mut self,
output_destination: &Place,
match_expr: &Match,
ctx: &Context,
) {
let enum_ptr_reg = self.emit_scalar_rvalue(&match_expr.expression, ctx);
let mut jump_to_exit_placeholders = Vec::new();
let arm_len_to_consider = if match_expr.contains_wildcard() {
match_expr.arms.len()
} else {
match_expr.arms.len()
};
let enum_tag_temp_reg = self.temp_registers.allocate(
VmType::new_contained_in_register(u8_type()),
"temp reg for enum tag",
); // TODO: support different tag sizes
let condition_reg = self.temp_registers.allocate(
VmType::new_contained_in_register(b8_type()),
"comparison reg",
);
self.builder.add_ld8_from_pointer_with_offset(
enum_tag_temp_reg.register(),
&enum_ptr_reg,
MemoryOffset(0), // TODO: take offset from tag union info
&match_expr.expression.node,
"read enum tag",
);
let underlying_enum = enum_ptr_reg.ty.basic_type.clone();
let BasicTypeKind::TaggedUnion(enum_type) = &underlying_enum.kind else {
//panic!("internal error enum {underlying_enum}");
return;
};
for (index, arm) in match_expr.arms.iter().enumerate() {
let is_last = index == arm_len_to_consider - 1;
// Each arm must set the CPU zero flag
let maybe_guard = match &arm.pattern {
Pattern::Normal(normal_pattern, maybe_guard) => match normal_pattern {
NormalPattern::PatternList(_) => None,
NormalPattern::EnumPattern(enum_variant, maybe_patterns) => {
self.builder.add_eq_u8_immediate(
condition_reg.register(),
enum_tag_temp_reg.register(),
enum_variant.common().container_index,
&arm.expression.node,
"check for enum variant",
);
maybe_guard.as_ref()
}
NormalPattern::Literal(_) => {
todo!()
}
},
Pattern::Wildcard(_) => {
// Wildcard is always true, so no comparison code is needed here at all
None
}
};
let did_add_comparison = !matches!(arm.pattern, Pattern::Wildcard(_));
let maybe_skip_added = if did_add_comparison {
Some(self.builder.add_jmp_if_not_equal_placeholder(
condition_reg.register(),
&arm.expression.node,
"placeholder for enum match",
))
} else {
None
};
// insert code here to emit patterns to variables
match &arm.pattern {
Pattern::Normal(normal_pattern, _maybe_guard) => match normal_pattern {
NormalPattern::PatternList(_) => todo!(),
NormalPattern::EnumPattern(enum_variant, maybe_patterns) => {
if let Some(patterns) = maybe_patterns {
for pattern in patterns {
match pattern {
PatternElement::Variable(var) => {
// For Variable patterns, we set the pointer to the payload to the variable
let var_reg = self
.variable_registers
.get(&var.unique_id_within_function)
.unwrap()
.clone();
let source_enum_payload_location = MemoryLocation {
base_ptr_reg: enum_ptr_reg.clone(),
offset: enum_type.payload_offset,
ty: var_reg.ty.clone(),
};
self.emit_load_value_from_memory_source(
&var_reg,
&source_enum_payload_location,
&var.name,
"load value from variant payload into variable",
);
}
PatternElement::VariableWithFieldIndex(
variable,
field_index_within_variant_payload_value,
) => {
let enum_variant_common = enum_type
.get_variant_as_offset_item(
enum_variant.common().index() as usize,
);
let field_offset_item_inside_payload = enum_variant_common
.ty
.get_field_offset(
*field_index_within_variant_payload_value,
)
.unwrap();
let total_offset = enum_type.payload_offset
+ field_offset_item_inside_payload.offset;
let var_reg = self
.variable_registers
.get(&variable.unique_id_within_function)
.unwrap()
.clone();
let source_enum_memory_location = MemoryLocation {
base_ptr_reg: enum_ptr_reg.clone(),
offset: total_offset,
ty: var_reg.ty.clone(),
};
self.emit_load_or_calculate_address_from_memory(
&var_reg,
&source_enum_memory_location,
&variable.name,
"load variant from field index",
);
}
PatternElement::Wildcard(_) => {
// Intentionally do nothing, the variable should not be handled
}
}
}
}
}
NormalPattern::Literal(_) => {
todo!()
}
},
Pattern::Wildcard(_) => {
// Intentionally do nothing, the expression will be used below
}
}
// evaluate the guard condition after pattern variables are loaded
let maybe_guard_skip = maybe_guard.map(|guard| self.emit_condition_context(guard, ctx));
self.emit_expression(output_destination, &arm.expression, ctx);
if !is_last {
let jump_to_exit_placeholder = self.builder.add_jump_placeholder(
&arm.expression.debug_last_expression().node,
"jump to exit",
);
jump_to_exit_placeholders.push(jump_to_exit_placeholder);
}
if let Some(skip) = maybe_skip_added {
self.builder.patch_jump_here(skip);
}
if let Some(guard_skip) = maybe_guard_skip {
self.builder.patch_jump_here(guard_skip);
}
}
for placeholder in jump_to_exit_placeholders {
self.builder.patch_jump_here(placeholder);
}
}
pub(crate) fn emit_match_literal(
&mut self,
output_destination: &Place,
match_expr: &Match,
ctx: &Context,
) {
let mut jump_to_exit_placeholders = Vec::new();
let arm_len_to_consider = if match_expr.contains_wildcard() {
match_expr.arms.len()
} else {
match_expr.arms.len()
};
let arm_bool_condition_reg = self.temp_registers.allocate(
VmType::new_contained_in_register(b8_type()),
"comparison bool reg",
);
let scrutinee_reg = self.emit_scalar_rvalue(&match_expr.expression, ctx);
for (index, arm) in match_expr.arms.iter().enumerate() {
let is_last = index == arm_len_to_consider - 1;
let maybe_guard = match &arm.pattern {
Pattern::Normal(normal_pattern, maybe_guard) => match normal_pattern {
NormalPattern::Literal(literal_expression) => {
let literal_expr_reg = self.emit_scalar_rvalue(literal_expression, ctx);
let node = literal_expression.node.clone();
self.emit_equality_to_bool_target(
arm_bool_condition_reg.register(),
&scrutinee_reg,
true,
&literal_expr_reg,
&node,
ctx,
);
maybe_guard.as_ref()
}
NormalPattern::PatternList(_) => panic!("not handled here"),
NormalPattern::EnumPattern(a, b) => {
panic!("got enum pattern in literals! {a:?} {b:?}")
}
},
Pattern::Wildcard(_) => {
// Wildcard is always true, so no comparison code is needed here at all
None
}
};
let did_add_comparison = !matches!(arm.pattern, Pattern::Wildcard(_));
let maybe_skip_added = if did_add_comparison {
Some(self.builder.add_jmp_if_not_equal_placeholder(
&arm_bool_condition_reg.register,
&arm.expression.node,
"placeholder for enum match",
))
} else {
None
};
// insert code here to emit patterns to variables
match &arm.pattern {
Pattern::Normal(normal_pattern, maybe_guard) => match normal_pattern {
NormalPattern::PatternList(_) => panic!("should not be handled here"),
NormalPattern::Literal(literal_expression) => {
// Intentionally do nothing, the expression will be used below
}
NormalPattern::EnumPattern(_, _) => panic!("should not be handled here"),
},
Pattern::Wildcard(_) => {
// Intentionally do nothing, the expression will be used below
}
}
// evaluate the guard condition after pattern variables are loaded
let maybe_guard_skip =
maybe_guard.map(|found_guard| self.emit_condition_context(found_guard, ctx));
self.emit_expression(output_destination, &arm.expression, ctx);
if !is_last {
let jump_to_exit_placeholder = self.builder.add_jump_placeholder(
&arm.expression.debug_last_expression().node,
"jump to exit",
);
jump_to_exit_placeholders.push(jump_to_exit_placeholder);
}
if let Some(skip) = maybe_skip_added {
self.builder.patch_jump_here(skip);
}
if let Some(guard_skip) = maybe_guard_skip {
self.builder.patch_jump_here(guard_skip);
}
}
for placeholder in jump_to_exit_placeholders {
self.builder.patch_jump_here(placeholder);
}
}
}