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