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@510 | 6 | Copyright (C) 2015, 2016, 2017 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@538 | 22 | from common import CommonOutput |
paul@523 | 23 | from encoders import encode_function_pointer, \ |
paul@136 | 24 | encode_instantiator_pointer, \ |
paul@136 | 25 | encode_literal_constant, encode_literal_constant_member, \ |
paul@378 | 26 | encode_literal_constant_size, encode_literal_constant_value, \ |
paul@283 | 27 | encode_literal_data_initialiser, \ |
paul@159 | 28 | encode_literal_instantiator, encode_literal_reference, \ |
paul@136 | 29 | encode_path, \ |
paul@150 | 30 | encode_predefined_reference, encode_size, \ |
paul@150 | 31 | encode_symbol, encode_tablename, \ |
paul@318 | 32 | encode_type_attribute, decode_type_attribute, \ |
paul@318 | 33 | is_type_attribute |
paul@354 | 34 | from os import listdir, mkdir |
paul@183 | 35 | from os.path import exists, isdir, join, split |
paul@126 | 36 | from referencing import Reference |
paul@126 | 37 | |
paul@126 | 38 | def copy(source, target): |
paul@126 | 39 | |
paul@126 | 40 | "Copy a text file from 'source' to 'target'." |
paul@126 | 41 | |
paul@126 | 42 | if isdir(target): |
paul@126 | 43 | target = join(target, split(source)[-1]) |
paul@126 | 44 | infile = open(source) |
paul@126 | 45 | outfile = open(target, "w") |
paul@126 | 46 | try: |
paul@126 | 47 | outfile.write(infile.read()) |
paul@126 | 48 | finally: |
paul@126 | 49 | outfile.close() |
paul@126 | 50 | infile.close() |
paul@126 | 51 | |
paul@126 | 52 | class Generator(CommonOutput): |
paul@126 | 53 | |
paul@126 | 54 | "A code generator." |
paul@126 | 55 | |
paul@274 | 56 | # NOTE: These must be synchronised with the library. |
paul@274 | 57 | |
paul@126 | 58 | function_type = "__builtins__.core.function" |
paul@400 | 59 | none_type = "__builtins__.none.NoneType" |
paul@360 | 60 | string_type = "__builtins__.str.string" |
paul@274 | 61 | type_type = "__builtins__.core.type" |
paul@400 | 62 | unicode_type = "__builtins__.unicode.utf8string" |
paul@400 | 63 | |
paul@400 | 64 | none_value = "__builtins__.none.None" |
paul@158 | 65 | |
paul@136 | 66 | predefined_constant_members = ( |
paul@158 | 67 | ("__builtins__.boolean", "False"), |
paul@158 | 68 | ("__builtins__.boolean", "True"), |
paul@136 | 69 | ("__builtins__.none", "None"), |
paul@136 | 70 | ("__builtins__.notimplemented", "NotImplemented"), |
paul@136 | 71 | ) |
paul@136 | 72 | |
paul@283 | 73 | literal_mapping_types = ( |
paul@159 | 74 | "__builtins__.dict.dict", |
paul@283 | 75 | ) |
paul@283 | 76 | |
paul@283 | 77 | literal_sequence_types = ( |
paul@159 | 78 | "__builtins__.list.list", |
paul@159 | 79 | "__builtins__.tuple.tuple", |
paul@159 | 80 | ) |
paul@159 | 81 | |
paul@283 | 82 | literal_instantiator_types = literal_mapping_types + literal_sequence_types |
paul@283 | 83 | |
paul@126 | 84 | def __init__(self, importer, optimiser, output): |
paul@357 | 85 | |
paul@357 | 86 | """ |
paul@357 | 87 | Initialise the generator with the given 'importer', 'optimiser' and |
paul@357 | 88 | 'output' directory. |
paul@357 | 89 | """ |
paul@357 | 90 | |
paul@126 | 91 | self.importer = importer |
paul@126 | 92 | self.optimiser = optimiser |
paul@126 | 93 | self.output = output |
paul@126 | 94 | |
paul@183 | 95 | def to_output(self, debug=False): |
paul@126 | 96 | |
paul@126 | 97 | "Write the generated code." |
paul@126 | 98 | |
paul@126 | 99 | self.check_output() |
paul@126 | 100 | self.write_structures() |
paul@511 | 101 | self.write_scripts(debug) |
paul@511 | 102 | self.copy_templates() |
paul@126 | 103 | |
paul@511 | 104 | def copy_templates(self): |
paul@126 | 105 | |
paul@126 | 106 | "Copy template files to the generated output directory." |
paul@126 | 107 | |
paul@126 | 108 | templates = join(split(__file__)[0], "templates") |
paul@126 | 109 | |
paul@126 | 110 | for filename in listdir(templates): |
paul@183 | 111 | target = self.output |
paul@354 | 112 | pathname = join(templates, filename) |
paul@354 | 113 | |
paul@354 | 114 | # Copy files into the target directory. |
paul@354 | 115 | |
paul@354 | 116 | if not isdir(pathname): |
paul@354 | 117 | copy(pathname, target) |
paul@354 | 118 | |
paul@354 | 119 | # Copy directories (such as the native code directory). |
paul@354 | 120 | |
paul@354 | 121 | else: |
paul@354 | 122 | target = join(self.output, filename) |
paul@354 | 123 | |
paul@354 | 124 | if not exists(target): |
paul@354 | 125 | mkdir(target) |
paul@354 | 126 | |
paul@354 | 127 | for filename in listdir(pathname): |
paul@354 | 128 | copy(join(pathname, filename), target) |
paul@126 | 129 | |
paul@126 | 130 | def write_structures(self): |
paul@126 | 131 | |
paul@126 | 132 | "Write structures used by the program." |
paul@126 | 133 | |
paul@126 | 134 | f_consts = open(join(self.output, "progconsts.h"), "w") |
paul@126 | 135 | f_defs = open(join(self.output, "progtypes.c"), "w") |
paul@126 | 136 | f_decls = open(join(self.output, "progtypes.h"), "w") |
paul@126 | 137 | f_signatures = open(join(self.output, "main.h"), "w") |
paul@126 | 138 | f_code = open(join(self.output, "main.c"), "w") |
paul@126 | 139 | |
paul@126 | 140 | try: |
paul@126 | 141 | # Output boilerplate. |
paul@126 | 142 | |
paul@126 | 143 | print >>f_consts, """\ |
paul@126 | 144 | #ifndef __PROGCONSTS_H__ |
paul@126 | 145 | #define __PROGCONSTS_H__ |
paul@126 | 146 | """ |
paul@126 | 147 | print >>f_decls, """\ |
paul@126 | 148 | #ifndef __PROGTYPES_H__ |
paul@126 | 149 | #define __PROGTYPES_H__ |
paul@126 | 150 | |
paul@126 | 151 | #include "progconsts.h" |
paul@126 | 152 | #include "types.h" |
paul@126 | 153 | """ |
paul@126 | 154 | print >>f_defs, """\ |
paul@126 | 155 | #include "progtypes.h" |
paul@132 | 156 | #include "progops.h" |
paul@126 | 157 | #include "main.h" |
paul@126 | 158 | """ |
paul@126 | 159 | print >>f_signatures, """\ |
paul@126 | 160 | #ifndef __MAIN_H__ |
paul@126 | 161 | #define __MAIN_H__ |
paul@126 | 162 | |
paul@126 | 163 | #include "types.h" |
paul@126 | 164 | """ |
paul@126 | 165 | print >>f_code, """\ |
paul@126 | 166 | #include <string.h> |
paul@159 | 167 | #include <stdio.h> |
paul@434 | 168 | #include "gc.h" |
paul@126 | 169 | #include "types.h" |
paul@159 | 170 | #include "exceptions.h" |
paul@126 | 171 | #include "ops.h" |
paul@126 | 172 | #include "progconsts.h" |
paul@126 | 173 | #include "progtypes.h" |
paul@490 | 174 | #include "main.h" |
paul@126 | 175 | #include "progops.h" |
paul@126 | 176 | """ |
paul@126 | 177 | |
paul@126 | 178 | # Generate table and structure data. |
paul@126 | 179 | |
paul@126 | 180 | function_instance_attrs = None |
paul@126 | 181 | objects = self.optimiser.attr_table.items() |
paul@126 | 182 | objects.sort() |
paul@126 | 183 | |
paul@357 | 184 | self.callables = {} |
paul@357 | 185 | |
paul@126 | 186 | for ref, indexes in objects: |
paul@126 | 187 | attrnames = self.get_attribute_names(indexes) |
paul@126 | 188 | |
paul@126 | 189 | kind = ref.get_kind() |
paul@126 | 190 | path = ref.get_origin() |
paul@150 | 191 | table_name = encode_tablename(kind, path) |
paul@150 | 192 | structure_size = encode_size(kind, path) |
paul@126 | 193 | |
paul@126 | 194 | # Generate structures for classes and modules. |
paul@126 | 195 | |
paul@126 | 196 | if kind != "<instance>": |
paul@126 | 197 | structure = [] |
paul@126 | 198 | attrs = self.get_static_attributes(kind, path, attrnames) |
paul@126 | 199 | |
paul@126 | 200 | # Set a special instantiator on the class. |
paul@126 | 201 | |
paul@126 | 202 | if kind == "<class>": |
paul@126 | 203 | |
paul@126 | 204 | # Write instantiator declarations based on the |
paul@126 | 205 | # applicable initialiser. |
paul@126 | 206 | |
paul@126 | 207 | init_ref = attrs["__init__"] |
paul@126 | 208 | |
paul@126 | 209 | # Write instantiator definitions. |
paul@126 | 210 | |
paul@159 | 211 | self.write_instantiator(f_code, f_signatures, path, init_ref) |
paul@126 | 212 | |
paul@357 | 213 | # Record the callable for parameter table generation. |
paul@357 | 214 | |
paul@357 | 215 | self.callables[path] = init_ref.get_origin() |
paul@126 | 216 | |
paul@357 | 217 | # Define special attributes. |
paul@357 | 218 | |
paul@357 | 219 | attrs["__fn__"] = path |
paul@362 | 220 | attrs["__args__"] = encode_size("pmin", path) |
paul@126 | 221 | |
paul@126 | 222 | self.populate_structure(Reference(kind, path), attrs, kind, structure) |
paul@136 | 223 | |
paul@136 | 224 | if kind == "<class>": |
paul@136 | 225 | self.write_instance_structure(f_decls, path, structure_size) |
paul@136 | 226 | |
paul@211 | 227 | self.write_structure(f_decls, f_defs, path, table_name, structure, |
paul@136 | 228 | kind == "<class>" and path) |
paul@126 | 229 | |
paul@126 | 230 | # Record function instance details for function generation below. |
paul@126 | 231 | |
paul@126 | 232 | else: |
paul@126 | 233 | attrs = self.get_instance_attributes(path, attrnames) |
paul@126 | 234 | if path == self.function_type: |
paul@126 | 235 | function_instance_attrs = attrs |
paul@126 | 236 | |
paul@357 | 237 | # Record the callable for parameter table generation. |
paul@357 | 238 | |
paul@357 | 239 | self.callables[path] = path |
paul@357 | 240 | |
paul@126 | 241 | # Write a table for all objects. |
paul@126 | 242 | |
paul@126 | 243 | table = [] |
paul@126 | 244 | self.populate_table(Reference(kind, path), table) |
paul@126 | 245 | self.write_table(f_decls, f_defs, table_name, structure_size, table) |
paul@126 | 246 | |
paul@126 | 247 | # Generate function instances. |
paul@126 | 248 | |
paul@195 | 249 | functions = self.importer.function_parameters.keys() |
paul@126 | 250 | functions.sort() |
paul@211 | 251 | extra_function_instances = [] |
paul@126 | 252 | |
paul@126 | 253 | for path in functions: |
paul@195 | 254 | |
paul@195 | 255 | # Instantiators are generated above. |
paul@195 | 256 | |
paul@195 | 257 | if self.importer.classes.has_key(path) or not self.importer.get_object(path): |
paul@195 | 258 | continue |
paul@195 | 259 | |
paul@357 | 260 | # Record the callable for parameter table generation. |
paul@357 | 261 | |
paul@357 | 262 | self.callables[path] = path |
paul@357 | 263 | |
paul@357 | 264 | # Define the structure details. |
paul@357 | 265 | |
paul@126 | 266 | cls = self.function_type |
paul@150 | 267 | table_name = encode_tablename("<instance>", cls) |
paul@211 | 268 | structure_size = encode_size("<instance>", path) |
paul@126 | 269 | |
paul@126 | 270 | # Set a special callable attribute on the instance. |
paul@126 | 271 | |
paul@126 | 272 | function_instance_attrs["__fn__"] = path |
paul@362 | 273 | function_instance_attrs["__args__"] = encode_size("pmin", path) |
paul@126 | 274 | |
paul@126 | 275 | # Produce two structures where a method is involved. |
paul@126 | 276 | |
paul@195 | 277 | parent, name = path.rsplit(".", 1) |
paul@195 | 278 | parent_ref = self.importer.get_object(parent) |
paul@126 | 279 | parent_kind = parent_ref and parent_ref.get_kind() |
paul@126 | 280 | |
paul@126 | 281 | # Populate and write each structure. |
paul@126 | 282 | |
paul@126 | 283 | if parent_kind == "<class>": |
paul@126 | 284 | |
paul@523 | 285 | # A method. |
paul@132 | 286 | |
paul@523 | 287 | structure = self.populate_function(path, function_instance_attrs) |
paul@211 | 288 | self.write_structure(f_decls, f_defs, path, table_name, structure) |
paul@126 | 289 | |
paul@132 | 290 | else: |
paul@132 | 291 | # A normal function. |
paul@126 | 292 | |
paul@523 | 293 | structure = self.populate_function(path, function_instance_attrs) |
paul@211 | 294 | self.write_structure(f_decls, f_defs, path, table_name, structure) |
paul@126 | 295 | |
paul@154 | 296 | # Functions with defaults need to declare instance structures. |
paul@154 | 297 | |
paul@154 | 298 | if self.importer.function_defaults.get(path): |
paul@154 | 299 | self.write_instance_structure(f_decls, path, structure_size) |
paul@211 | 300 | extra_function_instances.append(path) |
paul@154 | 301 | |
paul@126 | 302 | # Write function declarations. |
paul@126 | 303 | # Signature: __attr <name>(__attr[]); |
paul@126 | 304 | |
paul@126 | 305 | print >>f_signatures, "__attr %s(__attr args[]);" % encode_function_pointer(path) |
paul@126 | 306 | |
paul@357 | 307 | # Consolidate parameter tables for instantiators and functions. |
paul@357 | 308 | |
paul@357 | 309 | parameter_tables = set() |
paul@126 | 310 | |
paul@357 | 311 | for path, function_path in self.callables.items(): |
paul@357 | 312 | parameters = self.optimiser.parameters[function_path] |
paul@357 | 313 | if not parameters: |
paul@357 | 314 | parameters = () |
paul@357 | 315 | else: |
paul@357 | 316 | parameters = tuple(parameters) |
paul@357 | 317 | parameter_tables.add(parameters) |
paul@357 | 318 | |
paul@357 | 319 | # Generate parameter tables for distinct function signatures. |
paul@357 | 320 | |
paul@357 | 321 | for parameters in parameter_tables: |
paul@357 | 322 | self.make_parameter_table(f_decls, f_defs, parameters) |
paul@126 | 323 | |
paul@136 | 324 | # Generate predefined constants. |
paul@136 | 325 | |
paul@136 | 326 | for path, name in self.predefined_constant_members: |
paul@136 | 327 | self.make_predefined_constant(f_decls, f_defs, path, name) |
paul@136 | 328 | |
paul@136 | 329 | # Generate literal constants. |
paul@136 | 330 | |
paul@397 | 331 | for constant, n in self.optimiser.constants.items(): |
paul@397 | 332 | self.make_literal_constant(f_decls, f_defs, n, constant) |
paul@136 | 333 | |
paul@146 | 334 | # Finish the main source file. |
paul@146 | 335 | |
paul@146 | 336 | self.write_main_program(f_code, f_signatures) |
paul@146 | 337 | |
paul@211 | 338 | # Record size information for certain function instances as well as |
paul@211 | 339 | # for classes, modules and other instances. |
paul@211 | 340 | |
paul@211 | 341 | size_tables = {} |
paul@211 | 342 | |
paul@211 | 343 | for kind in ["<class>", "<module>", "<instance>"]: |
paul@211 | 344 | size_tables[kind] = {} |
paul@211 | 345 | |
paul@211 | 346 | # Generate structure size data. |
paul@211 | 347 | |
paul@211 | 348 | for ref, structure in self.optimiser.structures.items(): |
paul@211 | 349 | size_tables[ref.get_kind()][ref.get_origin()] = len(structure) |
paul@211 | 350 | |
paul@211 | 351 | for path in extra_function_instances: |
paul@211 | 352 | defaults = self.importer.function_defaults[path] |
paul@211 | 353 | size_tables["<instance>"][path] = size_tables["<instance>"][self.function_type] + len(defaults) |
paul@211 | 354 | |
paul@211 | 355 | size_tables = size_tables.items() |
paul@211 | 356 | size_tables.sort() |
paul@211 | 357 | |
paul@211 | 358 | for kind, sizes in size_tables: |
paul@211 | 359 | self.write_size_constants(f_consts, kind, sizes, 0) |
paul@211 | 360 | |
paul@211 | 361 | # Generate parameter table size data. |
paul@211 | 362 | |
paul@211 | 363 | min_sizes = {} |
paul@211 | 364 | max_sizes = {} |
paul@211 | 365 | |
paul@362 | 366 | # Determine the minimum number of parameters for each |
paul@357 | 367 | |
paul@362 | 368 | for path in self.optimiser.parameters.keys(): |
paul@362 | 369 | argmin, argmax = self.get_argument_limits(path) |
paul@362 | 370 | min_sizes[path] = argmin |
paul@357 | 371 | |
paul@362 | 372 | # Use the parameter table details to define the maximum number. |
paul@362 | 373 | # The context is already present in the collection. |
paul@362 | 374 | |
paul@362 | 375 | for parameters in parameter_tables: |
paul@357 | 376 | signature = self.get_parameter_signature(parameters) |
paul@362 | 377 | max_sizes[signature] = len(parameters) |
paul@211 | 378 | |
paul@211 | 379 | self.write_size_constants(f_consts, "pmin", min_sizes, 0) |
paul@211 | 380 | self.write_size_constants(f_consts, "pmax", max_sizes, 0) |
paul@211 | 381 | |
paul@211 | 382 | # Generate parameter codes. |
paul@211 | 383 | |
paul@211 | 384 | self.write_code_constants(f_consts, self.optimiser.all_paramnames, self.optimiser.arg_locations, "pcode", "ppos") |
paul@211 | 385 | |
paul@211 | 386 | # Generate attribute codes. |
paul@211 | 387 | |
paul@211 | 388 | self.write_code_constants(f_consts, self.optimiser.all_attrnames, self.optimiser.locations, "code", "pos") |
paul@211 | 389 | |
paul@126 | 390 | # Output more boilerplate. |
paul@126 | 391 | |
paul@126 | 392 | print >>f_consts, """\ |
paul@126 | 393 | |
paul@126 | 394 | #endif /* __PROGCONSTS_H__ */""" |
paul@126 | 395 | |
paul@126 | 396 | print >>f_decls, """\ |
paul@126 | 397 | |
paul@126 | 398 | #define __FUNCTION_TYPE %s |
paul@126 | 399 | #define __FUNCTION_INSTANCE_SIZE %s |
paul@274 | 400 | #define __TYPE_CLASS_TYPE %s |
paul@274 | 401 | #define __TYPE_CLASS_POS %s |
paul@274 | 402 | #define __TYPE_CLASS_CODE %s |
paul@126 | 403 | |
paul@126 | 404 | #endif /* __PROGTYPES_H__ */""" % ( |
paul@126 | 405 | encode_path(self.function_type), |
paul@233 | 406 | encode_size("<instance>", self.function_type), |
paul@274 | 407 | encode_path(self.type_type), |
paul@274 | 408 | encode_symbol("pos", encode_type_attribute(self.type_type)), |
paul@274 | 409 | encode_symbol("code", encode_type_attribute(self.type_type)), |
paul@126 | 410 | ) |
paul@126 | 411 | |
paul@126 | 412 | print >>f_signatures, """\ |
paul@126 | 413 | |
paul@126 | 414 | #endif /* __MAIN_H__ */""" |
paul@126 | 415 | |
paul@126 | 416 | finally: |
paul@126 | 417 | f_consts.close() |
paul@126 | 418 | f_defs.close() |
paul@126 | 419 | f_decls.close() |
paul@126 | 420 | f_signatures.close() |
paul@126 | 421 | f_code.close() |
paul@126 | 422 | |
paul@511 | 423 | def write_scripts(self, debug): |
paul@511 | 424 | |
paul@511 | 425 | "Write scripts used to build the program." |
paul@511 | 426 | |
paul@511 | 427 | f_native = open(join(self.output, "native.mk"), "w") |
paul@511 | 428 | f_options = open(join(self.output, "options.mk"), "w") |
paul@511 | 429 | try: |
paul@511 | 430 | if debug: |
paul@511 | 431 | print >>f_options, "CFLAGS = -g" |
paul@511 | 432 | |
paul@511 | 433 | # Identify native modules used by the program. |
paul@511 | 434 | |
paul@511 | 435 | native_modules = ["native/common.c"] |
paul@511 | 436 | |
paul@511 | 437 | for name in self.importer.modules.keys(): |
paul@511 | 438 | parts = name.split(".", 1) |
paul@511 | 439 | |
paul@511 | 440 | # Identify source files to be built. |
paul@511 | 441 | |
paul@511 | 442 | if parts[0] == "native": |
paul@511 | 443 | native_modules.append("native/%s.c" % parts[1]) |
paul@511 | 444 | |
paul@511 | 445 | print >>f_native, "SRC =", " ".join(native_modules) |
paul@511 | 446 | |
paul@511 | 447 | finally: |
paul@511 | 448 | f_native.close() |
paul@511 | 449 | f_options.close() |
paul@511 | 450 | |
paul@397 | 451 | def make_literal_constant(self, f_decls, f_defs, n, constant): |
paul@136 | 452 | |
paul@136 | 453 | """ |
paul@136 | 454 | Write literal constant details to 'f_decls' (to declare a structure) and |
paul@136 | 455 | to 'f_defs' (to define the contents) for the constant with the number |
paul@397 | 456 | 'n' with the given 'constant'. |
paul@136 | 457 | """ |
paul@136 | 458 | |
paul@406 | 459 | value, value_type, encoding = constant |
paul@397 | 460 | |
paul@136 | 461 | const_path = encode_literal_constant(n) |
paul@136 | 462 | structure_name = encode_literal_reference(n) |
paul@136 | 463 | |
paul@397 | 464 | ref = Reference("<instance>", value_type) |
paul@406 | 465 | self.make_constant(f_decls, f_defs, ref, const_path, structure_name, value, encoding) |
paul@136 | 466 | |
paul@136 | 467 | def make_predefined_constant(self, f_decls, f_defs, path, name): |
paul@136 | 468 | |
paul@136 | 469 | """ |
paul@136 | 470 | Write predefined constant details to 'f_decls' (to declare a structure) |
paul@136 | 471 | and to 'f_defs' (to define the contents) for the constant located in |
paul@136 | 472 | 'path' with the given 'name'. |
paul@136 | 473 | """ |
paul@136 | 474 | |
paul@136 | 475 | # Determine the details of the constant. |
paul@136 | 476 | |
paul@136 | 477 | attr_path = "%s.%s" % (path, name) |
paul@136 | 478 | structure_name = encode_predefined_reference(attr_path) |
paul@136 | 479 | ref = self.importer.get_object(attr_path) |
paul@136 | 480 | |
paul@136 | 481 | self.make_constant(f_decls, f_defs, ref, attr_path, structure_name) |
paul@136 | 482 | |
paul@406 | 483 | def make_constant(self, f_decls, f_defs, ref, const_path, structure_name, data=None, encoding=None): |
paul@136 | 484 | |
paul@136 | 485 | """ |
paul@136 | 486 | Write constant details to 'f_decls' (to declare a structure) and to |
paul@136 | 487 | 'f_defs' (to define the contents) for the constant described by 'ref' |
paul@136 | 488 | having the given 'path' and 'structure_name' (for the constant structure |
paul@136 | 489 | itself). |
paul@406 | 490 | |
paul@406 | 491 | The additional 'data' and 'encoding' are used to describe specific |
paul@406 | 492 | values. |
paul@136 | 493 | """ |
paul@136 | 494 | |
paul@136 | 495 | # Obtain the attributes. |
paul@136 | 496 | |
paul@136 | 497 | cls = ref.get_origin() |
paul@136 | 498 | indexes = self.optimiser.attr_table[ref] |
paul@136 | 499 | attrnames = self.get_attribute_names(indexes) |
paul@136 | 500 | attrs = self.get_instance_attributes(cls, attrnames) |
paul@136 | 501 | |
paul@136 | 502 | # Set the data, if provided. |
paul@136 | 503 | |
paul@136 | 504 | if data is not None: |
paul@136 | 505 | attrs["__data__"] = data |
paul@136 | 506 | |
paul@360 | 507 | # Also set a key for dynamic attribute lookup, if a string. |
paul@360 | 508 | |
paul@397 | 509 | if attrs.has_key("__key__"): |
paul@360 | 510 | if data in self.optimiser.all_attrnames: |
paul@360 | 511 | attrs["__key__"] = data |
paul@360 | 512 | else: |
paul@360 | 513 | attrs["__key__"] = None |
paul@360 | 514 | |
paul@400 | 515 | # Define Unicode constant encoding details. |
paul@400 | 516 | |
paul@400 | 517 | if cls == self.unicode_type: |
paul@406 | 518 | |
paul@406 | 519 | # Reference the encoding's own constant value. |
paul@406 | 520 | |
paul@406 | 521 | if encoding: |
paul@406 | 522 | n = self.optimiser.constants[(encoding, self.string_type, None)] |
paul@406 | 523 | |
paul@406 | 524 | # Employ a special alias that will be tested specifically in |
paul@406 | 525 | # encode_member. |
paul@406 | 526 | |
paul@406 | 527 | encoding_ref = Reference("<instance>", self.string_type, "$c%d" % n) |
paul@406 | 528 | |
paul@406 | 529 | # Use None where no encoding was indicated. |
paul@406 | 530 | |
paul@406 | 531 | else: |
paul@406 | 532 | encoding_ref = Reference("<instance>", self.none_type) |
paul@406 | 533 | |
paul@406 | 534 | attrs["encoding"] = encoding_ref |
paul@400 | 535 | |
paul@136 | 536 | # Define the structure details. An object is created for the constant, |
paul@136 | 537 | # but an attribute is provided, referring to the object, for access to |
paul@136 | 538 | # the constant in the program. |
paul@136 | 539 | |
paul@136 | 540 | structure = [] |
paul@150 | 541 | table_name = encode_tablename("<instance>", cls) |
paul@136 | 542 | self.populate_structure(ref, attrs, ref.get_kind(), structure) |
paul@211 | 543 | self.write_structure(f_decls, f_defs, structure_name, table_name, structure) |
paul@136 | 544 | |
paul@136 | 545 | # Define a macro for the constant. |
paul@136 | 546 | |
paul@136 | 547 | attr_name = encode_path(const_path) |
paul@477 | 548 | print >>f_decls, "#define %s ((__attr) {.context=&%s, .value=&%s})" % (attr_name, structure_name, structure_name) |
paul@136 | 549 | |
paul@357 | 550 | def make_parameter_table(self, f_decls, f_defs, parameters): |
paul@126 | 551 | |
paul@126 | 552 | """ |
paul@126 | 553 | Write parameter table details to 'f_decls' (to declare a table) and to |
paul@357 | 554 | 'f_defs' (to define the contents) for the given 'parameters'. |
paul@126 | 555 | """ |
paul@126 | 556 | |
paul@357 | 557 | # Use a signature for the table name instead of a separate name for each |
paul@357 | 558 | # function. |
paul@357 | 559 | |
paul@357 | 560 | signature = self.get_parameter_signature(parameters) |
paul@357 | 561 | table_name = encode_tablename("<function>", signature) |
paul@357 | 562 | structure_size = encode_size("pmax", signature) |
paul@357 | 563 | |
paul@126 | 564 | table = [] |
paul@357 | 565 | self.populate_parameter_table(parameters, table) |
paul@126 | 566 | self.write_parameter_table(f_decls, f_defs, table_name, structure_size, table) |
paul@126 | 567 | |
paul@357 | 568 | def get_parameter_signature(self, parameters): |
paul@357 | 569 | |
paul@357 | 570 | "Return a signature for the given 'parameters'." |
paul@357 | 571 | |
paul@357 | 572 | l = [] |
paul@357 | 573 | for parameter in parameters: |
paul@357 | 574 | if parameter is None: |
paul@357 | 575 | l.append("") |
paul@357 | 576 | else: |
paul@357 | 577 | name, pos = parameter |
paul@361 | 578 | l.append("%s_%s" % (name, pos)) |
paul@357 | 579 | return l and "__".join(l) or "__void" |
paul@357 | 580 | |
paul@357 | 581 | def get_signature_for_callable(self, path): |
paul@357 | 582 | |
paul@357 | 583 | "Return the signature for the callable with the given 'path'." |
paul@357 | 584 | |
paul@357 | 585 | function_path = self.callables[path] |
paul@357 | 586 | parameters = self.optimiser.parameters[function_path] |
paul@357 | 587 | return self.get_parameter_signature(parameters) |
paul@357 | 588 | |
paul@126 | 589 | def write_size_constants(self, f_consts, size_prefix, sizes, padding): |
paul@126 | 590 | |
paul@126 | 591 | """ |
paul@126 | 592 | Write size constants to 'f_consts' for the given 'size_prefix', using |
paul@126 | 593 | the 'sizes' dictionary to populate the definition, adding the given |
paul@126 | 594 | 'padding' to the basic sizes. |
paul@126 | 595 | """ |
paul@126 | 596 | |
paul@126 | 597 | print >>f_consts, "enum %s {" % encode_size(size_prefix) |
paul@126 | 598 | first = True |
paul@126 | 599 | for path, size in sizes.items(): |
paul@126 | 600 | if not first: |
paul@126 | 601 | print >>f_consts, "," |
paul@126 | 602 | else: |
paul@126 | 603 | first = False |
paul@126 | 604 | f_consts.write(" %s = %d" % (encode_size(size_prefix, path), size + padding)) |
paul@126 | 605 | print >>f_consts, "\n };" |
paul@126 | 606 | |
paul@132 | 607 | def write_code_constants(self, f_consts, attrnames, locations, code_prefix, pos_prefix): |
paul@126 | 608 | |
paul@126 | 609 | """ |
paul@126 | 610 | Write code constants to 'f_consts' for the given 'attrnames' and |
paul@126 | 611 | attribute 'locations'. |
paul@126 | 612 | """ |
paul@126 | 613 | |
paul@132 | 614 | print >>f_consts, "enum %s {" % encode_symbol(code_prefix) |
paul@126 | 615 | first = True |
paul@126 | 616 | for i, attrname in enumerate(attrnames): |
paul@126 | 617 | if not first: |
paul@126 | 618 | print >>f_consts, "," |
paul@126 | 619 | else: |
paul@126 | 620 | first = False |
paul@132 | 621 | f_consts.write(" %s = %d" % (encode_symbol(code_prefix, attrname), i)) |
paul@126 | 622 | print >>f_consts, "\n };" |
paul@126 | 623 | |
paul@132 | 624 | print >>f_consts, "enum %s {" % encode_symbol(pos_prefix) |
paul@126 | 625 | first = True |
paul@126 | 626 | for i, attrnames in enumerate(locations): |
paul@126 | 627 | for attrname in attrnames: |
paul@126 | 628 | if not first: |
paul@126 | 629 | print >>f_consts, "," |
paul@126 | 630 | else: |
paul@126 | 631 | first = False |
paul@132 | 632 | f_consts.write(" %s = %d" % (encode_symbol(pos_prefix, attrname), i)) |
paul@126 | 633 | print >>f_consts, "\n };" |
paul@126 | 634 | |
paul@126 | 635 | def write_table(self, f_decls, f_defs, table_name, structure_size, table): |
paul@126 | 636 | |
paul@126 | 637 | """ |
paul@126 | 638 | Write the declarations to 'f_decls' and definitions to 'f_defs' for |
paul@126 | 639 | the object having the given 'table_name' and the given 'structure_size', |
paul@126 | 640 | with 'table' details used to populate the definition. |
paul@126 | 641 | """ |
paul@126 | 642 | |
paul@126 | 643 | print >>f_decls, "extern const __table %s;\n" % table_name |
paul@126 | 644 | |
paul@126 | 645 | # Write the corresponding definition. |
paul@126 | 646 | |
paul@126 | 647 | print >>f_defs, "const __table %s = {\n %s,\n {\n %s\n }\n };\n" % ( |
paul@126 | 648 | table_name, structure_size, |
paul@126 | 649 | ",\n ".join(table)) |
paul@126 | 650 | |
paul@126 | 651 | def write_parameter_table(self, f_decls, f_defs, table_name, structure_size, table): |
paul@126 | 652 | |
paul@126 | 653 | """ |
paul@126 | 654 | Write the declarations to 'f_decls' and definitions to 'f_defs' for |
paul@126 | 655 | the object having the given 'table_name' and the given 'structure_size', |
paul@126 | 656 | with 'table' details used to populate the definition. |
paul@126 | 657 | """ |
paul@126 | 658 | |
paul@126 | 659 | print >>f_decls, "extern const __ptable %s;\n" % table_name |
paul@126 | 660 | |
paul@126 | 661 | # Write the corresponding definition. |
paul@126 | 662 | |
paul@126 | 663 | print >>f_defs, "const __ptable %s = {\n %s,\n {\n %s\n }\n };\n" % ( |
paul@126 | 664 | table_name, structure_size, |
paul@126 | 665 | ",\n ".join([("{%s, %s}" % t) for t in table])) |
paul@126 | 666 | |
paul@136 | 667 | def write_instance_structure(self, f_decls, path, structure_size): |
paul@126 | 668 | |
paul@126 | 669 | """ |
paul@136 | 670 | Write a declaration to 'f_decls' for the object having the given 'path' |
paul@136 | 671 | and the given 'structure_size'. |
paul@126 | 672 | """ |
paul@126 | 673 | |
paul@126 | 674 | # Write an instance-specific type definition for instances of classes. |
paul@126 | 675 | # See: templates/types.h |
paul@126 | 676 | |
paul@126 | 677 | print >>f_decls, """\ |
paul@126 | 678 | typedef struct { |
paul@126 | 679 | const __table * table; |
paul@126 | 680 | unsigned int pos; |
paul@126 | 681 | __attr attrs[%s]; |
paul@126 | 682 | } %s; |
paul@136 | 683 | """ % (structure_size, encode_symbol("obj", path)) |
paul@136 | 684 | |
paul@211 | 685 | def write_structure(self, f_decls, f_defs, structure_name, table_name, structure, path=None): |
paul@126 | 686 | |
paul@136 | 687 | """ |
paul@136 | 688 | Write the declarations to 'f_decls' and definitions to 'f_defs' for |
paul@136 | 689 | the object having the given 'structure_name', the given 'table_name', |
paul@211 | 690 | and the given 'structure' details used to populate the definition. |
paul@136 | 691 | """ |
paul@126 | 692 | |
paul@136 | 693 | if f_decls: |
paul@136 | 694 | print >>f_decls, "extern __obj %s;\n" % encode_path(structure_name) |
paul@136 | 695 | |
paul@136 | 696 | is_class = path and self.importer.get_object(path).has_kind("<class>") |
paul@132 | 697 | pos = is_class and encode_symbol("pos", encode_type_attribute(path)) or "0" |
paul@132 | 698 | |
paul@132 | 699 | print >>f_defs, """\ |
paul@132 | 700 | __obj %s = { |
paul@132 | 701 | &%s, |
paul@132 | 702 | %s, |
paul@132 | 703 | { |
paul@132 | 704 | %s |
paul@132 | 705 | }}; |
paul@132 | 706 | """ % ( |
paul@136 | 707 | encode_path(structure_name), table_name, pos, |
paul@126 | 708 | ",\n ".join(structure)) |
paul@126 | 709 | |
paul@132 | 710 | def get_argument_limits(self, path): |
paul@126 | 711 | |
paul@132 | 712 | """ |
paul@132 | 713 | Return the argument minimum and maximum for the callable at 'path', |
paul@132 | 714 | adding an argument position for a universal context. |
paul@132 | 715 | """ |
paul@132 | 716 | |
paul@126 | 717 | parameters = self.importer.function_parameters[path] |
paul@126 | 718 | defaults = self.importer.function_defaults.get(path) |
paul@132 | 719 | num_parameters = len(parameters) + 1 |
paul@132 | 720 | return num_parameters - (defaults and len(defaults) or 0), num_parameters |
paul@126 | 721 | |
paul@126 | 722 | def get_attribute_names(self, indexes): |
paul@126 | 723 | |
paul@126 | 724 | """ |
paul@126 | 725 | Given a list of attribute table 'indexes', return a list of attribute |
paul@126 | 726 | names. |
paul@126 | 727 | """ |
paul@126 | 728 | |
paul@126 | 729 | all_attrnames = self.optimiser.all_attrnames |
paul@126 | 730 | attrnames = [] |
paul@126 | 731 | for i in indexes: |
paul@126 | 732 | if i is None: |
paul@126 | 733 | attrnames.append(None) |
paul@126 | 734 | else: |
paul@126 | 735 | attrnames.append(all_attrnames[i]) |
paul@126 | 736 | return attrnames |
paul@126 | 737 | |
paul@126 | 738 | def get_static_attributes(self, kind, name, attrnames): |
paul@126 | 739 | |
paul@126 | 740 | """ |
paul@126 | 741 | Return a mapping of attribute names to paths for attributes belonging |
paul@126 | 742 | to objects of the given 'kind' (being "<class>" or "<module>") with |
paul@126 | 743 | the given 'name' and supporting the given 'attrnames'. |
paul@126 | 744 | """ |
paul@126 | 745 | |
paul@126 | 746 | attrs = {} |
paul@126 | 747 | |
paul@126 | 748 | for attrname in attrnames: |
paul@126 | 749 | if attrname is None: |
paul@126 | 750 | continue |
paul@126 | 751 | if kind == "<class>": |
paul@126 | 752 | path = self.importer.all_class_attrs[name][attrname] |
paul@126 | 753 | elif kind == "<module>": |
paul@126 | 754 | path = "%s.%s" % (name, attrname) |
paul@126 | 755 | else: |
paul@126 | 756 | continue |
paul@126 | 757 | |
paul@126 | 758 | # The module may be hidden. |
paul@126 | 759 | |
paul@126 | 760 | attr = self.importer.get_object(path) |
paul@126 | 761 | if not attr: |
paul@126 | 762 | module = self.importer.hidden.get(path) |
paul@126 | 763 | if module: |
paul@126 | 764 | attr = Reference(module.name, "<module>") |
paul@126 | 765 | attrs[attrname] = attr |
paul@126 | 766 | |
paul@126 | 767 | return attrs |
paul@126 | 768 | |
paul@126 | 769 | def get_instance_attributes(self, name, attrnames): |
paul@126 | 770 | |
paul@126 | 771 | """ |
paul@126 | 772 | Return a mapping of attribute names to references for attributes |
paul@126 | 773 | belonging to instances of the class with the given 'name', where the |
paul@126 | 774 | given 'attrnames' are supported. |
paul@126 | 775 | """ |
paul@126 | 776 | |
paul@126 | 777 | consts = self.importer.all_instance_attr_constants[name] |
paul@126 | 778 | attrs = {} |
paul@126 | 779 | for attrname in attrnames: |
paul@126 | 780 | if attrname is None: |
paul@126 | 781 | continue |
paul@126 | 782 | const = consts.get(attrname) |
paul@126 | 783 | attrs[attrname] = const or Reference("<var>", "%s.%s" % (name, attrname)) |
paul@126 | 784 | return attrs |
paul@126 | 785 | |
paul@357 | 786 | def populate_table(self, path, table): |
paul@126 | 787 | |
paul@126 | 788 | """ |
paul@126 | 789 | Traverse the attributes in the determined order for the structure having |
paul@357 | 790 | the given 'path', adding entries to the attribute 'table'. |
paul@126 | 791 | """ |
paul@126 | 792 | |
paul@357 | 793 | for attrname in self.optimiser.structures[path]: |
paul@126 | 794 | |
paul@126 | 795 | # Handle gaps in the structure. |
paul@126 | 796 | |
paul@126 | 797 | if attrname is None: |
paul@126 | 798 | table.append("0") |
paul@126 | 799 | else: |
paul@126 | 800 | table.append(encode_symbol("code", attrname)) |
paul@126 | 801 | |
paul@357 | 802 | def populate_parameter_table(self, parameters, table): |
paul@126 | 803 | |
paul@126 | 804 | """ |
paul@357 | 805 | Traverse the 'parameters' in the determined order, adding entries to the |
paul@357 | 806 | attribute 'table'. |
paul@126 | 807 | """ |
paul@126 | 808 | |
paul@357 | 809 | for value in parameters: |
paul@126 | 810 | |
paul@126 | 811 | # Handle gaps in the structure. |
paul@126 | 812 | |
paul@126 | 813 | if value is None: |
paul@126 | 814 | table.append(("0", "0")) |
paul@126 | 815 | else: |
paul@126 | 816 | name, pos = value |
paul@126 | 817 | table.append((encode_symbol("pcode", name), pos)) |
paul@126 | 818 | |
paul@523 | 819 | def populate_function(self, path, function_instance_attrs): |
paul@126 | 820 | |
paul@126 | 821 | """ |
paul@126 | 822 | Populate a structure for the function with the given 'path'. The given |
paul@523 | 823 | 'attrs' provide the instance attributes. |
paul@126 | 824 | """ |
paul@126 | 825 | |
paul@126 | 826 | structure = [] |
paul@523 | 827 | self.populate_structure(Reference("<function>", path), function_instance_attrs, "<instance>", structure) |
paul@126 | 828 | |
paul@126 | 829 | # Append default members. |
paul@126 | 830 | |
paul@126 | 831 | self.append_defaults(path, structure) |
paul@126 | 832 | return structure |
paul@126 | 833 | |
paul@523 | 834 | def populate_structure(self, ref, attrs, kind, structure): |
paul@126 | 835 | |
paul@126 | 836 | """ |
paul@126 | 837 | Traverse the attributes in the determined order for the structure having |
paul@126 | 838 | the given 'ref' whose members are provided by the 'attrs' mapping, in a |
paul@126 | 839 | structure of the given 'kind', adding entries to the object 'structure'. |
paul@126 | 840 | """ |
paul@126 | 841 | |
paul@174 | 842 | # Populate function instance structures for functions. |
paul@174 | 843 | |
paul@174 | 844 | if ref.has_kind("<function>"): |
paul@174 | 845 | origin = self.function_type |
paul@174 | 846 | structure_ref = Reference("<instance>", self.function_type) |
paul@174 | 847 | |
paul@174 | 848 | # Otherwise, just populate the appropriate structures. |
paul@126 | 849 | |
paul@174 | 850 | else: |
paul@174 | 851 | origin = ref.get_origin() |
paul@174 | 852 | structure_ref = ref |
paul@174 | 853 | |
paul@174 | 854 | for attrname in self.optimiser.structures[structure_ref]: |
paul@126 | 855 | |
paul@126 | 856 | # Handle gaps in the structure. |
paul@126 | 857 | |
paul@126 | 858 | if attrname is None: |
paul@477 | 859 | structure.append("__NULL") |
paul@126 | 860 | |
paul@126 | 861 | # Handle non-constant and constant members. |
paul@126 | 862 | |
paul@126 | 863 | else: |
paul@126 | 864 | attr = attrs[attrname] |
paul@126 | 865 | |
paul@136 | 866 | # Special function pointer member. |
paul@136 | 867 | |
paul@126 | 868 | if attrname == "__fn__": |
paul@126 | 869 | |
paul@523 | 870 | # Classes offer instantiators which can be called without a |
paul@523 | 871 | # context. |
paul@126 | 872 | |
paul@126 | 873 | if kind == "<class>": |
paul@126 | 874 | attr = encode_instantiator_pointer(attr) |
paul@523 | 875 | unbound_attr = attr |
paul@126 | 876 | |
paul@523 | 877 | # Provide bound method and unbound function pointers if |
paul@523 | 878 | # populating methods in a class. |
paul@126 | 879 | |
paul@126 | 880 | else: |
paul@126 | 881 | attr = encode_function_pointer(attr) |
paul@523 | 882 | unbound_attr = "__unbound_method" |
paul@126 | 883 | |
paul@523 | 884 | structure.append("{.inv=%s, .fn=%s}" % (unbound_attr, attr)) |
paul@126 | 885 | continue |
paul@126 | 886 | |
paul@136 | 887 | # Special argument specification member. |
paul@136 | 888 | |
paul@126 | 889 | elif attrname == "__args__": |
paul@357 | 890 | signature = self.get_signature_for_callable(ref.get_origin()) |
paul@357 | 891 | ptable = encode_tablename("<function>", signature) |
paul@357 | 892 | |
paul@174 | 893 | structure.append("{.min=%s, .ptable=&%s}" % (attr, ptable)) |
paul@126 | 894 | continue |
paul@126 | 895 | |
paul@136 | 896 | # Special internal data member. |
paul@136 | 897 | |
paul@136 | 898 | elif attrname == "__data__": |
paul@378 | 899 | structure.append("{.size=%d, .%s=%s}" % ( |
paul@378 | 900 | encode_literal_constant_size(attr), |
paul@378 | 901 | encode_literal_constant_member(attr), |
paul@378 | 902 | encode_literal_constant_value(attr))) |
paul@136 | 903 | continue |
paul@136 | 904 | |
paul@360 | 905 | # Special internal key member. |
paul@360 | 906 | |
paul@360 | 907 | elif attrname == "__key__": |
paul@360 | 908 | structure.append("{.code=%s, .pos=%s}" % (attr and encode_symbol("code", attr) or "0", |
paul@360 | 909 | attr and encode_symbol("pos", attr) or "0")) |
paul@360 | 910 | continue |
paul@360 | 911 | |
paul@251 | 912 | # Special cases. |
paul@251 | 913 | |
paul@499 | 914 | elif attrname in ("__file__", "__name__"): |
paul@251 | 915 | path = ref.get_origin() |
paul@397 | 916 | value_type = self.string_type |
paul@271 | 917 | |
paul@489 | 918 | # Provide constant values. These must match the values |
paul@489 | 919 | # originally recorded during inspection. |
paul@489 | 920 | |
paul@271 | 921 | if attrname == "__file__": |
paul@271 | 922 | module = self.importer.get_module(path) |
paul@271 | 923 | value = module.filename |
paul@489 | 924 | |
paul@489 | 925 | # Function and class names are leafnames. |
paul@489 | 926 | |
paul@499 | 927 | elif attrname == "__name__" and not ref.has_kind("<module>"): |
paul@489 | 928 | value = path.rsplit(".", 1)[-1] |
paul@489 | 929 | |
paul@489 | 930 | # All other names just use the object path information. |
paul@489 | 931 | |
paul@271 | 932 | else: |
paul@271 | 933 | value = path |
paul@271 | 934 | |
paul@406 | 935 | encoding = None |
paul@406 | 936 | |
paul@406 | 937 | local_number = self.importer.all_constants[path][(value, value_type, encoding)] |
paul@251 | 938 | constant_name = "$c%d" % local_number |
paul@251 | 939 | attr_path = "%s.%s" % (path, constant_name) |
paul@251 | 940 | constant_number = self.optimiser.constant_numbers[attr_path] |
paul@251 | 941 | constant_value = "__const%d" % constant_number |
paul@251 | 942 | structure.append("%s /* %s */" % (constant_value, attrname)) |
paul@251 | 943 | continue |
paul@251 | 944 | |
paul@499 | 945 | elif attrname == "__parent__": |
paul@499 | 946 | path = ref.get_origin() |
paul@499 | 947 | |
paul@499 | 948 | # Parents of classes and functions are derived from their |
paul@499 | 949 | # object paths. |
paul@499 | 950 | |
paul@499 | 951 | value = path.rsplit(".", 1)[0] |
paul@499 | 952 | structure.append("{.context=0, .value=&%s}" % encode_path(value)) |
paul@499 | 953 | continue |
paul@499 | 954 | |
paul@318 | 955 | # Special class relationship attributes. |
paul@318 | 956 | |
paul@318 | 957 | elif is_type_attribute(attrname): |
paul@477 | 958 | structure.append("{.context=0, .value=&%s}" % encode_path(decode_type_attribute(attrname))) |
paul@318 | 959 | continue |
paul@318 | 960 | |
paul@406 | 961 | # All other kinds of members. |
paul@406 | 962 | |
paul@126 | 963 | structure.append(self.encode_member(origin, attrname, attr, kind)) |
paul@126 | 964 | |
paul@126 | 965 | def encode_member(self, path, name, ref, structure_type): |
paul@126 | 966 | |
paul@126 | 967 | """ |
paul@126 | 968 | Encode within the structure provided by 'path', the member whose 'name' |
paul@126 | 969 | provides 'ref', within the given 'structure_type'. |
paul@126 | 970 | """ |
paul@126 | 971 | |
paul@126 | 972 | kind = ref.get_kind() |
paul@126 | 973 | origin = ref.get_origin() |
paul@126 | 974 | |
paul@126 | 975 | # References to constant literals. |
paul@126 | 976 | |
paul@338 | 977 | if kind == "<instance>" and ref.is_constant_alias(): |
paul@338 | 978 | alias = ref.get_name() |
paul@126 | 979 | |
paul@406 | 980 | # Use the alias directly if appropriate. |
paul@406 | 981 | |
paul@406 | 982 | if alias.startswith("$c"): |
paul@406 | 983 | constant_value = encode_literal_constant(int(alias[2:])) |
paul@406 | 984 | return "%s /* %s */" % (constant_value, name) |
paul@406 | 985 | |
paul@126 | 986 | # Obtain a constant value directly assigned to the attribute. |
paul@126 | 987 | |
paul@338 | 988 | if self.optimiser.constant_numbers.has_key(alias): |
paul@338 | 989 | constant_number = self.optimiser.constant_numbers[alias] |
paul@406 | 990 | constant_value = encode_literal_constant(constant_number) |
paul@247 | 991 | return "%s /* %s */" % (constant_value, name) |
paul@126 | 992 | |
paul@400 | 993 | # Usage of predefined constants, currently only None supported. |
paul@400 | 994 | |
paul@400 | 995 | if kind == "<instance>" and origin == self.none_type: |
paul@400 | 996 | attr_path = encode_predefined_reference(self.none_value) |
paul@477 | 997 | return "{.context=&%s, .value=&%s} /* %s */" % (attr_path, attr_path, name) |
paul@400 | 998 | |
paul@400 | 999 | # Predefined constant members. |
paul@136 | 1000 | |
paul@136 | 1001 | if (path, name) in self.predefined_constant_members: |
paul@136 | 1002 | attr_path = encode_predefined_reference("%s.%s" % (path, name)) |
paul@477 | 1003 | return "{.context=&%s, .value=&%s} /* %s */" % (attr_path, attr_path, name) |
paul@136 | 1004 | |
paul@126 | 1005 | # General undetermined members. |
paul@126 | 1006 | |
paul@126 | 1007 | if kind in ("<var>", "<instance>"): |
paul@404 | 1008 | attr_path = encode_predefined_reference(self.none_value) |
paul@477 | 1009 | return "{.context=&%s, .value=&%s} /* %s */" % (attr_path, attr_path, name) |
paul@126 | 1010 | |
paul@126 | 1011 | # Set the context depending on the kind of attribute. |
paul@139 | 1012 | # For methods: {&<parent>, &<attr>} |
paul@126 | 1013 | # For other attributes: {&<attr>, &<attr>} |
paul@126 | 1014 | |
paul@126 | 1015 | else: |
paul@139 | 1016 | if kind == "<function>" and structure_type == "<class>": |
paul@139 | 1017 | parent = origin.rsplit(".", 1)[0] |
paul@139 | 1018 | context = "&%s" % encode_path(parent) |
paul@139 | 1019 | elif kind == "<instance>": |
paul@139 | 1020 | context = "&%s" % encode_path(origin) |
paul@139 | 1021 | else: |
paul@139 | 1022 | context = "0" |
paul@477 | 1023 | return "{.context=%s, .value=&%s}" % (context, encode_path(origin)) |
paul@126 | 1024 | |
paul@126 | 1025 | def append_defaults(self, path, structure): |
paul@126 | 1026 | |
paul@126 | 1027 | """ |
paul@126 | 1028 | For the given 'path', append default parameter members to the given |
paul@126 | 1029 | 'structure'. |
paul@126 | 1030 | """ |
paul@126 | 1031 | |
paul@126 | 1032 | for name, default in self.importer.function_defaults.get(path): |
paul@126 | 1033 | structure.append(self.encode_member(path, name, default, "<instance>")) |
paul@126 | 1034 | |
paul@159 | 1035 | def write_instantiator(self, f_code, f_signatures, path, init_ref): |
paul@126 | 1036 | |
paul@126 | 1037 | """ |
paul@159 | 1038 | Write an instantiator to 'f_code', with a signature to 'f_signatures', |
paul@159 | 1039 | for instances of the class with the given 'path', with 'init_ref' as the |
paul@159 | 1040 | initialiser function reference. |
paul@126 | 1041 | |
paul@126 | 1042 | NOTE: This also needs to initialise any __fn__ and __args__ members |
paul@126 | 1043 | NOTE: where __call__ is provided by the class. |
paul@126 | 1044 | """ |
paul@126 | 1045 | |
paul@132 | 1046 | parameters = self.importer.function_parameters[init_ref.get_origin()] |
paul@126 | 1047 | |
paul@126 | 1048 | print >>f_code, """\ |
paul@132 | 1049 | __attr %s(__attr __args[]) |
paul@126 | 1050 | { |
paul@159 | 1051 | /* Allocate the structure. */ |
paul@132 | 1052 | __args[0] = __new(&%s, &%s, sizeof(%s)); |
paul@159 | 1053 | |
paul@159 | 1054 | /* Call the initialiser. */ |
paul@146 | 1055 | %s(__args); |
paul@159 | 1056 | |
paul@159 | 1057 | /* Return the allocated object details. */ |
paul@132 | 1058 | return __args[0]; |
paul@126 | 1059 | } |
paul@126 | 1060 | """ % ( |
paul@126 | 1061 | encode_instantiator_pointer(path), |
paul@150 | 1062 | encode_tablename("<instance>", path), |
paul@150 | 1063 | encode_path(path), |
paul@150 | 1064 | encode_symbol("obj", path), |
paul@126 | 1065 | encode_function_pointer(init_ref.get_origin()) |
paul@126 | 1066 | ) |
paul@126 | 1067 | |
paul@291 | 1068 | print >>f_signatures, "#define __HAVE_%s" % encode_path(path) |
paul@159 | 1069 | print >>f_signatures, "__attr %s(__attr[]);" % encode_instantiator_pointer(path) |
paul@159 | 1070 | |
paul@159 | 1071 | # Write additional literal instantiators. These do not call the |
paul@159 | 1072 | # initialisers but instead populate the structures directly. |
paul@159 | 1073 | |
paul@159 | 1074 | if path in self.literal_instantiator_types: |
paul@283 | 1075 | if path in self.literal_mapping_types: |
paul@283 | 1076 | style = "mapping" |
paul@283 | 1077 | else: |
paul@283 | 1078 | style = "sequence" |
paul@283 | 1079 | |
paul@159 | 1080 | print >>f_code, """\ |
paul@159 | 1081 | __attr %s(__attr __args[], unsigned int number) |
paul@159 | 1082 | { |
paul@159 | 1083 | /* Allocate the structure. */ |
paul@159 | 1084 | __args[0] = __new(&%s, &%s, sizeof(%s)); |
paul@159 | 1085 | |
paul@283 | 1086 | /* Allocate a structure for the data and set it on the __data__ attribute. */ |
paul@283 | 1087 | %s(__args, number); |
paul@159 | 1088 | |
paul@159 | 1089 | /* Return the allocated object details. */ |
paul@159 | 1090 | return __args[0]; |
paul@159 | 1091 | } |
paul@159 | 1092 | """ % ( |
paul@159 | 1093 | encode_literal_instantiator(path), |
paul@159 | 1094 | encode_tablename("<instance>", path), |
paul@159 | 1095 | encode_path(path), |
paul@159 | 1096 | encode_symbol("obj", path), |
paul@283 | 1097 | encode_literal_data_initialiser(style) |
paul@159 | 1098 | ) |
paul@159 | 1099 | |
paul@232 | 1100 | print >>f_signatures, "__attr %s(__attr[], unsigned int);" % encode_literal_instantiator(path) |
paul@159 | 1101 | |
paul@146 | 1102 | def write_main_program(self, f_code, f_signatures): |
paul@146 | 1103 | |
paul@146 | 1104 | """ |
paul@146 | 1105 | Write the main program to 'f_code', invoking the program's modules. Also |
paul@146 | 1106 | write declarations for module main functions to 'f_signatures'. |
paul@146 | 1107 | """ |
paul@146 | 1108 | |
paul@146 | 1109 | print >>f_code, """\ |
paul@146 | 1110 | int main(int argc, char *argv[]) |
paul@159 | 1111 | { |
paul@190 | 1112 | __exc __tmp_exc; |
paul@272 | 1113 | |
paul@434 | 1114 | GC_INIT(); |
paul@434 | 1115 | |
paul@159 | 1116 | __Try |
paul@159 | 1117 | {""" |
paul@146 | 1118 | |
paul@186 | 1119 | for name in self.importer.order_modules(): |
paul@146 | 1120 | function_name = "__main_%s" % encode_path(name) |
paul@146 | 1121 | print >>f_signatures, "void %s();" % function_name |
paul@146 | 1122 | |
paul@354 | 1123 | # Omit the native modules. |
paul@146 | 1124 | |
paul@354 | 1125 | parts = name.split(".") |
paul@354 | 1126 | |
paul@354 | 1127 | if parts[0] != "native": |
paul@146 | 1128 | print >>f_code, """\ |
paul@165 | 1129 | %s();""" % function_name |
paul@146 | 1130 | |
paul@146 | 1131 | print >>f_code, """\ |
paul@159 | 1132 | } |
paul@190 | 1133 | __Catch(__tmp_exc) |
paul@159 | 1134 | { |
paul@477 | 1135 | if (__ISINSTANCE(__tmp_exc.arg, ((__attr) {.context=0, .value=&__builtins___exception_system_SystemExit}))) |
paul@464 | 1136 | return __load_via_object( |
paul@464 | 1137 | __load_via_object(__tmp_exc.arg.value, %s).value, |
paul@464 | 1138 | %s).intvalue; |
paul@464 | 1139 | |
paul@273 | 1140 | fprintf(stderr, "Program terminated due to exception: %%s.\\n", |
paul@272 | 1141 | __load_via_object( |
paul@477 | 1142 | %s((__attr[]) {__NULL, __tmp_exc.arg}).value, |
paul@273 | 1143 | %s).strvalue); |
paul@159 | 1144 | return 1; |
paul@159 | 1145 | } |
paul@477 | 1146 | |
paul@477 | 1147 | return 0; |
paul@146 | 1148 | } |
paul@273 | 1149 | """ % ( |
paul@464 | 1150 | encode_symbol("pos", "value"), |
paul@464 | 1151 | encode_symbol("pos", "__data__"), |
paul@273 | 1152 | encode_function_pointer("__builtins__.str.str"), |
paul@273 | 1153 | encode_symbol("pos", "__data__") |
paul@273 | 1154 | ) |
paul@146 | 1155 | |
paul@126 | 1156 | # vim: tabstop=4 expandtab shiftwidth=4 |