paulb@273 | 1 | #!/usr/bin/env python |
paulb@273 | 2 | |
paulb@273 | 3 | """ |
paulb@273 | 4 | Generate RSVP code from simplified nodes. |
paulb@273 | 5 | |
paulb@273 | 6 | Copyright (C) 2007 Paul Boddie <paul@boddie.org.uk> |
paulb@273 | 7 | |
paulb@273 | 8 | This program is free software; you can redistribute it and/or modify it under |
paulb@273 | 9 | the terms of the GNU General Public License as published by the Free Software |
paulb@273 | 10 | Foundation; either version 3 of the License, or (at your option) any later |
paulb@273 | 11 | version. |
paulb@273 | 12 | |
paulb@273 | 13 | This program is distributed in the hope that it will be useful, but WITHOUT |
paulb@273 | 14 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
paulb@273 | 15 | FOR A PARTICULAR PURPOSE. See the GNU General Public License for more |
paulb@273 | 16 | details. |
paulb@273 | 17 | |
paulb@273 | 18 | You should have received a copy of the GNU General Public License along with |
paulb@273 | 19 | this program. If not, see <http://www.gnu.org/licenses/>. |
paulb@273 | 20 | """ |
paulb@273 | 21 | |
paulb@273 | 22 | from simplify.simplified import * |
paulb@273 | 23 | import rsvp |
paulb@273 | 24 | |
paulb@273 | 25 | # Classes. |
paulb@273 | 26 | |
paulb@273 | 27 | class Generator(Visitor): |
paulb@273 | 28 | |
paul@282 | 29 | """ |
paul@282 | 30 | An RSVP code generator. |
paul@282 | 31 | |
paul@282 | 32 | Covered: |
paul@282 | 33 | |
paul@282 | 34 | Missing: Assign, CheckType, Conditional, Constant, Global, Import, |
paul@282 | 35 | InvokeRef, InvokeFunction, LoadAttr, LoadExc, LoadName, LoadRef, |
paul@282 | 36 | LoadTemp, Module, Not, Pass, Raise, ReleaseTemp, ReturnFromBlock, |
paul@282 | 37 | ReturnFromFunction, StoreAttr, StoreName, StoreTemp, Subprogram, |
paul@282 | 38 | Try. |
paul@282 | 39 | """ |
paulb@273 | 40 | |
paul@277 | 41 | def __init__(self, debug=0): |
paulb@273 | 42 | |
paulb@273 | 43 | "Initialise the visitor." |
paulb@273 | 44 | |
paul@277 | 45 | self.debug = debug |
paulb@273 | 46 | Visitor.__init__(self) |
paulb@273 | 47 | self.assemblers = [] |
paulb@273 | 48 | self.current_assemblers = [] |
paulb@273 | 49 | |
paulb@273 | 50 | # Satisfy visitor issues. |
paulb@273 | 51 | |
paulb@273 | 52 | self.visitor = self |
paulb@273 | 53 | |
paulb@273 | 54 | def get_code(self): |
paul@277 | 55 | return rsvp.get_merged(*self.assemblers) |
paulb@273 | 56 | |
paulb@273 | 57 | def assembler(self): |
paulb@273 | 58 | return self.current_assemblers[-1] |
paulb@273 | 59 | |
paulb@273 | 60 | def process(self, module): |
paulb@273 | 61 | |
paulb@273 | 62 | """ |
paulb@273 | 63 | Process the given 'module', creating RSVP assemblers to store code and |
paulb@273 | 64 | data. |
paulb@273 | 65 | """ |
paulb@273 | 66 | |
paulb@273 | 67 | self.module = module |
paulb@273 | 68 | |
paul@277 | 69 | main = rsvp.RSVPAssembler(self.debug) |
paulb@273 | 70 | self.current_assemblers.append(main) |
paulb@273 | 71 | |
paulb@273 | 72 | # Store constants. |
paulb@273 | 73 | |
paulb@273 | 74 | for name, const in module.simplifier.constants.items(): |
paulb@273 | 75 | main.label("const$%s$%s" % (self.module.name, name)) |
paulb@273 | 76 | main.add(const.value) |
paulb@273 | 77 | |
paulb@273 | 78 | # Reserve the namespace. |
paulb@273 | 79 | |
paulb@273 | 80 | for name in module.namespace.keys(): |
paulb@273 | 81 | main.label("global$%s$%s" % (self.module.name, name)) |
paulb@273 | 82 | main.add(None) |
paulb@273 | 83 | |
paulb@273 | 84 | # Generate subprograms. |
paulb@273 | 85 | |
paulb@273 | 86 | for subprogram in module.simplifier.subprograms: |
paulb@273 | 87 | if not subprogram.internal: |
paulb@273 | 88 | |
paulb@273 | 89 | # Generate specific subprograms. |
paulb@273 | 90 | |
paulb@273 | 91 | for specialisation in subprogram.specialisations(): |
paulb@273 | 92 | self.process_subprogram(specialisation) |
paulb@273 | 93 | |
paulb@273 | 94 | # Generate the initialisation. |
paulb@273 | 95 | |
paulb@273 | 96 | self.dispatch(module) |
paulb@273 | 97 | |
paulb@273 | 98 | # Remember the assembler used. |
paulb@273 | 99 | |
paulb@273 | 100 | self.current_assemblers.pop() |
paulb@273 | 101 | self.assemblers.append(main) |
paulb@273 | 102 | |
paulb@273 | 103 | def default(self, node, *args): |
paulb@273 | 104 | if hasattr(node, "code"): |
paulb@273 | 105 | self.dispatches(node.code) |
paulb@273 | 106 | |
paulb@273 | 107 | def visitInvoke(self, invoke): |
paulb@273 | 108 | |
paulb@273 | 109 | "Process the given 'invoke' node." |
paulb@273 | 110 | |
paulb@273 | 111 | sub = self.assembler() |
paulb@273 | 112 | |
paulb@273 | 113 | # NOTE: Generate arguments in a way compatible with subprogram |
paulb@273 | 114 | # NOTE: consumption as parameters. |
paulb@273 | 115 | |
paulb@273 | 116 | for subprogram in invoke.invocations: |
paul@277 | 117 | prepare_args = not isinstance(invoke, InvokeRef) and not invoke.share_locals |
paul@277 | 118 | if prepare_args: |
paul@277 | 119 | consumed_args = invoke.consumed_args[subprogram] |
paulb@273 | 120 | |
paul@277 | 121 | # NOTE: Generate switch table. |
paulb@276 | 122 | |
paul@277 | 123 | # Save the current frame. |
paul@277 | 124 | |
paul@277 | 125 | sub.add("SCF") |
paulb@276 | 126 | |
paul@277 | 127 | # Extend the stack for the arguments. |
paulb@276 | 128 | |
paul@277 | 129 | nparams = len(consumed_args) |
paulb@276 | 130 | sub.add("ESF", nparams) |
paulb@276 | 131 | |
paulb@276 | 132 | # For each argument, evaluate the expression. |
paulb@273 | 133 | |
paulb@276 | 134 | for arg in invoke.args: |
paulb@276 | 135 | if arg is None: |
paulb@276 | 136 | continue |
paulb@276 | 137 | |
paulb@276 | 138 | self.dispatch(arg) |
paulb@276 | 139 | |
paulb@276 | 140 | # Then, save the argument to the correct part of the arguments |
paulb@276 | 141 | # frame. |
paulb@276 | 142 | |
paulb@276 | 143 | pos = self.arg_position(consumed_args, arg) |
paulb@276 | 144 | sub.add("MVA", pos) |
paulb@276 | 145 | |
paulb@276 | 146 | # Reserve the namespace. |
paulb@276 | 147 | |
paulb@273 | 148 | sub.add("NSF", nparams) |
paulb@273 | 149 | |
paulb@276 | 150 | sub.add("JAS", "sub$%s$%s" % (subprogram.module.name, subprogram.full_name())) |
paulb@273 | 151 | |
paul@277 | 152 | if prepare_args: |
paulb@273 | 153 | sub.add("PSF") # previous stack frame |
paulb@273 | 154 | |
paulb@273 | 155 | visitInvokeFunction = visitInvoke |
paulb@273 | 156 | visitInvokeRef = visitInvoke |
paulb@273 | 157 | |
paulb@273 | 158 | def visitModule(self, module): |
paulb@273 | 159 | |
paulb@273 | 160 | "Process the given 'module'." |
paulb@273 | 161 | |
paulb@273 | 162 | self.dispatches(module.code) |
paulb@273 | 163 | |
paulb@273 | 164 | def visitSubprogram(self, subprogram): |
paulb@273 | 165 | |
paulb@273 | 166 | "Process the 'subprogram'." |
paulb@273 | 167 | |
paulb@273 | 168 | if subprogram.internal: |
paulb@273 | 169 | self.process_subprogram(subprogram) |
paulb@273 | 170 | |
paulb@273 | 171 | def process_subprogram(self, subprogram): |
paulb@273 | 172 | |
paul@277 | 173 | sub = rsvp.RSVPAssembler(self.debug) |
paulb@273 | 174 | self.current_assemblers.append(sub) |
paulb@273 | 175 | |
paulb@273 | 176 | sub.label("sub$%s$%s" % (self.module.name, subprogram.full_name())) |
paulb@273 | 177 | |
paulb@273 | 178 | locals = subprogram.namespace.keys() |
paulb@273 | 179 | params = subprogram.paramtypes.keys() |
paulb@273 | 180 | nparams = len(params) |
paulb@273 | 181 | non_params = len(locals) - nparams |
paulb@273 | 182 | |
paulb@273 | 183 | if non_params > 0: |
paulb@273 | 184 | sub.add("ESF", non_params) # extend stack frame for locals |
paulb@273 | 185 | |
paulb@273 | 186 | # Produce the standard end of subprogram return. |
paulb@273 | 187 | |
paulb@273 | 188 | sub.add("RAC") # return |
paulb@273 | 189 | |
paulb@273 | 190 | self.current_assemblers.pop() |
paulb@273 | 191 | self.assemblers.append(sub) |
paulb@273 | 192 | |
paulb@276 | 193 | def arg_position(self, consumed_args, arg): |
paulb@276 | 194 | |
paulb@276 | 195 | """ |
paulb@276 | 196 | Return, for the specified 'consumed_args', the position of 'arg' in the |
paulb@276 | 197 | list, handling keyword arguments appropriately. |
paulb@276 | 198 | """ |
paulb@276 | 199 | |
paulb@276 | 200 | if isinstance(arg, Keyword): |
paulb@276 | 201 | arg = arg.expr |
paulb@276 | 202 | return consumed_args.index(arg) |
paulb@276 | 203 | |
paulb@273 | 204 | # Convenience functions. |
paulb@273 | 205 | |
paul@279 | 206 | def get_attribute_table(importer): |
paul@279 | 207 | |
paul@279 | 208 | """ |
paul@279 | 209 | Return an attribute table for all structures known to the given 'importer'. |
paul@279 | 210 | """ |
paul@279 | 211 | |
paul@279 | 212 | master_table = {} |
paul@279 | 213 | for module in importer.get_modules(): |
paul@279 | 214 | for structure in module.simplifier.structures: |
paul@279 | 215 | classname = structure.fully_qualified_name() |
paul@279 | 216 | master_table[classname] = [] |
paul@279 | 217 | |
paul@279 | 218 | # NOTE: Assume enquiries of class attribute items produce the same |
paul@279 | 219 | # NOTE: ordering every time. |
paul@279 | 220 | |
paul@279 | 221 | positions = {} |
paul@279 | 222 | for name, (defining_cls, defs) in structure.get_visible_class_attributes().items(): |
paul@279 | 223 | first = 1 |
paul@279 | 224 | for attr in defs: |
paul@279 | 225 | if not first: |
paul@279 | 226 | print "Ambiguous attribute '%s' in '%s'." % (name, classname) |
paul@279 | 227 | break |
paul@279 | 228 | if not positions.has_key(defining_cls): |
paul@279 | 229 | positions[defining_cls] = 1 |
paul@280 | 230 | master_table[classname].append((name, (name, "A", defining_cls.fully_qualified_name(), positions[defining_cls]))) |
paul@279 | 231 | positions[defining_cls] += 1 |
paul@279 | 232 | first = 0 |
paul@279 | 233 | |
paul@279 | 234 | i = 1 |
paul@279 | 235 | for name in structure.get_instance_attribute_names(): |
paul@280 | 236 | master_table[classname].append((name, (name, "A", "I", i))) |
paul@279 | 237 | i += 1 |
paul@279 | 238 | |
paul@279 | 239 | return master_table |
paul@279 | 240 | |
paulb@273 | 241 | def generate(module): |
paulb@273 | 242 | generator = Generator() |
paulb@273 | 243 | generator.process(module) |
paulb@273 | 244 | return generator.get_code() |
paulb@273 | 245 | |
paulb@273 | 246 | # vim: tabstop=4 expandtab shiftwidth=4 |