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