1 //===- ir.go - Bindings for ir --------------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file defines bindings for the ir component.
10 //
11 //===----------------------------------------------------------------------===//
12
13 package llvm
14
15 /*
16 #include "llvm-c/Core.h"
17 #include "llvm-c/Comdat.h"
18 #include "IRBindings.h"
19 #include <stdlib.h>
20 */
21 import "C"
22 import (
23 "errors"
24 "unsafe"
25 )
26
27 type (
28 // We use these weird structs here because *Ref types are pointers and
29 // Go's spec says that a pointer cannot be used as a receiver base type.
30 Context struct {
31 C C.LLVMContextRef
32 }
33 Module struct {
34 C C.LLVMModuleRef
35 }
36 Type struct {
37 C C.LLVMTypeRef
38 }
39 Value struct {
40 C C.LLVMValueRef
41 }
42 Comdat struct {
43 C C.LLVMComdatRef
44 }
45 BasicBlock struct {
46 C C.LLVMBasicBlockRef
47 }
48 Builder struct {
49 C C.LLVMBuilderRef
50 }
51 ModuleProvider struct {
52 C C.LLVMModuleProviderRef
53 }
54 MemoryBuffer struct {
55 C C.LLVMMemoryBufferRef
56 }
57 PassManager struct {
58 C C.LLVMPassManagerRef
59 }
60 Use struct {
61 C C.LLVMUseRef
62 }
63 Metadata struct {
64 C C.LLVMMetadataRef
65 }
66 Attribute struct {
67 C C.LLVMAttributeRef
68 }
69 Opcode C.LLVMOpcode
70 AtomicRMWBinOp C.LLVMAtomicRMWBinOp
71 AtomicOrdering C.LLVMAtomicOrdering
72 TypeKind C.LLVMTypeKind
73 Linkage C.LLVMLinkage
74 Visibility C.LLVMVisibility
75 CallConv C.LLVMCallConv
76 ComdatSelectionKind C.LLVMComdatSelectionKind
77 IntPredicate C.LLVMIntPredicate
78 FloatPredicate C.LLVMRealPredicate
79 LandingPadClause C.LLVMLandingPadClauseTy
80 InlineAsmDialect C.LLVMInlineAsmDialect
81 )
82
83 func (c Context) IsNil() bool { return c.C == nil }
84 func (c Module) IsNil() bool { return c.C == nil }
85 func (c Type) IsNil() bool { return c.C == nil }
86 func (c Value) IsNil() bool { return c.C == nil }
87 func (c BasicBlock) IsNil() bool { return c.C == nil }
88 func (c Builder) IsNil() bool { return c.C == nil }
89 func (c ModuleProvider) IsNil() bool { return c.C == nil }
90 func (c MemoryBuffer) IsNil() bool { return c.C == nil }
91 func (c PassManager) IsNil() bool { return c.C == nil }
92 func (c Use) IsNil() bool { return c.C == nil }
93 func (c Attribute) IsNil() bool { return c.C == nil }
94 func (c Metadata) IsNil() bool { return c.C == nil }
95
96 // helpers
97 func llvmTypeRefPtr(t *Type) *C.LLVMTypeRef { return (*C.LLVMTypeRef)(unsafe.Pointer(t)) }
98 func llvmValueRefPtr(t *Value) *C.LLVMValueRef { return (*C.LLVMValueRef)(unsafe.Pointer(t)) }
99 func llvmMetadataRefPtr(t *Metadata) *C.LLVMMetadataRef {
100 return (*C.LLVMMetadataRef)(unsafe.Pointer(t))
101 }
102 func llvmBasicBlockRefPtr(t *BasicBlock) *C.LLVMBasicBlockRef {
103 return (*C.LLVMBasicBlockRef)(unsafe.Pointer(t))
104 }
105 func boolToLLVMBool(b bool) C.LLVMBool {
106 if b {
107 return C.LLVMBool(1)
108 }
109 return C.LLVMBool(0)
110 }
111
112 func llvmValueRefs(values []Value) (*C.LLVMValueRef, C.unsigned) {
113 var pt *C.LLVMValueRef
114 ptlen := C.unsigned(len(values))
115 if ptlen > 0 {
116 pt = llvmValueRefPtr(&values[0])
117 }
118 return pt, ptlen
119 }
120
121 func llvmMetadataRefs(mds []Metadata) (*C.LLVMMetadataRef, C.unsigned) {
122 var pt *C.LLVMMetadataRef
123 ptlen := C.unsigned(len(mds))
124 if ptlen > 0 {
125 pt = llvmMetadataRefPtr(&mds[0])
126 }
127 return pt, ptlen
128 }
129
130 //-------------------------------------------------------------------------
131 // llvm.Opcode
132 //-------------------------------------------------------------------------
133
134 const (
135 Ret Opcode = C.LLVMRet
136 Br Opcode = C.LLVMBr
137 Switch Opcode = C.LLVMSwitch
138 IndirectBr Opcode = C.LLVMIndirectBr
139 Invoke Opcode = C.LLVMInvoke
140 Unreachable Opcode = C.LLVMUnreachable
141
142 // Standard Binary Operators
143 Add Opcode = C.LLVMAdd
144 FAdd Opcode = C.LLVMFAdd
145 Sub Opcode = C.LLVMSub
146 FSub Opcode = C.LLVMFSub
147 Mul Opcode = C.LLVMMul
148 FMul Opcode = C.LLVMFMul
149 UDiv Opcode = C.LLVMUDiv
150 SDiv Opcode = C.LLVMSDiv
151 FDiv Opcode = C.LLVMFDiv
152 URem Opcode = C.LLVMURem
153 SRem Opcode = C.LLVMSRem
154 FRem Opcode = C.LLVMFRem
155
156 // Logical Operators
157 Shl Opcode = C.LLVMShl
158 LShr Opcode = C.LLVMLShr
159 AShr Opcode = C.LLVMAShr
160 And Opcode = C.LLVMAnd
161 Or Opcode = C.LLVMOr
162 Xor Opcode = C.LLVMXor
163
164 // Memory Operators
165 Alloca Opcode = C.LLVMAlloca
166 Load Opcode = C.LLVMLoad
167 Store Opcode = C.LLVMStore
168 GetElementPtr Opcode = C.LLVMGetElementPtr
169
170 // Cast Operators
171 Trunc Opcode = C.LLVMTrunc
172 ZExt Opcode = C.LLVMZExt
173 SExt Opcode = C.LLVMSExt
174 FPToUI Opcode = C.LLVMFPToUI
175 FPToSI Opcode = C.LLVMFPToSI
176 UIToFP Opcode = C.LLVMUIToFP
177 SIToFP Opcode = C.LLVMSIToFP
178 FPTrunc Opcode = C.LLVMFPTrunc
179 FPExt Opcode = C.LLVMFPExt
180 PtrToInt Opcode = C.LLVMPtrToInt
181 IntToPtr Opcode = C.LLVMIntToPtr
182 BitCast Opcode = C.LLVMBitCast
183
184 // Other Operators
185 ICmp Opcode = C.LLVMICmp
186 FCmp Opcode = C.LLVMFCmp
187 PHI Opcode = C.LLVMPHI
188 Call Opcode = C.LLVMCall
189 Select Opcode = C.LLVMSelect
190 // UserOp1
191 // UserOp2
192 VAArg Opcode = C.LLVMVAArg
193 ExtractElement Opcode = C.LLVMExtractElement
194 InsertElement Opcode = C.LLVMInsertElement
195 ShuffleVector Opcode = C.LLVMShuffleVector
196 ExtractValue Opcode = C.LLVMExtractValue
197 InsertValue Opcode = C.LLVMInsertValue
198
199 // Exception Handling Operators
200 Resume Opcode = C.LLVMResume
201 LandingPad Opcode = C.LLVMLandingPad
202 CleanupRet Opcode = C.LLVMCleanupRet
203 CatchRet Opcode = C.LLVMCatchRet
204 CatchPad Opcode = C.LLVMCatchPad
205 CleanupPad Opcode = C.LLVMCleanupPad
206 CatchSwitch Opcode = C.LLVMCatchSwitch
207 )
208
209 const (
210 AtomicRMWBinOpXchg AtomicRMWBinOp = C.LLVMAtomicRMWBinOpXchg
211 AtomicRMWBinOpAdd AtomicRMWBinOp = C.LLVMAtomicRMWBinOpAdd
212 AtomicRMWBinOpSub AtomicRMWBinOp = C.LLVMAtomicRMWBinOpSub
213 AtomicRMWBinOpAnd AtomicRMWBinOp = C.LLVMAtomicRMWBinOpAnd
214 AtomicRMWBinOpNand AtomicRMWBinOp = C.LLVMAtomicRMWBinOpNand
215 AtomicRMWBinOpOr AtomicRMWBinOp = C.LLVMAtomicRMWBinOpOr
216 AtomicRMWBinOpXor AtomicRMWBinOp = C.LLVMAtomicRMWBinOpXor
217 AtomicRMWBinOpMax AtomicRMWBinOp = C.LLVMAtomicRMWBinOpMax
218 AtomicRMWBinOpMin AtomicRMWBinOp = C.LLVMAtomicRMWBinOpMin
219 AtomicRMWBinOpUMax AtomicRMWBinOp = C.LLVMAtomicRMWBinOpUMax
220 AtomicRMWBinOpUMin AtomicRMWBinOp = C.LLVMAtomicRMWBinOpUMin
221 )
222
223 const (
224 AtomicOrderingNotAtomic AtomicOrdering = C.LLVMAtomicOrderingNotAtomic
225 AtomicOrderingUnordered AtomicOrdering = C.LLVMAtomicOrderingUnordered
226 AtomicOrderingMonotonic AtomicOrdering = C.LLVMAtomicOrderingMonotonic
227 AtomicOrderingAcquire AtomicOrdering = C.LLVMAtomicOrderingAcquire
228 AtomicOrderingRelease AtomicOrdering = C.LLVMAtomicOrderingRelease
229 AtomicOrderingAcquireRelease AtomicOrdering = C.LLVMAtomicOrderingAcquireRelease
230 AtomicOrderingSequentiallyConsistent AtomicOrdering = C.LLVMAtomicOrderingSequentiallyConsistent
231 )
232
233 //-------------------------------------------------------------------------
234 // llvm.TypeKind
235 //-------------------------------------------------------------------------
236
237 const (
238 VoidTypeKind TypeKind = C.LLVMVoidTypeKind
239 FloatTypeKind TypeKind = C.LLVMFloatTypeKind
240 DoubleTypeKind TypeKind = C.LLVMDoubleTypeKind
241 X86_FP80TypeKind TypeKind = C.LLVMX86_FP80TypeKind
242 FP128TypeKind TypeKind = C.LLVMFP128TypeKind
243 PPC_FP128TypeKind TypeKind = C.LLVMPPC_FP128TypeKind
244 LabelTypeKind TypeKind = C.LLVMLabelTypeKind
245 IntegerTypeKind TypeKind = C.LLVMIntegerTypeKind
246 FunctionTypeKind TypeKind = C.LLVMFunctionTypeKind
247 StructTypeKind TypeKind = C.LLVMStructTypeKind
248 ArrayTypeKind TypeKind = C.LLVMArrayTypeKind
249 PointerTypeKind TypeKind = C.LLVMPointerTypeKind
250 VectorTypeKind TypeKind = C.LLVMVectorTypeKind
251 MetadataTypeKind TypeKind = C.LLVMMetadataTypeKind
252 TokenTypeKind TypeKind = C.LLVMTokenTypeKind
253 )
254
255 //-------------------------------------------------------------------------
256 // llvm.Linkage
257 //-------------------------------------------------------------------------
258
259 const (
260 ExternalLinkage Linkage = C.LLVMExternalLinkage
261 AvailableExternallyLinkage Linkage = C.LLVMAvailableExternallyLinkage
262 LinkOnceAnyLinkage Linkage = C.LLVMLinkOnceAnyLinkage
263 LinkOnceODRLinkage Linkage = C.LLVMLinkOnceODRLinkage
264 WeakAnyLinkage Linkage = C.LLVMWeakAnyLinkage
265 WeakODRLinkage Linkage = C.LLVMWeakODRLinkage
266 AppendingLinkage Linkage = C.LLVMAppendingLinkage
267 InternalLinkage Linkage = C.LLVMInternalLinkage
268 PrivateLinkage Linkage = C.LLVMPrivateLinkage
269 ExternalWeakLinkage Linkage = C.LLVMExternalWeakLinkage
270 CommonLinkage Linkage = C.LLVMCommonLinkage
271 )
272
273 //-------------------------------------------------------------------------
274 // llvm.Visibility
275 //-------------------------------------------------------------------------
276
277 const (
278 DefaultVisibility Visibility = C.LLVMDefaultVisibility
279 HiddenVisibility Visibility = C.LLVMHiddenVisibility
280 ProtectedVisibility Visibility = C.LLVMProtectedVisibility
281 )
282
283 //-------------------------------------------------------------------------
284 // llvm.CallConv
285 //-------------------------------------------------------------------------
286
287 const (
288 CCallConv CallConv = C.LLVMCCallConv
289 FastCallConv CallConv = C.LLVMFastCallConv
290 ColdCallConv CallConv = C.LLVMColdCallConv
291 X86StdcallCallConv CallConv = C.LLVMX86StdcallCallConv
292 X86FastcallCallConv CallConv = C.LLVMX86FastcallCallConv
293 )
294
295 //-------------------------------------------------------------------------
296 // llvm.ComdatSelectionKind
297 //-------------------------------------------------------------------------
298
299 const (
300 AnyComdatSelectionKind ComdatSelectionKind = C.LLVMAnyComdatSelectionKind
301 ExactMatchComdatSelectionKind ComdatSelectionKind = C.LLVMExactMatchComdatSelectionKind
302 LargestComdatSelectionKind ComdatSelectionKind = C.LLVMLargestComdatSelectionKind
303 NoDeduplicateComdatSelectionKind ComdatSelectionKind = C.LLVMNoDeduplicateComdatSelectionKind
304 SameSizeComdatSelectionKind ComdatSelectionKind = C.LLVMSameSizeComdatSelectionKind
305 )
306
307 //-------------------------------------------------------------------------
308 // llvm.IntPredicate
309 //-------------------------------------------------------------------------
310
311 const (
312 IntEQ IntPredicate = C.LLVMIntEQ
313 IntNE IntPredicate = C.LLVMIntNE
314 IntUGT IntPredicate = C.LLVMIntUGT
315 IntUGE IntPredicate = C.LLVMIntUGE
316 IntULT IntPredicate = C.LLVMIntULT
317 IntULE IntPredicate = C.LLVMIntULE
318 IntSGT IntPredicate = C.LLVMIntSGT
319 IntSGE IntPredicate = C.LLVMIntSGE
320 IntSLT IntPredicate = C.LLVMIntSLT
321 IntSLE IntPredicate = C.LLVMIntSLE
322 )
323
324 //-------------------------------------------------------------------------
325 // llvm.FloatPredicate
326 //-------------------------------------------------------------------------
327
328 const (
329 FloatPredicateFalse FloatPredicate = C.LLVMRealPredicateFalse
330 FloatOEQ FloatPredicate = C.LLVMRealOEQ
331 FloatOGT FloatPredicate = C.LLVMRealOGT
332 FloatOGE FloatPredicate = C.LLVMRealOGE
333 FloatOLT FloatPredicate = C.LLVMRealOLT
334 FloatOLE FloatPredicate = C.LLVMRealOLE
335 FloatONE FloatPredicate = C.LLVMRealONE
336 FloatORD FloatPredicate = C.LLVMRealORD
337 FloatUNO FloatPredicate = C.LLVMRealUNO
338 FloatUEQ FloatPredicate = C.LLVMRealUEQ
339 FloatUGT FloatPredicate = C.LLVMRealUGT
340 FloatUGE FloatPredicate = C.LLVMRealUGE
341 FloatULT FloatPredicate = C.LLVMRealULT
342 FloatULE FloatPredicate = C.LLVMRealULE
343 FloatUNE FloatPredicate = C.LLVMRealUNE
344 FloatPredicateTrue FloatPredicate = C.LLVMRealPredicateTrue
345 )
346
347 //-------------------------------------------------------------------------
348 // llvm.LandingPadClause
349 //-------------------------------------------------------------------------
350
351 const (
352 LandingPadCatch LandingPadClause = C.LLVMLandingPadCatch
353 LandingPadFilter LandingPadClause = C.LLVMLandingPadFilter
354 )
355
356 //-------------------------------------------------------------------------
357 // llvm.InlineAsmDialect
358 //-------------------------------------------------------------------------
359
360 const (
361 InlineAsmDialectATT InlineAsmDialect = C.LLVMInlineAsmDialectATT
362 InlineAsmDialectIntel InlineAsmDialect = C.LLVMInlineAsmDialectIntel
363 )
364
365 //-------------------------------------------------------------------------
366 // llvm.Context
367 //-------------------------------------------------------------------------
368
369 func NewContext() Context { return Context{C.LLVMContextCreate()} }
370 func GlobalContext() Context { return Context{C.LLVMGetGlobalContext()} }
371 func (c Context) Dispose() { C.LLVMContextDispose(c.C) }
372
373 func (c Context) MDKindID(name string) (id int) {
374 cname := C.CString(name)
375 defer C.free(unsafe.Pointer(cname))
376 id = int(C.LLVMGetMDKindIDInContext(c.C, cname, C.unsigned(len(name))))
377 return
378 }
379
380 func MDKindID(name string) (id int) {
381 cname := C.CString(name)
382 defer C.free(unsafe.Pointer(cname))
383 id = int(C.LLVMGetMDKindID(cname, C.unsigned(len(name))))
384 return
385 }
386
387 //-------------------------------------------------------------------------
388 // llvm.Attribute
389 //-------------------------------------------------------------------------
390
391 func AttributeKindID(name string) (id uint) {
392 cname := C.CString(name)
393 defer C.free(unsafe.Pointer(cname))
394 id = uint(C.LLVMGetEnumAttributeKindForName(cname, C.size_t(len(name))))
395 return
396 }
397
398 func (c Context) CreateEnumAttribute(kind uint, val uint64) (a Attribute) {
399 a.C = C.LLVMCreateEnumAttribute(c.C, C.unsigned(kind), C.uint64_t(val))
400 return
401 }
402
403 func (c Context) CreateTypeAttribute(kind uint, t Type) (a Attribute) {
404 a.C = C.LLVMCreateTypeAttribute(c.C, C.unsigned(kind), t.C)
405 return
406 }
407
408 func (a Attribute) GetTypeValue() (t Type) {
409 t.C = C.LLVMGetTypeAttributeValue(a.C)
410 return
411 }
412
413 func (a Attribute) GetEnumKind() (id int) {
414 id = int(C.LLVMGetEnumAttributeKind(a.C))
415 return
416 }
417
418 func (a Attribute) GetEnumValue() (val uint64) {
419 val = uint64(C.LLVMGetEnumAttributeValue(a.C))
420 return
421 }
422
423 func (c Context) CreateStringAttribute(kind string, val string) (a Attribute) {
424 ckind := C.CString(kind)
425 defer C.free(unsafe.Pointer(ckind))
426 cval := C.CString(val)
427 defer C.free(unsafe.Pointer(cval))
428 a.C = C.LLVMCreateStringAttribute(c.C,
429 ckind, C.unsigned(len(kind)),
430 cval, C.unsigned(len(val)))
431 return
432 }
433
434 func (a Attribute) GetStringKind() string {
435 length := C.unsigned(0)
436 ckind := C.LLVMGetStringAttributeKind(a.C, &length)
437 return C.GoStringN(ckind, C.int(length))
438 }
439
440 func (a Attribute) GetStringValue() string {
441 length := C.unsigned(0)
442 ckind := C.LLVMGetStringAttributeValue(a.C, &length)
443 return C.GoStringN(ckind, C.int(length))
444 }
445
446 func (a Attribute) IsEnum() bool {
447 return C.LLVMIsEnumAttribute(a.C) != 0
448 }
449
450 func (a Attribute) IsString() bool {
451 return C.LLVMIsStringAttribute(a.C) != 0
452 }
453
454 //-------------------------------------------------------------------------
455 // llvm.Module
456 //-------------------------------------------------------------------------
457
458 // Create and destroy modules.
459 // See llvm::Module::Module.
460 func (c Context) NewModule(name string) (m Module) {
461 cname := C.CString(name)
462 defer C.free(unsafe.Pointer(cname))
463 m.C = C.LLVMModuleCreateWithNameInContext(cname, c.C)
464 return
465 }
466
467 // See llvm::Module::~Module
468 func (m Module) Dispose() { C.LLVMDisposeModule(m.C) }
469
470 // Data layout. See Module::getDataLayout.
471 func (m Module) DataLayout() string {
472 clayout := C.LLVMGetDataLayout(m.C)
473 return C.GoString(clayout)
474 }
475
476 func (m Module) SetDataLayout(layout string) {
477 clayout := C.CString(layout)
478 defer C.free(unsafe.Pointer(clayout))
479 C.LLVMSetDataLayout(m.C, clayout)
480 }
481
482 // Target triple. See Module::getTargetTriple.
483 func (m Module) Target() string {
484 ctarget := C.LLVMGetTarget(m.C)
485 return C.GoString(ctarget)
486 }
487 func (m Module) SetTarget(target string) {
488 ctarget := C.CString(target)
489 defer C.free(unsafe.Pointer(ctarget))
490 C.LLVMSetTarget(m.C, ctarget)
491 }
492
493 func (m Module) GetTypeByName(name string) (t Type) {
494 cname := C.CString(name)
495 defer C.free(unsafe.Pointer(cname))
496 t.C = C.LLVMGetTypeByName(m.C, cname)
497 return
498 }
499
500 // See Module::dump.
501 func (m Module) Dump() {
502 C.LLVMDumpModule(m.C)
503 }
504
505 func (m Module) String() string {
506 cir := C.LLVMPrintModuleToString(m.C)
507 defer C.LLVMDisposeMessage(cir)
508 ir := C.GoString(cir)
509 return ir
510 }
511
512 // See Module::setModuleInlineAsm.
513 func (m Module) SetInlineAsm(asm string) {
514 casm := C.CString(asm)
515 defer C.free(unsafe.Pointer(casm))
516 C.LLVMSetModuleInlineAsm(m.C, casm)
517 }
518
519 func (m Module) AddNamedMetadataOperand(name string, operand Metadata) {
520 cname := C.CString(name)
521 defer C.free(unsafe.Pointer(cname))
522 C.LLVMAddNamedMetadataOperand2(m.C, cname, operand.C)
523 }
524
525 func (m Module) Context() (c Context) {
526 c.C = C.LLVMGetModuleContext(m.C)
527 return
528 }
529
530 //-------------------------------------------------------------------------
531 // llvm.Type
532 //-------------------------------------------------------------------------
533
534 // LLVM types conform to the following hierarchy:
535 //
536 // types:
537 // integer type
538 // real type
539 // function type
540 // sequence types:
541 // array type
542 // pointer type
543 // vector type
544 // void type
545 // label type
546 // opaque type
547
548 // See llvm::LLVMTypeKind::getTypeID.
549 func (t Type) TypeKind() TypeKind { return TypeKind(C.LLVMGetTypeKind(t.C)) }
550
551 // See llvm::LLVMType::getContext.
552 func (t Type) Context() (c Context) {
553 c.C = C.LLVMGetTypeContext(t.C)
554 return
555 }
556
557 // Operations on integer types
558 func (c Context) Int1Type() (t Type) { t.C = C.LLVMInt1TypeInContext(c.C); return }
559 func (c Context) Int8Type() (t Type) { t.C = C.LLVMInt8TypeInContext(c.C); return }
560 func (c Context) Int16Type() (t Type) { t.C = C.LLVMInt16TypeInContext(c.C); return }
561 func (c Context) Int32Type() (t Type) { t.C = C.LLVMInt32TypeInContext(c.C); return }
562 func (c Context) Int64Type() (t Type) { t.C = C.LLVMInt64TypeInContext(c.C); return }
563 func (c Context) IntType(numbits int) (t Type) {
564 t.C = C.LLVMIntTypeInContext(c.C, C.unsigned(numbits))
565 return
566 }
567
568 func (t Type) IntTypeWidth() int {
569 return int(C.LLVMGetIntTypeWidth(t.C))
570 }
571
572 // Operations on real types
573 func (c Context) FloatType() (t Type) { t.C = C.LLVMFloatTypeInContext(c.C); return }
574 func (c Context) DoubleType() (t Type) { t.C = C.LLVMDoubleTypeInContext(c.C); return }
575 func (c Context) X86FP80Type() (t Type) { t.C = C.LLVMX86FP80TypeInContext(c.C); return }
576 func (c Context) FP128Type() (t Type) { t.C = C.LLVMFP128TypeInContext(c.C); return }
577 func (c Context) PPCFP128Type() (t Type) { t.C = C.LLVMPPCFP128TypeInContext(c.C); return }
578
579 // Operations on function types
580 func FunctionType(returnType Type, paramTypes []Type, isVarArg bool) (t Type) {
581 var pt *C.LLVMTypeRef
582 var ptlen C.unsigned
583 if len(paramTypes) > 0 {
584 pt = llvmTypeRefPtr(¶mTypes[0])
585 ptlen = C.unsigned(len(paramTypes))
586 }
587 t.C = C.LLVMFunctionType(returnType.C,
588 pt,
589 ptlen,
590 boolToLLVMBool(isVarArg))
591 return
592 }
593
594 func (t Type) IsFunctionVarArg() bool { return C.LLVMIsFunctionVarArg(t.C) != 0 }
595 func (t Type) ReturnType() (rt Type) { rt.C = C.LLVMGetReturnType(t.C); return }
596 func (t Type) ParamTypesCount() int { return int(C.LLVMCountParamTypes(t.C)) }
597 func (t Type) ParamTypes() []Type {
598 count := t.ParamTypesCount()
599 if count > 0 {
600 out := make([]Type, count)
601 C.LLVMGetParamTypes(t.C, llvmTypeRefPtr(&out[0]))
602 return out
603 }
604 return nil
605 }
606
607 // Operations on struct types
608 func (c Context) StructType(elementTypes []Type, packed bool) (t Type) {
609 var pt *C.LLVMTypeRef
610 var ptlen C.unsigned
611 if len(elementTypes) > 0 {
612 pt = llvmTypeRefPtr(&elementTypes[0])
613 ptlen = C.unsigned(len(elementTypes))
614 }
615 t.C = C.LLVMStructTypeInContext(c.C,
616 pt,
617 ptlen,
618 boolToLLVMBool(packed))
619 return
620 }
621
622 func StructType(elementTypes []Type, packed bool) (t Type) {
623 var pt *C.LLVMTypeRef
624 var ptlen C.unsigned
625 if len(elementTypes) > 0 {
626 pt = llvmTypeRefPtr(&elementTypes[0])
627 ptlen = C.unsigned(len(elementTypes))
628 }
629 t.C = C.LLVMStructType(pt, ptlen, boolToLLVMBool(packed))
630 return
631 }
632
633 func (c Context) StructCreateNamed(name string) (t Type) {
634 cname := C.CString(name)
635 defer C.free(unsafe.Pointer(cname))
636 t.C = C.LLVMStructCreateNamed(c.C, cname)
637 return
638 }
639
640 func (t Type) StructName() string {
641 return C.GoString(C.LLVMGetStructName(t.C))
642 }
643
644 func (t Type) StructSetBody(elementTypes []Type, packed bool) {
645 var pt *C.LLVMTypeRef
646 var ptlen C.unsigned
647 if len(elementTypes) > 0 {
648 pt = llvmTypeRefPtr(&elementTypes[0])
649 ptlen = C.unsigned(len(elementTypes))
650 }
651 C.LLVMStructSetBody(t.C, pt, ptlen, boolToLLVMBool(packed))
652 }
653
654 func (t Type) IsStructPacked() bool { return C.LLVMIsPackedStruct(t.C) != 0 }
655 func (t Type) StructElementTypesCount() int { return int(C.LLVMCountStructElementTypes(t.C)) }
656 func (t Type) StructElementTypes() []Type {
657 out := make([]Type, t.StructElementTypesCount())
658 if len(out) > 0 {
659 C.LLVMGetStructElementTypes(t.C, llvmTypeRefPtr(&out[0]))
660 }
661 return out
662 }
663
664 // Operations on array, pointer, and vector types (sequence types)
665 func (t Type) Subtypes() (ret []Type) {
666 ret = make([]Type, C.LLVMGetNumContainedTypes(t.C))
667 C.LLVMGetSubtypes(t.C, llvmTypeRefPtr(&ret[0]))
668 return
669 }
670
671 func ArrayType(elementType Type, elementCount int) (t Type) {
672 t.C = C.LLVMArrayType(elementType.C, C.unsigned(elementCount))
673 return
674 }
675 func PointerType(elementType Type, addressSpace int) (t Type) {
676 t.C = C.LLVMPointerType(elementType.C, C.unsigned(addressSpace))
677 return
678 }
679 func VectorType(elementType Type, elementCount int) (t Type) {
680 t.C = C.LLVMVectorType(elementType.C, C.unsigned(elementCount))
681 return
682 }
683
684 func (t Type) ElementType() (rt Type) { rt.C = C.LLVMGetElementType(t.C); return }
685 func (t Type) ArrayLength() int { return int(C.LLVMGetArrayLength(t.C)) }
686 func (t Type) PointerAddressSpace() int { return int(C.LLVMGetPointerAddressSpace(t.C)) }
687 func (t Type) VectorSize() int { return int(C.LLVMGetVectorSize(t.C)) }
688
689 // Operations on other types
690 func (c Context) VoidType() (t Type) { t.C = C.LLVMVoidTypeInContext(c.C); return }
691 func (c Context) LabelType() (t Type) { t.C = C.LLVMLabelTypeInContext(c.C); return }
692 func (c Context) TokenType() (t Type) { t.C = C.LLVMTokenTypeInContext(c.C); return }
693
694 //-------------------------------------------------------------------------
695 // llvm.Value
696 //-------------------------------------------------------------------------
697
698 // Operations on all values
699 func (v Value) Type() (t Type) { t.C = C.LLVMTypeOf(v.C); return }
700 func (v Value) Name() string { return C.GoString(C.LLVMGetValueName(v.C)) }
701 func (v Value) SetName(name string) {
702 cname := C.CString(name)
703 defer C.free(unsafe.Pointer(cname))
704 C.LLVMSetValueName(v.C, cname)
705 }
706 func (v Value) Dump() { C.LLVMDumpValue(v.C) }
707 func (v Value) ReplaceAllUsesWith(nv Value) { C.LLVMReplaceAllUsesWith(v.C, nv.C) }
708 func (v Value) HasMetadata() bool { return C.LLVMHasMetadata(v.C) != 0 }
709 func (v Value) Metadata(kind int) (rv Value) {
710 rv.C = C.LLVMGetMetadata(v.C, C.unsigned(kind))
711 return
712 }
713 func (v Value) SetMetadata(kind int, node Metadata) {
714 C.LLVMSetMetadata2(v.C, C.unsigned(kind), node.C)
715 }
716
717 // Obtain the string value of the instruction. Same as would be printed with
718 // Value.Dump() (with two spaces at the start but no newline at the end).
719 func (v Value) String() string {
720 cstr := C.LLVMPrintValueToString(v.C)
721 defer C.LLVMDisposeMessage(cstr)
722 return C.GoString(cstr)
723 }
724
725 // Conversion functions.
726 // Return the input value if it is an instance of the specified class, otherwise NULL.
727 // See llvm::dyn_cast_or_null<>.
728 func (v Value) IsAArgument() (rv Value) { rv.C = C.LLVMIsAArgument(v.C); return }
729 func (v Value) IsABasicBlock() (rv Value) { rv.C = C.LLVMIsABasicBlock(v.C); return }
730 func (v Value) IsAInlineAsm() (rv Value) { rv.C = C.LLVMIsAInlineAsm(v.C); return }
731 func (v Value) IsAUser() (rv Value) { rv.C = C.LLVMIsAUser(v.C); return }
732 func (v Value) IsAConstant() (rv Value) { rv.C = C.LLVMIsAConstant(v.C); return }
733 func (v Value) IsAConstantAggregateZero() (rv Value) {
734 rv.C = C.LLVMIsAConstantAggregateZero(v.C)
735 return
736 }
737 func (v Value) IsAConstantArray() (rv Value) { rv.C = C.LLVMIsAConstantArray(v.C); return }
738 func (v Value) IsAConstantExpr() (rv Value) { rv.C = C.LLVMIsAConstantExpr(v.C); return }
739 func (v Value) IsAConstantFP() (rv Value) { rv.C = C.LLVMIsAConstantFP(v.C); return }
740 func (v Value) IsAConstantInt() (rv Value) { rv.C = C.LLVMIsAConstantInt(v.C); return }
741 func (v Value) IsAConstantPointerNull() (rv Value) { rv.C = C.LLVMIsAConstantPointerNull(v.C); return }
742 func (v Value) IsAConstantStruct() (rv Value) { rv.C = C.LLVMIsAConstantStruct(v.C); return }
743 func (v Value) IsAConstantVector() (rv Value) { rv.C = C.LLVMIsAConstantVector(v.C); return }
744 func (v Value) IsAGlobalValue() (rv Value) { rv.C = C.LLVMIsAGlobalValue(v.C); return }
745 func (v Value) IsAFunction() (rv Value) { rv.C = C.LLVMIsAFunction(v.C); return }
746 func (v Value) IsAGlobalAlias() (rv Value) { rv.C = C.LLVMIsAGlobalAlias(v.C); return }
747 func (v Value) IsAGlobalVariable() (rv Value) { rv.C = C.LLVMIsAGlobalVariable(v.C); return }
748 func (v Value) IsAUndefValue() (rv Value) { rv.C = C.LLVMIsAUndefValue(v.C); return }
749 func (v Value) IsAInstruction() (rv Value) { rv.C = C.LLVMIsAInstruction(v.C); return }
750 func (v Value) IsABinaryOperator() (rv Value) { rv.C = C.LLVMIsABinaryOperator(v.C); return }
751 func (v Value) IsACallInst() (rv Value) { rv.C = C.LLVMIsACallInst(v.C); return }
752 func (v Value) IsAIntrinsicInst() (rv Value) { rv.C = C.LLVMIsAIntrinsicInst(v.C); return }
753 func (v Value) IsADbgInfoIntrinsic() (rv Value) { rv.C = C.LLVMIsADbgInfoIntrinsic(v.C); return }
754 func (v Value) IsADbgDeclareInst() (rv Value) { rv.C = C.LLVMIsADbgDeclareInst(v.C); return }
755 func (v Value) IsAMemIntrinsic() (rv Value) { rv.C = C.LLVMIsAMemIntrinsic(v.C); return }
756 func (v Value) IsAMemCpyInst() (rv Value) { rv.C = C.LLVMIsAMemCpyInst(v.C); return }
757 func (v Value) IsAMemMoveInst() (rv Value) { rv.C = C.LLVMIsAMemMoveInst(v.C); return }
758 func (v Value) IsAMemSetInst() (rv Value) { rv.C = C.LLVMIsAMemSetInst(v.C); return }
759 func (v Value) IsACmpInst() (rv Value) { rv.C = C.LLVMIsACmpInst(v.C); return }
760 func (v Value) IsAFCmpInst() (rv Value) { rv.C = C.LLVMIsAFCmpInst(v.C); return }
761 func (v Value) IsAICmpInst() (rv Value) { rv.C = C.LLVMIsAICmpInst(v.C); return }
762 func (v Value) IsAExtractElementInst() (rv Value) { rv.C = C.LLVMIsAExtractElementInst(v.C); return }
763 func (v Value) IsAGetElementPtrInst() (rv Value) { rv.C = C.LLVMIsAGetElementPtrInst(v.C); return }
764 func (v Value) IsAInsertElementInst() (rv Value) { rv.C = C.LLVMIsAInsertElementInst(v.C); return }
765 func (v Value) IsAInsertValueInst() (rv Value) { rv.C = C.LLVMIsAInsertValueInst(v.C); return }
766 func (v Value) IsAPHINode() (rv Value) { rv.C = C.LLVMIsAPHINode(v.C); return }
767 func (v Value) IsASelectInst() (rv Value) { rv.C = C.LLVMIsASelectInst(v.C); return }
768 func (v Value) IsAShuffleVectorInst() (rv Value) { rv.C = C.LLVMIsAShuffleVectorInst(v.C); return }
769 func (v Value) IsAStoreInst() (rv Value) { rv.C = C.LLVMIsAStoreInst(v.C); return }
770 func (v Value) IsABranchInst() (rv Value) { rv.C = C.LLVMIsABranchInst(v.C); return }
771 func (v Value) IsAInvokeInst() (rv Value) { rv.C = C.LLVMIsAInvokeInst(v.C); return }
772 func (v Value) IsAReturnInst() (rv Value) { rv.C = C.LLVMIsAReturnInst(v.C); return }
773 func (v Value) IsASwitchInst() (rv Value) { rv.C = C.LLVMIsASwitchInst(v.C); return }
774 func (v Value) IsAUnreachableInst() (rv Value) { rv.C = C.LLVMIsAUnreachableInst(v.C); return }
775 func (v Value) IsAUnaryInstruction() (rv Value) { rv.C = C.LLVMIsAUnaryInstruction(v.C); return }
776 func (v Value) IsAAllocaInst() (rv Value) { rv.C = C.LLVMIsAAllocaInst(v.C); return }
777 func (v Value) IsACastInst() (rv Value) { rv.C = C.LLVMIsACastInst(v.C); return }
778 func (v Value) IsABitCastInst() (rv Value) { rv.C = C.LLVMIsABitCastInst(v.C); return }
779 func (v Value) IsAFPExtInst() (rv Value) { rv.C = C.LLVMIsAFPExtInst(v.C); return }
780 func (v Value) IsAFPToSIInst() (rv Value) { rv.C = C.LLVMIsAFPToSIInst(v.C); return }
781 func (v Value) IsAFPToUIInst() (rv Value) { rv.C = C.LLVMIsAFPToUIInst(v.C); return }
782 func (v Value) IsAFPTruncInst() (rv Value) { rv.C = C.LLVMIsAFPTruncInst(v.C); return }
783 func (v Value) IsAIntToPtrInst() (rv Value) { rv.C = C.LLVMIsAIntToPtrInst(v.C); return }
784 func (v Value) IsAPtrToIntInst() (rv Value) { rv.C = C.LLVMIsAPtrToIntInst(v.C); return }
785 func (v Value) IsASExtInst() (rv Value) { rv.C = C.LLVMIsASExtInst(v.C); return }
786 func (v Value) IsASIToFPInst() (rv Value) { rv.C = C.LLVMIsASIToFPInst(v.C); return }
787 func (v Value) IsATruncInst() (rv Value) { rv.C = C.LLVMIsATruncInst(v.C); return }
788 func (v Value) IsAUIToFPInst() (rv Value) { rv.C = C.LLVMIsAUIToFPInst(v.C); return }
789 func (v Value) IsAZExtInst() (rv Value) { rv.C = C.LLVMIsAZExtInst(v.C); return }
790 func (v Value) IsAExtractValueInst() (rv Value) { rv.C = C.LLVMIsAExtractValueInst(v.C); return }
791 func (v Value) IsALoadInst() (rv Value) { rv.C = C.LLVMIsALoadInst(v.C); return }
792 func (v Value) IsAVAArgInst() (rv Value) { rv.C = C.LLVMIsAVAArgInst(v.C); return }
793
794 // Operations on Uses
795 func (v Value) FirstUse() (u Use) { u.C = C.LLVMGetFirstUse(v.C); return }
796 func (u Use) NextUse() (ru Use) { ru.C = C.LLVMGetNextUse(u.C); return }
797 func (u Use) User() (v Value) { v.C = C.LLVMGetUser(u.C); return }
798 func (u Use) UsedValue() (v Value) { v.C = C.LLVMGetUsedValue(u.C); return }
799
800 // Operations on Users
801 func (v Value) Operand(i int) (rv Value) { rv.C = C.LLVMGetOperand(v.C, C.unsigned(i)); return }
802 func (v Value) SetOperand(i int, op Value) { C.LLVMSetOperand(v.C, C.unsigned(i), op.C) }
803 func (v Value) OperandsCount() int { return int(C.LLVMGetNumOperands(v.C)) }
804
805 // Operations on constants of any type
806 func ConstNull(t Type) (v Value) { v.C = C.LLVMConstNull(t.C); return }
807 func ConstAllOnes(t Type) (v Value) { v.C = C.LLVMConstAllOnes(t.C); return }
808 func Undef(t Type) (v Value) { v.C = C.LLVMGetUndef(t.C); return }
809 func (v Value) IsConstant() bool { return C.LLVMIsConstant(v.C) != 0 }
810 func (v Value) IsNull() bool { return C.LLVMIsNull(v.C) != 0 }
811 func (v Value) IsUndef() bool { return C.LLVMIsUndef(v.C) != 0 }
812 func ConstPointerNull(t Type) (v Value) { v.C = C.LLVMConstPointerNull(t.C); return }
813
814 // Operations on metadata
815 func (c Context) MDString(str string) (md Metadata) {
816 cstr := C.CString(str)
817 defer C.free(unsafe.Pointer(cstr))
818 md.C = C.LLVMMDString2(c.C, cstr, C.unsigned(len(str)))
819 return
820 }
821 func (c Context) MDNode(mds []Metadata) (md Metadata) {
822 ptr, nvals := llvmMetadataRefs(mds)
823 md.C = C.LLVMMDNode2(c.C, ptr, nvals)
824 return
825 }
826 func (v Value) ConstantAsMetadata() (md Metadata) {
827 md.C = C.LLVMConstantAsMetadata(v.C)
828 return
829 }
830
831 // Operations on scalar constants
832 func ConstInt(t Type, n uint64, signExtend bool) (v Value) {
833 v.C = C.LLVMConstInt(t.C,
834 C.ulonglong(n),
835 boolToLLVMBool(signExtend))
836 return
837 }
838 func ConstIntFromString(t Type, str string, radix int) (v Value) {
839 cstr := C.CString(str)
840 defer C.free(unsafe.Pointer(cstr))
841 v.C = C.LLVMConstIntOfString(t.C, cstr, C.uint8_t(radix))
842 return
843 }
844 func ConstFloat(t Type, n float64) (v Value) {
845 v.C = C.LLVMConstReal(t.C, C.double(n))
846 return
847 }
848 func ConstFloatFromString(t Type, str string) (v Value) {
849 cstr := C.CString(str)
850 defer C.free(unsafe.Pointer(cstr))
851 v.C = C.LLVMConstRealOfString(t.C, cstr)
852 return
853 }
854
855 func (v Value) ZExtValue() uint64 { return uint64(C.LLVMConstIntGetZExtValue(v.C)) }
856 func (v Value) SExtValue() int64 { return int64(C.LLVMConstIntGetSExtValue(v.C)) }
857 func (v Value) DoubleValue() (result float64, inexact bool) {
858 var losesInfo C.LLVMBool
859 doubleResult := C.LLVMConstRealGetDouble(v.C, &losesInfo)
860 return float64(doubleResult), losesInfo != 0
861 }
862
863 // Operations on composite constants
864 func (c Context) ConstString(str string, addnull bool) (v Value) {
865 cstr := C.CString(str)
866 defer C.free(unsafe.Pointer(cstr))
867 v.C = C.LLVMConstStringInContext(c.C, cstr,
868 C.unsigned(len(str)), boolToLLVMBool(!addnull))
869 return
870 }
871 func (c Context) ConstStruct(constVals []Value, packed bool) (v Value) {
872 ptr, nvals := llvmValueRefs(constVals)
873 v.C = C.LLVMConstStructInContext(c.C, ptr, nvals,
874 boolToLLVMBool(packed))
875 return
876 }
877 func ConstNamedStruct(t Type, constVals []Value) (v Value) {
878 ptr, nvals := llvmValueRefs(constVals)
879 v.C = C.LLVMConstNamedStruct(t.C, ptr, nvals)
880 return
881 }
882 func ConstString(str string, addnull bool) (v Value) {
883 cstr := C.CString(str)
884 defer C.free(unsafe.Pointer(cstr))
885 v.C = C.LLVMConstString(cstr,
886 C.unsigned(len(str)), boolToLLVMBool(!addnull))
887 return
888 }
889 func ConstArray(t Type, constVals []Value) (v Value) {
890 ptr, nvals := llvmValueRefs(constVals)
891 v.C = C.LLVMConstArray(t.C, ptr, nvals)
892 return
893 }
894 func ConstStruct(constVals []Value, packed bool) (v Value) {
895 ptr, nvals := llvmValueRefs(constVals)
896 v.C = C.LLVMConstStruct(ptr, nvals, boolToLLVMBool(packed))
897 return
898 }
899 func ConstVector(scalarConstVals []Value, packed bool) (v Value) {
900 ptr, nvals := llvmValueRefs(scalarConstVals)
901 v.C = C.LLVMConstVector(ptr, nvals)
902 return
903 }
904
905 // IsConstantString checks if the constant is an array of i8.
906 func (v Value) IsConstantString() bool {
907 return C.LLVMIsConstantString(v.C) != 0
908 }
909
910 // ConstGetAsString will return the string contained in a constant.
911 func (v Value) ConstGetAsString() string {
912 length := C.size_t(0)
913 cstr := C.LLVMGetAsString(v.C, &length)
914 return C.GoStringN(cstr, C.int(length))
915 }
916
917 // Constant expressions
918 func (v Value) Opcode() Opcode { return Opcode(C.LLVMGetConstOpcode(v.C)) }
919 func (v Value) InstructionOpcode() Opcode { return Opcode(C.LLVMGetInstructionOpcode(v.C)) }
920 func AlignOf(t Type) (v Value) { v.C = C.LLVMAlignOf(t.C); return }
921 func SizeOf(t Type) (v Value) { v.C = C.LLVMSizeOf(t.C); return }
922 func ConstNeg(v Value) (rv Value) { rv.C = C.LLVMConstNeg(v.C); return }
923 func ConstNSWNeg(v Value) (rv Value) { rv.C = C.LLVMConstNSWNeg(v.C); return }
924 func ConstNot(v Value) (rv Value) { rv.C = C.LLVMConstNot(v.C); return }
925 func ConstAdd(lhs, rhs Value) (v Value) { v.C = C.LLVMConstAdd(lhs.C, rhs.C); return }
926 func ConstNSWAdd(lhs, rhs Value) (v Value) { v.C = C.LLVMConstNSWAdd(lhs.C, rhs.C); return }
927 func ConstNUWAdd(lhs, rhs Value) (v Value) { v.C = C.LLVMConstNUWAdd(lhs.C, rhs.C); return }
928 func ConstSub(lhs, rhs Value) (v Value) { v.C = C.LLVMConstSub(lhs.C, rhs.C); return }
929 func ConstNSWSub(lhs, rhs Value) (v Value) { v.C = C.LLVMConstNSWSub(lhs.C, rhs.C); return }
930 func ConstNUWSub(lhs, rhs Value) (v Value) { v.C = C.LLVMConstNUWSub(lhs.C, rhs.C); return }
931 func ConstMul(lhs, rhs Value) (v Value) { v.C = C.LLVMConstMul(lhs.C, rhs.C); return }
932 func ConstNSWMul(lhs, rhs Value) (v Value) { v.C = C.LLVMConstNSWMul(lhs.C, rhs.C); return }
933 func ConstNUWMul(lhs, rhs Value) (v Value) { v.C = C.LLVMConstNUWMul(lhs.C, rhs.C); return }
934 func ConstXor(lhs, rhs Value) (v Value) { v.C = C.LLVMConstXor(lhs.C, rhs.C); return }
935
936 func ConstGEP(t Type, v Value, indices []Value) (rv Value) {
937 ptr, nvals := llvmValueRefs(indices)
938 rv.C = C.LLVMConstGEP2(t.C, v.C, ptr, nvals)
939 return
940 }
941 func ConstInBoundsGEP(t Type, v Value, indices []Value) (rv Value) {
942 ptr, nvals := llvmValueRefs(indices)
943 rv.C = C.LLVMConstInBoundsGEP2(t.C, v.C, ptr, nvals)
944 return
945 }
946 func ConstTrunc(v Value, t Type) (rv Value) { rv.C = C.LLVMConstTrunc(v.C, t.C); return }
947 func ConstPtrToInt(v Value, t Type) (rv Value) { rv.C = C.LLVMConstPtrToInt(v.C, t.C); return }
948 func ConstIntToPtr(v Value, t Type) (rv Value) { rv.C = C.LLVMConstIntToPtr(v.C, t.C); return }
949 func ConstBitCast(v Value, t Type) (rv Value) { rv.C = C.LLVMConstBitCast(v.C, t.C); return }
950 func ConstTruncOrBitCast(v Value, t Type) (rv Value) {
951 rv.C = C.LLVMConstTruncOrBitCast(v.C, t.C)
952 return
953 }
954 func ConstPointerCast(v Value, t Type) (rv Value) { rv.C = C.LLVMConstPointerCast(v.C, t.C); return }
955 func ConstExtractElement(vec, i Value) (rv Value) {
956 rv.C = C.LLVMConstExtractElement(vec.C, i.C)
957 return
958 }
959 func ConstInsertElement(vec, elem, i Value) (rv Value) {
960 rv.C = C.LLVMConstInsertElement(vec.C, elem.C, i.C)
961 return
962 }
963 func ConstShuffleVector(veca, vecb, mask Value) (rv Value) {
964 rv.C = C.LLVMConstShuffleVector(veca.C, vecb.C, mask.C)
965 return
966 }
967
968 func BlockAddress(f Value, bb BasicBlock) (v Value) {
969 v.C = C.LLVMBlockAddress(f.C, bb.C)
970 return
971 }
972
973 // Operations on global variables, functions, and aliases (globals)
974 func (v Value) GlobalParent() (m Module) { m.C = C.LLVMGetGlobalParent(v.C); return }
975 func (v Value) IsDeclaration() bool { return C.LLVMIsDeclaration(v.C) != 0 }
976 func (v Value) Linkage() Linkage { return Linkage(C.LLVMGetLinkage(v.C)) }
977 func (v Value) SetLinkage(l Linkage) { C.LLVMSetLinkage(v.C, C.LLVMLinkage(l)) }
978 func (v Value) Section() string { return C.GoString(C.LLVMGetSection(v.C)) }
979 func (v Value) SetSection(str string) {
980 cstr := C.CString(str)
981 defer C.free(unsafe.Pointer(cstr))
982 C.LLVMSetSection(v.C, cstr)
983 }
984 func (v Value) Visibility() Visibility { return Visibility(C.LLVMGetVisibility(v.C)) }
985 func (v Value) SetVisibility(vi Visibility) { C.LLVMSetVisibility(v.C, C.LLVMVisibility(vi)) }
986 func (v Value) Alignment() int { return int(C.LLVMGetAlignment(v.C)) }
987 func (v Value) SetAlignment(a int) { C.LLVMSetAlignment(v.C, C.unsigned(a)) }
988 func (v Value) SetUnnamedAddr(ua bool) { C.LLVMSetUnnamedAddr(v.C, boolToLLVMBool(ua)) }
989 func (v Value) GlobalValueType() (t Type) { t.C = C.LLVMGlobalGetValueType(v.C); return }
990
991 // Operations on global variables
992 func AddGlobal(m Module, t Type, name string) (v Value) {
993 cname := C.CString(name)
994 defer C.free(unsafe.Pointer(cname))
995 v.C = C.LLVMAddGlobal(m.C, t.C, cname)
996 return
997 }
998 func AddGlobalInAddressSpace(m Module, t Type, name string, addressSpace int) (v Value) {
999 cname := C.CString(name)
1000 defer C.free(unsafe.Pointer(cname))
1001 v.C = C.LLVMAddGlobalInAddressSpace(m.C, t.C, cname, C.unsigned(addressSpace))
1002 return
1003 }
1004 func (m Module) NamedGlobal(name string) (v Value) {
1005 cname := C.CString(name)
1006 defer C.free(unsafe.Pointer(cname))
1007 v.C = C.LLVMGetNamedGlobal(m.C, cname)
1008 return
1009 }
1010
1011 func (m Module) FirstGlobal() (v Value) { v.C = C.LLVMGetFirstGlobal(m.C); return }
1012 func (m Module) LastGlobal() (v Value) { v.C = C.LLVMGetLastGlobal(m.C); return }
1013 func NextGlobal(v Value) (rv Value) { rv.C = C.LLVMGetNextGlobal(v.C); return }
1014 func PrevGlobal(v Value) (rv Value) { rv.C = C.LLVMGetPreviousGlobal(v.C); return }
1015 func (v Value) EraseFromParentAsGlobal() { C.LLVMDeleteGlobal(v.C) }
1016 func (v Value) Initializer() (rv Value) { rv.C = C.LLVMGetInitializer(v.C); return }
1017 func (v Value) SetInitializer(cv Value) { C.LLVMSetInitializer(v.C, cv.C) }
1018 func (v Value) IsThreadLocal() bool { return C.LLVMIsThreadLocal(v.C) != 0 }
1019 func (v Value) SetThreadLocal(tl bool) { C.LLVMSetThreadLocal(v.C, boolToLLVMBool(tl)) }
1020 func (v Value) IsGlobalConstant() bool { return C.LLVMIsGlobalConstant(v.C) != 0 }
1021 func (v Value) SetGlobalConstant(gc bool) { C.LLVMSetGlobalConstant(v.C, boolToLLVMBool(gc)) }
1022 func (v Value) IsVolatile() bool { return C.LLVMGetVolatile(v.C) != 0 }
1023 func (v Value) SetVolatile(volatile bool) { C.LLVMSetVolatile(v.C, boolToLLVMBool(volatile)) }
1024 func (v Value) Ordering() AtomicOrdering { return AtomicOrdering(C.LLVMGetOrdering(v.C)) }
1025 func (v Value) SetOrdering(ordering AtomicOrdering) {
1026 C.LLVMSetOrdering(v.C, C.LLVMAtomicOrdering(ordering))
1027 }
1028 func (v Value) IsAtomicSingleThread() bool { return C.LLVMIsAtomicSingleThread(v.C) != 0 }
1029 func (v Value) SetAtomicSingleThread(singleThread bool) {
1030 C.LLVMSetAtomicSingleThread(v.C, boolToLLVMBool(singleThread))
1031 }
1032 func (v Value) CmpXchgSuccessOrdering() AtomicOrdering {
1033 return AtomicOrdering(C.LLVMGetCmpXchgSuccessOrdering(v.C))
1034 }
1035 func (v Value) SetCmpXchgSuccessOrdering(ordering AtomicOrdering) {
1036 C.LLVMSetCmpXchgSuccessOrdering(v.C, C.LLVMAtomicOrdering(ordering))
1037 }
1038 func (v Value) CmpXchgFailureOrdering() AtomicOrdering {
1039 return AtomicOrdering(C.LLVMGetCmpXchgFailureOrdering(v.C))
1040 }
1041 func (v Value) SetCmpXchgFailureOrdering(ordering AtomicOrdering) {
1042 C.LLVMSetCmpXchgFailureOrdering(v.C, C.LLVMAtomicOrdering(ordering))
1043 }
1044
1045 // Operations on aliases
1046 func AddAlias(m Module, t Type, addressSpace int, aliasee Value, name string) (v Value) {
1047 cname := C.CString(name)
1048 defer C.free(unsafe.Pointer(cname))
1049 v.C = C.LLVMAddAlias2(m.C, t.C, C.unsigned(addressSpace), aliasee.C, cname)
1050 return
1051 }
1052
1053 // Operations on comdat
1054 func (m Module) Comdat(name string) (c Comdat) {
1055 cname := C.CString(name)
1056 defer C.free(unsafe.Pointer(cname))
1057 c.C = C.LLVMGetOrInsertComdat(m.C, cname)
1058 return
1059 }
1060
1061 func (v Value) Comdat() (c Comdat) { c.C = C.LLVMGetComdat(v.C); return }
1062 func (v Value) SetComdat(c Comdat) { C.LLVMSetComdat(v.C, c.C) }
1063
1064 func (c Comdat) SelectionKind() ComdatSelectionKind {
1065 return ComdatSelectionKind(C.LLVMGetComdatSelectionKind(c.C))
1066 }
1067
1068 func (c Comdat) SetSelectionKind(k ComdatSelectionKind) {
1069 C.LLVMSetComdatSelectionKind(c.C, (C.LLVMComdatSelectionKind)(k))
1070 }
1071
1072 // Operations on functions
1073 func AddFunction(m Module, name string, ft Type) (v Value) {
1074 cname := C.CString(name)
1075 defer C.free(unsafe.Pointer(cname))
1076 v.C = C.LLVMAddFunction(m.C, cname, ft.C)
1077 return
1078 }
1079
1080 func (m Module) NamedFunction(name string) (v Value) {
1081 cname := C.CString(name)
1082 defer C.free(unsafe.Pointer(cname))
1083 v.C = C.LLVMGetNamedFunction(m.C, cname)
1084 return
1085 }
1086
1087 func (m Module) FirstFunction() (v Value) { v.C = C.LLVMGetFirstFunction(m.C); return }
1088 func (m Module) LastFunction() (v Value) { v.C = C.LLVMGetLastFunction(m.C); return }
1089 func NextFunction(v Value) (rv Value) { rv.C = C.LLVMGetNextFunction(v.C); return }
1090 func PrevFunction(v Value) (rv Value) { rv.C = C.LLVMGetPreviousFunction(v.C); return }
1091 func (v Value) EraseFromParentAsFunction() { C.LLVMDeleteFunction(v.C) }
1092 func (v Value) IntrinsicID() int { return int(C.LLVMGetIntrinsicID(v.C)) }
1093 func (v Value) FunctionCallConv() CallConv {
1094 return CallConv(C.LLVMCallConv(C.LLVMGetFunctionCallConv(v.C)))
1095 }
1096 func (v Value) SetFunctionCallConv(cc CallConv) { C.LLVMSetFunctionCallConv(v.C, C.unsigned(cc)) }
1097 func (v Value) GC() string { return C.GoString(C.LLVMGetGC(v.C)) }
1098 func (v Value) SetGC(name string) {
1099 cname := C.CString(name)
1100 defer C.free(unsafe.Pointer(cname))
1101 C.LLVMSetGC(v.C, cname)
1102 }
1103 func (v Value) AddAttributeAtIndex(i int, a Attribute) {
1104 C.LLVMAddAttributeAtIndex(v.C, C.LLVMAttributeIndex(i), a.C)
1105 }
1106 func (v Value) AddFunctionAttr(a Attribute) {
1107 v.AddAttributeAtIndex(C.LLVMAttributeFunctionIndex, a)
1108 }
1109 func (v Value) GetEnumAttributeAtIndex(i int, kind uint) (a Attribute) {
1110 a.C = C.LLVMGetEnumAttributeAtIndex(v.C, C.LLVMAttributeIndex(i), C.unsigned(kind))
1111 return
1112 }
1113 func (v Value) GetEnumFunctionAttribute(kind uint) Attribute {
1114 return v.GetEnumAttributeAtIndex(C.LLVMAttributeFunctionIndex, kind)
1115 }
1116 func (v Value) GetStringAttributeAtIndex(i int, kind string) (a Attribute) {
1117 ckind := C.CString(kind)
1118 defer C.free(unsafe.Pointer(ckind))
1119 a.C = C.LLVMGetStringAttributeAtIndex(v.C, C.LLVMAttributeIndex(i),
1120 ckind, C.unsigned(len(kind)))
1121 return
1122 }
1123 func (v Value) RemoveEnumAttributeAtIndex(i int, kind uint) {
1124 C.LLVMRemoveEnumAttributeAtIndex(v.C, C.LLVMAttributeIndex(i), C.unsigned(kind))
1125 }
1126 func (v Value) RemoveEnumFunctionAttribute(kind uint) {
1127 v.RemoveEnumAttributeAtIndex(C.LLVMAttributeFunctionIndex, kind)
1128 }
1129 func (v Value) RemoveStringAttributeAtIndex(i int, kind string) {
1130 ckind := C.CString(kind)
1131 defer C.free(unsafe.Pointer(ckind))
1132 C.LLVMRemoveStringAttributeAtIndex(v.C, C.LLVMAttributeIndex(i),
1133 ckind, C.unsigned(len(kind)))
1134 }
1135 func (v Value) AddTargetDependentFunctionAttr(attr, value string) {
1136 cattr := C.CString(attr)
1137 defer C.free(unsafe.Pointer(cattr))
1138 cvalue := C.CString(value)
1139 defer C.free(unsafe.Pointer(cvalue))
1140 C.LLVMAddTargetDependentFunctionAttr(v.C, cattr, cvalue)
1141 }
1142 func (v Value) SetPersonality(p Value) {
1143 C.LLVMSetPersonalityFn(v.C, p.C)
1144 }
1145
1146 // Operations on parameters
1147 func (v Value) ParamsCount() int { return int(C.LLVMCountParams(v.C)) }
1148 func (v Value) Params() []Value {
1149 out := make([]Value, v.ParamsCount())
1150 if len(out) > 0 {
1151 C.LLVMGetParams(v.C, llvmValueRefPtr(&out[0]))
1152 }
1153 return out
1154 }
1155 func (v Value) Param(i int) (rv Value) { rv.C = C.LLVMGetParam(v.C, C.unsigned(i)); return }
1156 func (v Value) ParamParent() (rv Value) { rv.C = C.LLVMGetParamParent(v.C); return }
1157 func (v Value) FirstParam() (rv Value) { rv.C = C.LLVMGetFirstParam(v.C); return }
1158 func (v Value) LastParam() (rv Value) { rv.C = C.LLVMGetLastParam(v.C); return }
1159 func NextParam(v Value) (rv Value) { rv.C = C.LLVMGetNextParam(v.C); return }
1160 func PrevParam(v Value) (rv Value) { rv.C = C.LLVMGetPreviousParam(v.C); return }
1161 func (v Value) SetParamAlignment(align int) { C.LLVMSetParamAlignment(v.C, C.unsigned(align)) }
1162
1163 // Operations on basic blocks
1164 func (bb BasicBlock) AsValue() (v Value) { v.C = C.LLVMBasicBlockAsValue(bb.C); return }
1165 func (v Value) IsBasicBlock() bool { return C.LLVMValueIsBasicBlock(v.C) != 0 }
1166 func (v Value) AsBasicBlock() (bb BasicBlock) { bb.C = C.LLVMValueAsBasicBlock(v.C); return }
1167 func (bb BasicBlock) Parent() (v Value) { v.C = C.LLVMGetBasicBlockParent(bb.C); return }
1168 func (v Value) BasicBlocksCount() int { return int(C.LLVMCountBasicBlocks(v.C)) }
1169 func (v Value) BasicBlocks() []BasicBlock {
1170 out := make([]BasicBlock, v.BasicBlocksCount())
1171 C.LLVMGetBasicBlocks(v.C, llvmBasicBlockRefPtr(&out[0]))
1172 return out
1173 }
1174 func (v Value) FirstBasicBlock() (bb BasicBlock) {
1175 bb.C = C.LLVMGetFirstBasicBlock(v.C)
1176 return
1177 }
1178 func (v Value) LastBasicBlock() (bb BasicBlock) {
1179 bb.C = C.LLVMGetLastBasicBlock(v.C)
1180 return
1181 }
1182 func NextBasicBlock(bb BasicBlock) (rbb BasicBlock) {
1183 rbb.C = C.LLVMGetNextBasicBlock(bb.C)
1184 return
1185 }
1186 func PrevBasicBlock(bb BasicBlock) (rbb BasicBlock) {
1187 rbb.C = C.LLVMGetPreviousBasicBlock(bb.C)
1188 return
1189 }
1190 func (v Value) EntryBasicBlock() (bb BasicBlock) {
1191 bb.C = C.LLVMGetEntryBasicBlock(v.C)
1192 return
1193 }
1194 func (c Context) AddBasicBlock(f Value, name string) (bb BasicBlock) {
1195 cname := C.CString(name)
1196 defer C.free(unsafe.Pointer(cname))
1197 bb.C = C.LLVMAppendBasicBlockInContext(c.C, f.C, cname)
1198 return
1199 }
1200 func (c Context) InsertBasicBlock(ref BasicBlock, name string) (bb BasicBlock) {
1201 cname := C.CString(name)
1202 defer C.free(unsafe.Pointer(cname))
1203 bb.C = C.LLVMInsertBasicBlockInContext(c.C, ref.C, cname)
1204 return
1205 }
1206 func AddBasicBlock(f Value, name string) (bb BasicBlock) {
1207 cname := C.CString(name)
1208 defer C.free(unsafe.Pointer(cname))
1209 bb.C = C.LLVMAppendBasicBlock(f.C, cname)
1210 return
1211 }
1212 func InsertBasicBlock(ref BasicBlock, name string) (bb BasicBlock) {
1213 cname := C.CString(name)
1214 defer C.free(unsafe.Pointer(cname))
1215 bb.C = C.LLVMInsertBasicBlock(ref.C, cname)
1216 return
1217 }
1218 func (bb BasicBlock) EraseFromParent() { C.LLVMDeleteBasicBlock(bb.C) }
1219 func (bb BasicBlock) MoveBefore(pos BasicBlock) { C.LLVMMoveBasicBlockBefore(bb.C, pos.C) }
1220 func (bb BasicBlock) MoveAfter(pos BasicBlock) { C.LLVMMoveBasicBlockAfter(bb.C, pos.C) }
1221
1222 // Operations on instructions
1223 func (v Value) EraseFromParentAsInstruction() { C.LLVMInstructionEraseFromParent(v.C) }
1224 func (v Value) RemoveFromParentAsInstruction() { C.LLVMInstructionRemoveFromParent(v.C) }
1225 func (v Value) InstructionParent() (bb BasicBlock) { bb.C = C.LLVMGetInstructionParent(v.C); return }
1226 func (v Value) InstructionDebugLoc() (md Metadata) { md.C = C.LLVMInstructionGetDebugLoc(v.C); return }
1227 func (v Value) InstructionSetDebugLoc(md Metadata) { C.LLVMInstructionSetDebugLoc(v.C, md.C) }
1228 func (bb BasicBlock) FirstInstruction() (v Value) { v.C = C.LLVMGetFirstInstruction(bb.C); return }
1229 func (bb BasicBlock) LastInstruction() (v Value) { v.C = C.LLVMGetLastInstruction(bb.C); return }
1230 func NextInstruction(v Value) (rv Value) { rv.C = C.LLVMGetNextInstruction(v.C); return }
1231 func PrevInstruction(v Value) (rv Value) { rv.C = C.LLVMGetPreviousInstruction(v.C); return }
1232
1233 // Operations on call sites
1234 func (v Value) SetInstructionCallConv(cc CallConv) {
1235 C.LLVMSetInstructionCallConv(v.C, C.unsigned(cc))
1236 }
1237 func (v Value) InstructionCallConv() CallConv {
1238 return CallConv(C.LLVMCallConv(C.LLVMGetInstructionCallConv(v.C)))
1239 }
1240 func (v Value) AddCallSiteAttribute(i int, a Attribute) {
1241 C.LLVMAddCallSiteAttribute(v.C, C.LLVMAttributeIndex(i), a.C)
1242 }
1243 func (v Value) GetCallSiteEnumAttribute(i int, kind uint) (a Attribute) {
1244 a.C = C.LLVMGetCallSiteEnumAttribute(v.C, C.LLVMAttributeIndex(i), C.unsigned(kind))
1245 return
1246 }
1247 func (v Value) GetCallSiteStringAttribute(i int, kind string) (a Attribute) {
1248 ckind := C.CString(kind)
1249 defer C.free(unsafe.Pointer(ckind))
1250 a.C = C.LLVMGetCallSiteStringAttribute(v.C, C.LLVMAttributeIndex(i),
1251 ckind, C.unsigned(len(kind)))
1252 return
1253 }
1254 func (v Value) SetInstrParamAlignment(i int, align int) {
1255 C.LLVMSetInstrParamAlignment(v.C, C.unsigned(i), C.unsigned(align))
1256 }
1257 func (v Value) CalledValue() (rv Value) {
1258 rv.C = C.LLVMGetCalledValue(v.C)
1259 return
1260 }
1261 func (v Value) CalledFunctionType() (t Type) {
1262 t.C = C.LLVMGetCalledFunctionType(v.C)
1263 return
1264 }
1265
1266 // Operations on call instructions (only)
1267 func (v Value) IsTailCall() bool { return C.LLVMIsTailCall(v.C) != 0 }
1268 func (v Value) SetTailCall(is bool) { C.LLVMSetTailCall(v.C, boolToLLVMBool(is)) }
1269
1270 // Operations on phi nodes
1271 func (v Value) AddIncoming(vals []Value, blocks []BasicBlock) {
1272 ptr, nvals := llvmValueRefs(vals)
1273 C.LLVMAddIncoming(v.C, ptr, llvmBasicBlockRefPtr(&blocks[0]), nvals)
1274 }
1275 func (v Value) IncomingCount() int { return int(C.LLVMCountIncoming(v.C)) }
1276 func (v Value) IncomingValue(i int) (rv Value) {
1277 rv.C = C.LLVMGetIncomingValue(v.C, C.unsigned(i))
1278 return
1279 }
1280 func (v Value) IncomingBlock(i int) (bb BasicBlock) {
1281 bb.C = C.LLVMGetIncomingBlock(v.C, C.unsigned(i))
1282 return
1283 }
1284
1285 // Operations on inline assembly
1286 func InlineAsm(t Type, asmString, constraints string, hasSideEffects, isAlignStack bool, dialect InlineAsmDialect, canThrow bool) (rv Value) {
1287 casm := C.CString(asmString)
1288 defer C.free(unsafe.Pointer(casm))
1289 cconstraints := C.CString(constraints)
1290 defer C.free(unsafe.Pointer(cconstraints))
1291 rv.C = C.LLVMGoGetInlineAsm(t.C, casm, C.size_t(len(asmString)), cconstraints, C.size_t(len(constraints)), boolToLLVMBool(hasSideEffects), boolToLLVMBool(isAlignStack), C.LLVMInlineAsmDialect(dialect), boolToLLVMBool(canThrow))
1292 return
1293 }
1294
1295 // Operations on aggregates
1296 func (v Value) Indices() []uint32 {
1297 num := C.LLVMGetNumIndices(v.C)
1298 indicesPtr := C.LLVMGetIndices(v.C)
1299 // https://github.com/golang/go/wiki/cgo#turning-c-arrays-into-go-slices
1300 rawIndices := (*[1 << 20]C.uint)(unsafe.Pointer(indicesPtr))[:num:num]
1301 indices := make([]uint32, num)
1302 for i := range indices {
1303 indices[i] = uint32(rawIndices[i])
1304 }
1305 return indices
1306 }
1307
1308 // Operations on comparisons
1309 func (v Value) IntPredicate() IntPredicate { return IntPredicate(C.LLVMGetICmpPredicate(v.C)) }
1310 func (v Value) FloatPredicate() FloatPredicate { return FloatPredicate(C.LLVMGetFCmpPredicate(v.C)) }
1311
1312 // Operations on GEPs
1313 func (v Value) GEPSourceElementType() (t Type) { t.C = C.LLVMGetGEPSourceElementType(v.C); return }
1314
1315 // Operations on allocas
1316 func (v Value) AllocatedType() (t Type) { t.C = C.LLVMGetAllocatedType(v.C); return }
1317
1318 //-------------------------------------------------------------------------
1319 // llvm.Builder
1320 //-------------------------------------------------------------------------
1321
1322 // An instruction builder represents a point within a basic block, and is the
1323 // exclusive means of building instructions using the C interface.
1324
1325 func (c Context) NewBuilder() (b Builder) { b.C = C.LLVMCreateBuilderInContext(c.C); return }
1326 func (b Builder) SetInsertPoint(block BasicBlock, instr Value) {
1327 C.LLVMPositionBuilder(b.C, block.C, instr.C)
1328 }
1329 func (b Builder) SetInsertPointBefore(instr Value) { C.LLVMPositionBuilderBefore(b.C, instr.C) }
1330 func (b Builder) SetInsertPointAtEnd(block BasicBlock) { C.LLVMPositionBuilderAtEnd(b.C, block.C) }
1331 func (b Builder) GetInsertBlock() (bb BasicBlock) { bb.C = C.LLVMGetInsertBlock(b.C); return }
1332 func (b Builder) ClearInsertionPoint() { C.LLVMClearInsertionPosition(b.C) }
1333 func (b Builder) Insert(instr Value) { C.LLVMInsertIntoBuilder(b.C, instr.C) }
1334 func (b Builder) InsertWithName(instr Value, name string) {
1335 cname := C.CString(name)
1336 defer C.free(unsafe.Pointer(cname))
1337 C.LLVMInsertIntoBuilderWithName(b.C, instr.C, cname)
1338 }
1339 func (b Builder) Dispose() { C.LLVMDisposeBuilder(b.C) }
1340
1341 // Metadata
1342 type DebugLoc struct {
1343 Line, Col uint
1344 Scope Metadata
1345 InlinedAt Metadata
1346 }
1347
1348 func (b Builder) SetCurrentDebugLocation(line, col uint, scope, inlinedAt Metadata) {
1349 C.LLVMGoSetCurrentDebugLocation(b.C, C.unsigned(line), C.unsigned(col), scope.C, inlinedAt.C)
1350 }
1351
1352 // Get current debug location. Please do not call this function until setting debug location with SetCurrentDebugLocation()
1353 func (b Builder) GetCurrentDebugLocation() (loc DebugLoc) {
1354 md := C.LLVMGoGetCurrentDebugLocation(b.C)
1355 loc.Line = uint(md.Line)
1356 loc.Col = uint(md.Col)
1357 loc.Scope = Metadata{C: md.Scope}
1358 loc.InlinedAt = Metadata{C: md.InlinedAt}
1359 return
1360 }
1361 func (b Builder) SetInstDebugLocation(v Value) { C.LLVMSetInstDebugLocation(b.C, v.C) }
1362 func (b Builder) InsertDeclare(module Module, storage Value, md Value) Value {
1363 f := module.NamedFunction("llvm.dbg.declare")
1364 ftyp := FunctionType(module.Context().VoidType(), []Type{storage.Type(), md.Type()}, false)
1365 if f.IsNil() {
1366 f = AddFunction(module, "llvm.dbg.declare", ftyp)
1367 }
1368 return b.CreateCall(ftyp, f, []Value{storage, md}, "")
1369 }
1370
1371 // Terminators
1372 func (b Builder) CreateRetVoid() (rv Value) { rv.C = C.LLVMBuildRetVoid(b.C); return }
1373 func (b Builder) CreateRet(v Value) (rv Value) { rv.C = C.LLVMBuildRet(b.C, v.C); return }
1374 func (b Builder) CreateAggregateRet(vs []Value) (rv Value) {
1375 ptr, nvals := llvmValueRefs(vs)
1376 rv.C = C.LLVMBuildAggregateRet(b.C, ptr, nvals)
1377 return
1378 }
1379 func (b Builder) CreateBr(bb BasicBlock) (rv Value) { rv.C = C.LLVMBuildBr(b.C, bb.C); return }
1380 func (b Builder) CreateCondBr(ifv Value, thenb, elseb BasicBlock) (rv Value) {
1381 rv.C = C.LLVMBuildCondBr(b.C, ifv.C, thenb.C, elseb.C)
1382 return
1383 }
1384 func (b Builder) CreateSwitch(v Value, elseb BasicBlock, numCases int) (rv Value) {
1385 rv.C = C.LLVMBuildSwitch(b.C, v.C, elseb.C, C.unsigned(numCases))
1386 return
1387 }
1388 func (b Builder) CreateIndirectBr(addr Value, numDests int) (rv Value) {
1389 rv.C = C.LLVMBuildIndirectBr(b.C, addr.C, C.unsigned(numDests))
1390 return
1391 }
1392 func (b Builder) CreateInvoke(t Type, fn Value, args []Value, then, catch BasicBlock, name string) (rv Value) {
1393 cname := C.CString(name)
1394 defer C.free(unsafe.Pointer(cname))
1395 ptr, nvals := llvmValueRefs(args)
1396 rv.C = C.LLVMBuildInvoke2(b.C, t.C, fn.C, ptr, nvals, then.C, catch.C, cname)
1397 return
1398 }
1399 func (b Builder) CreateUnreachable() (rv Value) { rv.C = C.LLVMBuildUnreachable(b.C); return }
1400
1401 // Exception Handling
1402
1403 func (b Builder) CreateResume(ex Value) (v Value) {
1404 v.C = C.LLVMBuildResume(b.C, ex.C)
1405 return
1406 }
1407
1408 func (b Builder) CreateLandingPad(t Type, nclauses int, name string) (l Value) {
1409 cname := C.CString(name)
1410 defer C.free(unsafe.Pointer(cname))
1411 l.C = C.LLVMBuildLandingPad(b.C, t.C, nil, C.unsigned(nclauses), cname)
1412 return l
1413 }
1414
1415 func (b Builder) CreateCleanupRet(catchpad Value, bb BasicBlock) (v Value) {
1416 v.C = C.LLVMBuildCleanupRet(b.C, catchpad.C, bb.C)
1417 return
1418 }
1419
1420 func (b Builder) CreateCatchRet(catchpad Value, bb BasicBlock) (v Value) {
1421 v.C = C.LLVMBuildCatchRet(b.C, catchpad.C, bb.C)
1422 return
1423 }
1424
1425 func (b Builder) CreateCatchPad(parentPad Value, args []Value, name string) (v Value) {
1426 cname := C.CString(name)
1427 defer C.free(unsafe.Pointer(cname))
1428 ptr, nvals := llvmValueRefs(args)
1429 v.C = C.LLVMBuildCatchPad(b.C, parentPad.C, ptr, nvals, cname)
1430 return
1431 }
1432
1433 func (b Builder) CreateCleanupPad(parentPad Value, args []Value, name string) (v Value) {
1434 cname := C.CString(name)
1435 defer C.free(unsafe.Pointer(cname))
1436 ptr, nvals := llvmValueRefs(args)
1437 v.C = C.LLVMBuildCleanupPad(b.C, parentPad.C, ptr, nvals, cname)
1438 return
1439 }
1440 func (b Builder) CreateCatchSwitch(parentPad Value, unwindBB BasicBlock, numHandlers int, name string) (v Value) {
1441 cname := C.CString(name)
1442 defer C.free(unsafe.Pointer(cname))
1443 v.C = C.LLVMBuildCatchSwitch(b.C, parentPad.C, unwindBB.C, C.unsigned(numHandlers), cname)
1444 return
1445 }
1446
1447 // Add a case to the switch instruction
1448 func (v Value) AddCase(on Value, dest BasicBlock) { C.LLVMAddCase(v.C, on.C, dest.C) }
1449
1450 // Add a destination to the indirectbr instruction
1451 func (v Value) AddDest(dest BasicBlock) { C.LLVMAddDestination(v.C, dest.C) }
1452
1453 // Add a destination to the catchswitch instruction.
1454 func (v Value) AddHandler(bb BasicBlock) { C.LLVMAddHandler(v.C, bb.C) }
1455
1456 // Obtain the basic blocks acting as handlers for a catchswitch instruction.
1457 func (v Value) GetHandlers() []BasicBlock {
1458 num := C.LLVMGetNumHandlers(v.C)
1459 if num == 0 {
1460 return nil
1461 }
1462 blocks := make([]BasicBlock, num)
1463 C.LLVMGetHandlers(v.C, &blocks[0].C)
1464 return blocks
1465 }
1466
1467 // Get the parent catchswitch instruction of a catchpad instruction.
1468 //
1469 // This only works on catchpad instructions.
1470 func (v Value) GetParentCatchSwitch() (rv Value) {
1471 rv.C = C.LLVMGetParentCatchSwitch(v.C)
1472 return
1473 }
1474
1475 // Set the parent catchswitch instruction of a catchpad instruction.
1476 //
1477 // This only works on llvm::CatchPadInst instructions.
1478 func (v Value) SetParentCatchSwitch(catchSwitch Value) {
1479 C.LLVMSetParentCatchSwitch(v.C, catchSwitch.C)
1480 }
1481
1482 // Arithmetic
1483 func (b Builder) CreateAdd(lhs, rhs Value, name string) (v Value) {
1484 cname := C.CString(name)
1485 defer C.free(unsafe.Pointer(cname))
1486 v.C = C.LLVMBuildAdd(b.C, lhs.C, rhs.C, cname)
1487 return
1488 }
1489 func (b Builder) CreateNSWAdd(lhs, rhs Value, name string) (v Value) {
1490 cname := C.CString(name)
1491 defer C.free(unsafe.Pointer(cname))
1492 v.C = C.LLVMBuildNSWAdd(b.C, lhs.C, rhs.C, cname)
1493 return
1494 }
1495 func (b Builder) CreateNUWAdd(lhs, rhs Value, name string) (v Value) {
1496 cname := C.CString(name)
1497 defer C.free(unsafe.Pointer(cname))
1498 v.C = C.LLVMBuildNUWAdd(b.C, lhs.C, rhs.C, cname)
1499 return
1500 }
1501 func (b Builder) CreateFAdd(lhs, rhs Value, name string) (v Value) {
1502 cname := C.CString(name)
1503 defer C.free(unsafe.Pointer(cname))
1504 v.C = C.LLVMBuildFAdd(b.C, lhs.C, rhs.C, cname)
1505 return
1506 }
1507 func (b Builder) CreateSub(lhs, rhs Value, name string) (v Value) {
1508 cname := C.CString(name)
1509 defer C.free(unsafe.Pointer(cname))
1510 v.C = C.LLVMBuildSub(b.C, lhs.C, rhs.C, cname)
1511 return
1512 }
1513 func (b Builder) CreateNSWSub(lhs, rhs Value, name string) (v Value) {
1514 cname := C.CString(name)
1515 defer C.free(unsafe.Pointer(cname))
1516 v.C = C.LLVMBuildNSWSub(b.C, lhs.C, rhs.C, cname)
1517 return
1518 }
1519 func (b Builder) CreateNUWSub(lhs, rhs Value, name string) (v Value) {
1520 cname := C.CString(name)
1521 defer C.free(unsafe.Pointer(cname))
1522 v.C = C.LLVMBuildNUWSub(b.C, lhs.C, rhs.C, cname)
1523 return
1524 }
1525 func (b Builder) CreateFSub(lhs, rhs Value, name string) (v Value) {
1526 cname := C.CString(name)
1527 v.C = C.LLVMBuildFSub(b.C, lhs.C, rhs.C, cname)
1528 C.free(unsafe.Pointer(cname))
1529 return
1530 }
1531 func (b Builder) CreateMul(lhs, rhs Value, name string) (v Value) {
1532 cname := C.CString(name)
1533 defer C.free(unsafe.Pointer(cname))
1534 v.C = C.LLVMBuildMul(b.C, lhs.C, rhs.C, cname)
1535 return
1536 }
1537 func (b Builder) CreateNSWMul(lhs, rhs Value, name string) (v Value) {
1538 cname := C.CString(name)
1539 defer C.free(unsafe.Pointer(cname))
1540 v.C = C.LLVMBuildNSWMul(b.C, lhs.C, rhs.C, cname)
1541 return
1542 }
1543 func (b Builder) CreateNUWMul(lhs, rhs Value, name string) (v Value) {
1544 cname := C.CString(name)
1545 defer C.free(unsafe.Pointer(cname))
1546 v.C = C.LLVMBuildNUWMul(b.C, lhs.C, rhs.C, cname)
1547 return
1548 }
1549 func (b Builder) CreateFMul(lhs, rhs Value, name string) (v Value) {
1550 cname := C.CString(name)
1551 defer C.free(unsafe.Pointer(cname))
1552 v.C = C.LLVMBuildFMul(b.C, lhs.C, rhs.C, cname)
1553 return
1554 }
1555 func (b Builder) CreateUDiv(lhs, rhs Value, name string) (v Value) {
1556 cname := C.CString(name)
1557 defer C.free(unsafe.Pointer(cname))
1558 v.C = C.LLVMBuildUDiv(b.C, lhs.C, rhs.C, cname)
1559 return
1560 }
1561 func (b Builder) CreateSDiv(lhs, rhs Value, name string) (v Value) {
1562 cname := C.CString(name)
1563 defer C.free(unsafe.Pointer(cname))
1564 v.C = C.LLVMBuildSDiv(b.C, lhs.C, rhs.C, cname)
1565 return
1566 }
1567 func (b Builder) CreateExactSDiv(lhs, rhs Value, name string) (v Value) {
1568 cname := C.CString(name)
1569 defer C.free(unsafe.Pointer(cname))
1570 v.C = C.LLVMBuildExactSDiv(b.C, lhs.C, rhs.C, cname)
1571 return
1572 }
1573 func (b Builder) CreateFDiv(lhs, rhs Value, name string) (v Value) {
1574 cname := C.CString(name)
1575 defer C.free(unsafe.Pointer(cname))
1576 v.C = C.LLVMBuildFDiv(b.C, lhs.C, rhs.C, cname)
1577 return
1578 }
1579 func (b Builder) CreateURem(lhs, rhs Value, name string) (v Value) {
1580 cname := C.CString(name)
1581 defer C.free(unsafe.Pointer(cname))
1582 v.C = C.LLVMBuildURem(b.C, lhs.C, rhs.C, cname)
1583 return
1584 }
1585 func (b Builder) CreateSRem(lhs, rhs Value, name string) (v Value) {
1586 cname := C.CString(name)
1587 defer C.free(unsafe.Pointer(cname))
1588 v.C = C.LLVMBuildSRem(b.C, lhs.C, rhs.C, cname)
1589 return
1590 }
1591 func (b Builder) CreateFRem(lhs, rhs Value, name string) (v Value) {
1592 cname := C.CString(name)
1593 defer C.free(unsafe.Pointer(cname))
1594 v.C = C.LLVMBuildFRem(b.C, lhs.C, rhs.C, cname)
1595 return
1596 }
1597 func (b Builder) CreateShl(lhs, rhs Value, name string) (v Value) {
1598 cname := C.CString(name)
1599 defer C.free(unsafe.Pointer(cname))
1600 v.C = C.LLVMBuildShl(b.C, lhs.C, rhs.C, cname)
1601 return
1602 }
1603 func (b Builder) CreateLShr(lhs, rhs Value, name string) (v Value) {
1604 cname := C.CString(name)
1605 defer C.free(unsafe.Pointer(cname))
1606 v.C = C.LLVMBuildLShr(b.C, lhs.C, rhs.C, cname)
1607 return
1608 }
1609 func (b Builder) CreateAShr(lhs, rhs Value, name string) (v Value) {
1610 cname := C.CString(name)
1611 defer C.free(unsafe.Pointer(cname))
1612 v.C = C.LLVMBuildAShr(b.C, lhs.C, rhs.C, cname)
1613 return
1614 }
1615 func (b Builder) CreateAnd(lhs, rhs Value, name string) (v Value) {
1616 cname := C.CString(name)
1617 defer C.free(unsafe.Pointer(cname))
1618 v.C = C.LLVMBuildAnd(b.C, lhs.C, rhs.C, cname)
1619 return
1620 }
1621 func (b Builder) CreateOr(lhs, rhs Value, name string) (v Value) {
1622 cname := C.CString(name)
1623 defer C.free(unsafe.Pointer(cname))
1624 v.C = C.LLVMBuildOr(b.C, lhs.C, rhs.C, cname)
1625 return
1626 }
1627 func (b Builder) CreateXor(lhs, rhs Value, name string) (v Value) {
1628 cname := C.CString(name)
1629 defer C.free(unsafe.Pointer(cname))
1630 v.C = C.LLVMBuildXor(b.C, lhs.C, rhs.C, cname)
1631 return
1632 }
1633 func (b Builder) CreateBinOp(op Opcode, lhs, rhs Value, name string) (v Value) {
1634 cname := C.CString(name)
1635 defer C.free(unsafe.Pointer(cname))
1636 v.C = C.LLVMBuildBinOp(b.C, C.LLVMOpcode(op), lhs.C, rhs.C, cname)
1637 return
1638 }
1639 func (b Builder) CreateNeg(v Value, name string) (rv Value) {
1640 cname := C.CString(name)
1641 defer C.free(unsafe.Pointer(cname))
1642 rv.C = C.LLVMBuildNeg(b.C, v.C, cname)
1643 return
1644 }
1645 func (b Builder) CreateNSWNeg(v Value, name string) (rv Value) {
1646 cname := C.CString(name)
1647 defer C.free(unsafe.Pointer(cname))
1648 rv.C = C.LLVMBuildNSWNeg(b.C, v.C, cname)
1649 return
1650 }
1651 func (b Builder) CreateFNeg(v Value, name string) (rv Value) {
1652 cname := C.CString(name)
1653 defer C.free(unsafe.Pointer(cname))
1654 rv.C = C.LLVMBuildFNeg(b.C, v.C, cname)
1655 return
1656 }
1657 func (b Builder) CreateNot(v Value, name string) (rv Value) {
1658 cname := C.CString(name)
1659 defer C.free(unsafe.Pointer(cname))
1660 rv.C = C.LLVMBuildNot(b.C, v.C, cname)
1661 return
1662 }
1663
1664 // Memory
1665
1666 func (b Builder) CreateMalloc(t Type, name string) (v Value) {
1667 cname := C.CString(name)
1668 defer C.free(unsafe.Pointer(cname))
1669 v.C = C.LLVMBuildMalloc(b.C, t.C, cname)
1670 return
1671 }
1672 func (b Builder) CreateArrayMalloc(t Type, val Value, name string) (v Value) {
1673 cname := C.CString(name)
1674 defer C.free(unsafe.Pointer(cname))
1675 v.C = C.LLVMBuildArrayMalloc(b.C, t.C, val.C, cname)
1676 return
1677 }
1678 func (b Builder) CreateAlloca(t Type, name string) (v Value) {
1679 cname := C.CString(name)
1680 defer C.free(unsafe.Pointer(cname))
1681 v.C = C.LLVMBuildAlloca(b.C, t.C, cname)
1682 return
1683 }
1684 func (b Builder) CreateArrayAlloca(t Type, val Value, name string) (v Value) {
1685 cname := C.CString(name)
1686 defer C.free(unsafe.Pointer(cname))
1687 v.C = C.LLVMBuildArrayAlloca(b.C, t.C, val.C, cname)
1688 return
1689 }
1690 func (b Builder) CreateFree(p Value) (v Value) {
1691 v.C = C.LLVMBuildFree(b.C, p.C)
1692 return
1693 }
1694 func (b Builder) CreateLoad(t Type, p Value, name string) (v Value) {
1695 cname := C.CString(name)
1696 defer C.free(unsafe.Pointer(cname))
1697 v.C = C.LLVMBuildLoad2(b.C, t.C, p.C, cname)
1698 return
1699 }
1700 func (b Builder) CreateStore(val Value, p Value) (v Value) {
1701 v.C = C.LLVMBuildStore(b.C, val.C, p.C)
1702 return
1703 }
1704 func (b Builder) CreateGEP(t Type, p Value, indices []Value, name string) (v Value) {
1705 cname := C.CString(name)
1706 defer C.free(unsafe.Pointer(cname))
1707 ptr, nvals := llvmValueRefs(indices)
1708 v.C = C.LLVMBuildGEP2(b.C, t.C, p.C, ptr, nvals, cname)
1709 return
1710 }
1711 func (b Builder) CreateInBoundsGEP(t Type, p Value, indices []Value, name string) (v Value) {
1712 cname := C.CString(name)
1713 defer C.free(unsafe.Pointer(cname))
1714 ptr, nvals := llvmValueRefs(indices)
1715 v.C = C.LLVMBuildInBoundsGEP2(b.C, t.C, p.C, ptr, nvals, cname)
1716 return
1717 }
1718 func (b Builder) CreateStructGEP(t Type, p Value, i int, name string) (v Value) {
1719 cname := C.CString(name)
1720 defer C.free(unsafe.Pointer(cname))
1721 v.C = C.LLVMBuildStructGEP2(b.C, t.C, p.C, C.unsigned(i), cname)
1722 return
1723 }
1724 func (b Builder) CreateGlobalString(str, name string) (v Value) {
1725 cstr := C.CString(str)
1726 defer C.free(unsafe.Pointer(cstr))
1727 cname := C.CString(name)
1728 defer C.free(unsafe.Pointer(cname))
1729 v.C = C.LLVMBuildGlobalString(b.C, cstr, cname)
1730 return
1731 }
1732 func (b Builder) CreateGlobalStringPtr(str, name string) (v Value) {
1733 cstr := C.CString(str)
1734 defer C.free(unsafe.Pointer(cstr))
1735 cname := C.CString(name)
1736 defer C.free(unsafe.Pointer(cname))
1737 v.C = C.LLVMBuildGlobalStringPtr(b.C, cstr, cname)
1738 return
1739 }
1740 func (b Builder) CreateAtomicRMW(op AtomicRMWBinOp, ptr, val Value, ordering AtomicOrdering, singleThread bool) (v Value) {
1741 v.C = C.LLVMBuildAtomicRMW(b.C, C.LLVMAtomicRMWBinOp(op), ptr.C, val.C, C.LLVMAtomicOrdering(ordering), boolToLLVMBool(singleThread))
1742 return
1743 }
1744 func (b Builder) CreateAtomicCmpXchg(ptr, cmp, newVal Value, successOrdering, failureOrdering AtomicOrdering, singleThread bool) (v Value) {
1745 v.C = C.LLVMBuildAtomicCmpXchg(b.C, ptr.C, cmp.C, newVal.C, C.LLVMAtomicOrdering(successOrdering), C.LLVMAtomicOrdering(failureOrdering), boolToLLVMBool(singleThread))
1746 return
1747 }
1748
1749 // Casts
1750 func (b Builder) CreateTrunc(val Value, t Type, name string) (v Value) {
1751 cname := C.CString(name)
1752 defer C.free(unsafe.Pointer(cname))
1753 v.C = C.LLVMBuildTrunc(b.C, val.C, t.C, cname)
1754 return
1755 }
1756 func (b Builder) CreateZExt(val Value, t Type, name string) (v Value) {
1757 cname := C.CString(name)
1758 defer C.free(unsafe.Pointer(cname))
1759 v.C = C.LLVMBuildZExt(b.C, val.C, t.C, cname)
1760 return
1761 }
1762 func (b Builder) CreateSExt(val Value, t Type, name string) (v Value) {
1763 cname := C.CString(name)
1764 defer C.free(unsafe.Pointer(cname))
1765 v.C = C.LLVMBuildSExt(b.C, val.C, t.C, cname)
1766 return
1767 }
1768 func (b Builder) CreateFPToUI(val Value, t Type, name string) (v Value) {
1769 cname := C.CString(name)
1770 defer C.free(unsafe.Pointer(cname))
1771 v.C = C.LLVMBuildFPToUI(b.C, val.C, t.C, cname)
1772 return
1773 }
1774 func (b Builder) CreateFPToSI(val Value, t Type, name string) (v Value) {
1775 cname := C.CString(name)
1776 defer C.free(unsafe.Pointer(cname))
1777 v.C = C.LLVMBuildFPToSI(b.C, val.C, t.C, cname)
1778 return
1779 }
1780 func (b Builder) CreateUIToFP(val Value, t Type, name string) (v Value) {
1781 cname := C.CString(name)
1782 defer C.free(unsafe.Pointer(cname))
1783 v.C = C.LLVMBuildUIToFP(b.C, val.C, t.C, cname)
1784 return
1785 }
1786 func (b Builder) CreateSIToFP(val Value, t Type, name string) (v Value) {
1787 cname := C.CString(name)
1788 defer C.free(unsafe.Pointer(cname))
1789 v.C = C.LLVMBuildSIToFP(b.C, val.C, t.C, cname)
1790 return
1791 }
1792 func (b Builder) CreateFPTrunc(val Value, t Type, name string) (v Value) {
1793 cname := C.CString(name)
1794 defer C.free(unsafe.Pointer(cname))
1795 v.C = C.LLVMBuildFPTrunc(b.C, val.C, t.C, cname)
1796 return
1797 }
1798 func (b Builder) CreateFPExt(val Value, t Type, name string) (v Value) {
1799 cname := C.CString(name)
1800 defer C.free(unsafe.Pointer(cname))
1801 v.C = C.LLVMBuildFPExt(b.C, val.C, t.C, cname)
1802 return
1803 }
1804 func (b Builder) CreatePtrToInt(val Value, t Type, name string) (v Value) {
1805 cname := C.CString(name)
1806 defer C.free(unsafe.Pointer(cname))
1807 v.C = C.LLVMBuildPtrToInt(b.C, val.C, t.C, cname)
1808 return
1809 }
1810 func (b Builder) CreateIntToPtr(val Value, t Type, name string) (v Value) {
1811 cname := C.CString(name)
1812 defer C.free(unsafe.Pointer(cname))
1813 v.C = C.LLVMBuildIntToPtr(b.C, val.C, t.C, cname)
1814 return
1815 }
1816 func (b Builder) CreateBitCast(val Value, t Type, name string) (v Value) {
1817 cname := C.CString(name)
1818 defer C.free(unsafe.Pointer(cname))
1819 v.C = C.LLVMBuildBitCast(b.C, val.C, t.C, cname)
1820 return
1821 }
1822 func (b Builder) CreateZExtOrBitCast(val Value, t Type, name string) (v Value) {
1823 cname := C.CString(name)
1824 defer C.free(unsafe.Pointer(cname))
1825 v.C = C.LLVMBuildZExtOrBitCast(b.C, val.C, t.C, cname)
1826 return
1827 }
1828 func (b Builder) CreateSExtOrBitCast(val Value, t Type, name string) (v Value) {
1829 cname := C.CString(name)
1830 defer C.free(unsafe.Pointer(cname))
1831 v.C = C.LLVMBuildSExtOrBitCast(b.C, val.C, t.C, cname)
1832 return
1833 }
1834 func (b Builder) CreateTruncOrBitCast(val Value, t Type, name string) (v Value) {
1835 cname := C.CString(name)
1836 defer C.free(unsafe.Pointer(cname))
1837 v.C = C.LLVMBuildTruncOrBitCast(b.C, val.C, t.C, cname)
1838 return
1839 }
1840 func (b Builder) CreateCast(val Value, op Opcode, t Type, name string) (v Value) {
1841 cname := C.CString(name)
1842 defer C.free(unsafe.Pointer(cname))
1843 v.C = C.LLVMBuildCast(b.C, C.LLVMOpcode(op), val.C, t.C, cname)
1844 return
1845 } //
1846 func (b Builder) CreatePointerCast(val Value, t Type, name string) (v Value) {
1847 cname := C.CString(name)
1848 defer C.free(unsafe.Pointer(cname))
1849 v.C = C.LLVMBuildPointerCast(b.C, val.C, t.C, cname)
1850 return
1851 }
1852 func (b Builder) CreateIntCast(val Value, t Type, name string) (v Value) {
1853 cname := C.CString(name)
1854 defer C.free(unsafe.Pointer(cname))
1855 v.C = C.LLVMBuildIntCast(b.C, val.C, t.C, cname)
1856 return
1857 }
1858 func (b Builder) CreateFPCast(val Value, t Type, name string) (v Value) {
1859 cname := C.CString(name)
1860 defer C.free(unsafe.Pointer(cname))
1861 v.C = C.LLVMBuildFPCast(b.C, val.C, t.C, cname)
1862 return
1863 }
1864
1865 // Comparisons
1866 func (b Builder) CreateICmp(pred IntPredicate, lhs, rhs Value, name string) (v Value) {
1867 cname := C.CString(name)
1868 defer C.free(unsafe.Pointer(cname))
1869 v.C = C.LLVMBuildICmp(b.C, C.LLVMIntPredicate(pred), lhs.C, rhs.C, cname)
1870 return
1871 }
1872 func (b Builder) CreateFCmp(pred FloatPredicate, lhs, rhs Value, name string) (v Value) {
1873 cname := C.CString(name)
1874 defer C.free(unsafe.Pointer(cname))
1875 v.C = C.LLVMBuildFCmp(b.C, C.LLVMRealPredicate(pred), lhs.C, rhs.C, cname)
1876 return
1877 }
1878
1879 // Miscellaneous instructions
1880 func (b Builder) CreatePHI(t Type, name string) (v Value) {
1881 cname := C.CString(name)
1882 defer C.free(unsafe.Pointer(cname))
1883 v.C = C.LLVMBuildPhi(b.C, t.C, cname)
1884 return
1885 }
1886 func (b Builder) CreateCall(t Type, fn Value, args []Value, name string) (v Value) {
1887 cname := C.CString(name)
1888 defer C.free(unsafe.Pointer(cname))
1889 ptr, nvals := llvmValueRefs(args)
1890 v.C = C.LLVMBuildCall2(b.C, t.C, fn.C, ptr, nvals, cname)
1891 return
1892 }
1893
1894 func (b Builder) CreateSelect(ifv, thenv, elsev Value, name string) (v Value) {
1895 cname := C.CString(name)
1896 defer C.free(unsafe.Pointer(cname))
1897 v.C = C.LLVMBuildSelect(b.C, ifv.C, thenv.C, elsev.C, cname)
1898 return
1899 }
1900
1901 func (b Builder) CreateVAArg(list Value, t Type, name string) (v Value) {
1902 cname := C.CString(name)
1903 defer C.free(unsafe.Pointer(cname))
1904 v.C = C.LLVMBuildVAArg(b.C, list.C, t.C, cname)
1905 return
1906 }
1907 func (b Builder) CreateExtractElement(vec, i Value, name string) (v Value) {
1908 cname := C.CString(name)
1909 defer C.free(unsafe.Pointer(cname))
1910 v.C = C.LLVMBuildExtractElement(b.C, vec.C, i.C, cname)
1911 return
1912 }
1913 func (b Builder) CreateInsertElement(vec, elt, i Value, name string) (v Value) {
1914 cname := C.CString(name)
1915 defer C.free(unsafe.Pointer(cname))
1916 v.C = C.LLVMBuildInsertElement(b.C, vec.C, elt.C, i.C, cname)
1917 return
1918 }
1919 func (b Builder) CreateShuffleVector(v1, v2, mask Value, name string) (v Value) {
1920 cname := C.CString(name)
1921 defer C.free(unsafe.Pointer(cname))
1922 v.C = C.LLVMBuildShuffleVector(b.C, v1.C, v2.C, mask.C, cname)
1923 return
1924 }
1925 func (b Builder) CreateExtractValue(agg Value, i int, name string) (v Value) {
1926 cname := C.CString(name)
1927 defer C.free(unsafe.Pointer(cname))
1928 v.C = C.LLVMBuildExtractValue(b.C, agg.C, C.unsigned(i), cname)
1929 return
1930 }
1931 func (b Builder) CreateInsertValue(agg, elt Value, i int, name string) (v Value) {
1932 cname := C.CString(name)
1933 defer C.free(unsafe.Pointer(cname))
1934 v.C = C.LLVMBuildInsertValue(b.C, agg.C, elt.C, C.unsigned(i), cname)
1935 return
1936 }
1937
1938 func (b Builder) CreateIsNull(val Value, name string) (v Value) {
1939 cname := C.CString(name)
1940 defer C.free(unsafe.Pointer(cname))
1941 v.C = C.LLVMBuildIsNull(b.C, val.C, cname)
1942 return
1943 }
1944 func (b Builder) CreateIsNotNull(val Value, name string) (v Value) {
1945 cname := C.CString(name)
1946 defer C.free(unsafe.Pointer(cname))
1947 v.C = C.LLVMBuildIsNotNull(b.C, val.C, cname)
1948 return
1949 }
1950 func (b Builder) CreatePtrDiff(t Type, lhs, rhs Value, name string) (v Value) {
1951 cname := C.CString(name)
1952 defer C.free(unsafe.Pointer(cname))
1953 v.C = C.LLVMBuildPtrDiff2(b.C, t.C, lhs.C, rhs.C, cname)
1954 return
1955 }
1956
1957 func (l Value) AddClause(v Value) {
1958 C.LLVMAddClause(l.C, v.C)
1959 }
1960
1961 func (l Value) SetCleanup(cleanup bool) {
1962 C.LLVMSetCleanup(l.C, boolToLLVMBool(cleanup))
1963 }
1964
1965 //-------------------------------------------------------------------------
1966 // llvm.ModuleProvider
1967 //-------------------------------------------------------------------------
1968
1969 // Changes the type of M so it can be passed to FunctionPassManagers and the
1970 // JIT. They take ModuleProviders for historical reasons.
1971 func NewModuleProviderForModule(m Module) (mp ModuleProvider) {
1972 mp.C = C.LLVMCreateModuleProviderForExistingModule(m.C)
1973 return
1974 }
1975
1976 // Destroys the module M.
1977 func (mp ModuleProvider) Dispose() { C.LLVMDisposeModuleProvider(mp.C) }
1978
1979 //-------------------------------------------------------------------------
1980 // llvm.MemoryBuffer
1981 //-------------------------------------------------------------------------
1982
1983 func NewMemoryBufferFromFile(path string) (b MemoryBuffer, err error) {
1984 var cmsg *C.char
1985 cpath := C.CString(path)
1986 defer C.free(unsafe.Pointer(cpath))
1987 fail := C.LLVMCreateMemoryBufferWithContentsOfFile(cpath, &b.C, &cmsg)
1988 if fail != 0 {
1989 b.C = nil
1990 err = errors.New(C.GoString(cmsg))
1991 C.LLVMDisposeMessage(cmsg)
1992 }
1993 return
1994 }
1995
1996 func NewMemoryBufferFromStdin() (b MemoryBuffer, err error) {
1997 var cmsg *C.char
1998 fail := C.LLVMCreateMemoryBufferWithSTDIN(&b.C, &cmsg)
1999 if fail != 0 {
2000 b.C = nil
2001 err = errors.New(C.GoString(cmsg))
2002 C.LLVMDisposeMessage(cmsg)
2003 }
2004 return
2005 }
2006
2007 func (b MemoryBuffer) Bytes() []byte {
2008 cstart := C.LLVMGetBufferStart(b.C)
2009 csize := C.LLVMGetBufferSize(b.C)
2010 return C.GoBytes(unsafe.Pointer(cstart), C.int(csize))
2011 }
2012
2013 func (b MemoryBuffer) Dispose() { C.LLVMDisposeMemoryBuffer(b.C) }
2014
2015 //-------------------------------------------------------------------------
2016 // llvm.PassManager
2017 //-------------------------------------------------------------------------
2018
2019 // Constructs a new whole-module pass pipeline. This type of pipeline is
2020 // suitable for link-time optimization and whole-module transformations.
2021 // See llvm::PassManager::PassManager.
2022 func NewPassManager() (pm PassManager) { pm.C = C.LLVMCreatePassManager(); return }
2023
2024 // Constructs a new function-by-function pass pipeline over the module
2025 // provider. It does not take ownership of the module provider. This type of
2026 // pipeline is suitable for code generation and JIT compilation tasks.
2027 // See llvm::FunctionPassManager::FunctionPassManager.
2028 func NewFunctionPassManagerForModule(m Module) (pm PassManager) {
2029 pm.C = C.LLVMCreateFunctionPassManagerForModule(m.C)
2030 return
2031 }
2032
2033 // Initializes, executes on the provided module, and finalizes all of the
2034 // passes scheduled in the pass manager. Returns 1 if any of the passes
2035 // modified the module, 0 otherwise. See llvm::PassManager::run(Module&).
2036 func (pm PassManager) Run(m Module) bool { return C.LLVMRunPassManager(pm.C, m.C) != 0 }
2037
2038 // Initializes all of the function passes scheduled in the function pass
2039 // manager. Returns 1 if any of the passes modified the module, 0 otherwise.
2040 // See llvm::FunctionPassManager::doInitialization.
2041 func (pm PassManager) InitializeFunc() bool { return C.LLVMInitializeFunctionPassManager(pm.C) != 0 }
2042
2043 // Executes all of the function passes scheduled in the function pass manager
2044 // on the provided function. Returns 1 if any of the passes modified the
2045 // function, false otherwise.
2046 // See llvm::FunctionPassManager::run(Function&).
2047 func (pm PassManager) RunFunc(f Value) bool { return C.LLVMRunFunctionPassManager(pm.C, f.C) != 0 }
2048
2049 // Finalizes all of the function passes scheduled in the function pass
2050 // manager. Returns 1 if any of the passes modified the module, 0 otherwise.
2051 // See llvm::FunctionPassManager::doFinalization.
2052 func (pm PassManager) FinalizeFunc() bool { return C.LLVMFinalizeFunctionPassManager(pm.C) != 0 }
2053
2054 // Frees the memory of a pass pipeline. For function pipelines, does not free
2055 // the module provider.
2056 // See llvm::PassManagerBase::~PassManagerBase.
2057 func (pm PassManager) Dispose() { C.LLVMDisposePassManager(pm.C) }
2058