blob bb5bef04 (86487B) - Raw
1 /* 2 * Copyright (c) 2015 Andrew Kelley 3 * 4 * This file is part of zig, which is MIT licensed. 5 * See http://opensource.org/licenses/MIT 6 */ 7 8 #ifndef ZIG_ALL_TYPES_HPP 9 #define ZIG_ALL_TYPES_HPP 10 11 #include "list.hpp" 12 #include "buffer.hpp" 13 #include "cache_hash.hpp" 14 #include "zig_llvm.h" 15 #include "hash_map.hpp" 16 #include "errmsg.hpp" 17 #include "bigint.hpp" 18 #include "bigfloat.hpp" 19 #include "target.hpp" 20 #include "tokenizer.hpp" 21 #include "libc_installation.hpp" 22 23 struct AstNode; 24 struct ZigFn; 25 struct Scope; 26 struct ScopeBlock; 27 struct ScopeFnDef; 28 struct ZigType; 29 struct ZigVar; 30 struct ErrorTableEntry; 31 struct BuiltinFnEntry; 32 struct TypeStructField; 33 struct CodeGen; 34 struct ConstExprValue; 35 struct IrInstruction; 36 struct IrInstructionCast; 37 struct IrInstructionAllocaGen; 38 struct IrBasicBlock; 39 struct ScopeDecls; 40 struct ZigWindowsSDK; 41 struct Tld; 42 struct TldExport; 43 struct IrAnalyze; 44 struct ResultLoc; 45 struct ResultLocPeer; 46 struct ResultLocPeerParent; 47 struct ResultLocBitCast; 48 49 enum X64CABIClass { 50 X64CABIClass_Unknown, 51 X64CABIClass_MEMORY, 52 X64CABIClass_INTEGER, 53 X64CABIClass_SSE, 54 }; 55 56 struct IrExecutable { 57 ZigList<IrBasicBlock *> basic_block_list; 58 Buf *name; 59 ZigFn *name_fn; 60 size_t mem_slot_count; 61 size_t next_debug_id; 62 size_t *backward_branch_count; 63 size_t *backward_branch_quota; 64 ZigFn *fn_entry; 65 Buf *c_import_buf; 66 AstNode *source_node; 67 IrExecutable *parent_exec; 68 IrExecutable *source_exec; 69 IrAnalyze *analysis; 70 Scope *begin_scope; 71 ZigList<Tld *> tld_list; 72 73 IrInstruction *coro_handle; 74 IrInstruction *atomic_state_field_ptr; // this one is shared and in the promise 75 IrInstruction *coro_result_ptr_field_ptr; 76 IrInstruction *coro_result_field_ptr; 77 IrInstruction *await_handle_var_ptr; // this one is where we put the one we extracted from the promise 78 IrBasicBlock *coro_early_final; 79 IrBasicBlock *coro_normal_final; 80 IrBasicBlock *coro_suspend_block; 81 IrBasicBlock *coro_final_cleanup_block; 82 ZigVar *coro_allocator_var; 83 84 bool invalid; 85 bool is_inline; 86 bool is_generic_instantiation; 87 }; 88 89 enum OutType { 90 OutTypeUnknown, 91 OutTypeExe, 92 OutTypeLib, 93 OutTypeObj, 94 }; 95 96 enum ConstParentId { 97 ConstParentIdNone, 98 ConstParentIdStruct, 99 ConstParentIdErrUnionCode, 100 ConstParentIdErrUnionPayload, 101 ConstParentIdOptionalPayload, 102 ConstParentIdArray, 103 ConstParentIdUnion, 104 ConstParentIdScalar, 105 }; 106 107 struct ConstParent { 108 ConstParentId id; 109 110 union { 111 struct { 112 ConstExprValue *array_val; 113 size_t elem_index; 114 } p_array; 115 struct { 116 ConstExprValue *struct_val; 117 size_t field_index; 118 } p_struct; 119 struct { 120 ConstExprValue *err_union_val; 121 } p_err_union_code; 122 struct { 123 ConstExprValue *err_union_val; 124 } p_err_union_payload; 125 struct { 126 ConstExprValue *optional_val; 127 } p_optional_payload; 128 struct { 129 ConstExprValue *union_val; 130 } p_union; 131 struct { 132 ConstExprValue *scalar_val; 133 } p_scalar; 134 } data; 135 }; 136 137 struct ConstStructValue { 138 ConstExprValue *fields; 139 }; 140 141 struct ConstUnionValue { 142 BigInt tag; 143 ConstExprValue *payload; 144 }; 145 146 enum ConstArraySpecial { 147 ConstArraySpecialNone, 148 ConstArraySpecialUndef, 149 ConstArraySpecialBuf, 150 }; 151 152 struct ConstArrayValue { 153 ConstArraySpecial special; 154 union { 155 struct { 156 ConstExprValue *elements; 157 } s_none; 158 Buf *s_buf; 159 } data; 160 }; 161 162 enum ConstPtrSpecial { 163 // Enforce explicitly setting this ID by making the zero value invalid. 164 ConstPtrSpecialInvalid, 165 // The pointer is a reference to a single object. 166 ConstPtrSpecialRef, 167 // The pointer points to an element in an underlying array. 168 ConstPtrSpecialBaseArray, 169 // The pointer points to a field in an underlying struct. 170 ConstPtrSpecialBaseStruct, 171 // The pointer points to the error set field of an error union 172 ConstPtrSpecialBaseErrorUnionCode, 173 // The pointer points to the payload field of an error union 174 ConstPtrSpecialBaseErrorUnionPayload, 175 // The pointer points to the payload field of an optional 176 ConstPtrSpecialBaseOptionalPayload, 177 // This means that we did a compile-time pointer reinterpret and we cannot 178 // understand the value of pointee at compile time. However, we will still 179 // emit a binary with a compile time known address. 180 // In this case index is the numeric address value. 181 ConstPtrSpecialHardCodedAddr, 182 // This means that the pointer represents memory of assigning to _. 183 // That is, storing discards the data, and loading is invalid. 184 ConstPtrSpecialDiscard, 185 // This is actually a function. 186 ConstPtrSpecialFunction, 187 // This means the pointer is null. This is only allowed when the type is ?*T. 188 // We use this instead of ConstPtrSpecialHardCodedAddr because often we check 189 // for that value to avoid doing comptime work. 190 // We need the data layout for ConstCastOnly == true 191 // types to be the same, so all optionals of pointer types use x_ptr 192 // instead of x_optional. 193 ConstPtrSpecialNull, 194 }; 195 196 enum ConstPtrMut { 197 // The pointer points to memory that is known at compile time and immutable. 198 ConstPtrMutComptimeConst, 199 // This means that the pointer points to memory used by a comptime variable, 200 // so attempting to write a non-compile-time known value is an error 201 // But the underlying value is allowed to change at compile time. 202 ConstPtrMutComptimeVar, 203 // The pointer points to memory that is known only at runtime. 204 // For example it may point to the initializer value of a variable. 205 ConstPtrMutRuntimeVar, 206 // The pointer points to memory for which it must be inferred whether the 207 // value is comptime known or not. 208 ConstPtrMutInfer, 209 }; 210 211 struct ConstPtrValue { 212 ConstPtrSpecial special; 213 ConstPtrMut mut; 214 215 union { 216 struct { 217 ConstExprValue *pointee; 218 } ref; 219 struct { 220 ConstExprValue *array_val; 221 size_t elem_index; 222 // This helps us preserve the null byte when performing compile-time 223 // concatenation on C strings. 224 bool is_cstr; 225 } base_array; 226 struct { 227 ConstExprValue *struct_val; 228 size_t field_index; 229 } base_struct; 230 struct { 231 ConstExprValue *err_union_val; 232 } base_err_union_code; 233 struct { 234 ConstExprValue *err_union_val; 235 } base_err_union_payload; 236 struct { 237 ConstExprValue *optional_val; 238 } base_optional_payload; 239 struct { 240 uint64_t addr; 241 } hard_coded_addr; 242 struct { 243 ZigFn *fn_entry; 244 } fn; 245 } data; 246 }; 247 248 struct ConstErrValue { 249 ConstExprValue *error_set; 250 ConstExprValue *payload; 251 }; 252 253 struct ConstBoundFnValue { 254 ZigFn *fn; 255 IrInstruction *first_arg; 256 }; 257 258 struct ConstArgTuple { 259 size_t start_index; 260 size_t end_index; 261 }; 262 263 enum ConstValSpecial { 264 ConstValSpecialRuntime, 265 ConstValSpecialStatic, 266 ConstValSpecialUndef, 267 }; 268 269 enum RuntimeHintErrorUnion { 270 RuntimeHintErrorUnionUnknown, 271 RuntimeHintErrorUnionError, 272 RuntimeHintErrorUnionNonError, 273 }; 274 275 enum RuntimeHintOptional { 276 RuntimeHintOptionalUnknown, 277 RuntimeHintOptionalNull, // TODO is this value even possible? if this is the case it might mean the const value is compile time known. 278 RuntimeHintOptionalNonNull, 279 }; 280 281 enum RuntimeHintPtr { 282 RuntimeHintPtrUnknown, 283 RuntimeHintPtrStack, 284 RuntimeHintPtrNonStack, 285 }; 286 287 enum RuntimeHintSliceId { 288 RuntimeHintSliceIdUnknown, 289 RuntimeHintSliceIdLen, 290 }; 291 292 struct RuntimeHintSlice { 293 enum RuntimeHintSliceId id; 294 uint64_t len; 295 }; 296 297 struct ConstGlobalRefs { 298 LLVMValueRef llvm_value; 299 LLVMValueRef llvm_global; 300 uint32_t align; 301 }; 302 303 struct ConstExprValue { 304 ZigType *type; 305 ConstValSpecial special; 306 ConstParent parent; 307 ConstGlobalRefs *global_refs; 308 309 union { 310 // populated if special == ConstValSpecialStatic 311 BigInt x_bigint; 312 BigFloat x_bigfloat; 313 float16_t x_f16; 314 float x_f32; 315 double x_f64; 316 float128_t x_f128; 317 bool x_bool; 318 ConstBoundFnValue x_bound_fn; 319 ZigType *x_type; 320 ConstExprValue *x_optional; 321 ConstErrValue x_err_union; 322 ErrorTableEntry *x_err_set; 323 BigInt x_enum_tag; 324 ConstStructValue x_struct; 325 ConstUnionValue x_union; 326 ConstArrayValue x_array; 327 ConstPtrValue x_ptr; 328 ConstArgTuple x_arg_tuple; 329 Buf *x_enum_literal; 330 331 // populated if special == ConstValSpecialRuntime 332 RuntimeHintErrorUnion rh_error_union; 333 RuntimeHintOptional rh_maybe; 334 RuntimeHintPtr rh_ptr; 335 RuntimeHintSlice rh_slice; 336 } data; 337 338 // uncomment these to find bugs. can't leave them uncommented because of a gcc-9 warning 339 //ConstExprValue(const ConstExprValue &other) = delete; // plz zero initialize with {} 340 //ConstExprValue& operator= (const ConstExprValue &other) = delete; // use copy_const_val 341 }; 342 343 enum ReturnKnowledge { 344 ReturnKnowledgeUnknown, 345 ReturnKnowledgeKnownError, 346 ReturnKnowledgeKnownNonError, 347 ReturnKnowledgeKnownNull, 348 ReturnKnowledgeKnownNonNull, 349 ReturnKnowledgeSkipDefers, 350 }; 351 352 enum VisibMod { 353 VisibModPrivate, 354 VisibModPub, 355 }; 356 357 enum GlobalLinkageId { 358 GlobalLinkageIdInternal, 359 GlobalLinkageIdStrong, 360 GlobalLinkageIdWeak, 361 GlobalLinkageIdLinkOnce, 362 }; 363 364 enum TldId { 365 TldIdVar, 366 TldIdFn, 367 TldIdContainer, 368 TldIdCompTime, 369 }; 370 371 enum TldResolution { 372 TldResolutionUnresolved, 373 TldResolutionResolving, 374 TldResolutionInvalid, 375 TldResolutionOk, 376 }; 377 378 struct Tld { 379 TldId id; 380 Buf *name; 381 VisibMod visib_mod; 382 AstNode *source_node; 383 384 ZigType *import; 385 Scope *parent_scope; 386 TldResolution resolution; 387 }; 388 389 struct TldVar { 390 Tld base; 391 392 ZigVar *var; 393 Buf *extern_lib_name; 394 Buf *section_name; 395 bool analyzing_type; // flag to detect dependency loops 396 }; 397 398 struct TldFn { 399 Tld base; 400 401 ZigFn *fn_entry; 402 Buf *extern_lib_name; 403 }; 404 405 struct TldContainer { 406 Tld base; 407 408 ScopeDecls *decls_scope; 409 ZigType *type_entry; 410 }; 411 412 struct TldCompTime { 413 Tld base; 414 }; 415 416 struct TypeEnumField { 417 Buf *name; 418 BigInt value; 419 uint32_t decl_index; 420 AstNode *decl_node; 421 }; 422 423 struct TypeUnionField { 424 Buf *name; 425 TypeEnumField *enum_field; 426 ZigType *type_entry; 427 AstNode *decl_node; 428 uint32_t gen_index; 429 }; 430 431 enum NodeType { 432 NodeTypeFnProto, 433 NodeTypeFnDef, 434 NodeTypeParamDecl, 435 NodeTypeBlock, 436 NodeTypeGroupedExpr, 437 NodeTypeReturnExpr, 438 NodeTypeDefer, 439 NodeTypeVariableDeclaration, 440 NodeTypeTestDecl, 441 NodeTypeBinOpExpr, 442 NodeTypeCatchExpr, 443 NodeTypeFloatLiteral, 444 NodeTypeIntLiteral, 445 NodeTypeStringLiteral, 446 NodeTypeCharLiteral, 447 NodeTypeSymbol, 448 NodeTypePrefixOpExpr, 449 NodeTypePointerType, 450 NodeTypeFnCallExpr, 451 NodeTypeArrayAccessExpr, 452 NodeTypeSliceExpr, 453 NodeTypeFieldAccessExpr, 454 NodeTypePtrDeref, 455 NodeTypeUnwrapOptional, 456 NodeTypeUse, 457 NodeTypeBoolLiteral, 458 NodeTypeNullLiteral, 459 NodeTypeUndefinedLiteral, 460 NodeTypeUnreachable, 461 NodeTypeIfBoolExpr, 462 NodeTypeWhileExpr, 463 NodeTypeForExpr, 464 NodeTypeSwitchExpr, 465 NodeTypeSwitchProng, 466 NodeTypeSwitchRange, 467 NodeTypeCompTime, 468 NodeTypeBreak, 469 NodeTypeContinue, 470 NodeTypeAsmExpr, 471 NodeTypeContainerDecl, 472 NodeTypeStructField, 473 NodeTypeContainerInitExpr, 474 NodeTypeStructValueField, 475 NodeTypeArrayType, 476 NodeTypeInferredArrayType, 477 NodeTypeErrorType, 478 NodeTypeIfErrorExpr, 479 NodeTypeIfOptional, 480 NodeTypeErrorSetDecl, 481 NodeTypeCancel, 482 NodeTypeResume, 483 NodeTypeAwaitExpr, 484 NodeTypeSuspend, 485 NodeTypePromiseType, 486 NodeTypeEnumLiteral, 487 }; 488 489 enum CallingConvention { 490 CallingConventionUnspecified, 491 CallingConventionC, 492 CallingConventionCold, 493 CallingConventionNaked, 494 CallingConventionStdcall, 495 CallingConventionAsync, 496 }; 497 498 struct AstNodeFnProto { 499 VisibMod visib_mod; 500 Buf *name; 501 ZigList<AstNode *> params; 502 AstNode *return_type; 503 Token *return_var_token; 504 bool is_var_args; 505 bool is_extern; 506 bool is_export; 507 bool is_inline; 508 CallingConvention cc; 509 AstNode *fn_def_node; 510 // populated if this is an extern declaration 511 Buf *lib_name; 512 // populated if the "align A" is present 513 AstNode *align_expr; 514 // populated if the "section(S)" is present 515 AstNode *section_expr; 516 517 bool auto_err_set; 518 AstNode *async_allocator_type; 519 }; 520 521 struct AstNodeFnDef { 522 AstNode *fn_proto; 523 AstNode *body; 524 }; 525 526 struct AstNodeParamDecl { 527 Buf *name; 528 AstNode *type; 529 Token *var_token; 530 bool is_noalias; 531 bool is_inline; 532 bool is_var_args; 533 }; 534 535 struct AstNodeBlock { 536 Buf *name; 537 ZigList<AstNode *> statements; 538 }; 539 540 enum ReturnKind { 541 ReturnKindUnconditional, 542 ReturnKindError, 543 }; 544 545 struct AstNodeReturnExpr { 546 ReturnKind kind; 547 // might be null in case of return void; 548 AstNode *expr; 549 }; 550 551 struct AstNodeDefer { 552 ReturnKind kind; 553 AstNode *expr; 554 555 // temporary data used in IR generation 556 Scope *child_scope; 557 Scope *expr_scope; 558 }; 559 560 struct AstNodeVariableDeclaration { 561 Buf *symbol; 562 // one or both of type and expr will be non null 563 AstNode *type; 564 AstNode *expr; 565 // populated if this is an extern declaration 566 Buf *lib_name; 567 // populated if the "align(A)" is present 568 AstNode *align_expr; 569 // populated if the "section(S)" is present 570 AstNode *section_expr; 571 Token *threadlocal_tok; 572 573 VisibMod visib_mod; 574 bool is_const; 575 bool is_comptime; 576 bool is_export; 577 bool is_extern; 578 }; 579 580 struct AstNodeTestDecl { 581 Buf *name; 582 583 AstNode *body; 584 }; 585 586 enum BinOpType { 587 BinOpTypeInvalid, 588 BinOpTypeAssign, 589 BinOpTypeAssignTimes, 590 BinOpTypeAssignTimesWrap, 591 BinOpTypeAssignDiv, 592 BinOpTypeAssignMod, 593 BinOpTypeAssignPlus, 594 BinOpTypeAssignPlusWrap, 595 BinOpTypeAssignMinus, 596 BinOpTypeAssignMinusWrap, 597 BinOpTypeAssignBitShiftLeft, 598 BinOpTypeAssignBitShiftRight, 599 BinOpTypeAssignBitAnd, 600 BinOpTypeAssignBitXor, 601 BinOpTypeAssignBitOr, 602 BinOpTypeAssignMergeErrorSets, 603 BinOpTypeBoolOr, 604 BinOpTypeBoolAnd, 605 BinOpTypeCmpEq, 606 BinOpTypeCmpNotEq, 607 BinOpTypeCmpLessThan, 608 BinOpTypeCmpGreaterThan, 609 BinOpTypeCmpLessOrEq, 610 BinOpTypeCmpGreaterOrEq, 611 BinOpTypeBinOr, 612 BinOpTypeBinXor, 613 BinOpTypeBinAnd, 614 BinOpTypeBitShiftLeft, 615 BinOpTypeBitShiftRight, 616 BinOpTypeAdd, 617 BinOpTypeAddWrap, 618 BinOpTypeSub, 619 BinOpTypeSubWrap, 620 BinOpTypeMult, 621 BinOpTypeMultWrap, 622 BinOpTypeDiv, 623 BinOpTypeMod, 624 BinOpTypeUnwrapOptional, 625 BinOpTypeArrayCat, 626 BinOpTypeArrayMult, 627 BinOpTypeErrorUnion, 628 BinOpTypeMergeErrorSets, 629 }; 630 631 struct AstNodeBinOpExpr { 632 AstNode *op1; 633 BinOpType bin_op; 634 AstNode *op2; 635 }; 636 637 struct AstNodeCatchExpr { 638 AstNode *op1; 639 AstNode *symbol; // can be null 640 AstNode *op2; 641 }; 642 643 struct AstNodeUnwrapOptional { 644 AstNode *expr; 645 }; 646 647 struct AstNodeFnCallExpr { 648 AstNode *fn_ref_expr; 649 ZigList<AstNode *> params; 650 bool is_builtin; 651 bool is_async; 652 bool seen; // used by @compileLog 653 AstNode *async_allocator; 654 }; 655 656 struct AstNodeArrayAccessExpr { 657 AstNode *array_ref_expr; 658 AstNode *subscript; 659 }; 660 661 struct AstNodeSliceExpr { 662 AstNode *array_ref_expr; 663 AstNode *start; 664 AstNode *end; 665 }; 666 667 struct AstNodeFieldAccessExpr { 668 AstNode *struct_expr; 669 Buf *field_name; 670 }; 671 672 struct AstNodePtrDerefExpr { 673 AstNode *target; 674 }; 675 676 enum PrefixOp { 677 PrefixOpInvalid, 678 PrefixOpBoolNot, 679 PrefixOpBinNot, 680 PrefixOpNegation, 681 PrefixOpNegationWrap, 682 PrefixOpOptional, 683 PrefixOpAddrOf, 684 }; 685 686 struct AstNodePrefixOpExpr { 687 PrefixOp prefix_op; 688 AstNode *primary_expr; 689 }; 690 691 struct AstNodePointerType { 692 Token *star_token; 693 AstNode *align_expr; 694 BigInt *bit_offset_start; 695 BigInt *host_int_bytes; 696 AstNode *op_expr; 697 Token *allow_zero_token; 698 bool is_const; 699 bool is_volatile; 700 }; 701 702 struct AstNodeInferredArrayType { 703 AstNode *child_type; 704 }; 705 706 struct AstNodeArrayType { 707 AstNode *size; 708 AstNode *child_type; 709 AstNode *align_expr; 710 Token *allow_zero_token; 711 bool is_const; 712 bool is_volatile; 713 }; 714 715 struct AstNodeUse { 716 VisibMod visib_mod; 717 AstNode *expr; 718 719 TldResolution resolution; 720 ConstExprValue *using_namespace_value; 721 }; 722 723 struct AstNodeIfBoolExpr { 724 AstNode *condition; 725 AstNode *then_block; 726 AstNode *else_node; // null, block node, or other if expr node 727 }; 728 729 struct AstNodeTryExpr { 730 Buf *var_symbol; 731 bool var_is_ptr; 732 AstNode *target_node; 733 AstNode *then_node; 734 AstNode *else_node; 735 Buf *err_symbol; 736 }; 737 738 struct AstNodeTestExpr { 739 Buf *var_symbol; 740 bool var_is_ptr; 741 AstNode *target_node; 742 AstNode *then_node; 743 AstNode *else_node; // null, block node, or other if expr node 744 }; 745 746 struct AstNodeWhileExpr { 747 Buf *name; 748 AstNode *condition; 749 Buf *var_symbol; 750 bool var_is_ptr; 751 AstNode *continue_expr; 752 AstNode *body; 753 AstNode *else_node; 754 Buf *err_symbol; 755 bool is_inline; 756 }; 757 758 struct AstNodeForExpr { 759 Buf *name; 760 AstNode *array_expr; 761 AstNode *elem_node; // always a symbol 762 AstNode *index_node; // always a symbol, might be null 763 AstNode *body; 764 AstNode *else_node; // can be null 765 bool elem_is_ptr; 766 bool is_inline; 767 }; 768 769 struct AstNodeSwitchExpr { 770 AstNode *expr; 771 ZigList<AstNode *> prongs; 772 }; 773 774 struct AstNodeSwitchProng { 775 ZigList<AstNode *> items; 776 AstNode *var_symbol; 777 AstNode *expr; 778 bool var_is_ptr; 779 bool any_items_are_range; 780 }; 781 782 struct AstNodeSwitchRange { 783 AstNode *start; 784 AstNode *end; 785 }; 786 787 struct AstNodeCompTime { 788 AstNode *expr; 789 }; 790 791 struct AsmOutput { 792 Buf *asm_symbolic_name; 793 Buf *constraint; 794 Buf *variable_name; 795 AstNode *return_type; // null unless "=r" and return 796 }; 797 798 struct AsmInput { 799 Buf *asm_symbolic_name; 800 Buf *constraint; 801 AstNode *expr; 802 }; 803 804 struct SrcPos { 805 size_t line; 806 size_t column; 807 }; 808 809 enum AsmTokenId { 810 AsmTokenIdTemplate, 811 AsmTokenIdPercent, 812 AsmTokenIdVar, 813 AsmTokenIdUniqueId, 814 }; 815 816 struct AsmToken { 817 enum AsmTokenId id; 818 size_t start; 819 size_t end; 820 }; 821 822 struct AstNodeAsmExpr { 823 Token *volatile_token; 824 Token *asm_template; 825 ZigList<AsmOutput*> output_list; 826 ZigList<AsmInput*> input_list; 827 ZigList<Buf*> clobber_list; 828 }; 829 830 enum ContainerKind { 831 ContainerKindStruct, 832 ContainerKindEnum, 833 ContainerKindUnion, 834 }; 835 836 enum ContainerLayout { 837 ContainerLayoutAuto, 838 ContainerLayoutExtern, 839 ContainerLayoutPacked, 840 }; 841 842 struct AstNodeContainerDecl { 843 ContainerKind kind; 844 ZigList<AstNode *> fields; 845 ZigList<AstNode *> decls; 846 ContainerLayout layout; 847 AstNode *init_arg_expr; // enum(T), struct(endianness), or union(T), or union(enum(T)) 848 bool auto_enum, is_root; // union(enum) 849 }; 850 851 struct AstNodeErrorSetDecl { 852 ZigList<AstNode *> decls; 853 }; 854 855 struct AstNodeStructField { 856 VisibMod visib_mod; 857 Buf *name; 858 AstNode *type; 859 AstNode *value; 860 }; 861 862 struct AstNodeStringLiteral { 863 Buf *buf; 864 bool c; 865 }; 866 867 struct AstNodeCharLiteral { 868 uint32_t value; 869 }; 870 871 struct AstNodeFloatLiteral { 872 BigFloat *bigfloat; 873 874 // overflow is true if when parsing the number, we discovered it would not 875 // fit without losing data in a double 876 bool overflow; 877 }; 878 879 struct AstNodeIntLiteral { 880 BigInt *bigint; 881 }; 882 883 struct AstNodeStructValueField { 884 Buf *name; 885 AstNode *expr; 886 }; 887 888 enum ContainerInitKind { 889 ContainerInitKindStruct, 890 ContainerInitKindArray, 891 }; 892 893 struct AstNodeContainerInitExpr { 894 AstNode *type; 895 ZigList<AstNode *> entries; 896 ContainerInitKind kind; 897 }; 898 899 struct AstNodeNullLiteral { 900 }; 901 902 struct AstNodeUndefinedLiteral { 903 }; 904 905 struct AstNodeThisLiteral { 906 }; 907 908 struct AstNodeSymbolExpr { 909 Buf *symbol; 910 }; 911 912 struct AstNodeBoolLiteral { 913 bool value; 914 }; 915 916 struct AstNodeBreakExpr { 917 Buf *name; 918 AstNode *expr; // may be null 919 }; 920 921 struct AstNodeCancelExpr { 922 AstNode *expr; 923 }; 924 925 struct AstNodeResumeExpr { 926 AstNode *expr; 927 }; 928 929 struct AstNodeContinueExpr { 930 Buf *name; 931 }; 932 933 struct AstNodeUnreachableExpr { 934 }; 935 936 937 struct AstNodeErrorType { 938 }; 939 940 struct AstNodeAwaitExpr { 941 AstNode *expr; 942 }; 943 944 struct AstNodeSuspend { 945 AstNode *block; 946 }; 947 948 struct AstNodePromiseType { 949 AstNode *payload_type; // can be NULL 950 }; 951 952 struct AstNodeEnumLiteral { 953 Token *period; 954 Token *identifier; 955 }; 956 957 struct AstNode { 958 enum NodeType type; 959 size_t line; 960 size_t column; 961 ZigType *owner; 962 union { 963 AstNodeFnDef fn_def; 964 AstNodeFnProto fn_proto; 965 AstNodeParamDecl param_decl; 966 AstNodeBlock block; 967 AstNode * grouped_expr; 968 AstNodeReturnExpr return_expr; 969 AstNodeDefer defer; 970 AstNodeVariableDeclaration variable_declaration; 971 AstNodeTestDecl test_decl; 972 AstNodeBinOpExpr bin_op_expr; 973 AstNodeCatchExpr unwrap_err_expr; 974 AstNodeUnwrapOptional unwrap_optional; 975 AstNodePrefixOpExpr prefix_op_expr; 976 AstNodePointerType pointer_type; 977 AstNodeFnCallExpr fn_call_expr; 978 AstNodeArrayAccessExpr array_access_expr; 979 AstNodeSliceExpr slice_expr; 980 AstNodeUse use; 981 AstNodeIfBoolExpr if_bool_expr; 982 AstNodeTryExpr if_err_expr; 983 AstNodeTestExpr test_expr; 984 AstNodeWhileExpr while_expr; 985 AstNodeForExpr for_expr; 986 AstNodeSwitchExpr switch_expr; 987 AstNodeSwitchProng switch_prong; 988 AstNodeSwitchRange switch_range; 989 AstNodeCompTime comptime_expr; 990 AstNodeAsmExpr asm_expr; 991 AstNodeFieldAccessExpr field_access_expr; 992 AstNodePtrDerefExpr ptr_deref_expr; 993 AstNodeContainerDecl container_decl; 994 AstNodeStructField struct_field; 995 AstNodeStringLiteral string_literal; 996 AstNodeCharLiteral char_literal; 997 AstNodeFloatLiteral float_literal; 998 AstNodeIntLiteral int_literal; 999 AstNodeContainerInitExpr container_init_expr; 1000 AstNodeStructValueField struct_val_field; 1001 AstNodeNullLiteral null_literal; 1002 AstNodeUndefinedLiteral undefined_literal; 1003 AstNodeThisLiteral this_literal; 1004 AstNodeSymbolExpr symbol_expr; 1005 AstNodeBoolLiteral bool_literal; 1006 AstNodeBreakExpr break_expr; 1007 AstNodeContinueExpr continue_expr; 1008 AstNodeUnreachableExpr unreachable_expr; 1009 AstNodeArrayType array_type; 1010 AstNodeInferredArrayType inferred_array_type; 1011 AstNodeErrorType error_type; 1012 AstNodeErrorSetDecl err_set_decl; 1013 AstNodeCancelExpr cancel_expr; 1014 AstNodeResumeExpr resume_expr; 1015 AstNodeAwaitExpr await_expr; 1016 AstNodeSuspend suspend; 1017 AstNodePromiseType promise_type; 1018 AstNodeEnumLiteral enum_literal; 1019 } data; 1020 }; 1021 1022 // this struct is allocated with allocate_nonzero 1023 struct FnTypeParamInfo { 1024 bool is_noalias; 1025 ZigType *type; 1026 }; 1027 1028 struct GenericFnTypeId { 1029 CodeGen *codegen; 1030 ZigFn *fn_entry; 1031 ConstExprValue *params; 1032 size_t param_count; 1033 }; 1034 1035 uint32_t generic_fn_type_id_hash(GenericFnTypeId *id); 1036 bool generic_fn_type_id_eql(GenericFnTypeId *a, GenericFnTypeId *b); 1037 1038 struct FnTypeId { 1039 ZigType *return_type; 1040 FnTypeParamInfo *param_info; 1041 size_t param_count; 1042 size_t next_param_index; 1043 bool is_var_args; 1044 CallingConvention cc; 1045 uint32_t alignment; 1046 ZigType *async_allocator_type; 1047 }; 1048 1049 uint32_t fn_type_id_hash(FnTypeId*); 1050 bool fn_type_id_eql(FnTypeId *a, FnTypeId *b); 1051 1052 enum PtrLen { 1053 PtrLenUnknown, 1054 PtrLenSingle, 1055 PtrLenC, 1056 }; 1057 1058 struct ZigTypePointer { 1059 ZigType *child_type; 1060 ZigType *slice_parent; 1061 PtrLen ptr_len; 1062 uint32_t explicit_alignment; // 0 means use ABI alignment 1063 uint32_t bit_offset_in_host; 1064 uint32_t host_int_bytes; // size of host integer. 0 means no host integer; this field is aligned 1065 bool is_const; 1066 bool is_volatile; 1067 bool allow_zero; 1068 }; 1069 1070 struct ZigTypeInt { 1071 uint32_t bit_count; 1072 bool is_signed; 1073 }; 1074 1075 struct ZigTypeFloat { 1076 size_t bit_count; 1077 }; 1078 1079 struct ZigTypeArray { 1080 ZigType *child_type; 1081 uint64_t len; 1082 }; 1083 1084 struct TypeStructField { 1085 Buf *name; 1086 ZigType *type_entry; 1087 size_t src_index; 1088 size_t gen_index; 1089 size_t offset; // byte offset from beginning of struct 1090 AstNode *decl_node; 1091 ConstExprValue *init_val; // null and then memoized 1092 uint32_t bit_offset_in_host; // offset from the memory at gen_index 1093 uint32_t host_int_bytes; // size of host integer 1094 }; 1095 1096 enum ResolveStatus { 1097 ResolveStatusUnstarted, 1098 ResolveStatusInvalid, 1099 ResolveStatusZeroBitsKnown, 1100 ResolveStatusAlignmentKnown, 1101 ResolveStatusSizeKnown, 1102 ResolveStatusLLVMFwdDecl, 1103 ResolveStatusLLVMFull, 1104 }; 1105 1106 struct ZigPackage { 1107 Buf root_src_dir; 1108 Buf root_src_path; // relative to root_src_dir 1109 Buf pkg_path; // a.b.c.d which follows the package dependency chain from the root package 1110 1111 // reminder: hash tables must be initialized before use 1112 HashMap<Buf *, ZigPackage *, buf_hash, buf_eql_buf> package_table; 1113 1114 bool added_to_cache; 1115 }; 1116 1117 // Stuff that only applies to a struct which is the implicit root struct of a file 1118 struct RootStruct { 1119 ZigPackage *package; 1120 Buf *path; // relative to root_package->root_src_dir 1121 ZigList<size_t> *line_offsets; 1122 Buf *source_code; 1123 ZigLLVMDIFile *di_file; 1124 }; 1125 1126 struct ZigTypeStruct { 1127 AstNode *decl_node; 1128 TypeStructField *fields; 1129 ScopeDecls *decls_scope; 1130 HashMap<Buf *, TypeStructField *, buf_hash, buf_eql_buf> fields_by_name; 1131 RootStruct *root_struct; 1132 uint32_t *host_int_bytes; // available for packed structs, indexed by gen_index 1133 1134 uint32_t src_field_count; 1135 uint32_t gen_field_count; 1136 1137 ContainerLayout layout; 1138 ResolveStatus resolve_status; 1139 1140 bool is_slice; 1141 bool resolve_loop_flag; // set this flag temporarily to detect infinite loops 1142 bool reported_infinite_err; 1143 // whether any of the fields require comptime 1144 // known after ResolveStatusZeroBitsKnown 1145 bool requires_comptime; 1146 }; 1147 1148 struct ZigTypeOptional { 1149 ZigType *child_type; 1150 }; 1151 1152 struct ZigTypeErrorUnion { 1153 ZigType *err_set_type; 1154 ZigType *payload_type; 1155 }; 1156 1157 struct ZigTypeErrorSet { 1158 uint32_t err_count; 1159 ErrorTableEntry **errors; 1160 ZigFn *infer_fn; 1161 }; 1162 1163 struct ZigTypeEnum { 1164 AstNode *decl_node; 1165 ContainerLayout layout; 1166 uint32_t src_field_count; 1167 TypeEnumField *fields; 1168 bool is_invalid; // true if any fields are invalid 1169 ZigType *tag_int_type; 1170 1171 ScopeDecls *decls_scope; 1172 1173 // set this flag temporarily to detect infinite loops 1174 bool embedded_in_current; 1175 bool reported_infinite_err; 1176 // whether we've finished resolving it 1177 bool complete; 1178 1179 bool zero_bits_loop_flag; 1180 bool zero_bits_known; 1181 1182 LLVMValueRef name_function; 1183 1184 HashMap<Buf *, TypeEnumField *, buf_hash, buf_eql_buf> fields_by_name; 1185 }; 1186 1187 uint32_t type_ptr_hash(const ZigType *ptr); 1188 bool type_ptr_eql(const ZigType *a, const ZigType *b); 1189 1190 struct ZigTypeUnion { 1191 AstNode *decl_node; 1192 TypeUnionField *fields; 1193 ScopeDecls *decls_scope; 1194 HashMap<Buf *, TypeUnionField *, buf_hash, buf_eql_buf> fields_by_name; 1195 ZigType *tag_type; // always an enum or null 1196 LLVMTypeRef union_llvm_type; 1197 ZigType *most_aligned_union_member; 1198 size_t gen_union_index; 1199 size_t gen_tag_index; 1200 size_t union_abi_size; 1201 1202 uint32_t src_field_count; 1203 uint32_t gen_field_count; 1204 1205 ContainerLayout layout; 1206 ResolveStatus resolve_status; 1207 1208 bool have_explicit_tag_type; 1209 bool resolve_loop_flag; // set this flag temporarily to detect infinite loops 1210 bool reported_infinite_err; 1211 // whether any of the fields require comptime 1212 // the value is not valid until zero_bits_known == true 1213 bool requires_comptime; 1214 }; 1215 1216 struct FnGenParamInfo { 1217 size_t src_index; 1218 size_t gen_index; 1219 bool is_byval; 1220 ZigType *type; 1221 }; 1222 1223 struct ZigTypeFn { 1224 FnTypeId fn_type_id; 1225 bool is_generic; 1226 ZigType *gen_return_type; 1227 size_t gen_param_count; 1228 FnGenParamInfo *gen_param_info; 1229 1230 LLVMTypeRef raw_type_ref; 1231 ZigLLVMDIType *raw_di_type; 1232 1233 ZigType *bound_fn_parent; 1234 }; 1235 1236 struct ZigTypeBoundFn { 1237 ZigType *fn_type; 1238 }; 1239 1240 struct ZigTypePromise { 1241 // null if `promise` instead of `promise->T` 1242 ZigType *result_type; 1243 }; 1244 1245 struct ZigTypeVector { 1246 // The type must be a pointer, integer, or float 1247 ZigType *elem_type; 1248 uint32_t len; 1249 }; 1250 1251 enum ZigTypeId { 1252 ZigTypeIdInvalid, 1253 ZigTypeIdMetaType, 1254 ZigTypeIdVoid, 1255 ZigTypeIdBool, 1256 ZigTypeIdUnreachable, 1257 ZigTypeIdInt, 1258 ZigTypeIdFloat, 1259 ZigTypeIdPointer, 1260 ZigTypeIdArray, 1261 ZigTypeIdStruct, 1262 ZigTypeIdComptimeFloat, 1263 ZigTypeIdComptimeInt, 1264 ZigTypeIdUndefined, 1265 ZigTypeIdNull, 1266 ZigTypeIdOptional, 1267 ZigTypeIdErrorUnion, 1268 ZigTypeIdErrorSet, 1269 ZigTypeIdEnum, 1270 ZigTypeIdUnion, 1271 ZigTypeIdFn, 1272 ZigTypeIdBoundFn, 1273 ZigTypeIdArgTuple, 1274 ZigTypeIdOpaque, 1275 ZigTypeIdPromise, 1276 ZigTypeIdVector, 1277 ZigTypeIdEnumLiteral, 1278 }; 1279 1280 enum OnePossibleValue { 1281 OnePossibleValueInvalid, 1282 OnePossibleValueNo, 1283 OnePossibleValueYes, 1284 }; 1285 1286 struct ZigTypeOpaque { 1287 Buf *bare_name; 1288 }; 1289 1290 struct ZigType { 1291 ZigTypeId id; 1292 Buf name; 1293 1294 // These are not supposed to be accessed directly. They're 1295 // null during semantic analysis, memoized with get_llvm_type 1296 // and get_llvm_di_type 1297 LLVMTypeRef llvm_type; 1298 ZigLLVMDIType *llvm_di_type; 1299 1300 union { 1301 ZigTypePointer pointer; 1302 ZigTypeInt integral; 1303 ZigTypeFloat floating; 1304 ZigTypeArray array; 1305 ZigTypeStruct structure; 1306 ZigTypeOptional maybe; 1307 ZigTypeErrorUnion error_union; 1308 ZigTypeErrorSet error_set; 1309 ZigTypeEnum enumeration; 1310 ZigTypeUnion unionation; 1311 ZigTypeFn fn; 1312 ZigTypeBoundFn bound_fn; 1313 ZigTypePromise promise; 1314 ZigTypeVector vector; 1315 ZigTypeOpaque opaque; 1316 } data; 1317 1318 // use these fields to make sure we don't duplicate type table entries for the same type 1319 ZigType *pointer_parent[2]; // [0 - mut, 1 - const] 1320 ZigType *optional_parent; 1321 ZigType *promise_parent; 1322 ZigType *promise_frame_parent; 1323 // If we generate a constant name value for this type, we memoize it here. 1324 // The type of this is array 1325 ConstExprValue *cached_const_name_val; 1326 1327 OnePossibleValue one_possible_value; 1328 // Known after ResolveStatusAlignmentKnown. 1329 uint32_t abi_align; 1330 // The offset in bytes between consecutive array elements of this type. Known 1331 // after ResolveStatusSizeKnown. 1332 size_t abi_size; 1333 // Number of bits of information in this type. Known after ResolveStatusSizeKnown. 1334 size_t size_in_bits; 1335 1336 bool gen_h_loop_flag; 1337 }; 1338 1339 enum FnAnalState { 1340 FnAnalStateReady, 1341 FnAnalStateProbing, 1342 FnAnalStateComplete, 1343 FnAnalStateInvalid, 1344 }; 1345 1346 enum FnInline { 1347 FnInlineAuto, 1348 FnInlineAlways, 1349 FnInlineNever, 1350 }; 1351 1352 struct GlobalExport { 1353 Buf name; 1354 GlobalLinkageId linkage; 1355 }; 1356 1357 struct ZigFn { 1358 CodeGen *codegen; 1359 LLVMValueRef llvm_value; 1360 const char *llvm_name; 1361 AstNode *proto_node; 1362 AstNode *body_node; 1363 ScopeFnDef *fndef_scope; // parent should be the top level decls or container decls 1364 Scope *child_scope; // parent is scope for last parameter 1365 ScopeBlock *def_scope; // parent is child_scope 1366 Buf symbol_name; 1367 ZigType *type_entry; // function type 1368 // in the case of normal functions this is the implicit return type 1369 // in the case of async functions this is the implicit return type according to the 1370 // zig source code, not according to zig ir 1371 ZigType *src_implicit_return_type; 1372 IrExecutable ir_executable; 1373 IrExecutable analyzed_executable; 1374 size_t prealloc_bbc; 1375 size_t prealloc_backward_branch_quota; 1376 AstNode **param_source_nodes; 1377 Buf **param_names; 1378 1379 AstNode *fn_no_inline_set_node; 1380 AstNode *fn_static_eval_set_node; 1381 1382 ZigList<IrInstructionAllocaGen *> alloca_gen_list; 1383 ZigList<ZigVar *> variable_list; 1384 1385 Buf *section_name; 1386 AstNode *set_alignstack_node; 1387 1388 AstNode *set_cold_node; 1389 1390 ZigList<GlobalExport> export_list; 1391 1392 LLVMValueRef valgrind_client_request_array; 1393 1394 FnInline fn_inline; 1395 FnAnalState anal_state; 1396 1397 uint32_t align_bytes; 1398 uint32_t alignstack_value; 1399 1400 bool calls_or_awaits_errorable_fn; 1401 bool is_cold; 1402 bool is_test; 1403 }; 1404 1405 uint32_t fn_table_entry_hash(ZigFn*); 1406 bool fn_table_entry_eql(ZigFn *a, ZigFn *b); 1407 1408 enum BuiltinFnId { 1409 BuiltinFnIdInvalid, 1410 BuiltinFnIdMemcpy, 1411 BuiltinFnIdMemset, 1412 BuiltinFnIdSizeof, 1413 BuiltinFnIdAlignOf, 1414 BuiltinFnIdMemberCount, 1415 BuiltinFnIdMemberType, 1416 BuiltinFnIdMemberName, 1417 BuiltinFnIdField, 1418 BuiltinFnIdTypeInfo, 1419 BuiltinFnIdTypeof, 1420 BuiltinFnIdAddWithOverflow, 1421 BuiltinFnIdSubWithOverflow, 1422 BuiltinFnIdMulWithOverflow, 1423 BuiltinFnIdShlWithOverflow, 1424 BuiltinFnIdMulAdd, 1425 BuiltinFnIdCInclude, 1426 BuiltinFnIdCDefine, 1427 BuiltinFnIdCUndef, 1428 BuiltinFnIdCompileErr, 1429 BuiltinFnIdCompileLog, 1430 BuiltinFnIdCtz, 1431 BuiltinFnIdClz, 1432 BuiltinFnIdPopCount, 1433 BuiltinFnIdBswap, 1434 BuiltinFnIdBitReverse, 1435 BuiltinFnIdImport, 1436 BuiltinFnIdCImport, 1437 BuiltinFnIdErrName, 1438 BuiltinFnIdBreakpoint, 1439 BuiltinFnIdReturnAddress, 1440 BuiltinFnIdFrameAddress, 1441 BuiltinFnIdHandle, 1442 BuiltinFnIdEmbedFile, 1443 BuiltinFnIdCmpxchgWeak, 1444 BuiltinFnIdCmpxchgStrong, 1445 BuiltinFnIdFence, 1446 BuiltinFnIdDivExact, 1447 BuiltinFnIdDivTrunc, 1448 BuiltinFnIdDivFloor, 1449 BuiltinFnIdRem, 1450 BuiltinFnIdMod, 1451 BuiltinFnIdSqrt, 1452 BuiltinFnIdSin, 1453 BuiltinFnIdCos, 1454 BuiltinFnIdExp, 1455 BuiltinFnIdExp2, 1456 BuiltinFnIdLn, 1457 BuiltinFnIdLog2, 1458 BuiltinFnIdLog10, 1459 BuiltinFnIdFabs, 1460 BuiltinFnIdFloor, 1461 BuiltinFnIdCeil, 1462 BuiltinFnIdTrunc, 1463 BuiltinFnIdNearbyInt, 1464 BuiltinFnIdRound, 1465 BuiltinFnIdTruncate, 1466 BuiltinFnIdIntCast, 1467 BuiltinFnIdFloatCast, 1468 BuiltinFnIdErrSetCast, 1469 BuiltinFnIdToBytes, 1470 BuiltinFnIdFromBytes, 1471 BuiltinFnIdIntToFloat, 1472 BuiltinFnIdFloatToInt, 1473 BuiltinFnIdBoolToInt, 1474 BuiltinFnIdErrToInt, 1475 BuiltinFnIdIntToErr, 1476 BuiltinFnIdEnumToInt, 1477 BuiltinFnIdIntToEnum, 1478 BuiltinFnIdIntType, 1479 BuiltinFnIdVectorType, 1480 BuiltinFnIdSetCold, 1481 BuiltinFnIdSetRuntimeSafety, 1482 BuiltinFnIdSetFloatMode, 1483 BuiltinFnIdTypeName, 1484 BuiltinFnIdPanic, 1485 BuiltinFnIdPtrCast, 1486 BuiltinFnIdBitCast, 1487 BuiltinFnIdIntToPtr, 1488 BuiltinFnIdPtrToInt, 1489 BuiltinFnIdTagName, 1490 BuiltinFnIdTagType, 1491 BuiltinFnIdFieldParentPtr, 1492 BuiltinFnIdByteOffsetOf, 1493 BuiltinFnIdBitOffsetOf, 1494 BuiltinFnIdInlineCall, 1495 BuiltinFnIdNoInlineCall, 1496 BuiltinFnIdNewStackCall, 1497 BuiltinFnIdTypeId, 1498 BuiltinFnIdShlExact, 1499 BuiltinFnIdShrExact, 1500 BuiltinFnIdSetEvalBranchQuota, 1501 BuiltinFnIdAlignCast, 1502 BuiltinFnIdOpaqueType, 1503 BuiltinFnIdThis, 1504 BuiltinFnIdSetAlignStack, 1505 BuiltinFnIdArgType, 1506 BuiltinFnIdExport, 1507 BuiltinFnIdErrorReturnTrace, 1508 BuiltinFnIdAtomicRmw, 1509 BuiltinFnIdAtomicLoad, 1510 BuiltinFnIdHasDecl, 1511 }; 1512 1513 struct BuiltinFnEntry { 1514 BuiltinFnId id; 1515 Buf name; 1516 size_t param_count; 1517 }; 1518 1519 enum PanicMsgId { 1520 PanicMsgIdUnreachable, 1521 PanicMsgIdBoundsCheckFailure, 1522 PanicMsgIdCastNegativeToUnsigned, 1523 PanicMsgIdCastTruncatedData, 1524 PanicMsgIdIntegerOverflow, 1525 PanicMsgIdShlOverflowedBits, 1526 PanicMsgIdShrOverflowedBits, 1527 PanicMsgIdDivisionByZero, 1528 PanicMsgIdRemainderDivisionByZero, 1529 PanicMsgIdExactDivisionRemainder, 1530 PanicMsgIdSliceWidenRemainder, 1531 PanicMsgIdUnwrapOptionalFail, 1532 PanicMsgIdInvalidErrorCode, 1533 PanicMsgIdIncorrectAlignment, 1534 PanicMsgIdBadUnionField, 1535 PanicMsgIdBadEnumValue, 1536 PanicMsgIdFloatToInt, 1537 PanicMsgIdPtrCastNull, 1538 1539 PanicMsgIdCount, 1540 }; 1541 1542 uint32_t fn_eval_hash(Scope*); 1543 bool fn_eval_eql(Scope *a, Scope *b); 1544 1545 struct TypeId { 1546 ZigTypeId id; 1547 1548 union { 1549 struct { 1550 ZigType *child_type; 1551 PtrLen ptr_len; 1552 uint32_t alignment; 1553 uint32_t bit_offset_in_host; 1554 uint32_t host_int_bytes; 1555 bool is_const; 1556 bool is_volatile; 1557 bool allow_zero; 1558 } pointer; 1559 struct { 1560 ZigType *child_type; 1561 uint64_t size; 1562 } array; 1563 struct { 1564 bool is_signed; 1565 uint32_t bit_count; 1566 } integer; 1567 struct { 1568 ZigType *err_set_type; 1569 ZigType *payload_type; 1570 } error_union; 1571 struct { 1572 ZigType *elem_type; 1573 uint32_t len; 1574 } vector; 1575 } data; 1576 }; 1577 1578 uint32_t type_id_hash(TypeId); 1579 bool type_id_eql(TypeId a, TypeId b); 1580 1581 enum ZigLLVMFnId { 1582 ZigLLVMFnIdCtz, 1583 ZigLLVMFnIdClz, 1584 ZigLLVMFnIdPopCount, 1585 ZigLLVMFnIdOverflowArithmetic, 1586 ZigLLVMFnIdFMA, 1587 ZigLLVMFnIdFloatOp, 1588 ZigLLVMFnIdBswap, 1589 ZigLLVMFnIdBitReverse, 1590 }; 1591 1592 // There are a bunch of places in code that rely on these values being in 1593 // exactly this order. 1594 enum AddSubMul { 1595 AddSubMulAdd = 0, 1596 AddSubMulSub = 1, 1597 AddSubMulMul = 2, 1598 }; 1599 1600 struct ZigLLVMFnKey { 1601 ZigLLVMFnId id; 1602 1603 union { 1604 struct { 1605 uint32_t bit_count; 1606 } ctz; 1607 struct { 1608 uint32_t bit_count; 1609 } clz; 1610 struct { 1611 uint32_t bit_count; 1612 } pop_count; 1613 struct { 1614 BuiltinFnId op; 1615 uint32_t bit_count; 1616 uint32_t vector_len; // 0 means not a vector 1617 } floating; 1618 struct { 1619 AddSubMul add_sub_mul; 1620 uint32_t bit_count; 1621 uint32_t vector_len; // 0 means not a vector 1622 bool is_signed; 1623 } overflow_arithmetic; 1624 struct { 1625 uint32_t bit_count; 1626 } bswap; 1627 struct { 1628 uint32_t bit_count; 1629 } bit_reverse; 1630 } data; 1631 }; 1632 1633 uint32_t zig_llvm_fn_key_hash(ZigLLVMFnKey); 1634 bool zig_llvm_fn_key_eql(ZigLLVMFnKey a, ZigLLVMFnKey b); 1635 1636 struct TimeEvent { 1637 double time; 1638 const char *name; 1639 }; 1640 1641 enum BuildMode { 1642 BuildModeDebug, 1643 BuildModeFastRelease, 1644 BuildModeSafeRelease, 1645 BuildModeSmallRelease, 1646 }; 1647 1648 enum EmitFileType { 1649 EmitFileTypeBinary, 1650 EmitFileTypeAssembly, 1651 EmitFileTypeLLVMIr, 1652 }; 1653 1654 struct LinkLib { 1655 Buf *name; 1656 Buf *path; 1657 ZigList<Buf *> symbols; // the list of symbols that we depend on from this lib 1658 bool provided_explicitly; 1659 }; 1660 1661 enum ValgrindSupport { 1662 ValgrindSupportAuto, 1663 ValgrindSupportDisabled, 1664 ValgrindSupportEnabled, 1665 }; 1666 1667 enum WantPIC { 1668 WantPICAuto, 1669 WantPICDisabled, 1670 WantPICEnabled, 1671 }; 1672 1673 enum WantStackCheck { 1674 WantStackCheckAuto, 1675 WantStackCheckDisabled, 1676 WantStackCheckEnabled, 1677 }; 1678 1679 struct CFile { 1680 ZigList<const char *> args; 1681 const char *source_path; 1682 }; 1683 1684 // When adding fields, check if they should be added to the hash computation in build_with_cache 1685 struct CodeGen { 1686 //////////////////////////// Runtime State 1687 LLVMModuleRef module; 1688 ZigList<ErrorMsg*> errors; 1689 LLVMBuilderRef builder; 1690 ZigLLVMDIBuilder *dbuilder; 1691 ZigLLVMDICompileUnit *compile_unit; 1692 ZigLLVMDIFile *compile_unit_file; 1693 LinkLib *libc_link_lib; 1694 LLVMTargetDataRef target_data_ref; 1695 LLVMTargetMachineRef target_machine; 1696 ZigLLVMDIFile *dummy_di_file; 1697 LLVMValueRef cur_ret_ptr; 1698 LLVMValueRef cur_fn_val; 1699 LLVMValueRef cur_err_ret_trace_val_arg; 1700 LLVMValueRef cur_err_ret_trace_val_stack; 1701 LLVMValueRef memcpy_fn_val; 1702 LLVMValueRef memset_fn_val; 1703 LLVMValueRef trap_fn_val; 1704 LLVMValueRef return_address_fn_val; 1705 LLVMValueRef frame_address_fn_val; 1706 LLVMValueRef coro_destroy_fn_val; 1707 LLVMValueRef coro_id_fn_val; 1708 LLVMValueRef coro_alloc_fn_val; 1709 LLVMValueRef coro_size_fn_val; 1710 LLVMValueRef coro_begin_fn_val; 1711 LLVMValueRef coro_suspend_fn_val; 1712 LLVMValueRef coro_end_fn_val; 1713 LLVMValueRef coro_free_fn_val; 1714 LLVMValueRef coro_resume_fn_val; 1715 LLVMValueRef coro_save_fn_val; 1716 LLVMValueRef coro_promise_fn_val; 1717 LLVMValueRef coro_alloc_helper_fn_val; 1718 LLVMValueRef coro_frame_fn_val; 1719 LLVMValueRef merge_err_ret_traces_fn_val; 1720 LLVMValueRef add_error_return_trace_addr_fn_val; 1721 LLVMValueRef stacksave_fn_val; 1722 LLVMValueRef stackrestore_fn_val; 1723 LLVMValueRef write_register_fn_val; 1724 LLVMValueRef sp_md_node; 1725 LLVMValueRef err_name_table; 1726 LLVMValueRef safety_crash_err_fn; 1727 LLVMValueRef return_err_fn; 1728 1729 // reminder: hash tables must be initialized before use 1730 HashMap<Buf *, ZigType *, buf_hash, buf_eql_buf> import_table; 1731 HashMap<Buf *, BuiltinFnEntry *, buf_hash, buf_eql_buf> builtin_fn_table; 1732 HashMap<Buf *, ZigType *, buf_hash, buf_eql_buf> primitive_type_table; 1733 HashMap<TypeId, ZigType *, type_id_hash, type_id_eql> type_table; 1734 HashMap<FnTypeId *, ZigType *, fn_type_id_hash, fn_type_id_eql> fn_type_table; 1735 HashMap<Buf *, ErrorTableEntry *, buf_hash, buf_eql_buf> error_table; 1736 HashMap<GenericFnTypeId *, ZigFn *, generic_fn_type_id_hash, generic_fn_type_id_eql> generic_table; 1737 HashMap<Scope *, ConstExprValue *, fn_eval_hash, fn_eval_eql> memoized_fn_eval_table; 1738 HashMap<ZigLLVMFnKey, LLVMValueRef, zig_llvm_fn_key_hash, zig_llvm_fn_key_eql> llvm_fn_table; 1739 HashMap<Buf *, Tld *, buf_hash, buf_eql_buf> exported_symbol_names; 1740 HashMap<Buf *, Tld *, buf_hash, buf_eql_buf> external_prototypes; 1741 HashMap<Buf *, ConstExprValue *, buf_hash, buf_eql_buf> string_literals_table; 1742 HashMap<const ZigType *, ConstExprValue *, type_ptr_hash, type_ptr_eql> type_info_cache; 1743 1744 ZigList<Tld *> resolve_queue; 1745 size_t resolve_queue_index; 1746 ZigList<AstNode *> use_queue; 1747 size_t use_queue_index; 1748 ZigList<TimeEvent> timing_events; 1749 ZigList<AstNode *> tld_ref_source_node_stack; 1750 ZigList<ZigFn *> inline_fns; 1751 ZigList<ZigFn *> test_fns; 1752 ZigList<ErrorTableEntry *> errors_by_index; 1753 ZigList<CacheHash *> caches_to_release; 1754 size_t largest_err_name_len; 1755 1756 ZigPackage *std_package; 1757 ZigPackage *panic_package; 1758 ZigPackage *test_runner_package; 1759 ZigPackage *compile_var_package; 1760 ZigType *compile_var_import; 1761 ZigType *root_import; 1762 ZigType *bootstrap_import; 1763 ZigType *test_runner_import; 1764 1765 struct { 1766 ZigType *entry_bool; 1767 ZigType *entry_c_int[CIntTypeCount]; 1768 ZigType *entry_c_longdouble; 1769 ZigType *entry_c_void; 1770 ZigType *entry_u8; 1771 ZigType *entry_u16; 1772 ZigType *entry_u32; 1773 ZigType *entry_u29; 1774 ZigType *entry_u64; 1775 ZigType *entry_i8; 1776 ZigType *entry_i32; 1777 ZigType *entry_i64; 1778 ZigType *entry_isize; 1779 ZigType *entry_usize; 1780 ZigType *entry_f16; 1781 ZigType *entry_f32; 1782 ZigType *entry_f64; 1783 ZigType *entry_f128; 1784 ZigType *entry_void; 1785 ZigType *entry_unreachable; 1786 ZigType *entry_type; 1787 ZigType *entry_invalid; 1788 ZigType *entry_block; 1789 ZigType *entry_num_lit_int; 1790 ZigType *entry_num_lit_float; 1791 ZigType *entry_undef; 1792 ZigType *entry_null; 1793 ZigType *entry_var; 1794 ZigType *entry_global_error_set; 1795 ZigType *entry_arg_tuple; 1796 ZigType *entry_promise; 1797 ZigType *entry_enum_literal; 1798 } builtin_types; 1799 ZigType *align_amt_type; 1800 ZigType *stack_trace_type; 1801 ZigType *ptr_to_stack_trace_type; 1802 ZigType *err_tag_type; 1803 ZigType *test_fn_type; 1804 1805 Buf triple_str; 1806 Buf global_asm; 1807 Buf output_file_path; 1808 Buf o_file_output_path; 1809 Buf *cache_dir; 1810 // As an input parameter, mutually exclusive with enable_cache. But it gets 1811 // populated in codegen_build_and_link. 1812 Buf *output_dir; 1813 Buf **libc_include_dir_list; 1814 size_t libc_include_dir_len; 1815 1816 Buf *zig_c_headers_dir; // Cannot be overridden; derived from zig_lib_dir. 1817 Buf *zig_std_special_dir; // Cannot be overridden; derived from zig_lib_dir. 1818 1819 IrInstruction *invalid_instruction; 1820 IrInstruction *unreach_instruction; 1821 1822 ConstExprValue const_void_val; 1823 ConstExprValue panic_msg_vals[PanicMsgIdCount]; 1824 1825 // The function definitions this module includes. 1826 ZigList<ZigFn *> fn_defs; 1827 size_t fn_defs_index; 1828 ZigList<TldVar *> global_vars; 1829 1830 ZigFn *cur_fn; 1831 ZigFn *main_fn; 1832 ZigFn *panic_fn; 1833 TldFn *panic_tld_fn; 1834 AstNode *root_export_decl; 1835 1836 WantPIC want_pic; 1837 WantStackCheck want_stack_check; 1838 CacheHash cache_hash; 1839 ErrColor err_color; 1840 uint32_t next_unresolved_index; 1841 unsigned pointer_size_bytes; 1842 uint32_t target_os_index; 1843 uint32_t target_arch_index; 1844 uint32_t target_sub_arch_index; 1845 uint32_t target_abi_index; 1846 uint32_t target_oformat_index; 1847 bool is_big_endian; 1848 bool have_pub_main; 1849 bool have_c_main; 1850 bool have_winmain; 1851 bool have_winmain_crt_startup; 1852 bool have_dllmain_crt_startup; 1853 bool have_pub_panic; 1854 bool have_err_ret_tracing; 1855 bool c_want_stdint; 1856 bool c_want_stdbool; 1857 bool verbose_tokenize; 1858 bool verbose_ast; 1859 bool verbose_link; 1860 bool verbose_ir; 1861 bool verbose_llvm_ir; 1862 bool verbose_cimport; 1863 bool verbose_cc; 1864 bool error_during_imports; 1865 bool generate_error_name_table; 1866 bool enable_cache; // mutually exclusive with output_dir 1867 bool enable_time_report; 1868 bool system_linker_hack; 1869 bool reported_bad_link_libc_error; 1870 bool is_dynamic; // shared library rather than static library. dynamic musl rather than static musl. 1871 1872 //////////////////////////// Participates in Input Parameter Cache Hash 1873 /////// Note: there is a separate cache hash for builtin.zig, when adding fields, 1874 /////// consider if they need to go into both. 1875 ZigList<LinkLib *> link_libs_list; 1876 // add -framework [name] args to linker 1877 ZigList<Buf *> darwin_frameworks; 1878 // add -rpath [name] args to linker 1879 ZigList<Buf *> rpath_list; 1880 ZigList<Buf *> forbidden_libs; 1881 ZigList<Buf *> link_objects; 1882 ZigList<Buf *> assembly_files; 1883 ZigList<CFile *> c_source_files; 1884 ZigList<const char *> lib_dirs; 1885 1886 ZigLibCInstallation *libc; 1887 1888 size_t version_major; 1889 size_t version_minor; 1890 size_t version_patch; 1891 const char *linker_script; 1892 1893 EmitFileType emit_file_type; 1894 BuildMode build_mode; 1895 OutType out_type; 1896 const ZigTarget *zig_target; 1897 TargetSubsystem subsystem; // careful using this directly; see detect_subsystem 1898 ValgrindSupport valgrind_support; 1899 bool strip_debug_symbols; 1900 bool is_test_build; 1901 bool is_single_threaded; 1902 bool want_single_threaded; 1903 bool linker_rdynamic; 1904 bool each_lib_rpath; 1905 bool is_dummy_so; 1906 bool disable_gen_h; 1907 bool bundle_compiler_rt; 1908 bool have_pic; 1909 bool have_dynamic_link; // this is whether the final thing will be dynamically linked. see also is_dynamic 1910 bool have_stack_probing; 1911 1912 Buf *mmacosx_version_min; 1913 Buf *mios_version_min; 1914 Buf *root_out_name; 1915 Buf *test_filter; 1916 Buf *test_name_prefix; 1917 ZigPackage *root_package; 1918 Buf *zig_lib_dir; 1919 Buf *zig_std_dir; 1920 Buf *dynamic_linker_path; 1921 1922 const char **llvm_argv; 1923 size_t llvm_argv_len; 1924 1925 const char **clang_argv; 1926 size_t clang_argv_len; 1927 }; 1928 1929 struct ZigVar { 1930 Buf name; 1931 ConstExprValue *const_value; 1932 ZigType *var_type; 1933 LLVMValueRef value_ref; 1934 IrInstruction *is_comptime; 1935 // which node is the declaration of the variable 1936 AstNode *decl_node; 1937 ZigLLVMDILocalVariable *di_loc_var; 1938 size_t src_arg_index; 1939 Scope *parent_scope; 1940 Scope *child_scope; 1941 LLVMValueRef param_value_ref; 1942 size_t mem_slot_index; 1943 IrExecutable *owner_exec; 1944 size_t ref_count; 1945 1946 // In an inline loop, multiple variables may be created, 1947 // In this case, a reference to a variable should follow 1948 // this pointer to the redefined variable. 1949 ZigVar *next_var; 1950 1951 ZigList<GlobalExport> export_list; 1952 1953 uint32_t align_bytes; 1954 1955 bool shadowable; 1956 bool src_is_const; 1957 bool gen_is_const; 1958 bool is_thread_local; 1959 }; 1960 1961 struct ErrorTableEntry { 1962 Buf name; 1963 uint32_t value; 1964 AstNode *decl_node; 1965 ZigType *set_with_only_this_in_it; 1966 // If we generate a constant error name value for this error, we memoize it here. 1967 // The type of this is array 1968 ConstExprValue *cached_error_name_val; 1969 }; 1970 1971 enum ScopeId { 1972 ScopeIdDecls, 1973 ScopeIdBlock, 1974 ScopeIdDefer, 1975 ScopeIdDeferExpr, 1976 ScopeIdVarDecl, 1977 ScopeIdCImport, 1978 ScopeIdLoop, 1979 ScopeIdSuspend, 1980 ScopeIdFnDef, 1981 ScopeIdCompTime, 1982 ScopeIdCoroPrelude, 1983 ScopeIdRuntime, 1984 }; 1985 1986 struct Scope { 1987 CodeGen *codegen; 1988 AstNode *source_node; 1989 1990 // if the scope has a parent, this is it 1991 Scope *parent; 1992 1993 ZigLLVMDIScope *di_scope; 1994 ScopeId id; 1995 }; 1996 1997 // This scope comes from global declarations or from 1998 // declarations in a container declaration 1999 // NodeTypeContainerDecl 2000 struct ScopeDecls { 2001 Scope base; 2002 2003 HashMap<Buf *, Tld *, buf_hash, buf_eql_buf> decl_table; 2004 ZigList<AstNode *> use_decls; 2005 AstNode *safety_set_node; 2006 AstNode *fast_math_set_node; 2007 ZigType *import; 2008 // If this is a scope from a container, this is the type entry, otherwise null 2009 ZigType *container_type; 2010 Buf *bare_name; 2011 2012 bool safety_off; 2013 bool fast_math_on; 2014 bool any_imports_failed; 2015 }; 2016 2017 enum LVal { 2018 LValNone, 2019 LValPtr, 2020 }; 2021 2022 // This scope comes from a block expression in user code. 2023 // NodeTypeBlock 2024 struct ScopeBlock { 2025 Scope base; 2026 2027 Buf *name; 2028 IrBasicBlock *end_block; 2029 IrInstruction *is_comptime; 2030 ResultLocPeerParent *peer_parent; 2031 ZigList<IrInstruction *> *incoming_values; 2032 ZigList<IrBasicBlock *> *incoming_blocks; 2033 2034 AstNode *safety_set_node; 2035 AstNode *fast_math_set_node; 2036 2037 LVal lval; 2038 bool safety_off; 2039 bool fast_math_on; 2040 }; 2041 2042 // This scope is created from every defer expression. 2043 // It's the code following the defer statement. 2044 // NodeTypeDefer 2045 struct ScopeDefer { 2046 Scope base; 2047 }; 2048 2049 // This scope is created from every defer expression. 2050 // It's the parent of the defer expression itself. 2051 // NodeTypeDefer 2052 struct ScopeDeferExpr { 2053 Scope base; 2054 2055 bool reported_err; 2056 }; 2057 2058 // This scope is created for every variable declaration inside an IrExecutable 2059 // NodeTypeVariableDeclaration, NodeTypeParamDecl 2060 struct ScopeVarDecl { 2061 Scope base; 2062 2063 // The variable that creates this scope 2064 ZigVar *var; 2065 }; 2066 2067 // This scope is created for a @cImport 2068 // NodeTypeFnCallExpr 2069 struct ScopeCImport { 2070 Scope base; 2071 2072 Buf buf; 2073 }; 2074 2075 // This scope is created for a loop such as for or while in order to 2076 // make break and continue statements work. 2077 // NodeTypeForExpr or NodeTypeWhileExpr 2078 struct ScopeLoop { 2079 Scope base; 2080 2081 LVal lval; 2082 Buf *name; 2083 IrBasicBlock *break_block; 2084 IrBasicBlock *continue_block; 2085 IrInstruction *is_comptime; 2086 ZigList<IrInstruction *> *incoming_values; 2087 ZigList<IrBasicBlock *> *incoming_blocks; 2088 ResultLocPeerParent *peer_parent; 2089 }; 2090 2091 // This scope blocks certain things from working such as comptime continue 2092 // inside a runtime if expression. 2093 // NodeTypeIfBoolExpr, NodeTypeWhileExpr, NodeTypeForExpr 2094 struct ScopeRuntime { 2095 Scope base; 2096 2097 IrInstruction *is_comptime; 2098 }; 2099 2100 // This scope is created for a suspend block in order to have labeled 2101 // suspend for breaking out of a suspend and for detecting if a suspend 2102 // block is inside a suspend block. 2103 struct ScopeSuspend { 2104 Scope base; 2105 2106 IrBasicBlock *resume_block; 2107 bool reported_err; 2108 }; 2109 2110 // This scope is created for a comptime expression. 2111 // NodeTypeCompTime, NodeTypeSwitchExpr 2112 struct ScopeCompTime { 2113 Scope base; 2114 }; 2115 2116 2117 // This scope is created for a function definition. 2118 // NodeTypeFnDef 2119 struct ScopeFnDef { 2120 Scope base; 2121 2122 ZigFn *fn_entry; 2123 }; 2124 2125 // This scope is created to indicate that the code in the scope 2126 // is auto-generated coroutine prelude stuff. 2127 struct ScopeCoroPrelude { 2128 Scope base; 2129 }; 2130 2131 // synchronized with code in define_builtin_compile_vars 2132 enum AtomicOrder { 2133 AtomicOrderUnordered, 2134 AtomicOrderMonotonic, 2135 AtomicOrderAcquire, 2136 AtomicOrderRelease, 2137 AtomicOrderAcqRel, 2138 AtomicOrderSeqCst, 2139 }; 2140 2141 // synchronized with the code in define_builtin_compile_vars 2142 enum AtomicRmwOp { 2143 AtomicRmwOp_xchg, 2144 AtomicRmwOp_add, 2145 AtomicRmwOp_sub, 2146 AtomicRmwOp_and, 2147 AtomicRmwOp_nand, 2148 AtomicRmwOp_or, 2149 AtomicRmwOp_xor, 2150 AtomicRmwOp_max, 2151 AtomicRmwOp_min, 2152 }; 2153 2154 // A basic block contains no branching. Branches send control flow 2155 // to another basic block. 2156 // Phi instructions must be first in a basic block. 2157 // The last instruction in a basic block must be of type unreachable. 2158 struct IrBasicBlock { 2159 ZigList<IrInstruction *> instruction_list; 2160 IrBasicBlock *other; 2161 Scope *scope; 2162 const char *name_hint; 2163 size_t debug_id; 2164 size_t ref_count; 2165 // index into the basic block list 2166 size_t index; 2167 LLVMBasicBlockRef llvm_block; 2168 LLVMBasicBlockRef llvm_exit_block; 2169 // The instruction that referenced this basic block and caused us to 2170 // analyze the basic block. If the same instruction wants us to emit 2171 // the same basic block, then we re-generate it instead of saving it. 2172 IrInstruction *ref_instruction; 2173 // When this is non-null, a branch to this basic block is only allowed 2174 // if the branch is comptime. The instruction points to the reason 2175 // the basic block must be comptime. 2176 IrInstruction *must_be_comptime_source_instr; 2177 IrInstruction *suspend_instruction_ref; 2178 bool already_appended; 2179 bool suspended; 2180 bool in_resume_stack; 2181 }; 2182 2183 // These instructions are in transition to having "pass 1" instructions 2184 // and "pass 2" instructions. The pass 1 instructions are suffixed with Src 2185 // and pass 2 are suffixed with Gen. 2186 // Once all instructions are separated in this way, they'll have different 2187 // base types for better type safety. 2188 // Src instructions are generated by ir_gen_* functions in ir.cpp from AST. 2189 // ir_analyze_* functions consume Src instructions and produce Gen instructions. 2190 // ir_render_* functions in codegen.cpp consume Gen instructions and produce LLVM IR. 2191 // Src instructions do not have type information; Gen instructions do. 2192 enum IrInstructionId { 2193 IrInstructionIdInvalid, 2194 IrInstructionIdDeclVarSrc, 2195 IrInstructionIdDeclVarGen, 2196 IrInstructionIdBr, 2197 IrInstructionIdCondBr, 2198 IrInstructionIdSwitchBr, 2199 IrInstructionIdSwitchVar, 2200 IrInstructionIdSwitchElseVar, 2201 IrInstructionIdSwitchTarget, 2202 IrInstructionIdPhi, 2203 IrInstructionIdUnOp, 2204 IrInstructionIdBinOp, 2205 IrInstructionIdLoadPtr, 2206 IrInstructionIdLoadPtrGen, 2207 IrInstructionIdStorePtr, 2208 IrInstructionIdFieldPtr, 2209 IrInstructionIdStructFieldPtr, 2210 IrInstructionIdUnionFieldPtr, 2211 IrInstructionIdElemPtr, 2212 IrInstructionIdVarPtr, 2213 IrInstructionIdReturnPtr, 2214 IrInstructionIdCallSrc, 2215 IrInstructionIdCallGen, 2216 IrInstructionIdConst, 2217 IrInstructionIdReturn, 2218 IrInstructionIdCast, 2219 IrInstructionIdResizeSlice, 2220 IrInstructionIdContainerInitList, 2221 IrInstructionIdContainerInitFields, 2222 IrInstructionIdUnreachable, 2223 IrInstructionIdTypeOf, 2224 IrInstructionIdSetCold, 2225 IrInstructionIdSetRuntimeSafety, 2226 IrInstructionIdSetFloatMode, 2227 IrInstructionIdArrayType, 2228 IrInstructionIdPromiseType, 2229 IrInstructionIdSliceType, 2230 IrInstructionIdGlobalAsm, 2231 IrInstructionIdAsm, 2232 IrInstructionIdSizeOf, 2233 IrInstructionIdTestNonNull, 2234 IrInstructionIdOptionalUnwrapPtr, 2235 IrInstructionIdOptionalWrap, 2236 IrInstructionIdUnionTag, 2237 IrInstructionIdClz, 2238 IrInstructionIdCtz, 2239 IrInstructionIdPopCount, 2240 IrInstructionIdBswap, 2241 IrInstructionIdBitReverse, 2242 IrInstructionIdImport, 2243 IrInstructionIdCImport, 2244 IrInstructionIdCInclude, 2245 IrInstructionIdCDefine, 2246 IrInstructionIdCUndef, 2247 IrInstructionIdRef, 2248 IrInstructionIdRefGen, 2249 IrInstructionIdCompileErr, 2250 IrInstructionIdCompileLog, 2251 IrInstructionIdErrName, 2252 IrInstructionIdEmbedFile, 2253 IrInstructionIdCmpxchgSrc, 2254 IrInstructionIdCmpxchgGen, 2255 IrInstructionIdFence, 2256 IrInstructionIdTruncate, 2257 IrInstructionIdIntCast, 2258 IrInstructionIdFloatCast, 2259 IrInstructionIdIntToFloat, 2260 IrInstructionIdFloatToInt, 2261 IrInstructionIdBoolToInt, 2262 IrInstructionIdIntType, 2263 IrInstructionIdVectorType, 2264 IrInstructionIdBoolNot, 2265 IrInstructionIdMemset, 2266 IrInstructionIdMemcpy, 2267 IrInstructionIdSliceSrc, 2268 IrInstructionIdSliceGen, 2269 IrInstructionIdMemberCount, 2270 IrInstructionIdMemberType, 2271 IrInstructionIdMemberName, 2272 IrInstructionIdBreakpoint, 2273 IrInstructionIdReturnAddress, 2274 IrInstructionIdFrameAddress, 2275 IrInstructionIdHandle, 2276 IrInstructionIdAlignOf, 2277 IrInstructionIdOverflowOp, 2278 IrInstructionIdTestErrSrc, 2279 IrInstructionIdTestErrGen, 2280 IrInstructionIdMulAdd, 2281 IrInstructionIdFloatOp, 2282 IrInstructionIdUnwrapErrCode, 2283 IrInstructionIdUnwrapErrPayload, 2284 IrInstructionIdErrWrapCode, 2285 IrInstructionIdErrWrapPayload, 2286 IrInstructionIdFnProto, 2287 IrInstructionIdTestComptime, 2288 IrInstructionIdPtrCastSrc, 2289 IrInstructionIdPtrCastGen, 2290 IrInstructionIdBitCastSrc, 2291 IrInstructionIdBitCastGen, 2292 IrInstructionIdWidenOrShorten, 2293 IrInstructionIdIntToPtr, 2294 IrInstructionIdPtrToInt, 2295 IrInstructionIdIntToEnum, 2296 IrInstructionIdEnumToInt, 2297 IrInstructionIdIntToErr, 2298 IrInstructionIdErrToInt, 2299 IrInstructionIdCheckSwitchProngs, 2300 IrInstructionIdCheckStatementIsVoid, 2301 IrInstructionIdTypeName, 2302 IrInstructionIdDeclRef, 2303 IrInstructionIdPanic, 2304 IrInstructionIdTagName, 2305 IrInstructionIdTagType, 2306 IrInstructionIdFieldParentPtr, 2307 IrInstructionIdByteOffsetOf, 2308 IrInstructionIdBitOffsetOf, 2309 IrInstructionIdTypeInfo, 2310 IrInstructionIdTypeId, 2311 IrInstructionIdSetEvalBranchQuota, 2312 IrInstructionIdPtrType, 2313 IrInstructionIdAlignCast, 2314 IrInstructionIdImplicitCast, 2315 IrInstructionIdResolveResult, 2316 IrInstructionIdResetResult, 2317 IrInstructionIdResultPtr, 2318 IrInstructionIdOpaqueType, 2319 IrInstructionIdSetAlignStack, 2320 IrInstructionIdArgType, 2321 IrInstructionIdExport, 2322 IrInstructionIdErrorReturnTrace, 2323 IrInstructionIdErrorUnion, 2324 IrInstructionIdCancel, 2325 IrInstructionIdGetImplicitAllocator, 2326 IrInstructionIdCoroId, 2327 IrInstructionIdCoroAlloc, 2328 IrInstructionIdCoroSize, 2329 IrInstructionIdCoroBegin, 2330 IrInstructionIdCoroAllocFail, 2331 IrInstructionIdCoroSuspend, 2332 IrInstructionIdCoroEnd, 2333 IrInstructionIdCoroFree, 2334 IrInstructionIdCoroResume, 2335 IrInstructionIdCoroSave, 2336 IrInstructionIdCoroPromise, 2337 IrInstructionIdCoroAllocHelper, 2338 IrInstructionIdAtomicRmw, 2339 IrInstructionIdAtomicLoad, 2340 IrInstructionIdPromiseResultType, 2341 IrInstructionIdAwaitBookkeeping, 2342 IrInstructionIdSaveErrRetAddr, 2343 IrInstructionIdAddImplicitReturnType, 2344 IrInstructionIdMergeErrRetTraces, 2345 IrInstructionIdMarkErrRetTracePtr, 2346 IrInstructionIdErrSetCast, 2347 IrInstructionIdToBytes, 2348 IrInstructionIdFromBytes, 2349 IrInstructionIdCheckRuntimeScope, 2350 IrInstructionIdVectorToArray, 2351 IrInstructionIdArrayToVector, 2352 IrInstructionIdAssertZero, 2353 IrInstructionIdAssertNonNull, 2354 IrInstructionIdHasDecl, 2355 IrInstructionIdUndeclaredIdent, 2356 IrInstructionIdAllocaSrc, 2357 IrInstructionIdAllocaGen, 2358 IrInstructionIdEndExpr, 2359 IrInstructionIdPtrOfArrayToSlice, 2360 }; 2361 2362 struct IrInstruction { 2363 Scope *scope; 2364 AstNode *source_node; 2365 ConstExprValue value; 2366 size_t debug_id; 2367 LLVMValueRef llvm_value; 2368 // if ref_count is zero and the instruction has no side effects, 2369 // the instruction can be omitted in codegen 2370 size_t ref_count; 2371 // When analyzing IR, instructions that point to this instruction in the "old ir" 2372 // can find the instruction that corresponds to this value in the "new ir" 2373 // with this child field. 2374 IrInstruction *child; 2375 IrBasicBlock *owner_bb; 2376 IrInstructionId id; 2377 // true if this instruction was generated by zig and not from user code 2378 bool is_gen; 2379 }; 2380 2381 struct IrInstructionDeclVarSrc { 2382 IrInstruction base; 2383 2384 ZigVar *var; 2385 IrInstruction *var_type; 2386 IrInstruction *align_value; 2387 IrInstruction *ptr; 2388 }; 2389 2390 struct IrInstructionDeclVarGen { 2391 IrInstruction base; 2392 2393 ZigVar *var; 2394 IrInstruction *var_ptr; 2395 }; 2396 2397 struct IrInstructionCondBr { 2398 IrInstruction base; 2399 2400 IrInstruction *condition; 2401 IrBasicBlock *then_block; 2402 IrBasicBlock *else_block; 2403 IrInstruction *is_comptime; 2404 ResultLoc *result_loc; 2405 }; 2406 2407 struct IrInstructionBr { 2408 IrInstruction base; 2409 2410 IrBasicBlock *dest_block; 2411 IrInstruction *is_comptime; 2412 }; 2413 2414 struct IrInstructionSwitchBrCase { 2415 IrInstruction *value; 2416 IrBasicBlock *block; 2417 }; 2418 2419 struct IrInstructionSwitchBr { 2420 IrInstruction base; 2421 2422 IrInstruction *target_value; 2423 IrBasicBlock *else_block; 2424 size_t case_count; 2425 IrInstructionSwitchBrCase *cases; 2426 IrInstruction *is_comptime; 2427 IrInstruction *switch_prongs_void; 2428 }; 2429 2430 struct IrInstructionSwitchVar { 2431 IrInstruction base; 2432 2433 IrInstruction *target_value_ptr; 2434 IrInstruction **prongs_ptr; 2435 size_t prongs_len; 2436 }; 2437 2438 struct IrInstructionSwitchElseVar { 2439 IrInstruction base; 2440 2441 IrInstruction *target_value_ptr; 2442 IrInstructionSwitchBr *switch_br; 2443 }; 2444 2445 struct IrInstructionSwitchTarget { 2446 IrInstruction base; 2447 2448 IrInstruction *target_value_ptr; 2449 }; 2450 2451 struct IrInstructionPhi { 2452 IrInstruction base; 2453 2454 size_t incoming_count; 2455 IrBasicBlock **incoming_blocks; 2456 IrInstruction **incoming_values; 2457 ResultLocPeerParent *peer_parent; 2458 }; 2459 2460 enum IrUnOp { 2461 IrUnOpInvalid, 2462 IrUnOpBinNot, 2463 IrUnOpNegation, 2464 IrUnOpNegationWrap, 2465 IrUnOpDereference, 2466 IrUnOpOptional, 2467 }; 2468 2469 struct IrInstructionUnOp { 2470 IrInstruction base; 2471 2472 IrUnOp op_id; 2473 LVal lval; 2474 IrInstruction *value; 2475 ResultLoc *result_loc; 2476 }; 2477 2478 enum IrBinOp { 2479 IrBinOpInvalid, 2480 IrBinOpBoolOr, 2481 IrBinOpBoolAnd, 2482 IrBinOpCmpEq, 2483 IrBinOpCmpNotEq, 2484 IrBinOpCmpLessThan, 2485 IrBinOpCmpGreaterThan, 2486 IrBinOpCmpLessOrEq, 2487 IrBinOpCmpGreaterOrEq, 2488 IrBinOpBinOr, 2489 IrBinOpBinXor, 2490 IrBinOpBinAnd, 2491 IrBinOpBitShiftLeftLossy, 2492 IrBinOpBitShiftLeftExact, 2493 IrBinOpBitShiftRightLossy, 2494 IrBinOpBitShiftRightExact, 2495 IrBinOpAdd, 2496 IrBinOpAddWrap, 2497 IrBinOpSub, 2498 IrBinOpSubWrap, 2499 IrBinOpMult, 2500 IrBinOpMultWrap, 2501 IrBinOpDivUnspecified, 2502 IrBinOpDivExact, 2503 IrBinOpDivTrunc, 2504 IrBinOpDivFloor, 2505 IrBinOpRemUnspecified, 2506 IrBinOpRemRem, 2507 IrBinOpRemMod, 2508 IrBinOpArrayCat, 2509 IrBinOpArrayMult, 2510 IrBinOpMergeErrorSets, 2511 }; 2512 2513 struct IrInstructionBinOp { 2514 IrInstruction base; 2515 2516 IrInstruction *op1; 2517 IrInstruction *op2; 2518 IrBinOp op_id; 2519 bool safety_check_on; 2520 }; 2521 2522 struct IrInstructionLoadPtr { 2523 IrInstruction base; 2524 2525 IrInstruction *ptr; 2526 }; 2527 2528 struct IrInstructionLoadPtrGen { 2529 IrInstruction base; 2530 2531 IrInstruction *ptr; 2532 IrInstruction *result_loc; 2533 }; 2534 2535 struct IrInstructionStorePtr { 2536 IrInstruction base; 2537 2538 IrInstruction *ptr; 2539 IrInstruction *value; 2540 }; 2541 2542 struct IrInstructionFieldPtr { 2543 IrInstruction base; 2544 2545 bool initializing; 2546 IrInstruction *container_ptr; 2547 Buf *field_name_buffer; 2548 IrInstruction *field_name_expr; 2549 }; 2550 2551 struct IrInstructionStructFieldPtr { 2552 IrInstruction base; 2553 2554 IrInstruction *struct_ptr; 2555 TypeStructField *field; 2556 bool is_const; 2557 }; 2558 2559 struct IrInstructionUnionFieldPtr { 2560 IrInstruction base; 2561 2562 bool safety_check_on; 2563 bool initializing; 2564 IrInstruction *union_ptr; 2565 TypeUnionField *field; 2566 }; 2567 2568 struct IrInstructionElemPtr { 2569 IrInstruction base; 2570 2571 IrInstruction *array_ptr; 2572 IrInstruction *elem_index; 2573 IrInstruction *init_array_type; 2574 PtrLen ptr_len; 2575 bool safety_check_on; 2576 }; 2577 2578 struct IrInstructionVarPtr { 2579 IrInstruction base; 2580 2581 ZigVar *var; 2582 ScopeFnDef *crossed_fndef_scope; 2583 }; 2584 2585 // For functions that have a return type for which handle_is_ptr is true, a 2586 // result location pointer is the secret first parameter ("sret"). This 2587 // instruction returns that pointer. 2588 struct IrInstructionReturnPtr { 2589 IrInstruction base; 2590 }; 2591 2592 struct IrInstructionCallSrc { 2593 IrInstruction base; 2594 2595 IrInstruction *fn_ref; 2596 ZigFn *fn_entry; 2597 size_t arg_count; 2598 IrInstruction **args; 2599 ResultLoc *result_loc; 2600 2601 IrInstruction *async_allocator; 2602 IrInstruction *new_stack; 2603 FnInline fn_inline; 2604 bool is_async; 2605 bool is_comptime; 2606 }; 2607 2608 struct IrInstructionCallGen { 2609 IrInstruction base; 2610 2611 IrInstruction *fn_ref; 2612 ZigFn *fn_entry; 2613 size_t arg_count; 2614 IrInstruction **args; 2615 IrInstruction *result_loc; 2616 2617 IrInstruction *async_allocator; 2618 IrInstruction *new_stack; 2619 FnInline fn_inline; 2620 bool is_async; 2621 }; 2622 2623 struct IrInstructionConst { 2624 IrInstruction base; 2625 }; 2626 2627 // When an IrExecutable is not in a function, a return instruction means that 2628 // the expression returns with that value, even though a return statement from 2629 // an AST perspective is invalid. 2630 struct IrInstructionReturn { 2631 IrInstruction base; 2632 2633 IrInstruction *value; 2634 }; 2635 2636 enum CastOp { 2637 CastOpNoCast, // signifies the function call expression is not a cast 2638 CastOpNoop, // fn call expr is a cast, but does nothing 2639 CastOpIntToFloat, 2640 CastOpFloatToInt, 2641 CastOpBoolToInt, 2642 CastOpNumLitToConcrete, 2643 CastOpErrSet, 2644 CastOpBitCast, 2645 }; 2646 2647 // TODO get rid of this instruction, replace with instructions for each op code 2648 struct IrInstructionCast { 2649 IrInstruction base; 2650 2651 IrInstruction *value; 2652 ZigType *dest_type; 2653 CastOp cast_op; 2654 }; 2655 2656 struct IrInstructionResizeSlice { 2657 IrInstruction base; 2658 2659 IrInstruction *operand; 2660 IrInstruction *result_loc; 2661 }; 2662 2663 struct IrInstructionContainerInitList { 2664 IrInstruction base; 2665 2666 IrInstruction *container_type; 2667 IrInstruction *elem_type; 2668 size_t item_count; 2669 IrInstruction **elem_result_loc_list; 2670 IrInstruction *result_loc; 2671 }; 2672 2673 struct IrInstructionContainerInitFieldsField { 2674 Buf *name; 2675 AstNode *source_node; 2676 TypeStructField *type_struct_field; 2677 IrInstruction *result_loc; 2678 }; 2679 2680 struct IrInstructionContainerInitFields { 2681 IrInstruction base; 2682 2683 IrInstruction *container_type; 2684 size_t field_count; 2685 IrInstructionContainerInitFieldsField *fields; 2686 IrInstruction *result_loc; 2687 }; 2688 2689 struct IrInstructionUnreachable { 2690 IrInstruction base; 2691 }; 2692 2693 struct IrInstructionTypeOf { 2694 IrInstruction base; 2695 2696 IrInstruction *value; 2697 }; 2698 2699 struct IrInstructionSetCold { 2700 IrInstruction base; 2701 2702 IrInstruction *is_cold; 2703 }; 2704 2705 struct IrInstructionSetRuntimeSafety { 2706 IrInstruction base; 2707 2708 IrInstruction *safety_on; 2709 }; 2710 2711 struct IrInstructionSetFloatMode { 2712 IrInstruction base; 2713 2714 IrInstruction *scope_value; 2715 IrInstruction *mode_value; 2716 }; 2717 2718 struct IrInstructionArrayType { 2719 IrInstruction base; 2720 2721 IrInstruction *size; 2722 IrInstruction *child_type; 2723 }; 2724 2725 struct IrInstructionPtrType { 2726 IrInstruction base; 2727 2728 IrInstruction *align_value; 2729 IrInstruction *child_type; 2730 uint32_t bit_offset_start; 2731 uint32_t host_int_bytes; 2732 PtrLen ptr_len; 2733 bool is_const; 2734 bool is_volatile; 2735 bool is_allow_zero; 2736 }; 2737 2738 struct IrInstructionPromiseType { 2739 IrInstruction base; 2740 2741 IrInstruction *payload_type; 2742 }; 2743 2744 struct IrInstructionSliceType { 2745 IrInstruction base; 2746 2747 IrInstruction *align_value; 2748 IrInstruction *child_type; 2749 bool is_const; 2750 bool is_volatile; 2751 bool is_allow_zero; 2752 }; 2753 2754 struct IrInstructionGlobalAsm { 2755 IrInstruction base; 2756 2757 Buf *asm_code; 2758 }; 2759 2760 struct IrInstructionAsm { 2761 IrInstruction base; 2762 2763 Buf *asm_template; 2764 AsmToken *token_list; 2765 size_t token_list_len; 2766 IrInstruction **input_list; 2767 IrInstruction **output_types; 2768 ZigVar **output_vars; 2769 size_t return_count; 2770 bool has_side_effects; 2771 }; 2772 2773 struct IrInstructionSizeOf { 2774 IrInstruction base; 2775 2776 IrInstruction *type_value; 2777 }; 2778 2779 // returns true if nonnull, returns false if null 2780 // this is so that `zeroes` sets maybe values to null 2781 struct IrInstructionTestNonNull { 2782 IrInstruction base; 2783 2784 IrInstruction *value; 2785 }; 2786 2787 // Takes a pointer to an optional value, returns a pointer 2788 // to the payload. 2789 struct IrInstructionOptionalUnwrapPtr { 2790 IrInstruction base; 2791 2792 bool safety_check_on; 2793 bool initializing; 2794 IrInstruction *base_ptr; 2795 }; 2796 2797 struct IrInstructionCtz { 2798 IrInstruction base; 2799 2800 IrInstruction *type; 2801 IrInstruction *op; 2802 }; 2803 2804 struct IrInstructionClz { 2805 IrInstruction base; 2806 2807 IrInstruction *type; 2808 IrInstruction *op; 2809 }; 2810 2811 struct IrInstructionPopCount { 2812 IrInstruction base; 2813 2814 IrInstruction *type; 2815 IrInstruction *op; 2816 }; 2817 2818 struct IrInstructionUnionTag { 2819 IrInstruction base; 2820 2821 IrInstruction *value; 2822 }; 2823 2824 struct IrInstructionImport { 2825 IrInstruction base; 2826 2827 IrInstruction *name; 2828 }; 2829 2830 struct IrInstructionRef { 2831 IrInstruction base; 2832 2833 IrInstruction *value; 2834 bool is_const; 2835 bool is_volatile; 2836 }; 2837 2838 struct IrInstructionRefGen { 2839 IrInstruction base; 2840 2841 IrInstruction *operand; 2842 IrInstruction *result_loc; 2843 }; 2844 2845 struct IrInstructionCompileErr { 2846 IrInstruction base; 2847 2848 IrInstruction *msg; 2849 }; 2850 2851 struct IrInstructionCompileLog { 2852 IrInstruction base; 2853 2854 size_t msg_count; 2855 IrInstruction **msg_list; 2856 }; 2857 2858 struct IrInstructionErrName { 2859 IrInstruction base; 2860 2861 IrInstruction *value; 2862 }; 2863 2864 struct IrInstructionCImport { 2865 IrInstruction base; 2866 }; 2867 2868 struct IrInstructionCInclude { 2869 IrInstruction base; 2870 2871 IrInstruction *name; 2872 }; 2873 2874 struct IrInstructionCDefine { 2875 IrInstruction base; 2876 2877 IrInstruction *name; 2878 IrInstruction *value; 2879 }; 2880 2881 struct IrInstructionCUndef { 2882 IrInstruction base; 2883 2884 IrInstruction *name; 2885 }; 2886 2887 struct IrInstructionEmbedFile { 2888 IrInstruction base; 2889 2890 IrInstruction *name; 2891 }; 2892 2893 struct IrInstructionCmpxchgSrc { 2894 IrInstruction base; 2895 2896 bool is_weak; 2897 IrInstruction *type_value; 2898 IrInstruction *ptr; 2899 IrInstruction *cmp_value; 2900 IrInstruction *new_value; 2901 IrInstruction *success_order_value; 2902 IrInstruction *failure_order_value; 2903 ResultLoc *result_loc; 2904 }; 2905 2906 struct IrInstructionCmpxchgGen { 2907 IrInstruction base; 2908 2909 bool is_weak; 2910 AtomicOrder success_order; 2911 AtomicOrder failure_order; 2912 IrInstruction *ptr; 2913 IrInstruction *cmp_value; 2914 IrInstruction *new_value; 2915 IrInstruction *result_loc; 2916 }; 2917 2918 struct IrInstructionFence { 2919 IrInstruction base; 2920 2921 IrInstruction *order_value; 2922 2923 // if this instruction gets to runtime then we know these values: 2924 AtomicOrder order; 2925 }; 2926 2927 struct IrInstructionTruncate { 2928 IrInstruction base; 2929 2930 IrInstruction *dest_type; 2931 IrInstruction *target; 2932 }; 2933 2934 struct IrInstructionIntCast { 2935 IrInstruction base; 2936 2937 IrInstruction *dest_type; 2938 IrInstruction *target; 2939 }; 2940 2941 struct IrInstructionFloatCast { 2942 IrInstruction base; 2943 2944 IrInstruction *dest_type; 2945 IrInstruction *target; 2946 }; 2947 2948 struct IrInstructionErrSetCast { 2949 IrInstruction base; 2950 2951 IrInstruction *dest_type; 2952 IrInstruction *target; 2953 }; 2954 2955 struct IrInstructionToBytes { 2956 IrInstruction base; 2957 2958 IrInstruction *target; 2959 ResultLoc *result_loc; 2960 }; 2961 2962 struct IrInstructionFromBytes { 2963 IrInstruction base; 2964 2965 IrInstruction *dest_child_type; 2966 IrInstruction *target; 2967 ResultLoc *result_loc; 2968 }; 2969 2970 struct IrInstructionIntToFloat { 2971 IrInstruction base; 2972 2973 IrInstruction *dest_type; 2974 IrInstruction *target; 2975 }; 2976 2977 struct IrInstructionFloatToInt { 2978 IrInstruction base; 2979 2980 IrInstruction *dest_type; 2981 IrInstruction *target; 2982 }; 2983 2984 struct IrInstructionBoolToInt { 2985 IrInstruction base; 2986 2987 IrInstruction *target; 2988 }; 2989 2990 struct IrInstructionIntType { 2991 IrInstruction base; 2992 2993 IrInstruction *is_signed; 2994 IrInstruction *bit_count; 2995 }; 2996 2997 struct IrInstructionVectorType { 2998 IrInstruction base; 2999 3000 IrInstruction *len; 3001 IrInstruction *elem_type; 3002 }; 3003 3004 struct IrInstructionBoolNot { 3005 IrInstruction base; 3006 3007 IrInstruction *value; 3008 }; 3009 3010 struct IrInstructionMemset { 3011 IrInstruction base; 3012 3013 IrInstruction *dest_ptr; 3014 IrInstruction *byte; 3015 IrInstruction *count; 3016 }; 3017 3018 struct IrInstructionMemcpy { 3019 IrInstruction base; 3020 3021 IrInstruction *dest_ptr; 3022 IrInstruction *src_ptr; 3023 IrInstruction *count; 3024 }; 3025 3026 struct IrInstructionSliceSrc { 3027 IrInstruction base; 3028 3029 bool safety_check_on; 3030 IrInstruction *ptr; 3031 IrInstruction *start; 3032 IrInstruction *end; 3033 ResultLoc *result_loc; 3034 }; 3035 3036 struct IrInstructionSliceGen { 3037 IrInstruction base; 3038 3039 bool safety_check_on; 3040 IrInstruction *ptr; 3041 IrInstruction *start; 3042 IrInstruction *end; 3043 IrInstruction *result_loc; 3044 }; 3045 3046 struct IrInstructionMemberCount { 3047 IrInstruction base; 3048 3049 IrInstruction *container; 3050 }; 3051 3052 struct IrInstructionMemberType { 3053 IrInstruction base; 3054 3055 IrInstruction *container_type; 3056 IrInstruction *member_index; 3057 }; 3058 3059 struct IrInstructionMemberName { 3060 IrInstruction base; 3061 3062 IrInstruction *container_type; 3063 IrInstruction *member_index; 3064 }; 3065 3066 struct IrInstructionBreakpoint { 3067 IrInstruction base; 3068 }; 3069 3070 struct IrInstructionReturnAddress { 3071 IrInstruction base; 3072 }; 3073 3074 struct IrInstructionFrameAddress { 3075 IrInstruction base; 3076 }; 3077 3078 struct IrInstructionHandle { 3079 IrInstruction base; 3080 }; 3081 3082 enum IrOverflowOp { 3083 IrOverflowOpAdd, 3084 IrOverflowOpSub, 3085 IrOverflowOpMul, 3086 IrOverflowOpShl, 3087 }; 3088 3089 struct IrInstructionOverflowOp { 3090 IrInstruction base; 3091 3092 IrOverflowOp op; 3093 IrInstruction *type_value; 3094 IrInstruction *op1; 3095 IrInstruction *op2; 3096 IrInstruction *result_ptr; 3097 3098 ZigType *result_ptr_type; 3099 }; 3100 3101 struct IrInstructionMulAdd { 3102 IrInstruction base; 3103 3104 IrInstruction *type_value; 3105 IrInstruction *op1; 3106 IrInstruction *op2; 3107 IrInstruction *op3; 3108 }; 3109 3110 struct IrInstructionAlignOf { 3111 IrInstruction base; 3112 3113 IrInstruction *type_value; 3114 }; 3115 3116 // returns true if error, returns false if not error 3117 struct IrInstructionTestErrSrc { 3118 IrInstruction base; 3119 3120 bool resolve_err_set; 3121 IrInstruction *base_ptr; 3122 }; 3123 3124 struct IrInstructionTestErrGen { 3125 IrInstruction base; 3126 3127 IrInstruction *err_union; 3128 }; 3129 3130 // Takes an error union pointer, returns a pointer to the error code. 3131 struct IrInstructionUnwrapErrCode { 3132 IrInstruction base; 3133 3134 bool initializing; 3135 IrInstruction *err_union_ptr; 3136 }; 3137 3138 struct IrInstructionUnwrapErrPayload { 3139 IrInstruction base; 3140 3141 bool safety_check_on; 3142 bool initializing; 3143 IrInstruction *value; 3144 }; 3145 3146 struct IrInstructionOptionalWrap { 3147 IrInstruction base; 3148 3149 IrInstruction *operand; 3150 IrInstruction *result_loc; 3151 }; 3152 3153 struct IrInstructionErrWrapPayload { 3154 IrInstruction base; 3155 3156 IrInstruction *operand; 3157 IrInstruction *result_loc; 3158 }; 3159 3160 struct IrInstructionErrWrapCode { 3161 IrInstruction base; 3162 3163 IrInstruction *operand; 3164 IrInstruction *result_loc; 3165 }; 3166 3167 struct IrInstructionFnProto { 3168 IrInstruction base; 3169 3170 IrInstruction **param_types; 3171 IrInstruction *align_value; 3172 IrInstruction *return_type; 3173 IrInstruction *async_allocator_type_value; 3174 bool is_var_args; 3175 }; 3176 3177 // true if the target value is compile time known, false otherwise 3178 struct IrInstructionTestComptime { 3179 IrInstruction base; 3180 3181 IrInstruction *value; 3182 }; 3183 3184 struct IrInstructionPtrCastSrc { 3185 IrInstruction base; 3186 3187 IrInstruction *dest_type; 3188 IrInstruction *ptr; 3189 bool safety_check_on; 3190 }; 3191 3192 struct IrInstructionPtrCastGen { 3193 IrInstruction base; 3194 3195 IrInstruction *ptr; 3196 bool safety_check_on; 3197 }; 3198 3199 struct IrInstructionBitCastSrc { 3200 IrInstruction base; 3201 3202 IrInstruction *operand; 3203 ResultLocBitCast *result_loc_bit_cast; 3204 }; 3205 3206 struct IrInstructionBitCastGen { 3207 IrInstruction base; 3208 3209 IrInstruction *operand; 3210 }; 3211 3212 struct IrInstructionWidenOrShorten { 3213 IrInstruction base; 3214 3215 IrInstruction *target; 3216 }; 3217 3218 struct IrInstructionPtrToInt { 3219 IrInstruction base; 3220 3221 IrInstruction *target; 3222 }; 3223 3224 struct IrInstructionIntToPtr { 3225 IrInstruction base; 3226 3227 IrInstruction *dest_type; 3228 IrInstruction *target; 3229 }; 3230 3231 struct IrInstructionIntToEnum { 3232 IrInstruction base; 3233 3234 IrInstruction *dest_type; 3235 IrInstruction *target; 3236 }; 3237 3238 struct IrInstructionEnumToInt { 3239 IrInstruction base; 3240 3241 IrInstruction *target; 3242 }; 3243 3244 struct IrInstructionIntToErr { 3245 IrInstruction base; 3246 3247 IrInstruction *target; 3248 }; 3249 3250 struct IrInstructionErrToInt { 3251 IrInstruction base; 3252 3253 IrInstruction *target; 3254 }; 3255 3256 struct IrInstructionCheckSwitchProngsRange { 3257 IrInstruction *start; 3258 IrInstruction *end; 3259 }; 3260 3261 struct IrInstructionCheckSwitchProngs { 3262 IrInstruction base; 3263 3264 IrInstruction *target_value; 3265 IrInstructionCheckSwitchProngsRange *ranges; 3266 size_t range_count; 3267 bool have_else_prong; 3268 }; 3269 3270 struct IrInstructionCheckStatementIsVoid { 3271 IrInstruction base; 3272 3273 IrInstruction *statement_value; 3274 }; 3275 3276 struct IrInstructionTypeName { 3277 IrInstruction base; 3278 3279 IrInstruction *type_value; 3280 }; 3281 3282 struct IrInstructionDeclRef { 3283 IrInstruction base; 3284 3285 LVal lval; 3286 Tld *tld; 3287 }; 3288 3289 struct IrInstructionPanic { 3290 IrInstruction base; 3291 3292 IrInstruction *msg; 3293 }; 3294 3295 struct IrInstructionTagName { 3296 IrInstruction base; 3297 3298 IrInstruction *target; 3299 }; 3300 3301 struct IrInstructionTagType { 3302 IrInstruction base; 3303 3304 IrInstruction *target; 3305 }; 3306 3307 struct IrInstructionFieldParentPtr { 3308 IrInstruction base; 3309 3310 IrInstruction *type_value; 3311 IrInstruction *field_name; 3312 IrInstruction *field_ptr; 3313 TypeStructField *field; 3314 }; 3315 3316 struct IrInstructionByteOffsetOf { 3317 IrInstruction base; 3318 3319 IrInstruction *type_value; 3320 IrInstruction *field_name; 3321 }; 3322 3323 struct IrInstructionBitOffsetOf { 3324 IrInstruction base; 3325 3326 IrInstruction *type_value; 3327 IrInstruction *field_name; 3328 }; 3329 3330 struct IrInstructionTypeInfo { 3331 IrInstruction base; 3332 3333 IrInstruction *type_value; 3334 }; 3335 3336 struct IrInstructionTypeId { 3337 IrInstruction base; 3338 3339 IrInstruction *type_value; 3340 }; 3341 3342 struct IrInstructionSetEvalBranchQuota { 3343 IrInstruction base; 3344 3345 IrInstruction *new_quota; 3346 }; 3347 3348 struct IrInstructionAlignCast { 3349 IrInstruction base; 3350 3351 IrInstruction *align_bytes; 3352 IrInstruction *target; 3353 }; 3354 3355 struct IrInstructionOpaqueType { 3356 IrInstruction base; 3357 }; 3358 3359 struct IrInstructionSetAlignStack { 3360 IrInstruction base; 3361 3362 IrInstruction *align_bytes; 3363 }; 3364 3365 struct IrInstructionArgType { 3366 IrInstruction base; 3367 3368 IrInstruction *fn_type; 3369 IrInstruction *arg_index; 3370 }; 3371 3372 struct IrInstructionExport { 3373 IrInstruction base; 3374 3375 IrInstruction *name; 3376 IrInstruction *linkage; 3377 IrInstruction *target; 3378 }; 3379 3380 struct IrInstructionErrorReturnTrace { 3381 IrInstruction base; 3382 3383 enum Optional { 3384 Null, 3385 NonNull, 3386 } optional; 3387 }; 3388 3389 struct IrInstructionErrorUnion { 3390 IrInstruction base; 3391 3392 IrInstruction *err_set; 3393 IrInstruction *payload; 3394 }; 3395 3396 struct IrInstructionCancel { 3397 IrInstruction base; 3398 3399 IrInstruction *target; 3400 }; 3401 3402 enum ImplicitAllocatorId { 3403 ImplicitAllocatorIdArg, 3404 ImplicitAllocatorIdLocalVar, 3405 }; 3406 3407 struct IrInstructionGetImplicitAllocator { 3408 IrInstruction base; 3409 3410 ImplicitAllocatorId id; 3411 }; 3412 3413 struct IrInstructionCoroId { 3414 IrInstruction base; 3415 3416 IrInstruction *promise_ptr; 3417 }; 3418 3419 struct IrInstructionCoroAlloc { 3420 IrInstruction base; 3421 3422 IrInstruction *coro_id; 3423 }; 3424 3425 struct IrInstructionCoroSize { 3426 IrInstruction base; 3427 }; 3428 3429 struct IrInstructionCoroBegin { 3430 IrInstruction base; 3431 3432 IrInstruction *coro_id; 3433 IrInstruction *coro_mem_ptr; 3434 }; 3435 3436 struct IrInstructionCoroAllocFail { 3437 IrInstruction base; 3438 3439 IrInstruction *err_val; 3440 }; 3441 3442 struct IrInstructionCoroSuspend { 3443 IrInstruction base; 3444 3445 IrInstruction *save_point; 3446 IrInstruction *is_final; 3447 }; 3448 3449 struct IrInstructionCoroEnd { 3450 IrInstruction base; 3451 }; 3452 3453 struct IrInstructionCoroFree { 3454 IrInstruction base; 3455 3456 IrInstruction *coro_id; 3457 IrInstruction *coro_handle; 3458 }; 3459 3460 struct IrInstructionCoroResume { 3461 IrInstruction base; 3462 3463 IrInstruction *awaiter_handle; 3464 }; 3465 3466 struct IrInstructionCoroSave { 3467 IrInstruction base; 3468 3469 IrInstruction *coro_handle; 3470 }; 3471 3472 struct IrInstructionCoroPromise { 3473 IrInstruction base; 3474 3475 IrInstruction *coro_handle; 3476 }; 3477 3478 struct IrInstructionCoroAllocHelper { 3479 IrInstruction base; 3480 3481 IrInstruction *realloc_fn; 3482 IrInstruction *coro_size; 3483 }; 3484 3485 struct IrInstructionAtomicRmw { 3486 IrInstruction base; 3487 3488 IrInstruction *operand_type; 3489 IrInstruction *ptr; 3490 IrInstruction *op; 3491 AtomicRmwOp resolved_op; 3492 IrInstruction *operand; 3493 IrInstruction *ordering; 3494 AtomicOrder resolved_ordering; 3495 }; 3496 3497 struct IrInstructionAtomicLoad { 3498 IrInstruction base; 3499 3500 IrInstruction *operand_type; 3501 IrInstruction *ptr; 3502 IrInstruction *ordering; 3503 AtomicOrder resolved_ordering; 3504 }; 3505 3506 struct IrInstructionPromiseResultType { 3507 IrInstruction base; 3508 3509 IrInstruction *promise_type; 3510 }; 3511 3512 struct IrInstructionAwaitBookkeeping { 3513 IrInstruction base; 3514 3515 IrInstruction *promise_result_type; 3516 }; 3517 3518 struct IrInstructionSaveErrRetAddr { 3519 IrInstruction base; 3520 }; 3521 3522 struct IrInstructionAddImplicitReturnType { 3523 IrInstruction base; 3524 3525 IrInstruction *value; 3526 }; 3527 3528 struct IrInstructionMergeErrRetTraces { 3529 IrInstruction base; 3530 3531 IrInstruction *coro_promise_ptr; 3532 IrInstruction *src_err_ret_trace_ptr; 3533 IrInstruction *dest_err_ret_trace_ptr; 3534 }; 3535 3536 struct IrInstructionMarkErrRetTracePtr { 3537 IrInstruction base; 3538 3539 IrInstruction *err_ret_trace_ptr; 3540 }; 3541 3542 // For float ops which take a single argument 3543 struct IrInstructionFloatOp { 3544 IrInstruction base; 3545 3546 BuiltinFnId op; 3547 IrInstruction *type; 3548 IrInstruction *op1; 3549 }; 3550 3551 struct IrInstructionCheckRuntimeScope { 3552 IrInstruction base; 3553 3554 IrInstruction *scope_is_comptime; 3555 IrInstruction *is_comptime; 3556 }; 3557 3558 struct IrInstructionBswap { 3559 IrInstruction base; 3560 3561 IrInstruction *type; 3562 IrInstruction *op; 3563 }; 3564 3565 struct IrInstructionBitReverse { 3566 IrInstruction base; 3567 3568 IrInstruction *type; 3569 IrInstruction *op; 3570 }; 3571 3572 struct IrInstructionArrayToVector { 3573 IrInstruction base; 3574 3575 IrInstruction *array; 3576 }; 3577 3578 struct IrInstructionVectorToArray { 3579 IrInstruction base; 3580 3581 IrInstruction *vector; 3582 IrInstruction *result_loc; 3583 }; 3584 3585 struct IrInstructionAssertZero { 3586 IrInstruction base; 3587 3588 IrInstruction *target; 3589 }; 3590 3591 struct IrInstructionAssertNonNull { 3592 IrInstruction base; 3593 3594 IrInstruction *target; 3595 }; 3596 3597 struct IrInstructionHasDecl { 3598 IrInstruction base; 3599 3600 IrInstruction *container; 3601 IrInstruction *name; 3602 }; 3603 3604 struct IrInstructionUndeclaredIdent { 3605 IrInstruction base; 3606 3607 Buf *name; 3608 }; 3609 3610 struct IrInstructionAllocaSrc { 3611 IrInstruction base; 3612 3613 IrInstruction *align; 3614 IrInstruction *is_comptime; 3615 const char *name_hint; 3616 }; 3617 3618 struct IrInstructionAllocaGen { 3619 IrInstruction base; 3620 3621 uint32_t align; 3622 const char *name_hint; 3623 }; 3624 3625 struct IrInstructionEndExpr { 3626 IrInstruction base; 3627 3628 IrInstruction *value; 3629 ResultLoc *result_loc; 3630 }; 3631 3632 struct IrInstructionImplicitCast { 3633 IrInstruction base; 3634 3635 IrInstruction *dest_type; 3636 IrInstruction *target; 3637 ResultLoc *result_loc; 3638 }; 3639 3640 // This one is for writing through the result pointer. 3641 struct IrInstructionResolveResult { 3642 IrInstruction base; 3643 3644 ResultLoc *result_loc; 3645 IrInstruction *ty; 3646 }; 3647 3648 // This one is when you want to read the value of the result. 3649 // You have to give the value in case it is comptime. 3650 struct IrInstructionResultPtr { 3651 IrInstruction base; 3652 3653 ResultLoc *result_loc; 3654 IrInstruction *result; 3655 }; 3656 3657 struct IrInstructionResetResult { 3658 IrInstruction base; 3659 3660 ResultLoc *result_loc; 3661 }; 3662 3663 struct IrInstructionPtrOfArrayToSlice { 3664 IrInstruction base; 3665 3666 IrInstruction *operand; 3667 IrInstruction *result_loc; 3668 }; 3669 3670 enum ResultLocId { 3671 ResultLocIdInvalid, 3672 ResultLocIdNone, 3673 ResultLocIdVar, 3674 ResultLocIdReturn, 3675 ResultLocIdPeer, 3676 ResultLocIdPeerParent, 3677 ResultLocIdInstruction, 3678 ResultLocIdBitCast, 3679 }; 3680 3681 // Additions to this struct may need to be handled in 3682 // ir_reset_result 3683 struct ResultLoc { 3684 ResultLocId id; 3685 bool written; 3686 IrInstruction *resolved_loc; // result ptr 3687 IrInstruction *source_instruction; 3688 IrInstruction *gen_instruction; // value to store to the result loc 3689 ZigType *implicit_elem_type; 3690 }; 3691 3692 struct ResultLocNone { 3693 ResultLoc base; 3694 }; 3695 3696 struct ResultLocVar { 3697 ResultLoc base; 3698 3699 ZigVar *var; 3700 }; 3701 3702 struct ResultLocReturn { 3703 ResultLoc base; 3704 }; 3705 3706 struct IrSuspendPosition { 3707 size_t basic_block_index; 3708 size_t instruction_index; 3709 }; 3710 3711 struct ResultLocPeerParent { 3712 ResultLoc base; 3713 3714 bool skipped; 3715 bool done_resuming; 3716 IrBasicBlock *end_bb; 3717 ResultLoc *parent; 3718 ZigList<ResultLocPeer *> peers; 3719 ZigType *resolved_type; 3720 IrInstruction *is_comptime; 3721 }; 3722 3723 struct ResultLocPeer { 3724 ResultLoc base; 3725 3726 ResultLocPeerParent *parent; 3727 IrBasicBlock *next_bb; 3728 IrSuspendPosition suspend_pos; 3729 }; 3730 3731 // The result location is the source instruction 3732 struct ResultLocInstruction { 3733 ResultLoc base; 3734 }; 3735 3736 // The source_instruction is the destination type 3737 struct ResultLocBitCast { 3738 ResultLoc base; 3739 3740 ResultLoc *parent; 3741 }; 3742 3743 static const size_t slice_ptr_index = 0; 3744 static const size_t slice_len_index = 1; 3745 3746 static const size_t maybe_child_index = 0; 3747 static const size_t maybe_null_index = 1; 3748 3749 static const size_t err_union_err_index = 0; 3750 static const size_t err_union_payload_index = 1; 3751 3752 // TODO call graph analysis to find out what this number needs to be for every function 3753 // MUST BE A POWER OF TWO. 3754 static const size_t stack_trace_ptr_count = 32; 3755 3756 // these belong to the async function 3757 #define RETURN_ADDRESSES_FIELD_NAME "return_addresses" 3758 #define ERR_RET_TRACE_FIELD_NAME "err_ret_trace" 3759 #define RESULT_FIELD_NAME "result" 3760 #define ASYNC_REALLOC_FIELD_NAME "reallocFn" 3761 #define ASYNC_SHRINK_FIELD_NAME "shrinkFn" 3762 #define ATOMIC_STATE_FIELD_NAME "atomic_state" 3763 // these point to data belonging to the awaiter 3764 #define ERR_RET_TRACE_PTR_FIELD_NAME "err_ret_trace_ptr" 3765 #define RESULT_PTR_FIELD_NAME "result_ptr" 3766 3767 #define NAMESPACE_SEP_CHAR '.' 3768 #define NAMESPACE_SEP_STR "." 3769 3770 enum FloatMode { 3771 FloatModeStrict, 3772 FloatModeOptimized, 3773 }; 3774 3775 enum FnWalkId { 3776 FnWalkIdAttrs, 3777 FnWalkIdCall, 3778 FnWalkIdTypes, 3779 FnWalkIdVars, 3780 FnWalkIdInits, 3781 }; 3782 3783 struct FnWalkAttrs { 3784 ZigFn *fn; 3785 unsigned gen_i; 3786 }; 3787 3788 struct FnWalkCall { 3789 ZigList<LLVMValueRef> *gen_param_values; 3790 IrInstructionCallGen *inst; 3791 bool is_var_args; 3792 }; 3793 3794 struct FnWalkTypes { 3795 ZigList<ZigLLVMDIType *> *param_di_types; 3796 ZigList<LLVMTypeRef> *gen_param_types; 3797 }; 3798 3799 struct FnWalkVars { 3800 ZigType *import; 3801 LLVMValueRef llvm_fn; 3802 ZigFn *fn; 3803 ZigVar *var; 3804 unsigned gen_i; 3805 }; 3806 3807 struct FnWalkInits { 3808 LLVMValueRef llvm_fn; 3809 ZigFn *fn; 3810 unsigned gen_i; 3811 }; 3812 3813 struct FnWalk { 3814 FnWalkId id; 3815 union { 3816 FnWalkAttrs attrs; 3817 FnWalkCall call; 3818 FnWalkTypes types; 3819 FnWalkVars vars; 3820 FnWalkInits inits; 3821 } data; 3822 }; 3823 3824 #endif