mirror of
https://gitlab.com/freepascal.org/fpc/source.git
synced 2025-08-24 09:10:53 +02:00
[PATCH 078/188] adding support for the import section normalization
From 6d667cdb880b17716dc1ad1b995082dbcbd36ff2 Mon Sep 17 00:00:00 2001 From: Dmitry Boyarintsev <skalogryz.lists@gmail.com> Date: Mon, 9 Mar 2020 08:47:34 -0400 git-svn-id: branches/wasm@46074 -
This commit is contained in:
parent
c70f9ec15d
commit
25327812a6
@ -98,6 +98,7 @@ type
|
|||||||
public
|
public
|
||||||
LinkInfo : TLinkInfo;
|
LinkInfo : TLinkInfo;
|
||||||
id : string;
|
id : string;
|
||||||
|
idInt : Integer; // reference number (after Normalization)
|
||||||
instr : TWasmInstrList;
|
instr : TWasmInstrList;
|
||||||
functype : TWasmFuncType;
|
functype : TWasmFuncType;
|
||||||
|
|
||||||
@ -142,6 +143,7 @@ type
|
|||||||
destructor Destroy; override;
|
destructor Destroy; override;
|
||||||
|
|
||||||
function AddImport: TWasmImport;
|
function AddImport: TWasmImport;
|
||||||
|
function GetImport(i: integer): TWasmImport;
|
||||||
function ImportCount: Integer;
|
function ImportCount: Integer;
|
||||||
|
|
||||||
function AddFunc: TWasmFunc;
|
function AddFunc: TWasmFunc;
|
||||||
@ -420,6 +422,14 @@ begin
|
|||||||
imports.Add(Result);
|
imports.Add(Result);
|
||||||
end;
|
end;
|
||||||
|
|
||||||
|
function TWasmModule.GetImport(i: integer): TWasmImport;
|
||||||
|
begin
|
||||||
|
if (i>=0) and (i<imports.Count) then
|
||||||
|
Result:=TWasmImport(imports[i])
|
||||||
|
else
|
||||||
|
Result:=nil;
|
||||||
|
end;
|
||||||
|
|
||||||
function TWasmModule.ImportCount: Integer;
|
function TWasmModule.ImportCount: Integer;
|
||||||
begin
|
begin
|
||||||
Result:=imports.Count;
|
Result:=imports.Count;
|
||||||
@ -490,6 +500,7 @@ begin
|
|||||||
locals:=TList.Create;
|
locals:=TList.Create;
|
||||||
instr:=TWasmInstrList.Create;
|
instr:=TWasmInstrList.Create;
|
||||||
functype:=TWasmFuncType.Create;
|
functype:=TWasmFuncType.Create;
|
||||||
|
idInt:=-1;
|
||||||
end;
|
end;
|
||||||
|
|
||||||
destructor TWasmFunc.Destroy;
|
destructor TWasmFunc.Destroy;
|
||||||
@ -560,12 +571,21 @@ end;
|
|||||||
// finding functions by funcIdx
|
// finding functions by funcIdx
|
||||||
function FindFunc(m: TWasmModule; const funcIdx: string): integer;
|
function FindFunc(m: TWasmModule; const funcIdx: string): integer;
|
||||||
var
|
var
|
||||||
i : integer;
|
i : integer;
|
||||||
|
im : TWasmImport;
|
||||||
begin
|
begin
|
||||||
Result:=-1;
|
Result:=-1;
|
||||||
|
for i:=0 to m.ImportCount-1 do begin
|
||||||
|
im:=m.GetImport(i);
|
||||||
|
if Assigned(im.fn) and (im.fn.id = funcIdx) then begin
|
||||||
|
Result:=im.fn.idInt;
|
||||||
|
Exit;
|
||||||
|
end;
|
||||||
|
end;
|
||||||
|
|
||||||
for i:=0 to m.FuncCount-1 do
|
for i:=0 to m.FuncCount-1 do
|
||||||
if m.GetFunc(i).id = funcIdx then begin
|
if m.GetFunc(i).id = funcIdx then begin
|
||||||
Result:=i;
|
Result:=m.GetFunc(i).idInt;
|
||||||
Exit;
|
Exit;
|
||||||
end;
|
end;
|
||||||
end;
|
end;
|
||||||
@ -609,7 +629,7 @@ begin
|
|||||||
|
|
||||||
INST_CALL:
|
INST_CALL:
|
||||||
begin
|
begin
|
||||||
if (ci.operandIdx<>'') and (ci.operandNum<0) then
|
if (ci.operandIdx<>'') and ( ci.operandNum<0) then
|
||||||
ci.operandNum:=FindFunc(m,ci.operandIdx);
|
ci.operandNum:=FindFunc(m,ci.operandIdx);
|
||||||
end;
|
end;
|
||||||
end;
|
end;
|
||||||
@ -620,24 +640,53 @@ begin
|
|||||||
l.AddInstr(INST_END);
|
l.AddInstr(INST_END);
|
||||||
end;
|
end;
|
||||||
|
|
||||||
|
|
||||||
|
procedure NormalizeFuncType(m: TWasmModule; fn : TWasmFuncType);
|
||||||
|
begin
|
||||||
|
if fn.isNumOrIdx then begin
|
||||||
|
if fn.typeIdx<>'' then
|
||||||
|
fn.typeNum:=FindFuncType(m, fn.typeIdx);
|
||||||
|
end else
|
||||||
|
fn.typeNum:=RegisterFuncType(m, fn);
|
||||||
|
end;
|
||||||
|
|
||||||
|
procedure NormalizeImport(m: TWasmModule; var fnIdx: Integer);
|
||||||
|
var
|
||||||
|
i : integer;
|
||||||
|
im : TWasmImport;
|
||||||
|
begin
|
||||||
|
fnIdx := 0;
|
||||||
|
for i:=0 to m.ImportCount-1 do begin
|
||||||
|
im := m.GetImport(i);
|
||||||
|
if Assigned(im.fn) then begin
|
||||||
|
im.fn.idInt:=fnIdx;
|
||||||
|
NormalizeFuncType(m, im.fn.functype);
|
||||||
|
inc(fnIdx);
|
||||||
|
end;
|
||||||
|
end;
|
||||||
|
end;
|
||||||
|
|
||||||
// normalizing reference
|
// normalizing reference
|
||||||
procedure Normalize(m: TWasmModule);
|
procedure Normalize(m: TWasmModule);
|
||||||
var
|
var
|
||||||
i : integer;
|
i : integer;
|
||||||
f : TWasmFunc;
|
f : TWasmFunc;
|
||||||
x : TWasmExport;
|
x : TWasmExport;
|
||||||
|
fnIdx : Integer;
|
||||||
begin
|
begin
|
||||||
|
fnIdx := 0;
|
||||||
|
NormalizeImport(m, fnIdx);
|
||||||
|
|
||||||
for i:=0 to m.FuncCount-1 do begin
|
for i:=0 to m.FuncCount-1 do begin
|
||||||
f:=m.GetFunc(i);
|
f:=m.GetFunc(i);
|
||||||
if f.functype.isNumOrIdx then begin
|
f.idInt := fnIdx;
|
||||||
if f.functype.typeIdx<>'' then
|
|
||||||
f.functype.typeNum:=FindFuncType(m, f.functype.typeIdx);
|
|
||||||
end else
|
|
||||||
f.functype.typeNum:=RegisterFuncType(m, f.functype);
|
|
||||||
|
|
||||||
|
NormalizeFuncType(m, f.functype);
|
||||||
// finding the reference in functions
|
// finding the reference in functions
|
||||||
// populating "nums" where string "index" is used
|
// populating "nums" where string "index" is used
|
||||||
NormalizeInst(m, f, f.instr);
|
NormalizeInst(m, f, f.instr);
|
||||||
|
|
||||||
|
inc(fnIdx);
|
||||||
end;
|
end;
|
||||||
|
|
||||||
// normalizing exports
|
// normalizing exports
|
||||||
|
Loading…
Reference in New Issue
Block a user