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