authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2016-12-07 11:29:44-05:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2016-12-07 11:29:44-05:00
loga148096e6a2d28e6248eed9b5a4ad40482b74149
tree5a9cf9926af7b558027bbd368124e5c4069186ed
parent0ad580f001eb151d0feb7ad884e237b237220800

IR: add compileError builtin fn


4 files changed, 54 insertions(+), 55 deletions(-)

src/all_types.hpp+7-1
......@@ -1041,7 +1041,6 @@ enum BuiltinFnId {
10411041 BuiltinFnIdUnreachable,
10421042 BuiltinFnIdSetFnTest,
10431043 BuiltinFnIdSetFnVisible,
1044 BuiltinFnIdSetFnNoInline,
10451044 BuiltinFnIdSetDebugSafety,
10461045};
10471046
......@@ -1395,6 +1394,7 @@ enum IrInstructionId {
13951394 IrInstructionIdRef,
13961395 IrInstructionIdMinValue,
13971396 IrInstructionIdMaxValue,
1397 IrInstructionIdCompileErr,
13981398};
13991399
14001400struct IrInstruction {
......@@ -1805,6 +1805,12 @@ struct IrInstructionMaxValue {
18051805 IrInstruction *value;
18061806};
18071807
1808struct IrInstructionCompileErr {
1809 IrInstruction base;
1810
1811 IrInstruction *msg;
1812};
1813
18081814enum LValPurpose {
18091815 LValPurposeNone,
18101816 LValPurposeAssign,
src/codegen.cpp+1-1
......@@ -1823,6 +1823,7 @@ static LLVMValueRef ir_render_instruction(CodeGen *g, IrExecutable *executable,
18231823 case IrInstructionIdContainerInitFields:
18241824 case IrInstructionIdMinValue:
18251825 case IrInstructionIdMaxValue:
1826 case IrInstructionIdCompileErr:
18261827 zig_unreachable();
18271828 case IrInstructionIdReturn:
18281829 return ir_render_return(g, executable, (IrInstructionReturn *)instruction);
......@@ -3068,7 +3069,6 @@ static void define_builtin_fns(CodeGen *g) {
30683069 create_builtin_fn(g, BuiltinFnIdUnreachable, "unreachable", 0);
30693070 create_builtin_fn(g, BuiltinFnIdSetFnTest, "setFnTest", 1);
30703071 create_builtin_fn(g, BuiltinFnIdSetFnVisible, "setFnVisible", 2);
3071 create_builtin_fn(g, BuiltinFnIdSetFnNoInline, "setFnNoInline", 2);
30723072 create_builtin_fn(g, BuiltinFnIdSetDebugSafety, "setDebugSafety", 2);
30733073}
30743074
src/ir.cpp+37-53
......@@ -314,6 +314,10 @@ static constexpr IrInstructionId ir_instruction_id(IrInstructionMaxValue *) {
314314 return IrInstructionIdMaxValue;
315315}
316316
317static constexpr IrInstructionId ir_instruction_id(IrInstructionCompileErr *) {
318 return IrInstructionIdCompileErr;
319}
320
317321template<typename T>
318322static T *ir_create_instruction(IrExecutable *exec, Scope *scope, AstNode *source_node) {
319323 T *special_instruction = allocate<T>(1);
......@@ -1267,6 +1271,15 @@ static IrInstruction *ir_build_max_value(IrBuilder *irb, Scope *scope, AstNode *
12671271 return &instruction->base;
12681272}
12691273
1274static IrInstruction *ir_build_compile_err(IrBuilder *irb, Scope *scope, AstNode *source_node, IrInstruction *msg) {
1275 IrInstructionCompileErr *instruction = ir_build_instruction<IrInstructionCompileErr>(irb, scope, source_node);
1276 instruction->msg = msg;
1277
1278 ir_ref_instruction(msg);
1279
1280 return &instruction->base;
1281}
1282
12701283
12711284static void ir_gen_defers_for_block(IrBuilder *irb, Scope *inner_scope, Scope *outer_scope,
12721285 bool gen_error_defers, bool gen_maybe_defers)
......@@ -1924,6 +1937,15 @@ static IrInstruction *ir_gen_builtin_fn_call(IrBuilder *irb, Scope *scope, AstNo
19241937
19251938 return ir_build_min_value(irb, scope, node, arg0_value);
19261939 }
1940 case BuiltinFnIdCompileErr:
1941 {
1942 AstNode *arg0_node = node->data.fn_call_expr.params.at(0);
1943 IrInstruction *arg0_value = ir_gen_node(irb, arg0_node, scope);
1944 if (arg0_value == irb->codegen->invalid_instruction)
1945 return arg0_value;
1946
1947 return ir_build_compile_err(irb, scope, node, arg0_value);
1948 }
19271949 case BuiltinFnIdMemcpy:
19281950 case BuiltinFnIdMemset:
19291951 case BuiltinFnIdAlignof:
......@@ -1935,7 +1957,6 @@ static IrInstruction *ir_gen_builtin_fn_call(IrBuilder *irb, Scope *scope, AstNo
19351957 case BuiltinFnIdCInclude:
19361958 case BuiltinFnIdCDefine:
19371959 case BuiltinFnIdCUndef:
1938 case BuiltinFnIdCompileErr:
19391960 case BuiltinFnIdCImport:
19401961 case BuiltinFnIdErrName:
19411962 case BuiltinFnIdBreakpoint:
......@@ -1947,7 +1968,6 @@ static IrInstruction *ir_gen_builtin_fn_call(IrBuilder *irb, Scope *scope, AstNo
19471968 case BuiltinFnIdDivExact:
19481969 case BuiltinFnIdTruncate:
19491970 case BuiltinFnIdIntType:
1950 case BuiltinFnIdSetFnNoInline:
19511971 zig_panic("TODO IR gen more builtin functions");
19521972 }
19531973 zig_unreachable();
......@@ -6782,6 +6802,18 @@ static TypeTableEntry *ir_analyze_instruction_max_value(IrAnalyze *ira,
67826802 return ir_analyze_min_max(ira, &instruction->base, instruction->value->other, true);
67836803}
67846804
6805static TypeTableEntry *ir_analyze_instruction_compile_err(IrAnalyze *ira,
6806 IrInstructionCompileErr *instruction)
6807{
6808 IrInstruction *msg_value = instruction->msg->other;
6809 Buf *msg_buf = ir_resolve_str(ira, msg_value);
6810 if (!msg_buf)
6811 return ira->codegen->builtin_types.entry_invalid;
6812
6813 ir_add_error(ira, &instruction->base, msg_buf);
6814 return ira->codegen->builtin_types.entry_invalid;
6815}
6816
67856817static TypeTableEntry *ir_analyze_instruction_nocast(IrAnalyze *ira, IrInstruction *instruction) {
67866818 switch (instruction->id) {
67876819 case IrInstructionIdInvalid:
......@@ -6870,6 +6902,8 @@ static TypeTableEntry *ir_analyze_instruction_nocast(IrAnalyze *ira, IrInstructi
68706902 return ir_analyze_instruction_min_value(ira, (IrInstructionMinValue *)instruction);
68716903 case IrInstructionIdMaxValue:
68726904 return ir_analyze_instruction_max_value(ira, (IrInstructionMaxValue *)instruction);
6905 case IrInstructionIdCompileErr:
6906 return ir_analyze_instruction_compile_err(ira, (IrInstructionCompileErr *)instruction);
68736907 case IrInstructionIdCast:
68746908 case IrInstructionIdStructFieldPtr:
68756909 case IrInstructionIdEnumFieldPtr:
......@@ -6964,6 +6998,7 @@ bool ir_has_side_effects(IrInstruction *instruction) {
69646998 case IrInstructionIdSetFnVisible:
69656999 case IrInstructionIdSetDebugSafety:
69667000 case IrInstructionIdImport:
7001 case IrInstructionIdCompileErr:
69677002 return true;
69687003 case IrInstructionIdPhi:
69697004 case IrInstructionIdUnOp:
......@@ -7291,20 +7326,6 @@ bool ir_has_side_effects(IrInstruction *instruction) {
72917326// return dest_type;
72927327//}
72937328//
7294//static TypeTableEntry *analyze_compile_err(CodeGen *g, ImportTableEntry *import,
7295// BlockContext *context, AstNode *node)
7296//{
7297// AstNode *first_param_node = node->data.fn_call_expr.params.at(0);
7298// Buf *err_msg = resolve_const_expr_str(g, import, context, first_param_node->parent_field);
7299// if (!err_msg) {
7300// return g->builtin_types.entry_invalid;
7301// }
7302//
7303// add_node_error(g, node, err_msg);
7304//
7305// return g->builtin_types.entry_invalid;
7306//}
7307//
73087329//static TypeTableEntry *analyze_int_type(CodeGen *g, ImportTableEntry *import,
73097330// BlockContext *context, AstNode *node)
73107331//{
......@@ -7344,41 +7365,6 @@ bool ir_has_side_effects(IrInstruction *instruction) {
73447365//
73457366//}
73467367//
7347//static TypeTableEntry *analyze_set_fn_no_inline(CodeGen *g, ImportTableEntry *import,
7348// BlockContext *context, AstNode *node)
7349//{
7350// AstNode **fn_node = &node->data.fn_call_expr.params.at(0);
7351// AstNode **value_node = &node->data.fn_call_expr.params.at(1);
7352//
7353// FnTableEntry *fn_entry = resolve_const_expr_fn(g, import, context, fn_node);
7354// if (!fn_entry) {
7355// return g->builtin_types.entry_invalid;
7356// }
7357//
7358// bool is_noinline;
7359// bool ok = resolve_const_expr_bool(g, import, context, value_node, &is_noinline);
7360// if (!ok) {
7361// return g->builtin_types.entry_invalid;
7362// }
7363//
7364// if (fn_entry->fn_no_inline_set_node) {
7365// ErrorMsg *msg = add_node_error(g, node, buf_sprintf("function no inline attribute set twice"));
7366// add_error_note(g, msg, fn_entry->fn_no_inline_set_node, buf_sprintf("first set here"));
7367// return g->builtin_types.entry_invalid;
7368// }
7369// fn_entry->fn_no_inline_set_node = node;
7370//
7371// if (fn_entry->fn_inline == FnInlineAlways) {
7372// add_node_error(g, node, buf_sprintf("function is both inline and noinline"));
7373// fn_entry->proto_node->data.fn_proto.skip = true;
7374// return g->builtin_types.entry_invalid;
7375// } else if (is_noinline) {
7376// fn_entry->fn_inline = FnInlineNever;
7377// }
7378//
7379// return g->builtin_types.entry_void;
7380//}
7381//
73827368//static TypeTableEntry *analyze_builtin_fn_call_expr(CodeGen *g, ImportTableEntry *import, BlockContext *context,
73837369// TypeTableEntry *expected_type, AstNode *node)
73847370//{
......@@ -7566,8 +7552,6 @@ bool ir_has_side_effects(IrInstruction *instruction) {
75667552// return analyze_div_exact(g, import, context, node);
75677553// case BuiltinFnIdTruncate:
75687554// return analyze_truncate(g, import, context, node);
7569// case BuiltinFnIdCompileErr:
7570// return analyze_compile_err(g, import, context, node);
75717555// case BuiltinFnIdIntType:
75727556// return analyze_int_type(g, import, context, node);
75737557// case BuiltinFnIdSetFnTest:
src/ir_print.cpp+9
......@@ -670,6 +670,12 @@ static void ir_print_max_value(IrPrint *irp, IrInstructionMaxValue *instruction)
670670 fprintf(irp->f, ")");
671671}
672672
673static void ir_print_compile_err(IrPrint *irp, IrInstructionCompileErr *instruction) {
674 fprintf(irp->f, "@compileError(");
675 ir_print_other_instruction(irp, instruction->msg);
676 fprintf(irp->f, ")");
677}
678
673679static void ir_print_instruction(IrPrint *irp, IrInstruction *instruction) {
674680 ir_print_prefix(irp, instruction);
675681 switch (instruction->id) {
......@@ -813,6 +819,9 @@ static void ir_print_instruction(IrPrint *irp, IrInstruction *instruction) {
813819 case IrInstructionIdMaxValue:
814820 ir_print_max_value(irp, (IrInstructionMaxValue *)instruction);
815821 break;
822 case IrInstructionIdCompileErr:
823 ir_print_compile_err(irp, (IrInstructionCompileErr *)instruction);
824 break;
816825 }
817826 fprintf(irp->f, "\n");
818827}