|
| 1 | +/* SPIM S20 MIPS simulator. |
| 2 | + Code to manipulate data segment directives. |
| 3 | +
|
| 4 | + Copyright (c) 1990-2010, James R. Larus. |
| 5 | + All rights reserved. |
| 6 | +
|
| 7 | + Redistribution and use in source and binary forms, with or without modification, |
| 8 | + are permitted provided that the following conditions are met: |
| 9 | +
|
| 10 | + Redistributions of source code must retain the above copyright notice, |
| 11 | + this list of conditions and the following disclaimer. |
| 12 | +
|
| 13 | + Redistributions in binary form must reproduce the above copyright notice, |
| 14 | + this list of conditions and the following disclaimer in the documentation and/or |
| 15 | + other materials provided with the distribution. |
| 16 | +
|
| 17 | + Neither the name of the James R. Larus nor the names of its contributors may be |
| 18 | + used to endorse or promote products derived from this software without specific |
| 19 | + prior written permission. |
| 20 | +
|
| 21 | + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 22 | + AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 23 | + IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 24 | + ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE |
| 25 | + LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 26 | + CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE |
| 27 | + GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 28 | + HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| 29 | + LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
| 30 | + OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 31 | +*/ |
| 32 | + |
| 33 | + |
| 34 | +#include "spim.h" |
| 35 | +#include "string-stream.h" |
| 36 | +#include "spim-utils.h" |
| 37 | +#include "inst.h" |
| 38 | +#include "reg.h" |
| 39 | +#include "mem.h" |
| 40 | +#include "sym-tbl.h" |
| 41 | +#include "parser.h" |
| 42 | +#include "run.h" |
| 43 | +#include "data.h" |
| 44 | + |
| 45 | + |
| 46 | +/* The first 64K of the data segment are dedicated to small data |
| 47 | + segment, which is pointed to by $gp. This register points to the |
| 48 | + middle of the segment, so we can use the full offset field in an |
| 49 | + instruction. */ |
| 50 | + |
| 51 | +static mem_addr next_data_pc; /* Location for next datum in user process */ |
| 52 | + |
| 53 | +static mem_addr next_k_data_pc; /* Location for next datum in kernel */ |
| 54 | + |
| 55 | +static bool in_kernel = 0; /* => data goes to kdata, not data */ |
| 56 | + |
| 57 | +#define DATA_PC (in_kernel ? next_k_data_pc : next_data_pc) |
| 58 | + |
| 59 | +static mem_addr next_gp_item_addr; /* Address of next item accessed off $gp */ |
| 60 | + |
| 61 | +static bool auto_alignment = true; /* => align literal to natural bound*/ |
| 62 | + |
| 63 | + |
| 64 | + |
| 65 | +/* If TO_KERNEL is true, subsequent data will be placed in the |
| 66 | + kernel data segment. If false, data will go to the user's data |
| 67 | + segment.*/ |
| 68 | + |
| 69 | +void |
| 70 | +user_kernel_data_segment (bool to_kernel) |
| 71 | +{ |
| 72 | + in_kernel = to_kernel; |
| 73 | +} |
| 74 | + |
| 75 | + |
| 76 | +void |
| 77 | +end_of_assembly_file () |
| 78 | +{ |
| 79 | + in_kernel = false; |
| 80 | + auto_alignment = true; |
| 81 | +} |
| 82 | + |
| 83 | + |
| 84 | +/* Set the point at which the first datum is stored to be ADDRESS + |
| 85 | + 64K. The 64K increment allocates an area pointed to by register |
| 86 | + $gp, which is initialized. */ |
| 87 | + |
| 88 | +void |
| 89 | +data_begins_at_point (mem_addr addr) |
| 90 | +{ |
| 91 | + if (bare_machine) |
| 92 | + next_data_pc = addr; |
| 93 | + else |
| 94 | + { |
| 95 | + next_gp_item_addr = addr; |
| 96 | + gp_midpoint = addr + 32*K; |
| 97 | + R[REG_GP] = gp_midpoint; |
| 98 | + next_data_pc = addr + 64 * K; |
| 99 | + } |
| 100 | +} |
| 101 | + |
| 102 | + |
| 103 | +/* Set the point at which the first datum is stored in the kernel's |
| 104 | + data segment. */ |
| 105 | + |
| 106 | +void |
| 107 | +k_data_begins_at_point (mem_addr addr) |
| 108 | +{ |
| 109 | + next_k_data_pc = addr; |
| 110 | +} |
| 111 | + |
| 112 | + |
| 113 | +/* Arrange that the next datum is stored on a memory boundary with its |
| 114 | + low ALIGNMENT bits equal to 0. If argument is 0, disable automatic |
| 115 | + alignment.*/ |
| 116 | + |
| 117 | +void |
| 118 | +align_data (int alignment) |
| 119 | +{ |
| 120 | + if (alignment == 0) |
| 121 | + auto_alignment = false; |
| 122 | + else if (in_kernel) |
| 123 | + { |
| 124 | + next_k_data_pc = |
| 125 | + (next_k_data_pc + (1 << alignment) - 1) & (-1 << alignment); |
| 126 | + fix_current_label_address (next_k_data_pc); |
| 127 | + } |
| 128 | + else |
| 129 | + { |
| 130 | + next_data_pc = (next_data_pc + (1 << alignment) - 1) & (-1 << alignment); |
| 131 | + fix_current_label_address (next_data_pc); |
| 132 | + } |
| 133 | +} |
| 134 | + |
| 135 | + |
| 136 | +void |
| 137 | +set_data_alignment (int alignment) |
| 138 | +{ |
| 139 | + if (auto_alignment) |
| 140 | + align_data (alignment); |
| 141 | +} |
| 142 | + |
| 143 | + |
| 144 | +void |
| 145 | +enable_data_alignment () |
| 146 | +{ |
| 147 | + auto_alignment = true; |
| 148 | +} |
| 149 | + |
| 150 | + |
| 151 | +/* Set the location (in user or kernel data space) for the next datum. */ |
| 152 | + |
| 153 | +void |
| 154 | +set_data_pc (mem_addr addr) |
| 155 | +{ |
| 156 | + if (in_kernel) |
| 157 | + next_k_data_pc = addr; |
| 158 | + else |
| 159 | + next_data_pc = addr; |
| 160 | +} |
| 161 | + |
| 162 | + |
| 163 | +/* Return the address at which the next datum will be stored. */ |
| 164 | + |
| 165 | +mem_addr |
| 166 | +current_data_pc () |
| 167 | +{ |
| 168 | + return (DATA_PC); |
| 169 | +} |
| 170 | + |
| 171 | + |
| 172 | +/* Bump the address at which the next data will be stored by DELTA |
| 173 | + bytes. */ |
| 174 | + |
| 175 | +void |
| 176 | +increment_data_pc (int delta) |
| 177 | +{ |
| 178 | + if (in_kernel) |
| 179 | + { |
| 180 | + next_k_data_pc += delta; |
| 181 | + if (k_data_top <= next_k_data_pc) |
| 182 | + expand_k_data(ROUND_UP(next_k_data_pc - k_data_top + 1, 64*K)); |
| 183 | + } |
| 184 | + else |
| 185 | + { |
| 186 | + next_data_pc += delta; |
| 187 | + if (data_top <= next_data_pc) |
| 188 | + expand_data(ROUND_UP(next_data_pc - data_top + 1, 64*K)); |
| 189 | + } |
| 190 | +} |
| 191 | + |
| 192 | + |
| 193 | +/* Process a .extern NAME SIZE directive. */ |
| 194 | + |
| 195 | +void |
| 196 | +extern_directive (char *name, int size) |
| 197 | +{ |
| 198 | + label *sym = make_label_global (name); |
| 199 | + |
| 200 | + if (!bare_machine |
| 201 | + && !sym->gp_flag // Not already a global symbol |
| 202 | + && size > 0 && size <= SMALL_DATA_SEG_MAX_SIZE |
| 203 | + && next_gp_item_addr + size < gp_midpoint + 32*K) |
| 204 | + { |
| 205 | + sym->gp_flag = 1; |
| 206 | + sym->addr = next_gp_item_addr; |
| 207 | + next_gp_item_addr += size; |
| 208 | + } |
| 209 | +} |
| 210 | + |
| 211 | + |
| 212 | +/* Process a .lcomm NAME SIZE directive. */ |
| 213 | + |
| 214 | +void |
| 215 | +lcomm_directive (char *name, int size) |
| 216 | +{ |
| 217 | + if (!bare_machine |
| 218 | + && size > 0 && size <= SMALL_DATA_SEG_MAX_SIZE |
| 219 | + && next_gp_item_addr + size < gp_midpoint + 32*K) |
| 220 | + { |
| 221 | + label *sym = record_label (name, next_gp_item_addr, 1); |
| 222 | + sym->gp_flag = 1; |
| 223 | + |
| 224 | + next_gp_item_addr += size; |
| 225 | + /* Don't need to initialize since memory starts with 0's */ |
| 226 | + } |
| 227 | + else |
| 228 | + { |
| 229 | + (void)record_label (name, next_data_pc, 1); |
| 230 | + |
| 231 | + for ( ; size > 0; size --) |
| 232 | + { |
| 233 | + store_byte (0); |
| 234 | + } |
| 235 | + } |
| 236 | +} |
| 237 | + |
| 238 | + |
| 239 | +/* Process a .ascii STRING or .asciiz STRING directive. */ |
| 240 | + |
| 241 | +void |
| 242 | +store_string (char *string, int length, bool null_terminate) |
| 243 | +{ |
| 244 | + for ( ; length > 0; string ++, length --) { |
| 245 | + store_byte (*string); |
| 246 | + } |
| 247 | + if (null_terminate) |
| 248 | + { |
| 249 | + store_byte (0); |
| 250 | + } |
| 251 | +} |
| 252 | + |
| 253 | + |
| 254 | +/* Process a .byte EXPR directive. */ |
| 255 | + |
| 256 | +void |
| 257 | +store_byte (int value) |
| 258 | +{ |
| 259 | + set_mem_byte (DATA_PC, value); |
| 260 | + increment_data_pc (1); |
| 261 | +} |
| 262 | + |
| 263 | + |
| 264 | +/* Process a .half EXPR directive. */ |
| 265 | + |
| 266 | +void |
| 267 | +store_half (int value) |
| 268 | +{ |
| 269 | + if ((DATA_PC & 0x1) != 0) |
| 270 | + { |
| 271 | +#ifdef SPIM_BIGENDIAN |
| 272 | + store_byte ((value >> 8) & 0xff); |
| 273 | + store_byte (value & 0xff); |
| 274 | +#else |
| 275 | + store_byte (value & 0xff); |
| 276 | + store_byte ((value >> 8) & 0xff); |
| 277 | +#endif |
| 278 | + } |
| 279 | + else |
| 280 | + { |
| 281 | + set_mem_half (DATA_PC, value); |
| 282 | + increment_data_pc (BYTES_PER_WORD / 2); |
| 283 | + } |
| 284 | +} |
| 285 | + |
| 286 | + |
| 287 | +/* Process a .word EXPR directive. */ |
| 288 | + |
| 289 | +void |
| 290 | +store_word (int value) |
| 291 | +{ |
| 292 | + if ((DATA_PC & 0x3) != 0) |
| 293 | + { |
| 294 | +#ifdef SPIM_BIGENDIAN |
| 295 | + store_half ((value >> 16) & 0xffff); |
| 296 | + store_half (value & 0xffff); |
| 297 | +#else |
| 298 | + store_half (value & 0xffff); |
| 299 | + store_half ((value >> 16) & 0xffff); |
| 300 | +#endif |
| 301 | + } |
| 302 | + else |
| 303 | + { |
| 304 | + set_mem_word (DATA_PC, value); |
| 305 | + increment_data_pc (BYTES_PER_WORD); |
| 306 | + } |
| 307 | +} |
| 308 | + |
| 309 | + |
| 310 | +/* Process a .double EXPR directive. */ |
| 311 | + |
| 312 | +void |
| 313 | +store_double (double *value) |
| 314 | +{ |
| 315 | + if ((DATA_PC & 0x7) != 0) |
| 316 | + { |
| 317 | + store_word (* ((mem_word *) value)); |
| 318 | + store_word (* (((mem_word *) value) + 1)); |
| 319 | + } |
| 320 | + else |
| 321 | + { |
| 322 | + set_mem_word (DATA_PC, *((mem_word *) value)); |
| 323 | + increment_data_pc (BYTES_PER_WORD); |
| 324 | + set_mem_word (DATA_PC, *(((mem_word *) value) + 1)); |
| 325 | + increment_data_pc (BYTES_PER_WORD); |
| 326 | + } |
| 327 | +} |
| 328 | + |
| 329 | + |
| 330 | +/* Process a .float EXPR directive. */ |
| 331 | + |
| 332 | +void |
| 333 | +store_float (double *value) |
| 334 | +{ |
| 335 | + float val = (float)*value; |
| 336 | + float *vp = &val; |
| 337 | + |
| 338 | + if ((DATA_PC & 0x3) != 0) |
| 339 | + { |
| 340 | + store_half (*(mem_word *) vp & 0xffff); |
| 341 | + store_half ((*(mem_word *) vp >> 16) & 0xffff); |
| 342 | + } |
| 343 | + else |
| 344 | + { |
| 345 | + set_mem_word (DATA_PC, *((mem_word *) vp)); |
| 346 | + increment_data_pc (BYTES_PER_WORD); |
| 347 | + } |
| 348 | +} |
0 commit comments