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