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
33 #define UNSIGNED_SHORT(X) ((X) & 0xffff)
35 /* an easy to debug H8 stack frame looks like:
39 0x7905 nnnn mov.w #n,r5 or 0x1b87 subs #2,sp
44 #define IS_PUSH(x) ((x & 0xff00)==0x6d00)
45 #define IS_PUSH_FP(x) (x == 0x6df6)
46 #define IS_MOVE_FP(x) (x == 0x0d76)
47 #define IS_MOV_SP_FP(x) (x == 0x0d76)
48 #define IS_SUB2_SP(x) (x==0x1b87)
49 #define IS_MOVK_R5(x) (x==0x7905)
50 #define IS_SUB_R5SP(x) (x==0x1957)
51 CORE_ADDR
examine_prologue ();
53 void frame_find_saved_regs ();
55 h8300_skip_prologue (start_pc
)
61 w
= read_memory_unsigned_integer (start_pc
, 2);
62 /* Skip past all push insns */
63 while (IS_PUSH_FP (w
))
66 w
= read_memory_unsigned_integer (start_pc
, 2);
69 /* Skip past a move to FP */
73 w
= read_memory_unsigned_integer (start_pc
, 2);
76 /* Skip the stack adjust */
81 w
= read_memory_unsigned_integer (start_pc
, 2);
86 w
= read_memory_unsigned_integer (start_pc
, 2);
88 while (IS_SUB2_SP (w
))
91 w
= read_memory_unsigned_integer (start_pc
, 2);
99 print_insn (memaddr
, stream
)
103 disassemble_info info
;
104 GDB_INIT_DISASSEMBLE_INFO(info
, stream
);
105 return print_insn_h8300 (memaddr
, &info
);
108 /* Given a GDB frame, determine the address of the calling function's frame.
109 This will be used to create a new GDB frame struct, and then
110 INIT_EXTRA_FRAME_INFO and INIT_FRAME_PC will be called for the new frame.
112 For us, the frame address is its stack pointer value, so we look up
113 the function prologue to determine the caller's sp value, and return it. */
116 FRAME_CHAIN (thisframe
)
120 frame_find_saved_regs (thisframe
, (struct frame_saved_regs
*) 0);
121 return thisframe
->fsr
->regs
[SP_REGNUM
];
124 /* Put here the code to store, into a struct frame_saved_regs,
125 the addresses of the saved registers of frame described by FRAME_INFO.
126 This includes special registers such as pc and fp saved in special
127 ways in the stack frame. sp is even more special:
128 the address we return for it IS the sp for the next frame.
130 We cache the result of doing this in the frame_cache_obstack, since
131 it is fairly expensive. */
134 frame_find_saved_regs (fi
, fsr
)
135 struct frame_info
*fi
;
136 struct frame_saved_regs
*fsr
;
138 register CORE_ADDR next_addr
;
139 register CORE_ADDR
*saved_regs
;
141 register struct frame_saved_regs
*cache_fsr
;
142 extern struct obstack frame_cache_obstack
;
144 struct symtab_and_line sal
;
149 cache_fsr
= (struct frame_saved_regs
*)
150 obstack_alloc (&frame_cache_obstack
,
151 sizeof (struct frame_saved_regs
));
152 bzero (cache_fsr
, sizeof (struct frame_saved_regs
));
156 /* Find the start and end of the function prologue. If the PC
157 is in the function prologue, we only consider the part that
158 has executed already. */
160 ip
= get_pc_function_start (fi
->pc
);
161 sal
= find_pc_line (ip
, 0);
162 limit
= (sal
.end
&& sal
.end
< fi
->pc
) ? sal
.end
: fi
->pc
;
164 /* This will fill in fields in *fi as well as in cache_fsr. */
165 examine_prologue (ip
, limit
, fi
->frame
, cache_fsr
, fi
);
172 /* Fetch the instruction at ADDR, returning 0 if ADDR is beyond LIM or
173 is not the address of a valid instruction, the address of the next
174 instruction beyond ADDR otherwise. *PWORD1 receives the first word
175 of the instruction.*/
178 NEXT_PROLOGUE_INSN (addr
, lim
, pword1
)
185 read_memory (addr
, pword1
, sizeof (*pword1
));
186 SWAP_TARGET_AND_HOST (pword1
, sizeof (short));
193 /* Examine the prologue of a function. `ip' points to the first instruction.
194 `limit' is the limit of the prologue (e.g. the addr of the first
195 linenumber, or perhaps the program counter if we're stepping through).
196 `frame_sp' is the stack pointer value in use in this frame.
197 `fsr' is a pointer to a frame_saved_regs structure into which we put
198 info about the registers saved by this frame.
199 `fi' is a struct frame_info pointer; we fill in various fields in it
200 to reflect the offsets of the arg pointer and the locals pointer. */
203 examine_prologue (ip
, limit
, after_prolog_fp
, fsr
, fi
)
204 register CORE_ADDR ip
;
205 register CORE_ADDR limit
;
206 FRAME_ADDR after_prolog_fp
;
207 struct frame_saved_regs
*fsr
;
208 struct frame_info
*fi
;
210 register CORE_ADDR next_ip
;
216 register struct pic_prologue_code
*pcode
;
219 unsigned int reg_save_depth
= 2; /* Number of things pushed onto
220 stack, starts at 2, 'cause the
221 PC is already there */
223 unsigned int auto_depth
= 0; /* Number of bytes of autos */
225 char in_frame
[11]; /* One for each reg */
227 memset (in_frame
, 1, 11);
228 for (r
= 0; r
< 8; r
++)
232 if (after_prolog_fp
== 0)
234 after_prolog_fp
= read_register (SP_REGNUM
);
236 if (ip
== 0 || ip
& ~0xffff)
239 next_ip
= NEXT_PROLOGUE_INSN (ip
, limit
, &insn_word
);
241 /* Skip over any fp push instructions */
242 fsr
->regs
[6] = after_prolog_fp
;
243 while (next_ip
&& IS_PUSH_FP (insn_word
))
247 in_frame
[insn_word
& 0x7] = reg_save_depth
;
248 next_ip
= NEXT_PROLOGUE_INSN (ip
, limit
, &insn_word
);
252 /* Is this a move into the fp */
253 if (next_ip
&& IS_MOV_SP_FP (insn_word
))
256 next_ip
= NEXT_PROLOGUE_INSN (ip
, limit
, &insn_word
);
260 /* Skip over any stack adjustment, happens either with a number of
261 sub#2,sp or a mov #x,r5 sub r5,sp */
263 if (next_ip
&& IS_SUB2_SP (insn_word
))
265 while (next_ip
&& IS_SUB2_SP (insn_word
))
269 next_ip
= NEXT_PROLOGUE_INSN (ip
, limit
, &insn_word
);
274 if (next_ip
&& IS_MOVK_R5 (insn_word
))
277 next_ip
= NEXT_PROLOGUE_INSN (ip
, limit
, &insn_word
);
278 auto_depth
+= insn_word
;
280 next_ip
= NEXT_PROLOGUE_INSN (next_ip
, limit
, &insn_word
);
281 auto_depth
+= insn_word
;
285 /* Work out which regs are stored where */
286 while (next_ip
&& IS_PUSH (insn_word
))
289 next_ip
= NEXT_PROLOGUE_INSN (ip
, limit
, &insn_word
);
290 fsr
->regs
[r
] = after_prolog_fp
+ auto_depth
;
294 /* The args are always reffed based from the stack pointer */
295 fi
->args_pointer
= after_prolog_fp
;
296 /* Locals are always reffed based from the fp */
297 fi
->locals_pointer
= after_prolog_fp
;
298 /* The PC is at a known place */
299 fi
->from_pc
= read_memory_unsigned_integer (after_prolog_fp
+ 2, BINWORD
);
301 /* Rememeber any others too */
302 in_frame
[PC_REGNUM
] = 0;
305 /* We keep the old FP in the SP spot */
306 fsr
->regs
[SP_REGNUM
] = (read_memory_unsigned_integer
307 (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 */
326 /* Return the saved PC from this frame.
328 If the frame has a memory copy of SRP_REGNUM, use that. If not,
329 just use the register SRP_REGNUM itself. */
332 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
),
399 print_register_hook (regno
)
409 read_relative_register_raw_bytes (regno
, b
);
412 printf ("I-%d - ", (l
& 0x80) != 0);
413 printf ("H-%d - ", (l
& 0x20) != 0);
438 if ((Z
| (N
^ V
)) == 0)
440 if ((Z
| (N
^ V
)) == 1)