Skip to content

Commit

Permalink
reference variables fix
Browse files Browse the repository at this point in the history
  • Loading branch information
Xytabich committed Sep 27, 2021
1 parent 417d1b4 commit 1cb0e1a
Show file tree
Hide file tree
Showing 8 changed files with 35 additions and 15 deletions.
2 changes: 1 addition & 1 deletion Assets/Katsudon/Builder/Extensions/Udon/CallExtern.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ bool IOperationBuider.Process(IMethodDescriptor method)
{
if(parameters[index].IsOut)
{
mode = VariableMeta.UsageMode.OutOnly;
mode = VariableMeta.UsageMode.OutOnly | VariableMeta.UsageMode.Out;
}
else if(!parameters[index].IsIn)
{
Expand Down
1 change: 1 addition & 0 deletions Assets/Katsudon/Builder/Extensions/Udon/LdInstanceField.cs
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ public void LoadValue(IUdonProgramBlock block)

public void StoreValue(IUdonProgramBlock block)
{
tmpVariable.Allocate();
block.machine.SetVariableExtern(targetVariable, variableName, tmpVariable);
}

Expand Down
3 changes: 2 additions & 1 deletion Assets/Katsudon/Builder/Extensions/Udon/LdLocalField.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,12 @@ bool IOperationBuider.Process(IMethodDescriptor method)
{
if(method.PeekStack(0) is ThisVariable)
{
bool isReference = method.currentOp.opCode == OpCodes.Ldflda;
FieldInfo field;
if(ILUtils.TryGetLdfld(method.currentOp, out field))
{
method.PopStack().Use();
method.PushStack(fieldsCollection.GetField(field), true);
method.PushStack(fieldsCollection.GetField(field), !isReference);
return true;
}
}
Expand Down
2 changes: 1 addition & 1 deletion Assets/Katsudon/Builder/Opcodes/CastOpcode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ public CastedVariable(Type type, IVariable variable)

void IVariable.Allocate(int count)
{
variable.Allocate();
variable.Allocate(count);
}

void IVariable.Use()
Expand Down
38 changes: 27 additions & 11 deletions Assets/Katsudon/Builder/Opcodes/LdExternField.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ bool IOperationBuider.Process(IMethodDescriptor method)
if(method.currentOp.opCode == OpCodes.Ldflda && !string.IsNullOrEmpty(nameInfo.setterName))
{
method.PushStack(new ReferenceExternVariable(nameInfo.getterName, nameInfo.setterName,
method.GetTmpVariable(target.OwnType()).Reserve(), method.GetTmpVariable(field.FieldType)));
target, method.GetTmpVariable(field.FieldType)));
}
else
{
Expand Down Expand Up @@ -57,50 +57,66 @@ private class ReferenceExternVariable : IReferenceVariable
private string loadName;
private string storeName;
private ITmpVariable tmpVariable;
private ITmpVariable targetVariable;
private IVariable targetVariable;
private bool ignoreOnUse = false;

public ReferenceExternVariable(string loadName, string storeName, ITmpVariable targetVariable, ITmpVariable tmpVariable)
public ReferenceExternVariable(string loadName, string storeName, IVariable targetVariable, ITmpVariable tmpVariable)
{
this.loadName = loadName;
this.storeName = storeName;
this.tmpVariable = tmpVariable;
this.targetVariable = targetVariable;

tmpVariable.onRelease += ReleaseValue;
tmpVariable.onUse += OnUse;
tmpVariable.onRelease += OnRelease;
}

public void Use()
{
tmpVariable.Use();
}

private void ReleaseValue()
{
tmpVariable.onRelease -= ReleaseValue;
targetVariable.Release();
}

public void Allocate(int count = 1)
{
tmpVariable.Allocate(count);
targetVariable.Allocate(count);
}

public IVariable GetValueVariable() { return tmpVariable; }

public void LoadValue(IUdonProgramBlock block)
{
targetVariable.Allocate();
ignoreOnUse = true;
block.machine.AddExtern(loadName, tmpVariable, targetVariable.OwnType());
ignoreOnUse = false;
}

public void StoreValue(IUdonProgramBlock block)
{
block.machine.AddExtern(storeName, targetVariable.Mode(VariableMeta.UsageMode.Out), tmpVariable.OwnType());
tmpVariable.Allocate();
targetVariable.Allocate();
ignoreOnUse = true;
block.machine.AddExtern(storeName, targetVariable.Mode(VariableMeta.UsageMode.OutOnly | VariableMeta.UsageMode.Out), tmpVariable.OwnType());
ignoreOnUse = false;
}

void IVariable.SetAddress(uint address)
{
throw new NotImplementedException();
}

private void OnUse()
{
if(ignoreOnUse) return;
targetVariable.Use();
}

private void OnRelease()
{
tmpVariable.onUse -= OnUse;
tmpVariable.onRelease -= OnRelease;
}
}
}
}
1 change: 1 addition & 0 deletions Assets/Katsudon/Builder/Opcodes/LdelemaOpcode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ public void LoadValue(IUdonProgramBlock block)

public void StoreValue(IUdonProgramBlock block)
{
tmpVariable.Allocate();
block.machine.AddExtern(storeName, arrayVariable.OwnType(), indexVariable.UseType(typeof(int)), tmpVariable.OwnType());
}

Expand Down
1 change: 1 addition & 0 deletions Assets/Katsudon/Builder/UdonMachine/UdonProgramBlock.cs
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,7 @@ public void ApplyReferences()
var reference = referencesStack.Pop();
referencesStackStart.Push(referencesStack.Count);
reference.StoreValue(block);
reference.Use();
referencesStackStart.Pop();
}
}
Expand Down
2 changes: 1 addition & 1 deletion Assets/Katsudon/Builder/UdonMachine/VariableMeta.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public enum UsageMode
None = 0,
In = 1,
Out = 2,
OutOnly = 4 | Out
OutOnly = 4
}
}

Expand Down

0 comments on commit 1cb0e1a

Please sign in to comment.