Skip to content

Commit fcea26c

Browse files
committed
fix deep out/ref
1 parent 83c701d commit fcea26c

10 files changed

Lines changed: 287 additions & 79 deletions

File tree

‎src/men-sharp-codegen/src/generator.rs‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -773,6 +773,16 @@ struct Disposal {
773773
check_null: bool,
774774
}
775775

776+
/// A `ref`/`out` argument to a source method that was stood in for by a
777+
/// value cell (see `value_cell`): after the call, the cell's value goes
778+
/// home to `place` through `temporary`, a slot of the place's type.
779+
#[derive(Clone)]
780+
struct ByRefWriteBack {
781+
cell: DataId,
782+
temporary: DataId,
783+
place: Place,
784+
}
785+
776786
/// An assignable location.
777787
#[derive(Clone)]
778788
enum Place {

‎src/men-sharp-codegen/src/generator/delegates.rs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1102,7 +1102,7 @@ impl<'a, 'ast> Generator<'a, 'ast> {
11021102
call: &ResolvedCall,
11031103
receiver: Option<(DataId, Type)>,
11041104
values: Vec<DataId>,
1105-
source_by_ref: Vec<(DataId, Place)>,
1105+
source_by_ref: Vec<ByRefWriteBack>,
11061106
span: Range<usize>,
11071107
) -> Piece {
11081108
let Some((delegate, delegate_type)) = receiver else {

‎src/men-sharp-codegen/src/generator/expressions.rs‎

Lines changed: 53 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -4779,15 +4779,16 @@ impl<'a, 'ast> Generator<'a, 'ast> {
47794779
// `ref`/`out` slots standing in for a field, element or property: the
47804780
// extern writes the slot, and afterwards the slot is written home
47814781
let mut write_backs: Vec<(Place, DataId)> = Vec::new();
4782-
// for calls into source: `ref`/`out` arguments stood in for by a named
4783-
// temporary (an element, a property), and where each goes home after
4784-
let mut source_by_ref: Vec<(DataId, Place)> = Vec::new();
4782+
// for calls into source: `ref`/`out` arguments stood in for by a
4783+
// value cell (a frame local, a property), and where each goes home
4784+
// after
4785+
let mut source_by_ref: Vec<ByRefWriteBack> = Vec::new();
47854786
// for calls into *another* behaviour: Udon only raises an event by
47864787
// name, with the arguments written to the callee's variables first
47874788
// and `ref`/`out` ones read back after — values, not references
47884789
// (argument index, where the value read back goes)
47894790
let mut remote_by_ref: Vec<(usize, Place)> = Vec::new();
4790-
let mut ref_scratches: Vec<(DataId, DataId)> = Vec::new();
4791+
let mut ref_cells: Vec<(DataId, DataId)> = Vec::new();
47914792
let cross_program = receiver
47924793
.as_ref()
47934794
.is_some_and(|(_, receiver_type)| self.is_program_reference(receiver_type));
@@ -4856,7 +4857,7 @@ impl<'a, 'ast> Generator<'a, 'ast> {
48564857
argument,
48574858
modifier,
48584859
&parameter_type,
4859-
&mut ref_scratches,
4860+
&mut ref_cells,
48604861
) {
48614862
Some((cell, write_back)) => {
48624863
ordered[slot] = Some(cell);
@@ -5165,7 +5166,7 @@ impl<'a, 'ast> Generator<'a, 'ast> {
51655166
receiver: Option<(DataId, Type)>,
51665167
mut values: Vec<DataId>,
51675168
write_backs: Vec<(Place, DataId)>,
5168-
source_by_ref: Vec<(DataId, Place)>,
5169+
source_by_ref: Vec<ByRefWriteBack>,
51695170
remote_by_ref: Vec<(usize, Place)>,
51705171
span: Range<usize>,
51715172
non_virtual: bool,
@@ -5214,8 +5215,14 @@ impl<'a, 'ast> Generator<'a, 'ast> {
52145215
if let Some(piece) =
52155216
self.try_reflect_intrinsic(ctx, call, symbol, &values, span.clone())
52165217
{
5217-
for (temporary, place) in source_by_ref {
5218-
self.write_place(ctx, place, temporary, span.clone());
5218+
for write_back in source_by_ref {
5219+
self.read_value_cell(
5220+
ctx,
5221+
write_back.cell,
5222+
write_back.temporary,
5223+
span.clone(),
5224+
);
5225+
self.write_place(ctx, write_back.place, write_back.temporary, span.clone());
52195226
}
52205227
return piece;
52215228
}
@@ -5681,19 +5688,20 @@ impl<'a, 'ast> Generator<'a, 'ast> {
56815688
/// A `ref`/`out` argument to a source method, as the reference cell the
56825689
/// callee aliases the variable through (see `Place::ByName`): a variable
56835690
/// with a heap symbol of its own is named directly, another behaviour's
5684-
/// public variable likewise, a reference parameter is passed on as is —
5685-
/// and anything else (an element, a property, a captured local) is stood
5686-
/// in for by a named temporary, returned with the place it goes home to.
5691+
/// public variable likewise, a reference parameter is passed on as is,
5692+
/// an element is named through its array — and anything else (a frame
5693+
/// local, a property) is stood in for by a value cell, returned with the
5694+
/// place it goes home to.
56875695
fn source_by_ref_argument(
56885696
&mut self,
56895697
ctx: &mut Ctx<'ast>,
56905698
argument: &'ast Argument<'ast, 'ast>,
56915699
modifier: ArgumentModifier,
56925700
parameter_type: &Type,
5693-
// (referent slot, its scratch) for the call so far: two arguments
5694-
// naming one variable share one scratch, and so one referent
5695-
scratches: &mut Vec<(DataId, DataId)>,
5696-
) -> Option<(DataId, Option<(DataId, Place)>)> {
5701+
// (referent slot, its cell) for the call so far: two arguments naming
5702+
// one variable share one cell, and so one referent
5703+
cells: &mut Vec<(DataId, DataId)>,
5704+
) -> Option<(DataId, Option<ByRefWriteBack>)> {
56975705
let span = argument.span.clone();
56985706
let discard = self.discard_argument_slot(ctx, argument, parameter_type);
56995707
let place = match &argument.value {
@@ -5724,31 +5732,24 @@ impl<'a, 'ast> Generator<'a, 'ast> {
57245732
Place::Error => None,
57255733
// a slot of this function's own frame (a local, a parameter): a
57265734
// recursive call's frame restore would rewind what the callee
5727-
// wrote into it, so it is stood in for by a scratch outside every
5728-
// frame — one per variable, so `F(ref x, ref x)` still aliases —
5729-
// copied home after the call. A field or static, being no
5730-
// function's, is named directly
5735+
// wrote into it, so it is stood in for by a value cell — one per
5736+
// variable, so `F(ref x, ref x)` still aliases — read home after
5737+
// the call. A field or static, being no function's, is named
5738+
// directly
57315739
Place::Slot(slot, ty) if self.is_frame_local(ctx, slot) => {
5732-
let (scratch, fresh) =
5733-
match scratches.iter().find(|(referent, _)| *referent == slot) {
5734-
Some(&(_, scratch)) => (scratch, false),
5735-
None => {
5736-
let udon_type = self.program.data[slot.0].udon_type.clone();
5737-
let scratch = self.scratch_slot(&udon_type);
5738-
scratches.push((slot, scratch));
5739-
(scratch, true)
5740-
}
5741-
};
5742-
if fresh {
5743-
// the callee may read before writing (and an unwritten
5744-
// value slot would be null): the current value goes first
5745-
self.copy(slot, scratch);
5740+
if let Some(&(_, cell)) = cells.iter().find(|(referent, _)| *referent == slot) {
5741+
return Some((cell, None));
57465742
}
5747-
let name = self.program.data[scratch.0].name.clone();
5748-
let name = self.string_constant(&name);
5749-
let target = self.self_behaviour_slot();
5750-
let cell = self.reference_cell(ctx, target, name, span);
5751-
Some((cell, fresh.then_some((scratch, Place::Slot(slot, ty)))))
5743+
let cell = self.value_cell(ctx, Some(slot), span);
5744+
cells.push((slot, cell));
5745+
Some((
5746+
cell,
5747+
Some(ByRefWriteBack {
5748+
cell,
5749+
temporary: slot,
5750+
place: Place::Slot(slot, ty),
5751+
}),
5752+
))
57525753
}
57535754
Place::Slot(slot, _) => {
57545755
let name = self.program.data[slot.0].name.clone();
@@ -5780,17 +5781,23 @@ impl<'a, 'ast> Generator<'a, 'ast> {
57805781
}
57815782
other => {
57825783
let temporary = self.temp_for(parameter_type);
5783-
if modifier == ArgumentModifier::Ref {
5784+
let initial = if modifier == ArgumentModifier::Ref {
57845785
// the callee may read before writing: the current value
57855786
// has to be there first
57865787
let (value, _) = self.read_place(ctx, other.clone(), span.clone())?;
5787-
self.copy(value, temporary);
5788-
}
5789-
let name = self.program.data[temporary.0].name.clone();
5790-
let name = self.string_constant(&name);
5791-
let target = self.self_behaviour_slot();
5792-
let cell = self.reference_cell(ctx, target, name, span);
5793-
Some((cell, Some((temporary, other))))
5788+
Some(value)
5789+
} else {
5790+
None
5791+
};
5792+
let cell = self.value_cell(ctx, initial, span);
5793+
Some((
5794+
cell,
5795+
Some(ByRefWriteBack {
5796+
cell,
5797+
temporary,
5798+
place: other,
5799+
}),
5800+
))
57945801
}
57955802
}
57965803
}

‎src/men-sharp-codegen/src/generator/functions.rs‎

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -904,10 +904,11 @@ impl<'a, 'ast> Generator<'a, 'ast> {
904904
key: &FunctionKey,
905905
this: Option<DataId>,
906906
arguments: &[DataId],
907-
// `ref`/`out` arguments with no heap symbol of their own (an array
908-
// element, a property): the callee wrote the named temporary the
909-
// caller stood in for it, which is written home after the call
910-
write_backs: &[(DataId, Place)],
907+
// `ref`/`out` arguments with no heap symbol of their own the callee
908+
// could be given (a frame local, a property): the callee wrote the
909+
// value cell the caller stood in for it, which is read home after
910+
// the call
911+
write_backs: &[ByRefWriteBack],
911912
span: Range<usize>,
912913
) -> Option<DataId> {
913914
self.ensure_function(key);
@@ -991,10 +992,23 @@ impl<'a, 'ast> Generator<'a, 'ast> {
991992
self.program.code.push(Op::RestoreFrame(marker));
992993

993994
// a `ref`/`out` argument that had to be stood in for: the callee
994-
// wrote the caller's temporary by name; it goes home now. (A variable
995-
// with a symbol of its own was written directly — nothing to copy.)
996-
for (temporary, place) in write_backs {
997-
self.write_place(ctx, place.clone(), *temporary, span.clone());
995+
// wrote the value cell; it goes home now — after the frame restore,
996+
// which is what a slot written by name would have been rewound by.
997+
// (A variable with a symbol of its own was written directly —
998+
// nothing to copy.)
999+
for write_back in write_backs {
1000+
self.read_value_cell(ctx, write_back.cell, write_back.temporary, span.clone());
1001+
// a frame local is read straight into its slot
1002+
if matches!(write_back.place, Place::Slot(slot, _) if slot == write_back.temporary) {
1003+
self.effects.writes += 1;
1004+
continue;
1005+
}
1006+
self.write_place(
1007+
ctx,
1008+
write_back.place.clone(),
1009+
write_back.temporary,
1010+
span.clone(),
1011+
);
9981012
}
9991013

10001014
// the callee may have returned early with an exception pending: it

‎src/men-sharp-codegen/src/generator/programs.rs‎

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -853,6 +853,58 @@ impl<'a, 'ast> Generator<'a, 'ast> {
853853
cell
854854
}
855855

856+
/// A reference cell that is its own storage — `[cell, value, 1]`: an
857+
/// element cell naming index 1 of the cell itself. It stands in for a
858+
/// `ref`/`out` argument the callee cannot be given a heap symbol's name
859+
/// for: a slot of the caller's frame (a local, a parameter), a
860+
/// property, a `Deconstruct` part.
861+
///
862+
/// Why not a named scratch slot: a slot outside every frame is shared
863+
/// by every activation of the call site, so a recursive call through
864+
/// the same site handed the callee's own `out` parameter and the cell
865+
/// of its recursive call one slot — the callee read its parameter back
866+
/// after recursing and saw the innermost value. A slot inside the frame
867+
/// is rewound by the frame restore after the call, before the value
868+
/// can be copied home. A fresh allocation per call is per activation:
869+
/// the temp holding it is saved and restored with the frame, and the
870+
/// cell itself lives on the VM's heap until the call is over.
871+
pub(super) fn value_cell(
872+
&mut self,
873+
ctx: &Ctx<'ast>,
874+
initial: Option<DataId>,
875+
span: Range<usize>,
876+
) -> DataId {
877+
let cell = self.empty_reference_cell(ctx, span.clone());
878+
let zero = self.int_constant(0);
879+
let one = self.int_constant(1);
880+
let two = self.int_constant(2);
881+
self.set_element(ctx, cell, zero, cell, span.clone());
882+
// the callee may read before writing (`ref`): the current value goes
883+
// in first; an `out` starts null, as an unassigned slot would
884+
if let Some(initial) = initial {
885+
self.set_element(ctx, cell, one, initial, span.clone());
886+
}
887+
self.set_element(ctx, cell, two, one, span);
888+
cell
889+
}
890+
891+
/// Reads a value cell's value into `into`, a slot of the value's type.
892+
pub(super) fn read_value_cell(
893+
&mut self,
894+
ctx: &Ctx<'ast>,
895+
cell: DataId,
896+
into: DataId,
897+
span: Range<usize>,
898+
) {
899+
let one = self.int_constant(1);
900+
self.call_extern(
901+
ctx,
902+
"SystemObjectArray.__Get__SystemInt32__SystemObject",
903+
&[cell, one, into],
904+
span,
905+
);
906+
}
907+
856908
fn empty_reference_cell(&mut self, ctx: &Ctx<'ast>, span: Range<usize>) -> DataId {
857909
let three = self.int_constant(3);
858910
let cell = self.temp("SystemObjectArray");

‎src/men-sharp-codegen/src/generator/reflect.rs‎

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -352,27 +352,25 @@ impl<'a, 'ast> Generator<'a, 'ast> {
352352
bindings,
353353
};
354354
// `visitor.Visit(info, ref field)`: the field's current value
355-
// stands in, handed over as a reference cell naming it, and goes
356-
// back into the field after (see `Place::ByName`)
355+
// stands in, handed over in a value cell (see `value_cell`),
356+
// and goes back into the field after
357357
let place = Place::Field {
358358
object: target,
359359
index,
360360
ty: field.ty.clone(),
361361
};
362-
let udon_type = self.program.data[current.0].udon_type.clone();
363-
let scratch = self.scratch_slot(&udon_type);
364-
self.copy(current, scratch);
365-
let name = self.program.data[scratch.0].name.clone();
366-
let name = self.string_constant(&name);
367-
let target_behaviour = self.self_behaviour_slot();
368-
let cell = self.reference_cell(ctx, target_behaviour, name, span.clone());
362+
let cell = self.value_cell(ctx, Some(current), span.clone());
369363
self.field_visits.insert(key.clone());
370364
self.call_function(
371365
ctx,
372366
&key,
373367
Some(visitor),
374368
&[info, cell],
375-
&[(scratch, place)],
369+
&[ByRefWriteBack {
370+
cell,
371+
temporary: current,
372+
place,
373+
}],
376374
span.clone(),
377375
);
378376
}

‎src/men-sharp-codegen/src/generator/tuples.rs‎

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -718,25 +718,23 @@ impl<'a, 'ast> Generator<'a, 'ast> {
718718
let receiver = Some((value, self.substitute(ty, &ctx.key.bindings)));
719719
let (this, key) =
720720
self.source_call_target(ctx, &call, symbol, &receiver, false, span.clone())?;
721-
// one temp per `out` parameter, handed over as a reference cell
722-
// naming it (see `Place::ByName`): `Deconstruct` writes the temps
723-
// themselves, by name
724-
// the temps are frame slots a recursive `Deconstruct` would rewind,
725-
// so each is stood in for by a scratch outside every frame, copied
726-
// home after
721+
// one temp per `out` parameter, each stood in for by a value cell
722+
// (see `value_cell`: the temps are frame slots a recursive
723+
// `Deconstruct` would rewind) that `Deconstruct` writes and the
724+
// call reads home after
727725
let mut parts: Vec<(DataId, Type)> = Vec::with_capacity(wanted);
728726
let mut values: Vec<DataId> = Vec::with_capacity(wanted);
729-
let mut write_backs: Vec<(DataId, Place)> = Vec::with_capacity(wanted);
730-
let target = self.self_behaviour_slot();
727+
let mut write_backs: Vec<ByRefWriteBack> = Vec::with_capacity(wanted);
731728
for parameter in &call.signature.parameters {
732729
let part = self.substitute(&parameter.parameter_type, &ctx.key.bindings);
733730
let slot = self.temp_for(&part);
734-
let udon_type = self.program.data[slot.0].udon_type.clone();
735-
let scratch = self.scratch_slot(&udon_type);
736-
let name = self.program.data[scratch.0].name.clone();
737-
let name = self.string_constant(&name);
738-
values.push(self.reference_cell(ctx, target, name, span.clone()));
739-
write_backs.push((scratch, Place::Slot(slot, part.clone())));
731+
let cell = self.value_cell(ctx, None, span.clone());
732+
values.push(cell);
733+
write_backs.push(ByRefWriteBack {
734+
cell,
735+
temporary: slot,
736+
place: Place::Slot(slot, part.clone()),
737+
});
740738
parts.push((slot, part));
741739
}
742740
// `Deconstruct` returns void, so there is no result to take

0 commit comments

Comments
 (0)