1 /* Target-machine dependent code for Hitachi H8/300, for GDB.
2 Copyright (C) 1988, 1990, 1991 Free Software Foundation, Inc.
4 This file is part of GDB.
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.
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.
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. */
21 Contributed by Steve Chamberlain
33 #include "gdb_string.h"
36 extern int h8300hmode
, h8300smode
;
41 #define UNSIGNED_SHORT(X) ((X) & 0xffff)
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)
53 /* Local function declarations. */
55 static CORE_ADDR
examine_prologue ();
56 static void set_machine_hook
PARAMS ((char *filename
));
58 void h8300_frame_find_saved_regs ();
61 h8300_skip_prologue (start_pc
)
67 /* Skip past all push and stm insns. */
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)
74 w
= read_memory_unsigned_integer (start_pc
+ 2, 2);
80 start_pc
+= 2 + adjust
;
81 w
= read_memory_unsigned_integer (start_pc
, 2);
88 /* Skip past a move to FP, either word or long sized */
89 w
= read_memory_unsigned_integer (start_pc
, 2);
92 w
= read_memory_unsigned_integer (start_pc
+ 2, 2);
98 start_pc
+= 2 + adjust
;
99 w
= read_memory_unsigned_integer (start_pc
, 2);
102 /* Check for loading either a word constant into r5;
103 long versions are handled by the SUBL_SP below. */
107 w
= read_memory_unsigned_integer (start_pc
, 2);
110 /* Now check for subtracting r5 from sp, word sized only. */
113 start_pc
+= 2 + adjust
;
114 w
= read_memory_unsigned_integer (start_pc
, 2);
117 /* Check for subs #2 and subs #4. */
118 while (IS_SUB2_SP (w
) || IS_SUB4_SP (w
))
120 start_pc
+= 2 + adjust
;
121 w
= read_memory_unsigned_integer (start_pc
, 2);
124 /* Check for a 32bit subtract. */
126 start_pc
+= 6 + adjust
;
132 gdb_print_insn_h8300 (memaddr
, info
)
134 disassemble_info
*info
;
137 return print_insn_h8300s (memaddr
, info
);
139 return print_insn_h8300h (memaddr
, info
);
141 return print_insn_h8300 (memaddr
, info
);
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.
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. */
152 h8300_frame_chain (thisframe
)
153 struct frame_info
*thisframe
;
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
,
160 return thisframe
->frame
;
162 h8300_frame_find_saved_regs (thisframe
, (struct frame_saved_regs
*) 0);
163 return thisframe
->fsr
->regs
[SP_REGNUM
];
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.
172 We cache the result of doing this in the frame_cache_obstack, since
173 it is fairly expensive. */
176 h8300_frame_find_saved_regs (fi
, fsr
)
177 struct frame_info
*fi
;
178 struct frame_saved_regs
*fsr
;
180 register struct frame_saved_regs
*cache_fsr
;
181 extern struct obstack frame_cache_obstack
;
183 struct symtab_and_line sal
;
188 cache_fsr
= (struct frame_saved_regs
*)
189 obstack_alloc (&frame_cache_obstack
,
190 sizeof (struct frame_saved_regs
));
191 memset (cache_fsr
, '\0', sizeof (struct frame_saved_regs
));
195 if (PC_IN_CALL_DUMMY(fi
->pc
, fi
->frame
, fi
->frame
))
196 { /* no more to do. */
201 /* Find the start and end of the function prologue. If the PC
202 is in the function prologue, we only consider the part that
203 has executed already. */
205 ip
= get_pc_function_start (fi
->pc
);
206 sal
= find_pc_line (ip
, 0);
207 limit
= (sal
.end
&& sal
.end
< fi
->pc
) ? sal
.end
: fi
->pc
;
209 /* This will fill in fields in *fi as well as in cache_fsr. */
210 examine_prologue (ip
, limit
, fi
->frame
, cache_fsr
, fi
);
217 /* Fetch the instruction at ADDR, returning 0 if ADDR is beyond LIM or
218 is not the address of a valid instruction, the address of the next
219 instruction beyond ADDR otherwise. *PWORD1 receives the first word
220 of the instruction.*/
223 NEXT_PROLOGUE_INSN (addr
, lim
, pword1
)
231 read_memory (addr
, buf
, 2);
232 *pword1
= extract_signed_integer (buf
, 2);
239 /* Examine the prologue of a function. `ip' points to the first instruction.
240 `limit' is the limit of the prologue (e.g. the addr of the first
241 linenumber, or perhaps the program counter if we're stepping through).
242 `frame_sp' is the stack pointer value in use in this frame.
243 `fsr' is a pointer to a frame_saved_regs structure into which we put
244 info about the registers saved by this frame.
245 `fi' is a struct frame_info pointer; we fill in various fields in it
246 to reflect the offsets of the arg pointer and the locals pointer. */
249 examine_prologue (ip
, limit
, after_prolog_fp
, fsr
, fi
)
250 register CORE_ADDR ip
;
251 register CORE_ADDR limit
;
252 CORE_ADDR after_prolog_fp
;
253 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 */
392 fi
->from_pc
= read_memory_unsigned_integer (after_prolog_fp
+ BINWORD
, BINWORD
);
394 /* Rememeber any others too */
395 in_frame
[PC_REGNUM
] = 0;
398 /* We keep the old FP in the SP spot */
399 fsr
->regs
[SP_REGNUM
] = read_memory_unsigned_integer (fsr
->regs
[6], BINWORD
);
401 fsr
->regs
[SP_REGNUM
] = after_prolog_fp
+ auto_depth
;
407 h8300_init_extra_frame_info (fromleaf
, fi
)
409 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 (frame
)
428 struct frame_info
*frame
;
430 if (PC_IN_CALL_DUMMY(frame
->pc
, frame
->frame
, frame
->frame
))
431 return generic_read_register_dummy (frame
->pc
, frame
->frame
, PC_REGNUM
);
433 return frame
->from_pc
;
437 frame_locals_address (fi
)
438 struct frame_info
*fi
;
440 if (PC_IN_CALL_DUMMY(fi
->pc
, fi
->frame
, fi
->frame
))
441 return (CORE_ADDR
) 0; /* Not sure what else to do... */
442 if (!fi
->locals_pointer
)
444 struct frame_saved_regs ignore
;
446 get_frame_saved_regs (fi
, &ignore
);
449 return fi
->locals_pointer
;
452 /* Return the address of the argument block for the frame
453 described by FI. Returns 0 if the address is unknown. */
456 frame_args_address (fi
)
457 struct frame_info
*fi
;
459 if (PC_IN_CALL_DUMMY(fi
->pc
, fi
->frame
, fi
->frame
))
460 return (CORE_ADDR
) 0; /* Not sure what else to do... */
461 if (!fi
->args_pointer
)
463 struct frame_saved_regs ignore
;
465 get_frame_saved_regs (fi
, &ignore
);
469 return fi
->args_pointer
;
472 /* Function: push_arguments
473 Setup the function arguments for calling a function in the inferior.
475 On the Hitachi H8/300 architecture, there are three registers (R0 to R2)
476 which are dedicated for passing function arguments. Up to the first
477 three arguments (depending on size) may go into these registers.
478 The rest go on the stack.
480 Arguments that are smaller than WORDSIZE bytes will still take up a
481 whole register or a whole WORDSIZE word on the stack, and will be
482 right-justified in the register or the stack word. This includes
483 chars and small aggregate types. Note that WORDSIZE depends on the
486 Arguments that are larger than WORDSIZE bytes will be split between
487 two or more registers as available, but will NOT be split between a
488 register and the stack.
490 An exceptional case exists for struct arguments (and possibly other
491 aggregates such as arrays) -- if the size is larger than WORDSIZE
492 bytes but not a multiple of WORDSIZE bytes. In this case the
493 argument is never split between the registers and the stack, but
494 instead is copied in its entirety onto the stack, AND also copied
495 into as many registers as there is room for. In other words, space
496 in registers permitting, two copies of the same argument are passed
497 in. As far as I can tell, only the one on the stack is used,
498 although that may be a function of the level of compiler
499 optimization. I suspect this is a compiler bug. Arguments of
500 these odd sizes are left-justified within the word (as opposed to
501 arguments smaller than WORDSIZE bytes, which are right-justified).
503 If the function is to return an aggregate type such as a struct,
504 the caller must allocate space into which the callee will copy the
505 return value. In this case, a pointer to the return value location
506 is passed into the callee in register R0, which displaces one of
507 the other arguments passed in via registers R0 to R2. */
510 h8300_push_arguments(nargs
, args
, sp
, struct_return
, struct_addr
)
514 unsigned char struct_return
;
515 CORE_ADDR struct_addr
;
517 int stack_align
, stack_alloc
, stack_offset
;
527 if (h8300hmode
|| h8300smode
)
538 /* first force sp to a n-byte alignment */
539 sp
= sp
& ~stack_align
;
541 /* Now make sure there's space on the stack */
542 for (argnum
= 0, stack_alloc
= 0;
543 argnum
< nargs
; argnum
++)
544 stack_alloc
+= ((TYPE_LENGTH(VALUE_TYPE(args
[argnum
])) + stack_align
)
546 sp
-= stack_alloc
; /* make room on stack for args */
547 /* we may over-allocate a little here, but that won't hurt anything */
549 argreg
= ARG0_REGNUM
;
550 if (struct_return
) /* "struct return" pointer takes up one argreg */
552 write_register (argreg
++, struct_addr
);
555 /* Now load as many as possible of the first arguments into
556 registers, and push the rest onto the stack. There are 3N bytes
557 in three registers available. Loop thru args from first to last. */
559 for (argnum
= 0, stack_offset
= 0; argnum
< nargs
; argnum
++)
561 type
= VALUE_TYPE (args
[argnum
]);
562 len
= TYPE_LENGTH (type
);
563 memset(valbuf
, 0, sizeof(valbuf
));
566 /* the purpose of this is to right-justify the value within the word */
567 memcpy(valbuf
+ (wordsize
- len
),
568 (char *) VALUE_CONTENTS (args
[argnum
]), len
);
572 val
= (char *) VALUE_CONTENTS (args
[argnum
]);
574 if (len
> (ARGLAST_REGNUM
+1 - argreg
) * REGISTER_RAW_SIZE(ARG0_REGNUM
) ||
575 (len
> wordsize
&& (len
& stack_align
) != 0))
576 { /* passed on the stack */
577 write_memory (sp
+ stack_offset
, val
,
578 len
< wordsize
? wordsize
: len
);
579 stack_offset
+= (len
+ stack_align
) & ~stack_align
;
581 /* NOTE WELL!!!!! This is not an "else if" clause!!!
582 That's because some *&^%$ things get passed on the stack
583 AND in the registers! */
584 if (len
<= (ARGLAST_REGNUM
+1 - argreg
) * REGISTER_RAW_SIZE(ARG0_REGNUM
))
586 { /* there's room in registers */
587 regval
= extract_address (val
, wordsize
);
588 write_register (argreg
, regval
);
597 /* Function: push_return_address
598 Setup the return address for a dummy frame, as called by
599 call_function_by_hand. Only necessary when you are using an
600 empty CALL_DUMMY, ie. the target will not actually be executing
601 a JSR/BSR instruction. */
604 h8300_push_return_address (pc
, sp
)
608 unsigned char buf
[4];
611 if (h8300hmode
|| h8300smode
)
616 #if CALL_DUMMY_LOCATION != AT_ENTRY_POINT
617 pc
= pc
- CALL_DUMMY_START_OFFSET
+ CALL_DUMMY_BREAKPOINT_OFFSET
;
619 pc
= CALL_DUMMY_ADDRESS ();
620 #endif /* CALL_DUMMY_LOCATION */
622 store_unsigned_integer (buf
, wordsize
, pc
);
623 write_memory (sp
, buf
, wordsize
);
627 /* Function: pop_frame
628 Restore the machine to the state it had before the current frame
629 was created. Usually used either by the "RETURN" command, or by
630 call_function_by_hand after the dummy_frame is finished. */
636 struct frame_saved_regs fsr
;
637 struct frame_info
*frame
= get_current_frame ();
639 if (PC_IN_CALL_DUMMY(frame
->pc
, frame
->frame
, frame
->frame
))
641 generic_pop_dummy_frame();
645 get_frame_saved_regs (frame
, &fsr
);
647 for (regnum
= 0; regnum
< 8; regnum
++)
649 /* Don't forget SP_REGNUM is a frame_saved_regs struct is the
650 actual value we want, not the address of the value we want. */
651 if (fsr
.regs
[regnum
] && regnum
!= SP_REGNUM
)
652 write_register (regnum
,
653 read_memory_integer(fsr
.regs
[regnum
], BINWORD
));
654 else if (fsr
.regs
[regnum
] && regnum
== SP_REGNUM
)
655 write_register (regnum
, frame
->frame
+ 2 * BINWORD
);
658 /* Don't forget the update the PC too! */
659 write_pc (frame
->from_pc
);
661 flush_cached_frames ();
664 /* Function: extract_return_value
665 Figure out where in REGBUF the called function has left its return value.
666 Copy that into VALBUF. Be sure to account for CPU type. */
669 h8300_extract_return_value (type
, regbuf
, valbuf
)
676 if (h8300smode
|| h8300hmode
)
681 len
= TYPE_LENGTH(type
);
685 case 2: /* (short), (int) */
686 memcpy (valbuf
, regbuf
+ REGISTER_BYTE(0) + (wordsize
- len
), len
);
688 case 4: /* (long), (float) */
689 if (h8300smode
|| h8300hmode
)
691 memcpy (valbuf
, regbuf
+ REGISTER_BYTE(0), 4);
695 memcpy (valbuf
, regbuf
+ REGISTER_BYTE(0), 2);
696 memcpy (valbuf
+2, regbuf
+ REGISTER_BYTE(1), 2);
699 case 8: /* (double) (doesn't seem to happen, which is good,
700 because this almost certainly isn't right. */
701 error ("I don't know how a double is returned.");
706 /* Function: store_return_value
707 Place the appropriate value in the appropriate registers.
708 Primarily used by the RETURN command. */
711 h8300_store_return_value (type
, valbuf
)
715 int wordsize
, len
, regval
;
717 if (h8300hmode
|| h8300smode
)
722 len
= TYPE_LENGTH(type
);
725 case 2: /* short, int */
726 regval
= extract_address(valbuf
, len
);
727 write_register (0, regval
);
729 case 4: /* long, float */
730 regval
= extract_address(valbuf
, len
);
731 if (h8300smode
|| h8300hmode
)
733 write_register (0, regval
);
737 write_register (0, regval
>> 16);
738 write_register (1, regval
& 0xffff);
741 case 8: /* presumeably double, but doesn't seem to happen */
742 error ("I don't know how to return a double.");
747 /* Function: get_saved_register
748 Just call the generic_get_saved_register function. */
751 get_saved_register (raw_buffer
, optimized
, addrp
, frame
, regnum
, lval
)
755 struct frame_info
*frame
;
757 enum lval_type
*lval
;
759 generic_get_saved_register (raw_buffer
, optimized
, addrp
,
760 frame
, regnum
, lval
);
763 struct cmd_list_element
*setmemorylist
;
766 h8300_command(args
, from_tty
)
768 extern int h8300hmode
;
774 h8300h_command(args
, from_tty
)
776 extern int h8300hmode
;
781 h8300s_command(args
, from_tty
)
783 extern int h8300smode
;
784 extern int h8300hmode
;
791 set_machine (args
, from_tty
)
795 printf_unfiltered ("\"set machine\" must be followed by h8300, h8300h");
796 printf_unfiltered ("or h8300s");
797 help_list (setmemorylist
, "set memory ", -1, gdb_stdout
);
800 /* set_machine_hook is called as the exec file is being opened, but
801 before the symbol file is opened. This allows us to set the
802 h8300hmode flag based on the machine type specified in the exec
803 file. This in turn will cause subsequently defined pointer types
804 to be 16 or 32 bits as appropriate for the machine. */
807 set_machine_hook (filename
)
810 if (bfd_get_mach (exec_bfd
) == bfd_mach_h8300s
)
816 if (bfd_get_mach (exec_bfd
) == bfd_mach_h8300h
)
829 _initialize_h8300m ()
831 add_prefix_cmd ("machine", no_class
, set_machine
,
832 "set the machine type",
833 &setmemorylist
, "set machine ", 0,
836 add_cmd ("h8300", class_support
, h8300_command
,
837 "Set machine to be H8/300.", &setmemorylist
);
839 add_cmd ("h8300h", class_support
, h8300h_command
,
840 "Set machine to be H8/300H.", &setmemorylist
);
842 add_cmd ("h8300s", class_support
, h8300s_command
,
843 "Set machine to be H8/300S.", &setmemorylist
);
845 /* Add a hook to set the machine type when we're loading a file. */
847 specify_exec_file_hook(set_machine_hook
);
853 print_register_hook (regno
)
861 read_relative_register_raw_bytes (regno
, b
);
862 l
= b
[REGISTER_VIRTUAL_SIZE(8) -1];
863 printf_unfiltered ("\t");
864 printf_unfiltered ("I-%d - ", (l
& 0x80) != 0);
865 printf_unfiltered ("H-%d - ", (l
& 0x20) != 0);
870 printf_unfiltered ("N-%d ", N
);
871 printf_unfiltered ("Z-%d ", Z
);
872 printf_unfiltered ("V-%d ", V
);
873 printf_unfiltered ("C-%d ", C
);
875 printf_unfiltered ("u> ");
877 printf_unfiltered ("u<= ");
879 printf_unfiltered ("u>= ");
881 printf_unfiltered ("u< ");
883 printf_unfiltered ("!= ");
885 printf_unfiltered ("== ");
887 printf_unfiltered (">= ");
889 printf_unfiltered ("< ");
890 if ((Z
| (N
^ V
)) == 0)
891 printf_unfiltered ("> ");
892 if ((Z
| (N
^ V
)) == 1)
893 printf_unfiltered ("<= ");
898 _initialize_h8300_tdep ()
900 tm_print_insn
= gdb_print_insn_h8300
;