1 //===- ir.go - Bindings for ir --------------------------------------------===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 // 9 // This file defines bindings for the ir component. 10 // 11 //===----------------------------------------------------------------------===// 12 13 package llvm 14 15 /* 16 #include "llvm-c/Core.h" 17 #include "llvm-c/Comdat.h" 18 #include "IRBindings.h" 19 #include <stdlib.h> 20 */ 21 import "C" 22 import "unsafe" 23 import "errors" 24 25 type ( 26 // We use these weird structs here because *Ref types are pointers and 27 // Go's spec says that a pointer cannot be used as a receiver base type. 28 Context struct { 29 C C.LLVMContextRef 30 } 31 Module struct { 32 C C.LLVMModuleRef 33 } 34 Type struct { 35 C C.LLVMTypeRef 36 } 37 Value struct { 38 C C.LLVMValueRef 39 } 40 Comdat struct { 41 C C.LLVMComdatRef 42 } 43 BasicBlock struct { 44 C C.LLVMBasicBlockRef 45 } 46 Builder struct { 47 C C.LLVMBuilderRef 48 } 49 ModuleProvider struct { 50 C C.LLVMModuleProviderRef 51 } 52 MemoryBuffer struct { 53 C C.LLVMMemoryBufferRef 54 } 55 PassManager struct { 56 C C.LLVMPassManagerRef 57 } 58 Use struct { 59 C C.LLVMUseRef 60 } 61 Metadata struct { 62 C C.LLVMMetadataRef 63 } 64 Attribute struct { 65 C C.LLVMAttributeRef 66 } 67 Opcode C.LLVMOpcode 68 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 81 func (c Context) IsNil() bool { return c.C == nil } 82 func (c Module) IsNil() bool { return c.C == nil } 83 func (c Type) IsNil() bool { return c.C == nil } 84 func (c Value) IsNil() bool { return c.C == nil } 85 func (c BasicBlock) IsNil() bool { return c.C == nil } 86 func (c Builder) IsNil() bool { return c.C == nil } 87 func (c ModuleProvider) IsNil() bool { return c.C == nil } 88 func (c MemoryBuffer) IsNil() bool { return c.C == nil } 89 func (c PassManager) IsNil() bool { return c.C == nil } 90 func (c Use) IsNil() bool { return c.C == nil } 91 func (c Attribute) IsNil() bool { return c.C == nil } 92 func (c Metadata) IsNil() bool { return c.C == nil } 93 94 // helpers 95 func llvmTypeRefPtr(t *Type) *C.LLVMTypeRef { return (*C.LLVMTypeRef)(unsafe.Pointer(t)) } 96 func llvmValueRefPtr(t *Value) *C.LLVMValueRef { return (*C.LLVMValueRef)(unsafe.Pointer(t)) } 97 func llvmMetadataRefPtr(t *Metadata) *C.LLVMMetadataRef { 98 return (*C.LLVMMetadataRef)(unsafe.Pointer(t)) 99 } 100 func llvmBasicBlockRefPtr(t *BasicBlock) *C.LLVMBasicBlockRef { 101 return (*C.LLVMBasicBlockRef)(unsafe.Pointer(t)) 102 } 103 func boolToLLVMBool(b bool) C.LLVMBool { 104 if b { 105 return C.LLVMBool(1) 106 } 107 return C.LLVMBool(0) 108 } 109 110 func 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 119 func 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 132 const ( 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 198 const ( 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 212 const ( 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 226 const ( 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 249 const ( 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 267 const ( 268 DefaultVisibility Visibility = C.LLVMDefaultVisibility 269 HiddenVisibility Visibility = C.LLVMHiddenVisibility 270 ProtectedVisibility Visibility = C.LLVMProtectedVisibility 271 ) 272 273 //------------------------------------------------------------------------- 274 // llvm.CallConv 275 //------------------------------------------------------------------------- 276 277 const ( 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 289 const ( 290 AnyComdatSelectionKind ComdatSelectionKind = C.LLVMAnyComdatSelectionKind 291 ExactMatchComdatSelectionKind ComdatSelectionKind = C.LLVMExactMatchComdatSelectionKind 292 LargestComdatSelectionKind ComdatSelectionKind = C.LLVMLargestComdatSelectionKind 293 NoDuplicatesComdatSelectionKind ComdatSelectionKind = C.LLVMNoDuplicatesComdatSelectionKind 294 SameSizeComdatSelectionKind ComdatSelectionKind = C.LLVMSameSizeComdatSelectionKind 295 ) 296 297 //------------------------------------------------------------------------- 298 // llvm.IntPredicate 299 //------------------------------------------------------------------------- 300 301 const ( 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 318 const ( 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 341 const ( 342 LandingPadCatch LandingPadClause = C.LLVMLandingPadCatch 343 LandingPadFilter LandingPadClause = C.LLVMLandingPadFilter 344 ) 345 346 //------------------------------------------------------------------------- 347 // llvm.InlineAsmDialect 348 //------------------------------------------------------------------------- 349 350 const ( 351 InlineAsmDialectATT InlineAsmDialect = C.LLVMInlineAsmDialectATT 352 InlineAsmDialectIntel InlineAsmDialect = C.LLVMInlineAsmDialectIntel 353 ) 354 355 //------------------------------------------------------------------------- 356 // llvm.Context 357 //------------------------------------------------------------------------- 358 359 func NewContext() Context { return Context{C.LLVMContextCreate()} } 360 func GlobalContext() Context { return Context{C.LLVMGetGlobalContext()} } 361 func (c Context) Dispose() { C.LLVMContextDispose(c.C) } 362 363 func (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 370 func 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 381 func 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 388 func (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 393 func (a Attribute) GetEnumKind() (id int) { 394 id = int(C.LLVMGetEnumAttributeKind(a.C)) 395 return 396 } 397 398 func (a Attribute) GetEnumValue() (val uint64) { 399 val = uint64(C.LLVMGetEnumAttributeValue(a.C)) 400 return 401 } 402 403 func (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 414 func (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 420 func (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 426 func (a Attribute) IsEnum() bool { 427 return C.LLVMIsEnumAttribute(a.C) != 0 428 } 429 430 func (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. 440 func 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 447 func (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 455 func (m Module) Dispose() { C.LLVMDisposeModule(m.C) } 456 457 // Data layout. See Module::getDataLayout. 458 func (m Module) DataLayout() string { 459 clayout := C.LLVMGetDataLayout(m.C) 460 return C.GoString(clayout) 461 } 462 463 func (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. 470 func (m Module) Target() string { 471 ctarget := C.LLVMGetTarget(m.C) 472 return C.GoString(ctarget) 473 } 474 func (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 480 func (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. 488 func (m Module) Dump() { 489 C.LLVMDumpModule(m.C) 490 } 491 492 func (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. 500 func (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 506 func (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 512 func (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. 536 func (t Type) TypeKind() TypeKind { return TypeKind(C.LLVMGetTypeKind(t.C)) } 537 538 // See llvm::LLVMType::getContext. 539 func (t Type) Context() (c Context) { 540 c.C = C.LLVMGetTypeContext(t.C) 541 return 542 } 543 544 // Operations on integer types 545 func (c Context) Int1Type() (t Type) { t.C = C.LLVMInt1TypeInContext(c.C); return } 546 func (c Context) Int8Type() (t Type) { t.C = C.LLVMInt8TypeInContext(c.C); return } 547 func (c Context) Int16Type() (t Type) { t.C = C.LLVMInt16TypeInContext(c.C); return } 548 func (c Context) Int32Type() (t Type) { t.C = C.LLVMInt32TypeInContext(c.C); return } 549 func (c Context) Int64Type() (t Type) { t.C = C.LLVMInt64TypeInContext(c.C); return } 550 func (c Context) IntType(numbits int) (t Type) { 551 t.C = C.LLVMIntTypeInContext(c.C, C.unsigned(numbits)) 552 return 553 } 554 555 func Int1Type() (t Type) { t.C = C.LLVMInt1Type(); return } 556 func Int8Type() (t Type) { t.C = C.LLVMInt8Type(); return } 557 func Int16Type() (t Type) { t.C = C.LLVMInt16Type(); return } 558 func Int32Type() (t Type) { t.C = C.LLVMInt32Type(); return } 559 func Int64Type() (t Type) { t.C = C.LLVMInt64Type(); return } 560 561 func IntType(numbits int) (t Type) { 562 t.C = C.LLVMIntType(C.unsigned(numbits)) 563 return 564 } 565 566 func (t Type) IntTypeWidth() int { 567 return int(C.LLVMGetIntTypeWidth(t.C)) 568 } 569 570 // Operations on real types 571 func (c Context) FloatType() (t Type) { t.C = C.LLVMFloatTypeInContext(c.C); return } 572 func (c Context) DoubleType() (t Type) { t.C = C.LLVMDoubleTypeInContext(c.C); return } 573 func (c Context) X86FP80Type() (t Type) { t.C = C.LLVMX86FP80TypeInContext(c.C); return } 574 func (c Context) FP128Type() (t Type) { t.C = C.LLVMFP128TypeInContext(c.C); return } 575 func (c Context) PPCFP128Type() (t Type) { t.C = C.LLVMPPCFP128TypeInContext(c.C); return } 576 577 func FloatType() (t Type) { t.C = C.LLVMFloatType(); return } 578 func DoubleType() (t Type) { t.C = C.LLVMDoubleType(); return } 579 func X86FP80Type() (t Type) { t.C = C.LLVMX86FP80Type(); return } 580 func FP128Type() (t Type) { t.C = C.LLVMFP128Type(); return } 581 func PPCFP128Type() (t Type) { t.C = C.LLVMPPCFP128Type(); return } 582 583 // Operations on function types 584 func 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 598 func (t Type) IsFunctionVarArg() bool { return C.LLVMIsFunctionVarArg(t.C) != 0 } 599 func (t Type) ReturnType() (rt Type) { rt.C = C.LLVMGetReturnType(t.C); return } 600 func (t Type) ParamTypesCount() int { return int(C.LLVMCountParamTypes(t.C)) } 601 func (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 612 func (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 626 func 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 637 func (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 644 func (t Type) StructName() string { 645 return C.GoString(C.LLVMGetStructName(t.C)) 646 } 647 648 func (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 658 func (t Type) IsStructPacked() bool { return C.LLVMIsPackedStruct(t.C) != 0 } 659 func (t Type) StructElementTypesCount() int { return int(C.LLVMCountStructElementTypes(t.C)) } 660 func (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) 669 func (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 675 func ArrayType(elementType Type, elementCount int) (t Type) { 676 t.C = C.LLVMArrayType(elementType.C, C.unsigned(elementCount)) 677 return 678 } 679 func PointerType(elementType Type, addressSpace int) (t Type) { 680 t.C = C.LLVMPointerType(elementType.C, C.unsigned(addressSpace)) 681 return 682 } 683 func VectorType(elementType Type, elementCount int) (t Type) { 684 t.C = C.LLVMVectorType(elementType.C, C.unsigned(elementCount)) 685 return 686 } 687 688 func (t Type) ElementType() (rt Type) { rt.C = C.LLVMGetElementType(t.C); return } 689 func (t Type) ArrayLength() int { return int(C.LLVMGetArrayLength(t.C)) } 690 func (t Type) PointerAddressSpace() int { return int(C.LLVMGetPointerAddressSpace(t.C)) } 691 func (t Type) VectorSize() int { return int(C.LLVMGetVectorSize(t.C)) } 692 693 // Operations on other types 694 func (c Context) VoidType() (t Type) { t.C = C.LLVMVoidTypeInContext(c.C); return } 695 func (c Context) LabelType() (t Type) { t.C = C.LLVMLabelTypeInContext(c.C); return } 696 func (c Context) TokenType() (t Type) { t.C = C.LLVMTokenTypeInContext(c.C); return } 697 698 func VoidType() (t Type) { t.C = C.LLVMVoidType(); return } 699 func LabelType() (t Type) { t.C = C.LLVMLabelType(); return } 700 701 //------------------------------------------------------------------------- 702 // llvm.Value 703 //------------------------------------------------------------------------- 704 705 // Operations on all values 706 func (v Value) Type() (t Type) { t.C = C.LLVMTypeOf(v.C); return } 707 func (v Value) Name() string { return C.GoString(C.LLVMGetValueName(v.C)) } 708 func (v Value) SetName(name string) { 709 cname := C.CString(name) 710 defer C.free(unsafe.Pointer(cname)) 711 C.LLVMSetValueName(v.C, cname) 712 } 713 func (v Value) Dump() { C.LLVMDumpValue(v.C) } 714 func (v Value) ReplaceAllUsesWith(nv Value) { C.LLVMReplaceAllUsesWith(v.C, nv.C) } 715 func (v Value) HasMetadata() bool { return C.LLVMHasMetadata(v.C) != 0 } 716 func (v Value) Metadata(kind int) (rv Value) { 717 rv.C = C.LLVMGetMetadata(v.C, C.unsigned(kind)) 718 return 719 } 720 func (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<>. 727 func (v Value) IsAArgument() (rv Value) { rv.C = C.LLVMIsAArgument(v.C); return } 728 func (v Value) IsABasicBlock() (rv Value) { rv.C = C.LLVMIsABasicBlock(v.C); return } 729 func (v Value) IsAInlineAsm() (rv Value) { rv.C = C.LLVMIsAInlineAsm(v.C); return } 730 func (v Value) IsAUser() (rv Value) { rv.C = C.LLVMIsAUser(v.C); return } 731 func (v Value) IsAConstant() (rv Value) { rv.C = C.LLVMIsAConstant(v.C); return } 732 func (v Value) IsAConstantAggregateZero() (rv Value) { 733 rv.C = C.LLVMIsAConstantAggregateZero(v.C) 734 return 735 } 736 func (v Value) IsAConstantArray() (rv Value) { rv.C = C.LLVMIsAConstantArray(v.C); return } 737 func (v Value) IsAConstantExpr() (rv Value) { rv.C = C.LLVMIsAConstantExpr(v.C); return } 738 func (v Value) IsAConstantFP() (rv Value) { rv.C = C.LLVMIsAConstantFP(v.C); return } 739 func (v Value) IsAConstantInt() (rv Value) { rv.C = C.LLVMIsAConstantInt(v.C); return } 740 func (v Value) IsAConstantPointerNull() (rv Value) { rv.C = C.LLVMIsAConstantPointerNull(v.C); return } 741 func (v Value) IsAConstantStruct() (rv Value) { rv.C = C.LLVMIsAConstantStruct(v.C); return } 742 func (v Value) IsAConstantVector() (rv Value) { rv.C = C.LLVMIsAConstantVector(v.C); return } 743 func (v Value) IsAGlobalValue() (rv Value) { rv.C = C.LLVMIsAGlobalValue(v.C); return } 744 func (v Value) IsAFunction() (rv Value) { rv.C = C.LLVMIsAFunction(v.C); return } 745 func (v Value) IsAGlobalAlias() (rv Value) { rv.C = C.LLVMIsAGlobalAlias(v.C); return } 746 func (v Value) IsAGlobalVariable() (rv Value) { rv.C = C.LLVMIsAGlobalVariable(v.C); return } 747 func (v Value) IsAUndefValue() (rv Value) { rv.C = C.LLVMIsAUndefValue(v.C); return } 748 func (v Value) IsAInstruction() (rv Value) { rv.C = C.LLVMIsAInstruction(v.C); return } 749 func (v Value) IsABinaryOperator() (rv Value) { rv.C = C.LLVMIsABinaryOperator(v.C); return } 750 func (v Value) IsACallInst() (rv Value) { rv.C = C.LLVMIsACallInst(v.C); return } 751 func (v Value) IsAIntrinsicInst() (rv Value) { rv.C = C.LLVMIsAIntrinsicInst(v.C); return } 752 func (v Value) IsADbgInfoIntrinsic() (rv Value) { rv.C = C.LLVMIsADbgInfoIntrinsic(v.C); return } 753 func (v Value) IsADbgDeclareInst() (rv Value) { rv.C = C.LLVMIsADbgDeclareInst(v.C); return } 754 func (v Value) IsAMemIntrinsic() (rv Value) { rv.C = C.LLVMIsAMemIntrinsic(v.C); return } 755 func (v Value) IsAMemCpyInst() (rv Value) { rv.C = C.LLVMIsAMemCpyInst(v.C); return } 756 func (v Value) IsAMemMoveInst() (rv Value) { rv.C = C.LLVMIsAMemMoveInst(v.C); return } 757 func (v Value) IsAMemSetInst() (rv Value) { rv.C = C.LLVMIsAMemSetInst(v.C); return } 758 func (v Value) IsACmpInst() (rv Value) { rv.C = C.LLVMIsACmpInst(v.C); return } 759 func (v Value) IsAFCmpInst() (rv Value) { rv.C = C.LLVMIsAFCmpInst(v.C); return } 760 func (v Value) IsAICmpInst() (rv Value) { rv.C = C.LLVMIsAICmpInst(v.C); return } 761 func (v Value) IsAExtractElementInst() (rv Value) { rv.C = C.LLVMIsAExtractElementInst(v.C); return } 762 func (v Value) IsAGetElementPtrInst() (rv Value) { rv.C = C.LLVMIsAGetElementPtrInst(v.C); return } 763 func (v Value) IsAInsertElementInst() (rv Value) { rv.C = C.LLVMIsAInsertElementInst(v.C); return } 764 func (v Value) IsAInsertValueInst() (rv Value) { rv.C = C.LLVMIsAInsertValueInst(v.C); return } 765 func (v Value) IsAPHINode() (rv Value) { rv.C = C.LLVMIsAPHINode(v.C); return } 766 func (v Value) IsASelectInst() (rv Value) { rv.C = C.LLVMIsASelectInst(v.C); return } 767 func (v Value) IsAShuffleVectorInst() (rv Value) { rv.C = C.LLVMIsAShuffleVectorInst(v.C); return } 768 func (v Value) IsAStoreInst() (rv Value) { rv.C = C.LLVMIsAStoreInst(v.C); return } 769 func (v Value) IsABranchInst() (rv Value) { rv.C = C.LLVMIsABranchInst(v.C); return } 770 func (v Value) IsAInvokeInst() (rv Value) { rv.C = C.LLVMIsAInvokeInst(v.C); return } 771 func (v Value) IsAReturnInst() (rv Value) { rv.C = C.LLVMIsAReturnInst(v.C); return } 772 func (v Value) IsASwitchInst() (rv Value) { rv.C = C.LLVMIsASwitchInst(v.C); return } 773 func (v Value) IsAUnreachableInst() (rv Value) { rv.C = C.LLVMIsAUnreachableInst(v.C); return } 774 func (v Value) IsAUnaryInstruction() (rv Value) { rv.C = C.LLVMIsAUnaryInstruction(v.C); return } 775 func (v Value) IsAAllocaInst() (rv Value) { rv.C = C.LLVMIsAAllocaInst(v.C); return } 776 func (v Value) IsACastInst() (rv Value) { rv.C = C.LLVMIsACastInst(v.C); return } 777 func (v Value) IsABitCastInst() (rv Value) { rv.C = C.LLVMIsABitCastInst(v.C); return } 778 func (v Value) IsAFPExtInst() (rv Value) { rv.C = C.LLVMIsAFPExtInst(v.C); return } 779 func (v Value) IsAFPToSIInst() (rv Value) { rv.C = C.LLVMIsAFPToSIInst(v.C); return } 780 func (v Value) IsAFPToUIInst() (rv Value) { rv.C = C.LLVMIsAFPToUIInst(v.C); return } 781 func (v Value) IsAFPTruncInst() (rv Value) { rv.C = C.LLVMIsAFPTruncInst(v.C); return } 782 func (v Value) IsAIntToPtrInst() (rv Value) { rv.C = C.LLVMIsAIntToPtrInst(v.C); return } 783 func (v Value) IsAPtrToIntInst() (rv Value) { rv.C = C.LLVMIsAPtrToIntInst(v.C); return } 784 func (v Value) IsASExtInst() (rv Value) { rv.C = C.LLVMIsASExtInst(v.C); return } 785 func (v Value) IsASIToFPInst() (rv Value) { rv.C = C.LLVMIsASIToFPInst(v.C); return } 786 func (v Value) IsATruncInst() (rv Value) { rv.C = C.LLVMIsATruncInst(v.C); return } 787 func (v Value) IsAUIToFPInst() (rv Value) { rv.C = C.LLVMIsAUIToFPInst(v.C); return } 788 func (v Value) IsAZExtInst() (rv Value) { rv.C = C.LLVMIsAZExtInst(v.C); return } 789 func (v Value) IsAExtractValueInst() (rv Value) { rv.C = C.LLVMIsAExtractValueInst(v.C); return } 790 func (v Value) IsALoadInst() (rv Value) { rv.C = C.LLVMIsALoadInst(v.C); return } 791 func (v Value) IsAVAArgInst() (rv Value) { rv.C = C.LLVMIsAVAArgInst(v.C); return } 792 793 // Operations on Uses 794 func (v Value) FirstUse() (u Use) { u.C = C.LLVMGetFirstUse(v.C); return } 795 func (u Use) NextUse() (ru Use) { ru.C = C.LLVMGetNextUse(u.C); return } 796 func (u Use) User() (v Value) { v.C = C.LLVMGetUser(u.C); return } 797 func (u Use) UsedValue() (v Value) { v.C = C.LLVMGetUsedValue(u.C); return } 798 799 // Operations on Users 800 func (v Value) Operand(i int) (rv Value) { rv.C = C.LLVMGetOperand(v.C, C.unsigned(i)); return } 801 func (v Value) SetOperand(i int, op Value) { C.LLVMSetOperand(v.C, C.unsigned(i), op.C) } 802 func (v Value) OperandsCount() int { return int(C.LLVMGetNumOperands(v.C)) } 803 804 // Operations on constants of any type 805 func ConstNull(t Type) (v Value) { v.C = C.LLVMConstNull(t.C); return } 806 func ConstAllOnes(t Type) (v Value) { v.C = C.LLVMConstAllOnes(t.C); return } 807 func Undef(t Type) (v Value) { v.C = C.LLVMGetUndef(t.C); return } 808 func (v Value) IsConstant() bool { return C.LLVMIsConstant(v.C) != 0 } 809 func (v Value) IsNull() bool { return C.LLVMIsNull(v.C) != 0 } 810 func (v Value) IsUndef() bool { return C.LLVMIsUndef(v.C) != 0 } 811 func ConstPointerNull(t Type) (v Value) { v.C = C.LLVMConstPointerNull(t.C); return } 812 813 // Operations on metadata 814 func (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 } 820 func (c Context) MDNode(mds []Metadata) (md Metadata) { 821 ptr, nvals := llvmMetadataRefs(mds) 822 md.C = C.LLVMMDNode2(c.C, ptr, nvals) 823 return 824 } 825 func (v Value) ConstantAsMetadata() (md Metadata) { 826 md.C = C.LLVMConstantAsMetadata(v.C) 827 return 828 } 829 830 // Operations on scalar constants 831 func 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 } 837 func 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 } 843 func ConstFloat(t Type, n float64) (v Value) { 844 v.C = C.LLVMConstReal(t.C, C.double(n)) 845 return 846 } 847 func 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 854 func (v Value) ZExtValue() uint64 { return uint64(C.LLVMConstIntGetZExtValue(v.C)) } 855 func (v Value) SExtValue() int64 { return int64(C.LLVMConstIntGetSExtValue(v.C)) } 856 857 // Operations on composite constants 858 func (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 } 865 func (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 } 871 func ConstNamedStruct(t Type, constVals []Value) (v Value) { 872 ptr, nvals := llvmValueRefs(constVals) 873 v.C = C.LLVMConstNamedStruct(t.C, ptr, nvals) 874 return 875 } 876 func 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 } 883 func ConstArray(t Type, constVals []Value) (v Value) { 884 ptr, nvals := llvmValueRefs(constVals) 885 v.C = C.LLVMConstArray(t.C, ptr, nvals) 886 return 887 } 888 func ConstStruct(constVals []Value, packed bool) (v Value) { 889 ptr, nvals := llvmValueRefs(constVals) 890 v.C = C.LLVMConstStruct(ptr, nvals, boolToLLVMBool(packed)) 891 return 892 } 893 func 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 900 func (v Value) Opcode() Opcode { return Opcode(C.LLVMGetConstOpcode(v.C)) } 901 func (v Value) InstructionOpcode() Opcode { return Opcode(C.LLVMGetInstructionOpcode(v.C)) } 902 func AlignOf(t Type) (v Value) { v.C = C.LLVMAlignOf(t.C); return } 903 func SizeOf(t Type) (v Value) { v.C = C.LLVMSizeOf(t.C); return } 904 func ConstNeg(v Value) (rv Value) { rv.C = C.LLVMConstNeg(v.C); return } 905 func ConstNSWNeg(v Value) (rv Value) { rv.C = C.LLVMConstNSWNeg(v.C); return } 906 func ConstNUWNeg(v Value) (rv Value) { rv.C = C.LLVMConstNUWNeg(v.C); return } 907 func ConstFNeg(v Value) (rv Value) { rv.C = C.LLVMConstFNeg(v.C); return } 908 func ConstNot(v Value) (rv Value) { rv.C = C.LLVMConstNot(v.C); return } 909 func ConstAdd(lhs, rhs Value) (v Value) { v.C = C.LLVMConstAdd(lhs.C, rhs.C); return } 910 func ConstNSWAdd(lhs, rhs Value) (v Value) { v.C = C.LLVMConstNSWAdd(lhs.C, rhs.C); return } 911 func ConstNUWAdd(lhs, rhs Value) (v Value) { v.C = C.LLVMConstNUWAdd(lhs.C, rhs.C); return } 912 func ConstFAdd(lhs, rhs Value) (v Value) { v.C = C.LLVMConstFAdd(lhs.C, rhs.C); return } 913 func ConstSub(lhs, rhs Value) (v Value) { v.C = C.LLVMConstSub(lhs.C, rhs.C); return } 914 func ConstNSWSub(lhs, rhs Value) (v Value) { v.C = C.LLVMConstNSWSub(lhs.C, rhs.C); return } 915 func ConstNUWSub(lhs, rhs Value) (v Value) { v.C = C.LLVMConstNUWSub(lhs.C, rhs.C); return } 916 func ConstFSub(lhs, rhs Value) (v Value) { v.C = C.LLVMConstFSub(lhs.C, rhs.C); return } 917 func ConstMul(lhs, rhs Value) (v Value) { v.C = C.LLVMConstMul(lhs.C, rhs.C); return } 918 func ConstNSWMul(lhs, rhs Value) (v Value) { v.C = C.LLVMConstNSWMul(lhs.C, rhs.C); return } 919 func ConstNUWMul(lhs, rhs Value) (v Value) { v.C = C.LLVMConstNUWMul(lhs.C, rhs.C); return } 920 func ConstFMul(lhs, rhs Value) (v Value) { v.C = C.LLVMConstFMul(lhs.C, rhs.C); return } 921 func ConstUDiv(lhs, rhs Value) (v Value) { v.C = C.LLVMConstUDiv(lhs.C, rhs.C); return } 922 func ConstSDiv(lhs, rhs Value) (v Value) { v.C = C.LLVMConstSDiv(lhs.C, rhs.C); return } 923 func ConstExactSDiv(lhs, rhs Value) (v Value) { v.C = C.LLVMConstExactSDiv(lhs.C, rhs.C); return } 924 func ConstFDiv(lhs, rhs Value) (v Value) { v.C = C.LLVMConstFDiv(lhs.C, rhs.C); return } 925 func ConstURem(lhs, rhs Value) (v Value) { v.C = C.LLVMConstURem(lhs.C, rhs.C); return } 926 func ConstSRem(lhs, rhs Value) (v Value) { v.C = C.LLVMConstSRem(lhs.C, rhs.C); return } 927 func ConstFRem(lhs, rhs Value) (v Value) { v.C = C.LLVMConstFRem(lhs.C, rhs.C); return } 928 func ConstAnd(lhs, rhs Value) (v Value) { v.C = C.LLVMConstAnd(lhs.C, rhs.C); return } 929 func ConstOr(lhs, rhs Value) (v Value) { v.C = C.LLVMConstOr(lhs.C, rhs.C); return } 930 func ConstXor(lhs, rhs Value) (v Value) { v.C = C.LLVMConstXor(lhs.C, rhs.C); return } 931 932 func ConstICmp(pred IntPredicate, lhs, rhs Value) (v Value) { 933 v.C = C.LLVMConstICmp(C.LLVMIntPredicate(pred), lhs.C, rhs.C) 934 return 935 } 936 func ConstFCmp(pred FloatPredicate, lhs, rhs Value) (v Value) { 937 v.C = C.LLVMConstFCmp(C.LLVMRealPredicate(pred), lhs.C, rhs.C) 938 return 939 } 940 941 func ConstShl(lhs, rhs Value) (v Value) { v.C = C.LLVMConstShl(lhs.C, rhs.C); return } 942 func ConstLShr(lhs, rhs Value) (v Value) { v.C = C.LLVMConstLShr(lhs.C, rhs.C); return } 943 func ConstAShr(lhs, rhs Value) (v Value) { v.C = C.LLVMConstAShr(lhs.C, rhs.C); return } 944 945 func ConstGEP(v Value, indices []Value) (rv Value) { 946 ptr, nvals := llvmValueRefs(indices) 947 rv.C = C.LLVMConstGEP(v.C, ptr, nvals) 948 return 949 } 950 func ConstInBoundsGEP(v Value, indices []Value) (rv Value) { 951 ptr, nvals := llvmValueRefs(indices) 952 rv.C = C.LLVMConstInBoundsGEP(v.C, ptr, nvals) 953 return 954 } 955 func ConstTrunc(v Value, t Type) (rv Value) { rv.C = C.LLVMConstTrunc(v.C, t.C); return } 956 func ConstSExt(v Value, t Type) (rv Value) { rv.C = C.LLVMConstSExt(v.C, t.C); return } 957 func ConstZExt(v Value, t Type) (rv Value) { rv.C = C.LLVMConstZExt(v.C, t.C); return } 958 func ConstFPTrunc(v Value, t Type) (rv Value) { rv.C = C.LLVMConstFPTrunc(v.C, t.C); return } 959 func ConstFPExt(v Value, t Type) (rv Value) { rv.C = C.LLVMConstFPExt(v.C, t.C); return } 960 func ConstUIToFP(v Value, t Type) (rv Value) { rv.C = C.LLVMConstUIToFP(v.C, t.C); return } 961 func ConstSIToFP(v Value, t Type) (rv Value) { rv.C = C.LLVMConstSIToFP(v.C, t.C); return } 962 func ConstFPToUI(v Value, t Type) (rv Value) { rv.C = C.LLVMConstFPToUI(v.C, t.C); return } 963 func ConstFPToSI(v Value, t Type) (rv Value) { rv.C = C.LLVMConstFPToSI(v.C, t.C); return } 964 func ConstPtrToInt(v Value, t Type) (rv Value) { rv.C = C.LLVMConstPtrToInt(v.C, t.C); return } 965 func ConstIntToPtr(v Value, t Type) (rv Value) { rv.C = C.LLVMConstIntToPtr(v.C, t.C); return } 966 func ConstBitCast(v Value, t Type) (rv Value) { rv.C = C.LLVMConstBitCast(v.C, t.C); return } 967 func ConstZExtOrBitCast(v Value, t Type) (rv Value) { rv.C = C.LLVMConstZExtOrBitCast(v.C, t.C); return } 968 func ConstSExtOrBitCast(v Value, t Type) (rv Value) { rv.C = C.LLVMConstSExtOrBitCast(v.C, t.C); return } 969 func ConstTruncOrBitCast(v Value, t Type) (rv Value) { 970 rv.C = C.LLVMConstTruncOrBitCast(v.C, t.C) 971 return 972 } 973 func ConstPointerCast(v Value, t Type) (rv Value) { rv.C = C.LLVMConstPointerCast(v.C, t.C); return } 974 func ConstIntCast(v Value, t Type, signed bool) (rv Value) { 975 rv.C = C.LLVMConstIntCast(v.C, t.C, boolToLLVMBool(signed)) 976 return 977 } 978 func ConstFPCast(v Value, t Type) (rv Value) { rv.C = C.LLVMConstFPCast(v.C, t.C); return } 979 func ConstSelect(cond, iftrue, iffalse Value) (rv Value) { 980 rv.C = C.LLVMConstSelect(cond.C, iftrue.C, iffalse.C) 981 return 982 } 983 func ConstExtractElement(vec, i Value) (rv Value) { 984 rv.C = C.LLVMConstExtractElement(vec.C, i.C) 985 return 986 } 987 func ConstInsertElement(vec, elem, i Value) (rv Value) { 988 rv.C = C.LLVMConstInsertElement(vec.C, elem.C, i.C) 989 return 990 } 991 func 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 1000 func 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 1010 func 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 1020 func 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) 1026 func (v Value) GlobalParent() (m Module) { m.C = C.LLVMGetGlobalParent(v.C); return } 1027 func (v Value) IsDeclaration() bool { return C.LLVMIsDeclaration(v.C) != 0 } 1028 func (v Value) Linkage() Linkage { return Linkage(C.LLVMGetLinkage(v.C)) } 1029 func (v Value) SetLinkage(l Linkage) { C.LLVMSetLinkage(v.C, C.LLVMLinkage(l)) } 1030 func (v Value) Section() string { return C.GoString(C.LLVMGetSection(v.C)) } 1031 func (v Value) SetSection(str string) { 1032 cstr := C.CString(str) 1033 defer C.free(unsafe.Pointer(cstr)) 1034 C.LLVMSetSection(v.C, cstr) 1035 } 1036 func (v Value) Visibility() Visibility { return Visibility(C.LLVMGetVisibility(v.C)) } 1037 func (v Value) SetVisibility(vi Visibility) { C.LLVMSetVisibility(v.C, C.LLVMVisibility(vi)) } 1038 func (v Value) Alignment() int { return int(C.LLVMGetAlignment(v.C)) } 1039 func (v Value) SetAlignment(a int) { C.LLVMSetAlignment(v.C, C.unsigned(a)) } 1040 func (v Value) SetUnnamedAddr(ua bool) { C.LLVMSetUnnamedAddr(v.C, boolToLLVMBool(ua)) } 1041 1042 // Operations on global variables 1043 func 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 } 1049 func 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 } 1055 func (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 1062 func (m Module) FirstGlobal() (v Value) { v.C = C.LLVMGetFirstGlobal(m.C); return } 1063 func (m Module) LastGlobal() (v Value) { v.C = C.LLVMGetLastGlobal(m.C); return } 1064 func NextGlobal(v Value) (rv Value) { rv.C = C.LLVMGetNextGlobal(v.C); return } 1065 func PrevGlobal(v Value) (rv Value) { rv.C = C.LLVMGetPreviousGlobal(v.C); return } 1066 func (v Value) EraseFromParentAsGlobal() { C.LLVMDeleteGlobal(v.C) } 1067 func (v Value) Initializer() (rv Value) { rv.C = C.LLVMGetInitializer(v.C); return } 1068 func (v Value) SetInitializer(cv Value) { C.LLVMSetInitializer(v.C, cv.C) } 1069 func (v Value) IsThreadLocal() bool { return C.LLVMIsThreadLocal(v.C) != 0 } 1070 func (v Value) SetThreadLocal(tl bool) { C.LLVMSetThreadLocal(v.C, boolToLLVMBool(tl)) } 1071 func (v Value) IsGlobalConstant() bool { return C.LLVMIsGlobalConstant(v.C) != 0 } 1072 func (v Value) SetGlobalConstant(gc bool) { C.LLVMSetGlobalConstant(v.C, boolToLLVMBool(gc)) } 1073 func (v Value) IsVolatile() bool { return C.LLVMGetVolatile(v.C) != 0 } 1074 func (v Value) SetVolatile(volatile bool) { C.LLVMSetVolatile(v.C, boolToLLVMBool(volatile)) } 1075 func (v Value) Ordering() AtomicOrdering { return AtomicOrdering(C.LLVMGetOrdering(v.C)) } 1076 func (v Value) SetOrdering(ordering AtomicOrdering) { 1077 C.LLVMSetOrdering(v.C, C.LLVMAtomicOrdering(ordering)) 1078 } 1079 func (v Value) IsAtomicSingleThread() bool { return C.LLVMIsAtomicSingleThread(v.C) != 0 } 1080 func (v Value) SetAtomicSingleThread(singleThread bool) { 1081 C.LLVMSetAtomicSingleThread(v.C, boolToLLVMBool(singleThread)) 1082 } 1083 func (v Value) CmpXchgSuccessOrdering() AtomicOrdering { 1084 return AtomicOrdering(C.LLVMGetCmpXchgSuccessOrdering(v.C)) 1085 } 1086 func (v Value) SetCmpXchgSuccessOrdering(ordering AtomicOrdering) { 1087 C.LLVMSetCmpXchgSuccessOrdering(v.C, C.LLVMAtomicOrdering(ordering)) 1088 } 1089 func (v Value) CmpXchgFailureOrdering() AtomicOrdering { 1090 return AtomicOrdering(C.LLVMGetCmpXchgFailureOrdering(v.C)) 1091 } 1092 func (v Value) SetCmpXchgFailureOrdering(ordering AtomicOrdering) { 1093 C.LLVMSetCmpXchgFailureOrdering(v.C, C.LLVMAtomicOrdering(ordering)) 1094 } 1095 1096 // Operations on aliases 1097 func 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 1105 func (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 1112 func (v Value) Comdat() (c Comdat) { c.C = C.LLVMGetComdat(v.C); return } 1113 func (v Value) SetComdat(c Comdat) { C.LLVMSetComdat(v.C, c.C) } 1114 1115 func (c Comdat) SelectionKind() ComdatSelectionKind { 1116 return ComdatSelectionKind(C.LLVMGetComdatSelectionKind(c.C)) 1117 } 1118 1119 func (c Comdat) SetSelectionKind(k ComdatSelectionKind) { 1120 C.LLVMSetComdatSelectionKind(c.C, (C.LLVMComdatSelectionKind)(k)) 1121 } 1122 1123 // Operations on functions 1124 func 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 1131 func (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 1138 func (m Module) FirstFunction() (v Value) { v.C = C.LLVMGetFirstFunction(m.C); return } 1139 func (m Module) LastFunction() (v Value) { v.C = C.LLVMGetLastFunction(m.C); return } 1140 func NextFunction(v Value) (rv Value) { rv.C = C.LLVMGetNextFunction(v.C); return } 1141 func PrevFunction(v Value) (rv Value) { rv.C = C.LLVMGetPreviousFunction(v.C); return } 1142 func (v Value) EraseFromParentAsFunction() { C.LLVMDeleteFunction(v.C) } 1143 func (v Value) IntrinsicID() int { return int(C.LLVMGetIntrinsicID(v.C)) } 1144 func (v Value) FunctionCallConv() CallConv { 1145 return CallConv(C.LLVMCallConv(C.LLVMGetFunctionCallConv(v.C))) 1146 } 1147 func (v Value) SetFunctionCallConv(cc CallConv) { C.LLVMSetFunctionCallConv(v.C, C.unsigned(cc)) } 1148 func (v Value) GC() string { return C.GoString(C.LLVMGetGC(v.C)) } 1149 func (v Value) SetGC(name string) { 1150 cname := C.CString(name) 1151 defer C.free(unsafe.Pointer(cname)) 1152 C.LLVMSetGC(v.C, cname) 1153 } 1154 func (v Value) AddAttributeAtIndex(i int, a Attribute) { 1155 C.LLVMAddAttributeAtIndex(v.C, C.LLVMAttributeIndex(i), a.C) 1156 } 1157 func (v Value) AddFunctionAttr(a Attribute) { 1158 v.AddAttributeAtIndex(C.LLVMAttributeFunctionIndex, a) 1159 } 1160 func (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 } 1164 func (v Value) GetEnumFunctionAttribute(kind uint) Attribute { 1165 return v.GetEnumAttributeAtIndex(C.LLVMAttributeFunctionIndex, kind) 1166 } 1167 func (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 } 1174 func (v Value) RemoveEnumAttributeAtIndex(i int, kind uint) { 1175 C.LLVMRemoveEnumAttributeAtIndex(v.C, C.LLVMAttributeIndex(i), C.unsigned(kind)) 1176 } 1177 func (v Value) RemoveEnumFunctionAttribute(kind uint) { 1178 v.RemoveEnumAttributeAtIndex(C.LLVMAttributeFunctionIndex, kind) 1179 } 1180 func (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 } 1186 func (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 } 1193 func (v Value) SetPersonality(p Value) { 1194 C.LLVMSetPersonalityFn(v.C, p.C) 1195 } 1196 1197 // Operations on parameters 1198 func (v Value) ParamsCount() int { return int(C.LLVMCountParams(v.C)) } 1199 func (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 } 1206 func (v Value) Param(i int) (rv Value) { rv.C = C.LLVMGetParam(v.C, C.unsigned(i)); return } 1207 func (v Value) ParamParent() (rv Value) { rv.C = C.LLVMGetParamParent(v.C); return } 1208 func (v Value) FirstParam() (rv Value) { rv.C = C.LLVMGetFirstParam(v.C); return } 1209 func (v Value) LastParam() (rv Value) { rv.C = C.LLVMGetLastParam(v.C); return } 1210 func NextParam(v Value) (rv Value) { rv.C = C.LLVMGetNextParam(v.C); return } 1211 func PrevParam(v Value) (rv Value) { rv.C = C.LLVMGetPreviousParam(v.C); return } 1212 func (v Value) SetParamAlignment(align int) { C.LLVMSetParamAlignment(v.C, C.unsigned(align)) } 1213 1214 // Operations on basic blocks 1215 func (bb BasicBlock) AsValue() (v Value) { v.C = C.LLVMBasicBlockAsValue(bb.C); return } 1216 func (v Value) IsBasicBlock() bool { return C.LLVMValueIsBasicBlock(v.C) != 0 } 1217 func (v Value) AsBasicBlock() (bb BasicBlock) { bb.C = C.LLVMValueAsBasicBlock(v.C); return } 1218 func (bb BasicBlock) Parent() (v Value) { v.C = C.LLVMGetBasicBlockParent(bb.C); return } 1219 func (v Value) BasicBlocksCount() int { return int(C.LLVMCountBasicBlocks(v.C)) } 1220 func (v Value) BasicBlocks() []BasicBlock { 1221 out := make([]BasicBlock, v.BasicBlocksCount()) 1222 C.LLVMGetBasicBlocks(v.C, llvmBasicBlockRefPtr(&out[0])) 1223 return out 1224 } 1225 func (v Value) FirstBasicBlock() (bb BasicBlock) { bb.C = C.LLVMGetFirstBasicBlock(v.C); return } 1226 func (v Value) LastBasicBlock() (bb BasicBlock) { bb.C = C.LLVMGetLastBasicBlock(v.C); return } 1227 func NextBasicBlock(bb BasicBlock) (rbb BasicBlock) { rbb.C = C.LLVMGetNextBasicBlock(bb.C); return } 1228 func PrevBasicBlock(bb BasicBlock) (rbb BasicBlock) { rbb.C = C.LLVMGetPreviousBasicBlock(bb.C); return } 1229 func (v Value) EntryBasicBlock() (bb BasicBlock) { bb.C = C.LLVMGetEntryBasicBlock(v.C); return } 1230 func (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 } 1236 func (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 } 1242 func 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 } 1248 func 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 } 1254 func (bb BasicBlock) EraseFromParent() { C.LLVMDeleteBasicBlock(bb.C) } 1255 func (bb BasicBlock) MoveBefore(pos BasicBlock) { C.LLVMMoveBasicBlockBefore(bb.C, pos.C) } 1256 func (bb BasicBlock) MoveAfter(pos BasicBlock) { C.LLVMMoveBasicBlockAfter(bb.C, pos.C) } 1257 1258 // Operations on instructions 1259 func (v Value) EraseFromParentAsInstruction() { C.LLVMInstructionEraseFromParent(v.C) } 1260 func (v Value) RemoveFromParentAsInstruction() { C.LLVMInstructionRemoveFromParent(v.C) } 1261 func (v Value) InstructionParent() (bb BasicBlock) { bb.C = C.LLVMGetInstructionParent(v.C); return } 1262 func (v Value) InstructionDebugLoc() (md Metadata) { md.C = C.LLVMInstructionGetDebugLoc(v.C); return } 1263 func (v Value) InstructionSetDebugLoc(md Metadata) { C.LLVMInstructionSetDebugLoc(v.C, md.C) } 1264 func (bb BasicBlock) FirstInstruction() (v Value) { v.C = C.LLVMGetFirstInstruction(bb.C); return } 1265 func (bb BasicBlock) LastInstruction() (v Value) { v.C = C.LLVMGetLastInstruction(bb.C); return } 1266 func NextInstruction(v Value) (rv Value) { rv.C = C.LLVMGetNextInstruction(v.C); return } 1267 func PrevInstruction(v Value) (rv Value) { rv.C = C.LLVMGetPreviousInstruction(v.C); return } 1268 1269 // Operations on call sites 1270 func (v Value) SetInstructionCallConv(cc CallConv) { 1271 C.LLVMSetInstructionCallConv(v.C, C.unsigned(cc)) 1272 } 1273 func (v Value) InstructionCallConv() CallConv { 1274 return CallConv(C.LLVMCallConv(C.LLVMGetInstructionCallConv(v.C))) 1275 } 1276 func (v Value) AddCallSiteAttribute(i int, a Attribute) { 1277 C.LLVMAddCallSiteAttribute(v.C, C.LLVMAttributeIndex(i), a.C) 1278 } 1279 func (v Value) SetInstrParamAlignment(i int, align int) { 1280 C.LLVMSetInstrParamAlignment(v.C, C.unsigned(i), C.unsigned(align)) 1281 } 1282 func (v Value) CalledValue() (rv Value) { 1283 rv.C = C.LLVMGetCalledValue(v.C) 1284 return 1285 } 1286 1287 // Operations on call instructions (only) 1288 func (v Value) IsTailCall() bool { return C.LLVMIsTailCall(v.C) != 0 } 1289 func (v Value) SetTailCall(is bool) { C.LLVMSetTailCall(v.C, boolToLLVMBool(is)) } 1290 1291 // Operations on phi nodes 1292 func (v Value) AddIncoming(vals []Value, blocks []BasicBlock) { 1293 ptr, nvals := llvmValueRefs(vals) 1294 C.LLVMAddIncoming(v.C, ptr, llvmBasicBlockRefPtr(&blocks[0]), nvals) 1295 } 1296 func (v Value) IncomingCount() int { return int(C.LLVMCountIncoming(v.C)) } 1297 func (v Value) IncomingValue(i int) (rv Value) { 1298 rv.C = C.LLVMGetIncomingValue(v.C, C.unsigned(i)) 1299 return 1300 } 1301 func (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 1307 func 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 1317 func (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 1330 func (v Value) IntPredicate() IntPredicate { return IntPredicate(C.LLVMGetICmpPredicate(v.C)) } 1331 func (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 1340 func (c Context) NewBuilder() (b Builder) { b.C = C.LLVMCreateBuilderInContext(c.C); return } 1341 func NewBuilder() (b Builder) { b.C = C.LLVMCreateBuilder(); return } 1342 func (b Builder) SetInsertPoint(block BasicBlock, instr Value) { 1343 C.LLVMPositionBuilder(b.C, block.C, instr.C) 1344 } 1345 func (b Builder) SetInsertPointBefore(instr Value) { C.LLVMPositionBuilderBefore(b.C, instr.C) } 1346 func (b Builder) SetInsertPointAtEnd(block BasicBlock) { C.LLVMPositionBuilderAtEnd(b.C, block.C) } 1347 func (b Builder) GetInsertBlock() (bb BasicBlock) { bb.C = C.LLVMGetInsertBlock(b.C); return } 1348 func (b Builder) ClearInsertionPoint() { C.LLVMClearInsertionPosition(b.C) } 1349 func (b Builder) Insert(instr Value) { C.LLVMInsertIntoBuilder(b.C, instr.C) } 1350 func (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 } 1355 func (b Builder) Dispose() { C.LLVMDisposeBuilder(b.C) } 1356 1357 // Metadata 1358 type DebugLoc struct { 1359 Line, Col uint 1360 Scope Metadata 1361 InlinedAt Metadata 1362 } 1363 1364 func (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() 1369 func (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 } 1377 func (b Builder) SetInstDebugLocation(v Value) { C.LLVMSetInstDebugLocation(b.C, v.C) } 1378 func (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 1388 func (b Builder) CreateRetVoid() (rv Value) { rv.C = C.LLVMBuildRetVoid(b.C); return } 1389 func (b Builder) CreateRet(v Value) (rv Value) { rv.C = C.LLVMBuildRet(b.C, v.C); return } 1390 func (b Builder) CreateAggregateRet(vs []Value) (rv Value) { 1391 ptr, nvals := llvmValueRefs(vs) 1392 rv.C = C.LLVMBuildAggregateRet(b.C, ptr, nvals) 1393 return 1394 } 1395 func (b Builder) CreateBr(bb BasicBlock) (rv Value) { rv.C = C.LLVMBuildBr(b.C, bb.C); return } 1396 func (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 } 1400 func (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 } 1404 func (b Builder) CreateIndirectBr(addr Value, numDests int) (rv Value) { 1405 rv.C = C.LLVMBuildIndirectBr(b.C, addr.C, C.unsigned(numDests)) 1406 return 1407 } 1408 func (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 } 1415 func (b Builder) CreateUnreachable() (rv Value) { rv.C = C.LLVMBuildUnreachable(b.C); return } 1416 1417 // Add a case to the switch instruction 1418 func (v Value) AddCase(on Value, dest BasicBlock) { C.LLVMAddCase(v.C, on.C, dest.C) } 1419 1420 // Add a destination to the indirectbr instruction 1421 func (v Value) AddDest(dest BasicBlock) { C.LLVMAddDestination(v.C, dest.C) } 1422 1423 // Arithmetic 1424 func (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 } 1430 func (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 } 1436 func (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 } 1442 func (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 } 1448 func (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 } 1454 func (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 } 1460 func (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 } 1466 func (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 } 1472 func (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 } 1478 func (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 } 1484 func (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 } 1490 func (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 } 1496 func (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 } 1502 func (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 } 1508 func (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 } 1514 func (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 } 1520 func (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 } 1526 func (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 } 1532 func (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 } 1538 func (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 } 1544 func (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 } 1550 func (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 } 1556 func (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 } 1562 func (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 } 1568 func (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 } 1574 func (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 } 1580 func (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 } 1586 func (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 } 1592 func (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 } 1598 func (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 } 1604 func (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 1613 func (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 } 1619 func (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 } 1625 func (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 } 1631 func (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 } 1637 func (b Builder) CreateFree(p Value) (v Value) { 1638 v.C = C.LLVMBuildFree(b.C, p.C) 1639 return 1640 } 1641 func (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 } 1647 func (b Builder) CreateStore(val Value, p Value) (v Value) { 1648 v.C = C.LLVMBuildStore(b.C, val.C, p.C) 1649 return 1650 } 1651 func (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 } 1658 func (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 } 1665 func (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 } 1671 func (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 } 1679 func (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 } 1687 func (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 } 1691 func (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 1697 func (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 } 1703 func (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 } 1709 func (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 } 1715 func (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 } 1721 func (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 } 1727 func (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 } 1733 func (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 } 1739 func (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 } 1745 func (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 } 1751 func (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 } 1757 func (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 } 1763 func (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 } 1769 func (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 } 1775 func (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 } 1781 func (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 } 1787 func (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 } // 1793 func (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 } 1799 func (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 } 1805 func (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 1813 func (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 } 1819 func (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 1827 func (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 } 1833 func (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 1841 func (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 1848 func (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 } 1854 func (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 } 1860 func (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 } 1866 func (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 } 1872 func (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 } 1878 func (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 1885 func (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 } 1891 func (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 } 1897 func (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 1904 func (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 1911 func (l Value) AddClause(v Value) { 1912 C.LLVMAddClause(l.C, v.C) 1913 } 1914 1915 func (l Value) SetCleanup(cleanup bool) { 1916 C.LLVMSetCleanup(l.C, boolToLLVMBool(cleanup)) 1917 } 1918 1919 func (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. 1930 func NewModuleProviderForModule(m Module) (mp ModuleProvider) { 1931 mp.C = C.LLVMCreateModuleProviderForExistingModule(m.C) 1932 return 1933 } 1934 1935 // Destroys the module M. 1936 func (mp ModuleProvider) Dispose() { C.LLVMDisposeModuleProvider(mp.C) } 1937 1938 //------------------------------------------------------------------------- 1939 // llvm.MemoryBuffer 1940 //------------------------------------------------------------------------- 1941 1942 func 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 1955 func 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 1966 func (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 1972 func (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. 1981 func 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. 1987 func 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&). 1995 func (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. 2000 func (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&). 2006 func (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. 2011 func (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. 2016 func (pm PassManager) Dispose() { C.LLVMDisposePassManager(pm.C) } 2017