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