-
Notifications
You must be signed in to change notification settings - Fork 28
/
lpdisassembler.pas
371 lines (316 loc) · 10.4 KB
/
lpdisassembler.pas
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
{
Author: Niels A.D
Project: Lape (https://github.com/nielsAD/lape)
License: GNU Lesser GPL (http://www.gnu.org/licenses/lgpl.html)
Bytecode disassembler.
}
unit lpdisassembler;
{$I lape.inc}
{$IFNDEF FPC}
{$UNDEF Lape_Inline}
{$ENDIF}
interface
uses
Classes, SysUtils,
lptypes, lpvartypes;
type
TLapeDisassemblerPointerMap = {$IFDEF FPC}specialize{$ENDIF} TLapeStringMap<string>;
procedure DisassembleCode(Code: PByte; PointerNames: TLapeDisassemblerPointerMap); overload;
procedure DisassembleCode(Code: PByte; PointerNames: TLapeCompilerBase); overload;
procedure DisassembleCode(Code: PByte; PointerNames: array of TLapeDeclArray); overload;
implementation
uses
lpmessages, lpinterpreter_types, lpeval, lputils;
procedure DisassembleCode(Code: PByte; PointerNames: TLapeDisassemblerPointerMap);
var
CodeBase: PByte;
{$IFDEF Lape_EmitPos}p: TDocPos;{$ENDIF}
function IntToStr(i: Int64): string; overload;
begin
Result := SysUtils.IntToStr(i);
end;
function IntToStr(p: Pointer): string; overload;
var s: string;
begin
if (p = nil) then
Result := 'nil'
else
begin
s := IntToHex(PtrUInt(p), 0);
if (PointerNames <> nil) and PointerNames.ExistsKey(lpString(s)) then
Result := PointerNames[lpString(s)]
else
Result := '$' + s;
end;
end;
procedure _WriteLn(const s: string); overload;
begin
WriteLn('$', IntToHex(Code - CodeBase, 8), ' :: ', s);
end;
procedure _WriteLn(const s: string; const Args: array of const); overload;
begin
_WriteLn(Format(s, args));
end;
procedure DoIsScriptMethod; {$IFDEF Lape_Inline}inline;{$ENDIF}
begin
_WriteLn('IsScriptMethod');
_WriteLn('IncStack %d', [SizeOf(EvalBool) - SizeOf(Pointer)]);
Inc(Code, ocSize);
end;
procedure DoDumpCallStack;
begin
_WriteLn('DumpCallStack');
_WriteLn('IncStack %d', [SizeOf(lpString)]);
Inc(Code, ocSize);
end;
procedure DoGetScriptMethodName;
begin
_WriteLn('GetScriptMethodName');
_WriteLn('IncStack %d', [SizeOf(ShortString) - SizeOf(Pointer)]);
Inc(Code, ocSize);
end;
procedure DoGetExceptionMessage;
begin
_WriteLn('GetExceptionMessage');
_WriteLn('IncStack %d', [SizeOf(ShortString)]);
Inc(Code, ocSize);
end;
procedure DoGetExceptionLocation;
begin
_WriteLn('GetExceptionLocation');
_WriteLn('IncStack %d', [SizeOf(Pointer)]);
Inc(Code, ocSize);
end;
procedure DoGetCallerLocation;
begin
_WriteLn('GetCallerLocation');
_WriteLn('IncStack %d', [SizeOf(Pointer)]);
Inc(Code, ocSize);
end;
procedure DoGetCallerAddress;
begin
_WriteLn('GetCallerAddress');
_WriteLn('IncStack %d', [SizeOf(Pointer)]);
Inc(Code, ocSize);
end;
procedure DoDynArrayRangeCheck; {$IFDEF Lape_Inline}inline;{$ENDIF}
begin
_WriteLn('DynArrayRangeCheck');
_WriteLn('IncStack %d', [SizeOf(Pointer) + SizeOf(SizeInt)]);
Inc(Code, ocSize);
end;
procedure DoInitStackLen; {$IFDEF Lape_Inline}inline;{$ENDIF}
begin
_WriteLn('InitStackLen %d', [PStackOffset(PtrUInt(Code) + ocSize)^]);
Inc(Code, SizeOf(TStackOffset) + ocSize);
end;
procedure DoInitVarLen; {$IFDEF Lape_Inline}inline;{$ENDIF}
begin
_WriteLn('InitVarStackLen %d', [PStackOffset(PtrUInt(Code) + ocSize)^]);
Inc(Code, SizeOf(TStackOffset) + ocSize);
end;
procedure DoInitStack; {$IFDEF Lape_Inline}inline;{$ENDIF}
begin
_WriteLn('InitStack %d', [PStackOffset(PtrUInt(Code) + ocSize)^]);
Inc(Code, SizeOf(TStackOffset) + ocSize);
end;
procedure DoGrowStack; {$IFDEF Lape_Inline}inline;{$ENDIF}
begin
_WriteLn('GrowStack %d', [PStackOffset(PtrUInt(Code) + ocSize)^]);
Inc(Code, SizeOf(TStackOffset) + ocSize);
end;
procedure DoExpandVar; {$IFDEF Lape_Inline}inline;{$ENDIF}
begin
_WriteLn('ExpandVarStack %d', [PStackOffset(PtrUInt(Code) + ocSize)^]);
Inc(Code, SizeOf(TStackOffset) + ocSize);
end;
procedure DoExpandVarAndInit; {$IFDEF Lape_Inline}inline;{$ENDIF}
begin
_WriteLn('ExpandVarStackAndInit %d', [PStackOffset(PtrUInt(Code) + ocSize)^]);
Inc(Code, SizeOf(TStackOffset) + ocSize);
end;
procedure DoGrowVar; {$IFDEF Lape_Inline}inline;{$ENDIF}
begin
_WriteLn('GrowVarStack %d', [PStackOffset(PtrUInt(Code) + ocSize)^]);
Inc(Code, SizeOf(TStackOffset) + ocSize);
end;
procedure DoGrowVarAndInit; {$IFDEF Lape_Inline}inline;{$ENDIF}
begin
_WriteLn('GrowVarStackAndInit %d', [PStackOffset(PtrUInt(Code) + ocSize)^]);
Inc(Code, SizeOf(TStackOffset) + ocSize);
end;
procedure DoPopVar; {$IFDEF Lape_Inline}inline;{$ENDIF}
begin
_WriteLn('PopVarStack');
Inc(Code, ocSize);
end;
procedure DoPopStackToVar; {$IFDEF Lape_Inline}inline;{$ENDIF}
begin
with POC_PopStackToVar(PtrUInt(Code) + ocSize)^ do
begin
_WriteLn('PopStackToVar %d %d', [Size, VOffset]);
_WriteLn('DecStackPos %d', [Size]);
end;
Inc(Code, ocSize + SizeOf(TOC_PopStackToVar));
end;
procedure DoPopVarToStack; {$IFDEF Lape_Inline}inline;{$ENDIF}
begin
with POC_PopStackToVar(PtrUInt(Code) + ocSize)^ do
begin
_WriteLn('PopVarToStack %d %d', [Size, VOffset]);
_WriteLn('IncStackPos %d', [Size]);
end;
Inc(Code, ocSize + SizeOf(TOC_PopStackToVar));
end;
procedure DoJmpVar; {$IFDEF Lape_Inline}inline;{$ENDIF}
begin
_WriteLn('JmpVar');
Inc(Code, ocSize);
end;
procedure DoJmpSafe; {$IFDEF Lape_Inline}inline;{$ENDIF}
begin
_WriteLn('JmpSafe $%x', [PCodePos(PtrUInt(Code) + ocSize)^]);
Inc(Code, ocSize + SizeOf(TCodePos));
end;
procedure DoJmpSafeR; {$IFDEF Lape_Inline}inline;{$ENDIF}
begin
_WriteLn('JmpSafeR $%x', [PtrInt(Code - CodeBase) + PCodeOffset(PtrUInt(Code) + ocSize)^]);
Inc(Code, ocSize + SizeOf(TCodeOffset));
end;
procedure DoIncTry; {$IFDEF Lape_Inline}inline;{$ENDIF}
begin
with POC_IncTry(PtrUInt(Code) + ocSize)^ do
if (JmpFinally = Try_NoFinally) then
_WriteLn('IncTry $%x (NoFinally)', [PtrInt(Code - CodeBase) + Jmp])
else if (JmpFinally = Try_NoExcept) then
_WriteLn('IncTry $%x (NoExcept)', [PtrInt(Code - CodeBase) + Jmp])
else
_WriteLn('IncTry $%x $%x', [PtrInt(Code - CodeBase) + Jmp, PtrInt(Code - CodeBase) + Jmp + Int32(JmpFinally)]);
Inc(Code, ocSize + SizeOf(TOC_IncTry));
end;
procedure DoDecTry; {$IFDEF Lape_Inline}inline;{$ENDIF}
begin
_WriteLn('DecTry');
Inc(Code, ocSize);
end;
procedure DoEndTry; {$IFDEF Lape_Inline}inline;{$ENDIF}
begin
_WriteLn('EndTry');
Inc(Code, ocSize);
end;
procedure DoCatchException; {$IFDEF Lape_Inline}inline;{$ENDIF}
begin
_WriteLn('CatchException');
Inc(Code, ocSize);
end;
procedure DoReRaiseException; {$IFDEF Lape_Inline}inline;{$ENDIF}
begin
_WriteLn('ReRaiseException');
Inc(Code, ocSize);
end;
procedure DoDecCall; {$IFDEF Lape_Inline}inline;{$ENDIF}
begin
_WriteLn('DecCall');
Inc(Code, ocSize);
end;
procedure DoDecCall_EndTry; {$IFDEF Lape_Inline}inline;{$ENDIF}
begin
_WriteLn('DecCall_EndTry');
Inc(Code, ocSize);
end;
{$I lpdisassembler_doinvoke.inc}
{$I lpdisassembler_dojump.inc}
{$I lpdisassembler_doeval.inc}
begin
CodeBase := Code;
{$IFDEF Lape_EmitPos}
p.Line := 0;
p.Col := 0;
{$ENDIF}
try
while True do
begin
{$IFDEF Lape_EmitPos}
with PDocPos(PPointer(PtrUInt(Code) + SizeOf(opCode))^)^ do
if (p.FileName <> FileName) or (p.Line <> Line) or (p.Col <> Col) then
begin
p.FileName := FileName;
p.Line := Line;
p.Col := Col;
_WriteLn('--> File "'+string(FileName)+'", Line '+IntToStr(Line)+', Col '+IntToStr(Col));
end;
{$ENDIF}
{$I lpinterpreter_opcodecase.inc}
end;
except
on E: Exception do
LapeExceptionFmt(lpeRuntime, [E.Message] {$IFDEF Lape_EmitPos}, PDocPos(PPointer(PtrUInt(Code) + SizeOf(opCode))^)^ {$ENDIF});
end;
end;
procedure Disassemble__EvalProcs(pMap: TLapeDisassemblerPointerMap);
var
op: EOperator;
t1, t2: ELapeBaseType;
proc: TLapeEvalProc;
begin
Assert(pMap <> nil);
for op := Low(EOperator) to High(EOperator) do
begin
if (op_name[op] = '') then
Continue;
for t1 := High(ELapeBaseType) downto Low(ELapeBaseType) do
for t2 := High(ELapeBaseType) downto Low(ELapeBaseType) do
begin
proc := getEvalProc(op, t1, t2);
if ValidEvalFunction(proc) then
if (t1 = ltUnknown) then
pMap[lpString(IntToHex(PtrUInt({$IFNDEF FPC}@{$ENDIF}proc), 0))] := 'lpe'+string(op_name[op])
else if (t2 = ltUnknown) then
pMap[lpString(IntToHex(PtrUInt({$IFNDEF FPC}@{$ENDIF}proc), 0))] := 'lpe'+string(LapeTypeToString(t1))+'_'+string(op_name[op])
else
pMap[lpString(IntToHex(PtrUInt({$IFNDEF FPC}@{$ENDIF}proc), 0))] := 'lpe'+string(LapeTypeToString(t1))+'_'+string(op_name[op]+'_'+LapeTypeToString(t2));
end;
end;
end;
procedure Disassemble__PointerMap(v: TLapeGlobalVar; AName: lpString; Compiler: TLapeCompilerBase; var Arg);
begin
if (AName = '') then
TLapeDisassemblerPointerMap(Arg)[lpString(IntToHex(PtrUInt(v.Ptr), 0))] := string(v.AsString)
else
TLapeDisassemblerPointerMap(Arg)[lpString(IntToHex(PtrUInt(v.Ptr), 0))] := string(AName);
end;
procedure DisassembleCode(Code: PByte; PointerNames: TLapeCompilerBase);
var
pMap: TLapeDisassemblerPointerMap;
begin
pMap := TLapeDisassemblerPointerMap.Create('', dupIgnore, True);
try
Disassemble__EvalProcs(pMap);
TraverseGlobals(PointerNames, @Disassemble__PointerMap, pMap);
DisassembleCode(Code, pMap);
finally
pMap.Free();
end;
end;
procedure DisassembleCode(Code: PByte; PointerNames: array of TLapeDeclArray);
var
pMap: TLapeDisassemblerPointerMap;
i,j: Integer;
begin
pMap := TLapeDisassemblerPointerMap.Create('', dupIgnore, True);
try
Disassemble__EvalProcs(pMap);
for i := 0 to High(PointerNames) do
for j := 0 to High(PointerNames[i]) do
if (PointerNames[i][j].Name = '') and (PointerNames[i][j] is TLapeGlobalVar) then
pMap[lpString(IntToHex(PtrUInt(TLapeGlobalVar(PointerNames[i][j]).Ptr), 0))] := string(TLapeGlobalVar(PointerNames[i][j]).AsString)
else if (PointerNames[i][j] is TLapeGlobalVar) then
pMap[lpString(IntToHex(PtrUInt(TLapeGlobalVar(PointerNames[i][j]).Ptr), 0))] := string(PointerNames[i][j].Name)
else
pMap[lpString(IntToHex(PtrUInt(PointerNames[i][j]), 0))] := string(PointerNames[i][j].Name);
DisassembleCode(Code, pMap);
finally
pMap.Free();
end;
end;
end.