-
Notifications
You must be signed in to change notification settings - Fork 4
/
lpinterpreter.pas
593 lines (510 loc) · 17.1 KB
/
lpinterpreter.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
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
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
{
Author: Niels A.D
Project: Lape (http://code.google.com/p/la-pe/)
License: GNU Lesser GPL (http://www.gnu.org/licenses/lgpl.html)
Opcodes and bytecode interpreter.
}
unit lpinterpreter;
{$I lape.inc}
{$IFNDEF FPC}
{$UNDEF Lape_Inline}
{$ENDIF}
interface
uses
SysUtils,
lptypes;
type
opCode = (
ocNone,
ocIsInternal, //IsInternal
ocInitStackLen, //InitStackLen TStackOffset
ocInitVarLen, //InitVarLen TStackOffset
ocInitStack, //InitStack TStackOffset
ocGrowStack, //GrowStack TStackOffset
ocExpandVar, //ExpandVar TStackOffset
ocExpandVarAndInit, //ExpandVarAndInit TStackOffset
ocGrowVar, //GrowVar TStackOffset
ocGrowVarAndInit, //GrowVarAndInit TStackOffset
ocPopStackToVar, //PopStackToVar TStackOffset TVarStackOffset
ocPopVarToStack, //PopVarToStack TStackOffset TVarStackOffset
ocPopVar, //PopVar
ocJmpVar, //JmpVar
ocJmpSafe, //JmpSafe TCodePos
ocJmpSafeR, //JmpSafeR TCodeOffset
ocIncTry, //IncTry TCodeOffset UInt32
ocDecTry, //DecTry
ocEndTry, //EndTry
ocCatchException, //CatchException
ocDecCall, //DecCall
ocDecCall_EndTry, //DecCall_EndTry
{$I lpinterpreter_invokeopcodes.inc}
{$I lpinterpreter_jumpopcodes.inc}
{$I lpinterpreter_evalopcodes.inc}
);
opCodeTypeP = ^opCodeType;
{$IFDEF Lape_SmallCode}
opCodeType = Byte;
{$ELSE}
opCodeType = Integer; //Better alignment
{$ENDIF}
POC_PopStackToVar = ^TOC_PopStackToVar;
TOC_PopStackToVar = record
Size: TStackOffset;
VOffset: TVarStackOffset;
end;
POC_IncTry = ^TOC_IncTry;
TOC_IncTry = record
Jmp: TCodeOffset;
JmpFinally: UInt32;
end;
{$I lpinterpreter_invokerecords.inc}
{$I lpinterpreter_jumprecords.inc}
{$I lpinterpreter_evalrecords.inc}
const
ocSize = SizeOf(opCodeType) {$IFDEF Lape_EmitPos}+SizeOf(TDocPos){$ENDIF};
Try_NoFinally: UInt32 = UInt32(-1);
Try_NoExcept: UInt32 = UInt32(-2);
EndJump: TCodePos = TCodePos(-1);
StackSize = 2048 * SizeOf(Pointer);
VarStackSize = 512 * SizeOf(Pointer);
VarStackStackSize = 32;
TryStackSize = 512;
CallStackSize = 512;
procedure RunCode(Code: PByte; var DoContinue: TInitBool; InitialVarStack: TByteArray = nil; InitialJump: TCodePos = 0); overload;
procedure RunCode(Code: PByte; InitialVarStack: TByteArray = nil; InitialJump: TCodePos = 0); overload;
implementation
uses
lpexceptions;
{$OverFlowChecks Off}
type
TInJump = record
JumpException: record
Obj: Exception;
Pos: PDocPos;
end;
JumpSafe: PByte;
end;
const
InEmptyJump: TInJump = (JumpException: (Obj: nil; Pos: nil); JumpSafe: nil);
procedure MergeJumps(var AJump: TInJump; const Merge: TInJump);
begin
if (Merge.JumpException.Obj <> nil) then
begin
if (AJump.JumpException.Obj <> nil) and (AJump.JumpException.Obj <> Merge.JumpException.Obj) then
Merge.JumpException.Obj.Free()
else
AJump.JumpException := Merge.JumpException;
end;
if (Merge.JumpSafe > AJump.JumpSafe) then
AJump.JumpSafe := Merge.JumpSafe;
end;
procedure RunCode(Code: PByte; var DoContinue: TInitBool; InitialVarStack: TByteArray = nil; InitialJump: TCodePos = 0);
const
opNone: opCodeType = opCodeType(ocNone);
var
CodeBase: PByte;
Stack: TByteArray;
StackPos: UInt32;
VarStack: TByteArray;
VarStackIndex, VarStackPos, VarStackLen: UInt32;
VarStackStack: array of record
Stack: TByteArray;
Pos: UInt32;
end;
TryStack: array of record
Jmp: PByte;
JmpFinally: PByte;
end;
TryStackPos: UInt32;
InJump: TInJump;
CallStack: array of record
CalledFrom: PByte;
StackP, VarStackP: UInt32;
Jump: TInJump;
end;
CallStackPos: UInt32;
procedure ExpandVarStack(Size: UInt32); {$IFDEF Lape_Inline}inline;{$ENDIF}
begin
if (VarStackLen + Size > UInt32(Length(VarStack))) then
begin
VarStackStack[VarStackIndex].Pos := VarStackLen;
Inc(VarStackIndex);
if (VarStackIndex >= UInt32(Length(VarStackStack))) then
SetLength(VarStackStack, VarStackIndex + (VarStackStackSize div 2));
VarStackPos := 0;
VarStackLen := Size;
VarStack := VarStackStack[VarStackIndex].Stack;
if (Size > VarStackSize) then
SetLength(VarStack, Size)
else if (VarStack = nil) then
SetLength(VarStack, VarStackSize);
VarStackStack[VarStackIndex].Stack := VarStack;
end
else
begin
VarStackPos := VarStackLen;
Inc(VarStackLen, Size);
end;
end;
procedure GrowVarStack(Size: UInt32); {$IFDEF Lape_Inline}inline;{$ENDIF}
var
OldLen: UInt32;
begin
if (VarStackLen + Size > UInt32(Length(VarStack))) then
if (VarStackPos = 0) then
begin
Inc(VarStackLen, Size);
SetLength(VarStack, VarStackLen);
VarStackStack[VarStackIndex].Stack := VarStack;
end
else
begin
OldLen := VarStackLen - VarStackPos;
ExpandVarStack(Size + OldLen);
with VarStackStack[VarStackIndex - 1] do
begin
Dec(Pos, OldLen);
Move(Stack[Pos], VarStack[0], OldLen);
end;
end
else
Inc(VarStackLen, Size);
end;
procedure JumpTo(const Target: TCodePos); {$IFDEF Lape_Inline}inline;{$ENDIF}
begin
if (Target = 0) then
LapeException(lpeInvalidJump);
Code := PByte(PtrUInt(CodeBase) + Target);
end;
procedure JumpToRelative(const Offset: TCodeOffset); {$IFDEF Lape_Inline}inline;{$ENDIF}
begin
Inc(Code, Offset);
end;
procedure HandleException; {$IFDEF Lape_Inline}inline;{$ENDIF}
begin
if (TryStackPos > 0) then
begin
Dec(TryStackPos);
Code := TryStack[TryStackPos].Jmp;
end
else
raise InJump.JumpException.Obj;
end;
procedure HandleSafeJump; {$IFDEF Lape_Inline}inline;{$ENDIF}
var
IsEndJump: Boolean;
begin
IsEndJump := (CodeBase = PByte(PtrUInt(InJump.JumpSafe) - EndJump));
while (TryStackPos > 0) and (IsEndJump or (TryStack[TryStackPos - 1].Jmp < InJump.JumpSafe)) and (TryStack[TryStackPos - 1].JmpFinally = nil) do
Dec(TryStackPos);
if (TryStackPos > 0) and (IsEndJump or (TryStack[TryStackPos - 1].Jmp < InJump.JumpSafe)) and (TryStack[TryStackPos - 1].JmpFinally <> nil) then
begin
Assert(IsEndJump or (TryStack[TryStackPos - 1].JmpFinally >= Code));
Dec(TryStackPos);
Code := TryStack[TryStackPos].JmpFinally;
end
else if (CodeBase = PByte(PtrUInt(InJump.JumpSafe) - EndJump)) then
Code := @opNone
else
begin
Code := InJump.JumpSafe;
InJump.JumpSafe := nil;
end;
end;
procedure PushToVar(const Size: TStackOffset); {$IFDEF Lape_Inline}inline;{$ENDIF}
begin
Dec(StackPos, Size);
ExpandVarStack(Size);
Move(Stack[StackPos], VarStack[VarStackPos], Size);
end;
procedure DoCheckInternal; {$IFDEF Lape_Inline}inline;{$ENDIF}
begin
try
PEvalBool(@Stack[StackPos - SizeOf(Pointer)])^ := opCodeTypeP(PtrUInt(CodeBase) + PtrUInt(PCodePos(@Stack[StackPos - SizeOf(Pointer)])^))^ = opCodeType(ocIncTry);
except
PEvalBool(@Stack[StackPos - SizeOf(Pointer)])^ := False;
end;
Dec(StackPos, SizeOf(Pointer) - SizeOf(EvalBool));
Inc(Code, ocSize);
end;
procedure DoInitStackLen; {$IFDEF Lape_Inline}inline;{$ENDIF}
var
InitStackSize: TStackOffset;
begin
InitStackSize := PStackOffset(PtrUInt(Code) + ocSize)^;
if (StackPos + InitStackSize > UInt32(Length(Stack))) then
SetLength(Stack, StackPos + InitStackSize + (StackSize div 2));
Inc(Code, SizeOf(TStackOffset) + ocSize);
end;
procedure DoInitVarLen; {$IFDEF Lape_Inline}inline;{$ENDIF}
begin
SetLength(VarStack, PStackOffset(PtrUInt(Code) + ocSize)^);
VarStackStack[VarStackIndex].Stack := VarStack;
Inc(Code, SizeOf(TStackOffset) + ocSize);
end;
procedure DoInitStack; {$IFDEF Lape_Inline}inline;{$ENDIF}
var
InitStackSize: TStackOffset;
begin
InitStackSize := PStackOffset(PtrUInt(Code) + ocSize)^;
if (StackPos + InitStackSize > UInt32(Length(Stack))) then
SetLength(Stack, StackPos + InitStackSize + (StackSize div 2));
FillChar(Stack[StackPos], InitStackSize, 0);
Inc(Code, SizeOf(TStackOffset) + ocSize);
end;
procedure DoGrowStack; {$IFDEF Lape_Inline}inline;{$ENDIF}
var
GrowSize: TStackOffset;
begin
GrowSize := PStackOffset(PtrUInt(Code) + ocSize)^;
if (StackPos + GrowSize > UInt32(Length(Stack))) then
SetLength(Stack, StackPos + GrowSize + (StackSize div 2));
Inc(StackPos, GrowSize);
Inc(Code, SizeOf(TStackOffset) + ocSize);
end;
procedure DoExpandVar; {$IFDEF Lape_Inline}inline;{$ENDIF}
begin
ExpandVarStack(PStackOffset(PtrUInt(Code) + ocSize)^);
Inc(Code, SizeOf(TStackOffset) + ocSize);
end;
procedure DoExpandVarAndInit; {$IFDEF Lape_Inline}inline;{$ENDIF}
var
ExpandSize: TStackOffset;
begin
ExpandSize := PStackOffset(PtrUInt(Code) + ocSize)^;
ExpandVarStack(ExpandSize);
FillChar(VarStack[VarStackPos], ExpandSize, 0);
Inc(Code, SizeOf(TStackOffset) + ocSize);
end;
procedure DoGrowVar; {$IFDEF Lape_Inline}inline;{$ENDIF}
begin
GrowVarStack(PStackOffset(PtrUInt(Code) + ocSize)^);
Inc(Code, SizeOf(TStackOffset) + ocSize);
end;
procedure DoGrowVarAndInit; {$IFDEF Lape_Inline}inline;{$ENDIF}
var
GrowSize: TStackOffset;
begin
GrowSize := PStackOffset(PtrUInt(Code) + ocSize)^;
GrowVarStack(GrowSize);
FillChar(VarStack[VarStackLen - GrowSize], GrowSize, 0);
Inc(Code, SizeOf(TStackOffset) + ocSize);
end;
procedure DoPopVar; {$IFDEF Lape_Inline}inline;{$ENDIF}
begin
if (VarStackPos = 0) and (VarStackIndex > 0) then
begin
Dec(VarStackIndex);
with VarStackStack[VarStackIndex] do
begin
VarStack := Stack;
VarStackPos := Pos;
end;
end;
VarStackLen := VarStackPos;
Inc(Code, ocSize);
end;
procedure DoPopStackToVar; {$IFDEF Lape_Inline}inline;{$ENDIF}
begin
with POC_PopStackToVar(PtrUInt(Code) + ocSize)^ do
begin
Dec(StackPos, Size);
Move(Stack[StackPos], VarStack[VarStackPos + VOffset], 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
Move(VarStack[VarStackPos + VOffset], Stack[StackPos], Size);
FillChar(VarStack[VarStackPos + VOffset], Size, 0);
Inc(StackPos, Size);
end;
Inc(Code, ocSize + SizeOf(TOC_PopStackToVar));
end;
procedure DoJmpVar; {$IFDEF Lape_Inline}inline;{$ENDIF}
begin
Dec(StackPos, SizeOf(TCodePos));
//JumpTo(PCodePos(@Stack[StackPos])^);
InJump.JumpSafe := @Stack[StackPos];
if (PCodePos(InJump.JumpSafe)^ = 0) then
LapeException(lpeInvalidJump);
InJump.JumpSafe := PByte(PtrUInt(CodeBase) + PCodePos(InJump.JumpSafe)^);
HandleSafeJump();
end;
procedure DoJmpSafe; {$IFDEF Lape_Inline}inline;{$ENDIF}
begin
InJump.JumpSafe := PByte(PtrUInt(CodeBase) + PCodePos(PtrUInt(Code) + ocSize)^);
HandleSafeJump();
end;
procedure DoJmpSafeR; {$IFDEF Lape_Inline}inline;{$ENDIF}
begin
InJump.JumpSafe := PByte(PtrInt(Code) + PCodeOffset(PtrUInt(Code) + ocSize)^);
HandleSafeJump();
end;
procedure DoIncTry; {$IFDEF Lape_Inline}inline;{$ENDIF}
begin
if (TryStackPos >= UInt32(Length(TryStack))) then
SetLength(TryStack, TryStackPos + (TryStackSize div 2));
with POC_IncTry(PtrUInt(Code) + ocSize)^ do
begin
TryStack[TryStackPos].Jmp := PByte(PtrInt(Code) + Jmp);
if (JmpFinally = Try_NoFinally) then
TryStack[TryStackPos].JmpFinally := nil
else if (JmpFinally = Try_NoExcept) then
TryStack[TryStackPos].JmpFinally := TryStack[TryStackPos].Jmp
else
TryStack[TryStackPos].JmpFinally := PByte(PtrInt(Code) + Int32(JmpFinally) + Jmp);
end;
Inc(TryStackPos);
Inc(Code, ocSize + SizeOf(TOC_IncTry));
end;
procedure DoDecTry; {$IFDEF Lape_Inline}inline;{$ENDIF}
begin
Dec(TryStackPos);
Inc(Code, ocSize);
end;
procedure DoEndTry; {$IFDEF Lape_Inline}inline;{$ENDIF}
begin
if (InJump.JumpException.Obj <> nil) then
HandleException()
else if (InJump.JumpSafe <> nil) then
HandleSafeJump()
else
Inc(Code, ocSize);
end;
procedure DoCatchException; {$IFDEF Lape_Inline}inline;{$ENDIF}
begin
FreeAndNil(InJump.JumpException.Obj);
Inc(Code, ocSize);
end;
procedure DoIncCall(RecSize: Integer; Jmp: TCodePos; ParamSize: TParamSize; StackPosOffset: TStackInc = 0); {$IFDEF Lape_Inline}inline;{$ENDIF}
begin
if (CallStackPos >= UInt32(Length(CallStack))) then
SetLength(CallStack, CallStackPos + (CallStackSize div 2));
with CallStack[CallStackPos] do
begin
CalledFrom := PByte(PtrInt(Code) + ocSize + RecSize);
VarStackP := VarStackPos;
PushToVar(ParamSize);
StackP := StackPos + UInt32(StackPosOffset);
Jump := InJump;
InJump := InEmptyJump;
JumpTo(Jmp);
end;
Inc(CallStackPos);
end;
procedure DoDecCall; {$IFDEF Lape_Inline}inline;{$ENDIF}
begin
if (CallStackPos <= 0) then
Code := @opNone
else
begin
DoPopVar();
Dec(CallStackPos);
with CallStack[CallStackPos] do
begin
Code := CalledFrom;
VarStackPos := VarStackP;
StackPos := StackP;
MergeJumps(InJump, Jump);
end;
end;
end;
procedure DoDecCall_EndTry; {$IFDEF Lape_Inline}inline;{$ENDIF}
var
tmp: TInJump;
begin
tmp := InJump;
DoDecCall();
if (tmp.JumpException.Obj = InJump.JumpException.Obj) and (InJump.JumpException.Obj <> nil) then
HandleException()
else if (tmp.JumpSafe = InJump.JumpSafe) and (InJump.JumpSafe <> nil) then
HandleSafeJump();
end;
procedure DoInvokeImportedProc(RecSize: Integer; Ptr: Pointer; ParamSize: UInt16; StackPosOffset: Integer = 0); {$IFDEF Lape_Inline}inline;{$ENDIF}
begin
TLapeImportedProc(Ptr)(@Stack[StackPos - ParamSize]);
Dec(StackPos, ParamSize - StackPosOffset);
Inc(Code, RecSize + ocSize);
end;
procedure DoInvokeImportedFunc(RecSize: Integer; Ptr, Res: Pointer; ParamSize: UInt16; StackPosOffset: Integer = 0); {$IFDEF Lape_Inline}inline;{$ENDIF}
begin
TLapeImportedFunc(Ptr)(@Stack[StackPos - ParamSize], Res);
Dec(StackPos, ParamSize - StackPosOffset);
Inc(Code, RecSize + ocSize);
end;
{$I lpinterpreter_doinvoke.inc}
{$I lpinterpreter_dojump.inc}
{$I lpinterpreter_doeval.inc}
procedure DaLoop; {$IFDEF Lape_Inline}inline;{$ENDIF}
var
GoBack: Boolean;
label
Start;
begin
Start:
GoBack := False;
try
while (DoContinue = bTrue) do {$I lpinterpreter_opcodecase.inc}
except
{$IFDEF Lape_EmitPos}
if (ExceptObject <> InJump.JumpException.Obj) then
InJump.JumpException.Pos := PDocPos(PtrUInt(Code) + SizeOf(opCodeType));
{$ENDIF}
InJump.JumpException.Obj := Exception(AcquireExceptionObject());
HandleException();
GoBack := True;
end;
if (DoContinue = bUnknown) then
begin
Sleep(1);
goto Start;
end
else if (DoContinue = bFalse) then
begin
DoContinue := bTrue;
InJump.JumpSafe := PByte(PtrUInt(CodeBase) + EndJump);
HandleSafeJump();
goto Start;
end;
if GoBack then
goto Start;
end;
begin
CodeBase := Code;
SetLength(Stack, StackSize);
SetLength(TryStack, TryStackSize);
SetLength(CallStack, CallStackSize);
VarStackIndex := 0;
SetLength(VarStackStack, VarStackStackSize);
VarStackLen := Length(InitialVarStack);
if (VarStackLen < VarStackSize) then
SetLength(InitialVarStack, VarStackSize);
VarStackStack[0].Stack := InitialVarStack;
VarStack := VarStackStack[0].Stack;
StackPos := 0;
VarStackPos := 0;
TryStackPos := 0;
CallStackpos := 0;
InJump := InEmptyJump;
try
Code := PByte(PtrUInt(CodeBase) + InitialJump);
DaLoop();
except
on E: Exception do
if (E = InJump.JumpException.Obj) and (InJump.JumpException.Pos <> nil) then
LapeExceptionFmt(lpeRuntime, [E.Message], InJump.JumpException.Pos^)
else
LapeExceptionFmt(lpeRuntime, [E.Message]);
end;
end;
procedure RunCode(Code: PByte; InitialVarStack: TByteArray = nil; InitialJump: TCodePos = 0);
var
DoContinue: TInitBool;
begin
DoContinue := bTrue;
RunCode(Code, DoContinue, InitialVarStack, InitialJump);
end;
end.