feo3boy_opcodes/compiler/instr/
mod.rs1use std::fmt;
4use std::ops::Index;
5use std::slice::SliceIndex;
6
7use crate::compiler::instr::flow::Element;
8use crate::microcode::Microcode;
9use crate::opcode::{CBOpcode, InternalFetch, Opcode};
10
11pub mod builder;
12pub mod flow;
13
14#[derive(Debug, Copy, Clone, PartialEq, Eq, Ord, PartialOrd)]
16pub enum InstrId {
17 InternalFetch,
19 Opcode(Opcode),
21 CBOpcode(CBOpcode),
23}
24
25impl fmt::Display for InstrId {
26 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
27 match self {
28 Self::Opcode(opcode) => write!(f, "{}", opcode),
29 Self::CBOpcode(cbopcode) => write!(f, "{}", cbopcode),
30 Self::InternalFetch => write!(f, "{}", InternalFetch),
31 }
32 }
33}
34
35#[derive(Debug, Clone)]
38pub struct InstrDef {
39 id: InstrId,
41 microcode: Vec<Microcode>,
43 flow: Element,
45}
46
47impl InstrDef {
48 pub fn len(&self) -> usize {
50 self.microcode.len()
51 }
52
53 pub fn id(&self) -> InstrId {
55 self.id
56 }
57
58 pub fn flow(&self) -> &Element {
60 &self.flow
61 }
62}
63
64impl<T> Index<T> for InstrDef
66where
67 T: SliceIndex<[Microcode]>,
68{
69 type Output = <T as SliceIndex<[Microcode]>>::Output;
70
71 fn index(&self, index: T) -> &Self::Output {
72 &self.microcode[index]
73 }
74}