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