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