use crate::{
disassembler::options::DisassemblerOptions,
dxb_parser::body::{DXBParserError, iterate_instructions},
global::{
instruction_codes::InstructionCode,
protocol_structures::{
instruction_data::InstructionBlockDataDebugFlat,
instructions::{
CountOrUnbounded, Instruction,
NestedInstructionResolutionStrategy,
},
regular_instructions::RegularInstruction,
},
},
prelude::*,
utils::ansi_colors::{AnsiColor, AnsiWrite},
};
use alloc::{rc::Rc, vec::IntoIter};
use core::{
cell::RefCell,
fmt::{Debug, Write},
};
use serde::Serialize;
#[derive(Debug, Clone, PartialEq, Serialize)]
pub struct InstructionTree<T>
where
T: Debug + Clone,
{
instruction: Box<T>,
#[serde(skip_serializing_if = "Vec::is_empty")]
children: Vec<InstructionTree<T>>,
}
impl<T> From<T> for InstructionTree<T>
where
T: Debug + Clone,
{
fn from(instruction: T) -> Self {
InstructionTree::new(instruction)
}
}
impl From<RegularInstruction> for InstructionTree<Instruction> {
fn from(instruction: RegularInstruction) -> Self {
InstructionTree::new(Instruction::Regular(instruction))
}
}
impl From<Vec<InstructionTree<Instruction>>> for InstructionTree<Instruction> {
fn from(mut instruction_trees: Vec<InstructionTree<Instruction>>) -> Self {
if instruction_trees.len() == 1 {
instruction_trees.remove(0)
} else {
fn visit_next_child(
parent: &mut InstructionTree<Instruction>,
iterator: &mut IntoIter<InstructionTree<Instruction>>,
) -> bool {
let mut current = match iterator.next() {
Some(next) => next,
None => return false,
};
if current.children().is_empty()
&& let Some(child_count) = current
.instruction()
.get_next_expected_instructions()
.total_count()
&& let CountOrUnbounded::Count(child_count) = child_count
{
for _ in 0..child_count {
if !visit_next_child(&mut current, iterator) {
panic!(
"Expected {} children for instruction {:?}, but got fewer",
child_count,
current.instruction()
);
}
}
}
parent.children.push(current);
true
}
let mut iterator = instruction_trees.into_iter();
let mut root: InstructionTree<Instruction> =
iterator.next().unwrap();
if !root.children().is_empty() {
panic!("Multiple root nodes found in instruction tree.");
}
while visit_next_child(&mut root, &mut iterator) {}
root
}
}
}
impl InstructionTree<Instruction> {
pub fn flatten_instructions(self) -> Vec<Instruction> {
if let Instruction::Regular(
RegularInstruction::_RemoteExecutionDebugTree(tree),
) = *self.instruction
{
vec![Instruction::Regular(
RegularInstruction::_RemoteExecutionDebugFlat(
InstructionBlockDataDebugFlat {
length: tree.length,
injected_variable_count: tree.injected_variable_count,
injected_values: tree.injected_values,
body: tree.body.flatten_instructions(),
},
),
)]
} else {
let mut result = vec![*self.instruction];
for child in self.children {
result.extend(child.flatten_instructions());
}
result
}
}
}
impl<T> InstructionTree<T>
where
T: Debug + Clone,
{
pub fn new(instruction: T) -> Self {
Self {
instruction: Box::new(instruction),
children: Vec::new(),
}
}
pub fn new_with_children(
instruction: T,
children: Vec<InstructionTree<T>>,
) -> Self {
Self {
instruction: Box::new(instruction),
children,
}
}
pub fn flatten(&self) -> Vec<T> {
let mut result = vec![*self.instruction.clone()];
for child in &self.children {
result.extend(child.flatten());
}
result
}
pub fn map<N: Debug + Clone>(
self,
f: impl Fn(T) -> N + Clone,
) -> InstructionTree<N> {
InstructionTree {
instruction: Box::new(f(*self.instruction)),
children: self
.children
.into_iter()
.map(|child| child.map(f.clone()))
.collect(),
}
}
pub fn children(&self) -> &Vec<InstructionTree<T>> {
&self.children
}
pub fn instruction(&self) -> &T {
&self.instruction
}
}
#[derive(Debug, Clone)]
struct DetailedInstructionTree(
pub InstructionTree<(Instruction, Option<Box<DetailedInstructionTree>>)>,
);
#[derive(Default, Clone, Debug, PartialEq)]
pub enum InnerInstructions<'a> {
#[default]
None,
Flat(&'a Vec<Instruction>),
Tree(&'a InstructionTree<Instruction>),
}
pub(super) fn disassemble_body_to_string(
body: &[u8],
options: DisassemblerOptions,
) -> String {
let (instructions, err) = disassemble_body(
body,
options.nested_instructions_resolution_strategy(),
);
disassemble_instruction_tree_to_string(instructions, err, options)
}
pub fn disassemble_instruction_tree_to_string(
instruction_tree: InstructionTree<Instruction>,
error: Option<DXBParserError>,
options: DisassemblerOptions,
) -> String {
let instructions = instruction_tree_to_detailed_tree(instruction_tree);
let mut output = String::new();
if options.tree {
disassemble_body_to_string_inner(
&mut output,
instructions,
0,
true,
true,
false,
options.colorized,
0,
);
} else {
write_flat_instructions(
&mut output,
instructions,
options.colorized,
0,
);
}
if let Some(err) = error {
if options.colorized {
write!(
&mut output,
"\x1b[38;2;245;39;60m\n[!] Parser Error: {}\x1b[0m",
err
)
.unwrap();
} else {
write!(&mut output, "[!] Parser Error: {}", err).unwrap();
}
}
output
}
pub fn disassemble_body(
body: &[u8],
nested_instruction_resolution_strategy: NestedInstructionResolutionStrategy,
) -> (InstructionTree<Instruction>, Option<DXBParserError>) {
let mut iterator = iterate_instructions(
Rc::new(RefCell::new(body.to_vec())),
nested_instruction_resolution_strategy,
);
get_instruction_tree(&mut iterator)
}
pub fn get_instruction_tree_from_list(
instructions: Vec<Instruction>,
) -> (InstructionTree<Instruction>, Option<DXBParserError>) {
let mut iterator = instructions.into_iter().map(Ok);
get_instruction_tree(&mut iterator)
}
pub fn get_instruction_tree(
instructions: impl Iterator<Item = Result<Instruction, DXBParserError>>,
) -> (InstructionTree<Instruction>, Option<DXBParserError>) {
let mut tree = InstructionTree::new(Instruction::Regular(
RegularInstruction::UnboundedStatements,
)); let err = disassemble_body_inner(
&mut instructions.into_iter(),
&mut tree,
CountOrUnbounded::UnboundedStart,
true,
);
(tree, err)
}
fn write_flat_instructions(
output: &mut String,
instructions: DetailedInstructionTree,
colorized: bool,
level: u32,
) {
for (instruction, inner_instructions) in instructions.0.flatten() {
write!(output, "{}", " ".repeat(level as usize * 2),).unwrap();
write_instruction(output, &instruction, level, colorized);
if let Some(inner_instructions) = inner_instructions {
write_flat_instructions(
output,
*inner_instructions,
colorized,
level + 1,
);
}
}
}
fn write_instruction(
output: &mut String,
instruction: &Instruction,
level: u32,
colorized: bool,
) {
if colorized {
let color = color_for_level(level);
output.write_fg(color);
}
write!(
output,
"{}",
match instruction {
Instruction::Regular(instr) =>
InstructionCode::from(instr).to_string(),
Instruction::Type(instr) => instr.as_ref().to_string(),
}
)
.unwrap();
if colorized {
output.write_reset();
}
if let Some(metadata_string) = instruction.metadata_string() {
write!(output, " {}", metadata_string).unwrap();
}
writeln!(output).unwrap();
}
fn color_for_level(level: u32) -> AnsiColor {
match level % 10 {
0 => AnsiColor::Rgb(0, 153, 204), 1 => AnsiColor::Rgb(0, 204, 153), 2 => AnsiColor::Rgb(51, 204, 102), 3 => AnsiColor::Rgb(153, 255, 51), 4 => AnsiColor::Rgb(255, 221, 51), 5 => AnsiColor::Rgb(204, 204, 255), 6 => AnsiColor::Rgb(153, 153, 255), 7 => AnsiColor::Rgb(153, 102, 204), 8 => AnsiColor::Rgb(255, 153, 204), 9 => AnsiColor::Rgb(255, 204, 229), _ => AnsiColor::Rgb(200, 200, 200), }
}
fn disassemble_body_to_string_inner(
output: &mut String,
instructions: DetailedInstructionTree,
indent_width: usize,
is_root_child: bool,
is_last_child: bool,
is_inner: bool,
colorized: bool,
level: u32,
) {
let indent = " ".repeat(indent_width * 3);
let children_len = instructions.0.children.len();
let (main, inner) = *instructions.0.instruction;
write!(
output,
"{}{}",
indent,
if is_root_child {
""
} else if is_inner {
"↳ "
} else if is_last_child || children_len > 0 {
"└─ "
} else {
"├─ "
},
)
.unwrap();
write_instruction(output, &main, level, colorized);
if let Some(inner) = inner {
disassemble_body_to_string_inner(
output,
*inner,
if is_root_child { 0 } else { indent_width + 1 },
false,
false,
true,
colorized,
level + 1,
);
}
for (i, child) in instructions.0.children.into_iter().enumerate() {
disassemble_body_to_string_inner(
output,
DetailedInstructionTree(child),
if is_root_child { 0 } else { indent_width + 1 },
false,
i == children_len - 1,
false,
colorized,
level,
);
}
}
fn instruction_tree_to_detailed_tree(
instructions: InstructionTree<Instruction>,
) -> DetailedInstructionTree {
DetailedInstructionTree(instructions.map(|i| {
let inner = get_inner_instructions_as_detailed_tree(&i).map(Box::new);
(i, inner)
}))
}
fn get_inner_instructions_as_detailed_tree(
instruction: &Instruction,
) -> Option<DetailedInstructionTree> {
match instruction {
Instruction::Regular(
RegularInstruction::_RemoteExecutionDebugFlat(_data),
) => {
unreachable!()
}
Instruction::Regular(
RegularInstruction::_RemoteExecutionDebugTree(data),
) => Some(instruction_tree_to_detailed_tree(data.body.clone())),
_ => None,
}
}
fn disassemble_body_inner(
iterator: &mut impl Iterator<Item = Result<Instruction, DXBParserError>>,
parent: &mut InstructionTree<Instruction>,
count_or_unbounded: CountOrUnbounded,
is_root: bool,
) -> Option<DXBParserError> {
loop {
let next = iterator.next();
match next {
Some(instruction) => {
match instruction {
Err(e) => {
return Some(e);
}
Ok(instruction) => {
let next_expected_count = instruction
.get_next_expected_instructions()
.total_count();
let mut tree = InstructionTree::new(instruction);
let err = match next_expected_count {
Some(next_expected_count) => {
match next_expected_count {
CountOrUnbounded::UnboundedEnd => {
parent.children.push(tree);
return None;
}
_ => disassemble_body_inner(
iterator,
&mut tree,
next_expected_count,
false,
),
}
}
None => None,
};
if is_root {
*parent = tree;
} else {
parent.children.push(tree);
}
if let Some(err) = err {
return Some(err);
}
if let CountOrUnbounded::Count(expected_count) =
count_or_unbounded
&& parent.children.len() as u32 >= expected_count
{
break;
}
}
}
}
None => break,
}
}
None
}
#[cfg(test)]
mod tests {
use super::*;
use crate::{
core_compiler::value_compiler::append_instruction,
global::protocol_structures::{
instruction_data::{
InstructionBlockData, InstructionBlockDataDebugFlat,
InstructionBlockDataDebugTree, StatementsData, UInt8Data,
UnboundedStatementsData,
},
regular_instructions::RegularInstruction,
},
runtime::{Runtime, RuntimeConfig, RuntimeRunner},
};
use binrw::io::Cursor;
use test_case::test_case;
fn instructions_to_bytes(instructions: Vec<Instruction>) -> Vec<u8> {
let mut cursor = Cursor::new(Vec::new());
for instruction in instructions {
append_instruction(&mut cursor, instruction);
}
cursor.into_inner()
}
#[test_case(
&[],
InstructionTree::new(Instruction::Regular(RegularInstruction::UnboundedStatements)),
Some(DXBParserError::ExpectingMoreInstructions)
; "empty dxb")]
#[test_case(
&[
Instruction::Regular(RegularInstruction::True),
Instruction::Regular(RegularInstruction::False),
],
InstructionTree::new(Instruction::Regular(RegularInstruction::True)),
Some(DXBParserError::UnexpectedBytesAfterEndOfInstructions)
; "multiple root nodes")]
#[test_case(
&[
Instruction::Regular(RegularInstruction::Statements(StatementsData {statements_count: 2, terminated: true})),
Instruction::Regular(RegularInstruction::True),
Instruction::Regular(RegularInstruction::False),
],
InstructionTree {
instruction: Box::new(Instruction::Regular(RegularInstruction::Statements(StatementsData {statements_count: 2, terminated: true}))),
children: vec![
InstructionTree::new(Instruction::Regular(RegularInstruction::True)),
InstructionTree::new(Instruction::Regular(RegularInstruction::False)),
]
},
None
; "simple statements"
)]
#[test_case(
&[
Instruction::Regular(RegularInstruction::UnboundedStatements),
Instruction::Regular(RegularInstruction::True),
Instruction::Regular(RegularInstruction::False),
Instruction::Regular(RegularInstruction::UnboundedStatementsEnd(UnboundedStatementsData {terminated: false})),
],
InstructionTree {
instruction: Box::new(Instruction::Regular(RegularInstruction::UnboundedStatements)),
children: vec![
InstructionTree::new(Instruction::Regular(RegularInstruction::True)),
InstructionTree::new(Instruction::Regular(RegularInstruction::False)),
InstructionTree::new(Instruction::Regular(RegularInstruction::UnboundedStatementsEnd(UnboundedStatementsData {terminated: false})))
]
},
None
; "unbounded statements"
)]
#[test_case(
&[
Instruction::Regular(RegularInstruction::Statements(StatementsData {statements_count: 2, terminated: true})),
Instruction::Regular(RegularInstruction::UnboundedStatements),
Instruction::Regular(RegularInstruction::True),
Instruction::Regular(RegularInstruction::False),
Instruction::Regular(RegularInstruction::UnboundedStatementsEnd(UnboundedStatementsData {terminated: false})),
Instruction::Regular(RegularInstruction::Null),
],
InstructionTree {
instruction: Box::new(Instruction::Regular(RegularInstruction::Statements(StatementsData {statements_count: 2, terminated: true}))),
children: vec![
InstructionTree {
instruction: Box::new(Instruction::Regular(RegularInstruction::UnboundedStatements)),
children: vec![
InstructionTree::new(Instruction::Regular(RegularInstruction::True)),
InstructionTree::new(Instruction::Regular(RegularInstruction::False)),
InstructionTree::new(Instruction::Regular(RegularInstruction::UnboundedStatementsEnd(UnboundedStatementsData {terminated: false})))
]
},
InstructionTree::new(Instruction::Regular(RegularInstruction::Null)),
]
},
None
; "normal and unbounded statements"
)]
#[test_case(
&[
Instruction::Regular(RegularInstruction::True),
],
InstructionTree::new(Instruction::Regular(RegularInstruction::True)),
None
; "single instruction"
)]
#[test_case(
&[
Instruction::Regular(RegularInstruction::Statements(StatementsData {statements_count: 2, terminated: true})),
Instruction::Regular(RegularInstruction::True),
],
InstructionTree {
instruction: Box::new(Instruction::Regular(RegularInstruction::Statements(StatementsData {statements_count: 2, terminated: true}))),
children: vec![
InstructionTree::new(Instruction::Regular(RegularInstruction::True)),
]
},
Some(DXBParserError::ExpectingMoreInstructions)
; "statements with missing instructions"
)]
#[test_case(
&[
Instruction::Regular(RegularInstruction::RemoteExecution(InstructionBlockData {
length: 2,
injected_value_count: 0,
injected_values: vec![],
body: vec![
InstructionCode::UINT_8 as u8,
42,
]
})),
Instruction::Regular(RegularInstruction::True)
],
InstructionTree {
instruction: Box::new(Instruction::Regular(RegularInstruction::RemoteExecution(InstructionBlockData {
length: 2,
injected_value_count: 0,
injected_values: vec![],
body: vec![
InstructionCode::UINT_8 as u8,
42,
]
}))),
children: vec![
InstructionTree::new(Instruction::Regular(RegularInstruction::True)),
]
},
None
; "remote execution"
)]
fn disassemble_statements(
instructions: &[Instruction],
expected_tree: InstructionTree<Instruction>,
expected_err: Option<DXBParserError>,
) {
let dxb = instructions_to_bytes(instructions.to_vec());
let (tree, err) = disassemble_body(
&dxb,
NestedInstructionResolutionStrategy::default(),
);
assert_eq!(err, expected_err);
assert_eq!(tree, expected_tree,)
}
#[test]
fn disassemble_nested_flat() {
let instructions = vec![
Instruction::Regular(RegularInstruction::RemoteExecution(
InstructionBlockData {
length: 5,
injected_value_count: 0,
injected_values: vec![],
body: vec![
InstructionCode::ADD as u8,
InstructionCode::UINT_8 as u8,
42,
InstructionCode::UINT_8 as u8,
43,
],
},
)),
Instruction::Regular(RegularInstruction::True),
];
let dxb = instructions_to_bytes(instructions.to_vec());
let (tree, err) = disassemble_body(
&dxb,
NestedInstructionResolutionStrategy::ResolveNestedScopesFlat,
);
assert_eq!(err, None);
assert_eq!(
tree,
InstructionTree {
instruction: Box::new(Instruction::Regular(
RegularInstruction::_RemoteExecutionDebugFlat(
InstructionBlockDataDebugFlat {
length: 5,
injected_variable_count: 0,
injected_values: vec![],
body: vec![
Instruction::Regular(RegularInstruction::Add),
Instruction::Regular(
RegularInstruction::UInt8(UInt8Data(42))
),
Instruction::Regular(
RegularInstruction::UInt8(UInt8Data(43))
),
]
}
)
)),
children: vec![InstructionTree::new(Instruction::Regular(
RegularInstruction::True
)),]
}
);
}
#[test]
fn disassemble_nested_tree() {
let instructions = vec![
Instruction::Regular(RegularInstruction::RemoteExecution(
InstructionBlockData {
length: 5,
injected_value_count: 0,
injected_values: vec![],
body: vec![
InstructionCode::ADD as u8,
InstructionCode::UINT_8 as u8,
42,
InstructionCode::UINT_8 as u8,
43,
],
},
)),
Instruction::Regular(RegularInstruction::True),
];
let dxb = instructions_to_bytes(instructions.to_vec());
let (tree, err) = disassemble_body(
&dxb,
NestedInstructionResolutionStrategy::ResolveNestedScopesTree,
);
assert_eq!(err, None);
assert_eq!(
tree,
InstructionTree {
instruction: Box::new(Instruction::Regular(
RegularInstruction::_RemoteExecutionDebugTree(
InstructionBlockDataDebugTree {
length: 5,
injected_variable_count: 0,
injected_values: vec![],
body: InstructionTree {
instruction: Box::new(Instruction::Regular(
RegularInstruction::Add
)),
children: vec![
InstructionTree::new(Instruction::Regular(
RegularInstruction::UInt8(UInt8Data(
42
))
)),
InstructionTree::new(Instruction::Regular(
RegularInstruction::UInt8(UInt8Data(
43
))
)),
]
}
}
)
)),
children: vec![InstructionTree::new(Instruction::Regular(
RegularInstruction::True
)),]
}
);
}
#[ignore]
#[cfg(all(feature = "std", feature = "compiler"))]
#[test]
fn disassemble_string_test() {
use crate::compiler::{CompileOptions, compile_script};
let script = r#"
var x = 5;
var y = 42;
@example :: (
1;2;3;
@test :: (1 + 2);
)
"#;
let dxb =
compile_script(script, CompileOptions::default(), Runtime::stub())
.unwrap()
.0;
println!(
"{}",
disassemble_body_to_string(
&dxb,
DisassemblerOptions {
tree: true,
colorized: true,
recursive: true,
}
)
);
println!(
"{}",
disassemble_body_to_string(
&dxb,
DisassemblerOptions {
tree: false,
colorized: true,
recursive: true,
}
)
);
println!(
"{}",
disassemble_body_to_string(
&dxb,
DisassemblerOptions {
tree: true,
colorized: true,
recursive: false,
}
)
);
}
}