Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cleanup #30

Merged
merged 4 commits into from
Nov 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 38 additions & 32 deletions Wacs.Core/Instructions/Control.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@
{
private static readonly ByteCode BlockOp = OpCode.Block;
public override ByteCode Op => BlockOp;
private Block Block;

Check warning on line 74 in Wacs.Core/Instructions/Control.cs

View workflow job for this annotation

GitHub Actions / build-and-test

Non-nullable field 'Block' must contain a non-null value when exiting constructor. Consider adding the 'required' modifier or declaring the field as nullable.

public BlockType Type => Block.Type;

Expand All @@ -86,10 +86,11 @@
try
{
var funcType = context.Types.ResolveBlockType(Block.Type);
context.Assert(funcType, $"Invalid BlockType: {Block.Type}");
context.Assert(funcType, "Invalid BlockType: {0}",Block.Type);

//Check the parameters [t1*] and discard
context.OpStack.PopValues(funcType.ParameterTypes);
context.OpStack.PopValues(funcType.ParameterTypes, ref _aside);
_aside.Clear();

//ControlStack will push the values back on (Control Frame is our Label)
context.PushControlFrame(BlockOp, funcType);
Expand All @@ -102,7 +103,7 @@
_ = exc;
//Types didn't hit
context.Assert(false,
$"Instruction block was invalid. BlockType {Block.Type} did not exist in the Context.");
"Instruction block was invalid. BlockType {0} did not exist in the Context.",Block.Type);
}
}

Expand Down Expand Up @@ -162,10 +163,11 @@
try
{
var funcType = context.Types.ResolveBlockType(Block.Type);
context.Assert(funcType, $"Invalid BlockType: {Block.Type}");
context.Assert(funcType, "Invalid BlockType: {0}",Block.Type);

//Check the parameters [t1*] and discard
context.OpStack.PopValues(funcType.ParameterTypes);
context.OpStack.PopValues(funcType.ParameterTypes, ref _aside);
_aside.Clear();

//ControlStack will push the values back on (Control Frame is our Label)
context.PushControlFrame(LoopOp, funcType);
Expand All @@ -178,7 +180,7 @@
_ = exc;
//Types didn't hit
context.Assert(false,
$"Instruciton loop invalid. BlockType {Block.Type} did not exist in the Context.");
"Instruction loop invalid. BlockType {0} did not exist in the Context.",Block.Type);
}
}

Expand Down Expand Up @@ -241,13 +243,14 @@
try
{
var ifType = context.Types.ResolveBlockType(IfBlock.Type);
context.Assert(ifType, $"Invalid BlockType: {IfBlock.Type}");
context.Assert(ifType, "Invalid BlockType: {0}",IfBlock.Type);

//Pop the predicate
context.OpStack.PopI32();

//Check the parameters [t1*] and discard
context.OpStack.PopValues(ifType.ParameterTypes);
context.OpStack.PopValues(ifType.ParameterTypes, ref _aside);
_aside.Clear();

//ControlStack will push the values back on (Control Frame is our Label)
context.PushControlFrame(IfOp, ifType);
Expand All @@ -273,7 +276,7 @@
_ = exc;
//Types didn't hit
context.Assert(false,
$"Instruciton loop invalid. BlockType {IfBlock.Type} did not exist in the Context.");
"Instruction loop invalid. BlockType {0} did not exist in the Context.",IfBlock.Type);
}
}

Expand Down Expand Up @@ -345,8 +348,7 @@
public override void Validate(IWasmValidationContext context)
{
var frame = context.PopControlFrame();
context.Assert(frame.Opcode == OpCode.If,
"Else terminated a non-If block");
context.Assert(frame.Opcode == OpCode.If, "Else terminated a non-If block");
context.PushControlFrame(ElseOp, frame.Types);
}
}
Expand Down Expand Up @@ -435,17 +437,18 @@
public override ByteCode Op => OpCode.Br;

private LabelIdx L;

// @Spec 3.3.8.6. br l
public override void Validate(IWasmValidationContext context)
{
context.Assert(context.ContainsLabel(L.Value),
$"Instruction br invalid. Could not branch to label {L}");
"Instruction br invalid. Could not branch to label {0}",L);

var nthFrame = context.ControlStack.PeekAt((int)L.Value);

//Validate results, but leave them on the stack
context.OpStack.PopValues(nthFrame.LabelTypes);
context.OpStack.PopValues(nthFrame.LabelTypes, ref _aside);
_aside.Clear();

// if (!context.Unreachable)
// nthFrame.ConditionallyReachable = true;
Expand Down Expand Up @@ -530,12 +533,12 @@
public override ByteCode Op => OpCode.BrIf;

public LabelIdx L;

// @Spec 3.3.8.7. br_if
public override void Validate(IWasmValidationContext context)
{
context.Assert(context.ContainsLabel(L.Value),
$"Instruction br_if invalid. Could not branch to label {L}");
"Instruction br_if invalid. Could not branch to label {0}",L);

//Pop the predicate
context.OpStack.PopI32();
Expand All @@ -545,7 +548,7 @@
// nthFrame.ConditionallyReachable = true;

//Pop values like we branch
context.OpStack.PopValues(nthFrame.LabelTypes);
context.OpStack.PopValues(nthFrame.LabelTypes, ref _aside);
//But actually, we don't, so push them back on.
context.OpStack.PushResult(nthFrame.LabelTypes);
}
Expand Down Expand Up @@ -598,7 +601,7 @@
//Pop the switch
context.OpStack.PopI32();
context.Assert(context.ContainsLabel(Ln.Value),
$"Instruction br_table invalid. Context did not contain Label {Ln}");
"Instruction br_table invalid. Context did not contain Label {0}", Ln);

var mthFrame = context.ControlStack.PeekAt((int)Ln.Value);
var arity = mthFrame.LabelTypes.Arity;
Expand All @@ -609,20 +612,21 @@
foreach (var lidx in Ls)
{
context.Assert(context.ContainsLabel(lidx.Value),
$"Instruction br_table invalid. Context did not contain Label {lidx}");
"Instruction br_table invalid. Context did not contain Label {0}", lidx);

var nthFrame = context.ControlStack.PeekAt((int)lidx.Value);
context.Assert(nthFrame.LabelTypes.Arity == arity,
$"Instruction br_table invalid. Label {lidx} had different arity {nthFrame.LabelTypes.Arity} =/= {arity}");
"Instruction br_table invalid. Label {0} had different arity {1} =/= {2}", lidx, nthFrame.LabelTypes.Arity,arity);

// if (!context.Unreachable)
// nthFrame.ConditionallyReachable = true;

var vals = context.OpStack.PopValues(nthFrame.LabelTypes);
context.OpStack.PushValues(vals);
context.OpStack.PopValues(nthFrame.LabelTypes, ref _aside);
context.OpStack.PushValues(_aside);
}

context.OpStack.PopValues(mthFrame.LabelTypes);
context.OpStack.PopValues(mthFrame.LabelTypes, ref _aside);
_aside.Clear();
context.SetUnreachable();
}

Expand Down Expand Up @@ -705,13 +709,13 @@
{
public static readonly InstReturn Inst = new();
public override ByteCode Op => OpCode.Return;

// @Spec 3.3.8.9. return
public override void Validate(IWasmValidationContext context)
{
//keep the results for the block or function to validate
var vals = context.OpStack.PopValues(context.ReturnType);
context.OpStack.PushValues(vals);
context.OpStack.PopValues(context.ReturnType, ref _aside);
context.OpStack.PushValues(_aside);
context.SetUnreachable();
}

Expand Down Expand Up @@ -750,10 +754,11 @@
public override void Validate(IWasmValidationContext context)
{
context.Assert(context.Funcs.Contains(X),
$"Instruction call was invalid. Function {X} was not in the Context.");
"Instruction call was invalid. Function {0} was not in the Context.",X);
var func = context.Funcs[X];
var type = context.Types[func.TypeIndex];
context.OpStack.PopValues(type.ParameterTypes);
context.OpStack.PopValues(type.ParameterTypes, ref _aside);
_aside.Clear();
context.OpStack.PushResult(type.ResultType);
}

Expand Down Expand Up @@ -851,16 +856,17 @@
public override void Validate(IWasmValidationContext context)
{
context.Assert(context.Tables.Contains(X),
$"Instruction call_indirect was invalid. Table {X} was not in the Context.");
"Instruction call_indirect was invalid. Table {0} was not in the Context.",X);
var tableType = context.Tables[X];
context.Assert(tableType.ElementType == ReferenceType.Funcref,
$"Instruction call_indirect was invalid. Table type was not funcref");
"Instruction call_indirect was invalid. Table type was not funcref");
context.Assert(context.Types.Contains(Y),
$"Instruction call_indirect was invalid. Function type {Y} was not in the Context.");
"Instruction call_indirect was invalid. Function type {0} was not in the Context.",Y);
var funcType = context.Types[Y];

context.OpStack.PopI32();
context.OpStack.PopValues(funcType.ParameterTypes);
context.OpStack.PopValues(funcType.ParameterTypes, ref _aside);
_aside.Clear();
context.OpStack.PushResult(funcType.ResultType);
}

Expand Down
6 changes: 3 additions & 3 deletions Wacs.Core/Instructions/GlobalVariable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public override string RenderText(ExecContext? context)
public override void Validate(IWasmValidationContext context)
{
context.Assert(context.Globals.Contains(Index),
$"Instruction global.get was invalid. Context Globals did not contain {Index}");
"Instruction global.get was invalid. Context Globals did not contain {0}",Index);
var globalType = context.Globals[Index].Type;
context.OpStack.PushType(globalType.ContentType);
}
Expand Down Expand Up @@ -132,11 +132,11 @@ public override string RenderText(ExecContext? context)
public override void Validate(IWasmValidationContext context)
{
context.Assert(context.Globals.Contains(Index),
$"Instruction global.set was invalid. Context Globals did not contain {Index}");
"Instruction global.set was invalid. Context Globals did not contain {0}",Index);
var global = context.Globals[Index];
var mut = global.Type.Mutability;
context.Assert(mut == Mutability.Mutable,
$"Instruction global.set was invalid. Trying to set immutable global {Index}");
"Instruction global.set was invalid. Trying to set immutable global {0}",Index);
context.OpStack.PopType(global.Type.ContentType);

}
Expand Down
3 changes: 3 additions & 0 deletions Wacs.Core/Instructions/InstructionBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
// * limitations under the License.
// */

using System.Collections.Generic;
using System.IO;
using Wacs.Core.OpCodes;
using Wacs.Core.Runtime;
Expand Down Expand Up @@ -50,5 +51,7 @@ public abstract class InstructionBase : IInstruction
public virtual IInstruction Parse(BinaryReader reader) => this;

public virtual string RenderText(ExecContext? context) => Op.GetMnemonic();

protected static Stack<Value> _aside = new();
}
}
6 changes: 3 additions & 3 deletions Wacs.Core/Instructions/LocalVariable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@
public override void Validate(IWasmValidationContext context)
{
context.Assert(context.Locals.Contains(Index),
$"Instruction local.get was invalid. Context Locals did not contain {Index}");
"Instruction local.get was invalid. Context Locals did not contain {0}",Index);
var value = context.Locals.Get(Index);
context.OpStack.PushType(value.Type);
}
Expand All @@ -82,7 +82,7 @@
$"Instruction local.get could not get Local {Index}");
//3.
// var value = context.Frame.Locals.Get(Index);
var value = context.Frame.Locals.Data[Index.Value];

Check warning on line 85 in Wacs.Core/Instructions/LocalVariable.cs

View workflow job for this annotation

GitHub Actions / build-and-test

Dereference of a possibly null reference.
return value;
}

Expand Down Expand Up @@ -126,7 +126,7 @@
public override void Validate(IWasmValidationContext context)
{
context.Assert(context.Locals.Contains(Index),
$"Instruction local.set was invalid. Context Locals did not contain {Index}");
"Instruction local.set was invalid. Context Locals did not contain {0}",Index);
var value = context.Locals.Get(Index);
context.OpStack.PopType(value.Type);
}
Expand All @@ -141,7 +141,7 @@
context.Assert( context.OpStack.HasValue,
$"Operand Stack underflow in instruction local.set");
// var localValue = context.Frame.Locals.Get(Index);
var localValue = context.Frame.Locals.Data[Index.Value];

Check warning on line 144 in Wacs.Core/Instructions/LocalVariable.cs

View workflow job for this annotation

GitHub Actions / build-and-test

Dereference of a possibly null reference.
var type = localValue.Type;
//4.
var value = context.OpStack.PopType(type);
Expand All @@ -153,7 +153,7 @@
{
//5.
// context.Frame.Locals.Set(Index, value);
context.Frame.Locals.Data[Index.Value] = value;

Check warning on line 156 in Wacs.Core/Instructions/LocalVariable.cs

View workflow job for this annotation

GitHub Actions / build-and-test

Dereference of a possibly null reference.
}

public Action<ExecContext, Value> GetFunc => SetLocal;
Expand Down Expand Up @@ -191,7 +191,7 @@
public override void Validate(IWasmValidationContext context)
{
context.Assert(context.Locals.Contains(Index),
$"Instruction local.tee was invalid. Context Locals did not contain {Index}");
"Instruction local.tee was invalid. Context Locals did not contain {0}",Index);
var value = context.Locals.Get(Index);
context.OpStack.PopType(value.Type);
context.OpStack.PushType(value.Type);
Expand All @@ -206,7 +206,7 @@
context.Assert( context.OpStack.HasValue,
$"Operand Stack underflow in instruction local.tee");
// var localValue = context.Frame.Locals.Get(Index);
var localValue = context.Frame.Locals.Data[Index.Value];

Check warning on line 209 in Wacs.Core/Instructions/LocalVariable.cs

View workflow job for this annotation

GitHub Actions / build-and-test

Dereference of a possibly null reference.
//2.
var value = context.OpStack.PopType(localValue.Type);
//3.
Expand Down
9 changes: 4 additions & 5 deletions Wacs.Core/Instructions/Memory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,9 @@ public InstMemoryLoad(ValType type, BitWidth width) =>
public override void Validate(IWasmValidationContext context)
{
context.Assert(context.Mems.Contains(M.M),
$"Instruction {Op.GetMnemonic()} failed with invalid context memory 0.");
"Instruction {0} failed with invalid context memory 0.",Op.GetMnemonic());
context.Assert(M.Align.LinearSize() <= WidthT.ByteSize(),
$"Instruction {Op.GetMnemonic()} failed with invalid alignment {M.Align.LinearSize()} <= {WidthT}/8");
"Instruction {0} failed with invalid alignment {1} <= {2}/8",Op.GetMnemonic(),M.Align.LinearSize(),WidthT);

context.OpStack.PopI32();
context.OpStack.PushType(Type);
Expand Down Expand Up @@ -249,10 +249,9 @@ public IInstruction Immediate(MemArg m)
public override void Validate(IWasmValidationContext context)
{
context.Assert(context.Mems.Contains(M.M),
$"Instruction {Op.GetMnemonic()} failed with invalid context memory 0.");
"Instruction {0} failed with invalid context memory 0.",Op.GetMnemonic());
context.Assert(M.Align.LinearSize() <= TWidth.ByteSize(),

$"Instruction {Op.GetMnemonic()} failed with invalid alignment {M.Align.LinearSize()} <= {TWidth}/8");
"Instruction {0} failed with invalid alignment {1} <= {2}/8",Op.GetMnemonic(),M.Align.LinearSize(),TWidth);

//Pop parameters from right to left
context.OpStack.PopType(Type);
Expand Down
16 changes: 8 additions & 8 deletions Wacs.Core/Instructions/MemoryBulk.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public class InstMemorySize : InstructionBase
public override void Validate(IWasmValidationContext context)
{
context.Assert(context.Mems.Contains(M),
$"Instruction {Op.GetMnemonic()} failed with invalid context memory {M}.");
"Instruction {0} failed with invalid context memory {1}.",Op.GetMnemonic(),M);
context.OpStack.PushI32();
}

Expand Down Expand Up @@ -81,7 +81,7 @@ public class InstMemoryGrow : InstructionBase
public override void Validate(IWasmValidationContext context)
{
context.Assert(context.Mems.Contains(M),
$"Instruction {Op.GetMnemonic()} failed with invalid context memory {M}.");
"Instruction {0} failed with invalid context memory {1}.",Op.GetMnemonic(),M);
context.OpStack.PopI32();
context.OpStack.PushI32();
}
Expand Down Expand Up @@ -140,10 +140,10 @@ public class InstMemoryInit : InstructionBase
public override void Validate(IWasmValidationContext context)
{
context.Assert(context.Mems.Contains(Y),
$"Instruction {Op.GetMnemonic()} failed with invalid context memory {Y}.");
"Instruction {0} failed with invalid context memory {1}.",Op.GetMnemonic(),Y);

context.Assert(context.Datas.Contains(X),
$"Instruction {Op.GetMnemonic()} failed with invalid context data {X}.");
"Instruction {0} failed with invalid context data {1}.",Op.GetMnemonic(), X);

context.OpStack.PopI32();
context.OpStack.PopI32();
Expand Down Expand Up @@ -256,7 +256,7 @@ public class InstDataDrop : InstructionBase
public override void Validate(IWasmValidationContext context)
{
context.Assert(context.Datas.Contains(X),
$"Instruction {Op.GetMnemonic()} failed with invalid context data {X}.");
"Instruction {0} failed with invalid context data {1}.",Op.GetMnemonic(),X);
}

// @Spec 4.4.7.13
Expand Down Expand Up @@ -303,9 +303,9 @@ public class InstMemoryCopy : InstructionBase
public override void Validate(IWasmValidationContext context)
{
context.Assert(context.Mems.Contains(SrcX),
$"Instruction {Op.GetMnemonic()} failed with invalid context memory {SrcX}.");
"Instruction {0} failed with invalid context memory {1}.",Op.GetMnemonic(),SrcX);
context.Assert(context.Mems.Contains(DstY),
$"Instruction {Op.GetMnemonic()} failed with invalid context memory {DstY}.");
"Instruction {0} failed with invalid context memory {1}.",Op.GetMnemonic(), DstY);

context.OpStack.PopI32();
context.OpStack.PopI32();
Expand Down Expand Up @@ -426,7 +426,7 @@ public class InstMemoryFill : InstructionBase
public override void Validate(IWasmValidationContext context)
{
context.Assert(context.Mems.Contains(X),
$"Instruction {Op.GetMnemonic()} failed with invalid context memory {X}.");
"Instruction {0} failed with invalid context memory {1}.",Op.GetMnemonic(),X);

context.OpStack.PopI32();
context.OpStack.PopI32();
Expand Down
5 changes: 2 additions & 3 deletions Wacs.Core/Instructions/Parametric.cs
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,7 @@ public override void Validate(IWasmValidationContext context)
{
if (WithTypes)
{
context.Assert(Types.Length == 1,
$"Select instruction type must be of length 1");
context.Assert(Types.Length == 1, "Select instruction type must be of length 1");
var type = Types[0];
context.OpStack.PopI32();
context.OpStack.PopType(type);
Expand All @@ -84,7 +83,7 @@ public override void Validate(IWasmValidationContext context)
Value val2 = context.OpStack.PopAny();
Value val1 = context.OpStack.PopAny();
context.Assert(val1.Type.IsCompatible(val2.Type),
$"Select instruction expected matching types on the stack: {val1.Type} == {val2.Type}");
"Select instruction expected matching types on the stack: {0} == {1}",val1.Type,val2.Type);

if (!context.Attributes.Configure_RefTypes)
{
Expand Down
Loading
Loading