OSDN Git Service

[LLVM-C] Correct The Current Debug Location Accessors (Again)
[android-x86/external-llvm.git] / bindings / go / llvm / ir.go
1 //===- ir.go - Bindings for ir --------------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file defines bindings for the ir component.
10 //
11 //===----------------------------------------------------------------------===//
12
13 package llvm
14
15 /*
16 #include "llvm-c/Core.h"
17 #include "llvm-c/Comdat.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         Comdat struct {
41                 C C.LLVMComdatRef
42         }
43         BasicBlock struct {
44                 C C.LLVMBasicBlockRef
45         }
46         Builder struct {
47                 C C.LLVMBuilderRef
48         }
49         ModuleProvider struct {
50                 C C.LLVMModuleProviderRef
51         }
52         MemoryBuffer struct {
53                 C C.LLVMMemoryBufferRef
54         }
55         PassManager struct {
56                 C C.LLVMPassManagerRef
57         }
58         Use struct {
59                 C C.LLVMUseRef
60         }
61         Metadata struct {
62                 C C.LLVMMetadataRef
63         }
64         Attribute struct {
65                 C C.LLVMAttributeRef
66         }
67         Opcode              C.LLVMOpcode
68         TypeKind            C.LLVMTypeKind
69         Linkage             C.LLVMLinkage
70         Visibility          C.LLVMVisibility
71         CallConv            C.LLVMCallConv
72         ComdatSelectionKind C.LLVMComdatSelectionKind
73         IntPredicate        C.LLVMIntPredicate
74         FloatPredicate      C.LLVMRealPredicate
75         LandingPadClause    C.LLVMLandingPadClauseTy
76         InlineAsmDialect    C.LLVMInlineAsmDialect
77 )
78
79 func (c Context) IsNil() bool        { return c.C == nil }
80 func (c Module) IsNil() bool         { return c.C == nil }
81 func (c Type) IsNil() bool           { return c.C == nil }
82 func (c Value) IsNil() bool          { return c.C == nil }
83 func (c BasicBlock) IsNil() bool     { return c.C == nil }
84 func (c Builder) IsNil() bool        { return c.C == nil }
85 func (c ModuleProvider) IsNil() bool { return c.C == nil }
86 func (c MemoryBuffer) IsNil() bool   { return c.C == nil }
87 func (c PassManager) IsNil() bool    { return c.C == nil }
88 func (c Use) IsNil() bool            { return c.C == nil }
89 func (c Attribute) IsNil() bool      { return c.C == nil }
90
91 // helpers
92 func llvmTypeRefPtr(t *Type) *C.LLVMTypeRef    { return (*C.LLVMTypeRef)(unsafe.Pointer(t)) }
93 func llvmValueRefPtr(t *Value) *C.LLVMValueRef { return (*C.LLVMValueRef)(unsafe.Pointer(t)) }
94 func llvmMetadataRefPtr(t *Metadata) *C.LLVMMetadataRef {
95         return (*C.LLVMMetadataRef)(unsafe.Pointer(t))
96 }
97 func llvmBasicBlockRefPtr(t *BasicBlock) *C.LLVMBasicBlockRef {
98         return (*C.LLVMBasicBlockRef)(unsafe.Pointer(t))
99 }
100 func boolToLLVMBool(b bool) C.LLVMBool {
101         if b {
102                 return C.LLVMBool(1)
103         }
104         return C.LLVMBool(0)
105 }
106
107 func llvmValueRefs(values []Value) (*C.LLVMValueRef, C.unsigned) {
108         var pt *C.LLVMValueRef
109         ptlen := C.unsigned(len(values))
110         if ptlen > 0 {
111                 pt = llvmValueRefPtr(&values[0])
112         }
113         return pt, ptlen
114 }
115
116 func llvmMetadataRefs(mds []Metadata) (*C.LLVMMetadataRef, C.unsigned) {
117         var pt *C.LLVMMetadataRef
118         ptlen := C.unsigned(len(mds))
119         if ptlen > 0 {
120                 pt = llvmMetadataRefPtr(&mds[0])
121         }
122         return pt, ptlen
123 }
124
125 //-------------------------------------------------------------------------
126 // llvm.Opcode
127 //-------------------------------------------------------------------------
128
129 const (
130         Ret         Opcode = C.LLVMRet
131         Br          Opcode = C.LLVMBr
132         Switch      Opcode = C.LLVMSwitch
133         IndirectBr  Opcode = C.LLVMIndirectBr
134         Invoke      Opcode = C.LLVMInvoke
135         Unreachable Opcode = C.LLVMUnreachable
136
137         // Standard Binary Operators
138         Add  Opcode = C.LLVMAdd
139         FAdd Opcode = C.LLVMFAdd
140         Sub  Opcode = C.LLVMSub
141         FSub Opcode = C.LLVMFSub
142         Mul  Opcode = C.LLVMMul
143         FMul Opcode = C.LLVMFMul
144         UDiv Opcode = C.LLVMUDiv
145         SDiv Opcode = C.LLVMSDiv
146         FDiv Opcode = C.LLVMFDiv
147         URem Opcode = C.LLVMURem
148         SRem Opcode = C.LLVMSRem
149         FRem Opcode = C.LLVMFRem
150
151         // Logical Operators
152         Shl  Opcode = C.LLVMShl
153         LShr Opcode = C.LLVMLShr
154         AShr Opcode = C.LLVMAShr
155         And  Opcode = C.LLVMAnd
156         Or   Opcode = C.LLVMOr
157         Xor  Opcode = C.LLVMXor
158
159         // Memory Operators
160         Alloca        Opcode = C.LLVMAlloca
161         Load          Opcode = C.LLVMLoad
162         Store         Opcode = C.LLVMStore
163         GetElementPtr Opcode = C.LLVMGetElementPtr
164
165         // Cast Operators
166         Trunc    Opcode = C.LLVMTrunc
167         ZExt     Opcode = C.LLVMZExt
168         SExt     Opcode = C.LLVMSExt
169         FPToUI   Opcode = C.LLVMFPToUI
170         FPToSI   Opcode = C.LLVMFPToSI
171         UIToFP   Opcode = C.LLVMUIToFP
172         SIToFP   Opcode = C.LLVMSIToFP
173         FPTrunc  Opcode = C.LLVMFPTrunc
174         FPExt    Opcode = C.LLVMFPExt
175         PtrToInt Opcode = C.LLVMPtrToInt
176         IntToPtr Opcode = C.LLVMIntToPtr
177         BitCast  Opcode = C.LLVMBitCast
178
179         // Other Operators
180         ICmp   Opcode = C.LLVMICmp
181         FCmp   Opcode = C.LLVMFCmp
182         PHI    Opcode = C.LLVMPHI
183         Call   Opcode = C.LLVMCall
184         Select Opcode = C.LLVMSelect
185         // UserOp1
186         // UserOp2
187         VAArg          Opcode = C.LLVMVAArg
188         ExtractElement Opcode = C.LLVMExtractElement
189         InsertElement  Opcode = C.LLVMInsertElement
190         ShuffleVector  Opcode = C.LLVMShuffleVector
191         ExtractValue   Opcode = C.LLVMExtractValue
192         InsertValue    Opcode = C.LLVMInsertValue
193 )
194
195 //-------------------------------------------------------------------------
196 // llvm.TypeKind
197 //-------------------------------------------------------------------------
198
199 const (
200         VoidTypeKind      TypeKind = C.LLVMVoidTypeKind
201         FloatTypeKind     TypeKind = C.LLVMFloatTypeKind
202         DoubleTypeKind    TypeKind = C.LLVMDoubleTypeKind
203         X86_FP80TypeKind  TypeKind = C.LLVMX86_FP80TypeKind
204         FP128TypeKind     TypeKind = C.LLVMFP128TypeKind
205         PPC_FP128TypeKind TypeKind = C.LLVMPPC_FP128TypeKind
206         LabelTypeKind     TypeKind = C.LLVMLabelTypeKind
207         IntegerTypeKind   TypeKind = C.LLVMIntegerTypeKind
208         FunctionTypeKind  TypeKind = C.LLVMFunctionTypeKind
209         StructTypeKind    TypeKind = C.LLVMStructTypeKind
210         ArrayTypeKind     TypeKind = C.LLVMArrayTypeKind
211         PointerTypeKind   TypeKind = C.LLVMPointerTypeKind
212         VectorTypeKind    TypeKind = C.LLVMVectorTypeKind
213         MetadataTypeKind  TypeKind = C.LLVMMetadataTypeKind
214         TokenTypeKind     TypeKind = C.LLVMTokenTypeKind
215 )
216
217 //-------------------------------------------------------------------------
218 // llvm.Linkage
219 //-------------------------------------------------------------------------
220
221 const (
222         ExternalLinkage            Linkage = C.LLVMExternalLinkage
223         AvailableExternallyLinkage Linkage = C.LLVMAvailableExternallyLinkage
224         LinkOnceAnyLinkage         Linkage = C.LLVMLinkOnceAnyLinkage
225         LinkOnceODRLinkage         Linkage = C.LLVMLinkOnceODRLinkage
226         WeakAnyLinkage             Linkage = C.LLVMWeakAnyLinkage
227         WeakODRLinkage             Linkage = C.LLVMWeakODRLinkage
228         AppendingLinkage           Linkage = C.LLVMAppendingLinkage
229         InternalLinkage            Linkage = C.LLVMInternalLinkage
230         PrivateLinkage             Linkage = C.LLVMPrivateLinkage
231         ExternalWeakLinkage        Linkage = C.LLVMExternalWeakLinkage
232         CommonLinkage              Linkage = C.LLVMCommonLinkage
233 )
234
235 //-------------------------------------------------------------------------
236 // llvm.Visibility
237 //-------------------------------------------------------------------------
238
239 const (
240         DefaultVisibility   Visibility = C.LLVMDefaultVisibility
241         HiddenVisibility    Visibility = C.LLVMHiddenVisibility
242         ProtectedVisibility Visibility = C.LLVMProtectedVisibility
243 )
244
245 //-------------------------------------------------------------------------
246 // llvm.CallConv
247 //-------------------------------------------------------------------------
248
249 const (
250         CCallConv           CallConv = C.LLVMCCallConv
251         FastCallConv        CallConv = C.LLVMFastCallConv
252         ColdCallConv        CallConv = C.LLVMColdCallConv
253         X86StdcallCallConv  CallConv = C.LLVMX86StdcallCallConv
254         X86FastcallCallConv CallConv = C.LLVMX86FastcallCallConv
255 )
256
257 //-------------------------------------------------------------------------
258 // llvm.ComdatSelectionKind
259 //-------------------------------------------------------------------------
260
261 const (
262         AnyComdatSelectionKind          ComdatSelectionKind = C.LLVMAnyComdatSelectionKind
263         ExactMatchComdatSelectionKind   ComdatSelectionKind = C.LLVMExactMatchComdatSelectionKind
264         LargestComdatSelectionKind      ComdatSelectionKind = C.LLVMLargestComdatSelectionKind
265         NoDuplicatesComdatSelectionKind ComdatSelectionKind = C.LLVMNoDuplicatesComdatSelectionKind
266         SameSizeComdatSelectionKind     ComdatSelectionKind = C.LLVMSameSizeComdatSelectionKind
267 )
268
269 //-------------------------------------------------------------------------
270 // llvm.IntPredicate
271 //-------------------------------------------------------------------------
272
273 const (
274         IntEQ  IntPredicate = C.LLVMIntEQ
275         IntNE  IntPredicate = C.LLVMIntNE
276         IntUGT IntPredicate = C.LLVMIntUGT
277         IntUGE IntPredicate = C.LLVMIntUGE
278         IntULT IntPredicate = C.LLVMIntULT
279         IntULE IntPredicate = C.LLVMIntULE
280         IntSGT IntPredicate = C.LLVMIntSGT
281         IntSGE IntPredicate = C.LLVMIntSGE
282         IntSLT IntPredicate = C.LLVMIntSLT
283         IntSLE IntPredicate = C.LLVMIntSLE
284 )
285
286 //-------------------------------------------------------------------------
287 // llvm.FloatPredicate
288 //-------------------------------------------------------------------------
289
290 const (
291         FloatPredicateFalse FloatPredicate = C.LLVMRealPredicateFalse
292         FloatOEQ            FloatPredicate = C.LLVMRealOEQ
293         FloatOGT            FloatPredicate = C.LLVMRealOGT
294         FloatOGE            FloatPredicate = C.LLVMRealOGE
295         FloatOLT            FloatPredicate = C.LLVMRealOLT
296         FloatOLE            FloatPredicate = C.LLVMRealOLE
297         FloatONE            FloatPredicate = C.LLVMRealONE
298         FloatORD            FloatPredicate = C.LLVMRealORD
299         FloatUNO            FloatPredicate = C.LLVMRealUNO
300         FloatUEQ            FloatPredicate = C.LLVMRealUEQ
301         FloatUGT            FloatPredicate = C.LLVMRealUGT
302         FloatUGE            FloatPredicate = C.LLVMRealUGE
303         FloatULT            FloatPredicate = C.LLVMRealULT
304         FloatULE            FloatPredicate = C.LLVMRealULE
305         FloatUNE            FloatPredicate = C.LLVMRealUNE
306         FloatPredicateTrue  FloatPredicate = C.LLVMRealPredicateTrue
307 )
308
309 //-------------------------------------------------------------------------
310 // llvm.LandingPadClause
311 //-------------------------------------------------------------------------
312
313 const (
314         LandingPadCatch  LandingPadClause = C.LLVMLandingPadCatch
315         LandingPadFilter LandingPadClause = C.LLVMLandingPadFilter
316 )
317
318 //-------------------------------------------------------------------------
319 // llvm.InlineAsmDialect
320 //-------------------------------------------------------------------------
321
322 const (
323         InlineAsmDialectATT   InlineAsmDialect = C.LLVMInlineAsmDialectATT
324         InlineAsmDialectIntel InlineAsmDialect = C.LLVMInlineAsmDialectIntel
325 )
326
327 //-------------------------------------------------------------------------
328 // llvm.Context
329 //-------------------------------------------------------------------------
330
331 func NewContext() Context    { return Context{C.LLVMContextCreate()} }
332 func GlobalContext() Context { return Context{C.LLVMGetGlobalContext()} }
333 func (c Context) Dispose()   { C.LLVMContextDispose(c.C) }
334
335 func (c Context) MDKindID(name string) (id int) {
336         cname := C.CString(name)
337         defer C.free(unsafe.Pointer(cname))
338         id = int(C.LLVMGetMDKindIDInContext(c.C, cname, C.unsigned(len(name))))
339         return
340 }
341
342 func MDKindID(name string) (id int) {
343         cname := C.CString(name)
344         defer C.free(unsafe.Pointer(cname))
345         id = int(C.LLVMGetMDKindID(cname, C.unsigned(len(name))))
346         return
347 }
348
349 //-------------------------------------------------------------------------
350 // llvm.Attribute
351 //-------------------------------------------------------------------------
352
353 func AttributeKindID(name string) (id uint) {
354         cname := C.CString(name)
355         defer C.free(unsafe.Pointer(cname))
356         id = uint(C.LLVMGetEnumAttributeKindForName(cname, C.size_t(len(name))))
357         return
358 }
359
360 func (c Context) CreateEnumAttribute(kind uint, val uint64) (a Attribute) {
361   a.C = C.LLVMCreateEnumAttribute(c.C, C.unsigned(kind), C.uint64_t(val))
362   return
363 }
364
365 func (a Attribute) GetEnumKind() (id int) {
366   id = int(C.LLVMGetEnumAttributeKind(a.C))
367   return
368 }
369
370 func (a Attribute) GetEnumValue() (val uint64) {
371   val = uint64(C.LLVMGetEnumAttributeValue(a.C))
372   return
373 }
374
375 func (c Context) CreateStringAttribute(kind string, val string) (a Attribute) {
376   ckind := C.CString(kind)
377   defer C.free(unsafe.Pointer(ckind))
378   cval := C.CString(val)
379   defer C.free(unsafe.Pointer(cval))
380   a.C = C.LLVMCreateStringAttribute(c.C,
381                                     ckind, C.unsigned(len(kind)),
382                                     cval, C.unsigned(len(val)))
383   return
384 }
385
386 func (a Attribute) GetStringKind() string {
387   length := C.unsigned(0)
388   ckind := C.LLVMGetStringAttributeKind(a.C, &length)
389   return C.GoStringN(ckind, C.int(length))
390 }
391
392 func (a Attribute) GetStringValue() string {
393   length := C.unsigned(0)
394   ckind := C.LLVMGetStringAttributeValue(a.C, &length)
395   return C.GoStringN(ckind, C.int(length))
396 }
397
398 func (a Attribute) IsEnum() bool {
399   return C.LLVMIsEnumAttribute(a.C) != 0;
400 }
401
402 func (a Attribute) IsString() bool {
403   return C.LLVMIsStringAttribute(a.C) != 0;
404 }
405
406 //-------------------------------------------------------------------------
407 // llvm.Module
408 //-------------------------------------------------------------------------
409
410 // Create and destroy modules.
411 // See llvm::Module::Module.
412 func NewModule(name string) (m Module) {
413         cname := C.CString(name)
414         defer C.free(unsafe.Pointer(cname))
415         m.C = C.LLVMModuleCreateWithName(cname)
416         return
417 }
418
419 func (c Context) NewModule(name string) (m Module) {
420         cname := C.CString(name)
421         defer C.free(unsafe.Pointer(cname))
422         m.C = C.LLVMModuleCreateWithNameInContext(cname, c.C)
423         return
424 }
425
426 // See llvm::Module::~Module
427 func (m Module) Dispose() { C.LLVMDisposeModule(m.C) }
428
429 // Data layout. See Module::getDataLayout.
430 func (m Module) DataLayout() string {
431         clayout := C.LLVMGetDataLayout(m.C)
432         return C.GoString(clayout)
433 }
434
435 func (m Module) SetDataLayout(layout string) {
436         clayout := C.CString(layout)
437         defer C.free(unsafe.Pointer(clayout))
438         C.LLVMSetDataLayout(m.C, clayout)
439 }
440
441 // Target triple. See Module::getTargetTriple.
442 func (m Module) Target() string {
443         ctarget := C.LLVMGetTarget(m.C)
444         return C.GoString(ctarget)
445 }
446 func (m Module) SetTarget(target string) {
447         ctarget := C.CString(target)
448         defer C.free(unsafe.Pointer(ctarget))
449         C.LLVMSetTarget(m.C, ctarget)
450 }
451
452 func (m Module) GetTypeByName(name string) (t Type) {
453         cname := C.CString(name)
454         defer C.free(unsafe.Pointer(cname))
455         t.C = C.LLVMGetTypeByName(m.C, cname)
456         return
457 }
458
459 // See Module::dump.
460 func (m Module) Dump() {
461         C.LLVMDumpModule(m.C)
462 }
463
464 func (m Module) String() string {
465         cir := C.LLVMPrintModuleToString(m.C)
466         defer C.free(unsafe.Pointer(cir))
467         ir := C.GoString(cir)
468         return ir
469 }
470
471 // See Module::setModuleInlineAsm.
472 func (m Module) SetInlineAsm(asm string) {
473         casm := C.CString(asm)
474         defer C.free(unsafe.Pointer(casm))
475         C.LLVMSetModuleInlineAsm(m.C, casm)
476 }
477
478 func (m Module) AddNamedMetadataOperand(name string, operand Metadata) {
479         cname := C.CString(name)
480         defer C.free(unsafe.Pointer(cname))
481         C.LLVMAddNamedMetadataOperand2(m.C, cname, operand.C)
482 }
483
484 func (m Module) Context() (c Context) {
485         c.C = C.LLVMGetModuleContext(m.C)
486         return
487 }
488
489 //-------------------------------------------------------------------------
490 // llvm.Type
491 //-------------------------------------------------------------------------
492
493 // LLVM types conform to the following hierarchy:
494 //
495 //   types:
496 //     integer type
497 //     real type
498 //     function type
499 //     sequence types:
500 //       array type
501 //       pointer type
502 //       vector type
503 //     void type
504 //     label type
505 //     opaque type
506
507 // See llvm::LLVMTypeKind::getTypeID.
508 func (t Type) TypeKind() TypeKind { return TypeKind(C.LLVMGetTypeKind(t.C)) }
509
510 // See llvm::LLVMType::getContext.
511 func (t Type) Context() (c Context) {
512         c.C = C.LLVMGetTypeContext(t.C)
513         return
514 }
515
516 // Operations on integer types
517 func (c Context) Int1Type() (t Type)  { t.C = C.LLVMInt1TypeInContext(c.C); return }
518 func (c Context) Int8Type() (t Type)  { t.C = C.LLVMInt8TypeInContext(c.C); return }
519 func (c Context) Int16Type() (t Type) { t.C = C.LLVMInt16TypeInContext(c.C); return }
520 func (c Context) Int32Type() (t Type) { t.C = C.LLVMInt32TypeInContext(c.C); return }
521 func (c Context) Int64Type() (t Type) { t.C = C.LLVMInt64TypeInContext(c.C); return }
522 func (c Context) IntType(numbits int) (t Type) {
523         t.C = C.LLVMIntTypeInContext(c.C, C.unsigned(numbits))
524         return
525 }
526
527 func Int1Type() (t Type)  { t.C = C.LLVMInt1Type(); return }
528 func Int8Type() (t Type)  { t.C = C.LLVMInt8Type(); return }
529 func Int16Type() (t Type) { t.C = C.LLVMInt16Type(); return }
530 func Int32Type() (t Type) { t.C = C.LLVMInt32Type(); return }
531 func Int64Type() (t Type) { t.C = C.LLVMInt64Type(); return }
532
533 func IntType(numbits int) (t Type) {
534         t.C = C.LLVMIntType(C.unsigned(numbits))
535         return
536 }
537
538 func (t Type) IntTypeWidth() int {
539         return int(C.LLVMGetIntTypeWidth(t.C))
540 }
541
542 // Operations on real types
543 func (c Context) FloatType() (t Type)    { t.C = C.LLVMFloatTypeInContext(c.C); return }
544 func (c Context) DoubleType() (t Type)   { t.C = C.LLVMDoubleTypeInContext(c.C); return }
545 func (c Context) X86FP80Type() (t Type)  { t.C = C.LLVMX86FP80TypeInContext(c.C); return }
546 func (c Context) FP128Type() (t Type)    { t.C = C.LLVMFP128TypeInContext(c.C); return }
547 func (c Context) PPCFP128Type() (t Type) { t.C = C.LLVMPPCFP128TypeInContext(c.C); return }
548
549 func FloatType() (t Type)    { t.C = C.LLVMFloatType(); return }
550 func DoubleType() (t Type)   { t.C = C.LLVMDoubleType(); return }
551 func X86FP80Type() (t Type)  { t.C = C.LLVMX86FP80Type(); return }
552 func FP128Type() (t Type)    { t.C = C.LLVMFP128Type(); return }
553 func PPCFP128Type() (t Type) { t.C = C.LLVMPPCFP128Type(); return }
554
555 // Operations on function types
556 func FunctionType(returnType Type, paramTypes []Type, isVarArg bool) (t Type) {
557         var pt *C.LLVMTypeRef
558         var ptlen C.unsigned
559         if len(paramTypes) > 0 {
560                 pt = llvmTypeRefPtr(&paramTypes[0])
561                 ptlen = C.unsigned(len(paramTypes))
562         }
563         t.C = C.LLVMFunctionType(returnType.C,
564                 pt,
565                 ptlen,
566                 boolToLLVMBool(isVarArg))
567         return
568 }
569
570 func (t Type) IsFunctionVarArg() bool { return C.LLVMIsFunctionVarArg(t.C) != 0 }
571 func (t Type) ReturnType() (rt Type)  { rt.C = C.LLVMGetReturnType(t.C); return }
572 func (t Type) ParamTypesCount() int   { return int(C.LLVMCountParamTypes(t.C)) }
573 func (t Type) ParamTypes() []Type {
574         count := t.ParamTypesCount()
575         if count > 0 {
576                 out := make([]Type, count)
577                 C.LLVMGetParamTypes(t.C, llvmTypeRefPtr(&out[0]))
578                 return out
579         }
580         return nil
581 }
582
583 // Operations on struct types
584 func (c Context) StructType(elementTypes []Type, packed bool) (t Type) {
585         var pt *C.LLVMTypeRef
586         var ptlen C.unsigned
587         if len(elementTypes) > 0 {
588                 pt = llvmTypeRefPtr(&elementTypes[0])
589                 ptlen = C.unsigned(len(elementTypes))
590         }
591         t.C = C.LLVMStructTypeInContext(c.C,
592                 pt,
593                 ptlen,
594                 boolToLLVMBool(packed))
595         return
596 }
597
598 func StructType(elementTypes []Type, packed bool) (t Type) {
599         var pt *C.LLVMTypeRef
600         var ptlen C.unsigned
601         if len(elementTypes) > 0 {
602                 pt = llvmTypeRefPtr(&elementTypes[0])
603                 ptlen = C.unsigned(len(elementTypes))
604         }
605         t.C = C.LLVMStructType(pt, ptlen, boolToLLVMBool(packed))
606         return
607 }
608
609 func (c Context) StructCreateNamed(name string) (t Type) {
610         cname := C.CString(name)
611         defer C.free(unsafe.Pointer(cname))
612         t.C = C.LLVMStructCreateNamed(c.C, cname)
613         return
614 }
615
616 func (t Type) StructName() string {
617         return C.GoString(C.LLVMGetStructName(t.C))
618 }
619
620 func (t Type) StructSetBody(elementTypes []Type, packed bool) {
621         var pt *C.LLVMTypeRef
622         var ptlen C.unsigned
623         if len(elementTypes) > 0 {
624                 pt = llvmTypeRefPtr(&elementTypes[0])
625                 ptlen = C.unsigned(len(elementTypes))
626         }
627         C.LLVMStructSetBody(t.C, pt, ptlen, boolToLLVMBool(packed))
628 }
629
630 func (t Type) IsStructPacked() bool         { return C.LLVMIsPackedStruct(t.C) != 0 }
631 func (t Type) StructElementTypesCount() int { return int(C.LLVMCountStructElementTypes(t.C)) }
632 func (t Type) StructElementTypes() []Type {
633         out := make([]Type, t.StructElementTypesCount())
634         if len(out) > 0 {
635                 C.LLVMGetStructElementTypes(t.C, llvmTypeRefPtr(&out[0]))
636         }
637         return out
638 }
639
640 // Operations on array, pointer, and vector types (sequence types)
641 func (t Type) Subtypes() (ret []Type) {
642         ret = make([]Type, C.LLVMGetNumContainedTypes(t.C))
643         C.LLVMGetSubtypes(t.C, llvmTypeRefPtr(&ret[0]))
644         return
645 }
646
647 func ArrayType(elementType Type, elementCount int) (t Type) {
648         t.C = C.LLVMArrayType(elementType.C, C.unsigned(elementCount))
649         return
650 }
651 func PointerType(elementType Type, addressSpace int) (t Type) {
652         t.C = C.LLVMPointerType(elementType.C, C.unsigned(addressSpace))
653         return
654 }
655 func VectorType(elementType Type, elementCount int) (t Type) {
656         t.C = C.LLVMVectorType(elementType.C, C.unsigned(elementCount))
657         return
658 }
659
660 func (t Type) ElementType() (rt Type)   { rt.C = C.LLVMGetElementType(t.C); return }
661 func (t Type) ArrayLength() int         { return int(C.LLVMGetArrayLength(t.C)) }
662 func (t Type) PointerAddressSpace() int { return int(C.LLVMGetPointerAddressSpace(t.C)) }
663 func (t Type) VectorSize() int          { return int(C.LLVMGetVectorSize(t.C)) }
664
665 // Operations on other types
666 func (c Context) VoidType() (t Type)  { t.C = C.LLVMVoidTypeInContext(c.C); return }
667 func (c Context) LabelType() (t Type) { t.C = C.LLVMLabelTypeInContext(c.C); return }
668 func (c Context) TokenType() (t Type) { t.C = C.LLVMTokenTypeInContext(c.C); return }
669
670 func VoidType() (t Type)  { t.C = C.LLVMVoidType(); return }
671 func LabelType() (t Type) { t.C = C.LLVMLabelType(); return }
672
673 //-------------------------------------------------------------------------
674 // llvm.Value
675 //-------------------------------------------------------------------------
676
677 // Operations on all values
678 func (v Value) Type() (t Type) { t.C = C.LLVMTypeOf(v.C); return }
679 func (v Value) Name() string   { return C.GoString(C.LLVMGetValueName(v.C)) }
680 func (v Value) SetName(name string) {
681         cname := C.CString(name)
682         defer C.free(unsafe.Pointer(cname))
683         C.LLVMSetValueName(v.C, cname)
684 }
685 func (v Value) Dump()                       { C.LLVMDumpValue(v.C) }
686 func (v Value) ReplaceAllUsesWith(nv Value) { C.LLVMReplaceAllUsesWith(v.C, nv.C) }
687 func (v Value) HasMetadata() bool           { return C.LLVMHasMetadata(v.C) != 0 }
688 func (v Value) Metadata(kind int) (rv Value) {
689         rv.C = C.LLVMGetMetadata(v.C, C.unsigned(kind))
690         return
691 }
692 func (v Value) SetMetadata(kind int, node Metadata) {
693         C.LLVMSetMetadata2(v.C, C.unsigned(kind), node.C)
694 }
695
696 // Conversion functions.
697 // Return the input value if it is an instance of the specified class, otherwise NULL.
698 // See llvm::dyn_cast_or_null<>.
699 func (v Value) IsAArgument() (rv Value)   { rv.C = C.LLVMIsAArgument(v.C); return }
700 func (v Value) IsABasicBlock() (rv Value) { rv.C = C.LLVMIsABasicBlock(v.C); return }
701 func (v Value) IsAInlineAsm() (rv Value)  { rv.C = C.LLVMIsAInlineAsm(v.C); return }
702 func (v Value) IsAUser() (rv Value)       { rv.C = C.LLVMIsAUser(v.C); return }
703 func (v Value) IsAConstant() (rv Value)   { rv.C = C.LLVMIsAConstant(v.C); return }
704 func (v Value) IsAConstantAggregateZero() (rv Value) {
705         rv.C = C.LLVMIsAConstantAggregateZero(v.C)
706         return
707 }
708 func (v Value) IsAConstantArray() (rv Value)       { rv.C = C.LLVMIsAConstantArray(v.C); return }
709 func (v Value) IsAConstantExpr() (rv Value)        { rv.C = C.LLVMIsAConstantExpr(v.C); return }
710 func (v Value) IsAConstantFP() (rv Value)          { rv.C = C.LLVMIsAConstantFP(v.C); return }
711 func (v Value) IsAConstantInt() (rv Value)         { rv.C = C.LLVMIsAConstantInt(v.C); return }
712 func (v Value) IsAConstantPointerNull() (rv Value) { rv.C = C.LLVMIsAConstantPointerNull(v.C); return }
713 func (v Value) IsAConstantStruct() (rv Value)      { rv.C = C.LLVMIsAConstantStruct(v.C); return }
714 func (v Value) IsAConstantVector() (rv Value)      { rv.C = C.LLVMIsAConstantVector(v.C); return }
715 func (v Value) IsAGlobalValue() (rv Value)         { rv.C = C.LLVMIsAGlobalValue(v.C); return }
716 func (v Value) IsAFunction() (rv Value)            { rv.C = C.LLVMIsAFunction(v.C); return }
717 func (v Value) IsAGlobalAlias() (rv Value)         { rv.C = C.LLVMIsAGlobalAlias(v.C); return }
718 func (v Value) IsAGlobalVariable() (rv Value)      { rv.C = C.LLVMIsAGlobalVariable(v.C); return }
719 func (v Value) IsAUndefValue() (rv Value)          { rv.C = C.LLVMIsAUndefValue(v.C); return }
720 func (v Value) IsAInstruction() (rv Value)         { rv.C = C.LLVMIsAInstruction(v.C); return }
721 func (v Value) IsABinaryOperator() (rv Value)      { rv.C = C.LLVMIsABinaryOperator(v.C); return }
722 func (v Value) IsACallInst() (rv Value)            { rv.C = C.LLVMIsACallInst(v.C); return }
723 func (v Value) IsAIntrinsicInst() (rv Value)       { rv.C = C.LLVMIsAIntrinsicInst(v.C); return }
724 func (v Value) IsADbgInfoIntrinsic() (rv Value)    { rv.C = C.LLVMIsADbgInfoIntrinsic(v.C); return }
725 func (v Value) IsADbgDeclareInst() (rv Value)      { rv.C = C.LLVMIsADbgDeclareInst(v.C); return }
726 func (v Value) IsAMemIntrinsic() (rv Value)        { rv.C = C.LLVMIsAMemIntrinsic(v.C); return }
727 func (v Value) IsAMemCpyInst() (rv Value)          { rv.C = C.LLVMIsAMemCpyInst(v.C); return }
728 func (v Value) IsAMemMoveInst() (rv Value)         { rv.C = C.LLVMIsAMemMoveInst(v.C); return }
729 func (v Value) IsAMemSetInst() (rv Value)          { rv.C = C.LLVMIsAMemSetInst(v.C); return }
730 func (v Value) IsACmpInst() (rv Value)             { rv.C = C.LLVMIsACmpInst(v.C); return }
731 func (v Value) IsAFCmpInst() (rv Value)            { rv.C = C.LLVMIsAFCmpInst(v.C); return }
732 func (v Value) IsAICmpInst() (rv Value)            { rv.C = C.LLVMIsAICmpInst(v.C); return }
733 func (v Value) IsAExtractElementInst() (rv Value)  { rv.C = C.LLVMIsAExtractElementInst(v.C); return }
734 func (v Value) IsAGetElementPtrInst() (rv Value)   { rv.C = C.LLVMIsAGetElementPtrInst(v.C); return }
735 func (v Value) IsAInsertElementInst() (rv Value)   { rv.C = C.LLVMIsAInsertElementInst(v.C); return }
736 func (v Value) IsAInsertValueInst() (rv Value)     { rv.C = C.LLVMIsAInsertValueInst(v.C); return }
737 func (v Value) IsAPHINode() (rv Value)             { rv.C = C.LLVMIsAPHINode(v.C); return }
738 func (v Value) IsASelectInst() (rv Value)          { rv.C = C.LLVMIsASelectInst(v.C); return }
739 func (v Value) IsAShuffleVectorInst() (rv Value)   { rv.C = C.LLVMIsAShuffleVectorInst(v.C); return }
740 func (v Value) IsAStoreInst() (rv Value)           { rv.C = C.LLVMIsAStoreInst(v.C); return }
741 func (v Value) IsABranchInst() (rv Value)          { rv.C = C.LLVMIsABranchInst(v.C); return }
742 func (v Value) IsAInvokeInst() (rv Value)          { rv.C = C.LLVMIsAInvokeInst(v.C); return }
743 func (v Value) IsAReturnInst() (rv Value)          { rv.C = C.LLVMIsAReturnInst(v.C); return }
744 func (v Value) IsASwitchInst() (rv Value)          { rv.C = C.LLVMIsASwitchInst(v.C); return }
745 func (v Value) IsAUnreachableInst() (rv Value)     { rv.C = C.LLVMIsAUnreachableInst(v.C); return }
746 func (v Value) IsAUnaryInstruction() (rv Value)    { rv.C = C.LLVMIsAUnaryInstruction(v.C); return }
747 func (v Value) IsAAllocaInst() (rv Value)          { rv.C = C.LLVMIsAAllocaInst(v.C); return }
748 func (v Value) IsACastInst() (rv Value)            { rv.C = C.LLVMIsACastInst(v.C); return }
749 func (v Value) IsABitCastInst() (rv Value)         { rv.C = C.LLVMIsABitCastInst(v.C); return }
750 func (v Value) IsAFPExtInst() (rv Value)           { rv.C = C.LLVMIsAFPExtInst(v.C); return }
751 func (v Value) IsAFPToSIInst() (rv Value)          { rv.C = C.LLVMIsAFPToSIInst(v.C); return }
752 func (v Value) IsAFPToUIInst() (rv Value)          { rv.C = C.LLVMIsAFPToUIInst(v.C); return }
753 func (v Value) IsAFPTruncInst() (rv Value)         { rv.C = C.LLVMIsAFPTruncInst(v.C); return }
754 func (v Value) IsAIntToPtrInst() (rv Value)        { rv.C = C.LLVMIsAIntToPtrInst(v.C); return }
755 func (v Value) IsAPtrToIntInst() (rv Value)        { rv.C = C.LLVMIsAPtrToIntInst(v.C); return }
756 func (v Value) IsASExtInst() (rv Value)            { rv.C = C.LLVMIsASExtInst(v.C); return }
757 func (v Value) IsASIToFPInst() (rv Value)          { rv.C = C.LLVMIsASIToFPInst(v.C); return }
758 func (v Value) IsATruncInst() (rv Value)           { rv.C = C.LLVMIsATruncInst(v.C); return }
759 func (v Value) IsAUIToFPInst() (rv Value)          { rv.C = C.LLVMIsAUIToFPInst(v.C); return }
760 func (v Value) IsAZExtInst() (rv Value)            { rv.C = C.LLVMIsAZExtInst(v.C); return }
761 func (v Value) IsAExtractValueInst() (rv Value)    { rv.C = C.LLVMIsAExtractValueInst(v.C); return }
762 func (v Value) IsALoadInst() (rv Value)            { rv.C = C.LLVMIsALoadInst(v.C); return }
763 func (v Value) IsAVAArgInst() (rv Value)           { rv.C = C.LLVMIsAVAArgInst(v.C); return }
764
765 // Operations on Uses
766 func (v Value) FirstUse() (u Use)  { u.C = C.LLVMGetFirstUse(v.C); return }
767 func (u Use) NextUse() (ru Use)    { ru.C = C.LLVMGetNextUse(u.C); return }
768 func (u Use) User() (v Value)      { v.C = C.LLVMGetUser(u.C); return }
769 func (u Use) UsedValue() (v Value) { v.C = C.LLVMGetUsedValue(u.C); return }
770
771 // Operations on Users
772 func (v Value) Operand(i int) (rv Value)   { rv.C = C.LLVMGetOperand(v.C, C.unsigned(i)); return }
773 func (v Value) SetOperand(i int, op Value) { C.LLVMSetOperand(v.C, C.unsigned(i), op.C) }
774 func (v Value) OperandsCount() int         { return int(C.LLVMGetNumOperands(v.C)) }
775
776 // Operations on constants of any type
777 func ConstNull(t Type) (v Value)        { v.C = C.LLVMConstNull(t.C); return }
778 func ConstAllOnes(t Type) (v Value)     { v.C = C.LLVMConstAllOnes(t.C); return }
779 func Undef(t Type) (v Value)            { v.C = C.LLVMGetUndef(t.C); return }
780 func (v Value) IsConstant() bool        { return C.LLVMIsConstant(v.C) != 0 }
781 func (v Value) IsNull() bool            { return C.LLVMIsNull(v.C) != 0 }
782 func (v Value) IsUndef() bool           { return C.LLVMIsUndef(v.C) != 0 }
783 func ConstPointerNull(t Type) (v Value) { v.C = C.LLVMConstPointerNull(t.C); return }
784
785 // Operations on metadata
786 func (c Context) MDString(str string) (md Metadata) {
787         cstr := C.CString(str)
788         defer C.free(unsafe.Pointer(cstr))
789         md.C = C.LLVMMDString2(c.C, cstr, C.unsigned(len(str)))
790         return
791 }
792 func (c Context) MDNode(mds []Metadata) (md Metadata) {
793         ptr, nvals := llvmMetadataRefs(mds)
794         md.C = C.LLVMMDNode2(c.C, ptr, nvals)
795         return
796 }
797 func (v Value) ConstantAsMetadata() (md Metadata) {
798         md.C = C.LLVMConstantAsMetadata(v.C)
799         return
800 }
801
802 // Operations on scalar constants
803 func ConstInt(t Type, n uint64, signExtend bool) (v Value) {
804         v.C = C.LLVMConstInt(t.C,
805                 C.ulonglong(n),
806                 boolToLLVMBool(signExtend))
807         return
808 }
809 func ConstIntFromString(t Type, str string, radix int) (v Value) {
810         cstr := C.CString(str)
811         defer C.free(unsafe.Pointer(cstr))
812         v.C = C.LLVMConstIntOfString(t.C, cstr, C.uint8_t(radix))
813         return
814 }
815 func ConstFloat(t Type, n float64) (v Value) {
816         v.C = C.LLVMConstReal(t.C, C.double(n))
817         return
818 }
819 func ConstFloatFromString(t Type, str string) (v Value) {
820         cstr := C.CString(str)
821         defer C.free(unsafe.Pointer(cstr))
822         v.C = C.LLVMConstRealOfString(t.C, cstr)
823         return
824 }
825
826 func (v Value) ZExtValue() uint64 { return uint64(C.LLVMConstIntGetZExtValue(v.C)) }
827 func (v Value) SExtValue() int64  { return int64(C.LLVMConstIntGetSExtValue(v.C)) }
828
829 // Operations on composite constants
830 func (c Context) ConstString(str string, addnull bool) (v Value) {
831         cstr := C.CString(str)
832         defer C.free(unsafe.Pointer(cstr))
833         v.C = C.LLVMConstStringInContext(c.C, cstr,
834                 C.unsigned(len(str)), boolToLLVMBool(!addnull))
835         return
836 }
837 func (c Context) ConstStruct(constVals []Value, packed bool) (v Value) {
838         ptr, nvals := llvmValueRefs(constVals)
839         v.C = C.LLVMConstStructInContext(c.C, ptr, nvals,
840                 boolToLLVMBool(packed))
841         return
842 }
843 func ConstNamedStruct(t Type, constVals []Value) (v Value) {
844         ptr, nvals := llvmValueRefs(constVals)
845         v.C = C.LLVMConstNamedStruct(t.C, ptr, nvals)
846         return
847 }
848 func ConstString(str string, addnull bool) (v Value) {
849         cstr := C.CString(str)
850         defer C.free(unsafe.Pointer(cstr))
851         v.C = C.LLVMConstString(cstr,
852                 C.unsigned(len(str)), boolToLLVMBool(!addnull))
853         return
854 }
855 func ConstArray(t Type, constVals []Value) (v Value) {
856         ptr, nvals := llvmValueRefs(constVals)
857         v.C = C.LLVMConstArray(t.C, ptr, nvals)
858         return
859 }
860 func ConstStruct(constVals []Value, packed bool) (v Value) {
861         ptr, nvals := llvmValueRefs(constVals)
862         v.C = C.LLVMConstStruct(ptr, nvals, boolToLLVMBool(packed))
863         return
864 }
865 func ConstVector(scalarConstVals []Value, packed bool) (v Value) {
866         ptr, nvals := llvmValueRefs(scalarConstVals)
867         v.C = C.LLVMConstVector(ptr, nvals)
868         return
869 }
870
871 // Constant expressions
872 func (v Value) Opcode() Opcode                { return Opcode(C.LLVMGetConstOpcode(v.C)) }
873 func (v Value) InstructionOpcode() Opcode     { return Opcode(C.LLVMGetInstructionOpcode(v.C)) }
874 func AlignOf(t Type) (v Value)                { v.C = C.LLVMAlignOf(t.C); return }
875 func SizeOf(t Type) (v Value)                 { v.C = C.LLVMSizeOf(t.C); return }
876 func ConstNeg(v Value) (rv Value)             { rv.C = C.LLVMConstNeg(v.C); return }
877 func ConstNSWNeg(v Value) (rv Value)          { rv.C = C.LLVMConstNSWNeg(v.C); return }
878 func ConstNUWNeg(v Value) (rv Value)          { rv.C = C.LLVMConstNUWNeg(v.C); return }
879 func ConstFNeg(v Value) (rv Value)            { rv.C = C.LLVMConstFNeg(v.C); return }
880 func ConstNot(v Value) (rv Value)             { rv.C = C.LLVMConstNot(v.C); return }
881 func ConstAdd(lhs, rhs Value) (v Value)       { v.C = C.LLVMConstAdd(lhs.C, rhs.C); return }
882 func ConstNSWAdd(lhs, rhs Value) (v Value)    { v.C = C.LLVMConstNSWAdd(lhs.C, rhs.C); return }
883 func ConstNUWAdd(lhs, rhs Value) (v Value)    { v.C = C.LLVMConstNUWAdd(lhs.C, rhs.C); return }
884 func ConstFAdd(lhs, rhs Value) (v Value)      { v.C = C.LLVMConstFAdd(lhs.C, rhs.C); return }
885 func ConstSub(lhs, rhs Value) (v Value)       { v.C = C.LLVMConstSub(lhs.C, rhs.C); return }
886 func ConstNSWSub(lhs, rhs Value) (v Value)    { v.C = C.LLVMConstNSWSub(lhs.C, rhs.C); return }
887 func ConstNUWSub(lhs, rhs Value) (v Value)    { v.C = C.LLVMConstNUWSub(lhs.C, rhs.C); return }
888 func ConstFSub(lhs, rhs Value) (v Value)      { v.C = C.LLVMConstFSub(lhs.C, rhs.C); return }
889 func ConstMul(lhs, rhs Value) (v Value)       { v.C = C.LLVMConstMul(lhs.C, rhs.C); return }
890 func ConstNSWMul(lhs, rhs Value) (v Value)    { v.C = C.LLVMConstNSWMul(lhs.C, rhs.C); return }
891 func ConstNUWMul(lhs, rhs Value) (v Value)    { v.C = C.LLVMConstNUWMul(lhs.C, rhs.C); return }
892 func ConstFMul(lhs, rhs Value) (v Value)      { v.C = C.LLVMConstFMul(lhs.C, rhs.C); return }
893 func ConstUDiv(lhs, rhs Value) (v Value)      { v.C = C.LLVMConstUDiv(lhs.C, rhs.C); return }
894 func ConstSDiv(lhs, rhs Value) (v Value)      { v.C = C.LLVMConstSDiv(lhs.C, rhs.C); return }
895 func ConstExactSDiv(lhs, rhs Value) (v Value) { v.C = C.LLVMConstExactSDiv(lhs.C, rhs.C); return }
896 func ConstFDiv(lhs, rhs Value) (v Value)      { v.C = C.LLVMConstFDiv(lhs.C, rhs.C); return }
897 func ConstURem(lhs, rhs Value) (v Value)      { v.C = C.LLVMConstURem(lhs.C, rhs.C); return }
898 func ConstSRem(lhs, rhs Value) (v Value)      { v.C = C.LLVMConstSRem(lhs.C, rhs.C); return }
899 func ConstFRem(lhs, rhs Value) (v Value)      { v.C = C.LLVMConstFRem(lhs.C, rhs.C); return }
900 func ConstAnd(lhs, rhs Value) (v Value)       { v.C = C.LLVMConstAnd(lhs.C, rhs.C); return }
901 func ConstOr(lhs, rhs Value) (v Value)        { v.C = C.LLVMConstOr(lhs.C, rhs.C); return }
902 func ConstXor(lhs, rhs Value) (v Value)       { v.C = C.LLVMConstXor(lhs.C, rhs.C); return }
903
904 func ConstICmp(pred IntPredicate, lhs, rhs Value) (v Value) {
905         v.C = C.LLVMConstICmp(C.LLVMIntPredicate(pred), lhs.C, rhs.C)
906         return
907 }
908 func ConstFCmp(pred FloatPredicate, lhs, rhs Value) (v Value) {
909         v.C = C.LLVMConstFCmp(C.LLVMRealPredicate(pred), lhs.C, rhs.C)
910         return
911 }
912
913 func ConstShl(lhs, rhs Value) (v Value)  { v.C = C.LLVMConstShl(lhs.C, rhs.C); return }
914 func ConstLShr(lhs, rhs Value) (v Value) { v.C = C.LLVMConstLShr(lhs.C, rhs.C); return }
915 func ConstAShr(lhs, rhs Value) (v Value) { v.C = C.LLVMConstAShr(lhs.C, rhs.C); return }
916
917 func ConstGEP(v Value, indices []Value) (rv Value) {
918         ptr, nvals := llvmValueRefs(indices)
919         rv.C = C.LLVMConstGEP(v.C, ptr, nvals)
920         return
921 }
922 func ConstInBoundsGEP(v Value, indices []Value) (rv Value) {
923         ptr, nvals := llvmValueRefs(indices)
924         rv.C = C.LLVMConstInBoundsGEP(v.C, ptr, nvals)
925         return
926 }
927 func ConstTrunc(v Value, t Type) (rv Value)         { rv.C = C.LLVMConstTrunc(v.C, t.C); return }
928 func ConstSExt(v Value, t Type) (rv Value)          { rv.C = C.LLVMConstSExt(v.C, t.C); return }
929 func ConstZExt(v Value, t Type) (rv Value)          { rv.C = C.LLVMConstZExt(v.C, t.C); return }
930 func ConstFPTrunc(v Value, t Type) (rv Value)       { rv.C = C.LLVMConstFPTrunc(v.C, t.C); return }
931 func ConstFPExt(v Value, t Type) (rv Value)         { rv.C = C.LLVMConstFPExt(v.C, t.C); return }
932 func ConstUIToFP(v Value, t Type) (rv Value)        { rv.C = C.LLVMConstUIToFP(v.C, t.C); return }
933 func ConstSIToFP(v Value, t Type) (rv Value)        { rv.C = C.LLVMConstSIToFP(v.C, t.C); return }
934 func ConstFPToUI(v Value, t Type) (rv Value)        { rv.C = C.LLVMConstFPToUI(v.C, t.C); return }
935 func ConstFPToSI(v Value, t Type) (rv Value)        { rv.C = C.LLVMConstFPToSI(v.C, t.C); return }
936 func ConstPtrToInt(v Value, t Type) (rv Value)      { rv.C = C.LLVMConstPtrToInt(v.C, t.C); return }
937 func ConstIntToPtr(v Value, t Type) (rv Value)      { rv.C = C.LLVMConstIntToPtr(v.C, t.C); return }
938 func ConstBitCast(v Value, t Type) (rv Value)       { rv.C = C.LLVMConstBitCast(v.C, t.C); return }
939 func ConstZExtOrBitCast(v Value, t Type) (rv Value) { rv.C = C.LLVMConstZExtOrBitCast(v.C, t.C); return }
940 func ConstSExtOrBitCast(v Value, t Type) (rv Value) { rv.C = C.LLVMConstSExtOrBitCast(v.C, t.C); return }
941 func ConstTruncOrBitCast(v Value, t Type) (rv Value) {
942         rv.C = C.LLVMConstTruncOrBitCast(v.C, t.C)
943         return
944 }
945 func ConstPointerCast(v Value, t Type) (rv Value) { rv.C = C.LLVMConstPointerCast(v.C, t.C); return }
946 func ConstIntCast(v Value, t Type, signed bool) (rv Value) {
947         rv.C = C.LLVMConstIntCast(v.C, t.C, boolToLLVMBool(signed))
948         return
949 }
950 func ConstFPCast(v Value, t Type) (rv Value) { rv.C = C.LLVMConstFPCast(v.C, t.C); return }
951 func ConstSelect(cond, iftrue, iffalse Value) (rv Value) {
952         rv.C = C.LLVMConstSelect(cond.C, iftrue.C, iffalse.C)
953         return
954 }
955 func ConstExtractElement(vec, i Value) (rv Value) {
956         rv.C = C.LLVMConstExtractElement(vec.C, i.C)
957         return
958 }
959 func ConstInsertElement(vec, elem, i Value) (rv Value) {
960         rv.C = C.LLVMConstInsertElement(vec.C, elem.C, i.C)
961         return
962 }
963 func ConstShuffleVector(veca, vecb, mask Value) (rv Value) {
964         rv.C = C.LLVMConstShuffleVector(veca.C, vecb.C, mask.C)
965         return
966 }
967
968 //TODO
969 //LLVMValueRef LLVMConstExtractValue(LLVMValueRef AggConstant, unsigned *IdxList,
970 //                                   unsigned NumIdx);
971
972 func ConstExtractValue(agg Value, indices []uint32) (rv Value) {
973         n := len(indices)
974         if n == 0 {
975                 panic("one or more indices are required")
976         }
977         ptr := (*C.unsigned)(&indices[0])
978         rv.C = C.LLVMConstExtractValue(agg.C, ptr, C.unsigned(n))
979         return
980 }
981
982 func ConstInsertValue(agg, val Value, indices []uint32) (rv Value) {
983         n := len(indices)
984         if n == 0 {
985                 panic("one or more indices are required")
986         }
987         ptr := (*C.unsigned)(&indices[0])
988         rv.C = C.LLVMConstInsertValue(agg.C, val.C, ptr, C.unsigned(n))
989         return
990 }
991
992 func BlockAddress(f Value, bb BasicBlock) (v Value) {
993         v.C = C.LLVMBlockAddress(f.C, bb.C)
994         return
995 }
996
997 // Operations on global variables, functions, and aliases (globals)
998 func (v Value) GlobalParent() (m Module) { m.C = C.LLVMGetGlobalParent(v.C); return }
999 func (v Value) IsDeclaration() bool      { return C.LLVMIsDeclaration(v.C) != 0 }
1000 func (v Value) Linkage() Linkage         { return Linkage(C.LLVMGetLinkage(v.C)) }
1001 func (v Value) SetLinkage(l Linkage)     { C.LLVMSetLinkage(v.C, C.LLVMLinkage(l)) }
1002 func (v Value) Section() string          { return C.GoString(C.LLVMGetSection(v.C)) }
1003 func (v Value) SetSection(str string) {
1004         cstr := C.CString(str)
1005         defer C.free(unsafe.Pointer(cstr))
1006         C.LLVMSetSection(v.C, cstr)
1007 }
1008 func (v Value) Visibility() Visibility      { return Visibility(C.LLVMGetVisibility(v.C)) }
1009 func (v Value) SetVisibility(vi Visibility) { C.LLVMSetVisibility(v.C, C.LLVMVisibility(vi)) }
1010 func (v Value) Alignment() int              { return int(C.LLVMGetAlignment(v.C)) }
1011 func (v Value) SetAlignment(a int)          { C.LLVMSetAlignment(v.C, C.unsigned(a)) }
1012 func (v Value) SetUnnamedAddr(ua bool)      { C.LLVMSetUnnamedAddr(v.C, boolToLLVMBool(ua)) }
1013
1014 // Operations on global variables
1015 func AddGlobal(m Module, t Type, name string) (v Value) {
1016         cname := C.CString(name)
1017         defer C.free(unsafe.Pointer(cname))
1018         v.C = C.LLVMAddGlobal(m.C, t.C, cname)
1019         return
1020 }
1021 func AddGlobalInAddressSpace(m Module, t Type, name string, addressSpace int) (v Value) {
1022         cname := C.CString(name)
1023         defer C.free(unsafe.Pointer(cname))
1024         v.C = C.LLVMAddGlobalInAddressSpace(m.C, t.C, cname, C.unsigned(addressSpace))
1025         return
1026 }
1027 func (m Module) NamedGlobal(name string) (v Value) {
1028         cname := C.CString(name)
1029         defer C.free(unsafe.Pointer(cname))
1030         v.C = C.LLVMGetNamedGlobal(m.C, cname)
1031         return
1032 }
1033
1034 func (m Module) FirstGlobal() (v Value)   { v.C = C.LLVMGetFirstGlobal(m.C); return }
1035 func (m Module) LastGlobal() (v Value)    { v.C = C.LLVMGetLastGlobal(m.C); return }
1036 func NextGlobal(v Value) (rv Value)       { rv.C = C.LLVMGetNextGlobal(v.C); return }
1037 func PrevGlobal(v Value) (rv Value)       { rv.C = C.LLVMGetPreviousGlobal(v.C); return }
1038 func (v Value) EraseFromParentAsGlobal()  { C.LLVMDeleteGlobal(v.C) }
1039 func (v Value) Initializer() (rv Value)   { rv.C = C.LLVMGetInitializer(v.C); return }
1040 func (v Value) SetInitializer(cv Value)   { C.LLVMSetInitializer(v.C, cv.C) }
1041 func (v Value) IsThreadLocal() bool       { return C.LLVMIsThreadLocal(v.C) != 0 }
1042 func (v Value) SetThreadLocal(tl bool)    { C.LLVMSetThreadLocal(v.C, boolToLLVMBool(tl)) }
1043 func (v Value) IsGlobalConstant() bool    { return C.LLVMIsGlobalConstant(v.C) != 0 }
1044 func (v Value) SetGlobalConstant(gc bool) { C.LLVMSetGlobalConstant(v.C, boolToLLVMBool(gc)) }
1045 func (v Value) IsVolatile() bool          { return C.LLVMGetVolatile(v.C) != 0 }
1046 func (v Value) SetVolatile(volatile bool) { C.LLVMSetVolatile(v.C, boolToLLVMBool(volatile)) }
1047
1048 // Operations on aliases
1049 func AddAlias(m Module, t Type, aliasee Value, name string) (v Value) {
1050         cname := C.CString(name)
1051         defer C.free(unsafe.Pointer(cname))
1052         v.C = C.LLVMAddAlias(m.C, t.C, aliasee.C, cname)
1053         return
1054 }
1055
1056 // Operations on comdat
1057 func (m Module) Comdat(name string) (c Comdat) {
1058         cname := C.CString(name)
1059         defer C.free(unsafe.Pointer(cname))
1060         c.C = C.LLVMGetOrInsertComdat(m.C, cname)
1061         return
1062 }
1063
1064 func (v Value) Comdat() (c Comdat) { c.C = C.LLVMGetComdat(v.C); return }
1065 func (v Value) SetComdat(c Comdat) { C.LLVMSetComdat(v.C, c.C) }
1066
1067 func (c Comdat) SelectionKind() ComdatSelectionKind {
1068         return ComdatSelectionKind(C.LLVMGetComdatSelectionKind(c.C))
1069 }
1070
1071 func (c Comdat) SetSelectionKind(k ComdatSelectionKind) {
1072         C.LLVMSetComdatSelectionKind(c.C, (C.LLVMComdatSelectionKind)(k))
1073 }
1074
1075 // Operations on functions
1076 func AddFunction(m Module, name string, ft Type) (v Value) {
1077         cname := C.CString(name)
1078         defer C.free(unsafe.Pointer(cname))
1079         v.C = C.LLVMAddFunction(m.C, cname, ft.C)
1080         return
1081 }
1082
1083 func (m Module) NamedFunction(name string) (v Value) {
1084         cname := C.CString(name)
1085         defer C.free(unsafe.Pointer(cname))
1086         v.C = C.LLVMGetNamedFunction(m.C, cname)
1087         return
1088 }
1089
1090 func (m Module) FirstFunction() (v Value)  { v.C = C.LLVMGetFirstFunction(m.C); return }
1091 func (m Module) LastFunction() (v Value)   { v.C = C.LLVMGetLastFunction(m.C); return }
1092 func NextFunction(v Value) (rv Value)      { rv.C = C.LLVMGetNextFunction(v.C); return }
1093 func PrevFunction(v Value) (rv Value)      { rv.C = C.LLVMGetPreviousFunction(v.C); return }
1094 func (v Value) EraseFromParentAsFunction() { C.LLVMDeleteFunction(v.C) }
1095 func (v Value) IntrinsicID() int           { return int(C.LLVMGetIntrinsicID(v.C)) }
1096 func (v Value) FunctionCallConv() CallConv {
1097         return CallConv(C.LLVMCallConv(C.LLVMGetFunctionCallConv(v.C)))
1098 }
1099 func (v Value) SetFunctionCallConv(cc CallConv) { C.LLVMSetFunctionCallConv(v.C, C.unsigned(cc)) }
1100 func (v Value) GC() string                      { return C.GoString(C.LLVMGetGC(v.C)) }
1101 func (v Value) SetGC(name string) {
1102         cname := C.CString(name)
1103         defer C.free(unsafe.Pointer(cname))
1104         C.LLVMSetGC(v.C, cname)
1105 }
1106 func (v Value) AddAttributeAtIndex(i int, a Attribute) {
1107   C.LLVMAddAttributeAtIndex(v.C, C.LLVMAttributeIndex(i), a.C)
1108 }
1109 func (v Value) AddFunctionAttr(a Attribute) {
1110   v.AddAttributeAtIndex(C.LLVMAttributeFunctionIndex, a);
1111 }
1112 func (v Value) GetEnumAttributeAtIndex(i int, kind uint) (a Attribute) {
1113   a.C = C.LLVMGetEnumAttributeAtIndex(v.C, C.LLVMAttributeIndex(i), C.unsigned(kind))
1114   return
1115 }
1116 func (v Value) GetEnumFunctionAttribute(kind uint) Attribute {
1117   return v.GetEnumAttributeAtIndex(C.LLVMAttributeFunctionIndex, kind)
1118 }
1119 func (v Value) GetStringAttributeAtIndex(i int, kind string) (a Attribute) {
1120   ckind := C.CString(kind)
1121   defer C.free(unsafe.Pointer(ckind))
1122   a.C = C.LLVMGetStringAttributeAtIndex(v.C, C.LLVMAttributeIndex(i),
1123                                         ckind, C.unsigned(len(kind)))
1124   return
1125 }
1126 func (v Value) RemoveEnumAttributeAtIndex(i int, kind uint) {
1127   C.LLVMRemoveEnumAttributeAtIndex(v.C, C.LLVMAttributeIndex(i), C.unsigned(kind))
1128 }
1129 func (v Value) RemoveEnumFunctionAttribute(kind uint) {
1130   v.RemoveEnumAttributeAtIndex(C.LLVMAttributeFunctionIndex, kind);
1131 }
1132 func (v Value) RemoveStringAttributeAtIndex(i int, kind string) {
1133   ckind := C.CString(kind)
1134   defer C.free(unsafe.Pointer(ckind))
1135   C.LLVMRemoveStringAttributeAtIndex(v.C, C.LLVMAttributeIndex(i),
1136                                      ckind, C.unsigned(len(kind)))
1137 }
1138 func (v Value) AddTargetDependentFunctionAttr(attr, value string) {
1139         cattr := C.CString(attr)
1140         defer C.free(unsafe.Pointer(cattr))
1141         cvalue := C.CString(value)
1142         defer C.free(unsafe.Pointer(cvalue))
1143         C.LLVMAddTargetDependentFunctionAttr(v.C, cattr, cvalue)
1144 }
1145 func (v Value) SetPersonality(p Value) {
1146         C.LLVMSetPersonalityFn(v.C, p.C)
1147 }
1148
1149 // Operations on parameters
1150 func (v Value) ParamsCount() int { return int(C.LLVMCountParams(v.C)) }
1151 func (v Value) Params() []Value {
1152         out := make([]Value, v.ParamsCount())
1153         if len(out) > 0 {
1154                 C.LLVMGetParams(v.C, llvmValueRefPtr(&out[0]))
1155         }
1156         return out
1157 }
1158 func (v Value) Param(i int) (rv Value)  { rv.C = C.LLVMGetParam(v.C, C.unsigned(i)); return }
1159 func (v Value) ParamParent() (rv Value) { rv.C = C.LLVMGetParamParent(v.C); return }
1160 func (v Value) FirstParam() (rv Value)  { rv.C = C.LLVMGetFirstParam(v.C); return }
1161 func (v Value) LastParam() (rv Value)   { rv.C = C.LLVMGetLastParam(v.C); return }
1162 func NextParam(v Value) (rv Value)      { rv.C = C.LLVMGetNextParam(v.C); return }
1163 func PrevParam(v Value) (rv Value)      { rv.C = C.LLVMGetPreviousParam(v.C); return }
1164 func (v Value) SetParamAlignment(align int) { C.LLVMSetParamAlignment(v.C, C.unsigned(align)) }
1165
1166 // Operations on basic blocks
1167 func (bb BasicBlock) AsValue() (v Value)      { v.C = C.LLVMBasicBlockAsValue(bb.C); return }
1168 func (v Value) IsBasicBlock() bool            { return C.LLVMValueIsBasicBlock(v.C) != 0 }
1169 func (v Value) AsBasicBlock() (bb BasicBlock) { bb.C = C.LLVMValueAsBasicBlock(v.C); return }
1170 func (bb BasicBlock) Parent() (v Value)       { v.C = C.LLVMGetBasicBlockParent(bb.C); return }
1171 func (v Value) BasicBlocksCount() int         { return int(C.LLVMCountBasicBlocks(v.C)) }
1172 func (v Value) BasicBlocks() []BasicBlock {
1173         out := make([]BasicBlock, v.BasicBlocksCount())
1174         C.LLVMGetBasicBlocks(v.C, llvmBasicBlockRefPtr(&out[0]))
1175         return out
1176 }
1177 func (v Value) FirstBasicBlock() (bb BasicBlock)    { bb.C = C.LLVMGetFirstBasicBlock(v.C); return }
1178 func (v Value) LastBasicBlock() (bb BasicBlock)     { bb.C = C.LLVMGetLastBasicBlock(v.C); return }
1179 func NextBasicBlock(bb BasicBlock) (rbb BasicBlock) { rbb.C = C.LLVMGetNextBasicBlock(bb.C); return }
1180 func PrevBasicBlock(bb BasicBlock) (rbb BasicBlock) { rbb.C = C.LLVMGetPreviousBasicBlock(bb.C); return }
1181 func (v Value) EntryBasicBlock() (bb BasicBlock)    { bb.C = C.LLVMGetEntryBasicBlock(v.C); return }
1182 func (c Context) AddBasicBlock(f Value, name string) (bb BasicBlock) {
1183         cname := C.CString(name)
1184         defer C.free(unsafe.Pointer(cname))
1185         bb.C = C.LLVMAppendBasicBlockInContext(c.C, f.C, cname)
1186         return
1187 }
1188 func (c Context) InsertBasicBlock(ref BasicBlock, name string) (bb BasicBlock) {
1189         cname := C.CString(name)
1190         defer C.free(unsafe.Pointer(cname))
1191         bb.C = C.LLVMInsertBasicBlockInContext(c.C, ref.C, cname)
1192         return
1193 }
1194 func AddBasicBlock(f Value, name string) (bb BasicBlock) {
1195         cname := C.CString(name)
1196         defer C.free(unsafe.Pointer(cname))
1197         bb.C = C.LLVMAppendBasicBlock(f.C, cname)
1198         return
1199 }
1200 func InsertBasicBlock(ref BasicBlock, name string) (bb BasicBlock) {
1201         cname := C.CString(name)
1202         defer C.free(unsafe.Pointer(cname))
1203         bb.C = C.LLVMInsertBasicBlock(ref.C, cname)
1204         return
1205 }
1206 func (bb BasicBlock) EraseFromParent()          { C.LLVMDeleteBasicBlock(bb.C) }
1207 func (bb BasicBlock) MoveBefore(pos BasicBlock) { C.LLVMMoveBasicBlockBefore(bb.C, pos.C) }
1208 func (bb BasicBlock) MoveAfter(pos BasicBlock)  { C.LLVMMoveBasicBlockAfter(bb.C, pos.C) }
1209
1210 // Operations on instructions
1211 func (v Value) InstructionParent() (bb BasicBlock) { bb.C = C.LLVMGetInstructionParent(v.C); return }
1212 func (bb BasicBlock) FirstInstruction() (v Value)  { v.C = C.LLVMGetFirstInstruction(bb.C); return }
1213 func (bb BasicBlock) LastInstruction() (v Value)   { v.C = C.LLVMGetLastInstruction(bb.C); return }
1214 func NextInstruction(v Value) (rv Value)           { rv.C = C.LLVMGetNextInstruction(v.C); return }
1215 func PrevInstruction(v Value) (rv Value)           { rv.C = C.LLVMGetPreviousInstruction(v.C); return }
1216
1217 // Operations on call sites
1218 func (v Value) SetInstructionCallConv(cc CallConv) {
1219         C.LLVMSetInstructionCallConv(v.C, C.unsigned(cc))
1220 }
1221 func (v Value) InstructionCallConv() CallConv {
1222         return CallConv(C.LLVMCallConv(C.LLVMGetInstructionCallConv(v.C)))
1223 }
1224 func (v Value) AddCallSiteAttribute(i int, a Attribute) {
1225         C.LLVMAddCallSiteAttribute(v.C, C.LLVMAttributeIndex(i), a.C)
1226 }
1227 func (v Value) SetInstrParamAlignment(i int, align int) {
1228         C.LLVMSetInstrParamAlignment(v.C, C.unsigned(i), C.unsigned(align))
1229 }
1230
1231 // Operations on call instructions (only)
1232 func (v Value) IsTailCall() bool    { return C.LLVMIsTailCall(v.C) != 0 }
1233 func (v Value) SetTailCall(is bool) { C.LLVMSetTailCall(v.C, boolToLLVMBool(is)) }
1234
1235 // Operations on phi nodes
1236 func (v Value) AddIncoming(vals []Value, blocks []BasicBlock) {
1237         ptr, nvals := llvmValueRefs(vals)
1238         C.LLVMAddIncoming(v.C, ptr, llvmBasicBlockRefPtr(&blocks[0]), nvals)
1239 }
1240 func (v Value) IncomingCount() int { return int(C.LLVMCountIncoming(v.C)) }
1241 func (v Value) IncomingValue(i int) (rv Value) {
1242         rv.C = C.LLVMGetIncomingValue(v.C, C.unsigned(i))
1243         return
1244 }
1245 func (v Value) IncomingBlock(i int) (bb BasicBlock) {
1246         bb.C = C.LLVMGetIncomingBlock(v.C, C.unsigned(i))
1247         return
1248 }
1249
1250 // Operations on inline assembly
1251 func InlineAsm(t Type, asmString, constraints string, hasSideEffects, isAlignStack bool, dialect InlineAsmDialect) (rv Value) {
1252         casm := C.CString(asmString)
1253         defer C.free(unsafe.Pointer(casm))
1254         cconstraints := C.CString(constraints)
1255         defer C.free(unsafe.Pointer(cconstraints))
1256         rv.C = C.LLVMGetInlineAsm(t.C, casm, C.size_t(len(asmString)), cconstraints, C.size_t(len(constraints)), boolToLLVMBool(hasSideEffects), boolToLLVMBool(isAlignStack), C.LLVMInlineAsmDialect(dialect))
1257         return
1258 }
1259
1260 // Operations on aggregates
1261 func (v Value) Indices() []uint32 {
1262         num := C.LLVMGetNumIndices(v.C)
1263         indicesPtr := C.LLVMGetIndices(v.C)
1264         // https://github.com/golang/go/wiki/cgo#turning-c-arrays-into-go-slices
1265         rawIndices := (*[1 << 20]C.uint)(unsafe.Pointer(indicesPtr))[:num:num]
1266         indices := make([]uint32, num)
1267         for i := range indices {
1268                 indices[i] = uint32(rawIndices[i])
1269         }
1270         return indices
1271 }
1272
1273 //-------------------------------------------------------------------------
1274 // llvm.Builder
1275 //-------------------------------------------------------------------------
1276
1277 // An instruction builder represents a point within a basic block, and is the
1278 // exclusive means of building instructions using the C interface.
1279
1280 func (c Context) NewBuilder() (b Builder) { b.C = C.LLVMCreateBuilderInContext(c.C); return }
1281 func NewBuilder() (b Builder)             { b.C = C.LLVMCreateBuilder(); return }
1282 func (b Builder) SetInsertPoint(block BasicBlock, instr Value) {
1283         C.LLVMPositionBuilder(b.C, block.C, instr.C)
1284 }
1285 func (b Builder) SetInsertPointBefore(instr Value)     { C.LLVMPositionBuilderBefore(b.C, instr.C) }
1286 func (b Builder) SetInsertPointAtEnd(block BasicBlock) { C.LLVMPositionBuilderAtEnd(b.C, block.C) }
1287 func (b Builder) GetInsertBlock() (bb BasicBlock)      { bb.C = C.LLVMGetInsertBlock(b.C); return }
1288 func (b Builder) ClearInsertionPoint()                 { C.LLVMClearInsertionPosition(b.C) }
1289 func (b Builder) Insert(instr Value)                   { C.LLVMInsertIntoBuilder(b.C, instr.C) }
1290 func (b Builder) InsertWithName(instr Value, name string) {
1291         cname := C.CString(name)
1292         defer C.free(unsafe.Pointer(cname))
1293         C.LLVMInsertIntoBuilderWithName(b.C, instr.C, cname)
1294 }
1295 func (b Builder) Dispose() { C.LLVMDisposeBuilder(b.C) }
1296
1297 // Metadata
1298 type DebugLoc struct {
1299         Line, Col      uint
1300         Scope          Metadata
1301         InlinedAt      Metadata
1302 }
1303 func (b Builder) SetCurrentDebugLocation(line, col uint, scope, inlinedAt Metadata) {
1304         C.LLVMGoSetCurrentDebugLocation(b.C, C.unsigned(line), C.unsigned(col), scope.C, inlinedAt.C)
1305 }
1306 // Get current debug location. Please do not call this function until setting debug location with SetCurrentDebugLocation()
1307 func (b Builder) GetCurrentDebugLocation() (loc DebugLoc) {
1308         md := C.LLVMGoGetCurrentDebugLocation(b.C)
1309         loc.Line = uint(md.Line)
1310         loc.Col = uint(md.Col)
1311         loc.Scope = Metadata{C: md.Scope}
1312         loc.InlinedAt = Metadata{C: md.InlinedAt}
1313         return
1314 }
1315 func (b Builder) SetInstDebugLocation(v Value) { C.LLVMSetInstDebugLocation(b.C, v.C) }
1316 func (b Builder) InsertDeclare(module Module, storage Value, md Value) Value {
1317         f := module.NamedFunction("llvm.dbg.declare")
1318         if f.IsNil() {
1319                 ftyp := FunctionType(VoidType(), []Type{storage.Type(), md.Type()}, false)
1320                 f = AddFunction(module, "llvm.dbg.declare", ftyp)
1321         }
1322         return b.CreateCall(f, []Value{storage, md}, "")
1323 }
1324
1325 // Terminators
1326 func (b Builder) CreateRetVoid() (rv Value)    { rv.C = C.LLVMBuildRetVoid(b.C); return }
1327 func (b Builder) CreateRet(v Value) (rv Value) { rv.C = C.LLVMBuildRet(b.C, v.C); return }
1328 func (b Builder) CreateAggregateRet(vs []Value) (rv Value) {
1329         ptr, nvals := llvmValueRefs(vs)
1330         rv.C = C.LLVMBuildAggregateRet(b.C, ptr, nvals)
1331         return
1332 }
1333 func (b Builder) CreateBr(bb BasicBlock) (rv Value) { rv.C = C.LLVMBuildBr(b.C, bb.C); return }
1334 func (b Builder) CreateCondBr(ifv Value, thenb, elseb BasicBlock) (rv Value) {
1335         rv.C = C.LLVMBuildCondBr(b.C, ifv.C, thenb.C, elseb.C)
1336         return
1337 }
1338 func (b Builder) CreateSwitch(v Value, elseb BasicBlock, numCases int) (rv Value) {
1339         rv.C = C.LLVMBuildSwitch(b.C, v.C, elseb.C, C.unsigned(numCases))
1340         return
1341 }
1342 func (b Builder) CreateIndirectBr(addr Value, numDests int) (rv Value) {
1343         rv.C = C.LLVMBuildIndirectBr(b.C, addr.C, C.unsigned(numDests))
1344         return
1345 }
1346 func (b Builder) CreateInvoke(fn Value, args []Value, then, catch BasicBlock, name string) (rv Value) {
1347         cname := C.CString(name)
1348         defer C.free(unsafe.Pointer(cname))
1349         ptr, nvals := llvmValueRefs(args)
1350         rv.C = C.LLVMBuildInvoke(b.C, fn.C, ptr, nvals, then.C, catch.C, cname)
1351         return
1352 }
1353 func (b Builder) CreateUnreachable() (rv Value) { rv.C = C.LLVMBuildUnreachable(b.C); return }
1354
1355 // Add a case to the switch instruction
1356 func (v Value) AddCase(on Value, dest BasicBlock) { C.LLVMAddCase(v.C, on.C, dest.C) }
1357
1358 // Add a destination to the indirectbr instruction
1359 func (v Value) AddDest(dest BasicBlock) { C.LLVMAddDestination(v.C, dest.C) }
1360
1361 // Arithmetic
1362 func (b Builder) CreateAdd(lhs, rhs Value, name string) (v Value) {
1363         cname := C.CString(name)
1364         defer C.free(unsafe.Pointer(cname))
1365         v.C = C.LLVMBuildAdd(b.C, lhs.C, rhs.C, cname)
1366         return
1367 }
1368 func (b Builder) CreateNSWAdd(lhs, rhs Value, name string) (v Value) {
1369         cname := C.CString(name)
1370         defer C.free(unsafe.Pointer(cname))
1371         v.C = C.LLVMBuildNSWAdd(b.C, lhs.C, rhs.C, cname)
1372         return
1373 }
1374 func (b Builder) CreateNUWAdd(lhs, rhs Value, name string) (v Value) {
1375         cname := C.CString(name)
1376         defer C.free(unsafe.Pointer(cname))
1377         v.C = C.LLVMBuildNUWAdd(b.C, lhs.C, rhs.C, cname)
1378         return
1379 }
1380 func (b Builder) CreateFAdd(lhs, rhs Value, name string) (v Value) {
1381         cname := C.CString(name)
1382         defer C.free(unsafe.Pointer(cname))
1383         v.C = C.LLVMBuildFAdd(b.C, lhs.C, rhs.C, cname)
1384         return
1385 }
1386 func (b Builder) CreateSub(lhs, rhs Value, name string) (v Value) {
1387         cname := C.CString(name)
1388         defer C.free(unsafe.Pointer(cname))
1389         v.C = C.LLVMBuildSub(b.C, lhs.C, rhs.C, cname)
1390         return
1391 }
1392 func (b Builder) CreateNSWSub(lhs, rhs Value, name string) (v Value) {
1393         cname := C.CString(name)
1394         defer C.free(unsafe.Pointer(cname))
1395         v.C = C.LLVMBuildNSWSub(b.C, lhs.C, rhs.C, cname)
1396         return
1397 }
1398 func (b Builder) CreateNUWSub(lhs, rhs Value, name string) (v Value) {
1399         cname := C.CString(name)
1400         defer C.free(unsafe.Pointer(cname))
1401         v.C = C.LLVMBuildNUWSub(b.C, lhs.C, rhs.C, cname)
1402         return
1403 }
1404 func (b Builder) CreateFSub(lhs, rhs Value, name string) (v Value) {
1405         cname := C.CString(name)
1406         v.C = C.LLVMBuildFSub(b.C, lhs.C, rhs.C, cname)
1407         C.free(unsafe.Pointer(cname))
1408         return
1409 }
1410 func (b Builder) CreateMul(lhs, rhs Value, name string) (v Value) {
1411         cname := C.CString(name)
1412         defer C.free(unsafe.Pointer(cname))
1413         v.C = C.LLVMBuildMul(b.C, lhs.C, rhs.C, cname)
1414         return
1415 }
1416 func (b Builder) CreateNSWMul(lhs, rhs Value, name string) (v Value) {
1417         cname := C.CString(name)
1418         defer C.free(unsafe.Pointer(cname))
1419         v.C = C.LLVMBuildNSWMul(b.C, lhs.C, rhs.C, cname)
1420         return
1421 }
1422 func (b Builder) CreateNUWMul(lhs, rhs Value, name string) (v Value) {
1423         cname := C.CString(name)
1424         defer C.free(unsafe.Pointer(cname))
1425         v.C = C.LLVMBuildNUWMul(b.C, lhs.C, rhs.C, cname)
1426         return
1427 }
1428 func (b Builder) CreateFMul(lhs, rhs Value, name string) (v Value) {
1429         cname := C.CString(name)
1430         defer C.free(unsafe.Pointer(cname))
1431         v.C = C.LLVMBuildFMul(b.C, lhs.C, rhs.C, cname)
1432         return
1433 }
1434 func (b Builder) CreateUDiv(lhs, rhs Value, name string) (v Value) {
1435         cname := C.CString(name)
1436         defer C.free(unsafe.Pointer(cname))
1437         v.C = C.LLVMBuildUDiv(b.C, lhs.C, rhs.C, cname)
1438         return
1439 }
1440 func (b Builder) CreateSDiv(lhs, rhs Value, name string) (v Value) {
1441         cname := C.CString(name)
1442         defer C.free(unsafe.Pointer(cname))
1443         v.C = C.LLVMBuildSDiv(b.C, lhs.C, rhs.C, cname)
1444         return
1445 }
1446 func (b Builder) CreateExactSDiv(lhs, rhs Value, name string) (v Value) {
1447         cname := C.CString(name)
1448         defer C.free(unsafe.Pointer(cname))
1449         v.C = C.LLVMBuildExactSDiv(b.C, lhs.C, rhs.C, cname)
1450         return
1451 }
1452 func (b Builder) CreateFDiv(lhs, rhs Value, name string) (v Value) {
1453         cname := C.CString(name)
1454         defer C.free(unsafe.Pointer(cname))
1455         v.C = C.LLVMBuildFDiv(b.C, lhs.C, rhs.C, cname)
1456         return
1457 }
1458 func (b Builder) CreateURem(lhs, rhs Value, name string) (v Value) {
1459         cname := C.CString(name)
1460         defer C.free(unsafe.Pointer(cname))
1461         v.C = C.LLVMBuildURem(b.C, lhs.C, rhs.C, cname)
1462         return
1463 }
1464 func (b Builder) CreateSRem(lhs, rhs Value, name string) (v Value) {
1465         cname := C.CString(name)
1466         defer C.free(unsafe.Pointer(cname))
1467         v.C = C.LLVMBuildSRem(b.C, lhs.C, rhs.C, cname)
1468         return
1469 }
1470 func (b Builder) CreateFRem(lhs, rhs Value, name string) (v Value) {
1471         cname := C.CString(name)
1472         defer C.free(unsafe.Pointer(cname))
1473         v.C = C.LLVMBuildFRem(b.C, lhs.C, rhs.C, cname)
1474         return
1475 }
1476 func (b Builder) CreateShl(lhs, rhs Value, name string) (v Value) {
1477         cname := C.CString(name)
1478         defer C.free(unsafe.Pointer(cname))
1479         v.C = C.LLVMBuildShl(b.C, lhs.C, rhs.C, cname)
1480         return
1481 }
1482 func (b Builder) CreateLShr(lhs, rhs Value, name string) (v Value) {
1483         cname := C.CString(name)
1484         defer C.free(unsafe.Pointer(cname))
1485         v.C = C.LLVMBuildLShr(b.C, lhs.C, rhs.C, cname)
1486         return
1487 }
1488 func (b Builder) CreateAShr(lhs, rhs Value, name string) (v Value) {
1489         cname := C.CString(name)
1490         defer C.free(unsafe.Pointer(cname))
1491         v.C = C.LLVMBuildAShr(b.C, lhs.C, rhs.C, cname)
1492         return
1493 }
1494 func (b Builder) CreateAnd(lhs, rhs Value, name string) (v Value) {
1495         cname := C.CString(name)
1496         defer C.free(unsafe.Pointer(cname))
1497         v.C = C.LLVMBuildAnd(b.C, lhs.C, rhs.C, cname)
1498         return
1499 }
1500 func (b Builder) CreateOr(lhs, rhs Value, name string) (v Value) {
1501         cname := C.CString(name)
1502         defer C.free(unsafe.Pointer(cname))
1503         v.C = C.LLVMBuildOr(b.C, lhs.C, rhs.C, cname)
1504         return
1505 }
1506 func (b Builder) CreateXor(lhs, rhs Value, name string) (v Value) {
1507         cname := C.CString(name)
1508         defer C.free(unsafe.Pointer(cname))
1509         v.C = C.LLVMBuildXor(b.C, lhs.C, rhs.C, cname)
1510         return
1511 }
1512 func (b Builder) CreateBinOp(op Opcode, lhs, rhs Value, name string) (v Value) {
1513         cname := C.CString(name)
1514         defer C.free(unsafe.Pointer(cname))
1515         v.C = C.LLVMBuildBinOp(b.C, C.LLVMOpcode(op), lhs.C, rhs.C, cname)
1516         return
1517 }
1518 func (b Builder) CreateNeg(v Value, name string) (rv Value) {
1519         cname := C.CString(name)
1520         defer C.free(unsafe.Pointer(cname))
1521         rv.C = C.LLVMBuildNeg(b.C, v.C, cname)
1522         return
1523 }
1524 func (b Builder) CreateNSWNeg(v Value, name string) (rv Value) {
1525         cname := C.CString(name)
1526         defer C.free(unsafe.Pointer(cname))
1527         rv.C = C.LLVMBuildNSWNeg(b.C, v.C, cname)
1528         return
1529 }
1530 func (b Builder) CreateNUWNeg(v Value, name string) (rv Value) {
1531         cname := C.CString(name)
1532         defer C.free(unsafe.Pointer(cname))
1533         rv.C = C.LLVMBuildNUWNeg(b.C, v.C, cname)
1534         return
1535 }
1536 func (b Builder) CreateFNeg(v Value, name string) (rv Value) {
1537         cname := C.CString(name)
1538         defer C.free(unsafe.Pointer(cname))
1539         rv.C = C.LLVMBuildFNeg(b.C, v.C, cname)
1540         return
1541 }
1542 func (b Builder) CreateNot(v Value, name string) (rv Value) {
1543         cname := C.CString(name)
1544         defer C.free(unsafe.Pointer(cname))
1545         rv.C = C.LLVMBuildNot(b.C, v.C, cname)
1546         return
1547 }
1548
1549 // Memory
1550
1551 func (b Builder) CreateMalloc(t Type, name string) (v Value) {
1552         cname := C.CString(name)
1553         defer C.free(unsafe.Pointer(cname))
1554         v.C = C.LLVMBuildMalloc(b.C, t.C, cname)
1555         return
1556 }
1557 func (b Builder) CreateArrayMalloc(t Type, val Value, name string) (v Value) {
1558         cname := C.CString(name)
1559         defer C.free(unsafe.Pointer(cname))
1560         v.C = C.LLVMBuildArrayMalloc(b.C, t.C, val.C, cname)
1561         return
1562 }
1563 func (b Builder) CreateAlloca(t Type, name string) (v Value) {
1564         cname := C.CString(name)
1565         defer C.free(unsafe.Pointer(cname))
1566         v.C = C.LLVMBuildAlloca(b.C, t.C, cname)
1567         return
1568 }
1569 func (b Builder) CreateArrayAlloca(t Type, val Value, name string) (v Value) {
1570         cname := C.CString(name)
1571         defer C.free(unsafe.Pointer(cname))
1572         v.C = C.LLVMBuildArrayAlloca(b.C, t.C, val.C, cname)
1573         return
1574 }
1575 func (b Builder) CreateFree(p Value) (v Value) {
1576         v.C = C.LLVMBuildFree(b.C, p.C)
1577         return
1578 }
1579 func (b Builder) CreateLoad(p Value, name string) (v Value) {
1580         cname := C.CString(name)
1581         defer C.free(unsafe.Pointer(cname))
1582         v.C = C.LLVMBuildLoad(b.C, p.C, cname)
1583         return
1584 }
1585 func (b Builder) CreateStore(val Value, p Value) (v Value) {
1586         v.C = C.LLVMBuildStore(b.C, val.C, p.C)
1587         return
1588 }
1589 func (b Builder) CreateGEP(p Value, indices []Value, name string) (v Value) {
1590         cname := C.CString(name)
1591         defer C.free(unsafe.Pointer(cname))
1592         ptr, nvals := llvmValueRefs(indices)
1593         v.C = C.LLVMBuildGEP(b.C, p.C, ptr, nvals, cname)
1594         return
1595 }
1596 func (b Builder) CreateInBoundsGEP(p Value, indices []Value, name string) (v Value) {
1597         cname := C.CString(name)
1598         defer C.free(unsafe.Pointer(cname))
1599         ptr, nvals := llvmValueRefs(indices)
1600         v.C = C.LLVMBuildInBoundsGEP(b.C, p.C, ptr, nvals, cname)
1601         return
1602 }
1603 func (b Builder) CreateStructGEP(p Value, i int, name string) (v Value) {
1604         cname := C.CString(name)
1605         defer C.free(unsafe.Pointer(cname))
1606         v.C = C.LLVMBuildStructGEP(b.C, p.C, C.unsigned(i), cname)
1607         return
1608 }
1609 func (b Builder) CreateGlobalString(str, name string) (v Value) {
1610         cstr := C.CString(str)
1611         defer C.free(unsafe.Pointer(cstr))
1612         cname := C.CString(name)
1613         defer C.free(unsafe.Pointer(cname))
1614         v.C = C.LLVMBuildGlobalString(b.C, cstr, cname)
1615         return
1616 }
1617 func (b Builder) CreateGlobalStringPtr(str, name string) (v Value) {
1618         cstr := C.CString(str)
1619         defer C.free(unsafe.Pointer(cstr))
1620         cname := C.CString(name)
1621         defer C.free(unsafe.Pointer(cname))
1622         v.C = C.LLVMBuildGlobalStringPtr(b.C, cstr, cname)
1623         return
1624 }
1625
1626 // Casts
1627 func (b Builder) CreateTrunc(val Value, t Type, name string) (v Value) {
1628         cname := C.CString(name)
1629         defer C.free(unsafe.Pointer(cname))
1630         v.C = C.LLVMBuildTrunc(b.C, val.C, t.C, cname)
1631         return
1632 }
1633 func (b Builder) CreateZExt(val Value, t Type, name string) (v Value) {
1634         cname := C.CString(name)
1635         defer C.free(unsafe.Pointer(cname))
1636         v.C = C.LLVMBuildZExt(b.C, val.C, t.C, cname)
1637         return
1638 }
1639 func (b Builder) CreateSExt(val Value, t Type, name string) (v Value) {
1640         cname := C.CString(name)
1641         defer C.free(unsafe.Pointer(cname))
1642         v.C = C.LLVMBuildSExt(b.C, val.C, t.C, cname)
1643         return
1644 }
1645 func (b Builder) CreateFPToUI(val Value, t Type, name string) (v Value) {
1646         cname := C.CString(name)
1647         defer C.free(unsafe.Pointer(cname))
1648         v.C = C.LLVMBuildFPToUI(b.C, val.C, t.C, cname)
1649         return
1650 }
1651 func (b Builder) CreateFPToSI(val Value, t Type, name string) (v Value) {
1652         cname := C.CString(name)
1653         defer C.free(unsafe.Pointer(cname))
1654         v.C = C.LLVMBuildFPToSI(b.C, val.C, t.C, cname)
1655         return
1656 }
1657 func (b Builder) CreateUIToFP(val Value, t Type, name string) (v Value) {
1658         cname := C.CString(name)
1659         defer C.free(unsafe.Pointer(cname))
1660         v.C = C.LLVMBuildUIToFP(b.C, val.C, t.C, cname)
1661         return
1662 }
1663 func (b Builder) CreateSIToFP(val Value, t Type, name string) (v Value) {
1664         cname := C.CString(name)
1665         defer C.free(unsafe.Pointer(cname))
1666         v.C = C.LLVMBuildSIToFP(b.C, val.C, t.C, cname)
1667         return
1668 }
1669 func (b Builder) CreateFPTrunc(val Value, t Type, name string) (v Value) {
1670         cname := C.CString(name)
1671         defer C.free(unsafe.Pointer(cname))
1672         v.C = C.LLVMBuildFPTrunc(b.C, val.C, t.C, cname)
1673         return
1674 }
1675 func (b Builder) CreateFPExt(val Value, t Type, name string) (v Value) {
1676         cname := C.CString(name)
1677         defer C.free(unsafe.Pointer(cname))
1678         v.C = C.LLVMBuildFPExt(b.C, val.C, t.C, cname)
1679         return
1680 }
1681 func (b Builder) CreatePtrToInt(val Value, t Type, name string) (v Value) {
1682         cname := C.CString(name)
1683         defer C.free(unsafe.Pointer(cname))
1684         v.C = C.LLVMBuildPtrToInt(b.C, val.C, t.C, cname)
1685         return
1686 }
1687 func (b Builder) CreateIntToPtr(val Value, t Type, name string) (v Value) {
1688         cname := C.CString(name)
1689         defer C.free(unsafe.Pointer(cname))
1690         v.C = C.LLVMBuildIntToPtr(b.C, val.C, t.C, cname)
1691         return
1692 }
1693 func (b Builder) CreateBitCast(val Value, t Type, name string) (v Value) {
1694         cname := C.CString(name)
1695         defer C.free(unsafe.Pointer(cname))
1696         v.C = C.LLVMBuildBitCast(b.C, val.C, t.C, cname)
1697         return
1698 }
1699 func (b Builder) CreateZExtOrBitCast(val Value, t Type, name string) (v Value) {
1700         cname := C.CString(name)
1701         defer C.free(unsafe.Pointer(cname))
1702         v.C = C.LLVMBuildZExtOrBitCast(b.C, val.C, t.C, cname)
1703         return
1704 }
1705 func (b Builder) CreateSExtOrBitCast(val Value, t Type, name string) (v Value) {
1706         cname := C.CString(name)
1707         defer C.free(unsafe.Pointer(cname))
1708         v.C = C.LLVMBuildSExtOrBitCast(b.C, val.C, t.C, cname)
1709         return
1710 }
1711 func (b Builder) CreateTruncOrBitCast(val Value, t Type, name string) (v Value) {
1712         cname := C.CString(name)
1713         defer C.free(unsafe.Pointer(cname))
1714         v.C = C.LLVMBuildTruncOrBitCast(b.C, val.C, t.C, cname)
1715         return
1716 }
1717 func (b Builder) CreateCast(val Value, op Opcode, t Type, name string) (v Value) {
1718         cname := C.CString(name)
1719         defer C.free(unsafe.Pointer(cname))
1720         v.C = C.LLVMBuildCast(b.C, C.LLVMOpcode(op), val.C, t.C, cname)
1721         return
1722 } //
1723 func (b Builder) CreatePointerCast(val Value, t Type, name string) (v Value) {
1724         cname := C.CString(name)
1725         defer C.free(unsafe.Pointer(cname))
1726         v.C = C.LLVMBuildPointerCast(b.C, val.C, t.C, cname)
1727         return
1728 }
1729 func (b Builder) CreateIntCast(val Value, t Type, name string) (v Value) {
1730         cname := C.CString(name)
1731         defer C.free(unsafe.Pointer(cname))
1732         v.C = C.LLVMBuildIntCast(b.C, val.C, t.C, cname)
1733         return
1734 }
1735 func (b Builder) CreateFPCast(val Value, t Type, name string) (v Value) {
1736         cname := C.CString(name)
1737         defer C.free(unsafe.Pointer(cname))
1738         v.C = C.LLVMBuildFPCast(b.C, val.C, t.C, cname)
1739         return
1740 }
1741
1742 // Comparisons
1743 func (b Builder) CreateICmp(pred IntPredicate, lhs, rhs Value, name string) (v Value) {
1744         cname := C.CString(name)
1745         defer C.free(unsafe.Pointer(cname))
1746         v.C = C.LLVMBuildICmp(b.C, C.LLVMIntPredicate(pred), lhs.C, rhs.C, cname)
1747         return
1748 }
1749 func (b Builder) CreateFCmp(pred FloatPredicate, lhs, rhs Value, name string) (v Value) {
1750         cname := C.CString(name)
1751         defer C.free(unsafe.Pointer(cname))
1752         v.C = C.LLVMBuildFCmp(b.C, C.LLVMRealPredicate(pred), lhs.C, rhs.C, cname)
1753         return
1754 }
1755
1756 // Miscellaneous instructions
1757 func (b Builder) CreatePHI(t Type, name string) (v Value) {
1758         cname := C.CString(name)
1759         defer C.free(unsafe.Pointer(cname))
1760         v.C = C.LLVMBuildPhi(b.C, t.C, cname)
1761         return
1762 }
1763 func (b Builder) CreateCall(fn Value, args []Value, name string) (v Value) {
1764         cname := C.CString(name)
1765         defer C.free(unsafe.Pointer(cname))
1766         ptr, nvals := llvmValueRefs(args)
1767         v.C = C.LLVMBuildCall(b.C, fn.C, ptr, nvals, cname)
1768         return
1769 }
1770
1771 func (b Builder) CreateSelect(ifv, thenv, elsev Value, name string) (v Value) {
1772         cname := C.CString(name)
1773         defer C.free(unsafe.Pointer(cname))
1774         v.C = C.LLVMBuildSelect(b.C, ifv.C, thenv.C, elsev.C, cname)
1775         return
1776 }
1777
1778 func (b Builder) CreateVAArg(list Value, t Type, name string) (v Value) {
1779         cname := C.CString(name)
1780         defer C.free(unsafe.Pointer(cname))
1781         v.C = C.LLVMBuildVAArg(b.C, list.C, t.C, cname)
1782         return
1783 }
1784 func (b Builder) CreateExtractElement(vec, i Value, name string) (v Value) {
1785         cname := C.CString(name)
1786         defer C.free(unsafe.Pointer(cname))
1787         v.C = C.LLVMBuildExtractElement(b.C, vec.C, i.C, cname)
1788         return
1789 }
1790 func (b Builder) CreateInsertElement(vec, elt, i Value, name string) (v Value) {
1791         cname := C.CString(name)
1792         defer C.free(unsafe.Pointer(cname))
1793         v.C = C.LLVMBuildInsertElement(b.C, vec.C, elt.C, i.C, cname)
1794         return
1795 }
1796 func (b Builder) CreateShuffleVector(v1, v2, mask Value, name string) (v Value) {
1797         cname := C.CString(name)
1798         defer C.free(unsafe.Pointer(cname))
1799         v.C = C.LLVMBuildShuffleVector(b.C, v1.C, v2.C, mask.C, cname)
1800         return
1801 }
1802 func (b Builder) CreateExtractValue(agg Value, i int, name string) (v Value) {
1803         cname := C.CString(name)
1804         defer C.free(unsafe.Pointer(cname))
1805         v.C = C.LLVMBuildExtractValue(b.C, agg.C, C.unsigned(i), cname)
1806         return
1807 }
1808 func (b Builder) CreateInsertValue(agg, elt Value, i int, name string) (v Value) {
1809         cname := C.CString(name)
1810         defer C.free(unsafe.Pointer(cname))
1811         v.C = C.LLVMBuildInsertValue(b.C, agg.C, elt.C, C.unsigned(i), cname)
1812         return
1813 }
1814
1815 func (b Builder) CreateIsNull(val Value, name string) (v Value) {
1816         cname := C.CString(name)
1817         defer C.free(unsafe.Pointer(cname))
1818         v.C = C.LLVMBuildIsNull(b.C, val.C, cname)
1819         return
1820 }
1821 func (b Builder) CreateIsNotNull(val Value, name string) (v Value) {
1822         cname := C.CString(name)
1823         defer C.free(unsafe.Pointer(cname))
1824         v.C = C.LLVMBuildIsNotNull(b.C, val.C, cname)
1825         return
1826 }
1827 func (b Builder) CreatePtrDiff(lhs, rhs Value, name string) (v Value) {
1828         cname := C.CString(name)
1829         defer C.free(unsafe.Pointer(cname))
1830         v.C = C.LLVMBuildPtrDiff(b.C, lhs.C, rhs.C, cname)
1831         return
1832 }
1833
1834 func (b Builder) CreateLandingPad(t Type, nclauses int, name string) (l Value) {
1835         cname := C.CString(name)
1836         defer C.free(unsafe.Pointer(cname))
1837         l.C = C.LLVMBuildLandingPad(b.C, t.C, nil, C.unsigned(nclauses), cname)
1838         return l
1839 }
1840
1841 func (l Value) AddClause(v Value) {
1842         C.LLVMAddClause(l.C, v.C)
1843 }
1844
1845 func (l Value) SetCleanup(cleanup bool) {
1846         C.LLVMSetCleanup(l.C, boolToLLVMBool(cleanup))
1847 }
1848
1849 func (b Builder) CreateResume(ex Value) (v Value) {
1850         v.C = C.LLVMBuildResume(b.C, ex.C)
1851         return
1852 }
1853
1854 //-------------------------------------------------------------------------
1855 // llvm.ModuleProvider
1856 //-------------------------------------------------------------------------
1857
1858 // Changes the type of M so it can be passed to FunctionPassManagers and the
1859 // JIT. They take ModuleProviders for historical reasons.
1860 func NewModuleProviderForModule(m Module) (mp ModuleProvider) {
1861         mp.C = C.LLVMCreateModuleProviderForExistingModule(m.C)
1862         return
1863 }
1864
1865 // Destroys the module M.
1866 func (mp ModuleProvider) Dispose() { C.LLVMDisposeModuleProvider(mp.C) }
1867
1868 //-------------------------------------------------------------------------
1869 // llvm.MemoryBuffer
1870 //-------------------------------------------------------------------------
1871
1872 func NewMemoryBufferFromFile(path string) (b MemoryBuffer, err error) {
1873         var cmsg *C.char
1874         cpath := C.CString(path)
1875         defer C.free(unsafe.Pointer(cpath))
1876         fail := C.LLVMCreateMemoryBufferWithContentsOfFile(cpath, &b.C, &cmsg)
1877         if fail != 0 {
1878                 b.C = nil
1879                 err = errors.New(C.GoString(cmsg))
1880                 C.LLVMDisposeMessage(cmsg)
1881         }
1882         return
1883 }
1884
1885 func NewMemoryBufferFromStdin() (b MemoryBuffer, err error) {
1886         var cmsg *C.char
1887         fail := C.LLVMCreateMemoryBufferWithSTDIN(&b.C, &cmsg)
1888         if fail != 0 {
1889                 b.C = nil
1890                 err = errors.New(C.GoString(cmsg))
1891                 C.LLVMDisposeMessage(cmsg)
1892         }
1893         return
1894 }
1895
1896 func (b MemoryBuffer) Bytes() []byte {
1897         cstart := C.LLVMGetBufferStart(b.C)
1898         csize := C.LLVMGetBufferSize(b.C)
1899         return C.GoBytes(unsafe.Pointer(cstart), C.int(csize))
1900 }
1901
1902 func (b MemoryBuffer) Dispose() { C.LLVMDisposeMemoryBuffer(b.C) }
1903
1904 //-------------------------------------------------------------------------
1905 // llvm.PassManager
1906 //-------------------------------------------------------------------------
1907
1908 // Constructs a new whole-module pass pipeline. This type of pipeline is
1909 // suitable for link-time optimization and whole-module transformations.
1910 // See llvm::PassManager::PassManager.
1911 func NewPassManager() (pm PassManager) { pm.C = C.LLVMCreatePassManager(); return }
1912
1913 // Constructs a new function-by-function pass pipeline over the module
1914 // provider. It does not take ownership of the module provider. This type of
1915 // pipeline is suitable for code generation and JIT compilation tasks.
1916 // See llvm::FunctionPassManager::FunctionPassManager.
1917 func NewFunctionPassManagerForModule(m Module) (pm PassManager) {
1918         pm.C = C.LLVMCreateFunctionPassManagerForModule(m.C)
1919         return
1920 }
1921
1922 // Initializes, executes on the provided module, and finalizes all of the
1923 // passes scheduled in the pass manager. Returns 1 if any of the passes
1924 // modified the module, 0 otherwise. See llvm::PassManager::run(Module&).
1925 func (pm PassManager) Run(m Module) bool { return C.LLVMRunPassManager(pm.C, m.C) != 0 }
1926
1927 // Initializes all of the function passes scheduled in the function pass
1928 // manager. Returns 1 if any of the passes modified the module, 0 otherwise.
1929 // See llvm::FunctionPassManager::doInitialization.
1930 func (pm PassManager) InitializeFunc() bool { return C.LLVMInitializeFunctionPassManager(pm.C) != 0 }
1931
1932 // Executes all of the function passes scheduled in the function pass manager
1933 // on the provided function. Returns 1 if any of the passes modified the
1934 // function, false otherwise.
1935 // See llvm::FunctionPassManager::run(Function&).
1936 func (pm PassManager) RunFunc(f Value) bool { return C.LLVMRunFunctionPassManager(pm.C, f.C) != 0 }
1937
1938 // Finalizes all of the function passes scheduled in the function pass
1939 // manager. Returns 1 if any of the passes modified the module, 0 otherwise.
1940 // See llvm::FunctionPassManager::doFinalization.
1941 func (pm PassManager) FinalizeFunc() bool { return C.LLVMFinalizeFunctionPassManager(pm.C) != 0 }
1942
1943 // Frees the memory of a pass pipeline. For function pipelines, does not free
1944 // the module provider.
1945 // See llvm::PassManagerBase::~PassManagerBase.
1946 func (pm PassManager) Dispose() { C.LLVMDisposePassManager(pm.C) }