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(¶mTypes[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 ConstFAdd(lhs, rhs Value) (v Value) { v.C = C.LLVMConstFAdd(lhs.C, rhs.C); return } 913func ConstSub(lhs, rhs Value) (v Value) { v.C = C.LLVMConstSub(lhs.C, rhs.C); return } 914func ConstNSWSub(lhs, rhs Value) (v Value) { v.C = C.LLVMConstNSWSub(lhs.C, rhs.C); return } 915func ConstNUWSub(lhs, rhs Value) (v Value) { v.C = C.LLVMConstNUWSub(lhs.C, rhs.C); return } 916func ConstFSub(lhs, rhs Value) (v Value) { v.C = C.LLVMConstFSub(lhs.C, rhs.C); return } 917func ConstMul(lhs, rhs Value) (v Value) { v.C = C.LLVMConstMul(lhs.C, rhs.C); return } 918func ConstNSWMul(lhs, rhs Value) (v Value) { v.C = C.LLVMConstNSWMul(lhs.C, rhs.C); return } 919func ConstNUWMul(lhs, rhs Value) (v Value) { v.C = C.LLVMConstNUWMul(lhs.C, rhs.C); return } 920func ConstFMul(lhs, rhs Value) (v Value) { v.C = C.LLVMConstFMul(lhs.C, rhs.C); return } 921func ConstUDiv(lhs, rhs Value) (v Value) { v.C = C.LLVMConstUDiv(lhs.C, rhs.C); return } 922func ConstSDiv(lhs, rhs Value) (v Value) { v.C = C.LLVMConstSDiv(lhs.C, rhs.C); return } 923func ConstExactSDiv(lhs, rhs Value) (v Value) { v.C = C.LLVMConstExactSDiv(lhs.C, rhs.C); return } 924func ConstFDiv(lhs, rhs Value) (v Value) { v.C = C.LLVMConstFDiv(lhs.C, rhs.C); return } 925func ConstURem(lhs, rhs Value) (v Value) { v.C = C.LLVMConstURem(lhs.C, rhs.C); return } 926func ConstSRem(lhs, rhs Value) (v Value) { v.C = C.LLVMConstSRem(lhs.C, rhs.C); return } 927func ConstFRem(lhs, rhs Value) (v Value) { v.C = C.LLVMConstFRem(lhs.C, rhs.C); return } 928func ConstAnd(lhs, rhs Value) (v Value) { v.C = C.LLVMConstAnd(lhs.C, rhs.C); return } 929func ConstOr(lhs, rhs Value) (v Value) { v.C = C.LLVMConstOr(lhs.C, rhs.C); return } 930func ConstXor(lhs, rhs Value) (v Value) { v.C = C.LLVMConstXor(lhs.C, rhs.C); return } 931 932func ConstICmp(pred IntPredicate, lhs, rhs Value) (v Value) { 933 v.C = C.LLVMConstICmp(C.LLVMIntPredicate(pred), lhs.C, rhs.C) 934 return 935} 936func ConstFCmp(pred FloatPredicate, lhs, rhs Value) (v Value) { 937 v.C = C.LLVMConstFCmp(C.LLVMRealPredicate(pred), lhs.C, rhs.C) 938 return 939} 940 941func ConstShl(lhs, rhs Value) (v Value) { v.C = C.LLVMConstShl(lhs.C, rhs.C); return } 942func ConstLShr(lhs, rhs Value) (v Value) { v.C = C.LLVMConstLShr(lhs.C, rhs.C); return } 943func ConstAShr(lhs, rhs Value) (v Value) { v.C = C.LLVMConstAShr(lhs.C, rhs.C); return } 944 945func ConstGEP(v Value, indices []Value) (rv Value) { 946 ptr, nvals := llvmValueRefs(indices) 947 rv.C = C.LLVMConstGEP(v.C, ptr, nvals) 948 return 949} 950func ConstInBoundsGEP(v Value, indices []Value) (rv Value) { 951 ptr, nvals := llvmValueRefs(indices) 952 rv.C = C.LLVMConstInBoundsGEP(v.C, ptr, nvals) 953 return 954} 955func ConstTrunc(v Value, t Type) (rv Value) { rv.C = C.LLVMConstTrunc(v.C, t.C); return } 956func ConstSExt(v Value, t Type) (rv Value) { rv.C = C.LLVMConstSExt(v.C, t.C); return } 957func ConstZExt(v Value, t Type) (rv Value) { rv.C = C.LLVMConstZExt(v.C, t.C); return } 958func ConstFPTrunc(v Value, t Type) (rv Value) { rv.C = C.LLVMConstFPTrunc(v.C, t.C); return } 959func ConstFPExt(v Value, t Type) (rv Value) { rv.C = C.LLVMConstFPExt(v.C, t.C); return } 960func ConstUIToFP(v Value, t Type) (rv Value) { rv.C = C.LLVMConstUIToFP(v.C, t.C); return } 961func ConstSIToFP(v Value, t Type) (rv Value) { rv.C = C.LLVMConstSIToFP(v.C, t.C); return } 962func ConstFPToUI(v Value, t Type) (rv Value) { rv.C = C.LLVMConstFPToUI(v.C, t.C); return } 963func ConstFPToSI(v Value, t Type) (rv Value) { rv.C = C.LLVMConstFPToSI(v.C, t.C); return } 964func ConstPtrToInt(v Value, t Type) (rv Value) { rv.C = C.LLVMConstPtrToInt(v.C, t.C); return } 965func ConstIntToPtr(v Value, t Type) (rv Value) { rv.C = C.LLVMConstIntToPtr(v.C, t.C); return } 966func ConstBitCast(v Value, t Type) (rv Value) { rv.C = C.LLVMConstBitCast(v.C, t.C); return } 967func ConstZExtOrBitCast(v Value, t Type) (rv Value) { rv.C = C.LLVMConstZExtOrBitCast(v.C, t.C); return } 968func ConstSExtOrBitCast(v Value, t Type) (rv Value) { rv.C = C.LLVMConstSExtOrBitCast(v.C, t.C); return } 969func ConstTruncOrBitCast(v Value, t Type) (rv Value) { 970 rv.C = C.LLVMConstTruncOrBitCast(v.C, t.C) 971 return 972} 973func ConstPointerCast(v Value, t Type) (rv Value) { rv.C = C.LLVMConstPointerCast(v.C, t.C); return } 974func ConstIntCast(v Value, t Type, signed bool) (rv Value) { 975 rv.C = C.LLVMConstIntCast(v.C, t.C, boolToLLVMBool(signed)) 976 return 977} 978func ConstFPCast(v Value, t Type) (rv Value) { rv.C = C.LLVMConstFPCast(v.C, t.C); return } 979func ConstSelect(cond, iftrue, iffalse Value) (rv Value) { 980 rv.C = C.LLVMConstSelect(cond.C, iftrue.C, iffalse.C) 981 return 982} 983func ConstExtractElement(vec, i Value) (rv Value) { 984 rv.C = C.LLVMConstExtractElement(vec.C, i.C) 985 return 986} 987func ConstInsertElement(vec, elem, i Value) (rv Value) { 988 rv.C = C.LLVMConstInsertElement(vec.C, elem.C, i.C) 989 return 990} 991func ConstShuffleVector(veca, vecb, mask Value) (rv Value) { 992 rv.C = C.LLVMConstShuffleVector(veca.C, vecb.C, mask.C) 993 return 994} 995 996//TODO 997//LLVMValueRef LLVMConstExtractValue(LLVMValueRef AggConstant, unsigned *IdxList, 998// unsigned NumIdx); 999 1000func ConstExtractValue(agg Value, indices []uint32) (rv Value) { 1001 n := len(indices) 1002 if n == 0 { 1003 panic("one or more indices are required") 1004 } 1005 ptr := (*C.unsigned)(&indices[0]) 1006 rv.C = C.LLVMConstExtractValue(agg.C, ptr, C.unsigned(n)) 1007 return 1008} 1009 1010func ConstInsertValue(agg, val Value, indices []uint32) (rv Value) { 1011 n := len(indices) 1012 if n == 0 { 1013 panic("one or more indices are required") 1014 } 1015 ptr := (*C.unsigned)(&indices[0]) 1016 rv.C = C.LLVMConstInsertValue(agg.C, val.C, ptr, C.unsigned(n)) 1017 return 1018} 1019 1020func BlockAddress(f Value, bb BasicBlock) (v Value) { 1021 v.C = C.LLVMBlockAddress(f.C, bb.C) 1022 return 1023} 1024 1025// Operations on global variables, functions, and aliases (globals) 1026func (v Value) GlobalParent() (m Module) { m.C = C.LLVMGetGlobalParent(v.C); return } 1027func (v Value) IsDeclaration() bool { return C.LLVMIsDeclaration(v.C) != 0 } 1028func (v Value) Linkage() Linkage { return Linkage(C.LLVMGetLinkage(v.C)) } 1029func (v Value) SetLinkage(l Linkage) { C.LLVMSetLinkage(v.C, C.LLVMLinkage(l)) } 1030func (v Value) Section() string { return C.GoString(C.LLVMGetSection(v.C)) } 1031func (v Value) SetSection(str string) { 1032 cstr := C.CString(str) 1033 defer C.free(unsafe.Pointer(cstr)) 1034 C.LLVMSetSection(v.C, cstr) 1035} 1036func (v Value) Visibility() Visibility { return Visibility(C.LLVMGetVisibility(v.C)) } 1037func (v Value) SetVisibility(vi Visibility) { C.LLVMSetVisibility(v.C, C.LLVMVisibility(vi)) } 1038func (v Value) Alignment() int { return int(C.LLVMGetAlignment(v.C)) } 1039func (v Value) SetAlignment(a int) { C.LLVMSetAlignment(v.C, C.unsigned(a)) } 1040func (v Value) SetUnnamedAddr(ua bool) { C.LLVMSetUnnamedAddr(v.C, boolToLLVMBool(ua)) } 1041 1042// Operations on global variables 1043func AddGlobal(m Module, t Type, name string) (v Value) { 1044 cname := C.CString(name) 1045 defer C.free(unsafe.Pointer(cname)) 1046 v.C = C.LLVMAddGlobal(m.C, t.C, cname) 1047 return 1048} 1049func AddGlobalInAddressSpace(m Module, t Type, name string, addressSpace int) (v Value) { 1050 cname := C.CString(name) 1051 defer C.free(unsafe.Pointer(cname)) 1052 v.C = C.LLVMAddGlobalInAddressSpace(m.C, t.C, cname, C.unsigned(addressSpace)) 1053 return 1054} 1055func (m Module) NamedGlobal(name string) (v Value) { 1056 cname := C.CString(name) 1057 defer C.free(unsafe.Pointer(cname)) 1058 v.C = C.LLVMGetNamedGlobal(m.C, cname) 1059 return 1060} 1061 1062func (m Module) FirstGlobal() (v Value) { v.C = C.LLVMGetFirstGlobal(m.C); return } 1063func (m Module) LastGlobal() (v Value) { v.C = C.LLVMGetLastGlobal(m.C); return } 1064func NextGlobal(v Value) (rv Value) { rv.C = C.LLVMGetNextGlobal(v.C); return } 1065func PrevGlobal(v Value) (rv Value) { rv.C = C.LLVMGetPreviousGlobal(v.C); return } 1066func (v Value) EraseFromParentAsGlobal() { C.LLVMDeleteGlobal(v.C) } 1067func (v Value) Initializer() (rv Value) { rv.C = C.LLVMGetInitializer(v.C); return } 1068func (v Value) SetInitializer(cv Value) { C.LLVMSetInitializer(v.C, cv.C) } 1069func (v Value) IsThreadLocal() bool { return C.LLVMIsThreadLocal(v.C) != 0 } 1070func (v Value) SetThreadLocal(tl bool) { C.LLVMSetThreadLocal(v.C, boolToLLVMBool(tl)) } 1071func (v Value) IsGlobalConstant() bool { return C.LLVMIsGlobalConstant(v.C) != 0 } 1072func (v Value) SetGlobalConstant(gc bool) { C.LLVMSetGlobalConstant(v.C, boolToLLVMBool(gc)) } 1073func (v Value) IsVolatile() bool { return C.LLVMGetVolatile(v.C) != 0 } 1074func (v Value) SetVolatile(volatile bool) { C.LLVMSetVolatile(v.C, boolToLLVMBool(volatile)) } 1075func (v Value) Ordering() AtomicOrdering { return AtomicOrdering(C.LLVMGetOrdering(v.C)) } 1076func (v Value) SetOrdering(ordering AtomicOrdering) { 1077 C.LLVMSetOrdering(v.C, C.LLVMAtomicOrdering(ordering)) 1078} 1079func (v Value) IsAtomicSingleThread() bool { return C.LLVMIsAtomicSingleThread(v.C) != 0 } 1080func (v Value) SetAtomicSingleThread(singleThread bool) { 1081 C.LLVMSetAtomicSingleThread(v.C, boolToLLVMBool(singleThread)) 1082} 1083func (v Value) CmpXchgSuccessOrdering() AtomicOrdering { 1084 return AtomicOrdering(C.LLVMGetCmpXchgSuccessOrdering(v.C)) 1085} 1086func (v Value) SetCmpXchgSuccessOrdering(ordering AtomicOrdering) { 1087 C.LLVMSetCmpXchgSuccessOrdering(v.C, C.LLVMAtomicOrdering(ordering)) 1088} 1089func (v Value) CmpXchgFailureOrdering() AtomicOrdering { 1090 return AtomicOrdering(C.LLVMGetCmpXchgFailureOrdering(v.C)) 1091} 1092func (v Value) SetCmpXchgFailureOrdering(ordering AtomicOrdering) { 1093 C.LLVMSetCmpXchgFailureOrdering(v.C, C.LLVMAtomicOrdering(ordering)) 1094} 1095 1096// Operations on aliases 1097func AddAlias(m Module, t Type, aliasee Value, name string) (v Value) { 1098 cname := C.CString(name) 1099 defer C.free(unsafe.Pointer(cname)) 1100 v.C = C.LLVMAddAlias(m.C, t.C, aliasee.C, cname) 1101 return 1102} 1103 1104// Operations on comdat 1105func (m Module) Comdat(name string) (c Comdat) { 1106 cname := C.CString(name) 1107 defer C.free(unsafe.Pointer(cname)) 1108 c.C = C.LLVMGetOrInsertComdat(m.C, cname) 1109 return 1110} 1111 1112func (v Value) Comdat() (c Comdat) { c.C = C.LLVMGetComdat(v.C); return } 1113func (v Value) SetComdat(c Comdat) { C.LLVMSetComdat(v.C, c.C) } 1114 1115func (c Comdat) SelectionKind() ComdatSelectionKind { 1116 return ComdatSelectionKind(C.LLVMGetComdatSelectionKind(c.C)) 1117} 1118 1119func (c Comdat) SetSelectionKind(k ComdatSelectionKind) { 1120 C.LLVMSetComdatSelectionKind(c.C, (C.LLVMComdatSelectionKind)(k)) 1121} 1122 1123// Operations on functions 1124func AddFunction(m Module, name string, ft Type) (v Value) { 1125 cname := C.CString(name) 1126 defer C.free(unsafe.Pointer(cname)) 1127 v.C = C.LLVMAddFunction(m.C, cname, ft.C) 1128 return 1129} 1130 1131func (m Module) NamedFunction(name string) (v Value) { 1132 cname := C.CString(name) 1133 defer C.free(unsafe.Pointer(cname)) 1134 v.C = C.LLVMGetNamedFunction(m.C, cname) 1135 return 1136} 1137 1138func (m Module) FirstFunction() (v Value) { v.C = C.LLVMGetFirstFunction(m.C); return } 1139func (m Module) LastFunction() (v Value) { v.C = C.LLVMGetLastFunction(m.C); return } 1140func NextFunction(v Value) (rv Value) { rv.C = C.LLVMGetNextFunction(v.C); return } 1141func PrevFunction(v Value) (rv Value) { rv.C = C.LLVMGetPreviousFunction(v.C); return } 1142func (v Value) EraseFromParentAsFunction() { C.LLVMDeleteFunction(v.C) } 1143func (v Value) IntrinsicID() int { return int(C.LLVMGetIntrinsicID(v.C)) } 1144func (v Value) FunctionCallConv() CallConv { 1145 return CallConv(C.LLVMCallConv(C.LLVMGetFunctionCallConv(v.C))) 1146} 1147func (v Value) SetFunctionCallConv(cc CallConv) { C.LLVMSetFunctionCallConv(v.C, C.unsigned(cc)) } 1148func (v Value) GC() string { return C.GoString(C.LLVMGetGC(v.C)) } 1149func (v Value) SetGC(name string) { 1150 cname := C.CString(name) 1151 defer C.free(unsafe.Pointer(cname)) 1152 C.LLVMSetGC(v.C, cname) 1153} 1154func (v Value) AddAttributeAtIndex(i int, a Attribute) { 1155 C.LLVMAddAttributeAtIndex(v.C, C.LLVMAttributeIndex(i), a.C) 1156} 1157func (v Value) AddFunctionAttr(a Attribute) { 1158 v.AddAttributeAtIndex(C.LLVMAttributeFunctionIndex, a) 1159} 1160func (v Value) GetEnumAttributeAtIndex(i int, kind uint) (a Attribute) { 1161 a.C = C.LLVMGetEnumAttributeAtIndex(v.C, C.LLVMAttributeIndex(i), C.unsigned(kind)) 1162 return 1163} 1164func (v Value) GetEnumFunctionAttribute(kind uint) Attribute { 1165 return v.GetEnumAttributeAtIndex(C.LLVMAttributeFunctionIndex, kind) 1166} 1167func (v Value) GetStringAttributeAtIndex(i int, kind string) (a Attribute) { 1168 ckind := C.CString(kind) 1169 defer C.free(unsafe.Pointer(ckind)) 1170 a.C = C.LLVMGetStringAttributeAtIndex(v.C, C.LLVMAttributeIndex(i), 1171 ckind, C.unsigned(len(kind))) 1172 return 1173} 1174func (v Value) RemoveEnumAttributeAtIndex(i int, kind uint) { 1175 C.LLVMRemoveEnumAttributeAtIndex(v.C, C.LLVMAttributeIndex(i), C.unsigned(kind)) 1176} 1177func (v Value) RemoveEnumFunctionAttribute(kind uint) { 1178 v.RemoveEnumAttributeAtIndex(C.LLVMAttributeFunctionIndex, kind) 1179} 1180func (v Value) RemoveStringAttributeAtIndex(i int, kind string) { 1181 ckind := C.CString(kind) 1182 defer C.free(unsafe.Pointer(ckind)) 1183 C.LLVMRemoveStringAttributeAtIndex(v.C, C.LLVMAttributeIndex(i), 1184 ckind, C.unsigned(len(kind))) 1185} 1186func (v Value) AddTargetDependentFunctionAttr(attr, value string) { 1187 cattr := C.CString(attr) 1188 defer C.free(unsafe.Pointer(cattr)) 1189 cvalue := C.CString(value) 1190 defer C.free(unsafe.Pointer(cvalue)) 1191 C.LLVMAddTargetDependentFunctionAttr(v.C, cattr, cvalue) 1192} 1193func (v Value) SetPersonality(p Value) { 1194 C.LLVMSetPersonalityFn(v.C, p.C) 1195} 1196 1197// Operations on parameters 1198func (v Value) ParamsCount() int { return int(C.LLVMCountParams(v.C)) } 1199func (v Value) Params() []Value { 1200 out := make([]Value, v.ParamsCount()) 1201 if len(out) > 0 { 1202 C.LLVMGetParams(v.C, llvmValueRefPtr(&out[0])) 1203 } 1204 return out 1205} 1206func (v Value) Param(i int) (rv Value) { rv.C = C.LLVMGetParam(v.C, C.unsigned(i)); return } 1207func (v Value) ParamParent() (rv Value) { rv.C = C.LLVMGetParamParent(v.C); return } 1208func (v Value) FirstParam() (rv Value) { rv.C = C.LLVMGetFirstParam(v.C); return } 1209func (v Value) LastParam() (rv Value) { rv.C = C.LLVMGetLastParam(v.C); return } 1210func NextParam(v Value) (rv Value) { rv.C = C.LLVMGetNextParam(v.C); return } 1211func PrevParam(v Value) (rv Value) { rv.C = C.LLVMGetPreviousParam(v.C); return } 1212func (v Value) SetParamAlignment(align int) { C.LLVMSetParamAlignment(v.C, C.unsigned(align)) } 1213 1214// Operations on basic blocks 1215func (bb BasicBlock) AsValue() (v Value) { v.C = C.LLVMBasicBlockAsValue(bb.C); return } 1216func (v Value) IsBasicBlock() bool { return C.LLVMValueIsBasicBlock(v.C) != 0 } 1217func (v Value) AsBasicBlock() (bb BasicBlock) { bb.C = C.LLVMValueAsBasicBlock(v.C); return } 1218func (bb BasicBlock) Parent() (v Value) { v.C = C.LLVMGetBasicBlockParent(bb.C); return } 1219func (v Value) BasicBlocksCount() int { return int(C.LLVMCountBasicBlocks(v.C)) } 1220func (v Value) BasicBlocks() []BasicBlock { 1221 out := make([]BasicBlock, v.BasicBlocksCount()) 1222 C.LLVMGetBasicBlocks(v.C, llvmBasicBlockRefPtr(&out[0])) 1223 return out 1224} 1225func (v Value) FirstBasicBlock() (bb BasicBlock) { bb.C = C.LLVMGetFirstBasicBlock(v.C); return } 1226func (v Value) LastBasicBlock() (bb BasicBlock) { bb.C = C.LLVMGetLastBasicBlock(v.C); return } 1227func NextBasicBlock(bb BasicBlock) (rbb BasicBlock) { rbb.C = C.LLVMGetNextBasicBlock(bb.C); return } 1228func PrevBasicBlock(bb BasicBlock) (rbb BasicBlock) { rbb.C = C.LLVMGetPreviousBasicBlock(bb.C); return } 1229func (v Value) EntryBasicBlock() (bb BasicBlock) { bb.C = C.LLVMGetEntryBasicBlock(v.C); return } 1230func (c Context) AddBasicBlock(f Value, name string) (bb BasicBlock) { 1231 cname := C.CString(name) 1232 defer C.free(unsafe.Pointer(cname)) 1233 bb.C = C.LLVMAppendBasicBlockInContext(c.C, f.C, cname) 1234 return 1235} 1236func (c Context) InsertBasicBlock(ref BasicBlock, name string) (bb BasicBlock) { 1237 cname := C.CString(name) 1238 defer C.free(unsafe.Pointer(cname)) 1239 bb.C = C.LLVMInsertBasicBlockInContext(c.C, ref.C, cname) 1240 return 1241} 1242func AddBasicBlock(f Value, name string) (bb BasicBlock) { 1243 cname := C.CString(name) 1244 defer C.free(unsafe.Pointer(cname)) 1245 bb.C = C.LLVMAppendBasicBlock(f.C, cname) 1246 return 1247} 1248func InsertBasicBlock(ref BasicBlock, name string) (bb BasicBlock) { 1249 cname := C.CString(name) 1250 defer C.free(unsafe.Pointer(cname)) 1251 bb.C = C.LLVMInsertBasicBlock(ref.C, cname) 1252 return 1253} 1254func (bb BasicBlock) EraseFromParent() { C.LLVMDeleteBasicBlock(bb.C) } 1255func (bb BasicBlock) MoveBefore(pos BasicBlock) { C.LLVMMoveBasicBlockBefore(bb.C, pos.C) } 1256func (bb BasicBlock) MoveAfter(pos BasicBlock) { C.LLVMMoveBasicBlockAfter(bb.C, pos.C) } 1257 1258// Operations on instructions 1259func (v Value) EraseFromParentAsInstruction() { C.LLVMInstructionEraseFromParent(v.C) } 1260func (v Value) RemoveFromParentAsInstruction() { C.LLVMInstructionRemoveFromParent(v.C) } 1261func (v Value) InstructionParent() (bb BasicBlock) { bb.C = C.LLVMGetInstructionParent(v.C); return } 1262func (v Value) InstructionDebugLoc() (md Metadata) { md.C = C.LLVMInstructionGetDebugLoc(v.C); return } 1263func (v Value) InstructionSetDebugLoc(md Metadata) { C.LLVMInstructionSetDebugLoc(v.C, md.C) } 1264func (bb BasicBlock) FirstInstruction() (v Value) { v.C = C.LLVMGetFirstInstruction(bb.C); return } 1265func (bb BasicBlock) LastInstruction() (v Value) { v.C = C.LLVMGetLastInstruction(bb.C); return } 1266func NextInstruction(v Value) (rv Value) { rv.C = C.LLVMGetNextInstruction(v.C); return } 1267func PrevInstruction(v Value) (rv Value) { rv.C = C.LLVMGetPreviousInstruction(v.C); return } 1268 1269// Operations on call sites 1270func (v Value) SetInstructionCallConv(cc CallConv) { 1271 C.LLVMSetInstructionCallConv(v.C, C.unsigned(cc)) 1272} 1273func (v Value) InstructionCallConv() CallConv { 1274 return CallConv(C.LLVMCallConv(C.LLVMGetInstructionCallConv(v.C))) 1275} 1276func (v Value) AddCallSiteAttribute(i int, a Attribute) { 1277 C.LLVMAddCallSiteAttribute(v.C, C.LLVMAttributeIndex(i), a.C) 1278} 1279func (v Value) SetInstrParamAlignment(i int, align int) { 1280 C.LLVMSetInstrParamAlignment(v.C, C.LLVMAttributeIndex(i), C.unsigned(align)) 1281} 1282func (v Value) CalledValue() (rv Value) { 1283 rv.C = C.LLVMGetCalledValue(v.C) 1284 return 1285} 1286 1287// Operations on call instructions (only) 1288func (v Value) IsTailCall() bool { return C.LLVMIsTailCall(v.C) != 0 } 1289func (v Value) SetTailCall(is bool) { C.LLVMSetTailCall(v.C, boolToLLVMBool(is)) } 1290 1291// Operations on phi nodes 1292func (v Value) AddIncoming(vals []Value, blocks []BasicBlock) { 1293 ptr, nvals := llvmValueRefs(vals) 1294 C.LLVMAddIncoming(v.C, ptr, llvmBasicBlockRefPtr(&blocks[0]), nvals) 1295} 1296func (v Value) IncomingCount() int { return int(C.LLVMCountIncoming(v.C)) } 1297func (v Value) IncomingValue(i int) (rv Value) { 1298 rv.C = C.LLVMGetIncomingValue(v.C, C.unsigned(i)) 1299 return 1300} 1301func (v Value) IncomingBlock(i int) (bb BasicBlock) { 1302 bb.C = C.LLVMGetIncomingBlock(v.C, C.unsigned(i)) 1303 return 1304} 1305 1306// Operations on inline assembly 1307func InlineAsm(t Type, asmString, constraints string, hasSideEffects, isAlignStack bool, dialect InlineAsmDialect, canThrow bool) (rv Value) { 1308 casm := C.CString(asmString) 1309 defer C.free(unsafe.Pointer(casm)) 1310 cconstraints := C.CString(constraints) 1311 defer C.free(unsafe.Pointer(cconstraints)) 1312 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)) 1313 return 1314} 1315 1316// Operations on aggregates 1317func (v Value) Indices() []uint32 { 1318 num := C.LLVMGetNumIndices(v.C) 1319 indicesPtr := C.LLVMGetIndices(v.C) 1320 // https://github.com/golang/go/wiki/cgo#turning-c-arrays-into-go-slices 1321 rawIndices := (*[1 << 20]C.uint)(unsafe.Pointer(indicesPtr))[:num:num] 1322 indices := make([]uint32, num) 1323 for i := range indices { 1324 indices[i] = uint32(rawIndices[i]) 1325 } 1326 return indices 1327} 1328 1329// Operations on comparisons 1330func (v Value) IntPredicate() IntPredicate { return IntPredicate(C.LLVMGetICmpPredicate(v.C)) } 1331func (v Value) FloatPredicate() FloatPredicate { return FloatPredicate(C.LLVMGetFCmpPredicate(v.C)) } 1332 1333//------------------------------------------------------------------------- 1334// llvm.Builder 1335//------------------------------------------------------------------------- 1336 1337// An instruction builder represents a point within a basic block, and is the 1338// exclusive means of building instructions using the C interface. 1339 1340func (c Context) NewBuilder() (b Builder) { b.C = C.LLVMCreateBuilderInContext(c.C); return } 1341func NewBuilder() (b Builder) { b.C = C.LLVMCreateBuilder(); return } 1342func (b Builder) SetInsertPoint(block BasicBlock, instr Value) { 1343 C.LLVMPositionBuilder(b.C, block.C, instr.C) 1344} 1345func (b Builder) SetInsertPointBefore(instr Value) { C.LLVMPositionBuilderBefore(b.C, instr.C) } 1346func (b Builder) SetInsertPointAtEnd(block BasicBlock) { C.LLVMPositionBuilderAtEnd(b.C, block.C) } 1347func (b Builder) GetInsertBlock() (bb BasicBlock) { bb.C = C.LLVMGetInsertBlock(b.C); return } 1348func (b Builder) ClearInsertionPoint() { C.LLVMClearInsertionPosition(b.C) } 1349func (b Builder) Insert(instr Value) { C.LLVMInsertIntoBuilder(b.C, instr.C) } 1350func (b Builder) InsertWithName(instr Value, name string) { 1351 cname := C.CString(name) 1352 defer C.free(unsafe.Pointer(cname)) 1353 C.LLVMInsertIntoBuilderWithName(b.C, instr.C, cname) 1354} 1355func (b Builder) Dispose() { C.LLVMDisposeBuilder(b.C) } 1356 1357// Metadata 1358type DebugLoc struct { 1359 Line, Col uint 1360 Scope Metadata 1361 InlinedAt Metadata 1362} 1363 1364func (b Builder) SetCurrentDebugLocation(line, col uint, scope, inlinedAt Metadata) { 1365 C.LLVMGoSetCurrentDebugLocation(b.C, C.unsigned(line), C.unsigned(col), scope.C, inlinedAt.C) 1366} 1367 1368// Get current debug location. Please do not call this function until setting debug location with SetCurrentDebugLocation() 1369func (b Builder) GetCurrentDebugLocation() (loc DebugLoc) { 1370 md := C.LLVMGoGetCurrentDebugLocation(b.C) 1371 loc.Line = uint(md.Line) 1372 loc.Col = uint(md.Col) 1373 loc.Scope = Metadata{C: md.Scope} 1374 loc.InlinedAt = Metadata{C: md.InlinedAt} 1375 return 1376} 1377func (b Builder) SetInstDebugLocation(v Value) { C.LLVMSetInstDebugLocation(b.C, v.C) } 1378func (b Builder) InsertDeclare(module Module, storage Value, md Value) Value { 1379 f := module.NamedFunction("llvm.dbg.declare") 1380 if f.IsNil() { 1381 ftyp := FunctionType(VoidType(), []Type{storage.Type(), md.Type()}, false) 1382 f = AddFunction(module, "llvm.dbg.declare", ftyp) 1383 } 1384 return b.CreateCall(f, []Value{storage, md}, "") 1385} 1386 1387// Terminators 1388func (b Builder) CreateRetVoid() (rv Value) { rv.C = C.LLVMBuildRetVoid(b.C); return } 1389func (b Builder) CreateRet(v Value) (rv Value) { rv.C = C.LLVMBuildRet(b.C, v.C); return } 1390func (b Builder) CreateAggregateRet(vs []Value) (rv Value) { 1391 ptr, nvals := llvmValueRefs(vs) 1392 rv.C = C.LLVMBuildAggregateRet(b.C, ptr, nvals) 1393 return 1394} 1395func (b Builder) CreateBr(bb BasicBlock) (rv Value) { rv.C = C.LLVMBuildBr(b.C, bb.C); return } 1396func (b Builder) CreateCondBr(ifv Value, thenb, elseb BasicBlock) (rv Value) { 1397 rv.C = C.LLVMBuildCondBr(b.C, ifv.C, thenb.C, elseb.C) 1398 return 1399} 1400func (b Builder) CreateSwitch(v Value, elseb BasicBlock, numCases int) (rv Value) { 1401 rv.C = C.LLVMBuildSwitch(b.C, v.C, elseb.C, C.unsigned(numCases)) 1402 return 1403} 1404func (b Builder) CreateIndirectBr(addr Value, numDests int) (rv Value) { 1405 rv.C = C.LLVMBuildIndirectBr(b.C, addr.C, C.unsigned(numDests)) 1406 return 1407} 1408func (b Builder) CreateInvoke(fn Value, args []Value, then, catch BasicBlock, name string) (rv Value) { 1409 cname := C.CString(name) 1410 defer C.free(unsafe.Pointer(cname)) 1411 ptr, nvals := llvmValueRefs(args) 1412 rv.C = C.LLVMBuildInvoke(b.C, fn.C, ptr, nvals, then.C, catch.C, cname) 1413 return 1414} 1415func (b Builder) CreateUnreachable() (rv Value) { rv.C = C.LLVMBuildUnreachable(b.C); return } 1416 1417// Add a case to the switch instruction 1418func (v Value) AddCase(on Value, dest BasicBlock) { C.LLVMAddCase(v.C, on.C, dest.C) } 1419 1420// Add a destination to the indirectbr instruction 1421func (v Value) AddDest(dest BasicBlock) { C.LLVMAddDestination(v.C, dest.C) } 1422 1423// Arithmetic 1424func (b Builder) CreateAdd(lhs, rhs Value, name string) (v Value) { 1425 cname := C.CString(name) 1426 defer C.free(unsafe.Pointer(cname)) 1427 v.C = C.LLVMBuildAdd(b.C, lhs.C, rhs.C, cname) 1428 return 1429} 1430func (b Builder) CreateNSWAdd(lhs, rhs Value, name string) (v Value) { 1431 cname := C.CString(name) 1432 defer C.free(unsafe.Pointer(cname)) 1433 v.C = C.LLVMBuildNSWAdd(b.C, lhs.C, rhs.C, cname) 1434 return 1435} 1436func (b Builder) CreateNUWAdd(lhs, rhs Value, name string) (v Value) { 1437 cname := C.CString(name) 1438 defer C.free(unsafe.Pointer(cname)) 1439 v.C = C.LLVMBuildNUWAdd(b.C, lhs.C, rhs.C, cname) 1440 return 1441} 1442func (b Builder) CreateFAdd(lhs, rhs Value, name string) (v Value) { 1443 cname := C.CString(name) 1444 defer C.free(unsafe.Pointer(cname)) 1445 v.C = C.LLVMBuildFAdd(b.C, lhs.C, rhs.C, cname) 1446 return 1447} 1448func (b Builder) CreateSub(lhs, rhs Value, name string) (v Value) { 1449 cname := C.CString(name) 1450 defer C.free(unsafe.Pointer(cname)) 1451 v.C = C.LLVMBuildSub(b.C, lhs.C, rhs.C, cname) 1452 return 1453} 1454func (b Builder) CreateNSWSub(lhs, rhs Value, name string) (v Value) { 1455 cname := C.CString(name) 1456 defer C.free(unsafe.Pointer(cname)) 1457 v.C = C.LLVMBuildNSWSub(b.C, lhs.C, rhs.C, cname) 1458 return 1459} 1460func (b Builder) CreateNUWSub(lhs, rhs Value, name string) (v Value) { 1461 cname := C.CString(name) 1462 defer C.free(unsafe.Pointer(cname)) 1463 v.C = C.LLVMBuildNUWSub(b.C, lhs.C, rhs.C, cname) 1464 return 1465} 1466func (b Builder) CreateFSub(lhs, rhs Value, name string) (v Value) { 1467 cname := C.CString(name) 1468 v.C = C.LLVMBuildFSub(b.C, lhs.C, rhs.C, cname) 1469 C.free(unsafe.Pointer(cname)) 1470 return 1471} 1472func (b Builder) CreateMul(lhs, rhs Value, name string) (v Value) { 1473 cname := C.CString(name) 1474 defer C.free(unsafe.Pointer(cname)) 1475 v.C = C.LLVMBuildMul(b.C, lhs.C, rhs.C, cname) 1476 return 1477} 1478func (b Builder) CreateNSWMul(lhs, rhs Value, name string) (v Value) { 1479 cname := C.CString(name) 1480 defer C.free(unsafe.Pointer(cname)) 1481 v.C = C.LLVMBuildNSWMul(b.C, lhs.C, rhs.C, cname) 1482 return 1483} 1484func (b Builder) CreateNUWMul(lhs, rhs Value, name string) (v Value) { 1485 cname := C.CString(name) 1486 defer C.free(unsafe.Pointer(cname)) 1487 v.C = C.LLVMBuildNUWMul(b.C, lhs.C, rhs.C, cname) 1488 return 1489} 1490func (b Builder) CreateFMul(lhs, rhs Value, name string) (v Value) { 1491 cname := C.CString(name) 1492 defer C.free(unsafe.Pointer(cname)) 1493 v.C = C.LLVMBuildFMul(b.C, lhs.C, rhs.C, cname) 1494 return 1495} 1496func (b Builder) CreateUDiv(lhs, rhs Value, name string) (v Value) { 1497 cname := C.CString(name) 1498 defer C.free(unsafe.Pointer(cname)) 1499 v.C = C.LLVMBuildUDiv(b.C, lhs.C, rhs.C, cname) 1500 return 1501} 1502func (b Builder) CreateSDiv(lhs, rhs Value, name string) (v Value) { 1503 cname := C.CString(name) 1504 defer C.free(unsafe.Pointer(cname)) 1505 v.C = C.LLVMBuildSDiv(b.C, lhs.C, rhs.C, cname) 1506 return 1507} 1508func (b Builder) CreateExactSDiv(lhs, rhs Value, name string) (v Value) { 1509 cname := C.CString(name) 1510 defer C.free(unsafe.Pointer(cname)) 1511 v.C = C.LLVMBuildExactSDiv(b.C, lhs.C, rhs.C, cname) 1512 return 1513} 1514func (b Builder) CreateFDiv(lhs, rhs Value, name string) (v Value) { 1515 cname := C.CString(name) 1516 defer C.free(unsafe.Pointer(cname)) 1517 v.C = C.LLVMBuildFDiv(b.C, lhs.C, rhs.C, cname) 1518 return 1519} 1520func (b Builder) CreateURem(lhs, rhs Value, name string) (v Value) { 1521 cname := C.CString(name) 1522 defer C.free(unsafe.Pointer(cname)) 1523 v.C = C.LLVMBuildURem(b.C, lhs.C, rhs.C, cname) 1524 return 1525} 1526func (b Builder) CreateSRem(lhs, rhs Value, name string) (v Value) { 1527 cname := C.CString(name) 1528 defer C.free(unsafe.Pointer(cname)) 1529 v.C = C.LLVMBuildSRem(b.C, lhs.C, rhs.C, cname) 1530 return 1531} 1532func (b Builder) CreateFRem(lhs, rhs Value, name string) (v Value) { 1533 cname := C.CString(name) 1534 defer C.free(unsafe.Pointer(cname)) 1535 v.C = C.LLVMBuildFRem(b.C, lhs.C, rhs.C, cname) 1536 return 1537} 1538func (b Builder) CreateShl(lhs, rhs Value, name string) (v Value) { 1539 cname := C.CString(name) 1540 defer C.free(unsafe.Pointer(cname)) 1541 v.C = C.LLVMBuildShl(b.C, lhs.C, rhs.C, cname) 1542 return 1543} 1544func (b Builder) CreateLShr(lhs, rhs Value, name string) (v Value) { 1545 cname := C.CString(name) 1546 defer C.free(unsafe.Pointer(cname)) 1547 v.C = C.LLVMBuildLShr(b.C, lhs.C, rhs.C, cname) 1548 return 1549} 1550func (b Builder) CreateAShr(lhs, rhs Value, name string) (v Value) { 1551 cname := C.CString(name) 1552 defer C.free(unsafe.Pointer(cname)) 1553 v.C = C.LLVMBuildAShr(b.C, lhs.C, rhs.C, cname) 1554 return 1555} 1556func (b Builder) CreateAnd(lhs, rhs Value, name string) (v Value) { 1557 cname := C.CString(name) 1558 defer C.free(unsafe.Pointer(cname)) 1559 v.C = C.LLVMBuildAnd(b.C, lhs.C, rhs.C, cname) 1560 return 1561} 1562func (b Builder) CreateOr(lhs, rhs Value, name string) (v Value) { 1563 cname := C.CString(name) 1564 defer C.free(unsafe.Pointer(cname)) 1565 v.C = C.LLVMBuildOr(b.C, lhs.C, rhs.C, cname) 1566 return 1567} 1568func (b Builder) CreateXor(lhs, rhs Value, name string) (v Value) { 1569 cname := C.CString(name) 1570 defer C.free(unsafe.Pointer(cname)) 1571 v.C = C.LLVMBuildXor(b.C, lhs.C, rhs.C, cname) 1572 return 1573} 1574func (b Builder) CreateBinOp(op Opcode, lhs, rhs Value, name string) (v Value) { 1575 cname := C.CString(name) 1576 defer C.free(unsafe.Pointer(cname)) 1577 v.C = C.LLVMBuildBinOp(b.C, C.LLVMOpcode(op), lhs.C, rhs.C, cname) 1578 return 1579} 1580func (b Builder) CreateNeg(v Value, name string) (rv Value) { 1581 cname := C.CString(name) 1582 defer C.free(unsafe.Pointer(cname)) 1583 rv.C = C.LLVMBuildNeg(b.C, v.C, cname) 1584 return 1585} 1586func (b Builder) CreateNSWNeg(v Value, name string) (rv Value) { 1587 cname := C.CString(name) 1588 defer C.free(unsafe.Pointer(cname)) 1589 rv.C = C.LLVMBuildNSWNeg(b.C, v.C, cname) 1590 return 1591} 1592func (b Builder) CreateNUWNeg(v Value, name string) (rv Value) { 1593 cname := C.CString(name) 1594 defer C.free(unsafe.Pointer(cname)) 1595 rv.C = C.LLVMBuildNUWNeg(b.C, v.C, cname) 1596 return 1597} 1598func (b Builder) CreateFNeg(v Value, name string) (rv Value) { 1599 cname := C.CString(name) 1600 defer C.free(unsafe.Pointer(cname)) 1601 rv.C = C.LLVMBuildFNeg(b.C, v.C, cname) 1602 return 1603} 1604func (b Builder) CreateNot(v Value, name string) (rv Value) { 1605 cname := C.CString(name) 1606 defer C.free(unsafe.Pointer(cname)) 1607 rv.C = C.LLVMBuildNot(b.C, v.C, cname) 1608 return 1609} 1610 1611// Memory 1612 1613func (b Builder) CreateMalloc(t Type, name string) (v Value) { 1614 cname := C.CString(name) 1615 defer C.free(unsafe.Pointer(cname)) 1616 v.C = C.LLVMBuildMalloc(b.C, t.C, cname) 1617 return 1618} 1619func (b Builder) CreateArrayMalloc(t Type, val Value, name string) (v Value) { 1620 cname := C.CString(name) 1621 defer C.free(unsafe.Pointer(cname)) 1622 v.C = C.LLVMBuildArrayMalloc(b.C, t.C, val.C, cname) 1623 return 1624} 1625func (b Builder) CreateAlloca(t Type, name string) (v Value) { 1626 cname := C.CString(name) 1627 defer C.free(unsafe.Pointer(cname)) 1628 v.C = C.LLVMBuildAlloca(b.C, t.C, cname) 1629 return 1630} 1631func (b Builder) CreateArrayAlloca(t Type, val Value, name string) (v Value) { 1632 cname := C.CString(name) 1633 defer C.free(unsafe.Pointer(cname)) 1634 v.C = C.LLVMBuildArrayAlloca(b.C, t.C, val.C, cname) 1635 return 1636} 1637func (b Builder) CreateFree(p Value) (v Value) { 1638 v.C = C.LLVMBuildFree(b.C, p.C) 1639 return 1640} 1641func (b Builder) CreateLoad(p Value, name string) (v Value) { 1642 cname := C.CString(name) 1643 defer C.free(unsafe.Pointer(cname)) 1644 v.C = C.LLVMBuildLoad(b.C, p.C, cname) 1645 return 1646} 1647func (b Builder) CreateStore(val Value, p Value) (v Value) { 1648 v.C = C.LLVMBuildStore(b.C, val.C, p.C) 1649 return 1650} 1651func (b Builder) CreateGEP(p Value, indices []Value, name string) (v Value) { 1652 cname := C.CString(name) 1653 defer C.free(unsafe.Pointer(cname)) 1654 ptr, nvals := llvmValueRefs(indices) 1655 v.C = C.LLVMBuildGEP(b.C, p.C, ptr, nvals, cname) 1656 return 1657} 1658func (b Builder) CreateInBoundsGEP(p Value, indices []Value, name string) (v Value) { 1659 cname := C.CString(name) 1660 defer C.free(unsafe.Pointer(cname)) 1661 ptr, nvals := llvmValueRefs(indices) 1662 v.C = C.LLVMBuildInBoundsGEP(b.C, p.C, ptr, nvals, cname) 1663 return 1664} 1665func (b Builder) CreateStructGEP(p Value, i int, name string) (v Value) { 1666 cname := C.CString(name) 1667 defer C.free(unsafe.Pointer(cname)) 1668 v.C = C.LLVMBuildStructGEP(b.C, p.C, C.unsigned(i), cname) 1669 return 1670} 1671func (b Builder) CreateGlobalString(str, name string) (v Value) { 1672 cstr := C.CString(str) 1673 defer C.free(unsafe.Pointer(cstr)) 1674 cname := C.CString(name) 1675 defer C.free(unsafe.Pointer(cname)) 1676 v.C = C.LLVMBuildGlobalString(b.C, cstr, cname) 1677 return 1678} 1679func (b Builder) CreateGlobalStringPtr(str, name string) (v Value) { 1680 cstr := C.CString(str) 1681 defer C.free(unsafe.Pointer(cstr)) 1682 cname := C.CString(name) 1683 defer C.free(unsafe.Pointer(cname)) 1684 v.C = C.LLVMBuildGlobalStringPtr(b.C, cstr, cname) 1685 return 1686} 1687func (b Builder) CreateAtomicRMW(op AtomicRMWBinOp, ptr, val Value, ordering AtomicOrdering, singleThread bool) (v Value) { 1688 v.C = C.LLVMBuildAtomicRMW(b.C, C.LLVMAtomicRMWBinOp(op), ptr.C, val.C, C.LLVMAtomicOrdering(ordering), boolToLLVMBool(singleThread)) 1689 return 1690} 1691func (b Builder) CreateAtomicCmpXchg(ptr, cmp, newVal Value, successOrdering, failureOrdering AtomicOrdering, singleThread bool) (v Value) { 1692 v.C = C.LLVMBuildAtomicCmpXchg(b.C, ptr.C, cmp.C, newVal.C, C.LLVMAtomicOrdering(successOrdering), C.LLVMAtomicOrdering(failureOrdering), boolToLLVMBool(singleThread)) 1693 return 1694} 1695 1696// Casts 1697func (b Builder) CreateTrunc(val Value, t Type, name string) (v Value) { 1698 cname := C.CString(name) 1699 defer C.free(unsafe.Pointer(cname)) 1700 v.C = C.LLVMBuildTrunc(b.C, val.C, t.C, cname) 1701 return 1702} 1703func (b Builder) CreateZExt(val Value, t Type, name string) (v Value) { 1704 cname := C.CString(name) 1705 defer C.free(unsafe.Pointer(cname)) 1706 v.C = C.LLVMBuildZExt(b.C, val.C, t.C, cname) 1707 return 1708} 1709func (b Builder) CreateSExt(val Value, t Type, name string) (v Value) { 1710 cname := C.CString(name) 1711 defer C.free(unsafe.Pointer(cname)) 1712 v.C = C.LLVMBuildSExt(b.C, val.C, t.C, cname) 1713 return 1714} 1715func (b Builder) CreateFPToUI(val Value, t Type, name string) (v Value) { 1716 cname := C.CString(name) 1717 defer C.free(unsafe.Pointer(cname)) 1718 v.C = C.LLVMBuildFPToUI(b.C, val.C, t.C, cname) 1719 return 1720} 1721func (b Builder) CreateFPToSI(val Value, t Type, name string) (v Value) { 1722 cname := C.CString(name) 1723 defer C.free(unsafe.Pointer(cname)) 1724 v.C = C.LLVMBuildFPToSI(b.C, val.C, t.C, cname) 1725 return 1726} 1727func (b Builder) CreateUIToFP(val Value, t Type, name string) (v Value) { 1728 cname := C.CString(name) 1729 defer C.free(unsafe.Pointer(cname)) 1730 v.C = C.LLVMBuildUIToFP(b.C, val.C, t.C, cname) 1731 return 1732} 1733func (b Builder) CreateSIToFP(val Value, t Type, name string) (v Value) { 1734 cname := C.CString(name) 1735 defer C.free(unsafe.Pointer(cname)) 1736 v.C = C.LLVMBuildSIToFP(b.C, val.C, t.C, cname) 1737 return 1738} 1739func (b Builder) CreateFPTrunc(val Value, t Type, name string) (v Value) { 1740 cname := C.CString(name) 1741 defer C.free(unsafe.Pointer(cname)) 1742 v.C = C.LLVMBuildFPTrunc(b.C, val.C, t.C, cname) 1743 return 1744} 1745func (b Builder) CreateFPExt(val Value, t Type, name string) (v Value) { 1746 cname := C.CString(name) 1747 defer C.free(unsafe.Pointer(cname)) 1748 v.C = C.LLVMBuildFPExt(b.C, val.C, t.C, cname) 1749 return 1750} 1751func (b Builder) CreatePtrToInt(val Value, t Type, name string) (v Value) { 1752 cname := C.CString(name) 1753 defer C.free(unsafe.Pointer(cname)) 1754 v.C = C.LLVMBuildPtrToInt(b.C, val.C, t.C, cname) 1755 return 1756} 1757func (b Builder) CreateIntToPtr(val Value, t Type, name string) (v Value) { 1758 cname := C.CString(name) 1759 defer C.free(unsafe.Pointer(cname)) 1760 v.C = C.LLVMBuildIntToPtr(b.C, val.C, t.C, cname) 1761 return 1762} 1763func (b Builder) CreateBitCast(val Value, t Type, name string) (v Value) { 1764 cname := C.CString(name) 1765 defer C.free(unsafe.Pointer(cname)) 1766 v.C = C.LLVMBuildBitCast(b.C, val.C, t.C, cname) 1767 return 1768} 1769func (b Builder) CreateZExtOrBitCast(val Value, t Type, name string) (v Value) { 1770 cname := C.CString(name) 1771 defer C.free(unsafe.Pointer(cname)) 1772 v.C = C.LLVMBuildZExtOrBitCast(b.C, val.C, t.C, cname) 1773 return 1774} 1775func (b Builder) CreateSExtOrBitCast(val Value, t Type, name string) (v Value) { 1776 cname := C.CString(name) 1777 defer C.free(unsafe.Pointer(cname)) 1778 v.C = C.LLVMBuildSExtOrBitCast(b.C, val.C, t.C, cname) 1779 return 1780} 1781func (b Builder) CreateTruncOrBitCast(val Value, t Type, name string) (v Value) { 1782 cname := C.CString(name) 1783 defer C.free(unsafe.Pointer(cname)) 1784 v.C = C.LLVMBuildTruncOrBitCast(b.C, val.C, t.C, cname) 1785 return 1786} 1787func (b Builder) CreateCast(val Value, op Opcode, t Type, name string) (v Value) { 1788 cname := C.CString(name) 1789 defer C.free(unsafe.Pointer(cname)) 1790 v.C = C.LLVMBuildCast(b.C, C.LLVMOpcode(op), val.C, t.C, cname) 1791 return 1792} // 1793func (b Builder) CreatePointerCast(val Value, t Type, name string) (v Value) { 1794 cname := C.CString(name) 1795 defer C.free(unsafe.Pointer(cname)) 1796 v.C = C.LLVMBuildPointerCast(b.C, val.C, t.C, cname) 1797 return 1798} 1799func (b Builder) CreateIntCast(val Value, t Type, name string) (v Value) { 1800 cname := C.CString(name) 1801 defer C.free(unsafe.Pointer(cname)) 1802 v.C = C.LLVMBuildIntCast(b.C, val.C, t.C, cname) 1803 return 1804} 1805func (b Builder) CreateFPCast(val Value, t Type, name string) (v Value) { 1806 cname := C.CString(name) 1807 defer C.free(unsafe.Pointer(cname)) 1808 v.C = C.LLVMBuildFPCast(b.C, val.C, t.C, cname) 1809 return 1810} 1811 1812// Comparisons 1813func (b Builder) CreateICmp(pred IntPredicate, lhs, rhs Value, name string) (v Value) { 1814 cname := C.CString(name) 1815 defer C.free(unsafe.Pointer(cname)) 1816 v.C = C.LLVMBuildICmp(b.C, C.LLVMIntPredicate(pred), lhs.C, rhs.C, cname) 1817 return 1818} 1819func (b Builder) CreateFCmp(pred FloatPredicate, lhs, rhs Value, name string) (v Value) { 1820 cname := C.CString(name) 1821 defer C.free(unsafe.Pointer(cname)) 1822 v.C = C.LLVMBuildFCmp(b.C, C.LLVMRealPredicate(pred), lhs.C, rhs.C, cname) 1823 return 1824} 1825 1826// Miscellaneous instructions 1827func (b Builder) CreatePHI(t Type, name string) (v Value) { 1828 cname := C.CString(name) 1829 defer C.free(unsafe.Pointer(cname)) 1830 v.C = C.LLVMBuildPhi(b.C, t.C, cname) 1831 return 1832} 1833func (b Builder) CreateCall(fn Value, args []Value, name string) (v Value) { 1834 cname := C.CString(name) 1835 defer C.free(unsafe.Pointer(cname)) 1836 ptr, nvals := llvmValueRefs(args) 1837 v.C = C.LLVMBuildCall(b.C, fn.C, ptr, nvals, cname) 1838 return 1839} 1840 1841func (b Builder) CreateSelect(ifv, thenv, elsev Value, name string) (v Value) { 1842 cname := C.CString(name) 1843 defer C.free(unsafe.Pointer(cname)) 1844 v.C = C.LLVMBuildSelect(b.C, ifv.C, thenv.C, elsev.C, cname) 1845 return 1846} 1847 1848func (b Builder) CreateVAArg(list Value, t Type, name string) (v Value) { 1849 cname := C.CString(name) 1850 defer C.free(unsafe.Pointer(cname)) 1851 v.C = C.LLVMBuildVAArg(b.C, list.C, t.C, cname) 1852 return 1853} 1854func (b Builder) CreateExtractElement(vec, i Value, name string) (v Value) { 1855 cname := C.CString(name) 1856 defer C.free(unsafe.Pointer(cname)) 1857 v.C = C.LLVMBuildExtractElement(b.C, vec.C, i.C, cname) 1858 return 1859} 1860func (b Builder) CreateInsertElement(vec, elt, i Value, name string) (v Value) { 1861 cname := C.CString(name) 1862 defer C.free(unsafe.Pointer(cname)) 1863 v.C = C.LLVMBuildInsertElement(b.C, vec.C, elt.C, i.C, cname) 1864 return 1865} 1866func (b Builder) CreateShuffleVector(v1, v2, mask Value, name string) (v Value) { 1867 cname := C.CString(name) 1868 defer C.free(unsafe.Pointer(cname)) 1869 v.C = C.LLVMBuildShuffleVector(b.C, v1.C, v2.C, mask.C, cname) 1870 return 1871} 1872func (b Builder) CreateExtractValue(agg Value, i int, name string) (v Value) { 1873 cname := C.CString(name) 1874 defer C.free(unsafe.Pointer(cname)) 1875 v.C = C.LLVMBuildExtractValue(b.C, agg.C, C.unsigned(i), cname) 1876 return 1877} 1878func (b Builder) CreateInsertValue(agg, elt Value, i int, name string) (v Value) { 1879 cname := C.CString(name) 1880 defer C.free(unsafe.Pointer(cname)) 1881 v.C = C.LLVMBuildInsertValue(b.C, agg.C, elt.C, C.unsigned(i), cname) 1882 return 1883} 1884 1885func (b Builder) CreateIsNull(val Value, name string) (v Value) { 1886 cname := C.CString(name) 1887 defer C.free(unsafe.Pointer(cname)) 1888 v.C = C.LLVMBuildIsNull(b.C, val.C, cname) 1889 return 1890} 1891func (b Builder) CreateIsNotNull(val Value, name string) (v Value) { 1892 cname := C.CString(name) 1893 defer C.free(unsafe.Pointer(cname)) 1894 v.C = C.LLVMBuildIsNotNull(b.C, val.C, cname) 1895 return 1896} 1897func (b Builder) CreatePtrDiff(lhs, rhs Value, name string) (v Value) { 1898 cname := C.CString(name) 1899 defer C.free(unsafe.Pointer(cname)) 1900 v.C = C.LLVMBuildPtrDiff(b.C, lhs.C, rhs.C, cname) 1901 return 1902} 1903 1904func (b Builder) CreateLandingPad(t Type, nclauses int, name string) (l Value) { 1905 cname := C.CString(name) 1906 defer C.free(unsafe.Pointer(cname)) 1907 l.C = C.LLVMBuildLandingPad(b.C, t.C, nil, C.unsigned(nclauses), cname) 1908 return l 1909} 1910 1911func (l Value) AddClause(v Value) { 1912 C.LLVMAddClause(l.C, v.C) 1913} 1914 1915func (l Value) SetCleanup(cleanup bool) { 1916 C.LLVMSetCleanup(l.C, boolToLLVMBool(cleanup)) 1917} 1918 1919func (b Builder) CreateResume(ex Value) (v Value) { 1920 v.C = C.LLVMBuildResume(b.C, ex.C) 1921 return 1922} 1923 1924//------------------------------------------------------------------------- 1925// llvm.ModuleProvider 1926//------------------------------------------------------------------------- 1927 1928// Changes the type of M so it can be passed to FunctionPassManagers and the 1929// JIT. They take ModuleProviders for historical reasons. 1930func NewModuleProviderForModule(m Module) (mp ModuleProvider) { 1931 mp.C = C.LLVMCreateModuleProviderForExistingModule(m.C) 1932 return 1933} 1934 1935// Destroys the module M. 1936func (mp ModuleProvider) Dispose() { C.LLVMDisposeModuleProvider(mp.C) } 1937 1938//------------------------------------------------------------------------- 1939// llvm.MemoryBuffer 1940//------------------------------------------------------------------------- 1941 1942func NewMemoryBufferFromFile(path string) (b MemoryBuffer, err error) { 1943 var cmsg *C.char 1944 cpath := C.CString(path) 1945 defer C.free(unsafe.Pointer(cpath)) 1946 fail := C.LLVMCreateMemoryBufferWithContentsOfFile(cpath, &b.C, &cmsg) 1947 if fail != 0 { 1948 b.C = nil 1949 err = errors.New(C.GoString(cmsg)) 1950 C.LLVMDisposeMessage(cmsg) 1951 } 1952 return 1953} 1954 1955func NewMemoryBufferFromStdin() (b MemoryBuffer, err error) { 1956 var cmsg *C.char 1957 fail := C.LLVMCreateMemoryBufferWithSTDIN(&b.C, &cmsg) 1958 if fail != 0 { 1959 b.C = nil 1960 err = errors.New(C.GoString(cmsg)) 1961 C.LLVMDisposeMessage(cmsg) 1962 } 1963 return 1964} 1965 1966func (b MemoryBuffer) Bytes() []byte { 1967 cstart := C.LLVMGetBufferStart(b.C) 1968 csize := C.LLVMGetBufferSize(b.C) 1969 return C.GoBytes(unsafe.Pointer(cstart), C.int(csize)) 1970} 1971 1972func (b MemoryBuffer) Dispose() { C.LLVMDisposeMemoryBuffer(b.C) } 1973 1974//------------------------------------------------------------------------- 1975// llvm.PassManager 1976//------------------------------------------------------------------------- 1977 1978// Constructs a new whole-module pass pipeline. This type of pipeline is 1979// suitable for link-time optimization and whole-module transformations. 1980// See llvm::PassManager::PassManager. 1981func NewPassManager() (pm PassManager) { pm.C = C.LLVMCreatePassManager(); return } 1982 1983// Constructs a new function-by-function pass pipeline over the module 1984// provider. It does not take ownership of the module provider. This type of 1985// pipeline is suitable for code generation and JIT compilation tasks. 1986// See llvm::FunctionPassManager::FunctionPassManager. 1987func NewFunctionPassManagerForModule(m Module) (pm PassManager) { 1988 pm.C = C.LLVMCreateFunctionPassManagerForModule(m.C) 1989 return 1990} 1991 1992// Initializes, executes on the provided module, and finalizes all of the 1993// passes scheduled in the pass manager. Returns 1 if any of the passes 1994// modified the module, 0 otherwise. See llvm::PassManager::run(Module&). 1995func (pm PassManager) Run(m Module) bool { return C.LLVMRunPassManager(pm.C, m.C) != 0 } 1996 1997// Initializes all of the function passes scheduled in the function pass 1998// manager. Returns 1 if any of the passes modified the module, 0 otherwise. 1999// See llvm::FunctionPassManager::doInitialization. 2000func (pm PassManager) InitializeFunc() bool { return C.LLVMInitializeFunctionPassManager(pm.C) != 0 } 2001 2002// Executes all of the function passes scheduled in the function pass manager 2003// on the provided function. Returns 1 if any of the passes modified the 2004// function, false otherwise. 2005// See llvm::FunctionPassManager::run(Function&). 2006func (pm PassManager) RunFunc(f Value) bool { return C.LLVMRunFunctionPassManager(pm.C, f.C) != 0 } 2007 2008// Finalizes all of the function passes scheduled in the function pass 2009// manager. Returns 1 if any of the passes modified the module, 0 otherwise. 2010// See llvm::FunctionPassManager::doFinalization. 2011func (pm PassManager) FinalizeFunc() bool { return C.LLVMFinalizeFunctionPassManager(pm.C) != 0 } 2012 2013// Frees the memory of a pass pipeline. For function pipelines, does not free 2014// the module provider. 2015// See llvm::PassManagerBase::~PassManagerBase. 2016func (pm PassManager) Dispose() { C.LLVMDisposePassManager(pm.C) } 2017