| 1 | /* Target-dependent code for the Matsushita MN10200 for GDB, the GNU debugger. |
| 2 | Copyright 1997 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 | /* The main purpose of this file is dealing with prologues to extract |
| 32 | information about stack frames and saved registers. |
| 33 | |
| 34 | For reference here's how prologues look on the mn10200: |
| 35 | |
| 36 | With frame pointer: |
| 37 | mov fp,a0 |
| 38 | mov sp,fp |
| 39 | add <size>,sp |
| 40 | Register saves for d2, d3, a1, a2 as needed. Saves start |
| 41 | at fp - <size> + <outgoing_args_size> and work towards higher |
| 42 | addresses. Note that the saves are actually done off the stack |
| 43 | pointer in the prologue! This makes for smaller code and easier |
| 44 | prologue scanning as the displacement fields will unlikely |
| 45 | be more than 8 bits! |
| 46 | |
| 47 | Without frame pointer: |
| 48 | add <size>,sp |
| 49 | Register saves for d2, d3, a1, a2 as needed. Saves start |
| 50 | at sp + <outgoing_args_size> and work towards higher addresses. |
| 51 | |
| 52 | Out of line prologue: |
| 53 | add <local size>,sp -- optional |
| 54 | jsr __prologue |
| 55 | add <outgoing_size>,sp -- optional |
| 56 | |
| 57 | The stack pointer remains constant throughout the life of most |
| 58 | functions. As a result the compiler will usually omit the |
| 59 | frame pointer, so we must handle frame pointerless functions. */ |
| 60 | |
| 61 | /* Analyze the prologue to determine where registers are saved, |
| 62 | the end of the prologue, etc etc. Return the end of the prologue |
| 63 | scanned. |
| 64 | |
| 65 | We store into FI (if non-null) several tidbits of information: |
| 66 | |
| 67 | * stack_size -- size of this stack frame. Note that if we stop in |
| 68 | certain parts of the prologue/epilogue we may claim the size of the |
| 69 | current frame is zero. This happens when the current frame has |
| 70 | not been allocated yet or has already been deallocated. |
| 71 | |
| 72 | * fsr -- Addresses of registers saved in the stack by this frame. |
| 73 | |
| 74 | * status -- A (relatively) generic status indicator. It's a bitmask |
| 75 | with the following bits: |
| 76 | |
| 77 | MY_FRAME_IN_SP: The base of the current frame is actually in |
| 78 | the stack pointer. This can happen for frame pointerless |
| 79 | functions, or cases where we're stopped in the prologue/epilogue |
| 80 | itself. For these cases mn10200_analyze_prologue will need up |
| 81 | update fi->frame before returning or analyzing the register |
| 82 | save instructions. |
| 83 | |
| 84 | MY_FRAME_IN_FP: The base of the current frame is in the |
| 85 | frame pointer register ($a2). |
| 86 | |
| 87 | CALLER_A2_IN_A0: $a2 from the caller's frame is temporarily |
| 88 | in $a0. This can happen if we're stopped in the prologue. |
| 89 | |
| 90 | NO_MORE_FRAMES: Set this if the current frame is "start" or |
| 91 | if the first instruction looks like mov <imm>,sp. This tells |
| 92 | frame chain to not bother trying to unwind past this frame. */ |
| 93 | |
| 94 | #define MY_FRAME_IN_SP 0x1 |
| 95 | #define MY_FRAME_IN_FP 0x2 |
| 96 | #define CALLER_A2_IN_A0 0x4 |
| 97 | #define NO_MORE_FRAMES 0x8 |
| 98 | |
| 99 | static CORE_ADDR |
| 100 | mn10200_analyze_prologue (fi, pc) |
| 101 | struct frame_info *fi; |
| 102 | CORE_ADDR pc; |
| 103 | { |
| 104 | CORE_ADDR func_addr, func_end, addr, stop; |
| 105 | CORE_ADDR stack_size; |
| 106 | unsigned char buf[4]; |
| 107 | int status; |
| 108 | char *name; |
| 109 | int out_of_line_prologue = 0; |
| 110 | |
| 111 | /* Use the PC in the frame if it's provided to look up the |
| 112 | start of this function. */ |
| 113 | pc = (fi ? fi->pc : pc); |
| 114 | |
| 115 | /* Find the start of this function. */ |
| 116 | status = find_pc_partial_function (pc, &name, &func_addr, &func_end); |
| 117 | |
| 118 | /* Do nothing if we couldn't find the start of this function or if we're |
| 119 | stopped at the first instruction in the prologue. */ |
| 120 | if (status == 0) |
| 121 | return pc; |
| 122 | |
| 123 | /* If we're in start, then give up. */ |
| 124 | if (strcmp (name, "start") == 0) |
| 125 | { |
| 126 | fi->status = NO_MORE_FRAMES; |
| 127 | return pc; |
| 128 | } |
| 129 | |
| 130 | /* At the start of a function our frame is in the stack pointer. */ |
| 131 | if (fi) |
| 132 | fi->status = MY_FRAME_IN_SP; |
| 133 | |
| 134 | /* If we're physically on an RTS instruction, then our frame has already |
| 135 | been deallocated. |
| 136 | |
| 137 | fi->frame is bogus, we need to fix it. */ |
| 138 | if (fi && fi->pc + 1 == func_end) |
| 139 | { |
| 140 | status = target_read_memory (fi->pc, buf, 1); |
| 141 | if (status != 0) |
| 142 | { |
| 143 | if (fi->next == NULL) |
| 144 | fi->frame = read_sp (); |
| 145 | return fi->pc; |
| 146 | } |
| 147 | |
| 148 | if (buf[0] == 0xfe) |
| 149 | { |
| 150 | if (fi->next == NULL) |
| 151 | fi->frame = read_sp (); |
| 152 | return fi->pc; |
| 153 | } |
| 154 | } |
| 155 | |
| 156 | /* Similarly if we're stopped on the first insn of a prologue as our |
| 157 | frame hasn't been allocated yet. */ |
| 158 | if (fi && fi->pc == func_addr) |
| 159 | { |
| 160 | if (fi->next == NULL) |
| 161 | fi->frame = read_sp (); |
| 162 | return fi->pc; |
| 163 | } |
| 164 | |
| 165 | /* Figure out where to stop scanning. */ |
| 166 | stop = fi ? fi->pc : func_end; |
| 167 | |
| 168 | /* Don't walk off the end of the function. */ |
| 169 | stop = stop > func_end ? func_end : stop; |
| 170 | |
| 171 | /* Start scanning on the first instruction of this function. */ |
| 172 | addr = func_addr; |
| 173 | |
| 174 | status = target_read_memory (addr, buf, 2); |
| 175 | if (status != 0) |
| 176 | { |
| 177 | if (fi && fi->next == NULL && fi->status & MY_FRAME_IN_SP) |
| 178 | fi->frame = read_sp (); |
| 179 | return addr; |
| 180 | } |
| 181 | |
| 182 | /* First see if this insn sets the stack pointer; if so, it's something |
| 183 | we won't understand, so quit now. */ |
| 184 | if (buf[0] == 0xdf |
| 185 | || (buf[0] == 0xf4 && buf[1] == 0x77)) |
| 186 | { |
| 187 | if (fi) |
| 188 | fi->status = NO_MORE_FRAMES; |
| 189 | return addr; |
| 190 | } |
| 191 | |
| 192 | /* Now see if we have a frame pointer. |
| 193 | |
| 194 | Search for mov a2,a0 (0xf278) |
| 195 | then mov a3,a2 (0xf27e). */ |
| 196 | |
| 197 | if (buf[0] == 0xf2 && buf[1] == 0x78) |
| 198 | { |
| 199 | /* Our caller's $a2 will be found in $a0 now. Note it for |
| 200 | our callers. */ |
| 201 | if (fi) |
| 202 | fi->status |= CALLER_A2_IN_A0; |
| 203 | addr += 2; |
| 204 | if (addr >= stop) |
| 205 | { |
| 206 | /* We still haven't allocated our local stack. Handle this |
| 207 | as if we stopped on the first or last insn of a function. */ |
| 208 | if (fi && fi->next == NULL) |
| 209 | fi->frame = read_sp (); |
| 210 | return addr; |
| 211 | } |
| 212 | |
| 213 | status = target_read_memory (addr, buf, 2); |
| 214 | if (status != 0) |
| 215 | { |
| 216 | if (fi && fi->next == NULL) |
| 217 | fi->frame = read_sp (); |
| 218 | return addr; |
| 219 | } |
| 220 | if (buf[0] == 0xf2 && buf[1] == 0x7e) |
| 221 | { |
| 222 | addr += 2; |
| 223 | |
| 224 | /* Our frame pointer is valid now. */ |
| 225 | if (fi) |
| 226 | { |
| 227 | fi->status |= MY_FRAME_IN_FP; |
| 228 | fi->status &= ~MY_FRAME_IN_SP; |
| 229 | } |
| 230 | if (addr >= stop) |
| 231 | return addr; |
| 232 | } |
| 233 | else |
| 234 | { |
| 235 | if (fi && fi->next == NULL) |
| 236 | fi->frame = read_sp (); |
| 237 | return addr; |
| 238 | } |
| 239 | } |
| 240 | |
| 241 | /* Next we should allocate the local frame. |
| 242 | |
| 243 | Search for add imm8,a3 (0xd3XX) |
| 244 | or add imm16,a3 (0xf70bXXXX) |
| 245 | or add imm24,a3 (0xf467XXXXXX). |
| 246 | |
| 247 | If none of the above was found, then this prologue has |
| 248 | no stack, and therefore can't have any register saves, |
| 249 | so quit now. */ |
| 250 | status = target_read_memory (addr, buf, 2); |
| 251 | if (status != 0) |
| 252 | { |
| 253 | if (fi && fi->next == NULL && (fi->status & MY_FRAME_IN_SP)) |
| 254 | fi->frame = read_sp (); |
| 255 | return addr; |
| 256 | } |
| 257 | if (buf[0] == 0xd3) |
| 258 | { |
| 259 | stack_size = extract_signed_integer (&buf[1], 1); |
| 260 | if (fi) |
| 261 | fi->stack_size = stack_size; |
| 262 | addr += 2; |
| 263 | if (addr >= stop) |
| 264 | { |
| 265 | if (fi && fi->next == NULL && (fi->status & MY_FRAME_IN_SP)) |
| 266 | fi->frame = read_sp () - stack_size; |
| 267 | return addr; |
| 268 | } |
| 269 | } |
| 270 | else if (buf[0] == 0xf7 && buf[1] == 0x0b) |
| 271 | { |
| 272 | status = target_read_memory (addr + 2, buf, 2); |
| 273 | if (status != 0) |
| 274 | { |
| 275 | if (fi && fi->next == NULL && (fi->status & MY_FRAME_IN_SP)) |
| 276 | fi->frame = read_sp (); |
| 277 | return addr; |
| 278 | } |
| 279 | stack_size = extract_signed_integer (buf, 2); |
| 280 | if (fi) |
| 281 | fi->stack_size = stack_size; |
| 282 | addr += 4; |
| 283 | if (addr >= stop) |
| 284 | { |
| 285 | if (fi && fi->next == NULL && (fi->status & MY_FRAME_IN_SP)) |
| 286 | fi->frame = read_sp () - stack_size; |
| 287 | return addr; |
| 288 | } |
| 289 | } |
| 290 | else if (buf[0] == 0xf4 && buf[1] == 0x67) |
| 291 | { |
| 292 | status = target_read_memory (addr + 2, buf, 3); |
| 293 | if (status != 0) |
| 294 | { |
| 295 | if (fi && fi->next == NULL && (fi->status & MY_FRAME_IN_SP)) |
| 296 | fi->frame = read_sp (); |
| 297 | return addr; |
| 298 | } |
| 299 | stack_size = extract_signed_integer (buf, 3); |
| 300 | if (fi) |
| 301 | fi->stack_size = stack_size; |
| 302 | addr += 5; |
| 303 | if (addr >= stop) |
| 304 | { |
| 305 | if (fi && fi->next == NULL && (fi->status & MY_FRAME_IN_SP)) |
| 306 | fi->frame = read_sp () - stack_size; |
| 307 | return addr; |
| 308 | } |
| 309 | } |
| 310 | |
| 311 | /* Now see if we have a call to __prologue for an out of line |
| 312 | prologue. */ |
| 313 | status = target_read_memory (addr, buf, 2); |
| 314 | if (status != 0) |
| 315 | return addr; |
| 316 | |
| 317 | /* First check for 16bit pc-relative call to __prologue. */ |
| 318 | if (buf[0] == 0xfd) |
| 319 | { |
| 320 | CORE_ADDR temp; |
| 321 | status = target_read_memory (addr + 1, buf, 2); |
| 322 | if (status != 0) |
| 323 | { |
| 324 | if (fi && fi->next == NULL && (fi->status & MY_FRAME_IN_SP)) |
| 325 | fi->frame = read_sp (); |
| 326 | return addr; |
| 327 | } |
| 328 | |
| 329 | /* Get the PC this instruction will branch to. */ |
| 330 | temp = (extract_signed_integer (buf, 2) + addr) & 0xffffff; |
| 331 | |
| 332 | /* Get the name of the function at the target address. */ |
| 333 | status = find_pc_partial_function (temp, &name, NULL, NULL); |
| 334 | if (status == 0) |
| 335 | { |
| 336 | if (fi && fi->next == NULL && (fi->status & MY_FRAME_IN_SP)) |
| 337 | fi->frame = read_sp (); |
| 338 | return addr; |
| 339 | } |
| 340 | |
| 341 | /* Note if it is an out of line prologue. */ |
| 342 | out_of_line_prologue = (strcmp (name, "__prologue") == 0); |
| 343 | |
| 344 | /* This sucks up 3 bytes of instruction space. */ |
| 345 | if (out_of_line_prologue) |
| 346 | addr += 3; |
| 347 | |
| 348 | if (addr >= stop) |
| 349 | { |
| 350 | if (fi && fi->next == NULL) |
| 351 | { |
| 352 | fi->stack_size -= 16; |
| 353 | fi->frame = read_sp () - fi->stack_size; |
| 354 | } |
| 355 | return addr; |
| 356 | } |
| 357 | } |
| 358 | /* Now check for the 24bit pc-relative call to __prologue. */ |
| 359 | else if (buf[0] == 0xf4 && buf[1] == 0xe1) |
| 360 | { |
| 361 | CORE_ADDR temp; |
| 362 | status = target_read_memory (addr + 2, buf, 3); |
| 363 | if (status != 0) |
| 364 | { |
| 365 | if (fi && fi->next == NULL && (fi->status & MY_FRAME_IN_SP)) |
| 366 | fi->frame = read_sp (); |
| 367 | return addr; |
| 368 | } |
| 369 | |
| 370 | /* Get the PC this instruction will branch to. */ |
| 371 | temp = (extract_signed_integer (buf, 3) + addr) & 0xffffff; |
| 372 | |
| 373 | /* Get the name of the function at the target address. */ |
| 374 | status = find_pc_partial_function (temp, &name, NULL, NULL); |
| 375 | if (status == 0) |
| 376 | { |
| 377 | if (fi && fi->next == NULL && (fi->status & MY_FRAME_IN_SP)) |
| 378 | fi->frame = read_sp (); |
| 379 | return addr; |
| 380 | } |
| 381 | |
| 382 | /* Note if it is an out of line prologue. */ |
| 383 | out_of_line_prologue = (strcmp (name, "__prologue") == 0); |
| 384 | |
| 385 | /* This sucks up 5 bytes of instruction space. */ |
| 386 | if (out_of_line_prologue) |
| 387 | addr += 5; |
| 388 | |
| 389 | if (addr >= stop) |
| 390 | { |
| 391 | if (fi && fi->next == NULL && (fi->status & MY_FRAME_IN_SP)) |
| 392 | { |
| 393 | fi->stack_size -= 16; |
| 394 | fi->frame = read_sp () - fi->stack_size; |
| 395 | } |
| 396 | return addr; |
| 397 | } |
| 398 | } |
| 399 | |
| 400 | /* Now actually handle the out of line prologue. */ |
| 401 | if (out_of_line_prologue) |
| 402 | { |
| 403 | int outgoing_args_size = 0; |
| 404 | |
| 405 | /* First adjust the stack size for this function. The out of |
| 406 | line prologue saves 4 registers (16bytes of data). */ |
| 407 | if (fi) |
| 408 | fi->stack_size -= 16; |
| 409 | |
| 410 | /* Update fi->frame if necessary. */ |
| 411 | if (fi && fi->next == NULL) |
| 412 | fi->frame = read_sp () - fi->stack_size; |
| 413 | |
| 414 | /* After the out of line prologue, there may be another |
| 415 | stack adjustment for the outgoing arguments. |
| 416 | |
| 417 | Search for add imm8,a3 (0xd3XX) |
| 418 | or add imm16,a3 (0xf70bXXXX) |
| 419 | or add imm24,a3 (0xf467XXXXXX). */ |
| 420 | |
| 421 | status = target_read_memory (addr, buf, 2); |
| 422 | if (status != 0) |
| 423 | { |
| 424 | if (fi) |
| 425 | { |
| 426 | fi->fsr.regs[2] = fi->frame + fi->stack_size + 4; |
| 427 | fi->fsr.regs[3] = fi->frame + fi->stack_size + 8; |
| 428 | fi->fsr.regs[5] = fi->frame + fi->stack_size + 12; |
| 429 | fi->fsr.regs[6] = fi->frame + fi->stack_size + 16; |
| 430 | } |
| 431 | return addr; |
| 432 | } |
| 433 | |
| 434 | if (buf[0] == 0xd3) |
| 435 | { |
| 436 | outgoing_args_size = extract_signed_integer (&buf[1], 1); |
| 437 | addr += 2; |
| 438 | } |
| 439 | else if (buf[0] == 0xf7 && buf[1] == 0x0b) |
| 440 | { |
| 441 | status = target_read_memory (addr + 2, buf, 2); |
| 442 | if (status != 0) |
| 443 | { |
| 444 | if (fi) |
| 445 | { |
| 446 | fi->fsr.regs[2] = fi->frame + fi->stack_size + 4; |
| 447 | fi->fsr.regs[3] = fi->frame + fi->stack_size + 8; |
| 448 | fi->fsr.regs[5] = fi->frame + fi->stack_size + 12; |
| 449 | fi->fsr.regs[6] = fi->frame + fi->stack_size + 16; |
| 450 | } |
| 451 | return addr; |
| 452 | } |
| 453 | outgoing_args_size = extract_signed_integer (buf, 2); |
| 454 | addr += 4; |
| 455 | } |
| 456 | else if (buf[0] == 0xf4 && buf[1] == 0x67) |
| 457 | { |
| 458 | status = target_read_memory (addr + 2, buf, 3); |
| 459 | if (status != 0) |
| 460 | { |
| 461 | if (fi && fi->next == NULL) |
| 462 | { |
| 463 | fi->fsr.regs[2] = fi->frame + fi->stack_size + 4; |
| 464 | fi->fsr.regs[3] = fi->frame + fi->stack_size + 8; |
| 465 | fi->fsr.regs[5] = fi->frame + fi->stack_size + 12; |
| 466 | fi->fsr.regs[6] = fi->frame + fi->stack_size + 16; |
| 467 | } |
| 468 | return addr; |
| 469 | } |
| 470 | outgoing_args_size = extract_signed_integer (buf, 3); |
| 471 | addr += 5; |
| 472 | } |
| 473 | else |
| 474 | outgoing_args_size = 0; |
| 475 | |
| 476 | /* Now that we know the size of the outgoing arguments, fix |
| 477 | fi->frame again if this is the innermost frame. */ |
| 478 | if (fi && fi->next == NULL) |
| 479 | fi->frame -= outgoing_args_size; |
| 480 | |
| 481 | /* Note the register save information and update the stack |
| 482 | size for this frame too. */ |
| 483 | if (fi) |
| 484 | { |
| 485 | fi->fsr.regs[2] = fi->frame + fi->stack_size + 4; |
| 486 | fi->fsr.regs[3] = fi->frame + fi->stack_size + 8; |
| 487 | fi->fsr.regs[5] = fi->frame + fi->stack_size + 12; |
| 488 | fi->fsr.regs[6] = fi->frame + fi->stack_size + 16; |
| 489 | fi->stack_size += outgoing_args_size; |
| 490 | } |
| 491 | /* There can be no more prologue insns, so return now. */ |
| 492 | return addr; |
| 493 | } |
| 494 | |
| 495 | /* At this point fi->frame needs to be correct. |
| 496 | |
| 497 | If MY_FRAME_IN_SP is set and we're the innermost frame, then we |
| 498 | need to fix fi->frame so that backtracing, find_frame_saved_regs, |
| 499 | etc work correctly. */ |
| 500 | if (fi && fi->next == NULL && (fi->status & MY_FRAME_IN_SP) != 0) |
| 501 | fi->frame = read_sp () - fi->stack_size; |
| 502 | |
| 503 | /* And last we have the register saves. These are relatively |
| 504 | simple because they're physically done off the stack pointer, |
| 505 | and thus the number of different instructions we need to |
| 506 | check is greatly reduced because we know the displacements |
| 507 | will be small. |
| 508 | |
| 509 | Search for movx d2,(X,a3) (0xf55eXX) |
| 510 | then movx d3,(X,a3) (0xf55fXX) |
| 511 | then mov a1,(X,a3) (0x5dXX) No frame pointer case |
| 512 | then mov a2,(X,a3) (0x5eXX) No frame pointer case |
| 513 | or mov a0,(X,a3) (0x5cXX) Frame pointer case. */ |
| 514 | |
| 515 | status = target_read_memory (addr, buf, 2); |
| 516 | if (status != 0) |
| 517 | return addr; |
| 518 | if (buf[0] == 0xf5 && buf[1] == 0x5e) |
| 519 | { |
| 520 | if (fi) |
| 521 | { |
| 522 | status = target_read_memory (addr + 2, buf, 1); |
| 523 | if (status != 0) |
| 524 | return addr; |
| 525 | fi->fsr.regs[2] = (fi->frame + stack_size |
| 526 | + extract_signed_integer (buf, 1)); |
| 527 | } |
| 528 | addr += 3; |
| 529 | if (addr >= stop) |
| 530 | return addr; |
| 531 | status = target_read_memory (addr, buf, 2); |
| 532 | if (status != 0) |
| 533 | return addr; |
| 534 | } |
| 535 | if (buf[0] == 0xf5 && buf[1] == 0x5f) |
| 536 | { |
| 537 | if (fi) |
| 538 | { |
| 539 | status = target_read_memory (addr + 2, buf, 1); |
| 540 | if (status != 0) |
| 541 | return addr; |
| 542 | fi->fsr.regs[3] = (fi->frame + stack_size |
| 543 | + extract_signed_integer (buf, 1)); |
| 544 | } |
| 545 | addr += 3; |
| 546 | if (addr >= stop) |
| 547 | return addr; |
| 548 | status = target_read_memory (addr, buf, 2); |
| 549 | if (status != 0) |
| 550 | return addr; |
| 551 | } |
| 552 | if (buf[0] == 0x5d) |
| 553 | { |
| 554 | if (fi) |
| 555 | { |
| 556 | status = target_read_memory (addr + 1, buf, 1); |
| 557 | if (status != 0) |
| 558 | return addr; |
| 559 | fi->fsr.regs[5] = (fi->frame + stack_size |
| 560 | + extract_signed_integer (buf, 1)); |
| 561 | } |
| 562 | addr += 2; |
| 563 | if (addr >= stop) |
| 564 | return addr; |
| 565 | status = target_read_memory (addr, buf, 2); |
| 566 | if (status != 0) |
| 567 | return addr; |
| 568 | } |
| 569 | if (buf[0] == 0x5e || buf[0] == 0x5c) |
| 570 | { |
| 571 | if (fi) |
| 572 | { |
| 573 | status = target_read_memory (addr + 1, buf, 1); |
| 574 | if (status != 0) |
| 575 | return addr; |
| 576 | fi->fsr.regs[6] = (fi->frame + stack_size |
| 577 | + extract_signed_integer (buf, 1)); |
| 578 | fi->status &= ~CALLER_A2_IN_A0; |
| 579 | } |
| 580 | addr += 2; |
| 581 | if (addr >= stop) |
| 582 | return addr; |
| 583 | return addr; |
| 584 | } |
| 585 | return addr; |
| 586 | } |
| 587 | |
| 588 | /* Function: frame_chain |
| 589 | Figure out and return the caller's frame pointer given current |
| 590 | frame_info struct. |
| 591 | |
| 592 | We don't handle dummy frames yet but we would probably just return the |
| 593 | stack pointer that was in use at the time the function call was made? */ |
| 594 | |
| 595 | CORE_ADDR |
| 596 | mn10200_frame_chain (fi) |
| 597 | struct frame_info *fi; |
| 598 | { |
| 599 | struct frame_info dummy_frame; |
| 600 | |
| 601 | /* Walk through the prologue to determine the stack size, |
| 602 | location of saved registers, end of the prologue, etc. */ |
| 603 | if (fi->status == 0) |
| 604 | mn10200_analyze_prologue (fi, (CORE_ADDR)0); |
| 605 | |
| 606 | /* Quit now if mn10200_analyze_prologue set NO_MORE_FRAMES. */ |
| 607 | if (fi->status & NO_MORE_FRAMES) |
| 608 | return 0; |
| 609 | |
| 610 | /* Now that we've analyzed our prologue, determine the frame |
| 611 | pointer for our caller. |
| 612 | |
| 613 | If our caller has a frame pointer, then we need to |
| 614 | find the entry value of $a2 to our function. |
| 615 | |
| 616 | If CALLER_A2_IN_A0, then the chain is in $a0. |
| 617 | |
| 618 | If fsr.regs[6] is nonzero, then it's at the memory |
| 619 | location pointed to by fsr.regs[6]. |
| 620 | |
| 621 | Else it's still in $a2. |
| 622 | |
| 623 | If our caller does not have a frame pointer, then his |
| 624 | frame base is fi->frame + -caller's stack size + 4. */ |
| 625 | |
| 626 | /* The easiest way to get that info is to analyze our caller's frame. |
| 627 | |
| 628 | So we set up a dummy frame and call mn10200_analyze_prologue to |
| 629 | find stuff for us. */ |
| 630 | dummy_frame.pc = FRAME_SAVED_PC (fi); |
| 631 | dummy_frame.frame = fi->frame; |
| 632 | memset (dummy_frame.fsr.regs, '\000', sizeof dummy_frame.fsr.regs); |
| 633 | dummy_frame.status = 0; |
| 634 | dummy_frame.stack_size = 0; |
| 635 | mn10200_analyze_prologue (&dummy_frame); |
| 636 | |
| 637 | if (dummy_frame.status & MY_FRAME_IN_FP) |
| 638 | { |
| 639 | /* Our caller has a frame pointer. So find the frame in $a2, $a0, |
| 640 | or in the stack. */ |
| 641 | if (fi->fsr.regs[6]) |
| 642 | return (read_memory_integer (fi->fsr.regs[FP_REGNUM], REGISTER_SIZE) |
| 643 | & 0xffffff); |
| 644 | else if (fi->status & CALLER_A2_IN_A0) |
| 645 | return read_register (4); |
| 646 | else |
| 647 | return read_register (FP_REGNUM); |
| 648 | } |
| 649 | else |
| 650 | { |
| 651 | /* Our caller does not have a frame pointer. So his frame starts |
| 652 | at the base of our frame (fi->frame) + <his size> + 4 (saved pc). */ |
| 653 | return fi->frame + -dummy_frame.stack_size + 4; |
| 654 | } |
| 655 | } |
| 656 | |
| 657 | /* Function: skip_prologue |
| 658 | Return the address of the first inst past the prologue of the function. */ |
| 659 | |
| 660 | CORE_ADDR |
| 661 | mn10200_skip_prologue (pc) |
| 662 | CORE_ADDR pc; |
| 663 | { |
| 664 | /* We used to check the debug symbols, but that can lose if |
| 665 | we have a null prologue. */ |
| 666 | return mn10200_analyze_prologue (NULL, pc); |
| 667 | } |
| 668 | |
| 669 | /* Function: pop_frame |
| 670 | This routine gets called when either the user uses the `return' |
| 671 | command, or the call dummy breakpoint gets hit. */ |
| 672 | |
| 673 | void |
| 674 | mn10200_pop_frame (frame) |
| 675 | struct frame_info *frame; |
| 676 | { |
| 677 | int regnum; |
| 678 | |
| 679 | if (PC_IN_CALL_DUMMY(frame->pc, frame->frame, frame->frame)) |
| 680 | generic_pop_dummy_frame (); |
| 681 | else |
| 682 | { |
| 683 | write_register (PC_REGNUM, FRAME_SAVED_PC (frame)); |
| 684 | |
| 685 | /* Restore any saved registers. */ |
| 686 | for (regnum = 0; regnum < NUM_REGS; regnum++) |
| 687 | if (frame->fsr.regs[regnum] != 0) |
| 688 | { |
| 689 | ULONGEST value; |
| 690 | |
| 691 | value = read_memory_unsigned_integer (frame->fsr.regs[regnum], |
| 692 | REGISTER_RAW_SIZE (regnum)); |
| 693 | write_register (regnum, value); |
| 694 | } |
| 695 | |
| 696 | /* Actually cut back the stack. */ |
| 697 | write_register (SP_REGNUM, FRAME_FP (frame)); |
| 698 | |
| 699 | /* Don't we need to set the PC?!? XXX FIXME. */ |
| 700 | } |
| 701 | |
| 702 | /* Throw away any cached frame information. */ |
| 703 | flush_cached_frames (); |
| 704 | } |
| 705 | |
| 706 | /* Function: push_arguments |
| 707 | Setup arguments for a call to the target. Arguments go in |
| 708 | order on the stack. */ |
| 709 | |
| 710 | CORE_ADDR |
| 711 | mn10200_push_arguments (nargs, args, sp, struct_return, struct_addr) |
| 712 | int nargs; |
| 713 | value_ptr *args; |
| 714 | CORE_ADDR sp; |
| 715 | unsigned char struct_return; |
| 716 | CORE_ADDR struct_addr; |
| 717 | { |
| 718 | int argnum = 0; |
| 719 | int len = 0; |
| 720 | int stack_offset = 0; |
| 721 | int regsused = struct_return ? 1 : 0; |
| 722 | |
| 723 | /* This should be a nop, but align the stack just in case something |
| 724 | went wrong. Stacks are two byte aligned on the mn10200. */ |
| 725 | sp &= ~1; |
| 726 | |
| 727 | /* Now make space on the stack for the args. |
| 728 | |
| 729 | XXX This doesn't appear to handle pass-by-invisible reference |
| 730 | arguments. */ |
| 731 | for (argnum = 0; argnum < nargs; argnum++) |
| 732 | { |
| 733 | int arg_length = (TYPE_LENGTH (VALUE_TYPE (args[argnum])) + 1) & ~1; |
| 734 | |
| 735 | /* If we've used all argument registers, then this argument is |
| 736 | pushed. */ |
| 737 | if (regsused >= 2 || arg_length > 4) |
| 738 | { |
| 739 | regsused = 2; |
| 740 | len += arg_length; |
| 741 | } |
| 742 | /* We know we've got some arg register space left. If this argument |
| 743 | will fit entirely in regs, then put it there. */ |
| 744 | else if (arg_length <= 2 |
| 745 | || TYPE_CODE (VALUE_TYPE (args[argnum])) == TYPE_CODE_PTR) |
| 746 | { |
| 747 | regsused++; |
| 748 | } |
| 749 | else if (regsused == 0) |
| 750 | { |
| 751 | regsused = 2; |
| 752 | } |
| 753 | else |
| 754 | { |
| 755 | regsused = 2; |
| 756 | len += arg_length; |
| 757 | } |
| 758 | } |
| 759 | |
| 760 | /* Allocate stack space. */ |
| 761 | sp -= len; |
| 762 | |
| 763 | regsused = struct_return ? 1 : 0; |
| 764 | /* Push all arguments onto the stack. */ |
| 765 | for (argnum = 0; argnum < nargs; argnum++) |
| 766 | { |
| 767 | int len; |
| 768 | char *val; |
| 769 | |
| 770 | /* XXX Check this. What about UNIONS? */ |
| 771 | if (TYPE_CODE (VALUE_TYPE (*args)) == TYPE_CODE_STRUCT |
| 772 | && TYPE_LENGTH (VALUE_TYPE (*args)) > 8) |
| 773 | { |
| 774 | /* XXX Wrong, we want a pointer to this argument. */ |
| 775 | len = TYPE_LENGTH (VALUE_TYPE (*args)); |
| 776 | val = (char *)VALUE_CONTENTS (*args); |
| 777 | } |
| 778 | else |
| 779 | { |
| 780 | len = TYPE_LENGTH (VALUE_TYPE (*args)); |
| 781 | val = (char *)VALUE_CONTENTS (*args); |
| 782 | } |
| 783 | |
| 784 | if (regsused < 2 |
| 785 | && (len <= 2 |
| 786 | || TYPE_CODE (VALUE_TYPE (*args)) == TYPE_CODE_PTR)) |
| 787 | { |
| 788 | write_register (regsused, extract_unsigned_integer (val, 4)); |
| 789 | regsused++; |
| 790 | } |
| 791 | else if (regsused == 0 && len == 4) |
| 792 | { |
| 793 | write_register (regsused, extract_unsigned_integer (val, 2)); |
| 794 | write_register (regsused + 1, extract_unsigned_integer (val + 2, 2)); |
| 795 | regsused = 2; |
| 796 | } |
| 797 | else |
| 798 | { |
| 799 | regsused = 2; |
| 800 | while (len > 0) |
| 801 | { |
| 802 | write_memory (sp + stack_offset, val, 2); |
| 803 | |
| 804 | len -= 2; |
| 805 | val += 2; |
| 806 | stack_offset += 2; |
| 807 | } |
| 808 | } |
| 809 | args++; |
| 810 | } |
| 811 | |
| 812 | return sp; |
| 813 | } |
| 814 | |
| 815 | /* Function: push_return_address (pc) |
| 816 | Set up the return address for the inferior function call. |
| 817 | Needed for targets where we don't actually execute a JSR/BSR instruction */ |
| 818 | |
| 819 | CORE_ADDR |
| 820 | mn10200_push_return_address (pc, sp) |
| 821 | CORE_ADDR pc; |
| 822 | CORE_ADDR sp; |
| 823 | { |
| 824 | unsigned char buf[4]; |
| 825 | |
| 826 | store_unsigned_integer (buf, 4, CALL_DUMMY_ADDRESS ()); |
| 827 | write_memory (sp - 4, buf, 4); |
| 828 | return sp - 4; |
| 829 | } |
| 830 | |
| 831 | /* Function: store_struct_return (addr,sp) |
| 832 | Store the structure value return address for an inferior function |
| 833 | call. */ |
| 834 | |
| 835 | CORE_ADDR |
| 836 | mn10200_store_struct_return (addr, sp) |
| 837 | CORE_ADDR addr; |
| 838 | CORE_ADDR sp; |
| 839 | { |
| 840 | /* The structure return address is passed as the first argument. */ |
| 841 | write_register (0, addr); |
| 842 | return sp; |
| 843 | } |
| 844 | |
| 845 | /* Function: frame_saved_pc |
| 846 | Find the caller of this frame. We do this by seeing if RP_REGNUM |
| 847 | is saved in the stack anywhere, otherwise we get it from the |
| 848 | registers. If the inner frame is a dummy frame, return its PC |
| 849 | instead of RP, because that's where "caller" of the dummy-frame |
| 850 | will be found. */ |
| 851 | |
| 852 | CORE_ADDR |
| 853 | mn10200_frame_saved_pc (fi) |
| 854 | struct frame_info *fi; |
| 855 | { |
| 856 | /* The saved PC will always be at the base of the current frame. */ |
| 857 | return (read_memory_integer (fi->frame, REGISTER_SIZE) & 0xffffff); |
| 858 | } |
| 859 | |
| 860 | void |
| 861 | get_saved_register (raw_buffer, optimized, addrp, frame, regnum, lval) |
| 862 | char *raw_buffer; |
| 863 | int *optimized; |
| 864 | CORE_ADDR *addrp; |
| 865 | struct frame_info *frame; |
| 866 | int regnum; |
| 867 | enum lval_type *lval; |
| 868 | { |
| 869 | generic_get_saved_register (raw_buffer, optimized, addrp, |
| 870 | frame, regnum, lval); |
| 871 | } |
| 872 | |
| 873 | /* Function: init_extra_frame_info |
| 874 | Setup the frame's frame pointer, pc, and frame addresses for saved |
| 875 | registers. Most of the work is done in mn10200_analyze_prologue(). |
| 876 | |
| 877 | Note that when we are called for the last frame (currently active frame), |
| 878 | that fi->pc and fi->frame will already be setup. However, fi->frame will |
| 879 | be valid only if this routine uses FP. For previous frames, fi-frame will |
| 880 | always be correct. mn10200_analyze_prologue will fix fi->frame if |
| 881 | it's not valid. |
| 882 | |
| 883 | We can be called with the PC in the call dummy under two circumstances. |
| 884 | First, during normal backtracing, second, while figuring out the frame |
| 885 | pointer just prior to calling the target function (see run_stack_dummy). */ |
| 886 | |
| 887 | void |
| 888 | mn10200_init_extra_frame_info (fi) |
| 889 | struct frame_info *fi; |
| 890 | { |
| 891 | if (fi->next) |
| 892 | fi->pc = FRAME_SAVED_PC (fi->next); |
| 893 | |
| 894 | memset (fi->fsr.regs, '\000', sizeof fi->fsr.regs); |
| 895 | fi->status = 0; |
| 896 | fi->stack_size = 0; |
| 897 | |
| 898 | mn10200_analyze_prologue (fi, 0); |
| 899 | } |
| 900 | |
| 901 | void |
| 902 | _initialize_mn10200_tdep () |
| 903 | { |
| 904 | tm_print_insn = print_insn_mn10200; |
| 905 | } |
| 906 | |