paul@126 | 1 | #!/usr/bin/env python |
paul@126 | 2 | |
paul@126 | 3 | """ |
paul@126 | 4 | Generate C code from object layouts and other deduced information. |
paul@126 | 5 | |
paul@126 | 6 | Copyright (C) 2015, 2016 Paul Boddie <paul@boddie.org.uk> |
paul@126 | 7 | |
paul@126 | 8 | This program is free software; you can redistribute it and/or modify it under |
paul@126 | 9 | the terms of the GNU General Public License as published by the Free Software |
paul@126 | 10 | Foundation; either version 3 of the License, or (at your option) any later |
paul@126 | 11 | version. |
paul@126 | 12 | |
paul@126 | 13 | This program is distributed in the hope that it will be useful, but WITHOUT |
paul@126 | 14 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
paul@126 | 15 | FOR A PARTICULAR PURPOSE. See the GNU General Public License for more |
paul@126 | 16 | details. |
paul@126 | 17 | |
paul@126 | 18 | You should have received a copy of the GNU General Public License along with |
paul@126 | 19 | this program. If not, see <http://www.gnu.org/licenses/>. |
paul@126 | 20 | """ |
paul@126 | 21 | |
paul@126 | 22 | from common import CommonOutput |
paul@126 | 23 | from encoders import encode_bound_reference, encode_function_pointer, \ |
paul@126 | 24 | encode_instantiator_pointer, encode_path, encode_symbol |
paul@126 | 25 | from os import listdir |
paul@126 | 26 | from os.path import isdir, join, split |
paul@126 | 27 | from referencing import Reference |
paul@126 | 28 | |
paul@126 | 29 | def copy(source, target): |
paul@126 | 30 | |
paul@126 | 31 | "Copy a text file from 'source' to 'target'." |
paul@126 | 32 | |
paul@126 | 33 | if isdir(target): |
paul@126 | 34 | target = join(target, split(source)[-1]) |
paul@126 | 35 | infile = open(source) |
paul@126 | 36 | outfile = open(target, "w") |
paul@126 | 37 | try: |
paul@126 | 38 | outfile.write(infile.read()) |
paul@126 | 39 | finally: |
paul@126 | 40 | outfile.close() |
paul@126 | 41 | infile.close() |
paul@126 | 42 | |
paul@126 | 43 | class Generator(CommonOutput): |
paul@126 | 44 | |
paul@126 | 45 | "A code generator." |
paul@126 | 46 | |
paul@126 | 47 | function_type = "__builtins__.core.function" |
paul@126 | 48 | |
paul@126 | 49 | table_name_prefixes = { |
paul@126 | 50 | "<class>" : "Class", |
paul@126 | 51 | "<module>" : "Module", |
paul@126 | 52 | "<instance>" : "Instance" |
paul@126 | 53 | } |
paul@126 | 54 | |
paul@126 | 55 | structure_size_prefixes = { |
paul@126 | 56 | "<class>" : "c", |
paul@126 | 57 | "<module>" : "m", |
paul@126 | 58 | "<instance>" : "i" |
paul@126 | 59 | } |
paul@126 | 60 | |
paul@126 | 61 | def __init__(self, importer, optimiser, output): |
paul@126 | 62 | self.importer = importer |
paul@126 | 63 | self.optimiser = optimiser |
paul@126 | 64 | self.output = output |
paul@126 | 65 | |
paul@126 | 66 | def to_output(self): |
paul@126 | 67 | |
paul@126 | 68 | "Write the generated code." |
paul@126 | 69 | |
paul@126 | 70 | self.check_output() |
paul@126 | 71 | self.write_structures() |
paul@126 | 72 | self.copy_templates() |
paul@126 | 73 | |
paul@126 | 74 | def copy_templates(self): |
paul@126 | 75 | |
paul@126 | 76 | "Copy template files to the generated output directory." |
paul@126 | 77 | |
paul@126 | 78 | templates = join(split(__file__)[0], "templates") |
paul@126 | 79 | |
paul@126 | 80 | for filename in listdir(templates): |
paul@126 | 81 | copy(join(templates, filename), self.output) |
paul@126 | 82 | |
paul@126 | 83 | def write_structures(self): |
paul@126 | 84 | |
paul@126 | 85 | "Write structures used by the program." |
paul@126 | 86 | |
paul@126 | 87 | f_consts = open(join(self.output, "progconsts.h"), "w") |
paul@126 | 88 | f_defs = open(join(self.output, "progtypes.c"), "w") |
paul@126 | 89 | f_decls = open(join(self.output, "progtypes.h"), "w") |
paul@126 | 90 | f_signatures = open(join(self.output, "main.h"), "w") |
paul@126 | 91 | f_code = open(join(self.output, "main.c"), "w") |
paul@126 | 92 | |
paul@126 | 93 | try: |
paul@126 | 94 | # Output boilerplate. |
paul@126 | 95 | |
paul@126 | 96 | print >>f_consts, """\ |
paul@126 | 97 | #ifndef __PROGCONSTS_H__ |
paul@126 | 98 | #define __PROGCONSTS_H__ |
paul@126 | 99 | """ |
paul@126 | 100 | print >>f_decls, """\ |
paul@126 | 101 | #ifndef __PROGTYPES_H__ |
paul@126 | 102 | #define __PROGTYPES_H__ |
paul@126 | 103 | |
paul@126 | 104 | #include "progconsts.h" |
paul@126 | 105 | #include "types.h" |
paul@126 | 106 | """ |
paul@126 | 107 | print >>f_defs, """\ |
paul@126 | 108 | #include "progtypes.h" |
paul@126 | 109 | #include "main.h" |
paul@126 | 110 | """ |
paul@126 | 111 | print >>f_signatures, """\ |
paul@126 | 112 | #ifndef __MAIN_H__ |
paul@126 | 113 | #define __MAIN_H__ |
paul@126 | 114 | |
paul@126 | 115 | #include "types.h" |
paul@126 | 116 | """ |
paul@126 | 117 | print >>f_code, """\ |
paul@126 | 118 | #include <string.h> |
paul@126 | 119 | #include "types.h" |
paul@126 | 120 | #include "ops.h" |
paul@126 | 121 | #include "progconsts.h" |
paul@126 | 122 | #include "progtypes.h" |
paul@126 | 123 | #include "progops.h" |
paul@126 | 124 | #include "main.h" |
paul@126 | 125 | """ |
paul@126 | 126 | |
paul@126 | 127 | # Generate structure size data. |
paul@126 | 128 | |
paul@126 | 129 | size_tables = {} |
paul@126 | 130 | |
paul@126 | 131 | for kind in ["<class>", "<module>", "<instance>"]: |
paul@126 | 132 | size_tables[kind] = {} |
paul@126 | 133 | |
paul@126 | 134 | for ref, structure in self.optimiser.structures.items(): |
paul@126 | 135 | size_tables[ref.get_kind()][ref.get_origin()] = len(structure) |
paul@126 | 136 | |
paul@126 | 137 | size_tables = size_tables.items() |
paul@126 | 138 | size_tables.sort() |
paul@126 | 139 | |
paul@126 | 140 | for kind, sizes in size_tables: |
paul@126 | 141 | self.write_size_constants(f_consts, self.structure_size_prefixes[kind], sizes, 0) |
paul@126 | 142 | |
paul@126 | 143 | # Generate parameter table size data. |
paul@126 | 144 | |
paul@126 | 145 | min_sizes = {} |
paul@126 | 146 | max_sizes = {} |
paul@126 | 147 | |
paul@126 | 148 | for path, parameters in self.optimiser.parameters.items(): |
paul@126 | 149 | argmin, argmax = self.get_argument_limits(path) |
paul@126 | 150 | min_sizes[path] = argmin |
paul@126 | 151 | max_sizes[path] = argmax |
paul@126 | 152 | |
paul@126 | 153 | # Record instantiator limits. |
paul@126 | 154 | |
paul@126 | 155 | if path.endswith(".__init__"): |
paul@126 | 156 | path = path[:-len(".__init__")] |
paul@126 | 157 | min_sizes[path] = argmin - 1 |
paul@126 | 158 | max_sizes[path] = argmax - 1 |
paul@126 | 159 | |
paul@126 | 160 | self.write_size_constants(f_consts, "pmin", min_sizes, 0) |
paul@126 | 161 | self.write_size_constants(f_consts, "pmax", max_sizes, 0) |
paul@126 | 162 | |
paul@126 | 163 | # Generate attribute codes. |
paul@126 | 164 | |
paul@126 | 165 | self.write_code_constants(f_consts, self.optimiser.all_attrnames, self.optimiser.locations) |
paul@126 | 166 | |
paul@126 | 167 | # Generate table and structure data. |
paul@126 | 168 | |
paul@126 | 169 | function_instance_attrs = None |
paul@126 | 170 | objects = self.optimiser.attr_table.items() |
paul@126 | 171 | objects.sort() |
paul@126 | 172 | |
paul@126 | 173 | for ref, indexes in objects: |
paul@126 | 174 | attrnames = self.get_attribute_names(indexes) |
paul@126 | 175 | |
paul@126 | 176 | kind = ref.get_kind() |
paul@126 | 177 | path = ref.get_origin() |
paul@126 | 178 | table_name = encode_tablename(self.table_name_prefixes[kind], path) |
paul@126 | 179 | structure_size = encode_size(self.structure_size_prefixes[kind], path) |
paul@126 | 180 | |
paul@126 | 181 | # Generate structures for classes and modules. |
paul@126 | 182 | |
paul@126 | 183 | if kind != "<instance>": |
paul@126 | 184 | structure = [] |
paul@126 | 185 | attrs = self.get_static_attributes(kind, path, attrnames) |
paul@126 | 186 | |
paul@126 | 187 | # Set a special instantiator on the class. |
paul@126 | 188 | |
paul@126 | 189 | if kind == "<class>": |
paul@126 | 190 | attrs["__fn__"] = path |
paul@126 | 191 | attrs["__args__"] = encode_size("pmin", path) |
paul@126 | 192 | |
paul@126 | 193 | # Write instantiator declarations based on the |
paul@126 | 194 | # applicable initialiser. |
paul@126 | 195 | |
paul@126 | 196 | init_ref = attrs["__init__"] |
paul@126 | 197 | |
paul@126 | 198 | # Signature: __attr __new_<name>(__attr[]); |
paul@126 | 199 | |
paul@126 | 200 | print >>f_signatures, "__attr %s(__attr[]);" % encode_instantiator_pointer(path) |
paul@126 | 201 | |
paul@126 | 202 | # Write instantiator definitions. |
paul@126 | 203 | |
paul@126 | 204 | self.write_instantiator(f_code, path, init_ref) |
paul@126 | 205 | |
paul@126 | 206 | # Write parameter table. |
paul@126 | 207 | |
paul@126 | 208 | self.make_parameter_table(f_decls, f_defs, path, init_ref.get_origin()) |
paul@126 | 209 | |
paul@126 | 210 | self.populate_structure(Reference(kind, path), attrs, kind, structure) |
paul@126 | 211 | self.write_structure(f_decls, f_defs, path, table_name, structure_size, structure) |
paul@126 | 212 | |
paul@126 | 213 | # Record function instance details for function generation below. |
paul@126 | 214 | |
paul@126 | 215 | else: |
paul@126 | 216 | attrs = self.get_instance_attributes(path, attrnames) |
paul@126 | 217 | if path == self.function_type: |
paul@126 | 218 | function_instance_attrs = attrs |
paul@126 | 219 | |
paul@126 | 220 | # Write a table for all objects. |
paul@126 | 221 | |
paul@126 | 222 | table = [] |
paul@126 | 223 | self.populate_table(Reference(kind, path), table) |
paul@126 | 224 | self.write_table(f_decls, f_defs, table_name, structure_size, table) |
paul@126 | 225 | |
paul@126 | 226 | # Generate function instances. |
paul@126 | 227 | |
paul@126 | 228 | functions = set() |
paul@126 | 229 | |
paul@126 | 230 | for ref in self.importer.objects.values(): |
paul@126 | 231 | if ref.has_kind("<function>"): |
paul@126 | 232 | functions.add(ref.get_origin()) |
paul@126 | 233 | |
paul@126 | 234 | functions = list(functions) |
paul@126 | 235 | functions.sort() |
paul@126 | 236 | |
paul@126 | 237 | for path in functions: |
paul@126 | 238 | cls = self.function_type |
paul@126 | 239 | table_name = encode_tablename("Instance", cls) |
paul@126 | 240 | structure_size = encode_size(self.structure_size_prefixes["<instance>"], cls) |
paul@126 | 241 | |
paul@126 | 242 | # Set a special callable attribute on the instance. |
paul@126 | 243 | |
paul@126 | 244 | function_instance_attrs["__fn__"] = path |
paul@126 | 245 | function_instance_attrs["__args__"] = encode_size("pmin", path) |
paul@126 | 246 | |
paul@126 | 247 | # Produce two structures where a method is involved. |
paul@126 | 248 | |
paul@126 | 249 | ref = self.importer.get_object(path) |
paul@126 | 250 | parent_ref = self.importer.get_object(ref.parent()) |
paul@126 | 251 | parent_kind = parent_ref and parent_ref.get_kind() |
paul@126 | 252 | |
paul@126 | 253 | # Populate and write each structure. |
paul@126 | 254 | |
paul@126 | 255 | if parent_kind == "<class>": |
paul@126 | 256 | |
paul@126 | 257 | # An unbound version of a method. |
paul@126 | 258 | |
paul@126 | 259 | structure = self.populate_function(path, function_instance_attrs, True) |
paul@126 | 260 | self.write_structure(f_decls, f_defs, path, table_name, structure_size, structure) |
paul@126 | 261 | |
paul@126 | 262 | # A bound version of a method. |
paul@126 | 263 | |
paul@126 | 264 | structure = self.populate_function(path, function_instance_attrs, False) |
paul@126 | 265 | self.write_structure(f_decls, f_defs, encode_bound_reference(path), table_name, structure_size, structure) |
paul@126 | 266 | |
paul@126 | 267 | # A normal function. |
paul@126 | 268 | |
paul@126 | 269 | structure = self.populate_function(path, function_instance_attrs, False) |
paul@126 | 270 | self.write_structure(f_decls, f_defs, path, table_name, structure_size, structure) |
paul@126 | 271 | |
paul@126 | 272 | # Write function declarations. |
paul@126 | 273 | # Signature: __attr <name>(__attr[]); |
paul@126 | 274 | |
paul@126 | 275 | print >>f_signatures, "__attr %s(__attr args[]);" % encode_function_pointer(path) |
paul@126 | 276 | |
paul@126 | 277 | # Write parameter table. |
paul@126 | 278 | |
paul@126 | 279 | self.make_parameter_table(f_decls, f_defs, path, path) |
paul@126 | 280 | |
paul@126 | 281 | # Output more boilerplate. |
paul@126 | 282 | |
paul@126 | 283 | print >>f_consts, """\ |
paul@126 | 284 | |
paul@126 | 285 | #endif /* __PROGCONSTS_H__ */""" |
paul@126 | 286 | |
paul@126 | 287 | print >>f_decls, """\ |
paul@126 | 288 | |
paul@126 | 289 | #define __FUNCTION_TYPE %s |
paul@126 | 290 | #define __FUNCTION_INSTANCE_SIZE %s |
paul@126 | 291 | |
paul@126 | 292 | #endif /* __PROGTYPES_H__ */""" % ( |
paul@126 | 293 | encode_path(self.function_type), |
paul@126 | 294 | encode_size(self.structure_size_prefixes["<instance>"], self.function_type) |
paul@126 | 295 | ) |
paul@126 | 296 | |
paul@126 | 297 | print >>f_signatures, """\ |
paul@126 | 298 | |
paul@126 | 299 | #endif /* __MAIN_H__ */""" |
paul@126 | 300 | |
paul@126 | 301 | finally: |
paul@126 | 302 | f_consts.close() |
paul@126 | 303 | f_defs.close() |
paul@126 | 304 | f_decls.close() |
paul@126 | 305 | f_signatures.close() |
paul@126 | 306 | f_code.close() |
paul@126 | 307 | |
paul@126 | 308 | def make_parameter_table(self, f_decls, f_defs, path, function_path): |
paul@126 | 309 | |
paul@126 | 310 | """ |
paul@126 | 311 | Write parameter table details to 'f_decls' (to declare a table) and to |
paul@126 | 312 | 'f_defs' (to define the contents) for the function with the given |
paul@126 | 313 | 'path', using 'function_path' to obtain the parameter details. The |
paul@126 | 314 | latter two arguments may differ when describing an instantiator using |
paul@126 | 315 | the details of an initialiser. |
paul@126 | 316 | """ |
paul@126 | 317 | |
paul@126 | 318 | table = [] |
paul@126 | 319 | table_name = encode_tablename("Function", path) |
paul@126 | 320 | structure_size = encode_size("pmax", path) |
paul@126 | 321 | self.populate_parameter_table(function_path, table) |
paul@126 | 322 | self.write_parameter_table(f_decls, f_defs, table_name, structure_size, table) |
paul@126 | 323 | |
paul@126 | 324 | def write_size_constants(self, f_consts, size_prefix, sizes, padding): |
paul@126 | 325 | |
paul@126 | 326 | """ |
paul@126 | 327 | Write size constants to 'f_consts' for the given 'size_prefix', using |
paul@126 | 328 | the 'sizes' dictionary to populate the definition, adding the given |
paul@126 | 329 | 'padding' to the basic sizes. |
paul@126 | 330 | """ |
paul@126 | 331 | |
paul@126 | 332 | print >>f_consts, "enum %s {" % encode_size(size_prefix) |
paul@126 | 333 | first = True |
paul@126 | 334 | for path, size in sizes.items(): |
paul@126 | 335 | if not first: |
paul@126 | 336 | print >>f_consts, "," |
paul@126 | 337 | else: |
paul@126 | 338 | first = False |
paul@126 | 339 | f_consts.write(" %s = %d" % (encode_size(size_prefix, path), size + padding)) |
paul@126 | 340 | print >>f_consts, "\n };" |
paul@126 | 341 | |
paul@126 | 342 | def write_code_constants(self, f_consts, attrnames, locations): |
paul@126 | 343 | |
paul@126 | 344 | """ |
paul@126 | 345 | Write code constants to 'f_consts' for the given 'attrnames' and |
paul@126 | 346 | attribute 'locations'. |
paul@126 | 347 | """ |
paul@126 | 348 | |
paul@126 | 349 | print >>f_consts, "enum %s {" % encode_symbol("code") |
paul@126 | 350 | first = True |
paul@126 | 351 | for i, attrname in enumerate(attrnames): |
paul@126 | 352 | if not first: |
paul@126 | 353 | print >>f_consts, "," |
paul@126 | 354 | else: |
paul@126 | 355 | first = False |
paul@126 | 356 | f_consts.write(" %s = %d" % (encode_symbol("code", attrname), i)) |
paul@126 | 357 | print >>f_consts, "\n };" |
paul@126 | 358 | |
paul@126 | 359 | print >>f_consts, "enum %s {" % encode_symbol("pos") |
paul@126 | 360 | first = True |
paul@126 | 361 | for i, attrnames in enumerate(locations): |
paul@126 | 362 | for attrname in attrnames: |
paul@126 | 363 | if not first: |
paul@126 | 364 | print >>f_consts, "," |
paul@126 | 365 | else: |
paul@126 | 366 | first = False |
paul@126 | 367 | f_consts.write(" %s = %d" % (encode_symbol("pos", attrname), i)) |
paul@126 | 368 | print >>f_consts, "\n };" |
paul@126 | 369 | |
paul@126 | 370 | def write_table(self, f_decls, f_defs, table_name, structure_size, table): |
paul@126 | 371 | |
paul@126 | 372 | """ |
paul@126 | 373 | Write the declarations to 'f_decls' and definitions to 'f_defs' for |
paul@126 | 374 | the object having the given 'table_name' and the given 'structure_size', |
paul@126 | 375 | with 'table' details used to populate the definition. |
paul@126 | 376 | """ |
paul@126 | 377 | |
paul@126 | 378 | print >>f_decls, "extern const __table %s;\n" % table_name |
paul@126 | 379 | |
paul@126 | 380 | # Write the corresponding definition. |
paul@126 | 381 | |
paul@126 | 382 | print >>f_defs, "const __table %s = {\n %s,\n {\n %s\n }\n };\n" % ( |
paul@126 | 383 | table_name, structure_size, |
paul@126 | 384 | ",\n ".join(table)) |
paul@126 | 385 | |
paul@126 | 386 | def write_parameter_table(self, f_decls, f_defs, table_name, structure_size, table): |
paul@126 | 387 | |
paul@126 | 388 | """ |
paul@126 | 389 | Write the declarations to 'f_decls' and definitions to 'f_defs' for |
paul@126 | 390 | the object having the given 'table_name' and the given 'structure_size', |
paul@126 | 391 | with 'table' details used to populate the definition. |
paul@126 | 392 | """ |
paul@126 | 393 | |
paul@126 | 394 | print >>f_decls, "extern const __ptable %s;\n" % table_name |
paul@126 | 395 | |
paul@126 | 396 | # Write the corresponding definition. |
paul@126 | 397 | |
paul@126 | 398 | print >>f_defs, "const __ptable %s = {\n %s,\n {\n %s\n }\n };\n" % ( |
paul@126 | 399 | table_name, structure_size, |
paul@126 | 400 | ",\n ".join([("{%s, %s}" % t) for t in table])) |
paul@126 | 401 | |
paul@126 | 402 | def write_structure(self, f_decls, f_defs, path, table_name, structure_size, structure): |
paul@126 | 403 | |
paul@126 | 404 | """ |
paul@126 | 405 | Write the declarations to 'f_decls' and definitions to 'f_defs' for |
paul@126 | 406 | the object having the given 'path', the given 'table_name', and the |
paul@126 | 407 | given 'structure_size', with 'structure' details used to populate the |
paul@126 | 408 | definition. |
paul@126 | 409 | """ |
paul@126 | 410 | |
paul@126 | 411 | print >>f_decls, "extern __obj %s;\n" % encode_path(path) |
paul@126 | 412 | |
paul@126 | 413 | # Write an instance-specific type definition for instances of classes. |
paul@126 | 414 | # See: templates/types.h |
paul@126 | 415 | |
paul@126 | 416 | print >>f_decls, """\ |
paul@126 | 417 | typedef struct { |
paul@126 | 418 | const __table * table; |
paul@126 | 419 | unsigned int pos; |
paul@126 | 420 | __attr attrs[%s]; |
paul@126 | 421 | } %s; |
paul@126 | 422 | """ % (structure_size, encode_symbol("obj", path)) |
paul@126 | 423 | |
paul@126 | 424 | # Write the corresponding definition. |
paul@126 | 425 | |
paul@126 | 426 | print >>f_defs, "__obj %s = {\n &%s,\n %s,\n {\n %s\n }};\n" % ( |
paul@126 | 427 | encode_path(path), table_name, encode_symbol("pos", path), |
paul@126 | 428 | ",\n ".join(structure)) |
paul@126 | 429 | |
paul@126 | 430 | def get_parameters(self, ref): |
paul@126 | 431 | return self.importer.function_parameters[ref.get_origin()] |
paul@126 | 432 | |
paul@126 | 433 | def get_argument_limits(self, path): |
paul@126 | 434 | parameters = self.importer.function_parameters[path] |
paul@126 | 435 | defaults = self.importer.function_defaults.get(path) |
paul@126 | 436 | return len(parameters) - (defaults and len(defaults) or 0), len(parameters) |
paul@126 | 437 | |
paul@126 | 438 | def get_attribute_names(self, indexes): |
paul@126 | 439 | |
paul@126 | 440 | """ |
paul@126 | 441 | Given a list of attribute table 'indexes', return a list of attribute |
paul@126 | 442 | names. |
paul@126 | 443 | """ |
paul@126 | 444 | |
paul@126 | 445 | all_attrnames = self.optimiser.all_attrnames |
paul@126 | 446 | attrnames = [] |
paul@126 | 447 | for i in indexes: |
paul@126 | 448 | if i is None: |
paul@126 | 449 | attrnames.append(None) |
paul@126 | 450 | else: |
paul@126 | 451 | attrnames.append(all_attrnames[i]) |
paul@126 | 452 | return attrnames |
paul@126 | 453 | |
paul@126 | 454 | def get_static_attributes(self, kind, name, attrnames): |
paul@126 | 455 | |
paul@126 | 456 | """ |
paul@126 | 457 | Return a mapping of attribute names to paths for attributes belonging |
paul@126 | 458 | to objects of the given 'kind' (being "<class>" or "<module>") with |
paul@126 | 459 | the given 'name' and supporting the given 'attrnames'. |
paul@126 | 460 | """ |
paul@126 | 461 | |
paul@126 | 462 | attrs = {} |
paul@126 | 463 | |
paul@126 | 464 | for attrname in attrnames: |
paul@126 | 465 | if attrname is None: |
paul@126 | 466 | continue |
paul@126 | 467 | if kind == "<class>": |
paul@126 | 468 | path = self.importer.all_class_attrs[name][attrname] |
paul@126 | 469 | elif kind == "<module>": |
paul@126 | 470 | path = "%s.%s" % (name, attrname) |
paul@126 | 471 | else: |
paul@126 | 472 | continue |
paul@126 | 473 | |
paul@126 | 474 | # The module may be hidden. |
paul@126 | 475 | |
paul@126 | 476 | attr = self.importer.get_object(path) |
paul@126 | 477 | if not attr: |
paul@126 | 478 | module = self.importer.hidden.get(path) |
paul@126 | 479 | if module: |
paul@126 | 480 | attr = Reference(module.name, "<module>") |
paul@126 | 481 | attrs[attrname] = attr |
paul@126 | 482 | |
paul@126 | 483 | return attrs |
paul@126 | 484 | |
paul@126 | 485 | def get_instance_attributes(self, name, attrnames): |
paul@126 | 486 | |
paul@126 | 487 | """ |
paul@126 | 488 | Return a mapping of attribute names to references for attributes |
paul@126 | 489 | belonging to instances of the class with the given 'name', where the |
paul@126 | 490 | given 'attrnames' are supported. |
paul@126 | 491 | """ |
paul@126 | 492 | |
paul@126 | 493 | consts = self.importer.all_instance_attr_constants[name] |
paul@126 | 494 | attrs = {} |
paul@126 | 495 | for attrname in attrnames: |
paul@126 | 496 | if attrname is None: |
paul@126 | 497 | continue |
paul@126 | 498 | const = consts.get(attrname) |
paul@126 | 499 | attrs[attrname] = const or Reference("<var>", "%s.%s" % (name, attrname)) |
paul@126 | 500 | return attrs |
paul@126 | 501 | |
paul@126 | 502 | def populate_table(self, key, table): |
paul@126 | 503 | |
paul@126 | 504 | """ |
paul@126 | 505 | Traverse the attributes in the determined order for the structure having |
paul@126 | 506 | the given 'key', adding entries to the attribute 'table'. |
paul@126 | 507 | """ |
paul@126 | 508 | |
paul@126 | 509 | for attrname in self.optimiser.structures[key]: |
paul@126 | 510 | |
paul@126 | 511 | # Handle gaps in the structure. |
paul@126 | 512 | |
paul@126 | 513 | if attrname is None: |
paul@126 | 514 | table.append("0") |
paul@126 | 515 | else: |
paul@126 | 516 | table.append(encode_symbol("code", attrname)) |
paul@126 | 517 | |
paul@126 | 518 | def populate_parameter_table(self, key, table): |
paul@126 | 519 | |
paul@126 | 520 | """ |
paul@126 | 521 | Traverse the parameters in the determined order for the structure having |
paul@126 | 522 | the given 'key', adding entries to the attribute 'table'. |
paul@126 | 523 | """ |
paul@126 | 524 | |
paul@126 | 525 | for value in self.optimiser.parameters[key]: |
paul@126 | 526 | |
paul@126 | 527 | # Handle gaps in the structure. |
paul@126 | 528 | |
paul@126 | 529 | if value is None: |
paul@126 | 530 | table.append(("0", "0")) |
paul@126 | 531 | else: |
paul@126 | 532 | name, pos = value |
paul@126 | 533 | table.append((encode_symbol("pcode", name), pos)) |
paul@126 | 534 | |
paul@126 | 535 | def populate_function(self, path, function_instance_attrs, unbound=False): |
paul@126 | 536 | |
paul@126 | 537 | """ |
paul@126 | 538 | Populate a structure for the function with the given 'path'. The given |
paul@126 | 539 | 'attrs' provide the instance attributes, and if 'unbound' is set to a |
paul@126 | 540 | true value, an unbound method structure is produced (as opposed to a |
paul@126 | 541 | callable bound method structure). |
paul@126 | 542 | """ |
paul@126 | 543 | |
paul@126 | 544 | cls = self.function_type |
paul@126 | 545 | structure = [] |
paul@126 | 546 | self.populate_structure(Reference("<instance>", cls), function_instance_attrs, "<instance>", structure, unbound) |
paul@126 | 547 | |
paul@126 | 548 | # Append default members. |
paul@126 | 549 | |
paul@126 | 550 | self.append_defaults(path, structure) |
paul@126 | 551 | return structure |
paul@126 | 552 | |
paul@126 | 553 | def populate_structure(self, ref, attrs, kind, structure, unbound=False): |
paul@126 | 554 | |
paul@126 | 555 | """ |
paul@126 | 556 | Traverse the attributes in the determined order for the structure having |
paul@126 | 557 | the given 'ref' whose members are provided by the 'attrs' mapping, in a |
paul@126 | 558 | structure of the given 'kind', adding entries to the object 'structure'. |
paul@126 | 559 | If 'unbound' is set to a true value, an unbound method function pointer |
paul@126 | 560 | will be employed, with a reference to the bound method incorporated into |
paul@126 | 561 | the special __fn__ attribute. |
paul@126 | 562 | """ |
paul@126 | 563 | |
paul@126 | 564 | origin = ref.get_origin() |
paul@126 | 565 | |
paul@126 | 566 | for attrname in self.optimiser.structures[ref]: |
paul@126 | 567 | |
paul@126 | 568 | # Handle gaps in the structure. |
paul@126 | 569 | |
paul@126 | 570 | if attrname is None: |
paul@126 | 571 | structure.append("{0, 0}") |
paul@126 | 572 | |
paul@126 | 573 | # Handle non-constant and constant members. |
paul@126 | 574 | |
paul@126 | 575 | else: |
paul@126 | 576 | attr = attrs[attrname] |
paul@126 | 577 | |
paul@126 | 578 | if attrname == "__fn__": |
paul@126 | 579 | |
paul@126 | 580 | # Provide bound method references and the unbound function |
paul@126 | 581 | # pointer if populating methods in a class. |
paul@126 | 582 | |
paul@126 | 583 | bound_attr = None |
paul@126 | 584 | |
paul@126 | 585 | # Classes offer instantiators. |
paul@126 | 586 | |
paul@126 | 587 | if kind == "<class>": |
paul@126 | 588 | attr = encode_instantiator_pointer(attr) |
paul@126 | 589 | |
paul@126 | 590 | # Methods offers references to bound versions and an unbound |
paul@126 | 591 | # method function. |
paul@126 | 592 | |
paul@126 | 593 | elif unbound: |
paul@126 | 594 | bound_attr = encode_bound_reference(attr) |
paul@126 | 595 | attr = "__unbound_method" |
paul@126 | 596 | |
paul@126 | 597 | # Other functions just offer function pointers. |
paul@126 | 598 | |
paul@126 | 599 | else: |
paul@126 | 600 | attr = encode_function_pointer(attr) |
paul@126 | 601 | |
paul@126 | 602 | structure.append("{%s, .fn=%s}" % (bound_attr and ".b=%s" % bound_attr or "0", attr)) |
paul@126 | 603 | continue |
paul@126 | 604 | |
paul@126 | 605 | elif attrname == "__args__": |
paul@126 | 606 | structure.append("{.min=%s, .ptable=%s}" % (attr, encode_tablename("Function", origin))) |
paul@126 | 607 | continue |
paul@126 | 608 | |
paul@126 | 609 | structure.append(self.encode_member(origin, attrname, attr, kind)) |
paul@126 | 610 | |
paul@126 | 611 | def encode_member(self, path, name, ref, structure_type): |
paul@126 | 612 | |
paul@126 | 613 | """ |
paul@126 | 614 | Encode within the structure provided by 'path', the member whose 'name' |
paul@126 | 615 | provides 'ref', within the given 'structure_type'. |
paul@126 | 616 | """ |
paul@126 | 617 | |
paul@126 | 618 | kind = ref.get_kind() |
paul@126 | 619 | origin = ref.get_origin() |
paul@126 | 620 | |
paul@126 | 621 | # References to constant literals. |
paul@126 | 622 | |
paul@126 | 623 | if kind == "<instance>": |
paul@126 | 624 | attr_path = "%s.%s" % (path, name) |
paul@126 | 625 | |
paul@126 | 626 | # Obtain a constant value directly assigned to the attribute. |
paul@126 | 627 | |
paul@126 | 628 | if self.optimiser.constant_numbers.has_key(attr_path): |
paul@126 | 629 | constant_number = self.optimiser.constant_numbers[attr_path] |
paul@126 | 630 | constant_value = "const%d" % constant_number |
paul@126 | 631 | return "{&%s, &%s} /* %s */" % (constant_value, constant_value, name) |
paul@126 | 632 | |
paul@126 | 633 | # General undetermined members. |
paul@126 | 634 | |
paul@126 | 635 | if kind in ("<var>", "<instance>"): |
paul@126 | 636 | return "{0, 0} /* %s */" % name |
paul@126 | 637 | |
paul@126 | 638 | # Set the context depending on the kind of attribute. |
paul@126 | 639 | # For methods: {&<path>, &<attr>} |
paul@126 | 640 | # For other attributes: {&<attr>, &<attr>} |
paul@126 | 641 | |
paul@126 | 642 | else: |
paul@126 | 643 | context = (kind == "<function>" and structure_type == "<class>" and \ |
paul@126 | 644 | "&%s" % encode_path(path) or "0") or \ |
paul@126 | 645 | kind == "<instance>" and "&%s" % encode_path(origin) or "0" |
paul@126 | 646 | return "{%s, &%s}" % (context, encode_path(origin)) |
paul@126 | 647 | |
paul@126 | 648 | def append_defaults(self, path, structure): |
paul@126 | 649 | |
paul@126 | 650 | """ |
paul@126 | 651 | For the given 'path', append default parameter members to the given |
paul@126 | 652 | 'structure'. |
paul@126 | 653 | """ |
paul@126 | 654 | |
paul@126 | 655 | for name, default in self.importer.function_defaults.get(path): |
paul@126 | 656 | structure.append(self.encode_member(path, name, default, "<instance>")) |
paul@126 | 657 | |
paul@126 | 658 | def write_instantiator(self, f_code, path, init_ref): |
paul@126 | 659 | |
paul@126 | 660 | """ |
paul@126 | 661 | Write an instantiator to 'f_code' for instances of the class with the |
paul@126 | 662 | given 'path', with 'init_ref' as the initialiser function reference. |
paul@126 | 663 | |
paul@126 | 664 | NOTE: This also needs to initialise any __fn__ and __args__ members |
paul@126 | 665 | NOTE: where __call__ is provided by the class. |
paul@126 | 666 | """ |
paul@126 | 667 | |
paul@126 | 668 | parameters = self.get_parameters(init_ref) |
paul@126 | 669 | arg_copy = "memcpy(&__tmp_args[1], args, %d * sizeof(__attr));" % (len(parameters) - 1) |
paul@126 | 670 | |
paul@126 | 671 | print >>f_code, """\ |
paul@126 | 672 | __attr %s(__attr args[]) |
paul@126 | 673 | { |
paul@126 | 674 | __attr __tmp_args[%d]; |
paul@126 | 675 | __tmp_args[0] = __new(&%s, &%s, sizeof(%s)); |
paul@126 | 676 | %s |
paul@126 | 677 | %s(__tmp_args); |
paul@126 | 678 | return __tmp_args[0]; |
paul@126 | 679 | } |
paul@126 | 680 | """ % ( |
paul@126 | 681 | encode_instantiator_pointer(path), |
paul@126 | 682 | len(parameters), |
paul@126 | 683 | encode_tablename("Instance", path), encode_path(path), encode_symbol("obj", path), |
paul@126 | 684 | len(parameters) - 1 and arg_copy or "", |
paul@126 | 685 | encode_function_pointer(init_ref.get_origin()) |
paul@126 | 686 | ) |
paul@126 | 687 | |
paul@126 | 688 | def encode_size(table_type, path=None): |
paul@126 | 689 | return "__%ssize%s" % (table_type, path and "_%s" % encode_path(path) or "") |
paul@126 | 690 | |
paul@126 | 691 | def encode_tablename(table_type, path): |
paul@126 | 692 | return "__%sTable_%s" % (table_type, encode_path(path)) |
paul@126 | 693 | |
paul@126 | 694 | # vim: tabstop=4 expandtab shiftwidth=4 |