Lichen

Annotated generator.py

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