authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2016-12-13 01:48:40-05:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2016-12-13 01:48:40-05:00
loga6d2bdf6050642762cc7bc193bca34690f712d49
tree10db99199b91f09b0eb908013f96e35a6a5eceae
parent76a849b1f2c1bb57de4baf8dfd49fe9f9e2340f3

IR: implement breakpoint builtin


4 files changed, 44 insertions(+), 6 deletions(-)

src/all_types.hpp+5
......@@ -1419,6 +1419,7 @@ enum IrInstructionId {
14191419 IrInstructionIdMemcpy,
14201420 IrInstructionIdSlice,
14211421 IrInstructionIdMemberCount,
1422 IrInstructionIdBreakpoint,
14221423};
14231424
14241425struct IrInstruction {
......@@ -1960,6 +1961,10 @@ struct IrInstructionMemberCount {
19601961 IrInstruction *container;
19611962};
19621963
1964struct IrInstructionBreakpoint {
1965 IrInstruction base;
1966};
1967
19631968enum LValPurpose {
19641969 LValPurposeNone,
19651970 LValPurposeAssign,
src/codegen.cpp+7
......@@ -2053,6 +2053,11 @@ static LLVMValueRef ir_render_slice(CodeGen *g, IrExecutable *executable, IrInst
20532053 }
20542054}
20552055
2056static LLVMValueRef ir_render_breakpoint(CodeGen *g, IrExecutable *executable, IrInstructionBreakpoint *instruction) {
2057 LLVMBuildCall(g->builder, g->trap_fn_val, nullptr, 0, "");
2058 return nullptr;
2059}
2060
20562061static void set_debug_location(CodeGen *g, IrInstruction *instruction) {
20572062 AstNode *source_node = instruction->source_node;
20582063 Scope *scope = instruction->scope;
......@@ -2162,6 +2167,8 @@ static LLVMValueRef ir_render_instruction(CodeGen *g, IrExecutable *executable,
21622167 return ir_render_memcpy(g, executable, (IrInstructionMemcpy *)instruction);
21632168 case IrInstructionIdSlice:
21642169 return ir_render_slice(g, executable, (IrInstructionSlice *)instruction);
2170 case IrInstructionIdBreakpoint:
2171 return ir_render_breakpoint(g, executable, (IrInstructionBreakpoint *)instruction);
21652172 case IrInstructionIdSwitchVar:
21662173 case IrInstructionIdContainerInitList:
21672174 case IrInstructionIdStructInit:
src/ir.cpp+25-6
......@@ -391,6 +391,10 @@ static constexpr IrInstructionId ir_instruction_id(IrInstructionMemberCount *) {
391391 return IrInstructionIdMemberCount;
392392}
393393
394static constexpr IrInstructionId ir_instruction_id(IrInstructionBreakpoint *) {
395 return IrInstructionIdBreakpoint;
396}
397
394398template<typename T>
395399static T *ir_create_instruction(IrExecutable *exec, Scope *scope, AstNode *source_node) {
396400 T *special_instruction = allocate<T>(1);
......@@ -1609,6 +1613,17 @@ static IrInstruction *ir_build_member_count(IrBuilder *irb, Scope *scope, AstNod
16091613 return &instruction->base;
16101614}
16111615
1616static IrInstruction *ir_build_breakpoint(IrBuilder *irb, Scope *scope, AstNode *source_node) {
1617 IrInstructionBreakpoint *instruction = ir_build_instruction<IrInstructionBreakpoint>(irb, scope, source_node);
1618 return &instruction->base;
1619}
1620
1621static IrInstruction *ir_build_breakpoint_from(IrBuilder *irb, IrInstruction *old_instruction) {
1622 IrInstruction *new_instruction = ir_build_breakpoint(irb, old_instruction->scope, old_instruction->source_node);
1623 ir_link_new_instruction(new_instruction, old_instruction);
1624 return new_instruction;
1625}
1626
16121627static void ir_gen_defers_for_block(IrBuilder *irb, Scope *inner_scope, Scope *outer_scope,
16131628 bool gen_error_defers, bool gen_maybe_defers)
16141629{
......@@ -2491,12 +2506,13 @@ static IrInstruction *ir_gen_builtin_fn_call(IrBuilder *irb, Scope *scope, AstNo
24912506
24922507 return ir_build_member_count(irb, scope, node, arg0_value);
24932508 }
2509 case BuiltinFnIdBreakpoint:
2510 return ir_build_breakpoint(irb, scope, node);
24942511 case BuiltinFnIdAlignof:
24952512 case BuiltinFnIdAddWithOverflow:
24962513 case BuiltinFnIdSubWithOverflow:
24972514 case BuiltinFnIdMulWithOverflow:
24982515 case BuiltinFnIdShlWithOverflow:
2499 case BuiltinFnIdBreakpoint:
25002516 case BuiltinFnIdReturnAddress:
25012517 case BuiltinFnIdFrameAddress:
25022518 zig_panic("TODO IR gen more builtin functions");
......@@ -8456,6 +8472,11 @@ static TypeTableEntry *ir_analyze_instruction_member_count(IrAnalyze *ira, IrIns
84568472 return ira->codegen->builtin_types.entry_num_lit_int;
84578473}
84588474
8475static TypeTableEntry *ir_analyze_instruction_breakpoint(IrAnalyze *ira, IrInstructionBreakpoint *instruction) {
8476 ir_build_breakpoint_from(&ira->new_irb, &instruction->base);
8477 return ira->codegen->builtin_types.entry_void;
8478}
8479
84598480static TypeTableEntry *ir_analyze_instruction_nocast(IrAnalyze *ira, IrInstruction *instruction) {
84608481 switch (instruction->id) {
84618482 case IrInstructionIdInvalid:
......@@ -8580,6 +8601,8 @@ static TypeTableEntry *ir_analyze_instruction_nocast(IrAnalyze *ira, IrInstructi
85808601 return ir_analyze_instruction_slice(ira, (IrInstructionSlice *)instruction);
85818602 case IrInstructionIdMemberCount:
85828603 return ir_analyze_instruction_member_count(ira, (IrInstructionMemberCount *)instruction);
8604 case IrInstructionIdBreakpoint:
8605 return ir_analyze_instruction_breakpoint(ira, (IrInstructionBreakpoint *)instruction);
85838606 case IrInstructionIdCast:
85848607 case IrInstructionIdStructFieldPtr:
85858608 case IrInstructionIdEnumFieldPtr:
......@@ -8683,6 +8706,7 @@ bool ir_has_side_effects(IrInstruction *instruction) {
86838706 case IrInstructionIdFence:
86848707 case IrInstructionIdMemset:
86858708 case IrInstructionIdMemcpy:
8709 case IrInstructionIdBreakpoint:
86868710 return true;
86878711 case IrInstructionIdPhi:
86888712 case IrInstructionIdUnOp:
......@@ -8787,9 +8811,6 @@ bool ir_has_side_effects(IrInstruction *instruction) {
87878811// align_in_bytes, false);
87888812// }
87898813// }
8790// case BuiltinFnIdBreakpoint:
8791// mark_impure_fn(g, context, node);
8792// return g->builtin_types.entry_void;
87938814// case BuiltinFnIdReturnAddress:
87948815// case BuiltinFnIdFrameAddress:
87958816// mark_impure_fn(g, context, node);
......@@ -9035,8 +9056,6 @@ bool ir_has_side_effects(IrInstruction *instruction) {
90359056// zig_unreachable();
90369057// case BuiltinFnIdCompileVar:
90379058// return nullptr;
9038// case BuiltinFnIdBreakpoint:
9039// return LLVMBuildCall(g->builder, g->trap_fn_val, nullptr, 0, "");
90409059// case BuiltinFnIdFrameAddress:
90419060// case BuiltinFnIdReturnAddress:
90429061// {
src/ir_print.cpp+7
......@@ -812,6 +812,10 @@ static void ir_print_member_count(IrPrint *irp, IrInstructionMemberCount *instru
812812 fprintf(irp->f, ")");
813813}
814814
815static void ir_print_breakpoint(IrPrint *irp, IrInstructionBreakpoint *instruction) {
816 fprintf(irp->f, "@breakpoint()");
817}
818
815819static void ir_print_instruction(IrPrint *irp, IrInstruction *instruction) {
816820 ir_print_prefix(irp, instruction);
817821 switch (instruction->id) {
......@@ -1009,6 +1013,9 @@ static void ir_print_instruction(IrPrint *irp, IrInstruction *instruction) {
10091013 case IrInstructionIdMemberCount:
10101014 ir_print_member_count(irp, (IrInstructionMemberCount *)instruction);
10111015 break;
1016 case IrInstructionIdBreakpoint:
1017 ir_print_breakpoint(irp, (IrInstructionBreakpoint *)instruction);
1018 break;
10121019 }
10131020 fprintf(irp->f, "\n");
10141021}