Repository navigation
Expand file tree
/
Copy pathGenerateJITUnit.pas
More file actions
188 lines (159 loc) · 6.02 KB
/
Copy pathGenerateJITUnit.pas
File metadata and controls
188 lines (159 loc) · 6.02 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
{
This file is part of the MWA Software Pascal API Code Generator for OpenSSL .
Copyright © MWA Software 2024
This program is free software: you can redistribute it and/or modify it under
the terms of the GNU General Public License as published by the Free Software
Foundation, either version 3 of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with this program.
If not, see <https://www.gnu.org/licenses/>.
}
unit GenerateJITUnit;
{$IFDEF FPC}
{$mode Delphi}
{$ENDIF}
interface
(* The TGenerateJITUnit is a subclass of TGenerateSmartLoadUnit and is used to
customise the API function load strategy to a "Just in Time" approach.
Here, instead of loading every API function at load time, only API functions
marked as "allow nil" are loaded. The remaining API functions are initialised
(at compile time) to a specialised "load function". Thus, the first time a
given API function is called, it is the load function that is invoked and not
the API call.
The load function attempts to load the actual API function call and if successful
set ths API function variable to the actual API function call. It then calls the
API function on the caller's behalf and returns any result. The next time the
API function is called, the actual function is called.
If the actual API function call fails to load then a compatibility function is
used instead, if one exists, otherwise an exception is raised.
The advantages of this strategy over "smart loading" are:
- quicker library loading
- Only the used API calls are loaded.
*)
uses
Classes , SysUtils, GenerateHeaderUnit, APIFileReader, GenerateSmartLoad,
ProgramConstants;
type
{ TGenerateJITUnit }
TGenerateJITUnit = class(TGenerateSmartLoadUnit)
protected
procedure AddDynamicInterfaceSection(S : TStrings; firstFuncProc : integer); override;
procedure AddDynamicImplementationSection(S: TStrings; firstFuncProc: integer); override;
function GetInitialiser(funcProc : IFuncProcInfo) : string; override;
function PreLoadFunction(funcProc: IFuncProcInfo): boolean; override;
end;
implementation
{ TGenerateJITUnit }
procedure TGenerateJITUnit.AddDynamicInterfaceSection(S : TStrings;
firstFuncProc : integer);
var i: integer;
needsComment: boolean;
InLegacy: boolean;
funcProc: IFuncProcInfo;
begin
needsComment := true;
InLegacy := false;
with InterfaceSection do
for i := firstFuncProc to Count - 1 do
begin
if TObject(Items[i]) is TDirective then
with TDirective(Items[i]) do
S.Add('{$' + Name + Condition + '}')
else
if TObject(Items[i]) is IFuncProcInfo then
begin
funcProc := TObject(Items[i]) as IFuncProcInfo;
with funcProc do
if not AllowNil then
begin
if needsComment then
begin
S.Add('');
S.Add('{Declare external function initialisers - should not be called directly}');
S.Add('');
needsComment := false;
end;
AddNoLegacyConditionals(S,funcProc,InLegacy);
if IsFunction then
S.Add('function Load_' + ProcName + ProcHeader + ': ' + ResultType + '; cdecl;')
else
S.Add('procedure Load_' + ProcName + ProcHeader + '; cdecl;');
end;
end;
end;
AddNoLegacyConditionals(S,nil,InLegacy);
S.Add('');
inherited AddDynamicInterfaceSection(S , firstFuncProc);
end;
procedure TGenerateJITUnit.AddDynamicImplementationSection(S : TStrings;
firstFuncProc : integer);
var i: integer;
funcProc: IFuncProcInfo;
InLegacy: boolean;
begin
inherited AddDynamicImplementationSection(S,firstFuncProc);
InLegacy := false;
{Create an initialiser function for each dynamic API variable listed in TAPIInitializer}
with InterfaceSection do
for i := firstFuncProc to Count - 1 do
begin
if TObject(Items[i]) is TDirective then
with TDirective(Items[i]) do
S.Add('{$' + Name + Condition + '}')
else
if TObject(Items[i]) is IFuncProcInfo then
begin
funcProc := TObject(Items[i]) as IFuncProcInfo;
if not PreLoadFunction(funcProc) then
with funcProc do
begin
AddNoLegacyConditionals(S,funcProc,InLegacy);
if IsFunction then
S.Add('function Load_' + ProcName + ProcHeader + ': ' + ResultType + '; cdecl;')
else
S.Add('procedure Load_' + ProcName + ProcHeader + '; cdecl;');
S.Add('begin');
S.Add(' ' + procName + ' := Load' + LibName + 'Function(''' + ProcName + ''');');
S.Add(' if not assigned(' + procName + ') then');
if CompatibilityFunctions.IndexOf(ProcName) <> -1 then
begin
if not InLegacy then
S.Add('{$IFNDEF ' + NoLegacySupportSymbol + '}');
S.Add(' ' + procName + ' := @COMPAT_' + ProcName + ';');
if not InLegacy then
begin
S.Add('{$ELSE}');
S.Add(' ' + ErrorExceptionClassName + '.RaiseException(''' + ProcName + ''');');
S.Add('{$ENDIF} { End of ' + NoLegacySupportSymbol + '}');
end;
end
else
S.Add(' ' + ErrorExceptionClassName + '.RaiseException(''' + ProcName + ''');');
if IsFunction then
S.Add(' Result := ' + procName + '(' + GetParamNames.CommaText + ');')
else
S.Add(' ' + procName + '(' + GetParamNames.CommaText + ');');
S.Add('end;');
S.Add('');
end;
end;
end;
AddNoLegacyConditionals(S,nil,InLegacy);
end;
function TGenerateJITUnit.GetInitialiser(funcProc : IFuncProcInfo) : string;
begin
with funcProc do
if AllowNil then
Result := 'nil'
else
Result := 'Load_' + ProcName;
end;
function TGenerateJITUnit.PreLoadFunction(funcProc : IFuncProcInfo) : boolean;
begin
with funcProc do
Result := AllowNil;
end;
end.