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::MemLoadWBe => const_text("mem_loadw_be"),
233 Self::MemLoadWBeImm(value) => inst_with_imm("mem_loadw_be", value),
234 Self::MemLoadWLe => const_text("mem_loadw_le"),
235 Self::MemLoadWLeImm(value) => inst_with_imm("mem_loadw_le", value),
236 Self::LocLoad(value) => inst_with_imm("loc_load", value),
237 Self::LocLoadWBe(value) => inst_with_imm("loc_loadw_be", value),
238 Self::LocLoadWLe(value) => inst_with_imm("loc_loadw_le", value),
239
240 Self::MemStore => const_text("mem_store"),
241 Self::MemStoreImm(value) => inst_with_imm("mem_store", value),
242 Self::MemStoreWBe => const_text("mem_storew_be"),
243 Self::MemStoreWBeImm(value) => inst_with_imm("mem_storew_be", value),
244 Self::MemStoreWLe => const_text("mem_storew_le"),
245 Self::MemStoreWLeImm(value) => inst_with_imm("mem_storew_le", value),
246 Self::LocStore(value) => inst_with_imm("loc_store", value),
247 Self::LocStoreWBe(value) => inst_with_imm("loc_storew_be", value),
248 Self::LocStoreWLe(value) => inst_with_imm("loc_storew_le", value),
249
250 Self::MemStream => const_text("mem_stream"),
251 Self::AdvPipe => const_text("adv_pipe"),
252
253 Self::AdvPush(value) => inst_with_imm("adv_push", value),
254 Self::AdvLoadW => const_text("adv_loadw"),
255
256 Self::SysEvent(sys_event) => inst_with_imm("adv", sys_event),
257
258 Self::Hash => const_text("hash"),
260 Self::HMerge => const_text("hmerge"),
261 Self::HPerm => const_text("hperm"),
262 Self::MTreeGet => const_text("mtree_get"),
263 Self::MTreeSet => const_text("mtree_set"),
264 Self::MTreeMerge => const_text("mtree_merge"),
265 Self::MTreeVerify => const_text("mtree_verify"),
266 Self::MTreeVerifyWithError(err_code) => flatten(
267 const_text("mtree_verify.err") + const_text("=") + text(format!("\"{err_code}\"")),
268 ),
269
270 Self::FriExt2Fold4 => const_text("fri_ext2fold4"),
272 Self::HornerBase => const_text("horner_eval_base"),
273 Self::HornerExt => const_text("horner_eval_ext"),
274 Self::EvalCircuit => const_text("eval_circuit"),
275 Self::LogPrecompile => const_text("log_precompile"),
276
277 Self::Exec(InvocationTarget::MastRoot(root)) => flatten(
279 const_text("exec")
280 + const_text(".")
281 + text(format!("{:#x}", DisplayHex(root.as_bytes().as_slice()))),
282 ),
283 Self::Exec(InvocationTarget::ProcedureName(name)) => {
284 flatten(const_text("exec") + const_text(".") + text(name))
285 },
286 Self::Exec(InvocationTarget::ProcedurePath { name, module }) => {
287 const_text("exec") + const_text(".") + text(format!("{module}::{name}"))
288 },
289 Self::Exec(InvocationTarget::AbsoluteProcedurePath { name, path }) => {
290 const_text("exec") + const_text(".") + text(format!("::{path}::{name}"))
291 },
292 Self::Call(InvocationTarget::MastRoot(root)) => {
293 const_text("call")
294 + const_text(".")
295 + text(format!("{:#x}", DisplayHex(root.as_bytes().as_slice())))
296 },
297 Self::Call(InvocationTarget::ProcedureName(name)) => {
298 flatten(const_text("call") + const_text(".") + text(name))
299 },
300 Self::Call(InvocationTarget::ProcedurePath { name, module }) => {
301 const_text("call") + const_text(".") + text(format!("{module}::{name}"))
302 },
303 Self::Call(InvocationTarget::AbsoluteProcedurePath { name, path }) => {
304 const_text("call") + const_text(".") + text(format!("::{path}::{name}"))
305 },
306 Self::SysCall(InvocationTarget::MastRoot(root)) => {
307 const_text("syscall")
308 + const_text(".")
309 + text(format!("{:#x}", DisplayHex(root.as_bytes().as_slice())))
310 },
311 Self::SysCall(InvocationTarget::ProcedureName(name)) => {
312 flatten(const_text("syscall") + const_text(".") + text(format!("{name}")))
313 },
314 Self::SysCall(InvocationTarget::ProcedurePath { name, module }) => {
315 const_text("syscall") + const_text(".") + text(format!("{module}::{name}"))
316 },
317 Self::SysCall(InvocationTarget::AbsoluteProcedurePath { name, path }) => {
318 const_text("syscall") + const_text(".") + text(format!("::{path}::{name}"))
319 },
320 Self::DynExec => const_text("dynexec"),
321 Self::DynCall => const_text("dyncall"),
322 Self::ProcRef(InvocationTarget::MastRoot(_)) => {
323 panic!("invalid procref instruction: expected name not MAST root")
324 },
325 Self::ProcRef(InvocationTarget::ProcedureName(name)) => {
326 flatten(const_text("procref") + const_text(".") + text(name))
327 },
328 Self::ProcRef(InvocationTarget::ProcedurePath { name, module }) => {
329 flatten(const_text("procref") + const_text(".") + text(format!("{module}::{name}")))
330 },
331 Self::ProcRef(InvocationTarget::AbsoluteProcedurePath { name, path }) => {
332 flatten(const_text("procref") + const_text(".") + text(format!("::{path}::{name}")))
333 },
334
335 Self::Breakpoint => const_text("breakpoint"),
337 Self::Debug(options) => inst_with_imm("debug", options),
338
339 Self::Emit => const_text("emit"),
341 Self::EmitImm(value) => inst_with_felt_imm("emit", value),
342 Self::Trace(value) => inst_with_imm("trace", value),
343 }
344 }
345}
346
347fn inst_with_imm(name: &'static str, imm: &dyn PrettyPrint) -> Document {
348 use crate::prettier::*;
349
350 let imm = imm.render();
351
352 flatten(const_text(name) + const_text(".") + imm)
353}
354
355fn inst_with_felt_imm(name: &'static str, imm: &Immediate<crate::Felt>) -> Document {
356 use crate::prettier::*;
357
358 let value = match imm {
359 Immediate::Value(value) => display(*value),
360 Immediate::Constant(name) => text(name),
361 };
362
363 flatten(const_text(name) + const_text(".") + value)
364}
365
366fn inst_with_pretty_felt_params(inst: &'static str, params: &[crate::Felt]) -> Document {
367 use crate::prettier::*;
368
369 let single_line = text(inst)
370 + const_text(".")
371 + params
372 .iter()
373 .copied()
374 .map(display)
375 .reduce(|acc, doc| acc + const_text(".") + doc)
376 .unwrap_or_default();
377
378 let multi_line = params
379 .iter()
380 .copied()
381 .map(|v| text(inst) + const_text(".") + display(v))
382 .reduce(|acc, doc| acc + nl() + doc)
383 .unwrap_or_default();
384 single_line | multi_line
385}
386
387#[cfg(test)]
391mod tests {
392 use miden_core::crypto::hash::Rpo256;
393 use miden_debug_types::Span;
394
395 use crate::{Felt, ast::*};
396
397 #[test]
398 fn test_instruction_display() {
399 let instruction = format!("{}", Instruction::Assert);
400 assert_eq!("assert", instruction);
401
402 let instruction = format!("{}", Instruction::Add);
403 assert_eq!("add", instruction);
404
405 let instruction = format!("{}", Instruction::AddImm(Felt::new(5).into()));
406 assert_eq!("add.5", instruction);
407
408 let instruction = format!("{}", Instruction::ExpBitLength(32));
409 assert_eq!("exp.u32", instruction);
410
411 let instruction = format!(
412 "{}",
413 Instruction::PushFeltList(vec![Felt::new(3), Felt::new(4), Felt::new(8), Felt::new(9)])
414 );
415 assert_eq!("push.3.4.8.9", instruction);
416 let instruction = format!(
417 "{}",
418 Instruction::Push(Immediate::Value(miden_debug_types::Span::unknown(
419 crate::parser::PushValue::Int(crate::parser::IntValue::U8(3))
420 )))
421 );
422 assert_eq!("push.3", instruction);
423
424 let digest = Rpo256::hash(b"std::math::u64::add");
425 let target = InvocationTarget::MastRoot(Span::unknown(digest));
426 let instruction = format!("{}", Instruction::Exec(target));
427 assert_eq!(
428 "exec.0x90b3926941061b28638b6cc0bbdb3bcb335e834dc9ab8044250875055202d2fe",
429 instruction
430 );
431 }
432}