paul@438 | 1 | #!/usr/bin/env python |
paul@438 | 2 | |
paul@438 | 3 | """ |
paul@438 | 4 | Generate low-level code. |
paul@438 | 5 | |
paul@438 | 6 | Copyright (C) 2007, 2008, 2009, 2010, 2011 Paul Boddie <paul@boddie.org.uk> |
paul@438 | 7 | |
paul@438 | 8 | This program is free software; you can redistribute it and/or modify it under |
paul@438 | 9 | the terms of the GNU General Public License as published by the Free Software |
paul@438 | 10 | Foundation; either version 3 of the License, or (at your option) any later |
paul@438 | 11 | version. |
paul@438 | 12 | |
paul@438 | 13 | This program is distributed in the hope that it will be useful, but WITHOUT |
paul@438 | 14 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
paul@438 | 15 | FOR A PARTICULAR PURPOSE. See the GNU General Public License for more |
paul@438 | 16 | details. |
paul@438 | 17 | |
paul@438 | 18 | You should have received a copy of the GNU General Public License along with |
paul@438 | 19 | this program. If not, see <http://www.gnu.org/licenses/>. |
paul@438 | 20 | """ |
paul@438 | 21 | |
paul@438 | 22 | from micropython.opt import Optimiser |
paul@438 | 23 | from micropython.common import * |
paul@438 | 24 | from micropython.data import * |
paul@438 | 25 | from micropython.rsvp import * |
paul@438 | 26 | import compiler.ast |
paul@438 | 27 | |
paul@438 | 28 | class Assembler: |
paul@438 | 29 | |
paul@438 | 30 | "Support for generating low-level code." |
paul@438 | 31 | |
paul@438 | 32 | def __init__(self, optimisations): |
paul@438 | 33 | |
paul@438 | 34 | "Initialise the assembler with an optimiser and status attributes." |
paul@438 | 35 | |
paul@438 | 36 | # Optimisation. |
paul@438 | 37 | |
paul@438 | 38 | self.optimiser = Optimiser(self, optimisations) |
paul@438 | 39 | |
paul@438 | 40 | # The current unit being translated. |
paul@438 | 41 | |
paul@438 | 42 | self.unit = None |
paul@438 | 43 | |
paul@438 | 44 | # The temporary storage used by the current assignment expression. |
paul@438 | 45 | |
paul@438 | 46 | self.expr_temp = [] |
paul@438 | 47 | |
paul@438 | 48 | # Wiring within the code. |
paul@438 | 49 | |
paul@438 | 50 | self.labels = {} |
paul@438 | 51 | self.label_number = 0 |
paul@438 | 52 | self.loop_blocks = [] |
paul@438 | 53 | self.exception_blocks = [] |
paul@438 | 54 | |
paul@438 | 55 | def reset(self): |
paul@438 | 56 | |
paul@438 | 57 | "Reset the state of the assembler." |
paul@438 | 58 | |
paul@438 | 59 | # The code itself. This is limited to the code for a particular block |
paul@438 | 60 | # being processed. |
paul@438 | 61 | |
paul@438 | 62 | self.blocks = [] |
paul@438 | 63 | |
paul@438 | 64 | # Information about temporary values. |
paul@438 | 65 | |
paul@438 | 66 | self.temp_positions = set() |
paul@438 | 67 | self.max_temp_position = -1 |
paul@438 | 68 | |
paul@438 | 69 | # Information about instructions which construct frames. |
paul@438 | 70 | |
paul@438 | 71 | self.frame_makers = [] |
paul@438 | 72 | |
paul@438 | 73 | # Optimiser state must be reset for each unit. |
paul@438 | 74 | |
paul@438 | 75 | self.optimiser.reset() |
paul@438 | 76 | |
paul@438 | 77 | def new_block(self): |
paul@438 | 78 | |
paul@438 | 79 | "Return a new code block." |
paul@438 | 80 | |
paul@438 | 81 | return Block() |
paul@438 | 82 | |
paul@438 | 83 | def set_block(self, block): |
paul@438 | 84 | |
paul@438 | 85 | "Add the given 'block' to the unit's list of blocks." |
paul@438 | 86 | |
paul@438 | 87 | self.optimiser.reset() |
paul@438 | 88 | self.blocks.append(block) |
paul@438 | 89 | |
paul@438 | 90 | def get_loop_blocks(self): |
paul@438 | 91 | return self.loop_blocks[-1] |
paul@438 | 92 | |
paul@438 | 93 | def add_loop_blocks(self, next_block, exit_block): |
paul@438 | 94 | self.loop_blocks.append((next_block, exit_block)) |
paul@438 | 95 | |
paul@438 | 96 | def drop_loop_blocks(self): |
paul@438 | 97 | self.loop_blocks.pop() |
paul@438 | 98 | |
paul@438 | 99 | def add_exception_unit(self): |
paul@438 | 100 | self.exception_blocks.append([]) |
paul@438 | 101 | |
paul@438 | 102 | def get_exception_blocks(self): |
paul@438 | 103 | return self.exception_blocks[-1][-1] |
paul@438 | 104 | |
paul@438 | 105 | def add_exception_blocks(self, handler_block, exit_block): |
paul@438 | 106 | self.exception_blocks[-1].append((handler_block, exit_block)) |
paul@438 | 107 | |
paul@438 | 108 | def drop_exception_blocks(self): |
paul@438 | 109 | self.exception_blocks[-1].pop() |
paul@438 | 110 | |
paul@438 | 111 | def drop_exception_unit(self): |
paul@438 | 112 | self.exception_blocks.pop() |
paul@438 | 113 | |
paul@438 | 114 | # Assignment expression values. |
paul@438 | 115 | |
paul@438 | 116 | def record_value(self, immediate=1): |
paul@438 | 117 | |
paul@438 | 118 | """ |
paul@438 | 119 | Record the current active value for an assignment. If the optional |
paul@438 | 120 | 'immediate' parameter if set to a false value always allocates new |
paul@438 | 121 | temporary storage to hold the recorded value; otherwise, the |
paul@438 | 122 | value-providing instruction may be replicated in order to provide the |
paul@438 | 123 | active value later on. |
paul@438 | 124 | """ |
paul@438 | 125 | |
paul@438 | 126 | if immediate: |
paul@438 | 127 | temp = self.optimiser.optimise_temp_storage() |
paul@438 | 128 | else: |
paul@438 | 129 | temp = self.get_temp() |
paul@438 | 130 | self.expr_temp.append(temp) |
paul@438 | 131 | |
paul@438 | 132 | def discard_value(self): |
paul@438 | 133 | |
paul@438 | 134 | "Discard any temporary storage in use for the current assignment value." |
paul@438 | 135 | |
paul@438 | 136 | self.discard_temp(self.expr_temp.pop()) |
paul@438 | 137 | |
paul@438 | 138 | def set_source(self): |
paul@438 | 139 | |
paul@438 | 140 | """ |
paul@438 | 141 | Set the source of an assignment using the current assignment value. This |
paul@438 | 142 | sets the source input for the current instruction. |
paul@438 | 143 | """ |
paul@438 | 144 | |
paul@438 | 145 | self.optimiser.set_source(self.expr_temp[-1]) |
paul@438 | 146 | |
paul@438 | 147 | # Optimise away constant storage if appropriate. |
paul@438 | 148 | |
paul@438 | 149 | if self.optimiser.optimise_constant_storage(): |
paul@438 | 150 | self.remove_op() |
paul@438 | 151 | |
paul@438 | 152 | def is_immediate_user(self, node): |
paul@438 | 153 | |
paul@438 | 154 | """ |
paul@438 | 155 | Return whether 'node' is an immediate user of an assignment expression. |
paul@438 | 156 | """ |
paul@438 | 157 | |
paul@438 | 158 | return isinstance(node, (compiler.ast.AssName, compiler.ast.AssAttr)) |
paul@438 | 159 | |
paul@438 | 160 | def has_immediate_usage(self, nodes): |
paul@438 | 161 | |
paul@438 | 162 | """ |
paul@438 | 163 | Return whether 'nodes' are all immediate users of an assignment expression. |
paul@438 | 164 | """ |
paul@438 | 165 | |
paul@438 | 166 | for n in nodes: |
paul@438 | 167 | if not self.is_immediate_user(n): |
paul@438 | 168 | return 0 |
paul@438 | 169 | return 1 |
paul@438 | 170 | |
paul@438 | 171 | # Temporary storage administration. |
paul@438 | 172 | |
paul@438 | 173 | def get_temp(self): |
paul@438 | 174 | |
paul@438 | 175 | """ |
paul@438 | 176 | Add a temporary storage instruction for the current value and return a |
paul@438 | 177 | sequence of access instructions. |
paul@438 | 178 | """ |
paul@438 | 179 | |
paul@438 | 180 | position_in_frame = self.reserve_temp() |
paul@438 | 181 | self.new_op(StoreTemp(position_in_frame)) |
paul@438 | 182 | return LoadTemp(position_in_frame) |
paul@438 | 183 | |
paul@438 | 184 | def reserve_temp(self, temp_position=None): |
paul@438 | 185 | |
paul@438 | 186 | """ |
paul@438 | 187 | Reserve a new temporary storage position, or if the optional |
paul@438 | 188 | 'temp_position' is specified, ensure that this particular position is |
paul@438 | 189 | reserved. |
paul@438 | 190 | """ |
paul@438 | 191 | |
paul@438 | 192 | if temp_position is not None: |
paul@438 | 193 | pass |
paul@438 | 194 | elif not self.temp_positions: |
paul@438 | 195 | temp_position = 0 |
paul@438 | 196 | else: |
paul@438 | 197 | temp_position = max(self.temp_positions) + 1 |
paul@438 | 198 | |
paul@438 | 199 | self.temp_positions.add(temp_position) |
paul@438 | 200 | self.max_temp_position = max(self.max_temp_position, temp_position) |
paul@438 | 201 | return self.unit.all_local_usage + temp_position # position in frame |
paul@438 | 202 | |
paul@438 | 203 | def ensure_temp(self, instruction=None): |
paul@438 | 204 | |
paul@438 | 205 | """ |
paul@438 | 206 | Ensure that the 'instruction' is using a reserved temporary storage |
paul@438 | 207 | position. |
paul@438 | 208 | """ |
paul@438 | 209 | |
paul@438 | 210 | if isinstance(instruction, LoadTemp): |
paul@438 | 211 | temp_position = instruction.attr - self.unit.all_local_usage |
paul@438 | 212 | self.reserve_temp(temp_position) |
paul@438 | 213 | |
paul@438 | 214 | def discard_temp(self, instruction=None): |
paul@438 | 215 | |
paul@438 | 216 | "Discard any temporary storage position used by 'instruction'." |
paul@438 | 217 | |
paul@438 | 218 | if isinstance(instruction, LoadTemp): |
paul@438 | 219 | temp_position = instruction.attr - self.unit.all_local_usage |
paul@438 | 220 | self.free_temp(temp_position) |
paul@438 | 221 | |
paul@438 | 222 | def free_temp(self, temp_position): |
paul@438 | 223 | |
paul@438 | 224 | "Free the temporary storage position specified by 'temp_position'." |
paul@438 | 225 | |
paul@438 | 226 | if temp_position in self.temp_positions: |
paul@438 | 227 | self.temp_positions.remove(temp_position) |
paul@438 | 228 | |
paul@438 | 229 | def set_frame_usage(self, node, extend): |
paul@438 | 230 | |
paul@438 | 231 | """ |
paul@438 | 232 | Ensure that the frame usage for the unit associated with 'node' is set |
paul@438 | 233 | on the 'extend' instruction. |
paul@438 | 234 | """ |
paul@438 | 235 | |
paul@438 | 236 | # Remove any ExtendFrame instructions which do nothing. |
paul@438 | 237 | |
paul@438 | 238 | if self.last_op() is extend: |
paul@438 | 239 | self.remove_op() |
paul@438 | 240 | return |
paul@438 | 241 | |
paul@438 | 242 | ntemp = self.max_temp_position + 1 |
paul@438 | 243 | extend.attr = ntemp + node.unit.local_usage # NOTE: See get_code for similar code. |
paul@438 | 244 | |
paul@438 | 245 | # Code writing methods. |
paul@438 | 246 | |
paul@438 | 247 | def new_op(self, op): |
paul@438 | 248 | |
paul@438 | 249 | """ |
paul@438 | 250 | Add 'op' to the generated code, returning a true value if an instruction |
paul@438 | 251 | was added. |
paul@438 | 252 | """ |
paul@438 | 253 | |
paul@438 | 254 | # Optimise load operations employed by this instruction. |
paul@438 | 255 | |
paul@438 | 256 | self.optimiser.optimise_load_operations(op) |
paul@438 | 257 | if self.optimiser.optimise_away_no_operations(op) or self.optimiser.optimise_unused_handlers(op): |
paul@438 | 258 | return 0 |
paul@438 | 259 | |
paul@438 | 260 | # Add the operation to the current block. |
paul@438 | 261 | |
paul@438 | 262 | self.blocks[-1].code.append(op) |
paul@438 | 263 | self.optimiser.set_new(op) |
paul@438 | 264 | return 1 |
paul@438 | 265 | |
paul@438 | 266 | def remove_op(self): |
paul@438 | 267 | |
paul@438 | 268 | "Remove the last instruction." |
paul@438 | 269 | |
paul@438 | 270 | op = self.blocks[-1].code.pop() |
paul@438 | 271 | self.optimiser.clear_active() |
paul@438 | 272 | |
paul@438 | 273 | def replace_op(self, op): |
paul@438 | 274 | |
paul@438 | 275 | "Replace the last added instruction with 'op'." |
paul@438 | 276 | |
paul@438 | 277 | self.remove_op() |
paul@438 | 278 | self.new_op(op) |
paul@438 | 279 | |
paul@438 | 280 | def replace_active_value(self, op): |
paul@438 | 281 | |
paul@438 | 282 | """ |
paul@438 | 283 | Replace the value-providing active instruction with 'op' if appropriate. |
paul@438 | 284 | """ |
paul@438 | 285 | |
paul@438 | 286 | self.optimiser.remove_active_value() |
paul@438 | 287 | self.new_op(op) |
paul@438 | 288 | |
paul@438 | 289 | def last_op(self): |
paul@438 | 290 | |
paul@438 | 291 | "Return the last added instruction." |
paul@438 | 292 | |
paul@438 | 293 | try: |
paul@438 | 294 | return self.blocks[-1].code[-1] |
paul@438 | 295 | except IndexError: |
paul@438 | 296 | return None |
paul@438 | 297 | |
paul@438 | 298 | # vim: tabstop=4 expandtab shiftwidth=4 |