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