authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2016-12-08 01:52:57-05:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2016-12-08 01:52:57-05:00
log7d0fb281fee16d9c99f61c5bce090018228ae6df
tree4fe6b845e03cb6b19e399283f5501d444cf2185a
parenta148096e6a2d28e6248eed9b5a4ad40482b74149

IR: a bunch of fixes and some additions

* add errorName builtin function * add assertion for generated memcopy being on correct types * respect handle_is_ptr for constant values * fix return codegen to respect sret semantics * remove ArrayLen IR instruction; we already have StructFieldPtr with "len" field * fix gen_const_val for pointers inside aggregates

6 files changed, 203 insertions(+), 95 deletions(-)

src/all_types.hpp+9-1
......@@ -128,7 +128,8 @@ struct ConstExprValue {
128128 ConstBoundFnValue x_bound_fn;
129129 TypeTableEntry *x_type;
130130 ConstExprValue *x_maybe;
131 ConstErrValue x_err;
131 ConstErrValue x_err_union;
132 ErrorTableEntry *x_pure_err;
132133 ConstEnumValue x_enum;
133134 ConstStructValue x_struct;
134135 ConstArrayValue x_array;
......@@ -1395,6 +1396,7 @@ enum IrInstructionId {
13951396 IrInstructionIdMinValue,
13961397 IrInstructionIdMaxValue,
13971398 IrInstructionIdCompileErr,
1399 IrInstructionIdErrName,
13981400};
13991401
14001402struct IrInstruction {
......@@ -1811,6 +1813,12 @@ struct IrInstructionCompileErr {
18111813 IrInstruction *msg;
18121814};
18131815
1816struct IrInstructionErrName {
1817 IrInstruction base;
1818
1819 IrInstruction *value;
1820};
1821
18141822enum LValPurpose {
18151823 LValPurposeNone,
18161824 LValPurposeAssign,
src/codegen.cpp+61-10
......@@ -615,6 +615,9 @@ static LLVMValueRef gen_struct_memcpy(CodeGen *g, LLVMValueRef src, LLVMValueRef
615615{
616616 assert(handle_is_ptr(type_entry));
617617
618 assert(LLVMGetTypeKind(LLVMTypeOf(src)) == LLVMPointerTypeKind);
619 assert(LLVMGetTypeKind(LLVMTypeOf(dest)) == LLVMPointerTypeKind);
620
618621 LLVMTypeRef ptr_u8 = LLVMPointerType(LLVMInt8Type(), 0);
619622
620623 LLVMValueRef src_ptr = LLVMBuildBitCast(g->builder, src, ptr_u8, "");
......@@ -669,7 +672,12 @@ static LLVMValueRef ir_llvm_value(CodeGen *g, IrInstruction *instruction) {
669672 assert(instruction->static_value.special != ConstValSpecialRuntime);
670673 assert(instruction->type_entry);
671674 render_const_val(g, instruction->type_entry, &instruction->static_value);
672 instruction->llvm_value = instruction->static_value.llvm_value;
675 if (handle_is_ptr(instruction->type_entry)) {
676 render_const_val_global(g, instruction->type_entry, &instruction->static_value);
677 instruction->llvm_value = instruction->static_value.llvm_global;
678 } else {
679 instruction->llvm_value = instruction->static_value.llvm_value;
680 }
673681 assert(instruction->llvm_value);
674682 }
675683 if (instruction->static_value.special != ConstValSpecialRuntime) {
......@@ -681,7 +689,22 @@ static LLVMValueRef ir_llvm_value(CodeGen *g, IrInstruction *instruction) {
681689}
682690
683691static LLVMValueRef ir_render_return(CodeGen *g, IrExecutable *executable, IrInstructionReturn *return_instruction) {
684 LLVMBuildRet(g->builder, ir_llvm_value(g, return_instruction->value));
692 LLVMValueRef value = ir_llvm_value(g, return_instruction->value);
693 TypeTableEntry *return_type = g->cur_fn->type_entry->data.fn.fn_type_id.return_type;
694 bool is_extern = g->cur_fn->type_entry->data.fn.fn_type_id.is_extern;
695 if (handle_is_ptr(return_type)) {
696 if (is_extern) {
697 LLVMValueRef by_val_value = LLVMBuildLoad(g->builder, value, "");
698 LLVMBuildRet(g->builder, by_val_value);
699 } else {
700 assert(g->cur_ret_ptr);
701 gen_assign_raw(g, return_instruction->base.source_node, g->cur_ret_ptr, value,
702 return_type, return_instruction->value->type_entry);
703 LLVMBuildRetVoid(g->builder);
704 }
705 } else {
706 LLVMBuildRet(g->builder, value);
707 }
685708 return nullptr;
686709}
687710
......@@ -1790,6 +1813,28 @@ static LLVMValueRef ir_render_ref(CodeGen *g, IrExecutable *executable, IrInstru
17901813 }
17911814}
17921815
1816static LLVMValueRef ir_render_err_name(CodeGen *g, IrExecutable *executable, IrInstructionErrName *instruction) {
1817 assert(g->generate_error_name_table);
1818
1819 if (g->error_decls.length == 1) {
1820 LLVMBuildUnreachable(g->builder);
1821 return nullptr;
1822 }
1823
1824 LLVMValueRef err_val = ir_llvm_value(g, instruction->value);
1825 if (ir_want_debug_safety(g, &instruction->base)) {
1826 LLVMValueRef zero = LLVMConstNull(LLVMTypeOf(err_val));
1827 LLVMValueRef end_val = LLVMConstInt(LLVMTypeOf(err_val), g->error_decls.length, false);
1828 add_bounds_check(g, err_val, LLVMIntNE, zero, LLVMIntULT, end_val);
1829 }
1830
1831 LLVMValueRef indices[] = {
1832 LLVMConstNull(g->builtin_types.entry_usize->type_ref),
1833 err_val,
1834 };
1835 return LLVMBuildInBoundsGEP(g->builder, g->err_name_table, indices, 2, "");
1836}
1837
17931838static void set_debug_location(CodeGen *g, IrInstruction *instruction) {
17941839 AstNode *source_node = instruction->source_node;
17951840 Scope *scope = instruction->scope;
......@@ -1824,6 +1869,7 @@ static LLVMValueRef ir_render_instruction(CodeGen *g, IrExecutable *executable,
18241869 case IrInstructionIdMinValue:
18251870 case IrInstructionIdMaxValue:
18261871 case IrInstructionIdCompileErr:
1872 case IrInstructionIdArrayLen:
18271873 zig_unreachable();
18281874 case IrInstructionIdReturn:
18291875 return ir_render_return(g, executable, (IrInstructionReturn *)instruction);
......@@ -1871,11 +1917,12 @@ static LLVMValueRef ir_render_instruction(CodeGen *g, IrExecutable *executable,
18711917 return ir_render_phi(g, executable, (IrInstructionPhi *)instruction);
18721918 case IrInstructionIdRef:
18731919 return ir_render_ref(g, executable, (IrInstructionRef *)instruction);
1920 case IrInstructionIdErrName:
1921 return ir_render_err_name(g, executable, (IrInstructionErrName *)instruction);
18741922 case IrInstructionIdSwitchVar:
18751923 case IrInstructionIdContainerInitList:
18761924 case IrInstructionIdStructInit:
18771925 case IrInstructionIdEnumTag:
1878 case IrInstructionIdArrayLen:
18791926 zig_panic("TODO render more IR instructions to LLVM");
18801927 }
18811928 zig_unreachable();
......@@ -1919,9 +1966,9 @@ static LLVMValueRef gen_const_val(CodeGen *g, TypeTableEntry *type_entry, ConstE
19191966 case TypeTableEntryIdInt:
19201967 return LLVMConstInt(type_entry->type_ref, bignum_to_twos_complement(&const_val->data.x_bignum), false);
19211968 case TypeTableEntryIdPureError:
1922 assert(const_val->data.x_err.err);
1969 assert(const_val->data.x_pure_err);
19231970 return LLVMConstInt(g->builtin_types.entry_pure_error->type_ref,
1924 const_val->data.x_err.err->value, false);
1971 const_val->data.x_pure_err->value, false);
19251972 case TypeTableEntryIdFloat:
19261973 if (const_val->data.x_bignum.kind == BigNumKindFloat) {
19271974 return LLVMConstReal(type_entry->type_ref, const_val->data.x_bignum.data.x_float);
......@@ -2045,7 +2092,9 @@ static LLVMValueRef gen_const_val(CodeGen *g, TypeTableEntry *type_entry, ConstE
20452092 if (index == SIZE_MAX) {
20462093 render_const_val(g, child_type, const_val->data.x_ptr.base_ptr);
20472094 render_const_val_global(g, child_type, const_val->data.x_ptr.base_ptr);
2048 return const_val->data.x_ptr.base_ptr->llvm_global;
2095 const_val->llvm_value = const_val->data.x_ptr.base_ptr->llvm_global;
2096 render_const_val_global(g, type_entry, const_val);
2097 return const_val->llvm_value;
20492098 } else {
20502099 ConstExprValue *array_const_val = const_val->data.x_ptr.base_ptr;
20512100 TypeTableEntry *array_type = get_array_type(g, child_type,
......@@ -2058,6 +2107,8 @@ static LLVMValueRef gen_const_val(CodeGen *g, TypeTableEntry *type_entry, ConstE
20582107 LLVMConstInt(usize->type_ref, index, false),
20592108 };
20602109 LLVMValueRef ptr_val = LLVMConstInBoundsGEP(array_const_val->llvm_global, indices, 2);
2110 const_val->llvm_value = ptr_val;
2111 render_const_val_global(g, type_entry, const_val);
20612112 return ptr_val;
20622113 }
20632114 }
......@@ -2065,17 +2116,17 @@ static LLVMValueRef gen_const_val(CodeGen *g, TypeTableEntry *type_entry, ConstE
20652116 {
20662117 TypeTableEntry *child_type = type_entry->data.error.child_type;
20672118 if (!type_has_bits(child_type)) {
2068 uint64_t value = const_val->data.x_err.err ? const_val->data.x_err.err->value : 0;
2119 uint64_t value = const_val->data.x_err_union.err ? const_val->data.x_err_union.err->value : 0;
20692120 return LLVMConstInt(g->err_tag_type->type_ref, value, false);
20702121 } else {
20712122 LLVMValueRef err_tag_value;
20722123 LLVMValueRef err_payload_value;
2073 if (const_val->data.x_err.err) {
2074 err_tag_value = LLVMConstInt(g->err_tag_type->type_ref, const_val->data.x_err.err->value, false);
2124 if (const_val->data.x_err_union.err) {
2125 err_tag_value = LLVMConstInt(g->err_tag_type->type_ref, const_val->data.x_err_union.err->value, false);
20752126 err_payload_value = LLVMConstNull(child_type->type_ref);
20762127 } else {
20772128 err_tag_value = LLVMConstNull(g->err_tag_type->type_ref);
2078 err_payload_value = gen_const_val(g, child_type, const_val->data.x_err.payload);
2129 err_payload_value = gen_const_val(g, child_type, const_val->data.x_err_union.payload);
20792130 }
20802131 LLVMValueRef fields[] = {
20812132 err_tag_value,
src/eval.cpp+12-5
......@@ -30,7 +30,7 @@ bool const_values_equal(ConstExprValue *a, ConstExprValue *b, TypeTableEntry *ty
3030 case TypeTableEntryIdVoid:
3131 return true;
3232 case TypeTableEntryIdPureError:
33 return a->data.x_err.err == b->data.x_err.err;
33 return a->data.x_pure_err == b->data.x_pure_err;
3434 case TypeTableEntryIdFn:
3535 return a->data.x_fn == b->data.x_fn;
3636 case TypeTableEntryIdBool:
......@@ -351,17 +351,24 @@ void eval_const_expr_implicit_cast(CastOp cast_op,
351351 const_val->special = ConstValSpecialStatic;
352352 break;
353353 case CastOpErrorWrap:
354 const_val->data.x_err.err = nullptr;
355 const_val->data.x_err.payload = other_val;
354 const_val->data.x_err_union.err = nullptr;
355 const_val->data.x_err_union.payload = other_val;
356356 const_val->special = ConstValSpecialStatic;
357357 break;
358358 case CastOpPureErrorWrap:
359 const_val->data.x_err.err = other_val->data.x_err.err;
359 const_val->data.x_err_union.err = other_val->data.x_pure_err;
360360 const_val->special = ConstValSpecialStatic;
361361 break;
362362 case CastOpErrToInt:
363363 {
364 uint64_t value = other_val->data.x_err.err ? other_val->data.x_err.err->value : 0;
364 uint64_t value;
365 if (other_type->id == TypeTableEntryIdErrorUnion) {
366 value = other_val->data.x_err_union.err ? other_val->data.x_err_union.err->value : 0;
367 } else if (other_type->id == TypeTableEntryIdPureError) {
368 value = other_val->data.x_pure_err->value;
369 } else {
370 zig_unreachable();
371 }
365372 bignum_init_unsigned(&const_val->data.x_bignum, value);
366373 const_val->special = ConstValSpecialStatic;
367374 break;
src/ir.cpp+91-79
......@@ -318,6 +318,10 @@ static constexpr IrInstructionId ir_instruction_id(IrInstructionCompileErr *) {
318318 return IrInstructionIdCompileErr;
319319}
320320
321static constexpr IrInstructionId ir_instruction_id(IrInstructionErrName *) {
322 return IrInstructionIdErrName;
323}
324
321325template<typename T>
322326static T *ir_create_instruction(IrExecutable *exec, Scope *scope, AstNode *source_node) {
323327 T *special_instruction = allocate<T>(1);
......@@ -508,8 +512,8 @@ static IrInstruction *ir_build_const_bound_fn(IrBuilder *irb, Scope *scope, AstN
508512 return &const_instruction->base;
509513}
510514
511static IrInstruction *ir_build_const_str_lit(IrBuilder *irb, Scope *scope, AstNode *source_node, Buf *str) {
512 IrInstructionConst *const_instruction = ir_build_instruction<IrInstructionConst>(irb, scope, source_node);
515static IrInstruction *ir_create_const_str_lit(IrBuilder *irb, Scope *scope, AstNode *source_node, Buf *str) {
516 IrInstructionConst *const_instruction = ir_create_instruction<IrInstructionConst>(irb->exec, scope, source_node);
513517 TypeTableEntry *u8_type = irb->codegen->builtin_types.entry_u8;
514518 TypeTableEntry *type_entry = get_array_type(irb->codegen, u8_type, buf_len(str));
515519 const_instruction->base.type_entry = type_entry;
......@@ -526,6 +530,11 @@ static IrInstruction *ir_build_const_str_lit(IrBuilder *irb, Scope *scope, AstNo
526530
527531 return &const_instruction->base;
528532}
533static IrInstruction *ir_build_const_str_lit(IrBuilder *irb, Scope *scope, AstNode *source_node, Buf *str) {
534 IrInstruction *instruction = ir_create_const_str_lit(irb, scope, source_node, str);
535 ir_instruction_append(irb->current_basic_block, instruction);
536 return instruction;
537}
529538
530539static IrInstruction *ir_build_const_c_str_lit(IrBuilder *irb, Scope *scope, AstNode *source_node, Buf *str) {
531540 // first we build the underlying array
......@@ -1229,15 +1238,6 @@ static IrInstruction *ir_build_array_len(IrBuilder *irb, Scope *scope, AstNode *
12291238 return &instruction->base;
12301239}
12311240
1232static IrInstruction *ir_build_array_len_from(IrBuilder *irb, IrInstruction *old_instruction,
1233 IrInstruction *array_value)
1234{
1235 IrInstruction *new_instruction = ir_build_array_len(irb, old_instruction->scope,
1236 old_instruction->source_node, array_value);
1237 ir_link_new_instruction(new_instruction, old_instruction);
1238 return new_instruction;
1239}
1240
12411241static IrInstruction *ir_build_ref(IrBuilder *irb, Scope *scope, AstNode *source_node, IrInstruction *value) {
12421242 IrInstructionRef *instruction = ir_build_instruction<IrInstructionRef>(irb, scope, source_node);
12431243 instruction->value = value;
......@@ -1280,6 +1280,22 @@ static IrInstruction *ir_build_compile_err(IrBuilder *irb, Scope *scope, AstNode
12801280 return &instruction->base;
12811281}
12821282
1283static IrInstruction *ir_build_err_name(IrBuilder *irb, Scope *scope, AstNode *source_node, IrInstruction *value) {
1284 IrInstructionErrName *instruction = ir_build_instruction<IrInstructionErrName>(irb, scope, source_node);
1285 instruction->value = value;
1286
1287 ir_ref_instruction(value);
1288
1289 return &instruction->base;
1290}
1291
1292static IrInstruction *ir_build_err_name_from(IrBuilder *irb, IrInstruction *old_instruction, IrInstruction *value) {
1293 IrInstruction *new_instruction = ir_build_err_name(irb, old_instruction->scope,
1294 old_instruction->source_node, value);
1295 ir_link_new_instruction(new_instruction, old_instruction);
1296 return new_instruction;
1297}
1298
12831299
12841300static void ir_gen_defers_for_block(IrBuilder *irb, Scope *inner_scope, Scope *outer_scope,
12851301 bool gen_error_defers, bool gen_maybe_defers)
......@@ -1946,6 +1962,15 @@ static IrInstruction *ir_gen_builtin_fn_call(IrBuilder *irb, Scope *scope, AstNo
19461962
19471963 return ir_build_compile_err(irb, scope, node, arg0_value);
19481964 }
1965 case BuiltinFnIdErrName:
1966 {
1967 AstNode *arg0_node = node->data.fn_call_expr.params.at(0);
1968 IrInstruction *arg0_value = ir_gen_node(irb, arg0_node, scope);
1969 if (arg0_value == irb->codegen->invalid_instruction)
1970 return arg0_value;
1971
1972 return ir_build_err_name(irb, scope, node, arg0_value);
1973 }
19491974 case BuiltinFnIdMemcpy:
19501975 case BuiltinFnIdMemset:
19511976 case BuiltinFnIdAlignof:
......@@ -1958,7 +1983,6 @@ static IrInstruction *ir_gen_builtin_fn_call(IrBuilder *irb, Scope *scope, AstNo
19581983 case BuiltinFnIdCDefine:
19591984 case BuiltinFnIdCUndef:
19601985 case BuiltinFnIdCImport:
1961 case BuiltinFnIdErrName:
19621986 case BuiltinFnIdBreakpoint:
19631987 case BuiltinFnIdReturnAddress:
19641988 case BuiltinFnIdFrameAddress:
......@@ -2834,6 +2858,11 @@ static IrInstruction *ir_gen_type_literal(IrBuilder *irb, Scope *scope, AstNode
28342858 return ir_build_const_type(irb, scope, node, irb->codegen->builtin_types.entry_type);
28352859}
28362860
2861static IrInstruction *ir_gen_error_type(IrBuilder *irb, Scope *scope, AstNode *node) {
2862 assert(node->type == NodeTypeErrorType);
2863 return ir_build_const_type(irb, scope, node, irb->codegen->builtin_types.entry_pure_error);
2864}
2865
28372866static IrInstruction *ir_gen_defer(IrBuilder *irb, Scope *parent_scope, AstNode *node) {
28382867 assert(node->type == NodeTypeDefer);
28392868
......@@ -2903,6 +2932,8 @@ static IrInstruction *ir_gen_node_raw(IrBuilder *irb, AstNode *node, Scope *scop
29032932 return ir_lval_wrap(irb, scope, ir_gen_goto(irb, scope, node), lval);
29042933 case NodeTypeTypeLiteral:
29052934 return ir_lval_wrap(irb, scope, ir_gen_type_literal(irb, scope, node), lval);
2935 case NodeTypeErrorType:
2936 return ir_lval_wrap(irb, scope, ir_gen_error_type(irb, scope, node), lval);
29062937 case NodeTypeBreak:
29072938 return ir_lval_wrap(irb, scope, ir_gen_break(irb, scope, node), lval);
29082939 case NodeTypeContinue:
......@@ -2913,7 +2944,6 @@ static IrInstruction *ir_gen_node_raw(IrBuilder *irb, AstNode *node, Scope *scop
29132944 case NodeTypeSliceExpr:
29142945 case NodeTypeCharLiteral:
29152946 case NodeTypeZeroesLiteral:
2916 case NodeTypeErrorType:
29172947 case NodeTypeVarLiteral:
29182948 case NodeTypeRoot:
29192949 case NodeTypeFnProto:
......@@ -5564,7 +5594,20 @@ static TypeTableEntry *ir_analyze_instruction_field_ptr(IrAnalyze *ira, IrInstru
55645594 return ira->codegen->builtin_types.entry_invalid;
55655595 }
55665596 } else if (child_type->id == TypeTableEntryIdPureError) {
5567 zig_panic("TODO error type field");
5597 auto err_table_entry = ira->codegen->error_table.maybe_get(field_name);
5598 if (err_table_entry) {
5599 ConstExprValue *const_val = allocate<ConstExprValue>(1);
5600 const_val->special = ConstValSpecialStatic;
5601 const_val->data.x_pure_err = err_table_entry->value;
5602
5603 bool depends_on_compile_var = container_ptr->static_value.depends_on_compile_var;
5604 return ir_analyze_const_ptr(ira, &field_ptr_instruction->base, const_val,
5605 child_type, depends_on_compile_var, ConstPtrSpecialNone);
5606 }
5607
5608 ir_add_error(ira, &field_ptr_instruction->base,
5609 buf_sprintf("use of undeclared error value '%s'", buf_ptr(field_name)));
5610 return ira->codegen->builtin_types.entry_invalid;
55685611 } else if (child_type->id == TypeTableEntryIdInt) {
55695612 zig_panic("TODO integer type field");
55705613 } else {
......@@ -6525,7 +6568,12 @@ static TypeTableEntry *ir_analyze_instruction_array_len(IrAnalyze *ira,
65256568 len_val->data.x_bignum.data.x_uint, depends_on_compile_var);
65266569 }
65276570 }
6528 ir_build_array_len_from(&ira->new_irb, &array_len_instruction->base, array_value);
6571 TypeStructField *field = find_struct_type_field(canon_type, buf_create_from_str("len"));
6572 assert(field);
6573 IrInstruction *len_ptr = ir_build_struct_field_ptr(&ira->new_irb, array_len_instruction->base.scope,
6574 array_len_instruction->base.source_node, array_value, field);
6575 len_ptr->type_entry = get_pointer_to_type(ira->codegen, ira->codegen->builtin_types.entry_usize, true);
6576 ir_build_load_ptr_from(&ira->new_irb, &array_len_instruction->base, len_ptr);
65296577 return ira->codegen->builtin_types.entry_usize;
65306578 } else {
65316579 add_node_error(ira->codegen, array_len_instruction->base.source_node,
......@@ -6814,6 +6862,31 @@ static TypeTableEntry *ir_analyze_instruction_compile_err(IrAnalyze *ira,
68146862 return ira->codegen->builtin_types.entry_invalid;
68156863}
68166864
6865static TypeTableEntry *ir_analyze_instruction_err_name(IrAnalyze *ira, IrInstructionErrName *instruction) {
6866 IrInstruction *value = instruction->value->other;
6867 if (value->type_entry->id == TypeTableEntryIdInvalid)
6868 return ira->codegen->builtin_types.entry_invalid;
6869
6870 IrInstruction *casted_value = ir_get_casted_value(ira, value, value->type_entry);
6871 if (casted_value->type_entry->id == TypeTableEntryIdInvalid)
6872 return ira->codegen->builtin_types.entry_invalid;
6873
6874 TypeTableEntry *str_type = get_slice_type(ira->codegen, ira->codegen->builtin_types.entry_u8, true);
6875 if (casted_value->static_value.special == ConstValSpecialStatic) {
6876 ErrorTableEntry *err = casted_value->static_value.data.x_pure_err;
6877 IrInstruction *new_instruction = ir_create_const_str_lit(&ira->new_irb, instruction->base.scope,
6878 instruction->base.source_node, &err->name);
6879 ir_link_new_instruction(new_instruction, &instruction->base);
6880 new_instruction->static_value.special = ConstValSpecialStatic;
6881 new_instruction->static_value.depends_on_compile_var = casted_value->static_value.depends_on_compile_var;
6882 return str_type;
6883 }
6884
6885 ira->codegen->generate_error_name_table = true;
6886 ir_build_err_name_from(&ira->new_irb, &instruction->base, value);
6887 return str_type;
6888}
6889
68176890static TypeTableEntry *ir_analyze_instruction_nocast(IrAnalyze *ira, IrInstruction *instruction) {
68186891 switch (instruction->id) {
68196892 case IrInstructionIdInvalid:
......@@ -6904,6 +6977,8 @@ static TypeTableEntry *ir_analyze_instruction_nocast(IrAnalyze *ira, IrInstructi
69046977 return ir_analyze_instruction_max_value(ira, (IrInstructionMaxValue *)instruction);
69056978 case IrInstructionIdCompileErr:
69066979 return ir_analyze_instruction_compile_err(ira, (IrInstructionCompileErr *)instruction);
6980 case IrInstructionIdErrName:
6981 return ir_analyze_instruction_err_name(ira, (IrInstructionErrName *)instruction);
69076982 case IrInstructionIdCast:
69086983 case IrInstructionIdStructFieldPtr:
69096984 case IrInstructionIdEnumFieldPtr:
......@@ -7033,6 +7108,7 @@ bool ir_has_side_effects(IrInstruction *instruction) {
70337108 case IrInstructionIdRef:
70347109 case IrInstructionIdMinValue:
70357110 case IrInstructionIdMaxValue:
7111 case IrInstructionIdErrName:
70367112 return false;
70377113 case IrInstructionIdAsm:
70387114 {
......@@ -7103,25 +7179,6 @@ bool ir_has_side_effects(IrInstruction *instruction) {
71037179// return resolve_expr_const_val_as_import(g, node, child_import);
71047180//}
71057181//
7106//static TypeTableEntry *analyze_err_name(CodeGen *g, ImportTableEntry *import,
7107// BlockContext *context, AstNode *node)
7108//{
7109// assert(node->type == NodeTypeFnCallExpr);
7110//
7111// AstNode *err_value = node->data.fn_call_expr.params.at(0);
7112// TypeTableEntry *resolved_type = analyze_expression(g, import, context,
7113// g->builtin_types.entry_pure_error, err_value);
7114//
7115// if (resolved_type->id == TypeTableEntryIdInvalid) {
7116// return resolved_type;
7117// }
7118//
7119// g->generate_error_name_table = true;
7120//
7121// TypeTableEntry *str_type = get_slice_type(g, g->builtin_types.entry_u8, true);
7122// return str_type;
7123//}
7124//
71257182//static TypeTableEntry *analyze_embed_file(CodeGen *g, ImportTableEntry *import,
71267183// BlockContext *context, AstNode *node)
71277184//{
......@@ -7533,8 +7590,6 @@ bool ir_has_side_effects(IrInstruction *instruction) {
75337590// return analyze_import(g, import, context, node);
75347591// case BuiltinFnIdCImport:
75357592// return analyze_c_import(g, import, context, node);
7536// case BuiltinFnIdErrName:
7537// return analyze_err_name(g, import, context, node);
75387593// case BuiltinFnIdBreakpoint:
75397594// mark_impure_fn(g, context, node);
75407595// return g->builtin_types.entry_void;
......@@ -7927,21 +7982,6 @@ bool ir_has_side_effects(IrInstruction *instruction) {
79277982// }
79287983//}
79297984//
7930//static TypeTableEntry *analyze_error_literal_expr(CodeGen *g, ImportTableEntry *import,
7931// BlockContext *context, AstNode *node, Buf *err_name)
7932//{
7933// auto err_table_entry = g->error_table.maybe_get(err_name);
7934//
7935// if (err_table_entry) {
7936// return resolve_expr_const_val_as_err(g, node, err_table_entry->value);
7937// }
7938//
7939// add_node_error(g, node,
7940// buf_sprintf("use of undeclared error value '%s'", buf_ptr(err_name)));
7941//
7942// return g->builtin_types.entry_invalid;
7943//}
7944//
79457985//static void validate_voided_expr(CodeGen *g, AstNode *source_node, TypeTableEntry *type_entry) {
79467986// if (type_entry->id == TypeTableEntryIdMetaType) {
79477987// add_node_error(g, first_executing_node(source_node), buf_sprintf("expected expression, found type"));
......@@ -7952,32 +7992,6 @@ bool ir_has_side_effects(IrInstruction *instruction) {
79527992//
79537993
79547994
7955//static LLVMValueRef gen_err_name(CodeGen *g, AstNode *node) {
7956// assert(node->type == NodeTypeFnCallExpr);
7957// assert(g->generate_error_name_table);
7958//
7959// if (g->error_decls.length == 1) {
7960// LLVMBuildUnreachable(g->builder);
7961// return nullptr;
7962// }
7963//
7964//
7965// AstNode *err_val_node = node->data.fn_call_expr.params.at(0);
7966// LLVMValueRef err_val = gen_expr(g, err_val_node);
7967//
7968// if (want_debug_safety(g, node)) {
7969// LLVMValueRef zero = LLVMConstNull(LLVMTypeOf(err_val));
7970// LLVMValueRef end_val = LLVMConstInt(LLVMTypeOf(err_val), g->error_decls.length, false);
7971// add_bounds_check(g, err_val, LLVMIntNE, zero, LLVMIntULT, end_val);
7972// }
7973//
7974// LLVMValueRef indices[] = {
7975// LLVMConstNull(g->builtin_types.entry_usize->type_ref),
7976// err_val,
7977// };
7978// return LLVMBuildInBoundsGEP(g->builder, g->err_name_table, indices, 2, "");
7979//}
7980//
79817995//static LLVMValueRef gen_cmp_exchange(CodeGen *g, AstNode *node) {
79827996// assert(node->type == NodeTypeFnCallExpr);
79837997//
......@@ -8179,8 +8193,6 @@ bool ir_has_side_effects(IrInstruction *instruction) {
81798193// zig_unreachable();
81808194// case BuiltinFnIdCompileVar:
81818195// return nullptr;
8182// case BuiltinFnIdErrName:
8183// return gen_err_name(g, node);
81848196// case BuiltinFnIdBreakpoint:
81858197// return LLVMBuildCall(g->builder, g->trap_fn_val, nullptr, 0, "");
81868198// case BuiltinFnIdFrameAddress:
src/ir_print.cpp+9
......@@ -676,6 +676,12 @@ static void ir_print_compile_err(IrPrint *irp, IrInstructionCompileErr *instruct
676676 fprintf(irp->f, ")");
677677}
678678
679static void ir_print_err_name(IrPrint *irp, IrInstructionErrName *instruction) {
680 fprintf(irp->f, "@errorName(");
681 ir_print_other_instruction(irp, instruction->value);
682 fprintf(irp->f, ")");
683}
684
679685static void ir_print_instruction(IrPrint *irp, IrInstruction *instruction) {
680686 ir_print_prefix(irp, instruction);
681687 switch (instruction->id) {
......@@ -822,6 +828,9 @@ static void ir_print_instruction(IrPrint *irp, IrInstruction *instruction) {
822828 case IrInstructionIdCompileErr:
823829 ir_print_compile_err(irp, (IrInstructionCompileErr *)instruction);
824830 break;
831 case IrInstructionIdErrName:
832 ir_print_err_name(irp, (IrInstructionErrName *)instruction);
833 break;
825834 }
826835 fprintf(irp->f, "\n");
827836}
test/self_hosted2.zig+21
......@@ -238,6 +238,26 @@ fn testMinValueAndMaxValue() {
238238 assert(@minValue(i64) == -9223372036854775808);
239239}
240240
241fn first4KeysOfHomeRow() -> []const u8 {
242 "aoeu"
243}
244
245fn testReturnStringFromFunction() {
246 assert(memeql(first4KeysOfHomeRow(), "aoeu"));
247}
248
249pub fn memeql(a: []const u8, b: []const u8) -> bool {
250 sliceEql(u8, a, b)
251}
252
253pub fn sliceEql(inline T: type, a: []const T, b: []const T) -> bool {
254 if (a.len != b.len) return false;
255 for (a) |item, index| {
256 if (b[index] != item) return false;
257 }
258 return true;
259}
260
241261fn assert(ok: bool) {
242262 if (!ok)
243263 @unreachable();
......@@ -263,6 +283,7 @@ fn runAllTests() {
263283 testStaticAddOne();
264284 testInlineVarsAgain();
265285 testMinValueAndMaxValue();
286 testReturnStringFromFunction();
266287}
267288
268289export nakedcc fn _start() -> unreachable {