| 1 | /* Target-machine dependent code for Zilog Z8000, for GDB. |
| 2 | Copyright (C) 1992, 1993, 1994 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, |
| 19 | Boston, MA 02111-1307, USA. */ |
| 20 | |
| 21 | /* |
| 22 | Contributed by Steve Chamberlain |
| 23 | sac@cygnus.com |
| 24 | */ |
| 25 | |
| 26 | #include "defs.h" |
| 27 | #include "frame.h" |
| 28 | #include "obstack.h" |
| 29 | #include "symtab.h" |
| 30 | #include "gdbcmd.h" |
| 31 | #include "gdbtypes.h" |
| 32 | #include "dis-asm.h" |
| 33 | #include "gdbcore.h" |
| 34 | |
| 35 | #include "value.h" /* For read_register() */ |
| 36 | |
| 37 | |
| 38 | static int read_memory_pointer (CORE_ADDR x); |
| 39 | |
| 40 | /* Return the saved PC from this frame. |
| 41 | |
| 42 | If the frame has a memory copy of SRP_REGNUM, use that. If not, |
| 43 | just use the register SRP_REGNUM itself. */ |
| 44 | |
| 45 | CORE_ADDR |
| 46 | z8k_frame_saved_pc (struct frame_info *frame) |
| 47 | { |
| 48 | return read_memory_pointer (frame->frame + (BIG ? 4 : 2)); |
| 49 | } |
| 50 | |
| 51 | #define IS_PUSHL(x) (BIG ? ((x & 0xfff0) == 0x91e0):((x & 0xfff0) == 0x91F0)) |
| 52 | #define IS_PUSHW(x) (BIG ? ((x & 0xfff0) == 0x93e0):((x & 0xfff0)==0x93f0)) |
| 53 | #define IS_MOVE_FP(x) (BIG ? x == 0xa1ea : x == 0xa1fa) |
| 54 | #define IS_MOV_SP_FP(x) (BIG ? x == 0x94ea : x == 0x0d76) |
| 55 | #define IS_SUB2_SP(x) (x==0x1b87) |
| 56 | #define IS_MOVK_R5(x) (x==0x7905) |
| 57 | #define IS_SUB_SP(x) ((x & 0xffff) == 0x020f) |
| 58 | #define IS_PUSH_FP(x) (BIG ? (x == 0x93ea) : (x == 0x93fa)) |
| 59 | |
| 60 | /* work out how much local space is on the stack and |
| 61 | return the pc pointing to the first push */ |
| 62 | |
| 63 | static CORE_ADDR |
| 64 | skip_adjust (CORE_ADDR pc, int *size) |
| 65 | { |
| 66 | *size = 0; |
| 67 | |
| 68 | if (IS_PUSH_FP (read_memory_short (pc)) |
| 69 | && IS_MOV_SP_FP (read_memory_short (pc + 2))) |
| 70 | { |
| 71 | /* This is a function with an explict frame pointer */ |
| 72 | pc += 4; |
| 73 | *size += 2; /* remember the frame pointer */ |
| 74 | } |
| 75 | |
| 76 | /* remember any stack adjustment */ |
| 77 | if (IS_SUB_SP (read_memory_short (pc))) |
| 78 | { |
| 79 | *size += read_memory_short (pc + 2); |
| 80 | pc += 4; |
| 81 | } |
| 82 | return pc; |
| 83 | } |
| 84 | |
| 85 | static CORE_ADDR examine_frame (CORE_ADDR, CORE_ADDR * regs, CORE_ADDR); |
| 86 | static CORE_ADDR |
| 87 | examine_frame (CORE_ADDR pc, CORE_ADDR *regs, CORE_ADDR sp) |
| 88 | { |
| 89 | int w = read_memory_short (pc); |
| 90 | int offset = 0; |
| 91 | int regno; |
| 92 | |
| 93 | for (regno = 0; regno < NUM_REGS; regno++) |
| 94 | regs[regno] = 0; |
| 95 | |
| 96 | while (IS_PUSHW (w) || IS_PUSHL (w)) |
| 97 | { |
| 98 | /* work out which register is being pushed to where */ |
| 99 | if (IS_PUSHL (w)) |
| 100 | { |
| 101 | regs[w & 0xf] = offset; |
| 102 | regs[(w & 0xf) + 1] = offset + 2; |
| 103 | offset += 4; |
| 104 | } |
| 105 | else |
| 106 | { |
| 107 | regs[w & 0xf] = offset; |
| 108 | offset += 2; |
| 109 | } |
| 110 | pc += 2; |
| 111 | w = read_memory_short (pc); |
| 112 | } |
| 113 | |
| 114 | if (IS_MOVE_FP (w)) |
| 115 | { |
| 116 | /* We know the fp */ |
| 117 | |
| 118 | } |
| 119 | else if (IS_SUB_SP (w)) |
| 120 | { |
| 121 | /* Subtracting a value from the sp, so were in a function |
| 122 | which needs stack space for locals, but has no fp. We fake up |
| 123 | the values as if we had an fp */ |
| 124 | regs[FP_REGNUM] = sp; |
| 125 | } |
| 126 | else |
| 127 | { |
| 128 | /* This one didn't have an fp, we'll fake it up */ |
| 129 | regs[SP_REGNUM] = sp; |
| 130 | } |
| 131 | /* stack pointer contains address of next frame */ |
| 132 | /* regs[fp_regnum()] = fp; */ |
| 133 | regs[SP_REGNUM] = sp; |
| 134 | return pc; |
| 135 | } |
| 136 | |
| 137 | CORE_ADDR |
| 138 | z8k_skip_prologue (CORE_ADDR start_pc) |
| 139 | { |
| 140 | CORE_ADDR dummy[NUM_REGS]; |
| 141 | |
| 142 | return examine_frame (start_pc, dummy, 0); |
| 143 | } |
| 144 | |
| 145 | CORE_ADDR |
| 146 | z8k_addr_bits_remove (CORE_ADDR addr) |
| 147 | { |
| 148 | return (addr & PTR_MASK); |
| 149 | } |
| 150 | |
| 151 | static int |
| 152 | read_memory_pointer (CORE_ADDR x) |
| 153 | { |
| 154 | return read_memory_integer (ADDR_BITS_REMOVE (x), BIG ? 4 : 2); |
| 155 | } |
| 156 | |
| 157 | CORE_ADDR |
| 158 | z8k_frame_chain (struct frame_info *thisframe) |
| 159 | { |
| 160 | if (thisframe->prev == 0) |
| 161 | { |
| 162 | /* This is the top of the stack, let's get the sp for real */ |
| 163 | } |
| 164 | if (!inside_entry_file (thisframe->pc)) |
| 165 | { |
| 166 | return read_memory_pointer (thisframe->frame); |
| 167 | } |
| 168 | return 0; |
| 169 | } |
| 170 | |
| 171 | void |
| 172 | init_frame_pc (void) |
| 173 | { |
| 174 | abort (); |
| 175 | } |
| 176 | |
| 177 | /* Put here the code to store, into a struct frame_saved_regs, |
| 178 | the addresses of the saved registers of frame described by FRAME_INFO. |
| 179 | This includes special registers such as pc and fp saved in special |
| 180 | ways in the stack frame. sp is even more special: |
| 181 | the address we return for it IS the sp for the next frame. */ |
| 182 | |
| 183 | void |
| 184 | z8k_frame_init_saved_regs (struct frame_info *frame_info) |
| 185 | { |
| 186 | CORE_ADDR pc; |
| 187 | int w; |
| 188 | |
| 189 | frame_saved_regs_zalloc (frame_info); |
| 190 | pc = get_pc_function_start (frame_info->pc); |
| 191 | |
| 192 | /* wander down the instruction stream */ |
| 193 | examine_frame (pc, frame_info->saved_regs, frame_info->frame); |
| 194 | |
| 195 | } |
| 196 | |
| 197 | void |
| 198 | z8k_push_dummy_frame (void) |
| 199 | { |
| 200 | abort (); |
| 201 | } |
| 202 | |
| 203 | int |
| 204 | gdb_print_insn_z8k (bfd_vma memaddr, disassemble_info *info) |
| 205 | { |
| 206 | if (BIG) |
| 207 | return print_insn_z8001 (memaddr, info); |
| 208 | else |
| 209 | return print_insn_z8002 (memaddr, info); |
| 210 | } |
| 211 | |
| 212 | /* Fetch the instruction at ADDR, returning 0 if ADDR is beyond LIM or |
| 213 | is not the address of a valid instruction, the address of the next |
| 214 | instruction beyond ADDR otherwise. *PWORD1 receives the first word |
| 215 | of the instruction. */ |
| 216 | |
| 217 | CORE_ADDR |
| 218 | NEXT_PROLOGUE_INSN (CORE_ADDR addr, CORE_ADDR lim, short *pword1) |
| 219 | { |
| 220 | char buf[2]; |
| 221 | if (addr < lim + 8) |
| 222 | { |
| 223 | read_memory (addr, buf, 2); |
| 224 | *pword1 = extract_signed_integer (buf, 2); |
| 225 | |
| 226 | return addr + 2; |
| 227 | } |
| 228 | return 0; |
| 229 | } |
| 230 | |
| 231 | #if 0 |
| 232 | /* Put here the code to store, into a struct frame_saved_regs, |
| 233 | the addresses of the saved registers of frame described by FRAME_INFO. |
| 234 | This includes special registers such as pc and fp saved in special |
| 235 | ways in the stack frame. sp is even more special: |
| 236 | the address we return for it IS the sp for the next frame. |
| 237 | |
| 238 | We cache the result of doing this in the frame_cache_obstack, since |
| 239 | it is fairly expensive. */ |
| 240 | |
| 241 | void |
| 242 | frame_find_saved_regs (struct frame_info *fip, struct frame_saved_regs *fsrp) |
| 243 | { |
| 244 | int locals; |
| 245 | CORE_ADDR pc; |
| 246 | CORE_ADDR adr; |
| 247 | int i; |
| 248 | |
| 249 | memset (fsrp, 0, sizeof *fsrp); |
| 250 | |
| 251 | pc = skip_adjust (get_pc_function_start (fip->pc), &locals); |
| 252 | |
| 253 | { |
| 254 | adr = FRAME_FP (fip) - locals; |
| 255 | for (i = 0; i < 8; i++) |
| 256 | { |
| 257 | int word = read_memory_short (pc); |
| 258 | |
| 259 | pc += 2; |
| 260 | if (IS_PUSHL (word)) |
| 261 | { |
| 262 | fsrp->regs[word & 0xf] = adr; |
| 263 | fsrp->regs[(word & 0xf) + 1] = adr - 2; |
| 264 | adr -= 4; |
| 265 | } |
| 266 | else if (IS_PUSHW (word)) |
| 267 | { |
| 268 | fsrp->regs[word & 0xf] = adr; |
| 269 | adr -= 2; |
| 270 | } |
| 271 | else |
| 272 | break; |
| 273 | } |
| 274 | |
| 275 | } |
| 276 | |
| 277 | fsrp->regs[PC_REGNUM] = fip->frame + 4; |
| 278 | fsrp->regs[FP_REGNUM] = fip->frame; |
| 279 | |
| 280 | } |
| 281 | #endif |
| 282 | |
| 283 | int |
| 284 | z8k_saved_pc_after_call (struct frame_info *frame) |
| 285 | { |
| 286 | return ADDR_BITS_REMOVE |
| 287 | (read_memory_integer (read_register (SP_REGNUM), PTR_SIZE)); |
| 288 | } |
| 289 | |
| 290 | |
| 291 | void |
| 292 | extract_return_value (struct type *type, char *regbuf, char *valbuf) |
| 293 | { |
| 294 | int b; |
| 295 | int len = TYPE_LENGTH (type); |
| 296 | |
| 297 | for (b = 0; b < len; b += 2) |
| 298 | { |
| 299 | int todo = len - b; |
| 300 | |
| 301 | if (todo > 2) |
| 302 | todo = 2; |
| 303 | memcpy (valbuf + b, regbuf + b, todo); |
| 304 | } |
| 305 | } |
| 306 | |
| 307 | void |
| 308 | write_return_value (struct type *type, char *valbuf) |
| 309 | { |
| 310 | int reg; |
| 311 | int len; |
| 312 | |
| 313 | for (len = 0; len < TYPE_LENGTH (type); len += 2) |
| 314 | write_register_bytes (REGISTER_BYTE (len / 2 + 2), valbuf + len, 2); |
| 315 | } |
| 316 | |
| 317 | void |
| 318 | store_struct_return (CORE_ADDR addr, CORE_ADDR sp) |
| 319 | { |
| 320 | write_register (2, addr); |
| 321 | } |
| 322 | |
| 323 | |
| 324 | void |
| 325 | z8k_print_register_hook (int regno) |
| 326 | { |
| 327 | if ((regno & 1) == 0 && regno < 16) |
| 328 | { |
| 329 | unsigned short l[2]; |
| 330 | |
| 331 | read_relative_register_raw_bytes (regno, (char *) (l + 0)); |
| 332 | read_relative_register_raw_bytes (regno + 1, (char *) (l + 1)); |
| 333 | printf_unfiltered ("\t"); |
| 334 | printf_unfiltered ("%04x%04x", l[0], l[1]); |
| 335 | } |
| 336 | |
| 337 | if ((regno & 3) == 0 && regno < 16) |
| 338 | { |
| 339 | unsigned short l[4]; |
| 340 | |
| 341 | read_relative_register_raw_bytes (regno, (char *) (l + 0)); |
| 342 | read_relative_register_raw_bytes (regno + 1, (char *) (l + 1)); |
| 343 | read_relative_register_raw_bytes (regno + 2, (char *) (l + 2)); |
| 344 | read_relative_register_raw_bytes (regno + 3, (char *) (l + 3)); |
| 345 | |
| 346 | printf_unfiltered ("\t"); |
| 347 | printf_unfiltered ("%04x%04x%04x%04x", l[0], l[1], l[2], l[3]); |
| 348 | } |
| 349 | if (regno == 15) |
| 350 | { |
| 351 | unsigned short rval; |
| 352 | int i; |
| 353 | |
| 354 | read_relative_register_raw_bytes (regno, (char *) (&rval)); |
| 355 | |
| 356 | printf_unfiltered ("\n"); |
| 357 | for (i = 0; i < 10; i += 2) |
| 358 | { |
| 359 | printf_unfiltered ("(sp+%d=%04x)", i, |
| 360 | (unsigned int)read_memory_short (rval + i)); |
| 361 | } |
| 362 | } |
| 363 | |
| 364 | } |
| 365 | |
| 366 | void |
| 367 | z8k_pop_frame (void) |
| 368 | { |
| 369 | } |
| 370 | |
| 371 | struct cmd_list_element *setmemorylist; |
| 372 | |
| 373 | void |
| 374 | z8k_set_pointer_size (int newsize) |
| 375 | { |
| 376 | static int oldsize = 0; |
| 377 | |
| 378 | if (oldsize != newsize) |
| 379 | { |
| 380 | printf_unfiltered ("pointer size set to %d bits\n", newsize); |
| 381 | oldsize = newsize; |
| 382 | if (newsize == 32) |
| 383 | { |
| 384 | BIG = 1; |
| 385 | } |
| 386 | else |
| 387 | { |
| 388 | BIG = 0; |
| 389 | } |
| 390 | /* FIXME: This code should be using the GDBARCH framework to |
| 391 | handle changed type sizes. If this problem is ever fixed |
| 392 | (the direct reference to _initialize_gdbtypes() below |
| 393 | eliminated) then Makefile.in should be updated so that |
| 394 | z8k-tdep.c is again compiled with -Werror. */ |
| 395 | _initialize_gdbtypes (); |
| 396 | } |
| 397 | } |
| 398 | |
| 399 | static void |
| 400 | segmented_command (char *args, int from_tty) |
| 401 | { |
| 402 | z8k_set_pointer_size (32); |
| 403 | } |
| 404 | |
| 405 | static void |
| 406 | unsegmented_command (char *args, int from_tty) |
| 407 | { |
| 408 | z8k_set_pointer_size (16); |
| 409 | } |
| 410 | |
| 411 | static void |
| 412 | set_memory (char *args, int from_tty) |
| 413 | { |
| 414 | printf_unfiltered ("\"set memory\" must be followed by the name of a memory subcommand.\n"); |
| 415 | help_list (setmemorylist, "set memory ", -1, gdb_stdout); |
| 416 | } |
| 417 | |
| 418 | void |
| 419 | _initialize_z8ktdep (void) |
| 420 | { |
| 421 | tm_print_insn = gdb_print_insn_z8k; |
| 422 | |
| 423 | add_prefix_cmd ("memory", no_class, set_memory, |
| 424 | "set the memory model", &setmemorylist, "set memory ", 0, |
| 425 | &setlist); |
| 426 | add_cmd ("segmented", class_support, segmented_command, |
| 427 | "Set segmented memory model.", &setmemorylist); |
| 428 | add_cmd ("unsegmented", class_support, unsegmented_command, |
| 429 | "Set unsegmented memory model.", &setmemorylist); |
| 430 | |
| 431 | } |