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