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