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 | ||
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" | |
df14b38b | 29 | #include <dis-asm.h> |
256b4f37 SC |
30 | #undef NUM_REGS |
31 | #define NUM_REGS 11 | |
32 | ||
1f46923f | 33 | #define UNSIGNED_SHORT(X) ((X) & 0xffff) |
400943fb SC |
34 | |
35 | /* an easy to debug H8 stack frame looks like: | |
ec25d19b SC |
36 | 0x6df6 push r6 |
37 | 0x0d76 mov.w r7,r6 | |
38 | 0x6dfn push reg | |
39 | 0x7905 nnnn mov.w #n,r5 or 0x1b87 subs #2,sp | |
40 | 0x1957 sub.w r5,sp | |
400943fb SC |
41 | |
42 | */ | |
1f46923f | 43 | |
400943fb | 44 | #define IS_PUSH(x) ((x & 0xff00)==0x6d00) |
ec25d19b | 45 | #define IS_PUSH_FP(x) (x == 0x6df6) |
1f46923f SC |
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) | |
ec25d19b | 50 | #define IS_SUB_R5SP(x) (x==0x1957) |
1ca9e7c9 DE |
51 | |
52 | static CORE_ADDR examine_prologue (); | |
1f46923f | 53 | |
ec25d19b SC |
54 | void frame_find_saved_regs (); |
55 | CORE_ADDR | |
56 | h8300_skip_prologue (start_pc) | |
57 | CORE_ADDR start_pc; | |
0a8f9d31 | 58 | { |
ec25d19b | 59 | short int w; |
1f46923f | 60 | |
df14b38b | 61 | w = read_memory_unsigned_integer (start_pc, 2); |
400943fb | 62 | /* Skip past all push insns */ |
ec25d19b SC |
63 | while (IS_PUSH_FP (w)) |
64 | { | |
65 | start_pc += 2; | |
df14b38b | 66 | w = read_memory_unsigned_integer (start_pc, 2); |
ec25d19b | 67 | } |
0a8f9d31 | 68 | |
1f46923f | 69 | /* Skip past a move to FP */ |
ec25d19b SC |
70 | if (IS_MOVE_FP (w)) |
71 | { | |
72 | start_pc += 2; | |
df14b38b | 73 | w = read_memory_unsigned_integer (start_pc, 2); |
1f46923f SC |
74 | } |
75 | ||
ec25d19b | 76 | /* Skip the stack adjust */ |
0a8f9d31 | 77 | |
ec25d19b SC |
78 | if (IS_MOVK_R5 (w)) |
79 | { | |
80 | start_pc += 2; | |
df14b38b | 81 | w = read_memory_unsigned_integer (start_pc, 2); |
ec25d19b SC |
82 | } |
83 | if (IS_SUB_R5SP (w)) | |
84 | { | |
85 | start_pc += 2; | |
df14b38b | 86 | w = read_memory_unsigned_integer (start_pc, 2); |
ec25d19b SC |
87 | } |
88 | while (IS_SUB2_SP (w)) | |
89 | { | |
90 | start_pc += 2; | |
df14b38b | 91 | w = read_memory_unsigned_integer (start_pc, 2); |
ec25d19b SC |
92 | } |
93 | ||
94 | return start_pc; | |
ec25d19b | 95 | } |
1f46923f | 96 | |
400943fb | 97 | int |
ec25d19b SC |
98 | print_insn (memaddr, stream) |
99 | CORE_ADDR memaddr; | |
100 | FILE *stream; | |
0a8f9d31 | 101 | { |
df14b38b SC |
102 | disassemble_info info; |
103 | GDB_INIT_DISASSEMBLE_INFO(info, stream); | |
d0414a11 DE |
104 | if (HMODE) |
105 | return print_insn_h8300h (memaddr, &info); | |
106 | else | |
107 | return print_insn_h8300 (memaddr, &info); | |
0a8f9d31 | 108 | } |
ec25d19b | 109 | |
1f46923f SC |
110 | /* Given a GDB frame, determine the address of the calling function's frame. |
111 | This will be used to create a new GDB frame struct, and then | |
112 | INIT_EXTRA_FRAME_INFO and INIT_FRAME_PC will be called for the new frame. | |
113 | ||
114 | For us, the frame address is its stack pointer value, so we look up | |
115 | the function prologue to determine the caller's sp value, and return it. */ | |
116 | ||
117 | FRAME_ADDR | |
118 | FRAME_CHAIN (thisframe) | |
119 | FRAME thisframe; | |
120 | { | |
1f46923f | 121 | frame_find_saved_regs (thisframe, (struct frame_saved_regs *) 0); |
ec25d19b | 122 | return thisframe->fsr->regs[SP_REGNUM]; |
1f46923f SC |
123 | } |
124 | ||
1f46923f SC |
125 | /* Put here the code to store, into a struct frame_saved_regs, |
126 | the addresses of the saved registers of frame described by FRAME_INFO. | |
127 | This includes special registers such as pc and fp saved in special | |
128 | ways in the stack frame. sp is even more special: | |
129 | the address we return for it IS the sp for the next frame. | |
130 | ||
131 | We cache the result of doing this in the frame_cache_obstack, since | |
132 | it is fairly expensive. */ | |
133 | ||
134 | void | |
135 | frame_find_saved_regs (fi, fsr) | |
136 | struct frame_info *fi; | |
137 | struct frame_saved_regs *fsr; | |
138 | { | |
139 | register CORE_ADDR next_addr; | |
140 | register CORE_ADDR *saved_regs; | |
141 | register int regnum; | |
142 | register struct frame_saved_regs *cache_fsr; | |
143 | extern struct obstack frame_cache_obstack; | |
144 | CORE_ADDR ip; | |
145 | struct symtab_and_line sal; | |
146 | CORE_ADDR limit; | |
147 | ||
148 | if (!fi->fsr) | |
149 | { | |
150 | cache_fsr = (struct frame_saved_regs *) | |
ec25d19b SC |
151 | obstack_alloc (&frame_cache_obstack, |
152 | sizeof (struct frame_saved_regs)); | |
1f46923f | 153 | bzero (cache_fsr, sizeof (struct frame_saved_regs)); |
ec25d19b | 154 | |
1f46923f SC |
155 | fi->fsr = cache_fsr; |
156 | ||
157 | /* Find the start and end of the function prologue. If the PC | |
158 | is in the function prologue, we only consider the part that | |
159 | has executed already. */ | |
ec25d19b | 160 | |
1f46923f SC |
161 | ip = get_pc_function_start (fi->pc); |
162 | sal = find_pc_line (ip, 0); | |
ec25d19b | 163 | limit = (sal.end && sal.end < fi->pc) ? sal.end : fi->pc; |
1f46923f SC |
164 | |
165 | /* This will fill in fields in *fi as well as in cache_fsr. */ | |
166 | examine_prologue (ip, limit, fi->frame, cache_fsr, fi); | |
167 | } | |
168 | ||
169 | if (fsr) | |
170 | *fsr = *fi->fsr; | |
171 | } | |
1f46923f SC |
172 | |
173 | /* Fetch the instruction at ADDR, returning 0 if ADDR is beyond LIM or | |
174 | is not the address of a valid instruction, the address of the next | |
175 | instruction beyond ADDR otherwise. *PWORD1 receives the first word | |
176 | of the instruction.*/ | |
177 | ||
1f46923f | 178 | CORE_ADDR |
ec25d19b SC |
179 | NEXT_PROLOGUE_INSN (addr, lim, pword1) |
180 | CORE_ADDR addr; | |
181 | CORE_ADDR lim; | |
58e49e21 | 182 | INSN_WORD *pword1; |
1f46923f | 183 | { |
34df79fc | 184 | char buf[2]; |
ec25d19b SC |
185 | if (addr < lim + 8) |
186 | { | |
34df79fc JK |
187 | read_memory (addr, buf, 2); |
188 | *pword1 = extract_signed_integer (buf, 2); | |
1f46923f | 189 | |
ec25d19b SC |
190 | return addr + 2; |
191 | } | |
1f46923f | 192 | return 0; |
1f46923f SC |
193 | } |
194 | ||
195 | /* Examine the prologue of a function. `ip' points to the first instruction. | |
ec25d19b | 196 | `limit' is the limit of the prologue (e.g. the addr of the first |
1f46923f | 197 | linenumber, or perhaps the program counter if we're stepping through). |
ec25d19b | 198 | `frame_sp' is the stack pointer value in use in this frame. |
1f46923f | 199 | `fsr' is a pointer to a frame_saved_regs structure into which we put |
ec25d19b | 200 | info about the registers saved by this frame. |
1f46923f SC |
201 | `fi' is a struct frame_info pointer; we fill in various fields in it |
202 | to reflect the offsets of the arg pointer and the locals pointer. */ | |
203 | ||
1f46923f SC |
204 | static CORE_ADDR |
205 | examine_prologue (ip, limit, after_prolog_fp, fsr, fi) | |
206 | register CORE_ADDR ip; | |
207 | register CORE_ADDR limit; | |
208 | FRAME_ADDR after_prolog_fp; | |
209 | struct frame_saved_regs *fsr; | |
210 | struct frame_info *fi; | |
211 | { | |
212 | register CORE_ADDR next_ip; | |
213 | int r; | |
214 | int i; | |
215 | int have_fp = 0; | |
1f46923f SC |
216 | register int src; |
217 | register struct pic_prologue_code *pcode; | |
218 | INSN_WORD insn_word; | |
219 | int size, offset; | |
d0414a11 DE |
220 | /* Number of things pushed onto stack, starts at 2/4, 'cause the |
221 | PC is already there */ | |
222 | unsigned int reg_save_depth = HMODE ? 4 : 2; | |
1f46923f SC |
223 | |
224 | unsigned int auto_depth = 0; /* Number of bytes of autos */ | |
1f46923f | 225 | |
ddf30c37 | 226 | char in_frame[11]; /* One for each reg */ |
1f46923f | 227 | |
ddf30c37 | 228 | memset (in_frame, 1, 11); |
256b4f37 | 229 | for (r = 0; r < 8; r++) |
ec25d19b SC |
230 | { |
231 | fsr->regs[r] = 0; | |
232 | } | |
233 | if (after_prolog_fp == 0) | |
234 | { | |
235 | after_prolog_fp = read_register (SP_REGNUM); | |
236 | } | |
d0414a11 | 237 | if (ip == 0 || ip & (HMODE ? ~0xffff : ~0xffff)) |
ec25d19b | 238 | return 0; |
1f46923f | 239 | |
ec25d19b | 240 | next_ip = NEXT_PROLOGUE_INSN (ip, limit, &insn_word); |
1f46923f | 241 | |
ec25d19b SC |
242 | /* Skip over any fp push instructions */ |
243 | fsr->regs[6] = after_prolog_fp; | |
244 | while (next_ip && IS_PUSH_FP (insn_word)) | |
245 | { | |
246 | ip = next_ip; | |
1f46923f | 247 | |
ec25d19b SC |
248 | in_frame[insn_word & 0x7] = reg_save_depth; |
249 | next_ip = NEXT_PROLOGUE_INSN (ip, limit, &insn_word); | |
250 | reg_save_depth += 2; | |
251 | } | |
1f46923f SC |
252 | |
253 | /* Is this a move into the fp */ | |
ec25d19b SC |
254 | if (next_ip && IS_MOV_SP_FP (insn_word)) |
255 | { | |
256 | ip = next_ip; | |
257 | next_ip = NEXT_PROLOGUE_INSN (ip, limit, &insn_word); | |
258 | have_fp = 1; | |
259 | } | |
1f46923f SC |
260 | |
261 | /* Skip over any stack adjustment, happens either with a number of | |
262 | sub#2,sp or a mov #x,r5 sub r5,sp */ | |
263 | ||
ec25d19b | 264 | if (next_ip && IS_SUB2_SP (insn_word)) |
1f46923f | 265 | { |
ec25d19b SC |
266 | while (next_ip && IS_SUB2_SP (insn_word)) |
267 | { | |
268 | auto_depth += 2; | |
269 | ip = next_ip; | |
270 | next_ip = NEXT_PROLOGUE_INSN (ip, limit, &insn_word); | |
271 | } | |
1f46923f | 272 | } |
ec25d19b SC |
273 | else |
274 | { | |
275 | if (next_ip && IS_MOVK_R5 (insn_word)) | |
276 | { | |
277 | ip = next_ip; | |
278 | next_ip = NEXT_PROLOGUE_INSN (ip, limit, &insn_word); | |
279 | auto_depth += insn_word; | |
280 | ||
281 | next_ip = NEXT_PROLOGUE_INSN (next_ip, limit, &insn_word); | |
282 | auto_depth += insn_word; | |
ec25d19b SC |
283 | } |
284 | } | |
285 | /* Work out which regs are stored where */ | |
286 | while (next_ip && IS_PUSH (insn_word)) | |
1f46923f SC |
287 | { |
288 | ip = next_ip; | |
ec25d19b SC |
289 | next_ip = NEXT_PROLOGUE_INSN (ip, limit, &insn_word); |
290 | fsr->regs[r] = after_prolog_fp + auto_depth; | |
291 | auto_depth += 2; | |
1f46923f | 292 | } |
1f46923f | 293 | |
1f46923f | 294 | /* The args are always reffed based from the stack pointer */ |
ec25d19b | 295 | fi->args_pointer = after_prolog_fp; |
1f46923f | 296 | /* Locals are always reffed based from the fp */ |
ec25d19b | 297 | fi->locals_pointer = after_prolog_fp; |
1f46923f | 298 | /* The PC is at a known place */ |
df14b38b | 299 | fi->from_pc = read_memory_unsigned_integer (after_prolog_fp + 2, BINWORD); |
1f46923f SC |
300 | |
301 | /* Rememeber any others too */ | |
1f46923f | 302 | in_frame[PC_REGNUM] = 0; |
ec25d19b SC |
303 | |
304 | if (have_fp) | |
305 | /* We keep the old FP in the SP spot */ | |
b1d0b161 | 306 | fsr->regs[SP_REGNUM] = read_memory_unsigned_integer (fsr->regs[6], BINWORD); |
ec25d19b SC |
307 | else |
308 | fsr->regs[SP_REGNUM] = after_prolog_fp + auto_depth; | |
309 | ||
1f46923f SC |
310 | return (ip); |
311 | } | |
312 | ||
313 | void | |
314 | init_extra_frame_info (fromleaf, fi) | |
315 | int fromleaf; | |
316 | struct frame_info *fi; | |
317 | { | |
318 | fi->fsr = 0; /* Not yet allocated */ | |
319 | fi->args_pointer = 0; /* Unknown */ | |
320 | fi->locals_pointer = 0; /* Unknown */ | |
321 | fi->from_pc = 0; | |
1f46923f | 322 | } |
ec25d19b | 323 | |
1f46923f SC |
324 | /* Return the saved PC from this frame. |
325 | ||
326 | If the frame has a memory copy of SRP_REGNUM, use that. If not, | |
327 | just use the register SRP_REGNUM itself. */ | |
328 | ||
329 | CORE_ADDR | |
330 | frame_saved_pc (frame) | |
ec25d19b | 331 | FRAME frame; |
1f46923f SC |
332 | { |
333 | return frame->from_pc; | |
334 | } | |
335 | ||
1f46923f SC |
336 | CORE_ADDR |
337 | frame_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 | ||
353 | CORE_ADDR | |
354 | frame_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 |
368 | void |
369 | h8300_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 | ||
256b4f37 | 380 | for (regnum = 0; regnum < 8; regnum++) |
1f46923f | 381 | { |
ec25d19b SC |
382 | if (fsr.regs[regnum]) |
383 | { | |
df14b38b | 384 | write_register (regnum, read_memory_integer(fsr.regs[regnum]), BINWORD); |
ec25d19b SC |
385 | } |
386 | ||
387 | flush_cached_frames (); | |
388 | set_current_frame (create_new_frame (read_register (FP_REGNUM), | |
389 | read_pc ())); | |
1f46923f | 390 | } |
1f46923f | 391 | } |
ec25d19b SC |
392 | |
393 | void | |
394 | print_register_hook (regno) | |
395 | { | |
396 | if (regno == 8) | |
397 | { | |
398 | /* CCR register */ | |
399 | ||
400 | int C, Z, N, V; | |
401 | unsigned char b[2]; | |
402 | unsigned char l; | |
403 | ||
404 | read_relative_register_raw_bytes (regno, b); | |
405 | l = b[1]; | |
406 | printf ("\t"); | |
407 | printf ("I-%d - ", (l & 0x80) != 0); | |
408 | printf ("H-%d - ", (l & 0x20) != 0); | |
409 | N = (l & 0x8) != 0; | |
410 | Z = (l & 0x4) != 0; | |
411 | V = (l & 0x2) != 0; | |
412 | C = (l & 0x1) != 0; | |
413 | printf ("N-%d ", N); | |
414 | printf ("Z-%d ", Z); | |
415 | printf ("V-%d ", V); | |
416 | printf ("C-%d ", C); | |
417 | if ((C | Z) == 0) | |
418 | printf ("u> "); | |
419 | if ((C | Z) == 1) | |
420 | printf ("u<= "); | |
421 | if ((C == 0)) | |
422 | printf ("u>= "); | |
423 | if (C == 1) | |
424 | printf ("u< "); | |
425 | if (Z == 0) | |
426 | printf ("!= "); | |
427 | if (Z == 1) | |
428 | printf ("== "); | |
429 | if ((N ^ V) == 0) | |
430 | printf (">= "); | |
431 | if ((N ^ V) == 1) | |
432 | printf ("< "); | |
433 | if ((Z | (N ^ V)) == 0) | |
434 | printf ("> "); | |
435 | if ((Z | (N ^ V)) == 1) | |
436 | printf ("<= "); | |
437 | } | |
438 | } |