From cdba427bcf82e1c7af5a47b132f17550be4a473b Mon Sep 17 00:00:00 2001 From: Nikolay Nikolov Date: Tue, 18 Oct 2022 04:05:14 +0300 Subject: [PATCH] + added WASI multithreading helper for setting the stack pointer for the linear stack --- compiler/wasm32/ccpuinnr.inc | 4 +++- compiler/wasm32/hlcgcpu.pas | 3 ++- compiler/wasm32/nwasminl.pas | 31 ++++++++++++++++++++++++++++++- rtl/wasi/systhrd.inc | 7 ++++++- rtl/wasm32/cpuh.inc | 1 + rtl/wasm32/cpuinnr.inc | 1 + 6 files changed, 43 insertions(+), 4 deletions(-) diff --git a/compiler/wasm32/ccpuinnr.inc b/compiler/wasm32/ccpuinnr.inc index 6c4a03f929..c35d8b9f5e 100644 --- a/compiler/wasm32/ccpuinnr.inc +++ b/compiler/wasm32/ccpuinnr.inc @@ -97,5 +97,7 @@ in_wasm32_tls_size = in_cpu_first+73, in_wasm32_tls_align = in_cpu_first+74, - in_wasm32_tls_base = in_cpu_first+75 + in_wasm32_tls_base = in_cpu_first+75, + + in_wasm32_set_base_pointer = in_cpu_first+76 diff --git a/compiler/wasm32/hlcgcpu.pas b/compiler/wasm32/hlcgcpu.pas index 1e4a740072..627f825034 100644 --- a/compiler/wasm32/hlcgcpu.pas +++ b/compiler/wasm32/hlcgcpu.pas @@ -2052,7 +2052,8 @@ implementation g_procdef(list,pd); ttgwasm(tg).allocframepointer(list,pd.frame_pointer_ref); - ttgwasm(tg).allocbasepointer(list,pd.base_pointer_ref); + if pd.base_pointer_ref.base<>NR_LOCAL_STACK_POINTER_REG then + ttgwasm(tg).allocbasepointer(list,pd.base_pointer_ref); g_fingerprint(list); diff --git a/compiler/wasm32/nwasminl.pas b/compiler/wasm32/nwasminl.pas index c5e5878ce9..9a8d59d79c 100644 --- a/compiler/wasm32/nwasminl.pas +++ b/compiler/wasm32/nwasminl.pas @@ -59,6 +59,7 @@ interface procedure second_atomic_rmw_x_y(op: TAsmOp); procedure second_atomic_rmw_x_y_z(op: TAsmOp); procedure second_tls_get(const SymStr: string); + procedure second_set_base_pointer; protected function first_sqr_real: tnode; override; public @@ -72,12 +73,14 @@ interface implementation uses + procinfo, ninl,ncal,compinnr, aasmbase,aasmdata,aasmcpu, cgbase,cgutils, hlcgobj,hlcgcpu, defutil,pass_2,verbose, - symtype,symdef; + symtype,symdef,symcpu, + tgobj,tgcpu; {***************************************************************************** twasminlinenode @@ -557,6 +560,24 @@ implementation end; + procedure twasminlinenode.second_set_base_pointer; + var + pd: tcpuprocdef; + begin + location_reset(location,LOC_VOID,OS_NO); + secondpass(left); + + hlcg.location_force_reg(current_asmdata.CurrAsmList,left.location,left.resultdef,left.resultdef,false); + thlcgwasm(hlcg).a_load_reg_stack(current_asmdata.CurrAsmList,left.resultdef,left.location.register); + + pd:=tcpuprocdef(current_procinfo.procdef); + if pd.base_pointer_ref.base<>NR_LOCAL_STACK_POINTER_REG then + ttgwasm(tg).allocbasepointer(current_asmdata.CurrAsmList,pd.base_pointer_ref); + current_asmdata.CurrAsmList.Concat(taicpu.op_ref(a_local_set,pd.base_pointer_ref)); + thlcgwasm(hlcg).decstack(current_asmdata.CurrAsmList,1); + end; + + function twasminlinenode.first_sqr_real: tnode; begin expectloc:=LOC_FPUREGISTER; @@ -568,6 +589,11 @@ implementation begin Result:=nil; case inlinenumber of + in_wasm32_set_base_pointer: + begin + CheckParameters(1); + resultdef:=voidtype; + end; in_wasm32_memory_size: begin CheckParameters(0); @@ -730,6 +756,7 @@ implementation in_wasm32_memory_size, in_wasm32_memory_grow: expectloc:=LOC_REGISTER; + in_wasm32_set_base_pointer, in_wasm32_memory_fill, in_wasm32_memory_copy, in_wasm32_unreachable, @@ -967,6 +994,8 @@ implementation second_tls_get(TLS_ALIGN_SYM); in_wasm32_tls_base: second_tls_get(TLS_BASE_SYM); + in_wasm32_set_base_pointer: + second_set_base_pointer; else inherited pass_generate_code_cpu; end; diff --git a/rtl/wasi/systhrd.inc b/rtl/wasi/systhrd.inc index e2ca07ed98..2963178f9f 100644 --- a/rtl/wasi/systhrd.inc +++ b/rtl/wasi/systhrd.inc @@ -267,6 +267,11 @@ end; ----------------------------------------------------------------------} +procedure FPCWasmThreadSetStackPointer(Address: Pointer); [public, alias: 'FPC_WASM_THREAD_SET_STACK_POINTER']; +begin + fpc_wasm32_set_base_pointer(Address); +end; + // Javascript definition: TThreadInitInstanceFunction = Function(IsWorkerThread : Longint; IsMainThread : Integer; CanBlock : Integer) : Integer; Function FPCWasmThreadInit(IsWorkerThread : Longint; IsMainThread : Longint; CanBlock : Longint) : Longint; [public, alias: 'FPC_WASM_THREAD_INIT']; @@ -295,7 +300,7 @@ begin Result:=tthreadfunc(RunFunction)(args); end; -exports FPCWasmThreadInit, FPCWasmThreadEntry; +exports FPCWasmThreadSetStackPointer, FPCWasmThreadInit, FPCWasmThreadEntry; Function thread_spawn(thread_id : PInteger; attrs: Pointer; thread_start_func : Pointer; args : Pointer) : LongInt; external 'FPCThreading' name 'thread_spawn'; diff --git a/rtl/wasm32/cpuh.inc b/rtl/wasm32/cpuh.inc index 57d39e54cf..b302d9c898 100644 --- a/rtl/wasm32/cpuh.inc +++ b/rtl/wasm32/cpuh.inc @@ -107,3 +107,4 @@ function fpc_wasm32_tls_size: SizeUInt;[internproc:fpc_in_wasm32_tls_size]; function fpc_wasm32_tls_align: SizeUInt;[internproc:fpc_in_wasm32_tls_align]; function fpc_wasm32_tls_base: Pointer;[internproc:fpc_in_wasm32_tls_base]; +procedure fpc_wasm32_set_base_pointer(Address: Pointer);[internproc:fpc_in_wasm32_set_base_pointer]; diff --git a/rtl/wasm32/cpuinnr.inc b/rtl/wasm32/cpuinnr.inc index 9cb7ef7889..551659f2a9 100644 --- a/rtl/wasm32/cpuinnr.inc +++ b/rtl/wasm32/cpuinnr.inc @@ -99,3 +99,4 @@ fpc_in_wasm32_tls_align = fpc_in_cpu_first+74; fpc_in_wasm32_tls_base = fpc_in_cpu_first+75; + fpc_in_wasm32_set_base_pointer = fpc_in_cpu_first+76;