-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcore.go
335 lines (292 loc) · 10.1 KB
/
core.go
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
package llvm
/**
LLVM 3.1 Go Bindings
(c) 2012 Chanwit Kaewkasi / SUT
Inspired by on the Go-LLVM package of nsf
**/
/*
#cgo CFLAGS: -D__STDC_LIMIT_MACROS -D__STDC_CONSTANT_MACROS
#cgo LDFLAGS: -lLLVMCore
#include <llvm-c/Core.h>
#include <stdlib.h>
*/
import "C"
import "unsafe"
type (
Context struct { C C.LLVMContextRef }
Module struct { C C.LLVMModuleRef }
Type struct { C C.LLVMTypeRef }
Value struct { C C.LLVMValueRef }
BasicBlock struct { C C.LLVMBasicBlockRef }
Builder struct { C C.LLVMBuilderRef }
ModuleProvider struct { C C.LLVMModuleProviderRef }
MemoryBuffer struct { C C.LLVMMemoryBufferRef }
PassManager struct { C C.LLVMPassManagerRef } // or PassManagerBase ?
PassRegistry struct { C C.LLVMPassRegistryRef }
Use struct { C C.LLVMUseRef }
)
func (this Context) IsNil() bool { return this.C == nil }
func (this Module) IsNil() bool { return this.C == nil }
func (this Type) IsNil() bool { return this.C == nil }
func (this Value) IsNil() bool { return this.C == nil }
func (this BasicBlock) IsNil() bool { return this.C == nil }
func (this Builder) IsNil() bool { return this.C == nil }
func (this ModuleProvider) IsNil() bool { return this.C == nil }
func (this MemoryBuffer) IsNil() bool { return this.C == nil }
func (this PassManager) IsNil() bool { return this.C == nil }
func (this PassRegistry) IsNil() bool { return this.C == nil }
func (this Use) IsNil() bool { return this.C == nil }
// LLVMAttribute
const (
NoneAttribute = 0
ZExtAttribute = C.LLVMZExtAttribute
SExtAttribute = C.LLVMSExtAttribute
NoReturnAttribute = C.LLVMNoReturnAttribute
InRegAttribute = C.LLVMInRegAttribute
StructRetAttribute = C.LLVMStructRetAttribute
NoUnwindAttribute = C.LLVMNoUnwindAttribute
NoAliasAttribute = C.LLVMNoAliasAttribute
ByValAttribute = C.LLVMByValAttribute
NestAttribute = C.LLVMNestAttribute
ReadNoneAttribute = C.LLVMReadNoneAttribute
ReadOnlyAttribute = C.LLVMReadOnlyAttribute
NoInlineAttribute = C.LLVMNoInlineAttribute
AlwaysInlineAttribute = C.LLVMAlwaysInlineAttribute
OptimizeForSizeAttribute = C.LLVMOptimizeForSizeAttribute
StackProtectAttribute = C.LLVMStackProtectAttribute
StackProtectReqAttribute = C.LLVMStackProtectReqAttribute
Alignment = C.LLVMAlignment
NoCaptureAttribute = C.LLVMNoCaptureAttribute
NoRedZoneAttribute = C.LLVMNoRedZoneAttribute
NoImplicitFloatAttribute = C.LLVMNoImplicitFloatAttribute
NakedAttribute = C.LLVMNakedAttribute
InlineHintAttribute = C.LLVMInlineHintAttribute
StackAlignment = C.LLVMStackAlignment
ReturnsTwice = C.LLVMReturnsTwice
UWTable = C.LLVMUWTable
NonLazyBind = C.LLVMNonLazyBind
)
// LLVMOpcode
const (
Ret = C.LLVMRet
Br = C.LLVMBr
Switch = C.LLVMSwitch
IndirectBr = C.LLVMIndirectBr
Invoke = C.LLVMInvoke
/* removed 6 due to API changes */
Unreachable = C.LLVMUnreachable
/* Standard Binary Operators */
Add = C.LLVMAdd
FAdd = C.LLVMFAdd
Sub = C.LLVMSub
FSub = C.LLVMFSub
Mul = C.LLVMMul
FMul = C.LLVMFMul
UDiv = C.LLVMUDiv
SDiv = C.LLVMSDiv
FDiv = C.LLVMFDiv
URem = C.LLVMURem
SRem = C.LLVMSRem
FRem = C.LLVMFRem
/* Logical Operators */
Shl = C.LLVMShl
LShr = C.LLVMLShr
AShr = C.LLVMAShr
And = C.LLVMAnd
Or = C.LLVMOr
Xor = C.LLVMXor
/* Memory Operators */
Alloca = C.LLVMAlloca
Load = C.LLVMLoad
Store = C.LLVMStore
GetElementPtr = C.LLVMGetElementPtr
/* Cast Operators */
Trunc = C.LLVMTrunc
ZExt = C.LLVMZExt
SExt = C.LLVMSExt
FPToUI = C.LLVMFPToUI
FPToSI = C.LLVMFPToSI
UIToFP = C.LLVMUIToFP
SIToFP = C.LLVMSIToFP
FPTrunc = C.LLVMFPTrunc
FPExt = C.LLVMFPExt
PtrToInt = C.LLVMPtrToInt
IntToPtr = C.LLVMIntToPtr
BitCast = C.LLVMBitCast
/* Other Operators */
ICmp = C.LLVMICmp
FCmp = C.LLVMFCmp
PHI = C.LLVMPHI
Call = C.LLVMCall
Select = C.LLVMSelect
UserOp1 = C.LLVMUserOp1
UserOp2 = C.LLVMUserOp2
VAArg = C.LLVMVAArg
ExtractElement = C.LLVMExtractElement
InsertElement = C.LLVMInsertElement
ShuffleVector = C.LLVMShuffleVector
ExtractValue = C.LLVMExtractValue
InsertValue = C.LLVMInsertValue
/* Atomic operators */
Fence = C.LLVMFence
AtomicCmpXchg = C.LLVMAtomicCmpXchg
AtomicRMW = C.LLVMAtomicRMW
/* Exception Handling Operators */
Resume = C.LLVMResume
LandingPad = C.LLVMLandingPad
)
// LLVMTypeKind
const (
VoidTypeKind = C.LLVMVoidTypeKind
HalfTypeKind = C.LLVMHalfTypeKind
FloatTypeKind = C.LLVMFloatTypeKind
DoubleTypeKind = C.LLVMDoubleTypeKind
X86_FP80TypeKind = C.LLVMX86_FP80TypeKind
FP128TypeKind = C.LLVMFP128TypeKind
PPC_FP128TypeKind = C.LLVMPPC_FP128TypeKind
LabelTypeKind = C.LLVMLabelTypeKind
IntegerTypeKind = C.LLVMIntegerTypeKind
FunctionTypeKind = C.LLVMFunctionTypeKind
StructTypeKind = C.LLVMStructTypeKind
ArrayTypeKind = C.LLVMArrayTypeKind
PointerTypeKind = C.LLVMPointerTypeKind
VectorTypeKind = C.LLVMVectorTypeKind
MetadataTypeKind = C.LLVMMetadataTypeKind
X86_MMXTypeKind = C.LLVMX86_MMXTypeKind
)
// LLVMLinkage
const (
ExternalLinkage = C.LLVMExternalLinkage
AvailableExternallyLinkage = C.LLVMAvailableExternallyLinkage
LinkOnceAnyLinkage = C.LLVMLinkOnceAnyLinkage
LinkOnceODRLinkage = C.LLVMLinkOnceODRLinkage
WeakAnyLinkage = C.LLVMWeakAnyLinkage
WeakODRLinkage = C.LLVMWeakODRLinkage
AppendingLinkage = C.LLVMAppendingLinkage
InternalLinkage = C.LLVMInternalLinkage
PrivateLinkage = C.LLVMPrivateLinkage
DLLImportLinkage = C.LLVMDLLImportLinkage
DLLExportLinkage = C.LLVMDLLExportLinkage
ExternalWeakLinkage = C.LLVMExternalWeakLinkage
GhostLinkage = C.LLVMGhostLinkage
CommonLinkage = C.LLVMCommonLinkage
LinkerPrivateLinkage = C.LLVMLinkerPrivateLinkage
LinkerPrivateWeakLinkage = C.LLVMLinkerPrivateWeakLinkage
LinkerPrivateWeakDefAutoLinkage = C.LLVMLinkerPrivateWeakDefAutoLinkage
)
// LLVMVisibility
const (
DefaultVisibility = C.LLVMDefaultVisibility
HiddenVisibility = C.LLVMHiddenVisibility
ProtectedVisibility = C.LLVMProtectedVisibility
)
// LLVMCallConv
const (
CCallConv = C.LLVMCCallConv
FastCallConv = C.LLVMFastCallConv
ColdCallConv = C.LLVMColdCallConv
X86StdcallCallConv = C.LLVMX86StdcallCallConv
X86FastcallCallConv = C.LLVMX86FastcallCallConv
)
// LLVMIntPredicate
const (
IntEQ = C.LLVMIntEQ
IntNE = C.LLVMIntNE
IntUGT = C.LLVMIntUGT
IntUGE = C.LLVMIntUGE
IntULT = C.LLVMIntULT
IntULE = C.LLVMIntULE
IntSGT = C.LLVMIntSGT
IntSGE = C.LLVMIntSGE
IntSLT = C.LLVMIntSLT
IntSLE = C.LLVMIntSLE
)
// LLVMRealPredicate
const (
RealPredicateFalse = C.LLVMRealPredicateFalse
RealOEQ = C.LLVMRealOEQ
RealOGT = C.LLVMRealOGT
RealOGE = C.LLVMRealOGE
RealOLT = C.LLVMRealOLT
RealOLE = C.LLVMRealOLE
RealONE = C.LLVMRealONE
RealORD = C.LLVMRealORD
RealUNO = C.LLVMRealUNO
RealUEQ = C.LLVMRealUEQ
RealUGT = C.LLVMRealUGT
RealUGE = C.LLVMRealUGE
RealULT = C.LLVMRealULT
RealULE = C.LLVMRealULE
RealUNE = C.LLVMRealUNE
RealPredicateTrue = C.LLVMRealPredicateTrue
)
// LLVMLandingPadClauseTy
const (
LLVMLandingPadCatch = C.LLVMLandingPadCatch
LLVMLandingPadFilter = C.LLVMLandingPadFilter
)
func InitializeCore(registry PassRegistry) {
C.LLVMInitializeCore(registry.C)
}
//
// Error Handling
//
func DisposeMessage(message string) {
cmessage := C.CString(message)
C.LLVMDisposeMessage(cmessage)
C.free(unsafe.Pointer(cmessage))
}
//
// llvm.Context
//
func NewContext() Context { return Context{C.LLVMContextCreate()} }
func GetGlobalContext() Context { return Context{C.LLVMGetGlobalContext()} }
func (c Context) Dispose() { C.LLVMContextDispose(c.C) }
func (c Context) GetMDKindIDInContext(name string) (id int) {
cname := C.CString(name)
id := C.LLVMGetMDKindIDInContext(c.C, cname, C.unsigned(len(name)))
C.free(unsafe.Pointer(cname))
return
}
func GetMDKindID(name string) (id int) {
cname := C.CString(name)
C.LLVMGetMDKindID(cname, C.unsigned(len(name)))
C.free(unsafe.Pointer(cname))
return
}
//
// llvm.Module
//
func NewModule(name string) (m Module) {
cname := C.CString(name)
m := Module{C.LLVMModuleCreateWithName(cname)}
C.free(unsafe.Pointer(cname))
return
}
func NewModuleInContext(name string, context Context) (m Module) {
cname := C.CString(name)
m := Module{C.LLVMModuleCreateWithNameInContext(cname, context.C)}
C.free(unsafe.Pointer(cname))
return
}
func (m Module) Dispose() {
C.LLVMDisposeModule(m.C)
}
func (m Module) GetDataLayout() string {
cdataLayout := C.LLVMGetDataLayout(m.C)
return C.GoString(cdataLayout)
}
func (m Module) SetDataLayout(triple string) {
ctriple := C.CString(triple)
C.LLVMSetDataLayout(m.C, ctriple)
C.free(unsafe.Pointer(ctriple))
}
func (m Module) GetTargetTriple() string {
ctargetTriple := C.LLVMGetTarget(m.C)
return C.GoString(ctargetTriple)
}
func (m Module) SetTargetTriple(triple string) {
ctriple := C.CString(triple)
C.LLVMSetTarget(m.C, ctriple)
C.free(unsafe.Pointer(ctriple))
}