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 "zig_llvm.h"
14 #include "hash_map.hpp"
15 #include "errmsg.hpp"
16 #include "bigint.hpp"
17 #include "bigfloat.hpp"
18 #include "target.hpp"
19 #include "tokenizer.hpp"
20 
21 struct AstNode;
22 struct ZigFn;
23 struct Scope;
24 struct ScopeBlock;
25 struct ScopeFnDef;
26 struct ScopeExpr;
27 struct ZigType;
28 struct ZigVar;
29 struct ErrorTableEntry;
30 struct BuiltinFnEntry;
31 struct TypeStructField;
32 struct CodeGen;
33 struct ZigValue;
34 struct Stage1ZirInst;
35 struct Stage1AirInst;
36 struct Stage1AirInstCast;
37 struct Stage1AirInstAlloca;
38 struct Stage1AirInstCall;
39 struct Stage1AirInstAwait;
40 struct Stage1ZirBasicBlock;
41 struct Stage1AirBasicBlock;
42 struct ScopeDecls;
43 struct ZigWindowsSDK;
44 struct Tld;
45 struct TldExport;
46 struct IrAnalyze;
47 struct ResultLoc;
48 struct ResultLocPeer;
49 struct ResultLocPeerParent;
50 struct ResultLocBitCast;
51 struct ResultLocCast;
52 struct ResultLocReturn;
53 struct Stage1Air;
54 
55 enum FileExt {
56     FileExtUnknown,
57     FileExtAsm,
58     FileExtC,
59     FileExtCpp,
60     FileExtHeader,
61     FileExtLLVMIr,
62     FileExtLLVMBitCode,
63 };
64 
65 enum PtrLen {
66     PtrLenUnknown,
67     PtrLenSingle,
68     PtrLenC,
69 };
70 
71 enum CallingConvention {
72     CallingConventionUnspecified,
73     CallingConventionC,
74     CallingConventionNaked,
75     CallingConventionAsync,
76     CallingConventionInline,
77     CallingConventionInterrupt,
78     CallingConventionSignal,
79     CallingConventionStdcall,
80     CallingConventionFastcall,
81     CallingConventionVectorcall,
82     CallingConventionThiscall,
83     CallingConventionAPCS,
84     CallingConventionAAPCS,
85     CallingConventionAAPCSVFP,
86     CallingConventionSysV
87 };
88 
89 // Stage 1 supports only the generic address space
90 enum AddressSpace {
91     AddressSpaceGeneric,
92     AddressSpaceGS,
93     AddressSpaceFS,
94     AddressSpaceSS,
95 };
96 
97 // This one corresponds to the builtin.zig enum.
98 enum BuiltinPtrSize {
99     BuiltinPtrSizeOne,
100     BuiltinPtrSizeMany,
101     BuiltinPtrSizeSlice,
102     BuiltinPtrSizeC,
103 };
104 
105 enum UndefAllowed {
106     UndefOk,
107     UndefBad,
108     LazyOkNoUndef,
109     LazyOk,
110 };
111 
112 enum X64CABIClass {
113     X64CABIClass_Unknown,
114     X64CABIClass_MEMORY,
115     X64CABIClass_MEMORY_nobyval,
116     X64CABIClass_INTEGER,
117     X64CABIClass_SSE,
118     X64CABIClass_AGG,
119 };
120 
121 struct Stage1Zir {
122     ZigList<Stage1ZirBasicBlock *> basic_block_list;
123     Buf *name;
124     ZigFn *name_fn;
125     Scope *begin_scope;
126     ErrorMsg *first_err_trace_msg;
127     ZigList<Tld *> tld_list;
128 
129     bool is_inline;
130     bool need_err_code_spill;
131 };
132 
133 struct Stage1Air {
134     ZigList<Stage1AirBasicBlock *> basic_block_list;
135     Buf *name;
136     ZigFn *name_fn;
137     size_t mem_slot_count;
138     size_t next_debug_id;
139     Buf *c_import_buf;
140     AstNode *source_node;
141     Stage1Air *parent_exec;
142     Stage1Zir *source_exec;
143     Scope *begin_scope;
144     ErrorMsg *first_err_trace_msg;
145     ZigList<Tld *> tld_list;
146 
147     bool is_inline;
148     bool need_err_code_spill;
149 
150     // This is a function for use in the debugger to print
151     // the source location.
152     void src();
153 };
154 
155 enum OutType {
156     OutTypeUnknown,
157     OutTypeExe,
158     OutTypeLib,
159     OutTypeObj,
160 };
161 
162 enum ConstParentId {
163     ConstParentIdNone,
164     ConstParentIdStruct,
165     ConstParentIdErrUnionCode,
166     ConstParentIdErrUnionPayload,
167     ConstParentIdOptionalPayload,
168     ConstParentIdArray,
169     ConstParentIdUnion,
170     ConstParentIdScalar,
171 };
172 
173 struct ConstParent {
174     ConstParentId id;
175 
176     union {
177         struct {
178             ZigValue *array_val;
179             size_t elem_index;
180         } p_array;
181         struct {
182             ZigValue *struct_val;
183             size_t field_index;
184         } p_struct;
185         struct {
186             ZigValue *err_union_val;
187         } p_err_union_code;
188         struct {
189             ZigValue *err_union_val;
190         } p_err_union_payload;
191         struct {
192             ZigValue *optional_val;
193         } p_optional_payload;
194         struct {
195             ZigValue *union_val;
196         } p_union;
197         struct {
198             ZigValue *scalar_val;
199         } p_scalar;
200     } data;
201 };
202 
203 struct ConstStructValue {
204     ZigValue **fields;
205 };
206 
207 struct ConstUnionValue {
208     BigInt tag;
209     ZigValue *payload;
210 };
211 
212 enum ConstArraySpecial {
213     ConstArraySpecialNone,
214     ConstArraySpecialUndef,
215     ConstArraySpecialBuf,
216 };
217 
218 struct ConstArrayValue {
219     ConstArraySpecial special;
220     union {
221         struct {
222             ZigValue *elements;
223         } s_none;
224         Buf *s_buf;
225     } data;
226 };
227 
228 enum ConstPtrSpecial {
229     // Enforce explicitly setting this ID by making the zero value invalid.
230     ConstPtrSpecialInvalid,
231     // The pointer is a reference to a single object.
232     ConstPtrSpecialRef,
233     // The pointer points to an element in an underlying array.
234     // Not to be confused with ConstPtrSpecialSubArray.
235     ConstPtrSpecialBaseArray,
236     // The pointer points to a field in an underlying struct.
237     ConstPtrSpecialBaseStruct,
238     // The pointer points to the error set field of an error union
239     ConstPtrSpecialBaseErrorUnionCode,
240     // The pointer points to the payload field of an error union
241     ConstPtrSpecialBaseErrorUnionPayload,
242     // The pointer points to the payload field of an optional
243     ConstPtrSpecialBaseOptionalPayload,
244     // This means that we did a compile-time pointer reinterpret and we cannot
245     // understand the value of pointee at compile time. However, we will still
246     // emit a binary with a compile time known address.
247     // In this case index is the numeric address value.
248     ConstPtrSpecialHardCodedAddr,
249     // This means that the pointer represents memory of assigning to _.
250     // That is, storing discards the data, and loading is invalid.
251     ConstPtrSpecialDiscard,
252     // This is actually a function.
253     ConstPtrSpecialFunction,
254     // This means the pointer is null. This is only allowed when the type is ?*T.
255     // We use this instead of ConstPtrSpecialHardCodedAddr because often we check
256     // for that value to avoid doing comptime work.
257     // We need the data layout for ConstCastOnly == true
258     // types to be the same, so all optionals of pointer types use x_ptr
259     // instead of x_optional.
260     ConstPtrSpecialNull,
261     // The pointer points to a sub-array (not an individual element).
262     // Not to be confused with ConstPtrSpecialBaseArray. However, it uses the same
263     // union payload struct (base_array).
264     ConstPtrSpecialSubArray,
265 };
266 
267 enum ConstPtrMut {
268     // The pointer points to memory that is known at compile time and immutable.
269     ConstPtrMutComptimeConst,
270     // This means that the pointer points to memory used by a comptime variable,
271     // so attempting to write a non-compile-time known value is an error
272     // But the underlying value is allowed to change at compile time.
273     ConstPtrMutComptimeVar,
274     // The pointer points to memory that is known only at runtime.
275     // For example it may point to the initializer value of a variable.
276     ConstPtrMutRuntimeVar,
277     // The pointer points to memory for which it must be inferred whether the
278     // value is comptime known or not.
279     ConstPtrMutInfer,
280 };
281 
282 struct ConstPtrValue {
283     ConstPtrSpecial special;
284     ConstPtrMut mut;
285 
286     union {
287         struct {
288             ZigValue *pointee;
289         } ref;
290         struct {
291             ZigValue *array_val;
292             size_t elem_index;
293         } base_array;
294         struct {
295             ZigValue *struct_val;
296             size_t field_index;
297         } base_struct;
298         struct {
299             ZigValue *err_union_val;
300         } base_err_union_code;
301         struct {
302             ZigValue *err_union_val;
303         } base_err_union_payload;
304         struct {
305             ZigValue *optional_val;
306         } base_optional_payload;
307         struct {
308             uint64_t addr;
309         } hard_coded_addr;
310         struct {
311             ZigFn *fn_entry;
312         } fn;
313     } data;
314 };
315 
316 struct ConstErrValue {
317     ZigValue *error_set;
318     ZigValue *payload;
319 };
320 
321 struct ConstBoundFnValue {
322     ZigFn *fn;
323     Stage1AirInst *first_arg;
324     AstNode *first_arg_src;
325 };
326 
327 struct ConstArgTuple {
328     size_t start_index;
329     size_t end_index;
330 };
331 
332 enum ConstValSpecial {
333     // The value is only available at runtime. However there may be runtime hints
334     // narrowing the possible values down via the `data.rh_*` fields.
335     ConstValSpecialRuntime,
336     // The value is comptime-known and resolved. The `data.x_*` fields can be
337     // accessed.
338     ConstValSpecialStatic,
339     // The value is comptime-known to be `undefined`.
340     ConstValSpecialUndef,
341     // The value is comptime-known, but not yet resolved. The lazy value system
342     // helps avoid dependency loops by providing answers to certain questions
343     // about values without forcing them to be resolved. For example, the
344     // equation `@sizeOf(Foo) == 0` can be resolved without forcing the struct
345     // layout of `Foo` because we can know whether `Foo` is zero bits without
346     // performing field layout.
347     // A `ZigValue` can be converted from Lazy to Static/Undef by calling the
348     // appropriate resolve function.
349     ConstValSpecialLazy,
350 };
351 
352 enum RuntimeHintErrorUnion {
353     RuntimeHintErrorUnionUnknown,
354     RuntimeHintErrorUnionError,
355     RuntimeHintErrorUnionNonError,
356 };
357 
358 enum RuntimeHintOptional {
359     RuntimeHintOptionalUnknown,
360     RuntimeHintOptionalNull, // TODO is this value even possible? if this is the case it might mean the const value is compile time known.
361     RuntimeHintOptionalNonNull,
362 };
363 
364 enum RuntimeHintPtr {
365     RuntimeHintPtrUnknown,
366     RuntimeHintPtrStack,
367     RuntimeHintPtrNonStack,
368 };
369 
370 enum RuntimeHintSliceId {
371     RuntimeHintSliceIdUnknown,
372     RuntimeHintSliceIdLen,
373 };
374 
375 struct RuntimeHintSlice {
376     enum RuntimeHintSliceId id;
377     uint64_t len;
378 };
379 
380 enum LazyValueId {
381     LazyValueIdInvalid,
382     LazyValueIdAlignOf,
383     LazyValueIdSizeOf,
384     LazyValueIdPtrType,
385     LazyValueIdPtrTypeSimple,
386     LazyValueIdPtrTypeSimpleConst,
387     LazyValueIdOptType,
388     LazyValueIdSliceType,
389     LazyValueIdFnType,
390     LazyValueIdErrUnionType,
391     LazyValueIdArrayType,
392     LazyValueIdTypeInfoDecls,
393 };
394 
395 struct LazyValue {
396     LazyValueId id;
397 };
398 
399 struct LazyValueTypeInfoDecls {
400     LazyValue base;
401 
402     IrAnalyze *ira;
403 
404     ScopeDecls *decls_scope;
405     AstNode *source_node;
406 };
407 
408 struct LazyValueAlignOf {
409     LazyValue base;
410 
411     IrAnalyze *ira;
412     Stage1AirInst *target_type;
413 };
414 
415 struct LazyValueSizeOf {
416     LazyValue base;
417 
418     IrAnalyze *ira;
419     Stage1AirInst *target_type;
420 
421     bool bit_size;
422 };
423 
424 struct LazyValueSliceType {
425     LazyValue base;
426 
427     IrAnalyze *ira;
428     Stage1AirInst *sentinel; // can be null
429     Stage1AirInst *elem_type;
430     Stage1AirInst *align_inst; // can be null
431 
432     bool is_const;
433     bool is_volatile;
434     bool is_allowzero;
435 };
436 
437 struct LazyValueArrayType {
438     LazyValue base;
439 
440     IrAnalyze *ira;
441     Stage1AirInst *sentinel; // can be null
442     Stage1AirInst *elem_type;
443     uint64_t length;
444 };
445 
446 struct LazyValuePtrType {
447     LazyValue base;
448 
449     IrAnalyze *ira;
450     Stage1AirInst *sentinel; // can be null
451     Stage1AirInst *elem_type;
452     Stage1AirInst *align_inst; // can be null
453 
454     PtrLen ptr_len;
455     uint32_t bit_offset_in_host;
456 
457     uint32_t host_int_bytes;
458     bool is_const;
459     bool is_volatile;
460     bool is_allowzero;
461 };
462 
463 struct LazyValuePtrTypeSimple {
464     LazyValue base;
465 
466     IrAnalyze *ira;
467     Stage1AirInst *elem_type;
468 };
469 
470 struct LazyValueOptType {
471     LazyValue base;
472 
473     IrAnalyze *ira;
474     Stage1AirInst *payload_type;
475 };
476 
477 struct LazyValueFnType {
478     LazyValue base;
479 
480     IrAnalyze *ira;
481     AstNode *proto_node;
482     Stage1AirInst **param_types;
483     Stage1AirInst *align_inst; // can be null
484     Stage1AirInst *return_type;
485 
486     CallingConvention cc;
487     bool is_generic;
488 };
489 
490 struct LazyValueErrUnionType {
491     LazyValue base;
492 
493     IrAnalyze *ira;
494     Stage1AirInst *err_set_type;
495     Stage1AirInst *payload_type;
496     Buf *type_name;
497 };
498 
499 struct ZigValue {
500     ZigType *type;
501     // This field determines how the value is stored. It must be checked
502     // before accessing the `data` union.
503     ConstValSpecial special;
504     uint32_t llvm_align;
505     ConstParent parent;
506     LLVMValueRef llvm_value;
507     LLVMValueRef llvm_global;
508 
509     union {
510         // populated if special == ConstValSpecialLazy
511         LazyValue *x_lazy;
512 
513         // populated if special == ConstValSpecialStatic
514         BigInt x_bigint;
515         BigFloat x_bigfloat;
516         float16_t x_f16;
517         float x_f32;
518         double x_f64;
519         float128_t x_f128;
520         bool x_bool;
521         ConstBoundFnValue x_bound_fn;
522         ZigType *x_type;
523         ZigValue *x_optional;
524         ConstErrValue x_err_union;
525         ErrorTableEntry *x_err_set;
526         BigInt x_enum_tag;
527         ConstStructValue x_struct;
528         ConstUnionValue x_union;
529         ConstArrayValue x_array;
530         ConstPtrValue x_ptr;
531         ConstArgTuple x_arg_tuple;
532         Buf *x_enum_literal;
533 
534         // populated if special == ConstValSpecialRuntime
535         RuntimeHintErrorUnion rh_error_union;
536         RuntimeHintOptional rh_maybe;
537         RuntimeHintPtr rh_ptr;
538         RuntimeHintSlice rh_slice;
539     } data;
540 
541     // uncomment this to find bugs. can't leave it uncommented because of a gcc-9 warning
542     //ZigValue& operator= (const ZigValue &other) = delete; // use copy_const_val
543 
544     ZigValue(const ZigValue &other) = delete; // plz zero initialize with ZigValue val = {};
545 
546     // for use in debuggers
547     void dump();
548 };
549 
550 enum ReturnKnowledge {
551     ReturnKnowledgeUnknown,
552     ReturnKnowledgeKnownError,
553     ReturnKnowledgeKnownNonError,
554     ReturnKnowledgeKnownNull,
555     ReturnKnowledgeKnownNonNull,
556     ReturnKnowledgeSkipDefers,
557 };
558 
559 enum VisibMod {
560     VisibModPrivate,
561     VisibModPub,
562 };
563 
564 enum GlobalLinkageId {
565     GlobalLinkageIdInternal,
566     GlobalLinkageIdStrong,
567     GlobalLinkageIdWeak,
568     GlobalLinkageIdLinkOnce,
569 };
570 
571 enum TldId {
572     TldIdVar,
573     TldIdFn,
574     TldIdContainer,
575     TldIdCompTime,
576     TldIdUsingNamespace,
577 };
578 
579 enum TldResolution {
580     TldResolutionUnresolved,
581     TldResolutionResolving,
582     TldResolutionInvalid,
583     TldResolutionOkLazy,
584     TldResolutionOk,
585 };
586 
587 struct Tld {
588     TldId id;
589     Buf *name;
590     VisibMod visib_mod;
591     AstNode *source_node;
592 
593     ZigType *import;
594     Scope *parent_scope;
595     TldResolution resolution;
596 };
597 
598 struct TldVar {
599     Tld base;
600 
601     ZigVar *var;
602     Buf *extern_lib_name;
603     bool analyzing_type; // flag to detect dependency loops
604 };
605 
606 struct TldFn {
607     Tld base;
608 
609     ZigFn *fn_entry;
610     Buf *extern_lib_name;
611 };
612 
613 struct TldContainer {
614     Tld base;
615 
616     ScopeDecls *decls_scope;
617     ZigType *type_entry;
618 };
619 
620 struct TldCompTime {
621     Tld base;
622 };
623 
624 struct TldUsingNamespace {
625     Tld base;
626 
627     ZigValue *using_namespace_value;
628 };
629 
630 struct TypeEnumField {
631     Buf *name;
632     BigInt value;
633     uint32_t decl_index;
634     AstNode *decl_node;
635 };
636 
637 struct TypeUnionField {
638     Buf *name;
639     ZigType *type_entry; // available after ResolveStatusSizeKnown
640     ZigValue *type_val; // available after ResolveStatusZeroBitsKnown
641     TypeEnumField *enum_field;
642     AstNode *decl_node;
643     uint32_t gen_index;
644     uint32_t align;
645 };
646 
647 enum NodeType {
648     NodeTypeFnProto,
649     NodeTypeFnDef,
650     NodeTypeParamDecl,
651     NodeTypeBlock,
652     NodeTypeGroupedExpr,
653     NodeTypeReturnExpr,
654     NodeTypeDefer,
655     NodeTypeVariableDeclaration,
656     NodeTypeTestDecl,
657     NodeTypeBinOpExpr,
658     NodeTypeCatchExpr,
659     NodeTypeFloatLiteral,
660     NodeTypeIntLiteral,
661     NodeTypeStringLiteral,
662     NodeTypeCharLiteral,
663     NodeTypeIdentifier,
664     NodeTypePrefixOpExpr,
665     NodeTypePointerType,
666     NodeTypeFnCallExpr,
667     NodeTypeArrayAccessExpr,
668     NodeTypeSliceExpr,
669     NodeTypeFieldAccessExpr,
670     NodeTypePtrDeref,
671     NodeTypeUnwrapOptional,
672     NodeTypeUsingNamespace,
673     NodeTypeBoolLiteral,
674     NodeTypeNullLiteral,
675     NodeTypeUndefinedLiteral,
676     NodeTypeUnreachable,
677     NodeTypeIfBoolExpr,
678     NodeTypeWhileExpr,
679     NodeTypeForExpr,
680     NodeTypeSwitchExpr,
681     NodeTypeSwitchProng,
682     NodeTypeSwitchRange,
683     NodeTypeCompTime,
684     NodeTypeNoSuspend,
685     NodeTypeBreak,
686     NodeTypeContinue,
687     NodeTypeAsmExpr,
688     NodeTypeContainerDecl,
689     NodeTypeStructField,
690     NodeTypeContainerInitExpr,
691     NodeTypeStructValueField,
692     NodeTypeArrayType,
693     NodeTypeInferredArrayType,
694     NodeTypeErrorType,
695     NodeTypeIfErrorExpr,
696     NodeTypeIfOptional,
697     NodeTypeErrorSetDecl,
698     NodeTypeErrorSetField,
699     NodeTypeResume,
700     NodeTypeAwaitExpr,
701     NodeTypeSuspend,
702     NodeTypeAnyFrameType,
703     // main_token points to the identifier.
704     NodeTypeEnumLiteral,
705     NodeTypeAnyTypeField,
706 };
707 
708 enum FnInline {
709     FnInlineAuto,
710     FnInlineAlways,
711     FnInlineNever,
712 };
713 
714 struct AstNodeFnProto {
715     Buf *name;
716     ZigList<AstNode *> params;
717     AstNode *return_type;
718     AstNode *fn_def_node;
719     // populated if this is an extern declaration
720     Buf *lib_name;
721     // populated if the "align A" is present
722     AstNode *align_expr;
723     // populated if the "section(S)" is present
724     AstNode *section_expr;
725     // populated if the "callconv(S)" is present
726     AstNode *callconv_expr;
727 
728     TokenIndex doc_comments;
729 
730     // This is set based only on the existence of a noinline or inline keyword.
731     // This is then resolved to an is_noinline bool and (potentially .Inline)
732     // calling convention in resolve_decl_fn() in analyze.cpp.
733     FnInline fn_inline;
734 
735     VisibMod visib_mod;
736     bool auto_err_set;
737     bool is_var_args;
738     bool is_extern;
739     bool is_export;
740 };
741 
742 struct AstNodeFnDef {
743     AstNode *fn_proto;
744     AstNode *body;
745 };
746 
747 struct AstNodeParamDecl {
748     Buf *name;
749     AstNode *type;
750     TokenIndex doc_comments;
751     TokenIndex anytype_token;
752     bool is_noalias;
753     bool is_comptime;
754     bool is_var_args;
755 };
756 
757 struct AstNodeBlock {
758     Buf *name;
759     ZigList<AstNode *> statements;
760 };
761 
762 enum ReturnKind {
763     ReturnKindUnconditional,
764     ReturnKindError,
765 };
766 
767 struct AstNodeReturnExpr {
768     ReturnKind kind;
769     // might be null in case of return void;
770     AstNode *expr;
771 };
772 
773 struct AstNodeDefer {
774     ReturnKind kind;
775     AstNode *err_payload;
776     AstNode *expr;
777 
778     // temporary data used in IR generation
779     Scope *child_scope;
780     Scope *expr_scope;
781 };
782 
783 struct AstNodeVariableDeclaration {
784     Buf *symbol;
785     // one or both of type and expr will be non null
786     AstNode *type;
787     AstNode *expr;
788     // populated if this is an extern declaration
789     Buf *lib_name;
790     // populated if the "align(A)" is present
791     AstNode *align_expr;
792     // populated if the "section(S)" is present
793     AstNode *section_expr;
794     TokenIndex doc_comments;
795 
796     TokenIndex threadlocal_tok;
797     VisibMod visib_mod;
798     bool is_const;
799     bool is_comptime;
800     bool is_export;
801     bool is_extern;
802 };
803 
804 struct AstNodeTestDecl {
805     // nullptr if the test declaration has no name
806     Buf *name;
807 
808     AstNode *body;
809 };
810 
811 enum BinOpType {
812     BinOpTypeInvalid,
813     BinOpTypeAssign,
814     BinOpTypeAssignTimes,
815     BinOpTypeAssignTimesSat,
816     BinOpTypeAssignTimesWrap,
817     BinOpTypeAssignDiv,
818     BinOpTypeAssignMod,
819     BinOpTypeAssignPlus,
820     BinOpTypeAssignPlusSat,
821     BinOpTypeAssignPlusWrap,
822     BinOpTypeAssignMinus,
823     BinOpTypeAssignMinusSat,
824     BinOpTypeAssignMinusWrap,
825     BinOpTypeAssignBitShiftLeft,
826     BinOpTypeAssignBitShiftLeftSat,
827     BinOpTypeAssignBitShiftRight,
828     BinOpTypeAssignBitAnd,
829     BinOpTypeAssignBitXor,
830     BinOpTypeAssignBitOr,
831     BinOpTypeBoolOr,
832     BinOpTypeBoolAnd,
833     BinOpTypeCmpEq,
834     BinOpTypeCmpNotEq,
835     BinOpTypeCmpLessThan,
836     BinOpTypeCmpGreaterThan,
837     BinOpTypeCmpLessOrEq,
838     BinOpTypeCmpGreaterOrEq,
839     BinOpTypeBinOr,
840     BinOpTypeBinXor,
841     BinOpTypeBinAnd,
842     BinOpTypeBitShiftLeft,
843     BinOpTypeBitShiftLeftSat,
844     BinOpTypeBitShiftRight,
845     BinOpTypeAdd,
846     BinOpTypeAddSat,
847     BinOpTypeAddWrap,
848     BinOpTypeSub,
849     BinOpTypeSubSat,
850     BinOpTypeSubWrap,
851     BinOpTypeMult,
852     BinOpTypeMultSat,
853     BinOpTypeMultWrap,
854     BinOpTypeDiv,
855     BinOpTypeMod,
856     BinOpTypeUnwrapOptional,
857     BinOpTypeArrayCat,
858     BinOpTypeArrayMult,
859     BinOpTypeErrorUnion,
860     BinOpTypeMergeErrorSets,
861 };
862 
863 struct AstNodeBinOpExpr {
864     AstNode *op1;
865     BinOpType bin_op;
866     AstNode *op2;
867 };
868 
869 struct AstNodeCatchExpr {
870     AstNode *op1;
871     AstNode *symbol; // can be null
872     AstNode *op2;
873 };
874 
875 struct AstNodeUnwrapOptional {
876     AstNode *expr;
877 };
878 
879 // Must be synchronized with std.builtin.CallOptions.Modifier
880 enum CallModifier {
881     CallModifierNone,
882     CallModifierAsync,
883     CallModifierNeverTail,
884     CallModifierNeverInline,
885     CallModifierNoSuspend,
886     CallModifierAlwaysTail,
887     CallModifierAlwaysInline,
888     CallModifierCompileTime,
889 
890     // These are additional tags in the compiler, but not exposed in the std lib.
891     CallModifierBuiltin,
892 };
893 
894 struct AstNodeFnCallExpr {
895     AstNode *fn_ref_expr;
896     ZigList<AstNode *> params;
897     CallModifier modifier;
898     bool seen; // used by @compileLog
899 };
900 
901 // Must be kept in sync with std.builtin.PrefetchOptions.Rw
902 enum PrefetchRw {
903     PrefetchRwRead,
904     PrefetchRwWrite,
905 };
906 
907 // Must be kept in sync with std.builtin.PrefetchOptions.Cache
908 enum PrefetchCache {
909     PrefetchCacheInstruction,
910     PrefetchCacheData,
911 };
912 
913 struct AstNodeArrayAccessExpr {
914     AstNode *array_ref_expr;
915     AstNode *subscript;
916 };
917 
918 struct AstNodeSliceExpr {
919     AstNode *array_ref_expr;
920     AstNode *start;
921     AstNode *end;
922     AstNode *sentinel; // can be null
923 };
924 
925 struct AstNodeFieldAccessExpr {
926     AstNode *struct_expr;
927     Buf *field_name;
928 };
929 
930 struct AstNodePtrDerefExpr {
931     AstNode *target;
932 };
933 
934 enum PrefixOp {
935     PrefixOpInvalid,
936     PrefixOpBoolNot,
937     PrefixOpBinNot,
938     PrefixOpNegation,
939     PrefixOpNegationWrap,
940     PrefixOpOptional,
941     PrefixOpAddrOf,
942 };
943 
944 struct AstNodePrefixOpExpr {
945     PrefixOp prefix_op;
946     AstNode *primary_expr;
947 };
948 
949 struct AstNodePointerType {
950     TokenIndex star_token;
951     TokenIndex allow_zero_token;
952     TokenIndex bit_offset_start;
953     TokenIndex host_int_bytes;
954 
955     AstNode *sentinel;
956     AstNode *align_expr;
957     AstNode *op_expr;
958     bool is_const;
959     bool is_volatile;
960 };
961 
962 struct AstNodeInferredArrayType {
963     AstNode *sentinel; // can be null
964     AstNode *child_type;
965 };
966 
967 struct AstNodeArrayType {
968     AstNode *size;
969     AstNode *sentinel;
970     AstNode *child_type;
971     AstNode *align_expr;
972     TokenIndex allow_zero_token;
973     bool is_const;
974     bool is_volatile;
975 };
976 
977 struct AstNodeUsingNamespace {
978     VisibMod visib_mod;
979     AstNode *expr;
980 };
981 
982 struct AstNodeIfBoolExpr {
983     AstNode *condition;
984     AstNode *then_block;
985     AstNode *else_node; // null, block node, or other if expr node
986 };
987 
988 struct AstNodeTryExpr {
989     Buf *var_symbol;
990     AstNode *target_node;
991     AstNode *then_node;
992     AstNode *else_node;
993     Buf *err_symbol;
994     bool var_is_ptr;
995 };
996 
997 struct AstNodeTestExpr {
998     Buf *var_symbol;
999     bool var_is_ptr;
1000     AstNode *target_node;
1001     AstNode *then_node;
1002     AstNode *else_node; // null, block node, or other if expr node
1003 };
1004 
1005 struct AstNodeWhileExpr {
1006     Buf *name;
1007     AstNode *condition;
1008     Buf *var_symbol;
1009     AstNode *continue_expr;
1010     AstNode *body;
1011     AstNode *else_node;
1012     Buf *err_symbol;
1013     bool is_inline;
1014     bool var_is_ptr;
1015 };
1016 
1017 struct AstNodeForExpr {
1018     Buf *name;
1019     AstNode *array_expr;
1020     AstNode *elem_node; // always a symbol
1021     AstNode *index_node; // always a symbol, might be null
1022     AstNode *body;
1023     AstNode *else_node; // can be null
1024     bool elem_is_ptr;
1025     bool is_inline;
1026 };
1027 
1028 struct AstNodeSwitchExpr {
1029     AstNode *expr;
1030     ZigList<AstNode *> prongs;
1031 };
1032 
1033 struct AstNodeSwitchProng {
1034     ZigList<AstNode *> items;
1035     AstNode *var_symbol;
1036     AstNode *expr;
1037     bool var_is_ptr;
1038     bool any_items_are_range;
1039 };
1040 
1041 struct AstNodeSwitchRange {
1042     AstNode *start;
1043     AstNode *end;
1044 };
1045 
1046 struct AstNodeCompTime {
1047     AstNode *expr;
1048 };
1049 
1050 struct AstNodeNoSuspend {
1051     AstNode *expr;
1052 };
1053 
1054 struct AsmOutput {
1055     Buf *asm_symbolic_name;
1056     Buf *constraint;
1057     Buf *variable_name;
1058     AstNode *return_type; // null unless "=r" and return
1059 };
1060 
1061 struct AsmInput {
1062     Buf *asm_symbolic_name;
1063     Buf *constraint;
1064     AstNode *expr;
1065 };
1066 
1067 struct SrcPos {
1068     size_t line;
1069     size_t column;
1070 };
1071 
1072 enum AsmTokenId {
1073     AsmTokenIdTemplate,
1074     AsmTokenIdPercent,
1075     AsmTokenIdVar,
1076     AsmTokenIdUniqueId,
1077 };
1078 
1079 struct AsmToken {
1080     enum AsmTokenId id;
1081     size_t start;
1082     size_t end;
1083 };
1084 
1085 struct AstNodeAsmExpr {
1086     TokenIndex volatile_token;
1087     AstNode *asm_template;
1088     ZigList<AsmOutput*> output_list;
1089     ZigList<AsmInput*> input_list;
1090     ZigList<Buf*> clobber_list;
1091 };
1092 
1093 enum ContainerKind {
1094     ContainerKindStruct,
1095     ContainerKindEnum,
1096     ContainerKindUnion,
1097     ContainerKindOpaque,
1098 };
1099 
1100 enum ContainerLayout {
1101     ContainerLayoutAuto,
1102     ContainerLayoutExtern,
1103     ContainerLayoutPacked,
1104 };
1105 
1106 struct AstNodeContainerDecl {
1107     AstNode *init_arg_expr; // enum(T), struct(endianness), or union(T), or union(enum(T))
1108     ZigList<AstNode *> fields;
1109     ZigList<AstNode *> decls;
1110     TokenIndex doc_comments;
1111 
1112     ContainerKind kind;
1113     ContainerLayout layout;
1114 
1115     bool auto_enum, is_root; // union(enum)
1116 };
1117 
1118 struct AstNodeErrorSetField {
1119     TokenIndex doc_comments;
1120     AstNode *field_name;
1121 };
1122 
1123 struct AstNodeErrorSetDecl {
1124     // Each AstNode could be AstNodeErrorSetField or just AstNodeSymbolExpr to save memory
1125     ZigList<AstNode *> decls;
1126 };
1127 
1128 struct AstNodeStructField {
1129     Buf *name;
1130     AstNode *type;
1131     AstNode *value;
1132     // populated if the "align(A)" is present
1133     AstNode *align_expr;
1134     TokenIndex doc_comments;
1135     TokenIndex comptime_token;
1136 };
1137 
1138 struct AstNodeStructValueField {
1139     Buf *name;
1140     AstNode *expr;
1141 };
1142 
1143 enum ContainerInitKind {
1144     ContainerInitKindStruct,
1145     ContainerInitKindArray,
1146 };
1147 
1148 struct AstNodeContainerInitExpr {
1149     AstNode *type;
1150     ZigList<AstNode *> entries;
1151     ContainerInitKind kind;
1152 };
1153 
1154 struct AstNodeIdentifier {
1155     Buf *name;
1156     bool is_at_syntax;
1157 };
1158 
1159 struct AstNodeEnumLiteral {
1160     Buf *name;
1161 };
1162 
1163 struct AstNodeBoolLiteral {
1164     bool value;
1165 };
1166 
1167 struct AstNodeBreakExpr {
1168     Buf *name;
1169     AstNode *expr; // may be null
1170 };
1171 
1172 struct AstNodeResumeExpr {
1173     AstNode *expr;
1174 };
1175 
1176 struct AstNodeContinueExpr {
1177     Buf *name;
1178 };
1179 
1180 struct AstNodeAwaitExpr {
1181     AstNode *expr;
1182 };
1183 
1184 struct AstNodeSuspend {
1185     AstNode *block;
1186 };
1187 
1188 struct AstNodeAnyFrameType {
1189     AstNode *payload_type; // can be NULL
1190 };
1191 
1192 struct AstNode {
1193     enum NodeType type;
1194     TokenIndex main_token;
1195     bool already_traced_this_node;
1196     ZigType *owner;
1197     union {
1198         AstNodeFnDef fn_def;
1199         AstNodeFnProto fn_proto;
1200         AstNodeParamDecl param_decl;
1201         AstNodeBlock block;
1202         AstNode * grouped_expr;
1203         AstNodeReturnExpr return_expr;
1204         AstNodeDefer defer;
1205         AstNodeVariableDeclaration variable_declaration;
1206         AstNodeTestDecl test_decl;
1207         AstNodeBinOpExpr bin_op_expr;
1208         AstNodeCatchExpr unwrap_err_expr;
1209         AstNodeUnwrapOptional unwrap_optional;
1210         AstNodePrefixOpExpr prefix_op_expr;
1211         AstNodePointerType pointer_type;
1212         AstNodeFnCallExpr fn_call_expr;
1213         AstNodeArrayAccessExpr array_access_expr;
1214         AstNodeSliceExpr slice_expr;
1215         AstNodeUsingNamespace using_namespace;
1216         AstNodeIfBoolExpr if_bool_expr;
1217         AstNodeTryExpr if_err_expr;
1218         AstNodeTestExpr test_expr;
1219         AstNodeWhileExpr while_expr;
1220         AstNodeForExpr for_expr;
1221         AstNodeSwitchExpr switch_expr;
1222         AstNodeSwitchProng switch_prong;
1223         AstNodeSwitchRange switch_range;
1224         AstNodeCompTime comptime_expr;
1225         AstNodeNoSuspend nosuspend_expr;
1226         AstNodeAsmExpr asm_expr;
1227         AstNodeFieldAccessExpr field_access_expr;
1228         AstNodePtrDerefExpr ptr_deref_expr;
1229         AstNodeContainerDecl container_decl;
1230         AstNodeStructField struct_field;
1231         AstNodeContainerInitExpr container_init_expr;
1232         AstNodeStructValueField struct_val_field;
1233         AstNodeBoolLiteral bool_literal;
1234         AstNodeBreakExpr break_expr;
1235         AstNodeContinueExpr continue_expr;
1236         AstNodeArrayType array_type;
1237         AstNodeInferredArrayType inferred_array_type;
1238         AstNodeErrorSetDecl err_set_decl;
1239         AstNodeErrorSetField err_set_field;
1240         AstNodeResumeExpr resume_expr;
1241         AstNodeAwaitExpr await_expr;
1242         AstNodeSuspend suspend;
1243         AstNodeAnyFrameType anyframe_type;
1244 
1245         // These are part of an astgen workaround to use less memory by
1246         // memoizing into the AST. Once astgen is modified to only run once
1247         // per corresponding source, this workaround can be removed.
1248         AstNodeIdentifier identifier;
1249         AstNodeEnumLiteral enum_literal;
1250     } data;
1251 
1252     // This is a function for use in the debugger to print
1253     // the source location.
1254     void src();
1255 };
1256 
1257 // this struct is allocated with allocate_nonzero
1258 struct FnTypeParamInfo {
1259     bool is_noalias;
1260     ZigType *type;
1261 };
1262 
1263 struct GenericFnTypeId {
1264     CodeGen *codegen;
1265     ZigFn *fn_entry;
1266     ZigValue *params;
1267     size_t param_count;
1268 };
1269 
1270 uint32_t generic_fn_type_id_hash(GenericFnTypeId *id);
1271 bool generic_fn_type_id_eql(GenericFnTypeId *a, GenericFnTypeId *b);
1272 
1273 struct FnTypeId {
1274     ZigType *return_type;
1275     FnTypeParamInfo *param_info;
1276     size_t param_count;
1277     size_t next_param_index;
1278     bool is_var_args;
1279     CallingConvention cc;
1280     uint32_t alignment;
1281 };
1282 
1283 uint32_t fn_type_id_hash(FnTypeId*);
1284 bool fn_type_id_eql(FnTypeId *a, FnTypeId *b);
1285 
1286 static const uint32_t VECTOR_INDEX_NONE = UINT32_MAX;
1287 static const uint32_t VECTOR_INDEX_RUNTIME = UINT32_MAX - 1;
1288 
1289 struct InferredStructField {
1290     ZigType *inferred_struct_type;
1291     Buf *field_name;
1292     bool already_resolved;
1293 };
1294 
1295 struct ZigTypePointer {
1296     ZigType *child_type;
1297     ZigType *slice_parent;
1298 
1299     // Anonymous struct literal syntax uses this when the result location has
1300     // no type in it. This field is null if this pointer does not refer to
1301     // a field of a currently-being-inferred struct type.
1302     // When this is non-null, the pointer is pointing to the base of the inferred
1303     // struct.
1304     InferredStructField *inferred_struct_field;
1305 
1306     // This can be null. If it is non-null, it means the pointer is terminated by this
1307     // sentinel value. This is most commonly used for C-style strings, with a 0 byte
1308     // to specify the length of the memory pointed to.
1309     ZigValue *sentinel;
1310 
1311     PtrLen ptr_len;
1312     uint32_t explicit_alignment; // 0 means use ABI alignment
1313 
1314     uint32_t bit_offset_in_host;
1315     // size of host integer. 0 means no host integer; this field is aligned
1316     // when vector_index != VECTOR_INDEX_NONE this is the len of the containing vector
1317     uint32_t host_int_bytes;
1318 
1319     uint32_t vector_index; // see the VECTOR_INDEX_* constants
1320     bool is_const;
1321     bool is_volatile;
1322     bool allow_zero;
1323     bool resolve_loop_flag_zero_bits;
1324 };
1325 
1326 struct ZigTypeInt {
1327     uint32_t bit_count;
1328     bool is_signed;
1329 };
1330 
1331 struct ZigTypeFloat {
1332     size_t bit_count;
1333 };
1334 
1335 // Needs to have the same memory layout as ZigTypeVector
1336 struct ZigTypeArray {
1337     ZigType *child_type;
1338     uint64_t len;
1339     ZigValue *sentinel;
1340 };
1341 
1342 struct TypeStructField {
1343     Buf *name;
1344     ZigType *type_entry; // available after ResolveStatusSizeKnown
1345     ZigValue *type_val; // available after ResolveStatusZeroBitsKnown
1346     size_t src_index;
1347     size_t gen_index;
1348     size_t offset; // byte offset from beginning of struct
1349     AstNode *decl_node;
1350     ZigValue *init_val; // null and then memoized
1351     uint32_t bit_offset_in_host; // offset from the memory at gen_index
1352     uint32_t host_int_bytes; // size of host integer
1353     uint32_t align;
1354     bool is_comptime;
1355 };
1356 
1357 enum ResolveStatus {
1358     ResolveStatusUnstarted,
1359     ResolveStatusInvalid,
1360     ResolveStatusBeingInferred,
1361     ResolveStatusZeroBitsKnown,
1362     ResolveStatusAlignmentKnown,
1363     ResolveStatusSizeKnown,
1364     ResolveStatusLLVMFwdDecl,
1365     ResolveStatusLLVMFull,
1366 };
1367 
1368 struct ZigPackage {
1369     Buf root_src_dir;
1370     Buf root_src_path; // relative to root_src_dir
1371     Buf pkg_path; // a.b.c.d which follows the package dependency chain from the root package
1372 
1373     // reminder: hash tables must be initialized before use
1374     HashMap<Buf *, ZigPackage *, buf_hash, buf_eql_buf> package_table;
1375 
1376     bool added_to_cache;
1377 };
1378 
1379 // Stuff that only applies to a struct which is the implicit root struct of a file
1380 struct RootStruct {
1381     ZigPackage *package;
1382     Buf *path; // relative to root_package->root_src_dir
1383     Buf *source_code;
1384     ZigLLVMDIFile *di_file;
1385     size_t token_count;
1386     TokenId *token_ids;
1387     TokenLoc *token_locs;
1388 };
1389 
1390 enum StructSpecial {
1391     StructSpecialNone,
1392     StructSpecialSlice,
1393     StructSpecialInferredTuple,
1394     StructSpecialInferredStruct,
1395 };
1396 
1397 struct ZigTypeStruct {
1398     AstNode *decl_node;
1399     TypeStructField **fields;
1400     ScopeDecls *decls_scope;
1401     HashMap<Buf *, TypeStructField *, buf_hash, buf_eql_buf> fields_by_name;
1402     RootStruct *root_struct;
1403     uint32_t *host_int_bytes; // available for packed structs, indexed by gen_index
1404     size_t llvm_full_type_queue_index;
1405 
1406     uint32_t src_field_count;
1407     uint32_t gen_field_count;
1408 
1409     ContainerLayout layout;
1410     ResolveStatus resolve_status;
1411 
1412     StructSpecial special;
1413     // whether any of the fields require comptime
1414     // known after ResolveStatusZeroBitsKnown
1415     bool requires_comptime;
1416     bool resolve_loop_flag_zero_bits;
1417     bool resolve_loop_flag_other;
1418     bool created_by_at_type;
1419 };
1420 
1421 struct ZigTypeOptional {
1422     ZigType *child_type;
1423     ResolveStatus resolve_status;
1424 };
1425 
1426 struct ZigTypeErrorUnion {
1427     ZigType *err_set_type;
1428     ZigType *payload_type;
1429     size_t pad_bytes;
1430     LLVMTypeRef pad_llvm_type;
1431 };
1432 
1433 struct ZigTypeErrorSet {
1434     ErrorTableEntry **errors;
1435     ZigFn *infer_fn;
1436     uint32_t err_count;
1437     bool incomplete;
1438 };
1439 
1440 struct ZigTypeEnum {
1441     AstNode *decl_node;
1442     TypeEnumField *fields;
1443     ZigType *tag_int_type;
1444 
1445     ScopeDecls *decls_scope;
1446 
1447     LLVMValueRef name_function;
1448 
1449     HashMap<Buf *, TypeEnumField *, buf_hash, buf_eql_buf> fields_by_name;
1450     uint32_t src_field_count;
1451 
1452     ContainerLayout layout;
1453     ResolveStatus resolve_status;
1454 
1455     bool has_explicit_tag_type;
1456     bool non_exhaustive;
1457     bool resolve_loop_flag;
1458 };
1459 
1460 uint32_t type_ptr_hash(const ZigType *ptr);
1461 bool type_ptr_eql(const ZigType *a, const ZigType *b);
1462 
1463 uint32_t pkg_ptr_hash(const ZigPackage *ptr);
1464 bool pkg_ptr_eql(const ZigPackage *a, const ZigPackage *b);
1465 
1466 uint32_t tld_ptr_hash(const Tld *ptr);
1467 bool tld_ptr_eql(const Tld *a, const Tld *b);
1468 
1469 uint32_t node_ptr_hash(const AstNode *ptr);
1470 bool node_ptr_eql(const AstNode *a, const AstNode *b);
1471 
1472 uint32_t fn_ptr_hash(const ZigFn *ptr);
1473 bool fn_ptr_eql(const ZigFn *a, const ZigFn *b);
1474 
1475 uint32_t err_ptr_hash(const ErrorTableEntry *ptr);
1476 bool err_ptr_eql(const ErrorTableEntry *a, const ErrorTableEntry *b);
1477 
1478 struct ZigTypeUnion {
1479     AstNode *decl_node;
1480     TypeUnionField *fields;
1481     ScopeDecls *decls_scope;
1482     HashMap<Buf *, TypeUnionField *, buf_hash, buf_eql_buf> fields_by_name;
1483     ZigType *tag_type; // always an enum or null
1484     LLVMTypeRef union_llvm_type;
1485     TypeUnionField *most_aligned_union_member;
1486     size_t gen_union_index;
1487     size_t gen_tag_index;
1488     size_t union_abi_size;
1489 
1490     uint32_t src_field_count;
1491     uint32_t gen_field_count;
1492 
1493     ContainerLayout layout;
1494     ResolveStatus resolve_status;
1495 
1496     bool have_explicit_tag_type;
1497     // whether any of the fields require comptime
1498     // the value is not valid until zero_bits_known == true
1499     bool requires_comptime;
1500     bool resolve_loop_flag_zero_bits;
1501     bool resolve_loop_flag_other;
1502 };
1503 
1504 struct FnGenParamInfo {
1505     size_t src_index;
1506     size_t gen_index;
1507     bool is_byval;
1508     ZigType *type;
1509 };
1510 
1511 struct ZigTypeFn {
1512     FnTypeId fn_type_id;
1513     bool is_generic;
1514     ZigType *gen_return_type;
1515     size_t gen_param_count;
1516     FnGenParamInfo *gen_param_info;
1517 
1518     LLVMTypeRef raw_type_ref;
1519     ZigLLVMDIType *raw_di_type;
1520 
1521     ZigType *bound_fn_parent;
1522 };
1523 
1524 struct ZigTypeBoundFn {
1525     ZigType *fn_type;
1526 };
1527 
1528 // Needs to have the same memory layout as ZigTypeArray
1529 struct ZigTypeVector {
1530     // The type must be a pointer, integer, bool, or float
1531     ZigType *elem_type;
1532     uint64_t len;
1533     size_t padding;
1534 };
1535 
1536 // A lot of code is relying on ZigTypeArray and ZigTypeVector having the same layout/size
1537 static_assert(sizeof(ZigTypeVector) == sizeof(ZigTypeArray), "Size of ZigTypeVector and ZigTypeArray do not match!");
1538 
1539 enum ZigTypeId {
1540     ZigTypeIdInvalid,
1541     ZigTypeIdMetaType,
1542     ZigTypeIdVoid,
1543     ZigTypeIdBool,
1544     ZigTypeIdUnreachable,
1545     ZigTypeIdInt,
1546     ZigTypeIdFloat,
1547     ZigTypeIdPointer,
1548     ZigTypeIdArray,
1549     ZigTypeIdStruct,
1550     ZigTypeIdComptimeFloat,
1551     ZigTypeIdComptimeInt,
1552     ZigTypeIdUndefined,
1553     ZigTypeIdNull,
1554     ZigTypeIdOptional,
1555     ZigTypeIdErrorUnion,
1556     ZigTypeIdErrorSet,
1557     ZigTypeIdEnum,
1558     ZigTypeIdUnion,
1559     ZigTypeIdFn,
1560     ZigTypeIdBoundFn,
1561     ZigTypeIdOpaque,
1562     ZigTypeIdFnFrame,
1563     ZigTypeIdAnyFrame,
1564     ZigTypeIdVector,
1565     ZigTypeIdEnumLiteral,
1566 };
1567 
1568 enum OnePossibleValue {
1569     OnePossibleValueInvalid,
1570     OnePossibleValueNo,
1571     OnePossibleValueYes,
1572 };
1573 
1574 struct ZigTypeOpaque {
1575     AstNode *decl_node;
1576     Buf *bare_name;
1577 
1578     ScopeDecls *decls_scope;
1579 };
1580 
1581 struct ZigTypeFnFrame {
1582     ZigFn *fn;
1583     ZigType *locals_struct;
1584 
1585     // This is set to the type that resolving the frame currently depends on, null if none.
1586     // It's for generating a helpful error message.
1587     ZigType *resolve_loop_type;
1588     AstNode *resolve_loop_src_node;
1589     bool reported_loop_err;
1590 };
1591 
1592 struct ZigTypeAnyFrame {
1593     ZigType *result_type; // null if `anyframe` instead of `anyframe->T`
1594 };
1595 
1596 struct ZigType {
1597     ZigTypeId id;
1598     Buf name;
1599 
1600     // These are not supposed to be accessed directly. They're
1601     // null during semantic analysis, memoized with get_llvm_type
1602     // get_llvm_c_abi_type and get_llvm_di_type
1603     LLVMTypeRef llvm_type;
1604     LLVMTypeRef llvm_c_abi_type;
1605     ZigLLVMDIType *llvm_di_type;
1606 
1607     union {
1608         ZigTypePointer pointer;
1609         ZigTypeInt integral;
1610         ZigTypeFloat floating;
1611         ZigTypeArray array;
1612         ZigTypeStruct structure;
1613         ZigTypeOptional maybe;
1614         ZigTypeErrorUnion error_union;
1615         ZigTypeErrorSet error_set;
1616         ZigTypeEnum enumeration;
1617         ZigTypeUnion unionation;
1618         ZigTypeFn fn;
1619         ZigTypeBoundFn bound_fn;
1620         ZigTypeVector vector;
1621         ZigTypeOpaque opaque;
1622         ZigTypeFnFrame frame;
1623         ZigTypeAnyFrame any_frame;
1624     } data;
1625 
1626     // use these fields to make sure we don't duplicate type table entries for the same type
1627     ZigType *pointer_parent[2]; // [0 - mut, 1 - const]
1628     ZigType *optional_parent;
1629     ZigType *any_frame_parent;
1630     // If we generate a constant name value for this type, we memoize it here.
1631     // The type of this is array
1632     ZigValue *cached_const_name_val;
1633 
1634     OnePossibleValue one_possible_value;
1635     // Known after ResolveStatusAlignmentKnown.
1636     uint32_t abi_align;
1637     // The offset in bytes between consecutive array elements of this type. Known
1638     // after ResolveStatusSizeKnown.
1639     size_t abi_size;
1640     // Number of bits of information in this type. Known after ResolveStatusSizeKnown.
1641     size_t size_in_bits;
1642 };
1643 
1644 enum FnAnalState {
1645     FnAnalStateReady,
1646     FnAnalStateProbing,
1647     FnAnalStateComplete,
1648     FnAnalStateInvalid,
1649 };
1650 
1651 struct GlobalExport {
1652     Buf name;
1653     GlobalLinkageId linkage;
1654 };
1655 
1656 struct ZigFn {
1657     LLVMValueRef llvm_value;
1658     LLVMValueRef abi_return_value; // alloca used when converting at SysV ABI boundaries
1659     const char *llvm_name;
1660     AstNode *proto_node;
1661     AstNode *body_node;
1662     ScopeFnDef *fndef_scope; // parent should be the top level decls or container decls
1663     Scope *child_scope; // parent is scope for last parameter
1664     ScopeBlock *def_scope; // parent is child_scope
1665     Buf symbol_name;
1666     // This is the function type assuming the function does not suspend.
1667     // Note that for an async function, this can be shared with non-async functions. So the value here
1668     // should only be read for things in common between non-async and async function types.
1669     ZigType *type_entry;
1670     // For normal functions one could use the type_entry->raw_type_ref and type_entry->raw_di_type.
1671     // However for functions that suspend, those values could possibly be their non-suspending equivalents.
1672     // So these values should be preferred.
1673     LLVMTypeRef raw_type_ref;
1674     ZigLLVMDIType *raw_di_type;
1675 
1676     ZigType *frame_type;
1677     // in the case of normal functions this is the implicit return type
1678     // in the case of async functions this is the implicit return type according to the
1679     // zig source code, not according to zig ir
1680     ZigType *src_implicit_return_type;
1681     Stage1Zir *stage1_zir;
1682     Stage1Air analyzed_executable;
1683     size_t branch_quota;
1684     AstNode **param_source_nodes;
1685     Buf **param_names;
1686     Stage1AirInst *err_code_spill;
1687     AstNode *assumed_non_async;
1688 
1689     AstNode *fn_no_inline_set_node;
1690     AstNode *fn_static_eval_set_node;
1691 
1692     ZigList<Stage1AirInstAlloca *> alloca_gen_list;
1693     ZigList<ZigVar *> variable_list;
1694 
1695     Buf *section_name;
1696     AstNode *set_alignstack_node;
1697 
1698     AstNode *set_cold_node;
1699     const AstNode *inferred_async_node;
1700     ZigFn *inferred_async_fn;
1701     AstNode *non_async_node;
1702 
1703     ZigList<GlobalExport> export_list;
1704     ZigList<Stage1AirInstCall *> call_list;
1705     ZigList<Stage1AirInstAwait *> await_list;
1706 
1707     LLVMValueRef valgrind_client_request_array;
1708 
1709     FnAnalState anal_state;
1710 
1711     uint32_t align_bytes;
1712     uint32_t alignstack_value;
1713 
1714     bool calls_or_awaits_errorable_fn;
1715     bool is_cold;
1716     bool is_test;
1717     bool is_noinline;
1718 };
1719 
1720 uint32_t fn_table_entry_hash(ZigFn*);
1721 bool fn_table_entry_eql(ZigFn *a, ZigFn *b);
1722 
1723 enum BuiltinFnId {
1724     BuiltinFnIdInvalid,
1725     BuiltinFnIdMemcpy,
1726     BuiltinFnIdMemset,
1727     BuiltinFnIdSizeof,
1728     BuiltinFnIdAlignOf,
1729     BuiltinFnIdField,
1730     BuiltinFnIdTypeInfo,
1731     BuiltinFnIdType,
1732     BuiltinFnIdHasField,
1733     BuiltinFnIdTypeof,
1734     BuiltinFnIdAddWithOverflow,
1735     BuiltinFnIdSubWithOverflow,
1736     BuiltinFnIdMulWithOverflow,
1737     BuiltinFnIdShlWithOverflow,
1738     BuiltinFnIdMulAdd,
1739     BuiltinFnIdCInclude,
1740     BuiltinFnIdCDefine,
1741     BuiltinFnIdCUndef,
1742     BuiltinFnIdCompileErr,
1743     BuiltinFnIdCompileLog,
1744     BuiltinFnIdCtz,
1745     BuiltinFnIdClz,
1746     BuiltinFnIdPopCount,
1747     BuiltinFnIdBswap,
1748     BuiltinFnIdBitReverse,
1749     BuiltinFnIdImport,
1750     BuiltinFnIdCImport,
1751     BuiltinFnIdErrName,
1752     BuiltinFnIdBreakpoint,
1753     BuiltinFnIdReturnAddress,
1754     BuiltinFnIdEmbedFile,
1755     BuiltinFnIdCmpxchgWeak,
1756     BuiltinFnIdCmpxchgStrong,
1757     BuiltinFnIdFence,
1758     BuiltinFnIdDivExact,
1759     BuiltinFnIdDivTrunc,
1760     BuiltinFnIdDivFloor,
1761     BuiltinFnIdRem,
1762     BuiltinFnIdMod,
1763     BuiltinFnIdSqrt,
1764     BuiltinFnIdSin,
1765     BuiltinFnIdCos,
1766     BuiltinFnIdExp,
1767     BuiltinFnIdExp2,
1768     BuiltinFnIdLog,
1769     BuiltinFnIdLog2,
1770     BuiltinFnIdLog10,
1771     BuiltinFnIdFabs,
1772     BuiltinFnIdFloor,
1773     BuiltinFnIdCeil,
1774     BuiltinFnIdTrunc,
1775     BuiltinFnIdNearbyInt,
1776     BuiltinFnIdRound,
1777     BuiltinFnIdTruncate,
1778     BuiltinFnIdIntCast,
1779     BuiltinFnIdFloatCast,
1780     BuiltinFnIdErrSetCast,
1781     BuiltinFnIdIntToFloat,
1782     BuiltinFnIdFloatToInt,
1783     BuiltinFnIdBoolToInt,
1784     BuiltinFnIdErrToInt,
1785     BuiltinFnIdIntToErr,
1786     BuiltinFnIdEnumToInt,
1787     BuiltinFnIdIntToEnum,
1788     BuiltinFnIdVectorType,
1789     BuiltinFnIdShuffle,
1790     BuiltinFnIdSelect,
1791     BuiltinFnIdSplat,
1792     BuiltinFnIdSetCold,
1793     BuiltinFnIdSetRuntimeSafety,
1794     BuiltinFnIdSetFloatMode,
1795     BuiltinFnIdTypeName,
1796     BuiltinFnIdPanic,
1797     BuiltinFnIdPtrCast,
1798     BuiltinFnIdBitCast,
1799     BuiltinFnIdIntToPtr,
1800     BuiltinFnIdPtrToInt,
1801     BuiltinFnIdTagName,
1802     BuiltinFnIdFieldParentPtr,
1803     BuiltinFnIdOffsetOf,
1804     BuiltinFnIdBitOffsetOf,
1805     BuiltinFnIdAsyncCall,
1806     BuiltinFnIdShlExact,
1807     BuiltinFnIdShrExact,
1808     BuiltinFnIdSetEvalBranchQuota,
1809     BuiltinFnIdAlignCast,
1810     BuiltinFnIdThis,
1811     BuiltinFnIdSetAlignStack,
1812     BuiltinFnIdExport,
1813     BuiltinFnIdExtern,
1814     BuiltinFnIdErrorReturnTrace,
1815     BuiltinFnIdAtomicRmw,
1816     BuiltinFnIdAtomicLoad,
1817     BuiltinFnIdAtomicStore,
1818     BuiltinFnIdHasDecl,
1819     BuiltinFnIdUnionInit,
1820     BuiltinFnIdFrameAddress,
1821     BuiltinFnIdFrameType,
1822     BuiltinFnIdFrameHandle,
1823     BuiltinFnIdFrameSize,
1824     BuiltinFnIdAs,
1825     BuiltinFnIdCall,
1826     BuiltinFnIdBitSizeof,
1827     BuiltinFnIdWasmMemorySize,
1828     BuiltinFnIdWasmMemoryGrow,
1829     BuiltinFnIdSrc,
1830     BuiltinFnIdReduce,
1831     BuiltinFnIdMaximum,
1832     BuiltinFnIdMinimum,
1833     BuiltinFnIdPrefetch,
1834 };
1835 
1836 struct BuiltinFnEntry {
1837     BuiltinFnId id;
1838     Buf name;
1839     size_t param_count;
1840 };
1841 
1842 enum PanicMsgId {
1843     PanicMsgIdUnreachable,
1844     PanicMsgIdBoundsCheckFailure,
1845     PanicMsgIdCastNegativeToUnsigned,
1846     PanicMsgIdCastTruncatedData,
1847     PanicMsgIdIntegerOverflow,
1848     PanicMsgIdShlOverflowedBits,
1849     PanicMsgIdShrOverflowedBits,
1850     PanicMsgIdDivisionByZero,
1851     PanicMsgIdRemainderDivisionByZero,
1852     PanicMsgIdExactDivisionRemainder,
1853     PanicMsgIdUnwrapOptionalFail,
1854     PanicMsgIdInvalidErrorCode,
1855     PanicMsgIdIncorrectAlignment,
1856     PanicMsgIdBadUnionField,
1857     PanicMsgIdBadEnumValue,
1858     PanicMsgIdFloatToInt,
1859     PanicMsgIdPtrCastNull,
1860     PanicMsgIdBadResume,
1861     PanicMsgIdBadAwait,
1862     PanicMsgIdBadReturn,
1863     PanicMsgIdResumedAnAwaitingFn,
1864     PanicMsgIdFrameTooSmall,
1865     PanicMsgIdResumedFnPendingAwait,
1866     PanicMsgIdBadNoSuspendCall,
1867     PanicMsgIdResumeNotSuspendedFn,
1868     PanicMsgIdBadSentinel,
1869     PanicMsgIdShxTooBigRhs,
1870 
1871     PanicMsgIdCount,
1872 };
1873 
1874 uint32_t fn_eval_hash(Scope*);
1875 bool fn_eval_eql(Scope *a, Scope *b);
1876 
1877 struct TypeId {
1878     ZigTypeId id;
1879 
1880     union {
1881         struct {
1882             CodeGen *codegen;
1883             ZigType *child_type;
1884             InferredStructField *inferred_struct_field;
1885             ZigValue *sentinel;
1886             PtrLen ptr_len;
1887             uint32_t alignment;
1888 
1889             uint32_t bit_offset_in_host;
1890             uint32_t host_int_bytes;
1891 
1892             uint32_t vector_index;
1893             bool is_const;
1894             bool is_volatile;
1895             bool allow_zero;
1896         } pointer;
1897         struct {
1898             CodeGen *codegen;
1899             ZigType *child_type;
1900             uint64_t size;
1901             ZigValue *sentinel;
1902         } array;
1903         struct {
1904             bool is_signed;
1905             uint32_t bit_count;
1906         } integer;
1907         struct {
1908             ZigType *err_set_type;
1909             ZigType *payload_type;
1910         } error_union;
1911         struct {
1912             ZigType *elem_type;
1913             uint32_t len;
1914         } vector;
1915     } data;
1916 };
1917 
1918 uint32_t type_id_hash(TypeId const *);
1919 bool type_id_eql(TypeId const *a, TypeId const *b);
1920 
1921 enum ZigLLVMFnId {
1922     ZigLLVMFnIdCtz,
1923     ZigLLVMFnIdClz,
1924     ZigLLVMFnIdPopCount,
1925     ZigLLVMFnIdOverflowArithmetic,
1926     ZigLLVMFnIdFMA,
1927     ZigLLVMFnIdFloatOp,
1928     ZigLLVMFnIdBswap,
1929     ZigLLVMFnIdBitReverse,
1930 };
1931 
1932 // There are a bunch of places in code that rely on these values being in
1933 // exactly this order.
1934 enum AddSubMul {
1935     AddSubMulAdd = 0,
1936     AddSubMulSub = 1,
1937     AddSubMulMul = 2,
1938 };
1939 
1940 struct ZigLLVMFnKey {
1941     ZigLLVMFnId id;
1942 
1943     union {
1944         struct {
1945             uint32_t bit_count;
1946             uint32_t vector_len; // 0 means not a vector
1947         } ctz;
1948         struct {
1949             uint32_t bit_count;
1950             uint32_t vector_len; // 0 means not a vector
1951         } clz;
1952         struct {
1953             uint32_t bit_count;
1954             uint32_t vector_len; // 0 means not a vector
1955         } pop_count;
1956         struct {
1957             BuiltinFnId op;
1958             uint32_t bit_count;
1959             uint32_t vector_len; // 0 means not a vector
1960         } floating;
1961         struct {
1962             AddSubMul add_sub_mul;
1963             uint32_t bit_count;
1964             uint32_t vector_len; // 0 means not a vector
1965             bool is_signed;
1966         } overflow_arithmetic;
1967         struct {
1968             uint32_t bit_count;
1969             uint32_t vector_len; // 0 means not a vector
1970         } bswap;
1971         struct {
1972             uint32_t bit_count;
1973             uint32_t vector_len; // 0 means not a vector
1974         } bit_reverse;
1975     } data;
1976 };
1977 
1978 uint32_t zig_llvm_fn_key_hash(ZigLLVMFnKey const *);
1979 bool zig_llvm_fn_key_eql(ZigLLVMFnKey const *a, ZigLLVMFnKey const *b);
1980 
1981 struct TimeEvent {
1982     double time;
1983     const char *name;
1984 };
1985 
1986 struct CFile {
1987     ZigList<const char *> args;
1988     const char *source_path;
1989     const char *preprocessor_only_basename;
1990 };
1991 
1992 struct CodeGen {
1993     // Other code depends on this being first.
1994     ZigStage1 stage1;
1995 
1996     // arena allocator destroyed just prior to codegen emit
1997     heap::ArenaAllocator *pass1_arena;
1998 
1999     //////////////////////////// Runtime State
2000     LLVMModuleRef module;
2001     ZigList<ErrorMsg*> errors;
2002     ErrorMsg *trace_err;
2003     LLVMBuilderRef builder;
2004     ZigLLVMDIBuilder *dbuilder;
2005     ZigLLVMDICompileUnit *compile_unit;
2006     ZigLLVMDIFile *compile_unit_file;
2007     LLVMTargetDataRef target_data_ref;
2008     LLVMTargetMachineRef target_machine;
2009     ZigLLVMDIFile *dummy_di_file;
2010     LLVMValueRef cur_ret_ptr;
2011     LLVMValueRef cur_frame_ptr;
2012     LLVMValueRef cur_fn_val;
2013     LLVMValueRef cur_async_switch_instr;
2014     LLVMValueRef cur_async_resume_index_ptr;
2015     LLVMValueRef cur_async_awaiter_ptr;
2016     LLVMBasicBlockRef cur_preamble_llvm_block;
2017     size_t cur_resume_block_count;
2018     LLVMValueRef cur_err_ret_trace_val_arg;
2019     LLVMValueRef cur_err_ret_trace_val_stack;
2020     LLVMValueRef cur_bad_not_suspended_index;
2021     LLVMValueRef memcpy_fn_val;
2022     LLVMValueRef memset_fn_val;
2023     LLVMValueRef trap_fn_val;
2024     LLVMValueRef return_address_fn_val;
2025     LLVMValueRef frame_address_fn_val;
2026     LLVMValueRef add_error_return_trace_addr_fn_val;
2027     LLVMValueRef stacksave_fn_val;
2028     LLVMValueRef stackrestore_fn_val;
2029     LLVMValueRef write_register_fn_val;
2030     LLVMValueRef merge_err_ret_traces_fn_val;
2031     LLVMValueRef sp_md_node;
2032     LLVMValueRef err_name_table;
2033     LLVMValueRef safety_crash_err_fn;
2034     LLVMValueRef return_err_fn;
2035     LLVMValueRef wasm_memory_size;
2036     LLVMValueRef wasm_memory_grow;
2037     LLVMValueRef prefetch;
2038     LLVMTypeRef anyframe_fn_type;
2039 
2040     // reminder: hash tables must be initialized before use
2041     HashMap<Buf *, ZigType *, buf_hash, buf_eql_buf> import_table;
2042     HashMap<Buf *, BuiltinFnEntry *, buf_hash, buf_eql_buf> builtin_fn_table;
2043     HashMap<Buf *, ZigType *, buf_hash, buf_eql_buf> primitive_type_table;
2044     HashMap<TypeId, ZigType *, type_id_hash, type_id_eql> type_table;
2045     HashMap<FnTypeId *, ZigType *, fn_type_id_hash, fn_type_id_eql> fn_type_table;
2046     HashMap<Buf *, ErrorTableEntry *, buf_hash, buf_eql_buf> error_table;
2047     HashMap<GenericFnTypeId *, ZigFn *, generic_fn_type_id_hash, generic_fn_type_id_eql> generic_table;
2048     HashMap<Scope *, ZigValue *, fn_eval_hash, fn_eval_eql> memoized_fn_eval_table;
2049     HashMap<ZigLLVMFnKey, LLVMValueRef, zig_llvm_fn_key_hash, zig_llvm_fn_key_eql> llvm_fn_table;
2050     HashMap<Buf *, Tld *, buf_hash, buf_eql_buf> exported_symbol_names;
2051     HashMap<Buf *, Tld *, buf_hash, buf_eql_buf> external_symbol_names;
2052     HashMap<Buf *, ZigValue *, buf_hash, buf_eql_buf> string_literals_table;
2053     HashMap<const ZigType *, ZigValue *, type_ptr_hash, type_ptr_eql> type_info_cache;
2054     HashMap<const ZigType *, ZigValue *, type_ptr_hash, type_ptr_eql> one_possible_values;
2055 
2056     ZigList<Tld *> resolve_queue;
2057     size_t resolve_queue_index;
2058     ZigList<TimeEvent> timing_events;
2059     ZigList<ZigFn *> inline_fns;
2060     ZigList<ZigFn *> test_fns;
2061     ZigList<ErrorTableEntry *> errors_by_index;
2062     size_t largest_err_name_len;
2063     ZigList<ZigType *> type_resolve_stack;
2064 
2065     ZigPackage *std_package;
2066     ZigPackage *test_runner_package;
2067     ZigPackage *compile_var_package;
2068     ZigPackage *root_pkg; // @import("root")
2069     ZigPackage *main_pkg; // usually same as root_pkg, except for `zig test`
2070     ZigType *compile_var_import;
2071     ZigType *root_import;
2072     ZigType *start_import;
2073     ZigType *std_builtin_import;
2074 
2075     struct {
2076         ZigType *entry_bool;
2077         ZigType *entry_c_int[CIntTypeCount];
2078         ZigType *entry_c_longdouble;
2079         ZigType *entry_anyopaque;
2080         ZigType *entry_u8;
2081         ZigType *entry_u16;
2082         ZigType *entry_u32;
2083         ZigType *entry_u29;
2084         ZigType *entry_u64;
2085         ZigType *entry_i8;
2086         ZigType *entry_i32;
2087         ZigType *entry_i64;
2088         ZigType *entry_isize;
2089         ZigType *entry_usize;
2090         ZigType *entry_f16;
2091         ZigType *entry_f32;
2092         ZigType *entry_f64;
2093         ZigType *entry_f128;
2094         ZigType *entry_void;
2095         ZigType *entry_unreachable;
2096         ZigType *entry_type;
2097         ZigType *entry_invalid;
2098         ZigType *entry_block;
2099         ZigType *entry_num_lit_int;
2100         ZigType *entry_num_lit_float;
2101         ZigType *entry_undef;
2102         ZigType *entry_null;
2103         ZigType *entry_anytype;
2104         ZigType *entry_global_error_set;
2105         ZigType *entry_enum_literal;
2106         ZigType *entry_any_frame;
2107     } builtin_types;
2108 
2109     struct Intern {
2110         ZigValue x_undefined;
2111         ZigValue x_void;
2112         ZigValue x_null;
2113         ZigValue x_unreachable;
2114         ZigValue zero_byte;
2115 
2116         ZigValue *for_undefined();
2117         ZigValue *for_void();
2118         ZigValue *for_null();
2119         ZigValue *for_unreachable();
2120         ZigValue *for_zero_byte();
2121     } intern;
2122 
2123     ZigType *align_amt_type;
2124     ZigType *stack_trace_type;
2125     ZigType *err_tag_type;
2126     ZigType *test_fn_type;
2127 
2128     Buf llvm_triple_str;
2129     Buf global_asm;
2130     Buf o_file_output_path;
2131     Buf h_file_output_path;
2132     Buf asm_file_output_path;
2133     Buf llvm_ir_file_output_path;
2134     Buf bitcode_file_output_path;
2135     Buf analysis_json_output_path;
2136     Buf docs_output_path;
2137 
2138     Buf *builtin_zig_path;
2139     Buf *zig_std_special_dir; // Cannot be overridden; derived from zig_lib_dir.
2140 
2141     Stage1ZirInst *invalid_inst_src;
2142     Stage1AirInst *invalid_inst_gen;
2143     Stage1AirInst *unreach_instruction;
2144 
2145     ZigValue panic_msg_vals[PanicMsgIdCount];
2146 
2147     // The function definitions this module includes.
2148     ZigList<ZigFn *> fn_defs;
2149     size_t fn_defs_index;
2150     ZigList<TldVar *> global_vars;
2151 
2152     ZigFn *cur_fn;
2153     ZigFn *panic_fn;
2154 
2155     ZigFn *largest_frame_fn;
2156 
2157     Stage2ProgressNode *main_progress_node;
2158     Stage2ProgressNode *sub_progress_node;
2159 
2160     ErrColor err_color;
2161     uint32_t next_unresolved_index;
2162     unsigned pointer_size_bytes;
2163     bool is_big_endian;
2164     bool have_err_ret_tracing;
2165     bool verbose_ir;
2166     bool verbose_llvm_ir;
2167     bool verbose_cimport;
2168     bool verbose_llvm_cpu_features;
2169     bool error_during_imports;
2170     bool generate_error_name_table;
2171     bool enable_time_report;
2172     bool enable_stack_report;
2173     bool reported_bad_link_libc_error;
2174     bool need_frame_size_prefix_data;
2175     bool link_libc;
2176     bool link_libcpp;
2177 
2178     BuildMode build_mode;
2179     const ZigTarget *zig_target;
2180     TargetSubsystem subsystem; // careful using this directly; see detect_subsystem
2181     CodeModel code_model;
2182     bool strip_debug_symbols;
2183     bool is_test_build;
2184     bool is_single_threaded;
2185     bool have_pic;
2186     bool have_pie;
2187     bool have_lto;
2188     bool unwind_tables;
2189     bool link_mode_dynamic;
2190     bool dll_export_fns;
2191     bool have_stack_probing;
2192     bool red_zone;
2193     bool omit_frame_pointer;
2194     bool function_sections;
2195     bool include_compiler_rt;
2196     bool test_is_evented;
2197     bool valgrind_enabled;
2198     bool tsan_enabled;
2199 
2200     Buf *root_out_name;
2201     Buf *test_filter;
2202     Buf *test_name_prefix;
2203     Buf *zig_lib_dir;
2204     Buf *zig_std_dir;
2205 };
2206 
2207 struct ZigVar {
2208     const char *name;
2209     ZigValue *const_value;
2210     ZigType *var_type;
2211     LLVMValueRef value_ref;
2212     Stage1ZirInst *is_comptime;
2213     Stage1AirInst *ptr_instruction;
2214     // which node is the declaration of the variable
2215     AstNode *decl_node;
2216     ZigLLVMDILocalVariable *di_loc_var;
2217     size_t src_arg_index;
2218     Scope *parent_scope;
2219     Scope *child_scope;
2220     LLVMValueRef param_value_ref;
2221 
2222     Buf *section_name;
2223 
2224     // In an inline loop, multiple variables may be created,
2225     // In this case, a reference to a variable should follow
2226     // this pointer to the redefined variable.
2227     ZigVar *next_var;
2228 
2229     ZigList<GlobalExport> export_list;
2230 
2231     uint32_t align_bytes;
2232     uint32_t ref_count;
2233 
2234     bool shadowable;
2235     bool src_is_const;
2236     bool gen_is_const;
2237     bool is_thread_local;
2238     bool is_comptime_memoized;
2239     bool is_comptime_memoized_value;
2240     bool did_the_decl_codegen;
2241 };
2242 
2243 struct ErrorTableEntry {
2244     Buf name;
2245     uint32_t value;
2246     AstNode *decl_node;
2247     ErrorTableEntry *other; // null, or another error decl that was merged into this
2248     ZigType *set_with_only_this_in_it;
2249     // If we generate a constant error name value for this error, we memoize it here.
2250     // The type of this is array
2251     ZigValue *cached_error_name_val;
2252 };
2253 
2254 enum ScopeId {
2255     ScopeIdDecls,
2256     ScopeIdBlock,
2257     ScopeIdDefer,
2258     ScopeIdDeferExpr,
2259     ScopeIdVarDecl,
2260     ScopeIdCImport,
2261     ScopeIdLoop,
2262     ScopeIdSuspend,
2263     ScopeIdFnDef,
2264     ScopeIdCompTime,
2265     ScopeIdRuntime,
2266     ScopeIdTypeOf,
2267     ScopeIdExpr,
2268     ScopeIdNoSuspend,
2269 };
2270 
2271 struct Scope {
2272     CodeGen *codegen;
2273     AstNode *source_node;
2274 
2275     // if the scope has a parent, this is it
2276     Scope *parent;
2277 
2278     ZigLLVMDIScope *di_scope;
2279     ScopeId id;
2280 };
2281 
2282 // This scope comes from global declarations or from
2283 // declarations in a container declaration
2284 // NodeTypeContainerDecl
2285 struct ScopeDecls {
2286     Scope base;
2287 
2288     HashMap<Buf *, Tld *, buf_hash, buf_eql_buf> decl_table;
2289     ZigList<TldUsingNamespace *> use_decls;
2290     AstNode *safety_set_node;
2291     AstNode *fast_math_set_node;
2292     ZigType *import;
2293     // If this is a scope from a container, this is the type entry, otherwise null
2294     ZigType *container_type;
2295     Buf *bare_name;
2296 
2297     bool safety_off;
2298     bool fast_math_on;
2299     bool any_imports_failed;
2300 };
2301 
2302 enum LVal {
2303     LValNone,
2304     LValPtr,
2305     LValAssign,
2306 };
2307 
2308 // This scope comes from a block expression in user code.
2309 // NodeTypeBlock
2310 struct ScopeBlock {
2311     Scope base;
2312 
2313     Buf *name;
2314     Stage1ZirBasicBlock *end_block;
2315     Stage1ZirInst *is_comptime;
2316     ResultLocPeerParent *peer_parent;
2317     ZigList<Stage1ZirInst *> *incoming_values;
2318     ZigList<Stage1ZirBasicBlock *> *incoming_blocks;
2319 
2320     AstNode *safety_set_node;
2321     AstNode *fast_math_set_node;
2322 
2323     LVal lval;
2324     bool safety_off;
2325     bool fast_math_on;
2326     bool name_used;
2327 };
2328 
2329 // This scope is created from every defer expression.
2330 // It's the code following the defer statement.
2331 // NodeTypeDefer
2332 struct ScopeDefer {
2333     Scope base;
2334 };
2335 
2336 // This scope is created from every defer expression.
2337 // It's the parent of the defer expression itself.
2338 // NodeTypeDefer
2339 struct ScopeDeferExpr {
2340     Scope base;
2341 
2342     bool reported_err;
2343 };
2344 
2345 // This scope is created for every variable declaration inside an IrExecutable
2346 // NodeTypeVariableDeclaration, NodeTypeParamDecl
2347 struct ScopeVarDecl {
2348     Scope base;
2349 
2350     // The variable that creates this scope
2351     ZigVar *var;
2352 };
2353 
2354 // This scope is created for a @cImport
2355 // NodeTypeFnCallExpr
2356 struct ScopeCImport {
2357     Scope base;
2358 
2359     Buf buf;
2360 };
2361 
2362 // This scope is created for a loop such as for or while in order to
2363 // make break and continue statements work.
2364 // NodeTypeForExpr or NodeTypeWhileExpr
2365 struct ScopeLoop {
2366     Scope base;
2367 
2368     LVal lval;
2369     Buf *name;
2370     Stage1ZirBasicBlock *break_block;
2371     Stage1ZirBasicBlock *continue_block;
2372     Stage1ZirInst *is_comptime;
2373     ZigList<Stage1ZirInst *> *incoming_values;
2374     ZigList<Stage1ZirBasicBlock *> *incoming_blocks;
2375     ResultLocPeerParent *peer_parent;
2376     ScopeExpr *spill_scope;
2377 
2378     bool name_used;
2379 };
2380 
2381 // This scope blocks certain things from working such as comptime continue
2382 // inside a runtime if expression.
2383 // NodeTypeIfBoolExpr, NodeTypeWhileExpr, NodeTypeForExpr
2384 struct ScopeRuntime {
2385     Scope base;
2386 
2387     Stage1ZirInst *is_comptime;
2388 };
2389 
2390 // This scope is created for a suspend block in order to have labeled
2391 // suspend for breaking out of a suspend and for detecting if a suspend
2392 // block is inside a suspend block.
2393 struct ScopeSuspend {
2394     Scope base;
2395 
2396     bool reported_err;
2397 };
2398 
2399 // This scope is created for a comptime expression.
2400 // NodeTypeCompTime, NodeTypeSwitchExpr
2401 struct ScopeCompTime {
2402     Scope base;
2403 };
2404 
2405 // This scope is created for a nosuspend expression.
2406 // NodeTypeNoSuspend
2407 struct ScopeNoSuspend {
2408     Scope base;
2409 };
2410 
2411 // This scope is created for a function definition.
2412 // NodeTypeFnDef
2413 struct ScopeFnDef {
2414     Scope base;
2415 
2416     ZigFn *fn_entry;
2417 };
2418 
2419 // This scope is created for a @TypeOf.
2420 // All runtime side-effects are elided within it.
2421 // NodeTypeFnCallExpr
2422 struct ScopeTypeOf {
2423     Scope base;
2424 };
2425 
2426 enum MemoizedBool {
2427     MemoizedBoolUnknown,
2428     MemoizedBoolFalse,
2429     MemoizedBoolTrue,
2430 };
2431 
2432 // This scope is created for each expression.
2433 // It's used to identify when an instruction needs to be spilled,
2434 // so that it can be accessed after a suspend point.
2435 struct ScopeExpr {
2436     Scope base;
2437 
2438     ScopeExpr **children_ptr;
2439     size_t children_len;
2440 
2441     MemoizedBool need_spill;
2442     // This is a hack. I apologize for this, I need this to work so that I
2443     // can make progress on other fronts. I'll pay off this tech debt eventually.
2444     bool spill_harder;
2445 };
2446 
2447 // synchronized with code in define_builtin_compile_vars
2448 enum AtomicOrder {
2449     AtomicOrderUnordered,
2450     AtomicOrderMonotonic,
2451     AtomicOrderAcquire,
2452     AtomicOrderRelease,
2453     AtomicOrderAcqRel,
2454     AtomicOrderSeqCst,
2455 };
2456 
2457 // synchronized with code in define_builtin_compile_vars
2458 enum ReduceOp {
2459     ReduceOp_and,
2460     ReduceOp_or,
2461     ReduceOp_xor,
2462     ReduceOp_min,
2463     ReduceOp_max,
2464     ReduceOp_add,
2465     ReduceOp_mul,
2466 };
2467 
2468 // synchronized with the code in define_builtin_compile_vars
2469 enum AtomicRmwOp {
2470     AtomicRmwOp_xchg,
2471     AtomicRmwOp_add,
2472     AtomicRmwOp_sub,
2473     AtomicRmwOp_and,
2474     AtomicRmwOp_nand,
2475     AtomicRmwOp_or,
2476     AtomicRmwOp_xor,
2477     AtomicRmwOp_max,
2478     AtomicRmwOp_min,
2479 };
2480 
2481 // A basic block contains no branching. Branches send control flow
2482 // to another basic block.
2483 // Phi instructions must be first in a basic block.
2484 // The last instruction in a basic block must be of type unreachable.
2485 struct Stage1ZirBasicBlock {
2486     ZigList<Stage1ZirInst *> instruction_list;
2487     Stage1AirBasicBlock *child;
2488     Scope *scope;
2489     const char *name_hint;
2490     Stage1ZirInst *suspend_instruction_ref;
2491 
2492     uint32_t ref_count;
2493     uint32_t index; // index into the basic block list
2494 
2495     uint32_t debug_id;
2496     bool suspended;
2497     bool in_resume_stack;
2498 };
2499 
2500 struct Stage1AirBasicBlock {
2501     ZigList<Stage1AirInst *> instruction_list;
2502     Scope *scope;
2503     const char *name_hint;
2504     LLVMBasicBlockRef llvm_block;
2505     LLVMBasicBlockRef llvm_exit_block;
2506     // The instruction that referenced this basic block and caused us to
2507     // analyze the basic block. If the same instruction wants us to emit
2508     // the same basic block, then we re-generate it instead of saving it.
2509     Stage1ZirInst *ref_instruction;
2510     // When this is non-null, a branch to this basic block is only allowed
2511     // if the branch is comptime. The instruction points to the reason
2512     // the basic block must be comptime.
2513     AstNode *must_be_comptime_source_node;
2514 
2515     uint32_t debug_id;
2516     bool already_appended;
2517 };
2518 
2519 // Src instructions are generated by ir_gen_* functions in ir.cpp from AST.
2520 // ir_analyze_* functions consume Src instructions and produce Gen instructions.
2521 // Src instructions do not have type information; Gen instructions do.
2522 enum Stage1ZirInstId : uint8_t {
2523     Stage1ZirInstIdInvalid,
2524     Stage1ZirInstIdDeclVar,
2525     Stage1ZirInstIdBr,
2526     Stage1ZirInstIdCondBr,
2527     Stage1ZirInstIdSwitchBr,
2528     Stage1ZirInstIdSwitchVar,
2529     Stage1ZirInstIdSwitchElseVar,
2530     Stage1ZirInstIdSwitchTarget,
2531     Stage1ZirInstIdPhi,
2532     Stage1ZirInstIdUnOp,
2533     Stage1ZirInstIdBinOp,
2534     Stage1ZirInstIdMergeErrSets,
2535     Stage1ZirInstIdLoadPtr,
2536     Stage1ZirInstIdStorePtr,
2537     Stage1ZirInstIdFieldPtr,
2538     Stage1ZirInstIdElemPtr,
2539     Stage1ZirInstIdVarPtr,
2540     Stage1ZirInstIdCall,
2541     Stage1ZirInstIdCallArgs,
2542     Stage1ZirInstIdCallExtra,
2543     Stage1ZirInstIdAsyncCallExtra,
2544     Stage1ZirInstIdConst,
2545     Stage1ZirInstIdReturn,
2546     Stage1ZirInstIdContainerInitList,
2547     Stage1ZirInstIdContainerInitFields,
2548     Stage1ZirInstIdUnreachable,
2549     Stage1ZirInstIdTypeOf,
2550     Stage1ZirInstIdSetCold,
2551     Stage1ZirInstIdSetRuntimeSafety,
2552     Stage1ZirInstIdSetFloatMode,
2553     Stage1ZirInstIdArrayType,
2554     Stage1ZirInstIdAnyFrameType,
2555     Stage1ZirInstIdSliceType,
2556     Stage1ZirInstIdAsm,
2557     Stage1ZirInstIdSizeOf,
2558     Stage1ZirInstIdTestNonNull,
2559     Stage1ZirInstIdOptionalUnwrapPtr,
2560     Stage1ZirInstIdClz,
2561     Stage1ZirInstIdCtz,
2562     Stage1ZirInstIdPopCount,
2563     Stage1ZirInstIdBswap,
2564     Stage1ZirInstIdBitReverse,
2565     Stage1ZirInstIdImport,
2566     Stage1ZirInstIdCImport,
2567     Stage1ZirInstIdCInclude,
2568     Stage1ZirInstIdCDefine,
2569     Stage1ZirInstIdCUndef,
2570     Stage1ZirInstIdRef,
2571     Stage1ZirInstIdCompileErr,
2572     Stage1ZirInstIdCompileLog,
2573     Stage1ZirInstIdErrName,
2574     Stage1ZirInstIdEmbedFile,
2575     Stage1ZirInstIdCmpxchg,
2576     Stage1ZirInstIdFence,
2577     Stage1ZirInstIdReduce,
2578     Stage1ZirInstIdTruncate,
2579     Stage1ZirInstIdIntCast,
2580     Stage1ZirInstIdFloatCast,
2581     Stage1ZirInstIdIntToFloat,
2582     Stage1ZirInstIdFloatToInt,
2583     Stage1ZirInstIdBoolToInt,
2584     Stage1ZirInstIdVectorType,
2585     Stage1ZirInstIdShuffleVector,
2586     Stage1ZirInstIdSelect,
2587     Stage1ZirInstIdSplat,
2588     Stage1ZirInstIdBoolNot,
2589     Stage1ZirInstIdMemset,
2590     Stage1ZirInstIdMemcpy,
2591     Stage1ZirInstIdSlice,
2592     Stage1ZirInstIdBreakpoint,
2593     Stage1ZirInstIdReturnAddress,
2594     Stage1ZirInstIdFrameAddress,
2595     Stage1ZirInstIdFrameHandle,
2596     Stage1ZirInstIdFrameType,
2597     Stage1ZirInstIdFrameSize,
2598     Stage1ZirInstIdAlignOf,
2599     Stage1ZirInstIdOverflowOp,
2600     Stage1ZirInstIdTestErr,
2601     Stage1ZirInstIdMulAdd,
2602     Stage1ZirInstIdFloatOp,
2603     Stage1ZirInstIdUnwrapErrCode,
2604     Stage1ZirInstIdUnwrapErrPayload,
2605     Stage1ZirInstIdFnProto,
2606     Stage1ZirInstIdTestComptime,
2607     Stage1ZirInstIdPtrCast,
2608     Stage1ZirInstIdBitCast,
2609     Stage1ZirInstIdIntToPtr,
2610     Stage1ZirInstIdPtrToInt,
2611     Stage1ZirInstIdIntToEnum,
2612     Stage1ZirInstIdEnumToInt,
2613     Stage1ZirInstIdIntToErr,
2614     Stage1ZirInstIdErrToInt,
2615     Stage1ZirInstIdCheckSwitchProngsUnderYes,
2616     Stage1ZirInstIdCheckSwitchProngsUnderNo,
2617     Stage1ZirInstIdCheckStatementIsVoid,
2618     Stage1ZirInstIdTypeName,
2619     Stage1ZirInstIdDeclRef,
2620     Stage1ZirInstIdPanic,
2621     Stage1ZirInstIdTagName,
2622     Stage1ZirInstIdFieldParentPtr,
2623     Stage1ZirInstIdOffsetOf,
2624     Stage1ZirInstIdBitOffsetOf,
2625     Stage1ZirInstIdTypeInfo,
2626     Stage1ZirInstIdType,
2627     Stage1ZirInstIdHasField,
2628     Stage1ZirInstIdSetEvalBranchQuota,
2629     Stage1ZirInstIdPtrType,
2630     Stage1ZirInstIdPtrTypeSimple,
2631     Stage1ZirInstIdPtrTypeSimpleConst,
2632     Stage1ZirInstIdAlignCast,
2633     Stage1ZirInstIdImplicitCast,
2634     Stage1ZirInstIdResolveResult,
2635     Stage1ZirInstIdResetResult,
2636     Stage1ZirInstIdSetAlignStack,
2637     Stage1ZirInstIdArgTypeAllowVarFalse,
2638     Stage1ZirInstIdArgTypeAllowVarTrue,
2639     Stage1ZirInstIdExport,
2640     Stage1ZirInstIdExtern,
2641     Stage1ZirInstIdErrorReturnTrace,
2642     Stage1ZirInstIdErrorUnion,
2643     Stage1ZirInstIdAtomicRmw,
2644     Stage1ZirInstIdAtomicLoad,
2645     Stage1ZirInstIdAtomicStore,
2646     Stage1ZirInstIdSaveErrRetAddr,
2647     Stage1ZirInstIdAddImplicitReturnType,
2648     Stage1ZirInstIdErrSetCast,
2649     Stage1ZirInstIdCheckRuntimeScope,
2650     Stage1ZirInstIdHasDecl,
2651     Stage1ZirInstIdUndeclaredIdent,
2652     Stage1ZirInstIdAlloca,
2653     Stage1ZirInstIdEndExpr,
2654     Stage1ZirInstIdUnionInitNamedField,
2655     Stage1ZirInstIdSuspendBegin,
2656     Stage1ZirInstIdSuspendFinish,
2657     Stage1ZirInstIdAwait,
2658     Stage1ZirInstIdResume,
2659     Stage1ZirInstIdSpillBegin,
2660     Stage1ZirInstIdSpillEnd,
2661     Stage1ZirInstIdWasmMemorySize,
2662     Stage1ZirInstIdWasmMemoryGrow,
2663     Stage1ZirInstIdSrc,
2664     Stage1ZirInstIdPrefetch,
2665 };
2666 
2667 // ir_render_* functions in codegen.cpp consume Gen instructions and produce LLVM IR.
2668 // Src instructions do not have type information; Gen instructions do.
2669 enum Stage1AirInstId : uint8_t {
2670     Stage1AirInstIdInvalid,
2671     Stage1AirInstIdDeclVar,
2672     Stage1AirInstIdBr,
2673     Stage1AirInstIdCondBr,
2674     Stage1AirInstIdSwitchBr,
2675     Stage1AirInstIdPhi,
2676     Stage1AirInstIdBinaryNot,
2677     Stage1AirInstIdNegation,
2678     Stage1AirInstIdBinOp,
2679     Stage1AirInstIdLoadPtr,
2680     Stage1AirInstIdStorePtr,
2681     Stage1AirInstIdVectorStoreElem,
2682     Stage1AirInstIdStructFieldPtr,
2683     Stage1AirInstIdUnionFieldPtr,
2684     Stage1AirInstIdElemPtr,
2685     Stage1AirInstIdVarPtr,
2686     Stage1AirInstIdReturnPtr,
2687     Stage1AirInstIdCall,
2688     Stage1AirInstIdReturn,
2689     Stage1AirInstIdCast,
2690     Stage1AirInstIdUnreachable,
2691     Stage1AirInstIdAsm,
2692     Stage1AirInstIdTestNonNull,
2693     Stage1AirInstIdOptionalUnwrapPtr,
2694     Stage1AirInstIdOptionalWrap,
2695     Stage1AirInstIdUnionTag,
2696     Stage1AirInstIdClz,
2697     Stage1AirInstIdCtz,
2698     Stage1AirInstIdPopCount,
2699     Stage1AirInstIdBswap,
2700     Stage1AirInstIdBitReverse,
2701     Stage1AirInstIdRef,
2702     Stage1AirInstIdErrName,
2703     Stage1AirInstIdCmpxchg,
2704     Stage1AirInstIdFence,
2705     Stage1AirInstIdReduce,
2706     Stage1AirInstIdTruncate,
2707     Stage1AirInstIdShuffleVector,
2708     Stage1AirInstIdSelect,
2709     Stage1AirInstIdSplat,
2710     Stage1AirInstIdBoolNot,
2711     Stage1AirInstIdMemset,
2712     Stage1AirInstIdMemcpy,
2713     Stage1AirInstIdSlice,
2714     Stage1AirInstIdBreakpoint,
2715     Stage1AirInstIdReturnAddress,
2716     Stage1AirInstIdFrameAddress,
2717     Stage1AirInstIdFrameHandle,
2718     Stage1AirInstIdFrameSize,
2719     Stage1AirInstIdOverflowOp,
2720     Stage1AirInstIdTestErr,
2721     Stage1AirInstIdMulAdd,
2722     Stage1AirInstIdFloatOp,
2723     Stage1AirInstIdUnwrapErrCode,
2724     Stage1AirInstIdUnwrapErrPayload,
2725     Stage1AirInstIdErrWrapCode,
2726     Stage1AirInstIdErrWrapPayload,
2727     Stage1AirInstIdPtrCast,
2728     Stage1AirInstIdBitCast,
2729     Stage1AirInstIdWidenOrShorten,
2730     Stage1AirInstIdIntToPtr,
2731     Stage1AirInstIdPtrToInt,
2732     Stage1AirInstIdIntToEnum,
2733     Stage1AirInstIdIntToErr,
2734     Stage1AirInstIdErrToInt,
2735     Stage1AirInstIdPanic,
2736     Stage1AirInstIdTagName,
2737     Stage1AirInstIdFieldParentPtr,
2738     Stage1AirInstIdAlignCast,
2739     Stage1AirInstIdErrorReturnTrace,
2740     Stage1AirInstIdAtomicRmw,
2741     Stage1AirInstIdAtomicLoad,
2742     Stage1AirInstIdAtomicStore,
2743     Stage1AirInstIdSaveErrRetAddr,
2744     Stage1AirInstIdVectorToArray,
2745     Stage1AirInstIdArrayToVector,
2746     Stage1AirInstIdAssertZero,
2747     Stage1AirInstIdAssertNonNull,
2748     Stage1AirInstIdPtrOfArrayToSlice,
2749     Stage1AirInstIdSuspendBegin,
2750     Stage1AirInstIdSuspendFinish,
2751     Stage1AirInstIdAwait,
2752     Stage1AirInstIdResume,
2753     Stage1AirInstIdSpillBegin,
2754     Stage1AirInstIdSpillEnd,
2755     Stage1AirInstIdVectorExtractElem,
2756     Stage1AirInstIdAlloca,
2757     Stage1AirInstIdConst,
2758     Stage1AirInstIdWasmMemorySize,
2759     Stage1AirInstIdWasmMemoryGrow,
2760     Stage1AirInstIdExtern,
2761     Stage1AirInstIdPrefetch,
2762 };
2763 
2764 struct Stage1ZirInst {
2765     Stage1ZirInstId id;
2766     uint16_t ref_count;
2767     uint32_t debug_id;
2768 
2769     Scope *scope;
2770     AstNode *source_node;
2771 
2772     // When analyzing IR, instructions that point to this instruction in the "old ir"
2773     // can find the instruction that corresponds to this value in the "new ir"
2774     // with this child field.
2775     Stage1AirInst *child;
2776     Stage1ZirBasicBlock *owner_bb;
2777 
2778     // for debugging purposes, these are useful to call to inspect the instruction
2779     void dump();
2780     void src();
2781 };
2782 
2783 struct Stage1AirInst {
2784     Stage1AirInstId id;
2785     // if ref_count is zero and the instruction has no side effects,
2786     // the instruction can be omitted in codegen
2787     uint16_t ref_count;
2788     uint32_t debug_id;
2789 
2790     Scope *scope;
2791     AstNode *source_node;
2792 
2793     LLVMValueRef llvm_value;
2794     ZigValue *value;
2795     // Nearly any instruction can have to be stored as a local variable before suspending
2796     // and then loaded after resuming, in case there is an expression with a suspend point
2797     // in it, such as: x + await y
2798     Stage1AirInst *spill;
2799 
2800     // for debugging purposes, these are useful to call to inspect the instruction
2801     void dump();
2802     void src();
2803 };
2804 
2805 struct Stage1ZirInstDeclVar {
2806     Stage1ZirInst base;
2807 
2808     ZigVar *var;
2809     Stage1ZirInst *var_type;
2810     Stage1ZirInst *align_value;
2811     Stage1ZirInst *ptr;
2812 };
2813 
2814 struct Stage1AirInstDeclVar {
2815     Stage1AirInst base;
2816 
2817     ZigVar *var;
2818     Stage1AirInst *var_ptr;
2819 };
2820 
2821 struct Stage1ZirInstCondBr {
2822     Stage1ZirInst base;
2823 
2824     Stage1ZirInst *condition;
2825     Stage1ZirBasicBlock *then_block;
2826     Stage1ZirBasicBlock *else_block;
2827     Stage1ZirInst *is_comptime;
2828     ResultLoc *result_loc;
2829 };
2830 
2831 struct Stage1AirInstCondBr {
2832     Stage1AirInst base;
2833 
2834     Stage1AirInst *condition;
2835     Stage1AirBasicBlock *then_block;
2836     Stage1AirBasicBlock *else_block;
2837 };
2838 
2839 struct Stage1ZirInstBr {
2840     Stage1ZirInst base;
2841 
2842     Stage1ZirBasicBlock *dest_block;
2843     Stage1ZirInst *is_comptime;
2844 };
2845 
2846 struct Stage1AirInstBr {
2847     Stage1AirInst base;
2848 
2849     Stage1AirBasicBlock *dest_block;
2850 };
2851 
2852 struct Stage1ZirInstSwitchBrCase {
2853     Stage1ZirInst *value;
2854     Stage1ZirBasicBlock *block;
2855 };
2856 
2857 struct Stage1ZirInstSwitchBr {
2858     Stage1ZirInst base;
2859 
2860     Stage1ZirInst *target_value;
2861     Stage1ZirBasicBlock *else_block;
2862     size_t case_count;
2863     Stage1ZirInstSwitchBrCase *cases;
2864     Stage1ZirInst *is_comptime;
2865     Stage1ZirInst *switch_prongs_void;
2866 };
2867 
2868 struct Stage1AirInstSwitchBrCase {
2869     Stage1AirInst *value;
2870     Stage1AirBasicBlock *block;
2871 };
2872 
2873 struct Stage1AirInstSwitchBr {
2874     Stage1AirInst base;
2875 
2876     Stage1AirInst *target_value;
2877     Stage1AirBasicBlock *else_block;
2878     size_t case_count;
2879     Stage1AirInstSwitchBrCase *cases;
2880 };
2881 
2882 struct Stage1ZirInstSwitchVar {
2883     Stage1ZirInst base;
2884 
2885     Stage1ZirInst *target_value_ptr;
2886     Stage1ZirInst **prongs_ptr;
2887     size_t prongs_len;
2888 };
2889 
2890 struct Stage1ZirInstSwitchElseVar {
2891     Stage1ZirInst base;
2892 
2893     Stage1ZirInst *target_value_ptr;
2894     Stage1ZirInstSwitchBr *switch_br;
2895 };
2896 
2897 struct Stage1ZirInstSwitchTarget {
2898     Stage1ZirInst base;
2899 
2900     Stage1ZirInst *target_value_ptr;
2901 };
2902 
2903 struct Stage1ZirInstPhi {
2904     Stage1ZirInst base;
2905 
2906     size_t incoming_count;
2907     Stage1ZirBasicBlock **incoming_blocks;
2908     Stage1ZirInst **incoming_values;
2909     ResultLocPeerParent *peer_parent;
2910 };
2911 
2912 struct Stage1AirInstPhi {
2913     Stage1AirInst base;
2914 
2915     size_t incoming_count;
2916     Stage1AirBasicBlock **incoming_blocks;
2917     Stage1AirInst **incoming_values;
2918 };
2919 
2920 enum IrUnOp {
2921     IrUnOpInvalid,
2922     IrUnOpBinNot,
2923     IrUnOpNegation,
2924     IrUnOpNegationWrap,
2925     IrUnOpDereference,
2926     IrUnOpOptional,
2927 };
2928 
2929 struct Stage1ZirInstUnOp {
2930     Stage1ZirInst base;
2931 
2932     IrUnOp op_id;
2933     LVal lval;
2934     Stage1ZirInst *value;
2935     ResultLoc *result_loc;
2936 };
2937 
2938 struct Stage1AirInstBinaryNot {
2939     Stage1AirInst base;
2940     Stage1AirInst *operand;
2941 };
2942 
2943 struct Stage1AirInstNegation {
2944     Stage1AirInst base;
2945     Stage1AirInst *operand;
2946     bool wrapping;
2947 };
2948 
2949 enum IrBinOp {
2950     IrBinOpInvalid,
2951     IrBinOpBoolOr,
2952     IrBinOpBoolAnd,
2953     IrBinOpCmpEq,
2954     IrBinOpCmpNotEq,
2955     IrBinOpCmpLessThan,
2956     IrBinOpCmpGreaterThan,
2957     IrBinOpCmpLessOrEq,
2958     IrBinOpCmpGreaterOrEq,
2959     IrBinOpBinOr,
2960     IrBinOpBinXor,
2961     IrBinOpBinAnd,
2962     IrBinOpBitShiftLeftLossy,
2963     IrBinOpBitShiftLeftExact,
2964     IrBinOpBitShiftRightLossy,
2965     IrBinOpBitShiftRightExact,
2966     IrBinOpAdd,
2967     IrBinOpAddWrap,
2968     IrBinOpSub,
2969     IrBinOpSubWrap,
2970     IrBinOpMult,
2971     IrBinOpMultWrap,
2972     IrBinOpDivUnspecified,
2973     IrBinOpDivExact,
2974     IrBinOpDivTrunc,
2975     IrBinOpDivFloor,
2976     IrBinOpRemUnspecified,
2977     IrBinOpRemRem,
2978     IrBinOpRemMod,
2979     IrBinOpArrayCat,
2980     IrBinOpArrayMult,
2981     IrBinOpMaximum,
2982     IrBinOpMinimum,
2983     IrBinOpAddSat,
2984     IrBinOpSubSat,
2985     IrBinOpMultSat,
2986     IrBinOpShlSat,
2987 };
2988 
2989 struct Stage1ZirInstBinOp {
2990     Stage1ZirInst base;
2991 
2992     Stage1ZirInst *op1;
2993     Stage1ZirInst *op2;
2994     IrBinOp op_id;
2995     bool safety_check_on;
2996 };
2997 
2998 struct Stage1AirInstBinOp {
2999     Stage1AirInst base;
3000 
3001     Stage1AirInst *op1;
3002     Stage1AirInst *op2;
3003     IrBinOp op_id;
3004     bool safety_check_on;
3005 };
3006 
3007 struct Stage1ZirInstMergeErrSets {
3008     Stage1ZirInst base;
3009 
3010     Stage1ZirInst *op1;
3011     Stage1ZirInst *op2;
3012     Buf *type_name;
3013 };
3014 
3015 struct Stage1ZirInstLoadPtr {
3016     Stage1ZirInst base;
3017 
3018     Stage1ZirInst *ptr;
3019 };
3020 
3021 struct Stage1AirInstLoadPtr {
3022     Stage1AirInst base;
3023 
3024     Stage1AirInst *ptr;
3025     Stage1AirInst *result_loc;
3026 };
3027 
3028 struct Stage1ZirInstStorePtr {
3029     Stage1ZirInst base;
3030 
3031     Stage1ZirInst *ptr;
3032     Stage1ZirInst *value;
3033 
3034     bool allow_write_through_const;
3035 };
3036 
3037 struct Stage1AirInstStorePtr {
3038     Stage1AirInst base;
3039 
3040     Stage1AirInst *ptr;
3041     Stage1AirInst *value;
3042 };
3043 
3044 struct Stage1AirInstVectorStoreElem {
3045     Stage1AirInst base;
3046 
3047     Stage1AirInst *vector_ptr;
3048     Stage1AirInst *index;
3049     Stage1AirInst *value;
3050 };
3051 
3052 struct Stage1ZirInstFieldPtr {
3053     Stage1ZirInst base;
3054 
3055     Stage1ZirInst *container_ptr;
3056     Buf *field_name_buffer;
3057     Stage1ZirInst *field_name_expr;
3058     bool initializing;
3059 };
3060 
3061 struct Stage1AirInstStructFieldPtr {
3062     Stage1AirInst base;
3063 
3064     Stage1AirInst *struct_ptr;
3065     TypeStructField *field;
3066     bool is_const;
3067 };
3068 
3069 struct Stage1AirInstUnionFieldPtr {
3070     Stage1AirInst base;
3071 
3072     Stage1AirInst *union_ptr;
3073     TypeUnionField *field;
3074     bool safety_check_on;
3075     bool initializing;
3076 };
3077 
3078 struct Stage1ZirInstElemPtr {
3079     Stage1ZirInst base;
3080 
3081     Stage1ZirInst *array_ptr;
3082     Stage1ZirInst *elem_index;
3083     AstNode *init_array_type_source_node;
3084     PtrLen ptr_len;
3085     bool safety_check_on;
3086 };
3087 
3088 struct Stage1AirInstElemPtr {
3089     Stage1AirInst base;
3090 
3091     Stage1AirInst *array_ptr;
3092     Stage1AirInst *elem_index;
3093     bool safety_check_on;
3094 };
3095 
3096 struct Stage1ZirInstVarPtr {
3097     Stage1ZirInst base;
3098 
3099     ZigVar *var;
3100     ScopeFnDef *crossed_fndef_scope;
3101 };
3102 
3103 struct Stage1AirInstVarPtr {
3104     Stage1AirInst base;
3105 
3106     ZigVar *var;
3107 };
3108 
3109 // For functions that have a return type for which handle_is_ptr is true, a
3110 // result location pointer is the secret first parameter ("sret"). This
3111 // instruction returns that pointer.
3112 struct Stage1AirInstReturnPtr {
3113     Stage1AirInst base;
3114 };
3115 
3116 struct Stage1ZirInstCall {
3117     Stage1ZirInst base;
3118 
3119     Stage1ZirInst *fn_ref;
3120     ZigFn *fn_entry;
3121     size_t arg_count;
3122     Stage1ZirInst **args;
3123     Stage1ZirInst *ret_ptr;
3124     ResultLoc *result_loc;
3125 
3126     Stage1ZirInst *new_stack;
3127 
3128     CallModifier modifier;
3129     bool is_async_call_builtin;
3130 };
3131 
3132 // This is a pass1 instruction, used by @call when the args node is
3133 // a tuple or struct literal.
3134 struct Stage1ZirInstCallArgs {
3135     Stage1ZirInst base;
3136 
3137     Stage1ZirInst *options;
3138     Stage1ZirInst *fn_ref;
3139     Stage1ZirInst **args_ptr;
3140     size_t args_len;
3141     ResultLoc *result_loc;
3142 };
3143 
3144 // This is a pass1 instruction, used by @call, when the args node
3145 // is not a literal.
3146 // `args` is expected to be either a struct or a tuple.
3147 struct Stage1ZirInstCallExtra {
3148     Stage1ZirInst base;
3149 
3150     Stage1ZirInst *options;
3151     Stage1ZirInst *fn_ref;
3152     Stage1ZirInst *args;
3153     ResultLoc *result_loc;
3154 };
3155 
3156 // This is a pass1 instruction, used by @asyncCall, when the args node
3157 // is not a literal.
3158 // `args` is expected to be either a struct or a tuple.
3159 struct Stage1ZirInstAsyncCallExtra {
3160     Stage1ZirInst base;
3161 
3162     CallModifier modifier;
3163     Stage1ZirInst *fn_ref;
3164     Stage1ZirInst *ret_ptr;
3165     Stage1ZirInst *new_stack;
3166     Stage1ZirInst *args;
3167     ResultLoc *result_loc;
3168 };
3169 
3170 struct Stage1AirInstCall {
3171     Stage1AirInst base;
3172 
3173     Stage1AirInst *fn_ref;
3174     ZigFn *fn_entry;
3175     size_t arg_count;
3176     Stage1AirInst **args;
3177     Stage1AirInst *result_loc;
3178     Stage1AirInst *frame_result_loc;
3179     Stage1AirInst *new_stack;
3180 
3181     CallModifier modifier;
3182 
3183     bool is_async_call_builtin;
3184 };
3185 
3186 struct Stage1ZirInstConst {
3187     Stage1ZirInst base;
3188 
3189     ZigValue *value;
3190 };
3191 
3192 struct Stage1AirInstConst {
3193     Stage1AirInst base;
3194 };
3195 
3196 struct Stage1ZirInstReturn {
3197     Stage1ZirInst base;
3198 
3199     Stage1ZirInst *operand;
3200 };
3201 
3202 // When an IrExecutable is not in a function, a return instruction means that
3203 // the expression returns with that value, even though a return statement from
3204 // an AST perspective is invalid.
3205 struct Stage1AirInstReturn {
3206     Stage1AirInst base;
3207 
3208     Stage1AirInst *operand;
3209 };
3210 
3211 enum CastOp {
3212     CastOpNoCast, // signifies the function call expression is not a cast
3213     CastOpNoop, // fn call expr is a cast, but does nothing
3214     CastOpIntToFloat,
3215     CastOpFloatToInt,
3216     CastOpBoolToInt,
3217     CastOpNumLitToConcrete,
3218     CastOpErrSet,
3219     CastOpBitCast,
3220 };
3221 
3222 // TODO get rid of this instruction, replace with instructions for each op code
3223 struct Stage1AirInstCast {
3224     Stage1AirInst base;
3225 
3226     Stage1AirInst *value;
3227     CastOp cast_op;
3228 };
3229 
3230 struct Stage1ZirInstContainerInitList {
3231     Stage1ZirInst base;
3232 
3233     Stage1ZirInst *elem_type;
3234     size_t item_count;
3235     Stage1ZirInst **elem_result_loc_list;
3236     Stage1ZirInst *result_loc;
3237     AstNode *init_array_type_source_node;
3238 };
3239 
3240 struct Stage1ZirInstContainerInitFieldsField {
3241     Buf *name;
3242     AstNode *source_node;
3243     Stage1ZirInst *result_loc;
3244 };
3245 
3246 struct Stage1ZirInstContainerInitFields {
3247     Stage1ZirInst base;
3248 
3249     size_t field_count;
3250     Stage1ZirInstContainerInitFieldsField *fields;
3251     Stage1ZirInst *result_loc;
3252 };
3253 
3254 struct Stage1ZirInstUnreachable {
3255     Stage1ZirInst base;
3256 };
3257 
3258 struct Stage1AirInstUnreachable {
3259     Stage1AirInst base;
3260 };
3261 
3262 struct Stage1ZirInstTypeOf {
3263     Stage1ZirInst base;
3264 
3265     union {
3266         Stage1ZirInst *scalar; // value_count == 1
3267         Stage1ZirInst **list; // value_count > 1
3268     } value;
3269     size_t value_count;
3270 };
3271 
3272 struct Stage1ZirInstSetCold {
3273     Stage1ZirInst base;
3274 
3275     Stage1ZirInst *is_cold;
3276 };
3277 
3278 struct Stage1ZirInstSetRuntimeSafety {
3279     Stage1ZirInst base;
3280 
3281     Stage1ZirInst *safety_on;
3282 };
3283 
3284 struct Stage1ZirInstSetFloatMode {
3285     Stage1ZirInst base;
3286 
3287     Stage1ZirInst *scope_value;
3288     Stage1ZirInst *mode_value;
3289 };
3290 
3291 struct Stage1ZirInstArrayType {
3292     Stage1ZirInst base;
3293 
3294     Stage1ZirInst *size;
3295     Stage1ZirInst *sentinel;
3296     Stage1ZirInst *child_type;
3297 };
3298 
3299 struct Stage1ZirInstPtrTypeSimple {
3300     Stage1ZirInst base;
3301 
3302     Stage1ZirInst *child_type;
3303 };
3304 
3305 struct Stage1ZirInstPtrType {
3306     Stage1ZirInst base;
3307 
3308     Stage1ZirInst *sentinel;
3309     Stage1ZirInst *align_value;
3310     Stage1ZirInst *child_type;
3311     uint32_t bit_offset_start;
3312     uint32_t host_int_bytes;
3313     PtrLen ptr_len;
3314     bool is_const;
3315     bool is_volatile;
3316     bool is_allow_zero;
3317 };
3318 
3319 struct Stage1ZirInstAnyFrameType {
3320     Stage1ZirInst base;
3321 
3322     Stage1ZirInst *payload_type;
3323 };
3324 
3325 struct Stage1ZirInstSliceType {
3326     Stage1ZirInst base;
3327 
3328     Stage1ZirInst *sentinel;
3329     Stage1ZirInst *align_value;
3330     Stage1ZirInst *child_type;
3331     bool is_const;
3332     bool is_volatile;
3333     bool is_allow_zero;
3334 };
3335 
3336 struct Stage1ZirInstAsm {
3337     Stage1ZirInst base;
3338 
3339     Stage1ZirInst *asm_template;
3340     Stage1ZirInst **input_list;
3341     Stage1ZirInst **output_types;
3342     ZigVar **output_vars;
3343     size_t return_count;
3344     bool has_side_effects;
3345     bool is_global;
3346 };
3347 
3348 struct Stage1AirInstAsm {
3349     Stage1AirInst base;
3350 
3351     Buf *asm_template;
3352     AsmToken *token_list;
3353     size_t token_list_len;
3354     Stage1AirInst **input_list;
3355     Stage1AirInst **output_types;
3356     ZigVar **output_vars;
3357     size_t return_count;
3358     bool has_side_effects;
3359 };
3360 
3361 struct Stage1ZirInstSizeOf {
3362     Stage1ZirInst base;
3363 
3364     Stage1ZirInst *type_value;
3365     bool bit_size;
3366 };
3367 
3368 // returns true if nonnull, returns false if null
3369 struct Stage1ZirInstTestNonNull {
3370     Stage1ZirInst base;
3371 
3372     Stage1ZirInst *value;
3373 };
3374 
3375 struct Stage1AirInstTestNonNull {
3376     Stage1AirInst base;
3377 
3378     Stage1AirInst *value;
3379 };
3380 
3381 // Takes a pointer to an optional value, returns a pointer
3382 // to the payload.
3383 struct Stage1ZirInstOptionalUnwrapPtr {
3384     Stage1ZirInst base;
3385 
3386     Stage1ZirInst *base_ptr;
3387     bool safety_check_on;
3388 };
3389 
3390 struct Stage1AirInstOptionalUnwrapPtr {
3391     Stage1AirInst base;
3392 
3393     Stage1AirInst *base_ptr;
3394     bool safety_check_on;
3395     bool initializing;
3396 };
3397 
3398 struct Stage1ZirInstCtz {
3399     Stage1ZirInst base;
3400 
3401     Stage1ZirInst *type;
3402     Stage1ZirInst *op;
3403 };
3404 
3405 struct Stage1AirInstCtz {
3406     Stage1AirInst base;
3407 
3408     Stage1AirInst *op;
3409 };
3410 
3411 struct Stage1ZirInstClz {
3412     Stage1ZirInst base;
3413 
3414     Stage1ZirInst *type;
3415     Stage1ZirInst *op;
3416 };
3417 
3418 struct Stage1AirInstClz {
3419     Stage1AirInst base;
3420 
3421     Stage1AirInst *op;
3422 };
3423 
3424 struct Stage1ZirInstPopCount {
3425     Stage1ZirInst base;
3426 
3427     Stage1ZirInst *type;
3428     Stage1ZirInst *op;
3429 };
3430 
3431 struct Stage1AirInstPopCount {
3432     Stage1AirInst base;
3433 
3434     Stage1AirInst *op;
3435 };
3436 
3437 struct Stage1AirInstUnionTag {
3438     Stage1AirInst base;
3439 
3440     Stage1AirInst *value;
3441 };
3442 
3443 struct Stage1ZirInstImport {
3444     Stage1ZirInst base;
3445 
3446     Stage1ZirInst *name;
3447 };
3448 
3449 struct Stage1ZirInstRef {
3450     Stage1ZirInst base;
3451 
3452     Stage1ZirInst *value;
3453 };
3454 
3455 struct Stage1AirInstRef {
3456     Stage1AirInst base;
3457 
3458     Stage1AirInst *operand;
3459     Stage1AirInst *result_loc;
3460 };
3461 
3462 struct Stage1ZirInstCompileErr {
3463     Stage1ZirInst base;
3464 
3465     Stage1ZirInst *msg;
3466 };
3467 
3468 struct Stage1ZirInstCompileLog {
3469     Stage1ZirInst base;
3470 
3471     size_t msg_count;
3472     Stage1ZirInst **msg_list;
3473 };
3474 
3475 struct Stage1ZirInstErrName {
3476     Stage1ZirInst base;
3477 
3478     Stage1ZirInst *value;
3479 };
3480 
3481 struct Stage1AirInstErrName {
3482     Stage1AirInst base;
3483 
3484     Stage1AirInst *value;
3485 };
3486 
3487 struct Stage1ZirInstCImport {
3488     Stage1ZirInst base;
3489 };
3490 
3491 struct Stage1ZirInstCInclude {
3492     Stage1ZirInst base;
3493 
3494     Stage1ZirInst *name;
3495 };
3496 
3497 struct Stage1ZirInstCDefine {
3498     Stage1ZirInst base;
3499 
3500     Stage1ZirInst *name;
3501     Stage1ZirInst *value;
3502 };
3503 
3504 struct Stage1ZirInstCUndef {
3505     Stage1ZirInst base;
3506 
3507     Stage1ZirInst *name;
3508 };
3509 
3510 struct Stage1ZirInstEmbedFile {
3511     Stage1ZirInst base;
3512 
3513     Stage1ZirInst *name;
3514 };
3515 
3516 struct Stage1ZirInstCmpxchg {
3517     Stage1ZirInst base;
3518 
3519     bool is_weak;
3520     Stage1ZirInst *type_value;
3521     Stage1ZirInst *ptr;
3522     Stage1ZirInst *cmp_value;
3523     Stage1ZirInst *new_value;
3524     Stage1ZirInst *success_order_value;
3525     Stage1ZirInst *failure_order_value;
3526     ResultLoc *result_loc;
3527 };
3528 
3529 struct Stage1AirInstCmpxchg {
3530     Stage1AirInst base;
3531 
3532     AtomicOrder success_order;
3533     AtomicOrder failure_order;
3534     Stage1AirInst *ptr;
3535     Stage1AirInst *cmp_value;
3536     Stage1AirInst *new_value;
3537     Stage1AirInst *result_loc;
3538     bool is_weak;
3539 };
3540 
3541 struct Stage1ZirInstFence {
3542     Stage1ZirInst base;
3543 
3544     Stage1ZirInst *order;
3545 };
3546 
3547 struct Stage1AirInstFence {
3548     Stage1AirInst base;
3549 
3550     AtomicOrder order;
3551 };
3552 
3553 struct Stage1ZirInstReduce {
3554     Stage1ZirInst base;
3555 
3556     Stage1ZirInst *op;
3557     Stage1ZirInst *value;
3558 };
3559 
3560 struct Stage1AirInstReduce {
3561     Stage1AirInst base;
3562 
3563     ReduceOp op;
3564     Stage1AirInst *value;
3565 };
3566 
3567 struct Stage1ZirInstTruncate {
3568     Stage1ZirInst base;
3569 
3570     Stage1ZirInst *dest_type;
3571     Stage1ZirInst *target;
3572 };
3573 
3574 struct Stage1AirInstTruncate {
3575     Stage1AirInst base;
3576 
3577     Stage1AirInst *target;
3578 };
3579 
3580 struct Stage1ZirInstIntCast {
3581     Stage1ZirInst base;
3582 
3583     Stage1ZirInst *dest_type;
3584     Stage1ZirInst *target;
3585 };
3586 
3587 struct Stage1ZirInstFloatCast {
3588     Stage1ZirInst base;
3589 
3590     Stage1ZirInst *dest_type;
3591     Stage1ZirInst *target;
3592 };
3593 
3594 struct Stage1ZirInstErrSetCast {
3595     Stage1ZirInst base;
3596 
3597     Stage1ZirInst *dest_type;
3598     Stage1ZirInst *target;
3599 };
3600 
3601 struct Stage1ZirInstIntToFloat {
3602     Stage1ZirInst base;
3603 
3604     Stage1ZirInst *dest_type;
3605     Stage1ZirInst *target;
3606 };
3607 
3608 struct Stage1ZirInstFloatToInt {
3609     Stage1ZirInst base;
3610 
3611     Stage1ZirInst *dest_type;
3612     Stage1ZirInst *target;
3613 };
3614 
3615 struct Stage1ZirInstBoolToInt {
3616     Stage1ZirInst base;
3617 
3618     Stage1ZirInst *target;
3619 };
3620 
3621 struct Stage1ZirInstVectorType {
3622     Stage1ZirInst base;
3623 
3624     Stage1ZirInst *len;
3625     Stage1ZirInst *elem_type;
3626 };
3627 
3628 struct Stage1ZirInstBoolNot {
3629     Stage1ZirInst base;
3630 
3631     Stage1ZirInst *value;
3632 };
3633 
3634 struct Stage1AirInstBoolNot {
3635     Stage1AirInst base;
3636 
3637     Stage1AirInst *value;
3638 };
3639 
3640 struct Stage1ZirInstMemset {
3641     Stage1ZirInst base;
3642 
3643     Stage1ZirInst *dest_ptr;
3644     Stage1ZirInst *byte;
3645     Stage1ZirInst *count;
3646 };
3647 
3648 struct Stage1AirInstMemset {
3649     Stage1AirInst base;
3650 
3651     Stage1AirInst *dest_ptr;
3652     Stage1AirInst *byte;
3653     Stage1AirInst *count;
3654 };
3655 
3656 struct Stage1ZirInstMemcpy {
3657     Stage1ZirInst base;
3658 
3659     Stage1ZirInst *dest_ptr;
3660     Stage1ZirInst *src_ptr;
3661     Stage1ZirInst *count;
3662 };
3663 
3664 struct Stage1AirInstMemcpy {
3665     Stage1AirInst base;
3666 
3667     Stage1AirInst *dest_ptr;
3668     Stage1AirInst *src_ptr;
3669     Stage1AirInst *count;
3670 };
3671 
3672 struct Stage1ZirInstWasmMemorySize {
3673     Stage1ZirInst base;
3674 
3675     Stage1ZirInst *index;
3676 };
3677 
3678 struct Stage1AirInstWasmMemorySize {
3679     Stage1AirInst base;
3680 
3681     Stage1AirInst *index;
3682 };
3683 
3684 struct Stage1ZirInstWasmMemoryGrow {
3685     Stage1ZirInst base;
3686 
3687     Stage1ZirInst *index;
3688     Stage1ZirInst *delta;
3689 };
3690 
3691 struct Stage1AirInstWasmMemoryGrow {
3692     Stage1AirInst base;
3693 
3694     Stage1AirInst *index;
3695     Stage1AirInst *delta;
3696 };
3697 
3698 struct Stage1ZirInstSrc {
3699     Stage1ZirInst base;
3700 };
3701 
3702 struct Stage1ZirInstPrefetch {
3703     Stage1ZirInst base;
3704 
3705     Stage1ZirInst *ptr;
3706     Stage1ZirInst *options;
3707 };
3708 
3709 struct Stage1AirInstPrefetch {
3710     Stage1AirInst base;
3711 
3712     Stage1AirInst *ptr;
3713     PrefetchRw rw;
3714     // Must be in the range 0-3 inclusive
3715     uint8_t locality;
3716     PrefetchCache cache;
3717 };
3718 
3719 
3720 struct Stage1ZirInstSlice {
3721     Stage1ZirInst base;
3722 
3723     Stage1ZirInst *ptr;
3724     Stage1ZirInst *start;
3725     Stage1ZirInst *end;
3726     Stage1ZirInst *sentinel;
3727     ResultLoc *result_loc;
3728     bool safety_check_on;
3729 };
3730 
3731 struct Stage1AirInstSlice {
3732     Stage1AirInst base;
3733 
3734     Stage1AirInst *ptr;
3735     Stage1AirInst *start;
3736     Stage1AirInst *end;
3737     Stage1AirInst *result_loc;
3738     ZigValue *sentinel;
3739     bool safety_check_on;
3740 };
3741 
3742 struct Stage1ZirInstBreakpoint {
3743     Stage1ZirInst base;
3744 };
3745 
3746 struct Stage1AirInstBreakpoint {
3747     Stage1AirInst base;
3748 };
3749 
3750 struct Stage1ZirInstReturnAddress {
3751     Stage1ZirInst base;
3752 };
3753 
3754 struct Stage1AirInstReturnAddress {
3755     Stage1AirInst base;
3756 };
3757 
3758 struct Stage1ZirInstFrameAddress {
3759     Stage1ZirInst base;
3760 };
3761 
3762 struct Stage1AirInstFrameAddress {
3763     Stage1AirInst base;
3764 };
3765 
3766 struct Stage1ZirInstFrameHandle {
3767     Stage1ZirInst base;
3768 };
3769 
3770 struct Stage1AirInstFrameHandle {
3771     Stage1AirInst base;
3772 };
3773 
3774 struct Stage1ZirInstFrameType {
3775     Stage1ZirInst base;
3776 
3777     Stage1ZirInst *fn;
3778 };
3779 
3780 struct Stage1ZirInstFrameSize {
3781     Stage1ZirInst base;
3782 
3783     Stage1ZirInst *fn;
3784 };
3785 
3786 struct Stage1AirInstFrameSize {
3787     Stage1AirInst base;
3788 
3789     Stage1AirInst *fn;
3790 };
3791 
3792 enum IrOverflowOp {
3793     IrOverflowOpAdd,
3794     IrOverflowOpSub,
3795     IrOverflowOpMul,
3796     IrOverflowOpShl,
3797 };
3798 
3799 struct Stage1ZirInstOverflowOp {
3800     Stage1ZirInst base;
3801 
3802     IrOverflowOp op;
3803     Stage1ZirInst *type_value;
3804     Stage1ZirInst *op1;
3805     Stage1ZirInst *op2;
3806     Stage1ZirInst *result_ptr;
3807 };
3808 
3809 struct Stage1AirInstOverflowOp {
3810     Stage1AirInst base;
3811 
3812     IrOverflowOp op;
3813     Stage1AirInst *op1;
3814     Stage1AirInst *op2;
3815     Stage1AirInst *result_ptr;
3816 
3817     // TODO can this field be removed?
3818     ZigType *result_ptr_type;
3819 };
3820 
3821 struct Stage1ZirInstMulAdd {
3822     Stage1ZirInst base;
3823 
3824     Stage1ZirInst *type_value;
3825     Stage1ZirInst *op1;
3826     Stage1ZirInst *op2;
3827     Stage1ZirInst *op3;
3828 };
3829 
3830 struct Stage1AirInstMulAdd {
3831     Stage1AirInst base;
3832 
3833     Stage1AirInst *op1;
3834     Stage1AirInst *op2;
3835     Stage1AirInst *op3;
3836 };
3837 
3838 struct Stage1ZirInstAlignOf {
3839     Stage1ZirInst base;
3840 
3841     Stage1ZirInst *type_value;
3842 };
3843 
3844 // returns true if error, returns false if not error
3845 struct Stage1ZirInstTestErr {
3846     Stage1ZirInst base;
3847 
3848     Stage1ZirInst *base_ptr;
3849     bool resolve_err_set;
3850     bool base_ptr_is_payload;
3851 };
3852 
3853 struct Stage1AirInstTestErr {
3854     Stage1AirInst base;
3855 
3856     Stage1AirInst *err_union;
3857 };
3858 
3859 // Takes an error union pointer, returns a pointer to the error code.
3860 struct Stage1ZirInstUnwrapErrCode {
3861     Stage1ZirInst base;
3862 
3863     Stage1ZirInst *err_union_ptr;
3864     bool initializing;
3865 };
3866 
3867 struct Stage1AirInstUnwrapErrCode {
3868     Stage1AirInst base;
3869 
3870     Stage1AirInst *err_union_ptr;
3871     bool initializing;
3872 };
3873 
3874 struct Stage1ZirInstUnwrapErrPayload {
3875     Stage1ZirInst base;
3876 
3877     Stage1ZirInst *value;
3878     bool safety_check_on;
3879     bool initializing;
3880 };
3881 
3882 struct Stage1AirInstUnwrapErrPayload {
3883     Stage1AirInst base;
3884 
3885     Stage1AirInst *value;
3886     bool safety_check_on;
3887     bool initializing;
3888 };
3889 
3890 struct Stage1AirInstOptionalWrap {
3891     Stage1AirInst base;
3892 
3893     Stage1AirInst *operand;
3894     Stage1AirInst *result_loc;
3895 };
3896 
3897 struct Stage1AirInstErrWrapPayload {
3898     Stage1AirInst base;
3899 
3900     Stage1AirInst *operand;
3901     Stage1AirInst *result_loc;
3902 };
3903 
3904 struct Stage1AirInstErrWrapCode {
3905     Stage1AirInst base;
3906 
3907     Stage1AirInst *operand;
3908     Stage1AirInst *result_loc;
3909 };
3910 
3911 struct Stage1ZirInstFnProto {
3912     Stage1ZirInst base;
3913 
3914     Stage1ZirInst **param_types;
3915     Stage1ZirInst *align_value;
3916     Stage1ZirInst *callconv_value;
3917     Stage1ZirInst *return_type;
3918     bool is_var_args;
3919 };
3920 
3921 // true if the target value is compile time known, false otherwise
3922 struct Stage1ZirInstTestComptime {
3923     Stage1ZirInst base;
3924 
3925     Stage1ZirInst *value;
3926 };
3927 
3928 struct Stage1ZirInstPtrCast {
3929     Stage1ZirInst base;
3930 
3931     Stage1ZirInst *dest_type;
3932     Stage1ZirInst *ptr;
3933     bool safety_check_on;
3934 };
3935 
3936 struct Stage1AirInstPtrCast {
3937     Stage1AirInst base;
3938 
3939     Stage1AirInst *ptr;
3940     bool safety_check_on;
3941 };
3942 
3943 struct Stage1ZirInstImplicitCast {
3944     Stage1ZirInst base;
3945 
3946     Stage1ZirInst *operand;
3947     ResultLocCast *result_loc_cast;
3948 };
3949 
3950 struct Stage1ZirInstBitCast {
3951     Stage1ZirInst base;
3952 
3953     Stage1ZirInst *operand;
3954     ResultLocBitCast *result_loc_bit_cast;
3955 };
3956 
3957 struct Stage1AirInstBitCast {
3958     Stage1AirInst base;
3959 
3960     Stage1AirInst *operand;
3961 };
3962 
3963 struct Stage1AirInstWidenOrShorten {
3964     Stage1AirInst base;
3965 
3966     Stage1AirInst *target;
3967 };
3968 
3969 struct Stage1ZirInstPtrToInt {
3970     Stage1ZirInst base;
3971 
3972     Stage1ZirInst *target;
3973 };
3974 
3975 struct Stage1AirInstPtrToInt {
3976     Stage1AirInst base;
3977 
3978     Stage1AirInst *target;
3979 };
3980 
3981 struct Stage1ZirInstIntToPtr {
3982     Stage1ZirInst base;
3983 
3984     Stage1ZirInst *dest_type;
3985     Stage1ZirInst *target;
3986 };
3987 
3988 struct Stage1AirInstIntToPtr {
3989     Stage1AirInst base;
3990 
3991     Stage1AirInst *target;
3992 };
3993 
3994 struct Stage1ZirInstIntToEnum {
3995     Stage1ZirInst base;
3996 
3997     Stage1ZirInst *dest_type;
3998     Stage1ZirInst *target;
3999 };
4000 
4001 struct Stage1AirInstIntToEnum {
4002     Stage1AirInst base;
4003 
4004     Stage1AirInst *target;
4005 };
4006 
4007 struct Stage1ZirInstEnumToInt {
4008     Stage1ZirInst base;
4009 
4010     Stage1ZirInst *target;
4011 };
4012 
4013 struct Stage1ZirInstIntToErr {
4014     Stage1ZirInst base;
4015 
4016     Stage1ZirInst *target;
4017 };
4018 
4019 struct Stage1AirInstIntToErr {
4020     Stage1AirInst base;
4021 
4022     Stage1AirInst *target;
4023 };
4024 
4025 struct Stage1ZirInstErrToInt {
4026     Stage1ZirInst base;
4027 
4028     Stage1ZirInst *target;
4029 };
4030 
4031 struct Stage1AirInstErrToInt {
4032     Stage1AirInst base;
4033 
4034     Stage1AirInst *target;
4035 };
4036 
4037 struct Stage1ZirInstCheckSwitchProngsRange {
4038     Stage1ZirInst *start;
4039     Stage1ZirInst *end;
4040 };
4041 
4042 struct Stage1ZirInstCheckSwitchProngs {
4043     Stage1ZirInst base;
4044 
4045     Stage1ZirInst *target_value;
4046     Stage1ZirInstCheckSwitchProngsRange *ranges;
4047     size_t range_count;
4048     AstNode* else_prong;
4049 };
4050 
4051 struct Stage1ZirInstCheckStatementIsVoid {
4052     Stage1ZirInst base;
4053 
4054     Stage1ZirInst *statement_value;
4055 };
4056 
4057 struct Stage1ZirInstTypeName {
4058     Stage1ZirInst base;
4059 
4060     Stage1ZirInst *type_value;
4061 };
4062 
4063 struct Stage1ZirInstDeclRef {
4064     Stage1ZirInst base;
4065 
4066     LVal lval;
4067     Tld *tld;
4068 };
4069 
4070 struct Stage1ZirInstPanic {
4071     Stage1ZirInst base;
4072 
4073     Stage1ZirInst *msg;
4074 };
4075 
4076 struct Stage1AirInstPanic {
4077     Stage1AirInst base;
4078 
4079     Stage1AirInst *msg;
4080 };
4081 
4082 struct Stage1ZirInstTagName {
4083     Stage1ZirInst base;
4084 
4085     Stage1ZirInst *target;
4086 };
4087 
4088 struct Stage1AirInstTagName {
4089     Stage1AirInst base;
4090 
4091     Stage1AirInst *target;
4092 };
4093 
4094 struct Stage1ZirInstFieldParentPtr {
4095     Stage1ZirInst base;
4096 
4097     Stage1ZirInst *type_value;
4098     Stage1ZirInst *field_name;
4099     Stage1ZirInst *field_ptr;
4100 };
4101 
4102 struct Stage1AirInstFieldParentPtr {
4103     Stage1AirInst base;
4104 
4105     Stage1AirInst *field_ptr;
4106     TypeStructField *field;
4107 };
4108 
4109 struct Stage1ZirInstOffsetOf {
4110     Stage1ZirInst base;
4111 
4112     Stage1ZirInst *type_value;
4113     Stage1ZirInst *field_name;
4114 };
4115 
4116 struct Stage1ZirInstBitOffsetOf {
4117     Stage1ZirInst base;
4118 
4119     Stage1ZirInst *type_value;
4120     Stage1ZirInst *field_name;
4121 };
4122 
4123 struct Stage1ZirInstTypeInfo {
4124     Stage1ZirInst base;
4125 
4126     Stage1ZirInst *type_value;
4127 };
4128 
4129 struct Stage1ZirInstType {
4130     Stage1ZirInst base;
4131 
4132     Stage1ZirInst *type_info;
4133 };
4134 
4135 struct Stage1ZirInstHasField {
4136     Stage1ZirInst base;
4137 
4138     Stage1ZirInst *container_type;
4139     Stage1ZirInst *field_name;
4140 };
4141 
4142 struct Stage1ZirInstSetEvalBranchQuota {
4143     Stage1ZirInst base;
4144 
4145     Stage1ZirInst *new_quota;
4146 };
4147 
4148 struct Stage1ZirInstAlignCast {
4149     Stage1ZirInst base;
4150 
4151     Stage1ZirInst *align_bytes;
4152     Stage1ZirInst *target;
4153 };
4154 
4155 struct Stage1AirInstAlignCast {
4156     Stage1AirInst base;
4157 
4158     Stage1AirInst *target;
4159 };
4160 
4161 struct Stage1ZirInstSetAlignStack {
4162     Stage1ZirInst base;
4163 
4164     Stage1ZirInst *align_bytes;
4165 };
4166 
4167 struct Stage1ZirInstArgType {
4168     Stage1ZirInst base;
4169 
4170     Stage1ZirInst *fn_type;
4171     Stage1ZirInst *arg_index;
4172 };
4173 
4174 struct Stage1ZirInstExport {
4175     Stage1ZirInst base;
4176 
4177     Stage1ZirInst *target;
4178     Stage1ZirInst *options;
4179 };
4180 
4181 struct Stage1ZirInstExtern {
4182     Stage1ZirInst base;
4183 
4184     Stage1ZirInst *type;
4185     Stage1ZirInst *options;
4186 };
4187 
4188 struct Stage1AirInstExtern {
4189     Stage1AirInst base;
4190 
4191     Buf *name;
4192     GlobalLinkageId linkage;
4193     bool is_thread_local;
4194 };
4195 
4196 enum IrInstErrorReturnTraceOptional {
4197     IrInstErrorReturnTraceNull,
4198     IrInstErrorReturnTraceNonNull,
4199 };
4200 
4201 struct Stage1ZirInstErrorReturnTrace {
4202     Stage1ZirInst base;
4203 
4204     IrInstErrorReturnTraceOptional optional;
4205 };
4206 
4207 struct Stage1AirInstErrorReturnTrace {
4208     Stage1AirInst base;
4209 
4210     IrInstErrorReturnTraceOptional optional;
4211 };
4212 
4213 struct Stage1ZirInstErrorUnion {
4214     Stage1ZirInst base;
4215 
4216     Stage1ZirInst *err_set;
4217     Stage1ZirInst *payload;
4218     Buf *type_name;
4219 };
4220 
4221 struct Stage1ZirInstAtomicRmw {
4222     Stage1ZirInst base;
4223 
4224     Stage1ZirInst *operand_type;
4225     Stage1ZirInst *ptr;
4226     Stage1ZirInst *op;
4227     Stage1ZirInst *operand;
4228     Stage1ZirInst *ordering;
4229 };
4230 
4231 struct Stage1AirInstAtomicRmw {
4232     Stage1AirInst base;
4233 
4234     Stage1AirInst *ptr;
4235     Stage1AirInst *operand;
4236     AtomicRmwOp op;
4237     AtomicOrder ordering;
4238 };
4239 
4240 struct Stage1ZirInstAtomicLoad {
4241     Stage1ZirInst base;
4242 
4243     Stage1ZirInst *operand_type;
4244     Stage1ZirInst *ptr;
4245     Stage1ZirInst *ordering;
4246 };
4247 
4248 struct Stage1AirInstAtomicLoad {
4249     Stage1AirInst base;
4250 
4251     Stage1AirInst *ptr;
4252     AtomicOrder ordering;
4253 };
4254 
4255 struct Stage1ZirInstAtomicStore {
4256     Stage1ZirInst base;
4257 
4258     Stage1ZirInst *operand_type;
4259     Stage1ZirInst *ptr;
4260     Stage1ZirInst *value;
4261     Stage1ZirInst *ordering;
4262 };
4263 
4264 struct Stage1AirInstAtomicStore {
4265     Stage1AirInst base;
4266 
4267     Stage1AirInst *ptr;
4268     Stage1AirInst *value;
4269     AtomicOrder ordering;
4270 };
4271 
4272 struct Stage1ZirInstSaveErrRetAddr {
4273     Stage1ZirInst base;
4274 };
4275 
4276 struct Stage1AirInstSaveErrRetAddr {
4277     Stage1AirInst base;
4278 };
4279 
4280 struct Stage1ZirInstAddImplicitReturnType {
4281     Stage1ZirInst base;
4282 
4283     Stage1ZirInst *value;
4284     ResultLocReturn *result_loc_ret;
4285 };
4286 
4287 // For float ops that take a single argument
4288 struct Stage1ZirInstFloatOp {
4289     Stage1ZirInst base;
4290 
4291     Stage1ZirInst *operand;
4292     BuiltinFnId fn_id;
4293 };
4294 
4295 struct Stage1AirInstFloatOp {
4296     Stage1AirInst base;
4297 
4298     Stage1AirInst *operand;
4299     BuiltinFnId fn_id;
4300 };
4301 
4302 struct Stage1ZirInstCheckRuntimeScope {
4303     Stage1ZirInst base;
4304 
4305     Stage1ZirInst *scope_is_comptime;
4306     Stage1ZirInst *is_comptime;
4307 };
4308 
4309 struct Stage1ZirInstBswap {
4310     Stage1ZirInst base;
4311 
4312     Stage1ZirInst *type;
4313     Stage1ZirInst *op;
4314 };
4315 
4316 struct Stage1AirInstBswap {
4317     Stage1AirInst base;
4318 
4319     Stage1AirInst *op;
4320 };
4321 
4322 struct Stage1ZirInstBitReverse {
4323     Stage1ZirInst base;
4324 
4325     Stage1ZirInst *type;
4326     Stage1ZirInst *op;
4327 };
4328 
4329 struct Stage1AirInstBitReverse {
4330     Stage1AirInst base;
4331 
4332     Stage1AirInst *op;
4333 };
4334 
4335 struct Stage1AirInstArrayToVector {
4336     Stage1AirInst base;
4337 
4338     Stage1AirInst *array;
4339 };
4340 
4341 struct Stage1AirInstVectorToArray {
4342     Stage1AirInst base;
4343 
4344     Stage1AirInst *vector;
4345     Stage1AirInst *result_loc;
4346 };
4347 
4348 struct Stage1ZirInstShuffleVector {
4349     Stage1ZirInst base;
4350 
4351     Stage1ZirInst *scalar_type;
4352     Stage1ZirInst *a;
4353     Stage1ZirInst *b;
4354     Stage1ZirInst *mask; // This is in zig-format, not llvm format
4355 };
4356 
4357 struct Stage1AirInstShuffleVector {
4358     Stage1AirInst base;
4359 
4360     Stage1AirInst *a;
4361     Stage1AirInst *b;
4362     Stage1AirInst *mask; // This is in zig-format, not llvm format
4363 };
4364 
4365 struct Stage1ZirInstSelect {
4366     Stage1ZirInst base;
4367 
4368     Stage1ZirInst *scalar_type;
4369     Stage1ZirInst *pred; // This is in zig-format, not llvm format
4370     Stage1ZirInst *a;
4371     Stage1ZirInst *b;
4372 };
4373 
4374 struct Stage1AirInstSelect {
4375     Stage1AirInst base;
4376 
4377     Stage1AirInst *pred;  // This is in zig-format, not llvm format
4378     Stage1AirInst *a;
4379     Stage1AirInst *b;
4380 };
4381 
4382 struct Stage1ZirInstSplat {
4383     Stage1ZirInst base;
4384 
4385     Stage1ZirInst *len;
4386     Stage1ZirInst *scalar;
4387 };
4388 
4389 struct Stage1AirInstSplat {
4390     Stage1AirInst base;
4391 
4392     Stage1AirInst *scalar;
4393 };
4394 
4395 struct Stage1AirInstAssertZero {
4396     Stage1AirInst base;
4397 
4398     Stage1AirInst *target;
4399 };
4400 
4401 struct Stage1AirInstAssertNonNull {
4402     Stage1AirInst base;
4403 
4404     Stage1AirInst *target;
4405 };
4406 
4407 struct Stage1ZirInstUnionInitNamedField {
4408     Stage1ZirInst base;
4409 
4410     Stage1ZirInst *union_type;
4411     Stage1ZirInst *field_name;
4412     Stage1ZirInst *field_result_loc;
4413     Stage1ZirInst *result_loc;
4414 };
4415 
4416 struct Stage1ZirInstHasDecl {
4417     Stage1ZirInst base;
4418 
4419     Stage1ZirInst *container;
4420     Stage1ZirInst *name;
4421 };
4422 
4423 struct Stage1ZirInstUndeclaredIdent {
4424     Stage1ZirInst base;
4425 
4426     Buf *name;
4427 };
4428 
4429 struct Stage1ZirInstAlloca {
4430     Stage1ZirInst base;
4431 
4432     Stage1ZirInst *align;
4433     Stage1ZirInst *is_comptime;
4434     const char *name_hint;
4435 };
4436 
4437 struct Stage1AirInstAlloca {
4438     Stage1AirInst base;
4439 
4440     uint32_t align;
4441     const char *name_hint;
4442     size_t field_index;
4443 };
4444 
4445 struct Stage1ZirInstEndExpr {
4446     Stage1ZirInst base;
4447 
4448     Stage1ZirInst *value;
4449     ResultLoc *result_loc;
4450 };
4451 
4452 // This one is for writing through the result pointer.
4453 struct Stage1ZirInstResolveResult {
4454     Stage1ZirInst base;
4455 
4456     ResultLoc *result_loc;
4457     Stage1ZirInst *ty;
4458 };
4459 
4460 struct Stage1ZirInstResetResult {
4461     Stage1ZirInst base;
4462 
4463     ResultLoc *result_loc;
4464 };
4465 
4466 struct Stage1AirInstPtrOfArrayToSlice {
4467     Stage1AirInst base;
4468 
4469     Stage1AirInst *operand;
4470     Stage1AirInst *result_loc;
4471 };
4472 
4473 struct Stage1ZirInstSuspendBegin {
4474     Stage1ZirInst base;
4475 };
4476 
4477 struct Stage1AirInstSuspendBegin {
4478     Stage1AirInst base;
4479 
4480     LLVMBasicBlockRef resume_bb;
4481 };
4482 
4483 struct Stage1ZirInstSuspendFinish {
4484     Stage1ZirInst base;
4485 
4486     Stage1ZirInstSuspendBegin *begin;
4487 };
4488 
4489 struct Stage1AirInstSuspendFinish {
4490     Stage1AirInst base;
4491 
4492     Stage1AirInstSuspendBegin *begin;
4493 };
4494 
4495 struct Stage1ZirInstAwait {
4496     Stage1ZirInst base;
4497 
4498     Stage1ZirInst *frame;
4499     ResultLoc *result_loc;
4500     bool is_nosuspend;
4501 };
4502 
4503 struct Stage1AirInstAwait {
4504     Stage1AirInst base;
4505 
4506     Stage1AirInst *frame;
4507     Stage1AirInst *result_loc;
4508     ZigFn *target_fn;
4509     bool is_nosuspend;
4510 };
4511 
4512 struct Stage1ZirInstResume {
4513     Stage1ZirInst base;
4514 
4515     Stage1ZirInst *frame;
4516 };
4517 
4518 struct Stage1AirInstResume {
4519     Stage1AirInst base;
4520 
4521     Stage1AirInst *frame;
4522 };
4523 
4524 enum SpillId {
4525     SpillIdInvalid,
4526     SpillIdRetErrCode,
4527 };
4528 
4529 struct Stage1ZirInstSpillBegin {
4530     Stage1ZirInst base;
4531 
4532     Stage1ZirInst *operand;
4533     SpillId spill_id;
4534 };
4535 
4536 struct Stage1AirInstSpillBegin {
4537     Stage1AirInst base;
4538 
4539     SpillId spill_id;
4540     Stage1AirInst *operand;
4541 };
4542 
4543 struct Stage1ZirInstSpillEnd {
4544     Stage1ZirInst base;
4545 
4546     Stage1ZirInstSpillBegin *begin;
4547 };
4548 
4549 struct Stage1AirInstSpillEnd {
4550     Stage1AirInst base;
4551 
4552     Stage1AirInstSpillBegin *begin;
4553 };
4554 
4555 struct Stage1AirInstVectorExtractElem {
4556     Stage1AirInst base;
4557 
4558     Stage1AirInst *vector;
4559     Stage1AirInst *index;
4560 };
4561 
4562 enum ResultLocId {
4563     ResultLocIdInvalid,
4564     ResultLocIdNone,
4565     ResultLocIdVar,
4566     ResultLocIdReturn,
4567     ResultLocIdPeer,
4568     ResultLocIdPeerParent,
4569     ResultLocIdInstruction,
4570     ResultLocIdBitCast,
4571     ResultLocIdCast,
4572 };
4573 
4574 // Additions to this struct may need to be handled in
4575 // ir_reset_result
4576 struct ResultLoc {
4577     ResultLocId id;
4578     bool written;
4579     bool allow_write_through_const;
4580     Stage1AirInst *resolved_loc; // result ptr
4581     Stage1ZirInst *source_instruction;
4582     Stage1AirInst *gen_instruction; // value to store to the result loc
4583     ZigType *implicit_elem_type;
4584 };
4585 
4586 struct ResultLocNone {
4587     ResultLoc base;
4588 };
4589 
4590 struct ResultLocVar {
4591     ResultLoc base;
4592 
4593     ZigVar *var;
4594 };
4595 
4596 struct ResultLocReturn {
4597     ResultLoc base;
4598 
4599     bool implicit_return_type_done;
4600 };
4601 
4602 struct IrSuspendPosition {
4603     size_t basic_block_index;
4604     size_t instruction_index;
4605 };
4606 
4607 struct ResultLocPeerParent {
4608     ResultLoc base;
4609 
4610     bool skipped;
4611     bool done_resuming;
4612     Stage1ZirBasicBlock *end_bb;
4613     ResultLoc *parent;
4614     ZigList<ResultLocPeer *> peers;
4615     ZigType *resolved_type;
4616     Stage1ZirInst *is_comptime;
4617 };
4618 
4619 struct ResultLocPeer {
4620     ResultLoc base;
4621 
4622     ResultLocPeerParent *parent;
4623     Stage1ZirBasicBlock *next_bb;
4624     IrSuspendPosition suspend_pos;
4625 };
4626 
4627 // The result location is the source instruction
4628 struct ResultLocInstruction {
4629     ResultLoc base;
4630 };
4631 
4632 // The source_instruction is the destination type
4633 struct ResultLocBitCast {
4634     ResultLoc base;
4635 
4636     ResultLoc *parent;
4637 };
4638 
4639 // The source_instruction is the destination type
4640 struct ResultLocCast {
4641     ResultLoc base;
4642 
4643     ResultLoc *parent;
4644 };
4645 
4646 static const size_t slice_ptr_index = 0;
4647 static const size_t slice_len_index = 1;
4648 
4649 static const size_t maybe_child_index = 0;
4650 static const size_t maybe_null_index = 1;
4651 
4652 static const size_t err_union_payload_index = 0;
4653 static const size_t err_union_err_index = 1;
4654 
4655 // label (grep this): [fn_frame_struct_layout]
4656 static const size_t frame_fn_ptr_index = 0;
4657 static const size_t frame_resume_index = 1;
4658 static const size_t frame_awaiter_index = 2;
4659 static const size_t frame_ret_start = 3;
4660 
4661 // TODO https://github.com/ziglang/zig/issues/3056
4662 // We require this to be a power of 2 so that we can use shifting rather than
4663 // remainder division.
4664 static const size_t stack_trace_ptr_count = 32; // Must be a power of 2.
4665 
4666 #define NAMESPACE_SEP_CHAR '.'
4667 #define NAMESPACE_SEP_STR "."
4668 
4669 #define CACHE_OUT_SUBDIR "o"
4670 #define CACHE_HASH_SUBDIR "h"
4671 
4672 enum FloatMode {
4673     FloatModeStrict,
4674     FloatModeOptimized,
4675 };
4676 
4677 enum FnWalkId {
4678     FnWalkIdAttrs,
4679     FnWalkIdCall,
4680     FnWalkIdTypes,
4681     FnWalkIdVars,
4682     FnWalkIdInits,
4683 };
4684 
4685 struct FnWalkAttrs {
4686     ZigFn *fn;
4687     LLVMValueRef llvm_fn;
4688     unsigned gen_i;
4689 };
4690 
4691 struct FnWalkCall {
4692     ZigList<LLVMValueRef> *gen_param_values;
4693     ZigList<ZigType *> *gen_param_types;
4694     Stage1AirInstCall *inst;
4695     bool is_var_args;
4696 };
4697 
4698 struct FnWalkTypes {
4699     ZigList<ZigLLVMDIType *> *param_di_types;
4700     ZigList<LLVMTypeRef> *gen_param_types;
4701 };
4702 
4703 struct FnWalkVars {
4704     ZigType *import;
4705     LLVMValueRef llvm_fn;
4706     ZigFn *fn;
4707     ZigVar *var;
4708     unsigned gen_i;
4709 };
4710 
4711 struct FnWalkInits {
4712     LLVMValueRef llvm_fn;
4713     ZigFn *fn;
4714     unsigned gen_i;
4715 };
4716 
4717 struct FnWalk {
4718     FnWalkId id;
4719     union {
4720         FnWalkAttrs attrs;
4721         FnWalkCall call;
4722         FnWalkTypes types;
4723         FnWalkVars vars;
4724         FnWalkInits inits;
4725     } data;
4726 };
4727 
4728 #endif
4729