authorgravatar for alex_naskos@hotmail.comAlexandros Naskos <alex_naskos@hotmail.com> 2018-04-26 14:03:19+03:00
committergravatar for alex_naskos@hotmail.comAlexandros Naskos <alex_naskos@hotmail.com> 2018-04-26 14:03:19+03:00
logbb56360bfaa317809bd3c742a655b357bd5b6226
treeb402d0f319dfc6f45bd9304fc13389310eb680aa
parentdd88d7deda66a4e4e1527831e7b24a3cf358d1b7

Added TypeInfo cache


3 files changed, 70 insertions(+), 50 deletions(-)

src/all_types.hpp+1
...@@ -1507,6 +1507,7 @@ struct CodeGen {...@@ -1507,6 +1507,7 @@ struct CodeGen {
1507 HashMap<Buf *, AstNode *, buf_hash, buf_eql_buf> exported_symbol_names;1507 HashMap<Buf *, AstNode *, buf_hash, buf_eql_buf> exported_symbol_names;
1508 HashMap<Buf *, Tld *, buf_hash, buf_eql_buf> external_prototypes;1508 HashMap<Buf *, Tld *, buf_hash, buf_eql_buf> external_prototypes;
1509 HashMap<Buf *, ConstExprValue *, buf_hash, buf_eql_buf> string_literals_table;1509 HashMap<Buf *, ConstExprValue *, buf_hash, buf_eql_buf> string_literals_table;
1510 HashMap<const TypeTableEntry *, ConstExprValue *, type_ptr_hash, type_ptr_eql> type_info_cache;
15101511
15111512
1512 ZigList<ImportTableEntry *> import_queue;1513 ZigList<ImportTableEntry *> import_queue;
src/codegen.cpp+9-1
...@@ -88,6 +88,7 @@ CodeGen *codegen_create(Buf *root_src_path, const ZigTarget *target, OutType out...@@ -88,6 +88,7 @@ CodeGen *codegen_create(Buf *root_src_path, const ZigTarget *target, OutType out
88 g->exported_symbol_names.init(8);88 g->exported_symbol_names.init(8);
89 g->external_prototypes.init(8);89 g->external_prototypes.init(8);
90 g->string_literals_table.init(16);90 g->string_literals_table.init(16);
91 g->type_info_cache.init(32);
91 g->is_test_build = false;92 g->is_test_build = false;
92 g->want_h_file = (out_type == OutTypeObj || out_type == OutTypeLib);93 g->want_h_file = (out_type == OutTypeObj || out_type == OutTypeLib);
93 buf_resize(&g->global_asm, 0);94 buf_resize(&g->global_asm, 0);
...@@ -6347,7 +6348,8 @@ static void define_builtin_compile_vars(CodeGen *g) {...@@ -6347,7 +6348,8 @@ static void define_builtin_compile_vars(CodeGen *g) {
6347 buf_appendf(contents, "};\n\n");6348 buf_appendf(contents, "};\n\n");
6348 }6349 }
6349 {6350 {
6350 // TODO: Add method info where methods are supported.6351 // @TODO Add method info where methods are supported.
6352 // @TODO Add Namespace info.
6351 buf_appendf(contents,6353 buf_appendf(contents,
6352 "pub const TypeInfo = union(TypeId) {\n"6354 "pub const TypeInfo = union(TypeId) {\n"
6353 " Type: void,\n"6355 " Type: void,\n"
...@@ -6403,6 +6405,11 @@ static void define_builtin_compile_vars(CodeGen *g) {...@@ -6403,6 +6405,11 @@ static void define_builtin_compile_vars(CodeGen *g) {
6403 " Packed,\n"6405 " Packed,\n"
6404 " };\n"6406 " };\n"
6405 "\n"6407 "\n"
6408 " pub const Method = struct {\n"
6409 " name: []const u8,\n"
6410 " fn_info: Fn,\n"
6411 " };\n"
6412 "\n"
6406 " pub const StructField = struct {\n"6413 " pub const StructField = struct {\n"
6407 " name: []const u8,\n"6414 " name: []const u8,\n"
6408 " offset: usize,\n"6415 " offset: usize,\n"
...@@ -6412,6 +6419,7 @@ static void define_builtin_compile_vars(CodeGen *g) {...@@ -6412,6 +6419,7 @@ static void define_builtin_compile_vars(CodeGen *g) {
6412 " pub const Struct = struct {\n"6419 " pub const Struct = struct {\n"
6413 " layout: ContainerLayout,\n"6420 " layout: ContainerLayout,\n"
6414 " fields: []StructField,\n"6421 " fields: []StructField,\n"
6422 " methods: []Method,\n"
6415 " };\n"6423 " };\n"
6416 "\n"6424 "\n"
6417 " pub const Nullable = struct {\n"6425 " pub const Nullable = struct {\n"
src/ir.cpp+60-49
...@@ -15810,6 +15810,15 @@ static TypeTableEntry *ir_type_info_get_type(IrAnalyze *ira, const char *type_na...@@ -15810,6 +15810,15 @@ static TypeTableEntry *ir_type_info_get_type(IrAnalyze *ira, const char *type_na
15810static ConstExprValue *ir_make_type_info_value(IrAnalyze *ira, ConstExprValue *parent,15810static ConstExprValue *ir_make_type_info_value(IrAnalyze *ira, ConstExprValue *parent,
15811 ssize_t parent_field_index, TypeTableEntry *type_entry)15811 ssize_t parent_field_index, TypeTableEntry *type_entry)
15812{15812{
15813 // Lookup an available value in our cache.
15814 auto entry = ira->codegen->type_info_cache.maybe_get(type_entry);
15815 if (entry != nullptr)
15816 return entry->value;
15817
15818 ConstExprValue *result = nullptr;
15819
15820 // @TODO
15821 // We should probably cache the values generated with a type_entry key.
15813 assert(type_entry != nullptr);15822 assert(type_entry != nullptr);
15814 assert(!type_is_invalid(type_entry));15823 assert(!type_is_invalid(type_entry));
1581515824
...@@ -15832,75 +15841,73 @@ static ConstExprValue *ir_make_type_info_value(IrAnalyze *ira, ConstExprValue *p...@@ -15832,75 +15841,73 @@ static ConstExprValue *ir_make_type_info_value(IrAnalyze *ira, ConstExprValue *p
15832 return nullptr;15841 return nullptr;
15833 case TypeTableEntryIdInt:15842 case TypeTableEntryIdInt:
15834 {15843 {
15835 ConstExprValue *payload = create_const_vals(1);15844 result = create_const_vals(1);
15836 payload->special = ConstValSpecialStatic;15845 result->special = ConstValSpecialStatic;
15837 payload->type = ir_type_info_get_type(ira, "Int");15846 result->type = ir_type_info_get_type(ira, "Int");
1583815847
15839 ConstExprValue *fields = create_const_vals(2);15848 ConstExprValue *fields = create_const_vals(2);
15840 payload->data.x_struct.fields = fields;15849 result->data.x_struct.fields = fields;
1584115850
15842 ir_type_info_struct_set_parent(payload, parent, parent_field_index);15851 ir_type_info_struct_set_parent(result, parent, parent_field_index);
1584315852
15844 // is_signed: bool15853 // is_signed: bool
15845 ensure_field_index(payload->type, "is_signed", 0);15854 ensure_field_index(result->type, "is_signed", 0);
15846 fields[0].special = ConstValSpecialStatic;15855 fields[0].special = ConstValSpecialStatic;
15847 fields[0].type = ira->codegen->builtin_types.entry_bool;15856 fields[0].type = ira->codegen->builtin_types.entry_bool;
15848 fields[0].data.x_bool = type_entry->data.integral.is_signed;15857 fields[0].data.x_bool = type_entry->data.integral.is_signed;
15849 // bits: u815858 // bits: u8
15850 ensure_field_index(payload->type, "bits", 1);15859 ensure_field_index(result->type, "bits", 1);
15851 fields[1].special = ConstValSpecialStatic;15860 fields[1].special = ConstValSpecialStatic;
15852 fields[1].type = ira->codegen->builtin_types.entry_u8;15861 fields[1].type = ira->codegen->builtin_types.entry_u8;
15853 bigint_init_unsigned(&fields[1].data.x_bigint, type_entry->data.integral.bit_count);15862 bigint_init_unsigned(&fields[1].data.x_bigint, type_entry->data.integral.bit_count);
1585415863 break;
15855 return payload;
15856 }15864 }
15857 case TypeTableEntryIdFloat:15865 case TypeTableEntryIdFloat:
15858 {15866 {
15859 ConstExprValue *payload = create_const_vals(1);15867 result = create_const_vals(1);
15860 payload->special = ConstValSpecialStatic;15868 result->special = ConstValSpecialStatic;
15861 payload->type = ir_type_info_get_type(ira, "Float");15869 result->type = ir_type_info_get_type(ira, "Float");
1586215870
15863 ConstExprValue *fields = create_const_vals(1);15871 ConstExprValue *fields = create_const_vals(1);
15864 payload->data.x_struct.fields = fields;15872 result->data.x_struct.fields = fields;
1586515873
15866 ir_type_info_struct_set_parent(payload, parent, parent_field_index);15874 ir_type_info_struct_set_parent(result, parent, parent_field_index);
1586715875
15868 // bits: u815876 // bits: u8
15869 ensure_field_index(payload->type, "bits", 0);15877 ensure_field_index(result->type, "bits", 0);
15870 fields[0].special = ConstValSpecialStatic;15878 fields[0].special = ConstValSpecialStatic;
15871 fields[0].type = ira->codegen->builtin_types.entry_u8;15879 fields[0].type = ira->codegen->builtin_types.entry_u8;
15872 bigint_init_unsigned(&fields->data.x_bigint, type_entry->data.floating.bit_count);15880 bigint_init_unsigned(&fields->data.x_bigint, type_entry->data.floating.bit_count);
1587315881 break;
15874 return payload;
15875 }15882 }
15876 case TypeTableEntryIdPointer:15883 case TypeTableEntryIdPointer:
15877 {15884 {
15878 ConstExprValue *payload = create_const_vals(1);15885 result = create_const_vals(1);
15879 payload->special = ConstValSpecialStatic;15886 result->special = ConstValSpecialStatic;
15880 payload->type = ir_type_info_get_type(ira, "Pointer");15887 result->type = ir_type_info_get_type(ira, "Pointer");
1588115888
15882 ConstExprValue *fields = create_const_vals(4);15889 ConstExprValue *fields = create_const_vals(4);
15883 payload->data.x_struct.fields = fields;15890 result->data.x_struct.fields = fields;
1588415891
15885 ir_type_info_struct_set_parent(payload, parent, parent_field_index);15892 ir_type_info_struct_set_parent(result, parent, parent_field_index);
1588615893
15887 // is_const: bool15894 // is_const: bool
15888 ensure_field_index(payload->type, "is_const", 0);15895 ensure_field_index(result->type, "is_const", 0);
15889 fields[0].special = ConstValSpecialStatic;15896 fields[0].special = ConstValSpecialStatic;
15890 fields[0].type = ira->codegen->builtin_types.entry_bool;15897 fields[0].type = ira->codegen->builtin_types.entry_bool;
15891 fields[0].data.x_bool = type_entry->data.pointer.is_const;15898 fields[0].data.x_bool = type_entry->data.pointer.is_const;
15892 // is_volatile: bool15899 // is_volatile: bool
15893 ensure_field_index(payload->type, "is_volatile", 1);15900 ensure_field_index(result->type, "is_volatile", 1);
15894 fields[1].special = ConstValSpecialStatic;15901 fields[1].special = ConstValSpecialStatic;
15895 fields[1].type = ira->codegen->builtin_types.entry_bool;15902 fields[1].type = ira->codegen->builtin_types.entry_bool;
15896 fields[1].data.x_bool = type_entry->data.pointer.is_volatile;15903 fields[1].data.x_bool = type_entry->data.pointer.is_volatile;
15897 // alignment: u3215904 // alignment: u32
15898 ensure_field_index(payload->type, "alignment", 2);15905 ensure_field_index(result->type, "alignment", 2);
15899 fields[2].special = ConstValSpecialStatic;15906 fields[2].special = ConstValSpecialStatic;
15900 fields[2].type = ira->codegen->builtin_types.entry_u32;15907 fields[2].type = ira->codegen->builtin_types.entry_u32;
15901 bigint_init_unsigned(&fields[2].data.x_bigint, type_entry->data.pointer.alignment);15908 bigint_init_unsigned(&fields[2].data.x_bigint, type_entry->data.pointer.alignment);
15902 // child: &TypeInfo15909 // child: &TypeInfo
15903 ensure_field_index(payload->type, "child", 3);15910 ensure_field_index(result->type, "child", 3);
1590415911
15905 TypeTableEntry *type_info_type = ir_type_info_get_type(ira, nullptr);15912 TypeTableEntry *type_info_type = ir_type_info_get_type(ira, nullptr);
1590615913
...@@ -15917,26 +15924,26 @@ static ConstExprValue *ir_make_type_info_value(IrAnalyze *ira, ConstExprValue *p...@@ -15917,26 +15924,26 @@ static ConstExprValue *ir_make_type_info_value(IrAnalyze *ira, ConstExprValue *p
15917 type_entry->data.pointer.child_type);15924 type_entry->data.pointer.child_type);
1591815925
15919 fields[3].data.x_ptr.data.ref.pointee = union_val;15926 fields[3].data.x_ptr.data.ref.pointee = union_val;
15920 return payload;15927 break;
15921 }15928 }
15922 case TypeTableEntryIdArray:15929 case TypeTableEntryIdArray:
15923 {15930 {
15924 ConstExprValue *payload = create_const_vals(1);15931 result = create_const_vals(1);
15925 payload->special = ConstValSpecialStatic;15932 result->special = ConstValSpecialStatic;
15926 payload->type = ir_type_info_get_type(ira, "Array");15933 result->type = ir_type_info_get_type(ira, "Array");
1592715934
15928 ConstExprValue *fields = create_const_vals(2);15935 ConstExprValue *fields = create_const_vals(2);
15929 payload->data.x_struct.fields = fields;15936 result->data.x_struct.fields = fields;
1593015937
15931 ir_type_info_struct_set_parent(payload, parent, parent_field_index);15938 ir_type_info_struct_set_parent(result, parent, parent_field_index);
1593215939
15933 // len: usize15940 // len: usize
15934 ensure_field_index(payload->type, "len", 0);15941 ensure_field_index(result->type, "len", 0);
15935 fields[0].special = ConstValSpecialStatic;15942 fields[0].special = ConstValSpecialStatic;
15936 fields[0].type = ira->codegen->builtin_types.entry_usize;15943 fields[0].type = ira->codegen->builtin_types.entry_usize;
15937 bigint_init_unsigned(&fields[0].data.x_bigint, type_entry->data.array.len);15944 bigint_init_unsigned(&fields[0].data.x_bigint, type_entry->data.array.len);
15938 // child: &TypeInfo15945 // child: &TypeInfo
15939 ensure_field_index(payload->type, "child", 1);15946 ensure_field_index(result->type, "child", 1);
1594015947
15941 TypeTableEntry *type_info_type = ir_type_info_get_type(ira, nullptr);15948 TypeTableEntry *type_info_type = ir_type_info_get_type(ira, nullptr);
1594215949
...@@ -15953,21 +15960,21 @@ static ConstExprValue *ir_make_type_info_value(IrAnalyze *ira, ConstExprValue *p...@@ -15953,21 +15960,21 @@ static ConstExprValue *ir_make_type_info_value(IrAnalyze *ira, ConstExprValue *p
15953 type_entry->data.array.child_type);15960 type_entry->data.array.child_type);
1595415961
15955 fields[1].data.x_ptr.data.ref.pointee = union_val;15962 fields[1].data.x_ptr.data.ref.pointee = union_val;
15956 return payload;15963 break;
15957 }15964 }
15958 case TypeTableEntryIdMaybe:15965 case TypeTableEntryIdMaybe:
15959 {15966 {
15960 ConstExprValue *payload = create_const_vals(1);15967 result = create_const_vals(1);
15961 payload->special = ConstValSpecialStatic;15968 result->special = ConstValSpecialStatic;
15962 payload->type = ir_type_info_get_type(ira, "Nullable");15969 result->type = ir_type_info_get_type(ira, "Nullable");
1596315970
15964 ConstExprValue *fields = create_const_vals(1);15971 ConstExprValue *fields = create_const_vals(1);
15965 payload->data.x_struct.fields = fields;15972 result->data.x_struct.fields = fields;
1596615973
15967 ir_type_info_struct_set_parent(payload, parent, parent_field_index);15974 ir_type_info_struct_set_parent(result, parent, parent_field_index);
1596815975
15969 // child: &TypeInfo15976 // child: &TypeInfo
15970 ensure_field_index(payload->type, "child", 0);15977 ensure_field_index(result->type, "child", 0);
1597115978
15972 TypeTableEntry *type_info_type = ir_type_info_get_type(ira, nullptr);15979 TypeTableEntry *type_info_type = ir_type_info_get_type(ira, nullptr);
1597315980
...@@ -15984,21 +15991,21 @@ static ConstExprValue *ir_make_type_info_value(IrAnalyze *ira, ConstExprValue *p...@@ -15984,21 +15991,21 @@ static ConstExprValue *ir_make_type_info_value(IrAnalyze *ira, ConstExprValue *p
15984 type_entry->data.maybe.child_type);15991 type_entry->data.maybe.child_type);
1598515992
15986 fields[0].data.x_ptr.data.ref.pointee = union_val;15993 fields[0].data.x_ptr.data.ref.pointee = union_val;
15987 return payload;15994 break;
15988 }15995 }
15989 case TypeTableEntryIdPromise:15996 case TypeTableEntryIdPromise:
15990 {15997 {
15991 ConstExprValue *payload = create_const_vals(1);15998 result = create_const_vals(1);
15992 payload->special = ConstValSpecialStatic;15999 result->special = ConstValSpecialStatic;
15993 payload->type = ir_type_info_get_type(ira, "Promise");16000 result->type = ir_type_info_get_type(ira, "Promise");
1599416001
15995 ConstExprValue *fields = create_const_vals(1);16002 ConstExprValue *fields = create_const_vals(1);
15996 payload->data.x_struct.fields = fields;16003 result->data.x_struct.fields = fields;
1599716004
15998 ir_type_info_struct_set_parent(payload, parent, parent_field_index);16005 ir_type_info_struct_set_parent(result, parent, parent_field_index);
1599916006
16000 // child: ?&TypeInfo16007 // child: ?&TypeInfo
16001 ensure_field_index(payload->type, "child", 0);16008 ensure_field_index(result->type, "child", 0);
1600216009
16003 TypeTableEntry *type_info_type = ir_type_info_get_type(ira, nullptr);16010 TypeTableEntry *type_info_type = ir_type_info_get_type(ira, nullptr);
16004 TypeTableEntry *type_info_ptr_type = get_pointer_to_type(ira->codegen, type_info_type, false);16011 TypeTableEntry *type_info_ptr_type = get_pointer_to_type(ira->codegen, type_info_type, false);
...@@ -16029,11 +16036,15 @@ static ConstExprValue *ir_make_type_info_value(IrAnalyze *ira, ConstExprValue *p...@@ -16029,11 +16036,15 @@ static ConstExprValue *ir_make_type_info_value(IrAnalyze *ira, ConstExprValue *p
16029 maybe_value->data.x_ptr.data.ref.pointee = union_val;16036 maybe_value->data.x_ptr.data.ref.pointee = union_val;
16030 fields[0].data.x_maybe = maybe_value;16037 fields[0].data.x_maybe = maybe_value;
16031 }16038 }
16032 return payload;16039 break;
16033 }16040 }
16034 default:16041 default:
16035 zig_unreachable();16042 zig_unreachable();
16036 }16043 }
16044
16045 assert(result != nullptr);
16046 ira->codegen->type_info_cache.put(type_entry, result);
16047 return result;
16037}16048}
1603816049
16039static TypeTableEntry *ir_analyze_instruction_type_info(IrAnalyze *ira,16050static TypeTableEntry *ir_analyze_instruction_type_info(IrAnalyze *ira,