From c342a5f47373ab522ed67ba1f4e95f9a208dc83f Mon Sep 17 00:00:00 2001 From: Nikolay Nikolov Date: Sun, 31 Dec 2023 22:28:53 +0200 Subject: [PATCH] * converted some helper methods from TWasmObjOutput to global functions, so they can be reused in the exe writer --- compiler/ogwasm.pas | 110 +++++++++++++++++++++----------------------- 1 file changed, 52 insertions(+), 58 deletions(-) diff --git a/compiler/ogwasm.pas b/compiler/ogwasm.pas index 0b668100b5..1eae272bb5 100644 --- a/compiler/ogwasm.pas +++ b/compiler/ogwasm.pas @@ -180,11 +180,6 @@ interface FWasmSections: array [TWasmSectionID] of tdynamicarray; FWasmCustomSections: array [TWasmCustomSectionType] of tdynamicarray; FWasmLinkingSubsections: array [low(TWasmLinkingSubsectionType)..high(TWasmLinkingSubsectionType)] of tdynamicarray; - procedure WriteUleb(d: tdynamicarray; v: uint64); - procedure WriteUleb(w: TObjectWriter; v: uint64); - procedure WriteSleb(d: tdynamicarray; v: int64); - procedure WriteByte(d: tdynamicarray; b: byte); - procedure WriteName(d: tdynamicarray; const s: string); procedure WriteWasmSection(wsid: TWasmSectionID); procedure WriteWasmCustomSection(wcst: TWasmCustomSectionType); procedure CopyDynamicArray(src, dest: tdynamicarray; size: QWord); @@ -316,6 +311,58 @@ implementation d.write(b,1); end; end; + procedure WriteUleb(d: tdynamicarray; v: uint64); + var + b: byte; + begin + repeat + b:=byte(v) and 127; + v:=v shr 7; + if v<>0 then + b:=b or 128; + d.write(b,1); + until v=0; + end; + + procedure WriteUleb(w: TObjectWriter; v: uint64); + var + b: byte; + begin + repeat + b:=byte(v) and 127; + v:=v shr 7; + if v<>0 then + b:=b or 128; + w.write(b,1); + until v=0; + end; + + procedure WriteSleb(d: tdynamicarray; v: int64); + var + b: byte; + Done: Boolean=false; + begin + repeat + b:=byte(v) and 127; + v:=SarInt64(v,7); + if ((v=0) and ((b and 64)=0)) or ((v=-1) and ((b and 64)<>0)) then + Done:=true + else + b:=b or 128; + d.write(b,1); + until Done; + end; + + procedure WriteByte(d: tdynamicarray; b: byte); + begin + d.write(b,1); + end; + + procedure WriteName(d: tdynamicarray; const s: string); + begin + WriteUleb(d,Length(s)); + d.writestr(s); + end; function ReadUleb(d: tdynamicarray): uint64; var @@ -946,59 +993,6 @@ implementation TWasmObjOutput ****************************************************************************} - procedure TWasmObjOutput.WriteUleb(d: tdynamicarray; v: uint64); - var - b: byte; - begin - repeat - b:=byte(v) and 127; - v:=v shr 7; - if v<>0 then - b:=b or 128; - d.write(b,1); - until v=0; - end; - - procedure TWasmObjOutput.WriteUleb(w: TObjectWriter; v: uint64); - var - b: byte; - begin - repeat - b:=byte(v) and 127; - v:=v shr 7; - if v<>0 then - b:=b or 128; - w.write(b,1); - until v=0; - end; - - procedure TWasmObjOutput.WriteSleb(d: tdynamicarray; v: int64); - var - b: byte; - Done: Boolean=false; - begin - repeat - b:=byte(v) and 127; - v:=SarInt64(v,7); - if ((v=0) and ((b and 64)=0)) or ((v=-1) and ((b and 64)<>0)) then - Done:=true - else - b:=b or 128; - d.write(b,1); - until Done; - end; - - procedure TWasmObjOutput.WriteByte(d: tdynamicarray; b: byte); - begin - d.write(b,1); - end; - - procedure TWasmObjOutput.WriteName(d: tdynamicarray; const s: string); - begin - WriteUleb(d,Length(s)); - d.writestr(s); - end; - procedure TWasmObjOutput.WriteWasmSection(wsid: TWasmSectionID); var b: byte;