use core::ops::Deref;
use crate::{
collections::HashMap,
core_compiler::{
core_compilation_context::ByteCursor,
shared_value_tracking::{TrackedValueCollection, TrackedValueMetadata},
to_instructions::{ToInstructions, yield_all, yield_all_collected},
value_visitor::ValueVisitor,
},
global::stack_index::StackIndex,
instruction::{
instruction_data::{MoveWithValue, SharedRef, SharedRefWithValue},
regular_instruction::RegularInstruction,
},
prelude::*,
runtime::cache::shared_references_cache::SharedReferencesCache,
shared_values::{
PointerAddress, ReferenceMutability, ReferencedSharedContainer,
SelfOwnedPointerAddress, SharedContainer, SharedContainerMutability,
SharedContainerOwnership, traits::SharedContainerCommon,
},
values::value_container::ValueContainer,
};
use binrw::{BinWrite, io::Write};
#[derive(Debug)]
struct PreambleContext {
self_referencing_containers: HashMap<SharedContainer, StackIndex>,
known_container: HashSet<SharedContainer>,
current_stack_index: StackIndex,
}
impl ValueVisitor for PreambleContext {
fn visit_shared_container<'a>(
&'a mut self,
shared_container: &'a SharedContainer,
cache: &'a mut SharedReferencesCache,
) -> Box<dyn Iterator<Item = crate::instruction::Instruction> + 'a> {
match self.self_referencing_containers.get(shared_container) {
None => {
Box::new(append_shared_container(
self,
cache,
shared_container,
self.known_container.contains(shared_container),
))
}
Some(stack_index) => {
if shared_container.treat_as_move() {
todo!("Create test case here!");
} else {
match shared_container.ownership() {
SharedContainerOwnership::Owned => {
unreachable!("Owned shared container")
}
SharedContainerOwnership::Referenced(
reference_mut,
) => match reference_mut {
ReferenceMutability::Immutable => {
Box::new(core::iter::once(
RegularInstruction::get_stack_value_shared_ref(*stack_index).into()
))
}
ReferenceMutability::Mutable => {
Box::new(core::iter::once(
RegularInstruction::get_stack_value_shared_ref_mut(*stack_index).into()
))
}
},
}
}
}
}
}
}
impl PreambleContext {
fn get_next_stack_index(&mut self) -> StackIndex {
let stack_index = self.current_stack_index;
self.current_stack_index = StackIndex(self.current_stack_index.0 + 1);
stack_index
}
}
pub(super) fn append_injected_values_preamble(
collection: TrackedValueCollection, body: Vec<u8>,
cache: &mut SharedReferencesCache,
) -> (Vec<u8>, Vec<SharedContainer>) {
if !collection.has_tracked_values() {
return (body, vec![]);
}
let tracked_values = collection.tracked_values;
let mut cursor = ByteCursor::new(vec![]);
let mut root_containers =
Vec::<SharedContainer>::with_capacity(collection.root_count);
let mut root_container_stack_indices: Vec<Option<StackIndex>> =
vec![None; collection.root_count];
let inner_statement_count = &mut 0u32;
{
let context = &mut PreambleContext {
self_referencing_containers: HashMap::new(),
current_stack_index: StackIndex(0),
known_container: HashSet::new(),
};
for (tracked_value, metadata) in tracked_values.iter() {
if metadata.is_self_referencing() {
let new_index = context.get_next_stack_index();
for instruction in append_uninitialized_shared_container(
context,
cache,
tracked_value,
metadata,
inner_statement_count,
)
.collect::<Vec<_>>()
{
instruction.write(&mut cursor).unwrap();
}
context
.self_referencing_containers
.insert(tracked_value.clone(), new_index);
}
if metadata.is_known() {
context.known_container.insert(tracked_value.clone());
}
}
for (tracked_value, metadata) in tracked_values.into_iter() {
if let TrackedValueMetadata::Root {
index,
is_self_referencing,
is_known,
} = metadata
{
let new_index = if is_self_referencing {
let uninitialized_index = *context.self_referencing_containers.get(&tracked_value).expect("Self referencing container should have been inserted");
for instruction in
append_self_referencing_shared_container_to_stack(
context,
cache,
&tracked_value,
inner_statement_count,
uninitialized_index,
)
.collect::<Vec<crate::instruction::Instruction>>()
{
instruction.write(&mut cursor).unwrap();
}
uninitialized_index
} else {
let index = context.get_next_stack_index();
for instruction in append_shared_container_to_stack(
context,
cache,
&tracked_value,
is_known,
inner_statement_count,
)
.collect::<Vec<crate::instruction::Instruction>>()
{
instruction.write(&mut cursor).unwrap();
}
index
};
root_containers.push(tracked_value);
root_container_stack_indices[index.0 as usize] =
Some(new_index);
}
}
let root_container_stack_indices_sorted = root_container_stack_indices
.iter()
.map(|index| {
index.expect("Root stack index should have been registered")
})
.collect::<Vec<_>>();
RegularInstruction::list(
root_container_stack_indices_sorted.len() as u32
)
.write(&mut cursor)
.unwrap();
for stack_index in root_container_stack_indices_sorted {
RegularInstruction::take_stack_value(stack_index)
.write(&mut cursor)
.unwrap();
}
*inner_statement_count += 1;
}
let mut byte_cursor = ByteCursor::new(vec![]);
RegularInstruction::statements(2, false)
.write(&mut byte_cursor)
.unwrap();
RegularInstruction::push_list_to_stack()
.write(&mut byte_cursor)
.unwrap();
RegularInstruction::statements(*inner_statement_count, false)
.write(&mut byte_cursor)
.unwrap();
byte_cursor.write_all(&cursor.into_inner()).unwrap();
byte_cursor.write_all(&body).unwrap();
(byte_cursor.into_inner(), root_containers)
}
gen fn append_uninitialized_shared_container(
context: &mut PreambleContext,
cache: &mut SharedReferencesCache,
container: &SharedContainer,
metadata: &TrackedValueMetadata,
statements_count: &mut u32,
) -> crate::instruction::Instruction {
*statements_count += 1;
yield RegularInstruction::push_to_stack().into();
match container {
SharedContainer::Owned(owned_container) => {
for instruction in append_move_with_value(
context,
cache,
owned_container.container_mutability(),
owned_container.pointer_address().clone(),
None,
) {
yield instruction;
}
}
SharedContainer::Referenced(referenced_container) => {
if referenced_container.treat_as_move() {
for instruction in append_move_with_value(
context,
cache,
referenced_container.container_mutability(),
match referenced_container.pointer_address() {
PointerAddress::SelfOwned(address) => address.clone(),
_ => unreachable!(
"Referenced shared container should not have a remote pointer address"
),
},
None,
) {
yield instruction;
}
} else if !metadata.is_known() {
for instruction in append_referenced_shared_container_with_value(
context,
cache,
referenced_container,
true,
) {
yield instruction;
}
} else {
for instruction in
append_referenced_shared_container(referenced_container)
{
yield instruction;
}
}
}
};
}
gen fn append_self_referencing_shared_container_to_stack(
context: &mut PreambleContext,
cache: &mut SharedReferencesCache,
container: &SharedContainer,
statements_count: &mut u32,
uninitialized_index: StackIndex,
) -> crate::instruction::Instruction {
*statements_count += 1;
yield RegularInstruction::set_shared_container_value().into();
yield_all_collected!(
container
.value_container()
.deref()
.to_instructions(context, cache)
);
yield RegularInstruction::borrow_stack_value(uninitialized_index).into();
}
gen fn append_shared_container_to_stack(
context: &mut PreambleContext,
cache: &mut SharedReferencesCache,
container: &SharedContainer,
is_known: bool,
statements_count: &mut u32,
) -> crate::instruction::Instruction {
*statements_count += 1;
yield RegularInstruction::push_to_stack().into();
for instruction in
append_shared_container(context, cache, container, is_known)
{
yield instruction;
}
}
gen fn append_shared_container(
context: &mut PreambleContext,
cache: &mut SharedReferencesCache,
container: &SharedContainer,
is_known: bool,
) -> crate::instruction::Instruction {
match container {
SharedContainer::Owned(owned_container) => {
let value_container = owned_container.value_container();
for instruction in append_move_with_value(
context,
cache,
owned_container.container_mutability(),
owned_container.pointer_address().clone(),
Some(value_container.deref()),
)
.collect::<Vec<_>>()
{
yield instruction;
}
}
SharedContainer::Referenced(referenced_container) => {
if referenced_container.treat_as_move() {
let value_container = referenced_container.value_container();
for instruction in append_move_with_value(
context,
cache,
referenced_container.container_mutability(),
match referenced_container.pointer_address() {
PointerAddress::SelfOwned(address) => address.clone(),
_ => unreachable!(
"Referenced shared container should not have a remote pointer address"
),
},
Some(value_container.deref()),
).collect::<Vec<_>>() {
yield instruction;
}
} else if !is_known {
for instruction in append_referenced_shared_container_with_value(
context,
cache,
referenced_container,
false,
) {
yield instruction;
}
} else {
for instruction in
append_referenced_shared_container(referenced_container)
{
yield instruction;
}
}
}
}
}
gen fn append_move_with_value(
context: &mut PreambleContext,
cache: &mut SharedReferencesCache,
container_mutability: SharedContainerMutability,
previous_address: SelfOwnedPointerAddress,
value_container: Option<&ValueContainer>,
) -> crate::instruction::Instruction {
yield RegularInstruction::move_with_value(MoveWithValue {
mutability: container_mutability,
previous_address,
})
.into();
if let Some(value_container) = value_container {
yield_all!(value_container.to_instructions(context, cache));
} else {
yield RegularInstruction::Uninitialized.into();
}
}
gen fn append_referenced_shared_container(
referenced_container: &ReferencedSharedContainer,
) -> crate::instruction::Instruction {
match referenced_container.pointer_address() {
PointerAddress::SelfOwned(pointer_address) => {
yield RegularInstruction::shared_ref(SharedRef {
address: PointerAddress::SelfOwned(pointer_address),
ref_mutability: referenced_container.reference_mutability(),
container_mutability: referenced_container
.container_mutability(),
is_debug: referenced_container.is_debug(),
})
.into();
}
PointerAddress::Remote(pointer_address) => {
yield RegularInstruction::shared_ref(SharedRef {
address: PointerAddress::Remote(pointer_address),
ref_mutability: referenced_container.reference_mutability(),
container_mutability: referenced_container
.container_mutability(),
is_debug: referenced_container.is_debug(),
})
.into();
}
}
}
gen fn append_referenced_shared_container_with_value(
context: &mut PreambleContext,
cache: &mut SharedReferencesCache,
referenced_container: &ReferencedSharedContainer,
uninitialized: bool,
) -> crate::instruction::Instruction {
match referenced_container.pointer_address() {
PointerAddress::SelfOwned(pointer_address) => {
yield RegularInstruction::shared_ref_with_value(
SharedRefWithValue {
address: pointer_address,
ref_mutability: referenced_container.reference_mutability(),
container_mutability: referenced_container
.container_mutability(),
is_debug: referenced_container.is_debug(),
},
)
.into();
if uninitialized {
yield RegularInstruction::Uninitialized.into();
} else {
yield_all_collected!(
referenced_container
.value_container()
.deref()
.to_instructions(context, cache)
);
}
}
PointerAddress::Remote(pointer_address) => {
yield RegularInstruction::shared_ref(SharedRef {
address: PointerAddress::Remote(pointer_address),
ref_mutability: referenced_container.reference_mutability(),
container_mutability: referenced_container
.container_mutability(),
is_debug: referenced_container.is_debug(),
})
.into();
}
}
}
#[cfg(test)]
#[cfg(feature = "disassembler")]
mod tests {
use binrw::BinWrite;
use crate::{
core_compiler::{
core_compilation_context::ByteCursor,
preamble::append_injected_values_preamble,
shared_value_tracking::{
TrackedValueCollection, TrackedValueMetadata,
},
},
disassembler::{
InstructionTree,
assertions::{assert_instructions_equal, instructions},
},
global::stack_index::StackIndex,
instruction::{
Instruction,
instruction_data::{
Int32Data, MoveWithValue, SharedRefWithValue, ShortListData,
ShortMapData, ShortTextData,
},
regular_instruction::RegularInstruction,
},
prelude::*,
runtime::{
cache::shared_references_cache::SharedReferencesCache,
pointer_address_provider::SelfOwnedPointerAddressProvider,
},
shared_values::{
OwnedSharedContainer, ReferenceMutability,
ReferencedSharedContainer, SelfOwnedPointerAddress,
SharedContainer, SharedContainerMutability,
SharedContainerOwnership, traits::SharedContainerCommon,
},
values::{
core_values::{list::List, map::Map},
value_container::ValueContainer,
},
};
fn assert_preamble_instructions(
tracked_values: Vec<(SharedContainer, TrackedValueMetadata)>,
instructions: Vec<impl Into<InstructionTree<Instruction>>>,
) {
let cache = &mut SharedReferencesCache::default();
let mut cursor = ByteCursor::new(vec![]);
RegularInstruction::null().write(&mut cursor).unwrap();
let root_count = tracked_values
.iter()
.filter(|(_, metadata)| {
matches!(metadata, TrackedValueMetadata::Root { .. })
})
.count();
let collection = TrackedValueCollection {
root_count,
tracked_values,
};
let (bytes, _) = append_injected_values_preamble(
collection,
cursor.into_inner(),
cache,
);
assert_instructions_equal!(&bytes, instructions);
}
fn generate_shared_owned_value(
address_provider: &mut SelfOwnedPointerAddressProvider,
value: impl Into<ValueContainer>,
mutability: SharedContainerMutability,
) -> (OwnedSharedContainer, SelfOwnedPointerAddress) {
match generate_shared_value(
address_provider,
value,
SharedContainerOwnership::Owned,
mutability,
) {
(SharedContainer::Owned(owned_container), address) => {
(owned_container, address)
}
_ => unreachable!(),
}
}
fn generate_shared_referenced_value(
address_provider: &mut SelfOwnedPointerAddressProvider,
value: impl Into<ValueContainer>,
reference_mutability: ReferenceMutability,
mutability: SharedContainerMutability,
) -> (ReferencedSharedContainer, SelfOwnedPointerAddress) {
match generate_shared_value(
address_provider,
value,
SharedContainerOwnership::Referenced(reference_mutability),
mutability,
) {
(SharedContainer::Referenced(referenced_container), address) => {
(referenced_container, address)
}
_ => unreachable!(),
}
}
fn generate_shared_value(
address_provider: &mut SelfOwnedPointerAddressProvider,
value: impl Into<ValueContainer>,
ownership: SharedContainerOwnership,
mutability: SharedContainerMutability,
) -> (SharedContainer, SelfOwnedPointerAddress) {
let value_container = value.into();
let mut shared_container =
SharedContainer::new_owned_with_inferred_allowed_type(
value_container,
mutability,
address_provider,
);
let address = match &shared_container {
SharedContainer::Owned(owned_container) => {
owned_container.pointer_address().clone()
}
_ => unreachable!(),
};
if let SharedContainerOwnership::Referenced(mutability) = ownership {
shared_container = SharedContainer::Referenced(
shared_container
.try_derive_reference_with_mutability(mutability)
.unwrap(),
);
};
(shared_container, address)
}
#[test]
fn preamble_no_injected_values() {
assert_preamble_instructions(vec![], vec![RegularInstruction::Null]);
}
#[test]
fn preamble_single_non_referencing_ref() {
let address_provider = &mut SelfOwnedPointerAddressProvider::default();
let (reference, address) = generate_shared_referenced_value(
address_provider,
42,
ReferenceMutability::Immutable,
SharedContainerMutability::Immutable,
);
assert_preamble_instructions(
vec![(
SharedContainer::Referenced(reference),
TrackedValueMetadata::Root {
index: StackIndex(0),
is_known: false,
is_self_referencing: false,
},
)],
vec![RegularInstruction::statements_with_children(
false,
instructions!(
RegularInstruction::PushListToStack,
RegularInstruction::statements_with_children(
false,
instructions!(
RegularInstruction::PushToStack,
RegularInstruction::SharedRefWithValue(
SharedRefWithValue {
address,
ref_mutability:
ReferenceMutability::Immutable,
container_mutability:
SharedContainerMutability::Immutable,
is_debug: false,
}
),
RegularInstruction::Int32(Int32Data(42)),
RegularInstruction::list_with_children(
instructions!(
RegularInstruction::TakeStackValue(
StackIndex(0)
)
)
),
)
),
RegularInstruction::Null,
),
)],
);
}
#[test]
fn preamble_single_recursive_ref() {
let address_provider = &mut SelfOwnedPointerAddressProvider::default();
let (owned, address) = generate_shared_owned_value(
address_provider,
ValueContainer::Local(List::default().into()),
SharedContainerMutability::Mutable,
);
let owned_container = SharedContainer::Owned(owned);
{
let container_ref = SharedContainer::Referenced(
owned_container.derive_reference_with_max_mutability(),
);
let mut container_mut = owned_container.value_container_mut();
let list = container_mut.try_as_mut::<List>().unwrap();
list.push(ValueContainer::Shared(container_ref));
}
assert_preamble_instructions(
vec![(
owned_container,
TrackedValueMetadata::Root {
index: StackIndex(0),
is_known: false,
is_self_referencing: true,
},
)],
vec![RegularInstruction::statements_with_children(
false,
instructions!(
RegularInstruction::PushListToStack,
RegularInstruction::statements_with_children(
false,
instructions!(
RegularInstruction::PushToStack,
RegularInstruction::MoveWithValue(MoveWithValue {
mutability: SharedContainerMutability::Mutable,
previous_address: address.clone(),
})
.with_children(instructions!(RegularInstruction::Uninitialized)),
RegularInstruction::SetSharedContainerValue.with_children(
instructions!(
RegularInstruction::ShortList(
ShortListData { element_count: 1 }
)
.with_children(instructions!(
RegularInstruction::GetStackValueSharedRefMut(
StackIndex(0)
)
)),
RegularInstruction::BorrowStackValue(StackIndex(0))
)
),
RegularInstruction::list_with_children(
instructions!(
RegularInstruction::TakeStackValue(
StackIndex(0)
)
)
),
)
),
RegularInstruction::Null
),
)],
);
}
#[test]
fn preamble_nested_recursive_ref() {
let address_provider = &mut SelfOwnedPointerAddressProvider::default();
let (owned, address) = generate_shared_owned_value(
address_provider,
ValueContainer::Local(Map::default().into()),
SharedContainerMutability::Mutable,
);
let owned_container = SharedContainer::Owned(owned);
{
let container_ref = SharedContainer::Referenced(
owned_container.derive_reference_with_max_mutability(),
);
let mut container_mut = owned_container.value_container_mut();
let map = container_mut.try_as_mut::<Map>().unwrap();
map.set_unchecked(
"a",
Map::from(vec![(
ValueContainer::from("b"),
ValueContainer::Shared(container_ref),
)]),
);
}
assert_preamble_instructions(
vec![(
owned_container,
TrackedValueMetadata::Root {
index: StackIndex(0),
is_known: false,
is_self_referencing: true,
},
)],
vec![RegularInstruction::statements_with_children(
false,
instructions!(
RegularInstruction::PushListToStack,
RegularInstruction::statements_with_children(
false,
instructions!(
RegularInstruction::PushToStack,
RegularInstruction::MoveWithValue(MoveWithValue {
mutability: SharedContainerMutability::Mutable,
previous_address: address.clone(),
})
.with_children(
instructions!(
RegularInstruction::Uninitialized
)
),
RegularInstruction::SetSharedContainerValue.with_children(instructions!(
RegularInstruction::ShortMap(ShortMapData {
element_count: 1,
allow_only_text_keys: false,
})
.with_children(instructions!(
RegularInstruction::KeyValueShortText(
ShortTextData("a".to_string())
)
.with_children(instructions!(
RegularInstruction::ShortMap(
ShortMapData { element_count: 1, allow_only_text_keys: false, }
)
.with_children(instructions!(
RegularInstruction::KeyValueShortText(
ShortTextData("b".to_string())
)
.with_children(instructions!(
RegularInstruction::GetStackValueSharedRefMut(StackIndex(0))
))
))
)))),
RegularInstruction::BorrowStackValue(StackIndex(0))
)),
RegularInstruction::list_with_children(
instructions!(
RegularInstruction::TakeStackValue(
StackIndex(0)
)
)
)
)
),
RegularInstruction::Null
),
)],
);
}
#[test]
fn preamble_triple_ref() {
let address_provider = &mut SelfOwnedPointerAddressProvider::default();
let (owned_container_a, address_a) = generate_shared_owned_value(
address_provider,
ValueContainer::Local(Map::default().into()),
SharedContainerMutability::Mutable,
);
let (owned_container_b, address_b) = generate_shared_owned_value(
address_provider,
ValueContainer::Local(Map::default().into()),
SharedContainerMutability::Mutable,
);
let (owned_container_c, address_c) = generate_shared_owned_value(
address_provider,
ValueContainer::Local(
Map::from(vec![(
ValueContainer::from("a"),
ValueContainer::Shared(SharedContainer::Referenced(
owned_container_a.derive_with_max_mutability(),
)),
)])
.into(),
),
SharedContainerMutability::Mutable,
);
{
let mut container_mut = owned_container_a.value_container_mut();
let map = container_mut.try_as_mut::<Map>().unwrap();
map.set_unchecked(
"b",
ValueContainer::Shared(SharedContainer::Referenced(
owned_container_b.derive_with_max_mutability(),
)),
);
}
{
let mut container_mut = owned_container_b.value_container_mut();
let map = container_mut.try_as_mut::<Map>().unwrap();
map.set_unchecked(
"c",
ValueContainer::Shared(SharedContainer::Referenced(
owned_container_c.derive_with_max_mutability(),
)),
);
}
assert_preamble_instructions(
vec![
(
SharedContainer::Referenced(
owned_container_c.derive_with_max_mutability(),
),
TrackedValueMetadata::Child {
is_known: false,
is_self_referencing: true,
},
),
(
SharedContainer::Referenced(
owned_container_b.derive_with_max_mutability(),
),
TrackedValueMetadata::Child {
is_known: false,
is_self_referencing: true,
},
),
(
SharedContainer::Owned(owned_container_a),
TrackedValueMetadata::Root {
index: StackIndex(0),
is_known: false,
is_self_referencing: true,
},
),
],
vec![RegularInstruction::statements_with_children(
false,
instructions!(
RegularInstruction::PushListToStack,
RegularInstruction::statements_with_children(
false,
instructions!(
RegularInstruction::PushToStack,
RegularInstruction::SharedRefWithValue(
SharedRefWithValue {
ref_mutability:
ReferenceMutability::Mutable,
address: address_c.clone(),
container_mutability:
SharedContainerMutability::Mutable,
is_debug: false,
}
)
.with_children(
instructions!(
RegularInstruction::Uninitialized
)
),
RegularInstruction::PushToStack,
RegularInstruction::SharedRefWithValue(
SharedRefWithValue {
ref_mutability:
ReferenceMutability::Mutable,
address: address_b.clone(),
container_mutability:
SharedContainerMutability::Mutable,
is_debug: false,
}
)
.with_children(
instructions!(
RegularInstruction::Uninitialized
)
),
RegularInstruction::PushToStack,
RegularInstruction::MoveWithValue(MoveWithValue {
mutability: SharedContainerMutability::Mutable,
previous_address: address_a.clone(),
})
.with_children(
instructions!(
RegularInstruction::Uninitialized
)
),
RegularInstruction::SetSharedContainerValue.with_children(
instructions!(
RegularInstruction::ShortMap(
ShortMapData { element_count: 1, allow_only_text_keys: false }
)
.with_children(instructions!(
RegularInstruction::KeyValueShortText(
ShortTextData("b".to_string())
)
.with_children(instructions!(
RegularInstruction::GetStackValueSharedRefMut(
StackIndex(1)
)
))
)),
RegularInstruction::BorrowStackValue(StackIndex(2))
)
),
RegularInstruction::list_with_children(
instructions!(
RegularInstruction::TakeStackValue(
StackIndex(2)
)
)
)
)
),
RegularInstruction::Null
),
)],
);
}
}