Recommit r310809 with a fix for the spill problem
[llvm-core.git] / bindings / go / llvm / ir.go
blob2220970343071d09b4cc3906db47ad83d2e375a7
1 //===- ir.go - Bindings for ir --------------------------------------------===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file defines bindings for the ir component.
12 //===----------------------------------------------------------------------===//
14 package llvm
17 #include "llvm-c/Core.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 BasicBlock struct {
41 C C.LLVMBasicBlockRef
43 Builder struct {
44 C C.LLVMBuilderRef
46 ModuleProvider struct {
47 C C.LLVMModuleProviderRef
49 MemoryBuffer struct {
50 C C.LLVMMemoryBufferRef
52 PassManager struct {
53 C C.LLVMPassManagerRef
55 Use struct {
56 C C.LLVMUseRef
58 Metadata struct {
59 C C.LLVMMetadataRef
61 Attribute struct {
62 C C.LLVMAttributeRef
64 Opcode C.LLVMOpcode
65 TypeKind C.LLVMTypeKind
66 Linkage C.LLVMLinkage
67 Visibility C.LLVMVisibility
68 CallConv C.LLVMCallConv
69 IntPredicate C.LLVMIntPredicate
70 FloatPredicate C.LLVMRealPredicate
71 LandingPadClause C.LLVMLandingPadClauseTy
74 func (c Context) IsNil() bool { return c.C == nil }
75 func (c Module) IsNil() bool { return c.C == nil }
76 func (c Type) IsNil() bool { return c.C == nil }
77 func (c Value) IsNil() bool { return c.C == nil }
78 func (c BasicBlock) IsNil() bool { return c.C == nil }
79 func (c Builder) IsNil() bool { return c.C == nil }
80 func (c ModuleProvider) IsNil() bool { return c.C == nil }
81 func (c MemoryBuffer) IsNil() bool { return c.C == nil }
82 func (c PassManager) IsNil() bool { return c.C == nil }
83 func (c Use) IsNil() bool { return c.C == nil }
84 func (c Attribute) IsNil() bool { return c.C == nil }
86 // helpers
87 func llvmTypeRefPtr(t *Type) *C.LLVMTypeRef { return (*C.LLVMTypeRef)(unsafe.Pointer(t)) }
88 func llvmValueRefPtr(t *Value) *C.LLVMValueRef { return (*C.LLVMValueRef)(unsafe.Pointer(t)) }
89 func llvmMetadataRefPtr(t *Metadata) *C.LLVMMetadataRef {
90 return (*C.LLVMMetadataRef)(unsafe.Pointer(t))
92 func llvmBasicBlockRefPtr(t *BasicBlock) *C.LLVMBasicBlockRef {
93 return (*C.LLVMBasicBlockRef)(unsafe.Pointer(t))
95 func boolToLLVMBool(b bool) C.LLVMBool {
96 if b {
97 return C.LLVMBool(1)
99 return C.LLVMBool(0)
102 func llvmValueRefs(values []Value) (*C.LLVMValueRef, C.unsigned) {
103 var pt *C.LLVMValueRef
104 ptlen := C.unsigned(len(values))
105 if ptlen > 0 {
106 pt = llvmValueRefPtr(&values[0])
108 return pt, ptlen
111 func llvmMetadataRefs(mds []Metadata) (*C.LLVMMetadataRef, C.unsigned) {
112 var pt *C.LLVMMetadataRef
113 ptlen := C.unsigned(len(mds))
114 if ptlen > 0 {
115 pt = llvmMetadataRefPtr(&mds[0])
117 return pt, ptlen
120 //-------------------------------------------------------------------------
121 // llvm.Opcode
122 //-------------------------------------------------------------------------
124 const (
125 Ret Opcode = C.LLVMRet
126 Br Opcode = C.LLVMBr
127 Switch Opcode = C.LLVMSwitch
128 IndirectBr Opcode = C.LLVMIndirectBr
129 Invoke Opcode = C.LLVMInvoke
130 Unreachable Opcode = C.LLVMUnreachable
132 // Standard Binary Operators
133 Add Opcode = C.LLVMAdd
134 FAdd Opcode = C.LLVMFAdd
135 Sub Opcode = C.LLVMSub
136 FSub Opcode = C.LLVMFSub
137 Mul Opcode = C.LLVMMul
138 FMul Opcode = C.LLVMFMul
139 UDiv Opcode = C.LLVMUDiv
140 SDiv Opcode = C.LLVMSDiv
141 FDiv Opcode = C.LLVMFDiv
142 URem Opcode = C.LLVMURem
143 SRem Opcode = C.LLVMSRem
144 FRem Opcode = C.LLVMFRem
146 // Logical Operators
147 Shl Opcode = C.LLVMShl
148 LShr Opcode = C.LLVMLShr
149 AShr Opcode = C.LLVMAShr
150 And Opcode = C.LLVMAnd
151 Or Opcode = C.LLVMOr
152 Xor Opcode = C.LLVMXor
154 // Memory Operators
155 Alloca Opcode = C.LLVMAlloca
156 Load Opcode = C.LLVMLoad
157 Store Opcode = C.LLVMStore
158 GetElementPtr Opcode = C.LLVMGetElementPtr
160 // Cast Operators
161 Trunc Opcode = C.LLVMTrunc
162 ZExt Opcode = C.LLVMZExt
163 SExt Opcode = C.LLVMSExt
164 FPToUI Opcode = C.LLVMFPToUI
165 FPToSI Opcode = C.LLVMFPToSI
166 UIToFP Opcode = C.LLVMUIToFP
167 SIToFP Opcode = C.LLVMSIToFP
168 FPTrunc Opcode = C.LLVMFPTrunc
169 FPExt Opcode = C.LLVMFPExt
170 PtrToInt Opcode = C.LLVMPtrToInt
171 IntToPtr Opcode = C.LLVMIntToPtr
172 BitCast Opcode = C.LLVMBitCast
174 // Other Operators
175 ICmp Opcode = C.LLVMICmp
176 FCmp Opcode = C.LLVMFCmp
177 PHI Opcode = C.LLVMPHI
178 Call Opcode = C.LLVMCall
179 Select Opcode = C.LLVMSelect
180 // UserOp1
181 // UserOp2
182 VAArg Opcode = C.LLVMVAArg
183 ExtractElement Opcode = C.LLVMExtractElement
184 InsertElement Opcode = C.LLVMInsertElement
185 ShuffleVector Opcode = C.LLVMShuffleVector
186 ExtractValue Opcode = C.LLVMExtractValue
187 InsertValue Opcode = C.LLVMInsertValue
190 //-------------------------------------------------------------------------
191 // llvm.TypeKind
192 //-------------------------------------------------------------------------
194 const (
195 VoidTypeKind TypeKind = C.LLVMVoidTypeKind
196 FloatTypeKind TypeKind = C.LLVMFloatTypeKind
197 DoubleTypeKind TypeKind = C.LLVMDoubleTypeKind
198 X86_FP80TypeKind TypeKind = C.LLVMX86_FP80TypeKind
199 FP128TypeKind TypeKind = C.LLVMFP128TypeKind
200 PPC_FP128TypeKind TypeKind = C.LLVMPPC_FP128TypeKind
201 LabelTypeKind TypeKind = C.LLVMLabelTypeKind
202 IntegerTypeKind TypeKind = C.LLVMIntegerTypeKind
203 FunctionTypeKind TypeKind = C.LLVMFunctionTypeKind
204 StructTypeKind TypeKind = C.LLVMStructTypeKind
205 ArrayTypeKind TypeKind = C.LLVMArrayTypeKind
206 PointerTypeKind TypeKind = C.LLVMPointerTypeKind
207 VectorTypeKind TypeKind = C.LLVMVectorTypeKind
208 MetadataTypeKind TypeKind = C.LLVMMetadataTypeKind
211 //-------------------------------------------------------------------------
212 // llvm.Linkage
213 //-------------------------------------------------------------------------
215 const (
216 ExternalLinkage Linkage = C.LLVMExternalLinkage
217 AvailableExternallyLinkage Linkage = C.LLVMAvailableExternallyLinkage
218 LinkOnceAnyLinkage Linkage = C.LLVMLinkOnceAnyLinkage
219 LinkOnceODRLinkage Linkage = C.LLVMLinkOnceODRLinkage
220 WeakAnyLinkage Linkage = C.LLVMWeakAnyLinkage
221 WeakODRLinkage Linkage = C.LLVMWeakODRLinkage
222 AppendingLinkage Linkage = C.LLVMAppendingLinkage
223 InternalLinkage Linkage = C.LLVMInternalLinkage
224 PrivateLinkage Linkage = C.LLVMPrivateLinkage
225 ExternalWeakLinkage Linkage = C.LLVMExternalWeakLinkage
226 CommonLinkage Linkage = C.LLVMCommonLinkage
229 //-------------------------------------------------------------------------
230 // llvm.Visibility
231 //-------------------------------------------------------------------------
233 const (
234 DefaultVisibility Visibility = C.LLVMDefaultVisibility
235 HiddenVisibility Visibility = C.LLVMHiddenVisibility
236 ProtectedVisibility Visibility = C.LLVMProtectedVisibility
239 //-------------------------------------------------------------------------
240 // llvm.CallConv
241 //-------------------------------------------------------------------------
243 const (
244 CCallConv CallConv = C.LLVMCCallConv
245 FastCallConv CallConv = C.LLVMFastCallConv
246 ColdCallConv CallConv = C.LLVMColdCallConv
247 X86StdcallCallConv CallConv = C.LLVMX86StdcallCallConv
248 X86FastcallCallConv CallConv = C.LLVMX86FastcallCallConv
251 //-------------------------------------------------------------------------
252 // llvm.IntPredicate
253 //-------------------------------------------------------------------------
255 const (
256 IntEQ IntPredicate = C.LLVMIntEQ
257 IntNE IntPredicate = C.LLVMIntNE
258 IntUGT IntPredicate = C.LLVMIntUGT
259 IntUGE IntPredicate = C.LLVMIntUGE
260 IntULT IntPredicate = C.LLVMIntULT
261 IntULE IntPredicate = C.LLVMIntULE
262 IntSGT IntPredicate = C.LLVMIntSGT
263 IntSGE IntPredicate = C.LLVMIntSGE
264 IntSLT IntPredicate = C.LLVMIntSLT
265 IntSLE IntPredicate = C.LLVMIntSLE
268 //-------------------------------------------------------------------------
269 // llvm.FloatPredicate
270 //-------------------------------------------------------------------------
272 const (
273 FloatPredicateFalse FloatPredicate = C.LLVMRealPredicateFalse
274 FloatOEQ FloatPredicate = C.LLVMRealOEQ
275 FloatOGT FloatPredicate = C.LLVMRealOGT
276 FloatOGE FloatPredicate = C.LLVMRealOGE
277 FloatOLT FloatPredicate = C.LLVMRealOLT
278 FloatOLE FloatPredicate = C.LLVMRealOLE
279 FloatONE FloatPredicate = C.LLVMRealONE
280 FloatORD FloatPredicate = C.LLVMRealORD
281 FloatUNO FloatPredicate = C.LLVMRealUNO
282 FloatUEQ FloatPredicate = C.LLVMRealUEQ
283 FloatUGT FloatPredicate = C.LLVMRealUGT
284 FloatUGE FloatPredicate = C.LLVMRealUGE
285 FloatULT FloatPredicate = C.LLVMRealULT
286 FloatULE FloatPredicate = C.LLVMRealULE
287 FloatUNE FloatPredicate = C.LLVMRealUNE
288 FloatPredicateTrue FloatPredicate = C.LLVMRealPredicateTrue
291 //-------------------------------------------------------------------------
292 // llvm.LandingPadClause
293 //-------------------------------------------------------------------------
295 const (
296 LandingPadCatch LandingPadClause = C.LLVMLandingPadCatch
297 LandingPadFilter LandingPadClause = C.LLVMLandingPadFilter
300 //-------------------------------------------------------------------------
301 // llvm.Context
302 //-------------------------------------------------------------------------
304 func NewContext() Context { return Context{C.LLVMContextCreate()} }
305 func GlobalContext() Context { return Context{C.LLVMGetGlobalContext()} }
306 func (c Context) Dispose() { C.LLVMContextDispose(c.C) }
308 func (c Context) MDKindID(name string) (id int) {
309 cname := C.CString(name)
310 defer C.free(unsafe.Pointer(cname))
311 id = int(C.LLVMGetMDKindIDInContext(c.C, cname, C.unsigned(len(name))))
312 return
315 func MDKindID(name string) (id int) {
316 cname := C.CString(name)
317 defer C.free(unsafe.Pointer(cname))
318 id = int(C.LLVMGetMDKindID(cname, C.unsigned(len(name))))
319 return
322 //-------------------------------------------------------------------------
323 // llvm.Attribute
324 //-------------------------------------------------------------------------
326 func AttributeKindID(name string) (id uint) {
327 cname := C.CString(name)
328 defer C.free(unsafe.Pointer(cname))
329 id = uint(C.LLVMGetEnumAttributeKindForName(cname, C.size_t(len(name))))
330 return
333 func (c Context) CreateEnumAttribute(kind uint, val uint64) (a Attribute) {
334 a.C = C.LLVMCreateEnumAttribute(c.C, C.unsigned(kind), C.uint64_t(val))
335 return
338 func (a Attribute) GetEnumKind() (id int) {
339 id = int(C.LLVMGetEnumAttributeKind(a.C))
340 return
343 func (a Attribute) GetEnumValue() (val uint64) {
344 val = uint64(C.LLVMGetEnumAttributeValue(a.C))
345 return
348 func (c Context) CreateStringAttribute(kind string, val string) (a Attribute) {
349 ckind := C.CString(kind)
350 defer C.free(unsafe.Pointer(ckind))
351 cval := C.CString(val)
352 defer C.free(unsafe.Pointer(cval))
353 a.C = C.LLVMCreateStringAttribute(c.C,
354 ckind, C.unsigned(len(kind)),
355 cval, C.unsigned(len(val)))
356 return
359 func (a Attribute) GetStringKind() string {
360 length := C.unsigned(0)
361 ckind := C.LLVMGetStringAttributeKind(a.C, &length)
362 return C.GoStringN(ckind, C.int(length))
365 func (a Attribute) GetStringValue() string {
366 length := C.unsigned(0)
367 ckind := C.LLVMGetStringAttributeValue(a.C, &length)
368 return C.GoStringN(ckind, C.int(length))
371 func (a Attribute) IsEnum() bool {
372 return C.LLVMIsEnumAttribute(a.C) != 0;
375 func (a Attribute) IsString() bool {
376 return C.LLVMIsStringAttribute(a.C) != 0;
379 //-------------------------------------------------------------------------
380 // llvm.Module
381 //-------------------------------------------------------------------------
383 // Create and destroy modules.
384 // See llvm::Module::Module.
385 func NewModule(name string) (m Module) {
386 cname := C.CString(name)
387 defer C.free(unsafe.Pointer(cname))
388 m.C = C.LLVMModuleCreateWithName(cname)
389 return
392 func (c Context) NewModule(name string) (m Module) {
393 cname := C.CString(name)
394 defer C.free(unsafe.Pointer(cname))
395 m.C = C.LLVMModuleCreateWithNameInContext(cname, c.C)
396 return
399 // See llvm::Module::~Module
400 func (m Module) Dispose() { C.LLVMDisposeModule(m.C) }
402 // Data layout. See Module::getDataLayout.
403 func (m Module) DataLayout() string {
404 clayout := C.LLVMGetDataLayout(m.C)
405 return C.GoString(clayout)
408 func (m Module) SetDataLayout(layout string) {
409 clayout := C.CString(layout)
410 defer C.free(unsafe.Pointer(clayout))
411 C.LLVMSetDataLayout(m.C, clayout)
414 // Target triple. See Module::getTargetTriple.
415 func (m Module) Target() string {
416 ctarget := C.LLVMGetTarget(m.C)
417 return C.GoString(ctarget)
419 func (m Module) SetTarget(target string) {
420 ctarget := C.CString(target)
421 defer C.free(unsafe.Pointer(ctarget))
422 C.LLVMSetTarget(m.C, ctarget)
425 func (m Module) GetTypeByName(name string) (t Type) {
426 cname := C.CString(name)
427 defer C.free(unsafe.Pointer(cname))
428 t.C = C.LLVMGetTypeByName(m.C, cname)
429 return
432 // See Module::dump.
433 func (m Module) Dump() {
434 C.LLVMDumpModule(m.C)
437 func (m Module) String() string {
438 cir := C.LLVMPrintModuleToString(m.C)
439 defer C.free(unsafe.Pointer(cir))
440 ir := C.GoString(cir)
441 return ir
444 // See Module::setModuleInlineAsm.
445 func (m Module) SetInlineAsm(asm string) {
446 casm := C.CString(asm)
447 defer C.free(unsafe.Pointer(casm))
448 C.LLVMSetModuleInlineAsm(m.C, casm)
451 func (m Module) AddNamedMetadataOperand(name string, operand Metadata) {
452 cname := C.CString(name)
453 defer C.free(unsafe.Pointer(cname))
454 C.LLVMAddNamedMetadataOperand2(m.C, cname, operand.C)
457 func (m Module) Context() (c Context) {
458 c.C = C.LLVMGetModuleContext(m.C)
459 return
462 //-------------------------------------------------------------------------
463 // llvm.Type
464 //-------------------------------------------------------------------------
466 // LLVM types conform to the following hierarchy:
468 // types:
469 // integer type
470 // real type
471 // function type
472 // sequence types:
473 // array type
474 // pointer type
475 // vector type
476 // void type
477 // label type
478 // opaque type
480 // See llvm::LLVMTypeKind::getTypeID.
481 func (t Type) TypeKind() TypeKind { return TypeKind(C.LLVMGetTypeKind(t.C)) }
483 // See llvm::LLVMType::getContext.
484 func (t Type) Context() (c Context) {
485 c.C = C.LLVMGetTypeContext(t.C)
486 return
489 // Operations on integer types
490 func (c Context) Int1Type() (t Type) { t.C = C.LLVMInt1TypeInContext(c.C); return }
491 func (c Context) Int8Type() (t Type) { t.C = C.LLVMInt8TypeInContext(c.C); return }
492 func (c Context) Int16Type() (t Type) { t.C = C.LLVMInt16TypeInContext(c.C); return }
493 func (c Context) Int32Type() (t Type) { t.C = C.LLVMInt32TypeInContext(c.C); return }
494 func (c Context) Int64Type() (t Type) { t.C = C.LLVMInt64TypeInContext(c.C); return }
495 func (c Context) IntType(numbits int) (t Type) {
496 t.C = C.LLVMIntTypeInContext(c.C, C.unsigned(numbits))
497 return
500 func Int1Type() (t Type) { t.C = C.LLVMInt1Type(); return }
501 func Int8Type() (t Type) { t.C = C.LLVMInt8Type(); return }
502 func Int16Type() (t Type) { t.C = C.LLVMInt16Type(); return }
503 func Int32Type() (t Type) { t.C = C.LLVMInt32Type(); return }
504 func Int64Type() (t Type) { t.C = C.LLVMInt64Type(); return }
506 func IntType(numbits int) (t Type) {
507 t.C = C.LLVMIntType(C.unsigned(numbits))
508 return
511 func (t Type) IntTypeWidth() int {
512 return int(C.LLVMGetIntTypeWidth(t.C))
515 // Operations on real types
516 func (c Context) FloatType() (t Type) { t.C = C.LLVMFloatTypeInContext(c.C); return }
517 func (c Context) DoubleType() (t Type) { t.C = C.LLVMDoubleTypeInContext(c.C); return }
518 func (c Context) X86FP80Type() (t Type) { t.C = C.LLVMX86FP80TypeInContext(c.C); return }
519 func (c Context) FP128Type() (t Type) { t.C = C.LLVMFP128TypeInContext(c.C); return }
520 func (c Context) PPCFP128Type() (t Type) { t.C = C.LLVMPPCFP128TypeInContext(c.C); return }
522 func FloatType() (t Type) { t.C = C.LLVMFloatType(); return }
523 func DoubleType() (t Type) { t.C = C.LLVMDoubleType(); return }
524 func X86FP80Type() (t Type) { t.C = C.LLVMX86FP80Type(); return }
525 func FP128Type() (t Type) { t.C = C.LLVMFP128Type(); return }
526 func PPCFP128Type() (t Type) { t.C = C.LLVMPPCFP128Type(); return }
528 // Operations on function types
529 func FunctionType(returnType Type, paramTypes []Type, isVarArg bool) (t Type) {
530 var pt *C.LLVMTypeRef
531 var ptlen C.unsigned
532 if len(paramTypes) > 0 {
533 pt = llvmTypeRefPtr(&paramTypes[0])
534 ptlen = C.unsigned(len(paramTypes))
536 t.C = C.LLVMFunctionType(returnType.C,
538 ptlen,
539 boolToLLVMBool(isVarArg))
540 return
543 func (t Type) IsFunctionVarArg() bool { return C.LLVMIsFunctionVarArg(t.C) != 0 }
544 func (t Type) ReturnType() (rt Type) { rt.C = C.LLVMGetReturnType(t.C); return }
545 func (t Type) ParamTypesCount() int { return int(C.LLVMCountParamTypes(t.C)) }
546 func (t Type) ParamTypes() []Type {
547 count := t.ParamTypesCount()
548 if count > 0 {
549 out := make([]Type, count)
550 C.LLVMGetParamTypes(t.C, llvmTypeRefPtr(&out[0]))
551 return out
553 return nil
556 // Operations on struct types
557 func (c Context) StructType(elementTypes []Type, packed bool) (t Type) {
558 var pt *C.LLVMTypeRef
559 var ptlen C.unsigned
560 if len(elementTypes) > 0 {
561 pt = llvmTypeRefPtr(&elementTypes[0])
562 ptlen = C.unsigned(len(elementTypes))
564 t.C = C.LLVMStructTypeInContext(c.C,
566 ptlen,
567 boolToLLVMBool(packed))
568 return
571 func StructType(elementTypes []Type, packed bool) (t Type) {
572 var pt *C.LLVMTypeRef
573 var ptlen C.unsigned
574 if len(elementTypes) > 0 {
575 pt = llvmTypeRefPtr(&elementTypes[0])
576 ptlen = C.unsigned(len(elementTypes))
578 t.C = C.LLVMStructType(pt, ptlen, boolToLLVMBool(packed))
579 return
582 func (c Context) StructCreateNamed(name string) (t Type) {
583 cname := C.CString(name)
584 defer C.free(unsafe.Pointer(cname))
585 t.C = C.LLVMStructCreateNamed(c.C, cname)
586 return
589 func (t Type) StructName() string {
590 return C.GoString(C.LLVMGetStructName(t.C))
593 func (t Type) StructSetBody(elementTypes []Type, packed bool) {
594 var pt *C.LLVMTypeRef
595 var ptlen C.unsigned
596 if len(elementTypes) > 0 {
597 pt = llvmTypeRefPtr(&elementTypes[0])
598 ptlen = C.unsigned(len(elementTypes))
600 C.LLVMStructSetBody(t.C, pt, ptlen, boolToLLVMBool(packed))
603 func (t Type) IsStructPacked() bool { return C.LLVMIsPackedStruct(t.C) != 0 }
604 func (t Type) StructElementTypesCount() int { return int(C.LLVMCountStructElementTypes(t.C)) }
605 func (t Type) StructElementTypes() []Type {
606 out := make([]Type, t.StructElementTypesCount())
607 if len(out) > 0 {
608 C.LLVMGetStructElementTypes(t.C, llvmTypeRefPtr(&out[0]))
610 return out
613 // Operations on array, pointer, and vector types (sequence types)
614 func (t Type) Subtypes() (ret []Type) {
615 ret = make([]Type, C.LLVMGetNumContainedTypes(t.C))
616 C.LLVMGetSubtypes(t.C, llvmTypeRefPtr(&ret[0]))
617 return
620 func ArrayType(elementType Type, elementCount int) (t Type) {
621 t.C = C.LLVMArrayType(elementType.C, C.unsigned(elementCount))
622 return
624 func PointerType(elementType Type, addressSpace int) (t Type) {
625 t.C = C.LLVMPointerType(elementType.C, C.unsigned(addressSpace))
626 return
628 func VectorType(elementType Type, elementCount int) (t Type) {
629 t.C = C.LLVMVectorType(elementType.C, C.unsigned(elementCount))
630 return
633 func (t Type) ElementType() (rt Type) { rt.C = C.LLVMGetElementType(t.C); return }
634 func (t Type) ArrayLength() int { return int(C.LLVMGetArrayLength(t.C)) }
635 func (t Type) PointerAddressSpace() int { return int(C.LLVMGetPointerAddressSpace(t.C)) }
636 func (t Type) VectorSize() int { return int(C.LLVMGetVectorSize(t.C)) }
638 // Operations on other types
639 func (c Context) VoidType() (t Type) { t.C = C.LLVMVoidTypeInContext(c.C); return }
640 func (c Context) LabelType() (t Type) { t.C = C.LLVMLabelTypeInContext(c.C); return }
642 func VoidType() (t Type) { t.C = C.LLVMVoidType(); return }
643 func LabelType() (t Type) { t.C = C.LLVMLabelType(); return }
645 //-------------------------------------------------------------------------
646 // llvm.Value
647 //-------------------------------------------------------------------------
649 // Operations on all values
650 func (v Value) Type() (t Type) { t.C = C.LLVMTypeOf(v.C); return }
651 func (v Value) Name() string { return C.GoString(C.LLVMGetValueName(v.C)) }
652 func (v Value) SetName(name string) {
653 cname := C.CString(name)
654 defer C.free(unsafe.Pointer(cname))
655 C.LLVMSetValueName(v.C, cname)
657 func (v Value) Dump() { C.LLVMDumpValue(v.C) }
658 func (v Value) ReplaceAllUsesWith(nv Value) { C.LLVMReplaceAllUsesWith(v.C, nv.C) }
659 func (v Value) HasMetadata() bool { return C.LLVMHasMetadata(v.C) != 0 }
660 func (v Value) Metadata(kind int) (rv Value) {
661 rv.C = C.LLVMGetMetadata(v.C, C.unsigned(kind))
662 return
664 func (v Value) SetMetadata(kind int, node Metadata) {
665 C.LLVMSetMetadata2(v.C, C.unsigned(kind), node.C)
668 // Conversion functions.
669 // Return the input value if it is an instance of the specified class, otherwise NULL.
670 // See llvm::dyn_cast_or_null<>.
671 func (v Value) IsAArgument() (rv Value) { rv.C = C.LLVMIsAArgument(v.C); return }
672 func (v Value) IsABasicBlock() (rv Value) { rv.C = C.LLVMIsABasicBlock(v.C); return }
673 func (v Value) IsAInlineAsm() (rv Value) { rv.C = C.LLVMIsAInlineAsm(v.C); return }
674 func (v Value) IsAUser() (rv Value) { rv.C = C.LLVMIsAUser(v.C); return }
675 func (v Value) IsAConstant() (rv Value) { rv.C = C.LLVMIsAConstant(v.C); return }
676 func (v Value) IsAConstantAggregateZero() (rv Value) {
677 rv.C = C.LLVMIsAConstantAggregateZero(v.C)
678 return
680 func (v Value) IsAConstantArray() (rv Value) { rv.C = C.LLVMIsAConstantArray(v.C); return }
681 func (v Value) IsAConstantExpr() (rv Value) { rv.C = C.LLVMIsAConstantExpr(v.C); return }
682 func (v Value) IsAConstantFP() (rv Value) { rv.C = C.LLVMIsAConstantFP(v.C); return }
683 func (v Value) IsAConstantInt() (rv Value) { rv.C = C.LLVMIsAConstantInt(v.C); return }
684 func (v Value) IsAConstantPointerNull() (rv Value) { rv.C = C.LLVMIsAConstantPointerNull(v.C); return }
685 func (v Value) IsAConstantStruct() (rv Value) { rv.C = C.LLVMIsAConstantStruct(v.C); return }
686 func (v Value) IsAConstantVector() (rv Value) { rv.C = C.LLVMIsAConstantVector(v.C); return }
687 func (v Value) IsAGlobalValue() (rv Value) { rv.C = C.LLVMIsAGlobalValue(v.C); return }
688 func (v Value) IsAFunction() (rv Value) { rv.C = C.LLVMIsAFunction(v.C); return }
689 func (v Value) IsAGlobalAlias() (rv Value) { rv.C = C.LLVMIsAGlobalAlias(v.C); return }
690 func (v Value) IsAGlobalVariable() (rv Value) { rv.C = C.LLVMIsAGlobalVariable(v.C); return }
691 func (v Value) IsAUndefValue() (rv Value) { rv.C = C.LLVMIsAUndefValue(v.C); return }
692 func (v Value) IsAInstruction() (rv Value) { rv.C = C.LLVMIsAInstruction(v.C); return }
693 func (v Value) IsABinaryOperator() (rv Value) { rv.C = C.LLVMIsABinaryOperator(v.C); return }
694 func (v Value) IsACallInst() (rv Value) { rv.C = C.LLVMIsACallInst(v.C); return }
695 func (v Value) IsAIntrinsicInst() (rv Value) { rv.C = C.LLVMIsAIntrinsicInst(v.C); return }
696 func (v Value) IsADbgInfoIntrinsic() (rv Value) { rv.C = C.LLVMIsADbgInfoIntrinsic(v.C); return }
697 func (v Value) IsADbgDeclareInst() (rv Value) { rv.C = C.LLVMIsADbgDeclareInst(v.C); return }
698 func (v Value) IsAMemIntrinsic() (rv Value) { rv.C = C.LLVMIsAMemIntrinsic(v.C); return }
699 func (v Value) IsAMemCpyInst() (rv Value) { rv.C = C.LLVMIsAMemCpyInst(v.C); return }
700 func (v Value) IsAMemMoveInst() (rv Value) { rv.C = C.LLVMIsAMemMoveInst(v.C); return }
701 func (v Value) IsAMemSetInst() (rv Value) { rv.C = C.LLVMIsAMemSetInst(v.C); return }
702 func (v Value) IsACmpInst() (rv Value) { rv.C = C.LLVMIsACmpInst(v.C); return }
703 func (v Value) IsAFCmpInst() (rv Value) { rv.C = C.LLVMIsAFCmpInst(v.C); return }
704 func (v Value) IsAICmpInst() (rv Value) { rv.C = C.LLVMIsAICmpInst(v.C); return }
705 func (v Value) IsAExtractElementInst() (rv Value) { rv.C = C.LLVMIsAExtractElementInst(v.C); return }
706 func (v Value) IsAGetElementPtrInst() (rv Value) { rv.C = C.LLVMIsAGetElementPtrInst(v.C); return }
707 func (v Value) IsAInsertElementInst() (rv Value) { rv.C = C.LLVMIsAInsertElementInst(v.C); return }
708 func (v Value) IsAInsertValueInst() (rv Value) { rv.C = C.LLVMIsAInsertValueInst(v.C); return }
709 func (v Value) IsAPHINode() (rv Value) { rv.C = C.LLVMIsAPHINode(v.C); return }
710 func (v Value) IsASelectInst() (rv Value) { rv.C = C.LLVMIsASelectInst(v.C); return }
711 func (v Value) IsAShuffleVectorInst() (rv Value) { rv.C = C.LLVMIsAShuffleVectorInst(v.C); return }
712 func (v Value) IsAStoreInst() (rv Value) { rv.C = C.LLVMIsAStoreInst(v.C); return }
713 func (v Value) IsATerminatorInst() (rv Value) { rv.C = C.LLVMIsATerminatorInst(v.C); return }
714 func (v Value) IsABranchInst() (rv Value) { rv.C = C.LLVMIsABranchInst(v.C); return }
715 func (v Value) IsAInvokeInst() (rv Value) { rv.C = C.LLVMIsAInvokeInst(v.C); return }
716 func (v Value) IsAReturnInst() (rv Value) { rv.C = C.LLVMIsAReturnInst(v.C); return }
717 func (v Value) IsASwitchInst() (rv Value) { rv.C = C.LLVMIsASwitchInst(v.C); return }
718 func (v Value) IsAUnreachableInst() (rv Value) { rv.C = C.LLVMIsAUnreachableInst(v.C); return }
719 func (v Value) IsAUnaryInstruction() (rv Value) { rv.C = C.LLVMIsAUnaryInstruction(v.C); return }
720 func (v Value) IsAAllocaInst() (rv Value) { rv.C = C.LLVMIsAAllocaInst(v.C); return }
721 func (v Value) IsACastInst() (rv Value) { rv.C = C.LLVMIsACastInst(v.C); return }
722 func (v Value) IsABitCastInst() (rv Value) { rv.C = C.LLVMIsABitCastInst(v.C); return }
723 func (v Value) IsAFPExtInst() (rv Value) { rv.C = C.LLVMIsAFPExtInst(v.C); return }
724 func (v Value) IsAFPToSIInst() (rv Value) { rv.C = C.LLVMIsAFPToSIInst(v.C); return }
725 func (v Value) IsAFPToUIInst() (rv Value) { rv.C = C.LLVMIsAFPToUIInst(v.C); return }
726 func (v Value) IsAFPTruncInst() (rv Value) { rv.C = C.LLVMIsAFPTruncInst(v.C); return }
727 func (v Value) IsAIntToPtrInst() (rv Value) { rv.C = C.LLVMIsAIntToPtrInst(v.C); return }
728 func (v Value) IsAPtrToIntInst() (rv Value) { rv.C = C.LLVMIsAPtrToIntInst(v.C); return }
729 func (v Value) IsASExtInst() (rv Value) { rv.C = C.LLVMIsASExtInst(v.C); return }
730 func (v Value) IsASIToFPInst() (rv Value) { rv.C = C.LLVMIsASIToFPInst(v.C); return }
731 func (v Value) IsATruncInst() (rv Value) { rv.C = C.LLVMIsATruncInst(v.C); return }
732 func (v Value) IsAUIToFPInst() (rv Value) { rv.C = C.LLVMIsAUIToFPInst(v.C); return }
733 func (v Value) IsAZExtInst() (rv Value) { rv.C = C.LLVMIsAZExtInst(v.C); return }
734 func (v Value) IsAExtractValueInst() (rv Value) { rv.C = C.LLVMIsAExtractValueInst(v.C); return }
735 func (v Value) IsALoadInst() (rv Value) { rv.C = C.LLVMIsALoadInst(v.C); return }
736 func (v Value) IsAVAArgInst() (rv Value) { rv.C = C.LLVMIsAVAArgInst(v.C); return }
738 // Operations on Uses
739 func (v Value) FirstUse() (u Use) { u.C = C.LLVMGetFirstUse(v.C); return }
740 func (u Use) NextUse() (ru Use) { ru.C = C.LLVMGetNextUse(u.C); return }
741 func (u Use) User() (v Value) { v.C = C.LLVMGetUser(u.C); return }
742 func (u Use) UsedValue() (v Value) { v.C = C.LLVMGetUsedValue(u.C); return }
744 // Operations on Users
745 func (v Value) Operand(i int) (rv Value) { rv.C = C.LLVMGetOperand(v.C, C.unsigned(i)); return }
746 func (v Value) SetOperand(i int, op Value) { C.LLVMSetOperand(v.C, C.unsigned(i), op.C) }
747 func (v Value) OperandsCount() int { return int(C.LLVMGetNumOperands(v.C)) }
749 // Operations on constants of any type
750 func ConstNull(t Type) (v Value) { v.C = C.LLVMConstNull(t.C); return }
751 func ConstAllOnes(t Type) (v Value) { v.C = C.LLVMConstAllOnes(t.C); return }
752 func Undef(t Type) (v Value) { v.C = C.LLVMGetUndef(t.C); return }
753 func (v Value) IsConstant() bool { return C.LLVMIsConstant(v.C) != 0 }
754 func (v Value) IsNull() bool { return C.LLVMIsNull(v.C) != 0 }
755 func (v Value) IsUndef() bool { return C.LLVMIsUndef(v.C) != 0 }
756 func ConstPointerNull(t Type) (v Value) { v.C = C.LLVMConstPointerNull(t.C); return }
758 // Operations on metadata
759 func (c Context) MDString(str string) (md Metadata) {
760 cstr := C.CString(str)
761 defer C.free(unsafe.Pointer(cstr))
762 md.C = C.LLVMMDString2(c.C, cstr, C.unsigned(len(str)))
763 return
765 func (c Context) MDNode(mds []Metadata) (md Metadata) {
766 ptr, nvals := llvmMetadataRefs(mds)
767 md.C = C.LLVMMDNode2(c.C, ptr, nvals)
768 return
770 func (c Context) TemporaryMDNode(mds []Metadata) (md Metadata) {
771 ptr, nvals := llvmMetadataRefs(mds)
772 md.C = C.LLVMTemporaryMDNode(c.C, ptr, nvals)
773 return
775 func (v Value) ConstantAsMetadata() (md Metadata) {
776 md.C = C.LLVMConstantAsMetadata(v.C)
777 return
780 // Operations on scalar constants
781 func ConstInt(t Type, n uint64, signExtend bool) (v Value) {
782 v.C = C.LLVMConstInt(t.C,
783 C.ulonglong(n),
784 boolToLLVMBool(signExtend))
785 return
787 func ConstIntFromString(t Type, str string, radix int) (v Value) {
788 cstr := C.CString(str)
789 defer C.free(unsafe.Pointer(cstr))
790 v.C = C.LLVMConstIntOfString(t.C, cstr, C.uint8_t(radix))
791 return
793 func ConstFloat(t Type, n float64) (v Value) {
794 v.C = C.LLVMConstReal(t.C, C.double(n))
795 return
797 func ConstFloatFromString(t Type, str string) (v Value) {
798 cstr := C.CString(str)
799 defer C.free(unsafe.Pointer(cstr))
800 v.C = C.LLVMConstRealOfString(t.C, cstr)
801 return
804 func (v Value) ZExtValue() uint64 { return uint64(C.LLVMConstIntGetZExtValue(v.C)) }
805 func (v Value) SExtValue() int64 { return int64(C.LLVMConstIntGetSExtValue(v.C)) }
807 // Operations on composite constants
808 func (c Context) ConstString(str string, addnull bool) (v Value) {
809 cstr := C.CString(str)
810 defer C.free(unsafe.Pointer(cstr))
811 v.C = C.LLVMConstStringInContext(c.C, cstr,
812 C.unsigned(len(str)), boolToLLVMBool(!addnull))
813 return
815 func (c Context) ConstStruct(constVals []Value, packed bool) (v Value) {
816 ptr, nvals := llvmValueRefs(constVals)
817 v.C = C.LLVMConstStructInContext(c.C, ptr, nvals,
818 boolToLLVMBool(packed))
819 return
821 func ConstNamedStruct(t Type, constVals []Value) (v Value) {
822 ptr, nvals := llvmValueRefs(constVals)
823 v.C = C.LLVMConstNamedStruct(t.C, ptr, nvals)
824 return
826 func ConstString(str string, addnull bool) (v Value) {
827 cstr := C.CString(str)
828 defer C.free(unsafe.Pointer(cstr))
829 v.C = C.LLVMConstString(cstr,
830 C.unsigned(len(str)), boolToLLVMBool(!addnull))
831 return
833 func ConstArray(t Type, constVals []Value) (v Value) {
834 ptr, nvals := llvmValueRefs(constVals)
835 v.C = C.LLVMConstArray(t.C, ptr, nvals)
836 return
838 func ConstStruct(constVals []Value, packed bool) (v Value) {
839 ptr, nvals := llvmValueRefs(constVals)
840 v.C = C.LLVMConstStruct(ptr, nvals, boolToLLVMBool(packed))
841 return
843 func ConstVector(scalarConstVals []Value, packed bool) (v Value) {
844 ptr, nvals := llvmValueRefs(scalarConstVals)
845 v.C = C.LLVMConstVector(ptr, nvals)
846 return
849 // Constant expressions
850 func (v Value) Opcode() Opcode { return Opcode(C.LLVMGetConstOpcode(v.C)) }
851 func (v Value) InstructionOpcode() Opcode { return Opcode(C.LLVMGetInstructionOpcode(v.C)) }
852 func AlignOf(t Type) (v Value) { v.C = C.LLVMAlignOf(t.C); return }
853 func SizeOf(t Type) (v Value) { v.C = C.LLVMSizeOf(t.C); return }
854 func ConstNeg(v Value) (rv Value) { rv.C = C.LLVMConstNeg(v.C); return }
855 func ConstNSWNeg(v Value) (rv Value) { rv.C = C.LLVMConstNSWNeg(v.C); return }
856 func ConstNUWNeg(v Value) (rv Value) { rv.C = C.LLVMConstNUWNeg(v.C); return }
857 func ConstFNeg(v Value) (rv Value) { rv.C = C.LLVMConstFNeg(v.C); return }
858 func ConstNot(v Value) (rv Value) { rv.C = C.LLVMConstNot(v.C); return }
859 func ConstAdd(lhs, rhs Value) (v Value) { v.C = C.LLVMConstAdd(lhs.C, rhs.C); return }
860 func ConstNSWAdd(lhs, rhs Value) (v Value) { v.C = C.LLVMConstNSWAdd(lhs.C, rhs.C); return }
861 func ConstNUWAdd(lhs, rhs Value) (v Value) { v.C = C.LLVMConstNUWAdd(lhs.C, rhs.C); return }
862 func ConstFAdd(lhs, rhs Value) (v Value) { v.C = C.LLVMConstFAdd(lhs.C, rhs.C); return }
863 func ConstSub(lhs, rhs Value) (v Value) { v.C = C.LLVMConstSub(lhs.C, rhs.C); return }
864 func ConstNSWSub(lhs, rhs Value) (v Value) { v.C = C.LLVMConstNSWSub(lhs.C, rhs.C); return }
865 func ConstNUWSub(lhs, rhs Value) (v Value) { v.C = C.LLVMConstNUWSub(lhs.C, rhs.C); return }
866 func ConstFSub(lhs, rhs Value) (v Value) { v.C = C.LLVMConstFSub(lhs.C, rhs.C); return }
867 func ConstMul(lhs, rhs Value) (v Value) { v.C = C.LLVMConstMul(lhs.C, rhs.C); return }
868 func ConstNSWMul(lhs, rhs Value) (v Value) { v.C = C.LLVMConstNSWMul(lhs.C, rhs.C); return }
869 func ConstNUWMul(lhs, rhs Value) (v Value) { v.C = C.LLVMConstNUWMul(lhs.C, rhs.C); return }
870 func ConstFMul(lhs, rhs Value) (v Value) { v.C = C.LLVMConstFMul(lhs.C, rhs.C); return }
871 func ConstUDiv(lhs, rhs Value) (v Value) { v.C = C.LLVMConstUDiv(lhs.C, rhs.C); return }
872 func ConstSDiv(lhs, rhs Value) (v Value) { v.C = C.LLVMConstSDiv(lhs.C, rhs.C); return }
873 func ConstExactSDiv(lhs, rhs Value) (v Value) { v.C = C.LLVMConstExactSDiv(lhs.C, rhs.C); return }
874 func ConstFDiv(lhs, rhs Value) (v Value) { v.C = C.LLVMConstFDiv(lhs.C, rhs.C); return }
875 func ConstURem(lhs, rhs Value) (v Value) { v.C = C.LLVMConstURem(lhs.C, rhs.C); return }
876 func ConstSRem(lhs, rhs Value) (v Value) { v.C = C.LLVMConstSRem(lhs.C, rhs.C); return }
877 func ConstFRem(lhs, rhs Value) (v Value) { v.C = C.LLVMConstFRem(lhs.C, rhs.C); return }
878 func ConstAnd(lhs, rhs Value) (v Value) { v.C = C.LLVMConstAnd(lhs.C, rhs.C); return }
879 func ConstOr(lhs, rhs Value) (v Value) { v.C = C.LLVMConstOr(lhs.C, rhs.C); return }
880 func ConstXor(lhs, rhs Value) (v Value) { v.C = C.LLVMConstXor(lhs.C, rhs.C); return }
882 func ConstICmp(pred IntPredicate, lhs, rhs Value) (v Value) {
883 v.C = C.LLVMConstICmp(C.LLVMIntPredicate(pred), lhs.C, rhs.C)
884 return
886 func ConstFCmp(pred FloatPredicate, lhs, rhs Value) (v Value) {
887 v.C = C.LLVMConstFCmp(C.LLVMRealPredicate(pred), lhs.C, rhs.C)
888 return
891 func ConstShl(lhs, rhs Value) (v Value) { v.C = C.LLVMConstShl(lhs.C, rhs.C); return }
892 func ConstLShr(lhs, rhs Value) (v Value) { v.C = C.LLVMConstLShr(lhs.C, rhs.C); return }
893 func ConstAShr(lhs, rhs Value) (v Value) { v.C = C.LLVMConstAShr(lhs.C, rhs.C); return }
895 func ConstGEP(v Value, indices []Value) (rv Value) {
896 ptr, nvals := llvmValueRefs(indices)
897 rv.C = C.LLVMConstGEP(v.C, ptr, nvals)
898 return
900 func ConstInBoundsGEP(v Value, indices []Value) (rv Value) {
901 ptr, nvals := llvmValueRefs(indices)
902 rv.C = C.LLVMConstInBoundsGEP(v.C, ptr, nvals)
903 return
905 func ConstTrunc(v Value, t Type) (rv Value) { rv.C = C.LLVMConstTrunc(v.C, t.C); return }
906 func ConstSExt(v Value, t Type) (rv Value) { rv.C = C.LLVMConstSExt(v.C, t.C); return }
907 func ConstZExt(v Value, t Type) (rv Value) { rv.C = C.LLVMConstZExt(v.C, t.C); return }
908 func ConstFPTrunc(v Value, t Type) (rv Value) { rv.C = C.LLVMConstFPTrunc(v.C, t.C); return }
909 func ConstFPExt(v Value, t Type) (rv Value) { rv.C = C.LLVMConstFPExt(v.C, t.C); return }
910 func ConstUIToFP(v Value, t Type) (rv Value) { rv.C = C.LLVMConstUIToFP(v.C, t.C); return }
911 func ConstSIToFP(v Value, t Type) (rv Value) { rv.C = C.LLVMConstSIToFP(v.C, t.C); return }
912 func ConstFPToUI(v Value, t Type) (rv Value) { rv.C = C.LLVMConstFPToUI(v.C, t.C); return }
913 func ConstFPToSI(v Value, t Type) (rv Value) { rv.C = C.LLVMConstFPToSI(v.C, t.C); return }
914 func ConstPtrToInt(v Value, t Type) (rv Value) { rv.C = C.LLVMConstPtrToInt(v.C, t.C); return }
915 func ConstIntToPtr(v Value, t Type) (rv Value) { rv.C = C.LLVMConstIntToPtr(v.C, t.C); return }
916 func ConstBitCast(v Value, t Type) (rv Value) { rv.C = C.LLVMConstBitCast(v.C, t.C); return }
917 func ConstZExtOrBitCast(v Value, t Type) (rv Value) { rv.C = C.LLVMConstZExtOrBitCast(v.C, t.C); return }
918 func ConstSExtOrBitCast(v Value, t Type) (rv Value) { rv.C = C.LLVMConstSExtOrBitCast(v.C, t.C); return }
919 func ConstTruncOrBitCast(v Value, t Type) (rv Value) {
920 rv.C = C.LLVMConstTruncOrBitCast(v.C, t.C)
921 return
923 func ConstPointerCast(v Value, t Type) (rv Value) { rv.C = C.LLVMConstPointerCast(v.C, t.C); return }
924 func ConstIntCast(v Value, t Type, signed bool) (rv Value) {
925 rv.C = C.LLVMConstIntCast(v.C, t.C, boolToLLVMBool(signed))
926 return
928 func ConstFPCast(v Value, t Type) (rv Value) { rv.C = C.LLVMConstFPCast(v.C, t.C); return }
929 func ConstSelect(cond, iftrue, iffalse Value) (rv Value) {
930 rv.C = C.LLVMConstSelect(cond.C, iftrue.C, iffalse.C)
931 return
933 func ConstExtractElement(vec, i Value) (rv Value) {
934 rv.C = C.LLVMConstExtractElement(vec.C, i.C)
935 return
937 func ConstInsertElement(vec, elem, i Value) (rv Value) {
938 rv.C = C.LLVMConstInsertElement(vec.C, elem.C, i.C)
939 return
941 func ConstShuffleVector(veca, vecb, mask Value) (rv Value) {
942 rv.C = C.LLVMConstShuffleVector(veca.C, vecb.C, mask.C)
943 return
946 //TODO
947 //LLVMValueRef LLVMConstExtractValue(LLVMValueRef AggConstant, unsigned *IdxList,
948 // unsigned NumIdx);
950 func ConstExtractValue(agg Value, indices []uint32) (rv Value) {
951 n := len(indices)
952 if n == 0 {
953 panic("one or more indices are required")
955 ptr := (*C.unsigned)(&indices[0])
956 rv.C = C.LLVMConstExtractValue(agg.C, ptr, C.unsigned(n))
957 return
960 func ConstInsertValue(agg, val Value, indices []uint32) (rv Value) {
961 n := len(indices)
962 if n == 0 {
963 panic("one or more indices are required")
965 ptr := (*C.unsigned)(&indices[0])
966 rv.C = C.LLVMConstInsertValue(agg.C, val.C, ptr, C.unsigned(n))
967 return
970 func BlockAddress(f Value, bb BasicBlock) (v Value) {
971 v.C = C.LLVMBlockAddress(f.C, bb.C)
972 return
975 // Operations on global variables, functions, and aliases (globals)
976 func (v Value) GlobalParent() (m Module) { m.C = C.LLVMGetGlobalParent(v.C); return }
977 func (v Value) IsDeclaration() bool { return C.LLVMIsDeclaration(v.C) != 0 }
978 func (v Value) Linkage() Linkage { return Linkage(C.LLVMGetLinkage(v.C)) }
979 func (v Value) SetLinkage(l Linkage) { C.LLVMSetLinkage(v.C, C.LLVMLinkage(l)) }
980 func (v Value) Section() string { return C.GoString(C.LLVMGetSection(v.C)) }
981 func (v Value) SetSection(str string) {
982 cstr := C.CString(str)
983 defer C.free(unsafe.Pointer(cstr))
984 C.LLVMSetSection(v.C, cstr)
986 func (v Value) Visibility() Visibility { return Visibility(C.LLVMGetVisibility(v.C)) }
987 func (v Value) SetVisibility(vi Visibility) { C.LLVMSetVisibility(v.C, C.LLVMVisibility(vi)) }
988 func (v Value) Alignment() int { return int(C.LLVMGetAlignment(v.C)) }
989 func (v Value) SetAlignment(a int) { C.LLVMSetAlignment(v.C, C.unsigned(a)) }
990 func (v Value) SetUnnamedAddr(ua bool) { C.LLVMSetUnnamedAddr(v.C, boolToLLVMBool(ua)) }
992 // Operations on global variables
993 func AddGlobal(m Module, t Type, name string) (v Value) {
994 cname := C.CString(name)
995 defer C.free(unsafe.Pointer(cname))
996 v.C = C.LLVMAddGlobal(m.C, t.C, cname)
997 return
999 func AddGlobalInAddressSpace(m Module, t Type, name string, addressSpace int) (v Value) {
1000 cname := C.CString(name)
1001 defer C.free(unsafe.Pointer(cname))
1002 v.C = C.LLVMAddGlobalInAddressSpace(m.C, t.C, cname, C.unsigned(addressSpace))
1003 return
1005 func (m Module) NamedGlobal(name string) (v Value) {
1006 cname := C.CString(name)
1007 defer C.free(unsafe.Pointer(cname))
1008 v.C = C.LLVMGetNamedGlobal(m.C, cname)
1009 return
1012 func (m Module) FirstGlobal() (v Value) { v.C = C.LLVMGetFirstGlobal(m.C); return }
1013 func (m Module) LastGlobal() (v Value) { v.C = C.LLVMGetLastGlobal(m.C); return }
1014 func NextGlobal(v Value) (rv Value) { rv.C = C.LLVMGetNextGlobal(v.C); return }
1015 func PrevGlobal(v Value) (rv Value) { rv.C = C.LLVMGetPreviousGlobal(v.C); return }
1016 func (v Value) EraseFromParentAsGlobal() { C.LLVMDeleteGlobal(v.C) }
1017 func (v Value) Initializer() (rv Value) { rv.C = C.LLVMGetInitializer(v.C); return }
1018 func (v Value) SetInitializer(cv Value) { C.LLVMSetInitializer(v.C, cv.C) }
1019 func (v Value) IsThreadLocal() bool { return C.LLVMIsThreadLocal(v.C) != 0 }
1020 func (v Value) SetThreadLocal(tl bool) { C.LLVMSetThreadLocal(v.C, boolToLLVMBool(tl)) }
1021 func (v Value) IsGlobalConstant() bool { return C.LLVMIsGlobalConstant(v.C) != 0 }
1022 func (v Value) SetGlobalConstant(gc bool) { C.LLVMSetGlobalConstant(v.C, boolToLLVMBool(gc)) }
1024 // Operations on aliases
1025 func AddAlias(m Module, t Type, aliasee Value, name string) (v Value) {
1026 cname := C.CString(name)
1027 defer C.free(unsafe.Pointer(cname))
1028 v.C = C.LLVMAddAlias(m.C, t.C, aliasee.C, cname)
1029 return
1032 // Operations on functions
1033 func AddFunction(m Module, name string, ft Type) (v Value) {
1034 cname := C.CString(name)
1035 defer C.free(unsafe.Pointer(cname))
1036 v.C = C.LLVMAddFunction(m.C, cname, ft.C)
1037 return
1040 func (m Module) NamedFunction(name string) (v Value) {
1041 cname := C.CString(name)
1042 defer C.free(unsafe.Pointer(cname))
1043 v.C = C.LLVMGetNamedFunction(m.C, cname)
1044 return
1047 func (m Module) FirstFunction() (v Value) { v.C = C.LLVMGetFirstFunction(m.C); return }
1048 func (m Module) LastFunction() (v Value) { v.C = C.LLVMGetLastFunction(m.C); return }
1049 func NextFunction(v Value) (rv Value) { rv.C = C.LLVMGetNextFunction(v.C); return }
1050 func PrevFunction(v Value) (rv Value) { rv.C = C.LLVMGetPreviousFunction(v.C); return }
1051 func (v Value) EraseFromParentAsFunction() { C.LLVMDeleteFunction(v.C) }
1052 func (v Value) IntrinsicID() int { return int(C.LLVMGetIntrinsicID(v.C)) }
1053 func (v Value) FunctionCallConv() CallConv {
1054 return CallConv(C.LLVMCallConv(C.LLVMGetFunctionCallConv(v.C)))
1056 func (v Value) SetFunctionCallConv(cc CallConv) { C.LLVMSetFunctionCallConv(v.C, C.unsigned(cc)) }
1057 func (v Value) GC() string { return C.GoString(C.LLVMGetGC(v.C)) }
1058 func (v Value) SetGC(name string) {
1059 cname := C.CString(name)
1060 defer C.free(unsafe.Pointer(cname))
1061 C.LLVMSetGC(v.C, cname)
1063 func (v Value) AddAttributeAtIndex(i int, a Attribute) {
1064 C.LLVMAddAttributeAtIndex(v.C, C.LLVMAttributeIndex(i), a.C)
1066 func (v Value) AddFunctionAttr(a Attribute) {
1067 v.AddAttributeAtIndex(C.LLVMAttributeFunctionIndex, a);
1069 func (v Value) GetEnumAttributeAtIndex(i int, kind uint) (a Attribute) {
1070 a.C = C.LLVMGetEnumAttributeAtIndex(v.C, C.LLVMAttributeIndex(i), C.unsigned(kind))
1071 return
1073 func (v Value) GetEnumFunctionAttribute(kind uint) Attribute {
1074 return v.GetEnumAttributeAtIndex(C.LLVMAttributeFunctionIndex, kind)
1076 func (v Value) GetStringAttributeAtIndex(i int, kind string) (a Attribute) {
1077 ckind := C.CString(kind)
1078 defer C.free(unsafe.Pointer(ckind))
1079 a.C = C.LLVMGetStringAttributeAtIndex(v.C, C.LLVMAttributeIndex(i),
1080 ckind, C.unsigned(len(kind)))
1081 return
1083 func (v Value) RemoveEnumAttributeAtIndex(i int, kind uint) {
1084 C.LLVMRemoveEnumAttributeAtIndex(v.C, C.LLVMAttributeIndex(i), C.unsigned(kind))
1086 func (v Value) RemoveEnumFunctionAttribute(kind uint) {
1087 v.RemoveEnumAttributeAtIndex(C.LLVMAttributeFunctionIndex, kind);
1089 func (v Value) RemoveStringAttributeAtIndex(i int, kind string) {
1090 ckind := C.CString(kind)
1091 defer C.free(unsafe.Pointer(ckind))
1092 C.LLVMRemoveStringAttributeAtIndex(v.C, C.LLVMAttributeIndex(i),
1093 ckind, C.unsigned(len(kind)))
1095 func (v Value) AddTargetDependentFunctionAttr(attr, value string) {
1096 cattr := C.CString(attr)
1097 defer C.free(unsafe.Pointer(cattr))
1098 cvalue := C.CString(value)
1099 defer C.free(unsafe.Pointer(cvalue))
1100 C.LLVMAddTargetDependentFunctionAttr(v.C, cattr, cvalue)
1102 func (v Value) SetPersonality(p Value) {
1103 C.LLVMSetPersonalityFn(v.C, p.C)
1105 func (v Value) SetSubprogram(sp Metadata) {
1106 C.LLVMSetSubprogram(v.C, sp.C)
1109 // Operations on parameters
1110 func (v Value) ParamsCount() int { return int(C.LLVMCountParams(v.C)) }
1111 func (v Value) Params() []Value {
1112 out := make([]Value, v.ParamsCount())
1113 if len(out) > 0 {
1114 C.LLVMGetParams(v.C, llvmValueRefPtr(&out[0]))
1116 return out
1118 func (v Value) Param(i int) (rv Value) { rv.C = C.LLVMGetParam(v.C, C.unsigned(i)); return }
1119 func (v Value) ParamParent() (rv Value) { rv.C = C.LLVMGetParamParent(v.C); return }
1120 func (v Value) FirstParam() (rv Value) { rv.C = C.LLVMGetFirstParam(v.C); return }
1121 func (v Value) LastParam() (rv Value) { rv.C = C.LLVMGetLastParam(v.C); return }
1122 func NextParam(v Value) (rv Value) { rv.C = C.LLVMGetNextParam(v.C); return }
1123 func PrevParam(v Value) (rv Value) { rv.C = C.LLVMGetPreviousParam(v.C); return }
1124 func (v Value) SetParamAlignment(align int) { C.LLVMSetParamAlignment(v.C, C.unsigned(align)) }
1126 // Operations on basic blocks
1127 func (bb BasicBlock) AsValue() (v Value) { v.C = C.LLVMBasicBlockAsValue(bb.C); return }
1128 func (v Value) IsBasicBlock() bool { return C.LLVMValueIsBasicBlock(v.C) != 0 }
1129 func (v Value) AsBasicBlock() (bb BasicBlock) { bb.C = C.LLVMValueAsBasicBlock(v.C); return }
1130 func (bb BasicBlock) Parent() (v Value) { v.C = C.LLVMGetBasicBlockParent(bb.C); return }
1131 func (v Value) BasicBlocksCount() int { return int(C.LLVMCountBasicBlocks(v.C)) }
1132 func (v Value) BasicBlocks() []BasicBlock {
1133 out := make([]BasicBlock, v.BasicBlocksCount())
1134 C.LLVMGetBasicBlocks(v.C, llvmBasicBlockRefPtr(&out[0]))
1135 return out
1137 func (v Value) FirstBasicBlock() (bb BasicBlock) { bb.C = C.LLVMGetFirstBasicBlock(v.C); return }
1138 func (v Value) LastBasicBlock() (bb BasicBlock) { bb.C = C.LLVMGetLastBasicBlock(v.C); return }
1139 func NextBasicBlock(bb BasicBlock) (rbb BasicBlock) { rbb.C = C.LLVMGetNextBasicBlock(bb.C); return }
1140 func PrevBasicBlock(bb BasicBlock) (rbb BasicBlock) { rbb.C = C.LLVMGetPreviousBasicBlock(bb.C); return }
1141 func (v Value) EntryBasicBlock() (bb BasicBlock) { bb.C = C.LLVMGetEntryBasicBlock(v.C); return }
1142 func (c Context) AddBasicBlock(f Value, name string) (bb BasicBlock) {
1143 cname := C.CString(name)
1144 defer C.free(unsafe.Pointer(cname))
1145 bb.C = C.LLVMAppendBasicBlockInContext(c.C, f.C, cname)
1146 return
1148 func (c Context) InsertBasicBlock(ref BasicBlock, name string) (bb BasicBlock) {
1149 cname := C.CString(name)
1150 defer C.free(unsafe.Pointer(cname))
1151 bb.C = C.LLVMInsertBasicBlockInContext(c.C, ref.C, cname)
1152 return
1154 func AddBasicBlock(f Value, name string) (bb BasicBlock) {
1155 cname := C.CString(name)
1156 defer C.free(unsafe.Pointer(cname))
1157 bb.C = C.LLVMAppendBasicBlock(f.C, cname)
1158 return
1160 func InsertBasicBlock(ref BasicBlock, name string) (bb BasicBlock) {
1161 cname := C.CString(name)
1162 defer C.free(unsafe.Pointer(cname))
1163 bb.C = C.LLVMInsertBasicBlock(ref.C, cname)
1164 return
1166 func (bb BasicBlock) EraseFromParent() { C.LLVMDeleteBasicBlock(bb.C) }
1167 func (bb BasicBlock) MoveBefore(pos BasicBlock) { C.LLVMMoveBasicBlockBefore(bb.C, pos.C) }
1168 func (bb BasicBlock) MoveAfter(pos BasicBlock) { C.LLVMMoveBasicBlockAfter(bb.C, pos.C) }
1170 // Operations on instructions
1171 func (v Value) InstructionParent() (bb BasicBlock) { bb.C = C.LLVMGetInstructionParent(v.C); return }
1172 func (bb BasicBlock) FirstInstruction() (v Value) { v.C = C.LLVMGetFirstInstruction(bb.C); return }
1173 func (bb BasicBlock) LastInstruction() (v Value) { v.C = C.LLVMGetLastInstruction(bb.C); return }
1174 func NextInstruction(v Value) (rv Value) { rv.C = C.LLVMGetNextInstruction(v.C); return }
1175 func PrevInstruction(v Value) (rv Value) { rv.C = C.LLVMGetPreviousInstruction(v.C); return }
1177 // Operations on call sites
1178 func (v Value) SetInstructionCallConv(cc CallConv) {
1179 C.LLVMSetInstructionCallConv(v.C, C.unsigned(cc))
1181 func (v Value) InstructionCallConv() CallConv {
1182 return CallConv(C.LLVMCallConv(C.LLVMGetInstructionCallConv(v.C)))
1184 func (v Value) AddCallSiteAttribute(i int, a Attribute) {
1185 C.LLVMAddCallSiteAttribute(v.C, C.LLVMAttributeIndex(i), a.C)
1187 func (v Value) SetInstrParamAlignment(i int, align int) {
1188 C.LLVMSetInstrParamAlignment(v.C, C.unsigned(i), C.unsigned(align))
1191 // Operations on call instructions (only)
1192 func (v Value) IsTailCall() bool { return C.LLVMIsTailCall(v.C) != 0 }
1193 func (v Value) SetTailCall(is bool) { C.LLVMSetTailCall(v.C, boolToLLVMBool(is)) }
1195 // Operations on phi nodes
1196 func (v Value) AddIncoming(vals []Value, blocks []BasicBlock) {
1197 ptr, nvals := llvmValueRefs(vals)
1198 C.LLVMAddIncoming(v.C, ptr, llvmBasicBlockRefPtr(&blocks[0]), nvals)
1200 func (v Value) IncomingCount() int { return int(C.LLVMCountIncoming(v.C)) }
1201 func (v Value) IncomingValue(i int) (rv Value) {
1202 rv.C = C.LLVMGetIncomingValue(v.C, C.unsigned(i))
1203 return
1205 func (v Value) IncomingBlock(i int) (bb BasicBlock) {
1206 bb.C = C.LLVMGetIncomingBlock(v.C, C.unsigned(i))
1207 return
1210 //-------------------------------------------------------------------------
1211 // llvm.Builder
1212 //-------------------------------------------------------------------------
1214 // An instruction builder represents a point within a basic block, and is the
1215 // exclusive means of building instructions using the C interface.
1217 func (c Context) NewBuilder() (b Builder) { b.C = C.LLVMCreateBuilderInContext(c.C); return }
1218 func NewBuilder() (b Builder) { b.C = C.LLVMCreateBuilder(); return }
1219 func (b Builder) SetInsertPoint(block BasicBlock, instr Value) {
1220 C.LLVMPositionBuilder(b.C, block.C, instr.C)
1222 func (b Builder) SetInsertPointBefore(instr Value) { C.LLVMPositionBuilderBefore(b.C, instr.C) }
1223 func (b Builder) SetInsertPointAtEnd(block BasicBlock) { C.LLVMPositionBuilderAtEnd(b.C, block.C) }
1224 func (b Builder) GetInsertBlock() (bb BasicBlock) { bb.C = C.LLVMGetInsertBlock(b.C); return }
1225 func (b Builder) ClearInsertionPoint() { C.LLVMClearInsertionPosition(b.C) }
1226 func (b Builder) Insert(instr Value) { C.LLVMInsertIntoBuilder(b.C, instr.C) }
1227 func (b Builder) InsertWithName(instr Value, name string) {
1228 cname := C.CString(name)
1229 defer C.free(unsafe.Pointer(cname))
1230 C.LLVMInsertIntoBuilderWithName(b.C, instr.C, cname)
1232 func (b Builder) Dispose() { C.LLVMDisposeBuilder(b.C) }
1234 // Metadata
1235 type DebugLoc struct {
1236 Line, Col uint
1237 Scope Metadata
1238 InlinedAt Metadata
1240 func (b Builder) SetCurrentDebugLocation(line, col uint, scope, inlinedAt Metadata) {
1241 C.LLVMSetCurrentDebugLocation2(b.C, C.unsigned(line), C.unsigned(col), scope.C, inlinedAt.C)
1243 // Get current debug location. Please do not call this function until setting debug location with SetCurrentDebugLocation()
1244 func (b Builder) GetCurrentDebugLocation() (loc DebugLoc) {
1245 md := C.LLVMGetCurrentDebugLocation2(b.C)
1246 loc.Line = uint(md.Line)
1247 loc.Col = uint(md.Col)
1248 loc.Scope = Metadata{C: md.Scope}
1249 loc.InlinedAt = Metadata{C: md.InlinedAt}
1250 return
1252 func (b Builder) SetInstDebugLocation(v Value) { C.LLVMSetInstDebugLocation(b.C, v.C) }
1253 func (b Builder) InsertDeclare(module Module, storage Value, md Value) Value {
1254 f := module.NamedFunction("llvm.dbg.declare")
1255 if f.IsNil() {
1256 ftyp := FunctionType(VoidType(), []Type{storage.Type(), md.Type()}, false)
1257 f = AddFunction(module, "llvm.dbg.declare", ftyp)
1259 return b.CreateCall(f, []Value{storage, md}, "")
1262 // Terminators
1263 func (b Builder) CreateRetVoid() (rv Value) { rv.C = C.LLVMBuildRetVoid(b.C); return }
1264 func (b Builder) CreateRet(v Value) (rv Value) { rv.C = C.LLVMBuildRet(b.C, v.C); return }
1265 func (b Builder) CreateAggregateRet(vs []Value) (rv Value) {
1266 ptr, nvals := llvmValueRefs(vs)
1267 rv.C = C.LLVMBuildAggregateRet(b.C, ptr, nvals)
1268 return
1270 func (b Builder) CreateBr(bb BasicBlock) (rv Value) { rv.C = C.LLVMBuildBr(b.C, bb.C); return }
1271 func (b Builder) CreateCondBr(ifv Value, thenb, elseb BasicBlock) (rv Value) {
1272 rv.C = C.LLVMBuildCondBr(b.C, ifv.C, thenb.C, elseb.C)
1273 return
1275 func (b Builder) CreateSwitch(v Value, elseb BasicBlock, numCases int) (rv Value) {
1276 rv.C = C.LLVMBuildSwitch(b.C, v.C, elseb.C, C.unsigned(numCases))
1277 return
1279 func (b Builder) CreateIndirectBr(addr Value, numDests int) (rv Value) {
1280 rv.C = C.LLVMBuildIndirectBr(b.C, addr.C, C.unsigned(numDests))
1281 return
1283 func (b Builder) CreateInvoke(fn Value, args []Value, then, catch BasicBlock, name string) (rv Value) {
1284 cname := C.CString(name)
1285 defer C.free(unsafe.Pointer(cname))
1286 ptr, nvals := llvmValueRefs(args)
1287 rv.C = C.LLVMBuildInvoke(b.C, fn.C, ptr, nvals, then.C, catch.C, cname)
1288 return
1290 func (b Builder) CreateUnreachable() (rv Value) { rv.C = C.LLVMBuildUnreachable(b.C); return }
1292 // Add a case to the switch instruction
1293 func (v Value) AddCase(on Value, dest BasicBlock) { C.LLVMAddCase(v.C, on.C, dest.C) }
1295 // Add a destination to the indirectbr instruction
1296 func (v Value) AddDest(dest BasicBlock) { C.LLVMAddDestination(v.C, dest.C) }
1298 // Arithmetic
1299 func (b Builder) CreateAdd(lhs, rhs Value, name string) (v Value) {
1300 cname := C.CString(name)
1301 defer C.free(unsafe.Pointer(cname))
1302 v.C = C.LLVMBuildAdd(b.C, lhs.C, rhs.C, cname)
1303 return
1305 func (b Builder) CreateNSWAdd(lhs, rhs Value, name string) (v Value) {
1306 cname := C.CString(name)
1307 defer C.free(unsafe.Pointer(cname))
1308 v.C = C.LLVMBuildNSWAdd(b.C, lhs.C, rhs.C, cname)
1309 return
1311 func (b Builder) CreateNUWAdd(lhs, rhs Value, name string) (v Value) {
1312 cname := C.CString(name)
1313 defer C.free(unsafe.Pointer(cname))
1314 v.C = C.LLVMBuildNUWAdd(b.C, lhs.C, rhs.C, cname)
1315 return
1317 func (b Builder) CreateFAdd(lhs, rhs Value, name string) (v Value) {
1318 cname := C.CString(name)
1319 defer C.free(unsafe.Pointer(cname))
1320 v.C = C.LLVMBuildFAdd(b.C, lhs.C, rhs.C, cname)
1321 return
1323 func (b Builder) CreateSub(lhs, rhs Value, name string) (v Value) {
1324 cname := C.CString(name)
1325 defer C.free(unsafe.Pointer(cname))
1326 v.C = C.LLVMBuildSub(b.C, lhs.C, rhs.C, cname)
1327 return
1329 func (b Builder) CreateNSWSub(lhs, rhs Value, name string) (v Value) {
1330 cname := C.CString(name)
1331 defer C.free(unsafe.Pointer(cname))
1332 v.C = C.LLVMBuildNSWSub(b.C, lhs.C, rhs.C, cname)
1333 return
1335 func (b Builder) CreateNUWSub(lhs, rhs Value, name string) (v Value) {
1336 cname := C.CString(name)
1337 defer C.free(unsafe.Pointer(cname))
1338 v.C = C.LLVMBuildNUWSub(b.C, lhs.C, rhs.C, cname)
1339 return
1341 func (b Builder) CreateFSub(lhs, rhs Value, name string) (v Value) {
1342 cname := C.CString(name)
1343 v.C = C.LLVMBuildFSub(b.C, lhs.C, rhs.C, cname)
1344 C.free(unsafe.Pointer(cname))
1345 return
1347 func (b Builder) CreateMul(lhs, rhs Value, name string) (v Value) {
1348 cname := C.CString(name)
1349 defer C.free(unsafe.Pointer(cname))
1350 v.C = C.LLVMBuildMul(b.C, lhs.C, rhs.C, cname)
1351 return
1353 func (b Builder) CreateNSWMul(lhs, rhs Value, name string) (v Value) {
1354 cname := C.CString(name)
1355 defer C.free(unsafe.Pointer(cname))
1356 v.C = C.LLVMBuildNSWMul(b.C, lhs.C, rhs.C, cname)
1357 return
1359 func (b Builder) CreateNUWMul(lhs, rhs Value, name string) (v Value) {
1360 cname := C.CString(name)
1361 defer C.free(unsafe.Pointer(cname))
1362 v.C = C.LLVMBuildNUWMul(b.C, lhs.C, rhs.C, cname)
1363 return
1365 func (b Builder) CreateFMul(lhs, rhs Value, name string) (v Value) {
1366 cname := C.CString(name)
1367 defer C.free(unsafe.Pointer(cname))
1368 v.C = C.LLVMBuildFMul(b.C, lhs.C, rhs.C, cname)
1369 return
1371 func (b Builder) CreateUDiv(lhs, rhs Value, name string) (v Value) {
1372 cname := C.CString(name)
1373 defer C.free(unsafe.Pointer(cname))
1374 v.C = C.LLVMBuildUDiv(b.C, lhs.C, rhs.C, cname)
1375 return
1377 func (b Builder) CreateSDiv(lhs, rhs Value, name string) (v Value) {
1378 cname := C.CString(name)
1379 defer C.free(unsafe.Pointer(cname))
1380 v.C = C.LLVMBuildSDiv(b.C, lhs.C, rhs.C, cname)
1381 return
1383 func (b Builder) CreateExactSDiv(lhs, rhs Value, name string) (v Value) {
1384 cname := C.CString(name)
1385 defer C.free(unsafe.Pointer(cname))
1386 v.C = C.LLVMBuildExactSDiv(b.C, lhs.C, rhs.C, cname)
1387 return
1389 func (b Builder) CreateFDiv(lhs, rhs Value, name string) (v Value) {
1390 cname := C.CString(name)
1391 defer C.free(unsafe.Pointer(cname))
1392 v.C = C.LLVMBuildFDiv(b.C, lhs.C, rhs.C, cname)
1393 return
1395 func (b Builder) CreateURem(lhs, rhs Value, name string) (v Value) {
1396 cname := C.CString(name)
1397 defer C.free(unsafe.Pointer(cname))
1398 v.C = C.LLVMBuildURem(b.C, lhs.C, rhs.C, cname)
1399 return
1401 func (b Builder) CreateSRem(lhs, rhs Value, name string) (v Value) {
1402 cname := C.CString(name)
1403 defer C.free(unsafe.Pointer(cname))
1404 v.C = C.LLVMBuildSRem(b.C, lhs.C, rhs.C, cname)
1405 return
1407 func (b Builder) CreateFRem(lhs, rhs Value, name string) (v Value) {
1408 cname := C.CString(name)
1409 defer C.free(unsafe.Pointer(cname))
1410 v.C = C.LLVMBuildFRem(b.C, lhs.C, rhs.C, cname)
1411 return
1413 func (b Builder) CreateShl(lhs, rhs Value, name string) (v Value) {
1414 cname := C.CString(name)
1415 defer C.free(unsafe.Pointer(cname))
1416 v.C = C.LLVMBuildShl(b.C, lhs.C, rhs.C, cname)
1417 return
1419 func (b Builder) CreateLShr(lhs, rhs Value, name string) (v Value) {
1420 cname := C.CString(name)
1421 defer C.free(unsafe.Pointer(cname))
1422 v.C = C.LLVMBuildLShr(b.C, lhs.C, rhs.C, cname)
1423 return
1425 func (b Builder) CreateAShr(lhs, rhs Value, name string) (v Value) {
1426 cname := C.CString(name)
1427 defer C.free(unsafe.Pointer(cname))
1428 v.C = C.LLVMBuildAShr(b.C, lhs.C, rhs.C, cname)
1429 return
1431 func (b Builder) CreateAnd(lhs, rhs Value, name string) (v Value) {
1432 cname := C.CString(name)
1433 defer C.free(unsafe.Pointer(cname))
1434 v.C = C.LLVMBuildAnd(b.C, lhs.C, rhs.C, cname)
1435 return
1437 func (b Builder) CreateOr(lhs, rhs Value, name string) (v Value) {
1438 cname := C.CString(name)
1439 defer C.free(unsafe.Pointer(cname))
1440 v.C = C.LLVMBuildOr(b.C, lhs.C, rhs.C, cname)
1441 return
1443 func (b Builder) CreateXor(lhs, rhs Value, name string) (v Value) {
1444 cname := C.CString(name)
1445 defer C.free(unsafe.Pointer(cname))
1446 v.C = C.LLVMBuildXor(b.C, lhs.C, rhs.C, cname)
1447 return
1449 func (b Builder) CreateBinOp(op Opcode, lhs, rhs Value, name string) (v Value) {
1450 cname := C.CString(name)
1451 defer C.free(unsafe.Pointer(cname))
1452 v.C = C.LLVMBuildBinOp(b.C, C.LLVMOpcode(op), lhs.C, rhs.C, cname)
1453 return
1455 func (b Builder) CreateNeg(v Value, name string) (rv Value) {
1456 cname := C.CString(name)
1457 defer C.free(unsafe.Pointer(cname))
1458 rv.C = C.LLVMBuildNeg(b.C, v.C, cname)
1459 return
1461 func (b Builder) CreateNSWNeg(v Value, name string) (rv Value) {
1462 cname := C.CString(name)
1463 defer C.free(unsafe.Pointer(cname))
1464 rv.C = C.LLVMBuildNSWNeg(b.C, v.C, cname)
1465 return
1467 func (b Builder) CreateNUWNeg(v Value, name string) (rv Value) {
1468 cname := C.CString(name)
1469 defer C.free(unsafe.Pointer(cname))
1470 rv.C = C.LLVMBuildNUWNeg(b.C, v.C, cname)
1471 return
1473 func (b Builder) CreateFNeg(v Value, name string) (rv Value) {
1474 cname := C.CString(name)
1475 defer C.free(unsafe.Pointer(cname))
1476 rv.C = C.LLVMBuildFNeg(b.C, v.C, cname)
1477 return
1479 func (b Builder) CreateNot(v Value, name string) (rv Value) {
1480 cname := C.CString(name)
1481 defer C.free(unsafe.Pointer(cname))
1482 rv.C = C.LLVMBuildNot(b.C, v.C, cname)
1483 return
1486 // Memory
1488 func (b Builder) CreateMalloc(t Type, name string) (v Value) {
1489 cname := C.CString(name)
1490 defer C.free(unsafe.Pointer(cname))
1491 v.C = C.LLVMBuildMalloc(b.C, t.C, cname)
1492 return
1494 func (b Builder) CreateArrayMalloc(t Type, val Value, name string) (v Value) {
1495 cname := C.CString(name)
1496 defer C.free(unsafe.Pointer(cname))
1497 v.C = C.LLVMBuildArrayMalloc(b.C, t.C, val.C, cname)
1498 return
1500 func (b Builder) CreateAlloca(t Type, name string) (v Value) {
1501 cname := C.CString(name)
1502 defer C.free(unsafe.Pointer(cname))
1503 v.C = C.LLVMBuildAlloca(b.C, t.C, cname)
1504 return
1506 func (b Builder) CreateArrayAlloca(t Type, val Value, name string) (v Value) {
1507 cname := C.CString(name)
1508 defer C.free(unsafe.Pointer(cname))
1509 v.C = C.LLVMBuildArrayAlloca(b.C, t.C, val.C, cname)
1510 return
1512 func (b Builder) CreateFree(p Value) (v Value) {
1513 v.C = C.LLVMBuildFree(b.C, p.C)
1514 return
1516 func (b Builder) CreateLoad(p Value, name string) (v Value) {
1517 cname := C.CString(name)
1518 defer C.free(unsafe.Pointer(cname))
1519 v.C = C.LLVMBuildLoad(b.C, p.C, cname)
1520 return
1522 func (b Builder) CreateStore(val Value, p Value) (v Value) {
1523 v.C = C.LLVMBuildStore(b.C, val.C, p.C)
1524 return
1526 func (b Builder) CreateGEP(p Value, indices []Value, name string) (v Value) {
1527 cname := C.CString(name)
1528 defer C.free(unsafe.Pointer(cname))
1529 ptr, nvals := llvmValueRefs(indices)
1530 v.C = C.LLVMBuildGEP(b.C, p.C, ptr, nvals, cname)
1531 return
1533 func (b Builder) CreateInBoundsGEP(p Value, indices []Value, name string) (v Value) {
1534 cname := C.CString(name)
1535 defer C.free(unsafe.Pointer(cname))
1536 ptr, nvals := llvmValueRefs(indices)
1537 v.C = C.LLVMBuildInBoundsGEP(b.C, p.C, ptr, nvals, cname)
1538 return
1540 func (b Builder) CreateStructGEP(p Value, i int, name string) (v Value) {
1541 cname := C.CString(name)
1542 defer C.free(unsafe.Pointer(cname))
1543 v.C = C.LLVMBuildStructGEP(b.C, p.C, C.unsigned(i), cname)
1544 return
1546 func (b Builder) CreateGlobalString(str, name string) (v Value) {
1547 cstr := C.CString(str)
1548 defer C.free(unsafe.Pointer(cstr))
1549 cname := C.CString(name)
1550 defer C.free(unsafe.Pointer(cname))
1551 v.C = C.LLVMBuildGlobalString(b.C, cstr, cname)
1552 return
1554 func (b Builder) CreateGlobalStringPtr(str, name string) (v Value) {
1555 cstr := C.CString(str)
1556 defer C.free(unsafe.Pointer(cstr))
1557 cname := C.CString(name)
1558 defer C.free(unsafe.Pointer(cname))
1559 v.C = C.LLVMBuildGlobalStringPtr(b.C, cstr, cname)
1560 return
1563 // Casts
1564 func (b Builder) CreateTrunc(val Value, t Type, name string) (v Value) {
1565 cname := C.CString(name)
1566 defer C.free(unsafe.Pointer(cname))
1567 v.C = C.LLVMBuildTrunc(b.C, val.C, t.C, cname)
1568 return
1570 func (b Builder) CreateZExt(val Value, t Type, name string) (v Value) {
1571 cname := C.CString(name)
1572 defer C.free(unsafe.Pointer(cname))
1573 v.C = C.LLVMBuildZExt(b.C, val.C, t.C, cname)
1574 return
1576 func (b Builder) CreateSExt(val Value, t Type, name string) (v Value) {
1577 cname := C.CString(name)
1578 defer C.free(unsafe.Pointer(cname))
1579 v.C = C.LLVMBuildSExt(b.C, val.C, t.C, cname)
1580 return
1582 func (b Builder) CreateFPToUI(val Value, t Type, name string) (v Value) {
1583 cname := C.CString(name)
1584 defer C.free(unsafe.Pointer(cname))
1585 v.C = C.LLVMBuildFPToUI(b.C, val.C, t.C, cname)
1586 return
1588 func (b Builder) CreateFPToSI(val Value, t Type, name string) (v Value) {
1589 cname := C.CString(name)
1590 defer C.free(unsafe.Pointer(cname))
1591 v.C = C.LLVMBuildFPToSI(b.C, val.C, t.C, cname)
1592 return
1594 func (b Builder) CreateUIToFP(val Value, t Type, name string) (v Value) {
1595 cname := C.CString(name)
1596 defer C.free(unsafe.Pointer(cname))
1597 v.C = C.LLVMBuildUIToFP(b.C, val.C, t.C, cname)
1598 return
1600 func (b Builder) CreateSIToFP(val Value, t Type, name string) (v Value) {
1601 cname := C.CString(name)
1602 defer C.free(unsafe.Pointer(cname))
1603 v.C = C.LLVMBuildSIToFP(b.C, val.C, t.C, cname)
1604 return
1606 func (b Builder) CreateFPTrunc(val Value, t Type, name string) (v Value) {
1607 cname := C.CString(name)
1608 defer C.free(unsafe.Pointer(cname))
1609 v.C = C.LLVMBuildFPTrunc(b.C, val.C, t.C, cname)
1610 return
1612 func (b Builder) CreateFPExt(val Value, t Type, name string) (v Value) {
1613 cname := C.CString(name)
1614 defer C.free(unsafe.Pointer(cname))
1615 v.C = C.LLVMBuildFPExt(b.C, val.C, t.C, cname)
1616 return
1618 func (b Builder) CreatePtrToInt(val Value, t Type, name string) (v Value) {
1619 cname := C.CString(name)
1620 defer C.free(unsafe.Pointer(cname))
1621 v.C = C.LLVMBuildPtrToInt(b.C, val.C, t.C, cname)
1622 return
1624 func (b Builder) CreateIntToPtr(val Value, t Type, name string) (v Value) {
1625 cname := C.CString(name)
1626 defer C.free(unsafe.Pointer(cname))
1627 v.C = C.LLVMBuildIntToPtr(b.C, val.C, t.C, cname)
1628 return
1630 func (b Builder) CreateBitCast(val Value, t Type, name string) (v Value) {
1631 cname := C.CString(name)
1632 defer C.free(unsafe.Pointer(cname))
1633 v.C = C.LLVMBuildBitCast(b.C, val.C, t.C, cname)
1634 return
1636 func (b Builder) CreateZExtOrBitCast(val Value, t Type, name string) (v Value) {
1637 cname := C.CString(name)
1638 defer C.free(unsafe.Pointer(cname))
1639 v.C = C.LLVMBuildZExtOrBitCast(b.C, val.C, t.C, cname)
1640 return
1642 func (b Builder) CreateSExtOrBitCast(val Value, t Type, name string) (v Value) {
1643 cname := C.CString(name)
1644 defer C.free(unsafe.Pointer(cname))
1645 v.C = C.LLVMBuildSExtOrBitCast(b.C, val.C, t.C, cname)
1646 return
1648 func (b Builder) CreateTruncOrBitCast(val Value, t Type, name string) (v Value) {
1649 cname := C.CString(name)
1650 defer C.free(unsafe.Pointer(cname))
1651 v.C = C.LLVMBuildTruncOrBitCast(b.C, val.C, t.C, cname)
1652 return
1654 func (b Builder) CreateCast(val Value, op Opcode, t Type, name string) (v Value) {
1655 cname := C.CString(name)
1656 defer C.free(unsafe.Pointer(cname))
1657 v.C = C.LLVMBuildCast(b.C, C.LLVMOpcode(op), val.C, t.C, cname)
1658 return
1659 } //
1660 func (b Builder) CreatePointerCast(val Value, t Type, name string) (v Value) {
1661 cname := C.CString(name)
1662 defer C.free(unsafe.Pointer(cname))
1663 v.C = C.LLVMBuildPointerCast(b.C, val.C, t.C, cname)
1664 return
1666 func (b Builder) CreateIntCast(val Value, t Type, name string) (v Value) {
1667 cname := C.CString(name)
1668 defer C.free(unsafe.Pointer(cname))
1669 v.C = C.LLVMBuildIntCast(b.C, val.C, t.C, cname)
1670 return
1672 func (b Builder) CreateFPCast(val Value, t Type, name string) (v Value) {
1673 cname := C.CString(name)
1674 defer C.free(unsafe.Pointer(cname))
1675 v.C = C.LLVMBuildFPCast(b.C, val.C, t.C, cname)
1676 return
1679 // Comparisons
1680 func (b Builder) CreateICmp(pred IntPredicate, lhs, rhs Value, name string) (v Value) {
1681 cname := C.CString(name)
1682 defer C.free(unsafe.Pointer(cname))
1683 v.C = C.LLVMBuildICmp(b.C, C.LLVMIntPredicate(pred), lhs.C, rhs.C, cname)
1684 return
1686 func (b Builder) CreateFCmp(pred FloatPredicate, lhs, rhs Value, name string) (v Value) {
1687 cname := C.CString(name)
1688 defer C.free(unsafe.Pointer(cname))
1689 v.C = C.LLVMBuildFCmp(b.C, C.LLVMRealPredicate(pred), lhs.C, rhs.C, cname)
1690 return
1693 // Miscellaneous instructions
1694 func (b Builder) CreatePHI(t Type, name string) (v Value) {
1695 cname := C.CString(name)
1696 defer C.free(unsafe.Pointer(cname))
1697 v.C = C.LLVMBuildPhi(b.C, t.C, cname)
1698 return
1700 func (b Builder) CreateCall(fn Value, args []Value, name string) (v Value) {
1701 cname := C.CString(name)
1702 defer C.free(unsafe.Pointer(cname))
1703 ptr, nvals := llvmValueRefs(args)
1704 v.C = C.LLVMBuildCall(b.C, fn.C, ptr, nvals, cname)
1705 return
1708 func (b Builder) CreateSelect(ifv, thenv, elsev Value, name string) (v Value) {
1709 cname := C.CString(name)
1710 defer C.free(unsafe.Pointer(cname))
1711 v.C = C.LLVMBuildSelect(b.C, ifv.C, thenv.C, elsev.C, cname)
1712 return
1715 func (b Builder) CreateVAArg(list Value, t Type, name string) (v Value) {
1716 cname := C.CString(name)
1717 defer C.free(unsafe.Pointer(cname))
1718 v.C = C.LLVMBuildVAArg(b.C, list.C, t.C, cname)
1719 return
1721 func (b Builder) CreateExtractElement(vec, i Value, name string) (v Value) {
1722 cname := C.CString(name)
1723 defer C.free(unsafe.Pointer(cname))
1724 v.C = C.LLVMBuildExtractElement(b.C, vec.C, i.C, cname)
1725 return
1727 func (b Builder) CreateInsertElement(vec, elt, i Value, name string) (v Value) {
1728 cname := C.CString(name)
1729 defer C.free(unsafe.Pointer(cname))
1730 v.C = C.LLVMBuildInsertElement(b.C, vec.C, elt.C, i.C, cname)
1731 return
1733 func (b Builder) CreateShuffleVector(v1, v2, mask Value, name string) (v Value) {
1734 cname := C.CString(name)
1735 defer C.free(unsafe.Pointer(cname))
1736 v.C = C.LLVMBuildShuffleVector(b.C, v1.C, v2.C, mask.C, cname)
1737 return
1739 func (b Builder) CreateExtractValue(agg Value, i int, name string) (v Value) {
1740 cname := C.CString(name)
1741 defer C.free(unsafe.Pointer(cname))
1742 v.C = C.LLVMBuildExtractValue(b.C, agg.C, C.unsigned(i), cname)
1743 return
1745 func (b Builder) CreateInsertValue(agg, elt Value, i int, name string) (v Value) {
1746 cname := C.CString(name)
1747 defer C.free(unsafe.Pointer(cname))
1748 v.C = C.LLVMBuildInsertValue(b.C, agg.C, elt.C, C.unsigned(i), cname)
1749 return
1752 func (b Builder) CreateIsNull(val Value, name string) (v Value) {
1753 cname := C.CString(name)
1754 defer C.free(unsafe.Pointer(cname))
1755 v.C = C.LLVMBuildIsNull(b.C, val.C, cname)
1756 return
1758 func (b Builder) CreateIsNotNull(val Value, name string) (v Value) {
1759 cname := C.CString(name)
1760 defer C.free(unsafe.Pointer(cname))
1761 v.C = C.LLVMBuildIsNotNull(b.C, val.C, cname)
1762 return
1764 func (b Builder) CreatePtrDiff(lhs, rhs Value, name string) (v Value) {
1765 cname := C.CString(name)
1766 defer C.free(unsafe.Pointer(cname))
1767 v.C = C.LLVMBuildPtrDiff(b.C, lhs.C, rhs.C, cname)
1768 return
1771 func (b Builder) CreateLandingPad(t Type, nclauses int, name string) (l Value) {
1772 cname := C.CString(name)
1773 defer C.free(unsafe.Pointer(cname))
1774 l.C = C.LLVMBuildLandingPad(b.C, t.C, nil, C.unsigned(nclauses), cname)
1775 return l
1778 func (l Value) AddClause(v Value) {
1779 C.LLVMAddClause(l.C, v.C)
1782 func (l Value) SetCleanup(cleanup bool) {
1783 C.LLVMSetCleanup(l.C, boolToLLVMBool(cleanup))
1786 func (b Builder) CreateResume(ex Value) (v Value) {
1787 v.C = C.LLVMBuildResume(b.C, ex.C)
1788 return
1791 //-------------------------------------------------------------------------
1792 // llvm.ModuleProvider
1793 //-------------------------------------------------------------------------
1795 // Changes the type of M so it can be passed to FunctionPassManagers and the
1796 // JIT. They take ModuleProviders for historical reasons.
1797 func NewModuleProviderForModule(m Module) (mp ModuleProvider) {
1798 mp.C = C.LLVMCreateModuleProviderForExistingModule(m.C)
1799 return
1802 // Destroys the module M.
1803 func (mp ModuleProvider) Dispose() { C.LLVMDisposeModuleProvider(mp.C) }
1805 //-------------------------------------------------------------------------
1806 // llvm.MemoryBuffer
1807 //-------------------------------------------------------------------------
1809 func NewMemoryBufferFromFile(path string) (b MemoryBuffer, err error) {
1810 var cmsg *C.char
1811 cpath := C.CString(path)
1812 defer C.free(unsafe.Pointer(cpath))
1813 fail := C.LLVMCreateMemoryBufferWithContentsOfFile(cpath, &b.C, &cmsg)
1814 if fail != 0 {
1815 b.C = nil
1816 err = errors.New(C.GoString(cmsg))
1817 C.LLVMDisposeMessage(cmsg)
1819 return
1822 func NewMemoryBufferFromStdin() (b MemoryBuffer, err error) {
1823 var cmsg *C.char
1824 fail := C.LLVMCreateMemoryBufferWithSTDIN(&b.C, &cmsg)
1825 if fail != 0 {
1826 b.C = nil
1827 err = errors.New(C.GoString(cmsg))
1828 C.LLVMDisposeMessage(cmsg)
1830 return
1833 func (b MemoryBuffer) Bytes() []byte {
1834 cstart := C.LLVMGetBufferStart(b.C)
1835 csize := C.LLVMGetBufferSize(b.C)
1836 return C.GoBytes(unsafe.Pointer(cstart), C.int(csize))
1839 func (b MemoryBuffer) Dispose() { C.LLVMDisposeMemoryBuffer(b.C) }
1841 //-------------------------------------------------------------------------
1842 // llvm.PassManager
1843 //-------------------------------------------------------------------------
1845 // Constructs a new whole-module pass pipeline. This type of pipeline is
1846 // suitable for link-time optimization and whole-module transformations.
1847 // See llvm::PassManager::PassManager.
1848 func NewPassManager() (pm PassManager) { pm.C = C.LLVMCreatePassManager(); return }
1850 // Constructs a new function-by-function pass pipeline over the module
1851 // provider. It does not take ownership of the module provider. This type of
1852 // pipeline is suitable for code generation and JIT compilation tasks.
1853 // See llvm::FunctionPassManager::FunctionPassManager.
1854 func NewFunctionPassManagerForModule(m Module) (pm PassManager) {
1855 pm.C = C.LLVMCreateFunctionPassManagerForModule(m.C)
1856 return
1859 // Initializes, executes on the provided module, and finalizes all of the
1860 // passes scheduled in the pass manager. Returns 1 if any of the passes
1861 // modified the module, 0 otherwise. See llvm::PassManager::run(Module&).
1862 func (pm PassManager) Run(m Module) bool { return C.LLVMRunPassManager(pm.C, m.C) != 0 }
1864 // Initializes all of the function passes scheduled in the function pass
1865 // manager. Returns 1 if any of the passes modified the module, 0 otherwise.
1866 // See llvm::FunctionPassManager::doInitialization.
1867 func (pm PassManager) InitializeFunc() bool { return C.LLVMInitializeFunctionPassManager(pm.C) != 0 }
1869 // Executes all of the function passes scheduled in the function pass manager
1870 // on the provided function. Returns 1 if any of the passes modified the
1871 // function, false otherwise.
1872 // See llvm::FunctionPassManager::run(Function&).
1873 func (pm PassManager) RunFunc(f Value) bool { return C.LLVMRunFunctionPassManager(pm.C, f.C) != 0 }
1875 // Finalizes all of the function passes scheduled in in the function pass
1876 // manager. Returns 1 if any of the passes modified the module, 0 otherwise.
1877 // See llvm::FunctionPassManager::doFinalization.
1878 func (pm PassManager) FinalizeFunc() bool { return C.LLVMFinalizeFunctionPassManager(pm.C) != 0 }
1880 // Frees the memory of a pass pipeline. For function pipelines, does not free
1881 // the module provider.
1882 // See llvm::PassManagerBase::~PassManagerBase.
1883 func (pm PassManager) Dispose() { C.LLVMDisposePassManager(pm.C) }
1885 //-------------------------------------------------------------------------
1886 // llvm.Metadata
1887 //-------------------------------------------------------------------------
1889 func (md Metadata) ReplaceAllUsesWith(new Metadata) {
1890 C.LLVMMetadataReplaceAllUsesWith(md.C, new.C)