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., 675 Mass Ave, Cambridge, MA 02139, USA. */
21 Contributed by Steve Chamberlain
36 #define UNSIGNED_SHORT(X) ((X) & 0xffff)
38 /* an easy to debug H8 stack frame looks like:
42 0x7905 nnnn mov.w #n,r5 or 0x1b87 subs #2,sp
47 #define IS_PUSH(x) ((x & 0xff00)==0x6d00)
48 #define IS_PUSH_FP(x) (x == 0x6df6)
49 #define IS_MOVE_FP(x) (x == 0x0d76)
50 #define IS_MOV_SP_FP(x) (x == 0x0d76)
51 #define IS_SUB2_SP(x) (x==0x1b87)
52 #define IS_MOVK_R5(x) (x==0x7905)
53 #define IS_SUB_R5SP(x) (x==0x1957)
55 static CORE_ADDR examine_prologue ();
57 void frame_find_saved_regs ();
59 h8300_skip_prologue (start_pc)
64 w = read_memory_unsigned_integer (start_pc, 2);
65 /* Skip past all push insns */
66 while (IS_PUSH_FP (w))
69 w = read_memory_unsigned_integer (start_pc, 2);
72 /* Skip past a move to FP */
76 w = read_memory_unsigned_integer (start_pc, 2);
79 /* Skip the stack adjust */
84 w = read_memory_unsigned_integer (start_pc, 2);
89 w = read_memory_unsigned_integer (start_pc, 2);
91 while (IS_SUB2_SP (w))
94 w = read_memory_unsigned_integer (start_pc, 2);
101 print_insn (memaddr, stream)
105 disassemble_info info;
106 GDB_INIT_DISASSEMBLE_INFO(info, stream);
108 return print_insn_h8300h (memaddr, &info);
110 return print_insn_h8300 (memaddr, &info);
113 /* Given a GDB frame, determine the address of the calling function's frame.
114 This will be used to create a new GDB frame struct, and then
115 INIT_EXTRA_FRAME_INFO and INIT_FRAME_PC will be called for the new frame.
117 For us, the frame address is its stack pointer value, so we look up
118 the function prologue to determine the caller's sp value, and return it. */
121 FRAME_CHAIN (thisframe)
124 frame_find_saved_regs (thisframe, (struct frame_saved_regs *) 0);
125 return thisframe->fsr->regs[SP_REGNUM];
128 /* Put here the code to store, into a struct frame_saved_regs,
129 the addresses of the saved registers of frame described by FRAME_INFO.
130 This includes special registers such as pc and fp saved in special
131 ways in the stack frame. sp is even more special:
132 the address we return for it IS the sp for the next frame.
134 We cache the result of doing this in the frame_cache_obstack, since
135 it is fairly expensive. */
138 frame_find_saved_regs (fi, fsr)
139 struct frame_info *fi;
140 struct frame_saved_regs *fsr;
142 register CORE_ADDR next_addr;
143 register CORE_ADDR *saved_regs;
145 register struct frame_saved_regs *cache_fsr;
146 extern struct obstack frame_cache_obstack;
148 struct symtab_and_line sal;
153 cache_fsr = (struct frame_saved_regs *)
154 obstack_alloc (&frame_cache_obstack,
155 sizeof (struct frame_saved_regs));
156 memset (cache_fsr, '\0', sizeof (struct frame_saved_regs));
160 /* Find the start and end of the function prologue. If the PC
161 is in the function prologue, we only consider the part that
162 has executed already. */
164 ip = get_pc_function_start (fi->pc);
165 sal = find_pc_line (ip, 0);
166 limit = (sal.end && sal.end < fi->pc) ? sal.end : fi->pc;
168 /* This will fill in fields in *fi as well as in cache_fsr. */
169 examine_prologue (ip, limit, fi->frame, cache_fsr, fi);
176 /* Fetch the instruction at ADDR, returning 0 if ADDR is beyond LIM or
177 is not the address of a valid instruction, the address of the next
178 instruction beyond ADDR otherwise. *PWORD1 receives the first word
179 of the instruction.*/
182 NEXT_PROLOGUE_INSN (addr, lim, pword1)
190 read_memory (addr, buf, 2);
191 *pword1 = extract_signed_integer (buf, 2);
198 /* Examine the prologue of a function. `ip' points to the first instruction.
199 `limit' is the limit of the prologue (e.g. the addr of the first
200 linenumber, or perhaps the program counter if we're stepping through).
201 `frame_sp' is the stack pointer value in use in this frame.
202 `fsr' is a pointer to a frame_saved_regs structure into which we put
203 info about the registers saved by this frame.
204 `fi' is a struct frame_info pointer; we fill in various fields in it
205 to reflect the offsets of the arg pointer and the locals pointer. */
208 examine_prologue (ip, limit, after_prolog_fp, fsr, fi)
209 register CORE_ADDR ip;
210 register CORE_ADDR limit;
211 FRAME_ADDR after_prolog_fp;
212 struct frame_saved_regs *fsr;
213 struct frame_info *fi;
215 register CORE_ADDR next_ip;
220 register struct pic_prologue_code *pcode;
223 /* Number of things pushed onto stack, starts at 2/4, 'cause the
224 PC is already there */
225 unsigned int reg_save_depth = h8300hmode ? 4 : 2;
227 unsigned int auto_depth = 0; /* Number of bytes of autos */
229 char in_frame[11]; /* One for each reg */
231 memset (in_frame, 1, 11);
232 for (r = 0; r < 8; r++)
236 if (after_prolog_fp == 0)
238 after_prolog_fp = read_register (SP_REGNUM);
240 if (ip == 0 || ip & (h8300hmode ? ~0xffff : ~0xffff))
243 next_ip = NEXT_PROLOGUE_INSN (ip, limit, &insn_word);
245 /* Skip over any fp push instructions */
246 fsr->regs[6] = after_prolog_fp;
247 while (next_ip && IS_PUSH_FP (insn_word))
251 in_frame[insn_word & 0x7] = reg_save_depth;
252 next_ip = NEXT_PROLOGUE_INSN (ip, limit, &insn_word);
256 /* Is this a move into the fp */
257 if (next_ip && IS_MOV_SP_FP (insn_word))
260 next_ip = NEXT_PROLOGUE_INSN (ip, limit, &insn_word);
264 /* Skip over any stack adjustment, happens either with a number of
265 sub#2,sp or a mov #x,r5 sub r5,sp */
267 if (next_ip && IS_SUB2_SP (insn_word))
269 while (next_ip && IS_SUB2_SP (insn_word))
273 next_ip = NEXT_PROLOGUE_INSN (ip, limit, &insn_word);
278 if (next_ip && IS_MOVK_R5 (insn_word))
281 next_ip = NEXT_PROLOGUE_INSN (ip, limit, &insn_word);
282 auto_depth += insn_word;
284 next_ip = NEXT_PROLOGUE_INSN (next_ip, limit, &insn_word);
285 auto_depth += insn_word;
288 /* Work out which regs are stored where */
289 while (next_ip && IS_PUSH (insn_word))
292 next_ip = NEXT_PROLOGUE_INSN (ip, limit, &insn_word);
293 fsr->regs[r] = after_prolog_fp + auto_depth;
297 /* The args are always reffed based from the stack pointer */
298 fi->args_pointer = after_prolog_fp;
299 /* Locals are always reffed based from the fp */
300 fi->locals_pointer = after_prolog_fp;
301 /* The PC is at a known place */
302 fi->from_pc = read_memory_unsigned_integer (after_prolog_fp + 2, BINWORD);
304 /* Rememeber any others too */
305 in_frame[PC_REGNUM] = 0;
308 /* We keep the old FP in the SP spot */
309 fsr->regs[SP_REGNUM] = read_memory_unsigned_integer (fsr->regs[6], BINWORD);
311 fsr->regs[SP_REGNUM] = after_prolog_fp + auto_depth;
317 init_extra_frame_info (fromleaf, fi)
319 struct frame_info *fi;
321 fi->fsr = 0; /* Not yet allocated */
322 fi->args_pointer = 0; /* Unknown */
323 fi->locals_pointer = 0; /* Unknown */
327 /* Return the saved PC from this frame.
329 If the frame has a memory copy of SRP_REGNUM, use that. If not,
330 just use the register SRP_REGNUM itself. */
333 frame_saved_pc (frame)
336 return frame->from_pc;
340 frame_locals_address (fi)
341 struct frame_info *fi;
343 if (!fi->locals_pointer)
345 struct frame_saved_regs ignore;
347 get_frame_saved_regs (fi, &ignore);
350 return fi->locals_pointer;
353 /* Return the address of the argument block for the frame
354 described by FI. Returns 0 if the address is unknown. */
357 frame_args_address (fi)
358 struct frame_info *fi;
360 if (!fi->args_pointer)
362 struct frame_saved_regs ignore;
364 get_frame_saved_regs (fi, &ignore);
368 return fi->args_pointer;
375 struct frame_saved_regs fsr;
376 struct frame_info *fi;
378 FRAME frame = get_current_frame ();
380 fi = get_frame_info (frame);
381 get_frame_saved_regs (fi, &fsr);
383 for (regnum = 0; regnum < 8; regnum++)
385 if (fsr.regs[regnum])
387 write_register (regnum, read_memory_integer(fsr.regs[regnum]), BINWORD);
390 flush_cached_frames ();
391 set_current_frame (create_new_frame (read_register (FP_REGNUM),
397 struct cmd_list_element *setmemorylist;
400 h8300_command(args, from_tty)
402 extern int h8300hmode;
407 h8300h_command(args, from_tty)
409 extern int h8300hmode;
414 set_machine (args, from_tty)
418 printf_unfiltered ("\"set machine\" must be followed by h8300 or h8300h.\n");
419 help_list (setmemorylist, "set memory ", -1, gdb_stdout);
423 _initialize_h8300m ()
425 add_prefix_cmd ("machine", no_class, set_machine,
426 "set the machine type", &setmemorylist, "set machine ", 0,
429 add_cmd ("h8300", class_support, h8300_command,
430 "Set machine to be H8/300.", &setmemorylist);
432 add_cmd ("h8300h", class_support, h8300h_command,
433 "Set machine to be H8/300H.", &setmemorylist);
439 print_register_hook (regno)
449 read_relative_register_raw_bytes (regno, b);
451 printf_unfiltered ("\t");
452 printf_unfiltered ("I-%d - ", (l & 0x80) != 0);
453 printf_unfiltered ("H-%d - ", (l & 0x20) != 0);
458 printf_unfiltered ("N-%d ", N);
459 printf_unfiltered ("Z-%d ", Z);
460 printf_unfiltered ("V-%d ", V);
461 printf_unfiltered ("C-%d ", C);
463 printf_unfiltered ("u> ");
465 printf_unfiltered ("u<= ");
467 printf_unfiltered ("u>= ");
469 printf_unfiltered ("u< ");
471 printf_unfiltered ("!= ");
473 printf_unfiltered ("== ");
475 printf_unfiltered (">= ");
477 printf_unfiltered ("< ");
478 if ((Z | (N ^ V)) == 0)
479 printf_unfiltered ("> ");
480 if ((Z | (N ^ V)) == 1)
481 printf_unfiltered ("<= ");