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
29 #define UNSIGNED_SHORT(X) ((X) & 0xffff)
31 /* an easy to debug H8 stack frame looks like:
36 subs stuff,sp mov.w #x,r5
41 #define IS_PUSH(x) ((x & 0xff00)==0x6d00)
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)
46 CORE_ADDR
examine_prologue();
48 void frame_find_saved_regs ();
49 CORE_ADDR
h8300_skip_prologue(start_pc
)
54 /* Skip past all push insns */
57 w
= read_memory_short(start_pc
);
61 w
= read_memory_short(start_pc
);
64 /* Skip past a move to FP */
67 w
= read_memory_short(start_pc
);
76 print_insn(memaddr
, stream
)
80 /* Nothing is bigger than 8 bytes */
82 read_memory (memaddr
, data
, sizeof(data
));
83 return print_insn_h8300(memaddr
, data
, stream
);
87 /* Given a GDB frame, determine the address of the calling function's frame.
88 This will be used to create a new GDB frame struct, and then
89 INIT_EXTRA_FRAME_INFO and INIT_FRAME_PC will be called for the new frame.
91 For us, the frame address is its stack pointer value, so we look up
92 the function prologue to determine the caller's sp value, and return it. */
95 FRAME_CHAIN (thisframe
)
99 frame_find_saved_regs (thisframe
, (struct frame_saved_regs
*) 0);
100 return thisframe
->fsr
->regs
[SP_REGNUM
];
105 /* Put here the code to store, into a struct frame_saved_regs,
106 the addresses of the saved registers of frame described by FRAME_INFO.
107 This includes special registers such as pc and fp saved in special
108 ways in the stack frame. sp is even more special:
109 the address we return for it IS the sp for the next frame.
111 We cache the result of doing this in the frame_cache_obstack, since
112 it is fairly expensive. */
115 frame_find_saved_regs (fi
, fsr
)
116 struct frame_info
*fi
;
117 struct frame_saved_regs
*fsr
;
119 register CORE_ADDR next_addr
;
120 register CORE_ADDR
*saved_regs
;
122 register struct frame_saved_regs
*cache_fsr
;
123 extern struct obstack frame_cache_obstack
;
125 struct symtab_and_line sal
;
130 cache_fsr
= (struct frame_saved_regs
*)
131 obstack_alloc (&frame_cache_obstack
,
132 sizeof (struct frame_saved_regs
));
133 bzero (cache_fsr
, sizeof (struct frame_saved_regs
));
136 /* Find the start and end of the function prologue. If the PC
137 is in the function prologue, we only consider the part that
138 has executed already. */
140 ip
= get_pc_function_start (fi
->pc
);
141 sal
= find_pc_line (ip
, 0);
142 limit
= (sal
.end
&& sal
.end
< fi
->pc
) ? sal
.end
: fi
->pc
;
144 /* This will fill in fields in *fi as well as in cache_fsr. */
145 examine_prologue (ip
, limit
, fi
->frame
, cache_fsr
, fi
);
153 /* Fetch the instruction at ADDR, returning 0 if ADDR is beyond LIM or
154 is not the address of a valid instruction, the address of the next
155 instruction beyond ADDR otherwise. *PWORD1 receives the first word
156 of the instruction.*/
160 NEXT_PROLOGUE_INSN(addr
, lim
, pword1
)
167 read_memory (addr
, pword1
, sizeof(*pword1
));
168 SWAP_TARGET_AND_HOST (pword1
, sizeof (short));
176 /* Examine the prologue of a function. `ip' points to the first instruction.
177 `limit' is the limit of the prologue (e.g. the addr of the first
178 linenumber, or perhaps the program counter if we're stepping through).
179 `frame_sp' is the stack pointer value in use in this frame.
180 `fsr' is a pointer to a frame_saved_regs structure into which we put
181 info about the registers saved by this frame.
182 `fi' is a struct frame_info pointer; we fill in various fields in it
183 to reflect the offsets of the arg pointer and the locals pointer. */
185 /* We will find two sorts of prologue, framefull and non framefull:
201 examine_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
;
208 register CORE_ADDR next_ip
;
214 register struct pic_prologue_code
*pcode
;
217 unsigned int reg_save_depth
= 2; /* Number of things pushed onto
218 stack, starts at 2, 'cause the
219 PC is already there */
221 unsigned int auto_depth
= 0; /* Number of bytes of autos */
223 char in_frame
[NUM_REGS
]; /* One for each reg */
225 memset(in_frame
, 1, NUM_REGS
);
227 if (after_prolog_fp
== 0) {
228 after_prolog_fp
= read_register(SP_REGNUM
);
230 if (ip
== 0 || ip
& ~0xffff) return 0;
232 next_ip
= NEXT_PROLOGUE_INSN (ip
, limit
, &insn_word
);
234 /* Skip over any push instructions, and remember where they were saved */
237 while (next_ip
&& IS_PUSH(insn_word
))
240 in_frame
[insn_word
& 0x7] = reg_save_depth
;
241 next_ip
= NEXT_PROLOGUE_INSN(ip
, limit
, &insn_word
);
247 /* Is this a move into the fp */
248 if (next_ip
&& IS_MOV_SP_FP(insn_word
))
251 next_ip
= NEXT_PROLOGUE_INSN(ip
, limit
, &insn_word
);
257 /* Skip over any stack adjustment, happens either with a number of
258 sub#2,sp or a mov #x,r5 sub r5,sp */
261 if (next_ip
&& IS_SUB2_SP(insn_word
))
263 while (next_ip
&& IS_SUB2_SP(insn_word
))
267 next_ip
= NEXT_PROLOGUE_INSN(ip
, limit
, &insn_word
);
272 if (next_ip
&& IS_MOVK_R5(insn_word
))
275 next_ip
= NEXT_PROLOGUE_INSN(ip
, limit
, &insn_word
);
276 auto_depth
+= insn_word
;
284 /* The args are always reffed based from the stack pointer */
285 fi
->args_pointer
= after_prolog_fp
- auto_depth
;
286 /* Locals are always reffed based from the fp */
287 fi
->locals_pointer
= after_prolog_fp
;
288 /* The PC is at a known place */
289 fi
->from_pc
= read_memory_short(after_prolog_fp
+ reg_save_depth
-2 );
292 /* Rememeber any others too */
294 in_frame
[PC_REGNUM
] = 0;
296 for (r
= 0; r
< NUM_REGS
; r
++)
298 if (in_frame
[r
] != 1)
300 fsr
->regs
[r
] = after_prolog_fp
+ reg_save_depth
- in_frame
[r
] -2;
308 /* We keep the old FP in the SP spot */
309 fsr
->regs
[SP_REGNUM
] = (read_memory_short(fsr
->regs
[6])) ;
311 fsr
->regs
[SP_REGNUM
] = after_prolog_fp
+ reg_save_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
)
337 return frame
->from_pc
;
342 frame_locals_address (fi
)
343 struct frame_info
*fi
;
345 if (!fi
->locals_pointer
)
347 struct frame_saved_regs ignore
;
348 get_frame_saved_regs(fi
, &ignore
);
351 return fi
->locals_pointer
;
354 /* Return the address of the argument block for the frame
355 described by FI. Returns 0 if the address is unknown. */
358 frame_args_address (fi
)
359 struct frame_info
*fi
;
361 if (!fi
->args_pointer
)
363 struct frame_saved_regs ignore
;
364 get_frame_saved_regs(fi
, &ignore
);
368 return fi
->args_pointer
;
372 void h8300_pop_frame()
375 struct frame_saved_regs fsr
;
376 struct frame_info
*fi
;
378 FRAME frame
= get_current_frame();
379 fi
= get_frame_info(frame
);
380 get_frame_saved_regs(fi
, &fsr
);
382 for (regnum
= 0; regnum
< NUM_REGS
; regnum
++)
386 write_register(regnum
, read_memory_short (fsr
.regs
[regnum
]));
389 flush_cached_frames();
390 set_current_frame(create_new_frame(read_register(FP_REGNUM
),