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