authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2017-02-16 13:58:42-05:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2017-02-16 13:58:42-05:00
log4b5cc80f665314067e0e5b96c859acca1b2e1cb0
tree1d8a99cba66634ad98a3faae29d3160ccefec0c9
parent4a957b9ea32e0539772993278a9a1f99ee3df68b

move volatile pointers to central type table


4 files changed, 50 insertions(+), 28 deletions(-)

src/all_types.hpp+1-1
...@@ -1038,7 +1038,7 @@ struct TypeTableEntry {...@@ -1038,7 +1038,7 @@ struct TypeTableEntry {
1038 } data;1038 } data;
10391039
1040 // use these fields to make sure we don't duplicate type table entries for the same type1040 // use these fields to make sure we don't duplicate type table entries for the same type
1041 TypeTableEntry *pointer_parent[2][2]; // [0 - mut, 1 - const][0 - normal, 1 - volatile]1041 TypeTableEntry *pointer_parent[2]; // [0 - mut, 1 - const]
1042 TypeTableEntry *slice_parent[2]; // [0 - mut, 1 - const]1042 TypeTableEntry *slice_parent[2]; // [0 - mut, 1 - const]
1043 TypeTableEntry *maybe_parent;1043 TypeTableEntry *maybe_parent;
1044 TypeTableEntry *error_parent;1044 TypeTableEntry *error_parent;
src/analyze.cpp+29-8
...@@ -286,11 +286,28 @@ TypeTableEntry *get_smallest_unsigned_int_type(CodeGen *g, uint64_t x) {...@@ -286,11 +286,28 @@ TypeTableEntry *get_smallest_unsigned_int_type(CodeGen *g, uint64_t x) {
286 return get_int_type(g, false, bits_needed_for_unsigned(x));286 return get_int_type(g, false, bits_needed_for_unsigned(x));
287}287}
288288
289TypeTableEntry *get_pointer_to_type_volatile(CodeGen *g, TypeTableEntry *child_type, bool is_const, bool is_volatile) {289TypeTableEntry *get_pointer_to_type_extra(CodeGen *g, TypeTableEntry *child_type, bool is_const,
290 uint8_t bit_offset, bool is_volatile)
291{
290 assert(child_type->id != TypeTableEntryIdInvalid);292 assert(child_type->id != TypeTableEntryIdInvalid);
291 TypeTableEntry **parent_pointer = &child_type->pointer_parent[(is_const ? 1 : 0)][(is_volatile ? 1 : 0)];293
292 if (*parent_pointer)294 TypeId type_id = {};
293 return *parent_pointer;295 TypeTableEntry **parent_pointer = nullptr;
296 if (bit_offset != 0 || is_volatile) {
297 type_id.id = TypeTableEntryIdPointer;
298 type_id.data.pointer.child_type = child_type;
299 type_id.data.pointer.is_const = is_const;
300 type_id.data.pointer.is_volatile = is_volatile;
301 type_id.data.pointer.bit_offset = bit_offset;
302
303 auto existing_entry = g->type_table.maybe_get(type_id);
304 if (existing_entry)
305 return existing_entry->value;
306 } else {
307 parent_pointer = &child_type->pointer_parent[(is_const ? 1 : 0)];
308 if (*parent_pointer)
309 return *parent_pointer;
310 }
294311
295 type_ensure_zero_bits_known(g, child_type);312 type_ensure_zero_bits_known(g, child_type);
296313
...@@ -322,12 +339,16 @@ TypeTableEntry *get_pointer_to_type_volatile(CodeGen *g, TypeTableEntry *child_t...@@ -322,12 +339,16 @@ TypeTableEntry *get_pointer_to_type_volatile(CodeGen *g, TypeTableEntry *child_t
322 entry->data.pointer.is_const = is_const;339 entry->data.pointer.is_const = is_const;
323 entry->data.pointer.is_volatile = is_volatile;340 entry->data.pointer.is_volatile = is_volatile;
324341
325 *parent_pointer = entry;342 if (parent_pointer) {
343 *parent_pointer = entry;
344 } else {
345 g->type_table.put(type_id, entry);
346 }
326 return entry;347 return entry;
327}348}
328349
329TypeTableEntry *get_pointer_to_type(CodeGen *g, TypeTableEntry *child_type, bool is_const) {350TypeTableEntry *get_pointer_to_type(CodeGen *g, TypeTableEntry *child_type, bool is_const) {
330 return get_pointer_to_type_volatile(g, child_type, is_const, false);351 return get_pointer_to_type_extra(g, child_type, is_const, 0, false);
331}352}
332353
333TypeTableEntry *get_maybe_type(CodeGen *g, TypeTableEntry *child_type) {354TypeTableEntry *get_maybe_type(CodeGen *g, TypeTableEntry *child_type) {
...@@ -488,7 +509,7 @@ TypeTableEntry *get_error_type(CodeGen *g, TypeTableEntry *child_type) {...@@ -488,7 +509,7 @@ TypeTableEntry *get_error_type(CodeGen *g, TypeTableEntry *child_type) {
488}509}
489510
490TypeTableEntry *get_array_type(CodeGen *g, TypeTableEntry *child_type, uint64_t array_size) {511TypeTableEntry *get_array_type(CodeGen *g, TypeTableEntry *child_type, uint64_t array_size) {
491 TypeId type_id;512 TypeId type_id = {};
492 type_id.id = TypeTableEntryIdArray;513 type_id.id = TypeTableEntryIdArray;
493 type_id.data.array.child_type = child_type;514 type_id.data.array.child_type = child_type;
494 type_id.data.array.size = array_size;515 type_id.data.array.size = array_size;
...@@ -2825,7 +2846,7 @@ TypeTableEntry *get_int_type(CodeGen *g, bool is_signed, uint8_t size_in_bits) {...@@ -2825,7 +2846,7 @@ TypeTableEntry *get_int_type(CodeGen *g, bool is_signed, uint8_t size_in_bits) {
2825 if (common_entry)2846 if (common_entry)
2826 return *common_entry;2847 return *common_entry;
28272848
2828 TypeId type_id;2849 TypeId type_id = {};
2829 type_id.id = TypeTableEntryIdInt;2850 type_id.id = TypeTableEntryIdInt;
2830 type_id.data.integer.is_signed = is_signed;2851 type_id.data.integer.is_signed = is_signed;
2831 type_id.data.integer.bit_count = size_in_bits;2852 type_id.data.integer.bit_count = size_in_bits;
src/analyze.hpp+2-1
...@@ -15,7 +15,8 @@ ErrorMsg *add_node_error(CodeGen *g, AstNode *node, Buf *msg);...@@ -15,7 +15,8 @@ ErrorMsg *add_node_error(CodeGen *g, AstNode *node, Buf *msg);
15ErrorMsg *add_error_note(CodeGen *g, ErrorMsg *parent_msg, AstNode *node, Buf *msg);15ErrorMsg *add_error_note(CodeGen *g, ErrorMsg *parent_msg, AstNode *node, Buf *msg);
16TypeTableEntry *new_type_table_entry(TypeTableEntryId id);16TypeTableEntry *new_type_table_entry(TypeTableEntryId id);
17TypeTableEntry *get_pointer_to_type(CodeGen *g, TypeTableEntry *child_type, bool is_const);17TypeTableEntry *get_pointer_to_type(CodeGen *g, TypeTableEntry *child_type, bool is_const);
18TypeTableEntry *get_pointer_to_type_volatile(CodeGen *g, TypeTableEntry *child_type, bool is_const, bool is_volatile);18TypeTableEntry *get_pointer_to_type_extra(CodeGen *g, TypeTableEntry *child_type, bool is_const,
19 uint8_t bit_offset, bool is_volatile);
19bool is_node_void_expr(AstNode *node);20bool is_node_void_expr(AstNode *node);
20uint64_t type_size(CodeGen *g, TypeTableEntry *type_entry);21uint64_t type_size(CodeGen *g, TypeTableEntry *type_entry);
21TypeTableEntry **get_int_type_ptr(CodeGen *g, bool is_signed, uint8_t size_in_bits);22TypeTableEntry **get_int_type_ptr(CodeGen *g, bool is_signed, uint8_t size_in_bits);
src/ir.cpp+18-18
...@@ -6226,12 +6226,12 @@ static IrInstruction *ir_get_const_ptr(IrAnalyze *ira, IrInstruction *instructio...@@ -6226,12 +6226,12 @@ static IrInstruction *ir_get_const_ptr(IrAnalyze *ira, IrInstruction *instructio
6226 ConstExprValue *const_val = &const_instr->value;6226 ConstExprValue *const_val = &const_instr->value;
6227 const_val->type = pointee_type;6227 const_val->type = pointee_type;
6228 type_ensure_zero_bits_known(ira->codegen, type_entry);6228 type_ensure_zero_bits_known(ira->codegen, type_entry);
6229 const_val->data.x_type = get_pointer_to_type_volatile(ira->codegen, type_entry,6229 const_val->data.x_type = get_pointer_to_type_extra(ira->codegen, type_entry,
6230 ptr_is_const, ptr_is_volatile);6230 ptr_is_const, 0, ptr_is_volatile);
6231 return const_instr;6231 return const_instr;
6232 } else {6232 } else {
6233 TypeTableEntry *ptr_type = get_pointer_to_type_volatile(ira->codegen, pointee_type,6233 TypeTableEntry *ptr_type = get_pointer_to_type_extra(ira->codegen, pointee_type,
6234 ptr_is_const, ptr_is_volatile);6234 ptr_is_const, 0, ptr_is_volatile);
6235 IrInstruction *const_instr = ir_get_const(ira, instruction);6235 IrInstruction *const_instr = ir_get_const(ira, instruction);
6236 ConstExprValue *const_val = &const_instr->value;6236 ConstExprValue *const_val = &const_instr->value;
6237 const_val->type = ptr_type;6237 const_val->type = ptr_type;
...@@ -6547,7 +6547,7 @@ static IrInstruction *ir_get_ref(IrAnalyze *ira, IrInstruction *source_instructi...@@ -6547,7 +6547,7 @@ static IrInstruction *ir_get_ref(IrAnalyze *ira, IrInstruction *source_instructi
6547 ConstPtrMutComptimeConst, is_const, is_volatile);6547 ConstPtrMutComptimeConst, is_const, is_volatile);
6548 }6548 }
65496549
6550 TypeTableEntry *ptr_type = get_pointer_to_type_volatile(ira->codegen, value->value.type, is_const, is_volatile);6550 TypeTableEntry *ptr_type = get_pointer_to_type_extra(ira->codegen, value->value.type, is_const, 0, is_volatile);
6551 FnTableEntry *fn_entry = exec_fn_entry(ira->new_irb.exec);6551 FnTableEntry *fn_entry = exec_fn_entry(ira->new_irb.exec);
6552 assert(fn_entry);6552 assert(fn_entry);
6553 IrInstruction *new_instruction = ir_build_ref(&ira->new_irb, source_instruction->scope,6553 IrInstruction *new_instruction = ir_build_ref(&ira->new_irb, source_instruction->scope,
...@@ -8838,8 +8838,8 @@ static TypeTableEntry *ir_analyze_instruction_elem_ptr(IrAnalyze *ira, IrInstruc...@@ -8838,8 +8838,8 @@ static TypeTableEntry *ir_analyze_instruction_elem_ptr(IrAnalyze *ira, IrInstruc
8838 buf_sprintf("index 0 outside array of size 0"));8838 buf_sprintf("index 0 outside array of size 0"));
8839 }8839 }
8840 TypeTableEntry *child_type = array_type->data.array.child_type;8840 TypeTableEntry *child_type = array_type->data.array.child_type;
8841 return_type = get_pointer_to_type_volatile(ira->codegen, child_type,8841 return_type = get_pointer_to_type_extra(ira->codegen, child_type,
8842 ptr_type->data.pointer.is_const, ptr_type->data.pointer.is_volatile);8842 ptr_type->data.pointer.is_const, 0, ptr_type->data.pointer.is_volatile);
8843 } else if (array_type->id == TypeTableEntryIdPointer) {8843 } else if (array_type->id == TypeTableEntryIdPointer) {
8844 return_type = array_type;8844 return_type = array_type;
8845 } else if (is_slice(array_type)) {8845 } else if (is_slice(array_type)) {
...@@ -9056,8 +9056,8 @@ static TypeTableEntry *ir_analyze_container_field_ptr(IrAnalyze *ira, Buf *field...@@ -9056,8 +9056,8 @@ static TypeTableEntry *ir_analyze_container_field_ptr(IrAnalyze *ira, Buf *field
9056 if (ptr_val->data.x_ptr.special != ConstPtrSpecialHardCodedAddr) {9056 if (ptr_val->data.x_ptr.special != ConstPtrSpecialHardCodedAddr) {
9057 ConstExprValue *struct_val = const_ptr_pointee(ptr_val);9057 ConstExprValue *struct_val = const_ptr_pointee(ptr_val);
9058 ConstExprValue *field_val = &struct_val->data.x_struct.fields[field->src_index];9058 ConstExprValue *field_val = &struct_val->data.x_struct.fields[field->src_index];
9059 TypeTableEntry *ptr_type = get_pointer_to_type_volatile(ira->codegen, field_val->type,9059 TypeTableEntry *ptr_type = get_pointer_to_type_extra(ira->codegen, field_val->type,
9060 is_const, is_volatile);9060 is_const, 0, is_volatile);
9061 ConstExprValue *const_val = ir_build_const_from(ira, &field_ptr_instruction->base);9061 ConstExprValue *const_val = ir_build_const_from(ira, &field_ptr_instruction->base);
9062 const_val->data.x_ptr.special = ConstPtrSpecialBaseStruct;9062 const_val->data.x_ptr.special = ConstPtrSpecialBaseStruct;
9063 const_val->data.x_ptr.mut = container_ptr->value.data.x_ptr.mut;9063 const_val->data.x_ptr.mut = container_ptr->value.data.x_ptr.mut;
...@@ -9067,7 +9067,7 @@ static TypeTableEntry *ir_analyze_container_field_ptr(IrAnalyze *ira, Buf *field...@@ -9067,7 +9067,7 @@ static TypeTableEntry *ir_analyze_container_field_ptr(IrAnalyze *ira, Buf *field
9067 }9067 }
9068 }9068 }
9069 ir_build_struct_field_ptr_from(&ira->new_irb, &field_ptr_instruction->base, container_ptr, field);9069 ir_build_struct_field_ptr_from(&ira->new_irb, &field_ptr_instruction->base, container_ptr, field);
9070 return get_pointer_to_type_volatile(ira->codegen, field->type_entry, is_const, is_volatile);9070 return get_pointer_to_type_extra(ira->codegen, field->type_entry, is_const, 0, is_volatile);
9071 } else {9071 } else {
9072 return ir_analyze_container_member_access_inner(ira, bare_type, field_name,9072 return ir_analyze_container_member_access_inner(ira, bare_type, field_name,
9073 field_ptr_instruction, container_ptr, container_type);9073 field_ptr_instruction, container_ptr, container_type);
...@@ -9079,7 +9079,7 @@ static TypeTableEntry *ir_analyze_container_field_ptr(IrAnalyze *ira, Buf *field...@@ -9079,7 +9079,7 @@ static TypeTableEntry *ir_analyze_container_field_ptr(IrAnalyze *ira, Buf *field
9079 TypeEnumField *field = find_enum_type_field(bare_type, field_name);9079 TypeEnumField *field = find_enum_type_field(bare_type, field_name);
9080 if (field) {9080 if (field) {
9081 ir_build_enum_field_ptr_from(&ira->new_irb, &field_ptr_instruction->base, container_ptr, field);9081 ir_build_enum_field_ptr_from(&ira->new_irb, &field_ptr_instruction->base, container_ptr, field);
9082 return get_pointer_to_type_volatile(ira->codegen, field->type_entry, is_const, is_volatile);9082 return get_pointer_to_type_extra(ira->codegen, field->type_entry, is_const, 0, is_volatile);
9083 } else {9083 } else {
9084 return ir_analyze_container_member_access_inner(ira, bare_type, field_name,9084 return ir_analyze_container_member_access_inner(ira, bare_type, field_name,
9085 field_ptr_instruction, container_ptr, container_type);9085 field_ptr_instruction, container_ptr, container_type);
...@@ -10014,8 +10014,8 @@ static TypeTableEntry *ir_analyze_instruction_unwrap_maybe(IrAnalyze *ira,...@@ -10014,8 +10014,8 @@ static TypeTableEntry *ir_analyze_instruction_unwrap_maybe(IrAnalyze *ira,
10014 return ira->codegen->builtin_types.entry_invalid;10014 return ira->codegen->builtin_types.entry_invalid;
10015 }10015 }
10016 TypeTableEntry *child_type = type_entry->data.maybe.child_type;10016 TypeTableEntry *child_type = type_entry->data.maybe.child_type;
10017 TypeTableEntry *result_type = get_pointer_to_type_volatile(ira->codegen, child_type,10017 TypeTableEntry *result_type = get_pointer_to_type_extra(ira->codegen, child_type,
10018 ptr_type->data.pointer.is_const, ptr_type->data.pointer.is_volatile);10018 ptr_type->data.pointer.is_const, 0, ptr_type->data.pointer.is_volatile);
1001910019
10020 if (instr_is_comptime(value)) {10020 if (instr_is_comptime(value)) {
10021 ConstExprValue *val = ir_resolve_const(ira, value, UndefBad);10021 ConstExprValue *val = ir_resolve_const(ira, value, UndefBad);
...@@ -11321,7 +11321,7 @@ static TypeTableEntry *ir_analyze_instruction_memset(IrAnalyze *ira, IrInstructi...@@ -11321,7 +11321,7 @@ static TypeTableEntry *ir_analyze_instruction_memset(IrAnalyze *ira, IrInstructi
1132111321
11322 TypeTableEntry *usize = ira->codegen->builtin_types.entry_usize;11322 TypeTableEntry *usize = ira->codegen->builtin_types.entry_usize;
11323 TypeTableEntry *u8 = ira->codegen->builtin_types.entry_u8;11323 TypeTableEntry *u8 = ira->codegen->builtin_types.entry_u8;
11324 TypeTableEntry *u8_ptr = get_pointer_to_type_volatile(ira->codegen, u8, false, dest_is_volatile);11324 TypeTableEntry *u8_ptr = get_pointer_to_type_extra(ira->codegen, u8, false, 0, dest_is_volatile);
1132511325
11326 IrInstruction *casted_dest_ptr = ir_implicit_cast(ira, dest_ptr, u8_ptr);11326 IrInstruction *casted_dest_ptr = ir_implicit_cast(ira, dest_ptr, u8_ptr);
11327 if (casted_dest_ptr->value.type->id == TypeTableEntryIdInvalid)11327 if (casted_dest_ptr->value.type->id == TypeTableEntryIdInvalid)
...@@ -11409,8 +11409,8 @@ static TypeTableEntry *ir_analyze_instruction_memcpy(IrAnalyze *ira, IrInstructi...@@ -11409,8 +11409,8 @@ static TypeTableEntry *ir_analyze_instruction_memcpy(IrAnalyze *ira, IrInstructi
1140911409
11410 TypeTableEntry *usize = ira->codegen->builtin_types.entry_usize;11410 TypeTableEntry *usize = ira->codegen->builtin_types.entry_usize;
11411 TypeTableEntry *u8 = ira->codegen->builtin_types.entry_u8;11411 TypeTableEntry *u8 = ira->codegen->builtin_types.entry_u8;
11412 TypeTableEntry *u8_ptr_mut = get_pointer_to_type_volatile(ira->codegen, u8, false, dest_is_volatile);11412 TypeTableEntry *u8_ptr_mut = get_pointer_to_type_extra(ira->codegen, u8, false, 0, dest_is_volatile);
11413 TypeTableEntry *u8_ptr_const = get_pointer_to_type_volatile(ira->codegen, u8, true, src_is_volatile);11413 TypeTableEntry *u8_ptr_const = get_pointer_to_type_extra(ira->codegen, u8, true, 0, src_is_volatile);
1141411414
11415 IrInstruction *casted_dest_ptr = ir_implicit_cast(ira, dest_ptr, u8_ptr_mut);11415 IrInstruction *casted_dest_ptr = ir_implicit_cast(ira, dest_ptr, u8_ptr_mut);
11416 if (casted_dest_ptr->value.type->id == TypeTableEntryIdInvalid)11416 if (casted_dest_ptr->value.type->id == TypeTableEntryIdInvalid)
...@@ -11927,8 +11927,8 @@ static TypeTableEntry *ir_analyze_instruction_unwrap_err_payload(IrAnalyze *ira,...@@ -11927,8 +11927,8 @@ static TypeTableEntry *ir_analyze_instruction_unwrap_err_payload(IrAnalyze *ira,
11927 return ira->codegen->builtin_types.entry_invalid;11927 return ira->codegen->builtin_types.entry_invalid;
11928 } else if (canon_type->id == TypeTableEntryIdErrorUnion) {11928 } else if (canon_type->id == TypeTableEntryIdErrorUnion) {
11929 TypeTableEntry *child_type = canon_type->data.error.child_type;11929 TypeTableEntry *child_type = canon_type->data.error.child_type;
11930 TypeTableEntry *result_type = get_pointer_to_type_volatile(ira->codegen, child_type,11930 TypeTableEntry *result_type = get_pointer_to_type_extra(ira->codegen, child_type,
11931 ptr_type->data.pointer.is_const, ptr_type->data.pointer.is_volatile);11931 ptr_type->data.pointer.is_const, 0, ptr_type->data.pointer.is_volatile);
11932 if (instr_is_comptime(value)) {11932 if (instr_is_comptime(value)) {
11933 ConstExprValue *ptr_val = ir_resolve_const(ira, value, UndefBad);11933 ConstExprValue *ptr_val = ir_resolve_const(ira, value, UndefBad);
11934 if (!ptr_val)11934 if (!ptr_val)