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