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