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
163 ConvergentAttribute Attribute = 1 << 46
164 SafeStackAttribute Attribute = 1 << 47
167 //-------------------------------------------------------------------------
169 //-------------------------------------------------------------------------
172 Ret Opcode = C.LLVMRet
174 Switch Opcode = C.LLVMSwitch
175 IndirectBr Opcode = C.LLVMIndirectBr
176 Invoke Opcode = C.LLVMInvoke
177 Unreachable Opcode = C.LLVMUnreachable
179 // Standard Binary Operators
180 Add Opcode = C.LLVMAdd
181 FAdd Opcode = C.LLVMFAdd
182 Sub Opcode = C.LLVMSub
183 FSub Opcode = C.LLVMFSub
184 Mul Opcode = C.LLVMMul
185 FMul Opcode = C.LLVMFMul
186 UDiv Opcode = C.LLVMUDiv
187 SDiv Opcode = C.LLVMSDiv
188 FDiv Opcode = C.LLVMFDiv
189 URem Opcode = C.LLVMURem
190 SRem Opcode = C.LLVMSRem
191 FRem Opcode = C.LLVMFRem
194 Shl Opcode = C.LLVMShl
195 LShr Opcode = C.LLVMLShr
196 AShr Opcode = C.LLVMAShr
197 And Opcode = C.LLVMAnd
199 Xor Opcode = C.LLVMXor
202 Alloca Opcode = C.LLVMAlloca
203 Load Opcode = C.LLVMLoad
204 Store Opcode = C.LLVMStore
205 GetElementPtr Opcode = C.LLVMGetElementPtr
208 Trunc Opcode = C.LLVMTrunc
209 ZExt Opcode = C.LLVMZExt
210 SExt Opcode = C.LLVMSExt
211 FPToUI Opcode = C.LLVMFPToUI
212 FPToSI Opcode = C.LLVMFPToSI
213 UIToFP Opcode = C.LLVMUIToFP
214 SIToFP Opcode = C.LLVMSIToFP
215 FPTrunc Opcode = C.LLVMFPTrunc
216 FPExt Opcode = C.LLVMFPExt
217 PtrToInt Opcode = C.LLVMPtrToInt
218 IntToPtr Opcode = C.LLVMIntToPtr
219 BitCast Opcode = C.LLVMBitCast
222 ICmp Opcode = C.LLVMICmp
223 FCmp Opcode = C.LLVMFCmp
224 PHI Opcode = C.LLVMPHI
225 Call Opcode = C.LLVMCall
226 Select Opcode = C.LLVMSelect
229 VAArg Opcode = C.LLVMVAArg
230 ExtractElement Opcode = C.LLVMExtractElement
231 InsertElement Opcode = C.LLVMInsertElement
232 ShuffleVector Opcode = C.LLVMShuffleVector
233 ExtractValue Opcode = C.LLVMExtractValue
234 InsertValue Opcode = C.LLVMInsertValue
237 //-------------------------------------------------------------------------
239 //-------------------------------------------------------------------------
242 VoidTypeKind TypeKind = C.LLVMVoidTypeKind
243 FloatTypeKind TypeKind = C.LLVMFloatTypeKind
244 DoubleTypeKind TypeKind = C.LLVMDoubleTypeKind
245 X86_FP80TypeKind TypeKind = C.LLVMX86_FP80TypeKind
246 FP128TypeKind TypeKind = C.LLVMFP128TypeKind
247 PPC_FP128TypeKind TypeKind = C.LLVMPPC_FP128TypeKind
248 LabelTypeKind TypeKind = C.LLVMLabelTypeKind
249 IntegerTypeKind TypeKind = C.LLVMIntegerTypeKind
250 FunctionTypeKind TypeKind = C.LLVMFunctionTypeKind
251 StructTypeKind TypeKind = C.LLVMStructTypeKind
252 ArrayTypeKind TypeKind = C.LLVMArrayTypeKind
253 PointerTypeKind TypeKind = C.LLVMPointerTypeKind
254 VectorTypeKind TypeKind = C.LLVMVectorTypeKind
255 MetadataTypeKind TypeKind = C.LLVMMetadataTypeKind
258 //-------------------------------------------------------------------------
260 //-------------------------------------------------------------------------
263 ExternalLinkage Linkage = C.LLVMExternalLinkage
264 AvailableExternallyLinkage Linkage = C.LLVMAvailableExternallyLinkage
265 LinkOnceAnyLinkage Linkage = C.LLVMLinkOnceAnyLinkage
266 LinkOnceODRLinkage Linkage = C.LLVMLinkOnceODRLinkage
267 WeakAnyLinkage Linkage = C.LLVMWeakAnyLinkage
268 WeakODRLinkage Linkage = C.LLVMWeakODRLinkage
269 AppendingLinkage Linkage = C.LLVMAppendingLinkage
270 InternalLinkage Linkage = C.LLVMInternalLinkage
271 PrivateLinkage Linkage = C.LLVMPrivateLinkage
272 ExternalWeakLinkage Linkage = C.LLVMExternalWeakLinkage
273 CommonLinkage Linkage = C.LLVMCommonLinkage
276 //-------------------------------------------------------------------------
278 //-------------------------------------------------------------------------
281 DefaultVisibility Visibility = C.LLVMDefaultVisibility
282 HiddenVisibility Visibility = C.LLVMHiddenVisibility
283 ProtectedVisibility Visibility = C.LLVMProtectedVisibility
286 //-------------------------------------------------------------------------
288 //-------------------------------------------------------------------------
291 CCallConv CallConv = C.LLVMCCallConv
292 FastCallConv CallConv = C.LLVMFastCallConv
293 ColdCallConv CallConv = C.LLVMColdCallConv
294 X86StdcallCallConv CallConv = C.LLVMX86StdcallCallConv
295 X86FastcallCallConv CallConv = C.LLVMX86FastcallCallConv
298 //-------------------------------------------------------------------------
300 //-------------------------------------------------------------------------
303 IntEQ IntPredicate = C.LLVMIntEQ
304 IntNE IntPredicate = C.LLVMIntNE
305 IntUGT IntPredicate = C.LLVMIntUGT
306 IntUGE IntPredicate = C.LLVMIntUGE
307 IntULT IntPredicate = C.LLVMIntULT
308 IntULE IntPredicate = C.LLVMIntULE
309 IntSGT IntPredicate = C.LLVMIntSGT
310 IntSGE IntPredicate = C.LLVMIntSGE
311 IntSLT IntPredicate = C.LLVMIntSLT
312 IntSLE IntPredicate = C.LLVMIntSLE
315 //-------------------------------------------------------------------------
316 // llvm.FloatPredicate
317 //-------------------------------------------------------------------------
320 FloatPredicateFalse FloatPredicate = C.LLVMRealPredicateFalse
321 FloatOEQ FloatPredicate = C.LLVMRealOEQ
322 FloatOGT FloatPredicate = C.LLVMRealOGT
323 FloatOGE FloatPredicate = C.LLVMRealOGE
324 FloatOLT FloatPredicate = C.LLVMRealOLT
325 FloatOLE FloatPredicate = C.LLVMRealOLE
326 FloatONE FloatPredicate = C.LLVMRealONE
327 FloatORD FloatPredicate = C.LLVMRealORD
328 FloatUNO FloatPredicate = C.LLVMRealUNO
329 FloatUEQ FloatPredicate = C.LLVMRealUEQ
330 FloatUGT FloatPredicate = C.LLVMRealUGT
331 FloatUGE FloatPredicate = C.LLVMRealUGE
332 FloatULT FloatPredicate = C.LLVMRealULT
333 FloatULE FloatPredicate = C.LLVMRealULE
334 FloatUNE FloatPredicate = C.LLVMRealUNE
335 FloatPredicateTrue FloatPredicate = C.LLVMRealPredicateTrue
338 //-------------------------------------------------------------------------
339 // llvm.LandingPadClause
340 //-------------------------------------------------------------------------
343 LandingPadCatch LandingPadClause = C.LLVMLandingPadCatch
344 LandingPadFilter LandingPadClause = C.LLVMLandingPadFilter
347 //-------------------------------------------------------------------------
349 //-------------------------------------------------------------------------
351 func NewContext() Context { return Context{C.LLVMContextCreate()} }
352 func GlobalContext() Context { return Context{C.LLVMGetGlobalContext()} }
353 func (c Context) Dispose() { C.LLVMContextDispose(c.C) }
355 func (c Context) MDKindID(name string) (id int) {
356 cname := C.CString(name)
357 defer C.free(unsafe.Pointer(cname))
358 id = int(C.LLVMGetMDKindIDInContext(c.C, cname, C.unsigned(len(name))))
362 func MDKindID(name string) (id int) {
363 cname := C.CString(name)
364 defer C.free(unsafe.Pointer(cname))
365 id = int(C.LLVMGetMDKindID(cname, C.unsigned(len(name))))
369 //-------------------------------------------------------------------------
371 //-------------------------------------------------------------------------
373 // Create and destroy modules.
374 // See llvm::Module::Module.
375 func NewModule(name string) (m Module) {
376 cname := C.CString(name)
377 defer C.free(unsafe.Pointer(cname))
378 m.C = C.LLVMModuleCreateWithName(cname)
382 func (c Context) NewModule(name string) (m Module) {
383 cname := C.CString(name)
384 defer C.free(unsafe.Pointer(cname))
385 m.C = C.LLVMModuleCreateWithNameInContext(cname, c.C)
389 // See llvm::Module::~Module
390 func (m Module) Dispose() { C.LLVMDisposeModule(m.C) }
392 // Data layout. See Module::getDataLayout.
393 func (m Module) DataLayout() string {
394 clayout := C.LLVMGetDataLayout(m.C)
395 return C.GoString(clayout)
398 func (m Module) SetDataLayout(layout string) {
399 clayout := C.CString(layout)
400 defer C.free(unsafe.Pointer(clayout))
401 C.LLVMSetDataLayout(m.C, clayout)
404 // Target triple. See Module::getTargetTriple.
405 func (m Module) Target() string {
406 ctarget := C.LLVMGetTarget(m.C)
407 return C.GoString(ctarget)
409 func (m Module) SetTarget(target string) {
410 ctarget := C.CString(target)
411 defer C.free(unsafe.Pointer(ctarget))
412 C.LLVMSetTarget(m.C, ctarget)
415 func (m Module) GetTypeByName(name string) (t Type) {
416 cname := C.CString(name)
417 defer C.free(unsafe.Pointer(cname))
418 t.C = C.LLVMGetTypeByName(m.C, cname)
423 func (m Module) Dump() {
424 C.LLVMDumpModule(m.C)
427 func (m Module) String() string {
428 cir := C.LLVMPrintModuleToString(m.C)
429 defer C.free(unsafe.Pointer(cir))
430 ir := C.GoString(cir)
434 // See Module::setModuleInlineAsm.
435 func (m Module) SetInlineAsm(asm string) {
436 casm := C.CString(asm)
437 defer C.free(unsafe.Pointer(casm))
438 C.LLVMSetModuleInlineAsm(m.C, casm)
441 func (m Module) AddNamedMetadataOperand(name string, operand Metadata) {
442 cname := C.CString(name)
443 defer C.free(unsafe.Pointer(cname))
444 C.LLVMAddNamedMetadataOperand2(m.C, cname, operand.C)
447 func (m Module) Context() (c Context) {
448 c.C = C.LLVMGetModuleContext(m.C)
452 //-------------------------------------------------------------------------
454 //-------------------------------------------------------------------------
456 // LLVM types conform to the following hierarchy:
470 // See llvm::LLVMTypeKind::getTypeID.
471 func (t Type) TypeKind() TypeKind { return TypeKind(C.LLVMGetTypeKind(t.C)) }
473 // See llvm::LLVMType::getContext.
474 func (t Type) Context() (c Context) {
475 c.C = C.LLVMGetTypeContext(t.C)
479 // Operations on integer types
480 func (c Context) Int1Type() (t Type) { t.C = C.LLVMInt1TypeInContext(c.C); return }
481 func (c Context) Int8Type() (t Type) { t.C = C.LLVMInt8TypeInContext(c.C); return }
482 func (c Context) Int16Type() (t Type) { t.C = C.LLVMInt16TypeInContext(c.C); return }
483 func (c Context) Int32Type() (t Type) { t.C = C.LLVMInt32TypeInContext(c.C); return }
484 func (c Context) Int64Type() (t Type) { t.C = C.LLVMInt64TypeInContext(c.C); return }
485 func (c Context) IntType(numbits int) (t Type) {
486 t.C = C.LLVMIntTypeInContext(c.C, C.unsigned(numbits))
490 func Int1Type() (t Type) { t.C = C.LLVMInt1Type(); return }
491 func Int8Type() (t Type) { t.C = C.LLVMInt8Type(); return }
492 func Int16Type() (t Type) { t.C = C.LLVMInt16Type(); return }
493 func Int32Type() (t Type) { t.C = C.LLVMInt32Type(); return }
494 func Int64Type() (t Type) { t.C = C.LLVMInt64Type(); return }
496 func IntType(numbits int) (t Type) {
497 t.C = C.LLVMIntType(C.unsigned(numbits))
501 func (t Type) IntTypeWidth() int {
502 return int(C.LLVMGetIntTypeWidth(t.C))
505 // Operations on real types
506 func (c Context) FloatType() (t Type) { t.C = C.LLVMFloatTypeInContext(c.C); return }
507 func (c Context) DoubleType() (t Type) { t.C = C.LLVMDoubleTypeInContext(c.C); return }
508 func (c Context) X86FP80Type() (t Type) { t.C = C.LLVMX86FP80TypeInContext(c.C); return }
509 func (c Context) FP128Type() (t Type) { t.C = C.LLVMFP128TypeInContext(c.C); return }
510 func (c Context) PPCFP128Type() (t Type) { t.C = C.LLVMPPCFP128TypeInContext(c.C); return }
512 func FloatType() (t Type) { t.C = C.LLVMFloatType(); return }
513 func DoubleType() (t Type) { t.C = C.LLVMDoubleType(); return }
514 func X86FP80Type() (t Type) { t.C = C.LLVMX86FP80Type(); return }
515 func FP128Type() (t Type) { t.C = C.LLVMFP128Type(); return }
516 func PPCFP128Type() (t Type) { t.C = C.LLVMPPCFP128Type(); return }
518 // Operations on function types
519 func FunctionType(returnType Type, paramTypes []Type, isVarArg bool) (t Type) {
520 var pt *C.LLVMTypeRef
522 if len(paramTypes) > 0 {
523 pt = llvmTypeRefPtr(¶mTypes[0])
524 ptlen = C.unsigned(len(paramTypes))
526 t.C = C.LLVMFunctionType(returnType.C,
529 boolToLLVMBool(isVarArg))
533 func (t Type) IsFunctionVarArg() bool { return C.LLVMIsFunctionVarArg(t.C) != 0 }
534 func (t Type) ReturnType() (rt Type) { rt.C = C.LLVMGetReturnType(t.C); return }
535 func (t Type) ParamTypesCount() int { return int(C.LLVMCountParamTypes(t.C)) }
536 func (t Type) ParamTypes() []Type {
537 count := t.ParamTypesCount()
539 out := make([]Type, count)
540 C.LLVMGetParamTypes(t.C, llvmTypeRefPtr(&out[0]))
546 // Operations on struct types
547 func (c Context) StructType(elementTypes []Type, packed bool) (t Type) {
548 var pt *C.LLVMTypeRef
550 if len(elementTypes) > 0 {
551 pt = llvmTypeRefPtr(&elementTypes[0])
552 ptlen = C.unsigned(len(elementTypes))
554 t.C = C.LLVMStructTypeInContext(c.C,
557 boolToLLVMBool(packed))
561 func StructType(elementTypes []Type, packed bool) (t Type) {
562 var pt *C.LLVMTypeRef
564 if len(elementTypes) > 0 {
565 pt = llvmTypeRefPtr(&elementTypes[0])
566 ptlen = C.unsigned(len(elementTypes))
568 t.C = C.LLVMStructType(pt, ptlen, boolToLLVMBool(packed))
572 func (c Context) StructCreateNamed(name string) (t Type) {
573 cname := C.CString(name)
574 defer C.free(unsafe.Pointer(cname))
575 t.C = C.LLVMStructCreateNamed(c.C, cname)
579 func (t Type) StructName() string {
580 return C.GoString(C.LLVMGetStructName(t.C))
583 func (t Type) StructSetBody(elementTypes []Type, packed bool) {
584 var pt *C.LLVMTypeRef
586 if len(elementTypes) > 0 {
587 pt = llvmTypeRefPtr(&elementTypes[0])
588 ptlen = C.unsigned(len(elementTypes))
590 C.LLVMStructSetBody(t.C, pt, ptlen, boolToLLVMBool(packed))
593 func (t Type) IsStructPacked() bool { return C.LLVMIsPackedStruct(t.C) != 0 }
594 func (t Type) StructElementTypesCount() int { return int(C.LLVMCountStructElementTypes(t.C)) }
595 func (t Type) StructElementTypes() []Type {
596 out := make([]Type, t.StructElementTypesCount())
598 C.LLVMGetStructElementTypes(t.C, llvmTypeRefPtr(&out[0]))
603 // Operations on array, pointer, and vector types (sequence types)
604 func ArrayType(elementType Type, elementCount int) (t Type) {
605 t.C = C.LLVMArrayType(elementType.C, C.unsigned(elementCount))
608 func PointerType(elementType Type, addressSpace int) (t Type) {
609 t.C = C.LLVMPointerType(elementType.C, C.unsigned(addressSpace))
612 func VectorType(elementType Type, elementCount int) (t Type) {
613 t.C = C.LLVMVectorType(elementType.C, C.unsigned(elementCount))
617 func (t Type) ElementType() (rt Type) { rt.C = C.LLVMGetElementType(t.C); return }
618 func (t Type) ArrayLength() int { return int(C.LLVMGetArrayLength(t.C)) }
619 func (t Type) PointerAddressSpace() int { return int(C.LLVMGetPointerAddressSpace(t.C)) }
620 func (t Type) VectorSize() int { return int(C.LLVMGetVectorSize(t.C)) }
622 // Operations on other types
623 func (c Context) VoidType() (t Type) { t.C = C.LLVMVoidTypeInContext(c.C); return }
624 func (c Context) LabelType() (t Type) { t.C = C.LLVMLabelTypeInContext(c.C); return }
626 func VoidType() (t Type) { t.C = C.LLVMVoidType(); return }
627 func LabelType() (t Type) { t.C = C.LLVMLabelType(); return }
629 //-------------------------------------------------------------------------
631 //-------------------------------------------------------------------------
633 // Operations on all values
634 func (v Value) Type() (t Type) { t.C = C.LLVMTypeOf(v.C); return }
635 func (v Value) Name() string { return C.GoString(C.LLVMGetValueName(v.C)) }
636 func (v Value) SetName(name string) {
637 cname := C.CString(name)
638 defer C.free(unsafe.Pointer(cname))
639 C.LLVMSetValueName(v.C, cname)
641 func (v Value) Dump() { C.LLVMDumpValue(v.C) }
642 func (v Value) ReplaceAllUsesWith(nv Value) { C.LLVMReplaceAllUsesWith(v.C, nv.C) }
643 func (v Value) HasMetadata() bool { return C.LLVMHasMetadata(v.C) != 0 }
644 func (v Value) Metadata(kind int) (rv Value) {
645 rv.C = C.LLVMGetMetadata(v.C, C.unsigned(kind))
648 func (v Value) SetMetadata(kind int, node Metadata) {
649 C.LLVMSetMetadata2(v.C, C.unsigned(kind), node.C)
652 // Conversion functions.
653 // Return the input value if it is an instance of the specified class, otherwise NULL.
654 // See llvm::dyn_cast_or_null<>.
655 func (v Value) IsAArgument() (rv Value) { rv.C = C.LLVMIsAArgument(v.C); return }
656 func (v Value) IsABasicBlock() (rv Value) { rv.C = C.LLVMIsABasicBlock(v.C); return }
657 func (v Value) IsAInlineAsm() (rv Value) { rv.C = C.LLVMIsAInlineAsm(v.C); return }
658 func (v Value) IsAUser() (rv Value) { rv.C = C.LLVMIsAUser(v.C); return }
659 func (v Value) IsAConstant() (rv Value) { rv.C = C.LLVMIsAConstant(v.C); return }
660 func (v Value) IsAConstantAggregateZero() (rv Value) {
661 rv.C = C.LLVMIsAConstantAggregateZero(v.C)
664 func (v Value) IsAConstantArray() (rv Value) { rv.C = C.LLVMIsAConstantArray(v.C); return }
665 func (v Value) IsAConstantExpr() (rv Value) { rv.C = C.LLVMIsAConstantExpr(v.C); return }
666 func (v Value) IsAConstantFP() (rv Value) { rv.C = C.LLVMIsAConstantFP(v.C); return }
667 func (v Value) IsAConstantInt() (rv Value) { rv.C = C.LLVMIsAConstantInt(v.C); return }
668 func (v Value) IsAConstantPointerNull() (rv Value) { rv.C = C.LLVMIsAConstantPointerNull(v.C); return }
669 func (v Value) IsAConstantStruct() (rv Value) { rv.C = C.LLVMIsAConstantStruct(v.C); return }
670 func (v Value) IsAConstantVector() (rv Value) { rv.C = C.LLVMIsAConstantVector(v.C); return }
671 func (v Value) IsAGlobalValue() (rv Value) { rv.C = C.LLVMIsAGlobalValue(v.C); return }
672 func (v Value) IsAFunction() (rv Value) { rv.C = C.LLVMIsAFunction(v.C); return }
673 func (v Value) IsAGlobalAlias() (rv Value) { rv.C = C.LLVMIsAGlobalAlias(v.C); return }
674 func (v Value) IsAGlobalVariable() (rv Value) { rv.C = C.LLVMIsAGlobalVariable(v.C); return }
675 func (v Value) IsAUndefValue() (rv Value) { rv.C = C.LLVMIsAUndefValue(v.C); return }
676 func (v Value) IsAInstruction() (rv Value) { rv.C = C.LLVMIsAInstruction(v.C); return }
677 func (v Value) IsABinaryOperator() (rv Value) { rv.C = C.LLVMIsABinaryOperator(v.C); return }
678 func (v Value) IsACallInst() (rv Value) { rv.C = C.LLVMIsACallInst(v.C); return }
679 func (v Value) IsAIntrinsicInst() (rv Value) { rv.C = C.LLVMIsAIntrinsicInst(v.C); return }
680 func (v Value) IsADbgInfoIntrinsic() (rv Value) { rv.C = C.LLVMIsADbgInfoIntrinsic(v.C); return }
681 func (v Value) IsADbgDeclareInst() (rv Value) { rv.C = C.LLVMIsADbgDeclareInst(v.C); return }
682 func (v Value) IsAMemIntrinsic() (rv Value) { rv.C = C.LLVMIsAMemIntrinsic(v.C); return }
683 func (v Value) IsAMemCpyInst() (rv Value) { rv.C = C.LLVMIsAMemCpyInst(v.C); return }
684 func (v Value) IsAMemMoveInst() (rv Value) { rv.C = C.LLVMIsAMemMoveInst(v.C); return }
685 func (v Value) IsAMemSetInst() (rv Value) { rv.C = C.LLVMIsAMemSetInst(v.C); return }
686 func (v Value) IsACmpInst() (rv Value) { rv.C = C.LLVMIsACmpInst(v.C); return }
687 func (v Value) IsAFCmpInst() (rv Value) { rv.C = C.LLVMIsAFCmpInst(v.C); return }
688 func (v Value) IsAICmpInst() (rv Value) { rv.C = C.LLVMIsAICmpInst(v.C); return }
689 func (v Value) IsAExtractElementInst() (rv Value) { rv.C = C.LLVMIsAExtractElementInst(v.C); return }
690 func (v Value) IsAGetElementPtrInst() (rv Value) { rv.C = C.LLVMIsAGetElementPtrInst(v.C); return }
691 func (v Value) IsAInsertElementInst() (rv Value) { rv.C = C.LLVMIsAInsertElementInst(v.C); return }
692 func (v Value) IsAInsertValueInst() (rv Value) { rv.C = C.LLVMIsAInsertValueInst(v.C); return }
693 func (v Value) IsAPHINode() (rv Value) { rv.C = C.LLVMIsAPHINode(v.C); return }
694 func (v Value) IsASelectInst() (rv Value) { rv.C = C.LLVMIsASelectInst(v.C); return }
695 func (v Value) IsAShuffleVectorInst() (rv Value) { rv.C = C.LLVMIsAShuffleVectorInst(v.C); return }
696 func (v Value) IsAStoreInst() (rv Value) { rv.C = C.LLVMIsAStoreInst(v.C); return }
697 func (v Value) IsATerminatorInst() (rv Value) { rv.C = C.LLVMIsATerminatorInst(v.C); return }
698 func (v Value) IsABranchInst() (rv Value) { rv.C = C.LLVMIsABranchInst(v.C); return }
699 func (v Value) IsAInvokeInst() (rv Value) { rv.C = C.LLVMIsAInvokeInst(v.C); return }
700 func (v Value) IsAReturnInst() (rv Value) { rv.C = C.LLVMIsAReturnInst(v.C); return }
701 func (v Value) IsASwitchInst() (rv Value) { rv.C = C.LLVMIsASwitchInst(v.C); return }
702 func (v Value) IsAUnreachableInst() (rv Value) { rv.C = C.LLVMIsAUnreachableInst(v.C); return }
703 func (v Value) IsAUnaryInstruction() (rv Value) { rv.C = C.LLVMIsAUnaryInstruction(v.C); return }
704 func (v Value) IsAAllocaInst() (rv Value) { rv.C = C.LLVMIsAAllocaInst(v.C); return }
705 func (v Value) IsACastInst() (rv Value) { rv.C = C.LLVMIsACastInst(v.C); return }
706 func (v Value) IsABitCastInst() (rv Value) { rv.C = C.LLVMIsABitCastInst(v.C); return }
707 func (v Value) IsAFPExtInst() (rv Value) { rv.C = C.LLVMIsAFPExtInst(v.C); return }
708 func (v Value) IsAFPToSIInst() (rv Value) { rv.C = C.LLVMIsAFPToSIInst(v.C); return }
709 func (v Value) IsAFPToUIInst() (rv Value) { rv.C = C.LLVMIsAFPToUIInst(v.C); return }
710 func (v Value) IsAFPTruncInst() (rv Value) { rv.C = C.LLVMIsAFPTruncInst(v.C); return }
711 func (v Value) IsAIntToPtrInst() (rv Value) { rv.C = C.LLVMIsAIntToPtrInst(v.C); return }
712 func (v Value) IsAPtrToIntInst() (rv Value) { rv.C = C.LLVMIsAPtrToIntInst(v.C); return }
713 func (v Value) IsASExtInst() (rv Value) { rv.C = C.LLVMIsASExtInst(v.C); return }
714 func (v Value) IsASIToFPInst() (rv Value) { rv.C = C.LLVMIsASIToFPInst(v.C); return }
715 func (v Value) IsATruncInst() (rv Value) { rv.C = C.LLVMIsATruncInst(v.C); return }
716 func (v Value) IsAUIToFPInst() (rv Value) { rv.C = C.LLVMIsAUIToFPInst(v.C); return }
717 func (v Value) IsAZExtInst() (rv Value) { rv.C = C.LLVMIsAZExtInst(v.C); return }
718 func (v Value) IsAExtractValueInst() (rv Value) { rv.C = C.LLVMIsAExtractValueInst(v.C); return }
719 func (v Value) IsALoadInst() (rv Value) { rv.C = C.LLVMIsALoadInst(v.C); return }
720 func (v Value) IsAVAArgInst() (rv Value) { rv.C = C.LLVMIsAVAArgInst(v.C); return }
722 // Operations on Uses
723 func (v Value) FirstUse() (u Use) { u.C = C.LLVMGetFirstUse(v.C); return }
724 func (u Use) NextUse() (ru Use) { ru.C = C.LLVMGetNextUse(u.C); return }
725 func (u Use) User() (v Value) { v.C = C.LLVMGetUser(u.C); return }
726 func (u Use) UsedValue() (v Value) { v.C = C.LLVMGetUsedValue(u.C); return }
728 // Operations on Users
729 func (v Value) Operand(i int) (rv Value) { rv.C = C.LLVMGetOperand(v.C, C.unsigned(i)); return }
730 func (v Value) SetOperand(i int, op Value) { C.LLVMSetOperand(v.C, C.unsigned(i), op.C) }
731 func (v Value) OperandsCount() int { return int(C.LLVMGetNumOperands(v.C)) }
733 // Operations on constants of any type
734 func ConstNull(t Type) (v Value) { v.C = C.LLVMConstNull(t.C); return }
735 func ConstAllOnes(t Type) (v Value) { v.C = C.LLVMConstAllOnes(t.C); return }
736 func Undef(t Type) (v Value) { v.C = C.LLVMGetUndef(t.C); return }
737 func (v Value) IsConstant() bool { return C.LLVMIsConstant(v.C) != 0 }
738 func (v Value) IsNull() bool { return C.LLVMIsNull(v.C) != 0 }
739 func (v Value) IsUndef() bool { return C.LLVMIsUndef(v.C) != 0 }
740 func ConstPointerNull(t Type) (v Value) { v.C = C.LLVMConstPointerNull(t.C); return }
742 // Operations on metadata
743 func (c Context) MDString(str string) (md Metadata) {
744 cstr := C.CString(str)
745 defer C.free(unsafe.Pointer(cstr))
746 md.C = C.LLVMMDString2(c.C, cstr, C.unsigned(len(str)))
749 func (c Context) MDNode(mds []Metadata) (md Metadata) {
750 ptr, nvals := llvmMetadataRefs(mds)
751 md.C = C.LLVMMDNode2(c.C, ptr, nvals)
754 func (c Context) TemporaryMDNode(mds []Metadata) (md Metadata) {
755 ptr, nvals := llvmMetadataRefs(mds)
756 md.C = C.LLVMTemporaryMDNode(c.C, ptr, nvals)
759 func (v Value) ConstantAsMetadata() (md Metadata) {
760 md.C = C.LLVMConstantAsMetadata(v.C)
764 // Operations on scalar constants
765 func ConstInt(t Type, n uint64, signExtend bool) (v Value) {
766 v.C = C.LLVMConstInt(t.C,
768 boolToLLVMBool(signExtend))
771 func ConstIntFromString(t Type, str string, radix int) (v Value) {
772 cstr := C.CString(str)
773 defer C.free(unsafe.Pointer(cstr))
774 v.C = C.LLVMConstIntOfString(t.C, cstr, C.uint8_t(radix))
777 func ConstFloat(t Type, n float64) (v Value) {
778 v.C = C.LLVMConstReal(t.C, C.double(n))
781 func ConstFloatFromString(t Type, str string) (v Value) {
782 cstr := C.CString(str)
783 defer C.free(unsafe.Pointer(cstr))
784 v.C = C.LLVMConstRealOfString(t.C, cstr)
788 func (v Value) ZExtValue() uint64 { return uint64(C.LLVMConstIntGetZExtValue(v.C)) }
789 func (v Value) SExtValue() int64 { return int64(C.LLVMConstIntGetSExtValue(v.C)) }
791 // Operations on composite constants
792 func (c Context) ConstString(str string, addnull bool) (v Value) {
793 cstr := C.CString(str)
794 defer C.free(unsafe.Pointer(cstr))
795 v.C = C.LLVMConstStringInContext(c.C, cstr,
796 C.unsigned(len(str)), boolToLLVMBool(!addnull))
799 func (c Context) ConstStruct(constVals []Value, packed bool) (v Value) {
800 ptr, nvals := llvmValueRefs(constVals)
801 v.C = C.LLVMConstStructInContext(c.C, ptr, nvals,
802 boolToLLVMBool(packed))
805 func ConstNamedStruct(t Type, constVals []Value) (v Value) {
806 ptr, nvals := llvmValueRefs(constVals)
807 v.C = C.LLVMConstNamedStruct(t.C, ptr, nvals)
810 func ConstString(str string, addnull bool) (v Value) {
811 cstr := C.CString(str)
812 defer C.free(unsafe.Pointer(cstr))
813 v.C = C.LLVMConstString(cstr,
814 C.unsigned(len(str)), boolToLLVMBool(!addnull))
817 func ConstArray(t Type, constVals []Value) (v Value) {
818 ptr, nvals := llvmValueRefs(constVals)
819 v.C = C.LLVMConstArray(t.C, ptr, nvals)
822 func ConstStruct(constVals []Value, packed bool) (v Value) {
823 ptr, nvals := llvmValueRefs(constVals)
824 v.C = C.LLVMConstStruct(ptr, nvals, boolToLLVMBool(packed))
827 func ConstVector(scalarConstVals []Value, packed bool) (v Value) {
828 ptr, nvals := llvmValueRefs(scalarConstVals)
829 v.C = C.LLVMConstVector(ptr, nvals)
833 // Constant expressions
834 func (v Value) Opcode() Opcode { return Opcode(C.LLVMGetConstOpcode(v.C)) }
835 func (v Value) InstructionOpcode() Opcode { return Opcode(C.LLVMGetInstructionOpcode(v.C)) }
836 func AlignOf(t Type) (v Value) { v.C = C.LLVMAlignOf(t.C); return }
837 func SizeOf(t Type) (v Value) { v.C = C.LLVMSizeOf(t.C); return }
838 func ConstNeg(v Value) (rv Value) { rv.C = C.LLVMConstNeg(v.C); return }
839 func ConstNSWNeg(v Value) (rv Value) { rv.C = C.LLVMConstNSWNeg(v.C); return }
840 func ConstNUWNeg(v Value) (rv Value) { rv.C = C.LLVMConstNUWNeg(v.C); return }
841 func ConstFNeg(v Value) (rv Value) { rv.C = C.LLVMConstFNeg(v.C); return }
842 func ConstNot(v Value) (rv Value) { rv.C = C.LLVMConstNot(v.C); return }
843 func ConstAdd(lhs, rhs Value) (v Value) { v.C = C.LLVMConstAdd(lhs.C, rhs.C); return }
844 func ConstNSWAdd(lhs, rhs Value) (v Value) { v.C = C.LLVMConstNSWAdd(lhs.C, rhs.C); return }
845 func ConstNUWAdd(lhs, rhs Value) (v Value) { v.C = C.LLVMConstNUWAdd(lhs.C, rhs.C); return }
846 func ConstFAdd(lhs, rhs Value) (v Value) { v.C = C.LLVMConstFAdd(lhs.C, rhs.C); return }
847 func ConstSub(lhs, rhs Value) (v Value) { v.C = C.LLVMConstSub(lhs.C, rhs.C); return }
848 func ConstNSWSub(lhs, rhs Value) (v Value) { v.C = C.LLVMConstNSWSub(lhs.C, rhs.C); return }
849 func ConstNUWSub(lhs, rhs Value) (v Value) { v.C = C.LLVMConstNUWSub(lhs.C, rhs.C); return }
850 func ConstFSub(lhs, rhs Value) (v Value) { v.C = C.LLVMConstFSub(lhs.C, rhs.C); return }
851 func ConstMul(lhs, rhs Value) (v Value) { v.C = C.LLVMConstMul(lhs.C, rhs.C); return }
852 func ConstNSWMul(lhs, rhs Value) (v Value) { v.C = C.LLVMConstNSWMul(lhs.C, rhs.C); return }
853 func ConstNUWMul(lhs, rhs Value) (v Value) { v.C = C.LLVMConstNUWMul(lhs.C, rhs.C); return }
854 func ConstFMul(lhs, rhs Value) (v Value) { v.C = C.LLVMConstFMul(lhs.C, rhs.C); return }
855 func ConstUDiv(lhs, rhs Value) (v Value) { v.C = C.LLVMConstUDiv(lhs.C, rhs.C); return }
856 func ConstSDiv(lhs, rhs Value) (v Value) { v.C = C.LLVMConstSDiv(lhs.C, rhs.C); return }
857 func ConstExactSDiv(lhs, rhs Value) (v Value) { v.C = C.LLVMConstExactSDiv(lhs.C, rhs.C); return }
858 func ConstFDiv(lhs, rhs Value) (v Value) { v.C = C.LLVMConstFDiv(lhs.C, rhs.C); return }
859 func ConstURem(lhs, rhs Value) (v Value) { v.C = C.LLVMConstURem(lhs.C, rhs.C); return }
860 func ConstSRem(lhs, rhs Value) (v Value) { v.C = C.LLVMConstSRem(lhs.C, rhs.C); return }
861 func ConstFRem(lhs, rhs Value) (v Value) { v.C = C.LLVMConstFRem(lhs.C, rhs.C); return }
862 func ConstAnd(lhs, rhs Value) (v Value) { v.C = C.LLVMConstAnd(lhs.C, rhs.C); return }
863 func ConstOr(lhs, rhs Value) (v Value) { v.C = C.LLVMConstOr(lhs.C, rhs.C); return }
864 func ConstXor(lhs, rhs Value) (v Value) { v.C = C.LLVMConstXor(lhs.C, rhs.C); return }
866 func ConstICmp(pred IntPredicate, lhs, rhs Value) (v Value) {
867 v.C = C.LLVMConstICmp(C.LLVMIntPredicate(pred), lhs.C, rhs.C)
870 func ConstFCmp(pred FloatPredicate, lhs, rhs Value) (v Value) {
871 v.C = C.LLVMConstFCmp(C.LLVMRealPredicate(pred), lhs.C, rhs.C)
875 func ConstShl(lhs, rhs Value) (v Value) { v.C = C.LLVMConstShl(lhs.C, rhs.C); return }
876 func ConstLShr(lhs, rhs Value) (v Value) { v.C = C.LLVMConstLShr(lhs.C, rhs.C); return }
877 func ConstAShr(lhs, rhs Value) (v Value) { v.C = C.LLVMConstAShr(lhs.C, rhs.C); return }
879 func ConstGEP(v Value, indices []Value) (rv Value) {
880 ptr, nvals := llvmValueRefs(indices)
881 rv.C = C.LLVMConstGEP(v.C, ptr, nvals)
884 func ConstInBoundsGEP(v Value, indices []Value) (rv Value) {
885 ptr, nvals := llvmValueRefs(indices)
886 rv.C = C.LLVMConstInBoundsGEP(v.C, ptr, nvals)
889 func ConstTrunc(v Value, t Type) (rv Value) { rv.C = C.LLVMConstTrunc(v.C, t.C); return }
890 func ConstSExt(v Value, t Type) (rv Value) { rv.C = C.LLVMConstSExt(v.C, t.C); return }
891 func ConstZExt(v Value, t Type) (rv Value) { rv.C = C.LLVMConstZExt(v.C, t.C); return }
892 func ConstFPTrunc(v Value, t Type) (rv Value) { rv.C = C.LLVMConstFPTrunc(v.C, t.C); return }
893 func ConstFPExt(v Value, t Type) (rv Value) { rv.C = C.LLVMConstFPExt(v.C, t.C); return }
894 func ConstUIToFP(v Value, t Type) (rv Value) { rv.C = C.LLVMConstUIToFP(v.C, t.C); return }
895 func ConstSIToFP(v Value, t Type) (rv Value) { rv.C = C.LLVMConstSIToFP(v.C, t.C); return }
896 func ConstFPToUI(v Value, t Type) (rv Value) { rv.C = C.LLVMConstFPToUI(v.C, t.C); return }
897 func ConstFPToSI(v Value, t Type) (rv Value) { rv.C = C.LLVMConstFPToSI(v.C, t.C); return }
898 func ConstPtrToInt(v Value, t Type) (rv Value) { rv.C = C.LLVMConstPtrToInt(v.C, t.C); return }
899 func ConstIntToPtr(v Value, t Type) (rv Value) { rv.C = C.LLVMConstIntToPtr(v.C, t.C); return }
900 func ConstBitCast(v Value, t Type) (rv Value) { rv.C = C.LLVMConstBitCast(v.C, t.C); return }
901 func ConstZExtOrBitCast(v Value, t Type) (rv Value) { rv.C = C.LLVMConstZExtOrBitCast(v.C, t.C); return }
902 func ConstSExtOrBitCast(v Value, t Type) (rv Value) { rv.C = C.LLVMConstSExtOrBitCast(v.C, t.C); return }
903 func ConstTruncOrBitCast(v Value, t Type) (rv Value) {
904 rv.C = C.LLVMConstTruncOrBitCast(v.C, t.C)
907 func ConstPointerCast(v Value, t Type) (rv Value) { rv.C = C.LLVMConstPointerCast(v.C, t.C); return }
908 func ConstIntCast(v Value, t Type, signed bool) (rv Value) {
909 rv.C = C.LLVMConstIntCast(v.C, t.C, boolToLLVMBool(signed))
912 func ConstFPCast(v Value, t Type) (rv Value) { rv.C = C.LLVMConstFPCast(v.C, t.C); return }
913 func ConstSelect(cond, iftrue, iffalse Value) (rv Value) {
914 rv.C = C.LLVMConstSelect(cond.C, iftrue.C, iffalse.C)
917 func ConstExtractElement(vec, i Value) (rv Value) {
918 rv.C = C.LLVMConstExtractElement(vec.C, i.C)
921 func ConstInsertElement(vec, elem, i Value) (rv Value) {
922 rv.C = C.LLVMConstInsertElement(vec.C, elem.C, i.C)
925 func ConstShuffleVector(veca, vecb, mask Value) (rv Value) {
926 rv.C = C.LLVMConstShuffleVector(veca.C, vecb.C, mask.C)
931 //LLVMValueRef LLVMConstExtractValue(LLVMValueRef AggConstant, unsigned *IdxList,
934 func ConstExtractValue(agg Value, indices []uint32) (rv Value) {
937 panic("one or more indices are required")
939 ptr := (*C.unsigned)(&indices[0])
940 rv.C = C.LLVMConstExtractValue(agg.C, ptr, C.unsigned(n))
944 func ConstInsertValue(agg, val Value, indices []uint32) (rv Value) {
947 panic("one or more indices are required")
949 ptr := (*C.unsigned)(&indices[0])
950 rv.C = C.LLVMConstInsertValue(agg.C, val.C, ptr, C.unsigned(n))
954 func BlockAddress(f Value, bb BasicBlock) (v Value) {
955 v.C = C.LLVMBlockAddress(f.C, bb.C)
959 // Operations on global variables, functions, and aliases (globals)
960 func (v Value) GlobalParent() (m Module) { m.C = C.LLVMGetGlobalParent(v.C); return }
961 func (v Value) IsDeclaration() bool { return C.LLVMIsDeclaration(v.C) != 0 }
962 func (v Value) Linkage() Linkage { return Linkage(C.LLVMGetLinkage(v.C)) }
963 func (v Value) SetLinkage(l Linkage) { C.LLVMSetLinkage(v.C, C.LLVMLinkage(l)) }
964 func (v Value) Section() string { return C.GoString(C.LLVMGetSection(v.C)) }
965 func (v Value) SetSection(str string) {
966 cstr := C.CString(str)
967 defer C.free(unsafe.Pointer(cstr))
968 C.LLVMSetSection(v.C, cstr)
970 func (v Value) Visibility() Visibility { return Visibility(C.LLVMGetVisibility(v.C)) }
971 func (v Value) SetVisibility(vi Visibility) { C.LLVMSetVisibility(v.C, C.LLVMVisibility(vi)) }
972 func (v Value) Alignment() int { return int(C.LLVMGetAlignment(v.C)) }
973 func (v Value) SetAlignment(a int) { C.LLVMSetAlignment(v.C, C.unsigned(a)) }
974 func (v Value) SetUnnamedAddr(ua bool) { C.LLVMSetUnnamedAddr(v.C, boolToLLVMBool(ua)) }
976 // Operations on global variables
977 func AddGlobal(m Module, t Type, name string) (v Value) {
978 cname := C.CString(name)
979 defer C.free(unsafe.Pointer(cname))
980 v.C = C.LLVMAddGlobal(m.C, t.C, cname)
983 func AddGlobalInAddressSpace(m Module, t Type, name string, addressSpace int) (v Value) {
984 cname := C.CString(name)
985 defer C.free(unsafe.Pointer(cname))
986 v.C = C.LLVMAddGlobalInAddressSpace(m.C, t.C, cname, C.unsigned(addressSpace))
989 func (m Module) NamedGlobal(name string) (v Value) {
990 cname := C.CString(name)
991 defer C.free(unsafe.Pointer(cname))
992 v.C = C.LLVMGetNamedGlobal(m.C, cname)
996 func (m Module) FirstGlobal() (v Value) { v.C = C.LLVMGetFirstGlobal(m.C); return }
997 func (m Module) LastGlobal() (v Value) { v.C = C.LLVMGetLastGlobal(m.C); return }
998 func NextGlobal(v Value) (rv Value) { rv.C = C.LLVMGetNextGlobal(v.C); return }
999 func PrevGlobal(v Value) (rv Value) { rv.C = C.LLVMGetPreviousGlobal(v.C); return }
1000 func (v Value) EraseFromParentAsGlobal() { C.LLVMDeleteGlobal(v.C) }
1001 func (v Value) Initializer() (rv Value) { rv.C = C.LLVMGetInitializer(v.C); return }
1002 func (v Value) SetInitializer(cv Value) { C.LLVMSetInitializer(v.C, cv.C) }
1003 func (v Value) IsThreadLocal() bool { return C.LLVMIsThreadLocal(v.C) != 0 }
1004 func (v Value) SetThreadLocal(tl bool) { C.LLVMSetThreadLocal(v.C, boolToLLVMBool(tl)) }
1005 func (v Value) IsGlobalConstant() bool { return C.LLVMIsGlobalConstant(v.C) != 0 }
1006 func (v Value) SetGlobalConstant(gc bool) { C.LLVMSetGlobalConstant(v.C, boolToLLVMBool(gc)) }
1008 // Operations on aliases
1009 func AddAlias(m Module, t Type, aliasee Value, name string) (v Value) {
1010 cname := C.CString(name)
1011 defer C.free(unsafe.Pointer(cname))
1012 v.C = C.LLVMAddAlias(m.C, t.C, aliasee.C, cname)
1016 // Operations on functions
1017 func AddFunction(m Module, name string, ft Type) (v Value) {
1018 cname := C.CString(name)
1019 defer C.free(unsafe.Pointer(cname))
1020 v.C = C.LLVMAddFunction(m.C, cname, ft.C)
1024 func (m Module) NamedFunction(name string) (v Value) {
1025 cname := C.CString(name)
1026 defer C.free(unsafe.Pointer(cname))
1027 v.C = C.LLVMGetNamedFunction(m.C, cname)
1031 func (m Module) FirstFunction() (v Value) { v.C = C.LLVMGetFirstFunction(m.C); return }
1032 func (m Module) LastFunction() (v Value) { v.C = C.LLVMGetLastFunction(m.C); return }
1033 func NextFunction(v Value) (rv Value) { rv.C = C.LLVMGetNextFunction(v.C); return }
1034 func PrevFunction(v Value) (rv Value) { rv.C = C.LLVMGetPreviousFunction(v.C); return }
1035 func (v Value) EraseFromParentAsFunction() { C.LLVMDeleteFunction(v.C) }
1036 func (v Value) IntrinsicID() int { return int(C.LLVMGetIntrinsicID(v.C)) }
1037 func (v Value) FunctionCallConv() CallConv {
1038 return CallConv(C.LLVMCallConv(C.LLVMGetFunctionCallConv(v.C)))
1040 func (v Value) SetFunctionCallConv(cc CallConv) { C.LLVMSetFunctionCallConv(v.C, C.unsigned(cc)) }
1041 func (v Value) GC() string { return C.GoString(C.LLVMGetGC(v.C)) }
1042 func (v Value) SetGC(name string) {
1043 cname := C.CString(name)
1044 defer C.free(unsafe.Pointer(cname))
1045 C.LLVMSetGC(v.C, cname)
1047 func (v Value) AddFunctionAttr(a Attribute) { C.LLVMAddFunctionAttr2(v.C, C.uint64_t(a)) }
1048 func (v Value) FunctionAttr() Attribute { return Attribute(C.LLVMGetFunctionAttr2(v.C)) }
1049 func (v Value) RemoveFunctionAttr(a Attribute) { C.LLVMRemoveFunctionAttr2(v.C, C.uint64_t(a)) }
1050 func (v Value) AddTargetDependentFunctionAttr(attr, value string) {
1051 cattr := C.CString(attr)
1052 defer C.free(unsafe.Pointer(cattr))
1053 cvalue := C.CString(value)
1054 defer C.free(unsafe.Pointer(cvalue))
1055 C.LLVMAddTargetDependentFunctionAttr(v.C, cattr, cvalue)
1057 func (v Value) SetPersonality(p Value) {
1058 C.LLVMSetPersonalityFn(v.C, p.C)
1061 // Operations on parameters
1062 func (v Value) ParamsCount() int { return int(C.LLVMCountParams(v.C)) }
1063 func (v Value) Params() []Value {
1064 out := make([]Value, v.ParamsCount())
1066 C.LLVMGetParams(v.C, llvmValueRefPtr(&out[0]))
1070 func (v Value) Param(i int) (rv Value) { rv.C = C.LLVMGetParam(v.C, C.unsigned(i)); return }
1071 func (v Value) ParamParent() (rv Value) { rv.C = C.LLVMGetParamParent(v.C); return }
1072 func (v Value) FirstParam() (rv Value) { rv.C = C.LLVMGetFirstParam(v.C); return }
1073 func (v Value) LastParam() (rv Value) { rv.C = C.LLVMGetLastParam(v.C); return }
1074 func NextParam(v Value) (rv Value) { rv.C = C.LLVMGetNextParam(v.C); return }
1075 func PrevParam(v Value) (rv Value) { rv.C = C.LLVMGetPreviousParam(v.C); return }
1076 func (v Value) AddAttribute(a Attribute) {
1078 panic("attribute value currently unsupported")
1080 C.LLVMAddAttribute(v.C, C.LLVMAttribute(a))
1082 func (v Value) RemoveAttribute(a Attribute) {
1084 panic("attribute value currently unsupported")
1086 C.LLVMRemoveAttribute(v.C, C.LLVMAttribute(a))
1088 func (v Value) Attribute() Attribute { return Attribute(C.LLVMGetAttribute(v.C)) }
1089 func (v Value) SetParamAlignment(align int) { C.LLVMSetParamAlignment(v.C, C.unsigned(align)) }
1091 // Operations on basic blocks
1092 func (bb BasicBlock) AsValue() (v Value) { v.C = C.LLVMBasicBlockAsValue(bb.C); return }
1093 func (v Value) IsBasicBlock() bool { return C.LLVMValueIsBasicBlock(v.C) != 0 }
1094 func (v Value) AsBasicBlock() (bb BasicBlock) { bb.C = C.LLVMValueAsBasicBlock(v.C); return }
1095 func (bb BasicBlock) Parent() (v Value) { v.C = C.LLVMGetBasicBlockParent(bb.C); return }
1096 func (v Value) BasicBlocksCount() int { return int(C.LLVMCountBasicBlocks(v.C)) }
1097 func (v Value) BasicBlocks() []BasicBlock {
1098 out := make([]BasicBlock, v.BasicBlocksCount())
1099 C.LLVMGetBasicBlocks(v.C, llvmBasicBlockRefPtr(&out[0]))
1102 func (v Value) FirstBasicBlock() (bb BasicBlock) { bb.C = C.LLVMGetFirstBasicBlock(v.C); return }
1103 func (v Value) LastBasicBlock() (bb BasicBlock) { bb.C = C.LLVMGetLastBasicBlock(v.C); return }
1104 func NextBasicBlock(bb BasicBlock) (rbb BasicBlock) { rbb.C = C.LLVMGetNextBasicBlock(bb.C); return }
1105 func PrevBasicBlock(bb BasicBlock) (rbb BasicBlock) { rbb.C = C.LLVMGetPreviousBasicBlock(bb.C); return }
1106 func (v Value) EntryBasicBlock() (bb BasicBlock) { bb.C = C.LLVMGetEntryBasicBlock(v.C); return }
1107 func (c Context) AddBasicBlock(f Value, name string) (bb BasicBlock) {
1108 cname := C.CString(name)
1109 defer C.free(unsafe.Pointer(cname))
1110 bb.C = C.LLVMAppendBasicBlockInContext(c.C, f.C, cname)
1113 func (c Context) InsertBasicBlock(ref BasicBlock, name string) (bb BasicBlock) {
1114 cname := C.CString(name)
1115 defer C.free(unsafe.Pointer(cname))
1116 bb.C = C.LLVMInsertBasicBlockInContext(c.C, ref.C, cname)
1119 func AddBasicBlock(f Value, name string) (bb BasicBlock) {
1120 cname := C.CString(name)
1121 defer C.free(unsafe.Pointer(cname))
1122 bb.C = C.LLVMAppendBasicBlock(f.C, cname)
1125 func InsertBasicBlock(ref BasicBlock, name string) (bb BasicBlock) {
1126 cname := C.CString(name)
1127 defer C.free(unsafe.Pointer(cname))
1128 bb.C = C.LLVMInsertBasicBlock(ref.C, cname)
1131 func (bb BasicBlock) EraseFromParent() { C.LLVMDeleteBasicBlock(bb.C) }
1132 func (bb BasicBlock) MoveBefore(pos BasicBlock) { C.LLVMMoveBasicBlockBefore(bb.C, pos.C) }
1133 func (bb BasicBlock) MoveAfter(pos BasicBlock) { C.LLVMMoveBasicBlockAfter(bb.C, pos.C) }
1135 // Operations on instructions
1136 func (v Value) InstructionParent() (bb BasicBlock) { bb.C = C.LLVMGetInstructionParent(v.C); return }
1137 func (bb BasicBlock) FirstInstruction() (v Value) { v.C = C.LLVMGetFirstInstruction(bb.C); return }
1138 func (bb BasicBlock) LastInstruction() (v Value) { v.C = C.LLVMGetLastInstruction(bb.C); return }
1139 func NextInstruction(v Value) (rv Value) { rv.C = C.LLVMGetNextInstruction(v.C); return }
1140 func PrevInstruction(v Value) (rv Value) { rv.C = C.LLVMGetPreviousInstruction(v.C); return }
1142 // Operations on call sites
1143 func (v Value) SetInstructionCallConv(cc CallConv) {
1144 C.LLVMSetInstructionCallConv(v.C, C.unsigned(cc))
1146 func (v Value) InstructionCallConv() CallConv {
1147 return CallConv(C.LLVMCallConv(C.LLVMGetInstructionCallConv(v.C)))
1149 func (v Value) AddInstrAttribute(i int, a Attribute) {
1151 panic("attribute value currently unsupported")
1153 C.LLVMAddInstrAttribute(v.C, C.unsigned(i), C.LLVMAttribute(a))
1155 func (v Value) RemoveInstrAttribute(i int, a Attribute) {
1157 panic("attribute value currently unsupported")
1159 C.LLVMRemoveInstrAttribute(v.C, C.unsigned(i), C.LLVMAttribute(a))
1161 func (v Value) SetInstrParamAlignment(i int, align int) {
1162 C.LLVMSetInstrParamAlignment(v.C, C.unsigned(i), C.unsigned(align))
1165 // Operations on call instructions (only)
1166 func (v Value) IsTailCall() bool { return C.LLVMIsTailCall(v.C) != 0 }
1167 func (v Value) SetTailCall(is bool) { C.LLVMSetTailCall(v.C, boolToLLVMBool(is)) }
1169 // Operations on phi nodes
1170 func (v Value) AddIncoming(vals []Value, blocks []BasicBlock) {
1171 ptr, nvals := llvmValueRefs(vals)
1172 C.LLVMAddIncoming(v.C, ptr, llvmBasicBlockRefPtr(&blocks[0]), nvals)
1174 func (v Value) IncomingCount() int { return int(C.LLVMCountIncoming(v.C)) }
1175 func (v Value) IncomingValue(i int) (rv Value) {
1176 rv.C = C.LLVMGetIncomingValue(v.C, C.unsigned(i))
1179 func (v Value) IncomingBlock(i int) (bb BasicBlock) {
1180 bb.C = C.LLVMGetIncomingBlock(v.C, C.unsigned(i))
1184 //-------------------------------------------------------------------------
1186 //-------------------------------------------------------------------------
1188 // An instruction builder represents a point within a basic block, and is the
1189 // exclusive means of building instructions using the C interface.
1191 func (c Context) NewBuilder() (b Builder) { b.C = C.LLVMCreateBuilderInContext(c.C); return }
1192 func NewBuilder() (b Builder) { b.C = C.LLVMCreateBuilder(); return }
1193 func (b Builder) SetInsertPoint(block BasicBlock, instr Value) {
1194 C.LLVMPositionBuilder(b.C, block.C, instr.C)
1196 func (b Builder) SetInsertPointBefore(instr Value) { C.LLVMPositionBuilderBefore(b.C, instr.C) }
1197 func (b Builder) SetInsertPointAtEnd(block BasicBlock) { C.LLVMPositionBuilderAtEnd(b.C, block.C) }
1198 func (b Builder) GetInsertBlock() (bb BasicBlock) { bb.C = C.LLVMGetInsertBlock(b.C); return }
1199 func (b Builder) ClearInsertionPoint() { C.LLVMClearInsertionPosition(b.C) }
1200 func (b Builder) Insert(instr Value) { C.LLVMInsertIntoBuilder(b.C, instr.C) }
1201 func (b Builder) InsertWithName(instr Value, name string) {
1202 cname := C.CString(name)
1203 defer C.free(unsafe.Pointer(cname))
1204 C.LLVMInsertIntoBuilderWithName(b.C, instr.C, cname)
1206 func (b Builder) Dispose() { C.LLVMDisposeBuilder(b.C) }
1209 func (b Builder) SetCurrentDebugLocation(line, col uint, scope, inlinedAt Metadata) {
1210 C.LLVMSetCurrentDebugLocation2(b.C, C.unsigned(line), C.unsigned(col), scope.C, inlinedAt.C)
1212 func (b Builder) SetInstDebugLocation(v Value) { C.LLVMSetInstDebugLocation(b.C, v.C) }
1213 func (b Builder) InsertDeclare(module Module, storage Value, md Value) Value {
1214 f := module.NamedFunction("llvm.dbg.declare")
1216 ftyp := FunctionType(VoidType(), []Type{storage.Type(), md.Type()}, false)
1217 f = AddFunction(module, "llvm.dbg.declare", ftyp)
1219 return b.CreateCall(f, []Value{storage, md}, "")
1223 func (b Builder) CreateRetVoid() (rv Value) { rv.C = C.LLVMBuildRetVoid(b.C); return }
1224 func (b Builder) CreateRet(v Value) (rv Value) { rv.C = C.LLVMBuildRet(b.C, v.C); return }
1225 func (b Builder) CreateAggregateRet(vs []Value) (rv Value) {
1226 ptr, nvals := llvmValueRefs(vs)
1227 rv.C = C.LLVMBuildAggregateRet(b.C, ptr, nvals)
1230 func (b Builder) CreateBr(bb BasicBlock) (rv Value) { rv.C = C.LLVMBuildBr(b.C, bb.C); return }
1231 func (b Builder) CreateCondBr(ifv Value, thenb, elseb BasicBlock) (rv Value) {
1232 rv.C = C.LLVMBuildCondBr(b.C, ifv.C, thenb.C, elseb.C)
1235 func (b Builder) CreateSwitch(v Value, elseb BasicBlock, numCases int) (rv Value) {
1236 rv.C = C.LLVMBuildSwitch(b.C, v.C, elseb.C, C.unsigned(numCases))
1239 func (b Builder) CreateIndirectBr(addr Value, numDests int) (rv Value) {
1240 rv.C = C.LLVMBuildIndirectBr(b.C, addr.C, C.unsigned(numDests))
1243 func (b Builder) CreateInvoke(fn Value, args []Value, then, catch BasicBlock, name string) (rv Value) {
1244 cname := C.CString(name)
1245 defer C.free(unsafe.Pointer(cname))
1246 ptr, nvals := llvmValueRefs(args)
1247 rv.C = C.LLVMBuildInvoke(b.C, fn.C, ptr, nvals, then.C, catch.C, cname)
1250 func (b Builder) CreateUnreachable() (rv Value) { rv.C = C.LLVMBuildUnreachable(b.C); return }
1252 // Add a case to the switch instruction
1253 func (v Value) AddCase(on Value, dest BasicBlock) { C.LLVMAddCase(v.C, on.C, dest.C) }
1255 // Add a destination to the indirectbr instruction
1256 func (v Value) AddDest(dest BasicBlock) { C.LLVMAddDestination(v.C, dest.C) }
1259 func (b Builder) CreateAdd(lhs, rhs Value, name string) (v Value) {
1260 cname := C.CString(name)
1261 defer C.free(unsafe.Pointer(cname))
1262 v.C = C.LLVMBuildAdd(b.C, lhs.C, rhs.C, cname)
1265 func (b Builder) CreateNSWAdd(lhs, rhs Value, name string) (v Value) {
1266 cname := C.CString(name)
1267 defer C.free(unsafe.Pointer(cname))
1268 v.C = C.LLVMBuildNSWAdd(b.C, lhs.C, rhs.C, cname)
1271 func (b Builder) CreateNUWAdd(lhs, rhs Value, name string) (v Value) {
1272 cname := C.CString(name)
1273 defer C.free(unsafe.Pointer(cname))
1274 v.C = C.LLVMBuildNUWAdd(b.C, lhs.C, rhs.C, cname)
1277 func (b Builder) CreateFAdd(lhs, rhs Value, name string) (v Value) {
1278 cname := C.CString(name)
1279 defer C.free(unsafe.Pointer(cname))
1280 v.C = C.LLVMBuildFAdd(b.C, lhs.C, rhs.C, cname)
1283 func (b Builder) CreateSub(lhs, rhs Value, name string) (v Value) {
1284 cname := C.CString(name)
1285 defer C.free(unsafe.Pointer(cname))
1286 v.C = C.LLVMBuildSub(b.C, lhs.C, rhs.C, cname)
1289 func (b Builder) CreateNSWSub(lhs, rhs Value, name string) (v Value) {
1290 cname := C.CString(name)
1291 defer C.free(unsafe.Pointer(cname))
1292 v.C = C.LLVMBuildNSWSub(b.C, lhs.C, rhs.C, cname)
1295 func (b Builder) CreateNUWSub(lhs, rhs Value, name string) (v Value) {
1296 cname := C.CString(name)
1297 defer C.free(unsafe.Pointer(cname))
1298 v.C = C.LLVMBuildNUWSub(b.C, lhs.C, rhs.C, cname)
1301 func (b Builder) CreateFSub(lhs, rhs Value, name string) (v Value) {
1302 cname := C.CString(name)
1303 v.C = C.LLVMBuildFSub(b.C, lhs.C, rhs.C, cname)
1304 C.free(unsafe.Pointer(cname))
1307 func (b Builder) CreateMul(lhs, rhs Value, name string) (v Value) {
1308 cname := C.CString(name)
1309 defer C.free(unsafe.Pointer(cname))
1310 v.C = C.LLVMBuildMul(b.C, lhs.C, rhs.C, cname)
1313 func (b Builder) CreateNSWMul(lhs, rhs Value, name string) (v Value) {
1314 cname := C.CString(name)
1315 defer C.free(unsafe.Pointer(cname))
1316 v.C = C.LLVMBuildNSWMul(b.C, lhs.C, rhs.C, cname)
1319 func (b Builder) CreateNUWMul(lhs, rhs Value, name string) (v Value) {
1320 cname := C.CString(name)
1321 defer C.free(unsafe.Pointer(cname))
1322 v.C = C.LLVMBuildNUWMul(b.C, lhs.C, rhs.C, cname)
1325 func (b Builder) CreateFMul(lhs, rhs Value, name string) (v Value) {
1326 cname := C.CString(name)
1327 defer C.free(unsafe.Pointer(cname))
1328 v.C = C.LLVMBuildFMul(b.C, lhs.C, rhs.C, cname)
1331 func (b Builder) CreateUDiv(lhs, rhs Value, name string) (v Value) {
1332 cname := C.CString(name)
1333 defer C.free(unsafe.Pointer(cname))
1334 v.C = C.LLVMBuildUDiv(b.C, lhs.C, rhs.C, cname)
1337 func (b Builder) CreateSDiv(lhs, rhs Value, name string) (v Value) {
1338 cname := C.CString(name)
1339 defer C.free(unsafe.Pointer(cname))
1340 v.C = C.LLVMBuildSDiv(b.C, lhs.C, rhs.C, cname)
1343 func (b Builder) CreateExactSDiv(lhs, rhs Value, name string) (v Value) {
1344 cname := C.CString(name)
1345 defer C.free(unsafe.Pointer(cname))
1346 v.C = C.LLVMBuildExactSDiv(b.C, lhs.C, rhs.C, cname)
1349 func (b Builder) CreateFDiv(lhs, rhs Value, name string) (v Value) {
1350 cname := C.CString(name)
1351 defer C.free(unsafe.Pointer(cname))
1352 v.C = C.LLVMBuildFDiv(b.C, lhs.C, rhs.C, cname)
1355 func (b Builder) CreateURem(lhs, rhs Value, name string) (v Value) {
1356 cname := C.CString(name)
1357 defer C.free(unsafe.Pointer(cname))
1358 v.C = C.LLVMBuildURem(b.C, lhs.C, rhs.C, cname)
1361 func (b Builder) CreateSRem(lhs, rhs Value, name string) (v Value) {
1362 cname := C.CString(name)
1363 defer C.free(unsafe.Pointer(cname))
1364 v.C = C.LLVMBuildSRem(b.C, lhs.C, rhs.C, cname)
1367 func (b Builder) CreateFRem(lhs, rhs Value, name string) (v Value) {
1368 cname := C.CString(name)
1369 defer C.free(unsafe.Pointer(cname))
1370 v.C = C.LLVMBuildFRem(b.C, lhs.C, rhs.C, cname)
1373 func (b Builder) CreateShl(lhs, rhs Value, name string) (v Value) {
1374 cname := C.CString(name)
1375 defer C.free(unsafe.Pointer(cname))
1376 v.C = C.LLVMBuildShl(b.C, lhs.C, rhs.C, cname)
1379 func (b Builder) CreateLShr(lhs, rhs Value, name string) (v Value) {
1380 cname := C.CString(name)
1381 defer C.free(unsafe.Pointer(cname))
1382 v.C = C.LLVMBuildLShr(b.C, lhs.C, rhs.C, cname)
1385 func (b Builder) CreateAShr(lhs, rhs Value, name string) (v Value) {
1386 cname := C.CString(name)
1387 defer C.free(unsafe.Pointer(cname))
1388 v.C = C.LLVMBuildAShr(b.C, lhs.C, rhs.C, cname)
1391 func (b Builder) CreateAnd(lhs, rhs Value, name string) (v Value) {
1392 cname := C.CString(name)
1393 defer C.free(unsafe.Pointer(cname))
1394 v.C = C.LLVMBuildAnd(b.C, lhs.C, rhs.C, cname)
1397 func (b Builder) CreateOr(lhs, rhs Value, name string) (v Value) {
1398 cname := C.CString(name)
1399 defer C.free(unsafe.Pointer(cname))
1400 v.C = C.LLVMBuildOr(b.C, lhs.C, rhs.C, cname)
1403 func (b Builder) CreateXor(lhs, rhs Value, name string) (v Value) {
1404 cname := C.CString(name)
1405 defer C.free(unsafe.Pointer(cname))
1406 v.C = C.LLVMBuildXor(b.C, lhs.C, rhs.C, cname)
1409 func (b Builder) CreateBinOp(op Opcode, lhs, rhs Value, name string) (v Value) {
1410 cname := C.CString(name)
1411 defer C.free(unsafe.Pointer(cname))
1412 v.C = C.LLVMBuildBinOp(b.C, C.LLVMOpcode(op), lhs.C, rhs.C, cname)
1415 func (b Builder) CreateNeg(v Value, name string) (rv Value) {
1416 cname := C.CString(name)
1417 defer C.free(unsafe.Pointer(cname))
1418 rv.C = C.LLVMBuildNeg(b.C, v.C, cname)
1421 func (b Builder) CreateNSWNeg(v Value, name string) (rv Value) {
1422 cname := C.CString(name)
1423 defer C.free(unsafe.Pointer(cname))
1424 rv.C = C.LLVMBuildNSWNeg(b.C, v.C, cname)
1427 func (b Builder) CreateNUWNeg(v Value, name string) (rv Value) {
1428 cname := C.CString(name)
1429 defer C.free(unsafe.Pointer(cname))
1430 rv.C = C.LLVMBuildNUWNeg(b.C, v.C, cname)
1433 func (b Builder) CreateFNeg(v Value, name string) (rv Value) {
1434 cname := C.CString(name)
1435 defer C.free(unsafe.Pointer(cname))
1436 rv.C = C.LLVMBuildFNeg(b.C, v.C, cname)
1439 func (b Builder) CreateNot(v Value, name string) (rv Value) {
1440 cname := C.CString(name)
1441 defer C.free(unsafe.Pointer(cname))
1442 rv.C = C.LLVMBuildNot(b.C, v.C, cname)
1448 func (b Builder) CreateMalloc(t Type, name string) (v Value) {
1449 cname := C.CString(name)
1450 defer C.free(unsafe.Pointer(cname))
1451 v.C = C.LLVMBuildMalloc(b.C, t.C, cname)
1454 func (b Builder) CreateArrayMalloc(t Type, val Value, name string) (v Value) {
1455 cname := C.CString(name)
1456 defer C.free(unsafe.Pointer(cname))
1457 v.C = C.LLVMBuildArrayMalloc(b.C, t.C, val.C, cname)
1460 func (b Builder) CreateAlloca(t Type, name string) (v Value) {
1461 cname := C.CString(name)
1462 defer C.free(unsafe.Pointer(cname))
1463 v.C = C.LLVMBuildAlloca(b.C, t.C, cname)
1466 func (b Builder) CreateArrayAlloca(t Type, val Value, name string) (v Value) {
1467 cname := C.CString(name)
1468 defer C.free(unsafe.Pointer(cname))
1469 v.C = C.LLVMBuildArrayAlloca(b.C, t.C, val.C, cname)
1472 func (b Builder) CreateFree(p Value) (v Value) {
1473 v.C = C.LLVMBuildFree(b.C, p.C)
1476 func (b Builder) CreateLoad(p Value, name string) (v Value) {
1477 cname := C.CString(name)
1478 defer C.free(unsafe.Pointer(cname))
1479 v.C = C.LLVMBuildLoad(b.C, p.C, cname)
1482 func (b Builder) CreateStore(val Value, p Value) (v Value) {
1483 v.C = C.LLVMBuildStore(b.C, val.C, p.C)
1486 func (b Builder) CreateGEP(p Value, indices []Value, name string) (v Value) {
1487 cname := C.CString(name)
1488 defer C.free(unsafe.Pointer(cname))
1489 ptr, nvals := llvmValueRefs(indices)
1490 v.C = C.LLVMBuildGEP(b.C, p.C, ptr, nvals, cname)
1493 func (b Builder) CreateInBoundsGEP(p Value, indices []Value, name string) (v Value) {
1494 cname := C.CString(name)
1495 defer C.free(unsafe.Pointer(cname))
1496 ptr, nvals := llvmValueRefs(indices)
1497 v.C = C.LLVMBuildInBoundsGEP(b.C, p.C, ptr, nvals, cname)
1500 func (b Builder) CreateStructGEP(p Value, i int, name string) (v Value) {
1501 cname := C.CString(name)
1502 defer C.free(unsafe.Pointer(cname))
1503 v.C = C.LLVMBuildStructGEP(b.C, p.C, C.unsigned(i), cname)
1506 func (b Builder) CreateGlobalString(str, name string) (v Value) {
1507 cstr := C.CString(str)
1508 defer C.free(unsafe.Pointer(cstr))
1509 cname := C.CString(name)
1510 defer C.free(unsafe.Pointer(cname))
1511 v.C = C.LLVMBuildGlobalString(b.C, cstr, cname)
1514 func (b Builder) CreateGlobalStringPtr(str, name string) (v Value) {
1515 cstr := C.CString(str)
1516 defer C.free(unsafe.Pointer(cstr))
1517 cname := C.CString(name)
1518 defer C.free(unsafe.Pointer(cname))
1519 v.C = C.LLVMBuildGlobalStringPtr(b.C, cstr, cname)
1524 func (b Builder) CreateTrunc(val Value, t Type, name string) (v Value) {
1525 cname := C.CString(name)
1526 defer C.free(unsafe.Pointer(cname))
1527 v.C = C.LLVMBuildTrunc(b.C, val.C, t.C, cname)
1530 func (b Builder) CreateZExt(val Value, t Type, name string) (v Value) {
1531 cname := C.CString(name)
1532 defer C.free(unsafe.Pointer(cname))
1533 v.C = C.LLVMBuildZExt(b.C, val.C, t.C, cname)
1536 func (b Builder) CreateSExt(val Value, t Type, name string) (v Value) {
1537 cname := C.CString(name)
1538 defer C.free(unsafe.Pointer(cname))
1539 v.C = C.LLVMBuildSExt(b.C, val.C, t.C, cname)
1542 func (b Builder) CreateFPToUI(val Value, t Type, name string) (v Value) {
1543 cname := C.CString(name)
1544 defer C.free(unsafe.Pointer(cname))
1545 v.C = C.LLVMBuildFPToUI(b.C, val.C, t.C, cname)
1548 func (b Builder) CreateFPToSI(val Value, t Type, name string) (v Value) {
1549 cname := C.CString(name)
1550 defer C.free(unsafe.Pointer(cname))
1551 v.C = C.LLVMBuildFPToSI(b.C, val.C, t.C, cname)
1554 func (b Builder) CreateUIToFP(val Value, t Type, name string) (v Value) {
1555 cname := C.CString(name)
1556 defer C.free(unsafe.Pointer(cname))
1557 v.C = C.LLVMBuildUIToFP(b.C, val.C, t.C, cname)
1560 func (b Builder) CreateSIToFP(val Value, t Type, name string) (v Value) {
1561 cname := C.CString(name)
1562 defer C.free(unsafe.Pointer(cname))
1563 v.C = C.LLVMBuildSIToFP(b.C, val.C, t.C, cname)
1566 func (b Builder) CreateFPTrunc(val Value, t Type, name string) (v Value) {
1567 cname := C.CString(name)
1568 defer C.free(unsafe.Pointer(cname))
1569 v.C = C.LLVMBuildFPTrunc(b.C, val.C, t.C, cname)
1572 func (b Builder) CreateFPExt(val Value, t Type, name string) (v Value) {
1573 cname := C.CString(name)
1574 defer C.free(unsafe.Pointer(cname))
1575 v.C = C.LLVMBuildFPExt(b.C, val.C, t.C, cname)
1578 func (b Builder) CreatePtrToInt(val Value, t Type, name string) (v Value) {
1579 cname := C.CString(name)
1580 defer C.free(unsafe.Pointer(cname))
1581 v.C = C.LLVMBuildPtrToInt(b.C, val.C, t.C, cname)
1584 func (b Builder) CreateIntToPtr(val Value, t Type, name string) (v Value) {
1585 cname := C.CString(name)
1586 defer C.free(unsafe.Pointer(cname))
1587 v.C = C.LLVMBuildIntToPtr(b.C, val.C, t.C, cname)
1590 func (b Builder) CreateBitCast(val Value, t Type, name string) (v Value) {
1591 cname := C.CString(name)
1592 defer C.free(unsafe.Pointer(cname))
1593 v.C = C.LLVMBuildBitCast(b.C, val.C, t.C, cname)
1596 func (b Builder) CreateZExtOrBitCast(val Value, t Type, name string) (v Value) {
1597 cname := C.CString(name)
1598 defer C.free(unsafe.Pointer(cname))
1599 v.C = C.LLVMBuildZExtOrBitCast(b.C, val.C, t.C, cname)
1602 func (b Builder) CreateSExtOrBitCast(val Value, t Type, name string) (v Value) {
1603 cname := C.CString(name)
1604 defer C.free(unsafe.Pointer(cname))
1605 v.C = C.LLVMBuildSExtOrBitCast(b.C, val.C, t.C, cname)
1608 func (b Builder) CreateTruncOrBitCast(val Value, t Type, name string) (v Value) {
1609 cname := C.CString(name)
1610 defer C.free(unsafe.Pointer(cname))
1611 v.C = C.LLVMBuildTruncOrBitCast(b.C, val.C, t.C, cname)
1614 func (b Builder) CreateCast(val Value, op Opcode, t Type, name string) (v Value) {
1615 cname := C.CString(name)
1616 defer C.free(unsafe.Pointer(cname))
1617 v.C = C.LLVMBuildCast(b.C, C.LLVMOpcode(op), val.C, t.C, cname)
1620 func (b Builder) CreatePointerCast(val Value, t Type, name string) (v Value) {
1621 cname := C.CString(name)
1622 defer C.free(unsafe.Pointer(cname))
1623 v.C = C.LLVMBuildPointerCast(b.C, val.C, t.C, cname)
1626 func (b Builder) CreateIntCast(val Value, t Type, name string) (v Value) {
1627 cname := C.CString(name)
1628 defer C.free(unsafe.Pointer(cname))
1629 v.C = C.LLVMBuildIntCast(b.C, val.C, t.C, cname)
1632 func (b Builder) CreateFPCast(val Value, t Type, name string) (v Value) {
1633 cname := C.CString(name)
1634 defer C.free(unsafe.Pointer(cname))
1635 v.C = C.LLVMBuildFPCast(b.C, val.C, t.C, cname)
1640 func (b Builder) CreateICmp(pred IntPredicate, lhs, rhs Value, name string) (v Value) {
1641 cname := C.CString(name)
1642 defer C.free(unsafe.Pointer(cname))
1643 v.C = C.LLVMBuildICmp(b.C, C.LLVMIntPredicate(pred), lhs.C, rhs.C, cname)
1646 func (b Builder) CreateFCmp(pred FloatPredicate, lhs, rhs Value, name string) (v Value) {
1647 cname := C.CString(name)
1648 defer C.free(unsafe.Pointer(cname))
1649 v.C = C.LLVMBuildFCmp(b.C, C.LLVMRealPredicate(pred), lhs.C, rhs.C, cname)
1653 // Miscellaneous instructions
1654 func (b Builder) CreatePHI(t Type, name string) (v Value) {
1655 cname := C.CString(name)
1656 defer C.free(unsafe.Pointer(cname))
1657 v.C = C.LLVMBuildPhi(b.C, t.C, cname)
1660 func (b Builder) CreateCall(fn Value, args []Value, name string) (v Value) {
1661 cname := C.CString(name)
1662 defer C.free(unsafe.Pointer(cname))
1663 ptr, nvals := llvmValueRefs(args)
1664 v.C = C.LLVMBuildCall(b.C, fn.C, ptr, nvals, cname)
1668 func (b Builder) CreateSelect(ifv, thenv, elsev Value, name string) (v Value) {
1669 cname := C.CString(name)
1670 defer C.free(unsafe.Pointer(cname))
1671 v.C = C.LLVMBuildSelect(b.C, ifv.C, thenv.C, elsev.C, cname)
1675 func (b Builder) CreateVAArg(list Value, t Type, name string) (v Value) {
1676 cname := C.CString(name)
1677 defer C.free(unsafe.Pointer(cname))
1678 v.C = C.LLVMBuildVAArg(b.C, list.C, t.C, cname)
1681 func (b Builder) CreateExtractElement(vec, i Value, name string) (v Value) {
1682 cname := C.CString(name)
1683 defer C.free(unsafe.Pointer(cname))
1684 v.C = C.LLVMBuildExtractElement(b.C, vec.C, i.C, cname)
1687 func (b Builder) CreateInsertElement(vec, elt, i Value, name string) (v Value) {
1688 cname := C.CString(name)
1689 defer C.free(unsafe.Pointer(cname))
1690 v.C = C.LLVMBuildInsertElement(b.C, vec.C, elt.C, i.C, cname)
1693 func (b Builder) CreateShuffleVector(v1, v2, mask Value, name string) (v Value) {
1694 cname := C.CString(name)
1695 defer C.free(unsafe.Pointer(cname))
1696 v.C = C.LLVMBuildShuffleVector(b.C, v1.C, v2.C, mask.C, cname)
1699 func (b Builder) CreateExtractValue(agg Value, i int, name string) (v Value) {
1700 cname := C.CString(name)
1701 defer C.free(unsafe.Pointer(cname))
1702 v.C = C.LLVMBuildExtractValue(b.C, agg.C, C.unsigned(i), cname)
1705 func (b Builder) CreateInsertValue(agg, elt Value, i int, name string) (v Value) {
1706 cname := C.CString(name)
1707 defer C.free(unsafe.Pointer(cname))
1708 v.C = C.LLVMBuildInsertValue(b.C, agg.C, elt.C, C.unsigned(i), cname)
1712 func (b Builder) CreateIsNull(val Value, name string) (v Value) {
1713 cname := C.CString(name)
1714 defer C.free(unsafe.Pointer(cname))
1715 v.C = C.LLVMBuildIsNull(b.C, val.C, cname)
1718 func (b Builder) CreateIsNotNull(val Value, name string) (v Value) {
1719 cname := C.CString(name)
1720 defer C.free(unsafe.Pointer(cname))
1721 v.C = C.LLVMBuildIsNotNull(b.C, val.C, cname)
1724 func (b Builder) CreatePtrDiff(lhs, rhs Value, name string) (v Value) {
1725 cname := C.CString(name)
1726 defer C.free(unsafe.Pointer(cname))
1727 v.C = C.LLVMBuildPtrDiff(b.C, lhs.C, rhs.C, cname)
1731 func (b Builder) CreateLandingPad(t Type, nclauses int, name string) (l Value) {
1732 cname := C.CString(name)
1733 defer C.free(unsafe.Pointer(cname))
1734 l.C = C.LLVMBuildLandingPad(b.C, t.C, nil, C.unsigned(nclauses), cname)
1738 func (l Value) AddClause(v Value) {
1739 C.LLVMAddClause(l.C, v.C)
1742 func (l Value) SetCleanup(cleanup bool) {
1743 C.LLVMSetCleanup(l.C, boolToLLVMBool(cleanup))
1746 func (b Builder) CreateResume(ex Value) (v Value) {
1747 v.C = C.LLVMBuildResume(b.C, ex.C)
1751 //-------------------------------------------------------------------------
1752 // llvm.ModuleProvider
1753 //-------------------------------------------------------------------------
1755 // Changes the type of M so it can be passed to FunctionPassManagers and the
1756 // JIT. They take ModuleProviders for historical reasons.
1757 func NewModuleProviderForModule(m Module) (mp ModuleProvider) {
1758 mp.C = C.LLVMCreateModuleProviderForExistingModule(m.C)
1762 // Destroys the module M.
1763 func (mp ModuleProvider) Dispose() { C.LLVMDisposeModuleProvider(mp.C) }
1765 //-------------------------------------------------------------------------
1766 // llvm.MemoryBuffer
1767 //-------------------------------------------------------------------------
1769 func NewMemoryBufferFromFile(path string) (b MemoryBuffer, err error) {
1771 cpath := C.CString(path)
1772 defer C.free(unsafe.Pointer(cpath))
1773 fail := C.LLVMCreateMemoryBufferWithContentsOfFile(cpath, &b.C, &cmsg)
1776 err = errors.New(C.GoString(cmsg))
1777 C.LLVMDisposeMessage(cmsg)
1782 func NewMemoryBufferFromStdin() (b MemoryBuffer, err error) {
1784 fail := C.LLVMCreateMemoryBufferWithSTDIN(&b.C, &cmsg)
1787 err = errors.New(C.GoString(cmsg))
1788 C.LLVMDisposeMessage(cmsg)
1793 func (b MemoryBuffer) Bytes() []byte {
1794 cstart := C.LLVMGetBufferStart(b.C)
1795 csize := C.LLVMGetBufferSize(b.C)
1796 return C.GoBytes(unsafe.Pointer(cstart), C.int(csize))
1799 func (b MemoryBuffer) Dispose() { C.LLVMDisposeMemoryBuffer(b.C) }
1801 //-------------------------------------------------------------------------
1803 //-------------------------------------------------------------------------
1805 // Constructs a new whole-module pass pipeline. This type of pipeline is
1806 // suitable for link-time optimization and whole-module transformations.
1807 // See llvm::PassManager::PassManager.
1808 func NewPassManager() (pm PassManager) { pm.C = C.LLVMCreatePassManager(); return }
1810 // Constructs a new function-by-function pass pipeline over the module
1811 // provider. It does not take ownership of the module provider. This type of
1812 // pipeline is suitable for code generation and JIT compilation tasks.
1813 // See llvm::FunctionPassManager::FunctionPassManager.
1814 func NewFunctionPassManagerForModule(m Module) (pm PassManager) {
1815 pm.C = C.LLVMCreateFunctionPassManagerForModule(m.C)
1819 // Initializes, executes on the provided module, and finalizes all of the
1820 // passes scheduled in the pass manager. Returns 1 if any of the passes
1821 // modified the module, 0 otherwise. See llvm::PassManager::run(Module&).
1822 func (pm PassManager) Run(m Module) bool { return C.LLVMRunPassManager(pm.C, m.C) != 0 }
1824 // Initializes all of the function passes scheduled in the function pass
1825 // manager. Returns 1 if any of the passes modified the module, 0 otherwise.
1826 // See llvm::FunctionPassManager::doInitialization.
1827 func (pm PassManager) InitializeFunc() bool { return C.LLVMInitializeFunctionPassManager(pm.C) != 0 }
1829 // Executes all of the function passes scheduled in the function pass manager
1830 // on the provided function. Returns 1 if any of the passes modified the
1831 // function, false otherwise.
1832 // See llvm::FunctionPassManager::run(Function&).
1833 func (pm PassManager) RunFunc(f Value) bool { return C.LLVMRunFunctionPassManager(pm.C, f.C) != 0 }
1835 // Finalizes all of the function passes scheduled in in the function pass
1836 // manager. Returns 1 if any of the passes modified the module, 0 otherwise.
1837 // See llvm::FunctionPassManager::doFinalization.
1838 func (pm PassManager) FinalizeFunc() bool { return C.LLVMFinalizeFunctionPassManager(pm.C) != 0 }
1840 // Frees the memory of a pass pipeline. For function pipelines, does not free
1841 // the module provider.
1842 // See llvm::PassManagerBase::~PassManagerBase.
1843 func (pm PassManager) Dispose() { C.LLVMDisposePassManager(pm.C) }
1845 //-------------------------------------------------------------------------
1847 //-------------------------------------------------------------------------
1849 func (md Metadata) ReplaceAllUsesWith(new Metadata) {
1850 C.LLVMMetadataReplaceAllUsesWith(md.C, new.C)