1use crate::{
2 disassembler::{
3 InstructionTree, disassemble_body, disassemble_body_to_string,
4 disassemble_instruction_tree_to_string, get_instruction_tree_from_list,
5 options::DisassemblerOptions,
6 },
7 dxb_parser::body::InstructionWithSpan,
8 instruction::{
9 CountOrUnbounded, Instruction, NestedInstructionResolutionStrategy,
10 regular_instruction::RegularInstruction,
11 },
12 prelude::*,
13};
14use core::slice::Iter;
15
16pub macro assert_instructions_equal {
17 ($dxb:expr, ($($expr:expr),* $(,)?)) => {{
18 use $crate::disassembler::assertions::{resolve_instructions, assert_instruction_lists_eq};
19 use $crate::disassembler::{InstructionTree};
20 use $crate::instruction::Instruction;
21
22 let dxb = $dxb;
23 assert_instruction_lists_eq(
24 resolve_instructions(dxb),
25 InstructionTree::<Instruction>::from(vec![$(InstructionTree::<Instruction>::from($expr),)*]).flatten_instructions(),
26 dxb,
27 );
28 }},
29 ($dxb:expr, $vec:expr $(,)?) => {{
30 use $crate::disassembler::assertions::{resolve_instructions, assert_instruction_lists_eq};
31 use $crate::disassembler::{InstructionTree};
32 use $crate::instruction::Instruction;
33
34 let dxb = $dxb;
35 assert_instruction_lists_eq(
36 resolve_instructions(dxb),
37 InstructionTree::<Instruction>::from($vec.into_iter().map(|i| i.into()).collect::<Vec<_>>()).flatten_instructions(),
38 dxb,
39 );
40 }}
41}
42
43pub macro assert_instruction_lists_eq {
46 ($output:expr, ($($expected:expr),* $(,)?)) => {{
47 use $crate::disassembler::assertions::assert_instruction_lists_eq;
48 use $crate::core_compiler::value_visitor::compile_instruction_iterator_to_dxb;
49 let output_instructions = $output;
50 let output_dxb = compile_instruction_iterator_to_dxb(
51 output_instructions.iter().cloned(),
52 );
53 let expected_instructions = InstructionTree::<Instruction>::from(vec![$($expected),*]).flatten_instructions();
54 assert_instruction_lists_eq(output_instructions, expected_instructions, &output_dxb);
55 }},
56 ($output:expr, $vec:expr $(,)?) => {{
57 use $crate::disassembler::assertions::assert_instruction_lists_eq;
58 use $crate::core_compiler::value_visitor::compile_instruction_iterator_to_dxb;
59
60 let output_instructions = $output;
61 let output_dxb = compile_instruction_iterator_to_dxb(
62 output_instructions.iter().cloned(),
63 );
64 let expected_instructions = InstructionTree::<Instruction>::from($vec.into_iter().map(|i| i.into()).collect::<Vec<_>>()).flatten_instructions();
65 assert_instruction_lists_eq(output_instructions, expected_instructions, &output_dxb);
66 }}
67}
68
69pub fn assert_instruction_lists_eq(
70 output_instructions: Vec<Instruction>,
71 expected_instructions: Vec<Instruction>,
72 output_dxb: &[u8],
73) {
74 if output_instructions != expected_instructions {
75 let (expected_tree, expected_err) = get_instruction_tree_from_list(
76 expected_instructions
77 .into_iter()
78 .map(InstructionWithSpan::from)
79 .collect::<Vec<_>>(),
80 );
81
82 panic!(
83 "Output did not match expected instructions:\n\nOutput:\n{}\n\nExpected:\n{}\n",
84 disassemble_body_to_string(
85 output_dxb,
86 DisassemblerOptions::default()
87 ),
88 disassemble_instruction_tree_to_string(
89 expected_tree,
90 expected_err,
91 DisassemblerOptions::default()
92 ),
93 );
94 }
95}
96
97pub fn resolve_instructions(dxb: &[u8]) -> Vec<Instruction> {
100 let (instructions, err) = disassemble_body(
101 dxb,
102 NestedInstructionResolutionStrategy::ResolveNestedScopesFlat,
103 );
104 if let Some(err) = err {
105 panic!(
106 "Disassembly error: {}:\n{}",
107 err,
108 disassemble_body_to_string(dxb, DisassemblerOptions::default())
109 );
110 }
111 instructions
112 .flatten()
113 .into_iter()
114 .map(Instruction::from)
115 .collect::<Vec<_>>()
116}
117
118impl RegularInstruction {
119 pub fn with_children(
120 self,
121 children: Vec<InstructionTree<Instruction>>,
122 ) -> InstructionTree<Instruction> {
123 if children.is_empty()
125 && let CountOrUnbounded::Count(count) = self
126 .get_next_expected_instructions()
127 .total_count()
128 .expect("Expected count should be set for this instruction")
129 && count as usize != children.len()
130 {
131 panic!(
132 "Expected {} children for instruction {:?}, but got {}",
133 count,
134 self,
135 children.len()
136 );
137 }
138
139 InstructionTree::new_with_children(self.into(), children)
140 }
141
142 fn calculate_children_count(
144 children: &[InstructionTree<Instruction>],
145 ) -> u32 {
146 fn visit_next_child(
147 count: &mut u32,
148 skip: bool,
149 iterator: &mut Iter<InstructionTree<Instruction>>,
150 ) -> Option<u32> {
151 let current = match iterator.next() {
152 Some(next) => next,
153 None => return Some(*count),
154 };
155
156 if !skip {
157 *count += 1;
158 }
159
160 if current.children().is_empty()
162 && let Some(child_count) = current
163 .instruction()
164 .get_next_expected_instructions()
165 .total_count()
166 && let CountOrUnbounded::Count(child_count) = child_count
167 {
168 for _ in 0..child_count {
169 visit_next_child(count, true, iterator);
170 }
171 }
172
173 None
174 }
175
176 let iterator = &mut children.iter();
177 let mut count = 0;
178 loop {
179 if let Some(count) = visit_next_child(&mut count, false, iterator) {
180 return count;
181 }
182 }
183 }
184
185 pub fn statements_with_children(
186 terminated: bool,
187 children: Vec<InstructionTree<Instruction>>,
188 ) -> InstructionTree<Instruction> {
189 RegularInstruction::statements(
190 RegularInstruction::calculate_children_count(&children),
191 terminated,
192 )
193 .with_children(children)
194 }
195
196 pub fn list_with_children(
197 children: Vec<InstructionTree<Instruction>>,
198 ) -> InstructionTree<Instruction> {
199 RegularInstruction::list(RegularInstruction::calculate_children_count(
200 &children,
201 ))
202 .with_children(children)
203 }
204}
205
206pub macro instructions {
207 ($($expr:expr),* $(,)?) => {vec![
208 $($expr.into(),)*
209 ]}
210}
211
212pub macro instructions_with_span {
213 ($($expr:expr),* $(,)?) => {vec![
214 $(InstructionWithSpan::from($expr),)*
215 ]}
216}