-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathwasmnormalize.pas
More file actions
350 lines (303 loc) · 8.98 KB
/
wasmnormalize.pas
File metadata and controls
350 lines (303 loc) · 8.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
unit wasmnormalize;
interface
uses
SysUtils, Classes,
wasmmodule, wasmbin, wasmbincode, wasmlink;
procedure Normalize(m: TWasmModule);
implementation
procedure PopulateRelocData(module: TWasmModule; ci: TWasmInstr);
var
idx : integer;
obj : TObject;
begin
case INST_FLAGS[ci.code].Param of
ipi32OrFunc:
if (ci.operand1.textVal<>'') and (ci.operand1.textVal[1]='$') then begin
//if not ci.hasRelocIdx then
idx := RegisterFuncInElem(module, ci.operand1.textVal);
obj := GetFuncByNum(module, idx);
//AddReloc(rt, dst.Position+ofsAddition, idx);
ci.operandNum := idx;
ci.SetReloc(INST_RELOC_FLAGS[ci.code].relocType, obj);
end;
ipLeb:
if (INST_RELOC_FLAGS[ci.code].doReloc) then begin
case INST_RELOC_FLAGS[ci.code].relocType of
R_WASM_FUNCTION_INDEX_LEB:
ci.SetReloc(INST_RELOC_FLAGS[ci.code].relocType, GetFuncByNum(module, ci.operandNum));
R_WASM_GLOBAL_INDEX_LEB:
ci.SetReloc(INST_RELOC_FLAGS[ci.code].relocType, GetGlobalByNum(module, ci.operandNum));
R_WASM_MEMORY_ADDR_LEB :
ci.SetReloc(INST_RELOC_FLAGS[ci.code].relocType, GetMemByNum(module, ci.operandNum));
end;
end;
ipCallType:
if Assigned(ci.insttype) then
begin
ci.SetReloc(INST_RELOC_FLAGS[ci.code].relocType, ci.insttype);
end;
end;
end;
// searching back in the labels stack.
// returning the "number" of steps to jump back to the label
function GetJumpLabelIndex(const JumpToLbl: string; LblStack: TStrings): Integer;
var
i : integer;
begin
i:=LblStack.Count-1;
while (i>=0) and (LblStack[i]<>JumpToLbl) do
dec(i);
Result := LblStack.Count-i-1;
end;
// Normalizing instruction list, popuplating index reference ($index)
// with the actual numbers. (params, locals, globals, memory, functions index)
//
// pass "f" as nil, if instruction list doesn't belong to a function
function NormalizeInst(m: TWasmModule; f: TWasmFunc; l: TWasmInstrList; checkEnd: boolean = true): Boolean;
var
i : integer;
j : integer;
ci : TWasmInstr;
endNeed : Integer;
lbl : TStringList;
const
ValidResTypes = [VALTYPE_NONE,VALTYPE_I32,VALTYPE_I64,VALTYPE_F32,VALTYPE_F64];
begin
Result := true;
endNeed := 1;
lbl := TStringList.Create;
try
for i:=0 to l.Count-1 do begin
ci:=l[i];
case INST_FLAGS[ci.code].Param of
ipResType:
begin
inc(endNeed);
if not (byte(ci.operandNum) in ValidResTypes) then
ci.operandNum := VALTYPE_NONE;
lbl.Add(ci.jumplabel);
end;
end;
case ci.code of
INST_local_get, INST_local_set, INST_local_tee:
begin
if not Assigned(f) then begin
Result:=false;
Exit;
end;
if (ci.operandIdx<>'') and (ci.operandNum<0) then begin
j:=FindParam(f.functype.params, ci.operandIdx);
if j<0 then begin
j:=FindParam(f.locals, ci.operandIdx);
if j>=0 then inc(j, f.functype.ParamCount);
end;
ci.operandNum:=j;
end;
end;
INST_global_get, INST_global_set:
begin
if not Assigned(f) then begin
Result:=false;
Exit;
end;
if (ci.operandIdx<>'') and (ci.operandNum<0) then begin
j:=FindGlobal(m, ci.operandIdx);
ci.operandNum:=j;
end;
end;
INST_call:
begin
if (ci.operandIdx<>'') and (ci.operandNum<0) then
ci.operandNum:=FindFunc(m,ci.operandIdx);
end;
INST_call_indirect:
begin
if Assigned(ci.insttype) and (ci.insttype.typeNum<0) then begin
if ci.insttype.typeIdx <> '' then
ci.insttype.typeNum:=FindFuncType(m, ci.insttype.typeIdx)
else
ci.insttype.typeNum:=RegisterFuncType(m, ci.insttype);
end;
end;
INST_br, INST_br_if, INST_br_table: begin
if ci.code = INST_br_table then
for j:=0 to ci.vecTableCount-1 do
if ci.vecTable[j].id <> '' then
ci.vecTable[j].idNum:=GetJumpLabelIndex(ci.vecTable[j].id, lbl);
if ci.operandIdx<>'' then
ci.operandNum:=GetJumpLabelIndex(ci.operandIdx, lbl);
end;
INST_END: begin
dec(endNeed);
if lbl.Count>0 then lbl.Delete(lbl.Count-1);
end;
end;
PopulateRelocData(m, ci);
end;
// adding end instruction
if checkEnd and (endNeed>0) then
l.AddInstr(INST_END);
finally
lbl.Free;
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; out fnIdx: Integer;
out memIdx: Integer; out globIdx: Integer; out tblIdx : Integer);
var
i : integer;
im : TWasmImport;
begin
fnIdx := 0;
memIdx := 0;
globIdx := 0;
tblIdx := 0;
for i:=0 to m.ImportCount-1 do begin
im := m.GetImport(i);
if Assigned(im.fn) then begin
im.fn.idNum:=fnIdx;
NormalizeFuncType(m, im.fn.functype);
inc(fnIdx);
end else if Assigned(im.mem) then begin
im.mem.id.idNum := memIdx;
inc(memIdx);
end else if Assigned(im.glob) then begin
im.glob.id.idNum := globIdx;
inc(globIdx);
end else if Assigned(im.table) then begin
im.table.id.idNum := tblIdx;
inc(tblIdx);
end;
end;
end;
procedure NormalizeGlobals(m: TWasmModule);
var
i : integer;
begin
for i:=0 to m.GlobalCount-1 do
m.GetGlobal(i).id.idNum:=i;
end;
procedure NormalizeTable(m: TWasmModule);
var
i : integer;
j : integer;
t : TWasmTable;
se : TWasmElement;
de : TWasmElement;
begin
for i:=0 to m.TableCount-1 do begin
t := m.GetTable(i);
t.id.idNum:=i; // todo: is it safe?
end;
for i:=0 to m.TableCount-1 do begin
t := m.GetTable(i);
if not Assigned(t.elem) then continue;
se:=t.elem;
de := m.AddElement;
de.tableId.idNum := t.id.idNum;
if t.elemsType = 0 then t.elemsType := ELEMTYPE_FUNC;
de.funcCount:=se.funcCount;
if se.funcCount>0 then begin
SetLength(de.funcs, de.funcCount);
for j:=0 to de.funcCount-1 do begin
de.funcs[j].id := se.funcs[j].id;
de.funcs[j].idNum := se.funcs[j].idNum;
end;
end;
end;
end;
procedure NormalizeTableLimit(m: TWasmModule);
var
i : integer;
elCount : integer;
t : TWasmTable;
begin
elCount:=0;
for i:=0 to m.ElementCount-1 do
inc(elCount, m.GetElement(i).funcCount );
for i:=0 to m.TableCount-1 do begin
t := m.GetTable(i);
if (t.min=0) and (t.max=0) then begin
t.min := elCount;
t.max := elCount;
Break;
end;
end;
end;
// For each element on the module, funcId is replaced with the proper IdNum
// it should only be called after all functions have their IDs resolved
procedure NormalizeElems(m: TWasmModule);
var
i : integer;
e : TWasmElement;
j : integer;
l : TWasmInstrList;
begin
//todo: resolve offsets
for i:=0 to m.ElementCount-1 do begin
e := m.GetElement(i);
l := e.AddOffset;
if (l.Count=0) then l.AddInstr(INST_i32_const).operand1.s32:=0;
NormalizeInst( m, nil, l);
for j := 0 to e.funcCount-1 do
if e.funcs[j].idNum<0 then
e.funcs[j].idNum := FindFunc(m, e.funcs[j].id);
end;
end;
// normalizing reference
procedure Normalize(m: TWasmModule);
var
i : integer;
f : TWasmFunc;
x : TWasmExport;
fnIdx : Integer;
memIdx : Integer;
globIdx : Integer;
tblIdx : Integer;
g : TWasmGlobal;
begin
fnIdx := 0;
NormalizeGlobals(m);
NormalizeTable(m);
NormalizeImport(m, fnIdx, memIdx, globIdx, tblIdx);
NormalizeTableLimit(m);
for i:=0 to m.FuncCount-1 do begin
f:=m.GetFunc(i);
f.idNum := fnIdx;
NormalizeFuncType(m, f.functype);
inc(fnIdx);
end;
NormalizeElems(m);
for i:=0 to m.GlobalCount-1 do
begin
g := m.GetGlobal(i);
g.id.idNum := globIdx;
inc(globIdx);
NormalizeInst(m, nil, g.StartValue);
end;
// normalizing function body
for i:=0 to m.FuncCount-1 do begin
f:=m.GetFunc(i);
// finding the reference in functions
// populating "nums" where string "index" is used
NormalizeInst(m, f, f.instr);
end;
// normalizing exports
for i:=0 to m.ExportCount-1 do begin
x:=m.GetExport(i);
if x.exportNum<0 then
case x.exportType of
EXPDESC_FUNC:
if x.exportIdx<>'' then
x.exportNum := FindFunc(m, x.exportIdx);
end;
end;
end;
end.