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)
757 func (v Value) ConstantAsMetadata() (md Metadata) {
758 md.C = C.LLVMConstantAsMetadata(v.C)
762 // Operations on scalar constants
763 func ConstInt(t Type, n uint64, signExtend bool) (v Value) {
764 v.C = C.LLVMConstInt(t.C,
766 boolToLLVMBool(signExtend))
769 func ConstIntFromString(t Type, str string, radix int) (v Value) {
770 cstr := C.CString(str)
771 defer C.free(unsafe.Pointer(cstr))
772 v.C = C.LLVMConstIntOfString(t.C, cstr, C.uint8_t(radix))
775 func ConstFloat(t Type, n float64) (v Value) {
776 v.C = C.LLVMConstReal(t.C, C.double(n))
779 func ConstFloatFromString(t Type, str string) (v Value) {
780 cstr := C.CString(str)
781 defer C.free(unsafe.Pointer(cstr))
782 v.C = C.LLVMConstRealOfString(t.C, cstr)
786 func (v Value) ZExtValue() uint64 { return uint64(C.LLVMConstIntGetZExtValue(v.C)) }
787 func (v Value) SExtValue() int64 { return int64(C.LLVMConstIntGetSExtValue(v.C)) }
789 // Operations on composite constants
790 func (c Context) ConstString(str string, addnull bool) (v Value) {
791 cstr := C.CString(str)
792 defer C.free(unsafe.Pointer(cstr))
793 v.C = C.LLVMConstStringInContext(c.C, cstr,
794 C.unsigned(len(str)), boolToLLVMBool(!addnull))
797 func (c Context) ConstStruct(constVals []Value, packed bool) (v Value) {
798 ptr, nvals := llvmValueRefs(constVals)
799 v.C = C.LLVMConstStructInContext(c.C, ptr, nvals,
800 boolToLLVMBool(packed))
803 func ConstNamedStruct(t Type, constVals []Value) (v Value) {
804 ptr, nvals := llvmValueRefs(constVals)
805 v.C = C.LLVMConstNamedStruct(t.C, ptr, nvals)
808 func ConstString(str string, addnull bool) (v Value) {
809 cstr := C.CString(str)
810 defer C.free(unsafe.Pointer(cstr))
811 v.C = C.LLVMConstString(cstr,
812 C.unsigned(len(str)), boolToLLVMBool(!addnull))
815 func ConstArray(t Type, constVals []Value) (v Value) {
816 ptr, nvals := llvmValueRefs(constVals)
817 v.C = C.LLVMConstArray(t.C, ptr, nvals)
820 func ConstStruct(constVals []Value, packed bool) (v Value) {
821 ptr, nvals := llvmValueRefs(constVals)
822 v.C = C.LLVMConstStruct(ptr, nvals, boolToLLVMBool(packed))
825 func ConstVector(scalarConstVals []Value, packed bool) (v Value) {
826 ptr, nvals := llvmValueRefs(scalarConstVals)
827 v.C = C.LLVMConstVector(ptr, nvals)
831 // Constant expressions
832 func (v Value) Opcode() Opcode { return Opcode(C.LLVMGetConstOpcode(v.C)) }
833 func (v Value) InstructionOpcode() Opcode { return Opcode(C.LLVMGetInstructionOpcode(v.C)) }
834 func AlignOf(t Type) (v Value) { v.C = C.LLVMAlignOf(t.C); return }
835 func SizeOf(t Type) (v Value) { v.C = C.LLVMSizeOf(t.C); return }
836 func ConstNeg(v Value) (rv Value) { rv.C = C.LLVMConstNeg(v.C); return }
837 func ConstNSWNeg(v Value) (rv Value) { rv.C = C.LLVMConstNSWNeg(v.C); return }
838 func ConstNUWNeg(v Value) (rv Value) { rv.C = C.LLVMConstNUWNeg(v.C); return }
839 func ConstFNeg(v Value) (rv Value) { rv.C = C.LLVMConstFNeg(v.C); return }
840 func ConstNot(v Value) (rv Value) { rv.C = C.LLVMConstNot(v.C); return }
841 func ConstAdd(lhs, rhs Value) (v Value) { v.C = C.LLVMConstAdd(lhs.C, rhs.C); return }
842 func ConstNSWAdd(lhs, rhs Value) (v Value) { v.C = C.LLVMConstNSWAdd(lhs.C, rhs.C); return }
843 func ConstNUWAdd(lhs, rhs Value) (v Value) { v.C = C.LLVMConstNUWAdd(lhs.C, rhs.C); return }
844 func ConstFAdd(lhs, rhs Value) (v Value) { v.C = C.LLVMConstFAdd(lhs.C, rhs.C); return }
845 func ConstSub(lhs, rhs Value) (v Value) { v.C = C.LLVMConstSub(lhs.C, rhs.C); return }
846 func ConstNSWSub(lhs, rhs Value) (v Value) { v.C = C.LLVMConstNSWSub(lhs.C, rhs.C); return }
847 func ConstNUWSub(lhs, rhs Value) (v Value) { v.C = C.LLVMConstNUWSub(lhs.C, rhs.C); return }
848 func ConstFSub(lhs, rhs Value) (v Value) { v.C = C.LLVMConstFSub(lhs.C, rhs.C); return }
849 func ConstMul(lhs, rhs Value) (v Value) { v.C = C.LLVMConstMul(lhs.C, rhs.C); return }
850 func ConstNSWMul(lhs, rhs Value) (v Value) { v.C = C.LLVMConstNSWMul(lhs.C, rhs.C); return }
851 func ConstNUWMul(lhs, rhs Value) (v Value) { v.C = C.LLVMConstNUWMul(lhs.C, rhs.C); return }
852 func ConstFMul(lhs, rhs Value) (v Value) { v.C = C.LLVMConstFMul(lhs.C, rhs.C); return }
853 func ConstUDiv(lhs, rhs Value) (v Value) { v.C = C.LLVMConstUDiv(lhs.C, rhs.C); return }
854 func ConstSDiv(lhs, rhs Value) (v Value) { v.C = C.LLVMConstSDiv(lhs.C, rhs.C); return }
855 func ConstExactSDiv(lhs, rhs Value) (v Value) { v.C = C.LLVMConstExactSDiv(lhs.C, rhs.C); return }
856 func ConstFDiv(lhs, rhs Value) (v Value) { v.C = C.LLVMConstFDiv(lhs.C, rhs.C); return }
857 func ConstURem(lhs, rhs Value) (v Value) { v.C = C.LLVMConstURem(lhs.C, rhs.C); return }
858 func ConstSRem(lhs, rhs Value) (v Value) { v.C = C.LLVMConstSRem(lhs.C, rhs.C); return }
859 func ConstFRem(lhs, rhs Value) (v Value) { v.C = C.LLVMConstFRem(lhs.C, rhs.C); return }
860 func ConstAnd(lhs, rhs Value) (v Value) { v.C = C.LLVMConstAnd(lhs.C, rhs.C); return }
861 func ConstOr(lhs, rhs Value) (v Value) { v.C = C.LLVMConstOr(lhs.C, rhs.C); return }
862 func ConstXor(lhs, rhs Value) (v Value) { v.C = C.LLVMConstXor(lhs.C, rhs.C); return }
864 func ConstICmp(pred IntPredicate, lhs, rhs Value) (v Value) {
865 v.C = C.LLVMConstICmp(C.LLVMIntPredicate(pred), lhs.C, rhs.C)
868 func ConstFCmp(pred FloatPredicate, lhs, rhs Value) (v Value) {
869 v.C = C.LLVMConstFCmp(C.LLVMRealPredicate(pred), lhs.C, rhs.C)
873 func ConstShl(lhs, rhs Value) (v Value) { v.C = C.LLVMConstShl(lhs.C, rhs.C); return }
874 func ConstLShr(lhs, rhs Value) (v Value) { v.C = C.LLVMConstLShr(lhs.C, rhs.C); return }
875 func ConstAShr(lhs, rhs Value) (v Value) { v.C = C.LLVMConstAShr(lhs.C, rhs.C); return }
877 func ConstGEP(v Value, indices []Value) (rv Value) {
878 ptr, nvals := llvmValueRefs(indices)
879 rv.C = C.LLVMConstGEP(v.C, ptr, nvals)
882 func ConstInBoundsGEP(v Value, indices []Value) (rv Value) {
883 ptr, nvals := llvmValueRefs(indices)
884 rv.C = C.LLVMConstInBoundsGEP(v.C, ptr, nvals)
887 func ConstTrunc(v Value, t Type) (rv Value) { rv.C = C.LLVMConstTrunc(v.C, t.C); return }
888 func ConstSExt(v Value, t Type) (rv Value) { rv.C = C.LLVMConstSExt(v.C, t.C); return }
889 func ConstZExt(v Value, t Type) (rv Value) { rv.C = C.LLVMConstZExt(v.C, t.C); return }
890 func ConstFPTrunc(v Value, t Type) (rv Value) { rv.C = C.LLVMConstFPTrunc(v.C, t.C); return }
891 func ConstFPExt(v Value, t Type) (rv Value) { rv.C = C.LLVMConstFPExt(v.C, t.C); return }
892 func ConstUIToFP(v Value, t Type) (rv Value) { rv.C = C.LLVMConstUIToFP(v.C, t.C); return }
893 func ConstSIToFP(v Value, t Type) (rv Value) { rv.C = C.LLVMConstSIToFP(v.C, t.C); return }
894 func ConstFPToUI(v Value, t Type) (rv Value) { rv.C = C.LLVMConstFPToUI(v.C, t.C); return }
895 func ConstFPToSI(v Value, t Type) (rv Value) { rv.C = C.LLVMConstFPToSI(v.C, t.C); return }
896 func ConstPtrToInt(v Value, t Type) (rv Value) { rv.C = C.LLVMConstPtrToInt(v.C, t.C); return }
897 func ConstIntToPtr(v Value, t Type) (rv Value) { rv.C = C.LLVMConstIntToPtr(v.C, t.C); return }
898 func ConstBitCast(v Value, t Type) (rv Value) { rv.C = C.LLVMConstBitCast(v.C, t.C); return }
899 func ConstZExtOrBitCast(v Value, t Type) (rv Value) { rv.C = C.LLVMConstZExtOrBitCast(v.C, t.C); return }
900 func ConstSExtOrBitCast(v Value, t Type) (rv Value) { rv.C = C.LLVMConstSExtOrBitCast(v.C, t.C); return }
901 func ConstTruncOrBitCast(v Value, t Type) (rv Value) {
902 rv.C = C.LLVMConstTruncOrBitCast(v.C, t.C)
905 func ConstPointerCast(v Value, t Type) (rv Value) { rv.C = C.LLVMConstPointerCast(v.C, t.C); return }
906 func ConstIntCast(v Value, t Type, signed bool) (rv Value) {
907 rv.C = C.LLVMConstIntCast(v.C, t.C, boolToLLVMBool(signed))
910 func ConstFPCast(v Value, t Type) (rv Value) { rv.C = C.LLVMConstFPCast(v.C, t.C); return }
911 func ConstSelect(cond, iftrue, iffalse Value) (rv Value) {
912 rv.C = C.LLVMConstSelect(cond.C, iftrue.C, iffalse.C)
915 func ConstExtractElement(vec, i Value) (rv Value) {
916 rv.C = C.LLVMConstExtractElement(vec.C, i.C)
919 func ConstInsertElement(vec, elem, i Value) (rv Value) {
920 rv.C = C.LLVMConstInsertElement(vec.C, elem.C, i.C)
923 func ConstShuffleVector(veca, vecb, mask Value) (rv Value) {
924 rv.C = C.LLVMConstShuffleVector(veca.C, vecb.C, mask.C)
929 //LLVMValueRef LLVMConstExtractValue(LLVMValueRef AggConstant, unsigned *IdxList,
932 func ConstExtractValue(agg Value, indices []uint32) (rv Value) {
935 panic("one or more indices are required")
937 ptr := (*C.unsigned)(&indices[0])
938 rv.C = C.LLVMConstExtractValue(agg.C, ptr, C.unsigned(n))
942 func ConstInsertValue(agg, val Value, indices []uint32) (rv Value) {
945 panic("one or more indices are required")
947 ptr := (*C.unsigned)(&indices[0])
948 rv.C = C.LLVMConstInsertValue(agg.C, val.C, ptr, C.unsigned(n))
952 func BlockAddress(f Value, bb BasicBlock) (v Value) {
953 v.C = C.LLVMBlockAddress(f.C, bb.C)
957 // Operations on global variables, functions, and aliases (globals)
958 func (v Value) GlobalParent() (m Module) { m.C = C.LLVMGetGlobalParent(v.C); return }
959 func (v Value) IsDeclaration() bool { return C.LLVMIsDeclaration(v.C) != 0 }
960 func (v Value) Linkage() Linkage { return Linkage(C.LLVMGetLinkage(v.C)) }
961 func (v Value) SetLinkage(l Linkage) { C.LLVMSetLinkage(v.C, C.LLVMLinkage(l)) }
962 func (v Value) Section() string { return C.GoString(C.LLVMGetSection(v.C)) }
963 func (v Value) SetSection(str string) {
964 cstr := C.CString(str)
965 defer C.free(unsafe.Pointer(cstr))
966 C.LLVMSetSection(v.C, cstr)
968 func (v Value) Visibility() Visibility { return Visibility(C.LLVMGetVisibility(v.C)) }
969 func (v Value) SetVisibility(vi Visibility) { C.LLVMSetVisibility(v.C, C.LLVMVisibility(vi)) }
970 func (v Value) Alignment() int { return int(C.LLVMGetAlignment(v.C)) }
971 func (v Value) SetAlignment(a int) { C.LLVMSetAlignment(v.C, C.unsigned(a)) }
972 func (v Value) SetUnnamedAddr(ua bool) { C.LLVMSetUnnamedAddr(v.C, boolToLLVMBool(ua)) }
974 // Operations on global variables
975 func AddGlobal(m Module, t Type, name string) (v Value) {
976 cname := C.CString(name)
977 defer C.free(unsafe.Pointer(cname))
978 v.C = C.LLVMAddGlobal(m.C, t.C, cname)
981 func AddGlobalInAddressSpace(m Module, t Type, name string, addressSpace int) (v Value) {
982 cname := C.CString(name)
983 defer C.free(unsafe.Pointer(cname))
984 v.C = C.LLVMAddGlobalInAddressSpace(m.C, t.C, cname, C.unsigned(addressSpace))
987 func (m Module) NamedGlobal(name string) (v Value) {
988 cname := C.CString(name)
989 defer C.free(unsafe.Pointer(cname))
990 v.C = C.LLVMGetNamedGlobal(m.C, cname)
994 func (m Module) FirstGlobal() (v Value) { v.C = C.LLVMGetFirstGlobal(m.C); return }
995 func (m Module) LastGlobal() (v Value) { v.C = C.LLVMGetLastGlobal(m.C); return }
996 func NextGlobal(v Value) (rv Value) { rv.C = C.LLVMGetNextGlobal(v.C); return }
997 func PrevGlobal(v Value) (rv Value) { rv.C = C.LLVMGetPreviousGlobal(v.C); return }
998 func (v Value) EraseFromParentAsGlobal() { C.LLVMDeleteGlobal(v.C) }
999 func (v Value) Initializer() (rv Value) { rv.C = C.LLVMGetInitializer(v.C); return }
1000 func (v Value) SetInitializer(cv Value) { C.LLVMSetInitializer(v.C, cv.C) }
1001 func (v Value) IsThreadLocal() bool { return C.LLVMIsThreadLocal(v.C) != 0 }
1002 func (v Value) SetThreadLocal(tl bool) { C.LLVMSetThreadLocal(v.C, boolToLLVMBool(tl)) }
1003 func (v Value) IsGlobalConstant() bool { return C.LLVMIsGlobalConstant(v.C) != 0 }
1004 func (v Value) SetGlobalConstant(gc bool) { C.LLVMSetGlobalConstant(v.C, boolToLLVMBool(gc)) }
1006 // Operations on aliases
1007 func AddAlias(m Module, t Type, aliasee Value, name string) (v Value) {
1008 cname := C.CString(name)
1009 defer C.free(unsafe.Pointer(cname))
1010 v.C = C.LLVMAddAlias(m.C, t.C, aliasee.C, cname)
1014 // Operations on functions
1015 func AddFunction(m Module, name string, ft Type) (v Value) {
1016 cname := C.CString(name)
1017 defer C.free(unsafe.Pointer(cname))
1018 v.C = C.LLVMAddFunction(m.C, cname, ft.C)
1022 func (m Module) NamedFunction(name string) (v Value) {
1023 cname := C.CString(name)
1024 defer C.free(unsafe.Pointer(cname))
1025 v.C = C.LLVMGetNamedFunction(m.C, cname)
1029 func (m Module) FirstFunction() (v Value) { v.C = C.LLVMGetFirstFunction(m.C); return }
1030 func (m Module) LastFunction() (v Value) { v.C = C.LLVMGetLastFunction(m.C); return }
1031 func NextFunction(v Value) (rv Value) { rv.C = C.LLVMGetNextFunction(v.C); return }
1032 func PrevFunction(v Value) (rv Value) { rv.C = C.LLVMGetPreviousFunction(v.C); return }
1033 func (v Value) EraseFromParentAsFunction() { C.LLVMDeleteFunction(v.C) }
1034 func (v Value) IntrinsicID() int { return int(C.LLVMGetIntrinsicID(v.C)) }
1035 func (v Value) FunctionCallConv() CallConv {
1036 return CallConv(C.LLVMCallConv(C.LLVMGetFunctionCallConv(v.C)))
1038 func (v Value) SetFunctionCallConv(cc CallConv) { C.LLVMSetFunctionCallConv(v.C, C.unsigned(cc)) }
1039 func (v Value) GC() string { return C.GoString(C.LLVMGetGC(v.C)) }
1040 func (v Value) SetGC(name string) {
1041 cname := C.CString(name)
1042 defer C.free(unsafe.Pointer(cname))
1043 C.LLVMSetGC(v.C, cname)
1045 func (v Value) AddFunctionAttr(a Attribute) { C.LLVMAddFunctionAttr2(v.C, C.uint64_t(a)) }
1046 func (v Value) FunctionAttr() Attribute { return Attribute(C.LLVMGetFunctionAttr2(v.C)) }
1047 func (v Value) RemoveFunctionAttr(a Attribute) { C.LLVMRemoveFunctionAttr2(v.C, C.uint64_t(a)) }
1048 func (v Value) AddTargetDependentFunctionAttr(attr, value string) {
1049 cattr := C.CString(attr)
1050 defer C.free(unsafe.Pointer(cattr))
1051 cvalue := C.CString(value)
1052 defer C.free(unsafe.Pointer(cvalue))
1053 C.LLVMAddTargetDependentFunctionAttr(v.C, cattr, cvalue)
1056 // Operations on parameters
1057 func (v Value) ParamsCount() int { return int(C.LLVMCountParams(v.C)) }
1058 func (v Value) Params() []Value {
1059 out := make([]Value, v.ParamsCount())
1061 C.LLVMGetParams(v.C, llvmValueRefPtr(&out[0]))
1065 func (v Value) Param(i int) (rv Value) { rv.C = C.LLVMGetParam(v.C, C.unsigned(i)); return }
1066 func (v Value) ParamParent() (rv Value) { rv.C = C.LLVMGetParamParent(v.C); return }
1067 func (v Value) FirstParam() (rv Value) { rv.C = C.LLVMGetFirstParam(v.C); return }
1068 func (v Value) LastParam() (rv Value) { rv.C = C.LLVMGetLastParam(v.C); return }
1069 func NextParam(v Value) (rv Value) { rv.C = C.LLVMGetNextParam(v.C); return }
1070 func PrevParam(v Value) (rv Value) { rv.C = C.LLVMGetPreviousParam(v.C); return }
1071 func (v Value) AddAttribute(a Attribute) {
1073 panic("attribute value currently unsupported")
1075 C.LLVMAddAttribute(v.C, C.LLVMAttribute(a))
1077 func (v Value) RemoveAttribute(a Attribute) {
1079 panic("attribute value currently unsupported")
1081 C.LLVMRemoveAttribute(v.C, C.LLVMAttribute(a))
1083 func (v Value) Attribute() Attribute { return Attribute(C.LLVMGetAttribute(v.C)) }
1084 func (v Value) SetParamAlignment(align int) { C.LLVMSetParamAlignment(v.C, C.unsigned(align)) }
1086 // Operations on basic blocks
1087 func (bb BasicBlock) AsValue() (v Value) { v.C = C.LLVMBasicBlockAsValue(bb.C); return }
1088 func (v Value) IsBasicBlock() bool { return C.LLVMValueIsBasicBlock(v.C) != 0 }
1089 func (v Value) AsBasicBlock() (bb BasicBlock) { bb.C = C.LLVMValueAsBasicBlock(v.C); return }
1090 func (bb BasicBlock) Parent() (v Value) { v.C = C.LLVMGetBasicBlockParent(bb.C); return }
1091 func (v Value) BasicBlocksCount() int { return int(C.LLVMCountBasicBlocks(v.C)) }
1092 func (v Value) BasicBlocks() []BasicBlock {
1093 out := make([]BasicBlock, v.BasicBlocksCount())
1094 C.LLVMGetBasicBlocks(v.C, llvmBasicBlockRefPtr(&out[0]))
1097 func (v Value) FirstBasicBlock() (bb BasicBlock) { bb.C = C.LLVMGetFirstBasicBlock(v.C); return }
1098 func (v Value) LastBasicBlock() (bb BasicBlock) { bb.C = C.LLVMGetLastBasicBlock(v.C); return }
1099 func NextBasicBlock(bb BasicBlock) (rbb BasicBlock) { rbb.C = C.LLVMGetNextBasicBlock(bb.C); return }
1100 func PrevBasicBlock(bb BasicBlock) (rbb BasicBlock) { rbb.C = C.LLVMGetPreviousBasicBlock(bb.C); return }
1101 func (v Value) EntryBasicBlock() (bb BasicBlock) { bb.C = C.LLVMGetEntryBasicBlock(v.C); return }
1102 func (c Context) AddBasicBlock(f Value, name string) (bb BasicBlock) {
1103 cname := C.CString(name)
1104 defer C.free(unsafe.Pointer(cname))
1105 bb.C = C.LLVMAppendBasicBlockInContext(c.C, f.C, cname)
1108 func (c Context) InsertBasicBlock(ref BasicBlock, name string) (bb BasicBlock) {
1109 cname := C.CString(name)
1110 defer C.free(unsafe.Pointer(cname))
1111 bb.C = C.LLVMInsertBasicBlockInContext(c.C, ref.C, cname)
1114 func AddBasicBlock(f Value, name string) (bb BasicBlock) {
1115 cname := C.CString(name)
1116 defer C.free(unsafe.Pointer(cname))
1117 bb.C = C.LLVMAppendBasicBlock(f.C, cname)
1120 func InsertBasicBlock(ref BasicBlock, name string) (bb BasicBlock) {
1121 cname := C.CString(name)
1122 defer C.free(unsafe.Pointer(cname))
1123 bb.C = C.LLVMInsertBasicBlock(ref.C, cname)
1126 func (bb BasicBlock) EraseFromParent() { C.LLVMDeleteBasicBlock(bb.C) }
1127 func (bb BasicBlock) MoveBefore(pos BasicBlock) { C.LLVMMoveBasicBlockBefore(bb.C, pos.C) }
1128 func (bb BasicBlock) MoveAfter(pos BasicBlock) { C.LLVMMoveBasicBlockAfter(bb.C, pos.C) }
1130 // Operations on instructions
1131 func (v Value) InstructionParent() (bb BasicBlock) { bb.C = C.LLVMGetInstructionParent(v.C); return }
1132 func (bb BasicBlock) FirstInstruction() (v Value) { v.C = C.LLVMGetFirstInstruction(bb.C); return }
1133 func (bb BasicBlock) LastInstruction() (v Value) { v.C = C.LLVMGetLastInstruction(bb.C); return }
1134 func NextInstruction(v Value) (rv Value) { rv.C = C.LLVMGetNextInstruction(v.C); return }
1135 func PrevInstruction(v Value) (rv Value) { rv.C = C.LLVMGetPreviousInstruction(v.C); return }
1137 // Operations on call sites
1138 func (v Value) SetInstructionCallConv(cc CallConv) {
1139 C.LLVMSetInstructionCallConv(v.C, C.unsigned(cc))
1141 func (v Value) InstructionCallConv() CallConv {
1142 return CallConv(C.LLVMCallConv(C.LLVMGetInstructionCallConv(v.C)))
1144 func (v Value) AddInstrAttribute(i int, a Attribute) {
1146 panic("attribute value currently unsupported")
1148 C.LLVMAddInstrAttribute(v.C, C.unsigned(i), C.LLVMAttribute(a))
1150 func (v Value) RemoveInstrAttribute(i int, a Attribute) {
1152 panic("attribute value currently unsupported")
1154 C.LLVMRemoveInstrAttribute(v.C, C.unsigned(i), C.LLVMAttribute(a))
1156 func (v Value) SetInstrParamAlignment(i int, align int) {
1157 C.LLVMSetInstrParamAlignment(v.C, C.unsigned(i), C.unsigned(align))
1160 // Operations on call instructions (only)
1161 func (v Value) IsTailCall() bool { return C.LLVMIsTailCall(v.C) != 0 }
1162 func (v Value) SetTailCall(is bool) { C.LLVMSetTailCall(v.C, boolToLLVMBool(is)) }
1164 // Operations on phi nodes
1165 func (v Value) AddIncoming(vals []Value, blocks []BasicBlock) {
1166 ptr, nvals := llvmValueRefs(vals)
1167 C.LLVMAddIncoming(v.C, ptr, llvmBasicBlockRefPtr(&blocks[0]), nvals)
1169 func (v Value) IncomingCount() int { return int(C.LLVMCountIncoming(v.C)) }
1170 func (v Value) IncomingValue(i int) (rv Value) {
1171 rv.C = C.LLVMGetIncomingValue(v.C, C.unsigned(i))
1174 func (v Value) IncomingBlock(i int) (bb BasicBlock) {
1175 bb.C = C.LLVMGetIncomingBlock(v.C, C.unsigned(i))
1179 //-------------------------------------------------------------------------
1181 //-------------------------------------------------------------------------
1183 // An instruction builder represents a point within a basic block, and is the
1184 // exclusive means of building instructions using the C interface.
1186 func (c Context) NewBuilder() (b Builder) { b.C = C.LLVMCreateBuilderInContext(c.C); return }
1187 func NewBuilder() (b Builder) { b.C = C.LLVMCreateBuilder(); return }
1188 func (b Builder) SetInsertPoint(block BasicBlock, instr Value) {
1189 C.LLVMPositionBuilder(b.C, block.C, instr.C)
1191 func (b Builder) SetInsertPointBefore(instr Value) { C.LLVMPositionBuilderBefore(b.C, instr.C) }
1192 func (b Builder) SetInsertPointAtEnd(block BasicBlock) { C.LLVMPositionBuilderAtEnd(b.C, block.C) }
1193 func (b Builder) GetInsertBlock() (bb BasicBlock) { bb.C = C.LLVMGetInsertBlock(b.C); return }
1194 func (b Builder) ClearInsertionPoint() { C.LLVMClearInsertionPosition(b.C) }
1195 func (b Builder) Insert(instr Value) { C.LLVMInsertIntoBuilder(b.C, instr.C) }
1196 func (b Builder) InsertWithName(instr Value, name string) {
1197 cname := C.CString(name)
1198 defer C.free(unsafe.Pointer(cname))
1199 C.LLVMInsertIntoBuilderWithName(b.C, instr.C, cname)
1201 func (b Builder) Dispose() { C.LLVMDisposeBuilder(b.C) }
1204 func (b Builder) SetCurrentDebugLocation(line, col uint, scope, inlinedAt Metadata) {
1205 C.LLVMSetCurrentDebugLocation2(b.C, C.unsigned(line), C.unsigned(col), scope.C, inlinedAt.C)
1207 func (b Builder) SetInstDebugLocation(v Value) { C.LLVMSetInstDebugLocation(b.C, v.C) }
1208 func (b Builder) InsertDeclare(module Module, storage Value, md Value) Value {
1209 f := module.NamedFunction("llvm.dbg.declare")
1211 ftyp := FunctionType(VoidType(), []Type{storage.Type(), md.Type()}, false)
1212 f = AddFunction(module, "llvm.dbg.declare", ftyp)
1214 return b.CreateCall(f, []Value{storage, md}, "")
1218 func (b Builder) CreateRetVoid() (rv Value) { rv.C = C.LLVMBuildRetVoid(b.C); return }
1219 func (b Builder) CreateRet(v Value) (rv Value) { rv.C = C.LLVMBuildRet(b.C, v.C); return }
1220 func (b Builder) CreateAggregateRet(vs []Value) (rv Value) {
1221 ptr, nvals := llvmValueRefs(vs)
1222 rv.C = C.LLVMBuildAggregateRet(b.C, ptr, nvals)
1225 func (b Builder) CreateBr(bb BasicBlock) (rv Value) { rv.C = C.LLVMBuildBr(b.C, bb.C); return }
1226 func (b Builder) CreateCondBr(ifv Value, thenb, elseb BasicBlock) (rv Value) {
1227 rv.C = C.LLVMBuildCondBr(b.C, ifv.C, thenb.C, elseb.C)
1230 func (b Builder) CreateSwitch(v Value, elseb BasicBlock, numCases int) (rv Value) {
1231 rv.C = C.LLVMBuildSwitch(b.C, v.C, elseb.C, C.unsigned(numCases))
1234 func (b Builder) CreateIndirectBr(addr Value, numDests int) (rv Value) {
1235 rv.C = C.LLVMBuildIndirectBr(b.C, addr.C, C.unsigned(numDests))
1238 func (b Builder) CreateInvoke(fn Value, args []Value, then, catch BasicBlock, name string) (rv Value) {
1239 cname := C.CString(name)
1240 defer C.free(unsafe.Pointer(cname))
1241 ptr, nvals := llvmValueRefs(args)
1242 rv.C = C.LLVMBuildInvoke(b.C, fn.C, ptr, nvals, then.C, catch.C, cname)
1245 func (b Builder) CreateUnreachable() (rv Value) { rv.C = C.LLVMBuildUnreachable(b.C); return }
1247 // Add a case to the switch instruction
1248 func (v Value) AddCase(on Value, dest BasicBlock) { C.LLVMAddCase(v.C, on.C, dest.C) }
1250 // Add a destination to the indirectbr instruction
1251 func (v Value) AddDest(dest BasicBlock) { C.LLVMAddDestination(v.C, dest.C) }
1254 func (b Builder) CreateAdd(lhs, rhs Value, name string) (v Value) {
1255 cname := C.CString(name)
1256 defer C.free(unsafe.Pointer(cname))
1257 v.C = C.LLVMBuildAdd(b.C, lhs.C, rhs.C, cname)
1260 func (b Builder) CreateNSWAdd(lhs, rhs Value, name string) (v Value) {
1261 cname := C.CString(name)
1262 defer C.free(unsafe.Pointer(cname))
1263 v.C = C.LLVMBuildNSWAdd(b.C, lhs.C, rhs.C, cname)
1266 func (b Builder) CreateNUWAdd(lhs, rhs Value, name string) (v Value) {
1267 cname := C.CString(name)
1268 defer C.free(unsafe.Pointer(cname))
1269 v.C = C.LLVMBuildNUWAdd(b.C, lhs.C, rhs.C, cname)
1272 func (b Builder) CreateFAdd(lhs, rhs Value, name string) (v Value) {
1273 cname := C.CString(name)
1274 defer C.free(unsafe.Pointer(cname))
1275 v.C = C.LLVMBuildFAdd(b.C, lhs.C, rhs.C, cname)
1278 func (b Builder) CreateSub(lhs, rhs Value, name string) (v Value) {
1279 cname := C.CString(name)
1280 defer C.free(unsafe.Pointer(cname))
1281 v.C = C.LLVMBuildSub(b.C, lhs.C, rhs.C, cname)
1284 func (b Builder) CreateNSWSub(lhs, rhs Value, name string) (v Value) {
1285 cname := C.CString(name)
1286 defer C.free(unsafe.Pointer(cname))
1287 v.C = C.LLVMBuildNSWSub(b.C, lhs.C, rhs.C, cname)
1290 func (b Builder) CreateNUWSub(lhs, rhs Value, name string) (v Value) {
1291 cname := C.CString(name)
1292 defer C.free(unsafe.Pointer(cname))
1293 v.C = C.LLVMBuildNUWSub(b.C, lhs.C, rhs.C, cname)
1296 func (b Builder) CreateFSub(lhs, rhs Value, name string) (v Value) {
1297 cname := C.CString(name)
1298 v.C = C.LLVMBuildFSub(b.C, lhs.C, rhs.C, cname)
1299 C.free(unsafe.Pointer(cname))
1302 func (b Builder) CreateMul(lhs, rhs Value, name string) (v Value) {
1303 cname := C.CString(name)
1304 defer C.free(unsafe.Pointer(cname))
1305 v.C = C.LLVMBuildMul(b.C, lhs.C, rhs.C, cname)
1308 func (b Builder) CreateNSWMul(lhs, rhs Value, name string) (v Value) {
1309 cname := C.CString(name)
1310 defer C.free(unsafe.Pointer(cname))
1311 v.C = C.LLVMBuildNSWMul(b.C, lhs.C, rhs.C, cname)
1314 func (b Builder) CreateNUWMul(lhs, rhs Value, name string) (v Value) {
1315 cname := C.CString(name)
1316 defer C.free(unsafe.Pointer(cname))
1317 v.C = C.LLVMBuildNUWMul(b.C, lhs.C, rhs.C, cname)
1320 func (b Builder) CreateFMul(lhs, rhs Value, name string) (v Value) {
1321 cname := C.CString(name)
1322 defer C.free(unsafe.Pointer(cname))
1323 v.C = C.LLVMBuildFMul(b.C, lhs.C, rhs.C, cname)
1326 func (b Builder) CreateUDiv(lhs, rhs Value, name string) (v Value) {
1327 cname := C.CString(name)
1328 defer C.free(unsafe.Pointer(cname))
1329 v.C = C.LLVMBuildUDiv(b.C, lhs.C, rhs.C, cname)
1332 func (b Builder) CreateSDiv(lhs, rhs Value, name string) (v Value) {
1333 cname := C.CString(name)
1334 defer C.free(unsafe.Pointer(cname))
1335 v.C = C.LLVMBuildSDiv(b.C, lhs.C, rhs.C, cname)
1338 func (b Builder) CreateExactSDiv(lhs, rhs Value, name string) (v Value) {
1339 cname := C.CString(name)
1340 defer C.free(unsafe.Pointer(cname))
1341 v.C = C.LLVMBuildExactSDiv(b.C, lhs.C, rhs.C, cname)
1344 func (b Builder) CreateFDiv(lhs, rhs Value, name string) (v Value) {
1345 cname := C.CString(name)
1346 defer C.free(unsafe.Pointer(cname))
1347 v.C = C.LLVMBuildFDiv(b.C, lhs.C, rhs.C, cname)
1350 func (b Builder) CreateURem(lhs, rhs Value, name string) (v Value) {
1351 cname := C.CString(name)
1352 defer C.free(unsafe.Pointer(cname))
1353 v.C = C.LLVMBuildURem(b.C, lhs.C, rhs.C, cname)
1356 func (b Builder) CreateSRem(lhs, rhs Value, name string) (v Value) {
1357 cname := C.CString(name)
1358 defer C.free(unsafe.Pointer(cname))
1359 v.C = C.LLVMBuildSRem(b.C, lhs.C, rhs.C, cname)
1362 func (b Builder) CreateFRem(lhs, rhs Value, name string) (v Value) {
1363 cname := C.CString(name)
1364 defer C.free(unsafe.Pointer(cname))
1365 v.C = C.LLVMBuildFRem(b.C, lhs.C, rhs.C, cname)
1368 func (b Builder) CreateShl(lhs, rhs Value, name string) (v Value) {
1369 cname := C.CString(name)
1370 defer C.free(unsafe.Pointer(cname))
1371 v.C = C.LLVMBuildShl(b.C, lhs.C, rhs.C, cname)
1374 func (b Builder) CreateLShr(lhs, rhs Value, name string) (v Value) {
1375 cname := C.CString(name)
1376 defer C.free(unsafe.Pointer(cname))
1377 v.C = C.LLVMBuildLShr(b.C, lhs.C, rhs.C, cname)
1380 func (b Builder) CreateAShr(lhs, rhs Value, name string) (v Value) {
1381 cname := C.CString(name)
1382 defer C.free(unsafe.Pointer(cname))
1383 v.C = C.LLVMBuildAShr(b.C, lhs.C, rhs.C, cname)
1386 func (b Builder) CreateAnd(lhs, rhs Value, name string) (v Value) {
1387 cname := C.CString(name)
1388 defer C.free(unsafe.Pointer(cname))
1389 v.C = C.LLVMBuildAnd(b.C, lhs.C, rhs.C, cname)
1392 func (b Builder) CreateOr(lhs, rhs Value, name string) (v Value) {
1393 cname := C.CString(name)
1394 defer C.free(unsafe.Pointer(cname))
1395 v.C = C.LLVMBuildOr(b.C, lhs.C, rhs.C, cname)
1398 func (b Builder) CreateXor(lhs, rhs Value, name string) (v Value) {
1399 cname := C.CString(name)
1400 defer C.free(unsafe.Pointer(cname))
1401 v.C = C.LLVMBuildXor(b.C, lhs.C, rhs.C, cname)
1404 func (b Builder) CreateBinOp(op Opcode, lhs, rhs Value, name string) (v Value) {
1405 cname := C.CString(name)
1406 defer C.free(unsafe.Pointer(cname))
1407 v.C = C.LLVMBuildBinOp(b.C, C.LLVMOpcode(op), lhs.C, rhs.C, cname)
1410 func (b Builder) CreateNeg(v Value, name string) (rv Value) {
1411 cname := C.CString(name)
1412 defer C.free(unsafe.Pointer(cname))
1413 rv.C = C.LLVMBuildNeg(b.C, v.C, cname)
1416 func (b Builder) CreateNSWNeg(v Value, name string) (rv Value) {
1417 cname := C.CString(name)
1418 defer C.free(unsafe.Pointer(cname))
1419 rv.C = C.LLVMBuildNSWNeg(b.C, v.C, cname)
1422 func (b Builder) CreateNUWNeg(v Value, name string) (rv Value) {
1423 cname := C.CString(name)
1424 defer C.free(unsafe.Pointer(cname))
1425 rv.C = C.LLVMBuildNUWNeg(b.C, v.C, cname)
1428 func (b Builder) CreateFNeg(v Value, name string) (rv Value) {
1429 cname := C.CString(name)
1430 defer C.free(unsafe.Pointer(cname))
1431 rv.C = C.LLVMBuildFNeg(b.C, v.C, cname)
1434 func (b Builder) CreateNot(v Value, name string) (rv Value) {
1435 cname := C.CString(name)
1436 defer C.free(unsafe.Pointer(cname))
1437 rv.C = C.LLVMBuildNot(b.C, v.C, cname)
1443 func (b Builder) CreateMalloc(t Type, name string) (v Value) {
1444 cname := C.CString(name)
1445 defer C.free(unsafe.Pointer(cname))
1446 v.C = C.LLVMBuildMalloc(b.C, t.C, cname)
1449 func (b Builder) CreateArrayMalloc(t Type, val Value, name string) (v Value) {
1450 cname := C.CString(name)
1451 defer C.free(unsafe.Pointer(cname))
1452 v.C = C.LLVMBuildArrayMalloc(b.C, t.C, val.C, cname)
1455 func (b Builder) CreateAlloca(t Type, name string) (v Value) {
1456 cname := C.CString(name)
1457 defer C.free(unsafe.Pointer(cname))
1458 v.C = C.LLVMBuildAlloca(b.C, t.C, cname)
1461 func (b Builder) CreateArrayAlloca(t Type, val Value, name string) (v Value) {
1462 cname := C.CString(name)
1463 defer C.free(unsafe.Pointer(cname))
1464 v.C = C.LLVMBuildArrayAlloca(b.C, t.C, val.C, cname)
1467 func (b Builder) CreateFree(p Value) (v Value) {
1468 v.C = C.LLVMBuildFree(b.C, p.C)
1471 func (b Builder) CreateLoad(p Value, name string) (v Value) {
1472 cname := C.CString(name)
1473 defer C.free(unsafe.Pointer(cname))
1474 v.C = C.LLVMBuildLoad(b.C, p.C, cname)
1477 func (b Builder) CreateStore(val Value, p Value) (v Value) {
1478 v.C = C.LLVMBuildStore(b.C, val.C, p.C)
1481 func (b Builder) CreateGEP(p Value, indices []Value, name string) (v Value) {
1482 cname := C.CString(name)
1483 defer C.free(unsafe.Pointer(cname))
1484 ptr, nvals := llvmValueRefs(indices)
1485 v.C = C.LLVMBuildGEP(b.C, p.C, ptr, nvals, cname)
1488 func (b Builder) CreateInBoundsGEP(p Value, indices []Value, name string) (v Value) {
1489 cname := C.CString(name)
1490 defer C.free(unsafe.Pointer(cname))
1491 ptr, nvals := llvmValueRefs(indices)
1492 v.C = C.LLVMBuildInBoundsGEP(b.C, p.C, ptr, nvals, cname)
1495 func (b Builder) CreateStructGEP(p Value, i int, name string) (v Value) {
1496 cname := C.CString(name)
1497 defer C.free(unsafe.Pointer(cname))
1498 v.C = C.LLVMBuildStructGEP(b.C, p.C, C.unsigned(i), cname)
1501 func (b Builder) CreateGlobalString(str, name string) (v Value) {
1502 cstr := C.CString(str)
1503 defer C.free(unsafe.Pointer(cstr))
1504 cname := C.CString(name)
1505 defer C.free(unsafe.Pointer(cname))
1506 v.C = C.LLVMBuildGlobalString(b.C, cstr, cname)
1509 func (b Builder) CreateGlobalStringPtr(str, name string) (v Value) {
1510 cstr := C.CString(str)
1511 defer C.free(unsafe.Pointer(cstr))
1512 cname := C.CString(name)
1513 defer C.free(unsafe.Pointer(cname))
1514 v.C = C.LLVMBuildGlobalStringPtr(b.C, cstr, cname)
1519 func (b Builder) CreateTrunc(val Value, t Type, name string) (v Value) {
1520 cname := C.CString(name)
1521 defer C.free(unsafe.Pointer(cname))
1522 v.C = C.LLVMBuildTrunc(b.C, val.C, t.C, cname)
1525 func (b Builder) CreateZExt(val Value, t Type, name string) (v Value) {
1526 cname := C.CString(name)
1527 defer C.free(unsafe.Pointer(cname))
1528 v.C = C.LLVMBuildZExt(b.C, val.C, t.C, cname)
1531 func (b Builder) CreateSExt(val Value, t Type, name string) (v Value) {
1532 cname := C.CString(name)
1533 defer C.free(unsafe.Pointer(cname))
1534 v.C = C.LLVMBuildSExt(b.C, val.C, t.C, cname)
1537 func (b Builder) CreateFPToUI(val Value, t Type, name string) (v Value) {
1538 cname := C.CString(name)
1539 defer C.free(unsafe.Pointer(cname))
1540 v.C = C.LLVMBuildFPToUI(b.C, val.C, t.C, cname)
1543 func (b Builder) CreateFPToSI(val Value, t Type, name string) (v Value) {
1544 cname := C.CString(name)
1545 defer C.free(unsafe.Pointer(cname))
1546 v.C = C.LLVMBuildFPToSI(b.C, val.C, t.C, cname)
1549 func (b Builder) CreateUIToFP(val Value, t Type, name string) (v Value) {
1550 cname := C.CString(name)
1551 defer C.free(unsafe.Pointer(cname))
1552 v.C = C.LLVMBuildUIToFP(b.C, val.C, t.C, cname)
1555 func (b Builder) CreateSIToFP(val Value, t Type, name string) (v Value) {
1556 cname := C.CString(name)
1557 defer C.free(unsafe.Pointer(cname))
1558 v.C = C.LLVMBuildSIToFP(b.C, val.C, t.C, cname)
1561 func (b Builder) CreateFPTrunc(val Value, t Type, name string) (v Value) {
1562 cname := C.CString(name)
1563 defer C.free(unsafe.Pointer(cname))
1564 v.C = C.LLVMBuildFPTrunc(b.C, val.C, t.C, cname)
1567 func (b Builder) CreateFPExt(val Value, t Type, name string) (v Value) {
1568 cname := C.CString(name)
1569 defer C.free(unsafe.Pointer(cname))
1570 v.C = C.LLVMBuildFPExt(b.C, val.C, t.C, cname)
1573 func (b Builder) CreatePtrToInt(val Value, t Type, name string) (v Value) {
1574 cname := C.CString(name)
1575 defer C.free(unsafe.Pointer(cname))
1576 v.C = C.LLVMBuildPtrToInt(b.C, val.C, t.C, cname)
1579 func (b Builder) CreateIntToPtr(val Value, t Type, name string) (v Value) {
1580 cname := C.CString(name)
1581 defer C.free(unsafe.Pointer(cname))
1582 v.C = C.LLVMBuildIntToPtr(b.C, val.C, t.C, cname)
1585 func (b Builder) CreateBitCast(val Value, t Type, name string) (v Value) {
1586 cname := C.CString(name)
1587 defer C.free(unsafe.Pointer(cname))
1588 v.C = C.LLVMBuildBitCast(b.C, val.C, t.C, cname)
1591 func (b Builder) CreateZExtOrBitCast(val Value, t Type, name string) (v Value) {
1592 cname := C.CString(name)
1593 defer C.free(unsafe.Pointer(cname))
1594 v.C = C.LLVMBuildZExtOrBitCast(b.C, val.C, t.C, cname)
1597 func (b Builder) CreateSExtOrBitCast(val Value, t Type, name string) (v Value) {
1598 cname := C.CString(name)
1599 defer C.free(unsafe.Pointer(cname))
1600 v.C = C.LLVMBuildSExtOrBitCast(b.C, val.C, t.C, cname)
1603 func (b Builder) CreateTruncOrBitCast(val Value, t Type, name string) (v Value) {
1604 cname := C.CString(name)
1605 defer C.free(unsafe.Pointer(cname))
1606 v.C = C.LLVMBuildTruncOrBitCast(b.C, val.C, t.C, cname)
1609 func (b Builder) CreateCast(val Value, op Opcode, t Type, name string) (v Value) {
1610 cname := C.CString(name)
1611 defer C.free(unsafe.Pointer(cname))
1612 v.C = C.LLVMBuildCast(b.C, C.LLVMOpcode(op), val.C, t.C, cname)
1615 func (b Builder) CreatePointerCast(val Value, t Type, name string) (v Value) {
1616 cname := C.CString(name)
1617 defer C.free(unsafe.Pointer(cname))
1618 v.C = C.LLVMBuildPointerCast(b.C, val.C, t.C, cname)
1621 func (b Builder) CreateIntCast(val Value, t Type, name string) (v Value) {
1622 cname := C.CString(name)
1623 defer C.free(unsafe.Pointer(cname))
1624 v.C = C.LLVMBuildIntCast(b.C, val.C, t.C, cname)
1627 func (b Builder) CreateFPCast(val Value, t Type, name string) (v Value) {
1628 cname := C.CString(name)
1629 defer C.free(unsafe.Pointer(cname))
1630 v.C = C.LLVMBuildFPCast(b.C, val.C, t.C, cname)
1635 func (b Builder) CreateICmp(pred IntPredicate, lhs, rhs Value, name string) (v Value) {
1636 cname := C.CString(name)
1637 defer C.free(unsafe.Pointer(cname))
1638 v.C = C.LLVMBuildICmp(b.C, C.LLVMIntPredicate(pred), lhs.C, rhs.C, cname)
1641 func (b Builder) CreateFCmp(pred FloatPredicate, lhs, rhs Value, name string) (v Value) {
1642 cname := C.CString(name)
1643 defer C.free(unsafe.Pointer(cname))
1644 v.C = C.LLVMBuildFCmp(b.C, C.LLVMRealPredicate(pred), lhs.C, rhs.C, cname)
1648 // Miscellaneous instructions
1649 func (b Builder) CreatePHI(t Type, name string) (v Value) {
1650 cname := C.CString(name)
1651 defer C.free(unsafe.Pointer(cname))
1652 v.C = C.LLVMBuildPhi(b.C, t.C, cname)
1655 func (b Builder) CreateCall(fn Value, args []Value, name string) (v Value) {
1656 cname := C.CString(name)
1657 defer C.free(unsafe.Pointer(cname))
1658 ptr, nvals := llvmValueRefs(args)
1659 v.C = C.LLVMBuildCall(b.C, fn.C, ptr, nvals, cname)
1663 func (b Builder) CreateSelect(ifv, thenv, elsev Value, name string) (v Value) {
1664 cname := C.CString(name)
1665 defer C.free(unsafe.Pointer(cname))
1666 v.C = C.LLVMBuildSelect(b.C, ifv.C, thenv.C, elsev.C, cname)
1670 func (b Builder) CreateVAArg(list Value, t Type, name string) (v Value) {
1671 cname := C.CString(name)
1672 defer C.free(unsafe.Pointer(cname))
1673 v.C = C.LLVMBuildVAArg(b.C, list.C, t.C, cname)
1676 func (b Builder) CreateExtractElement(vec, i Value, name string) (v Value) {
1677 cname := C.CString(name)
1678 defer C.free(unsafe.Pointer(cname))
1679 v.C = C.LLVMBuildExtractElement(b.C, vec.C, i.C, cname)
1682 func (b Builder) CreateInsertElement(vec, elt, i Value, name string) (v Value) {
1683 cname := C.CString(name)
1684 defer C.free(unsafe.Pointer(cname))
1685 v.C = C.LLVMBuildInsertElement(b.C, vec.C, elt.C, i.C, cname)
1688 func (b Builder) CreateShuffleVector(v1, v2, mask Value, name string) (v Value) {
1689 cname := C.CString(name)
1690 defer C.free(unsafe.Pointer(cname))
1691 v.C = C.LLVMBuildShuffleVector(b.C, v1.C, v2.C, mask.C, cname)
1694 func (b Builder) CreateExtractValue(agg Value, i int, name string) (v Value) {
1695 cname := C.CString(name)
1696 defer C.free(unsafe.Pointer(cname))
1697 v.C = C.LLVMBuildExtractValue(b.C, agg.C, C.unsigned(i), cname)
1700 func (b Builder) CreateInsertValue(agg, elt Value, i int, name string) (v Value) {
1701 cname := C.CString(name)
1702 defer C.free(unsafe.Pointer(cname))
1703 v.C = C.LLVMBuildInsertValue(b.C, agg.C, elt.C, C.unsigned(i), cname)
1707 func (b Builder) CreateIsNull(val Value, name string) (v Value) {
1708 cname := C.CString(name)
1709 defer C.free(unsafe.Pointer(cname))
1710 v.C = C.LLVMBuildIsNull(b.C, val.C, cname)
1713 func (b Builder) CreateIsNotNull(val Value, name string) (v Value) {
1714 cname := C.CString(name)
1715 defer C.free(unsafe.Pointer(cname))
1716 v.C = C.LLVMBuildIsNotNull(b.C, val.C, cname)
1719 func (b Builder) CreatePtrDiff(lhs, rhs Value, name string) (v Value) {
1720 cname := C.CString(name)
1721 defer C.free(unsafe.Pointer(cname))
1722 v.C = C.LLVMBuildPtrDiff(b.C, lhs.C, rhs.C, cname)
1726 func (b Builder) CreateLandingPad(t Type, personality Value, nclauses int, name string) (l Value) {
1727 cname := C.CString(name)
1728 defer C.free(unsafe.Pointer(cname))
1729 l.C = C.LLVMBuildLandingPad(b.C, t.C, personality.C, C.unsigned(nclauses), cname)
1733 func (l Value) AddClause(v Value) {
1734 C.LLVMAddClause(l.C, v.C)
1737 func (l Value) SetCleanup(cleanup bool) {
1738 C.LLVMSetCleanup(l.C, boolToLLVMBool(cleanup))
1741 func (b Builder) CreateResume(ex Value) (v Value) {
1742 v.C = C.LLVMBuildResume(b.C, ex.C)
1746 //-------------------------------------------------------------------------
1747 // llvm.ModuleProvider
1748 //-------------------------------------------------------------------------
1750 // Changes the type of M so it can be passed to FunctionPassManagers and the
1751 // JIT. They take ModuleProviders for historical reasons.
1752 func NewModuleProviderForModule(m Module) (mp ModuleProvider) {
1753 mp.C = C.LLVMCreateModuleProviderForExistingModule(m.C)
1757 // Destroys the module M.
1758 func (mp ModuleProvider) Dispose() { C.LLVMDisposeModuleProvider(mp.C) }
1760 //-------------------------------------------------------------------------
1761 // llvm.MemoryBuffer
1762 //-------------------------------------------------------------------------
1764 func NewMemoryBufferFromFile(path string) (b MemoryBuffer, err error) {
1766 cpath := C.CString(path)
1767 defer C.free(unsafe.Pointer(cpath))
1768 fail := C.LLVMCreateMemoryBufferWithContentsOfFile(cpath, &b.C, &cmsg)
1771 err = errors.New(C.GoString(cmsg))
1772 C.LLVMDisposeMessage(cmsg)
1777 func NewMemoryBufferFromStdin() (b MemoryBuffer, err error) {
1779 fail := C.LLVMCreateMemoryBufferWithSTDIN(&b.C, &cmsg)
1782 err = errors.New(C.GoString(cmsg))
1783 C.LLVMDisposeMessage(cmsg)
1788 func (b MemoryBuffer) Bytes() []byte {
1789 cstart := C.LLVMGetBufferStart(b.C)
1790 csize := C.LLVMGetBufferSize(b.C)
1791 return C.GoBytes(unsafe.Pointer(cstart), C.int(csize))
1794 func (b MemoryBuffer) Dispose() { C.LLVMDisposeMemoryBuffer(b.C) }
1796 //-------------------------------------------------------------------------
1798 //-------------------------------------------------------------------------
1800 // Constructs a new whole-module pass pipeline. This type of pipeline is
1801 // suitable for link-time optimization and whole-module transformations.
1802 // See llvm::PassManager::PassManager.
1803 func NewPassManager() (pm PassManager) { pm.C = C.LLVMCreatePassManager(); return }
1805 // Constructs a new function-by-function pass pipeline over the module
1806 // provider. It does not take ownership of the module provider. This type of
1807 // pipeline is suitable for code generation and JIT compilation tasks.
1808 // See llvm::FunctionPassManager::FunctionPassManager.
1809 func NewFunctionPassManagerForModule(m Module) (pm PassManager) {
1810 pm.C = C.LLVMCreateFunctionPassManagerForModule(m.C)
1814 // Initializes, executes on the provided module, and finalizes all of the
1815 // passes scheduled in the pass manager. Returns 1 if any of the passes
1816 // modified the module, 0 otherwise. See llvm::PassManager::run(Module&).
1817 func (pm PassManager) Run(m Module) bool { return C.LLVMRunPassManager(pm.C, m.C) != 0 }
1819 // Initializes all of the function passes scheduled in the function pass
1820 // manager. Returns 1 if any of the passes modified the module, 0 otherwise.
1821 // See llvm::FunctionPassManager::doInitialization.
1822 func (pm PassManager) InitializeFunc() bool { return C.LLVMInitializeFunctionPassManager(pm.C) != 0 }
1824 // Executes all of the function passes scheduled in the function pass manager
1825 // on the provided function. Returns 1 if any of the passes modified the
1826 // function, false otherwise.
1827 // See llvm::FunctionPassManager::run(Function&).
1828 func (pm PassManager) RunFunc(f Value) bool { return C.LLVMRunFunctionPassManager(pm.C, f.C) != 0 }
1830 // Finalizes all of the function passes scheduled in in the function pass
1831 // manager. Returns 1 if any of the passes modified the module, 0 otherwise.
1832 // See llvm::FunctionPassManager::doFinalization.
1833 func (pm PassManager) FinalizeFunc() bool { return C.LLVMFinalizeFunctionPassManager(pm.C) != 0 }
1835 // Frees the memory of a pass pipeline. For function pipelines, does not free
1836 // the module provider.
1837 // See llvm::PassManagerBase::~PassManagerBase.
1838 func (pm PassManager) Dispose() { C.LLVMDisposePassManager(pm.C) }
1840 //-------------------------------------------------------------------------
1842 //-------------------------------------------------------------------------
1844 func (md Metadata) ReplaceAllUsesWith(new Metadata) {
1845 C.LLVMMetadataReplaceAllUsesWith(md.C, new.C)