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