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"
40 #define UNSIGNED_SHORT(X) ((X) & 0xffff)
42 /* an easy to debug H8 stack frame looks like:
46 0x7905 nnnn mov.w #n,r5 or 0x1b87 subs #2,sp
51 #define IS_PUSH(x) ((x & 0xff00)==0x6d00)
52 #define IS_PUSH_FP(x) (x == 0x6df6)
53 #define IS_MOVE_FP(x) (x == 0x0d76)
54 #define IS_MOV_SP_FP(x) (x == 0x0d76)
55 #define IS_SUB2_SP(x) (x==0x1b87)
56 #define IS_MOVK_R5(x) (x==0x7905)
57 #define IS_SUB_R5SP(x) (x==0x1957)
59 /* Local function declarations. */
61 static CORE_ADDR examine_prologue ();
62 static void set_machine_hook PARAMS ((char *filename));
64 void frame_find_saved_regs ();
66 h8300_skip_prologue (start_pc)
71 w = read_memory_unsigned_integer (start_pc, 2);
72 /* Skip past all push insns */
73 while (IS_PUSH_FP (w))
76 w = read_memory_unsigned_integer (start_pc, 2);
79 /* Skip past a move to FP */
83 w = read_memory_unsigned_integer (start_pc, 2);
86 /* Skip the stack adjust */
91 w = read_memory_unsigned_integer (start_pc, 2);
96 w = read_memory_unsigned_integer (start_pc, 2);
98 while (IS_SUB2_SP (w))
101 w = read_memory_unsigned_integer (start_pc, 2);
108 gdb_print_insn_h8300 (memaddr, info)
110 disassemble_info *info;
113 return print_insn_h8300h (memaddr, info);
115 return print_insn_h8300 (memaddr, info);
118 /* Given a GDB frame, determine the address of the calling function's frame.
119 This will be used to create a new GDB frame struct, and then
120 INIT_EXTRA_FRAME_INFO and INIT_FRAME_PC will be called for the new frame.
122 For us, the frame address is its stack pointer value, so we look up
123 the function prologue to determine the caller's sp value, and return it. */
126 h8300_frame_chain (thisframe)
127 struct frame_info *thisframe;
129 frame_find_saved_regs (thisframe, (struct frame_saved_regs *) 0);
130 return thisframe->fsr->regs[SP_REGNUM];
133 /* Put here the code to store, into a struct frame_saved_regs,
134 the addresses of the saved registers of frame described by FRAME_INFO.
135 This includes special registers such as pc and fp saved in special
136 ways in the stack frame. sp is even more special:
137 the address we return for it IS the sp for the next frame.
139 We cache the result of doing this in the frame_cache_obstack, since
140 it is fairly expensive. */
143 frame_find_saved_regs (fi, fsr)
144 struct frame_info *fi;
145 struct frame_saved_regs *fsr;
147 register struct frame_saved_regs *cache_fsr;
148 extern struct obstack frame_cache_obstack;
150 struct symtab_and_line sal;
155 cache_fsr = (struct frame_saved_regs *)
156 obstack_alloc (&frame_cache_obstack,
157 sizeof (struct frame_saved_regs));
158 memset (cache_fsr, '\0', sizeof (struct frame_saved_regs));
162 /* Find the start and end of the function prologue. If the PC
163 is in the function prologue, we only consider the part that
164 has executed already. */
166 ip = get_pc_function_start (fi->pc);
167 sal = find_pc_line (ip, 0);
168 limit = (sal.end && sal.end < fi->pc) ? sal.end : fi->pc;
170 /* This will fill in fields in *fi as well as in cache_fsr. */
171 examine_prologue (ip, limit, fi->frame, cache_fsr, fi);
178 /* Fetch the instruction at ADDR, returning 0 if ADDR is beyond LIM or
179 is not the address of a valid instruction, the address of the next
180 instruction beyond ADDR otherwise. *PWORD1 receives the first word
181 of the instruction.*/
184 NEXT_PROLOGUE_INSN (addr, lim, pword1)
192 read_memory (addr, buf, 2);
193 *pword1 = extract_signed_integer (buf, 2);
200 /* Examine the prologue of a function. `ip' points to the first instruction.
201 `limit' is the limit of the prologue (e.g. the addr of the first
202 linenumber, or perhaps the program counter if we're stepping through).
203 `frame_sp' is the stack pointer value in use in this frame.
204 `fsr' is a pointer to a frame_saved_regs structure into which we put
205 info about the registers saved by this frame.
206 `fi' is a struct frame_info pointer; we fill in various fields in it
207 to reflect the offsets of the arg pointer and the locals pointer. */
210 examine_prologue (ip, limit, after_prolog_fp, fsr, fi)
211 register CORE_ADDR ip;
212 register CORE_ADDR limit;
213 CORE_ADDR after_prolog_fp;
214 struct frame_saved_regs *fsr;
215 struct frame_info *fi;
217 register CORE_ADDR next_ip;
221 /* Number of things pushed onto stack, starts at 2/4, 'cause the
222 PC is already there */
223 unsigned int reg_save_depth = h8300hmode ? 4 : 2;
225 unsigned int auto_depth = 0; /* Number of bytes of autos */
227 char in_frame[11]; /* One for each reg */
229 memset (in_frame, 1, 11);
230 for (r = 0; r < 8; r++)
234 if (after_prolog_fp == 0)
236 after_prolog_fp = read_register (SP_REGNUM);
238 if (ip == 0 || ip & (h8300hmode ? ~0xffff : ~0xffff))
241 next_ip = NEXT_PROLOGUE_INSN (ip, limit, &insn_word);
243 /* Skip over any fp push instructions */
244 fsr->regs[6] = after_prolog_fp;
245 while (next_ip && IS_PUSH_FP (insn_word))
249 in_frame[insn_word & 0x7] = reg_save_depth;
250 next_ip = NEXT_PROLOGUE_INSN (ip, limit, &insn_word);
254 /* Is this a move into the fp */
255 if (next_ip && IS_MOV_SP_FP (insn_word))
258 next_ip = NEXT_PROLOGUE_INSN (ip, limit, &insn_word);
262 /* Skip over any stack adjustment, happens either with a number of
263 sub#2,sp or a mov #x,r5 sub r5,sp */
265 if (next_ip && IS_SUB2_SP (insn_word))
267 while (next_ip && IS_SUB2_SP (insn_word))
271 next_ip = NEXT_PROLOGUE_INSN (ip, limit, &insn_word);
276 if (next_ip && IS_MOVK_R5 (insn_word))
279 next_ip = NEXT_PROLOGUE_INSN (ip, limit, &insn_word);
280 auto_depth += insn_word;
282 next_ip = NEXT_PROLOGUE_INSN (next_ip, limit, &insn_word);
283 auto_depth += insn_word;
286 /* Work out which regs are stored where */
287 while (next_ip && IS_PUSH (insn_word))
290 next_ip = NEXT_PROLOGUE_INSN (ip, limit, &insn_word);
291 fsr->regs[r] = after_prolog_fp + auto_depth;
295 /* The args are always reffed based from the stack pointer */
296 fi->args_pointer = after_prolog_fp;
297 /* Locals are always reffed based from the fp */
298 fi->locals_pointer = after_prolog_fp;
299 /* The PC is at a known place */
300 fi->from_pc = read_memory_unsigned_integer (after_prolog_fp + 2, BINWORD);
302 /* Rememeber any others too */
303 in_frame[PC_REGNUM] = 0;
306 /* We keep the old FP in the SP spot */
307 fsr->regs[SP_REGNUM] = read_memory_unsigned_integer (fsr->regs[6], BINWORD);
309 fsr->regs[SP_REGNUM] = after_prolog_fp + auto_depth;
315 init_extra_frame_info (fromleaf, fi)
317 struct frame_info *fi;
319 fi->fsr = 0; /* Not yet allocated */
320 fi->args_pointer = 0; /* Unknown */
321 fi->locals_pointer = 0; /* Unknown */
325 /* Return the saved PC from this frame.
327 If the frame has a memory copy of SRP_REGNUM, use that. If not,
328 just use the register SRP_REGNUM itself. */
331 frame_saved_pc (frame)
332 struct frame_info *frame;
334 return frame->from_pc;
338 frame_locals_address (fi)
339 struct frame_info *fi;
341 if (!fi->locals_pointer)
343 struct frame_saved_regs ignore;
345 get_frame_saved_regs (fi, &ignore);
348 return fi->locals_pointer;
351 /* Return the address of the argument block for the frame
352 described by FI. Returns 0 if the address is unknown. */
355 frame_args_address (fi)
356 struct frame_info *fi;
358 if (!fi->args_pointer)
360 struct frame_saved_regs ignore;
362 get_frame_saved_regs (fi, &ignore);
366 return fi->args_pointer;
373 struct frame_saved_regs fsr;
374 struct frame_info *frame = get_current_frame ();
376 get_frame_saved_regs (frame, &fsr);
378 for (regnum = 0; regnum < 8; regnum++)
380 /* Don't forget SP_REGNUM is a frame_saved_regs struct is the
381 actual value we want, not the address of the value we want. */
382 if (fsr.regs[regnum] && regnum != SP_REGNUM)
383 write_register (regnum, read_memory_integer(fsr.regs[regnum], BINWORD));
384 else if (fsr.regs[regnum] && regnum == SP_REGNUM)
385 write_register (regnum, fsr.regs[regnum]);
388 /* Don't forget the update the PC too! */
389 write_pc (frame->from_pc);
390 flush_cached_frames ();
394 struct cmd_list_element *setmemorylist;
397 h8300_command(args, from_tty)
399 extern int h8300hmode;
404 h8300h_command(args, from_tty)
406 extern int h8300hmode;
411 set_machine (args, from_tty)
415 printf_unfiltered ("\"set machine\" must be followed by h8300 or h8300h.\n");
416 help_list (setmemorylist, "set memory ", -1, gdb_stdout);
419 /* set_machine_hook is called as the exec file is being opened, but
420 before the symbol file is opened. This allows us to set the
421 h8300hmode flag based on the machine type specified in the exec
422 file. This in turn will cause subsequently defined pointer types
423 to be 16 or 32 bits as appropriate for the machine. */
426 set_machine_hook (filename)
429 h8300hmode = (bfd_get_mach (exec_bfd) == bfd_mach_h8300h);
433 _initialize_h8300m ()
435 add_prefix_cmd ("machine", no_class, set_machine,
436 "set the machine type", &setmemorylist, "set machine ", 0,
439 add_cmd ("h8300", class_support, h8300_command,
440 "Set machine to be H8/300.", &setmemorylist);
442 add_cmd ("h8300h", class_support, h8300h_command,
443 "Set machine to be H8/300H.", &setmemorylist);
445 /* Add a hook to set the machine type when we're loading a file. */
447 specify_exec_file_hook(set_machine_hook);
453 print_register_hook (regno)
461 read_relative_register_raw_bytes (regno, b);
462 l = b[REGISTER_VIRTUAL_SIZE(8) -1];
463 printf_unfiltered ("\t");
464 printf_unfiltered ("I-%d - ", (l & 0x80) != 0);
465 printf_unfiltered ("H-%d - ", (l & 0x20) != 0);
470 printf_unfiltered ("N-%d ", N);
471 printf_unfiltered ("Z-%d ", Z);
472 printf_unfiltered ("V-%d ", V);
473 printf_unfiltered ("C-%d ", C);
475 printf_unfiltered ("u> ");
477 printf_unfiltered ("u<= ");
479 printf_unfiltered ("u>= ");
481 printf_unfiltered ("u< ");
483 printf_unfiltered ("!= ");
485 printf_unfiltered ("== ");
487 printf_unfiltered (">= ");
489 printf_unfiltered ("< ");
490 if ((Z | (N ^ V)) == 0)
491 printf_unfiltered ("> ");
492 if ((Z | (N ^ V)) == 1)
493 printf_unfiltered ("<= ");
498 _initialize_h8300_tdep ()
500 tm_print_insn = gdb_print_insn_h8300;