-
Notifications
You must be signed in to change notification settings - Fork 8
/
LLVM.Imports.Target.pas
205 lines (155 loc) · 6.83 KB
/
LLVM.Imports.Target.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
unit LLVM.Imports.Target;
interface
//based on Target.h
uses
LLVM.Imports,
LLVM.Imports.Types;
{$MINENUMSIZE 4}
type
TLLVMByteOrdering = (LLVMBigEndian, LLVMLittleEndian);
TLLVMTargetDataRef = type TLLVMRef;
TLLVMTargetLibraryInfoRef = type TLLVMRef;
//LLVM supports macros to generate the functions to import
//we'll simply do it at runtime by providing parameterized functions
//which allows to initialize other new targets in the future even if they're not listed here
const
//Targets
CLLVMTARGET_AArch64 = 'AArch64';
CLLVMTARGET_AMDGPU = 'AMDGPU';
CLLVMTARGET_ARM = 'ARM';
CLLVMTARGET_BPF = 'BPF';
CLLVMTARGET_Hexagon = 'Hexagon';
CLLVMTARGET_Lanai = 'Lanai';
CLLVMTARGET_Mips = 'Mips';
CLLVMTARGET_MSP430 = 'MSP430';
CLLVMTARGET_NVPTX = 'NVPTX';
CLLVMTARGET_PowerPC = 'PowerPC';
CLLVMTARGET_Sparc = 'Sparc';
CLLVMTARGET_SystemZ = 'SystemZ';
CLLVMTARGET_X86 = 'X86';
CLLVMTARGET_XCore = 'XCore';
//AsmPrinters
CLLVMASMPRINTER_AArch64 = 'AArch64';
CLLVMASMPRINTER_AMDGPU = 'AMDGPU';
CLLVMASMPRINTER_ARM = 'ARM';
CLLVMASMPRINTER_BPF = 'BPF';
CLLVMASMPRINTER_Hexagon = 'Hexagon';
CLLVMASMPRINTER_Lanai = 'Lanai';
CLLVMASMPRINTER_Mips = 'Mips';
CLLVMASMPRINTER_MSP430 = 'MSP430';
CLLVMASMPRINTER_NVPTX = 'NVPTX';
CLLVMASMPRINTER_PowerPC = 'PowerPC';
CLLVMASMPRINTER_Sparc = 'Sparc';
CLLVMASMPRINTER_SystemZ = 'SystemZ';
CLLVMASMPRINTER_X86 = 'X86';
CLLVMASMPRINTER_XCore = 'XCore';
//AsmParser
CLLVMASMPARSER_AArch64 = 'AArch64';
CLLVMASMPARSER_AMDGPU = 'AMDGPU';
CLLVMASMPARSER_ARM = 'ARM';
CLLVMASMPARSER_Hexagon = 'Hexagon';
CLLVMASMPARSER_Lanai = 'Lanai';
CLLVMASMPARSER_Mips = 'Mips';
CLLVMASMPARSER_PowerPC = 'PowerPC';
CLLVMASMPARSER_Sparc = 'Sparc';
CLLVMASMPARSER_SystemZ = 'SystemZ';
CLLVMASMPARSER_X86 = 'X86';
//Disassembler
CLLVMDISASSEMBLER_AArch64 = 'AArch64';
CLLVMDISASSEMBLER_AMDGPU = 'AMDGPU';
CLLVMDISASSEMBLER_ARM = 'ARM';
CLLVMDISASSEMBLER_BPF = 'BPF';
CLLVMDISASSEMBLER_Hexagon = 'Hexagon';
CLLVMDISASSEMBLER_Lanai = 'Lanai';
CLLVMDISASSEMBLER_Mips = 'Mips';
CLLVMDISASSEMBLER_PowerPC = 'PowerPC';
CLLVMDISASSEMBLER_Sparc = 'Sparc';
CLLVMDISASSEMBLER_SystemZ = 'SystemZ';
CLLVMDISASSEMBLER_X86 = 'x86';
CLLVMDISASSEMBLER_XCore = 'XCore';
//routines to dynamic call a specific function by name
function LLVMInitializeTargetInfo(const TargetName: string): Boolean;
function LLVMInitializeTarget(const TargetName: string): Boolean;
function LLVMInitializeTargetMC(const TargetName: string): Boolean;
function LLVMInitializeAsmPrinter(const AsmPrinterName: string): Boolean;
function LLVMInitializeAsmParser(const AsmParserName: string): Boolean;
function LLVMInitializeDisassembler(const DisassemblerName: string): Boolean;
function LLVMGetModuleDataLayout(M: TLLVMModuleRef): TLLVMTargetDataRef; cdecl; external CLLVMLibrary;
procedure LLVMSetModuleDataLayout(M: TLLVMModuleRef; DL: TLLVMTargetDataRef); cdecl; external CLLVMLibrary;
function LLVMCreateTargetData(const StringRep: PLLVMChar): TLLVMTargetDataRef; cdecl; external CLLVMLibrary;
procedure LLVMDisposeTargetData(TD: TLLVMTargetDataRef); cdecl; external CLLVMLibrary;
procedure LLVMAddTargetLibraryInfo(TLI: TLLVMTargetLibraryInfoRef; PM: TLLVMPassManagerRef); cdecl; external CLLVMLibrary;
function LLVMCopyStringRepOfTargetData(TD: TLLVMTargetDataRef): PLLVMChar; cdecl; external CLLVMLibrary;
function LLVMByteOrder(TD: TLLVMTargetDataRef): TLLVMByteOrdering; cdecl; external CLLVMLibrary;
function LLVMPointerSize(TD: TLLVMTargetDataRef): Cardinal; cdecl; external CLLVMLibrary;
function LLVMPointerSizeForAS(TD: TLLVMTargetDataRef; AdressSpace: Cardinal): Cardinal; cdecl; external CLLVMLibrary;
function LLVMIntPtrType(TD: TLLVMTargetDataRef): TLLVMTypeRef; cdecl; external CLLVMLibrary;
function LLVMIntPtrTypeForAS(TD: TLLVMTargetDataRef; AddressSpace: Cardinal): TLLVMTypeRef; cdecl; external CLLVMLibrary;
function LLVMIntPtrTypeInContext(C: TLLVMContextRef; TD: TLLVMTargetDataRef): TLLVMTypeRef; cdecl; external CLLVMLibrary;
function LLVMIntPtrTypeForASInContext(C: TLLVMContextRef; TD: TLLVMTargetDataRef; AddressSpace: Cardinal): TLLVMTypeRef; cdecl; external CLLVMLibrary;
function LLVMSizeOfTypeInBits(TD: TLLVMTargetDataRef; Ty: TLLVMTypeRef): UInt64; cdecl; external CLLVMLibrary;
function LLVMStoreSizeOfType(TD: TLLVMTargetDataRef; Ty: TLLVMTypeRef): UInt64; cdecl; external CLLVMLibrary;
function LLVMABISizeOfType(TD: TLLVMTargetDataRef; Ty: TLLVMTypeRef): UInt64; cdecl; external CLLVMLibrary;
function LLVMABIAlignmentOfType(TD: TLLVMTargetDataRef; Ty: TLLVMTypeRef): Cardinal; cdecl; external CLLVMLibrary;
function LLVMCallFrameAlignmentOfType(TD: TLLVMTargetDataRef; Ty: TLLVMTypeRef): Cardinal; cdecl; external CLLVMLibrary;
function LLVMPreferredAlignmentOfType(TD: TLLVMTargetDataRef; Ty: TLLVMTypeRef): Cardinal; cdecl; external CLLVMLibrary;
function LLVMPreferredAlignmentOfGlobal(TD: TLLVMTargetDataRef; GlobalVar: TLLVMValueRef): Cardinal; cdecl; external CLLVMLibrary;
function LLVMElementAtOffset(TD: TLLVMTargetDataRef; StructTy: TLLVMTypeRef; Offset: UInt64): Cardinal; cdecl; external CLLVMLibrary;
function LLVMOffsetOfElement(TD: TLLVMTargetDataRef; StructTy: TLLVMTypeRef; Element: Cardinal): UInt64; cdecl; external CLLVMLibrary;
implementation
uses
Windows,
SysUtils;
const
CTargetInfoProc = 'LLVMInitialize%sTargetInfo';
CTargetProc = 'LLVMInitialize%sTarget';
CTargetMCProc = 'LLVMInitialize%sTargetMC';
CAsmPrinterProc = 'LLVMInitialize%sAsmPrinter';
CAsmParserProc = 'LLVMInitialize%sAsmParser';
CDisassemblerProc = 'LLVMInitialize%sDisassembler';
type
TInitProc = procedure; cdecl;
function TryDynCallProc(const AName: string): Boolean;
var
LLib: THandle;
LDynProc: TInitProc;
begin
if AName = '' then
Exit(False);
LLib := GetModuleHandle(CLLVMLibrary);
if LLib <> 0 then
begin
@LDynProc := GetProcAddress(LLib, PWideChar(AName));
if Assigned(LDynProc) then
begin
LDynProc();
Exit(True);
end;
end;
Result := False;
end;
function LLVMInitializeTargetInfo(const TargetName: string): Boolean;
begin
Result := TryDynCallProc(Format(CTargetInfoProc, [TargetName]));
end;
function LLVMInitializeTarget(const TargetName: string): Boolean;
begin
Result := TryDynCallProc(Format(CTargetProc, [TargetName]));
end;
function LLVMInitializeTargetMC(const TargetName: string): Boolean;
begin
Result := TryDynCallProc(Format(CTargetMCProc, [TargetName]));
end;
function LLVMInitializeAsmPrinter(const AsmPrinterName: string): Boolean;
begin
Result := TryDynCallProc(Format(CAsmPrinterProc, [AsmPrinterName]));
end;
function LLVMInitializeAsmParser(const AsmParserName: string): Boolean;
begin
Result := TryDynCallProc(Format(CAsmParserProc, [AsmParserName]));
end;
function LLVMInitializeDisassembler(const DisassemblerName: string): Boolean;
begin
Result := TryDynCallProc(Format(CDisassemblerProc, [DisassemblerName]));
end;
end.