paul@17 | 1 | #!/usr/bin/env python |
paul@17 | 2 | |
paul@17 | 3 | """ |
paul@17 | 4 | Translate the AST of a Python program into a more interpretable representation. |
paul@17 | 5 | |
paul@17 | 6 | Copyright (C) 2007 Paul Boddie <paul@boddie.org.uk> |
paul@17 | 7 | |
paul@17 | 8 | This program is free software; you can redistribute it and/or modify it under |
paul@17 | 9 | the terms of the GNU General Public License as published by the Free Software |
paul@17 | 10 | Foundation; either version 3 of the License, or (at your option) any later |
paul@17 | 11 | version. |
paul@17 | 12 | |
paul@17 | 13 | This program is distributed in the hope that it will be useful, but WITHOUT |
paul@17 | 14 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
paul@17 | 15 | FOR A PARTICULAR PURPOSE. See the GNU General Public License for more |
paul@17 | 16 | details. |
paul@17 | 17 | |
paul@17 | 18 | You should have received a copy of the GNU General Public License along with |
paul@17 | 19 | this program. If not, see <http://www.gnu.org/licenses/>. |
paul@17 | 20 | """ |
paul@17 | 21 | |
paul@17 | 22 | import micropython.inspect |
paul@17 | 23 | from micropython.rsvp import * |
paul@17 | 24 | import compiler.ast |
paul@17 | 25 | from compiler.visitor import ASTVisitor |
paul@17 | 26 | try: |
paul@17 | 27 | set |
paul@17 | 28 | except NameError: |
paul@17 | 29 | from sets import Set as set |
paul@17 | 30 | |
paul@17 | 31 | class TranslateError(Exception): |
paul@17 | 32 | |
paul@17 | 33 | "A translation error." |
paul@17 | 34 | |
paul@17 | 35 | pass |
paul@17 | 36 | |
paul@19 | 37 | class Label: |
paul@19 | 38 | |
paul@19 | 39 | "A reference to a location." |
paul@19 | 40 | |
paul@19 | 41 | def __init__(self, number, location=None): |
paul@19 | 42 | self.number = number |
paul@19 | 43 | self.location = location |
paul@19 | 44 | |
paul@19 | 45 | def __repr__(self): |
paul@19 | 46 | return "Label(%r, location=%r)" % (self.number, self.location) |
paul@19 | 47 | |
paul@17 | 48 | # Program visitors. |
paul@17 | 49 | |
paul@17 | 50 | class Translation(ASTVisitor): |
paul@17 | 51 | |
paul@17 | 52 | "A translated module." |
paul@17 | 53 | |
paul@21 | 54 | def __init__(self, module, objtable, paramtable): |
paul@18 | 55 | |
paul@21 | 56 | """ |
paul@21 | 57 | Initialise the translation with an inspected 'module' and an attribute |
paul@21 | 58 | table 'objtable' and parameter table 'paramtable'. |
paul@21 | 59 | """ |
paul@18 | 60 | |
paul@17 | 61 | ASTVisitor.__init__(self) |
paul@17 | 62 | self.visitor = self |
paul@17 | 63 | self.module = module |
paul@21 | 64 | self.objtable = objtable |
paul@21 | 65 | self.paramtable = paramtable |
paul@17 | 66 | self.unit = None |
paul@17 | 67 | |
paul@19 | 68 | # Wiring within the code. |
paul@19 | 69 | |
paul@19 | 70 | self.labels = {} |
paul@19 | 71 | self.label_number = 0 |
paul@19 | 72 | self.code = None |
paul@19 | 73 | |
paul@17 | 74 | def get_module_code(self): |
paul@17 | 75 | |
paul@17 | 76 | "Return the top-level module code." |
paul@17 | 77 | |
paul@17 | 78 | self.unit = self.module |
paul@19 | 79 | self.code = [] |
paul@19 | 80 | self.dispatch(self.module.module) |
paul@19 | 81 | return self.code |
paul@17 | 82 | |
paul@17 | 83 | def get_code(self, unit): |
paul@17 | 84 | |
paul@17 | 85 | "Return the code for the given 'unit'." |
paul@17 | 86 | |
paul@17 | 87 | self.unit = unit |
paul@19 | 88 | self.code = [] |
paul@25 | 89 | if unit.node is not None: |
paul@25 | 90 | self.dispatch(unit.node) |
paul@19 | 91 | return self.code |
paul@17 | 92 | |
paul@17 | 93 | def __repr__(self): |
paul@17 | 94 | return "Translation(%r)" % self.module |
paul@17 | 95 | |
paul@17 | 96 | def get_scope(self, name): |
paul@17 | 97 | if self.unit.has_key(name): |
paul@17 | 98 | return "local" |
paul@17 | 99 | elif self.module.has_key(name): |
paul@17 | 100 | return "global" |
paul@17 | 101 | else: |
paul@17 | 102 | return "builtins" |
paul@17 | 103 | |
paul@19 | 104 | # Code writing methods. |
paul@19 | 105 | |
paul@19 | 106 | def new_label(self): |
paul@19 | 107 | number = self.label_number |
paul@19 | 108 | label = Label(number) |
paul@19 | 109 | self.labels[label] = label |
paul@19 | 110 | self.label_number += 1 |
paul@19 | 111 | return label |
paul@19 | 112 | |
paul@19 | 113 | def set_label(self, label): |
paul@19 | 114 | |
paul@19 | 115 | """ |
paul@19 | 116 | Set the location of 'label' to that within the entire image: the |
paul@19 | 117 | location within the code combined with location of the code unit. |
paul@19 | 118 | """ |
paul@19 | 119 | |
paul@19 | 120 | label.location = len(self.code) + self.unit.code_location |
paul@19 | 121 | |
paul@19 | 122 | def new_op(self, op): |
paul@19 | 123 | self.code.append(op) |
paul@19 | 124 | |
paul@26 | 125 | def last_op(self): |
paul@26 | 126 | return self.code[-1] |
paul@26 | 127 | |
paul@17 | 128 | # Visitor methods. |
paul@17 | 129 | |
paul@17 | 130 | def default(self, node, *args): |
paul@17 | 131 | raise TranslateError, node.__class__ |
paul@17 | 132 | |
paul@17 | 133 | def dispatch(self, node, *args): |
paul@17 | 134 | return ASTVisitor.dispatch(self, node, *args) |
paul@17 | 135 | |
paul@17 | 136 | def _visitName(self, node, classes): |
paul@25 | 137 | #print self.module.name, node.lineno |
paul@17 | 138 | name = node.name |
paul@17 | 139 | scope = self.get_scope(name) |
paul@17 | 140 | NameInstruction, AttrInstruction = classes |
paul@17 | 141 | |
paul@17 | 142 | if scope == "local": |
paul@17 | 143 | unit = self.unit |
paul@17 | 144 | if isinstance(unit, micropython.inspect.Function): |
paul@19 | 145 | self.new_op(NameInstruction(unit.locals()[name])) |
paul@17 | 146 | elif isinstance(unit, micropython.inspect.Class): |
paul@19 | 147 | self.new_op(AttrInstruction(unit.all_class_attributes()[name])) |
paul@17 | 148 | elif isinstance(unit, micropython.inspect.Module): |
paul@19 | 149 | self.new_op(AttrInstruction(unit.module_attributes()[name])) |
paul@17 | 150 | else: |
paul@17 | 151 | raise TranslateError, "Program unit %r has no local %r" % (unit, name) |
paul@17 | 152 | |
paul@17 | 153 | elif scope == "global": |
paul@17 | 154 | globals = self.module.module_attributes() |
paul@17 | 155 | if globals.has_key(name): |
paul@19 | 156 | self.new_op(AttrInstruction(globals[name])) |
paul@17 | 157 | else: |
paul@17 | 158 | raise TranslateError, "Module %r has no attribute %r" % (self.module, name) |
paul@17 | 159 | |
paul@17 | 160 | else: |
paul@17 | 161 | builtins = micropython.inspect.builtins.module_attributes() |
paul@19 | 162 | self.new_op(AttrInstruction(builtins[name])) |
paul@17 | 163 | |
paul@21 | 164 | def visitAdd(self, node): |
paul@17 | 165 | |
paul@21 | 166 | """ |
paul@21 | 167 | _t1 = node.left |
paul@21 | 168 | _t2 = node.right |
paul@21 | 169 | try: |
paul@21 | 170 | _t1.__add__(_t2) |
paul@21 | 171 | except AttributeError: |
paul@21 | 172 | _t2.__radd__(_t1) |
paul@21 | 173 | """ |
paul@17 | 174 | |
paul@19 | 175 | def visitAnd(self, node): pass |
paul@17 | 176 | |
paul@19 | 177 | def visitAssert(self, node): pass |
paul@17 | 178 | |
paul@17 | 179 | def visitAssign(self, node): |
paul@19 | 180 | self.dispatch(node.expr) |
paul@17 | 181 | for n in node.nodes: |
paul@19 | 182 | self.dispatch(n) |
paul@17 | 183 | |
paul@19 | 184 | def visitAssAttr(self, node): pass |
paul@17 | 185 | |
paul@19 | 186 | def visitAssList(self, node): pass |
paul@17 | 187 | |
paul@17 | 188 | def visitAssName(self, node): |
paul@19 | 189 | self._visitName(node, (StoreName, StoreAttr)) |
paul@17 | 190 | |
paul@17 | 191 | visitAssTuple = visitAssList |
paul@17 | 192 | |
paul@19 | 193 | def visitAugAssign(self, node): pass |
paul@17 | 194 | |
paul@19 | 195 | def visitBackquote(self, node): pass |
paul@17 | 196 | |
paul@19 | 197 | def visitBitand(self, node): pass |
paul@17 | 198 | |
paul@19 | 199 | def visitBitor(self, node): pass |
paul@17 | 200 | |
paul@19 | 201 | def visitBitxor(self, node): pass |
paul@17 | 202 | |
paul@19 | 203 | def visitBreak(self, node): pass |
paul@17 | 204 | |
paul@21 | 205 | def visitCallFunc(self, node): |
paul@21 | 206 | |
paul@21 | 207 | """ |
paul@21 | 208 | Evaluate positional arguments, evaluate and store keyword arguments in |
paul@21 | 209 | the correct location, then invoke the function. |
paul@21 | 210 | """ |
paul@21 | 211 | |
paul@21 | 212 | # Record the location of the invocation. |
paul@21 | 213 | |
paul@21 | 214 | self.new_op(MakeFrame()) # records the start of the frame |
paul@21 | 215 | |
paul@21 | 216 | # Evaluate the target. |
paul@21 | 217 | |
paul@21 | 218 | self.dispatch(node.node) |
paul@21 | 219 | |
paul@26 | 220 | # NOTE: Only simple cases are used for optimisations. |
paul@26 | 221 | |
paul@26 | 222 | last = self.last_op() |
paul@26 | 223 | if isinstance(last, (LoadName, LoadAttr)) and last.attr.assignments == 1: |
paul@26 | 224 | target = last.attr.value |
paul@26 | 225 | else: |
paul@26 | 226 | target = None |
paul@26 | 227 | |
paul@21 | 228 | # Evaluate the arguments. |
paul@21 | 229 | |
paul@22 | 230 | positional = 1 |
paul@22 | 231 | |
paul@21 | 232 | for i, arg in enumerate(node.args): |
paul@21 | 233 | if isinstance(arg, compiler.ast.Keyword): |
paul@22 | 234 | if positional: |
paul@22 | 235 | self.new_op(ReserveFrame(len(node.args) - i)) |
paul@22 | 236 | positional = 0 |
paul@22 | 237 | |
paul@21 | 238 | self.dispatch(arg.expr) |
paul@21 | 239 | |
paul@26 | 240 | # Optimise where the target is known now. |
paul@26 | 241 | |
paul@26 | 242 | if target is not None: |
paul@26 | 243 | pos = self.paramtable.table[target.full_name()][arg.name] |
paul@26 | 244 | self.new_op(StoreFrame(pos)) |
paul@26 | 245 | |
paul@26 | 246 | # Otherwise, generate the code needed to obtain the details of |
paul@26 | 247 | # the parameter location. |
paul@26 | 248 | |
paul@26 | 249 | else: |
paul@21 | 250 | |
paul@26 | 251 | # Combine the target details with the name to get the location. |
paul@26 | 252 | # See the access method on the List class. |
paul@26 | 253 | |
paul@26 | 254 | paramindex = self.paramtable.get_index(arg.name) |
paul@21 | 255 | |
paul@26 | 256 | self.new_op(StoreFrameIndex(paramindex)) |
paul@26 | 257 | |
paul@26 | 258 | # use (callable+0)+paramindex+table |
paul@26 | 259 | # checks embedded offset against (callable+0) |
paul@26 | 260 | # moves the top of stack to frame+position |
paul@21 | 261 | |
paul@21 | 262 | else: |
paul@21 | 263 | self.dispatch(arg) |
paul@21 | 264 | |
paul@21 | 265 | self.new_op(LoadCallable()) # uses the start of the frame to get the callable |
paul@21 | 266 | self.new_op(Jump()) |
paul@21 | 267 | |
paul@21 | 268 | # NOTE: Exception handling required. |
paul@21 | 269 | |
paul@21 | 270 | self.new_op(DropFrame()) |
paul@17 | 271 | |
paul@19 | 272 | def visitClass(self, node): pass |
paul@17 | 273 | |
paul@21 | 274 | def visitCompare(self, node): |
paul@21 | 275 | |
paul@21 | 276 | """ |
paul@21 | 277 | self.dispatch(node.expr) |
paul@21 | 278 | for op_name, next_node in compare.ops: |
paul@21 | 279 | methods = self.comparison_methods[op_name] |
paul@21 | 280 | if methods is not None: |
paul@21 | 281 | # Generate method call using evaluated argument and next node. |
paul@21 | 282 | else: |
paul@21 | 283 | # Deal with the special operators. |
paul@21 | 284 | # Provide short-circuiting. |
paul@21 | 285 | """ |
paul@17 | 286 | |
paul@22 | 287 | def visitConst(self, node): |
paul@22 | 288 | const = self.module.constant_values[node.value] |
paul@22 | 289 | self.new_op(LoadConst(const)) |
paul@17 | 290 | |
paul@19 | 291 | def visitContinue(self, node): pass |
paul@17 | 292 | |
paul@19 | 293 | def visitDecorators(self, node): pass |
paul@17 | 294 | |
paul@19 | 295 | def visitDict(self, node): pass |
paul@17 | 296 | |
paul@18 | 297 | def visitDiscard(self, node): |
paul@19 | 298 | self.dispatch(node.expr) |
paul@17 | 299 | |
paul@19 | 300 | def visitDiv(self, node): pass |
paul@17 | 301 | |
paul@19 | 302 | def visitEllipsis(self, node): pass |
paul@17 | 303 | |
paul@19 | 304 | def visitExec(self, node): pass |
paul@17 | 305 | |
paul@19 | 306 | def visitExpression(self, node): pass |
paul@17 | 307 | |
paul@19 | 308 | def visitFloorDiv(self, node): pass |
paul@17 | 309 | |
paul@19 | 310 | def visitFor(self, node): pass |
paul@17 | 311 | |
paul@19 | 312 | def visitFrom(self, node): pass |
paul@17 | 313 | |
paul@17 | 314 | def visitFunction(self, node): |
paul@17 | 315 | |
paul@17 | 316 | # Only store the name when visiting this node from outside. |
paul@17 | 317 | |
paul@17 | 318 | if self.unit is self.module: |
paul@21 | 319 | self.new_op(LoadConst(node.function)) |
paul@19 | 320 | self._visitName(node, (StoreName, StoreAttr)) |
paul@22 | 321 | |
paul@22 | 322 | # Visiting of the code occurs when get_code is invoked on this node. |
paul@22 | 323 | |
paul@17 | 324 | else: |
paul@19 | 325 | self.dispatch(node.code) |
paul@21 | 326 | self.new_op(Return()) |
paul@17 | 327 | |
paul@19 | 328 | def visitGenExpr(self, node): pass |
paul@17 | 329 | |
paul@19 | 330 | def visitGenExprFor(self, node): pass |
paul@17 | 331 | |
paul@19 | 332 | def visitGenExprIf(self, node): pass |
paul@17 | 333 | |
paul@19 | 334 | def visitGenExprInner(self, node): pass |
paul@17 | 335 | |
paul@19 | 336 | def visitGetattr(self, node): pass |
paul@17 | 337 | |
paul@19 | 338 | def visitGlobal(self, node): pass |
paul@17 | 339 | |
paul@17 | 340 | def visitIf(self, node): |
paul@19 | 341 | first = 1 |
paul@19 | 342 | exit_label = self.new_label() |
paul@17 | 343 | |
paul@19 | 344 | for test, body in node.tests + [(None, node.else_)]: |
paul@19 | 345 | if body is None: |
paul@19 | 346 | break |
paul@19 | 347 | if not first: |
paul@19 | 348 | self.set_label(next_label) |
paul@19 | 349 | if test is not None: |
paul@19 | 350 | self.dispatch(test) |
paul@19 | 351 | next_label = self.new_label() |
paul@19 | 352 | self.new_op(Jump(next_label)) |
paul@19 | 353 | self.dispatch(body) |
paul@19 | 354 | self.new_op(Jump(exit_label)) |
paul@19 | 355 | first = 0 |
paul@17 | 356 | |
paul@19 | 357 | self.set_label(exit_label) |
paul@17 | 358 | |
paul@19 | 359 | def visitImport(self, node): pass |
paul@19 | 360 | |
paul@19 | 361 | def visitInvert(self, node): pass |
paul@17 | 362 | |
paul@19 | 363 | def visitKeyword(self, node): pass |
paul@17 | 364 | |
paul@19 | 365 | def visitLambda(self, node): pass |
paul@19 | 366 | |
paul@19 | 367 | def visitLeftShift(self, node): pass |
paul@17 | 368 | |
paul@19 | 369 | def visitList(self, node): pass |
paul@17 | 370 | |
paul@19 | 371 | def visitListComp(self, node): pass |
paul@17 | 372 | |
paul@19 | 373 | def visitListCompFor(self, node): pass |
paul@17 | 374 | |
paul@19 | 375 | def visitListCompIf(self, node): pass |
paul@19 | 376 | |
paul@19 | 377 | def visitMod(self, node): pass |
paul@17 | 378 | |
paul@17 | 379 | def visitModule(self, node): |
paul@19 | 380 | self.dispatch(node.node) |
paul@17 | 381 | |
paul@19 | 382 | def visitMul(self, node): pass |
paul@17 | 383 | |
paul@17 | 384 | def visitName(self, node): |
paul@19 | 385 | self._visitName(node, (LoadName, LoadAttr)) |
paul@17 | 386 | |
paul@19 | 387 | def visitNot(self, node): pass |
paul@17 | 388 | |
paul@19 | 389 | def visitOr(self, node): pass |
paul@17 | 390 | |
paul@19 | 391 | def visitPass(self, node): pass |
paul@17 | 392 | |
paul@19 | 393 | def visitPower(self, node): pass |
paul@17 | 394 | |
paul@19 | 395 | def visitPrint(self, node): pass |
paul@17 | 396 | |
paul@19 | 397 | def visitPrintnl(self, node): pass |
paul@17 | 398 | |
paul@19 | 399 | def visitRaise(self, node): pass |
paul@17 | 400 | |
paul@21 | 401 | def visitReturn(self, node): |
paul@21 | 402 | if node.value is not None: |
paul@21 | 403 | self.dispatch(node.value) |
paul@21 | 404 | self.new_op(Return()) |
paul@17 | 405 | |
paul@19 | 406 | def visitRightShift(self, node): pass |
paul@17 | 407 | |
paul@19 | 408 | def visitSlice(self, node): pass |
paul@17 | 409 | |
paul@17 | 410 | def visitStmt(self, node): |
paul@17 | 411 | for n in node.nodes: |
paul@19 | 412 | self.dispatch(n) |
paul@19 | 413 | |
paul@19 | 414 | def visitSub(self, node): pass |
paul@17 | 415 | |
paul@19 | 416 | def visitSubscript(self, node): pass |
paul@17 | 417 | |
paul@19 | 418 | def visitTryExcept(self, node): pass |
paul@17 | 419 | |
paul@19 | 420 | def visitTryFinally(self, node): pass |
paul@17 | 421 | |
paul@19 | 422 | def visitTuple(self, node): pass |
paul@17 | 423 | |
paul@19 | 424 | def visitUnaryAdd(self, node): pass |
paul@17 | 425 | |
paul@19 | 426 | def visitUnarySub(self, node): pass |
paul@17 | 427 | |
paul@19 | 428 | def visitWhile(self, node): pass |
paul@17 | 429 | |
paul@19 | 430 | def visitWith(self, node): pass |
paul@17 | 431 | |
paul@19 | 432 | def visitYield(self, node): pass |
paul@17 | 433 | |
paul@21 | 434 | # Useful data. |
paul@21 | 435 | |
paul@21 | 436 | comparison_methods = { |
paul@21 | 437 | "==" : ("__eq__", "__ne__"), |
paul@21 | 438 | "!=" : ("__ne__", "__eq__"), |
paul@21 | 439 | "<" : ("__lt__", "__gt__"), |
paul@21 | 440 | "<=" : ("__le__", "__ge__"), |
paul@21 | 441 | ">=" : ("__ge__", "__le__"), |
paul@21 | 442 | ">" : ("__gt__", "__lt__"), |
paul@21 | 443 | "is" : None, |
paul@21 | 444 | "is not" : None, |
paul@21 | 445 | "in" : None, |
paul@21 | 446 | "not in" : None |
paul@21 | 447 | } |
paul@21 | 448 | |
paul@17 | 449 | # vim: tabstop=4 expandtab shiftwidth=4 |