| ... | ... | @@ -307,6 +307,10 @@ static constexpr IrInstructionId ir_instruction_id(IrInstructionStaticEval *) { |
| 307 | 307 | return IrInstructionIdStaticEval; |
| 308 | 308 | } |
| 309 | 309 | |
| 310 | static constexpr IrInstructionId ir_instruction_id(IrInstructionGeneratedCode *) { |
| 311 | return IrInstructionIdGeneratedCode; |
| 312 | } |
| 313 | |
| 310 | 314 | static constexpr IrInstructionId ir_instruction_id(IrInstructionImport *) { |
| 311 | 315 | return IrInstructionIdImport; |
| 312 | 316 | } |
| ... | ... | @@ -616,19 +620,20 @@ static IrInstruction *ir_build_const_usize(IrBuilder *irb, Scope *scope, AstNode |
| 616 | 620 | } |
| 617 | 621 | |
| 618 | 622 | static IrInstruction *ir_create_const_type(IrBuilder *irb, Scope *scope, AstNode *source_node, |
| 619 | | TypeTableEntry *type_entry) |
| 623 | TypeTableEntry *type_entry, bool depends_on_compile_var) |
| 620 | 624 | { |
| 621 | 625 | IrInstructionConst *const_instruction = ir_create_instruction<IrInstructionConst>(irb, scope, source_node); |
| 622 | 626 | const_instruction->base.value.type = irb->codegen->builtin_types.entry_type; |
| 623 | 627 | const_instruction->base.value.special = ConstValSpecialStatic; |
| 628 | const_instruction->base.value.depends_on_compile_var = depends_on_compile_var; |
| 624 | 629 | const_instruction->base.value.data.x_type = type_entry; |
| 625 | 630 | return &const_instruction->base; |
| 626 | 631 | } |
| 627 | 632 | |
| 628 | 633 | static IrInstruction *ir_build_const_type(IrBuilder *irb, Scope *scope, AstNode *source_node, |
| 629 | | TypeTableEntry *type_entry) |
| 634 | TypeTableEntry *type_entry, bool depends_on_compile_var) |
| 630 | 635 | { |
| 631 | | IrInstruction *instruction = ir_create_const_type(irb, scope, source_node, type_entry); |
| 636 | IrInstruction *instruction = ir_create_const_type(irb, scope, source_node, type_entry, depends_on_compile_var); |
| 632 | 637 | ir_instruction_append(irb->current_basic_block, instruction); |
| 633 | 638 | return instruction; |
| 634 | 639 | } |
| ... | ... | @@ -1392,6 +1397,17 @@ static IrInstruction *ir_build_static_eval(IrBuilder *irb, Scope *scope, AstNode |
| 1392 | 1397 | return &instruction->base; |
| 1393 | 1398 | } |
| 1394 | 1399 | |
| 1400 | static IrInstruction *ir_build_generated_code(IrBuilder *irb, Scope *scope, AstNode *source_node, |
| 1401 | IrInstruction *value) |
| 1402 | { |
| 1403 | IrInstructionGeneratedCode *instruction = ir_build_instruction<IrInstructionGeneratedCode>(irb, scope, source_node); |
| 1404 | instruction->value = value; |
| 1405 | |
| 1406 | ir_ref_instruction(value, irb->current_basic_block); |
| 1407 | |
| 1408 | return &instruction->base; |
| 1409 | } |
| 1410 | |
| 1395 | 1411 | static IrInstruction *ir_build_import(IrBuilder *irb, Scope *scope, AstNode *source_node, IrInstruction *name) { |
| 1396 | 1412 | IrInstructionImport *instruction = ir_build_instruction<IrInstructionImport>(irb, scope, source_node); |
| 1397 | 1413 | instruction->name = name; |
| ... | ... | @@ -2258,6 +2274,13 @@ static IrInstruction *ir_instruction_staticeval_get_dep(IrInstructionStaticEval |
| 2258 | 2274 | } |
| 2259 | 2275 | } |
| 2260 | 2276 | |
| 2277 | static IrInstruction *ir_instruction_generatedcode_get_dep(IrInstructionGeneratedCode *instruction, size_t index) { |
| 2278 | switch (index) { |
| 2279 | case 0: return instruction->value; |
| 2280 | default: return nullptr; |
| 2281 | } |
| 2282 | } |
| 2283 | |
| 2261 | 2284 | static IrInstruction *ir_instruction_import_get_dep(IrInstructionImport *instruction, size_t index) { |
| 2262 | 2285 | switch (index) { |
| 2263 | 2286 | case 0: return instruction->name; |
| ... | ... | @@ -2645,6 +2668,8 @@ static IrInstruction *ir_instruction_get_dep(IrInstruction *instruction, size_t |
| 2645 | 2668 | return ir_instruction_ctz_get_dep((IrInstructionCtz *) instruction, index); |
| 2646 | 2669 | case IrInstructionIdStaticEval: |
| 2647 | 2670 | return ir_instruction_staticeval_get_dep((IrInstructionStaticEval *) instruction, index); |
| 2671 | case IrInstructionIdGeneratedCode: |
| 2672 | return ir_instruction_generatedcode_get_dep((IrInstructionGeneratedCode *) instruction, index); |
| 2648 | 2673 | case IrInstructionIdImport: |
| 2649 | 2674 | return ir_instruction_import_get_dep((IrInstructionImport *) instruction, index); |
| 2650 | 2675 | case IrInstructionIdCImport: |
| ... | ... | @@ -2846,7 +2871,7 @@ static IrInstruction *ir_gen_return(IrBuilder *irb, Scope *scope, AstNode *node, |
| 2846 | 2871 | is_comptime = ir_build_test_comptime(irb, scope, node, is_err); |
| 2847 | 2872 | } |
| 2848 | 2873 | |
| 2849 | | ir_build_cond_br(irb, scope, node, is_err, err_block, ok_block, is_comptime); |
| 2874 | ir_mark_gen(ir_build_cond_br(irb, scope, node, is_err, err_block, ok_block, is_comptime)); |
| 2850 | 2875 | |
| 2851 | 2876 | ir_set_cursor_at_end(irb, err_block); |
| 2852 | 2877 | ir_gen_defers_for_block(irb, scope, outer_scope, true, false); |
| ... | ... | @@ -2868,7 +2893,7 @@ static IrInstruction *ir_gen_return(IrBuilder *irb, Scope *scope, AstNode *node, |
| 2868 | 2893 | is_comptime = ir_build_test_comptime(irb, scope, node, is_non_null); |
| 2869 | 2894 | } |
| 2870 | 2895 | |
| 2871 | | ir_build_cond_br(irb, scope, node, is_non_null, ok_block, null_block, is_comptime); |
| 2896 | ir_mark_gen(ir_build_cond_br(irb, scope, node, is_non_null, ok_block, null_block, is_comptime)); |
| 2872 | 2897 | |
| 2873 | 2898 | ir_set_cursor_at_end(irb, null_block); |
| 2874 | 2899 | ir_gen_defers_for_block(irb, scope, outer_scope, false, true); |
| ... | ... | @@ -2895,7 +2920,7 @@ static IrInstruction *ir_gen_return(IrBuilder *irb, Scope *scope, AstNode *node, |
| 2895 | 2920 | IrBasicBlock *return_block = ir_build_basic_block(irb, scope, "ErrRetReturn"); |
| 2896 | 2921 | IrBasicBlock *continue_block = ir_build_basic_block(irb, scope, "ErrRetContinue"); |
| 2897 | 2922 | IrInstruction *is_comptime = ir_build_const_bool(irb, scope, node, ir_should_inline(irb)); |
| 2898 | | ir_build_cond_br(irb, scope, node, is_err_val, return_block, continue_block, is_comptime); |
| 2923 | ir_mark_gen(ir_build_cond_br(irb, scope, node, is_err_val, return_block, continue_block, is_comptime)); |
| 2899 | 2924 | |
| 2900 | 2925 | ir_set_cursor_at_end(irb, return_block); |
| 2901 | 2926 | ir_gen_defers_for_block(irb, scope, outer_scope, true, false); |
| ... | ... | @@ -2921,7 +2946,7 @@ static IrInstruction *ir_gen_return(IrBuilder *irb, Scope *scope, AstNode *node, |
| 2921 | 2946 | IrBasicBlock *return_block = ir_build_basic_block(irb, scope, "MaybeRetReturn"); |
| 2922 | 2947 | IrBasicBlock *continue_block = ir_build_basic_block(irb, scope, "MaybeRetContinue"); |
| 2923 | 2948 | IrInstruction *is_comptime = ir_build_const_bool(irb, scope, node, ir_should_inline(irb)); |
| 2924 | | ir_build_cond_br(irb, scope, node, is_non_null, continue_block, return_block, is_comptime); |
| 2949 | ir_mark_gen(ir_build_cond_br(irb, scope, node, is_non_null, continue_block, return_block, is_comptime)); |
| 2925 | 2950 | |
| 2926 | 2951 | ir_set_cursor_at_end(irb, return_block); |
| 2927 | 2952 | ir_gen_defers_for_block(irb, scope, outer_scope, false, true); |
| ... | ... | @@ -3388,7 +3413,7 @@ static IrInstruction *ir_gen_null_literal(IrBuilder *irb, Scope *scope, AstNode |
| 3388 | 3413 | static IrInstruction *ir_gen_var_literal(IrBuilder *irb, Scope *scope, AstNode *node) { |
| 3389 | 3414 | assert(node->type == NodeTypeVarLiteral); |
| 3390 | 3415 | |
| 3391 | | return ir_build_const_type(irb, scope, node, irb->codegen->builtin_types.entry_var); |
| 3416 | return ir_build_const_type(irb, scope, node, irb->codegen->builtin_types.entry_var, false); |
| 3392 | 3417 | } |
| 3393 | 3418 | |
| 3394 | 3419 | static IrInstruction *ir_gen_decl_ref(IrBuilder *irb, AstNode *source_node, Tld *tld, |
| ... | ... | @@ -3426,7 +3451,7 @@ static IrInstruction *ir_gen_decl_ref(IrBuilder *irb, AstNode *source_node, Tld |
| 3426 | 3451 | { |
| 3427 | 3452 | TldTypeDef *tld_typedef = (TldTypeDef *)tld; |
| 3428 | 3453 | TypeTableEntry *typedef_type = tld_typedef->type_entry; |
| 3429 | | IrInstruction *ref_instruction = ir_build_const_type(irb, scope, source_node, typedef_type); |
| 3454 | IrInstruction *ref_instruction = ir_build_const_type(irb, scope, source_node, typedef_type, false); |
| 3430 | 3455 | if (lval != LValPurposeNone) |
| 3431 | 3456 | return ir_build_ref(irb, scope, source_node, ref_instruction, true); |
| 3432 | 3457 | else |
| ... | ... | @@ -3443,7 +3468,7 @@ static IrInstruction *ir_gen_symbol(IrBuilder *irb, Scope *scope, AstNode *node, |
| 3443 | 3468 | |
| 3444 | 3469 | auto primitive_table_entry = irb->codegen->primitive_type_table.maybe_get(variable_name); |
| 3445 | 3470 | if (primitive_table_entry) { |
| 3446 | | IrInstruction *value = ir_build_const_type(irb, scope, node, primitive_table_entry->value); |
| 3471 | IrInstruction *value = ir_build_const_type(irb, scope, node, primitive_table_entry->value, false); |
| 3447 | 3472 | if (lval != LValPurposeNone) { |
| 3448 | 3473 | return ir_build_ref(irb, scope, node, value, lval == LValPurposeAddressOfConst); |
| 3449 | 3474 | } else { |
| ... | ... | @@ -3664,6 +3689,15 @@ static IrInstruction *ir_gen_builtin_fn_call(IrBuilder *irb, Scope *scope, AstNo |
| 3664 | 3689 | |
| 3665 | 3690 | return ir_build_static_eval(irb, scope, node, arg0_value); |
| 3666 | 3691 | } |
| 3692 | case BuiltinFnIdGeneratedCode: |
| 3693 | { |
| 3694 | AstNode *arg0_node = node->data.fn_call_expr.params.at(0); |
| 3695 | IrInstruction *arg0_value = ir_gen_node(irb, arg0_node, scope); |
| 3696 | if (arg0_value == irb->codegen->invalid_instruction) |
| 3697 | return arg0_value; |
| 3698 | |
| 3699 | return ir_build_generated_code(irb, scope, node, arg0_value); |
| 3700 | } |
| 3667 | 3701 | case BuiltinFnIdImport: |
| 3668 | 3702 | { |
| 3669 | 3703 | AstNode *arg0_node = node->data.fn_call_expr.params.at(0); |
| ... | ... | @@ -4327,7 +4361,7 @@ static IrInstruction *ir_gen_for_expr(IrBuilder *irb, Scope *parent_scope, AstNo |
| 4327 | 4361 | } |
| 4328 | 4362 | child_scope = index_var->child_scope; |
| 4329 | 4363 | |
| 4330 | | IrInstruction *usize = ir_build_const_type(irb, child_scope, node, irb->codegen->builtin_types.entry_usize); |
| 4364 | IrInstruction *usize = ir_build_const_type(irb, child_scope, node, irb->codegen->builtin_types.entry_usize, false); |
| 4331 | 4365 | IrInstruction *zero = ir_build_const_usize(irb, child_scope, node, 0); |
| 4332 | 4366 | IrInstruction *one = ir_build_const_usize(irb, child_scope, node, 1); |
| 4333 | 4367 | ir_build_var_decl(irb, child_scope, index_var_source_node, index_var, usize, zero); |
| ... | ... | @@ -4345,7 +4379,7 @@ static IrInstruction *ir_gen_for_expr(IrBuilder *irb, Scope *parent_scope, AstNo |
| 4345 | 4379 | ir_set_cursor_at_end(irb, cond_block); |
| 4346 | 4380 | IrInstruction *index_val = ir_build_load_ptr(irb, child_scope, node, index_ptr); |
| 4347 | 4381 | IrInstruction *cond = ir_build_bin_op(irb, child_scope, node, IrBinOpCmpLessThan, index_val, len_val, false); |
| 4348 | | ir_build_cond_br(irb, child_scope, node, cond, body_block, end_block, is_comptime); |
| 4382 | ir_mark_gen(ir_build_cond_br(irb, child_scope, node, cond, body_block, end_block, is_comptime)); |
| 4349 | 4383 | |
| 4350 | 4384 | ir_set_cursor_at_end(irb, body_block); |
| 4351 | 4385 | IrInstruction *elem_ptr = ir_build_elem_ptr(irb, child_scope, node, array_val_ptr, index_val, false); |
| ... | ... | @@ -4391,7 +4425,7 @@ static IrInstruction *ir_gen_this_literal(IrBuilder *irb, Scope *scope, AstNode |
| 4391 | 4425 | ScopeDecls *decls_scope = (ScopeDecls *)scope; |
| 4392 | 4426 | TypeTableEntry *container_type = decls_scope->container_type; |
| 4393 | 4427 | assert(container_type); |
| 4394 | | return ir_build_const_type(irb, scope, node, container_type); |
| 4428 | return ir_build_const_type(irb, scope, node, container_type, false); |
| 4395 | 4429 | } |
| 4396 | 4430 | |
| 4397 | 4431 | if (scope->id == ScopeIdBlock) |
| ... | ... | @@ -4719,7 +4753,8 @@ static IrInstruction *ir_gen_switch_expr(IrBuilder *irb, Scope *scope, AstNode * |
| 4719 | 4753 | |
| 4720 | 4754 | assert(ok_bit); |
| 4721 | 4755 | assert(last_item_node); |
| 4722 | | ir_build_cond_br(irb, scope, last_item_node, ok_bit, range_block_yes, range_block_no, is_comptime); |
| 4756 | ir_mark_gen(ir_build_cond_br(irb, scope, last_item_node, ok_bit, range_block_yes, |
| 4757 | range_block_no, is_comptime)); |
| 4723 | 4758 | |
| 4724 | 4759 | ir_set_cursor_at_end(irb, range_block_yes); |
| 4725 | 4760 | if (!ir_gen_switch_prong_expr(irb, scope, node, prong_node, end_block, |
| ... | ... | @@ -4843,12 +4878,12 @@ static IrInstruction *ir_gen_continue(IrBuilder *irb, Scope *scope, AstNode *nod |
| 4843 | 4878 | |
| 4844 | 4879 | static IrInstruction *ir_gen_type_literal(IrBuilder *irb, Scope *scope, AstNode *node) { |
| 4845 | 4880 | assert(node->type == NodeTypeTypeLiteral); |
| 4846 | | return ir_build_const_type(irb, scope, node, irb->codegen->builtin_types.entry_type); |
| 4881 | return ir_build_const_type(irb, scope, node, irb->codegen->builtin_types.entry_type, false); |
| 4847 | 4882 | } |
| 4848 | 4883 | |
| 4849 | 4884 | static IrInstruction *ir_gen_error_type(IrBuilder *irb, Scope *scope, AstNode *node) { |
| 4850 | 4885 | assert(node->type == NodeTypeErrorType); |
| 4851 | | return ir_build_const_type(irb, scope, node, irb->codegen->builtin_types.entry_pure_error); |
| 4886 | return ir_build_const_type(irb, scope, node, irb->codegen->builtin_types.entry_pure_error, false); |
| 4852 | 4887 | } |
| 4853 | 4888 | |
| 4854 | 4889 | static IrInstruction *ir_gen_defer(IrBuilder *irb, Scope *parent_scope, AstNode *node) { |
| ... | ... | @@ -4922,7 +4957,7 @@ static IrInstruction *ir_gen_err_ok_or(IrBuilder *irb, Scope *parent_scope, AstN |
| 4922 | 4957 | if (var_node) { |
| 4923 | 4958 | assert(var_node->type == NodeTypeSymbol); |
| 4924 | 4959 | IrInstruction *var_type = ir_build_const_type(irb, parent_scope, node, |
| 4925 | | irb->codegen->builtin_types.entry_pure_error); |
| 4960 | irb->codegen->builtin_types.entry_pure_error, false); |
| 4926 | 4961 | Buf *var_name = var_node->data.symbol_expr.symbol; |
| 4927 | 4962 | bool is_const = true; |
| 4928 | 4963 | bool is_shadowable = false; |
| ... | ... | @@ -5008,7 +5043,7 @@ static IrInstruction *ir_gen_container_decl(IrBuilder *irb, Scope *parent_scope, |
| 5008 | 5043 | } |
| 5009 | 5044 | irb->codegen->resolve_queue.append(&tld_container->base); |
| 5010 | 5045 | |
| 5011 | | return ir_build_const_type(irb, parent_scope, node, container_type); |
| 5046 | return ir_build_const_type(irb, parent_scope, node, container_type, false); |
| 5012 | 5047 | } |
| 5013 | 5048 | |
| 5014 | 5049 | static IrInstruction *ir_gen_fn_proto(IrBuilder *irb, Scope *parent_scope, AstNode *node) { |
| ... | ... | @@ -6530,8 +6565,10 @@ static IrInstruction *ir_get_deref(IrAnalyze *ira, IrInstruction *source_instruc |
| 6530 | 6565 | ConstExprValue *pointee = const_ptr_pointee(&ptr->value); |
| 6531 | 6566 | if (pointee->special != ConstValSpecialRuntime) { |
| 6532 | 6567 | IrInstruction *result = ir_create_const(&ira->new_irb, source_instruction->scope, |
| 6533 | | source_instruction->source_node, child_type, pointee->depends_on_compile_var); |
| 6568 | source_instruction->source_node, child_type, false); |
| 6534 | 6569 | result->value = *pointee; |
| 6570 | result->value.depends_on_compile_var = pointee->depends_on_compile_var || |
| 6571 | ptr->value.depends_on_compile_var; |
| 6535 | 6572 | return result; |
| 6536 | 6573 | } |
| 6537 | 6574 | } |
| ... | ... | @@ -6547,7 +6584,8 @@ static IrInstruction *ir_get_deref(IrAnalyze *ira, IrInstruction *source_instruc |
| 6547 | 6584 | TypeTableEntry *ptr_type = ptr_val->data.x_type; |
| 6548 | 6585 | if (ptr_type->id == TypeTableEntryIdPointer) { |
| 6549 | 6586 | TypeTableEntry *child_type = ptr_type->data.pointer.child_type; |
| 6550 | | return ir_create_const_type(&ira->new_irb, source_instruction->scope, source_instruction->source_node, child_type); |
| 6587 | return ir_create_const_type(&ira->new_irb, source_instruction->scope, |
| 6588 | source_instruction->source_node, child_type, ptr_val->depends_on_compile_var); |
| 6551 | 6589 | } else { |
| 6552 | 6590 | ir_add_error(ira, source_instruction, |
| 6553 | 6591 | buf_sprintf("attempt to dereference non pointer type '%s'", buf_ptr(&ptr_type->name))); |
| ... | ... | @@ -6572,7 +6610,7 @@ static TypeTableEntry *ir_analyze_ref(IrAnalyze *ira, IrInstruction *source_inst |
| 6572 | 6610 | if (!val) |
| 6573 | 6611 | return ira->codegen->builtin_types.entry_invalid; |
| 6574 | 6612 | return ir_analyze_const_ptr(ira, source_instruction, val, value->value.type, |
| 6575 | | false, ConstPtrSpecialNone, is_const); |
| 6613 | value->value.depends_on_compile_var, ConstPtrSpecialNone, is_const); |
| 6576 | 6614 | } |
| 6577 | 6615 | |
| 6578 | 6616 | TypeTableEntry *ptr_type = get_pointer_to_type(ira->codegen, value->value.type, true); |
| ... | ... | @@ -7367,6 +7405,7 @@ static bool ir_analyze_fn_call_inline_arg(IrAnalyze *ira, AstNode *fn_proto_node |
| 7367 | 7405 | Buf *param_name = param_decl_node->data.param_decl.name; |
| 7368 | 7406 | VariableTableEntry *var = add_variable(ira->codegen, param_decl_node, |
| 7369 | 7407 | *exec_scope, param_name, true, arg_val); |
| 7408 | var->value.depends_on_compile_var = true; |
| 7370 | 7409 | *exec_scope = var->child_scope; |
| 7371 | 7410 | *next_proto_i += 1; |
| 7372 | 7411 | |
| ... | ... | @@ -7408,6 +7447,7 @@ static bool ir_analyze_fn_call_generic_arg(IrAnalyze *ira, AstNode *fn_proto_nod |
| 7408 | 7447 | Buf *param_name = param_decl_node->data.param_decl.name; |
| 7409 | 7448 | VariableTableEntry *var = add_variable(ira->codegen, param_decl_node, |
| 7410 | 7449 | *child_scope, param_name, true, arg_val); |
| 7450 | var->value.depends_on_compile_var = true; |
| 7411 | 7451 | *child_scope = var->child_scope; |
| 7412 | 7452 | |
| 7413 | 7453 | if (inline_arg || is_var_type) { |
| ... | ... | @@ -7986,6 +8026,14 @@ static TypeTableEntry *ir_analyze_instruction_cond_br(IrAnalyze *ira, IrInstruct |
| 7986 | 8026 | if (!ir_resolve_bool(ira, condition, &cond_is_true)) |
| 7987 | 8027 | return ir_unreach_error(ira); |
| 7988 | 8028 | |
| 8029 | if (!cond_br_instruction->base.is_gen && !condition->value.depends_on_compile_var && |
| 8030 | !ir_should_inline(&ira->new_irb)) |
| 8031 | { |
| 8032 | const char *true_or_false = cond_is_true ? "true" : "false"; |
| 8033 | ir_add_error(ira, &cond_br_instruction->base, |
| 8034 | buf_sprintf("condition is always %s; unnecessary if statement", true_or_false)); |
| 8035 | } |
| 8036 | |
| 7989 | 8037 | IrBasicBlock *old_dest_block = cond_is_true ? |
| 7990 | 8038 | cond_br_instruction->then_block : cond_br_instruction->else_block; |
| 7991 | 8039 | |
| ... | ... | @@ -8028,9 +8076,9 @@ static TypeTableEntry *ir_analyze_instruction_phi(IrAnalyze *ira, IrInstructionP |
| 8028 | 8076 | return ira->codegen->builtin_types.entry_invalid; |
| 8029 | 8077 | |
| 8030 | 8078 | if (value->value.special != ConstValSpecialRuntime) { |
| 8031 | | ConstExprValue *out_val = ir_build_const_from(ira, &phi_instruction->base, |
| 8032 | | value->value.depends_on_compile_var); |
| 8079 | ConstExprValue *out_val = ir_build_const_from(ira, &phi_instruction->base, true); |
| 8033 | 8080 | *out_val = value->value; |
| 8081 | out_val->depends_on_compile_var = true; |
| 8034 | 8082 | } else { |
| 8035 | 8083 | phi_instruction->base.other = value; |
| 8036 | 8084 | } |
| ... | ... | @@ -8064,7 +8112,7 @@ static TypeTableEntry *ir_analyze_instruction_phi(IrAnalyze *ira, IrInstructionP |
| 8064 | 8112 | } |
| 8065 | 8113 | |
| 8066 | 8114 | if (new_incoming_blocks.length == 0) { |
| 8067 | | ir_build_const_from(ira, &phi_instruction->base, false); |
| 8115 | ir_build_const_from(ira, &phi_instruction->base, true); |
| 8068 | 8116 | return ira->codegen->builtin_types.entry_void; |
| 8069 | 8117 | } |
| 8070 | 8118 | |
| ... | ... | @@ -8080,7 +8128,9 @@ static TypeTableEntry *ir_analyze_instruction_phi(IrAnalyze *ira, IrInstructionP |
| 8080 | 8128 | return resolved_type; |
| 8081 | 8129 | |
| 8082 | 8130 | if (resolved_type->id == TypeTableEntryIdNumLitFloat || |
| 8083 | | resolved_type->id == TypeTableEntryIdNumLitInt) |
| 8131 | resolved_type->id == TypeTableEntryIdNumLitInt || |
| 8132 | resolved_type->id == TypeTableEntryIdNullLit || |
| 8133 | resolved_type->id == TypeTableEntryIdUndefLit) |
| 8084 | 8134 | { |
| 8085 | 8135 | ir_add_error_node(ira, phi_instruction->base.source_node, |
| 8086 | 8136 | buf_sprintf("unable to infer expression type")); |
| ... | ... | @@ -8109,7 +8159,7 @@ static TypeTableEntry *ir_analyze_instruction_phi(IrAnalyze *ira, IrInstructionP |
| 8109 | 8159 | } |
| 8110 | 8160 | |
| 8111 | 8161 | static TypeTableEntry *ir_analyze_var_ptr(IrAnalyze *ira, IrInstruction *instruction, |
| 8112 | | VariableTableEntry *var, bool is_const_ptr) |
| 8162 | VariableTableEntry *var, bool is_const_ptr, bool depends_on_compile_var) |
| 8113 | 8163 | { |
| 8114 | 8164 | assert(var->value.type); |
| 8115 | 8165 | if (var->value.type->id == TypeTableEntryIdInvalid) |
| ... | ... | @@ -8131,7 +8181,8 @@ static TypeTableEntry *ir_analyze_var_ptr(IrAnalyze *ira, IrInstruction *instruc |
| 8131 | 8181 | if (mem_slot && mem_slot->special != ConstValSpecialRuntime) { |
| 8132 | 8182 | ConstPtrSpecial ptr_special = is_comptime ? ConstPtrSpecialInline : ConstPtrSpecialNone; |
| 8133 | 8183 | bool is_const = (var->value.type->id == TypeTableEntryIdMetaType) ? is_const_ptr : var->src_is_const; |
| 8134 | | return ir_analyze_const_ptr(ira, instruction, mem_slot, var->value.type, false, ptr_special, is_const); |
| 8184 | return ir_analyze_const_ptr(ira, instruction, mem_slot, var->value.type, |
| 8185 | mem_slot->depends_on_compile_var || depends_on_compile_var, ptr_special, is_const); |
| 8135 | 8186 | } else { |
| 8136 | 8187 | ir_build_var_ptr_from(&ira->new_irb, instruction, var, false); |
| 8137 | 8188 | type_ensure_zero_bits_known(ira->codegen, var->value.type); |
| ... | ... | @@ -8141,7 +8192,7 @@ static TypeTableEntry *ir_analyze_var_ptr(IrAnalyze *ira, IrInstruction *instruc |
| 8141 | 8192 | |
| 8142 | 8193 | static TypeTableEntry *ir_analyze_instruction_var_ptr(IrAnalyze *ira, IrInstructionVarPtr *var_ptr_instruction) { |
| 8143 | 8194 | VariableTableEntry *var = var_ptr_instruction->var; |
| 8144 | | return ir_analyze_var_ptr(ira, &var_ptr_instruction->base, var, var_ptr_instruction->is_const); |
| 8195 | return ir_analyze_var_ptr(ira, &var_ptr_instruction->base, var, var_ptr_instruction->is_const, false); |
| 8145 | 8196 | } |
| 8146 | 8197 | |
| 8147 | 8198 | static TypeTableEntry *ir_analyze_instruction_elem_ptr(IrAnalyze *ira, IrInstructionElemPtr *elem_ptr_instruction) { |
| ... | ... | @@ -8295,6 +8346,9 @@ static TypeTableEntry *ir_analyze_container_field_ptr(IrAnalyze *ira, Buf *field |
| 8295 | 8346 | ensure_complete_type(ira->codegen, bare_type); |
| 8296 | 8347 | |
| 8297 | 8348 | if (bare_type->id == TypeTableEntryIdStruct) { |
| 8349 | if (bare_type->data.structure.is_invalid) |
| 8350 | return ira->codegen->builtin_types.entry_invalid; |
| 8351 | |
| 8298 | 8352 | TypeStructField *field = find_struct_type_field(bare_type, field_name); |
| 8299 | 8353 | if (field) { |
| 8300 | 8354 | ir_build_struct_field_ptr_from(&ira->new_irb, &field_ptr_instruction->base, container_ptr, field); |
| ... | ... | @@ -8304,6 +8358,9 @@ static TypeTableEntry *ir_analyze_container_field_ptr(IrAnalyze *ira, Buf *field |
| 8304 | 8358 | field_ptr_instruction, container_ptr, container_type); |
| 8305 | 8359 | } |
| 8306 | 8360 | } else if (bare_type->id == TypeTableEntryIdEnum) { |
| 8361 | if (bare_type->data.enumeration.is_invalid) |
| 8362 | return ira->codegen->builtin_types.entry_invalid; |
| 8363 | |
| 8307 | 8364 | TypeEnumField *field = find_enum_type_field(bare_type, field_name); |
| 8308 | 8365 | if (field) { |
| 8309 | 8366 | ir_build_enum_field_ptr_from(&ira->new_irb, &field_ptr_instruction->base, container_ptr, field); |
| ... | ... | @@ -8334,7 +8391,7 @@ static TypeTableEntry *ir_analyze_decl_ref(IrAnalyze *ira, IrInstruction *source |
| 8334 | 8391 | { |
| 8335 | 8392 | TldVar *tld_var = (TldVar *)tld; |
| 8336 | 8393 | VariableTableEntry *var = tld_var->var; |
| 8337 | | return ir_analyze_var_ptr(ira, source_instruction, var, false); |
| 8394 | return ir_analyze_var_ptr(ira, source_instruction, var, false, depends_on_compile_var); |
| 8338 | 8395 | } |
| 8339 | 8396 | case TldIdFn: |
| 8340 | 8397 | { |
| ... | ... | @@ -9065,9 +9122,8 @@ static TypeTableEntry *ir_analyze_instruction_size_of(IrAnalyze *ira, |
| 9065 | 9122 | case TypeTableEntryIdEnumTag: |
| 9066 | 9123 | { |
| 9067 | 9124 | uint64_t size_in_bytes = type_size(ira->codegen, type_entry); |
| 9068 | | bool depends_on_compile_var = false; // TODO types should be able to depend on compile var |
| 9069 | 9125 | ConstExprValue *out_val = ir_build_const_from(ira, &size_of_instruction->base, |
| 9070 | | depends_on_compile_var); |
| 9126 | type_entry->size_depends_on_compile_var); |
| 9071 | 9127 | bignum_init_unsigned(&out_val->data.x_bignum, size_in_bytes); |
| 9072 | 9128 | return ira->codegen->builtin_types.entry_num_lit_int; |
| 9073 | 9129 | } |
| ... | ... | @@ -9463,6 +9519,26 @@ static TypeTableEntry *ir_analyze_instruction_static_eval(IrAnalyze *ira, |
| 9463 | 9519 | return value->value.type; |
| 9464 | 9520 | } |
| 9465 | 9521 | |
| 9522 | static TypeTableEntry *ir_analyze_instruction_generated_code(IrAnalyze *ira, IrInstructionGeneratedCode *instruction) { |
| 9523 | IrInstruction *value = instruction->value->other; |
| 9524 | if (value->value.type->id == TypeTableEntryIdInvalid) |
| 9525 | return ira->codegen->builtin_types.entry_invalid; |
| 9526 | |
| 9527 | if (instr_is_comptime(value)) { |
| 9528 | ConstExprValue *val = ir_resolve_const(ira, value, UndefOk); |
| 9529 | if (!val) |
| 9530 | return ira->codegen->builtin_types.entry_invalid; |
| 9531 | |
| 9532 | ConstExprValue *out_val = ir_build_const_from(ira, &instruction->base, false); |
| 9533 | *out_val = *val; |
| 9534 | out_val->depends_on_compile_var = true; |
| 9535 | return value->value.type; |
| 9536 | } |
| 9537 | |
| 9538 | instruction->base.other = value; |
| 9539 | return value->value.type; |
| 9540 | } |
| 9541 | |
| 9466 | 9542 | static TypeTableEntry *ir_analyze_instruction_import(IrAnalyze *ira, IrInstructionImport *import_instruction) { |
| 9467 | 9543 | IrInstruction *name_value = import_instruction->name->other; |
| 9468 | 9544 | Buf *import_target_str = ir_resolve_str(ira, name_value); |
| ... | ... | @@ -11078,6 +11154,8 @@ static TypeTableEntry *ir_analyze_instruction_nocast(IrAnalyze *ira, IrInstructi |
| 11078 | 11154 | return ir_analyze_instruction_enum_tag(ira, (IrInstructionEnumTag *)instruction); |
| 11079 | 11155 | case IrInstructionIdStaticEval: |
| 11080 | 11156 | return ir_analyze_instruction_static_eval(ira, (IrInstructionStaticEval *)instruction); |
| 11157 | case IrInstructionIdGeneratedCode: |
| 11158 | return ir_analyze_instruction_generated_code(ira, (IrInstructionGeneratedCode *)instruction); |
| 11081 | 11159 | case IrInstructionIdImport: |
| 11082 | 11160 | return ir_analyze_instruction_import(ira, (IrInstructionImport *)instruction); |
| 11083 | 11161 | case IrInstructionIdArrayLen: |
| ... | ... | @@ -11284,6 +11362,7 @@ bool ir_has_side_effects(IrInstruction *instruction) { |
| 11284 | 11362 | case IrInstructionIdSwitchTarget: |
| 11285 | 11363 | case IrInstructionIdEnumTag: |
| 11286 | 11364 | case IrInstructionIdStaticEval: |
| 11365 | case IrInstructionIdGeneratedCode: |
| 11287 | 11366 | case IrInstructionIdRef: |
| 11288 | 11367 | case IrInstructionIdMinValue: |
| 11289 | 11368 | case IrInstructionIdMaxValue: |