1 //===- ir.go - Bindings for ir --------------------------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file defines bindings for the ir component.
12 //===----------------------------------------------------------------------===//
17 #include "llvm-c/Core.h"
18 #include "IRBindings.h"
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.
46 ModuleProvider struct {
47 C C.LLVMModuleProviderRef
50 C C.LLVMMemoryBufferRef
53 C C.LLVMPassManagerRef
63 TypeKind C.LLVMTypeKind
65 Visibility C.LLVMVisibility
66 CallConv C.LLVMCallConv
67 IntPredicate C.LLVMIntPredicate
68 FloatPredicate C.LLVMRealPredicate
69 LandingPadClause C.LLVMLandingPadClauseTy
72 func (c Context) IsNil() bool { return c.C == nil }
73 func (c Module) IsNil() bool { return c.C == nil }
74 func (c Type) IsNil() bool { return c.C == nil }
75 func (c Value) IsNil() bool { return c.C == nil }
76 func (c BasicBlock) IsNil() bool { return c.C == nil }
77 func (c Builder) IsNil() bool { return c.C == nil }
78 func (c ModuleProvider) IsNil() bool { return c.C == nil }
79 func (c MemoryBuffer) IsNil() bool { return c.C == nil }
80 func (c PassManager) IsNil() bool { return c.C == nil }
81 func (c Use) IsNil() bool { return c.C == nil }
84 func llvmTypeRefPtr(t *Type) *C.LLVMTypeRef { return (*C.LLVMTypeRef)(unsafe.Pointer(t)) }
85 func llvmValueRefPtr(t *Value) *C.LLVMValueRef { return (*C.LLVMValueRef)(unsafe.Pointer(t)) }
86 func llvmMetadataRefPtr(t *Metadata) *C.LLVMMetadataRef {
87 return (*C.LLVMMetadataRef)(unsafe.Pointer(t))
89 func llvmBasicBlockRefPtr(t *BasicBlock) *C.LLVMBasicBlockRef {
90 return (*C.LLVMBasicBlockRef)(unsafe.Pointer(t))
92 func boolToLLVMBool(b bool) C.LLVMBool {
99 func llvmValueRefs(values []Value) (*C.LLVMValueRef, C.unsigned) {
100 var pt *C.LLVMValueRef
101 ptlen := C.unsigned(len(values))
103 pt = llvmValueRefPtr(&values[0])
108 func llvmMetadataRefs(mds []Metadata) (*C.LLVMMetadataRef, C.unsigned) {
109 var pt *C.LLVMMetadataRef
110 ptlen := C.unsigned(len(mds))
112 pt = llvmMetadataRefPtr(&mds[0])
117 //-------------------------------------------------------------------------
119 //-------------------------------------------------------------------------
122 NoneAttribute Attribute = 0
123 ZExtAttribute Attribute = C.LLVMZExtAttribute
124 SExtAttribute Attribute = C.LLVMSExtAttribute
125 NoReturnAttribute Attribute = C.LLVMNoReturnAttribute
126 InRegAttribute Attribute = C.LLVMInRegAttribute
127 StructRetAttribute Attribute = C.LLVMStructRetAttribute
128 NoUnwindAttribute Attribute = C.LLVMNoUnwindAttribute
129 NoAliasAttribute Attribute = C.LLVMNoAliasAttribute
130 ByValAttribute Attribute = C.LLVMByValAttribute
131 NestAttribute Attribute = C.LLVMNestAttribute
132 ReadNoneAttribute Attribute = C.LLVMReadNoneAttribute
133 ReadOnlyAttribute Attribute = C.LLVMReadOnlyAttribute
134 NoInlineAttribute Attribute = C.LLVMNoInlineAttribute
135 AlwaysInlineAttribute Attribute = C.LLVMAlwaysInlineAttribute
136 OptimizeForSizeAttribute Attribute = C.LLVMOptimizeForSizeAttribute
137 StackProtectAttribute Attribute = C.LLVMStackProtectAttribute
138 StackProtectReqAttribute Attribute = C.LLVMStackProtectReqAttribute
139 Alignment Attribute = C.LLVMAlignment
140 NoCaptureAttribute Attribute = C.LLVMNoCaptureAttribute
141 NoRedZoneAttribute Attribute = C.LLVMNoRedZoneAttribute
142 NoImplicitFloatAttribute Attribute = C.LLVMNoImplicitFloatAttribute
143 NakedAttribute Attribute = C.LLVMNakedAttribute
144 InlineHintAttribute Attribute = C.LLVMInlineHintAttribute
145 StackAlignment Attribute = C.LLVMStackAlignment
146 ReturnsTwiceAttribute Attribute = C.LLVMReturnsTwice
147 UWTableAttribute Attribute = C.LLVMUWTable
148 NonLazyBindAttribute Attribute = 1 << 31
149 SanitizeAddressAttribute Attribute = 1 << 32
150 MinSizeAttribute Attribute = 1 << 33
151 NoDuplicateAttribute Attribute = 1 << 34
152 StackProtectStrongAttribute Attribute = 1 << 35
153 SanitizeThreadAttribute Attribute = 1 << 36
154 SanitizeMemoryAttribute Attribute = 1 << 37
155 NoBuiltinAttribute Attribute = 1 << 38
156 ReturnedAttribute Attribute = 1 << 39
157 ColdAttribute Attribute = 1 << 40
158 BuiltinAttribute Attribute = 1 << 41
159 OptimizeNoneAttribute Attribute = 1 << 42
160 InAllocaAttribute Attribute = 1 << 43
161 NonNullAttribute Attribute = 1 << 44
162 JumpTableAttribute Attribute = 1 << 45
165 //-------------------------------------------------------------------------
167 //-------------------------------------------------------------------------
170 Ret Opcode = C.LLVMRet
172 Switch Opcode = C.LLVMSwitch
173 IndirectBr Opcode = C.LLVMIndirectBr
174 Invoke Opcode = C.LLVMInvoke
175 Unreachable Opcode = C.LLVMUnreachable
177 // Standard Binary Operators
178 Add Opcode = C.LLVMAdd
179 FAdd Opcode = C.LLVMFAdd
180 Sub Opcode = C.LLVMSub
181 FSub Opcode = C.LLVMFSub
182 Mul Opcode = C.LLVMMul
183 FMul Opcode = C.LLVMFMul
184 UDiv Opcode = C.LLVMUDiv
185 SDiv Opcode = C.LLVMSDiv
186 FDiv Opcode = C.LLVMFDiv
187 URem Opcode = C.LLVMURem
188 SRem Opcode = C.LLVMSRem
189 FRem Opcode = C.LLVMFRem
192 Shl Opcode = C.LLVMShl
193 LShr Opcode = C.LLVMLShr
194 AShr Opcode = C.LLVMAShr
195 And Opcode = C.LLVMAnd
197 Xor Opcode = C.LLVMXor
200 Alloca Opcode = C.LLVMAlloca
201 Load Opcode = C.LLVMLoad
202 Store Opcode = C.LLVMStore
203 GetElementPtr Opcode = C.LLVMGetElementPtr
206 Trunc Opcode = C.LLVMTrunc
207 ZExt Opcode = C.LLVMZExt
208 SExt Opcode = C.LLVMSExt
209 FPToUI Opcode = C.LLVMFPToUI
210 FPToSI Opcode = C.LLVMFPToSI
211 UIToFP Opcode = C.LLVMUIToFP
212 SIToFP Opcode = C.LLVMSIToFP
213 FPTrunc Opcode = C.LLVMFPTrunc
214 FPExt Opcode = C.LLVMFPExt
215 PtrToInt Opcode = C.LLVMPtrToInt
216 IntToPtr Opcode = C.LLVMIntToPtr
217 BitCast Opcode = C.LLVMBitCast
220 ICmp Opcode = C.LLVMICmp
221 FCmp Opcode = C.LLVMFCmp
222 PHI Opcode = C.LLVMPHI
223 Call Opcode = C.LLVMCall
224 Select Opcode = C.LLVMSelect
227 VAArg Opcode = C.LLVMVAArg
228 ExtractElement Opcode = C.LLVMExtractElement
229 InsertElement Opcode = C.LLVMInsertElement
230 ShuffleVector Opcode = C.LLVMShuffleVector
231 ExtractValue Opcode = C.LLVMExtractValue
232 InsertValue Opcode = C.LLVMInsertValue
235 //-------------------------------------------------------------------------
237 //-------------------------------------------------------------------------
240 VoidTypeKind TypeKind = C.LLVMVoidTypeKind
241 FloatTypeKind TypeKind = C.LLVMFloatTypeKind
242 DoubleTypeKind TypeKind = C.LLVMDoubleTypeKind
243 X86_FP80TypeKind TypeKind = C.LLVMX86_FP80TypeKind
244 FP128TypeKind TypeKind = C.LLVMFP128TypeKind
245 PPC_FP128TypeKind TypeKind = C.LLVMPPC_FP128TypeKind
246 LabelTypeKind TypeKind = C.LLVMLabelTypeKind
247 IntegerTypeKind TypeKind = C.LLVMIntegerTypeKind
248 FunctionTypeKind TypeKind = C.LLVMFunctionTypeKind
249 StructTypeKind TypeKind = C.LLVMStructTypeKind
250 ArrayTypeKind TypeKind = C.LLVMArrayTypeKind
251 PointerTypeKind TypeKind = C.LLVMPointerTypeKind
252 VectorTypeKind TypeKind = C.LLVMVectorTypeKind
253 MetadataTypeKind TypeKind = C.LLVMMetadataTypeKind
256 //-------------------------------------------------------------------------
258 //-------------------------------------------------------------------------
261 ExternalLinkage Linkage = C.LLVMExternalLinkage
262 AvailableExternallyLinkage Linkage = C.LLVMAvailableExternallyLinkage
263 LinkOnceAnyLinkage Linkage = C.LLVMLinkOnceAnyLinkage
264 LinkOnceODRLinkage Linkage = C.LLVMLinkOnceODRLinkage
265 WeakAnyLinkage Linkage = C.LLVMWeakAnyLinkage
266 WeakODRLinkage Linkage = C.LLVMWeakODRLinkage
267 AppendingLinkage Linkage = C.LLVMAppendingLinkage
268 InternalLinkage Linkage = C.LLVMInternalLinkage
269 PrivateLinkage Linkage = C.LLVMPrivateLinkage
270 ExternalWeakLinkage Linkage = C.LLVMExternalWeakLinkage
271 CommonLinkage Linkage = C.LLVMCommonLinkage
274 //-------------------------------------------------------------------------
276 //-------------------------------------------------------------------------
279 DefaultVisibility Visibility = C.LLVMDefaultVisibility
280 HiddenVisibility Visibility = C.LLVMHiddenVisibility
281 ProtectedVisibility Visibility = C.LLVMProtectedVisibility
284 //-------------------------------------------------------------------------
286 //-------------------------------------------------------------------------
289 CCallConv CallConv = C.LLVMCCallConv
290 FastCallConv CallConv = C.LLVMFastCallConv
291 ColdCallConv CallConv = C.LLVMColdCallConv
292 X86StdcallCallConv CallConv = C.LLVMX86StdcallCallConv
293 X86FastcallCallConv CallConv = C.LLVMX86FastcallCallConv
296 //-------------------------------------------------------------------------
298 //-------------------------------------------------------------------------
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 //-------------------------------------------------------------------------
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 //-------------------------------------------------------------------------
341 LandingPadCatch LandingPadClause = C.LLVMLandingPadCatch
342 LandingPadFilter LandingPadClause = C.LLVMLandingPadFilter
345 //-------------------------------------------------------------------------
347 //-------------------------------------------------------------------------
349 func NewContext() Context { return Context{C.LLVMContextCreate()} }
350 func GlobalContext() Context { return Context{C.LLVMGetGlobalContext()} }
351 func (c Context) Dispose() { C.LLVMContextDispose(c.C) }
353 func (c Context) MDKindID(name string) (id int) {
354 cname := C.CString(name)
355 defer C.free(unsafe.Pointer(cname))
356 id = int(C.LLVMGetMDKindIDInContext(c.C, cname, C.unsigned(len(name))))
360 func MDKindID(name string) (id int) {
361 cname := C.CString(name)
362 defer C.free(unsafe.Pointer(cname))
363 id = int(C.LLVMGetMDKindID(cname, C.unsigned(len(name))))
367 //-------------------------------------------------------------------------
369 //-------------------------------------------------------------------------
371 // Create and destroy modules.
372 // See llvm::Module::Module.
373 func NewModule(name string) (m Module) {
374 cname := C.CString(name)
375 defer C.free(unsafe.Pointer(cname))
376 m.C = C.LLVMModuleCreateWithName(cname)
380 func (c Context) NewModule(name string) (m Module) {
381 cname := C.CString(name)
382 defer C.free(unsafe.Pointer(cname))
383 m.C = C.LLVMModuleCreateWithNameInContext(cname, c.C)
387 // See llvm::Module::~Module
388 func (m Module) Dispose() { C.LLVMDisposeModule(m.C) }
390 // Data layout. See Module::getDataLayout.
391 func (m Module) DataLayout() string {
392 clayout := C.LLVMGetDataLayout(m.C)
393 return C.GoString(clayout)
396 func (m Module) SetDataLayout(layout string) {
397 clayout := C.CString(layout)
398 defer C.free(unsafe.Pointer(clayout))
399 C.LLVMSetDataLayout(m.C, clayout)
402 // Target triple. See Module::getTargetTriple.
403 func (m Module) Target() string {
404 ctarget := C.LLVMGetTarget(m.C)
405 return C.GoString(ctarget)
407 func (m Module) SetTarget(target string) {
408 ctarget := C.CString(target)
409 defer C.free(unsafe.Pointer(ctarget))
410 C.LLVMSetTarget(m.C, ctarget)
413 func (m Module) GetTypeByName(name string) (t Type) {
414 cname := C.CString(name)
415 defer C.free(unsafe.Pointer(cname))
416 t.C = C.LLVMGetTypeByName(m.C, cname)
421 func (m Module) Dump() {
422 C.LLVMDumpModule(m.C)
425 func (m Module) String() string {
426 cir := C.LLVMPrintModuleToString(m.C)
427 defer C.free(unsafe.Pointer(cir))
428 ir := C.GoString(cir)
432 // See Module::setModuleInlineAsm.
433 func (m Module) SetInlineAsm(asm string) {
434 casm := C.CString(asm)
435 defer C.free(unsafe.Pointer(casm))
436 C.LLVMSetModuleInlineAsm(m.C, casm)
439 func (m Module) AddNamedMetadataOperand(name string, operand Metadata) {
440 cname := C.CString(name)
441 defer C.free(unsafe.Pointer(cname))
442 C.LLVMAddNamedMetadataOperand2(m.C, cname, operand.C)
445 func (m Module) Context() (c Context) {
446 c.C = C.LLVMGetModuleContext(m.C)
450 //-------------------------------------------------------------------------
452 //-------------------------------------------------------------------------
454 // LLVM types conform to the following hierarchy:
468 // See llvm::LLVMTypeKind::getTypeID.
469 func (t Type) TypeKind() TypeKind { return TypeKind(C.LLVMGetTypeKind(t.C)) }
471 // See llvm::LLVMType::getContext.
472 func (t Type) Context() (c Context) {
473 c.C = C.LLVMGetTypeContext(t.C)
477 // Operations on integer types
478 func (c Context) Int1Type() (t Type) { t.C = C.LLVMInt1TypeInContext(c.C); return }
479 func (c Context) Int8Type() (t Type) { t.C = C.LLVMInt8TypeInContext(c.C); return }
480 func (c Context) Int16Type() (t Type) { t.C = C.LLVMInt16TypeInContext(c.C); return }
481 func (c Context) Int32Type() (t Type) { t.C = C.LLVMInt32TypeInContext(c.C); return }
482 func (c Context) Int64Type() (t Type) { t.C = C.LLVMInt64TypeInContext(c.C); return }
483 func (c Context) IntType(numbits int) (t Type) {
484 t.C = C.LLVMIntTypeInContext(c.C, C.unsigned(numbits))
488 func Int1Type() (t Type) { t.C = C.LLVMInt1Type(); return }
489 func Int8Type() (t Type) { t.C = C.LLVMInt8Type(); return }
490 func Int16Type() (t Type) { t.C = C.LLVMInt16Type(); return }
491 func Int32Type() (t Type) { t.C = C.LLVMInt32Type(); return }
492 func Int64Type() (t Type) { t.C = C.LLVMInt64Type(); return }
494 func IntType(numbits int) (t Type) {
495 t.C = C.LLVMIntType(C.unsigned(numbits))
499 func (t Type) IntTypeWidth() int {
500 return int(C.LLVMGetIntTypeWidth(t.C))
503 // Operations on real types
504 func (c Context) FloatType() (t Type) { t.C = C.LLVMFloatTypeInContext(c.C); return }
505 func (c Context) DoubleType() (t Type) { t.C = C.LLVMDoubleTypeInContext(c.C); return }
506 func (c Context) X86FP80Type() (t Type) { t.C = C.LLVMX86FP80TypeInContext(c.C); return }
507 func (c Context) FP128Type() (t Type) { t.C = C.LLVMFP128TypeInContext(c.C); return }
508 func (c Context) PPCFP128Type() (t Type) { t.C = C.LLVMPPCFP128TypeInContext(c.C); return }
510 func FloatType() (t Type) { t.C = C.LLVMFloatType(); return }
511 func DoubleType() (t Type) { t.C = C.LLVMDoubleType(); return }
512 func X86FP80Type() (t Type) { t.C = C.LLVMX86FP80Type(); return }
513 func FP128Type() (t Type) { t.C = C.LLVMFP128Type(); return }
514 func PPCFP128Type() (t Type) { t.C = C.LLVMPPCFP128Type(); return }
516 // Operations on function types
517 func FunctionType(returnType Type, paramTypes []Type, isVarArg bool) (t Type) {
518 var pt *C.LLVMTypeRef
520 if len(paramTypes) > 0 {
521 pt = llvmTypeRefPtr(¶mTypes[0])
522 ptlen = C.unsigned(len(paramTypes))
524 t.C = C.LLVMFunctionType(returnType.C,
527 boolToLLVMBool(isVarArg))
531 func (t Type) IsFunctionVarArg() bool { return C.LLVMIsFunctionVarArg(t.C) != 0 }
532 func (t Type) ReturnType() (rt Type) { rt.C = C.LLVMGetReturnType(t.C); return }
533 func (t Type) ParamTypesCount() int { return int(C.LLVMCountParamTypes(t.C)) }
534 func (t Type) ParamTypes() []Type {
535 count := t.ParamTypesCount()
537 out := make([]Type, count)
538 C.LLVMGetParamTypes(t.C, llvmTypeRefPtr(&out[0]))
544 // Operations on struct types
545 func (c Context) StructType(elementTypes []Type, packed bool) (t Type) {
546 var pt *C.LLVMTypeRef
548 if len(elementTypes) > 0 {
549 pt = llvmTypeRefPtr(&elementTypes[0])
550 ptlen = C.unsigned(len(elementTypes))
552 t.C = C.LLVMStructTypeInContext(c.C,
555 boolToLLVMBool(packed))
559 func StructType(elementTypes []Type, packed bool) (t Type) {
560 var pt *C.LLVMTypeRef
562 if len(elementTypes) > 0 {
563 pt = llvmTypeRefPtr(&elementTypes[0])
564 ptlen = C.unsigned(len(elementTypes))
566 t.C = C.LLVMStructType(pt, ptlen, boolToLLVMBool(packed))
570 func (c Context) StructCreateNamed(name string) (t Type) {
571 cname := C.CString(name)
572 defer C.free(unsafe.Pointer(cname))
573 t.C = C.LLVMStructCreateNamed(c.C, cname)
577 func (t Type) StructName() string {
578 return C.GoString(C.LLVMGetStructName(t.C))
581 func (t Type) StructSetBody(elementTypes []Type, packed bool) {
582 var pt *C.LLVMTypeRef
584 if len(elementTypes) > 0 {
585 pt = llvmTypeRefPtr(&elementTypes[0])
586 ptlen = C.unsigned(len(elementTypes))
588 C.LLVMStructSetBody(t.C, pt, ptlen, boolToLLVMBool(packed))
591 func (t Type) IsStructPacked() bool { return C.LLVMIsPackedStruct(t.C) != 0 }
592 func (t Type) StructElementTypesCount() int { return int(C.LLVMCountStructElementTypes(t.C)) }
593 func (t Type) StructElementTypes() []Type {
594 out := make([]Type, t.StructElementTypesCount())
596 C.LLVMGetStructElementTypes(t.C, llvmTypeRefPtr(&out[0]))
601 // Operations on array, pointer, and vector types (sequence types)
602 func ArrayType(elementType Type, elementCount int) (t Type) {
603 t.C = C.LLVMArrayType(elementType.C, C.unsigned(elementCount))
606 func PointerType(elementType Type, addressSpace int) (t Type) {
607 t.C = C.LLVMPointerType(elementType.C, C.unsigned(addressSpace))
610 func VectorType(elementType Type, elementCount int) (t Type) {
611 t.C = C.LLVMVectorType(elementType.C, C.unsigned(elementCount))
615 func (t Type) ElementType() (rt Type) { rt.C = C.LLVMGetElementType(t.C); return }
616 func (t Type) ArrayLength() int { return int(C.LLVMGetArrayLength(t.C)) }
617 func (t Type) PointerAddressSpace() int { return int(C.LLVMGetPointerAddressSpace(t.C)) }
618 func (t Type) VectorSize() int { return int(C.LLVMGetVectorSize(t.C)) }
620 // Operations on other types
621 func (c Context) VoidType() (t Type) { t.C = C.LLVMVoidTypeInContext(c.C); return }
622 func (c Context) LabelType() (t Type) { t.C = C.LLVMLabelTypeInContext(c.C); return }
624 func VoidType() (t Type) { t.C = C.LLVMVoidType(); return }
625 func LabelType() (t Type) { t.C = C.LLVMLabelType(); return }
627 //-------------------------------------------------------------------------
629 //-------------------------------------------------------------------------
631 // Operations on all values
632 func (v Value) Type() (t Type) { t.C = C.LLVMTypeOf(v.C); return }
633 func (v Value) Name() string { return C.GoString(C.LLVMGetValueName(v.C)) }
634 func (v Value) SetName(name string) {
635 cname := C.CString(name)
636 defer C.free(unsafe.Pointer(cname))
637 C.LLVMSetValueName(v.C, cname)
639 func (v Value) Dump() { C.LLVMDumpValue(v.C) }
640 func (v Value) ReplaceAllUsesWith(nv Value) { C.LLVMReplaceAllUsesWith(v.C, nv.C) }
641 func (v Value) HasMetadata() bool { return C.LLVMHasMetadata(v.C) != 0 }
642 func (v Value) Metadata(kind int) (rv Value) {
643 rv.C = C.LLVMGetMetadata(v.C, C.unsigned(kind))
646 func (v Value) SetMetadata(kind int, node Metadata) {
647 C.LLVMSetMetadata2(v.C, C.unsigned(kind), node.C)
650 // Conversion functions.
651 // Return the input value if it is an instance of the specified class, otherwise NULL.
652 // See llvm::dyn_cast_or_null<>.
653 func (v Value) IsAArgument() (rv Value) { rv.C = C.LLVMIsAArgument(v.C); return }
654 func (v Value) IsABasicBlock() (rv Value) { rv.C = C.LLVMIsABasicBlock(v.C); return }
655 func (v Value) IsAInlineAsm() (rv Value) { rv.C = C.LLVMIsAInlineAsm(v.C); return }
656 func (v Value) IsAUser() (rv Value) { rv.C = C.LLVMIsAUser(v.C); return }
657 func (v Value) IsAConstant() (rv Value) { rv.C = C.LLVMIsAConstant(v.C); return }
658 func (v Value) IsAConstantAggregateZero() (rv Value) {
659 rv.C = C.LLVMIsAConstantAggregateZero(v.C)
662 func (v Value) IsAConstantArray() (rv Value) { rv.C = C.LLVMIsAConstantArray(v.C); return }
663 func (v Value) IsAConstantExpr() (rv Value) { rv.C = C.LLVMIsAConstantExpr(v.C); return }
664 func (v Value) IsAConstantFP() (rv Value) { rv.C = C.LLVMIsAConstantFP(v.C); return }
665 func (v Value) IsAConstantInt() (rv Value) { rv.C = C.LLVMIsAConstantInt(v.C); return }
666 func (v Value) IsAConstantPointerNull() (rv Value) { rv.C = C.LLVMIsAConstantPointerNull(v.C); return }
667 func (v Value) IsAConstantStruct() (rv Value) { rv.C = C.LLVMIsAConstantStruct(v.C); return }
668 func (v Value) IsAConstantVector() (rv Value) { rv.C = C.LLVMIsAConstantVector(v.C); return }
669 func (v Value) IsAGlobalValue() (rv Value) { rv.C = C.LLVMIsAGlobalValue(v.C); return }
670 func (v Value) IsAFunction() (rv Value) { rv.C = C.LLVMIsAFunction(v.C); return }
671 func (v Value) IsAGlobalAlias() (rv Value) { rv.C = C.LLVMIsAGlobalAlias(v.C); return }
672 func (v Value) IsAGlobalVariable() (rv Value) { rv.C = C.LLVMIsAGlobalVariable(v.C); return }
673 func (v Value) IsAUndefValue() (rv Value) { rv.C = C.LLVMIsAUndefValue(v.C); return }
674 func (v Value) IsAInstruction() (rv Value) { rv.C = C.LLVMIsAInstruction(v.C); return }
675 func (v Value) IsABinaryOperator() (rv Value) { rv.C = C.LLVMIsABinaryOperator(v.C); return }
676 func (v Value) IsACallInst() (rv Value) { rv.C = C.LLVMIsACallInst(v.C); return }
677 func (v Value) IsAIntrinsicInst() (rv Value) { rv.C = C.LLVMIsAIntrinsicInst(v.C); return }
678 func (v Value) IsADbgInfoIntrinsic() (rv Value) { rv.C = C.LLVMIsADbgInfoIntrinsic(v.C); return }
679 func (v Value) IsADbgDeclareInst() (rv Value) { rv.C = C.LLVMIsADbgDeclareInst(v.C); return }
680 func (v Value) IsAMemIntrinsic() (rv Value) { rv.C = C.LLVMIsAMemIntrinsic(v.C); return }
681 func (v Value) IsAMemCpyInst() (rv Value) { rv.C = C.LLVMIsAMemCpyInst(v.C); return }
682 func (v Value) IsAMemMoveInst() (rv Value) { rv.C = C.LLVMIsAMemMoveInst(v.C); return }
683 func (v Value) IsAMemSetInst() (rv Value) { rv.C = C.LLVMIsAMemSetInst(v.C); return }
684 func (v Value) IsACmpInst() (rv Value) { rv.C = C.LLVMIsACmpInst(v.C); return }
685 func (v Value) IsAFCmpInst() (rv Value) { rv.C = C.LLVMIsAFCmpInst(v.C); return }
686 func (v Value) IsAICmpInst() (rv Value) { rv.C = C.LLVMIsAICmpInst(v.C); return }
687 func (v Value) IsAExtractElementInst() (rv Value) { rv.C = C.LLVMIsAExtractElementInst(v.C); return }
688 func (v Value) IsAGetElementPtrInst() (rv Value) { rv.C = C.LLVMIsAGetElementPtrInst(v.C); return }
689 func (v Value) IsAInsertElementInst() (rv Value) { rv.C = C.LLVMIsAInsertElementInst(v.C); return }
690 func (v Value) IsAInsertValueInst() (rv Value) { rv.C = C.LLVMIsAInsertValueInst(v.C); return }
691 func (v Value) IsAPHINode() (rv Value) { rv.C = C.LLVMIsAPHINode(v.C); return }
692 func (v Value) IsASelectInst() (rv Value) { rv.C = C.LLVMIsASelectInst(v.C); return }
693 func (v Value) IsAShuffleVectorInst() (rv Value) { rv.C = C.LLVMIsAShuffleVectorInst(v.C); return }
694 func (v Value) IsAStoreInst() (rv Value) { rv.C = C.LLVMIsAStoreInst(v.C); return }
695 func (v Value) IsATerminatorInst() (rv Value) { rv.C = C.LLVMIsATerminatorInst(v.C); return }
696 func (v Value) IsABranchInst() (rv Value) { rv.C = C.LLVMIsABranchInst(v.C); return }
697 func (v Value) IsAInvokeInst() (rv Value) { rv.C = C.LLVMIsAInvokeInst(v.C); return }
698 func (v Value) IsAReturnInst() (rv Value) { rv.C = C.LLVMIsAReturnInst(v.C); return }
699 func (v Value) IsASwitchInst() (rv Value) { rv.C = C.LLVMIsASwitchInst(v.C); return }
700 func (v Value) IsAUnreachableInst() (rv Value) { rv.C = C.LLVMIsAUnreachableInst(v.C); return }
701 func (v Value) IsAUnaryInstruction() (rv Value) { rv.C = C.LLVMIsAUnaryInstruction(v.C); return }
702 func (v Value) IsAAllocaInst() (rv Value) { rv.C = C.LLVMIsAAllocaInst(v.C); return }
703 func (v Value) IsACastInst() (rv Value) { rv.C = C.LLVMIsACastInst(v.C); return }
704 func (v Value) IsABitCastInst() (rv Value) { rv.C = C.LLVMIsABitCastInst(v.C); return }
705 func (v Value) IsAFPExtInst() (rv Value) { rv.C = C.LLVMIsAFPExtInst(v.C); return }
706 func (v Value) IsAFPToSIInst() (rv Value) { rv.C = C.LLVMIsAFPToSIInst(v.C); return }
707 func (v Value) IsAFPToUIInst() (rv Value) { rv.C = C.LLVMIsAFPToUIInst(v.C); return }
708 func (v Value) IsAFPTruncInst() (rv Value) { rv.C = C.LLVMIsAFPTruncInst(v.C); return }
709 func (v Value) IsAIntToPtrInst() (rv Value) { rv.C = C.LLVMIsAIntToPtrInst(v.C); return }
710 func (v Value) IsAPtrToIntInst() (rv Value) { rv.C = C.LLVMIsAPtrToIntInst(v.C); return }
711 func (v Value) IsASExtInst() (rv Value) { rv.C = C.LLVMIsASExtInst(v.C); return }
712 func (v Value) IsASIToFPInst() (rv Value) { rv.C = C.LLVMIsASIToFPInst(v.C); return }
713 func (v Value) IsATruncInst() (rv Value) { rv.C = C.LLVMIsATruncInst(v.C); return }
714 func (v Value) IsAUIToFPInst() (rv Value) { rv.C = C.LLVMIsAUIToFPInst(v.C); return }
715 func (v Value) IsAZExtInst() (rv Value) { rv.C = C.LLVMIsAZExtInst(v.C); return }
716 func (v Value) IsAExtractValueInst() (rv Value) { rv.C = C.LLVMIsAExtractValueInst(v.C); return }
717 func (v Value) IsALoadInst() (rv Value) { rv.C = C.LLVMIsALoadInst(v.C); return }
718 func (v Value) IsAVAArgInst() (rv Value) { rv.C = C.LLVMIsAVAArgInst(v.C); return }
720 // Operations on Uses
721 func (v Value) FirstUse() (u Use) { u.C = C.LLVMGetFirstUse(v.C); return }
722 func (u Use) NextUse() (ru Use) { ru.C = C.LLVMGetNextUse(u.C); return }
723 func (u Use) User() (v Value) { v.C = C.LLVMGetUser(u.C); return }
724 func (u Use) UsedValue() (v Value) { v.C = C.LLVMGetUsedValue(u.C); return }
726 // Operations on Users
727 func (v Value) Operand(i int) (rv Value) { rv.C = C.LLVMGetOperand(v.C, C.unsigned(i)); return }
728 func (v Value) SetOperand(i int, op Value) { C.LLVMSetOperand(v.C, C.unsigned(i), op.C) }
729 func (v Value) OperandsCount() int { return int(C.LLVMGetNumOperands(v.C)) }
731 // Operations on constants of any type
732 func ConstNull(t Type) (v Value) { v.C = C.LLVMConstNull(t.C); return }
733 func ConstAllOnes(t Type) (v Value) { v.C = C.LLVMConstAllOnes(t.C); return }
734 func Undef(t Type) (v Value) { v.C = C.LLVMGetUndef(t.C); return }
735 func (v Value) IsConstant() bool { return C.LLVMIsConstant(v.C) != 0 }
736 func (v Value) IsNull() bool { return C.LLVMIsNull(v.C) != 0 }
737 func (v Value) IsUndef() bool { return C.LLVMIsUndef(v.C) != 0 }
738 func ConstPointerNull(t Type) (v Value) { v.C = C.LLVMConstPointerNull(t.C); return }
740 // Operations on metadata
741 func (c Context) MDString(str string) (md Metadata) {
742 cstr := C.CString(str)
743 defer C.free(unsafe.Pointer(cstr))
744 md.C = C.LLVMMDString2(c.C, cstr, C.unsigned(len(str)))
747 func (c Context) MDNode(mds []Metadata) (md Metadata) {
748 ptr, nvals := llvmMetadataRefs(mds)
749 md.C = C.LLVMMDNode2(c.C, ptr, nvals)
753 // Operations on scalar constants
754 func ConstInt(t Type, n uint64, signExtend bool) (v Value) {
755 v.C = C.LLVMConstInt(t.C,
757 boolToLLVMBool(signExtend))
760 func ConstIntFromString(t Type, str string, radix int) (v Value) {
761 cstr := C.CString(str)
762 defer C.free(unsafe.Pointer(cstr))
763 v.C = C.LLVMConstIntOfString(t.C, cstr, C.uint8_t(radix))
766 func ConstFloat(t Type, n float64) (v Value) {
767 v.C = C.LLVMConstReal(t.C, C.double(n))
770 func ConstFloatFromString(t Type, str string) (v Value) {
771 cstr := C.CString(str)
772 defer C.free(unsafe.Pointer(cstr))
773 v.C = C.LLVMConstRealOfString(t.C, cstr)
777 func (v Value) ZExtValue() uint64 { return uint64(C.LLVMConstIntGetZExtValue(v.C)) }
778 func (v Value) SExtValue() int64 { return int64(C.LLVMConstIntGetSExtValue(v.C)) }
780 // Operations on composite constants
781 func (c Context) ConstString(str string, addnull bool) (v Value) {
782 cstr := C.CString(str)
783 defer C.free(unsafe.Pointer(cstr))
784 v.C = C.LLVMConstStringInContext(c.C, cstr,
785 C.unsigned(len(str)), boolToLLVMBool(!addnull))
788 func (c Context) ConstStruct(constVals []Value, packed bool) (v Value) {
789 ptr, nvals := llvmValueRefs(constVals)
790 v.C = C.LLVMConstStructInContext(c.C, ptr, nvals,
791 boolToLLVMBool(packed))
794 func ConstNamedStruct(t Type, constVals []Value) (v Value) {
795 ptr, nvals := llvmValueRefs(constVals)
796 v.C = C.LLVMConstNamedStruct(t.C, ptr, nvals)
799 func ConstString(str string, addnull bool) (v Value) {
800 cstr := C.CString(str)
801 defer C.free(unsafe.Pointer(cstr))
802 v.C = C.LLVMConstString(cstr,
803 C.unsigned(len(str)), boolToLLVMBool(!addnull))
806 func ConstArray(t Type, constVals []Value) (v Value) {
807 ptr, nvals := llvmValueRefs(constVals)
808 v.C = C.LLVMConstArray(t.C, ptr, nvals)
811 func ConstStruct(constVals []Value, packed bool) (v Value) {
812 ptr, nvals := llvmValueRefs(constVals)
813 v.C = C.LLVMConstStruct(ptr, nvals, boolToLLVMBool(packed))
816 func ConstVector(scalarConstVals []Value, packed bool) (v Value) {
817 ptr, nvals := llvmValueRefs(scalarConstVals)
818 v.C = C.LLVMConstVector(ptr, nvals)
822 // Constant expressions
823 func (v Value) Opcode() Opcode { return Opcode(C.LLVMGetConstOpcode(v.C)) }
824 func (v Value) InstructionOpcode() Opcode { return Opcode(C.LLVMGetInstructionOpcode(v.C)) }
825 func AlignOf(t Type) (v Value) { v.C = C.LLVMAlignOf(t.C); return }
826 func SizeOf(t Type) (v Value) { v.C = C.LLVMSizeOf(t.C); return }
827 func ConstNeg(v Value) (rv Value) { rv.C = C.LLVMConstNeg(v.C); return }
828 func ConstNSWNeg(v Value) (rv Value) { rv.C = C.LLVMConstNSWNeg(v.C); return }
829 func ConstNUWNeg(v Value) (rv Value) { rv.C = C.LLVMConstNUWNeg(v.C); return }
830 func ConstFNeg(v Value) (rv Value) { rv.C = C.LLVMConstFNeg(v.C); return }
831 func ConstNot(v Value) (rv Value) { rv.C = C.LLVMConstNot(v.C); return }
832 func ConstAdd(lhs, rhs Value) (v Value) { v.C = C.LLVMConstAdd(lhs.C, rhs.C); return }
833 func ConstNSWAdd(lhs, rhs Value) (v Value) { v.C = C.LLVMConstNSWAdd(lhs.C, rhs.C); return }
834 func ConstNUWAdd(lhs, rhs Value) (v Value) { v.C = C.LLVMConstNUWAdd(lhs.C, rhs.C); return }
835 func ConstFAdd(lhs, rhs Value) (v Value) { v.C = C.LLVMConstFAdd(lhs.C, rhs.C); return }
836 func ConstSub(lhs, rhs Value) (v Value) { v.C = C.LLVMConstSub(lhs.C, rhs.C); return }
837 func ConstNSWSub(lhs, rhs Value) (v Value) { v.C = C.LLVMConstNSWSub(lhs.C, rhs.C); return }
838 func ConstNUWSub(lhs, rhs Value) (v Value) { v.C = C.LLVMConstNUWSub(lhs.C, rhs.C); return }
839 func ConstFSub(lhs, rhs Value) (v Value) { v.C = C.LLVMConstFSub(lhs.C, rhs.C); return }
840 func ConstMul(lhs, rhs Value) (v Value) { v.C = C.LLVMConstMul(lhs.C, rhs.C); return }
841 func ConstNSWMul(lhs, rhs Value) (v Value) { v.C = C.LLVMConstNSWMul(lhs.C, rhs.C); return }
842 func ConstNUWMul(lhs, rhs Value) (v Value) { v.C = C.LLVMConstNUWMul(lhs.C, rhs.C); return }
843 func ConstFMul(lhs, rhs Value) (v Value) { v.C = C.LLVMConstFMul(lhs.C, rhs.C); return }
844 func ConstUDiv(lhs, rhs Value) (v Value) { v.C = C.LLVMConstUDiv(lhs.C, rhs.C); return }
845 func ConstSDiv(lhs, rhs Value) (v Value) { v.C = C.LLVMConstSDiv(lhs.C, rhs.C); return }
846 func ConstExactSDiv(lhs, rhs Value) (v Value) { v.C = C.LLVMConstExactSDiv(lhs.C, rhs.C); return }
847 func ConstFDiv(lhs, rhs Value) (v Value) { v.C = C.LLVMConstFDiv(lhs.C, rhs.C); return }
848 func ConstURem(lhs, rhs Value) (v Value) { v.C = C.LLVMConstURem(lhs.C, rhs.C); return }
849 func ConstSRem(lhs, rhs Value) (v Value) { v.C = C.LLVMConstSRem(lhs.C, rhs.C); return }
850 func ConstFRem(lhs, rhs Value) (v Value) { v.C = C.LLVMConstFRem(lhs.C, rhs.C); return }
851 func ConstAnd(lhs, rhs Value) (v Value) { v.C = C.LLVMConstAnd(lhs.C, rhs.C); return }
852 func ConstOr(lhs, rhs Value) (v Value) { v.C = C.LLVMConstOr(lhs.C, rhs.C); return }
853 func ConstXor(lhs, rhs Value) (v Value) { v.C = C.LLVMConstXor(lhs.C, rhs.C); return }
855 func ConstICmp(pred IntPredicate, lhs, rhs Value) (v Value) {
856 v.C = C.LLVMConstICmp(C.LLVMIntPredicate(pred), lhs.C, rhs.C)
859 func ConstFCmp(pred FloatPredicate, lhs, rhs Value) (v Value) {
860 v.C = C.LLVMConstFCmp(C.LLVMRealPredicate(pred), lhs.C, rhs.C)
864 func ConstShl(lhs, rhs Value) (v Value) { v.C = C.LLVMConstShl(lhs.C, rhs.C); return }
865 func ConstLShr(lhs, rhs Value) (v Value) { v.C = C.LLVMConstLShr(lhs.C, rhs.C); return }
866 func ConstAShr(lhs, rhs Value) (v Value) { v.C = C.LLVMConstAShr(lhs.C, rhs.C); return }
868 func ConstGEP(v Value, indices []Value) (rv Value) {
869 ptr, nvals := llvmValueRefs(indices)
870 rv.C = C.LLVMConstGEP(v.C, ptr, nvals)
873 func ConstInBoundsGEP(v Value, indices []Value) (rv Value) {
874 ptr, nvals := llvmValueRefs(indices)
875 rv.C = C.LLVMConstInBoundsGEP(v.C, ptr, nvals)
878 func ConstTrunc(v Value, t Type) (rv Value) { rv.C = C.LLVMConstTrunc(v.C, t.C); return }
879 func ConstSExt(v Value, t Type) (rv Value) { rv.C = C.LLVMConstSExt(v.C, t.C); return }
880 func ConstZExt(v Value, t Type) (rv Value) { rv.C = C.LLVMConstZExt(v.C, t.C); return }
881 func ConstFPTrunc(v Value, t Type) (rv Value) { rv.C = C.LLVMConstFPTrunc(v.C, t.C); return }
882 func ConstFPExt(v Value, t Type) (rv Value) { rv.C = C.LLVMConstFPExt(v.C, t.C); return }
883 func ConstUIToFP(v Value, t Type) (rv Value) { rv.C = C.LLVMConstUIToFP(v.C, t.C); return }
884 func ConstSIToFP(v Value, t Type) (rv Value) { rv.C = C.LLVMConstSIToFP(v.C, t.C); return }
885 func ConstFPToUI(v Value, t Type) (rv Value) { rv.C = C.LLVMConstFPToUI(v.C, t.C); return }
886 func ConstFPToSI(v Value, t Type) (rv Value) { rv.C = C.LLVMConstFPToSI(v.C, t.C); return }
887 func ConstPtrToInt(v Value, t Type) (rv Value) { rv.C = C.LLVMConstPtrToInt(v.C, t.C); return }
888 func ConstIntToPtr(v Value, t Type) (rv Value) { rv.C = C.LLVMConstIntToPtr(v.C, t.C); return }
889 func ConstBitCast(v Value, t Type) (rv Value) { rv.C = C.LLVMConstBitCast(v.C, t.C); return }
890 func ConstZExtOrBitCast(v Value, t Type) (rv Value) { rv.C = C.LLVMConstZExtOrBitCast(v.C, t.C); return }
891 func ConstSExtOrBitCast(v Value, t Type) (rv Value) { rv.C = C.LLVMConstSExtOrBitCast(v.C, t.C); return }
892 func ConstTruncOrBitCast(v Value, t Type) (rv Value) {
893 rv.C = C.LLVMConstTruncOrBitCast(v.C, t.C)
896 func ConstPointerCast(v Value, t Type) (rv Value) { rv.C = C.LLVMConstPointerCast(v.C, t.C); return }
897 func ConstIntCast(v Value, t Type, signed bool) (rv Value) {
898 rv.C = C.LLVMConstIntCast(v.C, t.C, boolToLLVMBool(signed))
901 func ConstFPCast(v Value, t Type) (rv Value) { rv.C = C.LLVMConstFPCast(v.C, t.C); return }
902 func ConstSelect(cond, iftrue, iffalse Value) (rv Value) {
903 rv.C = C.LLVMConstSelect(cond.C, iftrue.C, iffalse.C)
906 func ConstExtractElement(vec, i Value) (rv Value) {
907 rv.C = C.LLVMConstExtractElement(vec.C, i.C)
910 func ConstInsertElement(vec, elem, i Value) (rv Value) {
911 rv.C = C.LLVMConstInsertElement(vec.C, elem.C, i.C)
914 func ConstShuffleVector(veca, vecb, mask Value) (rv Value) {
915 rv.C = C.LLVMConstShuffleVector(veca.C, vecb.C, mask.C)
920 //LLVMValueRef LLVMConstExtractValue(LLVMValueRef AggConstant, unsigned *IdxList,
923 func ConstExtractValue(agg Value, indices []uint32) (rv Value) {
926 panic("one or more indices are required")
928 ptr := (*C.unsigned)(&indices[0])
929 rv.C = C.LLVMConstExtractValue(agg.C, ptr, C.unsigned(n))
933 func ConstInsertValue(agg, val Value, indices []uint32) (rv Value) {
936 panic("one or more indices are required")
938 ptr := (*C.unsigned)(&indices[0])
939 rv.C = C.LLVMConstInsertValue(agg.C, val.C, ptr, C.unsigned(n))
943 func BlockAddress(f Value, bb BasicBlock) (v Value) {
944 v.C = C.LLVMBlockAddress(f.C, bb.C)
948 // Operations on global variables, functions, and aliases (globals)
949 func (v Value) GlobalParent() (m Module) { m.C = C.LLVMGetGlobalParent(v.C); return }
950 func (v Value) IsDeclaration() bool { return C.LLVMIsDeclaration(v.C) != 0 }
951 func (v Value) Linkage() Linkage { return Linkage(C.LLVMGetLinkage(v.C)) }
952 func (v Value) SetLinkage(l Linkage) { C.LLVMSetLinkage(v.C, C.LLVMLinkage(l)) }
953 func (v Value) Section() string { return C.GoString(C.LLVMGetSection(v.C)) }
954 func (v Value) SetSection(str string) {
955 cstr := C.CString(str)
956 defer C.free(unsafe.Pointer(cstr))
957 C.LLVMSetSection(v.C, cstr)
959 func (v Value) Visibility() Visibility { return Visibility(C.LLVMGetVisibility(v.C)) }
960 func (v Value) SetVisibility(vi Visibility) { C.LLVMSetVisibility(v.C, C.LLVMVisibility(vi)) }
961 func (v Value) Alignment() int { return int(C.LLVMGetAlignment(v.C)) }
962 func (v Value) SetAlignment(a int) { C.LLVMSetAlignment(v.C, C.unsigned(a)) }
963 func (v Value) SetUnnamedAddr(ua bool) { C.LLVMSetUnnamedAddr(v.C, boolToLLVMBool(ua)) }
965 // Operations on global variables
966 func AddGlobal(m Module, t Type, name string) (v Value) {
967 cname := C.CString(name)
968 defer C.free(unsafe.Pointer(cname))
969 v.C = C.LLVMAddGlobal(m.C, t.C, cname)
972 func AddGlobalInAddressSpace(m Module, t Type, name string, addressSpace int) (v Value) {
973 cname := C.CString(name)
974 defer C.free(unsafe.Pointer(cname))
975 v.C = C.LLVMAddGlobalInAddressSpace(m.C, t.C, cname, C.unsigned(addressSpace))
978 func (m Module) NamedGlobal(name string) (v Value) {
979 cname := C.CString(name)
980 defer C.free(unsafe.Pointer(cname))
981 v.C = C.LLVMGetNamedGlobal(m.C, cname)
985 func (m Module) FirstGlobal() (v Value) { v.C = C.LLVMGetFirstGlobal(m.C); return }
986 func (m Module) LastGlobal() (v Value) { v.C = C.LLVMGetLastGlobal(m.C); return }
987 func NextGlobal(v Value) (rv Value) { rv.C = C.LLVMGetNextGlobal(v.C); return }
988 func PrevGlobal(v Value) (rv Value) { rv.C = C.LLVMGetPreviousGlobal(v.C); return }
989 func (v Value) EraseFromParentAsGlobal() { C.LLVMDeleteGlobal(v.C) }
990 func (v Value) Initializer() (rv Value) { rv.C = C.LLVMGetInitializer(v.C); return }
991 func (v Value) SetInitializer(cv Value) { C.LLVMSetInitializer(v.C, cv.C) }
992 func (v Value) IsThreadLocal() bool { return C.LLVMIsThreadLocal(v.C) != 0 }
993 func (v Value) SetThreadLocal(tl bool) { C.LLVMSetThreadLocal(v.C, boolToLLVMBool(tl)) }
994 func (v Value) IsGlobalConstant() bool { return C.LLVMIsGlobalConstant(v.C) != 0 }
995 func (v Value) SetGlobalConstant(gc bool) { C.LLVMSetGlobalConstant(v.C, boolToLLVMBool(gc)) }
997 // Operations on aliases
998 func AddAlias(m Module, t Type, aliasee Value, name string) (v Value) {
999 cname := C.CString(name)
1000 defer C.free(unsafe.Pointer(cname))
1001 v.C = C.LLVMAddAlias(m.C, t.C, aliasee.C, cname)
1005 // Operations on functions
1006 func AddFunction(m Module, name string, ft Type) (v Value) {
1007 cname := C.CString(name)
1008 defer C.free(unsafe.Pointer(cname))
1009 v.C = C.LLVMAddFunction(m.C, cname, ft.C)
1013 func (m Module) NamedFunction(name string) (v Value) {
1014 cname := C.CString(name)
1015 defer C.free(unsafe.Pointer(cname))
1016 v.C = C.LLVMGetNamedFunction(m.C, cname)
1020 func (m Module) FirstFunction() (v Value) { v.C = C.LLVMGetFirstFunction(m.C); return }
1021 func (m Module) LastFunction() (v Value) { v.C = C.LLVMGetLastFunction(m.C); return }
1022 func NextFunction(v Value) (rv Value) { rv.C = C.LLVMGetNextFunction(v.C); return }
1023 func PrevFunction(v Value) (rv Value) { rv.C = C.LLVMGetPreviousFunction(v.C); return }
1024 func (v Value) EraseFromParentAsFunction() { C.LLVMDeleteFunction(v.C) }
1025 func (v Value) IntrinsicID() int { return int(C.LLVMGetIntrinsicID(v.C)) }
1026 func (v Value) FunctionCallConv() CallConv {
1027 return CallConv(C.LLVMCallConv(C.LLVMGetFunctionCallConv(v.C)))
1029 func (v Value) SetFunctionCallConv(cc CallConv) { C.LLVMSetFunctionCallConv(v.C, C.unsigned(cc)) }
1030 func (v Value) GC() string { return C.GoString(C.LLVMGetGC(v.C)) }
1031 func (v Value) SetGC(name string) {
1032 cname := C.CString(name)
1033 defer C.free(unsafe.Pointer(cname))
1034 C.LLVMSetGC(v.C, cname)
1036 func (v Value) AddFunctionAttr(a Attribute) { C.LLVMAddFunctionAttr2(v.C, C.uint64_t(a)) }
1037 func (v Value) FunctionAttr() Attribute { return Attribute(C.LLVMGetFunctionAttr2(v.C)) }
1038 func (v Value) RemoveFunctionAttr(a Attribute) { C.LLVMRemoveFunctionAttr2(v.C, C.uint64_t(a)) }
1039 func (v Value) AddTargetDependentFunctionAttr(attr, value string) {
1040 cattr := C.CString(attr)
1041 defer C.free(unsafe.Pointer(cattr))
1042 cvalue := C.CString(value)
1043 defer C.free(unsafe.Pointer(cvalue))
1044 C.LLVMAddTargetDependentFunctionAttr(v.C, cattr, cvalue)
1047 // Operations on parameters
1048 func (v Value) ParamsCount() int { return int(C.LLVMCountParams(v.C)) }
1049 func (v Value) Params() []Value {
1050 out := make([]Value, v.ParamsCount())
1052 C.LLVMGetParams(v.C, llvmValueRefPtr(&out[0]))
1056 func (v Value) Param(i int) (rv Value) { rv.C = C.LLVMGetParam(v.C, C.unsigned(i)); return }
1057 func (v Value) ParamParent() (rv Value) { rv.C = C.LLVMGetParamParent(v.C); return }
1058 func (v Value) FirstParam() (rv Value) { rv.C = C.LLVMGetFirstParam(v.C); return }
1059 func (v Value) LastParam() (rv Value) { rv.C = C.LLVMGetLastParam(v.C); return }
1060 func NextParam(v Value) (rv Value) { rv.C = C.LLVMGetNextParam(v.C); return }
1061 func PrevParam(v Value) (rv Value) { rv.C = C.LLVMGetPreviousParam(v.C); return }
1062 func (v Value) AddAttribute(a Attribute) {
1064 panic("attribute value currently unsupported")
1066 C.LLVMAddAttribute(v.C, C.LLVMAttribute(a))
1068 func (v Value) RemoveAttribute(a Attribute) {
1070 panic("attribute value currently unsupported")
1072 C.LLVMRemoveAttribute(v.C, C.LLVMAttribute(a))
1074 func (v Value) Attribute() Attribute { return Attribute(C.LLVMGetAttribute(v.C)) }
1075 func (v Value) SetParamAlignment(align int) { C.LLVMSetParamAlignment(v.C, C.unsigned(align)) }
1077 // Operations on basic blocks
1078 func (bb BasicBlock) AsValue() (v Value) { v.C = C.LLVMBasicBlockAsValue(bb.C); return }
1079 func (v Value) IsBasicBlock() bool { return C.LLVMValueIsBasicBlock(v.C) != 0 }
1080 func (v Value) AsBasicBlock() (bb BasicBlock) { bb.C = C.LLVMValueAsBasicBlock(v.C); return }
1081 func (bb BasicBlock) Parent() (v Value) { v.C = C.LLVMGetBasicBlockParent(bb.C); return }
1082 func (v Value) BasicBlocksCount() int { return int(C.LLVMCountBasicBlocks(v.C)) }
1083 func (v Value) BasicBlocks() []BasicBlock {
1084 out := make([]BasicBlock, v.BasicBlocksCount())
1085 C.LLVMGetBasicBlocks(v.C, llvmBasicBlockRefPtr(&out[0]))
1088 func (v Value) FirstBasicBlock() (bb BasicBlock) { bb.C = C.LLVMGetFirstBasicBlock(v.C); return }
1089 func (v Value) LastBasicBlock() (bb BasicBlock) { bb.C = C.LLVMGetLastBasicBlock(v.C); return }
1090 func NextBasicBlock(bb BasicBlock) (rbb BasicBlock) { rbb.C = C.LLVMGetNextBasicBlock(bb.C); return }
1091 func PrevBasicBlock(bb BasicBlock) (rbb BasicBlock) { rbb.C = C.LLVMGetPreviousBasicBlock(bb.C); return }
1092 func (v Value) EntryBasicBlock() (bb BasicBlock) { bb.C = C.LLVMGetEntryBasicBlock(v.C); return }
1093 func (c Context) AddBasicBlock(f Value, name string) (bb BasicBlock) {
1094 cname := C.CString(name)
1095 defer C.free(unsafe.Pointer(cname))
1096 bb.C = C.LLVMAppendBasicBlockInContext(c.C, f.C, cname)
1099 func (c Context) InsertBasicBlock(ref BasicBlock, name string) (bb BasicBlock) {
1100 cname := C.CString(name)
1101 defer C.free(unsafe.Pointer(cname))
1102 bb.C = C.LLVMInsertBasicBlockInContext(c.C, ref.C, cname)
1105 func AddBasicBlock(f Value, name string) (bb BasicBlock) {
1106 cname := C.CString(name)
1107 defer C.free(unsafe.Pointer(cname))
1108 bb.C = C.LLVMAppendBasicBlock(f.C, cname)
1111 func InsertBasicBlock(ref BasicBlock, name string) (bb BasicBlock) {
1112 cname := C.CString(name)
1113 defer C.free(unsafe.Pointer(cname))
1114 bb.C = C.LLVMInsertBasicBlock(ref.C, cname)
1117 func (bb BasicBlock) EraseFromParent() { C.LLVMDeleteBasicBlock(bb.C) }
1118 func (bb BasicBlock) MoveBefore(pos BasicBlock) { C.LLVMMoveBasicBlockBefore(bb.C, pos.C) }
1119 func (bb BasicBlock) MoveAfter(pos BasicBlock) { C.LLVMMoveBasicBlockAfter(bb.C, pos.C) }
1121 // Operations on instructions
1122 func (v Value) InstructionParent() (bb BasicBlock) { bb.C = C.LLVMGetInstructionParent(v.C); return }
1123 func (bb BasicBlock) FirstInstruction() (v Value) { v.C = C.LLVMGetFirstInstruction(bb.C); return }
1124 func (bb BasicBlock) LastInstruction() (v Value) { v.C = C.LLVMGetLastInstruction(bb.C); return }
1125 func NextInstruction(v Value) (rv Value) { rv.C = C.LLVMGetNextInstruction(v.C); return }
1126 func PrevInstruction(v Value) (rv Value) { rv.C = C.LLVMGetPreviousInstruction(v.C); return }
1128 // Operations on call sites
1129 func (v Value) SetInstructionCallConv(cc CallConv) {
1130 C.LLVMSetInstructionCallConv(v.C, C.unsigned(cc))
1132 func (v Value) InstructionCallConv() CallConv {
1133 return CallConv(C.LLVMCallConv(C.LLVMGetInstructionCallConv(v.C)))
1135 func (v Value) AddInstrAttribute(i int, a Attribute) {
1137 panic("attribute value currently unsupported")
1139 C.LLVMAddInstrAttribute(v.C, C.unsigned(i), C.LLVMAttribute(a))
1141 func (v Value) RemoveInstrAttribute(i int, a Attribute) {
1143 panic("attribute value currently unsupported")
1145 C.LLVMRemoveInstrAttribute(v.C, C.unsigned(i), C.LLVMAttribute(a))
1147 func (v Value) SetInstrParamAlignment(i int, align int) {
1148 C.LLVMSetInstrParamAlignment(v.C, C.unsigned(i), C.unsigned(align))
1151 // Operations on call instructions (only)
1152 func (v Value) IsTailCall() bool { return C.LLVMIsTailCall(v.C) != 0 }
1153 func (v Value) SetTailCall(is bool) { C.LLVMSetTailCall(v.C, boolToLLVMBool(is)) }
1155 // Operations on phi nodes
1156 func (v Value) AddIncoming(vals []Value, blocks []BasicBlock) {
1157 ptr, nvals := llvmValueRefs(vals)
1158 C.LLVMAddIncoming(v.C, ptr, llvmBasicBlockRefPtr(&blocks[0]), nvals)
1160 func (v Value) IncomingCount() int { return int(C.LLVMCountIncoming(v.C)) }
1161 func (v Value) IncomingValue(i int) (rv Value) {
1162 rv.C = C.LLVMGetIncomingValue(v.C, C.unsigned(i))
1165 func (v Value) IncomingBlock(i int) (bb BasicBlock) {
1166 bb.C = C.LLVMGetIncomingBlock(v.C, C.unsigned(i))
1170 //-------------------------------------------------------------------------
1172 //-------------------------------------------------------------------------
1174 // An instruction builder represents a point within a basic block, and is the
1175 // exclusive means of building instructions using the C interface.
1177 func (c Context) NewBuilder() (b Builder) { b.C = C.LLVMCreateBuilderInContext(c.C); return }
1178 func NewBuilder() (b Builder) { b.C = C.LLVMCreateBuilder(); return }
1179 func (b Builder) SetInsertPoint(block BasicBlock, instr Value) {
1180 C.LLVMPositionBuilder(b.C, block.C, instr.C)
1182 func (b Builder) SetInsertPointBefore(instr Value) { C.LLVMPositionBuilderBefore(b.C, instr.C) }
1183 func (b Builder) SetInsertPointAtEnd(block BasicBlock) { C.LLVMPositionBuilderAtEnd(b.C, block.C) }
1184 func (b Builder) GetInsertBlock() (bb BasicBlock) { bb.C = C.LLVMGetInsertBlock(b.C); return }
1185 func (b Builder) ClearInsertionPoint() { C.LLVMClearInsertionPosition(b.C) }
1186 func (b Builder) Insert(instr Value) { C.LLVMInsertIntoBuilder(b.C, instr.C) }
1187 func (b Builder) InsertWithName(instr Value, name string) {
1188 cname := C.CString(name)
1189 defer C.free(unsafe.Pointer(cname))
1190 C.LLVMInsertIntoBuilderWithName(b.C, instr.C, cname)
1192 func (b Builder) Dispose() { C.LLVMDisposeBuilder(b.C) }
1195 func (b Builder) SetCurrentDebugLocation(line, col uint, scope, inlinedAt Metadata) {
1196 C.LLVMSetCurrentDebugLocation2(b.C, C.unsigned(line), C.unsigned(col), scope.C, inlinedAt.C)
1198 func (b Builder) SetInstDebugLocation(v Value) { C.LLVMSetInstDebugLocation(b.C, v.C) }
1199 func (b Builder) InsertDeclare(module Module, storage Value, md Value) Value {
1200 f := module.NamedFunction("llvm.dbg.declare")
1202 ftyp := FunctionType(VoidType(), []Type{storage.Type(), md.Type()}, false)
1203 f = AddFunction(module, "llvm.dbg.declare", ftyp)
1205 return b.CreateCall(f, []Value{storage, md}, "")
1209 func (b Builder) CreateRetVoid() (rv Value) { rv.C = C.LLVMBuildRetVoid(b.C); return }
1210 func (b Builder) CreateRet(v Value) (rv Value) { rv.C = C.LLVMBuildRet(b.C, v.C); return }
1211 func (b Builder) CreateAggregateRet(vs []Value) (rv Value) {
1212 ptr, nvals := llvmValueRefs(vs)
1213 rv.C = C.LLVMBuildAggregateRet(b.C, ptr, nvals)
1216 func (b Builder) CreateBr(bb BasicBlock) (rv Value) { rv.C = C.LLVMBuildBr(b.C, bb.C); return }
1217 func (b Builder) CreateCondBr(ifv Value, thenb, elseb BasicBlock) (rv Value) {
1218 rv.C = C.LLVMBuildCondBr(b.C, ifv.C, thenb.C, elseb.C)
1221 func (b Builder) CreateSwitch(v Value, elseb BasicBlock, numCases int) (rv Value) {
1222 rv.C = C.LLVMBuildSwitch(b.C, v.C, elseb.C, C.unsigned(numCases))
1225 func (b Builder) CreateIndirectBr(addr Value, numDests int) (rv Value) {
1226 rv.C = C.LLVMBuildIndirectBr(b.C, addr.C, C.unsigned(numDests))
1229 func (b Builder) CreateInvoke(fn Value, args []Value, then, catch BasicBlock, name string) (rv Value) {
1230 cname := C.CString(name)
1231 defer C.free(unsafe.Pointer(cname))
1232 ptr, nvals := llvmValueRefs(args)
1233 rv.C = C.LLVMBuildInvoke(b.C, fn.C, ptr, nvals, then.C, catch.C, cname)
1236 func (b Builder) CreateUnreachable() (rv Value) { rv.C = C.LLVMBuildUnreachable(b.C); return }
1238 // Add a case to the switch instruction
1239 func (v Value) AddCase(on Value, dest BasicBlock) { C.LLVMAddCase(v.C, on.C, dest.C) }
1241 // Add a destination to the indirectbr instruction
1242 func (v Value) AddDest(dest BasicBlock) { C.LLVMAddDestination(v.C, dest.C) }
1245 func (b Builder) CreateAdd(lhs, rhs Value, name string) (v Value) {
1246 cname := C.CString(name)
1247 defer C.free(unsafe.Pointer(cname))
1248 v.C = C.LLVMBuildAdd(b.C, lhs.C, rhs.C, cname)
1251 func (b Builder) CreateNSWAdd(lhs, rhs Value, name string) (v Value) {
1252 cname := C.CString(name)
1253 defer C.free(unsafe.Pointer(cname))
1254 v.C = C.LLVMBuildNSWAdd(b.C, lhs.C, rhs.C, cname)
1257 func (b Builder) CreateNUWAdd(lhs, rhs Value, name string) (v Value) {
1258 cname := C.CString(name)
1259 defer C.free(unsafe.Pointer(cname))
1260 v.C = C.LLVMBuildNUWAdd(b.C, lhs.C, rhs.C, cname)
1263 func (b Builder) CreateFAdd(lhs, rhs Value, name string) (v Value) {
1264 cname := C.CString(name)
1265 defer C.free(unsafe.Pointer(cname))
1266 v.C = C.LLVMBuildFAdd(b.C, lhs.C, rhs.C, cname)
1269 func (b Builder) CreateSub(lhs, rhs Value, name string) (v Value) {
1270 cname := C.CString(name)
1271 defer C.free(unsafe.Pointer(cname))
1272 v.C = C.LLVMBuildSub(b.C, lhs.C, rhs.C, cname)
1275 func (b Builder) CreateNSWSub(lhs, rhs Value, name string) (v Value) {
1276 cname := C.CString(name)
1277 defer C.free(unsafe.Pointer(cname))
1278 v.C = C.LLVMBuildNSWSub(b.C, lhs.C, rhs.C, cname)
1281 func (b Builder) CreateNUWSub(lhs, rhs Value, name string) (v Value) {
1282 cname := C.CString(name)
1283 defer C.free(unsafe.Pointer(cname))
1284 v.C = C.LLVMBuildNUWSub(b.C, lhs.C, rhs.C, cname)
1287 func (b Builder) CreateFSub(lhs, rhs Value, name string) (v Value) {
1288 cname := C.CString(name)
1289 v.C = C.LLVMBuildFSub(b.C, lhs.C, rhs.C, cname)
1290 C.free(unsafe.Pointer(cname))
1293 func (b Builder) CreateMul(lhs, rhs Value, name string) (v Value) {
1294 cname := C.CString(name)
1295 defer C.free(unsafe.Pointer(cname))
1296 v.C = C.LLVMBuildMul(b.C, lhs.C, rhs.C, cname)
1299 func (b Builder) CreateNSWMul(lhs, rhs Value, name string) (v Value) {
1300 cname := C.CString(name)
1301 defer C.free(unsafe.Pointer(cname))
1302 v.C = C.LLVMBuildNSWMul(b.C, lhs.C, rhs.C, cname)
1305 func (b Builder) CreateNUWMul(lhs, rhs Value, name string) (v Value) {
1306 cname := C.CString(name)
1307 defer C.free(unsafe.Pointer(cname))
1308 v.C = C.LLVMBuildNUWMul(b.C, lhs.C, rhs.C, cname)
1311 func (b Builder) CreateFMul(lhs, rhs Value, name string) (v Value) {
1312 cname := C.CString(name)
1313 defer C.free(unsafe.Pointer(cname))
1314 v.C = C.LLVMBuildFMul(b.C, lhs.C, rhs.C, cname)
1317 func (b Builder) CreateUDiv(lhs, rhs Value, name string) (v Value) {
1318 cname := C.CString(name)
1319 defer C.free(unsafe.Pointer(cname))
1320 v.C = C.LLVMBuildUDiv(b.C, lhs.C, rhs.C, cname)
1323 func (b Builder) CreateSDiv(lhs, rhs Value, name string) (v Value) {
1324 cname := C.CString(name)
1325 defer C.free(unsafe.Pointer(cname))
1326 v.C = C.LLVMBuildSDiv(b.C, lhs.C, rhs.C, cname)
1329 func (b Builder) CreateExactSDiv(lhs, rhs Value, name string) (v Value) {
1330 cname := C.CString(name)
1331 defer C.free(unsafe.Pointer(cname))
1332 v.C = C.LLVMBuildExactSDiv(b.C, lhs.C, rhs.C, cname)
1335 func (b Builder) CreateFDiv(lhs, rhs Value, name string) (v Value) {
1336 cname := C.CString(name)
1337 defer C.free(unsafe.Pointer(cname))
1338 v.C = C.LLVMBuildFDiv(b.C, lhs.C, rhs.C, cname)
1341 func (b Builder) CreateURem(lhs, rhs Value, name string) (v Value) {
1342 cname := C.CString(name)
1343 defer C.free(unsafe.Pointer(cname))
1344 v.C = C.LLVMBuildURem(b.C, lhs.C, rhs.C, cname)
1347 func (b Builder) CreateSRem(lhs, rhs Value, name string) (v Value) {
1348 cname := C.CString(name)
1349 defer C.free(unsafe.Pointer(cname))
1350 v.C = C.LLVMBuildSRem(b.C, lhs.C, rhs.C, cname)
1353 func (b Builder) CreateFRem(lhs, rhs Value, name string) (v Value) {
1354 cname := C.CString(name)
1355 defer C.free(unsafe.Pointer(cname))
1356 v.C = C.LLVMBuildFRem(b.C, lhs.C, rhs.C, cname)
1359 func (b Builder) CreateShl(lhs, rhs Value, name string) (v Value) {
1360 cname := C.CString(name)
1361 defer C.free(unsafe.Pointer(cname))
1362 v.C = C.LLVMBuildShl(b.C, lhs.C, rhs.C, cname)
1365 func (b Builder) CreateLShr(lhs, rhs Value, name string) (v Value) {
1366 cname := C.CString(name)
1367 defer C.free(unsafe.Pointer(cname))
1368 v.C = C.LLVMBuildLShr(b.C, lhs.C, rhs.C, cname)
1371 func (b Builder) CreateAShr(lhs, rhs Value, name string) (v Value) {
1372 cname := C.CString(name)
1373 defer C.free(unsafe.Pointer(cname))
1374 v.C = C.LLVMBuildAShr(b.C, lhs.C, rhs.C, cname)
1377 func (b Builder) CreateAnd(lhs, rhs Value, name string) (v Value) {
1378 cname := C.CString(name)
1379 defer C.free(unsafe.Pointer(cname))
1380 v.C = C.LLVMBuildAnd(b.C, lhs.C, rhs.C, cname)
1383 func (b Builder) CreateOr(lhs, rhs Value, name string) (v Value) {
1384 cname := C.CString(name)
1385 defer C.free(unsafe.Pointer(cname))
1386 v.C = C.LLVMBuildOr(b.C, lhs.C, rhs.C, cname)
1389 func (b Builder) CreateXor(lhs, rhs Value, name string) (v Value) {
1390 cname := C.CString(name)
1391 defer C.free(unsafe.Pointer(cname))
1392 v.C = C.LLVMBuildXor(b.C, lhs.C, rhs.C, cname)
1395 func (b Builder) CreateBinOp(op Opcode, lhs, rhs Value, name string) (v Value) {
1396 cname := C.CString(name)
1397 defer C.free(unsafe.Pointer(cname))
1398 v.C = C.LLVMBuildBinOp(b.C, C.LLVMOpcode(op), lhs.C, rhs.C, cname)
1401 func (b Builder) CreateNeg(v Value, name string) (rv Value) {
1402 cname := C.CString(name)
1403 defer C.free(unsafe.Pointer(cname))
1404 rv.C = C.LLVMBuildNeg(b.C, v.C, cname)
1407 func (b Builder) CreateNSWNeg(v Value, name string) (rv Value) {
1408 cname := C.CString(name)
1409 defer C.free(unsafe.Pointer(cname))
1410 rv.C = C.LLVMBuildNSWNeg(b.C, v.C, cname)
1413 func (b Builder) CreateNUWNeg(v Value, name string) (rv Value) {
1414 cname := C.CString(name)
1415 defer C.free(unsafe.Pointer(cname))
1416 rv.C = C.LLVMBuildNUWNeg(b.C, v.C, cname)
1419 func (b Builder) CreateFNeg(v Value, name string) (rv Value) {
1420 cname := C.CString(name)
1421 defer C.free(unsafe.Pointer(cname))
1422 rv.C = C.LLVMBuildFNeg(b.C, v.C, cname)
1425 func (b Builder) CreateNot(v Value, name string) (rv Value) {
1426 cname := C.CString(name)
1427 defer C.free(unsafe.Pointer(cname))
1428 rv.C = C.LLVMBuildNot(b.C, v.C, cname)
1434 func (b Builder) CreateMalloc(t Type, name string) (v Value) {
1435 cname := C.CString(name)
1436 defer C.free(unsafe.Pointer(cname))
1437 v.C = C.LLVMBuildMalloc(b.C, t.C, cname)
1440 func (b Builder) CreateArrayMalloc(t Type, val Value, name string) (v Value) {
1441 cname := C.CString(name)
1442 defer C.free(unsafe.Pointer(cname))
1443 v.C = C.LLVMBuildArrayMalloc(b.C, t.C, val.C, cname)
1446 func (b Builder) CreateAlloca(t Type, name string) (v Value) {
1447 cname := C.CString(name)
1448 defer C.free(unsafe.Pointer(cname))
1449 v.C = C.LLVMBuildAlloca(b.C, t.C, cname)
1452 func (b Builder) CreateArrayAlloca(t Type, val Value, name string) (v Value) {
1453 cname := C.CString(name)
1454 defer C.free(unsafe.Pointer(cname))
1455 v.C = C.LLVMBuildArrayAlloca(b.C, t.C, val.C, cname)
1458 func (b Builder) CreateFree(p Value) (v Value) {
1459 v.C = C.LLVMBuildFree(b.C, p.C)
1462 func (b Builder) CreateLoad(p Value, name string) (v Value) {
1463 cname := C.CString(name)
1464 defer C.free(unsafe.Pointer(cname))
1465 v.C = C.LLVMBuildLoad(b.C, p.C, cname)
1468 func (b Builder) CreateStore(val Value, p Value) (v Value) {
1469 v.C = C.LLVMBuildStore(b.C, val.C, p.C)
1472 func (b Builder) CreateGEP(p Value, indices []Value, name string) (v Value) {
1473 cname := C.CString(name)
1474 defer C.free(unsafe.Pointer(cname))
1475 ptr, nvals := llvmValueRefs(indices)
1476 v.C = C.LLVMBuildGEP(b.C, p.C, ptr, nvals, cname)
1479 func (b Builder) CreateInBoundsGEP(p Value, indices []Value, name string) (v Value) {
1480 cname := C.CString(name)
1481 defer C.free(unsafe.Pointer(cname))
1482 ptr, nvals := llvmValueRefs(indices)
1483 v.C = C.LLVMBuildInBoundsGEP(b.C, p.C, ptr, nvals, cname)
1486 func (b Builder) CreateStructGEP(p Value, i int, name string) (v Value) {
1487 cname := C.CString(name)
1488 defer C.free(unsafe.Pointer(cname))
1489 v.C = C.LLVMBuildStructGEP(b.C, p.C, C.unsigned(i), cname)
1492 func (b Builder) CreateGlobalString(str, name string) (v Value) {
1493 cstr := C.CString(str)
1494 defer C.free(unsafe.Pointer(cstr))
1495 cname := C.CString(name)
1496 defer C.free(unsafe.Pointer(cname))
1497 v.C = C.LLVMBuildGlobalString(b.C, cstr, cname)
1500 func (b Builder) CreateGlobalStringPtr(str, name string) (v Value) {
1501 cstr := C.CString(str)
1502 defer C.free(unsafe.Pointer(cstr))
1503 cname := C.CString(name)
1504 defer C.free(unsafe.Pointer(cname))
1505 v.C = C.LLVMBuildGlobalStringPtr(b.C, cstr, cname)
1510 func (b Builder) CreateTrunc(val Value, t Type, name string) (v Value) {
1511 cname := C.CString(name)
1512 defer C.free(unsafe.Pointer(cname))
1513 v.C = C.LLVMBuildTrunc(b.C, val.C, t.C, cname)
1516 func (b Builder) CreateZExt(val Value, t Type, name string) (v Value) {
1517 cname := C.CString(name)
1518 defer C.free(unsafe.Pointer(cname))
1519 v.C = C.LLVMBuildZExt(b.C, val.C, t.C, cname)
1522 func (b Builder) CreateSExt(val Value, t Type, name string) (v Value) {
1523 cname := C.CString(name)
1524 defer C.free(unsafe.Pointer(cname))
1525 v.C = C.LLVMBuildSExt(b.C, val.C, t.C, cname)
1528 func (b Builder) CreateFPToUI(val Value, t Type, name string) (v Value) {
1529 cname := C.CString(name)
1530 defer C.free(unsafe.Pointer(cname))
1531 v.C = C.LLVMBuildFPToUI(b.C, val.C, t.C, cname)
1534 func (b Builder) CreateFPToSI(val Value, t Type, name string) (v Value) {
1535 cname := C.CString(name)
1536 defer C.free(unsafe.Pointer(cname))
1537 v.C = C.LLVMBuildFPToSI(b.C, val.C, t.C, cname)
1540 func (b Builder) CreateUIToFP(val Value, t Type, name string) (v Value) {
1541 cname := C.CString(name)
1542 defer C.free(unsafe.Pointer(cname))
1543 v.C = C.LLVMBuildUIToFP(b.C, val.C, t.C, cname)
1546 func (b Builder) CreateSIToFP(val Value, t Type, name string) (v Value) {
1547 cname := C.CString(name)
1548 defer C.free(unsafe.Pointer(cname))
1549 v.C = C.LLVMBuildSIToFP(b.C, val.C, t.C, cname)
1552 func (b Builder) CreateFPTrunc(val Value, t Type, name string) (v Value) {
1553 cname := C.CString(name)
1554 defer C.free(unsafe.Pointer(cname))
1555 v.C = C.LLVMBuildFPTrunc(b.C, val.C, t.C, cname)
1558 func (b Builder) CreateFPExt(val Value, t Type, name string) (v Value) {
1559 cname := C.CString(name)
1560 defer C.free(unsafe.Pointer(cname))
1561 v.C = C.LLVMBuildFPExt(b.C, val.C, t.C, cname)
1564 func (b Builder) CreatePtrToInt(val Value, t Type, name string) (v Value) {
1565 cname := C.CString(name)
1566 defer C.free(unsafe.Pointer(cname))
1567 v.C = C.LLVMBuildPtrToInt(b.C, val.C, t.C, cname)
1570 func (b Builder) CreateIntToPtr(val Value, t Type, name string) (v Value) {
1571 cname := C.CString(name)
1572 defer C.free(unsafe.Pointer(cname))
1573 v.C = C.LLVMBuildIntToPtr(b.C, val.C, t.C, cname)
1576 func (b Builder) CreateBitCast(val Value, t Type, name string) (v Value) {
1577 cname := C.CString(name)
1578 defer C.free(unsafe.Pointer(cname))
1579 v.C = C.LLVMBuildBitCast(b.C, val.C, t.C, cname)
1582 func (b Builder) CreateZExtOrBitCast(val Value, t Type, name string) (v Value) {
1583 cname := C.CString(name)
1584 defer C.free(unsafe.Pointer(cname))
1585 v.C = C.LLVMBuildZExtOrBitCast(b.C, val.C, t.C, cname)
1588 func (b Builder) CreateSExtOrBitCast(val Value, t Type, name string) (v Value) {
1589 cname := C.CString(name)
1590 defer C.free(unsafe.Pointer(cname))
1591 v.C = C.LLVMBuildSExtOrBitCast(b.C, val.C, t.C, cname)
1594 func (b Builder) CreateTruncOrBitCast(val Value, t Type, name string) (v Value) {
1595 cname := C.CString(name)
1596 defer C.free(unsafe.Pointer(cname))
1597 v.C = C.LLVMBuildTruncOrBitCast(b.C, val.C, t.C, cname)
1600 func (b Builder) CreateCast(val Value, op Opcode, t Type, name string) (v Value) {
1601 cname := C.CString(name)
1602 defer C.free(unsafe.Pointer(cname))
1603 v.C = C.LLVMBuildCast(b.C, C.LLVMOpcode(op), val.C, t.C, cname)
1606 func (b Builder) CreatePointerCast(val Value, t Type, name string) (v Value) {
1607 cname := C.CString(name)
1608 defer C.free(unsafe.Pointer(cname))
1609 v.C = C.LLVMBuildPointerCast(b.C, val.C, t.C, cname)
1612 func (b Builder) CreateIntCast(val Value, t Type, name string) (v Value) {
1613 cname := C.CString(name)
1614 defer C.free(unsafe.Pointer(cname))
1615 v.C = C.LLVMBuildIntCast(b.C, val.C, t.C, cname)
1618 func (b Builder) CreateFPCast(val Value, t Type, name string) (v Value) {
1619 cname := C.CString(name)
1620 defer C.free(unsafe.Pointer(cname))
1621 v.C = C.LLVMBuildFPCast(b.C, val.C, t.C, cname)
1626 func (b Builder) CreateICmp(pred IntPredicate, lhs, rhs Value, name string) (v Value) {
1627 cname := C.CString(name)
1628 defer C.free(unsafe.Pointer(cname))
1629 v.C = C.LLVMBuildICmp(b.C, C.LLVMIntPredicate(pred), lhs.C, rhs.C, cname)
1632 func (b Builder) CreateFCmp(pred FloatPredicate, lhs, rhs Value, name string) (v Value) {
1633 cname := C.CString(name)
1634 defer C.free(unsafe.Pointer(cname))
1635 v.C = C.LLVMBuildFCmp(b.C, C.LLVMRealPredicate(pred), lhs.C, rhs.C, cname)
1639 // Miscellaneous instructions
1640 func (b Builder) CreatePHI(t Type, name string) (v Value) {
1641 cname := C.CString(name)
1642 defer C.free(unsafe.Pointer(cname))
1643 v.C = C.LLVMBuildPhi(b.C, t.C, cname)
1646 func (b Builder) CreateCall(fn Value, args []Value, name string) (v Value) {
1647 cname := C.CString(name)
1648 defer C.free(unsafe.Pointer(cname))
1649 ptr, nvals := llvmValueRefs(args)
1650 v.C = C.LLVMBuildCall(b.C, fn.C, ptr, nvals, cname)
1654 func (b Builder) CreateSelect(ifv, thenv, elsev Value, name string) (v Value) {
1655 cname := C.CString(name)
1656 defer C.free(unsafe.Pointer(cname))
1657 v.C = C.LLVMBuildSelect(b.C, ifv.C, thenv.C, elsev.C, cname)
1661 func (b Builder) CreateVAArg(list Value, t Type, name string) (v Value) {
1662 cname := C.CString(name)
1663 defer C.free(unsafe.Pointer(cname))
1664 v.C = C.LLVMBuildVAArg(b.C, list.C, t.C, cname)
1667 func (b Builder) CreateExtractElement(vec, i Value, name string) (v Value) {
1668 cname := C.CString(name)
1669 defer C.free(unsafe.Pointer(cname))
1670 v.C = C.LLVMBuildExtractElement(b.C, vec.C, i.C, cname)
1673 func (b Builder) CreateInsertElement(vec, elt, i Value, name string) (v Value) {
1674 cname := C.CString(name)
1675 defer C.free(unsafe.Pointer(cname))
1676 v.C = C.LLVMBuildInsertElement(b.C, vec.C, elt.C, i.C, cname)
1679 func (b Builder) CreateShuffleVector(v1, v2, mask Value, name string) (v Value) {
1680 cname := C.CString(name)
1681 defer C.free(unsafe.Pointer(cname))
1682 v.C = C.LLVMBuildShuffleVector(b.C, v1.C, v2.C, mask.C, cname)
1685 func (b Builder) CreateExtractValue(agg Value, i int, name string) (v Value) {
1686 cname := C.CString(name)
1687 defer C.free(unsafe.Pointer(cname))
1688 v.C = C.LLVMBuildExtractValue(b.C, agg.C, C.unsigned(i), cname)
1691 func (b Builder) CreateInsertValue(agg, elt Value, i int, name string) (v Value) {
1692 cname := C.CString(name)
1693 defer C.free(unsafe.Pointer(cname))
1694 v.C = C.LLVMBuildInsertValue(b.C, agg.C, elt.C, C.unsigned(i), cname)
1698 func (b Builder) CreateIsNull(val Value, name string) (v Value) {
1699 cname := C.CString(name)
1700 defer C.free(unsafe.Pointer(cname))
1701 v.C = C.LLVMBuildIsNull(b.C, val.C, cname)
1704 func (b Builder) CreateIsNotNull(val Value, name string) (v Value) {
1705 cname := C.CString(name)
1706 defer C.free(unsafe.Pointer(cname))
1707 v.C = C.LLVMBuildIsNotNull(b.C, val.C, cname)
1710 func (b Builder) CreatePtrDiff(lhs, rhs Value, name string) (v Value) {
1711 cname := C.CString(name)
1712 defer C.free(unsafe.Pointer(cname))
1713 v.C = C.LLVMBuildPtrDiff(b.C, lhs.C, rhs.C, cname)
1717 func (b Builder) CreateLandingPad(t Type, personality Value, nclauses int, name string) (l Value) {
1718 cname := C.CString(name)
1719 defer C.free(unsafe.Pointer(cname))
1720 l.C = C.LLVMBuildLandingPad(b.C, t.C, personality.C, C.unsigned(nclauses), cname)
1724 func (l Value) AddClause(v Value) {
1725 C.LLVMAddClause(l.C, v.C)
1728 func (l Value) SetCleanup(cleanup bool) {
1729 C.LLVMSetCleanup(l.C, boolToLLVMBool(cleanup))
1732 func (b Builder) CreateResume(ex Value) (v Value) {
1733 v.C = C.LLVMBuildResume(b.C, ex.C)
1737 //-------------------------------------------------------------------------
1738 // llvm.ModuleProvider
1739 //-------------------------------------------------------------------------
1741 // Changes the type of M so it can be passed to FunctionPassManagers and the
1742 // JIT. They take ModuleProviders for historical reasons.
1743 func NewModuleProviderForModule(m Module) (mp ModuleProvider) {
1744 mp.C = C.LLVMCreateModuleProviderForExistingModule(m.C)
1748 // Destroys the module M.
1749 func (mp ModuleProvider) Dispose() { C.LLVMDisposeModuleProvider(mp.C) }
1751 //-------------------------------------------------------------------------
1752 // llvm.MemoryBuffer
1753 //-------------------------------------------------------------------------
1755 func NewMemoryBufferFromFile(path string) (b MemoryBuffer, err error) {
1757 cpath := C.CString(path)
1758 defer C.free(unsafe.Pointer(cpath))
1759 fail := C.LLVMCreateMemoryBufferWithContentsOfFile(cpath, &b.C, &cmsg)
1762 err = errors.New(C.GoString(cmsg))
1763 C.LLVMDisposeMessage(cmsg)
1768 func NewMemoryBufferFromStdin() (b MemoryBuffer, err error) {
1770 fail := C.LLVMCreateMemoryBufferWithSTDIN(&b.C, &cmsg)
1773 err = errors.New(C.GoString(cmsg))
1774 C.LLVMDisposeMessage(cmsg)
1779 func (b MemoryBuffer) Bytes() []byte {
1780 cstart := C.LLVMGetBufferStart(b.C)
1781 csize := C.LLVMGetBufferSize(b.C)
1782 return C.GoBytes(unsafe.Pointer(cstart), C.int(csize))
1785 func (b MemoryBuffer) Dispose() { C.LLVMDisposeMemoryBuffer(b.C) }
1787 //-------------------------------------------------------------------------
1789 //-------------------------------------------------------------------------
1791 // Constructs a new whole-module pass pipeline. This type of pipeline is
1792 // suitable for link-time optimization and whole-module transformations.
1793 // See llvm::PassManager::PassManager.
1794 func NewPassManager() (pm PassManager) { pm.C = C.LLVMCreatePassManager(); return }
1796 // Constructs a new function-by-function pass pipeline over the module
1797 // provider. It does not take ownership of the module provider. This type of
1798 // pipeline is suitable for code generation and JIT compilation tasks.
1799 // See llvm::FunctionPassManager::FunctionPassManager.
1800 func NewFunctionPassManagerForModule(m Module) (pm PassManager) {
1801 pm.C = C.LLVMCreateFunctionPassManagerForModule(m.C)
1805 // Initializes, executes on the provided module, and finalizes all of the
1806 // passes scheduled in the pass manager. Returns 1 if any of the passes
1807 // modified the module, 0 otherwise. See llvm::PassManager::run(Module&).
1808 func (pm PassManager) Run(m Module) bool { return C.LLVMRunPassManager(pm.C, m.C) != 0 }
1810 // Initializes all of the function passes scheduled in the function pass
1811 // manager. Returns 1 if any of the passes modified the module, 0 otherwise.
1812 // See llvm::FunctionPassManager::doInitialization.
1813 func (pm PassManager) InitializeFunc() bool { return C.LLVMInitializeFunctionPassManager(pm.C) != 0 }
1815 // Executes all of the function passes scheduled in the function pass manager
1816 // on the provided function. Returns 1 if any of the passes modified the
1817 // function, false otherwise.
1818 // See llvm::FunctionPassManager::run(Function&).
1819 func (pm PassManager) RunFunc(f Value) bool { return C.LLVMRunFunctionPassManager(pm.C, f.C) != 0 }
1821 // Finalizes all of the function passes scheduled in in the function pass
1822 // manager. Returns 1 if any of the passes modified the module, 0 otherwise.
1823 // See llvm::FunctionPassManager::doFinalization.
1824 func (pm PassManager) FinalizeFunc() bool { return C.LLVMFinalizeFunctionPassManager(pm.C) != 0 }
1826 // Frees the memory of a pass pipeline. For function pipelines, does not free
1827 // the module provider.
1828 // See llvm::PassManagerBase::~PassManagerBase.
1829 func (pm PassManager) Dispose() { C.LLVMDisposePassManager(pm.C) }
1831 //-------------------------------------------------------------------------
1833 //-------------------------------------------------------------------------
1835 func (md Metadata) ReplaceAllUsesWith(new Metadata) {
1836 C.LLVMMetadataReplaceAllUsesWith(md.C, new.C)