@@ -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 }
0 commit comments