-
Notifications
You must be signed in to change notification settings - Fork 28
/
lpinterpreter_debugevals.pas
191 lines (164 loc) · 5.61 KB
/
lpinterpreter_debugevals.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
{
Author: Niels A.D
Project: Lape (https://github.com/nielsAD/lape)
License: GNU Lesser GPL (http://www.gnu.org/licenses/lgpl.html)
}
unit lpinterpreter_debugevals;
{$i lape.inc}
interface
uses
Classes, SysUtils,
lptypes, lpinterpreter_types;
type
TLapeDebugEvals = class
protected
FHits: array of array[opCode] of Int64;
FNames: TStringArray;
FLo: PtrUInt;
FHi: PtrUInt;
FTotalHits: Int64;
function getName(Proc: TLapeEvalProc): lpString;
public
constructor Create;
destructor Destroy; override;
procedure Add(Proc: TLapeEvalProc; op: opCode);
property Name[Proc: TLapeEvalProc]: lpString read getName;
end;
implementation
uses
TypInfo, StrUtils,
lpeval, lpeval_extra;
constructor TLapeDebugEvals.Create;
var
op: EOperator;
t1, t2: ELapeBaseType;
proc: TLapeEvalProc;
i: Integer;
TheName: string;
begin
FLo := High(PtrUInt);
FHi := 0;
FNames := 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
begin
if (PtrUInt({$IFNDEF FPC}@{$ENDIF}Proc) < FLo) then FLo := PtrUInt({$IFNDEF FPC}@{$ENDIF}Proc);
if (PtrUInt({$IFNDEF FPC}@{$ENDIF}Proc) > FHi) then FHi := PtrUInt({$IFNDEF FPC}@{$ENDIF}Proc);
end;
end;
end;
// TODO
//for i := Low(LapePointerIndexEvals) to High(LapePointerIndexEvals) do
// for t1 := Low(LapeIntegerTypeRange) to High(LapeIntegerTypeRange) do
// begin
// proc := LapePointerIndexEvals[i][t1];
// if ValidEvalFunction(proc) then
// begin
// if (PtrUInt({$IFNDEF FPC}@{$ENDIF}Proc) < FLo) then FLo := PtrUInt({$IFNDEF FPC}@{$ENDIF}Proc);
// if (PtrUInt({$IFNDEF FPC}@{$ENDIF}Proc) > FHi) then FHi := PtrUInt({$IFNDEF FPC}@{$ENDIF}Proc);
// end;
// end;
//
// for t1 := Low(LapeIntegerTypeRange) to High(LapeIntegerTypeRange) do
// begin
// proc := LapeDynArrayRangeCheckEvals[t1];
// if ValidEvalFunction(proc) then
// begin
// if (PtrUInt({$IFNDEF FPC}@{$ENDIF}Proc) < FLo) then FLo := PtrUInt({$IFNDEF FPC}@{$ENDIF}Proc);
// if (PtrUInt({$IFNDEF FPC}@{$ENDIF}Proc) > FHi) then FHi := PtrUInt({$IFNDEF FPC}@{$ENDIF}Proc);
// end;
// end;
SetLength(FHits, (FHi - FLo) + 1);
SetLength(FNames, (FHi - FLo) + 1);
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
begin
if (t1 = ltUnknown) then
TheName := 'lpe'+string(op_name[op])
else if (t2 = ltUnknown) then
TheName := 'lpe'+string(LapeTypeToString(t1))+'_'+string(op_name[op])
else
TheName := 'lpe'+string(LapeTypeToString(t1))+'_'+string(op_name[op]+'_'+LapeTypeToString(t2));
FNames[PtrUInt({$IFNDEF FPC}@{$ENDIF}Proc) - FLo] := TheName;
end;
end;
end;
// TODO:
//for i := Low(LapePointerIndexEvals) to High(LapePointerIndexEvals) do
// for t1 := Low(LapeIntegerTypeRange) to High(LapeIntegerTypeRange) do
// begin
// proc := LapePointerIndexEvals[i][t1];
// if ValidEvalFunction(proc) then
// FNames[PtrUInt({$IFNDEF FPC}@{$ENDIF}Proc) - FLo] := 'lpePointerIndexBy' + IntToStr(i) + '_With' + LapeTypeToString(t1);
// end;
//
// for t1 := Low(LapeIntegerTypeRange) to High(LapeIntegerTypeRange) do
// begin
// proc := LapeDynArrayRangeCheckEvals[t1];
// if ValidEvalFunction(proc) then
// FNames[PtrUInt({$IFNDEF FPC}@{$ENDIF}Proc) - FLo] := 'lpeDynArrayRangeCheck_With' + LapeTypeToString(t1);
// end;
end;
destructor TLapeDebugEvals.Destroy;
var
i: Integer;
op: opCode;
Weights: TInt64Array;
Lines: TStringArray;
begin
Weights := nil;
Lines := nil;
for i := 0 to High(FHits) do
for op := Low(opCode) to High(opCode) do
begin
if FHits[i][op] > 0 then
begin
SetLength(Weights, Length(Weights) + 1);
SetLength(Lines, Length(Lines) + 1);
Weights[High(Weights)] := FHits[I][op];
Lines[High(Lines)] := Format('%s %s %d - %f%%', [
{$IFDEF FPC}PadRight(FNames[i], 35){$ELSE}FNames[i]{$ENDIF},
{$IFDEF FPC}PadRight(GetEnumName(TypeInfo(opCode), Ord(op)), 25){$ELSE}GetEnumName(TypeInfo(opCode), Ord(op)){$ENDIF},
FHits[i][op],
FHits[i][op] / FTotalHits * 100
]);
end;
end;
if Length(Lines) > 0 then
begin
{$IFDEF FPC}specialize{$ENDIF} TLapeSorter<Int64>.QuickSort(@Lines[0], SizeOf(String), Length(Lines), {$IFDEF FPC}specialize{$ENDIF}TLapeSorter<Int64>.TWeightArr(Weights), False);
for i := 0 to High(Lines) do
WriteLn(Lines[i]);
end;
inherited Destroy();
end;
function TLapeDebugEvals.getName(Proc: TLapeEvalProc): lpString;
begin
if (PtrUInt({$IFNDEF FPC}@{$ENDIF}Proc) >= FLo) and (PtrUInt({$IFNDEF FPC}@{$ENDIF}Proc) <= FHi) then
Result := FNames[PtrUInt({$IFNDEF FPC}@{$ENDIF}Proc) - FLo]
else
Result := 'EvalProc(' + IntToHex(PtrUInt({$IFNDEF FPC}@{$ENDIF}Proc)) + ')';
end;
procedure TLapeDebugEvals.Add(Proc: TLapeEvalProc; op: opCode);
begin
if (PtrUInt({$IFNDEF FPC}@{$ENDIF}Proc) >= FLo) and (PtrUInt({$IFNDEF FPC}@{$ENDIF}Proc) <= FHi) then
begin
Inc(FHits[PtrUInt({$IFNDEF FPC}@{$ENDIF}Proc) - FLo][op]);
Inc(FTotalHits);
end;
end;
end.