datex_core/instruction/
mod.rs1pub mod instruction_codes;
3pub mod instruction_data;
4pub mod regular_instruction;
5pub mod traits;
6pub mod type_instruction;
7pub mod type_instruction_codes;
8
9use crate::{
10 instruction::{
11 regular_instruction::RegularInstruction,
12 type_instruction::TypeInstruction,
13 },
14 prelude::*,
15};
16mod serializeable;
17use binrw::{
18 BinResult, BinWrite, Endian,
19 io::{Seek, Write},
20 meta::{EndianKind, WriteEndian},
21};
22use core::fmt::Display;
23use serde::Serialize;
24mod bytecode_item;
25pub use bytecode_item::{BytecodeItem, Label};
26
27#[derive(Clone, Debug, PartialEq)]
28#[cfg_attr(feature = "disassembler", derive(Serialize))]
29#[cfg_attr(feature = "disassembler", serde(untagged))]
30pub enum Instruction {
31 Regular(RegularInstruction),
33 Type(TypeInstruction),
35}
36
37impl Instruction {
38 pub fn get_next_expected_instructions(&self) -> NextExpectedInstructions {
39 match self {
40 Instruction::Regular(instr) => {
41 instr.get_next_expected_instructions()
42 }
43 Instruction::Type(instr) => instr.get_next_expected_instructions(),
44 }
45 }
46
47 pub fn metadata_string(&self) -> Option<String> {
48 match self {
49 Instruction::Regular(instr) => instr.metadata_string(),
50 Instruction::Type(instr) => instr.metadata_string(),
51 }
52 }
53}
54
55impl BinWrite for Instruction {
56 type Args<'a> = ();
57
58 fn write_options<W: Write + Seek>(
59 &self,
60 writer: &mut W,
61 endian: Endian,
62 _: Self::Args<'_>,
63 ) -> BinResult<()> {
64 match self {
65 Instruction::Regular(instr) => {
66 instr.write_options(writer, endian, ())
67 }
68 Instruction::Type(instr) => instr.write_options(writer, endian, ()),
69 }
70 }
71}
72
73impl WriteEndian for Instruction {
74 const ENDIAN: EndianKind = EndianKind::Endian(Endian::Little);
75}
76
77impl Display for Instruction {
78 fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
79 match self {
80 Instruction::Regular(instr) => {
81 write!(f, "{}", instr)
82 }
83 Instruction::Type(instr) => {
84 write!(f, "TYPE_INSTRUCTION {}", instr)
85 }
86 }
87 }
88}
89
90impl From<RegularInstruction> for Instruction {
91 fn from(instruction: RegularInstruction) -> Self {
92 Instruction::Regular(instruction)
93 }
94}
95
96impl From<TypeInstruction> for Instruction {
97 fn from(instruction: TypeInstruction) -> Self {
98 Instruction::Type(instruction)
99 }
100}
101
102pub enum NextExpectedInstructions {
103 None,
104 Regular(u32),
105 Type(u32),
106 UnboundedStart,
107 UnboundedEnd,
108 RegularAndType(u32, u32),
109}
110
111pub enum CountOrUnbounded {
112 Count(u32),
113 UnboundedStart,
114 UnboundedEnd,
115}
116
117impl NextExpectedInstructions {
118 pub fn total_count(&self) -> Option<CountOrUnbounded> {
119 match self {
120 NextExpectedInstructions::None => None,
121 NextExpectedInstructions::Regular(count) => {
122 if count == &0 {
123 None
124 } else {
125 Some(CountOrUnbounded::Count(*count))
126 }
127 }
128 NextExpectedInstructions::Type(count) => {
129 if count == &0 {
130 None
131 } else {
132 Some(CountOrUnbounded::Count(*count))
133 }
134 }
135 NextExpectedInstructions::UnboundedStart => {
136 Some(CountOrUnbounded::UnboundedStart)
137 }
138 NextExpectedInstructions::UnboundedEnd => {
139 Some(CountOrUnbounded::UnboundedEnd)
140 }
141 NextExpectedInstructions::RegularAndType(
142 regular_count,
143 type_count,
144 ) => {
145 let total_count = regular_count + type_count;
146 if total_count == 0 {
147 None
148 } else {
149 Some(CountOrUnbounded::Count(total_count))
150 }
151 }
152 }
153 }
154}
155
156#[derive(Default, Clone, Debug, Copy, PartialEq, Eq)]
157pub enum NestedInstructionResolutionStrategy {
158 #[default]
160 None,
161 ResolveNestedScopesFlat,
163 ResolveNestedScopesTree,
165}