diff --git a/Flazzy/ABC/AVM2/ASCode.cs b/Flazzy/ABC/AVM2/ASCode.cs index db95031..8f46329 100644 --- a/Flazzy/ABC/AVM2/ASCode.cs +++ b/Flazzy/ABC/AVM2/ASCode.cs @@ -577,95 +577,94 @@ private void LoadInstructions() var marks = new Dictionary(); var sharedExits = new Dictionary>(); var switchCases = new Dictionary>(); - using (var input = new FlashReader(_body.Code)) + + var input = new SpanFlashReader(_body.Code); + while (input.IsDataAvailable) { - while (input.IsDataAvailable) - { - long previousPosition = input.Position; - var instruction = ASInstruction.Create(_abc, input); - marks[previousPosition] = instruction; + long previousPosition = input.Position; + var instruction = ASInstruction.Create(_abc, ref input); + marks[previousPosition] = instruction; - _indices.Add(instruction, _indices.Count); - _instructions.Add(instruction); + _indices.Add(instruction, _indices.Count); + _instructions.Add(instruction); - if (!_opGroups.TryGetValue(instruction.OP, out List instructions)) - { - instructions = new List(); - _opGroups.Add(instruction.OP, instructions); - } - instructions.Add(instruction); + if (!_opGroups.TryGetValue(instruction.OP, out List instructions)) + { + instructions = new List(); + _opGroups.Add(instruction.OP, instructions); + } + instructions.Add(instruction); - if (sharedExits.TryGetValue(previousPosition, out List jumpers)) + if (sharedExits.TryGetValue(previousPosition, out List jumpers)) + { + // This is an exit position two or more jump instructions. + foreach (Jumper jumper in jumpers) { - // This is an exit position two or more jump instructions. - foreach (Jumper jumper in jumpers) - { - JumpExits.Add(jumper, instruction); - } - sharedExits.Remove(previousPosition); + JumpExits.Add(jumper, instruction); } + sharedExits.Remove(previousPosition); + } - if (switchCases.TryGetValue(previousPosition, out List<(LookUpSwitchIns, int)> caseExits)) + if (switchCases.TryGetValue(previousPosition, out List<(LookUpSwitchIns, int)> caseExits)) + { + foreach ((LookUpSwitchIns owner, int index) in caseExits) { - foreach ((LookUpSwitchIns owner, int index) in caseExits) - { - SwitchExits[owner][index] = instruction; - } - switchCases.Remove(previousPosition); + SwitchExits[owner][index] = instruction; } + switchCases.Remove(previousPosition); + } - if (instruction.OP == OPCode.LookUpSwitch) - { - var lookUpSwitchIns = (LookUpSwitchIns)instruction; - var offsets = new List(lookUpSwitchIns.CaseOffsets) { lookUpSwitchIns.DefaultOffset }; + if (instruction.OP == OPCode.LookUpSwitch) + { + var lookUpSwitchIns = (LookUpSwitchIns)instruction; + var offsets = new List(lookUpSwitchIns.CaseOffsets) { lookUpSwitchIns.DefaultOffset }; - var exits = new ASInstruction[offsets.Count]; - for (int i = 0; i < offsets.Count; i++) + var exits = new ASInstruction[offsets.Count]; + for (int i = 0; i < offsets.Count; i++) + { + long exitPosition = previousPosition + offsets[i]; + if (exitPosition <= input.Length) { - long exitPosition = previousPosition + offsets[i]; - if (exitPosition <= input.Length) + if (!switchCases.TryGetValue(exitPosition, out caseExits)) { - if (!switchCases.TryGetValue(exitPosition, out caseExits)) - { - caseExits = new List<(LookUpSwitchIns, int)>(); - switchCases.Add(exitPosition, caseExits); - } - caseExits.Add((lookUpSwitchIns, i)); + caseExits = new List<(LookUpSwitchIns, int)>(); + switchCases.Add(exitPosition, caseExits); } - else exits[i] = marks[exitPosition - uint.MaxValue - 1]; + caseExits.Add((lookUpSwitchIns, i)); } - SwitchExits.Add(lookUpSwitchIns, exits); + else exits[i] = marks[exitPosition - uint.MaxValue - 1]; } - else if (Jumper.IsValid(instruction.OP)) - { - var jumper = (Jumper)instruction; - if (jumper.Offset == 0) continue; + SwitchExits.Add(lookUpSwitchIns, exits); + } + else if (Jumper.IsValid(instruction.OP)) + { + var jumper = (Jumper)instruction; + if (jumper.Offset == 0) continue; - long exitPosition = (input.Position + jumper.Offset); - if (exitPosition == input.Length) - { - // Jump exit does not exist at this (non-existent)index, do not look for exit. - continue; - } - else if (exitPosition < input.Length) // Forward jump. + long exitPosition = (input.Position + jumper.Offset); + if (exitPosition == input.Length) + { + // Jump exit does not exist at this (non-existent)index, do not look for exit. + continue; + } + else if (exitPosition < input.Length) // Forward jump. + { + if (!sharedExits.TryGetValue(exitPosition, out jumpers)) { - if (!sharedExits.TryGetValue(exitPosition, out jumpers)) - { - jumpers = new List(); - sharedExits.Add(exitPosition, jumpers); - } - jumpers.Add(jumper); + jumpers = new List(); + sharedExits.Add(exitPosition, jumpers); } - else // Backwards jump. + jumpers.Add(jumper); + } + else // Backwards jump. + { + long markIndex = exitPosition - uint.MaxValue - 1; + if (marks[markIndex].OP == OPCode.Label) { - long markIndex = exitPosition - uint.MaxValue - 1; - if (marks[markIndex].OP == OPCode.Label) - { - var label = (LabelIns)marks[markIndex]; - JumpExits.Add(jumper, label); - } - // TODO: Check if not adding an impossible label is fine... + var label = (LabelIns)marks[markIndex]; + JumpExits.Add(jumper, label); } + // TODO: Check if not adding an impossible label is fine... } } } diff --git a/Flazzy/ABC/AVM2/Instructions/ASInstruction.cs b/Flazzy/ABC/AVM2/Instructions/ASInstruction.cs index 855f0a4..5afe2f1 100644 --- a/Flazzy/ABC/AVM2/Instructions/ASInstruction.cs +++ b/Flazzy/ABC/AVM2/Instructions/ASInstruction.cs @@ -1,4 +1,6 @@ -using Flazzy.IO; +using System.Runtime.CompilerServices; + +using Flazzy.IO; namespace Flazzy.ABC.AVM2.Instructions; @@ -6,7 +8,6 @@ public abstract class ASInstruction : FlashItem, ICloneable { public OPCode OP { get; } protected ABCFile ABC { get; } - protected override string DebuggerDisplay => OP.ToString(); public ASInstruction(OPCode op) { @@ -18,26 +19,35 @@ protected ASInstruction(OPCode op, ABCFile abc) ABC = abc; } - public override void WriteTo(FlashWriter output) - { - output.Write((byte)OP); - WriteValuesTo(output); - } - protected virtual void WriteValuesTo(FlashWriter output) + public virtual int GetPopCount() => 0; + public virtual int GetPushCount() => 0; + public virtual void Execute(ASMachine machine) { } - public virtual int GetPopCount() + protected virtual int GetBodySize() => 0; + protected virtual void WriteValuesTo(ref SpanFlashWriter output) + { } + + public int GetSize() => sizeof(OPCode) + GetBodySize(); + public void WriteTo(ref SpanFlashWriter output) { - return 0; + output.Write((byte)OP); + WriteValuesTo(ref output); } - public virtual int GetPushCount() + + [SkipLocalsInit] + // Temporary hack stackalloc until rest of the IO is "spanified". + public override void WriteTo(FlashWriter output) { - return 0; + Span buffer = stackalloc byte[64].Slice(0, GetSize()); + + var outputWriter = new SpanFlashWriter(buffer); + WriteTo(ref outputWriter); + + output.Write(buffer); } - public virtual void Execute(ASMachine machine) - { } - protected int ResolveMultinamePops(ASMultiname multiname) + protected static int ResolveMultinamePops(ASMultiname multiname) { int popCount = 0; if (multiname.IsNameNeeded) @@ -50,7 +60,7 @@ protected int ResolveMultinamePops(ASMultiname multiname) } return popCount; } - protected void ResolveMultiname(ASMachine machine, ASMultiname multiname) + protected static void ResolveMultiname(ASMachine machine, ASMultiname multiname) { if (multiname.IsNameNeeded) { @@ -62,465 +72,186 @@ protected void ResolveMultiname(ASMachine machine, ASMultiname multiname) } } - // TODO: Use source generator to add new items at compile time. - public static bool IsPropertyContainer(OPCode op) + public static bool IsPropertyContainer(OPCode op) => op is OPCode.CallPropVoid or OPCode.CallProperty or OPCode.ConstructProp; + public static ASInstruction Create(ABCFile abc, ref SpanFlashReader input) { - switch (op) + OPCode op = (OPCode)input.ReadByte(); + return op switch { - case OPCode.CallPropVoid: - case OPCode.CallProperty: - case OPCode.ConstructProp: return true; - default: return false; - } + // Arithmetic + OPCode.Add_i => new AddIIns(), + OPCode.Add => new AddIns(), + OPCode.Decrement_i => new DecrementIIns(), + OPCode.Decrement => new DecrementIns(), + OPCode.Divide => new DivideIns(), + OPCode.Equals => new EqualsIns(), + OPCode.GreaterEquals => new GreaterEqualsIns(), + OPCode.GreaterThan => new GreaterThanIns(), + OPCode.Increment_i => new IncrementIIns(), + OPCode.Increment => new IncrementIns(), + OPCode.In => new InIns(), + OPCode.IsTypeLate => new IsTypeLateIns(), + OPCode.LessEquals => new LessEqualsIns(), + OPCode.LessThan => new LessThanIns(), + OPCode.Modulo => new ModuloIns(), + OPCode.Multiply_i => new MultiplyIIns(), + OPCode.Multiply => new MultiplyIns(), + OPCode.Negate_i => new NegateIIns(), + OPCode.Negate => new NegateIns(), + OPCode.StrictEquals => new StrictEqualsIns(), + OPCode.Subtract_i => new SubtractIIns(), + OPCode.Subtract => new SubtractIns(), + + // Bit manipulation + OPCode.BitAnd => new BitAndIns(), + OPCode.BitNot => new BitNotIns(), + OPCode.BitOr => new BitOrIns(), + OPCode.BitXor => new BitXorIns(), + OPCode.LShift => new LShiftIns(), + OPCode.RShift => new RShiftIns(), + OPCode.URShift => new URShiftIns(), + + // Control transfer + OPCode.IfEq => new IfEqualIns(ref input), + OPCode.IfGe => new IfGreaterEqualIns(ref input), + OPCode.IfGt => new IfGreaterThanIns(ref input), + OPCode.IfLe => new IfLessEqualIns(ref input), + OPCode.IfLt => new IfLessThanIns(ref input), + OPCode.IfNe => new IfNotEqualIns(ref input), + OPCode.IfNGe => new IfNotGreaterEqualIns(ref input), + OPCode.IfNGt => new IfNotGreaterThanIns(ref input), + OPCode.IfNLe => new IfNotLessEqualIns(ref input), + OPCode.IfNLt => new IfNotLessThanIns(ref input), + OPCode.IfStrictEq => new IfStrictEqualIns(ref input), + OPCode.IfStrictNE => new IfStrictNotEqualIns(ref input), + OPCode.IfTrue => new IfTrueIns(ref input), + OPCode.IfFalse => new IfFalseIns(ref input), + OPCode.Jump => new JumpIns(ref input), + + OPCode.Label => new LabelIns(), + OPCode.LookUpSwitch => new LookUpSwitchIns(ref input), + + // Register management + OPCode.DecLocal_i => new DecLocalIIns(ref input), + OPCode.DecLocal => new DecLocalIns(ref input), + OPCode.GetLocal_0 => new GetLocal0Ins(), + OPCode.GetLocal_1 => new GetLocal1Ins(), + OPCode.GetLocal_2 => new GetLocal2Ins(), + OPCode.GetLocal_3 => new GetLocal3Ins(), + OPCode.GetLocal => new GetLocalIns(ref input), + OPCode.IncLocal_i => new IncLocalIIns(ref input), + OPCode.IncLocal => new IncLocalIns(ref input), + OPCode.Kill => new KillIns(ref input), + OPCode.SetLocal_0 => new SetLocal0Ins(), + OPCode.SetLocal_1 => new SetLocal1Ins(), + OPCode.SetLocal_2 => new SetLocal2Ins(), + OPCode.SetLocal_3 => new SetLocal3Ins(), + OPCode.SetLocal => new SetLocalIns(ref input), + + // Stack management + OPCode.PushNull => new PushNullIns(), + OPCode.PushUndefined => new PushUndefinedIns(), + + OPCode.PushByte => new PushByteIns(ref input), + OPCode.PushShort => new PushShortIns(ref input), + OPCode.PushTrue => new PushTrueIns(), + OPCode.PushFalse => new PushFalseIns(), + OPCode.PushString => new PushStringIns(abc, ref input), + OPCode.PushNan => new PushNaNIns(), + OPCode.PushInt => new PushIntIns(abc, ref input), + OPCode.PushUInt => new PushUIntIns(abc, ref input), + OPCode.PushDouble => new PushDoubleIns(abc, ref input), + + OPCode.Dup => new DupIns(), + OPCode.Swap => new SwapIns(), + OPCode.Not => new NotIns(), + OPCode.Pop => new PopIns(), + OPCode.PopScope => new PopScopeIns(), + OPCode.PushWith => new PushWithIns(), + OPCode.PushScope => new PushScopeIns(), + + // Type conversion + OPCode.Coerce_a => new CoerceAIns(), + OPCode.Coerce => new CoerceIns(abc, ref input), + OPCode.Coerce_s => new CoerceSIns(), + OPCode.Convert_b => new ConvertBIns(), + OPCode.Convert_d => new ConvertDIns(), + OPCode.Convert_i => new ConvertIIns(), + OPCode.Convert_o => new ConvertOIns(), + OPCode.Convert_s => new ConvertSIns(), + OPCode.Convert_u => new ConvertUIns(), + + // Miscellaneous + OPCode.Nop => new NopIns(), + OPCode.ApplyType => new ApplyTypeIns(ref input), + OPCode.AsType => new AsTypeIns(abc, ref input), + OPCode.AsTypeLate => new AsTypeLateIns(), + + OPCode.Call => new CallIns(ref input), + OPCode.CallMethod => new CallMethodIns(abc, ref input), + OPCode.CallProperty => new CallPropertyIns(abc, ref input), + OPCode.CallPropLex => new CallPropLexIns(abc, ref input), + OPCode.CallPropVoid => new CallPropVoidIns(abc, ref input), + OPCode.CallStatic => new CallStaticIns(abc, ref input), + OPCode.CallSuper => new CallSuperIns(abc, ref input), + OPCode.CallSuperVoid => new CallSuperVoidIns(abc, ref input), + + OPCode.CheckFilter => new CheckFilterIns(), + + OPCode.Construct => new ConstructIns(ref input), + OPCode.ConstructProp => new ConstructPropIns(abc, ref input), + OPCode.ConstructSuper => new ConstructSuperIns(ref input), + + OPCode.DebugFile => new DebugFileIns(abc, ref input), + OPCode.Debug => new DebugIns(abc, ref input), + OPCode.DebugLine => new DebugLineIns(ref input), + + OPCode.DeleteProperty => new DeletePropertyIns(abc, ref input), + + OPCode.Dxns => new DxnsIns(abc, ref input), + OPCode.DxnsLate => new DxnsLateIns(), + + OPCode.Esc_XElem => new EscXElemIns(), + OPCode.Esc_XAttr => new EscXAttrIns(), + OPCode.FindProperty => new FindPropertyIns(abc, ref input), + OPCode.FindPropStrict => new FindPropStrictIns(abc, ref input), + OPCode.GetDescendants => new GetDescendantsIns(abc, ref input), + OPCode.GetGlobalScope => new GetGlobalScopeIns(), + OPCode.GetLex => new GetLexIns(abc, ref input), + OPCode.GetProperty => new GetPropertyIns(abc, ref input), + OPCode.GetScopeObject => new GetScopeObjectIns(ref input), + OPCode.GetSlot => new GetSlotIns(ref input), + OPCode.GetSuper => new GetSuperIns(abc, ref input), + + OPCode.HasNext => new HasNextIns(), + OPCode.HasNext2 => new HasNext2Ins(ref input), + + OPCode.InitProperty => new InitPropertyIns(abc, ref input), + OPCode.InstanceOf => new InstanceOfIns(), + + OPCode.NewActivation => new NewActivationIns(), + OPCode.NewArray => new NewArrayIns(ref input), + OPCode.NewCatch => new NewCatchIns(ref input), + OPCode.NewClass => new NewClassIns(abc, ref input), + OPCode.NewFunction => new NewFunctionIns(abc, ref input), + OPCode.NewObject => new NewObjectIns(ref input), + + OPCode.NextName => new NextNameIns(), + OPCode.NextValue => new NextValueIns(), + OPCode.ReturnValue => new ReturnValueIns(), + OPCode.ReturnVoid => new ReturnVoidIns(), + OPCode.SetProperty => new SetPropertyIns(abc, ref input), + OPCode.SetSlot => new SetSlotIns(ref input), + OPCode.SetSuper => new SetSuperIns(abc, ref input), + OPCode.Throw => new ThrowIns(), + OPCode.TypeOf => new TypeOfIns(), + + _ => throw new Exception("Unhandled OPCode: " + op) + }; } - public static ASInstruction Create(ABCFile abc, FlashReader input) - { - var op = (OPCode)input.ReadByte(); - switch (op) - { - #region Arithmetic - case OPCode.Add_i: - return new AddIIns(); - - case OPCode.Add: - return new AddIns(); - - case OPCode.Decrement_i: - return new DecrementIIns(); - - case OPCode.Decrement: - return new DecrementIns(); - - case OPCode.Divide: - return new DivideIns(); - - case OPCode.Equals: - return new EqualsIns(); - - case OPCode.GreaterEquals: - return new GreaterEqualsIns(); - - case OPCode.GreaterThan: - return new GreaterThanIns(); - - case OPCode.Increment_i: - return new IncrementIIns(); - - case OPCode.Increment: - return new IncrementIns(); - - case OPCode.In: - return new InIns(); - - case OPCode.IsTypeLate: - return new IsTypeLateIns(); - - case OPCode.LessEquals: - return new LessEqualsIns(); - - case OPCode.LessThan: - return new LessThanIns(); - - case OPCode.Modulo: - return new ModuloIns(); - - case OPCode.Multiply_i: - return new MultiplyIIns(); - - case OPCode.Multiply: - return new MultiplyIns(); - - case OPCode.Negate_i: - return new NegateIIns(); - - case OPCode.Negate: - return new NegateIns(); - - case OPCode.StrictEquals: - return new StrictEqualsIns(); - - case OPCode.Subtract_i: - return new SubtractIIns(); - - case OPCode.Subtract: - return new SubtractIns(); - #endregion - - #region Bit Manipulation - case OPCode.BitAnd: - return new BitAndIns(); - - case OPCode.BitNot: - return new BitNotIns(); - - case OPCode.BitOr: - return new BitOrIns(); - - case OPCode.BitXor: - return new BitXorIns(); - - case OPCode.LShift: - return new LShiftIns(); - - case OPCode.RShift: - return new RShiftIns(); - - case OPCode.URShift: - return new URShiftIns(); - #endregion - - #region Control Transfer - case OPCode.IfEq: - return new IfEqualIns(input); - - case OPCode.IfFalse: - return new IfFalseIns(input); - - case OPCode.IfGe: - return new IfGreaterEqualIns(input); - - case OPCode.IfGt: - return new IfGreaterThanIns(input); - - case OPCode.IfLe: - return new IfLessEqualIns(input); - - case OPCode.IfLt: - return new IfLessThanIns(input); - - case OPCode.IfNe: - return new IfNotEqualIns(input); - - case OPCode.IfNGe: - return new IfNotGreaterEqualIns(input); - - case OPCode.IfNGt: - return new IfNotGreaterThanIns(input); - - case OPCode.IfNLe: - return new IfNotLessEqualIns(input); - - case OPCode.IfNLt: - return new IfNotLessThanIns(input); - - case OPCode.IfStrictEq: - return new IfStrictEqualIns(input); - - case OPCode.IfStrictNE: - return new IfStrictNotEqualIns(input); - - case OPCode.IfTrue: - return new IfTrueIns(input); - - case OPCode.Jump: - return new JumpIns(input); - #endregion - - #region Register Management - case OPCode.DecLocal_i: - return new DecLocalIIns(input); - - case OPCode.DecLocal: - return new DecLocalIns(input); - - case OPCode.GetLocal_0: - return new GetLocal0Ins(); - - case OPCode.GetLocal_1: - return new GetLocal1Ins(); - - case OPCode.GetLocal_2: - return new GetLocal2Ins(); - - case OPCode.GetLocal_3: - return new GetLocal3Ins(); - - case OPCode.GetLocal: - return new GetLocalIns(input); - - case OPCode.IncLocal_i: - return new IncLocalIIns(input); - - case OPCode.IncLocal: - return new IncLocalIns(input); - - case OPCode.Kill: - return new KillIns(input); - - case OPCode.SetLocal_0: - return new SetLocal0Ins(); - - case OPCode.SetLocal_1: - return new SetLocal1Ins(); - - case OPCode.SetLocal_2: - return new SetLocal2Ins(); - - case OPCode.SetLocal_3: - return new SetLocal3Ins(); - - case OPCode.SetLocal: - return new SetLocalIns(input); - #endregion - - #region Stack Management - case OPCode.PushByte: - return new PushByteIns(input); - - case OPCode.PushDouble: - return new PushDoubleIns(abc, input); - - case OPCode.PushFalse: - return new PushFalseIns(); - - case OPCode.PushInt: - return new PushIntIns(abc, input); - - case OPCode.PushNan: - return new PushNaNIns(); - case OPCode.PushNull: - return new PushNullIns(); + object ICloneable.Clone() => Clone(); + public ASInstruction Clone() => (ASInstruction)MemberwiseClone(); - case OPCode.PushShort: - return new PushShortIns(input); - - case OPCode.PushString: - return new PushStringIns(abc, input); - - case OPCode.PushTrue: - return new PushTrueIns(); - - case OPCode.PushUInt: - return new PushUIntIns(abc, input); - #endregion - - #region Type Conversion - case OPCode.Coerce_a: - return new CoerceAIns(); - - case OPCode.Coerce: - return new CoerceIns(abc, input); - - case OPCode.Coerce_s: - return new CoerceSIns(); - - case OPCode.Convert_b: - return new ConvertBIns(); - - case OPCode.Convert_d: - return new ConvertDIns(); - - case OPCode.Convert_i: - return new ConvertIIns(); - - case OPCode.Convert_o: - return new ConvertOIns(); - - case OPCode.Convert_s: - return new ConvertSIns(); - - case OPCode.Convert_u: - return new ConvertUIns(); - #endregion - - #region Miscellaneous - case OPCode.ApplyType: - return new ApplyTypeIns(input); - - case OPCode.AsType: - return new AsTypeIns(abc, input); - - case OPCode.AsTypeLate: - return new AsTypeLateIns(); - - case OPCode.Call: - return new CallIns(input); - - case OPCode.CallMethod: - return new CallMethodIns(abc, input); - - case OPCode.CallProperty: - return new CallPropertyIns(abc, input); - - case OPCode.CallPropLex: - return new CallPropLexIns(abc, input); - - case OPCode.CallPropVoid: - return new CallPropVoidIns(abc, input); - - case OPCode.CallStatic: - return new CallStaticIns(abc, input); - - case OPCode.CallSuper: - return new CallSuperIns(abc, input); - - case OPCode.CallSuperVoid: - return new CallSuperVoidIns(abc, input); - - case OPCode.CheckFilter: - return new CheckFilterIns(); - - case OPCode.Construct: - return new ConstructIns(input); - - case OPCode.ConstructProp: - return new ConstructPropIns(abc, input); - - case OPCode.ConstructSuper: - return new ConstructSuperIns(input); - - case OPCode.DebugFile: - return new DebugFileIns(abc, input); - - case OPCode.Debug: - return new DebugIns(abc, input); - - case OPCode.DebugLine: - return new DebugLineIns(input); - - case OPCode.DeleteProperty: - return new DeletePropertyIns(abc, input); - - case OPCode.Dup: - return new DupIns(); - - case OPCode.Dxns: - return new DxnsIns(abc, input); - - case OPCode.DxnsLate: - return new DxnsLateIns(); - - case OPCode.Esc_XElem: - return new EscXElemIns(); - - case OPCode.Esc_XAttr: - return new EscXAttrIns(); - - case OPCode.FindProperty: - return new FindPropertyIns(abc, input); - - case OPCode.FindPropStrict: - return new FindPropStrictIns(abc, input); - - case OPCode.GetDescendants: - return new GetDescendantsIns(abc, input); - - case OPCode.GetGlobalScope: - return new GetGlobalScopeIns(); - - case OPCode.GetLex: - return new GetLexIns(abc, input); - - case OPCode.GetProperty: - return new GetPropertyIns(abc, input); - - case OPCode.GetScopeObject: - return new GetScopeObjectIns(input); - - case OPCode.GetSlot: - return new GetSlotIns(input); - - case OPCode.GetSuper: - return new GetSuperIns(abc, input); - - case OPCode.HasNext2: - return new HasNext2Ins(input); - - case OPCode.HasNext: - return new HasNextIns(); - - case OPCode.InitProperty: - return new InitPropertyIns(abc, input); - - case OPCode.InstanceOf: - return new InstanceOfIns(); - - case OPCode.Label: - return new LabelIns(); - - case OPCode.LookUpSwitch: - return new LookUpSwitchIns(input); - - case OPCode.NewActivation: - return new NewActivationIns(); - - case OPCode.NewArray: - return new NewArrayIns(input); - - case OPCode.NewCatch: - return new NewCatchIns(input); - - case OPCode.NewClass: - return new NewClassIns(abc, input); - - case OPCode.NewFunction: - return new NewFunctionIns(abc, input); - - case OPCode.NewObject: - return new NewObjectIns(input); - - case OPCode.NextName: - return new NextNameIns(); - - case OPCode.NextValue: - return new NextValueIns(); - - case OPCode.Nop: - return new NopIns(); - - case OPCode.Not: - return new NotIns(); - - case OPCode.Pop: - return new PopIns(); - - case OPCode.PopScope: - return new PopScopeIns(); - - case OPCode.PushScope: - return new PushScopeIns(); - - case OPCode.PushUndefined: - return new PushUndefinedIns(); - - case OPCode.PushWith: - return new PushWithIns(); - - case OPCode.ReturnValue: - return new ReturnValueIns(); - - case OPCode.ReturnVoid: - return new ReturnVoidIns(); - - case OPCode.SetProperty: - return new SetPropertyIns(abc, input); - - case OPCode.SetSlot: - return new SetSlotIns(input); - - case OPCode.SetSuper: - return new SetSuperIns(abc, input); - - case OPCode.Swap: - return new SwapIns(); - - case OPCode.Throw: - return new ThrowIns(); - - case OPCode.TypeOf: - return new TypeOfIns(); - #endregion - } - throw new Exception("Unhandled OPCode: " + op); - } - - object ICloneable.Clone() - { - return Clone(); - } - public ASInstruction Clone() - { - return (ASInstruction)MemberwiseClone(); - } + public override string ToString() => OP.ToString(); } \ No newline at end of file diff --git a/Flazzy/ABC/AVM2/Instructions/ApplyTypeIns.cs b/Flazzy/ABC/AVM2/Instructions/ApplyTypeIns.cs index 225e855..69736ae 100644 --- a/Flazzy/ABC/AVM2/Instructions/ApplyTypeIns.cs +++ b/Flazzy/ABC/AVM2/Instructions/ApplyTypeIns.cs @@ -2,7 +2,7 @@ namespace Flazzy.ABC.AVM2.Instructions; -public class ApplyTypeIns : ASInstruction +public sealed class ApplyTypeIns : ASInstruction { public int ParamCount { get; set; } @@ -14,14 +14,18 @@ public ApplyTypeIns(int paramCount) { ParamCount = paramCount; } - public ApplyTypeIns(FlashReader input) + public ApplyTypeIns(ref SpanFlashReader input) : this() { - ParamCount = input.ReadInt30(); + ParamCount = input.ReadEncodedInt(); } - protected override void WriteValuesTo(FlashWriter output) + protected override int GetBodySize() { - output.WriteInt30(ParamCount); + return SpanFlashWriter.GetEncodedIntSize(ParamCount); + } + protected override void WriteValuesTo(ref SpanFlashWriter output) + { + output.WriteEncodedInt(ParamCount); } } \ No newline at end of file diff --git a/Flazzy/ABC/AVM2/Instructions/Arithmetic/AddIIns.cs b/Flazzy/ABC/AVM2/Instructions/Arithmetic/AddIIns.cs index 8f6c621..449d7cf 100644 --- a/Flazzy/ABC/AVM2/Instructions/Arithmetic/AddIIns.cs +++ b/Flazzy/ABC/AVM2/Instructions/Arithmetic/AddIIns.cs @@ -1,13 +1,10 @@ namespace Flazzy.ABC.AVM2.Instructions; -public class AddIIns : Computation +public sealed class AddIIns : Computation { public AddIIns() : base(OPCode.Add_i) { } - protected override object Execute(dynamic left, dynamic right) - { - return (left + right); - } + protected override object Execute(dynamic left, dynamic right) => left + right; } \ No newline at end of file diff --git a/Flazzy/ABC/AVM2/Instructions/Arithmetic/AddIns.cs b/Flazzy/ABC/AVM2/Instructions/Arithmetic/AddIns.cs index 6e581de..2a49240 100644 --- a/Flazzy/ABC/AVM2/Instructions/Arithmetic/AddIns.cs +++ b/Flazzy/ABC/AVM2/Instructions/Arithmetic/AddIns.cs @@ -1,13 +1,10 @@ namespace Flazzy.ABC.AVM2.Instructions; -public class AddIns : Computation +public sealed class AddIns : Computation { public AddIns() : base(OPCode.Add) { } - protected override object Execute(dynamic left, dynamic right) - { - return (left + right); - } + protected override object Execute(dynamic left, dynamic right) => left + right; } \ No newline at end of file diff --git a/Flazzy/ABC/AVM2/Instructions/Arithmetic/Computation.cs b/Flazzy/ABC/AVM2/Instructions/Arithmetic/Computation.cs index 4d3ecfe..3f3124e 100644 --- a/Flazzy/ABC/AVM2/Instructions/Arithmetic/Computation.cs +++ b/Flazzy/ABC/AVM2/Instructions/Arithmetic/Computation.cs @@ -6,14 +6,8 @@ public Computation(OPCode op) : base(op) { } - public override int GetPopCount() - { - return 2; - } - public override int GetPushCount() - { - return 1; - } + public override int GetPopCount() => 2; + public override int GetPushCount() => 1; public override void Execute(ASMachine machine) { object right = machine.Values.Pop(); @@ -28,28 +22,22 @@ public override void Execute(ASMachine machine) } protected abstract object Execute(object left, object right); - public static bool IsValid(OPCode op) + public static bool IsValid(OPCode op) => op switch { - switch (op) - { - case OPCode.Add_i: - case OPCode.Add: - case OPCode.Divide: - case OPCode.Equals: - case OPCode.GreaterEquals: - case OPCode.GreaterThan: - case OPCode.LessEquals: - case OPCode.LessThan: - case OPCode.Modulo: - case OPCode.Multiply_i: - case OPCode.Multiply: - case OPCode.StrictEquals: - case OPCode.Subtract_i: - case OPCode.Subtract: - return true; - - default: - return false; - } - } + OPCode.Add_i or + OPCode.Add or + OPCode.Divide or + OPCode.Equals or + OPCode.GreaterEquals or + OPCode.GreaterThan or + OPCode.LessEquals or + OPCode.LessThan or + OPCode.Modulo or + OPCode.Multiply_i or + OPCode.Multiply or + OPCode.StrictEquals or + OPCode.Subtract_i or + OPCode.Subtract => true, + _ => false, + }; } \ No newline at end of file diff --git a/Flazzy/ABC/AVM2/Instructions/Arithmetic/DecrementIIns.cs b/Flazzy/ABC/AVM2/Instructions/Arithmetic/DecrementIIns.cs index c23c3e8..a456ccb 100644 --- a/Flazzy/ABC/AVM2/Instructions/Arithmetic/DecrementIIns.cs +++ b/Flazzy/ABC/AVM2/Instructions/Arithmetic/DecrementIIns.cs @@ -1,26 +1,20 @@ namespace Flazzy.ABC.AVM2.Instructions; -public class DecrementIIns : ASInstruction +public sealed class DecrementIIns : ASInstruction { public DecrementIIns() : base(OPCode.Decrement_i) { } - public override int GetPopCount() - { - return 1; - } - public override int GetPushCount() - { - return 1; - } + public override int GetPopCount() => 1; + public override int GetPushCount() => 1; public override void Execute(ASMachine machine) { object result = null; object value = machine.Values.Pop(); if (value != null) { - result = (Convert.ToInt32(value) - 1); + result = Convert.ToInt32(value) - 1; } machine.Values.Push(result); } diff --git a/Flazzy/ABC/AVM2/Instructions/Arithmetic/DecrementIns.cs b/Flazzy/ABC/AVM2/Instructions/Arithmetic/DecrementIns.cs index 34b9999..244cd26 100644 --- a/Flazzy/ABC/AVM2/Instructions/Arithmetic/DecrementIns.cs +++ b/Flazzy/ABC/AVM2/Instructions/Arithmetic/DecrementIns.cs @@ -1,26 +1,20 @@ namespace Flazzy.ABC.AVM2.Instructions; -public class DecrementIns : ASInstruction +public sealed class DecrementIns : ASInstruction { public DecrementIns() : base(OPCode.Decrement) { } - public override int GetPopCount() - { - return 1; - } - public override int GetPushCount() - { - return 1; - } + public override int GetPopCount() => 1; + public override int GetPushCount() => 1; public override void Execute(ASMachine machine) { object result = null; object value = machine.Values.Pop(); if (value != null) { - result = (Convert.ToDouble(value) - 1); + result = Convert.ToDouble(value) - 1; } machine.Values.Push(result); } diff --git a/Flazzy/ABC/AVM2/Instructions/Arithmetic/DivideIns.cs b/Flazzy/ABC/AVM2/Instructions/Arithmetic/DivideIns.cs index b709608..1c9ffa0 100644 --- a/Flazzy/ABC/AVM2/Instructions/Arithmetic/DivideIns.cs +++ b/Flazzy/ABC/AVM2/Instructions/Arithmetic/DivideIns.cs @@ -1,13 +1,10 @@ namespace Flazzy.ABC.AVM2.Instructions; -public class DivideIns : Computation +public sealed class DivideIns : Computation { public DivideIns() : base(OPCode.Divide) { } - protected override object Execute(dynamic left, dynamic right) - { - return (left / right); - } + protected override object Execute(dynamic left, dynamic right) => left / right; } \ No newline at end of file diff --git a/Flazzy/ABC/AVM2/Instructions/Arithmetic/EqualsIns.cs b/Flazzy/ABC/AVM2/Instructions/Arithmetic/EqualsIns.cs index 1ff5a97..41c9b14 100644 --- a/Flazzy/ABC/AVM2/Instructions/Arithmetic/EqualsIns.cs +++ b/Flazzy/ABC/AVM2/Instructions/Arithmetic/EqualsIns.cs @@ -1,13 +1,10 @@ namespace Flazzy.ABC.AVM2.Instructions; -public class EqualsIns : Computation +public sealed class EqualsIns : Computation { public EqualsIns() : base(OPCode.Equals) { } - protected override object Execute(dynamic left, dynamic right) - { - return (left == right); - } + protected override object Execute(dynamic left, dynamic right) => left == right; } \ No newline at end of file diff --git a/Flazzy/ABC/AVM2/Instructions/Arithmetic/GreaterEqualsIns.cs b/Flazzy/ABC/AVM2/Instructions/Arithmetic/GreaterEqualsIns.cs index 80e451c..1a4a5df 100644 --- a/Flazzy/ABC/AVM2/Instructions/Arithmetic/GreaterEqualsIns.cs +++ b/Flazzy/ABC/AVM2/Instructions/Arithmetic/GreaterEqualsIns.cs @@ -1,13 +1,10 @@ namespace Flazzy.ABC.AVM2.Instructions; -public class GreaterEqualsIns : Computation +public sealed class GreaterEqualsIns : Computation { public GreaterEqualsIns() : base(OPCode.GreaterEquals) { } - protected override object Execute(dynamic left, dynamic right) - { - return (left >= right); - } + protected override object Execute(dynamic left, dynamic right) => left >= right; } \ No newline at end of file diff --git a/Flazzy/ABC/AVM2/Instructions/Arithmetic/GreaterThanIns.cs b/Flazzy/ABC/AVM2/Instructions/Arithmetic/GreaterThanIns.cs index 3851c50..3f4b1b7 100644 --- a/Flazzy/ABC/AVM2/Instructions/Arithmetic/GreaterThanIns.cs +++ b/Flazzy/ABC/AVM2/Instructions/Arithmetic/GreaterThanIns.cs @@ -1,13 +1,10 @@ namespace Flazzy.ABC.AVM2.Instructions; -public class GreaterThanIns : Computation +public sealed class GreaterThanIns : Computation { public GreaterThanIns() : base(OPCode.GreaterThan) { } - protected override object Execute(dynamic left, dynamic right) - { - return (left > right); - } + protected override object Execute(dynamic left, dynamic right) => left > right; } \ No newline at end of file diff --git a/Flazzy/ABC/AVM2/Instructions/Arithmetic/InIns.cs b/Flazzy/ABC/AVM2/Instructions/Arithmetic/InIns.cs index 54a5757..4521c28 100644 --- a/Flazzy/ABC/AVM2/Instructions/Arithmetic/InIns.cs +++ b/Flazzy/ABC/AVM2/Instructions/Arithmetic/InIns.cs @@ -1,19 +1,13 @@ namespace Flazzy.ABC.AVM2.Instructions; -public class InIns : ASInstruction +public sealed class InIns : ASInstruction { public InIns() : base(OPCode.In) { } - public override int GetPopCount() - { - return 2; - } - public override int GetPushCount() - { - return 1; - } + public override int GetPopCount() => 2; + public override int GetPushCount() => 1; public override void Execute(ASMachine machine) { object obj = machine.Values.Pop(); diff --git a/Flazzy/ABC/AVM2/Instructions/Arithmetic/IncrementIIns.cs b/Flazzy/ABC/AVM2/Instructions/Arithmetic/IncrementIIns.cs index 56065b4..848d895 100644 --- a/Flazzy/ABC/AVM2/Instructions/Arithmetic/IncrementIIns.cs +++ b/Flazzy/ABC/AVM2/Instructions/Arithmetic/IncrementIIns.cs @@ -1,26 +1,20 @@ namespace Flazzy.ABC.AVM2.Instructions; -public class IncrementIIns : ASInstruction +public sealed class IncrementIIns : ASInstruction { public IncrementIIns() : base(OPCode.Increment_i) { } - public override int GetPopCount() - { - return 1; - } - public override int GetPushCount() - { - return 1; - } + public override int GetPopCount() => 1; + public override int GetPushCount() => 1; public override void Execute(ASMachine machine) { object result = null; object value = machine.Values.Pop(); if (value != null) { - result = (Convert.ToInt32(value) + 1); + result = Convert.ToInt32(value) + 1; } machine.Values.Push(result); } diff --git a/Flazzy/ABC/AVM2/Instructions/Arithmetic/IncrementIns.cs b/Flazzy/ABC/AVM2/Instructions/Arithmetic/IncrementIns.cs index c304e2c..e3a3897 100644 --- a/Flazzy/ABC/AVM2/Instructions/Arithmetic/IncrementIns.cs +++ b/Flazzy/ABC/AVM2/Instructions/Arithmetic/IncrementIns.cs @@ -1,26 +1,20 @@ namespace Flazzy.ABC.AVM2.Instructions; -public class IncrementIns : ASInstruction +public sealed class IncrementIns : ASInstruction { public IncrementIns() : base(OPCode.Increment) { } - public override int GetPopCount() - { - return 1; - } - public override int GetPushCount() - { - return 1; - } + public override int GetPopCount() => 1; + public override int GetPushCount() => 1; public override void Execute(ASMachine machine) { object result = null; object value = machine.Values.Pop(); if (value != null) { - result = (Convert.ToDouble(value) + 1); + result = Convert.ToDouble(value) + 1; } machine.Values.Push(result); } diff --git a/Flazzy/ABC/AVM2/Instructions/Arithmetic/IsTypeLateIns.cs b/Flazzy/ABC/AVM2/Instructions/Arithmetic/IsTypeLateIns.cs index 3c8b63c..3778a8b 100644 --- a/Flazzy/ABC/AVM2/Instructions/Arithmetic/IsTypeLateIns.cs +++ b/Flazzy/ABC/AVM2/Instructions/Arithmetic/IsTypeLateIns.cs @@ -1,19 +1,13 @@ namespace Flazzy.ABC.AVM2.Instructions; -public class IsTypeLateIns : ASInstruction +public sealed class IsTypeLateIns : ASInstruction { public IsTypeLateIns() : base(OPCode.IsTypeLate) { } - public override int GetPopCount() - { - return 2; - } - public override int GetPushCount() - { - return 1; - } + public override int GetPopCount() => 2; + public override int GetPushCount() => 1; public override void Execute(ASMachine machine) { object type = machine.Values.Pop(); diff --git a/Flazzy/ABC/AVM2/Instructions/Arithmetic/LessEqualsIns.cs b/Flazzy/ABC/AVM2/Instructions/Arithmetic/LessEqualsIns.cs index 6838ad7..58d9b23 100644 --- a/Flazzy/ABC/AVM2/Instructions/Arithmetic/LessEqualsIns.cs +++ b/Flazzy/ABC/AVM2/Instructions/Arithmetic/LessEqualsIns.cs @@ -1,13 +1,10 @@ namespace Flazzy.ABC.AVM2.Instructions; -public class LessEqualsIns : Computation +public sealed class LessEqualsIns : Computation { public LessEqualsIns() : base(OPCode.LessEquals) { } - protected override object Execute(dynamic left, dynamic right) - { - return (left <= right); - } + protected override object Execute(dynamic left, dynamic right) => left <= right; } \ No newline at end of file diff --git a/Flazzy/ABC/AVM2/Instructions/Arithmetic/LessThanIns.cs b/Flazzy/ABC/AVM2/Instructions/Arithmetic/LessThanIns.cs index 3e5e49f..f37d8d5 100644 --- a/Flazzy/ABC/AVM2/Instructions/Arithmetic/LessThanIns.cs +++ b/Flazzy/ABC/AVM2/Instructions/Arithmetic/LessThanIns.cs @@ -1,13 +1,10 @@ namespace Flazzy.ABC.AVM2.Instructions; -public class LessThanIns : Computation +public sealed class LessThanIns : Computation { public LessThanIns() : base(OPCode.LessThan) { } - protected override object Execute(dynamic left, dynamic right) - { - return (left < right); - } + protected override object Execute(dynamic left, dynamic right) => left < right; } \ No newline at end of file diff --git a/Flazzy/ABC/AVM2/Instructions/Arithmetic/ModuloIns.cs b/Flazzy/ABC/AVM2/Instructions/Arithmetic/ModuloIns.cs index 92b6ca8..f245301 100644 --- a/Flazzy/ABC/AVM2/Instructions/Arithmetic/ModuloIns.cs +++ b/Flazzy/ABC/AVM2/Instructions/Arithmetic/ModuloIns.cs @@ -1,13 +1,10 @@ namespace Flazzy.ABC.AVM2.Instructions; -public class ModuloIns : Computation +public sealed class ModuloIns : Computation { public ModuloIns() : base(OPCode.Modulo) { } - protected override object Execute(dynamic left, dynamic right) - { - return (left % right); - } + protected override object Execute(dynamic left, dynamic right) => left % right; } \ No newline at end of file diff --git a/Flazzy/ABC/AVM2/Instructions/Arithmetic/MultiplyIIns.cs b/Flazzy/ABC/AVM2/Instructions/Arithmetic/MultiplyIIns.cs index a980013..8ac3999 100644 --- a/Flazzy/ABC/AVM2/Instructions/Arithmetic/MultiplyIIns.cs +++ b/Flazzy/ABC/AVM2/Instructions/Arithmetic/MultiplyIIns.cs @@ -1,13 +1,10 @@ namespace Flazzy.ABC.AVM2.Instructions; -public class MultiplyIIns : Computation +public sealed class MultiplyIIns : Computation { public MultiplyIIns() : base(OPCode.Multiply_i) { } - protected override object Execute(dynamic left, dynamic right) - { - return (left * right); - } + protected override object Execute(dynamic left, dynamic right) => left * right; } \ No newline at end of file diff --git a/Flazzy/ABC/AVM2/Instructions/Arithmetic/MultiplyIns.cs b/Flazzy/ABC/AVM2/Instructions/Arithmetic/MultiplyIns.cs index 4e6f300..b933626 100644 --- a/Flazzy/ABC/AVM2/Instructions/Arithmetic/MultiplyIns.cs +++ b/Flazzy/ABC/AVM2/Instructions/Arithmetic/MultiplyIns.cs @@ -1,13 +1,10 @@ namespace Flazzy.ABC.AVM2.Instructions; -public class MultiplyIns : Computation +public sealed class MultiplyIns : Computation { public MultiplyIns() : base(OPCode.Multiply) { } - protected override object Execute(dynamic left, dynamic right) - { - return (left * right); - } + protected override object Execute(dynamic left, dynamic right) => left * right; } \ No newline at end of file diff --git a/Flazzy/ABC/AVM2/Instructions/Arithmetic/NegateIIns.cs b/Flazzy/ABC/AVM2/Instructions/Arithmetic/NegateIIns.cs index 0f19655..a2c3ece 100644 --- a/Flazzy/ABC/AVM2/Instructions/Arithmetic/NegateIIns.cs +++ b/Flazzy/ABC/AVM2/Instructions/Arithmetic/NegateIIns.cs @@ -1,26 +1,20 @@ namespace Flazzy.ABC.AVM2.Instructions; -public class NegateIIns : ASInstruction +public sealed class NegateIIns : ASInstruction { public NegateIIns() : base(OPCode.Negate_i) { } - public override int GetPopCount() - { - return 1; - } - public override int GetPushCount() - { - return 1; - } + public override int GetPopCount() => 1; + public override int GetPushCount() => 1; public override void Execute(ASMachine machine) { object result = null; object value = machine.Values.Pop(); if (value != null) { - result = (Convert.ToInt32(value) * -1); + result = Convert.ToInt32(value) * -1; } machine.Values.Push(result); } diff --git a/Flazzy/ABC/AVM2/Instructions/Arithmetic/NegateIns.cs b/Flazzy/ABC/AVM2/Instructions/Arithmetic/NegateIns.cs index 8e13331..46bcc8f 100644 --- a/Flazzy/ABC/AVM2/Instructions/Arithmetic/NegateIns.cs +++ b/Flazzy/ABC/AVM2/Instructions/Arithmetic/NegateIns.cs @@ -1,26 +1,20 @@ namespace Flazzy.ABC.AVM2.Instructions; -public class NegateIns : ASInstruction +public sealed class NegateIns : ASInstruction { public NegateIns() : base(OPCode.Negate) { } - public override int GetPopCount() - { - return 1; - } - public override int GetPushCount() - { - return 1; - } + public override int GetPopCount() => 1; + public override int GetPushCount() => 1; public override void Execute(ASMachine machine) { object result = null; object value = machine.Values.Pop(); if (value != null) { - result = (Convert.ToDouble(value) * -1); + result = Convert.ToDouble(value) * -1; } machine.Values.Push(result); } diff --git a/Flazzy/ABC/AVM2/Instructions/Arithmetic/StrictEqualsIns.cs b/Flazzy/ABC/AVM2/Instructions/Arithmetic/StrictEqualsIns.cs index eb56356..e0ed02c 100644 --- a/Flazzy/ABC/AVM2/Instructions/Arithmetic/StrictEqualsIns.cs +++ b/Flazzy/ABC/AVM2/Instructions/Arithmetic/StrictEqualsIns.cs @@ -1,6 +1,6 @@ namespace Flazzy.ABC.AVM2.Instructions; -public class StrictEqualsIns : Computation +public sealed class StrictEqualsIns : Computation { public StrictEqualsIns() : base(OPCode.StrictEquals) @@ -8,10 +8,9 @@ public StrictEqualsIns() protected override object Execute(object left, object right) { - var cLeft = (left as IComparable); - var cRight = (right as IComparable); - if (cLeft == null || cRight == null) return null; + if (left is not IComparable cLeft || + right is not IComparable cRight) return null; - return (cLeft.CompareTo(cRight) == 0); + return cLeft.CompareTo(cRight) == 0; } } \ No newline at end of file diff --git a/Flazzy/ABC/AVM2/Instructions/Arithmetic/SubtractIIns.cs b/Flazzy/ABC/AVM2/Instructions/Arithmetic/SubtractIIns.cs index 9bb59b8..86bfad9 100644 --- a/Flazzy/ABC/AVM2/Instructions/Arithmetic/SubtractIIns.cs +++ b/Flazzy/ABC/AVM2/Instructions/Arithmetic/SubtractIIns.cs @@ -1,13 +1,10 @@ namespace Flazzy.ABC.AVM2.Instructions; -public class SubtractIIns : Computation +public sealed class SubtractIIns : Computation { public SubtractIIns() : base(OPCode.Subtract_i) { } - protected override object Execute(dynamic left, dynamic right) - { - return (left - right); - } + protected override object Execute(dynamic left, dynamic right) => left - right; } \ No newline at end of file diff --git a/Flazzy/ABC/AVM2/Instructions/Arithmetic/SubtractIns.cs b/Flazzy/ABC/AVM2/Instructions/Arithmetic/SubtractIns.cs index 605d358..824328e 100644 --- a/Flazzy/ABC/AVM2/Instructions/Arithmetic/SubtractIns.cs +++ b/Flazzy/ABC/AVM2/Instructions/Arithmetic/SubtractIns.cs @@ -1,13 +1,10 @@ namespace Flazzy.ABC.AVM2.Instructions; -public class SubtractIns : Computation +public sealed class SubtractIns : Computation { public SubtractIns() : base(OPCode.Subtract) { } - protected override object Execute(dynamic left, dynamic right) - { - return (left - right); - } + protected override object Execute(dynamic left, dynamic right) => left - right; } \ No newline at end of file diff --git a/Flazzy/ABC/AVM2/Instructions/AsTypeIns.cs b/Flazzy/ABC/AVM2/Instructions/AsTypeIns.cs index 825057a..4a9b57f 100644 --- a/Flazzy/ABC/AVM2/Instructions/AsTypeIns.cs +++ b/Flazzy/ABC/AVM2/Instructions/AsTypeIns.cs @@ -2,7 +2,7 @@ namespace Flazzy.ABC.AVM2.Instructions; -public class AsTypeIns : ASInstruction +public sealed class AsTypeIns : ASInstruction { public int TypeNameIndex { get; set; } public ASMultiname TypeName => ABC.Pool.Multinames[TypeNameIndex]; @@ -15,28 +15,26 @@ public AsTypeIns(ABCFile abc, int typeNameIndex) { TypeNameIndex = typeNameIndex; } - public AsTypeIns(ABCFile abc, FlashReader input) + public AsTypeIns(ABCFile abc, ref SpanFlashReader input) : this(abc) { - TypeNameIndex = input.ReadInt30(); + TypeNameIndex = input.ReadEncodedInt(); } - public override int GetPopCount() - { - return 1; - } - public override int GetPushCount() - { - return 1; - } + public override int GetPopCount() => 1; + public override int GetPushCount() => 1; public override void Execute(ASMachine machine) { object value = machine.Values.Pop(); machine.Values.Push(null); } - protected override void WriteValuesTo(FlashWriter output) + protected override int GetBodySize() + { + return SpanFlashWriter.GetEncodedIntSize(TypeNameIndex); + } + protected override void WriteValuesTo(ref SpanFlashWriter output) { - output.WriteInt30(TypeNameIndex); + output.WriteEncodedInt(TypeNameIndex); } } \ No newline at end of file diff --git a/Flazzy/ABC/AVM2/Instructions/AsTypeLateIns.cs b/Flazzy/ABC/AVM2/Instructions/AsTypeLateIns.cs index 46bfc66..c543d06 100644 --- a/Flazzy/ABC/AVM2/Instructions/AsTypeLateIns.cs +++ b/Flazzy/ABC/AVM2/Instructions/AsTypeLateIns.cs @@ -1,19 +1,13 @@ namespace Flazzy.ABC.AVM2.Instructions; -public class AsTypeLateIns : ASInstruction +public sealed class AsTypeLateIns : ASInstruction { public AsTypeLateIns() : base(OPCode.AsTypeLate) { } - public override int GetPopCount() - { - return 2; - } - public override int GetPushCount() - { - return 1; - } + public override int GetPopCount() => 2; + public override int GetPushCount() => 1; public override void Execute(ASMachine machine) { object @class = machine.Values.Pop(); diff --git a/Flazzy/ABC/AVM2/Instructions/Bit Manipulation/BitAndIns.cs b/Flazzy/ABC/AVM2/Instructions/Bit Manipulation/BitAndIns.cs index f468374..fac4714 100644 --- a/Flazzy/ABC/AVM2/Instructions/Bit Manipulation/BitAndIns.cs +++ b/Flazzy/ABC/AVM2/Instructions/Bit Manipulation/BitAndIns.cs @@ -1,19 +1,13 @@ namespace Flazzy.ABC.AVM2.Instructions; -public class BitAndIns : ASInstruction +public sealed class BitAndIns : ASInstruction { public BitAndIns() : base(OPCode.BitAnd) { } - public override int GetPopCount() - { - return 2; - } - public override int GetPushCount() - { - return 1; - } + public override int GetPopCount() => 2; + public override int GetPushCount() => 1; public override void Execute(ASMachine machine) { object result = null; @@ -23,7 +17,7 @@ public override void Execute(ASMachine machine) { var iLeft = (int)Convert.ToDouble(left); var iRight = (int)Convert.ToDouble(right); - result = (iLeft & iRight); + result = iLeft & iRight; } machine.Values.Push(result); } diff --git a/Flazzy/ABC/AVM2/Instructions/Bit Manipulation/BitNotIns.cs b/Flazzy/ABC/AVM2/Instructions/Bit Manipulation/BitNotIns.cs index ba990b0..3c06550 100644 --- a/Flazzy/ABC/AVM2/Instructions/Bit Manipulation/BitNotIns.cs +++ b/Flazzy/ABC/AVM2/Instructions/Bit Manipulation/BitNotIns.cs @@ -1,26 +1,20 @@ namespace Flazzy.ABC.AVM2.Instructions; -public class BitNotIns : ASInstruction +public sealed class BitNotIns : ASInstruction { public BitNotIns() : base(OPCode.BitNot) { } - public override int GetPopCount() - { - return 1; - } - public override int GetPushCount() - { - return 1; - } + public override int GetPopCount() => 1; + public override int GetPushCount() => 1; public override void Execute(ASMachine machine) { object result = null; object value = machine.Values.Pop(); if (value != null) { - result = (~Convert.ToInt32(value)); + result = ~Convert.ToInt32(value); } machine.Values.Push(result); } diff --git a/Flazzy/ABC/AVM2/Instructions/Bit Manipulation/BitOrIns.cs b/Flazzy/ABC/AVM2/Instructions/Bit Manipulation/BitOrIns.cs index 5cc8e6f..bc52559 100644 --- a/Flazzy/ABC/AVM2/Instructions/Bit Manipulation/BitOrIns.cs +++ b/Flazzy/ABC/AVM2/Instructions/Bit Manipulation/BitOrIns.cs @@ -1,19 +1,13 @@ namespace Flazzy.ABC.AVM2.Instructions; -public class BitOrIns : ASInstruction +public sealed class BitOrIns : ASInstruction { public BitOrIns() : base(OPCode.BitOr) { } - public override int GetPopCount() - { - return 2; - } - public override int GetPushCount() - { - return 1; - } + public override int GetPopCount() => 2; + public override int GetPushCount() => 1; public override void Execute(ASMachine machine) { object result = null; @@ -23,7 +17,7 @@ public override void Execute(ASMachine machine) { int iLeft = Convert.ToInt32(left); int iRight = Convert.ToInt32(right); - result = (iLeft | iRight); + result = iLeft | iRight; } machine.Values.Push(result); } diff --git a/Flazzy/ABC/AVM2/Instructions/Bit Manipulation/BitXorIns.cs b/Flazzy/ABC/AVM2/Instructions/Bit Manipulation/BitXorIns.cs index 9ec567e..11dfa4a 100644 --- a/Flazzy/ABC/AVM2/Instructions/Bit Manipulation/BitXorIns.cs +++ b/Flazzy/ABC/AVM2/Instructions/Bit Manipulation/BitXorIns.cs @@ -1,19 +1,13 @@ namespace Flazzy.ABC.AVM2.Instructions; -public class BitXorIns : ASInstruction +public sealed class BitXorIns : ASInstruction { public BitXorIns() : base(OPCode.BitXor) { } - public override int GetPopCount() - { - return 2; - } - public override int GetPushCount() - { - return 1; - } + public override int GetPopCount() => 2; + public override int GetPushCount() => 1; public override void Execute(ASMachine machine) { object result = null; @@ -23,7 +17,7 @@ public override void Execute(ASMachine machine) { int iLeft = Convert.ToInt32(left); int iRight = Convert.ToInt32(right); - result = (iLeft ^ iRight); + result = iLeft ^ iRight; } machine.Values.Push(result); } diff --git a/Flazzy/ABC/AVM2/Instructions/Bit Manipulation/LShiftIns.cs b/Flazzy/ABC/AVM2/Instructions/Bit Manipulation/LShiftIns.cs index 262097e..154e171 100644 --- a/Flazzy/ABC/AVM2/Instructions/Bit Manipulation/LShiftIns.cs +++ b/Flazzy/ABC/AVM2/Instructions/Bit Manipulation/LShiftIns.cs @@ -1,19 +1,13 @@ namespace Flazzy.ABC.AVM2.Instructions; -public class LShiftIns : ASInstruction +public sealed class LShiftIns : ASInstruction { public LShiftIns() : base(OPCode.LShift) { } - public override int GetPopCount() - { - return 2; - } - public override int GetPushCount() - { - return 1; - } + public override int GetPopCount() => 2; + public override int GetPushCount() => 1; public override void Execute(ASMachine machine) { object result = null; @@ -22,8 +16,8 @@ public override void Execute(ASMachine machine) if (right != null && left != null) { int iLeft = Convert.ToInt32(left); - int iRight = (Convert.ToInt32(right) & 0x1F); - result = (iLeft << iRight); + int iRight = Convert.ToInt32(right) & 0x1F; + result = iLeft << iRight; } machine.Values.Push(result); } diff --git a/Flazzy/ABC/AVM2/Instructions/Bit Manipulation/RShiftIns.cs b/Flazzy/ABC/AVM2/Instructions/Bit Manipulation/RShiftIns.cs index 725a4fe..48118bb 100644 --- a/Flazzy/ABC/AVM2/Instructions/Bit Manipulation/RShiftIns.cs +++ b/Flazzy/ABC/AVM2/Instructions/Bit Manipulation/RShiftIns.cs @@ -1,19 +1,13 @@ namespace Flazzy.ABC.AVM2.Instructions; -public class RShiftIns : ASInstruction +public sealed class RShiftIns : ASInstruction { public RShiftIns() : base(OPCode.RShift) { } - public override int GetPopCount() - { - return 2; - } - public override int GetPushCount() - { - return 1; - } + public override int GetPopCount() => 2; + public override int GetPushCount() => 1; public override void Execute(ASMachine machine) { object result = null; @@ -22,8 +16,8 @@ public override void Execute(ASMachine machine) if (right != null && left != null) { int iLeft = Convert.ToInt32(left); - int iRight = (Convert.ToInt32(right) & 0x1F); - result = (iLeft >> iRight); + int iRight = Convert.ToInt32(right) & 0x1F; + result = iLeft >> iRight; } machine.Values.Push(result); } diff --git a/Flazzy/ABC/AVM2/Instructions/Bit Manipulation/URShiftIns.cs b/Flazzy/ABC/AVM2/Instructions/Bit Manipulation/URShiftIns.cs index ee8a147..9b918c9 100644 --- a/Flazzy/ABC/AVM2/Instructions/Bit Manipulation/URShiftIns.cs +++ b/Flazzy/ABC/AVM2/Instructions/Bit Manipulation/URShiftIns.cs @@ -1,19 +1,13 @@ namespace Flazzy.ABC.AVM2.Instructions; -public class URShiftIns : ASInstruction +public sealed class URShiftIns : ASInstruction { public URShiftIns() : base(OPCode.URShift) { } - public override int GetPopCount() - { - return 2; - } - public override int GetPushCount() - { - return 1; - } + public override int GetPopCount() => 2; + public override int GetPushCount() => 1; public override void Execute(ASMachine machine) { machine.Values.Pop(); diff --git a/Flazzy/ABC/AVM2/Instructions/CallIns.cs b/Flazzy/ABC/AVM2/Instructions/CallIns.cs index 834cd5f..7007ad0 100644 --- a/Flazzy/ABC/AVM2/Instructions/CallIns.cs +++ b/Flazzy/ABC/AVM2/Instructions/CallIns.cs @@ -2,7 +2,7 @@ namespace Flazzy.ABC.AVM2.Instructions; -public class CallIns : ASInstruction +public sealed class CallIns : ASInstruction { public int ArgCount { get; set; } @@ -14,20 +14,14 @@ public CallIns(int argCount) { ArgCount = argCount; } - public CallIns(FlashReader input) + public CallIns(ref SpanFlashReader input) : this() { - ArgCount = input.ReadInt30(); + ArgCount = input.ReadEncodedInt(); } - public override int GetPopCount() - { - return (ArgCount + 2); - } - public override int GetPushCount() - { - return 1; - } + public override int GetPopCount() => ArgCount + 2; + public override int GetPushCount() => 1; public override void Execute(ASMachine machine) { for (int i = 0; i < ArgCount; i++) @@ -39,8 +33,12 @@ public override void Execute(ASMachine machine) machine.Values.Push(null); } - protected override void WriteValuesTo(FlashWriter output) + protected override int GetBodySize() + { + return SpanFlashWriter.GetEncodedIntSize(ArgCount); + } + protected override void WriteValuesTo(ref SpanFlashWriter output) { - output.WriteInt30(ArgCount); + output.WriteEncodedInt(ArgCount); } } \ No newline at end of file diff --git a/Flazzy/ABC/AVM2/Instructions/CallMethodIns.cs b/Flazzy/ABC/AVM2/Instructions/CallMethodIns.cs index 44c3cfe..6ff3d03 100644 --- a/Flazzy/ABC/AVM2/Instructions/CallMethodIns.cs +++ b/Flazzy/ABC/AVM2/Instructions/CallMethodIns.cs @@ -2,7 +2,7 @@ namespace Flazzy.ABC.AVM2.Instructions; -public class CallMethodIns : ASInstruction +public sealed class CallMethodIns : ASInstruction { public int MethodIndex { get; set; } public int ArgCount { get; set; } @@ -10,11 +10,11 @@ public class CallMethodIns : ASInstruction public CallMethodIns(ABCFile abc) : base(OPCode.CallMethod, abc) { } - public CallMethodIns(ABCFile abc, FlashReader input) + public CallMethodIns(ABCFile abc, ref SpanFlashReader input) : this(abc) { - MethodIndex = input.ReadInt30(); - ArgCount = input.ReadInt30(); + MethodIndex = input.ReadEncodedInt(); + ArgCount = input.ReadEncodedInt(); } public CallMethodIns(ABCFile abc, int methodIndex) : this(abc) @@ -28,14 +28,8 @@ public CallMethodIns(ABCFile abc, int methodIndex, int argCount) ArgCount = argCount; } - public override int GetPopCount() - { - return ArgCount + 1; - } - public override int GetPushCount() - { - return 1; - } + public override int GetPopCount() => ArgCount + 1; + public override int GetPushCount() => 1; public override void Execute(ASMachine machine) { for (int i = 0; i < ArgCount; i++) @@ -46,9 +40,16 @@ public override void Execute(ASMachine machine) machine.Values.Push(null); } - protected override void WriteValuesTo(FlashWriter output) + protected override int GetBodySize() + { + int size = 0; + size += SpanFlashWriter.GetEncodedIntSize(MethodIndex); + size += SpanFlashWriter.GetEncodedIntSize(ArgCount); + return size; + } + protected override void WriteValuesTo(ref SpanFlashWriter output) { - output.WriteInt30(MethodIndex); - output.WriteInt30(ArgCount); + output.WriteEncodedInt(MethodIndex); + output.WriteEncodedInt(ArgCount); } } \ No newline at end of file diff --git a/Flazzy/ABC/AVM2/Instructions/CallPropLexIns.cs b/Flazzy/ABC/AVM2/Instructions/CallPropLexIns.cs index a00fb4b..8b82a77 100644 --- a/Flazzy/ABC/AVM2/Instructions/CallPropLexIns.cs +++ b/Flazzy/ABC/AVM2/Instructions/CallPropLexIns.cs @@ -2,7 +2,7 @@ namespace Flazzy.ABC.AVM2.Instructions; -public class CallPropLexIns : ASInstruction +public sealed class CallPropLexIns : ASInstruction { public int PropertyNameIndex { get; set; } public ASMultiname PropertyName => ABC.Pool.Multinames[PropertyNameIndex]; @@ -12,11 +12,11 @@ public class CallPropLexIns : ASInstruction public CallPropLexIns(ABCFile abc) : base(OPCode.CallPropLex, abc) { } - public CallPropLexIns(ABCFile abc, FlashReader input) + public CallPropLexIns(ABCFile abc, ref SpanFlashReader input) : this(abc) { - PropertyNameIndex = input.ReadInt30(); - ArgCount = input.ReadInt30(); + PropertyNameIndex = input.ReadEncodedInt(); + ArgCount = input.ReadEncodedInt(); } public CallPropLexIns(ABCFile abc, int propertyNameIndex) : this(abc) @@ -30,14 +30,9 @@ public CallPropLexIns(ABCFile abc, int propertyNameIndex, int argCount) ArgCount = argCount; } - public override int GetPopCount() - { - return (ArgCount + ResolveMultinamePops(PropertyName) + 1); - } - public override int GetPushCount() - { - return 1; - } + public override int GetPopCount() => ArgCount + ResolveMultinamePops(PropertyName) + 1; + public override int GetPushCount() => 1; + public override void Execute(ASMachine machine) { for (int i = 0; i < ArgCount; i++) @@ -49,9 +44,16 @@ public override void Execute(ASMachine machine) machine.Values.Push(null); } - protected override void WriteValuesTo(FlashWriter output) + protected override int GetBodySize() + { + int size = 0; + size += SpanFlashWriter.GetEncodedIntSize(PropertyNameIndex); + size += SpanFlashWriter.GetEncodedIntSize(ArgCount); + return size; + } + protected override void WriteValuesTo(ref SpanFlashWriter output) { - output.WriteInt30(PropertyNameIndex); - output.WriteInt30(ArgCount); + output.WriteEncodedInt(PropertyNameIndex); + output.WriteEncodedInt(ArgCount); } } \ No newline at end of file diff --git a/Flazzy/ABC/AVM2/Instructions/CallPropVoidIns.cs b/Flazzy/ABC/AVM2/Instructions/CallPropVoidIns.cs index 43dfe15..df67f96 100644 --- a/Flazzy/ABC/AVM2/Instructions/CallPropVoidIns.cs +++ b/Flazzy/ABC/AVM2/Instructions/CallPropVoidIns.cs @@ -3,7 +3,7 @@ namespace Flazzy.ABC.AVM2.Instructions; -public class CallPropVoidIns : ASInstruction, IPropertyContainer +public sealed class CallPropVoidIns : ASInstruction, IPropertyContainer { public int PropertyNameIndex { get; set; } public ASMultiname PropertyName => ABC.Pool.Multinames[PropertyNameIndex]; @@ -13,11 +13,11 @@ public class CallPropVoidIns : ASInstruction, IPropertyContainer public CallPropVoidIns(ABCFile abc) : base(OPCode.CallPropVoid, abc) { } - public CallPropVoidIns(ABCFile abc, FlashReader input) + public CallPropVoidIns(ABCFile abc, ref SpanFlashReader input) : this(abc) { - PropertyNameIndex = input.ReadInt30(); - ArgCount = input.ReadInt30(); + PropertyNameIndex = input.ReadEncodedInt(); + ArgCount = input.ReadEncodedInt(); } public CallPropVoidIns(ABCFile abc, int propertyNameIndex) : this(abc) @@ -33,7 +33,7 @@ public CallPropVoidIns(ABCFile abc, int propertyNameIndex, int argCount) public override int GetPopCount() { - return (ArgCount + ResolveMultinamePops(PropertyName) + 1); + return ArgCount + ResolveMultinamePops(PropertyName) + 1; } public override void Execute(ASMachine machine) { @@ -45,9 +45,16 @@ public override void Execute(ASMachine machine) object obj = machine.Values.Pop(); } - protected override void WriteValuesTo(FlashWriter output) + protected override int GetBodySize() { - output.WriteInt30(PropertyNameIndex); - output.WriteInt30(ArgCount); + int size = 0; + size += SpanFlashWriter.GetEncodedIntSize(PropertyNameIndex); + size += SpanFlashWriter.GetEncodedIntSize(ArgCount); + return size; + } + protected override void WriteValuesTo(ref SpanFlashWriter output) + { + output.WriteEncodedInt(PropertyNameIndex); + output.WriteEncodedInt(ArgCount); } } \ No newline at end of file diff --git a/Flazzy/ABC/AVM2/Instructions/CallPropertyIns.cs b/Flazzy/ABC/AVM2/Instructions/CallPropertyIns.cs index 2e2d01e..fee2e40 100644 --- a/Flazzy/ABC/AVM2/Instructions/CallPropertyIns.cs +++ b/Flazzy/ABC/AVM2/Instructions/CallPropertyIns.cs @@ -3,7 +3,7 @@ namespace Flazzy.ABC.AVM2.Instructions; -public class CallPropertyIns : ASInstruction, IPropertyContainer +public sealed class CallPropertyIns : ASInstruction, IPropertyContainer { public int PropertyNameIndex { get; set; } public ASMultiname PropertyName => ABC.Pool.Multinames[PropertyNameIndex]; @@ -13,11 +13,11 @@ public class CallPropertyIns : ASInstruction, IPropertyContainer public CallPropertyIns(ABCFile abc) : base(OPCode.CallProperty, abc) { } - public CallPropertyIns(ABCFile abc, FlashReader input) + public CallPropertyIns(ABCFile abc, ref SpanFlashReader input) : this(abc) { - PropertyNameIndex = input.ReadInt30(); - ArgCount = input.ReadInt30(); + PropertyNameIndex = input.ReadEncodedInt(); + ArgCount = input.ReadEncodedInt(); } public CallPropertyIns(ABCFile abc, int propertyNameIndex) : this(abc) @@ -33,12 +33,9 @@ public CallPropertyIns(ABCFile abc, int propertyNameIndex, int argCount) public override int GetPopCount() { - return (ArgCount + ResolveMultinamePops(PropertyName) + 1); - } - public override int GetPushCount() - { - return 1; + return ArgCount + ResolveMultinamePops(PropertyName) + 1; } + public override int GetPushCount() => 1; public override void Execute(ASMachine machine) { for (int i = 0; i < ArgCount; i++) @@ -50,9 +47,16 @@ public override void Execute(ASMachine machine) machine.Values.Push(null); } - protected override void WriteValuesTo(FlashWriter output) + protected override int GetBodySize() + { + int size = 0; + size += SpanFlashWriter.GetEncodedIntSize(PropertyNameIndex); + size += SpanFlashWriter.GetEncodedIntSize(ArgCount); + return size; + } + protected override void WriteValuesTo(ref SpanFlashWriter output) { - output.WriteInt30(PropertyNameIndex); - output.WriteInt30(ArgCount); + output.WriteEncodedInt(PropertyNameIndex); + output.WriteEncodedInt(ArgCount); } } \ No newline at end of file diff --git a/Flazzy/ABC/AVM2/Instructions/CallStaticIns.cs b/Flazzy/ABC/AVM2/Instructions/CallStaticIns.cs index 1c48b15..22e2f56 100644 --- a/Flazzy/ABC/AVM2/Instructions/CallStaticIns.cs +++ b/Flazzy/ABC/AVM2/Instructions/CallStaticIns.cs @@ -2,7 +2,7 @@ namespace Flazzy.ABC.AVM2.Instructions; -public class CallStaticIns : ASInstruction +public sealed class CallStaticIns : ASInstruction { public int MethodIndex { get; set; } public ASMethod Method => ABC.Methods[MethodIndex]; @@ -12,11 +12,11 @@ public class CallStaticIns : ASInstruction public CallStaticIns(ABCFile abc) : base(OPCode.CallStatic, abc) { } - public CallStaticIns(ABCFile abc, FlashReader input) + public CallStaticIns(ABCFile abc, ref SpanFlashReader input) : this(abc) { - MethodIndex = input.ReadInt30(); - ArgCount = input.ReadInt30(); + MethodIndex = input.ReadEncodedInt(); + ArgCount = input.ReadEncodedInt(); } public CallStaticIns(ABCFile abc, int methodIndex) : this(abc) @@ -30,14 +30,9 @@ public CallStaticIns(ABCFile abc, int methodIndex, int argCount) ArgCount = argCount; } - public override int GetPopCount() - { - return ArgCount + 1; - } - public override int GetPushCount() - { - return 1; - } + public override int GetPopCount() => ArgCount + 1; + public override int GetPushCount() => 1; + public override void Execute(ASMachine machine) { for (int i = 0; i < ArgCount; i++) @@ -48,9 +43,16 @@ public override void Execute(ASMachine machine) machine.Values.Push(null); } - protected override void WriteValuesTo(FlashWriter output) + protected override int GetBodySize() + { + int size = 0; + size += SpanFlashWriter.GetEncodedIntSize(MethodIndex); + size += SpanFlashWriter.GetEncodedIntSize(ArgCount); + return size; + } + protected override void WriteValuesTo(ref SpanFlashWriter output) { - output.WriteInt30(MethodIndex); - output.WriteInt30(ArgCount); + output.WriteEncodedInt(MethodIndex); + output.WriteEncodedInt(ArgCount); } } \ No newline at end of file diff --git a/Flazzy/ABC/AVM2/Instructions/CallSuperIns.cs b/Flazzy/ABC/AVM2/Instructions/CallSuperIns.cs index 57a44b9..139fa5c 100644 --- a/Flazzy/ABC/AVM2/Instructions/CallSuperIns.cs +++ b/Flazzy/ABC/AVM2/Instructions/CallSuperIns.cs @@ -2,7 +2,7 @@ namespace Flazzy.ABC.AVM2.Instructions; -public class CallSuperIns : ASInstruction +public sealed class CallSuperIns : ASInstruction { public int MethodNameIndex { get; set; } public ASMultiname MethodName => ABC.Pool.Multinames[MethodNameIndex]; @@ -12,11 +12,11 @@ public class CallSuperIns : ASInstruction public CallSuperIns(ABCFile abc) : base(OPCode.CallSuper, abc) { } - public CallSuperIns(ABCFile abc, FlashReader input) + public CallSuperIns(ABCFile abc, ref SpanFlashReader input) : this(abc) { - MethodNameIndex = input.ReadInt30(); - ArgCount = input.ReadInt30(); + MethodNameIndex = input.ReadEncodedInt(); + ArgCount = input.ReadEncodedInt(); } public CallSuperIns(ABCFile abc, int methodNameIndex) : this(abc) @@ -32,12 +32,9 @@ public CallSuperIns(ABCFile abc, int methodNameIndex, int argCount) public override int GetPopCount() { - return (ArgCount + ResolveMultinamePops(MethodName) + 1); - } - public override int GetPushCount() - { - return 1; + return ArgCount + ResolveMultinamePops(MethodName) + 1; } + public override int GetPushCount() => 1; public override void Execute(ASMachine machine) { for (int i = 0; i < ArgCount; i++) @@ -49,9 +46,16 @@ public override void Execute(ASMachine machine) machine.Values.Push(null); } - protected override void WriteValuesTo(FlashWriter output) + protected override int GetBodySize() + { + int size = 0; + size += SpanFlashWriter.GetEncodedIntSize(MethodNameIndex); + size += SpanFlashWriter.GetEncodedIntSize(ArgCount); + return size; + } + protected override void WriteValuesTo(ref SpanFlashWriter output) { - output.WriteInt30(MethodNameIndex); - output.WriteInt30(ArgCount); + output.WriteEncodedInt(MethodNameIndex); + output.WriteEncodedInt(ArgCount); } } \ No newline at end of file diff --git a/Flazzy/ABC/AVM2/Instructions/CallSuperVoidIns.cs b/Flazzy/ABC/AVM2/Instructions/CallSuperVoidIns.cs index eb865d1..f707416 100644 --- a/Flazzy/ABC/AVM2/Instructions/CallSuperVoidIns.cs +++ b/Flazzy/ABC/AVM2/Instructions/CallSuperVoidIns.cs @@ -2,7 +2,7 @@ namespace Flazzy.ABC.AVM2.Instructions; -public class CallSuperVoidIns : ASInstruction +public sealed class CallSuperVoidIns : ASInstruction { public int MethodNameIndex { get; set; } public ASMultiname MethodName => ABC.Pool.Multinames[MethodNameIndex]; @@ -12,11 +12,11 @@ public class CallSuperVoidIns : ASInstruction public CallSuperVoidIns(ABCFile abc) : base(OPCode.CallSuperVoid, abc) { } - public CallSuperVoidIns(ABCFile abc, FlashReader input) + public CallSuperVoidIns(ABCFile abc, ref SpanFlashReader input) : this(abc) { - MethodNameIndex = input.ReadInt30(); - ArgCount = input.ReadInt30(); + MethodNameIndex = input.ReadEncodedInt(); + ArgCount = input.ReadEncodedInt(); } public CallSuperVoidIns(ABCFile abc, int methodNameIndex) : this(abc) @@ -32,12 +32,9 @@ public CallSuperVoidIns(ABCFile abc, int methodNameIndex, int argCount) public override int GetPopCount() { - return (ArgCount + ResolveMultinamePops(MethodName) + 1); - } - public override int GetPushCount() - { - return 1; + return ArgCount + ResolveMultinamePops(MethodName) + 1; } + public override int GetPushCount() => 1; public override void Execute(ASMachine machine) { for (int i = 0; i < ArgCount; i++) @@ -49,9 +46,16 @@ public override void Execute(ASMachine machine) machine.Values.Push(null); } - protected override void WriteValuesTo(FlashWriter output) + protected override int GetBodySize() + { + int size = 0; + size += SpanFlashWriter.GetEncodedIntSize(MethodNameIndex); + size += SpanFlashWriter.GetEncodedIntSize(ArgCount); + return size; + } + protected override void WriteValuesTo(ref SpanFlashWriter output) { - output.WriteInt30(MethodNameIndex); - output.WriteInt30(ArgCount); + output.WriteEncodedInt(MethodNameIndex); + output.WriteEncodedInt(ArgCount); } } \ No newline at end of file diff --git a/Flazzy/ABC/AVM2/Instructions/CheckFilterIns.cs b/Flazzy/ABC/AVM2/Instructions/CheckFilterIns.cs index 8fe34fb..de46267 100644 --- a/Flazzy/ABC/AVM2/Instructions/CheckFilterIns.cs +++ b/Flazzy/ABC/AVM2/Instructions/CheckFilterIns.cs @@ -1,19 +1,13 @@ namespace Flazzy.ABC.AVM2.Instructions; -public class CheckFilterIns : ASInstruction +public sealed class CheckFilterIns : ASInstruction { public CheckFilterIns() : base(OPCode.CheckFilter) { } - public override int GetPopCount() - { - return 1; - } - public override int GetPushCount() - { - return 1; - } + public override int GetPopCount() => 1; + public override int GetPushCount() => 1; public override void Execute(ASMachine machine) { object value = machine.Values.Pop(); diff --git a/Flazzy/ABC/AVM2/Instructions/ConstructIns.cs b/Flazzy/ABC/AVM2/Instructions/ConstructIns.cs index ddb5f24..9eb635d 100644 --- a/Flazzy/ABC/AVM2/Instructions/ConstructIns.cs +++ b/Flazzy/ABC/AVM2/Instructions/ConstructIns.cs @@ -2,7 +2,7 @@ namespace Flazzy.ABC.AVM2.Instructions; -public class ConstructIns : ASInstruction +public sealed class ConstructIns : ASInstruction { public int ArgCount { get; set; } @@ -14,20 +14,14 @@ public ConstructIns(int argCount) { ArgCount = argCount; } - public ConstructIns(FlashReader input) + public ConstructIns(ref SpanFlashReader input) : this() { - ArgCount = input.ReadInt30(); + ArgCount = input.ReadEncodedInt(); } - public override int GetPopCount() - { - return (ArgCount + 1); - } - public override int GetPushCount() - { - return 1; - } + public override int GetPopCount() => ArgCount + 1; + public override int GetPushCount() => 1; public override void Execute(ASMachine machine) { for (int i = 0; i < ArgCount; i++) @@ -38,8 +32,12 @@ public override void Execute(ASMachine machine) machine.Values.Push(null); } - protected override void WriteValuesTo(FlashWriter output) + protected override int GetBodySize() + { + return SpanFlashWriter.GetEncodedIntSize(ArgCount); + } + protected override void WriteValuesTo(ref SpanFlashWriter output) { - output.WriteInt30(ArgCount); + output.WriteEncodedInt(ArgCount); } } \ No newline at end of file diff --git a/Flazzy/ABC/AVM2/Instructions/ConstructPropIns.cs b/Flazzy/ABC/AVM2/Instructions/ConstructPropIns.cs index 9594792..da970b3 100644 --- a/Flazzy/ABC/AVM2/Instructions/ConstructPropIns.cs +++ b/Flazzy/ABC/AVM2/Instructions/ConstructPropIns.cs @@ -3,7 +3,7 @@ namespace Flazzy.ABC.AVM2.Instructions; -public class ConstructPropIns : ASInstruction, IPropertyContainer +public sealed class ConstructPropIns : ASInstruction, IPropertyContainer { public int PropertyNameIndex { get; set; } public ASMultiname PropertyName => ABC.Pool.Multinames[PropertyNameIndex]; @@ -13,11 +13,11 @@ public class ConstructPropIns : ASInstruction, IPropertyContainer public ConstructPropIns(ABCFile abc) : base(OPCode.ConstructProp, abc) { } - public ConstructPropIns(ABCFile abc, FlashReader input) + public ConstructPropIns(ABCFile abc, ref SpanFlashReader input) : this(abc) { - PropertyNameIndex = input.ReadInt30(); - ArgCount = input.ReadInt30(); + PropertyNameIndex = input.ReadEncodedInt(); + ArgCount = input.ReadEncodedInt(); } public ConstructPropIns(ABCFile abc, int propertyNameIndex) : this(abc) @@ -33,12 +33,9 @@ public ConstructPropIns(ABCFile abc, int propertyNameIndex, int argCount) public override int GetPopCount() { - return (ArgCount + ResolveMultinamePops(PropertyName) + 1); - } - public override int GetPushCount() - { - return 1; + return ArgCount + ResolveMultinamePops(PropertyName) + 1; } + public override int GetPushCount() => 1; public override void Execute(ASMachine machine) { for (int i = 0; i < ArgCount; i++) @@ -50,9 +47,16 @@ public override void Execute(ASMachine machine) machine.Values.Push(null); } - protected override void WriteValuesTo(FlashWriter output) + protected override int GetBodySize() + { + int size = 0; + size += SpanFlashWriter.GetEncodedIntSize(PropertyNameIndex); + size += SpanFlashWriter.GetEncodedIntSize(ArgCount); + return size; + } + protected override void WriteValuesTo(ref SpanFlashWriter output) { - output.WriteInt30(PropertyNameIndex); - output.WriteInt30(ArgCount); + output.WriteEncodedInt(PropertyNameIndex); + output.WriteEncodedInt(ArgCount); } } \ No newline at end of file diff --git a/Flazzy/ABC/AVM2/Instructions/ConstructSuperIns.cs b/Flazzy/ABC/AVM2/Instructions/ConstructSuperIns.cs index d47b223..a93194a 100644 --- a/Flazzy/ABC/AVM2/Instructions/ConstructSuperIns.cs +++ b/Flazzy/ABC/AVM2/Instructions/ConstructSuperIns.cs @@ -2,7 +2,7 @@ namespace Flazzy.ABC.AVM2.Instructions; -public class ConstructSuperIns : ASInstruction +public sealed class ConstructSuperIns : ASInstruction { public int ArgCount { get; set; } @@ -14,16 +14,13 @@ public ConstructSuperIns(int argCount) { ArgCount = argCount; } - public ConstructSuperIns(FlashReader input) + public ConstructSuperIns(ref SpanFlashReader input) : this() { - ArgCount = input.ReadInt30(); + ArgCount = input.ReadEncodedInt(); } - public override int GetPopCount() - { - return (ArgCount + 1); - } + public override int GetPopCount() => ArgCount + 1; public override void Execute(ASMachine machine) { for (int i = 0; i < ArgCount; i++) @@ -33,8 +30,12 @@ public override void Execute(ASMachine machine) object obj = machine.Values.Pop(); } - protected override void WriteValuesTo(FlashWriter output) + protected override int GetBodySize() + { + return SpanFlashWriter.GetEncodedIntSize(ArgCount); + } + protected override void WriteValuesTo(ref SpanFlashWriter output) { - output.WriteInt30(ArgCount); + output.WriteEncodedInt(ArgCount); } } \ No newline at end of file diff --git a/Flazzy/ABC/AVM2/Instructions/Control Transfer/IfEqualIns.cs b/Flazzy/ABC/AVM2/Instructions/Control Transfer/IfEqualIns.cs index b192098..cebb6ee 100644 --- a/Flazzy/ABC/AVM2/Instructions/Control Transfer/IfEqualIns.cs +++ b/Flazzy/ABC/AVM2/Instructions/Control Transfer/IfEqualIns.cs @@ -2,13 +2,13 @@ namespace Flazzy.ABC.AVM2.Instructions; -public class IfEqualIns : Jumper +public sealed class IfEqualIns : Jumper { public IfEqualIns() : base(OPCode.IfEq) { } - public IfEqualIns(FlashReader input) - : base(OPCode.IfEq, input) + public IfEqualIns(ref SpanFlashReader input) + : base(OPCode.IfEq, ref input) { } public override bool? RunCondition(ASMachine machine) diff --git a/Flazzy/ABC/AVM2/Instructions/Control Transfer/IfFalseIns.cs b/Flazzy/ABC/AVM2/Instructions/Control Transfer/IfFalseIns.cs index b5808f1..3a9999a 100644 --- a/Flazzy/ABC/AVM2/Instructions/Control Transfer/IfFalseIns.cs +++ b/Flazzy/ABC/AVM2/Instructions/Control Transfer/IfFalseIns.cs @@ -2,20 +2,20 @@ namespace Flazzy.ABC.AVM2.Instructions; -public class IfFalseIns : Jumper +public sealed class IfFalseIns : Jumper { public IfFalseIns() : base(OPCode.IfFalse) { } - public IfFalseIns(FlashReader input) - : base(OPCode.IfFalse, input) + public IfFalseIns(ref SpanFlashReader input) + : base(OPCode.IfFalse, ref input) { } public override bool? RunCondition(ASMachine machine) { - var value = (machine.Values.Pop() as bool?); + var value = machine.Values.Pop() as bool?; if (value == null) return null; - return (value == false); + return value == false; } } \ No newline at end of file diff --git a/Flazzy/ABC/AVM2/Instructions/Control Transfer/IfGreaterEqualIns.cs b/Flazzy/ABC/AVM2/Instructions/Control Transfer/IfGreaterEqualIns.cs index 48c0285..887ed49 100644 --- a/Flazzy/ABC/AVM2/Instructions/Control Transfer/IfGreaterEqualIns.cs +++ b/Flazzy/ABC/AVM2/Instructions/Control Transfer/IfGreaterEqualIns.cs @@ -2,13 +2,13 @@ namespace Flazzy.ABC.AVM2.Instructions; -public class IfGreaterEqualIns : Jumper +public sealed class IfGreaterEqualIns : Jumper { public IfGreaterEqualIns() : base(OPCode.IfGe) { } - public IfGreaterEqualIns(FlashReader input) - : base(OPCode.IfGe, input) + public IfGreaterEqualIns(ref SpanFlashReader input) + : base(OPCode.IfGe, ref input) { } public override bool? RunCondition(ASMachine machine) @@ -17,6 +17,6 @@ public IfGreaterEqualIns(FlashReader input) var left = machine.Values.Pop(); if (left == null || right == null) return null; - return (Convert.ToDouble(left) >= Convert.ToDouble(right)); + return Convert.ToDouble(left) >= Convert.ToDouble(right); } } \ No newline at end of file diff --git a/Flazzy/ABC/AVM2/Instructions/Control Transfer/IfGreaterThanIns.cs b/Flazzy/ABC/AVM2/Instructions/Control Transfer/IfGreaterThanIns.cs index 2ba091d..5d425d0 100644 --- a/Flazzy/ABC/AVM2/Instructions/Control Transfer/IfGreaterThanIns.cs +++ b/Flazzy/ABC/AVM2/Instructions/Control Transfer/IfGreaterThanIns.cs @@ -2,13 +2,13 @@ namespace Flazzy.ABC.AVM2.Instructions; -public class IfGreaterThanIns : Jumper +public sealed class IfGreaterThanIns : Jumper { public IfGreaterThanIns() : base(OPCode.IfGt) { } - public IfGreaterThanIns(FlashReader input) - : base(OPCode.IfGt, input) + public IfGreaterThanIns(ref SpanFlashReader input) + : base(OPCode.IfGt, ref input) { } public override bool? RunCondition(ASMachine machine) @@ -17,6 +17,6 @@ public IfGreaterThanIns(FlashReader input) var left = machine.Values.Pop(); if (left == null || right == null) return null; - return (Convert.ToDouble(left) > Convert.ToDouble(right)); + return Convert.ToDouble(left) > Convert.ToDouble(right); } } \ No newline at end of file diff --git a/Flazzy/ABC/AVM2/Instructions/Control Transfer/IfLessEqualIns.cs b/Flazzy/ABC/AVM2/Instructions/Control Transfer/IfLessEqualIns.cs index c790e50..7ee7caa 100644 --- a/Flazzy/ABC/AVM2/Instructions/Control Transfer/IfLessEqualIns.cs +++ b/Flazzy/ABC/AVM2/Instructions/Control Transfer/IfLessEqualIns.cs @@ -2,13 +2,13 @@ namespace Flazzy.ABC.AVM2.Instructions; -public class IfLessEqualIns : Jumper +public sealed class IfLessEqualIns : Jumper { public IfLessEqualIns() : base(OPCode.IfLe) { } - public IfLessEqualIns(FlashReader input) - : base(OPCode.IfLe, input) + public IfLessEqualIns(ref SpanFlashReader input) + : base(OPCode.IfLe, ref input) { } public override bool? RunCondition(ASMachine machine) @@ -17,6 +17,6 @@ public IfLessEqualIns(FlashReader input) var left = machine.Values.Pop(); if (left == null || right == null) return null; - return (Convert.ToDouble(left) <= Convert.ToDouble(right)); + return Convert.ToDouble(left) <= Convert.ToDouble(right); } } \ No newline at end of file diff --git a/Flazzy/ABC/AVM2/Instructions/Control Transfer/IfLessThanIns.cs b/Flazzy/ABC/AVM2/Instructions/Control Transfer/IfLessThanIns.cs index 7ba1499..6dc1457 100644 --- a/Flazzy/ABC/AVM2/Instructions/Control Transfer/IfLessThanIns.cs +++ b/Flazzy/ABC/AVM2/Instructions/Control Transfer/IfLessThanIns.cs @@ -2,13 +2,13 @@ namespace Flazzy.ABC.AVM2.Instructions; -public class IfLessThanIns : Jumper +public sealed class IfLessThanIns : Jumper { public IfLessThanIns() : base(OPCode.IfLt) { } - public IfLessThanIns(FlashReader input) - : base(OPCode.IfLt, input) + public IfLessThanIns(ref SpanFlashReader input) + : base(OPCode.IfLt, ref input) { } public override bool? RunCondition(ASMachine machine) diff --git a/Flazzy/ABC/AVM2/Instructions/Control Transfer/IfNotEqualIns.cs b/Flazzy/ABC/AVM2/Instructions/Control Transfer/IfNotEqualIns.cs index d3606ca..d70874c 100644 --- a/Flazzy/ABC/AVM2/Instructions/Control Transfer/IfNotEqualIns.cs +++ b/Flazzy/ABC/AVM2/Instructions/Control Transfer/IfNotEqualIns.cs @@ -2,13 +2,13 @@ namespace Flazzy.ABC.AVM2.Instructions; -public class IfNotEqualIns : Jumper +public sealed class IfNotEqualIns : Jumper { public IfNotEqualIns() : base(OPCode.IfNe) { } - public IfNotEqualIns(FlashReader input) - : base(OPCode.IfNe, input) + public IfNotEqualIns(ref SpanFlashReader input) + : base(OPCode.IfNe, ref input) { } public override bool? RunCondition(ASMachine machine) @@ -17,6 +17,6 @@ public IfNotEqualIns(FlashReader input) dynamic left = machine.Values.Pop(); if (left == null || right == null) return null; - return (left != right); + return left != right; } } \ No newline at end of file diff --git a/Flazzy/ABC/AVM2/Instructions/Control Transfer/IfNotGreaterEqualIns.cs b/Flazzy/ABC/AVM2/Instructions/Control Transfer/IfNotGreaterEqualIns.cs index 6d220cf..8c37ef6 100644 --- a/Flazzy/ABC/AVM2/Instructions/Control Transfer/IfNotGreaterEqualIns.cs +++ b/Flazzy/ABC/AVM2/Instructions/Control Transfer/IfNotGreaterEqualIns.cs @@ -2,13 +2,13 @@ namespace Flazzy.ABC.AVM2.Instructions; -public class IfNotGreaterEqualIns : Jumper +public sealed class IfNotGreaterEqualIns : Jumper { public IfNotGreaterEqualIns() : base(OPCode.IfNGe) { } - public IfNotGreaterEqualIns(FlashReader input) - : base(OPCode.IfNGe, input) + public IfNotGreaterEqualIns(ref SpanFlashReader input) + : base(OPCode.IfNGe, ref input) { } public override bool? RunCondition(ASMachine machine) diff --git a/Flazzy/ABC/AVM2/Instructions/Control Transfer/IfNotGreaterThanIns.cs b/Flazzy/ABC/AVM2/Instructions/Control Transfer/IfNotGreaterThanIns.cs index 8038414..5445d80 100644 --- a/Flazzy/ABC/AVM2/Instructions/Control Transfer/IfNotGreaterThanIns.cs +++ b/Flazzy/ABC/AVM2/Instructions/Control Transfer/IfNotGreaterThanIns.cs @@ -2,13 +2,13 @@ namespace Flazzy.ABC.AVM2.Instructions; -public class IfNotGreaterThanIns : Jumper +public sealed class IfNotGreaterThanIns : Jumper { public IfNotGreaterThanIns() : base(OPCode.IfNGt) { } - public IfNotGreaterThanIns(FlashReader input) - : base(OPCode.IfNGt, input) + public IfNotGreaterThanIns(ref SpanFlashReader input) + : base(OPCode.IfNGt, ref input) { } public override bool? RunCondition(ASMachine machine) diff --git a/Flazzy/ABC/AVM2/Instructions/Control Transfer/IfNotLessEqualIns.cs b/Flazzy/ABC/AVM2/Instructions/Control Transfer/IfNotLessEqualIns.cs index 1f88d28..f1f79e8 100644 --- a/Flazzy/ABC/AVM2/Instructions/Control Transfer/IfNotLessEqualIns.cs +++ b/Flazzy/ABC/AVM2/Instructions/Control Transfer/IfNotLessEqualIns.cs @@ -2,13 +2,13 @@ namespace Flazzy.ABC.AVM2.Instructions; -public class IfNotLessEqualIns : Jumper +public sealed class IfNotLessEqualIns : Jumper { public IfNotLessEqualIns() : base(OPCode.IfNLe) { } - public IfNotLessEqualIns(FlashReader input) - : base(OPCode.IfNLe, input) + public IfNotLessEqualIns(ref SpanFlashReader input) + : base(OPCode.IfNLe, ref input) { } public override bool? RunCondition(ASMachine machine) diff --git a/Flazzy/ABC/AVM2/Instructions/Control Transfer/IfNotLessThanIns.cs b/Flazzy/ABC/AVM2/Instructions/Control Transfer/IfNotLessThanIns.cs index dc85fef..e457192 100644 --- a/Flazzy/ABC/AVM2/Instructions/Control Transfer/IfNotLessThanIns.cs +++ b/Flazzy/ABC/AVM2/Instructions/Control Transfer/IfNotLessThanIns.cs @@ -2,13 +2,13 @@ namespace Flazzy.ABC.AVM2.Instructions; -public class IfNotLessThanIns : Jumper +public sealed class IfNotLessThanIns : Jumper { public IfNotLessThanIns() : base(OPCode.IfNLt) { } - public IfNotLessThanIns(FlashReader input) - : base(OPCode.IfNLt, input) + public IfNotLessThanIns(ref SpanFlashReader input) + : base(OPCode.IfNLt, ref input) { } public override bool? RunCondition(ASMachine machine) diff --git a/Flazzy/ABC/AVM2/Instructions/Control Transfer/IfStrictEqualIns.cs b/Flazzy/ABC/AVM2/Instructions/Control Transfer/IfStrictEqualIns.cs index c6c5531..a836f55 100644 --- a/Flazzy/ABC/AVM2/Instructions/Control Transfer/IfStrictEqualIns.cs +++ b/Flazzy/ABC/AVM2/Instructions/Control Transfer/IfStrictEqualIns.cs @@ -2,13 +2,13 @@ namespace Flazzy.ABC.AVM2.Instructions; -public class IfStrictEqualIns : Jumper +public sealed class IfStrictEqualIns : Jumper { public IfStrictEqualIns() : base(OPCode.IfStrictEq) { } - public IfStrictEqualIns(FlashReader input) - : base(OPCode.IfStrictEq, input) + public IfStrictEqualIns(ref SpanFlashReader input) + : base(OPCode.IfStrictEq, ref input) { } public override bool? RunCondition(ASMachine machine) diff --git a/Flazzy/ABC/AVM2/Instructions/Control Transfer/IfStrictNotEqualIns.cs b/Flazzy/ABC/AVM2/Instructions/Control Transfer/IfStrictNotEqualIns.cs index 31818c5..60f2cea 100644 --- a/Flazzy/ABC/AVM2/Instructions/Control Transfer/IfStrictNotEqualIns.cs +++ b/Flazzy/ABC/AVM2/Instructions/Control Transfer/IfStrictNotEqualIns.cs @@ -2,13 +2,13 @@ namespace Flazzy.ABC.AVM2.Instructions; -public class IfStrictNotEqualIns : Jumper +public sealed class IfStrictNotEqualIns : Jumper { public IfStrictNotEqualIns() : base(OPCode.IfStrictNE) { } - public IfStrictNotEqualIns(FlashReader input) - : base(OPCode.IfStrictNE, input) + public IfStrictNotEqualIns(ref SpanFlashReader input) + : base(OPCode.IfStrictNE, ref input) { } public override bool? RunCondition(ASMachine machine) diff --git a/Flazzy/ABC/AVM2/Instructions/Control Transfer/IfTrueIns.cs b/Flazzy/ABC/AVM2/Instructions/Control Transfer/IfTrueIns.cs index 910d3f5..774b300 100644 --- a/Flazzy/ABC/AVM2/Instructions/Control Transfer/IfTrueIns.cs +++ b/Flazzy/ABC/AVM2/Instructions/Control Transfer/IfTrueIns.cs @@ -2,17 +2,17 @@ namespace Flazzy.ABC.AVM2.Instructions; -public class IfTrueIns : Jumper +public sealed class IfTrueIns : Jumper { public IfTrueIns() : base(OPCode.IfTrue) { } - public IfTrueIns(FlashReader input) - : base(OPCode.IfTrue, input) + public IfTrueIns(ref SpanFlashReader input) + : base(OPCode.IfTrue, ref input) { } public override bool? RunCondition(ASMachine machine) { - return (machine.Values.Pop() as bool?); + return machine.Values.Pop() as bool?; } } \ No newline at end of file diff --git a/Flazzy/ABC/AVM2/Instructions/Control Transfer/JumpIns.cs b/Flazzy/ABC/AVM2/Instructions/Control Transfer/JumpIns.cs index 8f6d0d9..4262d3f 100644 --- a/Flazzy/ABC/AVM2/Instructions/Control Transfer/JumpIns.cs +++ b/Flazzy/ABC/AVM2/Instructions/Control Transfer/JumpIns.cs @@ -2,17 +2,14 @@ namespace Flazzy.ABC.AVM2.Instructions; -public class JumpIns : Jumper +public sealed class JumpIns : Jumper { public JumpIns() : base(OPCode.Jump) { } - public JumpIns(FlashReader input) - : base(OPCode.Jump, input) + public JumpIns(ref SpanFlashReader input) + : base(OPCode.Jump, ref input) { } - public override bool? RunCondition(ASMachine machine) - { - return true; - } + public override bool? RunCondition(ASMachine machine) => true; } \ No newline at end of file diff --git a/Flazzy/ABC/AVM2/Instructions/Control Transfer/Jumper.cs b/Flazzy/ABC/AVM2/Instructions/Control Transfer/Jumper.cs index 4ffc148..486da0f 100644 --- a/Flazzy/ABC/AVM2/Instructions/Control Transfer/Jumper.cs +++ b/Flazzy/ABC/AVM2/Instructions/Control Transfer/Jumper.cs @@ -14,30 +14,21 @@ public Jumper(OPCode op, uint offset) { Offset = offset; } - public Jumper(OPCode op, FlashReader input) + public Jumper(OPCode op, ref SpanFlashReader input) : this(op) { Offset = input.ReadUInt24(); } - public override int GetPopCount() + public override int GetPopCount() => OP switch { - switch (OP) - { - case OPCode.Jump: - return 0; + OPCode.Jump => 0, + OPCode.IfTrue or OPCode.IfFalse => 1, - case OPCode.IfTrue: - case OPCode.IfFalse: - return 1; + _ => 2, + }; + public override int GetPushCount() => 0; - default: return 2; - } - } - public override int GetPushCount() - { - return 0; - } public override void Execute(ASMachine machine) { int popCount = GetPopCount(); @@ -48,33 +39,30 @@ public override void Execute(ASMachine machine) } public abstract bool? RunCondition(ASMachine machine); - protected override void WriteValuesTo(FlashWriter output) + protected override int GetBodySize() => 3; + protected override void WriteValuesTo(ref SpanFlashWriter output) { output.WriteUInt24(Offset); } - public static bool IsValid(OPCode op) + public static bool IsValid(OPCode op) => op switch { - switch (op) - { - case OPCode.IfEq: - case OPCode.IfGe: - case OPCode.IfGt: - case OPCode.IfLe: - case OPCode.IfLt: - case OPCode.Jump: - case OPCode.IfNe: - case OPCode.IfNGe: - case OPCode.IfNGt: - case OPCode.IfNLe: - case OPCode.IfNLt: - case OPCode.IfTrue: - case OPCode.IfFalse: - case OPCode.IfStrictEq: - case OPCode.IfStrictNE: - return true; + OPCode.IfEq or + OPCode.IfGe or + OPCode.IfGt or + OPCode.IfLe or + OPCode.IfLt or + OPCode.Jump or + OPCode.IfNe or + OPCode.IfNGe or + OPCode.IfNGt or + OPCode.IfNLe or + OPCode.IfNLt or + OPCode.IfTrue or + OPCode.IfFalse or + OPCode.IfStrictEq or + OPCode.IfStrictNE => true, - default: return false; - } - } + _ => false, + }; } \ No newline at end of file diff --git a/Flazzy/ABC/AVM2/Instructions/DebugFileIns.cs b/Flazzy/ABC/AVM2/Instructions/DebugFileIns.cs index 5ccde02..fa72e9f 100644 --- a/Flazzy/ABC/AVM2/Instructions/DebugFileIns.cs +++ b/Flazzy/ABC/AVM2/Instructions/DebugFileIns.cs @@ -2,7 +2,7 @@ namespace Flazzy.ABC.AVM2.Instructions; -public class DebugFileIns : ASInstruction +public sealed class DebugFileIns : ASInstruction { public int FileNameIndex { get; set; } public string FileName => ABC.Pool.Strings[FileNameIndex]; @@ -10,10 +10,10 @@ public class DebugFileIns : ASInstruction public DebugFileIns(ABCFile abc) : base(OPCode.DebugFile, abc) { } - public DebugFileIns(ABCFile abc, FlashReader input) + public DebugFileIns(ABCFile abc, ref SpanFlashReader input) : this(abc) { - FileNameIndex = input.ReadInt30(); + FileNameIndex = input.ReadEncodedInt(); } public DebugFileIns(ABCFile abc, int fileNameIndex) : this(abc) @@ -21,8 +21,12 @@ public DebugFileIns(ABCFile abc, int fileNameIndex) FileNameIndex = fileNameIndex; } - protected override void WriteValuesTo(FlashWriter output) + protected override int GetBodySize() { - output.WriteInt30(FileNameIndex); + return SpanFlashWriter.GetEncodedIntSize(FileNameIndex); + } + protected override void WriteValuesTo(ref SpanFlashWriter output) + { + output.WriteEncodedInt(FileNameIndex); } } \ No newline at end of file diff --git a/Flazzy/ABC/AVM2/Instructions/DebugIns.cs b/Flazzy/ABC/AVM2/Instructions/DebugIns.cs index f1d739e..7cac16d 100644 --- a/Flazzy/ABC/AVM2/Instructions/DebugIns.cs +++ b/Flazzy/ABC/AVM2/Instructions/DebugIns.cs @@ -2,7 +2,7 @@ namespace Flazzy.ABC.AVM2.Instructions; -public class DebugIns : ASInstruction +public sealed class DebugIns : ASInstruction { public int NameIndex { get; set; } public string Name => ABC.Pool.Strings[NameIndex]; @@ -14,13 +14,13 @@ public class DebugIns : ASInstruction public DebugIns(ABCFile abc) : base(OPCode.Debug, abc) { } - public DebugIns(ABCFile abc, FlashReader input) + public DebugIns(ABCFile abc, ref SpanFlashReader input) : this(abc) { DebugType = input.ReadByte(); - NameIndex = input.ReadInt30(); + NameIndex = input.ReadEncodedInt(); RegisterIndex = input.ReadByte(); - Extra = input.ReadInt30(); + Extra = input.ReadEncodedInt(); } public DebugIns(ABCFile abc, int nameIndex, byte debugType, byte registerIndex) : this(abc) @@ -30,11 +30,20 @@ public DebugIns(ABCFile abc, int nameIndex, byte debugType, byte registerIndex) RegisterIndex = registerIndex; } - protected override void WriteValuesTo(FlashWriter output) + protected override int GetBodySize() + { + int size = 0; + size += sizeof(byte); + size += SpanFlashWriter.GetEncodedIntSize(NameIndex); + size += sizeof(byte); + size += SpanFlashWriter.GetEncodedIntSize(Extra); + return size; + } + protected override void WriteValuesTo(ref SpanFlashWriter output) { output.Write(DebugType); - output.WriteInt30(NameIndex); + output.WriteEncodedInt(NameIndex); output.Write(RegisterIndex); - output.WriteInt30(Extra); + output.WriteEncodedInt(Extra); } } \ No newline at end of file diff --git a/Flazzy/ABC/AVM2/Instructions/DebugLineIns.cs b/Flazzy/ABC/AVM2/Instructions/DebugLineIns.cs index 8f5cc39..e7ec8d7 100644 --- a/Flazzy/ABC/AVM2/Instructions/DebugLineIns.cs +++ b/Flazzy/ABC/AVM2/Instructions/DebugLineIns.cs @@ -2,7 +2,7 @@ namespace Flazzy.ABC.AVM2.Instructions; -public class DebugLineIns : ASInstruction +public sealed class DebugLineIns : ASInstruction { public int LineNumber { get; set; } @@ -14,14 +14,18 @@ public DebugLineIns(int lineNumber) { LineNumber = lineNumber; } - public DebugLineIns(FlashReader input) + public DebugLineIns(ref SpanFlashReader input) : this() { - LineNumber = input.ReadInt30(); + LineNumber = input.ReadEncodedInt(); } - protected override void WriteValuesTo(FlashWriter output) + protected override int GetBodySize() { - output.WriteInt30(LineNumber); + return SpanFlashWriter.GetEncodedIntSize(LineNumber); + } + protected override void WriteValuesTo(ref SpanFlashWriter output) + { + output.WriteEncodedInt(LineNumber); } } \ No newline at end of file diff --git a/Flazzy/ABC/AVM2/Instructions/DeletePropertyIns.cs b/Flazzy/ABC/AVM2/Instructions/DeletePropertyIns.cs index 802b9dd..6aa0728 100644 --- a/Flazzy/ABC/AVM2/Instructions/DeletePropertyIns.cs +++ b/Flazzy/ABC/AVM2/Instructions/DeletePropertyIns.cs @@ -2,7 +2,7 @@ namespace Flazzy.ABC.AVM2.Instructions; -public class DeletePropertyIns : ASInstruction +public sealed class DeletePropertyIns : ASInstruction { public int PropertyNameIndex { get; set; } public ASMultiname PropertyName => ABC.Pool.Multinames[PropertyNameIndex]; @@ -10,10 +10,10 @@ public class DeletePropertyIns : ASInstruction public DeletePropertyIns(ABCFile abc) : base(OPCode.DeleteProperty, abc) { } - public DeletePropertyIns(ABCFile abc, FlashReader input) + public DeletePropertyIns(ABCFile abc, ref SpanFlashReader input) : this(abc) { - PropertyNameIndex = input.ReadInt30(); + PropertyNameIndex = input.ReadEncodedInt(); } public DeletePropertyIns(ABCFile abc, int propertyNameIndex) : this(abc) @@ -23,12 +23,9 @@ public DeletePropertyIns(ABCFile abc, int propertyNameIndex) public override int GetPopCount() { - return (ResolveMultinamePops(PropertyName) + 1); - } - public override int GetPushCount() - { - return 1; + return ResolveMultinamePops(PropertyName) + 1; } + public override int GetPushCount() => 1; public override void Execute(ASMachine machine) { ResolveMultiname(machine, PropertyName); @@ -36,8 +33,12 @@ public override void Execute(ASMachine machine) machine.Values.Push(null); } - protected override void WriteValuesTo(FlashWriter output) + protected override int GetBodySize() + { + return SpanFlashWriter.GetEncodedIntSize(PropertyNameIndex); + } + protected override void WriteValuesTo(ref SpanFlashWriter output) { - output.WriteInt30(PropertyNameIndex); + output.WriteEncodedInt(PropertyNameIndex); } } \ No newline at end of file diff --git a/Flazzy/ABC/AVM2/Instructions/DupIns.cs b/Flazzy/ABC/AVM2/Instructions/DupIns.cs index c34fb0c..e061d10 100644 --- a/Flazzy/ABC/AVM2/Instructions/DupIns.cs +++ b/Flazzy/ABC/AVM2/Instructions/DupIns.cs @@ -1,15 +1,12 @@ namespace Flazzy.ABC.AVM2.Instructions; -public class DupIns : ASInstruction +public sealed class DupIns : ASInstruction { public DupIns() : base(OPCode.Dup) { } - public override int GetPushCount() - { - return 1; - } + public override int GetPushCount() => 1; public override void Execute(ASMachine machine) { object value = machine.Values.Peek(); diff --git a/Flazzy/ABC/AVM2/Instructions/DxnsIns.cs b/Flazzy/ABC/AVM2/Instructions/DxnsIns.cs index 6f61b8c..b5f26c6 100644 --- a/Flazzy/ABC/AVM2/Instructions/DxnsIns.cs +++ b/Flazzy/ABC/AVM2/Instructions/DxnsIns.cs @@ -2,7 +2,7 @@ namespace Flazzy.ABC.AVM2.Instructions; -public class DxnsIns : ASInstruction +public sealed class DxnsIns : ASInstruction { public int UriIndex { get; set; } public string Uri => ABC.Pool.Strings[UriIndex]; @@ -10,9 +10,18 @@ public class DxnsIns : ASInstruction public DxnsIns(ABCFile abc) : base(OPCode.Dxns, abc) { } - public DxnsIns(ABCFile abc, FlashReader input) + public DxnsIns(ABCFile abc, ref SpanFlashReader input) : this(abc) { - UriIndex = input.ReadInt30(); + UriIndex = input.ReadEncodedInt(); + } + + protected override int GetBodySize() + { + return SpanFlashWriter.GetEncodedIntSize(UriIndex); + } + protected override void WriteValuesTo(ref SpanFlashWriter output) + { + output.WriteEncodedInt(UriIndex); } } \ No newline at end of file diff --git a/Flazzy/ABC/AVM2/Instructions/DxnsLateIns.cs b/Flazzy/ABC/AVM2/Instructions/DxnsLateIns.cs index b6692e7..cb3b1f4 100644 --- a/Flazzy/ABC/AVM2/Instructions/DxnsLateIns.cs +++ b/Flazzy/ABC/AVM2/Instructions/DxnsLateIns.cs @@ -1,15 +1,12 @@ namespace Flazzy.ABC.AVM2.Instructions; -public class DxnsLateIns : ASInstruction +public sealed class DxnsLateIns : ASInstruction { public DxnsLateIns() : base(OPCode.DxnsLate) { } - public override int GetPopCount() - { - return 1; - } + public override int GetPopCount() => 1; public override void Execute(ASMachine machine) { object value = machine.Values.Pop(); diff --git a/Flazzy/ABC/AVM2/Instructions/Enums/OPCode.cs b/Flazzy/ABC/AVM2/Instructions/Enums/OPCode.cs index 47f4e58..d3ef3f2 100644 --- a/Flazzy/ABC/AVM2/Instructions/Enums/OPCode.cs +++ b/Flazzy/ABC/AVM2/Instructions/Enums/OPCode.cs @@ -1,6 +1,6 @@ namespace Flazzy.ABC.AVM2.Instructions; -public enum OPCode +public enum OPCode : byte { /// /// Add two values. diff --git a/Flazzy/ABC/AVM2/Instructions/EscXAttrIns.cs b/Flazzy/ABC/AVM2/Instructions/EscXAttrIns.cs index 1ded94d..19a3a5e 100644 --- a/Flazzy/ABC/AVM2/Instructions/EscXAttrIns.cs +++ b/Flazzy/ABC/AVM2/Instructions/EscXAttrIns.cs @@ -1,19 +1,13 @@ namespace Flazzy.ABC.AVM2.Instructions; -public class EscXAttrIns : ASInstruction +public sealed class EscXAttrIns : ASInstruction { public EscXAttrIns() : base(OPCode.Esc_XAttr) { } - public override int GetPopCount() - { - return 1; - } - public override int GetPushCount() - { - return 1; - } + public override int GetPopCount() => 1; + public override int GetPushCount() => 1; public override void Execute(ASMachine machine) { object result = null; diff --git a/Flazzy/ABC/AVM2/Instructions/EscXElemIns.cs b/Flazzy/ABC/AVM2/Instructions/EscXElemIns.cs index f7e3511..26f8bc1 100644 --- a/Flazzy/ABC/AVM2/Instructions/EscXElemIns.cs +++ b/Flazzy/ABC/AVM2/Instructions/EscXElemIns.cs @@ -1,19 +1,13 @@ namespace Flazzy.ABC.AVM2.Instructions; -public class EscXElemIns : ASInstruction +public sealed class EscXElemIns : ASInstruction { public EscXElemIns() : base(OPCode.Esc_XElem) { } - public override int GetPopCount() - { - return 1; - } - public override int GetPushCount() - { - return 1; - } + public override int GetPopCount() => 1; + public override int GetPushCount() => 1; public override void Execute(ASMachine machine) { object value = machine.Values.Pop(); diff --git a/Flazzy/ABC/AVM2/Instructions/FindPropStrictIns.cs b/Flazzy/ABC/AVM2/Instructions/FindPropStrictIns.cs index 3e61325..e57ab51 100644 --- a/Flazzy/ABC/AVM2/Instructions/FindPropStrictIns.cs +++ b/Flazzy/ABC/AVM2/Instructions/FindPropStrictIns.cs @@ -2,7 +2,7 @@ namespace Flazzy.ABC.AVM2.Instructions; -public class FindPropStrictIns : ASInstruction +public sealed class FindPropStrictIns : ASInstruction { public int PropertyNameIndex { get; set; } public ASMultiname PropertyName => ABC.Pool.Multinames[PropertyNameIndex]; @@ -10,10 +10,10 @@ public class FindPropStrictIns : ASInstruction public FindPropStrictIns(ABCFile abc) : base(OPCode.FindPropStrict, abc) { } - public FindPropStrictIns(ABCFile abc, FlashReader input) + public FindPropStrictIns(ABCFile abc, ref SpanFlashReader input) : this(abc) { - PropertyNameIndex = input.ReadInt30(); + PropertyNameIndex = input.ReadEncodedInt(); } public FindPropStrictIns(ABCFile abc, int propertyNameIndex) : this(abc) @@ -25,18 +25,19 @@ public override int GetPopCount() { return ResolveMultinamePops(PropertyName); } - public override int GetPushCount() - { - return 1; - } + public override int GetPushCount() => 1; public override void Execute(ASMachine machine) { ResolveMultiname(machine, PropertyName); machine.Values.Push(null); } - protected override void WriteValuesTo(FlashWriter output) + protected override int GetBodySize() + { + return SpanFlashWriter.GetEncodedIntSize(PropertyNameIndex); + } + protected override void WriteValuesTo(ref SpanFlashWriter output) { - output.WriteInt30(PropertyNameIndex); + output.WriteEncodedInt(PropertyNameIndex); } } \ No newline at end of file diff --git a/Flazzy/ABC/AVM2/Instructions/FindPropertyIns.cs b/Flazzy/ABC/AVM2/Instructions/FindPropertyIns.cs index 98b7ede..e1f3d00 100644 --- a/Flazzy/ABC/AVM2/Instructions/FindPropertyIns.cs +++ b/Flazzy/ABC/AVM2/Instructions/FindPropertyIns.cs @@ -2,7 +2,7 @@ namespace Flazzy.ABC.AVM2.Instructions; -public class FindPropertyIns : ASInstruction +public sealed class FindPropertyIns : ASInstruction { public int PropertyNameIndex { get; set; } public ASMultiname PropertyName => ABC.Pool.Multinames[PropertyNameIndex]; @@ -10,10 +10,10 @@ public class FindPropertyIns : ASInstruction public FindPropertyIns(ABCFile abc) : base(OPCode.FindProperty, abc) { } - public FindPropertyIns(ABCFile abc, FlashReader input) + public FindPropertyIns(ABCFile abc, ref SpanFlashReader input) : this(abc) { - PropertyNameIndex = input.ReadInt30(); + PropertyNameIndex = input.ReadEncodedInt(); } public FindPropertyIns(ABCFile abc, int propertyNameIndex) : this(abc) @@ -25,18 +25,19 @@ public override int GetPopCount() { return ResolveMultinamePops(PropertyName); } - public override int GetPushCount() - { - return 1; - } + public override int GetPushCount() => 1; public override void Execute(ASMachine machine) { ResolveMultiname(machine, PropertyName); machine.Values.Push(null); } - protected override void WriteValuesTo(FlashWriter output) + protected override int GetBodySize() + { + return SpanFlashWriter.GetEncodedIntSize(PropertyNameIndex); + } + protected override void WriteValuesTo(ref SpanFlashWriter output) { - output.WriteInt30(PropertyNameIndex); + output.WriteEncodedInt(PropertyNameIndex); } } \ No newline at end of file diff --git a/Flazzy/ABC/AVM2/Instructions/GetDescendantsIns.cs b/Flazzy/ABC/AVM2/Instructions/GetDescendantsIns.cs index a096e8b..de1a46f 100644 --- a/Flazzy/ABC/AVM2/Instructions/GetDescendantsIns.cs +++ b/Flazzy/ABC/AVM2/Instructions/GetDescendantsIns.cs @@ -2,7 +2,7 @@ namespace Flazzy.ABC.AVM2.Instructions; -public class GetDescendantsIns : ASInstruction +public sealed class GetDescendantsIns : ASInstruction { public int DescendantIndex { get; set; } public ASMultiname Descendant => ABC.Pool.Multinames[DescendantIndex]; @@ -10,10 +10,10 @@ public class GetDescendantsIns : ASInstruction public GetDescendantsIns(ABCFile abc) : base(OPCode.GetDescendants, abc) { } - public GetDescendantsIns(ABCFile abc, FlashReader input) + public GetDescendantsIns(ABCFile abc, ref SpanFlashReader input) : this(abc) { - DescendantIndex = input.ReadInt30(); + DescendantIndex = input.ReadEncodedInt(); } public GetDescendantsIns(ABCFile abc, int descendantIndex) : this(abc) @@ -23,12 +23,9 @@ public GetDescendantsIns(ABCFile abc, int descendantIndex) public override int GetPopCount() { - return (ResolveMultinamePops(Descendant) + 1); - } - public override int GetPushCount() - { - return 1; + return ResolveMultinamePops(Descendant) + 1; } + public override int GetPushCount() => 1; public override void Execute(ASMachine machine) { ResolveMultiname(machine, Descendant); @@ -36,8 +33,12 @@ public override void Execute(ASMachine machine) machine.Values.Push(null); } - protected override void WriteValuesTo(FlashWriter output) + protected override int GetBodySize() + { + return SpanFlashWriter.GetEncodedIntSize(DescendantIndex); + } + protected override void WriteValuesTo(ref SpanFlashWriter output) { - output.WriteInt30(DescendantIndex); + output.WriteEncodedInt(DescendantIndex); } } \ No newline at end of file diff --git a/Flazzy/ABC/AVM2/Instructions/GetGlobalScopeIns.cs b/Flazzy/ABC/AVM2/Instructions/GetGlobalScopeIns.cs index 8d6106a..9a718fe 100644 --- a/Flazzy/ABC/AVM2/Instructions/GetGlobalScopeIns.cs +++ b/Flazzy/ABC/AVM2/Instructions/GetGlobalScopeIns.cs @@ -1,15 +1,12 @@ namespace Flazzy.ABC.AVM2.Instructions; -public class GetGlobalScopeIns : ASInstruction +public sealed class GetGlobalScopeIns : ASInstruction { public GetGlobalScopeIns() : base(OPCode.GetGlobalScope) { } - public override int GetPushCount() - { - return 1; - } + public override int GetPushCount() => 1; public override void Execute(ASMachine machine) { machine.Values.Push(null); diff --git a/Flazzy/ABC/AVM2/Instructions/GetLexIns.cs b/Flazzy/ABC/AVM2/Instructions/GetLexIns.cs index decb786..6ded917 100644 --- a/Flazzy/ABC/AVM2/Instructions/GetLexIns.cs +++ b/Flazzy/ABC/AVM2/Instructions/GetLexIns.cs @@ -2,7 +2,7 @@ namespace Flazzy.ABC.AVM2.Instructions; -public class GetLexIns : ASInstruction +public sealed class GetLexIns : ASInstruction { public int TypeNameIndex { get; set; } public ASMultiname TypeName => ABC.Pool.Multinames[TypeNameIndex]; @@ -10,10 +10,10 @@ public class GetLexIns : ASInstruction public GetLexIns(ABCFile abc) : base(OPCode.GetLex, abc) { } - public GetLexIns(ABCFile abc, FlashReader input) + public GetLexIns(ABCFile abc, ref SpanFlashReader input) : this(abc) { - TypeNameIndex = input.ReadInt30(); + TypeNameIndex = input.ReadEncodedInt(); } public GetLexIns(ABCFile abc, int typeNameIndex) : this(abc) @@ -21,17 +21,18 @@ public GetLexIns(ABCFile abc, int typeNameIndex) TypeNameIndex = typeNameIndex; } - public override int GetPushCount() - { - return 1; - } + public override int GetPushCount() => 1; public override void Execute(ASMachine machine) { machine.Values.Push(null); } - protected override void WriteValuesTo(FlashWriter output) + protected override int GetBodySize() + { + return SpanFlashWriter.GetEncodedIntSize(TypeNameIndex); + } + protected override void WriteValuesTo(ref SpanFlashWriter output) { - output.WriteInt30(TypeNameIndex); + output.WriteEncodedInt(TypeNameIndex); } } \ No newline at end of file diff --git a/Flazzy/ABC/AVM2/Instructions/GetPropertyIns.cs b/Flazzy/ABC/AVM2/Instructions/GetPropertyIns.cs index d74b8fd..e50886f 100644 --- a/Flazzy/ABC/AVM2/Instructions/GetPropertyIns.cs +++ b/Flazzy/ABC/AVM2/Instructions/GetPropertyIns.cs @@ -2,7 +2,7 @@ namespace Flazzy.ABC.AVM2.Instructions; -public class GetPropertyIns : ASInstruction +public sealed class GetPropertyIns : ASInstruction { public int PropertyNameIndex { get; set; } public ASMultiname PropertyName => ABC.Pool.Multinames[PropertyNameIndex]; @@ -10,10 +10,10 @@ public class GetPropertyIns : ASInstruction public GetPropertyIns(ABCFile abc) : base(OPCode.GetProperty, abc) { } - public GetPropertyIns(ABCFile abc, FlashReader input) + public GetPropertyIns(ABCFile abc, ref SpanFlashReader input) : this(abc) { - PropertyNameIndex = input.ReadInt30(); + PropertyNameIndex = input.ReadEncodedInt(); } public GetPropertyIns(ABCFile abc, int propertyNameIndex) : this(abc) @@ -23,12 +23,9 @@ public GetPropertyIns(ABCFile abc, int propertyNameIndex) public override int GetPopCount() { - return (ResolveMultinamePops(PropertyName) + 1); - } - public override int GetPushCount() - { - return 1; + return ResolveMultinamePops(PropertyName) + 1; } + public override int GetPushCount() => 1; public override void Execute(ASMachine machine) { ResolveMultiname(machine, PropertyName); @@ -36,8 +33,12 @@ public override void Execute(ASMachine machine) machine.Values.Push(null); } - protected override void WriteValuesTo(FlashWriter output) + protected override int GetBodySize() + { + return SpanFlashWriter.GetEncodedIntSize(PropertyNameIndex); + } + protected override void WriteValuesTo(ref SpanFlashWriter output) { - output.WriteInt30(PropertyNameIndex); + output.WriteEncodedInt(PropertyNameIndex); } } \ No newline at end of file diff --git a/Flazzy/ABC/AVM2/Instructions/GetScopeObjectIns.cs b/Flazzy/ABC/AVM2/Instructions/GetScopeObjectIns.cs index 73479b5..1f2a6f9 100644 --- a/Flazzy/ABC/AVM2/Instructions/GetScopeObjectIns.cs +++ b/Flazzy/ABC/AVM2/Instructions/GetScopeObjectIns.cs @@ -2,7 +2,7 @@ namespace Flazzy.ABC.AVM2.Instructions; -public class GetScopeObjectIns : ASInstruction +public sealed class GetScopeObjectIns : ASInstruction { public byte ScopeIndex { get; set; } @@ -14,22 +14,23 @@ public GetScopeObjectIns(byte scopeIndex) { ScopeIndex = scopeIndex; } - public GetScopeObjectIns(FlashReader input) + public GetScopeObjectIns(ref SpanFlashReader input) : this() { ScopeIndex = input.ReadByte(); } - public override int GetPushCount() - { - return 1; - } + public override int GetPushCount() => 1; public override void Execute(ASMachine machine) { machine.Values.Push(null); } - protected override void WriteValuesTo(FlashWriter output) + protected override int GetBodySize() + { + return SpanFlashWriter.GetEncodedIntSize(ScopeIndex); + } + protected override void WriteValuesTo(ref SpanFlashWriter output) { output.Write(ScopeIndex); } diff --git a/Flazzy/ABC/AVM2/Instructions/GetSlotIns.cs b/Flazzy/ABC/AVM2/Instructions/GetSlotIns.cs index eef8169..a5c1c1b 100644 --- a/Flazzy/ABC/AVM2/Instructions/GetSlotIns.cs +++ b/Flazzy/ABC/AVM2/Instructions/GetSlotIns.cs @@ -2,7 +2,7 @@ namespace Flazzy.ABC.AVM2.Instructions; -public class GetSlotIns : ASInstruction +public sealed class GetSlotIns : ASInstruction { public int SlotIndex { get; set; } @@ -14,28 +14,26 @@ public GetSlotIns(int slotIndex) { SlotIndex = slotIndex; } - public GetSlotIns(FlashReader input) + public GetSlotIns(ref SpanFlashReader input) : this() { - SlotIndex = input.ReadInt30(); + SlotIndex = input.ReadEncodedInt(); } - public override int GetPopCount() - { - return 1; - } - public override int GetPushCount() - { - return 1; - } + public override int GetPopCount() => 1; + public override int GetPushCount() => 1; public override void Execute(ASMachine machine) { object obj = machine.Values.Pop(); machine.Values.Push(null); } - protected override void WriteValuesTo(FlashWriter output) + protected override int GetBodySize() + { + return SpanFlashWriter.GetEncodedIntSize(SlotIndex); + } + protected override void WriteValuesTo(ref SpanFlashWriter output) { - output.WriteInt30(SlotIndex); + output.WriteEncodedInt(SlotIndex); } } \ No newline at end of file diff --git a/Flazzy/ABC/AVM2/Instructions/GetSuperIns.cs b/Flazzy/ABC/AVM2/Instructions/GetSuperIns.cs index 2f3a124..ab443f0 100644 --- a/Flazzy/ABC/AVM2/Instructions/GetSuperIns.cs +++ b/Flazzy/ABC/AVM2/Instructions/GetSuperIns.cs @@ -2,7 +2,7 @@ namespace Flazzy.ABC.AVM2.Instructions; -public class GetSuperIns : ASInstruction +public sealed class GetSuperIns : ASInstruction { public int PropertyNameIndex { get; set; } public ASMultiname PropertyName => ABC.Pool.Multinames[PropertyNameIndex]; @@ -10,10 +10,10 @@ public class GetSuperIns : ASInstruction public GetSuperIns(ABCFile abc) : base(OPCode.GetSuper, abc) { } - public GetSuperIns(ABCFile abc, FlashReader input) + public GetSuperIns(ABCFile abc, ref SpanFlashReader input) : this(abc) { - PropertyNameIndex = input.ReadInt30(); + PropertyNameIndex = input.ReadEncodedInt(); } public GetSuperIns(ABCFile abc, int propertyNameIndex) : this(abc) @@ -23,12 +23,9 @@ public GetSuperIns(ABCFile abc, int propertyNameIndex) public override int GetPopCount() { - return (ResolveMultinamePops(PropertyName) + 1); - } - public override int GetPushCount() - { - return 1; + return ResolveMultinamePops(PropertyName) + 1; } + public override int GetPushCount() => 1; public override void Execute(ASMachine machine) { ResolveMultiname(machine, PropertyName); @@ -36,8 +33,12 @@ public override void Execute(ASMachine machine) machine.Values.Push(null); } - protected override void WriteValuesTo(FlashWriter output) + protected override int GetBodySize() + { + return SpanFlashWriter.GetEncodedIntSize(PropertyNameIndex); + } + protected override void WriteValuesTo(ref SpanFlashWriter output) { - output.WriteInt30(PropertyNameIndex); + output.WriteEncodedInt(PropertyNameIndex); } } \ No newline at end of file diff --git a/Flazzy/ABC/AVM2/Instructions/HasNext2Ins.cs b/Flazzy/ABC/AVM2/Instructions/HasNext2Ins.cs index 1b0852e..ad5d2b6 100644 --- a/Flazzy/ABC/AVM2/Instructions/HasNext2Ins.cs +++ b/Flazzy/ABC/AVM2/Instructions/HasNext2Ins.cs @@ -2,7 +2,7 @@ namespace Flazzy.ABC.AVM2.Instructions; -public class HasNext2Ins : ASInstruction +public sealed class HasNext2Ins : ASInstruction { public int ObjectIndex { get; set; } public int RegisterIndex { get; set; } @@ -10,11 +10,11 @@ public class HasNext2Ins : ASInstruction public HasNext2Ins() : base(OPCode.HasNext2) { } - public HasNext2Ins(FlashReader input) + public HasNext2Ins(ref SpanFlashReader input) : this() { - ObjectIndex = input.ReadInt30(); - RegisterIndex = input.ReadInt30(); + ObjectIndex = input.ReadEncodedInt(); + RegisterIndex = input.ReadEncodedInt(); } public HasNext2Ins(int objectIndex, int registerIndex) : this() @@ -23,18 +23,22 @@ public HasNext2Ins(int objectIndex, int registerIndex) RegisterIndex = registerIndex; } - public override int GetPushCount() - { - return 1; - } + public override int GetPushCount() => 1; public override void Execute(ASMachine machine) { machine.Values.Push(null); } - protected override void WriteValuesTo(FlashWriter output) + protected override int GetBodySize() + { + int size = 0; + size += SpanFlashWriter.GetEncodedIntSize(ObjectIndex); + size += SpanFlashWriter.GetEncodedIntSize(RegisterIndex); + return size; + } + protected override void WriteValuesTo(ref SpanFlashWriter output) { - output.WriteInt30(ObjectIndex); - output.WriteInt30(RegisterIndex); + output.WriteEncodedInt(ObjectIndex); + output.WriteEncodedInt(RegisterIndex); } } \ No newline at end of file diff --git a/Flazzy/ABC/AVM2/Instructions/HasNextIns.cs b/Flazzy/ABC/AVM2/Instructions/HasNextIns.cs index 760b1ec..240283e 100644 --- a/Flazzy/ABC/AVM2/Instructions/HasNextIns.cs +++ b/Flazzy/ABC/AVM2/Instructions/HasNextIns.cs @@ -1,19 +1,13 @@ namespace Flazzy.ABC.AVM2.Instructions; -public class HasNextIns : ASInstruction +public sealed class HasNextIns : ASInstruction { public HasNextIns() : base(OPCode.HasNext) { } - public override int GetPopCount() - { - return 2; - } - public override int GetPushCount() - { - return 1; - } + public override int GetPopCount() => 2; + public override int GetPushCount() => 1; public override void Execute(ASMachine machine) { object curIndex = machine.Values.Pop(); diff --git a/Flazzy/ABC/AVM2/Instructions/InitPropertyIns.cs b/Flazzy/ABC/AVM2/Instructions/InitPropertyIns.cs index 992490c..fada315 100644 --- a/Flazzy/ABC/AVM2/Instructions/InitPropertyIns.cs +++ b/Flazzy/ABC/AVM2/Instructions/InitPropertyIns.cs @@ -2,7 +2,7 @@ namespace Flazzy.ABC.AVM2.Instructions; -public class InitPropertyIns : ASInstruction +public sealed class InitPropertyIns : ASInstruction { public int PropertyNameIndex { get; set; } public ASMultiname PropertyName => ABC.Pool.Multinames[PropertyNameIndex]; @@ -10,10 +10,10 @@ public class InitPropertyIns : ASInstruction public InitPropertyIns(ABCFile abc) : base(OPCode.InitProperty, abc) { } - public InitPropertyIns(ABCFile abc, FlashReader input) + public InitPropertyIns(ABCFile abc, ref SpanFlashReader input) : this(abc) { - PropertyNameIndex = input.ReadInt30(); + PropertyNameIndex = input.ReadEncodedInt(); } public InitPropertyIns(ABCFile abc, int propertyNameIndex) : this(abc) @@ -23,7 +23,7 @@ public InitPropertyIns(ABCFile abc, int propertyNameIndex) public override int GetPopCount() { - return (1 + ResolveMultinamePops(PropertyName) + 1); + return 1 + ResolveMultinamePops(PropertyName) + 1; } public override void Execute(ASMachine machine) { @@ -32,8 +32,12 @@ public override void Execute(ASMachine machine) object obj = machine.Values.Pop(); } - protected override void WriteValuesTo(FlashWriter output) + protected override int GetBodySize() { - output.WriteInt30(PropertyNameIndex); + return SpanFlashWriter.GetEncodedIntSize(PropertyNameIndex); + } + protected override void WriteValuesTo(ref SpanFlashWriter output) + { + output.WriteEncodedInt(PropertyNameIndex); } } \ No newline at end of file diff --git a/Flazzy/ABC/AVM2/Instructions/InstanceOfIns.cs b/Flazzy/ABC/AVM2/Instructions/InstanceOfIns.cs index d1c89b8..5a8fec1 100644 --- a/Flazzy/ABC/AVM2/Instructions/InstanceOfIns.cs +++ b/Flazzy/ABC/AVM2/Instructions/InstanceOfIns.cs @@ -1,19 +1,13 @@ namespace Flazzy.ABC.AVM2.Instructions; -public class InstanceOfIns : ASInstruction +public sealed class InstanceOfIns : ASInstruction { public InstanceOfIns() : base(OPCode.InstanceOf) { } - public override int GetPopCount() - { - return 2; - } - public override int GetPushCount() - { - return 1; - } + public override int GetPopCount() => 2; + public override int GetPushCount() => 1; public override void Execute(ASMachine machine) { object type = machine.Values.Pop(); diff --git a/Flazzy/ABC/AVM2/Instructions/LabelIns.cs b/Flazzy/ABC/AVM2/Instructions/LabelIns.cs index 417d892..dca1663 100644 --- a/Flazzy/ABC/AVM2/Instructions/LabelIns.cs +++ b/Flazzy/ABC/AVM2/Instructions/LabelIns.cs @@ -1,6 +1,6 @@ namespace Flazzy.ABC.AVM2.Instructions; -public class LabelIns : ASInstruction +public sealed class LabelIns : ASInstruction { public LabelIns() : base(OPCode.Label) diff --git a/Flazzy/ABC/AVM2/Instructions/LookUpSwitchIns.cs b/Flazzy/ABC/AVM2/Instructions/LookUpSwitchIns.cs index ab2021b..873a087 100644 --- a/Flazzy/ABC/AVM2/Instructions/LookUpSwitchIns.cs +++ b/Flazzy/ABC/AVM2/Instructions/LookUpSwitchIns.cs @@ -2,7 +2,7 @@ namespace Flazzy.ABC.AVM2.Instructions; -public class LookUpSwitchIns : ASInstruction +public sealed class LookUpSwitchIns : ASInstruction { public List CaseOffsets { get; } public uint DefaultOffset { get; set; } @@ -12,11 +12,11 @@ public LookUpSwitchIns() { CaseOffsets = new List(); } - public LookUpSwitchIns(FlashReader input) + public LookUpSwitchIns(ref SpanFlashReader input) : this() { DefaultOffset = input.ReadUInt24(); - CaseOffsets.Capacity = (input.ReadInt30() + 1); + CaseOffsets.Capacity = input.ReadEncodedInt() + 1; for (int i = 0; i < CaseOffsets.Capacity; i++) { CaseOffsets.Add(input.ReadUInt24()); @@ -31,23 +31,27 @@ public LookUpSwitchIns(uint defaultOffset, params uint[] caseOffsets) CaseOffsets.Add(DefaultOffset); } - public override int GetPopCount() - { - return 1; - } + public override int GetPopCount() => 1; public override void Execute(ASMachine machine) { machine.Values.Pop(); } - protected override void WriteValuesTo(FlashWriter output) + protected override int GetBodySize() + { + int size = 0; + size += 3; + size += SpanFlashWriter.GetEncodedIntSize(CaseOffsets.Count - 1); + size += CaseOffsets.Count * 3; + return size; + } + protected override void WriteValuesTo(ref SpanFlashWriter output) { output.WriteUInt24(DefaultOffset); - output.WriteInt30(CaseOffsets.Count - 1); + output.WriteEncodedInt(CaseOffsets.Count - 1); for (int i = 0; i < CaseOffsets.Count; i++) { - uint offset = CaseOffsets[i]; - output.WriteUInt24(offset); + output.WriteUInt24(CaseOffsets[i]); } } } \ No newline at end of file diff --git a/Flazzy/ABC/AVM2/Instructions/NewActivationIns.cs b/Flazzy/ABC/AVM2/Instructions/NewActivationIns.cs index 3fe856a..fd38d5c 100644 --- a/Flazzy/ABC/AVM2/Instructions/NewActivationIns.cs +++ b/Flazzy/ABC/AVM2/Instructions/NewActivationIns.cs @@ -1,15 +1,12 @@ namespace Flazzy.ABC.AVM2.Instructions; -public class NewActivationIns : ASInstruction +public sealed class NewActivationIns : ASInstruction { public NewActivationIns() : base(OPCode.NewActivation) { } - public override int GetPushCount() - { - return 1; - } + public override int GetPushCount() => 1; public override void Execute(ASMachine machine) { machine.Values.Push(null); diff --git a/Flazzy/ABC/AVM2/Instructions/NewArrayIns.cs b/Flazzy/ABC/AVM2/Instructions/NewArrayIns.cs index 11ef92c..966af8a 100644 --- a/Flazzy/ABC/AVM2/Instructions/NewArrayIns.cs +++ b/Flazzy/ABC/AVM2/Instructions/NewArrayIns.cs @@ -2,7 +2,7 @@ namespace Flazzy.ABC.AVM2.Instructions; -public class NewArrayIns : ASInstruction +public sealed class NewArrayIns : ASInstruction { public int ArgCount { get; set; } @@ -14,20 +14,14 @@ public NewArrayIns(int argCount) { ArgCount = argCount; } - public NewArrayIns(FlashReader input) + public NewArrayIns(ref SpanFlashReader input) : this() { - ArgCount = input.ReadInt30(); + ArgCount = input.ReadEncodedInt(); } - public override int GetPopCount() - { - return ArgCount; - } - public override int GetPushCount() - { - return 1; - } + public override int GetPopCount() => ArgCount; + public override int GetPushCount() => 1; public override void Execute(ASMachine machine) { var newarray = new object[ArgCount]; @@ -38,8 +32,12 @@ public override void Execute(ASMachine machine) machine.Values.Push(newarray); } - protected override void WriteValuesTo(FlashWriter output) + protected override int GetBodySize() + { + return SpanFlashWriter.GetEncodedIntSize(ArgCount); + } + protected override void WriteValuesTo(ref SpanFlashWriter output) { - output.WriteInt30(ArgCount); + output.WriteEncodedInt(ArgCount); } } \ No newline at end of file diff --git a/Flazzy/ABC/AVM2/Instructions/NewCatchIns.cs b/Flazzy/ABC/AVM2/Instructions/NewCatchIns.cs index ef78203..e24aca6 100644 --- a/Flazzy/ABC/AVM2/Instructions/NewCatchIns.cs +++ b/Flazzy/ABC/AVM2/Instructions/NewCatchIns.cs @@ -2,17 +2,17 @@ namespace Flazzy.ABC.AVM2.Instructions; -public class NewCatchIns : ASInstruction +public sealed class NewCatchIns : ASInstruction { public int ExceptionIndex { get; set; } public NewCatchIns() : base(OPCode.NewCatch) { } - public NewCatchIns(FlashReader input) + public NewCatchIns(ref SpanFlashReader input) : this() { - ExceptionIndex = input.ReadInt30(); + ExceptionIndex = input.ReadEncodedInt(); } public NewCatchIns(int exceptionIndex) : this() @@ -20,17 +20,18 @@ public NewCatchIns(int exceptionIndex) ExceptionIndex = exceptionIndex; } - public override int GetPushCount() - { - return 1; - } + public override int GetPushCount() => 1; public override void Execute(ASMachine machine) { machine.Values.Push(null); } - protected override void WriteValuesTo(FlashWriter output) + protected override int GetBodySize() + { + return SpanFlashWriter.GetEncodedIntSize(ExceptionIndex); + } + protected override void WriteValuesTo(ref SpanFlashWriter output) { - output.WriteInt30(ExceptionIndex); + output.WriteEncodedInt(ExceptionIndex); } } \ No newline at end of file diff --git a/Flazzy/ABC/AVM2/Instructions/NewClassIns.cs b/Flazzy/ABC/AVM2/Instructions/NewClassIns.cs index e6860f1..89a9a92 100644 --- a/Flazzy/ABC/AVM2/Instructions/NewClassIns.cs +++ b/Flazzy/ABC/AVM2/Instructions/NewClassIns.cs @@ -2,7 +2,7 @@ namespace Flazzy.ABC.AVM2.Instructions; -public class NewClassIns : ASInstruction +public sealed class NewClassIns : ASInstruction { public int ClassIndex { get; set; } public ASClass Class => ABC.Classes[ClassIndex]; @@ -15,28 +15,26 @@ public NewClassIns(ABCFile abc, int classIndex) { ClassIndex = classIndex; } - public NewClassIns(ABCFile abc, FlashReader input) + public NewClassIns(ABCFile abc, ref SpanFlashReader input) : this(abc) { - ClassIndex = input.ReadInt30(); + ClassIndex = input.ReadEncodedInt(); } - public override int GetPopCount() - { - return 1; - } - public override int GetPushCount() - { - return 1; - } + public override int GetPopCount() => 1; + public override int GetPushCount() => 1; public override void Execute(ASMachine machine) { object baseType = machine.Values.Pop(); machine.Values.Push(null); } - protected override void WriteValuesTo(FlashWriter output) + protected override int GetBodySize() + { + return SpanFlashWriter.GetEncodedIntSize(ClassIndex); + } + protected override void WriteValuesTo(ref SpanFlashWriter output) { - output.WriteInt30(ClassIndex); + output.WriteEncodedInt(ClassIndex); } } \ No newline at end of file diff --git a/Flazzy/ABC/AVM2/Instructions/NewFunctionIns.cs b/Flazzy/ABC/AVM2/Instructions/NewFunctionIns.cs index 70f3d74..39d9f8e 100644 --- a/Flazzy/ABC/AVM2/Instructions/NewFunctionIns.cs +++ b/Flazzy/ABC/AVM2/Instructions/NewFunctionIns.cs @@ -2,7 +2,7 @@ namespace Flazzy.ABC.AVM2.Instructions; -public class NewFunctionIns : ASInstruction +public sealed class NewFunctionIns : ASInstruction { public int MethodIndex { get; set; } public ASMethod Method => ABC.Methods[MethodIndex]; @@ -15,23 +15,24 @@ public NewFunctionIns(ABCFile abc, int methodIndex) { MethodIndex = methodIndex; } - public NewFunctionIns(ABCFile abc, FlashReader input) + public NewFunctionIns(ABCFile abc, ref SpanFlashReader input) : this(abc) { - MethodIndex = input.ReadInt30(); + MethodIndex = input.ReadEncodedInt(); } - public override int GetPushCount() - { - return 1; - } + public override int GetPushCount() => 1; public override void Execute(ASMachine machine) { machine.Values.Push(null); } - protected override void WriteValuesTo(FlashWriter output) + protected override int GetBodySize() + { + return SpanFlashWriter.GetEncodedIntSize(MethodIndex); + } + protected override void WriteValuesTo(ref SpanFlashWriter output) { - output.WriteInt30(MethodIndex); + output.WriteEncodedInt(MethodIndex); } } \ No newline at end of file diff --git a/Flazzy/ABC/AVM2/Instructions/NewObjectIns.cs b/Flazzy/ABC/AVM2/Instructions/NewObjectIns.cs index 781f8c5..2068f3f 100644 --- a/Flazzy/ABC/AVM2/Instructions/NewObjectIns.cs +++ b/Flazzy/ABC/AVM2/Instructions/NewObjectIns.cs @@ -2,7 +2,7 @@ namespace Flazzy.ABC.AVM2.Instructions; -public class NewObjectIns : ASInstruction +public sealed class NewObjectIns : ASInstruction { public int ArgCount { get; set; } @@ -14,20 +14,14 @@ public NewObjectIns(int argCount) { ArgCount = argCount; } - public NewObjectIns(FlashReader input) + public NewObjectIns(ref SpanFlashReader input) : this() { - ArgCount = input.ReadInt30(); + ArgCount = input.ReadEncodedInt(); } - public override int GetPopCount() - { - return (ArgCount * 2); - } - public override int GetPushCount() - { - return 1; - } + public override int GetPopCount() => ArgCount * 2; + public override int GetPushCount() => 1; public override void Execute(ASMachine machine) { for (int i = 0; i < ArgCount; i++) @@ -38,8 +32,12 @@ public override void Execute(ASMachine machine) machine.Values.Push(null); } - protected override void WriteValuesTo(FlashWriter output) + protected override int GetBodySize() + { + return SpanFlashWriter.GetEncodedIntSize(ArgCount); + } + protected override void WriteValuesTo(ref SpanFlashWriter output) { - output.WriteInt30(ArgCount); + output.WriteEncodedInt(ArgCount); } } \ No newline at end of file diff --git a/Flazzy/ABC/AVM2/Instructions/NextNameIns.cs b/Flazzy/ABC/AVM2/Instructions/NextNameIns.cs index f66e878..4b6ad7c 100644 --- a/Flazzy/ABC/AVM2/Instructions/NextNameIns.cs +++ b/Flazzy/ABC/AVM2/Instructions/NextNameIns.cs @@ -1,19 +1,13 @@ namespace Flazzy.ABC.AVM2.Instructions; -public class NextNameIns : ASInstruction +public sealed class NextNameIns : ASInstruction { public NextNameIns() : base(OPCode.NextName) { } - public override int GetPopCount() - { - return 2; - } - public override int GetPushCount() - { - return 1; - } + public override int GetPopCount() => 2; + public override int GetPushCount() => 1; public override void Execute(ASMachine machine) { object index = machine.Values.Pop(); diff --git a/Flazzy/ABC/AVM2/Instructions/NextValueIns.cs b/Flazzy/ABC/AVM2/Instructions/NextValueIns.cs index a300fc0..2839ec9 100644 --- a/Flazzy/ABC/AVM2/Instructions/NextValueIns.cs +++ b/Flazzy/ABC/AVM2/Instructions/NextValueIns.cs @@ -1,19 +1,13 @@ namespace Flazzy.ABC.AVM2.Instructions; -public class NextValueIns : ASInstruction +public sealed class NextValueIns : ASInstruction { public NextValueIns() : base(OPCode.NextValue) { } - public override int GetPopCount() - { - return 2; - } - public override int GetPushCount() - { - return 1; - } + public override int GetPopCount() => 2; + public override int GetPushCount() => 1; public override void Execute(ASMachine machine) { object index = machine.Values.Pop(); diff --git a/Flazzy/ABC/AVM2/Instructions/NopIns.cs b/Flazzy/ABC/AVM2/Instructions/NopIns.cs index 52139ba..d608fb9 100644 --- a/Flazzy/ABC/AVM2/Instructions/NopIns.cs +++ b/Flazzy/ABC/AVM2/Instructions/NopIns.cs @@ -1,6 +1,6 @@ namespace Flazzy.ABC.AVM2.Instructions; -public class NopIns : ASInstruction +public sealed class NopIns : ASInstruction { public NopIns() : base(OPCode.Nop) diff --git a/Flazzy/ABC/AVM2/Instructions/NotIns.cs b/Flazzy/ABC/AVM2/Instructions/NotIns.cs index 5ca6dd5..1ec17a8 100644 --- a/Flazzy/ABC/AVM2/Instructions/NotIns.cs +++ b/Flazzy/ABC/AVM2/Instructions/NotIns.cs @@ -1,27 +1,21 @@ namespace Flazzy.ABC.AVM2.Instructions; -public class NotIns : ASInstruction +public sealed class NotIns : ASInstruction { public NotIns() : base(OPCode.Not) { } - public override int GetPopCount() - { - return 1; - } - public override int GetPushCount() - { - return 1; - } + public override int GetPopCount() => 1; + public override int GetPushCount() => 1; public override void Execute(ASMachine machine) { object value = machine.Values.Pop(); if (value != null) { - if (value is string) + if (value is string @string) { - value = !string.IsNullOrEmpty((string)value); + value = !string.IsNullOrEmpty(@string); } else value = !Convert.ToBoolean(value); } diff --git a/Flazzy/ABC/AVM2/Instructions/PopIns.cs b/Flazzy/ABC/AVM2/Instructions/PopIns.cs index f8e29f1..ddceb5d 100644 --- a/Flazzy/ABC/AVM2/Instructions/PopIns.cs +++ b/Flazzy/ABC/AVM2/Instructions/PopIns.cs @@ -1,15 +1,12 @@ namespace Flazzy.ABC.AVM2.Instructions; -public class PopIns : ASInstruction +public sealed class PopIns : ASInstruction { public PopIns() : base(OPCode.Pop) { } - public override int GetPopCount() - { - return 1; - } + public override int GetPopCount() => 1; public override void Execute(ASMachine machine) { machine.Values.Pop(); diff --git a/Flazzy/ABC/AVM2/Instructions/PopScopeIns.cs b/Flazzy/ABC/AVM2/Instructions/PopScopeIns.cs index eb921d5..6ef15ad 100644 --- a/Flazzy/ABC/AVM2/Instructions/PopScopeIns.cs +++ b/Flazzy/ABC/AVM2/Instructions/PopScopeIns.cs @@ -1,6 +1,6 @@ namespace Flazzy.ABC.AVM2.Instructions; -public class PopScopeIns : ASInstruction +public sealed class PopScopeIns : ASInstruction { public PopScopeIns() : base(OPCode.PopScope) diff --git a/Flazzy/ABC/AVM2/Instructions/PushScopeIns.cs b/Flazzy/ABC/AVM2/Instructions/PushScopeIns.cs index 5bd7e9c..1835330 100644 --- a/Flazzy/ABC/AVM2/Instructions/PushScopeIns.cs +++ b/Flazzy/ABC/AVM2/Instructions/PushScopeIns.cs @@ -1,15 +1,12 @@ namespace Flazzy.ABC.AVM2.Instructions; -public class PushScopeIns : ASInstruction +public sealed class PushScopeIns : ASInstruction { public PushScopeIns() : base(OPCode.PushScope) { } - public override int GetPopCount() - { - return 1; - } + public override int GetPopCount() => 1; public override void Execute(ASMachine machine) { object value = machine.Values.Pop(); diff --git a/Flazzy/ABC/AVM2/Instructions/PushUndefinedIns.cs b/Flazzy/ABC/AVM2/Instructions/PushUndefinedIns.cs index 53cc3ed..9926ce3 100644 --- a/Flazzy/ABC/AVM2/Instructions/PushUndefinedIns.cs +++ b/Flazzy/ABC/AVM2/Instructions/PushUndefinedIns.cs @@ -1,15 +1,12 @@ namespace Flazzy.ABC.AVM2.Instructions; -public class PushUndefinedIns : ASInstruction +public sealed class PushUndefinedIns : ASInstruction { public PushUndefinedIns() : base(OPCode.PushUndefined) { } - public override int GetPushCount() - { - return 1; - } + public override int GetPushCount() => 1; public override void Execute(ASMachine machine) { machine.Values.Push(null); diff --git a/Flazzy/ABC/AVM2/Instructions/PushWithIns.cs b/Flazzy/ABC/AVM2/Instructions/PushWithIns.cs index 3775759..273a4e9 100644 --- a/Flazzy/ABC/AVM2/Instructions/PushWithIns.cs +++ b/Flazzy/ABC/AVM2/Instructions/PushWithIns.cs @@ -1,15 +1,12 @@ namespace Flazzy.ABC.AVM2.Instructions; -public class PushWithIns : ASInstruction +public sealed class PushWithIns : ASInstruction { public PushWithIns() : base(OPCode.PushWith) { } - public override int GetPopCount() - { - return 1; - } + public override int GetPopCount() => 1; public override void Execute(ASMachine machine) { object scopeObj = machine.Values.Pop(); diff --git a/Flazzy/ABC/AVM2/Instructions/Register Management/DecLocalIIns.cs b/Flazzy/ABC/AVM2/Instructions/Register Management/DecLocalIIns.cs index 7ab71fc..691c32e 100644 --- a/Flazzy/ABC/AVM2/Instructions/Register Management/DecLocalIIns.cs +++ b/Flazzy/ABC/AVM2/Instructions/Register Management/DecLocalIIns.cs @@ -2,13 +2,13 @@ namespace Flazzy.ABC.AVM2.Instructions; -public class DecLocalIIns : Local +public sealed class DecLocalIIns : Local { public DecLocalIIns(int register) : base(OPCode.DecLocal_i, register) { } - public DecLocalIIns(FlashReader input) - : base(OPCode.DecLocal_i, input) + public DecLocalIIns(ref SpanFlashReader input) + : base(OPCode.DecLocal_i, ref input) { } public override void Execute(ASMachine machine) @@ -16,7 +16,7 @@ public override void Execute(ASMachine machine) object value = machine.Registers[Register]; if (value != null) { - value = (Convert.ToInt32(value) - 1); + value = Convert.ToInt32(value) - 1; } machine.Registers[Register] = value; } diff --git a/Flazzy/ABC/AVM2/Instructions/Register Management/DecLocalIns.cs b/Flazzy/ABC/AVM2/Instructions/Register Management/DecLocalIns.cs index df46529..b4f6282 100644 --- a/Flazzy/ABC/AVM2/Instructions/Register Management/DecLocalIns.cs +++ b/Flazzy/ABC/AVM2/Instructions/Register Management/DecLocalIns.cs @@ -2,13 +2,13 @@ namespace Flazzy.ABC.AVM2.Instructions; -public class DecLocalIns : Local +public sealed class DecLocalIns : Local { public DecLocalIns(int register) : base(OPCode.DecLocal, register) { } - public DecLocalIns(FlashReader input) - : base(OPCode.DecLocal, input) + public DecLocalIns(ref SpanFlashReader input) + : base(OPCode.DecLocal, ref input) { } public override void Execute(ASMachine machine) @@ -16,7 +16,7 @@ public override void Execute(ASMachine machine) object value = machine.Registers[Register]; if (value != null) { - value = (Convert.ToDouble(value) - 1); + value = Convert.ToDouble(value) - 1; } machine.Registers[Register] = value; } diff --git a/Flazzy/ABC/AVM2/Instructions/Register Management/GetLocal0Ins.cs b/Flazzy/ABC/AVM2/Instructions/Register Management/GetLocal0Ins.cs index fdc29d9..ba9b36f 100644 --- a/Flazzy/ABC/AVM2/Instructions/Register Management/GetLocal0Ins.cs +++ b/Flazzy/ABC/AVM2/Instructions/Register Management/GetLocal0Ins.cs @@ -1,12 +1,8 @@ namespace Flazzy.ABC.AVM2.Instructions; -public class GetLocal0Ins : Local +public sealed class GetLocal0Ins : Local { - public override int Register - { - get => 0; - set => throw new NotSupportedException(); - } + public override int Register => 0; public GetLocal0Ins() : base(OPCode.GetLocal_0) diff --git a/Flazzy/ABC/AVM2/Instructions/Register Management/GetLocal1Ins.cs b/Flazzy/ABC/AVM2/Instructions/Register Management/GetLocal1Ins.cs index bc5f801..267558f 100644 --- a/Flazzy/ABC/AVM2/Instructions/Register Management/GetLocal1Ins.cs +++ b/Flazzy/ABC/AVM2/Instructions/Register Management/GetLocal1Ins.cs @@ -1,12 +1,8 @@ namespace Flazzy.ABC.AVM2.Instructions; -public class GetLocal1Ins : Local +public sealed class GetLocal1Ins : Local { - public override int Register - { - get => 1; - set => throw new NotSupportedException(); - } + public override int Register => 1; public GetLocal1Ins() : base(OPCode.GetLocal_1) diff --git a/Flazzy/ABC/AVM2/Instructions/Register Management/GetLocal2Ins.cs b/Flazzy/ABC/AVM2/Instructions/Register Management/GetLocal2Ins.cs index da6a3be..614f406 100644 --- a/Flazzy/ABC/AVM2/Instructions/Register Management/GetLocal2Ins.cs +++ b/Flazzy/ABC/AVM2/Instructions/Register Management/GetLocal2Ins.cs @@ -1,12 +1,8 @@ namespace Flazzy.ABC.AVM2.Instructions; -public class GetLocal2Ins : Local +public sealed class GetLocal2Ins : Local { - public override int Register - { - get => 2; - set => throw new NotSupportedException(); - } + public override int Register => 2; public GetLocal2Ins() : base(OPCode.GetLocal_2) diff --git a/Flazzy/ABC/AVM2/Instructions/Register Management/GetLocal3Ins.cs b/Flazzy/ABC/AVM2/Instructions/Register Management/GetLocal3Ins.cs index bf939cf..a71641d 100644 --- a/Flazzy/ABC/AVM2/Instructions/Register Management/GetLocal3Ins.cs +++ b/Flazzy/ABC/AVM2/Instructions/Register Management/GetLocal3Ins.cs @@ -1,12 +1,8 @@ namespace Flazzy.ABC.AVM2.Instructions; -public class GetLocal3Ins : Local +public sealed class GetLocal3Ins : Local { - public override int Register - { - get => 3; - set => throw new NotSupportedException(); - } + public override int Register => 3; public GetLocal3Ins() : base(OPCode.GetLocal_3) diff --git a/Flazzy/ABC/AVM2/Instructions/Register Management/GetLocalIns.cs b/Flazzy/ABC/AVM2/Instructions/Register Management/GetLocalIns.cs index 13d11b9..e2b8f54 100644 --- a/Flazzy/ABC/AVM2/Instructions/Register Management/GetLocalIns.cs +++ b/Flazzy/ABC/AVM2/Instructions/Register Management/GetLocalIns.cs @@ -2,12 +2,12 @@ namespace Flazzy.ABC.AVM2.Instructions; -public class GetLocalIns : Local +public sealed class GetLocalIns : Local { public GetLocalIns(int register) : base(OPCode.GetLocal, register) { } - public GetLocalIns(FlashReader input) - : base(OPCode.GetLocal, input) + public GetLocalIns(ref SpanFlashReader input) + : base(OPCode.GetLocal, ref input) { } } \ No newline at end of file diff --git a/Flazzy/ABC/AVM2/Instructions/Register Management/IncLocalIIns.cs b/Flazzy/ABC/AVM2/Instructions/Register Management/IncLocalIIns.cs index 2cd8607..f613ce2 100644 --- a/Flazzy/ABC/AVM2/Instructions/Register Management/IncLocalIIns.cs +++ b/Flazzy/ABC/AVM2/Instructions/Register Management/IncLocalIIns.cs @@ -2,13 +2,13 @@ namespace Flazzy.ABC.AVM2.Instructions; -public class IncLocalIIns : Local +public sealed class IncLocalIIns : Local { public IncLocalIIns(int register) : base(OPCode.IncLocal_i, register) { } - public IncLocalIIns(FlashReader input) - : base(OPCode.IncLocal_i, input) + public IncLocalIIns(ref SpanFlashReader input) + : base(OPCode.IncLocal_i, ref input) { } public override void Execute(ASMachine machine) @@ -16,7 +16,7 @@ public override void Execute(ASMachine machine) object value = machine.Registers[Register]; if (value != null) { - value = (Convert.ToInt32(value) + 1); + value = Convert.ToInt32(value) + 1; } machine.Registers[Register] = value; } diff --git a/Flazzy/ABC/AVM2/Instructions/Register Management/IncLocalIns.cs b/Flazzy/ABC/AVM2/Instructions/Register Management/IncLocalIns.cs index 41bb9db..286371c 100644 --- a/Flazzy/ABC/AVM2/Instructions/Register Management/IncLocalIns.cs +++ b/Flazzy/ABC/AVM2/Instructions/Register Management/IncLocalIns.cs @@ -2,13 +2,13 @@ namespace Flazzy.ABC.AVM2.Instructions; -public class IncLocalIns : Local +public sealed class IncLocalIns : Local { public IncLocalIns(int register) : base(OPCode.IncLocal, register) { } - public IncLocalIns(FlashReader input) - : base(OPCode.IncLocal, input) + public IncLocalIns(ref SpanFlashReader input) + : base(OPCode.IncLocal, ref input) { } public override void Execute(ASMachine machine) @@ -16,7 +16,7 @@ public override void Execute(ASMachine machine) object value = machine.Registers[Register]; if (value != null) { - value = (Convert.ToDouble(value) + 1); + value = Convert.ToDouble(value) + 1; } machine.Registers[Register] = value; } diff --git a/Flazzy/ABC/AVM2/Instructions/Register Management/KillIns.cs b/Flazzy/ABC/AVM2/Instructions/Register Management/KillIns.cs index c02369c..3dead70 100644 --- a/Flazzy/ABC/AVM2/Instructions/Register Management/KillIns.cs +++ b/Flazzy/ABC/AVM2/Instructions/Register Management/KillIns.cs @@ -2,13 +2,13 @@ namespace Flazzy.ABC.AVM2.Instructions; -public class KillIns : Local +public sealed class KillIns : Local { public KillIns(int register) : base(OPCode.Kill, register) { } - public KillIns(FlashReader input) - : base(OPCode.Kill, input) + public KillIns(ref SpanFlashReader input) + : base(OPCode.Kill, ref input) { } public override void Execute(ASMachine machine) diff --git a/Flazzy/ABC/AVM2/Instructions/Register Management/Local.cs b/Flazzy/ABC/AVM2/Instructions/Register Management/Local.cs index 596a668..05e70e3 100644 --- a/Flazzy/ABC/AVM2/Instructions/Register Management/Local.cs +++ b/Flazzy/ABC/AVM2/Instructions/Register Management/Local.cs @@ -4,7 +4,7 @@ namespace Flazzy.ABC.AVM2.Instructions; public abstract class Local : ASInstruction { - public virtual int Register { get; set; } + public virtual int Register { get; } public Local(OPCode op) : base(op) @@ -14,19 +14,19 @@ public Local(OPCode op, int register) { Register = register; } - public Local(OPCode op, FlashReader input) + public Local(OPCode op, ref SpanFlashReader input) : this(op) { - Register = input.ReadInt30(); + Register = input.ReadEncodedInt(); } public override int GetPopCount() { - return (IsSetLocal(OP) ? 1 : 0); + return IsSetLocal(OP) ? 1 : 0; } public override int GetPushCount() { - return (IsGetLocal(OP) ? 1 : 0); + return IsGetLocal(OP) ? 1 : 0; } public override void Execute(ASMachine machine) { @@ -41,105 +41,104 @@ public override void Execute(ASMachine machine) } } - protected override void WriteValuesTo(FlashWriter output) + protected override int GetBodySize() { - switch (OP) + return OP switch { - case OPCode.SetLocal_0: - case OPCode.SetLocal_1: - case OPCode.SetLocal_2: - case OPCode.SetLocal_3: - - case OPCode.GetLocal_0: - case OPCode.GetLocal_1: - case OPCode.GetLocal_2: - case OPCode.GetLocal_3: return; - - default: - output.WriteInt30(Register); - break; - } - } + OPCode.Kill or + + OPCode.SetLocal or + OPCode.GetLocal or + + OPCode.IncLocal or + OPCode.IncLocal_i or - public static bool IsValid(OPCode op) + OPCode.DecLocal or + OPCode.DecLocal_i => SpanFlashWriter.GetEncodedIntSize(Register), + + _ => 0 + }; + } + protected override void WriteValuesTo(ref SpanFlashWriter output) { - switch (op) + switch (OP) { case OPCode.Kill: - case OPCode.DecLocal: - case OPCode.DecLocal_i: + case OPCode.SetLocal: + case OPCode.GetLocal: case OPCode.IncLocal: case OPCode.IncLocal_i: - case OPCode.GetLocal: - case OPCode.GetLocal_0: - case OPCode.GetLocal_1: - case OPCode.GetLocal_2: - case OPCode.GetLocal_3: - - case OPCode.SetLocal: - case OPCode.SetLocal_0: - case OPCode.SetLocal_1: - case OPCode.SetLocal_2: - case OPCode.SetLocal_3: - return true; - - default: return false; + case OPCode.DecLocal: + case OPCode.DecLocal_i: + output.WriteEncodedInt(Register); + break; } } - public static bool IsGetLocal(OPCode op) - { - switch (op) - { - case OPCode.GetLocal: - case OPCode.GetLocal_0: - case OPCode.GetLocal_1: - case OPCode.GetLocal_2: - case OPCode.GetLocal_3: - return true; - default: return false; - } - } - public static bool IsSetLocal(OPCode op) + public static bool IsValid(OPCode op) => op switch { - switch (op) - { - case OPCode.SetLocal: - case OPCode.SetLocal_0: - case OPCode.SetLocal_1: - case OPCode.SetLocal_2: - case OPCode.SetLocal_3: - return true; + OPCode.Kill or - default: return false; - } - } + OPCode.DecLocal or + OPCode.DecLocal_i or + + OPCode.IncLocal or + OPCode.IncLocal_i or + + OPCode.GetLocal or + OPCode.GetLocal_0 or + OPCode.GetLocal_1 or + OPCode.GetLocal_2 or + OPCode.GetLocal_3 or - public static Local CreateSet(int register) + OPCode.SetLocal or + OPCode.SetLocal_0 or + OPCode.SetLocal_1 or + OPCode.SetLocal_2 or + OPCode.SetLocal_3 => true, + + _ => false + }; + public static bool IsGetLocal(OPCode op) => op switch { - switch (register) - { - case 0: return new SetLocal0Ins(); - case 1: return new SetLocal1Ins(); - case 2: return new SetLocal2Ins(); - case 3: return new SetLocal3Ins(); + OPCode.GetLocal or + OPCode.GetLocal_0 or + OPCode.GetLocal_1 or + OPCode.GetLocal_2 or + OPCode.GetLocal_3 => true, + + _ => false + }; + public static bool IsSetLocal(OPCode op) => op switch + { + OPCode.SetLocal or + OPCode.SetLocal_0 or + OPCode.SetLocal_1 or + OPCode.SetLocal_2 or + OPCode.SetLocal_3 => true, - default: return new SetLocalIns(register); - } - } - public static Local CreateGet(int register) + _ => false + }; + + public static Local CreateSet(int register) => register switch { - switch (register) - { - case 0: return new GetLocal0Ins(); - case 1: return new GetLocal1Ins(); - case 2: return new GetLocal2Ins(); - case 3: return new GetLocal3Ins(); + 0 => new SetLocal0Ins(), + 1 => new SetLocal1Ins(), + 2 => new SetLocal2Ins(), + 3 => new SetLocal3Ins(), + + _ => new SetLocalIns(register), + }; + public static Local CreateGet(int register) => register switch + { + 0 => new GetLocal0Ins(), + 1 => new GetLocal1Ins(), + 2 => new GetLocal2Ins(), + 3 => new GetLocal3Ins(), - default: return new GetLocalIns(register); - } - } + _ => new GetLocalIns(register), + }; } \ No newline at end of file diff --git a/Flazzy/ABC/AVM2/Instructions/Register Management/SetLocal0Ins.cs b/Flazzy/ABC/AVM2/Instructions/Register Management/SetLocal0Ins.cs index 4f76469..dc4c19a 100644 --- a/Flazzy/ABC/AVM2/Instructions/Register Management/SetLocal0Ins.cs +++ b/Flazzy/ABC/AVM2/Instructions/Register Management/SetLocal0Ins.cs @@ -1,12 +1,8 @@ namespace Flazzy.ABC.AVM2.Instructions; -public class SetLocal0Ins : Local +public sealed class SetLocal0Ins : Local { - public override int Register - { - get => 0; - set => throw new NotSupportedException(); - } + public override int Register => 0; public SetLocal0Ins() : base(OPCode.SetLocal_0) diff --git a/Flazzy/ABC/AVM2/Instructions/Register Management/SetLocal1Ins.cs b/Flazzy/ABC/AVM2/Instructions/Register Management/SetLocal1Ins.cs index 6950203..0ca8f96 100644 --- a/Flazzy/ABC/AVM2/Instructions/Register Management/SetLocal1Ins.cs +++ b/Flazzy/ABC/AVM2/Instructions/Register Management/SetLocal1Ins.cs @@ -1,12 +1,8 @@ namespace Flazzy.ABC.AVM2.Instructions; -public class SetLocal1Ins : Local +public sealed class SetLocal1Ins : Local { - public override int Register - { - get => 1; - set => throw new NotSupportedException(); - } + public override int Register => 1; public SetLocal1Ins() : base(OPCode.SetLocal_1) diff --git a/Flazzy/ABC/AVM2/Instructions/Register Management/SetLocal2Ins.cs b/Flazzy/ABC/AVM2/Instructions/Register Management/SetLocal2Ins.cs index e4bf9ad..3c1589b 100644 --- a/Flazzy/ABC/AVM2/Instructions/Register Management/SetLocal2Ins.cs +++ b/Flazzy/ABC/AVM2/Instructions/Register Management/SetLocal2Ins.cs @@ -1,12 +1,8 @@ namespace Flazzy.ABC.AVM2.Instructions; -public class SetLocal2Ins : Local +public sealed class SetLocal2Ins : Local { - public override int Register - { - get => 2; - set => throw new NotSupportedException(); - } + public override int Register => 2; public SetLocal2Ins() : base(OPCode.SetLocal_2) diff --git a/Flazzy/ABC/AVM2/Instructions/Register Management/SetLocal3Ins.cs b/Flazzy/ABC/AVM2/Instructions/Register Management/SetLocal3Ins.cs index e7df691..c3e03d3 100644 --- a/Flazzy/ABC/AVM2/Instructions/Register Management/SetLocal3Ins.cs +++ b/Flazzy/ABC/AVM2/Instructions/Register Management/SetLocal3Ins.cs @@ -1,12 +1,8 @@ namespace Flazzy.ABC.AVM2.Instructions; -public class SetLocal3Ins : Local +public sealed class SetLocal3Ins : Local { - public override int Register - { - get => 3; - set => throw new NotSupportedException(); - } + public override int Register => 3; public SetLocal3Ins() : base(OPCode.SetLocal_3) diff --git a/Flazzy/ABC/AVM2/Instructions/Register Management/SetLocalIns.cs b/Flazzy/ABC/AVM2/Instructions/Register Management/SetLocalIns.cs index 0361691..8745782 100644 --- a/Flazzy/ABC/AVM2/Instructions/Register Management/SetLocalIns.cs +++ b/Flazzy/ABC/AVM2/Instructions/Register Management/SetLocalIns.cs @@ -2,12 +2,12 @@ namespace Flazzy.ABC.AVM2.Instructions; -public class SetLocalIns : Local +public sealed class SetLocalIns : Local { public SetLocalIns(int register) : base(OPCode.SetLocal, register) { } - public SetLocalIns(FlashReader input) - : base(OPCode.SetLocal, input) + public SetLocalIns(ref SpanFlashReader input) + : base(OPCode.SetLocal, ref input) { } } \ No newline at end of file diff --git a/Flazzy/ABC/AVM2/Instructions/ReturnValueIns.cs b/Flazzy/ABC/AVM2/Instructions/ReturnValueIns.cs index b291a50..ac62835 100644 --- a/Flazzy/ABC/AVM2/Instructions/ReturnValueIns.cs +++ b/Flazzy/ABC/AVM2/Instructions/ReturnValueIns.cs @@ -1,15 +1,12 @@ namespace Flazzy.ABC.AVM2.Instructions; -public class ReturnValueIns : ASInstruction +public sealed class ReturnValueIns : ASInstruction { public ReturnValueIns() : base(OPCode.ReturnValue) { } - public override int GetPopCount() - { - return 1; - } + public override int GetPopCount() => 1; public override void Execute(ASMachine machine) { machine.Values.Pop(); diff --git a/Flazzy/ABC/AVM2/Instructions/ReturnVoidIns.cs b/Flazzy/ABC/AVM2/Instructions/ReturnVoidIns.cs index 048e88a..fd9d52b 100644 --- a/Flazzy/ABC/AVM2/Instructions/ReturnVoidIns.cs +++ b/Flazzy/ABC/AVM2/Instructions/ReturnVoidIns.cs @@ -1,6 +1,6 @@ namespace Flazzy.ABC.AVM2.Instructions; -public class ReturnVoidIns : ASInstruction +public sealed class ReturnVoidIns : ASInstruction { public ReturnVoidIns() : base(OPCode.ReturnVoid) diff --git a/Flazzy/ABC/AVM2/Instructions/SetPropertyIns.cs b/Flazzy/ABC/AVM2/Instructions/SetPropertyIns.cs index 18cfcb2..109c1ac 100644 --- a/Flazzy/ABC/AVM2/Instructions/SetPropertyIns.cs +++ b/Flazzy/ABC/AVM2/Instructions/SetPropertyIns.cs @@ -2,7 +2,7 @@ namespace Flazzy.ABC.AVM2.Instructions; -public class SetPropertyIns : ASInstruction +public sealed class SetPropertyIns : ASInstruction { public int PropertyNameIndex { get; set; } public ASMultiname PropertyName => ABC.Pool.Multinames[PropertyNameIndex]; @@ -10,10 +10,10 @@ public class SetPropertyIns : ASInstruction public SetPropertyIns(ABCFile abc) : base(OPCode.SetProperty, abc) { } - public SetPropertyIns(ABCFile abc, FlashReader input) + public SetPropertyIns(ABCFile abc, ref SpanFlashReader input) : this(abc) { - PropertyNameIndex = input.ReadInt30(); + PropertyNameIndex = input.ReadEncodedInt(); } public SetPropertyIns(ABCFile abc, int propertyNameIndex) : this(abc) @@ -23,7 +23,7 @@ public SetPropertyIns(ABCFile abc, int propertyNameIndex) public override int GetPopCount() { - return (2 + ResolveMultinamePops(PropertyName)); + return 2 + ResolveMultinamePops(PropertyName); } public override void Execute(ASMachine machine) { @@ -32,8 +32,12 @@ public override void Execute(ASMachine machine) object obj = machine.Values.Pop(); } - protected override void WriteValuesTo(FlashWriter output) + protected override int GetBodySize() { - output.WriteInt30(PropertyNameIndex); + return SpanFlashWriter.GetEncodedIntSize(PropertyNameIndex); + } + protected override void WriteValuesTo(ref SpanFlashWriter output) + { + output.WriteEncodedInt(PropertyNameIndex); } } \ No newline at end of file diff --git a/Flazzy/ABC/AVM2/Instructions/SetSlotIns.cs b/Flazzy/ABC/AVM2/Instructions/SetSlotIns.cs index 2373f75..dfa52c0 100644 --- a/Flazzy/ABC/AVM2/Instructions/SetSlotIns.cs +++ b/Flazzy/ABC/AVM2/Instructions/SetSlotIns.cs @@ -2,7 +2,7 @@ namespace Flazzy.ABC.AVM2.Instructions; -public class SetSlotIns : ASInstruction +public sealed class SetSlotIns : ASInstruction { public int SlotIndex { get; set; } @@ -14,24 +14,25 @@ public SetSlotIns(int slotIndex) { SlotIndex = slotIndex; } - public SetSlotIns(FlashReader input) + public SetSlotIns(ref SpanFlashReader input) : this() { - SlotIndex = input.ReadInt30(); + SlotIndex = input.ReadEncodedInt(); } - public override int GetPopCount() - { - return 2; - } + public override int GetPopCount() => 2; public override void Execute(ASMachine machine) { object value = machine.Values.Pop(); object obj = machine.Values.Pop(); } - protected override void WriteValuesTo(FlashWriter output) + protected override int GetBodySize() + { + return SpanFlashWriter.GetEncodedIntSize(SlotIndex); + } + protected override void WriteValuesTo(ref SpanFlashWriter output) { - output.WriteInt30(SlotIndex); + output.WriteEncodedInt(SlotIndex); } } \ No newline at end of file diff --git a/Flazzy/ABC/AVM2/Instructions/SetSuperIns.cs b/Flazzy/ABC/AVM2/Instructions/SetSuperIns.cs index f73e7f3..e037864 100644 --- a/Flazzy/ABC/AVM2/Instructions/SetSuperIns.cs +++ b/Flazzy/ABC/AVM2/Instructions/SetSuperIns.cs @@ -2,7 +2,7 @@ namespace Flazzy.ABC.AVM2.Instructions; -public class SetSuperIns : ASInstruction +public sealed class SetSuperIns : ASInstruction { public int PropertyNameIndex { get; set; } public ASMultiname PropertyName => ABC.Pool.Multinames[PropertyNameIndex]; @@ -10,10 +10,10 @@ public class SetSuperIns : ASInstruction public SetSuperIns(ABCFile abc) : base(OPCode.SetSuper, abc) { } - public SetSuperIns(ABCFile abc, FlashReader input) + public SetSuperIns(ABCFile abc, ref SpanFlashReader input) : this(abc) { - PropertyNameIndex = input.ReadInt30(); + PropertyNameIndex = input.ReadEncodedInt(); } public SetSuperIns(ABCFile abc, int propertyNameIndex) : this(abc) @@ -23,7 +23,7 @@ public SetSuperIns(ABCFile abc, int propertyNameIndex) public override int GetPopCount() { - return (2 + ResolveMultinamePops(PropertyName)); + return 2 + ResolveMultinamePops(PropertyName); } public override void Execute(ASMachine machine) { @@ -32,8 +32,12 @@ public override void Execute(ASMachine machine) object obj = machine.Values.Pop(); } - protected override void WriteValuesTo(FlashWriter output) + protected override int GetBodySize() { - output.WriteInt30(PropertyNameIndex); + return SpanFlashWriter.GetEncodedIntSize(PropertyNameIndex); + } + protected override void WriteValuesTo(ref SpanFlashWriter output) + { + output.WriteEncodedInt(PropertyNameIndex); } } \ No newline at end of file diff --git a/Flazzy/ABC/AVM2/Instructions/Stack Management/Primitive.cs b/Flazzy/ABC/AVM2/Instructions/Stack Management/Primitive.cs index 700a2cf..fc78c48 100644 --- a/Flazzy/ABC/AVM2/Instructions/Stack Management/Primitive.cs +++ b/Flazzy/ABC/AVM2/Instructions/Stack Management/Primitive.cs @@ -11,14 +11,8 @@ public Primitive(OPCode op, ABCFile abc) : base(op, abc) { } - public override int GetPopCount() - { - return 0; - } - public override int GetPushCount() - { - return 1; - } + public override int GetPopCount() => 0; + public override int GetPushCount() => 1; public override void Execute(ASMachine machine) { machine.Values.Push(Value); @@ -26,60 +20,38 @@ public override void Execute(ASMachine machine) public static bool IsValid(OPCode op) { - switch (op) + return op switch { - case OPCode.PushByte: - case OPCode.PushDouble: - case OPCode.PushFalse: - case OPCode.PushInt: - case OPCode.PushNan: - case OPCode.PushNull: - case OPCode.PushShort: - case OPCode.PushString: - case OPCode.PushTrue: - case OPCode.PushUInt: - return true; - - default: - return false; - } + OPCode.PushNan or + OPCode.PushNull or + OPCode.PushByte or + OPCode.PushShort or + OPCode.PushInt or + OPCode.PushUInt or + OPCode.PushDouble or + OPCode.PushString or + OPCode.PushTrue or + OPCode.PushFalse => true, + + _ => false + }; } public static Primitive Create(ABCFile abc, object value) { - var typeCode = Type.GetTypeCode(value.GetType()); - switch (typeCode) + return value switch { - case TypeCode.Byte: - case TypeCode.Int32: - return new PushIntIns(abc, (int)value); - - case TypeCode.Int16: - return new PushShortIns((int)value); - - case TypeCode.UInt32: - return new PushUIntIns(abc, (uint)value); - - case TypeCode.Double: - return new PushDoubleIns(abc, (double)value); - - case TypeCode.String: - return new PushStringIns(abc, (string)value); - - case TypeCode.Boolean: - { - var result = (bool)value; - if (result) - { - return new PushTrueIns(); - } - else return new PushFalseIns(); - } - - case TypeCode.Empty: - return new PushNullIns(); - - default: - return new PushNaNIns(); - } + byte @byte => new PushByteIns(@byte), + short @short => new PushShortIns(@short), + int @int => new PushIntIns(abc, @int), + uint @uint => new PushUIntIns(abc, @uint), + double @double => new PushDoubleIns(abc, @double), + string @string => new PushStringIns(abc, @string), + + bool @bool when @bool => new PushTrueIns(), + bool @bool when !@bool => new PushFalseIns(), + + null => new PushNullIns(), + _ => new PushNaNIns() + }; } } \ No newline at end of file diff --git a/Flazzy/ABC/AVM2/Instructions/Stack Management/PushByteIns.cs b/Flazzy/ABC/AVM2/Instructions/Stack Management/PushByteIns.cs index 303f253..1edee0b 100644 --- a/Flazzy/ABC/AVM2/Instructions/Stack Management/PushByteIns.cs +++ b/Flazzy/ABC/AVM2/Instructions/Stack Management/PushByteIns.cs @@ -2,7 +2,7 @@ namespace Flazzy.ABC.AVM2.Instructions; -public class PushByteIns : Primitive +public sealed class PushByteIns : Primitive { private byte _value; new public byte Value @@ -23,13 +23,14 @@ public PushByteIns(byte value) { Value = value; } - public PushByteIns(FlashReader input) + public PushByteIns(ref SpanFlashReader input) : this() { Value = input.ReadByte(); } - protected override void WriteValuesTo(FlashWriter output) + protected override int GetBodySize() => sizeof(byte); + protected override void WriteValuesTo(ref SpanFlashWriter output) { output.Write(Value); } diff --git a/Flazzy/ABC/AVM2/Instructions/Stack Management/PushDoubleIns.cs b/Flazzy/ABC/AVM2/Instructions/Stack Management/PushDoubleIns.cs index 7cd0ded..d09218e 100644 --- a/Flazzy/ABC/AVM2/Instructions/Stack Management/PushDoubleIns.cs +++ b/Flazzy/ABC/AVM2/Instructions/Stack Management/PushDoubleIns.cs @@ -2,7 +2,7 @@ namespace Flazzy.ABC.AVM2.Instructions; -public class PushDoubleIns : Primitive +public sealed class PushDoubleIns : Primitive { private double _value; new public double Value @@ -38,14 +38,18 @@ public PushDoubleIns(ABCFile abc, double value) { Value = value; } - public PushDoubleIns(ABCFile abc, FlashReader input) + public PushDoubleIns(ABCFile abc, ref SpanFlashReader input) : this(abc) { - ValueIndex = input.ReadInt30(); + ValueIndex = input.ReadEncodedInt(); } - protected override void WriteValuesTo(FlashWriter output) + protected override int GetBodySize() { - output.WriteInt30(ValueIndex); + return SpanFlashWriter.GetEncodedIntSize(ValueIndex); + } + protected override void WriteValuesTo(ref SpanFlashWriter output) + { + output.WriteEncodedInt(ValueIndex); } } \ No newline at end of file diff --git a/Flazzy/ABC/AVM2/Instructions/Stack Management/PushFalseIns.cs b/Flazzy/ABC/AVM2/Instructions/Stack Management/PushFalseIns.cs index 5517797..97ef8fb 100644 --- a/Flazzy/ABC/AVM2/Instructions/Stack Management/PushFalseIns.cs +++ b/Flazzy/ABC/AVM2/Instructions/Stack Management/PushFalseIns.cs @@ -1,6 +1,6 @@ namespace Flazzy.ABC.AVM2.Instructions; -public class PushFalseIns : Primitive +public sealed class PushFalseIns : Primitive { public override object Value { diff --git a/Flazzy/ABC/AVM2/Instructions/Stack Management/PushIntIns.cs b/Flazzy/ABC/AVM2/Instructions/Stack Management/PushIntIns.cs index 36a09c6..ce74038 100644 --- a/Flazzy/ABC/AVM2/Instructions/Stack Management/PushIntIns.cs +++ b/Flazzy/ABC/AVM2/Instructions/Stack Management/PushIntIns.cs @@ -2,7 +2,7 @@ namespace Flazzy.ABC.AVM2.Instructions; -public class PushIntIns : Primitive +public sealed class PushIntIns : Primitive { private int _value; new public int Value @@ -38,14 +38,18 @@ public PushIntIns(ABCFile abc, int value) { Value = value; } - public PushIntIns(ABCFile abc, FlashReader input) + public PushIntIns(ABCFile abc, ref SpanFlashReader input) : this(abc) { - ValueIndex = input.ReadInt30(); + ValueIndex = input.ReadEncodedInt(); } - protected override void WriteValuesTo(FlashWriter output) + protected override int GetBodySize() { - output.WriteInt30(ValueIndex); + return SpanFlashWriter.GetEncodedIntSize(ValueIndex); + } + protected override void WriteValuesTo(ref SpanFlashWriter output) + { + output.WriteEncodedInt(ValueIndex); } } \ No newline at end of file diff --git a/Flazzy/ABC/AVM2/Instructions/Stack Management/PushNaNIns.cs b/Flazzy/ABC/AVM2/Instructions/Stack Management/PushNaNIns.cs index 3ad7894..df2303d 100644 --- a/Flazzy/ABC/AVM2/Instructions/Stack Management/PushNaNIns.cs +++ b/Flazzy/ABC/AVM2/Instructions/Stack Management/PushNaNIns.cs @@ -1,6 +1,6 @@ namespace Flazzy.ABC.AVM2.Instructions; -public class PushNaNIns : Primitive +public sealed class PushNaNIns : Primitive { public override object Value { diff --git a/Flazzy/ABC/AVM2/Instructions/Stack Management/PushNullIns.cs b/Flazzy/ABC/AVM2/Instructions/Stack Management/PushNullIns.cs index 5c27d31..3f3375e 100644 --- a/Flazzy/ABC/AVM2/Instructions/Stack Management/PushNullIns.cs +++ b/Flazzy/ABC/AVM2/Instructions/Stack Management/PushNullIns.cs @@ -1,6 +1,6 @@ namespace Flazzy.ABC.AVM2.Instructions; -public class PushNullIns : Primitive +public sealed class PushNullIns : Primitive { public override object Value { diff --git a/Flazzy/ABC/AVM2/Instructions/Stack Management/PushShortIns.cs b/Flazzy/ABC/AVM2/Instructions/Stack Management/PushShortIns.cs index 05aa8ff..9ae46ba 100644 --- a/Flazzy/ABC/AVM2/Instructions/Stack Management/PushShortIns.cs +++ b/Flazzy/ABC/AVM2/Instructions/Stack Management/PushShortIns.cs @@ -2,7 +2,7 @@ namespace Flazzy.ABC.AVM2.Instructions; -public class PushShortIns : Primitive +public sealed class PushShortIns : Primitive { private int _value; new public int Value @@ -23,14 +23,18 @@ public PushShortIns(int value) { Value = value; } - public PushShortIns(FlashReader input) + public PushShortIns(ref SpanFlashReader input) : this() { - Value = input.ReadInt30(); + Value = input.ReadEncodedInt(); } - protected override void WriteValuesTo(FlashWriter output) + protected override int GetBodySize() { - output.WriteInt30(Value); + return SpanFlashWriter.GetEncodedIntSize(Value); + } + protected override void WriteValuesTo(ref SpanFlashWriter output) + { + output.WriteEncodedInt(Value); } } \ No newline at end of file diff --git a/Flazzy/ABC/AVM2/Instructions/Stack Management/PushStringIns.cs b/Flazzy/ABC/AVM2/Instructions/Stack Management/PushStringIns.cs index a29ed20..24d3652 100644 --- a/Flazzy/ABC/AVM2/Instructions/Stack Management/PushStringIns.cs +++ b/Flazzy/ABC/AVM2/Instructions/Stack Management/PushStringIns.cs @@ -2,7 +2,7 @@ namespace Flazzy.ABC.AVM2.Instructions; -public class PushStringIns : Primitive +public sealed class PushStringIns : Primitive { private string _value; new public string Value @@ -43,14 +43,18 @@ public PushStringIns(ABCFile abc, int valueIndex) { ValueIndex = valueIndex; } - public PushStringIns(ABCFile abc, FlashReader input) + public PushStringIns(ABCFile abc, ref SpanFlashReader input) : this(abc) { - ValueIndex = input.ReadInt30(); + ValueIndex = input.ReadEncodedInt(); } - protected override void WriteValuesTo(FlashWriter output) + protected override int GetBodySize() { - output.WriteInt30(ValueIndex); + return SpanFlashWriter.GetEncodedIntSize(ValueIndex); + } + protected override void WriteValuesTo(ref SpanFlashWriter output) + { + output.WriteEncodedInt(ValueIndex); } } \ No newline at end of file diff --git a/Flazzy/ABC/AVM2/Instructions/Stack Management/PushTrueIns.cs b/Flazzy/ABC/AVM2/Instructions/Stack Management/PushTrueIns.cs index 44cb622..c08b7c9 100644 --- a/Flazzy/ABC/AVM2/Instructions/Stack Management/PushTrueIns.cs +++ b/Flazzy/ABC/AVM2/Instructions/Stack Management/PushTrueIns.cs @@ -1,6 +1,6 @@ namespace Flazzy.ABC.AVM2.Instructions; -public class PushTrueIns : Primitive +public sealed class PushTrueIns : Primitive { public override object Value { diff --git a/Flazzy/ABC/AVM2/Instructions/Stack Management/PushUIntIns.cs b/Flazzy/ABC/AVM2/Instructions/Stack Management/PushUIntIns.cs index 0be01a6..2a88688 100644 --- a/Flazzy/ABC/AVM2/Instructions/Stack Management/PushUIntIns.cs +++ b/Flazzy/ABC/AVM2/Instructions/Stack Management/PushUIntIns.cs @@ -2,7 +2,7 @@ namespace Flazzy.ABC.AVM2.Instructions; -public class PushUIntIns : Primitive +public sealed class PushUIntIns : Primitive { private uint _value; new public uint Value @@ -38,14 +38,18 @@ public PushUIntIns(ABCFile abc, uint value) { Value = value; } - public PushUIntIns(ABCFile abc, FlashReader input) + public PushUIntIns(ABCFile abc, ref SpanFlashReader input) : this(abc) { - ValueIndex = input.ReadInt30(); + ValueIndex = input.ReadEncodedInt(); } - protected override void WriteValuesTo(FlashWriter output) + protected override int GetBodySize() { - output.WriteInt30(ValueIndex); + return SpanFlashWriter.GetEncodedIntSize(ValueIndex); + } + protected override void WriteValuesTo(ref SpanFlashWriter output) + { + output.WriteEncodedInt(ValueIndex); } } \ No newline at end of file diff --git a/Flazzy/ABC/AVM2/Instructions/SwapIns.cs b/Flazzy/ABC/AVM2/Instructions/SwapIns.cs index 544d2d8..036fb23 100644 --- a/Flazzy/ABC/AVM2/Instructions/SwapIns.cs +++ b/Flazzy/ABC/AVM2/Instructions/SwapIns.cs @@ -1,19 +1,13 @@ namespace Flazzy.ABC.AVM2.Instructions; -public class SwapIns : ASInstruction +public sealed class SwapIns : ASInstruction { public SwapIns() : base(OPCode.Swap) { } - public override int GetPopCount() - { - return 2; - } - public override int GetPushCount() - { - return 2; - } + public override int GetPopCount() => 2; + public override int GetPushCount() => 2; public override void Execute(ASMachine machine) { object value2 = machine.Values.Pop(); diff --git a/Flazzy/ABC/AVM2/Instructions/ThrowIns.cs b/Flazzy/ABC/AVM2/Instructions/ThrowIns.cs index 428908b..8fa2272 100644 --- a/Flazzy/ABC/AVM2/Instructions/ThrowIns.cs +++ b/Flazzy/ABC/AVM2/Instructions/ThrowIns.cs @@ -1,15 +1,12 @@ namespace Flazzy.ABC.AVM2.Instructions; -public class ThrowIns : ASInstruction +public sealed class ThrowIns : ASInstruction { public ThrowIns() : base(OPCode.Throw) { } - public override int GetPopCount() - { - return 1; - } + public override int GetPopCount() => 1; public override void Execute(ASMachine machine) { object value = machine.Values.Pop(); diff --git a/Flazzy/ABC/AVM2/Instructions/Type Conversion/CoerceAIns.cs b/Flazzy/ABC/AVM2/Instructions/Type Conversion/CoerceAIns.cs index 7ed6b30..68fcac2 100644 --- a/Flazzy/ABC/AVM2/Instructions/Type Conversion/CoerceAIns.cs +++ b/Flazzy/ABC/AVM2/Instructions/Type Conversion/CoerceAIns.cs @@ -1,19 +1,13 @@ namespace Flazzy.ABC.AVM2.Instructions; -public class CoerceAIns : ASInstruction +public sealed class CoerceAIns : ASInstruction { public CoerceAIns() : base(OPCode.Coerce_a) { } - public override int GetPopCount() - { - return 1; - } - public override int GetPushCount() - { - return 1; - } + public override int GetPopCount() => 1; + public override int GetPushCount() => 1; public override void Execute(ASMachine machine) { object value = machine.Values.Pop(); diff --git a/Flazzy/ABC/AVM2/Instructions/Type Conversion/CoerceIns.cs b/Flazzy/ABC/AVM2/Instructions/Type Conversion/CoerceIns.cs index 3fbabc1..8479927 100644 --- a/Flazzy/ABC/AVM2/Instructions/Type Conversion/CoerceIns.cs +++ b/Flazzy/ABC/AVM2/Instructions/Type Conversion/CoerceIns.cs @@ -2,7 +2,7 @@ namespace Flazzy.ABC.AVM2.Instructions; -public class CoerceIns : ASInstruction +public sealed class CoerceIns : ASInstruction { public int TypeNameIndex { get; set; } public ASMultiname TypeName => ABC.Pool.Multinames[TypeNameIndex]; @@ -15,28 +15,26 @@ public CoerceIns(ABCFile abc, int typeNameIndex) { TypeNameIndex = typeNameIndex; } - public CoerceIns(ABCFile abc, FlashReader input) + public CoerceIns(ABCFile abc, ref SpanFlashReader input) : this(abc) { - TypeNameIndex = input.ReadInt30(); + TypeNameIndex = input.ReadEncodedInt(); } - public override int GetPopCount() - { - return 1; - } - public override int GetPushCount() - { - return 1; - } + public override int GetPopCount() => 1; + public override int GetPushCount() => 1; public override void Execute(ASMachine machine) { object value = machine.Values.Pop(); machine.Values.Push(null); } - protected override void WriteValuesTo(FlashWriter output) + protected override int GetBodySize() + { + return SpanFlashWriter.GetEncodedIntSize(TypeNameIndex); + } + protected override void WriteValuesTo(ref SpanFlashWriter output) { - output.WriteInt30(TypeNameIndex); + output.WriteEncodedInt(TypeNameIndex); } } \ No newline at end of file diff --git a/Flazzy/ABC/AVM2/Instructions/Type Conversion/CoerceSIns.cs b/Flazzy/ABC/AVM2/Instructions/Type Conversion/CoerceSIns.cs index c9fa4c6..d87f602 100644 --- a/Flazzy/ABC/AVM2/Instructions/Type Conversion/CoerceSIns.cs +++ b/Flazzy/ABC/AVM2/Instructions/Type Conversion/CoerceSIns.cs @@ -1,19 +1,13 @@ namespace Flazzy.ABC.AVM2.Instructions; -public class CoerceSIns : ASInstruction +public sealed class CoerceSIns : ASInstruction { public CoerceSIns() : base(OPCode.Coerce_s) { } - public override int GetPopCount() - { - return 1; - } - public override int GetPushCount() - { - return 1; - } + public override int GetPopCount() => 1; + public override int GetPushCount() => 1; public override void Execute(ASMachine machine) { string result = null; diff --git a/Flazzy/ABC/AVM2/Instructions/Type Conversion/ConvertBIns.cs b/Flazzy/ABC/AVM2/Instructions/Type Conversion/ConvertBIns.cs index 0d45b56..97aab28 100644 --- a/Flazzy/ABC/AVM2/Instructions/Type Conversion/ConvertBIns.cs +++ b/Flazzy/ABC/AVM2/Instructions/Type Conversion/ConvertBIns.cs @@ -1,19 +1,13 @@ namespace Flazzy.ABC.AVM2.Instructions; -public class ConvertBIns : ASInstruction +public sealed class ConvertBIns : ASInstruction { public ConvertBIns() : base(OPCode.Convert_b) { } - public override int GetPopCount() - { - return 1; - } - public override int GetPushCount() - { - return 1; - } + public override int GetPopCount() => 1; + public override int GetPushCount() => 1; public override void Execute(ASMachine machine) { object result = null; diff --git a/Flazzy/ABC/AVM2/Instructions/Type Conversion/ConvertDIns.cs b/Flazzy/ABC/AVM2/Instructions/Type Conversion/ConvertDIns.cs index a6a561a..fcb5f5a 100644 --- a/Flazzy/ABC/AVM2/Instructions/Type Conversion/ConvertDIns.cs +++ b/Flazzy/ABC/AVM2/Instructions/Type Conversion/ConvertDIns.cs @@ -1,19 +1,13 @@ namespace Flazzy.ABC.AVM2.Instructions; -public class ConvertDIns : ASInstruction +public sealed class ConvertDIns : ASInstruction { public ConvertDIns() : base(OPCode.Convert_d) { } - public override int GetPopCount() - { - return 1; - } - public override int GetPushCount() - { - return 1; - } + public override int GetPopCount() => 1; + public override int GetPushCount() => 1; public override void Execute(ASMachine machine) { object result = null; diff --git a/Flazzy/ABC/AVM2/Instructions/Type Conversion/ConvertIIns.cs b/Flazzy/ABC/AVM2/Instructions/Type Conversion/ConvertIIns.cs index 47a308a..745c645 100644 --- a/Flazzy/ABC/AVM2/Instructions/Type Conversion/ConvertIIns.cs +++ b/Flazzy/ABC/AVM2/Instructions/Type Conversion/ConvertIIns.cs @@ -1,19 +1,13 @@ namespace Flazzy.ABC.AVM2.Instructions; -public class ConvertIIns : ASInstruction +public sealed class ConvertIIns : ASInstruction { public ConvertIIns() : base(OPCode.Convert_i) { } - public override int GetPopCount() - { - return 1; - } - public override int GetPushCount() - { - return 1; - } + public override int GetPopCount() => 1; + public override int GetPushCount() => 1; public override void Execute(ASMachine machine) { object result = null; diff --git a/Flazzy/ABC/AVM2/Instructions/Type Conversion/ConvertOIns.cs b/Flazzy/ABC/AVM2/Instructions/Type Conversion/ConvertOIns.cs index c6e47c1..e2ab346 100644 --- a/Flazzy/ABC/AVM2/Instructions/Type Conversion/ConvertOIns.cs +++ b/Flazzy/ABC/AVM2/Instructions/Type Conversion/ConvertOIns.cs @@ -1,19 +1,13 @@ namespace Flazzy.ABC.AVM2.Instructions; -public class ConvertOIns : ASInstruction +public sealed class ConvertOIns : ASInstruction { public ConvertOIns() : base(OPCode.Convert_o) { } - public override int GetPopCount() - { - return 1; - } - public override int GetPushCount() - { - return 1; - } + public override int GetPopCount() => 1; + public override int GetPushCount() => 1; public override void Execute(ASMachine machine) { object value = machine.Values.Pop(); diff --git a/Flazzy/ABC/AVM2/Instructions/Type Conversion/ConvertSIns.cs b/Flazzy/ABC/AVM2/Instructions/Type Conversion/ConvertSIns.cs index 227a5d0..9832b0f 100644 --- a/Flazzy/ABC/AVM2/Instructions/Type Conversion/ConvertSIns.cs +++ b/Flazzy/ABC/AVM2/Instructions/Type Conversion/ConvertSIns.cs @@ -1,19 +1,13 @@ namespace Flazzy.ABC.AVM2.Instructions; -public class ConvertSIns : ASInstruction +public sealed class ConvertSIns : ASInstruction { public ConvertSIns() : base(OPCode.Convert_s) { } - public override int GetPopCount() - { - return 1; - } - public override int GetPushCount() - { - return 1; - } + public override int GetPopCount() => 1; + public override int GetPushCount() => 1; public override void Execute(ASMachine machine) { object result = null; diff --git a/Flazzy/ABC/AVM2/Instructions/Type Conversion/ConvertUIns.cs b/Flazzy/ABC/AVM2/Instructions/Type Conversion/ConvertUIns.cs index 64d0456..9921ba9 100644 --- a/Flazzy/ABC/AVM2/Instructions/Type Conversion/ConvertUIns.cs +++ b/Flazzy/ABC/AVM2/Instructions/Type Conversion/ConvertUIns.cs @@ -1,19 +1,13 @@ namespace Flazzy.ABC.AVM2.Instructions; -public class ConvertUIns : ASInstruction +public sealed class ConvertUIns : ASInstruction { public ConvertUIns() : base(OPCode.Convert_u) { } - public override int GetPopCount() - { - return 1; - } - public override int GetPushCount() - { - return 1; - } + public override int GetPopCount() => 1; + public override int GetPushCount() => 1; public override void Execute(ASMachine machine) { object result = null; diff --git a/Flazzy/ABC/AVM2/Instructions/TypeOfIns.cs b/Flazzy/ABC/AVM2/Instructions/TypeOfIns.cs index abea401..38edc91 100644 --- a/Flazzy/ABC/AVM2/Instructions/TypeOfIns.cs +++ b/Flazzy/ABC/AVM2/Instructions/TypeOfIns.cs @@ -1,19 +1,13 @@ namespace Flazzy.ABC.AVM2.Instructions; -public class TypeOfIns : ASInstruction +public sealed class TypeOfIns : ASInstruction { public TypeOfIns() : base(OPCode.TypeOf) { } - public override int GetPopCount() - { - return 1; - } - public override int GetPushCount() - { - return 1; - } + public override int GetPopCount() => 1; + public override int GetPushCount() => 1; public override void Execute(ASMachine machine) { object value = machine.Values.Pop(); diff --git a/Flazzy/Flazzy.csproj b/Flazzy/Flazzy.csproj index cdde06f..aa4e56a 100644 --- a/Flazzy/Flazzy.csproj +++ b/Flazzy/Flazzy.csproj @@ -14,6 +14,7 @@ MIT https://github.com/ArachisH/Flazzy .NET library for [dis]assembling shockwave flash files(swf). + true diff --git a/Flazzy/IO/SpanFlashReader.cs b/Flazzy/IO/SpanFlashReader.cs new file mode 100644 index 0000000..7bc247d --- /dev/null +++ b/Flazzy/IO/SpanFlashReader.cs @@ -0,0 +1,126 @@ +using System.Buffers.Binary; +using System.Runtime.CompilerServices; +using System.Text; + +namespace Flazzy.IO; + +public ref struct SpanFlashReader +{ + private readonly ReadOnlySpan _data; + + public int Position { get; set; } + + public readonly int Length => _data.Length; + public readonly bool IsDataAvailable => Position < _data.Length; + + public SpanFlashReader(ReadOnlySpan data) + { + _data = data; + Position = 0; + } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public byte ReadByte() => _data[Position++]; + + public ReadOnlySpan ReadBytes(int count) + { + ReadOnlySpan data = _data.Slice(Position, count); + Position += count; + return data; + } + + public void ReadBytes(Span buffer) + { + _data.Slice(Position, buffer.Length).CopyTo(buffer); + Position += buffer.Length; + } + + public short ReadInt16() + { + short value = BinaryPrimitives.ReadInt16LittleEndian(_data.Slice(Position)); + Position += sizeof(short); + return value; + } + public ushort ReadUInt16() + { + ushort value = BinaryPrimitives.ReadUInt16LittleEndian(_data.Slice(Position)); + Position += sizeof(ushort); + return value; + } + + public uint ReadUInt24() + { + uint value = (uint)((_data[Position + 2] << 16) + (_data[Position + 1] << 8) + _data[Position]); + Position += 3; + + if ((value >> 23) == 1) + value |= 0xff000000; + + return value; + } + + public int ReadInt32() + { + int value = BinaryPrimitives.ReadInt32LittleEndian(_data.Slice(Position)); + Position += sizeof(int); + return value; + } + public uint ReadUInt32() + { + uint value = BinaryPrimitives.ReadUInt32LittleEndian(_data.Slice(Position)); + Position += sizeof(uint); + return value; + } + + public ulong ReadUInt64() + { + ulong value = BinaryPrimitives.ReadUInt64LittleEndian(_data.Slice(Position)); + Position += sizeof(ulong); + return value; + } + public double ReadDouble() + { + double value = BinaryPrimitives.ReadDoubleLittleEndian(_data.Slice(Position)); + Position += sizeof(double); + return value; + } + + public int ReadEncodedInt() + { + return (int)ReadEncodedUInt(); + } + public uint ReadEncodedUInt() + { + uint result = ReadByte(); + if ((result & 0x80) == 0) return result; + + result = (result & 0x7F) | (uint)ReadByte() << 7; + if ((result & 0x4000) == 0) return result; + + result = (result & 0x3FFF) | (uint)ReadByte() << 14; + if ((result & 0x200000) == 0) return result; + + result = (result & 0x1FFFFF) | (uint)ReadByte() << 21; + if ((result & 0x10000000) == 0) return result; + + return (result & 0xFFFFFFF) | (uint)ReadByte() << 28; + } + + public string ReadString() + { + int length = ReadEncodedInt(); + return Encoding.UTF8.GetString(ReadBytes(length)); + } + public string ReadString(int length) + { + return Encoding.UTF8.GetString(ReadBytes(length)); + } + public string ReadNullString() + { + int length = _data.Slice(Position).IndexOf((byte)0); + string value = Encoding.UTF8.GetString(_data.Slice(Position, length)); + + Position += length + 1; + return value; + } +} \ No newline at end of file diff --git a/Flazzy/IO/SpanFlashWriter.cs b/Flazzy/IO/SpanFlashWriter.cs new file mode 100644 index 0000000..dba647f --- /dev/null +++ b/Flazzy/IO/SpanFlashWriter.cs @@ -0,0 +1,151 @@ +using System.Buffers.Binary; +using System.Numerics; +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; +using System.Text; + +namespace Flazzy.IO; + +public ref struct SpanFlashWriter +{ + private readonly Span _data; + + public int Position { get; set; } + + public SpanFlashWriter(Span data) + { + _data = data; + Position = 0; + } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public void Write(byte value) => _data[Position++] = value; + + public void Write(bool value) => Write((byte)(value ? 1 : 0)); + public void Write(short value) + { + BinaryPrimitives.WriteInt16LittleEndian(_data.Slice(Position), value); + Position += sizeof(short); + } + public void Write(ushort value) + { + BinaryPrimitives.WriteUInt16LittleEndian(_data.Slice(Position), value); + Position += sizeof(ushort); + } + public void Write(int value) + { + BinaryPrimitives.WriteInt32LittleEndian(_data.Slice(Position), value); + Position += sizeof(int); + } + public void Write(uint value) + { + BinaryPrimitives.WriteUInt32LittleEndian(_data.Slice(Position), value); + Position += sizeof(uint); + } + public void Write(ulong value) + { + BinaryPrimitives.WriteUInt64LittleEndian(_data.Slice(Position), value); + Position += sizeof(ulong); + } + public void Write(double value) + { + BinaryPrimitives.WriteDoubleLittleEndian(_data.Slice(Position), value); + Position += sizeof(double); + } + public void WriteUInt24(uint value) + { + Span slice = _data.Slice(Position, 3); + slice[2] = (byte)(value >> 16 & 0xff); + slice[1] = (byte)(value >> 8 & 0xff); + slice[0] = (byte)(value & 0xff); + + Position += 3; + } + + public void WriteEncodedInt(int value) + { + WriteEncodedUInt((uint)value); + } + public void WriteEncodedUInt(uint value) + { + while (value > 0x7Fu) + { + Write((byte)(value | ~0x7Fu)); + value >>= 7; + } + Write((byte)value); + } + + public void Write(ReadOnlySpan value) + { + value.CopyTo(_data.Slice(Position)); + Position += value.Length; + } + + internal void WriteDoubleArray(ReadOnlySpan values) + { + // TODO: Investigate if this fails on platforms that don't support unaligned reads/writes.. ARM? + if (BitConverter.IsLittleEndian) + { + // Because we are on little-endian platform, we can just blit the values. + MemoryMarshal.AsBytes(values).CopyTo(_data.Slice(Position)); + } + else + { + // Execute bounds-check. + if (_data.Length - Position < values.Length * sizeof(double)) + ThrowOutOfRange(); + + ref byte destinationPtr = ref Unsafe.Add( + ref MemoryMarshal.GetReference(_data), (nint)(uint)Position); + + for (int i = 0; i < values.Length; i++) + { + // Reverse the binary representation of the double and blit it to the output + Unsafe.WriteUnaligned(ref destinationPtr, + BinaryPrimitives.ReverseEndianness( + BitConverter.DoubleToUInt64Bits(values[i]))); + + // Bump the destination pointer by element size. + destinationPtr = Unsafe.Add(ref destinationPtr, sizeof(double)); + } + } + + Position += values.Length * sizeof(double); + + [MethodImpl(MethodImplOptions.NoInlining)] + static void ThrowOutOfRange() => throw new IndexOutOfRangeException(); + } + + public void WriteString(ReadOnlySpan value) + { + // TODO: UTF8 "Encoding" 2x vs. UTF8.GetMaxByteCount -> stackalloc/rent -> encoding + WriteEncodedInt(Encoding.UTF8.GetByteCount(value)); + + int len = Encoding.UTF8.GetBytes(value, _data.Slice(Position)); + Position += len; + } + public void WriteNullString(ReadOnlySpan value) + { + int len = Encoding.UTF8.GetBytes(value, _data.Slice(Position)); + _data[Position + len] = 0; + + Position += len + 1; + } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static int GetEncodedIntSize(int value) + { + return GetEncodedUIntSize((uint)value); + } + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static int GetEncodedUIntSize(uint value) + { + // bits = (value != 0) ? 32 - CLZ(value) : 1 <=> 32 - CLZ(value | 1) + // bytes = ceil(bits / 7.0); <=> (6 + bits) / 7 + int x = 6 + 32 - BitOperations.LeadingZeroCount(value | 1); + // Division by 7 is done by (x * 37) >> 8 where 37 = ceil(256 / 7). + // This works for 0 <= x < 256 / (7 * 37 - 256), i.e. 0 <= x <= 85. + return (x * 37) >> 8; + } +} \ No newline at end of file