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