* h8300-dep.c (gdb_print_insn_h8300): Handle the H8/S.
[platform/upstream/binutils.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 /* start-sanitize-h8s */
125   if (h8300smode)
126     return print_insn_h8300s (memaddr, info);
127   else
128 /* end-sanitize-h8s */
129     if (h8300hmode)
130     return print_insn_h8300h (memaddr, info);
131   else
132     return print_insn_h8300 (memaddr, info);
133 }
134
135 /* Given a GDB frame, determine the address of the calling function's frame.
136    This will be used to create a new GDB frame struct, and then
137    INIT_EXTRA_FRAME_INFO and INIT_FRAME_PC will be called for the new frame.
138
139    For us, the frame address is its stack pointer value, so we look up
140    the function prologue to determine the caller's sp value, and return it.  */
141
142 CORE_ADDR
143 h8300_frame_chain (thisframe)
144      struct frame_info *thisframe;
145 {
146   frame_find_saved_regs (thisframe, (struct frame_saved_regs *) 0);
147   return thisframe->fsr->regs[SP_REGNUM];
148 }
149
150 /* Put here the code to store, into a struct frame_saved_regs,
151    the addresses of the saved registers of frame described by FRAME_INFO.
152    This includes special registers such as pc and fp saved in special
153    ways in the stack frame.  sp is even more special:
154    the address we return for it IS the sp for the next frame.
155
156    We cache the result of doing this in the frame_cache_obstack, since
157    it is fairly expensive.  */
158
159 void
160 frame_find_saved_regs (fi, fsr)
161      struct frame_info *fi;
162      struct frame_saved_regs *fsr;
163 {
164   register struct frame_saved_regs *cache_fsr;
165   extern struct obstack frame_cache_obstack;
166   CORE_ADDR ip;
167   struct symtab_and_line sal;
168   CORE_ADDR limit;
169
170   if (!fi->fsr)
171     {
172       cache_fsr = (struct frame_saved_regs *)
173         obstack_alloc (&frame_cache_obstack,
174                        sizeof (struct frame_saved_regs));
175       memset (cache_fsr, '\0', sizeof (struct frame_saved_regs));
176
177       fi->fsr = cache_fsr;
178
179       /* Find the start and end of the function prologue.  If the PC
180          is in the function prologue, we only consider the part that
181          has executed already.  */
182
183       ip = get_pc_function_start (fi->pc);
184       sal = find_pc_line (ip, 0);
185       limit = (sal.end && sal.end < fi->pc) ? sal.end : fi->pc;
186
187       /* This will fill in fields in *fi as well as in cache_fsr.  */
188       examine_prologue (ip, limit, fi->frame, cache_fsr, fi);
189     }
190
191   if (fsr)
192     *fsr = *fi->fsr;
193 }
194
195 /* Fetch the instruction at ADDR, returning 0 if ADDR is beyond LIM or
196    is not the address of a valid instruction, the address of the next
197    instruction beyond ADDR otherwise.  *PWORD1 receives the first word
198    of the instruction.*/
199
200 CORE_ADDR
201 NEXT_PROLOGUE_INSN (addr, lim, pword1)
202      CORE_ADDR addr;
203      CORE_ADDR lim;
204      INSN_WORD *pword1;
205 {
206   char buf[2];
207   if (addr < lim + 8)
208     {
209       read_memory (addr, buf, 2);
210       *pword1 = extract_signed_integer (buf, 2);
211
212       return addr + 2;
213     }
214   return 0;
215 }
216
217 /* Examine the prologue of a function.  `ip' points to the first instruction.
218    `limit' is the limit of the prologue (e.g. the addr of the first
219    linenumber, or perhaps the program counter if we're stepping through).
220    `frame_sp' is the stack pointer value in use in this frame.
221    `fsr' is a pointer to a frame_saved_regs structure into which we put
222    info about the registers saved by this frame.
223    `fi' is a struct frame_info pointer; we fill in various fields in it
224    to reflect the offsets of the arg pointer and the locals pointer.  */
225
226 static CORE_ADDR
227 examine_prologue (ip, limit, after_prolog_fp, fsr, fi)
228      register CORE_ADDR ip;
229      register CORE_ADDR limit;
230      CORE_ADDR after_prolog_fp;
231      struct frame_saved_regs *fsr;
232      struct frame_info *fi;
233 {
234   register CORE_ADDR next_ip;
235   int r;
236   int have_fp = 0;
237   INSN_WORD insn_word;
238   /* Number of things pushed onto stack, starts at 2/4, 'cause the
239      PC is already there */
240   unsigned int reg_save_depth = h8300hmode ? 4 : 2;
241
242   unsigned int auto_depth = 0;  /* Number of bytes of autos */
243
244   char in_frame[11];            /* One for each reg */
245
246   int adjust = 0;
247
248   memset (in_frame, 1, 11);
249   for (r = 0; r < 8; r++)
250     {
251       fsr->regs[r] = 0;
252     }
253   if (after_prolog_fp == 0)
254     {
255       after_prolog_fp = read_register (SP_REGNUM);
256     }
257   if (ip == 0 || ip & (h8300hmode ? ~0xffffff : ~0xffff))
258     return 0;
259
260   next_ip = NEXT_PROLOGUE_INSN (ip, limit, &insn_word);
261
262   if (insn_word == 0x0100)
263     {
264       insn_word = read_memory_unsigned_integer (ip + 2, 2);
265       adjust = 2;
266     }
267
268   /* Skip over any fp push instructions */
269   fsr->regs[6] = after_prolog_fp;
270   while (next_ip && IS_PUSH_FP (insn_word))
271     {
272       ip = next_ip + adjust;
273
274       in_frame[insn_word & 0x7] = reg_save_depth;
275       next_ip = NEXT_PROLOGUE_INSN (ip, limit, &insn_word);
276       reg_save_depth += 2 + adjust;
277     }
278
279   /* Is this a move into the fp */
280   if (next_ip && IS_MOV_SP_FP (insn_word))
281     {
282       ip = next_ip;
283       next_ip = NEXT_PROLOGUE_INSN (ip, limit, &insn_word);
284       have_fp = 1;
285     }
286
287   /* Skip over any stack adjustment, happens either with a number of
288      sub#2,sp or a mov #x,r5 sub r5,sp */
289
290   if (next_ip && (IS_SUB2_SP (insn_word) || IS_SUB4_SP (insn_word)))
291     {
292       while (next_ip && (IS_SUB2_SP (insn_word) || IS_SUB4_SP (insn_word)))
293         {
294           auto_depth += IS_SUB2_SP (insn_word) ? 2 : 4;
295           ip = next_ip;
296           next_ip = NEXT_PROLOGUE_INSN (ip, limit, &insn_word);
297         }
298     }
299   else
300     {
301       if (next_ip && IS_MOVK_R5 (insn_word))
302         {
303           ip = next_ip;
304           next_ip = NEXT_PROLOGUE_INSN (ip, limit, &insn_word);
305           auto_depth += insn_word;
306
307           next_ip = NEXT_PROLOGUE_INSN (next_ip, limit, &insn_word);
308           auto_depth += insn_word;
309         }
310       if (next_ip && IS_SUBL_SP (insn_word))
311         {
312           ip = next_ip;
313           auto_depth += read_memory_unsigned_integer (ip, 4);
314           ip += 4;
315
316           next_ip = NEXT_PROLOGUE_INSN (ip, limit, &insn_word);
317         }
318     }
319
320   /* Work out which regs are stored where */
321   while (next_ip && IS_PUSH (insn_word))
322     {
323       ip = next_ip;
324       next_ip = NEXT_PROLOGUE_INSN (ip, limit, &insn_word);
325       fsr->regs[r] = after_prolog_fp + auto_depth;
326       auto_depth += 2;
327     }
328
329   /* The args are always reffed based from the stack pointer */
330   fi->args_pointer = after_prolog_fp;
331   /* Locals are always reffed based from the fp */
332   fi->locals_pointer = after_prolog_fp;
333   /* The PC is at a known place */
334   fi->from_pc = read_memory_unsigned_integer (after_prolog_fp + BINWORD, BINWORD);
335
336   /* Rememeber any others too */
337   in_frame[PC_REGNUM] = 0;
338
339   if (have_fp)
340     /* We keep the old FP in the SP spot */
341     fsr->regs[SP_REGNUM] = read_memory_unsigned_integer (fsr->regs[6], BINWORD);
342   else
343     fsr->regs[SP_REGNUM] = after_prolog_fp + auto_depth;
344
345   return (ip);
346 }
347
348 void
349 init_extra_frame_info (fromleaf, fi)
350      int fromleaf;
351      struct frame_info *fi;
352 {
353   fi->fsr = 0;                  /* Not yet allocated */
354   fi->args_pointer = 0;         /* Unknown */
355   fi->locals_pointer = 0;       /* Unknown */
356   fi->from_pc = 0;
357 }
358
359 /* Return the saved PC from this frame.
360
361    If the frame has a memory copy of SRP_REGNUM, use that.  If not,
362    just use the register SRP_REGNUM itself.  */
363
364 CORE_ADDR
365 frame_saved_pc (frame)
366      struct frame_info *frame;
367 {
368   return frame->from_pc;
369 }
370
371 CORE_ADDR
372 frame_locals_address (fi)
373      struct frame_info *fi;
374 {
375   if (!fi->locals_pointer)
376     {
377       struct frame_saved_regs ignore;
378
379       get_frame_saved_regs (fi, &ignore);
380
381     }
382   return fi->locals_pointer;
383 }
384
385 /* Return the address of the argument block for the frame
386    described by FI.  Returns 0 if the address is unknown.  */
387
388 CORE_ADDR
389 frame_args_address (fi)
390      struct frame_info *fi;
391 {
392   if (!fi->args_pointer)
393     {
394       struct frame_saved_regs ignore;
395
396       get_frame_saved_regs (fi, &ignore);
397
398     }
399
400   return fi->args_pointer;
401 }
402
403 void 
404 h8300_pop_frame ()
405 {
406   unsigned regnum;
407   struct frame_saved_regs fsr;
408   struct frame_info *frame = get_current_frame ();
409
410   get_frame_saved_regs (frame, &fsr);
411
412   for (regnum = 0; regnum < 8; regnum++)
413     {
414       /* Don't forget SP_REGNUM is a frame_saved_regs struct is the
415          actual value we want, not the address of the value we want.  */
416       if (fsr.regs[regnum] && regnum != SP_REGNUM)
417         write_register (regnum, read_memory_integer(fsr.regs[regnum], BINWORD));
418       else if (fsr.regs[regnum] && regnum == SP_REGNUM)
419         write_register (regnum, fsr.regs[regnum]);
420     }
421
422   /* Don't forget the update the PC too!  */
423   write_pc (frame->from_pc);
424   flush_cached_frames ();
425 }
426
427
428 struct cmd_list_element *setmemorylist;
429
430 static void
431 h8300_command(args, from_tty)
432 {
433   extern int h8300hmode;
434   h8300hmode = 0;
435 /* start-sanitize-h8s */
436   h8300smode = 0;
437 /* end-sanitize-h8s */
438 }
439
440 static void
441 h8300h_command(args, from_tty)
442 {
443   extern int h8300hmode;
444   h8300hmode = 1;
445 /* start-sanitize-h8s */
446   h8300smode = 0;
447 /* end-sanitize-h8s */
448 }
449 /* start-sanitize-h8s */
450 static void
451 h8300s_command(args, from_tty)
452 {
453   extern int h8300smode;
454   extern int h8300hmode;
455   h8300smode = 1;
456   h8300hmode = 1;
457 }
458 /* end-santiize-h8s */
459
460
461 static void 
462 set_machine (args, from_tty)
463      char *args;
464      int from_tty;
465 {
466   printf_unfiltered ("\"set machine\" must be followed by h8300, h8300h");
467 /* start-sanitize-h8s */
468   printf_unfiltered ("or h8300s");
469 /* end-sanitize-h8s */
470   help_list (setmemorylist, "set memory ", -1, gdb_stdout);
471 }
472
473 /* set_machine_hook is called as the exec file is being opened, but
474    before the symbol file is opened.  This allows us to set the
475    h8300hmode flag based on the machine type specified in the exec
476    file.  This in turn will cause subsequently defined pointer types
477    to be 16 or 32 bits as appropriate for the machine.  */
478
479 static void
480 set_machine_hook (filename)
481      char *filename;
482 {
483 /* start-sanitize-h8s */
484   if (bfd_get_mach (exec_bfd) == bfd_mach_h8300s)
485     {
486       h8300smode = 1;
487       h8300hmode = 1;
488     }
489   else 
490 /* end-sanitize-h8s */
491     if (bfd_get_mach (exec_bfd) == bfd_mach_h8300h)
492     {
493 /* start-sanitize-h8s */
494       h8300smode = 0;
495 /* end-sanitize-h8s */
496       h8300hmode = 1;
497     }
498   else
499     {
500 /* start-sanitize-h8s */
501       h8300smode = 0;
502 /* end-sanitize-h8s */
503       h8300hmode = 0;
504     }
505 }
506
507 void
508 _initialize_h8300m ()
509 {
510   add_prefix_cmd ("machine", no_class, set_machine,
511                   "set the machine type", &setmemorylist, "set machine ", 0,
512                   &setlist);
513
514   add_cmd ("h8300", class_support, h8300_command,
515            "Set machine to be H8/300.", &setmemorylist);
516
517   add_cmd ("h8300h", class_support, h8300h_command,
518            "Set machine to be H8/300H.", &setmemorylist);
519
520 /* start-sanitize-h8s */
521   add_cmd ("h8300s", class_support, h8300s_command,
522            "Set machine to be H8/300S.", &setmemorylist);
523 /* end-sanitize-h8s */
524
525   /* Add a hook to set the machine type when we're loading a file. */
526
527   specify_exec_file_hook(set_machine_hook);
528 }
529
530
531
532 void
533 print_register_hook (regno)
534 {
535   if (regno == 8)
536     {
537       /* CCR register */
538       int C, Z, N, V;
539       unsigned char b[4];
540       unsigned char l;
541       read_relative_register_raw_bytes (regno, b);
542       l = b[REGISTER_VIRTUAL_SIZE(8) -1];
543       printf_unfiltered ("\t");
544       printf_unfiltered ("I-%d - ", (l & 0x80) != 0);
545       printf_unfiltered ("H-%d - ", (l & 0x20) != 0);
546       N = (l & 0x8) != 0;
547       Z = (l & 0x4) != 0;
548       V = (l & 0x2) != 0;
549       C = (l & 0x1) != 0;
550       printf_unfiltered ("N-%d ", N);
551       printf_unfiltered ("Z-%d ", Z);
552       printf_unfiltered ("V-%d ", V);
553       printf_unfiltered ("C-%d ", C);
554       if ((C | Z) == 0)
555         printf_unfiltered ("u> ");
556       if ((C | Z) == 1)
557         printf_unfiltered ("u<= ");
558       if ((C == 0))
559         printf_unfiltered ("u>= ");
560       if (C == 1)
561         printf_unfiltered ("u< ");
562       if (Z == 0)
563         printf_unfiltered ("!= ");
564       if (Z == 1)
565         printf_unfiltered ("== ");
566       if ((N ^ V) == 0)
567         printf_unfiltered (">= ");
568       if ((N ^ V) == 1)
569         printf_unfiltered ("< ");
570       if ((Z | (N ^ V)) == 0)
571         printf_unfiltered ("> ");
572       if ((Z | (N ^ V)) == 1)
573         printf_unfiltered ("<= ");
574     }
575 }
576
577 void
578 _initialize_h8300_tdep ()
579 {
580   tm_print_insn = gdb_print_insn_h8300;
581 }