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
|
(*
This file is distributed under the Lesser GNU General Public License
(see the file COPYING.LGPL) with the following modification:
As a special exception, the copyright holders of this library give you
permission to link this library with independent modules to produce an
executable, regardless of the license terms of these independent modules,
and to copy and distribute the resulting executable under terms of your choice,
provided that you also meet, for each linked independent module, the terms
and conditions of the license of that module. An independent module is a
module which is not derived from or based on this library. If you modify this
library, you may extend this exception to your version of the library, but
you are not obligated to do so. If you do not wish to do so, delete this
exception statement from your version.
If you didn't receive a copy of the file COPYING.LGPL, contact:
Free Software Foundation, Inc.,
675 Mass Ave
Cambridge, MA 02139
USA
*)
unit JitHelper;
{$mode objfpc}{$H+}
{$ModeSwitch typehelpers}
{$ModeSwitch advancedrecords}
{.$Inline off}
interface
uses
SysUtils, TypInfo, JitRttiWriter, Rtti;
type
{ TProcCallParamList
Unified access to params of tkProcVar and tkMethod
}
TProcCallParam = record
ParamName: String;
ParamFlags: TParamFlags;
ParamTypeRef: PTypeInfo;
end;
TProcCallParamList = record
Kind: TTypeKind;
MethodKind : TMethodKind;
CC: TCallConv;
ResultType: PTypeInfo;
ParamCount: integer;
Params: array of TProcCallParam;
end;
{ TPropDataPointerHelper }
TPropDataPointerHelper = type helper for PPropData
function SizeOf: Integer;
end;
{ TTypeDataPointerHelper }
TTypeDataPointerHelper = type helper for PTypeData
function DataSize(AKind: TTypeKind): Integer;
// for tkClass
function PtrPropData: PPropData; inline;
end;
{ TTypeInfoPointerHelper }
TTypeInfoPointerHelper = type helper for PTypeInfo
function SizeOf: Integer;
function PtrPropData: PPropData; inline;
function DataSize: Integer; inline;
function IsManaged: boolean; inline;
// for tkMethod / tkProcVar
function GetTkMethodData(out ParamList: TProcCallParamList): Boolean;
end;
implementation
{ TPropDataPointerHelper }
function TPropDataPointerHelper.SizeOf: Integer;
var
i: SmallInt;
p: PPropInfo;
begin
i := Self^.PropCount;
p := Self^.Prop[0];
while i > 0 do begin
p := p^.Next;
dec(i);
end;
Result := pointer(p) - pointer(Self);
end;
{ TTypeDataPointerHelper }
function TTypeDataPointerHelper.DataSize(AKind: TTypeKind): Integer;
begin
Result := 4;
case AKind of
tkInterface, tkInterfaceRaw,
tkDynArray,
tkClass,
tkWString, tkUString, tkAString:
Result:= sizeof(Pointer);
tkChar, tkBool:
Result:=1;
tkWChar:
Result:=2;
tkSet:
Result := Self^.SetSize; // In ver_3_0 use OrdType
tkEnumeration,
tkInteger:
case Self^.OrdType of
otSByte,otUByte: Result := 1;
otSWord,otUWord: Result := 2;
otSQWord,otUQWord: Result := 8;
end;
tkInt64 :
Result:=8;
tkQword :
Result:=8;
tkMethod:
Result := SizeOf(TMethod);
tkProcVar:
Result := SizeOf(CodePointer);
tkRecord:
Result := Self^.RecSize;
end;
end;
function TTypeDataPointerHelper.PtrPropData: PPropData;
begin
Result := aligntoptr(pointer(@Self^.UnitName)+Length(Self^.UnitName)+1);
end;
{ TTypeInfoPointerHelper }
function TTypeInfoPointerHelper.SizeOf: Integer;
var
t: PTypeData;
pd: PPropData;
begin
t := GetTypeData(Self);
case Self^.Kind of
tkClass: begin
pd := t.PtrPropData;
Result := pointer(pd) - pointer(Self) + pd.SizeOf;
end;
else begin
Result := (pointer(t) - pointer(Self)) + system.SizeOf(TTypeData);
end;
end;
end;
function TTypeInfoPointerHelper.PtrPropData: PPropData;
begin
Result := GetTypeData(Self).PtrPropData;
end;
function TTypeInfoPointerHelper.DataSize: Integer;
begin
Result := GetTypeData(Self).DataSize(Self^.Kind);
end;
function TTypeInfoPointerHelper.IsManaged: boolean;
begin
Result := Rtti.IsManaged(Self);
end;
function TTypeInfoPointerHelper.GetTkMethodData(out ParamList: TProcCallParamList): Boolean;
type
PParamFlags = ^TParamFlags;
PCallConv = ^ TCallConv;
PProcedureSignature = ^TProcedureSignature;
var
td: PTypeData;
Cnt, i: Integer;
mem: Pointer;
sig: PProcedureSignature;
par: PProcedureParam;
begin
Result := True;
ParamList.Kind := Self^.Kind;
td := GetTypeData(Self);
case Self^.Kind of
tkMethod: begin
ParamList.MethodKind := td^.MethodKind;
Cnt := td^.ParamCount;
ParamList.ParamCount := Cnt;
SetLength(ParamList.Params, Cnt);
mem := @td^.ParamList;
for i := 0 to Cnt - 1 do begin
ParamList.Params[i].ParamFlags := PParamFlags(mem)^;
inc(PParamFlags(mem));
ParamList.Params[i].ParamName := PShortString(mem)^;
inc(mem, PByte(mem)^ + 1); // skip shortstring / ParamName
inc(mem, PByte(mem)^ + 1); // skip shortstring / TypeName
end;
if ParamList.MethodKind in [mkFunction, mkClassFunction] then begin
inc(mem, PByte(mem)^ + 1); // skip shortstring / Result TypeName
mem := aligntoptr(mem);
ParamList.ResultType := TypeInfoPtrToPTypeInfo(PTypeInfoPtr(mem)^);
inc(PTypeInfoPtr(mem));
end;
ParamList.CC := PCallConv(mem)^;
inc(PCallConv(mem));
mem := AlignTypeData(mem);
for i := 0 to Cnt - 1 do begin
ParamList.Params[i].ParamTypeRef := TypeInfoPtrToPTypeInfo(PTypeInfoPtr(mem)^);
inc(PTypeInfoPtr(mem));
end;
end;
tkProcVar: begin
sig := @td^.ProcSig;
if sig^.ResultTypeRef <> nil then
ParamList.MethodKind := mkFunction
else
ParamList.MethodKind := mkProcedure;
ParamList.CC := sig^.CC;
ParamList.ResultType:= sig^.ResultType;
Cnt := sig^.ParamCount;
ParamList.ParamCount := Cnt;
SetLength(ParamList.Params, Cnt);
for i := 0 to Cnt - 1 do begin
par := sig^.GetParam(i);
ParamList.Params[i].ParamFlags := par^.ParamFlags;
ParamList.Params[i].ParamName := par^.Name;
ParamList.Params[i].ParamTypeRef := par^.ParamType;
end;
end;
else
Result := False;
end;
end;
end.
|