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
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
/*
* 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::{BinaryOperatorKind, Expression, ExpressionKind, PostfixKind};
use swamp_types::TypeKind;
use swamp_vm_layout::LayoutCache;
use swamp_vm_types::types::{int_type, Place, TypedRegister, VmType};
use swamp_vm_types::MemoryLocation;
use tracing::{info, warn};
impl CodeBuilder<'_> {
/// The expression materializer! Transforms high-level expressions into their code representation,
/// making sure each value finds its proper home in either a register or memory location.
///
/// # Optimization Magic
///
/// Uses Destination-Passing Style (DPS) to optimize code generation. Instead of creating
/// temporary values and copying them around, we tell each expression exactly where its
/// result should end up. This means we can often construct values directly in their
/// final location, avoiding unnecessary copies and temporary allocations.
///
/// # Arguments
///
/// * `output` - Where should our expression's result live? (register or memory location)
/// * `expr` - The expression we're bringing to life
/// * `ctx` - The context with all our compilation knowledge
///
/// If something needs temporary storage, we'll handle that too.
#[allow(clippy::too_many_lines)]
pub fn emit_expression(&mut self, output: &Place, expr: &Expression, ctx: &Context) {
let node = &expr.node;
if self.options.should_show_debug {
self.debug_expression(expr, "emitting expression");
}
// Must check for initializer first, since they do not require temporary memory
// or similar, should be handled first and then return.
match &expr.kind {
ExpressionKind::InitializerList(_element_type, expressions) => {
self.emit_vec_like_collection_init_from_initialization_list(
output,
expressions,
&expr.node,
ctx,
);
return;
}
ExpressionKind::InitializerPairList(_element_type, expressions) => {
self.emit_map_like_init_from_initialization_pair_list(
output,
expressions,
&expr.node,
ctx,
);
return;
}
_ => {}
}
// If the expression needs a memory target, and the current output is not a memory target, create temp memory to materialize in
// and return a pointer in the register instead and hopefully it works out.
if !matches!(output, Place::Memory(_))
&& Self::rvalue_needs_memory_location_to_materialize_in(
&mut self.state.layout_cache,
expr,
)
{
let expr_basic_type = self.state.layout_cache.layout(&expr.ty);
let temp_materialization_target = self
.allocate_frame_space_and_return_destination_to_it(
&expr_basic_type,
true, // we don't know if the type has padding
&expr.node,
"rvalue temporary materialization",
);
self.emit_expression(&temp_materialization_target, expr, ctx);
self.builder.add_mov_reg(
output.grab_register(),
&temp_materialization_target
.grab_memory_location()
.base_ptr_reg,
node,
"copy temp materialization memory pointer reg",
);
return;
}
let hwm = self.temp_registers.save_mark();
match &expr.kind {
&ExpressionKind::InitializerList(_, _) | &ExpressionKind::InitializerPairList(_, _) => {
panic!("handled earlier")
}
ExpressionKind::Error(_) => {
return;
}
ExpressionKind::StringLiteral(str) => {
self.emit_string_literal(output, node, str, ctx);
}
ExpressionKind::IntLiteral(int) => match output {
Place::Register(target_reg) => {
self.builder.add_mov_32_immediate_value(
target_reg,
*int as u32,
node,
"int literal",
);
}
Place::Memory(location) => {
let temp_int_literal_reg = self.temp_registers.allocate(
VmType::new_contained_in_register(int_type()),
"temporary for int literal",
);
self.builder.add_mov_32_immediate_value(
temp_int_literal_reg.register(),
*int as u32,
node,
"int literal",
);
self.builder.add_st32_using_ptr_with_offset(
location,
temp_int_literal_reg.register(),
node,
&format!("copy int literal into destination memory {location} <- {temp_int_literal_reg}"),
);
}
Place::Discard => {
panic!("int can not materialize into nothing")
}
},
ExpressionKind::ByteLiteral(byte) => match output {
Place::Register(target_reg) => {
self.builder
.add_mov8_immediate(target_reg, *byte, node, "int literal");
}
Place::Memory(location) => {
let temp_byte_literal_reg = self.temp_registers.allocate(
VmType::new_contained_in_register(int_type()),
"temporary for byte literal",
);
self.builder.add_mov8_immediate(
temp_byte_literal_reg.register(),
*byte,
node,
"byte literal",
);
self.builder.add_st8_using_ptr_with_offset(
location,
temp_byte_literal_reg.register(),
node,
&format!("copy byte literal into destination memory {location} <- {temp_byte_literal_reg}"),
);
}
Place::Discard => {
panic!("byte can not materialize into nothing")
}
},
ExpressionKind::FloatLiteral(fixed_point) => match output {
Place::Register(target_reg) => {
self.builder.add_mov_32_immediate_value(
target_reg,
fixed_point.inner() as u32,
node,
"float literal",
);
}
Place::Memory(location) => {
let temp_fixed_point_temp_reg = self.temp_registers.allocate(
VmType::new_contained_in_register(int_type()),
"temporary for float literal",
);
self.builder.add_mov_32_immediate_value(
temp_fixed_point_temp_reg.register(),
fixed_point.inner() as u32,
node,
"float literal",
);
self.builder.add_st32_using_ptr_with_offset(
location,
temp_fixed_point_temp_reg.register(),
node,
"copy float literal into destination memory",
);
}
Place::Discard => {
panic!("int can not materialize into nothing")
}
},
ExpressionKind::NoneLiteral => {
//let union_info = output.ty().unwrap_info().unwrap();
match output {
Place::Register(target_reg) => {
self.builder
.add_mov8_immediate(target_reg, 0, node, "none literal");
}
Place::Memory(location) => {
let temp_none_literal_reg = self.temp_registers.allocate(
VmType::new_contained_in_register(int_type()),
"temporary for none literal",
);
self.builder.add_mov8_immediate(
temp_none_literal_reg.register(),
0,
node,
"none literal",
);
self.builder.add_st8_using_ptr_with_offset(
location,
temp_none_literal_reg.register(),
node,
"copy none literal into destination memory",
);
}
Place::Discard => {
panic!("none can not materialize into nothing")
}
}
}
ExpressionKind::BoolLiteral(truthy) => match output {
Place::Register(target_reg) => {
self.builder.add_mov8_immediate(
target_reg,
u8::from(*truthy),
node,
"bool literal",
);
}
Place::Memory(location) => {
let temp_bool_literal_reg = self.temp_registers.allocate(
VmType::new_contained_in_register(int_type()),
"temporary for bool literal",
);
self.builder.add_mov8_immediate(
temp_bool_literal_reg.register(),
u8::from(*truthy),
node,
"bool literal",
);
self.builder.add_st8_using_ptr_with_offset(
location,
temp_bool_literal_reg.register(),
node,
"copy bool literal into destination memory",
);
}
Place::Discard => {
panic!("int can not materialize into nothing")
}
},
ExpressionKind::EnumVariantLiteral(enum_variant, expressions) => {
// A enum variant literal can not be represented as a register, not even a pointer to it, it needs materialization into memory
self.emit_enum_variant_to_memory_location(
&output.grab_aggregate_memory_location(),
&expr.ty,
enum_variant,
expressions,
node,
ctx,
);
}
ExpressionKind::TupleLiteral(expressions) => {
// A tuple literal can not be represented as a register, not even a pointer to it, it needs materialization into memory
self.emit_tuple_literal_into_memory(
&output.grab_aggregate_memory_location(),
&expr.ty,
expressions,
ctx,
node,
);
}
ExpressionKind::If(condition, true_expression, maybe_false_expression) => {
self.emit_if(
output,
condition,
true_expression,
maybe_false_expression.as_deref(),
ctx,
);
}
ExpressionKind::Block(expressions) => {
self.emit_block(output, expressions, ctx);
}
ExpressionKind::NamedStructLiteral(inner) => {
self.emit_expression(output, inner, ctx);
}
ExpressionKind::AnonymousStructLiteral(anon_struct) => {
// Literals can not have pointers to them, they need to materialize into a memory location
self.emit_anonymous_struct_literal_into_memory_location(
&output.grab_aggregate_memory_location(),
anon_struct,
&expr.ty,
node,
"struct literal",
ctx,
);
}
ExpressionKind::Option(maybe_option) => self
.emit_option_expression_into_target_memory_location(
output,
node,
maybe_option.as_deref(),
ctx,
),
ExpressionKind::ConstantAccess(constant_ref) => {
self.emit_constant_access(output, constant_ref, &expr.node, ctx);
}
ExpressionKind::VariableAccess(variable_ref) => {
let variable_register = self.get_variable_register(variable_ref).clone();
let source_destination = Place::Register(variable_register);
self.emit_copy_value_between_places(
output,
&source_destination,
node,
&format!(
"copy variable '{}' to destination",
variable_ref.assigned_name
),
);
}
ExpressionKind::BorrowMutRef(expression) => {
if !output.is_register() {
self.debug_expression(expr, "emitting expression");
}
self.emit_borrow_mutable_reference(
output.grab_register(),
&expr.node,
expression,
ctx,
); // todo:
}
ExpressionKind::BinaryOp(operator) => match operator.kind {
BinaryOperatorKind::NoneCoalesce => self.emit_none_coalesce_operator(
output,
&operator.left,
&operator.right,
&operator.node,
ctx,
),
_ => match output {
Place::Register(reg) => {
self.emit_binary_operator(reg, operator, ctx);
}
Place::Memory(mem_loc) => {
let temp_reg = self
.temp_registers
.allocate(mem_loc.ty.clone(), "binary_op_temp");
self.emit_binary_operator(temp_reg.register(), operator, ctx);
self.builder.add_st32_using_ptr_with_offset(
mem_loc,
temp_reg.register(),
node,
"store binary op result directly to memory with offset",
);
}
Place::Discard => {
panic!("binary operator always returns a value")
}
},
},
ExpressionKind::UnaryOp(operator) => match output {
Place::Register(reg) => {
self.emit_unary_operator(reg, operator, ctx);
}
Place::Memory(mem_loc) => {
let temp_reg = self
.temp_registers
.allocate(mem_loc.ty.clone(), "unary_op_temp");
self.emit_unary_operator(temp_reg.register(), operator, ctx);
self.builder.add_st32_using_ptr_with_offset(
mem_loc,
temp_reg.register(),
node,
"store unary op result directly to memory with offset",
);
}
Place::Discard => {
warn!("unary operator always returns a value")
}
},
ExpressionKind::PostfixChain(start, chain) => {
self.emit_postfix_chain(output, start, chain, ctx);
}
ExpressionKind::Match(match_expr) => match &*match_expr.expression.ty.kind {
TypeKind::Enum(_enum_type) => {
self.emit_match_enum(output, match_expr, ctx);
}
_ => {
self.emit_match_literal(output, match_expr, ctx);
}
},
ExpressionKind::Guard(guards) => self.emit_guard(output, guards, ctx),
ExpressionKind::When(bindings, true_expr, false_expr) => {
self.emit_when(output, bindings, true_expr, false_expr.as_deref(), ctx);
}
ExpressionKind::IntrinsicCallEx(intrinsic_fn, arguments) => {
self.emit_single_intrinsic_call(output, &expr.node, intrinsic_fn, arguments, ctx);
}
ExpressionKind::CoerceToAny(a) => {
self.emit_coerce_to_any(output, a, ctx);
}
ExpressionKind::CoerceOptionToBool(a) => {
self.emit_coerce_option_to_bool(output.grab_register(), a, ctx);
}
ExpressionKind::CoerceIntToChar(a) => {
self.emit_coerce_int_to_char(output.grab_register(), a, ctx);
}
ExpressionKind::CoerceIntToByte(a) => {
self.emit_coerce_int_to_byte(output, a, ctx);
}
ExpressionKind::CoerceIntToShort(a) => {
self.emit_coerce_int_to_short(output, a, ctx);
}
ExpressionKind::CoerceIntToPointer(a) => {
todo!("not supporting pointers yet");
}
ExpressionKind::InternalCall(internal, arguments) => {
self.emit_internal_call(output, &expr.node, internal, arguments, ctx);
}
ExpressionKind::HostCall(host_fn, arguments) => {
self.emit_host_call(output, &expr.node, host_fn, arguments, ctx);
}
// Statements - can not return anything, so should assert that output is unit (nothing)
ExpressionKind::TupleDestructuring(variables, tuple_type, tuple_expression) => {
debug_assert!(output.is_unit());
self.emit_tuple_destructuring(variables, tuple_type, tuple_expression, ctx);
}
ExpressionKind::Assignment(target_mut_location_expr, source_expr) => {
debug_assert!(output.is_unit());
self.emit_assignment(target_mut_location_expr, source_expr, "", ctx);
}
ExpressionKind::VariableDefinition(variable, expression) => {
debug_assert!(output.is_unit());
self.emit_variable_definition(variable, expression, ctx);
}
ExpressionKind::VariableDefinitionLValue(variable, location_expr) => {
// For lvalue definitions, the variable should be initialized with the memory address
// This is used in `with` statements, e.g. `mut var = &some.field`
match output {
Place::Discard => {
let lvalue_destination = self.emit_lvalue_address(location_expr, ctx);
let alias_register = self.get_variable_register(variable).clone();
self.emit_compute_effective_address_to_target_register(
&alias_register,
&lvalue_destination,
&variable.name,
"initialize alias variable with lvalue address",
);
}
_ => {
panic!("VariableDefinitionLValue should only be used with Unit destination")
}
}
}
ExpressionKind::VariableReassignment(variable, expression) => {
assert!(output.is_unit());
self.emit_variable_reassignment(variable, expression, ctx);
}
ExpressionKind::CompoundAssignment(target_location, operator_kind, source_expr) => {
assert!(output.is_unit());
self.emit_compound_assignment(target_location, operator_kind, source_expr, ctx);
}
ExpressionKind::ForLoop(for_pattern, collection, lambda_expr) => {
if !output.is_unit() {
let x = 3;
eprintln!("problem");
}
assert!(output.is_unit());
self.emit_for_loop(
output,
&expr.node,
for_pattern,
collection,
lambda_expr,
ctx,
);
}
ExpressionKind::WhileLoop(condition, expression) => {
assert!(output.is_unit());
self.emit_while_loop(condition, expression, ctx);
}
// Illegal
ExpressionKind::Lambda(_vec, _x) => {
panic!("something went wrong. non-capturing lambdas can not be evaluated")
}
}
self.temp_registers.restore_to_mark(hwm);
}
pub(crate) fn rvalue_needs_memory_location_to_materialize_in(
layout_cache: &mut LayoutCache,
expr: &Expression,
) -> bool {
// TODO: Should have more robust check here
match &expr.kind {
// Why does it break if we check for is_reg_copy/scalar for these?
ExpressionKind::EnumVariantLiteral(_, _)
| ExpressionKind::TupleLiteral(_)
| ExpressionKind::InitializerList(_, _)
| ExpressionKind::InitializerPairList(_, _)
| ExpressionKind::Option(_)
| ExpressionKind::AnonymousStructLiteral(_)
| ExpressionKind::CoerceToAny(_) => true,
ExpressionKind::InternalCall(_, _)
| ExpressionKind::HostCall(_, _)
| ExpressionKind::IntrinsicCallEx(_, _) => {
let basic_type = layout_cache.layout(&expr.ty);
!basic_type.is_reg_copy()
}
ExpressionKind::PostfixChain(_chain, postfixes) => {
// TODO: this seems to work, but it feels all kind of wrong
let last_is_member_call = matches!(
postfixes[postfixes.len() - 1].kind,
PostfixKind::MemberCall(..)
);
let basic_type = layout_cache.layout(&expr.ty);
last_is_member_call && !basic_type.is_reg_copy()
}
_ => false,
}
}
pub(crate) fn emit_expression_into_target_memory(
&mut self,
memory_location: &MemoryLocation,
expr: &Expression,
comment: &str,
ctx: &Context,
) {
// Only initialize if it's a direct collection type (not inside a tuple/struct)
if matches!(
expr.kind,
ExpressionKind::InitializerList(_, _) | ExpressionKind::InitializerPairList(_, _)
) {
self.emit_initialize_memory_for_any_type(
memory_location,
&expr.node,
&format!("{comment} - initialize memory for collection"),
);
}
let output = Place::new_location(memory_location.clone());
self.emit_expression(&output, expr, ctx);
}
pub(crate) fn emit_expression_into_register(
&mut self,
target_register: &TypedRegister,
expr: &Expression,
comment: &str,
ctx: &Context,
) {
let output = Place::new_reg(target_register.clone());
self.emit_expression(&output, expr, &ctx.clone().with_comment(comment));
}
}