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 # Process any guards defined for the parameters. 808 809 for name in self.importer.function_parameters.get(function_name): 810 self.generate_guard(name) 811 812 # Produce the body and any additional return statement. 813 814 expr = self.process_structure_node(n.code) or \ 815 self.in_method() and \ 816 function_name.rsplit(".", 1)[-1] == "__init__" and \ 817 TrResolvedNameRef("self", self.importer.function_locals[function_name]["self"]) or \ 818 PredefinedConstantRef("None") 819 820 if not isinstance(expr, ReturnRef): 821 self.writestmt("return %s;" % expr) 822 823 self.in_conditional = in_conditional 824 825 self.end_function(function_name) 826 827 def generate_guard(self, name): 828 829 """ 830 Get the accessor details for 'name', found in the current namespace, and 831 generate any guards defined for it. 832 """ 833 834 # Obtain the location, keeping track of assignment versions. 835 836 location = self.get_accessor_location(name) 837 test = self.deducer.accessor_guard_tests.get(location) 838 839 # Generate any guard from the deduced information. 840 841 if test: 842 guard, guard_type = test 843 844 if guard == "specific": 845 ref = first(self.deducer.accessor_all_types[location]) 846 argstr = "&%s" % encode_path(ref.get_origin()) 847 elif guard == "common": 848 ref = first(self.deducer.accessor_all_general_types[location]) 849 argstr = encode_path(encode_type_attribute(ref.get_origin())) 850 else: 851 return 852 853 # Produce an appropriate access to an attribute's value. 854 855 name_to_value = "%s.value" % name 856 857 # Write a test that raises a TypeError upon failure. 858 859 self.writestmt("if (!__test_%s_%s(%s, %s)) __raise_type_error();" % ( 860 guard, guard_type, name_to_value, argstr)) 861 862 def process_function_node(self, n): 863 864 """ 865 Process the given function, lambda, if expression or list comprehension 866 node 'n', generating any initialisation statements. 867 """ 868 869 # Where a function is declared conditionally, use a separate name for 870 # the definition, and assign the definition to the stated name. 871 872 original_name = n.name 873 874 if self.in_conditional or self.in_function: 875 name = self.get_lambda_name() 876 else: 877 name = n.name 878 879 objpath = self.get_object_path(name) 880 881 # Obtain details of the defaults. 882 883 defaults = self.process_function_defaults(n, name, objpath) 884 if defaults: 885 for default in defaults: 886 self.writeline("%s;" % default) 887 888 # Where a function is set conditionally or where the name may refer to 889 # different values, assign the name. 890 891 ref = self.importer.identify(objpath) 892 893 if self.in_conditional or self.in_function: 894 self.process_assignment_for_object(original_name, compiler.ast.Name(name)) 895 elif not ref.static(): 896 context = self.is_method(objpath) 897 898 self.process_assignment_for_object(original_name, 899 make_expression("__ATTRVALUE(&%s)" % encode_path(objpath))) 900 901 def process_function_defaults(self, n, name, objpath, instance_name=None): 902 903 """ 904 Process the given function or lambda node 'n', initialising defaults 905 that are dynamically set. The given 'name' indicates the name of the 906 function. The given 'objpath' indicates the origin of the function. 907 The given 'instance_name' indicates the name of any separate instance 908 of the function created to hold the defaults. 909 910 Return a list of operations setting defaults on a function instance. 911 """ 912 913 function_name = self.get_object_path(name) 914 function_defaults = self.importer.function_defaults.get(function_name) 915 if not function_defaults: 916 return None 917 918 # Determine whether any unidentified defaults are involved. 919 920 for argname, default in function_defaults: 921 if not default.static(): 922 break 923 else: 924 return None 925 926 # Handle bound methods. 927 928 if not instance_name: 929 instance_name = "&%s" % encode_path(objpath) 930 931 # Where defaults are involved but cannot be identified, obtain a new 932 # instance of the lambda and populate the defaults. 933 934 defaults = [] 935 936 # Join the original defaults with the inspected defaults. 937 938 original_defaults = [(argname, default) for (argname, default) in compiler.ast.get_defaults(n) if default] 939 940 for i, (original, inspected) in enumerate(map(None, original_defaults, function_defaults)): 941 942 # Obtain any reference for the default. 943 944 if original: 945 argname, default = original 946 name_ref = self.process_structure_node(default) 947 elif inspected: 948 argname, default = inspected 949 name_ref = TrResolvedNameRef(argname, default) 950 else: 951 continue 952 953 # Generate default initialisers except when constants are employed. 954 # Constants should be used when populating the function structures. 955 956 if name_ref and not isinstance(name_ref, TrConstantValueRef): 957 defaults.append("__SETDEFAULT(%s, %s, %s)" % (instance_name, i, name_ref)) 958 959 return defaults 960 961 def process_if_node(self, n): 962 963 """ 964 Process the given "if" node 'n'. 965 """ 966 967 first = True 968 for test, body in n.tests: 969 test_ref = self.process_structure_node(test) 970 self.start_if(first, test_ref) 971 972 in_conditional = self.in_conditional 973 self.in_conditional = True 974 self.process_structure_node(body) 975 self.in_conditional = in_conditional 976 977 self.end_if() 978 first = False 979 980 if n.else_: 981 self.start_else() 982 self.process_structure_node(n.else_) 983 self.end_else() 984 985 print >>self.out 986 987 def process_invocation_node(self, n): 988 989 "Process the given invocation node 'n'." 990 991 # Any invocations in the expression will store target details in a 992 # different location. 993 994 self.function_target += 1 995 996 # Process the expression. 997 998 expr = self.process_structure_node(n.node) 999 1000 # Reference the current target again. 1001 1002 self.function_target -= 1 1003 1004 # Obtain details of the invocation expression. 1005 1006 objpath = expr.get_origin() 1007 location = expr.access_location() 1008 1009 # Identified target details. 1010 1011 target = None 1012 target_structure = None 1013 1014 # Specific function target information. 1015 1016 function = None 1017 1018 # Instantiation involvement. 1019 1020 instantiation = False 1021 literal_instantiation = False 1022 1023 # Invocation requirements. 1024 1025 context_required = True 1026 have_access_context = isinstance(expr, AttrResult) 1027 context_identity = have_access_context and expr.context() 1028 parameters = None 1029 1030 # Obtain details of the callable and of its parameters. 1031 1032 # Literals may be instantiated specially. 1033 1034 if expr.is_name() and expr.name.startswith("$L") and objpath: 1035 instantiation = literal_instantiation = objpath 1036 target = encode_literal_instantiator(objpath) 1037 context_required = False 1038 1039 # Identified targets employ function pointers directly. 1040 1041 elif objpath: 1042 parameters = self.importer.function_parameters.get(objpath) 1043 1044 # Class invocation involves instantiators. 1045 1046 if expr.has_kind("<class>"): 1047 instantiation = objpath 1048 target = encode_instantiator_pointer(objpath) 1049 init_ref = self.importer.all_class_attrs[objpath]["__init__"] 1050 target_structure = "&%s" % encode_path(init_ref) 1051 context_required = False 1052 1053 # Only plain functions and bound methods employ function pointers. 1054 1055 elif expr.has_kind("<function>"): 1056 function = objpath 1057 1058 # Test for functions and methods. 1059 1060 context_required = self.is_method(objpath) 1061 accessor_kinds = self.get_accessor_kinds(location) 1062 instance_accessor = accessor_kinds and \ 1063 len(accessor_kinds) == 1 and \ 1064 first(accessor_kinds) == "<instance>" 1065 1066 # Only identify certain bound methods or functions. 1067 1068 if not context_required or instance_accessor: 1069 target = encode_function_pointer(objpath) 1070 1071 # Access bound method defaults even if it is not clear whether 1072 # the accessor is appropriate. 1073 1074 target_structure = "&%s" % encode_path(objpath) 1075 1076 # Other targets are retrieved at run-time. Some information about them 1077 # may be available and be used to provide warnings about argument 1078 # compatibility. 1079 1080 elif self.importer.give_warning("args"): 1081 unsuitable = self.get_referenced_attribute_invocations(location) 1082 1083 if unsuitable: 1084 for ref in unsuitable: 1085 _objpath = ref.get_origin() 1086 num_parameters = len(self.importer.function_parameters[_objpath]) 1087 print >>sys.stderr, \ 1088 "In %s, at line %d, inappropriate number of " \ 1089 "arguments given. Need %d arguments to call %s." % ( 1090 self.get_namespace_path(), n.lineno, num_parameters, 1091 _objpath) 1092 1093 # Determine any readily-accessible target identity. 1094 1095 target_identity = target or expr.is_name() and str(expr) or None 1096 target_var = target_identity or "__tmp_targets[%d]" % self.function_target 1097 1098 if not target_identity: 1099 self.record_temp("__tmp_targets") 1100 1101 if context_identity and context_identity.startswith("__tmp_contexts"): 1102 self.record_temp("__tmp_contexts") 1103 1104 # Arguments are presented in a temporary frame array with any context 1105 # always being the first argument. Where it would be unused, it may be 1106 # set to null. 1107 1108 if context_required: 1109 if have_access_context: 1110 args = ["__ATTRVALUE(%s)" % context_identity] 1111 else: 1112 args = ["__CONTEXT_AS_VALUE(%s)" % target_var] 1113 else: 1114 args = ["__NULL"] 1115 1116 # Complete the array with null values, permitting tests for a complete 1117 # set of arguments. 1118 1119 args += [None] * (parameters is None and len(n.args) or parameters is not None and len(parameters) or 0) 1120 kwcodes = [] 1121 kwargs = [] 1122 1123 # Any invocations in the arguments will store target details in a 1124 # different location. 1125 1126 self.function_target += 1 1127 1128 for i, arg in enumerate(n.args): 1129 argexpr = self.process_structure_node(arg) 1130 1131 # Store a keyword argument, either in the argument list or 1132 # in a separate keyword argument list for subsequent lookup. 1133 1134 if isinstance(arg, compiler.ast.Keyword): 1135 1136 # With knowledge of the target, store the keyword 1137 # argument directly. 1138 1139 if parameters: 1140 try: 1141 argnum = parameters.index(arg.name) 1142 except ValueError: 1143 raise TranslateError("Argument %s is not recognised." % arg.name, 1144 self.get_namespace_path(), n) 1145 args[argnum+1] = str(argexpr) 1146 1147 # Otherwise, store the details in a separate collection. 1148 1149 else: 1150 kwargs.append(str(argexpr)) 1151 kwcodes.append("{%s, %s}" % ( 1152 encode_ppos(arg.name), encode_pcode(arg.name))) 1153 1154 # Store non-keyword arguments in the argument list, rejecting 1155 # superfluous arguments. 1156 1157 else: 1158 try: 1159 args[i+1] = str(argexpr) 1160 except IndexError: 1161 raise TranslateError("Too many arguments specified.", 1162 self.get_namespace_path(), n) 1163 1164 # Reference the current target again. 1165 1166 self.function_target -= 1 1167 1168 # Defaults are added to the frame where arguments are missing. 1169 1170 if parameters: 1171 function_defaults = self.importer.function_defaults.get(objpath) 1172 if function_defaults: 1173 1174 # Visit each default and set any missing arguments. 1175 # Use the target structure to obtain defaults, as opposed to the 1176 # actual function involved. 1177 1178 for i, (argname, default) in enumerate(function_defaults): 1179 argnum = parameters.index(argname) 1180 if not args[argnum+1]: 1181 args[argnum+1] = "__GETDEFAULT(%s, %d)" % (target_structure, i) 1182 1183 # Test for missing arguments. 1184 1185 if None in args: 1186 raise TranslateError("Not all arguments supplied.", 1187 self.get_namespace_path(), n) 1188 1189 # Encode the arguments. 1190 1191 # Where literal instantiation is occurring, add an argument indicating 1192 # the number of values. The context is excluded. 1193 1194 if literal_instantiation: 1195 argstr = "__ARGS(%s), %d" % (", ".join(args[1:]), len(args) - 1) 1196 else: 1197 argstr = ", ".join(args) 1198 1199 kwargstr = kwargs and ("__ARGS(%s)" % ", ".join(kwargs)) or "0" 1200 kwcodestr = kwcodes and ("__KWARGS(%s)" % ", ".join(kwcodes)) or "0" 1201 1202 # First, the invocation expression is presented. 1203 1204 stages = [] 1205 1206 # Without a known specific callable, the expression provides the target. 1207 1208 if not target or context_required: 1209 if target: 1210 if expr: 1211 stages.append(str(expr)) 1212 elif not target_identity: 1213 stages.append("%s = %s" % (target_var, expr)) 1214 1215 # Any specific callable is then obtained. 1216 1217 if target: 1218 stages.append(target) 1219 1220 # Methods accessed via unidentified accessors are obtained. 1221 1222 elif function: 1223 if context_required: 1224 if have_access_context: 1225 stages.append("__get_function(%s, %s)" % ( 1226 context_identity, target_var)) 1227 else: 1228 stages.append("__get_function(__CONTEXT_AS_VALUE(%s).value, %s)" % ( 1229 target_var, target_var)) 1230 else: 1231 stages.append("__load_via_object(%s.value, __fn__).fn" % target_var) 1232 1233 # With a known target, the function is obtained directly and called. 1234 # By putting the invocation at the end of the final element in the 1235 # instruction sequence (the stages), the result becomes the result of 1236 # the sequence. Moreover, the parameters become part of the sequence 1237 # and thereby participate in a guaranteed evaluation order. 1238 1239 if target or function: 1240 stages[-1] += "(%s)" % argstr 1241 if instantiation: 1242 return InstantiationResult(instantiation, stages) 1243 else: 1244 return InvocationResult(stages) 1245 1246 # With unknown targets, the generic invocation function is applied to 1247 # the callable and argument collections. 1248 1249 else: 1250 stages.append("__invoke(\n%s,\n%d, %d, %s, %s,\n%d, %s\n)" % ( 1251 target_var, 1252 self.always_callable and 1 or 0, 1253 len(kwargs), kwcodestr, kwargstr, 1254 len(args), "__ARGS(%s)" % argstr)) 1255 return InvocationResult(stages) 1256 1257 def always_callable(self, refs): 1258 1259 "Determine whether all 'refs' are callable." 1260 1261 for ref in refs: 1262 if not ref.static(): 1263 return False 1264 else: 1265 origin = ref.final() 1266 if not self.importer.get_attribute(origin, "__fn__"): 1267 return False 1268 return True 1269 1270 def need_default_arguments(self, objpath, nargs): 1271 1272 """ 1273 Return whether any default arguments are needed when invoking the object 1274 given by 'objpath'. 1275 """ 1276 1277 parameters = self.importer.function_parameters.get(objpath) 1278 return nargs < len(parameters) 1279 1280 def process_lambda_node(self, n): 1281 1282 "Process the given lambda node 'n'." 1283 1284 name = self.get_lambda_name() 1285 function_name = self.get_object_path(name) 1286 1287 defaults = self.process_function_defaults(n, name, function_name, "__tmp_value") 1288 1289 # Without defaults, produce an attribute referring to the function. 1290 1291 if not defaults: 1292 return make_expression("__ATTRVALUE(&%s)" % encode_path(function_name)) 1293 1294 # With defaults, copy the function structure and set the defaults on the 1295 # copy. 1296 1297 else: 1298 self.record_temp("__tmp_value") 1299 return make_expression("(__tmp_value = __COPY(&%s, sizeof(%s)), %s, __ATTRVALUE(__tmp_value))" % ( 1300 encode_path(function_name), 1301 encode_symbol("obj", function_name), 1302 ", ".join(defaults))) 1303 1304 def process_logical_node(self, n): 1305 1306 "Process the given operator node 'n'." 1307 1308 self.record_temp("__tmp_result") 1309 1310 conjunction = isinstance(n, compiler.ast.And) 1311 results = [] 1312 1313 for node in n.nodes: 1314 results.append(self.process_structure_node(node)) 1315 1316 return LogicalOperationResult(results, conjunction) 1317 1318 def process_name_node(self, n, expr=None): 1319 1320 "Process the given name node 'n' with the optional assignment 'expr'." 1321 1322 # Determine whether the name refers to a static external entity. 1323 1324 if n.name in predefined_constants: 1325 return PredefinedConstantRef(n.name, expr) 1326 1327 # Convert literal references, operator function names, and print 1328 # function names to references. 1329 1330 elif n.name.startswith("$L") or n.name.startswith("$op") or \ 1331 n.name.startswith("$print"): 1332 1333 ref, paths = self.importer.get_module(self.name).special[n.name] 1334 return TrResolvedNameRef(n.name, ref) 1335 1336 # Temporary names are output program locals. 1337 1338 elif n.name.startswith("$t"): 1339 return TrResolvedNameRef(n.name, Reference("<var>"), expr=expr) 1340 1341 # Get the appropriate name for the name reference, using the same method 1342 # as in the inspector. 1343 1344 path = self.get_namespace_path() 1345 objpath = self.get_object_path(n.name) 1346 1347 # Determine any assigned globals. 1348 1349 globals = self.importer.get_module(self.name).scope_globals.get(path) 1350 1351 # Explicitly declared globals. 1352 1353 if globals and n.name in globals: 1354 objpath = self.get_global_path(n.name) 1355 is_global = True 1356 1357 # Implicitly referenced globals in functions. 1358 1359 elif self.in_function: 1360 is_global = n.name not in self.importer.function_locals[path] 1361 1362 # Implicitly referenced globals elsewhere. 1363 1364 else: 1365 namespace = self.importer.identify(path) 1366 is_global = not self.importer.get_attributes(namespace, n.name) 1367 1368 # Get the static identity of the name. 1369 1370 ref = self.importer.identify(objpath) 1371 if ref and not ref.get_name(): 1372 ref = ref.alias(objpath) 1373 1374 # Obtain any resolved names for non-assignment names. 1375 1376 if not expr and not ref and self.in_function: 1377 locals = self.importer.function_locals.get(path) 1378 ref = locals and locals.get(n.name) 1379 1380 # Determine whether the name refers to a parameter. The generation of 1381 # parameter references is different from other names. 1382 1383 parameters = self.importer.function_parameters.get(path) 1384 parameter = n.name == "self" and self.in_method() or \ 1385 parameters and n.name in parameters 1386 1387 # Find any invocation details. 1388 1389 location = self.get_access_location(n.name) 1390 1391 # Qualified names are used for resolved static references or for 1392 # static namespace members. The reference should be configured to return 1393 # such names. 1394 1395 return TrResolvedNameRef(n.name, ref, expr=expr, is_global=is_global, location=location) 1396 1397 def process_not_node(self, n): 1398 1399 "Process the given operator node 'n'." 1400 1401 return self.make_negation(self.process_structure_node(n.expr)) 1402 1403 def process_raise_node(self, n): 1404 1405 "Process the given raise node 'n'." 1406 1407 # NOTE: Determine which raise statement variants should be permitted. 1408 1409 if n.expr1: 1410 1411 # Names with accompanying arguments are treated like invocations. 1412 1413 if n.expr2: 1414 call = compiler.ast.CallFunc(n.expr1, [n.expr2]) 1415 exc = self.process_structure_node(call) 1416 self.writestmt("__Raise(%s);" % exc) 1417 1418 # Raise instances, testing the kind at run-time if necessary and 1419 # instantiating any non-instance. 1420 1421 else: 1422 exc = self.process_structure_node(n.expr1) 1423 1424 if isinstance(exc, TrInstanceRef): 1425 self.writestmt("__Raise(%s);" % exc) 1426 else: 1427 self.writestmt("__Raise(__ensure_instance(%s));" % exc) 1428 else: 1429 self.writestmt("__Throw(__tmp_exc);") 1430 1431 def process_return_node(self, n): 1432 1433 "Process the given return node 'n'." 1434 1435 expr = self.process_structure_node(n.value) or PredefinedConstantRef("None") 1436 if self.in_try_finally or self.in_try_except: 1437 self.writestmt("__Return(%s);" % expr) 1438 else: 1439 self.writestmt("return %s;" % expr) 1440 1441 return ReturnRef() 1442 1443 def process_try_node(self, n): 1444 1445 """ 1446 Process the given "try...except" node 'n'. 1447 """ 1448 1449 in_try_except = self.in_try_except 1450 self.in_try_except = True 1451 1452 # Use macros to implement exception handling. 1453 1454 self.writestmt("__Try") 1455 self.writeline("{") 1456 self.indent += 1 1457 self.process_structure_node(n.body) 1458 1459 # Put the else statement in another try block that handles any raised 1460 # exceptions and converts them to exceptions that will not be handled by 1461 # the main handling block. 1462 1463 if n.else_: 1464 self.writestmt("__Try") 1465 self.writeline("{") 1466 self.indent += 1 1467 self.process_structure_node(n.else_) 1468 self.indent -= 1 1469 self.writeline("}") 1470 self.writeline("__Catch (__tmp_exc)") 1471 self.writeline("{") 1472 self.indent += 1 1473 self.writeline("if (__tmp_exc.raising) __RaiseElse(__tmp_exc.arg);") 1474 self.writeline("else if (__tmp_exc.completing) __Throw(__tmp_exc);") 1475 self.indent -= 1 1476 self.writeline("}") 1477 1478 # Complete the try block and enter the finally block, if appropriate. 1479 1480 if self.in_try_finally: 1481 self.writestmt("__Complete;") 1482 1483 self.indent -= 1 1484 self.writeline("}") 1485 1486 self.in_try_except = in_try_except 1487 1488 # Handlers are tests within a common handler block. 1489 1490 self.writeline("__Catch (__tmp_exc)") 1491 self.writeline("{") 1492 self.indent += 1 1493 1494 # Introduce an if statement to handle the completion of a try block. 1495 1496 self.process_try_completion() 1497 1498 # Handle exceptions in else blocks converted to __RaiseElse, converting 1499 # them back to normal exceptions. 1500 1501 if n.else_: 1502 self.writeline("else if (__tmp_exc.raising_else) __Raise(__tmp_exc.arg);") 1503 1504 # Exception handling. 1505 1506 for name, var, handler in n.handlers: 1507 1508 # Test for specific exceptions. 1509 1510 if name is not None: 1511 name_ref = self.process_structure_node(name) 1512 self.writeline("else if (__ISINSTANCE(__tmp_exc.arg, %s))" % name_ref) 1513 else: 1514 self.writeline("else if (1)") 1515 1516 self.writeline("{") 1517 self.indent += 1 1518 1519 # Establish the local for the handler. 1520 1521 if var is not None: 1522 self.writestmt("%s;" % self.process_name_node(var, make_expression("__tmp_exc.arg"))) 1523 1524 if handler is not None: 1525 self.process_structure_node(handler) 1526 1527 self.indent -= 1 1528 self.writeline("}") 1529 1530 # Re-raise unhandled exceptions. 1531 1532 self.writeline("else __Throw(__tmp_exc);") 1533 1534 # End the handler block. 1535 1536 self.indent -= 1 1537 self.writeline("}") 1538 print >>self.out 1539 1540 def process_try_finally_node(self, n): 1541 1542 """ 1543 Process the given "try...finally" node 'n'. 1544 """ 1545 1546 in_try_finally = self.in_try_finally 1547 self.in_try_finally = True 1548 1549 # Use macros to implement exception handling. 1550 1551 self.writestmt("__Try") 1552 self.writeline("{") 1553 self.indent += 1 1554 self.process_structure_node(n.body) 1555 self.indent -= 1 1556 self.writeline("}") 1557 1558 self.in_try_finally = in_try_finally 1559 1560 # Finally clauses handle special exceptions. 1561 1562 self.writeline("__Catch (__tmp_exc)") 1563 self.writeline("{") 1564 self.indent += 1 1565 self.process_structure_node(n.final) 1566 1567 # Introduce an if statement to handle the completion of a try block. 1568 1569 self.process_try_completion() 1570 self.writeline("else __Throw(__tmp_exc);") 1571 1572 self.indent -= 1 1573 self.writeline("}") 1574 print >>self.out 1575 1576 def process_try_completion(self): 1577 1578 "Generate a test for the completion of a try block." 1579 1580 self.writestmt("if (__tmp_exc.completing)") 1581 self.writeline("{") 1582 self.indent += 1 1583 1584 # Do not return anything at the module level. 1585 1586 if self.get_namespace_path() != self.name: 1587 1588 # Only use the normal return statement if no surrounding try blocks 1589 # apply. 1590 1591 if not self.in_try_finally and not self.in_try_except: 1592 self.writeline("if (!__ISNULL(__tmp_exc.arg)) return __tmp_exc.arg;") 1593 else: 1594 self.writeline("if (!__ISNULL(__tmp_exc.arg)) __Throw(__tmp_exc);") 1595 1596 self.indent -= 1 1597 self.writeline("}") 1598 1599 def process_while_node(self, n): 1600 1601 "Process the given while node 'n'." 1602 1603 self.writeline("while (1)") 1604 self.writeline("{") 1605 self.indent += 1 1606 test = self.process_structure_node(n.test) 1607 1608 # Emit the loop termination condition unless "while <true value>" is 1609 # indicated. 1610 1611 if not (isinstance(test, PredefinedConstantRef) and test.value): 1612 1613 # Emit a negated test of the continuation condition. 1614 1615 self.start_if(True, self.make_negation(test)) 1616 if n.else_: 1617 self.process_structure_node(n.else_) 1618 self.writestmt("break;") 1619 self.end_if() 1620 1621 in_conditional = self.in_conditional 1622 self.in_conditional = True 1623 self.process_structure_node(n.body) 1624 self.in_conditional = in_conditional 1625 1626 self.indent -= 1 1627 self.writeline("}") 1628 print >>self.out 1629 1630 # Special variable usage. 1631 1632 def get_temp_path(self): 1633 1634 """ 1635 Return the appropriate namespace path for temporary names in the current 1636 namespace. 1637 """ 1638 1639 if self.in_function: 1640 return self.get_namespace_path() 1641 else: 1642 return self.name 1643 1644 def record_temp(self, name): 1645 1646 """ 1647 Record the use of the temporary 'name' in the current namespace. At the 1648 class or module level, the temporary name is associated with the module, 1649 since the variable will then be allocated in the module's own main 1650 program. 1651 """ 1652 1653 path = self.get_temp_path() 1654 1655 init_item(self.temp_usage, path, list) 1656 self.temp_usage[path].append(name) 1657 1658 def remove_temps(self, names): 1659 1660 """ 1661 Remove 'names' from temporary storage allocations, each instance 1662 removing each request for storage. 1663 """ 1664 1665 path = self.get_temp_path() 1666 1667 for name in names: 1668 if self.uses_temp(path, name): 1669 self.temp_usage[path].remove(name) 1670 1671 def uses_temp(self, path, name): 1672 1673 """ 1674 Return whether the given namespace 'path' employs a temporary variable 1675 with the given 'name'. Note that 'path' should only be a module or a 1676 function or method, not a class. 1677 """ 1678 1679 return self.temp_usage.has_key(path) and name in self.temp_usage[path] 1680 1681 def make_negation(self, expr): 1682 1683 "Return a negated form of 'expr'." 1684 1685 result = NegationResult(expr) 1686 1687 # Negation discards the temporary results of its operand. 1688 1689 temps = expr.discards_temporary() 1690 if temps: 1691 self.remove_temps(temps) 1692 1693 return result 1694 1695 # Output generation. 1696 1697 def start_output(self): 1698 1699 "Write the declarations at the top of each source file." 1700 1701 print >>self.out, """\ 1702 #include "types.h" 1703 #include "exceptions.h" 1704 #include "ops.h" 1705 #include "progconsts.h" 1706 #include "progops.h" 1707 #include "progtypes.h" 1708 #include "main.h" 1709 """ 1710 1711 def start_unit(self): 1712 1713 "Record output within a generated function for later use." 1714 1715 self.out = StringIO() 1716 1717 def end_unit(self, name): 1718 1719 "Add declarations and generated code." 1720 1721 # Restore the output stream. 1722 1723 out = self.out 1724 self.out = self.out_toplevel 1725 1726 self.write_temporaries(name) 1727 print >>self.out 1728 out.seek(0) 1729 self.out.write(out.read()) 1730 1731 self.indent -= 1 1732 print >>self.out, "}" 1733 1734 def start_module(self): 1735 1736 "Write the start of each module's main function." 1737 1738 print >>self.out, "void __main_%s()" % encode_path(self.name) 1739 print >>self.out, "{" 1740 self.indent += 1 1741 1742 # Define temporary variables, excluded from the module structure itself. 1743 1744 tempnames = [] 1745 1746 for n in self.importer.all_module_attrs[self.name]: 1747 if n.startswith("$t"): 1748 tempnames.append(encode_path(n)) 1749 1750 if tempnames: 1751 tempnames.sort() 1752 self.writeline("__attr %s;" % ", ".join(tempnames)) 1753 1754 self.start_unit() 1755 1756 def end_module(self): 1757 1758 "End each module by closing its main function." 1759 1760 self.end_unit(self.name) 1761 1762 def start_function(self, name): 1763 1764 "Start the function having the given 'name'." 1765 1766 self.write_parameters(name) 1767 1768 print >>self.out, "{" 1769 self.indent += 1 1770 1771 # Obtain local names from parameters. 1772 1773 parameters = self.importer.function_parameters[name] 1774 locals = self.importer.function_locals[name].keys() 1775 names = [] 1776 1777 for n in locals: 1778 1779 # Filter out special names and parameters. Note that self is a local 1780 # regardless of whether it originally appeared in the parameters or 1781 # not. 1782 1783 if n.startswith("$l") or n in parameters or n == "self": 1784 continue 1785 names.append(encode_path(n)) 1786 1787 # Emit required local names. 1788 1789 if names: 1790 names.sort() 1791 self.writeline("__attr %s;" % ", ".join(names)) 1792 1793 self.start_unit() 1794 1795 def end_function(self, name): 1796 1797 "End the function having the given 'name'." 1798 1799 self.end_unit(name) 1800 print >>self.out 1801 1802 def write_parameters(self, name): 1803 1804 """ 1805 For the function having the given 'name', write definitions of 1806 parameters found in the arguments array. 1807 """ 1808 1809 # Generate any self reference. 1810 1811 l = [] 1812 1813 if self.is_method(name): 1814 l.append("__attr self") 1815 else: 1816 l.append("__attr __self") 1817 1818 # Generate aliases for the parameters. 1819 1820 for parameter in self.importer.function_parameters[name]: 1821 l.append("__attr %s" % encode_path(parameter)) 1822 1823 self.writeline("__attr %s(%s)" % ( 1824 encode_function_pointer(name), ", ".join(l))) 1825 1826 def write_temporaries(self, name): 1827 1828 "Write temporary storage employed by 'name'." 1829 1830 # Provide space for the given number of targets. 1831 1832 targets = self.importer.function_targets.get(name) 1833 1834 if self.uses_temp(name, "__tmp_targets"): 1835 self.writeline("__attr __tmp_targets[%d];" % targets) 1836 if self.uses_temp(name, "__tmp_contexts"): 1837 self.writeline("__ref __tmp_contexts[%d];" % targets) 1838 1839 # Add temporary variable usage details. 1840 1841 if self.uses_temp(name, "__tmp_private_context"): 1842 self.writeline("__ref __tmp_private_context;") 1843 if self.uses_temp(name, "__tmp_value"): 1844 self.writeline("__ref __tmp_value;") 1845 if self.uses_temp(name, "__tmp_target_value"): 1846 self.writeline("__ref __tmp_target_value;") 1847 if self.uses_temp(name, "__tmp_result"): 1848 self.writeline("__attr __tmp_result;") 1849 1850 module = self.importer.get_module(self.name) 1851 1852 if name in module.exception_namespaces: 1853 self.writeline("__exc __tmp_exc;") 1854 1855 def start_if(self, first, test_ref): 1856 statement = "%sif" % (not first and "else " or "") 1857 1858 # Consume logical results directly. 1859 1860 if isinstance(test_ref, LogicalResult): 1861 self.writeline("%s %s" % (statement, test_ref.apply_test())) 1862 temps = test_ref.discards_temporary() 1863 if temps: 1864 self.remove_temps(temps) 1865 else: 1866 self.writeline("%s (__BOOL(%s))" % (statement, test_ref)) 1867 1868 self.writeline("{") 1869 self.indent += 1 1870 1871 def end_if(self): 1872 self.indent -= 1 1873 self.writeline("}") 1874 1875 def start_else(self): 1876 self.writeline("else") 1877 self.writeline("{") 1878 self.indent += 1 1879 1880 def end_else(self): 1881 self.indent -= 1 1882 self.writeline("}") 1883 1884 def statement(self, expr): 1885 s = str(expr) 1886 if s: 1887 self.writestmt("%s;" % s) 1888 1889 def statements(self, results): 1890 for result in results: 1891 self.statement(result) 1892 1893 def writeline(self, s): 1894 print >>self.out, "%s%s" % (self.pad(), self.indenttext(s, self.indent + 1)) 1895 1896 def writestmt(self, s): 1897 self.writeline(s) 1898 1899 def write_comment(self, s): 1900 self.writestmt("/* %s */" % s) 1901 1902 def pad(self, extra=0): 1903 return (self.indent + extra) * self.tabstop 1904 1905 def indenttext(self, s, levels): 1906 lines = s.split("\n") 1907 out = [lines[0]] 1908 for line in lines[1:]: 1909 out.append(levels * self.tabstop + line) 1910 if line.endswith("("): 1911 levels += 1 1912 elif line.startswith(")"): 1913 levels -= 1 1914 return "\n".join(out) 1915 1916 # vim: tabstop=4 expandtab shiftwidth=4