authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2019-12-01 17:09:11-05:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2019-12-01 17:09:11-05:00
logc2cee40aec5a65fa1c0d716f4a0660492717c356
tree5e48324b5a20c761b6aa021b6a320a5ed6d8bd6a
parent8524404f715ea660c4469dadb37701f7a46f85af
signaturelock-open Commit is signed but in an unrecognized format.

add advanced IR debugging support

and use it to improve copy_const_val with regards to parent backrefs

3 files changed, 86 insertions(+), 13 deletions(-)

src/all_types.hpp+2-1
......@@ -2649,8 +2649,9 @@ struct IrInstruction {
26492649 // true if this instruction was generated by zig and not from user code
26502650 bool is_gen;
26512651
2652 // for debugging purposes, this is useful to call to inspect the instruction
2652 // for debugging purposes, these are useful to call to inspect the instruction
26532653 void dump();
2654 void src();
26542655};
26552656
26562657struct IrInstructionDeclVarSrc {
src/ir.cpp+80-12
......@@ -42,6 +42,7 @@ struct IrAnalyze {
4242 ZigList<IrSuspendPosition> resume_stack;
4343 IrBasicBlock *const_predecessor_bb;
4444 size_t ref_count;
45 size_t break_debug_id; // for debugging purposes
4546
4647 // For the purpose of using in a debugger
4748 void dump();
......@@ -198,6 +199,14 @@ struct ConstCastIntShorten {
198199 ZigType *actual_type;
199200};
200201
202// for debugging purposes
203struct DbgIrBreakPoint {
204 const char *src_file;
205 uint32_t line;
206};
207DbgIrBreakPoint dbg_ir_breakpoints_buf[20];
208size_t dbg_ir_breakpoints_count = 0;
209
201210static IrInstruction *ir_gen_node(IrBuilder *irb, AstNode *node, Scope *scope);
202211static IrInstruction *ir_gen_node_extra(IrBuilder *irb, AstNode *node, Scope *scope, LVal lval,
203212 ResultLoc *result_loc);
......@@ -11355,8 +11364,12 @@ static void copy_const_val(ZigValue *dest, ZigValue *src) {
1135511364 dest->data.x_struct.fields = alloc_const_vals_ptrs(dest->type->data.structure.src_field_count);
1135611365 for (size_t i = 0; i < dest->type->data.structure.src_field_count; i += 1) {
1135711366 copy_const_val(dest->data.x_struct.fields[i], src->data.x_struct.fields[i]);
11367 dest->data.x_struct.fields[i]->parent.id = ConstParentIdStruct;
11368 dest->data.x_struct.fields[i]->parent.data.p_struct.struct_val = dest;
11369 dest->data.x_struct.fields[i]->parent.data.p_struct.field_index = i;
1135811370 }
1135911371 }
11372 dest->parent.id = ConstParentIdNone;
1136011373}
1136111374
1136211375static bool eval_const_expr_implicit_cast(IrAnalyze *ira, IrInstruction *source_instr,
......@@ -11474,6 +11487,14 @@ static IrInstruction *ir_const_noval(IrAnalyze *ira, IrInstruction *old_instruct
1147411487 return &const_instruction->base;
1147511488}
1147611489
11490// This function initializes the new IrInstruction with the provided ZigValue,
11491// rather than creating a new one.
11492static IrInstruction *ir_const_move(IrAnalyze *ira, IrInstruction *old_instruction, ZigValue *val) {
11493 IrInstruction *result = ir_const_noval(ira, old_instruction);
11494 result->value = val;
11495 return result;
11496}
11497
1147711498static IrInstruction *ir_resolve_cast(IrAnalyze *ira, IrInstruction *source_instr, IrInstruction *value,
1147811499 ZigType *wanted_type, CastOp cast_op)
1147911500{
......@@ -14216,9 +14237,7 @@ static IrInstruction *ir_analyze_instruction_return(IrAnalyze *ira, IrInstructio
1421614237}
1421714238
1421814239static IrInstruction *ir_analyze_instruction_const(IrAnalyze *ira, IrInstructionConst *instruction) {
14219 IrInstruction *result = ir_const(ira, &instruction->base, nullptr);
14220 copy_const_val(result->value, instruction->base.value);
14221 return result;
14240 return ir_const_move(ira, &instruction->base, instruction->base.value);
1422214241}
1422314242
1422414243static IrInstruction *ir_analyze_bin_op_bool(IrAnalyze *ira, IrInstructionBinOp *bin_op_instruction) {
......@@ -16633,6 +16652,7 @@ static IrInstruction *ir_resolve_result_raw(IrAnalyze *ira, IrInstruction *suspe
1663316652
1663416653 ZigType *parent_ptr_type = parent_result_loc->value->type;
1663516654 assert(parent_ptr_type->id == ZigTypeIdPointer);
16655
1663616656 if ((err = type_resolve(ira->codegen, parent_ptr_type->data.pointer.child_type,
1663716657 ResolveStatusAlignmentKnown)))
1663816658 {
......@@ -17283,6 +17303,7 @@ static IrInstruction *ir_analyze_store_ptr(IrAnalyze *ira, IrInstruction *source
1728317303 return ira->codegen->invalid_instruction;
1728417304 if (dest_val->special != ConstValSpecialRuntime) {
1728517305 copy_const_val(dest_val, value->value);
17306
1728617307 if (ptr->value->data.x_ptr.mut == ConstPtrMutComptimeVar &&
1728717308 !ira->new_irb.current_basic_block->must_be_comptime_source_instr)
1728817309 {
......@@ -17556,9 +17577,7 @@ static IrInstruction *ir_analyze_fn_call(IrAnalyze *ira, IrInstructionCallSrc *c
1755617577 }
1755717578 }
1755817579
17559 IrInstruction *new_instruction = ir_const(ira, &call_instruction->base, result->type);
17560 copy_const_val(new_instruction->value, result);
17561 new_instruction->value->type = return_type;
17580 IrInstruction *new_instruction = ir_const_move(ira, &call_instruction->base, result);
1756217581 return ir_finish_anal(ira, new_instruction);
1756317582 }
1756417583
......@@ -27978,7 +27997,24 @@ ZigType *ir_analyze(CodeGen *codegen, IrExecutable *old_exec, IrExecutable *new_
2797827997 }
2797927998
2798027999 if (ira->codegen->verbose_ir) {
27981 fprintf(stderr, "analyze #%" PRIu32 "\n", old_instruction->debug_id);
28000 fprintf(stderr, "~ ");
28001 old_instruction->src();
28002 fprintf(stderr, "~ ");
28003 ir_print_instruction(codegen, stderr, old_instruction, 0, IrPassSrc);
28004 bool want_break = false;
28005 if (ira->break_debug_id == old_instruction->debug_id) {
28006 want_break = true;
28007 } else if (old_instruction->source_node != nullptr) {
28008 for (size_t i = 0; i < dbg_ir_breakpoints_count; i += 1) {
28009 if (dbg_ir_breakpoints_buf[i].line == old_instruction->source_node->line + 1 &&
28010 buf_ends_with_str(old_instruction->source_node->owner->data.structure.root_struct->path,
28011 dbg_ir_breakpoints_buf[i].src_file))
28012 {
28013 want_break = true;
28014 }
28015 }
28016 }
28017 if (want_break) BREAKPOINT;
2798228018 }
2798328019 IrInstruction *new_instruction = ir_analyze_instruction_base(ira, old_instruction);
2798428020 if (new_instruction != nullptr) {
......@@ -27986,6 +28022,10 @@ ZigType *ir_analyze(CodeGen *codegen, IrExecutable *old_exec, IrExecutable *new_
2798628022 old_instruction->child = new_instruction;
2798728023
2798828024 if (type_is_invalid(new_instruction->value->type)) {
28025 if (ira->codegen->verbose_ir) {
28026 fprintf(stderr, "-> (invalid)");
28027 }
28028
2798928029 if (new_exec->first_err_trace_msg != nullptr) {
2799028030 ira->codegen->trace_err = new_exec->first_err_trace_msg;
2799128031 } else {
......@@ -27999,11 +28039,22 @@ ZigType *ir_analyze(CodeGen *codegen, IrExecutable *old_exec, IrExecutable *new_
2799928039 old_instruction->source_node, buf_create_from_str("referenced here"));
2800028040 }
2800128041 return ira->codegen->builtin_types.entry_invalid;
28042 } else if (ira->codegen->verbose_ir) {
28043 fprintf(stderr, "-> ");
28044 if (instr_is_unreachable(new_instruction)) {
28045 fprintf(stderr, "(noreturn)\n");
28046 } else {
28047 ir_print_instruction(codegen, stderr, new_instruction, 0, IrPassGen);
28048 }
2800228049 }
2800328050
2800428051 // unreachable instructions do their own control flow.
2800528052 if (new_instruction->value->type->id == ZigTypeIdUnreachable)
2800628053 continue;
28054 } else {
28055 if (ira->codegen->verbose_ir) {
28056 fprintf(stderr, "-> (null");
28057 }
2800728058 }
2800828059
2800928060 ira->instruction_index += 1;
......@@ -28667,18 +28718,27 @@ Error ir_resolve_lazy(CodeGen *codegen, AstNode *source_node, ZigValue *val) {
2866728718 return ErrorNone;
2866828719}
2866928720
28670void IrInstruction::dump() {
28721void IrInstruction::src() {
2867128722 IrInstruction *inst = this;
2867228723 if (inst->source_node != nullptr) {
2867328724 inst->source_node->src();
2867428725 } else {
2867528726 fprintf(stderr, "(null source node)\n");
2867628727 }
28728}
28729
28730void IrInstruction::dump() {
28731 IrInstruction *inst = this;
28732 inst->src();
2867728733 IrPass pass = (inst->child == nullptr) ? IrPassGen : IrPassSrc;
28678 ir_print_instruction(inst->scope->codegen, stderr, inst, 0, pass);
28679 if (pass == IrPassSrc) {
28680 fprintf(stderr, "-> ");
28681 ir_print_instruction(inst->scope->codegen, stderr, inst->child, 0, IrPassGen);
28734 if (inst->scope == nullptr) {
28735 fprintf(stderr, "(null scope)\n");
28736 } else {
28737 ir_print_instruction(inst->scope->codegen, stderr, inst, 0, pass);
28738 if (pass == IrPassSrc) {
28739 fprintf(stderr, "-> ");
28740 ir_print_instruction(inst->scope->codegen, stderr, inst->child, 0, IrPassGen);
28741 }
2868228742 }
2868328743}
2868428744
......@@ -28689,3 +28749,11 @@ void IrAnalyze::dump() {
2868928749 ir_print_basic_block(this->codegen, stderr, this->new_irb.current_basic_block, 1, IrPassGen);
2869028750 }
2869128751}
28752
28753void dbg_ir_break(const char *src_file, uint32_t line) {
28754 dbg_ir_breakpoints_buf[dbg_ir_breakpoints_count] = {src_file, line};
28755 dbg_ir_breakpoints_count += 1;
28756}
28757void dbg_ir_clear(void) {
28758 dbg_ir_breakpoints_count = 0;
28759}
src/ir.hpp+4
......@@ -35,4 +35,8 @@ ZigValue *const_ptr_pointee(IrAnalyze *ira, CodeGen *codegen, ZigValue *const_va
3535 AstNode *source_node);
3636const char *float_op_to_name(BuiltinFnId op, bool llvm_name);
3737
38// for debugging purposes
39void dbg_ir_break(const char *src_file, uint32_t line);
40void dbg_ir_clear(void);
41
3842#endif