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@21 | 67 | self.constants = {} |
paul@17 | 68 | |
paul@19 | 69 | # Wiring within the code. |
paul@19 | 70 | |
paul@19 | 71 | self.labels = {} |
paul@19 | 72 | self.label_number = 0 |
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@19 | 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@21 | 123 | print self.module.name, len(self.code) + self.unit.code_location, |
paul@19 | 124 | self.code.append(op) |
paul@21 | 125 | print op |
paul@19 | 126 | |
paul@17 | 127 | # Visitor methods. |
paul@17 | 128 | |
paul@17 | 129 | def default(self, node, *args): |
paul@17 | 130 | raise TranslateError, node.__class__ |
paul@17 | 131 | |
paul@17 | 132 | def dispatch(self, node, *args): |
paul@17 | 133 | return ASTVisitor.dispatch(self, node, *args) |
paul@17 | 134 | |
paul@17 | 135 | def _visitName(self, node, classes): |
paul@17 | 136 | name = node.name |
paul@17 | 137 | scope = self.get_scope(name) |
paul@17 | 138 | NameInstruction, AttrInstruction = classes |
paul@17 | 139 | |
paul@17 | 140 | if scope == "local": |
paul@17 | 141 | unit = self.unit |
paul@17 | 142 | if isinstance(unit, micropython.inspect.Function): |
paul@19 | 143 | self.new_op(NameInstruction(unit.locals()[name])) |
paul@17 | 144 | elif isinstance(unit, micropython.inspect.Class): |
paul@19 | 145 | self.new_op(AttrInstruction(unit.all_class_attributes()[name])) |
paul@17 | 146 | elif isinstance(unit, micropython.inspect.Module): |
paul@19 | 147 | self.new_op(AttrInstruction(unit.module_attributes()[name])) |
paul@17 | 148 | else: |
paul@17 | 149 | raise TranslateError, "Program unit %r has no local %r" % (unit, name) |
paul@17 | 150 | |
paul@17 | 151 | elif scope == "global": |
paul@17 | 152 | globals = self.module.module_attributes() |
paul@17 | 153 | if globals.has_key(name): |
paul@19 | 154 | self.new_op(AttrInstruction(globals[name])) |
paul@17 | 155 | else: |
paul@17 | 156 | raise TranslateError, "Module %r has no attribute %r" % (self.module, name) |
paul@17 | 157 | |
paul@17 | 158 | else: |
paul@17 | 159 | builtins = micropython.inspect.builtins.module_attributes() |
paul@19 | 160 | self.new_op(AttrInstruction(builtins[name])) |
paul@17 | 161 | |
paul@21 | 162 | def visitAdd(self, node): |
paul@17 | 163 | |
paul@21 | 164 | """ |
paul@21 | 165 | _t1 = node.left |
paul@21 | 166 | _t2 = node.right |
paul@21 | 167 | try: |
paul@21 | 168 | _t1.__add__(_t2) |
paul@21 | 169 | except AttributeError: |
paul@21 | 170 | _t2.__radd__(_t1) |
paul@21 | 171 | """ |
paul@17 | 172 | |
paul@19 | 173 | def visitAnd(self, node): pass |
paul@17 | 174 | |
paul@19 | 175 | def visitAssert(self, node): pass |
paul@17 | 176 | |
paul@17 | 177 | def visitAssign(self, node): |
paul@19 | 178 | self.dispatch(node.expr) |
paul@17 | 179 | for n in node.nodes: |
paul@19 | 180 | self.dispatch(n) |
paul@17 | 181 | |
paul@19 | 182 | def visitAssAttr(self, node): pass |
paul@17 | 183 | |
paul@19 | 184 | def visitAssList(self, node): pass |
paul@17 | 185 | |
paul@17 | 186 | def visitAssName(self, node): |
paul@19 | 187 | self._visitName(node, (StoreName, StoreAttr)) |
paul@17 | 188 | |
paul@17 | 189 | visitAssTuple = visitAssList |
paul@17 | 190 | |
paul@19 | 191 | def visitAugAssign(self, node): pass |
paul@17 | 192 | |
paul@19 | 193 | def visitBackquote(self, node): pass |
paul@17 | 194 | |
paul@19 | 195 | def visitBitand(self, node): pass |
paul@17 | 196 | |
paul@19 | 197 | def visitBitor(self, node): pass |
paul@17 | 198 | |
paul@19 | 199 | def visitBitxor(self, node): pass |
paul@17 | 200 | |
paul@19 | 201 | def visitBreak(self, node): pass |
paul@17 | 202 | |
paul@21 | 203 | def visitCallFunc(self, node): |
paul@21 | 204 | |
paul@21 | 205 | """ |
paul@21 | 206 | Evaluate positional arguments, evaluate and store keyword arguments in |
paul@21 | 207 | the correct location, then invoke the function. |
paul@21 | 208 | """ |
paul@21 | 209 | |
paul@21 | 210 | # Record the location of the invocation. |
paul@21 | 211 | |
paul@21 | 212 | self.new_op(MakeFrame()) # records the start of the frame |
paul@21 | 213 | |
paul@21 | 214 | # Evaluate the target. |
paul@21 | 215 | |
paul@21 | 216 | self.dispatch(node.node) |
paul@21 | 217 | |
paul@21 | 218 | # Evaluate the arguments. |
paul@21 | 219 | |
paul@21 | 220 | for i, arg in enumerate(node.args): |
paul@21 | 221 | if isinstance(arg, compiler.ast.Keyword): |
paul@21 | 222 | self.dispatch(arg.expr) |
paul@21 | 223 | |
paul@21 | 224 | # Combine the target details with the name to get the location. |
paul@21 | 225 | # See the access method on the Table class. |
paul@21 | 226 | |
paul@21 | 227 | paramindex = self.paramtable.get_index(arg.name) |
paul@21 | 228 | self.new_op(StoreFrame(paramindex)) |
paul@21 | 229 | |
paul@21 | 230 | # use (callable+0)+paramindex+table |
paul@21 | 231 | # checks embedded offset against (callable+0) |
paul@21 | 232 | # moves the top of stack to frame+position |
paul@21 | 233 | |
paul@21 | 234 | else: |
paul@21 | 235 | self.dispatch(arg) |
paul@21 | 236 | |
paul@21 | 237 | self.new_op(LoadCallable()) # uses the start of the frame to get the callable |
paul@21 | 238 | self.new_op(Jump()) |
paul@21 | 239 | |
paul@21 | 240 | # NOTE: Exception handling required. |
paul@21 | 241 | |
paul@21 | 242 | self.new_op(DropFrame()) |
paul@17 | 243 | |
paul@19 | 244 | def visitClass(self, node): pass |
paul@17 | 245 | |
paul@21 | 246 | def visitCompare(self, node): |
paul@21 | 247 | |
paul@21 | 248 | """ |
paul@21 | 249 | self.dispatch(node.expr) |
paul@21 | 250 | for op_name, next_node in compare.ops: |
paul@21 | 251 | methods = self.comparison_methods[op_name] |
paul@21 | 252 | if methods is not None: |
paul@21 | 253 | # Generate method call using evaluated argument and next node. |
paul@21 | 254 | else: |
paul@21 | 255 | # Deal with the special operators. |
paul@21 | 256 | # Provide short-circuiting. |
paul@21 | 257 | """ |
paul@17 | 258 | |
paul@19 | 259 | def visitConst(self, node): pass |
paul@17 | 260 | |
paul@19 | 261 | def visitContinue(self, node): pass |
paul@17 | 262 | |
paul@19 | 263 | def visitDecorators(self, node): pass |
paul@17 | 264 | |
paul@19 | 265 | def visitDict(self, node): pass |
paul@17 | 266 | |
paul@18 | 267 | def visitDiscard(self, node): |
paul@19 | 268 | self.dispatch(node.expr) |
paul@17 | 269 | |
paul@19 | 270 | def visitDiv(self, node): pass |
paul@17 | 271 | |
paul@19 | 272 | def visitEllipsis(self, node): pass |
paul@17 | 273 | |
paul@19 | 274 | def visitExec(self, node): pass |
paul@17 | 275 | |
paul@19 | 276 | def visitExpression(self, node): pass |
paul@17 | 277 | |
paul@19 | 278 | def visitFloorDiv(self, node): pass |
paul@17 | 279 | |
paul@19 | 280 | def visitFor(self, node): pass |
paul@17 | 281 | |
paul@19 | 282 | def visitFrom(self, node): pass |
paul@17 | 283 | |
paul@17 | 284 | def visitFunction(self, node): |
paul@17 | 285 | |
paul@17 | 286 | # Only store the name when visiting this node from outside. |
paul@17 | 287 | |
paul@17 | 288 | if self.unit is self.module: |
paul@21 | 289 | self.new_op(LoadConst(node.function)) |
paul@19 | 290 | self._visitName(node, (StoreName, StoreAttr)) |
paul@17 | 291 | else: |
paul@19 | 292 | self.dispatch(node.code) |
paul@21 | 293 | self.new_op(Return()) |
paul@17 | 294 | |
paul@19 | 295 | def visitGenExpr(self, node): pass |
paul@17 | 296 | |
paul@19 | 297 | def visitGenExprFor(self, node): pass |
paul@17 | 298 | |
paul@19 | 299 | def visitGenExprIf(self, node): pass |
paul@17 | 300 | |
paul@19 | 301 | def visitGenExprInner(self, node): pass |
paul@17 | 302 | |
paul@19 | 303 | def visitGetattr(self, node): pass |
paul@17 | 304 | |
paul@19 | 305 | def visitGlobal(self, node): pass |
paul@17 | 306 | |
paul@17 | 307 | def visitIf(self, node): |
paul@19 | 308 | first = 1 |
paul@19 | 309 | exit_label = self.new_label() |
paul@17 | 310 | |
paul@19 | 311 | for test, body in node.tests + [(None, node.else_)]: |
paul@19 | 312 | if body is None: |
paul@19 | 313 | break |
paul@19 | 314 | if not first: |
paul@19 | 315 | self.set_label(next_label) |
paul@19 | 316 | if test is not None: |
paul@19 | 317 | self.dispatch(test) |
paul@19 | 318 | next_label = self.new_label() |
paul@19 | 319 | self.new_op(Jump(next_label)) |
paul@19 | 320 | self.dispatch(body) |
paul@19 | 321 | self.new_op(Jump(exit_label)) |
paul@19 | 322 | first = 0 |
paul@17 | 323 | |
paul@19 | 324 | self.set_label(exit_label) |
paul@17 | 325 | |
paul@19 | 326 | def visitImport(self, node): pass |
paul@19 | 327 | |
paul@19 | 328 | def visitInvert(self, node): pass |
paul@17 | 329 | |
paul@19 | 330 | def visitKeyword(self, node): pass |
paul@17 | 331 | |
paul@19 | 332 | def visitLambda(self, node): pass |
paul@19 | 333 | |
paul@19 | 334 | def visitLeftShift(self, node): pass |
paul@17 | 335 | |
paul@19 | 336 | def visitList(self, node): pass |
paul@17 | 337 | |
paul@19 | 338 | def visitListComp(self, node): pass |
paul@17 | 339 | |
paul@19 | 340 | def visitListCompFor(self, node): pass |
paul@17 | 341 | |
paul@19 | 342 | def visitListCompIf(self, node): pass |
paul@19 | 343 | |
paul@19 | 344 | def visitMod(self, node): pass |
paul@17 | 345 | |
paul@17 | 346 | def visitModule(self, node): |
paul@19 | 347 | self.dispatch(node.node) |
paul@17 | 348 | |
paul@19 | 349 | def visitMul(self, node): pass |
paul@17 | 350 | |
paul@17 | 351 | def visitName(self, node): |
paul@19 | 352 | self._visitName(node, (LoadName, LoadAttr)) |
paul@17 | 353 | |
paul@19 | 354 | def visitNot(self, node): pass |
paul@17 | 355 | |
paul@19 | 356 | def visitOr(self, node): pass |
paul@17 | 357 | |
paul@19 | 358 | def visitPass(self, node): pass |
paul@17 | 359 | |
paul@19 | 360 | def visitPower(self, node): pass |
paul@17 | 361 | |
paul@19 | 362 | def visitPrint(self, node): pass |
paul@17 | 363 | |
paul@19 | 364 | def visitPrintnl(self, node): pass |
paul@17 | 365 | |
paul@19 | 366 | def visitRaise(self, node): pass |
paul@17 | 367 | |
paul@21 | 368 | def visitReturn(self, node): |
paul@21 | 369 | if node.value is not None: |
paul@21 | 370 | self.dispatch(node.value) |
paul@21 | 371 | self.new_op(Return()) |
paul@17 | 372 | |
paul@19 | 373 | def visitRightShift(self, node): pass |
paul@17 | 374 | |
paul@19 | 375 | def visitSlice(self, node): pass |
paul@17 | 376 | |
paul@17 | 377 | def visitStmt(self, node): |
paul@17 | 378 | for n in node.nodes: |
paul@19 | 379 | self.dispatch(n) |
paul@19 | 380 | |
paul@19 | 381 | def visitSub(self, node): pass |
paul@17 | 382 | |
paul@19 | 383 | def visitSubscript(self, node): pass |
paul@17 | 384 | |
paul@19 | 385 | def visitTryExcept(self, node): pass |
paul@17 | 386 | |
paul@19 | 387 | def visitTryFinally(self, node): pass |
paul@17 | 388 | |
paul@19 | 389 | def visitTuple(self, node): pass |
paul@17 | 390 | |
paul@19 | 391 | def visitUnaryAdd(self, node): pass |
paul@17 | 392 | |
paul@19 | 393 | def visitUnarySub(self, node): pass |
paul@17 | 394 | |
paul@19 | 395 | def visitWhile(self, node): pass |
paul@17 | 396 | |
paul@19 | 397 | def visitWith(self, node): pass |
paul@17 | 398 | |
paul@19 | 399 | def visitYield(self, node): pass |
paul@17 | 400 | |
paul@21 | 401 | # Useful data. |
paul@21 | 402 | |
paul@21 | 403 | comparison_methods = { |
paul@21 | 404 | "==" : ("__eq__", "__ne__"), |
paul@21 | 405 | "!=" : ("__ne__", "__eq__"), |
paul@21 | 406 | "<" : ("__lt__", "__gt__"), |
paul@21 | 407 | "<=" : ("__le__", "__ge__"), |
paul@21 | 408 | ">=" : ("__ge__", "__le__"), |
paul@21 | 409 | ">" : ("__gt__", "__lt__"), |
paul@21 | 410 | "is" : None, |
paul@21 | 411 | "is not" : None, |
paul@21 | 412 | "in" : None, |
paul@21 | 413 | "not in" : None |
paul@21 | 414 | } |
paul@21 | 415 | |
paul@17 | 416 | # vim: tabstop=4 expandtab shiftwidth=4 |