From 6217b401f94380a0a82aa979bc4ac90e7431267d Mon Sep 17 00:00:00 2001 From: Andrew Kelley Date: Wed, 19 Jun 2019 23:39:49 -0400 Subject: [PATCH] fix labeled break inside comptime if inside runtime if --- src/ir.cpp | 21 +++++++++++++++++++++ std/special/bootstrap.zig | 24 ++++++++++++------------ test/stage1/behavior.zig | 2 +- test/stage1/behavior/if.zig | 11 +++++++++++ test/stage1/behavior/misc.zig | 14 +++++++------- 5 files changed, 52 insertions(+), 20 deletions(-) diff --git a/src/ir.cpp b/src/ir.cpp index 9ac83443ab..32a614ad0e 100644 --- a/src/ir.cpp +++ b/src/ir.cpp @@ -10883,6 +10883,7 @@ static void ir_start_bb(IrAnalyze *ira, IrBasicBlock *old_bb, IrBasicBlock *cons ira->instruction_index = 0; ira->old_irb.current_basic_block = old_bb; ira->const_predecessor_bb = const_predecessor_bb; + ira->old_bb_index = old_bb->index; } static IrInstruction *ira_suspend(IrAnalyze *ira, IrInstruction *old_instruction, IrBasicBlock *next_bb, @@ -10894,6 +10895,14 @@ static IrInstruction *ira_suspend(IrAnalyze *ira, IrInstruction *old_instruction ira->new_irb.exec->basic_block_list.append(ira->new_irb.current_basic_block); } + //if (ira->codegen->verbose_ir) { + // fprintf(stderr, "suspend %s_%zu %s_%zu #%zu (%zu,%zu)\n", ira->old_irb.current_basic_block->name_hint, + // ira->old_irb.current_basic_block->debug_id, + // ira->old_irb.exec->basic_block_list.at(ira->old_bb_index)->name_hint, + // ira->old_irb.exec->basic_block_list.at(ira->old_bb_index)->debug_id, + // ira->old_irb.current_basic_block->instruction_list.at(ira->instruction_index)->debug_id, + // ira->old_bb_index, ira->instruction_index); + //} suspend_pos->basic_block_index = ira->old_bb_index; suspend_pos->instruction_index = ira->instruction_index; @@ -10914,10 +10923,19 @@ static IrInstruction *ira_suspend(IrAnalyze *ira, IrInstruction *old_instruction static IrInstruction *ira_resume(IrAnalyze *ira) { IrSuspendPosition pos = ira->resume_stack.pop(); + //if (ira->codegen->verbose_ir) { + // fprintf(stderr, "resume (%zu,%zu) ", pos.basic_block_index, pos.instruction_index); + //} ira->old_bb_index = pos.basic_block_index; ira->old_irb.current_basic_block = ira->old_irb.exec->basic_block_list.at(ira->old_bb_index); ira->old_irb.current_basic_block->suspended = false; ira->instruction_index = pos.instruction_index; + assert(pos.instruction_index < ira->old_irb.current_basic_block->instruction_list.length); + //if (ira->codegen->verbose_ir) { + // fprintf(stderr, "%s_%zu #%zu\n", ira->old_irb.current_basic_block->name_hint, + // ira->old_irb.current_basic_block->debug_id, + // ira->old_irb.current_basic_block->instruction_list.at(pos.instruction_index)->debug_id); + //} ira->const_predecessor_bb = nullptr; ira->new_irb.current_basic_block = ira->old_irb.current_basic_block->other; assert(ira->new_irb.current_basic_block != nullptr); @@ -24999,6 +25017,9 @@ ZigType *ir_analyze(CodeGen *codegen, IrExecutable *old_exec, IrExecutable *new_ continue; } + //if (ira->codegen->verbose_ir) { + // fprintf(stderr, "analyze #%zu\n", old_instruction->debug_id); + //} IrInstruction *new_instruction = ir_analyze_instruction_base(ira, old_instruction); if (new_instruction != nullptr) { ir_assert(new_instruction->value.type != nullptr || new_instruction->value.type != nullptr, old_instruction); diff --git a/std/special/bootstrap.zig b/std/special/bootstrap.zig index 20f2417972..f1286bd659 100644 --- a/std/special/bootstrap.zig +++ b/std/special/bootstrap.zig @@ -78,19 +78,19 @@ fn posixCallMainAndExit() noreturn { while (envp_optional[envp_count]) |_| : (envp_count += 1) {} const envp = @ptrCast([*][*]u8, envp_optional)[0..envp_count]; - //if (builtin.os == .linux) { - // // Find the beginning of the auxiliary vector - // const auxv = @ptrCast([*]std.elf.Auxv, envp.ptr + envp_count + 1); - // std.os.linux.elf_aux_maybe = auxv; - // // Initialize the TLS area - // std.os.linux.tls.initTLS(); + if (builtin.os == .linux) { + // Find the beginning of the auxiliary vector + const auxv = @ptrCast([*]std.elf.Auxv, envp.ptr + envp_count + 1); + std.os.linux.elf_aux_maybe = auxv; + // Initialize the TLS area + std.os.linux.tls.initTLS(); - // if (std.os.linux.tls.tls_image) |tls_img| { - // const tls_addr = std.os.linux.tls.allocateTLS(tls_img.alloc_size); - // const tp = std.os.linux.tls.copyTLS(tls_addr); - // std.os.linux.tls.setThreadPointer(tp); - // } - //} + if (std.os.linux.tls.tls_image) |tls_img| { + const tls_addr = std.os.linux.tls.allocateTLS(tls_img.alloc_size); + const tp = std.os.linux.tls.copyTLS(tls_addr); + std.os.linux.tls.setThreadPointer(tp); + } + } std.os.exit(callMainWithArgs(argc, argv, envp)); } diff --git a/test/stage1/behavior.zig b/test/stage1/behavior.zig index 0387f3c045..b3eeb9dd7f 100644 --- a/test/stage1/behavior.zig +++ b/test/stage1/behavior.zig @@ -62,7 +62,7 @@ comptime { _ = @import("behavior/ir_block_deps.zig"); _ = @import("behavior/math.zig"); _ = @import("behavior/merge_error_sets.zig"); - _ = @import("behavior/misc.zig"); // TODO + _ = @import("behavior/misc.zig"); _ = @import("behavior/namespace_depends_on_compile_var.zig"); _ = @import("behavior/new_stack_call.zig"); _ = @import("behavior/null.zig"); diff --git a/test/stage1/behavior/if.zig b/test/stage1/behavior/if.zig index a506a1e301..5f92962957 100644 --- a/test/stage1/behavior/if.zig +++ b/test/stage1/behavior/if.zig @@ -52,3 +52,14 @@ test "unwrap mutable global var" { expect(e == error.SomeError); } } + +test "labeled break inside comptime if inside runtime if" { + var answer: i32 = 0; + var c = true; + if (c) { + answer = if (true) blk: { + break :blk i32(42); + }; + } + expect(answer == 42); +} diff --git a/test/stage1/behavior/misc.zig b/test/stage1/behavior/misc.zig index fbc4e721e4..28df26f9fa 100644 --- a/test/stage1/behavior/misc.zig +++ b/test/stage1/behavior/misc.zig @@ -686,13 +686,13 @@ fn getNull() ?*i32 { return null; } -//test "thread local variable" { -// const S = struct { -// threadlocal var t: i32 = 1234; -// }; -// S.t += 1; -// expect(S.t == 1235); -//} +test "thread local variable" { + const S = struct { + threadlocal var t: i32 = 1234; + }; + S.t += 1; + expect(S.t == 1235); +} test "unicode escape in character literal" { var a: u24 = '\U01f4a9';