OSDN Git Service

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