| OLD | NEW |
| 1 """A flow graph representation for Python bytecode""" | 1 """A flow graph representation for Python bytecode""" |
| 2 | 2 |
| 3 import dis | 3 import dis |
| 4 import types | 4 import types |
| 5 import sys | 5 import sys |
| 6 | 6 |
| 7 from compiler import misc | 7 from compiler import misc |
| 8 from compiler.consts \ | 8 from compiler.consts \ |
| 9 import CO_OPTIMIZED, CO_NEWLOCALS, CO_VARARGS, CO_VARKEYWORDS | 9 import CO_OPTIMIZED, CO_NEWLOCALS, CO_VARARGS, CO_VARKEYWORDS |
| 10 | 10 |
| (...skipping 602 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 613 self.codeOffset = self.codeOffset + len(args) | 613 self.codeOffset = self.codeOffset + len(args) |
| 614 | 614 |
| 615 def nextLine(self, lineno): | 615 def nextLine(self, lineno): |
| 616 if self.firstline == 0: | 616 if self.firstline == 0: |
| 617 self.firstline = lineno | 617 self.firstline = lineno |
| 618 self.lastline = lineno | 618 self.lastline = lineno |
| 619 else: | 619 else: |
| 620 # compute deltas | 620 # compute deltas |
| 621 addr = self.codeOffset - self.lastoff | 621 addr = self.codeOffset - self.lastoff |
| 622 line = lineno - self.lastline | 622 line = lineno - self.lastline |
| 623 abs_line = abs(line) |
| 624 if line >= 0: |
| 625 sign_line = lambda l: l |
| 626 else: |
| 627 sign_line = lambda l: 256 - l |
| 623 # Python assumes that lineno always increases with | 628 # Python assumes that lineno always increases with |
| 624 # increasing bytecode address (lnotab is unsigned char). | 629 # increasing bytecode address (lnotab is unsigned char). |
| 625 # Depending on when SET_LINENO instructions are emitted | 630 # Depending on when SET_LINENO instructions are emitted |
| 626 # this is not always true. Consider the code: | 631 # this is not always true. Consider the code: |
| 627 # a = (1, | 632 # a = (1, |
| 628 # b) | 633 # b) |
| 629 # In the bytecode stream, the assignment to "a" occurs | 634 # In the bytecode stream, the assignment to "a" occurs |
| 630 # after the loading of "b". This works with the C Python | 635 # after the loading of "b". This works with the C Python |
| 631 # compiler because it only generates a SET_LINENO instruction | 636 # compiler because it only generates a SET_LINENO instruction |
| 632 # for the assignment. | 637 # for the assignment. |
| 633 if line >= 0: | 638 push = self.lnotab.append |
| 634 push = self.lnotab.append | 639 while addr > 255: |
| 635 while addr > 255: | 640 push(255); push(0) |
| 636 push(255); push(0) | 641 addr -= 255 |
| 637 addr -= 255 | 642 while abs_line > 127: |
| 638 while line > 255: | 643 push(addr); push(sign_line(127)) |
| 639 push(addr); push(255) | 644 abs_line -= 127 |
| 640 line -= 255 | 645 addr = 0 |
| 641 addr = 0 | 646 if addr > 0 or abs_line > 0: |
| 642 if addr > 0 or line > 0: | 647 push(addr); push(sign_line(abs_line)) |
| 643 push(addr); push(line) | 648 self.lastline = lineno |
| 644 self.lastline = lineno | 649 self.lastoff = self.codeOffset |
| 645 self.lastoff = self.codeOffset | |
| 646 | 650 |
| 647 def getCode(self): | 651 def getCode(self): |
| 648 return ''.join(self.code) | 652 return ''.join(self.code) |
| 649 | 653 |
| 650 def getTable(self): | 654 def getTable(self): |
| 651 return ''.join(map(chr, self.lnotab)) | 655 return ''.join(map(chr, self.lnotab)) |
| 652 | 656 |
| 653 class StackDepthTracker: | 657 class StackDepthTracker: |
| 654 # XXX 1. need to keep track of stack depth on jumps | 658 # XXX 1. need to keep track of stack depth on jumps |
| 655 # XXX 2. at least partly as a result, this code is broken | 659 # XXX 2. at least partly as a result, this code is broken |
| (...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 750 return -argc | 754 return -argc |
| 751 def BUILD_SLICE(self, argc): | 755 def BUILD_SLICE(self, argc): |
| 752 if argc == 2: | 756 if argc == 2: |
| 753 return -1 | 757 return -1 |
| 754 elif argc == 3: | 758 elif argc == 3: |
| 755 return -2 | 759 return -2 |
| 756 def DUP_TOPX(self, argc): | 760 def DUP_TOPX(self, argc): |
| 757 return argc | 761 return argc |
| 758 | 762 |
| 759 findDepth = StackDepthTracker().findDepth | 763 findDepth = StackDepthTracker().findDepth |
| OLD | NEW |