* blockframe.c (inside_entry_file, inside_main_func,
[deliverable/binutils-gdb.git] / gdb / h8300-tdep.c
CommitLineData
1f46923f
SC
1/* Target-machine dependent code for Hitachi H8/300, for GDB.
2 Copyright (C) 1988, 1990, 1991 Free Software Foundation, Inc.
3
4This file is part of GDB.
5
6This program is free software; you can redistribute it and/or modify
7it under the terms of the GNU General Public License as published by
8the Free Software Foundation; either version 2 of the License, or
9(at your option) any later version.
10
11This program is distributed in the hope that it will be useful,
12but WITHOUT ANY WARRANTY; without even the implied warranty of
13MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14GNU General Public License for more details.
15
16You should have received a copy of the GNU General Public License
17along with this program; if not, write to the Free Software
18Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
19
ec25d19b 20/*
1f46923f 21 Contributed by Steve Chamberlain
ec25d19b 22 sac@cygnus.com
1f46923f
SC
23 */
24
400943fb 25#include "defs.h"
1f46923f
SC
26#include "frame.h"
27#include "obstack.h"
28#include "symtab.h"
29#define UNSIGNED_SHORT(X) ((X) & 0xffff)
400943fb
SC
30
31/* an easy to debug H8 stack frame looks like:
ec25d19b
SC
320x6df6 push r6
330x0d76 mov.w r7,r6
340x6dfn push reg
350x7905 nnnn mov.w #n,r5 or 0x1b87 subs #2,sp
360x1957 sub.w r5,sp
400943fb
SC
37
38 */
1f46923f 39
400943fb 40#define IS_PUSH(x) ((x & 0xff00)==0x6d00)
ec25d19b 41#define IS_PUSH_FP(x) (x == 0x6df6)
1f46923f
SC
42#define IS_MOVE_FP(x) (x == 0x0d76)
43#define IS_MOV_SP_FP(x) (x == 0x0d76)
44#define IS_SUB2_SP(x) (x==0x1b87)
45#define IS_MOVK_R5(x) (x==0x7905)
ec25d19b
SC
46#define IS_SUB_R5SP(x) (x==0x1957)
47CORE_ADDR examine_prologue ();
1f46923f 48
ec25d19b
SC
49void frame_find_saved_regs ();
50CORE_ADDR
51h8300_skip_prologue (start_pc)
52 CORE_ADDR start_pc;
0a8f9d31 53
0a8f9d31 54{
ec25d19b 55 short int w;
1f46923f 56
ec25d19b 57 w = read_memory_short (start_pc);
400943fb 58 /* Skip past all push insns */
ec25d19b
SC
59 while (IS_PUSH_FP (w))
60 {
61 start_pc += 2;
62 w = read_memory_short (start_pc);
63 }
0a8f9d31 64
1f46923f 65 /* Skip past a move to FP */
ec25d19b
SC
66 if (IS_MOVE_FP (w))
67 {
68 start_pc += 2;
69 w = read_memory_short (start_pc);
1f46923f
SC
70 }
71
ec25d19b 72 /* Skip the stack adjust */
0a8f9d31 73
ec25d19b
SC
74 if (IS_MOVK_R5 (w))
75 {
76 start_pc += 2;
77 w = read_memory_short (start_pc);
78 }
79 if (IS_SUB_R5SP (w))
80 {
81 start_pc += 2;
82 w = read_memory_short (start_pc);
83 }
84 while (IS_SUB2_SP (w))
85 {
86 start_pc += 2;
87 w = read_memory_short (start_pc);
88 }
89
90 return start_pc;
91
92}
1f46923f 93
400943fb 94int
ec25d19b
SC
95print_insn (memaddr, stream)
96 CORE_ADDR memaddr;
97 FILE *stream;
0a8f9d31 98{
400943fb 99 /* Nothing is bigger than 8 bytes */
ec25d19b
SC
100 char data[8];
101
102 read_memory (memaddr, data, sizeof (data));
103 return print_insn_h8300 (memaddr, data, stream);
0a8f9d31 104}
ec25d19b 105
1f46923f
SC
106/* Given a GDB frame, determine the address of the calling function's frame.
107 This will be used to create a new GDB frame struct, and then
108 INIT_EXTRA_FRAME_INFO and INIT_FRAME_PC will be called for the new frame.
109
110 For us, the frame address is its stack pointer value, so we look up
111 the function prologue to determine the caller's sp value, and return it. */
112
113FRAME_ADDR
114FRAME_CHAIN (thisframe)
115 FRAME thisframe;
116{
117
118 frame_find_saved_regs (thisframe, (struct frame_saved_regs *) 0);
ec25d19b 119 return thisframe->fsr->regs[SP_REGNUM];
1f46923f
SC
120}
121
1f46923f
SC
122/* Put here the code to store, into a struct frame_saved_regs,
123 the addresses of the saved registers of frame described by FRAME_INFO.
124 This includes special registers such as pc and fp saved in special
125 ways in the stack frame. sp is even more special:
126 the address we return for it IS the sp for the next frame.
127
128 We cache the result of doing this in the frame_cache_obstack, since
129 it is fairly expensive. */
130
131void
132frame_find_saved_regs (fi, fsr)
133 struct frame_info *fi;
134 struct frame_saved_regs *fsr;
135{
136 register CORE_ADDR next_addr;
137 register CORE_ADDR *saved_regs;
138 register int regnum;
139 register struct frame_saved_regs *cache_fsr;
140 extern struct obstack frame_cache_obstack;
141 CORE_ADDR ip;
142 struct symtab_and_line sal;
143 CORE_ADDR limit;
144
145 if (!fi->fsr)
146 {
147 cache_fsr = (struct frame_saved_regs *)
ec25d19b
SC
148 obstack_alloc (&frame_cache_obstack,
149 sizeof (struct frame_saved_regs));
1f46923f 150 bzero (cache_fsr, sizeof (struct frame_saved_regs));
ec25d19b 151
1f46923f
SC
152 fi->fsr = cache_fsr;
153
154 /* Find the start and end of the function prologue. If the PC
155 is in the function prologue, we only consider the part that
156 has executed already. */
ec25d19b 157
1f46923f
SC
158 ip = get_pc_function_start (fi->pc);
159 sal = find_pc_line (ip, 0);
ec25d19b 160 limit = (sal.end && sal.end < fi->pc) ? sal.end : fi->pc;
1f46923f
SC
161
162 /* This will fill in fields in *fi as well as in cache_fsr. */
163 examine_prologue (ip, limit, fi->frame, cache_fsr, fi);
164 }
165
166 if (fsr)
167 *fsr = *fi->fsr;
168}
1f46923f
SC
169
170/* Fetch the instruction at ADDR, returning 0 if ADDR is beyond LIM or
171 is not the address of a valid instruction, the address of the next
172 instruction beyond ADDR otherwise. *PWORD1 receives the first word
173 of the instruction.*/
174
1f46923f 175CORE_ADDR
ec25d19b
SC
176NEXT_PROLOGUE_INSN (addr, lim, pword1)
177 CORE_ADDR addr;
178 CORE_ADDR lim;
179 short *pword1;
1f46923f 180{
ec25d19b
SC
181 if (addr < lim + 8)
182 {
183 read_memory (addr, pword1, sizeof (*pword1));
184 SWAP_TARGET_AND_HOST (pword1, sizeof (short));
1f46923f 185
ec25d19b
SC
186 return addr + 2;
187 }
1f46923f 188 return 0;
1f46923f
SC
189}
190
191/* Examine the prologue of a function. `ip' points to the first instruction.
ec25d19b 192 `limit' is the limit of the prologue (e.g. the addr of the first
1f46923f 193 linenumber, or perhaps the program counter if we're stepping through).
ec25d19b 194 `frame_sp' is the stack pointer value in use in this frame.
1f46923f 195 `fsr' is a pointer to a frame_saved_regs structure into which we put
ec25d19b 196 info about the registers saved by this frame.
1f46923f
SC
197 `fi' is a struct frame_info pointer; we fill in various fields in it
198 to reflect the offsets of the arg pointer and the locals pointer. */
199
1f46923f
SC
200static CORE_ADDR
201examine_prologue (ip, limit, after_prolog_fp, fsr, fi)
202 register CORE_ADDR ip;
203 register CORE_ADDR limit;
204 FRAME_ADDR after_prolog_fp;
205 struct frame_saved_regs *fsr;
206 struct frame_info *fi;
207{
208 register CORE_ADDR next_ip;
209 int r;
210 int i;
211 int have_fp = 0;
ec25d19b 212
1f46923f
SC
213 register int src;
214 register struct pic_prologue_code *pcode;
215 INSN_WORD insn_word;
216 int size, offset;
ec25d19b 217 unsigned int reg_save_depth = 2; /* Number of things pushed onto
1f46923f
SC
218 stack, starts at 2, 'cause the
219 PC is already there */
220
221 unsigned int auto_depth = 0; /* Number of bytes of autos */
1f46923f 222
ec25d19b 223 char in_frame[NUM_REGS]; /* One for each reg */
1f46923f 224
ec25d19b
SC
225 memset (in_frame, 1, NUM_REGS);
226 for (r = 0; r < NUM_REGS; r++)
227 {
228 fsr->regs[r] = 0;
229 }
230 if (after_prolog_fp == 0)
231 {
232 after_prolog_fp = read_register (SP_REGNUM);
233 }
234 if (ip == 0 || ip & ~0xffff)
235 return 0;
1f46923f 236
ec25d19b 237 next_ip = NEXT_PROLOGUE_INSN (ip, limit, &insn_word);
1f46923f 238
ec25d19b
SC
239 /* Skip over any fp push instructions */
240 fsr->regs[6] = after_prolog_fp;
241 while (next_ip && IS_PUSH_FP (insn_word))
242 {
243 ip = next_ip;
1f46923f 244
ec25d19b
SC
245 in_frame[insn_word & 0x7] = reg_save_depth;
246 next_ip = NEXT_PROLOGUE_INSN (ip, limit, &insn_word);
247 reg_save_depth += 2;
248 }
1f46923f
SC
249
250 /* Is this a move into the fp */
ec25d19b
SC
251 if (next_ip && IS_MOV_SP_FP (insn_word))
252 {
253 ip = next_ip;
254 next_ip = NEXT_PROLOGUE_INSN (ip, limit, &insn_word);
255 have_fp = 1;
256 }
1f46923f
SC
257
258 /* Skip over any stack adjustment, happens either with a number of
259 sub#2,sp or a mov #x,r5 sub r5,sp */
260
ec25d19b 261 if (next_ip && IS_SUB2_SP (insn_word))
1f46923f 262 {
ec25d19b
SC
263 while (next_ip && IS_SUB2_SP (insn_word))
264 {
265 auto_depth += 2;
266 ip = next_ip;
267 next_ip = NEXT_PROLOGUE_INSN (ip, limit, &insn_word);
268 }
1f46923f 269 }
ec25d19b
SC
270 else
271 {
272 if (next_ip && IS_MOVK_R5 (insn_word))
273 {
274 ip = next_ip;
275 next_ip = NEXT_PROLOGUE_INSN (ip, limit, &insn_word);
276 auto_depth += insn_word;
277
278 next_ip = NEXT_PROLOGUE_INSN (next_ip, limit, &insn_word);
279 auto_depth += insn_word;
280
281 }
282 }
283 /* Work out which regs are stored where */
284 while (next_ip && IS_PUSH (insn_word))
1f46923f
SC
285 {
286 ip = next_ip;
ec25d19b
SC
287 next_ip = NEXT_PROLOGUE_INSN (ip, limit, &insn_word);
288 fsr->regs[r] = after_prolog_fp + auto_depth;
289 auto_depth += 2;
1f46923f 290 }
1f46923f 291
1f46923f 292 /* The args are always reffed based from the stack pointer */
ec25d19b 293 fi->args_pointer = after_prolog_fp;
1f46923f 294 /* Locals are always reffed based from the fp */
ec25d19b 295 fi->locals_pointer = after_prolog_fp;
1f46923f 296 /* The PC is at a known place */
ec25d19b 297 fi->from_pc = read_memory_short (after_prolog_fp + 2);
1f46923f
SC
298
299 /* Rememeber any others too */
1f46923f 300 in_frame[PC_REGNUM] = 0;
ec25d19b
SC
301
302 if (have_fp)
303 /* We keep the old FP in the SP spot */
304 fsr->regs[SP_REGNUM] = (read_memory_short (fsr->regs[6]));
305 else
306 fsr->regs[SP_REGNUM] = after_prolog_fp + auto_depth;
307
1f46923f
SC
308 return (ip);
309}
310
311void
312init_extra_frame_info (fromleaf, fi)
313 int fromleaf;
314 struct frame_info *fi;
315{
316 fi->fsr = 0; /* Not yet allocated */
317 fi->args_pointer = 0; /* Unknown */
318 fi->locals_pointer = 0; /* Unknown */
319 fi->from_pc = 0;
ec25d19b 320
1f46923f 321}
ec25d19b 322
1f46923f
SC
323/* Return the saved PC from this frame.
324
325 If the frame has a memory copy of SRP_REGNUM, use that. If not,
326 just use the register SRP_REGNUM itself. */
327
328CORE_ADDR
329frame_saved_pc (frame)
ec25d19b 330 FRAME frame;
1f46923f
SC
331
332{
333 return frame->from_pc;
334}
335
1f46923f
SC
336CORE_ADDR
337frame_locals_address (fi)
338 struct frame_info *fi;
339{
ec25d19b
SC
340 if (!fi->locals_pointer)
341 {
342 struct frame_saved_regs ignore;
343
344 get_frame_saved_regs (fi, &ignore);
1f46923f 345
ec25d19b 346 }
1f46923f
SC
347 return fi->locals_pointer;
348}
349
350/* Return the address of the argument block for the frame
351 described by FI. Returns 0 if the address is unknown. */
352
353CORE_ADDR
354frame_args_address (fi)
355 struct frame_info *fi;
356{
ec25d19b
SC
357 if (!fi->args_pointer)
358 {
359 struct frame_saved_regs ignore;
360
361 get_frame_saved_regs (fi, &ignore);
362
363 }
1f46923f 364
1f46923f
SC
365 return fi->args_pointer;
366}
367
ec25d19b
SC
368void
369h8300_pop_frame ()
1f46923f
SC
370{
371 unsigned regnum;
372 struct frame_saved_regs fsr;
373 struct frame_info *fi;
374
ec25d19b 375 FRAME frame = get_current_frame ();
1f46923f 376
ec25d19b
SC
377 fi = get_frame_info (frame);
378 get_frame_saved_regs (fi, &fsr);
379
380 for (regnum = 0; regnum < NUM_REGS; regnum++)
1f46923f 381 {
ec25d19b
SC
382 if (fsr.regs[regnum])
383 {
384 write_register (regnum, read_memory_short (fsr.regs[regnum]));
385 }
386
387 flush_cached_frames ();
388 set_current_frame (create_new_frame (read_register (FP_REGNUM),
389 read_pc ()));
390
1f46923f 391 }
1f46923f
SC
392
393}
ec25d19b
SC
394
395void
396print_register_hook (regno)
397{
398 if (regno == 8)
399 {
400 /* CCR register */
401
402 int C, Z, N, V;
403 unsigned char b[2];
404 unsigned char l;
405
406 read_relative_register_raw_bytes (regno, b);
407 l = b[1];
408 printf ("\t");
409 printf ("I-%d - ", (l & 0x80) != 0);
410 printf ("H-%d - ", (l & 0x20) != 0);
411 N = (l & 0x8) != 0;
412 Z = (l & 0x4) != 0;
413 V = (l & 0x2) != 0;
414 C = (l & 0x1) != 0;
415 printf ("N-%d ", N);
416 printf ("Z-%d ", Z);
417 printf ("V-%d ", V);
418 printf ("C-%d ", C);
419 if ((C | Z) == 0)
420 printf ("u> ");
421 if ((C | Z) == 1)
422 printf ("u<= ");
423 if ((C == 0))
424 printf ("u>= ");
425 if (C == 1)
426 printf ("u< ");
427 if (Z == 0)
428 printf ("!= ");
429 if (Z == 1)
430 printf ("== ");
431 if ((N ^ V) == 0)
432 printf (">= ");
433 if ((N ^ V) == 1)
434 printf ("< ");
435 if ((Z | (N ^ V)) == 0)
436 printf ("> ");
437 if ((Z | (N ^ V)) == 1)
438 printf ("<= ");
439 }
440}
This page took 0.074156 seconds and 4 git commands to generate.