| 1 | /* Target-machine dependent code for Hitachi H8/300, for GDB. |
| 2 | Copyright 1988, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1998, 1999, |
| 3 | 2000, 2001 Free Software Foundation, Inc. |
| 4 | |
| 5 | This file is part of GDB. |
| 6 | |
| 7 | This program is free software; you can redistribute it and/or modify |
| 8 | it under the terms of the GNU General Public License as published by |
| 9 | the Free Software Foundation; either version 2 of the License, or |
| 10 | (at your option) any later version. |
| 11 | |
| 12 | This program is distributed in the hope that it will be useful, |
| 13 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | GNU General Public License for more details. |
| 16 | |
| 17 | You should have received a copy of the GNU General Public License |
| 18 | along with this program; if not, write to the Free Software |
| 19 | Foundation, Inc., 59 Temple Place - Suite 330, |
| 20 | Boston, MA 02111-1307, USA. */ |
| 21 | |
| 22 | /* |
| 23 | Contributed by Steve Chamberlain |
| 24 | sac@cygnus.com |
| 25 | */ |
| 26 | |
| 27 | #include "defs.h" |
| 28 | #include "frame.h" |
| 29 | #include "obstack.h" |
| 30 | #include "symtab.h" |
| 31 | #include "dis-asm.h" |
| 32 | #include "gdbcmd.h" |
| 33 | #include "gdbtypes.h" |
| 34 | #include "gdbcore.h" |
| 35 | #include "gdb_string.h" |
| 36 | #include "value.h" |
| 37 | #include "regcache.h" |
| 38 | |
| 39 | extern int h8300hmode, h8300smode; |
| 40 | |
| 41 | #undef NUM_REGS |
| 42 | #define NUM_REGS 11 |
| 43 | |
| 44 | #define UNSIGNED_SHORT(X) ((X) & 0xffff) |
| 45 | |
| 46 | #define IS_PUSH(x) ((x & 0xfff0)==0x6df0) |
| 47 | #define IS_PUSH_FP(x) (x == 0x6df6) |
| 48 | #define IS_MOVE_FP(x) (x == 0x0d76 || x == 0x0ff6) |
| 49 | #define IS_MOV_SP_FP(x) (x == 0x0d76 || x == 0x0ff6) |
| 50 | #define IS_SUB2_SP(x) (x==0x1b87) |
| 51 | #define IS_SUB4_SP(x) (x==0x1b97) |
| 52 | #define IS_SUBL_SP(x) (x==0x7a37) |
| 53 | #define IS_MOVK_R5(x) (x==0x7905) |
| 54 | #define IS_SUB_R5SP(x) (x==0x1957) |
| 55 | |
| 56 | |
| 57 | /* The register names change depending on whether the h8300h processor |
| 58 | type is selected. */ |
| 59 | |
| 60 | static char *original_register_names[] = REGISTER_NAMES; |
| 61 | |
| 62 | static char *h8300h_register_names[] = |
| 63 | {"er0", "er1", "er2", "er3", "er4", "er5", "er6", |
| 64 | "sp", "ccr", "pc", "cycles", "tick", "inst"}; |
| 65 | |
| 66 | char **h8300_register_names = original_register_names; |
| 67 | |
| 68 | |
| 69 | /* Local function declarations. */ |
| 70 | |
| 71 | static CORE_ADDR examine_prologue (); |
| 72 | static void set_machine_hook (char *filename); |
| 73 | |
| 74 | CORE_ADDR |
| 75 | h8300_skip_prologue (CORE_ADDR start_pc) |
| 76 | { |
| 77 | short int w; |
| 78 | int adjust = 0; |
| 79 | |
| 80 | /* Skip past all push and stm insns. */ |
| 81 | while (1) |
| 82 | { |
| 83 | w = read_memory_unsigned_integer (start_pc, 2); |
| 84 | /* First look for push insns. */ |
| 85 | if (w == 0x0100 || w == 0x0110 || w == 0x0120 || w == 0x0130) |
| 86 | { |
| 87 | w = read_memory_unsigned_integer (start_pc + 2, 2); |
| 88 | adjust = 2; |
| 89 | } |
| 90 | |
| 91 | if (IS_PUSH (w)) |
| 92 | { |
| 93 | start_pc += 2 + adjust; |
| 94 | w = read_memory_unsigned_integer (start_pc, 2); |
| 95 | continue; |
| 96 | } |
| 97 | adjust = 0; |
| 98 | break; |
| 99 | } |
| 100 | |
| 101 | /* Skip past a move to FP, either word or long sized */ |
| 102 | w = read_memory_unsigned_integer (start_pc, 2); |
| 103 | if (w == 0x0100) |
| 104 | { |
| 105 | w = read_memory_unsigned_integer (start_pc + 2, 2); |
| 106 | adjust += 2; |
| 107 | } |
| 108 | |
| 109 | if (IS_MOVE_FP (w)) |
| 110 | { |
| 111 | start_pc += 2 + adjust; |
| 112 | w = read_memory_unsigned_integer (start_pc, 2); |
| 113 | } |
| 114 | |
| 115 | /* Check for loading either a word constant into r5; |
| 116 | long versions are handled by the SUBL_SP below. */ |
| 117 | if (IS_MOVK_R5 (w)) |
| 118 | { |
| 119 | start_pc += 2; |
| 120 | w = read_memory_unsigned_integer (start_pc, 2); |
| 121 | } |
| 122 | |
| 123 | /* Now check for subtracting r5 from sp, word sized only. */ |
| 124 | if (IS_SUB_R5SP (w)) |
| 125 | { |
| 126 | start_pc += 2 + adjust; |
| 127 | w = read_memory_unsigned_integer (start_pc, 2); |
| 128 | } |
| 129 | |
| 130 | /* Check for subs #2 and subs #4. */ |
| 131 | while (IS_SUB2_SP (w) || IS_SUB4_SP (w)) |
| 132 | { |
| 133 | start_pc += 2 + adjust; |
| 134 | w = read_memory_unsigned_integer (start_pc, 2); |
| 135 | } |
| 136 | |
| 137 | /* Check for a 32bit subtract. */ |
| 138 | if (IS_SUBL_SP (w)) |
| 139 | start_pc += 6 + adjust; |
| 140 | |
| 141 | return start_pc; |
| 142 | } |
| 143 | |
| 144 | int |
| 145 | gdb_print_insn_h8300 (bfd_vma memaddr, disassemble_info *info) |
| 146 | { |
| 147 | if (h8300smode) |
| 148 | return print_insn_h8300s (memaddr, info); |
| 149 | else if (h8300hmode) |
| 150 | return print_insn_h8300h (memaddr, info); |
| 151 | else |
| 152 | return print_insn_h8300 (memaddr, info); |
| 153 | } |
| 154 | |
| 155 | /* Given a GDB frame, determine the address of the calling function's frame. |
| 156 | This will be used to create a new GDB frame struct, and then |
| 157 | INIT_EXTRA_FRAME_INFO and INIT_FRAME_PC will be called for the new frame. |
| 158 | |
| 159 | For us, the frame address is its stack pointer value, so we look up |
| 160 | the function prologue to determine the caller's sp value, and return it. */ |
| 161 | |
| 162 | CORE_ADDR |
| 163 | h8300_frame_chain (struct frame_info *thisframe) |
| 164 | { |
| 165 | if (PC_IN_CALL_DUMMY (thisframe->pc, thisframe->frame, thisframe->frame)) |
| 166 | { /* initialize the from_pc now */ |
| 167 | thisframe->from_pc = generic_read_register_dummy (thisframe->pc, |
| 168 | thisframe->frame, |
| 169 | PC_REGNUM); |
| 170 | return thisframe->frame; |
| 171 | } |
| 172 | h8300_frame_find_saved_regs (thisframe, (struct frame_saved_regs *) 0); |
| 173 | return thisframe->fsr->regs[SP_REGNUM]; |
| 174 | } |
| 175 | |
| 176 | /* Put here the code to store, into a struct frame_saved_regs, |
| 177 | the addresses of the saved registers of frame described by FRAME_INFO. |
| 178 | This includes special registers such as pc and fp saved in special |
| 179 | ways in the stack frame. sp is even more special: |
| 180 | the address we return for it IS the sp for the next frame. |
| 181 | |
| 182 | We cache the result of doing this in the frame_obstack, since it is |
| 183 | fairly expensive. */ |
| 184 | |
| 185 | void |
| 186 | h8300_frame_find_saved_regs (struct frame_info *fi, |
| 187 | struct frame_saved_regs *fsr) |
| 188 | { |
| 189 | register struct frame_saved_regs *cache_fsr; |
| 190 | CORE_ADDR ip; |
| 191 | struct symtab_and_line sal; |
| 192 | CORE_ADDR limit; |
| 193 | |
| 194 | if (!fi->fsr) |
| 195 | { |
| 196 | cache_fsr = (struct frame_saved_regs *) |
| 197 | frame_obstack_alloc (sizeof (struct frame_saved_regs)); |
| 198 | memset (cache_fsr, '\0', sizeof (struct frame_saved_regs)); |
| 199 | |
| 200 | fi->fsr = cache_fsr; |
| 201 | |
| 202 | if (PC_IN_CALL_DUMMY (fi->pc, fi->frame, fi->frame)) |
| 203 | { /* no more to do. */ |
| 204 | if (fsr) |
| 205 | *fsr = *fi->fsr; |
| 206 | return; |
| 207 | } |
| 208 | /* Find the start and end of the function prologue. If the PC |
| 209 | is in the function prologue, we only consider the part that |
| 210 | has executed already. */ |
| 211 | |
| 212 | ip = get_pc_function_start (fi->pc); |
| 213 | sal = find_pc_line (ip, 0); |
| 214 | limit = (sal.end && sal.end < fi->pc) ? sal.end : fi->pc; |
| 215 | |
| 216 | /* This will fill in fields in *fi as well as in cache_fsr. */ |
| 217 | examine_prologue (ip, limit, fi->frame, cache_fsr, fi); |
| 218 | } |
| 219 | |
| 220 | if (fsr) |
| 221 | *fsr = *fi->fsr; |
| 222 | } |
| 223 | |
| 224 | /* Fetch the instruction at ADDR, returning 0 if ADDR is beyond LIM or |
| 225 | is not the address of a valid instruction, the address of the next |
| 226 | instruction beyond ADDR otherwise. *PWORD1 receives the first word |
| 227 | of the instruction. */ |
| 228 | |
| 229 | CORE_ADDR |
| 230 | NEXT_PROLOGUE_INSN (CORE_ADDR addr, CORE_ADDR lim, INSN_WORD *pword1) |
| 231 | { |
| 232 | char buf[2]; |
| 233 | if (addr < lim + 8) |
| 234 | { |
| 235 | read_memory (addr, buf, 2); |
| 236 | *pword1 = extract_signed_integer (buf, 2); |
| 237 | |
| 238 | return addr + 2; |
| 239 | } |
| 240 | return 0; |
| 241 | } |
| 242 | |
| 243 | /* Examine the prologue of a function. `ip' points to the first instruction. |
| 244 | `limit' is the limit of the prologue (e.g. the addr of the first |
| 245 | linenumber, or perhaps the program counter if we're stepping through). |
| 246 | `frame_sp' is the stack pointer value in use in this frame. |
| 247 | `fsr' is a pointer to a frame_saved_regs structure into which we put |
| 248 | info about the registers saved by this frame. |
| 249 | `fi' is a struct frame_info pointer; we fill in various fields in it |
| 250 | to reflect the offsets of the arg pointer and the locals pointer. */ |
| 251 | |
| 252 | static CORE_ADDR |
| 253 | examine_prologue (register CORE_ADDR ip, register CORE_ADDR limit, |
| 254 | CORE_ADDR after_prolog_fp, struct frame_saved_regs *fsr, |
| 255 | struct frame_info *fi) |
| 256 | { |
| 257 | register CORE_ADDR next_ip; |
| 258 | int r; |
| 259 | int have_fp = 0; |
| 260 | INSN_WORD insn_word; |
| 261 | /* Number of things pushed onto stack, starts at 2/4, 'cause the |
| 262 | PC is already there */ |
| 263 | unsigned int reg_save_depth = h8300hmode ? 4 : 2; |
| 264 | |
| 265 | unsigned int auto_depth = 0; /* Number of bytes of autos */ |
| 266 | |
| 267 | char in_frame[11]; /* One for each reg */ |
| 268 | |
| 269 | int adjust = 0; |
| 270 | |
| 271 | memset (in_frame, 1, 11); |
| 272 | for (r = 0; r < 8; r++) |
| 273 | { |
| 274 | fsr->regs[r] = 0; |
| 275 | } |
| 276 | if (after_prolog_fp == 0) |
| 277 | { |
| 278 | after_prolog_fp = read_register (SP_REGNUM); |
| 279 | } |
| 280 | |
| 281 | /* If the PC isn't valid, quit now. */ |
| 282 | if (ip == 0 || ip & (h8300hmode ? ~0xffffff : ~0xffff)) |
| 283 | return 0; |
| 284 | |
| 285 | next_ip = NEXT_PROLOGUE_INSN (ip, limit, &insn_word); |
| 286 | |
| 287 | if (insn_word == 0x0100) |
| 288 | { |
| 289 | insn_word = read_memory_unsigned_integer (ip + 2, 2); |
| 290 | adjust = 2; |
| 291 | } |
| 292 | |
| 293 | /* Skip over any fp push instructions */ |
| 294 | fsr->regs[6] = after_prolog_fp; |
| 295 | while (next_ip && IS_PUSH_FP (insn_word)) |
| 296 | { |
| 297 | ip = next_ip + adjust; |
| 298 | |
| 299 | in_frame[insn_word & 0x7] = reg_save_depth; |
| 300 | next_ip = NEXT_PROLOGUE_INSN (ip, limit, &insn_word); |
| 301 | reg_save_depth += 2 + adjust; |
| 302 | } |
| 303 | |
| 304 | /* Is this a move into the fp */ |
| 305 | if (next_ip && IS_MOV_SP_FP (insn_word)) |
| 306 | { |
| 307 | ip = next_ip; |
| 308 | next_ip = NEXT_PROLOGUE_INSN (ip, limit, &insn_word); |
| 309 | have_fp = 1; |
| 310 | } |
| 311 | |
| 312 | /* Skip over any stack adjustment, happens either with a number of |
| 313 | sub#2,sp or a mov #x,r5 sub r5,sp */ |
| 314 | |
| 315 | if (next_ip && (IS_SUB2_SP (insn_word) || IS_SUB4_SP (insn_word))) |
| 316 | { |
| 317 | while (next_ip && (IS_SUB2_SP (insn_word) || IS_SUB4_SP (insn_word))) |
| 318 | { |
| 319 | auto_depth += IS_SUB2_SP (insn_word) ? 2 : 4; |
| 320 | ip = next_ip; |
| 321 | next_ip = NEXT_PROLOGUE_INSN (ip, limit, &insn_word); |
| 322 | } |
| 323 | } |
| 324 | else |
| 325 | { |
| 326 | if (next_ip && IS_MOVK_R5 (insn_word)) |
| 327 | { |
| 328 | ip = next_ip; |
| 329 | next_ip = NEXT_PROLOGUE_INSN (ip, limit, &insn_word); |
| 330 | auto_depth += insn_word; |
| 331 | |
| 332 | next_ip = NEXT_PROLOGUE_INSN (next_ip, limit, &insn_word); |
| 333 | auto_depth += insn_word; |
| 334 | } |
| 335 | if (next_ip && IS_SUBL_SP (insn_word)) |
| 336 | { |
| 337 | ip = next_ip; |
| 338 | auto_depth += read_memory_unsigned_integer (ip, 4); |
| 339 | ip += 4; |
| 340 | |
| 341 | next_ip = NEXT_PROLOGUE_INSN (ip, limit, &insn_word); |
| 342 | } |
| 343 | } |
| 344 | |
| 345 | /* Now examine the push insns to determine where everything lives |
| 346 | on the stack. */ |
| 347 | while (1) |
| 348 | { |
| 349 | adjust = 0; |
| 350 | if (!next_ip) |
| 351 | break; |
| 352 | |
| 353 | if (insn_word == 0x0100) |
| 354 | { |
| 355 | ip = next_ip; |
| 356 | next_ip = NEXT_PROLOGUE_INSN (ip, limit, &insn_word); |
| 357 | adjust = 2; |
| 358 | } |
| 359 | |
| 360 | if (IS_PUSH (insn_word)) |
| 361 | { |
| 362 | ip = next_ip; |
| 363 | next_ip = NEXT_PROLOGUE_INSN (ip, limit, &insn_word); |
| 364 | fsr->regs[r] = after_prolog_fp + auto_depth; |
| 365 | auto_depth += 2 + adjust; |
| 366 | continue; |
| 367 | } |
| 368 | |
| 369 | /* Now check for push multiple insns. */ |
| 370 | if (insn_word == 0x0110 || insn_word == 0x0120 || insn_word == 0x0130) |
| 371 | { |
| 372 | int count = ((insn_word >> 4) & 0xf) + 1; |
| 373 | int start, i; |
| 374 | |
| 375 | ip = next_ip; |
| 376 | next_ip = NEXT_PROLOGUE_INSN (ip, limit, &insn_word); |
| 377 | start = insn_word & 0x7; |
| 378 | |
| 379 | for (i = start; i <= start + count; i++) |
| 380 | { |
| 381 | fsr->regs[i] = after_prolog_fp + auto_depth; |
| 382 | auto_depth += 4; |
| 383 | } |
| 384 | } |
| 385 | break; |
| 386 | } |
| 387 | |
| 388 | /* The args are always reffed based from the stack pointer */ |
| 389 | fi->args_pointer = after_prolog_fp; |
| 390 | /* Locals are always reffed based from the fp */ |
| 391 | fi->locals_pointer = after_prolog_fp; |
| 392 | /* The PC is at a known place */ |
| 393 | fi->from_pc = read_memory_unsigned_integer (after_prolog_fp + BINWORD, BINWORD); |
| 394 | |
| 395 | /* Rememeber any others too */ |
| 396 | in_frame[PC_REGNUM] = 0; |
| 397 | |
| 398 | if (have_fp) |
| 399 | /* We keep the old FP in the SP spot */ |
| 400 | fsr->regs[SP_REGNUM] = read_memory_unsigned_integer (fsr->regs[6], BINWORD); |
| 401 | else |
| 402 | fsr->regs[SP_REGNUM] = after_prolog_fp + auto_depth; |
| 403 | |
| 404 | return (ip); |
| 405 | } |
| 406 | |
| 407 | void |
| 408 | h8300_init_extra_frame_info (int fromleaf, struct frame_info *fi) |
| 409 | { |
| 410 | fi->fsr = 0; /* Not yet allocated */ |
| 411 | fi->args_pointer = 0; /* Unknown */ |
| 412 | fi->locals_pointer = 0; /* Unknown */ |
| 413 | fi->from_pc = 0; |
| 414 | if (PC_IN_CALL_DUMMY (fi->pc, fi->frame, fi->frame)) |
| 415 | { /* anything special to do? */ |
| 416 | return; |
| 417 | } |
| 418 | } |
| 419 | |
| 420 | /* Return the saved PC from this frame. |
| 421 | |
| 422 | If the frame has a memory copy of SRP_REGNUM, use that. If not, |
| 423 | just use the register SRP_REGNUM itself. */ |
| 424 | |
| 425 | CORE_ADDR |
| 426 | h8300_frame_saved_pc (struct frame_info *frame) |
| 427 | { |
| 428 | if (PC_IN_CALL_DUMMY (frame->pc, frame->frame, frame->frame)) |
| 429 | return generic_read_register_dummy (frame->pc, frame->frame, PC_REGNUM); |
| 430 | else |
| 431 | return frame->from_pc; |
| 432 | } |
| 433 | |
| 434 | CORE_ADDR |
| 435 | h8300_frame_locals_address (struct frame_info *fi) |
| 436 | { |
| 437 | if (PC_IN_CALL_DUMMY (fi->pc, fi->frame, fi->frame)) |
| 438 | return (CORE_ADDR) 0; /* Not sure what else to do... */ |
| 439 | if (!fi->locals_pointer) |
| 440 | { |
| 441 | struct frame_saved_regs ignore; |
| 442 | |
| 443 | get_frame_saved_regs (fi, &ignore); |
| 444 | |
| 445 | } |
| 446 | return fi->locals_pointer; |
| 447 | } |
| 448 | |
| 449 | /* Return the address of the argument block for the frame |
| 450 | described by FI. Returns 0 if the address is unknown. */ |
| 451 | |
| 452 | CORE_ADDR |
| 453 | h8300_frame_args_address (struct frame_info *fi) |
| 454 | { |
| 455 | if (PC_IN_CALL_DUMMY (fi->pc, fi->frame, fi->frame)) |
| 456 | return (CORE_ADDR) 0; /* Not sure what else to do... */ |
| 457 | if (!fi->args_pointer) |
| 458 | { |
| 459 | struct frame_saved_regs ignore; |
| 460 | |
| 461 | get_frame_saved_regs (fi, &ignore); |
| 462 | |
| 463 | } |
| 464 | |
| 465 | return fi->args_pointer; |
| 466 | } |
| 467 | |
| 468 | /* Function: push_arguments |
| 469 | Setup the function arguments for calling a function in the inferior. |
| 470 | |
| 471 | On the Hitachi H8/300 architecture, there are three registers (R0 to R2) |
| 472 | which are dedicated for passing function arguments. Up to the first |
| 473 | three arguments (depending on size) may go into these registers. |
| 474 | The rest go on the stack. |
| 475 | |
| 476 | Arguments that are smaller than WORDSIZE bytes will still take up a |
| 477 | whole register or a whole WORDSIZE word on the stack, and will be |
| 478 | right-justified in the register or the stack word. This includes |
| 479 | chars and small aggregate types. Note that WORDSIZE depends on the |
| 480 | cpu type. |
| 481 | |
| 482 | Arguments that are larger than WORDSIZE bytes will be split between |
| 483 | two or more registers as available, but will NOT be split between a |
| 484 | register and the stack. |
| 485 | |
| 486 | An exceptional case exists for struct arguments (and possibly other |
| 487 | aggregates such as arrays) -- if the size is larger than WORDSIZE |
| 488 | bytes but not a multiple of WORDSIZE bytes. In this case the |
| 489 | argument is never split between the registers and the stack, but |
| 490 | instead is copied in its entirety onto the stack, AND also copied |
| 491 | into as many registers as there is room for. In other words, space |
| 492 | in registers permitting, two copies of the same argument are passed |
| 493 | in. As far as I can tell, only the one on the stack is used, |
| 494 | although that may be a function of the level of compiler |
| 495 | optimization. I suspect this is a compiler bug. Arguments of |
| 496 | these odd sizes are left-justified within the word (as opposed to |
| 497 | arguments smaller than WORDSIZE bytes, which are right-justified). |
| 498 | |
| 499 | If the function is to return an aggregate type such as a struct, |
| 500 | the caller must allocate space into which the callee will copy the |
| 501 | return value. In this case, a pointer to the return value location |
| 502 | is passed into the callee in register R0, which displaces one of |
| 503 | the other arguments passed in via registers R0 to R2. */ |
| 504 | |
| 505 | CORE_ADDR |
| 506 | h8300_push_arguments (int nargs, struct value **args, CORE_ADDR sp, |
| 507 | unsigned char struct_return, CORE_ADDR struct_addr) |
| 508 | { |
| 509 | int stack_align, stack_alloc, stack_offset; |
| 510 | int wordsize; |
| 511 | int argreg; |
| 512 | int argnum; |
| 513 | struct type *type; |
| 514 | CORE_ADDR regval; |
| 515 | char *val; |
| 516 | char valbuf[4]; |
| 517 | int len; |
| 518 | |
| 519 | if (h8300hmode || h8300smode) |
| 520 | { |
| 521 | stack_align = 3; |
| 522 | wordsize = 4; |
| 523 | } |
| 524 | else |
| 525 | { |
| 526 | stack_align = 1; |
| 527 | wordsize = 2; |
| 528 | } |
| 529 | |
| 530 | /* first force sp to a n-byte alignment */ |
| 531 | sp = sp & ~stack_align; |
| 532 | |
| 533 | /* Now make sure there's space on the stack */ |
| 534 | for (argnum = 0, stack_alloc = 0; |
| 535 | argnum < nargs; argnum++) |
| 536 | stack_alloc += ((TYPE_LENGTH (VALUE_TYPE (args[argnum])) + stack_align) |
| 537 | & ~stack_align); |
| 538 | sp -= stack_alloc; /* make room on stack for args */ |
| 539 | /* we may over-allocate a little here, but that won't hurt anything */ |
| 540 | |
| 541 | argreg = ARG0_REGNUM; |
| 542 | if (struct_return) /* "struct return" pointer takes up one argreg */ |
| 543 | { |
| 544 | write_register (argreg++, struct_addr); |
| 545 | } |
| 546 | |
| 547 | /* Now load as many as possible of the first arguments into |
| 548 | registers, and push the rest onto the stack. There are 3N bytes |
| 549 | in three registers available. Loop thru args from first to last. */ |
| 550 | |
| 551 | for (argnum = 0, stack_offset = 0; argnum < nargs; argnum++) |
| 552 | { |
| 553 | type = VALUE_TYPE (args[argnum]); |
| 554 | len = TYPE_LENGTH (type); |
| 555 | memset (valbuf, 0, sizeof (valbuf)); |
| 556 | if (len < wordsize) |
| 557 | { |
| 558 | /* the purpose of this is to right-justify the value within the word */ |
| 559 | memcpy (valbuf + (wordsize - len), |
| 560 | (char *) VALUE_CONTENTS (args[argnum]), len); |
| 561 | val = valbuf; |
| 562 | } |
| 563 | else |
| 564 | val = (char *) VALUE_CONTENTS (args[argnum]); |
| 565 | |
| 566 | if (len > (ARGLAST_REGNUM + 1 - argreg) * REGISTER_RAW_SIZE (ARG0_REGNUM) || |
| 567 | (len > wordsize && (len & stack_align) != 0)) |
| 568 | { /* passed on the stack */ |
| 569 | write_memory (sp + stack_offset, val, |
| 570 | len < wordsize ? wordsize : len); |
| 571 | stack_offset += (len + stack_align) & ~stack_align; |
| 572 | } |
| 573 | /* NOTE WELL!!!!! This is not an "else if" clause!!! |
| 574 | That's because some *&^%$ things get passed on the stack |
| 575 | AND in the registers! */ |
| 576 | if (len <= (ARGLAST_REGNUM + 1 - argreg) * REGISTER_RAW_SIZE (ARG0_REGNUM)) |
| 577 | while (len > 0) |
| 578 | { /* there's room in registers */ |
| 579 | regval = extract_address (val, wordsize); |
| 580 | write_register (argreg, regval); |
| 581 | len -= wordsize; |
| 582 | val += wordsize; |
| 583 | argreg++; |
| 584 | } |
| 585 | } |
| 586 | return sp; |
| 587 | } |
| 588 | |
| 589 | /* Function: push_return_address |
| 590 | Setup the return address for a dummy frame, as called by |
| 591 | call_function_by_hand. Only necessary when you are using an |
| 592 | empty CALL_DUMMY, ie. the target will not actually be executing |
| 593 | a JSR/BSR instruction. */ |
| 594 | |
| 595 | CORE_ADDR |
| 596 | h8300_push_return_address (CORE_ADDR pc, CORE_ADDR sp) |
| 597 | { |
| 598 | unsigned char buf[4]; |
| 599 | int wordsize; |
| 600 | |
| 601 | if (h8300hmode || h8300smode) |
| 602 | wordsize = 4; |
| 603 | else |
| 604 | wordsize = 2; |
| 605 | |
| 606 | sp -= wordsize; |
| 607 | store_unsigned_integer (buf, wordsize, CALL_DUMMY_ADDRESS ()); |
| 608 | write_memory (sp, buf, wordsize); |
| 609 | return sp; |
| 610 | } |
| 611 | |
| 612 | /* Function: h8300_pop_frame |
| 613 | Restore the machine to the state it had before the current frame |
| 614 | was created. Usually used either by the "RETURN" command, or by |
| 615 | call_function_by_hand after the dummy_frame is finished. */ |
| 616 | |
| 617 | void |
| 618 | h8300_pop_frame (void) |
| 619 | { |
| 620 | unsigned regnum; |
| 621 | struct frame_saved_regs fsr; |
| 622 | struct frame_info *frame = get_current_frame (); |
| 623 | |
| 624 | if (PC_IN_CALL_DUMMY (frame->pc, frame->frame, frame->frame)) |
| 625 | { |
| 626 | generic_pop_dummy_frame (); |
| 627 | } |
| 628 | else |
| 629 | { |
| 630 | get_frame_saved_regs (frame, &fsr); |
| 631 | |
| 632 | for (regnum = 0; regnum < 8; regnum++) |
| 633 | { |
| 634 | /* Don't forget SP_REGNUM is a frame_saved_regs struct is the |
| 635 | actual value we want, not the address of the value we want. */ |
| 636 | if (fsr.regs[regnum] && regnum != SP_REGNUM) |
| 637 | write_register (regnum, |
| 638 | read_memory_integer (fsr.regs[regnum], BINWORD)); |
| 639 | else if (fsr.regs[regnum] && regnum == SP_REGNUM) |
| 640 | write_register (regnum, frame->frame + 2 * BINWORD); |
| 641 | } |
| 642 | |
| 643 | /* Don't forget the update the PC too! */ |
| 644 | write_pc (frame->from_pc); |
| 645 | } |
| 646 | flush_cached_frames (); |
| 647 | } |
| 648 | |
| 649 | /* Function: extract_return_value |
| 650 | Figure out where in REGBUF the called function has left its return value. |
| 651 | Copy that into VALBUF. Be sure to account for CPU type. */ |
| 652 | |
| 653 | void |
| 654 | h8300_extract_return_value (struct type *type, char *regbuf, char *valbuf) |
| 655 | { |
| 656 | int wordsize, len; |
| 657 | |
| 658 | if (h8300smode || h8300hmode) |
| 659 | wordsize = 4; |
| 660 | else |
| 661 | wordsize = 2; |
| 662 | |
| 663 | len = TYPE_LENGTH (type); |
| 664 | |
| 665 | switch (len) |
| 666 | { |
| 667 | case 1: /* (char) */ |
| 668 | case 2: /* (short), (int) */ |
| 669 | memcpy (valbuf, regbuf + REGISTER_BYTE (0) + (wordsize - len), len); |
| 670 | break; |
| 671 | case 4: /* (long), (float) */ |
| 672 | if (h8300smode || h8300hmode) |
| 673 | { |
| 674 | memcpy (valbuf, regbuf + REGISTER_BYTE (0), 4); |
| 675 | } |
| 676 | else |
| 677 | { |
| 678 | memcpy (valbuf, regbuf + REGISTER_BYTE (0), 2); |
| 679 | memcpy (valbuf + 2, regbuf + REGISTER_BYTE (1), 2); |
| 680 | } |
| 681 | break; |
| 682 | case 8: /* (double) (doesn't seem to happen, which is good, |
| 683 | because this almost certainly isn't right. */ |
| 684 | error ("I don't know how a double is returned."); |
| 685 | break; |
| 686 | } |
| 687 | } |
| 688 | |
| 689 | /* Function: store_return_value |
| 690 | Place the appropriate value in the appropriate registers. |
| 691 | Primarily used by the RETURN command. */ |
| 692 | |
| 693 | void |
| 694 | h8300_store_return_value (struct type *type, char *valbuf) |
| 695 | { |
| 696 | int wordsize, len, regval; |
| 697 | |
| 698 | if (h8300hmode || h8300smode) |
| 699 | wordsize = 4; |
| 700 | else |
| 701 | wordsize = 2; |
| 702 | |
| 703 | len = TYPE_LENGTH (type); |
| 704 | switch (len) |
| 705 | { |
| 706 | case 1: /* char */ |
| 707 | case 2: /* short, int */ |
| 708 | regval = extract_address (valbuf, len); |
| 709 | write_register (0, regval); |
| 710 | break; |
| 711 | case 4: /* long, float */ |
| 712 | regval = extract_address (valbuf, len); |
| 713 | if (h8300smode || h8300hmode) |
| 714 | { |
| 715 | write_register (0, regval); |
| 716 | } |
| 717 | else |
| 718 | { |
| 719 | write_register (0, regval >> 16); |
| 720 | write_register (1, regval & 0xffff); |
| 721 | } |
| 722 | break; |
| 723 | case 8: /* presumeably double, but doesn't seem to happen */ |
| 724 | error ("I don't know how to return a double."); |
| 725 | break; |
| 726 | } |
| 727 | } |
| 728 | |
| 729 | struct cmd_list_element *setmemorylist; |
| 730 | |
| 731 | static void |
| 732 | set_register_names (void) |
| 733 | { |
| 734 | if (h8300hmode != 0) |
| 735 | h8300_register_names = h8300h_register_names; |
| 736 | else |
| 737 | h8300_register_names = original_register_names; |
| 738 | } |
| 739 | |
| 740 | static void |
| 741 | h8300_command (char *args, int from_tty) |
| 742 | { |
| 743 | extern int h8300hmode; |
| 744 | h8300hmode = 0; |
| 745 | h8300smode = 0; |
| 746 | set_register_names (); |
| 747 | } |
| 748 | |
| 749 | static void |
| 750 | h8300h_command (char *args, int from_tty) |
| 751 | { |
| 752 | extern int h8300hmode; |
| 753 | h8300hmode = 1; |
| 754 | h8300smode = 0; |
| 755 | set_register_names (); |
| 756 | } |
| 757 | |
| 758 | static void |
| 759 | h8300s_command (char *args, int from_tty) |
| 760 | { |
| 761 | extern int h8300smode; |
| 762 | extern int h8300hmode; |
| 763 | h8300smode = 1; |
| 764 | h8300hmode = 1; |
| 765 | set_register_names (); |
| 766 | } |
| 767 | |
| 768 | |
| 769 | static void |
| 770 | set_machine (char *args, int from_tty) |
| 771 | { |
| 772 | printf_unfiltered ("\"set machine\" must be followed by h8300, h8300h"); |
| 773 | printf_unfiltered ("or h8300s"); |
| 774 | help_list (setmemorylist, "set memory ", -1, gdb_stdout); |
| 775 | } |
| 776 | |
| 777 | /* set_machine_hook is called as the exec file is being opened, but |
| 778 | before the symbol file is opened. This allows us to set the |
| 779 | h8300hmode flag based on the machine type specified in the exec |
| 780 | file. This in turn will cause subsequently defined pointer types |
| 781 | to be 16 or 32 bits as appropriate for the machine. */ |
| 782 | |
| 783 | static void |
| 784 | set_machine_hook (char *filename) |
| 785 | { |
| 786 | if (bfd_get_mach (exec_bfd) == bfd_mach_h8300s) |
| 787 | { |
| 788 | h8300smode = 1; |
| 789 | h8300hmode = 1; |
| 790 | } |
| 791 | else if (bfd_get_mach (exec_bfd) == bfd_mach_h8300h) |
| 792 | { |
| 793 | h8300smode = 0; |
| 794 | h8300hmode = 1; |
| 795 | } |
| 796 | else |
| 797 | { |
| 798 | h8300smode = 0; |
| 799 | h8300hmode = 0; |
| 800 | } |
| 801 | set_register_names (); |
| 802 | } |
| 803 | |
| 804 | void |
| 805 | _initialize_h8300m (void) |
| 806 | { |
| 807 | add_prefix_cmd ("machine", no_class, set_machine, |
| 808 | "set the machine type", |
| 809 | &setmemorylist, "set machine ", 0, |
| 810 | &setlist); |
| 811 | |
| 812 | add_cmd ("h8300", class_support, h8300_command, |
| 813 | "Set machine to be H8/300.", &setmemorylist); |
| 814 | |
| 815 | add_cmd ("h8300h", class_support, h8300h_command, |
| 816 | "Set machine to be H8/300H.", &setmemorylist); |
| 817 | |
| 818 | add_cmd ("h8300s", class_support, h8300s_command, |
| 819 | "Set machine to be H8/300S.", &setmemorylist); |
| 820 | |
| 821 | /* Add a hook to set the machine type when we're loading a file. */ |
| 822 | |
| 823 | specify_exec_file_hook (set_machine_hook); |
| 824 | } |
| 825 | |
| 826 | |
| 827 | |
| 828 | void |
| 829 | h8300_print_register_hook (int regno) |
| 830 | { |
| 831 | if (regno == 8) |
| 832 | { |
| 833 | /* CCR register */ |
| 834 | int C, Z, N, V; |
| 835 | unsigned char b[4]; |
| 836 | unsigned char l; |
| 837 | read_relative_register_raw_bytes (regno, b); |
| 838 | l = b[REGISTER_VIRTUAL_SIZE (8) - 1]; |
| 839 | printf_unfiltered ("\t"); |
| 840 | printf_unfiltered ("I-%d - ", (l & 0x80) != 0); |
| 841 | printf_unfiltered ("H-%d - ", (l & 0x20) != 0); |
| 842 | N = (l & 0x8) != 0; |
| 843 | Z = (l & 0x4) != 0; |
| 844 | V = (l & 0x2) != 0; |
| 845 | C = (l & 0x1) != 0; |
| 846 | printf_unfiltered ("N-%d ", N); |
| 847 | printf_unfiltered ("Z-%d ", Z); |
| 848 | printf_unfiltered ("V-%d ", V); |
| 849 | printf_unfiltered ("C-%d ", C); |
| 850 | if ((C | Z) == 0) |
| 851 | printf_unfiltered ("u> "); |
| 852 | if ((C | Z) == 1) |
| 853 | printf_unfiltered ("u<= "); |
| 854 | if ((C == 0)) |
| 855 | printf_unfiltered ("u>= "); |
| 856 | if (C == 1) |
| 857 | printf_unfiltered ("u< "); |
| 858 | if (Z == 0) |
| 859 | printf_unfiltered ("!= "); |
| 860 | if (Z == 1) |
| 861 | printf_unfiltered ("== "); |
| 862 | if ((N ^ V) == 0) |
| 863 | printf_unfiltered (">= "); |
| 864 | if ((N ^ V) == 1) |
| 865 | printf_unfiltered ("< "); |
| 866 | if ((Z | (N ^ V)) == 0) |
| 867 | printf_unfiltered ("> "); |
| 868 | if ((Z | (N ^ V)) == 1) |
| 869 | printf_unfiltered ("<= "); |
| 870 | } |
| 871 | } |
| 872 | |
| 873 | void |
| 874 | _initialize_h8300_tdep (void) |
| 875 | { |
| 876 | tm_print_insn = gdb_print_insn_h8300; |
| 877 | } |