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
use miden_assembly_syntax::parser::WordValue;
use midenc_dialect_hir::assertions;
use midenc_hir::{
Felt, Immediate, SourceSpan, Type,
dialects::builtin::attributes::{ArgumentExtension, Signature},
};
use super::{OpEmitter, int64, masm};
use crate::TraceEvent;
impl OpEmitter<'_> {
/// Format a diagnostic message for a HIR assertion code when one is available.
fn assertion_message(code: Option<u32>, default: impl Into<String>) -> String {
let default = default.into();
match code.filter(|code| *code != 0) {
Some(assertions::ASSERT_FAILED_ALIGNMENT) => {
"pointer address does not meet minimum alignment for the type".into()
}
Some(code) => format!("{default} (assertion code 0x{code:08x})"),
None => default,
}
}
/// Assert that an integer value on the stack has the value 1
///
/// This operation consumes the input value.
pub fn assert(&mut self, code: Option<u32>, span: SourceSpan) {
let arg = self.stack.pop().expect("operand stack is empty");
let ty = arg.ty().clone();
let message = Self::assertion_message(code, format!("expected {ty} value to equal 1"));
match ty {
Type::Felt
| Type::U32
| Type::I32
| Type::U16
| Type::I16
| Type::U8
| Type::I8
| Type::I1 => {
self.emit(Self::assert_with_message_inst(message, span), span);
}
Type::I128 | Type::U128 => {
self.emit_all(
[
masm::Instruction::Push(masm::Immediate::Value(masm::Span::new(
span,
WordValue([Felt::ZERO, Felt::ZERO, Felt::ZERO, Felt::ONE]).into(),
))),
Self::assert_eqw_with_message_inst(message, span),
],
span,
);
}
Type::U64 | Type::I64 => {
self.emit_all(
[
Self::assertz_with_message_inst(message.clone(), span),
Self::assert_with_message_inst(message, span),
],
span,
);
}
ty if !ty.is_integer() => {
panic!("invalid argument to assert: expected integer, got {ty}")
}
ty => unimplemented!("support for assert on {ty} is not implemented"),
}
}
/// Assert that an integer value on the stack has the value 0
///
/// This operation consumes the input value.
pub fn assertz(&mut self, code: Option<u32>, span: SourceSpan) {
let arg = self.stack.pop().expect("operand stack is empty");
let ty = arg.ty().clone();
let message = Self::assertion_message(code, format!("expected {ty} value to equal 0"));
match ty {
Type::Felt
| Type::U32
| Type::I32
| Type::U16
| Type::I16
| Type::U8
| Type::I8
| Type::I1 => {
self.emit(Self::assertz_with_message_inst(message, span), span);
}
Type::U64 | Type::I64 => {
self.emit_all(
[
Self::assertz_with_message_inst(message.clone(), span),
Self::assertz_with_message_inst(message, span),
],
span,
);
}
Type::U128 | Type::I128 => {
self.emit_all(
[
masm::Instruction::Push(masm::Immediate::Value(masm::Span::new(
span,
WordValue([Felt::ZERO; 4]).into(),
))),
Self::assert_eqw_with_message_inst(message, span),
],
span,
);
}
ty if !ty.is_integer() => {
panic!("invalid argument to assertz: expected integer, got {ty}")
}
ty => unimplemented!("support for assertz on {ty} is not implemented"),
}
}
/// Assert that the top two integer values on the stack have the same value
///
/// This operation consumes the input values.
pub fn assert_eq(&mut self, code: Option<u32>, span: SourceSpan) {
let rhs = self.pop().expect("operand stack is empty");
let lhs = self.pop().expect("operand stack is empty");
let ty = lhs.ty().clone();
assert_eq!(ty, rhs.ty(), "expected assert_eq operands to have the same type");
let message = Self::assertion_message(code, format!("expected {ty} values to be equal"));
match ty {
Type::Felt
| Type::U32
| Type::I32
| Type::U16
| Type::I16
| Type::U8
| Type::I8
| Type::I1 => {
self.emit(Self::assert_eq_with_message_inst(message, span), span);
}
Type::U128 | Type::I128 => {
self.emit(Self::assert_eqw_with_message_inst(message, span), span)
}
Type::U64 | Type::I64 => {
self.emit_all(
[
// compare the hi bits
masm::Instruction::MovUp2,
Self::assert_eq_with_message_inst(message.clone(), span),
// compare the low bits
Self::assert_eq_with_message_inst(message, span),
],
span,
);
}
ty if !ty.is_integer() => {
panic!("invalid argument to assert_eq: expected integer, got {ty}")
}
ty => unimplemented!("support for assert_eq on {ty} is not implemented"),
}
}
/// Emit code to assert that an integer value on the stack has the same value
/// as the provided immediate.
///
/// This operation consumes the input value.
#[allow(unused)]
pub fn assert_eq_imm(&mut self, imm: Immediate, span: SourceSpan) {
let lhs = self.pop().expect("operand stack is empty");
let ty = lhs.ty().clone();
let message = format!("expected {ty} value to equal {imm}");
assert_eq!(ty, imm.ty(), "expected assert_eq_imm operands to have the same type");
match ty {
Type::Felt
| Type::U32
| Type::I32
| Type::U16
| Type::I16
| Type::U8
| Type::I8
| Type::I1 => {
self.emit_all(
[
masm::Instruction::EqImm(imm.as_felt().unwrap().into()),
Self::assert_with_message_inst(message, span),
],
span,
);
}
Type::I128 | Type::U128 => {
self.push_immediate(imm, span);
self.emit(Self::assert_eqw_with_message_inst(message, span), span)
}
Type::I64 | Type::U64 => {
let imm = match imm {
Immediate::I64(i) => i as u64,
Immediate::U64(i) => i,
_ => unreachable!(),
};
let (hi, lo) = int64::to_raw_parts(imm);
self.emit_all(
[
masm::Instruction::EqImm(Felt::new(hi as u64).into()),
Self::assert_with_message_inst(message.clone(), span),
masm::Instruction::EqImm(Felt::new(lo as u64).into()),
Self::assert_with_message_inst(message, span),
],
span,
)
}
ty if !ty.is_integer() => {
panic!("invalid argument to assert_eq: expected integer, got {ty}")
}
ty => unimplemented!("support for assert_eq on {ty} is not implemented"),
}
}
/// Emit code to select between two values of the same type, based on a boolean condition.
///
/// The semantics of this instruction are basically the same as Miden's `cdrop` instruction,
/// but with support for selecting between any of the representable integer/pointer types as
/// values. Given three values on the operand stack (in order of appearance), `c`, `b`, and
/// `a`:
///
/// * Pop `c` from the stack. This value must be an i1/boolean, or execution will trap.
/// * Pop `b` and `a` from the stack, and push back `b` if `c` is true, or `a` if `c` is false.
///
/// This operation will assert that the selected value is a valid value for the given type.
pub fn select(&mut self, span: SourceSpan) {
let c = self.stack.pop().expect("operand stack is empty");
let b = self.stack.pop().expect("operand stack is empty");
let a = self.stack.pop().expect("operand stack is empty");
assert_eq!(c.ty(), Type::I1, "expected selector operand to be an i1");
let ty = a.ty();
assert_eq!(ty, b.ty(), "expected selections to be of the same type");
match &ty {
Type::Felt
| Type::U32
| Type::I32
| Type::U16
| Type::I16
| Type::U8
| Type::I8
| Type::I1 => self.emit(masm::Instruction::CDrop, span),
Type::I128 | Type::U128 => self.emit(masm::Instruction::CDropW, span),
Type::I64 | Type::U64 => {
// Perform two conditional drops, one for each 32-bit limb
// corresponding to the value which is being selected
self.emit_all(
[
// stack starts as [c, b_hi, b_lo, a_hi, a_lo]
masm::Instruction::Dup0, // [c, c, b_hi, b_lo, a_hi, a_lo]
masm::Instruction::MovDn5, // [c, b_hi, b_lo, a_hi, a_lo, c]
masm::Instruction::MovUp3, // [a_hi, c, b_hi, b_lo, a_lo, c]
masm::Instruction::MovUp2, // [b_hi, a_hi, c, b_lo, a_lo, c]
masm::Instruction::MovUp5, // [c, b_hi, a_hi, c, b_lo, a_lo]
masm::Instruction::CDrop, // [d_hi, c, b_lo, a_lo]
masm::Instruction::MovDn3, // [c, b_lo, a_lo, d_hi]
masm::Instruction::CDrop, // [d_lo, d_hi]
masm::Instruction::Swap1, // [d_hi, d_lo]
],
span,
);
}
ty if !ty.is_integer() => {
panic!("invalid argument to assert_eq: expected integer, got {ty}")
}
ty => unimplemented!("support for assert_eq on {ty} is not implemented"),
}
self.push(ty);
}
/// Execute the given procedure.
///
/// A function called using this operation is invoked in the same memory context as the caller.
pub fn exec(
&mut self,
callee: masm::InvocationTarget,
signature: &Signature,
span: SourceSpan,
) {
self.process_call_signature(&callee, signature, span);
self.emit(masm::Instruction::Trace(TraceEvent::FrameStart.as_u32().into()), span);
self.emit(masm::Instruction::Nop, span);
self.emit(masm::Instruction::Exec(callee), span);
self.emit(masm::Instruction::Trace(TraceEvent::FrameEnd.as_u32().into()), span);
self.emit(masm::Instruction::Nop, span);
}
/// Execute the given procedure in a new context.
///
/// A function called using this operation is invoked in a new memory context.
pub fn call(
&mut self,
callee: masm::InvocationTarget,
signature: &Signature,
span: SourceSpan,
) {
self.process_call_signature(&callee, signature, span);
self.emit(masm::Instruction::Trace(TraceEvent::FrameStart.as_u32().into()), span);
self.emit(masm::Instruction::Nop, span);
self.emit(masm::Instruction::Call(callee), span);
self.emit(masm::Instruction::Trace(TraceEvent::FrameEnd.as_u32().into()), span);
self.emit(masm::Instruction::Nop, span);
}
fn process_call_signature(
&mut self,
callee: &masm::InvocationTarget,
signature: &Signature,
span: SourceSpan,
) {
for i in 0..signature.arity() {
let param = &signature.params[i];
let arg = self.stack.pop().expect("operand stack is empty");
let ty = arg.ty();
// Validate the purpose matches
if param.is_sret_param() {
assert_eq!(
i, 0,
"invalid function signature: sret parameters must be the first parameter, and \
only one sret parameter is allowed"
);
assert_eq!(
signature.results.len(),
0,
"invalid function signature: a function with sret parameters cannot also have \
results"
);
assert!(
ty.is_pointer(),
"invalid exec to {callee}: invalid argument for sret parameter, expected {}, \
got {ty}",
¶m.ty
);
}
// Validate that the argument type is valid for the parameter ABI
match param.extension() {
// Types must match exactly
ArgumentExtension::None => {
assert_eq!(
ty, param.ty,
"invalid call to {callee}: invalid argument type for parameter at index \
{i}"
);
}
// Caller can provide a smaller type which will be zero-extended to the expected
// type
//
// However, the argument must be an unsigned integer, and of smaller or equal size
// in order for the types to differ
ArgumentExtension::Zext if ty != param.ty => {
assert!(
param.ty.is_unsigned_integer(),
"invalid function signature: zero-extension is only valid for unsigned \
integer types"
);
assert!(
ty.is_unsigned_integer(),
"invalid call to {callee}: invalid argument type for parameter at index \
{i}, expected unsigned integer type, got {ty}"
);
let expected_size = param.ty.size_in_bits();
let provided_size = param.ty.size_in_bits();
assert!(
provided_size <= expected_size,
"invalid call to {callee}: invalid argument type for parameter at index \
{i}, expected integer width to be <= {expected_size} bits"
);
// Zero-extend this argument
self.stack.push(arg);
self.zext(¶m.ty, span);
self.stack.drop();
}
// Caller can provide a smaller type which will be sign-extended to the expected
// type
//
// However, the argument must be an integer which can fit in the range of the
// expected type
ArgumentExtension::Sext if ty != param.ty => {
assert!(
param.ty.is_signed_integer(),
"invalid function signature: sign-extension is only valid for signed \
integer types"
);
assert!(
ty.is_integer(),
"invalid call to {callee}: invalid argument type for parameter at index \
{i}, expected integer type, got {ty}"
);
let expected_size = param.ty.size_in_bits();
let provided_size = param.ty.size_in_bits();
if ty.is_unsigned_integer() {
assert!(
provided_size < expected_size,
"invalid call to {callee}: invalid argument type for parameter at \
index {i}, expected unsigned integer width to be < {expected_size} \
bits"
);
} else {
assert!(
provided_size <= expected_size,
"invalid call to {callee}: invalid argument type for parameter at \
index {i}, expected integer width to be <= {expected_size} bits"
);
}
// Push the operand back on the stack for `sext`
self.stack.push(arg);
self.sext(¶m.ty, span);
self.stack.drop();
}
ArgumentExtension::Zext | ArgumentExtension::Sext => (),
}
}
for result in signature.results.iter().rev() {
self.push(result.ty.clone());
}
}
}