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