From 1cb0e1a363d434255de64ff34a1599dd47748eaf Mon Sep 17 00:00:00 2001 From: Xytabich Date: Mon, 27 Sep 2021 14:03:18 +0300 Subject: [PATCH] reference variables fix --- .../Builder/Extensions/Udon/CallExtern.cs | 2 +- .../Extensions/Udon/LdInstanceField.cs | 1 + .../Builder/Extensions/Udon/LdLocalField.cs | 3 +- Assets/Katsudon/Builder/Opcodes/CastOpcode.cs | 2 +- .../Katsudon/Builder/Opcodes/LdExternField.cs | 38 +++++++++++++------ .../Katsudon/Builder/Opcodes/LdelemaOpcode.cs | 1 + .../Builder/UdonMachine/UdonProgramBlock.cs | 1 + .../Builder/UdonMachine/VariableMeta.cs | 2 +- 8 files changed, 35 insertions(+), 15 deletions(-) diff --git a/Assets/Katsudon/Builder/Extensions/Udon/CallExtern.cs b/Assets/Katsudon/Builder/Extensions/Udon/CallExtern.cs index 86caab5..a3ab635 100644 --- a/Assets/Katsudon/Builder/Extensions/Udon/CallExtern.cs +++ b/Assets/Katsudon/Builder/Extensions/Udon/CallExtern.cs @@ -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) { diff --git a/Assets/Katsudon/Builder/Extensions/Udon/LdInstanceField.cs b/Assets/Katsudon/Builder/Extensions/Udon/LdInstanceField.cs index 8641882..6db81bd 100644 --- a/Assets/Katsudon/Builder/Extensions/Udon/LdInstanceField.cs +++ b/Assets/Katsudon/Builder/Extensions/Udon/LdInstanceField.cs @@ -91,6 +91,7 @@ public void LoadValue(IUdonProgramBlock block) public void StoreValue(IUdonProgramBlock block) { + tmpVariable.Allocate(); block.machine.SetVariableExtern(targetVariable, variableName, tmpVariable); } diff --git a/Assets/Katsudon/Builder/Extensions/Udon/LdLocalField.cs b/Assets/Katsudon/Builder/Extensions/Udon/LdLocalField.cs index ea2c6c4..8fa4ed9 100644 --- a/Assets/Katsudon/Builder/Extensions/Udon/LdLocalField.cs +++ b/Assets/Katsudon/Builder/Extensions/Udon/LdLocalField.cs @@ -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; } } diff --git a/Assets/Katsudon/Builder/Opcodes/CastOpcode.cs b/Assets/Katsudon/Builder/Opcodes/CastOpcode.cs index 50691aa..05e2d19 100644 --- a/Assets/Katsudon/Builder/Opcodes/CastOpcode.cs +++ b/Assets/Katsudon/Builder/Opcodes/CastOpcode.cs @@ -124,7 +124,7 @@ public CastedVariable(Type type, IVariable variable) void IVariable.Allocate(int count) { - variable.Allocate(); + variable.Allocate(count); } void IVariable.Use() diff --git a/Assets/Katsudon/Builder/Opcodes/LdExternField.cs b/Assets/Katsudon/Builder/Opcodes/LdExternField.cs index 23f2e2d..882fc27 100644 --- a/Assets/Katsudon/Builder/Opcodes/LdExternField.cs +++ b/Assets/Katsudon/Builder/Opcodes/LdExternField.cs @@ -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 { @@ -57,16 +57,18 @@ 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() @@ -74,33 +76,47 @@ 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; + } } } } \ No newline at end of file diff --git a/Assets/Katsudon/Builder/Opcodes/LdelemaOpcode.cs b/Assets/Katsudon/Builder/Opcodes/LdelemaOpcode.cs index f93d91d..eb98f9a 100644 --- a/Assets/Katsudon/Builder/Opcodes/LdelemaOpcode.cs +++ b/Assets/Katsudon/Builder/Opcodes/LdelemaOpcode.cs @@ -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()); } diff --git a/Assets/Katsudon/Builder/UdonMachine/UdonProgramBlock.cs b/Assets/Katsudon/Builder/UdonMachine/UdonProgramBlock.cs index 08c9731..6ba9191 100644 --- a/Assets/Katsudon/Builder/UdonMachine/UdonProgramBlock.cs +++ b/Assets/Katsudon/Builder/UdonMachine/UdonProgramBlock.cs @@ -219,6 +219,7 @@ public void ApplyReferences() var reference = referencesStack.Pop(); referencesStackStart.Push(referencesStack.Count); reference.StoreValue(block); + reference.Use(); referencesStackStart.Pop(); } } diff --git a/Assets/Katsudon/Builder/UdonMachine/VariableMeta.cs b/Assets/Katsudon/Builder/UdonMachine/VariableMeta.cs index 077fac3..d5f214d 100644 --- a/Assets/Katsudon/Builder/UdonMachine/VariableMeta.cs +++ b/Assets/Katsudon/Builder/UdonMachine/VariableMeta.cs @@ -28,7 +28,7 @@ public enum UsageMode None = 0, In = 1, Out = 2, - OutOnly = 4 | Out + OutOnly = 4 } }