| 1 | /* Target-dependent code for the Matsushita MN10300 for GDB, the GNU debugger. |
| 2 | Copyright 1996, 1997, 1998 Free Software Foundation, Inc. |
| 3 | |
| 4 | This file is part of GDB. |
| 5 | |
| 6 | This program is free software; you can redistribute it and/or modify |
| 7 | it under the terms of the GNU General Public License as published by |
| 8 | the Free Software Foundation; either version 2 of the License, or |
| 9 | (at your option) any later version. |
| 10 | |
| 11 | This program is distributed in the hope that it will be useful, |
| 12 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | GNU General Public License for more details. |
| 15 | |
| 16 | You should have received a copy of the GNU General Public License |
| 17 | along with this program; if not, write to the Free Software |
| 18 | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ |
| 19 | |
| 20 | #include "defs.h" |
| 21 | #include "frame.h" |
| 22 | #include "inferior.h" |
| 23 | #include "obstack.h" |
| 24 | #include "target.h" |
| 25 | #include "value.h" |
| 26 | #include "bfd.h" |
| 27 | #include "gdb_string.h" |
| 28 | #include "gdbcore.h" |
| 29 | #include "symfile.h" |
| 30 | |
| 31 | static char *mn10300_generic_register_names[] = |
| 32 | { "d0", "d1", "d2", "d3", "a0", "a1", "a2", "a3", |
| 33 | "sp", "pc", "mdr", "psw", "lir", "lar", "", "", |
| 34 | "", "", "", "", "", "", "", "", |
| 35 | "", "", "", "", "", "", "", "fp" }; |
| 36 | |
| 37 | char **mn10300_register_names = mn10300_generic_register_names; |
| 38 | /* start-sanitize-am33 */ |
| 39 | static char *am33_register_names [] = |
| 40 | { "d0", "d1", "d2", "d3", "a0", "a1", "a2", "a3", |
| 41 | "sp", "pc", "mdr", "psw", "lir", "lar", "", |
| 42 | "r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7", |
| 43 | "ssp", "msp", "usp", "mcrh", "mcrl", "mcvf", "", "", ""}; |
| 44 | static int am33_mode; |
| 45 | /* end-sanitize-am33 */ |
| 46 | |
| 47 | static CORE_ADDR mn10300_analyze_prologue PARAMS ((struct frame_info *fi, |
| 48 | CORE_ADDR pc)); |
| 49 | |
| 50 | /* Values for frame_info.status */ |
| 51 | |
| 52 | #define MY_FRAME_IN_SP 0x1 |
| 53 | #define MY_FRAME_IN_FP 0x2 |
| 54 | #define NO_MORE_FRAMES 0x4 |
| 55 | |
| 56 | |
| 57 | /* Should call_function allocate stack space for a struct return? */ |
| 58 | int |
| 59 | mn10300_use_struct_convention (gcc_p, type) |
| 60 | int gcc_p; |
| 61 | struct type *type; |
| 62 | { |
| 63 | return (TYPE_NFIELDS (type) > 1 || TYPE_LENGTH (type) > 8); |
| 64 | } |
| 65 | |
| 66 | /* The breakpoint instruction must be the same size as the smallest |
| 67 | instruction in the instruction set. |
| 68 | |
| 69 | The Matsushita mn10x00 processors have single byte instructions |
| 70 | so we need a single byte breakpoint. Matsushita hasn't defined |
| 71 | one, so we defined it ourselves. */ |
| 72 | |
| 73 | unsigned char * |
| 74 | mn10300_breakpoint_from_pc (bp_addr, bp_size) |
| 75 | CORE_ADDR *bp_addr; |
| 76 | int *bp_size; |
| 77 | { |
| 78 | static char breakpoint[] = {0xff}; |
| 79 | *bp_size = 1; |
| 80 | return breakpoint; |
| 81 | } |
| 82 | |
| 83 | |
| 84 | /* Fix fi->frame if it's bogus at this point. This is a helper |
| 85 | function for mn10300_analyze_prologue. */ |
| 86 | |
| 87 | static void |
| 88 | fix_frame_pointer (fi, stack_size) |
| 89 | struct frame_info *fi; |
| 90 | int stack_size; |
| 91 | { |
| 92 | if (fi && fi->next == NULL) |
| 93 | { |
| 94 | if (fi->status & MY_FRAME_IN_SP) |
| 95 | fi->frame = read_sp () - stack_size; |
| 96 | else if (fi->status & MY_FRAME_IN_FP) |
| 97 | fi->frame = read_register (A3_REGNUM); |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | |
| 102 | /* Set offsets of registers saved by movm instruction. |
| 103 | This is a helper function for mn10300_analyze_prologue. */ |
| 104 | |
| 105 | static void |
| 106 | set_movm_offsets (fi, movm_args) |
| 107 | struct frame_info *fi; |
| 108 | int movm_args; |
| 109 | { |
| 110 | int offset = 0; |
| 111 | |
| 112 | if (fi == NULL || movm_args == 0) |
| 113 | return; |
| 114 | |
| 115 | if (movm_args & 0x10) |
| 116 | { |
| 117 | fi->fsr.regs[A3_REGNUM] = fi->frame + offset; |
| 118 | offset += 4; |
| 119 | } |
| 120 | if (movm_args & 0x20) |
| 121 | { |
| 122 | fi->fsr.regs[A2_REGNUM] = fi->frame + offset; |
| 123 | offset += 4; |
| 124 | } |
| 125 | if (movm_args & 0x40) |
| 126 | { |
| 127 | fi->fsr.regs[D3_REGNUM] = fi->frame + offset; |
| 128 | offset += 4; |
| 129 | } |
| 130 | if (movm_args & 0x80) |
| 131 | { |
| 132 | fi->fsr.regs[D2_REGNUM] = fi->frame + offset; |
| 133 | offset += 4; |
| 134 | } |
| 135 | /* start-sanitize-am33 */ |
| 136 | if (am33_mode && movm_args & 0x02) |
| 137 | { |
| 138 | fi->fsr.regs[E0_REGNUM+5] = fi->frame + offset; |
| 139 | fi->fsr.regs[E0_REGNUM+4] = fi->frame + offset + 4; |
| 140 | fi->fsr.regs[E0_REGNUM+3] = fi->frame + offset + 8; |
| 141 | fi->fsr.regs[E0_REGNUM+2] = fi->frame + offset + 12; |
| 142 | } |
| 143 | /* end-sanitize-am33 */ |
| 144 | } |
| 145 | |
| 146 | |
| 147 | /* The main purpose of this file is dealing with prologues to extract |
| 148 | information about stack frames and saved registers. |
| 149 | |
| 150 | For reference here's how prologues look on the mn10300: |
| 151 | |
| 152 | With frame pointer: |
| 153 | movm [d2,d3,a2,a3],sp |
| 154 | mov sp,a3 |
| 155 | add <size>,sp |
| 156 | |
| 157 | Without frame pointer: |
| 158 | movm [d2,d3,a2,a3],sp (if needed) |
| 159 | add <size>,sp |
| 160 | |
| 161 | One day we might keep the stack pointer constant, that won't |
| 162 | change the code for prologues, but it will make the frame |
| 163 | pointerless case much more common. */ |
| 164 | |
| 165 | /* Analyze the prologue to determine where registers are saved, |
| 166 | the end of the prologue, etc etc. Return the end of the prologue |
| 167 | scanned. |
| 168 | |
| 169 | We store into FI (if non-null) several tidbits of information: |
| 170 | |
| 171 | * stack_size -- size of this stack frame. Note that if we stop in |
| 172 | certain parts of the prologue/epilogue we may claim the size of the |
| 173 | current frame is zero. This happens when the current frame has |
| 174 | not been allocated yet or has already been deallocated. |
| 175 | |
| 176 | * fsr -- Addresses of registers saved in the stack by this frame. |
| 177 | |
| 178 | * status -- A (relatively) generic status indicator. It's a bitmask |
| 179 | with the following bits: |
| 180 | |
| 181 | MY_FRAME_IN_SP: The base of the current frame is actually in |
| 182 | the stack pointer. This can happen for frame pointerless |
| 183 | functions, or cases where we're stopped in the prologue/epilogue |
| 184 | itself. For these cases mn10300_analyze_prologue will need up |
| 185 | update fi->frame before returning or analyzing the register |
| 186 | save instructions. |
| 187 | |
| 188 | MY_FRAME_IN_FP: The base of the current frame is in the |
| 189 | frame pointer register ($a2). |
| 190 | |
| 191 | NO_MORE_FRAMES: Set this if the current frame is "start" or |
| 192 | if the first instruction looks like mov <imm>,sp. This tells |
| 193 | frame chain to not bother trying to unwind past this frame. */ |
| 194 | |
| 195 | static CORE_ADDR |
| 196 | mn10300_analyze_prologue (fi, pc) |
| 197 | struct frame_info *fi; |
| 198 | CORE_ADDR pc; |
| 199 | { |
| 200 | CORE_ADDR func_addr, func_end, addr, stop; |
| 201 | CORE_ADDR stack_size; |
| 202 | int imm_size; |
| 203 | unsigned char buf[4]; |
| 204 | int status, movm_args = 0; |
| 205 | char *name; |
| 206 | |
| 207 | /* Use the PC in the frame if it's provided to look up the |
| 208 | start of this function. */ |
| 209 | pc = (fi ? fi->pc : pc); |
| 210 | |
| 211 | /* Find the start of this function. */ |
| 212 | status = find_pc_partial_function (pc, &name, &func_addr, &func_end); |
| 213 | |
| 214 | /* Do nothing if we couldn't find the start of this function or if we're |
| 215 | stopped at the first instruction in the prologue. */ |
| 216 | if (status == 0) |
| 217 | return pc; |
| 218 | |
| 219 | /* If we're in start, then give up. */ |
| 220 | if (strcmp (name, "start") == 0) |
| 221 | { |
| 222 | if (fi != NULL) |
| 223 | fi->status = NO_MORE_FRAMES; |
| 224 | return pc; |
| 225 | } |
| 226 | |
| 227 | /* At the start of a function our frame is in the stack pointer. */ |
| 228 | if (fi) |
| 229 | fi->status = MY_FRAME_IN_SP; |
| 230 | |
| 231 | /* Get the next two bytes into buf, we need two because rets is a two |
| 232 | byte insn and the first isn't enough to uniquely identify it. */ |
| 233 | status = read_memory_nobpt (pc, buf, 2); |
| 234 | if (status != 0) |
| 235 | return pc; |
| 236 | |
| 237 | /* If we're physically on an "rets" instruction, then our frame has |
| 238 | already been deallocated. Note this can also be true for retf |
| 239 | and ret if they specify a size of zero. |
| 240 | |
| 241 | In this case fi->frame is bogus, we need to fix it. */ |
| 242 | if (fi && buf[0] == 0xf0 && buf[1] == 0xfc) |
| 243 | { |
| 244 | if (fi->next == NULL) |
| 245 | fi->frame = read_sp (); |
| 246 | return fi->pc; |
| 247 | } |
| 248 | |
| 249 | /* Similarly if we're stopped on the first insn of a prologue as our |
| 250 | frame hasn't been allocated yet. */ |
| 251 | if (fi && fi->pc == func_addr) |
| 252 | { |
| 253 | if (fi->next == NULL) |
| 254 | fi->frame = read_sp (); |
| 255 | return fi->pc; |
| 256 | } |
| 257 | |
| 258 | /* Figure out where to stop scanning. */ |
| 259 | stop = fi ? fi->pc : func_end; |
| 260 | |
| 261 | /* Don't walk off the end of the function. */ |
| 262 | stop = stop > func_end ? func_end : stop; |
| 263 | |
| 264 | /* Start scanning on the first instruction of this function. */ |
| 265 | addr = func_addr; |
| 266 | |
| 267 | /* Suck in two bytes. */ |
| 268 | status = read_memory_nobpt (addr, buf, 2); |
| 269 | if (status != 0) |
| 270 | { |
| 271 | fix_frame_pointer (fi, 0); |
| 272 | return addr; |
| 273 | } |
| 274 | |
| 275 | /* First see if this insn sets the stack pointer; if so, it's something |
| 276 | we won't understand, so quit now. */ |
| 277 | if (buf[0] == 0xf2 && (buf[1] & 0xf3) == 0xf0) |
| 278 | { |
| 279 | if (fi) |
| 280 | fi->status = NO_MORE_FRAMES; |
| 281 | return addr; |
| 282 | } |
| 283 | |
| 284 | /* Now look for movm [regs],sp, which saves the callee saved registers. |
| 285 | |
| 286 | At this time we don't know if fi->frame is valid, so we only note |
| 287 | that we encountered a movm instruction. Later, we'll set the entries |
| 288 | in fsr.regs as needed. */ |
| 289 | if (buf[0] == 0xcf) |
| 290 | { |
| 291 | /* Extract the register list for the movm instruction. */ |
| 292 | status = read_memory_nobpt (addr + 1, buf, 1); |
| 293 | movm_args = *buf; |
| 294 | |
| 295 | addr += 2; |
| 296 | |
| 297 | /* Quit now if we're beyond the stop point. */ |
| 298 | if (addr >= stop) |
| 299 | { |
| 300 | /* Fix fi->frame since it's bogus at this point. */ |
| 301 | if (fi && fi->next == NULL) |
| 302 | fi->frame = read_sp (); |
| 303 | |
| 304 | /* Note if/where callee saved registers were saved. */ |
| 305 | set_movm_offsets (fi, movm_args); |
| 306 | return addr; |
| 307 | } |
| 308 | |
| 309 | /* Get the next two bytes so the prologue scan can continue. */ |
| 310 | status = read_memory_nobpt (addr, buf, 2); |
| 311 | if (status != 0) |
| 312 | { |
| 313 | /* Fix fi->frame since it's bogus at this point. */ |
| 314 | if (fi && fi->next == NULL) |
| 315 | fi->frame = read_sp (); |
| 316 | |
| 317 | /* Note if/where callee saved registers were saved. */ |
| 318 | set_movm_offsets (fi, movm_args); |
| 319 | return addr; |
| 320 | } |
| 321 | } |
| 322 | |
| 323 | /* Now see if we set up a frame pointer via "mov sp,a3" */ |
| 324 | if (buf[0] == 0x3f) |
| 325 | { |
| 326 | addr += 1; |
| 327 | |
| 328 | /* The frame pointer is now valid. */ |
| 329 | if (fi) |
| 330 | { |
| 331 | fi->status |= MY_FRAME_IN_FP; |
| 332 | fi->status &= ~MY_FRAME_IN_SP; |
| 333 | } |
| 334 | |
| 335 | /* Quit now if we're beyond the stop point. */ |
| 336 | if (addr >= stop) |
| 337 | { |
| 338 | /* Fix fi->frame if it's bogus at this point. */ |
| 339 | fix_frame_pointer (fi, 0); |
| 340 | |
| 341 | /* Note if/where callee saved registers were saved. */ |
| 342 | set_movm_offsets (fi, movm_args); |
| 343 | return addr; |
| 344 | } |
| 345 | |
| 346 | /* Get two more bytes so scanning can continue. */ |
| 347 | status = read_memory_nobpt (addr, buf, 2); |
| 348 | if (status != 0) |
| 349 | { |
| 350 | /* Fix fi->frame if it's bogus at this point. */ |
| 351 | fix_frame_pointer (fi, 0); |
| 352 | |
| 353 | /* Note if/where callee saved registers were saved. */ |
| 354 | set_movm_offsets (fi, movm_args); |
| 355 | return addr; |
| 356 | } |
| 357 | } |
| 358 | |
| 359 | /* Next we should allocate the local frame. No more prologue insns |
| 360 | are found after allocating the local frame. |
| 361 | |
| 362 | Search for add imm8,sp (0xf8feXX) |
| 363 | or add imm16,sp (0xfafeXXXX) |
| 364 | or add imm32,sp (0xfcfeXXXXXXXX). |
| 365 | |
| 366 | If none of the above was found, then this prologue has no |
| 367 | additional stack. */ |
| 368 | |
| 369 | status = read_memory_nobpt (addr, buf, 2); |
| 370 | if (status != 0) |
| 371 | { |
| 372 | /* Fix fi->frame if it's bogus at this point. */ |
| 373 | fix_frame_pointer (fi, 0); |
| 374 | |
| 375 | /* Note if/where callee saved registers were saved. */ |
| 376 | set_movm_offsets (fi, movm_args); |
| 377 | return addr; |
| 378 | } |
| 379 | |
| 380 | imm_size = 0; |
| 381 | if (buf[0] == 0xf8 && buf[1] == 0xfe) |
| 382 | imm_size = 1; |
| 383 | else if (buf[0] == 0xfa && buf[1] == 0xfe) |
| 384 | imm_size = 2; |
| 385 | else if (buf[0] == 0xfc && buf[1] == 0xfe) |
| 386 | imm_size = 4; |
| 387 | |
| 388 | if (imm_size != 0) |
| 389 | { |
| 390 | /* Suck in imm_size more bytes, they'll hold the size of the |
| 391 | current frame. */ |
| 392 | status = read_memory_nobpt (addr + 2, buf, imm_size); |
| 393 | if (status != 0) |
| 394 | { |
| 395 | /* Fix fi->frame if it's bogus at this point. */ |
| 396 | fix_frame_pointer (fi, 0); |
| 397 | |
| 398 | /* Note if/where callee saved registers were saved. */ |
| 399 | set_movm_offsets (fi, movm_args); |
| 400 | return addr; |
| 401 | } |
| 402 | |
| 403 | /* Note the size of the stack in the frame info structure. */ |
| 404 | stack_size = extract_signed_integer (buf, imm_size); |
| 405 | if (fi) |
| 406 | fi->stack_size = stack_size; |
| 407 | |
| 408 | /* We just consumed 2 + imm_size bytes. */ |
| 409 | addr += 2 + imm_size; |
| 410 | |
| 411 | /* No more prologue insns follow, so begin preparation to return. */ |
| 412 | /* Fix fi->frame if it's bogus at this point. */ |
| 413 | fix_frame_pointer (fi, stack_size); |
| 414 | |
| 415 | /* Note if/where callee saved registers were saved. */ |
| 416 | set_movm_offsets (fi, movm_args); |
| 417 | return addr; |
| 418 | } |
| 419 | |
| 420 | /* We never found an insn which allocates local stack space, regardless |
| 421 | this is the end of the prologue. */ |
| 422 | /* Fix fi->frame if it's bogus at this point. */ |
| 423 | fix_frame_pointer (fi, 0); |
| 424 | |
| 425 | /* Note if/where callee saved registers were saved. */ |
| 426 | set_movm_offsets (fi, movm_args); |
| 427 | return addr; |
| 428 | } |
| 429 | |
| 430 | /* Function: frame_chain |
| 431 | Figure out and return the caller's frame pointer given current |
| 432 | frame_info struct. |
| 433 | |
| 434 | We don't handle dummy frames yet but we would probably just return the |
| 435 | stack pointer that was in use at the time the function call was made? */ |
| 436 | |
| 437 | CORE_ADDR |
| 438 | mn10300_frame_chain (fi) |
| 439 | struct frame_info *fi; |
| 440 | { |
| 441 | struct frame_info dummy_frame; |
| 442 | |
| 443 | /* Walk through the prologue to determine the stack size, |
| 444 | location of saved registers, end of the prologue, etc. */ |
| 445 | if (fi->status == 0) |
| 446 | mn10300_analyze_prologue (fi, (CORE_ADDR)0); |
| 447 | |
| 448 | /* Quit now if mn10300_analyze_prologue set NO_MORE_FRAMES. */ |
| 449 | if (fi->status & NO_MORE_FRAMES) |
| 450 | return 0; |
| 451 | |
| 452 | /* Now that we've analyzed our prologue, determine the frame |
| 453 | pointer for our caller. |
| 454 | |
| 455 | If our caller has a frame pointer, then we need to |
| 456 | find the entry value of $a3 to our function. |
| 457 | |
| 458 | If fsr.regs[A3_REGNUM] is nonzero, then it's at the memory |
| 459 | location pointed to by fsr.regs[A3_REGNUM]. |
| 460 | |
| 461 | Else it's still in $a3. |
| 462 | |
| 463 | If our caller does not have a frame pointer, then his |
| 464 | frame base is fi->frame + -caller's stack size. */ |
| 465 | |
| 466 | /* The easiest way to get that info is to analyze our caller's frame. |
| 467 | |
| 468 | So we set up a dummy frame and call mn10300_analyze_prologue to |
| 469 | find stuff for us. */ |
| 470 | dummy_frame.pc = FRAME_SAVED_PC (fi); |
| 471 | dummy_frame.frame = fi->frame; |
| 472 | memset (dummy_frame.fsr.regs, '\000', sizeof dummy_frame.fsr.regs); |
| 473 | dummy_frame.status = 0; |
| 474 | dummy_frame.stack_size = 0; |
| 475 | mn10300_analyze_prologue (&dummy_frame, 0); |
| 476 | |
| 477 | if (dummy_frame.status & MY_FRAME_IN_FP) |
| 478 | { |
| 479 | /* Our caller has a frame pointer. So find the frame in $a3 or |
| 480 | in the stack. */ |
| 481 | if (fi->fsr.regs[A3_REGNUM]) |
| 482 | return (read_memory_integer (fi->fsr.regs[A3_REGNUM], REGISTER_SIZE)); |
| 483 | else |
| 484 | return read_register (A3_REGNUM); |
| 485 | } |
| 486 | else |
| 487 | { |
| 488 | int adjust = 0; |
| 489 | |
| 490 | adjust += (fi->fsr.regs[D2_REGNUM] ? 4 : 0); |
| 491 | adjust += (fi->fsr.regs[D3_REGNUM] ? 4 : 0); |
| 492 | adjust += (fi->fsr.regs[A2_REGNUM] ? 4 : 0); |
| 493 | adjust += (fi->fsr.regs[A3_REGNUM] ? 4 : 0); |
| 494 | /* start-sanitize-am33 */ |
| 495 | if (am33_mode) |
| 496 | { |
| 497 | adjust += (fi->fsr.regs[E0_REGNUM+5] ? 4 : 0); |
| 498 | adjust += (fi->fsr.regs[E0_REGNUM+4] ? 4 : 0); |
| 499 | adjust += (fi->fsr.regs[E0_REGNUM+3] ? 4 : 0); |
| 500 | adjust += (fi->fsr.regs[E0_REGNUM+2] ? 4 : 0); |
| 501 | } |
| 502 | /* end-sanitize-am33 */ |
| 503 | |
| 504 | /* Our caller does not have a frame pointer. So his frame starts |
| 505 | at the base of our frame (fi->frame) + register save space |
| 506 | + <his size>. */ |
| 507 | return fi->frame + adjust + -dummy_frame.stack_size; |
| 508 | } |
| 509 | } |
| 510 | |
| 511 | /* Function: skip_prologue |
| 512 | Return the address of the first inst past the prologue of the function. */ |
| 513 | |
| 514 | CORE_ADDR |
| 515 | mn10300_skip_prologue (pc) |
| 516 | CORE_ADDR pc; |
| 517 | { |
| 518 | /* We used to check the debug symbols, but that can lose if |
| 519 | we have a null prologue. */ |
| 520 | return mn10300_analyze_prologue (NULL, pc); |
| 521 | } |
| 522 | |
| 523 | |
| 524 | /* Function: pop_frame |
| 525 | This routine gets called when either the user uses the `return' |
| 526 | command, or the call dummy breakpoint gets hit. */ |
| 527 | |
| 528 | void |
| 529 | mn10300_pop_frame (frame) |
| 530 | struct frame_info *frame; |
| 531 | { |
| 532 | int regnum; |
| 533 | |
| 534 | if (PC_IN_CALL_DUMMY(frame->pc, frame->frame, frame->frame)) |
| 535 | generic_pop_dummy_frame (); |
| 536 | else |
| 537 | { |
| 538 | write_register (PC_REGNUM, FRAME_SAVED_PC (frame)); |
| 539 | |
| 540 | /* Restore any saved registers. */ |
| 541 | for (regnum = 0; regnum < NUM_REGS; regnum++) |
| 542 | if (frame->fsr.regs[regnum] != 0) |
| 543 | { |
| 544 | ULONGEST value; |
| 545 | |
| 546 | value = read_memory_unsigned_integer (frame->fsr.regs[regnum], |
| 547 | REGISTER_RAW_SIZE (regnum)); |
| 548 | write_register (regnum, value); |
| 549 | } |
| 550 | |
| 551 | /* Actually cut back the stack. */ |
| 552 | write_register (SP_REGNUM, FRAME_FP (frame)); |
| 553 | |
| 554 | /* Don't we need to set the PC?!? XXX FIXME. */ |
| 555 | } |
| 556 | |
| 557 | /* Throw away any cached frame information. */ |
| 558 | flush_cached_frames (); |
| 559 | } |
| 560 | |
| 561 | /* Function: push_arguments |
| 562 | Setup arguments for a call to the target. Arguments go in |
| 563 | order on the stack. */ |
| 564 | |
| 565 | CORE_ADDR |
| 566 | mn10300_push_arguments (nargs, args, sp, struct_return, struct_addr) |
| 567 | int nargs; |
| 568 | value_ptr *args; |
| 569 | CORE_ADDR sp; |
| 570 | unsigned char struct_return; |
| 571 | CORE_ADDR struct_addr; |
| 572 | { |
| 573 | int argnum = 0; |
| 574 | int len = 0; |
| 575 | int stack_offset = 0; |
| 576 | int regsused = struct_return ? 1 : 0; |
| 577 | |
| 578 | /* This should be a nop, but align the stack just in case something |
| 579 | went wrong. Stacks are four byte aligned on the mn10300. */ |
| 580 | sp &= ~3; |
| 581 | |
| 582 | /* Now make space on the stack for the args. |
| 583 | |
| 584 | XXX This doesn't appear to handle pass-by-invisible reference |
| 585 | arguments. */ |
| 586 | for (argnum = 0; argnum < nargs; argnum++) |
| 587 | { |
| 588 | int arg_length = (TYPE_LENGTH (VALUE_TYPE (args[argnum])) + 3) & ~3; |
| 589 | |
| 590 | while (regsused < 2 && arg_length > 0) |
| 591 | { |
| 592 | regsused++; |
| 593 | arg_length -= 4; |
| 594 | } |
| 595 | len += arg_length; |
| 596 | } |
| 597 | |
| 598 | /* Allocate stack space. */ |
| 599 | sp -= len; |
| 600 | |
| 601 | regsused = struct_return ? 1 : 0; |
| 602 | /* Push all arguments onto the stack. */ |
| 603 | for (argnum = 0; argnum < nargs; argnum++) |
| 604 | { |
| 605 | int len; |
| 606 | char *val; |
| 607 | |
| 608 | /* XXX Check this. What about UNIONS? */ |
| 609 | if (TYPE_CODE (VALUE_TYPE (*args)) == TYPE_CODE_STRUCT |
| 610 | && TYPE_LENGTH (VALUE_TYPE (*args)) > 8) |
| 611 | { |
| 612 | /* XXX Wrong, we want a pointer to this argument. */ |
| 613 | len = TYPE_LENGTH (VALUE_TYPE (*args)); |
| 614 | val = (char *)VALUE_CONTENTS (*args); |
| 615 | } |
| 616 | else |
| 617 | { |
| 618 | len = TYPE_LENGTH (VALUE_TYPE (*args)); |
| 619 | val = (char *)VALUE_CONTENTS (*args); |
| 620 | } |
| 621 | |
| 622 | while (regsused < 2 && len > 0) |
| 623 | { |
| 624 | write_register (regsused, extract_unsigned_integer (val, 4)); |
| 625 | val += 4; |
| 626 | len -= 4; |
| 627 | regsused++; |
| 628 | } |
| 629 | |
| 630 | while (len > 0) |
| 631 | { |
| 632 | write_memory (sp + stack_offset, val, 4); |
| 633 | len -= 4; |
| 634 | val += 4; |
| 635 | stack_offset += 4; |
| 636 | } |
| 637 | |
| 638 | args++; |
| 639 | } |
| 640 | |
| 641 | /* Make space for the flushback area. */ |
| 642 | sp -= 8; |
| 643 | return sp; |
| 644 | } |
| 645 | |
| 646 | /* Function: push_return_address (pc) |
| 647 | Set up the return address for the inferior function call. |
| 648 | Needed for targets where we don't actually execute a JSR/BSR instruction */ |
| 649 | |
| 650 | CORE_ADDR |
| 651 | mn10300_push_return_address (pc, sp) |
| 652 | CORE_ADDR pc; |
| 653 | CORE_ADDR sp; |
| 654 | { |
| 655 | unsigned char buf[4]; |
| 656 | |
| 657 | store_unsigned_integer (buf, 4, CALL_DUMMY_ADDRESS ()); |
| 658 | write_memory (sp - 4, buf, 4); |
| 659 | return sp - 4; |
| 660 | } |
| 661 | |
| 662 | /* Function: store_struct_return (addr,sp) |
| 663 | Store the structure value return address for an inferior function |
| 664 | call. */ |
| 665 | |
| 666 | CORE_ADDR |
| 667 | mn10300_store_struct_return (addr, sp) |
| 668 | CORE_ADDR addr; |
| 669 | CORE_ADDR sp; |
| 670 | { |
| 671 | /* The structure return address is passed as the first argument. */ |
| 672 | write_register (0, addr); |
| 673 | return sp; |
| 674 | } |
| 675 | |
| 676 | /* Function: frame_saved_pc |
| 677 | Find the caller of this frame. We do this by seeing if RP_REGNUM |
| 678 | is saved in the stack anywhere, otherwise we get it from the |
| 679 | registers. If the inner frame is a dummy frame, return its PC |
| 680 | instead of RP, because that's where "caller" of the dummy-frame |
| 681 | will be found. */ |
| 682 | |
| 683 | CORE_ADDR |
| 684 | mn10300_frame_saved_pc (fi) |
| 685 | struct frame_info *fi; |
| 686 | { |
| 687 | int adjust = 0; |
| 688 | |
| 689 | adjust += (fi->fsr.regs[D2_REGNUM] ? 4 : 0); |
| 690 | adjust += (fi->fsr.regs[D3_REGNUM] ? 4 : 0); |
| 691 | adjust += (fi->fsr.regs[A2_REGNUM] ? 4 : 0); |
| 692 | adjust += (fi->fsr.regs[A3_REGNUM] ? 4 : 0); |
| 693 | /* start-sanitize-am33 */ |
| 694 | if (am33_mode) |
| 695 | { |
| 696 | adjust += (fi->fsr.regs[E0_REGNUM+5] ? 4 : 0); |
| 697 | adjust += (fi->fsr.regs[E0_REGNUM+4] ? 4 : 0); |
| 698 | adjust += (fi->fsr.regs[E0_REGNUM+3] ? 4 : 0); |
| 699 | adjust += (fi->fsr.regs[E0_REGNUM+2] ? 4 : 0); |
| 700 | } |
| 701 | /* end-sanitize-am33 */ |
| 702 | |
| 703 | return (read_memory_integer (fi->frame + adjust, REGISTER_SIZE)); |
| 704 | } |
| 705 | |
| 706 | void |
| 707 | get_saved_register (raw_buffer, optimized, addrp, frame, regnum, lval) |
| 708 | char *raw_buffer; |
| 709 | int *optimized; |
| 710 | CORE_ADDR *addrp; |
| 711 | struct frame_info *frame; |
| 712 | int regnum; |
| 713 | enum lval_type *lval; |
| 714 | { |
| 715 | generic_get_saved_register (raw_buffer, optimized, addrp, |
| 716 | frame, regnum, lval); |
| 717 | } |
| 718 | |
| 719 | /* Function: init_extra_frame_info |
| 720 | Setup the frame's frame pointer, pc, and frame addresses for saved |
| 721 | registers. Most of the work is done in mn10300_analyze_prologue(). |
| 722 | |
| 723 | Note that when we are called for the last frame (currently active frame), |
| 724 | that fi->pc and fi->frame will already be setup. However, fi->frame will |
| 725 | be valid only if this routine uses FP. For previous frames, fi-frame will |
| 726 | always be correct. mn10300_analyze_prologue will fix fi->frame if |
| 727 | it's not valid. |
| 728 | |
| 729 | We can be called with the PC in the call dummy under two circumstances. |
| 730 | First, during normal backtracing, second, while figuring out the frame |
| 731 | pointer just prior to calling the target function (see run_stack_dummy). */ |
| 732 | |
| 733 | void |
| 734 | mn10300_init_extra_frame_info (fi) |
| 735 | struct frame_info *fi; |
| 736 | { |
| 737 | if (fi->next) |
| 738 | fi->pc = FRAME_SAVED_PC (fi->next); |
| 739 | |
| 740 | memset (fi->fsr.regs, '\000', sizeof fi->fsr.regs); |
| 741 | fi->status = 0; |
| 742 | fi->stack_size = 0; |
| 743 | |
| 744 | mn10300_analyze_prologue (fi, 0); |
| 745 | } |
| 746 | |
| 747 | /* Function: mn10300_virtual_frame_pointer |
| 748 | Return the register that the function uses for a frame pointer, |
| 749 | plus any necessary offset to be applied to the register before |
| 750 | any frame pointer offsets. */ |
| 751 | |
| 752 | void |
| 753 | mn10300_virtual_frame_pointer (pc, reg, offset) |
| 754 | CORE_ADDR pc; |
| 755 | long *reg; |
| 756 | long *offset; |
| 757 | { |
| 758 | struct frame_info fi; |
| 759 | |
| 760 | /* Set up a dummy frame_info. */ |
| 761 | fi.next = NULL; |
| 762 | fi.prev = NULL; |
| 763 | fi.frame = 0; |
| 764 | fi.pc = pc; |
| 765 | |
| 766 | /* Analyze the prolog and fill in the extra info. */ |
| 767 | mn10300_init_extra_frame_info (&fi); |
| 768 | |
| 769 | /* Results will tell us which type of frame it uses. */ |
| 770 | if (fi.status & MY_FRAME_IN_SP) |
| 771 | { |
| 772 | *reg = SP_REGNUM; |
| 773 | *offset = -(fi.stack_size); |
| 774 | } |
| 775 | else |
| 776 | { |
| 777 | *reg = A3_REGNUM; |
| 778 | *offset = 0; |
| 779 | } |
| 780 | } |
| 781 | |
| 782 | /* This can be made more generic later. */ |
| 783 | static void |
| 784 | set_machine_hook (filename) |
| 785 | char *filename; |
| 786 | { |
| 787 | int i; |
| 788 | |
| 789 | if (bfd_get_mach (exec_bfd) == bfd_mach_mn10300 |
| 790 | || bfd_get_mach (exec_bfd) == 0) |
| 791 | { |
| 792 | mn10300_register_names = mn10300_generic_register_names; |
| 793 | } |
| 794 | |
| 795 | /* start-sanitize-am33 */ |
| 796 | am33_mode = 0; |
| 797 | if (bfd_get_mach (exec_bfd) == bfd_mach_am33) |
| 798 | { |
| 799 | |
| 800 | mn10300_register_names = am33_register_names; |
| 801 | am33_mode = 1; |
| 802 | } |
| 803 | /* end-sanitize-am33 */ |
| 804 | } |
| 805 | |
| 806 | void |
| 807 | _initialize_mn10300_tdep () |
| 808 | { |
| 809 | /* printf("_initialize_mn10300_tdep\n"); */ |
| 810 | |
| 811 | tm_print_insn = print_insn_mn10300; |
| 812 | |
| 813 | specify_exec_file_hook (set_machine_hook); |
| 814 | } |
| 815 | |