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