# HG changeset patch # User Paul Boddie # Date 1636499444 -3600 # Node ID 74b4f0b3d1d967dcb80e8d945290e2161f90010b # Parent db49ed1bde3b16fa9a00de619aeba54c5c53043f Added trailing data type sizes to object tables. diff -r db49ed1bde3b -r 74b4f0b3d1d9 generator.py --- a/generator.py Tue Nov 09 22:11:58 2021 +0100 +++ b/generator.py Wed Nov 10 00:10:44 2021 +0100 @@ -278,7 +278,8 @@ table = [] self.populate_table(Reference(kind, path), table) - self.write_table(f_decls, f_defs, table_name, structure_size, table) + self.write_table(f_decls, f_defs, table_name, structure_size, table, + self.get_trailing_data_type(Reference(kind, path))) # Generate function instances. @@ -770,14 +771,20 @@ f_consts.write(" %s = %d" % (pos_encoder(attrname), i)) print >>f_consts, "\n };" - def write_table(self, f_decls, f_defs, table_name, structure_size, table): + def write_table(self, f_decls, f_defs, table_name, structure_size, table, + trailing_data_type): """ Write the declarations to 'f_decls' and definitions to 'f_defs' for the object having the given 'table_name' and the given 'structure_size', with 'table' details used to populate the definition. + + To support value copying, the trailing data size is also written if + 'trailing_data_type' is defined. """ + trailing_size = trailing_data_type and "sizeof(%s)" % trailing_data_type or "0" + print >>f_decls, "extern const __table %s;\n" % table_name # Write the corresponding definition. @@ -785,11 +792,12 @@ print >>f_defs, """\ const __table %s = { %s, + %s, { %s } }; -""" % (table_name, structure_size, +""" % (table_name, structure_size, trailing_size, ",\n ".join(table)) def write_parameter_table(self, f_decls, f_defs, table_name, min_parameters, @@ -1141,13 +1149,24 @@ the 'attrs' mapping, adding entries to the 'trailing' member collection. """ + if self.get_trailing_data_type(ref): + trailing.append(encode_literal_constant_value(attrs["__trailing__"])) + + def get_trailing_data_type(self, ref): + + """ + For the structure having the given 'ref', return the type of any + trailing data. + """ + structure_ref = self.get_target_structure(ref) # Instances with trailing data. - if structure_ref.get_kind() == "" and \ - structure_ref.get_origin() in self.trailing_data_types: - trailing.append(encode_literal_constant_value(attrs["__trailing__"])) + if structure_ref.get_kind() == "": + return self.trailing_data_types.get(structure_ref.get_origin()) + else: + return None def get_target_structure(self, ref): diff -r db49ed1bde3b -r 74b4f0b3d1d9 templates/types.h --- a/templates/types.h Tue Nov 09 22:11:58 2021 +0100 +++ b/templates/types.h Wed Nov 10 00:10:44 2021 +0100 @@ -42,6 +42,7 @@ typedef struct __table { const __pos size; + const size_t trailing_size; const __code attrs[]; } __table; @@ -120,7 +121,7 @@ } __obj; -#define __INSTANCE_SIZE(NUMBER) ((NUMBER) * sizeof(__attr) + sizeof(__table *) + sizeof(__ppos)) +#define __INSTANCE_SIZE(REF) ((REF)->table->size * sizeof(__attr) + sizeof(__table *) + sizeof(__ppos) + (REF)->table->trailing_size) /* Fragments are simple collections of attributes employed by sequence types. They provide the basis of lists and tuples. */