Jeremy Hylton | 9c048f9 | 2000-10-13 21:58:13 +0000 | [diff] [blame] | 1 | import imp |
Jeremy Hylton | 53187f3 | 2000-02-08 19:01:29 +0000 | [diff] [blame] | 2 | import os |
Jeremy Hylton | 36cc6a2 | 2000-03-16 20:06:59 +0000 | [diff] [blame] | 3 | import marshal |
Jeremy Hylton | 53187f3 | 2000-02-08 19:01:29 +0000 | [diff] [blame] | 4 | import stat |
Jeremy Hylton | 2051608 | 2000-09-01 20:33:26 +0000 | [diff] [blame] | 5 | import string |
Jeremy Hylton | 53187f3 | 2000-02-08 19:01:29 +0000 | [diff] [blame] | 6 | import struct |
Jeremy Hylton | 9c048f9 | 2000-10-13 21:58:13 +0000 | [diff] [blame] | 7 | import sys |
Jeremy Hylton | ad9a86f | 2000-02-16 00:55:44 +0000 | [diff] [blame] | 8 | import types |
Jeremy Hylton | 36cc6a2 | 2000-03-16 20:06:59 +0000 | [diff] [blame] | 9 | from cStringIO import StringIO |
| 10 | |
| 11 | from compiler import ast, parse, walk |
| 12 | from compiler import pyassem, misc |
Thomas Wouters | 46cc7c0 | 2000-08-12 20:32:46 +0000 | [diff] [blame] | 13 | from compiler.pyassem import CO_VARARGS, CO_VARKEYWORDS, CO_NEWLOCALS, TupleArg |
Jeremy Hylton | 36cc6a2 | 2000-03-16 20:06:59 +0000 | [diff] [blame] | 14 | |
Jeremy Hylton | 9c048f9 | 2000-10-13 21:58:13 +0000 | [diff] [blame] | 15 | # Do we have Python 1.x or Python 2.x? |
| 16 | try: |
| 17 | VERSION = sys.version_info[0] |
| 18 | except AttributeError: |
| 19 | VERSION = 1 |
| 20 | |
Jeremy Hylton | be317e6 | 2000-05-02 22:32:59 +0000 | [diff] [blame] | 21 | callfunc_opcode_info = { |
| 22 | # (Have *args, Have **args) : opcode |
| 23 | (0,0) : "CALL_FUNCTION", |
| 24 | (1,0) : "CALL_FUNCTION_VAR", |
| 25 | (0,1) : "CALL_FUNCTION_KW", |
| 26 | (1,1) : "CALL_FUNCTION_VAR_KW", |
| 27 | } |
| 28 | |
Jeremy Hylton | 9c048f9 | 2000-10-13 21:58:13 +0000 | [diff] [blame] | 29 | def compile(filename, display=0): |
Jeremy Hylton | 36cc6a2 | 2000-03-16 20:06:59 +0000 | [diff] [blame] | 30 | f = open(filename) |
| 31 | buf = f.read() |
| 32 | f.close() |
| 33 | mod = Module(buf, filename) |
Jeremy Hylton | 9c048f9 | 2000-10-13 21:58:13 +0000 | [diff] [blame] | 34 | mod.compile(display) |
Jeremy Hylton | 36cc6a2 | 2000-03-16 20:06:59 +0000 | [diff] [blame] | 35 | f = open(filename + "c", "wb") |
| 36 | mod.dump(f) |
| 37 | f.close() |
| 38 | |
| 39 | class Module: |
| 40 | def __init__(self, source, filename): |
Thomas Wouters | 46cc7c0 | 2000-08-12 20:32:46 +0000 | [diff] [blame] | 41 | self.filename = filename |
| 42 | self.source = source |
| 43 | self.code = None |
Jeremy Hylton | 36cc6a2 | 2000-03-16 20:06:59 +0000 | [diff] [blame] | 44 | |
Jeremy Hylton | 9c048f9 | 2000-10-13 21:58:13 +0000 | [diff] [blame] | 45 | def compile(self, display=0): |
Thomas Wouters | 46cc7c0 | 2000-08-12 20:32:46 +0000 | [diff] [blame] | 46 | ast = parse(self.source) |
Jeremy Hylton | 36cc6a2 | 2000-03-16 20:06:59 +0000 | [diff] [blame] | 47 | root, filename = os.path.split(self.filename) |
Thomas Wouters | 46cc7c0 | 2000-08-12 20:32:46 +0000 | [diff] [blame] | 48 | gen = ModuleCodeGenerator(filename) |
| 49 | walk(ast, gen, 1) |
Jeremy Hylton | 9c048f9 | 2000-10-13 21:58:13 +0000 | [diff] [blame] | 50 | if display: |
| 51 | import pprint |
| 52 | print pprint.pprint(ast) |
Thomas Wouters | 46cc7c0 | 2000-08-12 20:32:46 +0000 | [diff] [blame] | 53 | self.code = gen.getCode() |
Jeremy Hylton | 36cc6a2 | 2000-03-16 20:06:59 +0000 | [diff] [blame] | 54 | |
| 55 | def dump(self, f): |
Thomas Wouters | 46cc7c0 | 2000-08-12 20:32:46 +0000 | [diff] [blame] | 56 | f.write(self.getPycHeader()) |
| 57 | marshal.dump(self.code, f) |
Jeremy Hylton | 36cc6a2 | 2000-03-16 20:06:59 +0000 | [diff] [blame] | 58 | |
Jeremy Hylton | 9c048f9 | 2000-10-13 21:58:13 +0000 | [diff] [blame] | 59 | MAGIC = imp.get_magic() |
Jeremy Hylton | 36cc6a2 | 2000-03-16 20:06:59 +0000 | [diff] [blame] | 60 | |
| 61 | def getPycHeader(self): |
Thomas Wouters | 46cc7c0 | 2000-08-12 20:32:46 +0000 | [diff] [blame] | 62 | # compile.c uses marshal to write a long directly, with |
| 63 | # calling the interface that would also generate a 1-byte code |
| 64 | # to indicate the type of the value. simplest way to get the |
| 65 | # same effect is to call marshal and then skip the code. |
Thomas Wouters | 46cc7c0 | 2000-08-12 20:32:46 +0000 | [diff] [blame] | 66 | mtime = os.stat(self.filename)[stat.ST_MTIME] |
| 67 | mtime = struct.pack('i', mtime) |
Jeremy Hylton | 9c048f9 | 2000-10-13 21:58:13 +0000 | [diff] [blame] | 68 | return self.MAGIC + mtime |
Jeremy Hylton | 8b6323d | 2000-02-04 00:28:21 +0000 | [diff] [blame] | 69 | |
Jeremy Hylton | 8b6323d | 2000-02-04 00:28:21 +0000 | [diff] [blame] | 70 | class CodeGenerator: |
Jeremy Hylton | 3e0910c | 2000-02-10 17:20:39 +0000 | [diff] [blame] | 71 | |
Jeremy Hylton | 36cc6a2 | 2000-03-16 20:06:59 +0000 | [diff] [blame] | 72 | optimized = 0 # is namespace access optimized? |
Jeremy Hylton | 3050d51 | 2000-02-12 00:12:38 +0000 | [diff] [blame] | 73 | |
Jeremy Hylton | 36cc6a2 | 2000-03-16 20:06:59 +0000 | [diff] [blame] | 74 | def __init__(self, filename): |
| 75 | ## Subclasses must define a constructor that intializes self.graph |
Jeremy Hylton | 9c048f9 | 2000-10-13 21:58:13 +0000 | [diff] [blame] | 76 | ## before calling this init function, e.g. |
Thomas Wouters | 46cc7c0 | 2000-08-12 20:32:46 +0000 | [diff] [blame] | 77 | ## self.graph = pyassem.PyFlowGraph() |
| 78 | self.filename = filename |
| 79 | self.locals = misc.Stack() |
| 80 | self.loops = misc.Stack() |
| 81 | self.curStack = 0 |
| 82 | self.maxStack = 0 |
Jeremy Hylton | 92f3972 | 2000-09-01 20:47:37 +0000 | [diff] [blame] | 83 | self.last_lineno = None |
Thomas Wouters | 46cc7c0 | 2000-08-12 20:32:46 +0000 | [diff] [blame] | 84 | self._setupGraphDelegation() |
Jeremy Hylton | 53187f3 | 2000-02-08 19:01:29 +0000 | [diff] [blame] | 85 | |
Jeremy Hylton | 36cc6a2 | 2000-03-16 20:06:59 +0000 | [diff] [blame] | 86 | def _setupGraphDelegation(self): |
Thomas Wouters | 46cc7c0 | 2000-08-12 20:32:46 +0000 | [diff] [blame] | 87 | self.emit = self.graph.emit |
| 88 | self.newBlock = self.graph.newBlock |
| 89 | self.startBlock = self.graph.startBlock |
| 90 | self.nextBlock = self.graph.nextBlock |
| 91 | self.setDocstring = self.graph.setDocstring |
Jeremy Hylton | a505812 | 2000-02-14 14:14:29 +0000 | [diff] [blame] | 92 | |
Jeremy Hylton | 36cc6a2 | 2000-03-16 20:06:59 +0000 | [diff] [blame] | 93 | def getCode(self): |
Thomas Wouters | 46cc7c0 | 2000-08-12 20:32:46 +0000 | [diff] [blame] | 94 | """Return a code object""" |
| 95 | return self.graph.getCode() |
Jeremy Hylton | 76d01b8 | 2000-02-11 20:27:07 +0000 | [diff] [blame] | 96 | |
Jeremy Hylton | 36cc6a2 | 2000-03-16 20:06:59 +0000 | [diff] [blame] | 97 | # Next five methods handle name access |
Jeremy Hylton | 53187f3 | 2000-02-08 19:01:29 +0000 | [diff] [blame] | 98 | |
Jeremy Hylton | 4024560 | 2000-02-08 21:15:48 +0000 | [diff] [blame] | 99 | def isLocalName(self, name): |
Jeremy Hylton | 873bdc1 | 2000-02-17 17:56:29 +0000 | [diff] [blame] | 100 | return self.locals.top().has_elt(name) |
Jeremy Hylton | 4024560 | 2000-02-08 21:15:48 +0000 | [diff] [blame] | 101 | |
Jeremy Hylton | 3e0910c | 2000-02-10 17:20:39 +0000 | [diff] [blame] | 102 | def storeName(self, name): |
| 103 | self._nameOp('STORE', name) |
| 104 | |
| 105 | def loadName(self, name): |
| 106 | self._nameOp('LOAD', name) |
| 107 | |
| 108 | def delName(self, name): |
| 109 | self._nameOp('DELETE', name) |
| 110 | |
Jeremy Hylton | 36cc6a2 | 2000-03-16 20:06:59 +0000 | [diff] [blame] | 111 | def _nameOp(self, prefix, name): |
Thomas Wouters | 46cc7c0 | 2000-08-12 20:32:46 +0000 | [diff] [blame] | 112 | if not self.optimized: |
| 113 | self.emit(prefix + '_NAME', name) |
| 114 | return |
Jeremy Hylton | 36cc6a2 | 2000-03-16 20:06:59 +0000 | [diff] [blame] | 115 | if self.isLocalName(name): |
Thomas Wouters | 46cc7c0 | 2000-08-12 20:32:46 +0000 | [diff] [blame] | 116 | self.emit(prefix + '_FAST', name) |
Jeremy Hylton | 36cc6a2 | 2000-03-16 20:06:59 +0000 | [diff] [blame] | 117 | else: |
| 118 | self.emit(prefix + '_GLOBAL', name) |
Jeremy Hylton | 5e0ce53 | 2000-02-10 00:47:08 +0000 | [diff] [blame] | 119 | |
Jeremy Hylton | 7daf04d | 2000-08-04 16:56:51 +0000 | [diff] [blame] | 120 | def set_lineno(self, node): |
Jeremy Hylton | 92f3972 | 2000-09-01 20:47:37 +0000 | [diff] [blame] | 121 | """Emit SET_LINENO if node has lineno attribute and it is |
| 122 | different than the last lineno emitted. |
Jeremy Hylton | 7daf04d | 2000-08-04 16:56:51 +0000 | [diff] [blame] | 123 | |
| 124 | Returns true if SET_LINENO was emitted. |
| 125 | |
| 126 | There are no rules for when an AST node should have a lineno |
| 127 | attribute. The transformer and AST code need to be reviewed |
| 128 | and a consistent policy implemented and documented. Until |
| 129 | then, this method works around missing line numbers. |
| 130 | """ |
| 131 | lineno = getattr(node, 'lineno', None) |
Jeremy Hylton | 92f3972 | 2000-09-01 20:47:37 +0000 | [diff] [blame] | 132 | if lineno is not None and lineno != self.last_lineno: |
Jeremy Hylton | 7daf04d | 2000-08-04 16:56:51 +0000 | [diff] [blame] | 133 | self.emit('SET_LINENO', lineno) |
Jeremy Hylton | 92f3972 | 2000-09-01 20:47:37 +0000 | [diff] [blame] | 134 | self.last_lineno = lineno |
Jeremy Hylton | 7daf04d | 2000-08-04 16:56:51 +0000 | [diff] [blame] | 135 | return 1 |
| 136 | return 0 |
| 137 | |
Jeremy Hylton | 36cc6a2 | 2000-03-16 20:06:59 +0000 | [diff] [blame] | 138 | # The first few visitor methods handle nodes that generator new |
| 139 | # code objects |
| 140 | |
| 141 | def visitModule(self, node): |
Thomas Wouters | 46cc7c0 | 2000-08-12 20:32:46 +0000 | [diff] [blame] | 142 | lnf = walk(node.node, LocalNameFinder(), 0) |
| 143 | self.locals.push(lnf.getLocals()) |
| 144 | self.setDocstring(node.doc) |
| 145 | self.visit(node.node) |
| 146 | self.emit('LOAD_CONST', None) |
| 147 | self.emit('RETURN_VALUE') |
Jeremy Hylton | 36cc6a2 | 2000-03-16 20:06:59 +0000 | [diff] [blame] | 148 | |
| 149 | def visitFunction(self, node): |
Thomas Wouters | 46cc7c0 | 2000-08-12 20:32:46 +0000 | [diff] [blame] | 150 | self._visitFuncOrLambda(node, isLambda=0) |
| 151 | self.storeName(node.name) |
Jeremy Hylton | 36cc6a2 | 2000-03-16 20:06:59 +0000 | [diff] [blame] | 152 | |
| 153 | def visitLambda(self, node): |
Thomas Wouters | 46cc7c0 | 2000-08-12 20:32:46 +0000 | [diff] [blame] | 154 | self._visitFuncOrLambda(node, isLambda=1) |
Jeremy Hylton | 36cc6a2 | 2000-03-16 20:06:59 +0000 | [diff] [blame] | 155 | |
| 156 | def _visitFuncOrLambda(self, node, isLambda): |
Thomas Wouters | 46cc7c0 | 2000-08-12 20:32:46 +0000 | [diff] [blame] | 157 | gen = FunctionCodeGenerator(node, self.filename, isLambda) |
| 158 | walk(node.code, gen) |
| 159 | gen.finish() |
Jeremy Hylton | 7daf04d | 2000-08-04 16:56:51 +0000 | [diff] [blame] | 160 | self.set_lineno(node) |
Jeremy Hylton | 36cc6a2 | 2000-03-16 20:06:59 +0000 | [diff] [blame] | 161 | for default in node.defaults: |
| 162 | self.visit(default) |
Jeremy Hylton | 314e3fb | 2000-11-06 03:43:11 +0000 | [diff] [blame] | 163 | self.emit('LOAD_CONST', gen) |
Jeremy Hylton | 36cc6a2 | 2000-03-16 20:06:59 +0000 | [diff] [blame] | 164 | self.emit('MAKE_FUNCTION', len(node.defaults)) |
| 165 | |
| 166 | def visitClass(self, node): |
| 167 | gen = ClassCodeGenerator(node, self.filename) |
| 168 | walk(node.code, gen) |
| 169 | gen.finish() |
Jeremy Hylton | 7daf04d | 2000-08-04 16:56:51 +0000 | [diff] [blame] | 170 | self.set_lineno(node) |
Jeremy Hylton | 36cc6a2 | 2000-03-16 20:06:59 +0000 | [diff] [blame] | 171 | self.emit('LOAD_CONST', node.name) |
| 172 | for base in node.bases: |
| 173 | self.visit(base) |
| 174 | self.emit('BUILD_TUPLE', len(node.bases)) |
| 175 | self.emit('LOAD_CONST', gen.getCode()) |
| 176 | self.emit('MAKE_FUNCTION', 0) |
| 177 | self.emit('CALL_FUNCTION', 0) |
| 178 | self.emit('BUILD_CLASS') |
| 179 | self.storeName(node.name) |
| 180 | |
| 181 | # The rest are standard visitor methods |
| 182 | |
| 183 | # The next few implement control-flow statements |
| 184 | |
| 185 | def visitIf(self, node): |
Thomas Wouters | 46cc7c0 | 2000-08-12 20:32:46 +0000 | [diff] [blame] | 186 | end = self.newBlock() |
| 187 | numtests = len(node.tests) |
| 188 | for i in range(numtests): |
| 189 | test, suite = node.tests[i] |
Jeremy Hylton | 7daf04d | 2000-08-04 16:56:51 +0000 | [diff] [blame] | 190 | self.set_lineno(test) |
Thomas Wouters | 46cc7c0 | 2000-08-12 20:32:46 +0000 | [diff] [blame] | 191 | self.visit(test) |
Thomas Wouters | 46cc7c0 | 2000-08-12 20:32:46 +0000 | [diff] [blame] | 192 | nextTest = self.newBlock() |
| 193 | self.emit('JUMP_IF_FALSE', nextTest) |
| 194 | self.nextBlock() |
| 195 | self.emit('POP_TOP') |
| 196 | self.visit(suite) |
| 197 | self.emit('JUMP_FORWARD', end) |
Jeremy Hylton | 314e3fb | 2000-11-06 03:43:11 +0000 | [diff] [blame] | 198 | self.startBlock(nextTest) |
Thomas Wouters | 46cc7c0 | 2000-08-12 20:32:46 +0000 | [diff] [blame] | 199 | self.emit('POP_TOP') |
| 200 | if node.else_: |
| 201 | self.visit(node.else_) |
| 202 | self.nextBlock(end) |
Jeremy Hylton | 36cc6a2 | 2000-03-16 20:06:59 +0000 | [diff] [blame] | 203 | |
| 204 | def visitWhile(self, node): |
Jeremy Hylton | 7daf04d | 2000-08-04 16:56:51 +0000 | [diff] [blame] | 205 | self.set_lineno(node) |
Jeremy Hylton | 36cc6a2 | 2000-03-16 20:06:59 +0000 | [diff] [blame] | 206 | |
Thomas Wouters | 46cc7c0 | 2000-08-12 20:32:46 +0000 | [diff] [blame] | 207 | loop = self.newBlock() |
| 208 | else_ = self.newBlock() |
Jeremy Hylton | 36cc6a2 | 2000-03-16 20:06:59 +0000 | [diff] [blame] | 209 | |
Thomas Wouters | 46cc7c0 | 2000-08-12 20:32:46 +0000 | [diff] [blame] | 210 | after = self.newBlock() |
| 211 | self.emit('SETUP_LOOP', after) |
Jeremy Hylton | 36cc6a2 | 2000-03-16 20:06:59 +0000 | [diff] [blame] | 212 | |
Thomas Wouters | 46cc7c0 | 2000-08-12 20:32:46 +0000 | [diff] [blame] | 213 | self.nextBlock(loop) |
| 214 | self.loops.push(loop) |
Jeremy Hylton | 36cc6a2 | 2000-03-16 20:06:59 +0000 | [diff] [blame] | 215 | |
Jeremy Hylton | 7daf04d | 2000-08-04 16:56:51 +0000 | [diff] [blame] | 216 | self.set_lineno(node) |
Thomas Wouters | 46cc7c0 | 2000-08-12 20:32:46 +0000 | [diff] [blame] | 217 | self.visit(node.test) |
| 218 | self.emit('JUMP_IF_FALSE', else_ or after) |
Jeremy Hylton | 36cc6a2 | 2000-03-16 20:06:59 +0000 | [diff] [blame] | 219 | |
Thomas Wouters | 46cc7c0 | 2000-08-12 20:32:46 +0000 | [diff] [blame] | 220 | self.nextBlock() |
| 221 | self.emit('POP_TOP') |
| 222 | self.visit(node.body) |
| 223 | self.emit('JUMP_ABSOLUTE', loop) |
Jeremy Hylton | 36cc6a2 | 2000-03-16 20:06:59 +0000 | [diff] [blame] | 224 | |
Thomas Wouters | 46cc7c0 | 2000-08-12 20:32:46 +0000 | [diff] [blame] | 225 | self.startBlock(else_) # or just the POPs if not else clause |
| 226 | self.emit('POP_TOP') |
| 227 | self.emit('POP_BLOCK') |
| 228 | if node.else_: |
| 229 | self.visit(node.else_) |
| 230 | self.loops.pop() |
| 231 | self.nextBlock(after) |
Jeremy Hylton | 36cc6a2 | 2000-03-16 20:06:59 +0000 | [diff] [blame] | 232 | |
| 233 | def visitFor(self, node): |
Thomas Wouters | 46cc7c0 | 2000-08-12 20:32:46 +0000 | [diff] [blame] | 234 | start = self.newBlock() |
Jeremy Hylton | 36cc6a2 | 2000-03-16 20:06:59 +0000 | [diff] [blame] | 235 | anchor = self.newBlock() |
Thomas Wouters | 46cc7c0 | 2000-08-12 20:32:46 +0000 | [diff] [blame] | 236 | after = self.newBlock() |
Jeremy Hylton | 36cc6a2 | 2000-03-16 20:06:59 +0000 | [diff] [blame] | 237 | self.loops.push(start) |
| 238 | |
Jeremy Hylton | 7daf04d | 2000-08-04 16:56:51 +0000 | [diff] [blame] | 239 | self.set_lineno(node) |
Thomas Wouters | 46cc7c0 | 2000-08-12 20:32:46 +0000 | [diff] [blame] | 240 | self.emit('SETUP_LOOP', after) |
Jeremy Hylton | 36cc6a2 | 2000-03-16 20:06:59 +0000 | [diff] [blame] | 241 | self.visit(node.list) |
| 242 | self.visit(ast.Const(0)) |
Thomas Wouters | 46cc7c0 | 2000-08-12 20:32:46 +0000 | [diff] [blame] | 243 | self.nextBlock(start) |
Jeremy Hylton | 7daf04d | 2000-08-04 16:56:51 +0000 | [diff] [blame] | 244 | self.set_lineno(node) |
Jeremy Hylton | 36cc6a2 | 2000-03-16 20:06:59 +0000 | [diff] [blame] | 245 | self.emit('FOR_LOOP', anchor) |
Jeremy Hylton | 314e3fb | 2000-11-06 03:43:11 +0000 | [diff] [blame] | 246 | self.nextBlock() |
Jeremy Hylton | 36cc6a2 | 2000-03-16 20:06:59 +0000 | [diff] [blame] | 247 | self.visit(node.assign) |
| 248 | self.visit(node.body) |
| 249 | self.emit('JUMP_ABSOLUTE', start) |
Jeremy Hylton | 314e3fb | 2000-11-06 03:43:11 +0000 | [diff] [blame] | 250 | self.startBlock(anchor) |
Jeremy Hylton | 36cc6a2 | 2000-03-16 20:06:59 +0000 | [diff] [blame] | 251 | self.emit('POP_BLOCK') |
| 252 | if node.else_: |
| 253 | self.visit(node.else_) |
Thomas Wouters | 46cc7c0 | 2000-08-12 20:32:46 +0000 | [diff] [blame] | 254 | self.loops.pop() |
| 255 | self.nextBlock(after) |
Jeremy Hylton | 36cc6a2 | 2000-03-16 20:06:59 +0000 | [diff] [blame] | 256 | |
| 257 | def visitBreak(self, node): |
Thomas Wouters | 46cc7c0 | 2000-08-12 20:32:46 +0000 | [diff] [blame] | 258 | if not self.loops: |
| 259 | raise SyntaxError, "'break' outside loop (%s, %d)" % \ |
| 260 | (self.filename, node.lineno) |
Jeremy Hylton | 7daf04d | 2000-08-04 16:56:51 +0000 | [diff] [blame] | 261 | self.set_lineno(node) |
Thomas Wouters | 46cc7c0 | 2000-08-12 20:32:46 +0000 | [diff] [blame] | 262 | self.emit('BREAK_LOOP') |
Jeremy Hylton | 36cc6a2 | 2000-03-16 20:06:59 +0000 | [diff] [blame] | 263 | |
| 264 | def visitContinue(self, node): |
| 265 | if not self.loops: |
| 266 | raise SyntaxError, "'continue' outside loop (%s, %d)" % \ |
Thomas Wouters | 46cc7c0 | 2000-08-12 20:32:46 +0000 | [diff] [blame] | 267 | (self.filename, node.lineno) |
Jeremy Hylton | 36cc6a2 | 2000-03-16 20:06:59 +0000 | [diff] [blame] | 268 | l = self.loops.top() |
Jeremy Hylton | 7daf04d | 2000-08-04 16:56:51 +0000 | [diff] [blame] | 269 | self.set_lineno(node) |
Jeremy Hylton | 36cc6a2 | 2000-03-16 20:06:59 +0000 | [diff] [blame] | 270 | self.emit('JUMP_ABSOLUTE', l) |
Thomas Wouters | 46cc7c0 | 2000-08-12 20:32:46 +0000 | [diff] [blame] | 271 | self.nextBlock() |
Jeremy Hylton | 36cc6a2 | 2000-03-16 20:06:59 +0000 | [diff] [blame] | 272 | |
| 273 | def visitTest(self, node, jump): |
Thomas Wouters | 46cc7c0 | 2000-08-12 20:32:46 +0000 | [diff] [blame] | 274 | end = self.newBlock() |
Jeremy Hylton | 36cc6a2 | 2000-03-16 20:06:59 +0000 | [diff] [blame] | 275 | for child in node.nodes[:-1]: |
| 276 | self.visit(child) |
| 277 | self.emit(jump, end) |
Thomas Wouters | 46cc7c0 | 2000-08-12 20:32:46 +0000 | [diff] [blame] | 278 | self.nextBlock() |
Jeremy Hylton | 36cc6a2 | 2000-03-16 20:06:59 +0000 | [diff] [blame] | 279 | self.emit('POP_TOP') |
| 280 | self.visit(node.nodes[-1]) |
Thomas Wouters | 46cc7c0 | 2000-08-12 20:32:46 +0000 | [diff] [blame] | 281 | self.nextBlock(end) |
Jeremy Hylton | 36cc6a2 | 2000-03-16 20:06:59 +0000 | [diff] [blame] | 282 | |
| 283 | def visitAnd(self, node): |
Thomas Wouters | 46cc7c0 | 2000-08-12 20:32:46 +0000 | [diff] [blame] | 284 | self.visitTest(node, 'JUMP_IF_FALSE') |
Jeremy Hylton | 36cc6a2 | 2000-03-16 20:06:59 +0000 | [diff] [blame] | 285 | |
| 286 | def visitOr(self, node): |
Thomas Wouters | 46cc7c0 | 2000-08-12 20:32:46 +0000 | [diff] [blame] | 287 | self.visitTest(node, 'JUMP_IF_TRUE') |
Jeremy Hylton | 36cc6a2 | 2000-03-16 20:06:59 +0000 | [diff] [blame] | 288 | |
| 289 | def visitCompare(self, node): |
Thomas Wouters | 46cc7c0 | 2000-08-12 20:32:46 +0000 | [diff] [blame] | 290 | self.visit(node.expr) |
| 291 | cleanup = self.newBlock() |
| 292 | for op, code in node.ops[:-1]: |
| 293 | self.visit(code) |
| 294 | self.emit('DUP_TOP') |
| 295 | self.emit('ROT_THREE') |
| 296 | self.emit('COMPARE_OP', op) |
| 297 | self.emit('JUMP_IF_FALSE', cleanup) |
| 298 | self.nextBlock() |
| 299 | self.emit('POP_TOP') |
| 300 | # now do the last comparison |
| 301 | if node.ops: |
| 302 | op, code = node.ops[-1] |
| 303 | self.visit(code) |
| 304 | self.emit('COMPARE_OP', op) |
| 305 | if len(node.ops) > 1: |
| 306 | end = self.newBlock() |
| 307 | self.emit('JUMP_FORWARD', end) |
Jeremy Hylton | 314e3fb | 2000-11-06 03:43:11 +0000 | [diff] [blame] | 308 | self.startBlock(cleanup) |
Thomas Wouters | 46cc7c0 | 2000-08-12 20:32:46 +0000 | [diff] [blame] | 309 | self.emit('ROT_TWO') |
| 310 | self.emit('POP_TOP') |
| 311 | self.nextBlock(end) |
Jeremy Hylton | 36cc6a2 | 2000-03-16 20:06:59 +0000 | [diff] [blame] | 312 | |
Jeremy Hylton | 9c048f9 | 2000-10-13 21:58:13 +0000 | [diff] [blame] | 313 | # list comprehensions |
| 314 | __list_count = 0 |
| 315 | |
| 316 | def visitListComp(self, node): |
| 317 | # XXX would it be easier to transform the AST into the form it |
| 318 | # would have if the list comp were expressed as a series of |
| 319 | # for and if stmts and an explicit append? |
| 320 | self.set_lineno(node) |
| 321 | # setup list |
| 322 | append = "$append%d" % self.__list_count |
| 323 | self.__list_count = self.__list_count + 1 |
| 324 | self.emit('BUILD_LIST', 0) |
| 325 | self.emit('DUP_TOP') |
| 326 | self.emit('LOAD_ATTR', 'append') |
| 327 | self.storeName(append) |
| 328 | l = len(node.quals) |
| 329 | stack = [] |
| 330 | for i, for_ in zip(range(l), node.quals): |
| 331 | start, anchor = self.visit(for_) |
| 332 | cont = None |
| 333 | for if_ in for_.ifs: |
| 334 | if cont is None: |
| 335 | cont = self.newBlock() |
| 336 | self.visit(if_, cont) |
| 337 | stack.insert(0, (start, cont, anchor)) |
| 338 | |
| 339 | self.loadName(append) |
| 340 | self.visit(node.expr) |
| 341 | self.emit('CALL_FUNCTION', 1) |
| 342 | self.emit('POP_TOP') |
| 343 | |
| 344 | for start, cont, anchor in stack: |
| 345 | if cont: |
| 346 | skip_one = self.newBlock() |
| 347 | self.emit('JUMP_FORWARD', skip_one) |
Jeremy Hylton | 314e3fb | 2000-11-06 03:43:11 +0000 | [diff] [blame] | 348 | self.startBlock(cont) |
Jeremy Hylton | 9c048f9 | 2000-10-13 21:58:13 +0000 | [diff] [blame] | 349 | self.emit('POP_TOP') |
| 350 | self.nextBlock(skip_one) |
| 351 | self.emit('JUMP_ABSOLUTE', start) |
Jeremy Hylton | 314e3fb | 2000-11-06 03:43:11 +0000 | [diff] [blame] | 352 | self.startBlock(anchor) |
Jeremy Hylton | 9c048f9 | 2000-10-13 21:58:13 +0000 | [diff] [blame] | 353 | self.delName(append) |
| 354 | |
| 355 | self.__list_count = self.__list_count - 1 |
| 356 | |
| 357 | def visitListCompFor(self, node): |
| 358 | self.set_lineno(node) |
| 359 | start = self.newBlock() |
| 360 | anchor = self.newBlock() |
| 361 | |
| 362 | self.visit(node.list) |
| 363 | self.visit(ast.Const(0)) |
| 364 | self.emit('SET_LINENO', node.lineno) |
| 365 | self.nextBlock(start) |
| 366 | self.emit('FOR_LOOP', anchor) |
Jeremy Hylton | 314e3fb | 2000-11-06 03:43:11 +0000 | [diff] [blame] | 367 | self.nextBlock() |
Jeremy Hylton | 9c048f9 | 2000-10-13 21:58:13 +0000 | [diff] [blame] | 368 | self.visit(node.assign) |
| 369 | return start, anchor |
| 370 | |
| 371 | def visitListCompIf(self, node, branch): |
| 372 | self.set_lineno(node) |
| 373 | self.visit(node.test) |
| 374 | self.emit('JUMP_IF_FALSE', branch) |
| 375 | self.newBlock() |
| 376 | self.emit('POP_TOP') |
| 377 | |
Jeremy Hylton | 36cc6a2 | 2000-03-16 20:06:59 +0000 | [diff] [blame] | 378 | # exception related |
| 379 | |
| 380 | def visitAssert(self, node): |
Thomas Wouters | 46cc7c0 | 2000-08-12 20:32:46 +0000 | [diff] [blame] | 381 | # XXX would be interesting to implement this via a |
| 382 | # transformation of the AST before this stage |
| 383 | end = self.newBlock() |
Jeremy Hylton | 7daf04d | 2000-08-04 16:56:51 +0000 | [diff] [blame] | 384 | self.set_lineno(node) |
Jeremy Hylton | 36cc6a2 | 2000-03-16 20:06:59 +0000 | [diff] [blame] | 385 | # XXX __debug__ and AssertionError appear to be special cases |
| 386 | # -- they are always loaded as globals even if there are local |
| 387 | # names. I guess this is a sort of renaming op. |
Thomas Wouters | 46cc7c0 | 2000-08-12 20:32:46 +0000 | [diff] [blame] | 388 | self.emit('LOAD_GLOBAL', '__debug__') |
| 389 | self.emit('JUMP_IF_FALSE', end) |
| 390 | self.nextBlock() |
| 391 | self.emit('POP_TOP') |
| 392 | self.visit(node.test) |
| 393 | self.emit('JUMP_IF_TRUE', end) |
| 394 | self.nextBlock() |
Jeremy Hylton | 314e3fb | 2000-11-06 03:43:11 +0000 | [diff] [blame] | 395 | self.emit('POP_TOP') |
Thomas Wouters | 46cc7c0 | 2000-08-12 20:32:46 +0000 | [diff] [blame] | 396 | self.emit('LOAD_GLOBAL', 'AssertionError') |
Jeremy Hylton | 314e3fb | 2000-11-06 03:43:11 +0000 | [diff] [blame] | 397 | if node.fail: |
| 398 | self.visit(node.fail) |
| 399 | self.emit('RAISE_VARARGS', 2) |
| 400 | else: |
| 401 | self.emit('RAISE_VARARGS', 1) |
Thomas Wouters | 46cc7c0 | 2000-08-12 20:32:46 +0000 | [diff] [blame] | 402 | self.nextBlock(end) |
| 403 | self.emit('POP_TOP') |
Jeremy Hylton | 36cc6a2 | 2000-03-16 20:06:59 +0000 | [diff] [blame] | 404 | |
| 405 | def visitRaise(self, node): |
Jeremy Hylton | 7daf04d | 2000-08-04 16:56:51 +0000 | [diff] [blame] | 406 | self.set_lineno(node) |
Jeremy Hylton | 36cc6a2 | 2000-03-16 20:06:59 +0000 | [diff] [blame] | 407 | n = 0 |
| 408 | if node.expr1: |
| 409 | self.visit(node.expr1) |
| 410 | n = n + 1 |
| 411 | if node.expr2: |
| 412 | self.visit(node.expr2) |
| 413 | n = n + 1 |
| 414 | if node.expr3: |
| 415 | self.visit(node.expr3) |
| 416 | n = n + 1 |
| 417 | self.emit('RAISE_VARARGS', n) |
| 418 | |
| 419 | def visitTryExcept(self, node): |
| 420 | handlers = self.newBlock() |
| 421 | end = self.newBlock() |
| 422 | if node.else_: |
| 423 | lElse = self.newBlock() |
| 424 | else: |
| 425 | lElse = end |
Jeremy Hylton | 7daf04d | 2000-08-04 16:56:51 +0000 | [diff] [blame] | 426 | self.set_lineno(node) |
Jeremy Hylton | 36cc6a2 | 2000-03-16 20:06:59 +0000 | [diff] [blame] | 427 | self.emit('SETUP_EXCEPT', handlers) |
Jeremy Hylton | 314e3fb | 2000-11-06 03:43:11 +0000 | [diff] [blame] | 428 | self.nextBlock() |
Jeremy Hylton | 36cc6a2 | 2000-03-16 20:06:59 +0000 | [diff] [blame] | 429 | self.visit(node.body) |
| 430 | self.emit('POP_BLOCK') |
| 431 | self.emit('JUMP_FORWARD', lElse) |
Jeremy Hylton | 314e3fb | 2000-11-06 03:43:11 +0000 | [diff] [blame] | 432 | self.startBlock(handlers) |
Jeremy Hylton | 36cc6a2 | 2000-03-16 20:06:59 +0000 | [diff] [blame] | 433 | |
| 434 | last = len(node.handlers) - 1 |
| 435 | for i in range(len(node.handlers)): |
| 436 | expr, target, body = node.handlers[i] |
Jeremy Hylton | 7daf04d | 2000-08-04 16:56:51 +0000 | [diff] [blame] | 437 | self.set_lineno(expr) |
Jeremy Hylton | 36cc6a2 | 2000-03-16 20:06:59 +0000 | [diff] [blame] | 438 | if expr: |
| 439 | self.emit('DUP_TOP') |
| 440 | self.visit(expr) |
| 441 | self.emit('COMPARE_OP', 'exception match') |
| 442 | next = self.newBlock() |
| 443 | self.emit('JUMP_IF_FALSE', next) |
Thomas Wouters | 46cc7c0 | 2000-08-12 20:32:46 +0000 | [diff] [blame] | 444 | self.nextBlock() |
Jeremy Hylton | 36cc6a2 | 2000-03-16 20:06:59 +0000 | [diff] [blame] | 445 | self.emit('POP_TOP') |
| 446 | self.emit('POP_TOP') |
| 447 | if target: |
| 448 | self.visit(target) |
| 449 | else: |
| 450 | self.emit('POP_TOP') |
| 451 | self.emit('POP_TOP') |
| 452 | self.visit(body) |
| 453 | self.emit('JUMP_FORWARD', end) |
| 454 | if expr: |
Thomas Wouters | 46cc7c0 | 2000-08-12 20:32:46 +0000 | [diff] [blame] | 455 | self.nextBlock(next) |
Jeremy Hylton | 314e3fb | 2000-11-06 03:43:11 +0000 | [diff] [blame] | 456 | else: |
| 457 | self.nextBlock() |
Jeremy Hylton | 36cc6a2 | 2000-03-16 20:06:59 +0000 | [diff] [blame] | 458 | self.emit('POP_TOP') |
| 459 | self.emit('END_FINALLY') |
| 460 | if node.else_: |
Thomas Wouters | 46cc7c0 | 2000-08-12 20:32:46 +0000 | [diff] [blame] | 461 | self.nextBlock(lElse) |
Jeremy Hylton | 36cc6a2 | 2000-03-16 20:06:59 +0000 | [diff] [blame] | 462 | self.visit(node.else_) |
Thomas Wouters | 46cc7c0 | 2000-08-12 20:32:46 +0000 | [diff] [blame] | 463 | self.nextBlock(end) |
Jeremy Hylton | 36cc6a2 | 2000-03-16 20:06:59 +0000 | [diff] [blame] | 464 | |
| 465 | def visitTryFinally(self, node): |
| 466 | final = self.newBlock() |
Jeremy Hylton | 7daf04d | 2000-08-04 16:56:51 +0000 | [diff] [blame] | 467 | self.set_lineno(node) |
Jeremy Hylton | 36cc6a2 | 2000-03-16 20:06:59 +0000 | [diff] [blame] | 468 | self.emit('SETUP_FINALLY', final) |
Jeremy Hylton | 314e3fb | 2000-11-06 03:43:11 +0000 | [diff] [blame] | 469 | self.nextBlock() |
Jeremy Hylton | 36cc6a2 | 2000-03-16 20:06:59 +0000 | [diff] [blame] | 470 | self.visit(node.body) |
| 471 | self.emit('POP_BLOCK') |
| 472 | self.emit('LOAD_CONST', None) |
Thomas Wouters | 46cc7c0 | 2000-08-12 20:32:46 +0000 | [diff] [blame] | 473 | self.nextBlock(final) |
Jeremy Hylton | 36cc6a2 | 2000-03-16 20:06:59 +0000 | [diff] [blame] | 474 | self.visit(node.final) |
| 475 | self.emit('END_FINALLY') |
| 476 | |
| 477 | # misc |
| 478 | |
Jeremy Hylton | 8b6323d | 2000-02-04 00:28:21 +0000 | [diff] [blame] | 479 | def visitDiscard(self, node): |
Jeremy Hylton | 5e0ce53 | 2000-02-10 00:47:08 +0000 | [diff] [blame] | 480 | self.visit(node.expr) |
Jeremy Hylton | a505812 | 2000-02-14 14:14:29 +0000 | [diff] [blame] | 481 | self.emit('POP_TOP') |
Jeremy Hylton | 8b6323d | 2000-02-04 00:28:21 +0000 | [diff] [blame] | 482 | |
Jeremy Hylton | 36cc6a2 | 2000-03-16 20:06:59 +0000 | [diff] [blame] | 483 | def visitConst(self, node): |
| 484 | self.emit('LOAD_CONST', node.value) |
| 485 | |
| 486 | def visitKeyword(self, node): |
Thomas Wouters | 46cc7c0 | 2000-08-12 20:32:46 +0000 | [diff] [blame] | 487 | self.emit('LOAD_CONST', node.name) |
| 488 | self.visit(node.expr) |
Jeremy Hylton | 36cc6a2 | 2000-03-16 20:06:59 +0000 | [diff] [blame] | 489 | |
| 490 | def visitGlobal(self, node): |
| 491 | # no code to generate |
Thomas Wouters | 46cc7c0 | 2000-08-12 20:32:46 +0000 | [diff] [blame] | 492 | pass |
Jeremy Hylton | 36cc6a2 | 2000-03-16 20:06:59 +0000 | [diff] [blame] | 493 | |
| 494 | def visitName(self, node): |
Jeremy Hylton | 92f3972 | 2000-09-01 20:47:37 +0000 | [diff] [blame] | 495 | self.set_lineno(node) |
Jeremy Hylton | 36cc6a2 | 2000-03-16 20:06:59 +0000 | [diff] [blame] | 496 | self.loadName(node.name) |
| 497 | |
Jeremy Hylton | 5e0ce53 | 2000-02-10 00:47:08 +0000 | [diff] [blame] | 498 | def visitPass(self, node): |
Jeremy Hylton | 7daf04d | 2000-08-04 16:56:51 +0000 | [diff] [blame] | 499 | self.set_lineno(node) |
Jeremy Hylton | 5e0ce53 | 2000-02-10 00:47:08 +0000 | [diff] [blame] | 500 | |
Jeremy Hylton | 5e0ce53 | 2000-02-10 00:47:08 +0000 | [diff] [blame] | 501 | def visitImport(self, node): |
Jeremy Hylton | 7daf04d | 2000-08-04 16:56:51 +0000 | [diff] [blame] | 502 | self.set_lineno(node) |
Jeremy Hylton | 2051608 | 2000-09-01 20:33:26 +0000 | [diff] [blame] | 503 | for name, alias in node.names: |
Jeremy Hylton | 9c048f9 | 2000-10-13 21:58:13 +0000 | [diff] [blame] | 504 | if VERSION > 1: |
| 505 | self.emit('LOAD_CONST', None) |
Jeremy Hylton | a505812 | 2000-02-14 14:14:29 +0000 | [diff] [blame] | 506 | self.emit('IMPORT_NAME', name) |
Jeremy Hylton | 9c048f9 | 2000-10-13 21:58:13 +0000 | [diff] [blame] | 507 | mod = string.split(name, ".")[0] |
| 508 | self.storeName(alias or mod) |
Jeremy Hylton | 5e0ce53 | 2000-02-10 00:47:08 +0000 | [diff] [blame] | 509 | |
| 510 | def visitFrom(self, node): |
Jeremy Hylton | 7daf04d | 2000-08-04 16:56:51 +0000 | [diff] [blame] | 511 | self.set_lineno(node) |
Jeremy Hylton | 2051608 | 2000-09-01 20:33:26 +0000 | [diff] [blame] | 512 | fromlist = map(lambda (name, alias): name, node.names) |
Jeremy Hylton | 9c048f9 | 2000-10-13 21:58:13 +0000 | [diff] [blame] | 513 | if VERSION > 1: |
| 514 | self.emit('LOAD_CONST', tuple(fromlist)) |
Jeremy Hylton | a505812 | 2000-02-14 14:14:29 +0000 | [diff] [blame] | 515 | self.emit('IMPORT_NAME', node.modname) |
Jeremy Hylton | 2051608 | 2000-09-01 20:33:26 +0000 | [diff] [blame] | 516 | for name, alias in node.names: |
Jeremy Hylton | 9c048f9 | 2000-10-13 21:58:13 +0000 | [diff] [blame] | 517 | if VERSION > 1: |
| 518 | if name == '*': |
| 519 | self.namespace = 0 |
| 520 | self.emit('IMPORT_STAR') |
| 521 | # There can only be one name w/ from ... import * |
| 522 | assert len(node.names) == 1 |
| 523 | return |
| 524 | else: |
| 525 | self.emit('IMPORT_FROM', name) |
| 526 | self._resolveDots(name) |
| 527 | self.storeName(alias or name) |
Jeremy Hylton | 4e1be72 | 2000-10-12 20:23:23 +0000 | [diff] [blame] | 528 | else: |
| 529 | self.emit('IMPORT_FROM', name) |
Jeremy Hylton | a505812 | 2000-02-14 14:14:29 +0000 | [diff] [blame] | 530 | self.emit('POP_TOP') |
Jeremy Hylton | 5e0ce53 | 2000-02-10 00:47:08 +0000 | [diff] [blame] | 531 | |
Jeremy Hylton | 2051608 | 2000-09-01 20:33:26 +0000 | [diff] [blame] | 532 | def _resolveDots(self, name): |
| 533 | elts = string.split(name, ".") |
| 534 | if len(elts) == 1: |
| 535 | return |
| 536 | for elt in elts[1:]: |
| 537 | self.emit('LOAD_ATTR', elt) |
| 538 | |
Jeremy Hylton | 36cc6a2 | 2000-03-16 20:06:59 +0000 | [diff] [blame] | 539 | def visitGetattr(self, node): |
| 540 | self.visit(node.expr) |
| 541 | self.emit('LOAD_ATTR', node.attrname) |
| 542 | |
| 543 | # next five implement assignments |
| 544 | |
| 545 | def visitAssign(self, node): |
Jeremy Hylton | 7daf04d | 2000-08-04 16:56:51 +0000 | [diff] [blame] | 546 | self.set_lineno(node) |
Jeremy Hylton | 36cc6a2 | 2000-03-16 20:06:59 +0000 | [diff] [blame] | 547 | self.visit(node.expr) |
| 548 | dups = len(node.nodes) - 1 |
| 549 | for i in range(len(node.nodes)): |
| 550 | elt = node.nodes[i] |
| 551 | if i < dups: |
| 552 | self.emit('DUP_TOP') |
| 553 | if isinstance(elt, ast.Node): |
| 554 | self.visit(elt) |
Jeremy Hylton | 3050d51 | 2000-02-12 00:12:38 +0000 | [diff] [blame] | 555 | |
Jeremy Hylton | 36cc6a2 | 2000-03-16 20:06:59 +0000 | [diff] [blame] | 556 | def visitAssName(self, node): |
| 557 | if node.flags == 'OP_ASSIGN': |
| 558 | self.storeName(node.name) |
| 559 | elif node.flags == 'OP_DELETE': |
| 560 | self.delName(node.name) |
| 561 | else: |
| 562 | print "oops", node.flags |
Jeremy Hylton | 76d01b8 | 2000-02-11 20:27:07 +0000 | [diff] [blame] | 563 | |
Jeremy Hylton | 36cc6a2 | 2000-03-16 20:06:59 +0000 | [diff] [blame] | 564 | def visitAssAttr(self, node): |
| 565 | self.visit(node.expr) |
| 566 | if node.flags == 'OP_ASSIGN': |
| 567 | self.emit('STORE_ATTR', node.attrname) |
| 568 | elif node.flags == 'OP_DELETE': |
| 569 | self.emit('DELETE_ATTR', node.attrname) |
| 570 | else: |
| 571 | print "warning: unexpected flags:", node.flags |
| 572 | print node |
Jeremy Hylton | 8b6323d | 2000-02-04 00:28:21 +0000 | [diff] [blame] | 573 | |
Jeremy Hylton | 9c048f9 | 2000-10-13 21:58:13 +0000 | [diff] [blame] | 574 | def _visitAssSequence(self, node, op='UNPACK_SEQUENCE'): |
Jeremy Hylton | 36cc6a2 | 2000-03-16 20:06:59 +0000 | [diff] [blame] | 575 | if findOp(node) != 'OP_DELETE': |
Jeremy Hylton | 9c048f9 | 2000-10-13 21:58:13 +0000 | [diff] [blame] | 576 | self.emit(op, len(node.nodes)) |
Jeremy Hylton | 36cc6a2 | 2000-03-16 20:06:59 +0000 | [diff] [blame] | 577 | for child in node.nodes: |
| 578 | self.visit(child) |
| 579 | |
Jeremy Hylton | 9c048f9 | 2000-10-13 21:58:13 +0000 | [diff] [blame] | 580 | if VERSION > 1: |
| 581 | visitAssTuple = _visitAssSequence |
| 582 | visitAssList = _visitAssSequence |
| 583 | else: |
| 584 | def visitAssTuple(self, node): |
| 585 | self._visitAssSequence(node, 'UNPACK_TUPLE') |
| 586 | |
| 587 | def visitAssList(self, node): |
| 588 | self._visitAssSequence(node, 'UNPACK_LIST') |
| 589 | |
| 590 | # augmented assignment |
| 591 | |
| 592 | def visitAugAssign(self, node): |
| 593 | aug_node = wrap_aug(node.node) |
| 594 | self.visit(aug_node, "load") |
| 595 | self.visit(node.expr) |
| 596 | self.emit(self._augmented_opcode[node.op]) |
| 597 | self.visit(aug_node, "store") |
| 598 | |
| 599 | _augmented_opcode = { |
| 600 | '+=' : 'INPLACE_ADD', |
| 601 | '-=' : 'INPLACE_SUBTRACT', |
| 602 | '*=' : 'INPLACE_MULTIPLY', |
| 603 | '/=' : 'INPLACE_DIVIDE', |
| 604 | '%=' : 'INPLACE_MODULO', |
| 605 | '**=': 'INPLACE_POWER', |
| 606 | '>>=': 'INPLACE_RSHIFT', |
| 607 | '<<=': 'INPLACE_LSHIFT', |
| 608 | '&=' : 'INPLACE_AND', |
| 609 | '^=' : 'INPLACE_XOR', |
| 610 | '|=' : 'INPLACE_OR', |
| 611 | } |
| 612 | |
| 613 | def visitAugName(self, node, mode): |
| 614 | if mode == "load": |
| 615 | self.loadName(node.name) |
| 616 | elif mode == "store": |
| 617 | self.storeName(node.name) |
| 618 | |
| 619 | def visitAugGetattr(self, node, mode): |
| 620 | if mode == "load": |
| 621 | self.visit(node.expr) |
| 622 | self.emit('DUP_TOP') |
| 623 | self.emit('LOAD_ATTR', node.attrname) |
| 624 | elif mode == "store": |
| 625 | self.emit('ROT_TWO') |
| 626 | self.emit('STORE_ATTR', node.attrname) |
| 627 | |
| 628 | def visitAugSlice(self, node, mode): |
| 629 | if mode == "load": |
| 630 | self.visitSlice(node, 1) |
| 631 | elif mode == "store": |
| 632 | slice = 0 |
| 633 | if node.lower: |
| 634 | slice = slice | 1 |
| 635 | if node.upper: |
| 636 | slice = slice | 2 |
| 637 | if slice == 0: |
| 638 | self.emit('ROT_TWO') |
| 639 | elif slice == 3: |
| 640 | self.emit('ROT_FOUR') |
| 641 | else: |
| 642 | self.emit('ROT_THREE') |
| 643 | self.emit('STORE_SLICE+%d' % slice) |
| 644 | |
| 645 | def visitAugSubscript(self, node, mode): |
| 646 | if len(node.subs) > 1: |
| 647 | raise SyntaxError, "augmented assignment to tuple is not possible" |
| 648 | if mode == "load": |
| 649 | self.visitSubscript(node, 1) |
| 650 | elif mode == "store": |
| 651 | self.emit('ROT_THREE') |
| 652 | self.emit('STORE_SUBSCR') |
Jeremy Hylton | 36cc6a2 | 2000-03-16 20:06:59 +0000 | [diff] [blame] | 653 | |
| 654 | def visitExec(self, node): |
| 655 | self.visit(node.expr) |
| 656 | if node.locals is None: |
| 657 | self.emit('LOAD_CONST', None) |
| 658 | else: |
| 659 | self.visit(node.locals) |
| 660 | if node.globals is None: |
| 661 | self.emit('DUP_TOP') |
| 662 | else: |
| 663 | self.visit(node.globals) |
| 664 | self.emit('EXEC_STMT') |
Jeremy Hylton | 76d01b8 | 2000-02-11 20:27:07 +0000 | [diff] [blame] | 665 | |
Jeremy Hylton | 8b6323d | 2000-02-04 00:28:21 +0000 | [diff] [blame] | 666 | def visitCallFunc(self, node): |
Jeremy Hylton | 3050d51 | 2000-02-12 00:12:38 +0000 | [diff] [blame] | 667 | pos = 0 |
| 668 | kw = 0 |
Jeremy Hylton | 7daf04d | 2000-08-04 16:56:51 +0000 | [diff] [blame] | 669 | self.set_lineno(node) |
Jeremy Hylton | 873bdc1 | 2000-02-17 17:56:29 +0000 | [diff] [blame] | 670 | self.visit(node.node) |
| 671 | for arg in node.args: |
| 672 | self.visit(arg) |
Jeremy Hylton | 3050d51 | 2000-02-12 00:12:38 +0000 | [diff] [blame] | 673 | if isinstance(arg, ast.Keyword): |
| 674 | kw = kw + 1 |
| 675 | else: |
| 676 | pos = pos + 1 |
Jeremy Hylton | be317e6 | 2000-05-02 22:32:59 +0000 | [diff] [blame] | 677 | if node.star_args is not None: |
| 678 | self.visit(node.star_args) |
| 679 | if node.dstar_args is not None: |
| 680 | self.visit(node.dstar_args) |
| 681 | have_star = node.star_args is not None |
| 682 | have_dstar = node.dstar_args is not None |
| 683 | opcode = callfunc_opcode_info[have_star, have_dstar] |
| 684 | self.emit(opcode, kw << 8 | pos) |
Jeremy Hylton | 8b6323d | 2000-02-04 00:28:21 +0000 | [diff] [blame] | 685 | |
Jeremy Hylton | 36cc6a2 | 2000-03-16 20:06:59 +0000 | [diff] [blame] | 686 | def visitPrint(self, node): |
Jeremy Hylton | 7daf04d | 2000-08-04 16:56:51 +0000 | [diff] [blame] | 687 | self.set_lineno(node) |
Jeremy Hylton | 9c048f9 | 2000-10-13 21:58:13 +0000 | [diff] [blame] | 688 | if node.dest: |
| 689 | self.visit(node.dest) |
Jeremy Hylton | 36cc6a2 | 2000-03-16 20:06:59 +0000 | [diff] [blame] | 690 | for child in node.nodes: |
Jeremy Hylton | 9c048f9 | 2000-10-13 21:58:13 +0000 | [diff] [blame] | 691 | if node.dest: |
| 692 | self.emit('DUP_TOP') |
Jeremy Hylton | 36cc6a2 | 2000-03-16 20:06:59 +0000 | [diff] [blame] | 693 | self.visit(child) |
Jeremy Hylton | 9c048f9 | 2000-10-13 21:58:13 +0000 | [diff] [blame] | 694 | if node.dest: |
| 695 | self.emit('ROT_TWO') |
| 696 | self.emit('PRINT_ITEM_TO') |
| 697 | else: |
| 698 | self.emit('PRINT_ITEM') |
Jeremy Hylton | 36cc6a2 | 2000-03-16 20:06:59 +0000 | [diff] [blame] | 699 | |
| 700 | def visitPrintnl(self, node): |
| 701 | self.visitPrint(node) |
Jeremy Hylton | 9c048f9 | 2000-10-13 21:58:13 +0000 | [diff] [blame] | 702 | if node.dest: |
| 703 | self.emit('PRINT_NEWLINE_TO') |
| 704 | else: |
| 705 | self.emit('PRINT_NEWLINE') |
Jeremy Hylton | 36cc6a2 | 2000-03-16 20:06:59 +0000 | [diff] [blame] | 706 | |
| 707 | def visitReturn(self, node): |
Jeremy Hylton | 7daf04d | 2000-08-04 16:56:51 +0000 | [diff] [blame] | 708 | self.set_lineno(node) |
Jeremy Hylton | 36cc6a2 | 2000-03-16 20:06:59 +0000 | [diff] [blame] | 709 | self.visit(node.value) |
| 710 | self.emit('RETURN_VALUE') |
Jeremy Hylton | 4024560 | 2000-02-08 21:15:48 +0000 | [diff] [blame] | 711 | |
Jeremy Hylton | 36cc6a2 | 2000-03-16 20:06:59 +0000 | [diff] [blame] | 712 | # slice and subscript stuff |
Jeremy Hylton | 5e0ce53 | 2000-02-10 00:47:08 +0000 | [diff] [blame] | 713 | |
Jeremy Hylton | 9c048f9 | 2000-10-13 21:58:13 +0000 | [diff] [blame] | 714 | def visitSlice(self, node, aug_flag=None): |
| 715 | # aug_flag is used by visitAugSlice |
Jeremy Hylton | 5e0ce53 | 2000-02-10 00:47:08 +0000 | [diff] [blame] | 716 | self.visit(node.expr) |
| 717 | slice = 0 |
| 718 | if node.lower: |
| 719 | self.visit(node.lower) |
| 720 | slice = slice | 1 |
Jeremy Hylton | 5e0ce53 | 2000-02-10 00:47:08 +0000 | [diff] [blame] | 721 | if node.upper: |
| 722 | self.visit(node.upper) |
| 723 | slice = slice | 2 |
Jeremy Hylton | 9c048f9 | 2000-10-13 21:58:13 +0000 | [diff] [blame] | 724 | if aug_flag: |
| 725 | if slice == 0: |
| 726 | self.emit('DUP_TOP') |
| 727 | elif slice == 3: |
| 728 | self.emit('DUP_TOPX', 3) |
| 729 | else: |
| 730 | self.emit('DUP_TOPX', 2) |
Jeremy Hylton | 5e0ce53 | 2000-02-10 00:47:08 +0000 | [diff] [blame] | 731 | if node.flags == 'OP_APPLY': |
Jeremy Hylton | a505812 | 2000-02-14 14:14:29 +0000 | [diff] [blame] | 732 | self.emit('SLICE+%d' % slice) |
Jeremy Hylton | 5e0ce53 | 2000-02-10 00:47:08 +0000 | [diff] [blame] | 733 | elif node.flags == 'OP_ASSIGN': |
Jeremy Hylton | a505812 | 2000-02-14 14:14:29 +0000 | [diff] [blame] | 734 | self.emit('STORE_SLICE+%d' % slice) |
Jeremy Hylton | 5e0ce53 | 2000-02-10 00:47:08 +0000 | [diff] [blame] | 735 | elif node.flags == 'OP_DELETE': |
Jeremy Hylton | a505812 | 2000-02-14 14:14:29 +0000 | [diff] [blame] | 736 | self.emit('DELETE_SLICE+%d' % slice) |
Jeremy Hylton | 5e0ce53 | 2000-02-10 00:47:08 +0000 | [diff] [blame] | 737 | else: |
Jeremy Hylton | 4f6bcd8 | 2000-02-15 23:45:26 +0000 | [diff] [blame] | 738 | print "weird slice", node.flags |
Jeremy Hylton | 5e0ce53 | 2000-02-10 00:47:08 +0000 | [diff] [blame] | 739 | raise |
Jeremy Hylton | 5e0ce53 | 2000-02-10 00:47:08 +0000 | [diff] [blame] | 740 | |
Jeremy Hylton | 9c048f9 | 2000-10-13 21:58:13 +0000 | [diff] [blame] | 741 | def visitSubscript(self, node, aug_flag=None): |
Jeremy Hylton | 4024560 | 2000-02-08 21:15:48 +0000 | [diff] [blame] | 742 | self.visit(node.expr) |
Jeremy Hylton | 36cc6a2 | 2000-03-16 20:06:59 +0000 | [diff] [blame] | 743 | for sub in node.subs: |
| 744 | self.visit(sub) |
Jeremy Hylton | 9c048f9 | 2000-10-13 21:58:13 +0000 | [diff] [blame] | 745 | if aug_flag: |
| 746 | self.emit('DUP_TOPX', 2) |
Jeremy Hylton | 36cc6a2 | 2000-03-16 20:06:59 +0000 | [diff] [blame] | 747 | if len(node.subs) > 1: |
| 748 | self.emit('BUILD_TUPLE', len(node.subs)) |
| 749 | if node.flags == 'OP_APPLY': |
| 750 | self.emit('BINARY_SUBSCR') |
| 751 | elif node.flags == 'OP_ASSIGN': |
| 752 | self.emit('STORE_SUBSCR') |
Jeremy Hylton | 3ec7e2c | 2000-02-17 22:09:35 +0000 | [diff] [blame] | 753 | elif node.flags == 'OP_DELETE': |
Jeremy Hylton | 36cc6a2 | 2000-03-16 20:06:59 +0000 | [diff] [blame] | 754 | self.emit('DELETE_SUBSCR') |
Jeremy Hylton | 5e0ce53 | 2000-02-10 00:47:08 +0000 | [diff] [blame] | 755 | |
Jeremy Hylton | 7fab23e | 2000-03-06 19:10:54 +0000 | [diff] [blame] | 756 | # binary ops |
| 757 | |
Jeremy Hylton | 8b6323d | 2000-02-04 00:28:21 +0000 | [diff] [blame] | 758 | def binaryOp(self, node, op): |
Jeremy Hylton | 873bdc1 | 2000-02-17 17:56:29 +0000 | [diff] [blame] | 759 | self.visit(node.left) |
| 760 | self.visit(node.right) |
| 761 | self.emit(op) |
Jeremy Hylton | 8b6323d | 2000-02-04 00:28:21 +0000 | [diff] [blame] | 762 | |
| 763 | def visitAdd(self, node): |
Jeremy Hylton | 873bdc1 | 2000-02-17 17:56:29 +0000 | [diff] [blame] | 764 | return self.binaryOp(node, 'BINARY_ADD') |
Jeremy Hylton | 8b6323d | 2000-02-04 00:28:21 +0000 | [diff] [blame] | 765 | |
| 766 | def visitSub(self, node): |
Jeremy Hylton | 873bdc1 | 2000-02-17 17:56:29 +0000 | [diff] [blame] | 767 | return self.binaryOp(node, 'BINARY_SUBTRACT') |
Jeremy Hylton | 8b6323d | 2000-02-04 00:28:21 +0000 | [diff] [blame] | 768 | |
| 769 | def visitMul(self, node): |
Jeremy Hylton | 873bdc1 | 2000-02-17 17:56:29 +0000 | [diff] [blame] | 770 | return self.binaryOp(node, 'BINARY_MULTIPLY') |
Jeremy Hylton | 8b6323d | 2000-02-04 00:28:21 +0000 | [diff] [blame] | 771 | |
| 772 | def visitDiv(self, node): |
Jeremy Hylton | 873bdc1 | 2000-02-17 17:56:29 +0000 | [diff] [blame] | 773 | return self.binaryOp(node, 'BINARY_DIVIDE') |
Jeremy Hylton | 8b6323d | 2000-02-04 00:28:21 +0000 | [diff] [blame] | 774 | |
Jeremy Hylton | 5e0ce53 | 2000-02-10 00:47:08 +0000 | [diff] [blame] | 775 | def visitMod(self, node): |
Jeremy Hylton | 873bdc1 | 2000-02-17 17:56:29 +0000 | [diff] [blame] | 776 | return self.binaryOp(node, 'BINARY_MODULO') |
Jeremy Hylton | 5e0ce53 | 2000-02-10 00:47:08 +0000 | [diff] [blame] | 777 | |
Jeremy Hylton | 126960b | 2000-02-14 21:33:10 +0000 | [diff] [blame] | 778 | def visitPower(self, node): |
Jeremy Hylton | 873bdc1 | 2000-02-17 17:56:29 +0000 | [diff] [blame] | 779 | return self.binaryOp(node, 'BINARY_POWER') |
Jeremy Hylton | 126960b | 2000-02-14 21:33:10 +0000 | [diff] [blame] | 780 | |
| 781 | def visitLeftShift(self, node): |
Jeremy Hylton | 873bdc1 | 2000-02-17 17:56:29 +0000 | [diff] [blame] | 782 | return self.binaryOp(node, 'BINARY_LSHIFT') |
Jeremy Hylton | 126960b | 2000-02-14 21:33:10 +0000 | [diff] [blame] | 783 | |
| 784 | def visitRightShift(self, node): |
Jeremy Hylton | 873bdc1 | 2000-02-17 17:56:29 +0000 | [diff] [blame] | 785 | return self.binaryOp(node, 'BINARY_RSHIFT') |
Jeremy Hylton | 126960b | 2000-02-14 21:33:10 +0000 | [diff] [blame] | 786 | |
Jeremy Hylton | 7fab23e | 2000-03-06 19:10:54 +0000 | [diff] [blame] | 787 | # unary ops |
| 788 | |
| 789 | def unaryOp(self, node, op): |
| 790 | self.visit(node.expr) |
| 791 | self.emit(op) |
Jeremy Hylton | 7fab23e | 2000-03-06 19:10:54 +0000 | [diff] [blame] | 792 | |
Jeremy Hylton | 126960b | 2000-02-14 21:33:10 +0000 | [diff] [blame] | 793 | def visitInvert(self, node): |
| 794 | return self.unaryOp(node, 'UNARY_INVERT') |
| 795 | |
Jeremy Hylton | 4024560 | 2000-02-08 21:15:48 +0000 | [diff] [blame] | 796 | def visitUnarySub(self, node): |
| 797 | return self.unaryOp(node, 'UNARY_NEGATIVE') |
| 798 | |
| 799 | def visitUnaryAdd(self, node): |
| 800 | return self.unaryOp(node, 'UNARY_POSITIVE') |
| 801 | |
| 802 | def visitUnaryInvert(self, node): |
| 803 | return self.unaryOp(node, 'UNARY_INVERT') |
| 804 | |
Jeremy Hylton | 5e0ce53 | 2000-02-10 00:47:08 +0000 | [diff] [blame] | 805 | def visitNot(self, node): |
| 806 | return self.unaryOp(node, 'UNARY_NOT') |
| 807 | |
Jeremy Hylton | 4024560 | 2000-02-08 21:15:48 +0000 | [diff] [blame] | 808 | def visitBackquote(self, node): |
| 809 | return self.unaryOp(node, 'UNARY_CONVERT') |
| 810 | |
Jeremy Hylton | 7fab23e | 2000-03-06 19:10:54 +0000 | [diff] [blame] | 811 | # bit ops |
| 812 | |
Jeremy Hylton | a505812 | 2000-02-14 14:14:29 +0000 | [diff] [blame] | 813 | def bitOp(self, nodes, op): |
Jeremy Hylton | 873bdc1 | 2000-02-17 17:56:29 +0000 | [diff] [blame] | 814 | self.visit(nodes[0]) |
| 815 | for node in nodes[1:]: |
| 816 | self.visit(node) |
| 817 | self.emit(op) |
Jeremy Hylton | a505812 | 2000-02-14 14:14:29 +0000 | [diff] [blame] | 818 | |
| 819 | def visitBitand(self, node): |
Jeremy Hylton | 873bdc1 | 2000-02-17 17:56:29 +0000 | [diff] [blame] | 820 | return self.bitOp(node.nodes, 'BINARY_AND') |
Jeremy Hylton | a505812 | 2000-02-14 14:14:29 +0000 | [diff] [blame] | 821 | |
| 822 | def visitBitor(self, node): |
Jeremy Hylton | 873bdc1 | 2000-02-17 17:56:29 +0000 | [diff] [blame] | 823 | return self.bitOp(node.nodes, 'BINARY_OR') |
Jeremy Hylton | a505812 | 2000-02-14 14:14:29 +0000 | [diff] [blame] | 824 | |
| 825 | def visitBitxor(self, node): |
Jeremy Hylton | 873bdc1 | 2000-02-17 17:56:29 +0000 | [diff] [blame] | 826 | return self.bitOp(node.nodes, 'BINARY_XOR') |
Jeremy Hylton | a505812 | 2000-02-14 14:14:29 +0000 | [diff] [blame] | 827 | |
Jeremy Hylton | 36cc6a2 | 2000-03-16 20:06:59 +0000 | [diff] [blame] | 828 | # object constructors |
Jeremy Hylton | 4024560 | 2000-02-08 21:15:48 +0000 | [diff] [blame] | 829 | |
Jeremy Hylton | a505812 | 2000-02-14 14:14:29 +0000 | [diff] [blame] | 830 | def visitEllipsis(self, node): |
Jeremy Hylton | 873bdc1 | 2000-02-17 17:56:29 +0000 | [diff] [blame] | 831 | self.emit('LOAD_CONST', Ellipsis) |
Jeremy Hylton | a505812 | 2000-02-14 14:14:29 +0000 | [diff] [blame] | 832 | |
Jeremy Hylton | 4024560 | 2000-02-08 21:15:48 +0000 | [diff] [blame] | 833 | def visitTuple(self, node): |
| 834 | for elt in node.nodes: |
| 835 | self.visit(elt) |
Jeremy Hylton | a505812 | 2000-02-14 14:14:29 +0000 | [diff] [blame] | 836 | self.emit('BUILD_TUPLE', len(node.nodes)) |
Jeremy Hylton | 8b6323d | 2000-02-04 00:28:21 +0000 | [diff] [blame] | 837 | |
Jeremy Hylton | 5e0ce53 | 2000-02-10 00:47:08 +0000 | [diff] [blame] | 838 | def visitList(self, node): |
| 839 | for elt in node.nodes: |
| 840 | self.visit(elt) |
Jeremy Hylton | a505812 | 2000-02-14 14:14:29 +0000 | [diff] [blame] | 841 | self.emit('BUILD_LIST', len(node.nodes)) |
Jeremy Hylton | 36cc6a2 | 2000-03-16 20:06:59 +0000 | [diff] [blame] | 842 | |
| 843 | def visitSliceobj(self, node): |
| 844 | for child in node.nodes: |
| 845 | self.visit(child) |
| 846 | self.emit('BUILD_SLICE', len(node.nodes)) |
Jeremy Hylton | 5e0ce53 | 2000-02-10 00:47:08 +0000 | [diff] [blame] | 847 | |
Jeremy Hylton | a505812 | 2000-02-14 14:14:29 +0000 | [diff] [blame] | 848 | def visitDict(self, node): |
Jeremy Hylton | 7daf04d | 2000-08-04 16:56:51 +0000 | [diff] [blame] | 849 | lineno = getattr(node, 'lineno', None) |
| 850 | if lineno: |
| 851 | set.emit('SET_LINENO', lineno) |
Jeremy Hylton | 873bdc1 | 2000-02-17 17:56:29 +0000 | [diff] [blame] | 852 | self.emit('BUILD_MAP', 0) |
| 853 | for k, v in node.items: |
Thomas Wouters | 46cc7c0 | 2000-08-12 20:32:46 +0000 | [diff] [blame] | 854 | lineno2 = getattr(node, 'lineno', None) |
Jeremy Hylton | 7daf04d | 2000-08-04 16:56:51 +0000 | [diff] [blame] | 855 | if lineno2 is not None and lineno != lineno2: |
Thomas Wouters | 46cc7c0 | 2000-08-12 20:32:46 +0000 | [diff] [blame] | 856 | self.emit('SET_LINENO', lineno2) |
| 857 | lineno = lineno2 |
Jeremy Hylton | 873bdc1 | 2000-02-17 17:56:29 +0000 | [diff] [blame] | 858 | self.emit('DUP_TOP') |
| 859 | self.visit(v) |
| 860 | self.emit('ROT_TWO') |
| 861 | self.visit(k) |
| 862 | self.emit('STORE_SUBSCR') |
Jeremy Hylton | a505812 | 2000-02-14 14:14:29 +0000 | [diff] [blame] | 863 | |
Jeremy Hylton | 36cc6a2 | 2000-03-16 20:06:59 +0000 | [diff] [blame] | 864 | class ModuleCodeGenerator(CodeGenerator): |
| 865 | super_init = CodeGenerator.__init__ |
| 866 | |
| 867 | def __init__(self, filename): |
Thomas Wouters | 46cc7c0 | 2000-08-12 20:32:46 +0000 | [diff] [blame] | 868 | # XXX <module> is ? in compile.c |
| 869 | self.graph = pyassem.PyFlowGraph("<module>", filename) |
| 870 | self.super_init(filename) |
Jeremy Hylton | 8b6323d | 2000-02-04 00:28:21 +0000 | [diff] [blame] | 871 | |
Jeremy Hylton | 36cc6a2 | 2000-03-16 20:06:59 +0000 | [diff] [blame] | 872 | class FunctionCodeGenerator(CodeGenerator): |
| 873 | super_init = CodeGenerator.__init__ |
Jeremy Hylton | 8b6323d | 2000-02-04 00:28:21 +0000 | [diff] [blame] | 874 | |
Jeremy Hylton | 36cc6a2 | 2000-03-16 20:06:59 +0000 | [diff] [blame] | 875 | optimized = 1 |
| 876 | lambdaCount = 0 |
Jeremy Hylton | 8b6323d | 2000-02-04 00:28:21 +0000 | [diff] [blame] | 877 | |
Jeremy Hylton | 36cc6a2 | 2000-03-16 20:06:59 +0000 | [diff] [blame] | 878 | def __init__(self, func, filename, isLambda=0): |
| 879 | if isLambda: |
| 880 | klass = FunctionCodeGenerator |
| 881 | name = "<lambda.%d>" % klass.lambdaCount |
| 882 | klass.lambdaCount = klass.lambdaCount + 1 |
Jeremy Hylton | 873bdc1 | 2000-02-17 17:56:29 +0000 | [diff] [blame] | 883 | else: |
Jeremy Hylton | 36cc6a2 | 2000-03-16 20:06:59 +0000 | [diff] [blame] | 884 | name = func.name |
Thomas Wouters | 46cc7c0 | 2000-08-12 20:32:46 +0000 | [diff] [blame] | 885 | args, hasTupleArg = generateArgList(func.argnames) |
| 886 | self.graph = pyassem.PyFlowGraph(name, filename, args, |
| 887 | optimized=1) |
| 888 | self.isLambda = isLambda |
| 889 | self.super_init(filename) |
Jeremy Hylton | 36cc6a2 | 2000-03-16 20:06:59 +0000 | [diff] [blame] | 890 | |
| 891 | lnf = walk(func.code, LocalNameFinder(args), 0) |
| 892 | self.locals.push(lnf.getLocals()) |
Thomas Wouters | 46cc7c0 | 2000-08-12 20:32:46 +0000 | [diff] [blame] | 893 | if func.varargs: |
| 894 | self.graph.setFlag(CO_VARARGS) |
| 895 | if func.kwargs: |
| 896 | self.graph.setFlag(CO_VARKEYWORDS) |
Jeremy Hylton | 7daf04d | 2000-08-04 16:56:51 +0000 | [diff] [blame] | 897 | self.set_lineno(func) |
Jeremy Hylton | 36cc6a2 | 2000-03-16 20:06:59 +0000 | [diff] [blame] | 898 | if hasTupleArg: |
| 899 | self.generateArgUnpack(func.argnames) |
| 900 | |
| 901 | def finish(self): |
Thomas Wouters | 46cc7c0 | 2000-08-12 20:32:46 +0000 | [diff] [blame] | 902 | self.graph.startExitBlock() |
| 903 | if not self.isLambda: |
| 904 | self.emit('LOAD_CONST', None) |
| 905 | self.emit('RETURN_VALUE') |
Jeremy Hylton | 36cc6a2 | 2000-03-16 20:06:59 +0000 | [diff] [blame] | 906 | |
| 907 | def generateArgUnpack(self, args): |
| 908 | count = 0 |
| 909 | for arg in args: |
| 910 | if type(arg) == types.TupleType: |
| 911 | self.emit('LOAD_FAST', '.nested%d' % count) |
| 912 | count = count + 1 |
Thomas Wouters | 46cc7c0 | 2000-08-12 20:32:46 +0000 | [diff] [blame] | 913 | self.unpackSequence(arg) |
Jeremy Hylton | 36cc6a2 | 2000-03-16 20:06:59 +0000 | [diff] [blame] | 914 | |
Thomas Wouters | 46cc7c0 | 2000-08-12 20:32:46 +0000 | [diff] [blame] | 915 | def unpackSequence(self, tup): |
Jeremy Hylton | 9c048f9 | 2000-10-13 21:58:13 +0000 | [diff] [blame] | 916 | if VERSION > 1: |
| 917 | self.emit('UNPACK_SEQUENCE', len(tup)) |
| 918 | else: |
| 919 | self.emit('UNPACK_TUPLE', len(tup)) |
Jeremy Hylton | 36cc6a2 | 2000-03-16 20:06:59 +0000 | [diff] [blame] | 920 | for elt in tup: |
| 921 | if type(elt) == types.TupleType: |
Thomas Wouters | 46cc7c0 | 2000-08-12 20:32:46 +0000 | [diff] [blame] | 922 | self.unpackSequence(elt) |
Jeremy Hylton | 36cc6a2 | 2000-03-16 20:06:59 +0000 | [diff] [blame] | 923 | else: |
| 924 | self.emit('STORE_FAST', elt) |
| 925 | |
Thomas Wouters | 46cc7c0 | 2000-08-12 20:32:46 +0000 | [diff] [blame] | 926 | unpackTuple = unpackSequence |
| 927 | |
Jeremy Hylton | 36cc6a2 | 2000-03-16 20:06:59 +0000 | [diff] [blame] | 928 | class ClassCodeGenerator(CodeGenerator): |
| 929 | super_init = CodeGenerator.__init__ |
| 930 | |
| 931 | def __init__(self, klass, filename): |
Thomas Wouters | 46cc7c0 | 2000-08-12 20:32:46 +0000 | [diff] [blame] | 932 | self.graph = pyassem.PyFlowGraph(klass.name, filename, |
| 933 | optimized=0) |
Jeremy Hylton | 36cc6a2 | 2000-03-16 20:06:59 +0000 | [diff] [blame] | 934 | self.super_init(filename) |
| 935 | lnf = walk(klass.code, LocalNameFinder(), 0) |
| 936 | self.locals.push(lnf.getLocals()) |
Thomas Wouters | 46cc7c0 | 2000-08-12 20:32:46 +0000 | [diff] [blame] | 937 | self.graph.setFlag(CO_NEWLOCALS) |
Jeremy Hylton | 36cc6a2 | 2000-03-16 20:06:59 +0000 | [diff] [blame] | 938 | |
| 939 | def finish(self): |
Thomas Wouters | 46cc7c0 | 2000-08-12 20:32:46 +0000 | [diff] [blame] | 940 | self.graph.startExitBlock() |
Jeremy Hylton | 36cc6a2 | 2000-03-16 20:06:59 +0000 | [diff] [blame] | 941 | self.emit('LOAD_LOCALS') |
Thomas Wouters | 46cc7c0 | 2000-08-12 20:32:46 +0000 | [diff] [blame] | 942 | self.emit('RETURN_VALUE') |
Jeremy Hylton | 36cc6a2 | 2000-03-16 20:06:59 +0000 | [diff] [blame] | 943 | |
Jeremy Hylton | 36cc6a2 | 2000-03-16 20:06:59 +0000 | [diff] [blame] | 944 | def generateArgList(arglist): |
| 945 | """Generate an arg list marking TupleArgs""" |
| 946 | args = [] |
| 947 | extra = [] |
| 948 | count = 0 |
| 949 | for elt in arglist: |
Thomas Wouters | 46cc7c0 | 2000-08-12 20:32:46 +0000 | [diff] [blame] | 950 | if type(elt) == types.StringType: |
| 951 | args.append(elt) |
| 952 | elif type(elt) == types.TupleType: |
| 953 | args.append(TupleArg(count, elt)) |
| 954 | count = count + 1 |
| 955 | extra.extend(misc.flatten(elt)) |
| 956 | else: |
| 957 | raise ValueError, "unexpect argument type:", elt |
Jeremy Hylton | 36cc6a2 | 2000-03-16 20:06:59 +0000 | [diff] [blame] | 958 | return args + extra, count |
Jeremy Hylton | a505812 | 2000-02-14 14:14:29 +0000 | [diff] [blame] | 959 | |
Jeremy Hylton | 8b6323d | 2000-02-04 00:28:21 +0000 | [diff] [blame] | 960 | class LocalNameFinder: |
Jeremy Hylton | 36cc6a2 | 2000-03-16 20:06:59 +0000 | [diff] [blame] | 961 | """Find local names in scope""" |
Jeremy Hylton | 8b6323d | 2000-02-04 00:28:21 +0000 | [diff] [blame] | 962 | def __init__(self, names=()): |
Jeremy Hylton | 873bdc1 | 2000-02-17 17:56:29 +0000 | [diff] [blame] | 963 | self.names = misc.Set() |
Jeremy Hylton | 5e0ce53 | 2000-02-10 00:47:08 +0000 | [diff] [blame] | 964 | self.globals = misc.Set() |
Jeremy Hylton | 873bdc1 | 2000-02-17 17:56:29 +0000 | [diff] [blame] | 965 | for name in names: |
| 966 | self.names.add(name) |
Jeremy Hylton | 8b6323d | 2000-02-04 00:28:21 +0000 | [diff] [blame] | 967 | |
| 968 | def getLocals(self): |
Jeremy Hylton | 36cc6a2 | 2000-03-16 20:06:59 +0000 | [diff] [blame] | 969 | for elt in self.globals.elements(): |
Jeremy Hylton | 5e0ce53 | 2000-02-10 00:47:08 +0000 | [diff] [blame] | 970 | if self.names.has_elt(elt): |
| 971 | self.names.remove(elt) |
Jeremy Hylton | 873bdc1 | 2000-02-17 17:56:29 +0000 | [diff] [blame] | 972 | return self.names |
Jeremy Hylton | 8b6323d | 2000-02-04 00:28:21 +0000 | [diff] [blame] | 973 | |
Jeremy Hylton | a505812 | 2000-02-14 14:14:29 +0000 | [diff] [blame] | 974 | def visitDict(self, node): |
Thomas Wouters | 46cc7c0 | 2000-08-12 20:32:46 +0000 | [diff] [blame] | 975 | pass |
Jeremy Hylton | a505812 | 2000-02-14 14:14:29 +0000 | [diff] [blame] | 976 | |
Jeremy Hylton | 5e0ce53 | 2000-02-10 00:47:08 +0000 | [diff] [blame] | 977 | def visitGlobal(self, node): |
| 978 | for name in node.names: |
| 979 | self.globals.add(name) |
Jeremy Hylton | 5e0ce53 | 2000-02-10 00:47:08 +0000 | [diff] [blame] | 980 | |
Jeremy Hylton | 8b6323d | 2000-02-04 00:28:21 +0000 | [diff] [blame] | 981 | def visitFunction(self, node): |
| 982 | self.names.add(node.name) |
Jeremy Hylton | 8b6323d | 2000-02-04 00:28:21 +0000 | [diff] [blame] | 983 | |
Jeremy Hylton | 76d01b8 | 2000-02-11 20:27:07 +0000 | [diff] [blame] | 984 | def visitLambda(self, node): |
Thomas Wouters | 46cc7c0 | 2000-08-12 20:32:46 +0000 | [diff] [blame] | 985 | pass |
Jeremy Hylton | 76d01b8 | 2000-02-11 20:27:07 +0000 | [diff] [blame] | 986 | |
Jeremy Hylton | 8b6323d | 2000-02-04 00:28:21 +0000 | [diff] [blame] | 987 | def visitImport(self, node): |
Jeremy Hylton | 2051608 | 2000-09-01 20:33:26 +0000 | [diff] [blame] | 988 | for name, alias in node.names: |
| 989 | self.names.add(alias or name) |
Jeremy Hylton | 8b6323d | 2000-02-04 00:28:21 +0000 | [diff] [blame] | 990 | |
| 991 | def visitFrom(self, node): |
Jeremy Hylton | 2051608 | 2000-09-01 20:33:26 +0000 | [diff] [blame] | 992 | for name, alias in node.names: |
| 993 | self.names.add(alias or name) |
Jeremy Hylton | 8b6323d | 2000-02-04 00:28:21 +0000 | [diff] [blame] | 994 | |
Jeremy Hylton | 7fab23e | 2000-03-06 19:10:54 +0000 | [diff] [blame] | 995 | def visitClass(self, node): |
Jeremy Hylton | 873bdc1 | 2000-02-17 17:56:29 +0000 | [diff] [blame] | 996 | self.names.add(node.name) |
Jeremy Hylton | 8b6323d | 2000-02-04 00:28:21 +0000 | [diff] [blame] | 997 | |
| 998 | def visitAssName(self, node): |
Jeremy Hylton | 873bdc1 | 2000-02-17 17:56:29 +0000 | [diff] [blame] | 999 | self.names.add(node.name) |
Jeremy Hylton | 8b6323d | 2000-02-04 00:28:21 +0000 | [diff] [blame] | 1000 | |
Jeremy Hylton | 36cc6a2 | 2000-03-16 20:06:59 +0000 | [diff] [blame] | 1001 | def findOp(node): |
| 1002 | """Find the op (DELETE, LOAD, STORE) in an AssTuple tree""" |
| 1003 | v = OpFinder() |
| 1004 | walk(node, v, 0) |
| 1005 | return v.op |
| 1006 | |
Jeremy Hylton | 3ec7e2c | 2000-02-17 22:09:35 +0000 | [diff] [blame] | 1007 | class OpFinder: |
| 1008 | def __init__(self): |
| 1009 | self.op = None |
| 1010 | def visitAssName(self, node): |
| 1011 | if self.op is None: |
| 1012 | self.op = node.flags |
| 1013 | elif self.op != node.flags: |
| 1014 | raise ValueError, "mixed ops in stmt" |
| 1015 | |
Jeremy Hylton | 9c048f9 | 2000-10-13 21:58:13 +0000 | [diff] [blame] | 1016 | class Delegator: |
| 1017 | """Base class to support delegation for augmented assignment nodes |
| 1018 | |
| 1019 | To generator code for augmented assignments, we use the following |
| 1020 | wrapper classes. In visitAugAssign, the left-hand expression node |
| 1021 | is visited twice. The first time the visit uses the normal method |
| 1022 | for that node . The second time the visit uses a different method |
| 1023 | that generates the appropriate code to perform the assignment. |
| 1024 | These delegator classes wrap the original AST nodes in order to |
| 1025 | support the variant visit methods. |
| 1026 | """ |
| 1027 | def __init__(self, obj): |
| 1028 | self.obj = obj |
| 1029 | |
| 1030 | def __getattr__(self, attr): |
| 1031 | return getattr(self.obj, attr) |
| 1032 | |
| 1033 | class AugGetattr(Delegator): |
| 1034 | pass |
| 1035 | |
| 1036 | class AugName(Delegator): |
| 1037 | pass |
| 1038 | |
| 1039 | class AugSlice(Delegator): |
| 1040 | pass |
| 1041 | |
| 1042 | class AugSubscript(Delegator): |
| 1043 | pass |
| 1044 | |
| 1045 | wrapper = { |
| 1046 | ast.Getattr: AugGetattr, |
| 1047 | ast.Name: AugName, |
| 1048 | ast.Slice: AugSlice, |
| 1049 | ast.Subscript: AugSubscript, |
| 1050 | } |
| 1051 | |
| 1052 | def wrap_aug(node): |
| 1053 | return wrapper[node.__class__](node) |
| 1054 | |
Jeremy Hylton | 36cc6a2 | 2000-03-16 20:06:59 +0000 | [diff] [blame] | 1055 | if __name__ == "__main__": |
| 1056 | import sys |
Jeremy Hylton | 3ec7e2c | 2000-02-17 22:09:35 +0000 | [diff] [blame] | 1057 | |
Jeremy Hylton | 36cc6a2 | 2000-03-16 20:06:59 +0000 | [diff] [blame] | 1058 | for file in sys.argv[1:]: |
Thomas Wouters | 46cc7c0 | 2000-08-12 20:32:46 +0000 | [diff] [blame] | 1059 | compile(file) |