| ... | ... | @@ -22,7 +22,7 @@ struct IrExecContext { |
| 22 | 22 | struct LoopStackItem { |
| 23 | 23 | IrBasicBlock *break_block; |
| 24 | 24 | IrBasicBlock *continue_block; |
| 25 | | bool is_inline; |
| 25 | IrInstruction *is_comptime; |
| 26 | 26 | }; |
| 27 | 27 | |
| 28 | 28 | struct IrBuilder { |
| ... | ... | @@ -443,6 +443,10 @@ static constexpr IrInstructionId ir_instruction_id(IrInstructionFnProto *) { |
| 443 | 443 | return IrInstructionIdFnProto; |
| 444 | 444 | } |
| 445 | 445 | |
| 446 | static constexpr IrInstructionId ir_instruction_id(IrInstructionTestComptime *) { |
| 447 | return IrInstructionIdTestComptime; |
| 448 | } |
| 449 | |
| 446 | 450 | template<typename T> |
| 447 | 451 | static T *ir_create_instruction(IrExecutable *exec, Scope *scope, AstNode *source_node) { |
| 448 | 452 | T *special_instruction = allocate<T>(1); |
| ... | ... | @@ -475,7 +479,7 @@ static IrInstruction *ir_build_cast(IrBuilder *irb, Scope *scope, AstNode *sourc |
| 475 | 479 | } |
| 476 | 480 | |
| 477 | 481 | static IrInstruction *ir_build_cond_br(IrBuilder *irb, Scope *scope, AstNode *source_node, IrInstruction *condition, |
| 478 | | IrBasicBlock *then_block, IrBasicBlock *else_block, bool is_inline) |
| 482 | IrBasicBlock *then_block, IrBasicBlock *else_block, IrInstruction *is_comptime) |
| 479 | 483 | { |
| 480 | 484 | IrInstructionCondBr *cond_br_instruction = ir_build_instruction<IrInstructionCondBr>(irb, scope, source_node); |
| 481 | 485 | cond_br_instruction->base.type_entry = irb->codegen->builtin_types.entry_unreachable; |
| ... | ... | @@ -483,20 +487,21 @@ static IrInstruction *ir_build_cond_br(IrBuilder *irb, Scope *scope, AstNode *so |
| 483 | 487 | cond_br_instruction->condition = condition; |
| 484 | 488 | cond_br_instruction->then_block = then_block; |
| 485 | 489 | cond_br_instruction->else_block = else_block; |
| 486 | | cond_br_instruction->is_inline = is_inline; |
| 490 | cond_br_instruction->is_comptime = is_comptime; |
| 487 | 491 | |
| 488 | 492 | ir_ref_instruction(condition); |
| 489 | 493 | ir_ref_bb(then_block); |
| 490 | 494 | ir_ref_bb(else_block); |
| 495 | if (is_comptime) ir_ref_instruction(is_comptime); |
| 491 | 496 | |
| 492 | 497 | return &cond_br_instruction->base; |
| 493 | 498 | } |
| 494 | 499 | |
| 495 | 500 | static IrInstruction *ir_build_cond_br_from(IrBuilder *irb, IrInstruction *old_instruction, |
| 496 | | IrInstruction *condition, IrBasicBlock *then_block, IrBasicBlock *else_block, bool is_inline) |
| 501 | IrInstruction *condition, IrBasicBlock *then_block, IrBasicBlock *else_block, IrInstruction *is_comptime) |
| 497 | 502 | { |
| 498 | 503 | IrInstruction *new_instruction = ir_build_cond_br(irb, old_instruction->scope, old_instruction->source_node, |
| 499 | | condition, then_block, else_block, is_inline); |
| 504 | condition, then_block, else_block, is_comptime); |
| 500 | 505 | ir_link_new_instruction(new_instruction, old_instruction); |
| 501 | 506 | return new_instruction; |
| 502 | 507 | } |
| ... | ... | @@ -859,30 +864,30 @@ static IrInstruction *ir_build_phi_from(IrBuilder *irb, IrInstruction *old_instr |
| 859 | 864 | } |
| 860 | 865 | |
| 861 | 866 | static IrInstruction *ir_create_br(IrBuilder *irb, Scope *scope, AstNode *source_node, |
| 862 | | IrBasicBlock *dest_block, bool is_inline) |
| 867 | IrBasicBlock *dest_block, IrInstruction *is_comptime) |
| 863 | 868 | { |
| 864 | 869 | IrInstructionBr *br_instruction = ir_create_instruction<IrInstructionBr>(irb->exec, scope, source_node); |
| 865 | 870 | br_instruction->base.type_entry = irb->codegen->builtin_types.entry_unreachable; |
| 866 | 871 | br_instruction->base.static_value.special = ConstValSpecialStatic; |
| 867 | 872 | br_instruction->dest_block = dest_block; |
| 868 | | br_instruction->is_inline = is_inline; |
| 873 | br_instruction->is_comptime = is_comptime; |
| 869 | 874 | |
| 870 | 875 | ir_ref_bb(dest_block); |
| 876 | if (is_comptime) ir_ref_instruction(is_comptime); |
| 871 | 877 | |
| 872 | 878 | return &br_instruction->base; |
| 873 | 879 | } |
| 874 | 880 | |
| 875 | 881 | static IrInstruction *ir_build_br(IrBuilder *irb, Scope *scope, AstNode *source_node, |
| 876 | | IrBasicBlock *dest_block, bool is_inline) |
| 882 | IrBasicBlock *dest_block, IrInstruction *is_comptime) |
| 877 | 883 | { |
| 878 | | IrInstruction *instruction = ir_create_br(irb, scope, source_node, dest_block, is_inline); |
| 884 | IrInstruction *instruction = ir_create_br(irb, scope, source_node, dest_block, is_comptime); |
| 879 | 885 | ir_instruction_append(irb->current_basic_block, instruction); |
| 880 | 886 | return instruction; |
| 881 | 887 | } |
| 882 | 888 | |
| 883 | 889 | static IrInstruction *ir_build_br_from(IrBuilder *irb, IrInstruction *old_instruction, IrBasicBlock *dest_block) { |
| 884 | | IrInstruction *new_instruction = ir_build_br(irb, old_instruction->scope, |
| 885 | | old_instruction->source_node, dest_block, false); |
| 890 | IrInstruction *new_instruction = ir_build_br(irb, old_instruction->scope, old_instruction->source_node, dest_block, nullptr); |
| 886 | 891 | ir_link_new_instruction(new_instruction, old_instruction); |
| 887 | 892 | return new_instruction; |
| 888 | 893 | } |
| ... | ... | @@ -1291,7 +1296,7 @@ static IrInstruction *ir_build_ctz_from(IrBuilder *irb, IrInstruction *old_instr |
| 1291 | 1296 | } |
| 1292 | 1297 | |
| 1293 | 1298 | static IrInstruction *ir_build_switch_br(IrBuilder *irb, Scope *scope, AstNode *source_node, IrInstruction *target_value, |
| 1294 | | IrBasicBlock *else_block, size_t case_count, IrInstructionSwitchBrCase *cases, bool is_inline) |
| 1299 | IrBasicBlock *else_block, size_t case_count, IrInstructionSwitchBrCase *cases, IrInstruction *is_comptime) |
| 1295 | 1300 | { |
| 1296 | 1301 | IrInstructionSwitchBr *instruction = ir_build_instruction<IrInstructionSwitchBr>(irb, scope, source_node); |
| 1297 | 1302 | instruction->base.type_entry = irb->codegen->builtin_types.entry_unreachable; |
| ... | ... | @@ -1300,9 +1305,10 @@ static IrInstruction *ir_build_switch_br(IrBuilder *irb, Scope *scope, AstNode * |
| 1300 | 1305 | instruction->else_block = else_block; |
| 1301 | 1306 | instruction->case_count = case_count; |
| 1302 | 1307 | instruction->cases = cases; |
| 1303 | | instruction->is_inline = is_inline; |
| 1308 | instruction->is_comptime = is_comptime; |
| 1304 | 1309 | |
| 1305 | 1310 | ir_ref_instruction(target_value); |
| 1311 | if (is_comptime) ir_ref_instruction(is_comptime); |
| 1306 | 1312 | ir_ref_bb(else_block); |
| 1307 | 1313 | |
| 1308 | 1314 | for (size_t i = 0; i < case_count; i += 1) { |
| ... | ... | @@ -1315,10 +1321,10 @@ static IrInstruction *ir_build_switch_br(IrBuilder *irb, Scope *scope, AstNode * |
| 1315 | 1321 | |
| 1316 | 1322 | static IrInstruction *ir_build_switch_br_from(IrBuilder *irb, IrInstruction *old_instruction, |
| 1317 | 1323 | IrInstruction *target_value, IrBasicBlock *else_block, size_t case_count, |
| 1318 | | IrInstructionSwitchBrCase *cases, bool is_inline) |
| 1324 | IrInstructionSwitchBrCase *cases, IrInstruction *is_comptime) |
| 1319 | 1325 | { |
| 1320 | 1326 | IrInstruction *new_instruction = ir_build_switch_br(irb, old_instruction->scope, old_instruction->source_node, |
| 1321 | | target_value, else_block, case_count, cases, is_inline); |
| 1327 | target_value, else_block, case_count, cases, is_comptime); |
| 1322 | 1328 | ir_link_new_instruction(new_instruction, old_instruction); |
| 1323 | 1329 | return new_instruction; |
| 1324 | 1330 | } |
| ... | ... | @@ -1846,6 +1852,15 @@ static IrInstruction *ir_build_fn_proto(IrBuilder *irb, Scope *scope, AstNode *s |
| 1846 | 1852 | return &instruction->base; |
| 1847 | 1853 | } |
| 1848 | 1854 | |
| 1855 | static IrInstruction *ir_build_test_comptime(IrBuilder *irb, Scope *scope, AstNode *source_node, IrInstruction *value) { |
| 1856 | IrInstructionTestComptime *instruction = ir_build_instruction<IrInstructionTestComptime>(irb, scope, source_node); |
| 1857 | instruction->value = value; |
| 1858 | |
| 1859 | ir_ref_instruction(value); |
| 1860 | |
| 1861 | return &instruction->base; |
| 1862 | } |
| 1863 | |
| 1849 | 1864 | static void ir_count_defers(IrBuilder *irb, Scope *inner_scope, Scope *outer_scope, size_t *results) { |
| 1850 | 1865 | results[ReturnKindUnconditional] = 0; |
| 1851 | 1866 | results[ReturnKindError] = 0; |
| ... | ... | @@ -1902,7 +1917,6 @@ static IrInstruction *ir_gen_return(IrBuilder *irb, Scope *scope, AstNode *node, |
| 1902 | 1917 | } |
| 1903 | 1918 | |
| 1904 | 1919 | Scope *outer_scope = fn_entry->child_scope; |
| 1905 | | bool is_inline = ir_should_inline(irb); |
| 1906 | 1920 | |
| 1907 | 1921 | AstNode *expr_node = node->data.return_expr.expr; |
| 1908 | 1922 | switch (node->data.return_expr.kind) { |
| ... | ... | @@ -1945,7 +1959,8 @@ static IrInstruction *ir_gen_return(IrBuilder *irb, Scope *scope, AstNode *node, |
| 1945 | 1959 | |
| 1946 | 1960 | IrBasicBlock *return_block = ir_build_basic_block(irb, scope, "ErrRetReturn"); |
| 1947 | 1961 | IrBasicBlock *continue_block = ir_build_basic_block(irb, scope, "ErrRetContinue"); |
| 1948 | | ir_build_cond_br(irb, scope, node, is_err_val, return_block, continue_block, is_inline); |
| 1962 | IrInstruction *is_comptime = ir_build_const_bool(irb, scope, node, ir_should_inline(irb)); |
| 1963 | ir_build_cond_br(irb, scope, node, is_err_val, return_block, continue_block, is_comptime); |
| 1949 | 1964 | |
| 1950 | 1965 | ir_set_cursor_at_end(irb, return_block); |
| 1951 | 1966 | ir_gen_defers_for_block(irb, scope, outer_scope, true, false); |
| ... | ... | @@ -1969,7 +1984,8 @@ static IrInstruction *ir_gen_return(IrBuilder *irb, Scope *scope, AstNode *node, |
| 1969 | 1984 | |
| 1970 | 1985 | IrBasicBlock *return_block = ir_build_basic_block(irb, scope, "MaybeRetReturn"); |
| 1971 | 1986 | IrBasicBlock *continue_block = ir_build_basic_block(irb, scope, "MaybeRetContinue"); |
| 1972 | | ir_build_cond_br(irb, scope, node, is_nonnull_val, continue_block, return_block, is_inline); |
| 1987 | IrInstruction *is_comptime = ir_build_const_bool(irb, scope, node, ir_should_inline(irb)); |
| 1988 | ir_build_cond_br(irb, scope, node, is_nonnull_val, continue_block, return_block, is_comptime); |
| 1973 | 1989 | |
| 1974 | 1990 | ir_set_cursor_at_end(irb, return_block); |
| 1975 | 1991 | ir_gen_defers_for_block(irb, scope, outer_scope, false, true); |
| ... | ... | @@ -1988,13 +2004,13 @@ static IrInstruction *ir_gen_return(IrBuilder *irb, Scope *scope, AstNode *node, |
| 1988 | 2004 | } |
| 1989 | 2005 | |
| 1990 | 2006 | static VariableTableEntry *create_local_var(CodeGen *codegen, AstNode *node, Scope *parent_scope, |
| 1991 | | Buf *name, bool src_is_const, bool gen_is_const, bool is_shadowable, bool is_inline) |
| 2007 | Buf *name, bool src_is_const, bool gen_is_const, bool is_shadowable, IrInstruction *is_comptime) |
| 1992 | 2008 | { |
| 1993 | 2009 | VariableTableEntry *variable_entry = allocate<VariableTableEntry>(1); |
| 1994 | 2010 | variable_entry->parent_scope = parent_scope; |
| 1995 | 2011 | variable_entry->shadowable = is_shadowable; |
| 1996 | 2012 | variable_entry->mem_slot_index = SIZE_MAX; |
| 1997 | | variable_entry->is_inline = is_inline; |
| 2013 | variable_entry->is_comptime = is_comptime; |
| 1998 | 2014 | variable_entry->src_arg_index = SIZE_MAX; |
| 1999 | 2015 | |
| 2000 | 2016 | if (name) { |
| ... | ... | @@ -2043,11 +2059,10 @@ static VariableTableEntry *create_local_var(CodeGen *codegen, AstNode *node, Sco |
| 2043 | 2059 | // Set name to nullptr to make the variable anonymous (not visible to programmer). |
| 2044 | 2060 | // After you call this function var->child_scope has the variable in scope |
| 2045 | 2061 | static VariableTableEntry *ir_create_var(IrBuilder *irb, AstNode *node, Scope *scope, Buf *name, |
| 2046 | | bool src_is_const, bool gen_is_const, bool is_shadowable, bool is_inline) |
| 2062 | bool src_is_const, bool gen_is_const, bool is_shadowable, IrInstruction *is_comptime) |
| 2047 | 2063 | { |
| 2048 | | VariableTableEntry *var = create_local_var(irb->codegen, node, scope, name, |
| 2049 | | src_is_const, gen_is_const, is_shadowable, is_inline); |
| 2050 | | if (is_inline || gen_is_const) |
| 2064 | VariableTableEntry *var = create_local_var(irb->codegen, node, scope, name, src_is_const, gen_is_const, is_shadowable, is_comptime); |
| 2065 | if (is_comptime != nullptr || gen_is_const) |
| 2051 | 2066 | var->mem_slot_index = exec_next_mem_slot(irb->exec); |
| 2052 | 2067 | assert(var->child_scope); |
| 2053 | 2068 | return var; |
| ... | ... | @@ -2123,19 +2138,24 @@ static IrInstruction *ir_gen_assign_op(IrBuilder *irb, Scope *scope, AstNode *no |
| 2123 | 2138 | static IrInstruction *ir_gen_bool_or(IrBuilder *irb, Scope *scope, AstNode *node) { |
| 2124 | 2139 | assert(node->type == NodeTypeBinOpExpr); |
| 2125 | 2140 | |
| 2126 | | bool is_inline = ir_should_inline(irb); |
| 2127 | | |
| 2128 | 2141 | IrInstruction *val1 = ir_gen_node(irb, node->data.bin_op_expr.op1, scope); |
| 2129 | 2142 | if (val1 == irb->codegen->invalid_instruction) |
| 2130 | 2143 | return irb->codegen->invalid_instruction; |
| 2131 | 2144 | IrBasicBlock *post_val1_block = irb->current_basic_block; |
| 2132 | 2145 | |
| 2146 | IrInstruction *is_comptime; |
| 2147 | if (ir_should_inline(irb)) { |
| 2148 | is_comptime = ir_build_const_bool(irb, scope, node, true); |
| 2149 | } else { |
| 2150 | is_comptime = ir_build_test_comptime(irb, scope, node, val1); |
| 2151 | } |
| 2152 | |
| 2133 | 2153 | // block for when val1 == false |
| 2134 | 2154 | IrBasicBlock *false_block = ir_build_basic_block(irb, scope, "BoolOrFalse"); |
| 2135 | 2155 | // block for when val1 == true (don't even evaluate the second part) |
| 2136 | 2156 | IrBasicBlock *true_block = ir_build_basic_block(irb, scope, "BoolOrTrue"); |
| 2137 | 2157 | |
| 2138 | | ir_build_cond_br(irb, scope, node, val1, true_block, false_block, is_inline); |
| 2158 | ir_build_cond_br(irb, scope, node, val1, true_block, false_block, is_comptime); |
| 2139 | 2159 | |
| 2140 | 2160 | ir_set_cursor_at_end(irb, false_block); |
| 2141 | 2161 | IrInstruction *val2 = ir_gen_node(irb, node->data.bin_op_expr.op2, scope); |
| ... | ... | @@ -2143,7 +2163,7 @@ static IrInstruction *ir_gen_bool_or(IrBuilder *irb, Scope *scope, AstNode *node |
| 2143 | 2163 | return irb->codegen->invalid_instruction; |
| 2144 | 2164 | IrBasicBlock *post_val2_block = irb->current_basic_block; |
| 2145 | 2165 | |
| 2146 | | ir_build_br(irb, scope, node, true_block, is_inline); |
| 2166 | ir_build_br(irb, scope, node, true_block, is_comptime); |
| 2147 | 2167 | |
| 2148 | 2168 | ir_set_cursor_at_end(irb, true_block); |
| 2149 | 2169 | |
| ... | ... | @@ -2160,19 +2180,24 @@ static IrInstruction *ir_gen_bool_or(IrBuilder *irb, Scope *scope, AstNode *node |
| 2160 | 2180 | static IrInstruction *ir_gen_bool_and(IrBuilder *irb, Scope *scope, AstNode *node) { |
| 2161 | 2181 | assert(node->type == NodeTypeBinOpExpr); |
| 2162 | 2182 | |
| 2163 | | bool is_inline = ir_should_inline(irb); |
| 2164 | | |
| 2165 | 2183 | IrInstruction *val1 = ir_gen_node(irb, node->data.bin_op_expr.op1, scope); |
| 2166 | 2184 | if (val1 == irb->codegen->invalid_instruction) |
| 2167 | 2185 | return irb->codegen->invalid_instruction; |
| 2168 | 2186 | IrBasicBlock *post_val1_block = irb->current_basic_block; |
| 2169 | 2187 | |
| 2188 | IrInstruction *is_comptime; |
| 2189 | if (ir_should_inline(irb)) { |
| 2190 | is_comptime = ir_build_const_bool(irb, scope, node, true); |
| 2191 | } else { |
| 2192 | is_comptime = ir_build_test_comptime(irb, scope, node, val1); |
| 2193 | } |
| 2194 | |
| 2170 | 2195 | // block for when val1 == true |
| 2171 | 2196 | IrBasicBlock *true_block = ir_build_basic_block(irb, scope, "BoolAndTrue"); |
| 2172 | 2197 | // block for when val1 == false (don't even evaluate the second part) |
| 2173 | 2198 | IrBasicBlock *false_block = ir_build_basic_block(irb, scope, "BoolAndFalse"); |
| 2174 | 2199 | |
| 2175 | | ir_build_cond_br(irb, scope, node, val1, true_block, false_block, is_inline); |
| 2200 | ir_build_cond_br(irb, scope, node, val1, true_block, false_block, is_comptime); |
| 2176 | 2201 | |
| 2177 | 2202 | ir_set_cursor_at_end(irb, true_block); |
| 2178 | 2203 | IrInstruction *val2 = ir_gen_node(irb, node->data.bin_op_expr.op2, scope); |
| ... | ... | @@ -2180,7 +2205,7 @@ static IrInstruction *ir_gen_bool_and(IrBuilder *irb, Scope *scope, AstNode *nod |
| 2180 | 2205 | return irb->codegen->invalid_instruction; |
| 2181 | 2206 | IrBasicBlock *post_val2_block = irb->current_basic_block; |
| 2182 | 2207 | |
| 2183 | | ir_build_br(irb, scope, node, false_block, is_inline); |
| 2208 | ir_build_br(irb, scope, node, false_block, is_comptime); |
| 2184 | 2209 | |
| 2185 | 2210 | ir_set_cursor_at_end(irb, false_block); |
| 2186 | 2211 | |
| ... | ... | @@ -2200,31 +2225,36 @@ static IrInstruction *ir_gen_maybe_ok_or(IrBuilder *irb, Scope *parent_scope, As |
| 2200 | 2225 | AstNode *op1_node = node->data.bin_op_expr.op1; |
| 2201 | 2226 | AstNode *op2_node = node->data.bin_op_expr.op2; |
| 2202 | 2227 | |
| 2203 | | bool is_inline = ir_should_inline(irb); |
| 2204 | | |
| 2205 | 2228 | IrInstruction *maybe_ptr = ir_gen_node_extra(irb, op1_node, parent_scope, LValPurposeAddressOf); |
| 2206 | 2229 | if (maybe_ptr == irb->codegen->invalid_instruction) |
| 2207 | 2230 | return irb->codegen->invalid_instruction; |
| 2208 | 2231 | |
| 2209 | 2232 | IrInstruction *is_non_null = ir_build_test_null(irb, parent_scope, node, maybe_ptr); |
| 2210 | 2233 | |
| 2234 | IrInstruction *is_comptime; |
| 2235 | if (ir_should_inline(irb)) { |
| 2236 | is_comptime = ir_build_const_bool(irb, parent_scope, node, true); |
| 2237 | } else { |
| 2238 | is_comptime = ir_build_test_comptime(irb, parent_scope, node, is_non_null); |
| 2239 | } |
| 2240 | |
| 2211 | 2241 | IrBasicBlock *ok_block = ir_build_basic_block(irb, parent_scope, "MaybeNonNull"); |
| 2212 | 2242 | IrBasicBlock *null_block = ir_build_basic_block(irb, parent_scope, "MaybeNull"); |
| 2213 | 2243 | IrBasicBlock *end_block = ir_build_basic_block(irb, parent_scope, "MaybeEnd"); |
| 2214 | | ir_build_cond_br(irb, parent_scope, node, is_non_null, ok_block, null_block, is_inline); |
| 2244 | ir_build_cond_br(irb, parent_scope, node, is_non_null, ok_block, null_block, is_comptime); |
| 2215 | 2245 | |
| 2216 | 2246 | ir_set_cursor_at_end(irb, null_block); |
| 2217 | 2247 | IrInstruction *null_result = ir_gen_node(irb, op2_node, parent_scope); |
| 2218 | 2248 | if (null_result == irb->codegen->invalid_instruction) |
| 2219 | 2249 | return irb->codegen->invalid_instruction; |
| 2220 | 2250 | IrBasicBlock *after_null_block = irb->current_basic_block; |
| 2221 | | ir_build_br(irb, parent_scope, node, end_block, is_inline); |
| 2251 | ir_build_br(irb, parent_scope, node, end_block, is_comptime); |
| 2222 | 2252 | |
| 2223 | 2253 | ir_set_cursor_at_end(irb, ok_block); |
| 2224 | 2254 | IrInstruction *unwrapped_ptr = ir_build_unwrap_maybe(irb, parent_scope, node, maybe_ptr, false); |
| 2225 | 2255 | IrInstruction *unwrapped_payload = ir_build_load_ptr(irb, parent_scope, node, unwrapped_ptr); |
| 2226 | 2256 | IrBasicBlock *after_ok_block = irb->current_basic_block; |
| 2227 | | ir_build_br(irb, parent_scope, node, end_block, is_inline); |
| 2257 | ir_build_br(irb, parent_scope, node, end_block, is_comptime); |
| 2228 | 2258 | |
| 2229 | 2259 | ir_set_cursor_at_end(irb, end_block); |
| 2230 | 2260 | IrInstruction **incoming_values = allocate<IrInstruction *>(2); |
| ... | ... | @@ -2944,6 +2974,13 @@ static IrInstruction *ir_gen_if_bool_expr(IrBuilder *irb, Scope *scope, AstNode |
| 2944 | 2974 | if (condition == irb->codegen->invalid_instruction) |
| 2945 | 2975 | return condition; |
| 2946 | 2976 | |
| 2977 | IrInstruction *is_comptime; |
| 2978 | if (ir_should_inline(irb) || node->data.if_bool_expr.is_inline) { |
| 2979 | is_comptime = ir_build_const_bool(irb, scope, node, true); |
| 2980 | } else { |
| 2981 | is_comptime = ir_build_test_comptime(irb, scope, node, condition); |
| 2982 | } |
| 2983 | |
| 2947 | 2984 | AstNode *then_node = node->data.if_bool_expr.then_block; |
| 2948 | 2985 | AstNode *else_node = node->data.if_bool_expr.else_node; |
| 2949 | 2986 | |
| ... | ... | @@ -2951,15 +2988,14 @@ static IrInstruction *ir_gen_if_bool_expr(IrBuilder *irb, Scope *scope, AstNode |
| 2951 | 2988 | IrBasicBlock *else_block = ir_build_basic_block(irb, scope, "Else"); |
| 2952 | 2989 | IrBasicBlock *endif_block = ir_build_basic_block(irb, scope, "EndIf"); |
| 2953 | 2990 | |
| 2954 | | bool is_inline = ir_should_inline(irb) || node->data.if_bool_expr.is_inline; |
| 2955 | | ir_build_cond_br(irb, scope, condition->source_node, condition, then_block, else_block, is_inline); |
| 2991 | ir_build_cond_br(irb, scope, condition->source_node, condition, then_block, else_block, is_comptime); |
| 2956 | 2992 | |
| 2957 | 2993 | ir_set_cursor_at_end(irb, then_block); |
| 2958 | 2994 | IrInstruction *then_expr_result = ir_gen_node(irb, then_node, scope); |
| 2959 | 2995 | if (then_expr_result == irb->codegen->invalid_instruction) |
| 2960 | 2996 | return then_expr_result; |
| 2961 | 2997 | IrBasicBlock *after_then_block = irb->current_basic_block; |
| 2962 | | ir_build_br(irb, scope, node, endif_block, is_inline); |
| 2998 | ir_build_br(irb, scope, node, endif_block, is_comptime); |
| 2963 | 2999 | |
| 2964 | 3000 | ir_set_cursor_at_end(irb, else_block); |
| 2965 | 3001 | IrInstruction *else_expr_result; |
| ... | ... | @@ -2971,7 +3007,7 @@ static IrInstruction *ir_gen_if_bool_expr(IrBuilder *irb, Scope *scope, AstNode |
| 2971 | 3007 | else_expr_result = ir_build_const_void(irb, scope, node); |
| 2972 | 3008 | } |
| 2973 | 3009 | IrBasicBlock *after_else_block = irb->current_basic_block; |
| 2974 | | ir_build_br(irb, scope, node, endif_block, is_inline); |
| 3010 | ir_build_br(irb, scope, node, endif_block, is_comptime); |
| 2975 | 3011 | |
| 2976 | 3012 | ir_set_cursor_at_end(irb, endif_block); |
| 2977 | 3013 | IrInstruction **incoming_values = allocate<IrInstruction *>(2); |
| ... | ... | @@ -3162,9 +3198,10 @@ static IrInstruction *ir_gen_var_decl(IrBuilder *irb, Scope *scope, AstNode *nod |
| 3162 | 3198 | bool is_shadowable = false; |
| 3163 | 3199 | bool is_const = variable_declaration->is_const; |
| 3164 | 3200 | bool is_extern = variable_declaration->is_extern; |
| 3165 | | bool is_inline = ir_should_inline(irb) || variable_declaration->is_inline; |
| 3166 | | VariableTableEntry *var = ir_create_var(irb, node, scope, |
| 3167 | | variable_declaration->symbol, is_const, is_const, is_shadowable, is_inline); |
| 3201 | IrInstruction *is_comptime = ir_build_const_bool(irb, scope, node, |
| 3202 | ir_should_inline(irb) || variable_declaration->is_inline); |
| 3203 | VariableTableEntry *var = ir_create_var(irb, node, scope, variable_declaration->symbol, |
| 3204 | is_const, is_const, is_shadowable, is_comptime); |
| 3168 | 3205 | // we detect IrInstructionIdDeclVar in gen_block to make sure the next node |
| 3169 | 3206 | // is inside var->child_scope |
| 3170 | 3207 | |
| ... | ... | @@ -3192,29 +3229,30 @@ static IrInstruction *ir_gen_while_expr(IrBuilder *irb, Scope *scope, AstNode *n |
| 3192 | 3229 | ir_build_basic_block(irb, scope, "WhileContinue") : cond_block; |
| 3193 | 3230 | IrBasicBlock *end_block = ir_build_basic_block(irb, scope, "WhileEnd"); |
| 3194 | 3231 | |
| 3195 | | bool is_inline = ir_should_inline(irb) || node->data.while_expr.is_inline; |
| 3196 | | ir_build_br(irb, scope, node, cond_block, is_inline); |
| 3232 | IrInstruction *is_comptime = ir_build_const_bool(irb, scope, node, |
| 3233 | ir_should_inline(irb) || node->data.while_expr.is_inline); |
| 3234 | ir_build_br(irb, scope, node, cond_block, is_comptime); |
| 3197 | 3235 | |
| 3198 | 3236 | if (continue_expr_node) { |
| 3199 | 3237 | ir_set_cursor_at_end(irb, continue_block); |
| 3200 | 3238 | ir_gen_node(irb, continue_expr_node, scope); |
| 3201 | | ir_build_br(irb, scope, node, cond_block, is_inline); |
| 3239 | ir_build_br(irb, scope, node, cond_block, is_comptime); |
| 3202 | 3240 | } |
| 3203 | 3241 | |
| 3204 | 3242 | ir_set_cursor_at_end(irb, cond_block); |
| 3205 | 3243 | IrInstruction *cond_val = ir_gen_node(irb, node->data.while_expr.condition, scope); |
| 3206 | | ir_build_cond_br(irb, scope, node->data.while_expr.condition, cond_val, body_block, end_block, is_inline); |
| 3244 | ir_build_cond_br(irb, scope, node->data.while_expr.condition, cond_val, body_block, end_block, is_comptime); |
| 3207 | 3245 | |
| 3208 | 3246 | ir_set_cursor_at_end(irb, body_block); |
| 3209 | 3247 | |
| 3210 | 3248 | LoopStackItem *loop_stack_item = irb->loop_stack.add_one(); |
| 3211 | 3249 | loop_stack_item->break_block = end_block; |
| 3212 | 3250 | loop_stack_item->continue_block = continue_block; |
| 3213 | | loop_stack_item->is_inline = is_inline; |
| 3251 | loop_stack_item->is_comptime = is_comptime; |
| 3214 | 3252 | ir_gen_node(irb, node->data.while_expr.body, scope); |
| 3215 | 3253 | irb->loop_stack.pop(); |
| 3216 | 3254 | |
| 3217 | | ir_build_br(irb, scope, node, continue_block, is_inline); |
| 3255 | ir_build_br(irb, scope, node, continue_block, is_comptime); |
| 3218 | 3256 | ir_set_cursor_at_end(irb, end_block); |
| 3219 | 3257 | |
| 3220 | 3258 | return ir_build_const_void(irb, scope, node); |
| ... | ... | @@ -3248,14 +3286,15 @@ static IrInstruction *ir_gen_for_expr(IrBuilder *irb, Scope *parent_scope, AstNo |
| 3248 | 3286 | } else { |
| 3249 | 3287 | elem_var_type = ir_build_ptr_type_child(irb, parent_scope, elem_node, pointer_type); |
| 3250 | 3288 | } |
| 3251 | | bool is_inline = ir_should_inline(irb) || node->data.for_expr.is_inline; |
| 3289 | |
| 3290 | IrInstruction *is_comptime = ir_build_const_bool(irb, parent_scope, node, |
| 3291 | ir_should_inline(irb) || node->data.for_expr.is_inline); |
| 3252 | 3292 | |
| 3253 | 3293 | Scope *child_scope = create_loop_scope(node, parent_scope); |
| 3254 | 3294 | |
| 3255 | 3295 | // TODO make it an error to write to element variable or i variable. |
| 3256 | 3296 | Buf *elem_var_name = elem_node->data.symbol_expr.symbol; |
| 3257 | | VariableTableEntry *elem_var = ir_create_var(irb, elem_node, child_scope, elem_var_name, |
| 3258 | | true, false, false, is_inline); |
| 3297 | VariableTableEntry *elem_var = ir_create_var(irb, elem_node, child_scope, elem_var_name, true, false, false, is_comptime); |
| 3259 | 3298 | child_scope = elem_var->child_scope; |
| 3260 | 3299 | |
| 3261 | 3300 | IrInstruction *undefined_value = ir_build_const_undefined(irb, child_scope, elem_node); |
| ... | ... | @@ -3267,10 +3306,10 @@ static IrInstruction *ir_gen_for_expr(IrBuilder *irb, Scope *parent_scope, AstNo |
| 3267 | 3306 | if (index_node) { |
| 3268 | 3307 | index_var_source_node = index_node; |
| 3269 | 3308 | Buf *index_var_name = index_node->data.symbol_expr.symbol; |
| 3270 | | index_var = ir_create_var(irb, index_node, child_scope, index_var_name, true, false, false, is_inline); |
| 3309 | index_var = ir_create_var(irb, index_node, child_scope, index_var_name, true, false, false, is_comptime); |
| 3271 | 3310 | } else { |
| 3272 | 3311 | index_var_source_node = node; |
| 3273 | | index_var = ir_create_var(irb, node, child_scope, nullptr, true, false, true, is_inline); |
| 3312 | index_var = ir_create_var(irb, node, child_scope, nullptr, true, false, true, is_comptime); |
| 3274 | 3313 | } |
| 3275 | 3314 | child_scope = index_var->child_scope; |
| 3276 | 3315 | |
| ... | ... | @@ -3287,12 +3326,12 @@ static IrInstruction *ir_gen_for_expr(IrBuilder *irb, Scope *parent_scope, AstNo |
| 3287 | 3326 | IrBasicBlock *continue_block = ir_build_basic_block(irb, child_scope, "ForContinue"); |
| 3288 | 3327 | |
| 3289 | 3328 | IrInstruction *len_val = ir_build_array_len(irb, child_scope, node, array_val); |
| 3290 | | ir_build_br(irb, child_scope, node, cond_block, is_inline); |
| 3329 | ir_build_br(irb, child_scope, node, cond_block, is_comptime); |
| 3291 | 3330 | |
| 3292 | 3331 | ir_set_cursor_at_end(irb, cond_block); |
| 3293 | 3332 | IrInstruction *index_val = ir_build_load_ptr(irb, child_scope, node, index_ptr); |
| 3294 | 3333 | IrInstruction *cond = ir_build_bin_op(irb, child_scope, node, IrBinOpCmpLessThan, index_val, len_val, false); |
| 3295 | | ir_build_cond_br(irb, child_scope, node, cond, body_block, end_block, is_inline); |
| 3334 | ir_build_cond_br(irb, child_scope, node, cond, body_block, end_block, is_comptime); |
| 3296 | 3335 | |
| 3297 | 3336 | ir_set_cursor_at_end(irb, body_block); |
| 3298 | 3337 | IrInstruction *elem_ptr = ir_build_elem_ptr(irb, child_scope, node, array_val_ptr, index_val, false); |
| ... | ... | @@ -3307,16 +3346,16 @@ static IrInstruction *ir_gen_for_expr(IrBuilder *irb, Scope *parent_scope, AstNo |
| 3307 | 3346 | LoopStackItem *loop_stack_item = irb->loop_stack.add_one(); |
| 3308 | 3347 | loop_stack_item->break_block = end_block; |
| 3309 | 3348 | loop_stack_item->continue_block = continue_block; |
| 3310 | | loop_stack_item->is_inline = is_inline; |
| 3349 | loop_stack_item->is_comptime = is_comptime; |
| 3311 | 3350 | ir_gen_node(irb, body_node, child_scope); |
| 3312 | 3351 | irb->loop_stack.pop(); |
| 3313 | 3352 | |
| 3314 | | ir_build_br(irb, child_scope, node, continue_block, is_inline); |
| 3353 | ir_build_br(irb, child_scope, node, continue_block, is_comptime); |
| 3315 | 3354 | |
| 3316 | 3355 | ir_set_cursor_at_end(irb, continue_block); |
| 3317 | 3356 | IrInstruction *new_index_val = ir_build_bin_op(irb, child_scope, node, IrBinOpAdd, index_val, one, false); |
| 3318 | 3357 | ir_build_store_ptr(irb, child_scope, node, index_ptr, new_index_val); |
| 3319 | | ir_build_br(irb, child_scope, node, cond_block, is_inline); |
| 3358 | ir_build_br(irb, child_scope, node, cond_block, is_comptime); |
| 3320 | 3359 | |
| 3321 | 3360 | ir_set_cursor_at_end(irb, end_block); |
| 3322 | 3361 | return ir_build_const_void(irb, child_scope, node); |
| ... | ... | @@ -3467,8 +3506,13 @@ static IrInstruction *ir_gen_if_var_expr(IrBuilder *irb, Scope *scope, AstNode * |
| 3467 | 3506 | IrBasicBlock *else_block = ir_build_basic_block(irb, scope, "MaybeElse"); |
| 3468 | 3507 | IrBasicBlock *endif_block = ir_build_basic_block(irb, scope, "MaybeEndIf"); |
| 3469 | 3508 | |
| 3470 | | bool is_inline = ir_should_inline(irb) || node->data.if_var_expr.is_inline; |
| 3471 | | ir_build_cond_br(irb, scope, node, is_nonnull_value, then_block, else_block, is_inline); |
| 3509 | IrInstruction *is_comptime; |
| 3510 | if (ir_should_inline(irb) || node->data.if_var_expr.is_inline) { |
| 3511 | is_comptime = ir_build_const_bool(irb, scope, node, true); |
| 3512 | } else { |
| 3513 | is_comptime = ir_build_test_comptime(irb, scope, node, is_nonnull_value); |
| 3514 | } |
| 3515 | ir_build_cond_br(irb, scope, node, is_nonnull_value, then_block, else_block, is_comptime); |
| 3472 | 3516 | |
| 3473 | 3517 | ir_set_cursor_at_end(irb, then_block); |
| 3474 | 3518 | IrInstruction *var_type = nullptr; |
| ... | ... | @@ -3480,7 +3524,7 @@ static IrInstruction *ir_gen_if_var_expr(IrBuilder *irb, Scope *scope, AstNode * |
| 3480 | 3524 | bool is_shadowable = false; |
| 3481 | 3525 | bool is_const = var_decl->is_const; |
| 3482 | 3526 | VariableTableEntry *var = ir_create_var(irb, node, scope, |
| 3483 | | var_decl->symbol, is_const, is_const, is_shadowable, is_inline); |
| 3527 | var_decl->symbol, is_const, is_const, is_shadowable, is_comptime); |
| 3484 | 3528 | |
| 3485 | 3529 | IrInstruction *var_ptr_value = ir_build_unwrap_maybe(irb, scope, node, expr_value, false); |
| 3486 | 3530 | IrInstruction *var_value = var_is_ptr ? var_ptr_value : ir_build_load_ptr(irb, scope, node, var_ptr_value); |
| ... | ... | @@ -3489,7 +3533,7 @@ static IrInstruction *ir_gen_if_var_expr(IrBuilder *irb, Scope *scope, AstNode * |
| 3489 | 3533 | if (then_expr_result == irb->codegen->invalid_instruction) |
| 3490 | 3534 | return then_expr_result; |
| 3491 | 3535 | IrBasicBlock *after_then_block = irb->current_basic_block; |
| 3492 | | ir_build_br(irb, scope, node, endif_block, is_inline); |
| 3536 | ir_build_br(irb, scope, node, endif_block, is_comptime); |
| 3493 | 3537 | |
| 3494 | 3538 | ir_set_cursor_at_end(irb, else_block); |
| 3495 | 3539 | IrInstruction *else_expr_result; |
| ... | ... | @@ -3501,7 +3545,7 @@ static IrInstruction *ir_gen_if_var_expr(IrBuilder *irb, Scope *scope, AstNode * |
| 3501 | 3545 | else_expr_result = ir_build_const_void(irb, scope, node); |
| 3502 | 3546 | } |
| 3503 | 3547 | IrBasicBlock *after_else_block = irb->current_basic_block; |
| 3504 | | ir_build_br(irb, scope, node, endif_block, is_inline); |
| 3548 | ir_build_br(irb, scope, node, endif_block, is_comptime); |
| 3505 | 3549 | |
| 3506 | 3550 | ir_set_cursor_at_end(irb, endif_block); |
| 3507 | 3551 | IrInstruction **incoming_values = allocate<IrInstruction *>(2); |
| ... | ... | @@ -3515,7 +3559,7 @@ static IrInstruction *ir_gen_if_var_expr(IrBuilder *irb, Scope *scope, AstNode * |
| 3515 | 3559 | } |
| 3516 | 3560 | |
| 3517 | 3561 | static bool ir_gen_switch_prong_expr(IrBuilder *irb, Scope *scope, AstNode *switch_node, AstNode *prong_node, |
| 3518 | | IrBasicBlock *end_block, bool is_inline, IrInstruction *target_value_ptr, IrInstruction *prong_value, |
| 3562 | IrBasicBlock *end_block, IrInstruction *is_comptime, IrInstruction *target_value_ptr, IrInstruction *prong_value, |
| 3519 | 3563 | ZigList<IrBasicBlock *> *incoming_blocks, ZigList<IrInstruction *> *incoming_values) |
| 3520 | 3564 | { |
| 3521 | 3565 | assert(switch_node->type == NodeTypeSwitchExpr); |
| ... | ... | @@ -3532,7 +3576,7 @@ static bool ir_gen_switch_prong_expr(IrBuilder *irb, Scope *scope, AstNode *swit |
| 3532 | 3576 | bool is_shadowable = false; |
| 3533 | 3577 | bool is_const = true; |
| 3534 | 3578 | VariableTableEntry *var = ir_create_var(irb, var_symbol_node, scope, |
| 3535 | | var_name, is_const, is_const, is_shadowable, is_inline); |
| 3579 | var_name, is_const, is_const, is_shadowable, is_comptime); |
| 3536 | 3580 | child_scope = var->child_scope; |
| 3537 | 3581 | IrInstruction *var_value; |
| 3538 | 3582 | if (prong_value) { |
| ... | ... | @@ -3550,7 +3594,7 @@ static bool ir_gen_switch_prong_expr(IrBuilder *irb, Scope *scope, AstNode *swit |
| 3550 | 3594 | IrInstruction *expr_result = ir_gen_node(irb, expr_node, child_scope); |
| 3551 | 3595 | if (expr_result == irb->codegen->invalid_instruction) |
| 3552 | 3596 | return false; |
| 3553 | | ir_build_br(irb, scope, switch_node, end_block, is_inline); |
| 3597 | ir_build_br(irb, scope, switch_node, end_block, is_comptime); |
| 3554 | 3598 | incoming_blocks->append(irb->current_basic_block); |
| 3555 | 3599 | incoming_values->append(expr_result); |
| 3556 | 3600 | return true; |
| ... | ... | @@ -3570,7 +3614,13 @@ static IrInstruction *ir_gen_switch_expr(IrBuilder *irb, Scope *scope, AstNode * |
| 3570 | 3614 | |
| 3571 | 3615 | size_t prong_count = node->data.switch_expr.prongs.length; |
| 3572 | 3616 | ZigList<IrInstructionSwitchBrCase> cases = {0}; |
| 3573 | | bool is_inline = ir_should_inline(irb) || node->data.switch_expr.is_inline; |
| 3617 | |
| 3618 | IrInstruction *is_comptime; |
| 3619 | if (ir_should_inline(irb) || node->data.switch_expr.is_inline) { |
| 3620 | is_comptime = ir_build_const_bool(irb, scope, node, true); |
| 3621 | } else { |
| 3622 | is_comptime = ir_build_test_comptime(irb, scope, node, target_value); |
| 3623 | } |
| 3574 | 3624 | |
| 3575 | 3625 | ZigList<IrInstruction *> incoming_values = {0}; |
| 3576 | 3626 | ZigList<IrBasicBlock *> incoming_blocks = {0}; |
| ... | ... | @@ -3592,7 +3642,7 @@ static IrInstruction *ir_gen_switch_expr(IrBuilder *irb, Scope *scope, AstNode * |
| 3592 | 3642 | IrBasicBlock *prev_block = irb->current_basic_block; |
| 3593 | 3643 | ir_set_cursor_at_end(irb, else_block); |
| 3594 | 3644 | if (!ir_gen_switch_prong_expr(irb, scope, node, prong_node, end_block, |
| 3595 | | is_inline, target_value_ptr, nullptr, &incoming_blocks, &incoming_values)) |
| 3645 | is_comptime, target_value_ptr, nullptr, &incoming_blocks, &incoming_values)) |
| 3596 | 3646 | { |
| 3597 | 3647 | return irb->codegen->invalid_instruction; |
| 3598 | 3648 | } |
| ... | ... | @@ -3650,11 +3700,11 @@ static IrInstruction *ir_gen_switch_expr(IrBuilder *irb, Scope *scope, AstNode * |
| 3650 | 3700 | |
| 3651 | 3701 | assert(ok_bit); |
| 3652 | 3702 | assert(last_item_node); |
| 3653 | | ir_build_cond_br(irb, scope, last_item_node, ok_bit, range_block_yes, range_block_no, is_inline); |
| 3703 | ir_build_cond_br(irb, scope, last_item_node, ok_bit, range_block_yes, range_block_no, is_comptime); |
| 3654 | 3704 | |
| 3655 | 3705 | ir_set_cursor_at_end(irb, range_block_yes); |
| 3656 | 3706 | if (!ir_gen_switch_prong_expr(irb, scope, node, prong_node, end_block, |
| 3657 | | is_inline, target_value_ptr, nullptr, &incoming_blocks, &incoming_values)) |
| 3707 | is_comptime, target_value_ptr, nullptr, &incoming_blocks, &incoming_values)) |
| 3658 | 3708 | { |
| 3659 | 3709 | return irb->codegen->invalid_instruction; |
| 3660 | 3710 | } |
| ... | ... | @@ -3683,7 +3733,7 @@ static IrInstruction *ir_gen_switch_expr(IrBuilder *irb, Scope *scope, AstNode * |
| 3683 | 3733 | IrBasicBlock *prev_block = irb->current_basic_block; |
| 3684 | 3734 | ir_set_cursor_at_end(irb, prong_block); |
| 3685 | 3735 | if (!ir_gen_switch_prong_expr(irb, scope, node, prong_node, end_block, |
| 3686 | | is_inline, target_value_ptr, only_item_value, &incoming_blocks, &incoming_values)) |
| 3736 | is_comptime, target_value_ptr, only_item_value, &incoming_blocks, &incoming_values)) |
| 3687 | 3737 | { |
| 3688 | 3738 | return irb->codegen->invalid_instruction; |
| 3689 | 3739 | } |
| ... | ... | @@ -3695,9 +3745,9 @@ static IrInstruction *ir_gen_switch_expr(IrBuilder *irb, Scope *scope, AstNode * |
| 3695 | 3745 | } |
| 3696 | 3746 | |
| 3697 | 3747 | if (cases.length == 0) { |
| 3698 | | ir_build_br(irb, scope, node, else_block, is_inline); |
| 3748 | ir_build_br(irb, scope, node, else_block, is_comptime); |
| 3699 | 3749 | } else { |
| 3700 | | ir_build_switch_br(irb, scope, node, target_value, else_block, cases.length, cases.items, is_inline); |
| 3750 | ir_build_switch_br(irb, scope, node, target_value, else_block, cases.length, cases.items, is_comptime); |
| 3701 | 3751 | } |
| 3702 | 3752 | |
| 3703 | 3753 | if (!else_prong) { |
| ... | ... | @@ -3754,8 +3804,9 @@ static IrInstruction *ir_gen_label(IrBuilder *irb, Scope *scope, AstNode *node) |
| 3754 | 3804 | scope_block->label_table.put(label_name, label); |
| 3755 | 3805 | } |
| 3756 | 3806 | |
| 3757 | | bool is_inline = ir_should_inline(irb); |
| 3758 | | ir_build_br(irb, scope, node, label_block, is_inline); |
| 3807 | IrInstruction *is_comptime = ir_build_const_bool(irb, scope, node, |
| 3808 | ir_should_inline(irb)); |
| 3809 | ir_build_br(irb, scope, node, label_block, is_comptime); |
| 3759 | 3810 | ir_set_cursor_at_end(irb, label_block); |
| 3760 | 3811 | return ir_build_const_void(irb, scope, node); |
| 3761 | 3812 | } |
| ... | ... | @@ -3785,11 +3836,18 @@ static IrInstruction *ir_gen_break(IrBuilder *irb, Scope *scope, AstNode *node) |
| 3785 | 3836 | return irb->codegen->invalid_instruction; |
| 3786 | 3837 | } |
| 3787 | 3838 | |
| 3788 | | bool is_inline = ir_should_inline(irb) || node->data.break_expr.is_inline; |
| 3789 | 3839 | LoopStackItem *loop_stack_item = &irb->loop_stack.last(); |
| 3840 | |
| 3841 | IrInstruction *is_comptime; |
| 3842 | if (ir_should_inline(irb) || node->data.break_expr.is_inline) { |
| 3843 | is_comptime = ir_build_const_bool(irb, scope, node, true); |
| 3844 | } else { |
| 3845 | is_comptime = loop_stack_item->is_comptime; |
| 3846 | } |
| 3847 | |
| 3790 | 3848 | IrBasicBlock *dest_block = loop_stack_item->break_block; |
| 3791 | 3849 | ir_gen_defers_for_block(irb, scope, dest_block->scope, false, false); |
| 3792 | | return ir_build_br(irb, scope, node, dest_block, is_inline); |
| 3850 | return ir_build_br(irb, scope, node, dest_block, is_comptime); |
| 3793 | 3851 | } |
| 3794 | 3852 | |
| 3795 | 3853 | static IrInstruction *ir_gen_continue(IrBuilder *irb, Scope *scope, AstNode *node) { |
| ... | ... | @@ -3801,11 +3859,18 @@ static IrInstruction *ir_gen_continue(IrBuilder *irb, Scope *scope, AstNode *nod |
| 3801 | 3859 | return irb->codegen->invalid_instruction; |
| 3802 | 3860 | } |
| 3803 | 3861 | |
| 3804 | | bool is_inline = ir_should_inline(irb) || node->data.continue_expr.is_inline; |
| 3805 | 3862 | LoopStackItem *loop_stack_item = &irb->loop_stack.last(); |
| 3863 | |
| 3864 | IrInstruction *is_comptime; |
| 3865 | if (ir_should_inline(irb) || node->data.continue_expr.is_inline) { |
| 3866 | is_comptime = ir_build_const_bool(irb, scope, node, true); |
| 3867 | } else { |
| 3868 | is_comptime = loop_stack_item->is_comptime; |
| 3869 | } |
| 3870 | |
| 3806 | 3871 | IrBasicBlock *dest_block = loop_stack_item->continue_block; |
| 3807 | 3872 | ir_gen_defers_for_block(irb, scope, dest_block->scope, false, false); |
| 3808 | | return ir_build_br(irb, scope, node, dest_block, is_inline); |
| 3873 | return ir_build_br(irb, scope, node, dest_block, is_comptime); |
| 3809 | 3874 | } |
| 3810 | 3875 | |
| 3811 | 3876 | static IrInstruction *ir_gen_type_literal(IrBuilder *irb, Scope *scope, AstNode *node) { |
| ... | ... | @@ -3863,18 +3928,23 @@ static IrInstruction *ir_gen_err_ok_or(IrBuilder *irb, Scope *parent_scope, AstN |
| 3863 | 3928 | AstNode *op2_node = node->data.unwrap_err_expr.op2; |
| 3864 | 3929 | AstNode *var_node = node->data.unwrap_err_expr.symbol; |
| 3865 | 3930 | |
| 3866 | | bool is_inline = ir_should_inline(irb); |
| 3867 | | |
| 3868 | 3931 | IrInstruction *err_union_ptr = ir_gen_node_extra(irb, op1_node, parent_scope, LValPurposeAddressOf); |
| 3869 | 3932 | if (err_union_ptr == irb->codegen->invalid_instruction) |
| 3870 | 3933 | return irb->codegen->invalid_instruction; |
| 3871 | 3934 | |
| 3872 | 3935 | IrInstruction *is_err = ir_build_test_err(irb, parent_scope, node, err_union_ptr); |
| 3873 | 3936 | |
| 3937 | IrInstruction *is_comptime; |
| 3938 | if (ir_should_inline(irb)) { |
| 3939 | is_comptime = ir_build_const_bool(irb, parent_scope, node, true); |
| 3940 | } else { |
| 3941 | is_comptime = ir_build_test_comptime(irb, parent_scope, node, is_err); |
| 3942 | } |
| 3943 | |
| 3874 | 3944 | IrBasicBlock *ok_block = ir_build_basic_block(irb, parent_scope, "UnwrapErrOk"); |
| 3875 | 3945 | IrBasicBlock *err_block = ir_build_basic_block(irb, parent_scope, "UnwrapErrError"); |
| 3876 | 3946 | IrBasicBlock *end_block = ir_build_basic_block(irb, parent_scope, "UnwrapErrEnd"); |
| 3877 | | ir_build_cond_br(irb, parent_scope, node, is_err, err_block, ok_block, is_inline); |
| 3947 | ir_build_cond_br(irb, parent_scope, node, is_err, err_block, ok_block, is_comptime); |
| 3878 | 3948 | |
| 3879 | 3949 | ir_set_cursor_at_end(irb, err_block); |
| 3880 | 3950 | Scope *err_scope; |
| ... | ... | @@ -3886,7 +3956,7 @@ static IrInstruction *ir_gen_err_ok_or(IrBuilder *irb, Scope *parent_scope, AstN |
| 3886 | 3956 | bool is_const = true; |
| 3887 | 3957 | bool is_shadowable = false; |
| 3888 | 3958 | VariableTableEntry *var = ir_create_var(irb, node, parent_scope, var_name, |
| 3889 | | is_const, is_const, is_shadowable, is_inline); |
| 3959 | is_const, is_const, is_shadowable, is_comptime); |
| 3890 | 3960 | err_scope = var->child_scope; |
| 3891 | 3961 | IrInstruction *err_val = ir_build_unwrap_err_code(irb, err_scope, node, err_union_ptr); |
| 3892 | 3962 | ir_build_var_decl(irb, err_scope, var_node, var, var_type, err_val); |
| ... | ... | @@ -3897,13 +3967,13 @@ static IrInstruction *ir_gen_err_ok_or(IrBuilder *irb, Scope *parent_scope, AstN |
| 3897 | 3967 | if (err_result == irb->codegen->invalid_instruction) |
| 3898 | 3968 | return irb->codegen->invalid_instruction; |
| 3899 | 3969 | IrBasicBlock *after_err_block = irb->current_basic_block; |
| 3900 | | ir_build_br(irb, err_scope, node, end_block, is_inline); |
| 3970 | ir_build_br(irb, err_scope, node, end_block, is_comptime); |
| 3901 | 3971 | |
| 3902 | 3972 | ir_set_cursor_at_end(irb, ok_block); |
| 3903 | 3973 | IrInstruction *unwrapped_ptr = ir_build_unwrap_err_payload(irb, parent_scope, node, err_union_ptr, false); |
| 3904 | 3974 | IrInstruction *unwrapped_payload = ir_build_load_ptr(irb, parent_scope, node, unwrapped_ptr); |
| 3905 | 3975 | IrBasicBlock *after_ok_block = irb->current_basic_block; |
| 3906 | | ir_build_br(irb, parent_scope, node, end_block, is_inline); |
| 3976 | ir_build_br(irb, parent_scope, node, end_block, is_comptime); |
| 3907 | 3977 | |
| 3908 | 3978 | ir_set_cursor_at_end(irb, end_block); |
| 3909 | 3979 | IrInstruction **incoming_values = allocate<IrInstruction *>(2); |
| ... | ... | @@ -4108,9 +4178,10 @@ static bool ir_goto_pass2(IrBuilder *irb) { |
| 4108 | 4178 | } |
| 4109 | 4179 | label->used = true; |
| 4110 | 4180 | |
| 4111 | | bool is_inline = ir_should_inline(irb) || source_node->data.goto_expr.is_inline; |
| 4181 | IrInstruction *is_comptime = ir_build_const_bool(irb, goto_item->scope, source_node, |
| 4182 | ir_should_inline(irb) || source_node->data.goto_expr.is_inline); |
| 4112 | 4183 | ir_gen_defers_for_block(irb, goto_item->scope, label->bb->scope, false, false); |
| 4113 | | ir_build_br(irb, goto_item->scope, source_node, label->bb, is_inline); |
| 4184 | ir_build_br(irb, goto_item->scope, source_node, label->bb, is_comptime); |
| 4114 | 4185 | } |
| 4115 | 4186 | |
| 4116 | 4187 | for (size_t i = 0; i < irb->exec->all_labels.length; i += 1) { |
| ... | ... | @@ -4176,6 +4247,8 @@ static void add_call_stack_errors(CodeGen *codegen, IrExecutable *exec, ErrorMsg |
| 4176 | 4247 | add_call_stack_errors(codegen, exec->parent_exec, err_msg, limit - 1); |
| 4177 | 4248 | } |
| 4178 | 4249 | |
| 4250 | // TODO migrate the rest of the add_node_error errors to use this so that we get better |
| 4251 | // messages for generic instantiations |
| 4179 | 4252 | static ErrorMsg *ir_add_error(IrAnalyze *ira, IrInstruction *source_instruction, Buf *msg) { |
| 4180 | 4253 | ira->new_irb.exec->invalid = true; |
| 4181 | 4254 | ErrorMsg *err_msg = add_node_error(ira->codegen, source_instruction->source_node, msg); |
| ... | ... | @@ -5967,18 +6040,20 @@ static TypeTableEntry *ir_analyze_instruction_decl_var(IrAnalyze *ira, IrInstruc |
| 5967 | 6040 | var->type = result_type; |
| 5968 | 6041 | assert(var->type); |
| 5969 | 6042 | |
| 6043 | bool is_comptime = ir_get_var_is_comptime(var); |
| 6044 | |
| 5970 | 6045 | if (casted_init_value->static_value.special != ConstValSpecialRuntime) { |
| 5971 | 6046 | if (var->mem_slot_index != SIZE_MAX) { |
| 5972 | 6047 | assert(var->mem_slot_index < ira->exec_context.mem_slot_count); |
| 5973 | 6048 | ConstExprValue *mem_slot = &ira->exec_context.mem_slot_list[var->mem_slot_index]; |
| 5974 | 6049 | *mem_slot = casted_init_value->static_value; |
| 5975 | 6050 | |
| 5976 | | if (var->is_inline) { |
| 6051 | if (is_comptime) { |
| 5977 | 6052 | ir_build_const_from(ira, &decl_var_instruction->base, false); |
| 5978 | 6053 | return ira->codegen->builtin_types.entry_void; |
| 5979 | 6054 | } |
| 5980 | 6055 | } |
| 5981 | | } else if (var->is_inline) { |
| 6056 | } else if (is_comptime) { |
| 5982 | 6057 | ir_add_error(ira, &decl_var_instruction->base, |
| 5983 | 6058 | buf_sprintf("cannot store runtime value in compile time variable")); |
| 5984 | 6059 | var->type = ira->codegen->builtin_types.entry_invalid; |
| ... | ... | @@ -6586,9 +6661,12 @@ static TypeTableEntry *ir_analyze_instruction_un_op(IrAnalyze *ira, IrInstructio |
| 6586 | 6661 | static TypeTableEntry *ir_analyze_instruction_br(IrAnalyze *ira, IrInstructionBr *br_instruction) { |
| 6587 | 6662 | IrBasicBlock *old_dest_block = br_instruction->dest_block; |
| 6588 | 6663 | |
| 6589 | | if (br_instruction->is_inline || old_dest_block->ref_count == 1) { |
| 6664 | bool is_comptime; |
| 6665 | if (!ir_resolve_bool(ira, br_instruction->is_comptime->other, &is_comptime)) |
| 6666 | return ir_unreach_error(ira); |
| 6667 | |
| 6668 | if (is_comptime || old_dest_block->ref_count == 1) |
| 6590 | 6669 | return ir_inline_bb(ira, &br_instruction->base, old_dest_block); |
| 6591 | | } |
| 6592 | 6670 | |
| 6593 | 6671 | IrBasicBlock *new_bb = ir_get_new_bb(ira, old_dest_block); |
| 6594 | 6672 | ir_build_br_from(&ira->new_irb, &br_instruction->base, new_bb); |
| ... | ... | @@ -6600,7 +6678,11 @@ static TypeTableEntry *ir_analyze_instruction_cond_br(IrAnalyze *ira, IrInstruct |
| 6600 | 6678 | if (condition->type_entry->id == TypeTableEntryIdInvalid) |
| 6601 | 6679 | return ir_unreach_error(ira); |
| 6602 | 6680 | |
| 6603 | | if (cond_br_instruction->is_inline || condition->static_value.special != ConstValSpecialRuntime) { |
| 6681 | bool is_comptime; |
| 6682 | if (!ir_resolve_bool(ira, cond_br_instruction->is_comptime->other, &is_comptime)) |
| 6683 | return ir_unreach_error(ira); |
| 6684 | |
| 6685 | if (is_comptime || instr_is_comptime(condition)) { |
| 6604 | 6686 | bool cond_is_true; |
| 6605 | 6687 | if (!ir_resolve_bool(ira, condition, &cond_is_true)) |
| 6606 | 6688 | return ir_unreach_error(ira); |
| ... | ... | @@ -6608,7 +6690,7 @@ static TypeTableEntry *ir_analyze_instruction_cond_br(IrAnalyze *ira, IrInstruct |
| 6608 | 6690 | IrBasicBlock *old_dest_block = cond_is_true ? |
| 6609 | 6691 | cond_br_instruction->then_block : cond_br_instruction->else_block; |
| 6610 | 6692 | |
| 6611 | | if (cond_br_instruction->is_inline || old_dest_block->ref_count == 1) |
| 6693 | if (is_comptime || old_dest_block->ref_count == 1) |
| 6612 | 6694 | return ir_inline_bb(ira, &cond_br_instruction->base, old_dest_block); |
| 6613 | 6695 | } |
| 6614 | 6696 | |
| ... | ... | @@ -6620,7 +6702,7 @@ static TypeTableEntry *ir_analyze_instruction_cond_br(IrAnalyze *ira, IrInstruct |
| 6620 | 6702 | IrBasicBlock *new_then_block = ir_get_new_bb(ira, cond_br_instruction->then_block); |
| 6621 | 6703 | IrBasicBlock *new_else_block = ir_get_new_bb(ira, cond_br_instruction->else_block); |
| 6622 | 6704 | ir_build_cond_br_from(&ira->new_irb, &cond_br_instruction->base, |
| 6623 | | casted_condition, new_then_block, new_else_block, false); |
| 6705 | casted_condition, new_then_block, new_else_block, nullptr); |
| 6624 | 6706 | return ir_finish_anal(ira, ira->codegen->builtin_types.entry_unreachable); |
| 6625 | 6707 | } |
| 6626 | 6708 | |
| ... | ... | @@ -6723,6 +6805,8 @@ static TypeTableEntry *ir_analyze_var_ptr(IrAnalyze *ira, IrInstruction *instruc |
| 6723 | 6805 | if (var->type->id == TypeTableEntryIdInvalid) |
| 6724 | 6806 | return var->type; |
| 6725 | 6807 | |
| 6808 | bool is_comptime = ir_get_var_is_comptime(var); |
| 6809 | |
| 6726 | 6810 | ConstExprValue *mem_slot = nullptr; |
| 6727 | 6811 | FnTableEntry *fn_entry = scope_fn_entry(var->parent_scope); |
| 6728 | 6812 | if (var->src_is_const && var->value) { |
| ... | ... | @@ -6730,12 +6814,12 @@ static TypeTableEntry *ir_analyze_var_ptr(IrAnalyze *ira, IrInstruction *instruc |
| 6730 | 6814 | assert(mem_slot->special != ConstValSpecialRuntime); |
| 6731 | 6815 | } else if (fn_entry) { |
| 6732 | 6816 | // TODO once the analyze code is fully ported over to IR we won't need this SIZE_MAX thing. |
| 6733 | | if (var->mem_slot_index != SIZE_MAX) |
| 6817 | if (var->mem_slot_index != SIZE_MAX && (is_comptime || var->gen_is_const)) |
| 6734 | 6818 | mem_slot = &ira->exec_context.mem_slot_list[var->mem_slot_index]; |
| 6735 | 6819 | } |
| 6736 | 6820 | |
| 6737 | 6821 | if (mem_slot && mem_slot->special != ConstValSpecialRuntime) { |
| 6738 | | ConstPtrSpecial ptr_special = var->is_inline ? ConstPtrSpecialInline : ConstPtrSpecialNone; |
| 6822 | ConstPtrSpecial ptr_special = is_comptime ? ConstPtrSpecialInline : ConstPtrSpecialNone; |
| 6739 | 6823 | return ir_analyze_const_ptr(ira, instruction, mem_slot, var->type, false, ptr_special, var->src_is_const); |
| 6740 | 6824 | } else { |
| 6741 | 6825 | ir_build_var_ptr_from(&ira->new_irb, instruction, var); |
| ... | ... | @@ -7827,9 +7911,12 @@ static TypeTableEntry *ir_analyze_instruction_switch_br(IrAnalyze *ira, |
| 7827 | 7911 | return ir_unreach_error(ira); |
| 7828 | 7912 | |
| 7829 | 7913 | size_t case_count = switch_br_instruction->case_count; |
| 7830 | | bool is_inline = ir_should_inline(&ira->new_irb) || switch_br_instruction->is_inline; |
| 7831 | 7914 | |
| 7832 | | if (is_inline || instr_is_comptime(target_value)) { |
| 7915 | bool is_comptime; |
| 7916 | if (!ir_resolve_bool(ira, switch_br_instruction->is_comptime->other, &is_comptime)) |
| 7917 | return ira->codegen->builtin_types.entry_invalid; |
| 7918 | |
| 7919 | if (is_comptime || instr_is_comptime(target_value)) { |
| 7833 | 7920 | ConstExprValue *target_val = ir_resolve_const(ira, target_value, UndefBad); |
| 7834 | 7921 | if (!target_val) |
| 7835 | 7922 | return ir_unreach_error(ira); |
| ... | ... | @@ -7856,7 +7943,7 @@ static TypeTableEntry *ir_analyze_instruction_switch_br(IrAnalyze *ira, |
| 7856 | 7943 | |
| 7857 | 7944 | if (const_values_equal(target_val, case_val, target_value->type_entry)) { |
| 7858 | 7945 | IrBasicBlock *old_dest_block = old_case->block; |
| 7859 | | if (is_inline || old_dest_block->ref_count == 1) { |
| 7946 | if (is_comptime || old_dest_block->ref_count == 1) { |
| 7860 | 7947 | return ir_inline_bb(ira, &switch_br_instruction->base, old_dest_block); |
| 7861 | 7948 | } else { |
| 7862 | 7949 | IrBasicBlock *new_dest_block = ir_get_new_bb(ira, old_dest_block); |
| ... | ... | @@ -7898,7 +7985,7 @@ static TypeTableEntry *ir_analyze_instruction_switch_br(IrAnalyze *ira, |
| 7898 | 7985 | |
| 7899 | 7986 | IrBasicBlock *new_else_block = ir_get_new_bb(ira, switch_br_instruction->else_block); |
| 7900 | 7987 | ir_build_switch_br_from(&ira->new_irb, &switch_br_instruction->base, |
| 7901 | | target_value, new_else_block, case_count, cases, false); |
| 7988 | target_value, new_else_block, case_count, cases, nullptr); |
| 7902 | 7989 | return ir_finish_anal(ira, ira->codegen->builtin_types.entry_unreachable); |
| 7903 | 7990 | } |
| 7904 | 7991 | |
| ... | ... | @@ -9520,6 +9607,16 @@ static TypeTableEntry *ir_analyze_instruction_fn_proto(IrAnalyze *ira, IrInstruc |
| 9520 | 9607 | return ira->codegen->builtin_types.entry_type; |
| 9521 | 9608 | } |
| 9522 | 9609 | |
| 9610 | static TypeTableEntry *ir_analyze_instruction_test_comptime(IrAnalyze *ira, IrInstructionTestComptime *instruction) { |
| 9611 | IrInstruction *value = instruction->value->other; |
| 9612 | if (value->type_entry->id == TypeTableEntryIdInvalid) |
| 9613 | return ira->codegen->builtin_types.entry_invalid; |
| 9614 | |
| 9615 | ConstExprValue *out_val = ir_build_const_from(ira, &instruction->base, false); |
| 9616 | out_val->data.x_bool = instr_is_comptime(value); |
| 9617 | return ira->codegen->builtin_types.entry_bool; |
| 9618 | } |
| 9619 | |
| 9523 | 9620 | static TypeTableEntry *ir_analyze_instruction_nocast(IrAnalyze *ira, IrInstruction *instruction) { |
| 9524 | 9621 | switch (instruction->id) { |
| 9525 | 9622 | case IrInstructionIdInvalid: |
| ... | ... | @@ -9662,6 +9759,8 @@ static TypeTableEntry *ir_analyze_instruction_nocast(IrAnalyze *ira, IrInstructi |
| 9662 | 9759 | return ir_analyze_instruction_unwrap_err_payload(ira, (IrInstructionUnwrapErrPayload *)instruction); |
| 9663 | 9760 | case IrInstructionIdFnProto: |
| 9664 | 9761 | return ir_analyze_instruction_fn_proto(ira, (IrInstructionFnProto *)instruction); |
| 9762 | case IrInstructionIdTestComptime: |
| 9763 | return ir_analyze_instruction_test_comptime(ira, (IrInstructionTestComptime *)instruction); |
| 9665 | 9764 | case IrInstructionIdMaybeWrap: |
| 9666 | 9765 | case IrInstructionIdErrWrapCode: |
| 9667 | 9766 | case IrInstructionIdErrWrapPayload: |
| ... | ... | @@ -9823,6 +9922,7 @@ bool ir_has_side_effects(IrInstruction *instruction) { |
| 9823 | 9922 | case IrInstructionIdErrWrapCode: |
| 9824 | 9923 | case IrInstructionIdErrWrapPayload: |
| 9825 | 9924 | case IrInstructionIdFnProto: |
| 9925 | case IrInstructionIdTestComptime: |
| 9826 | 9926 | return false; |
| 9827 | 9927 | case IrInstructionIdAsm: |
| 9828 | 9928 | { |
| ... | ... | @@ -9832,3 +9932,4 @@ bool ir_has_side_effects(IrInstruction *instruction) { |
| 9832 | 9932 | } |
| 9833 | 9933 | zig_unreachable(); |
| 9834 | 9934 | } |
| 9935 | |