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