From e073d5f7f79db76d758b96927c9287868a14227d Mon Sep 17 00:00:00 2001 From: Nikolay Nikolov Date: Thu, 8 Feb 2024 16:48:58 +0200 Subject: [PATCH] + implemented support for the local.get, local.set and local.tee instructions in TWasmValidationStacks.Validate --- compiler/wasm32/aasmcpu.pas | 43 +++++++++++++++++++++++++++++++++++-- 1 file changed, 41 insertions(+), 2 deletions(-) diff --git a/compiler/wasm32/aasmcpu.pas b/compiler/wasm32/aasmcpu.pas index 40fbbc5ab3..45760c33e0 100644 --- a/compiler/wasm32/aasmcpu.pas +++ b/compiler/wasm32/aasmcpu.pas @@ -89,14 +89,17 @@ uses taicpu = class; + TGetLocalTypeProc = function(localidx: Integer): TWasmBasicType of object; + { TWasmValidationStacks } TWasmValidationStacks = class private FValueStack: TWasmValueStack; FCtrlStack: TWasmControlStack; + FGetLocalType: TGetLocalTypeProc; public - constructor Create; + constructor Create(AGetLocalType: TGetLocalTypeProc); destructor Destroy; override; procedure PushVal(vt: TWasmBasicType); @@ -475,8 +478,9 @@ uses { TWasmValidationStacks } - constructor TWasmValidationStacks.Create; + constructor TWasmValidationStacks.Create(AGetLocalType: TGetLocalTypeProc); begin + FGetLocalType:=AGetLocalType; FValueStack:=TWasmValueStack.Create; FCtrlStack:=TWasmControlStack.Create; end; @@ -585,6 +589,32 @@ uses end; procedure TWasmValidationStacks.Validate(a: taicpu); + + function GetLocalIndex: Integer; + begin + Result:=-1; + with a do + begin + if ops<>1 then + internalerror(2024020801); + with oper[0]^ do + case typ of + top_ref: + begin + if assigned(ref^.symbol) then + internalerror(2024020802); + if ref^.base<>NR_STACK_POINTER_REG then + internalerror(2024020803); + if ref^.index<>NR_NO then + internalerror(2024020804); + Result:=ref^.offset; + end; + top_const: + Result:=val; + end; + end; + end; + begin case a.opcode of a_nop: @@ -932,6 +962,15 @@ uses PopVal(wbt_i32); PopVal(wbt_i32); end; + a_local_get: + PushVal(FGetLocalType(GetLocalIndex)); + a_local_set: + PopVal(FGetLocalType(GetLocalIndex)); + a_local_tee: + begin + PopVal(FGetLocalType(GetLocalIndex)); + PushVal(FGetLocalType(GetLocalIndex)); + end; else internalerror(2024030502); end;