authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2016-12-22 10:39:10-05:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2016-12-22 10:39:10-05:00
log6f91fb4c37a676002ccb68b78bea8044ba44b7e2
tree4500d5a03322813bb1f09b4a226a83cec939938b
parent8dc45bc50523d9fa58a937f525b9dc8f7569af0b

IR: support const ref


4 files changed, 60 insertions(+), 17 deletions(-)

src/all_types.hpp+2
......@@ -1849,6 +1849,7 @@ struct IrInstructionRef {
18491849
18501850 IrInstruction *value;
18511851 LLVMValueRef tmp_ptr;
1852 bool is_const;
18521853};
18531854
18541855struct IrInstructionMinValue {
......@@ -2099,6 +2100,7 @@ enum LValPurpose {
20992100 LValPurposeNone,
21002101 LValPurposeAssign,
21012102 LValPurposeAddressOf,
2103 LValPurposeAddressOfConst,
21022104};
21032105
21042106static const size_t slice_ptr_index = 0;
src/ir.cpp+25-16
......@@ -1399,17 +1399,23 @@ static IrInstruction *ir_build_array_len(IrBuilder *irb, Scope *scope, AstNode *
13991399 return &instruction->base;
14001400}
14011401
1402static IrInstruction *ir_build_ref(IrBuilder *irb, Scope *scope, AstNode *source_node, IrInstruction *value) {
1402static IrInstruction *ir_build_ref(IrBuilder *irb, Scope *scope, AstNode *source_node, IrInstruction *value,
1403 bool is_const)
1404{
14031405 IrInstructionRef *instruction = ir_build_instruction<IrInstructionRef>(irb, scope, source_node);
14041406 instruction->value = value;
1407 instruction->is_const = is_const;
14051408
14061409 ir_ref_instruction(value);
14071410
14081411 return &instruction->base;
14091412}
14101413
1411static IrInstruction *ir_build_ref_from(IrBuilder *irb, IrInstruction *old_instruction, IrInstruction *value) {
1412 IrInstruction *new_instruction = ir_build_ref(irb, old_instruction->scope, old_instruction->source_node, value);
1414static IrInstruction *ir_build_ref_from(IrBuilder *irb, IrInstruction *old_instruction, IrInstruction *value,
1415 bool is_const)
1416{
1417 IrInstruction *new_instruction = ir_build_ref(irb, old_instruction->scope, old_instruction->source_node,
1418 value, is_const);
14131419 ir_link_new_instruction(new_instruction, old_instruction);
14141420 return new_instruction;
14151421}
......@@ -2479,7 +2485,7 @@ static IrInstruction *ir_gen_decl_ref(IrBuilder *irb, AstNode *source_node, Tld
24792485 assert(fn_entry->type_entry);
24802486 IrInstruction *ref_instruction = ir_build_const_fn(irb, scope, source_node, fn_entry);
24812487 if (lval != LValPurposeNone)
2482 return ir_build_ref(irb, scope, source_node, ref_instruction);
2488 return ir_build_ref(irb, scope, source_node, ref_instruction, true);
24832489 else
24842490 return ref_instruction;
24852491 }
......@@ -2489,7 +2495,7 @@ static IrInstruction *ir_gen_decl_ref(IrBuilder *irb, AstNode *source_node, Tld
24892495 TypeTableEntry *typedef_type = tld_typedef->type_entry;
24902496 IrInstruction *ref_instruction = ir_build_const_type(irb, scope, source_node, typedef_type);
24912497 if (lval != LValPurposeNone)
2492 return ir_build_ref(irb, scope, source_node, ref_instruction);
2498 return ir_build_ref(irb, scope, source_node, ref_instruction, true);
24932499 else
24942500 return ref_instruction;
24952501 }
......@@ -2506,7 +2512,7 @@ static IrInstruction *ir_gen_symbol(IrBuilder *irb, Scope *scope, AstNode *node,
25062512 if (primitive_table_entry) {
25072513 IrInstruction *value = ir_build_const_type(irb, scope, node, primitive_table_entry->value);
25082514 if (lval != LValPurposeNone) {
2509 return ir_build_ref(irb, scope, node, value);
2515 return ir_build_ref(irb, scope, node, value, lval == LValPurposeAddressOfConst);
25102516 } else {
25112517 return value;
25122518 }
......@@ -3105,14 +3111,15 @@ static IrInstruction *ir_lval_wrap(IrBuilder *irb, Scope *scope, IrInstruction *
31053111
31063112 // We needed a pointer to a value, but we got a value. So we create
31073113 // an instruction which just makes a const pointer of it.
3108 return ir_build_ref(irb, scope, value->source_node, value);
3114 return ir_build_ref(irb, scope, value->source_node, value, true);
31093115}
31103116
31113117static IrInstruction *ir_gen_address_of(IrBuilder *irb, Scope *scope, AstNode *node, bool is_const, LValPurpose lval) {
31123118 assert(node->type == NodeTypePrefixOpExpr);
31133119 AstNode *expr_node = node->data.prefix_op_expr.primary_expr;
31143120
3115 IrInstruction *value = ir_gen_node_extra(irb, expr_node, scope, LValPurposeAddressOf);
3121 IrInstruction *value = ir_gen_node_extra(irb, expr_node, scope,
3122 is_const ? LValPurposeAddressOfConst : LValPurposeAddressOf);
31163123 if (value == irb->codegen->invalid_instruction)
31173124 return value;
31183125
......@@ -5102,7 +5109,8 @@ static IrInstruction *ir_analyze_cast_ref(IrAnalyze *ira, IrInstruction *source_
51025109 IrInstructionLoadPtr *load_ptr_inst = (IrInstructionLoadPtr *)value;
51035110 return load_ptr_inst->ptr;
51045111 } else {
5105 IrInstruction *new_instruction = ir_build_ref(&ira->new_irb, source_instr->scope, source_instr->source_node, value);
5112 IrInstruction *new_instruction = ir_build_ref(&ira->new_irb, source_instr->scope,
5113 source_instr->source_node, value, true);
51065114
51075115 TypeTableEntry *child_type = wanted_type->data.pointer.child_type;
51085116 if (type_has_bits(child_type)) {
......@@ -5454,7 +5462,9 @@ static IrInstruction *ir_get_deref(IrAnalyze *ira, IrInstruction *source_instruc
54545462 }
54555463}
54565464
5457static TypeTableEntry *ir_analyze_ref(IrAnalyze *ira, IrInstruction *source_instruction, IrInstruction *value) {
5465static TypeTableEntry *ir_analyze_ref(IrAnalyze *ira, IrInstruction *source_instruction, IrInstruction *value,
5466 bool is_const)
5467{
54585468 if (value->type_entry->id == TypeTableEntryIdInvalid)
54595469 return ira->codegen->builtin_types.entry_invalid;
54605470
......@@ -5462,15 +5472,14 @@ static TypeTableEntry *ir_analyze_ref(IrAnalyze *ira, IrInstruction *source_inst
54625472 ConstExprValue *val = ir_resolve_const(ira, value, UndefBad);
54635473 if (!val)
54645474 return ira->codegen->builtin_types.entry_invalid;
5465 bool ptr_is_const = false;
54665475 return ir_analyze_const_ptr(ira, source_instruction, val, value->type_entry,
5467 false, ConstPtrSpecialNone, ptr_is_const);
5476 false, ConstPtrSpecialNone, is_const);
54685477 }
54695478
54705479 TypeTableEntry *ptr_type = get_pointer_to_type(ira->codegen, value->type_entry, true);
54715480 FnTableEntry *fn_entry = exec_fn_entry(ira->new_irb.exec);
54725481 assert(fn_entry);
5473 IrInstruction *new_instruction = ir_build_ref_from(&ira->new_irb, source_instruction, value);
5482 IrInstruction *new_instruction = ir_build_ref_from(&ira->new_irb, source_instruction, value, is_const);
54745483 fn_entry->alloca_list.append(new_instruction);
54755484 return ptr_type;
54765485}
......@@ -6986,7 +6995,7 @@ static TypeTableEntry *ir_analyze_var_ptr(IrAnalyze *ira, IrInstruction *instruc
69866995 return ir_analyze_const_ptr(ira, instruction, mem_slot, var->type, false, ptr_special, var->src_is_const);
69876996 } else {
69886997 ir_build_var_ptr_from(&ira->new_irb, instruction, var);
6989 return get_pointer_to_type(ira->codegen, var->type, false);
6998 return get_pointer_to_type(ira->codegen, var->type, var->src_is_const);
69906999 }
69917000}
69927001
......@@ -7130,7 +7139,7 @@ static TypeTableEntry *ir_analyze_container_member_access_inner(IrAnalyze *ira,
71307139 bool depends_on_compile_var = container_ptr->static_value.depends_on_compile_var;
71317140 IrInstruction *bound_fn_value = ir_build_const_bound_fn(&ira->new_irb, field_ptr_instruction->base.scope,
71327141 field_ptr_instruction->base.source_node, fn_entry, container_ptr, depends_on_compile_var);
7133 return ir_analyze_ref(ira, &field_ptr_instruction->base, bound_fn_value);
7142 return ir_analyze_ref(ira, &field_ptr_instruction->base, bound_fn_value, true);
71347143 }
71357144 }
71367145 ir_add_error_node(ira, field_ptr_instruction->base.source_node,
......@@ -8413,7 +8422,7 @@ static TypeTableEntry *ir_analyze_instruction_array_len(IrAnalyze *ira,
84138422
84148423static TypeTableEntry *ir_analyze_instruction_ref(IrAnalyze *ira, IrInstructionRef *ref_instruction) {
84158424 IrInstruction *value = ref_instruction->value->other;
8416 return ir_analyze_ref(ira, &ref_instruction->base, value);
8425 return ir_analyze_ref(ira, &ref_instruction->base, value, ref_instruction->is_const);
84178426}
84188427
84198428static TypeTableEntry *ir_analyze_container_init_fields(IrAnalyze *ira, IrInstruction *instruction,
src/ir_print.cpp+2-1
......@@ -666,7 +666,8 @@ static void ir_print_array_len(IrPrint *irp, IrInstructionArrayLen *instruction)
666666}
667667
668668static void ir_print_ref(IrPrint *irp, IrInstructionRef *instruction) {
669 fprintf(irp->f, "ref ");
669 const char *const_str = instruction->is_const ? "const " : "";
670 fprintf(irp->f, "%sref ", const_str);
670671 ir_print_other_instruction(irp, instruction->value);
671672}
672673
test/cases3/misc.zig+31
......@@ -242,6 +242,24 @@ fn multilineString() {
242242 assert(memeql(s1, s2));
243243}
244244
245fn multilineCString() {
246 @setFnTest(this);
247
248 const s1 =
249 c\\one
250 c\\two)
251 c\\three
252 ;
253 const s2 = c"one\ntwo)\nthree";
254 assert(cstrcmp(s1, s2) == 0);
255}
256
257
258fn typeEquality() {
259 @setFnTest(this);
260
261 assert(&const u8 != &u8);
262}
245263
246264// TODO import from std.str
247265pub fn memeql(a: []const u8, b: []const u8) -> bool {
......@@ -257,6 +275,19 @@ pub fn sliceEql(inline T: type, a: []const T, b: []const T) -> bool {
257275 return true;
258276}
259277
278// TODO import from std.cstr
279pub fn cstrcmp(a: &const u8, b: &const u8) -> i8 {
280 var index: usize = 0;
281 while (a[index] == b[index] && a[index] != 0; index += 1) {}
282 return if (a[index] > b[index]) {
283 1
284 } else if (a[index] < b[index]) {
285 -1
286 } else {
287 i8(0)
288 };
289}
290
260291
261292// TODO const assert = @import("std").debug.assert;
262293fn assert(ok: bool) {