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