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