Skip to main content

vihaco_cpu/
display.rs

1// SPDX-FileCopyrightText: 2026 The vihaco Authors
2// SPDX-License-Identifier: MIT
3
4use crate::instruction::Instruction;
5use vihaco::color::{Themed, show_instruction};
6
7impl std::fmt::Display for Instruction {
8    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
9        use Instruction::*;
10        match self {
11            Span(file, start, end) => {
12                show_instruction!(
13                    f,
14                    "span ",
15                    file,
16                    " ",
17                    format!("0x{:X}", start),
18                    " ",
19                    format!("0x{:X}", end)
20                )
21            }
22            Label => show_instruction!(f, "label"),
23            FunctionStart => show_instruction!(f, "function_start"),
24            FunctionEnd => show_instruction!(f, "function_end"),
25            Breakpoint => show_instruction!(f, "breakpoint"),
26            Branch(target) => {
27                show_instruction!(f, "br ", format!("0x{:X}", target))
28            }
29            ConditionalBranch(true_target, false_target) => {
30                show_instruction!(
31                    f,
32                    "br_if ",
33                    format!("0x{:X}", true_target),
34                    " ",
35                    format!("0x{:X}", false_target)
36                )
37            }
38            Return(keep) => show_instruction!(f, "ret ", keep),
39            Call(arity, target) => {
40                show_instruction!(f, "call ", arity, " ", format!("0x{:X}", target))
41            }
42            IndirectCall => show_instruction!(f, "indirect_call"),
43            Halt => show_instruction!(f, "halt"),
44            Print => show_instruction!(f, "print"),
45            Load(ty, addr) => {
46                show_instruction!(f, "load ", ty, " ", format!("0x{:X}", addr))
47            }
48            Store(ty, addr) => {
49                show_instruction!(f, "store ", ty, " ", format!("0x{:X}", addr))
50            }
51            Dup => show_instruction!(f, "dup"),
52            HeapAlloc(n_elements) => show_instruction!(f, "heap_alloc ", n_elements),
53            GetItem => show_instruction!(f, "get_item"),
54            HeapDealloc => show_instruction!(f, "heap_dealloc"),
55            Const(v) => show_instruction!(f, "const.", v.type_of(), " ", v),
56            Add(ty) => show_instruction!(f, "add.", ty),
57            Sub(ty) => show_instruction!(f, "sub.", ty),
58            Mul(ty) => show_instruction!(f, "mul.", ty),
59            Div(ty) => show_instruction!(f, "div.", ty),
60            Rem(ty) => show_instruction!(f, "rem.", ty),
61            Neg(ty) => show_instruction!(f, "neg.", ty),
62            Shl(ty) => show_instruction!(f, "shl.", ty),
63            Shr(ty) => show_instruction!(f, "shr.", ty),
64            Rol(ty) => show_instruction!(f, "rol.", ty),
65            Ror(ty) => show_instruction!(f, "ror.", ty),
66            BitAnd(ty) => show_instruction!(f, "and.", ty),
67            BitOr(ty) => show_instruction!(f, "or.", ty),
68            BitXor(ty) => show_instruction!(f, "xor.", ty),
69
70            Not => show_instruction!(f, "not"),
71            And => show_instruction!(f, "and"),
72            Or => show_instruction!(f, "or"),
73            Xor => show_instruction!(f, "xor"),
74
75            Eq(ty) => show_instruction!(f, "eq.", ty),
76            Ne(ty) => show_instruction!(f, "ne.", ty),
77            Lt(ty) => show_instruction!(f, "lt.", ty),
78            Gt(ty) => show_instruction!(f, "gt.", ty),
79            Le(ty) => show_instruction!(f, "le.", ty),
80            Ge(ty) => show_instruction!(f, "ge.", ty),
81        }
82        Ok(())
83    }
84}