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 1286 # Methods accessed via unidentified accessors are obtained. 1287 1288 elif function: 1289 self.record_temp("__tmp_targets") 1290 stages.append("__load_via_object(__tmp_targets[%d].value, %s).fn" % ( 1291 self.function_target, encode_symbol("pos", "__fn__"))) 1292 1293 # With a known target, the function is obtained directly and called. 1294 # By putting the invocation at the end of the final element in the 1295 # instruction sequence (the stages), the result becomes the result of 1296 # the sequence. Moreover, the parameters become part of the sequence 1297 # and thereby participate in a guaranteed evaluation order. 1298 1299 if target or function: 1300 output = "(\n%s(%s))" % (",\n".join(stages), argstr) 1301 1302 # With unknown targets, the generic invocation function is applied to 1303 # the callable and argument collections. 1304 1305 else: 1306 self.record_temp("__tmp_targets") 1307 output = "(%s, __invoke(\n__tmp_targets[%d],\n%d, %d, %s, %s,\n%d, %s\n))" % ( 1308 ",\n".join(stages), 1309 self.function_target, 1310 self.always_callable and 1 or 0, 1311 len(kwargs), kwcodestr, kwargstr, 1312 len(args), argstr) 1313 1314 if instantiation: 1315 return TrInstanceRef(instantiation, output) 1316 else: 1317 return make_expression(output) 1318 1319 def always_callable(self, refs): 1320 1321 "Determine whether all 'refs' are callable." 1322 1323 for ref in refs: 1324 if not ref.static(): 1325 return False 1326 else: 1327 origin = ref.final() 1328 if not self.importer.get_attribute(origin, "__fn__"): 1329 return False 1330 return True 1331 1332 def need_default_arguments(self, objpath, nargs): 1333 1334 """ 1335 Return whether any default arguments are needed when invoking the object 1336 given by 'objpath'. 1337 """ 1338 1339 parameters = self.importer.function_parameters.get(objpath) 1340 return nargs < len(parameters) 1341 1342 def process_lambda_node(self, n): 1343 1344 "Process the given lambda node 'n'." 1345 1346 name = self.get_lambda_name() 1347 function_name = self.get_object_path(name) 1348 1349 defaults = self.process_function_defaults(n, name, function_name, "__tmp_value") 1350 1351 # Without defaults, produce an attribute referring to the function. 1352 1353 if not defaults: 1354 return make_expression("((__attr) {.context=0, .value=&%s})" % encode_path(function_name)) 1355 1356 # With defaults, copy the function structure and set the defaults on the 1357 # copy. 1358 1359 else: 1360 self.record_temp("__tmp_value") 1361 return make_expression("(__tmp_value = __COPY(&%s, sizeof(%s)), %s, (__attr) {.context=0, .value=__tmp_value})" % ( 1362 encode_path(function_name), 1363 encode_symbol("obj", function_name), 1364 ", ".join(defaults))) 1365 1366 def process_logical_node(self, n): 1367 1368 """ 1369 Process the given operator node 'n'. 1370 1371 Convert ... to ... 1372 1373 <a> and <b> 1374 (__tmp_result = <a>, !__BOOL(__tmp_result)) ? __tmp_result : <b> 1375 1376 <a> or <b> 1377 (__tmp_result = <a>, __BOOL(__tmp_result)) ? __tmp_result : <b> 1378 """ 1379 1380 self.record_temp("__tmp_result") 1381 1382 if isinstance(n, compiler.ast.And): 1383 op = "!" 1384 else: 1385 op = "" 1386 1387 results = [] 1388 1389 for node in n.nodes[:-1]: 1390 expr = self.process_structure_node(node) 1391 results.append("(__tmp_result = %s, %s__BOOL(__tmp_result)) ? __tmp_result : " % (expr, op)) 1392 1393 expr = self.process_structure_node(n.nodes[-1]) 1394 results.append(str(expr)) 1395 1396 return make_expression("(%s)" % "".join(results)) 1397 1398 def process_name_node(self, n, expr=None): 1399 1400 "Process the given name node 'n' with the optional assignment 'expr'." 1401 1402 # Determine whether the name refers to a static external entity. 1403 1404 if n.name in predefined_constants: 1405 return PredefinedConstantRef(n.name, expr) 1406 1407 # Convert literal references, operator function names, and print 1408 # function names to references. 1409 1410 elif n.name.startswith("$L") or n.name.startswith("$op") or \ 1411 n.name.startswith("$print"): 1412 1413 ref, paths = self.importer.get_module(self.name).special[n.name] 1414 return TrResolvedNameRef(n.name, ref) 1415 1416 # Get the appropriate name for the name reference, using the same method 1417 # as in the inspector. 1418 1419 path = self.get_namespace_path() 1420 objpath = self.get_object_path(n.name) 1421 1422 # Determine any assigned globals. 1423 1424 globals = self.importer.get_module(self.name).scope_globals.get(path) 1425 if globals and n.name in globals: 1426 objpath = self.get_global_path(n.name) 1427 1428 # Get the static identity of the name. 1429 1430 ref = self.importer.identify(objpath) 1431 if ref and not ref.get_name(): 1432 ref = ref.alias(objpath) 1433 1434 # Obtain any resolved names for non-assignment names. 1435 1436 if not expr and not ref and self.in_function: 1437 locals = self.importer.function_locals.get(path) 1438 ref = locals and locals.get(n.name) 1439 1440 # Determine whether the name refers to a parameter. The generation of 1441 # parameter references is different from other names. 1442 1443 parameters = self.importer.function_parameters.get(path) 1444 parameter = n.name == "self" and self.in_method() or \ 1445 parameters and n.name in parameters 1446 1447 # Qualified names are used for resolved static references or for 1448 # static namespace members. The reference should be configured to return 1449 # such names. 1450 1451 return TrResolvedNameRef(n.name, ref, expr=expr, parameter=parameter) 1452 1453 def process_not_node(self, n): 1454 1455 "Process the given operator node 'n'." 1456 1457 return make_expression("(__BOOL(%s) ? %s : %s)" % 1458 (self.process_structure_node(n.expr), PredefinedConstantRef("False"), 1459 PredefinedConstantRef("True"))) 1460 1461 def process_raise_node(self, n): 1462 1463 "Process the given raise node 'n'." 1464 1465 # NOTE: Determine which raise statement variants should be permitted. 1466 1467 if n.expr1: 1468 1469 # Names with accompanying arguments are treated like invocations. 1470 1471 if n.expr2: 1472 call = compiler.ast.CallFunc(n.expr1, [n.expr2]) 1473 exc = self.process_structure_node(call) 1474 self.writestmt("__Raise(%s);" % exc) 1475 1476 # Raise instances, testing the kind at run-time if necessary and 1477 # instantiating any non-instance. 1478 1479 else: 1480 exc = self.process_structure_node(n.expr1) 1481 1482 if isinstance(exc, TrInstanceRef): 1483 self.writestmt("__Raise(%s);" % exc) 1484 else: 1485 self.writestmt("__Raise(__ensure_instance(%s));" % exc) 1486 else: 1487 self.writestmt("__Throw(__tmp_exc);") 1488 1489 def process_return_node(self, n): 1490 1491 "Process the given return node 'n'." 1492 1493 expr = self.process_structure_node(n.value) or PredefinedConstantRef("None") 1494 if self.in_try_finally or self.in_try_except: 1495 self.writestmt("__Return(%s);" % expr) 1496 else: 1497 self.writestmt("return %s;" % expr) 1498 1499 return ReturnRef() 1500 1501 def process_try_node(self, n): 1502 1503 """ 1504 Process the given "try...except" node 'n'. 1505 """ 1506 1507 in_try_except = self.in_try_except 1508 self.in_try_except = True 1509 1510 # Use macros to implement exception handling. 1511 1512 self.writestmt("__Try") 1513 self.writeline("{") 1514 self.indent += 1 1515 self.process_structure_node(n.body) 1516 1517 # Put the else statement in another try block that handles any raised 1518 # exceptions and converts them to exceptions that will not be handled by 1519 # the main handling block. 1520 1521 if n.else_: 1522 self.writestmt("__Try") 1523 self.writeline("{") 1524 self.indent += 1 1525 self.process_structure_node(n.else_) 1526 self.indent -= 1 1527 self.writeline("}") 1528 self.writeline("__Catch (__tmp_exc)") 1529 self.writeline("{") 1530 self.indent += 1 1531 self.writeline("if (__tmp_exc.raising) __RaiseElse(__tmp_exc.arg);") 1532 self.writeline("else if (__tmp_exc.completing) __Throw(__tmp_exc);") 1533 self.indent -= 1 1534 self.writeline("}") 1535 1536 # Complete the try block and enter the finally block, if appropriate. 1537 1538 if self.in_try_finally: 1539 self.writestmt("__Complete;") 1540 1541 self.indent -= 1 1542 self.writeline("}") 1543 1544 self.in_try_except = in_try_except 1545 1546 # Handlers are tests within a common handler block. 1547 1548 self.writeline("__Catch (__tmp_exc)") 1549 self.writeline("{") 1550 self.indent += 1 1551 1552 # Introduce an if statement to handle the completion of a try block. 1553 1554 self.process_try_completion() 1555 1556 # Handle exceptions in else blocks converted to __RaiseElse, converting 1557 # them back to normal exceptions. 1558 1559 if n.else_: 1560 self.writeline("else if (__tmp_exc.raising_else) __Raise(__tmp_exc.arg);") 1561 1562 # Exception handling. 1563 1564 for name, var, handler in n.handlers: 1565 1566 # Test for specific exceptions. 1567 1568 if name is not None: 1569 name_ref = self.process_structure_node(name) 1570 self.writeline("else if (__ISINSTANCE(__tmp_exc.arg, %s))" % name_ref) 1571 else: 1572 self.writeline("else if (1)") 1573 1574 self.writeline("{") 1575 self.indent += 1 1576 1577 # Establish the local for the handler. 1578 1579 if var is not None: 1580 self.writestmt("%s;" % self.process_name_node(var, make_expression("__tmp_exc.arg"))) 1581 1582 if handler is not None: 1583 self.process_structure_node(handler) 1584 1585 self.indent -= 1 1586 self.writeline("}") 1587 1588 # Re-raise unhandled exceptions. 1589 1590 self.writeline("else __Throw(__tmp_exc);") 1591 1592 # End the handler block. 1593 1594 self.indent -= 1 1595 self.writeline("}") 1596 1597 def process_try_finally_node(self, n): 1598 1599 """ 1600 Process the given "try...finally" node 'n'. 1601 """ 1602 1603 in_try_finally = self.in_try_finally 1604 self.in_try_finally = True 1605 1606 # Use macros to implement exception handling. 1607 1608 self.writestmt("__Try") 1609 self.writeline("{") 1610 self.indent += 1 1611 self.process_structure_node(n.body) 1612 self.indent -= 1 1613 self.writeline("}") 1614 1615 self.in_try_finally = in_try_finally 1616 1617 # Finally clauses handle special exceptions. 1618 1619 self.writeline("__Catch (__tmp_exc)") 1620 self.writeline("{") 1621 self.indent += 1 1622 self.process_structure_node(n.final) 1623 1624 # Introduce an if statement to handle the completion of a try block. 1625 1626 self.process_try_completion() 1627 self.writeline("else __Throw(__tmp_exc);") 1628 1629 self.indent -= 1 1630 self.writeline("}") 1631 1632 def process_try_completion(self): 1633 1634 "Generate a test for the completion of a try block." 1635 1636 self.writestmt("if (__tmp_exc.completing)") 1637 self.writeline("{") 1638 self.indent += 1 1639 1640 # Do not return anything at the module level. 1641 1642 if self.get_namespace_path() != self.name: 1643 1644 # Only use the normal return statement if no surrounding try blocks 1645 # apply. 1646 1647 if not self.in_try_finally and not self.in_try_except: 1648 self.writeline("if (!__ISNULL(__tmp_exc.arg)) return __tmp_exc.arg;") 1649 else: 1650 self.writeline("if (!__ISNULL(__tmp_exc.arg)) __Throw(__tmp_exc);") 1651 1652 self.indent -= 1 1653 self.writeline("}") 1654 1655 def process_while_node(self, n): 1656 1657 "Process the given while node 'n'." 1658 1659 self.writeline("while (1)") 1660 self.writeline("{") 1661 self.indent += 1 1662 test = self.process_structure_node(n.test) 1663 1664 # Emit the loop termination condition unless "while <true value>" is 1665 # indicated. 1666 1667 if not (isinstance(test, PredefinedConstantRef) and test.value): 1668 1669 # NOTE: This needs to evaluate whether the operand is true or false 1670 # NOTE: according to Python rules. 1671 1672 self.writeline("if (!__BOOL(%s))" % test) 1673 self.writeline("{") 1674 self.indent += 1 1675 if n.else_: 1676 self.process_structure_node(n.else_) 1677 self.writestmt("break;") 1678 self.indent -= 1 1679 self.writeline("}") 1680 1681 in_conditional = self.in_conditional 1682 self.in_conditional = True 1683 self.process_structure_node(n.body) 1684 self.in_conditional = in_conditional 1685 1686 self.indent -= 1 1687 self.writeline("}") 1688 1689 # Special variable usage. 1690 1691 def record_temp(self, name): 1692 1693 """ 1694 Record the use of the temporary 'name' in the current namespace. At the 1695 class or module level, the temporary name is associated with the module, 1696 since the variable will then be allocated in the module's own main 1697 program. 1698 """ 1699 1700 if self.in_function: 1701 path = self.get_namespace_path() 1702 else: 1703 path = self.name 1704 1705 init_item(self.temp_usage, path, set) 1706 self.temp_usage[path].add(name) 1707 1708 def uses_temp(self, path, name): 1709 1710 """ 1711 Return whether the given namespace 'path' employs a temporary variable 1712 with the given 'name'. Note that 'path' should only be a module or a 1713 function or method, not a class. 1714 """ 1715 1716 return self.temp_usage.has_key(path) and name in self.temp_usage[path] 1717 1718 # Output generation. 1719 1720 def start_output(self): 1721 1722 "Write the declarations at the top of each source file." 1723 1724 print >>self.out, """\ 1725 #include "types.h" 1726 #include "exceptions.h" 1727 #include "ops.h" 1728 #include "progconsts.h" 1729 #include "progops.h" 1730 #include "progtypes.h" 1731 #include "main.h" 1732 """ 1733 1734 def start_unit(self): 1735 1736 "Record output within a generated function for later use." 1737 1738 self.out = StringIO() 1739 1740 def end_unit(self, name): 1741 1742 "Add declarations and generated code." 1743 1744 # Restore the output stream. 1745 1746 out = self.out 1747 self.out = self.out_toplevel 1748 1749 self.write_temporaries(name) 1750 out.seek(0) 1751 self.out.write(out.read()) 1752 1753 self.indent -= 1 1754 print >>self.out, "}" 1755 1756 def start_module(self): 1757 1758 "Write the start of each module's main function." 1759 1760 print >>self.out, "void __main_%s()" % encode_path(self.name) 1761 print >>self.out, "{" 1762 self.indent += 1 1763 self.start_unit() 1764 1765 def end_module(self): 1766 1767 "End each module by closing its main function." 1768 1769 self.end_unit(self.name) 1770 1771 def start_function(self, name): 1772 1773 "Start the function having the given 'name'." 1774 1775 print >>self.out, "__attr %s(__attr __args[])" % encode_function_pointer(name) 1776 print >>self.out, "{" 1777 self.indent += 1 1778 1779 # Obtain local names from parameters. 1780 1781 parameters = self.importer.function_parameters[name] 1782 locals = self.importer.function_locals[name].keys() 1783 names = [] 1784 1785 for n in locals: 1786 1787 # Filter out special names and parameters. Note that self is a local 1788 # regardless of whether it originally appeared in the parameters or 1789 # not. 1790 1791 if n.startswith("$l") or n in parameters or n == "self": 1792 continue 1793 names.append(encode_path(n)) 1794 1795 # Emit required local names. 1796 1797 if names: 1798 names.sort() 1799 self.writeline("__attr %s;" % ", ".join(names)) 1800 1801 self.write_parameters(name) 1802 self.start_unit() 1803 1804 def end_function(self, name): 1805 1806 "End the function having the given 'name'." 1807 1808 self.end_unit(name) 1809 print >>self.out 1810 1811 def write_temporaries(self, name): 1812 1813 "Write temporary storage employed by 'name'." 1814 1815 # Provide space for the given number of targets. 1816 1817 if self.uses_temp(name, "__tmp_targets"): 1818 targets = self.importer.function_targets.get(name) 1819 self.writeline("__attr __tmp_targets[%d];" % targets) 1820 1821 # Add temporary variable usage details. 1822 1823 if self.uses_temp(name, "__tmp_context"): 1824 self.writeline("__ref __tmp_context;") 1825 if self.uses_temp(name, "__tmp_value"): 1826 self.writeline("__ref __tmp_value;") 1827 if self.uses_temp(name, "__tmp_target_value"): 1828 self.writeline("__ref __tmp_target_value;") 1829 if self.uses_temp(name, "__tmp_result"): 1830 self.writeline("__attr __tmp_result;") 1831 1832 module = self.importer.get_module(self.name) 1833 1834 if name in module.exception_namespaces: 1835 self.writeline("__exc __tmp_exc;") 1836 1837 def write_parameters(self, name): 1838 1839 """ 1840 For the function having the given 'name', write definitions of 1841 parameters found in the arguments array. 1842 """ 1843 1844 parameters = self.importer.function_parameters[name] 1845 1846 # Generate any self reference. 1847 1848 if self.is_method(name): 1849 self.writeline("__attr * const self = &__args[0];") 1850 1851 # Generate aliases for the parameters. 1852 1853 for i, parameter in enumerate(parameters): 1854 self.writeline("__attr * const %s = &__args[%d];" % (encode_path(parameter), i+1)) 1855 1856 def start_if(self, first, test_ref): 1857 self.writestmt("%sif (__BOOL(%s))" % (not first and "else " or "", test_ref)) 1858 self.writeline("{") 1859 self.indent += 1 1860 1861 def end_if(self): 1862 self.indent -= 1 1863 self.writeline("}") 1864 1865 def start_else(self): 1866 self.writeline("else") 1867 self.writeline("{") 1868 self.indent += 1 1869 1870 def end_else(self): 1871 self.indent -= 1 1872 self.writeline("}") 1873 1874 def statement(self, expr): 1875 # NOTE: Should never be None. 1876 if not expr: 1877 self.writestmt("...;") 1878 s = str(expr) 1879 if s: 1880 self.writestmt("%s;" % s) 1881 1882 def statements(self, results): 1883 for result in results: 1884 self.statement(result) 1885 1886 def writeline(self, s): 1887 print >>self.out, "%s%s" % (self.pad(), self.indenttext(s, self.indent + 1)) 1888 1889 def writestmt(self, s): 1890 print >>self.out 1891 self.writeline(s) 1892 1893 def write_comment(self, s): 1894 self.writestmt("/* %s */" % s) 1895 1896 def pad(self, extra=0): 1897 return (self.indent + extra) * self.tabstop 1898 1899 def indenttext(self, s, levels): 1900 lines = s.split("\n") 1901 out = [lines[0]] 1902 for line in lines[1:]: 1903 out.append(levels * self.tabstop + line) 1904 if line.endswith("("): 1905 levels += 1 1906 elif line.startswith(")"): 1907 levels -= 1 1908 return "\n".join(out) 1909 1910 # vim: tabstop=4 expandtab shiftwidth=4