-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathwatscanner.pas
More file actions
465 lines (417 loc) · 11.7 KB
/
Copy pathwatscanner.pas
File metadata and controls
465 lines (417 loc) · 11.7 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
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
unit watscanner;
{$mode delphi}{$H+}
interface
uses
SysUtils, Classes, parseutils, wasmtext;
type
TWatToken = (weNone, weError,
weIdent,
weString, weNumber, weOpenBrace, weCloseBrace,
weAsmSymbol,
weInstr,
weFunc,
weParam, weResult,
weModule, weMut, weFuncRef,
wei32, wei64,
wef32, wef64,
weType,
weImport, weGlobal, weTable, weMemory, weLocal, weExport,
weElem, weData, weOffset, weAlign, weEqual
);
// used only for weNumber
TWatNumberFormat = (
wnfNo, // other than number
wnfInteger, // 00
wnfHex, // 0xABC
wnfFloat, // 0.000
wnfFloatHex // 0x000.bced
);
THexStr = record
num : QWord;
frac : QWord;
exp : integer;
isNeg : Boolean;
end;
{ TWatScanner }
TWatScanner = class(TObject)
protected
procedure DoComment(ofs: Integer; const cmt: string); virtual;
function CommentIsSymbol(const cmt: string): Boolean;
public
buf : string;
idx : integer;
instrCode : byte;
ofs : integer;
token : TWatToken;
numformat : TWatNumberFormat;
resText : string;
asmCmd : string;
skipAsmSym : Boolean;
procedure SetSource(const abuf: string);
function Next: Boolean;
function resInt32(const def: integer=-1): Integer;
function resWasmString: string;
end;
const
// see Identifiers of Textual format
IdStart = '$';
IdBody = AlphaNumChars
+ [ '!' ,'#' ,'$' ,'%' ,'&' ,'''' ,'*'
,'+' ,'-' ,'.' ,'/' ,':' ,'<' ,'='
,'>' ,'?' ,'@' ,'\' ,'^' ,'_' ,'`'
,'|' ,'~'];
GrammarChars = AlphaNumChars+['.','_'
,'/' // some old instructions are like that: "f32.reinterpret/i32"
];
procedure GetGrammar(const txt: string; out entity: TWatToken; out instByte: byte);
const
KEY_MODULE = 'module';
KEY_FUNC = 'func';
KEY_FUNCREF = 'funcref';
KEY_I32 = 'i32';
KEY_I64 = 'i64';
KEY_F32 = 'f32';
KEY_F64 = 'f64';
KEY_PARAM = 'param';
KEY_RESULT = 'result';
KEY_MUT = 'mut';
KEY_TYPE = 'type';
KEY_IMPORT = 'import';
KEY_GLOBAL = 'global';
KEY_TABLE = 'table';
KEY_MEMORY = 'memory';
KEY_LOCAL = 'local';
KEY_EXPORT = 'export';
KEY_ELEM = 'elem';
KEY_DATA = 'data';
KEY_OFFSET = 'offset';
function ScanString(const buf: string; var idx: integer): string;
function HexFloatStrToHexStr(const t: string; out hexStr: THexStr): Boolean;
function HexFracToSingle(const num, frac: QWord; exp: Integer; isNeg: Boolean): Single;
function HexFloatStrToSingle(const hexstr: string): Single;
function HexFracToDouble(const num, frac: QWord; exp: Integer; neg: Boolean): Double;
function HexFloatStrToDouble(const hexstr: string): double;
implementation
procedure GetGrammar(const txt: string; out entity: TWatToken; out instByte: byte);
begin
instByte:=0;
entity:=weError;
if txt='' then Exit;
case txt[1] of
'a':
if txt='anyfunc' then entity:=weFuncRef
else if txt = 'align' then entity:=weAlign
else if TextToInst(txt, instByte) then entity:=weInstr;
'd':
if txt=KEY_DATA then entity:=weData
else if TextToInst(txt, instByte) then entity:=weInstr;
'e':
if txt=KEY_EXPORT then entity:=weExport
else if txt=KEY_ELEM then entity:=weElem
else if TextToInst(txt, instByte) then entity:=weInstr;
'i':
if txt=KEY_I32 then entity:=wei32
else if txt=KEY_I64 then entity:=wei64
else if txt=KEY_IMPORT then entity:=weImport
else if TextToInst(txt, instByte) then entity:=weInstr;
'g':
if txt=KEY_GLOBAL then entity:=weGlobal
else if TextToInst(txt, instByte) then entity:=weInstr;
'f':
if txt=KEY_FUNC then entity:=weFunc
else if txt=KEY_FUNCREF then entity:=weFuncRef
else if txt=KEY_F32 then entity:=wef32
else if txt=KEY_F64 then entity:=wef64
else if TextToInst(txt, instByte) then entity:=weInstr;
'l':
if txt=KEY_LOCAL then entity:=weLocal
else if TextToInst(txt, instByte) then entity:=weInstr;
'm':
if txt=KEY_MODULE then entity:=weModule
else if txt = KEY_MUT then entity:=weMut
else if txt = KEY_MEMORY then entity:=weMemory
else if TextToInst(txt, instByte) then entity:=weInstr;
'o':
if txt=KEY_OFFSET then entity:=weOffset
else if TextToInst(txt, instByte) then entity:=weInstr;
'p':
if txt=KEY_PARAM then entity:=weParam
else if TextToInst(txt, instByte) then entity:=weInstr;
'r':
if txt=KEY_RESULT then entity:=weResult
else if TextToInst(txt, instByte) then entity:=weInstr;
't':
if txt=KEY_TYPE then entity:=weType
else if txt=KEY_TABLE then entity:=weTable
else if TextToInst(txt, instByte) then entity:=weInstr;
else
if TextToInst(txt, instByte) then entity:=weInstr;
end;
end;
{ TWatScanner }
procedure TWatScanner.DoComment(ofs: Integer; const cmt: string);
begin
end;
function TWatScanner.CommentIsSymbol(const cmt: string): Boolean;
var
i: integer;
t: string;
v: string;
begin
Result := false;
if (Pos(';;',cmt)<>1) then Exit;
i:=3;
ScanWhile(cmt, i, SpaceChars);
if (i>length(cmt)) or (cmt[i]<>'.') then Exit;
inc(i);
t := AnsiLowerCase(ScanTo(cmt, i, SpaceChars));
ScanWhile(cmt, i, SpaceChars);
v := ScanTo(cmt, i, SpaceChars);
asmCmd := t;
resText := v;
Result := true;
end;
procedure TWatScanner.SetSource(const abuf: string);
begin
buf:=abuf;
idx:=1;
end;
function ScanString(const buf: string; var idx: integer): string;
var
j : integer;
begin
if buf[idx]<>'"' then begin
Result:='';
Exit;
end;
j:=idx;
inc(idx);
while (buf[idx]<>'"') and (idx<length(buf)) do begin
if buf[idx]='\' then inc(idx);
inc(idx);
end;
inc(idx);
Result:=Copy(buf, j, idx-j);
end;
function TWatScanner.Next: Boolean;
var
cmt : string;
done: boolean;
fmt : TCNumberFormat;
si : integer;
begin
numformat := wnfNo;
Result := idx<=length(buf);
if not Result then Exit;
done:=false;
resText:='';
while not done do begin
ScanWhile(buf, idx, SpaceEolnChars);
Result := idx<=length(buf);
if not Result then Exit;
ofs:=idx;
if (idx<length(buf)) and (buf[idx] in [';','(']) and (buf[idx+1]=';') then begin
if (buf[idx]=';') then begin
// comment until the end of the line
cmt := ScanTo(buf, idx, EoLnChars);
ScanWhile(buf, idx, EoLnChars);
end else
// comment until the ;)
cmt := ScanToSubstr(buf, idx, ';)');
if not skipAsmSym and CommentIsSymbol(cmt) then begin
token:=weAsmSymbol;
done:=true;
end else
DoComment(ofs, cmt);
end else begin
done:=true;
if buf[idx] = '(' then begin
token:=weOpenBrace;
inc(idx);
end else if buf[idx]=')' then begin
token:=weCloseBrace;
inc(idx);
end else if buf[idx]='=' then begin
token:=weEqual;
inc(idx);
end else if buf[idx]='"' then begin
token:=weString;
resText:=ScanString(buf, idx);
end else if buf[idx] = IdStart then begin
token:=weIdent;
resText:=ScanWhile(buf, idx, IdBody);
end else if buf[idx] in SignNumericChars then begin
fmt := ScanNumberC(buf, idx, resText);
if fmt = nfError then begin
token := weError;
Exit;
end else
token:=weNumber;
case fmt of
nfFloat: numformat := wnfFloat;
nfFloatHex: numFormat := wnfFloatHex;
nfHex: numformat := wnfHex;
else
numformat := wnfInteger;
end;
end else if buf[idx] in GrammarChars then begin
si := idx;
resText:=ScanWhile(buf, idx, GrammarChars);
// second try for the number
if (resText = 'nan') or (resText = 'inf') then begin
idx := si;
fmt := ScanNumberC(buf, idx, resText);
if fmt = nfError then begin
token := weError;
Exit;
end else
token:=weNumber;
case fmt of
nfFloat: numformat := wnfFloat;
nfHex: numformat := wnfHex;
else
numformat := wnfInteger;
end;
end else
GetGrammar(resText, token, instrCode);
done:=true;
end else begin
token:=weError;
inc(idx);
done:=true;
end;
end;
end;
if resText='' then
resText := Copy(buf, ofs, idx-ofs);
end;
function TWatScanner.resInt32(const def: integer=-1): Integer;
var
err: integer;
begin
Val(resText, Result, err);
if err<>0 then Result:=def;
end;
function TWatScanner.resWasmString: string;
var
i : integer;
j : integer;
begin
if token<>weString then begin
Result:='';
Exit;
end;
Result:=Copy(resText, 2, length(resText)-2);
if Result='' then Exit;
i:=1;
j:=1;
while i<=length(Result) do begin
if Result[i]='\' then begin
inc(i);
if i<=length(Result) then
case Result[i] of
'r': Result[j]:=#13;
'n': Result[j]:=#10;
'\': Result[j]:='\';
'"': Result[j]:='"';
end;
end else
if (j<i) then Result[j]:=Result[i];
inc(j);
inc(i);
end;
SetLength(Result, j-1);
end;
function HexFloatStrToHexStr(const t: string; out hexStr: THexStr): Boolean;
var
i : integer;
j : integer;
err : Integer;
const
HexChars = ['0'..'9','a'..'f','A'..'F'];
begin
hexStr.isNeg:=false;
hexStr.num:=0;
hexStr.frac:=0;
hexStr.exp:=0;
if (t='') then begin
Result:=true;
Exit;
end;
i:=1;
hexStr.isNeg:=t[i]='-';
if (hexStr.isNeg) then inc(i);
inc(i,2); // skipping '0x'
j:=i;
while (i<=length(t)) and (t[i] in HexChars) do inc(i);
Val('$'+Copy(t, j, i-j), hexStr.num, err);
Result:=err=0;
if not Result then Exit;
if (t[i]='.') then begin
inc(i);
j:=i;
while (i<=length(t)) and (t[i] in HexChars) do inc(i);
Val('$'+Copy(t, j, i-j), hexStr.frac, err);
Result:=err=0;
if not Result then Exit;
end;
Result := (i<=length(t)) and (t[i] = 'p') or (t[i]='P');
inc(i);
Val(Copy(t, i, length(t)), hexStr.exp, err);
Result:=err=0;
end;
function HexFracToSingle(const num, frac: QWord; exp: Integer; isNeg: Boolean): Single;
var
x : QWord;
nm : QWord;
adjexp : integer;
sr : TSingleRec;
begin
nm := num;
x := frac;
adjexp := -1;
while (nm > 0) do begin
x:=(x shr 1) or ((nm and 1) shl 23);
nm := nm shr 1;
inc(adjexp);
end;
sr.Exp:=127 + exp + adjexp;
sr.Frac:=x;
sr.Sign:=isNeg;
Result := sr.Value;
end;
function HexFloatStrToSingle(const hexstr: string): Single;
var
st : THexStr;
begin
HexFloatStrToHexStr(hexstr, st);
Result:=HexFracToSingle(st.num, st.frac, st.exp, st.isNeg);
end;
function HexFracToDouble(const num, frac: QWord; exp: Integer; neg: Boolean): Double;
var
x : QWord;
nm : QWord;
adjexp : integer;
sr : TDoubleRec;
begin
nm := num;
x := frac;
adjexp := 0;
while (nm > 1) do begin
x:=(x shr 1) or ((nm and 1) shl 52);
nm := nm shr 1;
inc(adjexp);
end;
sr.Exp:=1023 + exp + adjexp;
sr.Frac:=x;
sr.Sign:=neg;
Result := sr.Value;
end;
function HexFloatStrToDouble(const hexstr: string): double;
var
st : THexStr;
begin
HexFloatStrToHexStr(hexstr, st);
Result:=HexFracToDouble(st.num, st.frac, st.exp, st.isNeg);
end;
end.