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