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