]>
Commit | Line | Data |
---|---|---|
1f46923f SC |
1 | /* Target-machine dependent code for Hitachi H8/300, for GDB. |
2 | Copyright (C) 1988, 1990, 1991 Free Software Foundation, Inc. | |
3 | ||
4 | This file is part of GDB. | |
5 | ||
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. | |
10 | ||
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. | |
15 | ||
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. */ | |
19 | ||
20 | /* | |
21 | Contributed by Steve Chamberlain | |
22 | [email protected] | |
23 | */ | |
24 | ||
400943fb SC |
25 | #include <stdio.h> |
26 | #include "defs.h" | |
1f46923f SC |
27 | #include "frame.h" |
28 | #include "obstack.h" | |
29 | #include "symtab.h" | |
30 | #define UNSIGNED_SHORT(X) ((X) & 0xffff) | |
400943fb SC |
31 | |
32 | /* an easy to debug H8 stack frame looks like: | |
33 | 0x6df2 push r2 | |
34 | 0x6df3 push r3 | |
35 | 0x6df6 push r6 | |
36 | 0x mov.w r7,r6 | |
37 | subs stuff,sp mov.w #x,r5 | |
38 | subs r5,sp | |
39 | ||
40 | */ | |
1f46923f | 41 | |
400943fb | 42 | #define IS_PUSH(x) ((x & 0xff00)==0x6d00) |
1f46923f SC |
43 | #define IS_MOVE_FP(x) (x == 0x0d76) |
44 | #define IS_MOV_SP_FP(x) (x == 0x0d76) | |
45 | #define IS_SUB2_SP(x) (x==0x1b87) | |
46 | #define IS_MOVK_R5(x) (x==0x7905) | |
47 | CORE_ADDR examine_prologue(); | |
48 | ||
49 | void frame_find_saved_regs (); | |
400943fb SC |
50 | CORE_ADDR h8300_skip_prologue(start_pc) |
51 | CORE_ADDR start_pc; | |
0a8f9d31 | 52 | |
0a8f9d31 | 53 | { |
1f46923f | 54 | |
400943fb SC |
55 | /* Skip past all push insns */ |
56 | short int w; | |
0a8f9d31 | 57 | |
400943fb SC |
58 | w = read_memory_integer(start_pc, 2); |
59 | while (IS_PUSH(w)) | |
60 | { | |
61 | start_pc+=2; | |
62 | w = read_memory_integer(start_pc, 2); | |
63 | } | |
0a8f9d31 | 64 | |
1f46923f SC |
65 | /* Skip past a move to FP */ |
66 | if (IS_MOVE_FP(w)) { | |
67 | start_pc +=2 ; | |
68 | w = read_memory_integer(start_pc, 2); | |
69 | } | |
70 | ||
71 | return start_pc; | |
0a8f9d31 SC |
72 | |
73 | } | |
74 | ||
1f46923f | 75 | |
400943fb SC |
76 | int |
77 | print_insn(memaddr, stream) | |
78 | CORE_ADDR memaddr; | |
79 | FILE *stream; | |
0a8f9d31 | 80 | { |
400943fb SC |
81 | /* Nothing is bigger than 8 bytes */ |
82 | char data[8]; | |
83 | read_memory (memaddr, data, sizeof(data)); | |
1f46923f | 84 | return print_insn_h8300(memaddr, data, stream); |
0a8f9d31 | 85 | } |
400943fb SC |
86 | |
87 | ||
1f46923f SC |
88 | /* Given a GDB frame, determine the address of the calling function's frame. |
89 | This will be used to create a new GDB frame struct, and then | |
90 | INIT_EXTRA_FRAME_INFO and INIT_FRAME_PC will be called for the new frame. | |
91 | ||
92 | For us, the frame address is its stack pointer value, so we look up | |
93 | the function prologue to determine the caller's sp value, and return it. */ | |
94 | ||
95 | FRAME_ADDR | |
96 | FRAME_CHAIN (thisframe) | |
97 | FRAME thisframe; | |
98 | { | |
99 | ||
100 | frame_find_saved_regs (thisframe, (struct frame_saved_regs *) 0); | |
101 | return thisframe->fsr->regs[SP_REGNUM]; | |
102 | } | |
103 | ||
400943fb | 104 | |
1f46923f SC |
105 | |
106 | /* Put here the code to store, into a struct frame_saved_regs, | |
107 | the addresses of the saved registers of frame described by FRAME_INFO. | |
108 | This includes special registers such as pc and fp saved in special | |
109 | ways in the stack frame. sp is even more special: | |
110 | the address we return for it IS the sp for the next frame. | |
111 | ||
112 | We cache the result of doing this in the frame_cache_obstack, since | |
113 | it is fairly expensive. */ | |
114 | ||
115 | void | |
116 | frame_find_saved_regs (fi, fsr) | |
117 | struct frame_info *fi; | |
118 | struct frame_saved_regs *fsr; | |
119 | { | |
120 | register CORE_ADDR next_addr; | |
121 | register CORE_ADDR *saved_regs; | |
122 | register int regnum; | |
123 | register struct frame_saved_regs *cache_fsr; | |
124 | extern struct obstack frame_cache_obstack; | |
125 | CORE_ADDR ip; | |
126 | struct symtab_and_line sal; | |
127 | CORE_ADDR limit; | |
128 | ||
129 | if (!fi->fsr) | |
130 | { | |
131 | cache_fsr = (struct frame_saved_regs *) | |
132 | obstack_alloc (&frame_cache_obstack, | |
133 | sizeof (struct frame_saved_regs)); | |
134 | bzero (cache_fsr, sizeof (struct frame_saved_regs)); | |
135 | fi->fsr = cache_fsr; | |
136 | ||
137 | /* Find the start and end of the function prologue. If the PC | |
138 | is in the function prologue, we only consider the part that | |
139 | has executed already. */ | |
140 | ||
141 | ip = get_pc_function_start (fi->pc); | |
142 | sal = find_pc_line (ip, 0); | |
143 | limit = (sal.end && sal.end < fi->pc) ? sal.end: fi->pc; | |
144 | ||
145 | /* This will fill in fields in *fi as well as in cache_fsr. */ | |
146 | examine_prologue (ip, limit, fi->frame, cache_fsr, fi); | |
147 | } | |
148 | ||
149 | if (fsr) | |
150 | *fsr = *fi->fsr; | |
151 | } | |
400943fb | 152 | |
1f46923f SC |
153 | |
154 | /* Fetch the instruction at ADDR, returning 0 if ADDR is beyond LIM or | |
155 | is not the address of a valid instruction, the address of the next | |
156 | instruction beyond ADDR otherwise. *PWORD1 receives the first word | |
157 | of the instruction.*/ | |
158 | ||
159 | ||
160 | CORE_ADDR | |
161 | NEXT_PROLOGUE_INSN(addr, lim, pword1) | |
162 | CORE_ADDR addr; | |
163 | CORE_ADDR lim; | |
164 | short *pword1; | |
165 | { | |
166 | if (addr < lim+8) | |
167 | { | |
168 | read_memory (addr, pword1, sizeof(*pword1)); | |
169 | SWAP_TARGET_AND_HOST (pword1, sizeof (short)); | |
170 | return addr + 2; | |
171 | } | |
172 | ||
173 | return 0; | |
174 | ||
175 | } | |
176 | ||
177 | /* Examine the prologue of a function. `ip' points to the first instruction. | |
178 | `limit' is the limit of the prologue (e.g. the addr of the first | |
179 | linenumber, or perhaps the program counter if we're stepping through). | |
180 | `frame_sp' is the stack pointer value in use in this frame. | |
181 | `fsr' is a pointer to a frame_saved_regs structure into which we put | |
182 | info about the registers saved by this frame. | |
183 | `fi' is a struct frame_info pointer; we fill in various fields in it | |
184 | to reflect the offsets of the arg pointer and the locals pointer. */ | |
185 | ||
186 | /* We will find two sorts of prologue, framefull and non framefull: | |
187 | ||
188 | push r2 | |
189 | push r3 | |
190 | push fp | |
191 | mov sp,fp | |
192 | stack_ad | |
193 | ||
194 | and | |
195 | push x | |
196 | push y | |
197 | stack_ad | |
198 | ||
199 | */ | |
200 | ||
201 | static CORE_ADDR | |
202 | examine_prologue (ip, limit, after_prolog_fp, fsr, fi) | |
203 | register CORE_ADDR ip; | |
204 | register CORE_ADDR limit; | |
205 | FRAME_ADDR after_prolog_fp; | |
206 | struct frame_saved_regs *fsr; | |
207 | struct frame_info *fi; | |
208 | { | |
209 | register CORE_ADDR next_ip; | |
210 | int r; | |
211 | int i; | |
212 | int have_fp = 0; | |
213 | ||
214 | register int src; | |
215 | register struct pic_prologue_code *pcode; | |
216 | INSN_WORD insn_word; | |
217 | int size, offset; | |
218 | unsigned int reg_save_depth = 2; /* Number of things pushed onto | |
219 | stack, starts at 2, 'cause the | |
220 | PC is already there */ | |
221 | ||
222 | unsigned int auto_depth = 0; /* Number of bytes of autos */ | |
223 | ||
224 | char in_frame[NUM_REGS]; /* One for each reg */ | |
225 | ||
226 | memset(in_frame, 1, NUM_REGS); | |
227 | ||
228 | if (after_prolog_fp == 0) { | |
229 | after_prolog_fp = read_register(SP_REGNUM); | |
230 | } | |
231 | if (ip == 0 || ip & ~0xffff) return 0; | |
232 | ||
233 | next_ip = NEXT_PROLOGUE_INSN (ip, limit, &insn_word); | |
234 | ||
235 | /* Skip over any push instructions, and remember where they were saved */ | |
236 | ||
237 | ||
238 | while (next_ip && IS_PUSH(insn_word)) | |
239 | { | |
240 | ip = next_ip; | |
241 | in_frame[insn_word & 0x7] = reg_save_depth; | |
242 | next_ip = NEXT_PROLOGUE_INSN(ip, limit, &insn_word); | |
243 | reg_save_depth +=2; | |
244 | ||
245 | } | |
246 | ||
247 | ||
248 | /* Is this a move into the fp */ | |
249 | if (next_ip && IS_MOV_SP_FP(insn_word)) | |
250 | { | |
251 | ip = next_ip; | |
252 | next_ip = NEXT_PROLOGUE_INSN(ip, limit, &insn_word); | |
253 | have_fp = 1; | |
254 | ||
255 | } | |
256 | ||
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 | ||
261 | ||
262 | if (next_ip && IS_SUB2_SP(insn_word)) | |
263 | { | |
264 | while (next_ip && IS_SUB2_SP(insn_word)) | |
265 | { | |
266 | auto_depth +=2 ; | |
267 | ip = next_ip; | |
268 | next_ip = NEXT_PROLOGUE_INSN(ip, limit, &insn_word); | |
269 | } | |
270 | } | |
271 | else | |
272 | { | |
273 | if (next_ip && IS_MOVK_R5(insn_word)) | |
274 | { | |
275 | ip = next_ip; | |
276 | next_ip = NEXT_PROLOGUE_INSN(ip, limit, &insn_word); | |
277 | auto_depth += insn_word; | |
278 | ip +=4; | |
279 | ||
280 | } | |
281 | } | |
282 | ||
283 | ||
284 | ||
285 | /* The args are always reffed based from the stack pointer */ | |
286 | fi->args_pointer = after_prolog_fp - auto_depth; | |
287 | /* Locals are always reffed based from the fp */ | |
288 | fi->locals_pointer = after_prolog_fp ; | |
289 | /* The PC is at a known place */ | |
290 | fi->from_pc = read_memory_integer(after_prolog_fp + reg_save_depth-2 , 2); | |
291 | ||
292 | ||
293 | /* Rememeber any others too */ | |
294 | ||
295 | in_frame[PC_REGNUM] = 0; | |
296 | ||
297 | for (r = 0; r < NUM_REGS; r++) | |
298 | { | |
299 | if (in_frame[r] != 1) | |
300 | { | |
301 | fsr->regs[r] = after_prolog_fp + reg_save_depth - in_frame[r] -2; | |
302 | } | |
303 | else | |
304 | { | |
305 | fsr->regs[r] = 0; | |
306 | } | |
307 | } | |
308 | if (have_fp) | |
309 | /* We keep the old FP in the SP spot */ | |
310 | fsr->regs[SP_REGNUM] = read_memory_integer(fsr->regs[6],2); | |
311 | else | |
312 | fsr->regs[SP_REGNUM] = after_prolog_fp + reg_save_depth; | |
313 | ||
314 | return (ip); | |
315 | } | |
316 | ||
317 | void | |
318 | init_extra_frame_info (fromleaf, fi) | |
319 | int fromleaf; | |
320 | struct frame_info *fi; | |
321 | { | |
322 | fi->fsr = 0; /* Not yet allocated */ | |
323 | fi->args_pointer = 0; /* Unknown */ | |
324 | fi->locals_pointer = 0; /* Unknown */ | |
325 | fi->from_pc = 0; | |
326 | ||
327 | } | |
328 | /* Return the saved PC from this frame. | |
329 | ||
330 | If the frame has a memory copy of SRP_REGNUM, use that. If not, | |
331 | just use the register SRP_REGNUM itself. */ | |
332 | ||
333 | CORE_ADDR | |
334 | frame_saved_pc (frame) | |
335 | FRAME frame; | |
336 | ||
337 | { | |
338 | return frame->from_pc; | |
339 | } | |
340 | ||
341 | ||
342 | CORE_ADDR | |
343 | frame_locals_address (fi) | |
344 | struct frame_info *fi; | |
345 | { | |
346 | if (!fi->locals_pointer) | |
347 | { | |
348 | struct frame_saved_regs ignore; | |
349 | get_frame_saved_regs(fi, &ignore); | |
350 | ||
351 | } | |
352 | return fi->locals_pointer; | |
353 | } | |
354 | ||
355 | /* Return the address of the argument block for the frame | |
356 | described by FI. Returns 0 if the address is unknown. */ | |
357 | ||
358 | CORE_ADDR | |
359 | frame_args_address (fi) | |
360 | struct frame_info *fi; | |
361 | { | |
362 | if (!fi->args_pointer) | |
363 | { | |
364 | struct frame_saved_regs ignore; | |
365 | get_frame_saved_regs(fi, &ignore); | |
366 | ||
367 | } | |
368 | ||
369 | return fi->args_pointer; | |
370 | } | |
371 | ||
372 | ||
373 | void h8300_pop_frame() | |
374 | { | |
375 | unsigned regnum; | |
376 | struct frame_saved_regs fsr; | |
377 | struct frame_info *fi; | |
378 | ||
379 | FRAME frame = get_current_frame(); | |
380 | fi = get_frame_info(frame); | |
381 | get_frame_saved_regs(fi, &fsr); | |
382 | ||
383 | for (regnum = 0; regnum < NUM_REGS; regnum ++) | |
384 | { | |
385 | if(fsr.regs[regnum]) | |
386 | { | |
387 | write_register(regnum, read_memory_integer (fsr.regs[regnum], 2)); | |
388 | } | |
389 | ||
390 | flush_cached_frames(); | |
391 | set_current_frame(create_new_frame(read_register(FP_REGNUM), | |
392 | read_pc())); | |
393 | ||
394 | } | |
395 | ||
396 | } |