| 1 | /* Target-machine dependent code for Zilog Z8000, for GDB. |
| 2 | |
| 3 | Copyright 1992, 1993, 1994, 1995, 1996, 1998, 1999, 2000, 2001, |
| 4 | 2002 Free Software Foundation, Inc. |
| 5 | |
| 6 | This file is part of GDB. |
| 7 | |
| 8 | This program is free software; you can redistribute it and/or modify |
| 9 | it under the terms of the GNU General Public License as published by |
| 10 | the Free Software Foundation; either version 2 of the License, or |
| 11 | (at your option) any later version. |
| 12 | |
| 13 | This program is distributed in the hope that it will be useful, |
| 14 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | GNU General Public License for more details. |
| 17 | |
| 18 | You should have received a copy of the GNU General Public License |
| 19 | along with this program; if not, write to the Free Software |
| 20 | Foundation, Inc., 59 Temple Place - Suite 330, |
| 21 | Boston, MA 02111-1307, USA. */ |
| 22 | |
| 23 | /* |
| 24 | Contributed by Steve Chamberlain |
| 25 | sac@cygnus.com |
| 26 | */ |
| 27 | |
| 28 | #include "defs.h" |
| 29 | #include "frame.h" |
| 30 | #include "symtab.h" |
| 31 | #include "gdbcmd.h" |
| 32 | #include "gdbtypes.h" |
| 33 | #include "dis-asm.h" |
| 34 | #include "gdbcore.h" |
| 35 | #include "regcache.h" |
| 36 | |
| 37 | #include "value.h" /* For read_register() */ |
| 38 | |
| 39 | |
| 40 | static int read_memory_pointer (CORE_ADDR x); |
| 41 | |
| 42 | /* Return the saved PC from this frame. |
| 43 | |
| 44 | If the frame has a memory copy of SRP_REGNUM, use that. If not, |
| 45 | just use the register SRP_REGNUM itself. */ |
| 46 | |
| 47 | CORE_ADDR |
| 48 | z8k_frame_saved_pc (struct frame_info *frame) |
| 49 | { |
| 50 | return read_memory_pointer (frame->frame + (BIG ? 4 : 2)); |
| 51 | } |
| 52 | |
| 53 | #define IS_PUSHL(x) (BIG ? ((x & 0xfff0) == 0x91e0):((x & 0xfff0) == 0x91F0)) |
| 54 | #define IS_PUSHW(x) (BIG ? ((x & 0xfff0) == 0x93e0):((x & 0xfff0)==0x93f0)) |
| 55 | #define IS_MOVE_FP(x) (BIG ? x == 0xa1ea : x == 0xa1fa) |
| 56 | #define IS_MOV_SP_FP(x) (BIG ? x == 0x94ea : x == 0x0d76) |
| 57 | #define IS_SUB2_SP(x) (x==0x1b87) |
| 58 | #define IS_MOVK_R5(x) (x==0x7905) |
| 59 | #define IS_SUB_SP(x) ((x & 0xffff) == 0x020f) |
| 60 | #define IS_PUSH_FP(x) (BIG ? (x == 0x93ea) : (x == 0x93fa)) |
| 61 | |
| 62 | /* work out how much local space is on the stack and |
| 63 | return the pc pointing to the first push */ |
| 64 | |
| 65 | static CORE_ADDR |
| 66 | skip_adjust (CORE_ADDR pc, int *size) |
| 67 | { |
| 68 | *size = 0; |
| 69 | |
| 70 | if (IS_PUSH_FP (read_memory_short (pc)) |
| 71 | && IS_MOV_SP_FP (read_memory_short (pc + 2))) |
| 72 | { |
| 73 | /* This is a function with an explict frame pointer */ |
| 74 | pc += 4; |
| 75 | *size += 2; /* remember the frame pointer */ |
| 76 | } |
| 77 | |
| 78 | /* remember any stack adjustment */ |
| 79 | if (IS_SUB_SP (read_memory_short (pc))) |
| 80 | { |
| 81 | *size += read_memory_short (pc + 2); |
| 82 | pc += 4; |
| 83 | } |
| 84 | return pc; |
| 85 | } |
| 86 | |
| 87 | static CORE_ADDR examine_frame (CORE_ADDR, CORE_ADDR * regs, CORE_ADDR); |
| 88 | static CORE_ADDR |
| 89 | examine_frame (CORE_ADDR pc, CORE_ADDR *regs, CORE_ADDR sp) |
| 90 | { |
| 91 | int w = read_memory_short (pc); |
| 92 | int offset = 0; |
| 93 | int regno; |
| 94 | |
| 95 | for (regno = 0; regno < NUM_REGS; regno++) |
| 96 | regs[regno] = 0; |
| 97 | |
| 98 | while (IS_PUSHW (w) || IS_PUSHL (w)) |
| 99 | { |
| 100 | /* work out which register is being pushed to where */ |
| 101 | if (IS_PUSHL (w)) |
| 102 | { |
| 103 | regs[w & 0xf] = offset; |
| 104 | regs[(w & 0xf) + 1] = offset + 2; |
| 105 | offset += 4; |
| 106 | } |
| 107 | else |
| 108 | { |
| 109 | regs[w & 0xf] = offset; |
| 110 | offset += 2; |
| 111 | } |
| 112 | pc += 2; |
| 113 | w = read_memory_short (pc); |
| 114 | } |
| 115 | |
| 116 | if (IS_MOVE_FP (w)) |
| 117 | { |
| 118 | /* We know the fp */ |
| 119 | |
| 120 | } |
| 121 | else if (IS_SUB_SP (w)) |
| 122 | { |
| 123 | /* Subtracting a value from the sp, so were in a function |
| 124 | which needs stack space for locals, but has no fp. We fake up |
| 125 | the values as if we had an fp */ |
| 126 | regs[FP_REGNUM] = sp; |
| 127 | } |
| 128 | else |
| 129 | { |
| 130 | /* This one didn't have an fp, we'll fake it up */ |
| 131 | regs[SP_REGNUM] = sp; |
| 132 | } |
| 133 | /* stack pointer contains address of next frame */ |
| 134 | /* regs[fp_regnum()] = fp; */ |
| 135 | regs[SP_REGNUM] = sp; |
| 136 | return pc; |
| 137 | } |
| 138 | |
| 139 | CORE_ADDR |
| 140 | z8k_skip_prologue (CORE_ADDR start_pc) |
| 141 | { |
| 142 | CORE_ADDR dummy[NUM_REGS]; |
| 143 | |
| 144 | return examine_frame (start_pc, dummy, 0); |
| 145 | } |
| 146 | |
| 147 | CORE_ADDR |
| 148 | z8k_addr_bits_remove (CORE_ADDR addr) |
| 149 | { |
| 150 | return (addr & PTR_MASK); |
| 151 | } |
| 152 | |
| 153 | static int |
| 154 | read_memory_pointer (CORE_ADDR x) |
| 155 | { |
| 156 | return read_memory_integer (ADDR_BITS_REMOVE (x), BIG ? 4 : 2); |
| 157 | } |
| 158 | |
| 159 | CORE_ADDR |
| 160 | z8k_frame_chain (struct frame_info *thisframe) |
| 161 | { |
| 162 | if (!inside_entry_file (thisframe->pc)) |
| 163 | { |
| 164 | return read_memory_pointer (thisframe->frame); |
| 165 | } |
| 166 | return 0; |
| 167 | } |
| 168 | |
| 169 | /* Put here the code to store, into a struct frame_saved_regs, |
| 170 | the addresses of the saved registers of frame described by FRAME_INFO. |
| 171 | This includes special registers such as pc and fp saved in special |
| 172 | ways in the stack frame. sp is even more special: |
| 173 | the address we return for it IS the sp for the next frame. */ |
| 174 | |
| 175 | void |
| 176 | z8k_frame_init_saved_regs (struct frame_info *frame_info) |
| 177 | { |
| 178 | CORE_ADDR pc; |
| 179 | int w; |
| 180 | |
| 181 | frame_saved_regs_zalloc (frame_info); |
| 182 | pc = get_pc_function_start (frame_info->pc); |
| 183 | |
| 184 | /* wander down the instruction stream */ |
| 185 | examine_frame (pc, frame_info->saved_regs, frame_info->frame); |
| 186 | |
| 187 | } |
| 188 | |
| 189 | void |
| 190 | z8k_push_dummy_frame (void) |
| 191 | { |
| 192 | internal_error (__FILE__, __LINE__, "failed internal consistency check"); |
| 193 | } |
| 194 | |
| 195 | int |
| 196 | gdb_print_insn_z8k (bfd_vma memaddr, disassemble_info *info) |
| 197 | { |
| 198 | if (BIG) |
| 199 | return print_insn_z8001 (memaddr, info); |
| 200 | else |
| 201 | return print_insn_z8002 (memaddr, info); |
| 202 | } |
| 203 | |
| 204 | /* Fetch the instruction at ADDR, returning 0 if ADDR is beyond LIM or |
| 205 | is not the address of a valid instruction, the address of the next |
| 206 | instruction beyond ADDR otherwise. *PWORD1 receives the first word |
| 207 | of the instruction. */ |
| 208 | |
| 209 | CORE_ADDR |
| 210 | NEXT_PROLOGUE_INSN (CORE_ADDR addr, CORE_ADDR lim, short *pword1) |
| 211 | { |
| 212 | char buf[2]; |
| 213 | if (addr < lim + 8) |
| 214 | { |
| 215 | read_memory (addr, buf, 2); |
| 216 | *pword1 = extract_signed_integer (buf, 2); |
| 217 | |
| 218 | return addr + 2; |
| 219 | } |
| 220 | return 0; |
| 221 | } |
| 222 | |
| 223 | #if 0 |
| 224 | /* Put here the code to store, into a struct frame_saved_regs, |
| 225 | the addresses of the saved registers of frame described by FRAME_INFO. |
| 226 | This includes special registers such as pc and fp saved in special |
| 227 | ways in the stack frame. sp is even more special: |
| 228 | the address we return for it IS the sp for the next frame. |
| 229 | |
| 230 | We cache the result of doing this in the frame_cache_obstack, since |
| 231 | it is fairly expensive. */ |
| 232 | |
| 233 | void |
| 234 | frame_find_saved_regs (struct frame_info *fip, struct frame_saved_regs *fsrp) |
| 235 | { |
| 236 | int locals; |
| 237 | CORE_ADDR pc; |
| 238 | CORE_ADDR adr; |
| 239 | int i; |
| 240 | |
| 241 | memset (fsrp, 0, sizeof *fsrp); |
| 242 | |
| 243 | pc = skip_adjust (get_pc_function_start (fip->pc), &locals); |
| 244 | |
| 245 | { |
| 246 | adr = get_frame_base (fip) - locals; |
| 247 | for (i = 0; i < 8; i++) |
| 248 | { |
| 249 | int word = read_memory_short (pc); |
| 250 | |
| 251 | pc += 2; |
| 252 | if (IS_PUSHL (word)) |
| 253 | { |
| 254 | fsrp->regs[word & 0xf] = adr; |
| 255 | fsrp->regs[(word & 0xf) + 1] = adr - 2; |
| 256 | adr -= 4; |
| 257 | } |
| 258 | else if (IS_PUSHW (word)) |
| 259 | { |
| 260 | fsrp->regs[word & 0xf] = adr; |
| 261 | adr -= 2; |
| 262 | } |
| 263 | else |
| 264 | break; |
| 265 | } |
| 266 | |
| 267 | } |
| 268 | |
| 269 | fsrp->regs[PC_REGNUM] = fip->frame + 4; |
| 270 | fsrp->regs[FP_REGNUM] = fip->frame; |
| 271 | |
| 272 | } |
| 273 | #endif |
| 274 | |
| 275 | int |
| 276 | z8k_saved_pc_after_call (struct frame_info *frame) |
| 277 | { |
| 278 | return ADDR_BITS_REMOVE |
| 279 | (read_memory_integer (read_register (SP_REGNUM), PTR_SIZE)); |
| 280 | } |
| 281 | |
| 282 | |
| 283 | void |
| 284 | extract_return_value (struct type *type, char *regbuf, char *valbuf) |
| 285 | { |
| 286 | int b; |
| 287 | int len = TYPE_LENGTH (type); |
| 288 | |
| 289 | for (b = 0; b < len; b += 2) |
| 290 | { |
| 291 | int todo = len - b; |
| 292 | |
| 293 | if (todo > 2) |
| 294 | todo = 2; |
| 295 | memcpy (valbuf + b, regbuf + b, todo); |
| 296 | } |
| 297 | } |
| 298 | |
| 299 | void |
| 300 | write_return_value (struct type *type, char *valbuf) |
| 301 | { |
| 302 | int reg; |
| 303 | int len; |
| 304 | |
| 305 | for (len = 0; len < TYPE_LENGTH (type); len += 2) |
| 306 | deprecated_write_register_bytes (REGISTER_BYTE (len / 2 + 2), |
| 307 | valbuf + len, 2); |
| 308 | } |
| 309 | |
| 310 | void |
| 311 | store_struct_return (CORE_ADDR addr, CORE_ADDR sp) |
| 312 | { |
| 313 | write_register (2, addr); |
| 314 | } |
| 315 | |
| 316 | |
| 317 | static void |
| 318 | z8k_print_register_hook (int regno) |
| 319 | { |
| 320 | if ((regno & 1) == 0 && regno < 16) |
| 321 | { |
| 322 | unsigned char l[4]; |
| 323 | |
| 324 | frame_register_read (deprecated_selected_frame, regno, l + 0); |
| 325 | frame_register_read (deprecated_selected_frame, regno + 1, l + 2); |
| 326 | printf_unfiltered ("\t"); |
| 327 | printf_unfiltered ("0x%02x%02x%02x%02x", l[0], l[1], l[2], l[3]); |
| 328 | } |
| 329 | |
| 330 | if ((regno & 3) == 0 && regno < 16) |
| 331 | { |
| 332 | unsigned char l[8]; |
| 333 | |
| 334 | frame_register_read (deprecated_selected_frame, regno, l + 0); |
| 335 | frame_register_read (deprecated_selected_frame, regno + 1, l + 2); |
| 336 | frame_register_read (deprecated_selected_frame, regno + 2, l + 4); |
| 337 | frame_register_read (deprecated_selected_frame, regno + 3, l + 6); |
| 338 | |
| 339 | printf_unfiltered ("\t"); |
| 340 | printf_unfiltered ("0x%02x%02x%02x%02x%02x%02x%02x%02x", |
| 341 | l[0], l[1], l[2], l[3], l[4], l[5], l[6], l[7]); |
| 342 | } |
| 343 | if (regno == 15) |
| 344 | { |
| 345 | unsigned short rval; |
| 346 | int i; |
| 347 | |
| 348 | frame_register_read (deprecated_selected_frame, regno, (char *) (&rval)); |
| 349 | |
| 350 | printf_unfiltered ("\n"); |
| 351 | for (i = 0; i < 10; i += 2) |
| 352 | { |
| 353 | printf_unfiltered ("(sp+%d=%04x)", i, |
| 354 | (unsigned int)read_memory_short (rval + i)); |
| 355 | } |
| 356 | } |
| 357 | } |
| 358 | |
| 359 | static void |
| 360 | z8k_print_registers_info (struct gdbarch *gdbarch, |
| 361 | struct ui_file *file, |
| 362 | struct frame_info *frame, |
| 363 | int regnum, int print_all) |
| 364 | { |
| 365 | int i; |
| 366 | const int numregs = NUM_REGS + NUM_PSEUDO_REGS; |
| 367 | char *raw_buffer = alloca (MAX_REGISTER_RAW_SIZE); |
| 368 | char *virtual_buffer = alloca (MAX_REGISTER_VIRTUAL_SIZE); |
| 369 | |
| 370 | for (i = 0; i < numregs; i++) |
| 371 | { |
| 372 | /* Decide between printing all regs, non-float / vector regs, or |
| 373 | specific reg. */ |
| 374 | if (regnum == -1) |
| 375 | { |
| 376 | if (!print_all) |
| 377 | { |
| 378 | if (TYPE_CODE (REGISTER_VIRTUAL_TYPE (i)) == TYPE_CODE_FLT) |
| 379 | continue; |
| 380 | if (TYPE_VECTOR (REGISTER_VIRTUAL_TYPE (i))) |
| 381 | continue; |
| 382 | } |
| 383 | } |
| 384 | else |
| 385 | { |
| 386 | if (i != regnum) |
| 387 | continue; |
| 388 | } |
| 389 | |
| 390 | /* If the register name is empty, it is undefined for this |
| 391 | processor, so don't display anything. */ |
| 392 | if (REGISTER_NAME (i) == NULL || *(REGISTER_NAME (i)) == '\0') |
| 393 | continue; |
| 394 | |
| 395 | fputs_filtered (REGISTER_NAME (i), file); |
| 396 | print_spaces_filtered (15 - strlen (REGISTER_NAME (i)), file); |
| 397 | |
| 398 | /* Get the data in raw format. */ |
| 399 | if (! frame_register_read (frame, i, raw_buffer)) |
| 400 | { |
| 401 | fprintf_filtered (file, "*value not available*\n"); |
| 402 | continue; |
| 403 | } |
| 404 | |
| 405 | /* FIXME: cagney/2002-08-03: This code shouldn't be necessary. |
| 406 | The function frame_register_read() should have returned the |
| 407 | pre-cooked register so no conversion is necessary. */ |
| 408 | /* Convert raw data to virtual format if necessary. */ |
| 409 | if (REGISTER_CONVERTIBLE (i)) |
| 410 | { |
| 411 | REGISTER_CONVERT_TO_VIRTUAL (i, REGISTER_VIRTUAL_TYPE (i), |
| 412 | raw_buffer, virtual_buffer); |
| 413 | } |
| 414 | else |
| 415 | { |
| 416 | memcpy (virtual_buffer, raw_buffer, |
| 417 | REGISTER_VIRTUAL_SIZE (i)); |
| 418 | } |
| 419 | |
| 420 | /* If virtual format is floating, print it that way, and in raw |
| 421 | hex. */ |
| 422 | if (TYPE_CODE (REGISTER_VIRTUAL_TYPE (i)) == TYPE_CODE_FLT) |
| 423 | { |
| 424 | int j; |
| 425 | |
| 426 | val_print (REGISTER_VIRTUAL_TYPE (i), virtual_buffer, 0, 0, |
| 427 | file, 0, 1, 0, Val_pretty_default); |
| 428 | |
| 429 | fprintf_filtered (file, "\t(raw 0x"); |
| 430 | for (j = 0; j < REGISTER_RAW_SIZE (i); j++) |
| 431 | { |
| 432 | int idx; |
| 433 | if (TARGET_BYTE_ORDER == BFD_ENDIAN_BIG) |
| 434 | idx = j; |
| 435 | else |
| 436 | idx = REGISTER_RAW_SIZE (i) - 1 - j; |
| 437 | fprintf_filtered (file, "%02x", (unsigned char) raw_buffer[idx]); |
| 438 | } |
| 439 | fprintf_filtered (file, ")"); |
| 440 | } |
| 441 | else |
| 442 | { |
| 443 | /* Print the register in hex. */ |
| 444 | val_print (REGISTER_VIRTUAL_TYPE (i), virtual_buffer, 0, 0, |
| 445 | file, 'x', 1, 0, Val_pretty_default); |
| 446 | /* If not a vector register, print it also according to its |
| 447 | natural format. */ |
| 448 | if (TYPE_VECTOR (REGISTER_VIRTUAL_TYPE (i)) == 0) |
| 449 | { |
| 450 | fprintf_filtered (file, "\t"); |
| 451 | val_print (REGISTER_VIRTUAL_TYPE (i), virtual_buffer, 0, 0, |
| 452 | file, 0, 1, 0, Val_pretty_default); |
| 453 | } |
| 454 | } |
| 455 | |
| 456 | /* Some z8k specific info. */ |
| 457 | z8k_print_register_hook (i); |
| 458 | |
| 459 | fprintf_filtered (file, "\n"); |
| 460 | } |
| 461 | } |
| 462 | |
| 463 | void |
| 464 | z8k_do_registers_info (int regnum, int all) |
| 465 | { |
| 466 | z8k_print_registers_info (current_gdbarch, gdb_stdout, |
| 467 | deprecated_selected_frame, regnum, all); |
| 468 | } |
| 469 | |
| 470 | void |
| 471 | z8k_pop_frame (void) |
| 472 | { |
| 473 | } |
| 474 | |
| 475 | struct cmd_list_element *setmemorylist; |
| 476 | |
| 477 | void |
| 478 | z8k_set_pointer_size (int newsize) |
| 479 | { |
| 480 | static int oldsize = 0; |
| 481 | |
| 482 | if (oldsize != newsize) |
| 483 | { |
| 484 | printf_unfiltered ("pointer size set to %d bits\n", newsize); |
| 485 | oldsize = newsize; |
| 486 | if (newsize == 32) |
| 487 | { |
| 488 | BIG = 1; |
| 489 | } |
| 490 | else |
| 491 | { |
| 492 | BIG = 0; |
| 493 | } |
| 494 | /* FIXME: This code should be using the GDBARCH framework to |
| 495 | handle changed type sizes. If this problem is ever fixed |
| 496 | (the direct reference to _initialize_gdbtypes() below |
| 497 | eliminated) then Makefile.in should be updated so that |
| 498 | z8k-tdep.c is again compiled with -Werror. */ |
| 499 | _initialize_gdbtypes (); |
| 500 | } |
| 501 | } |
| 502 | |
| 503 | static void |
| 504 | segmented_command (char *args, int from_tty) |
| 505 | { |
| 506 | z8k_set_pointer_size (32); |
| 507 | } |
| 508 | |
| 509 | static void |
| 510 | unsegmented_command (char *args, int from_tty) |
| 511 | { |
| 512 | z8k_set_pointer_size (16); |
| 513 | } |
| 514 | |
| 515 | static void |
| 516 | set_memory (char *args, int from_tty) |
| 517 | { |
| 518 | printf_unfiltered ("\"set memory\" must be followed by the name of a memory subcommand.\n"); |
| 519 | help_list (setmemorylist, "set memory ", -1, gdb_stdout); |
| 520 | } |
| 521 | |
| 522 | void |
| 523 | _initialize_z8ktdep (void) |
| 524 | { |
| 525 | tm_print_insn = gdb_print_insn_z8k; |
| 526 | |
| 527 | add_prefix_cmd ("memory", no_class, set_memory, |
| 528 | "set the memory model", &setmemorylist, "set memory ", 0, |
| 529 | &setlist); |
| 530 | add_cmd ("segmented", class_support, segmented_command, |
| 531 | "Set segmented memory model.", &setmemorylist); |
| 532 | add_cmd ("unsegmented", class_support, unsegmented_command, |
| 533 | "Set unsegmented memory model.", &setmemorylist); |
| 534 | |
| 535 | } |