34cc0eb6386c36385c80405d0dee715ee6b90a10
[external/binutils.git] / gdb / m68k-tdep.c
1 /* Target dependent code for the Motorola 68000 series.
2    Copyright 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1999, 2000, 2001
3    Free Software Foundation, Inc.
4
5    This file is part of GDB.
6
7    This program is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 2 of the License, or
10    (at your option) any later version.
11
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16
17    You should have received a copy of the GNU General Public License
18    along with this program; if not, write to the Free Software
19    Foundation, Inc., 59 Temple Place - Suite 330,
20    Boston, MA 02111-1307, USA.  */
21
22 #include "defs.h"
23 #include "frame.h"
24 #include "symtab.h"
25 #include "gdbcore.h"
26 #include "value.h"
27 #include "gdb_string.h"
28 #include "inferior.h"
29 #include "regcache.h"
30 #include "arch-utils.h"
31
32 #include "m68k-tdep.h"
33 \f
34
35 #define P_LINKL_FP      0x480e
36 #define P_LINKW_FP      0x4e56
37 #define P_PEA_FP        0x4856
38 #define P_MOVL_SP_FP    0x2c4f
39 #define P_MOVL          0x207c
40 #define P_JSR           0x4eb9
41 #define P_BSR           0x61ff
42 #define P_LEAL          0x43fb
43 #define P_MOVML         0x48ef
44 #define P_FMOVM         0xf237
45 #define P_TRAP          0x4e40
46
47
48 #define REGISTER_BYTES_FP (16*4 + 8 + 8*12 + 3*4)
49 #define REGISTER_BYTES_NOFP (16*4 + 8)
50
51 #define NUM_FREGS (NUM_REGS-24)
52
53 /* Offset from SP to first arg on stack at first instruction of a function */
54
55 #define SP_ARG0 (1 * 4)
56
57 /* This was determined by experimentation on hp300 BSD 4.3.  Perhaps
58    it corresponds to some offset in /usr/include/sys/user.h or
59    something like that.  Using some system include file would
60    have the advantage of probably being more robust in the face
61    of OS upgrades, but the disadvantage of being wrong for
62    cross-debugging.  */
63
64 #define SIG_PC_FP_OFFSET 530
65
66 #define TARGET_M68K
67
68
69 #if !defined (BPT_VECTOR)
70 #define BPT_VECTOR 0xf
71 #endif
72
73 #if !defined (REMOTE_BPT_VECTOR)
74 #define REMOTE_BPT_VECTOR 1
75 #endif
76
77
78 void m68k_frame_init_saved_regs (struct frame_info *frame_info);
79
80
81 /* gdbarch_breakpoint_from_pc is set to m68k_local_breakpoint_from_pc
82    so m68k_remote_breakpoint_from_pc is currently not used.  */
83
84 const static unsigned char *
85 m68k_remote_breakpoint_from_pc (CORE_ADDR *pcptr, int *lenptr)
86 {
87   static unsigned char break_insn[] = {0x4e, (0x40 | REMOTE_BPT_VECTOR)};
88   *lenptr = sizeof (break_insn);
89   return break_insn;
90 }
91
92 const static unsigned char *
93 m68k_local_breakpoint_from_pc (CORE_ADDR *pcptr, int *lenptr)
94 {
95   static unsigned char break_insn[] = {0x4e, (0x40 | BPT_VECTOR)};
96   *lenptr = sizeof (break_insn);
97   return break_insn;
98 }
99
100
101 static int
102 m68k_register_bytes_ok (long numbytes)
103 {
104   return ((numbytes == REGISTER_BYTES_FP)
105           || (numbytes == REGISTER_BYTES_NOFP));
106 }
107
108 /* Number of bytes of storage in the actual machine representation
109    for register regnum.  On the 68000, all regs are 4 bytes
110    except the floating point regs which are 12 bytes.  */
111 /* Note that the unsigned cast here forces the result of the
112    subtraction to very high positive values if regnum < FP0_REGNUM */
113
114 static int
115 m68k_register_raw_size (int regnum)
116 {
117   return (((unsigned) (regnum) - FP0_REGNUM) < 8 ? 12 : 4);
118 }
119
120 /* Number of bytes of storage in the program's representation
121    for register regnum.  On the 68000, all regs are 4 bytes
122    except the floating point regs which are 12-byte long doubles.  */
123
124 static int
125 m68k_register_virtual_size (int regnum)
126 {
127   return (((unsigned) (regnum) - FP0_REGNUM) < 8 ? 12 : 4);
128 }
129
130 /* Return the GDB type object for the "standard" data type of data in
131    register N.  This should be int for D0-D7, SR, FPCONTROL and
132    FPSTATUS, long double for FP0-FP7, and void pointer for all others
133    (A0-A7, PC, FPIADDR).  Note, for registers which contain
134    addresses return pointer to void, not pointer to char, because we
135    don't want to attempt to print the string after printing the
136    address.  */
137
138 static struct type *
139 m68k_register_virtual_type (int regnum)
140 {
141   if (regnum >= FP0_REGNUM && regnum <= FP0_REGNUM + 7)
142     return builtin_type_m68881_ext;
143
144   if (regnum == M68K_FPI_REGNUM || regnum == PC_REGNUM)
145     return builtin_type_void_func_ptr;
146
147   if (regnum == M68K_FPC_REGNUM || regnum == M68K_FPS_REGNUM
148       || regnum == PS_REGNUM)
149     return builtin_type_int32;
150
151   if (regnum >= M68K_A0_REGNUM && regnum <= M68K_A0_REGNUM + 7)
152     return builtin_type_void_data_ptr;
153
154   return builtin_type_int32;
155 }
156
157 /* Function: m68k_register_name
158    Returns the name of the standard m68k register regnum. */
159
160 static const char *
161 m68k_register_name (int regnum)
162 {
163   static char *register_names[] = {
164     "d0", "d1", "d2", "d3", "d4", "d5", "d6", "d7",
165     "a0", "a1", "a2", "a3", "a4", "a5", "fp", "sp",
166     "ps", "pc",
167     "fp0", "fp1", "fp2", "fp3", "fp4", "fp5", "fp6", "fp7",
168     "fpcontrol", "fpstatus", "fpiaddr", "fpcode", "fpflags"
169   };
170
171   if (regnum < 0 ||
172       regnum >= sizeof (register_names) / sizeof (register_names[0]))
173     internal_error (__FILE__, __LINE__,
174                     "m68k_register_name: illegal register number %d", regnum);
175   else
176     return register_names[regnum];
177 }
178
179 /* Stack must be kept short aligned when doing function calls.  */
180
181 static CORE_ADDR
182 m68k_stack_align (CORE_ADDR addr)
183 {
184   return ((addr + 1) & ~1);
185 }
186
187 /* Index within `registers' of the first byte of the space for
188    register regnum.  */
189
190 static int
191 m68k_register_byte (int regnum)
192 {
193   if (regnum >= M68K_FPC_REGNUM)
194     return (((regnum - M68K_FPC_REGNUM) * 4) + 168);
195   else if (regnum >= FP0_REGNUM)
196     return (((regnum - FP0_REGNUM) * 12) + 72);
197   else
198     return (regnum * 4);
199 }
200
201 /* Store the address of the place in which to copy the structure the
202    subroutine will return.  This is called from call_function. */
203
204 static void
205 m68k_store_struct_return (CORE_ADDR addr, CORE_ADDR sp)
206 {
207   write_register (M68K_A1_REGNUM, addr);
208 }
209
210 /* Extract from an array regbuf containing the (raw) register state
211    a function return value of type type, and copy that, in virtual format,
212    into valbuf.  This is assuming that floating point values are returned
213    as doubles in d0/d1.  */
214
215 static void
216 m68k_deprecated_extract_return_value (struct type *type, char *regbuf,
217                                       char *valbuf)
218 {
219   int offset = 0;
220   int typeLength = TYPE_LENGTH (type);
221
222   if (typeLength < 4)
223     offset = 4 - typeLength;
224
225   memcpy (valbuf, regbuf + offset, typeLength);
226 }
227
228 static CORE_ADDR
229 m68k_deprecated_extract_struct_value_address (char *regbuf)
230 {
231   return (*(CORE_ADDR *) (regbuf));
232 }
233
234 /* Write into appropriate registers a function return value
235    of type TYPE, given in virtual format.  Assumes floats are passed
236    in d0/d1.  */
237
238 static void
239 m68k_store_return_value (struct type *type, char *valbuf)
240 {
241   deprecated_write_register_bytes (0, valbuf, TYPE_LENGTH (type));
242 }
243
244 /* Describe the pointer in each stack frame to the previous stack frame
245    (its caller).  */
246
247 /* FRAME_CHAIN takes a frame's nominal address and produces the frame's
248    chain-pointer.
249    In the case of the 68000, the frame's nominal address
250    is the address of a 4-byte word containing the calling frame's address.  */
251
252 /* If we are chaining from sigtramp, then manufacture a sigtramp frame
253    (which isn't really on the stack.  I'm not sure this is right for anything
254    but BSD4.3 on an hp300.  */
255
256 static CORE_ADDR
257 m68k_frame_chain (struct frame_info *thisframe)
258 {
259   if (get_frame_type (thisframe) == SIGTRAMP_FRAME)
260     return thisframe->frame;
261   else if (!inside_entry_file (get_frame_pc (thisframe)))
262     return read_memory_unsigned_integer (thisframe->frame, 4);
263   else
264     return 0;
265 }
266
267 /* A function that tells us whether the function invocation represented
268    by fi does not have a frame on the stack associated with it.  If it
269    does not, FRAMELESS is set to 1, else 0.  */
270
271 static int
272 m68k_frameless_function_invocation (struct frame_info *fi)
273 {
274   if (get_frame_type (fi) == SIGTRAMP_FRAME)
275     return 0;
276   else
277     return frameless_look_for_prologue (fi);
278 }
279
280 static CORE_ADDR
281 m68k_frame_saved_pc (struct frame_info *frame)
282 {
283   if (get_frame_type (frame) == SIGTRAMP_FRAME)
284     {
285       if (frame->next)
286         return read_memory_unsigned_integer (frame->next->frame
287                                              + SIG_PC_FP_OFFSET, 4);
288       else
289         return read_memory_unsigned_integer (read_register (SP_REGNUM)
290                                              + SIG_PC_FP_OFFSET - 8, 4);
291     }
292   else
293     return read_memory_unsigned_integer (frame->frame + 4, 4);
294 }
295
296
297 /* The only reason this is here is the tm-altos.h reference below.  It
298    was moved back here from tm-m68k.h.  FIXME? */
299
300 extern CORE_ADDR
301 altos_skip_prologue (CORE_ADDR pc)
302 {
303   register int op = read_memory_unsigned_integer (pc, 2);
304   if (op == P_LINKW_FP)
305     pc += 4;                    /* Skip link #word */
306   else if (op == P_LINKL_FP)
307     pc += 6;                    /* Skip link #long */
308   /* Not sure why branches are here.  */
309   /* From tm-altos.h */
310   else if (op == 0060000)
311     pc += 4;                    /* Skip bra #word */
312   else if (op == 00600377)
313     pc += 6;                    /* skip bra #long */
314   else if ((op & 0177400) == 0060000)
315     pc += 2;                    /* skip bra #char */
316   return pc;
317 }
318
319 int
320 delta68_in_sigtramp (CORE_ADDR pc, char *name)
321 {
322   if (name != NULL)
323     return strcmp (name, "_sigcode") == 0;
324   else
325     return 0;
326 }
327
328 CORE_ADDR
329 delta68_frame_args_address (struct frame_info *frame_info)
330 {
331   /* we assume here that the only frameless functions are the system calls
332      or other functions who do not put anything on the stack. */
333   if (get_frame_type (frame_info) == SIGTRAMP_FRAME)
334     return frame_info->frame + 12;
335   else if (frameless_look_for_prologue (frame_info))
336     {
337       /* Check for an interrupted system call */
338       if (frame_info->next && (get_frame_type (frame_info->next) == SIGTRAMP_FRAME))
339         return frame_info->next->frame + 16;
340       else
341         return frame_info->frame + 4;
342     }
343   else
344     return frame_info->frame;
345 }
346
347 CORE_ADDR
348 delta68_frame_saved_pc (struct frame_info *frame_info)
349 {
350   return read_memory_unsigned_integer (delta68_frame_args_address (frame_info)
351                                        + 4, 4);
352 }
353
354 /* Return number of args passed to a frame.
355    Can return -1, meaning no way to tell.  */
356
357 int
358 isi_frame_num_args (struct frame_info *fi)
359 {
360   int val;
361   CORE_ADDR pc = FRAME_SAVED_PC (fi);
362   int insn = read_memory_unsigned_integer (pc, 2);
363   val = 0;
364   if (insn == 0047757 || insn == 0157374)       /* lea W(sp),sp or addaw #W,sp */
365     val = read_memory_integer (pc + 2, 2);
366   else if ((insn & 0170777) == 0050217  /* addql #N, sp */
367            || (insn & 0170777) == 0050117)      /* addqw */
368     {
369       val = (insn >> 9) & 7;
370       if (val == 0)
371         val = 8;
372     }
373   else if (insn == 0157774)     /* addal #WW, sp */
374     val = read_memory_integer (pc + 2, 4);
375   val >>= 2;
376   return val;
377 }
378
379 int
380 delta68_frame_num_args (struct frame_info *fi)
381 {
382   int val;
383   CORE_ADDR pc = FRAME_SAVED_PC (fi);
384   int insn = read_memory_unsigned_integer (pc, 2);
385   val = 0;
386   if (insn == 0047757 || insn == 0157374)       /* lea W(sp),sp or addaw #W,sp */
387     val = read_memory_integer (pc + 2, 2);
388   else if ((insn & 0170777) == 0050217  /* addql #N, sp */
389            || (insn & 0170777) == 0050117)      /* addqw */
390     {
391       val = (insn >> 9) & 7;
392       if (val == 0)
393         val = 8;
394     }
395   else if (insn == 0157774)     /* addal #WW, sp */
396     val = read_memory_integer (pc + 2, 4);
397   val >>= 2;
398   return val;
399 }
400
401 int
402 news_frame_num_args (struct frame_info *fi)
403 {
404   int val;
405   CORE_ADDR pc = FRAME_SAVED_PC (fi);
406   int insn = read_memory_unsigned_integer (pc, 2);
407   val = 0;
408   if (insn == 0047757 || insn == 0157374)       /* lea W(sp),sp or addaw #W,sp */
409     val = read_memory_integer (pc + 2, 2);
410   else if ((insn & 0170777) == 0050217  /* addql #N, sp */
411            || (insn & 0170777) == 0050117)      /* addqw */
412     {
413       val = (insn >> 9) & 7;
414       if (val == 0)
415         val = 8;
416     }
417   else if (insn == 0157774)     /* addal #WW, sp */
418     val = read_memory_integer (pc + 2, 4);
419   val >>= 2;
420   return val;
421 }
422
423 /* Insert the specified number of args and function address
424    into a call sequence of the above form stored at DUMMYNAME.
425    We use the BFD routines to store a big-endian value of known size.  */
426
427 void
428 m68k_fix_call_dummy (char *dummy, CORE_ADDR pc, CORE_ADDR fun, int nargs,
429                      struct value **args, struct type *type, int gcc_p)
430 {
431   bfd_putb32 (fun, (unsigned char *) dummy + CALL_DUMMY_START_OFFSET + 2);
432   bfd_putb32 (nargs * 4,
433               (unsigned char *) dummy + CALL_DUMMY_START_OFFSET + 8);
434 }
435
436
437 /* Push an empty stack frame, to record the current PC, etc.  */
438
439 void
440 m68k_push_dummy_frame (void)
441 {
442   register CORE_ADDR sp = read_register (SP_REGNUM);
443   register int regnum;
444   char raw_buffer[12];
445
446   sp = push_word (sp, read_register (PC_REGNUM));
447   sp = push_word (sp, read_register (FP_REGNUM));
448   write_register (FP_REGNUM, sp);
449
450   /* Always save the floating-point registers, whether they exist on
451      this target or not.  */
452   for (regnum = FP0_REGNUM + 7; regnum >= FP0_REGNUM; regnum--)
453     {
454       deprecated_read_register_bytes (REGISTER_BYTE (regnum), raw_buffer, 12);
455       sp = push_bytes (sp, raw_buffer, 12);
456     }
457
458   for (regnum = FP_REGNUM - 1; regnum >= 0; regnum--)
459     {
460       sp = push_word (sp, read_register (regnum));
461     }
462   sp = push_word (sp, read_register (PS_REGNUM));
463   write_register (SP_REGNUM, sp);
464 }
465
466 /* Discard from the stack the innermost frame,
467    restoring all saved registers.  */
468
469 void
470 m68k_pop_frame (void)
471 {
472   register struct frame_info *frame = get_current_frame ();
473   register CORE_ADDR fp;
474   register int regnum;
475   char raw_buffer[12];
476
477   fp = get_frame_base (frame);
478   m68k_frame_init_saved_regs (frame);
479   for (regnum = FP0_REGNUM + 7; regnum >= FP0_REGNUM; regnum--)
480     {
481       if (get_frame_saved_regs (frame)[regnum])
482         {
483           read_memory (get_frame_saved_regs (frame)[regnum], raw_buffer, 12);
484           deprecated_write_register_bytes (REGISTER_BYTE (regnum), raw_buffer,
485                                            12);
486         }
487     }
488   for (regnum = FP_REGNUM - 1; regnum >= 0; regnum--)
489     {
490       if (get_frame_saved_regs (frame)[regnum])
491         {
492           write_register (regnum,
493                           read_memory_integer (get_frame_saved_regs (frame)[regnum], 4));
494         }
495     }
496   if (get_frame_saved_regs (frame)[PS_REGNUM])
497     {
498       write_register (PS_REGNUM,
499                       read_memory_integer (get_frame_saved_regs (frame)[PS_REGNUM], 4));
500     }
501   write_register (FP_REGNUM, read_memory_integer (fp, 4));
502   write_register (PC_REGNUM, read_memory_integer (fp + 4, 4));
503   write_register (SP_REGNUM, fp + 8);
504   flush_cached_frames ();
505 }
506 \f
507
508 /* Given an ip value corresponding to the start of a function,
509    return the ip of the first instruction after the function 
510    prologue.  This is the generic m68k support.  Machines which
511    require something different can override the SKIP_PROLOGUE
512    macro to point elsewhere.
513
514    Some instructions which typically may appear in a function
515    prologue include:
516
517    A link instruction, word form:
518
519    link.w       %a6,&0                  4e56  XXXX
520
521    A link instruction, long form:
522
523    link.l  %fp,&F%1             480e  XXXX  XXXX
524
525    A movm instruction to preserve integer regs:
526
527    movm.l  &M%1,(4,%sp)         48ef  XXXX  XXXX
528
529    A fmovm instruction to preserve float regs:
530
531    fmovm   &FPM%1,(FPO%1,%sp)   f237  XXXX  XXXX  XXXX  XXXX
532
533    Some profiling setup code (FIXME, not recognized yet):
534
535    lea.l   (.L3,%pc),%a1                43fb  XXXX  XXXX  XXXX
536    bsr     _mcount                      61ff  XXXX  XXXX
537
538  */
539
540 CORE_ADDR
541 m68k_skip_prologue (CORE_ADDR ip)
542 {
543   register CORE_ADDR limit;
544   struct symtab_and_line sal;
545   register int op;
546
547   /* Find out if there is a known limit for the extent of the prologue.
548      If so, ensure we don't go past it.  If not, assume "infinity". */
549
550   sal = find_pc_line (ip, 0);
551   limit = (sal.end) ? sal.end : (CORE_ADDR) ~0;
552
553   while (ip < limit)
554     {
555       op = read_memory_unsigned_integer (ip, 2);
556
557       if (op == P_LINKW_FP)
558         ip += 4;                /* Skip link.w */
559       else if (op == P_PEA_FP)
560         ip += 2;                /* Skip pea %fp */
561       else if (op == P_MOVL_SP_FP)
562         ip += 2;                /* Skip move.l %sp, %fp */
563       else if (op == P_LINKL_FP)
564         ip += 6;                /* Skip link.l */
565       else if (op == P_MOVML)
566         ip += 6;                /* Skip movm.l */
567       else if (op == P_FMOVM)
568         ip += 10;               /* Skip fmovm */
569       else
570         break;                  /* Found unknown code, bail out. */
571     }
572   return (ip);
573 }
574
575 /* Store the addresses of the saved registers of the frame described by 
576    FRAME_INFO in its saved_regs field.
577    This includes special registers such as pc and fp saved in special
578    ways in the stack frame.  sp is even more special:
579    the address we return for it IS the sp for the next frame.  */
580
581 void
582 m68k_frame_init_saved_regs (struct frame_info *frame_info)
583 {
584   register int regnum;
585   register int regmask;
586   register CORE_ADDR next_addr;
587   register CORE_ADDR pc;
588
589   /* First possible address for a pc in a call dummy for this frame.  */
590   CORE_ADDR possible_call_dummy_start =
591     frame_info->frame - 28 - FP_REGNUM * 4 - 4 - 8 * 12;
592
593   int nextinsn;
594
595   if (get_frame_saved_regs (frame_info))
596     return;
597
598   frame_saved_regs_zalloc (frame_info);
599
600   memset (get_frame_saved_regs (frame_info), 0, SIZEOF_FRAME_SAVED_REGS);
601
602   if (get_frame_pc (frame_info) >= possible_call_dummy_start
603       && get_frame_pc (frame_info) <= frame_info->frame)
604     {
605
606       /* It is a call dummy.  We could just stop now, since we know
607          what the call dummy saves and where.  But this code proceeds
608          to parse the "prologue" which is part of the call dummy.
609          This is needlessly complex and confusing.  FIXME.  */
610
611       next_addr = frame_info->frame;
612       pc = possible_call_dummy_start;
613     }
614   else
615     {
616       pc = get_pc_function_start (get_frame_pc (frame_info));
617
618       nextinsn = read_memory_unsigned_integer (pc, 2);
619       if (P_PEA_FP == nextinsn
620           && P_MOVL_SP_FP == read_memory_unsigned_integer (pc + 2, 2))
621         {
622           /* pea %fp
623              move.l %sp, %fp */
624           next_addr = frame_info->frame;
625           pc += 4;
626         }
627       else if (P_LINKL_FP == nextinsn)
628         /* link.l %fp */
629         /* Find the address above the saved   
630            regs using the amount of storage from the link instruction.  */
631         {
632           next_addr = frame_info->frame + read_memory_integer (pc + 2, 4);
633           pc += 6;
634         }
635       else if (P_LINKW_FP == nextinsn)
636         /* link.w %fp */
637         /* Find the address above the saved   
638            regs using the amount of storage from the link instruction.  */
639         {
640           next_addr = frame_info->frame + read_memory_integer (pc + 2, 2);
641           pc += 4;
642         }
643       else
644         goto lose;
645
646       /* If have an addal #-n, sp next, adjust next_addr.  */
647       if (read_memory_unsigned_integer (pc, 2) == 0157774)
648         next_addr += read_memory_integer (pc += 2, 4), pc += 4;
649     }
650
651   for (;;)
652     {
653       nextinsn = read_memory_unsigned_integer (pc, 2);
654       regmask = read_memory_unsigned_integer (pc + 2, 2);
655       /* fmovemx to -(sp) */
656       if (0xf227 == nextinsn && (regmask & 0xff00) == 0xe000)
657         {
658           /* Regmask's low bit is for register fp7, the first pushed */
659           for (regnum = FP0_REGNUM + 8; --regnum >= FP0_REGNUM; regmask >>= 1)
660             if (regmask & 1)
661               get_frame_saved_regs (frame_info)[regnum] = (next_addr -= 12);
662           pc += 4;
663         }
664       /* fmovemx to (fp + displacement) */
665       else if (0171056 == nextinsn && (regmask & 0xff00) == 0xf000)
666         {
667           register CORE_ADDR addr;
668
669           addr = frame_info->frame + read_memory_integer (pc + 4, 2);
670           /* Regmask's low bit is for register fp7, the first pushed */
671           for (regnum = FP0_REGNUM + 8; --regnum >= FP0_REGNUM; regmask >>= 1)
672             if (regmask & 1)
673               {
674                 get_frame_saved_regs (frame_info)[regnum] = addr;
675                 addr += 12;
676               }
677           pc += 6;
678         }
679       /* moveml to (sp) */
680       else if (0044327 == nextinsn)
681         {
682           /* Regmask's low bit is for register 0, the first written */
683           for (regnum = 0; regnum < 16; regnum++, regmask >>= 1)
684             if (regmask & 1)
685               {
686                 get_frame_saved_regs (frame_info)[regnum] = next_addr;
687                 next_addr += 4;
688               }
689           pc += 4;
690         }
691       /* moveml to (fp + displacement) */
692       else if (0044356 == nextinsn)
693         {
694           register CORE_ADDR addr;
695
696           addr = frame_info->frame + read_memory_integer (pc + 4, 2);
697           /* Regmask's low bit is for register 0, the first written */
698           for (regnum = 0; regnum < 16; regnum++, regmask >>= 1)
699             if (regmask & 1)
700               {
701                 get_frame_saved_regs (frame_info)[regnum] = addr;
702                 addr += 4;
703               }
704           pc += 6;
705         }
706       /* moveml to -(sp) */
707       else if (0044347 == nextinsn)
708         {
709           /* Regmask's low bit is for register 15, the first pushed */
710           for (regnum = 16; --regnum >= 0; regmask >>= 1)
711             if (regmask & 1)
712               get_frame_saved_regs (frame_info)[regnum] = (next_addr -= 4);
713           pc += 4;
714         }
715       /* movl r,-(sp) */
716       else if (0x2f00 == (0xfff0 & nextinsn))
717         {
718           regnum = 0xf & nextinsn;
719           get_frame_saved_regs (frame_info)[regnum] = (next_addr -= 4);
720           pc += 2;
721         }
722       /* fmovemx to index of sp */
723       else if (0xf236 == nextinsn && (regmask & 0xff00) == 0xf000)
724         {
725           /* Regmask's low bit is for register fp0, the first written */
726           for (regnum = FP0_REGNUM + 8; --regnum >= FP0_REGNUM; regmask >>= 1)
727             if (regmask & 1)
728               {
729                 get_frame_saved_regs (frame_info)[regnum] = next_addr;
730                 next_addr += 12;
731               }
732           pc += 10;
733         }
734       /* clrw -(sp); movw ccr,-(sp) */
735       else if (0x4267 == nextinsn && 0x42e7 == regmask)
736         {
737           get_frame_saved_regs (frame_info)[PS_REGNUM] = (next_addr -= 4);
738           pc += 4;
739         }
740       else
741         break;
742     }
743 lose:;
744   get_frame_saved_regs (frame_info)[SP_REGNUM] = (frame_info)->frame + 8;
745   get_frame_saved_regs (frame_info)[FP_REGNUM] = (frame_info)->frame;
746   get_frame_saved_regs (frame_info)[PC_REGNUM] = (frame_info)->frame + 4;
747 #ifdef SIG_SP_FP_OFFSET
748   /* Adjust saved SP_REGNUM for fake _sigtramp frames.  */
749   if ((get_frame_type (frame_info) == SIGTRAMP_FRAME) && frame_info->next)
750     frame_info->saved_regs[SP_REGNUM] =
751       frame_info->next->frame + SIG_SP_FP_OFFSET;
752 #endif
753 }
754
755
756 #ifdef USE_PROC_FS              /* Target dependent support for /proc */
757
758 #include <sys/procfs.h>
759
760 /* Prototypes for supply_gregset etc. */
761 #include "gregset.h"
762
763 /*  The /proc interface divides the target machine's register set up into
764    two different sets, the general register set (gregset) and the floating
765    point register set (fpregset).  For each set, there is an ioctl to get
766    the current register set and another ioctl to set the current values.
767
768    The actual structure passed through the ioctl interface is, of course,
769    naturally machine dependent, and is different for each set of registers.
770    For the m68k for example, the general register set is typically defined
771    by:
772
773    typedef int gregset_t[18];
774
775    #define      R_D0    0
776    ...
777    #define      R_PS    17
778
779    and the floating point set by:
780
781    typedef      struct fpregset {
782    int  f_pcr;
783    int  f_psr;
784    int  f_fpiaddr;
785    int  f_fpregs[8][3];         (8 regs, 96 bits each)
786    } fpregset_t;
787
788    These routines provide the packing and unpacking of gregset_t and
789    fpregset_t formatted data.
790
791  */
792
793 /* Atari SVR4 has R_SR but not R_PS */
794
795 #if !defined (R_PS) && defined (R_SR)
796 #define R_PS R_SR
797 #endif
798
799 /*  Given a pointer to a general register set in /proc format (gregset_t *),
800    unpack the register contents and supply them as gdb's idea of the current
801    register values. */
802
803 void
804 supply_gregset (gregset_t *gregsetp)
805 {
806   register int regi;
807   register greg_t *regp = (greg_t *) gregsetp;
808
809   for (regi = 0; regi < R_PC; regi++)
810     {
811       supply_register (regi, (char *) (regp + regi));
812     }
813   supply_register (PS_REGNUM, (char *) (regp + R_PS));
814   supply_register (PC_REGNUM, (char *) (regp + R_PC));
815 }
816
817 void
818 fill_gregset (gregset_t *gregsetp, int regno)
819 {
820   register int regi;
821   register greg_t *regp = (greg_t *) gregsetp;
822
823   for (regi = 0; regi < R_PC; regi++)
824     {
825       if ((regno == -1) || (regno == regi))
826         {
827           *(regp + regi) = *(int *) &deprecated_registers[REGISTER_BYTE (regi)];
828         }
829     }
830   if ((regno == -1) || (regno == PS_REGNUM))
831     {
832       *(regp + R_PS) = *(int *) &deprecated_registers[REGISTER_BYTE (PS_REGNUM)];
833     }
834   if ((regno == -1) || (regno == PC_REGNUM))
835     {
836       *(regp + R_PC) = *(int *) &deprecated_registers[REGISTER_BYTE (PC_REGNUM)];
837     }
838 }
839
840 #if defined (FP0_REGNUM)
841
842 /*  Given a pointer to a floating point register set in /proc format
843    (fpregset_t *), unpack the register contents and supply them as gdb's
844    idea of the current floating point register values. */
845
846 void
847 supply_fpregset (fpregset_t *fpregsetp)
848 {
849   register int regi;
850   char *from;
851
852   for (regi = FP0_REGNUM; regi < M68K_FPC_REGNUM; regi++)
853     {
854       from = (char *) &(fpregsetp->f_fpregs[regi - FP0_REGNUM][0]);
855       supply_register (regi, from);
856     }
857   supply_register (M68K_FPC_REGNUM, (char *) &(fpregsetp->f_pcr));
858   supply_register (M68K_FPS_REGNUM, (char *) &(fpregsetp->f_psr));
859   supply_register (M68K_FPI_REGNUM, (char *) &(fpregsetp->f_fpiaddr));
860 }
861
862 /*  Given a pointer to a floating point register set in /proc format
863    (fpregset_t *), update the register specified by REGNO from gdb's idea
864    of the current floating point register set.  If REGNO is -1, update
865    them all. */
866
867 void
868 fill_fpregset (fpregset_t *fpregsetp, int regno)
869 {
870   int regi;
871   char *to;
872   char *from;
873
874   for (regi = FP0_REGNUM; regi < M68K_FPC_REGNUM; regi++)
875     {
876       if ((regno == -1) || (regno == regi))
877         {
878           from = (char *) &deprecated_registers[REGISTER_BYTE (regi)];
879           to = (char *) &(fpregsetp->f_fpregs[regi - FP0_REGNUM][0]);
880           memcpy (to, from, REGISTER_RAW_SIZE (regi));
881         }
882     }
883   if ((regno == -1) || (regno == M68K_FPC_REGNUM))
884     {
885       fpregsetp->f_pcr = *(int *) &deprecated_registers[REGISTER_BYTE (M68K_FPC_REGNUM)];
886     }
887   if ((regno == -1) || (regno == M68K_FPS_REGNUM))
888     {
889       fpregsetp->f_psr = *(int *) &deprecated_registers[REGISTER_BYTE (M68K_FPS_REGNUM)];
890     }
891   if ((regno == -1) || (regno == M68K_FPI_REGNUM))
892     {
893       fpregsetp->f_fpiaddr = *(int *) &deprecated_registers[REGISTER_BYTE (M68K_FPI_REGNUM)];
894     }
895 }
896
897 #endif /* defined (FP0_REGNUM) */
898
899 #endif /* USE_PROC_FS */
900
901 /* Figure out where the longjmp will land.  Slurp the args out of the stack.
902    We expect the first arg to be a pointer to the jmp_buf structure from which
903    we extract the pc (JB_PC) that we will land at.  The pc is copied into PC.
904    This routine returns true on success. */
905
906 /* NOTE: cagney/2000-11-08: For this function to be fully multi-arched
907    the macro's JB_PC and JB_ELEMENT_SIZE would need to be moved into
908    the ``struct gdbarch_tdep'' object and then set on a target ISA/ABI
909    dependant basis. */
910
911 int
912 m68k_get_longjmp_target (CORE_ADDR *pc)
913 {
914 #if defined (JB_PC) && defined (JB_ELEMENT_SIZE)
915   char *buf;
916   CORE_ADDR sp, jb_addr;
917
918   buf = alloca (TARGET_PTR_BIT / TARGET_CHAR_BIT);
919   sp = read_register (SP_REGNUM);
920
921   if (target_read_memory (sp + SP_ARG0, /* Offset of first arg on stack */
922                           buf, TARGET_PTR_BIT / TARGET_CHAR_BIT))
923     return 0;
924
925   jb_addr = extract_address (buf, TARGET_PTR_BIT / TARGET_CHAR_BIT);
926
927   if (target_read_memory (jb_addr + JB_PC * JB_ELEMENT_SIZE, buf,
928                           TARGET_PTR_BIT / TARGET_CHAR_BIT))
929     return 0;
930
931   *pc = extract_address (buf, TARGET_PTR_BIT / TARGET_CHAR_BIT);
932
933   return 1;
934 #else
935   internal_error (__FILE__, __LINE__,
936                   "m68k_get_longjmp_target: not implemented");
937   return 0;
938 #endif
939 }
940
941 /* Immediately after a function call, return the saved pc before the frame
942    is setup.  For sun3's, we check for the common case of being inside of a
943    system call, and if so, we know that Sun pushes the call # on the stack
944    prior to doing the trap. */
945
946 CORE_ADDR
947 m68k_saved_pc_after_call (struct frame_info *frame)
948 {
949 #ifdef SYSCALL_TRAP
950   int op;
951
952   op = read_memory_unsigned_integer (frame->pc - SYSCALL_TRAP_OFFSET, 2);
953
954   if (op == SYSCALL_TRAP)
955     return read_memory_unsigned_integer (read_register (SP_REGNUM) + 4, 4);
956   else
957 #endif /* SYSCALL_TRAP */
958     return read_memory_unsigned_integer (read_register (SP_REGNUM), 4);
959 }
960
961 /* Function: m68k_gdbarch_init
962    Initializer function for the m68k gdbarch vector.
963    Called by gdbarch.  Sets up the gdbarch vector(s) for this target. */
964
965 static struct gdbarch *
966 m68k_gdbarch_init (struct gdbarch_info info, struct gdbarch_list *arches)
967 {
968   static LONGEST call_dummy_words[7] = { 0xf227e0ff, 0x48e7fffc, 0x426742e7,
969     0x4eb93232, 0x3232dffc, 0x69696969,
970     (0x4e404e71 | (BPT_VECTOR << 16))
971   };
972   struct gdbarch_tdep *tdep = NULL;
973   struct gdbarch *gdbarch;
974
975   /* find a candidate among the list of pre-declared architectures. */
976   arches = gdbarch_list_lookup_by_info (arches, &info);
977   if (arches != NULL)
978     return (arches->gdbarch);
979
980 #if 0
981   tdep = (struct gdbarch_tdep *) xmalloc (sizeof (struct gdbarch_tdep));
982 #endif
983  
984   gdbarch = gdbarch_alloc (&info, 0);
985
986   /* NOTE: cagney/2002-12-06: This can be deleted when this arch is
987      ready to unwind the PC first (see frame.c:get_prev_frame()).  */
988   set_gdbarch_deprecated_init_frame_pc (gdbarch, init_frame_pc_default);
989
990   set_gdbarch_long_double_format (gdbarch, &floatformat_m68881_ext);
991   set_gdbarch_long_double_bit (gdbarch, 96);
992
993   set_gdbarch_function_start_offset (gdbarch, 0);
994
995   set_gdbarch_skip_prologue (gdbarch, m68k_skip_prologue);
996   set_gdbarch_saved_pc_after_call (gdbarch, m68k_saved_pc_after_call);
997   set_gdbarch_breakpoint_from_pc (gdbarch, m68k_local_breakpoint_from_pc);
998
999   /* Stack grows down. */
1000   set_gdbarch_inner_than (gdbarch, core_addr_lessthan);
1001   set_gdbarch_stack_align (gdbarch, m68k_stack_align);
1002
1003
1004   set_gdbarch_believe_pcc_promotion (gdbarch, 1);
1005   set_gdbarch_decr_pc_after_break (gdbarch, 2);
1006
1007   set_gdbarch_store_struct_return (gdbarch, m68k_store_struct_return);
1008   set_gdbarch_deprecated_extract_return_value (gdbarch,
1009                                                m68k_deprecated_extract_return_value);
1010   set_gdbarch_deprecated_store_return_value (gdbarch, m68k_store_return_value);
1011
1012   set_gdbarch_frame_chain (gdbarch, m68k_frame_chain);
1013   set_gdbarch_frame_chain_valid (gdbarch, generic_func_frame_chain_valid);
1014   set_gdbarch_frame_saved_pc (gdbarch, m68k_frame_saved_pc);
1015   set_gdbarch_frame_init_saved_regs (gdbarch, m68k_frame_init_saved_regs);
1016   set_gdbarch_frameless_function_invocation (gdbarch,
1017                                              m68k_frameless_function_invocation);
1018   /* OK to default this value to 'unknown'. */
1019   set_gdbarch_frame_num_args (gdbarch, frame_num_args_unknown);
1020   set_gdbarch_frame_args_skip (gdbarch, 8);
1021
1022   set_gdbarch_register_raw_size (gdbarch, m68k_register_raw_size);
1023   set_gdbarch_register_virtual_size (gdbarch, m68k_register_virtual_size);
1024   set_gdbarch_max_register_raw_size (gdbarch, 12);
1025   set_gdbarch_max_register_virtual_size (gdbarch, 12);
1026   set_gdbarch_register_virtual_type (gdbarch, m68k_register_virtual_type);
1027   set_gdbarch_register_name (gdbarch, m68k_register_name);
1028   set_gdbarch_register_size (gdbarch, 4);
1029   set_gdbarch_register_byte (gdbarch, m68k_register_byte);
1030   set_gdbarch_num_regs (gdbarch, 29);
1031   set_gdbarch_register_bytes_ok (gdbarch, m68k_register_bytes_ok);
1032   set_gdbarch_register_bytes (gdbarch, (16 * 4 + 8 + 8 * 12 + 3 * 4));
1033   set_gdbarch_sp_regnum (gdbarch, M68K_SP_REGNUM);
1034   set_gdbarch_fp_regnum (gdbarch, M68K_FP_REGNUM);
1035   set_gdbarch_pc_regnum (gdbarch, M68K_PC_REGNUM);
1036   set_gdbarch_ps_regnum (gdbarch, M68K_PS_REGNUM);
1037   set_gdbarch_fp0_regnum (gdbarch, M68K_FP0_REGNUM);
1038
1039   set_gdbarch_deprecated_use_generic_dummy_frames (gdbarch, 0);
1040   set_gdbarch_call_dummy_location (gdbarch, ON_STACK);
1041   set_gdbarch_call_dummy_breakpoint_offset_p (gdbarch, 1);
1042   set_gdbarch_call_dummy_breakpoint_offset (gdbarch, 24);
1043   set_gdbarch_deprecated_pc_in_call_dummy (gdbarch, deprecated_pc_in_call_dummy_on_stack);
1044   set_gdbarch_call_dummy_p (gdbarch, 1);
1045   set_gdbarch_call_dummy_stack_adjust_p (gdbarch, 0);
1046   set_gdbarch_call_dummy_length (gdbarch, 28);
1047   set_gdbarch_call_dummy_start_offset (gdbarch, 12);
1048
1049   set_gdbarch_call_dummy_words (gdbarch, call_dummy_words);
1050   set_gdbarch_sizeof_call_dummy_words (gdbarch, sizeof (call_dummy_words));
1051   set_gdbarch_call_dummy_stack_adjust_p (gdbarch, 0);
1052   set_gdbarch_fix_call_dummy (gdbarch, m68k_fix_call_dummy);
1053   set_gdbarch_push_dummy_frame (gdbarch, m68k_push_dummy_frame);
1054   set_gdbarch_pop_frame (gdbarch, m68k_pop_frame);
1055
1056   return gdbarch;
1057 }
1058
1059
1060 static void
1061 m68k_dump_tdep (struct gdbarch *current_gdbarch, struct ui_file *file)
1062 {
1063
1064 }
1065
1066 void
1067 _initialize_m68k_tdep (void)
1068 {
1069   gdbarch_register (bfd_arch_m68k, m68k_gdbarch_init, m68k_dump_tdep);
1070   tm_print_insn = print_insn_m68k;
1071 }