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