1 #!/usr/bin/env python 2 3 """ 4 Translate programs. 5 6 Copyright (C) 2015-2018, 2023, 2024 Paul Boddie <paul@boddie.org.uk> 7 8 This program is free software; you can redistribute it and/or modify it under 9 the terms of the GNU General Public License as published by the Free Software 10 Foundation; either version 3 of the License, or (at your option) any later 11 version. 12 13 This program is distributed in the hope that it will be useful, but WITHOUT 14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS 15 FOR A PARTICULAR PURPOSE. See the GNU General Public License for more 16 details. 17 18 You should have received a copy of the GNU General Public License along with 19 this program. If not, see <http://www.gnu.org/licenses/>. 20 """ 21 22 from common import AccessLocation, CommonModule, CommonOutput, Location, \ 23 first, get_builtin_class, init_item, is_newer, \ 24 predefined_constants 25 from encoders import encode_access_instruction, encode_access_instruction_arg, \ 26 encode_function_pointer, encode_literal_instantiator, \ 27 encode_instantiator_pointer, encode_path, encode_symbol, \ 28 encode_type_attribute, is_type_attribute, \ 29 type_ops, typename_ops 30 from errors import InspectError, TranslateError 31 from os.path import exists, join 32 from os import makedirs 33 from referencing import Reference, combine_types 34 from results import Result 35 from transresults import TrConstantValueRef, TrInstanceRef, \ 36 TrLiteralSequenceRef, TrResolvedNameRef, \ 37 AliasResult, AttrResult, Expression, InstantiationResult, \ 38 InvocationResult, LogicalOperationResult, \ 39 LogicalResult, NegationResult, PredefinedConstantRef, \ 40 ReturnRef 41 from StringIO import StringIO 42 import compiler 43 import sys 44 45 class Translator(CommonOutput): 46 47 "A program translator." 48 49 def __init__(self, importer, deducer, optimiser, output): 50 self.importer = importer 51 self.deducer = deducer 52 self.optimiser = optimiser 53 self.output = output 54 55 def to_output(self, reset=False, debug=False, gc_sections=False): 56 57 "Write a program to the configured output directory." 58 59 # Make a directory for the final sources. 60 61 output = join(self.output, "src") 62 63 if not exists(output): 64 makedirs(output) 65 66 # Clean the output directory of irrelevant data. 67 68 self.check_output("debug=%r gc_sections=%r" % (debug, gc_sections)) 69 70 for module in self.importer.modules.values(): 71 output_filename = join(output, "%s.c" % module.name) 72 73 # Do not generate modules in the native package. They are provided 74 # by native functionality source files. 75 76 parts = module.name.split(".") 77 78 if parts[0] != "native" and \ 79 (reset or is_newer(module.filename, output_filename)): 80 81 tm = TranslatedModule(module.name, self.importer, self.deducer, self.optimiser) 82 tm.translate(module.filename, output_filename) 83 84 85 86 def make_expression(expr): 87 88 "Make a new expression from the existing 'expr'." 89 90 if isinstance(expr, Result): 91 return expr 92 else: 93 return Expression(str(expr)) 94 95 96 97 # The actual translation process itself. 98 99 class TranslatedModule(CommonModule): 100 101 "A module translator." 102 103 def __init__(self, name, importer, deducer, optimiser): 104 CommonModule.__init__(self, name, importer) 105 self.deducer = deducer 106 self.optimiser = optimiser 107 108 # Output stream. 109 110 self.out_toplevel = self.out = None 111 self.indent = 0 112 self.tabstop = " " 113 114 # Recorded namespaces. 115 116 self.namespaces = [] 117 self.in_conditional = False 118 self.in_parameter_list = False 119 120 # Exception raising adjustments. 121 122 self.in_try_finally = False 123 self.in_try_except = False 124 125 # Attribute access and accessor counting. 126 127 self.attr_accesses = {} 128 self.attr_accessors = {} 129 130 # Special variable usage. 131 132 self.temp_usage = {} 133 134 # Initialise some data used for attribute access generation. 135 136 self.init_substitutions() 137 138 def __repr__(self): 139 return "TranslatedModule(%r, %r)" % (self.name, self.importer) 140 141 def translate(self, filename, output_filename): 142 143 """ 144 Parse the file having the given 'filename', writing the translation to 145 the given 'output_filename'. 146 """ 147 148 self.parse_file(filename) 149 150 # Collect function namespaces for separate processing. 151 152 self.record_namespaces(self.astnode) 153 154 # Reset the lambda naming (in order to obtain the same names again) and 155 # translate the program. 156 157 self.reset_lambdas() 158 159 self.out_toplevel = self.out = open(output_filename, "w") 160 try: 161 self.start_output() 162 163 # Process namespaces, writing the translation. 164 165 for path, node in self.namespaces: 166 self.process_namespace(path, node) 167 168 # Process the module namespace including class namespaces. 169 170 self.process_namespace([], self.astnode) 171 172 finally: 173 self.out.close() 174 175 def have_object(self): 176 177 "Return whether a namespace is a recorded object." 178 179 return self.importer.objects.get(self.get_namespace_path()) 180 181 def get_builtin_class(self, name): 182 183 "Return a reference to the actual object providing 'name'." 184 185 return self.importer.get_object(get_builtin_class(name)) 186 187 def is_method(self, path): 188 189 "Return whether 'path' is a method." 190 191 class_name, method_name = path.rsplit(".", 1) 192 return self.importer.classes.has_key(class_name) and class_name or None 193 194 def in_method(self): 195 196 "Return whether the current namespace provides a method." 197 198 return self.in_function and self.is_method(self.get_namespace_path()) 199 200 # Namespace recording. 201 202 def record_namespaces(self, node): 203 204 "Process the program structure 'node', recording namespaces." 205 206 for n in node.getChildNodes(): 207 self.record_namespaces_in_node(n) 208 209 def record_namespaces_in_node(self, node): 210 211 "Process the program structure 'node', recording namespaces." 212 213 # Function namespaces within modules, classes and other functions. 214 # Functions appearing within conditional statements are given arbitrary 215 # names. 216 217 if isinstance(node, compiler.ast.Function): 218 self.record_function_node(node, (self.in_conditional or self.in_function) and self.get_lambda_name() or node.name) 219 220 elif isinstance(node, compiler.ast.Lambda): 221 self.record_function_node(node, self.get_lambda_name()) 222 223 # Classes are visited, but may be ignored if inside functions. 224 225 elif isinstance(node, compiler.ast.Class): 226 self.enter_namespace(node.name) 227 if self.have_object(): 228 self.record_namespaces(node) 229 self.exit_namespace() 230 231 # Conditional nodes are tracked so that function definitions may be 232 # handled. Since "for" loops are converted to "while" loops, they are 233 # included here. 234 235 elif isinstance(node, (compiler.ast.For, compiler.ast.If, compiler.ast.While)): 236 in_conditional = self.in_conditional 237 self.in_conditional = True 238 self.record_namespaces(node) 239 self.in_conditional = in_conditional 240 241 # All other nodes are processed depth-first. 242 243 else: 244 self.record_namespaces(node) 245 246 def record_function_node(self, n, name): 247 248 """ 249 Record the given function, lambda, if expression or list comprehension 250 node 'n' with the given 'name'. 251 """ 252 253 self.in_function = True 254 self.enter_namespace(name) 255 256 if self.have_object(): 257 258 # Record the namespace path and the node itself. 259 260 self.namespaces.append((self.namespace_path[:], n)) 261 self.record_namespaces_in_node(n.code) 262 263 self.exit_namespace() 264 self.in_function = False 265 266 # Constant referencing. 267 268 def get_literal_instance(self, n, name=None): 269 270 """ 271 For node 'n', return a reference for the type of the given 'name', or if 272 'name' is not specified, deduce the type from the value. 273 """ 274 275 # Handle stray None constants (Sliceobj seems to produce them). 276 277 if name is None and n.value is None: 278 return self.process_name_node(compiler.ast.Name("None")) 279 280 if name in ("dict", "list", "tuple"): 281 ref = self.get_builtin_class(name) 282 return self.process_literal_sequence_node(n, name, ref, TrLiteralSequenceRef) 283 else: 284 value, typename, encoding = self.get_constant_value(n.value, n.literals) 285 ref = self.get_builtin_class(typename) 286 value_type = ref.get_origin() 287 288 path = self.get_namespace_path() 289 290 # Obtain the local numbering of the constant and thus the 291 # locally-qualified name. 292 293 local_number = self.importer.all_constants[path][(value, value_type, encoding)] 294 constant_name = "$c%d" % local_number 295 objpath = self.get_object_path(constant_name) 296 297 # Obtain the unique identifier for the constant. 298 299 number = self.optimiser.constant_numbers[objpath] 300 return TrConstantValueRef(constant_name, ref.instance_of(), value, number) 301 302 # Namespace translation. 303 304 def process_namespace(self, path, node): 305 306 """ 307 Process the namespace for the given 'path' defined by the given 'node'. 308 """ 309 310 self.namespace_path = path 311 312 if isinstance(node, (compiler.ast.Function, compiler.ast.Lambda)): 313 self.in_function = True 314 self.process_function_body_node(node) 315 else: 316 self.in_function = False 317 self.reset_temp_limits() 318 319 self.start_module() 320 self.process_structure(node) 321 self.end_module() 322 323 def process_structure(self, node): 324 325 "Process the given 'node' or result." 326 327 # Handle processing requests on results. 328 329 if isinstance(node, Result): 330 return node 331 332 # Handle processing requests on nodes. 333 334 else: 335 l = CommonModule.process_structure(self, node) 336 337 # Return indications of return statement usage. 338 339 if l and isinstance(l[-1], ReturnRef): 340 return l[-1] 341 else: 342 return None 343 344 def process_statement_node(self, node, is_lambda=False): 345 346 "Process the given statement 'node'." 347 348 self.reset_temp_counters() 349 350 if is_lambda: 351 self.result_target_name = "__result" 352 353 return CommonModule.process_statement_node(self, node) 354 355 def process_structure_node(self, n): 356 357 "Process the individual node 'n'." 358 359 # Plain statements emit their expressions. 360 361 if isinstance(n, compiler.ast.Discard): 362 expr = self.process_structure_node(n.expr) 363 self.statement(expr) 364 365 # Module import declarations. 366 367 elif isinstance(n, compiler.ast.From): 368 self.process_from_node(n) 369 370 # Nodes using operator module functions. 371 372 elif isinstance(n, compiler.ast.Operator): 373 return self.process_operator_node(n) 374 375 elif isinstance(n, compiler.ast.AugAssign): 376 self.process_augassign_node(n) 377 378 elif isinstance(n, compiler.ast.Compare): 379 return self.process_compare_node(n) 380 381 elif isinstance(n, compiler.ast.Slice): 382 return self.process_slice_node(n) 383 384 elif isinstance(n, compiler.ast.Sliceobj): 385 return self.process_sliceobj_node(n) 386 387 elif isinstance(n, compiler.ast.Subscript): 388 return self.process_subscript_node(n) 389 390 # Classes are visited, but may be ignored if inside functions. 391 392 elif isinstance(n, compiler.ast.Class): 393 self.process_class_node(n) 394 395 # Functions within namespaces have any dynamic defaults initialised. 396 397 elif isinstance(n, compiler.ast.Function): 398 self.process_function_node(n) 399 400 # Lambdas are replaced with references to separately-generated 401 # functions. 402 403 elif isinstance(n, compiler.ast.Lambda): 404 return self.process_lambda_node(n) 405 406 # Assignments. 407 408 elif isinstance(n, compiler.ast.Assign): 409 410 # Handle each assignment node. 411 412 for node in n.nodes: 413 self.process_assignment_node(node, n.expr) 414 415 # Accesses. 416 417 elif isinstance(n, compiler.ast.Getattr): 418 return self.process_attribute_access(n) 419 420 # Names. 421 422 elif isinstance(n, compiler.ast.Name): 423 return self.process_name_node(n) 424 425 # Loops and conditionals. 426 427 elif isinstance(n, compiler.ast.For): 428 self.process_for_node(n) 429 430 elif isinstance(n, compiler.ast.While): 431 self.process_while_node(n) 432 433 elif isinstance(n, compiler.ast.If): 434 self.process_if_node(n) 435 436 elif isinstance(n, (compiler.ast.And, compiler.ast.Or)): 437 return self.process_logical_node(n) 438 439 elif isinstance(n, compiler.ast.Not): 440 return self.process_not_node(n) 441 442 # Exception control-flow tracking. 443 444 elif isinstance(n, compiler.ast.TryExcept): 445 self.process_try_node(n) 446 447 elif isinstance(n, compiler.ast.TryFinally): 448 self.process_try_finally_node(n) 449 450 # Control-flow modification statements. 451 452 elif isinstance(n, compiler.ast.Break): 453 self.writestmt("break;") 454 455 elif isinstance(n, compiler.ast.Continue): 456 self.writestmt("continue;") 457 458 elif isinstance(n, compiler.ast.Raise): 459 self.process_raise_node(n) 460 461 elif isinstance(n, compiler.ast.Return): 462 return self.process_return_node(n) 463 464 # Print statements. 465 466 elif isinstance(n, (compiler.ast.Print, compiler.ast.Printnl)): 467 self.statement(self.process_print_node(n)) 468 469 # Invocations. 470 471 elif isinstance(n, compiler.ast.CallFunc): 472 return self.process_invocation_node(n) 473 474 elif isinstance(n, compiler.ast.Keyword): 475 return self.process_structure_node(n.expr) 476 477 # Constant usage. 478 479 elif isinstance(n, compiler.ast.Const): 480 return self.get_literal_instance(n) 481 482 elif isinstance(n, compiler.ast.Dict): 483 return self.get_literal_instance(n, "dict") 484 485 elif isinstance(n, compiler.ast.List): 486 return self.get_literal_instance(n, "list") 487 488 elif isinstance(n, compiler.ast.Tuple): 489 return self.get_literal_instance(n, "tuple") 490 491 # All other nodes are processed depth-first. 492 493 else: 494 return self.process_structure(n) 495 496 def process_assignment_node(self, n, expr): 497 498 "Process the individual node 'n' to be assigned the contents of 'expr'." 499 500 # Names and attributes are assigned the entire expression. 501 502 if isinstance(n, compiler.ast.AssName): 503 name_ref = self.process_name_node(n, expr, True) 504 self.statement(name_ref) 505 506 # Employ guards after assignments if required. 507 508 if expr and name_ref.is_name(): 509 self.generate_guard(name_ref.name) 510 511 elif isinstance(n, compiler.ast.AssAttr): 512 in_assignment = self.in_assignment 513 self.result_target_name = "*__get_attr_ref(%d)" % self.attribute_ref_index 514 self.in_assignment = self.process_structure_node(expr) 515 self.statement(self.process_attribute_access(n)) 516 self.in_assignment = in_assignment 517 518 # Lists and tuples are matched against the expression and their 519 # items assigned to expression items. 520 521 elif isinstance(n, (compiler.ast.AssList, compiler.ast.AssTuple)): 522 self.process_assignment_node_items(n, expr) 523 524 # Slices and subscripts are permitted within assignment nodes. 525 526 elif isinstance(n, compiler.ast.Slice): 527 self.statement(self.process_slice_node(n, expr)) 528 529 elif isinstance(n, compiler.ast.Subscript): 530 self.statement(self.process_subscript_node(n, expr)) 531 532 def process_attribute_access(self, n): 533 534 "Process the given attribute access node 'n'." 535 536 # Obtain any completed chain and return the reference to it. 537 538 attr_expr = self.process_attribute_chain(n) 539 if self.have_access_expression(n): 540 return attr_expr 541 542 # Where the start of the chain of attributes has been reached, process 543 # the complete access. 544 545 name_ref = attr_expr and attr_expr.is_name() and attr_expr 546 name = name_ref and self.get_name_for_tracking(name_ref.name, name_ref) or None 547 548 location = self.get_access_location(name, self.attrs) 549 refs = self.get_referenced_attributes(location) 550 551 # Generate access instructions. 552 553 subs = { 554 "<expr>" : attr_expr, 555 "<name>" : attr_expr, 556 "<assexpr>" : self.in_assignment, 557 "<new_context>" : self.result_target_name or "__NULL", 558 } 559 560 subs.update(self.temp_subs) 561 subs.update(self.op_subs) 562 563 output = [] 564 substituted = set() 565 566 # The context set or retrieved will be that used by any enclosing 567 # invocation. 568 569 accessor_index = self.accessor_index 570 attribute_ref_index = self.attribute_ref_index 571 context_index = self.context_index 572 context_identity = None 573 context_identity_verified = False 574 final_identity = None 575 accessor_test = False 576 accessor_stored = False 577 attribute_ref_stored = False 578 579 # Obtain encoded versions of each instruction, accumulating temporary 580 # variables. 581 582 for instruction in self.deducer.access_instructions[location]: 583 584 # Intercept a special instruction identifying the context. 585 586 if instruction[0] in ("<context_identity>", "<context_identity_verified>"): 587 context_identity, _substituted = \ 588 encode_access_instruction_arg(instruction[1], subs, instruction[0], 589 accessor_index, context_index, 590 attribute_ref_index) 591 context_identity_verified = instruction[0] == "<context_identity_verified>" 592 continue 593 594 # Intercept a special instruction identifying the target. The value 595 # is not encoded since it is used internally. 596 597 elif instruction[0] == "<final_identity>": 598 final_identity = instruction[1] 599 continue 600 601 # Modify test instructions. 602 603 elif instruction[0] in typename_ops or instruction[0] in type_ops: 604 instruction = ("__to_error", instruction) 605 accessor_test = True 606 607 # Detect accessor storage. 608 609 elif instruction[0] == "<set_accessor>": 610 accessor_stored = True 611 612 # Detect attribute reference storage. 613 614 elif instruction[0] == "<set_attr_ref>": 615 attribute_ref_stored = True 616 617 # Collect the encoded instruction, noting any temporary variables 618 # required by it. 619 620 encoded, _substituted = encode_access_instruction(instruction, subs, 621 accessor_index, context_index, 622 attribute_ref_index) 623 output.append(encoded) 624 substituted.update(_substituted) 625 626 # Record temporary name usage. 627 628 temps = set() 629 630 for sub in substituted: 631 if self.temp_subs.has_key(sub): 632 temps.add(self.temp_subs[sub]) 633 634 for temp in temps: 635 self.next_temp(temp) 636 637 # Get full final identity details. 638 639 if final_identity and not refs: 640 refs = set([self.importer.identify(final_identity)]) 641 642 del self.attrs[0] 643 return AttrResult(output, refs, location, 644 context_identity, context_identity_verified, 645 accessor_test, accessor_stored, attribute_ref_stored) 646 647 def init_substitutions(self): 648 649 """ 650 Initialise substitutions, defining temporary variable mappings, some of 651 which are also used as substitutions, together with operation mappings 652 used as substitutions in instructions defined by the optimiser. 653 """ 654 655 self.temp_subs = { 656 657 # Substitutions used by instructions. 658 659 "<private_context>" : "__tmp_private_context", 660 "<target_accessor>" : "__tmp_target_value", 661 662 # Mappings to be replaced by those given below. 663 664 "<accessor>" : "__tmp_values", 665 "<attr_ref>" : "__tmp_attr_refs", 666 "<context>" : "__tmp_contexts", 667 "<test_context_revert>" : "__tmp_contexts", 668 "<test_context_static>" : "__tmp_contexts", 669 "<set_context>" : "__tmp_contexts", 670 "<set_private_context>" : "__tmp_private_context", 671 "<set_accessor>" : "__tmp_values", 672 "<set_attr_ref>" : "__tmp_attr_refs", 673 "<set_target_accessor>" : "__tmp_target_value", 674 } 675 676 self.op_subs = { 677 "<accessor>" : "__get_accessor", 678 "<attr_ref>" : "__get_attr_ref", 679 "<context>" : "__get_context", 680 "<test_context_revert>" : "__test_context_revert", 681 "<test_context_static>" : "__test_context_static", 682 "<set_context>" : "__set_context", 683 "<set_private_context>" : "__set_private_context", 684 "<set_accessor>" : "__set_accessor", 685 "<set_attr_ref>" : "__set_attr_ref", 686 "<set_target_accessor>" : "__set_target_accessor", 687 } 688 689 def get_referenced_attributes(self, location): 690 691 """ 692 Convert 'location' to the form used by the deducer and retrieve any 693 identified attributes. 694 """ 695 696 # Determine whether any deduced references refer to the accessed 697 # attribute. 698 699 attrnames = location.attrnames 700 attrnames = attrnames and attrnames.split(".") 701 remaining = attrnames and len(attrnames) > 1 702 703 access_location = self.deducer.const_accesses.get(location) 704 705 if remaining and not access_location: 706 return set() 707 708 return self.deducer.get_references_for_access(access_location or location) 709 710 def get_referenced_attribute_invocations(self, location): 711 712 """ 713 Convert 'location' to the form used by the deducer and retrieve any 714 identified attribute invocation details. 715 """ 716 717 access_location = self.deducer.const_accesses.get(location) 718 return self.deducer.reference_invocations_unsuitable.get(access_location or location) 719 720 def get_accessor_kinds(self, location): 721 722 "Return the accessor kinds for 'location'." 723 724 return self.deducer.accessor_kinds.get(location) 725 726 def get_access_location(self, name, attrnames=None): 727 728 """ 729 Using the current namespace, the given 'name', and the 'attrnames' 730 employed in an access, return the access location. 731 """ 732 733 path = self.get_path_for_access() 734 735 # Get the location used by the deducer and optimiser and find any 736 # recorded access. 737 738 attrnames = attrnames and ".".join(self.attrs) 739 access_number = self.get_access_number(path, name, attrnames) 740 self.update_access_number(path, name, attrnames) 741 return AccessLocation(path, name, attrnames, access_number) 742 743 def get_access_number(self, path, name, attrnames): 744 access = name, attrnames 745 if self.attr_accesses.has_key(path) and self.attr_accesses[path].has_key(access): 746 return self.attr_accesses[path][access] 747 else: 748 return 0 749 750 def update_access_number(self, path, name, attrnames): 751 access = name, attrnames 752 if name: 753 init_item(self.attr_accesses, path, dict) 754 init_item(self.attr_accesses[path], access, lambda: 0) 755 self.attr_accesses[path][access] += 1 756 757 def get_accessor_location(self, name): 758 759 """ 760 Using the current namespace and the given 'name', return the accessor 761 location. 762 """ 763 764 path = self.get_path_for_access() 765 766 # Get the location used by the deducer and optimiser and find any 767 # recorded accessor. 768 769 version = self.get_accessor_number(path, name) 770 self.update_accessor_number(path, name) 771 return Location(path, name, None, version) 772 773 def get_accessor_number(self, path, name): 774 if self.attr_accessors.has_key(path) and self.attr_accessors[path].has_key(name): 775 return self.attr_accessors[path][name] 776 else: 777 return 0 778 779 def update_accessor_number(self, path, name): 780 if name: 781 init_item(self.attr_accessors, path, dict) 782 init_item(self.attr_accessors[path], name, lambda: 0) 783 self.attr_accessors[path][name] += 1 784 785 def process_class_node(self, n): 786 787 "Process the given class node 'n'." 788 789 class_name = self.get_object_path(n.name) 790 791 # Where a class is set conditionally or where the name may refer to 792 # different values, assign the name. 793 794 ref = self.importer.identify(class_name) 795 796 if not ref.static(): 797 self.process_assignment_for_object(n.name, 798 make_expression("__ATTRVALUE(&%s)" % encode_path(class_name))) 799 800 self.enter_namespace(n.name) 801 802 if self.have_object(): 803 self.write_comment("Class: %s" % class_name) 804 805 self.initialise_inherited_members(class_name) 806 807 self.process_structure(n) 808 self.write_comment("End class: %s" % class_name) 809 810 self.exit_namespace() 811 812 def initialise_inherited_members(self, class_name): 813 814 "Initialise members of 'class_name' inherited from its ancestors." 815 816 for name, path in self.importer.all_class_attrs[class_name].items(): 817 target = "%s.%s" % (class_name, name) 818 819 # Ignore attributes with definitions. 820 821 ref = self.importer.identify(target) 822 if ref: 823 continue 824 825 # Ignore special type attributes. 826 827 if is_type_attribute(name): 828 continue 829 830 # Reference inherited attributes. 831 832 ref = self.importer.identify(path) 833 if ref and not ref.static(): 834 parent, attrname = path.rsplit(".", 1) 835 836 self.writestmt("__store_via_attr_ref(__get_object_attr_ref(&%s, %s), __load_via_object(&%s, %s));" % ( 837 encode_path(class_name), name, 838 encode_path(parent), attrname 839 )) 840 841 def process_from_node(self, n): 842 843 "Process the given node 'n', importing from another module." 844 845 path = self.get_namespace_path() 846 847 # Attempt to obtain the referenced objects. 848 849 for name, alias in n.names: 850 if name == "*": 851 raise InspectError("Only explicitly specified names can be imported from modules.", path, n) 852 853 # Obtain the path of the assigned name. 854 855 objpath = self.get_object_path(alias or name) 856 857 # Obtain the identity of the name. 858 859 ref = self.importer.identify(objpath) 860 861 # Where the name is not static, assign the value. 862 863 if ref and not ref.static() and ref.get_name(): 864 self.writestmt("%s;" % 865 TrResolvedNameRef(alias or name, Reference("<var>", None, objpath), 866 expr=TrResolvedNameRef(name, ref))) 867 868 def process_function_body_node(self, n): 869 870 """ 871 Process the given function, lambda, if expression or list comprehension 872 node 'n', generating the body. 873 """ 874 875 function_name = self.get_namespace_path() 876 self.start_function(function_name) 877 878 # Process the function body. 879 880 in_conditional = self.in_conditional 881 self.in_conditional = False 882 883 # Reset temporary storage counters and limits. 884 885 self.reset_temp_limits() 886 887 # Reset result target storage details. To be effective, storage 888 # locations are not reused between statements. 889 890 self.max_result_target = 0 891 self.result_target = 0 892 893 # Volatile locals for exception handling. 894 895 self.volatile_locals = set() 896 897 # Process any guards defined for the parameters. 898 899 for name in self.importer.function_parameters.get(function_name): 900 self.generate_guard(name) 901 902 # Also support self in methods, since some mix-in methods may only work 903 # with certain descendant classes. 904 905 if self.in_method(): 906 self.generate_guard("self") 907 908 # Make assignments for .name entries in the parameters, provided this is 909 # a method. 910 911 if self.in_method(): 912 for name in self.importer.function_attr_initialisers.get(function_name) or []: 913 914 # Treat each assignment as a separate statement. 915 916 self.reset_temp_counters() 917 918 self.process_assignment_node( 919 compiler.ast.AssAttr(compiler.ast.Name("self"), name, "OP_ASSIGN"), 920 compiler.ast.Name(name)) 921 922 # Produce the body and any additional return statement. 923 924 is_lambda = isinstance(n, compiler.ast.Lambda) 925 926 expr = self.process_statement_node(n.code, is_lambda) or \ 927 self.in_method() and \ 928 function_name.rsplit(".", 1)[-1] == "__init__" and \ 929 TrResolvedNameRef("self", self.importer.function_locals[function_name]["self"]) or \ 930 PredefinedConstantRef("None") 931 932 if not isinstance(expr, ReturnRef): 933 self.writestmt("return %s;" % expr) 934 935 self.in_conditional = in_conditional 936 937 self.end_function(function_name) 938 939 def generate_guard(self, name): 940 941 """ 942 Get the accessor details for 'name', found in the current namespace, and 943 generate any guards defined for it. 944 """ 945 946 # Obtain the location, keeping track of assignment versions. 947 948 location = self.get_accessor_location(name) 949 test = self.deducer.accessor_guard_tests.get(location) 950 951 # Generate any guard from the deduced information. 952 953 if test: 954 guard, guard_type = test 955 956 if guard == "specific": 957 ref = first(self.deducer.accessor_all_types[location]) 958 argstr = "&%s" % encode_path(ref.get_origin()) 959 elif guard == "common": 960 ref = first(self.deducer.accessor_all_general_types[location]) 961 argstr = encode_path(encode_type_attribute(ref.get_origin())) 962 else: 963 return 964 965 # Write a test that raises a TypeError upon failure. 966 967 self.writestmt("if (!__test_%s_%s(__VALUE(%s), %s)) __raise_type_error();" % ( 968 guard, guard_type, encode_path(name), argstr)) 969 970 def process_function_node(self, n): 971 972 """ 973 Process the given function, lambda, if expression or list comprehension 974 node 'n', generating any initialisation statements. 975 """ 976 977 # Where a function is declared conditionally, use a separate name for 978 # the definition, and assign the definition to the stated name. 979 980 original_name = n.name 981 982 if self.in_conditional or self.in_function: 983 name = self.get_lambda_name() 984 else: 985 name = n.name 986 987 objpath = self.get_object_path(name) 988 989 # Obtain details of the defaults. 990 991 defaults = self.process_function_defaults(n, name, objpath) 992 if defaults: 993 for default in defaults: 994 self.writeline("%s;" % default) 995 996 # Where a function is set conditionally or where the name may refer to 997 # different values, assign the name. 998 999 ref = self.importer.identify(objpath) 1000 1001 if self.in_conditional or self.in_function: 1002 self.process_assignment_for_object(original_name, compiler.ast.Name(name)) 1003 elif not ref.static(): 1004 context = self.is_method(objpath) 1005 1006 self.process_assignment_for_object(original_name, 1007 make_expression("__ATTRVALUE(&%s)" % encode_path(objpath))) 1008 1009 def process_function_defaults(self, n, name, objpath, instance_name=None): 1010 1011 """ 1012 Process the given function or lambda node 'n', initialising defaults 1013 that are dynamically set. The given 'name' indicates the name of the 1014 function. The given 'objpath' indicates the origin of the function. 1015 The given 'instance_name' indicates the name of any separate instance 1016 of the function created to hold the defaults. 1017 1018 Return a list of operations setting defaults on a function instance. 1019 """ 1020 1021 function_name = self.get_object_path(name) 1022 function_defaults = self.importer.function_defaults.get(function_name) 1023 if not function_defaults: 1024 return None 1025 1026 # Determine whether any unidentified defaults are involved. 1027 1028 for argname, default in function_defaults: 1029 if not default.static(): 1030 break 1031 else: 1032 return None 1033 1034 # Handle bound methods. 1035 1036 if not instance_name: 1037 instance_name = "&%s" % encode_path(objpath) 1038 else: 1039 instance_name = "__VALUE(%s)" % instance_name 1040 1041 # Where defaults are involved but cannot be identified, obtain a new 1042 # instance of the lambda and populate the defaults. 1043 1044 defaults = [] 1045 1046 # Join the original defaults with the inspected defaults. 1047 1048 original_defaults = [(argname, default) for (argname, default) in compiler.ast.get_defaults(n) if default] 1049 1050 for i, (original, inspected) in enumerate(map(None, original_defaults, function_defaults)): 1051 1052 # Obtain any reference for the default. 1053 1054 if original: 1055 argname, default = original 1056 name_ref = self.process_structure_node(default) 1057 elif inspected: 1058 argname, default = inspected 1059 name_ref = TrResolvedNameRef(argname, default) 1060 else: 1061 continue 1062 1063 # Generate default initialisers except when constants are employed. 1064 # Constants should be used when populating the function structures. 1065 1066 if name_ref and not isinstance(name_ref, TrConstantValueRef): 1067 defaults.append("__SETDEFAULT(%s, %s, %s)" % (instance_name, i, name_ref)) 1068 1069 return defaults 1070 1071 def process_if_node(self, n): 1072 1073 """ 1074 Process the given "if" node 'n'. 1075 """ 1076 1077 first = True 1078 for test, body in n.tests: 1079 test_ref = self.process_statement_node(test) 1080 self.start_if(first, test_ref) 1081 1082 in_conditional = self.in_conditional 1083 self.in_conditional = True 1084 self.process_statement_node(body) 1085 self.in_conditional = in_conditional 1086 1087 self.end_if() 1088 first = False 1089 1090 if n.else_: 1091 self.start_else() 1092 self.process_statement_node(n.else_) 1093 self.end_else() 1094 1095 print >>self.out 1096 1097 def process_invocation_node(self, n): 1098 1099 "Process the given invocation node 'n'." 1100 1101 # Process the expression. 1102 1103 expr = self.process_structure_node(n.node) 1104 1105 # Obtain details of the invocation expression. 1106 1107 objpath = expr.get_origin() 1108 location = expr.access_location() 1109 refs = expr.references() 1110 1111 # Identified target details. 1112 1113 target = None 1114 target_structure = None 1115 1116 # Specific function target information. 1117 1118 function = None 1119 1120 # Instantiation involvement. 1121 1122 instantiation = False 1123 literal_instantiation = False 1124 1125 # Invocation requirements. 1126 1127 context_required = True 1128 have_access_context = isinstance(expr, AttrResult) 1129 1130 # The context identity is merely the thing providing the context. 1131 # A verified context is one that does not need further testing for 1132 # suitability. 1133 1134 context_identity = have_access_context and expr.context() 1135 context_verified = have_access_context and expr.context_verified() 1136 1137 # The presence of any test operations in the accessor expression. 1138 # With such operations present, the expression cannot be eliminated. 1139 1140 tests_accessor = have_access_context and expr.tests_accessor() 1141 stores_accessor = have_access_context and expr.stores_accessor() 1142 1143 # Parameter details and parameter list dimensions. 1144 1145 parameters = None 1146 num_parameters = None 1147 num_defaults = None 1148 1149 # Obtain details of the callable and of its parameters. 1150 1151 # Literals may be instantiated specially. 1152 1153 if expr.is_name() and expr.name.startswith("$L") and objpath: 1154 instantiation = literal_instantiation = expr 1155 target = encode_literal_instantiator(objpath) 1156 context_required = False 1157 1158 # Identified targets employ function pointers directly. 1159 1160 elif objpath: 1161 parameters = self.importer.function_parameters.get(objpath) 1162 function_defaults = self.importer.function_defaults.get(objpath) 1163 num_parameters = parameters and len(parameters) or 0 1164 num_defaults = function_defaults and len(function_defaults) or 0 1165 1166 # Class invocation involves instantiators. 1167 1168 if expr.has_kind("<class>"): 1169 instantiation = expr 1170 target = encode_instantiator_pointer(objpath) 1171 init_ref = self.importer.all_class_attrs[objpath]["__init__"] 1172 target_structure = "&%s" % encode_path(init_ref) 1173 context_required = False 1174 1175 # Only plain functions and bound methods employ function pointers. 1176 1177 elif expr.has_kind("<function>"): 1178 function = objpath 1179 1180 # Test for functions and methods. 1181 1182 context_required = self.is_method(objpath) 1183 1184 accessor_kinds = location and self.get_accessor_kinds(location) 1185 1186 instance_accessor = accessor_kinds and \ 1187 len(accessor_kinds) == 1 and \ 1188 first(accessor_kinds) == "<instance>" 1189 1190 # Only identify certain bound methods or functions. 1191 1192 if not context_required or instance_accessor: 1193 target = encode_function_pointer(objpath) 1194 1195 # Access bound method defaults even if it is not clear whether 1196 # the accessor is appropriate. 1197 1198 target_structure = "&%s" % encode_path(objpath) 1199 1200 # Other targets are retrieved at run-time. 1201 1202 else: 1203 if location: 1204 attrnames = location.attrnames 1205 attrname = attrnames and attrnames.rsplit(".", 1)[-1] 1206 1207 # Determine common aspects of any identifiable targets. 1208 1209 if attrname or refs: 1210 all_params = set() 1211 all_defaults = set() 1212 min_params = set() 1213 max_params = set() 1214 1215 # Employ references from the expression or find all 1216 # possible attributes for the given attribute name. 1217 1218 refs = refs or self.get_attributes_for_attrname(attrname) 1219 1220 # Obtain parameters and defaults for each possible target. 1221 1222 for ref in refs: 1223 origin = ref.get_origin() 1224 params = self.importer.function_parameters.get(origin) 1225 1226 defaults = self.importer.function_defaults.get(origin) 1227 if defaults is not None: 1228 all_defaults.add(tuple(defaults)) 1229 1230 if params is not None: 1231 all_params.add(tuple(params)) 1232 min_params.add(len(params) - (defaults and len(defaults) or 0)) 1233 max_params.add(len(params)) 1234 else: 1235 refs = set() 1236 break 1237 1238 # Where the parameters and defaults are always the same, 1239 # permit populating them in advance. 1240 1241 if refs: 1242 if self.uses_keyword_arguments(n): 1243 if len(all_params) == 1 and (not all_defaults or len(all_defaults) == 1): 1244 parameters = first(all_params) 1245 function_defaults = all_defaults and first(all_defaults) or [] 1246 num_parameters = parameters and len(parameters) or 0 1247 num_defaults = function_defaults and len(function_defaults) or 0 1248 else: 1249 if len(min_params) == 1 and len(max_params) == 1: 1250 num_parameters = first(max_params) 1251 num_defaults = first(max_params) - first(min_params) 1252 1253 # Some information about the target may be available and be used to 1254 # provide warnings about argument compatibility. 1255 1256 if self.importer.give_warning("args"): 1257 unsuitable = self.get_referenced_attribute_invocations(location) 1258 1259 if unsuitable: 1260 for ref in unsuitable: 1261 _objpath = ref.get_origin() 1262 print >>sys.stderr, \ 1263 "In %s, at line %d, inappropriate number of " \ 1264 "arguments given. Need %d arguments to call %s." % ( 1265 self.get_namespace_path(), n.lineno, 1266 len(self.importer.function_parameters[_objpath]), 1267 _objpath) 1268 1269 # Logical statement about available parameter information. 1270 1271 known_parameters = num_parameters is not None 1272 1273 # The source of context information: target or temporary. 1274 1275 need_context_target = context_required and not have_access_context 1276 1277 need_context_stored = context_required and context_identity and \ 1278 context_identity.startswith("__get_context") 1279 1280 # Determine any readily-accessible target identity. 1281 1282 target_named = expr.is_name() and str(expr) or None 1283 target_identity = target or target_named 1284 1285 # Use of target information to populate defaults. 1286 1287 defaults_target_var = not (parameters and function_defaults is not None) and \ 1288 known_parameters and len(n.args) < num_parameters 1289 1290 # Use of a temporary target variable in these situations: 1291 # 1292 # A target provided by an expression needed for defaults. 1293 # 1294 # A target providing the context but not using a name to do so. 1295 # 1296 # A target expression involving the definition of a context which may 1297 # then be evaluated and stored to ensure that the context is available 1298 # during argument evaluation. 1299 # 1300 # An expression featuring an accessor test. 1301 1302 need_target_stored = defaults_target_var and not target_identity or \ 1303 need_context_target and not target_identity or \ 1304 need_context_stored or \ 1305 tests_accessor and not target 1306 1307 # Define stored target details. 1308 1309 target_stored = "__tmp_targets[%d]" % self.function_target 1310 target_var = need_target_stored and target_stored or target_identity 1311 1312 if need_target_stored: 1313 self.record_temp("__tmp_targets") 1314 1315 if need_context_stored: 1316 self.record_temp("__tmp_contexts") 1317 1318 if stores_accessor: 1319 self.record_temp("__tmp_values") 1320 1321 # NOTE: Special case for optimisation. 1322 1323 int_type = "__builtins__.int.int" 1324 result_type = target and objpath == "native.int.is_int" and int_type or None 1325 yields_integer = result_type == int_type 1326 1327 # Employ result targets only in functions. 1328 1329 result_target = None 1330 1331 if self.in_function: 1332 if self.result_target_name: 1333 result_target = self.result_target_name 1334 self.result_target_name = None 1335 1336 # Reserve a temporary result target only if it will be used. 1337 1338 elif not literal_instantiation and not yields_integer: 1339 result_target = "__tmp_results[%d]" % self.result_target 1340 self.next_temp("__tmp_results") 1341 1342 # Arguments are presented in a temporary frame array with any context 1343 # always being the first argument. Where it would be unused, it may be 1344 # set to null. 1345 1346 if context_required: 1347 if have_access_context: 1348 context_arg = context_identity 1349 else: 1350 context_arg = "__CONTEXT_AS_VALUE(%s)" % target_var 1351 else: 1352 context_arg = "__NULL" 1353 1354 # Start with result target and context arguments for each invocation. 1355 1356 args = [result_target or "__NULL", context_arg] 1357 reserved_args = 2 1358 1359 # Complete the array with null values, permitting tests for a complete 1360 # set of arguments. 1361 1362 args += [None] * (num_parameters is None and len(n.args) or num_parameters is not None and num_parameters or 0) 1363 kwcodes = [] 1364 kwargs = [] 1365 1366 # Any invocations in the arguments will store target details in a 1367 # different location. 1368 1369 function_target = self.function_target 1370 context_index = self.context_index 1371 accessor_index = self.accessor_index 1372 1373 if need_target_stored: 1374 self.next_target() 1375 1376 if need_context_stored: 1377 self.next_context() 1378 1379 if stores_accessor: 1380 self.next_accessor() 1381 1382 in_parameter_list = self.in_parameter_list 1383 self.in_parameter_list = True 1384 1385 for i, arg in enumerate(n.args): 1386 argexpr = self.process_structure_node(arg) 1387 1388 # Convert any attributes indicating value replacement. 1389 1390 if isinstance(argexpr, InvocationResult) and argexpr.result_target: 1391 argexprstr = "__set_attr(&%s, %s)" % (argexpr.result_target, argexpr) 1392 else: 1393 argexprstr = str(argexpr) 1394 1395 # Store a keyword argument, either in the argument list or 1396 # in a separate keyword argument list for subsequent lookup. 1397 1398 if isinstance(arg, compiler.ast.Keyword): 1399 1400 # With knowledge of the target, store the keyword 1401 # argument directly. 1402 1403 if parameters: 1404 try: 1405 argnum = parameters.index(arg.name) 1406 except ValueError: 1407 raise TranslateError("Argument %s is not recognised." % arg.name, 1408 self.get_namespace_path(), n) 1409 args[argnum + reserved_args] = argexprstr 1410 1411 # Otherwise, store the details in a separate collection. 1412 1413 else: 1414 kwargs.append(argexprstr) 1415 kwcodes.append("{%s, %s}" % ( 1416 encode_ppos(arg.name), encode_pcode(arg.name))) 1417 1418 # Store non-keyword arguments in the argument list, rejecting 1419 # superfluous arguments. 1420 1421 else: 1422 try: 1423 args[i + reserved_args] = argexprstr 1424 except IndexError: 1425 raise TranslateError("Too many arguments specified.", 1426 self.get_namespace_path(), n) 1427 1428 # Reference the current target again. 1429 1430 self.in_parameter_list = in_parameter_list 1431 1432 if not self.in_parameter_list: 1433 self.function_target = function_target 1434 self.context_index = context_index 1435 self.accessor_index = accessor_index 1436 1437 # Defaults are added to the frame where arguments are missing. 1438 1439 if parameters and function_defaults is not None: 1440 1441 # Visit each default and set any missing arguments. Where keyword 1442 # arguments have been used, the defaults must be inspected and, if 1443 # necessary, inserted into gaps in the argument list. 1444 1445 for i, (argname, default) in enumerate(function_defaults): 1446 argnum = parameters.index(argname) 1447 if not args[argnum + reserved_args]: 1448 args[argnum + reserved_args] = "__GETDEFAULT(%s, %d)" % (target_structure, i) 1449 1450 elif known_parameters: 1451 1452 # No specific parameter details are provided, but no keyword 1453 # arguments are used. Thus, defaults can be supplied using position 1454 # information only. 1455 1456 i = len(n.args) 1457 pos = i - (num_parameters - num_defaults) 1458 while i < num_parameters: 1459 args[i + reserved_args] = "__GETDEFAULT(%s.value, %d)" % (target_var, pos) 1460 i += 1 1461 pos += 1 1462 1463 # Test for missing arguments. 1464 1465 if None in args: 1466 raise TranslateError("Not all arguments supplied.", 1467 self.get_namespace_path(), n) 1468 1469 # Encode the arguments. 1470 1471 # Where literal instantiation is occurring, add an argument indicating 1472 # the number of values. The result target and context are excluded. 1473 1474 if literal_instantiation: 1475 final_args = args[reserved_args:] 1476 argstr = "%d, %s" % (len(final_args), ", ".join(final_args)) 1477 else: 1478 # NOTE: Special case for optimisation. 1479 1480 if yields_integer: 1481 final_args = args[reserved_args:] 1482 else: 1483 final_args = args 1484 1485 argstr = ", ".join(final_args) 1486 1487 kwargstr = kwargs and ("__ARGS(%s)" % ", ".join(kwargs)) or "0" 1488 kwcodestr = kwcodes and ("__KWARGS(%s)" % ", ".join(kwcodes)) or "0" 1489 1490 # First, the invocation expression is presented. 1491 1492 stages = [] 1493 emit = stages.append 1494 1495 # Assign and yield any stored target. 1496 # The context may be set in the expression. 1497 1498 if need_target_stored: 1499 emit("%s = %s" % (target_var, expr)) 1500 target_expr = target_var 1501 1502 # Otherwise, retain the expression for later use. 1503 1504 else: 1505 target_expr = str(expr) 1506 1507 # Any specific callable is then obtained for invocation. 1508 1509 if target: 1510 1511 # An expression involving a test of the accessor providing the target. 1512 # This must be emitted in order to perform the test. 1513 1514 if tests_accessor: 1515 emit(str(expr)) 1516 1517 emit(target) 1518 1519 # Methods accessed via unidentified accessors are obtained for 1520 # invocation. 1521 1522 elif function: 1523 if context_required: 1524 1525 # Avoid further context testing if appropriate. 1526 1527 if have_access_context and context_verified: 1528 emit("__get_function_member(%s)" % target_expr) 1529 1530 # Otherwise, test the context for the function/method. 1531 1532 else: 1533 emit("__get_function(%s, %s)" % (context_arg, target_expr)) 1534 else: 1535 emit("_get_function_member(%s)" % target_expr) 1536 1537 # With known parameters, the target can be tested. 1538 1539 elif known_parameters: 1540 if self.always_callable(refs): 1541 if context_verified: 1542 emit("__get_function_member(%s)" % target_expr) 1543 else: 1544 emit("__get_function(%s, %s)" % (context_arg, target_expr)) 1545 else: 1546 emit("__check_and_get_function(%s, %s)" % (context_arg, target_expr)) 1547 1548 # With a known target, the function is obtained directly and called. 1549 # By putting the invocation at the end of the final element in the 1550 # instruction sequence (the stages), the result becomes the result of 1551 # the sequence. Moreover, the parameters become part of the sequence 1552 # and thereby participate in a guaranteed evaluation order. 1553 1554 if target or function or known_parameters: 1555 stages[-1] += "(%s)" % argstr 1556 if instantiation: 1557 return InstantiationResult(instantiation, stages) 1558 else: 1559 # Provide the parameter details for possible optimisation when 1560 # translating the result. 1561 1562 return InvocationResult(result_target, stages, result_type, 1563 result_type and final_args or None) 1564 1565 # With unknown targets, the generic invocation function is applied to 1566 # the callable and argument collections. 1567 1568 else: 1569 emit("__invoke(\n%s,\n%d, %d, %s, %s,\n%d, %s\n)" % ( 1570 target_expr, 1571 self.always_callable(refs) and 1 or 0, 1572 len(kwargs), kwcodestr, kwargstr, 1573 len(args), "__ARGS(%s)" % argstr)) 1574 return InvocationResult(result_target, stages) 1575 1576 def reset_temp_counters(self): 1577 1578 "Reset the target counters." 1579 1580 self.function_target = 0 1581 self.context_index = 0 1582 self.accessor_index = 0 1583 self.attribute_ref_index = 0 1584 self.result_target_name = None 1585 1586 def reset_temp_limits(self): 1587 1588 "Reset the target counter limits." 1589 1590 self.max_function_target = 0 1591 self.max_context_index = 0 1592 self.max_accessor_index = 0 1593 self.max_attribute_ref_index = 0 1594 1595 def next_temp(self, name): 1596 1597 "Allocate the next temporary storage element for 'name'." 1598 1599 if name == "__tmp_results": 1600 self.next_result() 1601 elif name == "__tmp_targets": 1602 self.next_target() 1603 elif name == "__tmp_contexts": 1604 self.next_context() 1605 elif name == "__tmp_values": 1606 self.next_accessor() 1607 elif name == "__tmp_attr_refs": 1608 self.next_attribute_ref() 1609 elif name in ("__tmp_private_context", "__tmp_target_value", "__tmp_result"): 1610 pass 1611 else: 1612 raise TranslateError("Temporary storage %s is not recognised." % name, 1613 self.get_namespace_path()) 1614 1615 self.record_temp(name) 1616 1617 def next_result(self): 1618 1619 "Allocate the next result target storage." 1620 1621 self.result_target += 1 1622 self.max_result_target = max(self.result_target, self.max_result_target) 1623 1624 def next_target(self): 1625 1626 "Allocate the next function target storage." 1627 1628 self.function_target += 1 1629 self.max_function_target = max(self.function_target, self.max_function_target) 1630 1631 def next_context(self): 1632 1633 "Allocate the next context value storage." 1634 1635 self.context_index += 1 1636 self.max_context_index = max(self.context_index, self.max_context_index) 1637 1638 def next_accessor(self): 1639 1640 "Allocate the next accessor value storage." 1641 1642 self.accessor_index += 1 1643 self.max_accessor_index = max(self.accessor_index, self.max_accessor_index) 1644 1645 def next_attribute_ref(self): 1646 1647 "Allocate the next attribute reference value storage." 1648 1649 self.attribute_ref_index += 1 1650 self.max_attribute_ref_index = max(self.attribute_ref_index, self.max_attribute_ref_index) 1651 1652 def always_callable(self, refs): 1653 1654 "Determine whether all 'refs' are callable." 1655 1656 if not refs: 1657 return False 1658 1659 for ref in refs: 1660 if not ref.has_kind("<function>") and not self.importer.get_attributes(ref, "__fn__"): 1661 return False 1662 1663 return True 1664 1665 def need_default_arguments(self, objpath, nargs): 1666 1667 """ 1668 Return whether any default arguments are needed when invoking the object 1669 given by 'objpath'. 1670 """ 1671 1672 parameters = self.importer.function_parameters.get(objpath) 1673 return nargs < len(parameters) 1674 1675 def uses_keyword_arguments(self, n): 1676 1677 "Return whether invocation node 'n' uses keyword arguments." 1678 1679 for arg in enumerate(n.args): 1680 if isinstance(arg, compiler.ast.Keyword): 1681 return True 1682 1683 return False 1684 1685 def get_attributes_for_attrname(self, attrname): 1686 1687 "Return a set of all attributes exposed by 'attrname'." 1688 1689 usage = [(attrname, True, False)] 1690 class_types = self.deducer.get_class_types_for_usage(usage) 1691 instance_types = self.deducer.get_instance_types_for_usage(usage) 1692 module_types = self.deducer.get_module_types_for_usage(usage) 1693 attrs = set() 1694 1695 for ref in combine_types(class_types, instance_types, module_types): 1696 attrs.update(self.importer.get_attributes(ref, attrname)) 1697 1698 return attrs 1699 1700 def process_lambda_node(self, n): 1701 1702 "Process the given lambda node 'n'." 1703 1704 name = self.get_lambda_name() 1705 function_name = self.get_object_path(name) 1706 instance_name = "__get_accessor(%d)" % self.accessor_index 1707 1708 defaults = self.process_function_defaults(n, name, function_name, instance_name) 1709 1710 # Without defaults, produce an attribute referring to the function. 1711 1712 if not defaults: 1713 return make_expression("__ATTRVALUE(&%s)" % encode_path(function_name)) 1714 1715 # With defaults, copy the function structure and set the defaults on the 1716 # copy. 1717 1718 else: 1719 self.record_temp("__tmp_values") 1720 accessor_index = self.accessor_index 1721 self.next_accessor() 1722 return make_expression("""\ 1723 (__set_accessor(%d, __ATTRVALUE(__COPY(&%s, sizeof(%s)))), 1724 %s, 1725 __get_accessor(%d))""" % ( 1726 accessor_index, 1727 encode_path(function_name), 1728 encode_symbol("obj", function_name), 1729 ", ".join(defaults), 1730 accessor_index)) 1731 1732 def process_logical_node(self, n): 1733 1734 "Process the given operator node 'n'." 1735 1736 self.record_temp("__tmp_result") 1737 1738 conjunction = isinstance(n, compiler.ast.And) 1739 results = [] 1740 1741 for node in n.nodes: 1742 results.append(self.process_structure_node(node)) 1743 1744 return LogicalOperationResult(results, conjunction) 1745 1746 def process_name_node(self, n, expr=None, process_expr=False): 1747 1748 "Process the given name node 'n' with the optional assignment 'expr'." 1749 1750 # Determine whether the name refers to a static external entity. 1751 1752 if n.name in predefined_constants: 1753 return PredefinedConstantRef(n.name, expr) 1754 1755 # Convert literal references, operator function names, and print 1756 # function names to references. 1757 1758 elif n.name.startswith("$L") or n.name.startswith("$op") or \ 1759 n.name.startswith("$seq") or n.name.startswith("$print"): 1760 1761 ref, paths = self.importer.get_module(self.name).special[n.name] 1762 return TrResolvedNameRef(n.name, ref) 1763 1764 # Get the appropriate name for the name reference, using the same method 1765 # as in the inspector. 1766 1767 path = self.get_namespace_path() 1768 objpath = self.get_object_path(n.name) 1769 1770 # Determine any assigned globals. 1771 1772 globals = self.importer.get_module(self.name).scope_globals.get(path) 1773 1774 # Explicitly declared globals. 1775 1776 if globals and n.name in globals: 1777 objpath = self.get_global_path(n.name) 1778 is_global = True 1779 1780 # Implicitly referenced globals in functions. 1781 1782 elif self.in_function: 1783 is_global = n.name not in self.importer.function_locals[path] 1784 1785 # Implicitly referenced globals elsewhere. 1786 1787 else: 1788 namespace = self.importer.identify(path) 1789 is_global = not self.importer.get_attributes(namespace, n.name) 1790 1791 # Get the static identity of the name. 1792 1793 ref = self.importer.identify(objpath) 1794 if ref and not ref.get_name(): 1795 ref = ref.alias(objpath) 1796 1797 # Obtain any resolved names for non-assignment names. 1798 1799 if not expr and not ref and self.in_function: 1800 locals = self.importer.function_locals.get(path) 1801 ref = locals and locals.get(n.name) 1802 1803 # Find any invocation or alias details. 1804 1805 name = self.get_name_for_tracking(n.name, is_global=is_global) 1806 location = not expr and self.get_access_location(name) or None 1807 1808 # Mark any local assignments as volatile in exception blocks. 1809 1810 if expr and self.in_function and not is_global and self.in_try_except: 1811 self.make_volatile(n.name) 1812 1813 # Set the replacement target. Note that this will not apply to all 1814 # objects, with only floats supporting replaceable values. 1815 1816 if expr: 1817 # Prevent parameters from becoming result targets. Otherwise, they 1818 # may inadvertently cause the modification of the supplied object. 1819 1820 parameters = self.importer.function_parameters.get(path) 1821 1822 if not parameters or n.name not in parameters: 1823 target_ref = TrResolvedNameRef(n.name, ref, is_global=is_global, 1824 location=location) 1825 self.result_target_name = str(target_ref) 1826 else: 1827 self.result_target_name = None 1828 1829 # Expression processing is deferred until after any result target has 1830 # been set. 1831 1832 if process_expr: 1833 expr = self.process_structure_node(expr) 1834 1835 # Qualified names are used for resolved static references or for 1836 # static namespace members. The reference should be configured to return 1837 # such names. 1838 1839 name_ref = TrResolvedNameRef(n.name, ref, expr=expr, is_global=is_global, 1840 location=location) 1841 1842 return not expr and self.get_aliases(name_ref) or name_ref 1843 1844 def get_aliases(self, name_ref): 1845 1846 "Return alias references for the given 'name_ref'." 1847 1848 location = name_ref.access_location() 1849 accessor_locations = self.deducer.access_index.get(location) 1850 1851 if not accessor_locations: 1852 return None 1853 1854 refs = set() 1855 1856 for accessor_location in accessor_locations: 1857 alias_refs = self.deducer.referenced_objects.get(accessor_location) 1858 if alias_refs: 1859 refs.update(alias_refs) 1860 1861 if refs: 1862 return AliasResult(name_ref, refs, location) 1863 else: 1864 return None 1865 1866 def make_volatile(self, name): 1867 1868 "Record 'name' as volatile in the current namespace." 1869 1870 self.volatile_locals.add(name) 1871 1872 def process_not_node(self, n): 1873 1874 "Process the given operator node 'n'." 1875 1876 return self.make_negation(self.process_structure_node(n.expr)) 1877 1878 def process_raise_node(self, n): 1879 1880 "Process the given raise node 'n'." 1881 1882 # NOTE: Determine which raise statement variants should be permitted. 1883 1884 if n.expr1: 1885 1886 # Names with accompanying arguments are treated like invocations. 1887 1888 if n.expr2: 1889 call = compiler.ast.CallFunc(n.expr1, [n.expr2]) 1890 exc = self.process_structure_node(call) 1891 self.writestmt("__Raise(%s);" % exc) 1892 1893 # Raise instances, testing the kind at run-time if necessary and 1894 # instantiating any non-instance. 1895 1896 else: 1897 exc = self.process_structure_node(n.expr1) 1898 1899 if isinstance(exc, TrInstanceRef) or exc.is_well_defined_instance(): 1900 self.writestmt("__Raise(%s);" % exc) 1901 else: 1902 self.writestmt("__Raise(__ensure_instance(%s));" % exc) 1903 else: 1904 self.writestmt("__Throw(__tmp_exc);") 1905 1906 def process_return_node(self, n): 1907 1908 "Process the given return node 'n'." 1909 1910 if self.in_function: 1911 self.result_target_name = "__result" 1912 1913 expr = self.process_structure_node(n.value) or PredefinedConstantRef("None") 1914 1915 if self.in_try_finally or self.in_try_except: 1916 self.writestmt("__Return(%s);" % expr) 1917 else: 1918 self.writestmt("return %s;" % expr) 1919 1920 return ReturnRef() 1921 1922 def process_try_node(self, n): 1923 1924 """ 1925 Process the given "try...except" node 'n'. 1926 """ 1927 1928 in_try_except = self.in_try_except 1929 self.in_try_except = True 1930 1931 # Use macros to implement exception handling. 1932 1933 self.writestmt("__Try") 1934 self.writeline("{") 1935 self.indent += 1 1936 self.process_statement_node(n.body) 1937 1938 # Put the else statement in another try block that handles any raised 1939 # exceptions and converts them to exceptions that will not be handled by 1940 # the main handling block. 1941 1942 if n.else_: 1943 self.writestmt("__Try") 1944 self.writeline("{") 1945 self.indent += 1 1946 self.process_statement_node(n.else_) 1947 self.indent -= 1 1948 self.writeline("}") 1949 self.writeline("__Catch (__tmp_exc)") 1950 self.writeline("{") 1951 self.indent += 1 1952 self.writeline("if (__tmp_exc.raising) __RaiseElse(__tmp_exc.arg);") 1953 self.writeline("else if (__tmp_exc.completing) __Throw(__tmp_exc);") 1954 self.indent -= 1 1955 self.writeline("}") 1956 1957 # Complete the try block and enter the finally block, if appropriate. 1958 1959 if self.in_try_finally: 1960 self.writestmt("__Complete;") 1961 1962 self.indent -= 1 1963 self.writeline("}") 1964 1965 self.in_try_except = in_try_except 1966 1967 # Handlers are tests within a common handler block. 1968 1969 self.writeline("__Catch (__tmp_exc)") 1970 self.writeline("{") 1971 self.indent += 1 1972 1973 # Introduce an if statement to handle the completion of a try block. 1974 1975 self.process_try_completion() 1976 1977 # Handle exceptions in else blocks converted to __RaiseElse, converting 1978 # them back to normal exceptions. 1979 1980 if n.else_: 1981 self.writeline("else if (__tmp_exc.raising_else) __Raise(__tmp_exc.arg);") 1982 1983 # Exception handling. 1984 1985 for name, var, handler in n.handlers: 1986 1987 # Test for specific exceptions. 1988 1989 if name is not None: 1990 name_ref = self.process_statement_node(name) 1991 self.writeline("else if (__ISINSTANCE(__tmp_exc.arg, %s))" % name_ref) 1992 else: 1993 self.writeline("else if (1)") 1994 1995 self.writeline("{") 1996 self.indent += 1 1997 1998 # Establish the local for the handler. 1999 2000 if var is not None: 2001 self.writestmt("%s;" % self.process_name_node(var, make_expression("__tmp_exc.arg"))) 2002 2003 if handler is not None: 2004 self.process_statement_node(handler) 2005 2006 self.indent -= 1 2007 self.writeline("}") 2008 2009 # Re-raise unhandled exceptions. 2010 2011 self.writeline("else __Throw(__tmp_exc);") 2012 2013 # End the handler block. 2014 2015 self.indent -= 1 2016 self.writeline("}") 2017 print >>self.out 2018 2019 def process_try_finally_node(self, n): 2020 2021 """ 2022 Process the given "try...finally" node 'n'. 2023 """ 2024 2025 in_try_finally = self.in_try_finally 2026 self.in_try_finally = True 2027 2028 # Use macros to implement exception handling. 2029 2030 self.writestmt("__Try") 2031 self.writeline("{") 2032 self.indent += 1 2033 self.process_statement_node(n.body) 2034 self.indent -= 1 2035 self.writeline("}") 2036 2037 self.in_try_finally = in_try_finally 2038 2039 # Finally clauses handle special exceptions. 2040 2041 self.writeline("__Catch (__tmp_exc)") 2042 self.writeline("{") 2043 self.indent += 1 2044 self.process_statement_node(n.final) 2045 2046 # Introduce an if statement to handle the completion of a try block. 2047 2048 self.process_try_completion() 2049 self.writeline("else __Throw(__tmp_exc);") 2050 2051 self.indent -= 1 2052 self.writeline("}") 2053 print >>self.out 2054 2055 def process_try_completion(self): 2056 2057 "Generate a test for the completion of a try block." 2058 2059 self.writestmt("if (__tmp_exc.completing)") 2060 self.writeline("{") 2061 self.indent += 1 2062 2063 # Do not return anything at the module level. 2064 2065 if self.get_namespace_path() != self.name: 2066 2067 # Only use the normal return statement if no surrounding try blocks 2068 # apply. 2069 2070 if not self.in_try_finally and not self.in_try_except: 2071 self.writeline("if (!__ISNULL(__tmp_exc.arg)) return __tmp_exc.arg;") 2072 else: 2073 self.writeline("if (!__ISNULL(__tmp_exc.arg)) __Throw(__tmp_exc);") 2074 2075 self.indent -= 1 2076 self.writeline("}") 2077 2078 def process_while_node(self, n): 2079 2080 "Process the given while node 'n'." 2081 2082 self.writeline("while (1)") 2083 self.writeline("{") 2084 self.indent += 1 2085 test = self.process_statement_node(n.test) 2086 2087 # Emit the loop termination condition unless "while <true value>" is 2088 # indicated. 2089 2090 if not (isinstance(test, PredefinedConstantRef) and test.value): 2091 2092 # Emit a negated test of the continuation condition. 2093 2094 self.start_if(True, self.make_negation(test)) 2095 if n.else_: 2096 self.process_statement_node(n.else_) 2097 self.writestmt("break;") 2098 self.end_if() 2099 2100 in_conditional = self.in_conditional 2101 self.in_conditional = True 2102 self.process_statement_node(n.body) 2103 self.in_conditional = in_conditional 2104 2105 self.indent -= 1 2106 self.writeline("}") 2107 print >>self.out 2108 2109 # Special variable usage. 2110 2111 def get_temp_path(self): 2112 2113 """ 2114 Return the appropriate namespace path for temporary names in the current 2115 namespace. 2116 """ 2117 2118 if self.in_function: 2119 return self.get_namespace_path() 2120 else: 2121 return self.name 2122 2123 def record_temp(self, name): 2124 2125 """ 2126 Record the use of the temporary 'name' in the current namespace. At the 2127 class or module level, the temporary name is associated with the module, 2128 since the variable will then be allocated in the module's own main 2129 program. 2130 """ 2131 2132 path = self.get_temp_path() 2133 2134 init_item(self.temp_usage, path, list) 2135 self.temp_usage[path].append(name) 2136 2137 def remove_temps(self, names): 2138 2139 """ 2140 Remove 'names' from temporary storage allocations, each instance 2141 removing each request for storage. 2142 """ 2143 2144 path = self.get_temp_path() 2145 2146 for name in names: 2147 if self.uses_temp(path, name): 2148 self.temp_usage[path].remove(name) 2149 2150 def uses_temp(self, path, name): 2151 2152 """ 2153 Return whether the given namespace 'path' employs a temporary variable 2154 with the given 'name'. Note that 'path' should only be a module or a 2155 function or method, not a class. 2156 """ 2157 2158 return self.temp_usage.has_key(path) and name in self.temp_usage[path] 2159 2160 def make_negation(self, expr): 2161 2162 "Return a negated form of 'expr'." 2163 2164 result = NegationResult(expr) 2165 2166 # Negation discards the temporary results of its operand. 2167 2168 temps = expr.discards_temporary() 2169 if temps: 2170 self.remove_temps(temps) 2171 2172 return result 2173 2174 # Output generation. 2175 2176 def start_output(self): 2177 2178 "Write the declarations at the top of each source file." 2179 2180 print >>self.out, """\ 2181 #include "types.h" 2182 #include "exceptions.h" 2183 #include "ops.h" 2184 #include "progconsts.h" 2185 #include "progops.h" 2186 #include "progtypes.h" 2187 #include "main.h" 2188 """ 2189 2190 def start_unit(self): 2191 2192 "Record output within a generated function for later use." 2193 2194 self.out = StringIO() 2195 2196 def end_unit(self): 2197 2198 "Restore the output stream." 2199 2200 out = self.out 2201 self.out = self.out_toplevel 2202 return out 2203 2204 def flush_unit(self, name, out): 2205 2206 "Add declarations and generated code." 2207 2208 self.write_temporaries(name) 2209 print >>self.out 2210 out.seek(0) 2211 self.out.write(out.read()) 2212 2213 def start_module(self): 2214 2215 "Write the start of each module's main function." 2216 2217 print >>self.out, "void __main_%s()" % encode_path(self.name) 2218 print >>self.out, "{" 2219 self.indent += 1 2220 2221 # Define temporary variables, excluded from the module structure itself. 2222 2223 tempnames = [] 2224 2225 for n in self.importer.all_module_attrs[self.name]: 2226 if n.startswith("$t"): 2227 tempnames.append(encode_path(n)) 2228 2229 if tempnames: 2230 tempnames.sort() 2231 self.writeline("__attr %s;" % ", ".join(tempnames)) 2232 2233 self.start_unit() 2234 2235 def end_module(self): 2236 2237 "End each module by closing its main function." 2238 2239 out = self.end_unit() 2240 self.flush_unit(self.name, out) 2241 2242 self.indent -= 1 2243 print >>self.out, "}" 2244 2245 def start_function(self, name): 2246 2247 "Start the function having the given 'name'." 2248 2249 self.indent += 1 2250 2251 self.start_unit() 2252 2253 def end_function(self, name): 2254 2255 "End the function having the given 'name'." 2256 2257 out = self.end_unit() 2258 2259 # Write the signature at the top indentation level. 2260 2261 self.indent -= 1 2262 self.write_parameters(name) 2263 print >>self.out, "{" 2264 2265 # Obtain local names from parameters. 2266 2267 parameters = self.importer.function_parameters[name] 2268 locals = self.importer.function_locals[name].keys() 2269 names = [] 2270 volatile_names = [] 2271 2272 for n in locals: 2273 2274 # Filter out special names and parameters. Note that self is a local 2275 # regardless of whether it originally appeared in the parameters or 2276 # not. 2277 2278 if n.startswith("$l") or n in parameters or n == "self": 2279 continue 2280 if n in self.volatile_locals: 2281 volatile_names.append("%s = __NULL" % encode_path(n)) 2282 else: 2283 names.append("%s = __NULL" % encode_path(n)) 2284 2285 # Emit required local names at the function indentation level. 2286 2287 self.indent += 1 2288 2289 if names: 2290 names.sort() 2291 self.writeline("__attr %s;" % ", ".join(names)) 2292 2293 if volatile_names: 2294 volatile_names.sort() 2295 self.writeline("volatile __attr %s;" % ", ".join(volatile_names)) 2296 2297 self.flush_unit(name, out) 2298 2299 self.indent -= 1 2300 print >>self.out, "}" 2301 print >>self.out 2302 2303 def write_parameters(self, name): 2304 2305 """ 2306 For the function having the given 'name', write definitions of 2307 parameters found in the arguments array. 2308 """ 2309 2310 # Generate any self reference. 2311 2312 l = [] 2313 l.append("__attr __result") 2314 2315 if self.is_method(name): 2316 l.append("__attr self") 2317 else: 2318 l.append("__attr __self") 2319 2320 # Generate aliases for the parameters. 2321 2322 for parameter in self.importer.function_parameters[name]: 2323 l.append("%s__attr %s" % ( 2324 parameter in self.volatile_locals and "volatile " or "", 2325 encode_path(parameter))) 2326 2327 self.writeline("__attr %s(%s)" % ( 2328 encode_function_pointer(name), ", ".join(l))) 2329 2330 def write_temporaries(self, name): 2331 2332 "Write temporary storage employed by 'name'." 2333 2334 # Provide space for the recorded number of temporary variables. 2335 2336 if self.uses_temp(name, "__tmp_targets") and self.max_function_target: 2337 self.writeline("__attr __tmp_targets[%d];" % self.max_function_target) 2338 2339 if self.uses_temp(name, "__tmp_contexts") and self.max_context_index: 2340 self.writeline("__attr __tmp_contexts[%d];" % self.max_context_index) 2341 2342 if self.uses_temp(name, "__tmp_values") and self.max_accessor_index: 2343 self.writeline("__attr __tmp_values[%d];" % self.max_accessor_index) 2344 2345 if self.uses_temp(name, "__tmp_attr_refs") and self.max_attribute_ref_index: 2346 self.writeline("__attr *__tmp_attr_refs[%d];" % self.max_attribute_ref_index) 2347 2348 if self.uses_temp(name, "__tmp_results") and self.max_result_target: 2349 self.writeline("__attr __tmp_results[%d] = {0};" % self.max_result_target) 2350 2351 if self.uses_temp(name, "__tmp_private_context"): 2352 self.writeline("__attr __tmp_private_context;") 2353 if self.uses_temp(name, "__tmp_target_value"): 2354 self.writeline("__attr __tmp_target_value;") 2355 if self.uses_temp(name, "__tmp_result"): 2356 self.writeline("__attr __tmp_result;") 2357 2358 module = self.importer.get_module(self.name) 2359 2360 if name in module.exception_namespaces: 2361 self.writeline("__exc __tmp_exc;") 2362 2363 def start_if(self, first, test_ref): 2364 statement = "%sif" % (not first and "else " or "") 2365 2366 # Consume logical results directly. 2367 2368 if isinstance(test_ref, LogicalResult): 2369 self.writeline("%s %s" % (statement, test_ref.apply_test())) 2370 temps = test_ref.discards_temporary() 2371 if temps: 2372 self.remove_temps(temps) 2373 else: 2374 self.writeline("%s (%s)" % (statement, test_ref.apply_test())) 2375 2376 self.writeline("{") 2377 self.indent += 1 2378 2379 def end_if(self): 2380 self.indent -= 1 2381 self.writeline("}") 2382 2383 def start_else(self): 2384 self.writeline("else") 2385 self.writeline("{") 2386 self.indent += 1 2387 2388 def end_else(self): 2389 self.indent -= 1 2390 self.writeline("}") 2391 2392 def statement(self, expr): 2393 s = str(expr) 2394 if s: 2395 self.writestmt("%s;" % s) 2396 2397 def statements(self, results): 2398 for result in results: 2399 self.statement(result) 2400 2401 def writeline(self, s): 2402 print >>self.out, "%s%s" % (self.pad(), self.indenttext(s, self.indent + 1)) 2403 2404 def writestmt(self, s): 2405 self.writeline(s) 2406 2407 def write_comment(self, s): 2408 self.writestmt("/* %s */" % s) 2409 2410 def pad(self, extra=0): 2411 return (self.indent + extra) * self.tabstop 2412 2413 def indenttext(self, s, levels): 2414 lines = s.split("\n") 2415 out = [lines[0]] 2416 for line in lines[1:]: 2417 out.append(levels * self.tabstop + line) 2418 if line.endswith("("): 2419 levels += 1 2420 elif line.startswith(")"): 2421 levels -= 1 2422 return "\n".join(out) 2423 2424 # vim: tabstop=4 expandtab shiftwidth=4