spice_packages/llvm/llvm.spice
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | #![core.compiler.warnings.ignore] | ||
| 2 | |||
| 3 | // Std imports | ||
| 4 | import "std/data/vector"; | ||
| 5 | import "std/text/string-ext"; | ||
| 6 | |||
| 7 | // Own imports | ||
| 8 | import "src/linker-flags"; | ||
| 9 | |||
| 10 | // External functions | ||
| 11 | ext f<int> snprintf(char*, unsigned long, string, ...); | ||
| 12 | |||
| 13 | // ===== External type definitions ===== | ||
| 14 | type VoidPtr alias byte*; | ||
| 15 | type LLVMBool alias int; | ||
| 16 | type LLVMContextRef alias VoidPtr; | ||
| 17 | type LLVMModuleRef alias VoidPtr; | ||
| 18 | type LLVMBuilderRef alias VoidPtr; | ||
| 19 | type LLVMTypeRef alias VoidPtr; | ||
| 20 | type LLVMValueRef alias VoidPtr; | ||
| 21 | type LLVMFunctionRef alias VoidPtr; | ||
| 22 | type LLVMBasicBlockRef alias VoidPtr; | ||
| 23 | type LLVMPassBuilderOptionsRef alias VoidPtr; | ||
| 24 | type LLVMTargetRef alias VoidPtr; | ||
| 25 | type LLVMTargetMachineRef alias VoidPtr; | ||
| 26 | type LLVMTargetDataRef alias VoidPtr; | ||
| 27 | type LLVMErrorRef alias VoidPtr; | ||
| 28 | type LLVMAttributeRef alias VoidPtr; | ||
| 29 | type LLVMMetadataRef alias VoidPtr; | ||
| 30 | type LLVMComdatRef alias VoidPtr; | ||
| 31 | type LLVMMemoryBufferRef alias VoidPtr; | ||
| 32 | type LLVMDIBuilderRef alias VoidPtr; | ||
| 33 | type LLVMDbgRecordRef alias VoidPtr; | ||
| 34 | type LLVMDiagnosticInfoRef alias VoidPtr; | ||
| 35 | |||
| 36 | // ===== Generic helper definitions ===== | ||
| 37 | type ShortIntLong unsigned short|unsigned int|unsigned long; | ||
| 38 | |||
| 39 | // ===== Enums ===== | ||
| 40 | public type LLVMOpcode enum { | ||
| 41 | // Terminator Instructions | ||
| 42 | Return = 1, | ||
| 43 | Br = 2, | ||
| 44 | Switch = 3, | ||
| 45 | IndirectBr = 4, | ||
| 46 | Invoke = 5, | ||
| 47 | // removed 6 due to API changes | ||
| 48 | Unreachable = 7, | ||
| 49 | CallBr = 67, | ||
| 50 | |||
| 51 | // Standard Unary Operators | ||
| 52 | FNeg = 66, | ||
| 53 | |||
| 54 | // Standard Binary Operators | ||
| 55 | Add = 8, | ||
| 56 | FAdd = 9, | ||
| 57 | Sub = 10, | ||
| 58 | FSub = 11, | ||
| 59 | Mul = 12, | ||
| 60 | FMul = 13, | ||
| 61 | UDiv = 14, | ||
| 62 | SDiv = 15, | ||
| 63 | FDiv = 16, | ||
| 64 | URem = 17, | ||
| 65 | SRem = 18, | ||
| 66 | FRem = 19, | ||
| 67 | |||
| 68 | // Logical Operators | ||
| 69 | Shl = 20, | ||
| 70 | LShr = 21, | ||
| 71 | AShr = 22, | ||
| 72 | And = 23, | ||
| 73 | Or = 24, | ||
| 74 | Xor = 25, | ||
| 75 | |||
| 76 | // Memory Operators | ||
| 77 | Alloca = 26, | ||
| 78 | Load = 27, | ||
| 79 | Store = 28, | ||
| 80 | GetElementPtr = 29, | ||
| 81 | |||
| 82 | // Cast Operators | ||
| 83 | Trunc = 30, | ||
| 84 | ZExt = 31, | ||
| 85 | SExt = 32, | ||
| 86 | FPToUI = 33, | ||
| 87 | FPToSI = 34, | ||
| 88 | UIToFP = 35, | ||
| 89 | SIToFP = 36, | ||
| 90 | FPTrunc = 37, | ||
| 91 | FPExt = 38, | ||
| 92 | PtrToInt = 39, | ||
| 93 | IntToPtr = 40, | ||
| 94 | BitCast = 41, | ||
| 95 | AddrSpaceCast = 60, | ||
| 96 | |||
| 97 | // Other Operators | ||
| 98 | ICmp = 42, | ||
| 99 | FCmp = 43, | ||
| 100 | PHI = 44, | ||
| 101 | Call = 45, | ||
| 102 | Select = 46, | ||
| 103 | UserOp1 = 47, | ||
| 104 | UserOp2 = 48, | ||
| 105 | VAArg = 49, | ||
| 106 | ExtractElement = 50, | ||
| 107 | InsertElement = 51, | ||
| 108 | ShuffleVector = 52, | ||
| 109 | ExtractValue = 53, | ||
| 110 | InsertValue = 54, | ||
| 111 | Freeze = 68, | ||
| 112 | |||
| 113 | // Atomic operators | ||
| 114 | Fence = 55, | ||
| 115 | AtomicCmpXchg = 56, | ||
| 116 | AtomicRMW = 57, | ||
| 117 | |||
| 118 | // Exception Handling Operators | ||
| 119 | Resume = 58, | ||
| 120 | LandingPad = 59, | ||
| 121 | CleanupRet = 61, | ||
| 122 | CatchRet = 62, | ||
| 123 | CatchPad = 63, | ||
| 124 | CleanupPad = 64, | ||
| 125 | CatchSwitch = 65 | ||
| 126 | } | ||
| 127 | |||
| 128 | public type LLVMTypeKind enum { | ||
| 129 | VoidTypeKind, | ||
| 130 | HalfTypeKind, | ||
| 131 | FloatTypeKind, | ||
| 132 | DoubleTypeKind, | ||
| 133 | X86_FP80TypeKind, | ||
| 134 | FP128TypeKind, | ||
| 135 | PPC_FP128TypeKind, | ||
| 136 | LabelTypeKind, | ||
| 137 | IntegerTypeKind, | ||
| 138 | FunctionTypeKind, | ||
| 139 | StructTypeKind, | ||
| 140 | ArrayTypeKind, | ||
| 141 | PointerTypeKind, | ||
| 142 | VectorTypeKind, | ||
| 143 | MetadataTypeKind, | ||
| 144 | X86_MMXTypeKind, | ||
| 145 | TokenTypeKind, | ||
| 146 | ScalableVectorTypeKind, | ||
| 147 | BFloatTypeKind, | ||
| 148 | X86AMXTypeKind, | ||
| 149 | TargetExtTypeKind | ||
| 150 | } | ||
| 151 | |||
| 152 | public type LLVMLinkage enum { | ||
| 153 | ExternalLinkage, | ||
| 154 | AvailableExternallyLinkage, | ||
| 155 | LinkOnceAnyLinkage, | ||
| 156 | LinkOnceODRLinkage, | ||
| 157 | LinkOnceODRAutoHideLinkage, | ||
| 158 | WeakAnyLinkage, | ||
| 159 | WeakODRLinkage, | ||
| 160 | AppendingLinkage, | ||
| 161 | InternalLinkage, | ||
| 162 | PrivateLinkage, | ||
| 163 | DLLImportLinkage, | ||
| 164 | DLLExportLinkage, | ||
| 165 | ExternalWeakLinkage, | ||
| 166 | GhostLinkage, | ||
| 167 | CommonLinkage, | ||
| 168 | LinkerPrivateLinkage, | ||
| 169 | LinkerPrivateWeakLinkage | ||
| 170 | } | ||
| 171 | |||
| 172 | public type LLVMVisibility enum { | ||
| 173 | DefaultVisibility, | ||
| 174 | HiddenVisibility, | ||
| 175 | ProtectedVisibility | ||
| 176 | } | ||
| 177 | |||
| 178 | public type LLVMUnnamedAddr enum { | ||
| 179 | NoUnnamedAddr, | ||
| 180 | LocalUnnamedAddr, | ||
| 181 | GlobalUnnamedAddr | ||
| 182 | } | ||
| 183 | |||
| 184 | public type LLVMDLLStorageClass enum { | ||
| 185 | Default = 0, | ||
| 186 | DLLImport = 1, | ||
| 187 | DLLExport = 2 | ||
| 188 | } | ||
| 189 | |||
| 190 | public type LLVMCallConv enum { | ||
| 191 | CCallConv = 0, | ||
| 192 | FastCallConv = 8, | ||
| 193 | ColdCallConv = 9, | ||
| 194 | GHCALLCallConv = 10, | ||
| 195 | HiPECallConv = 11, | ||
| 196 | AnyRegCallConv = 13, | ||
| 197 | PreserveMostCallConv = 14, | ||
| 198 | PreserveAllCallConv = 15, | ||
| 199 | SwiftCallConv = 16, | ||
| 200 | CXXFASTTLSCallConv = 17, | ||
| 201 | X86StdcallCallConv = 64, | ||
| 202 | X86FastcallCallConv = 65, | ||
| 203 | ARMAPCSCallConv = 66, | ||
| 204 | ARMAAPCSCallConv = 67, | ||
| 205 | ARMAAPCSVFPCallConv = 68, | ||
| 206 | MSP430INTRCallConv = 69, | ||
| 207 | X86ThisCallCallConv = 70, | ||
| 208 | PTXKernelCallConv = 71, | ||
| 209 | PTXDeviceCallConv = 72, | ||
| 210 | SPIRFUNCCallConv = 75, | ||
| 211 | SPIRKERNELCallConv = 76, | ||
| 212 | IntelOCLBICallConv = 77, | ||
| 213 | X8664SysVCallConv = 78, | ||
| 214 | Win64CallConv = 79, | ||
| 215 | X86VectorCallCallConv = 80, | ||
| 216 | HHVMCallConv = 81, | ||
| 217 | HHVMCCallConv = 82, | ||
| 218 | X86INTRCallConv = 83, | ||
| 219 | AVRINTRCallConv = 84, | ||
| 220 | AVRSIGNALCallConv = 85, | ||
| 221 | AVRBUILTINCallConv = 86, | ||
| 222 | AMDGPUVSCallConv = 87, | ||
| 223 | AMDGPUGSCallConv = 88, | ||
| 224 | AMDGPUPSCallConv = 89, | ||
| 225 | AMDGPUCSCallConv = 90, | ||
| 226 | AMDGPUKERNELCallConv = 91, | ||
| 227 | X86RegCallCallConv = 92, | ||
| 228 | AMDGPUHSCallConv = 93, | ||
| 229 | MSP430BUILTINCallConv = 94, | ||
| 230 | AMDGPULSCallConv = 95, | ||
| 231 | AMDGPUESCallConv = 96 | ||
| 232 | } | ||
| 233 | |||
| 234 | public type LLVMValueKind enum { | ||
| 235 | ArgumentValue, | ||
| 236 | BasicBlockValue, | ||
| 237 | MemoryUseValue, | ||
| 238 | MemoryDefValue, | ||
| 239 | MemoryPhiValue, | ||
| 240 | |||
| 241 | FunctionValue, | ||
| 242 | GlobalAliasValue, | ||
| 243 | GlobalIFuncValue, | ||
| 244 | GlobalVariableValue, | ||
| 245 | BlockAddressValue, | ||
| 246 | ConstantExprValue, | ||
| 247 | ConstantArrayValue, | ||
| 248 | ConstantStructValue, | ||
| 249 | ConstantVectorValue, | ||
| 250 | |||
| 251 | UndefValueValue, | ||
| 252 | ConstantAggregateZeroValue, | ||
| 253 | ConstantDataArrayValue, | ||
| 254 | ConstantDataVectorValue, | ||
| 255 | ConstantIntValue, | ||
| 256 | ConstantFPValue, | ||
| 257 | ConstantPointerNullValue, | ||
| 258 | ConstantTokenNoneValue, | ||
| 259 | |||
| 260 | MetadataAsValueValue, | ||
| 261 | InlineAsmValue, | ||
| 262 | |||
| 263 | InstructionValue, | ||
| 264 | PoisonValue, | ||
| 265 | ConstantTargetNoneValue | ||
| 266 | } | ||
| 267 | |||
| 268 | public type LLVMIntPredicate enum { | ||
| 269 | EQ = 32, | ||
| 270 | NE = 33, | ||
| 271 | UGT = 34, | ||
| 272 | UGE = 35, | ||
| 273 | ULT = 36, | ||
| 274 | ULE = 37, | ||
| 275 | SGT = 38, | ||
| 276 | SGE = 39, | ||
| 277 | SLT = 40, | ||
| 278 | SLE = 41 | ||
| 279 | } | ||
| 280 | |||
| 281 | public type LLVMRealPredicate enum { | ||
| 282 | PredicateFalse, | ||
| 283 | OEQ, | ||
| 284 | OGT, | ||
| 285 | OGE, | ||
| 286 | OLT, | ||
| 287 | OLE, | ||
| 288 | ONE, | ||
| 289 | ORD, | ||
| 290 | UNO, | ||
| 291 | UEQ, | ||
| 292 | UGT, | ||
| 293 | UGE, | ||
| 294 | ULT, | ||
| 295 | ULE, | ||
| 296 | UNE, | ||
| 297 | PredicateTrue | ||
| 298 | } | ||
| 299 | |||
| 300 | public type LLVMLandingPadClauseTy enum { | ||
| 301 | Catch, | ||
| 302 | Filter | ||
| 303 | } | ||
| 304 | |||
| 305 | public type LLVMThreadLocalMode enum { | ||
| 306 | NotThreadLocal = 0, | ||
| 307 | GeneralDynamicTLSModel = 1, | ||
| 308 | LocalDynamicTLSModel = 2, | ||
| 309 | InitialExecTLSModel = 3, | ||
| 310 | LocalExecTLSModel = 4 | ||
| 311 | } | ||
| 312 | |||
| 313 | public type LLVMAtomicOrdering enum { | ||
| 314 | NotAtomic = 0, | ||
| 315 | Unordered = 1, | ||
| 316 | Monotonic = 2, | ||
| 317 | Acquire = 4, | ||
| 318 | Release = 5, | ||
| 319 | AcquireRelease = 6, | ||
| 320 | SequentiallyConsistent = 7 | ||
| 321 | } | ||
| 322 | |||
| 323 | public type LLVMAtomicRMWBinOp enum { | ||
| 324 | Xchg, | ||
| 325 | Add, | ||
| 326 | Sub, | ||
| 327 | And, | ||
| 328 | Nand, | ||
| 329 | Or, | ||
| 330 | Xor, | ||
| 331 | Max, | ||
| 332 | Min, | ||
| 333 | UMax, | ||
| 334 | UMin, | ||
| 335 | FAdd, | ||
| 336 | FSub, | ||
| 337 | FMax, | ||
| 338 | FMin | ||
| 339 | } | ||
| 340 | |||
| 341 | public type LLVMDiagnosticSeverity enum { | ||
| 342 | Error, | ||
| 343 | Warning, | ||
| 344 | Remark, | ||
| 345 | Note | ||
| 346 | } | ||
| 347 | |||
| 348 | public type LLVMInlineAsmDialect enum { | ||
| 349 | ATT, | ||
| 350 | Intel | ||
| 351 | } | ||
| 352 | |||
| 353 | public type LLVMModuleFlagBehaviour enum { | ||
| 354 | Error, | ||
| 355 | Warning, | ||
| 356 | Require, | ||
| 357 | Override, | ||
| 358 | Append, | ||
| 359 | AppendUnique | ||
| 360 | } | ||
| 361 | |||
| 362 | public type LLVMTailCallKind enum { | ||
| 363 | None, | ||
| 364 | Tail, | ||
| 365 | MustTail, | ||
| 366 | NoTail | ||
| 367 | } | ||
| 368 | |||
| 369 | public type LLVMCodeGenOptLevel enum { | ||
| 370 | None = 0, | ||
| 371 | Less = 1, | ||
| 372 | Default = 2, | ||
| 373 | Aggressive = 3 | ||
| 374 | } | ||
| 375 | |||
| 376 | public type LLVMRelocMode enum { | ||
| 377 | Default = 0, | ||
| 378 | Static = 1, | ||
| 379 | PIC = 2, | ||
| 380 | DynamicNoPic = 3, | ||
| 381 | ROPI = 4, | ||
| 382 | RWPI = 5, | ||
| 383 | ROPI_RWPI = 6 | ||
| 384 | } | ||
| 385 | |||
| 386 | public type LLVMCodeModel enum { | ||
| 387 | Default = 0, | ||
| 388 | JITDefault = 1, | ||
| 389 | Tiny = 2, | ||
| 390 | Small = 3, | ||
| 391 | Kernel = 4, | ||
| 392 | Medium = 5, | ||
| 393 | Large = 6 | ||
| 394 | } | ||
| 395 | |||
| 396 | public type LLVMCodeGenFileType enum { | ||
| 397 | AssemblyFile = 0, | ||
| 398 | ObjectFile = 1 | ||
| 399 | } | ||
| 400 | |||
| 401 | public type LLVMVerifierFailureAction enum { | ||
| 402 | AbortProcessAction = 0, | ||
| 403 | PrintMessageAction = 1, | ||
| 404 | ReturnStatusAction = 2 | ||
| 405 | } | ||
| 406 | |||
| 407 | // ===== Additional helper enums, that are not included in llvm-c ===== | ||
| 408 | public type OptimizationLevel enum { | ||
| 409 | O0 = 0, | ||
| 410 | O1 = 1, | ||
| 411 | O2 = 2, | ||
| 412 | O3 = 3, | ||
| 413 | Os = 4, | ||
| 414 | Oz = 5 | ||
| 415 | } | ||
| 416 | |||
| 417 | // ===== External function declarations ===== | ||
| 418 | ext f<LLVMContextRef> LLVMContextCreate(); | ||
| 419 | ext p LLVMContextDispose(LLVMContextRef /*C*/); | ||
| 420 | ext f<LLVMModuleRef> LLVMModuleCreateWithNameInContext(string /*ModuleID*/, LLVMContextRef /*C*/); | ||
| 421 | ext f<LLVMContextRef> LLVMGetModuleContext(LLVMModuleRef /*M*/); | ||
| 422 | ext p LLVMDisposeModule(LLVMModuleRef /*M*/); | ||
| 423 | ext f<LLVMBuilderRef> LLVMCreateBuilderInContext(LLVMContextRef /*C*/); | ||
| 424 | ext p LLVMDisposeBuilder(LLVMBuilderRef /*B*/); | ||
| 425 | ext f<LLVMTypeRef> LLVMFunctionType(LLVMTypeRef /*ReturnType*/, LLVMTypeRef* /*ParamTypes*/, unsigned int /*ParamCount*/, LLVMBool /*IsVarArg*/); | ||
| 426 | ext f<LLVMTypeRef> LLVMInt1TypeInContext(LLVMContextRef /*C*/); | ||
| 427 | ext f<LLVMTypeRef> LLVMInt8TypeInContext(LLVMContextRef /*C*/); | ||
| 428 | ext f<LLVMTypeRef> LLVMInt16TypeInContext(LLVMContextRef /*C*/); | ||
| 429 | ext f<LLVMTypeRef> LLVMInt32TypeInContext(LLVMContextRef /*C*/); | ||
| 430 | ext f<LLVMTypeRef> LLVMInt64TypeInContext(LLVMContextRef /*C*/); | ||
| 431 | ext f<LLVMTypeRef> LLVMDoubleTypeInContext(LLVMContextRef /*C*/); | ||
| 432 | ext f<LLVMTypeRef> LLVMPointerTypeInContext(LLVMContextRef /*C*/, unsigned int /*AddressSpace*/); | ||
| 433 | ext f<LLVMTypeRef> LLVMArrayType2(LLVMTypeRef /*ElementType*/, unsigned long /*ElementCount*/); | ||
| 434 | ext f<LLVMValueRef> LLVMConstInt(LLVMTypeRef /*IntTy*/, unsigned long /*N*/, LLVMBool /*SignExtend*/); | ||
| 435 | ext f<LLVMValueRef> LLVMConstReal(LLVMTypeRef /*RealTy*/, double /*N*/); | ||
| 436 | ext f<LLVMValueRef> LLVMConstNull(LLVMTypeRef /*Ty*/); | ||
| 437 | ext f<LLVMValueRef> LLVMConstStructInContext(LLVMContextRef /*C*/, LLVMValueRef* /*ConstantVals*/, unsigned int /*Count*/, LLVMBool /*Packed*/); | ||
| 438 | ext f<LLVMValueRef> LLVMConstArray2(LLVMTypeRef /*ElementTy*/, LLVMValueRef* /*ConstantVals*/, unsigned long /*Length*/); | ||
| 439 | ext p LLVMDumpModule(LLVMModuleRef /*M*/); | ||
| 440 | ext p LLVMDumpType(LLVMTypeRef /*Val*/); | ||
| 441 | ext f<LLVMTypeRef> LLVMStructTypeInContext(LLVMContextRef /*C*/, LLVMTypeRef* /*ElementTypes*/, unsigned int /*ElementCount*/, LLVMBool /*Packed*/); | ||
| 442 | ext f<LLVMTypeRef> LLVMStructCreateNamed(LLVMContextRef /*C*/, string /*Name*/); | ||
| 443 | ext f<string> LLVMGetStructName(LLVMTypeRef /*Ty*/); | ||
| 444 | ext p LLVMStructSetBody(LLVMTypeRef /*StructTy*/, LLVMTypeRef* /*ElementTypes*/, unsigned int /*ElementCount*/, LLVMBool /*Packed*/); | ||
| 445 | ext f<LLVMTypeRef> LLVMStructGetTypeAtIndex(LLVMTypeRef /*StructTy*/, unsigned int /*i*/); | ||
| 446 | ext f<unsigned int> LLVMCountStructElementTypes(LLVMTypeRef /*StructTy*/); | ||
| 447 | ext f<LLVMTypeRef> LLVMIntTypeInContext(LLVMContextRef /*C*/, unsigned int /*NumBits*/); | ||
| 448 | ext p LLVMDumpValue(LLVMValueRef /*Val*/); | ||
| 449 | ext f<LLVMBool> LLVMVerifyModule(LLVMModuleRef /*M*/, LLVMVerifierFailureAction /*Action*/, string* /*OutMessage*/); | ||
| 450 | ext f<LLVMBool> LLVMVerifyFunction(LLVMFunctionRef /*Fn*/, LLVMVerifierFailureAction /*Action*/); | ||
| 451 | ext p LLVMViewFunctionCFG(LLVMValueRef /*Fn*/); | ||
| 452 | ext p LLVMViewFunctionCFGOnly(LLVMValueRef /*Fn*/); | ||
| 453 | ext f<LLVMValueRef> LLVMAddFunction(LLVMModuleRef /*M*/, string /*Name*/, LLVMTypeRef /*FunctionTy*/); | ||
| 454 | ext p LLVMSetLinkage(LLVMValueRef /*Global*/, LLVMLinkage /*Linkage*/); | ||
| 455 | ext f<LLVMBasicBlockRef> LLVMCreateBasicBlockInContext(LLVMContextRef /*C*/, string /*Name*/); | ||
| 456 | ext f<LLVMBasicBlockRef> LLVMGetInsertBlock(LLVMBuilderRef /*Builder*/); | ||
| 457 | ext p LLVMAppendExistingBasicBlock(LLVMValueRef /*Fn*/, LLVMBasicBlockRef /*BB*/); | ||
| 458 | ext p LLVMPositionBuilderAtEnd(LLVMBuilderRef /*Builder*/, LLVMBasicBlockRef /*BB*/); | ||
| 459 | ext f<LLVMValueRef> LLVMGetNamedGlobal(LLVMModuleRef /*M*/, string /*Name*/); | ||
| 460 | ext f<LLVMValueRef> LLVMBuildGlobalString(LLVMBuilderRef /*B*/, string /*Str*/, string /*Name*/); | ||
| 461 | ext f<LLVMValueRef> LLVMBuildRet(LLVMBuilderRef /*B*/, LLVMValueRef /*V*/); | ||
| 462 | ext f<LLVMValueRef> LLVMBuildRetVoid(LLVMBuilderRef /*B*/); | ||
| 463 | ext f<LLVMValueRef> LLVMBuildBr(LLVMBuilderRef /*B*/, LLVMBasicBlockRef /*Dest*/); | ||
| 464 | ext f<LLVMValueRef> LLVMBuildUnreachable(LLVMBuilderRef /*B*/); | ||
| 465 | ext f<LLVMValueRef> LLVMBuildCondBr(LLVMBuilderRef /*B*/, LLVMValueRef /*If*/, LLVMBasicBlockRef /*Then*/, LLVMBasicBlockRef /*Else*/); | ||
| 466 | ext f<LLVMValueRef> LLVMBuildAlloca(LLVMBuilderRef /*B*/, LLVMTypeRef /*Ty*/, string /*Name*/); | ||
| 467 | ext f<LLVMValueRef> LLVMBuildArrayAlloca(LLVMBuilderRef /*B*/, LLVMTypeRef /*Ty*/, LLVMValueRef /*Val*/, string /*Name*/); | ||
| 468 | ext f<LLVMValueRef> LLVMBuildStore(LLVMBuilderRef /*B*/, LLVMValueRef /*Val*/, LLVMValueRef /*Ptr*/); | ||
| 469 | ext f<LLVMValueRef> LLVMBuildLoad2(LLVMBuilderRef /*B*/, LLVMTypeRef /*Ty*/, LLVMValueRef /*PointerVal*/, string /*Name*/); | ||
| 470 | ext f<LLVMValueRef> LLVMBuildGEP2(LLVMBuilderRef /*B*/, LLVMTypeRef /*Ty*/, LLVMValueRef /*Ptr*/, LLVMValueRef* /*Indices*/, unsigned int /*NumIndices*/, string /*Name*/); | ||
| 471 | ext f<LLVMValueRef> LLVMBuildInBoundsGEP2(LLVMBuilderRef /*B*/, LLVMTypeRef /*Ty*/, LLVMValueRef /*Ptr*/, LLVMValueRef* /*Indices*/, unsigned int /*NumIndices*/, string /*Name*/); | ||
| 472 | ext f<LLVMValueRef> LLVMBuildStructGEP2(LLVMBuilderRef /*B*/, LLVMTypeRef /*Ty*/, LLVMValueRef /*Ptr*/, unsigned int /*Idx*/, string /*Name*/); | ||
| 473 | ext f<LLVMValueRef> LLVMBuildSelect(LLVMBuilderRef /*B*/, LLVMValueRef /*If*/, LLVMValueRef /*Then*/, LLVMValueRef /*Else*/, string /*Name*/); | ||
| 474 | ext f<LLVMValueRef> LLVMBuildPhi(LLVMBuilderRef /*B*/, LLVMTypeRef /*Ty*/, string /*Name*/); | ||
| 475 | ext f<LLVMValueRef> LLVMBuildAdd(LLVMBuilderRef /*B*/, LLVMValueRef /*LHS*/, LLVMValueRef /*RHS*/, string /*Name*/); | ||
| 476 | ext f<LLVMValueRef> LLVMBuildSub(LLVMBuilderRef /*B*/, LLVMValueRef /*LHS*/, LLVMValueRef /*RHS*/, string /*Name*/); | ||
| 477 | ext f<LLVMValueRef> LLVMBuildMul(LLVMBuilderRef /*B*/, LLVMValueRef /*LHS*/, LLVMValueRef /*RHS*/, string /*Name*/); | ||
| 478 | ext f<LLVMValueRef> LLVMBuildSDiv(LLVMBuilderRef /*B*/, LLVMValueRef /*LHS*/, LLVMValueRef /*RHS*/, string /*Name*/); | ||
| 479 | ext f<LLVMValueRef> LLVMBuildUDiv(LLVMBuilderRef /*B*/, LLVMValueRef /*LHS*/, LLVMValueRef /*RHS*/, string /*Name*/); | ||
| 480 | ext f<LLVMValueRef> LLVMBuildURem(LLVMBuilderRef /*B*/, LLVMValueRef /*LHS*/, LLVMValueRef /*RHS*/, string /*Name*/); | ||
| 481 | ext f<LLVMValueRef> LLVMBuildSRem(LLVMBuilderRef /*B*/, LLVMValueRef /*LHS*/, LLVMValueRef /*RHS*/, string /*Name*/); | ||
| 482 | ext f<LLVMValueRef> LLVMBuildFRem(LLVMBuilderRef /*B*/, LLVMValueRef /*LHS*/, LLVMValueRef /*RHS*/, string /*Name*/); | ||
| 483 | ext f<LLVMValueRef> LLVMBuildFAdd(LLVMBuilderRef /*B*/, LLVMValueRef /*LHS*/, LLVMValueRef /*RHS*/, string /*Name*/); | ||
| 484 | ext f<LLVMValueRef> LLVMBuildFSub(LLVMBuilderRef /*B*/, LLVMValueRef /*LHS*/, LLVMValueRef /*RHS*/, string /*Name*/); | ||
| 485 | ext f<LLVMValueRef> LLVMBuildFMul(LLVMBuilderRef /*B*/, LLVMValueRef /*LHS*/, LLVMValueRef /*RHS*/, string /*Name*/); | ||
| 486 | ext f<LLVMValueRef> LLVMBuildFDiv(LLVMBuilderRef /*B*/, LLVMValueRef /*LHS*/, LLVMValueRef /*RHS*/, string /*Name*/); | ||
| 487 | ext f<LLVMValueRef> LLVMBuildICmp(LLVMBuilderRef /*B*/, LLVMIntPredicate /*Predicate*/, LLVMValueRef /*LHS*/, LLVMValueRef /*RHS*/, string /*Name*/); | ||
| 488 | ext f<LLVMValueRef> LLVMBuildFCmp(LLVMBuilderRef /*B*/, LLVMRealPredicate /*Predicate*/, LLVMValueRef /*LHS*/, LLVMValueRef /*RHS*/, string /*Name*/); | ||
| 489 | ext f<LLVMValueRef> LLVMBuildShl(LLVMBuilderRef /*B*/, LLVMValueRef /*LHS*/, LLVMValueRef /*RHS*/, string /*Name*/); | ||
| 490 | ext f<LLVMValueRef> LLVMBuildLShr(LLVMBuilderRef /*B*/, LLVMValueRef /*LHS*/, LLVMValueRef /*RHS*/, string /*Name*/); | ||
| 491 | ext f<LLVMValueRef> LLVMBuildAShr(LLVMBuilderRef /*B*/, LLVMValueRef /*LHS*/, LLVMValueRef /*RHS*/, string /*Name*/); | ||
| 492 | ext f<LLVMValueRef> LLVMBuildAnd(LLVMBuilderRef /*B*/, LLVMValueRef /*LHS*/, LLVMValueRef /*RHS*/, string /*Name*/); | ||
| 493 | ext f<LLVMValueRef> LLVMBuildOr(LLVMBuilderRef /*B*/, LLVMValueRef /*LHS*/, LLVMValueRef /*RHS*/, string /*Name*/); | ||
| 494 | ext f<LLVMValueRef> LLVMBuildXor(LLVMBuilderRef /*B*/, LLVMValueRef /*LHS*/, LLVMValueRef /*RHS*/, string /*Name*/); | ||
| 495 | ext f<LLVMValueRef> LLVMBuildNeg(LLVMBuilderRef /*B*/, LLVMValueRef /*V*/, string /*Name*/); | ||
| 496 | ext f<LLVMValueRef> LLVMBuildFNeg(LLVMBuilderRef /*B*/, LLVMValueRef /*V*/, string /*Name*/); | ||
| 497 | ext f<LLVMValueRef> LLVMBuildNot(LLVMBuilderRef /*B*/, LLVMValueRef /*V*/, string /*Name*/); | ||
| 498 | ext p LLVMSetVolatile(LLVMValueRef /*MemoryAccessInst*/, LLVMBool /*IsVolatile*/); | ||
| 499 | ext f<LLVMValueRef> LLVMBuildCall2(LLVMBuilderRef /*B*/, LLVMTypeRef /*FctTy*/, LLVMValueRef /*Fn*/, LLVMValueRef* /*Args*/, unsigned int /*NumArgs*/, string /*Name*/); | ||
| 500 | ext f<LLVMBool> LLVMPrintModuleToFile(LLVMModuleRef /*M*/, string /*Filename*/, string& /*ErrorMessage*/); | ||
| 501 | ext f<string> LLVMPrintModuleToString(LLVMModuleRef /*M*/); | ||
| 502 | ext f<LLVMPassBuilderOptionsRef> LLVMCreatePassBuilderOptions(); | ||
| 503 | ext p LLVMDisposePassBuilderOptions(LLVMPassBuilderOptionsRef /*Options*/); | ||
| 504 | ext p LLVMPassBuilderOptionsSetDebugLogging(LLVMPassBuilderOptionsRef /*Options*/, LLVMBool /*Enable*/); | ||
| 505 | ext p LLVMPassBuilderOptionsSetLoopInterleaving(LLVMPassBuilderOptionsRef /*Options*/, LLVMBool /*Enable*/); | ||
| 506 | ext p LLVMPassBuilderOptionsSetLoopVectorization(LLVMPassBuilderOptionsRef /*Options*/, LLVMBool /*Enable*/); | ||
| 507 | ext p LLVMPassBuilderOptionsSetSLPVectorization(LLVMPassBuilderOptionsRef /*Options*/, LLVMBool /*Enable*/); | ||
| 508 | ext p LLVMPassBuilderOptionsSetLoopUnrolling(LLVMPassBuilderOptionsRef /*Options*/, LLVMBool /*Enable*/); | ||
| 509 | ext p LLVMPassBuilderOptionsSetLicmMssaNoAccForPromotionCap(LLVMPassBuilderOptionsRef /*Options*/, unsigned int /*LicmMssaNoAccForPromotionCap*/); | ||
| 510 | ext p LLVMPassBuilderOptionsSetCallGraphProfile(LLVMPassBuilderOptionsRef /*Options*/, LLVMBool /*Enable*/); | ||
| 511 | ext p LLVMPassBuilderOptionsSetMergeFunctions(LLVMPassBuilderOptionsRef /*Options*/, LLVMBool /*Enable*/); | ||
| 512 | ext p LLVMPassBuilderOptionsSetInlinerThreshold(LLVMPassBuilderOptionsRef /*Options*/, int /*InlinerThreshold*/); | ||
| 513 | ext f<LLVMErrorRef> LLVMRunPasses(LLVMModuleRef /*M*/, string /*Passes*/, LLVMTargetMachineRef /*TM*/, LLVMPassBuilderOptionsRef /*Options*/); | ||
| 514 | ext p LLVM_InitializeNativeTarget(); | ||
| 515 | ext p LLVM_InitializeNativeAsmParser(); | ||
| 516 | ext p LLVM_InitializeNativeAsmPrinter(); | ||
| 517 | ext p LLVM_InitializeNativeDisassembler(); | ||
| 518 | ext p LLVM_InitializeAllTargetInfos(); | ||
| 519 | ext p LLVM_InitializeAllTargets(); | ||
| 520 | ext p LLVM_InitializeAllTargetMCs(); | ||
| 521 | ext p LLVM_InitializeAllAsmParsers(); | ||
| 522 | ext p LLVM_InitializeAllAsmPrinters(); | ||
| 523 | ext p LLVM_InitializeAllDisassemblers(); | ||
| 524 | ext f<string> LLVMGetDefaultTargetTriple(); | ||
| 525 | ext f<string> LLVMNormalizeTargetTriple(string /*TripleIn*/); | ||
| 526 | ext f<string> LLVMGetHostCPUName(); | ||
| 527 | ext f<string> LLVMGetHostCPUFeatures(); | ||
| 528 | ext f<LLVMBool> LLVMGetTargetFromTriple(string /*Triple*/, LLVMTargetMachineRef* /*OutMachine*/, string* /*OutError*/); | ||
| 529 | ext f<LLVMTargetMachineRef> LLVMCreateTargetMachine(LLVMTargetRef /*T*/, string /*Triple*/, string /*CPU*/, string /*Features*/, LLVMCodeGenOptLevel /*OptLevel*/, LLVMRelocMode /*Reloc*/, LLVMCodeModel /*CodeModel*/); | ||
| 530 | ext p LLVMDisposeTargetMachine(LLVMTargetMachineRef /*TM*/); | ||
| 531 | ext p LLVMSetTargetMachineAsmVerbosity(LLVMTargetMachineRef /*T*/, LLVMBool /*VerboseAsm*/); | ||
| 532 | ext f<LLVMBool> LLVMTargetMachineEmitToFile(LLVMTargetMachineRef /*T*/, LLVMModuleRef /*M*/, string /*Filename*/, LLVMCodeGenFileType /*codegen*/, string* /*ErrorMessage*/); | ||
| 533 | ext f<LLVMTargetDataRef> LLVMCreateTargetDataLayout(LLVMTargetMachineRef /*TM*/); | ||
| 534 | ext p LLVMDisposeTargetData(LLVMTargetDataRef /*TD*/); | ||
| 535 | ext p LLVMSetDataLayout(LLVMModuleRef /*M*/, string /*DataLayoutStr*/); | ||
| 536 | ext p LLVMSetModuleDataLayout(LLVMModuleRef /*M*/, LLVMTargetDataRef /*DL*/); | ||
| 537 | ext p LLVMSetTarget(LLVMModuleRef /*M*/, string /*Triple*/); | ||
| 538 | ext f<unsigned int> LLVMPointerSize(LLVMTargetDataRef /*TD*/); | ||
| 539 | ext f<unsigned long> LLVMSizeOfTypeInBits(LLVMTargetDataRef /*TD*/, LLVMTypeRef /*Ty*/); | ||
| 540 | ext f<unsigned long> LLVMABISizeOfType(LLVMTargetDataRef /*TD*/, LLVMTypeRef /*Ty*/); | ||
| 541 | ext f<unsigned int> LLVMPreferredAlignmentOfType(LLVMTargetDataRef /*TD*/, LLVMTypeRef /*Ty*/); | ||
| 542 | ext f<unsigned int> LLVMABIAlignmentOfType(LLVMTargetDataRef /*TD*/, LLVMTypeRef /*Ty*/); | ||
| 543 | ext f<unsigned long> LLVMOffsetOfElement(LLVMTargetDataRef /*TD*/, LLVMTypeRef /*StructTy*/, unsigned int /*Element*/); | ||
| 544 | ext f<unsigned int> LLVMElementAtOffset(LLVMTargetDataRef /*TD*/, LLVMTypeRef /*StructTy*/, unsigned long /*Offset*/); | ||
| 545 | ext p LLVMParseCommandLineOptions(int /*argc*/, const string* /*argv*/, string /*Overview*/); | ||
| 546 | ext f<LLVMErrorRef> LLVMCreateStringError(string /*ErrMsg*/); | ||
| 547 | ext f<string> LLVMGetErrorMessage(LLVMErrorRef /*Err*/); | ||
| 548 | ext p LLVMDisposeErrorMessage(string /*ErrMsg*/); | ||
| 549 | ext p LLVMConsumeError(LLVMErrorRef /*Err*/); | ||
| 550 | ext f<string> LLVMCreateMessage(string /*Message*/); | ||
| 551 | ext p LLVMDisposeMessage(string /*Message*/); | ||
| 552 | ext p LLVMGetVersion(unsigned int* /*Major*/, unsigned int* /*Minor*/, unsigned int* /*Patch*/); | ||
| 553 | ext p LLVMShutdown(); | ||
| 554 | |||
| 555 | // Extended API, used by the IR generator of the bootstrap compiler | ||
| 556 | ext f<LLVMContextRef> LLVMGetTypeContext(LLVMTypeRef /*Ty*/); | ||
| 557 | ext p LLVMContextSetDiscardValueNames(LLVMContextRef /*C*/, LLVMBool /*Discard*/); | ||
| 558 | ext p LLVMContextSetDiagnosticHandler(LLVMContextRef /*C*/, byte* /*Handler*/, byte* /*DiagnosticContext*/); | ||
| 559 | ext f<string> LLVMGetDiagInfoDescription(LLVMDiagnosticInfoRef /*DI*/); | ||
| 560 | ext f<LLVMDiagnosticSeverity> LLVMGetDiagInfoSeverity(LLVMDiagnosticInfoRef /*DI*/); | ||
| 561 | ext f<LLVMTypeRef> LLVMVoidTypeInContext(LLVMContextRef /*C*/); | ||
| 562 | ext f<LLVMTypeKind> LLVMGetTypeKind(LLVMTypeRef /*Ty*/); | ||
| 563 | ext f<unsigned int> LLVMGetIntTypeWidth(LLVMTypeRef /*IntegerTy*/); | ||
| 564 | ext f<LLVMTypeRef> LLVMTypeOf(LLVMValueRef /*Val*/); | ||
| 565 | ext f<LLVMTypeRef> LLVMGetReturnType(LLVMTypeRef /*FunctionTy*/); | ||
| 566 | ext f<unsigned int> LLVMCountParamTypes(LLVMTypeRef /*FunctionTy*/); | ||
| 567 | ext p LLVMGetParamTypes(LLVMTypeRef /*FunctionTy*/, LLVMTypeRef* /*Dest*/); | ||
| 568 | ext f<LLVMBool> LLVMIsFunctionVarArg(LLVMTypeRef /*FunctionTy*/); | ||
| 569 | ext f<LLVMTypeRef> LLVMGlobalGetValueType(LLVMValueRef /*Global*/); | ||
| 570 | ext f<unsigned long> LLVMGetArrayLength2(LLVMTypeRef /*ArrayTy*/); | ||
| 571 | ext f<LLVMTypeRef> LLVMGetElementType(LLVMTypeRef /*Ty*/); | ||
| 572 | ext f<LLVMValueRef> LLVMGetNamedFunction(LLVMModuleRef /*M*/, string /*Name*/); | ||
| 573 | ext f<LLVMValueRef> LLVMAddGlobal(LLVMModuleRef /*M*/, LLVMTypeRef /*Ty*/, string /*Name*/); | ||
| 574 | ext p LLVMSetInitializer(LLVMValueRef /*GlobalVar*/, LLVMValueRef /*ConstantVal*/); | ||
| 575 | ext p LLVMSetGlobalConstant(LLVMValueRef /*GlobalVar*/, LLVMBool /*IsConstant*/); | ||
| 576 | ext f<LLVMBool> LLVMIsGlobalConstant(LLVMValueRef /*GlobalVar*/); | ||
| 577 | ext p LLVMSetUnnamedAddress(LLVMValueRef /*Global*/, LLVMUnnamedAddr /*UnnamedAddr*/); | ||
| 578 | ext p LLVMSetAlignment(LLVMValueRef /*V*/, unsigned int /*Bytes*/); | ||
| 579 | ext p LLVMSetThreadLocal(LLVMValueRef /*GlobalVar*/, LLVMBool /*IsThreadLocal*/); | ||
| 580 | ext p LLVMSetDLLStorageClass(LLVMValueRef /*Global*/, LLVMDLLStorageClass /*Class*/); | ||
| 581 | ext p LLVMDeleteGlobal(LLVMValueRef /*GlobalVar*/); | ||
| 582 | ext f<LLVMComdatRef> LLVMGetOrInsertComdat(LLVMModuleRef /*M*/, string /*Name*/); | ||
| 583 | ext p LLVMSetComdat(LLVMValueRef /*V*/, LLVMComdatRef /*C*/); | ||
| 584 | ext f<LLVMValueRef> LLVMConstStringInContext2(LLVMContextRef /*C*/, string /*Str*/, unsigned long /*Length*/, LLVMBool /*DontNullTerminate*/); | ||
| 585 | ext f<LLVMValueRef> LLVMConstNamedStruct(LLVMTypeRef /*StructTy*/, LLVMValueRef* /*ConstantVals*/, unsigned int /*Count*/); | ||
| 586 | ext f<LLVMValueRef> LLVMConstInBoundsGEP2(LLVMTypeRef /*Ty*/, LLVMValueRef /*ConstantVal*/, LLVMValueRef* /*ConstantIndices*/, unsigned int /*NumIndices*/); | ||
| 587 | ext f<LLVMBool> LLVMIsNull(LLVMValueRef /*Val*/); | ||
| 588 | ext f<LLVMOpcode> LLVMGetConstOpcode(LLVMValueRef /*ConstantVal*/); | ||
| 589 | ext f<LLVMValueRef> LLVMGetOperand(LLVMValueRef /*Val*/, unsigned int /*Index*/); | ||
| 590 | ext f<LLVMValueRef> LLVMIsAAllocaInst(LLVMValueRef /*Val*/); | ||
| 591 | ext f<LLVMValueRef> LLVMIsAPHINode(LLVMValueRef /*Val*/); | ||
| 592 | ext f<LLVMValueRef> LLVMIsAGlobalVariable(LLVMValueRef /*Val*/); | ||
| 593 | ext f<LLVMValueRef> LLVMIsAConstantExpr(LLVMValueRef /*Val*/); | ||
| 594 | ext f<LLVMValueRef> LLVMIsAConstantInt(LLVMValueRef /*Val*/); | ||
| 595 | ext f<LLVMValueRef> LLVMIsAConstantFP(LLVMValueRef /*Val*/); | ||
| 596 | ext f<LLVMValueRef> LLVMIsAFunction(LLVMValueRef /*Val*/); | ||
| 597 | ext f<unsigned long> LLVMConstIntGetZExtValue(LLVMValueRef /*ConstantVal*/); | ||
| 598 | ext f<double> LLVMConstRealGetDouble(LLVMValueRef /*ConstantVal*/, LLVMBool* /*LosesInfo*/); | ||
| 599 | ext f<LLVMValueRef> LLVMGetParam(LLVMValueRef /*Fn*/, unsigned int /*Index*/); | ||
| 600 | ext f<unsigned int> LLVMCountParams(LLVMValueRef /*Fn*/); | ||
| 601 | ext p LLVMSetValueName2(LLVMValueRef /*Val*/, string /*Name*/, unsigned long /*NameLen*/); | ||
| 602 | ext f<string> LLVMGetValueName2(LLVMValueRef /*Val*/, unsigned long* /*Length*/); | ||
| 603 | ext f<LLVMValueRef> LLVMGetBasicBlockParent(LLVMBasicBlockRef /*BB*/); | ||
| 604 | ext f<unsigned int> LLVMCountBasicBlocks(LLVMValueRef /*Fn*/); | ||
| 605 | ext f<LLVMValueRef> LLVMGetFirstInstruction(LLVMBasicBlockRef /*BB*/); | ||
| 606 | ext f<LLVMValueRef> LLVMGetNextInstruction(LLVMValueRef /*Inst*/); | ||
| 607 | ext f<LLVMValueRef> LLVMGetFirstFunction(LLVMModuleRef /*M*/); | ||
| 608 | ext f<LLVMValueRef> LLVMGetNextFunction(LLVMValueRef /*Fn*/); | ||
| 609 | ext f<LLVMBasicBlockRef> LLVMGetFirstBasicBlock(LLVMValueRef /*Fn*/); | ||
| 610 | ext f<LLVMBasicBlockRef> LLVMGetNextBasicBlock(LLVMBasicBlockRef /*BB*/); | ||
| 611 | ext f<LLVMBool> LLVMIsDeclaration(LLVMValueRef /*Global*/); | ||
| 612 | ext p LLVMPositionBuilderBeforeDbgRecords(LLVMBuilderRef /*Builder*/, LLVMBasicBlockRef /*Block*/, LLVMValueRef /*Instr*/); | ||
| 613 | ext p LLVMInstructionSetDebugLoc(LLVMValueRef /*Inst*/, LLVMMetadataRef /*Loc*/); | ||
| 614 | ext f<unsigned int> LLVMGetEnumAttributeKindForName(string /*Name*/, unsigned long /*SLen*/); | ||
| 615 | ext f<LLVMAttributeRef> LLVMCreateEnumAttribute(LLVMContextRef /*C*/, unsigned int /*KindID*/, unsigned long /*Val*/); | ||
| 616 | ext f<LLVMAttributeRef> LLVMCreateStringAttribute(LLVMContextRef /*C*/, string /*K*/, unsigned int /*KLength*/, string /*V*/, unsigned int /*VLength*/); | ||
| 617 | ext f<LLVMAttributeRef> LLVMCreateTypeAttribute(LLVMContextRef /*C*/, unsigned int /*KindID*/, LLVMTypeRef /*TypeRef*/); | ||
| 618 | ext p LLVMAddAttributeAtIndex(LLVMValueRef /*F*/, unsigned int /*Idx*/, LLVMAttributeRef /*A*/); | ||
| 619 | ext p LLVMAddCallSiteAttribute(LLVMValueRef /*C*/, unsigned int /*Idx*/, LLVMAttributeRef /*A*/); | ||
| 620 | ext f<LLVMAttributeRef> LLVMGetEnumAttributeAtIndex(LLVMValueRef /*F*/, unsigned int /*Idx*/, unsigned int /*KindID*/); | ||
| 621 | ext f<LLVMTypeRef> LLVMGetTypeAttributeValue(LLVMAttributeRef /*A*/); | ||
| 622 | ext f<LLVMValueRef> LLVMBuildSwitch(LLVMBuilderRef /*B*/, LLVMValueRef /*V*/, LLVMBasicBlockRef /*Else*/, unsigned int /*NumCases*/); | ||
| 623 | ext p LLVMAddCase(LLVMValueRef /*Switch*/, LLVMValueRef /*OnVal*/, LLVMBasicBlockRef /*Dest*/); | ||
| 624 | ext p LLVMAddIncoming(LLVMValueRef /*PhiNode*/, LLVMValueRef* /*IncomingValues*/, LLVMBasicBlockRef* /*IncomingBlocks*/, unsigned int /*Count*/); | ||
| 625 | ext f<LLVMValueRef> LLVMBuildIntCast2(LLVMBuilderRef /*B*/, LLVMValueRef /*Val*/, LLVMTypeRef /*DestTy*/, LLVMBool /*IsSigned*/, string /*Name*/); | ||
| 626 | ext f<LLVMValueRef> LLVMBuildZExt(LLVMBuilderRef /*B*/, LLVMValueRef /*Val*/, LLVMTypeRef /*DestTy*/, string /*Name*/); | ||
| 627 | ext f<LLVMValueRef> LLVMBuildSExt(LLVMBuilderRef /*B*/, LLVMValueRef /*Val*/, LLVMTypeRef /*DestTy*/, string /*Name*/); | ||
| 628 | ext f<LLVMValueRef> LLVMBuildTrunc(LLVMBuilderRef /*B*/, LLVMValueRef /*Val*/, LLVMTypeRef /*DestTy*/, string /*Name*/); | ||
| 629 | ext f<LLVMValueRef> LLVMBuildBitCast(LLVMBuilderRef /*B*/, LLVMValueRef /*Val*/, LLVMTypeRef /*DestTy*/, string /*Name*/); | ||
| 630 | ext f<LLVMValueRef> LLVMBuildPtrToInt(LLVMBuilderRef /*B*/, LLVMValueRef /*Val*/, LLVMTypeRef /*DestTy*/, string /*Name*/); | ||
| 631 | ext f<LLVMValueRef> LLVMBuildIntToPtr(LLVMBuilderRef /*B*/, LLVMValueRef /*Val*/, LLVMTypeRef /*DestTy*/, string /*Name*/); | ||
| 632 | ext f<LLVMValueRef> LLVMBuildSIToFP(LLVMBuilderRef /*B*/, LLVMValueRef /*Val*/, LLVMTypeRef /*DestTy*/, string /*Name*/); | ||
| 633 | ext f<LLVMValueRef> LLVMBuildUIToFP(LLVMBuilderRef /*B*/, LLVMValueRef /*Val*/, LLVMTypeRef /*DestTy*/, string /*Name*/); | ||
| 634 | ext f<LLVMValueRef> LLVMBuildFPToSI(LLVMBuilderRef /*B*/, LLVMValueRef /*Val*/, LLVMTypeRef /*DestTy*/, string /*Name*/); | ||
| 635 | ext f<LLVMValueRef> LLVMBuildFPToUI(LLVMBuilderRef /*B*/, LLVMValueRef /*Val*/, LLVMTypeRef /*DestTy*/, string /*Name*/); | ||
| 636 | ext p LLVMSetNSW(LLVMValueRef /*ArithInst*/, LLVMBool /*HasNSW*/); | ||
| 637 | ext f<LLVMValueRef> LLVMBuildNSWAdd(LLVMBuilderRef /*B*/, LLVMValueRef /*LHS*/, LLVMValueRef /*RHS*/, string /*Name*/); | ||
| 638 | ext f<LLVMValueRef> LLVMBuildNSWSub(LLVMBuilderRef /*B*/, LLVMValueRef /*LHS*/, LLVMValueRef /*RHS*/, string /*Name*/); | ||
| 639 | ext f<LLVMValueRef> LLVMBuildNSWMul(LLVMBuilderRef /*B*/, LLVMValueRef /*LHS*/, LLVMValueRef /*RHS*/, string /*Name*/); | ||
| 640 | ext f<LLVMValueRef> LLVMGetInlineAsm(LLVMTypeRef /*Ty*/, string /*AsmString*/, unsigned long /*AsmStringSize*/, string /*Constraints*/, unsigned long /*ConstraintsSize*/, LLVMBool /*HasSideEffects*/, LLVMBool /*IsAlignStack*/, LLVMInlineAsmDialect /*Dialect*/, LLVMBool /*CanThrow*/); | ||
| 641 | ext f<unsigned int> LLVMLookupIntrinsicID(string /*Name*/, unsigned long /*NameLen*/); | ||
| 642 | ext f<LLVMValueRef> LLVMGetIntrinsicDeclaration(LLVMModuleRef /*Mod*/, unsigned int /*ID*/, LLVMTypeRef* /*ParamTypes*/, unsigned long /*ParamCount*/); | ||
| 643 | ext f<unsigned int> LLVMGetMDKindIDInContext(LLVMContextRef /*C*/, string /*Name*/, unsigned int /*SLen*/); | ||
| 644 | ext f<LLVMMetadataRef> LLVMMDStringInContext2(LLVMContextRef /*C*/, string /*Str*/, unsigned long /*SLen*/); | ||
| 645 | ext f<LLVMMetadataRef> LLVMMDNodeInContext2(LLVMContextRef /*C*/, LLVMMetadataRef* /*MDs*/, unsigned long /*Count*/); | ||
| 646 | ext f<LLVMMetadataRef> LLVMValueAsMetadata(LLVMValueRef /*Val*/); | ||
| 647 | ext f<LLVMValueRef> LLVMMetadataAsValue(LLVMContextRef /*C*/, LLVMMetadataRef /*MD*/); | ||
| 648 | ext p LLVMSetMetadata(LLVMValueRef /*Val*/, unsigned int /*KindID*/, LLVMValueRef /*Node*/); | ||
| 649 | ext p LLVMAddNamedMetadataOperand(LLVMModuleRef /*M*/, string /*Name*/, LLVMValueRef /*Val*/); | ||
| 650 | ext f<LLVMTargetDataRef> LLVMGetModuleDataLayout(LLVMModuleRef /*M*/); | ||
| 651 | ext f<unsigned long> LLVMStoreSizeOfType(LLVMTargetDataRef /*TD*/, LLVMTypeRef /*Ty*/); | ||
| 652 | ext f<int> LLVMByteOrder(LLVMTargetDataRef /*TD*/); | ||
| 653 | ext f<string> LLVMGetDataLayoutStr(LLVMModuleRef /*M*/); | ||
| 654 | ext f<string> LLVMGetTarget(LLVMModuleRef /*M*/); | ||
| 655 | ext p LLVMPassBuilderOptionsSetVerifyEach(LLVMPassBuilderOptionsRef /*Options*/, LLVMBool /*VerifyEach*/); | ||
| 656 | ext f<LLVMBool> LLVMTargetMachineEmitToMemoryBuffer(LLVMTargetMachineRef /*T*/, LLVMModuleRef /*M*/, LLVMCodeGenFileType /*codegen*/, string* /*ErrorMessage*/, LLVMMemoryBufferRef* /*OutMemBuf*/); | ||
| 657 | ext f<string> LLVMGetBufferStart(LLVMMemoryBufferRef /*MemBuf*/); | ||
| 658 | ext f<unsigned long> LLVMGetBufferSize(LLVMMemoryBufferRef /*MemBuf*/); | ||
| 659 | ext p LLVMDisposeMemoryBuffer(LLVMMemoryBufferRef /*MemBuf*/); | ||
| 660 | ext f<LLVMBool> LLVMLinkModules2(LLVMModuleRef /*Dest*/, LLVMModuleRef /*Src*/); | ||
| 661 | |||
| 662 | // ===== Debug info (llvm-c/DebugInfo.h) ===== | ||
| 663 | ext f<LLVMDIBuilderRef> LLVMCreateDIBuilder(LLVMModuleRef /*M*/); | ||
| 664 | ext p LLVMDisposeDIBuilder(LLVMDIBuilderRef /*Builder*/); | ||
| 665 | ext p LLVMDIBuilderFinalize(LLVMDIBuilderRef /*Builder*/); | ||
| 666 | ext f<LLVMMetadataRef> LLVMDIBuilderCreateFile(LLVMDIBuilderRef /*Builder*/, string /*Filename*/, unsigned long /*FilenameLen*/, string /*Directory*/, unsigned long /*DirectoryLen*/); | ||
| 667 | ext f<LLVMMetadataRef> LLVMDIBuilderCreateCompileUnit(LLVMDIBuilderRef /*Builder*/, int /*Lang*/, LLVMMetadataRef /*FileRef*/, string /*Producer*/, unsigned long /*ProducerLen*/, LLVMBool /*isOptimized*/, string /*Flags*/, unsigned long /*FlagsLen*/, unsigned int /*RuntimeVer*/, string /*SplitName*/, unsigned long /*SplitNameLen*/, int /*Kind*/, unsigned int /*DWOId*/, LLVMBool /*SplitDebugInlining*/, LLVMBool /*DebugInfoForProfiling*/, string /*SysRoot*/, unsigned long /*SysRootLen*/, string /*SDK*/, unsigned long /*SDKLen*/); | ||
| 668 | ext f<LLVMMetadataRef> LLVMDIBuilderCreateBasicType(LLVMDIBuilderRef /*Builder*/, string /*Name*/, unsigned long /*NameLen*/, unsigned long /*SizeInBits*/, unsigned int /*Encoding*/, int /*Flags*/); | ||
| 669 | ext f<LLVMMetadataRef> LLVMDIBuilderCreatePointerType(LLVMDIBuilderRef /*Builder*/, LLVMMetadataRef /*PointeeTy*/, unsigned long /*SizeInBits*/, unsigned int /*AlignInBits*/, unsigned int /*AddressSpace*/, string /*Name*/, unsigned long /*NameLen*/); | ||
| 670 | ext f<LLVMMetadataRef> LLVMDIBuilderCreateReferenceType(LLVMDIBuilderRef /*Builder*/, unsigned int /*Tag*/, LLVMMetadataRef /*Type*/); | ||
| 671 | ext f<LLVMMetadataRef> LLVMDIBuilderCreateArrayType(LLVMDIBuilderRef /*Builder*/, unsigned long /*Size*/, unsigned int /*AlignInBits*/, LLVMMetadataRef /*Ty*/, LLVMMetadataRef* /*Subscripts*/, unsigned int /*NumSubscripts*/); | ||
| 672 | ext f<LLVMMetadataRef> LLVMDIBuilderCreateStructType(LLVMDIBuilderRef /*Builder*/, LLVMMetadataRef /*Scope*/, string /*Name*/, unsigned long /*NameLen*/, LLVMMetadataRef /*File*/, unsigned int /*LineNumber*/, unsigned long /*SizeInBits*/, unsigned int /*AlignInBits*/, int /*Flags*/, LLVMMetadataRef /*DerivedFrom*/, LLVMMetadataRef* /*Elements*/, unsigned int /*NumElements*/, unsigned int /*RunTimeLang*/, LLVMMetadataRef /*VTableHolder*/, string /*UniqueId*/, unsigned long /*UniqueIdLen*/); | ||
| 673 | ext f<LLVMMetadataRef> LLVMDIBuilderCreateUnionType(LLVMDIBuilderRef /*Builder*/, LLVMMetadataRef /*Scope*/, string /*Name*/, unsigned long /*NameLen*/, LLVMMetadataRef /*File*/, unsigned int /*LineNumber*/, unsigned long /*SizeInBits*/, unsigned int /*AlignInBits*/, int /*Flags*/, LLVMMetadataRef* /*Elements*/, unsigned int /*NumElements*/, unsigned int /*RunTimeLang*/, string /*UniqueId*/, unsigned long /*UniqueIdLen*/); | ||
| 674 | ext f<LLVMMetadataRef> LLVMDIBuilderCreateClassType(LLVMDIBuilderRef /*Builder*/, LLVMMetadataRef /*Scope*/, string /*Name*/, unsigned long /*NameLen*/, LLVMMetadataRef /*File*/, unsigned int /*LineNumber*/, unsigned long /*SizeInBits*/, unsigned int /*AlignInBits*/, unsigned long /*OffsetInBits*/, int /*Flags*/, LLVMMetadataRef /*DerivedFrom*/, LLVMMetadataRef* /*Elements*/, unsigned int /*NumElements*/, LLVMMetadataRef /*VTableHolder*/, LLVMMetadataRef /*TemplateParamsNode*/, string /*UniqueIdentifier*/, unsigned long /*UniqueIdentifierLen*/); | ||
| 675 | ext f<LLVMMetadataRef> LLVMDIBuilderCreateMemberType(LLVMDIBuilderRef /*Builder*/, LLVMMetadataRef /*Scope*/, string /*Name*/, unsigned long /*NameLen*/, LLVMMetadataRef /*File*/, unsigned int /*LineNo*/, unsigned long /*SizeInBits*/, unsigned int /*AlignInBits*/, unsigned long /*OffsetInBits*/, int /*Flags*/, LLVMMetadataRef /*Ty*/); | ||
| 676 | ext f<LLVMMetadataRef> LLVMDIBuilderCreateQualifiedType(LLVMDIBuilderRef /*Builder*/, unsigned int /*Tag*/, LLVMMetadataRef /*Type*/); | ||
| 677 | ext f<LLVMMetadataRef> LLVMDIBuilderCreateSubroutineType(LLVMDIBuilderRef /*Builder*/, LLVMMetadataRef /*File*/, LLVMMetadataRef* /*ParameterTypes*/, unsigned int /*NumParameterTypes*/, int /*Flags*/); | ||
| 678 | ext f<LLVMMetadataRef> LLVMDIBuilderCreateFunction(LLVMDIBuilderRef /*Builder*/, LLVMMetadataRef /*Scope*/, string /*Name*/, unsigned long /*NameLen*/, string /*LinkageName*/, unsigned long /*LinkageNameLen*/, LLVMMetadataRef /*File*/, unsigned int /*LineNo*/, LLVMMetadataRef /*Ty*/, LLVMBool /*IsLocalToUnit*/, LLVMBool /*IsDefinition*/, unsigned int /*ScopeLine*/, int /*Flags*/, LLVMBool /*IsOptimized*/); | ||
| 679 | ext f<LLVMMetadataRef> LLVMDIBuilderCreateLexicalBlock(LLVMDIBuilderRef /*Builder*/, LLVMMetadataRef /*Scope*/, LLVMMetadataRef /*File*/, unsigned int /*Line*/, unsigned int /*Column*/); | ||
| 680 | ext f<LLVMMetadataRef> LLVMDIBuilderCreateParameterVariable(LLVMDIBuilderRef /*Builder*/, LLVMMetadataRef /*Scope*/, string /*Name*/, unsigned long /*NameLen*/, unsigned int /*ArgNo*/, LLVMMetadataRef /*File*/, unsigned int /*LineNo*/, LLVMMetadataRef /*Ty*/, LLVMBool /*AlwaysPreserve*/, int /*Flags*/); | ||
| 681 | ext f<LLVMMetadataRef> LLVMDIBuilderCreateAutoVariable(LLVMDIBuilderRef /*Builder*/, LLVMMetadataRef /*Scope*/, string /*Name*/, unsigned long /*NameLen*/, LLVMMetadataRef /*File*/, unsigned int /*LineNo*/, LLVMMetadataRef /*Ty*/, LLVMBool /*AlwaysPreserve*/, int /*Flags*/, unsigned int /*AlignInBits*/); | ||
| 682 | ext f<LLVMMetadataRef> LLVMDIBuilderCreateExpression(LLVMDIBuilderRef /*Builder*/, unsigned long* /*Addr*/, unsigned long /*Length*/); | ||
| 683 | ext f<LLVMMetadataRef> LLVMDIBuilderCreateGlobalVariableExpression(LLVMDIBuilderRef /*Builder*/, LLVMMetadataRef /*Scope*/, string /*Name*/, unsigned long /*NameLen*/, string /*Linkage*/, unsigned long /*LinkLen*/, LLVMMetadataRef /*File*/, unsigned int /*LineNo*/, LLVMMetadataRef /*Ty*/, LLVMBool /*LocalToUnit*/, LLVMMetadataRef /*Expr*/, LLVMMetadataRef /*Decl*/, unsigned int /*AlignInBits*/); | ||
| 684 | ext p LLVMReplaceArrays(LLVMDIBuilderRef /*Builder*/, LLVMMetadataRef* /*T*/, LLVMMetadataRef* /*Elements*/, unsigned int /*NumElements*/); | ||
| 685 | ext f<LLVMMetadataRef> LLVMDIBuilderCreateDebugLocation(LLVMContextRef /*Ctx*/, unsigned int /*Line*/, unsigned int /*Column*/, LLVMMetadataRef /*Scope*/, LLVMMetadataRef /*InlinedAt*/); | ||
| 686 | ext f<LLVMDbgRecordRef> LLVMDIBuilderInsertDeclareRecordAtEnd(LLVMDIBuilderRef /*Builder*/, LLVMValueRef /*Storage*/, LLVMMetadataRef /*VarInfo*/, LLVMMetadataRef /*Expr*/, LLVMMetadataRef /*DebugLoc*/, LLVMBasicBlockRef /*Block*/); | ||
| 687 | ext f<unsigned long> LLVMDITypeGetSizeInBits(LLVMMetadataRef /*DType*/); | ||
| 688 | ext f<unsigned int> LLVMDITypeGetAlignInBits(LLVMMetadataRef /*DType*/); | ||
| 689 | ext p LLVMSetSubprogram(LLVMValueRef /*Func*/, LLVMMetadataRef /*SP*/); | ||
| 690 | ext p LLVMSetCurrentDebugLocation2(LLVMBuilderRef /*Builder*/, LLVMMetadataRef /*Loc*/); | ||
| 691 | ext f<LLVMMetadataRef> LLVMGetCurrentDebugLocation2(LLVMBuilderRef /*Builder*/); | ||
| 692 | ext p LLVMGlobalSetMetadata(LLVMValueRef /*Global*/, unsigned int /*Kind*/, LLVMMetadataRef /*MD*/); | ||
| 693 | |||
| 694 | // ===== Message ===== | ||
| 695 | |||
| 696 | // Helper for obtaining and disposing heap-allocated strings, allocated by LLVM | ||
| 697 | public type Message struct { | ||
| 698 | string message | ||
| 699 | } | ||
| 700 | |||
| 701 | 8827 | public p Message.ctor(string message = nil<string>) { | |
| 702 | 8827 | this.message = message; | |
| 703 | 8827 | } | |
| 704 | |||
| 705 | 8827 | public p Message.dtor() { | |
| 706 | 8827 | if this.message != nil<string> { | |
| 707 | 8827 | LLVMDisposeMessage(this.message); | |
| 708 | 8827 | this.message = nil<string>; | |
| 709 | 8827 | } | |
| 710 | 8827 | } | |
| 711 | |||
| 712 | 1 | p Message.set(string message) { | |
| 713 | 1 | this.message = message; | |
| 714 | 1 | } | |
| 715 | |||
| 716 | 8826 | public const f<string> Message.get() { | |
| 717 | 8826 | return this.message; | |
| 718 | } | ||
| 719 | |||
| 720 | // ===== LLVMError ===== | ||
| 721 | public type LLVMError struct { | ||
| 722 | LLVMErrorRef self | ||
| 723 | } | ||
| 724 | |||
| 725 | ✗ | public p LLVMError.ctor(string message) { | |
| 726 | ✗ | this.self = LLVMCreateStringError(message); | |
| 727 | ✗ | } | |
| 728 | |||
| 729 | 4414 | public p LLVMError.ctor(LLVMErrorRef error) { | |
| 730 | 4414 | this.self = error; | |
| 731 | 4414 | } | |
| 732 | |||
| 733 | 4414 | public p LLVMError.dtor() { | |
| 734 | 4414 | if this.self != nil<LLVMErrorRef> { | |
| 735 | ✗ | LLVMConsumeError(this.self); | |
| 736 | ✗ | } | |
| 737 | 4414 | } | |
| 738 | |||
| 739 | // Check if this is an actual error (and not a success value) | ||
| 740 | 4413 | public f<bool> LLVMError.isError() { | |
| 741 | 4413 | return this.self != nil<LLVMErrorRef>; | |
| 742 | } | ||
| 743 | |||
| 744 | // Retrieve the error message. This consumes the error | ||
| 745 | ✗ | public f<Message> LLVMError.getMessage() { | |
| 746 | ✗ | const string errorMessage = LLVMGetErrorMessage(this.self); | |
| 747 | ✗ | this.self = nil<LLVMErrorRef>; | |
| 748 | // The error message has to be released with LLVMDisposeErrorMessage, a Message with LLVMDisposeMessage | ||
| 749 | ✗ | const string message = LLVMCreateMessage(errorMessage); | |
| 750 | ✗ | LLVMDisposeErrorMessage(errorMessage); | |
| 751 | ✗ | return Message(message); | |
| 752 | } | ||
| 753 | |||
| 754 | // ===== DiagnosticCollector ===== | ||
| 755 | /** | ||
| 756 | * Collects the diagnostics of an LLVM context, see LLVMContext.setDiagnosticCollector | ||
| 757 | */ | ||
| 758 | 4406 | public type DiagnosticCollector struct { | |
| 759 | public Vector<String> errors | ||
| 760 | public Vector<String> warnings // Warnings and notes. Remarks are dropped | ||
| 761 | } | ||
| 762 | |||
| 763 | 4407 | public p DiagnosticCollector.ctor() {} | |
| 764 | |||
| 765 | // Receives a diagnostic from LLVM | ||
| 766 | 91447 | p collectDiagnostic(LLVMDiagnosticInfoRef diagInfo, byte* context) { | |
| 767 | 91447 | DiagnosticCollector* collector; | |
| 768 | unsafe { | ||
| 769 | 91447 | collector = cast<DiagnosticCollector*>(context); | |
| 770 | } | ||
| 771 | 91447 | const LLVMDiagnosticSeverity severity = LLVMGetDiagInfoSeverity(diagInfo); | |
| 772 | 91447 | if severity == LLVMDiagnosticSeverity::Remark { | |
| 773 | 91446 | return; | |
| 774 | } | ||
| 775 | 1 | const string description = LLVMGetDiagInfoDescription(diagInfo); | |
| 776 | 1 | if severity == LLVMDiagnosticSeverity::Error { | |
| 777 | 1 | collector.errors.pushBack(String(description)); | |
| 778 | 1 | } else { | |
| 779 | ✗ | collector.warnings.pushBack(String(description)); | |
| 780 | ✗ | } | |
| 781 | 1 | LLVMDisposeMessage(description); | |
| 782 | 1 | } | |
| 783 | |||
| 784 | // ===== LLVMContext ===== | ||
| 785 | public type LLVMContext struct { | ||
| 786 | LLVMContextRef self | ||
| 787 | } | ||
| 788 | |||
| 789 | 6255 | public p LLVMContext.ctor() { | |
| 790 | 6255 | this.self = LLVMContextCreate(); | |
| 791 | 6255 | } | |
| 792 | |||
| 793 | // Like llvm::LLVMContext::setDiscardValueNames | ||
| 794 | 5018 | public p LLVMContext.setDiscardValueNames(bool discard) { | |
| 795 | 5018 | LLVMContextSetDiscardValueNames(this.self, cast<LLVMBool>(discard)); | |
| 796 | 5018 | } | |
| 797 | |||
| 798 | /** | ||
| 799 | * Let the given collector receive the diagnostics of this context, instead of the default diagnostic handler of LLVM, | ||
| 800 | * which prints them and exits the process on errors. The collector has to outlive its use, so reset the handler with | ||
| 801 | * resetDiagnosticHandler before it goes out of scope. | ||
| 802 | * | ||
| 803 | * @param collector Collector for the diagnostics | ||
| 804 | */ | ||
| 805 | 4407 | public p LLVMContext.setDiagnosticCollector(DiagnosticCollector* collector) { | |
| 806 | // The handler is passed as 'byte*' (its fat thunk), since LLVM calls it like a C function | ||
| 807 | unsafe { | ||
| 808 | 4407 | LLVMContextSetDiagnosticHandler(this.self, cast<byte*>(collectDiagnostic), cast<byte*>(collector)); | |
| 809 | } | ||
| 810 | 4407 | } | |
| 811 | |||
| 812 | // Restore the default diagnostic handler of LLVM | ||
| 813 | 4407 | public p LLVMContext.resetDiagnosticHandler() { | |
| 814 | 4407 | LLVMContextSetDiagnosticHandler(this.self, nil<byte*>, nil<byte*>); | |
| 815 | 4407 | } | |
| 816 | |||
| 817 | 5322 | public p LLVMContext.dtor() { | |
| 818 | 5322 | this.dispose(); | |
| 819 | 5322 | } | |
| 820 | |||
| 821 | /** | ||
| 822 | * Dispose the context before the end of its lifetime, e.g. to release its memory early. All modules and builders of the | ||
| 823 | * context have to be disposed before. Disposing it twice has no effect | ||
| 824 | */ | ||
| 825 | 9729 | public p LLVMContext.dispose() { | |
| 826 | 9729 | if this.self != nil<LLVMContextRef> { | |
| 827 | 5322 | LLVMContextDispose(this.self); | |
| 828 | 5322 | this.self = nil<LLVMContextRef>; | |
| 829 | 5322 | } | |
| 830 | 9729 | } | |
| 831 | |||
| 832 | // ===== Type ===== | ||
| 833 | public type Type struct { | ||
| 834 | LLVMTypeRef self | ||
| 835 | } | ||
| 836 | |||
| 837 | ✗ | public p Type.dump() { | |
| 838 | ✗ | LLVMDumpType(this.self); | |
| 839 | ✗ | } | |
| 840 | |||
| 841 | 11215 | public f<Type> Type.getStructElementType(unsigned int idx) { | |
| 842 | 11215 | return Type{ LLVMStructGetTypeAtIndex(this.self, idx) }; | |
| 843 | } | ||
| 844 | |||
| 845 | 2586 | public f<Type> getInt1Ty(const LLVMContext& context) { | |
| 846 | 2586 | return Type{ LLVMInt1TypeInContext(context.self) }; | |
| 847 | } | ||
| 848 | |||
| 849 | 2539 | public f<Type> getInt8Ty(const LLVMContext& context) { | |
| 850 | 2539 | return Type{ LLVMInt8TypeInContext(context.self) }; | |
| 851 | } | ||
| 852 | |||
| 853 | 334 | public f<Type> getInt16Ty(const LLVMContext& context) { | |
| 854 | 334 | return Type{ LLVMInt16TypeInContext(context.self) }; | |
| 855 | } | ||
| 856 | |||
| 857 | 3111 | public f<Type> getInt32Ty(const LLVMContext& context) { | |
| 858 | 3111 | return Type{ LLVMInt32TypeInContext(context.self) }; | |
| 859 | } | ||
| 860 | |||
| 861 | 2655 | public f<Type> getInt64Ty(const LLVMContext& context) { | |
| 862 | 2655 | return Type{ LLVMInt64TypeInContext(context.self) }; | |
| 863 | } | ||
| 864 | |||
| 865 | 707 | public f<Type> getIntNTy(const LLVMContext& context, unsigned int numBits) { | |
| 866 | 707 | return Type{ LLVMIntTypeInContext(context.self, numBits) }; | |
| 867 | } | ||
| 868 | |||
| 869 | 414 | public f<Type> getDoubleTy(const LLVMContext& context) { | |
| 870 | 414 | return Type{ LLVMDoubleTypeInContext(context.self) }; | |
| 871 | } | ||
| 872 | |||
| 873 | 26606 | public f<Type> getPtrTy(const LLVMContext& context, unsigned int addressSpace = 0) { | |
| 874 | 26606 | return Type{ LLVMPointerTypeInContext(context.self, addressSpace) }; | |
| 875 | } | ||
| 876 | |||
| 877 | 3204 | public f<Type> getArrayType(Type elementType, unsigned long numElements) { | |
| 878 | 3204 | return Type{ LLVMArrayType2(elementType.self, numElements) }; | |
| 879 | } | ||
| 880 | |||
| 881 | // Equivalent to llvm::StructType::get: Get a literal (unnamed) struct type | ||
| 882 | 11026 | public f<Type> getStructType(const LLVMContext& context, const Vector<Type>& elementTypes, bool packed = false) { | |
| 883 | 11026 | return Type{ LLVMStructTypeInContext(context.self, &elementTypes.getDataPtr().self, cast<unsigned int>(elementTypes.getSize()), cast<LLVMBool>(packed)) }; | |
| 884 | } | ||
| 885 | |||
| 886 | // Equivalent to llvm::StructType::create: Create a named struct type with the given body | ||
| 887 | 13003 | public f<Type> createStructType(const LLVMContext& context, const Vector<Type>& elementTypes, string name, bool packed = false) { | |
| 888 | 13003 | LLVMTypeRef structTy = LLVMStructCreateNamed(context.self, name); | |
| 889 | 13003 | LLVMStructSetBody(structTy, &elementTypes.getDataPtr().self, cast<unsigned int>(elementTypes.getSize()), cast<LLVMBool>(packed)); | |
| 890 | 13003 | return Type{ structTy }; | |
| 891 | } | ||
| 892 | |||
| 893 | // ===== Value ===== | ||
| 894 | public type Value struct { | ||
| 895 | LLVMValueRef self | ||
| 896 | } | ||
| 897 | |||
| 898 | 9228741 | public p Value.ctor() { | |
| 899 | 9228741 | this.self = nil<LLVMValueRef>; | |
| 900 | 9228741 | } | |
| 901 | |||
| 902 | ✗ | public p Value.dump() { | |
| 903 | ✗ | LLVMDumpValue(this.self); | |
| 904 | ✗ | } | |
| 905 | |||
| 906 | // ===== Constant ===== | ||
| 907 | public type Constant alias Value; | ||
| 908 | |||
| 909 | // ===== StructType ===== | ||
| 910 | public type StructType struct { | ||
| 911 | LLVMTypeRef self | ||
| 912 | } | ||
| 913 | |||
| 914 | ✗ | public p StructType.ctor(const LLVMContext& context, const Vector<Type>& elementTypes, bool packed = false) { | |
| 915 | ✗ | this.self = LLVMStructTypeInContext(context.self, &elementTypes.getDataPtr().self, cast<unsigned int>(elementTypes.getSize()), cast<LLVMBool>(packed)); | |
| 916 | ✗ | } | |
| 917 | |||
| 918 | ✗ | public p StructType.ctor(const LLVMContext& context, string name = "") { | |
| 919 | ✗ | this.self = LLVMStructCreateNamed(context.self, name); | |
| 920 | ✗ | } | |
| 921 | |||
| 922 | ✗ | public f<string> StructType.getName() { | |
| 923 | ✗ | return LLVMGetStructName(this.self); | |
| 924 | } | ||
| 925 | |||
| 926 | ✗ | public p StructType.setBody(const Vector<Type>& elementTypes, bool packed = false) { | |
| 927 | ✗ | LLVMStructSetBody(this.self, &elementTypes.getDataPtr().self, cast<unsigned int>(elementTypes.getSize()), cast<LLVMBool>(packed)); | |
| 928 | ✗ | } | |
| 929 | |||
| 930 | ✗ | public p StructType.dump() { | |
| 931 | ✗ | LLVMDumpType(this.self); | |
| 932 | ✗ | } | |
| 933 | |||
| 934 | // ===== DataLayout ===== | ||
| 935 | public type DataLayout struct { | ||
| 936 | LLVMTargetDataRef self | ||
| 937 | } | ||
| 938 | |||
| 939 | 5673 | public p DataLayout.dtor() { | |
| 940 | 5673 | LLVMDisposeTargetData(this.self); | |
| 941 | 5673 | } | |
| 942 | |||
| 943 | ✗ | public f<unsigned int> DataLayout.getPointerSize() { | |
| 944 | ✗ | return LLVMPointerSize(this.self); | |
| 945 | } | ||
| 946 | |||
| 947 | ✗ | public inline f<unsigned int> DataLayout.getPointerSizeInBits() { | |
| 948 | ✗ | return this.getPointerSize() * 8; | |
| 949 | } | ||
| 950 | |||
| 951 | ✗ | public f<unsigned long> DataLayout.getTypeSize(Type ty) { | |
| 952 | ✗ | return this.getTypeSizeInBits(ty) / 8; | |
| 953 | } | ||
| 954 | |||
| 955 | ✗ | public f<unsigned long> DataLayout.getTypeSizeInBits(Type ty) { | |
| 956 | ✗ | return LLVMSizeOfTypeInBits(this.self, ty.self); | |
| 957 | } | ||
| 958 | |||
| 959 | 697 | public f<unsigned long> DataLayout.getTypeAllocSize(Type ty) { | |
| 960 | 697 | return LLVMABISizeOfType(this.self, ty.self); | |
| 961 | } | ||
| 962 | |||
| 963 | ✗ | public f<unsigned int> DataLayout.getTypeAlignment(Type ty) { | |
| 964 | ✗ | return LLVMPreferredAlignmentOfType(this.self, ty.self); | |
| 965 | } | ||
| 966 | |||
| 967 | 83 | public f<unsigned int> DataLayout.getABITypeAlign(Type ty) { | |
| 968 | 83 | return LLVMABIAlignmentOfType(this.self, ty.self); | |
| 969 | } | ||
| 970 | |||
| 971 | // Equivalent to getStructLayout(structTy)->getElementOffset(idx) | ||
| 972 | 1331 | public f<unsigned long> DataLayout.getStructElementOffset(Type structTy, unsigned int idx) { | |
| 973 | 1331 | return LLVMOffsetOfElement(this.self, structTy.self, idx); | |
| 974 | } | ||
| 975 | |||
| 976 | // ===== Triple ===== | ||
| 977 | public type Triple struct { | ||
| 978 | public string value | ||
| 979 | } | ||
| 980 | |||
| 981 | 5783 | public p Triple.ctor() { | |
| 982 | 5783 | this.value = nil<string>; | |
| 983 | 5783 | } | |
| 984 | |||
| 985 | 138 | public p Triple.ctor(string triple) { | |
| 986 | 138 | this.value = LLVMCreateMessage(triple); | |
| 987 | 138 | } | |
| 988 | |||
| 989 | 12 | public p Triple.ctor(const String& archStr, const String& vendorStr, const String& osStr) { | |
| 990 | 12 | const String newString = archStr + '-' + vendorStr + '-' + osStr; | |
| 991 | 12 | this.value = LLVMCreateMessage(newString.getRaw()); | |
| 992 | 12 | } | |
| 993 | |||
| 994 | // A triple owns its message, so copies have to allocate their own one. Otherwise, they would share the message and it would be | ||
| 995 | // disposed as soon as the first of the copies goes out of scope | ||
| 996 | 14453 | public p Triple.ctor(const Triple& other) { | |
| 997 | 14453 | this.value = nil<string>; | |
| 998 | 14453 | if other.value != nil<string> { | |
| 999 | 14452 | this.value = LLVMCreateMessage(other.value); | |
| 1000 | 14452 | } | |
| 1001 | 14453 | } | |
| 1002 | |||
| 1003 | 5754 | public p operator=(Triple& this, const Triple& other) { | |
| 1004 | 5754 | if this.value == other.value { | |
| 1005 | 1 | return; | |
| 1006 | } | ||
| 1007 | 5753 | if this.value != nil<string> { | |
| 1008 | 14 | LLVMDisposeMessage(this.value); | |
| 1009 | 14 | } | |
| 1010 | 5753 | this.value = nil<string>; | |
| 1011 | 5753 | if other.value != nil<string> { | |
| 1012 | 5753 | this.value = LLVMCreateMessage(other.value); | |
| 1013 | 5753 | } | |
| 1014 | 5753 | } | |
| 1015 | |||
| 1016 | 23976 | public p Triple.dtor() { | |
| 1017 | 23976 | LLVMDisposeMessage(this.value); | |
| 1018 | 23976 | } | |
| 1019 | |||
| 1020 | 1391 | public f<Triple> Triple.normalize() { | |
| 1021 | 1391 | return Triple{ LLVMNormalizeTargetTriple(this.value) }; | |
| 1022 | } | ||
| 1023 | |||
| 1024 | 471173 | f<Vector<String>> Triple.getTripleFragments() { | |
| 1025 | 942346 | return split(String(this.value), '-'); | |
| 1026 | 471173 | } | |
| 1027 | |||
| 1028 | 403981 | public f<String> Triple.getArchName() { | |
| 1029 | 403981 | const Vector<String> fragments = this.getTripleFragments(); | |
| 1030 | 807962 | return fragments.get(0); | |
| 1031 | 403981 | } | |
| 1032 | |||
| 1033 | 1300 | public f<String> Triple.getVendorName() { | |
| 1034 | 1300 | const Vector<String> fragments = this.getTripleFragments(); | |
| 1035 | 2600 | return fragments.get(1); | |
| 1036 | 1300 | } | |
| 1037 | |||
| 1038 | 65892 | public f<String> Triple.getOSName() { | |
| 1039 | 65892 | const Vector<String> fragments = this.getTripleFragments(); | |
| 1040 | 131784 | return fragments.get(2); | |
| 1041 | 65892 | } | |
| 1042 | |||
| 1043 | // Like llvm::Triple::getArchTypeName(getArch()): the canonical name of the architecture, e.g. 'aarch64' for 'arm64' | ||
| 1044 | 11938 | public f<String> Triple.getArchTypeName() { | |
| 1045 | 11938 | const String archName = this.getArchName(); | |
| 1046 | // Aliases of llvm::Triple::parseArch, that do not match the canonical name | ||
| 1047 | 11938 | const dyn aarch64Aliases = ["arm64", "arm64e", "arm64ec", "aarch64_lfi"]; | |
| 1048 | 59381 | for unsigned long i = 0l; i < len(aarch64Aliases); i++ { | |
| 1049 | 47521 | if archName == aarch64Aliases[i] { | |
| 1050 | 156 | return String("aarch64"); | |
| 1051 | } | ||
| 1052 | 47443 | } | |
| 1053 | 11860 | if archName == "arm64_32" { | |
| 1054 | 2 | return String("aarch64_32"); | |
| 1055 | } | ||
| 1056 | 11859 | const dyn x86_64Aliases = ["amd64", "x86_64h", "x86_64_lfi"]; | |
| 1057 | 47433 | for unsigned long i = 0l; i < len(x86_64Aliases); i++ { | |
| 1058 | 35575 | if archName == x86_64Aliases[i] { | |
| 1059 | 2 | return String("x86_64"); | |
| 1060 | } | ||
| 1061 | 35574 | } | |
| 1062 | 11858 | const dyn x86Aliases = ["i386", "i486", "i586", "i686", "i786", "i886", "i986"]; | |
| 1063 | 94856 | for unsigned long i = 0l; i < len(x86Aliases); i++ { | |
| 1064 | 83000 | if archName == x86Aliases[i] { | |
| 1065 | 4 | return String("i386"); | |
| 1066 | } | ||
| 1067 | 82998 | } | |
| 1068 | 11856 | return archName; | |
| 1069 | 82 | } | |
| 1070 | |||
| 1071 | // Like llvm::Triple::getOSTypeName(getOS()): the canonical name of the OS without version, e.g. 'darwin' for 'darwin25.6.0' | ||
| 1072 | 12257 | public f<String> Triple.getOSTypeName() { | |
| 1073 | 12257 | const String osName = this.getOSName(); | |
| 1074 | // OS names and their aliases in the order of llvm/TargetParser/TripleName.def. Like llvm::Triple::parseOS, the first one, | ||
| 1075 | // that the OS fragment starts with, wins. This also skips version suffixes, while keeping digits of names like 'ps4'. | ||
| 1076 | // 'mingw' and 'cygwin' are no OS names there, but llvm::Triple::normalize() turns them into Windows triples. | ||
| 1077 | const dyn osNames = [ | ||
| 1078 | "darwin", "dragonfly", "freebsd", "fuchsia", "ios", "kfreebsd", "linux", "lv2", "macosx", "macos", "managarm", | ||
| 1079 | "netbsd", "openbsd", "solaris", "uefi", "windows", "win32", "zos", "haiku", "rtems", "aix", "cuda", "nvcl", "amdhsa", | ||
| 1080 | "ps4", "ps5", "elfiamcu", "tvos", "watchos", "bridgeos", "driverkit", "xros", "visionos", "mesa3d", "amdpal", "hermit", | ||
| 1081 | "hurd", "wasip1", "wasip2", "wasip3", "wasi", "emscripten", "shadermodel", "liteos", "serenity", "vulkan", | ||
| 1082 | 12257 | "cheriotrtos", "opencl", "chipstar", "firmware", "qurt", "h2", "mingw", "cygwin" | |
| 1083 | ]; | ||
| 1084 | 88069 | for unsigned long i = 0l; i < len(osNames); i++ { | |
| 1085 | 88046 | const string name = osNames[i]; | |
| 1086 | 88046 | if !osName.startsWith(name) { | |
| 1087 | 75812 | continue; | |
| 1088 | } | ||
| 1089 | // Map aliases to their canonical names | ||
| 1090 | 12234 | const String matchedName = String(name); | |
| 1091 | 12234 | if matchedName == "macos" { | |
| 1092 | ✗ | return String("macosx"); | |
| 1093 | } | ||
| 1094 | 12234 | if matchedName == "win32" || matchedName == "mingw" || matchedName == "cygwin" { | |
| 1095 | 4 | return String("windows"); | |
| 1096 | } | ||
| 1097 | 12232 | if matchedName == "visionos" { | |
| 1098 | 2 | return String("xros"); | |
| 1099 | } | ||
| 1100 | 24462 | return matchedName; | |
| 1101 | 3 | } | |
| 1102 | 46 | return String("unknown"); | |
| 1103 | 12257 | } | |
| 1104 | |||
| 1105 | 1303 | public f<bool> Triple.empty() { | |
| 1106 | 1303 | return this.value == nil<string> || len(this.value) == 0; | |
| 1107 | } | ||
| 1108 | |||
| 1109 | // The following predicates mirror the ones of llvm::Triple, based on the textual triple fragments | ||
| 1110 | |||
| 1111 | 3759 | public f<bool> Triple.isOSLinux() { | |
| 1112 | 3759 | const String osName = this.getOSName(); | |
| 1113 | 7518 | return osName.startsWith("linux"); | |
| 1114 | 3759 | } | |
| 1115 | |||
| 1116 | 30759 | public f<bool> Triple.isOSWindows() { | |
| 1117 | 30759 | const String osName = this.getOSName(); | |
| 1118 | 30759 | return osName.startsWith("windows") || osName.startsWith("win32") || osName.startsWith("mingw") || osName.startsWith("cygwin"); | |
| 1119 | 30759 | } | |
| 1120 | |||
| 1121 | // All Apple OSes are Darwin based, like llvm::Triple::isOSDarwin() | ||
| 1122 | 17811 | public f<bool> Triple.isOSDarwin() { | |
| 1123 | 17811 | const String osName = this.getOSName(); | |
| 1124 | 17811 | if osName.startsWith("darwin") || osName.startsWith("macos") || osName.startsWith("ios") || osName.startsWith("tvos") { | |
| 1125 | 74 | return true; | |
| 1126 | } | ||
| 1127 | 17737 | if osName.startsWith("watchos") || osName.startsWith("driverkit") || osName.startsWith("xros") || osName.startsWith("visionos") { | |
| 1128 | ✗ | return true; | |
| 1129 | } | ||
| 1130 | 35474 | return osName.startsWith("bridgeos"); | |
| 1131 | 17811 | } | |
| 1132 | |||
| 1133 | 115 | public f<bool> Triple.isX86() { | |
| 1134 | 115 | const String archName = this.getArchName(); | |
| 1135 | 115 | return archName == "i386" || archName == "i486" || archName == "i586" || archName == "i686" || archName == "x86"; | |
| 1136 | 115 | } | |
| 1137 | |||
| 1138 | 18814 | public f<bool> Triple.isX86_64() { | |
| 1139 | 18814 | const String archName = this.getArchName(); | |
| 1140 | 18814 | return archName == "x86_64" || archName == "amd64"; | |
| 1141 | 18814 | } | |
| 1142 | |||
| 1143 | 398 | public f<bool> Triple.isAArch64() { | |
| 1144 | 398 | const String archName = this.getArchName(); | |
| 1145 | 398 | return archName.startsWith("aarch64") || archName.startsWith("arm64"); | |
| 1146 | 398 | } | |
| 1147 | |||
| 1148 | 370700 | public f<bool> Triple.isPPC() { | |
| 1149 | 370700 | const String archName = this.getArchName(); | |
| 1150 | 370700 | return archName.startsWith("powerpc") || archName.startsWith("ppc"); | |
| 1151 | 370700 | } | |
| 1152 | |||
| 1153 | 713 | public f<bool> Triple.isWasm() { | |
| 1154 | 713 | const String archName = this.getArchName(); | |
| 1155 | 1426 | return archName.startsWith("wasm"); | |
| 1156 | 713 | } | |
| 1157 | |||
| 1158 | // Like getObjectFormat() == llvm::Triple::MachO. Darwin based OSes use the MachO object format | ||
| 1159 | 5898 | public f<bool> Triple.isMachO() { | |
| 1160 | 5898 | return this.isOSDarwin(); | |
| 1161 | } | ||
| 1162 | |||
| 1163 | 88 | public f<bool> operator==(const Triple& a, const Triple& b) { | |
| 1164 | 88 | return a.value == b.value; | |
| 1165 | } | ||
| 1166 | |||
| 1167 | ✗ | public f<bool> operator!=(const Triple& a, const Triple& b) { | |
| 1168 | ✗ | return !(a.value == b.value); | |
| 1169 | } | ||
| 1170 | |||
| 1171 | // ===== LLVMModule ===== | ||
| 1172 | public type Module struct { | ||
| 1173 | 10668 | LLVMModuleRef self = nil<LLVMModuleRef> | |
| 1174 | } | ||
| 1175 | |||
| 1176 | 6252 | public p Module.ctor() { | |
| 1177 | 6252 | this.self = nil<LLVMModuleRef>; | |
| 1178 | 6252 | } | |
| 1179 | |||
| 1180 | 4416 | public p Module.ctor(string name, const LLVMContext& ctx) { | |
| 1181 | 4416 | this.self = LLVMModuleCreateWithNameInContext(name, ctx.self); | |
| 1182 | 4416 | } | |
| 1183 | |||
| 1184 | 9735 | public p Module.dtor() { | |
| 1185 | 9735 | this.dispose(); | |
| 1186 | 9735 | } | |
| 1187 | |||
| 1188 | /** | ||
| 1189 | * Dispose the module before the end of its lifetime, e.g. to release its memory early. Disposing it twice has no effect | ||
| 1190 | */ | ||
| 1191 | 14142 | public p Module.dispose() { | |
| 1192 | 14142 | if this.self != nil<LLVMModuleRef> { | |
| 1193 | 4411 | LLVMDisposeModule(this.self); | |
| 1194 | 4411 | this.self = nil<LLVMModuleRef>; | |
| 1195 | 4411 | } | |
| 1196 | 14142 | } | |
| 1197 | |||
| 1198 | // Raw LLVM C API handle of this module, for bindings that build on top of the LLVM C API (e.g. the TPDE bindings of the Spice std) | ||
| 1199 | 8 | public const f<byte*> Module.getRef() { | |
| 1200 | 8 | return this.self; | |
| 1201 | } | ||
| 1202 | |||
| 1203 | // Like llvm::Linker::linkModules: link the source module into this module. The source module is destroyed in the process | ||
| 1204 | 4 | public f<bool> Module.linkInModule(Module& src) { | |
| 1205 | 4 | const bool failed = LLVMLinkModules2(this.self, src.self) != 0; | |
| 1206 | 4 | src.self = nil<LLVMModuleRef>; | |
| 1207 | 4 | return failed; | |
| 1208 | } | ||
| 1209 | |||
| 1210 | ✗ | public f<LLVMContext> Module.getContext() { | |
| 1211 | ✗ | return LLVMContext{ LLVMGetModuleContext(this.self) }; | |
| 1212 | } | ||
| 1213 | |||
| 1214 | ✗ | public p Module.dump() { | |
| 1215 | ✗ | LLVMDumpModule(this.self); | |
| 1216 | ✗ | } | |
| 1217 | |||
| 1218 | 8826 | public f<Message> Module.print() { | |
| 1219 | 8826 | return Message(LLVMPrintModuleToString(this.self)); | |
| 1220 | } | ||
| 1221 | |||
| 1222 | ✗ | public f<string> Module.printToFile(string filename) { | |
| 1223 | ✗ | string errorMessage; | |
| 1224 | ✗ | if LLVMPrintModuleToFile(this.self, filename, errorMessage) != 0 { | |
| 1225 | ✗ | return errorMessage; | |
| 1226 | } | ||
| 1227 | ✗ | return ""; | |
| 1228 | } | ||
| 1229 | |||
| 1230 | 4412 | public p Module.setTargetTriple(Triple triple) { | |
| 1231 | 4412 | LLVMSetTarget(this.self, triple.value); | |
| 1232 | 4412 | } | |
| 1233 | |||
| 1234 | 4412 | public p Module.setDataLayout(DataLayout dataLayout) { | |
| 1235 | 4412 | LLVMSetModuleDataLayout(this.self, dataLayout.self); | |
| 1236 | 4412 | } | |
| 1237 | |||
| 1238 | // Number of functions with a body in this module. Declarations are not counted | ||
| 1239 | 2 | public f<unsigned long> Module.getDefinedFunctionCount() { | |
| 1240 | 2 | unsigned long count = 0l; | |
| 1241 | 2 | LLVMValueRef fct = LLVMGetFirstFunction(this.self); | |
| 1242 | 6 | while fct != nil<LLVMValueRef> { | |
| 1243 | 4 | if LLVMIsDeclaration(fct) == 0 { | |
| 1244 | 2 | count++; | |
| 1245 | 2 | } | |
| 1246 | 4 | fct = LLVMGetNextFunction(fct); | |
| 1247 | 4 | } | |
| 1248 | 2 | return count; | |
| 1249 | } | ||
| 1250 | |||
| 1251 | // Like llvm::Module::getInstructionCount: number of instructions in all functions of this module | ||
| 1252 | 2 | public f<unsigned long> Module.getInstructionCount() { | |
| 1253 | 2 | unsigned long count = 0l; | |
| 1254 | 2 | LLVMValueRef fct = LLVMGetFirstFunction(this.self); | |
| 1255 | 6 | while fct != nil<LLVMValueRef> { | |
| 1256 | 4 | LLVMBasicBlockRef block = LLVMGetFirstBasicBlock(fct); | |
| 1257 | 6 | while block != nil<LLVMBasicBlockRef> { | |
| 1258 | 2 | LLVMValueRef inst = LLVMGetFirstInstruction(block); | |
| 1259 | 6 | while inst != nil<LLVMValueRef> { | |
| 1260 | 4 | count++; | |
| 1261 | 4 | inst = LLVMGetNextInstruction(inst); | |
| 1262 | 4 | } | |
| 1263 | 2 | block = LLVMGetNextBasicBlock(block); | |
| 1264 | 2 | } | |
| 1265 | 4 | fct = LLVMGetNextFunction(fct); | |
| 1266 | 4 | } | |
| 1267 | 2 | return count; | |
| 1268 | } | ||
| 1269 | |||
| 1270 | // ===== BasicBlock ===== | ||
| 1271 | |||
| 1272 | public type BasicBlock struct { | ||
| 1273 | LLVMBasicBlockRef self | ||
| 1274 | } | ||
| 1275 | |||
| 1276 | 233886 | public p BasicBlock.ctor(const LLVMContext& ctx, string name = "") { | |
| 1277 | 233886 | this.self = LLVMCreateBasicBlockInContext(ctx.self, name); | |
| 1278 | 233886 | } | |
| 1279 | |||
| 1280 | // ===== Function ===== | ||
| 1281 | |||
| 1282 | public type Function struct { | ||
| 1283 | LLVMFunctionRef self | ||
| 1284 | Type fctType | ||
| 1285 | } | ||
| 1286 | |||
| 1287 | 2 | public p Function.ctor(Module module, string name, Type fctTy) { | |
| 1288 | 2 | this.fctType = fctTy; | |
| 1289 | 2 | this.self = LLVMAddFunction(module.self, name, fctTy.self); | |
| 1290 | 2 | } | |
| 1291 | |||
| 1292 | ✗ | public f<Type> Function.getType() { | |
| 1293 | ✗ | return this.fctType; | |
| 1294 | } | ||
| 1295 | |||
| 1296 | 58566 | public p Function.setLinkage(LLVMLinkage linkage) { | |
| 1297 | 58566 | LLVMSetLinkage(this.self, linkage); | |
| 1298 | 58566 | } | |
| 1299 | |||
| 1300 | 233886 | public p Function.pushBack(BasicBlock bb) { | |
| 1301 | 233886 | LLVMAppendExistingBasicBlock(this.self, bb.self); | |
| 1302 | 233886 | } | |
| 1303 | |||
| 1304 | 98326 | #[ignoreUnusedReturnValue] | |
| 1305 | 98326 | public f<Function> Module.getOrInsertFunction(string name, Type fctType) { | |
| 1306 | 98326 | LLVMFunctionRef fctRef = LLVMGetNamedFunction(this.self, name); | |
| 1307 | 98326 | if fctRef == nil<LLVMFunctionRef> { | |
| 1308 | 92942 | fctRef = LLVMAddFunction(this.self, name, fctType.self); | |
| 1309 | 92942 | } | |
| 1310 | 98326 | return Function{ fctRef, fctType }; | |
| 1311 | } | ||
| 1312 | |||
| 1313 | // ===== LLVMBuilder ===== | ||
| 1314 | public type IRBuilder struct { | ||
| 1315 | 10034 | LLVMBuilderRef self = nil<LLVMBuilderRef> | |
| 1316 | 10034 | LLVMContextRef ctx = nil<LLVMContextRef> | |
| 1317 | } | ||
| 1318 | |||
| 1319 | 5016 | public p IRBuilder.ctor() { | |
| 1320 | 5016 | this.self = nil<LLVMBuilderRef>; | |
| 1321 | 5016 | this.ctx = nil<LLVMContextRef>; | |
| 1322 | 5016 | } | |
| 1323 | |||
| 1324 | 5018 | public p IRBuilder.ctor(const LLVMContext& parentCtx) { | |
| 1325 | 5018 | this.ctx = parentCtx.self; | |
| 1326 | 5018 | this.self = LLVMCreateBuilderInContext(this.ctx); | |
| 1327 | 5018 | } | |
| 1328 | |||
| 1329 | 9460 | public p IRBuilder.dtor() { | |
| 1330 | 9460 | this.dispose(); | |
| 1331 | 9460 | } | |
| 1332 | |||
| 1333 | /** | ||
| 1334 | * Dispose the builder before the end of its lifetime, e.g. to release its memory early. Disposing it twice has no effect | ||
| 1335 | */ | ||
| 1336 | 13867 | public p IRBuilder.dispose() { | |
| 1337 | 13867 | if this.self != nil<LLVMBuilderRef> { | |
| 1338 | 4444 | LLVMDisposeBuilder(this.self); | |
| 1339 | 4444 | this.self = nil<LLVMBuilderRef>; | |
| 1340 | 4444 | } | |
| 1341 | 13867 | } | |
| 1342 | |||
| 1343 | 4199 | public f<Type> IRBuilder.getInt1Ty() { | |
| 1344 | 4199 | return Type{ LLVMInt1TypeInContext(this.ctx) }; | |
| 1345 | } | ||
| 1346 | |||
| 1347 | 15182 | public f<Type> IRBuilder.getInt8Ty() { | |
| 1348 | 15182 | return Type{ LLVMInt8TypeInContext(this.ctx) }; | |
| 1349 | } | ||
| 1350 | |||
| 1351 | 3004 | public f<Type> IRBuilder.getInt16Ty() { | |
| 1352 | 3004 | return Type{ LLVMInt16TypeInContext(this.ctx) }; | |
| 1353 | } | ||
| 1354 | |||
| 1355 | 165645 | public f<Type> IRBuilder.getInt32Ty() { | |
| 1356 | 165645 | return Type{ LLVMInt32TypeInContext(this.ctx) }; | |
| 1357 | } | ||
| 1358 | |||
| 1359 | 171761 | public f<Type> IRBuilder.getInt64Ty() { | |
| 1360 | 171761 | return Type{ LLVMInt64TypeInContext(this.ctx) }; | |
| 1361 | } | ||
| 1362 | |||
| 1363 | 182923 | public f<Type> IRBuilder.getPtrTy() { | |
| 1364 | 182923 | return Type{ LLVMPointerTypeInContext(this.ctx, 0) }; | |
| 1365 | } | ||
| 1366 | |||
| 1367 | public f<Value> IRBuilder.getInt1<ShortIntLong>(ShortIntLong value, bool isSigned = false) { | ||
| 1368 | LLVMTypeRef typeRef = LLVMInt1TypeInContext(this.ctx); | ||
| 1369 | return Value{ LLVMConstInt(typeRef, cast<unsigned long>(value), cast<LLVMBool>(isSigned)) }; | ||
| 1370 | } | ||
| 1371 | |||
| 1372 | public f<Value> IRBuilder.getInt8<ShortIntLong>(ShortIntLong value, bool isSigned = false) { | ||
| 1373 | LLVMTypeRef typeRef = LLVMInt8TypeInContext(this.ctx); | ||
| 1374 | return Value{ LLVMConstInt(typeRef, cast<unsigned long>(value), cast<LLVMBool>(isSigned)) }; | ||
| 1375 | } | ||
| 1376 | |||
| 1377 | 40 | public f<Value> IRBuilder.getInt16<ShortIntLong>(ShortIntLong value, bool isSigned = false) { | |
| 1378 | 40 | LLVMTypeRef typeRef = LLVMInt16TypeInContext(this.ctx); | |
| 1379 | 40 | return Value{ LLVMConstInt(typeRef, cast<unsigned long>(value), cast<LLVMBool>(isSigned)) }; | |
| 1380 | } | ||
| 1381 | |||
| 1382 | 11625 | public f<Value> IRBuilder.getInt32<ShortIntLong>(ShortIntLong value, bool isSigned = false) { | |
| 1383 | 11625 | LLVMTypeRef typeRef = LLVMInt32TypeInContext(this.ctx); | |
| 1384 | 11625 | return Value{ LLVMConstInt(typeRef, cast<unsigned long>(value), cast<LLVMBool>(isSigned)) }; | |
| 1385 | } | ||
| 1386 | |||
| 1387 | 20467 | public f<Value> IRBuilder.getInt64<ShortIntLong>(ShortIntLong value, bool isSigned = false) { | |
| 1388 | 20467 | LLVMTypeRef typeRef = LLVMInt64TypeInContext(this.ctx); | |
| 1389 | 20467 | return Value{ LLVMConstInt(typeRef, cast<unsigned long>(value), cast<LLVMBool>(isSigned)) }; | |
| 1390 | } | ||
| 1391 | |||
| 1392 | 8584 | public f<Value> IRBuilder.getFalse() { | |
| 1393 | 8584 | LLVMTypeRef typeRef = LLVMInt1TypeInContext(this.ctx); | |
| 1394 | 8584 | return Value{ LLVMConstInt(typeRef, 0l, 0) }; | |
| 1395 | } | ||
| 1396 | |||
| 1397 | 8118 | public f<Value> IRBuilder.getTrue() { | |
| 1398 | 8118 | LLVMTypeRef typeRef = LLVMInt1TypeInContext(this.ctx); | |
| 1399 | 8118 | return Value{ LLVMConstInt(typeRef, 1l, 0) }; | |
| 1400 | } | ||
| 1401 | |||
| 1402 | ✗ | public f<Value> IRBuilder.getDouble(double value) { | |
| 1403 | ✗ | LLVMTypeRef typeRef = LLVMDoubleTypeInContext(this.ctx); | |
| 1404 | ✗ | return Value{ LLVMConstReal(typeRef, value) }; | |
| 1405 | } | ||
| 1406 | |||
| 1407 | ✗ | public f<Value> IRBuilder.getNull(Type ty) { | |
| 1408 | ✗ | return Value{ LLVMConstNull(ty.self) }; | |
| 1409 | } | ||
| 1410 | |||
| 1411 | ✗ | public f<Value> IRBuilder.getStruct(const Vector<Value>& values, bool packed = false) { | |
| 1412 | unsafe { | ||
| 1413 | ✗ | LLVMValueRef* valuesRef = cast<LLVMValueRef*>(values.getDataPtr()); | |
| 1414 | ✗ | LLVMValueRef valueRef = LLVMConstStructInContext(this.ctx, valuesRef, cast<unsigned int>(values.getSize()), cast<LLVMBool>(packed)); | |
| 1415 | ✗ | return Value{ valueRef }; | |
| 1416 | } | ||
| 1417 | } | ||
| 1418 | |||
| 1419 | ✗ | public f<Value> IRBuilder.getArray(Type ty, const Vector<Value>& values) { | |
| 1420 | unsafe { | ||
| 1421 | ✗ | LLVMValueRef* valuesRef = cast<LLVMValueRef*>(values.getDataPtr()); | |
| 1422 | ✗ | LLVMValueRef valueRef = LLVMConstArray2(ty.self, valuesRef, values.getSize()); | |
| 1423 | ✗ | return Value{ valueRef }; | |
| 1424 | } | ||
| 1425 | } | ||
| 1426 | |||
| 1427 | 508028 | public f<BasicBlock> IRBuilder.getInsertBlock() { | |
| 1428 | 508028 | return BasicBlock{ LLVMGetInsertBlock(this.self) }; | |
| 1429 | } | ||
| 1430 | |||
| 1431 | 477421 | public p IRBuilder.setInsertPoint(BasicBlock bb) { | |
| 1432 | 477421 | LLVMPositionBuilderAtEnd(this.self, bb.self); | |
| 1433 | 477421 | } | |
| 1434 | |||
| 1435 | 1 | public f<Value> IRBuilder.createGlobalString(string content, string name) { | |
| 1436 | 1 | return Value{ LLVMBuildGlobalString(this.self, content, name) }; | |
| 1437 | } | ||
| 1438 | |||
| 1439 | ✗ | public f<Value> IRBuilder.createAlloca(Type ty, Value arraySize = Value(), string name = "") { | |
| 1440 | ✗ | if arraySize.self == nil<LLVMValueRef> { | |
| 1441 | ✗ | return Value{ LLVMBuildAlloca(this.self, ty.self, name) }; | |
| 1442 | } else { | ||
| 1443 | ✗ | return Value{ LLVMBuildArrayAlloca(this.self, ty.self, arraySize.self, name) }; | |
| 1444 | } | ||
| 1445 | ✗ | } | |
| 1446 | |||
| 1447 | 235238 | public f<Value> IRBuilder.createStore(Value value, Value ptr, bool volatile = false) { | |
| 1448 | 235238 | LLVMValueRef valueRef = LLVMBuildStore(this.self, value.self, ptr.self); | |
| 1449 | 235238 | if volatile { | |
| 1450 | 23 | LLVMSetVolatile(valueRef, 1); | |
| 1451 | 23 | } | |
| 1452 | 235238 | return Value{ valueRef }; | |
| 1453 | } | ||
| 1454 | |||
| 1455 | 471722 | public f<Value> IRBuilder.createLoad(Value ptr, Type ty, string name = "", bool volatile = false) { | |
| 1456 | 471722 | LLVMValueRef valueRef = LLVMBuildLoad2(this.self, ty.self, ptr.self, name); | |
| 1457 | 471722 | if volatile { | |
| 1458 | 21 | LLVMSetVolatile(valueRef, 1); | |
| 1459 | 21 | } | |
| 1460 | 471722 | return Value{ valueRef }; | |
| 1461 | } | ||
| 1462 | |||
| 1463 | ✗ | public f<Value> IRBuilder.createGEP(Type ty, Value ptr, const Vector<Value>& indices, string name = "") { | |
| 1464 | unsafe { | ||
| 1465 | ✗ | LLVMValueRef* indicesRef = cast<LLVMValueRef*>(indices.getDataPtr()); | |
| 1466 | ✗ | LLVMValueRef valueRef = LLVMBuildGEP2(this.self, ty.self, ptr.self, indicesRef, cast<unsigned int>(indices.getSize()), name); | |
| 1467 | ✗ | return Value{ valueRef }; | |
| 1468 | } | ||
| 1469 | } | ||
| 1470 | |||
| 1471 | 114081 | public f<Value> IRBuilder.createInBoundsGEP(Type ty, Value ptr, const Vector<Value>& indices, string name = "") { | |
| 1472 | unsafe { | ||
| 1473 | 114081 | LLVMValueRef* indicesRef = cast<LLVMValueRef*>(indices.getDataPtr()); | |
| 1474 | 114081 | LLVMValueRef valueRef = LLVMBuildInBoundsGEP2(this.self, ty.self, ptr.self, indicesRef, cast<unsigned int>(indices.getSize()), name); | |
| 1475 | 114081 | return Value{ valueRef }; | |
| 1476 | } | ||
| 1477 | } | ||
| 1478 | |||
| 1479 | 24662 | public f<Value> IRBuilder.createStructGEP(Type ty, Value ptr, unsigned int index, string name = "") { | |
| 1480 | 24662 | LLVMValueRef valueRef = LLVMBuildStructGEP2(this.self, ty.self, ptr.self, index, name); | |
| 1481 | 24662 | return Value{ valueRef }; | |
| 1482 | } | ||
| 1483 | |||
| 1484 | 1037 | public f<Value> IRBuilder.createSelect(Value condition, Value thenValue, Value elseValue, string name = "") { | |
| 1485 | 1037 | return Value{ LLVMBuildSelect(this.self, condition.self, thenValue.self, elseValue.self, name) }; | |
| 1486 | } | ||
| 1487 | |||
| 1488 | 14133 | public f<Value> IRBuilder.createPhi(Type ty, string name = "") { | |
| 1489 | 14133 | return Value{ LLVMBuildPhi(this.self, ty.self, name) }; | |
| 1490 | } | ||
| 1491 | |||
| 1492 | 23266 | public f<Value> IRBuilder.createAdd(Value lhs, Value rhs, string name = "") { | |
| 1493 | 23266 | return Value{ LLVMBuildAdd(this.self, lhs.self, rhs.self, name) }; | |
| 1494 | } | ||
| 1495 | |||
| 1496 | 9910 | public f<Value> IRBuilder.createSub(Value lhs, Value rhs, string name = "") { | |
| 1497 | 9910 | return Value{ LLVMBuildSub(this.self, lhs.self, rhs.self, name) }; | |
| 1498 | } | ||
| 1499 | |||
| 1500 | 3225 | public f<Value> IRBuilder.createMul(Value lhs, Value rhs, string name = "") { | |
| 1501 | 3225 | return Value{ LLVMBuildMul(this.self, lhs.self, rhs.self, name) }; | |
| 1502 | } | ||
| 1503 | |||
| 1504 | 264 | public f<Value> IRBuilder.createSDiv(Value lhs, Value rhs, string name = "") { | |
| 1505 | 264 | return Value{ LLVMBuildSDiv(this.self, lhs.self, rhs.self, name) }; | |
| 1506 | } | ||
| 1507 | |||
| 1508 | 583 | public f<Value> IRBuilder.createUDiv(Value lhs, Value rhs, string name = "") { | |
| 1509 | 583 | return Value{ LLVMBuildUDiv(this.self, lhs.self, rhs.self, name) }; | |
| 1510 | } | ||
| 1511 | |||
| 1512 | 609 | public f<Value> IRBuilder.createURem(Value lhs, Value rhs, string name = "") { | |
| 1513 | 609 | return Value{ LLVMBuildURem(this.self, lhs.self, rhs.self, name) }; | |
| 1514 | } | ||
| 1515 | |||
| 1516 | 115 | public f<Value> IRBuilder.createSRem(Value lhs, Value rhs, string name = "") { | |
| 1517 | 115 | return Value{ LLVMBuildSRem(this.self, lhs.self, rhs.self, name) }; | |
| 1518 | } | ||
| 1519 | |||
| 1520 | 3 | public f<Value> IRBuilder.createFRem(Value lhs, Value rhs, string name = "") { | |
| 1521 | 3 | return Value{ LLVMBuildFRem(this.self, lhs.self, rhs.self, name) }; | |
| 1522 | } | ||
| 1523 | |||
| 1524 | 591 | public f<Value> IRBuilder.createFAdd(Value lhs, Value rhs, string name = "") { | |
| 1525 | 591 | return Value{ LLVMBuildFAdd(this.self, lhs.self, rhs.self, name) }; | |
| 1526 | } | ||
| 1527 | |||
| 1528 | 133 | public f<Value> IRBuilder.createFSub(Value lhs, Value rhs, string name = "") { | |
| 1529 | 133 | return Value{ LLVMBuildFSub(this.self, lhs.self, rhs.self, name) }; | |
| 1530 | } | ||
| 1531 | |||
| 1532 | 447 | public f<Value> IRBuilder.createFMul(Value lhs, Value rhs, string name = "") { | |
| 1533 | 447 | return Value{ LLVMBuildFMul(this.self, lhs.self, rhs.self, name) }; | |
| 1534 | } | ||
| 1535 | |||
| 1536 | 447 | public f<Value> IRBuilder.createFDiv(Value lhs, Value rhs, string name = "") { | |
| 1537 | 447 | return Value{ LLVMBuildFDiv(this.self, lhs.self, rhs.self, name) }; | |
| 1538 | } | ||
| 1539 | |||
| 1540 | 64330 | public f<Value> IRBuilder.createICmp(LLVMIntPredicate predicate, Value lhs, Value rhs, string name = "") { | |
| 1541 | 64330 | return Value{ LLVMBuildICmp(this.self, predicate, lhs.self, rhs.self, name) }; | |
| 1542 | } | ||
| 1543 | |||
| 1544 | 763 | public f<Value> IRBuilder.createFCmp(LLVMRealPredicate predicate, Value lhs, Value rhs, string name = "") { | |
| 1545 | 763 | return Value{ LLVMBuildFCmp(this.self, predicate, lhs.self, rhs.self, name) }; | |
| 1546 | } | ||
| 1547 | |||
| 1548 | 795 | public f<Value> IRBuilder.createShl(Value lhs, Value rhs, string name = "") { | |
| 1549 | 795 | return Value{ LLVMBuildShl(this.self, lhs.self, rhs.self, name) }; | |
| 1550 | } | ||
| 1551 | |||
| 1552 | 1215 | public f<Value> IRBuilder.createLShr(Value lhs, Value rhs, string name = "") { | |
| 1553 | 1215 | return Value{ LLVMBuildLShr(this.self, lhs.self, rhs.self, name) }; | |
| 1554 | } | ||
| 1555 | |||
| 1556 | 142 | public f<Value> IRBuilder.createAShr(Value lhs, Value rhs, string name = "") { | |
| 1557 | 142 | return Value{ LLVMBuildAShr(this.self, lhs.self, rhs.self, name) }; | |
| 1558 | } | ||
| 1559 | |||
| 1560 | 2061 | public f<Value> IRBuilder.createAnd(Value lhs, Value rhs, string name = "") { | |
| 1561 | 2061 | return Value{ LLVMBuildAnd(this.self, lhs.self, rhs.self, name) }; | |
| 1562 | } | ||
| 1563 | |||
| 1564 | 1305 | public f<Value> IRBuilder.createOr(Value lhs, Value rhs, string name = "") { | |
| 1565 | 1305 | return Value{ LLVMBuildOr(this.self, lhs.self, rhs.self, name) }; | |
| 1566 | } | ||
| 1567 | |||
| 1568 | 347 | public f<Value> IRBuilder.createXor(Value lhs, Value rhs, string name = "") { | |
| 1569 | 347 | return Value{ LLVMBuildXor(this.self, lhs.self, rhs.self, name) }; | |
| 1570 | } | ||
| 1571 | |||
| 1572 | 2089 | public f<Value> IRBuilder.createNeg(Value value, string name = "") { | |
| 1573 | 2089 | return Value{ LLVMBuildNeg(this.self, value.self, name) }; | |
| 1574 | } | ||
| 1575 | |||
| 1576 | 376 | public f<Value> IRBuilder.createFNeg(Value value, string name = "") { | |
| 1577 | 376 | return Value{ LLVMBuildFNeg(this.self, value.self, name) }; | |
| 1578 | } | ||
| 1579 | |||
| 1580 | 6937 | public f<Value> IRBuilder.createNot(Value value, string name = "") { | |
| 1581 | 6937 | return Value{ LLVMBuildNot(this.self, value.self, name) }; | |
| 1582 | } | ||
| 1583 | |||
| 1584 | 60311 | #[ignoreUnusedReturnValue] | |
| 1585 | 60311 | public f<Value> IRBuilder.createCall(Function callee, const Vector<Value>& args, string name = "") { | |
| 1586 | unsafe { | ||
| 1587 | 60311 | LLVMValueRef* argsRef = cast<LLVMValueRef*>(args.getDataPtr()); | |
| 1588 | 60311 | LLVMValueRef valueRef = LLVMBuildCall2(this.self, callee.fctType.self, callee.self, argsRef, cast<unsigned int>(args.getSize()), name); | |
| 1589 | 60311 | return Value{ valueRef }; | |
| 1590 | } | ||
| 1591 | } | ||
| 1592 | |||
| 1593 | 43900 | #[ignoreUnusedReturnValue] | |
| 1594 | 43900 | public f<Value> IRBuilder.createRet(Value returnValue) { | |
| 1595 | 43900 | return Value{ LLVMBuildRet(this.self, returnValue.self) }; | |
| 1596 | } | ||
| 1597 | |||
| 1598 | 37622 | #[ignoreUnusedReturnValue] | |
| 1599 | public f<Value> IRBuilder.createRetVoid() { | ||
| 1600 | 37622 | return Value{ LLVMBuildRetVoid(this.self) }; | |
| 1601 | } | ||
| 1602 | |||
| 1603 | 69283 | #[ignoreUnusedReturnValue] | |
| 1604 | 69283 | public f<Value> IRBuilder.createBr(BasicBlock bb) { | |
| 1605 | 69283 | return Value{ LLVMBuildBr(this.self, bb.self) }; | |
| 1606 | } | ||
| 1607 | |||
| 1608 | 74287 | public f<Value> IRBuilder.createCondBr(Value condition, BasicBlock thenBB, BasicBlock elseBB) { | |
| 1609 | 74287 | return Value{ LLVMBuildCondBr(this.self, condition.self, thenBB.self, elseBB.self) }; | |
| 1610 | } | ||
| 1611 | |||
| 1612 | 8744 | #[ignoreUnusedReturnValue] | |
| 1613 | public f<Value> IRBuilder.createUnreachable() { | ||
| 1614 | 8744 | return Value{ LLVMBuildUnreachable(this.self) }; | |
| 1615 | } | ||
| 1616 | |||
| 1617 | // ===== TargetMachine ===== | ||
| 1618 | public type TargetMachine struct { | ||
| 1619 | LLVMTargetMachineRef self = nil<LLVMTargetMachineRef> | ||
| 1620 | } | ||
| 1621 | |||
| 1622 | p TargetMachine.ctor(LLVMTargetRef targetRef, Triple triple, string cpu, string features, LLVMCodeGenOptLevel optLevel, LLVMRelocMode relocMode, LLVMCodeModel codeModel) { | ||
| 1623 | this.self = LLVMCreateTargetMachine(targetRef, triple.value, cpu, features, optLevel, relocMode, codeModel); | ||
| 1624 | } | ||
| 1625 | |||
| 1626 | 4444 | public p TargetMachine.dtor() { | |
| 1627 | 4444 | this.dispose(); | |
| 1628 | 4444 | } | |
| 1629 | |||
| 1630 | /** | ||
| 1631 | * Dispose the target machine before the end of its lifetime, e.g. to release its memory early. Disposing it twice has no | ||
| 1632 | * effect | ||
| 1633 | */ | ||
| 1634 | 8851 | public p TargetMachine.dispose() { | |
| 1635 | 8851 | if this.self != nil<LLVMTargetMachineRef> { | |
| 1636 | 4444 | LLVMDisposeTargetMachine(this.self); | |
| 1637 | 4444 | this.self = nil<LLVMTargetMachineRef>; | |
| 1638 | 4444 | } | |
| 1639 | 8851 | } | |
| 1640 | |||
| 1641 | /** | ||
| 1642 | * Set whether the target machine emits verbose assembly, i.e. with comments (e.g. block labels and function boundaries) | ||
| 1643 | * | ||
| 1644 | * @param verboseAsm Emit verbose assembly or not | ||
| 1645 | */ | ||
| 1646 | 5016 | public p TargetMachine.setAsmVerbosity(bool verboseAsm) { | |
| 1647 | 5016 | LLVMSetTargetMachineAsmVerbosity(this.self, cast<LLVMBool>(verboseAsm)); | |
| 1648 | 5016 | } | |
| 1649 | |||
| 1650 | ✗ | public f<LLVMError> TargetMachine.emitToFile(const Module& module, string filename, LLVMCodeGenFileType codegen) { | |
| 1651 | ✗ | string errorMessage; | |
| 1652 | ✗ | const bool errorOccurred = LLVMTargetMachineEmitToFile(this.self, module.self, filename, codegen, &errorMessage) != 0; | |
| 1653 | ✗ | assert !errorOccurred; | |
| 1654 | ✗ | return LLVMError(errorMessage); | |
| 1655 | } | ||
| 1656 | |||
| 1657 | /** | ||
| 1658 | * Emit the given module to a file | ||
| 1659 | * | ||
| 1660 | * @param module Module to emit | ||
| 1661 | * @param filename Output file path | ||
| 1662 | * @param codegen Type of the file to emit | ||
| 1663 | * @param errorMessage Error message, if the emission failed | ||
| 1664 | * @return True if the emission failed | ||
| 1665 | */ | ||
| 1666 | 4407 | public f<bool> TargetMachine.emitToFileWithError(const Module& module, string filename, LLVMCodeGenFileType codegen, String& errorMessage) { | |
| 1667 | 4407 | string outMessage = nil<string>; | |
| 1668 | 4407 | const bool errorOccurred = LLVMTargetMachineEmitToFile(this.self, module.self, filename, codegen, &outMessage) != 0; | |
| 1669 | 4407 | if outMessage != nil<string> { | |
| 1670 | ✗ | errorMessage = String(outMessage); | |
| 1671 | ✗ | LLVMDisposeMessage(outMessage); | |
| 1672 | ✗ | } | |
| 1673 | 4407 | return errorOccurred; | |
| 1674 | } | ||
| 1675 | |||
| 1676 | /** | ||
| 1677 | * Emit the given module to a string (e.g. assembly code) | ||
| 1678 | * | ||
| 1679 | * @param module Module to emit | ||
| 1680 | * @param codegen Type of the file to emit | ||
| 1681 | * @param output Emitted file content | ||
| 1682 | * @return True if the emission failed | ||
| 1683 | */ | ||
| 1684 | 4240 | public f<bool> TargetMachine.emitToString(const Module& module, LLVMCodeGenFileType codegen, String& output) { | |
| 1685 | 4240 | string outMessage = nil<string>; | |
| 1686 | 4240 | LLVMMemoryBufferRef buffer = nil<LLVMMemoryBufferRef>; | |
| 1687 | 4240 | const bool errorOccurred = LLVMTargetMachineEmitToMemoryBuffer(this.self, module.self, codegen, &outMessage, &buffer) != 0; | |
| 1688 | 4240 | if outMessage != nil<string> { | |
| 1689 | ✗ | LLVMDisposeMessage(outMessage); | |
| 1690 | ✗ | } | |
| 1691 | 4240 | if errorOccurred { | |
| 1692 | ✗ | return true; | |
| 1693 | } | ||
| 1694 | // The buffer is null-terminated (LLVM copies the emitted data into a null-terminated memory buffer) | ||
| 1695 | 4240 | const string bufferStart = LLVMGetBufferStart(buffer); | |
| 1696 | 4240 | output = String(bufferStart); | |
| 1697 | 4240 | LLVMDisposeMemoryBuffer(buffer); | |
| 1698 | 4240 | return false; | |
| 1699 | 4240 | } | |
| 1700 | |||
| 1701 | 5673 | public f<DataLayout> TargetMachine.createDataLayout() { | |
| 1702 | 5673 | return DataLayout{ LLVMCreateTargetDataLayout(this.self) }; | |
| 1703 | } | ||
| 1704 | |||
| 1705 | // ===== Target ===== | ||
| 1706 | public type Target struct { | ||
| 1707 | LLVMTargetRef self | ||
| 1708 | } | ||
| 1709 | |||
| 1710 | 5018 | public f<TargetMachine> Target.createTargetMachine(Triple triple, string cpu, string features, LLVMCodeGenOptLevel optLevel, LLVMRelocMode relocMode, LLVMCodeModel codeModel) { | |
| 1711 | 10036 | return TargetMachine{ LLVMCreateTargetMachine(this.self, triple.value, cpu, features, optLevel, relocMode, codeModel) }; | |
| 1712 | 5018 | } | |
| 1713 | |||
| 1714 | // ===== PassBuilderOptions ===== | ||
| 1715 | public type PassBuilderOptions struct { | ||
| 1716 | LLVMPassBuilderOptionsRef internalOptions | ||
| 1717 | } | ||
| 1718 | |||
| 1719 | 4414 | public p PassBuilderOptions.ctor() { | |
| 1720 | 4414 | this.internalOptions = LLVMCreatePassBuilderOptions(); | |
| 1721 | 4414 | } | |
| 1722 | |||
| 1723 | 4414 | public p PassBuilderOptions.dtor() { | |
| 1724 | 4414 | LLVMDisposePassBuilderOptions(this.internalOptions); | |
| 1725 | 4414 | } | |
| 1726 | |||
| 1727 | 4413 | public p PassBuilderOptions.setVerifyEach(bool enabled) { | |
| 1728 | 4413 | LLVMPassBuilderOptionsSetVerifyEach(this.internalOptions, cast<LLVMBool>(enabled)); | |
| 1729 | 4413 | } | |
| 1730 | |||
| 1731 | ✗ | public p PassBuilderOptions.setDebugLogging(bool enabled) { | |
| 1732 | ✗ | LLVMPassBuilderOptionsSetDebugLogging(this.internalOptions, cast<LLVMBool>(enabled)); | |
| 1733 | ✗ | } | |
| 1734 | |||
| 1735 | ✗ | public p PassBuilderOptions.setLoopInterleaving(bool enabled) { | |
| 1736 | ✗ | LLVMPassBuilderOptionsSetLoopInterleaving(this.internalOptions, cast<LLVMBool>(enabled)); | |
| 1737 | ✗ | } | |
| 1738 | |||
| 1739 | ✗ | public p PassBuilderOptions.setLoopVectorization(bool enabled) { | |
| 1740 | ✗ | LLVMPassBuilderOptionsSetLoopVectorization(this.internalOptions, cast<LLVMBool>(enabled)); | |
| 1741 | ✗ | } | |
| 1742 | |||
| 1743 | ✗ | public p PassBuilderOptions.setSLPVectorization(bool enabled) { | |
| 1744 | ✗ | LLVMPassBuilderOptionsSetSLPVectorization(this.internalOptions, cast<LLVMBool>(enabled)); | |
| 1745 | ✗ | } | |
| 1746 | |||
| 1747 | ✗ | public p PassBuilderOptions.setLoopUnrolling(bool enabled) { | |
| 1748 | ✗ | LLVMPassBuilderOptionsSetLoopUnrolling(this.internalOptions, cast<LLVMBool>(enabled)); | |
| 1749 | ✗ | } | |
| 1750 | |||
| 1751 | ✗ | public p PassBuilderOptions.setLicmMssaNoAccForPromotionCap(unsigned int licmMssaNoAccForPromotionCap) { | |
| 1752 | ✗ | LLVMPassBuilderOptionsSetLicmMssaNoAccForPromotionCap(this.internalOptions, licmMssaNoAccForPromotionCap); | |
| 1753 | ✗ | } | |
| 1754 | |||
| 1755 | ✗ | public p PassBuilderOptions.setCallGraphProfile(bool enabled) { | |
| 1756 | ✗ | LLVMPassBuilderOptionsSetCallGraphProfile(this.internalOptions, cast<LLVMBool>(enabled)); | |
| 1757 | ✗ | } | |
| 1758 | |||
| 1759 | ✗ | public p PassBuilderOptions.setMergeFunctions(bool enabled) { | |
| 1760 | ✗ | LLVMPassBuilderOptionsSetMergeFunctions(this.internalOptions, cast<LLVMBool>(enabled)); | |
| 1761 | ✗ | } | |
| 1762 | |||
| 1763 | ✗ | public p PassBuilderOptions.setInlinerThreshold(int threshold) { | |
| 1764 | ✗ | LLVMPassBuilderOptionsSetInlinerThreshold(this.internalOptions, threshold); | |
| 1765 | ✗ | } | |
| 1766 | |||
| 1767 | // ===== PassInfoMixin ===== | ||
| 1768 | public type PassInfo interface { | ||
| 1769 | f<string> getOption(); | ||
| 1770 | } | ||
| 1771 | |||
| 1772 | // ===== AlwaysInlinerPass ===== | ||
| 1773 | 1 | public type AlwaysInlinerPass struct : PassInfo {} | |
| 1774 | |||
| 1775 | 1 | f<string> AlwaysInlinerPass.getOption() { | |
| 1776 | 1 | return "always-inline"; | |
| 1777 | } | ||
| 1778 | |||
| 1779 | // ===== PassBuilder ===== | ||
| 1780 | 4414 | public type PassBuilder struct { | |
| 1781 | LLVMPassBuilderOptionsRef internalOptions | ||
| 1782 | Vector<String> passes | ||
| 1783 | String pipelineDescription // Textual representation of the passes to run (like for passing to opt -passes=...) | ||
| 1784 | } | ||
| 1785 | |||
| 1786 | 4414 | public p PassBuilder.ctor(const PassBuilderOptions& options) { | |
| 1787 | 4414 | this.internalOptions = options.internalOptions; | |
| 1788 | 4414 | } | |
| 1789 | |||
| 1790 | 4414 | public p PassBuilder.addPass(const String& pass) { | |
| 1791 | 4414 | this.passes.pushBack(pass); | |
| 1792 | 4414 | } | |
| 1793 | |||
| 1794 | 174 | public p PassBuilder.addPass(string pass) { | |
| 1795 | 174 | this.passes.pushBack(String(pass)); | |
| 1796 | 174 | } | |
| 1797 | |||
| 1798 | 1 | public p PassBuilder.addPass(const PassInfo& pass) { | |
| 1799 | 1 | this.passes.pushBack(String(pass.getOption())); | |
| 1800 | 1 | } | |
| 1801 | |||
| 1802 | 1 | public p PassBuilder.clearPasses() { | |
| 1803 | 1 | this.passes.clear(); | |
| 1804 | 1 | } | |
| 1805 | |||
| 1806 | 4414 | public p PassBuilder.buildCustomPipeline() { | |
| 1807 | 4414 | this.pipelineDescription.clear(); | |
| 1808 | 9002 | for unsigned int i = 0; i < this.passes.getSize(); i++ { | |
| 1809 | 4588 | if i > 0 { this.pipelineDescription += ','; } | |
| 1810 | 4588 | this.pipelineDescription += this.passes.get(i); | |
| 1811 | 4588 | } | |
| 1812 | 4414 | } | |
| 1813 | |||
| 1814 | 1 | public p PassBuilder.buildPerModuleDefaultPipeline(OptimizationLevel optLevel) { | |
| 1815 | 1 | this.clearPasses(); | |
| 1816 | 1 | this.addPass("default<" + getOptLevelNameFromOptLevel(optLevel) + ">"); | |
| 1817 | 1 | this.buildCustomPipeline(); | |
| 1818 | 1 | } | |
| 1819 | |||
| 1820 | 4414 | public f<LLVMError> PassBuilder.run(const Module& module, const TargetMachine& targetMachine) { | |
| 1821 | 4414 | LLVMErrorRef error = LLVMRunPasses(module.self, this.pipelineDescription.getRaw(), targetMachine.self, this.internalOptions); | |
| 1822 | 4414 | return LLVMError(error); | |
| 1823 | } | ||
| 1824 | |||
| 1825 | // ===== Static functions ===== | ||
| 1826 | |||
| 1827 | 99551 | public f<Type> getFunctionType(Type returnType, const Vector<Type>& paramTypes, bool isVarArg = false) { | |
| 1828 | unsafe { | ||
| 1829 | 99551 | LLVMTypeRef returnTypeRef = returnType.self; | |
| 1830 | 99551 | LLVMTypeRef* paramTypesRef = cast<LLVMTypeRef*>(paramTypes.getDataPtr()); | |
| 1831 | 99551 | LLVMTypeRef typeRef = LLVMFunctionType(returnTypeRef, paramTypesRef, cast<unsigned int>(paramTypes.getSize()), cast<LLVMBool>(isVarArg)); | |
| 1832 | 99551 | return Type{ typeRef }; | |
| 1833 | } | ||
| 1834 | } | ||
| 1835 | |||
| 1836 | 1 | public f<bool> verifyModule(Module module, Message& outMessage, LLVMVerifierFailureAction action = LLVMVerifierFailureAction::AbortProcessAction) { | |
| 1837 | 1 | string outMessageInternal; | |
| 1838 | 1 | const LLVMBool res = LLVMVerifyModule(module.self, action, &outMessageInternal); | |
| 1839 | 1 | outMessage.set(outMessageInternal); | |
| 1840 | 1 | return res != 0; | |
| 1841 | } | ||
| 1842 | |||
| 1843 | 59392 | public f<bool> verifyFunction(Function function, LLVMVerifierFailureAction action = LLVMVerifierFailureAction::AbortProcessAction) { | |
| 1844 | 59392 | return LLVMVerifyFunction(function.self, action) != 0; | |
| 1845 | } | ||
| 1846 | |||
| 1847 | ✗ | public p viewFunctionCFG(Function function) { | |
| 1848 | ✗ | LLVMViewFunctionCFG(function.self); | |
| 1849 | ✗ | } | |
| 1850 | |||
| 1851 | ✗ | public p viewFunctionCFGOnly(Function function) { | |
| 1852 | ✗ | LLVMViewFunctionCFGOnly(function.self); | |
| 1853 | ✗ | } | |
| 1854 | |||
| 1855 | 1185 | public p initializeNativeTarget() { | |
| 1856 | 1185 | LLVM_InitializeNativeTarget(); | |
| 1857 | 1185 | } | |
| 1858 | |||
| 1859 | 1183 | public p initializeNativeAsmParser() { | |
| 1860 | 1183 | LLVM_InitializeNativeAsmParser(); | |
| 1861 | 1183 | } | |
| 1862 | |||
| 1863 | 1185 | public p initializeNativeAsmPrinter() { | |
| 1864 | 1185 | LLVM_InitializeNativeAsmPrinter(); | |
| 1865 | 1185 | } | |
| 1866 | |||
| 1867 | ✗ | public p initializeNativeDisassembler() { | |
| 1868 | ✗ | LLVM_InitializeNativeDisassembler(); | |
| 1869 | ✗ | } | |
| 1870 | |||
| 1871 | 53 | public p initializeAllTargetInfos() { | |
| 1872 | 53 | LLVM_InitializeAllTargetInfos(); | |
| 1873 | 53 | } | |
| 1874 | |||
| 1875 | 53 | public p initializeAllTargets() { | |
| 1876 | 53 | LLVM_InitializeAllTargets(); | |
| 1877 | 53 | } | |
| 1878 | |||
| 1879 | 53 | public p initializeAllTargetMCs() { | |
| 1880 | 53 | LLVM_InitializeAllTargetMCs(); | |
| 1881 | 53 | } | |
| 1882 | |||
| 1883 | 53 | public p initializeAllAsmParsers() { | |
| 1884 | 53 | LLVM_InitializeAllAsmParsers(); | |
| 1885 | 53 | } | |
| 1886 | |||
| 1887 | 53 | public p initializeAllAsmPrinters() { | |
| 1888 | 53 | LLVM_InitializeAllAsmPrinters(); | |
| 1889 | 53 | } | |
| 1890 | |||
| 1891 | ✗ | public p initializeAllDisassemblers() { | |
| 1892 | ✗ | LLVM_InitializeAllDisassemblers(); | |
| 1893 | ✗ | } | |
| 1894 | |||
| 1895 | 1305 | public f<Triple> getDefaultTargetTriple() { | |
| 1896 | 1305 | return Triple{ LLVMGetDefaultTargetTriple() }; | |
| 1897 | } | ||
| 1898 | |||
| 1899 | 1303 | public f<Triple> normalizeTargetTriple(Triple triple) { | |
| 1900 | 2606 | return Triple{ LLVMNormalizeTargetTriple(triple.value) }; | |
| 1901 | 1303 | } | |
| 1902 | |||
| 1903 | ✗ | public f<heap string> getHostCPUName() { | |
| 1904 | ✗ | return LLVMGetHostCPUName(); | |
| 1905 | } | ||
| 1906 | |||
| 1907 | ✗ | public f<heap string> getHostCPUFeatures() { | |
| 1908 | ✗ | return LLVMGetHostCPUFeatures(); | |
| 1909 | } | ||
| 1910 | |||
| 1911 | /** | ||
| 1912 | * Look up the target for the given triple. If there is no such target, the error message is stored in outError, which | ||
| 1913 | * stays untouched otherwise. So the caller has to initialize it with nil and check it, before using the returned target. | ||
| 1914 | * | ||
| 1915 | * @param triple Target triple | ||
| 1916 | * @param outError Error message, if the target was not found. Without it, a failed lookup is fatal | ||
| 1917 | * @return Target | ||
| 1918 | */ | ||
| 1919 | 5018 | public f<Target> getTargetFromTriple(Triple triple, string* outError) { | |
| 1920 | 5018 | Target target; | |
| 1921 | 5018 | const bool failed = LLVMGetTargetFromTriple(triple.value, &target.self, outError) != 0; | |
| 1922 | // Without an error output, the caller cannot notice the failure | ||
| 1923 | 5018 | assert !failed || outError != nil<string*>; | |
| 1924 | 10036 | return target; | |
| 1925 | 5018 | } | |
| 1926 | |||
| 1927 | 1 | public f<string> getOptLevelNameFromOptLevel(OptimizationLevel optLevel) { | |
| 1928 | 1 | switch optLevel { | |
| 1929 | ✗ | case OptimizationLevel::O0: { return "O0"; } | |
| 1930 | ✗ | case OptimizationLevel::O1: { return "O1"; } | |
| 1931 | 1 | case OptimizationLevel::O2: { return "O2"; } | |
| 1932 | ✗ | case OptimizationLevel::O3: { return "O3"; } | |
| 1933 | ✗ | case OptimizationLevel::Os: { return "Os"; } | |
| 1934 | ✗ | case OptimizationLevel::Oz: { return "Oz"; } | |
| 1935 | } | ||
| 1936 | ✗ | } | |
| 1937 | |||
| 1938 | ✗ | public p parseCommandLineOptions(int argc, const Vector<string>& argv, string overview = "") { | |
| 1939 | unsafe { | ||
| 1940 | ✗ | const string* argvRef = argv.getDataPtr(); | |
| 1941 | ✗ | LLVMParseCommandLineOptions(argc, argvRef, overview); | |
| 1942 | } | ||
| 1943 | ✗ | } | |
| 1944 | |||
| 1945 | 1 | public p getVersion(unsigned int* major, unsigned int* minor, unsigned int* patch) { | |
| 1946 | 1 | LLVMGetVersion(major, minor, patch); | |
| 1947 | 1 | } | |
| 1948 | |||
| 1949 | 1331 | public f<String> getVersionString() { | |
| 1950 | 1331 | unsigned int major = 0; | |
| 1951 | 1331 | unsigned int minor = 0; | |
| 1952 | 1331 | unsigned int patch = 0; | |
| 1953 | 1331 | LLVMGetVersion(&major, &minor, &patch); | |
| 1954 | 1331 | const int length = snprintf(nil<char*>, 0l, "%d.%d.%d", major, minor, patch); | |
| 1955 | 1331 | result = String(length); | |
| 1956 | 1331 | const int actualLength = snprintf(cast<char*>(result.getRaw()), length + 1l, "%d.%d.%d", major, minor, patch); | |
| 1957 | 1331 | assert actualLength == length; | |
| 1958 | 1331 | } | |
| 1959 | |||
| 1960 | 877 | public p llvm_shutdown() { | |
| 1961 | 877 | LLVMShutdown(); | |
| 1962 | 877 | } | |
| 1963 | // ================================================= Extended API ================================================= | ||
| 1964 | // The following wrappers are used by the IR generator of the bootstrap compiler. They mirror the respective parts of | ||
| 1965 | // the LLVM C++ API, as far as the LLVM C API allows. | ||
| 1966 | |||
| 1967 | // Attribute indices, like LLVMAttributeReturnIndex and LLVMAttributeFunctionIndex in the C API | ||
| 1968 | public const unsigned int ATTRIBUTE_RETURN_INDEX = 0; | ||
| 1969 | public const unsigned int ATTRIBUTE_FUNCTION_INDEX = 4294967295u; | ||
| 1970 | |||
| 1971 | // Module flag behaviors (llvm::Module::ModFlagBehavior) | ||
| 1972 | public const int MOD_FLAG_BEHAVIOR_ERROR = 1; | ||
| 1973 | public const int MOD_FLAG_BEHAVIOR_WARNING = 2; | ||
| 1974 | public const int MOD_FLAG_BEHAVIOR_MAX = 7; | ||
| 1975 | public const int MOD_FLAG_BEHAVIOR_MIN = 8; | ||
| 1976 | |||
| 1977 | // Branch weights, used by llvm::MDBuilder::createLikelyBranchWeights and createUnlikelyBranchWeights | ||
| 1978 | public const unsigned int LIKELY_BRANCH_WEIGHT = 1048575; // (1 << 20) - 1 | ||
| 1979 | public const unsigned int UNLIKELY_BRANCH_WEIGHT = 1; | ||
| 1980 | |||
| 1981 | 1386040 | f<unsigned int> getAttrKind(string name) { | |
| 1982 | 1386040 | const unsigned int kind = LLVMGetEnumAttributeKindForName(name, cast<unsigned long>(len(name))); | |
| 1983 | 1386040 | assert kind != 0; // Unknown attribute kind | |
| 1984 | 1386040 | return kind; | |
| 1985 | } | ||
| 1986 | |||
| 1987 | 1247996 | f<LLVMAttributeRef> createEnumAttr(LLVMContextRef ctx, string kindName, unsigned long value = 0l) { | |
| 1988 | 1247996 | return LLVMCreateEnumAttribute(ctx, getAttrKind(kindName), value); | |
| 1989 | } | ||
| 1990 | |||
| 1991 | 104 | f<LLVMAttributeRef> createStringAttr(LLVMContextRef ctx, string key, string value) { | |
| 1992 | 104 | return LLVMCreateStringAttribute(ctx, key, cast<unsigned int>(len(key)), value, cast<unsigned int>(len(value))); | |
| 1993 | } | ||
| 1994 | |||
| 1995 | // ----- Metadata ----- | ||
| 1996 | 85987 | public type Metadata struct { | |
| 1997 | 85987 | LLVMMetadataRef self = nil<LLVMMetadataRef> | |
| 1998 | } | ||
| 1999 | |||
| 2000 | 3804 | public f<bool> Metadata.isNull() { | |
| 2001 | 3804 | return this.self == nil<LLVMMetadataRef>; | |
| 2002 | } | ||
| 2003 | |||
| 2004 | // Like llvm::MDString::get | ||
| 2005 | 13693 | public f<Metadata> getMDString(const LLVMContext& context, const String& value) { | |
| 2006 | 13693 | return Metadata{ LLVMMDStringInContext2(context.self, value.getRaw(), value.getLength()) }; | |
| 2007 | } | ||
| 2008 | |||
| 2009 | // Like llvm::ConstantAsMetadata::get | ||
| 2010 | 14141 | public f<Metadata> getConstantAsMetadata(Value constant) { | |
| 2011 | 14141 | return Metadata{ LLVMValueAsMetadata(constant.self) }; | |
| 2012 | } | ||
| 2013 | |||
| 2014 | // Like llvm::MDNode::get | ||
| 2015 | 13717 | public f<Metadata> getMDNode(const LLVMContext& context, const Vector<Metadata>& operands) { | |
| 2016 | unsafe { | ||
| 2017 | 13717 | LLVMMetadataRef* operandsRef = cast<LLVMMetadataRef*>(operands.getDataPtr()); | |
| 2018 | 13717 | return Metadata{ LLVMMDNodeInContext2(context.self, operandsRef, cast<unsigned long>(operands.getSize())) }; | |
| 2019 | } | ||
| 2020 | } | ||
| 2021 | |||
| 2022 | // Like llvm::DIType::getSizeInBits | ||
| 2023 | 988 | public f<unsigned long> Metadata.getDITypeSizeInBits() { | |
| 2024 | 988 | return LLVMDITypeGetSizeInBits(this.self); | |
| 2025 | } | ||
| 2026 | |||
| 2027 | // Like llvm::DIType::getAlignInBits | ||
| 2028 | 988 | public f<unsigned int> Metadata.getDITypeAlignInBits() { | |
| 2029 | 988 | return LLVMDITypeGetAlignInBits(this.self); | |
| 2030 | } | ||
| 2031 | |||
| 2032 | // Like llvm::GlobalObject::addDebugInfo / setMetadata: attach a metadata node under the given kind | ||
| 2033 | 416 | public p Value.setGlobalMetadata(unsigned int kind, Metadata md) { | |
| 2034 | 416 | LLVMGlobalSetMetadata(this.self, kind, md.self); | |
| 2035 | 416 | } | |
| 2036 | |||
| 2037 | // Like llvm::Function::setSubprogram | ||
| 2038 | 1742 | public p Function.setSubprogram(Metadata subprogram) { | |
| 2039 | 1742 | LLVMSetSubprogram(this.self, subprogram.self); | |
| 2040 | 1742 | } | |
| 2041 | |||
| 2042 | // Like llvm::IRBuilder::SetCurrentDebugLocation | ||
| 2043 | 230194 | public p IRBuilder.setCurrentDebugLocation(Metadata loc) { | |
| 2044 | 230194 | LLVMSetCurrentDebugLocation2(this.self, loc.self); | |
| 2045 | 230194 | } | |
| 2046 | |||
| 2047 | // Like llvm::IRBuilder::getCurrentDebugLocation | ||
| 2048 | 187275 | public f<Metadata> IRBuilder.getCurrentDebugLocation() { | |
| 2049 | 187275 | return Metadata{ LLVMGetCurrentDebugLocation2(this.self) }; | |
| 2050 | } | ||
| 2051 | |||
| 2052 | // ----- Debug info (DIBuilder) ----- | ||
| 2053 | // The compiler's debug info generator mirrors the one of the former host compiler, which used the LLVM C++ DIBuilder. | ||
| 2054 | // The C API represents every debug info node (types, scopes, subprograms, ...) as an LLVMMetadataRef, so the Metadata | ||
| 2055 | // wrapper above is reused for all of them. A few C++-only capabilities have no C-API equivalent (see the notes at the | ||
| 2056 | // respective call sites in debug-info-generator.spice). | ||
| 2057 | |||
| 2058 | // DWARF source languages (LLVMDWARFSourceLanguage ordinals) | ||
| 2059 | public const int DW_SOURCE_LANGUAGE_C_PLUS_PLUS_14 = 32; | ||
| 2060 | // DWARF debug info emission kinds (LLVMDWARFEmissionKind) | ||
| 2061 | public const int DW_EMISSION_FULL = 1; | ||
| 2062 | public const int DW_EMISSION_LINE_TABLES_ONLY = 2; | ||
| 2063 | // DWARF type encodings (llvm::dwarf::DW_ATE_*) | ||
| 2064 | public const unsigned int DW_ATE_BOOLEAN = 2u; | ||
| 2065 | public const unsigned int DW_ATE_FLOAT = 4u; | ||
| 2066 | public const unsigned int DW_ATE_SIGNED = 5u; | ||
| 2067 | public const unsigned int DW_ATE_SIGNED_CHAR = 6u; | ||
| 2068 | public const unsigned int DW_ATE_UNSIGNED = 7u; | ||
| 2069 | public const unsigned int DW_ATE_UNSIGNED_CHAR = 8u; | ||
| 2070 | // DWARF tags (llvm::dwarf::DW_TAG_*) | ||
| 2071 | public const unsigned int DW_TAG_REFERENCE_TYPE = 16u; | ||
| 2072 | public const unsigned int DW_TAG_CONST_TYPE = 38u; | ||
| 2073 | // DWARF expression operations (llvm::dwarf::DW_OP_*) | ||
| 2074 | public const unsigned long DW_OP_DEREF = 6ul; | ||
| 2075 | public const unsigned long DW_OP_PLUS_UCONST = 35ul; | ||
| 2076 | // Debug info flags (LLVMDIFlags) | ||
| 2077 | public const int DI_FLAG_ZERO = 0; | ||
| 2078 | public const int DI_FLAG_PUBLIC = 3; | ||
| 2079 | public const int DI_FLAG_PROTOTYPED = 256; // 1 << 8 | ||
| 2080 | public const int DI_FLAG_TYPE_PASS_BY_VALUE = 4194304; // 1 << 22 | ||
| 2081 | public const int DI_FLAG_TYPE_PASS_BY_REFERENCE = 8388608; // 1 << 23 | ||
| 2082 | public const int DI_FLAG_NON_TRIVIAL = 67108864; // 1 << 26 | ||
| 2083 | |||
| 2084 | public type DIBuilder struct { | ||
| 2085 | 4536 | LLVMDIBuilderRef self = nil<LLVMDIBuilderRef> | |
| 2086 | } | ||
| 2087 | |||
| 2088 | // Creates no builder. Owners only create one, if they emit debug info, but the dtor runs either way | ||
| 2089 | 8822 | public p DIBuilder.ctor() {} | |
| 2090 | |||
| 2091 | 125 | public p DIBuilder.ctor(Module module) { | |
| 2092 | 125 | this.self = LLVMCreateDIBuilder(module.self); | |
| 2093 | 125 | } | |
| 2094 | |||
| 2095 | 4536 | public p DIBuilder.dtor() { | |
| 2096 | 4536 | if this.self != nil<LLVMDIBuilderRef> { | |
| 2097 | 125 | LLVMDisposeDIBuilder(this.self); | |
| 2098 | 125 | } | |
| 2099 | 4536 | } | |
| 2100 | |||
| 2101 | // Like llvm::DIBuilder::finalize | ||
| 2102 | 125 | public p DIBuilder.finalize() { | |
| 2103 | 125 | LLVMDIBuilderFinalize(this.self); | |
| 2104 | 125 | } | |
| 2105 | |||
| 2106 | // Like llvm::DIBuilder::createFile | ||
| 2107 | 250 | public f<Metadata> DIBuilder.createFile(const String& filename, const String& directory) { | |
| 2108 | 250 | return Metadata{ LLVMDIBuilderCreateFile(this.self, filename.getRaw(), filename.getLength(), directory.getRaw(), directory.getLength()) }; | |
| 2109 | } | ||
| 2110 | |||
| 2111 | // Like llvm::DIBuilder::createCompileUnit (C API omits the name-table-kind knob, so it stays at the default) | ||
| 2112 | 125 | public f<Metadata> DIBuilder.createCompileUnit(int sourceLang, Metadata file, const String& producer, bool isOptimized, int emissionKind) { | |
| 2113 | 250 | return Metadata{ LLVMDIBuilderCreateCompileUnit(this.self, sourceLang, file.self, producer.getRaw(), producer.getLength(), | |
| 2114 | 125 | cast<LLVMBool>(isOptimized), "", 0l, 0u, "", 0l, emissionKind, 0u, cast<LLVMBool>(false), cast<LLVMBool>(false), "", 0l, "", 0l) }; | |
| 2115 | } | ||
| 2116 | |||
| 2117 | // Like llvm::DIBuilder::createBasicType | ||
| 2118 | 1133 | public f<Metadata> DIBuilder.createBasicType(const String& name, unsigned long sizeInBits, unsigned int encoding) { | |
| 2119 | 1133 | return Metadata{ LLVMDIBuilderCreateBasicType(this.self, name.getRaw(), name.getLength(), sizeInBits, encoding, DI_FLAG_ZERO) }; | |
| 2120 | } | ||
| 2121 | |||
| 2122 | // Like llvm::DIBuilder::createPointerType | ||
| 2123 | 1864 | public f<Metadata> DIBuilder.createPointerType(Metadata pointeeType, unsigned long sizeInBits, unsigned int alignInBits = 0u) { | |
| 2124 | 1864 | return Metadata{ LLVMDIBuilderCreatePointerType(this.self, pointeeType.self, sizeInBits, alignInBits, 0u, "", 0l) }; | |
| 2125 | } | ||
| 2126 | |||
| 2127 | // Like llvm::DIBuilder::createReferenceType | ||
| 2128 | 1223 | public f<Metadata> DIBuilder.createReferenceType(Metadata referencedType, unsigned long sizeInBits) { | |
| 2129 | // The C API createReferenceType takes no size; the pointer-sized reference is implied by the target data layout | ||
| 2130 | 1223 | return Metadata{ LLVMDIBuilderCreateReferenceType(this.self, DW_TAG_REFERENCE_TYPE, referencedType.self) }; | |
| 2131 | } | ||
| 2132 | |||
| 2133 | // Like llvm::DIBuilder::createArrayType (no subscripts, matching the host's empty subscript array) | ||
| 2134 | 366 | public f<Metadata> DIBuilder.createArrayType(unsigned long size, Metadata itemType) { | |
| 2135 | 366 | return Metadata{ LLVMDIBuilderCreateArrayType(this.self, size, 0u, itemType.self, nil<LLVMMetadataRef*>, 0u) }; | |
| 2136 | } | ||
| 2137 | |||
| 2138 | // Like llvm::DIBuilder::createQualifiedType | ||
| 2139 | 1134 | public f<Metadata> DIBuilder.createQualifiedType(unsigned int tag, Metadata baseType) { | |
| 2140 | 1134 | return Metadata{ LLVMDIBuilderCreateQualifiedType(this.self, tag, baseType.self) }; | |
| 2141 | } | ||
| 2142 | |||
| 2143 | // Like llvm::DIBuilder::createStructType (DerivedFrom and VTableHolder are unused by the host) | ||
| 2144 | 497 | public f<Metadata> DIBuilder.createStructType(Metadata scope, const String& name, Metadata file, unsigned int lineNumber, | |
| 2145 | 497 | unsigned long sizeInBits, unsigned int alignInBits, int flags, const Vector<Metadata>& elements, const String& uniqueId) { | |
| 2146 | unsafe { | ||
| 2147 | 497 | LLVMMetadataRef* elementsRef = elements.isEmpty() ? nil<LLVMMetadataRef*> : cast<LLVMMetadataRef*>(elements.getDataPtr()); | |
| 2148 | 994 | return Metadata{ LLVMDIBuilderCreateStructType(this.self, scope.self, name.getRaw(), name.getLength(), file.self, | |
| 2149 | 497 | lineNumber, sizeInBits, alignInBits, flags, nil<LLVMMetadataRef>, elementsRef, cast<unsigned int>(elements.getSize()), | |
| 2150 | 497 | 0u, nil<LLVMMetadataRef>, uniqueId.getRaw(), uniqueId.getLength()) }; | |
| 2151 | } | ||
| 2152 | } | ||
| 2153 | |||
| 2154 | // Like llvm::DIBuilder::createUnionType | ||
| 2155 | 8 | public f<Metadata> DIBuilder.createUnionType(Metadata scope, const String& name, Metadata file, unsigned int lineNumber, | |
| 2156 | 8 | unsigned long sizeInBits, unsigned int alignInBits, int flags, const Vector<Metadata>& elements, const String& uniqueId) { | |
| 2157 | unsafe { | ||
| 2158 | 8 | LLVMMetadataRef* elementsRef = elements.isEmpty() ? nil<LLVMMetadataRef*> : cast<LLVMMetadataRef*>(elements.getDataPtr()); | |
| 2159 | 16 | return Metadata{ LLVMDIBuilderCreateUnionType(this.self, scope.self, name.getRaw(), name.getLength(), file.self, | |
| 2160 | 8 | lineNumber, sizeInBits, alignInBits, flags, elementsRef, cast<unsigned int>(elements.getSize()), 0u, | |
| 2161 | 8 | uniqueId.getRaw(), uniqueId.getLength()) }; | |
| 2162 | } | ||
| 2163 | } | ||
| 2164 | |||
| 2165 | // Like llvm::DIBuilder::createClassType (used for anonymous lambda capture structs) | ||
| 2166 | 10 | public f<Metadata> DIBuilder.createClassType(Metadata scope, const String& name, Metadata file, unsigned int lineNumber, | |
| 2167 | 10 | unsigned long sizeInBits, unsigned int alignInBits, int flags, const Vector<Metadata>& elements) { | |
| 2168 | unsafe { | ||
| 2169 | 10 | LLVMMetadataRef* elementsRef = elements.isEmpty() ? nil<LLVMMetadataRef*> : cast<LLVMMetadataRef*>(elements.getDataPtr()); | |
| 2170 | 20 | return Metadata{ LLVMDIBuilderCreateClassType(this.self, scope.self, name.getRaw(), name.getLength(), file.self, | |
| 2171 | 10 | lineNumber, sizeInBits, alignInBits, 0l, flags, nil<LLVMMetadataRef>, elementsRef, | |
| 2172 | 10 | cast<unsigned int>(elements.getSize()), nil<LLVMMetadataRef>, nil<LLVMMetadataRef>, "", 0l) }; | |
| 2173 | } | ||
| 2174 | } | ||
| 2175 | |||
| 2176 | // Like llvm::DIBuilder::createMemberType (host always passes FlagZero here) | ||
| 2177 | 1297 | public f<Metadata> DIBuilder.createMemberType(Metadata scope, const String& name, Metadata file, unsigned int lineNumber, | |
| 2178 | 1297 | unsigned long sizeInBits, unsigned int alignInBits, unsigned long offsetInBits, Metadata diType) { | |
| 2179 | 2594 | return Metadata{ LLVMDIBuilderCreateMemberType(this.self, scope.self, name.getRaw(), name.getLength(), file.self, | |
| 2180 | 1297 | lineNumber, sizeInBits, alignInBits, offsetInBits, DI_FLAG_ZERO, diType.self) }; | |
| 2181 | } | ||
| 2182 | |||
| 2183 | // Like llvm::DIBuilder::createSubroutineType (folds the host's getOrCreateTypeArray call in) | ||
| 2184 | 1742 | public f<Metadata> DIBuilder.createSubroutineType(Metadata file, const Vector<Metadata>& parameterTypes) { | |
| 2185 | unsafe { | ||
| 2186 | 1742 | LLVMMetadataRef* paramsRef = parameterTypes.isEmpty() ? nil<LLVMMetadataRef*> : cast<LLVMMetadataRef*>(parameterTypes.getDataPtr()); | |
| 2187 | 1742 | return Metadata{ LLVMDIBuilderCreateSubroutineType(this.self, file.self, paramsRef, cast<unsigned int>(parameterTypes.getSize()), DI_FLAG_ZERO) }; | |
| 2188 | } | ||
| 2189 | } | ||
| 2190 | |||
| 2191 | // Like llvm::DIBuilder::createFunction. The C API exposes IsLocalToUnit/IsDefinition booleans instead of DISPFlags, so | ||
| 2192 | // the subprogram virtuality flags (SPFlagVirtual/SPFlagPureVirtual) cannot be set here. | ||
| 2193 | 1742 | public f<Metadata> DIBuilder.createFunction(Metadata scope, const String& name, const String& linkageName, Metadata file, | |
| 2194 | 1742 | unsigned int lineNo, Metadata diType, bool isLocalToUnit, bool isDefinition, unsigned int scopeLine, int flags, bool isOptimized) { | |
| 2195 | 3484 | return Metadata{ LLVMDIBuilderCreateFunction(this.self, scope.self, name.getRaw(), name.getLength(), linkageName.getRaw(), | |
| 2196 | 1742 | linkageName.getLength(), file.self, lineNo, diType.self, cast<LLVMBool>(isLocalToUnit), cast<LLVMBool>(isDefinition), | |
| 2197 | 1742 | scopeLine, flags, cast<LLVMBool>(isOptimized)) }; | |
| 2198 | } | ||
| 2199 | |||
| 2200 | // Like llvm::DIBuilder::createLexicalBlock | ||
| 2201 | 1513 | public f<Metadata> DIBuilder.createLexicalBlock(Metadata scope, Metadata file, unsigned int line, unsigned int column) { | |
| 2202 | 1513 | return Metadata{ LLVMDIBuilderCreateLexicalBlock(this.self, scope.self, file.self, line, column) }; | |
| 2203 | } | ||
| 2204 | |||
| 2205 | // Like llvm::DIBuilder::createParameterVariable | ||
| 2206 | 2263 | public f<Metadata> DIBuilder.createParameterVariable(Metadata scope, const String& name, unsigned int argNo, Metadata file, | |
| 2207 | 2263 | unsigned int lineNo, Metadata diType) { | |
| 2208 | 4526 | return Metadata{ LLVMDIBuilderCreateParameterVariable(this.self, scope.self, name.getRaw(), name.getLength(), argNo, | |
| 2209 | 2263 | file.self, lineNo, diType.self, cast<LLVMBool>(true), DI_FLAG_ZERO) }; | |
| 2210 | } | ||
| 2211 | |||
| 2212 | // Like llvm::DIBuilder::createAutoVariable | ||
| 2213 | 1192 | public f<Metadata> DIBuilder.createAutoVariable(Metadata scope, const String& name, Metadata file, unsigned int lineNo, Metadata diType) { | |
| 2214 | 2384 | return Metadata{ LLVMDIBuilderCreateAutoVariable(this.self, scope.self, name.getRaw(), name.getLength(), file.self, lineNo, | |
| 2215 | 1192 | diType.self, cast<LLVMBool>(true), DI_FLAG_ZERO, 0u) }; | |
| 2216 | } | ||
| 2217 | |||
| 2218 | // Like llvm::DIBuilder::createExpression (empty expression) | ||
| 2219 | 416 | public f<Metadata> DIBuilder.createExpression() { | |
| 2220 | 416 | return Metadata{ LLVMDIBuilderCreateExpression(this.self, nil<unsigned long*>, 0l) }; | |
| 2221 | } | ||
| 2222 | |||
| 2223 | // Like llvm::DIBuilder::createExpression | ||
| 2224 | 3455 | public f<Metadata> DIBuilder.createExpression(const Vector<unsigned long>& addressOps) { | |
| 2225 | unsafe { | ||
| 2226 | 3455 | unsigned long* addressOpsRef = addressOps.isEmpty() ? nil<unsigned long*> : addressOps.getDataPtr(); | |
| 2227 | 3455 | return Metadata{ LLVMDIBuilderCreateExpression(this.self, addressOpsRef, addressOps.getSize()) }; | |
| 2228 | } | ||
| 2229 | } | ||
| 2230 | |||
| 2231 | // Like llvm::DIBuilder::createGlobalVariableExpression | ||
| 2232 | 416 | public f<Metadata> DIBuilder.createGlobalVariableExpression(Metadata scope, const String& name, const String& linkage, | |
| 2233 | 416 | Metadata file, unsigned int lineNo, Metadata diType, bool localToUnit) { | |
| 2234 | 416 | const Metadata expr = this.createExpression(); | |
| 2235 | 832 | return Metadata{ LLVMDIBuilderCreateGlobalVariableExpression(this.self, scope.self, name.getRaw(), name.getLength(), | |
| 2236 | 416 | linkage.getRaw(), linkage.getLength(), file.self, lineNo, diType.self, cast<LLVMBool>(localToUnit), expr.self, | |
| 2237 | 416 | nil<LLVMMetadataRef>, 0u) }; | |
| 2238 | } | ||
| 2239 | |||
| 2240 | // Like llvm::DIBuilder::replaceArrays: replace the element list of a (forward-declared) composite type in place | ||
| 2241 | 514 | public p DIBuilder.replaceArrays(Metadata& composite, const Vector<Metadata>& elements) { | |
| 2242 | unsafe { | ||
| 2243 | 514 | LLVMMetadataRef* elementsRef = elements.isEmpty() ? nil<LLVMMetadataRef*> : cast<LLVMMetadataRef*>(elements.getDataPtr()); | |
| 2244 | 514 | LLVMReplaceArrays(this.self, &composite.self, elementsRef, cast<unsigned int>(elements.getSize())); | |
| 2245 | } | ||
| 2246 | 514 | } | |
| 2247 | |||
| 2248 | // Like llvm::DIBuilder::insertDeclare (new debug-record form): declare a local variable at the end of the given block | ||
| 2249 | 3455 | public p DIBuilder.insertDeclareAtEnd(Value storage, Metadata varInfo, Metadata expr, Metadata debugLoc, BasicBlock block) { | |
| 2250 | 3455 | LLVMDIBuilderInsertDeclareRecordAtEnd(this.self, storage.self, varInfo.self, expr.self, debugLoc.self, block.self); | |
| 2251 | 3455 | } | |
| 2252 | |||
| 2253 | // Like llvm::DILocation::get | ||
| 2254 | 46374 | public f<Metadata> getDebugLocation(const LLVMContext& context, unsigned int line, unsigned int column, Metadata scope) { | |
| 2255 | 46374 | return Metadata{ LLVMDIBuilderCreateDebugLocation(context.self, line, column, scope.self, nil<LLVMMetadataRef>) }; | |
| 2256 | } | ||
| 2257 | |||
| 2258 | // Metadata kind id for the "dbg" attachment (used to attach debug info to global variables) | ||
| 2259 | 416 | public f<unsigned int> getDebugMetadataKindID(const LLVMContext& context) { | |
| 2260 | 416 | return LLVMGetMDKindIDInContext(context.self, "dbg", 3u); | |
| 2261 | } | ||
| 2262 | |||
| 2263 | // ----- Comdat ----- | ||
| 2264 | public type Comdat struct { | ||
| 2265 | LLVMComdatRef self = nil<LLVMComdatRef> | ||
| 2266 | } | ||
| 2267 | |||
| 2268 | // ----- Type ----- | ||
| 2269 | 954615 | public p Type.ctor() { | |
| 2270 | 954615 | this.self = nil<LLVMTypeRef>; | |
| 2271 | 954615 | } | |
| 2272 | |||
| 2273 | 242733 | public f<bool> Type.isNull() { | |
| 2274 | 242733 | return this.self == nil<LLVMTypeRef>; | |
| 2275 | } | ||
| 2276 | |||
| 2277 | 43109 | public f<bool> Type.isSameAs(const Type& other) { | |
| 2278 | 43109 | return this.self == other.self; | |
| 2279 | } | ||
| 2280 | |||
| 2281 | 3191525 | public f<LLVMTypeKind> Type.getKind() { | |
| 2282 | 3191525 | return LLVMGetTypeKind(this.self); | |
| 2283 | } | ||
| 2284 | |||
| 2285 | 1179125 | public f<bool> Type.isPointerTy() { | |
| 2286 | 1179125 | return this.getKind() == LLVMTypeKind::PointerTypeKind; | |
| 2287 | } | ||
| 2288 | |||
| 2289 | 717688 | public f<bool> Type.isIntegerTy() { | |
| 2290 | 717688 | return this.getKind() == LLVMTypeKind::IntegerTypeKind; | |
| 2291 | } | ||
| 2292 | |||
| 2293 | 342321 | public f<bool> Type.isIntegerTy(unsigned int bitWidth) { | |
| 2294 | 342321 | return this.isIntegerTy() && LLVMGetIntTypeWidth(this.self) == bitWidth; | |
| 2295 | } | ||
| 2296 | |||
| 2297 | 147665 | public f<unsigned int> Type.getIntegerBitWidth() { | |
| 2298 | 147665 | return LLVMGetIntTypeWidth(this.self); | |
| 2299 | } | ||
| 2300 | |||
| 2301 | 370 | public f<bool> Type.isDoubleTy() { | |
| 2302 | 370 | return this.getKind() == LLVMTypeKind::DoubleTypeKind; | |
| 2303 | } | ||
| 2304 | |||
| 2305 | 54 | public f<bool> Type.isVoidTy() { | |
| 2306 | 54 | return this.getKind() == LLVMTypeKind::VoidTypeKind; | |
| 2307 | } | ||
| 2308 | |||
| 2309 | 584596 | public f<bool> Type.isArrayTy() { | |
| 2310 | 584596 | return this.getKind() == LLVMTypeKind::ArrayTypeKind; | |
| 2311 | } | ||
| 2312 | |||
| 2313 | 709692 | public f<bool> Type.isStructTy() { | |
| 2314 | 709692 | return this.getKind() == LLVMTypeKind::StructTypeKind; | |
| 2315 | } | ||
| 2316 | |||
| 2317 | 690659 | public f<bool> Type.isAggregateType() { | |
| 2318 | 690659 | return this.isStructTy() || this.isArrayTy(); | |
| 2319 | } | ||
| 2320 | |||
| 2321 | 3541 | public f<unsigned int> Type.getStructNumElements() { | |
| 2322 | 3541 | return LLVMCountStructElementTypes(this.self); | |
| 2323 | } | ||
| 2324 | |||
| 2325 | 86 | public f<unsigned long> Type.getArrayNumElements() { | |
| 2326 | 86 | return LLVMGetArrayLength2(this.self); | |
| 2327 | } | ||
| 2328 | |||
| 2329 | 86 | public f<Type> Type.getArrayElementType() { | |
| 2330 | 86 | return Type{ LLVMGetElementType(this.self) }; | |
| 2331 | } | ||
| 2332 | |||
| 2333 | // Function types | ||
| 2334 | 108 | public f<Type> Type.getReturnType() { | |
| 2335 | 108 | return Type{ LLVMGetReturnType(this.self) }; | |
| 2336 | } | ||
| 2337 | |||
| 2338 | ✗ | public f<unsigned int> Type.getNumParams() { | |
| 2339 | ✗ | return LLVMCountParamTypes(this.self); | |
| 2340 | } | ||
| 2341 | |||
| 2342 | 231 | public f<Vector<Type>> Type.getParamTypes() { | |
| 2343 | 231 | Vector<Type> paramTypes; | |
| 2344 | 231 | const unsigned int paramCount = LLVMCountParamTypes(this.self); | |
| 2345 | 656 | for unsigned int i = 0; i < paramCount; i++ { | |
| 2346 | 425 | paramTypes.pushBack(Type{ nil<LLVMTypeRef> }); | |
| 2347 | 425 | } | |
| 2348 | 231 | if paramCount > 0 { | |
| 2349 | unsafe { | ||
| 2350 | 220 | LLVMGetParamTypes(this.self, cast<LLVMTypeRef*>(paramTypes.getDataPtr())); | |
| 2351 | } | ||
| 2352 | 220 | } | |
| 2353 | 231 | return paramTypes; | |
| 2354 | } | ||
| 2355 | |||
| 2356 | 54 | public f<bool> Type.isFunctionVarArg() { | |
| 2357 | 54 | return LLVMIsFunctionVarArg(this.self) != 0; | |
| 2358 | } | ||
| 2359 | |||
| 2360 | ✗ | public f<Type> getVoidTy(const LLVMContext& context) { | |
| 2361 | ✗ | return Type{ LLVMVoidTypeInContext(context.self) }; | |
| 2362 | } | ||
| 2363 | |||
| 2364 | // ----- Value ----- | ||
| 2365 | 5724144 | public f<bool> Value.isNull() { | |
| 2366 | 5724144 | return this.self == nil<LLVMValueRef>; | |
| 2367 | } | ||
| 2368 | |||
| 2369 | 1390 | public f<bool> Value.isSameAs(const Value& other) { | |
| 2370 | 1390 | return this.self == other.self; | |
| 2371 | } | ||
| 2372 | |||
| 2373 | 1418238 | public f<Type> Value.getType() { | |
| 2374 | 1418238 | return Type{ LLVMTypeOf(this.self) }; | |
| 2375 | } | ||
| 2376 | |||
| 2377 | 57030 | public p Value.setName(string name) { | |
| 2378 | 57030 | LLVMSetValueName2(this.self, name, cast<unsigned long>(len(name))); | |
| 2379 | 57030 | } | |
| 2380 | |||
| 2381 | 439 | public f<String> Value.getName() { | |
| 2382 | 439 | unsigned long length = 0l; | |
| 2383 | 439 | const string name = LLVMGetValueName2(this.self, &length); | |
| 2384 | 439 | return String(name); | |
| 2385 | } | ||
| 2386 | |||
| 2387 | 124997 | public f<Value> Value.getNextInstruction() { | |
| 2388 | 124997 | return Value{ LLVMGetNextInstruction(this.self) }; | |
| 2389 | } | ||
| 2390 | |||
| 2391 | 1529 | public f<bool> Value.isAllocaInst() { | |
| 2392 | 1529 | return LLVMIsAAllocaInst(this.self) != nil<LLVMValueRef>; | |
| 2393 | } | ||
| 2394 | |||
| 2395 | 5310 | public f<bool> Value.isPHINode() { | |
| 2396 | 5310 | return LLVMIsAPHINode(this.self) != nil<LLVMValueRef>; | |
| 2397 | } | ||
| 2398 | |||
| 2399 | 66 | public f<bool> Value.isGlobalVariable() { | |
| 2400 | 66 | return LLVMIsAGlobalVariable(this.self) != nil<LLVMValueRef>; | |
| 2401 | } | ||
| 2402 | |||
| 2403 | 2 | public f<bool> Value.isConstantInt() { | |
| 2404 | 2 | return LLVMIsAConstantInt(this.self) != nil<LLVMValueRef>; | |
| 2405 | } | ||
| 2406 | |||
| 2407 | ✗ | public f<bool> Value.isConstantFP() { | |
| 2408 | ✗ | return LLVMIsAConstantFP(this.self) != nil<LLVMValueRef>; | |
| 2409 | } | ||
| 2410 | |||
| 2411 | // Like llvm::Constant::isNullValue() | ||
| 2412 | 2 | public f<bool> Value.isNullValue() { | |
| 2413 | 2 | return LLVMIsNull(this.self) != 0; | |
| 2414 | } | ||
| 2415 | |||
| 2416 | 2 | public f<unsigned long> Value.getZExtValue() { | |
| 2417 | 2 | return LLVMConstIntGetZExtValue(this.self); | |
| 2418 | } | ||
| 2419 | |||
| 2420 | ✗ | public f<double> Value.getDoubleValue() { | |
| 2421 | ✗ | LLVMBool losesInfo = 0; | |
| 2422 | ✗ | return LLVMConstRealGetDouble(this.self, &losesInfo); | |
| 2423 | } | ||
| 2424 | |||
| 2425 | /** | ||
| 2426 | * Like llvm::Value::stripInBoundsConstantOffsets(), for the subset of values, the IR generator produces: step through | ||
| 2427 | * constant GEP expressions to the underlying base pointer. | ||
| 2428 | */ | ||
| 2429 | 66 | public f<Value> Value.stripInBoundsConstantOffsets() { | |
| 2430 | 66 | LLVMValueRef value = this.self; | |
| 2431 | 66 | while LLVMIsAConstantExpr(value) != nil<LLVMValueRef> && LLVMGetConstOpcode(value) == LLVMOpcode::GetElementPtr { | |
| 2432 | ✗ | value = LLVMGetOperand(value, 0); | |
| 2433 | ✗ | } | |
| 2434 | 66 | return Value{ value }; | |
| 2435 | } | ||
| 2436 | |||
| 2437 | ✗ | public p Value.setVolatile(bool isVolatile) { | |
| 2438 | ✗ | LLVMSetVolatile(this.self, cast<LLVMBool>(isVolatile)); | |
| 2439 | ✗ | } | |
| 2440 | |||
| 2441 | 42730 | public p Value.setAlignment(unsigned int alignment) { | |
| 2442 | 42730 | LLVMSetAlignment(this.self, alignment); | |
| 2443 | 42730 | } | |
| 2444 | |||
| 2445 | 183820 | public p Value.dropLocation() { | |
| 2446 | 183820 | LLVMInstructionSetDebugLoc(this.self, nil<LLVMMetadataRef>); | |
| 2447 | 183820 | } | |
| 2448 | |||
| 2449 | // Global values / global variables | ||
| 2450 | 15664 | public p Value.setLinkage(LLVMLinkage linkage) { | |
| 2451 | 15664 | LLVMSetLinkage(this.self, linkage); | |
| 2452 | 15664 | } | |
| 2453 | |||
| 2454 | 11687 | public p Value.setInitializer(Value initializer) { | |
| 2455 | 11687 | LLVMSetInitializer(this.self, initializer.self); | |
| 2456 | 11687 | } | |
| 2457 | |||
| 2458 | 13716 | public p Value.setConstant(bool isConstant) { | |
| 2459 | 13716 | LLVMSetGlobalConstant(this.self, cast<LLVMBool>(isConstant)); | |
| 2460 | 13716 | } | |
| 2461 | |||
| 2462 | 9 | public f<bool> Value.isConstant() { | |
| 2463 | 9 | return LLVMIsGlobalConstant(this.self) != 0; | |
| 2464 | } | ||
| 2465 | |||
| 2466 | 8228 | public p Value.setUnnamedAddr(LLVMUnnamedAddr unnamedAddr) { | |
| 2467 | 8228 | LLVMSetUnnamedAddress(this.self, unnamedAddr); | |
| 2468 | 8228 | } | |
| 2469 | |||
| 2470 | 343 | public p Value.setThreadLocal(bool isThreadLocal) { | |
| 2471 | 343 | LLVMSetThreadLocal(this.self, cast<LLVMBool>(isThreadLocal)); | |
| 2472 | 343 | } | |
| 2473 | |||
| 2474 | ✗ | public p Value.setDLLStorageClass(LLVMDLLStorageClass storageClass) { | |
| 2475 | ✗ | LLVMSetDLLStorageClass(this.self, storageClass); | |
| 2476 | ✗ | } | |
| 2477 | |||
| 2478 | 5847 | public p Value.setComdat(Comdat comdat) { | |
| 2479 | 5847 | LLVMSetComdat(this.self, comdat.self); | |
| 2480 | 5847 | } | |
| 2481 | |||
| 2482 | 9 | public f<Type> Value.getValueType() { | |
| 2483 | 9 | return Type{ LLVMGlobalGetValueType(this.self) }; | |
| 2484 | } | ||
| 2485 | |||
| 2486 | 23 | public p Value.eraseGlobalFromParent() { | |
| 2487 | 23 | LLVMDeleteGlobal(this.self); | |
| 2488 | 23 | } | |
| 2489 | |||
| 2490 | // Instructions | ||
| 2491 | ✗ | public p Value.setNSW(bool hasNSW) { | |
| 2492 | ✗ | LLVMSetNSW(this.self, cast<LLVMBool>(hasNSW)); | |
| 2493 | ✗ | } | |
| 2494 | |||
| 2495 | 499682 | public p Value.addCallParamAttr(unsigned int argNo, string kindName, unsigned long value = 0l) { | |
| 2496 | 499682 | LLVMContextRef ctx = LLVMGetTypeContext(LLVMTypeOf(this.self)); | |
| 2497 | 499682 | LLVMAddCallSiteAttribute(this.self, argNo + 1, createEnumAttr(ctx, kindName, value)); | |
| 2498 | 499682 | } | |
| 2499 | |||
| 2500 | // Like llvm::CallBase::addDereferenceableParamAttr, which adds nothing for zero bytes | ||
| 2501 | 77997 | public p Value.addCallDereferenceableParamAttr(unsigned int argNo, unsigned long bytes) { | |
| 2502 | 77997 | if bytes != 0l { | |
| 2503 | 77945 | this.addCallParamAttr(argNo, "dereferenceable", bytes); | |
| 2504 | 77945 | } | |
| 2505 | 77997 | } | |
| 2506 | |||
| 2507 | 10336 | public p Value.addCallParamTypeAttr(unsigned int argNo, string kindName, Type ty) { | |
| 2508 | 10336 | LLVMContextRef ctx = LLVMGetTypeContext(ty.self); | |
| 2509 | 10336 | LLVMAddCallSiteAttribute(this.self, argNo + 1, LLVMCreateTypeAttribute(ctx, getAttrKind(kindName), ty.self)); | |
| 2510 | 10336 | } | |
| 2511 | |||
| 2512 | 73972 | public p Value.addCallRetAttr(string kindName) { | |
| 2513 | 73972 | LLVMContextRef ctx = LLVMGetTypeContext(LLVMTypeOf(this.self)); | |
| 2514 | 73972 | LLVMAddCallSiteAttribute(this.self, ATTRIBUTE_RETURN_INDEX, createEnumAttr(ctx, kindName)); | |
| 2515 | 73972 | } | |
| 2516 | |||
| 2517 | // Switch and phi instructions | ||
| 2518 | 593 | public p Value.addCase(Value onValue, BasicBlock dest) { | |
| 2519 | 593 | LLVMAddCase(this.self, onValue.self, dest.self); | |
| 2520 | 593 | } | |
| 2521 | |||
| 2522 | 30070 | public p Value.addIncoming(Value value, BasicBlock block) { | |
| 2523 | 30070 | LLVMValueRef valueRef = value.self; | |
| 2524 | 30070 | LLVMBasicBlockRef blockRef = block.self; | |
| 2525 | 30070 | LLVMAddIncoming(this.self, &valueRef, &blockRef, 1); | |
| 2526 | 30070 | } | |
| 2527 | |||
| 2528 | // Like llvm::Instruction::setMetadata with the kind given by its name (e.g. 'prof' for LLVMContext::MD_prof) | ||
| 2529 | 4871 | public p Value.setMetadata(string kindName, Metadata node) { | |
| 2530 | 4871 | LLVMContextRef ctx = LLVMGetTypeContext(LLVMTypeOf(this.self)); | |
| 2531 | 4871 | const unsigned int kindId = LLVMGetMDKindIDInContext(ctx, kindName, cast<unsigned int>(len(kindName))); | |
| 2532 | 4871 | LLVMSetMetadata(this.self, kindId, LLVMMetadataAsValue(ctx, node.self)); | |
| 2533 | 4871 | } | |
| 2534 | |||
| 2535 | ✗ | public f<Function> Value.asFunction() { | |
| 2536 | ✗ | assert LLVMIsAFunction(this.self) != nil<LLVMValueRef>; | |
| 2537 | ✗ | return Function{ this.self, Type{ LLVMGlobalGetValueType(this.self) } }; | |
| 2538 | } | ||
| 2539 | |||
| 2540 | // Constants | ||
| 2541 | 321287 | public f<Value> getConstInt(Type ty, unsigned long value, bool isSigned = false) { | |
| 2542 | 321287 | return Value{ LLVMConstInt(ty.self, value, cast<LLVMBool>(isSigned)) }; | |
| 2543 | } | ||
| 2544 | |||
| 2545 | 2543 | public f<Value> getConstDouble(Type ty, double value) { | |
| 2546 | 2543 | return Value{ LLVMConstReal(ty.self, value) }; | |
| 2547 | } | ||
| 2548 | |||
| 2549 | 20970 | public f<Value> getNullValue(Type ty) { | |
| 2550 | 20970 | return Value{ LLVMConstNull(ty.self) }; | |
| 2551 | } | ||
| 2552 | |||
| 2553 | 2578 | public f<Value> getConstArray(Type elementType, const Vector<Value>& values) { | |
| 2554 | unsafe { | ||
| 2555 | 2578 | LLVMValueRef* valuesRef = cast<LLVMValueRef*>(values.getDataPtr()); | |
| 2556 | 2578 | return Value{ LLVMConstArray2(elementType.self, valuesRef, cast<unsigned long>(values.getSize())) }; | |
| 2557 | } | ||
| 2558 | } | ||
| 2559 | |||
| 2560 | // Like llvm::ConstantStruct::get for named struct types | ||
| 2561 | 5369 | public f<Value> getConstNamedStruct(Type structType, const Vector<Value>& values) { | |
| 2562 | unsafe { | ||
| 2563 | 5369 | LLVMValueRef* valuesRef = cast<LLVMValueRef*>(values.getDataPtr()); | |
| 2564 | 5369 | return Value{ LLVMConstNamedStruct(structType.self, valuesRef, cast<unsigned int>(values.getSize())) }; | |
| 2565 | } | ||
| 2566 | } | ||
| 2567 | |||
| 2568 | // Like llvm::ConstantStruct::get for literal struct types | ||
| 2569 | ✗ | public f<Value> getConstStruct(const LLVMContext& context, const Vector<Value>& values, bool packed = false) { | |
| 2570 | unsafe { | ||
| 2571 | ✗ | LLVMValueRef* valuesRef = cast<LLVMValueRef*>(values.getDataPtr()); | |
| 2572 | ✗ | return Value{ LLVMConstStructInContext(context.self, valuesRef, cast<unsigned int>(values.getSize()), cast<LLVMBool>(packed)) }; | |
| 2573 | } | ||
| 2574 | } | ||
| 2575 | |||
| 2576 | // Like llvm::ConstantDataArray::getString | ||
| 2577 | ✗ | public f<Value> getConstString(const LLVMContext& context, const String& value, bool addNull = true) { | |
| 2578 | ✗ | return Value{ LLVMConstStringInContext2(context.self, value.getRaw(), value.getLength(), cast<LLVMBool>(!addNull)) }; | |
| 2579 | } | ||
| 2580 | |||
| 2581 | // Like llvm::ConstantDataArray::get for a raw byte buffer | ||
| 2582 | 2 | public f<Value> getConstByteArray(const LLVMContext& context, const Vector<char>& bytes) { | |
| 2583 | unsafe { | ||
| 2584 | 2 | const string data = cast<string>(bytes.getDataPtr()); | |
| 2585 | 2 | return Value{ LLVMConstStringInContext2(context.self, data, bytes.getSize(), 1) }; | |
| 2586 | } | ||
| 2587 | } | ||
| 2588 | |||
| 2589 | // Like llvm::ConstantExpr::getInBoundsGetElementPtr | ||
| 2590 | 1839 | public f<Value> getConstInBoundsGEP(Type ty, Value ptr, const Vector<Value>& indices) { | |
| 2591 | unsafe { | ||
| 2592 | 1839 | LLVMValueRef* indicesRef = cast<LLVMValueRef*>(indices.getDataPtr()); | |
| 2593 | 1839 | return Value{ LLVMConstInBoundsGEP2(ty.self, ptr.self, indicesRef, cast<unsigned int>(indices.getSize())) }; | |
| 2594 | } | ||
| 2595 | } | ||
| 2596 | |||
| 2597 | // Like llvm::InlineAsm::get | ||
| 2598 | 79 | public f<Value> getInlineAsm(Type fctType, const String& asmString, const String& constraints, bool hasSideEffects) { | |
| 2599 | 79 | return Value{ LLVMGetInlineAsm(fctType.self, asmString.getRaw(), asmString.getLength(), constraints.getRaw(), constraints.getLength(), cast<LLVMBool>(hasSideEffects), 0, LLVMInlineAsmDialect::ATT, 0) }; | |
| 2600 | } | ||
| 2601 | |||
| 2602 | // ----- BasicBlock ----- | ||
| 2603 | 46701 | public p BasicBlock.ctor() { | |
| 2604 | 46701 | this.self = nil<LLVMBasicBlockRef>; | |
| 2605 | 46701 | } | |
| 2606 | |||
| 2607 | 1406 | public f<bool> BasicBlock.isNull() { | |
| 2608 | 1406 | return this.self == nil<LLVMBasicBlockRef>; | |
| 2609 | } | ||
| 2610 | |||
| 2611 | ✗ | public f<bool> BasicBlock.isSameAs(const BasicBlock& other) { | |
| 2612 | ✗ | return this.self == other.self; | |
| 2613 | } | ||
| 2614 | |||
| 2615 | 233884 | public f<bool> BasicBlock.hasParent() { | |
| 2616 | 233884 | return LLVMGetBasicBlockParent(this.self) != nil<LLVMValueRef>; | |
| 2617 | } | ||
| 2618 | |||
| 2619 | 290452 | public f<Function> BasicBlock.getParent() { | |
| 2620 | 290452 | LLVMValueRef fctRef = LLVMGetBasicBlockParent(this.self); | |
| 2621 | 290452 | return Function{ fctRef, Type{ LLVMGlobalGetValueType(fctRef) } }; | |
| 2622 | } | ||
| 2623 | |||
| 2624 | 58823 | public f<Value> BasicBlock.getFirstInstruction() { | |
| 2625 | 58823 | return Value{ LLVMGetFirstInstruction(this.self) }; | |
| 2626 | } | ||
| 2627 | |||
| 2628 | // ----- Function ----- | ||
| 2629 | 177751 | public p Function.ctor() { | |
| 2630 | 177751 | this.self = nil<LLVMValueRef>; | |
| 2631 | 177751 | this.fctType = Type(); | |
| 2632 | 177751 | } | |
| 2633 | |||
| 2634 | 606199 | public f<bool> Function.isNull() { | |
| 2635 | 606199 | return this.self == nil<LLVMValueRef>; | |
| 2636 | } | ||
| 2637 | |||
| 2638 | 173166 | public f<Value> Function.asValue() { | |
| 2639 | 173166 | return Value{ this.self }; | |
| 2640 | } | ||
| 2641 | |||
| 2642 | 93051 | public f<Type> Function.getFunctionType() { | |
| 2643 | 93051 | return this.fctType; | |
| 2644 | } | ||
| 2645 | |||
| 2646 | 43117 | public f<Type> Function.getReturnType() { | |
| 2647 | 43117 | return Type{ LLVMGetReturnType(this.fctType.self) }; | |
| 2648 | } | ||
| 2649 | |||
| 2650 | 233812 | public f<unsigned int> Function.argSize() { | |
| 2651 | 233812 | return LLVMCountParams(this.self); | |
| 2652 | } | ||
| 2653 | |||
| 2654 | 120705 | public f<Value> Function.getArg(unsigned int argNo) { | |
| 2655 | 120705 | return Value{ LLVMGetParam(this.self, argNo) }; | |
| 2656 | } | ||
| 2657 | |||
| 2658 | 58387 | public f<bool> Function.isEmpty() { | |
| 2659 | 58387 | return LLVMCountBasicBlocks(this.self) == 0; | |
| 2660 | } | ||
| 2661 | |||
| 2662 | 85 | public f<String> Function.getName() { | |
| 2663 | 85 | unsigned long length = 0l; | |
| 2664 | 85 | return String(LLVMGetValueName2(this.self, &length)); | |
| 2665 | } | ||
| 2666 | |||
| 2667 | ✗ | public p Function.setDLLStorageClass(LLVMDLLStorageClass storageClass) { | |
| 2668 | ✗ | LLVMSetDLLStorageClass(this.self, storageClass); | |
| 2669 | ✗ | } | |
| 2670 | |||
| 2671 | 11367 | public p Function.setUnnamedAddr(LLVMUnnamedAddr unnamedAddr) { | |
| 2672 | 11367 | LLVMSetUnnamedAddress(this.self, unnamedAddr); | |
| 2673 | 11367 | } | |
| 2674 | |||
| 2675 | 685025 | f<LLVMContextRef> Function.getContextRef() { | |
| 2676 | 685025 | return LLVMGetTypeContext(this.fctType.self); | |
| 2677 | } | ||
| 2678 | |||
| 2679 | 280633 | public p Function.addFnAttr(string kindName, unsigned long value = 0l) { | |
| 2680 | 280633 | LLVMAddAttributeAtIndex(this.self, ATTRIBUTE_FUNCTION_INDEX, createEnumAttr(this.getContextRef(), kindName, value)); | |
| 2681 | 280633 | } | |
| 2682 | |||
| 2683 | 104 | public p Function.addFnStringAttr(string key, string value) { | |
| 2684 | 104 | LLVMAddAttributeAtIndex(this.self, ATTRIBUTE_FUNCTION_INDEX, createStringAttr(this.getContextRef(), key, value)); | |
| 2685 | 104 | } | |
| 2686 | |||
| 2687 | 333612 | public p Function.addParamAttr(unsigned int argNo, string kindName, unsigned long value = 0l) { | |
| 2688 | 333612 | LLVMAddAttributeAtIndex(this.self, argNo + 1, createEnumAttr(this.getContextRef(), kindName, value)); | |
| 2689 | 333612 | } | |
| 2690 | |||
| 2691 | // Like llvm::Function::addDereferenceableParamAttr, which adds nothing for zero bytes | ||
| 2692 | 37606 | public p Function.addDereferenceableParamAttr(unsigned int argNo, unsigned long bytes) { | |
| 2693 | 37606 | if bytes != 0l { | |
| 2694 | 37586 | this.addParamAttr(argNo, "dereferenceable", bytes); | |
| 2695 | 37586 | } | |
| 2696 | 37606 | } | |
| 2697 | |||
| 2698 | 10579 | public p Function.addParamTypeAttr(unsigned int argNo, string kindName, Type ty) { | |
| 2699 | 10579 | LLVMAddAttributeAtIndex(this.self, argNo + 1, LLVMCreateTypeAttribute(this.getContextRef(), getAttrKind(kindName), ty.self)); | |
| 2700 | 10579 | } | |
| 2701 | |||
| 2702 | 118229 | public f<bool> Function.hasParamAttr(unsigned int argNo, string kindName) { | |
| 2703 | 118229 | return argNo < this.argSize() && LLVMGetEnumAttributeAtIndex(this.self, argNo + 1, getAttrKind(kindName)) != nil<LLVMAttributeRef>; | |
| 2704 | } | ||
| 2705 | |||
| 2706 | // Get the type of a type attribute (e.g. sret) of the given parameter | ||
| 2707 | 1 | public f<Type> Function.getParamTypeAttr(unsigned int argNo, string kindName) { | |
| 2708 | 1 | LLVMAttributeRef attr = LLVMGetEnumAttributeAtIndex(this.self, argNo + 1, getAttrKind(kindName)); | |
| 2709 | 1 | assert attr != nil<LLVMAttributeRef>; | |
| 2710 | 1 | return Type{ LLVMGetTypeAttributeValue(attr) }; | |
| 2711 | } | ||
| 2712 | |||
| 2713 | 60096 | public p Function.addRetAttr(string kindName) { | |
| 2714 | 60096 | LLVMAddAttributeAtIndex(this.self, ATTRIBUTE_RETURN_INDEX, createEnumAttr(this.getContextRef(), kindName)); | |
| 2715 | 60096 | } | |
| 2716 | |||
| 2717 | // ----- Module ----- | ||
| 2718 | 381339 | public f<Function> Module.getFunction(string name) { | |
| 2719 | 381339 | LLVMValueRef fctRef = LLVMGetNamedFunction(this.self, name); | |
| 2720 | 381339 | if fctRef == nil<LLVMValueRef> { | |
| 2721 | 34912 | return Function{ nil<LLVMValueRef>, Type{ nil<LLVMTypeRef> } }; | |
| 2722 | } | ||
| 2723 | 346427 | return Function{ fctRef, Type{ LLVMGlobalGetValueType(fctRef) } }; | |
| 2724 | } | ||
| 2725 | |||
| 2726 | // Like llvm::Function::Create | ||
| 2727 | 880 | public f<Function> Module.createFunction(Type fctType, LLVMLinkage linkage, string name) { | |
| 2728 | 880 | LLVMValueRef fctRef = LLVMAddFunction(this.self, name, fctType.self); | |
| 2729 | 880 | LLVMSetLinkage(fctRef, linkage); | |
| 2730 | 880 | return Function{ fctRef, fctType }; | |
| 2731 | } | ||
| 2732 | |||
| 2733 | 2017074 | public f<Value> Module.getNamedGlobal(string name) { | |
| 2734 | 2017074 | return Value{ LLVMGetNamedGlobal(this.self, name) }; | |
| 2735 | } | ||
| 2736 | |||
| 2737 | 16618 | #[ignoreUnusedReturnValue] | |
| 2738 | 16618 | public f<Value> Module.getOrInsertGlobal(string name, Type ty) { | |
| 2739 | 16618 | LLVMValueRef globalRef = LLVMGetNamedGlobal(this.self, name); | |
| 2740 | 16618 | if globalRef == nil<LLVMValueRef> { | |
| 2741 | 15025 | globalRef = LLVMAddGlobal(this.self, ty.self, name); | |
| 2742 | 15025 | } | |
| 2743 | 16618 | return Value{ globalRef }; | |
| 2744 | } | ||
| 2745 | |||
| 2746 | 343 | public f<Value> Module.addGlobal(Type ty, string name) { | |
| 2747 | 343 | return Value{ LLVMAddGlobal(this.self, ty.self, name) }; | |
| 2748 | } | ||
| 2749 | |||
| 2750 | 5847 | public f<Comdat> Module.getOrInsertComdat(string name) { | |
| 2751 | 5847 | return Comdat{ LLVMGetOrInsertComdat(this.self, name) }; | |
| 2752 | } | ||
| 2753 | |||
| 2754 | /** | ||
| 2755 | * Like llvm::IRBuilder::CreateGlobalString with an explicit module. Unlike LLVMBuildGlobalString, this does not require the | ||
| 2756 | * builder to have an insert point. | ||
| 2757 | */ | ||
| 2758 | 35871 | public f<Value> Module.createGlobalString(const String& value, string name) { | |
| 2759 | 35871 | LLVMContextRef ctx = LLVMGetModuleContext(this.self); | |
| 2760 | 35871 | LLVMValueRef stringConstant = LLVMConstStringInContext2(ctx, value.getRaw(), value.getLength(), 0); | |
| 2761 | 35871 | LLVMValueRef global = LLVMAddGlobal(this.self, LLVMTypeOf(stringConstant), name); | |
| 2762 | 35871 | LLVMSetInitializer(global, stringConstant); | |
| 2763 | 35871 | LLVMSetGlobalConstant(global, 1); | |
| 2764 | 35871 | LLVMSetLinkage(global, LLVMLinkage::PrivateLinkage); | |
| 2765 | 35871 | LLVMSetUnnamedAddress(global, LLVMUnnamedAddr::GlobalUnnamedAddr); | |
| 2766 | 35871 | LLVMSetAlignment(global, 1); | |
| 2767 | 35871 | return Value{ global }; | |
| 2768 | } | ||
| 2769 | |||
| 2770 | // Like llvm::Intrinsic::getOrInsertDeclaration | ||
| 2771 | 5657 | public f<Function> Module.getIntrinsicDeclaration(string name, const Vector<Type>& overloadTypes) { | |
| 2772 | 5657 | const unsigned int id = LLVMLookupIntrinsicID(name, cast<unsigned long>(len(name))); | |
| 2773 | 5657 | assert id != 0; // Unknown intrinsic | |
| 2774 | unsafe { | ||
| 2775 | 5657 | LLVMTypeRef* typesRef = cast<LLVMTypeRef*>(overloadTypes.getDataPtr()); | |
| 2776 | 5657 | LLVMValueRef fctRef = LLVMGetIntrinsicDeclaration(this.self, id, typesRef, cast<unsigned long>(overloadTypes.getSize())); | |
| 2777 | 5657 | return Function{ fctRef, Type{ LLVMGlobalGetValueType(fctRef) } }; | |
| 2778 | } | ||
| 2779 | } | ||
| 2780 | |||
| 2781 | // Like llvm::Module::addModuleFlag with an integer value | ||
| 2782 | 17631 | public p Module.addModuleFlag(int behavior, string key, unsigned int value) { | |
| 2783 | 17631 | LLVMContextRef ctx = LLVMGetModuleContext(this.self); | |
| 2784 | 17631 | LLVMTypeRef int32Ty = LLVMInt32TypeInContext(ctx); | |
| 2785 | LLVMMetadataRef[3] ops = [ | ||
| 2786 | 17631 | LLVMValueAsMetadata(LLVMConstInt(int32Ty, cast<unsigned long>(behavior), 0)), | |
| 2787 | 17631 | LLVMMDStringInContext2(ctx, key, cast<unsigned long>(len(key))), | |
| 2788 | 17631 | LLVMValueAsMetadata(LLVMConstInt(int32Ty, cast<unsigned long>(value), 0)) | |
| 2789 | ]; | ||
| 2790 | 17631 | LLVMMetadataRef node = LLVMMDNodeInContext2(ctx, &ops[0], 3l); | |
| 2791 | 17631 | LLVMAddNamedMetadataOperand(this.self, "llvm.module.flags", LLVMMetadataAsValue(ctx, node)); | |
| 2792 | 17631 | } | |
| 2793 | |||
| 2794 | // Add a node with a single string operand to the given named metadata, e.g. llvm.ident | ||
| 2795 | 4411 | public p Module.addNamedStringMetadata(string name, const String& value) { | |
| 2796 | 4411 | LLVMContextRef ctx = LLVMGetModuleContext(this.self); | |
| 2797 | 4411 | LLVMMetadataRef op = LLVMMDStringInContext2(ctx, value.getRaw(), value.getLength()); | |
| 2798 | 4411 | LLVMMetadataRef node = LLVMMDNodeInContext2(ctx, &op, 1l); | |
| 2799 | 4411 | LLVMAddNamedMetadataOperand(this.self, name, LLVMMetadataAsValue(ctx, node)); | |
| 2800 | 4411 | } | |
| 2801 | |||
| 2802 | 8826 | public f<String> Module.getTargetTripleString() { | |
| 2803 | 8826 | return String(LLVMGetTarget(this.self)); | |
| 2804 | } | ||
| 2805 | |||
| 2806 | 16976 | public p Module.setTargetTripleString(string triple) { | |
| 2807 | 16976 | LLVMSetTarget(this.self, triple); | |
| 2808 | 16976 | } | |
| 2809 | |||
| 2810 | 8824 | public f<String> Module.getDataLayoutString() { | |
| 2811 | 8824 | return String(LLVMGetDataLayoutStr(this.self)); | |
| 2812 | } | ||
| 2813 | |||
| 2814 | 16976 | public p Module.setDataLayoutString(string dataLayout) { | |
| 2815 | 16976 | LLVMSetDataLayout(this.self, dataLayout); | |
| 2816 | 16976 | } | |
| 2817 | |||
| 2818 | 8824 | public f<String> Module.printToString() { | |
| 2819 | 8824 | const Message message = this.print(); | |
| 2820 | 17648 | return String(message.get()); | |
| 2821 | 8824 | } | |
| 2822 | |||
| 2823 | /** | ||
| 2824 | * Verify the module | ||
| 2825 | * | ||
| 2826 | * @param errorMessage Output of the verifier | ||
| 2827 | * @return True if the module is broken | ||
| 2828 | */ | ||
| 2829 | 4411 | public f<bool> Module.verify(String& errorMessage) { | |
| 2830 | 4411 | string outMessage = nil<string>; | |
| 2831 | 4411 | const bool isBroken = LLVMVerifyModule(this.self, LLVMVerifierFailureAction::ReturnStatusAction, &outMessage) != 0; | |
| 2832 | 4411 | if outMessage != nil<string> { | |
| 2833 | 4411 | errorMessage = String(outMessage); | |
| 2834 | 4411 | LLVMDisposeMessage(outMessage); | |
| 2835 | 4411 | } | |
| 2836 | 4411 | return isBroken; | |
| 2837 | } | ||
| 2838 | |||
| 2839 | // Data layout queries, like on llvm::Module::getDataLayout() | ||
| 2840 | 48542 | public f<unsigned long> Module.getTypeAllocSize(Type ty) { | |
| 2841 | 48542 | return LLVMABISizeOfType(LLVMGetModuleDataLayout(this.self), ty.self); | |
| 2842 | } | ||
| 2843 | |||
| 2844 | 121420 | public f<unsigned long> Module.getTypeStoreSize(Type ty) { | |
| 2845 | 121420 | return LLVMStoreSizeOfType(LLVMGetModuleDataLayout(this.self), ty.self); | |
| 2846 | } | ||
| 2847 | |||
| 2848 | 692 | public f<unsigned long> Module.getTypeSizeInBits(Type ty) { | |
| 2849 | 692 | return LLVMSizeOfTypeInBits(LLVMGetModuleDataLayout(this.self), ty.self); | |
| 2850 | } | ||
| 2851 | |||
| 2852 | 161026 | public f<unsigned int> Module.getABITypeAlign(Type ty) { | |
| 2853 | 161026 | return LLVMABIAlignmentOfType(LLVMGetModuleDataLayout(this.self), ty.self); | |
| 2854 | } | ||
| 2855 | |||
| 2856 | 1706 | public f<unsigned int> Module.getPrefTypeAlign(Type ty) { | |
| 2857 | 1706 | return LLVMPreferredAlignmentOfType(LLVMGetModuleDataLayout(this.self), ty.self); | |
| 2858 | } | ||
| 2859 | |||
| 2860 | // Equivalent to getStructLayout(structTy)->getElementOffset(idx) | ||
| 2861 | 11224 | public f<unsigned long> Module.getStructElementOffset(Type structTy, unsigned int idx) { | |
| 2862 | 11224 | return LLVMOffsetOfElement(LLVMGetModuleDataLayout(this.self), structTy.self, idx); | |
| 2863 | } | ||
| 2864 | |||
| 2865 | // Equivalent to getStructLayout(structTy)->getElementContainingOffset(offset) | ||
| 2866 | 4259 | public f<unsigned int> Module.getStructElementContainingOffset(Type structTy, unsigned long offset) { | |
| 2867 | 4259 | return LLVMElementAtOffset(LLVMGetModuleDataLayout(this.self), structTy.self, offset); | |
| 2868 | } | ||
| 2869 | |||
| 2870 | 200 | public f<unsigned int> Module.getPointerSize() { | |
| 2871 | 200 | return LLVMPointerSize(LLVMGetModuleDataLayout(this.self)); | |
| 2872 | } | ||
| 2873 | |||
| 2874 | 2 | public f<bool> Module.isLittleEndian() { | |
| 2875 | 2 | return LLVMByteOrder(LLVMGetModuleDataLayout(this.self)) == 1; | |
| 2876 | } | ||
| 2877 | |||
| 2878 | // ----- IRBuilder ----- | ||
| 2879 | 234058 | public f<Type> IRBuilder.getVoidTy() { | |
| 2880 | 234058 | return Type{ LLVMVoidTypeInContext(this.ctx) }; | |
| 2881 | } | ||
| 2882 | |||
| 2883 | 2543 | public f<Type> IRBuilder.getDoubleTy() { | |
| 2884 | 2543 | return Type{ LLVMDoubleTypeInContext(this.ctx) }; | |
| 2885 | } | ||
| 2886 | |||
| 2887 | // Position the builder before the given instruction and the debug records attached to it. Like | ||
| 2888 | // llvm::IRBuilder::SetInsertPoint(BB, It), this also sets the current debug location to the one of the given instruction | ||
| 2889 | 124336 | public p IRBuilder.setInsertPointBefore(BasicBlock block, Value inst) { | |
| 2890 | 124336 | LLVMPositionBuilderBeforeDbgRecords(this.self, block.self, inst.self); | |
| 2891 | 124336 | } | |
| 2892 | |||
| 2893 | 183820 | public f<Value> IRBuilder.createAlloca(Type ty, string name) { | |
| 2894 | 183820 | return Value{ LLVMBuildAlloca(this.self, ty.self, name) }; | |
| 2895 | } | ||
| 2896 | |||
| 2897 | ✗ | public f<Value> IRBuilder.createInBoundsGEP(Type ty, Value ptr, Value index, string name = "") { | |
| 2898 | ✗ | LLVMValueRef indexRef = index.self; | |
| 2899 | ✗ | return Value{ LLVMBuildInBoundsGEP2(this.self, ty.self, ptr.self, &indexRef, 1, name) }; | |
| 2900 | } | ||
| 2901 | |||
| 2902 | 3343 | public f<Value> IRBuilder.createGEP(Type ty, Value ptr, Value index, string name = "") { | |
| 2903 | 3343 | LLVMValueRef indexRef = index.self; | |
| 2904 | 3343 | return Value{ LLVMBuildGEP2(this.self, ty.self, ptr.self, &indexRef, 1, name) }; | |
| 2905 | } | ||
| 2906 | |||
| 2907 | 4058 | public f<Value> IRBuilder.createNSWAdd(Value lhs, Value rhs, string name = "") { | |
| 2908 | 4058 | return Value{ LLVMBuildNSWAdd(this.self, lhs.self, rhs.self, name) }; | |
| 2909 | } | ||
| 2910 | |||
| 2911 | 2556 | public f<Value> IRBuilder.createNSWSub(Value lhs, Value rhs, string name = "") { | |
| 2912 | 2556 | return Value{ LLVMBuildNSWSub(this.self, lhs.self, rhs.self, name) }; | |
| 2913 | } | ||
| 2914 | |||
| 2915 | 709 | public f<Value> IRBuilder.createNSWMul(Value lhs, Value rhs, string name = "") { | |
| 2916 | 709 | return Value{ LLVMBuildNSWMul(this.self, lhs.self, rhs.self, name) }; | |
| 2917 | } | ||
| 2918 | |||
| 2919 | 17742 | public f<Value> IRBuilder.createIntCast(Value value, Type destTy, bool isSigned, string name = "") { | |
| 2920 | 17742 | return Value{ LLVMBuildIntCast2(this.self, value.self, destTy.self, cast<LLVMBool>(isSigned), name) }; | |
| 2921 | } | ||
| 2922 | |||
| 2923 | 40 | public f<Value> IRBuilder.createZExt(Value value, Type destTy, string name = "") { | |
| 2924 | 40 | return Value{ LLVMBuildZExt(this.self, value.self, destTy.self, name) }; | |
| 2925 | } | ||
| 2926 | |||
| 2927 | ✗ | public f<Value> IRBuilder.createSExt(Value value, Type destTy, string name = "") { | |
| 2928 | ✗ | return Value{ LLVMBuildSExt(this.self, value.self, destTy.self, name) }; | |
| 2929 | } | ||
| 2930 | |||
| 2931 | 3 | public f<Value> IRBuilder.createTrunc(Value value, Type destTy, string name = "") { | |
| 2932 | 3 | return Value{ LLVMBuildTrunc(this.self, value.self, destTy.self, name) }; | |
| 2933 | } | ||
| 2934 | |||
| 2935 | // Like llvm::IRBuilder::CreateZExtOrTrunc | ||
| 2936 | 70 | public f<Value> IRBuilder.createZExtOrTrunc(Value value, Type destTy, string name = "") { | |
| 2937 | 70 | const unsigned int srcWidth = LLVMGetIntTypeWidth(LLVMTypeOf(value.self)); | |
| 2938 | 70 | const unsigned int destWidth = destTy.getIntegerBitWidth(); | |
| 2939 | 70 | if srcWidth < destWidth { return this.createZExt(value, destTy, name); } | |
| 2940 | 46 | if srcWidth > destWidth { return this.createTrunc(value, destTy, name); } | |
| 2941 | 46 | return value; | |
| 2942 | } | ||
| 2943 | |||
| 2944 | // Like llvm::IRBuilder::CreateSExtOrTrunc | ||
| 2945 | 18 | public f<Value> IRBuilder.createSExtOrTrunc(Value value, Type destTy, string name = "") { | |
| 2946 | 18 | const unsigned int srcWidth = LLVMGetIntTypeWidth(LLVMTypeOf(value.self)); | |
| 2947 | 18 | const unsigned int destWidth = destTy.getIntegerBitWidth(); | |
| 2948 | 18 | if srcWidth < destWidth { return this.createSExt(value, destTy, name); } | |
| 2949 | 18 | if srcWidth > destWidth { return this.createTrunc(value, destTy, name); } | |
| 2950 | 18 | return value; | |
| 2951 | } | ||
| 2952 | |||
| 2953 | // Like llvm::IRBuilder::CreateBitCast, which omits no-op casts | ||
| 2954 | ✗ | public f<Value> IRBuilder.createBitCast(Value value, Type destTy, string name = "") { | |
| 2955 | ✗ | if LLVMTypeOf(value.self) == destTy.self { return value; } | |
| 2956 | ✗ | return Value{ LLVMBuildBitCast(this.self, value.self, destTy.self, name) }; | |
| 2957 | } | ||
| 2958 | |||
| 2959 | 53 | public f<Value> IRBuilder.createPtrToInt(Value value, Type destTy, string name = "") { | |
| 2960 | 53 | return Value{ LLVMBuildPtrToInt(this.self, value.self, destTy.self, name) }; | |
| 2961 | } | ||
| 2962 | |||
| 2963 | 22 | public f<Value> IRBuilder.createIntToPtr(Value value, Type destTy, string name = "") { | |
| 2964 | 22 | return Value{ LLVMBuildIntToPtr(this.self, value.self, destTy.self, name) }; | |
| 2965 | } | ||
| 2966 | |||
| 2967 | 512 | public f<Value> IRBuilder.createSIToFP(Value value, Type destTy, string name = "") { | |
| 2968 | 512 | return Value{ LLVMBuildSIToFP(this.self, value.self, destTy.self, name) }; | |
| 2969 | } | ||
| 2970 | |||
| 2971 | 83 | public f<Value> IRBuilder.createUIToFP(Value value, Type destTy, string name = "") { | |
| 2972 | 83 | return Value{ LLVMBuildUIToFP(this.self, value.self, destTy.self, name) }; | |
| 2973 | } | ||
| 2974 | |||
| 2975 | 188 | public f<Value> IRBuilder.createFPToSI(Value value, Type destTy, string name = "") { | |
| 2976 | 188 | return Value{ LLVMBuildFPToSI(this.self, value.self, destTy.self, name) }; | |
| 2977 | } | ||
| 2978 | |||
| 2979 | 12 | public f<Value> IRBuilder.createFPToUI(Value value, Type destTy, string name = "") { | |
| 2980 | 12 | return Value{ LLVMBuildFPToUI(this.self, value.self, destTy.self, name) }; | |
| 2981 | } | ||
| 2982 | |||
| 2983 | 49 | public f<Value> IRBuilder.createSwitch(Value value, BasicBlock defaultDest, unsigned int numCases) { | |
| 2984 | 49 | return Value{ LLVMBuildSwitch(this.self, value.self, defaultDest.self, numCases) }; | |
| 2985 | } | ||
| 2986 | |||
| 2987 | // Call with an explicit function type, e.g. for calls through function pointers or inline assembly | ||
| 2988 | 113630 | #[ignoreUnusedReturnValue] | |
| 2989 | 113630 | public f<Value> IRBuilder.createCall(Type fctType, Value callee, const Vector<Value>& args, string name = "") { | |
| 2990 | unsafe { | ||
| 2991 | 113630 | LLVMValueRef* argsRef = cast<LLVMValueRef*>(args.getDataPtr()); | |
| 2992 | 113630 | return Value{ LLVMBuildCall2(this.self, fctType.self, callee.self, argsRef, cast<unsigned int>(args.getSize()), name) }; | |
| 2993 | } | ||
| 2994 | } | ||
| 2995 | |||
| 2996 | // ===== Tests ===== | ||
| 2997 | |||
| 2998 | // Takes the triple by value, like getTargetFromTriple | ||
| 2999 | 2 | f<bool> isLinuxTriple(Triple triple) { | |
| 3000 | 4 | return triple.getOSName() == "linux"; | |
| 3001 | 2 | } | |
| 3002 | |||
| 3003 | 5 | f<String> getTestArchTypeName(string tripleString) { | |
| 3004 | 5 | const Triple triple = Triple(tripleString); | |
| 3005 | 10 | return triple.getArchTypeName(); | |
| 3006 | 5 | } | |
| 3007 | |||
| 3008 | 8 | f<String> getTestOSTypeName(string tripleString) { | |
| 3009 | 8 | const Triple triple = Triple(tripleString); | |
| 3010 | 16 | return triple.getOSTypeName(); | |
| 3011 | 8 | } | |
| 3012 | |||
| 3013 | 1 | #[test] | |
| 3014 | f<bool> testVersion() { | ||
| 3015 | 1 | unsigned int major = 0; | |
| 3016 | 1 | unsigned int minor = 0; | |
| 3017 | 1 | unsigned int patch = 0; | |
| 3018 | 1 | getVersion(&major, &minor, &patch); | |
| 3019 | 1 | assert major > 0; | |
| 3020 | 1 | const String version = getVersionString(); | |
| 3021 | 1 | assert !version.isEmpty(); | |
| 3022 | 1 | assert version.contains("."); | |
| 3023 | 1 | return true; | |
| 3024 | 1 | } | |
| 3025 | |||
| 3026 | 1 | #[test] | |
| 3027 | f<bool> testTripleCopies() { | ||
| 3028 | 1 | Triple original = Triple(String("x86_64"), String("unknown"), String("linux")); | |
| 3029 | 1 | assert !original.empty(); | |
| 3030 | |||
| 3031 | // Copies own their message and stay valid after the original went out of scope | ||
| 3032 | 1 | Triple copy = original; | |
| 3033 | 1 | assert copy == original; | |
| 3034 | 1 | assert copy.getArchName() == "x86_64"; | |
| 3035 | 1 | Triple survivor; | |
| 3036 | { | ||
| 3037 | 1 | Triple scoped = Triple(String("riscv64"), String("unknown"), String("linux")); | |
| 3038 | 1 | Triple scopedCopy = scoped; // Copy constructor | |
| 3039 | 1 | survivor = scopedCopy; // Assignment | |
| 3040 | 1 | } | |
| 3041 | 1 | assert survivor.getArchName() == "riscv64"; | |
| 3042 | 1 | assert survivor.getOSName() == "linux"; | |
| 3043 | |||
| 3044 | // A by-value parameter is a copy, so the original stays valid after the call | ||
| 3045 | 1 | assert isLinuxTriple(original); | |
| 3046 | 1 | assert isLinuxTriple(original); | |
| 3047 | 1 | assert original.getVendorName() == "unknown"; | |
| 3048 | |||
| 3049 | // Assigning a temporary keeps the assigned value alive after the temporary was destroyed | ||
| 3050 | 1 | Triple assigned; | |
| 3051 | 1 | assert assigned.empty(); | |
| 3052 | 1 | assigned = Triple(String("aarch64"), String("apple"), String("darwin")); | |
| 3053 | 1 | assert !assigned.empty(); | |
| 3054 | 1 | assert assigned.getArchName() == "aarch64"; | |
| 3055 | 1 | assert assigned.getOSName() == "darwin"; | |
| 3056 | |||
| 3057 | // Re-assigning replaces the previous message | ||
| 3058 | 1 | assigned = original; | |
| 3059 | 1 | assert assigned == original; | |
| 3060 | 1 | assert assigned.getOSName() == "linux"; | |
| 3061 | 1 | assigned = assigned; // Self assignment | |
| 3062 | 1 | assert assigned.getOSName() == "linux"; | |
| 3063 | |||
| 3064 | // A triple can be created from its whole string. The message is copied, so the string does not have to outlive the triple | ||
| 3065 | 1 | Triple fromString; | |
| 3066 | { | ||
| 3067 | 1 | String tripleString = String("aarch64-unknown-linux-gnu"); | |
| 3068 | 1 | fromString = Triple(tripleString.getRaw()); | |
| 3069 | 1 | } | |
| 3070 | 1 | assert !fromString.empty(); | |
| 3071 | 1 | assert fromString.getArchName() == "aarch64"; | |
| 3072 | 1 | assert fromString.getVendorName() == "unknown"; | |
| 3073 | 1 | assert fromString.getOSName() == "linux"; | |
| 3074 | |||
| 3075 | // Copying an empty triple stays empty | ||
| 3076 | 1 | Triple empty; | |
| 3077 | 1 | Triple emptyCopy = empty; | |
| 3078 | 1 | assert emptyCopy.empty(); | |
| 3079 | 1 | return true; | |
| 3080 | 1 | } | |
| 3081 | |||
| 3082 | 1 | #[test] | |
| 3083 | f<bool> testTripleTypeNames() { | ||
| 3084 | // The canonical OS and arch names are the ones of llvm::Triple::getOSTypeName / getArchTypeName | ||
| 3085 | 1 | const Triple darwin = Triple("arm64-apple-darwin25.6.0"); | |
| 3086 | 1 | assert darwin.getOSName() == "darwin25.6.0"; | |
| 3087 | 1 | assert darwin.getArchName() == "arm64"; | |
| 3088 | 1 | assert darwin.getOSTypeName() == "darwin"; | |
| 3089 | 1 | assert darwin.getArchTypeName() == "aarch64"; | |
| 3090 | 1 | const Triple macos = Triple("aarch64-apple-macosx15.0"); | |
| 3091 | 1 | assert macos.getOSTypeName() == "macosx"; | |
| 3092 | 1 | assert macos.getArchTypeName() == "aarch64"; | |
| 3093 | 1 | const Triple linux = Triple("x86_64-unknown-linux-gnu"); | |
| 3094 | 1 | assert linux.getOSTypeName() == "linux"; | |
| 3095 | 1 | assert linux.getArchTypeName() == "x86_64"; | |
| 3096 | 1 | const Triple windows = Triple("x86_64-pc-windows-msvc"); | |
| 3097 | 1 | assert windows.getOSTypeName() == "windows"; | |
| 3098 | 1 | const Triple mingw = Triple("x86_64-w64-mingw32"); | |
| 3099 | 1 | assert mingw.getOSTypeName() == "windows"; | |
| 3100 | 1 | const Triple x86 = Triple("i686-pc-linux-gnu"); | |
| 3101 | 1 | assert x86.getArchTypeName() == "i386"; | |
| 3102 | 1 | const Triple wasm = Triple("wasm32-unknown-unknown"); | |
| 3103 | 1 | assert wasm.getOSTypeName() == "unknown"; | |
| 3104 | 1 | assert wasm.getArchTypeName() == "wasm32"; | |
| 3105 | // Architecture aliases | ||
| 3106 | 1 | assert getTestArchTypeName("arm64e-apple-darwin") == "aarch64"; | |
| 3107 | 1 | assert getTestArchTypeName("arm64ec-pc-windows-msvc") == "aarch64"; | |
| 3108 | 1 | assert getTestArchTypeName("arm64_32-apple-watchos") == "aarch64_32"; | |
| 3109 | 1 | assert getTestArchTypeName("aarch64_be-unknown-linux-gnu") == "aarch64_be"; | |
| 3110 | 1 | assert getTestArchTypeName("amd64-unknown-freebsd") == "x86_64"; | |
| 3111 | // Digits, that belong to the OS name, are kept. Version suffixes are stripped | ||
| 3112 | 1 | assert getTestOSTypeName("wasm32-unknown-wasip1") == "wasip1"; | |
| 3113 | 1 | assert getTestOSTypeName("wasm32-unknown-wasi") == "wasi"; | |
| 3114 | 1 | assert getTestOSTypeName("x86_64-scei-ps4") == "ps4"; | |
| 3115 | 1 | assert getTestOSTypeName("x86_64-sie-ps5") == "ps5"; | |
| 3116 | 1 | assert getTestOSTypeName("arm64-apple-ios17.0") == "ios"; | |
| 3117 | // OS aliases | ||
| 3118 | 1 | assert getTestOSTypeName("x86_64-pc-win32") == "windows"; | |
| 3119 | 1 | assert getTestOSTypeName("arm64-apple-visionos1.0") == "xros"; | |
| 3120 | 1 | assert getTestOSTypeName("x86_64-unknown-kfreebsd") == "kfreebsd"; | |
| 3121 | 1 | return true; | |
| 3122 | 1 | } | |
| 3123 | |||
| 3124 | 1 | #[test] | |
| 3125 | f<bool> testBuildOptimizeAndEmitModule() { | ||
| 3126 | 1 | initializeNativeTarget(); | |
| 3127 | 1 | initializeNativeAsmPrinter(); | |
| 3128 | |||
| 3129 | 1 | const Triple targetTriple = getDefaultTargetTriple(); | |
| 3130 | 1 | string error; | |
| 3131 | 1 | Target target = getTargetFromTriple(targetTriple, &error); | |
| 3132 | 1 | TargetMachine targetMachine = target.createTargetMachine(targetTriple, "generic", "", LLVMCodeGenOptLevel::Default, LLVMRelocMode::Default, LLVMCodeModel::Default); | |
| 3133 | |||
| 3134 | 1 | LLVMContext context; | |
| 3135 | 1 | Module module = Module("test", context); | |
| 3136 | 1 | IRBuilder builder = IRBuilder(context); | |
| 3137 | |||
| 3138 | // int main() { printf("Hello, world!\n", 1 + 2); return 0; } | ||
| 3139 | 1 | Vector<Type> argTypes; | |
| 3140 | 1 | Type funcType = getFunctionType(builder.getInt32Ty(), argTypes); | |
| 3141 | 1 | Function func = Function(module, "main", funcType); | |
| 3142 | 1 | func.setLinkage(LLVMLinkage::ExternalLinkage); | |
| 3143 | |||
| 3144 | 1 | BasicBlock entry = BasicBlock(context, ""); | |
| 3145 | 1 | func.pushBack(entry); | |
| 3146 | 1 | builder.setInsertPoint(entry); | |
| 3147 | |||
| 3148 | 1 | Value calcResult = builder.createAdd(builder.getInt32(1), builder.getInt32(2), "calcResult"); | |
| 3149 | 1 | Value helloWorldStr = builder.createGlobalString("Hello, world!\n", "helloWorldStr"); | |
| 3150 | 1 | Vector<Type> printfArgTypes; | |
| 3151 | 1 | printfArgTypes.pushBack(builder.getPtrTy()); | |
| 3152 | 1 | printfArgTypes.pushBack(builder.getInt32Ty()); | |
| 3153 | 1 | Type printfFuncType = getFunctionType(builder.getInt32Ty(), printfArgTypes, true); | |
| 3154 | 1 | Function printfFunc = module.getOrInsertFunction("printf", printfFuncType); | |
| 3155 | 1 | Vector<Value> printfArgs; | |
| 3156 | 1 | printfArgs.pushBack(helloWorldStr); | |
| 3157 | 1 | printfArgs.pushBack(calcResult); | |
| 3158 | 1 | const Value printfCall = builder.createCall(printfFunc, printfArgs); | |
| 3159 | 1 | assert printfCall.self != nil<LLVMValueRef>; | |
| 3160 | 1 | builder.createRet(builder.getInt32(0)); | |
| 3161 | |||
| 3162 | 1 | assert !verifyFunction(func); | |
| 3163 | 1 | Message verifierOutput; | |
| 3164 | 1 | assert !verifyModule(module, verifierOutput); | |
| 3165 | // The printf declaration is no defined function. The builder folded the add, so only the call and the return are left | ||
| 3166 | 1 | assert module.getDefinedFunctionCount() == 1l; | |
| 3167 | 1 | assert module.getInstructionCount() == 2l; | |
| 3168 | |||
| 3169 | 1 | const Message unoptimizedIR = module.print(); | |
| 3170 | 1 | const String unoptimizedIRStr = String(unoptimizedIR.get()); | |
| 3171 | 1 | assert unoptimizedIRStr.contains("@helloWorldStr = private unnamed_addr constant [15 x i8] c\"Hello, world!\\0A\\00\""); | |
| 3172 | 1 | assert unoptimizedIRStr.contains("call i32 (ptr, i32, ...) @printf(ptr @helloWorldStr, i32 3)"); | |
| 3173 | |||
| 3174 | 1 | PassBuilderOptions pto; | |
| 3175 | 1 | PassBuilder passBuilder = PassBuilder(pto); | |
| 3176 | 1 | passBuilder.buildPerModuleDefaultPipeline(OptimizationLevel::O2); | |
| 3177 | 1 | passBuilder.addPass(AlwaysInlinerPass()); | |
| 3178 | 1 | passBuilder.run(module, targetMachine); | |
| 3179 | |||
| 3180 | // The optimizer replaces printf with puts, which keeps the instruction count | ||
| 3181 | 1 | assert module.getDefinedFunctionCount() == 1l; | |
| 3182 | 1 | assert module.getInstructionCount() == 2l; | |
| 3183 | 1 | const Message optimizedIR = module.print(); | |
| 3184 | 1 | const String optimizedIRStr = String(optimizedIR.get()); | |
| 3185 | 1 | assert optimizedIRStr.contains("@puts("); | |
| 3186 | 1 | assert !optimizedIRStr.contains("@printf("); | |
| 3187 | |||
| 3188 | // Emit assembly to memory, so the test does not write files | ||
| 3189 | 1 | String assembly; | |
| 3190 | 1 | assert !targetMachine.emitToString(module, LLVMCodeGenFileType::AssemblyFile, assembly); | |
| 3191 | 1 | assert assembly.contains("main"); | |
| 3192 | |||
| 3193 | // Release the LLVM objects early, the builder and the module before their context. Disposing twice has no effect, so | ||
| 3194 | // the destructors do not dispose them again at the end of the scope | ||
| 3195 | 1 | builder.dispose(); | |
| 3196 | 1 | module.dispose(); | |
| 3197 | 1 | targetMachine.dispose(); | |
| 3198 | 1 | context.dispose(); | |
| 3199 | 1 | return true; | |
| 3200 | 1 | } | |
| 3201 |