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