1use crate::{
2 DisplayHex,
3 ast::{Immediate, Instruction, InvocationTarget},
4 prettier::{Document, PrettyPrint},
5};
6
7impl PrettyPrint for Instruction {
8 fn render(&self) -> Document {
9 use crate::prettier::*;
10
11 match self {
12 Self::Nop => const_text("nop"),
13 Self::Assert => const_text("assert"),
14 Self::AssertWithError(err_code) => flatten(
15 const_text("assert.err") + const_text("=") + text(format!("\"{err_code}\"")),
16 ),
17 Self::AssertEq => const_text("assert_eq"),
18 Self::AssertEqWithError(err_code) => flatten(
19 const_text("assert_eq.err") + const_text("=") + text(format!("\"{err_code}\"")),
20 ),
21 Self::AssertEqw => const_text("assert_eqw"),
22 Self::AssertEqwWithError(err_code) => flatten(
23 const_text("assert_eqw.err") + const_text("=") + text(format!("\"{err_code}\"")),
24 ),
25 Self::Assertz => const_text("assertz"),
26 Self::AssertzWithError(err_code) => flatten(
27 const_text("assertz.err") + const_text("=") + text(format!("\"{err_code}\"")),
28 ),
29 Self::Add => const_text("add"),
30 Self::AddImm(value) => inst_with_felt_imm("add", value),
31 Self::Sub => const_text("sub"),
32 Self::SubImm(value) => inst_with_felt_imm("sub", value),
33 Self::Mul => const_text("mul"),
34 Self::MulImm(value) => inst_with_felt_imm("mul", value),
35 Self::Div => const_text("div"),
36 Self::DivImm(value) => inst_with_felt_imm("div", value),
37 Self::Neg => const_text("neg"),
38 Self::ILog2 => const_text("ilog2"),
39 Self::Inv => const_text("inv"),
40 Self::Incr => const_text("add.1"),
41 Self::Pow2 => const_text("pow2"),
42 Self::Exp => const_text("exp"),
43 Self::ExpImm(value) => inst_with_felt_imm("exp", value),
44 Self::ExpBitLength(value) => text(format!("exp.u{value}")),
45 Self::Not => const_text("not"),
46 Self::And => const_text("and"),
47 Self::Or => const_text("or"),
48 Self::Xor => const_text("xor"),
49 Self::Eq => const_text("eq"),
50 Self::EqImm(value) => inst_with_felt_imm("eq", value),
51 Self::Neq => const_text("neq"),
52 Self::NeqImm(value) => inst_with_felt_imm("neq", value),
53 Self::Eqw => const_text("eqw"),
54 Self::Lt => const_text("lt"),
55 Self::Lte => const_text("lte"),
56 Self::Gt => const_text("gt"),
57 Self::Gte => const_text("gte"),
58 Self::IsOdd => const_text("is_odd"),
59
60 Self::Ext2Add => const_text("ext2add"),
62 Self::Ext2Sub => const_text("ext2sub"),
63 Self::Ext2Mul => const_text("ext2mul"),
64 Self::Ext2Div => const_text("ext2div"),
65 Self::Ext2Neg => const_text("ext2neg"),
66 Self::Ext2Inv => const_text("ext2inv"),
67
68 Self::U32Test => const_text("u32test"),
70 Self::U32TestW => const_text("u32testw"),
71 Self::U32Assert => const_text("u32assert"),
72 Self::U32AssertWithError(err_code) => flatten(
73 const_text("u32assert.err") + const_text("=") + text(format!("\"{err_code}\"")),
74 ),
75 Self::U32Assert2 => const_text("u32assert2"),
76 Self::U32Assert2WithError(err_code) => flatten(
77 const_text("u32assert2.err") + const_text("=") + text(format!("\"{err_code}\"")),
78 ),
79 Self::U32AssertW => const_text("u32assertw"),
80 Self::U32AssertWWithError(err_code) => flatten(
81 const_text("u32assertw.err") + const_text("=") + text(format!("\"{err_code}\"")),
82 ),
83 Self::U32Split => const_text("u32split"),
84 Self::U32Cast => const_text("u32cast"),
85 Self::U32WrappingAdd => const_text("u32wrapping_add"),
86 Self::U32WrappingAddImm(value) => inst_with_imm("u32wrapping_add", value),
87 Self::U32OverflowingAdd => const_text("u32overflowing_add"),
88 Self::U32OverflowingAddImm(value) => inst_with_imm("u32overflowing_add", value),
89 Self::U32OverflowingAdd3 => const_text("u32overflowing_add3"),
90 Self::U32WrappingAdd3 => const_text("u32wrapping_add3"),
91 Self::U32WrappingSub => const_text("u32wrapping_sub"),
92 Self::U32WrappingSubImm(value) => inst_with_imm("u32wrapping_sub", value),
93 Self::U32OverflowingSub => const_text("u32overflowing_sub"),
94 Self::U32OverflowingSubImm(value) => inst_with_imm("u32overflowing_sub", value),
95 Self::U32WrappingMul => const_text("u32wrapping_mul"),
96 Self::U32WrappingMulImm(value) => inst_with_imm("u32wrapping_mul", value),
97 Self::U32OverflowingMul => const_text("u32overflowing_mul"),
98 Self::U32OverflowingMulImm(value) => inst_with_imm("u32overflowing_mul", value),
99 Self::U32OverflowingMadd => const_text("u32overflowing_madd"),
100 Self::U32WrappingMadd => const_text("u32wrapping_madd"),
101 Self::U32Div => const_text("u32div"),
102 Self::U32DivImm(value) => inst_with_imm("u32div", value),
103 Self::U32Mod => const_text("u32mod"),
104 Self::U32ModImm(value) => inst_with_imm("u32mod", value),
105 Self::U32DivMod => const_text("u32divmod"),
106 Self::U32DivModImm(value) => inst_with_imm("u32divmod", value),
107 Self::U32And => const_text("u32and"),
108 Self::U32Or => const_text("u32or"),
109 Self::U32Xor => const_text("u32xor"),
110 Self::U32Not => const_text("u32not"),
111 Self::U32Shr => const_text("u32shr"),
112 Self::U32ShrImm(value) => inst_with_imm("u32shr", value),
113 Self::U32Shl => const_text("u32shl"),
114 Self::U32ShlImm(value) => inst_with_imm("u32shl", value),
115 Self::U32Rotr => const_text("u32rotr"),
116 Self::U32RotrImm(value) => inst_with_imm("u32rotr", value),
117 Self::U32Rotl => const_text("u32rotl"),
118 Self::U32RotlImm(value) => inst_with_imm("u32rotl", value),
119 Self::U32Popcnt => const_text("u32popcnt"),
120 Self::U32Clz => const_text("u32clz"),
121 Self::U32Ctz => const_text("u32ctz"),
122 Self::U32Clo => const_text("u32clo"),
123 Self::U32Cto => const_text("u32cto"),
124 Self::U32Lt => const_text("u32lt"),
125 Self::U32Lte => const_text("u32lte"),
126 Self::U32Gt => const_text("u32gt"),
127 Self::U32Gte => const_text("u32gte"),
128 Self::U32Min => const_text("u32min"),
129 Self::U32Max => const_text("u32max"),
130
131 Self::Drop => const_text("drop"),
133 Self::DropW => const_text("dropw"),
134 Self::PadW => const_text("padw"),
135 Self::Dup0 => const_text("dup.0"),
136 Self::Dup1 => const_text("dup.1"),
137 Self::Dup2 => const_text("dup.2"),
138 Self::Dup3 => const_text("dup.3"),
139 Self::Dup4 => const_text("dup.4"),
140 Self::Dup5 => const_text("dup.5"),
141 Self::Dup6 => const_text("dup.6"),
142 Self::Dup7 => const_text("dup.7"),
143 Self::Dup8 => const_text("dup.8"),
144 Self::Dup9 => const_text("dup.9"),
145 Self::Dup10 => const_text("dup.10"),
146 Self::Dup11 => const_text("dup.11"),
147 Self::Dup12 => const_text("dup.12"),
148 Self::Dup13 => const_text("dup.13"),
149 Self::Dup14 => const_text("dup.14"),
150 Self::Dup15 => const_text("dup.15"),
151 Self::DupW0 => const_text("dupw.0"),
152 Self::DupW1 => const_text("dupw.1"),
153 Self::DupW2 => const_text("dupw.2"),
154 Self::DupW3 => const_text("dupw.3"),
155 Self::Swap1 => const_text("swap.1"),
156 Self::Swap2 => const_text("swap.2"),
157 Self::Swap3 => const_text("swap.3"),
158 Self::Swap4 => const_text("swap.4"),
159 Self::Swap5 => const_text("swap.5"),
160 Self::Swap6 => const_text("swap.6"),
161 Self::Swap7 => const_text("swap.7"),
162 Self::Swap8 => const_text("swap.8"),
163 Self::Swap9 => const_text("swap.9"),
164 Self::Swap10 => const_text("swap.10"),
165 Self::Swap11 => const_text("swap.11"),
166 Self::Swap12 => const_text("swap.12"),
167 Self::Swap13 => const_text("swap.13"),
168 Self::Swap14 => const_text("swap.14"),
169 Self::Swap15 => const_text("swap.15"),
170 Self::SwapW1 => const_text("swapw.1"),
171 Self::SwapW2 => const_text("swapw.2"),
172 Self::SwapW3 => const_text("swapw.3"),
173 Self::SwapDw => const_text("swapdw"),
174 Self::MovUp2 => const_text("movup.2"),
175 Self::MovUp3 => const_text("movup.3"),
176 Self::MovUp4 => const_text("movup.4"),
177 Self::MovUp5 => const_text("movup.5"),
178 Self::MovUp6 => const_text("movup.6"),
179 Self::MovUp7 => const_text("movup.7"),
180 Self::MovUp8 => const_text("movup.8"),
181 Self::MovUp9 => const_text("movup.9"),
182 Self::MovUp10 => const_text("movup.10"),
183 Self::MovUp11 => const_text("movup.11"),
184 Self::MovUp12 => const_text("movup.12"),
185 Self::MovUp13 => const_text("movup.13"),
186 Self::MovUp14 => const_text("movup.14"),
187 Self::MovUp15 => const_text("movup.15"),
188 Self::MovUpW2 => const_text("movupw.2"),
189 Self::MovUpW3 => const_text("movupw.3"),
190 Self::MovDn2 => const_text("movdn.2"),
191 Self::MovDn3 => const_text("movdn.3"),
192 Self::MovDn4 => const_text("movdn.4"),
193 Self::MovDn5 => const_text("movdn.5"),
194 Self::MovDn6 => const_text("movdn.6"),
195 Self::MovDn7 => const_text("movdn.7"),
196 Self::MovDn8 => const_text("movdn.8"),
197 Self::MovDn9 => const_text("movdn.9"),
198 Self::MovDn10 => const_text("movdn.10"),
199 Self::MovDn11 => const_text("movdn.11"),
200 Self::MovDn12 => const_text("movdn.12"),
201 Self::MovDn13 => const_text("movdn.13"),
202 Self::MovDn14 => const_text("movdn.14"),
203 Self::MovDn15 => const_text("movdn.15"),
204 Self::MovDnW2 => const_text("movdnw.2"),
205 Self::MovDnW3 => const_text("movdnw.3"),
206 Self::Reversew => const_text("reversew"),
207 Self::Reversedw => const_text("reversedw"),
208 Self::CSwap => const_text("cswap"),
209 Self::CSwapW => const_text("cswapw"),
210 Self::CDrop => const_text("cdrop"),
211 Self::CDropW => const_text("cdropw"),
212
213 Self::Push(value) => inst_with_imm("push", value),
215 Self::PushSlice(value, range) => flatten(
216 const_text("push.")
217 + value.render()
218 + const_text("[")
219 + display(range.start)
220 + const_text("..")
221 + display(range.end)
222 + const_text("]"),
223 ),
224 Self::PushFeltList(values) => inst_with_pretty_felt_params("push", values),
225 Self::Locaddr(value) => inst_with_imm("locaddr", value),
226 Self::Sdepth => const_text("sdepth"),
227 Self::Caller => const_text("caller"),
228 Self::Clk => const_text("clk"),
229
230 Self::MemLoad => const_text("mem_load"),
231 Self::MemLoadImm(value) => inst_with_imm("mem_load", value),
232 Self::MemLoadW => const_text("mem_loadw"),
233 Self::MemLoadWImm(value) => inst_with_imm("mem_loadw", value),
234 Self::MemLoadWBe => const_text("mem_loadw_be"),
235 Self::MemLoadWBeImm(value) => inst_with_imm("mem_loadw_be", value),
236 Self::MemLoadWLe => const_text("mem_loadw_le"),
237 Self::MemLoadWLeImm(value) => inst_with_imm("mem_loadw_le", value),
238 Self::LocLoad(value) => inst_with_imm("loc_load", value),
239 Self::LocLoadW(value) => inst_with_imm("loc_loadw", value),
240
241 Self::MemStore => const_text("mem_store"),
242 Self::MemStoreImm(value) => inst_with_imm("mem_store", value),
243 Self::MemStoreW => const_text("mem_storew"),
244 Self::MemStoreWImm(value) => inst_with_imm("mem_storew", value),
245 Self::MemStoreWBe => const_text("mem_storew_be"),
246 Self::MemStoreWBeImm(value) => inst_with_imm("mem_storew_be", value),
247 Self::MemStoreWLe => const_text("mem_storew_le"),
248 Self::MemStoreWLeImm(value) => inst_with_imm("mem_storew_le", value),
249 Self::LocStore(value) => inst_with_imm("loc_store", value),
250 Self::LocStoreW(value) => inst_with_imm("loc_storew", value),
251
252 Self::MemStream => const_text("mem_stream"),
253 Self::AdvPipe => const_text("adv_pipe"),
254
255 Self::AdvPush(value) => inst_with_imm("adv_push", value),
256 Self::AdvLoadW => const_text("adv_loadw"),
257
258 Self::SysEvent(sys_event) => inst_with_imm("adv", sys_event),
259
260 Self::Hash => const_text("hash"),
262 Self::HMerge => const_text("hmerge"),
263 Self::HPerm => const_text("hperm"),
264 Self::MTreeGet => const_text("mtree_get"),
265 Self::MTreeSet => const_text("mtree_set"),
266 Self::MTreeMerge => const_text("mtree_merge"),
267 Self::MTreeVerify => const_text("mtree_verify"),
268 Self::MTreeVerifyWithError(err_code) => flatten(
269 const_text("mtree_verify.err") + const_text("=") + text(format!("\"{err_code}\"")),
270 ),
271
272 Self::FriExt2Fold4 => const_text("fri_ext2fold4"),
274 Self::HornerBase => const_text("horner_eval_base"),
275 Self::HornerExt => const_text("horner_eval_ext"),
276 Self::EvalCircuit => const_text("eval_circuit"),
277
278 Self::Exec(InvocationTarget::MastRoot(root)) => flatten(
280 const_text("exec")
281 + const_text(".")
282 + text(format!("{:#x}", DisplayHex(root.as_bytes().as_slice()))),
283 ),
284 Self::Exec(InvocationTarget::ProcedureName(name)) => {
285 flatten(const_text("exec") + const_text(".") + text(name))
286 },
287 Self::Exec(InvocationTarget::ProcedurePath { name, module }) => {
288 const_text("exec") + const_text(".") + text(format!("{module}::{name}"))
289 },
290 Self::Exec(InvocationTarget::AbsoluteProcedurePath { name, path }) => {
291 const_text("exec") + const_text(".") + text(format!("::{path}::{name}"))
292 },
293 Self::Call(InvocationTarget::MastRoot(root)) => {
294 const_text("call")
295 + const_text(".")
296 + text(format!("{:#x}", DisplayHex(root.as_bytes().as_slice())))
297 },
298 Self::Call(InvocationTarget::ProcedureName(name)) => {
299 flatten(const_text("call") + const_text(".") + text(name))
300 },
301 Self::Call(InvocationTarget::ProcedurePath { name, module }) => {
302 const_text("call") + const_text(".") + text(format!("{module}::{name}"))
303 },
304 Self::Call(InvocationTarget::AbsoluteProcedurePath { name, path }) => {
305 const_text("call") + const_text(".") + text(format!("::{path}::{name}"))
306 },
307 Self::SysCall(InvocationTarget::MastRoot(root)) => {
308 const_text("syscall")
309 + const_text(".")
310 + text(format!("{:#x}", DisplayHex(root.as_bytes().as_slice())))
311 },
312 Self::SysCall(InvocationTarget::ProcedureName(name)) => {
313 flatten(const_text("syscall") + const_text(".") + text(format!("{name}")))
314 },
315 Self::SysCall(InvocationTarget::ProcedurePath { name, module }) => {
316 const_text("syscall") + const_text(".") + text(format!("{module}::{name}"))
317 },
318 Self::SysCall(InvocationTarget::AbsoluteProcedurePath { name, path }) => {
319 const_text("syscall") + const_text(".") + text(format!("::{path}::{name}"))
320 },
321 Self::DynExec => const_text("dynexec"),
322 Self::DynCall => const_text("dyncall"),
323 Self::ProcRef(InvocationTarget::MastRoot(_)) => {
324 panic!("invalid procref instruction: expected name not MAST root")
325 },
326 Self::ProcRef(InvocationTarget::ProcedureName(name)) => {
327 flatten(const_text("procref") + const_text(".") + text(name))
328 },
329 Self::ProcRef(InvocationTarget::ProcedurePath { name, module }) => {
330 flatten(const_text("procref") + const_text(".") + text(format!("{module}::{name}")))
331 },
332 Self::ProcRef(InvocationTarget::AbsoluteProcedurePath { name, path }) => {
333 flatten(const_text("procref") + const_text(".") + text(format!("::{path}::{name}")))
334 },
335
336 Self::Breakpoint => const_text("breakpoint"),
338 Self::Debug(options) => inst_with_imm("debug", options),
339
340 Self::Emit => const_text("emit"),
342 Self::EmitImm(value) => inst_with_felt_imm("emit", value),
343 Self::Trace(value) => inst_with_imm("trace", value),
344 }
345 }
346}
347
348fn inst_with_imm(name: &'static str, imm: &dyn PrettyPrint) -> Document {
349 use crate::prettier::*;
350
351 let imm = imm.render();
352
353 flatten(const_text(name) + const_text(".") + imm)
354}
355
356fn inst_with_felt_imm(name: &'static str, imm: &Immediate<crate::Felt>) -> Document {
357 use crate::prettier::*;
358
359 let value = match imm {
360 Immediate::Value(value) => display(*value),
361 Immediate::Constant(name) => text(name),
362 };
363
364 flatten(const_text(name) + const_text(".") + value)
365}
366
367fn inst_with_pretty_felt_params(inst: &'static str, params: &[crate::Felt]) -> Document {
368 use crate::prettier::*;
369
370 let single_line = text(inst)
371 + const_text(".")
372 + params
373 .iter()
374 .copied()
375 .map(display)
376 .reduce(|acc, doc| acc + const_text(".") + doc)
377 .unwrap_or_default();
378
379 let multi_line = params
380 .iter()
381 .copied()
382 .map(|v| text(inst) + const_text(".") + display(v))
383 .reduce(|acc, doc| acc + nl() + doc)
384 .unwrap_or_default();
385 single_line | multi_line
386}
387
388#[cfg(test)]
392mod tests {
393 use miden_core::crypto::hash::Rpo256;
394 use miden_debug_types::Span;
395
396 use crate::{Felt, ast::*};
397
398 #[test]
399 fn test_instruction_display() {
400 let instruction = format!("{}", Instruction::Assert);
401 assert_eq!("assert", instruction);
402
403 let instruction = format!("{}", Instruction::Add);
404 assert_eq!("add", instruction);
405
406 let instruction = format!("{}", Instruction::AddImm(Felt::new(5).into()));
407 assert_eq!("add.5", instruction);
408
409 let instruction = format!("{}", Instruction::ExpBitLength(32));
410 assert_eq!("exp.u32", instruction);
411
412 let instruction = format!(
413 "{}",
414 Instruction::PushFeltList(vec![Felt::new(3), Felt::new(4), Felt::new(8), Felt::new(9)])
415 );
416 assert_eq!("push.3.4.8.9", instruction);
417 let instruction = format!(
418 "{}",
419 Instruction::Push(Immediate::Value(miden_debug_types::Span::unknown(
420 crate::parser::PushValue::Int(crate::parser::IntValue::U8(3))
421 )))
422 );
423 assert_eq!("push.3", instruction);
424
425 let digest = Rpo256::hash(b"std::math::u64::add");
426 let target = InvocationTarget::MastRoot(Span::unknown(digest));
427 let instruction = format!("{}", Instruction::Exec(target));
428 assert_eq!(
429 "exec.0x90b3926941061b28638b6cc0bbdb3bcb335e834dc9ab8044250875055202d2fe",
430 instruction
431 );
432 }
433}