OSDN Git Service

2003-05-13 Andrew Cagney <cagney@redhat.com>
[pf3gnuchains/pf3gnuchains4x.git] / gdb / i386-tdep.c
1 /* Intel 386 target-dependent stuff.
2
3    Copyright 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996,
4    1997, 1998, 1999, 2000, 2001, 2002, 2003 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 "gdb_string.h"
25 #include "frame.h"
26 #include "inferior.h"
27 #include "gdbcore.h"
28 #include "objfiles.h"
29 #include "target.h"
30 #include "floatformat.h"
31 #include "symfile.h"
32 #include "symtab.h"
33 #include "gdbcmd.h"
34 #include "command.h"
35 #include "arch-utils.h"
36 #include "regcache.h"
37 #include "doublest.h"
38 #include "value.h"
39 #include "gdb_assert.h"
40 #include "reggroups.h"
41 #include "dummy-frame.h"
42 #include "osabi.h"
43
44 #include "i386-tdep.h"
45 #include "i387-tdep.h"
46
47 /* Names of the registers.  The first 10 registers match the register
48    numbering scheme used by GCC for stabs and DWARF.  */
49
50 static char *i386_register_names[] =
51 {
52   "eax",   "ecx",    "edx",   "ebx",
53   "esp",   "ebp",    "esi",   "edi",
54   "eip",   "eflags", "cs",    "ss",
55   "ds",    "es",     "fs",    "gs",
56   "st0",   "st1",    "st2",   "st3",
57   "st4",   "st5",    "st6",   "st7",
58   "fctrl", "fstat",  "ftag",  "fiseg",
59   "fioff", "foseg",  "fooff", "fop",
60   "xmm0",  "xmm1",   "xmm2",  "xmm3",
61   "xmm4",  "xmm5",   "xmm6",  "xmm7",
62   "mxcsr"
63 };
64
65 static const int i386_num_register_names =
66   (sizeof (i386_register_names) / sizeof (*i386_register_names));
67
68 /* MMX registers.  */
69
70 static char *i386_mmx_names[] =
71 {
72   "mm0", "mm1", "mm2", "mm3",
73   "mm4", "mm5", "mm6", "mm7"
74 };
75
76 static const int i386_num_mmx_regs =
77   (sizeof (i386_mmx_names) / sizeof (i386_mmx_names[0]));
78
79 #define MM0_REGNUM NUM_REGS
80
81 static int
82 i386_mmx_regnum_p (int regnum)
83 {
84   return (regnum >= MM0_REGNUM
85           && regnum < MM0_REGNUM + i386_num_mmx_regs);
86 }
87
88 /* FP register?  */
89
90 int
91 i386_fp_regnum_p (int regnum)
92 {
93   return (regnum < NUM_REGS
94           && (FP0_REGNUM && FP0_REGNUM <= regnum && regnum < FPC_REGNUM));
95 }
96
97 int
98 i386_fpc_regnum_p (int regnum)
99 {
100   return (regnum < NUM_REGS
101           && (FPC_REGNUM <= regnum && regnum < XMM0_REGNUM));
102 }
103
104 /* SSE register?  */
105
106 int
107 i386_sse_regnum_p (int regnum)
108 {
109   return (regnum < NUM_REGS
110           && (XMM0_REGNUM <= regnum && regnum < MXCSR_REGNUM));
111 }
112
113 int
114 i386_mxcsr_regnum_p (int regnum)
115 {
116   return (regnum < NUM_REGS
117           && regnum == MXCSR_REGNUM);
118 }
119
120 /* Return the name of register REG.  */
121
122 const char *
123 i386_register_name (int reg)
124 {
125   if (reg >= 0 && reg < i386_num_register_names)
126     return i386_register_names[reg];
127
128   if (i386_mmx_regnum_p (reg))
129     return i386_mmx_names[reg - MM0_REGNUM];
130
131   return NULL;
132 }
133
134 /* Convert stabs register number REG to the appropriate register
135    number used by GDB.  */
136
137 static int
138 i386_stab_reg_to_regnum (int reg)
139 {
140   /* This implements what GCC calls the "default" register map.  */
141   if (reg >= 0 && reg <= 7)
142     {
143       /* General registers.  */
144       return reg;
145     }
146   else if (reg >= 12 && reg <= 19)
147     {
148       /* Floating-point registers.  */
149       return reg - 12 + FP0_REGNUM;
150     }
151   else if (reg >= 21 && reg <= 28)
152     {
153       /* SSE registers.  */
154       return reg - 21 + XMM0_REGNUM;
155     }
156   else if (reg >= 29 && reg <= 36)
157     {
158       /* MMX registers.  */
159       return reg - 29 + MM0_REGNUM;
160     }
161
162   /* This will hopefully provoke a warning.  */
163   return NUM_REGS + NUM_PSEUDO_REGS;
164 }
165
166 /* Convert DWARF register number REG to the appropriate register
167    number used by GDB.  */
168
169 static int
170 i386_dwarf_reg_to_regnum (int reg)
171 {
172   /* The DWARF register numbering includes %eip and %eflags, and
173      numbers the floating point registers differently.  */
174   if (reg >= 0 && reg <= 9)
175     {
176       /* General registers.  */
177       return reg;
178     }
179   else if (reg >= 11 && reg <= 18)
180     {
181       /* Floating-point registers.  */
182       return reg - 11 + FP0_REGNUM;
183     }
184   else if (reg >= 21)
185     {
186       /* The SSE and MMX registers have identical numbers as in stabs.  */
187       return i386_stab_reg_to_regnum (reg);
188     }
189
190   /* This will hopefully provoke a warning.  */
191   return NUM_REGS + NUM_PSEUDO_REGS;
192 }
193 \f
194
195 /* This is the variable that is set with "set disassembly-flavor", and
196    its legitimate values.  */
197 static const char att_flavor[] = "att";
198 static const char intel_flavor[] = "intel";
199 static const char *valid_flavors[] =
200 {
201   att_flavor,
202   intel_flavor,
203   NULL
204 };
205 static const char *disassembly_flavor = att_flavor;
206
207 /* Stdio style buffering was used to minimize calls to ptrace, but
208    this buffering did not take into account that the code section
209    being accessed may not be an even number of buffers long (even if
210    the buffer is only sizeof(int) long).  In cases where the code
211    section size happened to be a non-integral number of buffers long,
212    attempting to read the last buffer would fail.  Simply using
213    target_read_memory and ignoring errors, rather than read_memory, is
214    not the correct solution, since legitimate access errors would then
215    be totally ignored.  To properly handle this situation and continue
216    to use buffering would require that this code be able to determine
217    the minimum code section size granularity (not the alignment of the
218    section itself, since the actual failing case that pointed out this
219    problem had a section alignment of 4 but was not a multiple of 4
220    bytes long), on a target by target basis, and then adjust it's
221    buffer size accordingly.  This is messy, but potentially feasible.
222    It probably needs the bfd library's help and support.  For now, the
223    buffer size is set to 1.  (FIXME -fnf) */
224
225 #define CODESTREAM_BUFSIZ 1     /* Was sizeof(int), see note above.  */
226 static CORE_ADDR codestream_next_addr;
227 static CORE_ADDR codestream_addr;
228 static unsigned char codestream_buf[CODESTREAM_BUFSIZ];
229 static int codestream_off;
230 static int codestream_cnt;
231
232 #define codestream_tell() (codestream_addr + codestream_off)
233 #define codestream_peek() \
234   (codestream_cnt == 0 ? \
235    codestream_fill(1) : codestream_buf[codestream_off])
236 #define codestream_get() \
237   (codestream_cnt-- == 0 ? \
238    codestream_fill(0) : codestream_buf[codestream_off++])
239
240 static unsigned char
241 codestream_fill (int peek_flag)
242 {
243   codestream_addr = codestream_next_addr;
244   codestream_next_addr += CODESTREAM_BUFSIZ;
245   codestream_off = 0;
246   codestream_cnt = CODESTREAM_BUFSIZ;
247   read_memory (codestream_addr, (char *) codestream_buf, CODESTREAM_BUFSIZ);
248
249   if (peek_flag)
250     return (codestream_peek ());
251   else
252     return (codestream_get ());
253 }
254
255 static void
256 codestream_seek (CORE_ADDR place)
257 {
258   codestream_next_addr = place / CODESTREAM_BUFSIZ;
259   codestream_next_addr *= CODESTREAM_BUFSIZ;
260   codestream_cnt = 0;
261   codestream_fill (1);
262   while (codestream_tell () != place)
263     codestream_get ();
264 }
265
266 static void
267 codestream_read (unsigned char *buf, int count)
268 {
269   unsigned char *p;
270   int i;
271   p = buf;
272   for (i = 0; i < count; i++)
273     *p++ = codestream_get ();
274 }
275 \f
276
277 /* If the next instruction is a jump, move to its target.  */
278
279 static void
280 i386_follow_jump (void)
281 {
282   unsigned char buf[4];
283   long delta;
284
285   int data16;
286   CORE_ADDR pos;
287
288   pos = codestream_tell ();
289
290   data16 = 0;
291   if (codestream_peek () == 0x66)
292     {
293       codestream_get ();
294       data16 = 1;
295     }
296
297   switch (codestream_get ())
298     {
299     case 0xe9:
300       /* Relative jump: if data16 == 0, disp32, else disp16.  */
301       if (data16)
302         {
303           codestream_read (buf, 2);
304           delta = extract_signed_integer (buf, 2);
305
306           /* Include the size of the jmp instruction (including the
307              0x66 prefix).  */
308           pos += delta + 4;
309         }
310       else
311         {
312           codestream_read (buf, 4);
313           delta = extract_signed_integer (buf, 4);
314
315           pos += delta + 5;
316         }
317       break;
318     case 0xeb:
319       /* Relative jump, disp8 (ignore data16).  */
320       codestream_read (buf, 1);
321       /* Sign-extend it.  */
322       delta = extract_signed_integer (buf, 1);
323
324       pos += delta + 2;
325       break;
326     }
327   codestream_seek (pos);
328 }
329
330 /* Find & return the amount a local space allocated, and advance the
331    codestream to the first register push (if any).
332
333    If the entry sequence doesn't make sense, return -1, and leave
334    codestream pointer at a random spot.  */
335
336 static long
337 i386_get_frame_setup (CORE_ADDR pc)
338 {
339   unsigned char op;
340
341   codestream_seek (pc);
342
343   i386_follow_jump ();
344
345   op = codestream_get ();
346
347   if (op == 0x58)               /* popl %eax */
348     {
349       /* This function must start with
350
351             popl %eax             0x58
352             xchgl %eax, (%esp)    0x87 0x04 0x24
353          or xchgl %eax, 0(%esp)   0x87 0x44 0x24 0x00
354
355          (the System V compiler puts out the second `xchg'
356          instruction, and the assembler doesn't try to optimize it, so
357          the 'sib' form gets generated).  This sequence is used to get
358          the address of the return buffer for a function that returns
359          a structure.  */
360       int pos;
361       unsigned char buf[4];
362       static unsigned char proto1[3] = { 0x87, 0x04, 0x24 };
363       static unsigned char proto2[4] = { 0x87, 0x44, 0x24, 0x00 };
364
365       pos = codestream_tell ();
366       codestream_read (buf, 4);
367       if (memcmp (buf, proto1, 3) == 0)
368         pos += 3;
369       else if (memcmp (buf, proto2, 4) == 0)
370         pos += 4;
371
372       codestream_seek (pos);
373       op = codestream_get ();   /* Update next opcode.  */
374     }
375
376   if (op == 0x68 || op == 0x6a)
377     {
378       /* This function may start with
379
380             pushl constant
381             call _probe
382             addl $4, %esp
383            
384          followed by
385
386             pushl %ebp
387
388          etc.  */
389       int pos;
390       unsigned char buf[8];
391
392       /* Skip past the `pushl' instruction; it has either a one-byte 
393          or a four-byte operand, depending on the opcode.  */
394       pos = codestream_tell ();
395       if (op == 0x68)
396         pos += 4;
397       else
398         pos += 1;
399       codestream_seek (pos);
400
401       /* Read the following 8 bytes, which should be "call _probe" (6
402          bytes) followed by "addl $4,%esp" (2 bytes).  */
403       codestream_read (buf, sizeof (buf));
404       if (buf[0] == 0xe8 && buf[6] == 0xc4 && buf[7] == 0x4)
405         pos += sizeof (buf);
406       codestream_seek (pos);
407       op = codestream_get ();   /* Update next opcode.  */
408     }
409
410   if (op == 0x55)               /* pushl %ebp */
411     {
412       /* Check for "movl %esp, %ebp" -- can be written in two ways.  */
413       switch (codestream_get ())
414         {
415         case 0x8b:
416           if (codestream_get () != 0xec)
417             return -1;
418           break;
419         case 0x89:
420           if (codestream_get () != 0xe5)
421             return -1;
422           break;
423         default:
424           return -1;
425         }
426       /* Check for stack adjustment 
427
428            subl $XXX, %esp
429
430          NOTE: You can't subtract a 16 bit immediate from a 32 bit
431          reg, so we don't have to worry about a data16 prefix.  */
432       op = codestream_peek ();
433       if (op == 0x83)
434         {
435           /* `subl' with 8 bit immediate.  */
436           codestream_get ();
437           if (codestream_get () != 0xec)
438             /* Some instruction starting with 0x83 other than `subl'.  */
439             {
440               codestream_seek (codestream_tell () - 2);
441               return 0;
442             }
443           /* `subl' with signed byte immediate (though it wouldn't
444              make sense to be negative).  */
445           return (codestream_get ());
446         }
447       else if (op == 0x81)
448         {
449           char buf[4];
450           /* Maybe it is `subl' with a 32 bit immedediate.  */
451           codestream_get ();
452           if (codestream_get () != 0xec)
453             /* Some instruction starting with 0x81 other than `subl'.  */
454             {
455               codestream_seek (codestream_tell () - 2);
456               return 0;
457             }
458           /* It is `subl' with a 32 bit immediate.  */
459           codestream_read ((unsigned char *) buf, 4);
460           return extract_signed_integer (buf, 4);
461         }
462       else
463         {
464           return 0;
465         }
466     }
467   else if (op == 0xc8)
468     {
469       char buf[2];
470       /* `enter' with 16 bit unsigned immediate.  */
471       codestream_read ((unsigned char *) buf, 2);
472       codestream_get ();        /* Flush final byte of enter instruction.  */
473       return extract_unsigned_integer (buf, 2);
474     }
475   return (-1);
476 }
477
478 /* Signal trampolines don't have a meaningful frame.  The frame
479    pointer value we use is actually the frame pointer of the calling
480    frame -- that is, the frame which was in progress when the signal
481    trampoline was entered.  GDB mostly treats this frame pointer value
482    as a magic cookie.  We detect the case of a signal trampoline by
483    testing for get_frame_type() == SIGTRAMP_FRAME, which is set based
484    on PC_IN_SIGTRAMP.
485
486    When a signal trampoline is invoked from a frameless function, we
487    essentially have two frameless functions in a row.  In this case,
488    we use the same magic cookie for three frames in a row.  We detect
489    this case by seeing whether the next frame is a SIGTRAMP_FRAME,
490    and, if it does, checking whether the current frame is actually
491    frameless.  In this case, we need to get the PC by looking at the
492    SP register value stored in the signal context.
493
494    This should work in most cases except in horrible situations where
495    a signal occurs just as we enter a function but before the frame
496    has been set up.  Incidentally, that's just what happens when we
497    call a function from GDB with a signal pending (there's a test in
498    the testsuite that makes this happen).  Therefore we pretend that
499    we have a frameless function if we're stopped at the start of a
500    function.  */
501
502 /* Return non-zero if we're dealing with a frameless signal, that is,
503    a signal trampoline invoked from a frameless function.  */
504
505 int
506 i386_frameless_signal_p (struct frame_info *frame)
507 {
508   return (get_next_frame (frame)
509           && get_frame_type (get_next_frame (frame)) == SIGTRAMP_FRAME
510           && (frameless_look_for_prologue (frame)
511               || get_frame_pc (frame) == get_frame_func (frame)));
512 }
513
514 /* Return the chain-pointer for FRAME.  In the case of the i386, the
515    frame's nominal address is the address of a 4-byte word containing
516    the calling frame's address.  */
517
518 static CORE_ADDR
519 i386_frame_chain (struct frame_info *frame)
520 {
521   if (pc_in_dummy_frame (get_frame_pc (frame)))
522     return get_frame_base (frame);
523
524   if (get_frame_type (frame) == SIGTRAMP_FRAME
525       || i386_frameless_signal_p (frame))
526     return get_frame_base (frame);
527
528   if (! inside_entry_file (get_frame_pc (frame)))
529     return read_memory_unsigned_integer (get_frame_base (frame), 4);
530
531   return 0;
532 }
533
534 /* Determine whether the function invocation represented by FRAME does
535    not have a from on the stack associated with it.  If it does not,
536    return non-zero, otherwise return zero.  */
537
538 static int
539 i386_frameless_function_invocation (struct frame_info *frame)
540 {
541   if (get_frame_type (frame) == SIGTRAMP_FRAME)
542     return 0;
543
544   return frameless_look_for_prologue (frame);
545 }
546
547 /* Assuming FRAME is for a sigtramp routine, return the saved program
548    counter.  */
549
550 static CORE_ADDR
551 i386_sigtramp_saved_pc (struct frame_info *frame)
552 {
553   struct gdbarch_tdep *tdep = gdbarch_tdep (current_gdbarch);
554   CORE_ADDR addr;
555
556   addr = tdep->sigcontext_addr (frame);
557   return read_memory_unsigned_integer (addr + tdep->sc_pc_offset, 4);
558 }
559
560 /* Assuming FRAME is for a sigtramp routine, return the saved stack
561    pointer.  */
562
563 static CORE_ADDR
564 i386_sigtramp_saved_sp (struct frame_info *frame)
565 {
566   struct gdbarch_tdep *tdep = gdbarch_tdep (current_gdbarch);
567   CORE_ADDR addr;
568
569   addr = tdep->sigcontext_addr (frame);
570   return read_memory_unsigned_integer (addr + tdep->sc_sp_offset, 4);
571 }
572
573 /* Return the saved program counter for FRAME.  */
574
575 static CORE_ADDR
576 i386_frame_saved_pc (struct frame_info *frame)
577 {
578   if (pc_in_dummy_frame (get_frame_pc (frame)))
579     {
580       ULONGEST pc;
581
582       frame_unwind_unsigned_register (frame, PC_REGNUM, &pc);
583       return pc;
584     }
585
586   if (get_frame_type (frame) == SIGTRAMP_FRAME)
587     return i386_sigtramp_saved_pc (frame);
588
589   if (i386_frameless_signal_p (frame))
590     {
591       CORE_ADDR sp = i386_sigtramp_saved_sp (get_next_frame (frame));
592       return read_memory_unsigned_integer (sp, 4);
593     }
594
595   return read_memory_unsigned_integer (get_frame_base (frame) + 4, 4);
596 }
597
598 /* Immediately after a function call, return the saved pc.  */
599
600 static CORE_ADDR
601 i386_saved_pc_after_call (struct frame_info *frame)
602 {
603   if (get_frame_type (frame) == SIGTRAMP_FRAME)
604     return i386_sigtramp_saved_pc (frame);
605
606   return read_memory_unsigned_integer (read_register (SP_REGNUM), 4);
607 }
608
609 /* Parse the first few instructions the function to see what registers
610    were stored.
611    
612    We handle these cases:
613
614    The startup sequence can be at the start of the function, or the
615    function can start with a branch to startup code at the end.
616
617    %ebp can be set up with either the 'enter' instruction, or "pushl
618    %ebp, movl %esp, %ebp" (`enter' is too slow to be useful, but was
619    once used in the System V compiler).
620
621    Local space is allocated just below the saved %ebp by either the
622    'enter' instruction, or by "subl $<size>, %esp".  'enter' has a 16
623    bit unsigned argument for space to allocate, and the 'addl'
624    instruction could have either a signed byte, or 32 bit immediate.
625
626    Next, the registers used by this function are pushed.  With the
627    System V compiler they will always be in the order: %edi, %esi,
628    %ebx (and sometimes a harmless bug causes it to also save but not
629    restore %eax); however, the code below is willing to see the pushes
630    in any order, and will handle up to 8 of them.
631  
632    If the setup sequence is at the end of the function, then the next
633    instruction will be a branch back to the start.  */
634
635 static void
636 i386_frame_init_saved_regs (struct frame_info *fip)
637 {
638   long locals = -1;
639   unsigned char op;
640   CORE_ADDR addr;
641   CORE_ADDR pc;
642   int i;
643
644   if (get_frame_saved_regs (fip))
645     return;
646
647   frame_saved_regs_zalloc (fip);
648
649   pc = get_frame_func (fip);
650   if (pc != 0)
651     locals = i386_get_frame_setup (pc);
652
653   if (locals >= 0)
654     {
655       addr = get_frame_base (fip) - 4 - locals;
656       for (i = 0; i < 8; i++)
657         {
658           op = codestream_get ();
659           if (op < 0x50 || op > 0x57)
660             break;
661 #ifdef I386_REGNO_TO_SYMMETRY
662           /* Dynix uses different internal numbering.  Ick.  */
663           get_frame_saved_regs (fip)[I386_REGNO_TO_SYMMETRY (op - 0x50)] = addr;
664 #else
665           get_frame_saved_regs (fip)[op - 0x50] = addr;
666 #endif
667           addr -= 4;
668         }
669     }
670
671   get_frame_saved_regs (fip)[PC_REGNUM] = get_frame_base (fip) + 4;
672   get_frame_saved_regs (fip)[DEPRECATED_FP_REGNUM] = get_frame_base (fip);
673 }
674
675 /* Return PC of first real instruction.  */
676
677 static CORE_ADDR
678 i386_skip_prologue (CORE_ADDR pc)
679 {
680   unsigned char op;
681   int i;
682   static unsigned char pic_pat[6] =
683   { 0xe8, 0, 0, 0, 0,           /* call   0x0 */
684     0x5b,                       /* popl   %ebx */
685   };
686   CORE_ADDR pos;
687
688   if (i386_get_frame_setup (pc) < 0)
689     return (pc);
690
691   /* Found valid frame setup -- codestream now points to start of push
692      instructions for saving registers.  */
693
694   /* Skip over register saves.  */
695   for (i = 0; i < 8; i++)
696     {
697       op = codestream_peek ();
698       /* Break if not `pushl' instrunction.  */
699       if (op < 0x50 || op > 0x57)
700         break;
701       codestream_get ();
702     }
703
704   /* The native cc on SVR4 in -K PIC mode inserts the following code
705      to get the address of the global offset table (GOT) into register
706      %ebx
707      
708         call    0x0
709         popl    %ebx
710         movl    %ebx,x(%ebp)    (optional)
711         addl    y,%ebx
712
713      This code is with the rest of the prologue (at the end of the
714      function), so we have to skip it to get to the first real
715      instruction at the start of the function.  */
716
717   pos = codestream_tell ();
718   for (i = 0; i < 6; i++)
719     {
720       op = codestream_get ();
721       if (pic_pat[i] != op)
722         break;
723     }
724   if (i == 6)
725     {
726       unsigned char buf[4];
727       long delta = 6;
728
729       op = codestream_get ();
730       if (op == 0x89)           /* movl %ebx, x(%ebp) */
731         {
732           op = codestream_get ();
733           if (op == 0x5d)       /* One byte offset from %ebp.  */
734             {
735               delta += 3;
736               codestream_read (buf, 1);
737             }
738           else if (op == 0x9d)  /* Four byte offset from %ebp.  */
739             {
740               delta += 6;
741               codestream_read (buf, 4);
742             }
743           else                  /* Unexpected instruction.  */
744             delta = -1;
745           op = codestream_get ();
746         }
747       /* addl y,%ebx */
748       if (delta > 0 && op == 0x81 && codestream_get () == 0xc3)
749         {
750           pos += delta + 6;
751         }
752     }
753   codestream_seek (pos);
754
755   i386_follow_jump ();
756
757   return (codestream_tell ());
758 }
759
760 /* Use the program counter to determine the contents and size of a
761    breakpoint instruction.  Return a pointer to a string of bytes that
762    encode a breakpoint instruction, store the length of the string in
763    *LEN and optionally adjust *PC to point to the correct memory
764    location for inserting the breakpoint.
765
766    On the i386 we have a single breakpoint that fits in a single byte
767    and can be inserted anywhere.  */
768    
769 static const unsigned char *
770 i386_breakpoint_from_pc (CORE_ADDR *pc, int *len)
771 {
772   static unsigned char break_insn[] = { 0xcc }; /* int 3 */
773   
774   *len = sizeof (break_insn);
775   return break_insn;
776 }
777
778 /* Push the return address (pointing to the call dummy) onto the stack
779    and return the new value for the stack pointer.  */
780
781 static CORE_ADDR
782 i386_push_return_address (CORE_ADDR pc, CORE_ADDR sp)
783 {
784   char buf[4];
785
786   store_unsigned_integer (buf, 4, CALL_DUMMY_ADDRESS ());
787   write_memory (sp - 4, buf, 4);
788   return sp - 4;
789 }
790
791 static void
792 i386_do_pop_frame (struct frame_info *frame)
793 {
794   CORE_ADDR fp;
795   int regnum;
796   char regbuf[I386_MAX_REGISTER_SIZE];
797
798   fp = get_frame_base (frame);
799   i386_frame_init_saved_regs (frame);
800
801   for (regnum = 0; regnum < NUM_REGS; regnum++)
802     {
803       CORE_ADDR addr;
804       addr = get_frame_saved_regs (frame)[regnum];
805       if (addr)
806         {
807           read_memory (addr, regbuf, REGISTER_RAW_SIZE (regnum));
808           deprecated_write_register_gen (regnum, regbuf);
809         }
810     }
811   write_register (DEPRECATED_FP_REGNUM, read_memory_integer (fp, 4));
812   write_register (PC_REGNUM, read_memory_integer (fp + 4, 4));
813   write_register (SP_REGNUM, fp + 8);
814   flush_cached_frames ();
815 }
816
817 static void
818 i386_pop_frame (void)
819 {
820   generic_pop_current_frame (i386_do_pop_frame);
821 }
822 \f
823
824 /* Figure out where the longjmp will land.  Slurp the args out of the
825    stack.  We expect the first arg to be a pointer to the jmp_buf
826    structure from which we extract the address that we will land at.
827    This address is copied into PC.  This routine returns non-zero on
828    success.  */
829
830 static int
831 i386_get_longjmp_target (CORE_ADDR *pc)
832 {
833   char buf[8];
834   CORE_ADDR sp, jb_addr;
835   int jb_pc_offset = gdbarch_tdep (current_gdbarch)->jb_pc_offset;
836   int len = TYPE_LENGTH (builtin_type_void_func_ptr);
837
838   /* If JB_PC_OFFSET is -1, we have no way to find out where the
839      longjmp will land.  */
840   if (jb_pc_offset == -1)
841     return 0;
842
843   sp = read_register (SP_REGNUM);
844   if (target_read_memory (sp + len, buf, len))
845     return 0;
846
847   jb_addr = extract_typed_address (buf, builtin_type_void_func_ptr);
848   if (target_read_memory (jb_addr + jb_pc_offset, buf, len))
849     return 0;
850
851   *pc = extract_typed_address (buf, builtin_type_void_func_ptr);
852   return 1;
853 }
854 \f
855
856 static CORE_ADDR
857 i386_push_arguments (int nargs, struct value **args, CORE_ADDR sp,
858                      int struct_return, CORE_ADDR struct_addr)
859 {
860   sp = legacy_push_arguments (nargs, args, sp, struct_return, struct_addr);
861   
862   if (struct_return)
863     {
864       char buf[4];
865
866       sp -= 4;
867       store_unsigned_integer (buf, 4, struct_addr);
868       write_memory (sp, buf, 4);
869     }
870
871   return sp;
872 }
873
874 /* These registers are used for returning integers (and on some
875    targets also for returning `struct' and `union' values when their
876    size and alignment match an integer type).  */
877 #define LOW_RETURN_REGNUM 0     /* %eax */
878 #define HIGH_RETURN_REGNUM 2    /* %edx */
879
880 /* Extract from an array REGBUF containing the (raw) register state, a
881    function return value of TYPE, and copy that, in virtual format,
882    into VALBUF.  */
883
884 static void
885 i386_extract_return_value (struct type *type, struct regcache *regcache,
886                            void *dst)
887 {
888   bfd_byte *valbuf = dst;
889   int len = TYPE_LENGTH (type);
890   char buf[I386_MAX_REGISTER_SIZE];
891
892   if (TYPE_CODE (type) == TYPE_CODE_STRUCT
893       && TYPE_NFIELDS (type) == 1)
894     {
895       i386_extract_return_value (TYPE_FIELD_TYPE (type, 0), regcache, valbuf);
896       return;
897     }
898
899   if (TYPE_CODE (type) == TYPE_CODE_FLT)
900     {
901       if (FP0_REGNUM < 0)
902         {
903           warning ("Cannot find floating-point return value.");
904           memset (valbuf, 0, len);
905           return;
906         }
907
908       /* Floating-point return values can be found in %st(0).  Convert
909          its contents to the desired type.  This is probably not
910          exactly how it would happen on the target itself, but it is
911          the best we can do.  */
912       regcache_raw_read (regcache, FP0_REGNUM, buf);
913       convert_typed_floating (buf, builtin_type_i387_ext, valbuf, type);
914     }
915   else
916     {
917       int low_size = REGISTER_RAW_SIZE (LOW_RETURN_REGNUM);
918       int high_size = REGISTER_RAW_SIZE (HIGH_RETURN_REGNUM);
919
920       if (len <= low_size)
921         {
922           regcache_raw_read (regcache, LOW_RETURN_REGNUM, buf);
923           memcpy (valbuf, buf, len);
924         }
925       else if (len <= (low_size + high_size))
926         {
927           regcache_raw_read (regcache, LOW_RETURN_REGNUM, buf);
928           memcpy (valbuf, buf, low_size);
929           regcache_raw_read (regcache, HIGH_RETURN_REGNUM, buf);
930           memcpy (valbuf + low_size, buf, len - low_size);
931         }
932       else
933         internal_error (__FILE__, __LINE__,
934                         "Cannot extract return value of %d bytes long.", len);
935     }
936 }
937
938 /* Write into the appropriate registers a function return value stored
939    in VALBUF of type TYPE, given in virtual format.  */
940
941 static void
942 i386_store_return_value (struct type *type, struct regcache *regcache,
943                          const void *valbuf)
944 {
945   int len = TYPE_LENGTH (type);
946
947   if (TYPE_CODE (type) == TYPE_CODE_STRUCT
948       && TYPE_NFIELDS (type) == 1)
949     {
950       i386_store_return_value (TYPE_FIELD_TYPE (type, 0), regcache, valbuf);
951       return;
952     }
953
954   if (TYPE_CODE (type) == TYPE_CODE_FLT)
955     {
956       ULONGEST fstat;
957       char buf[FPU_REG_RAW_SIZE];
958
959       if (FP0_REGNUM < 0)
960         {
961           warning ("Cannot set floating-point return value.");
962           return;
963         }
964
965       /* Returning floating-point values is a bit tricky.  Apart from
966          storing the return value in %st(0), we have to simulate the
967          state of the FPU at function return point.  */
968
969       /* Convert the value found in VALBUF to the extended
970          floating-point format used by the FPU.  This is probably
971          not exactly how it would happen on the target itself, but
972          it is the best we can do.  */
973       convert_typed_floating (valbuf, type, buf, builtin_type_i387_ext);
974       regcache_raw_write (regcache, FP0_REGNUM, buf);
975
976       /* Set the top of the floating-point register stack to 7.  The
977          actual value doesn't really matter, but 7 is what a normal
978          function return would end up with if the program started out
979          with a freshly initialized FPU.  */
980       regcache_raw_read_unsigned (regcache, FSTAT_REGNUM, &fstat);
981       fstat |= (7 << 11);
982       regcache_raw_write_unsigned (regcache, FSTAT_REGNUM, fstat);
983
984       /* Mark %st(1) through %st(7) as empty.  Since we set the top of
985          the floating-point register stack to 7, the appropriate value
986          for the tag word is 0x3fff.  */
987       regcache_raw_write_unsigned (regcache, FTAG_REGNUM, 0x3fff);
988     }
989   else
990     {
991       int low_size = REGISTER_RAW_SIZE (LOW_RETURN_REGNUM);
992       int high_size = REGISTER_RAW_SIZE (HIGH_RETURN_REGNUM);
993
994       if (len <= low_size)
995         regcache_raw_write_part (regcache, LOW_RETURN_REGNUM, 0, len, valbuf);
996       else if (len <= (low_size + high_size))
997         {
998           regcache_raw_write (regcache, LOW_RETURN_REGNUM, valbuf);
999           regcache_raw_write_part (regcache, HIGH_RETURN_REGNUM, 0,
1000                                    len - low_size, (char *) valbuf + low_size);
1001         }
1002       else
1003         internal_error (__FILE__, __LINE__,
1004                         "Cannot store return value of %d bytes long.", len);
1005     }
1006 }
1007
1008 /* Extract from REGCACHE, which contains the (raw) register state, the
1009    address in which a function should return its structure value, as a
1010    CORE_ADDR.  */
1011
1012 static CORE_ADDR
1013 i386_extract_struct_value_address (struct regcache *regcache)
1014 {
1015   ULONGEST addr;
1016
1017   regcache_raw_read_unsigned (regcache, LOW_RETURN_REGNUM, &addr);
1018   return addr;
1019 }
1020 \f
1021
1022 /* This is the variable that is set with "set struct-convention", and
1023    its legitimate values.  */
1024 static const char default_struct_convention[] = "default";
1025 static const char pcc_struct_convention[] = "pcc";
1026 static const char reg_struct_convention[] = "reg";
1027 static const char *valid_conventions[] =
1028 {
1029   default_struct_convention,
1030   pcc_struct_convention,
1031   reg_struct_convention,
1032   NULL
1033 };
1034 static const char *struct_convention = default_struct_convention;
1035
1036 static int
1037 i386_use_struct_convention (int gcc_p, struct type *type)
1038 {
1039   enum struct_return struct_return;
1040
1041   if (struct_convention == default_struct_convention)
1042     struct_return = gdbarch_tdep (current_gdbarch)->struct_return;
1043   else if (struct_convention == pcc_struct_convention)
1044     struct_return = pcc_struct_return;
1045   else
1046     struct_return = reg_struct_return;
1047
1048   return generic_use_struct_convention (struct_return == reg_struct_return,
1049                                         type);
1050 }
1051 \f
1052
1053 /* Return the GDB type object for the "standard" data type of data in
1054    register REGNUM.  Perhaps %esi and %edi should go here, but
1055    potentially they could be used for things other than address.  */
1056
1057 static struct type *
1058 i386_register_type (struct gdbarch *gdbarch, int regnum)
1059 {
1060   if (regnum == PC_REGNUM || regnum == DEPRECATED_FP_REGNUM || regnum == SP_REGNUM)
1061     return lookup_pointer_type (builtin_type_void);
1062
1063   if (i386_fp_regnum_p (regnum))
1064     return builtin_type_i387_ext;
1065
1066   if (i386_sse_regnum_p (regnum))
1067     return builtin_type_vec128i;
1068
1069   if (i386_mmx_regnum_p (regnum))
1070     return builtin_type_vec64i;
1071
1072   return builtin_type_int;
1073 }
1074
1075 /* Map a cooked register onto a raw register or memory.  For the i386,
1076    the MMX registers need to be mapped onto floating-point registers.  */
1077
1078 static int
1079 i386_mmx_regnum_to_fp_regnum (struct regcache *regcache, int regnum)
1080 {
1081   int mmxi;
1082   ULONGEST fstat;
1083   int tos;
1084   int fpi;
1085
1086   mmxi = regnum - MM0_REGNUM;
1087   regcache_raw_read_unsigned (regcache, FSTAT_REGNUM, &fstat);
1088   tos = (fstat >> 11) & 0x7;
1089   fpi = (mmxi + tos) % 8;
1090
1091   return (FP0_REGNUM + fpi);
1092 }
1093
1094 static void
1095 i386_pseudo_register_read (struct gdbarch *gdbarch, struct regcache *regcache,
1096                            int regnum, void *buf)
1097 {
1098   if (i386_mmx_regnum_p (regnum))
1099     {
1100       char mmx_buf[MAX_REGISTER_SIZE];
1101       int fpnum = i386_mmx_regnum_to_fp_regnum (regcache, regnum);
1102
1103       /* Extract (always little endian).  */
1104       regcache_raw_read (regcache, fpnum, mmx_buf);
1105       memcpy (buf, mmx_buf, REGISTER_RAW_SIZE (regnum));
1106     }
1107   else
1108     regcache_raw_read (regcache, regnum, buf);
1109 }
1110
1111 static void
1112 i386_pseudo_register_write (struct gdbarch *gdbarch, struct regcache *regcache,
1113                             int regnum, const void *buf)
1114 {
1115   if (i386_mmx_regnum_p (regnum))
1116     {
1117       char mmx_buf[MAX_REGISTER_SIZE];
1118       int fpnum = i386_mmx_regnum_to_fp_regnum (regcache, regnum);
1119
1120       /* Read ...  */
1121       regcache_raw_read (regcache, fpnum, mmx_buf);
1122       /* ... Modify ... (always little endian).  */
1123       memcpy (mmx_buf, buf, REGISTER_RAW_SIZE (regnum));
1124       /* ... Write.  */
1125       regcache_raw_write (regcache, fpnum, mmx_buf);
1126     }
1127   else
1128     regcache_raw_write (regcache, regnum, buf);
1129 }
1130
1131 /* Return true iff register REGNUM's virtual format is different from
1132    its raw format.  Note that this definition assumes that the host
1133    supports IEEE 32-bit floats, since it doesn't say that SSE
1134    registers need conversion.  Even if we can't find a counterexample,
1135    this is still sloppy.  */
1136
1137 static int
1138 i386_register_convertible (int regnum)
1139 {
1140   return i386_fp_regnum_p (regnum);
1141 }
1142
1143 /* Convert data from raw format for register REGNUM in buffer FROM to
1144    virtual format with type TYPE in buffer TO.  */
1145
1146 static void
1147 i386_register_convert_to_virtual (int regnum, struct type *type,
1148                                   char *from, char *to)
1149 {
1150   gdb_assert (i386_fp_regnum_p (regnum));
1151
1152   /* We only support floating-point values.  */
1153   if (TYPE_CODE (type) != TYPE_CODE_FLT)
1154     {
1155       warning ("Cannot convert floating-point register value "
1156                "to non-floating-point type.");
1157       memset (to, 0, TYPE_LENGTH (type));
1158       return;
1159     }
1160
1161   /* Convert to TYPE.  This should be a no-op if TYPE is equivalent to
1162      the extended floating-point format used by the FPU.  */
1163   convert_typed_floating (from, builtin_type_i387_ext, to, type);
1164 }
1165
1166 /* Convert data from virtual format with type TYPE in buffer FROM to
1167    raw format for register REGNUM in buffer TO.  */
1168
1169 static void
1170 i386_register_convert_to_raw (struct type *type, int regnum,
1171                               char *from, char *to)
1172 {
1173   gdb_assert (i386_fp_regnum_p (regnum));
1174
1175   /* We only support floating-point values.  */
1176   if (TYPE_CODE (type) != TYPE_CODE_FLT)
1177     {
1178       warning ("Cannot convert non-floating-point type "
1179                "to floating-point register value.");
1180       memset (to, 0, TYPE_LENGTH (type));
1181       return;
1182     }
1183
1184   /* Convert from TYPE.  This should be a no-op if TYPE is equivalent
1185      to the extended floating-point format used by the FPU.  */
1186   convert_typed_floating (from, type, to, builtin_type_i387_ext);
1187 }
1188 \f     
1189
1190 #ifdef STATIC_TRANSFORM_NAME
1191 /* SunPRO encodes the static variables.  This is not related to C++
1192    mangling, it is done for C too.  */
1193
1194 char *
1195 sunpro_static_transform_name (char *name)
1196 {
1197   char *p;
1198   if (IS_STATIC_TRANSFORM_NAME (name))
1199     {
1200       /* For file-local statics there will be a period, a bunch of
1201          junk (the contents of which match a string given in the
1202          N_OPT), a period and the name.  For function-local statics
1203          there will be a bunch of junk (which seems to change the
1204          second character from 'A' to 'B'), a period, the name of the
1205          function, and the name.  So just skip everything before the
1206          last period.  */
1207       p = strrchr (name, '.');
1208       if (p != NULL)
1209         name = p + 1;
1210     }
1211   return name;
1212 }
1213 #endif /* STATIC_TRANSFORM_NAME */
1214 \f
1215
1216 /* Stuff for WIN32 PE style DLL's but is pretty generic really.  */
1217
1218 CORE_ADDR
1219 i386_pe_skip_trampoline_code (CORE_ADDR pc, char *name)
1220 {
1221   if (pc && read_memory_unsigned_integer (pc, 2) == 0x25ff) /* jmp *(dest) */
1222     {
1223       unsigned long indirect = read_memory_unsigned_integer (pc + 2, 4);
1224       struct minimal_symbol *indsym =
1225         indirect ? lookup_minimal_symbol_by_pc (indirect) : 0;
1226       char *symname = indsym ? SYMBOL_LINKAGE_NAME (indsym) : 0;
1227
1228       if (symname)
1229         {
1230           if (strncmp (symname, "__imp_", 6) == 0
1231               || strncmp (symname, "_imp_", 5) == 0)
1232             return name ? 1 : read_memory_unsigned_integer (indirect, 4);
1233         }
1234     }
1235   return 0;                     /* Not a trampoline.  */
1236 }
1237 \f
1238
1239 /* Return non-zero if PC and NAME show that we are in a signal
1240    trampoline.  */
1241
1242 static int
1243 i386_pc_in_sigtramp (CORE_ADDR pc, char *name)
1244 {
1245   return (name && strcmp ("_sigtramp", name) == 0);
1246 }
1247 \f
1248
1249 /* We have two flavours of disassembly.  The machinery on this page
1250    deals with switching between those.  */
1251
1252 static int
1253 i386_print_insn (bfd_vma pc, disassemble_info *info)
1254 {
1255   gdb_assert (disassembly_flavor == att_flavor
1256               || disassembly_flavor == intel_flavor);
1257
1258   /* FIXME: kettenis/20020915: Until disassembler_options is properly
1259      constified, cast to prevent a compiler warning.  */
1260   info->disassembler_options = (char *) disassembly_flavor;
1261   info->mach = gdbarch_bfd_arch_info (current_gdbarch)->mach;
1262
1263   return print_insn_i386 (pc, info);
1264 }
1265 \f
1266
1267 /* There are a few i386 architecture variants that differ only
1268    slightly from the generic i386 target.  For now, we don't give them
1269    their own source file, but include them here.  As a consequence,
1270    they'll always be included.  */
1271
1272 /* System V Release 4 (SVR4).  */
1273
1274 static int
1275 i386_svr4_pc_in_sigtramp (CORE_ADDR pc, char *name)
1276 {
1277   return (name && (strcmp ("_sigreturn", name) == 0
1278                    || strcmp ("_sigacthandler", name) == 0
1279                    || strcmp ("sigvechandler", name) == 0));
1280 }
1281
1282 /* Get address of the pushed ucontext (sigcontext) on the stack for
1283    all three variants of SVR4 sigtramps.  */
1284
1285 static CORE_ADDR
1286 i386_svr4_sigcontext_addr (struct frame_info *frame)
1287 {
1288   int sigcontext_offset = -1;
1289   char *name = NULL;
1290
1291   find_pc_partial_function (get_frame_pc (frame), &name, NULL, NULL);
1292   if (name)
1293     {
1294       if (strcmp (name, "_sigreturn") == 0)
1295         sigcontext_offset = 132;
1296       else if (strcmp (name, "_sigacthandler") == 0)
1297         sigcontext_offset = 80;
1298       else if (strcmp (name, "sigvechandler") == 0)
1299         sigcontext_offset = 120;
1300     }
1301
1302   gdb_assert (sigcontext_offset != -1);
1303
1304   if (get_next_frame (frame))
1305     return get_frame_base (get_next_frame (frame)) + sigcontext_offset;
1306   return read_register (SP_REGNUM) + sigcontext_offset;
1307 }
1308 \f
1309
1310 /* DJGPP.  */
1311
1312 static int
1313 i386_go32_pc_in_sigtramp (CORE_ADDR pc, char *name)
1314 {
1315   /* DJGPP doesn't have any special frames for signal handlers.  */
1316   return 0;
1317 }
1318 \f
1319
1320 /* Generic ELF.  */
1321
1322 void
1323 i386_elf_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch)
1324 {
1325   /* We typically use stabs-in-ELF with the DWARF register numbering.  */
1326   set_gdbarch_stab_reg_to_regnum (gdbarch, i386_dwarf_reg_to_regnum);
1327 }
1328
1329 /* System V Release 4 (SVR4).  */
1330
1331 void
1332 i386_svr4_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch)
1333 {
1334   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
1335
1336   /* System V Release 4 uses ELF.  */
1337   i386_elf_init_abi (info, gdbarch);
1338
1339   /* System V Release 4 has shared libraries.  */
1340   set_gdbarch_in_solib_call_trampoline (gdbarch, in_plt_section);
1341   set_gdbarch_skip_trampoline_code (gdbarch, find_solib_trampoline_target);
1342
1343   set_gdbarch_pc_in_sigtramp (gdbarch, i386_svr4_pc_in_sigtramp);
1344   tdep->sigcontext_addr = i386_svr4_sigcontext_addr;
1345   tdep->sc_pc_offset = 14 * 4;
1346   tdep->sc_sp_offset = 7 * 4;
1347
1348   tdep->jb_pc_offset = 20;
1349 }
1350
1351 /* DJGPP.  */
1352
1353 static void
1354 i386_go32_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch)
1355 {
1356   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
1357
1358   set_gdbarch_pc_in_sigtramp (gdbarch, i386_go32_pc_in_sigtramp);
1359
1360   tdep->jb_pc_offset = 36;
1361 }
1362
1363 /* NetWare.  */
1364
1365 static void
1366 i386_nw_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch)
1367 {
1368   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
1369
1370   tdep->jb_pc_offset = 24;
1371 }
1372 \f
1373
1374 /* i386 register groups.  In addition to the normal groups, add "mmx"
1375    and "sse".  */
1376
1377 static struct reggroup *i386_sse_reggroup;
1378 static struct reggroup *i386_mmx_reggroup;
1379
1380 static void
1381 i386_init_reggroups (void)
1382 {
1383   i386_sse_reggroup = reggroup_new ("sse", USER_REGGROUP);
1384   i386_mmx_reggroup = reggroup_new ("mmx", USER_REGGROUP);
1385 }
1386
1387 static void
1388 i386_add_reggroups (struct gdbarch *gdbarch)
1389 {
1390   reggroup_add (gdbarch, i386_sse_reggroup);
1391   reggroup_add (gdbarch, i386_mmx_reggroup);
1392   reggroup_add (gdbarch, general_reggroup);
1393   reggroup_add (gdbarch, float_reggroup);
1394   reggroup_add (gdbarch, all_reggroup);
1395   reggroup_add (gdbarch, save_reggroup);
1396   reggroup_add (gdbarch, restore_reggroup);
1397   reggroup_add (gdbarch, vector_reggroup);
1398   reggroup_add (gdbarch, system_reggroup);
1399 }
1400
1401 int
1402 i386_register_reggroup_p (struct gdbarch *gdbarch, int regnum,
1403                           struct reggroup *group)
1404 {
1405   int sse_regnum_p = (i386_sse_regnum_p (regnum)
1406                       || i386_mxcsr_regnum_p (regnum));
1407   int fp_regnum_p = (i386_fp_regnum_p (regnum)
1408                      || i386_fpc_regnum_p (regnum));
1409   int mmx_regnum_p = (i386_mmx_regnum_p (regnum));
1410   if (group == i386_mmx_reggroup)
1411     return mmx_regnum_p;
1412   if (group == i386_sse_reggroup)
1413     return sse_regnum_p;
1414   if (group == vector_reggroup)
1415     return (mmx_regnum_p || sse_regnum_p);
1416   if (group == float_reggroup)
1417     return fp_regnum_p;
1418   if (group == general_reggroup)
1419     return (!fp_regnum_p && !mmx_regnum_p && !sse_regnum_p);
1420   return default_register_reggroup_p (gdbarch, regnum, group);
1421 }
1422
1423 \f
1424 static struct gdbarch *
1425 i386_gdbarch_init (struct gdbarch_info info, struct gdbarch_list *arches)
1426 {
1427   struct gdbarch_tdep *tdep;
1428   struct gdbarch *gdbarch;
1429
1430   /* If there is already a candidate, use it.  */
1431   arches = gdbarch_list_lookup_by_info (arches, &info);
1432   if (arches != NULL)
1433     return arches->gdbarch;
1434
1435   /* Allocate space for the new architecture.  */
1436   tdep = XMALLOC (struct gdbarch_tdep);
1437   gdbarch = gdbarch_alloc (&info, tdep);
1438
1439   /* NOTE: cagney/2002-12-06: This can be deleted when this arch is
1440      ready to unwind the PC first (see frame.c:get_prev_frame()).  */
1441   set_gdbarch_deprecated_init_frame_pc (gdbarch, init_frame_pc_default);
1442
1443   /* The i386 default settings don't include the SSE registers.
1444      FIXME: kettenis/20020614: They do include the FPU registers for
1445      now, which probably is not quite right.  */
1446   tdep->num_xmm_regs = 0;
1447
1448   tdep->jb_pc_offset = -1;
1449   tdep->struct_return = pcc_struct_return;
1450   tdep->sigtramp_start = 0;
1451   tdep->sigtramp_end = 0;
1452   tdep->sigcontext_addr = NULL;
1453   tdep->sc_pc_offset = -1;
1454   tdep->sc_sp_offset = -1;
1455
1456   /* The format used for `long double' on almost all i386 targets is
1457      the i387 extended floating-point format.  In fact, of all targets
1458      in the GCC 2.95 tree, only OSF/1 does it different, and insists
1459      on having a `long double' that's not `long' at all.  */
1460   set_gdbarch_long_double_format (gdbarch, &floatformat_i387_ext);
1461
1462   /* Although the i387 extended floating-point has only 80 significant
1463      bits, a `long double' actually takes up 96, probably to enforce
1464      alignment.  */
1465   set_gdbarch_long_double_bit (gdbarch, 96);
1466
1467   /* NOTE: tm-i386aix.h, tm-i386bsd.h, tm-i386os9k.h, tm-ptx.h,
1468      tm-symmetry.h currently override this.  Sigh.  */
1469   set_gdbarch_num_regs (gdbarch, I386_NUM_GREGS + I386_NUM_FREGS);
1470
1471   set_gdbarch_sp_regnum (gdbarch, 4); /* %esp */
1472   set_gdbarch_deprecated_fp_regnum (gdbarch, 5); /* %ebp */
1473   set_gdbarch_pc_regnum (gdbarch, 8); /* %eip */
1474   set_gdbarch_ps_regnum (gdbarch, 9); /* %eflags */
1475   set_gdbarch_fp0_regnum (gdbarch, 16); /* %st(0) */
1476
1477   /* Use the "default" register numbering scheme for stabs and COFF.  */
1478   set_gdbarch_stab_reg_to_regnum (gdbarch, i386_stab_reg_to_regnum);
1479   set_gdbarch_sdb_reg_to_regnum (gdbarch, i386_stab_reg_to_regnum);
1480
1481   /* Use the DWARF register numbering scheme for DWARF and DWARF 2.  */
1482   set_gdbarch_dwarf_reg_to_regnum (gdbarch, i386_dwarf_reg_to_regnum);
1483   set_gdbarch_dwarf2_reg_to_regnum (gdbarch, i386_dwarf_reg_to_regnum);
1484
1485   /* We don't define ECOFF_REG_TO_REGNUM, since ECOFF doesn't seem to
1486      be in use on any of the supported i386 targets.  */
1487
1488   set_gdbarch_register_name (gdbarch, i386_register_name);
1489   set_gdbarch_deprecated_register_size (gdbarch, 4);
1490   set_gdbarch_register_bytes (gdbarch, I386_SIZEOF_GREGS + I386_SIZEOF_FREGS);
1491   set_gdbarch_register_type (gdbarch, i386_register_type);
1492
1493   set_gdbarch_print_float_info (gdbarch, i387_print_float_info);
1494
1495   set_gdbarch_get_longjmp_target (gdbarch, i386_get_longjmp_target);
1496
1497   /* Call dummy code.  */
1498   set_gdbarch_deprecated_call_dummy_words (gdbarch, NULL);
1499   set_gdbarch_deprecated_sizeof_call_dummy_words (gdbarch, 0);
1500
1501   set_gdbarch_register_convertible (gdbarch, i386_register_convertible);
1502   set_gdbarch_register_convert_to_virtual (gdbarch,
1503                                            i386_register_convert_to_virtual);
1504   set_gdbarch_register_convert_to_raw (gdbarch, i386_register_convert_to_raw);
1505
1506   /* "An argument's size is increased, if necessary, to make it a
1507      multiple of [32-bit] words.  This may require tail padding,
1508      depending on the size of the argument" -- from the x86 ABI.  */
1509   set_gdbarch_parm_boundary (gdbarch, 32);
1510
1511   set_gdbarch_extract_return_value (gdbarch, i386_extract_return_value);
1512   set_gdbarch_deprecated_push_arguments (gdbarch, i386_push_arguments);
1513   set_gdbarch_deprecated_push_return_address (gdbarch, i386_push_return_address);
1514   set_gdbarch_deprecated_pop_frame (gdbarch, i386_pop_frame);
1515   set_gdbarch_store_return_value (gdbarch, i386_store_return_value);
1516   set_gdbarch_extract_struct_value_address (gdbarch,
1517                                             i386_extract_struct_value_address);
1518   set_gdbarch_use_struct_convention (gdbarch, i386_use_struct_convention);
1519
1520   set_gdbarch_deprecated_frame_init_saved_regs (gdbarch, i386_frame_init_saved_regs);
1521   set_gdbarch_skip_prologue (gdbarch, i386_skip_prologue);
1522
1523   /* Stack grows downward.  */
1524   set_gdbarch_inner_than (gdbarch, core_addr_lessthan);
1525
1526   set_gdbarch_breakpoint_from_pc (gdbarch, i386_breakpoint_from_pc);
1527   set_gdbarch_decr_pc_after_break (gdbarch, 1);
1528   set_gdbarch_function_start_offset (gdbarch, 0);
1529
1530   /* The following redefines make backtracing through sigtramp work.
1531      They manufacture a fake sigtramp frame and obtain the saved pc in
1532      sigtramp from the sigcontext structure which is pushed by the
1533      kernel on the user stack, along with a pointer to it.  */
1534
1535   set_gdbarch_frame_args_skip (gdbarch, 8);
1536   set_gdbarch_frameless_function_invocation (gdbarch,
1537                                            i386_frameless_function_invocation);
1538   set_gdbarch_deprecated_frame_chain (gdbarch, i386_frame_chain);
1539   set_gdbarch_deprecated_frame_saved_pc (gdbarch, i386_frame_saved_pc);
1540   set_gdbarch_deprecated_saved_pc_after_call (gdbarch, i386_saved_pc_after_call);
1541   set_gdbarch_frame_num_args (gdbarch, frame_num_args_unknown);
1542   set_gdbarch_pc_in_sigtramp (gdbarch, i386_pc_in_sigtramp);
1543
1544   /* Wire in the MMX registers.  */
1545   set_gdbarch_num_pseudo_regs (gdbarch, i386_num_mmx_regs);
1546   set_gdbarch_pseudo_register_read (gdbarch, i386_pseudo_register_read);
1547   set_gdbarch_pseudo_register_write (gdbarch, i386_pseudo_register_write);
1548
1549   set_gdbarch_print_insn (gdbarch, i386_print_insn);
1550
1551   /* Add the i386 register groups.  */
1552   i386_add_reggroups (gdbarch);
1553   set_gdbarch_register_reggroup_p (gdbarch, i386_register_reggroup_p);
1554
1555   /* Should be using push_dummy_call.  */
1556   set_gdbarch_deprecated_dummy_write_sp (gdbarch, generic_target_write_sp);
1557
1558   /* Hook in ABI-specific overrides, if they have been registered.  */
1559   gdbarch_init_osabi (info, gdbarch);
1560
1561   return gdbarch;
1562 }
1563
1564 static enum gdb_osabi
1565 i386_coff_osabi_sniffer (bfd *abfd)
1566 {
1567   if (strcmp (bfd_get_target (abfd), "coff-go32-exe") == 0
1568       || strcmp (bfd_get_target (abfd), "coff-go32") == 0)
1569     return GDB_OSABI_GO32;
1570
1571   return GDB_OSABI_UNKNOWN;
1572 }
1573
1574 static enum gdb_osabi
1575 i386_nlm_osabi_sniffer (bfd *abfd)
1576 {
1577   return GDB_OSABI_NETWARE;
1578 }
1579 \f
1580
1581 /* Provide a prototype to silence -Wmissing-prototypes.  */
1582 void _initialize_i386_tdep (void);
1583
1584 void
1585 _initialize_i386_tdep (void)
1586 {
1587   register_gdbarch_init (bfd_arch_i386, i386_gdbarch_init);
1588
1589   /* Add the variable that controls the disassembly flavor.  */
1590   {
1591     struct cmd_list_element *new_cmd;
1592
1593     new_cmd = add_set_enum_cmd ("disassembly-flavor", no_class,
1594                                 valid_flavors,
1595                                 &disassembly_flavor,
1596                                 "\
1597 Set the disassembly flavor, the valid values are \"att\" and \"intel\", \
1598 and the default value is \"att\".",
1599                                 &setlist);
1600     add_show_from_set (new_cmd, &showlist);
1601   }
1602
1603   /* Add the variable that controls the convention for returning
1604      structs.  */
1605   {
1606     struct cmd_list_element *new_cmd;
1607
1608     new_cmd = add_set_enum_cmd ("struct-convention", no_class,
1609                                 valid_conventions,
1610                                 &struct_convention, "\
1611 Set the convention for returning small structs, valid values \
1612 are \"default\", \"pcc\" and \"reg\", and the default value is \"default\".",
1613                                 &setlist);
1614     add_show_from_set (new_cmd, &showlist);
1615   }
1616
1617   gdbarch_register_osabi_sniffer (bfd_arch_i386, bfd_target_coff_flavour,
1618                                   i386_coff_osabi_sniffer);
1619   gdbarch_register_osabi_sniffer (bfd_arch_i386, bfd_target_nlm_flavour,
1620                                   i386_nlm_osabi_sniffer);
1621
1622   gdbarch_register_osabi (bfd_arch_i386, 0, GDB_OSABI_SVR4,
1623                           i386_svr4_init_abi);
1624   gdbarch_register_osabi (bfd_arch_i386, 0, GDB_OSABI_GO32,
1625                           i386_go32_init_abi);
1626   gdbarch_register_osabi (bfd_arch_i386, 0, GDB_OSABI_NETWARE,
1627                           i386_nw_init_abi);
1628
1629   /* Initialize the i386 specific register groups.  */
1630   i386_init_reggroups ();
1631 }