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@17 | 125 | # Visitor methods. |
paul@17 | 126 | |
paul@17 | 127 | def default(self, node, *args): |
paul@17 | 128 | raise TranslateError, 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@25 | 134 | #print self.module.name, node.lineno |
paul@17 | 135 | name = node.name |
paul@17 | 136 | scope = self.get_scope(name) |
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@19 | 142 | self.new_op(NameInstruction(unit.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@17 | 148 | raise TranslateError, "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@17 | 155 | raise TranslateError, "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@21 | 217 | # Evaluate the arguments. |
paul@21 | 218 | |
paul@22 | 219 | positional = 1 |
paul@22 | 220 | |
paul@21 | 221 | for i, arg in enumerate(node.args): |
paul@21 | 222 | if isinstance(arg, compiler.ast.Keyword): |
paul@22 | 223 | if positional: |
paul@22 | 224 | self.new_op(ReserveFrame(len(node.args) - i)) |
paul@22 | 225 | positional = 0 |
paul@22 | 226 | |
paul@21 | 227 | self.dispatch(arg.expr) |
paul@21 | 228 | |
paul@21 | 229 | # Combine the target details with the name to get the location. |
paul@21 | 230 | # See the access method on the Table class. |
paul@21 | 231 | |
paul@21 | 232 | paramindex = self.paramtable.get_index(arg.name) |
paul@21 | 233 | self.new_op(StoreFrame(paramindex)) |
paul@21 | 234 | |
paul@21 | 235 | # use (callable+0)+paramindex+table |
paul@21 | 236 | # checks embedded offset against (callable+0) |
paul@21 | 237 | # moves the top of stack to frame+position |
paul@21 | 238 | |
paul@21 | 239 | else: |
paul@21 | 240 | self.dispatch(arg) |
paul@21 | 241 | |
paul@21 | 242 | self.new_op(LoadCallable()) # uses the start of the frame to get the callable |
paul@21 | 243 | self.new_op(Jump()) |
paul@21 | 244 | |
paul@21 | 245 | # NOTE: Exception handling required. |
paul@21 | 246 | |
paul@21 | 247 | self.new_op(DropFrame()) |
paul@17 | 248 | |
paul@19 | 249 | def visitClass(self, node): pass |
paul@17 | 250 | |
paul@21 | 251 | def visitCompare(self, node): |
paul@21 | 252 | |
paul@21 | 253 | """ |
paul@21 | 254 | self.dispatch(node.expr) |
paul@21 | 255 | for op_name, next_node in compare.ops: |
paul@21 | 256 | methods = self.comparison_methods[op_name] |
paul@21 | 257 | if methods is not None: |
paul@21 | 258 | # Generate method call using evaluated argument and next node. |
paul@21 | 259 | else: |
paul@21 | 260 | # Deal with the special operators. |
paul@21 | 261 | # Provide short-circuiting. |
paul@21 | 262 | """ |
paul@17 | 263 | |
paul@22 | 264 | def visitConst(self, node): |
paul@22 | 265 | const = self.module.constant_values[node.value] |
paul@22 | 266 | self.new_op(LoadConst(const)) |
paul@17 | 267 | |
paul@19 | 268 | def visitContinue(self, node): pass |
paul@17 | 269 | |
paul@19 | 270 | def visitDecorators(self, node): pass |
paul@17 | 271 | |
paul@19 | 272 | def visitDict(self, node): pass |
paul@17 | 273 | |
paul@18 | 274 | def visitDiscard(self, node): |
paul@19 | 275 | self.dispatch(node.expr) |
paul@17 | 276 | |
paul@19 | 277 | def visitDiv(self, node): pass |
paul@17 | 278 | |
paul@19 | 279 | def visitEllipsis(self, node): pass |
paul@17 | 280 | |
paul@19 | 281 | def visitExec(self, node): pass |
paul@17 | 282 | |
paul@19 | 283 | def visitExpression(self, node): pass |
paul@17 | 284 | |
paul@19 | 285 | def visitFloorDiv(self, node): pass |
paul@17 | 286 | |
paul@19 | 287 | def visitFor(self, node): pass |
paul@17 | 288 | |
paul@19 | 289 | def visitFrom(self, node): pass |
paul@17 | 290 | |
paul@17 | 291 | def visitFunction(self, node): |
paul@17 | 292 | |
paul@17 | 293 | # Only store the name when visiting this node from outside. |
paul@17 | 294 | |
paul@17 | 295 | if self.unit is self.module: |
paul@21 | 296 | self.new_op(LoadConst(node.function)) |
paul@19 | 297 | self._visitName(node, (StoreName, StoreAttr)) |
paul@22 | 298 | |
paul@22 | 299 | # Visiting of the code occurs when get_code is invoked on this node. |
paul@22 | 300 | |
paul@17 | 301 | else: |
paul@19 | 302 | self.dispatch(node.code) |
paul@21 | 303 | self.new_op(Return()) |
paul@17 | 304 | |
paul@19 | 305 | def visitGenExpr(self, node): pass |
paul@17 | 306 | |
paul@19 | 307 | def visitGenExprFor(self, node): pass |
paul@17 | 308 | |
paul@19 | 309 | def visitGenExprIf(self, node): pass |
paul@17 | 310 | |
paul@19 | 311 | def visitGenExprInner(self, node): pass |
paul@17 | 312 | |
paul@19 | 313 | def visitGetattr(self, node): pass |
paul@17 | 314 | |
paul@19 | 315 | def visitGlobal(self, node): pass |
paul@17 | 316 | |
paul@17 | 317 | def visitIf(self, node): |
paul@19 | 318 | first = 1 |
paul@19 | 319 | exit_label = self.new_label() |
paul@17 | 320 | |
paul@19 | 321 | for test, body in node.tests + [(None, node.else_)]: |
paul@19 | 322 | if body is None: |
paul@19 | 323 | break |
paul@19 | 324 | if not first: |
paul@19 | 325 | self.set_label(next_label) |
paul@19 | 326 | if test is not None: |
paul@19 | 327 | self.dispatch(test) |
paul@19 | 328 | next_label = self.new_label() |
paul@19 | 329 | self.new_op(Jump(next_label)) |
paul@19 | 330 | self.dispatch(body) |
paul@19 | 331 | self.new_op(Jump(exit_label)) |
paul@19 | 332 | first = 0 |
paul@17 | 333 | |
paul@19 | 334 | self.set_label(exit_label) |
paul@17 | 335 | |
paul@19 | 336 | def visitImport(self, node): pass |
paul@19 | 337 | |
paul@19 | 338 | def visitInvert(self, node): pass |
paul@17 | 339 | |
paul@19 | 340 | def visitKeyword(self, node): pass |
paul@17 | 341 | |
paul@19 | 342 | def visitLambda(self, node): pass |
paul@19 | 343 | |
paul@19 | 344 | def visitLeftShift(self, node): pass |
paul@17 | 345 | |
paul@19 | 346 | def visitList(self, node): pass |
paul@17 | 347 | |
paul@19 | 348 | def visitListComp(self, node): pass |
paul@17 | 349 | |
paul@19 | 350 | def visitListCompFor(self, node): pass |
paul@17 | 351 | |
paul@19 | 352 | def visitListCompIf(self, node): pass |
paul@19 | 353 | |
paul@19 | 354 | def visitMod(self, node): pass |
paul@17 | 355 | |
paul@17 | 356 | def visitModule(self, node): |
paul@19 | 357 | self.dispatch(node.node) |
paul@17 | 358 | |
paul@19 | 359 | def visitMul(self, node): pass |
paul@17 | 360 | |
paul@17 | 361 | def visitName(self, node): |
paul@19 | 362 | self._visitName(node, (LoadName, LoadAttr)) |
paul@17 | 363 | |
paul@19 | 364 | def visitNot(self, node): pass |
paul@17 | 365 | |
paul@19 | 366 | def visitOr(self, node): pass |
paul@17 | 367 | |
paul@19 | 368 | def visitPass(self, node): pass |
paul@17 | 369 | |
paul@19 | 370 | def visitPower(self, node): pass |
paul@17 | 371 | |
paul@19 | 372 | def visitPrint(self, node): pass |
paul@17 | 373 | |
paul@19 | 374 | def visitPrintnl(self, node): pass |
paul@17 | 375 | |
paul@19 | 376 | def visitRaise(self, node): pass |
paul@17 | 377 | |
paul@21 | 378 | def visitReturn(self, node): |
paul@21 | 379 | if node.value is not None: |
paul@21 | 380 | self.dispatch(node.value) |
paul@21 | 381 | self.new_op(Return()) |
paul@17 | 382 | |
paul@19 | 383 | def visitRightShift(self, node): pass |
paul@17 | 384 | |
paul@19 | 385 | def visitSlice(self, node): pass |
paul@17 | 386 | |
paul@17 | 387 | def visitStmt(self, node): |
paul@17 | 388 | for n in node.nodes: |
paul@19 | 389 | self.dispatch(n) |
paul@19 | 390 | |
paul@19 | 391 | def visitSub(self, node): pass |
paul@17 | 392 | |
paul@19 | 393 | def visitSubscript(self, node): pass |
paul@17 | 394 | |
paul@19 | 395 | def visitTryExcept(self, node): pass |
paul@17 | 396 | |
paul@19 | 397 | def visitTryFinally(self, node): pass |
paul@17 | 398 | |
paul@19 | 399 | def visitTuple(self, node): pass |
paul@17 | 400 | |
paul@19 | 401 | def visitUnaryAdd(self, node): pass |
paul@17 | 402 | |
paul@19 | 403 | def visitUnarySub(self, node): pass |
paul@17 | 404 | |
paul@19 | 405 | def visitWhile(self, node): pass |
paul@17 | 406 | |
paul@19 | 407 | def visitWith(self, node): pass |
paul@17 | 408 | |
paul@19 | 409 | def visitYield(self, node): pass |
paul@17 | 410 | |
paul@21 | 411 | # Useful data. |
paul@21 | 412 | |
paul@21 | 413 | comparison_methods = { |
paul@21 | 414 | "==" : ("__eq__", "__ne__"), |
paul@21 | 415 | "!=" : ("__ne__", "__eq__"), |
paul@21 | 416 | "<" : ("__lt__", "__gt__"), |
paul@21 | 417 | "<=" : ("__le__", "__ge__"), |
paul@21 | 418 | ">=" : ("__ge__", "__le__"), |
paul@21 | 419 | ">" : ("__gt__", "__lt__"), |
paul@21 | 420 | "is" : None, |
paul@21 | 421 | "is not" : None, |
paul@21 | 422 | "in" : None, |
paul@21 | 423 | "not in" : None |
paul@21 | 424 | } |
paul@21 | 425 | |
paul@17 | 426 | # vim: tabstop=4 expandtab shiftwidth=4 |