1 /* Target-machine dependent code for Hitachi H8/300, for GDB.
3 Copyright 1988, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1998,
4 1999, 2000, 2001, 2002 Free Software Foundation, Inc.
6 This file is part of GDB.
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.
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.
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. */
24 Contributed by Steve Chamberlain
35 #include "gdb_string.h"
39 extern int h8300hmode, h8300smode;
42 #define NUM_REGS (h8300smode?12:11)
44 #define UNSIGNED_SHORT(X) ((X) & 0xffff)
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)
56 /* The register names change depending on whether the h8300h processor
59 static char *original_register_names[] = REGISTER_NAMES;
61 static char *h8300h_register_names[] = {
62 "er0", "er1", "er2", "er3", "er4", "er5", "er6",
63 "sp", "ccr", "pc", "cycles", "exr", "tick", "inst"
66 char **h8300_register_names = original_register_names;
68 /* Local function declarations. */
70 static CORE_ADDR examine_prologue ();
71 static void set_machine_hook (char *filename);
74 h8300_skip_prologue (CORE_ADDR start_pc)
79 /* Skip past all push and stm insns. */
82 w = read_memory_unsigned_integer (start_pc, 2);
83 /* First look for push insns. */
84 if (w == 0x0100 || w == 0x0110 || w == 0x0120 || w == 0x0130)
86 w = read_memory_unsigned_integer (start_pc + 2, 2);
92 start_pc += 2 + adjust;
93 w = read_memory_unsigned_integer (start_pc, 2);
100 /* Skip past a move to FP, either word or long sized */
101 w = read_memory_unsigned_integer (start_pc, 2);
104 w = read_memory_unsigned_integer (start_pc + 2, 2);
110 start_pc += 2 + adjust;
111 w = read_memory_unsigned_integer (start_pc, 2);
114 /* Check for loading either a word constant into r5;
115 long versions are handled by the SUBL_SP below. */
119 w = read_memory_unsigned_integer (start_pc, 2);
122 /* Now check for subtracting r5 from sp, word sized only. */
125 start_pc += 2 + adjust;
126 w = read_memory_unsigned_integer (start_pc, 2);
129 /* Check for subs #2 and subs #4. */
130 while (IS_SUB2_SP (w) || IS_SUB4_SP (w))
132 start_pc += 2 + adjust;
133 w = read_memory_unsigned_integer (start_pc, 2);
136 /* Check for a 32bit subtract. */
138 start_pc += 6 + adjust;
144 gdb_print_insn_h8300 (bfd_vma memaddr, disassemble_info * info)
147 return print_insn_h8300s (memaddr, info);
149 return print_insn_h8300h (memaddr, info);
151 return print_insn_h8300 (memaddr, info);
154 /* Given a GDB frame, determine the address of the calling function's frame.
155 This will be used to create a new GDB frame struct, and then
156 INIT_EXTRA_FRAME_INFO and INIT_FRAME_PC will be called for the new frame.
158 For us, the frame address is its stack pointer value, so we look up
159 the function prologue to determine the caller's sp value, and return it. */
162 h8300_frame_chain (struct frame_info *thisframe)
164 if (PC_IN_CALL_DUMMY (thisframe->pc, thisframe->frame, thisframe->frame))
165 { /* initialize the from_pc now */
166 thisframe->from_pc = generic_read_register_dummy (thisframe->pc,
169 return thisframe->frame;
171 h8300_frame_find_saved_regs (thisframe, (struct frame_saved_regs *) 0);
172 return thisframe->fsr->regs[SP_REGNUM];
175 /* Put here the code to store, into a struct frame_saved_regs,
176 the addresses of the saved registers of frame described by FRAME_INFO.
177 This includes special registers such as pc and fp saved in special
178 ways in the stack frame. sp is even more special:
179 the address we return for it IS the sp for the next frame.
181 We cache the result of doing this in the frame_obstack, since it is
185 h8300_frame_find_saved_regs (struct frame_info *fi,
186 struct frame_saved_regs *fsr)
188 register struct frame_saved_regs *cache_fsr;
190 struct symtab_and_line sal;
195 cache_fsr = (struct frame_saved_regs *)
196 frame_obstack_alloc (sizeof (struct frame_saved_regs));
197 memset (cache_fsr, '\0', sizeof (struct frame_saved_regs));
201 if (PC_IN_CALL_DUMMY (fi->pc, fi->frame, fi->frame))
202 { /* no more to do. */
207 /* Find the start and end of the function prologue. If the PC
208 is in the function prologue, we only consider the part that
209 has executed already. */
211 ip = get_pc_function_start (fi->pc);
212 sal = find_pc_line (ip, 0);
213 limit = (sal.end && sal.end < fi->pc) ? sal.end : fi->pc;
215 /* This will fill in fields in *fi as well as in cache_fsr. */
216 examine_prologue (ip, limit, fi->frame, cache_fsr, fi);
223 /* Fetch the instruction at ADDR, returning 0 if ADDR is beyond LIM or
224 is not the address of a valid instruction, the address of the next
225 instruction beyond ADDR otherwise. *PWORD1 receives the first word
226 of the instruction. */
229 NEXT_PROLOGUE_INSN (CORE_ADDR addr, CORE_ADDR lim, INSN_WORD * pword1)
234 read_memory (addr, buf, 2);
235 *pword1 = extract_signed_integer (buf, 2);
242 /* Examine the prologue of a function. `ip' points to the first instruction.
243 `limit' is the limit of the prologue (e.g. the addr of the first
244 linenumber, or perhaps the program counter if we're stepping through).
245 `frame_sp' is the stack pointer value in use in this frame.
246 `fsr' is a pointer to a frame_saved_regs structure into which we put
247 info about the registers saved by this frame.
248 `fi' is a struct frame_info pointer; we fill in various fields in it
249 to reflect the offsets of the arg pointer and the locals pointer. */
252 examine_prologue (register CORE_ADDR ip, register CORE_ADDR limit,
253 CORE_ADDR after_prolog_fp, struct frame_saved_regs *fsr,
254 struct frame_info *fi)
256 register CORE_ADDR next_ip;
260 /* Number of things pushed onto stack, starts at 2/4, 'cause the
261 PC is already there */
262 unsigned int reg_save_depth = h8300hmode ? 4 : 2;
264 unsigned int auto_depth = 0; /* Number of bytes of autos */
266 char in_frame[11]; /* One for each reg */
270 memset (in_frame, 1, 11);
271 for (r = 0; r < 8; r++)
275 if (after_prolog_fp == 0)
277 after_prolog_fp = read_register (SP_REGNUM);
280 /* If the PC isn't valid, quit now. */
281 if (ip == 0 || ip & (h8300hmode ? ~0xffffff : ~0xffff))
284 next_ip = NEXT_PROLOGUE_INSN (ip, limit, &insn_word);
286 if (insn_word == 0x0100)
288 insn_word = read_memory_unsigned_integer (ip + 2, 2);
292 /* Skip over any fp push instructions */
293 fsr->regs[6] = after_prolog_fp;
294 while (next_ip && IS_PUSH_FP (insn_word))
296 ip = next_ip + adjust;
298 in_frame[insn_word & 0x7] = reg_save_depth;
299 next_ip = NEXT_PROLOGUE_INSN (ip, limit, &insn_word);
300 reg_save_depth += 2 + adjust;
303 /* Is this a move into the fp */
304 if (next_ip && IS_MOV_SP_FP (insn_word))
307 next_ip = NEXT_PROLOGUE_INSN (ip, limit, &insn_word);
311 /* Skip over any stack adjustment, happens either with a number of
312 sub#2,sp or a mov #x,r5 sub r5,sp */
314 if (next_ip && (IS_SUB2_SP (insn_word) || IS_SUB4_SP (insn_word)))
316 while (next_ip && (IS_SUB2_SP (insn_word) || IS_SUB4_SP (insn_word)))
318 auto_depth += IS_SUB2_SP (insn_word) ? 2 : 4;
320 next_ip = NEXT_PROLOGUE_INSN (ip, limit, &insn_word);
325 if (next_ip && IS_MOVK_R5 (insn_word))
328 next_ip = NEXT_PROLOGUE_INSN (ip, limit, &insn_word);
329 auto_depth += insn_word;
331 next_ip = NEXT_PROLOGUE_INSN (next_ip, limit, &insn_word);
332 auto_depth += insn_word;
334 if (next_ip && IS_SUBL_SP (insn_word))
337 auto_depth += read_memory_unsigned_integer (ip, 4);
340 next_ip = NEXT_PROLOGUE_INSN (ip, limit, &insn_word);
344 /* Now examine the push insns to determine where everything lives
352 if (insn_word == 0x0100)
355 next_ip = NEXT_PROLOGUE_INSN (ip, limit, &insn_word);
359 if (IS_PUSH (insn_word))
362 next_ip = NEXT_PROLOGUE_INSN (ip, limit, &insn_word);
363 fsr->regs[r] = after_prolog_fp + auto_depth;
364 auto_depth += 2 + adjust;
368 /* Now check for push multiple insns. */
369 if (insn_word == 0x0110 || insn_word == 0x0120 || insn_word == 0x0130)
371 int count = ((insn_word >> 4) & 0xf) + 1;
375 next_ip = NEXT_PROLOGUE_INSN (ip, limit, &insn_word);
376 start = insn_word & 0x7;
378 for (i = start; i <= start + count; i++)
380 fsr->regs[i] = after_prolog_fp + auto_depth;
387 /* The args are always reffed based from the stack pointer */
388 fi->args_pointer = after_prolog_fp;
389 /* Locals are always reffed based from the fp */
390 fi->locals_pointer = after_prolog_fp;
391 /* The PC is at a known place */
393 read_memory_unsigned_integer (after_prolog_fp + BINWORD, BINWORD);
395 /* Rememeber any others too */
396 in_frame[PC_REGNUM] = 0;
399 /* We keep the old FP in the SP spot */
400 fsr->regs[SP_REGNUM] =
401 read_memory_unsigned_integer (fsr->regs[6], BINWORD);
403 fsr->regs[SP_REGNUM] = after_prolog_fp + auto_depth;
409 h8300_init_extra_frame_info (int fromleaf, struct frame_info *fi)
411 fi->fsr = 0; /* Not yet allocated */
412 fi->args_pointer = 0; /* Unknown */
413 fi->locals_pointer = 0; /* Unknown */
415 if (PC_IN_CALL_DUMMY (fi->pc, fi->frame, fi->frame))
416 { /* anything special to do? */
421 /* Return the saved PC from this frame.
423 If the frame has a memory copy of SRP_REGNUM, use that. If not,
424 just use the register SRP_REGNUM itself. */
427 h8300_frame_saved_pc (struct frame_info *frame)
429 if (PC_IN_CALL_DUMMY (frame->pc, frame->frame, frame->frame))
430 return generic_read_register_dummy (frame->pc, frame->frame, PC_REGNUM);
432 return frame->from_pc;
436 h8300_frame_locals_address (struct frame_info *fi)
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)
442 struct frame_saved_regs ignore;
444 get_frame_saved_regs (fi, &ignore);
447 return fi->locals_pointer;
450 /* Return the address of the argument block for the frame
451 described by FI. Returns 0 if the address is unknown. */
454 h8300_frame_args_address (struct frame_info *fi)
456 if (PC_IN_CALL_DUMMY (fi->pc, fi->frame, fi->frame))
457 return (CORE_ADDR) 0; /* Not sure what else to do... */
458 if (!fi->args_pointer)
460 struct frame_saved_regs ignore;
462 get_frame_saved_regs (fi, &ignore);
466 return fi->args_pointer;
469 /* Function: push_arguments
470 Setup the function arguments for calling a function in the inferior.
472 On the Hitachi H8/300 architecture, there are three registers (R0 to R2)
473 which are dedicated for passing function arguments. Up to the first
474 three arguments (depending on size) may go into these registers.
475 The rest go on the stack.
477 Arguments that are smaller than WORDSIZE bytes will still take up a
478 whole register or a whole WORDSIZE word on the stack, and will be
479 right-justified in the register or the stack word. This includes
480 chars and small aggregate types. Note that WORDSIZE depends on the
483 Arguments that are larger than WORDSIZE bytes will be split between
484 two or more registers as available, but will NOT be split between a
485 register and the stack.
487 An exceptional case exists for struct arguments (and possibly other
488 aggregates such as arrays) -- if the size is larger than WORDSIZE
489 bytes but not a multiple of WORDSIZE bytes. In this case the
490 argument is never split between the registers and the stack, but
491 instead is copied in its entirety onto the stack, AND also copied
492 into as many registers as there is room for. In other words, space
493 in registers permitting, two copies of the same argument are passed
494 in. As far as I can tell, only the one on the stack is used,
495 although that may be a function of the level of compiler
496 optimization. I suspect this is a compiler bug. Arguments of
497 these odd sizes are left-justified within the word (as opposed to
498 arguments smaller than WORDSIZE bytes, which are right-justified).
500 If the function is to return an aggregate type such as a struct,
501 the caller must allocate space into which the callee will copy the
502 return value. In this case, a pointer to the return value location
503 is passed into the callee in register R0, which displaces one of
504 the other arguments passed in via registers R0 to R2. */
507 h8300_push_arguments (int nargs, struct value **args, CORE_ADDR sp,
508 unsigned char struct_return, CORE_ADDR struct_addr)
510 int stack_align, stack_alloc, stack_offset;
520 if (h8300hmode || h8300smode)
531 /* first force sp to a n-byte alignment */
532 sp = sp & ~stack_align;
534 /* Now make sure there's space on the stack */
535 for (argnum = 0, stack_alloc = 0; argnum < nargs; argnum++)
536 stack_alloc += ((TYPE_LENGTH (VALUE_TYPE (args[argnum])) + 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 */
541 argreg = ARG0_REGNUM;
542 if (struct_return) /* "struct return" pointer takes up one argreg */
544 write_register (argreg++, struct_addr);
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. */
551 for (argnum = 0, stack_offset = 0; argnum < nargs; argnum++)
553 type = VALUE_TYPE (args[argnum]);
554 len = TYPE_LENGTH (type);
555 memset (valbuf, 0, sizeof (valbuf));
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);
564 val = (char *) VALUE_CONTENTS (args[argnum]);
567 (ARGLAST_REGNUM + 1 - argreg) * REGISTER_RAW_SIZE (ARG0_REGNUM)
568 || (len > wordsize && (len & stack_align) != 0))
569 { /* passed on the stack */
570 write_memory (sp + stack_offset, val,
571 len < wordsize ? wordsize : len);
572 stack_offset += (len + stack_align) & ~stack_align;
574 /* NOTE WELL!!!!! This is not an "else if" clause!!!
575 That's because some *&^%$ things get passed on the stack
576 AND in the registers! */
578 (ARGLAST_REGNUM + 1 - argreg) * REGISTER_RAW_SIZE (ARG0_REGNUM))
580 { /* there's room in registers */
581 regval = extract_address (val, wordsize);
582 write_register (argreg, regval);
591 /* Function: push_return_address
592 Setup the return address for a dummy frame, as called by
593 call_function_by_hand. Only necessary when you are using an
594 empty CALL_DUMMY, ie. the target will not actually be executing
595 a JSR/BSR instruction. */
598 h8300_push_return_address (CORE_ADDR pc, CORE_ADDR sp)
600 unsigned char buf[4];
603 if (h8300hmode || h8300smode)
609 store_unsigned_integer (buf, wordsize, CALL_DUMMY_ADDRESS ());
610 write_memory (sp, buf, wordsize);
614 /* Function: h8300_pop_frame
615 Restore the machine to the state it had before the current frame
616 was created. Usually used either by the "RETURN" command, or by
617 call_function_by_hand after the dummy_frame is finished. */
620 h8300_pop_frame (void)
623 struct frame_saved_regs fsr;
624 struct frame_info *frame = get_current_frame ();
626 if (PC_IN_CALL_DUMMY (frame->pc, frame->frame, frame->frame))
628 generic_pop_dummy_frame ();
632 get_frame_saved_regs (frame, &fsr);
634 for (regnum = 0; regnum < 8; regnum++)
636 /* Don't forget SP_REGNUM is a frame_saved_regs struct is the
637 actual value we want, not the address of the value we want. */
638 if (fsr.regs[regnum] && regnum != SP_REGNUM)
639 write_register (regnum,
640 read_memory_integer (fsr.regs[regnum], BINWORD));
641 else if (fsr.regs[regnum] && regnum == SP_REGNUM)
642 write_register (regnum, frame->frame + 2 * BINWORD);
645 /* Don't forget the update the PC too! */
646 write_pc (frame->from_pc);
648 flush_cached_frames ();
651 /* Function: extract_return_value
652 Figure out where in REGBUF the called function has left its return value.
653 Copy that into VALBUF. Be sure to account for CPU type. */
656 h8300_extract_return_value (struct type *type, char *regbuf, char *valbuf)
660 if (h8300smode || h8300hmode)
665 len = TYPE_LENGTH (type);
670 case 2: /* (short), (int) */
671 memcpy (valbuf, regbuf + REGISTER_BYTE (0) + (wordsize - len), len);
673 case 4: /* (long), (float) */
674 if (h8300smode || h8300hmode)
676 memcpy (valbuf, regbuf + REGISTER_BYTE (0), 4);
680 memcpy (valbuf, regbuf + REGISTER_BYTE (0), 2);
681 memcpy (valbuf + 2, regbuf + REGISTER_BYTE (1), 2);
684 case 8: /* (double) (doesn't seem to happen, which is good,
685 because this almost certainly isn't right. */
686 error ("I don't know how a double is returned.");
691 /* Function: store_return_value
692 Place the appropriate value in the appropriate registers.
693 Primarily used by the RETURN command. */
696 h8300_store_return_value (struct type *type, char *valbuf)
698 int wordsize, len, regval;
700 if (h8300hmode || h8300smode)
705 len = TYPE_LENGTH (type);
709 case 2: /* short, int */
710 regval = extract_address (valbuf, len);
711 write_register (0, regval);
713 case 4: /* long, float */
714 regval = extract_address (valbuf, len);
715 if (h8300smode || h8300hmode)
717 write_register (0, regval);
721 write_register (0, regval >> 16);
722 write_register (1, regval & 0xffff);
725 case 8: /* presumeably double, but doesn't seem to happen */
726 error ("I don't know how to return a double.");
731 struct cmd_list_element *setmemorylist;
734 set_register_names (void)
737 h8300_register_names = h8300h_register_names;
739 h8300_register_names = original_register_names;
743 h8300_command (char *args, int from_tty)
745 extern int h8300hmode;
748 set_register_names ();
752 h8300h_command (char *args, int from_tty)
754 extern int h8300hmode;
757 set_register_names ();
761 h8300s_command (char *args, int from_tty)
763 extern int h8300smode;
764 extern int h8300hmode;
767 set_register_names ();
771 set_machine (char *args, int from_tty)
773 printf_unfiltered ("\"set machine\" must be followed by h8300, h8300h");
774 printf_unfiltered ("or h8300s");
775 help_list (setmemorylist, "set memory ", -1, gdb_stdout);
778 /* set_machine_hook is called as the exec file is being opened, but
779 before the symbol file is opened. This allows us to set the
780 h8300hmode flag based on the machine type specified in the exec
781 file. This in turn will cause subsequently defined pointer types
782 to be 16 or 32 bits as appropriate for the machine. */
785 set_machine_hook (char *filename)
787 if (bfd_get_mach (exec_bfd) == bfd_mach_h8300s)
792 else if (bfd_get_mach (exec_bfd) == bfd_mach_h8300h)
802 set_register_names ();
806 _initialize_h8300m (void)
808 add_prefix_cmd ("machine", no_class, set_machine,
809 "set the machine type",
810 &setmemorylist, "set machine ", 0, &setlist);
812 add_cmd ("h8300", class_support, h8300_command,
813 "Set machine to be H8/300.", &setmemorylist);
815 add_cmd ("h8300h", class_support, h8300h_command,
816 "Set machine to be H8/300H.", &setmemorylist);
818 add_cmd ("h8300s", class_support, h8300s_command,
819 "Set machine to be H8/300S.", &setmemorylist);
821 /* Add a hook to set the machine type when we're loading a file. */
823 specify_exec_file_hook (set_machine_hook);
827 h8300_print_register_hook (int regno)
829 if (regno == CCR_REGNUM)
833 unsigned char b[REGISTER_SIZE];
835 frame_register_read (selected_frame, regno, b);
836 l = b[REGISTER_VIRTUAL_SIZE (CCR_REGNUM) - 1];
837 printf_unfiltered ("\t");
838 printf_unfiltered ("I-%d ", (l & 0x80) != 0);
839 printf_unfiltered ("UI-%d ", (l & 0x40) != 0);
840 printf_unfiltered ("H-%d ", (l & 0x20) != 0);
841 printf_unfiltered ("U-%d ", (l & 0x10) != 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);
851 printf_unfiltered ("u> ");
853 printf_unfiltered ("u<= ");
855 printf_unfiltered ("u>= ");
857 printf_unfiltered ("u< ");
859 printf_unfiltered ("!= ");
861 printf_unfiltered ("== ");
863 printf_unfiltered (">= ");
865 printf_unfiltered ("< ");
866 if ((Z | (N ^ V)) == 0)
867 printf_unfiltered ("> ");
868 if ((Z | (N ^ V)) == 1)
869 printf_unfiltered ("<= ");
872 if (regno == EXR_REGNUM && h8300smode)
875 unsigned char b[REGISTER_SIZE];
877 frame_register_read (selected_frame, regno, b);
878 l = b[REGISTER_VIRTUAL_SIZE (EXR_REGNUM) - 1];
879 printf_unfiltered ("\t");
880 printf_unfiltered ("T-%d - - - ", (l & 0x80) != 0);
881 printf_unfiltered ("I2-%d ", (l & 4) != 0);
882 printf_unfiltered ("I1-%d ", (l & 2) != 0);
883 printf_unfiltered ("I0-%d", (l & 1) != 0);
888 _initialize_h8300_tdep (void)
890 tm_print_insn = gdb_print_insn_h8300;