#[cfg(feature = "disassembler")]
use crate::disassembler::InnerInstructions;
use crate::{
dxb_parser::body::DXBParserError,
global::{
instruction_codes::InstructionCode,
protocol_structures::{
instruction_data::{
ApplyData, Float32Data, Float64Data, FloatAsInt16Data,
FloatAsInt32Data, InstructionBlockData, Int8Data, Int16Data,
Int32Data, Int64Data, Int128Data, ListData, MapData,
ModifySharedContainerValue, ModifyStackValue, Move,
PerformMoves, SharedRef, SharedRefWithValue, ShortListData,
ShortMapData, ShortStatementsData, ShortTextData, StackIndex,
StatementsData, TaggedValue, TextData, UInt8Data, UInt16Data,
UInt32Data, UInt64Data, UInt128Data, UnboundedStatementsData,
},
instructions::NextExpectedInstructions,
},
root_properties::RootProperty,
},
libs::core::core_lib_id::CoreLibIdIndex,
prelude::*,
shared_values::{
PointerAddress, RemotePointerAddress, SelfOwnedPointerAddress,
},
values::core_values::{
decimal::{Decimal, typed_decimal::TypedDecimal},
endpoint::Endpoint,
integer::Integer,
},
};
use binrw::{
BinRead, BinResult, BinWrite, Endian,
io::{Read, Seek},
meta::{EndianKind, ReadEndian},
};
use core::fmt::{Display, Write as FmtWrite};
use serde::{Serialize, Serializer, ser::SerializeTuple};
#[derive(Clone, Debug, PartialEq, BinWrite)]
#[brw(little)]
pub enum RegularInstruction {
Int8(Int8Data),
Int16(Int16Data),
Int32(Int32Data),
Int64(Int64Data),
Int128(Int128Data),
UInt8(UInt8Data),
UInt16(UInt16Data),
UInt32(UInt32Data),
UInt64(UInt64Data),
UInt128(UInt128Data),
BigInteger(Integer),
Integer(Integer),
Range,
Endpoint(Endpoint),
DecimalF32(Float32Data),
DecimalF64(Float64Data),
DecimalAsInt16(FloatAsInt16Data),
DecimalAsInt32(FloatAsInt32Data),
BigDecimal(Decimal),
Decimal(Decimal),
RemoteExecution(InstructionBlockData),
ShortText(ShortTextData),
Text(TextData),
True,
False,
Null,
Statements(StatementsData),
ShortStatements(ShortStatementsData),
UnboundedStatements,
UnboundedStatementsEnd(UnboundedStatementsData),
List(ListData),
ShortList(ShortListData),
Map(MapData),
ShortMap(MapData),
KeyValueDynamic,
KeyValueShortText(ShortTextData),
TaggedValue(TaggedValue),
Add,
Subtract,
Multiply,
Divide,
UnaryMinus,
UnaryPlus,
BitwiseNot,
Apply(ApplyData),
GetPropertyText(ShortTextData),
SetPropertyText(ShortTextData),
TakePropertyText(ShortTextData),
GetPropertyIndex(UInt32Data),
SetPropertyIndex(UInt32Data),
TakePropertyIndex(UInt32Data),
GetPropertyDynamic,
SetPropertyDynamic,
TakePropertyDynamic,
Is,
Matches,
StructuralEqual,
Equal,
NotStructuralEqual,
NotEqual,
ModifyStackValue(ModifyStackValue),
GetSharedReference,
GetSharedReferenceMut,
CreateShared,
CreateSharedMut,
RequestRemoteSharedRef(RemotePointerAddress),
RequestRemoteSharedRefMut(RemotePointerAddress),
GetLocalSharedRef(SelfOwnedPointerAddress),
GetCoreLibValue(CoreLibIdIndex),
SharedRef(SharedRef),
SharedRefWithValue(SharedRefWithValue),
PerformMoves(PerformMoves),
Move(Move),
PushToStack,
PushListToStack,
CloneStackValue(StackIndex),
BorrowStackValue(StackIndex),
GetStackValueSharedRef(StackIndex),
GetStackValueSharedRefMut(StackIndex),
TakeStackValue(StackIndex),
SetStackValue(StackIndex),
GetRootProperty(RootProperty),
SetSharedContainerValue,
ModifySharedContainerValue(ModifySharedContainerValue),
Unbox,
TypedValue,
TypeExpression,
#[cfg(feature = "disassembler")]
_RemoteExecutionDebugFlat(crate::global::protocol_structures::instruction_data::InstructionBlockDataDebugFlat),
#[cfg(feature = "disassembler")]
_RemoteExecutionDebugTree(crate::global::protocol_structures::instruction_data::InstructionBlockDataDebugTree),
}
impl RegularInstruction {
pub fn statements(count: u32, terminated: bool) -> RegularInstruction {
match count {
0..=255 => {
RegularInstruction::ShortStatements(ShortStatementsData {
statements_count: count as u8,
terminated,
})
}
_ => RegularInstruction::Statements(StatementsData {
statements_count: count,
terminated,
}),
}
}
pub fn list(count: u32) -> RegularInstruction {
match count {
0..=255 => RegularInstruction::ShortList(ShortListData {
element_count: count as u8,
}),
_ => RegularInstruction::List(ListData {
element_count: count,
}),
}
}
}
impl From<&RegularInstruction> for InstructionCode {
fn from(instruction: &RegularInstruction) -> Self {
match instruction {
RegularInstruction::Int8(_) => InstructionCode::INT_8,
RegularInstruction::Int16(_) => InstructionCode::INT_16,
RegularInstruction::Int32(_) => InstructionCode::INT_32,
RegularInstruction::Int64(_) => InstructionCode::INT_64,
RegularInstruction::Int128(_) => InstructionCode::INT_128,
RegularInstruction::UInt8(_) => InstructionCode::UINT_8,
RegularInstruction::UInt16(_) => InstructionCode::UINT_16,
RegularInstruction::UInt32(_) => InstructionCode::UINT_32,
RegularInstruction::UInt64(_) => InstructionCode::UINT_64,
RegularInstruction::UInt128(_) => InstructionCode::UINT_128,
RegularInstruction::BigInteger(_) => InstructionCode::INT_BIG,
RegularInstruction::Integer(_) => InstructionCode::INT,
RegularInstruction::Endpoint(_) => InstructionCode::ENDPOINT,
RegularInstruction::DecimalF32(_) => InstructionCode::DECIMAL_F32,
RegularInstruction::DecimalF64(_) => InstructionCode::DECIMAL_F64,
RegularInstruction::DecimalAsInt16(_) => {
InstructionCode::DECIMAL_AS_INT_16
}
RegularInstruction::DecimalAsInt32(_) => {
InstructionCode::DECIMAL_AS_INT_32
}
RegularInstruction::BigDecimal(_) => InstructionCode::DECIMAL_BIG,
RegularInstruction::Decimal(_) => InstructionCode::DECIMAL,
RegularInstruction::Range => InstructionCode::RANGE,
RegularInstruction::RemoteExecution(_) => {
InstructionCode::REMOTE_EXECUTION
}
RegularInstruction::ShortText(_) => InstructionCode::SHORT_TEXT,
RegularInstruction::Text(_) => InstructionCode::TEXT,
RegularInstruction::True => InstructionCode::TRUE,
RegularInstruction::False => InstructionCode::FALSE,
RegularInstruction::Null => InstructionCode::NULL,
RegularInstruction::Statements(_) => InstructionCode::STATEMENTS,
RegularInstruction::ShortStatements(_) => {
InstructionCode::SHORT_STATEMENTS
}
RegularInstruction::UnboundedStatements => {
InstructionCode::UNBOUNDED_STATEMENTS
}
RegularInstruction::UnboundedStatementsEnd(_) => {
InstructionCode::UNBOUNDED_STATEMENTS_END
}
RegularInstruction::List(_) => InstructionCode::LIST,
RegularInstruction::ShortList(_) => InstructionCode::SHORT_LIST,
RegularInstruction::Map(_) => InstructionCode::MAP,
RegularInstruction::ShortMap(_) => InstructionCode::SHORT_MAP,
RegularInstruction::KeyValueDynamic => {
InstructionCode::KEY_VALUE_DYNAMIC
}
RegularInstruction::KeyValueShortText(_) => {
InstructionCode::KEY_VALUE_SHORT_TEXT
}
RegularInstruction::Add => InstructionCode::ADD,
RegularInstruction::Subtract => InstructionCode::SUBTRACT,
RegularInstruction::Multiply => InstructionCode::MULTIPLY,
RegularInstruction::Divide => InstructionCode::DIVIDE,
RegularInstruction::UnaryMinus => InstructionCode::UNARY_MINUS,
RegularInstruction::UnaryPlus => InstructionCode::UNARY_PLUS,
RegularInstruction::BitwiseNot => InstructionCode::BITWISE_NOT,
RegularInstruction::Apply(_) => InstructionCode::APPLY,
RegularInstruction::GetPropertyText(_) => {
InstructionCode::GET_PROPERTY_TEXT
}
RegularInstruction::SetPropertyText(_) => {
InstructionCode::SET_PROPERTY_TEXT
}
RegularInstruction::TakePropertyText(_) => {
InstructionCode::TAKE_PROPERTY_TEXT
}
RegularInstruction::GetPropertyIndex(_) => {
InstructionCode::GET_PROPERTY_INDEX
}
RegularInstruction::SetPropertyIndex(_) => {
InstructionCode::SET_PROPERTY_INDEX
}
RegularInstruction::TakePropertyIndex(_) => {
InstructionCode::TAKE_PROPERTY_INDEX
}
RegularInstruction::GetPropertyDynamic => {
InstructionCode::GET_PROPERTY_DYNAMIC
}
RegularInstruction::SetPropertyDynamic => {
InstructionCode::SET_PROPERTY_DYNAMIC
}
RegularInstruction::TakePropertyDynamic => {
InstructionCode::TAKE_PROPERTY_DYNAMIC
}
RegularInstruction::Is => InstructionCode::IS,
RegularInstruction::Matches => InstructionCode::MATCHES,
RegularInstruction::StructuralEqual => {
InstructionCode::STRUCTURAL_EQUAL
}
RegularInstruction::Equal => InstructionCode::EQUAL,
RegularInstruction::NotStructuralEqual => {
InstructionCode::NOT_STRUCTURAL_EQUAL
}
RegularInstruction::NotEqual => InstructionCode::NOT_EQUAL,
RegularInstruction::GetSharedReference => {
InstructionCode::GET_SHARED_REF
}
RegularInstruction::GetSharedReferenceMut => {
InstructionCode::GET_SHARED_REF_MUT
}
RegularInstruction::CreateShared => InstructionCode::CREATE_SHARED,
RegularInstruction::CreateSharedMut => {
InstructionCode::CREATE_SHARED_MUT
}
RegularInstruction::RequestRemoteSharedRef(_) => {
InstructionCode::REQUEST_REMOTE_SHARED_REF
}
RegularInstruction::RequestRemoteSharedRefMut(_) => {
InstructionCode::REQUEST_REMOTE_SHARED_REF_MUT
}
RegularInstruction::GetLocalSharedRef(_) => {
InstructionCode::GET_LOCAL_SHARED_REF
}
RegularInstruction::GetCoreLibValue(_) => {
InstructionCode::GET_CORE_LIB_VALUE
}
RegularInstruction::SharedRef(_) => InstructionCode::SHARED_REF,
RegularInstruction::SharedRefWithValue(_) => {
InstructionCode::SHARED_REF_WITH_VALUE
}
RegularInstruction::PerformMoves(_) => {
InstructionCode::PERFORM_MOVES
}
RegularInstruction::Move(_) => InstructionCode::MOVE,
RegularInstruction::PushToStack => InstructionCode::PUSH_TO_STACK,
RegularInstruction::PushListToStack => {
InstructionCode::PUSH_LIST_TO_STACK
}
RegularInstruction::CloneStackValue(_) => {
InstructionCode::CLONE_STACK_VALUE
}
RegularInstruction::BorrowStackValue(_) => {
InstructionCode::BORROW_STACK_VALUE
}
RegularInstruction::GetStackValueSharedRef(_) => {
InstructionCode::GET_STACK_VALUE_SHARED_REF
}
RegularInstruction::GetStackValueSharedRefMut(_) => {
InstructionCode::GET_STACK_VALUE_SHARED_REF_MUT
}
RegularInstruction::TakeStackValue(_) => {
InstructionCode::TAKE_STACK_VALUE
}
RegularInstruction::SetStackValue(_) => {
InstructionCode::SET_STACK_VALUE
}
RegularInstruction::ModifyStackValue(_) => {
InstructionCode::MODIFY_STACK_VALUE
}
RegularInstruction::GetRootProperty(_) => {
InstructionCode::GET_ROOT_PROPERTY
}
RegularInstruction::ModifySharedContainerValue(_) => {
InstructionCode::MODIFY_SHARED_CONTAINER_VALUE
}
RegularInstruction::SetSharedContainerValue => {
InstructionCode::SET_SHARED_CONTAINER_VALUE
}
RegularInstruction::Unbox => InstructionCode::UNBOX,
RegularInstruction::TypedValue => InstructionCode::TYPED_VALUE,
RegularInstruction::TypeExpression => {
InstructionCode::TYPE_EXPRESSION
}
RegularInstruction::TaggedValue(_) => InstructionCode::TAGGED_VALUE,
#[cfg(feature = "disassembler")]
RegularInstruction::_RemoteExecutionDebugFlat(_)
| RegularInstruction::_RemoteExecutionDebugTree(_) => {
InstructionCode::REMOTE_EXECUTION
}
}
}
}
impl RegularInstruction {
pub fn get_next_expected_instructions(&self) -> NextExpectedInstructions {
match self {
RegularInstruction::RemoteExecution(_) => {
NextExpectedInstructions::Regular(1)
}
#[cfg(feature = "disassembler")]
RegularInstruction::_RemoteExecutionDebugTree(_)
| RegularInstruction::_RemoteExecutionDebugFlat(_) => {
NextExpectedInstructions::Regular(1)
}
RegularInstruction::ShortList(list) => {
NextExpectedInstructions::Regular(list.element_count as u32)
}
RegularInstruction::List(list) => {
NextExpectedInstructions::Regular(list.element_count)
}
RegularInstruction::ShortMap(map)
| RegularInstruction::Map(map) => {
NextExpectedInstructions::Regular(map.element_count)
}
RegularInstruction::ShortStatements(statements) => {
NextExpectedInstructions::Regular(
statements.statements_count as u32,
)
}
RegularInstruction::Statements(statements) => {
NextExpectedInstructions::Regular(statements.statements_count)
}
RegularInstruction::UnboundedStatements => {
NextExpectedInstructions::UnboundedStart
}
RegularInstruction::UnboundedStatementsEnd(_) => {
NextExpectedInstructions::UnboundedEnd
}
RegularInstruction::Apply(apply_data) => {
NextExpectedInstructions::Regular(
apply_data.arg_count as u32 + 1,
)
}
RegularInstruction::GetPropertyText(_)
| RegularInstruction::GetPropertyIndex(_)
| RegularInstruction::TakePropertyText(_)
| RegularInstruction::TakePropertyIndex(_) => {
NextExpectedInstructions::Regular(1)
}
RegularInstruction::GetPropertyDynamic
| RegularInstruction::TakePropertyDynamic => {
NextExpectedInstructions::Regular(2)
}
RegularInstruction::SetPropertyText(_)
| RegularInstruction::SetPropertyIndex(_) => {
NextExpectedInstructions::Regular(2)
}
RegularInstruction::SetPropertyDynamic => {
NextExpectedInstructions::Regular(3)
}
RegularInstruction::Unbox => NextExpectedInstructions::Regular(1),
RegularInstruction::ModifySharedContainerValue(_) => {
NextExpectedInstructions::Regular(2)
}
RegularInstruction::SetSharedContainerValue => {
NextExpectedInstructions::Regular(2)
}
RegularInstruction::KeyValueDynamic => {
NextExpectedInstructions::Regular(2)
}
RegularInstruction::KeyValueShortText(_) => {
NextExpectedInstructions::Regular(1)
}
RegularInstruction::Matches => {
NextExpectedInstructions::RegularAndType(1, 1)
}
RegularInstruction::Add
| RegularInstruction::Multiply
| RegularInstruction::Subtract
| RegularInstruction::Divide => {
NextExpectedInstructions::Regular(2)
}
RegularInstruction::StructuralEqual
| RegularInstruction::NotStructuralEqual
| RegularInstruction::Equal
| RegularInstruction::NotEqual
| RegularInstruction::Is => NextExpectedInstructions::Regular(2),
RegularInstruction::UnaryMinus
| RegularInstruction::UnaryPlus
| RegularInstruction::BitwiseNot => {
NextExpectedInstructions::Regular(1)
}
RegularInstruction::GetSharedReference
| RegularInstruction::GetSharedReferenceMut
| RegularInstruction::CreateShared
| RegularInstruction::CreateSharedMut => {
NextExpectedInstructions::Regular(1)
}
RegularInstruction::PushToStack
| RegularInstruction::PushListToStack
| RegularInstruction::SetStackValue(_) => {
NextExpectedInstructions::Regular(1)
}
RegularInstruction::ModifyStackValue(_) => {
NextExpectedInstructions::Regular(1)
}
RegularInstruction::TypedValue => {
NextExpectedInstructions::RegularAndType(1, 1)
}
RegularInstruction::TypeExpression => {
NextExpectedInstructions::Type(1)
}
RegularInstruction::Range => NextExpectedInstructions::Regular(2),
RegularInstruction::TaggedValue(TaggedValue {
is_empty, ..
}) => {
if *is_empty {
NextExpectedInstructions::None
} else {
NextExpectedInstructions::Regular(1)
}
}
RegularInstruction::SharedRefWithValue(_) => {
NextExpectedInstructions::Regular(1)
}
_ => NextExpectedInstructions::None,
}
}
fn read_instruction<R: Read + Seek>(
reader: &mut R,
instruction_code: InstructionCode,
) -> BinResult<Self> {
match instruction_code {
InstructionCode::UINT_8 => {
UInt8Data::read(reader).map(RegularInstruction::UInt8)
}
InstructionCode::UINT_16 => {
UInt16Data::read(reader).map(RegularInstruction::UInt16)
}
InstructionCode::UINT_32 => {
UInt32Data::read(reader).map(RegularInstruction::UInt32)
}
InstructionCode::UINT_64 => {
UInt64Data::read(reader).map(RegularInstruction::UInt64)
}
InstructionCode::UINT_128 => {
UInt128Data::read(reader).map(RegularInstruction::UInt128)
}
InstructionCode::INT_8 => {
Int8Data::read(reader).map(RegularInstruction::Int8)
}
InstructionCode::INT_16 => {
Int16Data::read(reader).map(RegularInstruction::Int16)
}
InstructionCode::INT_32 => {
Int32Data::read(reader).map(RegularInstruction::Int32)
}
InstructionCode::INT_64 => {
Int64Data::read(reader).map(RegularInstruction::Int64)
}
InstructionCode::INT_128 => {
Int128Data::read(reader).map(RegularInstruction::Int128)
}
InstructionCode::INT_BIG => {
Integer::read(reader).map(RegularInstruction::BigInteger)
}
InstructionCode::INT => {
Integer::read(reader).map(RegularInstruction::Integer)
}
InstructionCode::DECIMAL_F32 => {
Float32Data::read(reader).map(RegularInstruction::DecimalF32)
}
InstructionCode::DECIMAL_F64 => {
Float64Data::read(reader).map(RegularInstruction::DecimalF64)
}
InstructionCode::DECIMAL_BIG => {
Decimal::read(reader).map(RegularInstruction::BigDecimal)
}
InstructionCode::DECIMAL_AS_INT_16 => {
FloatAsInt16Data::read(reader)
.map(RegularInstruction::DecimalAsInt16)
}
InstructionCode::DECIMAL_AS_INT_32 => {
FloatAsInt32Data::read(reader)
.map(RegularInstruction::DecimalAsInt32)
}
InstructionCode::DECIMAL => {
Decimal::read(reader).map(RegularInstruction::Decimal)
}
InstructionCode::REMOTE_EXECUTION => {
InstructionBlockData::read(reader)
.map(RegularInstruction::RemoteExecution)
}
InstructionCode::SHORT_TEXT => {
ShortTextData::read(reader).map(RegularInstruction::ShortText)
}
InstructionCode::ENDPOINT => {
Endpoint::read(reader).map(RegularInstruction::Endpoint)
}
InstructionCode::TEXT => {
TextData::read(reader).map(RegularInstruction::Text)
}
InstructionCode::TRUE => Ok(RegularInstruction::True),
InstructionCode::FALSE => Ok(RegularInstruction::False),
InstructionCode::NULL => Ok(RegularInstruction::Null),
InstructionCode::LIST => {
ListData::read(reader).map(RegularInstruction::List)
}
InstructionCode::SHORT_LIST => {
ShortListData::read(reader).map(RegularInstruction::ShortList)
}
InstructionCode::MAP => {
MapData::read(reader).map(RegularInstruction::Map)
}
InstructionCode::SHORT_MAP => ShortMapData::read(reader)
.map(|map| MapData {
element_count: map.element_count as u32,
})
.map(RegularInstruction::ShortMap),
InstructionCode::STATEMENTS => {
StatementsData::read(reader).map(RegularInstruction::Statements)
}
InstructionCode::SHORT_STATEMENTS => {
ShortStatementsData::read(reader)
.map(RegularInstruction::ShortStatements)
}
InstructionCode::UNBOUNDED_STATEMENTS => {
Ok(RegularInstruction::UnboundedStatements)
}
InstructionCode::UNBOUNDED_STATEMENTS_END => {
UnboundedStatementsData::read(reader)
.map(RegularInstruction::UnboundedStatementsEnd)
}
InstructionCode::TAGGED_VALUE => {
TaggedValue::read(reader).map(RegularInstruction::TaggedValue)
}
InstructionCode::APPLY_ZERO => {
Ok(RegularInstruction::Apply(ApplyData { arg_count: 0 }))
}
InstructionCode::APPLY_SINGLE => {
Ok(RegularInstruction::Apply(ApplyData { arg_count: 1 }))
}
InstructionCode::APPLY => {
ApplyData::read(reader).map(RegularInstruction::Apply)
}
InstructionCode::GET_PROPERTY_TEXT => ShortTextData::read(reader)
.map(RegularInstruction::GetPropertyText),
InstructionCode::GET_PROPERTY_INDEX => UInt32Data::read(reader)
.map(RegularInstruction::GetPropertyIndex),
InstructionCode::GET_PROPERTY_DYNAMIC => {
Ok(RegularInstruction::GetPropertyDynamic)
}
InstructionCode::TAKE_PROPERTY_TEXT => ShortTextData::read(reader)
.map(RegularInstruction::TakePropertyText),
InstructionCode::TAKE_PROPERTY_INDEX => UInt32Data::read(reader)
.map(RegularInstruction::TakePropertyIndex),
InstructionCode::TAKE_PROPERTY_DYNAMIC => {
Ok(RegularInstruction::TakePropertyDynamic)
}
InstructionCode::SET_PROPERTY_TEXT => ShortTextData::read(reader)
.map(RegularInstruction::SetPropertyText),
InstructionCode::SET_PROPERTY_INDEX => UInt32Data::read(reader)
.map(RegularInstruction::SetPropertyIndex),
InstructionCode::SET_PROPERTY_DYNAMIC => {
Ok(RegularInstruction::SetPropertyDynamic)
}
InstructionCode::UNBOX => Ok(RegularInstruction::Unbox),
InstructionCode::MODIFY_SHARED_CONTAINER_VALUE => {
ModifySharedContainerValue::read(reader)
.map(RegularInstruction::ModifySharedContainerValue)
}
InstructionCode::SET_SHARED_CONTAINER_VALUE => {
Ok(RegularInstruction::SetSharedContainerValue)
}
InstructionCode::KEY_VALUE_SHORT_TEXT => {
ShortTextData::read(reader)
.map(RegularInstruction::KeyValueShortText)
}
InstructionCode::KEY_VALUE_DYNAMIC => {
Ok(RegularInstruction::KeyValueDynamic)
}
InstructionCode::ADD => Ok(RegularInstruction::Add),
InstructionCode::SUBTRACT => Ok(RegularInstruction::Subtract),
InstructionCode::MULTIPLY => Ok(RegularInstruction::Multiply),
InstructionCode::DIVIDE => Ok(RegularInstruction::Divide),
InstructionCode::UNARY_MINUS => Ok(RegularInstruction::UnaryMinus),
InstructionCode::UNARY_PLUS => Ok(RegularInstruction::UnaryPlus),
InstructionCode::BITWISE_NOT => Ok(RegularInstruction::BitwiseNot),
InstructionCode::STRUCTURAL_EQUAL => {
Ok(RegularInstruction::StructuralEqual)
}
InstructionCode::EQUAL => Ok(RegularInstruction::Equal),
InstructionCode::NOT_STRUCTURAL_EQUAL => {
Ok(RegularInstruction::NotStructuralEqual)
}
InstructionCode::NOT_EQUAL => Ok(RegularInstruction::NotEqual),
InstructionCode::IS => Ok(RegularInstruction::Is),
InstructionCode::MATCHES => Ok(RegularInstruction::Matches),
InstructionCode::GET_SHARED_REF => {
Ok(RegularInstruction::GetSharedReference)
}
InstructionCode::GET_SHARED_REF_MUT => {
Ok(RegularInstruction::GetSharedReferenceMut)
}
InstructionCode::SHARED_REF => {
SharedRef::read(reader).map(RegularInstruction::SharedRef)
}
InstructionCode::SHARED_REF_WITH_VALUE => {
SharedRefWithValue::read(reader)
.map(RegularInstruction::SharedRefWithValue)
}
InstructionCode::CREATE_SHARED => {
Ok(RegularInstruction::CreateShared)
}
InstructionCode::CREATE_SHARED_MUT => {
Ok(RegularInstruction::CreateSharedMut)
}
InstructionCode::GET_ROOT_PROPERTY => RootProperty::read(reader)
.map(RegularInstruction::GetRootProperty),
InstructionCode::PUSH_TO_STACK => {
Ok(RegularInstruction::PushToStack)
}
InstructionCode::PUSH_LIST_TO_STACK => {
Ok(RegularInstruction::PushListToStack)
}
InstructionCode::CLONE_STACK_VALUE => StackIndex::read(reader)
.map(RegularInstruction::CloneStackValue),
InstructionCode::BORROW_STACK_VALUE => StackIndex::read(reader)
.map(RegularInstruction::BorrowStackValue),
InstructionCode::GET_STACK_VALUE_SHARED_REF => {
StackIndex::read(reader)
.map(RegularInstruction::GetStackValueSharedRef)
}
InstructionCode::GET_STACK_VALUE_SHARED_REF_MUT => {
StackIndex::read(reader)
.map(RegularInstruction::GetStackValueSharedRefMut)
}
InstructionCode::TAKE_STACK_VALUE => {
StackIndex::read(reader).map(RegularInstruction::TakeStackValue)
}
InstructionCode::SET_STACK_VALUE => {
StackIndex::read(reader).map(RegularInstruction::SetStackValue)
}
InstructionCode::REQUEST_REMOTE_SHARED_REF => {
RemotePointerAddress::read(reader)
.map(RegularInstruction::RequestRemoteSharedRef)
}
InstructionCode::REQUEST_REMOTE_SHARED_REF_MUT => {
RemotePointerAddress::read(reader)
.map(RegularInstruction::RequestRemoteSharedRefMut)
}
InstructionCode::GET_LOCAL_SHARED_REF => {
SelfOwnedPointerAddress::read(reader)
.map(RegularInstruction::GetLocalSharedRef)
}
InstructionCode::GET_CORE_LIB_VALUE => CoreLibIdIndex::read(reader)
.map(RegularInstruction::GetCoreLibValue),
InstructionCode::PERFORM_MOVES => {
PerformMoves::read(reader).map(RegularInstruction::PerformMoves)
}
InstructionCode::MOVE => {
Move::read(reader).map(RegularInstruction::Move)
}
InstructionCode::MODIFY_STACK_VALUE => {
ModifyStackValue::read(reader)
.map(RegularInstruction::ModifyStackValue)
}
InstructionCode::TYPED_VALUE => Ok(RegularInstruction::TypedValue),
InstructionCode::TYPE_EXPRESSION => {
Ok(RegularInstruction::TypeExpression)
}
InstructionCode::RANGE => Ok(RegularInstruction::Range),
InstructionCode::MODULO => todo!(),
InstructionCode::POWER => todo!(),
InstructionCode::AND => todo!(),
InstructionCode::OR => todo!(),
InstructionCode::NOT => todo!(),
InstructionCode::INCREMENT => todo!(),
InstructionCode::DECREMENT => todo!(),
}
}
fn read_regular_instruction_code<R: Read + Seek>(
mut reader: &mut R,
) -> Result<InstructionCode, DXBParserError> {
let instruction_code = u8::read(&mut reader)
.map_err(|_| DXBParserError::FailedToReadInstructionCode)?;
InstructionCode::try_from(instruction_code).map_err(|_| {
DXBParserError::InvalidInstructionCode(instruction_code)
})
}
pub fn metadata_string(&self) -> Option<String> {
let mut string = String::new();
match self {
RegularInstruction::Int8(data) => {
write!(string, "{}", data.0)
}
RegularInstruction::Int16(data) => {
write!(string, "{}", data.0)
}
RegularInstruction::Int32(data) => {
write!(string, "{}", data.0)
}
RegularInstruction::Int64(data) => {
write!(string, "{}", data.0)
}
RegularInstruction::Int128(data) => {
write!(string, "{}", data.0)
}
RegularInstruction::UInt8(data) => {
write!(string, "{}", data.0)
}
RegularInstruction::UInt16(data) => {
write!(string, "{}", data.0)
}
RegularInstruction::UInt32(data) => {
write!(string, "{}", data.0)
}
RegularInstruction::UInt64(data) => {
write!(string, "{}", data.0)
}
RegularInstruction::UInt128(data) => {
write!(string, "{}", data.0)
}
RegularInstruction::Apply(count) => {
write!(string, "[arg_count: {}]", count.arg_count)
}
RegularInstruction::BigInteger(data) => {
write!(string, "{}", data.0)
}
RegularInstruction::Integer(data) => {
write!(string, "{}", data.0)
}
RegularInstruction::Endpoint(data) => {
write!(string, "{data}")
}
RegularInstruction::DecimalAsInt16(data) => {
write!(string, "{}", data.0)
}
RegularInstruction::DecimalAsInt32(data) => {
write!(string, "{}", data.0)
}
RegularInstruction::DecimalF32(data) => {
write!(
string,
"{}",
TypedDecimal::F32(data.0.into())
)
}
RegularInstruction::DecimalF64(data) => {
write!(
string,
"{}",
TypedDecimal::F64(data.0.into())
)
}
RegularInstruction::BigDecimal(data) => {
write!(string, "{}", data)
}
RegularInstruction::Decimal(data) => {
write!(string, "{}", data)
}
RegularInstruction::ShortText(data) => {
write!(string, "{}", data.0)
}
RegularInstruction::Text(data) => {
write!(string, "{}", data.0)
}
RegularInstruction::Statements(data) => {
write!(string, "[count: {}, terminated: {}]", data.statements_count, data.terminated)
}
RegularInstruction::ShortStatements(data) => {
write!(string, "[count: {}, terminated: {}]", data.statements_count, data.terminated)
}
RegularInstruction::List(data) => {
write!(string, "{}", data.element_count)
}
RegularInstruction::ShortList(data) => {
write!(string, "{}", data.element_count)
}
RegularInstruction::Map(data) => {
write!(string, "{}", data.element_count)
}
RegularInstruction::ShortMap(data) => {
write!(string, "{}", data.element_count)
}
RegularInstruction::KeyValueShortText(data) => {
write!(string, "{}", data.0)
}
RegularInstruction::CloneStackValue(address) => {
write!(string, "{}", address.0)
}
RegularInstruction::GetRootProperty(property) => {
write!(string, "$.{}", property)
}
RegularInstruction::BorrowStackValue(address) => {
write!(string, "{}", address.0)
}
RegularInstruction::GetStackValueSharedRef(address) => {
write!(string, "{}", address.0)
}
RegularInstruction::GetStackValueSharedRefMut(address) => {
write!(string, "{}", address.0)
}
RegularInstruction::TakeStackValue(address) => {
write!(string, "{}", address.0)
}
RegularInstruction::SetStackValue(address) => {
write!(string, "{}", address.0)
}
RegularInstruction::ModifySharedContainerValue(set_shared_container_value) => {
write!(string, "{}", set_shared_container_value.operator)
}
RegularInstruction::RequestRemoteSharedRef(address) => {
write!(
string,
"[endpoint: {}, address:{}]",
address.endpoint(),
address
)
}
RegularInstruction::RequestRemoteSharedRefMut(address) => {
write!(
string,
"[endpoint: {}, address:{}]",
address.endpoint(),
address
)
}
RegularInstruction::GetLocalSharedRef(address) => {
write!(
string,
"[address:{}]",
address
)
}
RegularInstruction::GetCoreLibValue(address) => {
write!(
string,
"[id: {:4x}]",
&address.0
)
}
RegularInstruction::SharedRef(shared_ref) => {
write!(
string,
"[ref_mutability: {:?}, address: {}]",
shared_ref.ref_mutability, shared_ref.address.clone()
)
}
RegularInstruction::SharedRefWithValue(shared_ref) => {
write!(
string,
"[ref_mutability: {:?}, address: {}, container_mutability: {:?}]",
shared_ref.ref_mutability,
PointerAddress::from(shared_ref.address.clone()),
shared_ref.container_mutability
)
}
RegularInstruction::PerformMoves(perform_move) => {
write!(
string,
"[pointers: {}]",
perform_move.pointers.iter().map(|(_mut, addr)| addr.to_string()).collect::<Vec<_>>().join(", ")
)
}
RegularInstruction::Move(mv) => {
write!(
string,
"[pointer_count: {}, mappings: {:?}]",
mv.pointer_count, mv.address_mappings
)
}
RegularInstruction::RemoteExecution(data) => {
write!(
string,
"[length: {}, injected_variables: {:?}]",
data.length,
data.injected_values
)
}
#[cfg(feature = "disassembler")]
RegularInstruction::_RemoteExecutionDebugTree(data) => {
write!(
string,
"[length: {}, injected_variables: {:?}]",
data.length,
data.injected_values
)
}
#[cfg(feature = "disassembler")]
RegularInstruction::_RemoteExecutionDebugFlat(data) => {
write!(
string,
"[length: {}, injected_variables: {:?}]",
data.length,
data.injected_values
)
}
RegularInstruction::ModifyStackValue(modify_slot) => {
write!(string, "[index: {:?}, operator: {}]", modify_slot.index, modify_slot.operator)
}
RegularInstruction::GetPropertyIndex(uint_32_data) => {
write!(string, "{}", uint_32_data.0)
}
RegularInstruction::SetPropertyIndex(uint_32_data) => {
write!(string, "{}", uint_32_data.0)
}
RegularInstruction::TakePropertyIndex(uint_32_data) => {
write!(string, "{}", uint_32_data.0)
}
RegularInstruction::GetPropertyText(short_text_data) => {
write!(string, "{}", short_text_data.0)
}
RegularInstruction::TakePropertyText(short_text_data) => {
write!(string, "{}", short_text_data.0)
}
RegularInstruction::SetPropertyText(short_text_data) => {
write!(string, "{}", short_text_data.0)
}
_ => {
return None;
}
}.unwrap();
Some(string)
}
#[cfg(feature = "disassembler")]
pub fn inner_instructions(&self) -> InnerInstructions<'_> {
match self {
RegularInstruction::_RemoteExecutionDebugTree(data) => {
InnerInstructions::Tree(&data.body)
}
RegularInstruction::_RemoteExecutionDebugFlat(data) => {
InnerInstructions::Flat(&data.body)
}
_ => InnerInstructions::None,
}
}
}
#[cfg(feature = "disassembler")]
impl Serialize for RegularInstruction {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
let instruction_code = InstructionCode::from(self).to_string();
let metadata_string = self.metadata_string();
if let Some(metadata_string) = metadata_string {
let inner_instructions = self.inner_instructions();
let count = if inner_instructions == InnerInstructions::None {
2
} else {
3
};
let mut state = serializer.serialize_tuple(count)?;
state.serialize_element(&instruction_code)?;
state.serialize_element(&metadata_string)?;
match inner_instructions {
InnerInstructions::Tree(data) => {
state.serialize_element(&data)?
}
InnerInstructions::Flat(data) => {
state.serialize_element(&data)?
}
InnerInstructions::None => {}
}
state.end()
} else {
serializer.serialize_str(&instruction_code)
}
}
}
impl BinRead for RegularInstruction {
type Args<'a> = ();
fn read_options<R: Read + Seek>(
reader: &mut R,
_endian: Endian,
_: Self::Args<'_>,
) -> BinResult<Self> {
let instruction_code =
RegularInstruction::read_regular_instruction_code(reader).map_err(
|e| binrw::Error::AssertFail {
pos: reader.stream_position().unwrap_or(0),
message: e.to_string(),
},
)?;
RegularInstruction::read_instruction(reader, instruction_code)
}
}
impl ReadEndian for RegularInstruction {
const ENDIAN: EndianKind = EndianKind::Endian(Endian::Little);
}
impl Display for RegularInstruction {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
let code = InstructionCode::from(self);
write!(f, "{}", code)?;
if let Some(metadata_string) = self.metadata_string() {
write!(f, " {}", metadata_string)?;
}
Ok(())
}
}