-
Notifications
You must be signed in to change notification settings - Fork 5.3k
/
mod.rs
487 lines (469 loc) 路 17.7 KB
/
mod.rs
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
use super::*;
use crate::span::Span;
use crate::{
asm_lang::{virtual_ops::VirtualRegister, *},
parse_tree::CallPath,
};
use crate::{
parse_tree::Literal,
semantic_analysis::{
ast_node::{TypedAsmRegisterDeclaration, TypedCodeBlock, TypedExpressionVariant},
TypedExpression,
},
types::{MaybeResolvedType, ResolvedType},
};
mod contract_call;
mod enum_instantiation;
mod if_exp;
mod structs;
mod subfield;
use contract_call::convert_contract_call_to_asm;
use enum_instantiation::convert_enum_instantiation_to_asm;
use if_exp::convert_if_exp_to_asm;
pub(crate) use structs::{convert_struct_expression_to_asm, get_struct_memory_layout};
use subfield::convert_subfield_expression_to_asm;
/// Given a [TypedExpression], convert it to assembly and put its return value, if any, in the
/// `return_register`.
pub(crate) fn convert_expression_to_asm<'sc>(
exp: &TypedExpression<'sc>,
namespace: &mut AsmNamespace<'sc>,
return_register: &VirtualRegister,
register_sequencer: &mut RegisterSequencer,
) -> CompileResult<'sc, Vec<Op<'sc>>> {
let mut warnings = vec![];
let mut errors = vec![];
match &exp.expression {
TypedExpressionVariant::Literal(ref lit) => ok(
convert_literal_to_asm(
lit,
namespace,
return_register,
register_sequencer,
exp.span.clone(),
),
warnings,
errors,
),
TypedExpressionVariant::FunctionApplication {
name,
arguments,
function_body,
selector,
} => {
if let Some(metadata) = selector {
assert_eq!(
arguments.len(),
4,
"this is verified in the semantic analysis stage"
);
convert_contract_call_to_asm(
metadata,
// gas to forward
&arguments[0].1,
// coins to forward
&arguments[1].1,
// color of coins
&arguments[2].1,
// user parameter
&arguments[3].1,
register_sequencer,
namespace,
exp.span.clone(),
)
} else {
convert_fn_app_to_asm(
name,
arguments,
function_body,
namespace,
return_register,
register_sequencer,
)
}
}
TypedExpressionVariant::VariableExpression { name } => {
let var = check!(
namespace.look_up_variable(name),
return err(warnings, errors),
warnings,
errors
);
ok(
vec![Op::register_move(
return_register.into(),
var.into(),
exp.span.clone(),
)],
warnings,
errors,
)
}
TypedExpressionVariant::AsmExpression {
registers,
body,
returns,
whole_block_span,
} => {
let mut asm_buf = vec![];
let mut warnings = vec![];
let mut errors = vec![];
// Keep track of the mapping from the declared names of the registers to the actual
// registers from the sequencer for replacement
let mut mapping_of_real_registers_to_declared_names: HashMap<&str, VirtualRegister> =
Default::default();
for TypedAsmRegisterDeclaration {
name,
initializer,
name_span,
} in registers
{
let register = register_sequencer.next();
assert_or_warn!(
ConstantRegister::parse_register_name(name).is_none(),
warnings,
name_span.clone(),
Warning::ShadowingReservedRegister { reg_name: &name }
);
mapping_of_real_registers_to_declared_names.insert(name, register.clone());
// evaluate each register's initializer
if let Some(initializer) = initializer {
asm_buf.append(&mut check!(
convert_expression_to_asm(
initializer,
namespace,
®ister,
register_sequencer,
),
continue,
warnings,
errors
));
}
}
// For each opcode in the asm expression, attempt to parse it into an opcode and
// replace references to the above registers with the newly allocated ones.
for op in body {
let replaced_registers = op
.op_args
.iter()
.map(|x| -> Result<_, CompileError> {
match realize_register(
x.primary_name,
&mapping_of_real_registers_to_declared_names,
) {
Some(o) => Ok(o),
None => Err(CompileError::UnknownRegister {
span: x.span.clone(),
initialized_registers: mapping_of_real_registers_to_declared_names
.iter()
.map(|(name, _)| name.to_string())
.collect::<Vec<_>>()
.join("\n"),
}),
}
})
.collect::<Vec<Result<_, _>>>();
let replaced_registers = replaced_registers
.into_iter()
.filter_map(|x| match x {
Err(e) => {
errors.push(e);
None
}
Ok(o) => Some(o),
})
.collect::<Vec<VirtualRegister>>();
// parse the actual op and registers
let opcode = check!(
Op::parse_opcode(
&op.op_name,
replaced_registers.as_slice(),
&op.immediate,
op.span.clone()
),
continue,
warnings,
errors
);
asm_buf.push(Op {
opcode: either::Either::Left(opcode),
comment: String::new(),
owning_span: Some(op.span.clone()),
});
}
// Now, load the designated asm return register into the desired return register
match (returns, return_register) {
(Some((asm_reg, asm_reg_span)), return_reg) => {
// lookup and replace the return register
let mapped_asm_ret = match realize_register(
asm_reg.name.as_str(),
&mapping_of_real_registers_to_declared_names,
) {
Some(reg) => reg,
None => {
errors.push(CompileError::UnknownRegister {
span: asm_reg_span.clone(),
initialized_registers: mapping_of_real_registers_to_declared_names
.iter()
.map(|(name, _)| name.to_string())
.collect::<Vec<_>>()
.join("\n"),
});
return err(warnings, errors);
}
};
asm_buf.push(Op::unowned_register_move_comment(
return_reg.clone(),
mapped_asm_ret.clone(),
"return value from inline asm",
));
}
_ => {
errors.push(CompileError::InvalidAssemblyMismatchedReturn {
span: whole_block_span.clone(),
});
}
}
ok(asm_buf, warnings, errors)
}
TypedExpressionVariant::StructExpression {
struct_name,
fields,
} => convert_struct_expression_to_asm(struct_name, fields, namespace, register_sequencer),
TypedExpressionVariant::StructFieldAccess {
resolved_type_of_parent,
prefix,
field_to_access,
} => convert_subfield_expression_to_asm(
&exp.span,
prefix,
field_to_access,
resolved_type_of_parent,
namespace,
register_sequencer,
return_register,
),
TypedExpressionVariant::EnumInstantiation {
enum_decl,
variant_name,
tag,
contents,
} => convert_enum_instantiation_to_asm(
enum_decl,
variant_name,
*tag,
contents,
return_register,
namespace,
register_sequencer,
),
TypedExpressionVariant::IfExp {
condition,
then,
r#else,
} => convert_if_exp_to_asm(
&**condition,
&**then,
r#else,
return_register,
namespace,
register_sequencer,
),
TypedExpressionVariant::CodeBlock(block) => {
convert_code_block_to_asm(block, namespace, register_sequencer, Some(return_register))
}
TypedExpressionVariant::Unit => ok(vec![], warnings, errors),
// ABI casts are purely compile-time constructs and generate no corresponding bytecode
TypedExpressionVariant::AbiCast { .. } => ok(vec![], warnings, errors),
a => {
println!("unimplemented: {:?}", a);
errors.push(CompileError::Unimplemented(
"ASM generation has not yet been implemented for this.",
exp.span.clone(),
));
err(warnings, errors)
}
}
}
/// Takes a virtual register ID and either locates it in the register mapping, finds it is a reserved register,
/// or finds nothing and returns `None`.
fn realize_register(
register_name: &str,
mapping_of_real_registers_to_declared_names: &HashMap<&str, VirtualRegister>,
) -> Option<VirtualRegister> {
match mapping_of_real_registers_to_declared_names.get(register_name) {
Some(x) => Some(x.clone()),
None => match ConstantRegister::parse_register_name(register_name) {
Some(x) => Some(VirtualRegister::Constant(x)),
None => None,
},
}
}
pub(crate) fn convert_code_block_to_asm<'sc>(
block: &TypedCodeBlock<'sc>,
namespace: &mut AsmNamespace<'sc>,
register_sequencer: &mut RegisterSequencer,
// Where to put the return value of this code block, if there was any.
return_register: Option<&VirtualRegister>,
) -> CompileResult<'sc, Vec<Op<'sc>>> {
let mut asm_buf: Vec<Op> = vec![];
let mut warnings = vec![];
let mut errors = vec![];
// generate a label for this block
let exit_label = register_sequencer.get_label();
for node in &block.contents {
// If this is a return, then we jump to the end of the function and put the
// value in the return register
let res = check!(
convert_node_to_asm(node, namespace, register_sequencer, return_register),
continue,
warnings,
errors
);
match res {
NodeAsmResult::JustAsm(ops) => asm_buf.append(&mut ops.into_iter().collect()),
NodeAsmResult::ReturnStatement { mut asm } => {
// insert a placeholder to jump to the end of the block and put the register
asm_buf.append(&mut asm);
asm_buf.push(Op::jump_to_label(exit_label.clone()));
}
}
}
asm_buf.push(Op::unowned_jump_label(exit_label));
ok(asm_buf, warnings, errors)
}
/// Initializes [Literal] `lit` into [VirtualRegister] `return_register`.
fn convert_literal_to_asm<'sc>(
lit: &Literal<'sc>,
namespace: &mut AsmNamespace<'sc>,
return_register: &VirtualRegister,
_register_sequencer: &mut RegisterSequencer,
span: Span<'sc>,
) -> Vec<Op<'sc>> {
// first, insert the literal into the data section
let data_id = namespace.insert_data_value(lit);
// then get that literal id and use it to make a load word op
vec![Op {
opcode: either::Either::Left(VirtualOp::LWDataId(return_register.clone(), data_id)),
comment: "literal instantiation".into(),
owning_span: Some(span),
}]
}
/// For now, all functions are handled by inlining at the time of application.
fn convert_fn_app_to_asm<'sc>(
name: &CallPath<'sc>,
arguments: &[(Ident<'sc>, TypedExpression<'sc>)],
function_body: &TypedCodeBlock<'sc>,
parent_namespace: &mut AsmNamespace<'sc>,
return_register: &VirtualRegister,
register_sequencer: &mut RegisterSequencer,
) -> CompileResult<'sc, Vec<Op<'sc>>> {
let mut warnings = vec![];
let mut errors = vec![];
let mut asm_buf = vec![Op::new_comment(format!(
"{} fn call",
name.suffix.primary_name
))];
// Make a local namespace so that the namespace of this function does not pollute the outer
// scope
let mut namespace = parent_namespace.clone();
let mut args_and_registers: HashMap<Ident<'sc>, VirtualRegister> = Default::default();
// evaluate every expression being passed into the function
for (name, arg) in arguments {
let return_register = register_sequencer.next();
let mut ops = check!(
convert_expression_to_asm(arg, &mut namespace, &return_register, register_sequencer),
vec![],
warnings,
errors
);
asm_buf.append(&mut ops);
args_and_registers.insert(name.clone(), return_register);
}
// insert the arguments into the asm namespace with their registers mapped
for (name, reg) in args_and_registers {
namespace.insert_variable(name, reg);
}
// evaluate the function body
let mut body = check!(
convert_code_block_to_asm(
function_body,
&mut namespace,
register_sequencer,
Some(return_register),
),
vec![],
warnings,
errors
);
asm_buf.append(&mut body);
parent_namespace.data_section = namespace.data_section;
// the return value is already put in its proper register via the above statement, so the buf
// is done
ok(asm_buf, warnings, errors)
}
/// This is similar to `convert_fn_app_to_asm()`, except instead of function arguments, this
/// takes four registers where the registers are expected to be pre-loaded with the desired values
/// when this function is jumped to.
pub(crate) fn convert_abi_fn_to_asm<'sc>(
decl: &TypedFunctionDeclaration<'sc>,
user_argument: (Ident<'sc>, VirtualRegister),
cgas: (Ident<'sc>, VirtualRegister),
bal: (Ident<'sc>, VirtualRegister),
coin_color: (Ident<'sc>, VirtualRegister),
parent_namespace: &mut AsmNamespace<'sc>,
register_sequencer: &mut RegisterSequencer,
) -> CompileResult<'sc, Vec<Op<'sc>>> {
let mut warnings = vec![];
let mut errors = vec![];
let mut asm_buf = vec![Op::new_comment(format!(
"{} abi fn",
decl.name.primary_name
))];
// Make a local namespace so that the namespace of this function does not pollute the outer
// scope
let mut namespace = parent_namespace.clone();
let return_register = register_sequencer.next();
// insert the arguments into the asm namespace with their registers mapped
namespace.insert_variable(user_argument.0, user_argument.1);
namespace.insert_variable(cgas.0, cgas.1);
namespace.insert_variable(bal.0, bal.1);
namespace.insert_variable(coin_color.0, coin_color.1);
// evaluate the function body
let mut body = check!(
convert_code_block_to_asm(
&decl.body,
&mut namespace,
register_sequencer,
Some(&return_register),
),
vec![],
warnings,
errors
);
asm_buf.append(&mut body);
// return the value from the abi function
asm_buf.push(Op {
// TODO we are just returning zero for now and not supporting return values from abi
// functions
opcode: Either::Left(VirtualOp::RET(VirtualRegister::Constant(
ConstantRegister::Zero,
))),
owning_span: None,
comment: format!("{} abi fn return", decl.name.primary_name),
});
parent_namespace.data_section = namespace.data_section;
// because we are not supporting return values right now, throw an error if the function
// returns anything.
if decl.return_type != MaybeResolvedType::Resolved(ResolvedType::Unit)
&& decl.return_type != MaybeResolvedType::Resolved(ResolvedType::ErrorRecovery)
{
errors.push(CompileError::Unimplemented(
"ABI function return values are not yet implemented",
decl.return_type_span.clone(),
));
}
// the return value is already put in its proper register via the above statement, so the buf
// is done
ok(asm_buf, warnings, errors)
}