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 {
15071507 HashMap<Buf *, AstNode *, buf_hash, buf_eql_buf> exported_symbol_names;
15081508 HashMap<Buf *, Tld *, buf_hash, buf_eql_buf> external_prototypes;
15091509 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
15121513 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
8888 g->exported_symbol_names.init(8);
8989 g->external_prototypes.init(8);
9090 g->string_literals_table.init(16);
91 g->type_info_cache.init(32);
9192 g->is_test_build = false;
9293 g->want_h_file = (out_type == OutTypeObj || out_type == OutTypeLib);
9394 buf_resize(&g->global_asm, 0);
......@@ -6347,7 +6348,8 @@ static void define_builtin_compile_vars(CodeGen *g) {
63476348 buf_appendf(contents, "};\n\n");
63486349 }
63496350 {
6350 // TODO: Add method info where methods are supported.
6351 // @TODO Add method info where methods are supported.
6352 // @TODO Add Namespace info.
63516353 buf_appendf(contents,
63526354 "pub const TypeInfo = union(TypeId) {\n"
63536355 " Type: void,\n"
......@@ -6403,6 +6405,11 @@ static void define_builtin_compile_vars(CodeGen *g) {
64036405 " Packed,\n"
64046406 " };\n"
64056407 "\n"
6408 " pub const Method = struct {\n"
6409 " name: []const u8,\n"
6410 " fn_info: Fn,\n"
6411 " };\n"
6412 "\n"
64066413 " pub const StructField = struct {\n"
64076414 " name: []const u8,\n"
64086415 " offset: usize,\n"
......@@ -6412,6 +6419,7 @@ static void define_builtin_compile_vars(CodeGen *g) {
64126419 " pub const Struct = struct {\n"
64136420 " layout: ContainerLayout,\n"
64146421 " fields: []StructField,\n"
6422 " methods: []Method,\n"
64156423 " };\n"
64166424 "\n"
64176425 " 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
1581015810static ConstExprValue *ir_make_type_info_value(IrAnalyze *ira, ConstExprValue *parent,
1581115811 ssize_t parent_field_index, TypeTableEntry *type_entry)
1581215812{
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.
1581315822 assert(type_entry != nullptr);
1581415823 assert(!type_is_invalid(type_entry));
1581515824
......@@ -15832,75 +15841,73 @@ static ConstExprValue *ir_make_type_info_value(IrAnalyze *ira, ConstExprValue *p
1583215841 return nullptr;
1583315842 case TypeTableEntryIdInt:
1583415843 {
15835 ConstExprValue *payload = create_const_vals(1);
15836 payload->special = ConstValSpecialStatic;
15837 payload->type = ir_type_info_get_type(ira, "Int");
15844 result = create_const_vals(1);
15845 result->special = ConstValSpecialStatic;
15846 result->type = ir_type_info_get_type(ira, "Int");
1583815847
1583915848 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
1584415853 // is_signed: bool
15845 ensure_field_index(payload->type, "is_signed", 0);
15854 ensure_field_index(result->type, "is_signed", 0);
1584615855 fields[0].special = ConstValSpecialStatic;
1584715856 fields[0].type = ira->codegen->builtin_types.entry_bool;
1584815857 fields[0].data.x_bool = type_entry->data.integral.is_signed;
1584915858 // bits: u8
15850 ensure_field_index(payload->type, "bits", 1);
15859 ensure_field_index(result->type, "bits", 1);
1585115860 fields[1].special = ConstValSpecialStatic;
1585215861 fields[1].type = ira->codegen->builtin_types.entry_u8;
1585315862 bigint_init_unsigned(&fields[1].data.x_bigint, type_entry->data.integral.bit_count);
15854
15855 return payload;
15863 break;
1585615864 }
1585715865 case TypeTableEntryIdFloat:
1585815866 {
15859 ConstExprValue *payload = create_const_vals(1);
15860 payload->special = ConstValSpecialStatic;
15861 payload->type = ir_type_info_get_type(ira, "Float");
15867 result = create_const_vals(1);
15868 result->special = ConstValSpecialStatic;
15869 result->type = ir_type_info_get_type(ira, "Float");
1586215870
1586315871 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
1586815876 // bits: u8
15869 ensure_field_index(payload->type, "bits", 0);
15877 ensure_field_index(result->type, "bits", 0);
1587015878 fields[0].special = ConstValSpecialStatic;
1587115879 fields[0].type = ira->codegen->builtin_types.entry_u8;
1587215880 bigint_init_unsigned(&fields->data.x_bigint, type_entry->data.floating.bit_count);
15873
15874 return payload;
15881 break;
1587515882 }
1587615883 case TypeTableEntryIdPointer:
1587715884 {
15878 ConstExprValue *payload = create_const_vals(1);
15879 payload->special = ConstValSpecialStatic;
15880 payload->type = ir_type_info_get_type(ira, "Pointer");
15885 result = create_const_vals(1);
15886 result->special = ConstValSpecialStatic;
15887 result->type = ir_type_info_get_type(ira, "Pointer");
1588115888
1588215889 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
1588715894 // is_const: bool
15888 ensure_field_index(payload->type, "is_const", 0);
15895 ensure_field_index(result->type, "is_const", 0);
1588915896 fields[0].special = ConstValSpecialStatic;
1589015897 fields[0].type = ira->codegen->builtin_types.entry_bool;
1589115898 fields[0].data.x_bool = type_entry->data.pointer.is_const;
1589215899 // is_volatile: bool
15893 ensure_field_index(payload->type, "is_volatile", 1);
15900 ensure_field_index(result->type, "is_volatile", 1);
1589415901 fields[1].special = ConstValSpecialStatic;
1589515902 fields[1].type = ira->codegen->builtin_types.entry_bool;
1589615903 fields[1].data.x_bool = type_entry->data.pointer.is_volatile;
1589715904 // alignment: u32
15898 ensure_field_index(payload->type, "alignment", 2);
15905 ensure_field_index(result->type, "alignment", 2);
1589915906 fields[2].special = ConstValSpecialStatic;
1590015907 fields[2].type = ira->codegen->builtin_types.entry_u32;
1590115908 bigint_init_unsigned(&fields[2].data.x_bigint, type_entry->data.pointer.alignment);
1590215909 // child: &TypeInfo
15903 ensure_field_index(payload->type, "child", 3);
15910 ensure_field_index(result->type, "child", 3);
1590415911
1590515912 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
1591715924 type_entry->data.pointer.child_type);
1591815925
1591915926 fields[3].data.x_ptr.data.ref.pointee = union_val;
15920 return payload;
15927 break;
1592115928 }
1592215929 case TypeTableEntryIdArray:
1592315930 {
15924 ConstExprValue *payload = create_const_vals(1);
15925 payload->special = ConstValSpecialStatic;
15926 payload->type = ir_type_info_get_type(ira, "Array");
15931 result = create_const_vals(1);
15932 result->special = ConstValSpecialStatic;
15933 result->type = ir_type_info_get_type(ira, "Array");
1592715934
1592815935 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
1593315940 // len: usize
15934 ensure_field_index(payload->type, "len", 0);
15941 ensure_field_index(result->type, "len", 0);
1593515942 fields[0].special = ConstValSpecialStatic;
1593615943 fields[0].type = ira->codegen->builtin_types.entry_usize;
1593715944 bigint_init_unsigned(&fields[0].data.x_bigint, type_entry->data.array.len);
1593815945 // child: &TypeInfo
15939 ensure_field_index(payload->type, "child", 1);
15946 ensure_field_index(result->type, "child", 1);
1594015947
1594115948 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
1595315960 type_entry->data.array.child_type);
1595415961
1595515962 fields[1].data.x_ptr.data.ref.pointee = union_val;
15956 return payload;
15963 break;
1595715964 }
1595815965 case TypeTableEntryIdMaybe:
1595915966 {
15960 ConstExprValue *payload = create_const_vals(1);
15961 payload->special = ConstValSpecialStatic;
15962 payload->type = ir_type_info_get_type(ira, "Nullable");
15967 result = create_const_vals(1);
15968 result->special = ConstValSpecialStatic;
15969 result->type = ir_type_info_get_type(ira, "Nullable");
1596315970
1596415971 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
1596915976 // child: &TypeInfo
15970 ensure_field_index(payload->type, "child", 0);
15977 ensure_field_index(result->type, "child", 0);
1597115978
1597215979 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
1598415991 type_entry->data.maybe.child_type);
1598515992
1598615993 fields[0].data.x_ptr.data.ref.pointee = union_val;
15987 return payload;
15994 break;
1598815995 }
1598915996 case TypeTableEntryIdPromise:
1599015997 {
15991 ConstExprValue *payload = create_const_vals(1);
15992 payload->special = ConstValSpecialStatic;
15993 payload->type = ir_type_info_get_type(ira, "Promise");
15998 result = create_const_vals(1);
15999 result->special = ConstValSpecialStatic;
16000 result->type = ir_type_info_get_type(ira, "Promise");
1599416001
1599516002 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
1600016007 // child: ?&TypeInfo
16001 ensure_field_index(payload->type, "child", 0);
16008 ensure_field_index(result->type, "child", 0);
1600216009
1600316010 TypeTableEntry *type_info_type = ir_type_info_get_type(ira, nullptr);
1600416011 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
1602916036 maybe_value->data.x_ptr.data.ref.pointee = union_val;
1603016037 fields[0].data.x_maybe = maybe_value;
1603116038 }
16032 return payload;
16039 break;
1603316040 }
1603416041 default:
1603516042 zig_unreachable();
1603616043 }
16044
16045 assert(result != nullptr);
16046 ira->codegen->type_info_cache.put(type_entry, result);
16047 return result;
1603716048}
1603816049
1603916050static TypeTableEntry *ir_analyze_instruction_type_info(IrAnalyze *ira,