authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2019-06-26 18:29:19-04:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2019-06-26 18:29:19-04:00
log01ff0d4d629c3800ebac68edfd91edb570aeaa59
treefc1bdbf059e9bf9e99b212aae3056ed0e87e7e77
parent07c0d484eec327ca3bd1e3ce081c1ced230536a9
parent517bdea7549453d958c31cf2af5dc298ea5508a9
signaturebadge-question-mark Signed by PGP key 4AEE18F83AFDEB23

Merge pull request #2602 from ziglang/copy-elision-3

result location mechanism (part of no-copy semantics)

42 files changed, 4319 insertions(+), 2112 deletions(-)

CMakeLists.txt+1
......@@ -6734,6 +6734,7 @@ add_custom_command(
67346734 "-Doutput-dir=${CMAKE_BINARY_DIR}"
67356735 WORKING_DIRECTORY "${CMAKE_SOURCE_DIR}"
67366736 DEPENDS
6737 zig0
67376738 "${CMAKE_SOURCE_DIR}/src-self-hosted/dep_tokenizer.zig"
67386739 "${CMAKE_SOURCE_DIR}/src-self-hosted/stage1.zig"
67396740 "${CMAKE_SOURCE_DIR}/src-self-hosted/translate_c.zig"
build.zig+2-1
......@@ -74,7 +74,8 @@ pub fn build(b: *Builder) !void {
7474 const skip_non_native = b.option(bool, "skip-non-native", "Main test suite skips non-native builds") orelse false;
7575 const skip_self_hosted = b.option(bool, "skip-self-hosted", "Main test suite skips building self hosted compiler") orelse false;
7676 if (!skip_self_hosted) {
77 test_step.dependOn(&exe.step);
77 // TODO re-enable this after https://github.com/ziglang/zig/issues/2377
78 //test_step.dependOn(&exe.step);
7879 }
7980 const verbose_link_exe = b.option(bool, "verbose-link", "Print link command for self hosted compiler") orelse false;
8081 exe.setVerboseLink(verbose_link_exe);
doc/langref.html.in+1-1
......@@ -5096,7 +5096,7 @@ fn gimmeTheBiggerInteger(a: u64, b: u64) u64 {
50965096 <p>
50975097 For example, if we were to introduce another function to the above snippet:
50985098 </p>
5099 {#code_begin|test_err|values of type 'type' must be comptime known#}
5099 {#code_begin|test_err|cannot store runtime value in type 'type'#}
51005100fn max(comptime T: type, a: T, b: T) T {
51015101 return if (a > b) a else b;
51025102}
src/all_types.hpp+276-92
......@@ -34,12 +34,17 @@ struct CodeGen;
3434struct ConstExprValue;
3535struct IrInstruction;
3636struct IrInstructionCast;
37struct IrInstructionAllocaGen;
3738struct IrBasicBlock;
3839struct ScopeDecls;
3940struct ZigWindowsSDK;
4041struct Tld;
4142struct TldExport;
4243struct IrAnalyze;
44struct ResultLoc;
45struct ResultLocPeer;
46struct ResultLocPeerParent;
47struct ResultLocBitCast;
4348
4449enum X64CABIClass {
4550 X64CABIClass_Unknown,
......@@ -198,6 +203,9 @@ enum ConstPtrMut {
198203 // The pointer points to memory that is known only at runtime.
199204 // For example it may point to the initializer value of a variable.
200205 ConstPtrMutRuntimeVar,
206 // The pointer points to memory for which it must be inferred whether the
207 // value is comptime known or not.
208 ConstPtrMutInfer,
201209};
202210
203211struct ConstPtrValue {
......@@ -289,6 +297,7 @@ struct RuntimeHintSlice {
289297struct ConstGlobalRefs {
290298 LLVMValueRef llvm_value;
291299 LLVMValueRef llvm_global;
300 uint32_t align;
292301};
293302
294303struct ConstExprValue {
......@@ -325,6 +334,10 @@ struct ConstExprValue {
325334 RuntimeHintPtr rh_ptr;
326335 RuntimeHintSlice rh_slice;
327336 } data;
337
338 // uncomment these to find bugs. can't leave them uncommented because of a gcc-9 warning
339 //ConstExprValue(const ConstExprValue &other) = delete; // plz zero initialize with {}
340 //ConstExprValue& operator= (const ConstExprValue &other) = delete; // use copy_const_val
328341};
329342
330343enum ReturnKnowledge {
......@@ -426,7 +439,7 @@ enum NodeType {
426439 NodeTypeVariableDeclaration,
427440 NodeTypeTestDecl,
428441 NodeTypeBinOpExpr,
429 NodeTypeUnwrapErrorExpr,
442 NodeTypeCatchExpr,
430443 NodeTypeFloatLiteral,
431444 NodeTypeIntLiteral,
432445 NodeTypeStringLiteral,
......@@ -1364,7 +1377,7 @@ struct ZigFn {
13641377 AstNode *fn_no_inline_set_node;
13651378 AstNode *fn_static_eval_set_node;
13661379
1367 ZigList<IrInstruction *> alloca_list;
1380 ZigList<IrInstructionAllocaGen *> alloca_gen_list;
13681381 ZigList<ZigVar *> variable_list;
13691382
13701383 Buf *section_name;
......@@ -1999,6 +2012,11 @@ struct ScopeDecls {
19992012 bool any_imports_failed;
20002013};
20012014
2015enum LVal {
2016 LValNone,
2017 LValPtr,
2018};
2019
20022020// This scope comes from a block expression in user code.
20032021// NodeTypeBlock
20042022struct ScopeBlock {
......@@ -2007,12 +2025,14 @@ struct ScopeBlock {
20072025 Buf *name;
20082026 IrBasicBlock *end_block;
20092027 IrInstruction *is_comptime;
2028 ResultLocPeerParent *peer_parent;
20102029 ZigList<IrInstruction *> *incoming_values;
20112030 ZigList<IrBasicBlock *> *incoming_blocks;
20122031
20132032 AstNode *safety_set_node;
20142033 AstNode *fast_math_set_node;
20152034
2035 LVal lval;
20162036 bool safety_off;
20172037 bool fast_math_on;
20182038};
......@@ -2056,12 +2076,14 @@ struct ScopeCImport {
20562076struct ScopeLoop {
20572077 Scope base;
20582078
2079 LVal lval;
20592080 Buf *name;
20602081 IrBasicBlock *break_block;
20612082 IrBasicBlock *continue_block;
20622083 IrInstruction *is_comptime;
20632084 ZigList<IrInstruction *> *incoming_values;
20642085 ZigList<IrBasicBlock *> *incoming_blocks;
2086 ResultLocPeerParent *peer_parent;
20652087};
20662088
20672089// This scope blocks certain things from working such as comptime continue
......@@ -2138,6 +2160,8 @@ struct IrBasicBlock {
21382160 const char *name_hint;
21392161 size_t debug_id;
21402162 size_t ref_count;
2163 // index into the basic block list
2164 size_t index;
21412165 LLVMBasicBlockRef llvm_block;
21422166 LLVMBasicBlockRef llvm_exit_block;
21432167 // The instruction that referenced this basic block and caused us to
......@@ -2148,11 +2172,10 @@ struct IrBasicBlock {
21482172 // if the branch is comptime. The instruction points to the reason
21492173 // the basic block must be comptime.
21502174 IrInstruction *must_be_comptime_source_instr;
2151};
2152
2153enum LVal {
2154 LValNone,
2155 LValPtr,
2175 IrInstruction *suspend_instruction_ref;
2176 bool already_appended;
2177 bool suspended;
2178 bool in_resume_stack;
21562179};
21572180
21582181// These instructions are in transition to having "pass 1" instructions
......@@ -2185,19 +2208,17 @@ enum IrInstructionId {
21852208 IrInstructionIdUnionFieldPtr,
21862209 IrInstructionIdElemPtr,
21872210 IrInstructionIdVarPtr,
2188 IrInstructionIdCall,
2211 IrInstructionIdReturnPtr,
2212 IrInstructionIdCallSrc,
2213 IrInstructionIdCallGen,
21892214 IrInstructionIdConst,
21902215 IrInstructionIdReturn,
21912216 IrInstructionIdCast,
21922217 IrInstructionIdResizeSlice,
21932218 IrInstructionIdContainerInitList,
21942219 IrInstructionIdContainerInitFields,
2195 IrInstructionIdStructInit,
2196 IrInstructionIdUnionInit,
21972220 IrInstructionIdUnreachable,
21982221 IrInstructionIdTypeOf,
2199 IrInstructionIdToPtrType,
2200 IrInstructionIdPtrTypeChild,
22012222 IrInstructionIdSetCold,
22022223 IrInstructionIdSetRuntimeSafety,
22032224 IrInstructionIdSetFloatMode,
......@@ -2222,6 +2243,7 @@ enum IrInstructionId {
22222243 IrInstructionIdCDefine,
22232244 IrInstructionIdCUndef,
22242245 IrInstructionIdRef,
2246 IrInstructionIdRefGen,
22252247 IrInstructionIdCompileErr,
22262248 IrInstructionIdCompileLog,
22272249 IrInstructionIdErrName,
......@@ -2240,7 +2262,8 @@ enum IrInstructionId {
22402262 IrInstructionIdBoolNot,
22412263 IrInstructionIdMemset,
22422264 IrInstructionIdMemcpy,
2243 IrInstructionIdSlice,
2265 IrInstructionIdSliceSrc,
2266 IrInstructionIdSliceGen,
22442267 IrInstructionIdMemberCount,
22452268 IrInstructionIdMemberType,
22462269 IrInstructionIdMemberName,
......@@ -2250,9 +2273,10 @@ enum IrInstructionId {
22502273 IrInstructionIdHandle,
22512274 IrInstructionIdAlignOf,
22522275 IrInstructionIdOverflowOp,
2276 IrInstructionIdTestErrSrc,
2277 IrInstructionIdTestErrGen,
22532278 IrInstructionIdMulAdd,
22542279 IrInstructionIdFloatOp,
2255 IrInstructionIdTestErr,
22562280 IrInstructionIdUnwrapErrCode,
22572281 IrInstructionIdUnwrapErrPayload,
22582282 IrInstructionIdErrWrapCode,
......@@ -2261,7 +2285,7 @@ enum IrInstructionId {
22612285 IrInstructionIdTestComptime,
22622286 IrInstructionIdPtrCastSrc,
22632287 IrInstructionIdPtrCastGen,
2264 IrInstructionIdBitCast,
2288 IrInstructionIdBitCastSrc,
22652289 IrInstructionIdBitCastGen,
22662290 IrInstructionIdWidenOrShorten,
22672291 IrInstructionIdIntToPtr,
......@@ -2285,6 +2309,10 @@ enum IrInstructionId {
22852309 IrInstructionIdSetEvalBranchQuota,
22862310 IrInstructionIdPtrType,
22872311 IrInstructionIdAlignCast,
2312 IrInstructionIdImplicitCast,
2313 IrInstructionIdResolveResult,
2314 IrInstructionIdResetResult,
2315 IrInstructionIdResultPtr,
22882316 IrInstructionIdOpaqueType,
22892317 IrInstructionIdSetAlignStack,
22902318 IrInstructionIdArgType,
......@@ -2323,10 +2351,13 @@ enum IrInstructionId {
23232351 IrInstructionIdAssertNonNull,
23242352 IrInstructionIdHasDecl,
23252353 IrInstructionIdUndeclaredIdent,
2354 IrInstructionIdAllocaSrc,
2355 IrInstructionIdAllocaGen,
2356 IrInstructionIdEndExpr,
2357 IrInstructionIdPtrOfArrayToSlice,
23262358};
23272359
23282360struct IrInstruction {
2329 IrInstructionId id;
23302361 Scope *scope;
23312362 AstNode *source_node;
23322363 ConstExprValue value;
......@@ -2340,6 +2371,7 @@ struct IrInstruction {
23402371 // with this child field.
23412372 IrInstruction *child;
23422373 IrBasicBlock *owner_bb;
2374 IrInstructionId id;
23432375 // true if this instruction was generated by zig and not from user code
23442376 bool is_gen;
23452377};
......@@ -2350,14 +2382,14 @@ struct IrInstructionDeclVarSrc {
23502382 ZigVar *var;
23512383 IrInstruction *var_type;
23522384 IrInstruction *align_value;
2353 IrInstruction *init_value;
2385 IrInstruction *ptr;
23542386};
23552387
23562388struct IrInstructionDeclVarGen {
23572389 IrInstruction base;
23582390
23592391 ZigVar *var;
2360 IrInstruction *init_value;
2392 IrInstruction *var_ptr;
23612393};
23622394
23632395struct IrInstructionCondBr {
......@@ -2367,6 +2399,7 @@ struct IrInstructionCondBr {
23672399 IrBasicBlock *then_block;
23682400 IrBasicBlock *else_block;
23692401 IrInstruction *is_comptime;
2402 ResultLoc *result_loc;
23702403};
23712404
23722405struct IrInstructionBr {
......@@ -2419,6 +2452,7 @@ struct IrInstructionPhi {
24192452 size_t incoming_count;
24202453 IrBasicBlock **incoming_blocks;
24212454 IrInstruction **incoming_values;
2455 ResultLocPeerParent *peer_parent;
24222456};
24232457
24242458enum IrUnOp {
......@@ -2434,8 +2468,9 @@ struct IrInstructionUnOp {
24342468 IrInstruction base;
24352469
24362470 IrUnOp op_id;
2437 IrInstruction *value;
24382471 LVal lval;
2472 IrInstruction *value;
2473 ResultLoc *result_loc;
24392474};
24402475
24412476enum IrBinOp {
......@@ -2492,7 +2527,7 @@ struct IrInstructionLoadPtrGen {
24922527 IrInstruction base;
24932528
24942529 IrInstruction *ptr;
2495 LLVMValueRef tmp_ptr;
2530 IrInstruction *result_loc;
24962531};
24972532
24982533struct IrInstructionStorePtr {
......@@ -2505,6 +2540,7 @@ struct IrInstructionStorePtr {
25052540struct IrInstructionFieldPtr {
25062541 IrInstruction base;
25072542
2543 bool initializing;
25082544 IrInstruction *container_ptr;
25092545 Buf *field_name_buffer;
25102546 IrInstruction *field_name_expr;
......@@ -2521,9 +2557,10 @@ struct IrInstructionStructFieldPtr {
25212557struct IrInstructionUnionFieldPtr {
25222558 IrInstruction base;
25232559
2560 bool safety_check_on;
2561 bool initializing;
25242562 IrInstruction *union_ptr;
25252563 TypeUnionField *field;
2526 bool is_const;
25272564};
25282565
25292566struct IrInstructionElemPtr {
......@@ -2531,8 +2568,8 @@ struct IrInstructionElemPtr {
25312568
25322569 IrInstruction *array_ptr;
25332570 IrInstruction *elem_index;
2571 IrInstruction *init_array_type;
25342572 PtrLen ptr_len;
2535 bool is_const;
25362573 bool safety_check_on;
25372574};
25382575
......@@ -2543,14 +2580,21 @@ struct IrInstructionVarPtr {
25432580 ScopeFnDef *crossed_fndef_scope;
25442581};
25452582
2546struct IrInstructionCall {
2583// For functions that have a return type for which handle_is_ptr is true, a
2584// result location pointer is the secret first parameter ("sret"). This
2585// instruction returns that pointer.
2586struct IrInstructionReturnPtr {
2587 IrInstruction base;
2588};
2589
2590struct IrInstructionCallSrc {
25472591 IrInstruction base;
25482592
25492593 IrInstruction *fn_ref;
25502594 ZigFn *fn_entry;
25512595 size_t arg_count;
25522596 IrInstruction **args;
2553 LLVMValueRef tmp_ptr;
2597 ResultLoc *result_loc;
25542598
25552599 IrInstruction *async_allocator;
25562600 IrInstruction *new_stack;
......@@ -2559,6 +2603,21 @@ struct IrInstructionCall {
25592603 bool is_comptime;
25602604};
25612605
2606struct IrInstructionCallGen {
2607 IrInstruction base;
2608
2609 IrInstruction *fn_ref;
2610 ZigFn *fn_entry;
2611 size_t arg_count;
2612 IrInstruction **args;
2613 IrInstruction *result_loc;
2614
2615 IrInstruction *async_allocator;
2616 IrInstruction *new_stack;
2617 FnInline fn_inline;
2618 bool is_async;
2619};
2620
25622621struct IrInstructionConst {
25632622 IrInstruction base;
25642623};
......@@ -2581,7 +2640,6 @@ enum CastOp {
25812640 CastOpNumLitToConcrete,
25822641 CastOpErrSet,
25832642 CastOpBitCast,
2584 CastOpPtrOfArrayToSlice,
25852643};
25862644
25872645// TODO get rid of this instruction, replace with instructions for each op code
......@@ -2591,14 +2649,13 @@ struct IrInstructionCast {
25912649 IrInstruction *value;
25922650 ZigType *dest_type;
25932651 CastOp cast_op;
2594 LLVMValueRef tmp_ptr;
25952652};
25962653
25972654struct IrInstructionResizeSlice {
25982655 IrInstruction base;
25992656
26002657 IrInstruction *operand;
2601 LLVMValueRef tmp_ptr;
2658 IrInstruction *result_loc;
26022659};
26032660
26042661struct IrInstructionContainerInitList {
......@@ -2607,15 +2664,15 @@ struct IrInstructionContainerInitList {
26072664 IrInstruction *container_type;
26082665 IrInstruction *elem_type;
26092666 size_t item_count;
2610 IrInstruction **items;
2611 LLVMValueRef tmp_ptr;
2667 IrInstruction **elem_result_loc_list;
2668 IrInstruction *result_loc;
26122669};
26132670
26142671struct IrInstructionContainerInitFieldsField {
26152672 Buf *name;
2616 IrInstruction *value;
26172673 AstNode *source_node;
26182674 TypeStructField *type_struct_field;
2675 IrInstruction *result_loc;
26192676};
26202677
26212678struct IrInstructionContainerInitFields {
......@@ -2624,29 +2681,7 @@ struct IrInstructionContainerInitFields {
26242681 IrInstruction *container_type;
26252682 size_t field_count;
26262683 IrInstructionContainerInitFieldsField *fields;
2627};
2628
2629struct IrInstructionStructInitField {
2630 IrInstruction *value;
2631 TypeStructField *type_struct_field;
2632};
2633
2634struct IrInstructionStructInit {
2635 IrInstruction base;
2636
2637 ZigType *struct_type;
2638 size_t field_count;
2639 IrInstructionStructInitField *fields;
2640 LLVMValueRef tmp_ptr;
2641};
2642
2643struct IrInstructionUnionInit {
2644 IrInstruction base;
2645
2646 ZigType *union_type;
2647 TypeUnionField *field;
2648 IrInstruction *init_value;
2649 LLVMValueRef tmp_ptr;
2684 IrInstruction *result_loc;
26502685};
26512686
26522687struct IrInstructionUnreachable {
......@@ -2659,18 +2694,6 @@ struct IrInstructionTypeOf {
26592694 IrInstruction *value;
26602695};
26612696
2662struct IrInstructionToPtrType {
2663 IrInstruction base;
2664
2665 IrInstruction *ptr;
2666};
2667
2668struct IrInstructionPtrTypeChild {
2669 IrInstruction base;
2670
2671 IrInstruction *value;
2672};
2673
26742697struct IrInstructionSetCold {
26752698 IrInstruction base;
26762699
......@@ -2764,8 +2787,9 @@ struct IrInstructionTestNonNull {
27642787struct IrInstructionOptionalUnwrapPtr {
27652788 IrInstruction base;
27662789
2767 IrInstruction *base_ptr;
27682790 bool safety_check_on;
2791 bool initializing;
2792 IrInstruction *base_ptr;
27692793};
27702794
27712795struct IrInstructionCtz {
......@@ -2805,11 +2829,17 @@ struct IrInstructionRef {
28052829 IrInstruction base;
28062830
28072831 IrInstruction *value;
2808 LLVMValueRef tmp_ptr;
28092832 bool is_const;
28102833 bool is_volatile;
28112834};
28122835
2836struct IrInstructionRefGen {
2837 IrInstruction base;
2838
2839 IrInstruction *operand;
2840 IrInstruction *result_loc;
2841};
2842
28132843struct IrInstructionCompileErr {
28142844 IrInstruction base;
28152845
......@@ -2861,26 +2891,26 @@ struct IrInstructionEmbedFile {
28612891struct IrInstructionCmpxchgSrc {
28622892 IrInstruction base;
28632893
2894 bool is_weak;
28642895 IrInstruction *type_value;
28652896 IrInstruction *ptr;
28662897 IrInstruction *cmp_value;
28672898 IrInstruction *new_value;
28682899 IrInstruction *success_order_value;
28692900 IrInstruction *failure_order_value;
2870
2871 bool is_weak;
2901 ResultLoc *result_loc;
28722902};
28732903
28742904struct IrInstructionCmpxchgGen {
28752905 IrInstruction base;
28762906
2907 bool is_weak;
2908 AtomicOrder success_order;
2909 AtomicOrder failure_order;
28772910 IrInstruction *ptr;
28782911 IrInstruction *cmp_value;
28792912 IrInstruction *new_value;
2880 LLVMValueRef tmp_ptr;
2881 AtomicOrder success_order;
2882 AtomicOrder failure_order;
2883 bool is_weak;
2913 IrInstruction *result_loc;
28842914};
28852915
28862916struct IrInstructionFence {
......@@ -2924,6 +2954,7 @@ struct IrInstructionToBytes {
29242954 IrInstruction base;
29252955
29262956 IrInstruction *target;
2957 ResultLoc *result_loc;
29272958};
29282959
29292960struct IrInstructionFromBytes {
......@@ -2931,6 +2962,7 @@ struct IrInstructionFromBytes {
29312962
29322963 IrInstruction *dest_child_type;
29332964 IrInstruction *target;
2965 ResultLoc *result_loc;
29342966};
29352967
29362968struct IrInstructionIntToFloat {
......@@ -2989,14 +3021,24 @@ struct IrInstructionMemcpy {
29893021 IrInstruction *count;
29903022};
29913023
2992struct IrInstructionSlice {
3024struct IrInstructionSliceSrc {
29933025 IrInstruction base;
29943026
3027 bool safety_check_on;
29953028 IrInstruction *ptr;
29963029 IrInstruction *start;
29973030 IrInstruction *end;
3031 ResultLoc *result_loc;
3032};
3033
3034struct IrInstructionSliceGen {
3035 IrInstruction base;
3036
29983037 bool safety_check_on;
2999 LLVMValueRef tmp_ptr;
3038 IrInstruction *ptr;
3039 IrInstruction *start;
3040 IrInstruction *end;
3041 IrInstruction *result_loc;
30003042};
30013043
30023044struct IrInstructionMemberCount {
......@@ -3070,44 +3112,54 @@ struct IrInstructionAlignOf {
30703112};
30713113
30723114// returns true if error, returns false if not error
3073struct IrInstructionTestErr {
3115struct IrInstructionTestErrSrc {
30743116 IrInstruction base;
30753117
3076 IrInstruction *value;
3118 bool resolve_err_set;
3119 IrInstruction *base_ptr;
30773120};
30783121
3079struct IrInstructionUnwrapErrCode {
3122struct IrInstructionTestErrGen {
30803123 IrInstruction base;
30813124
30823125 IrInstruction *err_union;
30833126};
30843127
3128// Takes an error union pointer, returns a pointer to the error code.
3129struct IrInstructionUnwrapErrCode {
3130 IrInstruction base;
3131
3132 bool initializing;
3133 IrInstruction *err_union_ptr;
3134};
3135
30853136struct IrInstructionUnwrapErrPayload {
30863137 IrInstruction base;
30873138
3088 IrInstruction *value;
30893139 bool safety_check_on;
3140 bool initializing;
3141 IrInstruction *value;
30903142};
30913143
30923144struct IrInstructionOptionalWrap {
30933145 IrInstruction base;
30943146
3095 IrInstruction *value;
3096 LLVMValueRef tmp_ptr;
3147 IrInstruction *operand;
3148 IrInstruction *result_loc;
30973149};
30983150
30993151struct IrInstructionErrWrapPayload {
31003152 IrInstruction base;
31013153
3102 IrInstruction *value;
3103 LLVMValueRef tmp_ptr;
3154 IrInstruction *operand;
3155 IrInstruction *result_loc;
31043156};
31053157
31063158struct IrInstructionErrWrapCode {
31073159 IrInstruction base;
31083160
3109 IrInstruction *value;
3110 LLVMValueRef tmp_ptr;
3161 IrInstruction *operand;
3162 IrInstruction *result_loc;
31113163};
31123164
31133165struct IrInstructionFnProto {
......@@ -3142,18 +3194,17 @@ struct IrInstructionPtrCastGen {
31423194 bool safety_check_on;
31433195};
31443196
3145struct IrInstructionBitCast {
3197struct IrInstructionBitCastSrc {
31463198 IrInstruction base;
31473199
3148 IrInstruction *dest_type;
3149 IrInstruction *value;
3200 IrInstruction *operand;
3201 ResultLocBitCast *result_loc_bit_cast;
31503202};
31513203
31523204struct IrInstructionBitCastGen {
31533205 IrInstruction base;
31543206
31553207 IrInstruction *operand;
3156 LLVMValueRef tmp_ptr;
31573208};
31583209
31593210struct IrInstructionWidenOrShorten {
......@@ -3229,8 +3280,8 @@ struct IrInstructionTypeName {
32293280struct IrInstructionDeclRef {
32303281 IrInstruction base;
32313282
3232 Tld *tld;
32333283 LVal lval;
3284 Tld *tld;
32343285};
32353286
32363287struct IrInstructionPanic {
......@@ -3526,7 +3577,7 @@ struct IrInstructionVectorToArray {
35263577 IrInstruction base;
35273578
35283579 IrInstruction *vector;
3529 LLVMValueRef tmp_ptr;
3580 IrInstruction *result_loc;
35303581};
35313582
35323583struct IrInstructionAssertZero {
......@@ -3554,6 +3605,139 @@ struct IrInstructionUndeclaredIdent {
35543605 Buf *name;
35553606};
35563607
3608struct IrInstructionAllocaSrc {
3609 IrInstruction base;
3610
3611 IrInstruction *align;
3612 IrInstruction *is_comptime;
3613 const char *name_hint;
3614};
3615
3616struct IrInstructionAllocaGen {
3617 IrInstruction base;
3618
3619 uint32_t align;
3620 const char *name_hint;
3621};
3622
3623struct IrInstructionEndExpr {
3624 IrInstruction base;
3625
3626 IrInstruction *value;
3627 ResultLoc *result_loc;
3628};
3629
3630struct IrInstructionImplicitCast {
3631 IrInstruction base;
3632
3633 IrInstruction *dest_type;
3634 IrInstruction *target;
3635 ResultLoc *result_loc;
3636};
3637
3638// This one is for writing through the result pointer.
3639struct IrInstructionResolveResult {
3640 IrInstruction base;
3641
3642 ResultLoc *result_loc;
3643 IrInstruction *ty;
3644};
3645
3646// This one is when you want to read the value of the result.
3647// You have to give the value in case it is comptime.
3648struct IrInstructionResultPtr {
3649 IrInstruction base;
3650
3651 ResultLoc *result_loc;
3652 IrInstruction *result;
3653};
3654
3655struct IrInstructionResetResult {
3656 IrInstruction base;
3657
3658 ResultLoc *result_loc;
3659};
3660
3661struct IrInstructionPtrOfArrayToSlice {
3662 IrInstruction base;
3663
3664 IrInstruction *operand;
3665 IrInstruction *result_loc;
3666};
3667
3668enum ResultLocId {
3669 ResultLocIdInvalid,
3670 ResultLocIdNone,
3671 ResultLocIdVar,
3672 ResultLocIdReturn,
3673 ResultLocIdPeer,
3674 ResultLocIdPeerParent,
3675 ResultLocIdInstruction,
3676 ResultLocIdBitCast,
3677};
3678
3679// Additions to this struct may need to be handled in
3680// ir_reset_result
3681struct ResultLoc {
3682 ResultLocId id;
3683 bool written;
3684 IrInstruction *resolved_loc; // result ptr
3685 IrInstruction *source_instruction;
3686 IrInstruction *gen_instruction; // value to store to the result loc
3687 ZigType *implicit_elem_type;
3688};
3689
3690struct ResultLocNone {
3691 ResultLoc base;
3692};
3693
3694struct ResultLocVar {
3695 ResultLoc base;
3696
3697 ZigVar *var;
3698};
3699
3700struct ResultLocReturn {
3701 ResultLoc base;
3702};
3703
3704struct IrSuspendPosition {
3705 size_t basic_block_index;
3706 size_t instruction_index;
3707};
3708
3709struct ResultLocPeerParent {
3710 ResultLoc base;
3711
3712 bool skipped;
3713 bool done_resuming;
3714 IrBasicBlock *end_bb;
3715 ResultLoc *parent;
3716 ZigList<ResultLocPeer *> peers;
3717 ZigType *resolved_type;
3718 IrInstruction *is_comptime;
3719};
3720
3721struct ResultLocPeer {
3722 ResultLoc base;
3723
3724 ResultLocPeerParent *parent;
3725 IrBasicBlock *next_bb;
3726 IrSuspendPosition suspend_pos;
3727};
3728
3729// The result location is the source instruction
3730struct ResultLocInstruction {
3731 ResultLoc base;
3732};
3733
3734// The source_instruction is the destination type
3735struct ResultLocBitCast {
3736 ResultLoc base;
3737
3738 ResultLoc *parent;
3739};
3740
35573741static const size_t slice_ptr_index = 0;
35583742static const size_t slice_len_index = 1;
35593743
......@@ -3601,7 +3785,7 @@ struct FnWalkAttrs {
36013785
36023786struct FnWalkCall {
36033787 ZigList<LLVMValueRef> *gen_param_values;
3604 IrInstructionCall *inst;
3788 IrInstructionCallGen *inst;
36053789 bool is_var_args;
36063790};
36073791
src/analyze.cpp+9-14
......@@ -2995,7 +2995,7 @@ void scan_decls(CodeGen *g, ScopeDecls *decls_scope, AstNode *node) {
29952995 case NodeTypeBlock:
29962996 case NodeTypeGroupedExpr:
29972997 case NodeTypeBinOpExpr:
2998 case NodeTypeUnwrapErrorExpr:
2998 case NodeTypeCatchExpr:
29992999 case NodeTypeFnCallExpr:
30003000 case NodeTypeArrayAccessExpr:
30013001 case NodeTypeSliceExpr:
......@@ -4181,6 +4181,7 @@ static uint32_t hash_const_val_ptr(ConstExprValue *const_val) {
41814181 case ConstPtrMutComptimeConst:
41824182 hash_val += (uint32_t)4214318515;
41834183 break;
4184 case ConstPtrMutInfer:
41844185 case ConstPtrMutComptimeVar:
41854186 hash_val += (uint32_t)1103195694;
41864187 break;
......@@ -4511,6 +4512,8 @@ bool fn_eval_cacheable(Scope *scope, ZigType *return_type) {
45114512 ScopeVarDecl *var_scope = (ScopeVarDecl *)scope;
45124513 if (type_is_invalid(var_scope->var->var_type))
45134514 return false;
4515 if (var_scope->var->const_value->special == ConstValSpecialUndef)
4516 return false;
45144517 if (can_mutate_comptime_var_state(var_scope->var->const_value))
45154518 return false;
45164519 } else if (scope->id == ScopeIdFnDef) {
......@@ -4710,7 +4713,7 @@ ReqCompTime type_requires_comptime(CodeGen *g, ZigType *type_entry) {
47104713void init_const_str_lit(CodeGen *g, ConstExprValue *const_val, Buf *str) {
47114714 auto entry = g->string_literals_table.maybe_get(str);
47124715 if (entry != nullptr) {
4713 *const_val = *entry->value;
4716 memcpy(const_val, entry->value, sizeof(ConstExprValue));
47144717 return;
47154718 }
47164719
......@@ -4998,12 +5001,9 @@ void init_const_undefined(CodeGen *g, ConstExprValue *const_val) {
49985001 field_val->type = wanted_type->data.structure.fields[i].type_entry;
49995002 assert(field_val->type);
50005003 init_const_undefined(g, field_val);
5001 ConstParent *parent = get_const_val_parent(g, field_val);
5002 if (parent != nullptr) {
5003 parent->id = ConstParentIdStruct;
5004 parent->data.p_struct.struct_val = const_val;
5005 parent->data.p_struct.field_index = i;
5006 }
5004 field_val->parent.id = ConstParentIdStruct;
5005 field_val->parent.data.p_struct.struct_val = const_val;
5006 field_val->parent.data.p_struct.field_index = i;
50075007 }
50085008 } else {
50095009 const_val->special = ConstValSpecialUndef;
......@@ -5843,11 +5843,6 @@ void expand_undef_array(CodeGen *g, ConstExprValue *const_val) {
58435843 zig_unreachable();
58445844}
58455845
5846// Deprecated. Reference the parent field directly.
5847ConstParent *get_const_val_parent(CodeGen *g, ConstExprValue *value) {
5848 return &value->parent;
5849}
5850
58515846static const ZigTypeId all_type_ids[] = {
58525847 ZigTypeIdMetaType,
58535848 ZigTypeIdVoid,
......@@ -7281,6 +7276,6 @@ void src_assert(bool ok, AstNode *source_node) {
72817276 buf_ptr(source_node->owner->data.structure.root_struct->path),
72827277 (unsigned)source_node->line + 1, (unsigned)source_node->column + 1);
72837278 }
7284 const char *msg = "assertion failed";
7279 const char *msg = "assertion failed. This is a bug in the Zig compiler.";
72857280 stage2_panic(msg, strlen(msg));
72867281}
src/analyze.hpp-1
......@@ -180,7 +180,6 @@ void init_const_undefined(CodeGen *g, ConstExprValue *const_val);
180180ConstExprValue *create_const_vals(size_t count);
181181
182182ZigType *make_int_type(CodeGen *g, bool is_signed, uint32_t size_in_bits);
183ConstParent *get_const_val_parent(CodeGen *g, ConstExprValue *value);
184183void expand_undef_array(CodeGen *g, ConstExprValue *const_val);
185184void update_compile_var(CodeGen *g, Buf *name, ConstExprValue *value);
186185
src/ast_render.cpp+3-3
......@@ -165,8 +165,8 @@ static const char *node_type_str(NodeType node_type) {
165165 return "Parens";
166166 case NodeTypeBinOpExpr:
167167 return "BinOpExpr";
168 case NodeTypeUnwrapErrorExpr:
169 return "UnwrapErrorExpr";
168 case NodeTypeCatchExpr:
169 return "CatchExpr";
170170 case NodeTypeFnCallExpr:
171171 return "FnCallExpr";
172172 case NodeTypeArrayAccessExpr:
......@@ -1108,7 +1108,7 @@ static void render_node_extra(AstRender *ar, AstNode *node, bool grouped) {
11081108 fprintf(ar->f, "]");
11091109 break;
11101110 }
1111 case NodeTypeUnwrapErrorExpr:
1111 case NodeTypeCatchExpr:
11121112 {
11131113 render_node_ungrouped(ar, node->data.unwrap_err_expr.op1);
11141114 fprintf(ar->f, " catch ");
src/codegen.cpp+280-325
......@@ -691,7 +691,9 @@ static ZigLLVMDIScope *get_di_scope(CodeGen *g, Scope *scope) {
691691 is_definition, scope_line, flags, is_optimized, nullptr);
692692
693693 scope->di_scope = ZigLLVMSubprogramToScope(subprogram);
694 ZigLLVMFnSetSubprogram(fn_llvm_value(g, fn_table_entry), subprogram);
694 if (!g->strip_debug_symbols) {
695 ZigLLVMFnSetSubprogram(fn_llvm_value(g, fn_table_entry), subprogram);
696 }
695697 return scope->di_scope;
696698 }
697699 case ScopeIdDecls:
......@@ -859,9 +861,7 @@ static LLVMValueRef gen_store_untyped(CodeGen *g, LLVMValueRef value, LLVMValueR
859861{
860862 LLVMValueRef instruction = LLVMBuildStore(g->builder, value, ptr);
861863 if (is_volatile) LLVMSetVolatile(instruction, true);
862 if (alignment == 0) {
863 LLVMSetAlignment(instruction, LLVMABIAlignmentOfType(g->target_data_ref, LLVMTypeOf(value)));
864 } else {
864 if (alignment != 0) {
865865 LLVMSetAlignment(instruction, alignment);
866866 }
867867 return instruction;
......@@ -1339,7 +1339,9 @@ static LLVMValueRef get_add_error_return_trace_addr_fn(CodeGen *g) {
13391339 LLVMBuildRetVoid(g->builder);
13401340
13411341 LLVMPositionBuilderAtEnd(g->builder, prev_block);
1342 LLVMSetCurrentDebugLocation(g->builder, prev_debug_location);
1342 if (!g->strip_debug_symbols) {
1343 LLVMSetCurrentDebugLocation(g->builder, prev_debug_location);
1344 }
13431345
13441346 g->add_error_return_trace_addr_fn_val = fn_val;
13451347 return fn_val;
......@@ -1470,7 +1472,9 @@ static LLVMValueRef get_merge_err_ret_traces_fn_val(CodeGen *g) {
14701472 LLVMBuildBr(g->builder, loop_block);
14711473
14721474 LLVMPositionBuilderAtEnd(g->builder, prev_block);
1473 LLVMSetCurrentDebugLocation(g->builder, prev_debug_location);
1475 if (!g->strip_debug_symbols) {
1476 LLVMSetCurrentDebugLocation(g->builder, prev_debug_location);
1477 }
14741478
14751479 g->merge_err_ret_traces_fn_val = fn_val;
14761480 return fn_val;
......@@ -1526,7 +1530,9 @@ static LLVMValueRef get_return_err_fn(CodeGen *g) {
15261530 LLVMBuildRetVoid(g->builder);
15271531
15281532 LLVMPositionBuilderAtEnd(g->builder, prev_block);
1529 LLVMSetCurrentDebugLocation(g->builder, prev_debug_location);
1533 if (!g->strip_debug_symbols) {
1534 LLVMSetCurrentDebugLocation(g->builder, prev_debug_location);
1535 }
15301536
15311537 g->return_err_fn = fn_val;
15321538 return fn_val;
......@@ -1654,7 +1660,9 @@ static LLVMValueRef get_safety_crash_err_fn(CodeGen *g) {
16541660 gen_panic(g, msg_slice, err_ret_trace_arg);
16551661
16561662 LLVMPositionBuilderAtEnd(g->builder, prev_block);
1657 LLVMSetCurrentDebugLocation(g->builder, prev_debug_location);
1663 if (!g->strip_debug_symbols) {
1664 LLVMSetCurrentDebugLocation(g->builder, prev_debug_location);
1665 }
16581666
16591667 g->safety_crash_err_fn = fn_val;
16601668 return fn_val;
......@@ -2004,6 +2012,7 @@ static LLVMValueRef gen_assign_raw(CodeGen *g, LLVMValueRef ptr, ZigType *ptr_ty
20042012}
20052013
20062014static void gen_var_debug_decl(CodeGen *g, ZigVar *var) {
2015 if (g->strip_debug_symbols) return;
20072016 assert(var->di_loc_var != nullptr);
20082017 AstNode *source_node = var->decl_node;
20092018 ZigLLVMDILocation *debug_loc = ZigLLVMGetDebugLoc((unsigned)source_node->line + 1,
......@@ -2016,7 +2025,7 @@ static LLVMValueRef ir_llvm_value(CodeGen *g, IrInstruction *instruction) {
20162025 if (!type_has_bits(instruction->value.type))
20172026 return nullptr;
20182027 if (!instruction->llvm_value) {
2019 assert(instruction->value.special != ConstValSpecialRuntime);
2028 src_assert(instruction->value.special != ConstValSpecialRuntime, instruction->source_node);
20202029 assert(instruction->value.type);
20212030 render_const_val(g, &instruction->value, "");
20222031 // we might have to do some pointer casting here due to the way union
......@@ -2025,11 +2034,9 @@ static LLVMValueRef ir_llvm_value(CodeGen *g, IrInstruction *instruction) {
20252034 render_const_val_global(g, &instruction->value, "");
20262035 ZigType *ptr_type = get_pointer_to_type(g, instruction->value.type, true);
20272036 instruction->llvm_value = LLVMBuildBitCast(g->builder, instruction->value.global_refs->llvm_global, get_llvm_type(g, ptr_type), "");
2028 } else if (get_codegen_ptr_type(instruction->value.type) != nullptr) {
2037 } else {
20292038 instruction->llvm_value = LLVMBuildBitCast(g->builder, instruction->value.global_refs->llvm_value,
20302039 get_llvm_type(g, instruction->value.type), "");
2031 } else {
2032 instruction->llvm_value = instruction->value.global_refs->llvm_value;
20332040 }
20342041 assert(instruction->llvm_value);
20352042 }
......@@ -2310,7 +2317,7 @@ void walk_function_params(CodeGen *g, ZigType *fn_type, FnWalk *fn_walk) {
23102317 return;
23112318 }
23122319 if (fn_walk->id == FnWalkIdCall) {
2313 IrInstructionCall *instruction = fn_walk->data.call.inst;
2320 IrInstructionCallGen *instruction = fn_walk->data.call.inst;
23142321 bool is_var_args = fn_walk->data.call.is_var_args;
23152322 for (size_t call_i = 0; call_i < instruction->arg_count; call_i += 1) {
23162323 IrInstruction *param_instruction = instruction->args[call_i];
......@@ -2404,17 +2411,33 @@ static LLVMValueRef ir_render_save_err_ret_addr(CodeGen *g, IrExecutable *execut
24042411}
24052412
24062413static LLVMValueRef ir_render_return(CodeGen *g, IrExecutable *executable, IrInstructionReturn *return_instruction) {
2407 LLVMValueRef value = ir_llvm_value(g, return_instruction->value);
2408 ZigType *return_type = return_instruction->value->value.type;
2409
24102414 if (want_first_arg_sret(g, &g->cur_fn->type_entry->data.fn.fn_type_id)) {
2415 if (return_instruction->value == nullptr) {
2416 LLVMBuildRetVoid(g->builder);
2417 return nullptr;
2418 }
24112419 assert(g->cur_ret_ptr);
2420 src_assert(return_instruction->value->value.special != ConstValSpecialRuntime,
2421 return_instruction->base.source_node);
2422 LLVMValueRef value = ir_llvm_value(g, return_instruction->value);
2423 ZigType *return_type = return_instruction->value->value.type;
24122424 gen_assign_raw(g, g->cur_ret_ptr, get_pointer_to_type(g, return_type, false), value);
24132425 LLVMBuildRetVoid(g->builder);
2414 } else if (handle_is_ptr(return_type)) {
2415 LLVMValueRef by_val_value = gen_load_untyped(g, value, 0, false, "");
2416 LLVMBuildRet(g->builder, by_val_value);
2426 } else if (g->cur_fn->type_entry->data.fn.fn_type_id.cc != CallingConventionAsync &&
2427 handle_is_ptr(g->cur_fn->type_entry->data.fn.fn_type_id.return_type))
2428 {
2429 if (return_instruction->value == nullptr) {
2430 LLVMValueRef by_val_value = gen_load_untyped(g, g->cur_ret_ptr, 0, false, "");
2431 LLVMBuildRet(g->builder, by_val_value);
2432 } else {
2433 LLVMValueRef value = ir_llvm_value(g, return_instruction->value);
2434 LLVMValueRef by_val_value = gen_load_untyped(g, value, 0, false, "");
2435 LLVMBuildRet(g->builder, by_val_value);
2436 }
2437 } else if (return_instruction->value == nullptr) {
2438 LLVMBuildRetVoid(g->builder);
24172439 } else {
2440 LLVMValueRef value = ir_llvm_value(g, return_instruction->value);
24182441 LLVMBuildRet(g->builder, value);
24192442 }
24202443 return nullptr;
......@@ -2952,7 +2975,7 @@ static LLVMValueRef ir_render_resize_slice(CodeGen *g, IrExecutable *executable,
29522975 LLVMValueRef expr_val = ir_llvm_value(g, instruction->operand);
29532976 assert(expr_val);
29542977
2955 assert(instruction->tmp_ptr);
2978 LLVMValueRef result_loc = ir_llvm_value(g, instruction->result_loc);
29562979 assert(wanted_type->id == ZigTypeIdStruct);
29572980 assert(wanted_type->data.structure.is_slice);
29582981 assert(actual_type->id == ZigTypeIdStruct);
......@@ -2973,7 +2996,7 @@ static LLVMValueRef ir_render_resize_slice(CodeGen *g, IrExecutable *executable,
29732996 LLVMValueRef src_ptr = gen_load_untyped(g, src_ptr_ptr, 0, false, "");
29742997 LLVMValueRef src_ptr_casted = LLVMBuildBitCast(g->builder, src_ptr,
29752998 get_llvm_type(g, wanted_type->data.structure.fields[0].type_entry), "");
2976 LLVMValueRef dest_ptr_ptr = LLVMBuildStructGEP(g->builder, instruction->tmp_ptr,
2999 LLVMValueRef dest_ptr_ptr = LLVMBuildStructGEP(g->builder, result_loc,
29773000 (unsigned)wanted_ptr_index, "");
29783001 gen_store_untyped(g, src_ptr_casted, dest_ptr_ptr, 0, false);
29793002
......@@ -3006,12 +3029,10 @@ static LLVMValueRef ir_render_resize_slice(CodeGen *g, IrExecutable *executable,
30063029 zig_unreachable();
30073030 }
30083031
3009 LLVMValueRef dest_len_ptr = LLVMBuildStructGEP(g->builder, instruction->tmp_ptr,
3010 (unsigned)wanted_len_index, "");
3032 LLVMValueRef dest_len_ptr = LLVMBuildStructGEP(g->builder, result_loc, (unsigned)wanted_len_index, "");
30113033 gen_store_untyped(g, new_len, dest_len_ptr, 0, false);
30123034
3013
3014 return instruction->tmp_ptr;
3035 return result_loc;
30153036}
30163037
30173038static LLVMValueRef ir_render_cast(CodeGen *g, IrExecutable *executable,
......@@ -3081,33 +3102,39 @@ static LLVMValueRef ir_render_cast(CodeGen *g, IrExecutable *executable,
30813102 return expr_val;
30823103 case CastOpBitCast:
30833104 return LLVMBuildBitCast(g->builder, expr_val, get_llvm_type(g, wanted_type), "");
3084 case CastOpPtrOfArrayToSlice: {
3085 assert(cast_instruction->tmp_ptr);
3086 assert(actual_type->id == ZigTypeIdPointer);
3087 ZigType *array_type = actual_type->data.pointer.child_type;
3088 assert(array_type->id == ZigTypeIdArray);
3089
3090 LLVMValueRef ptr_field_ptr = LLVMBuildStructGEP(g->builder, cast_instruction->tmp_ptr,
3091 slice_ptr_index, "");
3092 LLVMValueRef indices[] = {
3093 LLVMConstNull(g->builtin_types.entry_usize->llvm_type),
3094 LLVMConstInt(g->builtin_types.entry_usize->llvm_type, 0, false),
3095 };
3096 LLVMValueRef slice_start_ptr = LLVMBuildInBoundsGEP(g->builder, expr_val, indices, 2, "");
3097 gen_store_untyped(g, slice_start_ptr, ptr_field_ptr, 0, false);
3098
3099 LLVMValueRef len_field_ptr = LLVMBuildStructGEP(g->builder, cast_instruction->tmp_ptr,
3100 slice_len_index, "");
3101 LLVMValueRef len_value = LLVMConstInt(g->builtin_types.entry_usize->llvm_type,
3102 array_type->data.array.len, false);
3103 gen_store_untyped(g, len_value, len_field_ptr, 0, false);
3104
3105 return cast_instruction->tmp_ptr;
3106 }
31073105 }
31083106 zig_unreachable();
31093107}
31103108
3109static LLVMValueRef ir_render_ptr_of_array_to_slice(CodeGen *g, IrExecutable *executable,
3110 IrInstructionPtrOfArrayToSlice *instruction)
3111{
3112 ZigType *actual_type = instruction->operand->value.type;
3113 LLVMValueRef expr_val = ir_llvm_value(g, instruction->operand);
3114 assert(expr_val);
3115
3116 LLVMValueRef result_loc = ir_llvm_value(g, instruction->result_loc);
3117
3118 assert(actual_type->id == ZigTypeIdPointer);
3119 ZigType *array_type = actual_type->data.pointer.child_type;
3120 assert(array_type->id == ZigTypeIdArray);
3121
3122 LLVMValueRef ptr_field_ptr = LLVMBuildStructGEP(g->builder, result_loc, slice_ptr_index, "");
3123 LLVMValueRef indices[] = {
3124 LLVMConstNull(g->builtin_types.entry_usize->llvm_type),
3125 LLVMConstInt(g->builtin_types.entry_usize->llvm_type, 0, false),
3126 };
3127 LLVMValueRef slice_start_ptr = LLVMBuildInBoundsGEP(g->builder, expr_val, indices, 2, "");
3128 gen_store_untyped(g, slice_start_ptr, ptr_field_ptr, 0, false);
3129
3130 LLVMValueRef len_field_ptr = LLVMBuildStructGEP(g->builder, result_loc, slice_len_index, "");
3131 LLVMValueRef len_value = LLVMConstInt(g->builtin_types.entry_usize->llvm_type,
3132 array_type->data.array.len, false);
3133 gen_store_untyped(g, len_value, len_field_ptr, 0, false);
3134
3135 return result_loc;
3136}
3137
31113138static LLVMValueRef ir_render_ptr_cast(CodeGen *g, IrExecutable *executable,
31123139 IrInstructionPtrCastGen *instruction)
31133140{
......@@ -3154,12 +3181,7 @@ static LLVMValueRef ir_render_bit_cast(CodeGen *g, IrExecutable *executable,
31543181 uint32_t alignment = get_abi_alignment(g, actual_type);
31553182 return gen_load_untyped(g, bitcasted_ptr, alignment, false, "");
31563183 } else {
3157 assert(instruction->tmp_ptr != nullptr);
3158 LLVMTypeRef wanted_ptr_type_ref = LLVMPointerType(get_llvm_type(g, actual_type), 0);
3159 LLVMValueRef bitcasted_ptr = LLVMBuildBitCast(g->builder, instruction->tmp_ptr, wanted_ptr_type_ref, "");
3160 uint32_t alignment = get_abi_alignment(g, wanted_type);
3161 gen_store_untyped(g, value, bitcasted_ptr, alignment, false);
3162 return instruction->tmp_ptr;
3184 zig_unreachable();
31633185 }
31643186}
31653187
......@@ -3345,31 +3367,13 @@ static LLVMValueRef ir_render_bool_not(CodeGen *g, IrExecutable *executable, IrI
33453367 return LLVMBuildICmp(g->builder, LLVMIntEQ, value, zero, "");
33463368}
33473369
3348static LLVMValueRef ir_render_decl_var(CodeGen *g, IrExecutable *executable,
3349 IrInstructionDeclVarGen *decl_var_instruction)
3350{
3351 ZigVar *var = decl_var_instruction->var;
3370static LLVMValueRef ir_render_decl_var(CodeGen *g, IrExecutable *executable, IrInstructionDeclVarGen *instruction) {
3371 ZigVar *var = instruction->var;
33523372
33533373 if (!type_has_bits(var->var_type))
33543374 return nullptr;
33553375
3356 if (var->ref_count == 0 && g->build_mode != BuildModeDebug)
3357 return nullptr;
3358
3359 IrInstruction *init_value = decl_var_instruction->init_value;
3360
3361 bool have_init_expr = !value_is_all_undef(&init_value->value);
3362
3363 if (have_init_expr) {
3364 ZigType *var_ptr_type = get_pointer_to_type_extra(g, var->var_type, false, false,
3365 PtrLenSingle, var->align_bytes, 0, 0, false);
3366 LLVMValueRef llvm_init_val = ir_llvm_value(g, init_value);
3367 gen_assign_raw(g, var->value_ref, var_ptr_type, llvm_init_val);
3368 } else if (ir_want_runtime_safety(g, &decl_var_instruction->base)) {
3369 uint32_t align_bytes = (var->align_bytes == 0) ? get_abi_alignment(g, var->var_type) : var->align_bytes;
3370 gen_undef_init(g, align_bytes, var->var_type, var->value_ref);
3371 }
3372
3376 var->value_ref = ir_llvm_value(g, instruction->var_ptr);
33733377 gen_var_debug_decl(g, var);
33743378 return nullptr;
33753379}
......@@ -3401,13 +3405,13 @@ static LLVMValueRef ir_render_load_ptr(CodeGen *g, IrExecutable *executable, IrI
34013405 LLVMValueRef shifted_value = LLVMBuildLShr(g->builder, containing_int, shift_amt_val, "");
34023406
34033407 if (handle_is_ptr(child_type)) {
3404 assert(instruction->tmp_ptr != nullptr);
3408 LLVMValueRef result_loc = ir_llvm_value(g, instruction->result_loc);
34053409 LLVMTypeRef same_size_int = LLVMIntType(size_in_bits);
34063410 LLVMValueRef truncated_int = LLVMBuildTrunc(g->builder, shifted_value, same_size_int, "");
3407 LLVMValueRef bitcasted_ptr = LLVMBuildBitCast(g->builder, instruction->tmp_ptr,
3411 LLVMValueRef bitcasted_ptr = LLVMBuildBitCast(g->builder, result_loc,
34083412 LLVMPointerType(same_size_int, 0), "");
34093413 LLVMBuildStore(g->builder, truncated_int, bitcasted_ptr);
3410 return instruction->tmp_ptr;
3414 return result_loc;
34113415 }
34123416
34133417 if (child_type->id == ZigTypeIdFloat) {
......@@ -3585,6 +3589,14 @@ static LLVMValueRef ir_render_var_ptr(CodeGen *g, IrExecutable *executable, IrIn
35853589 }
35863590}
35873591
3592static LLVMValueRef ir_render_return_ptr(CodeGen *g, IrExecutable *executable,
3593 IrInstructionReturnPtr *instruction)
3594{
3595 src_assert(g->cur_ret_ptr != nullptr || !type_has_bits(instruction->base.value.type),
3596 instruction->base.source_node);
3597 return g->cur_ret_ptr;
3598}
3599
35883600static LLVMValueRef ir_render_elem_ptr(CodeGen *g, IrExecutable *executable, IrInstructionElemPtr *instruction) {
35893601 LLVMValueRef array_ptr_ptr = ir_llvm_value(g, instruction->array_ptr);
35903602 ZigType *array_ptr_type = instruction->array_ptr->value.type;
......@@ -3736,7 +3748,7 @@ static void set_call_instr_sret(CodeGen *g, LLVMValueRef call_instr) {
37363748 LLVMAddCallSiteAttribute(call_instr, 1, sret_attr);
37373749}
37383750
3739static LLVMValueRef ir_render_call(CodeGen *g, IrExecutable *executable, IrInstructionCall *instruction) {
3751static LLVMValueRef ir_render_call(CodeGen *g, IrExecutable *executable, IrInstructionCallGen *instruction) {
37403752 LLVMValueRef fn_val;
37413753 ZigType *fn_type;
37423754 if (instruction->fn_entry) {
......@@ -3759,8 +3771,9 @@ static LLVMValueRef ir_render_call(CodeGen *g, IrExecutable *executable, IrInstr
37593771 bool prefix_arg_err_ret_stack = get_prefix_arg_err_ret_stack(g, fn_type_id);
37603772 bool is_var_args = fn_type_id->is_var_args;
37613773 ZigList<LLVMValueRef> gen_param_values = {};
3774 LLVMValueRef result_loc = instruction->result_loc ? ir_llvm_value(g, instruction->result_loc) : nullptr;
37623775 if (first_arg_ret) {
3763 gen_param_values.append(instruction->tmp_ptr);
3776 gen_param_values.append(result_loc);
37643777 }
37653778 if (prefix_arg_err_ret_stack) {
37663779 gen_param_values.append(get_cur_err_ret_trace_val(g, instruction->base.scope));
......@@ -3768,7 +3781,7 @@ static LLVMValueRef ir_render_call(CodeGen *g, IrExecutable *executable, IrInstr
37683781 if (instruction->is_async) {
37693782 gen_param_values.append(ir_llvm_value(g, instruction->async_allocator));
37703783
3771 LLVMValueRef err_val_ptr = LLVMBuildStructGEP(g->builder, instruction->tmp_ptr, err_union_err_index, "");
3784 LLVMValueRef err_val_ptr = LLVMBuildStructGEP(g->builder, result_loc, err_union_err_index, "");
37723785 gen_param_values.append(err_val_ptr);
37733786 }
37743787 FnWalk fn_walk = {};
......@@ -3811,9 +3824,9 @@ static LLVMValueRef ir_render_call(CodeGen *g, IrExecutable *executable, IrInstr
38113824
38123825
38133826 if (instruction->is_async) {
3814 LLVMValueRef payload_ptr = LLVMBuildStructGEP(g->builder, instruction->tmp_ptr, err_union_payload_index, "");
3827 LLVMValueRef payload_ptr = LLVMBuildStructGEP(g->builder, result_loc, err_union_payload_index, "");
38153828 LLVMBuildStore(g->builder, result, payload_ptr);
3816 return instruction->tmp_ptr;
3829 return result_loc;
38173830 }
38183831
38193832 if (src_return_type->id == ZigTypeIdUnreachable) {
......@@ -3822,11 +3835,11 @@ static LLVMValueRef ir_render_call(CodeGen *g, IrExecutable *executable, IrInstr
38223835 return nullptr;
38233836 } else if (first_arg_ret) {
38243837 set_call_instr_sret(g, result);
3825 return instruction->tmp_ptr;
3838 return result_loc;
38263839 } else if (handle_is_ptr(src_return_type)) {
3827 auto store_instr = LLVMBuildStore(g->builder, result, instruction->tmp_ptr);
3828 LLVMSetAlignment(store_instr, LLVMGetAlignment(instruction->tmp_ptr));
3829 return instruction->tmp_ptr;
3840 LLVMValueRef store_instr = LLVMBuildStore(g->builder, result, result_loc);
3841 LLVMSetAlignment(store_instr, LLVMGetAlignment(result_loc));
3842 return result_loc;
38303843 } else {
38313844 return result;
38323845 }
......@@ -3835,6 +3848,9 @@ static LLVMValueRef ir_render_call(CodeGen *g, IrExecutable *executable, IrInstr
38353848static LLVMValueRef ir_render_struct_field_ptr(CodeGen *g, IrExecutable *executable,
38363849 IrInstructionStructFieldPtr *instruction)
38373850{
3851 if (instruction->base.value.special != ConstValSpecialRuntime)
3852 return nullptr;
3853
38383854 LLVMValueRef struct_ptr = ir_llvm_value(g, instruction->struct_ptr);
38393855 // not necessarily a pointer. could be ZigTypeIdStruct
38403856 ZigType *struct_ptr_type = instruction->struct_ptr->value.type;
......@@ -3856,6 +3872,9 @@ static LLVMValueRef ir_render_struct_field_ptr(CodeGen *g, IrExecutable *executa
38563872static LLVMValueRef ir_render_union_field_ptr(CodeGen *g, IrExecutable *executable,
38573873 IrInstructionUnionFieldPtr *instruction)
38583874{
3875 if (instruction->base.value.special != ConstValSpecialRuntime)
3876 return nullptr;
3877
38593878 ZigType *union_ptr_type = instruction->union_ptr->value.type;
38603879 assert(union_ptr_type->id == ZigTypeIdPointer);
38613880 ZigType *union_type = union_ptr_type->data.pointer.child_type;
......@@ -3863,8 +3882,20 @@ static LLVMValueRef ir_render_union_field_ptr(CodeGen *g, IrExecutable *executab
38633882
38643883 TypeUnionField *field = instruction->field;
38653884
3866 if (!type_has_bits(field->type_entry))
3885 if (!type_has_bits(field->type_entry)) {
3886 if (union_type->data.unionation.gen_tag_index == SIZE_MAX) {
3887 return nullptr;
3888 }
3889 if (instruction->initializing) {
3890 LLVMValueRef union_ptr = ir_llvm_value(g, instruction->union_ptr);
3891 LLVMValueRef tag_field_ptr = LLVMBuildStructGEP(g->builder, union_ptr,
3892 union_type->data.unionation.gen_tag_index, "");
3893 LLVMValueRef tag_value = bigint_to_llvm_const(get_llvm_type(g, union_type->data.unionation.tag_type),
3894 &field->enum_field->value);
3895 gen_store_untyped(g, tag_value, tag_field_ptr, 0, false);
3896 }
38673897 return nullptr;
3898 }
38683899
38693900 LLVMValueRef union_ptr = ir_llvm_value(g, instruction->union_ptr);
38703901 LLVMTypeRef field_type_ref = LLVMPointerType(get_llvm_type(g, field->type_entry), 0);
......@@ -3875,7 +3906,12 @@ static LLVMValueRef ir_render_union_field_ptr(CodeGen *g, IrExecutable *executab
38753906 return bitcasted_union_field_ptr;
38763907 }
38773908
3878 if (ir_want_runtime_safety(g, &instruction->base)) {
3909 if (instruction->initializing) {
3910 LLVMValueRef tag_field_ptr = LLVMBuildStructGEP(g->builder, union_ptr, union_type->data.unionation.gen_tag_index, "");
3911 LLVMValueRef tag_value = bigint_to_llvm_const(get_llvm_type(g, union_type->data.unionation.tag_type),
3912 &field->enum_field->value);
3913 gen_store_untyped(g, tag_value, tag_field_ptr, 0, false);
3914 } else if (instruction->safety_check_on && ir_want_runtime_safety(g, &instruction->base)) {
38793915 LLVMValueRef tag_field_ptr = LLVMBuildStructGEP(g->builder, union_ptr, union_type->data.unionation.gen_tag_index, "");
38803916 LLVMValueRef tag_value = gen_load_untyped(g, tag_field_ptr, 0, false, "");
38813917
......@@ -4075,14 +4111,17 @@ static LLVMValueRef ir_render_test_non_null(CodeGen *g, IrExecutable *executable
40754111static LLVMValueRef ir_render_optional_unwrap_ptr(CodeGen *g, IrExecutable *executable,
40764112 IrInstructionOptionalUnwrapPtr *instruction)
40774113{
4114 if (instruction->base.value.special != ConstValSpecialRuntime)
4115 return nullptr;
4116
40784117 ZigType *ptr_type = instruction->base_ptr->value.type;
40794118 assert(ptr_type->id == ZigTypeIdPointer);
40804119 ZigType *maybe_type = ptr_type->data.pointer.child_type;
40814120 assert(maybe_type->id == ZigTypeIdOptional);
40824121 ZigType *child_type = maybe_type->data.maybe.child_type;
4083 LLVMValueRef maybe_ptr = ir_llvm_value(g, instruction->base_ptr);
4084 if (ir_want_runtime_safety(g, &instruction->base) && instruction->safety_check_on) {
4085 LLVMValueRef maybe_handle = get_handle_value(g, maybe_ptr, maybe_type, ptr_type);
4122 LLVMValueRef base_ptr = ir_llvm_value(g, instruction->base_ptr);
4123 if (instruction->safety_check_on && ir_want_runtime_safety(g, &instruction->base)) {
4124 LLVMValueRef maybe_handle = get_handle_value(g, base_ptr, maybe_type, ptr_type);
40864125 LLVMValueRef non_null_bit = gen_non_null_bit(g, maybe_type, maybe_handle);
40874126 LLVMBasicBlockRef fail_block = LLVMAppendBasicBlock(g->cur_fn_val, "UnwrapOptionalFail");
40884127 LLVMBasicBlockRef ok_block = LLVMAppendBasicBlock(g->cur_fn_val, "UnwrapOptionalOk");
......@@ -4098,10 +4137,16 @@ static LLVMValueRef ir_render_optional_unwrap_ptr(CodeGen *g, IrExecutable *exec
40984137 } else {
40994138 bool is_scalar = !handle_is_ptr(maybe_type);
41004139 if (is_scalar) {
4101 return maybe_ptr;
4140 return base_ptr;
41024141 } else {
4103 LLVMValueRef maybe_struct_ref = get_handle_value(g, maybe_ptr, maybe_type, ptr_type);
4104 return LLVMBuildStructGEP(g->builder, maybe_struct_ref, maybe_child_index, "");
4142 LLVMValueRef optional_struct_ref = get_handle_value(g, base_ptr, maybe_type, ptr_type);
4143 if (instruction->initializing) {
4144 LLVMValueRef non_null_bit_ptr = LLVMBuildStructGEP(g->builder, optional_struct_ref,
4145 maybe_null_index, "");
4146 LLVMValueRef non_null_bit = LLVMConstInt(LLVMInt1Type(), 1, false);
4147 gen_store_untyped(g, non_null_bit, non_null_bit_ptr, 0, false);
4148 }
4149 return LLVMBuildStructGEP(g->builder, optional_struct_ref, maybe_child_index, "");
41054150 }
41064151 }
41074152}
......@@ -4224,17 +4269,17 @@ static LLVMValueRef ir_render_phi(CodeGen *g, IrExecutable *executable, IrInstru
42244269 return phi;
42254270}
42264271
4227static LLVMValueRef ir_render_ref(CodeGen *g, IrExecutable *executable, IrInstructionRef *instruction) {
4272static LLVMValueRef ir_render_ref(CodeGen *g, IrExecutable *executable, IrInstructionRefGen *instruction) {
42284273 if (!type_has_bits(instruction->base.value.type)) {
42294274 return nullptr;
42304275 }
4231 LLVMValueRef value = ir_llvm_value(g, instruction->value);
4232 if (handle_is_ptr(instruction->value->value.type)) {
4276 LLVMValueRef value = ir_llvm_value(g, instruction->operand);
4277 if (handle_is_ptr(instruction->operand->value.type)) {
42334278 return value;
42344279 } else {
4235 assert(instruction->tmp_ptr);
4236 gen_store_untyped(g, value, instruction->tmp_ptr, 0, false);
4237 return instruction->tmp_ptr;
4280 LLVMValueRef result_loc = ir_llvm_value(g, instruction->result_loc);
4281 gen_store_untyped(g, value, result_loc, 0, false);
4282 return result_loc;
42384283 }
42394284}
42404285
......@@ -4350,7 +4395,9 @@ static LLVMValueRef get_enum_tag_name_function(CodeGen *g, ZigType *enum_type) {
43504395 g->cur_fn = prev_cur_fn;
43514396 g->cur_fn_val = prev_cur_fn_val;
43524397 LLVMPositionBuilderAtEnd(g->builder, prev_block);
4353 LLVMSetCurrentDebugLocation(g->builder, prev_debug_location);
4398 if (!g->strip_debug_symbols) {
4399 LLVMSetCurrentDebugLocation(g->builder, prev_debug_location);
4400 }
43544401
43554402 enum_type->data.enumeration.name_function = fn_val;
43564403 return fn_val;
......@@ -4526,28 +4573,28 @@ static LLVMValueRef ir_render_cmpxchg(CodeGen *g, IrExecutable *executable, IrIn
45264573 LLVMValueRef result_val = ZigLLVMBuildCmpXchg(g->builder, ptr_val, cmp_val, new_val,
45274574 success_order, failure_order, instruction->is_weak);
45284575
4529 ZigType *maybe_type = instruction->base.value.type;
4530 assert(maybe_type->id == ZigTypeIdOptional);
4531 ZigType *child_type = maybe_type->data.maybe.child_type;
4576 ZigType *optional_type = instruction->base.value.type;
4577 assert(optional_type->id == ZigTypeIdOptional);
4578 ZigType *child_type = optional_type->data.maybe.child_type;
45324579
4533 if (!handle_is_ptr(maybe_type)) {
4580 if (!handle_is_ptr(optional_type)) {
45344581 LLVMValueRef payload_val = LLVMBuildExtractValue(g->builder, result_val, 0, "");
45354582 LLVMValueRef success_bit = LLVMBuildExtractValue(g->builder, result_val, 1, "");
45364583 return LLVMBuildSelect(g->builder, success_bit, LLVMConstNull(get_llvm_type(g, child_type)), payload_val, "");
45374584 }
45384585
4539 assert(instruction->tmp_ptr != nullptr);
4586 LLVMValueRef result_loc = ir_llvm_value(g, instruction->result_loc);
45404587 assert(type_has_bits(child_type));
45414588
45424589 LLVMValueRef payload_val = LLVMBuildExtractValue(g->builder, result_val, 0, "");
4543 LLVMValueRef val_ptr = LLVMBuildStructGEP(g->builder, instruction->tmp_ptr, maybe_child_index, "");
4590 LLVMValueRef val_ptr = LLVMBuildStructGEP(g->builder, result_loc, maybe_child_index, "");
45444591 gen_assign_raw(g, val_ptr, get_pointer_to_type(g, child_type, false), payload_val);
45454592
45464593 LLVMValueRef success_bit = LLVMBuildExtractValue(g->builder, result_val, 1, "");
45474594 LLVMValueRef nonnull_bit = LLVMBuildNot(g->builder, success_bit, "");
4548 LLVMValueRef maybe_ptr = LLVMBuildStructGEP(g->builder, instruction->tmp_ptr, maybe_null_index, "");
4595 LLVMValueRef maybe_ptr = LLVMBuildStructGEP(g->builder, result_loc, maybe_null_index, "");
45494596 gen_store_untyped(g, nonnull_bit, maybe_ptr, 0, false);
4550 return instruction->tmp_ptr;
4597 return result_loc;
45514598}
45524599
45534600static LLVMValueRef ir_render_fence(CodeGen *g, IrExecutable *executable, IrInstructionFence *instruction) {
......@@ -4619,16 +4666,14 @@ static LLVMValueRef ir_render_memcpy(CodeGen *g, IrExecutable *executable, IrIns
46194666 return nullptr;
46204667}
46214668
4622static LLVMValueRef ir_render_slice(CodeGen *g, IrExecutable *executable, IrInstructionSlice *instruction) {
4623 assert(instruction->tmp_ptr);
4624
4669static LLVMValueRef ir_render_slice(CodeGen *g, IrExecutable *executable, IrInstructionSliceGen *instruction) {
46254670 LLVMValueRef array_ptr_ptr = ir_llvm_value(g, instruction->ptr);
46264671 ZigType *array_ptr_type = instruction->ptr->value.type;
46274672 assert(array_ptr_type->id == ZigTypeIdPointer);
46284673 ZigType *array_type = array_ptr_type->data.pointer.child_type;
46294674 LLVMValueRef array_ptr = get_handle_value(g, array_ptr_ptr, array_type, array_ptr_type);
46304675
4631 LLVMValueRef tmp_struct_ptr = instruction->tmp_ptr;
4676 LLVMValueRef tmp_struct_ptr = ir_llvm_value(g, instruction->result_loc);
46324677
46334678 bool want_runtime_safety = instruction->safety_check_on && ir_want_runtime_safety(g, &instruction->base);
46344679
......@@ -4646,7 +4691,9 @@ static LLVMValueRef ir_render_slice(CodeGen *g, IrExecutable *executable, IrInst
46464691 end_val = LLVMConstInt(g->builtin_types.entry_usize->llvm_type, array_type->data.array.len, false);
46474692 }
46484693 if (want_runtime_safety) {
4649 add_bounds_check(g, start_val, LLVMIntEQ, nullptr, LLVMIntULE, end_val);
4694 if (instruction->start->value.special == ConstValSpecialRuntime || instruction->end) {
4695 add_bounds_check(g, start_val, LLVMIntEQ, nullptr, LLVMIntULE, end_val);
4696 }
46504697 if (instruction->end) {
46514698 LLVMValueRef array_end = LLVMConstInt(g->builtin_types.entry_usize->llvm_type,
46524699 array_type->data.array.len, false);
......@@ -4877,10 +4924,10 @@ static LLVMValueRef ir_render_overflow_op(CodeGen *g, IrExecutable *executable,
48774924 return overflow_bit;
48784925}
48794926
4880static LLVMValueRef ir_render_test_err(CodeGen *g, IrExecutable *executable, IrInstructionTestErr *instruction) {
4881 ZigType *err_union_type = instruction->value->value.type;
4927static LLVMValueRef ir_render_test_err(CodeGen *g, IrExecutable *executable, IrInstructionTestErrGen *instruction) {
4928 ZigType *err_union_type = instruction->err_union->value.type;
48824929 ZigType *payload_type = err_union_type->data.error_union.payload_type;
4883 LLVMValueRef err_union_handle = ir_llvm_value(g, instruction->value);
4930 LLVMValueRef err_union_handle = ir_llvm_value(g, instruction->err_union);
48844931
48854932 LLVMValueRef err_val;
48864933 if (type_has_bits(payload_type)) {
......@@ -4897,25 +4944,30 @@ static LLVMValueRef ir_render_test_err(CodeGen *g, IrExecutable *executable, IrI
48974944static LLVMValueRef ir_render_unwrap_err_code(CodeGen *g, IrExecutable *executable,
48984945 IrInstructionUnwrapErrCode *instruction)
48994946{
4900 ZigType *ptr_type = instruction->err_union->value.type;
4947 if (instruction->base.value.special != ConstValSpecialRuntime)
4948 return nullptr;
4949
4950 ZigType *ptr_type = instruction->err_union_ptr->value.type;
49014951 assert(ptr_type->id == ZigTypeIdPointer);
49024952 ZigType *err_union_type = ptr_type->data.pointer.child_type;
49034953 ZigType *payload_type = err_union_type->data.error_union.payload_type;
4904 LLVMValueRef err_union_ptr = ir_llvm_value(g, instruction->err_union);
4905 LLVMValueRef err_union_handle = get_handle_value(g, err_union_ptr, err_union_type, ptr_type);
4906
4907 if (type_has_bits(payload_type)) {
4908 LLVMValueRef err_val_ptr = LLVMBuildStructGEP(g->builder, err_union_handle, err_union_err_index, "");
4909 return gen_load_untyped(g, err_val_ptr, 0, false, "");
4954 LLVMValueRef err_union_ptr = ir_llvm_value(g, instruction->err_union_ptr);
4955 if (!type_has_bits(payload_type)) {
4956 return err_union_ptr;
49104957 } else {
4911 return err_union_handle;
4958 // TODO assign undef to the payload
4959 LLVMValueRef err_union_handle = get_handle_value(g, err_union_ptr, err_union_type, ptr_type);
4960 return LLVMBuildStructGEP(g->builder, err_union_handle, err_union_err_index, "");
49124961 }
49134962}
49144963
49154964static LLVMValueRef ir_render_unwrap_err_payload(CodeGen *g, IrExecutable *executable,
49164965 IrInstructionUnwrapErrPayload *instruction)
49174966{
4918 bool want_safety = ir_want_runtime_safety(g, &instruction->base) && instruction->safety_check_on &&
4967 if (instruction->base.value.special != ConstValSpecialRuntime)
4968 return nullptr;
4969
4970 bool want_safety = instruction->safety_check_on && ir_want_runtime_safety(g, &instruction->base) &&
49194971 g->errors_by_index.length > 1;
49204972 if (!want_safety && !type_has_bits(instruction->base.value.type))
49214973 return nullptr;
......@@ -4951,13 +5003,18 @@ static LLVMValueRef ir_render_unwrap_err_payload(CodeGen *g, IrExecutable *execu
49515003 }
49525004
49535005 if (type_has_bits(payload_type)) {
5006 if (instruction->initializing) {
5007 LLVMValueRef err_tag_ptr = LLVMBuildStructGEP(g->builder, err_union_handle, err_union_err_index, "");
5008 LLVMValueRef ok_err_val = LLVMConstNull(get_llvm_type(g, g->err_tag_type));
5009 gen_store_untyped(g, ok_err_val, err_tag_ptr, 0, false);
5010 }
49545011 return LLVMBuildStructGEP(g->builder, err_union_handle, err_union_payload_index, "");
49555012 } else {
49565013 return nullptr;
49575014 }
49585015}
49595016
4960static LLVMValueRef ir_render_maybe_wrap(CodeGen *g, IrExecutable *executable, IrInstructionOptionalWrap *instruction) {
5017static LLVMValueRef ir_render_optional_wrap(CodeGen *g, IrExecutable *executable, IrInstructionOptionalWrap *instruction) {
49615018 ZigType *wanted_type = instruction->base.value.type;
49625019
49635020 assert(wanted_type->id == ZigTypeIdOptional);
......@@ -4965,23 +5022,32 @@ static LLVMValueRef ir_render_maybe_wrap(CodeGen *g, IrExecutable *executable, I
49655022 ZigType *child_type = wanted_type->data.maybe.child_type;
49665023
49675024 if (!type_has_bits(child_type)) {
4968 return LLVMConstInt(LLVMInt1Type(), 1, false);
5025 LLVMValueRef result = LLVMConstAllOnes(LLVMInt1Type());
5026 if (instruction->result_loc != nullptr) {
5027 LLVMValueRef result_loc = ir_llvm_value(g, instruction->result_loc);
5028 gen_store_untyped(g, result, result_loc, 0, false);
5029 }
5030 return result;
49695031 }
49705032
4971 LLVMValueRef payload_val = ir_llvm_value(g, instruction->value);
5033 LLVMValueRef payload_val = ir_llvm_value(g, instruction->operand);
49725034 if (!handle_is_ptr(wanted_type)) {
5035 if (instruction->result_loc != nullptr) {
5036 LLVMValueRef result_loc = ir_llvm_value(g, instruction->result_loc);
5037 gen_store_untyped(g, payload_val, result_loc, 0, false);
5038 }
49735039 return payload_val;
49745040 }
49755041
4976 assert(instruction->tmp_ptr);
5042 LLVMValueRef result_loc = ir_llvm_value(g, instruction->result_loc);
49775043
4978 LLVMValueRef val_ptr = LLVMBuildStructGEP(g->builder, instruction->tmp_ptr, maybe_child_index, "");
5044 LLVMValueRef val_ptr = LLVMBuildStructGEP(g->builder, result_loc, maybe_child_index, "");
49795045 // child_type and instruction->value->value.type may differ by constness
49805046 gen_assign_raw(g, val_ptr, get_pointer_to_type(g, child_type, false), payload_val);
4981 LLVMValueRef maybe_ptr = LLVMBuildStructGEP(g->builder, instruction->tmp_ptr, maybe_null_index, "");
5047 LLVMValueRef maybe_ptr = LLVMBuildStructGEP(g->builder, result_loc, maybe_null_index, "");
49825048 gen_store_untyped(g, LLVMConstAllOnes(LLVMInt1Type()), maybe_ptr, 0, false);
49835049
4984 return instruction->tmp_ptr;
5050 return result_loc;
49855051}
49865052
49875053static LLVMValueRef ir_render_err_wrap_code(CodeGen *g, IrExecutable *executable, IrInstructionErrWrapCode *instruction) {
......@@ -4989,20 +5055,19 @@ static LLVMValueRef ir_render_err_wrap_code(CodeGen *g, IrExecutable *executable
49895055
49905056 assert(wanted_type->id == ZigTypeIdErrorUnion);
49915057
4992 ZigType *payload_type = wanted_type->data.error_union.payload_type;
4993 ZigType *err_set_type = wanted_type->data.error_union.err_set_type;
4994
4995 LLVMValueRef err_val = ir_llvm_value(g, instruction->value);
5058 LLVMValueRef err_val = ir_llvm_value(g, instruction->operand);
49965059
4997 if (!type_has_bits(payload_type) || !type_has_bits(err_set_type))
5060 if (!handle_is_ptr(wanted_type))
49985061 return err_val;
49995062
5000 assert(instruction->tmp_ptr);
5063 LLVMValueRef result_loc = ir_llvm_value(g, instruction->result_loc);
50015064
5002 LLVMValueRef err_tag_ptr = LLVMBuildStructGEP(g->builder, instruction->tmp_ptr, err_union_err_index, "");
5065 LLVMValueRef err_tag_ptr = LLVMBuildStructGEP(g->builder, result_loc, err_union_err_index, "");
50035066 gen_store_untyped(g, err_val, err_tag_ptr, 0, false);
50045067
5005 return instruction->tmp_ptr;
5068 // TODO store undef to the payload
5069
5070 return result_loc;
50065071}
50075072
50085073static LLVMValueRef ir_render_err_wrap_payload(CodeGen *g, IrExecutable *executable, IrInstructionErrWrapPayload *instruction) {
......@@ -5014,7 +5079,7 @@ static LLVMValueRef ir_render_err_wrap_payload(CodeGen *g, IrExecutable *executa
50145079 ZigType *err_set_type = wanted_type->data.error_union.err_set_type;
50155080
50165081 if (!type_has_bits(err_set_type)) {
5017 return ir_llvm_value(g, instruction->value);
5082 return ir_llvm_value(g, instruction->operand);
50185083 }
50195084
50205085 LLVMValueRef ok_err_val = LLVMConstNull(get_llvm_type(g, g->err_tag_type));
......@@ -5022,17 +5087,18 @@ static LLVMValueRef ir_render_err_wrap_payload(CodeGen *g, IrExecutable *executa
50225087 if (!type_has_bits(payload_type))
50235088 return ok_err_val;
50245089
5025 assert(instruction->tmp_ptr);
50265090
5027 LLVMValueRef payload_val = ir_llvm_value(g, instruction->value);
5091 LLVMValueRef result_loc = ir_llvm_value(g, instruction->result_loc);
50285092
5029 LLVMValueRef err_tag_ptr = LLVMBuildStructGEP(g->builder, instruction->tmp_ptr, err_union_err_index, "");
5093 LLVMValueRef payload_val = ir_llvm_value(g, instruction->operand);
5094
5095 LLVMValueRef err_tag_ptr = LLVMBuildStructGEP(g->builder, result_loc, err_union_err_index, "");
50305096 gen_store_untyped(g, ok_err_val, err_tag_ptr, 0, false);
50315097
5032 LLVMValueRef payload_ptr = LLVMBuildStructGEP(g->builder, instruction->tmp_ptr, err_union_payload_index, "");
5098 LLVMValueRef payload_ptr = LLVMBuildStructGEP(g->builder, result_loc, err_union_payload_index, "");
50335099 gen_assign_raw(g, payload_ptr, get_pointer_to_type(g, payload_type, false), payload_val);
50345100
5035 return instruction->tmp_ptr;
5101 return result_loc;
50365102}
50375103
50385104static LLVMValueRef ir_render_union_tag(CodeGen *g, IrExecutable *executable, IrInstructionUnionTag *instruction) {
......@@ -5053,90 +5119,6 @@ static LLVMValueRef ir_render_union_tag(CodeGen *g, IrExecutable *executable, Ir
50535119 return get_handle_value(g, tag_field_ptr, tag_type, ptr_type);
50545120}
50555121
5056static LLVMValueRef ir_render_struct_init(CodeGen *g, IrExecutable *executable, IrInstructionStructInit *instruction) {
5057 for (size_t i = 0; i < instruction->field_count; i += 1) {
5058 IrInstructionStructInitField *field = &instruction->fields[i];
5059 TypeStructField *type_struct_field = field->type_struct_field;
5060 if (!type_has_bits(type_struct_field->type_entry))
5061 continue;
5062
5063 LLVMValueRef field_ptr = LLVMBuildStructGEP(g->builder, instruction->tmp_ptr,
5064 (unsigned)type_struct_field->gen_index, "");
5065 LLVMValueRef value = ir_llvm_value(g, field->value);
5066
5067 uint32_t field_align_bytes = get_abi_alignment(g, type_struct_field->type_entry);
5068 uint32_t host_int_bytes = get_host_int_bytes(g, instruction->struct_type, type_struct_field);
5069
5070 ZigType *ptr_type = get_pointer_to_type_extra(g, type_struct_field->type_entry,
5071 false, false, PtrLenSingle, field_align_bytes,
5072 (uint32_t)type_struct_field->bit_offset_in_host, host_int_bytes, false);
5073
5074 gen_assign_raw(g, field_ptr, ptr_type, value);
5075 }
5076 return instruction->tmp_ptr;
5077}
5078
5079static LLVMValueRef ir_render_union_init(CodeGen *g, IrExecutable *executable, IrInstructionUnionInit *instruction) {
5080 TypeUnionField *type_union_field = instruction->field;
5081
5082 if (!type_has_bits(type_union_field->type_entry))
5083 return nullptr;
5084
5085 uint32_t field_align_bytes = get_abi_alignment(g, type_union_field->type_entry);
5086 ZigType *ptr_type = get_pointer_to_type_extra(g, type_union_field->type_entry,
5087 false, false, PtrLenSingle, field_align_bytes,
5088 0, 0, false);
5089
5090 LLVMValueRef uncasted_union_ptr;
5091 // Even if safety is off in this block, if the union type has the safety field, we have to populate it
5092 // correctly. Otherwise safety code somewhere other than here could fail.
5093 ZigType *union_type = instruction->union_type;
5094 if (union_type->data.unionation.gen_tag_index != SIZE_MAX) {
5095 LLVMValueRef tag_field_ptr = LLVMBuildStructGEP(g->builder, instruction->tmp_ptr,
5096 union_type->data.unionation.gen_tag_index, "");
5097
5098 LLVMValueRef tag_value = bigint_to_llvm_const(get_llvm_type(g, union_type->data.unionation.tag_type),
5099 &type_union_field->enum_field->value);
5100 gen_store_untyped(g, tag_value, tag_field_ptr, 0, false);
5101
5102 uncasted_union_ptr = LLVMBuildStructGEP(g->builder, instruction->tmp_ptr,
5103 (unsigned)union_type->data.unionation.gen_union_index, "");
5104 } else {
5105 uncasted_union_ptr = LLVMBuildStructGEP(g->builder, instruction->tmp_ptr, (unsigned)0, "");
5106 }
5107
5108 LLVMValueRef field_ptr = LLVMBuildBitCast(g->builder, uncasted_union_ptr, get_llvm_type(g, ptr_type), "");
5109 LLVMValueRef value = ir_llvm_value(g, instruction->init_value);
5110
5111 gen_assign_raw(g, field_ptr, ptr_type, value);
5112
5113 return instruction->tmp_ptr;
5114}
5115
5116static LLVMValueRef ir_render_container_init_list(CodeGen *g, IrExecutable *executable,
5117 IrInstructionContainerInitList *instruction)
5118{
5119 ZigType *array_type = instruction->base.value.type;
5120 assert(array_type->id == ZigTypeIdArray);
5121 LLVMValueRef tmp_array_ptr = instruction->tmp_ptr;
5122 assert(tmp_array_ptr);
5123
5124 size_t field_count = instruction->item_count;
5125
5126 ZigType *child_type = array_type->data.array.child_type;
5127 for (size_t i = 0; i < field_count; i += 1) {
5128 LLVMValueRef elem_val = ir_llvm_value(g, instruction->items[i]);
5129 LLVMValueRef indices[] = {
5130 LLVMConstNull(g->builtin_types.entry_usize->llvm_type),
5131 LLVMConstInt(g->builtin_types.entry_usize->llvm_type, i, false),
5132 };
5133 LLVMValueRef elem_ptr = LLVMBuildInBoundsGEP(g->builder, tmp_array_ptr, indices, 2, "");
5134 gen_assign_raw(g, elem_ptr, get_pointer_to_type(g, child_type, false), elem_val);
5135 }
5136
5137 return tmp_array_ptr;
5138}
5139
51405122static LLVMValueRef ir_render_panic(CodeGen *g, IrExecutable *executable, IrInstructionPanic *instruction) {
51415123 gen_panic(g, ir_llvm_value(g, instruction->msg), get_cur_err_ret_trace_val(g, instruction->base.scope));
51425124 return nullptr;
......@@ -5353,7 +5335,9 @@ static LLVMValueRef get_coro_alloc_helper_fn_val(CodeGen *g, LLVMTypeRef alloc_f
53535335 g->cur_fn = prev_cur_fn;
53545336 g->cur_fn_val = prev_cur_fn_val;
53555337 LLVMPositionBuilderAtEnd(g->builder, prev_block);
5356 LLVMSetCurrentDebugLocation(g->builder, prev_debug_location);
5338 if (!g->strip_debug_symbols) {
5339 LLVMSetCurrentDebugLocation(g->builder, prev_debug_location);
5340 }
53575341
53585342 g->coro_alloc_helper_fn_val = fn_val;
53595343 return fn_val;
......@@ -5499,12 +5483,12 @@ static LLVMValueRef ir_render_vector_to_array(CodeGen *g, IrExecutable *executab
54995483 ZigType *array_type = instruction->base.value.type;
55005484 assert(array_type->id == ZigTypeIdArray);
55015485 assert(handle_is_ptr(array_type));
5502 assert(instruction->tmp_ptr);
5486 LLVMValueRef result_loc = ir_llvm_value(g, instruction->result_loc);
55035487 LLVMValueRef vector = ir_llvm_value(g, instruction->vector);
5504 LLVMValueRef casted_ptr = LLVMBuildBitCast(g->builder, instruction->tmp_ptr,
5488 LLVMValueRef casted_ptr = LLVMBuildBitCast(g->builder, result_loc,
55055489 LLVMPointerType(get_llvm_type(g, instruction->vector->value.type), 0), "");
5506 gen_store_untyped(g, vector, casted_ptr, 0, false);
5507 return instruction->tmp_ptr;
5490 gen_store_untyped(g, vector, casted_ptr, get_ptr_align(g, instruction->result_loc->value.type), false);
5491 return result_loc;
55085492}
55095493
55105494static LLVMValueRef ir_render_array_to_vector(CodeGen *g, IrExecutable *executable,
......@@ -5567,14 +5551,10 @@ static void set_debug_location(CodeGen *g, IrInstruction *instruction) {
55675551}
55685552
55695553static LLVMValueRef ir_render_instruction(CodeGen *g, IrExecutable *executable, IrInstruction *instruction) {
5570 set_debug_location(g, instruction);
5571
55725554 switch (instruction->id) {
55735555 case IrInstructionIdInvalid:
55745556 case IrInstructionIdConst:
55755557 case IrInstructionIdTypeOf:
5576 case IrInstructionIdToPtrType:
5577 case IrInstructionIdPtrTypeChild:
55785558 case IrInstructionIdFieldPtr:
55795559 case IrInstructionIdSetCold:
55805560 case IrInstructionIdSetRuntimeSafety:
......@@ -5636,10 +5616,22 @@ static LLVMValueRef ir_render_instruction(CodeGen *g, IrExecutable *executable,
56365616 case IrInstructionIdPtrCastSrc:
56375617 case IrInstructionIdCmpxchgSrc:
56385618 case IrInstructionIdLoadPtr:
5639 case IrInstructionIdBitCast:
56405619 case IrInstructionIdGlobalAsm:
56415620 case IrInstructionIdHasDecl:
56425621 case IrInstructionIdUndeclaredIdent:
5622 case IrInstructionIdCallSrc:
5623 case IrInstructionIdAllocaSrc:
5624 case IrInstructionIdEndExpr:
5625 case IrInstructionIdAllocaGen:
5626 case IrInstructionIdImplicitCast:
5627 case IrInstructionIdResolveResult:
5628 case IrInstructionIdResetResult:
5629 case IrInstructionIdResultPtr:
5630 case IrInstructionIdContainerInitList:
5631 case IrInstructionIdSliceSrc:
5632 case IrInstructionIdRef:
5633 case IrInstructionIdBitCastSrc:
5634 case IrInstructionIdTestErrSrc:
56435635 zig_unreachable();
56445636
56455637 case IrInstructionIdDeclVarGen:
......@@ -5664,10 +5656,12 @@ static LLVMValueRef ir_render_instruction(CodeGen *g, IrExecutable *executable,
56645656 return ir_render_store_ptr(g, executable, (IrInstructionStorePtr *)instruction);
56655657 case IrInstructionIdVarPtr:
56665658 return ir_render_var_ptr(g, executable, (IrInstructionVarPtr *)instruction);
5659 case IrInstructionIdReturnPtr:
5660 return ir_render_return_ptr(g, executable, (IrInstructionReturnPtr *)instruction);
56675661 case IrInstructionIdElemPtr:
56685662 return ir_render_elem_ptr(g, executable, (IrInstructionElemPtr *)instruction);
5669 case IrInstructionIdCall:
5670 return ir_render_call(g, executable, (IrInstructionCall *)instruction);
5663 case IrInstructionIdCallGen:
5664 return ir_render_call(g, executable, (IrInstructionCallGen *)instruction);
56715665 case IrInstructionIdStructFieldPtr:
56725666 return ir_render_struct_field_ptr(g, executable, (IrInstructionStructFieldPtr *)instruction);
56735667 case IrInstructionIdUnionFieldPtr:
......@@ -5692,8 +5686,8 @@ static LLVMValueRef ir_render_instruction(CodeGen *g, IrExecutable *executable,
56925686 return ir_render_bit_reverse(g, executable, (IrInstructionBitReverse *)instruction);
56935687 case IrInstructionIdPhi:
56945688 return ir_render_phi(g, executable, (IrInstructionPhi *)instruction);
5695 case IrInstructionIdRef:
5696 return ir_render_ref(g, executable, (IrInstructionRef *)instruction);
5689 case IrInstructionIdRefGen:
5690 return ir_render_ref(g, executable, (IrInstructionRefGen *)instruction);
56975691 case IrInstructionIdErrName:
56985692 return ir_render_err_name(g, executable, (IrInstructionErrName *)instruction);
56995693 case IrInstructionIdCmpxchgGen:
......@@ -5708,8 +5702,8 @@ static LLVMValueRef ir_render_instruction(CodeGen *g, IrExecutable *executable,
57085702 return ir_render_memset(g, executable, (IrInstructionMemset *)instruction);
57095703 case IrInstructionIdMemcpy:
57105704 return ir_render_memcpy(g, executable, (IrInstructionMemcpy *)instruction);
5711 case IrInstructionIdSlice:
5712 return ir_render_slice(g, executable, (IrInstructionSlice *)instruction);
5705 case IrInstructionIdSliceGen:
5706 return ir_render_slice(g, executable, (IrInstructionSliceGen *)instruction);
57135707 case IrInstructionIdBreakpoint:
57145708 return ir_render_breakpoint(g, executable, (IrInstructionBreakpoint *)instruction);
57155709 case IrInstructionIdReturnAddress:
......@@ -5720,24 +5714,20 @@ static LLVMValueRef ir_render_instruction(CodeGen *g, IrExecutable *executable,
57205714 return ir_render_handle(g, executable, (IrInstructionHandle *)instruction);
57215715 case IrInstructionIdOverflowOp:
57225716 return ir_render_overflow_op(g, executable, (IrInstructionOverflowOp *)instruction);
5723 case IrInstructionIdTestErr:
5724 return ir_render_test_err(g, executable, (IrInstructionTestErr *)instruction);
5717 case IrInstructionIdTestErrGen:
5718 return ir_render_test_err(g, executable, (IrInstructionTestErrGen *)instruction);
57255719 case IrInstructionIdUnwrapErrCode:
57265720 return ir_render_unwrap_err_code(g, executable, (IrInstructionUnwrapErrCode *)instruction);
57275721 case IrInstructionIdUnwrapErrPayload:
57285722 return ir_render_unwrap_err_payload(g, executable, (IrInstructionUnwrapErrPayload *)instruction);
57295723 case IrInstructionIdOptionalWrap:
5730 return ir_render_maybe_wrap(g, executable, (IrInstructionOptionalWrap *)instruction);
5724 return ir_render_optional_wrap(g, executable, (IrInstructionOptionalWrap *)instruction);
57315725 case IrInstructionIdErrWrapCode:
57325726 return ir_render_err_wrap_code(g, executable, (IrInstructionErrWrapCode *)instruction);
57335727 case IrInstructionIdErrWrapPayload:
57345728 return ir_render_err_wrap_payload(g, executable, (IrInstructionErrWrapPayload *)instruction);
57355729 case IrInstructionIdUnionTag:
57365730 return ir_render_union_tag(g, executable, (IrInstructionUnionTag *)instruction);
5737 case IrInstructionIdStructInit:
5738 return ir_render_struct_init(g, executable, (IrInstructionStructInit *)instruction);
5739 case IrInstructionIdUnionInit:
5740 return ir_render_union_init(g, executable, (IrInstructionUnionInit *)instruction);
57415731 case IrInstructionIdPtrCastGen:
57425732 return ir_render_ptr_cast(g, executable, (IrInstructionPtrCastGen *)instruction);
57435733 case IrInstructionIdBitCastGen:
......@@ -5754,8 +5744,6 @@ static LLVMValueRef ir_render_instruction(CodeGen *g, IrExecutable *executable,
57545744 return ir_render_int_to_err(g, executable, (IrInstructionIntToErr *)instruction);
57555745 case IrInstructionIdErrToInt:
57565746 return ir_render_err_to_int(g, executable, (IrInstructionErrToInt *)instruction);
5757 case IrInstructionIdContainerInitList:
5758 return ir_render_container_init_list(g, executable, (IrInstructionContainerInitList *)instruction);
57595747 case IrInstructionIdPanic:
57605748 return ir_render_panic(g, executable, (IrInstructionPanic *)instruction);
57615749 case IrInstructionIdTagName:
......@@ -5818,6 +5806,8 @@ static LLVMValueRef ir_render_instruction(CodeGen *g, IrExecutable *executable,
58185806 return ir_render_assert_non_null(g, executable, (IrInstructionAssertNonNull *)instruction);
58195807 case IrInstructionIdResizeSlice:
58205808 return ir_render_resize_slice(g, executable, (IrInstructionResizeSlice *)instruction);
5809 case IrInstructionIdPtrOfArrayToSlice:
5810 return ir_render_ptr_of_array_to_slice(g, executable, (IrInstructionPtrOfArrayToSlice *)instruction);
58215811 }
58225812 zig_unreachable();
58235813}
......@@ -5829,7 +5819,6 @@ static void ir_render(CodeGen *g, ZigFn *fn_entry) {
58295819 assert(executable->basic_block_list.length > 0);
58305820 for (size_t block_i = 0; block_i < executable->basic_block_list.length; block_i += 1) {
58315821 IrBasicBlock *current_block = executable->basic_block_list.at(block_i);
5832 //assert(current_block->ref_count > 0);
58335822 assert(current_block->llvm_block);
58345823 LLVMPositionBuilderAtEnd(g->builder, current_block->llvm_block);
58355824 for (size_t instr_i = 0; instr_i < current_block->instruction_list.length; instr_i += 1) {
......@@ -5837,6 +5826,9 @@ static void ir_render(CodeGen *g, ZigFn *fn_entry) {
58375826 if (instruction->ref_count == 0 && !ir_has_side_effects(instruction))
58385827 continue;
58395828
5829 if (!g->strip_debug_symbols) {
5830 set_debug_location(g, instruction);
5831 }
58405832 instruction->llvm_value = ir_render_instruction(g, executable, instruction);
58415833 }
58425834 current_block->llvm_exit_block = LLVMGetInsertBlock(g->builder);
......@@ -6626,7 +6618,8 @@ static void render_const_val_global(CodeGen *g, ConstExprValue *const_val, const
66266618 LLVMSetLinkage(global_value, LLVMInternalLinkage);
66276619 LLVMSetGlobalConstant(global_value, true);
66286620 LLVMSetUnnamedAddr(global_value, true);
6629 LLVMSetAlignment(global_value, get_abi_alignment(g, const_val->type));
6621 LLVMSetAlignment(global_value, (const_val->global_refs->align == 0) ?
6622 get_abi_alignment(g, const_val->type) : const_val->global_refs->align);
66306623
66316624 const_val->global_refs->llvm_global = global_value;
66326625 }
......@@ -6751,7 +6744,7 @@ static void do_code_gen(CodeGen *g) {
67516744 zig_panic("TODO debug info for var with ptr casted value");
67526745 }
67536746 ZigType *var_type = g->builtin_types.entry_f128;
6754 ConstExprValue coerced_value;
6747 ConstExprValue coerced_value = {};
67556748 coerced_value.special = ConstValSpecialStatic;
67566749 coerced_value.type = var_type;
67576750 coerced_value.data.x_f128 = bigfloat_to_f128(&const_val->data.x_bigfloat);
......@@ -6856,20 +6849,24 @@ static void do_code_gen(CodeGen *g) {
68566849 FnTypeId *fn_type_id = &fn_table_entry->type_entry->data.fn.fn_type_id;
68576850 CallingConvention cc = fn_type_id->cc;
68586851 bool is_c_abi = cc == CallingConventionC;
6852 bool want_sret = want_first_arg_sret(g, fn_type_id);
68596853
68606854 LLVMValueRef fn = fn_llvm_value(g, fn_table_entry);
68616855 g->cur_fn = fn_table_entry;
68626856 g->cur_fn_val = fn;
6863 ZigType *return_type = fn_type_id->return_type;
6864 if (handle_is_ptr(return_type)) {
6857
6858 build_all_basic_blocks(g, fn_table_entry);
6859 clear_debug_source_node(g);
6860
6861 if (want_sret) {
68656862 g->cur_ret_ptr = LLVMGetParam(fn, 0);
6863 } else if (handle_is_ptr(fn_type_id->return_type)) {
6864 g->cur_ret_ptr = build_alloca(g, fn_type_id->return_type, "result", 0);
6865 // TODO add debug info variable for this
68666866 } else {
68676867 g->cur_ret_ptr = nullptr;
68686868 }
68696869
6870 build_all_basic_blocks(g, fn_table_entry);
6871 clear_debug_source_node(g);
6872
68736870 uint32_t err_ret_trace_arg_index = get_err_ret_trace_arg_index(g, fn_table_entry);
68746871 bool have_err_ret_trace_arg = err_ret_trace_arg_index != UINT32_MAX;
68756872 if (have_err_ret_trace_arg) {
......@@ -6894,68 +6891,28 @@ static void do_code_gen(CodeGen *g) {
68946891 }
68956892
68966893 // allocate temporary stack data
6897 for (size_t alloca_i = 0; alloca_i < fn_table_entry->alloca_list.length; alloca_i += 1) {
6898 IrInstruction *instruction = fn_table_entry->alloca_list.at(alloca_i);
6899 LLVMValueRef *slot;
6900 ZigType *slot_type = instruction->value.type;
6901 uint32_t alignment_bytes = 0;
6902 if (instruction->id == IrInstructionIdCast) {
6903 IrInstructionCast *cast_instruction = (IrInstructionCast *)instruction;
6904 slot = &cast_instruction->tmp_ptr;
6905 } else if (instruction->id == IrInstructionIdRef) {
6906 IrInstructionRef *ref_instruction = (IrInstructionRef *)instruction;
6907 slot = &ref_instruction->tmp_ptr;
6908 assert(instruction->value.type->id == ZigTypeIdPointer);
6909 slot_type = instruction->value.type->data.pointer.child_type;
6910 } else if (instruction->id == IrInstructionIdContainerInitList) {
6911 IrInstructionContainerInitList *container_init_list_instruction = (IrInstructionContainerInitList *)instruction;
6912 slot = &container_init_list_instruction->tmp_ptr;
6913 } else if (instruction->id == IrInstructionIdStructInit) {
6914 IrInstructionStructInit *struct_init_instruction = (IrInstructionStructInit *)instruction;
6915 slot = &struct_init_instruction->tmp_ptr;
6916 } else if (instruction->id == IrInstructionIdUnionInit) {
6917 IrInstructionUnionInit *union_init_instruction = (IrInstructionUnionInit *)instruction;
6918 slot = &union_init_instruction->tmp_ptr;
6919 } else if (instruction->id == IrInstructionIdCall) {
6920 IrInstructionCall *call_instruction = (IrInstructionCall *)instruction;
6921 slot = &call_instruction->tmp_ptr;
6922 } else if (instruction->id == IrInstructionIdSlice) {
6923 IrInstructionSlice *slice_instruction = (IrInstructionSlice *)instruction;
6924 slot = &slice_instruction->tmp_ptr;
6925 } else if (instruction->id == IrInstructionIdOptionalWrap) {
6926 IrInstructionOptionalWrap *maybe_wrap_instruction = (IrInstructionOptionalWrap *)instruction;
6927 slot = &maybe_wrap_instruction->tmp_ptr;
6928 } else if (instruction->id == IrInstructionIdErrWrapPayload) {
6929 IrInstructionErrWrapPayload *err_wrap_payload_instruction = (IrInstructionErrWrapPayload *)instruction;
6930 slot = &err_wrap_payload_instruction->tmp_ptr;
6931 } else if (instruction->id == IrInstructionIdErrWrapCode) {
6932 IrInstructionErrWrapCode *err_wrap_code_instruction = (IrInstructionErrWrapCode *)instruction;
6933 slot = &err_wrap_code_instruction->tmp_ptr;
6934 } else if (instruction->id == IrInstructionIdCmpxchgGen) {
6935 IrInstructionCmpxchgGen *cmpxchg_instruction = (IrInstructionCmpxchgGen *)instruction;
6936 slot = &cmpxchg_instruction->tmp_ptr;
6937 } else if (instruction->id == IrInstructionIdResizeSlice) {
6938 IrInstructionResizeSlice *resize_slice_instruction = (IrInstructionResizeSlice *)instruction;
6939 slot = &resize_slice_instruction->tmp_ptr;
6940 } else if (instruction->id == IrInstructionIdLoadPtrGen) {
6941 IrInstructionLoadPtrGen *load_ptr_inst = (IrInstructionLoadPtrGen *)instruction;
6942 slot = &load_ptr_inst->tmp_ptr;
6943 } else if (instruction->id == IrInstructionIdBitCastGen) {
6944 IrInstructionBitCastGen *bit_cast_inst = (IrInstructionBitCastGen *)instruction;
6945 slot = &bit_cast_inst->tmp_ptr;
6946 } else if (instruction->id == IrInstructionIdVectorToArray) {
6947 IrInstructionVectorToArray *vector_to_array_instruction = (IrInstructionVectorToArray *)instruction;
6948 alignment_bytes = get_abi_alignment(g, vector_to_array_instruction->vector->value.type);
6949 slot = &vector_to_array_instruction->tmp_ptr;
6950 } else {
6951 zig_unreachable();
6894 for (size_t alloca_i = 0; alloca_i < fn_table_entry->alloca_gen_list.length; alloca_i += 1) {
6895 IrInstructionAllocaGen *instruction = fn_table_entry->alloca_gen_list.at(alloca_i);
6896 ZigType *ptr_type = instruction->base.value.type;
6897 assert(ptr_type->id == ZigTypeIdPointer);
6898 ZigType *child_type = ptr_type->data.pointer.child_type;
6899 if (!type_has_bits(child_type))
6900 continue;
6901 if (instruction->base.ref_count == 0)
6902 continue;
6903 if (instruction->base.value.special != ConstValSpecialRuntime) {
6904 if (const_ptr_pointee(nullptr, g, &instruction->base.value, nullptr)->special !=
6905 ConstValSpecialRuntime)
6906 {
6907 continue;
6908 }
69526909 }
6953 *slot = build_alloca(g, slot_type, "", alignment_bytes);
6910 instruction->base.llvm_value = build_alloca(g, child_type, instruction->name_hint,
6911 get_ptr_align(g, ptr_type));
69546912 }
69556913
69566914 ZigType *import = get_scope_import(&fn_table_entry->fndef_scope->base);
6957
6958 unsigned gen_i_init = want_first_arg_sret(g, fn_type_id) ? 1 : 0;
6915 unsigned gen_i_init = want_sret ? 1 : 0;
69596916
69606917 // create debug variable declarations for variables and allocate all local variables
69616918 FnWalk fn_walk_var = {};
......@@ -6982,8 +6939,6 @@ static void do_code_gen(CodeGen *g) {
69826939 }
69836940
69846941 if (var->src_arg_index == SIZE_MAX) {
6985 var->value_ref = build_alloca(g, var->var_type, buf_ptr(&var->name), var->align_bytes);
6986
69876942 var->di_loc_var = ZigLLVMCreateAutoVariable(g->dbuilder, get_di_scope(g, var->parent_scope),
69886943 buf_ptr(&var->name), import->data.structure.root_struct->di_file, (unsigned)(var->decl_node->line + 1),
69896944 get_llvm_di_type(g, var->var_type), !g->strip_debug_symbols, 0);
src/ir.cpp+3051-1481
......@@ -38,6 +38,7 @@ struct IrAnalyze {
3838 ZigType *explicit_return_type;
3939 AstNode *explicit_return_type_source_node;
4040 ZigList<IrInstruction *> src_implicit_return_type_list;
41 ZigList<IrSuspendPosition> resume_stack;
4142 IrBasicBlock *const_predecessor_bb;
4243};
4344
......@@ -157,16 +158,19 @@ enum UndefAllowed {
157158};
158159
159160static IrInstruction *ir_gen_node(IrBuilder *irb, AstNode *node, Scope *scope);
160static IrInstruction *ir_gen_node_extra(IrBuilder *irb, AstNode *node, Scope *scope, LVal lval);
161static IrInstruction *ir_analyze_instruction(IrAnalyze *ira, IrInstruction *instruction);
161static IrInstruction *ir_gen_node_extra(IrBuilder *irb, AstNode *node, Scope *scope, LVal lval,
162 ResultLoc *result_loc);
162163static IrInstruction *ir_implicit_cast(IrAnalyze *ira, IrInstruction *value, ZigType *expected_type);
163static IrInstruction *ir_get_deref(IrAnalyze *ira, IrInstruction *source_instruction, IrInstruction *ptr);
164static IrInstruction *ir_get_deref(IrAnalyze *ira, IrInstruction *source_instruction, IrInstruction *ptr,
165 ResultLoc *result_loc);
164166static ErrorMsg *exec_add_error_node(CodeGen *codegen, IrExecutable *exec, AstNode *source_node, Buf *msg);
165167static IrInstruction *ir_analyze_container_field_ptr(IrAnalyze *ira, Buf *field_name,
166 IrInstruction *source_instr, IrInstruction *container_ptr, ZigType *container_type);
168 IrInstruction *source_instr, IrInstruction *container_ptr, ZigType *container_type, bool initializing);
169static void ir_assert(bool ok, IrInstruction *source_instruction);
167170static IrInstruction *ir_get_var_ptr(IrAnalyze *ira, IrInstruction *instruction, ZigVar *var);
168171static ZigType *ir_resolve_atomic_operand_type(IrAnalyze *ira, IrInstruction *op);
169static IrInstruction *ir_lval_wrap(IrBuilder *irb, Scope *scope, IrInstruction *value, LVal lval);
172static IrInstruction *ir_lval_wrap(IrBuilder *irb, Scope *scope, IrInstruction *value, LVal lval, ResultLoc *result_loc);
173static IrInstruction *ir_expr_wrap(IrBuilder *irb, Scope *scope, IrInstruction *inst, ResultLoc *result_loc);
170174static ZigType *adjust_ptr_align(CodeGen *g, ZigType *ptr_type, uint32_t new_align);
171175static ZigType *adjust_slice_align(CodeGen *g, ZigType *slice_type, uint32_t new_align);
172176static Error buf_read_value_bytes(IrAnalyze *ira, CodeGen *codegen, AstNode *source_node, uint8_t *buf, ConstExprValue *val);
......@@ -178,17 +182,28 @@ static IrInstruction *ir_analyze_ptr_cast(IrAnalyze *ira, IrInstruction *source_
178182static ConstExprValue *ir_resolve_const(IrAnalyze *ira, IrInstruction *value, UndefAllowed undef_allowed);
179183static void copy_const_val(ConstExprValue *dest, ConstExprValue *src, bool same_global_refs);
180184static Error resolve_ptr_align(IrAnalyze *ira, ZigType *ty, uint32_t *result_align);
181static void ir_add_alloca(IrAnalyze *ira, IrInstruction *instruction, ZigType *type_entry);
182185static IrInstruction *ir_analyze_int_to_ptr(IrAnalyze *ira, IrInstruction *source_instr, IrInstruction *target,
183186 ZigType *ptr_type);
184187static IrInstruction *ir_analyze_bit_cast(IrAnalyze *ira, IrInstruction *source_instr, IrInstruction *value,
185188 ZigType *dest_type);
189static IrInstruction *ir_resolve_result_raw(IrAnalyze *ira, IrInstruction *suspend_source_instr,
190 ResultLoc *result_loc, ZigType *value_type, IrInstruction *value, bool force_runtime, bool non_null_comptime);
191static IrInstruction *ir_resolve_result(IrAnalyze *ira, IrInstruction *suspend_source_instr,
192 ResultLoc *result_loc, ZigType *value_type, IrInstruction *value, bool force_runtime, bool non_null_comptime);
193static IrInstruction *ir_analyze_unwrap_optional_payload(IrAnalyze *ira, IrInstruction *source_instr,
194 IrInstruction *base_ptr, bool safety_check_on, bool initializing);
195static IrInstruction *ir_analyze_unwrap_error_payload(IrAnalyze *ira, IrInstruction *source_instr,
196 IrInstruction *base_ptr, bool safety_check_on, bool initializing);
197static IrInstruction *ir_analyze_unwrap_err_code(IrAnalyze *ira, IrInstruction *source_instr,
198 IrInstruction *base_ptr, bool initializing);
199static IrInstruction *ir_analyze_store_ptr(IrAnalyze *ira, IrInstruction *source_instr,
200 IrInstruction *ptr, IrInstruction *uncasted_value);
186201
187202static ConstExprValue *const_ptr_pointee_unchecked(CodeGen *g, ConstExprValue *const_val) {
188203 assert(get_src_ptr_type(const_val->type) != nullptr);
189204 assert(const_val->special == ConstValSpecialStatic);
190205 ConstExprValue *result;
191
206
192207 switch (type_has_one_possible_value(g, const_val->type->data.pointer.child_type)) {
193208 case OnePossibleValueInvalid:
194209 zig_unreachable();
......@@ -200,7 +215,7 @@ static ConstExprValue *const_ptr_pointee_unchecked(CodeGen *g, ConstExprValue *c
200215 case OnePossibleValueNo:
201216 break;
202217 }
203
218
204219 switch (const_val->data.x_ptr.special) {
205220 case ConstPtrSpecialInvalid:
206221 zig_unreachable();
......@@ -246,6 +261,15 @@ static bool is_opt_err_set(ZigType *ty) {
246261 (ty->id == ZigTypeIdOptional && ty->data.maybe.child_type->id == ZigTypeIdErrorSet);
247262}
248263
264static bool is_slice(ZigType *type) {
265 return type->id == ZigTypeIdStruct && type->data.structure.is_slice;
266}
267
268static bool slice_is_const(ZigType *type) {
269 assert(is_slice(type));
270 return type->data.structure.fields[slice_ptr_index].type_entry->data.pointer.is_const;
271}
272
249273// This function returns true when you can change the type of a ConstExprValue and the
250274// value remains meaningful.
251275static bool types_have_same_zig_comptime_repr(ZigType *a, ZigType *b) {
......@@ -282,8 +306,9 @@ static bool types_have_same_zig_comptime_repr(ZigType *a, ZigType *b) {
282306 return a->data.floating.bit_count == b->data.floating.bit_count;
283307 case ZigTypeIdInt:
284308 return a->data.integral.is_signed == b->data.integral.is_signed;
285 case ZigTypeIdArray:
286309 case ZigTypeIdStruct:
310 return is_slice(a) && is_slice(b);
311 case ZigTypeIdArray:
287312 case ZigTypeIdOptional:
288313 case ZigTypeIdErrorUnion:
289314 case ZigTypeIdEnum:
......@@ -386,6 +411,7 @@ static IrBasicBlock *ir_create_basic_block(IrBuilder *irb, Scope *scope, const c
386411 result->scope = scope;
387412 result->name_hint = name_hint;
388413 result->debug_id = exec_next_debug_id(irb->exec);
414 result->index = SIZE_MAX; // set later
389415 return result;
390416}
391417
......@@ -475,8 +501,16 @@ static constexpr IrInstructionId ir_instruction_id(IrInstructionVarPtr *) {
475501 return IrInstructionIdVarPtr;
476502}
477503
478static constexpr IrInstructionId ir_instruction_id(IrInstructionCall *) {
479 return IrInstructionIdCall;
504static constexpr IrInstructionId ir_instruction_id(IrInstructionReturnPtr *) {
505 return IrInstructionIdReturnPtr;
506}
507
508static constexpr IrInstructionId ir_instruction_id(IrInstructionCallSrc *) {
509 return IrInstructionIdCallSrc;
510}
511
512static constexpr IrInstructionId ir_instruction_id(IrInstructionCallGen *) {
513 return IrInstructionIdCallGen;
480514}
481515
482516static constexpr IrInstructionId ir_instruction_id(IrInstructionConst *) {
......@@ -511,14 +545,6 @@ static constexpr IrInstructionId ir_instruction_id(IrInstructionTypeOf *) {
511545 return IrInstructionIdTypeOf;
512546}
513547
514static constexpr IrInstructionId ir_instruction_id(IrInstructionToPtrType *) {
515 return IrInstructionIdToPtrType;
516}
517
518static constexpr IrInstructionId ir_instruction_id(IrInstructionPtrTypeChild *) {
519 return IrInstructionIdPtrTypeChild;
520}
521
522548static constexpr IrInstructionId ir_instruction_id(IrInstructionSetCold *) {
523549 return IrInstructionIdSetCold;
524550}
......@@ -611,12 +637,8 @@ static constexpr IrInstructionId ir_instruction_id(IrInstructionRef *) {
611637 return IrInstructionIdRef;
612638}
613639
614static constexpr IrInstructionId ir_instruction_id(IrInstructionStructInit *) {
615 return IrInstructionIdStructInit;
616}
617
618static constexpr IrInstructionId ir_instruction_id(IrInstructionUnionInit *) {
619 return IrInstructionIdUnionInit;
640static constexpr IrInstructionId ir_instruction_id(IrInstructionRefGen *) {
641 return IrInstructionIdRefGen;
620642}
621643
622644static constexpr IrInstructionId ir_instruction_id(IrInstructionCompileErr *) {
......@@ -703,8 +725,12 @@ static constexpr IrInstructionId ir_instruction_id(IrInstructionMemcpy *) {
703725 return IrInstructionIdMemcpy;
704726}
705727
706static constexpr IrInstructionId ir_instruction_id(IrInstructionSlice *) {
707 return IrInstructionIdSlice;
728static constexpr IrInstructionId ir_instruction_id(IrInstructionSliceSrc *) {
729 return IrInstructionIdSliceSrc;
730}
731
732static constexpr IrInstructionId ir_instruction_id(IrInstructionSliceGen *) {
733 return IrInstructionIdSliceGen;
708734}
709735
710736static constexpr IrInstructionId ir_instruction_id(IrInstructionMemberCount *) {
......@@ -743,8 +769,12 @@ static constexpr IrInstructionId ir_instruction_id(IrInstructionOverflowOp *) {
743769 return IrInstructionIdOverflowOp;
744770}
745771
746static constexpr IrInstructionId ir_instruction_id(IrInstructionTestErr *) {
747 return IrInstructionIdTestErr;
772static constexpr IrInstructionId ir_instruction_id(IrInstructionTestErrSrc *) {
773 return IrInstructionIdTestErrSrc;
774}
775
776static constexpr IrInstructionId ir_instruction_id(IrInstructionTestErrGen *) {
777 return IrInstructionIdTestErrGen;
748778}
749779
750780static constexpr IrInstructionId ir_instruction_id(IrInstructionMulAdd *) {
......@@ -787,8 +817,8 @@ static constexpr IrInstructionId ir_instruction_id(IrInstructionPtrCastGen *) {
787817 return IrInstructionIdPtrCastGen;
788818}
789819
790static constexpr IrInstructionId ir_instruction_id(IrInstructionBitCast *) {
791 return IrInstructionIdBitCast;
820static constexpr IrInstructionId ir_instruction_id(IrInstructionBitCastSrc *) {
821 return IrInstructionIdBitCastSrc;
792822}
793823
794824static constexpr IrInstructionId ir_instruction_id(IrInstructionBitCastGen *) {
......@@ -883,6 +913,26 @@ static constexpr IrInstructionId ir_instruction_id(IrInstructionAlignCast *) {
883913 return IrInstructionIdAlignCast;
884914}
885915
916static constexpr IrInstructionId ir_instruction_id(IrInstructionImplicitCast *) {
917 return IrInstructionIdImplicitCast;
918}
919
920static constexpr IrInstructionId ir_instruction_id(IrInstructionResolveResult *) {
921 return IrInstructionIdResolveResult;
922}
923
924static constexpr IrInstructionId ir_instruction_id(IrInstructionResetResult *) {
925 return IrInstructionIdResetResult;
926}
927
928static constexpr IrInstructionId ir_instruction_id(IrInstructionResultPtr *) {
929 return IrInstructionIdResultPtr;
930}
931
932static constexpr IrInstructionId ir_instruction_id(IrInstructionPtrOfArrayToSlice *) {
933 return IrInstructionIdPtrOfArrayToSlice;
934}
935
886936static constexpr IrInstructionId ir_instruction_id(IrInstructionOpaqueType *) {
887937 return IrInstructionIdOpaqueType;
888938}
......@@ -1023,6 +1073,18 @@ static constexpr IrInstructionId ir_instruction_id(IrInstructionUndeclaredIdent
10231073 return IrInstructionIdUndeclaredIdent;
10241074}
10251075
1076static constexpr IrInstructionId ir_instruction_id(IrInstructionAllocaSrc *) {
1077 return IrInstructionIdAllocaSrc;
1078}
1079
1080static constexpr IrInstructionId ir_instruction_id(IrInstructionAllocaGen *) {
1081 return IrInstructionIdAllocaGen;
1082}
1083
1084static constexpr IrInstructionId ir_instruction_id(IrInstructionEndExpr *) {
1085 return IrInstructionIdEndExpr;
1086}
1087
10261088template<typename T>
10271089static T *ir_create_instruction(IrBuilder *irb, Scope *scope, AstNode *source_node) {
10281090 T *special_instruction = allocate<T>(1);
......@@ -1074,13 +1136,15 @@ static IrInstruction *ir_build_cond_br(IrBuilder *irb, Scope *scope, AstNode *so
10741136 return &cond_br_instruction->base;
10751137}
10761138
1077static IrInstruction *ir_build_return(IrBuilder *irb, Scope *scope, AstNode *source_node, IrInstruction *return_value) {
1139static IrInstruction *ir_build_return(IrBuilder *irb, Scope *scope, AstNode *source_node,
1140 IrInstruction *return_value)
1141{
10781142 IrInstructionReturn *return_instruction = ir_build_instruction<IrInstructionReturn>(irb, scope, source_node);
10791143 return_instruction->base.value.type = irb->codegen->builtin_types.entry_unreachable;
10801144 return_instruction->base.value.special = ConstValSpecialStatic;
10811145 return_instruction->value = return_value;
10821146
1083 ir_ref_instruction(return_value, irb->current_basic_block);
1147 if (return_value != nullptr) ir_ref_instruction(return_value, irb->current_basic_block);
10841148
10851149 return &return_instruction->base;
10861150}
......@@ -1258,17 +1322,27 @@ static IrInstruction *ir_build_var_ptr(IrBuilder *irb, Scope *scope, AstNode *so
12581322 return ir_build_var_ptr_x(irb, scope, source_node, var, nullptr);
12591323}
12601324
1261static IrInstruction *ir_build_elem_ptr(IrBuilder *irb, Scope *scope, AstNode *source_node, IrInstruction *array_ptr,
1262 IrInstruction *elem_index, bool safety_check_on, PtrLen ptr_len)
1325static IrInstruction *ir_build_return_ptr(IrAnalyze *ira, IrInstruction *source_instruction, ZigType *ty) {
1326 IrInstructionReturnPtr *instruction = ir_build_instruction<IrInstructionReturnPtr>(&ira->new_irb,
1327 source_instruction->scope, source_instruction->source_node);
1328 instruction->base.value.type = ty;
1329 return &instruction->base;
1330}
1331
1332static IrInstruction *ir_build_elem_ptr(IrBuilder *irb, Scope *scope, AstNode *source_node,
1333 IrInstruction *array_ptr, IrInstruction *elem_index, bool safety_check_on, PtrLen ptr_len,
1334 IrInstruction *init_array_type)
12631335{
12641336 IrInstructionElemPtr *instruction = ir_build_instruction<IrInstructionElemPtr>(irb, scope, source_node);
12651337 instruction->array_ptr = array_ptr;
12661338 instruction->elem_index = elem_index;
12671339 instruction->safety_check_on = safety_check_on;
12681340 instruction->ptr_len = ptr_len;
1341 instruction->init_array_type = init_array_type;
12691342
12701343 ir_ref_instruction(array_ptr, irb->current_basic_block);
12711344 ir_ref_instruction(elem_index, irb->current_basic_block);
1345 if (init_array_type != nullptr) ir_ref_instruction(init_array_type, irb->current_basic_block);
12721346
12731347 return &instruction->base;
12741348}
......@@ -1288,12 +1362,13 @@ static IrInstruction *ir_build_field_ptr_instruction(IrBuilder *irb, Scope *scop
12881362}
12891363
12901364static IrInstruction *ir_build_field_ptr(IrBuilder *irb, Scope *scope, AstNode *source_node,
1291 IrInstruction *container_ptr, Buf *field_name)
1365 IrInstruction *container_ptr, Buf *field_name, bool initializing)
12921366{
12931367 IrInstructionFieldPtr *instruction = ir_build_instruction<IrInstructionFieldPtr>(irb, scope, source_node);
12941368 instruction->container_ptr = container_ptr;
12951369 instruction->field_name_buffer = field_name;
12961370 instruction->field_name_expr = nullptr;
1371 instruction->initializing = initializing;
12971372
12981373 ir_ref_instruction(container_ptr, irb->current_basic_block);
12991374
......@@ -1313,9 +1388,11 @@ static IrInstruction *ir_build_struct_field_ptr(IrBuilder *irb, Scope *scope, As
13131388}
13141389
13151390static IrInstruction *ir_build_union_field_ptr(IrBuilder *irb, Scope *scope, AstNode *source_node,
1316 IrInstruction *union_ptr, TypeUnionField *field)
1391 IrInstruction *union_ptr, TypeUnionField *field, bool safety_check_on, bool initializing)
13171392{
13181393 IrInstructionUnionFieldPtr *instruction = ir_build_instruction<IrInstructionUnionFieldPtr>(irb, scope, source_node);
1394 instruction->initializing = initializing;
1395 instruction->safety_check_on = safety_check_on;
13191396 instruction->union_ptr = union_ptr;
13201397 instruction->field = field;
13211398
......@@ -1324,12 +1401,12 @@ static IrInstruction *ir_build_union_field_ptr(IrBuilder *irb, Scope *scope, Ast
13241401 return &instruction->base;
13251402}
13261403
1327static IrInstruction *ir_build_call(IrBuilder *irb, Scope *scope, AstNode *source_node,
1404static IrInstruction *ir_build_call_src(IrBuilder *irb, Scope *scope, AstNode *source_node,
13281405 ZigFn *fn_entry, IrInstruction *fn_ref, size_t arg_count, IrInstruction **args,
13291406 bool is_comptime, FnInline fn_inline, bool is_async, IrInstruction *async_allocator,
1330 IrInstruction *new_stack)
1407 IrInstruction *new_stack, ResultLoc *result_loc)
13311408{
1332 IrInstructionCall *call_instruction = ir_build_instruction<IrInstructionCall>(irb, scope, source_node);
1409 IrInstructionCallSrc *call_instruction = ir_build_instruction<IrInstructionCallSrc>(irb, scope, source_node);
13331410 call_instruction->fn_entry = fn_entry;
13341411 call_instruction->fn_ref = fn_ref;
13351412 call_instruction->is_comptime = is_comptime;
......@@ -1339,6 +1416,7 @@ static IrInstruction *ir_build_call(IrBuilder *irb, Scope *scope, AstNode *sourc
13391416 call_instruction->is_async = is_async;
13401417 call_instruction->async_allocator = async_allocator;
13411418 call_instruction->new_stack = new_stack;
1419 call_instruction->result_loc = result_loc;
13421420
13431421 if (fn_ref != nullptr) ir_ref_instruction(fn_ref, irb->current_basic_block);
13441422 for (size_t i = 0; i < arg_count; i += 1)
......@@ -1349,8 +1427,37 @@ static IrInstruction *ir_build_call(IrBuilder *irb, Scope *scope, AstNode *sourc
13491427 return &call_instruction->base;
13501428}
13511429
1430static IrInstruction *ir_build_call_gen(IrAnalyze *ira, IrInstruction *source_instruction,
1431 ZigFn *fn_entry, IrInstruction *fn_ref, size_t arg_count, IrInstruction **args,
1432 FnInline fn_inline, bool is_async, IrInstruction *async_allocator, IrInstruction *new_stack,
1433 IrInstruction *result_loc, ZigType *return_type)
1434{
1435 IrInstructionCallGen *call_instruction = ir_build_instruction<IrInstructionCallGen>(&ira->new_irb,
1436 source_instruction->scope, source_instruction->source_node);
1437 call_instruction->base.value.type = return_type;
1438 call_instruction->fn_entry = fn_entry;
1439 call_instruction->fn_ref = fn_ref;
1440 call_instruction->fn_inline = fn_inline;
1441 call_instruction->args = args;
1442 call_instruction->arg_count = arg_count;
1443 call_instruction->is_async = is_async;
1444 call_instruction->async_allocator = async_allocator;
1445 call_instruction->new_stack = new_stack;
1446 call_instruction->result_loc = result_loc;
1447
1448 if (fn_ref != nullptr) ir_ref_instruction(fn_ref, ira->new_irb.current_basic_block);
1449 for (size_t i = 0; i < arg_count; i += 1)
1450 ir_ref_instruction(args[i], ira->new_irb.current_basic_block);
1451 if (async_allocator != nullptr) ir_ref_instruction(async_allocator, ira->new_irb.current_basic_block);
1452 if (new_stack != nullptr) ir_ref_instruction(new_stack, ira->new_irb.current_basic_block);
1453 if (result_loc != nullptr) ir_ref_instruction(result_loc, ira->new_irb.current_basic_block);
1454
1455 return &call_instruction->base;
1456}
1457
13521458static IrInstruction *ir_build_phi(IrBuilder *irb, Scope *scope, AstNode *source_node,
1353 size_t incoming_count, IrBasicBlock **incoming_blocks, IrInstruction **incoming_values)
1459 size_t incoming_count, IrBasicBlock **incoming_blocks, IrInstruction **incoming_values,
1460 ResultLocPeerParent *peer_parent)
13541461{
13551462 assert(incoming_count != 0);
13561463 assert(incoming_count != SIZE_MAX);
......@@ -1359,6 +1466,7 @@ static IrInstruction *ir_build_phi(IrBuilder *irb, Scope *scope, AstNode *source
13591466 phi_instruction->incoming_count = incoming_count;
13601467 phi_instruction->incoming_blocks = incoming_blocks;
13611468 phi_instruction->incoming_values = incoming_values;
1469 phi_instruction->peer_parent = peer_parent;
13621470
13631471 for (size_t i = 0; i < incoming_count; i += 1) {
13641472 ir_ref_bb(incoming_blocks[i]);
......@@ -1412,12 +1520,13 @@ static IrInstruction *ir_build_ptr_type(IrBuilder *irb, Scope *scope, AstNode *s
14121520}
14131521
14141522static IrInstruction *ir_build_un_op_lval(IrBuilder *irb, Scope *scope, AstNode *source_node, IrUnOp op_id,
1415 IrInstruction *value, LVal lval)
1523 IrInstruction *value, LVal lval, ResultLoc *result_loc)
14161524{
14171525 IrInstructionUnOp *instruction = ir_build_instruction<IrInstructionUnOp>(irb, scope, source_node);
14181526 instruction->op_id = op_id;
14191527 instruction->value = value;
14201528 instruction->lval = lval;
1529 instruction->result_loc = result_loc;
14211530
14221531 ir_ref_instruction(value, irb->current_basic_block);
14231532
......@@ -1427,72 +1536,49 @@ static IrInstruction *ir_build_un_op_lval(IrBuilder *irb, Scope *scope, AstNode
14271536static IrInstruction *ir_build_un_op(IrBuilder *irb, Scope *scope, AstNode *source_node, IrUnOp op_id,
14281537 IrInstruction *value)
14291538{
1430 return ir_build_un_op_lval(irb, scope, source_node, op_id, value, LValNone);
1539 return ir_build_un_op_lval(irb, scope, source_node, op_id, value, LValNone, nullptr);
14311540}
14321541
14331542static IrInstruction *ir_build_container_init_list(IrBuilder *irb, Scope *scope, AstNode *source_node,
1434 IrInstruction *container_type, IrInstruction *elem_type, size_t item_count, IrInstruction **items)
1543 IrInstruction *container_type, size_t item_count, IrInstruction **elem_result_loc_list,
1544 IrInstruction *result_loc)
14351545{
14361546 IrInstructionContainerInitList *container_init_list_instruction =
14371547 ir_build_instruction<IrInstructionContainerInitList>(irb, scope, source_node);
14381548 container_init_list_instruction->container_type = container_type;
1439 container_init_list_instruction->elem_type = elem_type;
14401549 container_init_list_instruction->item_count = item_count;
1441 container_init_list_instruction->items = items;
1550 container_init_list_instruction->elem_result_loc_list = elem_result_loc_list;
1551 container_init_list_instruction->result_loc = result_loc;
14421552
1443 if (container_type != nullptr) ir_ref_instruction(container_type, irb->current_basic_block);
1444 if (elem_type != nullptr) ir_ref_instruction(elem_type, irb->current_basic_block);
1553 ir_ref_instruction(container_type, irb->current_basic_block);
14451554 for (size_t i = 0; i < item_count; i += 1) {
1446 ir_ref_instruction(items[i], irb->current_basic_block);
1555 ir_ref_instruction(elem_result_loc_list[i], irb->current_basic_block);
14471556 }
1557 if (result_loc != nullptr) ir_ref_instruction(result_loc, irb->current_basic_block);
14481558
14491559 return &container_init_list_instruction->base;
14501560}
14511561
14521562static IrInstruction *ir_build_container_init_fields(IrBuilder *irb, Scope *scope, AstNode *source_node,
1453 IrInstruction *container_type, size_t field_count, IrInstructionContainerInitFieldsField *fields)
1563 IrInstruction *container_type, size_t field_count, IrInstructionContainerInitFieldsField *fields,
1564 IrInstruction *result_loc)
14541565{
14551566 IrInstructionContainerInitFields *container_init_fields_instruction =
14561567 ir_build_instruction<IrInstructionContainerInitFields>(irb, scope, source_node);
14571568 container_init_fields_instruction->container_type = container_type;
14581569 container_init_fields_instruction->field_count = field_count;
14591570 container_init_fields_instruction->fields = fields;
1571 container_init_fields_instruction->result_loc = result_loc;
14601572
14611573 ir_ref_instruction(container_type, irb->current_basic_block);
14621574 for (size_t i = 0; i < field_count; i += 1) {
1463 ir_ref_instruction(fields[i].value, irb->current_basic_block);
1575 ir_ref_instruction(fields[i].result_loc, irb->current_basic_block);
14641576 }
1577 if (result_loc != nullptr) ir_ref_instruction(result_loc, irb->current_basic_block);
14651578
14661579 return &container_init_fields_instruction->base;
14671580}
14681581
1469static IrInstruction *ir_build_struct_init(IrBuilder *irb, Scope *scope, AstNode *source_node,
1470 ZigType *struct_type, size_t field_count, IrInstructionStructInitField *fields)
1471{
1472 IrInstructionStructInit *struct_init_instruction = ir_build_instruction<IrInstructionStructInit>(irb, scope, source_node);
1473 struct_init_instruction->struct_type = struct_type;
1474 struct_init_instruction->field_count = field_count;
1475 struct_init_instruction->fields = fields;
1476
1477 for (size_t i = 0; i < field_count; i += 1)
1478 ir_ref_instruction(fields[i].value, irb->current_basic_block);
1479
1480 return &struct_init_instruction->base;
1481}
1482
1483static IrInstruction *ir_build_union_init(IrBuilder *irb, Scope *scope, AstNode *source_node,
1484 ZigType *union_type, TypeUnionField *field, IrInstruction *init_value)
1485{
1486 IrInstructionUnionInit *union_init_instruction = ir_build_instruction<IrInstructionUnionInit>(irb, scope, source_node);
1487 union_init_instruction->union_type = union_type;
1488 union_init_instruction->field = field;
1489 union_init_instruction->init_value = init_value;
1490
1491 ir_ref_instruction(init_value, irb->current_basic_block);
1492
1493 return &union_init_instruction->base;
1494}
1495
14961582static IrInstruction *ir_build_unreachable(IrBuilder *irb, Scope *scope, AstNode *source_node) {
14971583 IrInstructionUnreachable *unreachable_instruction =
14981584 ir_build_instruction<IrInstructionUnreachable>(irb, scope, source_node);
......@@ -1517,47 +1603,47 @@ static IrInstruction *ir_build_store_ptr(IrBuilder *irb, Scope *scope, AstNode *
15171603}
15181604
15191605static IrInstruction *ir_build_var_decl_src(IrBuilder *irb, Scope *scope, AstNode *source_node,
1520 ZigVar *var, IrInstruction *var_type, IrInstruction *align_value, IrInstruction *init_value)
1606 ZigVar *var, IrInstruction *align_value, IrInstruction *ptr)
15211607{
15221608 IrInstructionDeclVarSrc *decl_var_instruction = ir_build_instruction<IrInstructionDeclVarSrc>(irb, scope, source_node);
15231609 decl_var_instruction->base.value.special = ConstValSpecialStatic;
15241610 decl_var_instruction->base.value.type = irb->codegen->builtin_types.entry_void;
15251611 decl_var_instruction->var = var;
1526 decl_var_instruction->var_type = var_type;
15271612 decl_var_instruction->align_value = align_value;
1528 decl_var_instruction->init_value = init_value;
1613 decl_var_instruction->ptr = ptr;
15291614
1530 if (var_type != nullptr) ir_ref_instruction(var_type, irb->current_basic_block);
15311615 if (align_value != nullptr) ir_ref_instruction(align_value, irb->current_basic_block);
1532 ir_ref_instruction(init_value, irb->current_basic_block);
1616 ir_ref_instruction(ptr, irb->current_basic_block);
15331617
15341618 return &decl_var_instruction->base;
15351619}
15361620
15371621static IrInstruction *ir_build_var_decl_gen(IrAnalyze *ira, IrInstruction *source_instruction,
1538 ZigVar *var, IrInstruction *init_value)
1622 ZigVar *var, IrInstruction *var_ptr)
15391623{
15401624 IrInstructionDeclVarGen *decl_var_instruction = ir_build_instruction<IrInstructionDeclVarGen>(&ira->new_irb,
15411625 source_instruction->scope, source_instruction->source_node);
15421626 decl_var_instruction->base.value.special = ConstValSpecialStatic;
15431627 decl_var_instruction->base.value.type = ira->codegen->builtin_types.entry_void;
15441628 decl_var_instruction->var = var;
1545 decl_var_instruction->init_value = init_value;
1629 decl_var_instruction->var_ptr = var_ptr;
15461630
1547 ir_ref_instruction(init_value, ira->new_irb.current_basic_block);
1631 ir_ref_instruction(var_ptr, ira->new_irb.current_basic_block);
15481632
15491633 return &decl_var_instruction->base;
15501634}
15511635
15521636static IrInstruction *ir_build_resize_slice(IrAnalyze *ira, IrInstruction *source_instruction,
1553 IrInstruction *operand, ZigType *ty)
1637 IrInstruction *operand, ZigType *ty, IrInstruction *result_loc)
15541638{
15551639 IrInstructionResizeSlice *instruction = ir_build_instruction<IrInstructionResizeSlice>(&ira->new_irb,
15561640 source_instruction->scope, source_instruction->source_node);
15571641 instruction->base.value.type = ty;
15581642 instruction->operand = operand;
1643 instruction->result_loc = result_loc;
15591644
15601645 ir_ref_instruction(operand, ira->new_irb.current_basic_block);
1646 ir_ref_instruction(result_loc, ira->new_irb.current_basic_block);
15611647
15621648 return &instruction->base;
15631649}
......@@ -1598,27 +1684,6 @@ static IrInstruction *ir_build_typeof(IrBuilder *irb, Scope *scope, AstNode *sou
15981684 return &instruction->base;
15991685}
16001686
1601static IrInstruction *ir_build_to_ptr_type(IrBuilder *irb, Scope *scope, AstNode *source_node, IrInstruction *ptr) {
1602 IrInstructionToPtrType *instruction = ir_build_instruction<IrInstructionToPtrType>(irb, scope, source_node);
1603 instruction->ptr = ptr;
1604
1605 ir_ref_instruction(ptr, irb->current_basic_block);
1606
1607 return &instruction->base;
1608}
1609
1610static IrInstruction *ir_build_ptr_type_child(IrBuilder *irb, Scope *scope, AstNode *source_node,
1611 IrInstruction *value)
1612{
1613 IrInstructionPtrTypeChild *instruction = ir_build_instruction<IrInstructionPtrTypeChild>(
1614 irb, scope, source_node);
1615 instruction->value = value;
1616
1617 ir_ref_instruction(value, irb->current_basic_block);
1618
1619 return &instruction->base;
1620}
1621
16221687static IrInstruction *ir_build_set_cold(IrBuilder *irb, Scope *scope, AstNode *source_node, IrInstruction *is_cold) {
16231688 IrInstructionSetCold *instruction = ir_build_instruction<IrInstructionSetCold>(irb, scope, source_node);
16241689 instruction->is_cold = is_cold;
......@@ -1744,40 +1809,59 @@ static IrInstruction *ir_build_test_nonnull(IrBuilder *irb, Scope *scope, AstNod
17441809}
17451810
17461811static IrInstruction *ir_build_optional_unwrap_ptr(IrBuilder *irb, Scope *scope, AstNode *source_node,
1747 IrInstruction *base_ptr, bool safety_check_on)
1812 IrInstruction *base_ptr, bool safety_check_on, bool initializing)
17481813{
17491814 IrInstructionOptionalUnwrapPtr *instruction = ir_build_instruction<IrInstructionOptionalUnwrapPtr>(irb, scope, source_node);
17501815 instruction->base_ptr = base_ptr;
17511816 instruction->safety_check_on = safety_check_on;
1817 instruction->initializing = initializing;
17521818
17531819 ir_ref_instruction(base_ptr, irb->current_basic_block);
17541820
17551821 return &instruction->base;
17561822}
17571823
1758static IrInstruction *ir_build_maybe_wrap(IrBuilder *irb, Scope *scope, AstNode *source_node, IrInstruction *value) {
1759 IrInstructionOptionalWrap *instruction = ir_build_instruction<IrInstructionOptionalWrap>(irb, scope, source_node);
1760 instruction->value = value;
1824static IrInstruction *ir_build_optional_wrap(IrAnalyze *ira, IrInstruction *source_instruction, ZigType *result_ty,
1825 IrInstruction *operand, IrInstruction *result_loc)
1826{
1827 IrInstructionOptionalWrap *instruction = ir_build_instruction<IrInstructionOptionalWrap>(
1828 &ira->new_irb, source_instruction->scope, source_instruction->source_node);
1829 instruction->base.value.type = result_ty;
1830 instruction->operand = operand;
1831 instruction->result_loc = result_loc;
17611832
1762 ir_ref_instruction(value, irb->current_basic_block);
1833 ir_ref_instruction(operand, ira->new_irb.current_basic_block);
1834 if (result_loc != nullptr) ir_ref_instruction(result_loc, ira->new_irb.current_basic_block);
17631835
17641836 return &instruction->base;
17651837}
17661838
1767static IrInstruction *ir_build_err_wrap_payload(IrBuilder *irb, Scope *scope, AstNode *source_node, IrInstruction *value) {
1768 IrInstructionErrWrapPayload *instruction = ir_build_instruction<IrInstructionErrWrapPayload>(irb, scope, source_node);
1769 instruction->value = value;
1839static IrInstruction *ir_build_err_wrap_payload(IrAnalyze *ira, IrInstruction *source_instruction,
1840 ZigType *result_type, IrInstruction *operand, IrInstruction *result_loc)
1841{
1842 IrInstructionErrWrapPayload *instruction = ir_build_instruction<IrInstructionErrWrapPayload>(
1843 &ira->new_irb, source_instruction->scope, source_instruction->source_node);
1844 instruction->base.value.type = result_type;
1845 instruction->operand = operand;
1846 instruction->result_loc = result_loc;
17701847
1771 ir_ref_instruction(value, irb->current_basic_block);
1848 ir_ref_instruction(operand, ira->new_irb.current_basic_block);
1849 if (result_loc != nullptr) ir_ref_instruction(result_loc, ira->new_irb.current_basic_block);
17721850
17731851 return &instruction->base;
17741852}
17751853
1776static IrInstruction *ir_build_err_wrap_code(IrBuilder *irb, Scope *scope, AstNode *source_node, IrInstruction *value) {
1777 IrInstructionErrWrapCode *instruction = ir_build_instruction<IrInstructionErrWrapCode>(irb, scope, source_node);
1778 instruction->value = value;
1854static IrInstruction *ir_build_err_wrap_code(IrAnalyze *ira, IrInstruction *source_instruction,
1855 ZigType *result_type, IrInstruction *operand, IrInstruction *result_loc)
1856{
1857 IrInstructionErrWrapCode *instruction = ir_build_instruction<IrInstructionErrWrapCode>(
1858 &ira->new_irb, source_instruction->scope, source_instruction->source_node);
1859 instruction->base.value.type = result_type;
1860 instruction->operand = operand;
1861 instruction->result_loc = result_loc;
17791862
1780 ir_ref_instruction(value, irb->current_basic_block);
1863 ir_ref_instruction(operand, ira->new_irb.current_basic_block);
1864 if (result_loc != nullptr) ir_ref_instruction(result_loc, ira->new_irb.current_basic_block);
17811865
17821866 return &instruction->base;
17831867}
......@@ -1934,6 +2018,21 @@ static IrInstruction *ir_build_ref(IrBuilder *irb, Scope *scope, AstNode *source
19342018 return &instruction->base;
19352019}
19362020
2021static IrInstruction *ir_build_ref_gen(IrAnalyze *ira, IrInstruction *source_instruction, ZigType *result_type,
2022 IrInstruction *operand, IrInstruction *result_loc)
2023{
2024 IrInstructionRefGen *instruction = ir_build_instruction<IrInstructionRefGen>(&ira->new_irb,
2025 source_instruction->scope, source_instruction->source_node);
2026 instruction->base.value.type = result_type;
2027 instruction->operand = operand;
2028 instruction->result_loc = result_loc;
2029
2030 ir_ref_instruction(operand, ira->new_irb.current_basic_block);
2031 if (result_loc != nullptr) ir_ref_instruction(result_loc, ira->new_irb.current_basic_block);
2032
2033 return &instruction->base;
2034}
2035
19372036static IrInstruction *ir_build_compile_err(IrBuilder *irb, Scope *scope, AstNode *source_node, IrInstruction *msg) {
19382037 IrInstructionCompileErr *instruction = ir_build_instruction<IrInstructionCompileErr>(irb, scope, source_node);
19392038 instruction->msg = msg;
......@@ -2011,8 +2110,7 @@ static IrInstruction *ir_build_embed_file(IrBuilder *irb, Scope *scope, AstNode
20112110
20122111static IrInstruction *ir_build_cmpxchg_src(IrBuilder *irb, Scope *scope, AstNode *source_node,
20132112 IrInstruction *type_value, IrInstruction *ptr, IrInstruction *cmp_value, IrInstruction *new_value,
2014 IrInstruction *success_order_value, IrInstruction *failure_order_value,
2015 bool is_weak)
2113 IrInstruction *success_order_value, IrInstruction *failure_order_value, bool is_weak, ResultLoc *result_loc)
20162114{
20172115 IrInstructionCmpxchgSrc *instruction = ir_build_instruction<IrInstructionCmpxchgSrc>(irb, scope, source_node);
20182116 instruction->type_value = type_value;
......@@ -2022,6 +2120,7 @@ static IrInstruction *ir_build_cmpxchg_src(IrBuilder *irb, Scope *scope, AstNode
20222120 instruction->success_order_value = success_order_value;
20232121 instruction->failure_order_value = failure_order_value;
20242122 instruction->is_weak = is_weak;
2123 instruction->result_loc = result_loc;
20252124
20262125 ir_ref_instruction(type_value, irb->current_basic_block);
20272126 ir_ref_instruction(ptr, irb->current_basic_block);
......@@ -2033,22 +2132,25 @@ static IrInstruction *ir_build_cmpxchg_src(IrBuilder *irb, Scope *scope, AstNode
20332132 return &instruction->base;
20342133}
20352134
2036static IrInstruction *ir_build_cmpxchg_gen(IrAnalyze *ira, IrInstruction *source_instruction,
2135static IrInstruction *ir_build_cmpxchg_gen(IrAnalyze *ira, IrInstruction *source_instruction, ZigType *result_type,
20372136 IrInstruction *ptr, IrInstruction *cmp_value, IrInstruction *new_value,
2038 AtomicOrder success_order, AtomicOrder failure_order, bool is_weak)
2137 AtomicOrder success_order, AtomicOrder failure_order, bool is_weak, IrInstruction *result_loc)
20392138{
20402139 IrInstructionCmpxchgGen *instruction = ir_build_instruction<IrInstructionCmpxchgGen>(&ira->new_irb,
20412140 source_instruction->scope, source_instruction->source_node);
2141 instruction->base.value.type = result_type;
20422142 instruction->ptr = ptr;
20432143 instruction->cmp_value = cmp_value;
20442144 instruction->new_value = new_value;
20452145 instruction->success_order = success_order;
20462146 instruction->failure_order = failure_order;
20472147 instruction->is_weak = is_weak;
2148 instruction->result_loc = result_loc;
20482149
20492150 ir_ref_instruction(ptr, ira->new_irb.current_basic_block);
20502151 ir_ref_instruction(cmp_value, ira->new_irb.current_basic_block);
20512152 ir_ref_instruction(new_value, ira->new_irb.current_basic_block);
2153 if (result_loc != nullptr) ir_ref_instruction(result_loc, ira->new_irb.current_basic_block);
20522154
20532155 return &instruction->base;
20542156}
......@@ -2107,19 +2209,25 @@ static IrInstruction *ir_build_err_set_cast(IrBuilder *irb, Scope *scope, AstNod
21072209 return &instruction->base;
21082210}
21092211
2110static IrInstruction *ir_build_to_bytes(IrBuilder *irb, Scope *scope, AstNode *source_node, IrInstruction *target) {
2212static IrInstruction *ir_build_to_bytes(IrBuilder *irb, Scope *scope, AstNode *source_node, IrInstruction *target,
2213 ResultLoc *result_loc)
2214{
21112215 IrInstructionToBytes *instruction = ir_build_instruction<IrInstructionToBytes>(irb, scope, source_node);
21122216 instruction->target = target;
2217 instruction->result_loc = result_loc;
21132218
21142219 ir_ref_instruction(target, irb->current_basic_block);
21152220
21162221 return &instruction->base;
21172222}
21182223
2119static IrInstruction *ir_build_from_bytes(IrBuilder *irb, Scope *scope, AstNode *source_node, IrInstruction *dest_child_type, IrInstruction *target) {
2224static IrInstruction *ir_build_from_bytes(IrBuilder *irb, Scope *scope, AstNode *source_node,
2225 IrInstruction *dest_child_type, IrInstruction *target, ResultLoc *result_loc)
2226{
21202227 IrInstructionFromBytes *instruction = ir_build_instruction<IrInstructionFromBytes>(irb, scope, source_node);
21212228 instruction->dest_child_type = dest_child_type;
21222229 instruction->target = target;
2230 instruction->result_loc = result_loc;
21232231
21242232 ir_ref_instruction(dest_child_type, irb->current_basic_block);
21252233 ir_ref_instruction(target, irb->current_basic_block);
......@@ -2221,14 +2329,15 @@ static IrInstruction *ir_build_memcpy(IrBuilder *irb, Scope *scope, AstNode *sou
22212329 return &instruction->base;
22222330}
22232331
2224static IrInstruction *ir_build_slice(IrBuilder *irb, Scope *scope, AstNode *source_node,
2225 IrInstruction *ptr, IrInstruction *start, IrInstruction *end, bool safety_check_on)
2332static IrInstruction *ir_build_slice_src(IrBuilder *irb, Scope *scope, AstNode *source_node,
2333 IrInstruction *ptr, IrInstruction *start, IrInstruction *end, bool safety_check_on, ResultLoc *result_loc)
22262334{
2227 IrInstructionSlice *instruction = ir_build_instruction<IrInstructionSlice>(irb, scope, source_node);
2335 IrInstructionSliceSrc *instruction = ir_build_instruction<IrInstructionSliceSrc>(irb, scope, source_node);
22282336 instruction->ptr = ptr;
22292337 instruction->start = start;
22302338 instruction->end = end;
22312339 instruction->safety_check_on = safety_check_on;
2340 instruction->result_loc = result_loc;
22322341
22332342 ir_ref_instruction(ptr, irb->current_basic_block);
22342343 ir_ref_instruction(start, irb->current_basic_block);
......@@ -2237,6 +2346,26 @@ static IrInstruction *ir_build_slice(IrBuilder *irb, Scope *scope, AstNode *sour
22372346 return &instruction->base;
22382347}
22392348
2349static IrInstruction *ir_build_slice_gen(IrAnalyze *ira, IrInstruction *source_instruction, ZigType *slice_type,
2350 IrInstruction *ptr, IrInstruction *start, IrInstruction *end, bool safety_check_on, IrInstruction *result_loc)
2351{
2352 IrInstructionSliceGen *instruction = ir_build_instruction<IrInstructionSliceGen>(
2353 &ira->new_irb, source_instruction->scope, source_instruction->source_node);
2354 instruction->base.value.type = slice_type;
2355 instruction->ptr = ptr;
2356 instruction->start = start;
2357 instruction->end = end;
2358 instruction->safety_check_on = safety_check_on;
2359 instruction->result_loc = result_loc;
2360
2361 ir_ref_instruction(ptr, ira->new_irb.current_basic_block);
2362 ir_ref_instruction(start, ira->new_irb.current_basic_block);
2363 if (end) ir_ref_instruction(end, ira->new_irb.current_basic_block);
2364 ir_ref_instruction(result_loc, ira->new_irb.current_basic_block);
2365
2366 return &instruction->base;
2367}
2368
22402369static IrInstruction *ir_build_member_count(IrBuilder *irb, Scope *scope, AstNode *source_node, IrInstruction *container) {
22412370 IrInstructionMemberCount *instruction = ir_build_instruction<IrInstructionMemberCount>(irb, scope, source_node);
22422371 instruction->container = container;
......@@ -2390,34 +2519,49 @@ static IrInstruction *ir_build_align_of(IrBuilder *irb, Scope *scope, AstNode *s
23902519 return &instruction->base;
23912520}
23922521
2393static IrInstruction *ir_build_test_err(IrBuilder *irb, Scope *scope, AstNode *source_node,
2394 IrInstruction *value)
2522static IrInstruction *ir_build_test_err_src(IrBuilder *irb, Scope *scope, AstNode *source_node,
2523 IrInstruction *base_ptr, bool resolve_err_set)
23952524{
2396 IrInstructionTestErr *instruction = ir_build_instruction<IrInstructionTestErr>(irb, scope, source_node);
2397 instruction->value = value;
2525 IrInstructionTestErrSrc *instruction = ir_build_instruction<IrInstructionTestErrSrc>(irb, scope, source_node);
2526 instruction->base_ptr = base_ptr;
2527 instruction->resolve_err_set = resolve_err_set;
23982528
2399 ir_ref_instruction(value, irb->current_basic_block);
2529 ir_ref_instruction(base_ptr, irb->current_basic_block);
24002530
24012531 return &instruction->base;
24022532}
24032533
2404static IrInstruction *ir_build_unwrap_err_code(IrBuilder *irb, Scope *scope, AstNode *source_node,
2534static IrInstruction *ir_build_test_err_gen(IrAnalyze *ira, IrInstruction *source_instruction,
24052535 IrInstruction *err_union)
24062536{
2407 IrInstructionUnwrapErrCode *instruction = ir_build_instruction<IrInstructionUnwrapErrCode>(irb, scope, source_node);
2537 IrInstructionTestErrGen *instruction = ir_build_instruction<IrInstructionTestErrGen>(
2538 &ira->new_irb, source_instruction->scope, source_instruction->source_node);
2539 instruction->base.value.type = ira->codegen->builtin_types.entry_bool;
24082540 instruction->err_union = err_union;
24092541
2410 ir_ref_instruction(err_union, irb->current_basic_block);
2542 ir_ref_instruction(err_union, ira->new_irb.current_basic_block);
2543
2544 return &instruction->base;
2545}
2546
2547static IrInstruction *ir_build_unwrap_err_code(IrBuilder *irb, Scope *scope, AstNode *source_node,
2548 IrInstruction *err_union_ptr)
2549{
2550 IrInstructionUnwrapErrCode *instruction = ir_build_instruction<IrInstructionUnwrapErrCode>(irb, scope, source_node);
2551 instruction->err_union_ptr = err_union_ptr;
2552
2553 ir_ref_instruction(err_union_ptr, irb->current_basic_block);
24112554
24122555 return &instruction->base;
24132556}
24142557
24152558static IrInstruction *ir_build_unwrap_err_payload(IrBuilder *irb, Scope *scope, AstNode *source_node,
2416 IrInstruction *value, bool safety_check_on)
2559 IrInstruction *value, bool safety_check_on, bool initializing)
24172560{
24182561 IrInstructionUnwrapErrPayload *instruction = ir_build_instruction<IrInstructionUnwrapErrPayload>(irb, scope, source_node);
24192562 instruction->value = value;
24202563 instruction->safety_check_on = safety_check_on;
2564 instruction->initializing = initializing;
24212565
24222566 ir_ref_instruction(value, irb->current_basic_block);
24232567
......@@ -2487,28 +2631,28 @@ static IrInstruction *ir_build_ptr_cast_gen(IrAnalyze *ira, IrInstruction *sourc
24872631}
24882632
24892633static IrInstruction *ir_build_load_ptr_gen(IrAnalyze *ira, IrInstruction *source_instruction,
2490 IrInstruction *ptr, ZigType *ty)
2634 IrInstruction *ptr, ZigType *ty, IrInstruction *result_loc)
24912635{
24922636 IrInstructionLoadPtrGen *instruction = ir_build_instruction<IrInstructionLoadPtrGen>(
24932637 &ira->new_irb, source_instruction->scope, source_instruction->source_node);
24942638 instruction->base.value.type = ty;
24952639 instruction->ptr = ptr;
2640 instruction->result_loc = result_loc;
24962641
24972642 ir_ref_instruction(ptr, ira->new_irb.current_basic_block);
2643 if (result_loc != nullptr) ir_ref_instruction(result_loc, ira->new_irb.current_basic_block);
24982644
24992645 return &instruction->base;
25002646}
25012647
2502static IrInstruction *ir_build_bit_cast(IrBuilder *irb, Scope *scope, AstNode *source_node,
2503 IrInstruction *dest_type, IrInstruction *value)
2648static IrInstruction *ir_build_bit_cast_src(IrBuilder *irb, Scope *scope, AstNode *source_node,
2649 IrInstruction *operand, ResultLocBitCast *result_loc_bit_cast)
25042650{
2505 IrInstructionBitCast *instruction = ir_build_instruction<IrInstructionBitCast>(
2506 irb, scope, source_node);
2507 instruction->dest_type = dest_type;
2508 instruction->value = value;
2651 IrInstructionBitCastSrc *instruction = ir_build_instruction<IrInstructionBitCastSrc>(irb, scope, source_node);
2652 instruction->operand = operand;
2653 instruction->result_loc_bit_cast = result_loc_bit_cast;
25092654
2510 ir_ref_instruction(dest_type, irb->current_basic_block);
2511 ir_ref_instruction(value, irb->current_basic_block);
2655 ir_ref_instruction(operand, irb->current_basic_block);
25122656
25132657 return &instruction->base;
25142658}
......@@ -2660,11 +2804,8 @@ static IrInstruction *ir_build_type_name(IrBuilder *irb, Scope *scope, AstNode *
26602804 return &instruction->base;
26612805}
26622806
2663static IrInstruction *ir_build_decl_ref(IrBuilder *irb, Scope *scope, AstNode *source_node,
2664 Tld *tld, LVal lval)
2665{
2666 IrInstructionDeclRef *instruction = ir_build_instruction<IrInstructionDeclRef>(
2667 irb, scope, source_node);
2807static IrInstruction *ir_build_decl_ref(IrBuilder *irb, Scope *scope, AstNode *source_node, Tld *tld, LVal lval) {
2808 IrInstructionDeclRef *instruction = ir_build_instruction<IrInstructionDeclRef>(irb, scope, source_node);
26682809 instruction->tld = tld;
26692810 instruction->lval = lval;
26702811
......@@ -2792,6 +2933,53 @@ static IrInstruction *ir_build_align_cast(IrBuilder *irb, Scope *scope, AstNode
27922933 return &instruction->base;
27932934}
27942935
2936static IrInstruction *ir_build_implicit_cast(IrBuilder *irb, Scope *scope, AstNode *source_node,
2937 IrInstruction *dest_type, IrInstruction *target, ResultLoc *result_loc)
2938{
2939 IrInstructionImplicitCast *instruction = ir_build_instruction<IrInstructionImplicitCast>(irb, scope, source_node);
2940 instruction->dest_type = dest_type;
2941 instruction->target = target;
2942 instruction->result_loc = result_loc;
2943
2944 ir_ref_instruction(dest_type, irb->current_basic_block);
2945 ir_ref_instruction(target, irb->current_basic_block);
2946
2947 return &instruction->base;
2948}
2949
2950static IrInstruction *ir_build_resolve_result(IrBuilder *irb, Scope *scope, AstNode *source_node,
2951 ResultLoc *result_loc, IrInstruction *ty)
2952{
2953 IrInstructionResolveResult *instruction = ir_build_instruction<IrInstructionResolveResult>(irb, scope, source_node);
2954 instruction->result_loc = result_loc;
2955 instruction->ty = ty;
2956
2957 ir_ref_instruction(ty, irb->current_basic_block);
2958
2959 return &instruction->base;
2960}
2961
2962static IrInstruction *ir_build_reset_result(IrBuilder *irb, Scope *scope, AstNode *source_node,
2963 ResultLoc *result_loc)
2964{
2965 IrInstructionResetResult *instruction = ir_build_instruction<IrInstructionResetResult>(irb, scope, source_node);
2966 instruction->result_loc = result_loc;
2967
2968 return &instruction->base;
2969}
2970
2971static IrInstruction *ir_build_result_ptr(IrBuilder *irb, Scope *scope, AstNode *source_node,
2972 ResultLoc *result_loc, IrInstruction *result)
2973{
2974 IrInstructionResultPtr *instruction = ir_build_instruction<IrInstructionResultPtr>(irb, scope, source_node);
2975 instruction->result_loc = result_loc;
2976 instruction->result = result;
2977
2978 ir_ref_instruction(result, irb->current_basic_block);
2979
2980 return &instruction->base;
2981}
2982
27952983static IrInstruction *ir_build_opaque_type(IrBuilder *irb, Scope *scope, AstNode *source_node) {
27962984 IrInstructionOpaqueType *instruction = ir_build_instruction<IrInstructionOpaqueType>(irb, scope, source_node);
27972985
......@@ -3120,16 +3308,31 @@ static IrInstruction *ir_build_check_runtime_scope(IrBuilder *irb, Scope *scope,
31203308}
31213309
31223310static IrInstruction *ir_build_vector_to_array(IrAnalyze *ira, IrInstruction *source_instruction,
3123 IrInstruction *vector, ZigType *result_type)
3311 ZigType *result_type, IrInstruction *vector, IrInstruction *result_loc)
31243312{
31253313 IrInstructionVectorToArray *instruction = ir_build_instruction<IrInstructionVectorToArray>(&ira->new_irb,
31263314 source_instruction->scope, source_instruction->source_node);
31273315 instruction->base.value.type = result_type;
31283316 instruction->vector = vector;
3317 instruction->result_loc = result_loc;
31293318
31303319 ir_ref_instruction(vector, ira->new_irb.current_basic_block);
3320 ir_ref_instruction(result_loc, ira->new_irb.current_basic_block);
3321
3322 return &instruction->base;
3323}
3324
3325static IrInstruction *ir_build_ptr_of_array_to_slice(IrAnalyze *ira, IrInstruction *source_instruction,
3326 ZigType *result_type, IrInstruction *operand, IrInstruction *result_loc)
3327{
3328 IrInstructionPtrOfArrayToSlice *instruction = ir_build_instruction<IrInstructionPtrOfArrayToSlice>(&ira->new_irb,
3329 source_instruction->scope, source_instruction->source_node);
3330 instruction->base.value.type = result_type;
3331 instruction->operand = operand;
3332 instruction->result_loc = result_loc;
31313333
3132 ir_add_alloca(ira, &instruction->base, result_type);
3334 ir_ref_instruction(operand, ira->new_irb.current_basic_block);
3335 ir_ref_instruction(result_loc, ira->new_irb.current_basic_block);
31333336
31343337 return &instruction->base;
31353338}
......@@ -3173,6 +3376,45 @@ static IrInstruction *ir_build_assert_non_null(IrAnalyze *ira, IrInstruction *so
31733376 return &instruction->base;
31743377}
31753378
3379static IrInstruction *ir_build_alloca_src(IrBuilder *irb, Scope *scope, AstNode *source_node,
3380 IrInstruction *align, const char *name_hint, IrInstruction *is_comptime)
3381{
3382 IrInstructionAllocaSrc *instruction = ir_build_instruction<IrInstructionAllocaSrc>(irb, scope, source_node);
3383 instruction->base.is_gen = true;
3384 instruction->align = align;
3385 instruction->name_hint = name_hint;
3386 instruction->is_comptime = is_comptime;
3387
3388 if (align != nullptr) ir_ref_instruction(align, irb->current_basic_block);
3389 if (is_comptime != nullptr) ir_ref_instruction(is_comptime, irb->current_basic_block);
3390
3391 return &instruction->base;
3392}
3393
3394static IrInstructionAllocaGen *ir_create_alloca_gen(IrAnalyze *ira, IrInstruction *source_instruction,
3395 uint32_t align, const char *name_hint)
3396{
3397 IrInstructionAllocaGen *instruction = ir_create_instruction<IrInstructionAllocaGen>(&ira->new_irb,
3398 source_instruction->scope, source_instruction->source_node);
3399 instruction->align = align;
3400 instruction->name_hint = name_hint;
3401
3402 return instruction;
3403}
3404
3405static IrInstruction *ir_build_end_expr(IrBuilder *irb, Scope *scope, AstNode *source_node,
3406 IrInstruction *value, ResultLoc *result_loc)
3407{
3408 IrInstructionEndExpr *instruction = ir_build_instruction<IrInstructionEndExpr>(irb, scope, source_node);
3409 instruction->base.is_gen = true;
3410 instruction->value = value;
3411 instruction->result_loc = result_loc;
3412
3413 ir_ref_instruction(value, irb->current_basic_block);
3414
3415 return &instruction->base;
3416}
3417
31763418static void ir_count_defers(IrBuilder *irb, Scope *inner_scope, Scope *outer_scope, size_t *results) {
31773419 results[ReturnKindUnconditional] = 0;
31783420 results[ReturnKindError] = 0;
......@@ -3273,6 +3515,7 @@ static void ir_set_cursor_at_end(IrBuilder *irb, IrBasicBlock *basic_block) {
32733515}
32743516
32753517static void ir_set_cursor_at_end_and_append_block(IrBuilder *irb, IrBasicBlock *basic_block) {
3518 basic_block->index = irb->exec->basic_block_list.length;
32763519 irb->exec->basic_block_list.append(basic_block);
32773520 ir_set_cursor_at_end(irb, basic_block);
32783521}
......@@ -3361,7 +3604,7 @@ static IrInstruction *ir_gen_async_return(IrBuilder *irb, Scope *scope, AstNode
33613604 return ir_build_cond_br(irb, scope, node, is_canceled_bool, irb->exec->coro_final_cleanup_block, irb->exec->coro_early_final, is_comptime);
33623605}
33633606
3364static IrInstruction *ir_gen_return(IrBuilder *irb, Scope *scope, AstNode *node, LVal lval) {
3607static IrInstruction *ir_gen_return(IrBuilder *irb, Scope *scope, AstNode *node, LVal lval, ResultLoc *result_loc) {
33653608 assert(node->type == NodeTypeReturnExpr);
33663609
33673610 ZigFn *fn_entry = exec_fn_entry(irb->exec);
......@@ -3385,12 +3628,16 @@ static IrInstruction *ir_gen_return(IrBuilder *irb, Scope *scope, AstNode *node,
33853628 switch (node->data.return_expr.kind) {
33863629 case ReturnKindUnconditional:
33873630 {
3631 ResultLocReturn *result_loc_ret = allocate<ResultLocReturn>(1);
3632 result_loc_ret->base.id = ResultLocIdReturn;
3633 ir_build_reset_result(irb, scope, node, &result_loc_ret->base);
3634
33883635 IrInstruction *return_value;
33893636 if (expr_node) {
33903637 // Temporarily set this so that if we return a type it gets the name of the function
33913638 ZigFn *prev_name_fn = irb->exec->name_fn;
33923639 irb->exec->name_fn = exec_fn_entry(irb->exec);
3393 return_value = ir_gen_node(irb, expr_node, scope);
3640 return_value = ir_gen_node_extra(irb, expr_node, scope, LValNone, &result_loc_ret->base);
33943641 irb->exec->name_fn = prev_name_fn;
33953642 if (return_value == irb->codegen->invalid_instruction)
33963643 return irb->codegen->invalid_instruction;
......@@ -3408,7 +3655,9 @@ static IrInstruction *ir_gen_return(IrBuilder *irb, Scope *scope, AstNode *node,
34083655 ir_gen_defers_for_block(irb, scope, outer_scope, false);
34093656 }
34103657
3411 IrInstruction *is_err = ir_build_test_err(irb, scope, node, return_value);
3658 IrInstruction *ret_ptr = ir_build_result_ptr(irb, scope, node, &result_loc_ret->base,
3659 return_value);
3660 IrInstruction *is_err = ir_build_test_err_src(irb, scope, node, ret_ptr, false);
34123661
34133662 bool should_inline = ir_should_inline(irb->exec, scope);
34143663 IrInstruction *is_comptime;
......@@ -3437,21 +3686,24 @@ static IrInstruction *ir_gen_return(IrBuilder *irb, Scope *scope, AstNode *node,
34373686 ir_build_br(irb, scope, node, ret_stmt_block, is_comptime);
34383687
34393688 ir_set_cursor_at_end_and_append_block(irb, ret_stmt_block);
3440 return ir_gen_async_return(irb, scope, node, return_value, false);
3689 IrInstruction *result = ir_gen_async_return(irb, scope, node, return_value, false);
3690 result_loc_ret->base.source_instruction = result;
3691 return result;
34413692 } else {
34423693 // generate unconditional defers
34433694 ir_gen_defers_for_block(irb, scope, outer_scope, false);
3444 return ir_gen_async_return(irb, scope, node, return_value, false);
3695 IrInstruction *result = ir_gen_async_return(irb, scope, node, return_value, false);
3696 result_loc_ret->base.source_instruction = result;
3697 return result;
34453698 }
34463699 }
34473700 case ReturnKindError:
34483701 {
34493702 assert(expr_node);
3450 IrInstruction *err_union_ptr = ir_gen_node_extra(irb, expr_node, scope, LValPtr);
3703 IrInstruction *err_union_ptr = ir_gen_node_extra(irb, expr_node, scope, LValPtr, nullptr);
34513704 if (err_union_ptr == irb->codegen->invalid_instruction)
34523705 return irb->codegen->invalid_instruction;
3453 IrInstruction *err_union_val = ir_build_load_ptr(irb, scope, node, err_union_ptr);
3454 IrInstruction *is_err_val = ir_build_test_err(irb, scope, node, err_union_val);
3706 IrInstruction *is_err_val = ir_build_test_err_src(irb, scope, node, err_union_ptr, true);
34553707
34563708 IrBasicBlock *return_block = ir_create_basic_block(irb, scope, "ErrRetReturn");
34573709 IrBasicBlock *continue_block = ir_create_basic_block(irb, scope, "ErrRetContinue");
......@@ -3466,19 +3718,27 @@ static IrInstruction *ir_gen_return(IrBuilder *irb, Scope *scope, AstNode *node,
34663718
34673719 ir_set_cursor_at_end_and_append_block(irb, return_block);
34683720 if (!ir_gen_defers_for_block(irb, scope, outer_scope, true)) {
3469 IrInstruction *err_val = ir_build_unwrap_err_code(irb, scope, node, err_union_ptr);
3721 IrInstruction *err_val_ptr = ir_build_unwrap_err_code(irb, scope, node, err_union_ptr);
3722 IrInstruction *err_val = ir_build_load_ptr(irb, scope, node, err_val_ptr);
3723
3724 ResultLocReturn *result_loc_ret = allocate<ResultLocReturn>(1);
3725 result_loc_ret->base.id = ResultLocIdReturn;
3726 ir_build_reset_result(irb, scope, node, &result_loc_ret->base);
3727 ir_build_end_expr(irb, scope, node, err_val, &result_loc_ret->base);
3728
34703729 if (irb->codegen->have_err_ret_tracing && !should_inline) {
34713730 ir_build_save_err_ret_addr(irb, scope, node);
34723731 }
3473 ir_gen_async_return(irb, scope, node, err_val, false);
3732 IrInstruction *ret_inst = ir_gen_async_return(irb, scope, node, err_val, false);
3733 result_loc_ret->base.source_instruction = ret_inst;
34743734 }
34753735
34763736 ir_set_cursor_at_end_and_append_block(irb, continue_block);
3477 IrInstruction *unwrapped_ptr = ir_build_unwrap_err_payload(irb, scope, node, err_union_ptr, false);
3737 IrInstruction *unwrapped_ptr = ir_build_unwrap_err_payload(irb, scope, node, err_union_ptr, false, false);
34783738 if (lval == LValPtr)
34793739 return unwrapped_ptr;
34803740 else
3481 return ir_build_load_ptr(irb, scope, node, unwrapped_ptr);
3741 return ir_expr_wrap(irb, scope, ir_build_load_ptr(irb, scope, node, unwrapped_ptr), result_loc);
34823742 }
34833743 }
34843744 zig_unreachable();
......@@ -3562,7 +3822,17 @@ static ZigVar *ir_create_var(IrBuilder *irb, AstNode *node, Scope *scope, Buf *n
35623822 return var;
35633823}
35643824
3565static IrInstruction *ir_gen_block(IrBuilder *irb, Scope *parent_scope, AstNode *block_node) {
3825static ResultLocPeer *create_peer_result(ResultLocPeerParent *peer_parent) {
3826 ResultLocPeer *result = allocate<ResultLocPeer>(1);
3827 result->base.id = ResultLocIdPeer;
3828 result->base.source_instruction = peer_parent->base.source_instruction;
3829 result->parent = peer_parent;
3830 return result;
3831}
3832
3833static IrInstruction *ir_gen_block(IrBuilder *irb, Scope *parent_scope, AstNode *block_node, LVal lval,
3834 ResultLoc *result_loc)
3835{
35663836 assert(block_node->type == NodeTypeBlock);
35673837
35683838 ZigList<IrInstruction *> incoming_values = {0};
......@@ -3580,15 +3850,24 @@ static IrInstruction *ir_gen_block(IrBuilder *irb, Scope *parent_scope, AstNode
35803850
35813851 if (block_node->data.block.statements.length == 0) {
35823852 // {}
3583 return ir_build_const_void(irb, child_scope, block_node);
3853 return ir_lval_wrap(irb, parent_scope, ir_build_const_void(irb, child_scope, block_node), lval, result_loc);
35843854 }
35853855
35863856 if (block_node->data.block.name != nullptr) {
3857 scope_block->lval = lval;
35873858 scope_block->incoming_blocks = &incoming_blocks;
35883859 scope_block->incoming_values = &incoming_values;
35893860 scope_block->end_block = ir_create_basic_block(irb, parent_scope, "BlockEnd");
35903861 scope_block->is_comptime = ir_build_const_bool(irb, parent_scope, block_node,
35913862 ir_should_inline(irb->exec, parent_scope));
3863
3864 scope_block->peer_parent = allocate<ResultLocPeerParent>(1);
3865 scope_block->peer_parent->base.id = ResultLocIdPeerParent;
3866 scope_block->peer_parent->base.source_instruction = scope_block->is_comptime;
3867 scope_block->peer_parent->end_bb = scope_block->end_block;
3868 scope_block->peer_parent->is_comptime = scope_block->is_comptime;
3869 scope_block->peer_parent->parent = result_loc;
3870 ir_build_reset_result(irb, parent_scope, block_node, &scope_block->peer_parent->base);
35923871 }
35933872
35943873 bool is_continuation_unreachable = false;
......@@ -3602,6 +3881,8 @@ static IrInstruction *ir_gen_block(IrBuilder *irb, Scope *parent_scope, AstNode
36023881 // keep the last noreturn statement value around in case we need to return it
36033882 noreturn_return_value = statement_value;
36043883 }
3884 // This logic must be kept in sync with
3885 // [STMT_EXPR_TEST_THING] <--- (search this token)
36053886 if (statement_node->type == NodeTypeDefer && statement_value != irb->codegen->invalid_instruction) {
36063887 // defer starts a new scope
36073888 child_scope = statement_node->data.defer.child_scope;
......@@ -3622,21 +3903,41 @@ static IrInstruction *ir_gen_block(IrBuilder *irb, Scope *parent_scope, AstNode
36223903 return noreturn_return_value;
36233904 }
36243905
3906 if (scope_block->peer_parent != nullptr && scope_block->peer_parent->peers.length != 0) {
3907 scope_block->peer_parent->peers.last()->next_bb = scope_block->end_block;
3908 }
36253909 ir_set_cursor_at_end_and_append_block(irb, scope_block->end_block);
3626 return ir_build_phi(irb, parent_scope, block_node, incoming_blocks.length, incoming_blocks.items, incoming_values.items);
3910 IrInstruction *phi = ir_build_phi(irb, parent_scope, block_node, incoming_blocks.length,
3911 incoming_blocks.items, incoming_values.items, scope_block->peer_parent);
3912 return ir_expr_wrap(irb, parent_scope, phi, result_loc);
36273913 } else {
36283914 incoming_blocks.append(irb->current_basic_block);
3629 incoming_values.append(ir_mark_gen(ir_build_const_void(irb, parent_scope, block_node)));
3915 IrInstruction *else_expr_result = ir_mark_gen(ir_build_const_void(irb, parent_scope, block_node));
3916
3917 if (scope_block->peer_parent != nullptr) {
3918 ResultLocPeer *peer_result = create_peer_result(scope_block->peer_parent);
3919 scope_block->peer_parent->peers.append(peer_result);
3920 ir_build_end_expr(irb, parent_scope, block_node, else_expr_result, &peer_result->base);
3921
3922 if (scope_block->peer_parent->peers.length != 0) {
3923 scope_block->peer_parent->peers.last()->next_bb = scope_block->end_block;
3924 }
3925 }
3926
3927 incoming_values.append(else_expr_result);
36303928 }
36313929
36323930 if (block_node->data.block.name != nullptr) {
36333931 ir_gen_defers_for_block(irb, child_scope, outer_block_scope, false);
36343932 ir_mark_gen(ir_build_br(irb, parent_scope, block_node, scope_block->end_block, scope_block->is_comptime));
36353933 ir_set_cursor_at_end_and_append_block(irb, scope_block->end_block);
3636 return ir_build_phi(irb, parent_scope, block_node, incoming_blocks.length, incoming_blocks.items, incoming_values.items);
3934 IrInstruction *phi = ir_build_phi(irb, parent_scope, block_node, incoming_blocks.length,
3935 incoming_blocks.items, incoming_values.items, scope_block->peer_parent);
3936 return ir_expr_wrap(irb, parent_scope, phi, result_loc);
36373937 } else {
36383938 ir_gen_defers_for_block(irb, child_scope, outer_block_scope, false);
3639 return ir_mark_gen(ir_mark_gen(ir_build_const_void(irb, child_scope, block_node)));
3939 IrInstruction *void_inst = ir_mark_gen(ir_build_const_void(irb, child_scope, block_node));
3940 return ir_lval_wrap(irb, parent_scope, void_inst, lval, result_loc);
36403941 }
36413942}
36423943
......@@ -3656,7 +3957,7 @@ static IrInstruction *ir_gen_bin_op_id(IrBuilder *irb, Scope *scope, AstNode *no
36563957}
36573958
36583959static IrInstruction *ir_gen_assign(IrBuilder *irb, Scope *scope, AstNode *node) {
3659 IrInstruction *lvalue = ir_gen_node_extra(irb, node->data.bin_op_expr.op1, scope, LValPtr);
3960 IrInstruction *lvalue = ir_gen_node_extra(irb, node->data.bin_op_expr.op1, scope, LValPtr, nullptr);
36603961 IrInstruction *rvalue = ir_gen_node(irb, node->data.bin_op_expr.op2, scope);
36613962
36623963 if (lvalue == irb->codegen->invalid_instruction || rvalue == irb->codegen->invalid_instruction)
......@@ -3667,7 +3968,7 @@ static IrInstruction *ir_gen_assign(IrBuilder *irb, Scope *scope, AstNode *node)
36673968}
36683969
36693970static IrInstruction *ir_gen_assign_op(IrBuilder *irb, Scope *scope, AstNode *node, IrBinOp op_id) {
3670 IrInstruction *lvalue = ir_gen_node_extra(irb, node->data.bin_op_expr.op1, scope, LValPtr);
3971 IrInstruction *lvalue = ir_gen_node_extra(irb, node->data.bin_op_expr.op1, scope, LValPtr, nullptr);
36713972 if (lvalue == irb->codegen->invalid_instruction)
36723973 return lvalue;
36733974 IrInstruction *op1 = ir_build_load_ptr(irb, scope, node->data.bin_op_expr.op1, lvalue);
......@@ -3718,7 +4019,7 @@ static IrInstruction *ir_gen_bool_or(IrBuilder *irb, Scope *scope, AstNode *node
37184019 incoming_blocks[0] = post_val1_block;
37194020 incoming_blocks[1] = post_val2_block;
37204021
3721 return ir_build_phi(irb, scope, node, 2, incoming_blocks, incoming_values);
4022 return ir_build_phi(irb, scope, node, 2, incoming_blocks, incoming_values, nullptr);
37224023}
37234024
37244025static IrInstruction *ir_gen_bool_and(IrBuilder *irb, Scope *scope, AstNode *node) {
......@@ -3760,16 +4061,51 @@ static IrInstruction *ir_gen_bool_and(IrBuilder *irb, Scope *scope, AstNode *nod
37604061 incoming_blocks[0] = post_val1_block;
37614062 incoming_blocks[1] = post_val2_block;
37624063
3763 return ir_build_phi(irb, scope, node, 2, incoming_blocks, incoming_values);
4064 return ir_build_phi(irb, scope, node, 2, incoming_blocks, incoming_values, nullptr);
4065}
4066
4067static ResultLocPeerParent *ir_build_result_peers(IrBuilder *irb, IrInstruction *cond_br_inst,
4068 IrBasicBlock *end_block, ResultLoc *parent, IrInstruction *is_comptime)
4069{
4070 ResultLocPeerParent *peer_parent = allocate<ResultLocPeerParent>(1);
4071 peer_parent->base.id = ResultLocIdPeerParent;
4072 peer_parent->base.source_instruction = cond_br_inst;
4073 peer_parent->end_bb = end_block;
4074 peer_parent->is_comptime = is_comptime;
4075 peer_parent->parent = parent;
4076
4077 IrInstruction *popped_inst = irb->current_basic_block->instruction_list.pop();
4078 ir_assert(popped_inst == cond_br_inst, cond_br_inst);
4079
4080 ir_build_reset_result(irb, cond_br_inst->scope, cond_br_inst->source_node, &peer_parent->base);
4081 irb->current_basic_block->instruction_list.append(popped_inst);
4082
4083 return peer_parent;
37644084}
37654085
3766static IrInstruction *ir_gen_orelse(IrBuilder *irb, Scope *parent_scope, AstNode *node) {
4086static ResultLocPeerParent *ir_build_binary_result_peers(IrBuilder *irb, IrInstruction *cond_br_inst,
4087 IrBasicBlock *else_block, IrBasicBlock *end_block, ResultLoc *parent, IrInstruction *is_comptime)
4088{
4089 ResultLocPeerParent *peer_parent = ir_build_result_peers(irb, cond_br_inst, end_block, parent, is_comptime);
4090
4091 peer_parent->peers.append(create_peer_result(peer_parent));
4092 peer_parent->peers.last()->next_bb = else_block;
4093
4094 peer_parent->peers.append(create_peer_result(peer_parent));
4095 peer_parent->peers.last()->next_bb = end_block;
4096
4097 return peer_parent;
4098}
4099
4100static IrInstruction *ir_gen_orelse(IrBuilder *irb, Scope *parent_scope, AstNode *node, LVal lval,
4101 ResultLoc *result_loc)
4102{
37674103 assert(node->type == NodeTypeBinOpExpr);
37684104
37694105 AstNode *op1_node = node->data.bin_op_expr.op1;
37704106 AstNode *op2_node = node->data.bin_op_expr.op2;
37714107
3772 IrInstruction *maybe_ptr = ir_gen_node_extra(irb, op1_node, parent_scope, LValPtr);
4108 IrInstruction *maybe_ptr = ir_gen_node_extra(irb, op1_node, parent_scope, LValPtr, nullptr);
37734109 if (maybe_ptr == irb->codegen->invalid_instruction)
37744110 return irb->codegen->invalid_instruction;
37754111
......@@ -3786,10 +4122,14 @@ static IrInstruction *ir_gen_orelse(IrBuilder *irb, Scope *parent_scope, AstNode
37864122 IrBasicBlock *ok_block = ir_create_basic_block(irb, parent_scope, "OptionalNonNull");
37874123 IrBasicBlock *null_block = ir_create_basic_block(irb, parent_scope, "OptionalNull");
37884124 IrBasicBlock *end_block = ir_create_basic_block(irb, parent_scope, "OptionalEnd");
3789 ir_build_cond_br(irb, parent_scope, node, is_non_null, ok_block, null_block, is_comptime);
4125 IrInstruction *cond_br_inst = ir_build_cond_br(irb, parent_scope, node, is_non_null, ok_block, null_block, is_comptime);
4126
4127 ResultLocPeerParent *peer_parent = ir_build_binary_result_peers(irb, cond_br_inst, ok_block, end_block,
4128 result_loc, is_comptime);
37904129
37914130 ir_set_cursor_at_end_and_append_block(irb, null_block);
3792 IrInstruction *null_result = ir_gen_node(irb, op2_node, parent_scope);
4131 IrInstruction *null_result = ir_gen_node_extra(irb, op2_node, parent_scope, LValNone,
4132 &peer_parent->peers.at(0)->base);
37934133 if (null_result == irb->codegen->invalid_instruction)
37944134 return irb->codegen->invalid_instruction;
37954135 IrBasicBlock *after_null_block = irb->current_basic_block;
......@@ -3797,8 +4137,9 @@ static IrInstruction *ir_gen_orelse(IrBuilder *irb, Scope *parent_scope, AstNode
37974137 ir_mark_gen(ir_build_br(irb, parent_scope, node, end_block, is_comptime));
37984138
37994139 ir_set_cursor_at_end_and_append_block(irb, ok_block);
3800 IrInstruction *unwrapped_ptr = ir_build_optional_unwrap_ptr(irb, parent_scope, node, maybe_ptr, false);
4140 IrInstruction *unwrapped_ptr = ir_build_optional_unwrap_ptr(irb, parent_scope, node, maybe_ptr, false, false);
38014141 IrInstruction *unwrapped_payload = ir_build_load_ptr(irb, parent_scope, node, unwrapped_ptr);
4142 ir_build_end_expr(irb, parent_scope, node, unwrapped_payload, &peer_parent->peers.at(1)->base);
38024143 IrBasicBlock *after_ok_block = irb->current_basic_block;
38034144 ir_build_br(irb, parent_scope, node, end_block, is_comptime);
38044145
......@@ -3809,7 +4150,8 @@ static IrInstruction *ir_gen_orelse(IrBuilder *irb, Scope *parent_scope, AstNode
38094150 IrBasicBlock **incoming_blocks = allocate<IrBasicBlock *>(2);
38104151 incoming_blocks[0] = after_null_block;
38114152 incoming_blocks[1] = after_ok_block;
3812 return ir_build_phi(irb, parent_scope, node, 2, incoming_blocks, incoming_values);
4153 IrInstruction *phi = ir_build_phi(irb, parent_scope, node, 2, incoming_blocks, incoming_values, peer_parent);
4154 return ir_lval_wrap(irb, parent_scope, phi, lval, result_loc);
38134155}
38144156
38154157static IrInstruction *ir_gen_error_union(IrBuilder *irb, Scope *parent_scope, AstNode *node) {
......@@ -3829,7 +4171,7 @@ static IrInstruction *ir_gen_error_union(IrBuilder *irb, Scope *parent_scope, As
38294171 return ir_build_error_union(irb, parent_scope, node, err_set, payload);
38304172}
38314173
3832static IrInstruction *ir_gen_bin_op(IrBuilder *irb, Scope *scope, AstNode *node) {
4174static IrInstruction *ir_gen_bin_op(IrBuilder *irb, Scope *scope, AstNode *node, LVal lval, ResultLoc *result_loc) {
38334175 assert(node->type == NodeTypeBinOpExpr);
38344176
38354177 BinOpType bin_op_type = node->data.bin_op_expr.bin_op;
......@@ -3837,87 +4179,87 @@ static IrInstruction *ir_gen_bin_op(IrBuilder *irb, Scope *scope, AstNode *node)
38374179 case BinOpTypeInvalid:
38384180 zig_unreachable();
38394181 case BinOpTypeAssign:
3840 return ir_gen_assign(irb, scope, node);
4182 return ir_lval_wrap(irb, scope, ir_gen_assign(irb, scope, node), lval, result_loc);
38414183 case BinOpTypeAssignTimes:
3842 return ir_gen_assign_op(irb, scope, node, IrBinOpMult);
4184 return ir_lval_wrap(irb, scope, ir_gen_assign_op(irb, scope, node, IrBinOpMult), lval, result_loc);
38434185 case BinOpTypeAssignTimesWrap:
3844 return ir_gen_assign_op(irb, scope, node, IrBinOpMultWrap);
4186 return ir_lval_wrap(irb, scope, ir_gen_assign_op(irb, scope, node, IrBinOpMultWrap), lval, result_loc);
38454187 case BinOpTypeAssignDiv:
3846 return ir_gen_assign_op(irb, scope, node, IrBinOpDivUnspecified);
4188 return ir_lval_wrap(irb, scope, ir_gen_assign_op(irb, scope, node, IrBinOpDivUnspecified), lval, result_loc);
38474189 case BinOpTypeAssignMod:
3848 return ir_gen_assign_op(irb, scope, node, IrBinOpRemUnspecified);
4190 return ir_lval_wrap(irb, scope, ir_gen_assign_op(irb, scope, node, IrBinOpRemUnspecified), lval, result_loc);
38494191 case BinOpTypeAssignPlus:
3850 return ir_gen_assign_op(irb, scope, node, IrBinOpAdd);
4192 return ir_lval_wrap(irb, scope, ir_gen_assign_op(irb, scope, node, IrBinOpAdd), lval, result_loc);
38514193 case BinOpTypeAssignPlusWrap:
3852 return ir_gen_assign_op(irb, scope, node, IrBinOpAddWrap);
4194 return ir_lval_wrap(irb, scope, ir_gen_assign_op(irb, scope, node, IrBinOpAddWrap), lval, result_loc);
38534195 case BinOpTypeAssignMinus:
3854 return ir_gen_assign_op(irb, scope, node, IrBinOpSub);
4196 return ir_lval_wrap(irb, scope, ir_gen_assign_op(irb, scope, node, IrBinOpSub), lval, result_loc);
38554197 case BinOpTypeAssignMinusWrap:
3856 return ir_gen_assign_op(irb, scope, node, IrBinOpSubWrap);
4198 return ir_lval_wrap(irb, scope, ir_gen_assign_op(irb, scope, node, IrBinOpSubWrap), lval, result_loc);
38574199 case BinOpTypeAssignBitShiftLeft:
3858 return ir_gen_assign_op(irb, scope, node, IrBinOpBitShiftLeftLossy);
4200 return ir_lval_wrap(irb, scope, ir_gen_assign_op(irb, scope, node, IrBinOpBitShiftLeftLossy), lval, result_loc);
38594201 case BinOpTypeAssignBitShiftRight:
3860 return ir_gen_assign_op(irb, scope, node, IrBinOpBitShiftRightLossy);
4202 return ir_lval_wrap(irb, scope, ir_gen_assign_op(irb, scope, node, IrBinOpBitShiftRightLossy), lval, result_loc);
38614203 case BinOpTypeAssignBitAnd:
3862 return ir_gen_assign_op(irb, scope, node, IrBinOpBinAnd);
4204 return ir_lval_wrap(irb, scope, ir_gen_assign_op(irb, scope, node, IrBinOpBinAnd), lval, result_loc);
38634205 case BinOpTypeAssignBitXor:
3864 return ir_gen_assign_op(irb, scope, node, IrBinOpBinXor);
4206 return ir_lval_wrap(irb, scope, ir_gen_assign_op(irb, scope, node, IrBinOpBinXor), lval, result_loc);
38654207 case BinOpTypeAssignBitOr:
3866 return ir_gen_assign_op(irb, scope, node, IrBinOpBinOr);
4208 return ir_lval_wrap(irb, scope, ir_gen_assign_op(irb, scope, node, IrBinOpBinOr), lval, result_loc);
38674209 case BinOpTypeAssignMergeErrorSets:
3868 return ir_gen_assign_op(irb, scope, node, IrBinOpMergeErrorSets);
4210 return ir_lval_wrap(irb, scope, ir_gen_assign_op(irb, scope, node, IrBinOpMergeErrorSets), lval, result_loc);
38694211 case BinOpTypeBoolOr:
3870 return ir_gen_bool_or(irb, scope, node);
4212 return ir_lval_wrap(irb, scope, ir_gen_bool_or(irb, scope, node), lval, result_loc);
38714213 case BinOpTypeBoolAnd:
3872 return ir_gen_bool_and(irb, scope, node);
4214 return ir_lval_wrap(irb, scope, ir_gen_bool_and(irb, scope, node), lval, result_loc);
38734215 case BinOpTypeCmpEq:
3874 return ir_gen_bin_op_id(irb, scope, node, IrBinOpCmpEq);
4216 return ir_lval_wrap(irb, scope, ir_gen_bin_op_id(irb, scope, node, IrBinOpCmpEq), lval, result_loc);
38754217 case BinOpTypeCmpNotEq:
3876 return ir_gen_bin_op_id(irb, scope, node, IrBinOpCmpNotEq);
4218 return ir_lval_wrap(irb, scope, ir_gen_bin_op_id(irb, scope, node, IrBinOpCmpNotEq), lval, result_loc);
38774219 case BinOpTypeCmpLessThan:
3878 return ir_gen_bin_op_id(irb, scope, node, IrBinOpCmpLessThan);
4220 return ir_lval_wrap(irb, scope, ir_gen_bin_op_id(irb, scope, node, IrBinOpCmpLessThan), lval, result_loc);
38794221 case BinOpTypeCmpGreaterThan:
3880 return ir_gen_bin_op_id(irb, scope, node, IrBinOpCmpGreaterThan);
4222 return ir_lval_wrap(irb, scope, ir_gen_bin_op_id(irb, scope, node, IrBinOpCmpGreaterThan), lval, result_loc);
38814223 case BinOpTypeCmpLessOrEq:
3882 return ir_gen_bin_op_id(irb, scope, node, IrBinOpCmpLessOrEq);
4224 return ir_lval_wrap(irb, scope, ir_gen_bin_op_id(irb, scope, node, IrBinOpCmpLessOrEq), lval, result_loc);
38834225 case BinOpTypeCmpGreaterOrEq:
3884 return ir_gen_bin_op_id(irb, scope, node, IrBinOpCmpGreaterOrEq);
4226 return ir_lval_wrap(irb, scope, ir_gen_bin_op_id(irb, scope, node, IrBinOpCmpGreaterOrEq), lval, result_loc);
38854227 case BinOpTypeBinOr:
3886 return ir_gen_bin_op_id(irb, scope, node, IrBinOpBinOr);
4228 return ir_lval_wrap(irb, scope, ir_gen_bin_op_id(irb, scope, node, IrBinOpBinOr), lval, result_loc);
38874229 case BinOpTypeBinXor:
3888 return ir_gen_bin_op_id(irb, scope, node, IrBinOpBinXor);
4230 return ir_lval_wrap(irb, scope, ir_gen_bin_op_id(irb, scope, node, IrBinOpBinXor), lval, result_loc);
38894231 case BinOpTypeBinAnd:
3890 return ir_gen_bin_op_id(irb, scope, node, IrBinOpBinAnd);
4232 return ir_lval_wrap(irb, scope, ir_gen_bin_op_id(irb, scope, node, IrBinOpBinAnd), lval, result_loc);
38914233 case BinOpTypeBitShiftLeft:
3892 return ir_gen_bin_op_id(irb, scope, node, IrBinOpBitShiftLeftLossy);
4234 return ir_lval_wrap(irb, scope, ir_gen_bin_op_id(irb, scope, node, IrBinOpBitShiftLeftLossy), lval, result_loc);
38934235 case BinOpTypeBitShiftRight:
3894 return ir_gen_bin_op_id(irb, scope, node, IrBinOpBitShiftRightLossy);
4236 return ir_lval_wrap(irb, scope, ir_gen_bin_op_id(irb, scope, node, IrBinOpBitShiftRightLossy), lval, result_loc);
38954237 case BinOpTypeAdd:
3896 return ir_gen_bin_op_id(irb, scope, node, IrBinOpAdd);
4238 return ir_lval_wrap(irb, scope, ir_gen_bin_op_id(irb, scope, node, IrBinOpAdd), lval, result_loc);
38974239 case BinOpTypeAddWrap:
3898 return ir_gen_bin_op_id(irb, scope, node, IrBinOpAddWrap);
4240 return ir_lval_wrap(irb, scope, ir_gen_bin_op_id(irb, scope, node, IrBinOpAddWrap), lval, result_loc);
38994241 case BinOpTypeSub:
3900 return ir_gen_bin_op_id(irb, scope, node, IrBinOpSub);
4242 return ir_lval_wrap(irb, scope, ir_gen_bin_op_id(irb, scope, node, IrBinOpSub), lval, result_loc);
39014243 case BinOpTypeSubWrap:
3902 return ir_gen_bin_op_id(irb, scope, node, IrBinOpSubWrap);
4244 return ir_lval_wrap(irb, scope, ir_gen_bin_op_id(irb, scope, node, IrBinOpSubWrap), lval, result_loc);
39034245 case BinOpTypeMult:
3904 return ir_gen_bin_op_id(irb, scope, node, IrBinOpMult);
4246 return ir_lval_wrap(irb, scope, ir_gen_bin_op_id(irb, scope, node, IrBinOpMult), lval, result_loc);
39054247 case BinOpTypeMultWrap:
3906 return ir_gen_bin_op_id(irb, scope, node, IrBinOpMultWrap);
4248 return ir_lval_wrap(irb, scope, ir_gen_bin_op_id(irb, scope, node, IrBinOpMultWrap), lval, result_loc);
39074249 case BinOpTypeDiv:
3908 return ir_gen_bin_op_id(irb, scope, node, IrBinOpDivUnspecified);
4250 return ir_lval_wrap(irb, scope, ir_gen_bin_op_id(irb, scope, node, IrBinOpDivUnspecified), lval, result_loc);
39094251 case BinOpTypeMod:
3910 return ir_gen_bin_op_id(irb, scope, node, IrBinOpRemUnspecified);
4252 return ir_lval_wrap(irb, scope, ir_gen_bin_op_id(irb, scope, node, IrBinOpRemUnspecified), lval, result_loc);
39114253 case BinOpTypeArrayCat:
3912 return ir_gen_bin_op_id(irb, scope, node, IrBinOpArrayCat);
4254 return ir_lval_wrap(irb, scope, ir_gen_bin_op_id(irb, scope, node, IrBinOpArrayCat), lval, result_loc);
39134255 case BinOpTypeArrayMult:
3914 return ir_gen_bin_op_id(irb, scope, node, IrBinOpArrayMult);
4256 return ir_lval_wrap(irb, scope, ir_gen_bin_op_id(irb, scope, node, IrBinOpArrayMult), lval, result_loc);
39154257 case BinOpTypeMergeErrorSets:
3916 return ir_gen_bin_op_id(irb, scope, node, IrBinOpMergeErrorSets);
4258 return ir_lval_wrap(irb, scope, ir_gen_bin_op_id(irb, scope, node, IrBinOpMergeErrorSets), lval, result_loc);
39174259 case BinOpTypeUnwrapOptional:
3918 return ir_gen_orelse(irb, scope, node);
4260 return ir_gen_orelse(irb, scope, node, lval, result_loc);
39194261 case BinOpTypeErrorUnion:
3920 return ir_gen_error_union(irb, scope, node);
4262 return ir_lval_wrap(irb, scope, ir_gen_error_union(irb, scope, node), lval, result_loc);
39214263 }
39224264 zig_unreachable();
39234265}
......@@ -3962,12 +4304,12 @@ static void populate_invalid_variable_in_scope(CodeGen *g, Scope *scope, AstNode
39624304 TldVar *tld_var = allocate<TldVar>(1);
39634305 init_tld(&tld_var->base, TldIdVar, var_name, VisibModPub, node, &scope_decls->base);
39644306 tld_var->base.resolution = TldResolutionInvalid;
3965 tld_var->var = add_variable(g, node, &scope_decls->base, var_name, false,
4307 tld_var->var = add_variable(g, node, &scope_decls->base, var_name, false,
39664308 &g->invalid_instruction->value, &tld_var->base, g->builtin_types.entry_invalid);
39674309 scope_decls->decl_table.put(var_name, &tld_var->base);
39684310}
39694311
3970static IrInstruction *ir_gen_symbol(IrBuilder *irb, Scope *scope, AstNode *node, LVal lval) {
4312static IrInstruction *ir_gen_symbol(IrBuilder *irb, Scope *scope, AstNode *node, LVal lval, ResultLoc *result_loc) {
39714313 Error err;
39724314 assert(node->type == NodeTypeSymbol);
39734315
......@@ -4001,7 +4343,7 @@ static IrInstruction *ir_gen_symbol(IrBuilder *irb, Scope *scope, AstNode *node,
40014343 if (lval == LValPtr) {
40024344 return ir_build_ref(irb, scope, node, value, false, false);
40034345 } else {
4004 return value;
4346 return ir_expr_wrap(irb, scope, value, result_loc);
40054347 }
40064348 }
40074349
......@@ -4009,15 +4351,22 @@ static IrInstruction *ir_gen_symbol(IrBuilder *irb, Scope *scope, AstNode *node,
40094351 ZigVar *var = find_variable(irb->codegen, scope, variable_name, &crossed_fndef_scope);
40104352 if (var) {
40114353 IrInstruction *var_ptr = ir_build_var_ptr_x(irb, scope, node, var, crossed_fndef_scope);
4012 if (lval == LValPtr)
4354 if (lval == LValPtr) {
40134355 return var_ptr;
4014 else
4015 return ir_build_load_ptr(irb, scope, node, var_ptr);
4356 } else {
4357 return ir_expr_wrap(irb, scope, ir_build_load_ptr(irb, scope, node, var_ptr), result_loc);
4358 }
40164359 }
40174360
40184361 Tld *tld = find_decl(irb->codegen, scope, variable_name);
4019 if (tld)
4020 return ir_build_decl_ref(irb, scope, node, tld, lval);
4362 if (tld) {
4363 IrInstruction *decl_ref = ir_build_decl_ref(irb, scope, node, tld, lval);
4364 if (lval == LValPtr) {
4365 return decl_ref;
4366 } else {
4367 return ir_expr_wrap(irb, scope, decl_ref, result_loc);
4368 }
4369 }
40214370
40224371 if (get_container_scope(node->owner)->any_imports_failed) {
40234372 // skip the error message since we had a failing import in this file
......@@ -4028,11 +4377,13 @@ static IrInstruction *ir_gen_symbol(IrBuilder *irb, Scope *scope, AstNode *node,
40284377 return ir_build_undeclared_identifier(irb, scope, node, variable_name);
40294378}
40304379
4031static IrInstruction *ir_gen_array_access(IrBuilder *irb, Scope *scope, AstNode *node, LVal lval) {
4380static IrInstruction *ir_gen_array_access(IrBuilder *irb, Scope *scope, AstNode *node, LVal lval,
4381 ResultLoc *result_loc)
4382{
40324383 assert(node->type == NodeTypeArrayAccessExpr);
40334384
40344385 AstNode *array_ref_node = node->data.array_access_expr.array_ref_expr;
4035 IrInstruction *array_ref_instruction = ir_gen_node_extra(irb, array_ref_node, scope, LValPtr);
4386 IrInstruction *array_ref_instruction = ir_gen_node_extra(irb, array_ref_node, scope, LValPtr, nullptr);
40364387 if (array_ref_instruction == irb->codegen->invalid_instruction)
40374388 return array_ref_instruction;
40384389
......@@ -4042,11 +4393,12 @@ static IrInstruction *ir_gen_array_access(IrBuilder *irb, Scope *scope, AstNode
40424393 return subscript_instruction;
40434394
40444395 IrInstruction *ptr_instruction = ir_build_elem_ptr(irb, scope, node, array_ref_instruction,
4045 subscript_instruction, true, PtrLenSingle);
4396 subscript_instruction, true, PtrLenSingle, nullptr);
40464397 if (lval == LValPtr)
40474398 return ptr_instruction;
40484399
4049 return ir_build_load_ptr(irb, scope, node, ptr_instruction);
4400 IrInstruction *load_ptr = ir_build_load_ptr(irb, scope, node, ptr_instruction);
4401 return ir_expr_wrap(irb, scope, load_ptr, result_loc);
40504402}
40514403
40524404static IrInstruction *ir_gen_field_access(IrBuilder *irb, Scope *scope, AstNode *node) {
......@@ -4055,11 +4407,11 @@ static IrInstruction *ir_gen_field_access(IrBuilder *irb, Scope *scope, AstNode
40554407 AstNode *container_ref_node = node->data.field_access_expr.struct_expr;
40564408 Buf *field_name = node->data.field_access_expr.field_name;
40574409
4058 IrInstruction *container_ref_instruction = ir_gen_node_extra(irb, container_ref_node, scope, LValPtr);
4410 IrInstruction *container_ref_instruction = ir_gen_node_extra(irb, container_ref_node, scope, LValPtr, nullptr);
40594411 if (container_ref_instruction == irb->codegen->invalid_instruction)
40604412 return container_ref_instruction;
40614413
4062 return ir_build_field_ptr(irb, scope, node, container_ref_instruction, field_name);
4414 return ir_build_field_ptr(irb, scope, node, container_ref_instruction, field_name, false);
40634415}
40644416
40654417static IrInstruction *ir_gen_overflow_op(IrBuilder *irb, Scope *scope, AstNode *node, IrOverflowOp op) {
......@@ -4132,7 +4484,9 @@ static IrInstruction *ir_gen_this(IrBuilder *irb, Scope *orig_scope, AstNode *no
41324484 zig_unreachable();
41334485}
41344486
4135static IrInstruction *ir_gen_builtin_fn_call(IrBuilder *irb, Scope *scope, AstNode *node, LVal lval) {
4487static IrInstruction *ir_gen_builtin_fn_call(IrBuilder *irb, Scope *scope, AstNode *node, LVal lval,
4488 ResultLoc *result_loc)
4489{
41364490 assert(node->type == NodeTypeFnCallExpr);
41374491
41384492 AstNode *fn_ref_expr = node->data.fn_call_expr.fn_ref_expr;
......@@ -4168,7 +4522,7 @@ static IrInstruction *ir_gen_builtin_fn_call(IrBuilder *irb, Scope *scope, AstNo
41684522 return arg;
41694523
41704524 IrInstruction *type_of = ir_build_typeof(irb, scope, node, arg);
4171 return ir_lval_wrap(irb, scope, type_of, lval);
4525 return ir_lval_wrap(irb, scope, type_of, lval, result_loc);
41724526 }
41734527 case BuiltinFnIdSetCold:
41744528 {
......@@ -4178,7 +4532,7 @@ static IrInstruction *ir_gen_builtin_fn_call(IrBuilder *irb, Scope *scope, AstNo
41784532 return arg0_value;
41794533
41804534 IrInstruction *set_cold = ir_build_set_cold(irb, scope, node, arg0_value);
4181 return ir_lval_wrap(irb, scope, set_cold, lval);
4535 return ir_lval_wrap(irb, scope, set_cold, lval, result_loc);
41824536 }
41834537 case BuiltinFnIdSetRuntimeSafety:
41844538 {
......@@ -4188,7 +4542,7 @@ static IrInstruction *ir_gen_builtin_fn_call(IrBuilder *irb, Scope *scope, AstNo
41884542 return arg0_value;
41894543
41904544 IrInstruction *set_safety = ir_build_set_runtime_safety(irb, scope, node, arg0_value);
4191 return ir_lval_wrap(irb, scope, set_safety, lval);
4545 return ir_lval_wrap(irb, scope, set_safety, lval, result_loc);
41924546 }
41934547 case BuiltinFnIdSetFloatMode:
41944548 {
......@@ -4198,7 +4552,7 @@ static IrInstruction *ir_gen_builtin_fn_call(IrBuilder *irb, Scope *scope, AstNo
41984552 return arg0_value;
41994553
42004554 IrInstruction *set_float_mode = ir_build_set_float_mode(irb, scope, node, arg0_value);
4201 return ir_lval_wrap(irb, scope, set_float_mode, lval);
4555 return ir_lval_wrap(irb, scope, set_float_mode, lval, result_loc);
42024556 }
42034557 case BuiltinFnIdSizeof:
42044558 {
......@@ -4208,7 +4562,7 @@ static IrInstruction *ir_gen_builtin_fn_call(IrBuilder *irb, Scope *scope, AstNo
42084562 return arg0_value;
42094563
42104564 IrInstruction *size_of = ir_build_size_of(irb, scope, node, arg0_value);
4211 return ir_lval_wrap(irb, scope, size_of, lval);
4565 return ir_lval_wrap(irb, scope, size_of, lval, result_loc);
42124566 }
42134567 case BuiltinFnIdImport:
42144568 {
......@@ -4218,12 +4572,12 @@ static IrInstruction *ir_gen_builtin_fn_call(IrBuilder *irb, Scope *scope, AstNo
42184572 return arg0_value;
42194573
42204574 IrInstruction *import = ir_build_import(irb, scope, node, arg0_value);
4221 return ir_lval_wrap(irb, scope, import, lval);
4575 return ir_lval_wrap(irb, scope, import, lval, result_loc);
42224576 }
42234577 case BuiltinFnIdCImport:
42244578 {
42254579 IrInstruction *c_import = ir_build_c_import(irb, scope, node);
4226 return ir_lval_wrap(irb, scope, c_import, lval);
4580 return ir_lval_wrap(irb, scope, c_import, lval, result_loc);
42274581 }
42284582 case BuiltinFnIdCInclude:
42294583 {
......@@ -4238,7 +4592,7 @@ static IrInstruction *ir_gen_builtin_fn_call(IrBuilder *irb, Scope *scope, AstNo
42384592 }
42394593
42404594 IrInstruction *c_include = ir_build_c_include(irb, scope, node, arg0_value);
4241 return ir_lval_wrap(irb, scope, c_include, lval);
4595 return ir_lval_wrap(irb, scope, c_include, lval, result_loc);
42424596 }
42434597 case BuiltinFnIdCDefine:
42444598 {
......@@ -4258,7 +4612,7 @@ static IrInstruction *ir_gen_builtin_fn_call(IrBuilder *irb, Scope *scope, AstNo
42584612 }
42594613
42604614 IrInstruction *c_define = ir_build_c_define(irb, scope, node, arg0_value, arg1_value);
4261 return ir_lval_wrap(irb, scope, c_define, lval);
4615 return ir_lval_wrap(irb, scope, c_define, lval, result_loc);
42624616 }
42634617 case BuiltinFnIdCUndef:
42644618 {
......@@ -4273,7 +4627,7 @@ static IrInstruction *ir_gen_builtin_fn_call(IrBuilder *irb, Scope *scope, AstNo
42734627 }
42744628
42754629 IrInstruction *c_undef = ir_build_c_undef(irb, scope, node, arg0_value);
4276 return ir_lval_wrap(irb, scope, c_undef, lval);
4630 return ir_lval_wrap(irb, scope, c_undef, lval, result_loc);
42774631 }
42784632 case BuiltinFnIdCompileErr:
42794633 {
......@@ -4283,7 +4637,7 @@ static IrInstruction *ir_gen_builtin_fn_call(IrBuilder *irb, Scope *scope, AstNo
42834637 return arg0_value;
42844638
42854639 IrInstruction *compile_err = ir_build_compile_err(irb, scope, node, arg0_value);
4286 return ir_lval_wrap(irb, scope, compile_err, lval);
4640 return ir_lval_wrap(irb, scope, compile_err, lval, result_loc);
42874641 }
42884642 case BuiltinFnIdCompileLog:
42894643 {
......@@ -4297,7 +4651,7 @@ static IrInstruction *ir_gen_builtin_fn_call(IrBuilder *irb, Scope *scope, AstNo
42974651 }
42984652
42994653 IrInstruction *compile_log = ir_build_compile_log(irb, scope, node, actual_param_count, args);
4300 return ir_lval_wrap(irb, scope, compile_log, lval);
4654 return ir_lval_wrap(irb, scope, compile_log, lval, result_loc);
43014655 }
43024656 case BuiltinFnIdErrName:
43034657 {
......@@ -4307,7 +4661,7 @@ static IrInstruction *ir_gen_builtin_fn_call(IrBuilder *irb, Scope *scope, AstNo
43074661 return arg0_value;
43084662
43094663 IrInstruction *err_name = ir_build_err_name(irb, scope, node, arg0_value);
4310 return ir_lval_wrap(irb, scope, err_name, lval);
4664 return ir_lval_wrap(irb, scope, err_name, lval, result_loc);
43114665 }
43124666 case BuiltinFnIdEmbedFile:
43134667 {
......@@ -4317,7 +4671,7 @@ static IrInstruction *ir_gen_builtin_fn_call(IrBuilder *irb, Scope *scope, AstNo
43174671 return arg0_value;
43184672
43194673 IrInstruction *embed_file = ir_build_embed_file(irb, scope, node, arg0_value);
4320 return ir_lval_wrap(irb, scope, embed_file, lval);
4674 return ir_lval_wrap(irb, scope, embed_file, lval, result_loc);
43214675 }
43224676 case BuiltinFnIdCmpxchgWeak:
43234677 case BuiltinFnIdCmpxchgStrong:
......@@ -4353,8 +4707,9 @@ static IrInstruction *ir_gen_builtin_fn_call(IrBuilder *irb, Scope *scope, AstNo
43534707 return arg5_value;
43544708
43554709 IrInstruction *cmpxchg = ir_build_cmpxchg_src(irb, scope, node, arg0_value, arg1_value,
4356 arg2_value, arg3_value, arg4_value, arg5_value, (builtin_fn->id == BuiltinFnIdCmpxchgWeak));
4357 return ir_lval_wrap(irb, scope, cmpxchg, lval);
4710 arg2_value, arg3_value, arg4_value, arg5_value, (builtin_fn->id == BuiltinFnIdCmpxchgWeak),
4711 result_loc);
4712 return ir_lval_wrap(irb, scope, cmpxchg, lval, result_loc);
43584713 }
43594714 case BuiltinFnIdFence:
43604715 {
......@@ -4364,7 +4719,7 @@ static IrInstruction *ir_gen_builtin_fn_call(IrBuilder *irb, Scope *scope, AstNo
43644719 return arg0_value;
43654720
43664721 IrInstruction *fence = ir_build_fence(irb, scope, node, arg0_value, AtomicOrderUnordered);
4367 return ir_lval_wrap(irb, scope, fence, lval);
4722 return ir_lval_wrap(irb, scope, fence, lval, result_loc);
43684723 }
43694724 case BuiltinFnIdDivExact:
43704725 {
......@@ -4379,7 +4734,7 @@ static IrInstruction *ir_gen_builtin_fn_call(IrBuilder *irb, Scope *scope, AstNo
43794734 return arg1_value;
43804735
43814736 IrInstruction *bin_op = ir_build_bin_op(irb, scope, node, IrBinOpDivExact, arg0_value, arg1_value, true);
4382 return ir_lval_wrap(irb, scope, bin_op, lval);
4737 return ir_lval_wrap(irb, scope, bin_op, lval, result_loc);
43834738 }
43844739 case BuiltinFnIdDivTrunc:
43854740 {
......@@ -4394,7 +4749,7 @@ static IrInstruction *ir_gen_builtin_fn_call(IrBuilder *irb, Scope *scope, AstNo
43944749 return arg1_value;
43954750
43964751 IrInstruction *bin_op = ir_build_bin_op(irb, scope, node, IrBinOpDivTrunc, arg0_value, arg1_value, true);
4397 return ir_lval_wrap(irb, scope, bin_op, lval);
4752 return ir_lval_wrap(irb, scope, bin_op, lval, result_loc);
43984753 }
43994754 case BuiltinFnIdDivFloor:
44004755 {
......@@ -4409,7 +4764,7 @@ static IrInstruction *ir_gen_builtin_fn_call(IrBuilder *irb, Scope *scope, AstNo
44094764 return arg1_value;
44104765
44114766 IrInstruction *bin_op = ir_build_bin_op(irb, scope, node, IrBinOpDivFloor, arg0_value, arg1_value, true);
4412 return ir_lval_wrap(irb, scope, bin_op, lval);
4767 return ir_lval_wrap(irb, scope, bin_op, lval, result_loc);
44134768 }
44144769 case BuiltinFnIdRem:
44154770 {
......@@ -4424,7 +4779,7 @@ static IrInstruction *ir_gen_builtin_fn_call(IrBuilder *irb, Scope *scope, AstNo
44244779 return arg1_value;
44254780
44264781 IrInstruction *bin_op = ir_build_bin_op(irb, scope, node, IrBinOpRemRem, arg0_value, arg1_value, true);
4427 return ir_lval_wrap(irb, scope, bin_op, lval);
4782 return ir_lval_wrap(irb, scope, bin_op, lval, result_loc);
44284783 }
44294784 case BuiltinFnIdMod:
44304785 {
......@@ -4439,7 +4794,7 @@ static IrInstruction *ir_gen_builtin_fn_call(IrBuilder *irb, Scope *scope, AstNo
44394794 return arg1_value;
44404795
44414796 IrInstruction *bin_op = ir_build_bin_op(irb, scope, node, IrBinOpRemMod, arg0_value, arg1_value, true);
4442 return ir_lval_wrap(irb, scope, bin_op, lval);
4797 return ir_lval_wrap(irb, scope, bin_op, lval, result_loc);
44434798 }
44444799 case BuiltinFnIdSqrt:
44454800 case BuiltinFnIdSin:
......@@ -4467,7 +4822,7 @@ static IrInstruction *ir_gen_builtin_fn_call(IrBuilder *irb, Scope *scope, AstNo
44674822 return arg1_value;
44684823
44694824 IrInstruction *ir_sqrt = ir_build_float_op(irb, scope, node, arg0_value, arg1_value, builtin_fn->id);
4470 return ir_lval_wrap(irb, scope, ir_sqrt, lval);
4825 return ir_lval_wrap(irb, scope, ir_sqrt, lval, result_loc);
44714826 }
44724827 case BuiltinFnIdTruncate:
44734828 {
......@@ -4482,7 +4837,7 @@ static IrInstruction *ir_gen_builtin_fn_call(IrBuilder *irb, Scope *scope, AstNo
44824837 return arg1_value;
44834838
44844839 IrInstruction *truncate = ir_build_truncate(irb, scope, node, arg0_value, arg1_value);
4485 return ir_lval_wrap(irb, scope, truncate, lval);
4840 return ir_lval_wrap(irb, scope, truncate, lval, result_loc);
44864841 }
44874842 case BuiltinFnIdIntCast:
44884843 {
......@@ -4497,7 +4852,7 @@ static IrInstruction *ir_gen_builtin_fn_call(IrBuilder *irb, Scope *scope, AstNo
44974852 return arg1_value;
44984853
44994854 IrInstruction *result = ir_build_int_cast(irb, scope, node, arg0_value, arg1_value);
4500 return ir_lval_wrap(irb, scope, result, lval);
4855 return ir_lval_wrap(irb, scope, result, lval, result_loc);
45014856 }
45024857 case BuiltinFnIdFloatCast:
45034858 {
......@@ -4512,7 +4867,7 @@ static IrInstruction *ir_gen_builtin_fn_call(IrBuilder *irb, Scope *scope, AstNo
45124867 return arg1_value;
45134868
45144869 IrInstruction *result = ir_build_float_cast(irb, scope, node, arg0_value, arg1_value);
4515 return ir_lval_wrap(irb, scope, result, lval);
4870 return ir_lval_wrap(irb, scope, result, lval, result_loc);
45164871 }
45174872 case BuiltinFnIdErrSetCast:
45184873 {
......@@ -4527,7 +4882,7 @@ static IrInstruction *ir_gen_builtin_fn_call(IrBuilder *irb, Scope *scope, AstNo
45274882 return arg1_value;
45284883
45294884 IrInstruction *result = ir_build_err_set_cast(irb, scope, node, arg0_value, arg1_value);
4530 return ir_lval_wrap(irb, scope, result, lval);
4885 return ir_lval_wrap(irb, scope, result, lval, result_loc);
45314886 }
45324887 case BuiltinFnIdFromBytes:
45334888 {
......@@ -4541,8 +4896,8 @@ static IrInstruction *ir_gen_builtin_fn_call(IrBuilder *irb, Scope *scope, AstNo
45414896 if (arg1_value == irb->codegen->invalid_instruction)
45424897 return arg1_value;
45434898
4544 IrInstruction *result = ir_build_from_bytes(irb, scope, node, arg0_value, arg1_value);
4545 return ir_lval_wrap(irb, scope, result, lval);
4899 IrInstruction *result = ir_build_from_bytes(irb, scope, node, arg0_value, arg1_value, result_loc);
4900 return ir_lval_wrap(irb, scope, result, lval, result_loc);
45464901 }
45474902 case BuiltinFnIdToBytes:
45484903 {
......@@ -4551,8 +4906,8 @@ static IrInstruction *ir_gen_builtin_fn_call(IrBuilder *irb, Scope *scope, AstNo
45514906 if (arg0_value == irb->codegen->invalid_instruction)
45524907 return arg0_value;
45534908
4554 IrInstruction *result = ir_build_to_bytes(irb, scope, node, arg0_value);
4555 return ir_lval_wrap(irb, scope, result, lval);
4909 IrInstruction *result = ir_build_to_bytes(irb, scope, node, arg0_value, result_loc);
4910 return ir_lval_wrap(irb, scope, result, lval, result_loc);
45564911 }
45574912 case BuiltinFnIdIntToFloat:
45584913 {
......@@ -4567,7 +4922,7 @@ static IrInstruction *ir_gen_builtin_fn_call(IrBuilder *irb, Scope *scope, AstNo
45674922 return arg1_value;
45684923
45694924 IrInstruction *result = ir_build_int_to_float(irb, scope, node, arg0_value, arg1_value);
4570 return ir_lval_wrap(irb, scope, result, lval);
4925 return ir_lval_wrap(irb, scope, result, lval, result_loc);
45714926 }
45724927 case BuiltinFnIdFloatToInt:
45734928 {
......@@ -4582,7 +4937,7 @@ static IrInstruction *ir_gen_builtin_fn_call(IrBuilder *irb, Scope *scope, AstNo
45824937 return arg1_value;
45834938
45844939 IrInstruction *result = ir_build_float_to_int(irb, scope, node, arg0_value, arg1_value);
4585 return ir_lval_wrap(irb, scope, result, lval);
4940 return ir_lval_wrap(irb, scope, result, lval, result_loc);
45864941 }
45874942 case BuiltinFnIdErrToInt:
45884943 {
......@@ -4592,7 +4947,7 @@ static IrInstruction *ir_gen_builtin_fn_call(IrBuilder *irb, Scope *scope, AstNo
45924947 return arg0_value;
45934948
45944949 IrInstruction *result = ir_build_err_to_int(irb, scope, node, arg0_value);
4595 return ir_lval_wrap(irb, scope, result, lval);
4950 return ir_lval_wrap(irb, scope, result, lval, result_loc);
45964951 }
45974952 case BuiltinFnIdIntToErr:
45984953 {
......@@ -4602,7 +4957,7 @@ static IrInstruction *ir_gen_builtin_fn_call(IrBuilder *irb, Scope *scope, AstNo
46024957 return arg0_value;
46034958
46044959 IrInstruction *result = ir_build_int_to_err(irb, scope, node, arg0_value);
4605 return ir_lval_wrap(irb, scope, result, lval);
4960 return ir_lval_wrap(irb, scope, result, lval, result_loc);
46064961 }
46074962 case BuiltinFnIdBoolToInt:
46084963 {
......@@ -4612,7 +4967,7 @@ static IrInstruction *ir_gen_builtin_fn_call(IrBuilder *irb, Scope *scope, AstNo
46124967 return arg0_value;
46134968
46144969 IrInstruction *result = ir_build_bool_to_int(irb, scope, node, arg0_value);
4615 return ir_lval_wrap(irb, scope, result, lval);
4970 return ir_lval_wrap(irb, scope, result, lval, result_loc);
46164971 }
46174972 case BuiltinFnIdIntType:
46184973 {
......@@ -4627,7 +4982,7 @@ static IrInstruction *ir_gen_builtin_fn_call(IrBuilder *irb, Scope *scope, AstNo
46274982 return arg1_value;
46284983
46294984 IrInstruction *int_type = ir_build_int_type(irb, scope, node, arg0_value, arg1_value);
4630 return ir_lval_wrap(irb, scope, int_type, lval);
4985 return ir_lval_wrap(irb, scope, int_type, lval, result_loc);
46314986 }
46324987 case BuiltinFnIdVectorType:
46334988 {
......@@ -4642,7 +4997,7 @@ static IrInstruction *ir_gen_builtin_fn_call(IrBuilder *irb, Scope *scope, AstNo
46424997 return arg1_value;
46434998
46444999 IrInstruction *vector_type = ir_build_vector_type(irb, scope, node, arg0_value, arg1_value);
4645 return ir_lval_wrap(irb, scope, vector_type, lval);
5000 return ir_lval_wrap(irb, scope, vector_type, lval, result_loc);
46465001 }
46475002 case BuiltinFnIdMemcpy:
46485003 {
......@@ -4662,7 +5017,7 @@ static IrInstruction *ir_gen_builtin_fn_call(IrBuilder *irb, Scope *scope, AstNo
46625017 return arg2_value;
46635018
46645019 IrInstruction *ir_memcpy = ir_build_memcpy(irb, scope, node, arg0_value, arg1_value, arg2_value);
4665 return ir_lval_wrap(irb, scope, ir_memcpy, lval);
5020 return ir_lval_wrap(irb, scope, ir_memcpy, lval, result_loc);
46665021 }
46675022 case BuiltinFnIdMemset:
46685023 {
......@@ -4682,7 +5037,7 @@ static IrInstruction *ir_gen_builtin_fn_call(IrBuilder *irb, Scope *scope, AstNo
46825037 return arg2_value;
46835038
46845039 IrInstruction *ir_memset = ir_build_memset(irb, scope, node, arg0_value, arg1_value, arg2_value);
4685 return ir_lval_wrap(irb, scope, ir_memset, lval);
5040 return ir_lval_wrap(irb, scope, ir_memset, lval, result_loc);
46865041 }
46875042 case BuiltinFnIdMemberCount:
46885043 {
......@@ -4692,7 +5047,7 @@ static IrInstruction *ir_gen_builtin_fn_call(IrBuilder *irb, Scope *scope, AstNo
46925047 return arg0_value;
46935048
46945049 IrInstruction *member_count = ir_build_member_count(irb, scope, node, arg0_value);
4695 return ir_lval_wrap(irb, scope, member_count, lval);
5050 return ir_lval_wrap(irb, scope, member_count, lval, result_loc);
46965051 }
46975052 case BuiltinFnIdMemberType:
46985053 {
......@@ -4708,7 +5063,7 @@ static IrInstruction *ir_gen_builtin_fn_call(IrBuilder *irb, Scope *scope, AstNo
47085063
47095064
47105065 IrInstruction *member_type = ir_build_member_type(irb, scope, node, arg0_value, arg1_value);
4711 return ir_lval_wrap(irb, scope, member_type, lval);
5066 return ir_lval_wrap(irb, scope, member_type, lval, result_loc);
47125067 }
47135068 case BuiltinFnIdMemberName:
47145069 {
......@@ -4724,12 +5079,12 @@ static IrInstruction *ir_gen_builtin_fn_call(IrBuilder *irb, Scope *scope, AstNo
47245079
47255080
47265081 IrInstruction *member_name = ir_build_member_name(irb, scope, node, arg0_value, arg1_value);
4727 return ir_lval_wrap(irb, scope, member_name, lval);
5082 return ir_lval_wrap(irb, scope, member_name, lval, result_loc);
47285083 }
47295084 case BuiltinFnIdField:
47305085 {
47315086 AstNode *arg0_node = node->data.fn_call_expr.params.at(0);
4732 IrInstruction *arg0_value = ir_gen_node_extra(irb, arg0_node, scope, LValPtr);
5087 IrInstruction *arg0_value = ir_gen_node_extra(irb, arg0_node, scope, LValPtr, nullptr);
47335088 if (arg0_value == irb->codegen->invalid_instruction)
47345089 return arg0_value;
47355090
......@@ -4743,7 +5098,8 @@ static IrInstruction *ir_gen_builtin_fn_call(IrBuilder *irb, Scope *scope, AstNo
47435098 if (lval == LValPtr)
47445099 return ptr_instruction;
47455100
4746 return ir_build_load_ptr(irb, scope, node, ptr_instruction);
5101 IrInstruction *load_ptr = ir_build_load_ptr(irb, scope, node, ptr_instruction);
5102 return ir_expr_wrap(irb, scope, load_ptr, result_loc);
47475103 }
47485104 case BuiltinFnIdTypeInfo:
47495105 {
......@@ -4753,14 +5109,14 @@ static IrInstruction *ir_gen_builtin_fn_call(IrBuilder *irb, Scope *scope, AstNo
47535109 return arg0_value;
47545110
47555111 IrInstruction *type_info = ir_build_type_info(irb, scope, node, arg0_value);
4756 return ir_lval_wrap(irb, scope, type_info, lval);
5112 return ir_lval_wrap(irb, scope, type_info, lval, result_loc);
47575113 }
47585114 case BuiltinFnIdBreakpoint:
4759 return ir_lval_wrap(irb, scope, ir_build_breakpoint(irb, scope, node), lval);
5115 return ir_lval_wrap(irb, scope, ir_build_breakpoint(irb, scope, node), lval, result_loc);
47605116 case BuiltinFnIdReturnAddress:
4761 return ir_lval_wrap(irb, scope, ir_build_return_address(irb, scope, node), lval);
5117 return ir_lval_wrap(irb, scope, ir_build_return_address(irb, scope, node), lval, result_loc);
47625118 case BuiltinFnIdFrameAddress:
4763 return ir_lval_wrap(irb, scope, ir_build_frame_address(irb, scope, node), lval);
5119 return ir_lval_wrap(irb, scope, ir_build_frame_address(irb, scope, node), lval, result_loc);
47645120 case BuiltinFnIdHandle:
47655121 if (!irb->exec->fn_entry) {
47665122 add_node_error(irb->codegen, node, buf_sprintf("@handle() called outside of function definition"));
......@@ -4770,7 +5126,7 @@ static IrInstruction *ir_gen_builtin_fn_call(IrBuilder *irb, Scope *scope, AstNo
47705126 add_node_error(irb->codegen, node, buf_sprintf("@handle() in non-async function"));
47715127 return irb->codegen->invalid_instruction;
47725128 }
4773 return ir_lval_wrap(irb, scope, ir_build_handle(irb, scope, node), lval);
5129 return ir_lval_wrap(irb, scope, ir_build_handle(irb, scope, node), lval, result_loc);
47745130 case BuiltinFnIdAlignOf:
47755131 {
47765132 AstNode *arg0_node = node->data.fn_call_expr.params.at(0);
......@@ -4779,18 +5135,18 @@ static IrInstruction *ir_gen_builtin_fn_call(IrBuilder *irb, Scope *scope, AstNo
47795135 return arg0_value;
47805136
47815137 IrInstruction *align_of = ir_build_align_of(irb, scope, node, arg0_value);
4782 return ir_lval_wrap(irb, scope, align_of, lval);
5138 return ir_lval_wrap(irb, scope, align_of, lval, result_loc);
47835139 }
47845140 case BuiltinFnIdAddWithOverflow:
4785 return ir_lval_wrap(irb, scope, ir_gen_overflow_op(irb, scope, node, IrOverflowOpAdd), lval);
5141 return ir_lval_wrap(irb, scope, ir_gen_overflow_op(irb, scope, node, IrOverflowOpAdd), lval, result_loc);
47865142 case BuiltinFnIdSubWithOverflow:
4787 return ir_lval_wrap(irb, scope, ir_gen_overflow_op(irb, scope, node, IrOverflowOpSub), lval);
5143 return ir_lval_wrap(irb, scope, ir_gen_overflow_op(irb, scope, node, IrOverflowOpSub), lval, result_loc);
47885144 case BuiltinFnIdMulWithOverflow:
4789 return ir_lval_wrap(irb, scope, ir_gen_overflow_op(irb, scope, node, IrOverflowOpMul), lval);
5145 return ir_lval_wrap(irb, scope, ir_gen_overflow_op(irb, scope, node, IrOverflowOpMul), lval, result_loc);
47905146 case BuiltinFnIdShlWithOverflow:
4791 return ir_lval_wrap(irb, scope, ir_gen_overflow_op(irb, scope, node, IrOverflowOpShl), lval);
5147 return ir_lval_wrap(irb, scope, ir_gen_overflow_op(irb, scope, node, IrOverflowOpShl), lval, result_loc);
47925148 case BuiltinFnIdMulAdd:
4793 return ir_lval_wrap(irb, scope, ir_gen_mul_add(irb, scope, node), lval);
5149 return ir_lval_wrap(irb, scope, ir_gen_mul_add(irb, scope, node), lval, result_loc);
47945150 case BuiltinFnIdTypeName:
47955151 {
47965152 AstNode *arg0_node = node->data.fn_call_expr.params.at(0);
......@@ -4799,7 +5155,7 @@ static IrInstruction *ir_gen_builtin_fn_call(IrBuilder *irb, Scope *scope, AstNo
47995155 return arg0_value;
48005156
48015157 IrInstruction *type_name = ir_build_type_name(irb, scope, node, arg0_value);
4802 return ir_lval_wrap(irb, scope, type_name, lval);
5158 return ir_lval_wrap(irb, scope, type_name, lval, result_loc);
48035159 }
48045160 case BuiltinFnIdPanic:
48055161 {
......@@ -4809,7 +5165,7 @@ static IrInstruction *ir_gen_builtin_fn_call(IrBuilder *irb, Scope *scope, AstNo
48095165 return arg0_value;
48105166
48115167 IrInstruction *panic = ir_build_panic(irb, scope, node, arg0_value);
4812 return ir_lval_wrap(irb, scope, panic, lval);
5168 return ir_lval_wrap(irb, scope, panic, lval, result_loc);
48135169 }
48145170 case BuiltinFnIdPtrCast:
48155171 {
......@@ -4824,22 +5180,31 @@ static IrInstruction *ir_gen_builtin_fn_call(IrBuilder *irb, Scope *scope, AstNo
48245180 return arg1_value;
48255181
48265182 IrInstruction *ptr_cast = ir_build_ptr_cast_src(irb, scope, node, arg0_value, arg1_value, true);
4827 return ir_lval_wrap(irb, scope, ptr_cast, lval);
5183 return ir_lval_wrap(irb, scope, ptr_cast, lval, result_loc);
48285184 }
48295185 case BuiltinFnIdBitCast:
48305186 {
4831 AstNode *arg0_node = node->data.fn_call_expr.params.at(0);
4832 IrInstruction *arg0_value = ir_gen_node(irb, arg0_node, scope);
4833 if (arg0_value == irb->codegen->invalid_instruction)
4834 return arg0_value;
5187 AstNode *dest_type_node = node->data.fn_call_expr.params.at(0);
5188 IrInstruction *dest_type = ir_gen_node(irb, dest_type_node, scope);
5189 if (dest_type == irb->codegen->invalid_instruction)
5190 return dest_type;
5191
5192 ResultLocBitCast *result_loc_bit_cast = allocate<ResultLocBitCast>(1);
5193 result_loc_bit_cast->base.id = ResultLocIdBitCast;
5194 result_loc_bit_cast->base.source_instruction = dest_type;
5195 ir_ref_instruction(dest_type, irb->current_basic_block);
5196 result_loc_bit_cast->parent = result_loc;
5197
5198 ir_build_reset_result(irb, scope, node, &result_loc_bit_cast->base);
48355199
48365200 AstNode *arg1_node = node->data.fn_call_expr.params.at(1);
4837 IrInstruction *arg1_value = ir_gen_node(irb, arg1_node, scope);
5201 IrInstruction *arg1_value = ir_gen_node_extra(irb, arg1_node, scope, LValNone,
5202 &result_loc_bit_cast->base);
48385203 if (arg1_value == irb->codegen->invalid_instruction)
48395204 return arg1_value;
48405205
4841 IrInstruction *bit_cast = ir_build_bit_cast(irb, scope, node, arg0_value, arg1_value);
4842 return ir_lval_wrap(irb, scope, bit_cast, lval);
5206 IrInstruction *bitcast = ir_build_bit_cast_src(irb, scope, arg1_node, arg1_value, result_loc_bit_cast);
5207 return ir_lval_wrap(irb, scope, bitcast, lval, result_loc);
48435208 }
48445209 case BuiltinFnIdIntToPtr:
48455210 {
......@@ -4854,7 +5219,7 @@ static IrInstruction *ir_gen_builtin_fn_call(IrBuilder *irb, Scope *scope, AstNo
48545219 return arg1_value;
48555220
48565221 IrInstruction *int_to_ptr = ir_build_int_to_ptr(irb, scope, node, arg0_value, arg1_value);
4857 return ir_lval_wrap(irb, scope, int_to_ptr, lval);
5222 return ir_lval_wrap(irb, scope, int_to_ptr, lval, result_loc);
48585223 }
48595224 case BuiltinFnIdPtrToInt:
48605225 {
......@@ -4864,7 +5229,7 @@ static IrInstruction *ir_gen_builtin_fn_call(IrBuilder *irb, Scope *scope, AstNo
48645229 return arg0_value;
48655230
48665231 IrInstruction *ptr_to_int = ir_build_ptr_to_int(irb, scope, node, arg0_value);
4867 return ir_lval_wrap(irb, scope, ptr_to_int, lval);
5232 return ir_lval_wrap(irb, scope, ptr_to_int, lval, result_loc);
48685233 }
48695234 case BuiltinFnIdTagName:
48705235 {
......@@ -4875,7 +5240,7 @@ static IrInstruction *ir_gen_builtin_fn_call(IrBuilder *irb, Scope *scope, AstNo
48755240
48765241 IrInstruction *actual_tag = ir_build_union_tag(irb, scope, node, arg0_value);
48775242 IrInstruction *tag_name = ir_build_tag_name(irb, scope, node, actual_tag);
4878 return ir_lval_wrap(irb, scope, tag_name, lval);
5243 return ir_lval_wrap(irb, scope, tag_name, lval, result_loc);
48795244 }
48805245 case BuiltinFnIdTagType:
48815246 {
......@@ -4885,7 +5250,7 @@ static IrInstruction *ir_gen_builtin_fn_call(IrBuilder *irb, Scope *scope, AstNo
48855250 return arg0_value;
48865251
48875252 IrInstruction *tag_type = ir_build_tag_type(irb, scope, node, arg0_value);
4888 return ir_lval_wrap(irb, scope, tag_type, lval);
5253 return ir_lval_wrap(irb, scope, tag_type, lval, result_loc);
48895254 }
48905255 case BuiltinFnIdFieldParentPtr:
48915256 {
......@@ -4905,7 +5270,7 @@ static IrInstruction *ir_gen_builtin_fn_call(IrBuilder *irb, Scope *scope, AstNo
49055270 return arg2_value;
49065271
49075272 IrInstruction *field_parent_ptr = ir_build_field_parent_ptr(irb, scope, node, arg0_value, arg1_value, arg2_value, nullptr);
4908 return ir_lval_wrap(irb, scope, field_parent_ptr, lval);
5273 return ir_lval_wrap(irb, scope, field_parent_ptr, lval, result_loc);
49095274 }
49105275 case BuiltinFnIdByteOffsetOf:
49115276 {
......@@ -4920,7 +5285,7 @@ static IrInstruction *ir_gen_builtin_fn_call(IrBuilder *irb, Scope *scope, AstNo
49205285 return arg1_value;
49215286
49225287 IrInstruction *offset_of = ir_build_byte_offset_of(irb, scope, node, arg0_value, arg1_value);
4923 return ir_lval_wrap(irb, scope, offset_of, lval);
5288 return ir_lval_wrap(irb, scope, offset_of, lval, result_loc);
49245289 }
49255290 case BuiltinFnIdBitOffsetOf:
49265291 {
......@@ -4935,7 +5300,7 @@ static IrInstruction *ir_gen_builtin_fn_call(IrBuilder *irb, Scope *scope, AstNo
49355300 return arg1_value;
49365301
49375302 IrInstruction *offset_of = ir_build_bit_offset_of(irb, scope, node, arg0_value, arg1_value);
4938 return ir_lval_wrap(irb, scope, offset_of, lval);
5303 return ir_lval_wrap(irb, scope, offset_of, lval, result_loc);
49395304 }
49405305 case BuiltinFnIdInlineCall:
49415306 case BuiltinFnIdNoInlineCall:
......@@ -4961,8 +5326,9 @@ static IrInstruction *ir_gen_builtin_fn_call(IrBuilder *irb, Scope *scope, AstNo
49615326 }
49625327 FnInline fn_inline = (builtin_fn->id == BuiltinFnIdInlineCall) ? FnInlineAlways : FnInlineNever;
49635328
4964 IrInstruction *call = ir_build_call(irb, scope, node, nullptr, fn_ref, arg_count, args, false, fn_inline, false, nullptr, nullptr);
4965 return ir_lval_wrap(irb, scope, call, lval);
5329 IrInstruction *call = ir_build_call_src(irb, scope, node, nullptr, fn_ref, arg_count, args, false,
5330 fn_inline, false, nullptr, nullptr, result_loc);
5331 return ir_lval_wrap(irb, scope, call, lval, result_loc);
49665332 }
49675333 case BuiltinFnIdNewStackCall:
49685334 {
......@@ -4991,8 +5357,9 @@ static IrInstruction *ir_gen_builtin_fn_call(IrBuilder *irb, Scope *scope, AstNo
49915357 return args[i];
49925358 }
49935359
4994 IrInstruction *call = ir_build_call(irb, scope, node, nullptr, fn_ref, arg_count, args, false, FnInlineAuto, false, nullptr, new_stack);
4995 return ir_lval_wrap(irb, scope, call, lval);
5360 IrInstruction *call = ir_build_call_src(irb, scope, node, nullptr, fn_ref, arg_count, args, false,
5361 FnInlineAuto, false, nullptr, new_stack, result_loc);
5362 return ir_lval_wrap(irb, scope, call, lval, result_loc);
49965363 }
49975364 case BuiltinFnIdTypeId:
49985365 {
......@@ -5002,7 +5369,7 @@ static IrInstruction *ir_gen_builtin_fn_call(IrBuilder *irb, Scope *scope, AstNo
50025369 return arg0_value;
50035370
50045371 IrInstruction *type_id = ir_build_type_id(irb, scope, node, arg0_value);
5005 return ir_lval_wrap(irb, scope, type_id, lval);
5372 return ir_lval_wrap(irb, scope, type_id, lval, result_loc);
50065373 }
50075374 case BuiltinFnIdShlExact:
50085375 {
......@@ -5017,7 +5384,7 @@ static IrInstruction *ir_gen_builtin_fn_call(IrBuilder *irb, Scope *scope, AstNo
50175384 return arg1_value;
50185385
50195386 IrInstruction *bin_op = ir_build_bin_op(irb, scope, node, IrBinOpBitShiftLeftExact, arg0_value, arg1_value, true);
5020 return ir_lval_wrap(irb, scope, bin_op, lval);
5387 return ir_lval_wrap(irb, scope, bin_op, lval, result_loc);
50215388 }
50225389 case BuiltinFnIdShrExact:
50235390 {
......@@ -5032,7 +5399,7 @@ static IrInstruction *ir_gen_builtin_fn_call(IrBuilder *irb, Scope *scope, AstNo
50325399 return arg1_value;
50335400
50345401 IrInstruction *bin_op = ir_build_bin_op(irb, scope, node, IrBinOpBitShiftRightExact, arg0_value, arg1_value, true);
5035 return ir_lval_wrap(irb, scope, bin_op, lval);
5402 return ir_lval_wrap(irb, scope, bin_op, lval, result_loc);
50365403 }
50375404 case BuiltinFnIdSetEvalBranchQuota:
50385405 {
......@@ -5042,7 +5409,7 @@ static IrInstruction *ir_gen_builtin_fn_call(IrBuilder *irb, Scope *scope, AstNo
50425409 return arg0_value;
50435410
50445411 IrInstruction *set_eval_branch_quota = ir_build_set_eval_branch_quota(irb, scope, node, arg0_value);
5045 return ir_lval_wrap(irb, scope, set_eval_branch_quota, lval);
5412 return ir_lval_wrap(irb, scope, set_eval_branch_quota, lval, result_loc);
50465413 }
50475414 case BuiltinFnIdAlignCast:
50485415 {
......@@ -5057,17 +5424,17 @@ static IrInstruction *ir_gen_builtin_fn_call(IrBuilder *irb, Scope *scope, AstNo
50575424 return arg1_value;
50585425
50595426 IrInstruction *align_cast = ir_build_align_cast(irb, scope, node, arg0_value, arg1_value);
5060 return ir_lval_wrap(irb, scope, align_cast, lval);
5427 return ir_lval_wrap(irb, scope, align_cast, lval, result_loc);
50615428 }
50625429 case BuiltinFnIdOpaqueType:
50635430 {
50645431 IrInstruction *opaque_type = ir_build_opaque_type(irb, scope, node);
5065 return ir_lval_wrap(irb, scope, opaque_type, lval);
5432 return ir_lval_wrap(irb, scope, opaque_type, lval, result_loc);
50665433 }
50675434 case BuiltinFnIdThis:
50685435 {
50695436 IrInstruction *this_inst = ir_gen_this(irb, scope, node);
5070 return ir_lval_wrap(irb, scope, this_inst, lval);
5437 return ir_lval_wrap(irb, scope, this_inst, lval, result_loc);
50715438 }
50725439 case BuiltinFnIdSetAlignStack:
50735440 {
......@@ -5077,7 +5444,7 @@ static IrInstruction *ir_gen_builtin_fn_call(IrBuilder *irb, Scope *scope, AstNo
50775444 return arg0_value;
50785445
50795446 IrInstruction *set_align_stack = ir_build_set_align_stack(irb, scope, node, arg0_value);
5080 return ir_lval_wrap(irb, scope, set_align_stack, lval);
5447 return ir_lval_wrap(irb, scope, set_align_stack, lval, result_loc);
50815448 }
50825449 case BuiltinFnIdArgType:
50835450 {
......@@ -5092,7 +5459,7 @@ static IrInstruction *ir_gen_builtin_fn_call(IrBuilder *irb, Scope *scope, AstNo
50925459 return arg1_value;
50935460
50945461 IrInstruction *arg_type = ir_build_arg_type(irb, scope, node, arg0_value, arg1_value);
5095 return ir_lval_wrap(irb, scope, arg_type, lval);
5462 return ir_lval_wrap(irb, scope, arg_type, lval, result_loc);
50965463 }
50975464 case BuiltinFnIdExport:
50985465 {
......@@ -5112,12 +5479,12 @@ static IrInstruction *ir_gen_builtin_fn_call(IrBuilder *irb, Scope *scope, AstNo
51125479 return arg2_value;
51135480
51145481 IrInstruction *ir_export = ir_build_export(irb, scope, node, arg0_value, arg1_value, arg2_value);
5115 return ir_lval_wrap(irb, scope, ir_export, lval);
5482 return ir_lval_wrap(irb, scope, ir_export, lval, result_loc);
51165483 }
51175484 case BuiltinFnIdErrorReturnTrace:
51185485 {
51195486 IrInstruction *error_return_trace = ir_build_error_return_trace(irb, scope, node, IrInstructionErrorReturnTrace::Null);
5120 return ir_lval_wrap(irb, scope, error_return_trace, lval);
5487 return ir_lval_wrap(irb, scope, error_return_trace, lval, result_loc);
51215488 }
51225489 case BuiltinFnIdAtomicRmw:
51235490 {
......@@ -5146,10 +5513,11 @@ static IrInstruction *ir_gen_builtin_fn_call(IrBuilder *irb, Scope *scope, AstNo
51465513 if (arg4_value == irb->codegen->invalid_instruction)
51475514 return arg4_value;
51485515
5149 return ir_build_atomic_rmw(irb, scope, node, arg0_value, arg1_value, arg2_value, arg3_value,
5516 IrInstruction *inst = ir_build_atomic_rmw(irb, scope, node, arg0_value, arg1_value, arg2_value, arg3_value,
51505517 arg4_value,
51515518 // these 2 values don't mean anything since we passed non-null values for other args
51525519 AtomicRmwOp_xchg, AtomicOrderMonotonic);
5520 return ir_lval_wrap(irb, scope, inst, lval, result_loc);
51535521 }
51545522 case BuiltinFnIdAtomicLoad:
51555523 {
......@@ -5168,9 +5536,10 @@ static IrInstruction *ir_gen_builtin_fn_call(IrBuilder *irb, Scope *scope, AstNo
51685536 if (arg2_value == irb->codegen->invalid_instruction)
51695537 return arg2_value;
51705538
5171 return ir_build_atomic_load(irb, scope, node, arg0_value, arg1_value, arg2_value,
5539 IrInstruction *inst = ir_build_atomic_load(irb, scope, node, arg0_value, arg1_value, arg2_value,
51725540 // this value does not mean anything since we passed non-null values for other arg
51735541 AtomicOrderMonotonic);
5542 return ir_lval_wrap(irb, scope, inst, lval, result_loc);
51745543 }
51755544 case BuiltinFnIdIntToEnum:
51765545 {
......@@ -5185,7 +5554,7 @@ static IrInstruction *ir_gen_builtin_fn_call(IrBuilder *irb, Scope *scope, AstNo
51855554 return arg1_value;
51865555
51875556 IrInstruction *result = ir_build_int_to_enum(irb, scope, node, arg0_value, arg1_value);
5188 return ir_lval_wrap(irb, scope, result, lval);
5557 return ir_lval_wrap(irb, scope, result, lval, result_loc);
51895558 }
51905559 case BuiltinFnIdEnumToInt:
51915560 {
......@@ -5195,7 +5564,7 @@ static IrInstruction *ir_gen_builtin_fn_call(IrBuilder *irb, Scope *scope, AstNo
51955564 return arg0_value;
51965565
51975566 IrInstruction *result = ir_build_enum_to_int(irb, scope, node, arg0_value);
5198 return ir_lval_wrap(irb, scope, result, lval);
5567 return ir_lval_wrap(irb, scope, result, lval, result_loc);
51995568 }
52005569 case BuiltinFnIdCtz:
52015570 case BuiltinFnIdPopCount:
......@@ -5233,7 +5602,7 @@ static IrInstruction *ir_gen_builtin_fn_call(IrBuilder *irb, Scope *scope, AstNo
52335602 default:
52345603 zig_unreachable();
52355604 }
5236 return ir_lval_wrap(irb, scope, result, lval);
5605 return ir_lval_wrap(irb, scope, result, lval, result_loc);
52375606 }
52385607 case BuiltinFnIdHasDecl:
52395608 {
......@@ -5248,17 +5617,19 @@ static IrInstruction *ir_gen_builtin_fn_call(IrBuilder *irb, Scope *scope, AstNo
52485617 return arg1_value;
52495618
52505619 IrInstruction *has_decl = ir_build_has_decl(irb, scope, node, arg0_value, arg1_value);
5251 return ir_lval_wrap(irb, scope, has_decl, lval);
5620 return ir_lval_wrap(irb, scope, has_decl, lval, result_loc);
52525621 }
52535622 }
52545623 zig_unreachable();
52555624}
52565625
5257static IrInstruction *ir_gen_fn_call(IrBuilder *irb, Scope *scope, AstNode *node, LVal lval) {
5626static IrInstruction *ir_gen_fn_call(IrBuilder *irb, Scope *scope, AstNode *node, LVal lval,
5627 ResultLoc *result_loc)
5628{
52585629 assert(node->type == NodeTypeFnCallExpr);
52595630
52605631 if (node->data.fn_call_expr.is_builtin)
5261 return ir_gen_builtin_fn_call(irb, scope, node, lval);
5632 return ir_gen_builtin_fn_call(irb, scope, node, lval, result_loc);
52625633
52635634 AstNode *fn_ref_node = node->data.fn_call_expr.fn_ref_expr;
52645635 IrInstruction *fn_ref = ir_gen_node(irb, fn_ref_node, scope);
......@@ -5284,12 +5655,14 @@ static IrInstruction *ir_gen_fn_call(IrBuilder *irb, Scope *scope, AstNode *node
52845655 }
52855656 }
52865657
5287 IrInstruction *fn_call = ir_build_call(irb, scope, node, nullptr, fn_ref, arg_count, args, false, FnInlineAuto,
5288 is_async, async_allocator, nullptr);
5289 return ir_lval_wrap(irb, scope, fn_call, lval);
5658 IrInstruction *fn_call = ir_build_call_src(irb, scope, node, nullptr, fn_ref, arg_count, args, false, FnInlineAuto,
5659 is_async, async_allocator, nullptr, result_loc);
5660 return ir_lval_wrap(irb, scope, fn_call, lval, result_loc);
52905661}
52915662
5292static IrInstruction *ir_gen_if_bool_expr(IrBuilder *irb, Scope *scope, AstNode *node) {
5663static IrInstruction *ir_gen_if_bool_expr(IrBuilder *irb, Scope *scope, AstNode *node, LVal lval,
5664 ResultLoc *result_loc)
5665{
52935666 assert(node->type == NodeTypeIfBoolExpr);
52945667
52955668 IrInstruction *condition = ir_gen_node(irb, node->data.if_bool_expr.condition, scope);
......@@ -5310,12 +5683,16 @@ static IrInstruction *ir_gen_if_bool_expr(IrBuilder *irb, Scope *scope, AstNode
53105683 IrBasicBlock *else_block = ir_create_basic_block(irb, scope, "Else");
53115684 IrBasicBlock *endif_block = ir_create_basic_block(irb, scope, "EndIf");
53125685
5313 ir_build_cond_br(irb, scope, condition->source_node, condition, then_block, else_block, is_comptime);
5686 IrInstruction *cond_br_inst = ir_build_cond_br(irb, scope, node, condition,
5687 then_block, else_block, is_comptime);
5688 ResultLocPeerParent *peer_parent = ir_build_binary_result_peers(irb, cond_br_inst, else_block, endif_block,
5689 result_loc, is_comptime);
53145690
53155691 ir_set_cursor_at_end_and_append_block(irb, then_block);
53165692
53175693 Scope *subexpr_scope = create_runtime_scope(irb->codegen, node, scope, is_comptime);
5318 IrInstruction *then_expr_result = ir_gen_node(irb, then_node, subexpr_scope);
5694 IrInstruction *then_expr_result = ir_gen_node_extra(irb, then_node, subexpr_scope, lval,
5695 &peer_parent->peers.at(0)->base);
53195696 if (then_expr_result == irb->codegen->invalid_instruction)
53205697 return irb->codegen->invalid_instruction;
53215698 IrBasicBlock *after_then_block = irb->current_basic_block;
......@@ -5325,11 +5702,12 @@ static IrInstruction *ir_gen_if_bool_expr(IrBuilder *irb, Scope *scope, AstNode
53255702 ir_set_cursor_at_end_and_append_block(irb, else_block);
53265703 IrInstruction *else_expr_result;
53275704 if (else_node) {
5328 else_expr_result = ir_gen_node(irb, else_node, subexpr_scope);
5705 else_expr_result = ir_gen_node_extra(irb, else_node, subexpr_scope, lval, &peer_parent->peers.at(1)->base);
53295706 if (else_expr_result == irb->codegen->invalid_instruction)
53305707 return irb->codegen->invalid_instruction;
53315708 } else {
53325709 else_expr_result = ir_build_const_void(irb, scope, node);
5710 ir_build_end_expr(irb, scope, node, else_expr_result, &peer_parent->peers.at(1)->base);
53335711 }
53345712 IrBasicBlock *after_else_block = irb->current_basic_block;
53355713 if (!instr_is_unreachable(else_expr_result))
......@@ -5343,14 +5721,15 @@ static IrInstruction *ir_gen_if_bool_expr(IrBuilder *irb, Scope *scope, AstNode
53435721 incoming_blocks[0] = after_then_block;
53445722 incoming_blocks[1] = after_else_block;
53455723
5346 return ir_build_phi(irb, scope, node, 2, incoming_blocks, incoming_values);
5724 IrInstruction *phi = ir_build_phi(irb, scope, node, 2, incoming_blocks, incoming_values, peer_parent);
5725 return ir_expr_wrap(irb, scope, phi, result_loc);
53475726}
53485727
53495728static IrInstruction *ir_gen_prefix_op_id_lval(IrBuilder *irb, Scope *scope, AstNode *node, IrUnOp op_id, LVal lval) {
53505729 assert(node->type == NodeTypePrefixOpExpr);
53515730 AstNode *expr_node = node->data.prefix_op_expr.primary_expr;
53525731
5353 IrInstruction *value = ir_gen_node_extra(irb, expr_node, scope, lval);
5732 IrInstruction *value = ir_gen_node_extra(irb, expr_node, scope, lval, nullptr);
53545733 if (value == irb->codegen->invalid_instruction)
53555734 return value;
53565735
......@@ -5361,15 +5740,34 @@ static IrInstruction *ir_gen_prefix_op_id(IrBuilder *irb, Scope *scope, AstNode
53615740 return ir_gen_prefix_op_id_lval(irb, scope, node, op_id, LValNone);
53625741}
53635742
5364static IrInstruction *ir_lval_wrap(IrBuilder *irb, Scope *scope, IrInstruction *value, LVal lval) {
5365 if (lval != LValPtr)
5743static IrInstruction *ir_expr_wrap(IrBuilder *irb, Scope *scope, IrInstruction *inst, ResultLoc *result_loc) {
5744 ir_build_end_expr(irb, scope, inst->source_node, inst, result_loc);
5745 return inst;
5746}
5747
5748static IrInstruction *ir_lval_wrap(IrBuilder *irb, Scope *scope, IrInstruction *value, LVal lval,
5749 ResultLoc *result_loc)
5750{
5751 // This logic must be kept in sync with
5752 // [STMT_EXPR_TEST_THING] <--- (search this token)
5753 if (value == irb->codegen->invalid_instruction ||
5754 instr_is_unreachable(value) ||
5755 value->source_node->type == NodeTypeDefer ||
5756 value->id == IrInstructionIdDeclVarSrc)
5757 {
53665758 return value;
5367 if (value == irb->codegen->invalid_instruction)
5759 }
5760
5761 if (lval == LValPtr) {
5762 // We needed a pointer to a value, but we got a value. So we create
5763 // an instruction which just makes a pointer of it.
5764 return ir_build_ref(irb, scope, value->source_node, value, false, false);
5765 } else if (result_loc != nullptr) {
5766 return ir_expr_wrap(irb, scope, value, result_loc);
5767 } else {
53685768 return value;
5769 }
53695770
5370 // We needed a pointer to a value, but we got a value. So we create
5371 // an instruction which just makes a const pointer of it.
5372 return ir_build_ref(irb, scope, value->source_node, value, false, false);
53735771}
53745772
53755773static PtrLen star_token_to_ptr_len(TokenId token_id) {
......@@ -5442,21 +5840,22 @@ static IrInstruction *ir_gen_pointer_type(IrBuilder *irb, Scope *scope, AstNode
54425840 ptr_len, align_value, bit_offset_start, host_int_bytes, is_allow_zero);
54435841}
54445842
5445static IrInstruction *ir_gen_catch_unreachable(IrBuilder *irb, Scope *scope, AstNode *source_node, AstNode *expr_node,
5446 LVal lval)
5843static IrInstruction *ir_gen_catch_unreachable(IrBuilder *irb, Scope *scope, AstNode *source_node,
5844 AstNode *expr_node, LVal lval, ResultLoc *result_loc)
54475845{
5448 IrInstruction *err_union_ptr = ir_gen_node_extra(irb, expr_node, scope, LValPtr);
5846 IrInstruction *err_union_ptr = ir_gen_node_extra(irb, expr_node, scope, LValPtr, nullptr);
54495847 if (err_union_ptr == irb->codegen->invalid_instruction)
54505848 return irb->codegen->invalid_instruction;
54515849
5452 IrInstruction *payload_ptr = ir_build_unwrap_err_payload(irb, scope, source_node, err_union_ptr, true);
5850 IrInstruction *payload_ptr = ir_build_unwrap_err_payload(irb, scope, source_node, err_union_ptr, true, false);
54535851 if (payload_ptr == irb->codegen->invalid_instruction)
54545852 return irb->codegen->invalid_instruction;
54555853
54565854 if (lval == LValPtr)
54575855 return payload_ptr;
54585856
5459 return ir_build_load_ptr(irb, scope, source_node, payload_ptr);
5857 IrInstruction *load_ptr = ir_build_load_ptr(irb, scope, source_node, payload_ptr);
5858 return ir_expr_wrap(irb, scope, load_ptr, result_loc);
54605859}
54615860
54625861static IrInstruction *ir_gen_bool_not(IrBuilder *irb, Scope *scope, AstNode *node) {
......@@ -5470,7 +5869,9 @@ static IrInstruction *ir_gen_bool_not(IrBuilder *irb, Scope *scope, AstNode *nod
54705869 return ir_build_bool_not(irb, scope, node, value);
54715870}
54725871
5473static IrInstruction *ir_gen_prefix_op_expr(IrBuilder *irb, Scope *scope, AstNode *node, LVal lval) {
5872static IrInstruction *ir_gen_prefix_op_expr(IrBuilder *irb, Scope *scope, AstNode *node, LVal lval,
5873 ResultLoc *result_loc)
5874{
54745875 assert(node->type == NodeTypePrefixOpExpr);
54755876
54765877 PrefixOp prefix_op = node->data.prefix_op_expr.prefix_op;
......@@ -5479,24 +5880,26 @@ static IrInstruction *ir_gen_prefix_op_expr(IrBuilder *irb, Scope *scope, AstNod
54795880 case PrefixOpInvalid:
54805881 zig_unreachable();
54815882 case PrefixOpBoolNot:
5482 return ir_lval_wrap(irb, scope, ir_gen_bool_not(irb, scope, node), lval);
5883 return ir_lval_wrap(irb, scope, ir_gen_bool_not(irb, scope, node), lval, result_loc);
54835884 case PrefixOpBinNot:
5484 return ir_lval_wrap(irb, scope, ir_gen_prefix_op_id(irb, scope, node, IrUnOpBinNot), lval);
5885 return ir_lval_wrap(irb, scope, ir_gen_prefix_op_id(irb, scope, node, IrUnOpBinNot), lval, result_loc);
54855886 case PrefixOpNegation:
5486 return ir_lval_wrap(irb, scope, ir_gen_prefix_op_id(irb, scope, node, IrUnOpNegation), lval);
5887 return ir_lval_wrap(irb, scope, ir_gen_prefix_op_id(irb, scope, node, IrUnOpNegation), lval, result_loc);
54875888 case PrefixOpNegationWrap:
5488 return ir_lval_wrap(irb, scope, ir_gen_prefix_op_id(irb, scope, node, IrUnOpNegationWrap), lval);
5889 return ir_lval_wrap(irb, scope, ir_gen_prefix_op_id(irb, scope, node, IrUnOpNegationWrap), lval, result_loc);
54895890 case PrefixOpOptional:
5490 return ir_lval_wrap(irb, scope, ir_gen_prefix_op_id(irb, scope, node, IrUnOpOptional), lval);
5891 return ir_lval_wrap(irb, scope, ir_gen_prefix_op_id(irb, scope, node, IrUnOpOptional), lval, result_loc);
54915892 case PrefixOpAddrOf: {
54925893 AstNode *expr_node = node->data.prefix_op_expr.primary_expr;
5493 return ir_lval_wrap(irb, scope, ir_gen_node_extra(irb, expr_node, scope, LValPtr), lval);
5894 return ir_lval_wrap(irb, scope, ir_gen_node_extra(irb, expr_node, scope, LValPtr, nullptr), lval, result_loc);
54945895 }
54955896 }
54965897 zig_unreachable();
54975898}
54985899
5499static IrInstruction *ir_gen_container_init_expr(IrBuilder *irb, Scope *scope, AstNode *node) {
5900static IrInstruction *ir_gen_container_init_expr(IrBuilder *irb, Scope *scope, AstNode *node, LVal lval,
5901 ResultLoc *parent_result_loc)
5902{
55005903 assert(node->type == NodeTypeContainerInitExpr);
55015904
55025905 AstNodeContainerInitExpr *container_init_expr = &node->data.container_init_expr;
......@@ -5514,45 +5917,104 @@ static IrInstruction *ir_gen_container_init_expr(IrBuilder *irb, Scope *scope, A
55145917 return container_type;
55155918 }
55165919
5517 if (kind == ContainerInitKindStruct) {
5518 if (elem_type != nullptr) {
5519 add_node_error(irb->codegen, container_init_expr->type,
5520 buf_sprintf("initializing array with struct syntax"));
5521 return irb->codegen->invalid_instruction;
5522 }
5920 switch (kind) {
5921 case ContainerInitKindStruct: {
5922 if (elem_type != nullptr) {
5923 add_node_error(irb->codegen, container_init_expr->type,
5924 buf_sprintf("initializing array with struct syntax"));
5925 return irb->codegen->invalid_instruction;
5926 }
5927
5928 IrInstruction *container_ptr = ir_build_resolve_result(irb, scope, node, parent_result_loc,
5929 container_type);
5930
5931 size_t field_count = container_init_expr->entries.length;
5932 IrInstructionContainerInitFieldsField *fields = allocate<IrInstructionContainerInitFieldsField>(field_count);
5933 for (size_t i = 0; i < field_count; i += 1) {
5934 AstNode *entry_node = container_init_expr->entries.at(i);
5935 assert(entry_node->type == NodeTypeStructValueField);
5936
5937 Buf *name = entry_node->data.struct_val_field.name;
5938 AstNode *expr_node = entry_node->data.struct_val_field.expr;
55235939
5524 size_t field_count = container_init_expr->entries.length;
5525 IrInstructionContainerInitFieldsField *fields = allocate<IrInstructionContainerInitFieldsField>(field_count);
5526 for (size_t i = 0; i < field_count; i += 1) {
5527 AstNode *entry_node = container_init_expr->entries.at(i);
5528 assert(entry_node->type == NodeTypeStructValueField);
5940 IrInstruction *field_ptr = ir_build_field_ptr(irb, scope, entry_node, container_ptr, name, true);
5941 ResultLocInstruction *result_loc_inst = allocate<ResultLocInstruction>(1);
5942 result_loc_inst->base.id = ResultLocIdInstruction;
5943 result_loc_inst->base.source_instruction = field_ptr;
5944 ir_ref_instruction(field_ptr, irb->current_basic_block);
5945 ir_build_reset_result(irb, scope, expr_node, &result_loc_inst->base);
55295946
5530 Buf *name = entry_node->data.struct_val_field.name;
5531 AstNode *expr_node = entry_node->data.struct_val_field.expr;
5532 IrInstruction *expr_value = ir_gen_node(irb, expr_node, scope);
5533 if (expr_value == irb->codegen->invalid_instruction)
5534 return expr_value;
5947 IrInstruction *expr_value = ir_gen_node_extra(irb, expr_node, scope, LValNone,
5948 &result_loc_inst->base);
5949 if (expr_value == irb->codegen->invalid_instruction)
5950 return expr_value;
55355951
5536 fields[i].name = name;
5537 fields[i].value = expr_value;
5538 fields[i].source_node = entry_node;
5952 fields[i].name = name;
5953 fields[i].source_node = entry_node;
5954 fields[i].result_loc = field_ptr;
5955 }
5956 IrInstruction *init_fields = ir_build_container_init_fields(irb, scope, node, container_type,
5957 field_count, fields, container_ptr);
5958
5959 return ir_lval_wrap(irb, scope, init_fields, lval, parent_result_loc);
55395960 }
5540 return ir_build_container_init_fields(irb, scope, node, container_type, field_count, fields);
5541 } else if (kind == ContainerInitKindArray) {
5542 size_t item_count = container_init_expr->entries.length;
5543 IrInstruction **values = allocate<IrInstruction *>(item_count);
5544 for (size_t i = 0; i < item_count; i += 1) {
5545 AstNode *expr_node = container_init_expr->entries.at(i);
5546 IrInstruction *expr_value = ir_gen_node(irb, expr_node, scope);
5547 if (expr_value == irb->codegen->invalid_instruction)
5548 return expr_value;
5961 case ContainerInitKindArray: {
5962 size_t item_count = container_init_expr->entries.length;
5963
5964 if (container_type == nullptr) {
5965 IrInstruction *item_count_inst = ir_build_const_usize(irb, scope, node, item_count);
5966 container_type = ir_build_array_type(irb, scope, node, item_count_inst, elem_type);
5967 }
55495968
5550 values[i] = expr_value;
5969 IrInstruction *container_ptr = ir_build_resolve_result(irb, scope, node, parent_result_loc,
5970 container_type);
5971
5972 IrInstruction **result_locs = allocate<IrInstruction *>(item_count);
5973 for (size_t i = 0; i < item_count; i += 1) {
5974 AstNode *expr_node = container_init_expr->entries.at(i);
5975
5976 IrInstruction *elem_index = ir_build_const_usize(irb, scope, expr_node, i);
5977 IrInstruction *elem_ptr = ir_build_elem_ptr(irb, scope, expr_node, container_ptr, elem_index,
5978 false, PtrLenSingle, container_type);
5979 ResultLocInstruction *result_loc_inst = allocate<ResultLocInstruction>(1);
5980 result_loc_inst->base.id = ResultLocIdInstruction;
5981 result_loc_inst->base.source_instruction = elem_ptr;
5982 ir_ref_instruction(elem_ptr, irb->current_basic_block);
5983 ir_build_reset_result(irb, scope, expr_node, &result_loc_inst->base);
5984
5985 IrInstruction *expr_value = ir_gen_node_extra(irb, expr_node, scope, LValNone,
5986 &result_loc_inst->base);
5987 if (expr_value == irb->codegen->invalid_instruction)
5988 return expr_value;
5989
5990 result_locs[i] = elem_ptr;
5991 }
5992 IrInstruction *init_list = ir_build_container_init_list(irb, scope, node, container_type,
5993 item_count, result_locs, container_ptr);
5994 return ir_lval_wrap(irb, scope, init_list, lval, parent_result_loc);
55515995 }
5552 return ir_build_container_init_list(irb, scope, node, container_type, elem_type, item_count, values);
5553 } else {
5554 zig_unreachable();
55555996 }
5997 zig_unreachable();
5998}
5999
6000static ResultLocVar *ir_build_var_result_loc(IrBuilder *irb, IrInstruction *alloca, ZigVar *var) {
6001 ResultLocVar *result_loc_var = allocate<ResultLocVar>(1);
6002 result_loc_var->base.id = ResultLocIdVar;
6003 result_loc_var->base.source_instruction = alloca;
6004 result_loc_var->var = var;
6005
6006 ir_build_reset_result(irb, alloca->scope, alloca->source_node, &result_loc_var->base);
6007
6008 return result_loc_var;
6009}
6010
6011static void build_decl_var_and_init(IrBuilder *irb, Scope *scope, AstNode *source_node, ZigVar *var,
6012 IrInstruction *init, const char *name_hint, IrInstruction *is_comptime)
6013{
6014 IrInstruction *alloca = ir_build_alloca_src(irb, scope, source_node, nullptr, name_hint, is_comptime);
6015 ResultLocVar *var_result_loc = ir_build_var_result_loc(irb, alloca, var);
6016 ir_build_end_expr(irb, scope, source_node, init, &var_result_loc->base);
6017 ir_build_var_decl_src(irb, scope, source_node, var, nullptr, alloca);
55566018}
55576019
55586020static IrInstruction *ir_gen_var_decl(IrBuilder *irb, Scope *scope, AstNode *node) {
......@@ -5565,9 +6027,12 @@ static IrInstruction *ir_gen_var_decl(IrBuilder *irb, Scope *scope, AstNode *nod
55656027 return irb->codegen->invalid_instruction;
55666028 }
55676029
6030 // Used for the type expr and the align expr
6031 Scope *comptime_scope = create_comptime_scope(irb->codegen, node, scope);
6032
55686033 IrInstruction *type_instruction;
55696034 if (variable_declaration->type != nullptr) {
5570 type_instruction = ir_gen_node(irb, variable_declaration->type, scope);
6035 type_instruction = ir_gen_node(irb, variable_declaration->type, comptime_scope);
55716036 if (type_instruction == irb->codegen->invalid_instruction)
55726037 return type_instruction;
55736038 } else {
......@@ -5578,8 +6043,8 @@ static IrInstruction *ir_gen_var_decl(IrBuilder *irb, Scope *scope, AstNode *nod
55786043 bool is_const = variable_declaration->is_const;
55796044 bool is_extern = variable_declaration->is_extern;
55806045
5581 IrInstruction *is_comptime = ir_build_const_bool(irb, scope, node,
5582 ir_should_inline(irb->exec, scope) || variable_declaration->is_comptime);
6046 bool is_comptime_scalar = ir_should_inline(irb->exec, scope) || variable_declaration->is_comptime;
6047 IrInstruction *is_comptime = ir_build_const_bool(irb, scope, node, is_comptime_scalar);
55836048 ZigVar *var = ir_create_var(irb, node, scope, variable_declaration->symbol,
55846049 is_const, is_const, is_shadowable, is_comptime);
55856050 // we detect IrInstructionIdDeclVarSrc in gen_block to make sure the next node
......@@ -5593,7 +6058,7 @@ static IrInstruction *ir_gen_var_decl(IrBuilder *irb, Scope *scope, AstNode *nod
55936058
55946059 IrInstruction *align_value = nullptr;
55956060 if (variable_declaration->align_expr != nullptr) {
5596 align_value = ir_gen_node(irb, variable_declaration->align_expr, scope);
6061 align_value = ir_gen_node(irb, variable_declaration->align_expr, comptime_scope);
55976062 if (align_value == irb->codegen->invalid_instruction)
55986063 return align_value;
55996064 }
......@@ -5606,20 +6071,39 @@ static IrInstruction *ir_gen_var_decl(IrBuilder *irb, Scope *scope, AstNode *nod
56066071 // Parser should ensure that this never happens
56076072 assert(variable_declaration->threadlocal_tok == nullptr);
56086073
6074 IrInstruction *alloca = ir_build_alloca_src(irb, scope, node, align_value,
6075 buf_ptr(variable_declaration->symbol), is_comptime);
6076
6077 // Create a result location for the initialization expression.
6078 ResultLocVar *result_loc_var = ir_build_var_result_loc(irb, alloca, var);
6079 ResultLoc *init_result_loc = (type_instruction == nullptr) ? &result_loc_var->base : nullptr;
6080
6081 Scope *init_scope = is_comptime_scalar ?
6082 create_comptime_scope(irb->codegen, variable_declaration->expr, scope) : scope;
6083
56096084 // Temporarily set the name of the IrExecutable to the VariableDeclaration
56106085 // so that the struct or enum from the init expression inherits the name.
56116086 Buf *old_exec_name = irb->exec->name;
56126087 irb->exec->name = variable_declaration->symbol;
5613 IrInstruction *init_value = ir_gen_node(irb, variable_declaration->expr, scope);
6088 IrInstruction *init_value = ir_gen_node_extra(irb, variable_declaration->expr, init_scope,
6089 LValNone, init_result_loc);
56146090 irb->exec->name = old_exec_name;
56156091
56166092 if (init_value == irb->codegen->invalid_instruction)
5617 return init_value;
6093 return irb->codegen->invalid_instruction;
6094
6095 if (type_instruction != nullptr) {
6096 IrInstruction *implicit_cast = ir_build_implicit_cast(irb, scope, node, type_instruction, init_value,
6097 &result_loc_var->base);
6098 ir_build_end_expr(irb, scope, node, implicit_cast, &result_loc_var->base);
6099 }
56186100
5619 return ir_build_var_decl_src(irb, scope, node, var, type_instruction, align_value, init_value);
6101 return ir_build_var_decl_src(irb, scope, node, var, align_value, alloca);
56206102}
56216103
5622static IrInstruction *ir_gen_while_expr(IrBuilder *irb, Scope *scope, AstNode *node) {
6104static IrInstruction *ir_gen_while_expr(IrBuilder *irb, Scope *scope, AstNode *node, LVal lval,
6105 ResultLoc *result_loc)
6106{
56236107 assert(node->type == NodeTypeWhileExpr);
56246108
56256109 AstNode *continue_expr_node = node->data.while_expr.continue_expr;
......@@ -5654,25 +6138,33 @@ static IrInstruction *ir_gen_while_expr(IrBuilder *irb, Scope *scope, AstNode *n
56546138 } else {
56556139 payload_scope = subexpr_scope;
56566140 }
5657 IrInstruction *err_val_ptr = ir_gen_node_extra(irb, node->data.while_expr.condition, subexpr_scope, LValPtr);
6141 IrInstruction *err_val_ptr = ir_gen_node_extra(irb, node->data.while_expr.condition, subexpr_scope,
6142 LValPtr, nullptr);
56586143 if (err_val_ptr == irb->codegen->invalid_instruction)
56596144 return err_val_ptr;
5660 IrInstruction *err_val = ir_build_load_ptr(irb, scope, node->data.while_expr.condition, err_val_ptr);
5661 IrInstruction *is_err = ir_build_test_err(irb, scope, node->data.while_expr.condition, err_val);
6145 IrInstruction *is_err = ir_build_test_err_src(irb, scope, node->data.while_expr.condition, err_val_ptr, true);
56626146 IrBasicBlock *after_cond_block = irb->current_basic_block;
56636147 IrInstruction *void_else_result = else_node ? nullptr : ir_mark_gen(ir_build_const_void(irb, scope, node));
6148 IrInstruction *cond_br_inst;
56646149 if (!instr_is_unreachable(is_err)) {
5665 ir_mark_gen(ir_build_cond_br(irb, scope, node->data.while_expr.condition, is_err,
5666 else_block, body_block, is_comptime));
6150 cond_br_inst = ir_build_cond_br(irb, scope, node->data.while_expr.condition, is_err,
6151 else_block, body_block, is_comptime);
6152 cond_br_inst->is_gen = true;
6153 } else {
6154 // for the purposes of the source instruction to ir_build_result_peers
6155 cond_br_inst = irb->current_basic_block->instruction_list.last();
56676156 }
56686157
6158 ResultLocPeerParent *peer_parent = ir_build_result_peers(irb, cond_br_inst, end_block, result_loc,
6159 is_comptime);
6160
56696161 ir_set_cursor_at_end_and_append_block(irb, body_block);
56706162 if (var_symbol) {
5671 IrInstruction *var_ptr_value = ir_build_unwrap_err_payload(irb, payload_scope, symbol_node,
5672 err_val_ptr, false);
5673 IrInstruction *var_value = node->data.while_expr.var_is_ptr ?
5674 var_ptr_value : ir_build_load_ptr(irb, payload_scope, symbol_node, var_ptr_value);
5675 ir_build_var_decl_src(irb, payload_scope, symbol_node, payload_var, nullptr, nullptr, var_value);
6163 IrInstruction *payload_ptr = ir_build_unwrap_err_payload(irb, payload_scope, symbol_node,
6164 err_val_ptr, false, false);
6165 IrInstruction *var_ptr = node->data.while_expr.var_is_ptr ?
6166 ir_build_ref(irb, payload_scope, symbol_node, payload_ptr, true, false) : payload_ptr;
6167 ir_build_var_decl_src(irb, payload_scope, symbol_node, payload_var, nullptr, var_ptr);
56766168 }
56776169
56786170 ZigList<IrInstruction *> incoming_values = {0};
......@@ -5684,7 +6176,12 @@ static IrInstruction *ir_gen_while_expr(IrBuilder *irb, Scope *scope, AstNode *n
56846176 loop_scope->is_comptime = is_comptime;
56856177 loop_scope->incoming_blocks = &incoming_blocks;
56866178 loop_scope->incoming_values = &incoming_values;
6179 loop_scope->lval = lval;
6180 loop_scope->peer_parent = peer_parent;
56876181
6182 // Note the body block of the loop is not the place that lval and result_loc are used -
6183 // it's actually in break statements, handled similarly to return statements.
6184 // That is why we set those values in loop_scope above and not in this ir_gen_node call.
56886185 IrInstruction *body_result = ir_gen_node(irb, node->data.while_expr.body, &loop_scope->base);
56896186 if (body_result == irb->codegen->invalid_instruction)
56906187 return body_result;
......@@ -5713,10 +6210,15 @@ static IrInstruction *ir_gen_while_expr(IrBuilder *irb, Scope *scope, AstNode *n
57136210 ZigVar *err_var = ir_create_var(irb, err_symbol_node, scope, err_symbol,
57146211 true, false, false, is_comptime);
57156212 Scope *err_scope = err_var->child_scope;
5716 IrInstruction *err_var_value = ir_build_unwrap_err_code(irb, err_scope, err_symbol_node, err_val_ptr);
5717 ir_build_var_decl_src(irb, err_scope, symbol_node, err_var, nullptr, nullptr, err_var_value);
6213 IrInstruction *err_ptr = ir_build_unwrap_err_code(irb, err_scope, err_symbol_node, err_val_ptr);
6214 ir_build_var_decl_src(irb, err_scope, symbol_node, err_var, nullptr, err_ptr);
57186215
5719 IrInstruction *else_result = ir_gen_node(irb, else_node, err_scope);
6216 if (peer_parent->peers.length != 0) {
6217 peer_parent->peers.last()->next_bb = else_block;
6218 }
6219 ResultLocPeer *peer_result = create_peer_result(peer_parent);
6220 peer_parent->peers.append(peer_result);
6221 IrInstruction *else_result = ir_gen_node_extra(irb, else_node, err_scope, lval, &peer_result->base);
57206222 if (else_result == irb->codegen->invalid_instruction)
57216223 return else_result;
57226224 if (!instr_is_unreachable(else_result))
......@@ -5730,8 +6232,13 @@ static IrInstruction *ir_gen_while_expr(IrBuilder *irb, Scope *scope, AstNode *n
57306232 incoming_blocks.append(after_cond_block);
57316233 incoming_values.append(void_else_result);
57326234 }
6235 if (peer_parent->peers.length != 0) {
6236 peer_parent->peers.last()->next_bb = end_block;
6237 }
57336238
5734 return ir_build_phi(irb, scope, node, incoming_blocks.length, incoming_blocks.items, incoming_values.items);
6239 IrInstruction *phi = ir_build_phi(irb, scope, node, incoming_blocks.length,
6240 incoming_blocks.items, incoming_values.items, peer_parent);
6241 return ir_expr_wrap(irb, scope, phi, result_loc);
57356242 } else if (var_symbol != nullptr) {
57366243 ir_set_cursor_at_end_and_append_block(irb, cond_block);
57376244 Scope *subexpr_scope = create_runtime_scope(irb->codegen, node, scope, is_comptime);
......@@ -5741,23 +6248,32 @@ static IrInstruction *ir_gen_while_expr(IrBuilder *irb, Scope *scope, AstNode *n
57416248 ZigVar *payload_var = ir_create_var(irb, symbol_node, subexpr_scope, var_symbol,
57426249 true, false, false, is_comptime);
57436250 Scope *child_scope = payload_var->child_scope;
5744 IrInstruction *maybe_val_ptr = ir_gen_node_extra(irb, node->data.while_expr.condition, subexpr_scope, LValPtr);
6251 IrInstruction *maybe_val_ptr = ir_gen_node_extra(irb, node->data.while_expr.condition, subexpr_scope,
6252 LValPtr, nullptr);
57456253 if (maybe_val_ptr == irb->codegen->invalid_instruction)
57466254 return maybe_val_ptr;
57476255 IrInstruction *maybe_val = ir_build_load_ptr(irb, scope, node->data.while_expr.condition, maybe_val_ptr);
57486256 IrInstruction *is_non_null = ir_build_test_nonnull(irb, scope, node->data.while_expr.condition, maybe_val);
57496257 IrBasicBlock *after_cond_block = irb->current_basic_block;
57506258 IrInstruction *void_else_result = else_node ? nullptr : ir_mark_gen(ir_build_const_void(irb, scope, node));
6259 IrInstruction *cond_br_inst;
57516260 if (!instr_is_unreachable(is_non_null)) {
5752 ir_mark_gen(ir_build_cond_br(irb, scope, node->data.while_expr.condition, is_non_null,
5753 body_block, else_block, is_comptime));
6261 cond_br_inst = ir_build_cond_br(irb, scope, node->data.while_expr.condition, is_non_null,
6262 body_block, else_block, is_comptime);
6263 cond_br_inst->is_gen = true;
6264 } else {
6265 // for the purposes of the source instruction to ir_build_result_peers
6266 cond_br_inst = irb->current_basic_block->instruction_list.last();
57546267 }
57556268
6269 ResultLocPeerParent *peer_parent = ir_build_result_peers(irb, cond_br_inst, end_block, result_loc,
6270 is_comptime);
6271
57566272 ir_set_cursor_at_end_and_append_block(irb, body_block);
5757 IrInstruction *var_ptr_value = ir_build_optional_unwrap_ptr(irb, child_scope, symbol_node, maybe_val_ptr, false);
5758 IrInstruction *var_value = node->data.while_expr.var_is_ptr ?
5759 var_ptr_value : ir_build_load_ptr(irb, child_scope, symbol_node, var_ptr_value);
5760 ir_build_var_decl_src(irb, child_scope, symbol_node, payload_var, nullptr, nullptr, var_value);
6273 IrInstruction *payload_ptr = ir_build_optional_unwrap_ptr(irb, child_scope, symbol_node, maybe_val_ptr, false, false);
6274 IrInstruction *var_ptr = node->data.while_expr.var_is_ptr ?
6275 ir_build_ref(irb, child_scope, symbol_node, payload_ptr, true, false) : payload_ptr;
6276 ir_build_var_decl_src(irb, child_scope, symbol_node, payload_var, nullptr, var_ptr);
57616277
57626278 ZigList<IrInstruction *> incoming_values = {0};
57636279 ZigList<IrBasicBlock *> incoming_blocks = {0};
......@@ -5768,7 +6284,12 @@ static IrInstruction *ir_gen_while_expr(IrBuilder *irb, Scope *scope, AstNode *n
57686284 loop_scope->is_comptime = is_comptime;
57696285 loop_scope->incoming_blocks = &incoming_blocks;
57706286 loop_scope->incoming_values = &incoming_values;
6287 loop_scope->lval = lval;
6288 loop_scope->peer_parent = peer_parent;
57716289
6290 // Note the body block of the loop is not the place that lval and result_loc are used -
6291 // it's actually in break statements, handled similarly to return statements.
6292 // That is why we set those values in loop_scope above and not in this ir_gen_node call.
57726293 IrInstruction *body_result = ir_gen_node(irb, node->data.while_expr.body, &loop_scope->base);
57736294 if (body_result == irb->codegen->invalid_instruction)
57746295 return body_result;
......@@ -5793,7 +6314,12 @@ static IrInstruction *ir_gen_while_expr(IrBuilder *irb, Scope *scope, AstNode *n
57936314 if (else_node) {
57946315 ir_set_cursor_at_end_and_append_block(irb, else_block);
57956316
5796 else_result = ir_gen_node(irb, else_node, scope);
6317 if (peer_parent->peers.length != 0) {
6318 peer_parent->peers.last()->next_bb = else_block;
6319 }
6320 ResultLocPeer *peer_result = create_peer_result(peer_parent);
6321 peer_parent->peers.append(peer_result);
6322 else_result = ir_gen_node_extra(irb, else_node, scope, lval, &peer_result->base);
57976323 if (else_result == irb->codegen->invalid_instruction)
57986324 return else_result;
57996325 if (!instr_is_unreachable(else_result))
......@@ -5808,8 +6334,13 @@ static IrInstruction *ir_gen_while_expr(IrBuilder *irb, Scope *scope, AstNode *n
58086334 incoming_blocks.append(after_cond_block);
58096335 incoming_values.append(void_else_result);
58106336 }
6337 if (peer_parent->peers.length != 0) {
6338 peer_parent->peers.last()->next_bb = end_block;
6339 }
58116340
5812 return ir_build_phi(irb, scope, node, incoming_blocks.length, incoming_blocks.items, incoming_values.items);
6341 IrInstruction *phi = ir_build_phi(irb, scope, node, incoming_blocks.length,
6342 incoming_blocks.items, incoming_values.items, peer_parent);
6343 return ir_expr_wrap(irb, scope, phi, result_loc);
58136344 } else {
58146345 ir_set_cursor_at_end_and_append_block(irb, cond_block);
58156346 IrInstruction *cond_val = ir_gen_node(irb, node->data.while_expr.condition, scope);
......@@ -5817,11 +6348,18 @@ static IrInstruction *ir_gen_while_expr(IrBuilder *irb, Scope *scope, AstNode *n
58176348 return cond_val;
58186349 IrBasicBlock *after_cond_block = irb->current_basic_block;
58196350 IrInstruction *void_else_result = else_node ? nullptr : ir_mark_gen(ir_build_const_void(irb, scope, node));
6351 IrInstruction *cond_br_inst;
58206352 if (!instr_is_unreachable(cond_val)) {
5821 ir_mark_gen(ir_build_cond_br(irb, scope, node->data.while_expr.condition, cond_val,
5822 body_block, else_block, is_comptime));
6353 cond_br_inst = ir_build_cond_br(irb, scope, node->data.while_expr.condition, cond_val,
6354 body_block, else_block, is_comptime);
6355 cond_br_inst->is_gen = true;
6356 } else {
6357 // for the purposes of the source instruction to ir_build_result_peers
6358 cond_br_inst = irb->current_basic_block->instruction_list.last();
58236359 }
58246360
6361 ResultLocPeerParent *peer_parent = ir_build_result_peers(irb, cond_br_inst, end_block, result_loc,
6362 is_comptime);
58256363 ir_set_cursor_at_end_and_append_block(irb, body_block);
58266364
58276365 ZigList<IrInstruction *> incoming_values = {0};
......@@ -5835,7 +6373,12 @@ static IrInstruction *ir_gen_while_expr(IrBuilder *irb, Scope *scope, AstNode *n
58356373 loop_scope->is_comptime = is_comptime;
58366374 loop_scope->incoming_blocks = &incoming_blocks;
58376375 loop_scope->incoming_values = &incoming_values;
6376 loop_scope->lval = lval;
6377 loop_scope->peer_parent = peer_parent;
58386378
6379 // Note the body block of the loop is not the place that lval and result_loc are used -
6380 // it's actually in break statements, handled similarly to return statements.
6381 // That is why we set those values in loop_scope above and not in this ir_gen_node call.
58396382 IrInstruction *body_result = ir_gen_node(irb, node->data.while_expr.body, &loop_scope->base);
58406383 if (body_result == irb->codegen->invalid_instruction)
58416384 return body_result;
......@@ -5860,7 +6403,13 @@ static IrInstruction *ir_gen_while_expr(IrBuilder *irb, Scope *scope, AstNode *n
58606403 if (else_node) {
58616404 ir_set_cursor_at_end_and_append_block(irb, else_block);
58626405
5863 else_result = ir_gen_node(irb, else_node, subexpr_scope);
6406 if (peer_parent->peers.length != 0) {
6407 peer_parent->peers.last()->next_bb = else_block;
6408 }
6409 ResultLocPeer *peer_result = create_peer_result(peer_parent);
6410 peer_parent->peers.append(peer_result);
6411
6412 else_result = ir_gen_node_extra(irb, else_node, subexpr_scope, lval, &peer_result->base);
58646413 if (else_result == irb->codegen->invalid_instruction)
58656414 return else_result;
58666415 if (!instr_is_unreachable(else_result))
......@@ -5875,12 +6424,19 @@ static IrInstruction *ir_gen_while_expr(IrBuilder *irb, Scope *scope, AstNode *n
58756424 incoming_blocks.append(after_cond_block);
58766425 incoming_values.append(void_else_result);
58776426 }
6427 if (peer_parent->peers.length != 0) {
6428 peer_parent->peers.last()->next_bb = end_block;
6429 }
58786430
5879 return ir_build_phi(irb, scope, node, incoming_blocks.length, incoming_blocks.items, incoming_values.items);
6431 IrInstruction *phi = ir_build_phi(irb, scope, node, incoming_blocks.length,
6432 incoming_blocks.items, incoming_values.items, peer_parent);
6433 return ir_expr_wrap(irb, scope, phi, result_loc);
58806434 }
58816435}
58826436
5883static IrInstruction *ir_gen_for_expr(IrBuilder *irb, Scope *parent_scope, AstNode *node) {
6437static IrInstruction *ir_gen_for_expr(IrBuilder *irb, Scope *parent_scope, AstNode *node, LVal lval,
6438 ResultLoc *result_loc)
6439{
58846440 assert(node->type == NodeTypeForExpr);
58856441
58866442 AstNode *array_node = node->data.for_expr.array_expr;
......@@ -5895,76 +6451,67 @@ static IrInstruction *ir_gen_for_expr(IrBuilder *irb, Scope *parent_scope, AstNo
58956451 }
58966452 assert(elem_node->type == NodeTypeSymbol);
58976453
5898 IrInstruction *array_val_ptr = ir_gen_node_extra(irb, array_node, parent_scope, LValPtr);
6454 IrInstruction *array_val_ptr = ir_gen_node_extra(irb, array_node, parent_scope, LValPtr, nullptr);
58996455 if (array_val_ptr == irb->codegen->invalid_instruction)
59006456 return array_val_ptr;
59016457
5902 IrInstruction *pointer_type = ir_build_to_ptr_type(irb, parent_scope, array_node, array_val_ptr);
5903 IrInstruction *elem_var_type;
5904 if (node->data.for_expr.elem_is_ptr) {
5905 elem_var_type = pointer_type;
5906 } else {
5907 elem_var_type = ir_build_ptr_type_child(irb, parent_scope, elem_node, pointer_type);
5908 }
5909
59106458 IrInstruction *is_comptime = ir_build_const_bool(irb, parent_scope, node,
59116459 ir_should_inline(irb->exec, parent_scope) || node->data.for_expr.is_inline);
59126460
5913 // TODO make it an error to write to element variable or i variable.
5914 Buf *elem_var_name = elem_node->data.symbol_expr.symbol;
5915 ZigVar *elem_var = ir_create_var(irb, elem_node, parent_scope, elem_var_name, true, false, false, is_comptime);
5916 Scope *child_scope = elem_var->child_scope;
5917
5918 IrInstruction *undefined_value = ir_build_const_undefined(irb, child_scope, elem_node);
5919 ir_build_var_decl_src(irb, child_scope, elem_node, elem_var, elem_var_type, nullptr, undefined_value);
5920 IrInstruction *elem_var_ptr = ir_build_var_ptr(irb, child_scope, node, elem_var);
5921
59226461 AstNode *index_var_source_node;
59236462 ZigVar *index_var;
6463 const char *index_var_name;
59246464 if (index_node) {
59256465 index_var_source_node = index_node;
5926 Buf *index_var_name = index_node->data.symbol_expr.symbol;
5927 index_var = ir_create_var(irb, index_node, child_scope, index_var_name, true, false, false, is_comptime);
6466 Buf *index_var_name_buf = index_node->data.symbol_expr.symbol;
6467 index_var = ir_create_var(irb, index_node, parent_scope, index_var_name_buf, true, false, false, is_comptime);
6468 index_var_name = buf_ptr(index_var_name_buf);
59286469 } else {
59296470 index_var_source_node = node;
5930 index_var = ir_create_var(irb, node, child_scope, nullptr, true, false, true, is_comptime);
6471 index_var = ir_create_var(irb, node, parent_scope, nullptr, true, false, true, is_comptime);
6472 index_var_name = "i";
59316473 }
5932 child_scope = index_var->child_scope;
59336474
5934 IrInstruction *usize = ir_build_const_type(irb, child_scope, node, irb->codegen->builtin_types.entry_usize);
5935 IrInstruction *zero = ir_build_const_usize(irb, child_scope, node, 0);
5936 IrInstruction *one = ir_build_const_usize(irb, child_scope, node, 1);
5937 ir_build_var_decl_src(irb, child_scope, index_var_source_node, index_var, usize, nullptr, zero);
5938 IrInstruction *index_ptr = ir_build_var_ptr(irb, child_scope, node, index_var);
6475 IrInstruction *zero = ir_build_const_usize(irb, parent_scope, node, 0);
6476 build_decl_var_and_init(irb, parent_scope, index_var_source_node, index_var, zero, index_var_name, is_comptime);
6477 parent_scope = index_var->child_scope;
6478
6479 IrInstruction *one = ir_build_const_usize(irb, parent_scope, node, 1);
6480 IrInstruction *index_ptr = ir_build_var_ptr(irb, parent_scope, node, index_var);
59396481
59406482
5941 IrBasicBlock *cond_block = ir_create_basic_block(irb, child_scope, "ForCond");
5942 IrBasicBlock *body_block = ir_create_basic_block(irb, child_scope, "ForBody");
5943 IrBasicBlock *end_block = ir_create_basic_block(irb, child_scope, "ForEnd");
5944 IrBasicBlock *else_block = else_node ? ir_create_basic_block(irb, child_scope, "ForElse") : end_block;
5945 IrBasicBlock *continue_block = ir_create_basic_block(irb, child_scope, "ForContinue");
6483 IrBasicBlock *cond_block = ir_create_basic_block(irb, parent_scope, "ForCond");
6484 IrBasicBlock *body_block = ir_create_basic_block(irb, parent_scope, "ForBody");
6485 IrBasicBlock *end_block = ir_create_basic_block(irb, parent_scope, "ForEnd");
6486 IrBasicBlock *else_block = else_node ? ir_create_basic_block(irb, parent_scope, "ForElse") : end_block;
6487 IrBasicBlock *continue_block = ir_create_basic_block(irb, parent_scope, "ForContinue");
59466488
59476489 Buf *len_field_name = buf_create_from_str("len");
5948 IrInstruction *len_ref = ir_build_field_ptr(irb, child_scope, node, array_val_ptr, len_field_name);
5949 IrInstruction *len_val = ir_build_load_ptr(irb, child_scope, node, len_ref);
5950 ir_build_br(irb, child_scope, node, cond_block, is_comptime);
6490 IrInstruction *len_ref = ir_build_field_ptr(irb, parent_scope, node, array_val_ptr, len_field_name, false);
6491 IrInstruction *len_val = ir_build_load_ptr(irb, parent_scope, node, len_ref);
6492 ir_build_br(irb, parent_scope, node, cond_block, is_comptime);
59516493
59526494 ir_set_cursor_at_end_and_append_block(irb, cond_block);
5953 IrInstruction *index_val = ir_build_load_ptr(irb, child_scope, node, index_ptr);
5954 IrInstruction *cond = ir_build_bin_op(irb, child_scope, node, IrBinOpCmpLessThan, index_val, len_val, false);
6495 IrInstruction *index_val = ir_build_load_ptr(irb, parent_scope, node, index_ptr);
6496 IrInstruction *cond = ir_build_bin_op(irb, parent_scope, node, IrBinOpCmpLessThan, index_val, len_val, false);
59556497 IrBasicBlock *after_cond_block = irb->current_basic_block;
59566498 IrInstruction *void_else_value = else_node ? nullptr : ir_mark_gen(ir_build_const_void(irb, parent_scope, node));
5957 ir_mark_gen(ir_build_cond_br(irb, child_scope, node, cond, body_block, else_block, is_comptime));
6499 IrInstruction *cond_br_inst = ir_mark_gen(ir_build_cond_br(irb, parent_scope, node, cond,
6500 body_block, else_block, is_comptime));
6501
6502 ResultLocPeerParent *peer_parent = ir_build_result_peers(irb, cond_br_inst, end_block, result_loc, is_comptime);
59586503
59596504 ir_set_cursor_at_end_and_append_block(irb, body_block);
5960 IrInstruction *elem_ptr = ir_build_elem_ptr(irb, child_scope, node, array_val_ptr, index_val, false, PtrLenSingle);
5961 IrInstruction *elem_val;
5962 if (node->data.for_expr.elem_is_ptr) {
5963 elem_val = elem_ptr;
5964 } else {
5965 elem_val = ir_build_load_ptr(irb, child_scope, node, elem_ptr);
5966 }
5967 ir_mark_gen(ir_build_store_ptr(irb, child_scope, node, elem_var_ptr, elem_val));
6505 IrInstruction *elem_ptr = ir_build_elem_ptr(irb, parent_scope, node, array_val_ptr, index_val, false,
6506 PtrLenSingle, nullptr);
6507 // TODO make it an error to write to element variable or i variable.
6508 Buf *elem_var_name = elem_node->data.symbol_expr.symbol;
6509 ZigVar *elem_var = ir_create_var(irb, elem_node, parent_scope, elem_var_name, true, false, false, is_comptime);
6510 Scope *child_scope = elem_var->child_scope;
6511
6512 IrInstruction *var_ptr = node->data.for_expr.elem_is_ptr ?
6513 ir_build_ref(irb, parent_scope, elem_node, elem_ptr, true, false) : elem_ptr;
6514 ir_build_var_decl_src(irb, parent_scope, elem_node, elem_var, nullptr, var_ptr);
59686515
59696516 ZigList<IrInstruction *> incoming_values = {0};
59706517 ZigList<IrBasicBlock *> incoming_blocks = {0};
......@@ -5974,7 +6521,12 @@ static IrInstruction *ir_gen_for_expr(IrBuilder *irb, Scope *parent_scope, AstNo
59746521 loop_scope->is_comptime = is_comptime;
59756522 loop_scope->incoming_blocks = &incoming_blocks;
59766523 loop_scope->incoming_values = &incoming_values;
6524 loop_scope->lval = lval;
6525 loop_scope->peer_parent = peer_parent;
59776526
6527 // Note the body block of the loop is not the place that lval and result_loc are used -
6528 // it's actually in break statements, handled similarly to return statements.
6529 // That is why we set those values in loop_scope above and not in this ir_gen_node call.
59786530 IrInstruction *body_result = ir_gen_node(irb, body_node, &loop_scope->base);
59796531
59806532 if (!instr_is_unreachable(body_result)) {
......@@ -5991,7 +6543,12 @@ static IrInstruction *ir_gen_for_expr(IrBuilder *irb, Scope *parent_scope, AstNo
59916543 if (else_node) {
59926544 ir_set_cursor_at_end_and_append_block(irb, else_block);
59936545
5994 else_result = ir_gen_node(irb, else_node, parent_scope);
6546 if (peer_parent->peers.length != 0) {
6547 peer_parent->peers.last()->next_bb = else_block;
6548 }
6549 ResultLocPeer *peer_result = create_peer_result(peer_parent);
6550 peer_parent->peers.append(peer_result);
6551 else_result = ir_gen_node_extra(irb, else_node, parent_scope, lval, &peer_result->base);
59956552 if (else_result == irb->codegen->invalid_instruction)
59966553 return else_result;
59976554 if (!instr_is_unreachable(else_result))
......@@ -6007,8 +6564,13 @@ static IrInstruction *ir_gen_for_expr(IrBuilder *irb, Scope *parent_scope, AstNo
60076564 incoming_blocks.append(after_cond_block);
60086565 incoming_values.append(void_else_value);
60096566 }
6567 if (peer_parent->peers.length != 0) {
6568 peer_parent->peers.last()->next_bb = end_block;
6569 }
60106570
6011 return ir_build_phi(irb, parent_scope, node, incoming_blocks.length, incoming_blocks.items, incoming_values.items);
6571 IrInstruction *phi = ir_build_phi(irb, parent_scope, node, incoming_blocks.length,
6572 incoming_blocks.items, incoming_values.items, peer_parent);
6573 return ir_expr_wrap(irb, parent_scope, phi, result_loc);
60126574}
60136575
60146576static IrInstruction *ir_gen_bool_literal(IrBuilder *irb, Scope *scope, AstNode *node) {
......@@ -6293,7 +6855,9 @@ static IrInstruction *ir_gen_asm_expr(IrBuilder *irb, Scope *scope, AstNode *nod
62936855 input_list, output_types, output_vars, return_count, is_volatile);
62946856}
62956857
6296static IrInstruction *ir_gen_if_optional_expr(IrBuilder *irb, Scope *scope, AstNode *node) {
6858static IrInstruction *ir_gen_if_optional_expr(IrBuilder *irb, Scope *scope, AstNode *node, LVal lval,
6859 ResultLoc *result_loc)
6860{
62976861 assert(node->type == NodeTypeIfOptional);
62986862
62996863 Buf *var_symbol = node->data.test_expr.var_symbol;
......@@ -6302,7 +6866,7 @@ static IrInstruction *ir_gen_if_optional_expr(IrBuilder *irb, Scope *scope, AstN
63026866 AstNode *else_node = node->data.test_expr.else_node;
63036867 bool var_is_ptr = node->data.test_expr.var_is_ptr;
63046868
6305 IrInstruction *maybe_val_ptr = ir_gen_node_extra(irb, expr_node, scope, LValPtr);
6869 IrInstruction *maybe_val_ptr = ir_gen_node_extra(irb, expr_node, scope, LValPtr, nullptr);
63066870 if (maybe_val_ptr == irb->codegen->invalid_instruction)
63076871 return maybe_val_ptr;
63086872
......@@ -6319,27 +6883,31 @@ static IrInstruction *ir_gen_if_optional_expr(IrBuilder *irb, Scope *scope, AstN
63196883 } else {
63206884 is_comptime = ir_build_test_comptime(irb, scope, node, is_non_null);
63216885 }
6322 ir_build_cond_br(irb, scope, node, is_non_null, then_block, else_block, is_comptime);
6886 IrInstruction *cond_br_inst = ir_build_cond_br(irb, scope, node, is_non_null,
6887 then_block, else_block, is_comptime);
6888
6889 ResultLocPeerParent *peer_parent = ir_build_binary_result_peers(irb, cond_br_inst, else_block, endif_block,
6890 result_loc, is_comptime);
63236891
63246892 ir_set_cursor_at_end_and_append_block(irb, then_block);
63256893
63266894 Scope *subexpr_scope = create_runtime_scope(irb->codegen, node, scope, is_comptime);
63276895 Scope *var_scope;
63286896 if (var_symbol) {
6329 IrInstruction *var_type = nullptr;
63306897 bool is_shadowable = false;
63316898 bool is_const = true;
63326899 ZigVar *var = ir_create_var(irb, node, subexpr_scope,
63336900 var_symbol, is_const, is_const, is_shadowable, is_comptime);
63346901
6335 IrInstruction *var_ptr_value = ir_build_optional_unwrap_ptr(irb, subexpr_scope, node, maybe_val_ptr, false);
6336 IrInstruction *var_value = var_is_ptr ? var_ptr_value : ir_build_load_ptr(irb, subexpr_scope, node, var_ptr_value);
6337 ir_build_var_decl_src(irb, subexpr_scope, node, var, var_type, nullptr, var_value);
6902 IrInstruction *payload_ptr = ir_build_optional_unwrap_ptr(irb, subexpr_scope, node, maybe_val_ptr, false, false);
6903 IrInstruction *var_ptr = var_is_ptr ? ir_build_ref(irb, subexpr_scope, node, payload_ptr, true, false) : payload_ptr;
6904 ir_build_var_decl_src(irb, subexpr_scope, node, var, nullptr, var_ptr);
63386905 var_scope = var->child_scope;
63396906 } else {
63406907 var_scope = subexpr_scope;
63416908 }
6342 IrInstruction *then_expr_result = ir_gen_node(irb, then_node, var_scope);
6909 IrInstruction *then_expr_result = ir_gen_node_extra(irb, then_node, var_scope, lval,
6910 &peer_parent->peers.at(0)->base);
63436911 if (then_expr_result == irb->codegen->invalid_instruction)
63446912 return then_expr_result;
63456913 IrBasicBlock *after_then_block = irb->current_basic_block;
......@@ -6349,11 +6917,12 @@ static IrInstruction *ir_gen_if_optional_expr(IrBuilder *irb, Scope *scope, AstN
63496917 ir_set_cursor_at_end_and_append_block(irb, else_block);
63506918 IrInstruction *else_expr_result;
63516919 if (else_node) {
6352 else_expr_result = ir_gen_node(irb, else_node, subexpr_scope);
6920 else_expr_result = ir_gen_node_extra(irb, else_node, subexpr_scope, lval, &peer_parent->peers.at(1)->base);
63536921 if (else_expr_result == irb->codegen->invalid_instruction)
63546922 return else_expr_result;
63556923 } else {
63566924 else_expr_result = ir_build_const_void(irb, scope, node);
6925 ir_build_end_expr(irb, scope, node, else_expr_result, &peer_parent->peers.at(1)->base);
63576926 }
63586927 IrBasicBlock *after_else_block = irb->current_basic_block;
63596928 if (!instr_is_unreachable(else_expr_result))
......@@ -6367,10 +6936,13 @@ static IrInstruction *ir_gen_if_optional_expr(IrBuilder *irb, Scope *scope, AstN
63676936 incoming_blocks[0] = after_then_block;
63686937 incoming_blocks[1] = after_else_block;
63696938
6370 return ir_build_phi(irb, scope, node, 2, incoming_blocks, incoming_values);
6939 IrInstruction *phi = ir_build_phi(irb, scope, node, 2, incoming_blocks, incoming_values, peer_parent);
6940 return ir_expr_wrap(irb, scope, phi, result_loc);
63716941}
63726942
6373static IrInstruction *ir_gen_if_err_expr(IrBuilder *irb, Scope *scope, AstNode *node) {
6943static IrInstruction *ir_gen_if_err_expr(IrBuilder *irb, Scope *scope, AstNode *node, LVal lval,
6944 ResultLoc *result_loc)
6945{
63746946 assert(node->type == NodeTypeIfErrorExpr);
63756947
63766948 AstNode *target_node = node->data.if_err_expr.target_node;
......@@ -6381,12 +6953,12 @@ static IrInstruction *ir_gen_if_err_expr(IrBuilder *irb, Scope *scope, AstNode *
63816953 Buf *var_symbol = node->data.if_err_expr.var_symbol;
63826954 Buf *err_symbol = node->data.if_err_expr.err_symbol;
63836955
6384 IrInstruction *err_val_ptr = ir_gen_node_extra(irb, target_node, scope, LValPtr);
6956 IrInstruction *err_val_ptr = ir_gen_node_extra(irb, target_node, scope, LValPtr, nullptr);
63856957 if (err_val_ptr == irb->codegen->invalid_instruction)
63866958 return err_val_ptr;
63876959
63886960 IrInstruction *err_val = ir_build_load_ptr(irb, scope, node, err_val_ptr);
6389 IrInstruction *is_err = ir_build_test_err(irb, scope, node, err_val);
6961 IrInstruction *is_err = ir_build_test_err_src(irb, scope, node, err_val_ptr, true);
63906962
63916963 IrBasicBlock *ok_block = ir_create_basic_block(irb, scope, "TryOk");
63926964 IrBasicBlock *else_block = ir_create_basic_block(irb, scope, "TryElse");
......@@ -6394,27 +6966,31 @@ static IrInstruction *ir_gen_if_err_expr(IrBuilder *irb, Scope *scope, AstNode *
63946966
63956967 bool force_comptime = ir_should_inline(irb->exec, scope);
63966968 IrInstruction *is_comptime = force_comptime ? ir_build_const_bool(irb, scope, node, true) : ir_build_test_comptime(irb, scope, node, is_err);
6397 ir_build_cond_br(irb, scope, node, is_err, else_block, ok_block, is_comptime);
6969 IrInstruction *cond_br_inst = ir_build_cond_br(irb, scope, node, is_err, else_block, ok_block, is_comptime);
6970
6971 ResultLocPeerParent *peer_parent = ir_build_binary_result_peers(irb, cond_br_inst, else_block, endif_block,
6972 result_loc, is_comptime);
63986973
63996974 ir_set_cursor_at_end_and_append_block(irb, ok_block);
64006975
64016976 Scope *subexpr_scope = create_runtime_scope(irb->codegen, node, scope, is_comptime);
64026977 Scope *var_scope;
64036978 if (var_symbol) {
6404 IrInstruction *var_type = nullptr;
64056979 bool is_shadowable = false;
64066980 IrInstruction *var_is_comptime = force_comptime ? ir_build_const_bool(irb, subexpr_scope, node, true) : ir_build_test_comptime(irb, subexpr_scope, node, err_val);
64076981 ZigVar *var = ir_create_var(irb, node, subexpr_scope,
64086982 var_symbol, var_is_const, var_is_const, is_shadowable, var_is_comptime);
64096983
6410 IrInstruction *var_ptr_value = ir_build_unwrap_err_payload(irb, subexpr_scope, node, err_val_ptr, false);
6411 IrInstruction *var_value = var_is_ptr ? var_ptr_value : ir_build_load_ptr(irb, subexpr_scope, node, var_ptr_value);
6412 ir_build_var_decl_src(irb, subexpr_scope, node, var, var_type, nullptr, var_value);
6984 IrInstruction *payload_ptr = ir_build_unwrap_err_payload(irb, subexpr_scope, node, err_val_ptr, false, false);
6985 IrInstruction *var_ptr = var_is_ptr ?
6986 ir_build_ref(irb, subexpr_scope, node, payload_ptr, true, false) : payload_ptr;
6987 ir_build_var_decl_src(irb, subexpr_scope, node, var, nullptr, var_ptr);
64136988 var_scope = var->child_scope;
64146989 } else {
64156990 var_scope = subexpr_scope;
64166991 }
6417 IrInstruction *then_expr_result = ir_gen_node(irb, then_node, var_scope);
6992 IrInstruction *then_expr_result = ir_gen_node_extra(irb, then_node, var_scope, lval,
6993 &peer_parent->peers.at(0)->base);
64186994 if (then_expr_result == irb->codegen->invalid_instruction)
64196995 return then_expr_result;
64206996 IrBasicBlock *after_then_block = irb->current_basic_block;
......@@ -6427,23 +7003,23 @@ static IrInstruction *ir_gen_if_err_expr(IrBuilder *irb, Scope *scope, AstNode *
64277003 if (else_node) {
64287004 Scope *err_var_scope;
64297005 if (err_symbol) {
6430 IrInstruction *var_type = nullptr;
64317006 bool is_shadowable = false;
64327007 bool is_const = true;
64337008 ZigVar *var = ir_create_var(irb, node, subexpr_scope,
64347009 err_symbol, is_const, is_const, is_shadowable, is_comptime);
64357010
6436 IrInstruction *var_value = ir_build_unwrap_err_code(irb, subexpr_scope, node, err_val_ptr);
6437 ir_build_var_decl_src(irb, subexpr_scope, node, var, var_type, nullptr, var_value);
7011 IrInstruction *err_ptr = ir_build_unwrap_err_code(irb, subexpr_scope, node, err_val_ptr);
7012 ir_build_var_decl_src(irb, subexpr_scope, node, var, nullptr, err_ptr);
64387013 err_var_scope = var->child_scope;
64397014 } else {
64407015 err_var_scope = subexpr_scope;
64417016 }
6442 else_expr_result = ir_gen_node(irb, else_node, err_var_scope);
7017 else_expr_result = ir_gen_node_extra(irb, else_node, err_var_scope, lval, &peer_parent->peers.at(1)->base);
64437018 if (else_expr_result == irb->codegen->invalid_instruction)
64447019 return else_expr_result;
64457020 } else {
64467021 else_expr_result = ir_build_const_void(irb, scope, node);
7022 ir_build_end_expr(irb, scope, node, else_expr_result, &peer_parent->peers.at(1)->base);
64477023 }
64487024 IrBasicBlock *after_else_block = irb->current_basic_block;
64497025 if (!instr_is_unreachable(else_expr_result))
......@@ -6457,14 +7033,15 @@ static IrInstruction *ir_gen_if_err_expr(IrBuilder *irb, Scope *scope, AstNode *
64577033 incoming_blocks[0] = after_then_block;
64587034 incoming_blocks[1] = after_else_block;
64597035
6460 return ir_build_phi(irb, scope, node, 2, incoming_blocks, incoming_values);
7036 IrInstruction *phi = ir_build_phi(irb, scope, node, 2, incoming_blocks, incoming_values, peer_parent);
7037 return ir_expr_wrap(irb, scope, phi, result_loc);
64617038}
64627039
64637040static bool ir_gen_switch_prong_expr(IrBuilder *irb, Scope *scope, AstNode *switch_node, AstNode *prong_node,
64647041 IrBasicBlock *end_block, IrInstruction *is_comptime, IrInstruction *var_is_comptime,
64657042 IrInstruction *target_value_ptr, IrInstruction **prong_values, size_t prong_values_len,
64667043 ZigList<IrBasicBlock *> *incoming_blocks, ZigList<IrInstruction *> *incoming_values,
6467 IrInstructionSwitchElseVar **out_switch_else_var)
7044 IrInstructionSwitchElseVar **out_switch_else_var, LVal lval, ResultLoc *result_loc)
64687045{
64697046 assert(switch_node->type == NodeTypeSwitchExpr);
64707047 assert(prong_node->type == NodeTypeSwitchProng);
......@@ -6482,28 +7059,27 @@ static bool ir_gen_switch_prong_expr(IrBuilder *irb, Scope *scope, AstNode *swit
64827059 ZigVar *var = ir_create_var(irb, var_symbol_node, scope,
64837060 var_name, is_const, is_const, is_shadowable, var_is_comptime);
64847061 child_scope = var->child_scope;
6485 IrInstruction *var_value;
7062 IrInstruction *var_ptr;
64867063 if (out_switch_else_var != nullptr) {
64877064 IrInstructionSwitchElseVar *switch_else_var = ir_build_switch_else_var(irb, scope, var_symbol_node,
64887065 target_value_ptr);
64897066 *out_switch_else_var = switch_else_var;
6490 IrInstruction *var_ptr_value = &switch_else_var->base;
6491 var_value = var_is_ptr ? var_ptr_value : ir_build_load_ptr(irb, scope, var_symbol_node, var_ptr_value);
7067 IrInstruction *payload_ptr = &switch_else_var->base;
7068 var_ptr = var_is_ptr ? ir_build_ref(irb, scope, var_symbol_node, payload_ptr, true, false) : payload_ptr;
64927069 } else if (prong_values != nullptr) {
6493 IrInstruction *var_ptr_value = ir_build_switch_var(irb, scope, var_symbol_node, target_value_ptr,
7070 IrInstruction *payload_ptr = ir_build_switch_var(irb, scope, var_symbol_node, target_value_ptr,
64947071 prong_values, prong_values_len);
6495 var_value = var_is_ptr ? var_ptr_value : ir_build_load_ptr(irb, scope, var_symbol_node, var_ptr_value);
7072 var_ptr = var_is_ptr ? ir_build_ref(irb, scope, var_symbol_node, payload_ptr, true, false) : payload_ptr;
64967073 } else {
6497 var_value = var_is_ptr ? target_value_ptr : ir_build_load_ptr(irb, scope, var_symbol_node,
6498target_value_ptr);
7074 var_ptr = var_is_ptr ?
7075 ir_build_ref(irb, scope, var_symbol_node, target_value_ptr, true, false) : target_value_ptr;
64997076 }
6500 IrInstruction *var_type = nullptr; // infer the type
6501 ir_build_var_decl_src(irb, scope, var_symbol_node, var, var_type, nullptr, var_value);
7077 ir_build_var_decl_src(irb, scope, var_symbol_node, var, nullptr, var_ptr);
65027078 } else {
65037079 child_scope = scope;
65047080 }
65057081
6506 IrInstruction *expr_result = ir_gen_node(irb, expr_node, child_scope);
7082 IrInstruction *expr_result = ir_gen_node_extra(irb, expr_node, child_scope, lval, result_loc);
65077083 if (expr_result == irb->codegen->invalid_instruction)
65087084 return false;
65097085 if (!instr_is_unreachable(expr_result))
......@@ -6513,11 +7089,13 @@ target_value_ptr);
65137089 return true;
65147090}
65157091
6516static IrInstruction *ir_gen_switch_expr(IrBuilder *irb, Scope *scope, AstNode *node) {
7092static IrInstruction *ir_gen_switch_expr(IrBuilder *irb, Scope *scope, AstNode *node, LVal lval,
7093 ResultLoc *result_loc)
7094{
65177095 assert(node->type == NodeTypeSwitchExpr);
65187096
65197097 AstNode *target_node = node->data.switch_expr.expr;
6520 IrInstruction *target_value_ptr = ir_gen_node_extra(irb, target_node, scope, LValPtr);
7098 IrInstruction *target_value_ptr = ir_gen_node_extra(irb, target_node, scope, LValPtr, nullptr);
65217099 if (target_value_ptr == irb->codegen->invalid_instruction)
65227100 return target_value_ptr;
65237101 IrInstruction *target_value = ir_build_switch_target(irb, scope, node, target_value_ptr);
......@@ -6544,6 +7122,14 @@ static IrInstruction *ir_gen_switch_expr(IrBuilder *irb, Scope *scope, AstNode *
65447122
65457123 IrInstructionSwitchElseVar *switch_else_var = nullptr;
65467124
7125 ResultLocPeerParent *peer_parent = allocate<ResultLocPeerParent>(1);
7126 peer_parent->base.id = ResultLocIdPeerParent;
7127 peer_parent->end_bb = end_block;
7128 peer_parent->is_comptime = is_comptime;
7129 peer_parent->parent = result_loc;
7130
7131 ir_build_reset_result(irb, scope, node, &peer_parent->base);
7132
65477133 // First do the else and the ranges
65487134 Scope *subexpr_scope = create_runtime_scope(irb->codegen, node, scope, is_comptime);
65497135 Scope *comptime_scope = create_comptime_scope(irb->codegen, node, scope);
......@@ -6552,6 +7138,7 @@ static IrInstruction *ir_gen_switch_expr(IrBuilder *irb, Scope *scope, AstNode *
65527138 AstNode *prong_node = node->data.switch_expr.prongs.at(prong_i);
65537139 size_t prong_item_count = prong_node->data.switch_prong.items.length;
65547140 if (prong_item_count == 0) {
7141 ResultLocPeer *this_peer_result_loc = create_peer_result(peer_parent);
65557142 if (else_prong) {
65567143 ErrorMsg *msg = add_node_error(irb->codegen, prong_node,
65577144 buf_sprintf("multiple else prongs in switch expression"));
......@@ -6562,15 +7149,21 @@ static IrInstruction *ir_gen_switch_expr(IrBuilder *irb, Scope *scope, AstNode *
65627149 else_prong = prong_node;
65637150
65647151 IrBasicBlock *prev_block = irb->current_basic_block;
7152 if (peer_parent->peers.length > 0) {
7153 peer_parent->peers.last()->next_bb = else_block;
7154 }
7155 peer_parent->peers.append(this_peer_result_loc);
65657156 ir_set_cursor_at_end_and_append_block(irb, else_block);
65667157 if (!ir_gen_switch_prong_expr(irb, subexpr_scope, node, prong_node, end_block,
65677158 is_comptime, var_is_comptime, target_value_ptr, nullptr, 0, &incoming_blocks, &incoming_values,
6568 &switch_else_var))
7159 &switch_else_var, lval, &this_peer_result_loc->base))
65697160 {
65707161 return irb->codegen->invalid_instruction;
65717162 }
65727163 ir_set_cursor_at_end(irb, prev_block);
65737164 } else if (prong_node->data.switch_prong.any_items_are_range) {
7165 ResultLocPeer *this_peer_result_loc = create_peer_result(peer_parent);
7166
65747167 IrInstruction *ok_bit = nullptr;
65757168 AstNode *last_item_node = nullptr;
65767169 for (size_t item_i = 0; item_i < prong_item_count; item_i += 1) {
......@@ -6627,13 +7220,20 @@ static IrInstruction *ir_gen_switch_expr(IrBuilder *irb, Scope *scope, AstNode *
66277220
66287221 assert(ok_bit);
66297222 assert(last_item_node);
6630 ir_mark_gen(ir_build_cond_br(irb, scope, last_item_node, ok_bit, range_block_yes,
6631 range_block_no, is_comptime));
7223 IrInstruction *br_inst = ir_mark_gen(ir_build_cond_br(irb, scope, last_item_node, ok_bit,
7224 range_block_yes, range_block_no, is_comptime));
7225 if (peer_parent->base.source_instruction == nullptr) {
7226 peer_parent->base.source_instruction = br_inst;
7227 }
66327228
7229 if (peer_parent->peers.length > 0) {
7230 peer_parent->peers.last()->next_bb = range_block_yes;
7231 }
7232 peer_parent->peers.append(this_peer_result_loc);
66337233 ir_set_cursor_at_end_and_append_block(irb, range_block_yes);
66347234 if (!ir_gen_switch_prong_expr(irb, subexpr_scope, node, prong_node, end_block,
66357235 is_comptime, var_is_comptime, target_value_ptr, nullptr, 0,
6636 &incoming_blocks, &incoming_values, nullptr))
7236 &incoming_blocks, &incoming_values, nullptr, lval, &this_peer_result_loc->base))
66377237 {
66387238 return irb->codegen->invalid_instruction;
66397239 }
......@@ -6651,6 +7251,8 @@ static IrInstruction *ir_gen_switch_expr(IrBuilder *irb, Scope *scope, AstNode *
66517251 if (prong_node->data.switch_prong.any_items_are_range)
66527252 continue;
66537253
7254 ResultLocPeer *this_peer_result_loc = create_peer_result(peer_parent);
7255
66547256 IrBasicBlock *prong_block = ir_create_basic_block(irb, scope, "SwitchProng");
66557257 IrInstruction **items = allocate<IrInstruction *>(prong_item_count);
66567258
......@@ -6674,10 +7276,14 @@ static IrInstruction *ir_gen_switch_expr(IrBuilder *irb, Scope *scope, AstNode *
66747276 }
66757277
66767278 IrBasicBlock *prev_block = irb->current_basic_block;
7279 if (peer_parent->peers.length > 0) {
7280 peer_parent->peers.last()->next_bb = prong_block;
7281 }
7282 peer_parent->peers.append(this_peer_result_loc);
66777283 ir_set_cursor_at_end_and_append_block(irb, prong_block);
66787284 if (!ir_gen_switch_prong_expr(irb, subexpr_scope, node, prong_node, end_block,
66797285 is_comptime, var_is_comptime, target_value_ptr, items, prong_item_count,
6680 &incoming_blocks, &incoming_values, nullptr))
7286 &incoming_blocks, &incoming_values, nullptr, lval, &this_peer_result_loc->base))
66817287 {
66827288 return irb->codegen->invalid_instruction;
66837289 }
......@@ -6686,38 +7292,57 @@ static IrInstruction *ir_gen_switch_expr(IrBuilder *irb, Scope *scope, AstNode *
66867292
66877293 }
66887294
6689 IrInstruction *switch_prongs_void = ir_build_check_switch_prongs(irb, scope, node, target_value, check_ranges.items, check_ranges.length,
6690 else_prong != nullptr);
7295 IrInstruction *switch_prongs_void = ir_build_check_switch_prongs(irb, scope, node, target_value,
7296 check_ranges.items, check_ranges.length, else_prong != nullptr);
66917297
7298 IrInstruction *br_instruction;
66927299 if (cases.length == 0) {
6693 ir_build_br(irb, scope, node, else_block, is_comptime);
7300 br_instruction = ir_build_br(irb, scope, node, else_block, is_comptime);
66947301 } else {
66957302 IrInstructionSwitchBr *switch_br = ir_build_switch_br(irb, scope, node, target_value, else_block,
66967303 cases.length, cases.items, is_comptime, switch_prongs_void);
66977304 if (switch_else_var != nullptr) {
66987305 switch_else_var->switch_br = switch_br;
66997306 }
7307 br_instruction = &switch_br->base;
7308 }
7309 if (peer_parent->base.source_instruction == nullptr) {
7310 peer_parent->base.source_instruction = br_instruction;
7311 }
7312 for (size_t i = 0; i < peer_parent->peers.length; i += 1) {
7313 peer_parent->peers.at(i)->base.source_instruction = peer_parent->base.source_instruction;
67007314 }
67017315
67027316 if (!else_prong) {
7317 if (peer_parent->peers.length != 0) {
7318 peer_parent->peers.last()->next_bb = else_block;
7319 }
67037320 ir_set_cursor_at_end_and_append_block(irb, else_block);
67047321 ir_build_unreachable(irb, scope, node);
7322 } else {
7323 if (peer_parent->peers.length != 0) {
7324 peer_parent->peers.last()->next_bb = end_block;
7325 }
67057326 }
67067327
67077328 ir_set_cursor_at_end_and_append_block(irb, end_block);
67087329 assert(incoming_blocks.length == incoming_values.length);
7330 IrInstruction *result_instruction;
67097331 if (incoming_blocks.length == 0) {
6710 return ir_build_const_void(irb, scope, node);
7332 result_instruction = ir_build_const_void(irb, scope, node);
67117333 } else {
6712 return ir_build_phi(irb, scope, node, incoming_blocks.length, incoming_blocks.items, incoming_values.items);
7334 result_instruction = ir_build_phi(irb, scope, node, incoming_blocks.length,
7335 incoming_blocks.items, incoming_values.items, peer_parent);
67137336 }
7337 return ir_expr_wrap(irb, scope, result_instruction, result_loc);
67147338}
67157339
67167340static IrInstruction *ir_gen_comptime(IrBuilder *irb, Scope *parent_scope, AstNode *node, LVal lval) {
67177341 assert(node->type == NodeTypeCompTime);
67187342
67197343 Scope *child_scope = create_comptime_scope(irb->codegen, node, parent_scope);
6720 return ir_gen_node_extra(irb, node->data.comptime_expr.expr, child_scope, lval);
7344 // purposefully pass null for result_loc and let EndExpr handle it
7345 return ir_gen_node_extra(irb, node->data.comptime_expr.expr, child_scope, lval, nullptr);
67217346}
67227347
67237348static IrInstruction *ir_gen_return_from_block(IrBuilder *irb, Scope *break_scope, AstNode *node, ScopeBlock *block_scope) {
......@@ -6730,7 +7355,11 @@ static IrInstruction *ir_gen_return_from_block(IrBuilder *irb, Scope *break_scop
67307355
67317356 IrInstruction *result_value;
67327357 if (node->data.break_expr.expr) {
6733 result_value = ir_gen_node(irb, node->data.break_expr.expr, break_scope);
7358 ResultLocPeer *peer_result = create_peer_result(block_scope->peer_parent);
7359 block_scope->peer_parent->peers.append(peer_result);
7360
7361 result_value = ir_gen_node_extra(irb, node->data.break_expr.expr, break_scope, block_scope->lval,
7362 &peer_result->base);
67347363 if (result_value == irb->codegen->invalid_instruction)
67357364 return irb->codegen->invalid_instruction;
67367365 } else {
......@@ -6800,7 +7429,11 @@ static IrInstruction *ir_gen_break(IrBuilder *irb, Scope *break_scope, AstNode *
68007429
68017430 IrInstruction *result_value;
68027431 if (node->data.break_expr.expr) {
6803 result_value = ir_gen_node(irb, node->data.break_expr.expr, break_scope);
7432 ResultLocPeer *peer_result = create_peer_result(loop_scope->peer_parent);
7433 loop_scope->peer_parent->peers.append(peer_result);
7434
7435 result_value = ir_gen_node_extra(irb, node->data.break_expr.expr, break_scope,
7436 loop_scope->lval, &peer_result->base);
68047437 if (result_value == irb->codegen->invalid_instruction)
68057438 return irb->codegen->invalid_instruction;
68067439 } else {
......@@ -6888,7 +7521,7 @@ static IrInstruction *ir_gen_defer(IrBuilder *irb, Scope *parent_scope, AstNode
68887521 return ir_build_const_void(irb, parent_scope, node);
68897522}
68907523
6891static IrInstruction *ir_gen_slice(IrBuilder *irb, Scope *scope, AstNode *node) {
7524static IrInstruction *ir_gen_slice(IrBuilder *irb, Scope *scope, AstNode *node, LVal lval, ResultLoc *result_loc) {
68927525 assert(node->type == NodeTypeSliceExpr);
68937526
68947527 AstNodeSliceExpr *slice_expr = &node->data.slice_expr;
......@@ -6896,7 +7529,7 @@ static IrInstruction *ir_gen_slice(IrBuilder *irb, Scope *scope, AstNode *node)
68967529 AstNode *start_node = slice_expr->start;
68977530 AstNode *end_node = slice_expr->end;
68987531
6899 IrInstruction *ptr_value = ir_gen_node_extra(irb, array_node, scope, LValPtr);
7532 IrInstruction *ptr_value = ir_gen_node_extra(irb, array_node, scope, LValPtr, nullptr);
69007533 if (ptr_value == irb->codegen->invalid_instruction)
69017534 return irb->codegen->invalid_instruction;
69027535
......@@ -6913,11 +7546,14 @@ static IrInstruction *ir_gen_slice(IrBuilder *irb, Scope *scope, AstNode *node)
69137546 end_value = nullptr;
69147547 }
69157548
6916 return ir_build_slice(irb, scope, node, ptr_value, start_value, end_value, true);
7549 IrInstruction *slice = ir_build_slice_src(irb, scope, node, ptr_value, start_value, end_value, true, result_loc);
7550 return ir_lval_wrap(irb, scope, slice, lval, result_loc);
69177551}
69187552
6919static IrInstruction *ir_gen_catch(IrBuilder *irb, Scope *parent_scope, AstNode *node) {
6920 assert(node->type == NodeTypeUnwrapErrorExpr);
7553static IrInstruction *ir_gen_catch(IrBuilder *irb, Scope *parent_scope, AstNode *node, LVal lval,
7554 ResultLoc *result_loc)
7555{
7556 assert(node->type == NodeTypeCatchExpr);
69217557
69227558 AstNode *op1_node = node->data.unwrap_err_expr.op1;
69237559 AstNode *op2_node = node->data.unwrap_err_expr.op2;
......@@ -6930,16 +7566,15 @@ static IrInstruction *ir_gen_catch(IrBuilder *irb, Scope *parent_scope, AstNode
69307566 add_node_error(irb->codegen, var_node, buf_sprintf("unused variable: '%s'", buf_ptr(var_name)));
69317567 return irb->codegen->invalid_instruction;
69327568 }
6933 return ir_gen_catch_unreachable(irb, parent_scope, node, op1_node, LValNone);
7569 return ir_gen_catch_unreachable(irb, parent_scope, node, op1_node, lval, result_loc);
69347570 }
69357571
69367572
6937 IrInstruction *err_union_ptr = ir_gen_node_extra(irb, op1_node, parent_scope, LValPtr);
7573 IrInstruction *err_union_ptr = ir_gen_node_extra(irb, op1_node, parent_scope, LValPtr, nullptr);
69387574 if (err_union_ptr == irb->codegen->invalid_instruction)
69397575 return irb->codegen->invalid_instruction;
69407576
6941 IrInstruction *err_union_val = ir_build_load_ptr(irb, parent_scope, node, err_union_ptr);
6942 IrInstruction *is_err = ir_build_test_err(irb, parent_scope, node, err_union_val);
7577 IrInstruction *is_err = ir_build_test_err_src(irb, parent_scope, node, err_union_ptr, true);
69437578
69447579 IrInstruction *is_comptime;
69457580 if (ir_should_inline(irb->exec, parent_scope)) {
......@@ -6951,7 +7586,10 @@ static IrInstruction *ir_gen_catch(IrBuilder *irb, Scope *parent_scope, AstNode
69517586 IrBasicBlock *ok_block = ir_create_basic_block(irb, parent_scope, "UnwrapErrOk");
69527587 IrBasicBlock *err_block = ir_create_basic_block(irb, parent_scope, "UnwrapErrError");
69537588 IrBasicBlock *end_block = ir_create_basic_block(irb, parent_scope, "UnwrapErrEnd");
6954 ir_build_cond_br(irb, parent_scope, node, is_err, err_block, ok_block, is_comptime);
7589 IrInstruction *cond_br_inst = ir_build_cond_br(irb, parent_scope, node, is_err, err_block, ok_block, is_comptime);
7590
7591 ResultLocPeerParent *peer_parent = ir_build_binary_result_peers(irb, cond_br_inst, ok_block, end_block, result_loc,
7592 is_comptime);
69557593
69567594 ir_set_cursor_at_end_and_append_block(irb, err_block);
69577595 Scope *err_scope;
......@@ -6963,12 +7601,12 @@ static IrInstruction *ir_gen_catch(IrBuilder *irb, Scope *parent_scope, AstNode
69637601 ZigVar *var = ir_create_var(irb, node, parent_scope, var_name,
69647602 is_const, is_const, is_shadowable, is_comptime);
69657603 err_scope = var->child_scope;
6966 IrInstruction *err_val = ir_build_unwrap_err_code(irb, err_scope, node, err_union_ptr);
6967 ir_build_var_decl_src(irb, err_scope, var_node, var, nullptr, nullptr, err_val);
7604 IrInstruction *err_ptr = ir_build_unwrap_err_code(irb, err_scope, node, err_union_ptr);
7605 ir_build_var_decl_src(irb, err_scope, var_node, var, nullptr, err_ptr);
69687606 } else {
69697607 err_scope = parent_scope;
69707608 }
6971 IrInstruction *err_result = ir_gen_node(irb, op2_node, err_scope);
7609 IrInstruction *err_result = ir_gen_node_extra(irb, op2_node, err_scope, LValNone, &peer_parent->peers.at(0)->base);
69727610 if (err_result == irb->codegen->invalid_instruction)
69737611 return irb->codegen->invalid_instruction;
69747612 IrBasicBlock *after_err_block = irb->current_basic_block;
......@@ -6976,8 +7614,9 @@ static IrInstruction *ir_gen_catch(IrBuilder *irb, Scope *parent_scope, AstNode
69767614 ir_mark_gen(ir_build_br(irb, err_scope, node, end_block, is_comptime));
69777615
69787616 ir_set_cursor_at_end_and_append_block(irb, ok_block);
6979 IrInstruction *unwrapped_ptr = ir_build_unwrap_err_payload(irb, parent_scope, node, err_union_ptr, false);
7617 IrInstruction *unwrapped_ptr = ir_build_unwrap_err_payload(irb, parent_scope, node, err_union_ptr, false, false);
69807618 IrInstruction *unwrapped_payload = ir_build_load_ptr(irb, parent_scope, node, unwrapped_ptr);
7619 ir_build_end_expr(irb, parent_scope, node, unwrapped_payload, &peer_parent->peers.at(1)->base);
69817620 IrBasicBlock *after_ok_block = irb->current_basic_block;
69827621 ir_build_br(irb, parent_scope, node, end_block, is_comptime);
69837622
......@@ -6988,7 +7627,8 @@ static IrInstruction *ir_gen_catch(IrBuilder *irb, Scope *parent_scope, AstNode
69887627 IrBasicBlock **incoming_blocks = allocate<IrBasicBlock *>(2);
69897628 incoming_blocks[0] = after_err_block;
69907629 incoming_blocks[1] = after_ok_block;
6991 return ir_build_phi(irb, parent_scope, node, 2, incoming_blocks, incoming_values);
7630 IrInstruction *phi = ir_build_phi(irb, parent_scope, node, 2, incoming_blocks, incoming_values, peer_parent);
7631 return ir_lval_wrap(irb, parent_scope, phi, lval, result_loc);
69927632}
69937633
69947634static bool render_instance_name_recursive(CodeGen *codegen, Buf *name, Scope *outer_scope, Scope *inner_scope) {
......@@ -7264,7 +7904,7 @@ static IrInstruction *ir_gen_cancel_target(IrBuilder *irb, Scope *scope, AstNode
72647904 IrInstruction *coro_promise_ptr = ir_build_coro_promise(irb, scope, node, casted_target_inst);
72657905 Buf *atomic_state_field_name = buf_create_from_str(ATOMIC_STATE_FIELD_NAME);
72667906 IrInstruction *atomic_state_ptr = ir_build_field_ptr(irb, scope, node, coro_promise_ptr,
7267 atomic_state_field_name);
7907 atomic_state_field_name, false);
72687908
72697909 // set the is_canceled bit
72707910 IrInstruction *prev_atomic_value = ir_build_atomic_rmw(irb, scope, node,
......@@ -7343,7 +7983,7 @@ static IrInstruction *ir_gen_resume_target(IrBuilder *irb, Scope *scope, AstNode
73437983 IrInstruction *coro_promise_ptr = ir_build_coro_promise(irb, scope, node, casted_target_inst);
73447984 Buf *atomic_state_field_name = buf_create_from_str(ATOMIC_STATE_FIELD_NAME);
73457985 IrInstruction *atomic_state_ptr = ir_build_field_ptr(irb, scope, node, coro_promise_ptr,
7346 atomic_state_field_name);
7986 atomic_state_field_name, false);
73477987
73487988 // clear the is_suspended bit
73497989 IrInstruction *prev_atomic_value = ir_build_atomic_rmw(irb, scope, node,
......@@ -7410,12 +8050,12 @@ static IrInstruction *ir_gen_await_expr(IrBuilder *irb, Scope *scope, AstNode *n
74108050
74118051 IrInstruction *coro_promise_ptr = ir_build_coro_promise(irb, scope, node, target_inst);
74128052 Buf *result_ptr_field_name = buf_create_from_str(RESULT_PTR_FIELD_NAME);
7413 IrInstruction *result_ptr_field_ptr = ir_build_field_ptr(irb, scope, node, coro_promise_ptr, result_ptr_field_name);
8053 IrInstruction *result_ptr_field_ptr = ir_build_field_ptr(irb, scope, node, coro_promise_ptr, result_ptr_field_name, false);
74148054
74158055 if (irb->codegen->have_err_ret_tracing) {
74168056 IrInstruction *err_ret_trace_ptr = ir_build_error_return_trace(irb, scope, node, IrInstructionErrorReturnTrace::NonNull);
74178057 Buf *err_ret_trace_ptr_field_name = buf_create_from_str(ERR_RET_TRACE_PTR_FIELD_NAME);
7418 IrInstruction *err_ret_trace_ptr_field_ptr = ir_build_field_ptr(irb, scope, node, coro_promise_ptr, err_ret_trace_ptr_field_name);
8058 IrInstruction *err_ret_trace_ptr_field_ptr = ir_build_field_ptr(irb, scope, node, coro_promise_ptr, err_ret_trace_ptr_field_name, false);
74198059 ir_build_store_ptr(irb, scope, node, err_ret_trace_ptr_field_ptr, err_ret_trace_ptr);
74208060 }
74218061
......@@ -7437,11 +8077,11 @@ static IrInstruction *ir_gen_await_expr(IrBuilder *irb, Scope *scope, AstNode *n
74378077
74388078 Buf *atomic_state_field_name = buf_create_from_str(ATOMIC_STATE_FIELD_NAME);
74398079 IrInstruction *atomic_state_ptr = ir_build_field_ptr(irb, scope, node, coro_promise_ptr,
7440 atomic_state_field_name);
8080 atomic_state_field_name, false);
74418081
74428082 IrInstruction *promise_type_val = ir_build_const_type(irb, scope, node, irb->codegen->builtin_types.entry_promise);
74438083 IrInstruction *const_bool_false = ir_build_const_bool(irb, scope, node, false);
7444 IrInstruction *undefined_value = ir_build_const_undefined(irb, scope, node);
8084 IrInstruction *undef = ir_build_const_undefined(irb, scope, node);
74458085 IrInstruction *usize_type_val = ir_build_const_type(irb, scope, node, irb->codegen->builtin_types.entry_usize);
74468086 IrInstruction *zero = ir_build_const_usize(irb, scope, node, 0);
74478087 IrInstruction *inverted_ptr_mask = ir_build_const_usize(irb, scope, node, 0x7); // 0b111
......@@ -7455,7 +8095,8 @@ static IrInstruction *ir_gen_await_expr(IrBuilder *irb, Scope *scope, AstNode *n
74558095 IrInstruction *target_promise_type = ir_build_typeof(irb, scope, node, target_inst);
74568096 IrInstruction *promise_result_type = ir_build_promise_result_type(irb, scope, node, target_promise_type);
74578097 ir_build_await_bookkeeping(irb, scope, node, promise_result_type);
7458 ir_build_var_decl_src(irb, scope, node, result_var, promise_result_type, nullptr, undefined_value);
8098 IrInstruction *undef_promise_result = ir_build_implicit_cast(irb, scope, node, promise_result_type, undef, nullptr);
8099 build_decl_var_and_init(irb, scope, node, result_var, undef_promise_result, "result", const_bool_false);
74598100 IrInstruction *my_result_var_ptr = ir_build_var_ptr(irb, scope, node, result_var);
74608101 ir_build_store_ptr(irb, scope, node, result_ptr_field_ptr, my_result_var_ptr);
74618102 IrInstruction *save_token = ir_build_coro_save(irb, scope, node, irb->exec->coro_handle);
......@@ -7490,12 +8131,12 @@ static IrInstruction *ir_gen_await_expr(IrBuilder *irb, Scope *scope, AstNode *n
74908131 ir_set_cursor_at_end_and_append_block(irb, no_suspend_block);
74918132 if (irb->codegen->have_err_ret_tracing) {
74928133 Buf *err_ret_trace_field_name = buf_create_from_str(ERR_RET_TRACE_FIELD_NAME);
7493 IrInstruction *src_err_ret_trace_ptr = ir_build_field_ptr(irb, scope, node, coro_promise_ptr, err_ret_trace_field_name);
8134 IrInstruction *src_err_ret_trace_ptr = ir_build_field_ptr(irb, scope, node, coro_promise_ptr, err_ret_trace_field_name, false);
74948135 IrInstruction *dest_err_ret_trace_ptr = ir_build_error_return_trace(irb, scope, node, IrInstructionErrorReturnTrace::NonNull);
74958136 ir_build_merge_err_ret_traces(irb, scope, node, coro_promise_ptr, src_err_ret_trace_ptr, dest_err_ret_trace_ptr);
74968137 }
74978138 Buf *result_field_name = buf_create_from_str(RESULT_FIELD_NAME);
7498 IrInstruction *promise_result_ptr = ir_build_field_ptr(irb, scope, node, coro_promise_ptr, result_field_name);
8139 IrInstruction *promise_result_ptr = ir_build_field_ptr(irb, scope, node, coro_promise_ptr, result_field_name, false);
74998140 // If the type of the result handle_is_ptr then this does not actually perform a load. But we need it to,
75008141 // because we're about to destroy the memory. So we store it into our result variable.
75018142 IrInstruction *no_suspend_result = ir_build_load_ptr(irb, scope, node, promise_result_ptr);
......@@ -7671,7 +8312,8 @@ static IrInstruction *ir_gen_suspend(IrBuilder *irb, Scope *parent_scope, AstNod
76718312 incoming_values[0] = const_bool_true;
76728313 incoming_blocks[1] = post_cancel_awaiter_block;
76738314 incoming_values[1] = const_bool_false;
7674 IrInstruction *destroy_ourselves = ir_build_phi(irb, parent_scope, node, 2, incoming_blocks, incoming_values);
8315 IrInstruction *destroy_ourselves = ir_build_phi(irb, parent_scope, node, 2, incoming_blocks, incoming_values,
8316 nullptr);
76758317 ir_gen_defers_for_block(irb, parent_scope, outer_scope, true);
76768318 ir_mark_gen(ir_build_cond_br(irb, parent_scope, node, destroy_ourselves, irb->exec->coro_final_cleanup_block, irb->exec->coro_early_final, const_bool_false));
76778319
......@@ -7680,7 +8322,7 @@ static IrInstruction *ir_gen_suspend(IrBuilder *irb, Scope *parent_scope, AstNod
76808322}
76818323
76828324static IrInstruction *ir_gen_node_raw(IrBuilder *irb, AstNode *node, Scope *scope,
7683 LVal lval)
8325 LVal lval, ResultLoc *result_loc)
76848326{
76858327 assert(scope);
76868328 switch (node->type) {
......@@ -7694,37 +8336,37 @@ static IrInstruction *ir_gen_node_raw(IrBuilder *irb, AstNode *node, Scope *scop
76948336 case NodeTypeTestDecl:
76958337 zig_unreachable();
76968338 case NodeTypeBlock:
7697 return ir_lval_wrap(irb, scope, ir_gen_block(irb, scope, node), lval);
8339 return ir_gen_block(irb, scope, node, lval, result_loc);
76988340 case NodeTypeGroupedExpr:
7699 return ir_gen_node_raw(irb, node->data.grouped_expr, scope, lval);
8341 return ir_gen_node_raw(irb, node->data.grouped_expr, scope, lval, result_loc);
77008342 case NodeTypeBinOpExpr:
7701 return ir_lval_wrap(irb, scope, ir_gen_bin_op(irb, scope, node), lval);
8343 return ir_gen_bin_op(irb, scope, node, lval, result_loc);
77028344 case NodeTypeIntLiteral:
7703 return ir_lval_wrap(irb, scope, ir_gen_int_lit(irb, scope, node), lval);
8345 return ir_lval_wrap(irb, scope, ir_gen_int_lit(irb, scope, node), lval, result_loc);
77048346 case NodeTypeFloatLiteral:
7705 return ir_lval_wrap(irb, scope, ir_gen_float_lit(irb, scope, node), lval);
8347 return ir_lval_wrap(irb, scope, ir_gen_float_lit(irb, scope, node), lval, result_loc);
77068348 case NodeTypeCharLiteral:
7707 return ir_lval_wrap(irb, scope, ir_gen_char_lit(irb, scope, node), lval);
8349 return ir_lval_wrap(irb, scope, ir_gen_char_lit(irb, scope, node), lval, result_loc);
77088350 case NodeTypeSymbol:
7709 return ir_gen_symbol(irb, scope, node, lval);
8351 return ir_gen_symbol(irb, scope, node, lval, result_loc);
77108352 case NodeTypeFnCallExpr:
7711 return ir_gen_fn_call(irb, scope, node, lval);
8353 return ir_gen_fn_call(irb, scope, node, lval, result_loc);
77128354 case NodeTypeIfBoolExpr:
7713 return ir_lval_wrap(irb, scope, ir_gen_if_bool_expr(irb, scope, node), lval);
8355 return ir_gen_if_bool_expr(irb, scope, node, lval, result_loc);
77148356 case NodeTypePrefixOpExpr:
7715 return ir_gen_prefix_op_expr(irb, scope, node, lval);
8357 return ir_gen_prefix_op_expr(irb, scope, node, lval, result_loc);
77168358 case NodeTypeContainerInitExpr:
7717 return ir_lval_wrap(irb, scope, ir_gen_container_init_expr(irb, scope, node), lval);
8359 return ir_gen_container_init_expr(irb, scope, node, lval, result_loc);
77188360 case NodeTypeVariableDeclaration:
7719 return ir_lval_wrap(irb, scope, ir_gen_var_decl(irb, scope, node), lval);
8361 return ir_gen_var_decl(irb, scope, node);
77208362 case NodeTypeWhileExpr:
7721 return ir_lval_wrap(irb, scope, ir_gen_while_expr(irb, scope, node), lval);
8363 return ir_gen_while_expr(irb, scope, node, lval, result_loc);
77228364 case NodeTypeForExpr:
7723 return ir_lval_wrap(irb, scope, ir_gen_for_expr(irb, scope, node), lval);
8365 return ir_gen_for_expr(irb, scope, node, lval, result_loc);
77248366 case NodeTypeArrayAccessExpr:
7725 return ir_gen_array_access(irb, scope, node, lval);
8367 return ir_gen_array_access(irb, scope, node, lval, result_loc);
77268368 case NodeTypeReturnExpr:
7727 return ir_gen_return(irb, scope, node, lval);
8369 return ir_gen_return(irb, scope, node, lval, result_loc);
77288370 case NodeTypeFieldAccessExpr:
77298371 {
77308372 IrInstruction *ptr_instruction = ir_gen_field_access(irb, scope, node);
......@@ -7733,86 +8375,89 @@ static IrInstruction *ir_gen_node_raw(IrBuilder *irb, AstNode *node, Scope *scop
77338375 if (lval == LValPtr)
77348376 return ptr_instruction;
77358377
7736 return ir_build_load_ptr(irb, scope, node, ptr_instruction);
8378 IrInstruction *load_ptr = ir_build_load_ptr(irb, scope, node, ptr_instruction);
8379 return ir_expr_wrap(irb, scope, load_ptr, result_loc);
77378380 }
77388381 case NodeTypePtrDeref: {
77398382 AstNode *expr_node = node->data.ptr_deref_expr.target;
7740 IrInstruction *value = ir_gen_node_extra(irb, expr_node, scope, lval);
8383 IrInstruction *value = ir_gen_node_extra(irb, expr_node, scope, lval, nullptr);
77418384 if (value == irb->codegen->invalid_instruction)
77428385 return value;
77438386
77448387 // We essentially just converted any lvalue from &(x.*) to (&x).*;
77458388 // this inhibits checking that x is a pointer later, so we directly
77468389 // record whether the pointer check is needed
7747 return ir_build_un_op_lval(irb, scope, node, IrUnOpDereference, value, lval);
8390 IrInstruction *un_op = ir_build_un_op_lval(irb, scope, node, IrUnOpDereference, value, lval, result_loc);
8391 return ir_expr_wrap(irb, scope, un_op, result_loc);
77488392 }
77498393 case NodeTypeUnwrapOptional: {
77508394 AstNode *expr_node = node->data.unwrap_optional.expr;
77518395
7752 IrInstruction *maybe_ptr = ir_gen_node_extra(irb, expr_node, scope, LValPtr);
8396 IrInstruction *maybe_ptr = ir_gen_node_extra(irb, expr_node, scope, LValPtr, nullptr);
77538397 if (maybe_ptr == irb->codegen->invalid_instruction)
77548398 return irb->codegen->invalid_instruction;
77558399
7756 IrInstruction *unwrapped_ptr = ir_build_optional_unwrap_ptr(irb, scope, node, maybe_ptr, true);
8400 IrInstruction *unwrapped_ptr = ir_build_optional_unwrap_ptr(irb, scope, node, maybe_ptr, true, false);
77578401 if (lval == LValPtr)
77588402 return unwrapped_ptr;
77598403
7760 return ir_build_load_ptr(irb, scope, node, unwrapped_ptr);
8404 IrInstruction *load_ptr = ir_build_load_ptr(irb, scope, node, unwrapped_ptr);
8405 return ir_expr_wrap(irb, scope, load_ptr, result_loc);
77618406 }
77628407 case NodeTypeBoolLiteral:
7763 return ir_lval_wrap(irb, scope, ir_gen_bool_literal(irb, scope, node), lval);
8408 return ir_lval_wrap(irb, scope, ir_gen_bool_literal(irb, scope, node), lval, result_loc);
77648409 case NodeTypeArrayType:
7765 return ir_lval_wrap(irb, scope, ir_gen_array_type(irb, scope, node), lval);
8410 return ir_lval_wrap(irb, scope, ir_gen_array_type(irb, scope, node), lval, result_loc);
77668411 case NodeTypePointerType:
7767 return ir_lval_wrap(irb, scope, ir_gen_pointer_type(irb, scope, node), lval);
8412 return ir_lval_wrap(irb, scope, ir_gen_pointer_type(irb, scope, node), lval, result_loc);
77688413 case NodeTypePromiseType:
7769 return ir_lval_wrap(irb, scope, ir_gen_promise_type(irb, scope, node), lval);
8414 return ir_lval_wrap(irb, scope, ir_gen_promise_type(irb, scope, node), lval, result_loc);
77708415 case NodeTypeStringLiteral:
7771 return ir_lval_wrap(irb, scope, ir_gen_string_literal(irb, scope, node), lval);
8416 return ir_lval_wrap(irb, scope, ir_gen_string_literal(irb, scope, node), lval, result_loc);
77728417 case NodeTypeUndefinedLiteral:
7773 return ir_lval_wrap(irb, scope, ir_gen_undefined_literal(irb, scope, node), lval);
8418 return ir_lval_wrap(irb, scope, ir_gen_undefined_literal(irb, scope, node), lval, result_loc);
77748419 case NodeTypeAsmExpr:
7775 return ir_lval_wrap(irb, scope, ir_gen_asm_expr(irb, scope, node), lval);
8420 return ir_lval_wrap(irb, scope, ir_gen_asm_expr(irb, scope, node), lval, result_loc);
77768421 case NodeTypeNullLiteral:
7777 return ir_lval_wrap(irb, scope, ir_gen_null_literal(irb, scope, node), lval);
8422 return ir_lval_wrap(irb, scope, ir_gen_null_literal(irb, scope, node), lval, result_loc);
77788423 case NodeTypeIfErrorExpr:
7779 return ir_lval_wrap(irb, scope, ir_gen_if_err_expr(irb, scope, node), lval);
8424 return ir_gen_if_err_expr(irb, scope, node, lval, result_loc);
77808425 case NodeTypeIfOptional:
7781 return ir_lval_wrap(irb, scope, ir_gen_if_optional_expr(irb, scope, node), lval);
8426 return ir_gen_if_optional_expr(irb, scope, node, lval, result_loc);
77828427 case NodeTypeSwitchExpr:
7783 return ir_lval_wrap(irb, scope, ir_gen_switch_expr(irb, scope, node), lval);
8428 return ir_gen_switch_expr(irb, scope, node, lval, result_loc);
77848429 case NodeTypeCompTime:
7785 return ir_gen_comptime(irb, scope, node, lval);
8430 return ir_expr_wrap(irb, scope, ir_gen_comptime(irb, scope, node, lval), result_loc);
77868431 case NodeTypeErrorType:
7787 return ir_lval_wrap(irb, scope, ir_gen_error_type(irb, scope, node), lval);
8432 return ir_lval_wrap(irb, scope, ir_gen_error_type(irb, scope, node), lval, result_loc);
77888433 case NodeTypeBreak:
7789 return ir_lval_wrap(irb, scope, ir_gen_break(irb, scope, node), lval);
8434 return ir_lval_wrap(irb, scope, ir_gen_break(irb, scope, node), lval, result_loc);
77908435 case NodeTypeContinue:
7791 return ir_lval_wrap(irb, scope, ir_gen_continue(irb, scope, node), lval);
8436 return ir_lval_wrap(irb, scope, ir_gen_continue(irb, scope, node), lval, result_loc);
77928437 case NodeTypeUnreachable:
7793 return ir_lval_wrap(irb, scope, ir_build_unreachable(irb, scope, node), lval);
8438 return ir_build_unreachable(irb, scope, node);
77948439 case NodeTypeDefer:
7795 return ir_lval_wrap(irb, scope, ir_gen_defer(irb, scope, node), lval);
8440 return ir_lval_wrap(irb, scope, ir_gen_defer(irb, scope, node), lval, result_loc);
77968441 case NodeTypeSliceExpr:
7797 return ir_lval_wrap(irb, scope, ir_gen_slice(irb, scope, node), lval);
7798 case NodeTypeUnwrapErrorExpr:
7799 return ir_lval_wrap(irb, scope, ir_gen_catch(irb, scope, node), lval);
8442 return ir_gen_slice(irb, scope, node, lval, result_loc);
8443 case NodeTypeCatchExpr:
8444 return ir_gen_catch(irb, scope, node, lval, result_loc);
78008445 case NodeTypeContainerDecl:
7801 return ir_lval_wrap(irb, scope, ir_gen_container_decl(irb, scope, node), lval);
8446 return ir_lval_wrap(irb, scope, ir_gen_container_decl(irb, scope, node), lval, result_loc);
78028447 case NodeTypeFnProto:
7803 return ir_lval_wrap(irb, scope, ir_gen_fn_proto(irb, scope, node), lval);
8448 return ir_lval_wrap(irb, scope, ir_gen_fn_proto(irb, scope, node), lval, result_loc);
78048449 case NodeTypeErrorSetDecl:
7805 return ir_lval_wrap(irb, scope, ir_gen_err_set_decl(irb, scope, node), lval);
8450 return ir_lval_wrap(irb, scope, ir_gen_err_set_decl(irb, scope, node), lval, result_loc);
78068451 case NodeTypeCancel:
7807 return ir_lval_wrap(irb, scope, ir_gen_cancel(irb, scope, node), lval);
8452 return ir_lval_wrap(irb, scope, ir_gen_cancel(irb, scope, node), lval, result_loc);
78088453 case NodeTypeResume:
7809 return ir_lval_wrap(irb, scope, ir_gen_resume(irb, scope, node), lval);
8454 return ir_lval_wrap(irb, scope, ir_gen_resume(irb, scope, node), lval, result_loc);
78108455 case NodeTypeAwaitExpr:
7811 return ir_lval_wrap(irb, scope, ir_gen_await_expr(irb, scope, node), lval);
8456 return ir_lval_wrap(irb, scope, ir_gen_await_expr(irb, scope, node), lval, result_loc);
78128457 case NodeTypeSuspend:
7813 return ir_lval_wrap(irb, scope, ir_gen_suspend(irb, scope, node), lval);
8458 return ir_lval_wrap(irb, scope, ir_gen_suspend(irb, scope, node), lval, result_loc);
78148459 case NodeTypeEnumLiteral:
7815 return ir_lval_wrap(irb, scope, ir_gen_enum_literal(irb, scope, node), lval);
8460 return ir_lval_wrap(irb, scope, ir_gen_enum_literal(irb, scope, node), lval, result_loc);
78168461 case NodeTypeInferredArrayType:
78178462 add_node_error(irb->codegen, node,
78188463 buf_sprintf("inferred array size invalid here"));
......@@ -7821,14 +8466,28 @@ static IrInstruction *ir_gen_node_raw(IrBuilder *irb, AstNode *node, Scope *scop
78218466 zig_unreachable();
78228467}
78238468
7824static IrInstruction *ir_gen_node_extra(IrBuilder *irb, AstNode *node, Scope *scope, LVal lval) {
7825 IrInstruction *result = ir_gen_node_raw(irb, node, scope, lval);
8469static ResultLoc *no_result_loc(void) {
8470 ResultLocNone *result_loc_none = allocate<ResultLocNone>(1);
8471 result_loc_none->base.id = ResultLocIdNone;
8472 return &result_loc_none->base;
8473}
8474
8475static IrInstruction *ir_gen_node_extra(IrBuilder *irb, AstNode *node, Scope *scope, LVal lval,
8476 ResultLoc *result_loc)
8477{
8478 if (result_loc == nullptr) {
8479 // Create a result location indicating there is none - but if one gets created
8480 // it will be properly distributed.
8481 result_loc = no_result_loc();
8482 ir_build_reset_result(irb, scope, node, result_loc);
8483 }
8484 IrInstruction *result = ir_gen_node_raw(irb, node, scope, lval, result_loc);
78268485 irb->exec->invalid = irb->exec->invalid || (result == irb->codegen->invalid_instruction);
78278486 return result;
78288487}
78298488
78308489static IrInstruction *ir_gen_node(IrBuilder *irb, AstNode *node, Scope *scope) {
7831 return ir_gen_node_extra(irb, node, scope, LValNone);
8490 return ir_gen_node_extra(irb, node, scope, LValNone, nullptr);
78328491}
78338492
78348493static void invalidate_exec(IrExecutable *exec) {
......@@ -7879,17 +8538,19 @@ bool ir_gen(CodeGen *codegen, AstNode *node, Scope *scope, IrExecutable *ir_exec
78798538
78808539 return_type = fn_entry->type_entry->data.fn.fn_type_id.return_type;
78818540 IrInstruction *undef = ir_build_const_undefined(irb, coro_scope, node);
8541 // TODO mark this var decl as "no safety" e.g. disable initializing the undef value to 0xaa
78828542 ZigType *coro_frame_type = get_promise_frame_type(irb->codegen, return_type);
78838543 IrInstruction *coro_frame_type_value = ir_build_const_type(irb, coro_scope, node, coro_frame_type);
7884 // TODO mark this var decl as "no safety" e.g. disable initializing the undef value to 0xaa
7885 ir_build_var_decl_src(irb, coro_scope, node, promise_var, coro_frame_type_value, nullptr, undef);
8544 IrInstruction *undef_coro_frame = ir_build_implicit_cast(irb, coro_scope, node, coro_frame_type_value, undef, nullptr);
8545 build_decl_var_and_init(irb, coro_scope, node, promise_var, undef_coro_frame, "promise", const_bool_false);
78868546 coro_promise_ptr = ir_build_var_ptr(irb, coro_scope, node, promise_var);
78878547
78888548 ZigVar *await_handle_var = ir_create_var(irb, node, coro_scope, nullptr, false, false, true, const_bool_false);
78898549 IrInstruction *null_value = ir_build_const_null(irb, coro_scope, node);
78908550 IrInstruction *await_handle_type_val = ir_build_const_type(irb, coro_scope, node,
78918551 get_optional_type(irb->codegen, irb->codegen->builtin_types.entry_promise));
7892 ir_build_var_decl_src(irb, coro_scope, node, await_handle_var, await_handle_type_val, nullptr, null_value);
8552 IrInstruction *null_await_handle = ir_build_implicit_cast(irb, coro_scope, node, await_handle_type_val, null_value, nullptr);
8553 build_decl_var_and_init(irb, coro_scope, node, await_handle_var, null_await_handle, "await_handle", const_bool_false);
78938554 irb->exec->await_handle_var_ptr = ir_build_var_ptr(irb, coro_scope, node, await_handle_var);
78948555
78958556 u8_ptr_type = ir_build_const_type(irb, coro_scope, node,
......@@ -7899,13 +8560,14 @@ bool ir_gen(CodeGen *codegen, AstNode *node, Scope *scope, IrExecutable *ir_exec
78998560 coro_id = ir_build_coro_id(irb, coro_scope, node, promise_as_u8_ptr);
79008561 coro_size_var = ir_create_var(irb, node, coro_scope, nullptr, false, false, true, const_bool_false);
79018562 IrInstruction *coro_size = ir_build_coro_size(irb, coro_scope, node);
7902 ir_build_var_decl_src(irb, coro_scope, node, coro_size_var, nullptr, nullptr, coro_size);
8563 build_decl_var_and_init(irb, coro_scope, node, coro_size_var, coro_size, "coro_size", const_bool_false);
79038564 IrInstruction *implicit_allocator_ptr = ir_build_get_implicit_allocator(irb, coro_scope, node,
79048565 ImplicitAllocatorIdArg);
79058566 irb->exec->coro_allocator_var = ir_create_var(irb, node, coro_scope, nullptr, true, true, true, const_bool_false);
7906 ir_build_var_decl_src(irb, coro_scope, node, irb->exec->coro_allocator_var, nullptr, nullptr, implicit_allocator_ptr);
8567 build_decl_var_and_init(irb, coro_scope, node, irb->exec->coro_allocator_var, implicit_allocator_ptr,
8568 "allocator", const_bool_false);
79078569 Buf *realloc_field_name = buf_create_from_str(ASYNC_REALLOC_FIELD_NAME);
7908 IrInstruction *realloc_fn_ptr = ir_build_field_ptr(irb, coro_scope, node, implicit_allocator_ptr, realloc_field_name);
8570 IrInstruction *realloc_fn_ptr = ir_build_field_ptr(irb, coro_scope, node, implicit_allocator_ptr, realloc_field_name, false);
79098571 IrInstruction *realloc_fn = ir_build_load_ptr(irb, coro_scope, node, realloc_fn_ptr);
79108572 IrInstruction *maybe_coro_mem_ptr = ir_build_coro_alloc_helper(irb, coro_scope, node, realloc_fn, coro_size);
79118573 IrInstruction *alloc_result_is_ok = ir_build_test_nonnull(irb, coro_scope, node, maybe_coro_mem_ptr);
......@@ -7925,32 +8587,32 @@ bool ir_gen(CodeGen *codegen, AstNode *node, Scope *scope, IrExecutable *ir_exec
79258587
79268588 Buf *atomic_state_field_name = buf_create_from_str(ATOMIC_STATE_FIELD_NAME);
79278589 irb->exec->atomic_state_field_ptr = ir_build_field_ptr(irb, scope, node, coro_promise_ptr,
7928 atomic_state_field_name);
8590 atomic_state_field_name, false);
79298591 IrInstruction *zero = ir_build_const_usize(irb, scope, node, 0);
79308592 ir_build_store_ptr(irb, scope, node, irb->exec->atomic_state_field_ptr, zero);
79318593 Buf *result_field_name = buf_create_from_str(RESULT_FIELD_NAME);
7932 irb->exec->coro_result_field_ptr = ir_build_field_ptr(irb, scope, node, coro_promise_ptr, result_field_name);
8594 irb->exec->coro_result_field_ptr = ir_build_field_ptr(irb, scope, node, coro_promise_ptr, result_field_name, false);
79338595 result_ptr_field_name = buf_create_from_str(RESULT_PTR_FIELD_NAME);
7934 irb->exec->coro_result_ptr_field_ptr = ir_build_field_ptr(irb, scope, node, coro_promise_ptr, result_ptr_field_name);
8596 irb->exec->coro_result_ptr_field_ptr = ir_build_field_ptr(irb, scope, node, coro_promise_ptr, result_ptr_field_name, false);
79358597 ir_build_store_ptr(irb, scope, node, irb->exec->coro_result_ptr_field_ptr, irb->exec->coro_result_field_ptr);
79368598 if (irb->codegen->have_err_ret_tracing) {
79378599 // initialize the error return trace
79388600 Buf *return_addresses_field_name = buf_create_from_str(RETURN_ADDRESSES_FIELD_NAME);
7939 IrInstruction *return_addresses_ptr = ir_build_field_ptr(irb, scope, node, coro_promise_ptr, return_addresses_field_name);
8601 IrInstruction *return_addresses_ptr = ir_build_field_ptr(irb, scope, node, coro_promise_ptr, return_addresses_field_name, false);
79408602
79418603 Buf *err_ret_trace_field_name = buf_create_from_str(ERR_RET_TRACE_FIELD_NAME);
7942 err_ret_trace_ptr = ir_build_field_ptr(irb, scope, node, coro_promise_ptr, err_ret_trace_field_name);
8604 err_ret_trace_ptr = ir_build_field_ptr(irb, scope, node, coro_promise_ptr, err_ret_trace_field_name, false);
79438605 ir_build_mark_err_ret_trace_ptr(irb, scope, node, err_ret_trace_ptr);
79448606
79458607 // coordinate with builtin.zig
79468608 Buf *index_name = buf_create_from_str("index");
7947 IrInstruction *index_ptr = ir_build_field_ptr(irb, scope, node, err_ret_trace_ptr, index_name);
8609 IrInstruction *index_ptr = ir_build_field_ptr(irb, scope, node, err_ret_trace_ptr, index_name, false);
79488610 ir_build_store_ptr(irb, scope, node, index_ptr, zero);
79498611
79508612 Buf *instruction_addresses_name = buf_create_from_str("instruction_addresses");
7951 IrInstruction *addrs_slice_ptr = ir_build_field_ptr(irb, scope, node, err_ret_trace_ptr, instruction_addresses_name);
8613 IrInstruction *addrs_slice_ptr = ir_build_field_ptr(irb, scope, node, err_ret_trace_ptr, instruction_addresses_name, false);
79528614
7953 IrInstruction *slice_value = ir_build_slice(irb, scope, node, return_addresses_ptr, zero, nullptr, false);
8615 IrInstruction *slice_value = ir_build_slice_src(irb, scope, node, return_addresses_ptr, zero, nullptr, false, no_result_loc());
79548616 ir_build_store_ptr(irb, scope, node, addrs_slice_ptr, slice_value);
79558617 }
79568618
......@@ -7961,7 +8623,7 @@ bool ir_gen(CodeGen *codegen, AstNode *node, Scope *scope, IrExecutable *ir_exec
79618623 irb->exec->coro_final_cleanup_block = ir_create_basic_block(irb, scope, "FinalCleanup");
79628624 }
79638625
7964 IrInstruction *result = ir_gen_node_extra(irb, node, scope, LValNone);
8626 IrInstruction *result = ir_gen_node_extra(irb, node, scope, LValNone, nullptr);
79658627 assert(result);
79668628 if (irb->exec->invalid)
79678629 return false;
......@@ -8009,7 +8671,7 @@ bool ir_gen(CodeGen *codegen, AstNode *node, Scope *scope, IrExecutable *ir_exec
80098671 }
80108672 if (irb->codegen->have_err_ret_tracing) {
80118673 Buf *err_ret_trace_ptr_field_name = buf_create_from_str(ERR_RET_TRACE_PTR_FIELD_NAME);
8012 IrInstruction *err_ret_trace_ptr_field_ptr = ir_build_field_ptr(irb, scope, node, coro_promise_ptr, err_ret_trace_ptr_field_name);
8674 IrInstruction *err_ret_trace_ptr_field_ptr = ir_build_field_ptr(irb, scope, node, coro_promise_ptr, err_ret_trace_ptr_field_name, false);
80138675 IrInstruction *dest_err_ret_trace_ptr = ir_build_load_ptr(irb, scope, node, err_ret_trace_ptr_field_ptr);
80148676 ir_build_merge_err_ret_traces(irb, scope, node, coro_promise_ptr, err_ret_trace_ptr, dest_err_ret_trace_ptr);
80158677 }
......@@ -8017,7 +8679,7 @@ bool ir_gen(CodeGen *codegen, AstNode *node, Scope *scope, IrExecutable *ir_exec
80178679 // a register or local variable which does not get spilled into the frame,
80188680 // otherwise llvm tries to access memory inside the destroyed frame.
80198681 IrInstruction *unwrapped_await_handle_ptr = ir_build_optional_unwrap_ptr(irb, scope, node,
8020 irb->exec->await_handle_var_ptr, false);
8682 irb->exec->await_handle_var_ptr, false, false);
80218683 IrInstruction *await_handle_in_block = ir_build_load_ptr(irb, scope, node, unwrapped_await_handle_ptr);
80228684 ir_build_br(irb, scope, node, check_free_block, const_bool_false);
80238685
......@@ -8031,7 +8693,7 @@ bool ir_gen(CodeGen *codegen, AstNode *node, Scope *scope, IrExecutable *ir_exec
80318693 incoming_values[0] = const_bool_false;
80328694 incoming_blocks[1] = irb->exec->coro_normal_final;
80338695 incoming_values[1] = const_bool_true;
8034 IrInstruction *resume_awaiter = ir_build_phi(irb, scope, node, 2, incoming_blocks, incoming_values);
8696 IrInstruction *resume_awaiter = ir_build_phi(irb, scope, node, 2, incoming_blocks, incoming_values, nullptr);
80358697
80368698 IrBasicBlock **merge_incoming_blocks = allocate<IrBasicBlock *>(2);
80378699 IrInstruction **merge_incoming_values = allocate<IrInstruction *>(2);
......@@ -8039,12 +8701,12 @@ bool ir_gen(CodeGen *codegen, AstNode *node, Scope *scope, IrExecutable *ir_exec
80398701 merge_incoming_values[0] = ir_build_const_undefined(irb, scope, node);
80408702 merge_incoming_blocks[1] = irb->exec->coro_normal_final;
80418703 merge_incoming_values[1] = await_handle_in_block;
8042 IrInstruction *awaiter_handle = ir_build_phi(irb, scope, node, 2, merge_incoming_blocks, merge_incoming_values);
8704 IrInstruction *awaiter_handle = ir_build_phi(irb, scope, node, 2, merge_incoming_blocks, merge_incoming_values, nullptr);
80438705
80448706 Buf *shrink_field_name = buf_create_from_str(ASYNC_SHRINK_FIELD_NAME);
80458707 IrInstruction *implicit_allocator_ptr = ir_build_get_implicit_allocator(irb, scope, node,
80468708 ImplicitAllocatorIdLocalVar);
8047 IrInstruction *shrink_fn_ptr = ir_build_field_ptr(irb, scope, node, implicit_allocator_ptr, shrink_field_name);
8709 IrInstruction *shrink_fn_ptr = ir_build_field_ptr(irb, scope, node, implicit_allocator_ptr, shrink_field_name, false);
80488710 IrInstruction *shrink_fn = ir_build_load_ptr(irb, scope, node, shrink_fn_ptr);
80498711 IrInstruction *zero = ir_build_const_usize(irb, scope, node, 0);
80508712 IrInstruction *coro_mem_ptr_maybe = ir_build_coro_free(irb, scope, node, coro_id, irb->exec->coro_handle);
......@@ -8056,7 +8718,8 @@ bool ir_gen(CodeGen *codegen, AstNode *node, Scope *scope, IrExecutable *ir_exec
80568718 IrInstruction *coro_mem_ptr_ref = ir_build_ref(irb, scope, node, coro_mem_ptr, true, false);
80578719 IrInstruction *coro_size_ptr = ir_build_var_ptr(irb, scope, node, coro_size_var);
80588720 IrInstruction *coro_size = ir_build_load_ptr(irb, scope, node, coro_size_ptr);
8059 IrInstruction *mem_slice = ir_build_slice(irb, scope, node, coro_mem_ptr_ref, zero, coro_size, false);
8721 IrInstruction *mem_slice = ir_build_slice_src(irb, scope, node, coro_mem_ptr_ref, zero, coro_size, false,
8722 no_result_loc());
80608723 size_t arg_count = 5;
80618724 IrInstruction **args = allocate<IrInstruction *>(arg_count);
80628725 args[0] = implicit_allocator_ptr; // self
......@@ -8070,7 +8733,8 @@ bool ir_gen(CodeGen *codegen, AstNode *node, Scope *scope, IrExecutable *ir_exec
80708733 // non-allocating. Basically coroutines are not supported right now until they are reworked.
80718734 args[3] = ir_build_const_usize(irb, scope, node, 1); // new_size
80728735 args[4] = ir_build_const_usize(irb, scope, node, 1); // new_align
8073 ir_build_call(irb, scope, node, nullptr, shrink_fn, arg_count, args, false, FnInlineAuto, false, nullptr, nullptr);
8736 ir_build_call_src(irb, scope, node, nullptr, shrink_fn, arg_count, args, false, FnInlineAuto, false, nullptr,
8737 nullptr, no_result_loc());
80748738
80758739 IrBasicBlock *resume_block = ir_create_basic_block(irb, scope, "Resume");
80768740 ir_build_cond_br(irb, scope, node, resume_awaiter, resume_block, irb->exec->coro_suspend_block, const_bool_false);
......@@ -8177,6 +8841,15 @@ static ConstExprValue *ir_exec_const_result(CodeGen *codegen, IrExecutable *exec
81778841 }
81788842 return &value->value;
81798843 } else if (ir_has_side_effects(instruction)) {
8844 if (instr_is_comptime(instruction)) {
8845 switch (instruction->id) {
8846 case IrInstructionIdUnwrapErrPayload:
8847 case IrInstructionIdUnionFieldPtr:
8848 continue;
8849 default:
8850 break;
8851 }
8852 }
81808853 exec_add_error_node(codegen, exec, instruction->source_node,
81818854 buf_sprintf("unable to evaluate constant expression"));
81828855 return &codegen->invalid_instruction->value;
......@@ -9154,15 +9827,6 @@ static bool ir_num_lit_fits_in_other_type(IrAnalyze *ira, IrInstruction *instruc
91549827 return false;
91559828}
91569829
9157static bool is_slice(ZigType *type) {
9158 return type->id == ZigTypeIdStruct && type->data.structure.is_slice;
9159}
9160
9161static bool slice_is_const(ZigType *type) {
9162 assert(is_slice(type));
9163 return type->data.structure.fields[slice_ptr_index].type_entry->data.pointer.is_const;
9164}
9165
91669830static bool is_tagged_union(ZigType *type) {
91679831 if (type->id != ZigTypeIdUnion)
91689832 return false;
......@@ -9533,9 +10197,21 @@ static ZigType *ir_resolve_peer_types(IrAnalyze *ira, AstNode *source_node, ZigT
953310197{
953410198 Error err;
953510199 assert(instruction_count >= 1);
9536 IrInstruction *prev_inst = instructions[0];
9537 if (type_is_invalid(prev_inst->value.type)) {
9538 return ira->codegen->builtin_types.entry_invalid;
10200 IrInstruction *prev_inst;
10201 size_t i = 0;
10202 for (;;) {
10203 prev_inst = instructions[i];
10204 if (type_is_invalid(prev_inst->value.type)) {
10205 return ira->codegen->builtin_types.entry_invalid;
10206 }
10207 if (prev_inst->value.type->id == ZigTypeIdUnreachable) {
10208 i += 1;
10209 if (i == instruction_count) {
10210 return prev_inst->value.type;
10211 }
10212 continue;
10213 }
10214 break;
953910215 }
954010216 ErrorTableEntry **errors = nullptr;
954110217 size_t errors_count = 0;
......@@ -9560,7 +10236,7 @@ static ZigType *ir_resolve_peer_types(IrAnalyze *ira, AstNode *source_node, ZigT
956010236
956110237 bool any_are_null = (prev_inst->value.type->id == ZigTypeIdNull);
956210238 bool convert_to_const_slice = false;
9563 for (size_t i = 1; i < instruction_count; i += 1) {
10239 for (; i < instruction_count; i += 1) {
956410240 IrInstruction *cur_inst = instructions[i];
956510241 ZigType *cur_type = cur_inst->value.type;
956610242 ZigType *prev_type = prev_inst->value.type;
......@@ -9579,7 +10255,7 @@ static ZigType *ir_resolve_peer_types(IrAnalyze *ira, AstNode *source_node, ZigT
957910255 }
958010256
958110257 if (prev_type->id == ZigTypeIdErrorSet) {
9582 assert(err_set_type != nullptr);
10258 ir_assert(err_set_type != nullptr, prev_inst);
958310259 if (cur_type->id == ZigTypeIdErrorSet) {
958410260 if (type_is_global_error_set(err_set_type)) {
958510261 continue;
......@@ -9839,6 +10515,7 @@ static ZigType *ir_resolve_peer_types(IrAnalyze *ira, AstNode *source_node, ZigT
983910515
984010516 if (prev_type->id == ZigTypeIdNull) {
984110517 prev_inst = cur_inst;
10518 any_are_null = true;
984210519 continue;
984310520 }
984410521
......@@ -10153,6 +10830,8 @@ static ZigType *ir_resolve_peer_types(IrAnalyze *ira, AstNode *source_node, ZigT
1015310830 } else if (prev_inst->value.type->id == ZigTypeIdOptional) {
1015410831 return prev_inst->value.type;
1015510832 } else {
10833 if ((err = type_resolve(ira->codegen, prev_inst->value.type, ResolveStatusSizeKnown)))
10834 return ira->codegen->builtin_types.entry_invalid;
1015610835 return get_optional_type(ira->codegen, prev_inst->value.type);
1015710836 }
1015810837 } else {
......@@ -10160,24 +10839,18 @@ static ZigType *ir_resolve_peer_types(IrAnalyze *ira, AstNode *source_node, ZigT
1016010839 }
1016110840}
1016210841
10163static void ir_add_alloca(IrAnalyze *ira, IrInstruction *instruction, ZigType *type_entry) {
10164 if (type_has_bits(type_entry) && handle_is_ptr(type_entry)) {
10165 ZigFn *fn_entry = exec_fn_entry(ira->new_irb.exec);
10166 if (fn_entry != nullptr) {
10167 fn_entry->alloca_list.append(instruction);
10168 }
10169 }
10170}
10171
1017210842static void copy_const_val(ConstExprValue *dest, ConstExprValue *src, bool same_global_refs) {
1017310843 ConstGlobalRefs *global_refs = dest->global_refs;
10174 assert(!same_global_refs || src->global_refs != nullptr);
10175 *dest = *src;
10844 memcpy(dest, src, sizeof(ConstExprValue));
1017610845 if (!same_global_refs) {
1017710846 dest->global_refs = global_refs;
10847 if (src->special == ConstValSpecialUndef)
10848 return;
1017810849 if (dest->type->id == ZigTypeIdStruct) {
10179 dest->data.x_struct.fields = allocate_nonzero<ConstExprValue>(dest->type->data.structure.src_field_count);
10180 memcpy(dest->data.x_struct.fields, src->data.x_struct.fields, sizeof(ConstExprValue) * dest->type->data.structure.src_field_count);
10850 dest->data.x_struct.fields = create_const_vals(dest->type->data.structure.src_field_count);
10851 for (size_t i = 0; i < dest->type->data.structure.src_field_count; i += 1) {
10852 copy_const_val(&dest->data.x_struct.fields[i], &src->data.x_struct.fields[i], false);
10853 }
1018110854 }
1018210855 }
1018310856}
......@@ -10195,7 +10868,6 @@ static bool eval_const_expr_implicit_cast(IrAnalyze *ira, IrInstruction *source_
1019510868 zig_unreachable();
1019610869 case CastOpErrSet:
1019710870 case CastOpBitCast:
10198 case CastOpPtrOfArrayToSlice:
1019910871 zig_panic("TODO");
1020010872 case CastOpNoop:
1020110873 {
......@@ -10295,7 +10967,7 @@ static IrInstruction *ir_const(IrAnalyze *ira, IrInstruction *old_instruction, Z
1029510967}
1029610968
1029710969static IrInstruction *ir_resolve_cast(IrAnalyze *ira, IrInstruction *source_instr, IrInstruction *value,
10298 ZigType *wanted_type, CastOp cast_op, bool need_alloca)
10970 ZigType *wanted_type, CastOp cast_op)
1029910971{
1030010972 if (instr_is_comptime(value) || !type_has_bits(wanted_type)) {
1030110973 IrInstruction *result = ir_const(ira, source_instr, wanted_type);
......@@ -10308,9 +10980,6 @@ static IrInstruction *ir_resolve_cast(IrAnalyze *ira, IrInstruction *source_inst
1030810980 } else {
1030910981 IrInstruction *result = ir_build_cast(&ira->new_irb, source_instr->scope, source_instr->source_node, wanted_type, value, cast_op);
1031010982 result->value.type = wanted_type;
10311 if (need_alloca) {
10312 ir_add_alloca(ira, result, wanted_type);
10313 }
1031410983 return result;
1031510984 }
1031610985}
......@@ -10352,7 +11021,7 @@ static IrInstruction *ir_resolve_ptr_of_array_to_unknown_len_ptr(IrAnalyze *ira,
1035211021}
1035311022
1035411023static IrInstruction *ir_resolve_ptr_of_array_to_slice(IrAnalyze *ira, IrInstruction *source_instr,
10355 IrInstruction *value, ZigType *wanted_type)
11024 IrInstruction *value, ZigType *wanted_type, ResultLoc *result_loc)
1035611025{
1035711026 Error err;
1035811027
......@@ -10383,11 +11052,12 @@ static IrInstruction *ir_resolve_ptr_of_array_to_slice(IrAnalyze *ira, IrInstruc
1038311052 }
1038411053 }
1038511054
10386 IrInstruction *result = ir_build_cast(&ira->new_irb, source_instr->scope, source_instr->source_node,
10387 wanted_type, value, CastOpPtrOfArrayToSlice);
10388 result->value.type = wanted_type;
10389 ir_add_alloca(ira, result, wanted_type);
10390 return result;
11055 if (result_loc == nullptr) result_loc = no_result_loc();
11056 IrInstruction *result_loc_inst = ir_resolve_result(ira, source_instr, result_loc, wanted_type, nullptr, true, false);
11057 if (type_is_invalid(result_loc_inst->value.type) || instr_is_unreachable(result_loc_inst)) {
11058 return result_loc_inst;
11059 }
11060 return ir_build_ptr_of_array_to_slice(ira, source_instr, wanted_type, value, result_loc_inst);
1039111061}
1039211062
1039311063static IrBasicBlock *ir_get_new_bb(IrAnalyze *ira, IrBasicBlock *old_bb, IrInstruction *ref_old_instruction) {
......@@ -10419,51 +11089,136 @@ static IrBasicBlock *ir_get_new_bb_runtime(IrAnalyze *ira, IrBasicBlock *old_bb,
1041911089}
1042011090
1042111091static void ir_start_bb(IrAnalyze *ira, IrBasicBlock *old_bb, IrBasicBlock *const_predecessor_bb) {
11092 assert(!old_bb->suspended);
1042211093 ira->instruction_index = 0;
1042311094 ira->old_irb.current_basic_block = old_bb;
1042411095 ira->const_predecessor_bb = const_predecessor_bb;
11096 ira->old_bb_index = old_bb->index;
1042511097}
1042611098
10427static void ir_finish_bb(IrAnalyze *ira) {
10428 ira->new_irb.exec->basic_block_list.append(ira->new_irb.current_basic_block);
10429 ira->instruction_index += 1;
10430 while (ira->instruction_index < ira->old_irb.current_basic_block->instruction_list.length) {
10431 IrInstruction *next_instruction = ira->old_irb.current_basic_block->instruction_list.at(ira->instruction_index);
10432 if (!next_instruction->is_gen) {
10433 ir_add_error(ira, next_instruction, buf_sprintf("unreachable code"));
10434 break;
10435 }
10436 ira->instruction_index += 1;
11099static IrInstruction *ira_suspend(IrAnalyze *ira, IrInstruction *old_instruction, IrBasicBlock *next_bb,
11100 IrSuspendPosition *suspend_pos)
11101{
11102 if (ira->codegen->verbose_ir) {
11103 fprintf(stderr, "suspend %s_%zu %s_%zu #%zu (%zu,%zu)\n", ira->old_irb.current_basic_block->name_hint,
11104 ira->old_irb.current_basic_block->debug_id,
11105 ira->old_irb.exec->basic_block_list.at(ira->old_bb_index)->name_hint,
11106 ira->old_irb.exec->basic_block_list.at(ira->old_bb_index)->debug_id,
11107 ira->old_irb.current_basic_block->instruction_list.at(ira->instruction_index)->debug_id,
11108 ira->old_bb_index, ira->instruction_index);
11109 }
11110 suspend_pos->basic_block_index = ira->old_bb_index;
11111 suspend_pos->instruction_index = ira->instruction_index;
11112
11113 ira->old_irb.current_basic_block->suspended = true;
11114
11115 // null next_bb means that the caller plans to call ira_resume before returning
11116 if (next_bb != nullptr) {
11117 ira->old_bb_index = next_bb->index;
11118 ira->old_irb.current_basic_block = ira->old_irb.exec->basic_block_list.at(ira->old_bb_index);
11119 assert(ira->old_irb.current_basic_block == next_bb);
11120 ira->instruction_index = 0;
11121 ira->const_predecessor_bb = nullptr;
11122 next_bb->other = ir_get_new_bb_runtime(ira, next_bb, old_instruction);
11123 ira->new_irb.current_basic_block = next_bb->other;
1043711124 }
11125 return ira->codegen->unreach_instruction;
11126}
11127
11128static IrInstruction *ira_resume(IrAnalyze *ira) {
11129 IrSuspendPosition pos = ira->resume_stack.pop();
11130 if (ira->codegen->verbose_ir) {
11131 fprintf(stderr, "resume (%zu,%zu) ", pos.basic_block_index, pos.instruction_index);
11132 }
11133 ira->old_bb_index = pos.basic_block_index;
11134 ira->old_irb.current_basic_block = ira->old_irb.exec->basic_block_list.at(ira->old_bb_index);
11135 assert(ira->old_irb.current_basic_block->in_resume_stack);
11136 ira->old_irb.current_basic_block->in_resume_stack = false;
11137 ira->old_irb.current_basic_block->suspended = false;
11138 ira->instruction_index = pos.instruction_index;
11139 assert(pos.instruction_index < ira->old_irb.current_basic_block->instruction_list.length);
11140 if (ira->codegen->verbose_ir) {
11141 fprintf(stderr, "%s_%zu #%zu\n", ira->old_irb.current_basic_block->name_hint,
11142 ira->old_irb.current_basic_block->debug_id,
11143 ira->old_irb.current_basic_block->instruction_list.at(pos.instruction_index)->debug_id);
11144 }
11145 ira->const_predecessor_bb = nullptr;
11146 ira->new_irb.current_basic_block = ira->old_irb.current_basic_block->other;
11147 assert(ira->new_irb.current_basic_block != nullptr);
11148 return ira->codegen->unreach_instruction;
11149}
1043811150
10439 size_t my_old_bb_index = ira->old_bb_index;
11151static void ir_start_next_bb(IrAnalyze *ira) {
1044011152 ira->old_bb_index += 1;
1044111153
1044211154 bool need_repeat = true;
1044311155 for (;;) {
1044411156 while (ira->old_bb_index < ira->old_irb.exec->basic_block_list.length) {
1044511157 IrBasicBlock *old_bb = ira->old_irb.exec->basic_block_list.at(ira->old_bb_index);
10446 if (old_bb->other == nullptr) {
11158 if (old_bb->other == nullptr && old_bb->suspend_instruction_ref == nullptr) {
1044711159 ira->old_bb_index += 1;
1044811160 continue;
1044911161 }
10450 if (old_bb->other->instruction_list.length != 0 || ira->old_bb_index == my_old_bb_index) {
11162 // if it's already started, or
11163 // if it's a suspended block,
11164 // then skip it
11165 if (old_bb->suspended ||
11166 (old_bb->other != nullptr && old_bb->other->instruction_list.length != 0) ||
11167 (old_bb->other != nullptr && old_bb->other->already_appended))
11168 {
1045111169 ira->old_bb_index += 1;
1045211170 continue;
1045311171 }
10454 ira->new_irb.current_basic_block = old_bb->other;
1045511172
11173 // if there is a resume_stack, pop one from there rather than moving on.
11174 // the last item of the resume stack will be a basic block that will
11175 // move on to the next one below
11176 if (ira->resume_stack.length != 0) {
11177 ira_resume(ira);
11178 return;
11179 }
11180
11181 if (old_bb->other == nullptr) {
11182 old_bb->other = ir_get_new_bb_runtime(ira, old_bb, old_bb->suspend_instruction_ref);
11183 }
11184 ira->new_irb.current_basic_block = old_bb->other;
1045611185 ir_start_bb(ira, old_bb, nullptr);
1045711186 return;
1045811187 }
10459 if (!need_repeat)
11188 if (!need_repeat) {
11189 if (ira->resume_stack.length != 0) {
11190 ira_resume(ira);
11191 }
1046011192 return;
11193 }
1046111194 need_repeat = false;
1046211195 ira->old_bb_index = 0;
1046311196 continue;
1046411197 }
1046511198}
1046611199
11200static void ir_finish_bb(IrAnalyze *ira) {
11201 if (!ira->new_irb.current_basic_block->already_appended) {
11202 ira->new_irb.current_basic_block->already_appended = true;
11203 if (ira->codegen->verbose_ir) {
11204 fprintf(stderr, "append new bb %s_%zu\n", ira->new_irb.current_basic_block->name_hint,
11205 ira->new_irb.current_basic_block->debug_id);
11206 }
11207 ira->new_irb.exec->basic_block_list.append(ira->new_irb.current_basic_block);
11208 }
11209 ira->instruction_index += 1;
11210 while (ira->instruction_index < ira->old_irb.current_basic_block->instruction_list.length) {
11211 IrInstruction *next_instruction = ira->old_irb.current_basic_block->instruction_list.at(ira->instruction_index);
11212 if (!next_instruction->is_gen) {
11213 ir_add_error(ira, next_instruction, buf_sprintf("unreachable code"));
11214 break;
11215 }
11216 ira->instruction_index += 1;
11217 }
11218
11219 ir_start_next_bb(ira);
11220}
11221
1046711222static IrInstruction *ir_unreach_error(IrAnalyze *ira) {
1046811223 ira->old_bb_index = SIZE_MAX;
1046911224 ira->new_irb.exec->invalid = true;
......@@ -10524,6 +11279,12 @@ static IrInstruction *ir_const_undef(IrAnalyze *ira, IrInstruction *source_instr
1052411279 return result;
1052511280}
1052611281
11282static IrInstruction *ir_const_unreachable(IrAnalyze *ira, IrInstruction *source_instruction) {
11283 IrInstruction *result = ir_const(ira, source_instruction, ira->codegen->builtin_types.entry_unreachable);
11284 result->value.special = ConstValSpecialStatic;
11285 return result;
11286}
11287
1052711288static IrInstruction *ir_const_void(IrAnalyze *ira, IrInstruction *source_instruction) {
1052811289 return ir_const(ira, source_instruction, ira->codegen->builtin_types.entry_void);
1052911290}
......@@ -10721,7 +11482,7 @@ static ZigFn *ir_resolve_fn(IrAnalyze *ira, IrInstruction *fn_value) {
1072111482}
1072211483
1072311484static IrInstruction *ir_analyze_optional_wrap(IrAnalyze *ira, IrInstruction *source_instr, IrInstruction *value,
10724 ZigType *wanted_type)
11485 ZigType *wanted_type, ResultLoc *result_loc)
1072511486{
1072611487 assert(wanted_type->id == ZigTypeIdOptional);
1072711488
......@@ -10747,20 +11508,29 @@ static IrInstruction *ir_analyze_optional_wrap(IrAnalyze *ira, IrInstruction *so
1074711508 return &const_instruction->base;
1074811509 }
1074911510
10750 IrInstruction *result = ir_build_maybe_wrap(&ira->new_irb, source_instr->scope, source_instr->source_node, value);
10751 result->value.type = wanted_type;
11511 if (result_loc == nullptr && handle_is_ptr(wanted_type)) {
11512 result_loc = no_result_loc();
11513 }
11514 IrInstruction *result_loc_inst = nullptr;
11515 if (result_loc != nullptr) {
11516 result_loc_inst = ir_resolve_result(ira, source_instr, result_loc, wanted_type, nullptr, true, false);
11517 if (type_is_invalid(result_loc_inst->value.type) || instr_is_unreachable(result_loc_inst)) {
11518 return result_loc_inst;
11519 }
11520 }
11521 IrInstruction *result = ir_build_optional_wrap(ira, source_instr, wanted_type, value, result_loc_inst);
1075211522 result->value.data.rh_maybe = RuntimeHintOptionalNonNull;
10753 ir_add_alloca(ira, result, wanted_type);
1075411523 return result;
1075511524}
1075611525
1075711526static IrInstruction *ir_analyze_err_wrap_payload(IrAnalyze *ira, IrInstruction *source_instr,
10758 IrInstruction *value, ZigType *wanted_type)
11527 IrInstruction *value, ZigType *wanted_type, ResultLoc *result_loc)
1075911528{
1076011529 assert(wanted_type->id == ZigTypeIdErrorUnion);
1076111530
11531 ZigType *payload_type = wanted_type->data.error_union.payload_type;
11532 ZigType *err_set_type = wanted_type->data.error_union.err_set_type;
1076211533 if (instr_is_comptime(value)) {
10763 ZigType *payload_type = wanted_type->data.error_union.payload_type;
1076411534 IrInstruction *casted_payload = ir_implicit_cast(ira, value, payload_type);
1076511535 if (type_is_invalid(casted_payload->value.type))
1076611536 return ira->codegen->invalid_instruction;
......@@ -10770,7 +11540,7 @@ static IrInstruction *ir_analyze_err_wrap_payload(IrAnalyze *ira, IrInstruction
1077011540 return ira->codegen->invalid_instruction;
1077111541
1077211542 ConstExprValue *err_set_val = create_const_vals(1);
10773 err_set_val->type = wanted_type->data.error_union.err_set_type;
11543 err_set_val->type = err_set_type;
1077411544 err_set_val->special = ConstValSpecialStatic;
1077511545 err_set_val->data.x_err_set = nullptr;
1077611546
......@@ -10783,10 +11553,19 @@ static IrInstruction *ir_analyze_err_wrap_payload(IrAnalyze *ira, IrInstruction
1078311553 return &const_instruction->base;
1078411554 }
1078511555
10786 IrInstruction *result = ir_build_err_wrap_payload(&ira->new_irb, source_instr->scope, source_instr->source_node, value);
10787 result->value.type = wanted_type;
11556 IrInstruction *result_loc_inst;
11557 if (handle_is_ptr(wanted_type)) {
11558 if (result_loc == nullptr) result_loc = no_result_loc();
11559 result_loc_inst = ir_resolve_result(ira, source_instr, result_loc, wanted_type, nullptr, true, false);
11560 if (type_is_invalid(result_loc_inst->value.type) || instr_is_unreachable(result_loc_inst)) {
11561 return result_loc_inst;
11562 }
11563 } else {
11564 result_loc_inst = nullptr;
11565 }
11566
11567 IrInstruction *result = ir_build_err_wrap_payload(ira, source_instr, wanted_type, value, result_loc_inst);
1078811568 result->value.data.rh_error_union = RuntimeHintErrorUnionNonError;
10789 ir_add_alloca(ira, result, wanted_type);
1079011569 return result;
1079111570}
1079211571
......@@ -10833,7 +11612,9 @@ static IrInstruction *ir_analyze_err_set_cast(IrAnalyze *ira, IrInstruction *sou
1083311612 return result;
1083411613}
1083511614
10836static IrInstruction *ir_analyze_err_wrap_code(IrAnalyze *ira, IrInstruction *source_instr, IrInstruction *value, ZigType *wanted_type) {
11615static IrInstruction *ir_analyze_err_wrap_code(IrAnalyze *ira, IrInstruction *source_instr, IrInstruction *value,
11616 ZigType *wanted_type, ResultLoc *result_loc)
11617{
1083711618 assert(wanted_type->id == ZigTypeIdErrorUnion);
1083811619
1083911620 IrInstruction *casted_value = ir_implicit_cast(ira, value, wanted_type->data.error_union.err_set_type);
......@@ -10857,10 +11638,20 @@ static IrInstruction *ir_analyze_err_wrap_code(IrAnalyze *ira, IrInstruction *so
1085711638 return &const_instruction->base;
1085811639 }
1085911640
10860 IrInstruction *result = ir_build_err_wrap_code(&ira->new_irb, source_instr->scope, source_instr->source_node, value);
10861 result->value.type = wanted_type;
11641 IrInstruction *result_loc_inst;
11642 if (handle_is_ptr(wanted_type)) {
11643 if (result_loc == nullptr) result_loc = no_result_loc();
11644 result_loc_inst = ir_resolve_result(ira, source_instr, result_loc, wanted_type, nullptr, true, false);
11645 if (type_is_invalid(result_loc_inst->value.type) || instr_is_unreachable(result_loc_inst)) {
11646 return result_loc_inst;
11647 }
11648 } else {
11649 result_loc_inst = nullptr;
11650 }
11651
11652
11653 IrInstruction *result = ir_build_err_wrap_code(ira, source_instr, wanted_type, value, result_loc_inst);
1086211654 result->value.data.rh_error_union = RuntimeHintErrorUnionError;
10863 ir_add_alloca(ira, result, wanted_type);
1086411655 return result;
1086511656}
1086611657
......@@ -10920,20 +11711,21 @@ static IrInstruction *ir_get_ref(IrAnalyze *ira, IrInstruction *source_instructi
1092011711
1092111712 ZigType *ptr_type = get_pointer_to_type_extra(ira->codegen, value->value.type,
1092211713 is_const, is_volatile, PtrLenSingle, 0, 0, 0, false);
10923 IrInstruction *new_instruction = ir_build_ref(&ira->new_irb, source_instruction->scope,
10924 source_instruction->source_node, value, is_const, is_volatile);
10925 new_instruction->value.type = ptr_type;
10926 new_instruction->value.data.rh_ptr = RuntimeHintPtrStack;
11714
11715 IrInstruction *result_loc;
1092711716 if (type_has_bits(ptr_type) && !handle_is_ptr(value->value.type)) {
10928 ZigFn *fn_entry = exec_fn_entry(ira->new_irb.exec);
10929 assert(fn_entry);
10930 fn_entry->alloca_list.append(new_instruction);
11717 result_loc = ir_resolve_result(ira, source_instruction, no_result_loc(), value->value.type, nullptr, true, false);
11718 } else {
11719 result_loc = nullptr;
1093111720 }
11721
11722 IrInstruction *new_instruction = ir_build_ref_gen(ira, source_instruction, ptr_type, value, result_loc);
11723 new_instruction->value.data.rh_ptr = RuntimeHintPtrStack;
1093211724 return new_instruction;
1093311725}
1093411726
1093511727static IrInstruction *ir_analyze_array_to_slice(IrAnalyze *ira, IrInstruction *source_instr,
10936 IrInstruction *array_arg, ZigType *wanted_type)
11728 IrInstruction *array_arg, ZigType *wanted_type, ResultLoc *result_loc)
1093711729{
1093811730 assert(is_slice(wanted_type));
1093911731 // In this function we honor the const-ness of wanted_type, because
......@@ -10942,7 +11734,7 @@ static IrInstruction *ir_analyze_array_to_slice(IrAnalyze *ira, IrInstruction *s
1094211734 IrInstruction *array_ptr = nullptr;
1094311735 IrInstruction *array;
1094411736 if (array_arg->value.type->id == ZigTypeIdPointer) {
10945 array = ir_get_deref(ira, source_instr, array_arg);
11737 array = ir_get_deref(ira, source_instr, array_arg, nullptr);
1094611738 array_ptr = array_arg;
1094711739 } else {
1094811740 array = array_arg;
......@@ -10965,12 +11757,14 @@ static IrInstruction *ir_analyze_array_to_slice(IrAnalyze *ira, IrInstruction *s
1096511757
1096611758 if (!array_ptr) array_ptr = ir_get_ref(ira, source_instr, array, true, false);
1096711759
10968 IrInstruction *result = ir_build_slice(&ira->new_irb, source_instr->scope,
10969 source_instr->source_node, array_ptr, start, end, false);
10970 result->value.type = wanted_type;
11760 if (result_loc == nullptr) result_loc = no_result_loc();
11761 IrInstruction *result_loc_inst = ir_resolve_result(ira, source_instr, result_loc, wanted_type, nullptr, true, false);
11762 if (type_is_invalid(result_loc_inst->value.type) || instr_is_unreachable(result_loc_inst)) {
11763 return result_loc_inst;
11764 }
11765 IrInstruction *result = ir_build_slice_gen(ira, source_instr, wanted_type, array_ptr, start, end, false, result_loc_inst);
1097111766 result->value.data.rh_slice.id = RuntimeHintSliceIdLen;
1097211767 result->value.data.rh_slice.len = array_type->data.array.len;
10973 ir_add_alloca(ira, result, result->value.type);
1097411768
1097511769 return result;
1097611770}
......@@ -11608,7 +12402,7 @@ static IrInstruction *ir_analyze_array_to_vector(IrAnalyze *ira, IrInstruction *
1160812402}
1160912403
1161012404static IrInstruction *ir_analyze_vector_to_array(IrAnalyze *ira, IrInstruction *source_instr,
11611 IrInstruction *vector, ZigType *array_type)
12405 IrInstruction *vector, ZigType *array_type, ResultLoc *result_loc)
1161212406{
1161312407 if (instr_is_comptime(vector)) {
1161412408 // arrays and vectors have the same ConstExprValue representation
......@@ -11617,7 +12411,14 @@ static IrInstruction *ir_analyze_vector_to_array(IrAnalyze *ira, IrInstruction *
1161712411 result->value.type = array_type;
1161812412 return result;
1161912413 }
11620 return ir_build_vector_to_array(ira, source_instr, vector, array_type);
12414 if (result_loc == nullptr) {
12415 result_loc = no_result_loc();
12416 }
12417 IrInstruction *result_loc_inst = ir_resolve_result(ira, source_instr, result_loc, array_type, nullptr, true, false);
12418 if (type_is_invalid(result_loc_inst->value.type) || instr_is_unreachable(result_loc_inst)) {
12419 return result_loc_inst;
12420 }
12421 return ir_build_vector_to_array(ira, source_instr, array_type, vector, result_loc_inst);
1162112422}
1162212423
1162312424static IrInstruction *ir_analyze_int_to_c_ptr(IrAnalyze *ira, IrInstruction *source_instr,
......@@ -11667,7 +12468,7 @@ static bool is_pointery_and_elem_is_not_pointery(ZigType *ty) {
1166712468}
1166812469
1166912470static IrInstruction *ir_analyze_cast(IrAnalyze *ira, IrInstruction *source_instr,
11670 ZigType *wanted_type, IrInstruction *value)
12471 ZigType *wanted_type, IrInstruction *value, ResultLoc *result_loc)
1167112472{
1167212473 Error err;
1167312474 ZigType *actual_type = value->value.type;
......@@ -11683,7 +12484,7 @@ static IrInstruction *ir_analyze_cast(IrAnalyze *ira, IrInstruction *source_inst
1168312484 if (const_cast_result.id == ConstCastResultIdInvalid)
1168412485 return ira->codegen->invalid_instruction;
1168512486 if (const_cast_result.id == ConstCastResultIdOk) {
11686 return ir_resolve_cast(ira, source_instr, value, wanted_type, CastOpNoop, false);
12487 return ir_resolve_cast(ira, source_instr, value, wanted_type, CastOpNoop);
1168712488 }
1168812489
1168912490 // cast from T to ?T
......@@ -11693,12 +12494,12 @@ static IrInstruction *ir_analyze_cast(IrAnalyze *ira, IrInstruction *source_inst
1169312494 if (types_match_const_cast_only(ira, wanted_child_type, actual_type, source_node,
1169412495 false).id == ConstCastResultIdOk)
1169512496 {
11696 return ir_analyze_optional_wrap(ira, source_instr, value, wanted_type);
12497 return ir_analyze_optional_wrap(ira, source_instr, value, wanted_type, result_loc);
1169712498 } else if (actual_type->id == ZigTypeIdComptimeInt ||
1169812499 actual_type->id == ZigTypeIdComptimeFloat)
1169912500 {
1170012501 if (ir_num_lit_fits_in_other_type(ira, value, wanted_child_type, true)) {
11701 return ir_analyze_optional_wrap(ira, source_instr, value, wanted_type);
12502 return ir_analyze_optional_wrap(ira, source_instr, value, wanted_type, result_loc);
1170212503 } else {
1170312504 return ira->codegen->invalid_instruction;
1170412505 }
......@@ -11722,7 +12523,7 @@ static IrInstruction *ir_analyze_cast(IrAnalyze *ira, IrInstruction *source_inst
1172212523 wanted_child_type);
1172312524 if (type_is_invalid(cast1->value.type))
1172412525 return ira->codegen->invalid_instruction;
11725 return ir_analyze_optional_wrap(ira, source_instr, cast1, wanted_type);
12526 return ir_analyze_optional_wrap(ira, source_instr, cast1, wanted_type, result_loc);
1172612527 }
1172712528 }
1172812529 }
......@@ -11732,12 +12533,12 @@ static IrInstruction *ir_analyze_cast(IrAnalyze *ira, IrInstruction *source_inst
1173212533 if (types_match_const_cast_only(ira, wanted_type->data.error_union.payload_type, actual_type,
1173312534 source_node, false).id == ConstCastResultIdOk)
1173412535 {
11735 return ir_analyze_err_wrap_payload(ira, source_instr, value, wanted_type);
12536 return ir_analyze_err_wrap_payload(ira, source_instr, value, wanted_type, result_loc);
1173612537 } else if (actual_type->id == ZigTypeIdComptimeInt ||
1173712538 actual_type->id == ZigTypeIdComptimeFloat)
1173812539 {
1173912540 if (ir_num_lit_fits_in_other_type(ira, value, wanted_type->data.error_union.payload_type, true)) {
11740 return ir_analyze_err_wrap_payload(ira, source_instr, value, wanted_type);
12541 return ir_analyze_err_wrap_payload(ira, source_instr, value, wanted_type, result_loc);
1174112542 } else {
1174212543 return ira->codegen->invalid_instruction;
1174312544 }
......@@ -11755,11 +12556,11 @@ static IrInstruction *ir_analyze_cast(IrAnalyze *ira, IrInstruction *source_inst
1175512556 actual_type->id == ZigTypeIdComptimeInt ||
1175612557 actual_type->id == ZigTypeIdComptimeFloat)
1175712558 {
11758 IrInstruction *cast1 = ir_analyze_cast(ira, source_instr, wanted_type->data.error_union.payload_type, value);
12559 IrInstruction *cast1 = ir_analyze_cast(ira, source_instr, wanted_type->data.error_union.payload_type, value, nullptr);
1175912560 if (type_is_invalid(cast1->value.type))
1176012561 return ira->codegen->invalid_instruction;
1176112562
11762 IrInstruction *cast2 = ir_analyze_cast(ira, source_instr, wanted_type, cast1);
12563 IrInstruction *cast2 = ir_analyze_cast(ira, source_instr, wanted_type, cast1, result_loc);
1176312564 if (type_is_invalid(cast2->value.type))
1176412565 return ira->codegen->invalid_instruction;
1176512566
......@@ -11841,7 +12642,7 @@ static IrInstruction *ir_analyze_cast(IrAnalyze *ira, IrInstruction *source_inst
1184112642 types_match_const_cast_only(ira, ptr_type->data.pointer.child_type, actual_type->data.array.child_type,
1184212643 source_node, false).id == ConstCastResultIdOk)
1184312644 {
11844 return ir_analyze_array_to_slice(ira, source_instr, value, wanted_type);
12645 return ir_analyze_array_to_slice(ira, source_instr, value, wanted_type, result_loc);
1184512646 }
1184612647 }
1184712648
......@@ -11858,11 +12659,11 @@ static IrInstruction *ir_analyze_cast(IrAnalyze *ira, IrInstruction *source_inst
1185812659 types_match_const_cast_only(ira, ptr_type->data.pointer.child_type, actual_type->data.array.child_type,
1185912660 source_node, false).id == ConstCastResultIdOk)
1186012661 {
11861 IrInstruction *cast1 = ir_analyze_cast(ira, source_instr, wanted_type->data.maybe.child_type, value);
12662 IrInstruction *cast1 = ir_analyze_cast(ira, source_instr, wanted_type->data.maybe.child_type, value, nullptr);
1186212663 if (type_is_invalid(cast1->value.type))
1186312664 return ira->codegen->invalid_instruction;
1186412665
11865 IrInstruction *cast2 = ir_analyze_cast(ira, source_instr, wanted_type, cast1);
12666 IrInstruction *cast2 = ir_analyze_cast(ira, source_instr, wanted_type, cast1, result_loc);
1186612667 if (type_is_invalid(cast2->value.type))
1186712668 return ira->codegen->invalid_instruction;
1186812669
......@@ -11905,7 +12706,7 @@ static IrInstruction *ir_analyze_cast(IrAnalyze *ira, IrInstruction *source_inst
1190512706 array_type->data.array.child_type, source_node,
1190612707 !slice_ptr_type->data.pointer.is_const).id == ConstCastResultIdOk)
1190712708 {
11908 return ir_resolve_ptr_of_array_to_slice(ira, source_instr, value, wanted_type);
12709 return ir_resolve_ptr_of_array_to_slice(ira, source_instr, value, wanted_type, result_loc);
1190912710 }
1191012711 }
1191112712
......@@ -11936,11 +12737,11 @@ static IrInstruction *ir_analyze_cast(IrAnalyze *ira, IrInstruction *source_inst
1193612737 types_match_const_cast_only(ira, ptr_type->data.pointer.child_type, actual_type->data.array.child_type,
1193712738 source_node, false).id == ConstCastResultIdOk)
1193812739 {
11939 IrInstruction *cast1 = ir_analyze_cast(ira, source_instr, wanted_type->data.error_union.payload_type, value);
12740 IrInstruction *cast1 = ir_analyze_cast(ira, source_instr, wanted_type->data.error_union.payload_type, value, nullptr);
1194012741 if (type_is_invalid(cast1->value.type))
1194112742 return ira->codegen->invalid_instruction;
1194212743
11943 IrInstruction *cast2 = ir_analyze_cast(ira, source_instr, wanted_type, cast1);
12744 IrInstruction *cast2 = ir_analyze_cast(ira, source_instr, wanted_type, cast1, result_loc);
1194412745 if (type_is_invalid(cast2->value.type))
1194512746 return ira->codegen->invalid_instruction;
1194612747
......@@ -11952,7 +12753,7 @@ static IrInstruction *ir_analyze_cast(IrAnalyze *ira, IrInstruction *source_inst
1195212753 if (wanted_type->id == ZigTypeIdErrorUnion &&
1195312754 actual_type->id == ZigTypeIdErrorSet)
1195412755 {
11955 return ir_analyze_err_wrap_code(ira, source_instr, value, wanted_type);
12756 return ir_analyze_err_wrap_code(ira, source_instr, value, wanted_type, result_loc);
1195612757 }
1195712758
1195812759 // cast from typed number to integer or float literal.
......@@ -12076,7 +12877,7 @@ static IrInstruction *ir_analyze_cast(IrAnalyze *ira, IrInstruction *source_inst
1207612877 types_match_const_cast_only(ira, wanted_type->data.array.child_type,
1207712878 actual_type->data.vector.elem_type, source_node, false).id == ConstCastResultIdOk)
1207812879 {
12079 return ir_analyze_vector_to_array(ira, source_instr, value, wanted_type);
12880 return ir_analyze_vector_to_array(ira, source_instr, value, wanted_type, result_loc);
1208012881 }
1208112882
1208212883 // cast from [N]T to @Vector(N, T)
......@@ -12118,7 +12919,9 @@ static IrInstruction *ir_analyze_cast(IrAnalyze *ira, IrInstruction *source_inst
1211812919 return ira->codegen->invalid_instruction;
1211912920}
1212012921
12121static IrInstruction *ir_implicit_cast(IrAnalyze *ira, IrInstruction *value, ZigType *expected_type) {
12922static IrInstruction *ir_implicit_cast_with_result(IrAnalyze *ira, IrInstruction *value, ZigType *expected_type,
12923 ResultLoc *result_loc)
12924{
1212212925 assert(value);
1212312926 assert(value != ira->codegen->invalid_instruction);
1212412927 assert(!expected_type || !type_is_invalid(expected_type));
......@@ -12131,63 +12934,76 @@ static IrInstruction *ir_implicit_cast(IrAnalyze *ira, IrInstruction *value, Zig
1213112934 if (value->value.type->id == ZigTypeIdUnreachable)
1213212935 return value;
1213312936
12134 return ir_analyze_cast(ira, value, expected_type, value);
12937 return ir_analyze_cast(ira, value, expected_type, value, result_loc);
12938}
12939
12940static IrInstruction *ir_implicit_cast(IrAnalyze *ira, IrInstruction *value, ZigType *expected_type) {
12941 return ir_implicit_cast_with_result(ira, value, expected_type, nullptr);
1213512942}
1213612943
12137static IrInstruction *ir_get_deref(IrAnalyze *ira, IrInstruction *source_instruction, IrInstruction *ptr) {
12944static IrInstruction *ir_get_deref(IrAnalyze *ira, IrInstruction *source_instruction, IrInstruction *ptr,
12945 ResultLoc *result_loc)
12946{
1213812947 Error err;
1213912948 ZigType *type_entry = ptr->value.type;
12140 if (type_is_invalid(type_entry)) {
12949 if (type_is_invalid(type_entry))
1214112950 return ira->codegen->invalid_instruction;
12142 } else if (type_entry->id == ZigTypeIdPointer) {
12143 ZigType *child_type = type_entry->data.pointer.child_type;
12144 // if the child type has one possible value, the deref is comptime
12145 switch (type_has_one_possible_value(ira->codegen, child_type)) {
12146 case OnePossibleValueInvalid:
12147 return ira->codegen->invalid_instruction;
12148 case OnePossibleValueYes:
12149 return ir_const(ira, source_instruction, child_type);
12150 case OnePossibleValueNo:
12151 break;
12152 }
12153 if (instr_is_comptime(ptr)) {
12154 if (ptr->value.special == ConstValSpecialUndef) {
12155 ir_add_error(ira, ptr, buf_sprintf("attempt to dereference undefined value"));
12156 return ira->codegen->invalid_instruction;
12157 }
12158 if (ptr->value.data.x_ptr.mut == ConstPtrMutComptimeConst ||
12159 ptr->value.data.x_ptr.mut == ConstPtrMutComptimeVar)
12160 {
12161 ConstExprValue *pointee = const_ptr_pointee_unchecked(ira->codegen, &ptr->value);
12162 if (pointee->special != ConstValSpecialRuntime) {
12163 IrInstruction *result = ir_const(ira, source_instruction, child_type);
1216412951
12165 if ((err = ir_read_const_ptr(ira, ira->codegen, source_instruction->source_node, &result->value,
12166 &ptr->value)))
12167 {
12168 return ira->codegen->invalid_instruction;
12169 }
12170 result->value.type = child_type;
12171 return result;
12952 if (type_entry->id != ZigTypeIdPointer) {
12953 ir_add_error_node(ira, source_instruction->source_node,
12954 buf_sprintf("attempt to dereference non-pointer type '%s'",
12955 buf_ptr(&type_entry->name)));
12956 return ira->codegen->invalid_instruction;
12957 }
12958
12959 ZigType *child_type = type_entry->data.pointer.child_type;
12960 // if the child type has one possible value, the deref is comptime
12961 switch (type_has_one_possible_value(ira->codegen, child_type)) {
12962 case OnePossibleValueInvalid:
12963 return ira->codegen->invalid_instruction;
12964 case OnePossibleValueYes:
12965 return ir_const(ira, source_instruction, child_type);
12966 case OnePossibleValueNo:
12967 break;
12968 }
12969 if (instr_is_comptime(ptr)) {
12970 if (ptr->value.special == ConstValSpecialUndef) {
12971 ir_add_error(ira, ptr, buf_sprintf("attempt to dereference undefined value"));
12972 return ira->codegen->invalid_instruction;
12973 }
12974 if (ptr->value.data.x_ptr.mut != ConstPtrMutRuntimeVar) {
12975 ConstExprValue *pointee = const_ptr_pointee_unchecked(ira->codegen, &ptr->value);
12976 if (pointee->special != ConstValSpecialRuntime) {
12977 IrInstruction *result = ir_const(ira, source_instruction, child_type);
12978
12979 if ((err = ir_read_const_ptr(ira, ira->codegen, source_instruction->source_node, &result->value,
12980 &ptr->value)))
12981 {
12982 return ira->codegen->invalid_instruction;
1217212983 }
12984 result->value.type = child_type;
12985 return result;
1217312986 }
1217412987 }
12175 // if the instruction is a const ref instruction we can skip it
12176 if (ptr->id == IrInstructionIdRef) {
12177 IrInstructionRef *ref_inst = reinterpret_cast<IrInstructionRef *>(ptr);
12178 return ref_inst->value;
12179 }
12180 IrInstruction *result = ir_build_load_ptr_gen(ira, source_instruction, ptr, child_type);
12181 if (type_entry->data.pointer.host_int_bytes != 0 && handle_is_ptr(child_type)) {
12182 ir_add_alloca(ira, result, child_type);
12988 }
12989 // if the instruction is a const ref instruction we can skip it
12990 if (ptr->id == IrInstructionIdRef) {
12991 IrInstructionRef *ref_inst = reinterpret_cast<IrInstructionRef *>(ptr);
12992 return ref_inst->value;
12993 }
12994
12995 IrInstruction *result_loc_inst;
12996 if (type_entry->data.pointer.host_int_bytes != 0 && handle_is_ptr(child_type)) {
12997 if (result_loc == nullptr) result_loc = no_result_loc();
12998 result_loc_inst = ir_resolve_result(ira, source_instruction, result_loc, child_type, nullptr, true, false);
12999 if (type_is_invalid(result_loc_inst->value.type) || instr_is_unreachable(result_loc_inst)) {
13000 return result_loc_inst;
1218313001 }
12184 return result;
1218513002 } else {
12186 ir_add_error_node(ira, source_instruction->source_node,
12187 buf_sprintf("attempt to dereference non-pointer type '%s'",
12188 buf_ptr(&type_entry->name)));
12189 return ira->codegen->invalid_instruction;
13003 result_loc_inst = nullptr;
1219013004 }
13005
13006 return ir_build_load_ptr_gen(ira, source_instruction, ptr, child_type, result_loc_inst);
1219113007}
1219213008
1219313009static bool ir_resolve_align(IrAnalyze *ira, IrInstruction *value, uint32_t *out) {
......@@ -12401,6 +13217,14 @@ static IrInstruction *ir_analyze_instruction_return(IrAnalyze *ira, IrInstructio
1240113217 if (type_is_invalid(value->value.type))
1240213218 return ir_unreach_error(ira);
1240313219
13220 if (!instr_is_comptime(value) && handle_is_ptr(ira->explicit_return_type)) {
13221 // result location mechanism took care of it.
13222 IrInstruction *result = ir_build_return(&ira->new_irb, instruction->base.scope,
13223 instruction->base.source_node, nullptr);
13224 result->value.type = ira->codegen->builtin_types.entry_unreachable;
13225 return ir_finish_anal(ira, result);
13226 }
13227
1240413228 IrInstruction *casted_value = ir_implicit_cast(ira, value, ira->explicit_return_type);
1240513229 if (type_is_invalid(casted_value->value.type)) {
1240613230 AstNode *source_node = ira->explicit_return_type_source_node;
......@@ -12563,7 +13387,7 @@ static IrInstruction *ir_analyze_bin_op_cmp(IrAnalyze *ira, IrInstructionBinOp *
1256313387 } else {
1256413388 return is_non_null;
1256513389 }
12566 } else if (is_equality_cmp &&
13390 } else if (is_equality_cmp &&
1256713391 ((op1->value.type->id == ZigTypeIdNull && op2->value.type->id == ZigTypeIdPointer &&
1256813392 op2->value.type->data.pointer.ptr_len == PtrLenC) ||
1256913393 (op2->value.type->id == ZigTypeIdNull && op1->value.type->id == ZigTypeIdPointer &&
......@@ -13005,7 +13829,7 @@ static ErrorMsg *ir_eval_math_op_scalar(IrAnalyze *ira, IrInstruction *source_in
1300513829 }
1300613830 } else {
1300713831 float_div_trunc(out_val, op1_val, op2_val);
13008 ConstExprValue remainder;
13832 ConstExprValue remainder = {};
1300913833 float_rem(&remainder, op1_val, op2_val);
1301013834 if (float_cmp_zero(&remainder) != CmpEQ) {
1301113835 return ir_add_error(ira, source_instr, buf_sprintf("exact division had a remainder"));
......@@ -13380,8 +14204,8 @@ static IrInstruction *ir_analyze_bin_op_math(IrAnalyze *ira, IrInstructionBinOp
1338014204 // have a remainder function ambiguity problem
1338114205 ok = true;
1338214206 } else {
13383 ConstExprValue rem_result;
13384 ConstExprValue mod_result;
14207 ConstExprValue rem_result = {};
14208 ConstExprValue mod_result = {};
1338514209 float_rem(&rem_result, op1_val, op2_val);
1338614210 float_mod(&mod_result, op1_val, op2_val);
1338714211 ok = float_cmp(&rem_result, &mod_result) == CmpEQ;
......@@ -13604,10 +14428,12 @@ static IrInstruction *ir_analyze_array_cat(IrAnalyze *ira, IrInstructionBinOp *i
1360414428
1360514429 size_t next_index = 0;
1360614430 for (size_t i = op1_array_index; i < op1_array_end; i += 1, next_index += 1) {
13607 out_array_val->data.x_array.data.s_none.elements[next_index] = op1_array_val->data.x_array.data.s_none.elements[i];
14431 copy_const_val(&out_array_val->data.x_array.data.s_none.elements[next_index],
14432 &op1_array_val->data.x_array.data.s_none.elements[i], true);
1360814433 }
1360914434 for (size_t i = op2_array_index; i < op2_array_end; i += 1, next_index += 1) {
13610 out_array_val->data.x_array.data.s_none.elements[next_index] = op2_array_val->data.x_array.data.s_none.elements[i];
14435 copy_const_val(&out_array_val->data.x_array.data.s_none.elements[next_index],
14436 &op2_array_val->data.x_array.data.s_none.elements[i], true);
1361114437 }
1361214438 if (next_index < new_len) {
1361314439 ConstExprValue *null_byte = &out_array_val->data.x_array.data.s_none.elements[next_index];
......@@ -13668,7 +14494,8 @@ static IrInstruction *ir_analyze_array_mult(IrAnalyze *ira, IrInstructionBinOp *
1366814494 uint64_t i = 0;
1366914495 for (uint64_t x = 0; x < mult_amt; x += 1) {
1367014496 for (uint64_t y = 0; y < old_array_len; y += 1) {
13671 out_val->data.x_array.data.s_none.elements[i] = array_val->data.x_array.data.s_none.elements[y];
14497 copy_const_val(&out_val->data.x_array.data.s_none.elements[i],
14498 &array_val->data.x_array.data.s_none.elements[y], true);
1367214499 i += 1;
1367314500 }
1367414501 }
......@@ -13765,12 +14592,6 @@ static IrInstruction *ir_analyze_instruction_decl_var(IrAnalyze *ira,
1376514592 Error err;
1376614593 ZigVar *var = decl_var_instruction->var;
1376714594
13768 IrInstruction *init_value = decl_var_instruction->init_value->child;
13769 if (type_is_invalid(init_value->value.type)) {
13770 var->var_type = ira->codegen->builtin_types.entry_invalid;
13771 return ira->codegen->invalid_instruction;
13772 }
13773
1377414595 ZigType *explicit_type = nullptr;
1377514596 IrInstruction *var_type = nullptr;
1377614597 if (decl_var_instruction->var_type != nullptr) {
......@@ -13785,18 +14606,40 @@ static IrInstruction *ir_analyze_instruction_decl_var(IrAnalyze *ira,
1378514606
1378614607 AstNode *source_node = decl_var_instruction->base.source_node;
1378714608
13788 IrInstruction *casted_init_value = ir_implicit_cast(ira, init_value, explicit_type);
1378914609 bool is_comptime_var = ir_get_var_is_comptime(var);
1379014610
1379114611 bool var_class_requires_const = false;
1379214612
13793 ZigType *result_type = casted_init_value->value.type;
14613 IrInstruction *var_ptr = decl_var_instruction->ptr->child;
14614 // if this is null, a compiler error happened and did not initialize the variable.
14615 // if there are no compile errors there may be a missing ir_expr_wrap in pass1 IR generation.
14616 if (var_ptr == nullptr || type_is_invalid(var_ptr->value.type)) {
14617 ir_assert(var_ptr != nullptr || ira->codegen->errors.length != 0, &decl_var_instruction->base);
14618 var->var_type = ira->codegen->builtin_types.entry_invalid;
14619 return ira->codegen->invalid_instruction;
14620 }
14621
14622 // The ir_build_var_decl_src call is supposed to pass a pointer to the allocation, not an initialization value.
14623 ir_assert(var_ptr->value.type->id == ZigTypeIdPointer, &decl_var_instruction->base);
14624
14625 ZigType *result_type = var_ptr->value.type->data.pointer.child_type;
1379414626 if (type_is_invalid(result_type)) {
1379514627 result_type = ira->codegen->builtin_types.entry_invalid;
1379614628 } else if (result_type->id == ZigTypeIdUnreachable || result_type->id == ZigTypeIdOpaque) {
13797 ir_add_error_node(ira, source_node,
13798 buf_sprintf("variable of type '%s' not allowed", buf_ptr(&result_type->name)));
13799 result_type = ira->codegen->builtin_types.entry_invalid;
14629 zig_unreachable();
14630 }
14631
14632 ConstExprValue *init_val = nullptr;
14633 if (instr_is_comptime(var_ptr) && var_ptr->value.data.x_ptr.mut != ConstPtrMutRuntimeVar) {
14634 init_val = const_ptr_pointee(ira, ira->codegen, &var_ptr->value, decl_var_instruction->base.source_node);
14635 if (is_comptime_var) {
14636 if (var->gen_is_const) {
14637 var->const_value = init_val;
14638 } else {
14639 var->const_value = create_const_vals(1);
14640 copy_const_val(var->const_value, init_val, false);
14641 }
14642 }
1380014643 }
1380114644
1380214645 switch (type_requires_comptime(ira->codegen, result_type)) {
......@@ -13813,18 +14656,20 @@ static IrInstruction *ir_analyze_instruction_decl_var(IrAnalyze *ira,
1381314656 }
1381414657 break;
1381514658 case ReqCompTimeNo:
13816 if (casted_init_value->value.special == ConstValSpecialStatic &&
13817 casted_init_value->value.type->id == ZigTypeIdFn &&
13818 casted_init_value->value.data.x_ptr.special != ConstPtrSpecialHardCodedAddr &&
13819 casted_init_value->value.data.x_ptr.data.fn.fn_entry->fn_inline == FnInlineAlways)
13820 {
13821 var_class_requires_const = true;
13822 if (!var->src_is_const && !is_comptime_var) {
13823 ErrorMsg *msg = ir_add_error_node(ira, source_node,
13824 buf_sprintf("functions marked inline must be stored in const or comptime var"));
13825 AstNode *proto_node = casted_init_value->value.data.x_ptr.data.fn.fn_entry->proto_node;
13826 add_error_note(ira->codegen, msg, proto_node, buf_sprintf("declared here"));
13827 result_type = ira->codegen->builtin_types.entry_invalid;
14659 if (init_val != nullptr) {
14660 if (init_val->special == ConstValSpecialStatic &&
14661 init_val->type->id == ZigTypeIdFn &&
14662 init_val->data.x_ptr.special != ConstPtrSpecialHardCodedAddr &&
14663 init_val->data.x_ptr.data.fn.fn_entry->fn_inline == FnInlineAlways)
14664 {
14665 var_class_requires_const = true;
14666 if (!var->src_is_const && !is_comptime_var) {
14667 ErrorMsg *msg = ir_add_error_node(ira, source_node,
14668 buf_sprintf("functions marked inline must be stored in const or comptime var"));
14669 AstNode *proto_node = init_val->data.x_ptr.data.fn.fn_entry->proto_node;
14670 add_error_note(ira->codegen, msg, proto_node, buf_sprintf("declared here"));
14671 result_type = ira->codegen->builtin_types.entry_invalid;
14672 }
1382814673 }
1382914674 }
1383014675 break;
......@@ -13871,11 +14716,29 @@ static IrInstruction *ir_analyze_instruction_decl_var(IrAnalyze *ira,
1387114716 }
1387214717 }
1387314718
13874 if (casted_init_value->value.special != ConstValSpecialRuntime) {
13875 if (var->mem_slot_index != SIZE_MAX) {
14719 if (init_val != nullptr && init_val->special != ConstValSpecialRuntime) {
14720 // Resolve ConstPtrMutInfer
14721 if (var->gen_is_const) {
14722 var_ptr->value.data.x_ptr.mut = ConstPtrMutComptimeConst;
14723 } else if (is_comptime_var) {
14724 var_ptr->value.data.x_ptr.mut = ConstPtrMutComptimeVar;
14725 } else {
14726 // we need a runtime ptr but we have a comptime val.
14727 // since it's a comptime val there are no instructions for it.
14728 // we memcpy the init value here
14729 IrInstruction *deref = ir_get_deref(ira, var_ptr, var_ptr, nullptr);
14730 // If this assertion trips, something is wrong with the IR instructions, because
14731 // we expected the above deref to return a constant value, but it created a runtime
14732 // instruction.
14733 assert(deref->value.special != ConstValSpecialRuntime);
14734 var_ptr->value.special = ConstValSpecialRuntime;
14735 ir_analyze_store_ptr(ira, var_ptr, var_ptr, deref);
14736 }
14737
14738 if (var_ptr->value.special == ConstValSpecialStatic && var->mem_slot_index != SIZE_MAX) {
1387614739 assert(var->mem_slot_index < ira->exec_context.mem_slot_list.length);
1387714740 ConstExprValue *mem_slot = ira->exec_context.mem_slot_list.at(var->mem_slot_index);
13878 copy_const_val(mem_slot, &casted_init_value->value, !is_comptime_var || var->gen_is_const);
14741 copy_const_val(mem_slot, init_val, !is_comptime_var || var->gen_is_const);
1387914742
1388014743 if (is_comptime_var || (var_class_requires_const && var->gen_is_const)) {
1388114744 return ir_const_void(ira, &decl_var_instruction->base);
......@@ -13892,7 +14755,7 @@ static IrInstruction *ir_analyze_instruction_decl_var(IrAnalyze *ira,
1389214755 if (fn_entry)
1389314756 fn_entry->variable_list.append(var);
1389414757
13895 return ir_build_var_decl_gen(ira, &decl_var_instruction->base, var, casted_init_value);
14758 return ir_build_var_decl_gen(ira, &decl_var_instruction->base, var, var_ptr);
1389614759}
1389714760
1389814761static IrInstruction *ir_analyze_instruction_export(IrAnalyze *ira, IrInstructionExport *instruction) {
......@@ -14186,7 +15049,7 @@ IrInstruction *ir_get_implicit_allocator(IrAnalyze *ira, IrInstruction *source_i
1418615049 ZigVar *coro_allocator_var = ira->old_irb.exec->coro_allocator_var;
1418715050 assert(coro_allocator_var != nullptr);
1418815051 IrInstruction *var_ptr_inst = ir_get_var_ptr(ira, source_instr, coro_allocator_var);
14189 IrInstruction *result = ir_get_deref(ira, source_instr, var_ptr_inst);
15052 IrInstruction *result = ir_get_deref(ira, source_instr, var_ptr_inst, nullptr);
1419015053 assert(result->value.type != nullptr);
1419115054 return result;
1419215055 }
......@@ -14194,7 +15057,436 @@ IrInstruction *ir_get_implicit_allocator(IrAnalyze *ira, IrInstruction *source_i
1419415057 zig_unreachable();
1419515058}
1419615059
14197static IrInstruction *ir_analyze_async_call(IrAnalyze *ira, IrInstructionCall *call_instruction, ZigFn *fn_entry,
15060static IrInstruction *ir_analyze_alloca(IrAnalyze *ira, IrInstruction *source_inst, ZigType *var_type,
15061 uint32_t align, const char *name_hint, bool force_comptime)
15062{
15063 Error err;
15064
15065 ConstExprValue *pointee = create_const_vals(1);
15066 pointee->special = ConstValSpecialUndef;
15067
15068 IrInstructionAllocaGen *result = ir_create_alloca_gen(ira, source_inst, align, name_hint);
15069 result->base.value.special = ConstValSpecialStatic;
15070 result->base.value.data.x_ptr.special = ConstPtrSpecialRef;
15071 result->base.value.data.x_ptr.mut = force_comptime ? ConstPtrMutComptimeVar : ConstPtrMutInfer;
15072 result->base.value.data.x_ptr.data.ref.pointee = pointee;
15073
15074 if ((err = type_resolve(ira->codegen, var_type, ResolveStatusZeroBitsKnown)))
15075 return ira->codegen->invalid_instruction;
15076 assert(result->base.value.data.x_ptr.special != ConstPtrSpecialInvalid);
15077
15078 pointee->type = var_type;
15079 result->base.value.type = get_pointer_to_type_extra(ira->codegen, var_type, false, false,
15080 PtrLenSingle, align, 0, 0, false);
15081
15082 ZigFn *fn_entry = exec_fn_entry(ira->new_irb.exec);
15083 if (fn_entry != nullptr) {
15084 fn_entry->alloca_gen_list.append(result);
15085 }
15086 result->base.is_gen = true;
15087 return &result->base;
15088}
15089
15090static ZigType *ir_result_loc_expected_type(IrAnalyze *ira, IrInstruction *suspend_source_instr,
15091 ResultLoc *result_loc)
15092{
15093 switch (result_loc->id) {
15094 case ResultLocIdInvalid:
15095 case ResultLocIdPeerParent:
15096 zig_unreachable();
15097 case ResultLocIdNone:
15098 case ResultLocIdVar:
15099 case ResultLocIdBitCast:
15100 return nullptr;
15101 case ResultLocIdInstruction:
15102 return result_loc->source_instruction->child->value.type;
15103 case ResultLocIdReturn:
15104 return ira->explicit_return_type;
15105 case ResultLocIdPeer:
15106 return reinterpret_cast<ResultLocPeer*>(result_loc)->parent->resolved_type;
15107 }
15108 zig_unreachable();
15109}
15110
15111static bool type_can_bit_cast(ZigType *t) {
15112 switch (t->id) {
15113 case ZigTypeIdInvalid:
15114 zig_unreachable();
15115 case ZigTypeIdMetaType:
15116 case ZigTypeIdOpaque:
15117 case ZigTypeIdBoundFn:
15118 case ZigTypeIdArgTuple:
15119 case ZigTypeIdUnreachable:
15120 case ZigTypeIdComptimeFloat:
15121 case ZigTypeIdComptimeInt:
15122 case ZigTypeIdEnumLiteral:
15123 case ZigTypeIdUndefined:
15124 case ZigTypeIdNull:
15125 case ZigTypeIdPointer:
15126 return false;
15127 default:
15128 // TODO list these types out explicitly, there are probably some other invalid ones here
15129 return true;
15130 }
15131}
15132
15133static void set_up_result_loc_for_inferred_comptime(IrInstruction *ptr) {
15134 ConstExprValue *undef_child = create_const_vals(1);
15135 undef_child->type = ptr->value.type->data.pointer.child_type;
15136 undef_child->special = ConstValSpecialUndef;
15137 ptr->value.special = ConstValSpecialStatic;
15138 ptr->value.data.x_ptr.mut = ConstPtrMutInfer;
15139 ptr->value.data.x_ptr.special = ConstPtrSpecialRef;
15140 ptr->value.data.x_ptr.data.ref.pointee = undef_child;
15141}
15142
15143// when calling this function, at the callsite must check for result type noreturn and propagate it up
15144static IrInstruction *ir_resolve_result_raw(IrAnalyze *ira, IrInstruction *suspend_source_instr,
15145 ResultLoc *result_loc, ZigType *value_type, IrInstruction *value, bool force_runtime, bool non_null_comptime)
15146{
15147 Error err;
15148 if (result_loc->resolved_loc != nullptr) {
15149 // allow to redo the result location if the value is known and comptime and the previous one isn't
15150 if (value == nullptr || !instr_is_comptime(value) || instr_is_comptime(result_loc->resolved_loc)) {
15151 return result_loc->resolved_loc;
15152 }
15153 }
15154 result_loc->gen_instruction = value;
15155 result_loc->implicit_elem_type = value_type;
15156 switch (result_loc->id) {
15157 case ResultLocIdInvalid:
15158 case ResultLocIdPeerParent:
15159 zig_unreachable();
15160 case ResultLocIdNone: {
15161 if (value != nullptr) {
15162 return nullptr;
15163 }
15164 // need to return a result location and don't have one. use a stack allocation
15165 IrInstructionAllocaGen *alloca_gen = ir_create_alloca_gen(ira, suspend_source_instr, 0, "");
15166 if ((err = type_resolve(ira->codegen, value_type, ResolveStatusZeroBitsKnown)))
15167 return ira->codegen->invalid_instruction;
15168 alloca_gen->base.value.type = get_pointer_to_type_extra(ira->codegen, value_type, false, false,
15169 PtrLenSingle, 0, 0, 0, false);
15170 set_up_result_loc_for_inferred_comptime(&alloca_gen->base);
15171 ZigFn *fn_entry = exec_fn_entry(ira->new_irb.exec);
15172 if (fn_entry != nullptr) {
15173 fn_entry->alloca_gen_list.append(alloca_gen);
15174 }
15175 result_loc->written = true;
15176 result_loc->resolved_loc = &alloca_gen->base;
15177 return result_loc->resolved_loc;
15178 }
15179 case ResultLocIdVar: {
15180 ResultLocVar *result_loc_var = reinterpret_cast<ResultLocVar *>(result_loc);
15181 assert(result_loc->source_instruction->id == IrInstructionIdAllocaSrc);
15182
15183 if (value_type->id == ZigTypeIdUnreachable || value_type->id == ZigTypeIdOpaque) {
15184 ir_add_error(ira, result_loc->source_instruction,
15185 buf_sprintf("variable of type '%s' not allowed", buf_ptr(&value_type->name)));
15186 return ira->codegen->invalid_instruction;
15187 }
15188
15189 IrInstructionAllocaSrc *alloca_src =
15190 reinterpret_cast<IrInstructionAllocaSrc *>(result_loc->source_instruction);
15191 bool force_comptime;
15192 if (!ir_resolve_comptime(ira, alloca_src->is_comptime->child, &force_comptime))
15193 return ira->codegen->invalid_instruction;
15194 bool is_comptime = force_comptime || (value != nullptr &&
15195 value->value.special != ConstValSpecialRuntime && result_loc_var->var->gen_is_const);
15196
15197 if (alloca_src->base.child == nullptr || is_comptime) {
15198 uint32_t align = 0;
15199 if (alloca_src->align != nullptr && !ir_resolve_align(ira, alloca_src->align->child, &align)) {
15200 return ira->codegen->invalid_instruction;
15201 }
15202 IrInstruction *alloca_gen;
15203 if (is_comptime && value != nullptr) {
15204 if (align > value->value.global_refs->align) {
15205 value->value.global_refs->align = align;
15206 }
15207 alloca_gen = ir_get_ref(ira, result_loc->source_instruction, value, true, false);
15208 } else {
15209 alloca_gen = ir_analyze_alloca(ira, result_loc->source_instruction, value_type, align,
15210 alloca_src->name_hint, force_comptime);
15211 }
15212 if (alloca_src->base.child != nullptr) {
15213 alloca_src->base.child->ref_count = 0;
15214 }
15215 alloca_src->base.child = alloca_gen;
15216 }
15217 result_loc->written = true;
15218 result_loc->resolved_loc = is_comptime ? nullptr : alloca_src->base.child;
15219 return result_loc->resolved_loc;
15220 }
15221 case ResultLocIdInstruction: {
15222 result_loc->written = true;
15223 result_loc->resolved_loc = result_loc->source_instruction->child;
15224 return result_loc->resolved_loc;
15225 }
15226 case ResultLocIdReturn: {
15227 if (!non_null_comptime) {
15228 bool is_comptime = value != nullptr && value->value.special != ConstValSpecialRuntime;
15229 if (is_comptime)
15230 return nullptr;
15231 }
15232 if ((err = type_resolve(ira->codegen, ira->explicit_return_type, ResolveStatusZeroBitsKnown))) {
15233 return ira->codegen->invalid_instruction;
15234 }
15235 if (!type_has_bits(ira->explicit_return_type) || !handle_is_ptr(ira->explicit_return_type))
15236 return nullptr;
15237
15238 ZigType *ptr_return_type = get_pointer_to_type(ira->codegen, ira->explicit_return_type, false);
15239 result_loc->written = true;
15240 result_loc->resolved_loc = ir_build_return_ptr(ira, result_loc->source_instruction, ptr_return_type);
15241 if (ir_should_inline(ira->old_irb.exec, result_loc->source_instruction->scope)) {
15242 set_up_result_loc_for_inferred_comptime(result_loc->resolved_loc);
15243 }
15244 return result_loc->resolved_loc;
15245 }
15246 case ResultLocIdPeer: {
15247 ResultLocPeer *result_peer = reinterpret_cast<ResultLocPeer *>(result_loc);
15248 ResultLocPeerParent *peer_parent = result_peer->parent;
15249
15250 if (peer_parent->peers.length == 1) {
15251 IrInstruction *parent_result_loc = ir_resolve_result(ira, suspend_source_instr, peer_parent->parent,
15252 value_type, value, force_runtime, non_null_comptime);
15253 result_peer->suspend_pos.basic_block_index = SIZE_MAX;
15254 result_peer->suspend_pos.instruction_index = SIZE_MAX;
15255 if (parent_result_loc == nullptr || type_is_invalid(parent_result_loc->value.type) ||
15256 parent_result_loc->value.type->id == ZigTypeIdUnreachable)
15257 {
15258 return parent_result_loc;
15259 }
15260 result_loc->written = true;
15261 result_loc->resolved_loc = parent_result_loc;
15262 return result_loc->resolved_loc;
15263 }
15264
15265 bool is_comptime;
15266 if (!ir_resolve_comptime(ira, peer_parent->is_comptime->child, &is_comptime))
15267 return ira->codegen->invalid_instruction;
15268 peer_parent->skipped = is_comptime;
15269 if (peer_parent->skipped) {
15270 if (non_null_comptime) {
15271 return ir_resolve_result(ira, suspend_source_instr, peer_parent->parent,
15272 value_type, value, force_runtime, non_null_comptime);
15273 }
15274 return nullptr;
15275 }
15276
15277 if (peer_parent->resolved_type == nullptr) {
15278 if (peer_parent->end_bb->suspend_instruction_ref == nullptr) {
15279 peer_parent->end_bb->suspend_instruction_ref = suspend_source_instr;
15280 }
15281 IrInstruction *unreach_inst = ira_suspend(ira, suspend_source_instr, result_peer->next_bb,
15282 &result_peer->suspend_pos);
15283 if (result_peer->next_bb == nullptr) {
15284 ir_start_next_bb(ira);
15285 }
15286 return unreach_inst;
15287 }
15288
15289 IrInstruction *parent_result_loc = ir_resolve_result(ira, suspend_source_instr, peer_parent->parent,
15290 peer_parent->resolved_type, nullptr, force_runtime, non_null_comptime);
15291 if (parent_result_loc == nullptr || type_is_invalid(parent_result_loc->value.type) ||
15292 parent_result_loc->value.type->id == ZigTypeIdUnreachable)
15293 {
15294 return parent_result_loc;
15295 }
15296 // because is_comptime is false, we mark this a runtime pointer
15297 parent_result_loc->value.special = ConstValSpecialRuntime;
15298 result_loc->written = true;
15299 result_loc->resolved_loc = parent_result_loc;
15300 return result_loc->resolved_loc;
15301 }
15302 case ResultLocIdBitCast: {
15303 ResultLocBitCast *result_bit_cast = reinterpret_cast<ResultLocBitCast *>(result_loc);
15304 ZigType *dest_type = ir_resolve_type(ira, result_bit_cast->base.source_instruction->child);
15305 if (type_is_invalid(dest_type))
15306 return ira->codegen->invalid_instruction;
15307
15308 if (get_codegen_ptr_type(dest_type) != nullptr) {
15309 ir_add_error(ira, result_loc->source_instruction,
15310 buf_sprintf("unable to @bitCast to pointer type '%s'", buf_ptr(&dest_type->name)));
15311 return ira->codegen->invalid_instruction;
15312 }
15313
15314 if (!type_can_bit_cast(dest_type)) {
15315 ir_add_error(ira, result_loc->source_instruction,
15316 buf_sprintf("unable to @bitCast to type '%s'", buf_ptr(&dest_type->name)));
15317 return ira->codegen->invalid_instruction;
15318 }
15319
15320 if (get_codegen_ptr_type(value_type) != nullptr) {
15321 ir_add_error(ira, suspend_source_instr,
15322 buf_sprintf("unable to @bitCast from pointer type '%s'", buf_ptr(&value_type->name)));
15323 return ira->codegen->invalid_instruction;
15324 }
15325
15326 if (!type_can_bit_cast(value_type)) {
15327 ir_add_error(ira, suspend_source_instr,
15328 buf_sprintf("unable to @bitCast from type '%s'", buf_ptr(&value_type->name)));
15329 return ira->codegen->invalid_instruction;
15330 }
15331
15332 IrInstruction *bitcasted_value;
15333 if (value != nullptr) {
15334 bitcasted_value = ir_analyze_bit_cast(ira, result_loc->source_instruction, value, dest_type);
15335 } else {
15336 bitcasted_value = nullptr;
15337 }
15338
15339 IrInstruction *parent_result_loc = ir_resolve_result(ira, suspend_source_instr, result_bit_cast->parent,
15340 dest_type, bitcasted_value, force_runtime, non_null_comptime);
15341 if (parent_result_loc == nullptr || type_is_invalid(parent_result_loc->value.type) ||
15342 parent_result_loc->value.type->id == ZigTypeIdUnreachable)
15343 {
15344 return parent_result_loc;
15345 }
15346 ZigType *parent_ptr_type = parent_result_loc->value.type;
15347 assert(parent_ptr_type->id == ZigTypeIdPointer);
15348 if ((err = type_resolve(ira->codegen, parent_ptr_type->data.pointer.child_type,
15349 ResolveStatusAlignmentKnown)))
15350 {
15351 return ira->codegen->invalid_instruction;
15352 }
15353 uint64_t parent_ptr_align = get_ptr_align(ira->codegen, parent_ptr_type);
15354 ZigType *ptr_type = get_pointer_to_type_extra(ira->codegen, value_type,
15355 parent_ptr_type->data.pointer.is_const, parent_ptr_type->data.pointer.is_volatile, PtrLenSingle,
15356 parent_ptr_align, 0, 0, parent_ptr_type->data.pointer.allow_zero);
15357
15358 result_loc->written = true;
15359 result_loc->resolved_loc = ir_analyze_ptr_cast(ira, suspend_source_instr, parent_result_loc,
15360 ptr_type, result_bit_cast->base.source_instruction, false);
15361 return result_loc->resolved_loc;
15362 }
15363 }
15364 zig_unreachable();
15365}
15366
15367static IrInstruction *ir_resolve_result(IrAnalyze *ira, IrInstruction *suspend_source_instr,
15368 ResultLoc *result_loc_pass1, ZigType *value_type, IrInstruction *value, bool force_runtime,
15369 bool non_null_comptime)
15370{
15371 IrInstruction *result_loc = ir_resolve_result_raw(ira, suspend_source_instr, result_loc_pass1, value_type,
15372 value, force_runtime, non_null_comptime);
15373 if (result_loc == nullptr || (instr_is_unreachable(result_loc) || type_is_invalid(result_loc->value.type)))
15374 return result_loc;
15375
15376 if ((force_runtime || (value != nullptr && !instr_is_comptime(value))) &&
15377 result_loc_pass1->written && result_loc->value.data.x_ptr.mut == ConstPtrMutInfer)
15378 {
15379 result_loc->value.special = ConstValSpecialRuntime;
15380 }
15381
15382 ir_assert(result_loc->value.type->id == ZigTypeIdPointer, suspend_source_instr);
15383 ZigType *actual_elem_type = result_loc->value.type->data.pointer.child_type;
15384 if (actual_elem_type->id == ZigTypeIdOptional && value_type->id != ZigTypeIdOptional &&
15385 value_type->id != ZigTypeIdNull)
15386 {
15387 return ir_analyze_unwrap_optional_payload(ira, suspend_source_instr, result_loc, false, true);
15388 } else if (actual_elem_type->id == ZigTypeIdErrorUnion && value_type->id != ZigTypeIdErrorUnion) {
15389 if (value_type->id == ZigTypeIdErrorSet) {
15390 return ir_analyze_unwrap_err_code(ira, suspend_source_instr, result_loc, true);
15391 } else {
15392 IrInstruction *unwrapped_err_ptr = ir_analyze_unwrap_error_payload(ira, suspend_source_instr,
15393 result_loc, false, true);
15394 ZigType *actual_payload_type = actual_elem_type->data.error_union.payload_type;
15395 if (actual_payload_type->id == ZigTypeIdOptional && value_type->id != ZigTypeIdOptional) {
15396 return ir_analyze_unwrap_optional_payload(ira, suspend_source_instr, unwrapped_err_ptr, false, true);
15397 } else {
15398 return unwrapped_err_ptr;
15399 }
15400 }
15401 } else if (is_slice(actual_elem_type) && value_type->id == ZigTypeIdArray) {
15402 // need to allow EndExpr to do the implicit cast from array to slice
15403 result_loc_pass1->written = false;
15404 }
15405 return result_loc;
15406}
15407
15408static IrInstruction *ir_analyze_instruction_implicit_cast(IrAnalyze *ira, IrInstructionImplicitCast *instruction) {
15409 ZigType *dest_type = ir_resolve_type(ira, instruction->dest_type->child);
15410 if (type_is_invalid(dest_type))
15411 return ira->codegen->invalid_instruction;
15412
15413 IrInstruction *target = instruction->target->child;
15414 if (type_is_invalid(target->value.type))
15415 return ira->codegen->invalid_instruction;
15416
15417 return ir_implicit_cast_with_result(ira, target, dest_type, instruction->result_loc);
15418}
15419
15420static IrInstruction *ir_analyze_instruction_resolve_result(IrAnalyze *ira, IrInstructionResolveResult *instruction) {
15421 ZigType *implicit_elem_type = ir_resolve_type(ira, instruction->ty->child);
15422 if (type_is_invalid(implicit_elem_type))
15423 return ira->codegen->invalid_instruction;
15424 IrInstruction *result_loc = ir_resolve_result(ira, &instruction->base, instruction->result_loc,
15425 implicit_elem_type, nullptr, false, true);
15426 if (result_loc != nullptr)
15427 return result_loc;
15428
15429 ZigFn *fn = exec_fn_entry(ira->new_irb.exec);
15430 if (fn != nullptr && fn->type_entry->data.fn.fn_type_id.cc == CallingConventionAsync &&
15431 instruction->result_loc->id == ResultLocIdReturn)
15432 {
15433 result_loc = ir_resolve_result(ira, &instruction->base, no_result_loc(),
15434 implicit_elem_type, nullptr, false, true);
15435 if (result_loc != nullptr &&
15436 (type_is_invalid(result_loc->value.type) || instr_is_unreachable(result_loc)))
15437 {
15438 return result_loc;
15439 }
15440 result_loc->value.special = ConstValSpecialRuntime;
15441 return result_loc;
15442 }
15443
15444 IrInstruction *result = ir_const(ira, &instruction->base, implicit_elem_type);
15445 result->value.special = ConstValSpecialUndef;
15446 IrInstruction *ptr = ir_get_ref(ira, &instruction->base, result, false, false);
15447 ptr->value.data.x_ptr.mut = ConstPtrMutComptimeVar;
15448 return ptr;
15449}
15450
15451static void ir_reset_result(ResultLoc *result_loc) {
15452 result_loc->written = false;
15453 result_loc->resolved_loc = nullptr;
15454 result_loc->gen_instruction = nullptr;
15455 result_loc->implicit_elem_type = nullptr;
15456 switch (result_loc->id) {
15457 case ResultLocIdInvalid:
15458 zig_unreachable();
15459 case ResultLocIdPeerParent: {
15460 ResultLocPeerParent *peer_parent = reinterpret_cast<ResultLocPeerParent *>(result_loc);
15461 peer_parent->skipped = false;
15462 peer_parent->done_resuming = false;
15463 peer_parent->resolved_type = nullptr;
15464 for (size_t i = 0; i < peer_parent->peers.length; i += 1) {
15465 ir_reset_result(&peer_parent->peers.at(i)->base);
15466 }
15467 break;
15468 }
15469 case ResultLocIdVar: {
15470 IrInstructionAllocaSrc *alloca_src =
15471 reinterpret_cast<IrInstructionAllocaSrc *>(result_loc->source_instruction);
15472 alloca_src->base.child = nullptr;
15473 break;
15474 }
15475 case ResultLocIdPeer:
15476 case ResultLocIdNone:
15477 case ResultLocIdReturn:
15478 case ResultLocIdInstruction:
15479 case ResultLocIdBitCast:
15480 break;
15481 }
15482}
15483
15484static IrInstruction *ir_analyze_instruction_reset_result(IrAnalyze *ira, IrInstructionResetResult *instruction) {
15485 ir_reset_result(instruction->result_loc);
15486 return ir_const_void(ira, &instruction->base);
15487}
15488
15489static IrInstruction *ir_analyze_async_call(IrAnalyze *ira, IrInstructionCallSrc *call_instruction, ZigFn *fn_entry,
1419815490 ZigType *fn_type, IrInstruction *fn_ref, IrInstruction **casted_args, size_t arg_count,
1419915491 IrInstruction *async_allocator_inst)
1420015492{
......@@ -14202,7 +15494,7 @@ static IrInstruction *ir_analyze_async_call(IrAnalyze *ira, IrInstructionCall *c
1420215494 ir_assert(async_allocator_inst->value.type->id == ZigTypeIdPointer, &call_instruction->base);
1420315495 ZigType *container_type = async_allocator_inst->value.type->data.pointer.child_type;
1420415496 IrInstruction *field_ptr_inst = ir_analyze_container_field_ptr(ira, realloc_field_name, &call_instruction->base,
14205 async_allocator_inst, container_type);
15497 async_allocator_inst, container_type, false);
1420615498 if (type_is_invalid(field_ptr_inst->value.type)) {
1420715499 return ira->codegen->invalid_instruction;
1420815500 }
......@@ -14227,10 +15519,15 @@ static IrInstruction *ir_analyze_async_call(IrAnalyze *ira, IrInstructionCall *c
1422715519 ZigType *promise_type = get_promise_type(ira->codegen, return_type);
1422815520 ZigType *async_return_type = get_error_union_type(ira->codegen, alloc_fn_error_set_type, promise_type);
1422915521
14230 IrInstruction *result = ir_build_call(&ira->new_irb, call_instruction->base.scope, call_instruction->base.source_node,
14231 fn_entry, fn_ref, arg_count, casted_args, false, FnInlineAuto, true, async_allocator_inst, nullptr);
14232 result->value.type = async_return_type;
14233 return result;
15522 IrInstruction *result_loc = ir_resolve_result(ira, &call_instruction->base, no_result_loc(),
15523 async_return_type, nullptr, true, true);
15524 if (type_is_invalid(result_loc->value.type) || instr_is_unreachable(result_loc)) {
15525 return result_loc;
15526 }
15527
15528 return ir_build_call_gen(ira, &call_instruction->base, fn_entry, fn_ref, arg_count,
15529 casted_args, FnInlineAuto, true, async_allocator_inst, nullptr, result_loc,
15530 async_return_type);
1423415531}
1423515532
1423615533static bool ir_analyze_fn_call_inline_arg(IrAnalyze *ira, AstNode *fn_proto_node,
......@@ -14253,7 +15550,7 @@ static bool ir_analyze_fn_call_inline_arg(IrAnalyze *ira, AstNode *fn_proto_node
1425315550 casted_arg = arg;
1425415551 }
1425515552
14256 ConstExprValue *arg_val = ir_resolve_const(ira, casted_arg, UndefBad);
15553 ConstExprValue *arg_val = ir_resolve_const(ira, casted_arg, UndefOk);
1425715554 if (!arg_val)
1425815555 return false;
1425915556
......@@ -14309,7 +15606,7 @@ static bool ir_analyze_fn_call_generic_arg(IrAnalyze *ira, AstNode *fn_proto_nod
1430915606 arg_val = create_const_runtime(casted_arg->value.type);
1431015607 }
1431115608 if (arg_part_of_generic_id) {
14312 generic_id->params[generic_id->param_count] = *arg_val;
15609 copy_const_val(&generic_id->params[generic_id->param_count], arg_val, true);
1431315610 generic_id->param_count += 1;
1431415611 }
1431515612
......@@ -14480,7 +15777,9 @@ static IrInstruction *ir_analyze_store_ptr(IrAnalyze *ira, IrInstruction *source
1448015777 ir_add_error(ira, source_instr, buf_sprintf("cannot assign to constant"));
1448115778 return ira->codegen->invalid_instruction;
1448215779 }
14483 if (ptr->value.data.x_ptr.mut == ConstPtrMutComptimeVar) {
15780 if (ptr->value.data.x_ptr.mut == ConstPtrMutComptimeVar ||
15781 ptr->value.data.x_ptr.mut == ConstPtrMutInfer)
15782 {
1448415783 if (instr_is_comptime(value)) {
1448515784 ConstExprValue *dest_val = const_ptr_pointee(ira, ira->codegen, &ptr->value, source_instr->source_node);
1448615785 if (dest_val == nullptr)
......@@ -14494,18 +15793,24 @@ static IrInstruction *ir_analyze_store_ptr(IrAnalyze *ira, IrInstruction *source
1449415793 // ConstPtrMutComptimeVar, thus defeating the logic below.
1449515794 bool same_global_refs = ptr->value.data.x_ptr.mut != ConstPtrMutComptimeVar;
1449615795 copy_const_val(dest_val, &value->value, same_global_refs);
14497 if (!ira->new_irb.current_basic_block->must_be_comptime_source_instr) {
15796 if (ptr->value.data.x_ptr.mut == ConstPtrMutComptimeVar &&
15797 !ira->new_irb.current_basic_block->must_be_comptime_source_instr)
15798 {
1449815799 ira->new_irb.current_basic_block->must_be_comptime_source_instr = source_instr;
1449915800 }
1450015801 return ir_const_void(ira, source_instr);
1450115802 }
1450215803 }
14503 ir_add_error(ira, source_instr,
14504 buf_sprintf("cannot store runtime value in compile time variable"));
14505 ConstExprValue *dest_val = const_ptr_pointee_unchecked(ira->codegen, &ptr->value);
14506 dest_val->type = ira->codegen->builtin_types.entry_invalid;
15804 if (ptr->value.data.x_ptr.mut == ConstPtrMutInfer) {
15805 ptr->value.special = ConstValSpecialRuntime;
15806 } else {
15807 ir_add_error(ira, source_instr,
15808 buf_sprintf("cannot store runtime value in compile time variable"));
15809 ConstExprValue *dest_val = const_ptr_pointee_unchecked(ira->codegen, &ptr->value);
15810 dest_val->type = ira->codegen->builtin_types.entry_invalid;
1450715811
14508 return ira->codegen->invalid_instruction;
15812 return ira->codegen->invalid_instruction;
15813 }
1450915814 }
1451015815 }
1451115816
......@@ -14534,7 +15839,7 @@ static IrInstruction *ir_analyze_store_ptr(IrAnalyze *ira, IrInstruction *source
1453415839 return result;
1453515840}
1453615841
14537static IrInstruction *ir_analyze_fn_call(IrAnalyze *ira, IrInstructionCall *call_instruction,
15842static IrInstruction *ir_analyze_fn_call(IrAnalyze *ira, IrInstructionCallSrc *call_instruction,
1453815843 ZigFn *fn_entry, ZigType *fn_type, IrInstruction *fn_ref,
1453915844 IrInstruction *first_arg_ptr, bool comptime_fn_call, FnInline fn_inline)
1454015845{
......@@ -14637,7 +15942,7 @@ static IrInstruction *ir_analyze_fn_call(IrAnalyze *ira, IrInstructionCall *call
1463715942 if (!first_arg_known_bare && handle_is_ptr(first_arg_ptr->value.type->data.pointer.child_type)) {
1463815943 first_arg = first_arg_ptr;
1463915944 } else {
14640 first_arg = ir_get_deref(ira, first_arg_ptr, first_arg_ptr);
15945 first_arg = ir_get_deref(ira, first_arg_ptr, first_arg_ptr, nullptr);
1464115946 if (type_is_invalid(first_arg->value.type))
1464215947 return ira->codegen->invalid_instruction;
1464315948 }
......@@ -14796,7 +16101,7 @@ static IrInstruction *ir_analyze_fn_call(IrAnalyze *ira, IrInstructionCall *call
1479616101 if (!first_arg_known_bare && handle_is_ptr(first_arg_ptr->value.type->data.pointer.child_type)) {
1479716102 first_arg = first_arg_ptr;
1479816103 } else {
14799 first_arg = ir_get_deref(ira, first_arg_ptr, first_arg_ptr);
16104 first_arg = ir_get_deref(ira, first_arg_ptr, first_arg_ptr, nullptr);
1480016105 if (type_is_invalid(first_arg->value.type))
1480116106 return ira->codegen->invalid_instruction;
1480216107 }
......@@ -14840,7 +16145,7 @@ static IrInstruction *ir_analyze_fn_call(IrAnalyze *ira, IrInstructionCall *call
1484016145 if (type_is_invalid(arg_var_ptr_inst->value.type))
1484116146 return ira->codegen->invalid_instruction;
1484216147
14843 IrInstruction *arg_tuple_arg = ir_get_deref(ira, arg, arg_var_ptr_inst);
16148 IrInstruction *arg_tuple_arg = ir_get_deref(ira, arg, arg_var_ptr_inst, nullptr);
1484416149 if (type_is_invalid(arg_tuple_arg->value.type))
1484516150 return ira->codegen->invalid_instruction;
1484616151
......@@ -14889,7 +16194,7 @@ static IrInstruction *ir_analyze_fn_call(IrAnalyze *ira, IrInstructionCall *call
1488916194 nullptr, nullptr, fn_proto_node->data.fn_proto.align_expr, nullptr, ira->new_irb.exec, nullptr);
1489016195 IrInstructionConst *const_instruction = ir_create_instruction<IrInstructionConst>(&ira->new_irb,
1489116196 impl_fn->child_scope, fn_proto_node->data.fn_proto.align_expr);
14892 const_instruction->base.value = *align_result;
16197 copy_const_val(&const_instruction->base.value, align_result, true);
1489316198
1489416199 uint32_t align_bytes = 0;
1489516200 ir_resolve_align(ira, &const_instruction->base, &align_bytes);
......@@ -14971,6 +16276,19 @@ static IrInstruction *ir_analyze_fn_call(IrAnalyze *ira, IrInstructionCall *call
1497116276 }
1497216277
1497316278 FnTypeId *impl_fn_type_id = &impl_fn->type_entry->data.fn.fn_type_id;
16279 IrInstruction *result_loc;
16280 if (handle_is_ptr(impl_fn_type_id->return_type)) {
16281 result_loc = ir_resolve_result(ira, &call_instruction->base, call_instruction->result_loc,
16282 impl_fn_type_id->return_type, nullptr, true, true);
16283 if (result_loc != nullptr && (type_is_invalid(result_loc->value.type) ||
16284 instr_is_unreachable(result_loc)))
16285 {
16286 return result_loc;
16287 }
16288 } else {
16289 result_loc = nullptr;
16290 }
16291
1497416292 if (fn_type_can_fail(impl_fn_type_id)) {
1497516293 parent_fn_entry->calls_or_awaits_errorable_fn = true;
1497616294 }
......@@ -14979,18 +16297,14 @@ static IrInstruction *ir_analyze_fn_call(IrAnalyze *ira, IrInstructionCall *call
1497916297 if (call_instruction->is_async) {
1498016298 IrInstruction *result = ir_analyze_async_call(ira, call_instruction, impl_fn, impl_fn->type_entry,
1498116299 fn_ref, casted_args, impl_param_count, async_allocator_inst);
14982 ir_add_alloca(ira, result, result->value.type);
1498316300 return ir_finish_anal(ira, result);
1498416301 }
1498516302
1498616303 assert(async_allocator_inst == nullptr);
14987 IrInstruction *new_call_instruction = ir_build_call(&ira->new_irb,
14988 call_instruction->base.scope, call_instruction->base.source_node,
14989 impl_fn, nullptr, impl_param_count, casted_args, false, fn_inline,
14990 call_instruction->is_async, nullptr, casted_new_stack);
14991 new_call_instruction->value.type = impl_fn_type_id->return_type;
14992
14993 ir_add_alloca(ira, new_call_instruction, impl_fn_type_id->return_type);
16304 IrInstruction *new_call_instruction = ir_build_call_gen(ira, &call_instruction->base,
16305 impl_fn, nullptr, impl_param_count, casted_args, fn_inline,
16306 call_instruction->is_async, nullptr, casted_new_stack, result_loc,
16307 impl_fn_type_id->return_type);
1499416308
1499516309 return ir_finish_anal(ira, new_call_instruction);
1499616310 }
......@@ -15018,7 +16332,7 @@ static IrInstruction *ir_analyze_fn_call(IrAnalyze *ira, IrInstructionCall *call
1501816332 {
1501916333 first_arg = first_arg_ptr;
1502016334 } else {
15021 first_arg = ir_get_deref(ira, first_arg_ptr, first_arg_ptr);
16335 first_arg = ir_get_deref(ira, first_arg_ptr, first_arg_ptr, nullptr);
1502216336 if (type_is_invalid(first_arg->value.type))
1502316337 return ira->codegen->invalid_instruction;
1502416338 }
......@@ -15075,7 +16389,6 @@ static IrInstruction *ir_analyze_fn_call(IrAnalyze *ira, IrInstructionCall *call
1507516389
1507616390 IrInstruction *result = ir_analyze_async_call(ira, call_instruction, fn_entry, fn_type, fn_ref,
1507716391 casted_args, call_param_count, async_allocator_inst);
15078 ir_add_alloca(ira, result, result->value.type);
1507916392 return ir_finish_anal(ira, result);
1508016393 }
1508116394
......@@ -15085,15 +16398,24 @@ static IrInstruction *ir_analyze_fn_call(IrAnalyze *ira, IrInstructionCall *call
1508516398 return ira->codegen->invalid_instruction;
1508616399 }
1508716400
15088 IrInstruction *new_call_instruction = ir_build_call(&ira->new_irb,
15089 call_instruction->base.scope, call_instruction->base.source_node,
15090 fn_entry, fn_ref, call_param_count, casted_args, false, fn_inline, false, nullptr, casted_new_stack);
15091 new_call_instruction->value.type = return_type;
15092 ir_add_alloca(ira, new_call_instruction, return_type);
16401 IrInstruction *result_loc;
16402 if (handle_is_ptr(return_type)) {
16403 result_loc = ir_resolve_result(ira, &call_instruction->base, call_instruction->result_loc,
16404 return_type, nullptr, true, true);
16405 if (result_loc != nullptr && (type_is_invalid(result_loc->value.type) || instr_is_unreachable(result_loc))) {
16406 return result_loc;
16407 }
16408 } else {
16409 result_loc = nullptr;
16410 }
16411
16412 IrInstruction *new_call_instruction = ir_build_call_gen(ira, &call_instruction->base, fn_entry, fn_ref,
16413 call_param_count, casted_args, fn_inline, false, nullptr, casted_new_stack,
16414 result_loc, return_type);
1509316415 return ir_finish_anal(ira, new_call_instruction);
1509416416}
1509516417
15096static IrInstruction *ir_analyze_instruction_call(IrAnalyze *ira, IrInstructionCall *call_instruction) {
16418static IrInstruction *ir_analyze_instruction_call(IrAnalyze *ira, IrInstructionCallSrc *call_instruction) {
1509716419 IrInstruction *fn_ref = call_instruction->fn_ref->child;
1509816420 if (type_is_invalid(fn_ref->value.type))
1509916421 return ira->codegen->invalid_instruction;
......@@ -15117,7 +16439,8 @@ static IrInstruction *ir_analyze_instruction_call(IrAnalyze *ira, IrInstructionC
1511716439
1511816440 IrInstruction *arg = call_instruction->args[0]->child;
1511916441
15120 IrInstruction *cast_instruction = ir_analyze_cast(ira, &call_instruction->base, dest_type, arg);
16442 IrInstruction *cast_instruction = ir_analyze_cast(ira, &call_instruction->base, dest_type, arg,
16443 call_instruction->result_loc);
1512116444 if (type_is_invalid(cast_instruction->value.type))
1512216445 return ira->codegen->invalid_instruction;
1512316446 return ir_finish_anal(ira, cast_instruction);
......@@ -15170,7 +16493,7 @@ static Error ir_read_const_ptr(IrAnalyze *ira, CodeGen *codegen, AstNode *source
1517016493
1517116494 if (dst_size <= src_size) {
1517216495 if (src_size == dst_size && types_have_same_zig_comptime_repr(pointee->type, out_val->type)) {
15173 copy_const_val(out_val, pointee, ptr_val->data.x_ptr.mut == ConstPtrMutComptimeConst);
16496 copy_const_val(out_val, pointee, ptr_val->data.x_ptr.mut != ConstPtrMutComptimeVar);
1517416497 return ErrorNone;
1517516498 }
1517616499 Buf buf = BUF_INIT;
......@@ -15419,7 +16742,7 @@ static IrInstruction *ir_analyze_instruction_un_op(IrAnalyze *ira, IrInstruction
1541916742 return ira->codegen->invalid_instruction;
1542016743 }
1542116744
15422 IrInstruction *result = ir_get_deref(ira, &instruction->base, ptr);
16745 IrInstruction *result = ir_get_deref(ira, &instruction->base, ptr, instruction->result_loc);
1542316746 if (result == ira->codegen->invalid_instruction)
1542416747 return ira->codegen->invalid_instruction;
1542516748
......@@ -15438,6 +16761,19 @@ static IrInstruction *ir_analyze_instruction_un_op(IrAnalyze *ira, IrInstruction
1543816761 zig_unreachable();
1543916762}
1544016763
16764static void ir_push_resume(IrAnalyze *ira, IrSuspendPosition pos) {
16765 IrBasicBlock *old_bb = ira->old_irb.exec->basic_block_list.at(pos.basic_block_index);
16766 if (old_bb->in_resume_stack) return;
16767 ira->resume_stack.append(pos);
16768 old_bb->in_resume_stack = true;
16769}
16770
16771static void ir_push_resume_block(IrAnalyze *ira, IrBasicBlock *old_bb) {
16772 if (ira->resume_stack.length != 0) {
16773 ir_push_resume(ira, {old_bb->index, 0});
16774 }
16775}
16776
1544116777static IrInstruction *ir_analyze_instruction_br(IrAnalyze *ira, IrInstructionBr *br_instruction) {
1544216778 IrBasicBlock *old_dest_block = br_instruction->dest_block;
1544316779
......@@ -15445,13 +16781,15 @@ static IrInstruction *ir_analyze_instruction_br(IrAnalyze *ira, IrInstructionBr
1544516781 if (!ir_resolve_comptime(ira, br_instruction->is_comptime->child, &is_comptime))
1544616782 return ir_unreach_error(ira);
1544716783
15448 if (is_comptime || old_dest_block->ref_count == 1)
16784 if (is_comptime || (old_dest_block->ref_count == 1 && old_dest_block->suspend_instruction_ref == nullptr))
1544916785 return ir_inline_bb(ira, &br_instruction->base, old_dest_block);
1545016786
1545116787 IrBasicBlock *new_bb = ir_get_new_bb_runtime(ira, old_dest_block, &br_instruction->base);
1545216788 if (new_bb == nullptr)
1545316789 return ir_unreach_error(ira);
1545416790
16791 ir_push_resume_block(ira, old_dest_block);
16792
1545516793 IrInstruction *result = ir_build_br(&ira->new_irb,
1545616794 br_instruction->base.scope, br_instruction->base.source_node, new_bb, nullptr);
1545716795 result->value.type = ira->codegen->builtin_types.entry_unreachable;
......@@ -15480,13 +16818,15 @@ static IrInstruction *ir_analyze_instruction_cond_br(IrAnalyze *ira, IrInstructi
1548016818 IrBasicBlock *old_dest_block = cond_is_true ?
1548116819 cond_br_instruction->then_block : cond_br_instruction->else_block;
1548216820
15483 if (is_comptime || old_dest_block->ref_count == 1)
16821 if (is_comptime || (old_dest_block->ref_count == 1 && old_dest_block->suspend_instruction_ref == nullptr))
1548416822 return ir_inline_bb(ira, &cond_br_instruction->base, old_dest_block);
1548516823
1548616824 IrBasicBlock *new_dest_block = ir_get_new_bb_runtime(ira, old_dest_block, &cond_br_instruction->base);
1548716825 if (new_dest_block == nullptr)
1548816826 return ir_unreach_error(ira);
1548916827
16828 ir_push_resume_block(ira, old_dest_block);
16829
1549016830 IrInstruction *result = ir_build_br(&ira->new_irb,
1549116831 cond_br_instruction->base.scope, cond_br_instruction->base.source_node, new_dest_block, nullptr);
1549216832 result->value.type = ira->codegen->builtin_types.entry_unreachable;
......@@ -15502,6 +16842,9 @@ static IrInstruction *ir_analyze_instruction_cond_br(IrAnalyze *ira, IrInstructi
1550216842 if (new_else_block == nullptr)
1550316843 return ir_unreach_error(ira);
1550416844
16845 ir_push_resume_block(ira, cond_br_instruction->else_block);
16846 ir_push_resume_block(ira, cond_br_instruction->then_block);
16847
1550516848 IrInstruction *result = ir_build_cond_br(&ira->new_irb,
1550616849 cond_br_instruction->base.scope, cond_br_instruction->base.source_node,
1550716850 casted_condition, new_then_block, new_else_block, nullptr);
......@@ -15540,11 +16883,85 @@ static IrInstruction *ir_analyze_instruction_phi(IrAnalyze *ira, IrInstructionPh
1554016883 zig_unreachable();
1554116884 }
1554216885
15543 ZigList<IrBasicBlock*> new_incoming_blocks = {0};
15544 ZigList<IrInstruction*> new_incoming_values = {0};
15545
15546 for (size_t i = 0; i < phi_instruction->incoming_count; i += 1) {
15547 IrBasicBlock *predecessor = phi_instruction->incoming_blocks[i];
16886 ResultLocPeerParent *peer_parent = phi_instruction->peer_parent;
16887 if (peer_parent != nullptr && !peer_parent->skipped && !peer_parent->done_resuming &&
16888 peer_parent->peers.length >= 2)
16889 {
16890 if (peer_parent->resolved_type == nullptr) {
16891 IrInstruction **instructions = allocate<IrInstruction *>(peer_parent->peers.length);
16892 for (size_t i = 0; i < peer_parent->peers.length; i += 1) {
16893 ResultLocPeer *this_peer = peer_parent->peers.at(i);
16894
16895 IrInstruction *gen_instruction = this_peer->base.gen_instruction;
16896 if (gen_instruction == nullptr) {
16897 // unreachable instructions will cause implicit_elem_type to be null
16898 if (this_peer->base.implicit_elem_type == nullptr) {
16899 instructions[i] = ir_const_unreachable(ira, this_peer->base.source_instruction);
16900 } else {
16901 instructions[i] = ir_const(ira, this_peer->base.source_instruction,
16902 this_peer->base.implicit_elem_type);
16903 instructions[i]->value.special = ConstValSpecialRuntime;
16904 }
16905 } else {
16906 instructions[i] = gen_instruction;
16907 }
16908
16909 }
16910 ZigType *expected_type = ir_result_loc_expected_type(ira, &phi_instruction->base, peer_parent->parent);
16911 peer_parent->resolved_type = ir_resolve_peer_types(ira,
16912 peer_parent->base.source_instruction->source_node, expected_type, instructions,
16913 peer_parent->peers.length);
16914
16915 // the logic below assumes there are no instructions in the new current basic block yet
16916 ir_assert(ira->new_irb.current_basic_block->instruction_list.length == 0, &phi_instruction->base);
16917
16918 // In case resolving the parent activates a suspend, do it now
16919 IrInstruction *parent_result_loc = ir_resolve_result(ira, &phi_instruction->base, peer_parent->parent,
16920 peer_parent->resolved_type, nullptr, false, false);
16921 if (parent_result_loc != nullptr &&
16922 (type_is_invalid(parent_result_loc->value.type) || instr_is_unreachable(parent_result_loc)))
16923 {
16924 return parent_result_loc;
16925 }
16926 // If the above code generated any instructions in the current basic block, we need
16927 // to move them to the peer parent predecessor.
16928 ZigList<IrInstruction *> instrs_to_move = {};
16929 while (ira->new_irb.current_basic_block->instruction_list.length != 0) {
16930 instrs_to_move.append(ira->new_irb.current_basic_block->instruction_list.pop());
16931 }
16932 if (instrs_to_move.length != 0) {
16933 IrBasicBlock *predecessor = peer_parent->base.source_instruction->child->owner_bb;
16934 IrInstruction *branch_instruction = predecessor->instruction_list.pop();
16935 ir_assert(branch_instruction->value.type->id == ZigTypeIdUnreachable, &phi_instruction->base);
16936 while (instrs_to_move.length != 0) {
16937 predecessor->instruction_list.append(instrs_to_move.pop());
16938 }
16939 predecessor->instruction_list.append(branch_instruction);
16940 }
16941 }
16942
16943 IrSuspendPosition suspend_pos;
16944 ira_suspend(ira, &phi_instruction->base, nullptr, &suspend_pos);
16945 ir_push_resume(ira, suspend_pos);
16946
16947 for (size_t i = 0; i < peer_parent->peers.length; i += 1) {
16948 ResultLocPeer *opposite_peer = peer_parent->peers.at(peer_parent->peers.length - i - 1);
16949 if (opposite_peer->base.implicit_elem_type != nullptr &&
16950 opposite_peer->base.implicit_elem_type->id != ZigTypeIdUnreachable)
16951 {
16952 ir_push_resume(ira, opposite_peer->suspend_pos);
16953 }
16954 }
16955
16956 peer_parent->done_resuming = true;
16957 return ira_resume(ira);
16958 }
16959
16960 ZigList<IrBasicBlock*> new_incoming_blocks = {0};
16961 ZigList<IrInstruction*> new_incoming_values = {0};
16962
16963 for (size_t i = 0; i < phi_instruction->incoming_count; i += 1) {
16964 IrBasicBlock *predecessor = phi_instruction->incoming_blocks[i];
1554816965 if (predecessor->ref_count == 0)
1554916966 continue;
1555016967
......@@ -15612,7 +17029,7 @@ static IrInstruction *ir_analyze_instruction_phi(IrAnalyze *ira, IrInstructionPh
1561217029 IrInstruction *branch_instruction = predecessor->instruction_list.pop();
1561317030 ir_set_cursor_at_end(&ira->new_irb, predecessor);
1561417031 IrInstruction *casted_value = ir_implicit_cast(ira, new_value, resolved_type);
15615 if (casted_value == ira->codegen->invalid_instruction) {
17032 if (type_is_invalid(casted_value->value.type)) {
1561617033 return ira->codegen->invalid_instruction;
1561717034 }
1561817035 new_incoming_values.items[i] = casted_value;
......@@ -15628,7 +17045,7 @@ static IrInstruction *ir_analyze_instruction_phi(IrAnalyze *ira, IrInstructionPh
1562817045
1562917046 IrInstruction *result = ir_build_phi(&ira->new_irb,
1563017047 phi_instruction->base.scope, phi_instruction->base.source_node,
15631 new_incoming_blocks.length, new_incoming_blocks.items, new_incoming_values.items);
17048 new_incoming_blocks.length, new_incoming_blocks.items, new_incoming_values.items, nullptr);
1563217049 result->value.type = resolved_type;
1563317050
1563417051 if (all_stack_ptrs) {
......@@ -15846,6 +17263,48 @@ static IrInstruction *ir_analyze_instruction_elem_ptr(IrAnalyze *ira, IrInstruct
1584617263 if (array_ptr_val == nullptr)
1584717264 return ira->codegen->invalid_instruction;
1584817265
17266 if (array_ptr_val->special == ConstValSpecialUndef && elem_ptr_instruction->init_array_type != nullptr) {
17267 if (array_type->id == ZigTypeIdArray) {
17268 array_ptr_val->data.x_array.special = ConstArraySpecialNone;
17269 array_ptr_val->data.x_array.data.s_none.elements = create_const_vals(array_type->data.array.len);
17270 array_ptr_val->special = ConstValSpecialStatic;
17271 for (size_t i = 0; i < array_type->data.array.len; i += 1) {
17272 ConstExprValue *elem_val = &array_ptr_val->data.x_array.data.s_none.elements[i];
17273 elem_val->special = ConstValSpecialUndef;
17274 elem_val->type = array_type->data.array.child_type;
17275 elem_val->parent.id = ConstParentIdArray;
17276 elem_val->parent.data.p_array.array_val = array_ptr_val;
17277 elem_val->parent.data.p_array.elem_index = i;
17278 }
17279 } else if (is_slice(array_type)) {
17280 ZigType *actual_array_type = ir_resolve_type(ira, elem_ptr_instruction->init_array_type->child);
17281 if (type_is_invalid(actual_array_type))
17282 return ira->codegen->invalid_instruction;
17283 assert(actual_array_type->id == ZigTypeIdArray);
17284
17285 ConstExprValue *array_init_val = create_const_vals(1);
17286 array_init_val->special = ConstValSpecialStatic;
17287 array_init_val->type = actual_array_type;
17288 array_init_val->data.x_array.special = ConstArraySpecialNone;
17289 array_init_val->data.x_array.data.s_none.elements = create_const_vals(actual_array_type->data.array.len);
17290 array_init_val->special = ConstValSpecialStatic;
17291 for (size_t i = 0; i < actual_array_type->data.array.len; i += 1) {
17292 ConstExprValue *elem_val = &array_init_val->data.x_array.data.s_none.elements[i];
17293 elem_val->special = ConstValSpecialUndef;
17294 elem_val->type = actual_array_type->data.array.child_type;
17295 elem_val->parent.id = ConstParentIdArray;
17296 elem_val->parent.data.p_array.array_val = array_init_val;
17297 elem_val->parent.data.p_array.elem_index = i;
17298 }
17299
17300 init_const_slice(ira->codegen, array_ptr_val, array_init_val, 0, actual_array_type->data.array.len,
17301 false);
17302 array_ptr_val->data.x_struct.fields[slice_ptr_index].data.x_ptr.mut = ConstPtrMutInfer;
17303 } else {
17304 zig_unreachable();
17305 }
17306 }
17307
1584917308 if (array_ptr_val->special != ConstValSpecialRuntime &&
1585017309 (array_type->id != ZigTypeIdPointer ||
1585117310 array_ptr_val->data.x_ptr.special != ConstPtrSpecialHardCodedAddr))
......@@ -15911,8 +17370,9 @@ static IrInstruction *ir_analyze_instruction_elem_ptr(IrAnalyze *ira, IrInstruct
1591117370 } else if (is_slice(array_type)) {
1591217371 ConstExprValue *ptr_field = &array_ptr_val->data.x_struct.fields[slice_ptr_index];
1591317372 if (ptr_field->data.x_ptr.special == ConstPtrSpecialHardCodedAddr) {
15914 IrInstruction *result = ir_build_elem_ptr(&ira->new_irb, elem_ptr_instruction->base.scope, elem_ptr_instruction->base.source_node,
15915 array_ptr, casted_elem_index, false, elem_ptr_instruction->ptr_len);
17373 IrInstruction *result = ir_build_elem_ptr(&ira->new_irb, elem_ptr_instruction->base.scope,
17374 elem_ptr_instruction->base.source_node, array_ptr, casted_elem_index, false,
17375 elem_ptr_instruction->ptr_len, nullptr);
1591617376 result->value.type = return_type;
1591717377 return result;
1591817378 }
......@@ -15965,7 +17425,16 @@ static IrInstruction *ir_analyze_instruction_elem_ptr(IrAnalyze *ira, IrInstruct
1596517425 }
1596617426 return result;
1596717427 } else if (array_type->id == ZigTypeIdArray) {
15968 IrInstruction *result = ir_const(ira, &elem_ptr_instruction->base, return_type);
17428 IrInstruction *result;
17429 if (orig_array_ptr_val->data.x_ptr.mut == ConstPtrMutInfer) {
17430 result = ir_build_elem_ptr(&ira->new_irb, elem_ptr_instruction->base.scope,
17431 elem_ptr_instruction->base.source_node, array_ptr, casted_elem_index,
17432 false, elem_ptr_instruction->ptr_len, elem_ptr_instruction->init_array_type);
17433 result->value.type = return_type;
17434 result->value.special = ConstValSpecialStatic;
17435 } else {
17436 result = ir_const(ira, &elem_ptr_instruction->base, return_type);
17437 }
1596917438 ConstExprValue *out_val = &result->value;
1597017439 out_val->data.x_ptr.special = ConstPtrSpecialBaseArray;
1597117440 out_val->data.x_ptr.mut = orig_array_ptr_val->data.x_ptr.mut;
......@@ -15977,7 +17446,6 @@ static IrInstruction *ir_analyze_instruction_elem_ptr(IrAnalyze *ira, IrInstruct
1597717446 }
1597817447 }
1597917448 }
15980
1598117449 } else {
1598217450 // runtime known element index
1598317451 switch (type_requires_comptime(ira->codegen, return_type)) {
......@@ -16003,8 +17471,9 @@ static IrInstruction *ir_analyze_instruction_elem_ptr(IrAnalyze *ira, IrInstruct
1600317471 }
1600417472 }
1600517473
16006 IrInstruction *result = ir_build_elem_ptr(&ira->new_irb, elem_ptr_instruction->base.scope, elem_ptr_instruction->base.source_node,
16007 array_ptr, casted_elem_index, safety_check_on, elem_ptr_instruction->ptr_len);
17474 IrInstruction *result = ir_build_elem_ptr(&ira->new_irb, elem_ptr_instruction->base.scope,
17475 elem_ptr_instruction->base.source_node, array_ptr, casted_elem_index, safety_check_on,
17476 elem_ptr_instruction->ptr_len, elem_ptr_instruction->init_array_type);
1600817477 result->value.type = return_type;
1600917478 return result;
1601017479}
......@@ -16049,8 +17518,80 @@ static IrInstruction *ir_analyze_container_member_access_inner(IrAnalyze *ira,
1604917518 return ira->codegen->invalid_instruction;
1605017519}
1605117520
17521static IrInstruction *ir_analyze_struct_field_ptr(IrAnalyze *ira, IrInstruction *source_instr,
17522 TypeStructField *field, IrInstruction *struct_ptr, ZigType *struct_type, bool initializing)
17523{
17524 switch (type_has_one_possible_value(ira->codegen, field->type_entry)) {
17525 case OnePossibleValueInvalid:
17526 return ira->codegen->invalid_instruction;
17527 case OnePossibleValueYes: {
17528 IrInstruction *elem = ir_const(ira, source_instr, field->type_entry);
17529 return ir_get_ref(ira, source_instr, elem, false, false);
17530 }
17531 case OnePossibleValueNo:
17532 break;
17533 }
17534 assert(struct_ptr->value.type->id == ZigTypeIdPointer);
17535 bool is_packed = (struct_type->data.structure.layout == ContainerLayoutPacked);
17536 uint32_t align_bytes = is_packed ? 1 : get_abi_alignment(ira->codegen, field->type_entry);
17537 uint32_t ptr_bit_offset = struct_ptr->value.type->data.pointer.bit_offset_in_host;
17538 uint32_t ptr_host_int_bytes = struct_ptr->value.type->data.pointer.host_int_bytes;
17539 uint32_t host_int_bytes_for_result_type = (ptr_host_int_bytes == 0) ?
17540 get_host_int_bytes(ira->codegen, struct_type, field) : ptr_host_int_bytes;
17541 bool is_const = struct_ptr->value.type->data.pointer.is_const;
17542 bool is_volatile = struct_ptr->value.type->data.pointer.is_volatile;
17543 ZigType *ptr_type = get_pointer_to_type_extra(ira->codegen, field->type_entry,
17544 is_const, is_volatile, PtrLenSingle, align_bytes,
17545 (uint32_t)(ptr_bit_offset + field->bit_offset_in_host),
17546 (uint32_t)host_int_bytes_for_result_type, false);
17547 if (instr_is_comptime(struct_ptr)) {
17548 ConstExprValue *ptr_val = ir_resolve_const(ira, struct_ptr, UndefBad);
17549 if (!ptr_val)
17550 return ira->codegen->invalid_instruction;
17551
17552 if (ptr_val->data.x_ptr.special != ConstPtrSpecialHardCodedAddr) {
17553 ConstExprValue *struct_val = const_ptr_pointee(ira, ira->codegen, ptr_val, source_instr->source_node);
17554 if (struct_val == nullptr)
17555 return ira->codegen->invalid_instruction;
17556 if (type_is_invalid(struct_val->type))
17557 return ira->codegen->invalid_instruction;
17558 if (struct_val->special == ConstValSpecialUndef && initializing) {
17559 struct_val->data.x_struct.fields = create_const_vals(struct_type->data.structure.src_field_count);
17560 struct_val->special = ConstValSpecialStatic;
17561 for (size_t i = 0; i < struct_type->data.structure.src_field_count; i += 1) {
17562 ConstExprValue *field_val = &struct_val->data.x_struct.fields[i];
17563 field_val->special = ConstValSpecialUndef;
17564 field_val->type = struct_type->data.structure.fields[i].type_entry;
17565 field_val->parent.id = ConstParentIdStruct;
17566 field_val->parent.data.p_struct.struct_val = struct_val;
17567 field_val->parent.data.p_struct.field_index = i;
17568 }
17569 }
17570 IrInstruction *result;
17571 if (ptr_val->data.x_ptr.mut == ConstPtrMutInfer) {
17572 result = ir_build_struct_field_ptr(&ira->new_irb, source_instr->scope,
17573 source_instr->source_node, struct_ptr, field);
17574 result->value.type = ptr_type;
17575 result->value.special = ConstValSpecialStatic;
17576 } else {
17577 result = ir_const(ira, source_instr, ptr_type);
17578 }
17579 ConstExprValue *const_val = &result->value;
17580 const_val->data.x_ptr.special = ConstPtrSpecialBaseStruct;
17581 const_val->data.x_ptr.mut = ptr_val->data.x_ptr.mut;
17582 const_val->data.x_ptr.data.base_struct.struct_val = struct_val;
17583 const_val->data.x_ptr.data.base_struct.field_index = field->src_index;
17584 return result;
17585 }
17586 }
17587 IrInstruction *result = ir_build_struct_field_ptr(&ira->new_irb, source_instr->scope, source_instr->source_node,
17588 struct_ptr, field);
17589 result->value.type = ptr_type;
17590 return result;
17591}
17592
1605217593static IrInstruction *ir_analyze_container_field_ptr(IrAnalyze *ira, Buf *field_name,
16053 IrInstruction *source_instr, IrInstruction *container_ptr, ZigType *container_type)
17594 IrInstruction *source_instr, IrInstruction *container_ptr, ZigType *container_type, bool initializing)
1605417595{
1605517596 Error err;
1605617597
......@@ -16059,81 +17600,55 @@ static IrInstruction *ir_analyze_container_field_ptr(IrAnalyze *ira, Buf *field_
1605917600 return ira->codegen->invalid_instruction;
1606017601
1606117602 assert(container_ptr->value.type->id == ZigTypeIdPointer);
16062 bool is_const = container_ptr->value.type->data.pointer.is_const;
16063 bool is_volatile = container_ptr->value.type->data.pointer.is_volatile;
1606417603 if (bare_type->id == ZigTypeIdStruct) {
1606517604 TypeStructField *field = find_struct_type_field(bare_type, field_name);
16066 if (field) {
16067 switch (type_has_one_possible_value(ira->codegen, field->type_entry)) {
16068 case OnePossibleValueInvalid:
16069 return ira->codegen->invalid_instruction;
16070 case OnePossibleValueYes: {
16071 IrInstruction *elem = ir_const(ira, source_instr, field->type_entry);
16072 return ir_get_ref(ira, source_instr, elem, false, false);
16073 }
16074 case OnePossibleValueNo:
16075 break;
16076 }
16077 bool is_packed = (bare_type->data.structure.layout == ContainerLayoutPacked);
16078 uint32_t align_bytes = is_packed ? 1 : get_abi_alignment(ira->codegen, field->type_entry);
16079 uint32_t ptr_bit_offset = container_ptr->value.type->data.pointer.bit_offset_in_host;
16080 uint32_t ptr_host_int_bytes = container_ptr->value.type->data.pointer.host_int_bytes;
16081 uint32_t host_int_bytes_for_result_type = (ptr_host_int_bytes == 0) ?
16082 get_host_int_bytes(ira->codegen, bare_type, field) : ptr_host_int_bytes;
16083 if (instr_is_comptime(container_ptr)) {
16084 ConstExprValue *ptr_val = ir_resolve_const(ira, container_ptr, UndefBad);
16085 if (!ptr_val)
16086 return ira->codegen->invalid_instruction;
16087
16088 if (ptr_val->data.x_ptr.special != ConstPtrSpecialHardCodedAddr) {
16089 ConstExprValue *struct_val = const_ptr_pointee(ira, ira->codegen, ptr_val, source_instr->source_node);
16090 if (struct_val == nullptr)
16091 return ira->codegen->invalid_instruction;
16092 if (type_is_invalid(struct_val->type))
16093 return ira->codegen->invalid_instruction;
16094 ZigType *ptr_type = get_pointer_to_type_extra(ira->codegen, field->type_entry,
16095 is_const, is_volatile, PtrLenSingle, align_bytes,
16096 (uint32_t)(ptr_bit_offset + field->bit_offset_in_host),
16097 (uint32_t)host_int_bytes_for_result_type, false);
16098 IrInstruction *result = ir_const(ira, source_instr, ptr_type);
16099 ConstExprValue *const_val = &result->value;
16100 const_val->data.x_ptr.special = ConstPtrSpecialBaseStruct;
16101 const_val->data.x_ptr.mut = container_ptr->value.data.x_ptr.mut;
16102 const_val->data.x_ptr.data.base_struct.struct_val = struct_val;
16103 const_val->data.x_ptr.data.base_struct.field_index = field->src_index;
16104 return result;
16105 }
16106 }
16107 IrInstruction *result = ir_build_struct_field_ptr(&ira->new_irb, source_instr->scope, source_instr->source_node,
16108 container_ptr, field);
16109 result->value.type = get_pointer_to_type_extra(ira->codegen, field->type_entry, is_const, is_volatile,
16110 PtrLenSingle,
16111 align_bytes,
16112 (uint32_t)(ptr_bit_offset + field->bit_offset_in_host),
16113 host_int_bytes_for_result_type, false);
16114 return result;
17605 if (field != nullptr) {
17606 return ir_analyze_struct_field_ptr(ira, source_instr, field, container_ptr, bare_type, initializing);
1611517607 } else {
1611617608 return ir_analyze_container_member_access_inner(ira, bare_type, field_name,
1611717609 source_instr, container_ptr, container_type);
1611817610 }
16119 } else if (bare_type->id == ZigTypeIdEnum) {
17611 }
17612
17613 if (bare_type->id == ZigTypeIdEnum) {
1612017614 return ir_analyze_container_member_access_inner(ira, bare_type, field_name,
1612117615 source_instr, container_ptr, container_type);
16122 } else if (bare_type->id == ZigTypeIdUnion) {
17616 }
17617
17618 if (bare_type->id == ZigTypeIdUnion) {
17619 bool is_const = container_ptr->value.type->data.pointer.is_const;
17620 bool is_volatile = container_ptr->value.type->data.pointer.is_volatile;
17621
1612317622 TypeUnionField *field = find_union_type_field(bare_type, field_name);
16124 if (field) {
16125 if (instr_is_comptime(container_ptr)) {
16126 ConstExprValue *ptr_val = ir_resolve_const(ira, container_ptr, UndefBad);
16127 if (!ptr_val)
17623 if (field == nullptr) {
17624 return ir_analyze_container_member_access_inner(ira, bare_type, field_name,
17625 source_instr, container_ptr, container_type);
17626 }
17627 ZigType *ptr_type = get_pointer_to_type_extra(ira->codegen, field->type_entry,
17628 is_const, is_volatile, PtrLenSingle, 0, 0, 0, false);
17629 if (instr_is_comptime(container_ptr)) {
17630 ConstExprValue *ptr_val = ir_resolve_const(ira, container_ptr, UndefBad);
17631 if (!ptr_val)
17632 return ira->codegen->invalid_instruction;
17633
17634 if (ptr_val->data.x_ptr.special != ConstPtrSpecialHardCodedAddr) {
17635 ConstExprValue *union_val = const_ptr_pointee(ira, ira->codegen, ptr_val, source_instr->source_node);
17636 if (union_val == nullptr)
17637 return ira->codegen->invalid_instruction;
17638 if (type_is_invalid(union_val->type))
1612817639 return ira->codegen->invalid_instruction;
1612917640
16130 if (ptr_val->data.x_ptr.special != ConstPtrSpecialHardCodedAddr) {
16131 ConstExprValue *union_val = const_ptr_pointee(ira, ira->codegen, ptr_val, source_instr->source_node);
16132 if (union_val == nullptr)
16133 return ira->codegen->invalid_instruction;
16134 if (type_is_invalid(union_val->type))
16135 return ira->codegen->invalid_instruction;
17641 if (initializing) {
17642 ConstExprValue *payload_val = create_const_vals(1);
17643 payload_val->special = ConstValSpecialUndef;
17644 payload_val->type = field->type_entry;
17645 payload_val->parent.id = ConstParentIdUnion;
17646 payload_val->parent.data.p_union.union_val = union_val;
1613617647
17648 union_val->special = ConstValSpecialStatic;
17649 bigint_init_bigint(&union_val->data.x_union.tag, &field->enum_field->value);
17650 union_val->data.x_union.payload = payload_val;
17651 } else {
1613717652 TypeUnionField *actual_field = find_union_field_by_tag(bare_type, &union_val->data.x_union.tag);
1613817653 if (actual_field == nullptr)
1613917654 zig_unreachable();
......@@ -16144,33 +17659,35 @@ static IrInstruction *ir_analyze_container_field_ptr(IrAnalyze *ira, Buf *field_
1614417659 buf_ptr(actual_field->name)));
1614517660 return ira->codegen->invalid_instruction;
1614617661 }
17662 }
1614717663
16148 ConstExprValue *payload_val = union_val->data.x_union.payload;
17664 ConstExprValue *payload_val = union_val->data.x_union.payload;
1614917665
16150 ZigType *field_type = field->type_entry;
16151 ZigType *ptr_type = get_pointer_to_type_extra(ira->codegen, field_type,
16152 is_const, is_volatile, PtrLenSingle, 0, 0, 0, false);
1615317666
16154 IrInstruction *result = ir_const(ira, source_instr, ptr_type);
16155 ConstExprValue *const_val = &result->value;
16156 const_val->data.x_ptr.special = ConstPtrSpecialRef;
16157 const_val->data.x_ptr.mut = container_ptr->value.data.x_ptr.mut;
16158 const_val->data.x_ptr.data.ref.pointee = payload_val;
16159 return result;
17667 IrInstruction *result;
17668 if (ptr_val->data.x_ptr.mut == ConstPtrMutInfer) {
17669 result = ir_build_union_field_ptr(&ira->new_irb, source_instr->scope,
17670 source_instr->source_node, container_ptr, field, true, initializing);
17671 result->value.type = ptr_type;
17672 result->value.special = ConstValSpecialStatic;
17673 } else {
17674 result = ir_const(ira, source_instr, ptr_type);
1616017675 }
17676 ConstExprValue *const_val = &result->value;
17677 const_val->data.x_ptr.special = ConstPtrSpecialRef;
17678 const_val->data.x_ptr.mut = container_ptr->value.data.x_ptr.mut;
17679 const_val->data.x_ptr.data.ref.pointee = payload_val;
17680 return result;
1616117681 }
16162
16163 IrInstruction *result = ir_build_union_field_ptr(&ira->new_irb, source_instr->scope, source_instr->source_node, container_ptr, field);
16164 result->value.type = get_pointer_to_type_extra(ira->codegen, field->type_entry, is_const, is_volatile,
16165 PtrLenSingle, 0, 0, 0, false);
16166 return result;
16167 } else {
16168 return ir_analyze_container_member_access_inner(ira, bare_type, field_name,
16169 source_instr, container_ptr, container_type);
1617017682 }
16171 } else {
16172 zig_unreachable();
17683
17684 IrInstruction *result = ir_build_union_field_ptr(&ira->new_irb, source_instr->scope,
17685 source_instr->source_node, container_ptr, field, true, initializing);
17686 result->value.type = ptr_type;
17687 return result;
1617317688 }
17689
17690 zig_unreachable();
1617417691}
1617517692
1617617693static void add_link_lib_symbol(IrAnalyze *ira, Buf *lib_name, Buf *symbol_name, AstNode *source_node) {
......@@ -16208,6 +17725,11 @@ static void add_link_lib_symbol(IrAnalyze *ira, Buf *lib_name, Buf *symbol_name,
1620817725 link_lib->symbols.append(symbol_name);
1620917726}
1621017727
17728static IrInstruction *ir_error_dependency_loop(IrAnalyze *ira, IrInstruction *source_instr) {
17729 ErrorMsg *msg = ir_add_error(ira, source_instr, buf_sprintf("dependency loop detected"));
17730 emit_error_notes_for_ref_stack(ira->codegen, msg);
17731 return ira->codegen->invalid_instruction;
17732}
1621117733
1621217734static IrInstruction *ir_analyze_decl_ref(IrAnalyze *ira, IrInstruction *source_instruction, Tld *tld) {
1621317735 resolve_top_level_decl(ira->codegen, tld, source_instruction->source_node);
......@@ -16222,6 +17744,9 @@ static IrInstruction *ir_analyze_decl_ref(IrAnalyze *ira, IrInstruction *source_
1622217744 {
1622317745 TldVar *tld_var = (TldVar *)tld;
1622417746 ZigVar *var = tld_var->var;
17747 if (var == nullptr) {
17748 return ir_error_dependency_loop(ira, source_instruction);
17749 }
1622517750 if (tld_var->extern_lib_name != nullptr) {
1622617751 add_link_lib_symbol(ira, tld_var->extern_lib_name, &var->name, source_instruction->source_node);
1622717752 }
......@@ -16237,23 +17762,13 @@ static IrInstruction *ir_analyze_decl_ref(IrAnalyze *ira, IrInstruction *source_
1623717762 if (type_is_invalid(fn_entry->type_entry))
1623817763 return ira->codegen->invalid_instruction;
1623917764
16240 // TODO instead of allocating this every time, put it in the tld value and we can reference
16241 // the same one every time
16242 ConstExprValue *const_val = create_const_vals(1);
16243 const_val->special = ConstValSpecialStatic;
16244 const_val->type = fn_entry->type_entry;
16245 const_val->data.x_ptr.data.fn.fn_entry = fn_entry;
16246 const_val->data.x_ptr.special = ConstPtrSpecialFunction;
16247 const_val->data.x_ptr.mut = ConstPtrMutComptimeConst;
16248
1624917765 if (tld_fn->extern_lib_name != nullptr) {
1625017766 add_link_lib_symbol(ira, tld_fn->extern_lib_name, &fn_entry->symbol_name, source_instruction->source_node);
1625117767 }
1625217768
16253 bool ptr_is_const = true;
16254 bool ptr_is_volatile = false;
16255 return ir_get_const_ptr(ira, source_instruction, const_val, fn_entry->type_entry,
16256 ConstPtrMutComptimeConst, ptr_is_const, ptr_is_volatile, 0);
17769 IrInstruction *fn_inst = ir_create_const_fn(&ira->new_irb, source_instruction->scope,
17770 source_instruction->source_node, fn_entry);
17771 return ir_get_ref(ira, source_instruction, fn_inst, true, false);
1625717772 }
1625817773 }
1625917774 zig_unreachable();
......@@ -16295,14 +17810,14 @@ static IrInstruction *ir_analyze_instruction_field_ptr(IrAnalyze *ira, IrInstruc
1629517810 assert(container_ptr->value.type->id == ZigTypeIdPointer);
1629617811 if (container_type->id == ZigTypeIdPointer) {
1629717812 ZigType *bare_type = container_ref_type(container_type);
16298 IrInstruction *container_child = ir_get_deref(ira, &field_ptr_instruction->base, container_ptr);
16299 IrInstruction *result = ir_analyze_container_field_ptr(ira, field_name, &field_ptr_instruction->base, container_child, bare_type);
17813 IrInstruction *container_child = ir_get_deref(ira, &field_ptr_instruction->base, container_ptr, nullptr);
17814 IrInstruction *result = ir_analyze_container_field_ptr(ira, field_name, &field_ptr_instruction->base, container_child, bare_type, field_ptr_instruction->initializing);
1630017815 return result;
1630117816 } else {
16302 IrInstruction *result = ir_analyze_container_field_ptr(ira, field_name, &field_ptr_instruction->base, container_ptr, container_type);
17817 IrInstruction *result = ir_analyze_container_field_ptr(ira, field_name, &field_ptr_instruction->base, container_ptr, container_type, field_ptr_instruction->initializing);
1630317818 return result;
1630417819 }
16305 } else if (is_array_ref(container_type)) {
17820 } else if (is_array_ref(container_type) && !field_ptr_instruction->initializing) {
1630617821 if (buf_eql_str(field_name, "len")) {
1630717822 ConstExprValue *len_val = create_const_vals(1);
1630817823 if (container_type->id == ZigTypeIdPointer) {
......@@ -16617,6 +18132,10 @@ static IrInstruction *ir_analyze_instruction_field_ptr(IrAnalyze *ira, IrInstruc
1661718132 buf_sprintf("type '%s' does not support field access", buf_ptr(&child_type->name)));
1661818133 return ira->codegen->invalid_instruction;
1661918134 }
18135 } else if (field_ptr_instruction->initializing) {
18136 ir_add_error(ira, &field_ptr_instruction->base,
18137 buf_sprintf("type '%s' does not support struct initialization syntax", buf_ptr(&container_type->name)));
18138 return ira->codegen->invalid_instruction;
1662018139 } else {
1662118140 ir_add_error_node(ira, field_ptr_instruction->base.source_node,
1662218141 buf_sprintf("type '%s' does not support field access", buf_ptr(&container_type->name)));
......@@ -16640,7 +18159,7 @@ static IrInstruction *ir_analyze_instruction_load_ptr(IrAnalyze *ira, IrInstruct
1664018159 IrInstruction *ptr = instruction->ptr->child;
1664118160 if (type_is_invalid(ptr->value.type))
1664218161 return ira->codegen->invalid_instruction;
16643 return ir_get_deref(ira, &instruction->base, ptr);
18162 return ir_get_deref(ira, &instruction->base, ptr, nullptr);
1664418163}
1664518164
1664618165static IrInstruction *ir_analyze_instruction_typeof(IrAnalyze *ira, IrInstructionTypeOf *typeof_instruction) {
......@@ -16651,64 +18170,6 @@ static IrInstruction *ir_analyze_instruction_typeof(IrAnalyze *ira, IrInstructio
1665118170 return ir_const_type(ira, &typeof_instruction->base, type_entry);
1665218171}
1665318172
16654static IrInstruction *ir_analyze_instruction_to_ptr_type(IrAnalyze *ira,
16655 IrInstructionToPtrType *to_ptr_type_instruction)
16656{
16657 Error err;
16658 IrInstruction *ptr_ptr = to_ptr_type_instruction->ptr->child;
16659 if (type_is_invalid(ptr_ptr->value.type))
16660 return ira->codegen->invalid_instruction;
16661
16662 ZigType *ptr_ptr_type = ptr_ptr->value.type;
16663 assert(ptr_ptr_type->id == ZigTypeIdPointer);
16664 ZigType *type_entry = ptr_ptr_type->data.pointer.child_type;
16665
16666 ZigType *ptr_type;
16667 if (type_entry->id == ZigTypeIdArray) {
16668 ptr_type = get_pointer_to_type(ira->codegen, type_entry->data.array.child_type, ptr_ptr_type->data.pointer.is_const);
16669 } else if (is_array_ref(type_entry)) {
16670 ptr_type = get_pointer_to_type(ira->codegen,
16671 type_entry->data.pointer.child_type->data.array.child_type, type_entry->data.pointer.is_const);
16672 } else if (is_slice(type_entry)) {
16673 ZigType *slice_ptr_type = type_entry->data.structure.fields[0].type_entry;
16674 ptr_type = adjust_ptr_len(ira->codegen, slice_ptr_type, PtrLenSingle);
16675 // If the pointer is over-aligned, we may have to reduce it based on the alignment of the element type.
16676 if (slice_ptr_type->data.pointer.explicit_alignment != 0) {
16677 ZigType *elem_type = slice_ptr_type->data.pointer.child_type;
16678 if ((err = type_resolve(ira->codegen, elem_type, ResolveStatusAlignmentKnown)))
16679 return ira->codegen->invalid_instruction;
16680 uint32_t elem_align = get_abi_alignment(ira->codegen, elem_type);
16681 uint32_t reduced_align = min(elem_align, slice_ptr_type->data.pointer.explicit_alignment);
16682 ptr_type = adjust_ptr_align(ira->codegen, ptr_type, reduced_align);
16683 }
16684 } else if (type_entry->id == ZigTypeIdArgTuple) {
16685 zig_panic("TODO for loop on var args");
16686 } else {
16687 ir_add_error_node(ira, to_ptr_type_instruction->base.source_node,
16688 buf_sprintf("expected array type, found '%s'", buf_ptr(&type_entry->name)));
16689 return ira->codegen->invalid_instruction;
16690 }
16691
16692 return ir_const_type(ira, &to_ptr_type_instruction->base, ptr_type);
16693}
16694
16695static IrInstruction *ir_analyze_instruction_ptr_type_child(IrAnalyze *ira,
16696 IrInstructionPtrTypeChild *ptr_type_child_instruction)
16697{
16698 IrInstruction *type_value = ptr_type_child_instruction->value->child;
16699 ZigType *type_entry = ir_resolve_type(ira, type_value);
16700 if (type_is_invalid(type_entry))
16701 return ira->codegen->invalid_instruction;
16702
16703 if (type_entry->id != ZigTypeIdPointer) {
16704 ir_add_error_node(ira, ptr_type_child_instruction->base.source_node,
16705 buf_sprintf("expected pointer type, found '%s'", buf_ptr(&type_entry->name)));
16706 return ira->codegen->invalid_instruction;
16707 }
16708
16709 return ir_const_type(ira, &ptr_type_child_instruction->base, type_entry->data.pointer.child_type);
16710}
16711
1671218173static IrInstruction *ir_analyze_instruction_set_cold(IrAnalyze *ira, IrInstructionSetCold *instruction) {
1671318174 if (ira->new_irb.exec->is_inline) {
1671418175 // ignore setCold when running functions at compile time
......@@ -17138,7 +18599,7 @@ static IrInstruction *ir_analyze_instruction_test_non_null(IrAnalyze *ira, IrIns
1713818599}
1713918600
1714018601static IrInstruction *ir_analyze_unwrap_optional_payload(IrAnalyze *ira, IrInstruction *source_instr,
17141 IrInstruction *base_ptr, bool safety_check_on)
18602 IrInstruction *base_ptr, bool safety_check_on, bool initializing)
1714218603{
1714318604 ZigType *ptr_type = base_ptr->value.type;
1714418605 assert(ptr_type->id == ZigTypeIdPointer);
......@@ -17168,7 +18629,7 @@ static IrInstruction *ir_analyze_unwrap_optional_payload(IrAnalyze *ira, IrInstr
1716818629 }
1716918630 if (!safety_check_on)
1717018631 return base_ptr;
17171 IrInstruction *c_ptr_val = ir_get_deref(ira, source_instr, base_ptr);
18632 IrInstruction *c_ptr_val = ir_get_deref(ira, source_instr, base_ptr, nullptr);
1717218633 ir_build_assert_non_null(ira, source_instr, c_ptr_val);
1717318634 return base_ptr;
1717418635 }
......@@ -17183,34 +18644,84 @@ static IrInstruction *ir_analyze_unwrap_optional_payload(IrAnalyze *ira, IrInstr
1718318644 ZigType *result_type = get_pointer_to_type_extra(ira->codegen, child_type,
1718418645 ptr_type->data.pointer.is_const, ptr_type->data.pointer.is_volatile, PtrLenSingle, 0, 0, 0, false);
1718518646
18647 bool same_comptime_repr = types_have_same_zig_comptime_repr(type_entry, child_type);
18648
1718618649 if (instr_is_comptime(base_ptr)) {
17187 ConstExprValue *val = ir_resolve_const(ira, base_ptr, UndefBad);
17188 if (!val)
18650 ConstExprValue *ptr_val = ir_resolve_const(ira, base_ptr, UndefBad);
18651 if (!ptr_val)
1718918652 return ira->codegen->invalid_instruction;
17190 if (val->data.x_ptr.mut != ConstPtrMutRuntimeVar) {
17191 ConstExprValue *maybe_val = const_ptr_pointee(ira, ira->codegen, val, source_instr->source_node);
17192 if (maybe_val == nullptr)
18653 if (ptr_val->data.x_ptr.mut != ConstPtrMutRuntimeVar) {
18654 ConstExprValue *optional_val = const_ptr_pointee(ira, ira->codegen, ptr_val, source_instr->source_node);
18655 if (optional_val == nullptr)
1719318656 return ira->codegen->invalid_instruction;
1719418657
17195 if (optional_value_is_null(maybe_val)) {
18658 if (initializing && optional_val->special == ConstValSpecialUndef) {
18659 switch (type_has_one_possible_value(ira->codegen, child_type)) {
18660 case OnePossibleValueInvalid:
18661 return ira->codegen->invalid_instruction;
18662 case OnePossibleValueNo:
18663 if (!same_comptime_repr) {
18664 ConstExprValue *payload_val = create_const_vals(1);
18665 payload_val->type = child_type;
18666 payload_val->special = ConstValSpecialUndef;
18667 payload_val->parent.id = ConstParentIdOptionalPayload;
18668 payload_val->parent.data.p_optional_payload.optional_val = optional_val;
18669
18670 optional_val->data.x_optional = payload_val;
18671 optional_val->special = ConstValSpecialStatic;
18672 }
18673 break;
18674 case OnePossibleValueYes: {
18675 ConstExprValue *pointee = create_const_vals(1);
18676 pointee->special = ConstValSpecialStatic;
18677 pointee->type = child_type;
18678 pointee->parent.id = ConstParentIdOptionalPayload;
18679 pointee->parent.data.p_optional_payload.optional_val = optional_val;
18680
18681 optional_val->special = ConstValSpecialStatic;
18682 optional_val->data.x_optional = pointee;
18683 break;
18684 }
18685 }
18686 } else if (optional_value_is_null(optional_val)) {
1719618687 ir_add_error(ira, source_instr, buf_sprintf("unable to unwrap null"));
1719718688 return ira->codegen->invalid_instruction;
1719818689 }
17199 IrInstruction *result = ir_const(ira, source_instr, result_type);
17200 ConstExprValue *out_val = &result->value;
17201 out_val->data.x_ptr.special = ConstPtrSpecialRef;
17202 out_val->data.x_ptr.mut = val->data.x_ptr.mut;
17203 if (types_have_same_zig_comptime_repr(type_entry, child_type)) {
17204 out_val->data.x_ptr.data.ref.pointee = maybe_val;
18690
18691 IrInstruction *result;
18692 if (ptr_val->data.x_ptr.mut == ConstPtrMutInfer) {
18693 result = ir_build_optional_unwrap_ptr(&ira->new_irb, source_instr->scope,
18694 source_instr->source_node, base_ptr, false, initializing);
18695 result->value.type = result_type;
18696 result->value.special = ConstValSpecialStatic;
1720518697 } else {
17206 out_val->data.x_ptr.data.ref.pointee = maybe_val->data.x_optional;
18698 result = ir_const(ira, source_instr, result_type);
18699 }
18700 ConstExprValue *result_val = &result->value;
18701 result_val->data.x_ptr.special = ConstPtrSpecialRef;
18702 result_val->data.x_ptr.mut = ptr_val->data.x_ptr.mut;
18703 switch (type_has_one_possible_value(ira->codegen, child_type)) {
18704 case OnePossibleValueInvalid:
18705 return ira->codegen->invalid_instruction;
18706 case OnePossibleValueNo:
18707 if (same_comptime_repr) {
18708 result_val->data.x_ptr.data.ref.pointee = optional_val;
18709 } else {
18710 assert(optional_val->data.x_optional != nullptr);
18711 result_val->data.x_ptr.data.ref.pointee = optional_val->data.x_optional;
18712 }
18713 break;
18714 case OnePossibleValueYes:
18715 assert(optional_val->data.x_optional != nullptr);
18716 result_val->data.x_ptr.data.ref.pointee = optional_val->data.x_optional;
18717 break;
1720718718 }
1720818719 return result;
1720918720 }
1721018721 }
1721118722
1721218723 IrInstruction *result = ir_build_optional_unwrap_ptr(&ira->new_irb, source_instr->scope,
17213 source_instr->source_node, base_ptr, safety_check_on);
18724 source_instr->source_node, base_ptr, safety_check_on, initializing);
1721418725 result->value.type = result_type;
1721518726 return result;
1721618727}
......@@ -17222,7 +18733,8 @@ static IrInstruction *ir_analyze_instruction_optional_unwrap_ptr(IrAnalyze *ira,
1722218733 if (type_is_invalid(base_ptr->value.type))
1722318734 return ira->codegen->invalid_instruction;
1722418735
17225 return ir_analyze_unwrap_optional_payload(ira, &instruction->base, base_ptr, instruction->safety_check_on);
18736 return ir_analyze_unwrap_optional_payload(ira, &instruction->base, base_ptr,
18737 instruction->safety_check_on, false);
1722618738}
1722718739
1722818740static IrInstruction *ir_analyze_instruction_ctz(IrAnalyze *ira, IrInstructionCtz *instruction) {
......@@ -17523,7 +19035,7 @@ static IrInstruction *ir_analyze_instruction_switch_target(IrAnalyze *ira,
1752319035 return result;
1752419036 }
1752519037
17526 IrInstruction *result = ir_get_deref(ira, &switch_target_instruction->base, target_value_ptr);
19038 IrInstruction *result = ir_get_deref(ira, &switch_target_instruction->base, target_value_ptr, nullptr);
1752719039 result->value.type = target_type;
1752819040 return result;
1752919041 }
......@@ -17553,7 +19065,7 @@ static IrInstruction *ir_analyze_instruction_switch_target(IrAnalyze *ira,
1755319065 return result;
1755419066 }
1755519067
17556 IrInstruction *union_value = ir_get_deref(ira, &switch_target_instruction->base, target_value_ptr);
19068 IrInstruction *union_value = ir_get_deref(ira, &switch_target_instruction->base, target_value_ptr, nullptr);
1755719069 union_value->value.type = target_type;
1755819070
1755919071 IrInstruction *union_tag_inst = ir_build_union_tag(&ira->new_irb, switch_target_instruction->base.scope,
......@@ -17577,7 +19089,7 @@ static IrInstruction *ir_analyze_instruction_switch_target(IrAnalyze *ira,
1757719089 return result;
1757819090 }
1757919091
17580 IrInstruction *enum_value = ir_get_deref(ira, &switch_target_instruction->base, target_value_ptr);
19092 IrInstruction *enum_value = ir_get_deref(ira, &switch_target_instruction->base, target_value_ptr, nullptr);
1758119093 enum_value->value.type = target_type;
1758219094 return enum_value;
1758319095 }
......@@ -17650,7 +19162,7 @@ static IrInstruction *ir_analyze_instruction_switch_var(IrAnalyze *ira, IrInstru
1765019162 }
1765119163
1765219164 IrInstruction *result = ir_build_union_field_ptr(&ira->new_irb,
17653 instruction->base.scope, instruction->base.source_node, target_value_ptr, field);
19165 instruction->base.scope, instruction->base.source_node, target_value_ptr, field, false, false);
1765419166 result->value.type = get_pointer_to_type(ira->codegen, field->type_entry,
1765519167 target_value_ptr->value.type->data.pointer.is_const);
1765619168 return result;
......@@ -17860,7 +19372,8 @@ static IrInstruction *ir_analyze_instruction_ref(IrAnalyze *ira, IrInstructionRe
1786019372}
1786119373
1786219374static IrInstruction *ir_analyze_container_init_fields_union(IrAnalyze *ira, IrInstruction *instruction,
17863 ZigType *container_type, size_t instr_field_count, IrInstructionContainerInitFieldsField *fields)
19375 ZigType *container_type, size_t instr_field_count, IrInstructionContainerInitFieldsField *fields,
19376 IrInstruction *result_loc)
1786419377{
1786519378 Error err;
1786619379 assert(container_type->id == ZigTypeIdUnion);
......@@ -17875,12 +19388,12 @@ static IrInstruction *ir_analyze_container_init_fields_union(IrAnalyze *ira, IrI
1787519388 }
1787619389
1787719390 IrInstructionContainerInitFieldsField *field = &fields[0];
17878 IrInstruction *field_value = field->value->child;
17879 if (type_is_invalid(field_value->value.type))
19391 IrInstruction *field_result_loc = field->result_loc->child;
19392 if (type_is_invalid(field_result_loc->value.type))
1788019393 return ira->codegen->invalid_instruction;
1788119394
1788219395 TypeUnionField *type_field = find_union_type_field(container_type, field->name);
17883 if (!type_field) {
19396 if (type_field == nullptr) {
1788419397 ir_add_error_node(ira, field->source_node,
1788519398 buf_sprintf("no member named '%s' in union '%s'",
1788619399 buf_ptr(field->name), buf_ptr(&container_type->name)));
......@@ -17890,46 +19403,36 @@ static IrInstruction *ir_analyze_container_init_fields_union(IrAnalyze *ira, IrI
1789019403 if (type_is_invalid(type_field->type_entry))
1789119404 return ira->codegen->invalid_instruction;
1789219405
17893 IrInstruction *casted_field_value = ir_implicit_cast(ira, field_value, type_field->type_entry);
17894 if (casted_field_value == ira->codegen->invalid_instruction)
17895 return ira->codegen->invalid_instruction;
17896
17897 if ((err = type_resolve(ira->codegen, casted_field_value->value.type, ResolveStatusZeroBitsKnown)))
17898 return ira->codegen->invalid_instruction;
19406 if (result_loc->value.data.x_ptr.mut == ConstPtrMutInfer) {
19407 if (instr_is_comptime(field_result_loc) &&
19408 field_result_loc->value.data.x_ptr.mut != ConstPtrMutRuntimeVar)
19409 {
19410 // nothing
19411 } else {
19412 result_loc->value.special = ConstValSpecialRuntime;
19413 }
19414 }
1789919415
1790019416 bool is_comptime = ir_should_inline(ira->new_irb.exec, instruction->scope)
1790119417 || type_requires_comptime(ira->codegen, container_type) == ReqCompTimeYes;
17902 if (is_comptime || casted_field_value->value.special != ConstValSpecialRuntime ||
17903 !type_has_bits(casted_field_value->value.type))
17904 {
17905 ConstExprValue *field_val = ir_resolve_const(ira, casted_field_value, UndefOk);
17906 if (!field_val)
17907 return ira->codegen->invalid_instruction;
17908
17909 IrInstruction *result = ir_const(ira, instruction, container_type);
17910 ConstExprValue *out_val = &result->value;
17911 out_val->data.x_union.payload = field_val;
17912 out_val->data.x_union.tag = type_field->enum_field->value;
17913 out_val->parent.id = ConstParentIdUnion;
17914 out_val->parent.data.p_union.union_val = out_val;
1791519418
17916 return result;
19419 IrInstruction *result = ir_get_deref(ira, instruction, result_loc, nullptr);
19420 if (is_comptime && !instr_is_comptime(result)) {
19421 ir_add_error(ira, field->result_loc,
19422 buf_sprintf("unable to evaluate constant expression"));
19423 return ira->codegen->invalid_instruction;
1791719424 }
17918
17919 IrInstruction *new_instruction = ir_build_union_init(&ira->new_irb,
17920 instruction->scope, instruction->source_node,
17921 container_type, type_field, casted_field_value);
17922 new_instruction->value.type = container_type;
17923 ir_add_alloca(ira, new_instruction, container_type);
17924 return new_instruction;
19425 return result;
1792519426}
1792619427
1792719428static IrInstruction *ir_analyze_container_init_fields(IrAnalyze *ira, IrInstruction *instruction,
17928 ZigType *container_type, size_t instr_field_count, IrInstructionContainerInitFieldsField *fields)
19429 ZigType *container_type, size_t instr_field_count, IrInstructionContainerInitFieldsField *fields,
19430 IrInstruction *result_loc)
1792919431{
1793019432 Error err;
1793119433 if (container_type->id == ZigTypeIdUnion) {
17932 return ir_analyze_container_init_fields_union(ira, instruction, container_type, instr_field_count, fields);
19434 return ir_analyze_container_init_fields_union(ira, instruction, container_type, instr_field_count,
19435 fields, result_loc);
1793319436 }
1793419437 if (container_type->id != ZigTypeIdStruct || is_slice(container_type)) {
1793519438 ir_add_error(ira, instruction,
......@@ -17946,22 +19449,28 @@ static IrInstruction *ir_analyze_container_init_fields(IrAnalyze *ira, IrInstruc
1794619449 IrInstruction *first_non_const_instruction = nullptr;
1794719450
1794819451 AstNode **field_assign_nodes = allocate<AstNode *>(actual_field_count);
17949
17950 IrInstructionStructInitField *new_fields = allocate<IrInstructionStructInitField>(actual_field_count);
19452 ZigList<IrInstruction *> const_ptrs = {};
1795119453
1795219454 bool is_comptime = ir_should_inline(ira->new_irb.exec, instruction->scope)
1795319455 || type_requires_comptime(ira->codegen, container_type) == ReqCompTimeYes;
1795419456
17955 ConstExprValue const_val = {};
17956 const_val.special = ConstValSpecialStatic;
17957 const_val.type = container_type;
17958 // const_val.global_refs = allocate<ConstGlobalRefs>(1);
17959 const_val.data.x_struct.fields = create_const_vals(actual_field_count);
19457
19458 // Here we iterate over the fields that have been initialized, and emit
19459 // compile errors for missing fields and duplicate fields.
19460 // It is only now that we find out whether the struct initialization can be a comptime
19461 // value, but we have already emitted runtime instructions for the fields that
19462 // were initialized with runtime values, and have omitted instructions that would have
19463 // initialized fields with comptime values.
19464 // So now we must clean up this situation. If it turns out the struct initialization can
19465 // be a comptime value, overwrite ConstPtrMutInfer with ConstPtrMutComptimeConst.
19466 // Otherwise, we must emit instructions to runtime-initialize the fields that have
19467 // comptime-known values.
19468
1796019469 for (size_t i = 0; i < instr_field_count; i += 1) {
1796119470 IrInstructionContainerInitFieldsField *field = &fields[i];
1796219471
17963 IrInstruction *field_value = field->value->child;
17964 if (type_is_invalid(field_value->value.type))
19472 IrInstruction *field_result_loc = field->result_loc->child;
19473 if (type_is_invalid(field_result_loc->value.type))
1796519474 return ira->codegen->invalid_instruction;
1796619475
1796719476 TypeStructField *type_field = find_struct_type_field(container_type, field->name);
......@@ -17975,10 +19484,6 @@ static IrInstruction *ir_analyze_container_init_fields(IrAnalyze *ira, IrInstruc
1797519484 if (type_is_invalid(type_field->type_entry))
1797619485 return ira->codegen->invalid_instruction;
1797719486
17978 IrInstruction *casted_field_value = ir_implicit_cast(ira, field_value, type_field->type_entry);
17979 if (casted_field_value == ira->codegen->invalid_instruction)
17980 return ira->codegen->invalid_instruction;
17981
1798219487 size_t field_index = type_field->src_index;
1798319488 AstNode *existing_assign_node = field_assign_nodes[field_index];
1798419489 if (existing_assign_node) {
......@@ -17988,26 +19493,18 @@ static IrInstruction *ir_analyze_container_init_fields(IrAnalyze *ira, IrInstruc
1798819493 }
1798919494 field_assign_nodes[field_index] = field->source_node;
1799019495
17991 new_fields[field_index].value = casted_field_value;
17992 new_fields[field_index].type_struct_field = type_field;
17993
17994 if (const_val.special == ConstValSpecialStatic) {
17995 if (is_comptime || casted_field_value->value.special != ConstValSpecialRuntime) {
17996 ConstExprValue *field_val = ir_resolve_const(ira, casted_field_value, UndefOk);
17997 if (!field_val)
17998 return ira->codegen->invalid_instruction;
17999
18000 copy_const_val(&const_val.data.x_struct.fields[field_index], field_val, true);
18001 } else {
18002 first_non_const_instruction = casted_field_value;
18003 const_val.special = ConstValSpecialRuntime;
18004 }
19496 if (instr_is_comptime(field_result_loc) &&
19497 field_result_loc->value.data.x_ptr.mut != ConstPtrMutRuntimeVar)
19498 {
19499 const_ptrs.append(field_result_loc);
19500 } else {
19501 first_non_const_instruction = field_result_loc;
1800519502 }
1800619503 }
1800719504
1800819505 bool any_missing = false;
1800919506 for (size_t i = 0; i < actual_field_count; i += 1) {
18010 if (field_assign_nodes[i]) continue;
19507 if (field_assign_nodes[i] != nullptr) continue;
1801119508
1801219509 // look for a default field value
1801319510 TypeStructField *field = &container_type->data.structure.fields[i];
......@@ -18033,182 +19530,177 @@ static IrInstruction *ir_analyze_container_init_fields(IrAnalyze *ira, IrInstruc
1803319530 IrInstruction *runtime_inst = ir_const(ira, instruction, field->init_val->type);
1803419531 copy_const_val(&runtime_inst->value, field->init_val, true);
1803519532
18036 new_fields[i].value = runtime_inst;
18037 new_fields[i].type_struct_field = field;
18038
18039 if (const_val.special == ConstValSpecialStatic) {
18040 copy_const_val(&const_val.data.x_struct.fields[i], field->init_val, true);
19533 IrInstruction *field_ptr = ir_analyze_struct_field_ptr(ira, instruction, field, result_loc,
19534 container_type, true);
19535 ir_analyze_store_ptr(ira, instruction, field_ptr, runtime_inst);
19536 if (instr_is_comptime(field_ptr) && field_ptr->value.data.x_ptr.mut != ConstPtrMutRuntimeVar) {
19537 const_ptrs.append(field_ptr);
19538 } else {
19539 first_non_const_instruction = result_loc;
1804119540 }
1804219541 }
1804319542 if (any_missing)
1804419543 return ira->codegen->invalid_instruction;
1804519544
18046 if (const_val.special == ConstValSpecialStatic) {
18047 IrInstruction *result = ir_const(ira, instruction, nullptr);
18048 ConstExprValue *out_val = &result->value;
18049 copy_const_val(out_val, &const_val, false);
18050 out_val->type = container_type;
18051
18052 for (size_t i = 0; i < instr_field_count; i += 1) {
18053 ConstExprValue *field_val = &out_val->data.x_struct.fields[i];
18054 ConstParent *parent = get_const_val_parent(ira->codegen, field_val);
18055 if (parent != nullptr) {
18056 parent->id = ConstParentIdStruct;
18057 parent->data.p_struct.field_index = i;
18058 parent->data.p_struct.struct_val = out_val;
19545 if (result_loc->value.data.x_ptr.mut == ConstPtrMutInfer) {
19546 if (const_ptrs.length != actual_field_count) {
19547 result_loc->value.special = ConstValSpecialRuntime;
19548 for (size_t i = 0; i < const_ptrs.length; i += 1) {
19549 IrInstruction *field_result_loc = const_ptrs.at(i);
19550 IrInstruction *deref = ir_get_deref(ira, field_result_loc, field_result_loc, nullptr);
19551 field_result_loc->value.special = ConstValSpecialRuntime;
19552 ir_analyze_store_ptr(ira, field_result_loc, field_result_loc, deref);
1805919553 }
1806019554 }
18061
18062 return result;
1806319555 }
1806419556
18065 if (is_comptime) {
19557 IrInstruction *result = ir_get_deref(ira, instruction, result_loc, nullptr);
19558
19559 if (is_comptime && !instr_is_comptime(result)) {
1806619560 ir_add_error_node(ira, first_non_const_instruction->source_node,
1806719561 buf_sprintf("unable to evaluate constant expression"));
1806819562 return ira->codegen->invalid_instruction;
1806919563 }
1807019564
18071 IrInstruction *new_instruction = ir_build_struct_init(&ira->new_irb,
18072 instruction->scope, instruction->source_node,
18073 container_type, actual_field_count, new_fields);
18074 new_instruction->value.type = container_type;
18075 ir_add_alloca(ira, new_instruction, container_type);
18076 return new_instruction;
19565 return result;
1807719566}
1807819567
1807919568static IrInstruction *ir_analyze_instruction_container_init_list(IrAnalyze *ira,
1808019569 IrInstructionContainerInitList *instruction)
1808119570{
18082 Error err;
19571 ZigType *container_type = ir_resolve_type(ira, instruction->container_type->child);
19572 if (type_is_invalid(container_type))
19573 return ira->codegen->invalid_instruction;
1808319574
1808419575 size_t elem_count = instruction->item_count;
1808519576
18086 ZigType *container_type;
18087 if (instruction->container_type != nullptr) {
18088 container_type = ir_resolve_type(ira, instruction->container_type->child);
18089 if (type_is_invalid(container_type))
18090 return ira->codegen->invalid_instruction;
18091 } else {
18092 ZigType *elem_type = ir_resolve_type(ira, instruction->elem_type->child);
18093 if (type_is_invalid(elem_type))
18094 return ira->codegen->invalid_instruction;
18095 if ((err = type_resolve(ira->codegen, elem_type, ResolveStatusSizeKnown))) {
18096 return ira->codegen->invalid_instruction;
18097 }
18098 container_type = get_array_type(ira->codegen, elem_type, elem_count);
18099 }
18100
1810119577 if (is_slice(container_type)) {
1810219578 ir_add_error(ira, &instruction->base,
1810319579 buf_sprintf("expected array type or [_], found slice"));
1810419580 return ira->codegen->invalid_instruction;
18105 } else if (container_type->id == ZigTypeIdStruct && !is_slice(container_type) && elem_count == 0) {
18106 return ir_analyze_container_init_fields(ira, &instruction->base, container_type,
18107 0, nullptr);
18108 } else if (container_type->id == ZigTypeIdArray) {
18109 // array is same as slice init but we make a compile error if the length is wrong
18110 ZigType *child_type;
18111 if (container_type->id == ZigTypeIdArray) {
18112 child_type = container_type->data.array.child_type;
18113 if (container_type->data.array.len != elem_count) {
18114 ZigType *literal_type = get_array_type(ira->codegen, child_type, elem_count);
18115
18116 ir_add_error(ira, &instruction->base,
18117 buf_sprintf("expected %s literal, found %s literal",
18118 buf_ptr(&container_type->name), buf_ptr(&literal_type->name)));
18119 return ira->codegen->invalid_instruction;
18120 }
18121 } else {
18122 ZigType *pointer_type = container_type->data.structure.fields[slice_ptr_index].type_entry;
18123 assert(pointer_type->id == ZigTypeIdPointer);
18124 child_type = pointer_type->data.pointer.child_type;
18125 }
19581 }
1812619582
18127 if ((err = type_resolve(ira->codegen, child_type, ResolveStatusSizeKnown))) {
19583 if (container_type->id == ZigTypeIdVoid) {
19584 if (elem_count != 0) {
19585 ir_add_error_node(ira, instruction->base.source_node,
19586 buf_sprintf("void expression expects no arguments"));
1812819587 return ira->codegen->invalid_instruction;
1812919588 }
19589 return ir_const_void(ira, &instruction->base);
19590 }
1813019591
18131 ZigType *fixed_size_array_type = get_array_type(ira->codegen, child_type, elem_count);
19592 if (container_type->id == ZigTypeIdStruct && elem_count == 0) {
19593 ir_assert(instruction->result_loc != nullptr, &instruction->base);
19594 IrInstruction *result_loc = instruction->result_loc->child;
19595 if (type_is_invalid(result_loc->value.type))
19596 return result_loc;
19597 return ir_analyze_container_init_fields(ira, &instruction->base, container_type, 0, nullptr, result_loc);
19598 }
19599
19600 if (container_type->id != ZigTypeIdArray) {
19601 ir_add_error_node(ira, instruction->base.source_node,
19602 buf_sprintf("type '%s' does not support array initialization",
19603 buf_ptr(&container_type->name)));
19604 return ira->codegen->invalid_instruction;
19605 }
1813219606
18133 ConstExprValue const_val = {};
18134 const_val.special = ConstValSpecialStatic;
18135 const_val.type = fixed_size_array_type;
18136 // const_val.global_refs = allocate<ConstGlobalRefs>(1);
18137 const_val.data.x_array.data.s_none.elements = create_const_vals(elem_count);
19607 ir_assert(instruction->result_loc != nullptr, &instruction->base);
19608 IrInstruction *result_loc = instruction->result_loc->child;
19609 if (type_is_invalid(result_loc->value.type))
19610 return result_loc;
19611 ir_assert(result_loc->value.type->id == ZigTypeIdPointer, &instruction->base);
1813819612
18139 bool is_comptime = ir_should_inline(ira->new_irb.exec, instruction->base.scope);
19613 ZigType *child_type = container_type->data.array.child_type;
19614 if (container_type->data.array.len != elem_count) {
19615 ZigType *literal_type = get_array_type(ira->codegen, child_type, elem_count);
1814019616
18141 IrInstruction **new_items = allocate<IrInstruction *>(elem_count);
19617 ir_add_error(ira, &instruction->base,
19618 buf_sprintf("expected %s literal, found %s literal",
19619 buf_ptr(&container_type->name), buf_ptr(&literal_type->name)));
19620 return ira->codegen->invalid_instruction;
19621 }
1814219622
18143 IrInstruction *first_non_const_instruction = nullptr;
19623 switch (type_has_one_possible_value(ira->codegen, container_type)) {
19624 case OnePossibleValueInvalid:
19625 return ira->codegen->invalid_instruction;
19626 case OnePossibleValueYes:
19627 return ir_const(ira, &instruction->base, container_type);
19628 case OnePossibleValueNo:
19629 break;
19630 }
1814419631
18145 for (size_t i = 0; i < elem_count; i += 1) {
18146 IrInstruction *arg_value = instruction->items[i]->child;
18147 if (type_is_invalid(arg_value->value.type))
18148 return ira->codegen->invalid_instruction;
19632 bool is_comptime;
19633 switch (type_requires_comptime(ira->codegen, container_type)) {
19634 case ReqCompTimeInvalid:
19635 return ira->codegen->invalid_instruction;
19636 case ReqCompTimeNo:
19637 is_comptime = ir_should_inline(ira->new_irb.exec, instruction->base.scope);
19638 break;
19639 case ReqCompTimeYes:
19640 is_comptime = true;
19641 break;
19642 }
1814919643
18150 IrInstruction *casted_arg = ir_implicit_cast(ira, arg_value, child_type);
18151 if (casted_arg == ira->codegen->invalid_instruction)
18152 return ira->codegen->invalid_instruction;
19644 IrInstruction *first_non_const_instruction = nullptr;
1815319645
18154 new_items[i] = casted_arg;
19646 // The Result Location Mechanism has already emitted runtime instructions to
19647 // initialize runtime elements and has omitted instructions for the comptime
19648 // elements. However it is only now that we find out whether the array initialization
19649 // can be a comptime value. So we must clean up the situation. If it turns out
19650 // array initialization can be a comptime value, overwrite ConstPtrMutInfer with
19651 // ConstPtrMutComptimeConst. Otherwise, emit instructions to runtime-initialize the
19652 // elements that have comptime-known values.
19653 ZigList<IrInstruction *> const_ptrs = {};
19654
19655 for (size_t i = 0; i < elem_count; i += 1) {
19656 IrInstruction *elem_result_loc = instruction->elem_result_loc_list[i]->child;
19657 if (type_is_invalid(elem_result_loc->value.type))
19658 return ira->codegen->invalid_instruction;
1815519659
18156 if (const_val.special == ConstValSpecialStatic) {
18157 if (is_comptime || casted_arg->value.special != ConstValSpecialRuntime) {
18158 ConstExprValue *elem_val = ir_resolve_const(ira, casted_arg, UndefBad);
18159 if (!elem_val)
18160 return ira->codegen->invalid_instruction;
19660 assert(elem_result_loc->value.type->id == ZigTypeIdPointer);
1816119661
18162 copy_const_val(&const_val.data.x_array.data.s_none.elements[i], elem_val, true);
18163 } else {
18164 first_non_const_instruction = casted_arg;
18165 const_val.special = ConstValSpecialRuntime;
18166 }
18167 }
19662 if (instr_is_comptime(elem_result_loc) &&
19663 elem_result_loc->value.data.x_ptr.mut != ConstPtrMutRuntimeVar)
19664 {
19665 const_ptrs.append(elem_result_loc);
19666 } else {
19667 first_non_const_instruction = elem_result_loc;
1816819668 }
19669 }
1816919670
18170 if (const_val.special == ConstValSpecialStatic) {
18171 IrInstruction *result = ir_const(ira, &instruction->base, nullptr);
18172 ConstExprValue *out_val = &result->value;
18173 copy_const_val(out_val, &const_val, false);
18174 result->value.type = fixed_size_array_type;
18175 for (size_t i = 0; i < elem_count; i += 1) {
18176 ConstExprValue *elem_val = &out_val->data.x_array.data.s_none.elements[i];
18177 ConstParent *parent = get_const_val_parent(ira->codegen, elem_val);
18178 if (parent != nullptr) {
18179 parent->id = ConstParentIdArray;
18180 parent->data.p_array.array_val = out_val;
18181 parent->data.p_array.elem_index = i;
18182 }
19671 if (result_loc->value.data.x_ptr.mut == ConstPtrMutInfer) {
19672 if (const_ptrs.length != elem_count) {
19673 result_loc->value.special = ConstValSpecialRuntime;
19674 for (size_t i = 0; i < const_ptrs.length; i += 1) {
19675 IrInstruction *elem_result_loc = const_ptrs.at(i);
19676 assert(elem_result_loc->value.special == ConstValSpecialStatic);
19677 IrInstruction *deref = ir_get_deref(ira, elem_result_loc, elem_result_loc, nullptr);
19678 elem_result_loc->value.special = ConstValSpecialRuntime;
19679 ir_analyze_store_ptr(ira, elem_result_loc, elem_result_loc, deref);
1818319680 }
18184 return result;
1818519681 }
19682 }
1818619683
18187 if (is_comptime) {
18188 ir_add_error_node(ira, first_non_const_instruction->source_node,
18189 buf_sprintf("unable to evaluate constant expression"));
18190 return ira->codegen->invalid_instruction;
18191 }
19684 IrInstruction *result = ir_get_deref(ira, &instruction->base, result_loc, nullptr);
19685 if (instr_is_comptime(result))
19686 return result;
1819219687
18193 IrInstruction *new_instruction = ir_build_container_init_list(&ira->new_irb,
18194 instruction->base.scope, instruction->base.source_node,
18195 nullptr, nullptr, elem_count, new_items);
18196 new_instruction->value.type = fixed_size_array_type;
18197 ir_add_alloca(ira, new_instruction, fixed_size_array_type);
18198 return new_instruction;
18199 } else if (container_type->id == ZigTypeIdVoid) {
18200 if (elem_count != 0) {
18201 ir_add_error_node(ira, instruction->base.source_node,
18202 buf_sprintf("void expression expects no arguments"));
18203 return ira->codegen->invalid_instruction;
18204 }
18205 return ir_const_void(ira, &instruction->base);
18206 } else {
18207 ir_add_error_node(ira, instruction->base.source_node,
18208 buf_sprintf("type '%s' does not support array initialization",
18209 buf_ptr(&container_type->name)));
19688 if (is_comptime) {
19689 ir_add_error_node(ira, first_non_const_instruction->source_node,
19690 buf_sprintf("unable to evaluate constant expression"));
19691 return ira->codegen->invalid_instruction;
19692 }
19693
19694 ZigType *result_elem_type = result_loc->value.type->data.pointer.child_type;
19695 if (is_slice(result_elem_type)) {
19696 ErrorMsg *msg = ir_add_error(ira, &instruction->base,
19697 buf_sprintf("runtime-initialized array cannot be casted to slice type '%s'",
19698 buf_ptr(&result_elem_type->name)));
19699 add_error_note(ira->codegen, msg, first_non_const_instruction->source_node,
19700 buf_sprintf("this value is not comptime-known"));
1821019701 return ira->codegen->invalid_instruction;
1821119702 }
19703 return result;
1821219704}
1821319705
1821419706static IrInstruction *ir_analyze_instruction_container_init_fields(IrAnalyze *ira,
......@@ -18219,8 +19711,13 @@ static IrInstruction *ir_analyze_instruction_container_init_fields(IrAnalyze *ir
1821919711 if (type_is_invalid(container_type))
1822019712 return ira->codegen->invalid_instruction;
1822119713
19714 ir_assert(instruction->result_loc != nullptr, &instruction->base);
19715 IrInstruction *result_loc = instruction->result_loc->child;
19716 if (type_is_invalid(result_loc->value.type))
19717 return result_loc;
19718
1822219719 return ir_analyze_container_init_fields(ira, &instruction->base, container_type,
18223 instruction->field_count, instruction->fields);
19720 instruction->field_count, instruction->fields, result_loc);
1822419721}
1822519722
1822619723static IrInstruction *ir_analyze_instruction_compile_err(IrAnalyze *ira,
......@@ -18489,12 +19986,6 @@ static IrInstruction *ir_analyze_instruction_bit_offset_of(IrAnalyze *ira,
1848919986 return ir_const_unsigned(ira, &instruction->base, bit_offset);
1849019987}
1849119988
18492static IrInstruction *ir_error_dependency_loop(IrAnalyze *ira, IrInstruction *source_instr) {
18493 ErrorMsg *msg = ir_add_error(ira, source_instr, buf_sprintf("dependency loop detected"));
18494 emit_error_notes_for_ref_stack(ira->codegen, msg);
18495 return ira->codegen->invalid_instruction;
18496}
18497
1849819989static void ensure_field_index(ZigType *type, const char *field_name, size_t index) {
1849919990 Buf *field_name_buf;
1850019991
......@@ -19613,7 +21104,7 @@ static IrInstruction *ir_analyze_instruction_c_import(IrAnalyze *ira, IrInstruct
1961321104 ir_add_error_node(ira, node, buf_sprintf("C import failed: unable to make dir: %s", err_str(err)));
1961421105 return ira->codegen->invalid_instruction;
1961521106 }
19616
21107
1961721108 if ((err = os_write_file(&tmp_c_file_path, &cimport_scope->buf))) {
1961821109 ir_add_error_node(ira, node, buf_sprintf("C import failed: unable to write .h file: %s", err_str(err)));
1961921110 return ira->codegen->invalid_instruction;
......@@ -19900,12 +21391,21 @@ static IrInstruction *ir_analyze_instruction_cmpxchg(IrAnalyze *ira, IrInstructi
1990021391 zig_panic("TODO compile-time execution of cmpxchg");
1990121392 }
1990221393
19903 IrInstruction *result = ir_build_cmpxchg_gen(ira, &instruction->base,
21394 ZigType *result_type = get_optional_type(ira->codegen, operand_type);
21395 IrInstruction *result_loc;
21396 if (handle_is_ptr(result_type)) {
21397 result_loc = ir_resolve_result(ira, &instruction->base, instruction->result_loc,
21398 result_type, nullptr, true, false);
21399 if (type_is_invalid(result_loc->value.type) || instr_is_unreachable(result_loc)) {
21400 return result_loc;
21401 }
21402 } else {
21403 result_loc = nullptr;
21404 }
21405
21406 return ir_build_cmpxchg_gen(ira, &instruction->base, result_type,
1990421407 casted_ptr, casted_cmp_value, casted_new_value,
19905 success_order, failure_order, instruction->is_weak);
19906 result->value.type = get_optional_type(ira->codegen, operand_type);
19907 ir_add_alloca(ira, result, result->value.type);
19908 return result;
21408 success_order, failure_order, instruction->is_weak, result_loc);
1990921409}
1991021410
1991121411static IrInstruction *ir_analyze_instruction_fence(IrAnalyze *ira, IrInstructionFence *instruction) {
......@@ -20043,7 +21543,7 @@ static IrInstruction *ir_analyze_instruction_float_cast(IrAnalyze *ira, IrInstru
2004321543 } else {
2004421544 op = CastOpNumLitToConcrete;
2004521545 }
20046 return ir_resolve_cast(ira, &instruction->base, target, dest_type, op, false);
21546 return ir_resolve_cast(ira, &instruction->base, target, dest_type, op);
2004721547 } else {
2004821548 return ira->codegen->invalid_instruction;
2004921549 }
......@@ -20151,6 +21651,12 @@ static IrInstruction *ir_analyze_instruction_from_bytes(IrAnalyze *ira, IrInstru
2015121651 }
2015221652 }
2015321653
21654 IrInstruction *result_loc = ir_resolve_result(ira, &instruction->base, instruction->result_loc,
21655 dest_slice_type, nullptr, true, false);
21656 if (result_loc != nullptr && (type_is_invalid(result_loc->value.type) || instr_is_unreachable(result_loc))) {
21657 return result_loc;
21658 }
21659
2015421660 if (casted_value->value.data.rh_slice.id == RuntimeHintSliceIdLen) {
2015521661 known_len = casted_value->value.data.rh_slice.len;
2015621662 have_known_len = true;
......@@ -20170,9 +21676,7 @@ static IrInstruction *ir_analyze_instruction_from_bytes(IrAnalyze *ira, IrInstru
2017021676 }
2017121677 }
2017221678
20173 IrInstruction *result = ir_build_resize_slice(ira, &instruction->base, casted_value, dest_slice_type);
20174 ir_add_alloca(ira, result, dest_slice_type);
20175 return result;
21679 return ir_build_resize_slice(ira, &instruction->base, casted_value, dest_slice_type, result_loc);
2017621680}
2017721681
2017821682static IrInstruction *ir_analyze_instruction_to_bytes(IrAnalyze *ira, IrInstructionToBytes *instruction) {
......@@ -20224,9 +21728,13 @@ static IrInstruction *ir_analyze_instruction_to_bytes(IrAnalyze *ira, IrInstruct
2022421728 return result;
2022521729 }
2022621730
20227 IrInstruction *result = ir_build_resize_slice(ira, &instruction->base, target, dest_slice_type);
20228 ir_add_alloca(ira, result, dest_slice_type);
20229 return result;
21731 IrInstruction *result_loc = ir_resolve_result(ira, &instruction->base, instruction->result_loc,
21732 dest_slice_type, nullptr, true, false);
21733 if (type_is_invalid(result_loc->value.type) || instr_is_unreachable(result_loc)) {
21734 return result_loc;
21735 }
21736
21737 return ir_build_resize_slice(ira, &instruction->base, target, dest_slice_type, result_loc);
2023021738}
2023121739
2023221740static Error resolve_ptr_align(IrAnalyze *ira, ZigType *ty, uint32_t *result_align) {
......@@ -20258,7 +21766,7 @@ static IrInstruction *ir_analyze_instruction_int_to_float(IrAnalyze *ira, IrInst
2025821766 return ira->codegen->invalid_instruction;
2025921767 }
2026021768
20261 return ir_resolve_cast(ira, &instruction->base, target, dest_type, CastOpIntToFloat, false);
21769 return ir_resolve_cast(ira, &instruction->base, target, dest_type, CastOpIntToFloat);
2026221770}
2026321771
2026421772static IrInstruction *ir_analyze_instruction_float_to_int(IrAnalyze *ira, IrInstructionFloatToInt *instruction) {
......@@ -20280,7 +21788,7 @@ static IrInstruction *ir_analyze_instruction_float_to_int(IrAnalyze *ira, IrInst
2028021788 return ira->codegen->invalid_instruction;
2028121789 }
2028221790
20283 return ir_resolve_cast(ira, &instruction->base, target, dest_type, CastOpFloatToInt, false);
21791 return ir_resolve_cast(ira, &instruction->base, target, dest_type, CastOpFloatToInt);
2028421792}
2028521793
2028621794static IrInstruction *ir_analyze_instruction_err_to_int(IrAnalyze *ira, IrInstructionErrToInt *instruction) {
......@@ -20332,7 +21840,7 @@ static IrInstruction *ir_analyze_instruction_bool_to_int(IrAnalyze *ira, IrInstr
2033221840 }
2033321841
2033421842 ZigType *u1_type = get_int_type(ira->codegen, false, 1);
20335 return ir_resolve_cast(ira, &instruction->base, target, u1_type, CastOpBoolToInt, false);
21843 return ir_resolve_cast(ira, &instruction->base, target, u1_type, CastOpBoolToInt);
2033621844}
2033721845
2033821846static IrInstruction *ir_analyze_instruction_int_type(IrAnalyze *ira, IrInstructionIntType *instruction) {
......@@ -20493,7 +22001,7 @@ static IrInstruction *ir_analyze_instruction_memset(IrAnalyze *ira, IrInstructio
2049322001
2049422002 ConstExprValue *byte_val = &casted_byte->value;
2049522003 for (size_t i = start; i < end; i += 1) {
20496 dest_elements[i] = *byte_val;
22004 copy_const_val(&dest_elements[i], byte_val, true);
2049722005 }
2049822006
2049922007 return ir_const_void(ira, &instruction->base);
......@@ -20563,7 +22071,7 @@ static IrInstruction *ir_analyze_instruction_memcpy(IrAnalyze *ira, IrInstructio
2056322071 return ira->codegen->invalid_instruction;
2056422072
2056522073 // TODO test this at comptime with u8 and non-u8 types
20566 // TODO test with dest ptr being a global runtime variable
22074 // TODO test with dest ptr being a global runtime variable
2056722075 if (casted_dest_ptr->value.special == ConstValSpecialStatic &&
2056822076 casted_src_ptr->value.special == ConstValSpecialStatic &&
2056922077 casted_count->value.special == ConstValSpecialStatic &&
......@@ -20661,7 +22169,7 @@ static IrInstruction *ir_analyze_instruction_memcpy(IrAnalyze *ira, IrInstructio
2066122169 // TODO check for noalias violations - this should be generalized to work for any function
2066222170
2066322171 for (size_t i = 0; i < count; i += 1) {
20664 dest_elements[dest_start + i] = src_elements[src_start + i];
22172 copy_const_val(&dest_elements[dest_start + i], &src_elements[src_start + i], true);
2066522173 }
2066622174
2066722175 return ir_const_void(ira, &instruction->base);
......@@ -20673,7 +22181,7 @@ static IrInstruction *ir_analyze_instruction_memcpy(IrAnalyze *ira, IrInstructio
2067322181 return result;
2067422182}
2067522183
20676static IrInstruction *ir_analyze_instruction_slice(IrAnalyze *ira, IrInstructionSlice *instruction) {
22184static IrInstruction *ir_analyze_instruction_slice(IrAnalyze *ira, IrInstructionSliceSrc *instruction) {
2067722185 IrInstruction *ptr_ptr = instruction->ptr->child;
2067822186 if (type_is_invalid(ptr_ptr->value.type))
2067922187 return ira->codegen->invalid_instruction;
......@@ -20962,12 +22470,13 @@ static IrInstruction *ir_analyze_instruction_slice(IrAnalyze *ira, IrInstruction
2096222470 return result;
2096322471 }
2096422472
20965 IrInstruction *new_instruction = ir_build_slice(&ira->new_irb,
20966 instruction->base.scope, instruction->base.source_node,
20967 ptr_ptr, casted_start, end, instruction->safety_check_on);
20968 new_instruction->value.type = return_type;
20969 ir_add_alloca(ira, new_instruction, return_type);
20970 return new_instruction;
22473 IrInstruction *result_loc = ir_resolve_result(ira, &instruction->base, instruction->result_loc,
22474 return_type, nullptr, true, false);
22475 if (type_is_invalid(result_loc->value.type) || instr_is_unreachable(result_loc)) {
22476 return result_loc;
22477 }
22478 return ir_build_slice_gen(ira, &instruction->base, return_type,
22479 ptr_ptr, casted_start, end, instruction->safety_check_on, result_loc);
2097122480}
2097222481
2097322482static IrInstruction *ir_analyze_instruction_member_count(IrAnalyze *ira, IrInstructionMemberCount *instruction) {
......@@ -21291,6 +22800,19 @@ static IrInstruction *ir_analyze_instruction_overflow_op(IrAnalyze *ira, IrInstr
2129122800 return result;
2129222801}
2129322802
22803static IrInstruction *ir_analyze_instruction_result_ptr(IrAnalyze *ira, IrInstructionResultPtr *instruction) {
22804 IrInstruction *result = instruction->result->child;
22805 if (type_is_invalid(result->value.type))
22806 return result;
22807
22808 if (instruction->result_loc->written && instruction->result_loc->resolved_loc != nullptr &&
22809 !instr_is_comptime(result))
22810 {
22811 return instruction->result_loc->resolved_loc;
22812 }
22813 return ir_get_ref(ira, &instruction->base, result, true, false);
22814}
22815
2129422816static void ir_eval_mul_add(IrAnalyze *ira, IrInstructionMulAdd *source_instr, ZigType *float_type,
2129522817 ConstExprValue *op1, ConstExprValue *op2, ConstExprValue *op3, ConstExprValue *out_val) {
2129622818 if (float_type->id == ZigTypeIdComptimeFloat) {
......@@ -21410,15 +22932,17 @@ static IrInstruction *ir_analyze_instruction_mul_add(IrAnalyze *ira, IrInstructi
2141022932 return result;
2141122933}
2141222934
21413static IrInstruction *ir_analyze_instruction_test_err(IrAnalyze *ira, IrInstructionTestErr *instruction) {
21414 IrInstruction *value = instruction->value->child;
21415 if (type_is_invalid(value->value.type))
22935static IrInstruction *ir_analyze_instruction_test_err(IrAnalyze *ira, IrInstructionTestErrSrc *instruction) {
22936 IrInstruction *base_ptr = instruction->base_ptr->child;
22937 if (type_is_invalid(base_ptr->value.type))
2141622938 return ira->codegen->invalid_instruction;
2141722939
22940 IrInstruction *value = ir_get_deref(ira, &instruction->base, base_ptr, nullptr);
2141822941 ZigType *type_entry = value->value.type;
21419 if (type_is_invalid(type_entry)) {
22942 if (type_is_invalid(type_entry))
2142022943 return ira->codegen->invalid_instruction;
21421 } else if (type_entry->id == ZigTypeIdErrorUnion) {
22944
22945 if (type_entry->id == ZigTypeIdErrorUnion) {
2142222946 if (instr_is_comptime(value)) {
2142322947 ConstExprValue *err_union_val = ir_resolve_const(ira, value, UndefBad);
2142422948 if (!err_union_val)
......@@ -21430,21 +22954,20 @@ static IrInstruction *ir_analyze_instruction_test_err(IrAnalyze *ira, IrInstruct
2143022954 }
2143122955 }
2143222956
21433 ZigType *err_set_type = type_entry->data.error_union.err_set_type;
21434 if (!resolve_inferred_error_set(ira->codegen, err_set_type, instruction->base.source_node)) {
21435 return ira->codegen->invalid_instruction;
21436 }
21437 if (!type_is_global_error_set(err_set_type) &&
21438 err_set_type->data.error_set.err_count == 0)
21439 {
21440 assert(err_set_type->data.error_set.infer_fn == nullptr);
21441 return ir_const_bool(ira, &instruction->base, false);
22957 if (instruction->resolve_err_set) {
22958 ZigType *err_set_type = type_entry->data.error_union.err_set_type;
22959 if (!resolve_inferred_error_set(ira->codegen, err_set_type, instruction->base.source_node)) {
22960 return ira->codegen->invalid_instruction;
22961 }
22962 if (!type_is_global_error_set(err_set_type) &&
22963 err_set_type->data.error_set.err_count == 0)
22964 {
22965 assert(err_set_type->data.error_set.infer_fn == nullptr);
22966 return ir_const_bool(ira, &instruction->base, false);
22967 }
2144222968 }
2144322969
21444 IrInstruction *result = ir_build_test_err(&ira->new_irb,
21445 instruction->base.scope, instruction->base.source_node, value);
21446 result->value.type = ira->codegen->builtin_types.entry_bool;
21447 return result;
22970 return ir_build_test_err_gen(ira, &instruction->base, value);
2144822971 } else if (type_entry->id == ZigTypeIdErrorSet) {
2144922972 return ir_const_bool(ira, &instruction->base, true);
2145022973 } else {
......@@ -21452,10 +22975,9 @@ static IrInstruction *ir_analyze_instruction_test_err(IrAnalyze *ira, IrInstruct
2145222975 }
2145322976}
2145422977
21455static IrInstruction *ir_analyze_instruction_unwrap_err_code(IrAnalyze *ira, IrInstructionUnwrapErrCode *instruction) {
21456 IrInstruction *base_ptr = instruction->err_union->child;
21457 if (type_is_invalid(base_ptr->value.type))
21458 return ira->codegen->invalid_instruction;
22978static IrInstruction *ir_analyze_unwrap_err_code(IrAnalyze *ira, IrInstruction *source_instr,
22979 IrInstruction *base_ptr, bool initializing)
22980{
2145922981 ZigType *ptr_type = base_ptr->value.type;
2146022982
2146122983 // This will be a pointer type because unwrap err payload IR instruction operates on a pointer to a thing.
......@@ -21471,40 +22993,79 @@ static IrInstruction *ir_analyze_instruction_unwrap_err_code(IrAnalyze *ira, IrI
2147122993 return ira->codegen->invalid_instruction;
2147222994 }
2147322995
22996 ZigType *err_set_type = type_entry->data.error_union.err_set_type;
22997 ZigType *result_type = get_pointer_to_type_extra(ira->codegen, err_set_type,
22998 ptr_type->data.pointer.is_const, ptr_type->data.pointer.is_volatile, PtrLenSingle,
22999 ptr_type->data.pointer.explicit_alignment, 0, 0, false);
23000
2147423001 if (instr_is_comptime(base_ptr)) {
2147523002 ConstExprValue *ptr_val = ir_resolve_const(ira, base_ptr, UndefBad);
2147623003 if (!ptr_val)
2147723004 return ira->codegen->invalid_instruction;
21478 if (ptr_val->data.x_ptr.mut != ConstPtrMutRuntimeVar) {
21479 ConstExprValue *err_union_val = const_ptr_pointee(ira, ira->codegen, ptr_val, instruction->base.source_node);
23005 if (ptr_val->data.x_ptr.mut != ConstPtrMutRuntimeVar &&
23006 ptr_val->data.x_ptr.special != ConstPtrSpecialHardCodedAddr)
23007 {
23008 ConstExprValue *err_union_val = const_ptr_pointee(ira, ira->codegen, ptr_val, source_instr->source_node);
2148023009 if (err_union_val == nullptr)
2148123010 return ira->codegen->invalid_instruction;
21482 if (err_union_val->special != ConstValSpecialRuntime) {
21483 ErrorTableEntry *err = err_union_val->data.x_err_union.error_set->data.x_err_set;
21484 assert(err);
2148523011
21486 IrInstruction *result = ir_const(ira, &instruction->base,
21487 type_entry->data.error_union.err_set_type);
21488 result->value.data.x_err_set = err;
21489 return result;
23012 if (initializing && err_union_val->special == ConstValSpecialUndef) {
23013 ConstExprValue *vals = create_const_vals(2);
23014 ConstExprValue *err_set_val = &vals[0];
23015 ConstExprValue *payload_val = &vals[1];
23016
23017 err_set_val->special = ConstValSpecialUndef;
23018 err_set_val->type = err_set_type;
23019 err_set_val->parent.id = ConstParentIdErrUnionCode;
23020 err_set_val->parent.data.p_err_union_code.err_union_val = err_union_val;
23021
23022 payload_val->special = ConstValSpecialUndef;
23023 payload_val->type = type_entry->data.error_union.payload_type;
23024 payload_val->parent.id = ConstParentIdErrUnionPayload;
23025 payload_val->parent.data.p_err_union_payload.err_union_val = err_union_val;
23026
23027 err_union_val->special = ConstValSpecialStatic;
23028 err_union_val->data.x_err_union.error_set = err_set_val;
23029 err_union_val->data.x_err_union.payload = payload_val;
23030 }
23031 ir_assert(err_union_val->special != ConstValSpecialRuntime, source_instr);
23032
23033 IrInstruction *result;
23034 if (ptr_val->data.x_ptr.mut == ConstPtrMutInfer) {
23035 result = ir_build_unwrap_err_code(&ira->new_irb, source_instr->scope,
23036 source_instr->source_node, base_ptr);
23037 result->value.type = result_type;
23038 result->value.special = ConstValSpecialStatic;
23039 } else {
23040 result = ir_const(ira, source_instr, result_type);
2149023041 }
23042 ConstExprValue *const_val = &result->value;
23043 const_val->data.x_ptr.special = ConstPtrSpecialBaseErrorUnionCode;
23044 const_val->data.x_ptr.data.base_err_union_code.err_union_val = err_union_val;
23045 const_val->data.x_ptr.mut = ptr_val->data.x_ptr.mut;
23046 return result;
2149123047 }
2149223048 }
2149323049
2149423050 IrInstruction *result = ir_build_unwrap_err_code(&ira->new_irb,
21495 instruction->base.scope, instruction->base.source_node, base_ptr);
21496 result->value.type = type_entry->data.error_union.err_set_type;
23051 source_instr->scope, source_instr->source_node, base_ptr);
23052 result->value.type = result_type;
2149723053 return result;
2149823054}
2149923055
21500static IrInstruction *ir_analyze_instruction_unwrap_err_payload(IrAnalyze *ira,
21501 IrInstructionUnwrapErrPayload *instruction)
23056static IrInstruction *ir_analyze_instruction_unwrap_err_code(IrAnalyze *ira,
23057 IrInstructionUnwrapErrCode *instruction)
2150223058{
21503 assert(instruction->value->child);
21504 IrInstruction *value = instruction->value->child;
21505 if (type_is_invalid(value->value.type))
23059 IrInstruction *base_ptr = instruction->err_union_ptr->child;
23060 if (type_is_invalid(base_ptr->value.type))
2150623061 return ira->codegen->invalid_instruction;
21507 ZigType *ptr_type = value->value.type;
23062 return ir_analyze_unwrap_err_code(ira, &instruction->base, base_ptr, false);
23063}
23064
23065static IrInstruction *ir_analyze_unwrap_error_payload(IrAnalyze *ira, IrInstruction *source_instr,
23066 IrInstruction *base_ptr, bool safety_check_on, bool initializing)
23067{
23068 ZigType *ptr_type = base_ptr->value.type;
2150823069
2150923070 // This will be a pointer type because unwrap err payload IR instruction operates on a pointer to a thing.
2151023071 assert(ptr_type->id == ZigTypeIdPointer);
......@@ -21514,7 +23075,7 @@ static IrInstruction *ir_analyze_instruction_unwrap_err_payload(IrAnalyze *ira,
2151423075 return ira->codegen->invalid_instruction;
2151523076
2151623077 if (type_entry->id != ZigTypeIdErrorUnion) {
21517 ir_add_error(ira, value,
23078 ir_add_error(ira, base_ptr,
2151823079 buf_sprintf("expected error union type, found '%s'", buf_ptr(&type_entry->name)));
2151923080 return ira->codegen->invalid_instruction;
2152023081 }
......@@ -21526,36 +23087,73 @@ static IrInstruction *ir_analyze_instruction_unwrap_err_payload(IrAnalyze *ira,
2152623087 ZigType *result_type = get_pointer_to_type_extra(ira->codegen, payload_type,
2152723088 ptr_type->data.pointer.is_const, ptr_type->data.pointer.is_volatile,
2152823089 PtrLenSingle, 0, 0, 0, false);
21529 if (instr_is_comptime(value)) {
21530 ConstExprValue *ptr_val = ir_resolve_const(ira, value, UndefBad);
23090 if (instr_is_comptime(base_ptr)) {
23091 ConstExprValue *ptr_val = ir_resolve_const(ira, base_ptr, UndefBad);
2153123092 if (!ptr_val)
2153223093 return ira->codegen->invalid_instruction;
2153323094 if (ptr_val->data.x_ptr.mut != ConstPtrMutRuntimeVar) {
21534 ConstExprValue *err_union_val = const_ptr_pointee(ira, ira->codegen, ptr_val, instruction->base.source_node);
23095 ConstExprValue *err_union_val = const_ptr_pointee(ira, ira->codegen, ptr_val, source_instr->source_node);
2153523096 if (err_union_val == nullptr)
2153623097 return ira->codegen->invalid_instruction;
23098 if (err_union_val->special == ConstValSpecialUndef && initializing) {
23099 ConstExprValue *vals = create_const_vals(2);
23100 ConstExprValue *err_set_val = &vals[0];
23101 ConstExprValue *payload_val = &vals[1];
23102
23103 err_set_val->special = ConstValSpecialStatic;
23104 err_set_val->type = type_entry->data.error_union.err_set_type;
23105 err_set_val->data.x_err_set = nullptr;
23106
23107 payload_val->special = ConstValSpecialUndef;
23108 payload_val->type = payload_type;
23109
23110 err_union_val->special = ConstValSpecialStatic;
23111 err_union_val->data.x_err_union.error_set = err_set_val;
23112 err_union_val->data.x_err_union.payload = payload_val;
23113 }
23114
2153723115 if (err_union_val->special != ConstValSpecialRuntime) {
2153823116 ErrorTableEntry *err = err_union_val->data.x_err_union.error_set->data.x_err_set;
2153923117 if (err != nullptr) {
21540 ir_add_error(ira, &instruction->base,
23118 ir_add_error(ira, source_instr,
2154123119 buf_sprintf("caught unexpected error '%s'", buf_ptr(&err->name)));
2154223120 return ira->codegen->invalid_instruction;
2154323121 }
2154423122
21545 IrInstruction *result = ir_const(ira, &instruction->base, result_type);
23123 IrInstruction *result;
23124 if (ptr_val->data.x_ptr.mut == ConstPtrMutInfer) {
23125 result = ir_build_unwrap_err_payload(&ira->new_irb, source_instr->scope,
23126 source_instr->source_node, base_ptr, safety_check_on, initializing);
23127 result->value.type = result_type;
23128 result->value.special = ConstValSpecialStatic;
23129 } else {
23130 result = ir_const(ira, source_instr, result_type);
23131 }
2154623132 result->value.data.x_ptr.special = ConstPtrSpecialRef;
2154723133 result->value.data.x_ptr.data.ref.pointee = err_union_val->data.x_err_union.payload;
23134 result->value.data.x_ptr.mut = ptr_val->data.x_ptr.mut;
2154823135 return result;
2154923136 }
2155023137 }
2155123138 }
2155223139
21553 IrInstruction *result = ir_build_unwrap_err_payload(&ira->new_irb,
21554 instruction->base.scope, instruction->base.source_node, value, instruction->safety_check_on);
23140 IrInstruction *result = ir_build_unwrap_err_payload(&ira->new_irb, source_instr->scope,
23141 source_instr->source_node, base_ptr, safety_check_on, initializing);
2155523142 result->value.type = result_type;
2155623143 return result;
2155723144}
2155823145
23146static IrInstruction *ir_analyze_instruction_unwrap_err_payload(IrAnalyze *ira,
23147 IrInstructionUnwrapErrPayload *instruction)
23148{
23149 assert(instruction->value->child);
23150 IrInstruction *value = instruction->value->child;
23151 if (type_is_invalid(value->value.type))
23152 return ira->codegen->invalid_instruction;
23153
23154 return ir_analyze_unwrap_error_payload(ira, &instruction->base, value, instruction->safety_check_on, false);
23155}
23156
2155923157static IrInstruction *ir_analyze_instruction_fn_proto(IrAnalyze *ira, IrInstructionFnProto *instruction) {
2156023158 AstNode *proto_node = instruction->base.source_node;
2156123159 assert(proto_node->type == NodeTypeFnProto);
......@@ -22047,14 +23645,19 @@ static IrInstruction *ir_analyze_ptr_cast(IrAnalyze *ira, IrInstruction *source_
2204723645 }
2204823646 }
2204923647
22050 IrInstruction *result = ir_const(ira, source_instr, dest_type);
23648 IrInstruction *result;
23649 if (ptr->value.data.x_ptr.mut == ConstPtrMutInfer) {
23650 result = ir_build_ptr_cast_gen(ira, source_instr, dest_type, ptr, safety_check_on);
23651 } else {
23652 result = ir_const(ira, source_instr, dest_type);
23653 }
2205123654 copy_const_val(&result->value, val, true);
2205223655 result->value.type = dest_type;
2205323656
2205423657 // Keep the bigger alignment, it can only help-
2205523658 // unless the target is zero bits.
2205623659 if (src_align_bytes > dest_align_bytes && type_has_bits(dest_type)) {
22057 result = ir_align_cast(ira, result, src_align_bytes, false);
23660 result = ir_align_cast(ira, result, src_align_bytes, false);
2205823661 }
2205923662
2206023663 return result;
......@@ -22138,6 +23741,8 @@ static void buf_write_value_bytes(CodeGen *codegen, uint8_t *buf, ConstExprValue
2213823741 case ZigTypeIdUndefined:
2213923742 case ZigTypeIdNull:
2214023743 case ZigTypeIdPromise:
23744 case ZigTypeIdErrorUnion:
23745 case ZigTypeIdErrorSet:
2214123746 zig_unreachable();
2214223747 case ZigTypeIdVoid:
2214323748 return;
......@@ -22241,10 +23846,6 @@ static void buf_write_value_bytes(CodeGen *codegen, uint8_t *buf, ConstExprValue
2224123846 }
2224223847 case ZigTypeIdOptional:
2224323848 zig_panic("TODO buf_write_value_bytes maybe type");
22244 case ZigTypeIdErrorUnion:
22245 zig_panic("TODO buf_write_value_bytes error union");
22246 case ZigTypeIdErrorSet:
22247 zig_panic("TODO buf_write_value_bytes pure error type");
2224823849 case ZigTypeIdFn:
2224923850 zig_panic("TODO buf_write_value_bytes fn type");
2225023851 case ZigTypeIdUnion:
......@@ -22433,28 +24034,6 @@ static Error buf_read_value_bytes(IrAnalyze *ira, CodeGen *codegen, AstNode *sou
2243324034 zig_unreachable();
2243424035}
2243524036
22436static bool type_can_bit_cast(ZigType *t) {
22437 switch (t->id) {
22438 case ZigTypeIdInvalid:
22439 zig_unreachable();
22440 case ZigTypeIdMetaType:
22441 case ZigTypeIdOpaque:
22442 case ZigTypeIdBoundFn:
22443 case ZigTypeIdArgTuple:
22444 case ZigTypeIdUnreachable:
22445 case ZigTypeIdComptimeFloat:
22446 case ZigTypeIdComptimeInt:
22447 case ZigTypeIdEnumLiteral:
22448 case ZigTypeIdUndefined:
22449 case ZigTypeIdNull:
22450 case ZigTypeIdPointer:
22451 return false;
22452 default:
22453 // TODO list these types out explicitly, there are probably some other invalid ones here
22454 return true;
22455 }
22456}
22457
2245824037static IrInstruction *ir_analyze_bit_cast(IrAnalyze *ira, IrInstruction *source_instr, IrInstruction *value,
2245924038 ZigType *dest_type)
2246024039{
......@@ -22506,49 +24085,7 @@ static IrInstruction *ir_analyze_bit_cast(IrAnalyze *ira, IrInstruction *source_
2250624085 return result;
2250724086 }
2250824087
22509 IrInstruction *result = ir_build_bit_cast_gen(ira, source_instr, value, dest_type);
22510 if (handle_is_ptr(dest_type) && !handle_is_ptr(src_type)) {
22511 ir_add_alloca(ira, result, dest_type);
22512 }
22513 return result;
22514}
22515
22516static IrInstruction *ir_analyze_instruction_bit_cast(IrAnalyze *ira, IrInstructionBitCast *instruction) {
22517 IrInstruction *dest_type_value = instruction->dest_type->child;
22518 ZigType *dest_type = ir_resolve_type(ira, dest_type_value);
22519 if (type_is_invalid(dest_type))
22520 return ira->codegen->invalid_instruction;
22521
22522 IrInstruction *value = instruction->value->child;
22523 ZigType *src_type = value->value.type;
22524 if (type_is_invalid(src_type))
22525 return ira->codegen->invalid_instruction;
22526
22527 if (get_codegen_ptr_type(src_type) != nullptr) {
22528 ir_add_error(ira, value,
22529 buf_sprintf("unable to @bitCast from pointer type '%s'", buf_ptr(&src_type->name)));
22530 return ira->codegen->invalid_instruction;
22531 }
22532
22533 if (!type_can_bit_cast(src_type)) {
22534 ir_add_error(ira, dest_type_value,
22535 buf_sprintf("unable to @bitCast from type '%s'", buf_ptr(&src_type->name)));
22536 return ira->codegen->invalid_instruction;
22537 }
22538
22539 if (get_codegen_ptr_type(dest_type) != nullptr) {
22540 ir_add_error(ira, dest_type_value,
22541 buf_sprintf("unable to @bitCast to pointer type '%s'", buf_ptr(&dest_type->name)));
22542 return ira->codegen->invalid_instruction;
22543 }
22544
22545 if (!type_can_bit_cast(dest_type)) {
22546 ir_add_error(ira, dest_type_value,
22547 buf_sprintf("unable to @bitCast to type '%s'", buf_ptr(&dest_type->name)));
22548 return ira->codegen->invalid_instruction;
22549 }
22550
22551 return ir_analyze_bit_cast(ira, &instruction->base, value, dest_type);
24088 return ir_build_bit_cast_gen(ira, source_instr, value, dest_type);
2255224089}
2255324090
2255424091static IrInstruction *ir_analyze_int_to_ptr(IrAnalyze *ira, IrInstruction *source_instr, IrInstruction *target,
......@@ -22618,58 +24155,15 @@ static IrInstruction *ir_analyze_instruction_int_to_ptr(IrAnalyze *ira, IrInstru
2261824155static IrInstruction *ir_analyze_instruction_decl_ref(IrAnalyze *ira,
2261924156 IrInstructionDeclRef *instruction)
2262024157{
22621 Tld *tld = instruction->tld;
22622 LVal lval = instruction->lval;
22623
22624 resolve_top_level_decl(ira->codegen, tld, instruction->base.source_node);
22625 if (tld->resolution == TldResolutionInvalid)
24158 IrInstruction *ref_instruction = ir_analyze_decl_ref(ira, &instruction->base, instruction->tld);
24159 if (type_is_invalid(ref_instruction->value.type))
2262624160 return ira->codegen->invalid_instruction;
2262724161
22628 switch (tld->id) {
22629 case TldIdContainer:
22630 case TldIdCompTime:
22631 zig_unreachable();
22632 case TldIdVar: {
22633 TldVar *tld_var = (TldVar *)tld;
22634 ZigVar *var = tld_var->var;
22635
22636 if (var == nullptr) {
22637 return ir_error_dependency_loop(ira, &instruction->base);
22638 }
22639
22640 IrInstruction *var_ptr = ir_get_var_ptr(ira, &instruction->base, var);
22641 if (type_is_invalid(var_ptr->value.type))
22642 return ira->codegen->invalid_instruction;
22643
22644 if (tld_var->extern_lib_name != nullptr) {
22645 add_link_lib_symbol(ira, tld_var->extern_lib_name, &var->name, instruction->base.source_node);
22646 }
22647
22648 if (lval == LValPtr) {
22649 return var_ptr;
22650 } else {
22651 return ir_get_deref(ira, &instruction->base, var_ptr);
22652 }
22653 }
22654 case TldIdFn: {
22655 TldFn *tld_fn = (TldFn *)tld;
22656 ZigFn *fn_entry = tld_fn->fn_entry;
22657 ir_assert(fn_entry->type_entry, &instruction->base);
22658
22659 if (tld_fn->extern_lib_name != nullptr) {
22660 add_link_lib_symbol(ira, tld_fn->extern_lib_name, &fn_entry->symbol_name, instruction->base.source_node);
22661 }
22662
22663 IrInstruction *ref_instruction = ir_create_const_fn(&ira->new_irb, instruction->base.scope,
22664 instruction->base.source_node, fn_entry);
22665 if (lval == LValPtr) {
22666 return ir_get_ref(ira, &instruction->base, ref_instruction, true, false);
22667 } else {
22668 return ref_instruction;
22669 }
22670 }
24162 if (instruction->lval == LValPtr) {
24163 return ref_instruction;
24164 } else {
24165 return ir_get_deref(ira, &instruction->base, ref_instruction, nullptr);
2267124166 }
22672 zig_unreachable();
2267324167}
2267424168
2267524169static IrInstruction *ir_analyze_instruction_ptr_to_int(IrAnalyze *ira, IrInstructionPtrToInt *instruction) {
......@@ -23183,7 +24677,7 @@ static IrInstruction *ir_analyze_instruction_atomic_load(IrAnalyze *ira, IrInstr
2318324677 }
2318424678
2318524679 if (instr_is_comptime(casted_ptr)) {
23186 IrInstruction *result = ir_get_deref(ira, &instruction->base, casted_ptr);
24680 IrInstruction *result = ir_get_deref(ira, &instruction->base, casted_ptr, nullptr);
2318724681 ir_assert(result->value.type != nullptr, &instruction->base);
2318824682 return result;
2318924683 }
......@@ -23721,12 +25215,56 @@ static IrInstruction *ir_analyze_instruction_undeclared_ident(IrAnalyze *ira, Ir
2372125215 return ira->codegen->invalid_instruction;
2372225216}
2372325217
23724static IrInstruction *ir_analyze_instruction_nocast(IrAnalyze *ira, IrInstruction *instruction) {
25218static IrInstruction *ir_analyze_instruction_end_expr(IrAnalyze *ira, IrInstructionEndExpr *instruction) {
25219 IrInstruction *value = instruction->value->child;
25220 if (type_is_invalid(value->value.type))
25221 return ira->codegen->invalid_instruction;
25222
25223 bool was_written = instruction->result_loc->written;
25224 IrInstruction *result_loc = ir_resolve_result(ira, &instruction->base, instruction->result_loc,
25225 value->value.type, value, false, false);
25226 if (result_loc != nullptr) {
25227 if (type_is_invalid(result_loc->value.type))
25228 return ira->codegen->invalid_instruction;
25229 if (result_loc->value.type->id == ZigTypeIdUnreachable)
25230 return result_loc;
25231
25232 if (!was_written) {
25233 IrInstruction *store_ptr = ir_analyze_store_ptr(ira, &instruction->base, result_loc, value);
25234 if (type_is_invalid(store_ptr->value.type)) {
25235 return ira->codegen->invalid_instruction;
25236 }
25237 }
25238
25239 if (result_loc->value.data.x_ptr.mut == ConstPtrMutInfer) {
25240 if (instr_is_comptime(value)) {
25241 result_loc->value.data.x_ptr.mut = ConstPtrMutComptimeConst;
25242 } else {
25243 result_loc->value.special = ConstValSpecialRuntime;
25244 }
25245 }
25246 }
25247
25248 return ir_const_void(ira, &instruction->base);
25249}
25250
25251static IrInstruction *ir_analyze_instruction_bit_cast_src(IrAnalyze *ira, IrInstructionBitCastSrc *instruction) {
25252 IrInstruction *operand = instruction->operand->child;
25253 if (type_is_invalid(operand->value.type))
25254 return operand;
25255
25256 IrInstruction *result_loc = ir_resolve_result(ira, &instruction->base,
25257 &instruction->result_loc_bit_cast->base, operand->value.type, operand, false, false);
25258 if (result_loc != nullptr && (type_is_invalid(result_loc->value.type) || instr_is_unreachable(result_loc)))
25259 return result_loc;
25260
25261 return instruction->result_loc_bit_cast->parent->gen_instruction;
25262}
25263
25264static IrInstruction *ir_analyze_instruction_base(IrAnalyze *ira, IrInstruction *instruction) {
2372525265 switch (instruction->id) {
2372625266 case IrInstructionIdInvalid:
2372725267 case IrInstructionIdWidenOrShorten:
23728 case IrInstructionIdStructInit:
23729 case IrInstructionIdUnionInit:
2373025268 case IrInstructionIdStructFieldPtr:
2373125269 case IrInstructionIdUnionFieldPtr:
2373225270 case IrInstructionIdOptionalWrap:
......@@ -23738,11 +25276,18 @@ static IrInstruction *ir_analyze_instruction_nocast(IrAnalyze *ira, IrInstructio
2373825276 case IrInstructionIdCmpxchgGen:
2373925277 case IrInstructionIdArrayToVector:
2374025278 case IrInstructionIdVectorToArray:
25279 case IrInstructionIdPtrOfArrayToSlice:
2374125280 case IrInstructionIdAssertZero:
2374225281 case IrInstructionIdAssertNonNull:
2374325282 case IrInstructionIdResizeSlice:
2374425283 case IrInstructionIdLoadPtrGen:
2374525284 case IrInstructionIdBitCastGen:
25285 case IrInstructionIdCallGen:
25286 case IrInstructionIdReturnPtr:
25287 case IrInstructionIdAllocaGen:
25288 case IrInstructionIdSliceGen:
25289 case IrInstructionIdRefGen:
25290 case IrInstructionIdTestErrGen:
2374625291 zig_unreachable();
2374725292
2374825293 case IrInstructionIdReturn:
......@@ -23765,8 +25310,8 @@ static IrInstruction *ir_analyze_instruction_nocast(IrAnalyze *ira, IrInstructio
2376525310 return ir_analyze_instruction_var_ptr(ira, (IrInstructionVarPtr *)instruction);
2376625311 case IrInstructionIdFieldPtr:
2376725312 return ir_analyze_instruction_field_ptr(ira, (IrInstructionFieldPtr *)instruction);
23768 case IrInstructionIdCall:
23769 return ir_analyze_instruction_call(ira, (IrInstructionCall *)instruction);
25313 case IrInstructionIdCallSrc:
25314 return ir_analyze_instruction_call(ira, (IrInstructionCallSrc *)instruction);
2377025315 case IrInstructionIdBr:
2377125316 return ir_analyze_instruction_br(ira, (IrInstructionBr *)instruction);
2377225317 case IrInstructionIdCondBr:
......@@ -23777,10 +25322,6 @@ static IrInstruction *ir_analyze_instruction_nocast(IrAnalyze *ira, IrInstructio
2377725322 return ir_analyze_instruction_phi(ira, (IrInstructionPhi *)instruction);
2377825323 case IrInstructionIdTypeOf:
2377925324 return ir_analyze_instruction_typeof(ira, (IrInstructionTypeOf *)instruction);
23780 case IrInstructionIdToPtrType:
23781 return ir_analyze_instruction_to_ptr_type(ira, (IrInstructionToPtrType *)instruction);
23782 case IrInstructionIdPtrTypeChild:
23783 return ir_analyze_instruction_ptr_type_child(ira, (IrInstructionPtrTypeChild *)instruction);
2378425325 case IrInstructionIdSetCold:
2378525326 return ir_analyze_instruction_set_cold(ira, (IrInstructionSetCold *)instruction);
2378625327 case IrInstructionIdSetRuntimeSafety:
......@@ -23881,8 +25422,8 @@ static IrInstruction *ir_analyze_instruction_nocast(IrAnalyze *ira, IrInstructio
2388125422 return ir_analyze_instruction_memset(ira, (IrInstructionMemset *)instruction);
2388225423 case IrInstructionIdMemcpy:
2388325424 return ir_analyze_instruction_memcpy(ira, (IrInstructionMemcpy *)instruction);
23884 case IrInstructionIdSlice:
23885 return ir_analyze_instruction_slice(ira, (IrInstructionSlice *)instruction);
25425 case IrInstructionIdSliceSrc:
25426 return ir_analyze_instruction_slice(ira, (IrInstructionSliceSrc *)instruction);
2388625427 case IrInstructionIdMemberCount:
2388725428 return ir_analyze_instruction_member_count(ira, (IrInstructionMemberCount *)instruction);
2388825429 case IrInstructionIdMemberType:
......@@ -23901,8 +25442,8 @@ static IrInstruction *ir_analyze_instruction_nocast(IrAnalyze *ira, IrInstructio
2390125442 return ir_analyze_instruction_align_of(ira, (IrInstructionAlignOf *)instruction);
2390225443 case IrInstructionIdOverflowOp:
2390325444 return ir_analyze_instruction_overflow_op(ira, (IrInstructionOverflowOp *)instruction);
23904 case IrInstructionIdTestErr:
23905 return ir_analyze_instruction_test_err(ira, (IrInstructionTestErr *)instruction);
25445 case IrInstructionIdTestErrSrc:
25446 return ir_analyze_instruction_test_err(ira, (IrInstructionTestErrSrc *)instruction);
2390625447 case IrInstructionIdUnwrapErrCode:
2390725448 return ir_analyze_instruction_unwrap_err_code(ira, (IrInstructionUnwrapErrCode *)instruction);
2390825449 case IrInstructionIdUnwrapErrPayload:
......@@ -23921,8 +25462,6 @@ static IrInstruction *ir_analyze_instruction_nocast(IrAnalyze *ira, IrInstructio
2392125462 return ir_analyze_instruction_panic(ira, (IrInstructionPanic *)instruction);
2392225463 case IrInstructionIdPtrCastSrc:
2392325464 return ir_analyze_instruction_ptr_cast(ira, (IrInstructionPtrCastSrc *)instruction);
23924 case IrInstructionIdBitCast:
23925 return ir_analyze_instruction_bit_cast(ira, (IrInstructionBitCast *)instruction);
2392625465 case IrInstructionIdIntToPtr:
2392725466 return ir_analyze_instruction_int_to_ptr(ira, (IrInstructionIntToPtr *)instruction);
2392825467 case IrInstructionIdPtrToInt:
......@@ -23945,6 +25484,14 @@ static IrInstruction *ir_analyze_instruction_nocast(IrAnalyze *ira, IrInstructio
2394525484 return ir_analyze_instruction_ptr_type(ira, (IrInstructionPtrType *)instruction);
2394625485 case IrInstructionIdAlignCast:
2394725486 return ir_analyze_instruction_align_cast(ira, (IrInstructionAlignCast *)instruction);
25487 case IrInstructionIdImplicitCast:
25488 return ir_analyze_instruction_implicit_cast(ira, (IrInstructionImplicitCast *)instruction);
25489 case IrInstructionIdResolveResult:
25490 return ir_analyze_instruction_resolve_result(ira, (IrInstructionResolveResult *)instruction);
25491 case IrInstructionIdResetResult:
25492 return ir_analyze_instruction_reset_result(ira, (IrInstructionResetResult *)instruction);
25493 case IrInstructionIdResultPtr:
25494 return ir_analyze_instruction_result_ptr(ira, (IrInstructionResultPtr *)instruction);
2394825495 case IrInstructionIdOpaqueType:
2394925496 return ir_analyze_instruction_opaque_type(ira, (IrInstructionOpaqueType *)instruction);
2395025497 case IrInstructionIdSetAlignStack:
......@@ -24021,17 +25568,16 @@ static IrInstruction *ir_analyze_instruction_nocast(IrAnalyze *ira, IrInstructio
2402125568 return ir_analyze_instruction_has_decl(ira, (IrInstructionHasDecl *)instruction);
2402225569 case IrInstructionIdUndeclaredIdent:
2402325570 return ir_analyze_instruction_undeclared_ident(ira, (IrInstructionUndeclaredIdent *)instruction);
25571 case IrInstructionIdAllocaSrc:
25572 return nullptr;
25573 case IrInstructionIdEndExpr:
25574 return ir_analyze_instruction_end_expr(ira, (IrInstructionEndExpr *)instruction);
25575 case IrInstructionIdBitCastSrc:
25576 return ir_analyze_instruction_bit_cast_src(ira, (IrInstructionBitCastSrc *)instruction);
2402425577 }
2402525578 zig_unreachable();
2402625579}
2402725580
24028static IrInstruction *ir_analyze_instruction(IrAnalyze *ira, IrInstruction *old_instruction) {
24029 IrInstruction *new_instruction = ir_analyze_instruction_nocast(ira, old_instruction);
24030 ir_assert(new_instruction->value.type != nullptr, old_instruction);
24031 old_instruction->child = new_instruction;
24032 return new_instruction;
24033}
24034
2403525581// This function attempts to evaluate IR code while doing type checking and other analysis.
2403625582// It emits a new IrExecutable which is partially evaluated IR code.
2403725583ZigType *ir_analyze(CodeGen *codegen, IrExecutable *old_exec, IrExecutable *new_exec,
......@@ -24077,14 +25623,22 @@ ZigType *ir_analyze(CodeGen *codegen, IrExecutable *old_exec, IrExecutable *new_
2407725623 continue;
2407825624 }
2407925625
24080 IrInstruction *new_instruction = ir_analyze_instruction(ira, old_instruction);
24081 if (type_is_invalid(new_instruction->value.type) && ir_should_inline(new_exec, old_instruction->scope)) {
24082 return ira->codegen->builtin_types.entry_invalid;
25626 if (ira->codegen->verbose_ir) {
25627 fprintf(stderr, "analyze #%zu\n", old_instruction->debug_id);
2408325628 }
25629 IrInstruction *new_instruction = ir_analyze_instruction_base(ira, old_instruction);
25630 if (new_instruction != nullptr) {
25631 ir_assert(new_instruction->value.type != nullptr || new_instruction->value.type != nullptr, old_instruction);
25632 old_instruction->child = new_instruction;
2408425633
24085 // unreachable instructions do their own control flow.
24086 if (new_instruction->value.type->id == ZigTypeIdUnreachable)
24087 continue;
25634 if (type_is_invalid(new_instruction->value.type)) {
25635 return ira->codegen->builtin_types.entry_invalid;
25636 }
25637
25638 // unreachable instructions do their own control flow.
25639 if (new_instruction->value.type->id == ZigTypeIdUnreachable)
25640 continue;
25641 }
2408825642
2408925643 ira->instruction_index += 1;
2409025644 }
......@@ -24109,7 +25663,8 @@ bool ir_has_side_effects(IrInstruction *instruction) {
2410925663 case IrInstructionIdDeclVarSrc:
2411025664 case IrInstructionIdDeclVarGen:
2411125665 case IrInstructionIdStorePtr:
24112 case IrInstructionIdCall:
25666 case IrInstructionIdCallSrc:
25667 case IrInstructionIdCallGen:
2411325668 case IrInstructionIdReturn:
2411425669 case IrInstructionIdUnreachable:
2411525670 case IrInstructionIdSetCold:
......@@ -24156,27 +25711,28 @@ bool ir_has_side_effects(IrInstruction *instruction) {
2415625711 case IrInstructionIdResizeSlice:
2415725712 case IrInstructionIdGlobalAsm:
2415825713 case IrInstructionIdUndeclaredIdent:
25714 case IrInstructionIdEndExpr:
25715 case IrInstructionIdPtrOfArrayToSlice:
25716 case IrInstructionIdSliceGen:
25717 case IrInstructionIdOptionalWrap:
25718 case IrInstructionIdVectorToArray:
25719 case IrInstructionIdResetResult:
2415925720 return true;
2416025721
2416125722 case IrInstructionIdPhi:
2416225723 case IrInstructionIdUnOp:
2416325724 case IrInstructionIdBinOp:
2416425725 case IrInstructionIdLoadPtr:
24165 case IrInstructionIdLoadPtrGen:
2416625726 case IrInstructionIdConst:
2416725727 case IrInstructionIdCast:
2416825728 case IrInstructionIdContainerInitList:
2416925729 case IrInstructionIdContainerInitFields:
24170 case IrInstructionIdStructInit:
24171 case IrInstructionIdUnionInit:
2417225730 case IrInstructionIdFieldPtr:
2417325731 case IrInstructionIdElemPtr:
2417425732 case IrInstructionIdVarPtr:
25733 case IrInstructionIdReturnPtr:
2417525734 case IrInstructionIdTypeOf:
24176 case IrInstructionIdToPtrType:
24177 case IrInstructionIdPtrTypeChild:
2417825735 case IrInstructionIdStructFieldPtr:
24179 case IrInstructionIdUnionFieldPtr:
2418025736 case IrInstructionIdArrayType:
2418125737 case IrInstructionIdPromiseType:
2418225738 case IrInstructionIdSliceType:
......@@ -24198,7 +25754,7 @@ bool ir_has_side_effects(IrInstruction *instruction) {
2419825754 case IrInstructionIdIntType:
2419925755 case IrInstructionIdVectorType:
2420025756 case IrInstructionIdBoolNot:
24201 case IrInstructionIdSlice:
25757 case IrInstructionIdSliceSrc:
2420225758 case IrInstructionIdMemberCount:
2420325759 case IrInstructionIdMemberType:
2420425760 case IrInstructionIdMemberName:
......@@ -24206,16 +25762,13 @@ bool ir_has_side_effects(IrInstruction *instruction) {
2420625762 case IrInstructionIdReturnAddress:
2420725763 case IrInstructionIdFrameAddress:
2420825764 case IrInstructionIdHandle:
24209 case IrInstructionIdTestErr:
24210 case IrInstructionIdUnwrapErrCode:
24211 case IrInstructionIdOptionalWrap:
24212 case IrInstructionIdErrWrapCode:
24213 case IrInstructionIdErrWrapPayload:
25765 case IrInstructionIdTestErrSrc:
25766 case IrInstructionIdTestErrGen:
2421425767 case IrInstructionIdFnProto:
2421525768 case IrInstructionIdTestComptime:
2421625769 case IrInstructionIdPtrCastSrc:
2421725770 case IrInstructionIdPtrCastGen:
24218 case IrInstructionIdBitCast:
25771 case IrInstructionIdBitCastSrc:
2421925772 case IrInstructionIdBitCastGen:
2422025773 case IrInstructionIdWidenOrShorten:
2422125774 case IrInstructionIdPtrToInt:
......@@ -24233,6 +25786,8 @@ bool ir_has_side_effects(IrInstruction *instruction) {
2423325786 case IrInstructionIdTypeInfo:
2423425787 case IrInstructionIdTypeId:
2423525788 case IrInstructionIdAlignCast:
25789 case IrInstructionIdImplicitCast:
25790 case IrInstructionIdResolveResult:
2423625791 case IrInstructionIdOpaqueType:
2423725792 case IrInstructionIdArgType:
2423825793 case IrInstructionIdTagType:
......@@ -24257,9 +25812,11 @@ bool ir_has_side_effects(IrInstruction *instruction) {
2425725812 case IrInstructionIdFromBytes:
2425825813 case IrInstructionIdToBytes:
2425925814 case IrInstructionIdEnumToInt:
24260 case IrInstructionIdVectorToArray:
2426125815 case IrInstructionIdArrayToVector:
2426225816 case IrInstructionIdHasDecl:
25817 case IrInstructionIdAllocaSrc:
25818 case IrInstructionIdAllocaGen:
25819 case IrInstructionIdResultPtr:
2426325820 return false;
2426425821
2426525822 case IrInstructionIdAsm:
......@@ -24271,8 +25828,21 @@ bool ir_has_side_effects(IrInstruction *instruction) {
2427125828 {
2427225829 IrInstructionUnwrapErrPayload *unwrap_err_payload_instruction =
2427325830 (IrInstructionUnwrapErrPayload *)instruction;
24274 return unwrap_err_payload_instruction->safety_check_on;
25831 return unwrap_err_payload_instruction->safety_check_on ||
25832 unwrap_err_payload_instruction->initializing;
2427525833 }
25834 case IrInstructionIdUnwrapErrCode:
25835 return reinterpret_cast<IrInstructionUnwrapErrCode *>(instruction)->initializing;
25836 case IrInstructionIdUnionFieldPtr:
25837 return reinterpret_cast<IrInstructionUnionFieldPtr *>(instruction)->initializing;
25838 case IrInstructionIdErrWrapPayload:
25839 return reinterpret_cast<IrInstructionErrWrapPayload *>(instruction)->result_loc != nullptr;
25840 case IrInstructionIdErrWrapCode:
25841 return reinterpret_cast<IrInstructionErrWrapCode *>(instruction)->result_loc != nullptr;
25842 case IrInstructionIdLoadPtrGen:
25843 return reinterpret_cast<IrInstructionLoadPtrGen *>(instruction)->result_loc != nullptr;
25844 case IrInstructionIdRefGen:
25845 return reinterpret_cast<IrInstructionRefGen *>(instruction)->result_loc != nullptr;
2427625846 }
2427725847 zig_unreachable();
2427825848}
src/ir_print.cpp+253-90
......@@ -57,13 +57,18 @@ static void ir_print_other_instruction(IrPrint *irp, IrInstruction *instruction)
5757}
5858
5959static void ir_print_other_block(IrPrint *irp, IrBasicBlock *bb) {
60 fprintf(irp->f, "$%s_%" ZIG_PRI_usize "", bb->name_hint, bb->debug_id);
60 if (bb == nullptr) {
61 fprintf(irp->f, "(null block)");
62 } else {
63 fprintf(irp->f, "$%s_%" ZIG_PRI_usize "", bb->name_hint, bb->debug_id);
64 }
6165}
6266
6367static void ir_print_return(IrPrint *irp, IrInstructionReturn *return_instruction) {
64 assert(return_instruction->value);
6568 fprintf(irp->f, "return ");
66 ir_print_other_instruction(irp, return_instruction->value);
69 if (return_instruction->value != nullptr) {
70 ir_print_other_instruction(irp, return_instruction->value);
71 }
6772}
6873
6974static void ir_print_const(IrPrint *irp, IrInstructionConst *const_instruction) {
......@@ -188,7 +193,7 @@ static void ir_print_decl_var_src(IrPrint *irp, IrInstructionDeclVarSrc *decl_va
188193 fprintf(irp->f, " ");
189194 }
190195 fprintf(irp->f, "= ");
191 ir_print_other_instruction(irp, decl_var_instruction->init_value);
196 ir_print_other_instruction(irp, decl_var_instruction->ptr);
192197 if (decl_var_instruction->var->is_comptime != nullptr) {
193198 fprintf(irp->f, " // comptime = ");
194199 ir_print_other_instruction(irp, decl_var_instruction->var->is_comptime);
......@@ -201,7 +206,56 @@ static void ir_print_cast(IrPrint *irp, IrInstructionCast *cast_instruction) {
201206 fprintf(irp->f, " to %s", buf_ptr(&cast_instruction->dest_type->name));
202207}
203208
204static void ir_print_call(IrPrint *irp, IrInstructionCall *call_instruction) {
209static void ir_print_result_loc_var(IrPrint *irp, ResultLocVar *result_loc_var) {
210 fprintf(irp->f, "var(");
211 ir_print_other_instruction(irp, result_loc_var->base.source_instruction);
212 fprintf(irp->f, ")");
213}
214
215static void ir_print_result_loc_instruction(IrPrint *irp, ResultLocInstruction *result_loc_inst) {
216 fprintf(irp->f, "inst(");
217 ir_print_other_instruction(irp, result_loc_inst->base.source_instruction);
218 fprintf(irp->f, ")");
219}
220
221static void ir_print_result_loc_peer(IrPrint *irp, ResultLocPeer *result_loc_peer) {
222 fprintf(irp->f, "peer(next=");
223 ir_print_other_block(irp, result_loc_peer->next_bb);
224 fprintf(irp->f, ")");
225}
226
227static void ir_print_result_loc_bit_cast(IrPrint *irp, ResultLocBitCast *result_loc_bit_cast) {
228 fprintf(irp->f, "bitcast(ty=");
229 ir_print_other_instruction(irp, result_loc_bit_cast->base.source_instruction);
230 fprintf(irp->f, ")");
231}
232
233static void ir_print_result_loc(IrPrint *irp, ResultLoc *result_loc) {
234 switch (result_loc->id) {
235 case ResultLocIdInvalid:
236 zig_unreachable();
237 case ResultLocIdNone:
238 fprintf(irp->f, "none");
239 return;
240 case ResultLocIdReturn:
241 fprintf(irp->f, "return");
242 return;
243 case ResultLocIdVar:
244 return ir_print_result_loc_var(irp, (ResultLocVar *)result_loc);
245 case ResultLocIdInstruction:
246 return ir_print_result_loc_instruction(irp, (ResultLocInstruction *)result_loc);
247 case ResultLocIdPeer:
248 return ir_print_result_loc_peer(irp, (ResultLocPeer *)result_loc);
249 case ResultLocIdBitCast:
250 return ir_print_result_loc_bit_cast(irp, (ResultLocBitCast *)result_loc);
251 case ResultLocIdPeerParent:
252 fprintf(irp->f, "peer_parent");
253 return;
254 }
255 zig_unreachable();
256}
257
258static void ir_print_call_src(IrPrint *irp, IrInstructionCallSrc *call_instruction) {
205259 if (call_instruction->is_async) {
206260 fprintf(irp->f, "async");
207261 if (call_instruction->async_allocator != nullptr) {
......@@ -224,7 +278,35 @@ static void ir_print_call(IrPrint *irp, IrInstructionCall *call_instruction) {
224278 fprintf(irp->f, ", ");
225279 ir_print_other_instruction(irp, arg);
226280 }
227 fprintf(irp->f, ")");
281 fprintf(irp->f, ")result=");
282 ir_print_result_loc(irp, call_instruction->result_loc);
283}
284
285static void ir_print_call_gen(IrPrint *irp, IrInstructionCallGen *call_instruction) {
286 if (call_instruction->is_async) {
287 fprintf(irp->f, "async");
288 if (call_instruction->async_allocator != nullptr) {
289 fprintf(irp->f, "<");
290 ir_print_other_instruction(irp, call_instruction->async_allocator);
291 fprintf(irp->f, ">");
292 }
293 fprintf(irp->f, " ");
294 }
295 if (call_instruction->fn_entry) {
296 fprintf(irp->f, "%s", buf_ptr(&call_instruction->fn_entry->symbol_name));
297 } else {
298 assert(call_instruction->fn_ref);
299 ir_print_other_instruction(irp, call_instruction->fn_ref);
300 }
301 fprintf(irp->f, "(");
302 for (size_t i = 0; i < call_instruction->arg_count; i += 1) {
303 IrInstruction *arg = call_instruction->args[i];
304 if (i != 0)
305 fprintf(irp->f, ", ");
306 ir_print_other_instruction(irp, arg);
307 }
308 fprintf(irp->f, ")result=");
309 ir_print_other_instruction(irp, call_instruction->result_loc);
228310}
229311
230312static void ir_print_cond_br(IrPrint *irp, IrInstructionCondBr *cond_br_instruction) {
......@@ -270,10 +352,10 @@ static void ir_print_container_init_list(IrPrint *irp, IrInstructionContainerIni
270352 fprintf(irp->f, "...(%" ZIG_PRI_usize " items)...", instruction->item_count);
271353 } else {
272354 for (size_t i = 0; i < instruction->item_count; i += 1) {
273 IrInstruction *item = instruction->items[i];
355 IrInstruction *result_loc = instruction->elem_result_loc_list[i];
274356 if (i != 0)
275357 fprintf(irp->f, ", ");
276 ir_print_other_instruction(irp, item);
358 ir_print_other_instruction(irp, result_loc);
277359 }
278360 }
279361 fprintf(irp->f, "}");
......@@ -286,32 +368,11 @@ static void ir_print_container_init_fields(IrPrint *irp, IrInstructionContainerI
286368 IrInstructionContainerInitFieldsField *field = &instruction->fields[i];
287369 const char *comma = (i == 0) ? "" : ", ";
288370 fprintf(irp->f, "%s.%s = ", comma, buf_ptr(field->name));
289 ir_print_other_instruction(irp, field->value);
371 ir_print_other_instruction(irp, field->result_loc);
290372 }
291373 fprintf(irp->f, "} // container init");
292374}
293375
294static void ir_print_struct_init(IrPrint *irp, IrInstructionStructInit *instruction) {
295 fprintf(irp->f, "%s {", buf_ptr(&instruction->struct_type->name));
296 for (size_t i = 0; i < instruction->field_count; i += 1) {
297 IrInstructionStructInitField *field = &instruction->fields[i];
298 Buf *field_name = field->type_struct_field->name;
299 const char *comma = (i == 0) ? "" : ", ";
300 fprintf(irp->f, "%s.%s = ", comma, buf_ptr(field_name));
301 ir_print_other_instruction(irp, field->value);
302 }
303 fprintf(irp->f, "} // struct init");
304}
305
306static void ir_print_union_init(IrPrint *irp, IrInstructionUnionInit *instruction) {
307 Buf *field_name = instruction->field->enum_field->name;
308
309 fprintf(irp->f, "%s {", buf_ptr(&instruction->union_type->name));
310 fprintf(irp->f, ".%s = ", buf_ptr(field_name));
311 ir_print_other_instruction(irp, instruction->init_value);
312 fprintf(irp->f, "} // union init");
313}
314
315376static void ir_print_unreachable(IrPrint *irp, IrInstructionUnreachable *instruction) {
316377 fprintf(irp->f, "unreachable");
317378}
......@@ -331,14 +392,20 @@ static void ir_print_var_ptr(IrPrint *irp, IrInstructionVarPtr *instruction) {
331392 fprintf(irp->f, "&%s", buf_ptr(&instruction->var->name));
332393}
333394
395static void ir_print_return_ptr(IrPrint *irp, IrInstructionReturnPtr *instruction) {
396 fprintf(irp->f, "@ReturnPtr");
397}
398
334399static void ir_print_load_ptr(IrPrint *irp, IrInstructionLoadPtr *instruction) {
335400 ir_print_other_instruction(irp, instruction->ptr);
336401 fprintf(irp->f, ".*");
337402}
338403
339404static void ir_print_load_ptr_gen(IrPrint *irp, IrInstructionLoadPtrGen *instruction) {
405 fprintf(irp->f, "loadptr(");
340406 ir_print_other_instruction(irp, instruction->ptr);
341 fprintf(irp->f, ".*");
407 fprintf(irp->f, ")result=");
408 ir_print_other_instruction(irp, instruction->result_loc);
342409}
343410
344411static void ir_print_store_ptr(IrPrint *irp, IrInstructionStorePtr *instruction) {
......@@ -354,18 +421,6 @@ static void ir_print_typeof(IrPrint *irp, IrInstructionTypeOf *instruction) {
354421 fprintf(irp->f, ")");
355422}
356423
357static void ir_print_to_ptr_type(IrPrint *irp, IrInstructionToPtrType *instruction) {
358 fprintf(irp->f, "@toPtrType(");
359 ir_print_other_instruction(irp, instruction->ptr);
360 fprintf(irp->f, ")");
361}
362
363static void ir_print_ptr_type_child(IrPrint *irp, IrInstructionPtrTypeChild *instruction) {
364 fprintf(irp->f, "@ptrTypeChild(");
365 ir_print_other_instruction(irp, instruction->value);
366 fprintf(irp->f, ")");
367}
368
369424static void ir_print_field_ptr(IrPrint *irp, IrInstructionFieldPtr *instruction) {
370425 if (instruction->field_name_buffer) {
371426 fprintf(irp->f, "fieldptr ");
......@@ -618,6 +673,13 @@ static void ir_print_ref(IrPrint *irp, IrInstructionRef *instruction) {
618673 ir_print_other_instruction(irp, instruction->value);
619674}
620675
676static void ir_print_ref_gen(IrPrint *irp, IrInstructionRefGen *instruction) {
677 fprintf(irp->f, "@ref(");
678 ir_print_other_instruction(irp, instruction->operand);
679 fprintf(irp->f, ")result=");
680 ir_print_other_instruction(irp, instruction->result_loc);
681}
682
621683static void ir_print_compile_err(IrPrint *irp, IrInstructionCompileErr *instruction) {
622684 fprintf(irp->f, "@compileError(");
623685 ir_print_other_instruction(irp, instruction->msg);
......@@ -682,7 +744,8 @@ static void ir_print_cmpxchg_src(IrPrint *irp, IrInstructionCmpxchgSrc *instruct
682744 ir_print_other_instruction(irp, instruction->success_order_value);
683745 fprintf(irp->f, ", ");
684746 ir_print_other_instruction(irp, instruction->failure_order_value);
685 fprintf(irp->f, ")");
747 fprintf(irp->f, ")result=");
748 ir_print_result_loc(irp, instruction->result_loc);
686749}
687750
688751static void ir_print_cmpxchg_gen(IrPrint *irp, IrInstructionCmpxchgGen *instruction) {
......@@ -692,7 +755,8 @@ static void ir_print_cmpxchg_gen(IrPrint *irp, IrInstructionCmpxchgGen *instruct
692755 ir_print_other_instruction(irp, instruction->cmp_value);
693756 fprintf(irp->f, ", ");
694757 ir_print_other_instruction(irp, instruction->new_value);
695 fprintf(irp->f, ", TODO print atomic orders)");
758 fprintf(irp->f, ", TODO print atomic orders)result=");
759 ir_print_other_instruction(irp, instruction->result_loc);
696760}
697761
698762static void ir_print_fence(IrPrint *irp, IrInstructionFence *instruction) {
......@@ -810,14 +874,26 @@ static void ir_print_memcpy(IrPrint *irp, IrInstructionMemcpy *instruction) {
810874 fprintf(irp->f, ")");
811875}
812876
813static void ir_print_slice(IrPrint *irp, IrInstructionSlice *instruction) {
877static void ir_print_slice_src(IrPrint *irp, IrInstructionSliceSrc *instruction) {
814878 ir_print_other_instruction(irp, instruction->ptr);
815879 fprintf(irp->f, "[");
816880 ir_print_other_instruction(irp, instruction->start);
817881 fprintf(irp->f, "..");
818882 if (instruction->end)
819883 ir_print_other_instruction(irp, instruction->end);
820 fprintf(irp->f, "]");
884 fprintf(irp->f, "]result=");
885 ir_print_result_loc(irp, instruction->result_loc);
886}
887
888static void ir_print_slice_gen(IrPrint *irp, IrInstructionSliceGen *instruction) {
889 ir_print_other_instruction(irp, instruction->ptr);
890 fprintf(irp->f, "[");
891 ir_print_other_instruction(irp, instruction->start);
892 fprintf(irp->f, "..");
893 if (instruction->end)
894 ir_print_other_instruction(irp, instruction->end);
895 fprintf(irp->f, "]result=");
896 ir_print_other_instruction(irp, instruction->result_loc);
821897}
822898
823899static void ir_print_member_count(IrPrint *irp, IrInstructionMemberCount *instruction) {
......@@ -889,43 +965,49 @@ static void ir_print_overflow_op(IrPrint *irp, IrInstructionOverflowOp *instruct
889965 fprintf(irp->f, ")");
890966}
891967
892static void ir_print_test_err(IrPrint *irp, IrInstructionTestErr *instruction) {
968static void ir_print_test_err_src(IrPrint *irp, IrInstructionTestErrSrc *instruction) {
893969 fprintf(irp->f, "@testError(");
894 ir_print_other_instruction(irp, instruction->value);
970 ir_print_other_instruction(irp, instruction->base_ptr);
971 fprintf(irp->f, ")");
972}
973
974static void ir_print_test_err_gen(IrPrint *irp, IrInstructionTestErrGen *instruction) {
975 fprintf(irp->f, "@testError(");
976 ir_print_other_instruction(irp, instruction->err_union);
895977 fprintf(irp->f, ")");
896978}
897979
898980static void ir_print_unwrap_err_code(IrPrint *irp, IrInstructionUnwrapErrCode *instruction) {
899981 fprintf(irp->f, "UnwrapErrorCode(");
900 ir_print_other_instruction(irp, instruction->err_union);
982 ir_print_other_instruction(irp, instruction->err_union_ptr);
901983 fprintf(irp->f, ")");
902984}
903985
904986static void ir_print_unwrap_err_payload(IrPrint *irp, IrInstructionUnwrapErrPayload *instruction) {
905987 fprintf(irp->f, "ErrorUnionFieldPayload(");
906988 ir_print_other_instruction(irp, instruction->value);
907 fprintf(irp->f, ")");
908 if (!instruction->safety_check_on) {
909 fprintf(irp->f, " // no safety");
910 }
989 fprintf(irp->f, ")safety=%d,init=%d",instruction->safety_check_on, instruction->initializing);
911990}
912991
913static void ir_print_maybe_wrap(IrPrint *irp, IrInstructionOptionalWrap *instruction) {
914 fprintf(irp->f, "@maybeWrap(");
915 ir_print_other_instruction(irp, instruction->value);
916 fprintf(irp->f, ")");
992static void ir_print_optional_wrap(IrPrint *irp, IrInstructionOptionalWrap *instruction) {
993 fprintf(irp->f, "@optionalWrap(");
994 ir_print_other_instruction(irp, instruction->operand);
995 fprintf(irp->f, ")result=");
996 ir_print_other_instruction(irp, instruction->result_loc);
917997}
918998
919999static void ir_print_err_wrap_code(IrPrint *irp, IrInstructionErrWrapCode *instruction) {
9201000 fprintf(irp->f, "@errWrapCode(");
921 ir_print_other_instruction(irp, instruction->value);
922 fprintf(irp->f, ")");
1001 ir_print_other_instruction(irp, instruction->operand);
1002 fprintf(irp->f, ")result=");
1003 ir_print_other_instruction(irp, instruction->result_loc);
9231004}
9241005
9251006static void ir_print_err_wrap_payload(IrPrint *irp, IrInstructionErrWrapPayload *instruction) {
9261007 fprintf(irp->f, "@errWrapPayload(");
927 ir_print_other_instruction(irp, instruction->value);
928 fprintf(irp->f, ")");
1008 ir_print_other_instruction(irp, instruction->operand);
1009 fprintf(irp->f, ")result=");
1010 ir_print_other_instruction(irp, instruction->result_loc);
9291011}
9301012
9311013static void ir_print_fn_proto(IrPrint *irp, IrInstructionFnProto *instruction) {
......@@ -971,12 +1053,11 @@ static void ir_print_ptr_cast_gen(IrPrint *irp, IrInstructionPtrCastGen *instruc
9711053 fprintf(irp->f, ")");
9721054}
9731055
974static void ir_print_bit_cast(IrPrint *irp, IrInstructionBitCast *instruction) {
1056static void ir_print_bit_cast_src(IrPrint *irp, IrInstructionBitCastSrc *instruction) {
9751057 fprintf(irp->f, "@bitCast(");
976 ir_print_other_instruction(irp, instruction->dest_type);
977 fprintf(irp->f, ",");
978 ir_print_other_instruction(irp, instruction->value);
979 fprintf(irp->f, ")");
1058 ir_print_other_instruction(irp, instruction->operand);
1059 fprintf(irp->f, ")result=");
1060 ir_print_result_loc(irp, &instruction->result_loc_bit_cast->base);
9801061}
9811062
9821063static void ir_print_bit_cast_gen(IrPrint *irp, IrInstructionBitCastGen *instruction) {
......@@ -1043,7 +1124,15 @@ static void ir_print_array_to_vector(IrPrint *irp, IrInstructionArrayToVector *i
10431124static void ir_print_vector_to_array(IrPrint *irp, IrInstructionVectorToArray *instruction) {
10441125 fprintf(irp->f, "VectorToArray(");
10451126 ir_print_other_instruction(irp, instruction->vector);
1046 fprintf(irp->f, ")");
1127 fprintf(irp->f, ")result=");
1128 ir_print_other_instruction(irp, instruction->result_loc);
1129}
1130
1131static void ir_print_ptr_of_array_to_slice(IrPrint *irp, IrInstructionPtrOfArrayToSlice *instruction) {
1132 fprintf(irp->f, "PtrOfArrayToSlice(");
1133 ir_print_other_instruction(irp, instruction->operand);
1134 fprintf(irp->f, ")result=");
1135 ir_print_other_instruction(irp, instruction->result_loc);
10471136}
10481137
10491138static void ir_print_assert_zero(IrPrint *irp, IrInstructionAssertZero *instruction) {
......@@ -1061,6 +1150,25 @@ static void ir_print_assert_non_null(IrPrint *irp, IrInstructionAssertNonNull *i
10611150static void ir_print_resize_slice(IrPrint *irp, IrInstructionResizeSlice *instruction) {
10621151 fprintf(irp->f, "@resizeSlice(");
10631152 ir_print_other_instruction(irp, instruction->operand);
1153 fprintf(irp->f, ")result=");
1154 ir_print_other_instruction(irp, instruction->result_loc);
1155}
1156
1157static void ir_print_alloca_src(IrPrint *irp, IrInstructionAllocaSrc *instruction) {
1158 fprintf(irp->f, "Alloca(align=");
1159 ir_print_other_instruction(irp, instruction->align);
1160 fprintf(irp->f, ",name=%s)", instruction->name_hint);
1161}
1162
1163static void ir_print_alloca_gen(IrPrint *irp, IrInstructionAllocaGen *instruction) {
1164 fprintf(irp->f, "Alloca(align=%" PRIu32 ",name=%s)", instruction->align, instruction->name_hint);
1165}
1166
1167static void ir_print_end_expr(IrPrint *irp, IrInstructionEndExpr *instruction) {
1168 fprintf(irp->f, "EndExpr(result=");
1169 ir_print_result_loc(irp, instruction->result_loc);
1170 fprintf(irp->f, ",value=");
1171 ir_print_other_instruction(irp, instruction->value);
10641172 fprintf(irp->f, ")");
10651173}
10661174
......@@ -1186,6 +1294,34 @@ static void ir_print_align_cast(IrPrint *irp, IrInstructionAlignCast *instructio
11861294 fprintf(irp->f, ")");
11871295}
11881296
1297static void ir_print_implicit_cast(IrPrint *irp, IrInstructionImplicitCast *instruction) {
1298 fprintf(irp->f, "@implicitCast(");
1299 ir_print_other_instruction(irp, instruction->dest_type);
1300 fprintf(irp->f, ",");
1301 ir_print_other_instruction(irp, instruction->target);
1302 fprintf(irp->f, ")");
1303}
1304
1305static void ir_print_resolve_result(IrPrint *irp, IrInstructionResolveResult *instruction) {
1306 fprintf(irp->f, "ResolveResult(");
1307 ir_print_result_loc(irp, instruction->result_loc);
1308 fprintf(irp->f, ")");
1309}
1310
1311static void ir_print_reset_result(IrPrint *irp, IrInstructionResetResult *instruction) {
1312 fprintf(irp->f, "ResetResult(");
1313 ir_print_result_loc(irp, instruction->result_loc);
1314 fprintf(irp->f, ")");
1315}
1316
1317static void ir_print_result_ptr(IrPrint *irp, IrInstructionResultPtr *instruction) {
1318 fprintf(irp->f, "ResultPtr(");
1319 ir_print_result_loc(irp, instruction->result_loc);
1320 fprintf(irp->f, ",");
1321 ir_print_other_instruction(irp, instruction->result);
1322 fprintf(irp->f, ")");
1323}
1324
11891325static void ir_print_opaque_type(IrPrint *irp, IrInstructionOpaqueType *instruction) {
11901326 fprintf(irp->f, "@OpaqueType()");
11911327}
......@@ -1463,7 +1599,7 @@ static void ir_print_decl_var_gen(IrPrint *irp, IrInstructionDeclVarGen *decl_va
14631599 fprintf(irp->f, "%s %s: %s align(%u) = ", var_or_const, name, buf_ptr(&var->var_type->name),
14641600 var->align_bytes);
14651601
1466 ir_print_other_instruction(irp, decl_var_instruction->init_value);
1602 ir_print_other_instruction(irp, decl_var_instruction->var_ptr);
14671603 if (decl_var_instruction->var->is_comptime != nullptr) {
14681604 fprintf(irp->f, " // comptime = ");
14691605 ir_print_other_instruction(irp, decl_var_instruction->var->is_comptime);
......@@ -1502,8 +1638,11 @@ static void ir_print_instruction(IrPrint *irp, IrInstruction *instruction) {
15021638 case IrInstructionIdCast:
15031639 ir_print_cast(irp, (IrInstructionCast *)instruction);
15041640 break;
1505 case IrInstructionIdCall:
1506 ir_print_call(irp, (IrInstructionCall *)instruction);
1641 case IrInstructionIdCallSrc:
1642 ir_print_call_src(irp, (IrInstructionCallSrc *)instruction);
1643 break;
1644 case IrInstructionIdCallGen:
1645 ir_print_call_gen(irp, (IrInstructionCallGen *)instruction);
15071646 break;
15081647 case IrInstructionIdUnOp:
15091648 ir_print_un_op(irp, (IrInstructionUnOp *)instruction);
......@@ -1523,12 +1662,6 @@ static void ir_print_instruction(IrPrint *irp, IrInstruction *instruction) {
15231662 case IrInstructionIdContainerInitFields:
15241663 ir_print_container_init_fields(irp, (IrInstructionContainerInitFields *)instruction);
15251664 break;
1526 case IrInstructionIdStructInit:
1527 ir_print_struct_init(irp, (IrInstructionStructInit *)instruction);
1528 break;
1529 case IrInstructionIdUnionInit:
1530 ir_print_union_init(irp, (IrInstructionUnionInit *)instruction);
1531 break;
15321665 case IrInstructionIdUnreachable:
15331666 ir_print_unreachable(irp, (IrInstructionUnreachable *)instruction);
15341667 break;
......@@ -1538,6 +1671,9 @@ static void ir_print_instruction(IrPrint *irp, IrInstruction *instruction) {
15381671 case IrInstructionIdVarPtr:
15391672 ir_print_var_ptr(irp, (IrInstructionVarPtr *)instruction);
15401673 break;
1674 case IrInstructionIdReturnPtr:
1675 ir_print_return_ptr(irp, (IrInstructionReturnPtr *)instruction);
1676 break;
15411677 case IrInstructionIdLoadPtr:
15421678 ir_print_load_ptr(irp, (IrInstructionLoadPtr *)instruction);
15431679 break;
......@@ -1550,12 +1686,6 @@ static void ir_print_instruction(IrPrint *irp, IrInstruction *instruction) {
15501686 case IrInstructionIdTypeOf:
15511687 ir_print_typeof(irp, (IrInstructionTypeOf *)instruction);
15521688 break;
1553 case IrInstructionIdToPtrType:
1554 ir_print_to_ptr_type(irp, (IrInstructionToPtrType *)instruction);
1555 break;
1556 case IrInstructionIdPtrTypeChild:
1557 ir_print_ptr_type_child(irp, (IrInstructionPtrTypeChild *)instruction);
1558 break;
15591689 case IrInstructionIdFieldPtr:
15601690 ir_print_field_ptr(irp, (IrInstructionFieldPtr *)instruction);
15611691 break;
......@@ -1634,6 +1764,9 @@ static void ir_print_instruction(IrPrint *irp, IrInstruction *instruction) {
16341764 case IrInstructionIdRef:
16351765 ir_print_ref(irp, (IrInstructionRef *)instruction);
16361766 break;
1767 case IrInstructionIdRefGen:
1768 ir_print_ref_gen(irp, (IrInstructionRefGen *)instruction);
1769 break;
16371770 case IrInstructionIdCompileErr:
16381771 ir_print_compile_err(irp, (IrInstructionCompileErr *)instruction);
16391772 break;
......@@ -1709,8 +1842,11 @@ static void ir_print_instruction(IrPrint *irp, IrInstruction *instruction) {
17091842 case IrInstructionIdMemcpy:
17101843 ir_print_memcpy(irp, (IrInstructionMemcpy *)instruction);
17111844 break;
1712 case IrInstructionIdSlice:
1713 ir_print_slice(irp, (IrInstructionSlice *)instruction);
1845 case IrInstructionIdSliceSrc:
1846 ir_print_slice_src(irp, (IrInstructionSliceSrc *)instruction);
1847 break;
1848 case IrInstructionIdSliceGen:
1849 ir_print_slice_gen(irp, (IrInstructionSliceGen *)instruction);
17141850 break;
17151851 case IrInstructionIdMemberCount:
17161852 ir_print_member_count(irp, (IrInstructionMemberCount *)instruction);
......@@ -1739,8 +1875,11 @@ static void ir_print_instruction(IrPrint *irp, IrInstruction *instruction) {
17391875 case IrInstructionIdOverflowOp:
17401876 ir_print_overflow_op(irp, (IrInstructionOverflowOp *)instruction);
17411877 break;
1742 case IrInstructionIdTestErr:
1743 ir_print_test_err(irp, (IrInstructionTestErr *)instruction);
1878 case IrInstructionIdTestErrSrc:
1879 ir_print_test_err_src(irp, (IrInstructionTestErrSrc *)instruction);
1880 break;
1881 case IrInstructionIdTestErrGen:
1882 ir_print_test_err_gen(irp, (IrInstructionTestErrGen *)instruction);
17441883 break;
17451884 case IrInstructionIdUnwrapErrCode:
17461885 ir_print_unwrap_err_code(irp, (IrInstructionUnwrapErrCode *)instruction);
......@@ -1749,7 +1888,7 @@ static void ir_print_instruction(IrPrint *irp, IrInstruction *instruction) {
17491888 ir_print_unwrap_err_payload(irp, (IrInstructionUnwrapErrPayload *)instruction);
17501889 break;
17511890 case IrInstructionIdOptionalWrap:
1752 ir_print_maybe_wrap(irp, (IrInstructionOptionalWrap *)instruction);
1891 ir_print_optional_wrap(irp, (IrInstructionOptionalWrap *)instruction);
17531892 break;
17541893 case IrInstructionIdErrWrapCode:
17551894 ir_print_err_wrap_code(irp, (IrInstructionErrWrapCode *)instruction);
......@@ -1769,8 +1908,8 @@ static void ir_print_instruction(IrPrint *irp, IrInstruction *instruction) {
17691908 case IrInstructionIdPtrCastGen:
17701909 ir_print_ptr_cast_gen(irp, (IrInstructionPtrCastGen *)instruction);
17711910 break;
1772 case IrInstructionIdBitCast:
1773 ir_print_bit_cast(irp, (IrInstructionBitCast *)instruction);
1911 case IrInstructionIdBitCastSrc:
1912 ir_print_bit_cast_src(irp, (IrInstructionBitCastSrc *)instruction);
17741913 break;
17751914 case IrInstructionIdBitCastGen:
17761915 ir_print_bit_cast_gen(irp, (IrInstructionBitCastGen *)instruction);
......@@ -1835,6 +1974,18 @@ static void ir_print_instruction(IrPrint *irp, IrInstruction *instruction) {
18351974 case IrInstructionIdAlignCast:
18361975 ir_print_align_cast(irp, (IrInstructionAlignCast *)instruction);
18371976 break;
1977 case IrInstructionIdImplicitCast:
1978 ir_print_implicit_cast(irp, (IrInstructionImplicitCast *)instruction);
1979 break;
1980 case IrInstructionIdResolveResult:
1981 ir_print_resolve_result(irp, (IrInstructionResolveResult *)instruction);
1982 break;
1983 case IrInstructionIdResetResult:
1984 ir_print_reset_result(irp, (IrInstructionResetResult *)instruction);
1985 break;
1986 case IrInstructionIdResultPtr:
1987 ir_print_result_ptr(irp, (IrInstructionResultPtr *)instruction);
1988 break;
18381989 case IrInstructionIdOpaqueType:
18391990 ir_print_opaque_type(irp, (IrInstructionOpaqueType *)instruction);
18401991 break;
......@@ -1943,6 +2094,9 @@ static void ir_print_instruction(IrPrint *irp, IrInstruction *instruction) {
19432094 case IrInstructionIdVectorToArray:
19442095 ir_print_vector_to_array(irp, (IrInstructionVectorToArray *)instruction);
19452096 break;
2097 case IrInstructionIdPtrOfArrayToSlice:
2098 ir_print_ptr_of_array_to_slice(irp, (IrInstructionPtrOfArrayToSlice *)instruction);
2099 break;
19462100 case IrInstructionIdAssertZero:
19472101 ir_print_assert_zero(irp, (IrInstructionAssertZero *)instruction);
19482102 break;
......@@ -1958,6 +2112,15 @@ static void ir_print_instruction(IrPrint *irp, IrInstruction *instruction) {
19582112 case IrInstructionIdUndeclaredIdent:
19592113 ir_print_undeclared_ident(irp, (IrInstructionUndeclaredIdent *)instruction);
19602114 break;
2115 case IrInstructionIdAllocaSrc:
2116 ir_print_alloca_src(irp, (IrInstructionAllocaSrc *)instruction);
2117 break;
2118 case IrInstructionIdAllocaGen:
2119 ir_print_alloca_gen(irp, (IrInstructionAllocaGen *)instruction);
2120 break;
2121 case IrInstructionIdEndExpr:
2122 ir_print_end_expr(irp, (IrInstructionEndExpr *)instruction);
2123 break;
19612124 }
19622125 fprintf(irp->f, "\n");
19632126}
src/parser.cpp+3-3
......@@ -344,7 +344,7 @@ static AstNode *ast_parse_bin_op_expr(
344344 op->data.bin_op_expr.op1 = left;
345345 op->data.bin_op_expr.op2 = right;
346346 break;
347 case NodeTypeUnwrapErrorExpr:
347 case NodeTypeCatchExpr:
348348 op->data.unwrap_err_expr.op1 = left;
349349 op->data.unwrap_err_expr.op2 = right;
350350 break;
......@@ -2404,7 +2404,7 @@ static AstNode *ast_parse_bitwise_op(ParseContext *pc) {
24042404 Token *catch_token = eat_token_if(pc, TokenIdKeywordCatch);
24052405 if (catch_token != nullptr) {
24062406 Token *payload = ast_parse_payload(pc);
2407 AstNode *res = ast_create_node(pc, NodeTypeUnwrapErrorExpr, catch_token);
2407 AstNode *res = ast_create_node(pc, NodeTypeCatchExpr, catch_token);
24082408 if (payload != nullptr)
24092409 res->data.unwrap_err_expr.symbol = token_symbol(pc, payload);
24102410
......@@ -2897,7 +2897,7 @@ void ast_visit_node_children(AstNode *node, void (*visit)(AstNode **, void *cont
28972897 visit_field(&node->data.bin_op_expr.op1, visit, context);
28982898 visit_field(&node->data.bin_op_expr.op2, visit, context);
28992899 break;
2900 case NodeTypeUnwrapErrorExpr:
2900 case NodeTypeCatchExpr:
29012901 visit_field(&node->data.unwrap_err_expr.op1, visit, context);
29022902 visit_field(&node->data.unwrap_err_expr.symbol, visit, context);
29032903 visit_field(&node->data.unwrap_err_expr.op2, visit, context);
std/event/fs.zig+1-2
......@@ -1290,10 +1290,9 @@ pub fn Watch(comptime V: type) type {
12901290 error.FileDescriptorAlreadyPresentInSet => unreachable,
12911291 error.OperationCausesCircularLoop => unreachable,
12921292 error.FileDescriptorNotRegistered => unreachable,
1293 error.SystemResources => error.SystemResources,
1294 error.UserResourceLimitReached => error.UserResourceLimitReached,
12951293 error.FileDescriptorIncompatibleWithEpoll => unreachable,
12961294 error.Unexpected => unreachable,
1295 else => |e| e,
12971296 };
12981297 await (async channel.put(transformed_err) catch unreachable);
12991298 };
std/event/lock.zig-1
......@@ -123,7 +123,6 @@ pub const Lock = struct {
123123};
124124
125125test "std.event.Lock" {
126 // https://github.com/ziglang/zig/issues/1908
127126 if (builtin.single_threaded) return error.SkipZigTest;
128127
129128 const allocator = std.heap.direct_allocator;
std/event/net.zig+2-2
......@@ -263,8 +263,8 @@ pub async fn connect(loop: *Loop, _address: *const std.net.Address) !File {
263263}
264264
265265test "listen on a port, send bytes, receive bytes" {
266 // https://github.com/ziglang/zig/issues/1908
267 if (builtin.single_threaded) return error.SkipZigTest;
266 // https://github.com/ziglang/zig/issues/2377
267 if (true) return error.SkipZigTest;
268268
269269 if (builtin.os != builtin.Os.linux) {
270270 // TODO build abstractions for other operating systems
std/event/rwlock.zig+2-2
......@@ -212,8 +212,8 @@ pub const RwLock = struct {
212212};
213213
214214test "std.event.RwLock" {
215 // https://github.com/ziglang/zig/issues/1908
216 if (builtin.single_threaded or builtin.os != builtin.Os.linux) return error.SkipZigTest;
215 // https://github.com/ziglang/zig/issues/2377
216 if (true) return error.SkipZigTest;
217217
218218 const allocator = std.heap.direct_allocator;
219219
std/heap.zig+2-2
......@@ -360,9 +360,9 @@ pub const ArenaAllocator = struct {
360360 var it = self.buffer_list.first;
361361 while (it) |node| {
362362 // this has to occur before the free because the free frees node
363 it = node.next;
364
363 const next_it = node.next;
365364 self.child_allocator.free(node.data);
365 it = next_it;
366366 }
367367 }
368368
std/io.zig+10-10
......@@ -164,32 +164,32 @@ pub fn InStream(comptime ReadError: type) type {
164164
165165 /// Reads a native-endian integer
166166 pub fn readIntNative(self: *Self, comptime T: type) !T {
167 var bytes: [(T.bit_count + 7 )/ 8]u8 = undefined;
167 var bytes: [(T.bit_count + 7) / 8]u8 = undefined;
168168 try self.readNoEof(bytes[0..]);
169169 return mem.readIntNative(T, &bytes);
170170 }
171171
172172 /// Reads a foreign-endian integer
173173 pub fn readIntForeign(self: *Self, comptime T: type) !T {
174 var bytes: [(T.bit_count + 7 )/ 8]u8 = undefined;
174 var bytes: [(T.bit_count + 7) / 8]u8 = undefined;
175175 try self.readNoEof(bytes[0..]);
176176 return mem.readIntForeign(T, &bytes);
177177 }
178178
179179 pub fn readIntLittle(self: *Self, comptime T: type) !T {
180 var bytes: [(T.bit_count + 7 )/ 8]u8 = undefined;
180 var bytes: [(T.bit_count + 7) / 8]u8 = undefined;
181181 try self.readNoEof(bytes[0..]);
182182 return mem.readIntLittle(T, &bytes);
183183 }
184184
185185 pub fn readIntBig(self: *Self, comptime T: type) !T {
186 var bytes: [(T.bit_count + 7 )/ 8]u8 = undefined;
186 var bytes: [(T.bit_count + 7) / 8]u8 = undefined;
187187 try self.readNoEof(bytes[0..]);
188188 return mem.readIntBig(T, &bytes);
189189 }
190190
191191 pub fn readInt(self: *Self, comptime T: type, endian: builtin.Endian) !T {
192 var bytes: [(T.bit_count + 7 )/ 8]u8 = undefined;
192 var bytes: [(T.bit_count + 7) / 8]u8 = undefined;
193193 try self.readNoEof(bytes[0..]);
194194 return mem.readInt(T, &bytes, endian);
195195 }
......@@ -249,32 +249,32 @@ pub fn OutStream(comptime WriteError: type) type {
249249
250250 /// Write a native-endian integer.
251251 pub fn writeIntNative(self: *Self, comptime T: type, value: T) Error!void {
252 var bytes: [(T.bit_count + 7 )/ 8]u8 = undefined;
252 var bytes: [(T.bit_count + 7) / 8]u8 = undefined;
253253 mem.writeIntNative(T, &bytes, value);
254254 return self.writeFn(self, bytes);
255255 }
256256
257257 /// Write a foreign-endian integer.
258258 pub fn writeIntForeign(self: *Self, comptime T: type, value: T) Error!void {
259 var bytes: [(T.bit_count + 7 )/ 8]u8 = undefined;
259 var bytes: [(T.bit_count + 7) / 8]u8 = undefined;
260260 mem.writeIntForeign(T, &bytes, value);
261261 return self.writeFn(self, bytes);
262262 }
263263
264264 pub fn writeIntLittle(self: *Self, comptime T: type, value: T) Error!void {
265 var bytes: [(T.bit_count + 7 )/ 8]u8 = undefined;
265 var bytes: [(T.bit_count + 7) / 8]u8 = undefined;
266266 mem.writeIntLittle(T, &bytes, value);
267267 return self.writeFn(self, bytes);
268268 }
269269
270270 pub fn writeIntBig(self: *Self, comptime T: type, value: T) Error!void {
271 var bytes: [(T.bit_count + 7 )/ 8]u8 = undefined;
271 var bytes: [(T.bit_count + 7) / 8]u8 = undefined;
272272 mem.writeIntBig(T, &bytes, value);
273273 return self.writeFn(self, bytes);
274274 }
275275
276276 pub fn writeInt(self: *Self, comptime T: type, value: T, endian: builtin.Endian) Error!void {
277 var bytes: [(T.bit_count + 7 )/ 8]u8 = undefined;
277 var bytes: [(T.bit_count + 7) / 8]u8 = undefined;
278278 mem.writeInt(T, &bytes, value, endian);
279279 return self.writeFn(self, bytes);
280280 }
std/io/test.zig+4-1
......@@ -597,7 +597,10 @@ test "c out stream" {
597597
598598 const filename = c"tmp_io_test_file.txt";
599599 const out_file = std.c.fopen(filename, c"w") orelse return error.UnableToOpenTestFile;
600 defer fs.deleteFileC(filename) catch {};
600 defer {
601 _ = std.c.fclose(out_file);
602 fs.deleteFileC(filename) catch {};
603 }
601604
602605 const out_stream = &io.COutStream.init(out_file).stream;
603606 try out_stream.print("hi: {}\n", i32(123));
std/json.zig+2-1
......@@ -876,8 +876,9 @@ pub const TokenStream = struct {
876876
877877 pub fn next(self: *TokenStream) !?Token {
878878 if (self.token) |token| {
879 const copy = token;
879880 self.token = null;
880 return token;
881 return copy;
881882 }
882883
883884 var t1: ?Token = undefined;
std/os/windows.zig+2
......@@ -348,6 +348,7 @@ pub const DeleteFileError = error{
348348 FileNotFound,
349349 AccessDenied,
350350 NameTooLong,
351 FileBusy,
351352 Unexpected,
352353};
353354
......@@ -363,6 +364,7 @@ pub fn DeleteFileW(filename: [*]const u16) DeleteFileError!void {
363364 ERROR.ACCESS_DENIED => return error.AccessDenied,
364365 ERROR.FILENAME_EXCED_RANGE => return error.NameTooLong,
365366 ERROR.INVALID_PARAMETER => return error.NameTooLong,
367 ERROR.SHARING_VIOLATION => return error.FileBusy,
366368 else => |err| return unexpectedError(err),
367369 }
368370 }
std/special/bootstrap.zig+4-4
......@@ -114,20 +114,20 @@ extern fn main(c_argc: i32, c_argv: [*][*]u8, c_envp: [*]?[*]u8) i32 {
114114// and we want fewer call frames in stack traces.
115115inline fn callMain() u8 {
116116 switch (@typeId(@typeOf(root.main).ReturnType)) {
117 builtin.TypeId.NoReturn => {
117 .NoReturn => {
118118 root.main();
119119 },
120 builtin.TypeId.Void => {
120 .Void => {
121121 root.main();
122122 return 0;
123123 },
124 builtin.TypeId.Int => {
124 .Int => {
125125 if (@typeOf(root.main).ReturnType.bit_count != 8) {
126126 @compileError("expected return type of main to be 'u8', 'noreturn', 'void', or '!void'");
127127 }
128128 return root.main();
129129 },
130 builtin.TypeId.ErrorUnion => {
130 .ErrorUnion => {
131131 root.main() catch |err| {
132132 std.debug.warn("error: {}\n", @errorName(err));
133133 if (builtin.os != builtin.Os.zen) {
std/special/compiler_rt/comparetf2.zig+9-6
......@@ -73,12 +73,15 @@ pub extern fn __getf2(a: f128, b: f128) c_int {
7373
7474 if (aAbs > infRep or bAbs > infRep) return GE_UNORDERED;
7575 if ((aAbs | bAbs) == 0) return GE_EQUAL;
76 return if ((aInt & bInt) >= 0) if (aInt < bInt)
77 GE_LESS
78 else if (aInt == bInt)
79 GE_EQUAL
80 else
81 GE_GREATER else if (aInt > bInt)
76 // zig fmt issue here, see https://github.com/ziglang/zig/issues/2661
77 return if ((aInt & bInt) >= 0)
78 if (aInt < bInt)
79 GE_LESS
80 else if (aInt == bInt)
81 GE_EQUAL
82 else
83 GE_GREATER
84 else if (aInt > bInt)
8285 GE_LESS
8386 else if (aInt == bInt)
8487 GE_EQUAL
std/special/panic.zig+3-4
......@@ -9,16 +9,15 @@ const std = @import("std");
99pub fn panic(msg: []const u8, error_return_trace: ?*builtin.StackTrace) noreturn {
1010 @setCold(true);
1111 switch (builtin.os) {
12 // TODO: fix panic in zen
13 builtin.Os.freestanding, builtin.Os.zen => {
12 .freestanding => {
1413 while (true) {}
1514 },
16 builtin.Os.wasi => {
15 .wasi => {
1716 std.debug.warn("{}", msg);
1817 _ = std.os.wasi.proc_raise(std.os.wasi.SIGABRT);
1918 unreachable;
2019 },
21 builtin.Os.uefi => {
20 .uefi => {
2221 // TODO look into using the debug info and logging helpful messages
2322 std.os.abort();
2423 },
std/zig/parser_test.zig+2-2
......@@ -1,4 +1,4 @@
1// TODO remove `use` keyword eventually
1// TODO remove `use` keyword eventually: https://github.com/ziglang/zig/issues/2591
22test "zig fmt: change use to usingnamespace" {
33 try testTransform(
44 \\use @import("std");
......@@ -1105,7 +1105,7 @@ test "zig fmt: first line comment in struct initializer" {
11051105 try testCanonical(
11061106 \\pub async fn acquire(self: *Self) HeldLock {
11071107 \\ return HeldLock{
1108 \\ // TODO guaranteed allocation elision
1108 \\ // guaranteed allocation elision
11091109 \\ .held = await (async self.lock.acquire() catch unreachable),
11101110 \\ .value = &self.private_data,
11111111 \\ };
std/zig/render.zig+3-3
......@@ -939,10 +939,10 @@ fn renderExpression(
939939 }
940940
941941 switch (container_decl.init_arg_expr) {
942 ast.Node.ContainerDecl.InitArg.None => {
942 .None => {
943943 try renderToken(tree, stream, container_decl.kind_token, indent, start_col, Space.Space); // union
944944 },
945 ast.Node.ContainerDecl.InitArg.Enum => |enum_tag_type| {
945 .Enum => |enum_tag_type| {
946946 try renderToken(tree, stream, container_decl.kind_token, indent, start_col, Space.None); // union
947947
948948 const lparen = tree.nextToken(container_decl.kind_token);
......@@ -962,7 +962,7 @@ fn renderExpression(
962962 try renderToken(tree, stream, tree.nextToken(enum_token), indent, start_col, Space.Space); // )
963963 }
964964 },
965 ast.Node.ContainerDecl.InitArg.Type => |type_expr| {
965 .Type => |type_expr| {
966966 try renderToken(tree, stream, container_decl.kind_token, indent, start_col, Space.None); // union
967967
968968 const lparen = tree.nextToken(container_decl.kind_token);
test/compile_errors.zig+75-40
......@@ -49,16 +49,11 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
4949 \\const Foo = struct {
5050 \\ a: undefined,
5151 \\};
52 \\const Bar = union {
53 \\ a: undefined,
54 \\};
55 \\pub fn main() void {
52 \\export fn entry1() void {
5653 \\ const foo: Foo = undefined;
57 \\ const bar: Bar = undefined;
5854 \\}
5955 ,
6056 "tmp.zig:2:8: error: expected type 'type', found '(undefined)'",
61 "tmp.zig:5:8: error: expected type 'type', found '(undefined)'",
6257 );
6358
6459 cases.add(
......@@ -461,13 +456,25 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
461456 \\const G = packed struct {
462457 \\ x: Enum,
463458 \\};
464 \\export fn entry() void {
459 \\export fn entry1() void {
465460 \\ var a: A = undefined;
461 \\}
462 \\export fn entry2() void {
466463 \\ var b: B = undefined;
464 \\}
465 \\export fn entry3() void {
467466 \\ var r: C = undefined;
467 \\}
468 \\export fn entry4() void {
468469 \\ var d: D = undefined;
470 \\}
471 \\export fn entry5() void {
469472 \\ var e: E = undefined;
473 \\}
474 \\export fn entry6() void {
470475 \\ var f: F = undefined;
476 \\}
477 \\export fn entry7() void {
471478 \\ var g: G = undefined;
472479 \\}
473480 \\const S = struct {
......@@ -489,7 +496,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
489496 "tmp.zig:14:5: error: non-packed, non-extern struct 'U' not allowed in packed struct; no guaranteed in-memory representation",
490497 "tmp.zig:17:5: error: type '?anyerror' not allowed in packed struct; no guaranteed in-memory representation",
491498 "tmp.zig:20:5: error: type 'Enum' not allowed in packed struct; no guaranteed in-memory representation",
492 "tmp.zig:38:14: note: enum declaration does not specify an integer tag type",
499 "tmp.zig:50:14: note: enum declaration does not specify an integer tag type",
493500 );
494501
495502 cases.addCase(x: {
......@@ -721,7 +728,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
721728 \\ var oops = @bitCast(u7, byte);
722729 \\}
723730 ,
724 "tmp.zig:2:16: error: destination type 'u7' has 7 bits but source type 'u8' has 8 bits",
731 "tmp.zig:2:25: error: destination type 'u7' has 7 bits but source type 'u8' has 8 bits",
725732 );
726733
727734 cases.add(
......@@ -1381,7 +1388,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
13811388 \\ for (xx) |f| {}
13821389 \\}
13831390 ,
1384 "tmp.zig:7:15: error: variable of type 'Foo' must be const or comptime",
1391 "tmp.zig:7:5: error: values of type 'Foo' must be comptime known, but index value is runtime known",
13851392 );
13861393
13871394 cases.add(
......@@ -2250,6 +2257,9 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
22502257 \\}
22512258 \\
22522259 \\extern fn bar(x: *void) void { }
2260 \\export fn entry2() void {
2261 \\ bar(&{});
2262 \\}
22532263 ,
22542264 "tmp.zig:1:30: error: parameter of type '*void' has 0 bits; not allowed in function with calling convention 'ccc'",
22552265 "tmp.zig:7:18: error: parameter of type '*void' has 0 bits; not allowed in function with calling convention 'ccc'",
......@@ -2576,7 +2586,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
25762586 \\
25772587 \\fn b() void {}
25782588 ,
2579 "tmp.zig:3:5: error: unreachable code",
2589 "tmp.zig:3:6: error: unreachable code",
25802590 );
25812591
25822592 cases.add(
......@@ -2596,7 +2606,6 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
25962606 \\}
25972607 ,
25982608 "tmp.zig:3:5: error: use of undeclared identifier 'b'",
2599 "tmp.zig:4:5: error: use of undeclared identifier 'c'",
26002609 );
26012610
26022611 cases.add(
......@@ -2662,7 +2671,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
26622671 \\ const a: noreturn = {};
26632672 \\}
26642673 ,
2665 "tmp.zig:2:14: error: variable of type 'noreturn' not allowed",
2674 "tmp.zig:2:25: error: expected type 'noreturn', found 'void'",
26662675 );
26672676
26682677 cases.add(
......@@ -2725,9 +2734,13 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
27252734 \\ var bad : bool = undefined;
27262735 \\ bad[bad] = bad[bad];
27272736 \\}
2737 \\export fn g() void {
2738 \\ var bad : bool = undefined;
2739 \\ _ = bad[bad];
2740 \\}
27282741 ,
27292742 "tmp.zig:3:8: error: array access of non-array type 'bool'",
2730 "tmp.zig:3:19: error: array access of non-array type 'bool'",
2743 "tmp.zig:7:12: error: array access of non-array type 'bool'",
27312744 );
27322745
27332746 cases.add(
......@@ -2737,9 +2750,14 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
27372750 \\ var bad = false;
27382751 \\ array[bad] = array[bad];
27392752 \\}
2753 \\export fn g() void {
2754 \\ var array = "aoeu";
2755 \\ var bad = false;
2756 \\ _ = array[bad];
2757 \\}
27402758 ,
27412759 "tmp.zig:4:11: error: expected type 'usize', found 'bool'",
2742 "tmp.zig:4:24: error: expected type 'usize', found 'bool'",
2760 "tmp.zig:9:15: error: expected type 'usize', found 'bool'",
27432761 );
27442762
27452763 cases.add(
......@@ -2757,12 +2775,14 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
27572775 "missing else clause",
27582776 \\fn f(b: bool) void {
27592777 \\ const x : i32 = if (b) h: { break :h 1; };
2778 \\}
2779 \\fn g(b: bool) void {
27602780 \\ const y = if (b) h: { break :h i32(1); };
27612781 \\}
2762 \\export fn entry() void { f(true); }
2782 \\export fn entry() void { f(true); g(true); }
27632783 ,
27642784 "tmp.zig:2:42: error: integer value 1 cannot be implicitly casted to type 'void'",
2765 "tmp.zig:3:15: error: incompatible types: 'i32' and 'void'",
2785 "tmp.zig:5:15: error: incompatible types: 'i32' and 'void'",
27662786 );
27672787
27682788 cases.add(
......@@ -2773,9 +2793,13 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
27732793 \\ a.foo = 1;
27742794 \\ const y = a.bar;
27752795 \\}
2796 \\export fn g() void {
2797 \\ var a : A = undefined;
2798 \\ const y = a.bar;
2799 \\}
27762800 ,
27772801 "tmp.zig:4:6: error: no member named 'foo' in struct 'A'",
2778 "tmp.zig:5:16: error: no member named 'bar' in struct 'A'",
2802 "tmp.zig:9:16: error: no member named 'bar' in struct 'A'",
27792803 );
27802804
27812805 cases.add(
......@@ -2920,7 +2944,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
29202944 \\ _ = foo;
29212945 \\}
29222946 ,
2923 "tmp.zig:1:19: error: type '[3]u16' does not support struct initialization syntax",
2947 "tmp.zig:1:21: error: type '[3]u16' does not support struct initialization syntax",
29242948 );
29252949
29262950 cases.add(
......@@ -3239,7 +3263,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
32393263 \\
32403264 \\export fn entry() usize { return @sizeOf(@typeOf(Foo)); }
32413265 ,
3242 "tmp.zig:5:25: error: unable to evaluate constant expression",
3266 "tmp.zig:5:18: error: unable to evaluate constant expression",
32433267 "tmp.zig:2:12: note: called from here",
32443268 "tmp.zig:2:8: note: called from here",
32453269 );
......@@ -3856,7 +3880,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
38563880 \\ return 2;
38573881 \\}
38583882 ,
3859 "tmp.zig:2:15: error: values of type 'comptime_int' must be comptime known",
3883 "tmp.zig:5:17: error: cannot store runtime value in type 'comptime_int'",
38603884 );
38613885
38623886 cases.add(
......@@ -5108,7 +5132,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
51085132 \\ const array = [2]u8{1, 2, 3};
51095133 \\}
51105134 ,
5111 "tmp.zig:2:24: error: expected [2]u8 literal, found [3]u8 literal",
5135 "tmp.zig:2:31: error: index 2 outside array of size 2",
51125136 );
51135137
51145138 cases.add(
......@@ -5125,36 +5149,47 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
51255149
51265150 cases.add(
51275151 "non-const variables of things that require const variables",
5128 \\const Opaque = @OpaqueType();
5129 \\
5130 \\export fn entry(opaque: *Opaque) void {
5152 \\export fn entry1() void {
51315153 \\ var m2 = &2;
5132 \\ const y: u32 = m2.*;
5133 \\
5154 \\}
5155 \\export fn entry2() void {
51345156 \\ var a = undefined;
5157 \\}
5158 \\export fn entry3() void {
51355159 \\ var b = 1;
5160 \\}
5161 \\export fn entry4() void {
51365162 \\ var c = 1.0;
5163 \\}
5164 \\export fn entry5() void {
51375165 \\ var d = null;
5166 \\}
5167 \\export fn entry6(opaque: *Opaque) void {
51385168 \\ var e = opaque.*;
5169 \\}
5170 \\export fn entry7() void {
51395171 \\ var f = i32;
5172 \\}
5173 \\export fn entry8() void {
51405174 \\ var h = (Foo {}).bar;
5141 \\
5175 \\}
5176 \\export fn entry9() void {
51425177 \\ var z: noreturn = return;
51435178 \\}
5144 \\
5179 \\const Opaque = @OpaqueType();
51455180 \\const Foo = struct {
51465181 \\ fn bar(self: *const Foo) void {}
51475182 \\};
51485183 ,
5149 "tmp.zig:4:4: error: variable of type '*comptime_int' must be const or comptime",
5150 "tmp.zig:7:4: error: variable of type '(undefined)' must be const or comptime",
5184 "tmp.zig:2:4: error: variable of type '*comptime_int' must be const or comptime",
5185 "tmp.zig:5:4: error: variable of type '(undefined)' must be const or comptime",
51515186 "tmp.zig:8:4: error: variable of type 'comptime_int' must be const or comptime",
5152 "tmp.zig:9:4: error: variable of type 'comptime_float' must be const or comptime",
5153 "tmp.zig:10:4: error: variable of type '(null)' must be const or comptime",
5154 "tmp.zig:11:4: error: variable of type 'Opaque' not allowed",
5155 "tmp.zig:12:4: error: variable of type 'type' must be const or comptime",
5156 "tmp.zig:13:4: error: variable of type '(bound fn(*const Foo) void)' must be const or comptime",
5157 "tmp.zig:15:4: error: unreachable code",
5187 "tmp.zig:11:4: error: variable of type 'comptime_float' must be const or comptime",
5188 "tmp.zig:14:4: error: variable of type '(null)' must be const or comptime",
5189 "tmp.zig:17:4: error: variable of type 'Opaque' not allowed",
5190 "tmp.zig:20:4: error: variable of type 'type' must be const or comptime",
5191 "tmp.zig:23:4: error: variable of type '(bound fn(*const Foo) void)' must be const or comptime",
5192 "tmp.zig:26:4: error: unreachable code",
51585193 );
51595194
51605195 cases.add(
......@@ -5300,7 +5335,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
53005335 \\ }
53015336 \\}
53025337 ,
5303 "tmp.zig:37:16: error: cannot store runtime value in compile time variable",
5338 "tmp.zig:37:29: error: cannot store runtime value in compile time variable",
53045339 );
53055340
53065341 cases.add(
......@@ -5924,7 +5959,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
59245959 \\ const foo = Foo { .Bar = x, .Baz = u8 };
59255960 \\}
59265961 ,
5927 "tmp.zig:7:30: error: unable to evaluate constant expression",
5962 "tmp.zig:7:23: error: unable to evaluate constant expression",
59285963 );
59295964
59305965 cases.add(
......@@ -5938,7 +5973,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
59385973 \\ const foo = Foo { .Bar = x };
59395974 \\}
59405975 ,
5941 "tmp.zig:7:30: error: unable to evaluate constant expression",
5976 "tmp.zig:7:23: error: unable to evaluate constant expression",
59425977 );
59435978
59445979 cases.addTest(
test/stage1/behavior/array.zig+8-2
......@@ -172,6 +172,12 @@ fn plusOne(x: u32) u32 {
172172 return x + 1;
173173}
174174
175test "runtime initialize array elem and then implicit cast to slice" {
176 var two: i32 = 2;
177 const x: []const i32 = [_]i32{two};
178 expect(x[0] == 2);
179}
180
175181test "array literal as argument to function" {
176182 const S = struct {
177183 fn entry(two: i32) void {
......@@ -227,7 +233,7 @@ test "double nested array to const slice cast in array literal" {
227233
228234 const cases2 = [_][]const i32{
229235 [_]i32{1},
230 [_]i32{ two, 3 },
236 &[_]i32{ two, 3 },
231237 };
232238 expect(cases2.len == 2);
233239 expect(cases2[0].len == 1);
......@@ -238,7 +244,7 @@ test "double nested array to const slice cast in array literal" {
238244
239245 const cases3 = [_][]const []const i32{
240246 [_][]const i32{[_]i32{1}},
241 [_][]const i32{[_]i32{ two, 3 }},
247 &[_][]const i32{&[_]i32{ two, 3 }},
242248 [_][]const i32{
243249 [_]i32{4},
244250 [_]i32{ 5, 6, 7 },
test/stage1/behavior/bitcast.zig+13
......@@ -112,3 +112,16 @@ test "bitcast packed struct to integer and back" {
112112 S.doTheTest();
113113 comptime S.doTheTest();
114114}
115
116test "implicit cast to error union by returning" {
117 const S = struct {
118 fn entry() void {
119 expect((func(-1) catch unreachable) == maxInt(u64));
120 }
121 pub fn func(sz: i64) anyerror!u64 {
122 return @bitCast(u64, sz);
123 }
124 };
125 S.entry();
126 comptime S.entry();
127}
test/stage1/behavior/cast.zig+37
......@@ -482,3 +482,40 @@ test "@intCast to u0 and use the result" {
482482 S.doTheTest(0, 1, 0);
483483 comptime S.doTheTest(0, 1, 0);
484484}
485
486test "peer type resolution: unreachable, null, slice" {
487 const S = struct {
488 fn doTheTest(num: usize, word: []const u8) void {
489 const result = switch (num) {
490 0 => null,
491 1 => word,
492 else => unreachable,
493 };
494 expect(mem.eql(u8, result.?, "hi"));
495 }
496 };
497 S.doTheTest(1, "hi");
498}
499
500test "peer type resolution: unreachable, error set, unreachable" {
501 const Error = error {
502 FileDescriptorAlreadyPresentInSet,
503 OperationCausesCircularLoop,
504 FileDescriptorNotRegistered,
505 SystemResources,
506 UserResourceLimitReached,
507 FileDescriptorIncompatibleWithEpoll,
508 Unexpected,
509 };
510 var err = Error.SystemResources;
511 const transformed_err = switch (err) {
512 error.FileDescriptorAlreadyPresentInSet => unreachable,
513 error.OperationCausesCircularLoop => unreachable,
514 error.FileDescriptorNotRegistered => unreachable,
515 error.SystemResources => error.SystemResources,
516 error.UserResourceLimitReached => error.UserResourceLimitReached,
517 error.FileDescriptorIncompatibleWithEpoll => unreachable,
518 error.Unexpected => unreachable,
519 };
520 expect(transformed_err == error.SystemResources);
521}
test/stage1/behavior/error.zig+40
......@@ -335,3 +335,43 @@ test "debug info for optional error set" {
335335 const SomeError = error{Hello};
336336 var a_local_variable: ?SomeError = null;
337337}
338
339test "nested catch" {
340 const S = struct {
341 fn entry() void {
342 expectError(error.Bad, func());
343 }
344 fn fail() anyerror!Foo {
345 return error.Wrong;
346 }
347 fn func() anyerror!Foo {
348 const x = fail() catch
349 fail() catch
350 return error.Bad;
351 unreachable;
352 }
353 const Foo = struct {
354 field: i32,
355 };
356 };
357 S.entry();
358 comptime S.entry();
359}
360
361test "implicit cast to optional to error union to return result loc" {
362 const S = struct {
363 fn entry() void {
364 if (func(undefined)) |opt| {
365 expect(opt != null);
366 } else |_| @panic("expected non error");
367 }
368 fn func(f: *Foo) anyerror!?*Foo {
369 return f;
370 }
371 const Foo = struct {
372 field: i32,
373 };
374 };
375 S.entry();
376 //comptime S.entry(); TODO
377}
test/stage1/behavior/eval.zig+12-2
......@@ -190,6 +190,17 @@ fn testTryToTrickEvalWithRuntimeIf(b: bool) usize {
190190 }
191191}
192192
193test "inlined loop has array literal with elided runtime scope on first iteration but not second iteration" {
194 var runtime = [1]i32{3};
195 comptime var i: usize = 0;
196 inline while (i < 2) : (i += 1) {
197 const result = if (i == 0) [1]i32{2} else runtime;
198 }
199 comptime {
200 expect(i == 2);
201 }
202}
203
193204fn max(comptime T: type, a: T, b: T) T {
194205 if (T == bool) {
195206 return a or b;
......@@ -756,8 +767,7 @@ test "comptime bitwise operators" {
756767test "*align(1) u16 is the same as *align(1:0:2) u16" {
757768 comptime {
758769 expect(*align(1:0:2) u16 == *align(1) u16);
759 // TODO add parsing support for this syntax
760 //expect(*align(:0:2) u16 == *u16);
770 expect(*align(:0:2) u16 == *u16);
761771 }
762772}
763773
test/stage1/behavior/fn.zig+23
......@@ -205,3 +205,26 @@ test "extern struct with stdcallcc fn pointer" {
205205 s.ptr = S.foo;
206206 expect(s.ptr() == 1234);
207207}
208
209test "implicit cast fn call result to optional in field result" {
210 const S = struct {
211 fn entry() void {
212 var x = Foo{
213 .field = optionalPtr(),
214 };
215 expect(x.field.?.* == 999);
216 }
217
218 const glob: i32 = 999;
219
220 fn optionalPtr() *const i32 {
221 return &glob;
222 }
223
224 const Foo = struct {
225 field: ?*const i32,
226 };
227 };
228 S.entry();
229 comptime S.entry();
230}
test/stage1/behavior/for.zig+16
......@@ -110,3 +110,19 @@ fn testContinueOuter() void {
110110 }
111111 expect(counter == array.len);
112112}
113
114test "2 break statements and an else" {
115 const S = struct {
116 fn entry(t: bool, f: bool) void {
117 var buf: [10]u8 = undefined;
118 var ok = false;
119 ok = for (buf) |item| {
120 if (f) break false;
121 if (t) break true;
122 } else false;
123 expect(ok);
124 }
125 };
126 S.entry(true, false);
127 comptime S.entry(true, false);
128}
test/stage1/behavior/if.zig+11
......@@ -52,3 +52,14 @@ test "unwrap mutable global var" {
5252 expect(e == error.SomeError);
5353 }
5454}
55
56test "labeled break inside comptime if inside runtime if" {
57 var answer: i32 = 0;
58 var c = true;
59 if (c) {
60 answer = if (true) blk: {
61 break :blk i32(42);
62 };
63 }
64 expect(answer == 42);
65}
test/stage1/behavior/misc.zig+8
......@@ -698,3 +698,11 @@ test "unicode escape in character literal" {
698698 var a: u24 = '\U01f4a9';
699699 expect(a == 128169);
700700}
701
702test "result location zero sized array inside struct field implicit cast to slice" {
703 const E = struct {
704 entries: []u32,
705 };
706 var foo = E{ .entries = [_]u32{} };
707 expect(foo.entries.len == 0);
708}
test/stage1/behavior/optional.zig+23-2
......@@ -76,6 +76,27 @@ test "unwrap function call with optional pointer return value" {
7676 }
7777 };
7878 S.entry();
79 // TODO https://github.com/ziglang/zig/issues/1901
80 //comptime S.entry();
79 comptime S.entry();
80}
81
82test "nested orelse" {
83 const S = struct {
84 fn entry() void {
85 expect(func() == null);
86 }
87 fn maybe() ?Foo {
88 return null;
89 }
90 fn func() ?Foo {
91 const x = maybe() orelse
92 maybe() orelse
93 return null;
94 unreachable;
95 }
96 const Foo = struct {
97 field: i32,
98 };
99 };
100 S.entry();
101 comptime S.entry();
81102}
test/stage1/behavior/struct.zig+21
......@@ -578,3 +578,24 @@ test "default struct initialization fields" {
578578 };
579579 expectEqual(1239, x.a + x.b);
580580}
581
582test "extern fn returns struct by value" {
583 const S = struct {
584 fn entry() void {
585 var x = makeBar(10);
586 expectEqual(i32(10), x.handle);
587 }
588
589 const ExternBar = extern struct {
590 handle: i32,
591 };
592
593 extern fn makeBar(t: i32) ExternBar {
594 return ExternBar{
595 .handle = t,
596 };
597 }
598 };
599 S.entry();
600 comptime S.entry();
601}
test/stage1/behavior/switch.zig+16
......@@ -360,3 +360,19 @@ test "switch prongs with error set cases make a new error set type for capture v
360360 S.doTheTest();
361361 comptime S.doTheTest();
362362}
363
364test "return result loc and then switch with range implicit casted to error union" {
365 const S = struct {
366 fn doTheTest() void {
367 expect((func(0xb) catch unreachable) == 0xb);
368 }
369 fn func(d: u8) anyerror!u8 {
370 return switch (d) {
371 0xa...0xf => d,
372 else => unreachable,
373 };
374 }
375 };
376 S.doTheTest();
377 comptime S.doTheTest();
378}
test/stage1/behavior/union.zig+20
......@@ -402,3 +402,23 @@ test "comptime union field value equality" {
402402 expect(a0 != a1);
403403 expect(b0 != b1);
404404}
405
406test "return union init with void payload" {
407 const S = struct {
408 fn entry() void {
409 expect(func().state == State.one);
410 }
411 const Outer = union(enum) {
412 state: State,
413 };
414 const State = union(enum) {
415 one: void,
416 two: u32,
417 };
418 fn func() Outer {
419 return Outer{ .state = State{ .one = {} }};
420 }
421 };
422 S.entry();
423 comptime S.entry();
424}
test/stage1/behavior/vector.zig+13
......@@ -61,3 +61,16 @@ test "vector bit operators" {
6161 S.doTheTest();
6262 comptime S.doTheTest();
6363}
64
65test "implicit cast vector to array" {
66 const S = struct {
67 fn doTheTest() void {
68 var a: @Vector(4, i32) = [_]i32{ 1, 2, 3, 4 };
69 var result_array: [4]i32 = a;
70 result_array = a;
71 expect(mem.eql(i32, result_array, [4]i32{ 1, 2, 3, 4 }));
72 }
73 };
74 S.doTheTest();
75 comptime S.doTheTest();
76}
test/stage1/behavior/while.zig+45
......@@ -226,3 +226,48 @@ fn returnFalse() bool {
226226fn returnTrue() bool {
227227 return true;
228228}
229
230test "while bool 2 break statements and an else" {
231 const S = struct {
232 fn entry(t: bool, f: bool) void {
233 var ok = false;
234 ok = while (t) {
235 if (f) break false;
236 if (t) break true;
237 } else false;
238 expect(ok);
239 }
240 };
241 S.entry(true, false);
242 comptime S.entry(true, false);
243}
244
245test "while optional 2 break statements and an else" {
246 const S = struct {
247 fn entry(opt_t: ?bool, f: bool) void {
248 var ok = false;
249 ok = while (opt_t) |t| {
250 if (f) break false;
251 if (t) break true;
252 } else false;
253 expect(ok);
254 }
255 };
256 S.entry(true, false);
257 comptime S.entry(true, false);
258}
259
260test "while error 2 break statements and an else" {
261 const S = struct {
262 fn entry(opt_t: anyerror!bool, f: bool) void {
263 var ok = false;
264 ok = while (opt_t) |t| {
265 if (f) break false;
266 if (t) break true;
267 } else |_| false;
268 expect(ok);
269 }
270 };
271 S.entry(true, false);
272 comptime S.entry(true, false);
273}
test/tests.zig+13-15
......@@ -811,23 +811,21 @@ pub const CompileErrorContext = struct {
811811 pub fn addCase(self: *CompileErrorContext, case: *const TestCase) void {
812812 const b = self.b;
813813
814 for (self.modes) |mode| {
815 const annotated_case_name = fmt.allocPrint(self.b.allocator, "compile-error {} ({})", case.name, @tagName(mode)) catch unreachable;
816 if (self.test_filter) |filter| {
817 if (mem.indexOf(u8, annotated_case_name, filter) == null) continue;
818 }
814 const annotated_case_name = fmt.allocPrint(self.b.allocator, "compile-error {}", case.name) catch unreachable;
815 if (self.test_filter) |filter| {
816 if (mem.indexOf(u8, annotated_case_name, filter) == null) return;
817 }
819818
820 const compile_and_cmp_errors = CompileCmpOutputStep.create(self, annotated_case_name, case, mode);
821 self.step.dependOn(&compile_and_cmp_errors.step);
819 const compile_and_cmp_errors = CompileCmpOutputStep.create(self, annotated_case_name, case, .Debug);
820 self.step.dependOn(&compile_and_cmp_errors.step);
822821
823 for (case.sources.toSliceConst()) |src_file| {
824 const expanded_src_path = fs.path.join(
825 b.allocator,
826 [_][]const u8{ b.cache_root, src_file.filename },
827 ) catch unreachable;
828 const write_src = b.addWriteFile(expanded_src_path, src_file.source);
829 compile_and_cmp_errors.step.dependOn(&write_src.step);
830 }
822 for (case.sources.toSliceConst()) |src_file| {
823 const expanded_src_path = fs.path.join(
824 b.allocator,
825 [_][]const u8{ b.cache_root, src_file.filename },
826 ) catch unreachable;
827 const write_src = b.addWriteFile(expanded_src_path, src_file.source);
828 compile_and_cmp_errors.step.dependOn(&write_src.step);
831829 }
832830 }
833831};