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 {...@@ -1041,7 +1041,6 @@ enum BuiltinFnId {
1041 BuiltinFnIdUnreachable,1041 BuiltinFnIdUnreachable,
1042 BuiltinFnIdSetFnTest,1042 BuiltinFnIdSetFnTest,
1043 BuiltinFnIdSetFnVisible,1043 BuiltinFnIdSetFnVisible,
1044 BuiltinFnIdSetFnNoInline,
1045 BuiltinFnIdSetDebugSafety,1044 BuiltinFnIdSetDebugSafety,
1046};1045};
10471046
...@@ -1395,6 +1394,7 @@ enum IrInstructionId {...@@ -1395,6 +1394,7 @@ enum IrInstructionId {
1395 IrInstructionIdRef,1394 IrInstructionIdRef,
1396 IrInstructionIdMinValue,1395 IrInstructionIdMinValue,
1397 IrInstructionIdMaxValue,1396 IrInstructionIdMaxValue,
1397 IrInstructionIdCompileErr,
1398};1398};
13991399
1400struct IrInstruction {1400struct IrInstruction {
...@@ -1805,6 +1805,12 @@ struct IrInstructionMaxValue {...@@ -1805,6 +1805,12 @@ struct IrInstructionMaxValue {
1805 IrInstruction *value;1805 IrInstruction *value;
1806};1806};
18071807
1808struct IrInstructionCompileErr {
1809 IrInstruction base;
1810
1811 IrInstruction *msg;
1812};
1813
1808enum LValPurpose {1814enum LValPurpose {
1809 LValPurposeNone,1815 LValPurposeNone,
1810 LValPurposeAssign,1816 LValPurposeAssign,
src/codegen.cpp+1-1
...@@ -1823,6 +1823,7 @@ static LLVMValueRef ir_render_instruction(CodeGen *g, IrExecutable *executable,...@@ -1823,6 +1823,7 @@ static LLVMValueRef ir_render_instruction(CodeGen *g, IrExecutable *executable,
1823 case IrInstructionIdContainerInitFields:1823 case IrInstructionIdContainerInitFields:
1824 case IrInstructionIdMinValue:1824 case IrInstructionIdMinValue:
1825 case IrInstructionIdMaxValue:1825 case IrInstructionIdMaxValue:
1826 case IrInstructionIdCompileErr:
1826 zig_unreachable();1827 zig_unreachable();
1827 case IrInstructionIdReturn:1828 case IrInstructionIdReturn:
1828 return ir_render_return(g, executable, (IrInstructionReturn *)instruction);1829 return ir_render_return(g, executable, (IrInstructionReturn *)instruction);
...@@ -3068,7 +3069,6 @@ static void define_builtin_fns(CodeGen *g) {...@@ -3068,7 +3069,6 @@ static void define_builtin_fns(CodeGen *g) {
3068 create_builtin_fn(g, BuiltinFnIdUnreachable, "unreachable", 0);3069 create_builtin_fn(g, BuiltinFnIdUnreachable, "unreachable", 0);
3069 create_builtin_fn(g, BuiltinFnIdSetFnTest, "setFnTest", 1);3070 create_builtin_fn(g, BuiltinFnIdSetFnTest, "setFnTest", 1);
3070 create_builtin_fn(g, BuiltinFnIdSetFnVisible, "setFnVisible", 2);3071 create_builtin_fn(g, BuiltinFnIdSetFnVisible, "setFnVisible", 2);
3071 create_builtin_fn(g, BuiltinFnIdSetFnNoInline, "setFnNoInline", 2);
3072 create_builtin_fn(g, BuiltinFnIdSetDebugSafety, "setDebugSafety", 2);3072 create_builtin_fn(g, BuiltinFnIdSetDebugSafety, "setDebugSafety", 2);
3073}3073}
30743074
src/ir.cpp+37-53
...@@ -314,6 +314,10 @@ static constexpr IrInstructionId ir_instruction_id(IrInstructionMaxValue *) {...@@ -314,6 +314,10 @@ static constexpr IrInstructionId ir_instruction_id(IrInstructionMaxValue *) {
314 return IrInstructionIdMaxValue;314 return IrInstructionIdMaxValue;
315}315}
316316
317static constexpr IrInstructionId ir_instruction_id(IrInstructionCompileErr *) {
318 return IrInstructionIdCompileErr;
319}
320
317template<typename T>321template<typename T>
318static T *ir_create_instruction(IrExecutable *exec, Scope *scope, AstNode *source_node) {322static T *ir_create_instruction(IrExecutable *exec, Scope *scope, AstNode *source_node) {
319 T *special_instruction = allocate<T>(1);323 T *special_instruction = allocate<T>(1);
...@@ -1267,6 +1271,15 @@ static IrInstruction *ir_build_max_value(IrBuilder *irb, Scope *scope, AstNode *...@@ -1267,6 +1271,15 @@ static IrInstruction *ir_build_max_value(IrBuilder *irb, Scope *scope, AstNode *
1267 return &instruction->base;1271 return &instruction->base;
1268}1272}
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
1271static void ir_gen_defers_for_block(IrBuilder *irb, Scope *inner_scope, Scope *outer_scope,1284static void ir_gen_defers_for_block(IrBuilder *irb, Scope *inner_scope, Scope *outer_scope,
1272 bool gen_error_defers, bool gen_maybe_defers)1285 bool gen_error_defers, bool gen_maybe_defers)
...@@ -1924,6 +1937,15 @@ static IrInstruction *ir_gen_builtin_fn_call(IrBuilder *irb, Scope *scope, AstNo...@@ -1924,6 +1937,15 @@ static IrInstruction *ir_gen_builtin_fn_call(IrBuilder *irb, Scope *scope, AstNo
19241937
1925 return ir_build_min_value(irb, scope, node, arg0_value);1938 return ir_build_min_value(irb, scope, node, arg0_value);
1926 }1939 }
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 }
1927 case BuiltinFnIdMemcpy:1949 case BuiltinFnIdMemcpy:
1928 case BuiltinFnIdMemset:1950 case BuiltinFnIdMemset:
1929 case BuiltinFnIdAlignof:1951 case BuiltinFnIdAlignof:
...@@ -1935,7 +1957,6 @@ static IrInstruction *ir_gen_builtin_fn_call(IrBuilder *irb, Scope *scope, AstNo...@@ -1935,7 +1957,6 @@ static IrInstruction *ir_gen_builtin_fn_call(IrBuilder *irb, Scope *scope, AstNo
1935 case BuiltinFnIdCInclude:1957 case BuiltinFnIdCInclude:
1936 case BuiltinFnIdCDefine:1958 case BuiltinFnIdCDefine:
1937 case BuiltinFnIdCUndef:1959 case BuiltinFnIdCUndef:
1938 case BuiltinFnIdCompileErr:
1939 case BuiltinFnIdCImport:1960 case BuiltinFnIdCImport:
1940 case BuiltinFnIdErrName:1961 case BuiltinFnIdErrName:
1941 case BuiltinFnIdBreakpoint:1962 case BuiltinFnIdBreakpoint:
...@@ -1947,7 +1968,6 @@ static IrInstruction *ir_gen_builtin_fn_call(IrBuilder *irb, Scope *scope, AstNo...@@ -1947,7 +1968,6 @@ static IrInstruction *ir_gen_builtin_fn_call(IrBuilder *irb, Scope *scope, AstNo
1947 case BuiltinFnIdDivExact:1968 case BuiltinFnIdDivExact:
1948 case BuiltinFnIdTruncate:1969 case BuiltinFnIdTruncate:
1949 case BuiltinFnIdIntType:1970 case BuiltinFnIdIntType:
1950 case BuiltinFnIdSetFnNoInline:
1951 zig_panic("TODO IR gen more builtin functions");1971 zig_panic("TODO IR gen more builtin functions");
1952 }1972 }
1953 zig_unreachable();1973 zig_unreachable();
...@@ -6782,6 +6802,18 @@ static TypeTableEntry *ir_analyze_instruction_max_value(IrAnalyze *ira,...@@ -6782,6 +6802,18 @@ static TypeTableEntry *ir_analyze_instruction_max_value(IrAnalyze *ira,
6782 return ir_analyze_min_max(ira, &instruction->base, instruction->value->other, true);6802 return ir_analyze_min_max(ira, &instruction->base, instruction->value->other, true);
6783}6803}
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
6785static TypeTableEntry *ir_analyze_instruction_nocast(IrAnalyze *ira, IrInstruction *instruction) {6817static TypeTableEntry *ir_analyze_instruction_nocast(IrAnalyze *ira, IrInstruction *instruction) {
6786 switch (instruction->id) {6818 switch (instruction->id) {
6787 case IrInstructionIdInvalid:6819 case IrInstructionIdInvalid:
...@@ -6870,6 +6902,8 @@ static TypeTableEntry *ir_analyze_instruction_nocast(IrAnalyze *ira, IrInstructi...@@ -6870,6 +6902,8 @@ static TypeTableEntry *ir_analyze_instruction_nocast(IrAnalyze *ira, IrInstructi
6870 return ir_analyze_instruction_min_value(ira, (IrInstructionMinValue *)instruction);6902 return ir_analyze_instruction_min_value(ira, (IrInstructionMinValue *)instruction);
6871 case IrInstructionIdMaxValue:6903 case IrInstructionIdMaxValue:
6872 return ir_analyze_instruction_max_value(ira, (IrInstructionMaxValue *)instruction);6904 return ir_analyze_instruction_max_value(ira, (IrInstructionMaxValue *)instruction);
6905 case IrInstructionIdCompileErr:
6906 return ir_analyze_instruction_compile_err(ira, (IrInstructionCompileErr *)instruction);
6873 case IrInstructionIdCast:6907 case IrInstructionIdCast:
6874 case IrInstructionIdStructFieldPtr:6908 case IrInstructionIdStructFieldPtr:
6875 case IrInstructionIdEnumFieldPtr:6909 case IrInstructionIdEnumFieldPtr:
...@@ -6964,6 +6998,7 @@ bool ir_has_side_effects(IrInstruction *instruction) {...@@ -6964,6 +6998,7 @@ bool ir_has_side_effects(IrInstruction *instruction) {
6964 case IrInstructionIdSetFnVisible:6998 case IrInstructionIdSetFnVisible:
6965 case IrInstructionIdSetDebugSafety:6999 case IrInstructionIdSetDebugSafety:
6966 case IrInstructionIdImport:7000 case IrInstructionIdImport:
7001 case IrInstructionIdCompileErr:
6967 return true;7002 return true;
6968 case IrInstructionIdPhi:7003 case IrInstructionIdPhi:
6969 case IrInstructionIdUnOp:7004 case IrInstructionIdUnOp:
...@@ -7291,20 +7326,6 @@ bool ir_has_side_effects(IrInstruction *instruction) {...@@ -7291,20 +7326,6 @@ bool ir_has_side_effects(IrInstruction *instruction) {
7291// return dest_type;7326// return dest_type;
7292//}7327//}
7293//7328//
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//
7308//static TypeTableEntry *analyze_int_type(CodeGen *g, ImportTableEntry *import,7329//static TypeTableEntry *analyze_int_type(CodeGen *g, ImportTableEntry *import,
7309// BlockContext *context, AstNode *node)7330// BlockContext *context, AstNode *node)
7310//{7331//{
...@@ -7344,41 +7365,6 @@ bool ir_has_side_effects(IrInstruction *instruction) {...@@ -7344,41 +7365,6 @@ bool ir_has_side_effects(IrInstruction *instruction) {
7344//7365//
7345//}7366//}
7346//7367//
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//
7382//static TypeTableEntry *analyze_builtin_fn_call_expr(CodeGen *g, ImportTableEntry *import, BlockContext *context,7368//static TypeTableEntry *analyze_builtin_fn_call_expr(CodeGen *g, ImportTableEntry *import, BlockContext *context,
7383// TypeTableEntry *expected_type, AstNode *node)7369// TypeTableEntry *expected_type, AstNode *node)
7384//{7370//{
...@@ -7566,8 +7552,6 @@ bool ir_has_side_effects(IrInstruction *instruction) {...@@ -7566,8 +7552,6 @@ bool ir_has_side_effects(IrInstruction *instruction) {
7566// return analyze_div_exact(g, import, context, node);7552// return analyze_div_exact(g, import, context, node);
7567// case BuiltinFnIdTruncate:7553// case BuiltinFnIdTruncate:
7568// return analyze_truncate(g, import, context, node);7554// return analyze_truncate(g, import, context, node);
7569// case BuiltinFnIdCompileErr:
7570// return analyze_compile_err(g, import, context, node);
7571// case BuiltinFnIdIntType:7555// case BuiltinFnIdIntType:
7572// return analyze_int_type(g, import, context, node);7556// return analyze_int_type(g, import, context, node);
7573// case BuiltinFnIdSetFnTest:7557// case BuiltinFnIdSetFnTest:
src/ir_print.cpp+9
...@@ -670,6 +670,12 @@ static void ir_print_max_value(IrPrint *irp, IrInstructionMaxValue *instruction)...@@ -670,6 +670,12 @@ static void ir_print_max_value(IrPrint *irp, IrInstructionMaxValue *instruction)
670 fprintf(irp->f, ")");670 fprintf(irp->f, ")");
671}671}
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
673static void ir_print_instruction(IrPrint *irp, IrInstruction *instruction) {679static void ir_print_instruction(IrPrint *irp, IrInstruction *instruction) {
674 ir_print_prefix(irp, instruction);680 ir_print_prefix(irp, instruction);
675 switch (instruction->id) {681 switch (instruction->id) {
...@@ -813,6 +819,9 @@ static void ir_print_instruction(IrPrint *irp, IrInstruction *instruction) {...@@ -813,6 +819,9 @@ static void ir_print_instruction(IrPrint *irp, IrInstruction *instruction) {
813 case IrInstructionIdMaxValue:819 case IrInstructionIdMaxValue:
814 ir_print_max_value(irp, (IrInstructionMaxValue *)instruction);820 ir_print_max_value(irp, (IrInstructionMaxValue *)instruction);
815 break;821 break;
822 case IrInstructionIdCompileErr:
823 ir_print_compile_err(irp, (IrInstructionCompileErr *)instruction);
824 break;
816 }825 }
817 fprintf(irp->f, "\n");826 fprintf(irp->f, "\n");
818}827}