Skip to content

Commit 1665c1c

Browse files
authored
Merge pull request #56 from kelnishi/perfpass
Performance Pass
2 parents 2844660 + 7427a1f commit 1665c1c

74 files changed

Lines changed: 918 additions & 853 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

Wacs.Core/Instructions/Control.cs

Lines changed: 72 additions & 94 deletions
Large diffs are not rendered by default.

Wacs.Core/Instructions/Exceptions.cs

Lines changed: 13 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -29,14 +29,12 @@ namespace Wacs.Core.Instructions
2929
{
3030
public class InstTryTable : BlockTarget, IBlockInstruction, IExnHandler
3131
{
32-
private static readonly ByteCode TryTableOp = OpCode.TryTable;
32+
public InstTryTable() : base(ByteCode.TryTable) { }
33+
34+
private Block Block = null!;
35+
public CatchType[] Catches = null!;
36+
public BlockTarget?[] CatchTargets = null!;
3337

34-
private static readonly ByteCode CatchOp = WacsCode.Catch;
35-
private Block Block;
36-
public CatchType[] Catches;
37-
public BlockTarget?[] CatchTargets;
38-
39-
public override ByteCode Op => TryTableOp;
4038
public ValType BlockType => Block.BlockType;
4139
public int Count => 1;
4240
public int BlockSize => 1 + Block.Size;
@@ -60,7 +58,7 @@ public override void Validate(IWasmValidationContext context)
6058
"Catch Label {0} does not exist in the context.", handler.L);
6159
var labelFrame = context.ControlStack.PeekAt((int)handler.L.Value);
6260
var labelType = new FunctionType(ResultType.Empty, labelFrame.EndTypes);
63-
context.PushControlFrame(CatchOp, labelType);
61+
context.PushControlFrame(ByteCode.Catch, labelType);
6462
switch (handler.Mode)
6563
{
6664
case CatchFlags.None: //catch x
@@ -90,7 +88,7 @@ public override void Validate(IWasmValidationContext context)
9088
}
9189

9290
//ControlStack will push the values back on (Control Frame is our Label)
93-
context.PushControlFrame(TryTableOp, funcType);
91+
context.PushControlFrame(ByteCode.TryTable, funcType);
9492
context.ValidateBlock(Block);
9593
}
9694
catch (IndexOutOfRangeException exc)
@@ -106,7 +104,6 @@ public override InstructionBase Link(ExecContext context, int pointer)
106104
{
107105
_ = base.Link(context, pointer);
108106
CatchTargets = Catches.Select(catchType => InstBranch.PrecomputeStack(context, catchType.L + 1)).ToArray();
109-
Nop = true;
110107
return this;
111108
}
112109

@@ -129,8 +126,9 @@ public override InstructionBase Parse(BinaryReader reader)
129126

130127
public class InstThrow : InstructionBase
131128
{
129+
public InstThrow() : base(ByteCode.Throw) { }
130+
132131
private TagIdx X;
133-
public override ByteCode Op => OpCode.Throw;
134132

135133
public override void Validate(IWasmValidationContext context)
136134
{
@@ -157,11 +155,8 @@ public override InstructionBase Link(ExecContext context, int pointer)
157155
var compType = tagType.Expansion;
158156
var funcType = compType as FunctionType;
159157

160-
int stack = context.LinkOpStackHeight;
161-
context.LinkOpStackHeight -= funcType!.ParameterTypes.Arity;
162-
context.LinkOpStackHeight += 1;
163-
//For recordkeeping
164-
StackDiff = context.LinkOpStackHeight - stack;
158+
int stackDiff = +1 -funcType!.ParameterTypes.Arity;
159+
context.DeltaStack(stackDiff, 0);
165160

166161
context.LinkUnreachable = true;
167162
return this;
@@ -208,20 +203,18 @@ public override InstructionBase Parse(BinaryReader reader)
208203

209204
public class InstThrowRef : InstructionBase
210205
{
211-
public override ByteCode Op => OpCode.ThrowRef;
206+
public InstThrowRef() : base(ByteCode.ThrowRef, -1) { }
212207

213208
public override void Validate(IWasmValidationContext context)
214209
{
215210
context.OpStack.PopType(ValType.Exn);
216211
context.SetUnreachable();
217212
}
218213

219-
public override int StackDiff => -1;
220214
public override InstructionBase Link(ExecContext context, int pointer)
221215
{
222-
_ = base.Link(context, pointer);
223216
context.LinkUnreachable = true;
224-
return this;
217+
return base.Link(context, pointer);
225218
}
226219

227220
public override void Execute(ExecContext context)

Wacs.Core/Instructions/GC/Array.cs

Lines changed: 40 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,10 @@ namespace Wacs.Core.Instructions.GC
2828
{
2929
public class InstArrayNew : InstructionBase, IConstInstruction
3030
{
31+
public InstArrayNew() : base(ByteCode.ArrayNew, -1) { }
32+
3133
private TypeIdx X;
32-
public override ByteCode Op => GcCode.ArrayNew;
33-
public override int StackDiff => -1;
34-
34+
3535
public override void Validate(IWasmValidationContext context)
3636
{
3737
context.Assert(context.Types.Contains(X),
@@ -99,8 +99,9 @@ public override InstructionBase Parse(BinaryReader reader)
9999

100100
public class InstArrayNewDefault : InstructionBase, IConstInstruction
101101
{
102+
public InstArrayNewDefault() : base(ByteCode.ArrayNewDefault) { }
103+
102104
private TypeIdx X;
103-
public override ByteCode Op => GcCode.ArrayNewDefault;
104105

105106
public override void Validate(IWasmValidationContext context)
106107
{
@@ -157,10 +158,10 @@ public override InstructionBase Parse(BinaryReader reader)
157158

158159
public class InstArrayNewFixed : InstructionBase, IConstInstruction
159160
{
161+
public InstArrayNewFixed() : base(ByteCode.ArrayNewFixed) { }
162+
160163
private uint N;
161164
private TypeIdx X;
162-
public override ByteCode Op => GcCode.ArrayNewFixed;
163-
public override int StackDiff => -((int)N-1);
164165

165166
public override void Validate(IWasmValidationContext context)
166167
{
@@ -184,6 +185,13 @@ public override void Validate(IWasmValidationContext context)
184185
context.OpStack.PushType(resultType); // -(N-1)
185186
}
186187

188+
public override InstructionBase Link(ExecContext context, int pointer)
189+
{
190+
int stackDiff = +1 -(int)N;
191+
context.DeltaStack(stackDiff);
192+
return this;
193+
}
194+
187195
public override void Execute(ExecContext context)
188196
{
189197
//2
@@ -222,12 +230,11 @@ public override InstructionBase Parse(BinaryReader reader)
222230

223231
public class InstArrayNewData : InstructionBase
224232
{
233+
public InstArrayNewData() : base(ByteCode.ArrayNewData, -1) { }
234+
225235
private TypeIdx X;
226236
private DataIdx Y;
227237

228-
public override ByteCode Op => GcCode.ArrayNewData;
229-
public override int StackDiff => -1;
230-
231238
public override void Validate(IWasmValidationContext context)
232239
{
233240
context.Assert(context.Types.Contains(X),
@@ -334,12 +341,11 @@ public override InstructionBase Parse(BinaryReader reader)
334341

335342
public class InstArrayNewElem : InstructionBase
336343
{
344+
public InstArrayNewElem() : base(ByteCode.ArrayNewElem, -1) { }
345+
337346
private TypeIdx X;
338347
private ElemIdx Y;
339348

340-
public override ByteCode Op => GcCode.ArrayNewElem;
341-
public override int StackDiff => -1;
342-
343349
public override void Validate(IWasmValidationContext context)
344350
{
345351
context.Assert(context.Types.Contains(X),
@@ -424,21 +430,17 @@ public class InstArrayGet : InstructionBase
424430
private readonly PackedExt Sx;
425431
private TypeIdx X;
426432

427-
public InstArrayGet(PackedExt sx)
428-
{
429-
Sx = sx;
430-
}
433+
public InstArrayGet(PackedExt sx) : base(GetOp(sx), -1)
434+
=> Sx = sx;
431435

432-
public override ByteCode Op => Sx switch
436+
private static ByteCode GetOp(PackedExt sx) => sx switch
433437
{
434-
PackedExt.Signed => GcCode.ArrayGetS,
435-
PackedExt.NotPacked => GcCode.ArrayGet,
436-
PackedExt.Unsigned => GcCode.ArrayGetU,
437-
_ => throw new InvalidDataException($"Undefined packedtype: {Sx}")
438+
PackedExt.Signed => ByteCode.ArrayGetS,
439+
PackedExt.NotPacked => ByteCode.ArrayGet,
440+
PackedExt.Unsigned => ByteCode.ArrayGetU,
441+
_ => throw new InvalidDataException($"Undefined packedtype: {sx}")
438442
};
439443

440-
public override int StackDiff => -1;
441-
442444
/// <summary>
443445
/// https://webassembly.github.io/gc/core/bikeshed/index.html#-hrefsyntax-instr-structmathsfstructgetmathsf_hrefsyntax-sxmathitsxxy
444446
/// </summary>
@@ -532,11 +534,10 @@ public override InstructionBase Parse(BinaryReader reader)
532534

533535
public class InstArraySet : InstructionBase
534536
{
537+
public InstArraySet() : base(ByteCode.ArraySet, -3) { }
538+
535539
private TypeIdx X;
536-
537-
public override ByteCode Op => GcCode.ArraySet;
538-
public override int StackDiff => -3;
539-
540+
540541
public override void Validate(IWasmValidationContext context)
541542
{
542543
context.Assert(context.Types.Contains(X),
@@ -617,7 +618,7 @@ public override InstructionBase Parse(BinaryReader reader)
617618

618619
public class InstArrayLen : InstructionBase
619620
{
620-
public override ByteCode Op => GcCode.ArrayLen;
621+
public InstArrayLen() : base(ByteCode.ArrayLen) { }
621622

622623
public override void Validate(IWasmValidationContext context)
623624
{
@@ -653,10 +654,10 @@ public override void Execute(ExecContext context)
653654

654655
public class InstArrayFill : InstructionBase
655656
{
657+
public InstArrayFill() : base(ByteCode.ArrayFill, -4) { }
658+
656659
private TypeIdx X;
657-
public override ByteCode Op => GcCode.ArrayFill;
658-
public override int StackDiff => -4;
659-
660+
660661
public override void Validate(IWasmValidationContext context)
661662
{
662663
context.Assert(context.Types.Contains(X),
@@ -734,11 +735,11 @@ public override InstructionBase Parse(BinaryReader reader)
734735

735736
public class InstArrayCopy : InstructionBase
736737
{
738+
public InstArrayCopy() : base(ByteCode.ArrayCopy, -5) { }
739+
737740
private TypeIdx X; //dest
738741
private TypeIdx Y; //src
739-
public override ByteCode Op => GcCode.ArrayCopy;
740-
public override int StackDiff => -5;
741-
742+
742743
public override void Validate(IWasmValidationContext context)
743744
{
744745
context.Assert(context.Types.Contains(X),
@@ -862,12 +863,11 @@ public override InstructionBase Parse(BinaryReader reader)
862863

863864
public class InstArrayInitData : InstructionBase
864865
{
866+
public InstArrayInitData() : base(ByteCode.ArrayInitData, -4) { }
867+
865868
private TypeIdx X;
866869
private DataIdx Y;
867-
868-
public override ByteCode Op => GcCode.ArrayInitData;
869-
public override int StackDiff => -4;
870-
870+
871871
/// <summary>
872872
/// https://webassembly.github.io/gc/core/bikeshed/index.html#-hrefsyntax-instr-arraymathsfarrayinit_dataxy
873873
/// </summary>
@@ -993,12 +993,11 @@ public override InstructionBase Parse(BinaryReader reader)
993993

994994
public class InstArrayInitElem : InstructionBase
995995
{
996+
public InstArrayInitElem() : base(ByteCode.ArrayInitElem, -4) { }
997+
996998
private TypeIdx X;
997999
private ElemIdx Y;
9981000

999-
public override ByteCode Op => GcCode.ArrayInitElem;
1000-
public override int StackDiff => -4;
1001-
10021001
/// <summary>
10031002
/// https://webassembly.github.io/gc/core/bikeshed/index.html#-hrefsyntax-instr-arraymathsfarrayinit_elemxy
10041003
/// </summary>

Wacs.Core/Instructions/GC/Control.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,15 @@ namespace Wacs.Core.Instructions.GC
2424
{
2525
public class InstBrOnCast : InstructionBase
2626
{
27+
public InstBrOnCast() : base(ByteCode.BrOnCast) { }
28+
2729
public CastFlags Flags;
2830
public LabelIdx L;
2931
private BlockTarget? LinkedLabel;
3032

3133
private ValType Rt1;
3234
private ValType Rt2;
33-
34-
public override ByteCode Op => GcCode.BrOnCast;
35-
35+
3636
public override void Validate(IWasmValidationContext context)
3737
{
3838
context.Assert(context.ContainsLabel(L.Value),
@@ -95,15 +95,15 @@ public override InstructionBase Parse(BinaryReader reader)
9595

9696
public class InstBrOnCastFail : InstructionBase
9797
{
98+
public InstBrOnCastFail() : base(ByteCode.BrOnCastFail) { }
99+
98100
public CastFlags Flags;
99101
public LabelIdx L;
100102
private BlockTarget? LinkedLabel;
101103

102104
private ValType Rt1;
103105
private ValType Rt2;
104106

105-
public override ByteCode Op => GcCode.BrOnCastFail;
106-
107107
public override void Validate(IWasmValidationContext context)
108108
{
109109
context.Assert(context.ContainsLabel(L.Value),

Wacs.Core/Instructions/GC/ConvertExtern.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ namespace Wacs.Core.Instructions.GC
2222
{
2323
public class InstAnyConvertExtern : InstructionBase, IConstInstruction
2424
{
25-
public override ByteCode Op => GcCode.AnyConvertExtern;
25+
public InstAnyConvertExtern() : base(ByteCode.AnyConvertExtern) { }
2626

2727
/// <summary>
2828
/// https://webassembly.github.io/gc/core/bikeshed/index.html#-hrefsyntax-instr-externmathsfanyconvert_extern
@@ -62,8 +62,8 @@ public override void Execute(ExecContext context)
6262

6363
public class InstExternConvertAny : InstructionBase, IConstInstruction
6464
{
65-
public override ByteCode Op => GcCode.AnyConvertExtern;
66-
65+
public InstExternConvertAny() : base(ByteCode.ExternConvertAny) { }
66+
6767
/// <summary>
6868
/// https://webassembly.github.io/gc/core/bikeshed/index.html#-hrefsyntax-instr-externmathsfexternconvert_any
6969
/// </summary>

Wacs.Core/Instructions/GC/I31.cs

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,11 @@ namespace Wacs.Core.Instructions.GC
2323
{
2424
public class InstRefI31 : InstructionBase, IConstInstruction
2525
{
26+
public InstRefI31() : base(ByteCode.RefI31) { }
27+
2628
private const int SignBit31 = 0x4000_0000;
2729
private const int UnsignedMask = 0x3FFF_FFFF;
2830
private const ulong SignExtendBits = 0xFFFF_FFFF_C000_0000;
29-
public override ByteCode Op => GcCode.RefI31;
3031

3132
/// <summary>
3233
/// https://webassembly.github.io/gc/core/bikeshed/index.html#-hrefsyntax-instr-i31mathsfrefi31
@@ -61,12 +62,13 @@ public override void Execute(ExecContext context)
6162
}
6263
}
6364

64-
public class InstI32GetS : InstructionBase
65+
public class InstI31GetS : InstructionBase
6566
{
67+
public InstI31GetS() : base(ByteCode.I31GetS) { }
68+
6669
private const int SignBit31 = 0x4000_0000;
6770
private const int UnsignedMask = 0x3FFF_FFFF;
68-
public override ByteCode Op => GcCode.I31GetS;
69-
71+
7072
public override void Validate(IWasmValidationContext context)
7173
{
7274
context.OpStack.PopType(ValType.I31); // -1
@@ -96,11 +98,12 @@ public override void Execute(ExecContext context)
9698
}
9799
}
98100

99-
public class InstI32GetU : InstructionBase
101+
public class InstI31GetU : InstructionBase
100102
{
103+
public InstI31GetU() : base(ByteCode.I31GetU) { }
104+
101105
private const uint BitMask31 = 0x7FFF_FFFF;
102-
public override ByteCode Op => GcCode.I31GetU;
103-
106+
104107
public override void Validate(IWasmValidationContext context)
105108
{
106109
context.OpStack.PopType(ValType.I31); // -1

0 commit comments

Comments
 (0)