Change the graph structure to contain the code generator object for

embedded code objects (e.g. functions) rather than the generated code
object.  This change means that the compiler generates code for
everything at the end, rather then generating code for each function
as it finds it.  Implementation note: _convert_LOAD_CONST in
pyassem.py must be change to call getCode().

Other changes follow.  Several changes creates extra edges between
basic blocks to reflect control flow for loops and exceptions.  These
missing edges had gone unnoticed because they do not affect the
current compilation process.

pyassem.py:
    Add _enable_debug() and _disable_debug() methods that print
    instructions and blocks to stdout as they are generated.

    Add edges between blocks for instructions like SETUP_LOOP,
    FOR_LOOP, etc.

    Add pruneNext to get rid of bogus edges remaining after
    unconditional transfer ops (e.g. JUMP_FORWARD)

    Change repr of Block to omit block length.

pycodegen.py:
    Make sure a new block is started after FOR_LOOP, etc.

    Change assert implementation to use RAISE_VARARGS 1 when there is
    no user-specified failure output.

misc.py:
    Implement __contains__ and copy for Set.
This commit is contained in:
Jeremy Hylton 2000-11-06 03:43:11 +00:00
parent a59ac0a7df
commit 314e3fb215
6 changed files with 266 additions and 42 deletions

View file

@ -160,7 +160,7 @@ class CodeGenerator:
self.set_lineno(node)
for default in node.defaults:
self.visit(default)
self.emit('LOAD_CONST', gen.getCode())
self.emit('LOAD_CONST', gen)
self.emit('MAKE_FUNCTION', len(node.defaults))
def visitClass(self, node):
@ -195,7 +195,7 @@ class CodeGenerator:
self.emit('POP_TOP')
self.visit(suite)
self.emit('JUMP_FORWARD', end)
self.nextBlock(nextTest)
self.startBlock(nextTest)
self.emit('POP_TOP')
if node.else_:
self.visit(node.else_)
@ -243,10 +243,11 @@ class CodeGenerator:
self.nextBlock(start)
self.set_lineno(node)
self.emit('FOR_LOOP', anchor)
self.nextBlock()
self.visit(node.assign)
self.visit(node.body)
self.emit('JUMP_ABSOLUTE', start)
self.nextBlock(anchor)
self.startBlock(anchor)
self.emit('POP_BLOCK')
if node.else_:
self.visit(node.else_)
@ -304,7 +305,7 @@ class CodeGenerator:
if len(node.ops) > 1:
end = self.newBlock()
self.emit('JUMP_FORWARD', end)
self.nextBlock(cleanup)
self.startBlock(cleanup)
self.emit('ROT_TWO')
self.emit('POP_TOP')
self.nextBlock(end)
@ -344,11 +345,11 @@ class CodeGenerator:
if cont:
skip_one = self.newBlock()
self.emit('JUMP_FORWARD', skip_one)
self.nextBlock(cont)
self.startBlock(cont)
self.emit('POP_TOP')
self.nextBlock(skip_one)
self.emit('JUMP_ABSOLUTE', start)
self.nextBlock(anchor)
self.startBlock(anchor)
self.delName(append)
self.__list_count = self.__list_count - 1
@ -363,6 +364,7 @@ class CodeGenerator:
self.emit('SET_LINENO', node.lineno)
self.nextBlock(start)
self.emit('FOR_LOOP', anchor)
self.nextBlock()
self.visit(node.assign)
return start, anchor
@ -390,9 +392,13 @@ class CodeGenerator:
self.visit(node.test)
self.emit('JUMP_IF_TRUE', end)
self.nextBlock()
self.emit('POP_TOP')
self.emit('LOAD_GLOBAL', 'AssertionError')
self.visit(node.fail)
self.emit('RAISE_VARARGS', 2)
if node.fail:
self.visit(node.fail)
self.emit('RAISE_VARARGS', 2)
else:
self.emit('RAISE_VARARGS', 1)
self.nextBlock(end)
self.emit('POP_TOP')
@ -419,10 +425,11 @@ class CodeGenerator:
lElse = end
self.set_lineno(node)
self.emit('SETUP_EXCEPT', handlers)
self.nextBlock()
self.visit(node.body)
self.emit('POP_BLOCK')
self.emit('JUMP_FORWARD', lElse)
self.nextBlock(handlers)
self.startBlock(handlers)
last = len(node.handlers) - 1
for i in range(len(node.handlers)):
@ -446,6 +453,8 @@ class CodeGenerator:
self.emit('JUMP_FORWARD', end)
if expr:
self.nextBlock(next)
else:
self.nextBlock()
self.emit('POP_TOP')
self.emit('END_FINALLY')
if node.else_:
@ -457,6 +466,7 @@ class CodeGenerator:
final = self.newBlock()
self.set_lineno(node)
self.emit('SETUP_FINALLY', final)
self.nextBlock()
self.visit(node.body)
self.emit('POP_BLOCK')
self.emit('LOAD_CONST', None)