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