* ser-go32.c: Lint. (strncasecmp): Removed, now in libiberty.
[external/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., 675 Mass Ave, Cambridge, MA 02139, 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
33 #undef NUM_REGS
34 #define NUM_REGS 11
35
36 #define UNSIGNED_SHORT(X) ((X) & 0xffff)
37
38 /* an easy to debug H8 stack frame looks like:
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
44
45  */
46
47 #define IS_PUSH(x) ((x & 0xff00)==0x6d00)
48 #define IS_PUSH_FP(x) (x == 0x6df6)
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)
53 #define IS_SUB_R5SP(x) (x==0x1957)
54
55 static CORE_ADDR examine_prologue ();
56
57 void frame_find_saved_regs ();
58 CORE_ADDR 
59 h8300_skip_prologue (start_pc)
60      CORE_ADDR start_pc;
61 {
62   short int w;
63
64   w = read_memory_unsigned_integer (start_pc, 2);
65   /* Skip past all push insns */
66   while (IS_PUSH_FP (w))
67     {
68       start_pc += 2;
69       w = read_memory_unsigned_integer (start_pc, 2);
70     }
71
72   /* Skip past a move to FP */
73   if (IS_MOVE_FP (w))
74     {
75       start_pc += 2;
76       w = read_memory_unsigned_integer (start_pc, 2);
77     }
78
79   /* Skip the stack adjust */
80
81   if (IS_MOVK_R5 (w))
82     {
83       start_pc += 2;
84       w = read_memory_unsigned_integer (start_pc, 2);
85     }
86   if (IS_SUB_R5SP (w))
87     {
88       start_pc += 2;
89       w = read_memory_unsigned_integer (start_pc, 2);
90     }
91   while (IS_SUB2_SP (w))
92     {
93       start_pc += 2;
94       w = read_memory_unsigned_integer (start_pc, 2);
95     }
96
97   return start_pc;
98 }
99
100 int
101 print_insn (memaddr, stream)
102      CORE_ADDR memaddr;
103      GDB_FILE *stream;
104 {
105   disassemble_info info;
106   GDB_INIT_DISASSEMBLE_INFO(info, stream);
107   if (h8300hmode)
108     return print_insn_h8300h (memaddr, &info);
109   else
110     return print_insn_h8300 (memaddr, &info);
111 }
112
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 {
124   frame_find_saved_regs (thisframe, (struct frame_saved_regs *) 0);
125   return thisframe->fsr->regs[SP_REGNUM];
126 }
127
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 *)
154         obstack_alloc (&frame_cache_obstack,
155                        sizeof (struct frame_saved_regs));
156       memset (cache_fsr, '\0', sizeof (struct frame_saved_regs));
157
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.  */
163
164       ip = get_pc_function_start (fi->pc);
165       sal = find_pc_line (ip, 0);
166       limit = (sal.end && sal.end < fi->pc) ? sal.end : fi->pc;
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 }
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
181 CORE_ADDR
182 NEXT_PROLOGUE_INSN (addr, lim, pword1)
183      CORE_ADDR addr;
184      CORE_ADDR lim;
185      INSN_WORD *pword1;
186 {
187   char buf[2];
188   if (addr < lim + 8)
189     {
190       read_memory (addr, buf, 2);
191       *pword1 = extract_signed_integer (buf, 2);
192
193       return addr + 2;
194     }
195   return 0;
196 }
197
198 /* Examine the prologue of a function.  `ip' points to the first instruction.
199    `limit' is the limit of the prologue (e.g. the addr of the first
200    linenumber, or perhaps the program counter if we're stepping through).
201    `frame_sp' is the stack pointer value in use in this frame.
202    `fsr' is a pointer to a frame_saved_regs structure into which we put
203    info about the registers saved by this frame.
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
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;
219   register int src;
220   register struct pic_prologue_code *pcode;
221   INSN_WORD insn_word;
222   int size, offset;
223   /* Number of things pushed onto stack, starts at 2/4, 'cause the
224      PC is already there */
225   unsigned int reg_save_depth = h8300hmode ? 4 : 2;
226
227   unsigned int auto_depth = 0;  /* Number of bytes of autos */
228
229   char in_frame[11];            /* One for each reg */
230
231   memset (in_frame, 1, 11);
232   for (r = 0; r < 8; r++)
233     {
234       fsr->regs[r] = 0;
235     }
236   if (after_prolog_fp == 0)
237     {
238       after_prolog_fp = read_register (SP_REGNUM);
239     }
240   if (ip == 0 || ip & (h8300hmode ? ~0xffff : ~0xffff))
241     return 0;
242
243   next_ip = NEXT_PROLOGUE_INSN (ip, limit, &insn_word);
244
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;
250
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     }
255
256   /* Is this a move into the fp */
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     }
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
267   if (next_ip && IS_SUB2_SP (insn_word))
268     {
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         }
275     }
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;
286         }
287     }
288   /* Work out which regs are stored where */
289   while (next_ip && IS_PUSH (insn_word))
290     {
291       ip = next_ip;
292       next_ip = NEXT_PROLOGUE_INSN (ip, limit, &insn_word);
293       fsr->regs[r] = after_prolog_fp + auto_depth;
294       auto_depth += 2;
295     }
296
297   /* The args are always reffed based from the stack pointer */
298   fi->args_pointer = after_prolog_fp;
299   /* Locals are always reffed based from the fp */
300   fi->locals_pointer = after_prolog_fp;
301   /* The PC is at a known place */
302   fi->from_pc = read_memory_unsigned_integer (after_prolog_fp + 2, BINWORD);
303
304   /* Rememeber any others too */
305   in_frame[PC_REGNUM] = 0;
306
307   if (have_fp)
308     /* We keep the old FP in the SP spot */
309     fsr->regs[SP_REGNUM] = read_memory_unsigned_integer (fsr->regs[6], BINWORD);
310   else
311     fsr->regs[SP_REGNUM] = after_prolog_fp + auto_depth;
312
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;
325 }
326
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)
334      FRAME frame;
335 {
336   return frame->from_pc;
337 }
338
339 CORE_ADDR
340 frame_locals_address (fi)
341      struct frame_info *fi;
342 {
343   if (!fi->locals_pointer)
344     {
345       struct frame_saved_regs ignore;
346
347       get_frame_saved_regs (fi, &ignore);
348
349     }
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 {
360   if (!fi->args_pointer)
361     {
362       struct frame_saved_regs ignore;
363
364       get_frame_saved_regs (fi, &ignore);
365
366     }
367
368   return fi->args_pointer;
369 }
370
371 void 
372 h8300_pop_frame ()
373 {
374   unsigned regnum;
375   struct frame_saved_regs fsr;
376   struct frame_info *fi;
377
378   FRAME frame = get_current_frame ();
379
380   fi = get_frame_info (frame);
381   get_frame_saved_regs (fi, &fsr);
382
383   for (regnum = 0; regnum < 8; regnum++)
384     {
385       if (fsr.regs[regnum])
386         {
387           write_register (regnum, read_memory_integer(fsr.regs[regnum]), BINWORD);
388         }
389
390       flush_cached_frames ();
391       set_current_frame (create_new_frame (read_register (FP_REGNUM),
392                                            read_pc ()));
393     }
394 }
395
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 {
418   printf_unfiltered ("\"set machine\" must be followed by h8300 or h8300h.\n");
419   help_list (setmemorylist, "set memory ", -1, gdb_stdout);
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
438 void
439 print_register_hook (regno)
440 {
441   if (regno == 8)
442     {
443       /* CCR register */
444       int C, Z, N, V;
445       unsigned char b[4];
446       unsigned char l;
447       read_relative_register_raw_bytes (regno, b);
448       l = b[REGISTER_VIRTUAL_SIZE(8) -1];
449       printf_unfiltered ("\t");
450       printf_unfiltered ("I-%d - ", (l & 0x80) != 0);
451       printf_unfiltered ("H-%d - ", (l & 0x20) != 0);
452       N = (l & 0x8) != 0;
453       Z = (l & 0x4) != 0;
454       V = (l & 0x2) != 0;
455       C = (l & 0x1) != 0;
456       printf_unfiltered ("N-%d ", N);
457       printf_unfiltered ("Z-%d ", Z);
458       printf_unfiltered ("V-%d ", V);
459       printf_unfiltered ("C-%d ", C);
460       if ((C | Z) == 0)
461         printf_unfiltered ("u> ");
462       if ((C | Z) == 1)
463         printf_unfiltered ("u<= ");
464       if ((C == 0))
465         printf_unfiltered ("u>= ");
466       if (C == 1)
467         printf_unfiltered ("u< ");
468       if (Z == 0)
469         printf_unfiltered ("!= ");
470       if (Z == 1)
471         printf_unfiltered ("== ");
472       if ((N ^ V) == 0)
473         printf_unfiltered (">= ");
474       if ((N ^ V) == 1)
475         printf_unfiltered ("< ");
476       if ((Z | (N ^ V)) == 0)
477         printf_unfiltered ("> ");
478       if ((Z | (N ^ V)) == 1)
479         printf_unfiltered ("<= ");
480     }
481 }
482