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