From 88a42c3ee0c2c0c53acdb99e2972ab24ecc9faac Mon Sep 17 00:00:00 2001 From: Nikolay Nikolov Date: Sun, 31 Dec 2023 23:50:53 +0200 Subject: [PATCH] + write the type indexes correctly --- compiler/ogwasm.pas | 37 ++++++++++++++++++++++++++++++++++--- 1 file changed, 34 insertions(+), 3 deletions(-) diff --git a/compiler/ogwasm.pas b/compiler/ogwasm.pas index 5410e136f7..4da09e10b3 100644 --- a/compiler/ogwasm.pas +++ b/compiler/ogwasm.pas @@ -49,6 +49,7 @@ interface ImportName: string; FuncType: TWasmFuncType; ExeFunctionIndex: Integer; + ExeTypeIndex: Integer; constructor Create; destructor Destroy;override; @@ -231,6 +232,7 @@ interface FWasmSections: array [TWasmSectionID] of tdynamicarray; procedure WriteWasmSection(wsid: TWasmSectionID); procedure PrepareImports; + procedure PrepareFunctions; protected function writeData:boolean;override; procedure DoRelocationFixup(objsec:TObjSection);override; @@ -494,6 +496,7 @@ implementation constructor TWasmObjSymbolLinkingData.Create; begin ExeFunctionIndex:=-1; + ExeTypeIndex:=-1; end; destructor TWasmObjSymbolLinkingData.Destroy; @@ -4072,7 +4075,7 @@ implementation var i: Integer; exesec: TExeSection; - objsec: TObjSection; + objsec: TWasmObjSection; begin exesec:=FindExeSection('.text'); if not assigned(exesec) then @@ -4083,12 +4086,12 @@ implementation WriteUleb(FWasmSections[wsiCode],exesec.ObjSectionList.Count); for i:=0 to exesec.ObjSectionList.Count-1 do begin - objsec:=TObjSection(exesec.ObjSectionList[i]); + objsec:=TWasmObjSection(exesec.ObjSectionList[i]); if not (oso_data in objsec.secoptions) then internalerror(2023123104); if not assigned(objsec.data) then internalerror(2023123105); - WriteUleb(FWasmSections[wsiFunction],0); { todo: TypIdx } + WriteUleb(FWasmSections[wsiFunction],objsec.MainFuncSymbol.LinkingData.ExeTypeIndex); WriteUleb(FWasmSections[wsiCode],objsec.Data.size); objsec.Data.seek(0); CopyDynamicArray(objsec.Data,FWasmSections[wsiCode],objsec.Data.size); @@ -4172,6 +4175,7 @@ implementation procedure TWasmExeOutput.AfterUnusedSectionRemoval; begin PrepareImports; + PrepareFunctions; end; procedure TWasmExeOutput.PrepareImports; @@ -4221,6 +4225,33 @@ implementation end; end; + procedure TWasmExeOutput.PrepareFunctions; + var + i: Integer; + exesec: TExeSection; + objsec: TWasmObjSection; + fsym: TWasmObjSymbol; + begin + exesec:=FindExeSection('.text'); + if not assigned(exesec) then + internalerror(2023123106); + for i:=0 to exesec.ObjSectionList.Count-1 do + begin + objsec:=TWasmObjSection(exesec.ObjSectionList[i]); + fsym:=objsec.MainFuncSymbol; + if not assigned(fsym) then + internalerror(2023123107); + if not assigned(fsym.LinkingData.FuncType) then + internalerror(2023123108); + if fsym.LinkingData.ExeFunctionIndex<>-1 then + internalerror(2023123109); + if fsym.LinkingData.ExeTypeIndex<>-1 then + internalerror(2023123109); + fsym.LinkingData.ExeTypeIndex:=FFuncTypes.AddOrGetFuncType(fsym.LinkingData.FuncType); + fsym.LinkingData.ExeFunctionIndex:=i+Length(FFunctionImports); + end; + end; + {**************************************************************************** TWasmAssembler