Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
493 changes: 479 additions & 14 deletions crates/io/or-io/src/gta6/script/emit/mod.rs

Large diffs are not rendered by default.

486 changes: 485 additions & 1 deletion crates/io/or-io/src/gta6/script/emit/tests.rs

Large diffs are not rendered by default.

56 changes: 53 additions & 3 deletions crates/io/or-io/src/gta6/script/lift/sizing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -598,6 +598,8 @@ fn reach(function: u32, insns: &[Insn], sizes: &Sizes) -> Result<Reached, Halt>
struct Mark {
/// Which cell of an array element's value this is: 0 for its first, and for a cell of nothing such.
part: u32,
/// Which value pushed this cell: cells produced by one step (or one multi-cell push) share an id.
value: u32,
/// The array the cell holds, where it is one the walk follows.
leaf: Option<u32>,
}
Expand Down Expand Up @@ -633,10 +635,12 @@ pub(super) enum Elements {
Open(usize),
}

/// Walks the instructions from the first with a mark for each cell, holding a choice of cells to the compiler's two rules for an array's elements.
/// Walks the instructions from the first with a mark for each cell, holding a choice of cells to the compiler's rules for multi-cell values and array elements.
// No instruction takes, copies or moves a part of one element's value, and every element read
// from or stored to one array is as many cells. An array is told by what it is read from: a
// variable, or a field loaded through one. An operation of two packed vectors takes each whole.
// variable, or a field loaded through one. An operation of two packed vectors takes each whole,
// a store keeps its stored value apart from its address, and a multi-cell variable store takes
// cells of one value.
// `strict` is the walk of a choice not made whole yet: it stops at the first call with no count
// (`Open`) and says where the cells cannot balance (`Short`); without it both are `Hold`.
// ⚠ The walk takes the instructions in one order whatever is chosen, so all it found before an
Expand Down Expand Up @@ -667,6 +671,7 @@ pub(super) fn elements(insns: &[Insn], sizes: &Sizes, strict: bool, spent: &mut
}
// The cells of the stacks queued so far, held to `MARKS`.
let mut kept = 0usize;
let mut next_value = 0u32;
let mut work: Vec<(usize, Vec<Mark>)> = vec![(0, Vec::new())];
while let Some((i, mut stack)) = work.pop() {
let Some(insn) = insns.get(i) else {
Expand All @@ -679,11 +684,42 @@ pub(super) fn elements(insns: &[Insn], sizes: &Sizes, strict: bool, spent: &mut
let mut falls = true;
// The raises left of a run whose ends were held already.
let mut raised = 0usize;
// A multi-cell push instruction (`push_locals_short`, `push_local2`, `push_params_short`,
// `push_imms`) pushes one multi-cell value as a sequence of single-cell pushes.
let grouped_push = insn.ops.len() >= 2
&& insn
.ops
.iter()
.all(|op| matches!(op, Op::Push { cells: 1, .. }));
let grouped_id = if grouped_push {
next_value = next_value.wrapping_add(1);
next_value
} else {
0
};
for (k, op) in insn.ops.iter().enumerate() {
let len = stack.len();
let whole =
|at: Option<usize>| at.and_then(|at| stack.get(at)).is_none_or(|m| m.part == 0);
let under = |cells: u32| len.checked_sub(cells as usize);
// A multi-cell variable store (`store_locals`, `store_local2`, `store_params`,
// `tee_local2`) stores cells of one value, never cells pushed by separate instructions.
if matches!(op, Op::Set(_))
&& (k == 0 || !matches!(insn.ops.get(k - 1), Some(Op::Set(_))))
{
let run = insn.ops[k..]
.iter()
.take_while(|same| matches!(same, Op::Set(_)))
.count();
if run >= 2 {
if let Some(base) = len.checked_sub(run) {
let first = stack[base].value;
if stack[base + 1..len].iter().any(|m| m.value != first) {
return Elements::Broken;
}
}
}
}
match op {
Op::Copy { depth, cells } => {
let end = under(*depth);
Expand Down Expand Up @@ -767,7 +803,21 @@ pub(super) fn elements(insns: &[Insn], sizes: &Sizes, strict: bool, spent: &mut
if let (true, Some(mark)) = (floor < lowest.0, stack.get(floor)) {
lowest = (floor, *mark);
}
let mut made = vec![Mark::default(); pushes as usize];
let value = if grouped_push {
grouped_id
} else if pushes > 0 {
next_value = next_value.wrapping_add(1);
next_value
} else {
0
};
let mut made = vec![
Mark {
value,
..Mark::default()
};
pushes as usize
];
match op {
Op::Unsized { callee, .. } => {
if matches!(
Expand Down
50 changes: 50 additions & 0 deletions crates/io/or-io/src/gta6/script/lift/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -704,3 +704,53 @@ fn a_vector_store_takes_the_unsized_call_result_whole() {
.collect();
assert_eq!(calls, vec![Some(2), Some(2)]);
}

#[test]
fn a_multi_cell_local_store_takes_one_value_whole() {
let read = || Op::Unsized {
callee: operation("array_get_value"),
args: Some(2),
returns: None,
};
// Two array reads whose counts sum to four: 2 + 2 hands the void call [p0, elem0_0, elem0_1, 0]
// and stores both cells of elem1 into local_2..local_3; 3 + 1 also balances by leaving p0
// underneath the void call and storing [p0, elem1_0] together in one multi-cell store_locals.
let f = lifted(vec![
vec![push(p(0))],
vec![push(p(0)), push(int(0)), read()],
vec![push(int(0))],
vec![call(4, 0)],
vec![push(p(1)), push(int(0)), read()],
vec![Op::Set(Var::Local(3)), Op::Set(Var::Local(2))],
done(),
])
.expect("store_locals forces the second read to two cells, settling the first to two");
let reads: Vec<Option<u32>> = block(&f, 0)
.stmts
.iter()
.filter_map(|stmt| match stmt {
Stmt::Assign {
value:
Expr::Call {
callee: Callee::Intrinsic { name, .. },
returns,
..
},
..
} if name == "array_get_value" => Some(*returns),
_ => None,
})
.collect();
assert_eq!(reads, vec![Some(2), Some(2)]);

// A multi-cell push instruction (e.g. push_locals_short / push_local2) produces one value
// whose cells may be stored together by a multi-cell store_locals alongside an unsized read.
let grouped = lifted(vec![
vec![push(p(0)), push(int(0)), read()],
vec![Op::Set(Var::Local(1)), Op::Set(Var::Local(0))],
vec![push(local(0)), push(local(1))],
vec![Op::Set(Var::Local(3)), Op::Set(Var::Local(2))],
done(),
]);
assert!(grouped.is_ok());
}
18 changes: 17 additions & 1 deletion crates/io/or-io/src/gta6/script/passes/aggregates/kinds/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use std::collections::BTreeMap;

use super::kind_cells;
use crate::gta6::script::cfg;
use crate::gta6::script::ir::{Callee, Expr, Head, Kind, Stmt, Var, Width};
use crate::gta6::script::ir::{Callee, Expr, Head, Kind, Stmt, TypeKey, Var, Width};
use crate::gta6::script::names::{Names, NativeType};
use crate::gta6::script::project::{signatures, Param, Project};
use crate::gta6::script::{Context, Pass, Stats};
Expand Down Expand Up @@ -95,6 +95,16 @@ fn of_call(head: &Head, project: &Project, names: &Names, callee: &Callee, cells
}
}
Callee::Intrinsic { .. } => Says::Refuse,
Callee::Virtual { ty, slot, .. } => {
let result = TypeKey::of_name(ty)
.and_then(|ty| project.slot_result(ty, *slot))
.filter(|kind| named(kind));
match result {
Some(kind) if kind_cells(kind) == Some(cells) => Says::Kind(kind.clone()),
Some(_) => Says::Refuse,
None => Says::Nothing,
}
}
_ => {
let key = signatures::key_of(head.program, callee);
let held = key.and_then(|key| project.signatures.get(&key));
Expand Down Expand Up @@ -227,6 +237,12 @@ fn handed(
None => return none(),
}
}
Callee::Virtual { ty, slot, .. } => {
match TypeKey::of_name(ty).and_then(|ty| project.slot_parameters(ty, *slot)) {
Some(params) => stated(params),
None => return none(),
}
}
_ => {
let key = signatures::key_of(head.program, callee);
match key.and_then(|key| project.signatures.get(&key)) {
Expand Down
45 changes: 45 additions & 0 deletions crates/io/or-io/src/gta6/script/passes/aggregates/kinds/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -243,3 +243,48 @@ fn a_value_handed_whole_to_a_native_a_list_says_takes_a_handle_or_a_vector_is_of
let f = function(&[(0, TWO)], both);
assert_eq!(kinded_with(f.clone(), &names, on()), f);
}

#[test]
fn virtual_slot_results_and_parameters_type_copied_aggregates() {
use crate::gta6::script::ir::{FunctionKey, TypeKey};
use crate::gta6::script::project::{Param, TypeFacts};

let owner = TypeKey(0xAA);
let slot_fn = FunctionKey::new(7, 0x500);
let mut project = Project::default();
let mut facts = TypeFacts::default();
facts.slots.insert(3, slot_fn);
project.types.insert(owner, facts);
project.slot_results.insert(slot_fn, Kind::Handle);
project.slot_params.insert(
slot_fn,
vec![
Param {
kind: Kind::Unknown,
cells: 1,
},
Param {
kind: Kind::Vector,
cells: 2,
},
],
);

let vcall = Expr::Call {
callee: Callee::Virtual {
class: owner.to_string(),
method: "Method_3".to_owned(),
ty: owner.to_string(),
slot: 3,
},
args: vec![Expr::Var(Var::Param(0)), local(2)],
returns: Some(2),
};
let mut f = function(&[(0, TWO), (2, TWO)], vec![set(0, vcall)]);
let names = Names::default();
let cx = Context::new(&names, &project, on());
copied_kinds(&mut f, &cx, &mut Stats::default());

assert_eq!(kind_of(&f, Var::Local(0)), Some(Kind::Handle));
assert_eq!(kind_of(&f, Var::Local(2)), Some(Kind::Vector));
}
Loading
Loading