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)
752 func (c Context) TemporaryMDNode(mds []Metadata) (md Metadata) {
753 ptr, nvals := llvmMetadataRefs(mds)
754 md.C = C.LLVMTemporaryMDNode(c.C, ptr, nvals)
758 // Operations on scalar constants
759 func ConstInt(t Type, n uint64, signExtend bool) (v Value) {
760 v.C = C.LLVMConstInt(t.C,
762 boolToLLVMBool(signExtend))
765 func ConstIntFromString(t Type, str string, radix int) (v Value) {
766 cstr := C.CString(str)
767 defer C.free(unsafe.Pointer(cstr))
768 v.C = C.LLVMConstIntOfString(t.C, cstr, C.uint8_t(radix))
771 func ConstFloat(t Type, n float64) (v Value) {
772 v.C = C.LLVMConstReal(t.C, C.double(n))
775 func ConstFloatFromString(t Type, str string) (v Value) {
776 cstr := C.CString(str)
777 defer C.free(unsafe.Pointer(cstr))
778 v.C = C.LLVMConstRealOfString(t.C, cstr)
782 func (v Value) ZExtValue() uint64 { return uint64(C.LLVMConstIntGetZExtValue(v.C)) }
783 func (v Value) SExtValue() int64 { return int64(C.LLVMConstIntGetSExtValue(v.C)) }
785 // Operations on composite constants
786 func (c Context) ConstString(str string, addnull bool) (v Value) {
787 cstr := C.CString(str)
788 defer C.free(unsafe.Pointer(cstr))
789 v.C = C.LLVMConstStringInContext(c.C, cstr,
790 C.unsigned(len(str)), boolToLLVMBool(!addnull))
793 func (c Context) ConstStruct(constVals []Value, packed bool) (v Value) {
794 ptr, nvals := llvmValueRefs(constVals)
795 v.C = C.LLVMConstStructInContext(c.C, ptr, nvals,
796 boolToLLVMBool(packed))
799 func ConstNamedStruct(t Type, constVals []Value) (v Value) {
800 ptr, nvals := llvmValueRefs(constVals)
801 v.C = C.LLVMConstNamedStruct(t.C, ptr, nvals)
804 func ConstString(str string, addnull bool) (v Value) {
805 cstr := C.CString(str)
806 defer C.free(unsafe.Pointer(cstr))
807 v.C = C.LLVMConstString(cstr,
808 C.unsigned(len(str)), boolToLLVMBool(!addnull))
811 func ConstArray(t Type, constVals []Value) (v Value) {
812 ptr, nvals := llvmValueRefs(constVals)
813 v.C = C.LLVMConstArray(t.C, ptr, nvals)
816 func ConstStruct(constVals []Value, packed bool) (v Value) {
817 ptr, nvals := llvmValueRefs(constVals)
818 v.C = C.LLVMConstStruct(ptr, nvals, boolToLLVMBool(packed))
821 func ConstVector(scalarConstVals []Value, packed bool) (v Value) {
822 ptr, nvals := llvmValueRefs(scalarConstVals)
823 v.C = C.LLVMConstVector(ptr, nvals)
827 // Constant expressions
828 func (v Value) Opcode() Opcode { return Opcode(C.LLVMGetConstOpcode(v.C)) }
829 func (v Value) InstructionOpcode() Opcode { return Opcode(C.LLVMGetInstructionOpcode(v.C)) }
830 func AlignOf(t Type) (v Value) { v.C = C.LLVMAlignOf(t.C); return }
831 func SizeOf(t Type) (v Value) { v.C = C.LLVMSizeOf(t.C); return }
832 func ConstNeg(v Value) (rv Value) { rv.C = C.LLVMConstNeg(v.C); return }
833 func ConstNSWNeg(v Value) (rv Value) { rv.C = C.LLVMConstNSWNeg(v.C); return }
834 func ConstNUWNeg(v Value) (rv Value) { rv.C = C.LLVMConstNUWNeg(v.C); return }
835 func ConstFNeg(v Value) (rv Value) { rv.C = C.LLVMConstFNeg(v.C); return }
836 func ConstNot(v Value) (rv Value) { rv.C = C.LLVMConstNot(v.C); return }
837 func ConstAdd(lhs, rhs Value) (v Value) { v.C = C.LLVMConstAdd(lhs.C, rhs.C); return }
838 func ConstNSWAdd(lhs, rhs Value) (v Value) { v.C = C.LLVMConstNSWAdd(lhs.C, rhs.C); return }
839 func ConstNUWAdd(lhs, rhs Value) (v Value) { v.C = C.LLVMConstNUWAdd(lhs.C, rhs.C); return }
840 func ConstFAdd(lhs, rhs Value) (v Value) { v.C = C.LLVMConstFAdd(lhs.C, rhs.C); return }
841 func ConstSub(lhs, rhs Value) (v Value) { v.C = C.LLVMConstSub(lhs.C, rhs.C); return }
842 func ConstNSWSub(lhs, rhs Value) (v Value) { v.C = C.LLVMConstNSWSub(lhs.C, rhs.C); return }
843 func ConstNUWSub(lhs, rhs Value) (v Value) { v.C = C.LLVMConstNUWSub(lhs.C, rhs.C); return }
844 func ConstFSub(lhs, rhs Value) (v Value) { v.C = C.LLVMConstFSub(lhs.C, rhs.C); return }
845 func ConstMul(lhs, rhs Value) (v Value) { v.C = C.LLVMConstMul(lhs.C, rhs.C); return }
846 func ConstNSWMul(lhs, rhs Value) (v Value) { v.C = C.LLVMConstNSWMul(lhs.C, rhs.C); return }
847 func ConstNUWMul(lhs, rhs Value) (v Value) { v.C = C.LLVMConstNUWMul(lhs.C, rhs.C); return }
848 func ConstFMul(lhs, rhs Value) (v Value) { v.C = C.LLVMConstFMul(lhs.C, rhs.C); return }
849 func ConstUDiv(lhs, rhs Value) (v Value) { v.C = C.LLVMConstUDiv(lhs.C, rhs.C); return }
850 func ConstSDiv(lhs, rhs Value) (v Value) { v.C = C.LLVMConstSDiv(lhs.C, rhs.C); return }
851 func ConstExactSDiv(lhs, rhs Value) (v Value) { v.C = C.LLVMConstExactSDiv(lhs.C, rhs.C); return }
852 func ConstFDiv(lhs, rhs Value) (v Value) { v.C = C.LLVMConstFDiv(lhs.C, rhs.C); return }
853 func ConstURem(lhs, rhs Value) (v Value) { v.C = C.LLVMConstURem(lhs.C, rhs.C); return }
854 func ConstSRem(lhs, rhs Value) (v Value) { v.C = C.LLVMConstSRem(lhs.C, rhs.C); return }
855 func ConstFRem(lhs, rhs Value) (v Value) { v.C = C.LLVMConstFRem(lhs.C, rhs.C); return }
856 func ConstAnd(lhs, rhs Value) (v Value) { v.C = C.LLVMConstAnd(lhs.C, rhs.C); return }
857 func ConstOr(lhs, rhs Value) (v Value) { v.C = C.LLVMConstOr(lhs.C, rhs.C); return }
858 func ConstXor(lhs, rhs Value) (v Value) { v.C = C.LLVMConstXor(lhs.C, rhs.C); return }
860 func ConstICmp(pred IntPredicate, lhs, rhs Value) (v Value) {
861 v.C = C.LLVMConstICmp(C.LLVMIntPredicate(pred), lhs.C, rhs.C)
864 func ConstFCmp(pred FloatPredicate, lhs, rhs Value) (v Value) {
865 v.C = C.LLVMConstFCmp(C.LLVMRealPredicate(pred), lhs.C, rhs.C)
869 func ConstShl(lhs, rhs Value) (v Value) { v.C = C.LLVMConstShl(lhs.C, rhs.C); return }
870 func ConstLShr(lhs, rhs Value) (v Value) { v.C = C.LLVMConstLShr(lhs.C, rhs.C); return }
871 func ConstAShr(lhs, rhs Value) (v Value) { v.C = C.LLVMConstAShr(lhs.C, rhs.C); return }
873 func ConstGEP(v Value, indices []Value) (rv Value) {
874 ptr, nvals := llvmValueRefs(indices)
875 rv.C = C.LLVMConstGEP(v.C, ptr, nvals)
878 func ConstInBoundsGEP(v Value, indices []Value) (rv Value) {
879 ptr, nvals := llvmValueRefs(indices)
880 rv.C = C.LLVMConstInBoundsGEP(v.C, ptr, nvals)
883 func ConstTrunc(v Value, t Type) (rv Value) { rv.C = C.LLVMConstTrunc(v.C, t.C); return }
884 func ConstSExt(v Value, t Type) (rv Value) { rv.C = C.LLVMConstSExt(v.C, t.C); return }
885 func ConstZExt(v Value, t Type) (rv Value) { rv.C = C.LLVMConstZExt(v.C, t.C); return }
886 func ConstFPTrunc(v Value, t Type) (rv Value) { rv.C = C.LLVMConstFPTrunc(v.C, t.C); return }
887 func ConstFPExt(v Value, t Type) (rv Value) { rv.C = C.LLVMConstFPExt(v.C, t.C); return }
888 func ConstUIToFP(v Value, t Type) (rv Value) { rv.C = C.LLVMConstUIToFP(v.C, t.C); return }
889 func ConstSIToFP(v Value, t Type) (rv Value) { rv.C = C.LLVMConstSIToFP(v.C, t.C); return }
890 func ConstFPToUI(v Value, t Type) (rv Value) { rv.C = C.LLVMConstFPToUI(v.C, t.C); return }
891 func ConstFPToSI(v Value, t Type) (rv Value) { rv.C = C.LLVMConstFPToSI(v.C, t.C); return }
892 func ConstPtrToInt(v Value, t Type) (rv Value) { rv.C = C.LLVMConstPtrToInt(v.C, t.C); return }
893 func ConstIntToPtr(v Value, t Type) (rv Value) { rv.C = C.LLVMConstIntToPtr(v.C, t.C); return }
894 func ConstBitCast(v Value, t Type) (rv Value) { rv.C = C.LLVMConstBitCast(v.C, t.C); return }
895 func ConstZExtOrBitCast(v Value, t Type) (rv Value) { rv.C = C.LLVMConstZExtOrBitCast(v.C, t.C); return }
896 func ConstSExtOrBitCast(v Value, t Type) (rv Value) { rv.C = C.LLVMConstSExtOrBitCast(v.C, t.C); return }
897 func ConstTruncOrBitCast(v Value, t Type) (rv Value) {
898 rv.C = C.LLVMConstTruncOrBitCast(v.C, t.C)
901 func ConstPointerCast(v Value, t Type) (rv Value) { rv.C = C.LLVMConstPointerCast(v.C, t.C); return }
902 func ConstIntCast(v Value, t Type, signed bool) (rv Value) {
903 rv.C = C.LLVMConstIntCast(v.C, t.C, boolToLLVMBool(signed))
906 func ConstFPCast(v Value, t Type) (rv Value) { rv.C = C.LLVMConstFPCast(v.C, t.C); return }
907 func ConstSelect(cond, iftrue, iffalse Value) (rv Value) {
908 rv.C = C.LLVMConstSelect(cond.C, iftrue.C, iffalse.C)
911 func ConstExtractElement(vec, i Value) (rv Value) {
912 rv.C = C.LLVMConstExtractElement(vec.C, i.C)
915 func ConstInsertElement(vec, elem, i Value) (rv Value) {
916 rv.C = C.LLVMConstInsertElement(vec.C, elem.C, i.C)
919 func ConstShuffleVector(veca, vecb, mask Value) (rv Value) {
920 rv.C = C.LLVMConstShuffleVector(veca.C, vecb.C, mask.C)
925 //LLVMValueRef LLVMConstExtractValue(LLVMValueRef AggConstant, unsigned *IdxList,
928 func ConstExtractValue(agg Value, indices []uint32) (rv Value) {
931 panic("one or more indices are required")
933 ptr := (*C.unsigned)(&indices[0])
934 rv.C = C.LLVMConstExtractValue(agg.C, ptr, C.unsigned(n))
938 func ConstInsertValue(agg, val Value, indices []uint32) (rv Value) {
941 panic("one or more indices are required")
943 ptr := (*C.unsigned)(&indices[0])
944 rv.C = C.LLVMConstInsertValue(agg.C, val.C, ptr, C.unsigned(n))
948 func BlockAddress(f Value, bb BasicBlock) (v Value) {
949 v.C = C.LLVMBlockAddress(f.C, bb.C)
953 // Operations on global variables, functions, and aliases (globals)
954 func (v Value) GlobalParent() (m Module) { m.C = C.LLVMGetGlobalParent(v.C); return }
955 func (v Value) IsDeclaration() bool { return C.LLVMIsDeclaration(v.C) != 0 }
956 func (v Value) Linkage() Linkage { return Linkage(C.LLVMGetLinkage(v.C)) }
957 func (v Value) SetLinkage(l Linkage) { C.LLVMSetLinkage(v.C, C.LLVMLinkage(l)) }
958 func (v Value) Section() string { return C.GoString(C.LLVMGetSection(v.C)) }
959 func (v Value) SetSection(str string) {
960 cstr := C.CString(str)
961 defer C.free(unsafe.Pointer(cstr))
962 C.LLVMSetSection(v.C, cstr)
964 func (v Value) Visibility() Visibility { return Visibility(C.LLVMGetVisibility(v.C)) }
965 func (v Value) SetVisibility(vi Visibility) { C.LLVMSetVisibility(v.C, C.LLVMVisibility(vi)) }
966 func (v Value) Alignment() int { return int(C.LLVMGetAlignment(v.C)) }
967 func (v Value) SetAlignment(a int) { C.LLVMSetAlignment(v.C, C.unsigned(a)) }
968 func (v Value) SetUnnamedAddr(ua bool) { C.LLVMSetUnnamedAddr(v.C, boolToLLVMBool(ua)) }
970 // Operations on global variables
971 func AddGlobal(m Module, t Type, name string) (v Value) {
972 cname := C.CString(name)
973 defer C.free(unsafe.Pointer(cname))
974 v.C = C.LLVMAddGlobal(m.C, t.C, cname)
977 func AddGlobalInAddressSpace(m Module, t Type, name string, addressSpace int) (v Value) {
978 cname := C.CString(name)
979 defer C.free(unsafe.Pointer(cname))
980 v.C = C.LLVMAddGlobalInAddressSpace(m.C, t.C, cname, C.unsigned(addressSpace))
983 func (m Module) NamedGlobal(name string) (v Value) {
984 cname := C.CString(name)
985 defer C.free(unsafe.Pointer(cname))
986 v.C = C.LLVMGetNamedGlobal(m.C, cname)
990 func (m Module) FirstGlobal() (v Value) { v.C = C.LLVMGetFirstGlobal(m.C); return }
991 func (m Module) LastGlobal() (v Value) { v.C = C.LLVMGetLastGlobal(m.C); return }
992 func NextGlobal(v Value) (rv Value) { rv.C = C.LLVMGetNextGlobal(v.C); return }
993 func PrevGlobal(v Value) (rv Value) { rv.C = C.LLVMGetPreviousGlobal(v.C); return }
994 func (v Value) EraseFromParentAsGlobal() { C.LLVMDeleteGlobal(v.C) }
995 func (v Value) Initializer() (rv Value) { rv.C = C.LLVMGetInitializer(v.C); return }
996 func (v Value) SetInitializer(cv Value) { C.LLVMSetInitializer(v.C, cv.C) }
997 func (v Value) IsThreadLocal() bool { return C.LLVMIsThreadLocal(v.C) != 0 }
998 func (v Value) SetThreadLocal(tl bool) { C.LLVMSetThreadLocal(v.C, boolToLLVMBool(tl)) }
999 func (v Value) IsGlobalConstant() bool { return C.LLVMIsGlobalConstant(v.C) != 0 }
1000 func (v Value) SetGlobalConstant(gc bool) { C.LLVMSetGlobalConstant(v.C, boolToLLVMBool(gc)) }
1002 // Operations on aliases
1003 func AddAlias(m Module, t Type, aliasee Value, name string) (v Value) {
1004 cname := C.CString(name)
1005 defer C.free(unsafe.Pointer(cname))
1006 v.C = C.LLVMAddAlias(m.C, t.C, aliasee.C, cname)
1010 // Operations on functions
1011 func AddFunction(m Module, name string, ft Type) (v Value) {
1012 cname := C.CString(name)
1013 defer C.free(unsafe.Pointer(cname))
1014 v.C = C.LLVMAddFunction(m.C, cname, ft.C)
1018 func (m Module) NamedFunction(name string) (v Value) {
1019 cname := C.CString(name)
1020 defer C.free(unsafe.Pointer(cname))
1021 v.C = C.LLVMGetNamedFunction(m.C, cname)
1025 func (m Module) FirstFunction() (v Value) { v.C = C.LLVMGetFirstFunction(m.C); return }
1026 func (m Module) LastFunction() (v Value) { v.C = C.LLVMGetLastFunction(m.C); return }
1027 func NextFunction(v Value) (rv Value) { rv.C = C.LLVMGetNextFunction(v.C); return }
1028 func PrevFunction(v Value) (rv Value) { rv.C = C.LLVMGetPreviousFunction(v.C); return }
1029 func (v Value) EraseFromParentAsFunction() { C.LLVMDeleteFunction(v.C) }
1030 func (v Value) IntrinsicID() int { return int(C.LLVMGetIntrinsicID(v.C)) }
1031 func (v Value) FunctionCallConv() CallConv {
1032 return CallConv(C.LLVMCallConv(C.LLVMGetFunctionCallConv(v.C)))
1034 func (v Value) SetFunctionCallConv(cc CallConv) { C.LLVMSetFunctionCallConv(v.C, C.unsigned(cc)) }
1035 func (v Value) GC() string { return C.GoString(C.LLVMGetGC(v.C)) }
1036 func (v Value) SetGC(name string) {
1037 cname := C.CString(name)
1038 defer C.free(unsafe.Pointer(cname))
1039 C.LLVMSetGC(v.C, cname)
1041 func (v Value) AddFunctionAttr(a Attribute) { C.LLVMAddFunctionAttr2(v.C, C.uint64_t(a)) }
1042 func (v Value) FunctionAttr() Attribute { return Attribute(C.LLVMGetFunctionAttr2(v.C)) }
1043 func (v Value) RemoveFunctionAttr(a Attribute) { C.LLVMRemoveFunctionAttr2(v.C, C.uint64_t(a)) }
1044 func (v Value) AddTargetDependentFunctionAttr(attr, value string) {
1045 cattr := C.CString(attr)
1046 defer C.free(unsafe.Pointer(cattr))
1047 cvalue := C.CString(value)
1048 defer C.free(unsafe.Pointer(cvalue))
1049 C.LLVMAddTargetDependentFunctionAttr(v.C, cattr, cvalue)
1052 // Operations on parameters
1053 func (v Value) ParamsCount() int { return int(C.LLVMCountParams(v.C)) }
1054 func (v Value) Params() []Value {
1055 out := make([]Value, v.ParamsCount())
1057 C.LLVMGetParams(v.C, llvmValueRefPtr(&out[0]))
1061 func (v Value) Param(i int) (rv Value) { rv.C = C.LLVMGetParam(v.C, C.unsigned(i)); return }
1062 func (v Value) ParamParent() (rv Value) { rv.C = C.LLVMGetParamParent(v.C); return }
1063 func (v Value) FirstParam() (rv Value) { rv.C = C.LLVMGetFirstParam(v.C); return }
1064 func (v Value) LastParam() (rv Value) { rv.C = C.LLVMGetLastParam(v.C); return }
1065 func NextParam(v Value) (rv Value) { rv.C = C.LLVMGetNextParam(v.C); return }
1066 func PrevParam(v Value) (rv Value) { rv.C = C.LLVMGetPreviousParam(v.C); return }
1067 func (v Value) AddAttribute(a Attribute) {
1069 panic("attribute value currently unsupported")
1071 C.LLVMAddAttribute(v.C, C.LLVMAttribute(a))
1073 func (v Value) RemoveAttribute(a Attribute) {
1075 panic("attribute value currently unsupported")
1077 C.LLVMRemoveAttribute(v.C, C.LLVMAttribute(a))
1079 func (v Value) Attribute() Attribute { return Attribute(C.LLVMGetAttribute(v.C)) }
1080 func (v Value) SetParamAlignment(align int) { C.LLVMSetParamAlignment(v.C, C.unsigned(align)) }
1082 // Operations on basic blocks
1083 func (bb BasicBlock) AsValue() (v Value) { v.C = C.LLVMBasicBlockAsValue(bb.C); return }
1084 func (v Value) IsBasicBlock() bool { return C.LLVMValueIsBasicBlock(v.C) != 0 }
1085 func (v Value) AsBasicBlock() (bb BasicBlock) { bb.C = C.LLVMValueAsBasicBlock(v.C); return }
1086 func (bb BasicBlock) Parent() (v Value) { v.C = C.LLVMGetBasicBlockParent(bb.C); return }
1087 func (v Value) BasicBlocksCount() int { return int(C.LLVMCountBasicBlocks(v.C)) }
1088 func (v Value) BasicBlocks() []BasicBlock {
1089 out := make([]BasicBlock, v.BasicBlocksCount())
1090 C.LLVMGetBasicBlocks(v.C, llvmBasicBlockRefPtr(&out[0]))
1093 func (v Value) FirstBasicBlock() (bb BasicBlock) { bb.C = C.LLVMGetFirstBasicBlock(v.C); return }
1094 func (v Value) LastBasicBlock() (bb BasicBlock) { bb.C = C.LLVMGetLastBasicBlock(v.C); return }
1095 func NextBasicBlock(bb BasicBlock) (rbb BasicBlock) { rbb.C = C.LLVMGetNextBasicBlock(bb.C); return }
1096 func PrevBasicBlock(bb BasicBlock) (rbb BasicBlock) { rbb.C = C.LLVMGetPreviousBasicBlock(bb.C); return }
1097 func (v Value) EntryBasicBlock() (bb BasicBlock) { bb.C = C.LLVMGetEntryBasicBlock(v.C); return }
1098 func (c Context) AddBasicBlock(f Value, name string) (bb BasicBlock) {
1099 cname := C.CString(name)
1100 defer C.free(unsafe.Pointer(cname))
1101 bb.C = C.LLVMAppendBasicBlockInContext(c.C, f.C, cname)
1104 func (c Context) InsertBasicBlock(ref BasicBlock, name string) (bb BasicBlock) {
1105 cname := C.CString(name)
1106 defer C.free(unsafe.Pointer(cname))
1107 bb.C = C.LLVMInsertBasicBlockInContext(c.C, ref.C, cname)
1110 func AddBasicBlock(f Value, name string) (bb BasicBlock) {
1111 cname := C.CString(name)
1112 defer C.free(unsafe.Pointer(cname))
1113 bb.C = C.LLVMAppendBasicBlock(f.C, cname)
1116 func InsertBasicBlock(ref BasicBlock, name string) (bb BasicBlock) {
1117 cname := C.CString(name)
1118 defer C.free(unsafe.Pointer(cname))
1119 bb.C = C.LLVMInsertBasicBlock(ref.C, cname)
1122 func (bb BasicBlock) EraseFromParent() { C.LLVMDeleteBasicBlock(bb.C) }
1123 func (bb BasicBlock) MoveBefore(pos BasicBlock) { C.LLVMMoveBasicBlockBefore(bb.C, pos.C) }
1124 func (bb BasicBlock) MoveAfter(pos BasicBlock) { C.LLVMMoveBasicBlockAfter(bb.C, pos.C) }
1126 // Operations on instructions
1127 func (v Value) InstructionParent() (bb BasicBlock) { bb.C = C.LLVMGetInstructionParent(v.C); return }
1128 func (bb BasicBlock) FirstInstruction() (v Value) { v.C = C.LLVMGetFirstInstruction(bb.C); return }
1129 func (bb BasicBlock) LastInstruction() (v Value) { v.C = C.LLVMGetLastInstruction(bb.C); return }
1130 func NextInstruction(v Value) (rv Value) { rv.C = C.LLVMGetNextInstruction(v.C); return }
1131 func PrevInstruction(v Value) (rv Value) { rv.C = C.LLVMGetPreviousInstruction(v.C); return }
1133 // Operations on call sites
1134 func (v Value) SetInstructionCallConv(cc CallConv) {
1135 C.LLVMSetInstructionCallConv(v.C, C.unsigned(cc))
1137 func (v Value) InstructionCallConv() CallConv {
1138 return CallConv(C.LLVMCallConv(C.LLVMGetInstructionCallConv(v.C)))
1140 func (v Value) AddInstrAttribute(i int, a Attribute) {
1142 panic("attribute value currently unsupported")
1144 C.LLVMAddInstrAttribute(v.C, C.unsigned(i), C.LLVMAttribute(a))
1146 func (v Value) RemoveInstrAttribute(i int, a Attribute) {
1148 panic("attribute value currently unsupported")
1150 C.LLVMRemoveInstrAttribute(v.C, C.unsigned(i), C.LLVMAttribute(a))
1152 func (v Value) SetInstrParamAlignment(i int, align int) {
1153 C.LLVMSetInstrParamAlignment(v.C, C.unsigned(i), C.unsigned(align))
1156 // Operations on call instructions (only)
1157 func (v Value) IsTailCall() bool { return C.LLVMIsTailCall(v.C) != 0 }
1158 func (v Value) SetTailCall(is bool) { C.LLVMSetTailCall(v.C, boolToLLVMBool(is)) }
1160 // Operations on phi nodes
1161 func (v Value) AddIncoming(vals []Value, blocks []BasicBlock) {
1162 ptr, nvals := llvmValueRefs(vals)
1163 C.LLVMAddIncoming(v.C, ptr, llvmBasicBlockRefPtr(&blocks[0]), nvals)
1165 func (v Value) IncomingCount() int { return int(C.LLVMCountIncoming(v.C)) }
1166 func (v Value) IncomingValue(i int) (rv Value) {
1167 rv.C = C.LLVMGetIncomingValue(v.C, C.unsigned(i))
1170 func (v Value) IncomingBlock(i int) (bb BasicBlock) {
1171 bb.C = C.LLVMGetIncomingBlock(v.C, C.unsigned(i))
1175 //-------------------------------------------------------------------------
1177 //-------------------------------------------------------------------------
1179 // An instruction builder represents a point within a basic block, and is the
1180 // exclusive means of building instructions using the C interface.
1182 func (c Context) NewBuilder() (b Builder) { b.C = C.LLVMCreateBuilderInContext(c.C); return }
1183 func NewBuilder() (b Builder) { b.C = C.LLVMCreateBuilder(); return }
1184 func (b Builder) SetInsertPoint(block BasicBlock, instr Value) {
1185 C.LLVMPositionBuilder(b.C, block.C, instr.C)
1187 func (b Builder) SetInsertPointBefore(instr Value) { C.LLVMPositionBuilderBefore(b.C, instr.C) }
1188 func (b Builder) SetInsertPointAtEnd(block BasicBlock) { C.LLVMPositionBuilderAtEnd(b.C, block.C) }
1189 func (b Builder) GetInsertBlock() (bb BasicBlock) { bb.C = C.LLVMGetInsertBlock(b.C); return }
1190 func (b Builder) ClearInsertionPoint() { C.LLVMClearInsertionPosition(b.C) }
1191 func (b Builder) Insert(instr Value) { C.LLVMInsertIntoBuilder(b.C, instr.C) }
1192 func (b Builder) InsertWithName(instr Value, name string) {
1193 cname := C.CString(name)
1194 defer C.free(unsafe.Pointer(cname))
1195 C.LLVMInsertIntoBuilderWithName(b.C, instr.C, cname)
1197 func (b Builder) Dispose() { C.LLVMDisposeBuilder(b.C) }
1200 func (b Builder) SetCurrentDebugLocation(line, col uint, scope, inlinedAt Metadata) {
1201 C.LLVMSetCurrentDebugLocation2(b.C, C.unsigned(line), C.unsigned(col), scope.C, inlinedAt.C)
1203 func (b Builder) SetInstDebugLocation(v Value) { C.LLVMSetInstDebugLocation(b.C, v.C) }
1204 func (b Builder) InsertDeclare(module Module, storage Value, md Value) Value {
1205 f := module.NamedFunction("llvm.dbg.declare")
1207 ftyp := FunctionType(VoidType(), []Type{storage.Type(), md.Type()}, false)
1208 f = AddFunction(module, "llvm.dbg.declare", ftyp)
1210 return b.CreateCall(f, []Value{storage, md}, "")
1214 func (b Builder) CreateRetVoid() (rv Value) { rv.C = C.LLVMBuildRetVoid(b.C); return }
1215 func (b Builder) CreateRet(v Value) (rv Value) { rv.C = C.LLVMBuildRet(b.C, v.C); return }
1216 func (b Builder) CreateAggregateRet(vs []Value) (rv Value) {
1217 ptr, nvals := llvmValueRefs(vs)
1218 rv.C = C.LLVMBuildAggregateRet(b.C, ptr, nvals)
1221 func (b Builder) CreateBr(bb BasicBlock) (rv Value) { rv.C = C.LLVMBuildBr(b.C, bb.C); return }
1222 func (b Builder) CreateCondBr(ifv Value, thenb, elseb BasicBlock) (rv Value) {
1223 rv.C = C.LLVMBuildCondBr(b.C, ifv.C, thenb.C, elseb.C)
1226 func (b Builder) CreateSwitch(v Value, elseb BasicBlock, numCases int) (rv Value) {
1227 rv.C = C.LLVMBuildSwitch(b.C, v.C, elseb.C, C.unsigned(numCases))
1230 func (b Builder) CreateIndirectBr(addr Value, numDests int) (rv Value) {
1231 rv.C = C.LLVMBuildIndirectBr(b.C, addr.C, C.unsigned(numDests))
1234 func (b Builder) CreateInvoke(fn Value, args []Value, then, catch BasicBlock, name string) (rv Value) {
1235 cname := C.CString(name)
1236 defer C.free(unsafe.Pointer(cname))
1237 ptr, nvals := llvmValueRefs(args)
1238 rv.C = C.LLVMBuildInvoke(b.C, fn.C, ptr, nvals, then.C, catch.C, cname)
1241 func (b Builder) CreateUnreachable() (rv Value) { rv.C = C.LLVMBuildUnreachable(b.C); return }
1243 // Add a case to the switch instruction
1244 func (v Value) AddCase(on Value, dest BasicBlock) { C.LLVMAddCase(v.C, on.C, dest.C) }
1246 // Add a destination to the indirectbr instruction
1247 func (v Value) AddDest(dest BasicBlock) { C.LLVMAddDestination(v.C, dest.C) }
1250 func (b Builder) CreateAdd(lhs, rhs Value, name string) (v Value) {
1251 cname := C.CString(name)
1252 defer C.free(unsafe.Pointer(cname))
1253 v.C = C.LLVMBuildAdd(b.C, lhs.C, rhs.C, cname)
1256 func (b Builder) CreateNSWAdd(lhs, rhs Value, name string) (v Value) {
1257 cname := C.CString(name)
1258 defer C.free(unsafe.Pointer(cname))
1259 v.C = C.LLVMBuildNSWAdd(b.C, lhs.C, rhs.C, cname)
1262 func (b Builder) CreateNUWAdd(lhs, rhs Value, name string) (v Value) {
1263 cname := C.CString(name)
1264 defer C.free(unsafe.Pointer(cname))
1265 v.C = C.LLVMBuildNUWAdd(b.C, lhs.C, rhs.C, cname)
1268 func (b Builder) CreateFAdd(lhs, rhs Value, name string) (v Value) {
1269 cname := C.CString(name)
1270 defer C.free(unsafe.Pointer(cname))
1271 v.C = C.LLVMBuildFAdd(b.C, lhs.C, rhs.C, cname)
1274 func (b Builder) CreateSub(lhs, rhs Value, name string) (v Value) {
1275 cname := C.CString(name)
1276 defer C.free(unsafe.Pointer(cname))
1277 v.C = C.LLVMBuildSub(b.C, lhs.C, rhs.C, cname)
1280 func (b Builder) CreateNSWSub(lhs, rhs Value, name string) (v Value) {
1281 cname := C.CString(name)
1282 defer C.free(unsafe.Pointer(cname))
1283 v.C = C.LLVMBuildNSWSub(b.C, lhs.C, rhs.C, cname)
1286 func (b Builder) CreateNUWSub(lhs, rhs Value, name string) (v Value) {
1287 cname := C.CString(name)
1288 defer C.free(unsafe.Pointer(cname))
1289 v.C = C.LLVMBuildNUWSub(b.C, lhs.C, rhs.C, cname)
1292 func (b Builder) CreateFSub(lhs, rhs Value, name string) (v Value) {
1293 cname := C.CString(name)
1294 v.C = C.LLVMBuildFSub(b.C, lhs.C, rhs.C, cname)
1295 C.free(unsafe.Pointer(cname))
1298 func (b Builder) CreateMul(lhs, rhs Value, name string) (v Value) {
1299 cname := C.CString(name)
1300 defer C.free(unsafe.Pointer(cname))
1301 v.C = C.LLVMBuildMul(b.C, lhs.C, rhs.C, cname)
1304 func (b Builder) CreateNSWMul(lhs, rhs Value, name string) (v Value) {
1305 cname := C.CString(name)
1306 defer C.free(unsafe.Pointer(cname))
1307 v.C = C.LLVMBuildNSWMul(b.C, lhs.C, rhs.C, cname)
1310 func (b Builder) CreateNUWMul(lhs, rhs Value, name string) (v Value) {
1311 cname := C.CString(name)
1312 defer C.free(unsafe.Pointer(cname))
1313 v.C = C.LLVMBuildNUWMul(b.C, lhs.C, rhs.C, cname)
1316 func (b Builder) CreateFMul(lhs, rhs Value, name string) (v Value) {
1317 cname := C.CString(name)
1318 defer C.free(unsafe.Pointer(cname))
1319 v.C = C.LLVMBuildFMul(b.C, lhs.C, rhs.C, cname)
1322 func (b Builder) CreateUDiv(lhs, rhs Value, name string) (v Value) {
1323 cname := C.CString(name)
1324 defer C.free(unsafe.Pointer(cname))
1325 v.C = C.LLVMBuildUDiv(b.C, lhs.C, rhs.C, cname)
1328 func (b Builder) CreateSDiv(lhs, rhs Value, name string) (v Value) {
1329 cname := C.CString(name)
1330 defer C.free(unsafe.Pointer(cname))
1331 v.C = C.LLVMBuildSDiv(b.C, lhs.C, rhs.C, cname)
1334 func (b Builder) CreateExactSDiv(lhs, rhs Value, name string) (v Value) {
1335 cname := C.CString(name)
1336 defer C.free(unsafe.Pointer(cname))
1337 v.C = C.LLVMBuildExactSDiv(b.C, lhs.C, rhs.C, cname)
1340 func (b Builder) CreateFDiv(lhs, rhs Value, name string) (v Value) {
1341 cname := C.CString(name)
1342 defer C.free(unsafe.Pointer(cname))
1343 v.C = C.LLVMBuildFDiv(b.C, lhs.C, rhs.C, cname)
1346 func (b Builder) CreateURem(lhs, rhs Value, name string) (v Value) {
1347 cname := C.CString(name)
1348 defer C.free(unsafe.Pointer(cname))
1349 v.C = C.LLVMBuildURem(b.C, lhs.C, rhs.C, cname)
1352 func (b Builder) CreateSRem(lhs, rhs Value, name string) (v Value) {
1353 cname := C.CString(name)
1354 defer C.free(unsafe.Pointer(cname))
1355 v.C = C.LLVMBuildSRem(b.C, lhs.C, rhs.C, cname)
1358 func (b Builder) CreateFRem(lhs, rhs Value, name string) (v Value) {
1359 cname := C.CString(name)
1360 defer C.free(unsafe.Pointer(cname))
1361 v.C = C.LLVMBuildFRem(b.C, lhs.C, rhs.C, cname)
1364 func (b Builder) CreateShl(lhs, rhs Value, name string) (v Value) {
1365 cname := C.CString(name)
1366 defer C.free(unsafe.Pointer(cname))
1367 v.C = C.LLVMBuildShl(b.C, lhs.C, rhs.C, cname)
1370 func (b Builder) CreateLShr(lhs, rhs Value, name string) (v Value) {
1371 cname := C.CString(name)
1372 defer C.free(unsafe.Pointer(cname))
1373 v.C = C.LLVMBuildLShr(b.C, lhs.C, rhs.C, cname)
1376 func (b Builder) CreateAShr(lhs, rhs Value, name string) (v Value) {
1377 cname := C.CString(name)
1378 defer C.free(unsafe.Pointer(cname))
1379 v.C = C.LLVMBuildAShr(b.C, lhs.C, rhs.C, cname)
1382 func (b Builder) CreateAnd(lhs, rhs Value, name string) (v Value) {
1383 cname := C.CString(name)
1384 defer C.free(unsafe.Pointer(cname))
1385 v.C = C.LLVMBuildAnd(b.C, lhs.C, rhs.C, cname)
1388 func (b Builder) CreateOr(lhs, rhs Value, name string) (v Value) {
1389 cname := C.CString(name)
1390 defer C.free(unsafe.Pointer(cname))
1391 v.C = C.LLVMBuildOr(b.C, lhs.C, rhs.C, cname)
1394 func (b Builder) CreateXor(lhs, rhs Value, name string) (v Value) {
1395 cname := C.CString(name)
1396 defer C.free(unsafe.Pointer(cname))
1397 v.C = C.LLVMBuildXor(b.C, lhs.C, rhs.C, cname)
1400 func (b Builder) CreateBinOp(op Opcode, lhs, rhs Value, name string) (v Value) {
1401 cname := C.CString(name)
1402 defer C.free(unsafe.Pointer(cname))
1403 v.C = C.LLVMBuildBinOp(b.C, C.LLVMOpcode(op), lhs.C, rhs.C, cname)
1406 func (b Builder) CreateNeg(v Value, name string) (rv Value) {
1407 cname := C.CString(name)
1408 defer C.free(unsafe.Pointer(cname))
1409 rv.C = C.LLVMBuildNeg(b.C, v.C, cname)
1412 func (b Builder) CreateNSWNeg(v Value, name string) (rv Value) {
1413 cname := C.CString(name)
1414 defer C.free(unsafe.Pointer(cname))
1415 rv.C = C.LLVMBuildNSWNeg(b.C, v.C, cname)
1418 func (b Builder) CreateNUWNeg(v Value, name string) (rv Value) {
1419 cname := C.CString(name)
1420 defer C.free(unsafe.Pointer(cname))
1421 rv.C = C.LLVMBuildNUWNeg(b.C, v.C, cname)
1424 func (b Builder) CreateFNeg(v Value, name string) (rv Value) {
1425 cname := C.CString(name)
1426 defer C.free(unsafe.Pointer(cname))
1427 rv.C = C.LLVMBuildFNeg(b.C, v.C, cname)
1430 func (b Builder) CreateNot(v Value, name string) (rv Value) {
1431 cname := C.CString(name)
1432 defer C.free(unsafe.Pointer(cname))
1433 rv.C = C.LLVMBuildNot(b.C, v.C, cname)
1439 func (b Builder) CreateMalloc(t Type, name string) (v Value) {
1440 cname := C.CString(name)
1441 defer C.free(unsafe.Pointer(cname))
1442 v.C = C.LLVMBuildMalloc(b.C, t.C, cname)
1445 func (b Builder) CreateArrayMalloc(t Type, val Value, name string) (v Value) {
1446 cname := C.CString(name)
1447 defer C.free(unsafe.Pointer(cname))
1448 v.C = C.LLVMBuildArrayMalloc(b.C, t.C, val.C, cname)
1451 func (b Builder) CreateAlloca(t Type, name string) (v Value) {
1452 cname := C.CString(name)
1453 defer C.free(unsafe.Pointer(cname))
1454 v.C = C.LLVMBuildAlloca(b.C, t.C, cname)
1457 func (b Builder) CreateArrayAlloca(t Type, val Value, name string) (v Value) {
1458 cname := C.CString(name)
1459 defer C.free(unsafe.Pointer(cname))
1460 v.C = C.LLVMBuildArrayAlloca(b.C, t.C, val.C, cname)
1463 func (b Builder) CreateFree(p Value) (v Value) {
1464 v.C = C.LLVMBuildFree(b.C, p.C)
1467 func (b Builder) CreateLoad(p Value, name string) (v Value) {
1468 cname := C.CString(name)
1469 defer C.free(unsafe.Pointer(cname))
1470 v.C = C.LLVMBuildLoad(b.C, p.C, cname)
1473 func (b Builder) CreateStore(val Value, p Value) (v Value) {
1474 v.C = C.LLVMBuildStore(b.C, val.C, p.C)
1477 func (b Builder) CreateGEP(p Value, indices []Value, name string) (v Value) {
1478 cname := C.CString(name)
1479 defer C.free(unsafe.Pointer(cname))
1480 ptr, nvals := llvmValueRefs(indices)
1481 v.C = C.LLVMBuildGEP(b.C, p.C, ptr, nvals, cname)
1484 func (b Builder) CreateInBoundsGEP(p Value, indices []Value, name string) (v Value) {
1485 cname := C.CString(name)
1486 defer C.free(unsafe.Pointer(cname))
1487 ptr, nvals := llvmValueRefs(indices)
1488 v.C = C.LLVMBuildInBoundsGEP(b.C, p.C, ptr, nvals, cname)
1491 func (b Builder) CreateStructGEP(p Value, i int, name string) (v Value) {
1492 cname := C.CString(name)
1493 defer C.free(unsafe.Pointer(cname))
1494 v.C = C.LLVMBuildStructGEP(b.C, p.C, C.unsigned(i), cname)
1497 func (b Builder) CreateGlobalString(str, name string) (v Value) {
1498 cstr := C.CString(str)
1499 defer C.free(unsafe.Pointer(cstr))
1500 cname := C.CString(name)
1501 defer C.free(unsafe.Pointer(cname))
1502 v.C = C.LLVMBuildGlobalString(b.C, cstr, cname)
1505 func (b Builder) CreateGlobalStringPtr(str, name string) (v Value) {
1506 cstr := C.CString(str)
1507 defer C.free(unsafe.Pointer(cstr))
1508 cname := C.CString(name)
1509 defer C.free(unsafe.Pointer(cname))
1510 v.C = C.LLVMBuildGlobalStringPtr(b.C, cstr, cname)
1515 func (b Builder) CreateTrunc(val Value, t Type, name string) (v Value) {
1516 cname := C.CString(name)
1517 defer C.free(unsafe.Pointer(cname))
1518 v.C = C.LLVMBuildTrunc(b.C, val.C, t.C, cname)
1521 func (b Builder) CreateZExt(val Value, t Type, name string) (v Value) {
1522 cname := C.CString(name)
1523 defer C.free(unsafe.Pointer(cname))
1524 v.C = C.LLVMBuildZExt(b.C, val.C, t.C, cname)
1527 func (b Builder) CreateSExt(val Value, t Type, name string) (v Value) {
1528 cname := C.CString(name)
1529 defer C.free(unsafe.Pointer(cname))
1530 v.C = C.LLVMBuildSExt(b.C, val.C, t.C, cname)
1533 func (b Builder) CreateFPToUI(val Value, t Type, name string) (v Value) {
1534 cname := C.CString(name)
1535 defer C.free(unsafe.Pointer(cname))
1536 v.C = C.LLVMBuildFPToUI(b.C, val.C, t.C, cname)
1539 func (b Builder) CreateFPToSI(val Value, t Type, name string) (v Value) {
1540 cname := C.CString(name)
1541 defer C.free(unsafe.Pointer(cname))
1542 v.C = C.LLVMBuildFPToSI(b.C, val.C, t.C, cname)
1545 func (b Builder) CreateUIToFP(val Value, t Type, name string) (v Value) {
1546 cname := C.CString(name)
1547 defer C.free(unsafe.Pointer(cname))
1548 v.C = C.LLVMBuildUIToFP(b.C, val.C, t.C, cname)
1551 func (b Builder) CreateSIToFP(val Value, t Type, name string) (v Value) {
1552 cname := C.CString(name)
1553 defer C.free(unsafe.Pointer(cname))
1554 v.C = C.LLVMBuildSIToFP(b.C, val.C, t.C, cname)
1557 func (b Builder) CreateFPTrunc(val Value, t Type, name string) (v Value) {
1558 cname := C.CString(name)
1559 defer C.free(unsafe.Pointer(cname))
1560 v.C = C.LLVMBuildFPTrunc(b.C, val.C, t.C, cname)
1563 func (b Builder) CreateFPExt(val Value, t Type, name string) (v Value) {
1564 cname := C.CString(name)
1565 defer C.free(unsafe.Pointer(cname))
1566 v.C = C.LLVMBuildFPExt(b.C, val.C, t.C, cname)
1569 func (b Builder) CreatePtrToInt(val Value, t Type, name string) (v Value) {
1570 cname := C.CString(name)
1571 defer C.free(unsafe.Pointer(cname))
1572 v.C = C.LLVMBuildPtrToInt(b.C, val.C, t.C, cname)
1575 func (b Builder) CreateIntToPtr(val Value, t Type, name string) (v Value) {
1576 cname := C.CString(name)
1577 defer C.free(unsafe.Pointer(cname))
1578 v.C = C.LLVMBuildIntToPtr(b.C, val.C, t.C, cname)
1581 func (b Builder) CreateBitCast(val Value, t Type, name string) (v Value) {
1582 cname := C.CString(name)
1583 defer C.free(unsafe.Pointer(cname))
1584 v.C = C.LLVMBuildBitCast(b.C, val.C, t.C, cname)
1587 func (b Builder) CreateZExtOrBitCast(val Value, t Type, name string) (v Value) {
1588 cname := C.CString(name)
1589 defer C.free(unsafe.Pointer(cname))
1590 v.C = C.LLVMBuildZExtOrBitCast(b.C, val.C, t.C, cname)
1593 func (b Builder) CreateSExtOrBitCast(val Value, t Type, name string) (v Value) {
1594 cname := C.CString(name)
1595 defer C.free(unsafe.Pointer(cname))
1596 v.C = C.LLVMBuildSExtOrBitCast(b.C, val.C, t.C, cname)
1599 func (b Builder) CreateTruncOrBitCast(val Value, t Type, name string) (v Value) {
1600 cname := C.CString(name)
1601 defer C.free(unsafe.Pointer(cname))
1602 v.C = C.LLVMBuildTruncOrBitCast(b.C, val.C, t.C, cname)
1605 func (b Builder) CreateCast(val Value, op Opcode, t Type, name string) (v Value) {
1606 cname := C.CString(name)
1607 defer C.free(unsafe.Pointer(cname))
1608 v.C = C.LLVMBuildCast(b.C, C.LLVMOpcode(op), val.C, t.C, cname)
1611 func (b Builder) CreatePointerCast(val Value, t Type, name string) (v Value) {
1612 cname := C.CString(name)
1613 defer C.free(unsafe.Pointer(cname))
1614 v.C = C.LLVMBuildPointerCast(b.C, val.C, t.C, cname)
1617 func (b Builder) CreateIntCast(val Value, t Type, name string) (v Value) {
1618 cname := C.CString(name)
1619 defer C.free(unsafe.Pointer(cname))
1620 v.C = C.LLVMBuildIntCast(b.C, val.C, t.C, cname)
1623 func (b Builder) CreateFPCast(val Value, t Type, name string) (v Value) {
1624 cname := C.CString(name)
1625 defer C.free(unsafe.Pointer(cname))
1626 v.C = C.LLVMBuildFPCast(b.C, val.C, t.C, cname)
1631 func (b Builder) CreateICmp(pred IntPredicate, lhs, rhs Value, name string) (v Value) {
1632 cname := C.CString(name)
1633 defer C.free(unsafe.Pointer(cname))
1634 v.C = C.LLVMBuildICmp(b.C, C.LLVMIntPredicate(pred), lhs.C, rhs.C, cname)
1637 func (b Builder) CreateFCmp(pred FloatPredicate, lhs, rhs Value, name string) (v Value) {
1638 cname := C.CString(name)
1639 defer C.free(unsafe.Pointer(cname))
1640 v.C = C.LLVMBuildFCmp(b.C, C.LLVMRealPredicate(pred), lhs.C, rhs.C, cname)
1644 // Miscellaneous instructions
1645 func (b Builder) CreatePHI(t Type, name string) (v Value) {
1646 cname := C.CString(name)
1647 defer C.free(unsafe.Pointer(cname))
1648 v.C = C.LLVMBuildPhi(b.C, t.C, cname)
1651 func (b Builder) CreateCall(fn Value, args []Value, name string) (v Value) {
1652 cname := C.CString(name)
1653 defer C.free(unsafe.Pointer(cname))
1654 ptr, nvals := llvmValueRefs(args)
1655 v.C = C.LLVMBuildCall(b.C, fn.C, ptr, nvals, cname)
1659 func (b Builder) CreateSelect(ifv, thenv, elsev Value, name string) (v Value) {
1660 cname := C.CString(name)
1661 defer C.free(unsafe.Pointer(cname))
1662 v.C = C.LLVMBuildSelect(b.C, ifv.C, thenv.C, elsev.C, cname)
1666 func (b Builder) CreateVAArg(list Value, t Type, name string) (v Value) {
1667 cname := C.CString(name)
1668 defer C.free(unsafe.Pointer(cname))
1669 v.C = C.LLVMBuildVAArg(b.C, list.C, t.C, cname)
1672 func (b Builder) CreateExtractElement(vec, i Value, name string) (v Value) {
1673 cname := C.CString(name)
1674 defer C.free(unsafe.Pointer(cname))
1675 v.C = C.LLVMBuildExtractElement(b.C, vec.C, i.C, cname)
1678 func (b Builder) CreateInsertElement(vec, elt, i Value, name string) (v Value) {
1679 cname := C.CString(name)
1680 defer C.free(unsafe.Pointer(cname))
1681 v.C = C.LLVMBuildInsertElement(b.C, vec.C, elt.C, i.C, cname)
1684 func (b Builder) CreateShuffleVector(v1, v2, mask Value, name string) (v Value) {
1685 cname := C.CString(name)
1686 defer C.free(unsafe.Pointer(cname))
1687 v.C = C.LLVMBuildShuffleVector(b.C, v1.C, v2.C, mask.C, cname)
1690 func (b Builder) CreateExtractValue(agg Value, i int, name string) (v Value) {
1691 cname := C.CString(name)
1692 defer C.free(unsafe.Pointer(cname))
1693 v.C = C.LLVMBuildExtractValue(b.C, agg.C, C.unsigned(i), cname)
1696 func (b Builder) CreateInsertValue(agg, elt Value, i int, name string) (v Value) {
1697 cname := C.CString(name)
1698 defer C.free(unsafe.Pointer(cname))
1699 v.C = C.LLVMBuildInsertValue(b.C, agg.C, elt.C, C.unsigned(i), cname)
1703 func (b Builder) CreateIsNull(val Value, name string) (v Value) {
1704 cname := C.CString(name)
1705 defer C.free(unsafe.Pointer(cname))
1706 v.C = C.LLVMBuildIsNull(b.C, val.C, cname)
1709 func (b Builder) CreateIsNotNull(val Value, name string) (v Value) {
1710 cname := C.CString(name)
1711 defer C.free(unsafe.Pointer(cname))
1712 v.C = C.LLVMBuildIsNotNull(b.C, val.C, cname)
1715 func (b Builder) CreatePtrDiff(lhs, rhs Value, name string) (v Value) {
1716 cname := C.CString(name)
1717 defer C.free(unsafe.Pointer(cname))
1718 v.C = C.LLVMBuildPtrDiff(b.C, lhs.C, rhs.C, cname)
1722 func (b Builder) CreateLandingPad(t Type, personality Value, nclauses int, name string) (l Value) {
1723 cname := C.CString(name)
1724 defer C.free(unsafe.Pointer(cname))
1725 l.C = C.LLVMBuildLandingPad(b.C, t.C, personality.C, C.unsigned(nclauses), cname)
1729 func (l Value) AddClause(v Value) {
1730 C.LLVMAddClause(l.C, v.C)
1733 func (l Value) SetCleanup(cleanup bool) {
1734 C.LLVMSetCleanup(l.C, boolToLLVMBool(cleanup))
1737 func (b Builder) CreateResume(ex Value) (v Value) {
1738 v.C = C.LLVMBuildResume(b.C, ex.C)
1742 //-------------------------------------------------------------------------
1743 // llvm.ModuleProvider
1744 //-------------------------------------------------------------------------
1746 // Changes the type of M so it can be passed to FunctionPassManagers and the
1747 // JIT. They take ModuleProviders for historical reasons.
1748 func NewModuleProviderForModule(m Module) (mp ModuleProvider) {
1749 mp.C = C.LLVMCreateModuleProviderForExistingModule(m.C)
1753 // Destroys the module M.
1754 func (mp ModuleProvider) Dispose() { C.LLVMDisposeModuleProvider(mp.C) }
1756 //-------------------------------------------------------------------------
1757 // llvm.MemoryBuffer
1758 //-------------------------------------------------------------------------
1760 func NewMemoryBufferFromFile(path string) (b MemoryBuffer, err error) {
1762 cpath := C.CString(path)
1763 defer C.free(unsafe.Pointer(cpath))
1764 fail := C.LLVMCreateMemoryBufferWithContentsOfFile(cpath, &b.C, &cmsg)
1767 err = errors.New(C.GoString(cmsg))
1768 C.LLVMDisposeMessage(cmsg)
1773 func NewMemoryBufferFromStdin() (b MemoryBuffer, err error) {
1775 fail := C.LLVMCreateMemoryBufferWithSTDIN(&b.C, &cmsg)
1778 err = errors.New(C.GoString(cmsg))
1779 C.LLVMDisposeMessage(cmsg)
1784 func (b MemoryBuffer) Bytes() []byte {
1785 cstart := C.LLVMGetBufferStart(b.C)
1786 csize := C.LLVMGetBufferSize(b.C)
1787 return C.GoBytes(unsafe.Pointer(cstart), C.int(csize))
1790 func (b MemoryBuffer) Dispose() { C.LLVMDisposeMemoryBuffer(b.C) }
1792 //-------------------------------------------------------------------------
1794 //-------------------------------------------------------------------------
1796 // Constructs a new whole-module pass pipeline. This type of pipeline is
1797 // suitable for link-time optimization and whole-module transformations.
1798 // See llvm::PassManager::PassManager.
1799 func NewPassManager() (pm PassManager) { pm.C = C.LLVMCreatePassManager(); return }
1801 // Constructs a new function-by-function pass pipeline over the module
1802 // provider. It does not take ownership of the module provider. This type of
1803 // pipeline is suitable for code generation and JIT compilation tasks.
1804 // See llvm::FunctionPassManager::FunctionPassManager.
1805 func NewFunctionPassManagerForModule(m Module) (pm PassManager) {
1806 pm.C = C.LLVMCreateFunctionPassManagerForModule(m.C)
1810 // Initializes, executes on the provided module, and finalizes all of the
1811 // passes scheduled in the pass manager. Returns 1 if any of the passes
1812 // modified the module, 0 otherwise. See llvm::PassManager::run(Module&).
1813 func (pm PassManager) Run(m Module) bool { return C.LLVMRunPassManager(pm.C, m.C) != 0 }
1815 // Initializes all of the function passes scheduled in the function pass
1816 // manager. Returns 1 if any of the passes modified the module, 0 otherwise.
1817 // See llvm::FunctionPassManager::doInitialization.
1818 func (pm PassManager) InitializeFunc() bool { return C.LLVMInitializeFunctionPassManager(pm.C) != 0 }
1820 // Executes all of the function passes scheduled in the function pass manager
1821 // on the provided function. Returns 1 if any of the passes modified the
1822 // function, false otherwise.
1823 // See llvm::FunctionPassManager::run(Function&).
1824 func (pm PassManager) RunFunc(f Value) bool { return C.LLVMRunFunctionPassManager(pm.C, f.C) != 0 }
1826 // Finalizes all of the function passes scheduled in in the function pass
1827 // manager. Returns 1 if any of the passes modified the module, 0 otherwise.
1828 // See llvm::FunctionPassManager::doFinalization.
1829 func (pm PassManager) FinalizeFunc() bool { return C.LLVMFinalizeFunctionPassManager(pm.C) != 0 }
1831 // Frees the memory of a pass pipeline. For function pipelines, does not free
1832 // the module provider.
1833 // See llvm::PassManagerBase::~PassManagerBase.
1834 func (pm PassManager) Dispose() { C.LLVMDisposePassManager(pm.C) }
1836 //-------------------------------------------------------------------------
1838 //-------------------------------------------------------------------------
1840 func (md Metadata) ReplaceAllUsesWith(new Metadata) {
1841 C.LLVMMetadataReplaceAllUsesWith(md.C, new.C)