Go bindings: introduce llvm.TemporaryMDNode.
[oota-llvm.git] / bindings / go / llvm / ir.go
1 //===- ir.go - Bindings for ir --------------------------------------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file defines bindings for the ir component.
11 //
12 //===----------------------------------------------------------------------===//
13
14 package llvm
15
16 /*
17 #include "llvm-c/Core.h"
18 #include "IRBindings.h"
19 #include <stdlib.h>
20 */
21 import "C"
22 import "unsafe"
23 import "errors"
24
25 type (
26         // We use these weird structs here because *Ref types are pointers and
27         // Go's spec says that a pointer cannot be used as a receiver base type.
28         Context struct {
29                 C C.LLVMContextRef
30         }
31         Module struct {
32                 C C.LLVMModuleRef
33         }
34         Type struct {
35                 C C.LLVMTypeRef
36         }
37         Value struct {
38                 C C.LLVMValueRef
39         }
40         BasicBlock struct {
41                 C C.LLVMBasicBlockRef
42         }
43         Builder struct {
44                 C C.LLVMBuilderRef
45         }
46         ModuleProvider struct {
47                 C C.LLVMModuleProviderRef
48         }
49         MemoryBuffer struct {
50                 C C.LLVMMemoryBufferRef
51         }
52         PassManager struct {
53                 C C.LLVMPassManagerRef
54         }
55         Use struct {
56                 C C.LLVMUseRef
57         }
58         Metadata struct {
59                 C C.LLVMMetadataRef
60         }
61         Attribute        uint64
62         Opcode           C.LLVMOpcode
63         TypeKind         C.LLVMTypeKind
64         Linkage          C.LLVMLinkage
65         Visibility       C.LLVMVisibility
66         CallConv         C.LLVMCallConv
67         IntPredicate     C.LLVMIntPredicate
68         FloatPredicate   C.LLVMRealPredicate
69         LandingPadClause C.LLVMLandingPadClauseTy
70 )
71
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 }
82
83 // helpers
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))
88 }
89 func llvmBasicBlockRefPtr(t *BasicBlock) *C.LLVMBasicBlockRef {
90         return (*C.LLVMBasicBlockRef)(unsafe.Pointer(t))
91 }
92 func boolToLLVMBool(b bool) C.LLVMBool {
93         if b {
94                 return C.LLVMBool(1)
95         }
96         return C.LLVMBool(0)
97 }
98
99 func llvmValueRefs(values []Value) (*C.LLVMValueRef, C.unsigned) {
100         var pt *C.LLVMValueRef
101         ptlen := C.unsigned(len(values))
102         if ptlen > 0 {
103                 pt = llvmValueRefPtr(&values[0])
104         }
105         return pt, ptlen
106 }
107
108 func llvmMetadataRefs(mds []Metadata) (*C.LLVMMetadataRef, C.unsigned) {
109         var pt *C.LLVMMetadataRef
110         ptlen := C.unsigned(len(mds))
111         if ptlen > 0 {
112                 pt = llvmMetadataRefPtr(&mds[0])
113         }
114         return pt, ptlen
115 }
116
117 //-------------------------------------------------------------------------
118 // llvm.Attribute
119 //-------------------------------------------------------------------------
120
121 const (
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 )
164
165 //-------------------------------------------------------------------------
166 // llvm.Opcode
167 //-------------------------------------------------------------------------
168
169 const (
170         Ret         Opcode = C.LLVMRet
171         Br          Opcode = C.LLVMBr
172         Switch      Opcode = C.LLVMSwitch
173         IndirectBr  Opcode = C.LLVMIndirectBr
174         Invoke      Opcode = C.LLVMInvoke
175         Unreachable Opcode = C.LLVMUnreachable
176
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
190
191         // Logical Operators
192         Shl  Opcode = C.LLVMShl
193         LShr Opcode = C.LLVMLShr
194         AShr Opcode = C.LLVMAShr
195         And  Opcode = C.LLVMAnd
196         Or   Opcode = C.LLVMOr
197         Xor  Opcode = C.LLVMXor
198
199         // Memory Operators
200         Alloca        Opcode = C.LLVMAlloca
201         Load          Opcode = C.LLVMLoad
202         Store         Opcode = C.LLVMStore
203         GetElementPtr Opcode = C.LLVMGetElementPtr
204
205         // Cast Operators
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
218
219         // Other Operators
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
225         // UserOp1
226         // UserOp2
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
233 )
234
235 //-------------------------------------------------------------------------
236 // llvm.TypeKind
237 //-------------------------------------------------------------------------
238
239 const (
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
254 )
255
256 //-------------------------------------------------------------------------
257 // llvm.Linkage
258 //-------------------------------------------------------------------------
259
260 const (
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
272 )
273
274 //-------------------------------------------------------------------------
275 // llvm.Visibility
276 //-------------------------------------------------------------------------
277
278 const (
279         DefaultVisibility   Visibility = C.LLVMDefaultVisibility
280         HiddenVisibility    Visibility = C.LLVMHiddenVisibility
281         ProtectedVisibility Visibility = C.LLVMProtectedVisibility
282 )
283
284 //-------------------------------------------------------------------------
285 // llvm.CallConv
286 //-------------------------------------------------------------------------
287
288 const (
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
294 )
295
296 //-------------------------------------------------------------------------
297 // llvm.IntPredicate
298 //-------------------------------------------------------------------------
299
300 const (
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
311 )
312
313 //-------------------------------------------------------------------------
314 // llvm.FloatPredicate
315 //-------------------------------------------------------------------------
316
317 const (
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
334 )
335
336 //-------------------------------------------------------------------------
337 // llvm.LandingPadClause
338 //-------------------------------------------------------------------------
339
340 const (
341         LandingPadCatch  LandingPadClause = C.LLVMLandingPadCatch
342         LandingPadFilter LandingPadClause = C.LLVMLandingPadFilter
343 )
344
345 //-------------------------------------------------------------------------
346 // llvm.Context
347 //-------------------------------------------------------------------------
348
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) }
352
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))))
357         return
358 }
359
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))))
364         return
365 }
366
367 //-------------------------------------------------------------------------
368 // llvm.Module
369 //-------------------------------------------------------------------------
370
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)
377         return
378 }
379
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)
384         return
385 }
386
387 // See llvm::Module::~Module
388 func (m Module) Dispose() { C.LLVMDisposeModule(m.C) }
389
390 // Data layout. See Module::getDataLayout.
391 func (m Module) DataLayout() string {
392         clayout := C.LLVMGetDataLayout(m.C)
393         return C.GoString(clayout)
394 }
395
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)
400 }
401
402 // Target triple. See Module::getTargetTriple.
403 func (m Module) Target() string {
404         ctarget := C.LLVMGetTarget(m.C)
405         return C.GoString(ctarget)
406 }
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)
411 }
412
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)
417         return
418 }
419
420 // See Module::dump.
421 func (m Module) Dump() {
422         C.LLVMDumpModule(m.C)
423 }
424
425 func (m Module) String() string {
426         cir := C.LLVMPrintModuleToString(m.C)
427         defer C.free(unsafe.Pointer(cir))
428         ir := C.GoString(cir)
429         return ir
430 }
431
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)
437 }
438
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)
443 }
444
445 func (m Module) Context() (c Context) {
446         c.C = C.LLVMGetModuleContext(m.C)
447         return
448 }
449
450 //-------------------------------------------------------------------------
451 // llvm.Type
452 //-------------------------------------------------------------------------
453
454 // LLVM types conform to the following hierarchy:
455 //
456 //   types:
457 //     integer type
458 //     real type
459 //     function type
460 //     sequence types:
461 //       array type
462 //       pointer type
463 //       vector type
464 //     void type
465 //     label type
466 //     opaque type
467
468 // See llvm::LLVMTypeKind::getTypeID.
469 func (t Type) TypeKind() TypeKind { return TypeKind(C.LLVMGetTypeKind(t.C)) }
470
471 // See llvm::LLVMType::getContext.
472 func (t Type) Context() (c Context) {
473         c.C = C.LLVMGetTypeContext(t.C)
474         return
475 }
476
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))
485         return
486 }
487
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 }
493
494 func IntType(numbits int) (t Type) {
495         t.C = C.LLVMIntType(C.unsigned(numbits))
496         return
497 }
498
499 func (t Type) IntTypeWidth() int {
500         return int(C.LLVMGetIntTypeWidth(t.C))
501 }
502
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 }
509
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 }
515
516 // Operations on function types
517 func FunctionType(returnType Type, paramTypes []Type, isVarArg bool) (t Type) {
518         var pt *C.LLVMTypeRef
519         var ptlen C.unsigned
520         if len(paramTypes) > 0 {
521                 pt = llvmTypeRefPtr(&paramTypes[0])
522                 ptlen = C.unsigned(len(paramTypes))
523         }
524         t.C = C.LLVMFunctionType(returnType.C,
525                 pt,
526                 ptlen,
527                 boolToLLVMBool(isVarArg))
528         return
529 }
530
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()
536         if count > 0 {
537                 out := make([]Type, count)
538                 C.LLVMGetParamTypes(t.C, llvmTypeRefPtr(&out[0]))
539                 return out
540         }
541         return nil
542 }
543
544 // Operations on struct types
545 func (c Context) StructType(elementTypes []Type, packed bool) (t Type) {
546         var pt *C.LLVMTypeRef
547         var ptlen C.unsigned
548         if len(elementTypes) > 0 {
549                 pt = llvmTypeRefPtr(&elementTypes[0])
550                 ptlen = C.unsigned(len(elementTypes))
551         }
552         t.C = C.LLVMStructTypeInContext(c.C,
553                 pt,
554                 ptlen,
555                 boolToLLVMBool(packed))
556         return
557 }
558
559 func StructType(elementTypes []Type, packed bool) (t Type) {
560         var pt *C.LLVMTypeRef
561         var ptlen C.unsigned
562         if len(elementTypes) > 0 {
563                 pt = llvmTypeRefPtr(&elementTypes[0])
564                 ptlen = C.unsigned(len(elementTypes))
565         }
566         t.C = C.LLVMStructType(pt, ptlen, boolToLLVMBool(packed))
567         return
568 }
569
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)
574         return
575 }
576
577 func (t Type) StructName() string {
578         return C.GoString(C.LLVMGetStructName(t.C))
579 }
580
581 func (t Type) StructSetBody(elementTypes []Type, packed bool) {
582         var pt *C.LLVMTypeRef
583         var ptlen C.unsigned
584         if len(elementTypes) > 0 {
585                 pt = llvmTypeRefPtr(&elementTypes[0])
586                 ptlen = C.unsigned(len(elementTypes))
587         }
588         C.LLVMStructSetBody(t.C, pt, ptlen, boolToLLVMBool(packed))
589 }
590
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())
595         if len(out) > 0 {
596                 C.LLVMGetStructElementTypes(t.C, llvmTypeRefPtr(&out[0]))
597         }
598         return out
599 }
600
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))
604         return
605 }
606 func PointerType(elementType Type, addressSpace int) (t Type) {
607         t.C = C.LLVMPointerType(elementType.C, C.unsigned(addressSpace))
608         return
609 }
610 func VectorType(elementType Type, elementCount int) (t Type) {
611         t.C = C.LLVMVectorType(elementType.C, C.unsigned(elementCount))
612         return
613 }
614
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)) }
619
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 }
623
624 func VoidType() (t Type)  { t.C = C.LLVMVoidType(); return }
625 func LabelType() (t Type) { t.C = C.LLVMLabelType(); return }
626
627 //-------------------------------------------------------------------------
628 // llvm.Value
629 //-------------------------------------------------------------------------
630
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)
638 }
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))
644         return
645 }
646 func (v Value) SetMetadata(kind int, node Metadata) {
647         C.LLVMSetMetadata2(v.C, C.unsigned(kind), node.C)
648 }
649
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)
660         return
661 }
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 }
719
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 }
725
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)) }
730
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 }
739
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)))
745         return
746 }
747 func (c Context) MDNode(mds []Metadata) (md Metadata) {
748         ptr, nvals := llvmMetadataRefs(mds)
749         md.C = C.LLVMMDNode2(c.C, ptr, nvals)
750         return
751 }
752 func (c Context) TemporaryMDNode(mds []Metadata) (md Metadata) {
753         ptr, nvals := llvmMetadataRefs(mds)
754         md.C = C.LLVMTemporaryMDNode(c.C, ptr, nvals)
755         return
756 }
757
758 // Operations on scalar constants
759 func ConstInt(t Type, n uint64, signExtend bool) (v Value) {
760         v.C = C.LLVMConstInt(t.C,
761                 C.ulonglong(n),
762                 boolToLLVMBool(signExtend))
763         return
764 }
765 func ConstIntFromString(t Type, str string, radix int) (v Value) {
766         cstr := C.CString(str)
767         defer C.free(unsafe.Pointer(cstr))
768         v.C = C.LLVMConstIntOfString(t.C, cstr, C.uint8_t(radix))
769         return
770 }
771 func ConstFloat(t Type, n float64) (v Value) {
772         v.C = C.LLVMConstReal(t.C, C.double(n))
773         return
774 }
775 func ConstFloatFromString(t Type, str string) (v Value) {
776         cstr := C.CString(str)
777         defer C.free(unsafe.Pointer(cstr))
778         v.C = C.LLVMConstRealOfString(t.C, cstr)
779         return
780 }
781
782 func (v Value) ZExtValue() uint64 { return uint64(C.LLVMConstIntGetZExtValue(v.C)) }
783 func (v Value) SExtValue() int64  { return int64(C.LLVMConstIntGetSExtValue(v.C)) }
784
785 // Operations on composite constants
786 func (c Context) ConstString(str string, addnull bool) (v Value) {
787         cstr := C.CString(str)
788         defer C.free(unsafe.Pointer(cstr))
789         v.C = C.LLVMConstStringInContext(c.C, cstr,
790                 C.unsigned(len(str)), boolToLLVMBool(!addnull))
791         return
792 }
793 func (c Context) ConstStruct(constVals []Value, packed bool) (v Value) {
794         ptr, nvals := llvmValueRefs(constVals)
795         v.C = C.LLVMConstStructInContext(c.C, ptr, nvals,
796                 boolToLLVMBool(packed))
797         return
798 }
799 func ConstNamedStruct(t Type, constVals []Value) (v Value) {
800         ptr, nvals := llvmValueRefs(constVals)
801         v.C = C.LLVMConstNamedStruct(t.C, ptr, nvals)
802         return
803 }
804 func ConstString(str string, addnull bool) (v Value) {
805         cstr := C.CString(str)
806         defer C.free(unsafe.Pointer(cstr))
807         v.C = C.LLVMConstString(cstr,
808                 C.unsigned(len(str)), boolToLLVMBool(!addnull))
809         return
810 }
811 func ConstArray(t Type, constVals []Value) (v Value) {
812         ptr, nvals := llvmValueRefs(constVals)
813         v.C = C.LLVMConstArray(t.C, ptr, nvals)
814         return
815 }
816 func ConstStruct(constVals []Value, packed bool) (v Value) {
817         ptr, nvals := llvmValueRefs(constVals)
818         v.C = C.LLVMConstStruct(ptr, nvals, boolToLLVMBool(packed))
819         return
820 }
821 func ConstVector(scalarConstVals []Value, packed bool) (v Value) {
822         ptr, nvals := llvmValueRefs(scalarConstVals)
823         v.C = C.LLVMConstVector(ptr, nvals)
824         return
825 }
826
827 // Constant expressions
828 func (v Value) Opcode() Opcode                { return Opcode(C.LLVMGetConstOpcode(v.C)) }
829 func (v Value) InstructionOpcode() Opcode     { return Opcode(C.LLVMGetInstructionOpcode(v.C)) }
830 func AlignOf(t Type) (v Value)                { v.C = C.LLVMAlignOf(t.C); return }
831 func SizeOf(t Type) (v Value)                 { v.C = C.LLVMSizeOf(t.C); return }
832 func ConstNeg(v Value) (rv Value)             { rv.C = C.LLVMConstNeg(v.C); return }
833 func ConstNSWNeg(v Value) (rv Value)          { rv.C = C.LLVMConstNSWNeg(v.C); return }
834 func ConstNUWNeg(v Value) (rv Value)          { rv.C = C.LLVMConstNUWNeg(v.C); return }
835 func ConstFNeg(v Value) (rv Value)            { rv.C = C.LLVMConstFNeg(v.C); return }
836 func ConstNot(v Value) (rv Value)             { rv.C = C.LLVMConstNot(v.C); return }
837 func ConstAdd(lhs, rhs Value) (v Value)       { v.C = C.LLVMConstAdd(lhs.C, rhs.C); return }
838 func ConstNSWAdd(lhs, rhs Value) (v Value)    { v.C = C.LLVMConstNSWAdd(lhs.C, rhs.C); return }
839 func ConstNUWAdd(lhs, rhs Value) (v Value)    { v.C = C.LLVMConstNUWAdd(lhs.C, rhs.C); return }
840 func ConstFAdd(lhs, rhs Value) (v Value)      { v.C = C.LLVMConstFAdd(lhs.C, rhs.C); return }
841 func ConstSub(lhs, rhs Value) (v Value)       { v.C = C.LLVMConstSub(lhs.C, rhs.C); return }
842 func ConstNSWSub(lhs, rhs Value) (v Value)    { v.C = C.LLVMConstNSWSub(lhs.C, rhs.C); return }
843 func ConstNUWSub(lhs, rhs Value) (v Value)    { v.C = C.LLVMConstNUWSub(lhs.C, rhs.C); return }
844 func ConstFSub(lhs, rhs Value) (v Value)      { v.C = C.LLVMConstFSub(lhs.C, rhs.C); return }
845 func ConstMul(lhs, rhs Value) (v Value)       { v.C = C.LLVMConstMul(lhs.C, rhs.C); return }
846 func ConstNSWMul(lhs, rhs Value) (v Value)    { v.C = C.LLVMConstNSWMul(lhs.C, rhs.C); return }
847 func ConstNUWMul(lhs, rhs Value) (v Value)    { v.C = C.LLVMConstNUWMul(lhs.C, rhs.C); return }
848 func ConstFMul(lhs, rhs Value) (v Value)      { v.C = C.LLVMConstFMul(lhs.C, rhs.C); return }
849 func ConstUDiv(lhs, rhs Value) (v Value)      { v.C = C.LLVMConstUDiv(lhs.C, rhs.C); return }
850 func ConstSDiv(lhs, rhs Value) (v Value)      { v.C = C.LLVMConstSDiv(lhs.C, rhs.C); return }
851 func ConstExactSDiv(lhs, rhs Value) (v Value) { v.C = C.LLVMConstExactSDiv(lhs.C, rhs.C); return }
852 func ConstFDiv(lhs, rhs Value) (v Value)      { v.C = C.LLVMConstFDiv(lhs.C, rhs.C); return }
853 func ConstURem(lhs, rhs Value) (v Value)      { v.C = C.LLVMConstURem(lhs.C, rhs.C); return }
854 func ConstSRem(lhs, rhs Value) (v Value)      { v.C = C.LLVMConstSRem(lhs.C, rhs.C); return }
855 func ConstFRem(lhs, rhs Value) (v Value)      { v.C = C.LLVMConstFRem(lhs.C, rhs.C); return }
856 func ConstAnd(lhs, rhs Value) (v Value)       { v.C = C.LLVMConstAnd(lhs.C, rhs.C); return }
857 func ConstOr(lhs, rhs Value) (v Value)        { v.C = C.LLVMConstOr(lhs.C, rhs.C); return }
858 func ConstXor(lhs, rhs Value) (v Value)       { v.C = C.LLVMConstXor(lhs.C, rhs.C); return }
859
860 func ConstICmp(pred IntPredicate, lhs, rhs Value) (v Value) {
861         v.C = C.LLVMConstICmp(C.LLVMIntPredicate(pred), lhs.C, rhs.C)
862         return
863 }
864 func ConstFCmp(pred FloatPredicate, lhs, rhs Value) (v Value) {
865         v.C = C.LLVMConstFCmp(C.LLVMRealPredicate(pred), lhs.C, rhs.C)
866         return
867 }
868
869 func ConstShl(lhs, rhs Value) (v Value)  { v.C = C.LLVMConstShl(lhs.C, rhs.C); return }
870 func ConstLShr(lhs, rhs Value) (v Value) { v.C = C.LLVMConstLShr(lhs.C, rhs.C); return }
871 func ConstAShr(lhs, rhs Value) (v Value) { v.C = C.LLVMConstAShr(lhs.C, rhs.C); return }
872
873 func ConstGEP(v Value, indices []Value) (rv Value) {
874         ptr, nvals := llvmValueRefs(indices)
875         rv.C = C.LLVMConstGEP(v.C, ptr, nvals)
876         return
877 }
878 func ConstInBoundsGEP(v Value, indices []Value) (rv Value) {
879         ptr, nvals := llvmValueRefs(indices)
880         rv.C = C.LLVMConstInBoundsGEP(v.C, ptr, nvals)
881         return
882 }
883 func ConstTrunc(v Value, t Type) (rv Value)         { rv.C = C.LLVMConstTrunc(v.C, t.C); return }
884 func ConstSExt(v Value, t Type) (rv Value)          { rv.C = C.LLVMConstSExt(v.C, t.C); return }
885 func ConstZExt(v Value, t Type) (rv Value)          { rv.C = C.LLVMConstZExt(v.C, t.C); return }
886 func ConstFPTrunc(v Value, t Type) (rv Value)       { rv.C = C.LLVMConstFPTrunc(v.C, t.C); return }
887 func ConstFPExt(v Value, t Type) (rv Value)         { rv.C = C.LLVMConstFPExt(v.C, t.C); return }
888 func ConstUIToFP(v Value, t Type) (rv Value)        { rv.C = C.LLVMConstUIToFP(v.C, t.C); return }
889 func ConstSIToFP(v Value, t Type) (rv Value)        { rv.C = C.LLVMConstSIToFP(v.C, t.C); return }
890 func ConstFPToUI(v Value, t Type) (rv Value)        { rv.C = C.LLVMConstFPToUI(v.C, t.C); return }
891 func ConstFPToSI(v Value, t Type) (rv Value)        { rv.C = C.LLVMConstFPToSI(v.C, t.C); return }
892 func ConstPtrToInt(v Value, t Type) (rv Value)      { rv.C = C.LLVMConstPtrToInt(v.C, t.C); return }
893 func ConstIntToPtr(v Value, t Type) (rv Value)      { rv.C = C.LLVMConstIntToPtr(v.C, t.C); return }
894 func ConstBitCast(v Value, t Type) (rv Value)       { rv.C = C.LLVMConstBitCast(v.C, t.C); return }
895 func ConstZExtOrBitCast(v Value, t Type) (rv Value) { rv.C = C.LLVMConstZExtOrBitCast(v.C, t.C); return }
896 func ConstSExtOrBitCast(v Value, t Type) (rv Value) { rv.C = C.LLVMConstSExtOrBitCast(v.C, t.C); return }
897 func ConstTruncOrBitCast(v Value, t Type) (rv Value) {
898         rv.C = C.LLVMConstTruncOrBitCast(v.C, t.C)
899         return
900 }
901 func ConstPointerCast(v Value, t Type) (rv Value) { rv.C = C.LLVMConstPointerCast(v.C, t.C); return }
902 func ConstIntCast(v Value, t Type, signed bool) (rv Value) {
903         rv.C = C.LLVMConstIntCast(v.C, t.C, boolToLLVMBool(signed))
904         return
905 }
906 func ConstFPCast(v Value, t Type) (rv Value) { rv.C = C.LLVMConstFPCast(v.C, t.C); return }
907 func ConstSelect(cond, iftrue, iffalse Value) (rv Value) {
908         rv.C = C.LLVMConstSelect(cond.C, iftrue.C, iffalse.C)
909         return
910 }
911 func ConstExtractElement(vec, i Value) (rv Value) {
912         rv.C = C.LLVMConstExtractElement(vec.C, i.C)
913         return
914 }
915 func ConstInsertElement(vec, elem, i Value) (rv Value) {
916         rv.C = C.LLVMConstInsertElement(vec.C, elem.C, i.C)
917         return
918 }
919 func ConstShuffleVector(veca, vecb, mask Value) (rv Value) {
920         rv.C = C.LLVMConstShuffleVector(veca.C, vecb.C, mask.C)
921         return
922 }
923
924 //TODO
925 //LLVMValueRef LLVMConstExtractValue(LLVMValueRef AggConstant, unsigned *IdxList,
926 //                                   unsigned NumIdx);
927
928 func ConstExtractValue(agg Value, indices []uint32) (rv Value) {
929         n := len(indices)
930         if n == 0 {
931                 panic("one or more indices are required")
932         }
933         ptr := (*C.unsigned)(&indices[0])
934         rv.C = C.LLVMConstExtractValue(agg.C, ptr, C.unsigned(n))
935         return
936 }
937
938 func ConstInsertValue(agg, val Value, indices []uint32) (rv Value) {
939         n := len(indices)
940         if n == 0 {
941                 panic("one or more indices are required")
942         }
943         ptr := (*C.unsigned)(&indices[0])
944         rv.C = C.LLVMConstInsertValue(agg.C, val.C, ptr, C.unsigned(n))
945         return
946 }
947
948 func BlockAddress(f Value, bb BasicBlock) (v Value) {
949         v.C = C.LLVMBlockAddress(f.C, bb.C)
950         return
951 }
952
953 // Operations on global variables, functions, and aliases (globals)
954 func (v Value) GlobalParent() (m Module) { m.C = C.LLVMGetGlobalParent(v.C); return }
955 func (v Value) IsDeclaration() bool      { return C.LLVMIsDeclaration(v.C) != 0 }
956 func (v Value) Linkage() Linkage         { return Linkage(C.LLVMGetLinkage(v.C)) }
957 func (v Value) SetLinkage(l Linkage)     { C.LLVMSetLinkage(v.C, C.LLVMLinkage(l)) }
958 func (v Value) Section() string          { return C.GoString(C.LLVMGetSection(v.C)) }
959 func (v Value) SetSection(str string) {
960         cstr := C.CString(str)
961         defer C.free(unsafe.Pointer(cstr))
962         C.LLVMSetSection(v.C, cstr)
963 }
964 func (v Value) Visibility() Visibility      { return Visibility(C.LLVMGetVisibility(v.C)) }
965 func (v Value) SetVisibility(vi Visibility) { C.LLVMSetVisibility(v.C, C.LLVMVisibility(vi)) }
966 func (v Value) Alignment() int              { return int(C.LLVMGetAlignment(v.C)) }
967 func (v Value) SetAlignment(a int)          { C.LLVMSetAlignment(v.C, C.unsigned(a)) }
968 func (v Value) SetUnnamedAddr(ua bool)      { C.LLVMSetUnnamedAddr(v.C, boolToLLVMBool(ua)) }
969
970 // Operations on global variables
971 func AddGlobal(m Module, t Type, name string) (v Value) {
972         cname := C.CString(name)
973         defer C.free(unsafe.Pointer(cname))
974         v.C = C.LLVMAddGlobal(m.C, t.C, cname)
975         return
976 }
977 func AddGlobalInAddressSpace(m Module, t Type, name string, addressSpace int) (v Value) {
978         cname := C.CString(name)
979         defer C.free(unsafe.Pointer(cname))
980         v.C = C.LLVMAddGlobalInAddressSpace(m.C, t.C, cname, C.unsigned(addressSpace))
981         return
982 }
983 func (m Module) NamedGlobal(name string) (v Value) {
984         cname := C.CString(name)
985         defer C.free(unsafe.Pointer(cname))
986         v.C = C.LLVMGetNamedGlobal(m.C, cname)
987         return
988 }
989
990 func (m Module) FirstGlobal() (v Value)   { v.C = C.LLVMGetFirstGlobal(m.C); return }
991 func (m Module) LastGlobal() (v Value)    { v.C = C.LLVMGetLastGlobal(m.C); return }
992 func NextGlobal(v Value) (rv Value)       { rv.C = C.LLVMGetNextGlobal(v.C); return }
993 func PrevGlobal(v Value) (rv Value)       { rv.C = C.LLVMGetPreviousGlobal(v.C); return }
994 func (v Value) EraseFromParentAsGlobal()  { C.LLVMDeleteGlobal(v.C) }
995 func (v Value) Initializer() (rv Value)   { rv.C = C.LLVMGetInitializer(v.C); return }
996 func (v Value) SetInitializer(cv Value)   { C.LLVMSetInitializer(v.C, cv.C) }
997 func (v Value) IsThreadLocal() bool       { return C.LLVMIsThreadLocal(v.C) != 0 }
998 func (v Value) SetThreadLocal(tl bool)    { C.LLVMSetThreadLocal(v.C, boolToLLVMBool(tl)) }
999 func (v Value) IsGlobalConstant() bool    { return C.LLVMIsGlobalConstant(v.C) != 0 }
1000 func (v Value) SetGlobalConstant(gc bool) { C.LLVMSetGlobalConstant(v.C, boolToLLVMBool(gc)) }
1001
1002 // Operations on aliases
1003 func AddAlias(m Module, t Type, aliasee Value, name string) (v Value) {
1004         cname := C.CString(name)
1005         defer C.free(unsafe.Pointer(cname))
1006         v.C = C.LLVMAddAlias(m.C, t.C, aliasee.C, cname)
1007         return
1008 }
1009
1010 // Operations on functions
1011 func AddFunction(m Module, name string, ft Type) (v Value) {
1012         cname := C.CString(name)
1013         defer C.free(unsafe.Pointer(cname))
1014         v.C = C.LLVMAddFunction(m.C, cname, ft.C)
1015         return
1016 }
1017
1018 func (m Module) NamedFunction(name string) (v Value) {
1019         cname := C.CString(name)
1020         defer C.free(unsafe.Pointer(cname))
1021         v.C = C.LLVMGetNamedFunction(m.C, cname)
1022         return
1023 }
1024
1025 func (m Module) FirstFunction() (v Value)  { v.C = C.LLVMGetFirstFunction(m.C); return }
1026 func (m Module) LastFunction() (v Value)   { v.C = C.LLVMGetLastFunction(m.C); return }
1027 func NextFunction(v Value) (rv Value)      { rv.C = C.LLVMGetNextFunction(v.C); return }
1028 func PrevFunction(v Value) (rv Value)      { rv.C = C.LLVMGetPreviousFunction(v.C); return }
1029 func (v Value) EraseFromParentAsFunction() { C.LLVMDeleteFunction(v.C) }
1030 func (v Value) IntrinsicID() int           { return int(C.LLVMGetIntrinsicID(v.C)) }
1031 func (v Value) FunctionCallConv() CallConv {
1032         return CallConv(C.LLVMCallConv(C.LLVMGetFunctionCallConv(v.C)))
1033 }
1034 func (v Value) SetFunctionCallConv(cc CallConv) { C.LLVMSetFunctionCallConv(v.C, C.unsigned(cc)) }
1035 func (v Value) GC() string                      { return C.GoString(C.LLVMGetGC(v.C)) }
1036 func (v Value) SetGC(name string) {
1037         cname := C.CString(name)
1038         defer C.free(unsafe.Pointer(cname))
1039         C.LLVMSetGC(v.C, cname)
1040 }
1041 func (v Value) AddFunctionAttr(a Attribute)    { C.LLVMAddFunctionAttr2(v.C, C.uint64_t(a)) }
1042 func (v Value) FunctionAttr() Attribute        { return Attribute(C.LLVMGetFunctionAttr2(v.C)) }
1043 func (v Value) RemoveFunctionAttr(a Attribute) { C.LLVMRemoveFunctionAttr2(v.C, C.uint64_t(a)) }
1044 func (v Value) AddTargetDependentFunctionAttr(attr, value string) {
1045         cattr := C.CString(attr)
1046         defer C.free(unsafe.Pointer(cattr))
1047         cvalue := C.CString(value)
1048         defer C.free(unsafe.Pointer(cvalue))
1049         C.LLVMAddTargetDependentFunctionAttr(v.C, cattr, cvalue)
1050 }
1051
1052 // Operations on parameters
1053 func (v Value) ParamsCount() int { return int(C.LLVMCountParams(v.C)) }
1054 func (v Value) Params() []Value {
1055         out := make([]Value, v.ParamsCount())
1056         if len(out) > 0 {
1057                 C.LLVMGetParams(v.C, llvmValueRefPtr(&out[0]))
1058         }
1059         return out
1060 }
1061 func (v Value) Param(i int) (rv Value)  { rv.C = C.LLVMGetParam(v.C, C.unsigned(i)); return }
1062 func (v Value) ParamParent() (rv Value) { rv.C = C.LLVMGetParamParent(v.C); return }
1063 func (v Value) FirstParam() (rv Value)  { rv.C = C.LLVMGetFirstParam(v.C); return }
1064 func (v Value) LastParam() (rv Value)   { rv.C = C.LLVMGetLastParam(v.C); return }
1065 func NextParam(v Value) (rv Value)      { rv.C = C.LLVMGetNextParam(v.C); return }
1066 func PrevParam(v Value) (rv Value)      { rv.C = C.LLVMGetPreviousParam(v.C); return }
1067 func (v Value) AddAttribute(a Attribute) {
1068         if a >= 1<<32 {
1069                 panic("attribute value currently unsupported")
1070         }
1071         C.LLVMAddAttribute(v.C, C.LLVMAttribute(a))
1072 }
1073 func (v Value) RemoveAttribute(a Attribute) {
1074         if a >= 1<<32 {
1075                 panic("attribute value currently unsupported")
1076         }
1077         C.LLVMRemoveAttribute(v.C, C.LLVMAttribute(a))
1078 }
1079 func (v Value) Attribute() Attribute        { return Attribute(C.LLVMGetAttribute(v.C)) }
1080 func (v Value) SetParamAlignment(align int) { C.LLVMSetParamAlignment(v.C, C.unsigned(align)) }
1081
1082 // Operations on basic blocks
1083 func (bb BasicBlock) AsValue() (v Value)      { v.C = C.LLVMBasicBlockAsValue(bb.C); return }
1084 func (v Value) IsBasicBlock() bool            { return C.LLVMValueIsBasicBlock(v.C) != 0 }
1085 func (v Value) AsBasicBlock() (bb BasicBlock) { bb.C = C.LLVMValueAsBasicBlock(v.C); return }
1086 func (bb BasicBlock) Parent() (v Value)       { v.C = C.LLVMGetBasicBlockParent(bb.C); return }
1087 func (v Value) BasicBlocksCount() int         { return int(C.LLVMCountBasicBlocks(v.C)) }
1088 func (v Value) BasicBlocks() []BasicBlock {
1089         out := make([]BasicBlock, v.BasicBlocksCount())
1090         C.LLVMGetBasicBlocks(v.C, llvmBasicBlockRefPtr(&out[0]))
1091         return out
1092 }
1093 func (v Value) FirstBasicBlock() (bb BasicBlock)    { bb.C = C.LLVMGetFirstBasicBlock(v.C); return }
1094 func (v Value) LastBasicBlock() (bb BasicBlock)     { bb.C = C.LLVMGetLastBasicBlock(v.C); return }
1095 func NextBasicBlock(bb BasicBlock) (rbb BasicBlock) { rbb.C = C.LLVMGetNextBasicBlock(bb.C); return }
1096 func PrevBasicBlock(bb BasicBlock) (rbb BasicBlock) { rbb.C = C.LLVMGetPreviousBasicBlock(bb.C); return }
1097 func (v Value) EntryBasicBlock() (bb BasicBlock)    { bb.C = C.LLVMGetEntryBasicBlock(v.C); return }
1098 func (c Context) AddBasicBlock(f Value, name string) (bb BasicBlock) {
1099         cname := C.CString(name)
1100         defer C.free(unsafe.Pointer(cname))
1101         bb.C = C.LLVMAppendBasicBlockInContext(c.C, f.C, cname)
1102         return
1103 }
1104 func (c Context) InsertBasicBlock(ref BasicBlock, name string) (bb BasicBlock) {
1105         cname := C.CString(name)
1106         defer C.free(unsafe.Pointer(cname))
1107         bb.C = C.LLVMInsertBasicBlockInContext(c.C, ref.C, cname)
1108         return
1109 }
1110 func AddBasicBlock(f Value, name string) (bb BasicBlock) {
1111         cname := C.CString(name)
1112         defer C.free(unsafe.Pointer(cname))
1113         bb.C = C.LLVMAppendBasicBlock(f.C, cname)
1114         return
1115 }
1116 func InsertBasicBlock(ref BasicBlock, name string) (bb BasicBlock) {
1117         cname := C.CString(name)
1118         defer C.free(unsafe.Pointer(cname))
1119         bb.C = C.LLVMInsertBasicBlock(ref.C, cname)
1120         return
1121 }
1122 func (bb BasicBlock) EraseFromParent()          { C.LLVMDeleteBasicBlock(bb.C) }
1123 func (bb BasicBlock) MoveBefore(pos BasicBlock) { C.LLVMMoveBasicBlockBefore(bb.C, pos.C) }
1124 func (bb BasicBlock) MoveAfter(pos BasicBlock)  { C.LLVMMoveBasicBlockAfter(bb.C, pos.C) }
1125
1126 // Operations on instructions
1127 func (v Value) InstructionParent() (bb BasicBlock) { bb.C = C.LLVMGetInstructionParent(v.C); return }
1128 func (bb BasicBlock) FirstInstruction() (v Value)  { v.C = C.LLVMGetFirstInstruction(bb.C); return }
1129 func (bb BasicBlock) LastInstruction() (v Value)   { v.C = C.LLVMGetLastInstruction(bb.C); return }
1130 func NextInstruction(v Value) (rv Value)           { rv.C = C.LLVMGetNextInstruction(v.C); return }
1131 func PrevInstruction(v Value) (rv Value)           { rv.C = C.LLVMGetPreviousInstruction(v.C); return }
1132
1133 // Operations on call sites
1134 func (v Value) SetInstructionCallConv(cc CallConv) {
1135         C.LLVMSetInstructionCallConv(v.C, C.unsigned(cc))
1136 }
1137 func (v Value) InstructionCallConv() CallConv {
1138         return CallConv(C.LLVMCallConv(C.LLVMGetInstructionCallConv(v.C)))
1139 }
1140 func (v Value) AddInstrAttribute(i int, a Attribute) {
1141         if a >= 1<<32 {
1142                 panic("attribute value currently unsupported")
1143         }
1144         C.LLVMAddInstrAttribute(v.C, C.unsigned(i), C.LLVMAttribute(a))
1145 }
1146 func (v Value) RemoveInstrAttribute(i int, a Attribute) {
1147         if a >= 1<<32 {
1148                 panic("attribute value currently unsupported")
1149         }
1150         C.LLVMRemoveInstrAttribute(v.C, C.unsigned(i), C.LLVMAttribute(a))
1151 }
1152 func (v Value) SetInstrParamAlignment(i int, align int) {
1153         C.LLVMSetInstrParamAlignment(v.C, C.unsigned(i), C.unsigned(align))
1154 }
1155
1156 // Operations on call instructions (only)
1157 func (v Value) IsTailCall() bool    { return C.LLVMIsTailCall(v.C) != 0 }
1158 func (v Value) SetTailCall(is bool) { C.LLVMSetTailCall(v.C, boolToLLVMBool(is)) }
1159
1160 // Operations on phi nodes
1161 func (v Value) AddIncoming(vals []Value, blocks []BasicBlock) {
1162         ptr, nvals := llvmValueRefs(vals)
1163         C.LLVMAddIncoming(v.C, ptr, llvmBasicBlockRefPtr(&blocks[0]), nvals)
1164 }
1165 func (v Value) IncomingCount() int { return int(C.LLVMCountIncoming(v.C)) }
1166 func (v Value) IncomingValue(i int) (rv Value) {
1167         rv.C = C.LLVMGetIncomingValue(v.C, C.unsigned(i))
1168         return
1169 }
1170 func (v Value) IncomingBlock(i int) (bb BasicBlock) {
1171         bb.C = C.LLVMGetIncomingBlock(v.C, C.unsigned(i))
1172         return
1173 }
1174
1175 //-------------------------------------------------------------------------
1176 // llvm.Builder
1177 //-------------------------------------------------------------------------
1178
1179 // An instruction builder represents a point within a basic block, and is the
1180 // exclusive means of building instructions using the C interface.
1181
1182 func (c Context) NewBuilder() (b Builder) { b.C = C.LLVMCreateBuilderInContext(c.C); return }
1183 func NewBuilder() (b Builder)             { b.C = C.LLVMCreateBuilder(); return }
1184 func (b Builder) SetInsertPoint(block BasicBlock, instr Value) {
1185         C.LLVMPositionBuilder(b.C, block.C, instr.C)
1186 }
1187 func (b Builder) SetInsertPointBefore(instr Value)     { C.LLVMPositionBuilderBefore(b.C, instr.C) }
1188 func (b Builder) SetInsertPointAtEnd(block BasicBlock) { C.LLVMPositionBuilderAtEnd(b.C, block.C) }
1189 func (b Builder) GetInsertBlock() (bb BasicBlock)      { bb.C = C.LLVMGetInsertBlock(b.C); return }
1190 func (b Builder) ClearInsertionPoint()                 { C.LLVMClearInsertionPosition(b.C) }
1191 func (b Builder) Insert(instr Value)                   { C.LLVMInsertIntoBuilder(b.C, instr.C) }
1192 func (b Builder) InsertWithName(instr Value, name string) {
1193         cname := C.CString(name)
1194         defer C.free(unsafe.Pointer(cname))
1195         C.LLVMInsertIntoBuilderWithName(b.C, instr.C, cname)
1196 }
1197 func (b Builder) Dispose() { C.LLVMDisposeBuilder(b.C) }
1198
1199 // Metadata
1200 func (b Builder) SetCurrentDebugLocation(line, col uint, scope, inlinedAt Metadata) {
1201         C.LLVMSetCurrentDebugLocation2(b.C, C.unsigned(line), C.unsigned(col), scope.C, inlinedAt.C)
1202 }
1203 func (b Builder) SetInstDebugLocation(v Value)    { C.LLVMSetInstDebugLocation(b.C, v.C) }
1204 func (b Builder) InsertDeclare(module Module, storage Value, md Value) Value {
1205         f := module.NamedFunction("llvm.dbg.declare")
1206         if f.IsNil() {
1207                 ftyp := FunctionType(VoidType(), []Type{storage.Type(), md.Type()}, false)
1208                 f = AddFunction(module, "llvm.dbg.declare", ftyp)
1209         }
1210         return b.CreateCall(f, []Value{storage, md}, "")
1211 }
1212
1213 // Terminators
1214 func (b Builder) CreateRetVoid() (rv Value)    { rv.C = C.LLVMBuildRetVoid(b.C); return }
1215 func (b Builder) CreateRet(v Value) (rv Value) { rv.C = C.LLVMBuildRet(b.C, v.C); return }
1216 func (b Builder) CreateAggregateRet(vs []Value) (rv Value) {
1217         ptr, nvals := llvmValueRefs(vs)
1218         rv.C = C.LLVMBuildAggregateRet(b.C, ptr, nvals)
1219         return
1220 }
1221 func (b Builder) CreateBr(bb BasicBlock) (rv Value) { rv.C = C.LLVMBuildBr(b.C, bb.C); return }
1222 func (b Builder) CreateCondBr(ifv Value, thenb, elseb BasicBlock) (rv Value) {
1223         rv.C = C.LLVMBuildCondBr(b.C, ifv.C, thenb.C, elseb.C)
1224         return
1225 }
1226 func (b Builder) CreateSwitch(v Value, elseb BasicBlock, numCases int) (rv Value) {
1227         rv.C = C.LLVMBuildSwitch(b.C, v.C, elseb.C, C.unsigned(numCases))
1228         return
1229 }
1230 func (b Builder) CreateIndirectBr(addr Value, numDests int) (rv Value) {
1231         rv.C = C.LLVMBuildIndirectBr(b.C, addr.C, C.unsigned(numDests))
1232         return
1233 }
1234 func (b Builder) CreateInvoke(fn Value, args []Value, then, catch BasicBlock, name string) (rv Value) {
1235         cname := C.CString(name)
1236         defer C.free(unsafe.Pointer(cname))
1237         ptr, nvals := llvmValueRefs(args)
1238         rv.C = C.LLVMBuildInvoke(b.C, fn.C, ptr, nvals, then.C, catch.C, cname)
1239         return
1240 }
1241 func (b Builder) CreateUnreachable() (rv Value) { rv.C = C.LLVMBuildUnreachable(b.C); return }
1242
1243 // Add a case to the switch instruction
1244 func (v Value) AddCase(on Value, dest BasicBlock) { C.LLVMAddCase(v.C, on.C, dest.C) }
1245
1246 // Add a destination to the indirectbr instruction
1247 func (v Value) AddDest(dest BasicBlock) { C.LLVMAddDestination(v.C, dest.C) }
1248
1249 // Arithmetic
1250 func (b Builder) CreateAdd(lhs, rhs Value, name string) (v Value) {
1251         cname := C.CString(name)
1252         defer C.free(unsafe.Pointer(cname))
1253         v.C = C.LLVMBuildAdd(b.C, lhs.C, rhs.C, cname)
1254         return
1255 }
1256 func (b Builder) CreateNSWAdd(lhs, rhs Value, name string) (v Value) {
1257         cname := C.CString(name)
1258         defer C.free(unsafe.Pointer(cname))
1259         v.C = C.LLVMBuildNSWAdd(b.C, lhs.C, rhs.C, cname)
1260         return
1261 }
1262 func (b Builder) CreateNUWAdd(lhs, rhs Value, name string) (v Value) {
1263         cname := C.CString(name)
1264         defer C.free(unsafe.Pointer(cname))
1265         v.C = C.LLVMBuildNUWAdd(b.C, lhs.C, rhs.C, cname)
1266         return
1267 }
1268 func (b Builder) CreateFAdd(lhs, rhs Value, name string) (v Value) {
1269         cname := C.CString(name)
1270         defer C.free(unsafe.Pointer(cname))
1271         v.C = C.LLVMBuildFAdd(b.C, lhs.C, rhs.C, cname)
1272         return
1273 }
1274 func (b Builder) CreateSub(lhs, rhs Value, name string) (v Value) {
1275         cname := C.CString(name)
1276         defer C.free(unsafe.Pointer(cname))
1277         v.C = C.LLVMBuildSub(b.C, lhs.C, rhs.C, cname)
1278         return
1279 }
1280 func (b Builder) CreateNSWSub(lhs, rhs Value, name string) (v Value) {
1281         cname := C.CString(name)
1282         defer C.free(unsafe.Pointer(cname))
1283         v.C = C.LLVMBuildNSWSub(b.C, lhs.C, rhs.C, cname)
1284         return
1285 }
1286 func (b Builder) CreateNUWSub(lhs, rhs Value, name string) (v Value) {
1287         cname := C.CString(name)
1288         defer C.free(unsafe.Pointer(cname))
1289         v.C = C.LLVMBuildNUWSub(b.C, lhs.C, rhs.C, cname)
1290         return
1291 }
1292 func (b Builder) CreateFSub(lhs, rhs Value, name string) (v Value) {
1293         cname := C.CString(name)
1294         v.C = C.LLVMBuildFSub(b.C, lhs.C, rhs.C, cname)
1295         C.free(unsafe.Pointer(cname))
1296         return
1297 }
1298 func (b Builder) CreateMul(lhs, rhs Value, name string) (v Value) {
1299         cname := C.CString(name)
1300         defer C.free(unsafe.Pointer(cname))
1301         v.C = C.LLVMBuildMul(b.C, lhs.C, rhs.C, cname)
1302         return
1303 }
1304 func (b Builder) CreateNSWMul(lhs, rhs Value, name string) (v Value) {
1305         cname := C.CString(name)
1306         defer C.free(unsafe.Pointer(cname))
1307         v.C = C.LLVMBuildNSWMul(b.C, lhs.C, rhs.C, cname)
1308         return
1309 }
1310 func (b Builder) CreateNUWMul(lhs, rhs Value, name string) (v Value) {
1311         cname := C.CString(name)
1312         defer C.free(unsafe.Pointer(cname))
1313         v.C = C.LLVMBuildNUWMul(b.C, lhs.C, rhs.C, cname)
1314         return
1315 }
1316 func (b Builder) CreateFMul(lhs, rhs Value, name string) (v Value) {
1317         cname := C.CString(name)
1318         defer C.free(unsafe.Pointer(cname))
1319         v.C = C.LLVMBuildFMul(b.C, lhs.C, rhs.C, cname)
1320         return
1321 }
1322 func (b Builder) CreateUDiv(lhs, rhs Value, name string) (v Value) {
1323         cname := C.CString(name)
1324         defer C.free(unsafe.Pointer(cname))
1325         v.C = C.LLVMBuildUDiv(b.C, lhs.C, rhs.C, cname)
1326         return
1327 }
1328 func (b Builder) CreateSDiv(lhs, rhs Value, name string) (v Value) {
1329         cname := C.CString(name)
1330         defer C.free(unsafe.Pointer(cname))
1331         v.C = C.LLVMBuildSDiv(b.C, lhs.C, rhs.C, cname)
1332         return
1333 }
1334 func (b Builder) CreateExactSDiv(lhs, rhs Value, name string) (v Value) {
1335         cname := C.CString(name)
1336         defer C.free(unsafe.Pointer(cname))
1337         v.C = C.LLVMBuildExactSDiv(b.C, lhs.C, rhs.C, cname)
1338         return
1339 }
1340 func (b Builder) CreateFDiv(lhs, rhs Value, name string) (v Value) {
1341         cname := C.CString(name)
1342         defer C.free(unsafe.Pointer(cname))
1343         v.C = C.LLVMBuildFDiv(b.C, lhs.C, rhs.C, cname)
1344         return
1345 }
1346 func (b Builder) CreateURem(lhs, rhs Value, name string) (v Value) {
1347         cname := C.CString(name)
1348         defer C.free(unsafe.Pointer(cname))
1349         v.C = C.LLVMBuildURem(b.C, lhs.C, rhs.C, cname)
1350         return
1351 }
1352 func (b Builder) CreateSRem(lhs, rhs Value, name string) (v Value) {
1353         cname := C.CString(name)
1354         defer C.free(unsafe.Pointer(cname))
1355         v.C = C.LLVMBuildSRem(b.C, lhs.C, rhs.C, cname)
1356         return
1357 }
1358 func (b Builder) CreateFRem(lhs, rhs Value, name string) (v Value) {
1359         cname := C.CString(name)
1360         defer C.free(unsafe.Pointer(cname))
1361         v.C = C.LLVMBuildFRem(b.C, lhs.C, rhs.C, cname)
1362         return
1363 }
1364 func (b Builder) CreateShl(lhs, rhs Value, name string) (v Value) {
1365         cname := C.CString(name)
1366         defer C.free(unsafe.Pointer(cname))
1367         v.C = C.LLVMBuildShl(b.C, lhs.C, rhs.C, cname)
1368         return
1369 }
1370 func (b Builder) CreateLShr(lhs, rhs Value, name string) (v Value) {
1371         cname := C.CString(name)
1372         defer C.free(unsafe.Pointer(cname))
1373         v.C = C.LLVMBuildLShr(b.C, lhs.C, rhs.C, cname)
1374         return
1375 }
1376 func (b Builder) CreateAShr(lhs, rhs Value, name string) (v Value) {
1377         cname := C.CString(name)
1378         defer C.free(unsafe.Pointer(cname))
1379         v.C = C.LLVMBuildAShr(b.C, lhs.C, rhs.C, cname)
1380         return
1381 }
1382 func (b Builder) CreateAnd(lhs, rhs Value, name string) (v Value) {
1383         cname := C.CString(name)
1384         defer C.free(unsafe.Pointer(cname))
1385         v.C = C.LLVMBuildAnd(b.C, lhs.C, rhs.C, cname)
1386         return
1387 }
1388 func (b Builder) CreateOr(lhs, rhs Value, name string) (v Value) {
1389         cname := C.CString(name)
1390         defer C.free(unsafe.Pointer(cname))
1391         v.C = C.LLVMBuildOr(b.C, lhs.C, rhs.C, cname)
1392         return
1393 }
1394 func (b Builder) CreateXor(lhs, rhs Value, name string) (v Value) {
1395         cname := C.CString(name)
1396         defer C.free(unsafe.Pointer(cname))
1397         v.C = C.LLVMBuildXor(b.C, lhs.C, rhs.C, cname)
1398         return
1399 }
1400 func (b Builder) CreateBinOp(op Opcode, lhs, rhs Value, name string) (v Value) {
1401         cname := C.CString(name)
1402         defer C.free(unsafe.Pointer(cname))
1403         v.C = C.LLVMBuildBinOp(b.C, C.LLVMOpcode(op), lhs.C, rhs.C, cname)
1404         return
1405 }
1406 func (b Builder) CreateNeg(v Value, name string) (rv Value) {
1407         cname := C.CString(name)
1408         defer C.free(unsafe.Pointer(cname))
1409         rv.C = C.LLVMBuildNeg(b.C, v.C, cname)
1410         return
1411 }
1412 func (b Builder) CreateNSWNeg(v Value, name string) (rv Value) {
1413         cname := C.CString(name)
1414         defer C.free(unsafe.Pointer(cname))
1415         rv.C = C.LLVMBuildNSWNeg(b.C, v.C, cname)
1416         return
1417 }
1418 func (b Builder) CreateNUWNeg(v Value, name string) (rv Value) {
1419         cname := C.CString(name)
1420         defer C.free(unsafe.Pointer(cname))
1421         rv.C = C.LLVMBuildNUWNeg(b.C, v.C, cname)
1422         return
1423 }
1424 func (b Builder) CreateFNeg(v Value, name string) (rv Value) {
1425         cname := C.CString(name)
1426         defer C.free(unsafe.Pointer(cname))
1427         rv.C = C.LLVMBuildFNeg(b.C, v.C, cname)
1428         return
1429 }
1430 func (b Builder) CreateNot(v Value, name string) (rv Value) {
1431         cname := C.CString(name)
1432         defer C.free(unsafe.Pointer(cname))
1433         rv.C = C.LLVMBuildNot(b.C, v.C, cname)
1434         return
1435 }
1436
1437 // Memory
1438
1439 func (b Builder) CreateMalloc(t Type, name string) (v Value) {
1440         cname := C.CString(name)
1441         defer C.free(unsafe.Pointer(cname))
1442         v.C = C.LLVMBuildMalloc(b.C, t.C, cname)
1443         return
1444 }
1445 func (b Builder) CreateArrayMalloc(t Type, val Value, name string) (v Value) {
1446         cname := C.CString(name)
1447         defer C.free(unsafe.Pointer(cname))
1448         v.C = C.LLVMBuildArrayMalloc(b.C, t.C, val.C, cname)
1449         return
1450 }
1451 func (b Builder) CreateAlloca(t Type, name string) (v Value) {
1452         cname := C.CString(name)
1453         defer C.free(unsafe.Pointer(cname))
1454         v.C = C.LLVMBuildAlloca(b.C, t.C, cname)
1455         return
1456 }
1457 func (b Builder) CreateArrayAlloca(t Type, val Value, name string) (v Value) {
1458         cname := C.CString(name)
1459         defer C.free(unsafe.Pointer(cname))
1460         v.C = C.LLVMBuildArrayAlloca(b.C, t.C, val.C, cname)
1461         return
1462 }
1463 func (b Builder) CreateFree(p Value) (v Value) {
1464         v.C = C.LLVMBuildFree(b.C, p.C)
1465         return
1466 }
1467 func (b Builder) CreateLoad(p Value, name string) (v Value) {
1468         cname := C.CString(name)
1469         defer C.free(unsafe.Pointer(cname))
1470         v.C = C.LLVMBuildLoad(b.C, p.C, cname)
1471         return
1472 }
1473 func (b Builder) CreateStore(val Value, p Value) (v Value) {
1474         v.C = C.LLVMBuildStore(b.C, val.C, p.C)
1475         return
1476 }
1477 func (b Builder) CreateGEP(p Value, indices []Value, name string) (v Value) {
1478         cname := C.CString(name)
1479         defer C.free(unsafe.Pointer(cname))
1480         ptr, nvals := llvmValueRefs(indices)
1481         v.C = C.LLVMBuildGEP(b.C, p.C, ptr, nvals, cname)
1482         return
1483 }
1484 func (b Builder) CreateInBoundsGEP(p Value, indices []Value, name string) (v Value) {
1485         cname := C.CString(name)
1486         defer C.free(unsafe.Pointer(cname))
1487         ptr, nvals := llvmValueRefs(indices)
1488         v.C = C.LLVMBuildInBoundsGEP(b.C, p.C, ptr, nvals, cname)
1489         return
1490 }
1491 func (b Builder) CreateStructGEP(p Value, i int, name string) (v Value) {
1492         cname := C.CString(name)
1493         defer C.free(unsafe.Pointer(cname))
1494         v.C = C.LLVMBuildStructGEP(b.C, p.C, C.unsigned(i), cname)
1495         return
1496 }
1497 func (b Builder) CreateGlobalString(str, name string) (v Value) {
1498         cstr := C.CString(str)
1499         defer C.free(unsafe.Pointer(cstr))
1500         cname := C.CString(name)
1501         defer C.free(unsafe.Pointer(cname))
1502         v.C = C.LLVMBuildGlobalString(b.C, cstr, cname)
1503         return
1504 }
1505 func (b Builder) CreateGlobalStringPtr(str, name string) (v Value) {
1506         cstr := C.CString(str)
1507         defer C.free(unsafe.Pointer(cstr))
1508         cname := C.CString(name)
1509         defer C.free(unsafe.Pointer(cname))
1510         v.C = C.LLVMBuildGlobalStringPtr(b.C, cstr, cname)
1511         return
1512 }
1513
1514 // Casts
1515 func (b Builder) CreateTrunc(val Value, t Type, name string) (v Value) {
1516         cname := C.CString(name)
1517         defer C.free(unsafe.Pointer(cname))
1518         v.C = C.LLVMBuildTrunc(b.C, val.C, t.C, cname)
1519         return
1520 }
1521 func (b Builder) CreateZExt(val Value, t Type, name string) (v Value) {
1522         cname := C.CString(name)
1523         defer C.free(unsafe.Pointer(cname))
1524         v.C = C.LLVMBuildZExt(b.C, val.C, t.C, cname)
1525         return
1526 }
1527 func (b Builder) CreateSExt(val Value, t Type, name string) (v Value) {
1528         cname := C.CString(name)
1529         defer C.free(unsafe.Pointer(cname))
1530         v.C = C.LLVMBuildSExt(b.C, val.C, t.C, cname)
1531         return
1532 }
1533 func (b Builder) CreateFPToUI(val Value, t Type, name string) (v Value) {
1534         cname := C.CString(name)
1535         defer C.free(unsafe.Pointer(cname))
1536         v.C = C.LLVMBuildFPToUI(b.C, val.C, t.C, cname)
1537         return
1538 }
1539 func (b Builder) CreateFPToSI(val Value, t Type, name string) (v Value) {
1540         cname := C.CString(name)
1541         defer C.free(unsafe.Pointer(cname))
1542         v.C = C.LLVMBuildFPToSI(b.C, val.C, t.C, cname)
1543         return
1544 }
1545 func (b Builder) CreateUIToFP(val Value, t Type, name string) (v Value) {
1546         cname := C.CString(name)
1547         defer C.free(unsafe.Pointer(cname))
1548         v.C = C.LLVMBuildUIToFP(b.C, val.C, t.C, cname)
1549         return
1550 }
1551 func (b Builder) CreateSIToFP(val Value, t Type, name string) (v Value) {
1552         cname := C.CString(name)
1553         defer C.free(unsafe.Pointer(cname))
1554         v.C = C.LLVMBuildSIToFP(b.C, val.C, t.C, cname)
1555         return
1556 }
1557 func (b Builder) CreateFPTrunc(val Value, t Type, name string) (v Value) {
1558         cname := C.CString(name)
1559         defer C.free(unsafe.Pointer(cname))
1560         v.C = C.LLVMBuildFPTrunc(b.C, val.C, t.C, cname)
1561         return
1562 }
1563 func (b Builder) CreateFPExt(val Value, t Type, name string) (v Value) {
1564         cname := C.CString(name)
1565         defer C.free(unsafe.Pointer(cname))
1566         v.C = C.LLVMBuildFPExt(b.C, val.C, t.C, cname)
1567         return
1568 }
1569 func (b Builder) CreatePtrToInt(val Value, t Type, name string) (v Value) {
1570         cname := C.CString(name)
1571         defer C.free(unsafe.Pointer(cname))
1572         v.C = C.LLVMBuildPtrToInt(b.C, val.C, t.C, cname)
1573         return
1574 }
1575 func (b Builder) CreateIntToPtr(val Value, t Type, name string) (v Value) {
1576         cname := C.CString(name)
1577         defer C.free(unsafe.Pointer(cname))
1578         v.C = C.LLVMBuildIntToPtr(b.C, val.C, t.C, cname)
1579         return
1580 }
1581 func (b Builder) CreateBitCast(val Value, t Type, name string) (v Value) {
1582         cname := C.CString(name)
1583         defer C.free(unsafe.Pointer(cname))
1584         v.C = C.LLVMBuildBitCast(b.C, val.C, t.C, cname)
1585         return
1586 }
1587 func (b Builder) CreateZExtOrBitCast(val Value, t Type, name string) (v Value) {
1588         cname := C.CString(name)
1589         defer C.free(unsafe.Pointer(cname))
1590         v.C = C.LLVMBuildZExtOrBitCast(b.C, val.C, t.C, cname)
1591         return
1592 }
1593 func (b Builder) CreateSExtOrBitCast(val Value, t Type, name string) (v Value) {
1594         cname := C.CString(name)
1595         defer C.free(unsafe.Pointer(cname))
1596         v.C = C.LLVMBuildSExtOrBitCast(b.C, val.C, t.C, cname)
1597         return
1598 }
1599 func (b Builder) CreateTruncOrBitCast(val Value, t Type, name string) (v Value) {
1600         cname := C.CString(name)
1601         defer C.free(unsafe.Pointer(cname))
1602         v.C = C.LLVMBuildTruncOrBitCast(b.C, val.C, t.C, cname)
1603         return
1604 }
1605 func (b Builder) CreateCast(val Value, op Opcode, t Type, name string) (v Value) {
1606         cname := C.CString(name)
1607         defer C.free(unsafe.Pointer(cname))
1608         v.C = C.LLVMBuildCast(b.C, C.LLVMOpcode(op), val.C, t.C, cname)
1609         return
1610 } //
1611 func (b Builder) CreatePointerCast(val Value, t Type, name string) (v Value) {
1612         cname := C.CString(name)
1613         defer C.free(unsafe.Pointer(cname))
1614         v.C = C.LLVMBuildPointerCast(b.C, val.C, t.C, cname)
1615         return
1616 }
1617 func (b Builder) CreateIntCast(val Value, t Type, name string) (v Value) {
1618         cname := C.CString(name)
1619         defer C.free(unsafe.Pointer(cname))
1620         v.C = C.LLVMBuildIntCast(b.C, val.C, t.C, cname)
1621         return
1622 }
1623 func (b Builder) CreateFPCast(val Value, t Type, name string) (v Value) {
1624         cname := C.CString(name)
1625         defer C.free(unsafe.Pointer(cname))
1626         v.C = C.LLVMBuildFPCast(b.C, val.C, t.C, cname)
1627         return
1628 }
1629
1630 // Comparisons
1631 func (b Builder) CreateICmp(pred IntPredicate, lhs, rhs Value, name string) (v Value) {
1632         cname := C.CString(name)
1633         defer C.free(unsafe.Pointer(cname))
1634         v.C = C.LLVMBuildICmp(b.C, C.LLVMIntPredicate(pred), lhs.C, rhs.C, cname)
1635         return
1636 }
1637 func (b Builder) CreateFCmp(pred FloatPredicate, lhs, rhs Value, name string) (v Value) {
1638         cname := C.CString(name)
1639         defer C.free(unsafe.Pointer(cname))
1640         v.C = C.LLVMBuildFCmp(b.C, C.LLVMRealPredicate(pred), lhs.C, rhs.C, cname)
1641         return
1642 }
1643
1644 // Miscellaneous instructions
1645 func (b Builder) CreatePHI(t Type, name string) (v Value) {
1646         cname := C.CString(name)
1647         defer C.free(unsafe.Pointer(cname))
1648         v.C = C.LLVMBuildPhi(b.C, t.C, cname)
1649         return
1650 }
1651 func (b Builder) CreateCall(fn Value, args []Value, name string) (v Value) {
1652         cname := C.CString(name)
1653         defer C.free(unsafe.Pointer(cname))
1654         ptr, nvals := llvmValueRefs(args)
1655         v.C = C.LLVMBuildCall(b.C, fn.C, ptr, nvals, cname)
1656         return
1657 }
1658
1659 func (b Builder) CreateSelect(ifv, thenv, elsev Value, name string) (v Value) {
1660         cname := C.CString(name)
1661         defer C.free(unsafe.Pointer(cname))
1662         v.C = C.LLVMBuildSelect(b.C, ifv.C, thenv.C, elsev.C, cname)
1663         return
1664 }
1665
1666 func (b Builder) CreateVAArg(list Value, t Type, name string) (v Value) {
1667         cname := C.CString(name)
1668         defer C.free(unsafe.Pointer(cname))
1669         v.C = C.LLVMBuildVAArg(b.C, list.C, t.C, cname)
1670         return
1671 }
1672 func (b Builder) CreateExtractElement(vec, i Value, name string) (v Value) {
1673         cname := C.CString(name)
1674         defer C.free(unsafe.Pointer(cname))
1675         v.C = C.LLVMBuildExtractElement(b.C, vec.C, i.C, cname)
1676         return
1677 }
1678 func (b Builder) CreateInsertElement(vec, elt, i Value, name string) (v Value) {
1679         cname := C.CString(name)
1680         defer C.free(unsafe.Pointer(cname))
1681         v.C = C.LLVMBuildInsertElement(b.C, vec.C, elt.C, i.C, cname)
1682         return
1683 }
1684 func (b Builder) CreateShuffleVector(v1, v2, mask Value, name string) (v Value) {
1685         cname := C.CString(name)
1686         defer C.free(unsafe.Pointer(cname))
1687         v.C = C.LLVMBuildShuffleVector(b.C, v1.C, v2.C, mask.C, cname)
1688         return
1689 }
1690 func (b Builder) CreateExtractValue(agg Value, i int, name string) (v Value) {
1691         cname := C.CString(name)
1692         defer C.free(unsafe.Pointer(cname))
1693         v.C = C.LLVMBuildExtractValue(b.C, agg.C, C.unsigned(i), cname)
1694         return
1695 }
1696 func (b Builder) CreateInsertValue(agg, elt Value, i int, name string) (v Value) {
1697         cname := C.CString(name)
1698         defer C.free(unsafe.Pointer(cname))
1699         v.C = C.LLVMBuildInsertValue(b.C, agg.C, elt.C, C.unsigned(i), cname)
1700         return
1701 }
1702
1703 func (b Builder) CreateIsNull(val Value, name string) (v Value) {
1704         cname := C.CString(name)
1705         defer C.free(unsafe.Pointer(cname))
1706         v.C = C.LLVMBuildIsNull(b.C, val.C, cname)
1707         return
1708 }
1709 func (b Builder) CreateIsNotNull(val Value, name string) (v Value) {
1710         cname := C.CString(name)
1711         defer C.free(unsafe.Pointer(cname))
1712         v.C = C.LLVMBuildIsNotNull(b.C, val.C, cname)
1713         return
1714 }
1715 func (b Builder) CreatePtrDiff(lhs, rhs Value, name string) (v Value) {
1716         cname := C.CString(name)
1717         defer C.free(unsafe.Pointer(cname))
1718         v.C = C.LLVMBuildPtrDiff(b.C, lhs.C, rhs.C, cname)
1719         return
1720 }
1721
1722 func (b Builder) CreateLandingPad(t Type, personality Value, nclauses int, name string) (l Value) {
1723         cname := C.CString(name)
1724         defer C.free(unsafe.Pointer(cname))
1725         l.C = C.LLVMBuildLandingPad(b.C, t.C, personality.C, C.unsigned(nclauses), cname)
1726         return l
1727 }
1728
1729 func (l Value) AddClause(v Value) {
1730         C.LLVMAddClause(l.C, v.C)
1731 }
1732
1733 func (l Value) SetCleanup(cleanup bool) {
1734         C.LLVMSetCleanup(l.C, boolToLLVMBool(cleanup))
1735 }
1736
1737 func (b Builder) CreateResume(ex Value) (v Value) {
1738         v.C = C.LLVMBuildResume(b.C, ex.C)
1739         return
1740 }
1741
1742 //-------------------------------------------------------------------------
1743 // llvm.ModuleProvider
1744 //-------------------------------------------------------------------------
1745
1746 // Changes the type of M so it can be passed to FunctionPassManagers and the
1747 // JIT. They take ModuleProviders for historical reasons.
1748 func NewModuleProviderForModule(m Module) (mp ModuleProvider) {
1749         mp.C = C.LLVMCreateModuleProviderForExistingModule(m.C)
1750         return
1751 }
1752
1753 // Destroys the module M.
1754 func (mp ModuleProvider) Dispose() { C.LLVMDisposeModuleProvider(mp.C) }
1755
1756 //-------------------------------------------------------------------------
1757 // llvm.MemoryBuffer
1758 //-------------------------------------------------------------------------
1759
1760 func NewMemoryBufferFromFile(path string) (b MemoryBuffer, err error) {
1761         var cmsg *C.char
1762         cpath := C.CString(path)
1763         defer C.free(unsafe.Pointer(cpath))
1764         fail := C.LLVMCreateMemoryBufferWithContentsOfFile(cpath, &b.C, &cmsg)
1765         if fail != 0 {
1766                 b.C = nil
1767                 err = errors.New(C.GoString(cmsg))
1768                 C.LLVMDisposeMessage(cmsg)
1769         }
1770         return
1771 }
1772
1773 func NewMemoryBufferFromStdin() (b MemoryBuffer, err error) {
1774         var cmsg *C.char
1775         fail := C.LLVMCreateMemoryBufferWithSTDIN(&b.C, &cmsg)
1776         if fail != 0 {
1777                 b.C = nil
1778                 err = errors.New(C.GoString(cmsg))
1779                 C.LLVMDisposeMessage(cmsg)
1780         }
1781         return
1782 }
1783
1784 func (b MemoryBuffer) Bytes() []byte {
1785         cstart := C.LLVMGetBufferStart(b.C)
1786         csize := C.LLVMGetBufferSize(b.C)
1787         return C.GoBytes(unsafe.Pointer(cstart), C.int(csize))
1788 }
1789
1790 func (b MemoryBuffer) Dispose() { C.LLVMDisposeMemoryBuffer(b.C) }
1791
1792 //-------------------------------------------------------------------------
1793 // llvm.PassManager
1794 //-------------------------------------------------------------------------
1795
1796 // Constructs a new whole-module pass pipeline. This type of pipeline is
1797 // suitable for link-time optimization and whole-module transformations.
1798 // See llvm::PassManager::PassManager.
1799 func NewPassManager() (pm PassManager) { pm.C = C.LLVMCreatePassManager(); return }
1800
1801 // Constructs a new function-by-function pass pipeline over the module
1802 // provider. It does not take ownership of the module provider. This type of
1803 // pipeline is suitable for code generation and JIT compilation tasks.
1804 // See llvm::FunctionPassManager::FunctionPassManager.
1805 func NewFunctionPassManagerForModule(m Module) (pm PassManager) {
1806         pm.C = C.LLVMCreateFunctionPassManagerForModule(m.C)
1807         return
1808 }
1809
1810 // Initializes, executes on the provided module, and finalizes all of the
1811 // passes scheduled in the pass manager. Returns 1 if any of the passes
1812 // modified the module, 0 otherwise. See llvm::PassManager::run(Module&).
1813 func (pm PassManager) Run(m Module) bool { return C.LLVMRunPassManager(pm.C, m.C) != 0 }
1814
1815 // Initializes all of the function passes scheduled in the function pass
1816 // manager. Returns 1 if any of the passes modified the module, 0 otherwise.
1817 // See llvm::FunctionPassManager::doInitialization.
1818 func (pm PassManager) InitializeFunc() bool { return C.LLVMInitializeFunctionPassManager(pm.C) != 0 }
1819
1820 // Executes all of the function passes scheduled in the function pass manager
1821 // on the provided function. Returns 1 if any of the passes modified the
1822 // function, false otherwise.
1823 // See llvm::FunctionPassManager::run(Function&).
1824 func (pm PassManager) RunFunc(f Value) bool { return C.LLVMRunFunctionPassManager(pm.C, f.C) != 0 }
1825
1826 // Finalizes all of the function passes scheduled in in the function pass
1827 // manager. Returns 1 if any of the passes modified the module, 0 otherwise.
1828 // See llvm::FunctionPassManager::doFinalization.
1829 func (pm PassManager) FinalizeFunc() bool { return C.LLVMFinalizeFunctionPassManager(pm.C) != 0 }
1830
1831 // Frees the memory of a pass pipeline. For function pipelines, does not free
1832 // the module provider.
1833 // See llvm::PassManagerBase::~PassManagerBase.
1834 func (pm PassManager) Dispose() { C.LLVMDisposePassManager(pm.C) }
1835
1836 //-------------------------------------------------------------------------
1837 // llvm.Metadata
1838 //-------------------------------------------------------------------------
1839
1840 func (md Metadata) ReplaceAllUsesWith(new Metadata) {
1841         C.LLVMMetadataReplaceAllUsesWith(md.C, new.C)
1842 }