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 | |
paulb@273 | 29 | "An RSVP code generator." |
paulb@273 | 30 | |
paul@277 | 31 | def __init__(self, debug=0): |
paulb@273 | 32 | |
paulb@273 | 33 | "Initialise the visitor." |
paulb@273 | 34 | |
paul@277 | 35 | self.debug = debug |
paulb@273 | 36 | Visitor.__init__(self) |
paulb@273 | 37 | self.assemblers = [] |
paulb@273 | 38 | self.current_assemblers = [] |
paulb@273 | 39 | |
paulb@273 | 40 | # Satisfy visitor issues. |
paulb@273 | 41 | |
paulb@273 | 42 | self.visitor = self |
paulb@273 | 43 | |
paulb@273 | 44 | def get_code(self): |
paul@277 | 45 | return rsvp.get_merged(*self.assemblers) |
paulb@273 | 46 | |
paulb@273 | 47 | def assembler(self): |
paulb@273 | 48 | return self.current_assemblers[-1] |
paulb@273 | 49 | |
paulb@273 | 50 | def process(self, module): |
paulb@273 | 51 | |
paulb@273 | 52 | """ |
paulb@273 | 53 | Process the given 'module', creating RSVP assemblers to store code and |
paulb@273 | 54 | data. |
paulb@273 | 55 | """ |
paulb@273 | 56 | |
paulb@273 | 57 | self.module = module |
paulb@273 | 58 | |
paul@277 | 59 | main = rsvp.RSVPAssembler(self.debug) |
paulb@273 | 60 | self.current_assemblers.append(main) |
paulb@273 | 61 | |
paulb@273 | 62 | # Store constants. |
paulb@273 | 63 | |
paulb@273 | 64 | for name, const in module.simplifier.constants.items(): |
paulb@273 | 65 | main.label("const$%s$%s" % (self.module.name, name)) |
paulb@273 | 66 | main.add(const.value) |
paulb@273 | 67 | |
paulb@273 | 68 | # Reserve the namespace. |
paulb@273 | 69 | |
paulb@273 | 70 | for name in module.namespace.keys(): |
paulb@273 | 71 | main.label("global$%s$%s" % (self.module.name, name)) |
paulb@273 | 72 | main.add(None) |
paulb@273 | 73 | |
paulb@273 | 74 | # Generate subprograms. |
paulb@273 | 75 | |
paulb@273 | 76 | for subprogram in module.simplifier.subprograms: |
paulb@273 | 77 | if not subprogram.internal: |
paulb@273 | 78 | |
paulb@273 | 79 | # Generate specific subprograms. |
paulb@273 | 80 | |
paulb@273 | 81 | for specialisation in subprogram.specialisations(): |
paulb@273 | 82 | self.process_subprogram(specialisation) |
paulb@273 | 83 | |
paulb@273 | 84 | # Generate the initialisation. |
paulb@273 | 85 | |
paulb@273 | 86 | self.dispatch(module) |
paulb@273 | 87 | |
paulb@273 | 88 | # Remember the assembler used. |
paulb@273 | 89 | |
paulb@273 | 90 | self.current_assemblers.pop() |
paulb@273 | 91 | self.assemblers.append(main) |
paulb@273 | 92 | |
paulb@273 | 93 | def default(self, node, *args): |
paulb@273 | 94 | if hasattr(node, "code"): |
paulb@273 | 95 | self.dispatches(node.code) |
paulb@273 | 96 | |
paulb@273 | 97 | def visitInvoke(self, invoke): |
paulb@273 | 98 | |
paulb@273 | 99 | "Process the given 'invoke' node." |
paulb@273 | 100 | |
paulb@273 | 101 | sub = self.assembler() |
paulb@273 | 102 | |
paulb@273 | 103 | # NOTE: Generate arguments in a way compatible with subprogram |
paulb@273 | 104 | # NOTE: consumption as parameters. |
paulb@273 | 105 | |
paulb@273 | 106 | for subprogram in invoke.invocations: |
paul@277 | 107 | prepare_args = not isinstance(invoke, InvokeRef) and not invoke.share_locals |
paul@277 | 108 | if prepare_args: |
paul@277 | 109 | consumed_args = invoke.consumed_args[subprogram] |
paulb@273 | 110 | |
paul@277 | 111 | # NOTE: Generate switch table. |
paulb@276 | 112 | |
paul@277 | 113 | # Save the current frame. |
paul@277 | 114 | |
paul@277 | 115 | sub.add("SCF") |
paulb@276 | 116 | |
paul@277 | 117 | # Extend the stack for the arguments. |
paulb@276 | 118 | |
paul@277 | 119 | nparams = len(consumed_args) |
paulb@276 | 120 | sub.add("ESF", nparams) |
paulb@276 | 121 | |
paulb@276 | 122 | # For each argument, evaluate the expression. |
paulb@273 | 123 | |
paulb@276 | 124 | for arg in invoke.args: |
paulb@276 | 125 | if arg is None: |
paulb@276 | 126 | continue |
paulb@276 | 127 | |
paulb@276 | 128 | self.dispatch(arg) |
paulb@276 | 129 | |
paulb@276 | 130 | # Then, save the argument to the correct part of the arguments |
paulb@276 | 131 | # frame. |
paulb@276 | 132 | |
paulb@276 | 133 | pos = self.arg_position(consumed_args, arg) |
paulb@276 | 134 | sub.add("MVA", pos) |
paulb@276 | 135 | |
paulb@276 | 136 | # Reserve the namespace. |
paulb@276 | 137 | |
paulb@273 | 138 | sub.add("NSF", nparams) |
paulb@273 | 139 | |
paulb@276 | 140 | sub.add("JAS", "sub$%s$%s" % (subprogram.module.name, subprogram.full_name())) |
paulb@273 | 141 | |
paul@277 | 142 | if prepare_args: |
paulb@273 | 143 | sub.add("PSF") # previous stack frame |
paulb@273 | 144 | |
paulb@273 | 145 | visitInvokeFunction = visitInvoke |
paulb@273 | 146 | visitInvokeRef = visitInvoke |
paulb@273 | 147 | |
paulb@273 | 148 | def visitModule(self, module): |
paulb@273 | 149 | |
paulb@273 | 150 | "Process the given 'module'." |
paulb@273 | 151 | |
paulb@273 | 152 | self.dispatches(module.code) |
paulb@273 | 153 | |
paulb@273 | 154 | def visitSubprogram(self, subprogram): |
paulb@273 | 155 | |
paulb@273 | 156 | "Process the 'subprogram'." |
paulb@273 | 157 | |
paulb@273 | 158 | if subprogram.internal: |
paulb@273 | 159 | self.process_subprogram(subprogram) |
paulb@273 | 160 | |
paulb@273 | 161 | def process_subprogram(self, subprogram): |
paulb@273 | 162 | |
paul@277 | 163 | sub = rsvp.RSVPAssembler(self.debug) |
paulb@273 | 164 | self.current_assemblers.append(sub) |
paulb@273 | 165 | |
paulb@273 | 166 | sub.label("sub$%s$%s" % (self.module.name, subprogram.full_name())) |
paulb@273 | 167 | |
paulb@273 | 168 | locals = subprogram.namespace.keys() |
paulb@273 | 169 | params = subprogram.paramtypes.keys() |
paulb@273 | 170 | nparams = len(params) |
paulb@273 | 171 | non_params = len(locals) - nparams |
paulb@273 | 172 | |
paulb@273 | 173 | if non_params > 0: |
paulb@273 | 174 | sub.add("ESF", non_params) # extend stack frame for locals |
paulb@273 | 175 | |
paulb@273 | 176 | # Produce the standard end of subprogram return. |
paulb@273 | 177 | |
paulb@273 | 178 | sub.add("RAC") # return |
paulb@273 | 179 | |
paulb@273 | 180 | self.current_assemblers.pop() |
paulb@273 | 181 | self.assemblers.append(sub) |
paulb@273 | 182 | |
paulb@276 | 183 | def arg_position(self, consumed_args, arg): |
paulb@276 | 184 | |
paulb@276 | 185 | """ |
paulb@276 | 186 | Return, for the specified 'consumed_args', the position of 'arg' in the |
paulb@276 | 187 | list, handling keyword arguments appropriately. |
paulb@276 | 188 | """ |
paulb@276 | 189 | |
paulb@276 | 190 | if isinstance(arg, Keyword): |
paulb@276 | 191 | arg = arg.expr |
paulb@276 | 192 | return consumed_args.index(arg) |
paulb@276 | 193 | |
paulb@273 | 194 | # Convenience functions. |
paulb@273 | 195 | |
paul@279 | 196 | def get_attribute_table(importer): |
paul@279 | 197 | |
paul@279 | 198 | """ |
paul@279 | 199 | Return an attribute table for all structures known to the given 'importer'. |
paul@279 | 200 | """ |
paul@279 | 201 | |
paul@279 | 202 | master_table = {} |
paul@279 | 203 | for module in importer.get_modules(): |
paul@279 | 204 | for structure in module.simplifier.structures: |
paul@279 | 205 | classname = structure.fully_qualified_name() |
paul@279 | 206 | master_table[classname] = [] |
paul@279 | 207 | |
paul@279 | 208 | # NOTE: Assume enquiries of class attribute items produce the same |
paul@279 | 209 | # NOTE: ordering every time. |
paul@279 | 210 | |
paul@279 | 211 | positions = {} |
paul@279 | 212 | for name, (defining_cls, defs) in structure.get_visible_class_attributes().items(): |
paul@279 | 213 | first = 1 |
paul@279 | 214 | for attr in defs: |
paul@279 | 215 | if not first: |
paul@279 | 216 | print "Ambiguous attribute '%s' in '%s'." % (name, classname) |
paul@279 | 217 | break |
paul@279 | 218 | if not positions.has_key(defining_cls): |
paul@279 | 219 | positions[defining_cls] = 1 |
paul@280 | 220 | master_table[classname].append((name, (name, "A", defining_cls.fully_qualified_name(), positions[defining_cls]))) |
paul@279 | 221 | positions[defining_cls] += 1 |
paul@279 | 222 | first = 0 |
paul@279 | 223 | |
paul@279 | 224 | i = 1 |
paul@279 | 225 | for name in structure.get_instance_attribute_names(): |
paul@280 | 226 | master_table[classname].append((name, (name, "A", "I", i))) |
paul@279 | 227 | i += 1 |
paul@279 | 228 | |
paul@279 | 229 | return master_table |
paul@279 | 230 | |
paulb@273 | 231 | def generate(module): |
paulb@273 | 232 | generator = Generator() |
paulb@273 | 233 | generator.process(module) |
paulb@273 | 234 | return generator.get_code() |
paulb@273 | 235 | |
paulb@273 | 236 | # vim: tabstop=4 expandtab shiftwidth=4 |