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