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