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