2004-02-09 Andrew Cagney <cagney@redhat.com>
[external/binutils.git] / gdb / s390-tdep.c
1 /* Target-dependent code for GDB, the GNU debugger.
2
3    Copyright 2001, 2002, 2003, 2004 Free Software Foundation, Inc.
4
5    Contributed by D.J. Barrow (djbarrow@de.ibm.com,barrow_dj@yahoo.com)
6    for IBM Deutschland Entwicklung GmbH, IBM Corporation.
7
8    This file is part of GDB.
9
10    This program is free software; you can redistribute it and/or modify
11    it under the terms of the GNU General Public License as published by
12    the Free Software Foundation; either version 2 of the License, or
13    (at your option) any later version.
14
15    This program is distributed in the hope that it will be useful,
16    but WITHOUT ANY WARRANTY; without even the implied warranty of
17    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18    GNU General Public License for more details.
19
20    You should have received a copy of the GNU General Public License
21    along with this program; if not, write to the Free Software
22    Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
23    02111-1307, USA.  */
24
25 #define S390_TDEP               /* for special macros in tm-s390.h */
26 #include <defs.h>
27 #include "arch-utils.h"
28 #include "frame.h"
29 #include "inferior.h"
30 #include "symtab.h"
31 #include "target.h"
32 #include "gdbcore.h"
33 #include "gdbcmd.h"
34 #include "objfiles.h"
35 #include "tm.h"
36 #include "../bfd/bfd.h"
37 #include "floatformat.h"
38 #include "regcache.h"
39 #include "value.h"
40 #include "gdb_assert.h"
41 #include "dis-asm.h"
42
43
44
45 /* Number of bytes of storage in the actual machine representation
46    for register N.  */
47 static int
48 s390_register_raw_size (int reg_nr)
49 {
50   if (S390_FP0_REGNUM <= reg_nr
51       && reg_nr < S390_FP0_REGNUM + S390_NUM_FPRS)
52     return S390_FPR_SIZE;
53   else
54     return 4;
55 }
56
57 static int
58 s390x_register_raw_size (int reg_nr)
59 {
60   return (reg_nr == S390_FPC_REGNUM)
61     || (reg_nr >= S390_FIRST_ACR && reg_nr <= S390_LAST_ACR) ? 4 : 8;
62 }
63
64 static int
65 s390_cannot_fetch_register (int regno)
66 {
67   return (regno >= S390_FIRST_CR && regno < (S390_FIRST_CR + 9)) ||
68     (regno >= (S390_FIRST_CR + 12) && regno <= S390_LAST_CR);
69 }
70
71 static int
72 s390_register_byte (int reg_nr)
73 {
74   if (reg_nr <= S390_GP_LAST_REGNUM)
75     return reg_nr * S390_GPR_SIZE;
76   if (reg_nr <= S390_LAST_ACR)
77     return S390_ACR0_OFFSET + (((reg_nr) - S390_FIRST_ACR) * S390_ACR_SIZE);
78   if (reg_nr <= S390_LAST_CR)
79     return S390_CR0_OFFSET + (((reg_nr) - S390_FIRST_CR) * S390_CR_SIZE);
80   if (reg_nr == S390_FPC_REGNUM)
81     return S390_FPC_OFFSET;
82   else
83     return S390_FP0_OFFSET + (((reg_nr) - S390_FP0_REGNUM) * S390_FPR_SIZE);
84 }
85
86 #define S390_MAX_INSTR_SIZE (6)
87 #define S390_SYSCALL_OPCODE (0x0a)
88 #define S390_SYSCALL_SIZE   (2)
89 #define S390_SIGCONTEXT_SREGS_OFFSET (8)
90 #define S390X_SIGCONTEXT_SREGS_OFFSET (8)
91 #define S390_SIGREGS_FP0_OFFSET       (144)
92 #define S390X_SIGREGS_FP0_OFFSET      (216)
93 #define S390_UC_MCONTEXT_OFFSET    (256)
94 #define S390X_UC_MCONTEXT_OFFSET   (344)
95 #define S390_STACK_FRAME_OVERHEAD  16*DEPRECATED_REGISTER_SIZE+32
96 #define S390_STACK_PARAMETER_ALIGNMENT  DEPRECATED_REGISTER_SIZE
97 #define S390_NUM_FP_PARAMETER_REGISTERS (GDB_TARGET_IS_ESAME ? 4:2)
98 #define S390_SIGNAL_FRAMESIZE  (GDB_TARGET_IS_ESAME ? 160:96)
99 #define s390_NR_sigreturn          119
100 #define s390_NR_rt_sigreturn       173
101
102
103
104 struct frame_extra_info
105 {
106   int initialised;
107   int good_prologue;
108   CORE_ADDR function_start;
109   CORE_ADDR skip_prologue_function_start;
110   CORE_ADDR saved_pc_valid;
111   CORE_ADDR saved_pc;
112   CORE_ADDR sig_fixed_saved_pc_valid;
113   CORE_ADDR sig_fixed_saved_pc;
114   CORE_ADDR frame_pointer_saved_pc;     /* frame pointer needed for alloca */
115   CORE_ADDR stack_bought_valid;
116   CORE_ADDR stack_bought;       /* amount we decrement the stack pointer by */
117   CORE_ADDR sigcontext;
118 };
119
120
121 static CORE_ADDR s390_frame_saved_pc_nofix (struct frame_info *fi);
122
123 static int
124 s390_readinstruction (bfd_byte instr[], CORE_ADDR at)
125 {
126   int instrlen;
127
128   static int s390_instrlen[] = {
129     2,
130     4,
131     4,
132     6
133   };
134   if (target_read_memory (at, &instr[0], 2))
135     return -1;
136   instrlen = s390_instrlen[instr[0] >> 6];
137   if (instrlen > 2)
138     {
139       if (target_read_memory (at + 2, &instr[2], instrlen - 2))
140         return -1;
141     }
142   return instrlen;
143 }
144
145 static void
146 s390_memset_extra_info (struct frame_extra_info *fextra_info)
147 {
148   memset (fextra_info, 0, sizeof (struct frame_extra_info));
149 }
150
151
152
153 static const char *
154 s390_register_name (int reg_nr)
155 {
156   static char *register_names[] = {
157     "pswm", "pswa",
158     "r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7",
159     "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15",
160     "acr0", "acr1", "acr2", "acr3", "acr4", "acr5", "acr6", "acr7",
161     "acr8", "acr9", "acr10", "acr11", "acr12", "acr13", "acr14", "acr15",
162     "cr0", "cr1", "cr2", "cr3", "cr4", "cr5", "cr6", "cr7",
163     "cr8", "cr9", "cr10", "cr11", "cr12", "cr13", "cr14", "cr15",
164     "fpc",
165     "f0", "f1", "f2", "f3", "f4", "f5", "f6", "f7",
166     "f8", "f9", "f10", "f11", "f12", "f13", "f14", "f15"
167   };
168
169   if (reg_nr <= S390_LAST_REGNUM)
170     return register_names[reg_nr];
171   else
172     return NULL;
173 }
174
175
176
177
178 static int
179 s390_stab_reg_to_regnum (int regno)
180 {
181   return regno >= 64 ? S390_PSWM_REGNUM - 64 :
182     regno >= 48 ? S390_FIRST_ACR - 48 :
183     regno >= 32 ? S390_FIRST_CR - 32 :
184     regno <= 15 ? (regno + 2) :
185     S390_FP0_REGNUM + ((regno - 16) & 8) + (((regno - 16) & 3) << 1) +
186     (((regno - 16) & 4) >> 2);
187 }
188
189
190 /* Prologue analysis.  */
191
192 /* When we analyze a prologue, we're really doing 'abstract
193    interpretation' or 'pseudo-evaluation': running the function's code
194    in simulation, but using conservative approximations of the values
195    it would have when it actually runs.  For example, if our function
196    starts with the instruction:
197
198       ahi r1, 42     # add halfword immediate 42 to r1
199
200    we don't know exactly what value will be in r1 after executing this
201    instruction, but we do know it'll be 42 greater than its original
202    value.
203
204    If we then see an instruction like:
205
206       ahi r1, 22     # add halfword immediate 22 to r1
207
208    we still don't know what r1's value is, but again, we can say it is
209    now 64 greater than its original value.
210
211    If the next instruction were:
212
213       lr r2, r1      # set r2 to r1's value
214
215    then we can say that r2's value is now the original value of r1
216    plus 64.  And so on.
217
218    Of course, this can only go so far before it gets unreasonable.  If
219    we wanted to be able to say anything about the value of r1 after
220    the instruction:
221
222       xr r1, r3      # exclusive-or r1 and r3, place result in r1
223
224    then things would get pretty complex.  But remember, we're just
225    doing a conservative approximation; if exclusive-or instructions
226    aren't relevant to prologues, we can just say r1's value is now
227    'unknown'.  We can ignore things that are too complex, if that loss
228    of information is acceptable for our application.
229
230    Once you've reached an instruction that you don't know how to
231    simulate, you stop.  Now you examine the state of the registers and
232    stack slots you've kept track of.  For example:
233
234    - To see how large your stack frame is, just check the value of sp;
235      if it's the original value of sp minus a constant, then that
236      constant is the stack frame's size.  If the sp's value has been
237      marked as 'unknown', then that means the prologue has done
238      something too complex for us to track, and we don't know the
239      frame size.
240
241    - To see whether we've saved the SP in the current frame's back
242      chain slot, we just check whether the current value of the back
243      chain stack slot is the original value of the sp.
244
245    Sure, this takes some work.  But prologue analyzers aren't
246    quick-and-simple pattern patching to recognize a few fixed prologue
247    forms any more; they're big, hairy functions.  Along with inferior
248    function calls, prologue analysis accounts for a substantial
249    portion of the time needed to stabilize a GDB port.  So I think
250    it's worthwhile to look for an approach that will be easier to
251    understand and maintain.  In the approach used here:
252
253    - It's easier to see that the analyzer is correct: you just see
254      whether the analyzer properly (albiet conservatively) simulates
255      the effect of each instruction.
256
257    - It's easier to extend the analyzer: you can add support for new
258      instructions, and know that you haven't broken anything that
259      wasn't already broken before.
260
261    - It's orthogonal: to gather new information, you don't need to
262      complicate the code for each instruction.  As long as your domain
263      of conservative values is already detailed enough to tell you
264      what you need, then all the existing instruction simulations are
265      already gathering the right data for you.
266
267    A 'struct prologue_value' is a conservative approximation of the
268    real value the register or stack slot will have.  */
269
270 struct prologue_value {
271
272   /* What sort of value is this?  This determines the interpretation
273      of subsequent fields.  */
274   enum {
275
276     /* We don't know anything about the value.  This is also used for
277        values we could have kept track of, when doing so would have
278        been too complex and we don't want to bother.  The bottom of
279        our lattice.  */
280     pv_unknown,
281
282     /* A known constant.  K is its value.  */
283     pv_constant,
284
285     /* The value that register REG originally had *UPON ENTRY TO THE
286        FUNCTION*, plus K.  If K is zero, this means, obviously, just
287        the value REG had upon entry to the function.  REG is a GDB
288        register number.  Before we start interpreting, we initialize
289        every register R to { pv_register, R, 0 }.  */
290     pv_register,
291
292   } kind;
293
294   /* The meanings of the following fields depend on 'kind'; see the
295      comments for the specific 'kind' values.  */
296   int reg;
297   CORE_ADDR k;
298 };
299
300
301 /* Set V to be unknown.  */
302 static void
303 pv_set_to_unknown (struct prologue_value *v)
304 {
305   v->kind = pv_unknown;
306 }
307
308
309 /* Set V to the constant K.  */
310 static void
311 pv_set_to_constant (struct prologue_value *v, CORE_ADDR k)
312 {
313   v->kind = pv_constant;
314   v->k = k;
315 }
316
317
318 /* Set V to the original value of register REG, plus K.  */
319 static void
320 pv_set_to_register (struct prologue_value *v, int reg, CORE_ADDR k)
321 {
322   v->kind = pv_register;
323   v->reg = reg;
324   v->k = k;
325 }
326
327
328 /* If one of *A and *B is a constant, and the other isn't, swap the
329    pointers as necessary to ensure that *B points to the constant.
330    This can reduce the number of cases we need to analyze in the
331    functions below.  */
332 static void
333 pv_constant_last (struct prologue_value **a,
334                   struct prologue_value **b)
335 {
336   if ((*a)->kind == pv_constant
337       && (*b)->kind != pv_constant)
338     {
339       struct prologue_value *temp = *a;
340       *a = *b;
341       *b = temp;
342     }
343 }
344
345
346 /* Set SUM to the sum of A and B.  SUM, A, and B may point to the same
347    'struct prologue_value' object.  */
348 static void
349 pv_add (struct prologue_value *sum,
350         struct prologue_value *a,
351         struct prologue_value *b)
352 {
353   pv_constant_last (&a, &b);
354
355   /* We can handle adding constants to registers, and other constants.  */
356   if (b->kind == pv_constant
357       && (a->kind == pv_register
358           || a->kind == pv_constant))
359     {
360       sum->kind = a->kind;
361       sum->reg = a->reg;    /* not meaningful if a is pv_constant, but
362                                harmless */
363       sum->k = a->k + b->k;
364     }
365
366   /* Anything else we don't know how to add.  We don't have a
367      representation for, say, the sum of two registers, or a multiple
368      of a register's value (adding a register to itself).  */
369   else
370     sum->kind = pv_unknown;
371 }
372
373
374 /* Add the constant K to V.  */
375 static void
376 pv_add_constant (struct prologue_value *v, CORE_ADDR k)
377 {
378   struct prologue_value pv_k;
379
380   /* Rather than thinking of all the cases we can and can't handle,
381      we'll just let pv_add take care of that for us.  */
382   pv_set_to_constant (&pv_k, k);
383   pv_add (v, v, &pv_k);
384 }
385
386
387 /* Subtract B from A, and put the result in DIFF.
388
389    This isn't quite the same as negating B and adding it to A, since
390    we don't have a representation for the negation of anything but a
391    constant.  For example, we can't negate { pv_register, R1, 10 },
392    but we do know that { pv_register, R1, 10 } minus { pv_register,
393    R1, 5 } is { pv_constant, <ignored>, 5 }.
394
395    This means, for example, that we can subtract two stack addresses;
396    they're both relative to the original SP.  Since the frame pointer
397    is set based on the SP, its value will be the original SP plus some
398    constant (probably zero), so we can use its value just fine.  */
399 static void
400 pv_subtract (struct prologue_value *diff,
401              struct prologue_value *a,
402              struct prologue_value *b)
403 {
404   pv_constant_last (&a, &b);
405
406   /* We can subtract a constant from another constant, or from a
407      register.  */
408   if (b->kind == pv_constant
409       && (a->kind == pv_register
410           || a->kind == pv_constant))
411     {
412       diff->kind = a->kind;
413       diff->reg = a->reg;    /* not always meaningful, but harmless */
414       diff->k = a->k - b->k;
415     }
416
417   /* We can subtract a register from itself, yielding a constant.  */
418   else if (a->kind == pv_register
419            && b->kind == pv_register
420            && a->reg == b->reg)
421     {
422       diff->kind = pv_constant;
423       diff->k = a->k - b->k;
424     }
425
426   /* We don't know how to subtract anything else.  */
427   else
428     diff->kind = pv_unknown;
429 }
430
431
432 /* Set AND to the logical and of A and B.  */
433 static void
434 pv_logical_and (struct prologue_value *and,
435                 struct prologue_value *a,
436                 struct prologue_value *b)
437 {
438   pv_constant_last (&a, &b);
439
440   /* We can 'and' two constants.  */
441   if (a->kind == pv_constant
442       && b->kind == pv_constant)
443     {
444       and->kind = pv_constant;
445       and->k = a->k & b->k;
446     }
447
448   /* We can 'and' anything with the constant zero.  */
449   else if (b->kind == pv_constant
450            && b->k == 0)
451     {
452       and->kind = pv_constant;
453       and->k = 0;
454     }
455   
456   /* We can 'and' anything with ~0.  */
457   else if (b->kind == pv_constant
458            && b->k == ~ (CORE_ADDR) 0)
459     *and = *a;
460
461   /* We can 'and' a register with itself.  */
462   else if (a->kind == pv_register
463            && b->kind == pv_register
464            && a->reg == b->reg
465            && a->k == b->k)
466     *and = *a;
467
468   /* Otherwise, we don't know.  */
469   else
470     pv_set_to_unknown (and);
471 }
472
473
474 /* Return non-zero iff A and B are identical expressions.
475
476    This is not the same as asking if the two values are equal; the
477    result of such a comparison would have to be a pv_boolean, and
478    asking whether two 'unknown' values were equal would give you
479    pv_maybe.  Same for comparing, say, { pv_register, R1, 0 } and {
480    pv_register, R2, 0}.  Instead, this is asking whether the two
481    representations are the same.  */
482 static int
483 pv_is_identical (struct prologue_value *a,
484                  struct prologue_value *b)
485 {
486   if (a->kind != b->kind)
487     return 0;
488
489   switch (a->kind)
490     {
491     case pv_unknown:
492       return 1;
493     case pv_constant:
494       return (a->k == b->k);
495     case pv_register:
496       return (a->reg == b->reg && a->k == b->k);
497     default:
498       gdb_assert (0);
499     }
500 }
501
502
503 /* Return non-zero if A is the original value of register number R
504    plus K, zero otherwise.  */
505 static int
506 pv_is_register (struct prologue_value *a, int r, CORE_ADDR k)
507 {
508   return (a->kind == pv_register
509           && a->reg == r
510           && a->k == k);
511 }
512
513
514 /* A prologue-value-esque boolean type, including "maybe", when we
515    can't figure out whether something is true or not.  */
516 enum pv_boolean {
517   pv_maybe,
518   pv_definite_yes,
519   pv_definite_no,
520 };
521
522
523 /* Decide whether a reference to SIZE bytes at ADDR refers exactly to
524    an element of an array.  The array starts at ARRAY_ADDR, and has
525    ARRAY_LEN values of ELT_SIZE bytes each.  If ADDR definitely does
526    refer to an array element, set *I to the index of the referenced
527    element in the array, and return pv_definite_yes.  If it definitely
528    doesn't, return pv_definite_no.  If we can't tell, return pv_maybe.
529
530    If the reference does touch the array, but doesn't fall exactly on
531    an element boundary, or doesn't refer to the whole element, return
532    pv_maybe.  */
533 static enum pv_boolean
534 pv_is_array_ref (struct prologue_value *addr,
535                  CORE_ADDR size,
536                  struct prologue_value *array_addr,
537                  CORE_ADDR array_len, 
538                  CORE_ADDR elt_size,
539                  int *i)
540 {
541   struct prologue_value offset;
542
543   /* Note that, since ->k is a CORE_ADDR, and CORE_ADDR is unsigned,
544      if addr is *before* the start of the array, then this isn't going
545      to be negative...  */
546   pv_subtract (&offset, addr, array_addr);
547
548   if (offset.kind == pv_constant)
549     {
550       /* This is a rather odd test.  We want to know if the SIZE bytes
551          at ADDR don't overlap the array at all, so you'd expect it to
552          be an || expression: "if we're completely before || we're
553          completely after".  But with unsigned arithmetic, things are
554          different: since it's a number circle, not a number line, the
555          right values for offset.k are actually one contiguous range.  */
556       if (offset.k <= -size
557           && offset.k >= array_len * elt_size)
558         return pv_definite_no;
559       else if (offset.k % elt_size != 0
560                || size != elt_size)
561         return pv_maybe;
562       else
563         {
564           *i = offset.k / elt_size;
565           return pv_definite_yes;
566         }
567     }
568   else
569     return pv_maybe;
570 }
571
572
573
574 /* Decoding S/390 instructions.  */
575
576 /* Named opcode values for the S/390 instructions we recognize.  Some
577    instructions have their opcode split across two fields; those are the
578    op1_* and op2_* enums.  */
579 enum
580   {
581     op1_aghi = 0xa7,   op2_aghi = 0xb,
582     op1_ahi  = 0xa7,   op2_ahi  = 0xa,
583     op_ar    = 0x1a,
584     op_basr  = 0x0d,
585     op1_bras = 0xa7,   op2_bras = 0x5,
586     op_l     = 0x58,
587     op_la    = 0x41,
588     op1_larl = 0xc0,   op2_larl = 0x0,
589     op_lgr   = 0xb904,
590     op1_lghi = 0xa7,   op2_lghi = 0x9,
591     op1_lhi  = 0xa7,   op2_lhi  = 0x8,
592     op_lr    = 0x18,
593     op_nr    = 0x14,
594     op_ngr   = 0xb980,
595     op_s     = 0x5b,
596     op_st    = 0x50,
597     op_std   = 0x60,
598     op1_stg  = 0xe3,   op2_stg  = 0x24,
599     op_stm   = 0x90,
600     op1_stmg = 0xeb,   op2_stmg = 0x24,
601     op_svc   = 0x0a,
602   };
603
604
605 /* The functions below are for recognizing and decoding S/390
606    instructions of various formats.  Each of them checks whether INSN
607    is an instruction of the given format, with the specified opcodes.
608    If it is, it sets the remaining arguments to the values of the
609    instruction's fields, and returns a non-zero value; otherwise, it
610    returns zero.
611
612    These functions' arguments appear in the order they appear in the
613    instruction, not in the machine-language form.  So, opcodes always
614    come first, even though they're sometimes scattered around the
615    instructions.  And displacements appear before base and extension
616    registers, as they do in the assembly syntax, not at the end, as
617    they do in the machine language.  */
618 static int
619 is_ri (bfd_byte *insn, int op1, int op2, unsigned int *r1, int *i2)
620 {
621   if (insn[0] == op1 && (insn[1] & 0xf) == op2)
622     {
623       *r1 = (insn[1] >> 4) & 0xf;
624       /* i2 is a 16-bit signed quantity.  */
625       *i2 = (((insn[2] << 8) | insn[3]) ^ 0x8000) - 0x8000;
626       return 1;
627     }
628   else
629     return 0;
630 }
631
632
633 static int
634 is_ril (bfd_byte *insn, int op1, int op2,
635         unsigned int *r1, int *i2)
636 {
637   if (insn[0] == op1 && (insn[1] & 0xf) == op2)
638     {
639       *r1 = (insn[1] >> 4) & 0xf;
640       /* i2 is a signed quantity.  If the host 'int' is 32 bits long,
641          no sign extension is necessary, but we don't want to assume
642          that.  */
643       *i2 = (((insn[2] << 24)
644               | (insn[3] << 16)
645               | (insn[4] << 8)
646               | (insn[5])) ^ 0x80000000) - 0x80000000;
647       return 1;
648     }
649   else
650     return 0;
651 }
652
653
654 static int
655 is_rr (bfd_byte *insn, int op, unsigned int *r1, unsigned int *r2)
656 {
657   if (insn[0] == op)
658     {
659       *r1 = (insn[1] >> 4) & 0xf;
660       *r2 = insn[1] & 0xf;
661       return 1;
662     }
663   else
664     return 0;
665 }
666
667
668 static int
669 is_rre (bfd_byte *insn, int op, unsigned int *r1, unsigned int *r2)
670 {
671   if (((insn[0] << 8) | insn[1]) == op)
672     {
673       /* Yes, insn[3].  insn[2] is unused in RRE format.  */
674       *r1 = (insn[3] >> 4) & 0xf;
675       *r2 = insn[3] & 0xf;
676       return 1;
677     }
678   else
679     return 0;
680 }
681
682
683 static int
684 is_rs (bfd_byte *insn, int op,
685        unsigned int *r1, unsigned int *r3, unsigned int *d2, unsigned int *b2)
686 {
687   if (insn[0] == op)
688     {
689       *r1 = (insn[1] >> 4) & 0xf;
690       *r3 = insn[1] & 0xf;
691       *b2 = (insn[2] >> 4) & 0xf;
692       *d2 = ((insn[2] & 0xf) << 8) | insn[3];
693       return 1;
694     }
695   else
696     return 0;
697 }
698
699
700 static int
701 is_rse (bfd_byte *insn, int op1, int op2,
702         unsigned int *r1, unsigned int *r3, unsigned int *d2, unsigned int *b2)
703 {
704   if (insn[0] == op1
705       /* Yes, insn[5].  insn[4] is unused.  */
706       && insn[5] == op2)
707     {
708       *r1 = (insn[1] >> 4) & 0xf;
709       *r3 = insn[1] & 0xf;
710       *b2 = (insn[2] >> 4) & 0xf;
711       *d2 = ((insn[2] & 0xf) << 8) | insn[3];
712       return 1;
713     }
714   else
715     return 0;
716 }
717
718
719 static int
720 is_rx (bfd_byte *insn, int op,
721        unsigned int *r1, unsigned int *d2, unsigned int *x2, unsigned int *b2)
722 {
723   if (insn[0] == op)
724     {
725       *r1 = (insn[1] >> 4) & 0xf;
726       *x2 = insn[1] & 0xf;
727       *b2 = (insn[2] >> 4) & 0xf;
728       *d2 = ((insn[2] & 0xf) << 8) | insn[3];
729       return 1;
730     }
731   else
732     return 0;
733 }
734
735
736 static int
737 is_rxe (bfd_byte *insn, int op1, int op2,
738         unsigned int *r1, unsigned int *d2, unsigned int *x2, unsigned int *b2)
739 {
740   if (insn[0] == op1
741       /* Yes, insn[5].  insn[4] is unused.  */
742       && insn[5] == op2)
743     {
744       *r1 = (insn[1] >> 4) & 0xf;
745       *x2 = insn[1] & 0xf;
746       *b2 = (insn[2] >> 4) & 0xf;
747       *d2 = ((insn[2] & 0xf) << 8) | insn[3];
748       return 1;
749     }
750   else
751     return 0;
752 }
753
754
755 /* Set ADDR to the effective address for an X-style instruction, like:
756
757         L R1, D2(X2, B2)
758
759    Here, X2 and B2 are registers, and D2 is an unsigned 12-bit
760    constant; the effective address is the sum of all three.  If either
761    X2 or B2 are zero, then it doesn't contribute to the sum --- this
762    means that r0 can't be used as either X2 or B2.
763
764    GPR is an array of general register values, indexed by GPR number,
765    not GDB register number.  */
766 static void
767 compute_x_addr (struct prologue_value *addr, 
768                 struct prologue_value *gpr,
769                 unsigned int d2, unsigned int x2, unsigned int b2)
770 {
771   /* We can't just add stuff directly in addr; it might alias some of
772      the registers we need to read.  */
773   struct prologue_value result;
774
775   pv_set_to_constant (&result, d2);
776   if (x2)
777     pv_add (&result, &result, &gpr[x2]);
778   if (b2)
779     pv_add (&result, &result, &gpr[b2]);
780
781   *addr = result;
782 }
783
784
785 /* The number of GPR and FPR spill slots in an S/390 stack frame.  We
786    track general-purpose registers r2 -- r15, and floating-point
787    registers f0, f2, f4, and f6.  */
788 #define S390_NUM_SPILL_SLOTS (14 + 4)
789
790
791 /* If the SIZE bytes at ADDR are a stack slot we're actually tracking,
792    return pv_definite_yes and set *STACK to point to the slot.  If
793    we're sure that they are not any of our stack slots, then return
794    pv_definite_no.  Otherwise, return pv_maybe.
795    - GPR is an array indexed by GPR number giving the current values
796      of the general-purpose registers.
797    - SPILL is an array tracking the spill area of the caller's frame;
798      SPILL[i] is the i'th spill slot.  The spill slots are designated
799      for r2 -- r15, and then f0, f2, f4, and f6.
800    - BACK_CHAIN is the value of the back chain slot; it's only valid
801      when the current frame actually has some space for a back chain
802      slot --- that is, when the current value of the stack pointer
803      (according to GPR) is at least S390_STACK_FRAME_OVERHEAD bytes
804      less than its original value.  */
805 static enum pv_boolean
806 s390_on_stack (struct prologue_value *addr,
807                CORE_ADDR size,
808                struct prologue_value *gpr,
809                struct prologue_value *spill, 
810                struct prologue_value *back_chain,
811                struct prologue_value **stack)
812 {
813   struct prologue_value gpr_spill_addr;
814   struct prologue_value fpr_spill_addr;
815   struct prologue_value back_chain_addr;  
816   int i;
817   enum pv_boolean b;
818
819   /* Construct the addresses of the spill arrays and the back chain.  */
820   pv_set_to_register (&gpr_spill_addr, S390_SP_REGNUM, 2 * S390_GPR_SIZE);
821   pv_set_to_register (&fpr_spill_addr, S390_SP_REGNUM, 16 * S390_GPR_SIZE);
822   back_chain_addr = gpr[S390_SP_REGNUM - S390_GP0_REGNUM];
823
824   /* We have to check for GPR and FPR references using two separate
825      calls to pv_is_array_ref, since the GPR and FPR spill slots are
826      different sizes.  (SPILL is an array, but the thing it tracks
827      isn't really an array.)  */
828
829   /* Was it a reference to the GPR spill array?  */
830   b = pv_is_array_ref (addr, size, &gpr_spill_addr, 14, S390_GPR_SIZE, &i);
831   if (b == pv_definite_yes)
832     {
833       *stack = &spill[i];
834       return pv_definite_yes;
835     }
836   if (b == pv_maybe)
837     return pv_maybe;
838
839   /* Was it a reference to the FPR spill array?  */
840   b = pv_is_array_ref (addr, size, &fpr_spill_addr, 4, S390_FPR_SIZE, &i);
841   if (b == pv_definite_yes)
842     {
843       *stack = &spill[14 + i];
844       return pv_definite_yes;
845     }
846   if (b == pv_maybe)
847     return pv_maybe;
848
849   /* Was it a reference to the back chain?
850      This isn't quite right.  We ought to check whether we have
851      actually allocated any new frame at all.  */
852   b = pv_is_array_ref (addr, size, &back_chain_addr, 1, S390_GPR_SIZE, &i);
853   if (b == pv_definite_yes)
854     {
855       *stack = back_chain;
856       return pv_definite_yes;
857     }
858   if (b == pv_maybe)
859     return pv_maybe;
860
861   /* All the above queries returned definite 'no's.  */
862   return pv_definite_no;
863 }
864
865
866 /* Do a SIZE-byte store of VALUE to ADDR.  GPR, SPILL, and BACK_CHAIN,
867    and the return value are as described for s390_on_stack, above.
868    Note that, when this returns pv_maybe, we have to assume that all
869    of our memory now contains unknown values.  */
870 static enum pv_boolean
871 s390_store (struct prologue_value *addr,
872             CORE_ADDR size,
873             struct prologue_value *value,
874             struct prologue_value *gpr,
875             struct prologue_value *spill, 
876             struct prologue_value *back_chain)
877 {
878   struct prologue_value *stack;
879   enum pv_boolean on_stack
880     = s390_on_stack (addr, size, gpr, spill, back_chain, &stack);
881
882   if (on_stack == pv_definite_yes)
883     *stack = *value;
884
885   return on_stack;
886 }
887             
888
889 /* The current frame looks like a signal delivery frame: the first
890    instruction is an 'svc' opcode.  If the next frame is a signal
891    handler's frame, set FI's saved register map to point into the
892    signal context structure.  */
893 static void
894 s390_get_signal_frame_info (struct frame_info *fi)
895 {
896   struct frame_info *next_frame = get_next_frame (fi);
897
898   if (next_frame
899       && get_frame_extra_info (next_frame)
900       && get_frame_extra_info (next_frame)->sigcontext)
901     {
902       /* We're definitely backtracing from a signal handler.  */
903       CORE_ADDR *saved_regs = deprecated_get_frame_saved_regs (fi);
904       CORE_ADDR save_reg_addr = (get_frame_extra_info (next_frame)->sigcontext
905                                  + DEPRECATED_REGISTER_BYTE (S390_GP0_REGNUM));
906       int reg;
907
908       for (reg = 0; reg < S390_NUM_GPRS; reg++)
909         {
910           saved_regs[S390_GP0_REGNUM + reg] = save_reg_addr;
911           save_reg_addr += S390_GPR_SIZE;
912         }
913
914       save_reg_addr = (get_frame_extra_info (next_frame)->sigcontext
915                        + (GDB_TARGET_IS_ESAME ? S390X_SIGREGS_FP0_OFFSET :
916                           S390_SIGREGS_FP0_OFFSET));
917       for (reg = 0; reg < S390_NUM_FPRS; reg++)
918         {
919           saved_regs[S390_FP0_REGNUM + reg] = save_reg_addr;
920           save_reg_addr += S390_FPR_SIZE;
921         }
922     }
923 }
924
925
926 static int
927 s390_get_frame_info (CORE_ADDR start_pc,
928                      struct frame_extra_info *fextra_info,
929                      struct frame_info *fi,
930                      int init_extra_info)
931 {
932   /* Our return value:
933      zero if we were able to read all the instructions we wanted, or
934      -1 if we got an error trying to read memory.  */
935   int result = 0;
936
937   /* The current PC for our abstract interpretation.  */
938   CORE_ADDR pc;
939
940   /* The address of the next instruction after that.  */
941   CORE_ADDR next_pc;
942   
943   /* The general-purpose registers.  */
944   struct prologue_value gpr[S390_NUM_GPRS];
945
946   /* The floating-point registers.  */
947   struct prologue_value fpr[S390_NUM_FPRS];
948
949   /* The register spill stack slots in the caller's frame ---
950      general-purpose registers r2 through r15, and floating-point
951      registers.  spill[i] is where gpr i+2 gets spilled;
952      spill[(14, 15, 16, 17)] is where (f0, f2, f4, f6) get spilled.  */
953   struct prologue_value spill[S390_NUM_SPILL_SLOTS];
954
955   /* The value of the back chain slot.  This is only valid if the stack
956      pointer is known to be less than its original value --- that is,
957      if we have indeed allocated space on the stack.  */
958   struct prologue_value back_chain;
959
960   /* The address of the instruction after the last one that changed
961      the SP, FP, or back chain.  */
962   CORE_ADDR after_last_frame_setup_insn = start_pc;
963
964   /* Set up everything's initial value.  */
965   {
966     int i;
967
968     for (i = 0; i < S390_NUM_GPRS; i++)
969       pv_set_to_register (&gpr[i], S390_GP0_REGNUM + i, 0);
970
971     for (i = 0; i < S390_NUM_FPRS; i++)
972       pv_set_to_register (&fpr[i], S390_FP0_REGNUM + i, 0);
973
974     for (i = 0; i < S390_NUM_SPILL_SLOTS; i++)
975       pv_set_to_unknown (&spill[i]);
976
977     pv_set_to_unknown (&back_chain);
978   }
979
980   /* Start interpreting instructions, until we hit something we don't
981      know how to interpret.  (Ideally, we should stop at the frame's
982      real current PC, but at the moment, our callers don't give us
983      that info.)  */
984   for (pc = start_pc; ; pc = next_pc)
985     {
986       bfd_byte insn[S390_MAX_INSTR_SIZE];
987       int insn_len = s390_readinstruction (insn, pc);
988
989       /* Fields for various kinds of instructions.  */
990       unsigned int b2, r1, r2, d2, x2, r3;
991       int i2;
992
993       /* The values of SP, FP, and back chain before this instruction,
994          for detecting instructions that change them.  */
995       struct prologue_value pre_insn_sp, pre_insn_fp, pre_insn_back_chain;
996
997       /* If we got an error trying to read the instruction, report it.  */
998       if (insn_len < 0)
999         {
1000           result = -1;
1001           break;
1002         }
1003
1004       next_pc = pc + insn_len;
1005
1006       pre_insn_sp = gpr[S390_SP_REGNUM - S390_GP0_REGNUM];
1007       pre_insn_fp = gpr[S390_FRAME_REGNUM - S390_GP0_REGNUM];
1008       pre_insn_back_chain = back_chain;
1009
1010       /* A special case, first --- only recognized as the very first
1011          instruction of the function, for signal delivery frames:
1012          SVC i --- system call  */
1013       if (pc == start_pc
1014           && is_rr (insn, op_svc, &r1, &r2))
1015         {
1016           if (fi)
1017             s390_get_signal_frame_info (fi);
1018           break;
1019         }
1020         
1021       /* AHI r1, i2 --- add halfword immediate */
1022       else if (is_ri (insn, op1_ahi, op2_ahi, &r1, &i2))
1023         pv_add_constant (&gpr[r1], i2);
1024
1025
1026       /* AGHI r1, i2 --- add halfword immediate (64-bit version) */
1027       else if (GDB_TARGET_IS_ESAME
1028                && is_ri (insn, op1_aghi, op2_aghi, &r1, &i2))
1029         pv_add_constant (&gpr[r1], i2);
1030
1031       /* AR r1, r2 -- add register */
1032       else if (is_rr (insn, op_ar, &r1, &r2))
1033         pv_add (&gpr[r1], &gpr[r1], &gpr[r2]);
1034
1035       /* BASR r1, 0 --- branch and save
1036          Since r2 is zero, this saves the PC in r1, but doesn't branch.  */
1037       else if (is_rr (insn, op_basr, &r1, &r2)
1038                && r2 == 0)
1039         pv_set_to_constant (&gpr[r1], next_pc);
1040
1041       /* BRAS r1, i2 --- branch relative and save */
1042       else if (is_ri (insn, op1_bras, op2_bras, &r1, &i2))
1043         {
1044           pv_set_to_constant (&gpr[r1], next_pc);
1045           next_pc = pc + i2 * 2;
1046
1047           /* We'd better not interpret any backward branches.  We'll
1048              never terminate.  */
1049           if (next_pc <= pc)
1050             break;
1051         }
1052
1053       /* L r1, d2(x2, b2) --- load */
1054       else if (is_rx (insn, op_l, &r1, &d2, &x2, &b2))
1055         {
1056           struct prologue_value addr;
1057           struct prologue_value *stack;
1058
1059           compute_x_addr (&addr, gpr, d2, x2, b2);
1060
1061           /* If it's a load from an in-line constant pool, then we can
1062              simulate that, under the assumption that the code isn't
1063              going to change between the time the processor actually
1064              executed it creating the current frame, and the time when
1065              we're analyzing the code to unwind past that frame.  */
1066           if (addr.kind == pv_constant
1067               && start_pc <= addr.k 
1068               && addr.k < next_pc)
1069             pv_set_to_constant (&gpr[r1], 
1070                                 read_memory_integer (addr.k, 4));
1071
1072           /* If it's definitely a reference to something on the stack, 
1073              we can do that.  */
1074           else if (s390_on_stack (&addr, 4, gpr, spill, &back_chain, &stack)
1075                    == pv_definite_yes)
1076             gpr[r1] = *stack;
1077
1078           /* Otherwise, we don't know the value.  */
1079           else
1080             pv_set_to_unknown (&gpr[r1]);
1081         }
1082
1083       /* LA r1, d2(x2, b2) --- load address */
1084       else if (is_rx (insn, op_la, &r1, &d2, &x2, &b2))
1085         compute_x_addr (&gpr[r1], gpr, d2, x2, b2);
1086
1087       /* LARL r1, i2 --- load address relative long */
1088       else if (GDB_TARGET_IS_ESAME 
1089                && is_ril (insn, op1_larl, op2_larl, &r1, &i2))
1090         pv_set_to_constant (&gpr[r1], pc + i2 * 2);
1091
1092       /* LGR r1, r2 --- load from register */
1093       else if (GDB_TARGET_IS_ESAME
1094                && is_rre (insn, op_lgr, &r1, &r2))
1095         gpr[r1] = gpr[r2];
1096
1097       /* LHI r1, i2 --- load halfword immediate */
1098       else if (is_ri (insn, op1_lhi, op2_lhi, &r1, &i2))
1099         pv_set_to_constant (&gpr[r1], i2);
1100
1101       /* LGHI r1, i2 --- load halfword immediate --- 64-bit version */
1102       else if (is_ri (insn, op1_lghi, op2_lghi, &r1, &i2))
1103         pv_set_to_constant (&gpr[r1], i2);
1104
1105       /* LR r1, r2 --- load from register */
1106       else if (is_rr (insn, op_lr, &r1, &r2))
1107         gpr[r1] = gpr[r2];
1108
1109       /* NGR r1, r2 --- logical and --- 64-bit version */
1110       else if (GDB_TARGET_IS_ESAME
1111                && is_rre (insn, op_ngr, &r1, &r2))
1112         pv_logical_and (&gpr[r1], &gpr[r1], &gpr[r2]);
1113
1114       /* NR r1, r2 --- logical and */
1115       else if (is_rr (insn, op_nr, &r1, &r2))
1116         pv_logical_and (&gpr[r1], &gpr[r1], &gpr[r2]);
1117
1118       /* NGR r1, r2 --- logical and --- 64-bit version */
1119       else if (GDB_TARGET_IS_ESAME
1120                && is_rre (insn, op_ngr, &r1, &r2))
1121         pv_logical_and (&gpr[r1], &gpr[r1], &gpr[r2]);
1122
1123       /* NR r1, r2 --- logical and */
1124       else if (is_rr (insn, op_nr, &r1, &r2))
1125         pv_logical_and (&gpr[r1], &gpr[r1], &gpr[r2]);
1126
1127       /* S r1, d2(x2, b2) --- subtract from memory */
1128       else if (is_rx (insn, op_s, &r1, &d2, &x2, &b2))
1129         {
1130           struct prologue_value addr;
1131           struct prologue_value value;
1132           struct prologue_value *stack;
1133
1134           compute_x_addr (&addr, gpr, d2, x2, b2);
1135
1136           /* If it's a load from an in-line constant pool, then we can
1137              simulate that, under the assumption that the code isn't
1138              going to change between the time the processor actually
1139              executed it and the time when we're analyzing it.  */
1140           if (addr.kind == pv_constant
1141               && start_pc <= addr.k 
1142               && addr.k < pc)
1143             pv_set_to_constant (&value, read_memory_integer (addr.k, 4));
1144
1145           /* If it's definitely a reference to something on the stack,
1146              we could do that.  */
1147           else if (s390_on_stack (&addr, 4, gpr, spill, &back_chain, &stack)
1148                    == pv_definite_yes)
1149             value = *stack;
1150
1151           /* Otherwise, we don't know the value.  */
1152           else
1153             pv_set_to_unknown (&value);
1154
1155           pv_subtract (&gpr[r1], &gpr[r1], &value);
1156         }
1157
1158       /* ST r1, d2(x2, b2) --- store  */
1159       else if (is_rx (insn, op_st, &r1, &d2, &x2, &b2))
1160         {
1161           struct prologue_value addr;
1162
1163           compute_x_addr (&addr, gpr, d2, x2, b2);
1164
1165           /* The below really should be '4', not 'S390_GPR_SIZE'; this
1166              instruction always stores 32 bits, regardless of the full
1167              size of the GPR.  */
1168           if (s390_store (&addr, 4, &gpr[r1], gpr, spill, &back_chain)
1169               == pv_maybe)
1170             /* If we can't be sure that it's *not* a store to
1171                something we're tracing, then we would have to mark all
1172                our memory as unknown --- after all, it *could* be a
1173                store to any of them --- so we might as well just stop
1174                interpreting.  */
1175             break;
1176         }
1177
1178       /* STD r1, d2(x2,b2) --- store floating-point register  */
1179       else if (is_rx (insn, op_std, &r1, &d2, &x2, &b2))
1180         {
1181           struct prologue_value addr;
1182
1183           compute_x_addr (&addr, gpr, d2, x2, b2);
1184
1185           if (s390_store (&addr, 8, &fpr[r1], gpr, spill, &back_chain)
1186               == pv_maybe)
1187             /* If we can't be sure that it's *not* a store to
1188                something we're tracing, then we would have to mark all
1189                our memory as unknown --- after all, it *could* be a
1190                store to any of them --- so we might as well just stop
1191                interpreting.  */
1192             break;
1193         }
1194
1195       /* STG r1, d2(x2, b2) --- 64-bit store */
1196       else if (GDB_TARGET_IS_ESAME
1197                && is_rxe (insn, op1_stg, op2_stg, &r1, &d2, &x2, &b2))
1198         {
1199           struct prologue_value addr;
1200
1201           compute_x_addr (&addr, gpr, d2, x2, b2);
1202
1203           /* The below really should be '8', not 'S390_GPR_SIZE'; this
1204              instruction always stores 64 bits, regardless of the full
1205              size of the GPR.  */
1206           if (s390_store (&addr, 8, &gpr[r1], gpr, spill, &back_chain)
1207               == pv_maybe)
1208             /* If we can't be sure that it's *not* a store to
1209                something we're tracing, then we would have to mark all
1210                our memory as unknown --- after all, it *could* be a
1211                store to any of them --- so we might as well just stop
1212                interpreting.  */
1213             break;
1214         }
1215
1216       /* STM r1, r3, d2(b2) --- store multiple */
1217       else if (is_rs (insn, op_stm, &r1, &r3, &d2, &b2))
1218         {
1219           int regnum;
1220           int offset;
1221           struct prologue_value addr;
1222
1223           for (regnum = r1, offset = 0;
1224                regnum <= r3;
1225                regnum++, offset += 4)
1226             {
1227               compute_x_addr (&addr, gpr, d2 + offset, 0, b2);
1228               
1229               if (s390_store (&addr, 4, &gpr[regnum], gpr, spill, &back_chain)
1230                   == pv_maybe)
1231                 /* If we can't be sure that it's *not* a store to
1232                    something we're tracing, then we would have to mark all
1233                    our memory as unknown --- after all, it *could* be a
1234                    store to any of them --- so we might as well just stop
1235                    interpreting.  */
1236                 break;
1237             }
1238
1239           /* If we left the loop early, we should stop interpreting
1240              altogether.  */
1241           if (regnum <= r3)
1242             break;
1243         }
1244
1245       /* STMG r1, r3, d2(b2) --- store multiple, 64-bit */
1246       else if (GDB_TARGET_IS_ESAME
1247                && is_rse (insn, op1_stmg, op2_stmg, &r1, &r3, &d2, &b2))
1248         {
1249           int regnum;
1250           int offset;
1251           struct prologue_value addr;
1252
1253           for (regnum = r1, offset = 0;
1254                regnum <= r3;
1255                regnum++, offset += 8)
1256             {
1257               compute_x_addr (&addr, gpr, d2 + offset, 0, b2);
1258               
1259               if (s390_store (&addr, 8, &gpr[regnum], gpr, spill, &back_chain)
1260                   == pv_maybe)
1261                 /* If we can't be sure that it's *not* a store to
1262                    something we're tracing, then we would have to mark all
1263                    our memory as unknown --- after all, it *could* be a
1264                    store to any of them --- so we might as well just stop
1265                    interpreting.  */
1266                 break;
1267             }
1268
1269           /* If we left the loop early, we should stop interpreting
1270              altogether.  */
1271           if (regnum <= r3)
1272             break;
1273         }
1274
1275       else
1276         /* An instruction we don't know how to simulate.  The only
1277            safe thing to do would be to set every value we're tracking
1278            to 'unknown'.  Instead, we'll be optimistic: we just stop
1279            interpreting, and assume that the machine state we've got
1280            now is good enough for unwinding the stack.  */
1281         break;
1282
1283       /* Record the address after the last instruction that changed
1284          the FP, SP, or backlink.  Ignore instructions that changed
1285          them back to their original values --- those are probably
1286          restore instructions.  (The back chain is never restored,
1287          just popped.)  */
1288       {
1289         struct prologue_value *sp = &gpr[S390_SP_REGNUM - S390_GP0_REGNUM];
1290         struct prologue_value *fp = &gpr[S390_FRAME_REGNUM - S390_GP0_REGNUM];
1291         
1292         if ((! pv_is_identical (&pre_insn_sp, sp)
1293              && ! pv_is_register (sp, S390_SP_REGNUM, 0))
1294             || (! pv_is_identical (&pre_insn_fp, fp)
1295                 && ! pv_is_register (fp, S390_FRAME_REGNUM, 0))
1296             || ! pv_is_identical (&pre_insn_back_chain, &back_chain))
1297           after_last_frame_setup_insn = next_pc;
1298       }
1299     }
1300
1301   /* Okay, now gpr[], fpr[], spill[], and back_chain reflect the state
1302      of the machine as of the first instruction we couldn't interpret
1303      (hopefully the first non-prologue instruction).  */
1304   {
1305     /* The size of the frame, or (CORE_ADDR) -1 if we couldn't figure
1306        that out.  */
1307     CORE_ADDR frame_size = -1;
1308
1309     /* The value the SP had upon entry to the function, or
1310        (CORE_ADDR) -1 if we can't figure that out.  */
1311     CORE_ADDR original_sp = -1;
1312
1313     /* Are we using S390_FRAME_REGNUM as a frame pointer register?  */
1314     int using_frame_pointer = 0;
1315
1316     /* If S390_FRAME_REGNUM is some constant offset from the SP, then
1317        that strongly suggests that we're going to use that as our
1318        frame pointer register, not the SP.  */
1319     {
1320       struct prologue_value *fp = &gpr[S390_FRAME_REGNUM - S390_GP0_REGNUM];
1321
1322       if (fp->kind == pv_register
1323           && fp->reg == S390_SP_REGNUM)
1324         using_frame_pointer = 1;
1325     }
1326
1327     /* If we were given a frame_info structure, we may be able to use
1328        the frame's base address to figure out the actual value of the
1329        original SP.  */
1330     if (fi && get_frame_base (fi))
1331       {
1332         int frame_base_regno;
1333         struct prologue_value *frame_base;
1334
1335         /* The meaning of the frame base depends on whether the
1336            function uses a frame pointer register other than the SP or
1337            not (see s390_read_fp):
1338            - If the function does use a frame pointer register other
1339              than the SP, then the frame base is that register's
1340              value.
1341            - If the function doesn't use a frame pointer, then the
1342              frame base is the SP itself.
1343            We're duplicating some of the logic of s390_fp_regnum here,
1344            but we don't want to call that, because it would just do
1345            exactly the same analysis we've already done above.  */
1346         if (using_frame_pointer)
1347           frame_base_regno = S390_FRAME_REGNUM;
1348         else
1349           frame_base_regno = S390_SP_REGNUM;
1350
1351         frame_base = &gpr[frame_base_regno - S390_GP0_REGNUM];
1352
1353         /* We know the frame base address; if the value of whatever
1354            register it came from is a constant offset from the
1355            original SP, then we can reconstruct the original SP just
1356            by subtracting off that constant.  */
1357         if (frame_base->kind == pv_register
1358             && frame_base->reg == S390_SP_REGNUM)
1359           original_sp = get_frame_base (fi) - frame_base->k;
1360       }
1361
1362     /* If the analysis said that the current SP value is the original
1363        value less some constant, then that constant is the frame size.  */
1364     {
1365       struct prologue_value *sp = &gpr[S390_SP_REGNUM - S390_GP0_REGNUM];
1366
1367       if (sp->kind == pv_register
1368           && sp->reg == S390_SP_REGNUM)
1369         frame_size = -sp->k;
1370     }
1371
1372     /* If we knew other registers' current values, we could check if
1373        the analysis said any of those were related to the original SP
1374        value, too.  But for now, we'll just punt.  */
1375
1376     /* If the caller passed in an 'extra info' structure, fill in the
1377        parts we can.  */
1378     if (fextra_info)
1379       {
1380         if (init_extra_info || ! fextra_info->initialised)
1381           {
1382             s390_memset_extra_info (fextra_info);
1383             fextra_info->function_start = start_pc;
1384             fextra_info->initialised = 1;
1385           }
1386
1387         if (frame_size != -1)
1388           {
1389             fextra_info->stack_bought_valid = 1;
1390             fextra_info->stack_bought = frame_size;
1391           }
1392
1393         /* Assume everything was okay, and indicate otherwise when we
1394            find something amiss.  */
1395         fextra_info->good_prologue = 1;
1396
1397         if (using_frame_pointer)
1398           /* Actually, nobody cares about the exact PC, so any
1399              non-zero value will do here.  */
1400           fextra_info->frame_pointer_saved_pc = 1;
1401
1402         /* If we weren't able to find the size of the frame, or find
1403            the original sp based on actual current register values,
1404            then we're not going to be able to unwind this frame.
1405
1406            (If we're just doing prologue analysis to set a breakpoint,
1407            then frame_size might be known, but original_sp unknown; if
1408            we're analyzing a real frame which uses alloca, then
1409            original_sp might be known (from the frame pointer
1410            register), but the frame size might be unknown.)  */
1411         if (original_sp == -1 && frame_size == -1)
1412           fextra_info->good_prologue = 0;
1413         
1414         if (fextra_info->good_prologue)
1415           fextra_info->skip_prologue_function_start
1416             = after_last_frame_setup_insn;
1417         else 
1418           /* If the prologue was too complex for us to make sense of,
1419              then perhaps it's better to just not skip anything at
1420              all.  */
1421           fextra_info->skip_prologue_function_start = start_pc;
1422       }
1423
1424     /* Indicate where registers were saved on the stack, if:
1425        - the caller seems to want to know,
1426        - the caller provided an actual SP, and
1427        - the analysis gave us enough information to actually figure it
1428          out.  */
1429     if (fi
1430         && deprecated_get_frame_saved_regs (fi)
1431         && original_sp != -1)
1432       {
1433         int slot_num;
1434         CORE_ADDR slot_addr;
1435         CORE_ADDR *saved_regs = deprecated_get_frame_saved_regs (fi);
1436
1437         /* Scan the spill array; if a spill slot says it holds the
1438            original value of some register, then record that slot's
1439            address as the place that register was saved.
1440
1441            Just for kicks, note that, even if registers aren't saved
1442            in their officially-sanctioned slots, this will still work
1443            --- we know what really got put where.  */
1444
1445         /* First, the slots for r2 -- r15.  */
1446         for (slot_num = 0, slot_addr = original_sp + 2 * S390_GPR_SIZE;
1447              slot_num < 14;
1448              slot_num++, slot_addr += S390_GPR_SIZE)
1449           {
1450             struct prologue_value *slot = &spill[slot_num];
1451
1452             if (slot->kind == pv_register
1453                 && slot->k == 0)
1454               saved_regs[slot->reg] = slot_addr;
1455           }
1456
1457         /* Then, the slots for f0, f2, f4, and f6.  They're a
1458            different size.  */
1459         for (slot_num = 14, slot_addr = original_sp + 16 * S390_GPR_SIZE;
1460              slot_num < S390_NUM_SPILL_SLOTS;
1461              slot_num++, slot_addr += S390_FPR_SIZE)
1462           {
1463             struct prologue_value *slot = &spill[slot_num];
1464             
1465             if (slot->kind == pv_register
1466                 && slot->k == 0)
1467               saved_regs[slot->reg] = slot_addr;
1468           }
1469
1470         /* The stack pointer's element of saved_regs[] is special.  */
1471         saved_regs[S390_SP_REGNUM] = original_sp;
1472       }
1473   }
1474
1475   return result;
1476 }
1477
1478
1479 static int
1480 s390_check_function_end (CORE_ADDR pc)
1481 {
1482   bfd_byte instr[S390_MAX_INSTR_SIZE];
1483   int regidx, instrlen;
1484
1485   instrlen = s390_readinstruction (instr, pc);
1486   if (instrlen < 0)
1487     return -1;
1488   /* check for BR */
1489   if (instrlen != 2 || instr[0] != 07 || (instr[1] >> 4) != 0xf)
1490     return 0;
1491   regidx = instr[1] & 0xf;
1492   /* Check for LMG or LG */
1493   instrlen =
1494     s390_readinstruction (instr, pc - (GDB_TARGET_IS_ESAME ? 6 : 4));
1495   if (instrlen < 0)
1496     return -1;
1497   if (GDB_TARGET_IS_ESAME)
1498     {
1499
1500       if (instrlen != 6 || instr[0] != 0xeb || instr[5] != 0x4)
1501         return 0;
1502     }
1503   else if (instrlen != 4 || instr[0] != 0x98)
1504     {
1505       return 0;
1506     }
1507   if ((instr[2] >> 4) != 0xf)
1508     return 0;
1509   if (regidx == 14)
1510     return 1;
1511   instrlen = s390_readinstruction (instr, pc - (GDB_TARGET_IS_ESAME ? 12 : 8));
1512   if (instrlen < 0)
1513     return -1;
1514   if (GDB_TARGET_IS_ESAME)
1515     {
1516       /* Check for LG */
1517       if (instrlen != 6 || instr[0] != 0xe3 || instr[5] != 0x4)
1518         return 0;
1519     }
1520   else
1521     {
1522       /* Check for L */
1523       if (instrlen != 4 || instr[0] != 0x58)
1524         return 0;
1525     }
1526   if (instr[2] >> 4 != 0xf)
1527     return 0;
1528   if (instr[1] >> 4 != regidx)
1529     return 0;
1530   return 1;
1531 }
1532
1533 static CORE_ADDR
1534 s390_sniff_pc_function_start (CORE_ADDR pc, struct frame_info *fi)
1535 {
1536   CORE_ADDR function_start, test_function_start;
1537   int loop_cnt, err, function_end;
1538   struct frame_extra_info fextra_info;
1539   function_start = get_pc_function_start (pc);
1540
1541   if (function_start == 0)
1542     {
1543       test_function_start = pc;
1544       if (test_function_start & 1)
1545         return 0;               /* This has to be bogus */
1546       loop_cnt = 0;
1547       do
1548         {
1549
1550           err =
1551             s390_get_frame_info (test_function_start, &fextra_info, fi, 1);
1552           loop_cnt++;
1553           test_function_start -= 2;
1554           function_end = s390_check_function_end (test_function_start);
1555         }
1556       while (!(function_end == 1 || err || loop_cnt >= 4096 ||
1557                (fextra_info.good_prologue)));
1558       if (fextra_info.good_prologue)
1559         function_start = fextra_info.function_start;
1560       else if (function_end == 1)
1561         function_start = test_function_start;
1562     }
1563   return function_start;
1564 }
1565
1566
1567 static int
1568 s390_frameless_function_invocation (struct frame_info *fi)
1569 {
1570   struct frame_extra_info fextra_info, *fextra_info_ptr;
1571   int frameless = 0;
1572
1573   if (get_next_frame (fi) == NULL)              /* no may be frameless */
1574     {
1575       if (get_frame_extra_info (fi))
1576         fextra_info_ptr = get_frame_extra_info (fi);
1577       else
1578         {
1579           fextra_info_ptr = &fextra_info;
1580           s390_get_frame_info (s390_sniff_pc_function_start (get_frame_pc (fi), fi),
1581                                fextra_info_ptr, fi, 1);
1582         }
1583       frameless = (fextra_info_ptr->stack_bought_valid
1584                    && fextra_info_ptr->stack_bought == 0);
1585     }
1586   return frameless;
1587
1588 }
1589
1590
1591 static int
1592 s390_is_sigreturn (CORE_ADDR pc, struct frame_info *sighandler_fi,
1593                    CORE_ADDR *sregs, CORE_ADDR *sigcaller_pc)
1594 {
1595   bfd_byte instr[S390_MAX_INSTR_SIZE];
1596   int instrlen;
1597   CORE_ADDR scontext;
1598   int retval = 0;
1599   CORE_ADDR orig_sp;
1600   CORE_ADDR temp_sregs;
1601
1602   scontext = temp_sregs = 0;
1603
1604   instrlen = s390_readinstruction (instr, pc);
1605   if (sigcaller_pc)
1606     *sigcaller_pc = 0;
1607   if (((instrlen == S390_SYSCALL_SIZE) &&
1608        (instr[0] == S390_SYSCALL_OPCODE)) &&
1609       ((instr[1] == s390_NR_sigreturn) || (instr[1] == s390_NR_rt_sigreturn)))
1610     {
1611       if (sighandler_fi)
1612         {
1613           if (s390_frameless_function_invocation (sighandler_fi))
1614             orig_sp = get_frame_base (sighandler_fi);
1615           else
1616             orig_sp = ADDR_BITS_REMOVE ((CORE_ADDR)
1617                                         read_memory_integer (get_frame_base (sighandler_fi),
1618                                                              S390_GPR_SIZE));
1619           if (orig_sp && sigcaller_pc)
1620             {
1621               scontext = orig_sp + S390_SIGNAL_FRAMESIZE;
1622               if (pc == scontext && instr[1] == s390_NR_rt_sigreturn)
1623                 {
1624                   /* We got a new style rt_signal */
1625                   /* get address of read ucontext->uc_mcontext */
1626                   temp_sregs = orig_sp + (GDB_TARGET_IS_ESAME ?
1627                                           S390X_UC_MCONTEXT_OFFSET :
1628                                           S390_UC_MCONTEXT_OFFSET);
1629                 }
1630               else
1631                 {
1632                   /* read sigcontext->sregs */
1633                   temp_sregs = ADDR_BITS_REMOVE ((CORE_ADDR)
1634                                                  read_memory_integer (scontext
1635                                                                       +
1636                                                                       (GDB_TARGET_IS_ESAME
1637                                                                        ?
1638                                                                        S390X_SIGCONTEXT_SREGS_OFFSET
1639                                                                        :
1640                                                                        S390_SIGCONTEXT_SREGS_OFFSET),
1641                                                                       S390_GPR_SIZE));
1642
1643                 }
1644               /* read sigregs->psw.addr */
1645               *sigcaller_pc =
1646                 ADDR_BITS_REMOVE ((CORE_ADDR)
1647                                   read_memory_integer (temp_sregs +
1648                                                        DEPRECATED_REGISTER_BYTE (S390_PC_REGNUM),
1649                                                        S390_PSW_ADDR_SIZE));
1650             }
1651         }
1652       retval = 1;
1653     }
1654   if (sregs)
1655     *sregs = temp_sregs;
1656   return retval;
1657 }
1658
1659 /*
1660   We need to do something better here but this will keep us out of trouble
1661   for the moment.
1662   For some reason the blockframe.c calls us with fi->next->fromleaf
1663   so this seems of little use to us. */
1664 static CORE_ADDR
1665 s390_init_frame_pc_first (int next_fromleaf, struct frame_info *fi)
1666 {
1667   CORE_ADDR sigcaller_pc;
1668   CORE_ADDR pc = 0;
1669   if (next_fromleaf)
1670     {
1671       pc = ADDR_BITS_REMOVE (read_register (S390_RETADDR_REGNUM));
1672       /* fix signal handlers */
1673     }
1674   else if (get_next_frame (fi) && get_frame_pc (get_next_frame (fi)))
1675     pc = s390_frame_saved_pc_nofix (get_next_frame (fi));
1676   if (pc && get_next_frame (fi) && get_frame_base (get_next_frame (fi))
1677       && s390_is_sigreturn (pc, get_next_frame (fi), NULL, &sigcaller_pc))
1678     {
1679       pc = sigcaller_pc;
1680     }
1681   return pc;
1682 }
1683
1684 static void
1685 s390_init_extra_frame_info (int fromleaf, struct frame_info *fi)
1686 {
1687   frame_extra_info_zalloc (fi, sizeof (struct frame_extra_info));
1688   if (get_frame_pc (fi))
1689     s390_get_frame_info (s390_sniff_pc_function_start (get_frame_pc (fi), fi),
1690                          get_frame_extra_info (fi), fi, 1);
1691   else
1692     s390_memset_extra_info (get_frame_extra_info (fi));
1693 }
1694
1695 /* If saved registers of frame FI are not known yet, read and cache them.
1696    &FEXTRA_INFOP contains struct frame_extra_info; TDATAP can be NULL,
1697    in which case the framedata are read.  */
1698
1699 static void
1700 s390_frame_init_saved_regs (struct frame_info *fi)
1701 {
1702
1703   int quick;
1704
1705   if (deprecated_get_frame_saved_regs (fi) == NULL)
1706     {
1707       /* zalloc memsets the saved regs */
1708       frame_saved_regs_zalloc (fi);
1709       if (get_frame_pc (fi))
1710         {
1711           quick = (get_frame_extra_info (fi)
1712                    && get_frame_extra_info (fi)->initialised
1713                    && get_frame_extra_info (fi)->good_prologue);
1714           s390_get_frame_info (quick
1715                                ? get_frame_extra_info (fi)->function_start
1716                                : s390_sniff_pc_function_start (get_frame_pc (fi), fi),
1717                                get_frame_extra_info (fi), fi, !quick);
1718         }
1719     }
1720 }
1721
1722
1723
1724 static CORE_ADDR
1725 s390_frame_saved_pc_nofix (struct frame_info *fi)
1726 {
1727   if (get_frame_extra_info (fi) && get_frame_extra_info (fi)->saved_pc_valid)
1728     return get_frame_extra_info (fi)->saved_pc;
1729
1730   if (deprecated_generic_find_dummy_frame (get_frame_pc (fi),
1731                                            get_frame_base (fi)))
1732     return deprecated_read_register_dummy (get_frame_pc (fi),
1733                                            get_frame_base (fi), S390_PC_REGNUM);
1734
1735   s390_frame_init_saved_regs (fi);
1736   if (get_frame_extra_info (fi))
1737     {
1738       get_frame_extra_info (fi)->saved_pc_valid = 1;
1739       if (get_frame_extra_info (fi)->good_prologue
1740           && deprecated_get_frame_saved_regs (fi)[S390_RETADDR_REGNUM])
1741         get_frame_extra_info (fi)->saved_pc
1742           = ADDR_BITS_REMOVE (read_memory_integer
1743                               (deprecated_get_frame_saved_regs (fi)[S390_RETADDR_REGNUM],
1744                                S390_GPR_SIZE));
1745       else
1746         get_frame_extra_info (fi)->saved_pc
1747           = ADDR_BITS_REMOVE (read_register (S390_RETADDR_REGNUM));
1748       return get_frame_extra_info (fi)->saved_pc;
1749     }
1750   return 0;
1751 }
1752
1753 static CORE_ADDR
1754 s390_frame_saved_pc (struct frame_info *fi)
1755 {
1756   CORE_ADDR saved_pc = 0, sig_pc;
1757
1758   if (get_frame_extra_info (fi)
1759       && get_frame_extra_info (fi)->sig_fixed_saved_pc_valid)
1760     return get_frame_extra_info (fi)->sig_fixed_saved_pc;
1761   saved_pc = s390_frame_saved_pc_nofix (fi);
1762
1763   if (get_frame_extra_info (fi))
1764     {
1765       get_frame_extra_info (fi)->sig_fixed_saved_pc_valid = 1;
1766       if (saved_pc)
1767         {
1768           if (s390_is_sigreturn (saved_pc, fi, NULL, &sig_pc))
1769             saved_pc = sig_pc;
1770         }
1771       get_frame_extra_info (fi)->sig_fixed_saved_pc = saved_pc;
1772     }
1773   return saved_pc;
1774 }
1775
1776
1777
1778
1779 /* We want backtraces out of signal handlers so we don't set
1780    (get_frame_type (thisframe) == SIGTRAMP_FRAME) to 1 */
1781
1782 static CORE_ADDR
1783 s390_frame_chain (struct frame_info *thisframe)
1784 {
1785   CORE_ADDR prev_fp = 0;
1786
1787   if (deprecated_generic_find_dummy_frame (get_frame_pc (thisframe),
1788                                            get_frame_base (thisframe)))
1789     return deprecated_read_register_dummy (get_frame_pc (thisframe),
1790                                            get_frame_base (thisframe),
1791                                            S390_SP_REGNUM);
1792   else
1793     {
1794       int sigreturn = 0;
1795       CORE_ADDR sregs = 0;
1796       struct frame_extra_info prev_fextra_info;
1797
1798       memset (&prev_fextra_info, 0, sizeof (prev_fextra_info));
1799       if (get_frame_pc (thisframe))
1800         {
1801           CORE_ADDR saved_pc, sig_pc;
1802
1803           saved_pc = s390_frame_saved_pc_nofix (thisframe);
1804           if (saved_pc)
1805             {
1806               if ((sigreturn =
1807                    s390_is_sigreturn (saved_pc, thisframe, &sregs, &sig_pc)))
1808                 saved_pc = sig_pc;
1809               s390_get_frame_info (s390_sniff_pc_function_start
1810                                    (saved_pc, NULL), &prev_fextra_info, NULL,
1811                                    1);
1812             }
1813         }
1814       if (sigreturn)
1815         {
1816           /* read sigregs,regs.gprs[11 or 15] */
1817           prev_fp = read_memory_integer (sregs +
1818                                          DEPRECATED_REGISTER_BYTE (S390_GP0_REGNUM +
1819                                                         (prev_fextra_info.
1820                                                          frame_pointer_saved_pc
1821                                                          ? 11 : 15)),
1822                                          S390_GPR_SIZE);
1823           get_frame_extra_info (thisframe)->sigcontext = sregs;
1824         }
1825       else
1826         {
1827           if (deprecated_get_frame_saved_regs (thisframe))
1828             {
1829               int regno;
1830
1831               if (prev_fextra_info.frame_pointer_saved_pc
1832                   && deprecated_get_frame_saved_regs (thisframe)[S390_FRAME_REGNUM])
1833                 regno = S390_FRAME_REGNUM;
1834               else
1835                 regno = S390_SP_REGNUM;
1836
1837               if (deprecated_get_frame_saved_regs (thisframe)[regno])
1838                 {
1839                   /* The SP's entry of `saved_regs' is special.  */
1840                   if (regno == S390_SP_REGNUM)
1841                     prev_fp = deprecated_get_frame_saved_regs (thisframe)[regno];
1842                   else
1843                     prev_fp =
1844                       read_memory_integer (deprecated_get_frame_saved_regs (thisframe)[regno],
1845                                            S390_GPR_SIZE);
1846                 }
1847             }
1848         }
1849     }
1850   return ADDR_BITS_REMOVE (prev_fp);
1851 }
1852
1853 /*
1854   Whether struct frame_extra_info is actually needed I'll have to figure
1855   out as our frames are similar to rs6000 there is a possibility
1856   i386 dosen't need it. */
1857
1858
1859
1860 /* NOTE: cagney/2003-10-31: "return_value" makes
1861    "extract_struct_value_address", "extract_return_value", and
1862    "use_struct_convention" redundant.  */
1863 static CORE_ADDR
1864 s390_cannot_extract_struct_value_address (struct regcache *regcache)
1865 {
1866   return 0;
1867 }
1868
1869 /* a given return value in `regbuf' with a type `valtype', extract and copy its
1870    value into `valbuf' */
1871 static void
1872 s390_extract_return_value (struct type *valtype, char *regbuf, char *valbuf)
1873 {
1874   /* floats and doubles are returned in fpr0. fpr's have a size of 8 bytes.
1875      We need to truncate the return value into float size (4 byte) if
1876      necessary. */
1877   int len = TYPE_LENGTH (valtype);
1878
1879   if (TYPE_CODE (valtype) == TYPE_CODE_FLT)
1880     memcpy (valbuf, &regbuf[DEPRECATED_REGISTER_BYTE (S390_FP0_REGNUM)], len);
1881   else
1882     {
1883       int offset = 0;
1884       /* return value is copied starting from r2. */
1885       if (TYPE_LENGTH (valtype) < S390_GPR_SIZE)
1886         offset = S390_GPR_SIZE - TYPE_LENGTH (valtype);
1887       memcpy (valbuf,
1888               regbuf + DEPRECATED_REGISTER_BYTE (S390_GP0_REGNUM + 2) + offset,
1889               TYPE_LENGTH (valtype));
1890     }
1891 }
1892
1893
1894 static char *
1895 s390_promote_integer_argument (struct type *valtype, char *valbuf,
1896                                char *reg_buff, int *arglen)
1897 {
1898   char *value = valbuf;
1899   int len = TYPE_LENGTH (valtype);
1900
1901   if (len < S390_GPR_SIZE)
1902     {
1903       /* We need to upgrade this value to a register to pass it correctly */
1904       int idx, diff = S390_GPR_SIZE - len, negative =
1905         (!TYPE_UNSIGNED (valtype) && value[0] & 0x80);
1906       for (idx = 0; idx < S390_GPR_SIZE; idx++)
1907         {
1908           reg_buff[idx] = (idx < diff ? (negative ? 0xff : 0x0) :
1909                            value[idx - diff]);
1910         }
1911       value = reg_buff;
1912       *arglen = S390_GPR_SIZE;
1913     }
1914   else
1915     {
1916       if (len & (S390_GPR_SIZE - 1))
1917         {
1918           fprintf_unfiltered (gdb_stderr,
1919                               "s390_promote_integer_argument detected an argument not "
1920                               "a multiple of S390_GPR_SIZE & greater than S390_GPR_SIZE "
1921                               "we might not deal with this correctly.\n");
1922         }
1923       *arglen = len;
1924     }
1925
1926   return (value);
1927 }
1928
1929 static void
1930 s390_store_return_value (struct type *valtype, char *valbuf)
1931 {
1932   int arglen;
1933   char *reg_buff = alloca (max (S390_FPR_SIZE, DEPRECATED_REGISTER_SIZE)), *value;
1934
1935   if (TYPE_CODE (valtype) == TYPE_CODE_FLT)
1936     {
1937       if (TYPE_LENGTH (valtype) == 4
1938           || TYPE_LENGTH (valtype) == 8)
1939         deprecated_write_register_bytes (DEPRECATED_REGISTER_BYTE (S390_FP0_REGNUM),
1940                                          valbuf, TYPE_LENGTH (valtype));
1941       else
1942         error ("GDB is unable to return `long double' values "
1943                "on this architecture.");
1944     }
1945   else
1946     {
1947       value =
1948         s390_promote_integer_argument (valtype, valbuf, reg_buff, &arglen);
1949       /* Everything else is returned in GPR2 and up. */
1950       deprecated_write_register_bytes (DEPRECATED_REGISTER_BYTE (S390_GP0_REGNUM + 2),
1951                                        value, arglen);
1952     }
1953 }
1954
1955
1956 /* Not the most efficent code in the world */
1957 static int
1958 s390_fp_regnum (void)
1959 {
1960   int regno = S390_SP_REGNUM;
1961   struct frame_extra_info fextra_info;
1962
1963   CORE_ADDR pc = ADDR_BITS_REMOVE (read_register (S390_PC_REGNUM));
1964
1965   s390_get_frame_info (s390_sniff_pc_function_start (pc, NULL), &fextra_info,
1966                        NULL, 1);
1967   if (fextra_info.frame_pointer_saved_pc)
1968     regno = S390_FRAME_REGNUM;
1969   return regno;
1970 }
1971
1972 static CORE_ADDR
1973 s390_read_fp (void)
1974 {
1975   return read_register (s390_fp_regnum ());
1976 }
1977
1978
1979 static void
1980 s390_pop_frame_regular (struct frame_info *frame)
1981 {
1982   int regnum;
1983
1984   write_register (S390_PC_REGNUM, DEPRECATED_FRAME_SAVED_PC (frame));
1985
1986   /* Restore any saved registers.  */
1987   if (deprecated_get_frame_saved_regs (frame))
1988     {
1989       for (regnum = 0; regnum < NUM_REGS; regnum++)
1990         if (deprecated_get_frame_saved_regs (frame)[regnum] != 0)
1991           {
1992             ULONGEST value;
1993             
1994             value = read_memory_unsigned_integer (deprecated_get_frame_saved_regs (frame)[regnum],
1995                                                   DEPRECATED_REGISTER_RAW_SIZE (regnum));
1996             write_register (regnum, value);
1997           }
1998
1999       /* Actually cut back the stack.  Remember that the SP's element of
2000          saved_regs is the old SP itself, not the address at which it is
2001          saved.  */
2002       write_register (S390_SP_REGNUM, deprecated_get_frame_saved_regs (frame)[S390_SP_REGNUM]);
2003     }
2004
2005   /* Throw away any cached frame information.  */
2006   flush_cached_frames ();
2007 }
2008
2009
2010 /* Destroy the innermost (Top-Of-Stack) stack frame, restoring the 
2011    machine state that was in effect before the frame was created. 
2012    Used in the contexts of the "return" command, and of 
2013    target function calls from the debugger.  */
2014 static void
2015 s390_pop_frame (void)
2016 {
2017   /* This function checks for and handles generic dummy frames, and
2018      calls back to our function for ordinary frames.  */
2019   generic_pop_current_frame (s390_pop_frame_regular);
2020 }
2021
2022
2023 /* Return non-zero if TYPE is an integer-like type, zero otherwise.
2024    "Integer-like" types are those that should be passed the way
2025    integers are: integers, enums, ranges, characters, and booleans.  */
2026 static int
2027 is_integer_like (struct type *type)
2028 {
2029   enum type_code code = TYPE_CODE (type);
2030
2031   return (code == TYPE_CODE_INT
2032           || code == TYPE_CODE_ENUM
2033           || code == TYPE_CODE_RANGE
2034           || code == TYPE_CODE_CHAR
2035           || code == TYPE_CODE_BOOL);
2036 }
2037
2038
2039 /* Return non-zero if TYPE is a pointer-like type, zero otherwise.
2040    "Pointer-like" types are those that should be passed the way
2041    pointers are: pointers and references.  */
2042 static int
2043 is_pointer_like (struct type *type)
2044 {
2045   enum type_code code = TYPE_CODE (type);
2046
2047   return (code == TYPE_CODE_PTR
2048           || code == TYPE_CODE_REF);
2049 }
2050
2051
2052 /* Return non-zero if TYPE is a `float singleton' or `double
2053    singleton', zero otherwise.
2054
2055    A `T singleton' is a struct type with one member, whose type is
2056    either T or a `T singleton'.  So, the following are all float
2057    singletons:
2058
2059    struct { float x };
2060    struct { struct { float x; } x; };
2061    struct { struct { struct { float x; } x; } x; };
2062
2063    ... and so on.
2064
2065    WHY THE HECK DO WE CARE ABOUT THIS???  Well, it turns out that GCC
2066    passes all float singletons and double singletons as if they were
2067    simply floats or doubles.  This is *not* what the ABI says it
2068    should do.  */
2069 static int
2070 is_float_singleton (struct type *type)
2071 {
2072   return (TYPE_CODE (type) == TYPE_CODE_STRUCT
2073           && TYPE_NFIELDS (type) == 1
2074           && (TYPE_CODE (TYPE_FIELD_TYPE (type, 0)) == TYPE_CODE_FLT
2075               || is_float_singleton (TYPE_FIELD_TYPE (type, 0))));
2076 }
2077
2078
2079 /* Return non-zero if TYPE is a struct-like type, zero otherwise.
2080    "Struct-like" types are those that should be passed as structs are:
2081    structs and unions.
2082
2083    As an odd quirk, not mentioned in the ABI, GCC passes float and
2084    double singletons as if they were a plain float, double, etc.  (The
2085    corresponding union types are handled normally.)  So we exclude
2086    those types here.  *shrug* */
2087 static int
2088 is_struct_like (struct type *type)
2089 {
2090   enum type_code code = TYPE_CODE (type);
2091
2092   return (code == TYPE_CODE_UNION
2093           || (code == TYPE_CODE_STRUCT && ! is_float_singleton (type)));
2094 }
2095
2096
2097 /* Return non-zero if TYPE is a float-like type, zero otherwise.
2098    "Float-like" types are those that should be passed as
2099    floating-point values are.
2100
2101    You'd think this would just be floats, doubles, long doubles, etc.
2102    But as an odd quirk, not mentioned in the ABI, GCC passes float and
2103    double singletons as if they were a plain float, double, etc.  (The
2104    corresponding union types are handled normally.)  So we include
2105    those types here.  *shrug* */
2106 static int
2107 is_float_like (struct type *type)
2108 {
2109   return (TYPE_CODE (type) == TYPE_CODE_FLT
2110           || is_float_singleton (type));
2111 }
2112
2113
2114 /* Return non-zero if TYPE is considered a `DOUBLE_OR_FLOAT', as
2115    defined by the parameter passing conventions described in the
2116    "GNU/Linux for S/390 ELF Application Binary Interface Supplement".
2117    Otherwise, return zero.  */
2118 static int
2119 is_double_or_float (struct type *type)
2120 {
2121   return (is_float_like (type)
2122           && (TYPE_LENGTH (type) == 4
2123               || TYPE_LENGTH (type) == 8));
2124 }
2125
2126
2127 /* Return non-zero if TYPE is a `DOUBLE_ARG', as defined by the
2128    parameter passing conventions described in the "GNU/Linux for S/390
2129    ELF Application Binary Interface Supplement".  Return zero
2130    otherwise.  */
2131 static int
2132 is_double_arg (struct type *type)
2133 {
2134   unsigned length = TYPE_LENGTH (type);
2135
2136   /* The s390x ABI doesn't handle DOUBLE_ARGS specially.  */
2137   if (GDB_TARGET_IS_ESAME)
2138     return 0;
2139
2140   return ((is_integer_like (type)
2141            || is_struct_like (type))
2142           && length == 8);
2143 }
2144
2145
2146 /* Return non-zero if TYPE is considered a `SIMPLE_ARG', as defined by
2147    the parameter passing conventions described in the "GNU/Linux for
2148    S/390 ELF Application Binary Interface Supplement".  Return zero
2149    otherwise.  */
2150 static int
2151 is_simple_arg (struct type *type)
2152 {
2153   unsigned length = TYPE_LENGTH (type);
2154
2155   /* This is almost a direct translation of the ABI's language, except
2156      that we have to exclude 8-byte structs; those are DOUBLE_ARGs.  */
2157   return ((is_integer_like (type) && length <= DEPRECATED_REGISTER_SIZE)
2158           || is_pointer_like (type)
2159           || (is_struct_like (type) && !is_double_arg (type)));
2160 }
2161
2162
2163 static int
2164 is_power_of_two (unsigned int n)
2165 {
2166   return ((n & (n - 1)) == 0);
2167 }
2168
2169 /* Return non-zero if TYPE should be passed as a pointer to a copy,
2170    zero otherwise.  TYPE must be a SIMPLE_ARG, as recognized by
2171    `is_simple_arg'.  */
2172 static int
2173 pass_by_copy_ref (struct type *type)
2174 {
2175   unsigned length = TYPE_LENGTH (type);
2176
2177   return (is_struct_like (type)
2178           && !(is_power_of_two (length) && length <= DEPRECATED_REGISTER_SIZE));
2179 }
2180
2181
2182 /* Return ARG, a `SIMPLE_ARG', sign-extended or zero-extended to a full
2183    word as required for the ABI.  */
2184 static LONGEST
2185 extend_simple_arg (struct value *arg)
2186 {
2187   struct type *type = VALUE_TYPE (arg);
2188
2189   /* Even structs get passed in the least significant bits of the
2190      register / memory word.  It's not really right to extract them as
2191      an integer, but it does take care of the extension.  */
2192   if (TYPE_UNSIGNED (type))
2193     return extract_unsigned_integer (VALUE_CONTENTS (arg),
2194                                      TYPE_LENGTH (type));
2195   else
2196     return extract_signed_integer (VALUE_CONTENTS (arg),
2197                                    TYPE_LENGTH (type));
2198 }
2199
2200
2201 /* Return the alignment required by TYPE.  */
2202 static int
2203 alignment_of (struct type *type)
2204 {
2205   int alignment;
2206
2207   if (is_integer_like (type)
2208       || is_pointer_like (type)
2209       || TYPE_CODE (type) == TYPE_CODE_FLT)
2210     alignment = TYPE_LENGTH (type);
2211   else if (TYPE_CODE (type) == TYPE_CODE_STRUCT
2212            || TYPE_CODE (type) == TYPE_CODE_UNION)
2213     {
2214       int i;
2215
2216       alignment = 1;
2217       for (i = 0; i < TYPE_NFIELDS (type); i++)
2218         {
2219           int field_alignment = alignment_of (TYPE_FIELD_TYPE (type, i));
2220
2221           if (field_alignment > alignment)
2222             alignment = field_alignment;
2223         }
2224     }
2225   else
2226     alignment = 1;
2227
2228   /* Check that everything we ever return is a power of two.  Lots of
2229      code doesn't want to deal with aligning things to arbitrary
2230      boundaries.  */
2231   gdb_assert ((alignment & (alignment - 1)) == 0);
2232
2233   return alignment;
2234 }
2235
2236
2237 /* Put the actual parameter values pointed to by ARGS[0..NARGS-1] in
2238    place to be passed to a function, as specified by the "GNU/Linux
2239    for S/390 ELF Application Binary Interface Supplement".
2240
2241    SP is the current stack pointer.  We must put arguments, links,
2242    padding, etc. whereever they belong, and return the new stack
2243    pointer value.
2244    
2245    If STRUCT_RETURN is non-zero, then the function we're calling is
2246    going to return a structure by value; STRUCT_ADDR is the address of
2247    a block we've allocated for it on the stack.
2248
2249    Our caller has taken care of any type promotions needed to satisfy
2250    prototypes or the old K&R argument-passing rules.  */
2251 static CORE_ADDR
2252 s390_push_arguments (int nargs, struct value **args, CORE_ADDR sp,
2253                      int struct_return, CORE_ADDR struct_addr)
2254 {
2255   int i;
2256   int pointer_size = (TARGET_PTR_BIT / TARGET_CHAR_BIT);
2257
2258   /* The number of arguments passed by reference-to-copy.  */
2259   int num_copies;
2260
2261   /* If the i'th argument is passed as a reference to a copy, then
2262      copy_addr[i] is the address of the copy we made.  */
2263   CORE_ADDR *copy_addr = alloca (nargs * sizeof (CORE_ADDR));
2264
2265   /* Build the reference-to-copy area.  */
2266   num_copies = 0;
2267   for (i = 0; i < nargs; i++)
2268     {
2269       struct value *arg = args[i];
2270       struct type *type = VALUE_TYPE (arg);
2271       unsigned length = TYPE_LENGTH (type);
2272
2273       if (is_simple_arg (type)
2274           && pass_by_copy_ref (type))
2275         {
2276           sp -= length;
2277           sp = align_down (sp, alignment_of (type));
2278           write_memory (sp, VALUE_CONTENTS (arg), length);
2279           copy_addr[i] = sp;
2280           num_copies++;
2281         }
2282     }
2283
2284   /* Reserve space for the parameter area.  As a conservative
2285      simplification, we assume that everything will be passed on the
2286      stack.  */
2287   {
2288     int i;
2289
2290     for (i = 0; i < nargs; i++)
2291       {
2292         struct value *arg = args[i];
2293         struct type *type = VALUE_TYPE (arg);
2294         int length = TYPE_LENGTH (type);
2295         
2296         sp = align_down (sp, alignment_of (type));
2297
2298         /* SIMPLE_ARG values get extended to DEPRECATED_REGISTER_SIZE bytes. 
2299            Assume every argument is.  */
2300         if (length < DEPRECATED_REGISTER_SIZE) length = DEPRECATED_REGISTER_SIZE;
2301         sp -= length;
2302       }
2303   }
2304
2305   /* Include space for any reference-to-copy pointers.  */
2306   sp = align_down (sp, pointer_size);
2307   sp -= num_copies * pointer_size;
2308     
2309   /* After all that, make sure it's still aligned on an eight-byte
2310      boundary.  */
2311   sp = align_down (sp, 8);
2312
2313   /* Finally, place the actual parameters, working from SP towards
2314      higher addresses.  The code above is supposed to reserve enough
2315      space for this.  */
2316   {
2317     int fr = 0;
2318     int gr = 2;
2319     CORE_ADDR starg = sp;
2320
2321     /* A struct is returned using general register 2 */
2322     if (struct_return)
2323       gr++;
2324
2325     for (i = 0; i < nargs; i++)
2326       {
2327         struct value *arg = args[i];
2328         struct type *type = VALUE_TYPE (arg);
2329         
2330         if (is_double_or_float (type)
2331             && fr <= S390_NUM_FP_PARAMETER_REGISTERS * 2 - 2)
2332           {
2333             /* When we store a single-precision value in an FP register,
2334                it occupies the leftmost bits.  */
2335             deprecated_write_register_bytes (DEPRECATED_REGISTER_BYTE (S390_FP0_REGNUM + fr),
2336                                              VALUE_CONTENTS (arg),
2337                                              TYPE_LENGTH (type));
2338             fr += 2;
2339           }
2340         else if (is_simple_arg (type)
2341                  && gr <= 6)
2342           {
2343             /* Do we need to pass a pointer to our copy of this
2344                argument?  */
2345             if (pass_by_copy_ref (type))
2346               write_register (S390_GP0_REGNUM + gr, copy_addr[i]);
2347             else
2348               write_register (S390_GP0_REGNUM + gr, extend_simple_arg (arg));
2349
2350             gr++;
2351           }
2352         else if (is_double_arg (type)
2353                  && gr <= 5)
2354           {
2355             deprecated_write_register_gen (S390_GP0_REGNUM + gr,
2356                                            VALUE_CONTENTS (arg));
2357             deprecated_write_register_gen (S390_GP0_REGNUM + gr + 1,
2358                                            VALUE_CONTENTS (arg) + DEPRECATED_REGISTER_SIZE);
2359             gr += 2;
2360           }
2361         else
2362           {
2363             /* The `OTHER' case.  */
2364             enum type_code code = TYPE_CODE (type);
2365             unsigned length = TYPE_LENGTH (type);
2366             
2367             /* If we skipped r6 because we couldn't fit a DOUBLE_ARG
2368                in it, then don't go back and use it again later.  */
2369             if (is_double_arg (type) && gr == 6)
2370               gr = 7;
2371
2372             if (is_simple_arg (type))
2373               {
2374                 /* Simple args are always extended to 
2375                    DEPRECATED_REGISTER_SIZE bytes.  */
2376                 starg = align_up (starg, DEPRECATED_REGISTER_SIZE);
2377
2378                 /* Do we need to pass a pointer to our copy of this
2379                    argument?  */
2380                 if (pass_by_copy_ref (type))
2381                   write_memory_signed_integer (starg, pointer_size,
2382                                                copy_addr[i]);
2383                 else
2384                   /* Simple args are always extended to 
2385                      DEPRECATED_REGISTER_SIZE bytes. */
2386                   write_memory_signed_integer (starg, DEPRECATED_REGISTER_SIZE,
2387                                                extend_simple_arg (arg));
2388                 starg += DEPRECATED_REGISTER_SIZE;
2389               }
2390             else
2391               {
2392                 /* You'd think we should say:
2393                    starg = align_up (starg, alignment_of (type));
2394                    Unfortunately, GCC seems to simply align the stack on
2395                    a four/eight-byte boundary, even when passing doubles. */
2396                 starg = align_up (starg, S390_STACK_PARAMETER_ALIGNMENT);
2397                 write_memory (starg, VALUE_CONTENTS (arg), length);
2398                 starg += length;
2399               }
2400           }
2401       }
2402   }
2403
2404   /* Allocate the standard frame areas: the register save area, the
2405      word reserved for the compiler (which seems kind of meaningless),
2406      and the back chain pointer.  */
2407   sp -= S390_STACK_FRAME_OVERHEAD;
2408
2409   /* Write the back chain pointer into the first word of the stack
2410      frame.  This will help us get backtraces from within functions
2411      called from GDB.  */
2412   write_memory_unsigned_integer (sp, (TARGET_PTR_BIT / TARGET_CHAR_BIT),
2413                                  deprecated_read_fp ());
2414
2415   return sp;
2416 }
2417
2418
2419 static CORE_ADDR
2420 s390_frame_align (struct gdbarch *gdbarch, CORE_ADDR addr)
2421 {
2422   /* Both the 32- and 64-bit ABI's say that the stack pointer should
2423      always be aligned on an eight-byte boundary.  */
2424   return (addr & -8);
2425 }
2426
2427
2428 static int
2429 s390_use_struct_convention (int gcc_p, struct type *value_type)
2430 {
2431   enum type_code code = TYPE_CODE (value_type);
2432
2433   return (code == TYPE_CODE_STRUCT
2434           || code == TYPE_CODE_UNION);
2435 }
2436
2437
2438 /* Return the GDB type object for the "standard" data type
2439    of data in register N.  */
2440 static struct type *
2441 s390_register_virtual_type (int regno)
2442 {
2443   if (S390_FP0_REGNUM <= regno && regno < S390_FP0_REGNUM + S390_NUM_FPRS)
2444     return builtin_type_double;
2445   else
2446     return builtin_type_int;
2447 }
2448
2449
2450 static struct type *
2451 s390x_register_virtual_type (int regno)
2452 {
2453   return (regno == S390_FPC_REGNUM) ||
2454     (regno >= S390_FIRST_ACR && regno <= S390_LAST_ACR) ? builtin_type_int :
2455     (regno >= S390_FP0_REGNUM) ? builtin_type_double : builtin_type_long;
2456 }
2457
2458
2459
2460 static void
2461 s390_store_struct_return (CORE_ADDR addr, CORE_ADDR sp)
2462 {
2463   write_register (S390_GP0_REGNUM + 2, addr);
2464 }
2465
2466
2467
2468 static const unsigned char *
2469 s390_breakpoint_from_pc (CORE_ADDR *pcptr, int *lenptr)
2470 {
2471   static unsigned char breakpoint[] = { 0x0, 0x1 };
2472
2473   *lenptr = sizeof (breakpoint);
2474   return breakpoint;
2475 }
2476
2477 /* Advance PC across any function entry prologue instructions to reach some
2478    "real" code.  */
2479 static CORE_ADDR
2480 s390_skip_prologue (CORE_ADDR pc)
2481 {
2482   struct frame_extra_info fextra_info;
2483
2484   s390_get_frame_info (pc, &fextra_info, NULL, 1);
2485   return fextra_info.skip_prologue_function_start;
2486 }
2487
2488 /* Immediately after a function call, return the saved pc.
2489    Can't go through the frames for this because on some machines
2490    the new frame is not set up until the new function executes
2491    some instructions.  */
2492 static CORE_ADDR
2493 s390_saved_pc_after_call (struct frame_info *frame)
2494 {
2495   return ADDR_BITS_REMOVE (read_register (S390_RETADDR_REGNUM));
2496 }
2497
2498 static CORE_ADDR
2499 s390_addr_bits_remove (CORE_ADDR addr)
2500 {
2501   return (addr) & 0x7fffffff;
2502 }
2503
2504
2505 static CORE_ADDR
2506 s390_push_return_address (CORE_ADDR pc, CORE_ADDR sp)
2507 {
2508   write_register (S390_RETADDR_REGNUM, entry_point_address ());
2509   return sp;
2510 }
2511
2512 static int
2513 s390_address_class_type_flags (int byte_size, int dwarf2_addr_class)
2514 {
2515   if (byte_size == 4)
2516     return TYPE_FLAG_ADDRESS_CLASS_1;
2517   else
2518     return 0;
2519 }
2520
2521 static const char *
2522 s390_address_class_type_flags_to_name (struct gdbarch *gdbarch, int type_flags)
2523 {
2524   if (type_flags & TYPE_FLAG_ADDRESS_CLASS_1)
2525     return "mode32";
2526   else
2527     return NULL;
2528 }
2529
2530 static int
2531 s390_address_class_name_to_type_flags (struct gdbarch *gdbarch, const char *name,
2532                                        int *type_flags_ptr)
2533 {
2534   if (strcmp (name, "mode32") == 0)
2535     {
2536       *type_flags_ptr = TYPE_FLAG_ADDRESS_CLASS_1;
2537       return 1;
2538     }
2539   else
2540     return 0;
2541 }
2542
2543 static struct gdbarch *
2544 s390_gdbarch_init (struct gdbarch_info info, struct gdbarch_list *arches)
2545 {
2546   static LONGEST s390_call_dummy_words[] = { 0 };
2547   struct gdbarch *gdbarch;
2548   struct gdbarch_tdep *tdep;
2549   int elf_flags;
2550
2551   /* First see if there is already a gdbarch that can satisfy the request.  */
2552   arches = gdbarch_list_lookup_by_info (arches, &info);
2553   if (arches != NULL)
2554     return arches->gdbarch;
2555
2556   /* None found: is the request for a s390 architecture? */
2557   if (info.bfd_arch_info->arch != bfd_arch_s390)
2558     return NULL;                /* No; then it's not for us.  */
2559
2560   /* Yes: create a new gdbarch for the specified machine type.  */
2561   gdbarch = gdbarch_alloc (&info, NULL);
2562
2563   /* NOTE: cagney/2002-12-06: This can be deleted when this arch is
2564      ready to unwind the PC first (see frame.c:get_prev_frame()).  */
2565   set_gdbarch_deprecated_init_frame_pc (gdbarch, deprecated_init_frame_pc_default);
2566
2567   set_gdbarch_believe_pcc_promotion (gdbarch, 0);
2568   set_gdbarch_char_signed (gdbarch, 0);
2569
2570   set_gdbarch_deprecated_frame_chain (gdbarch, s390_frame_chain);
2571   set_gdbarch_deprecated_frame_init_saved_regs (gdbarch, s390_frame_init_saved_regs);
2572   set_gdbarch_deprecated_store_struct_return (gdbarch, s390_store_struct_return);
2573   set_gdbarch_deprecated_extract_return_value (gdbarch, s390_extract_return_value);
2574   set_gdbarch_deprecated_store_return_value (gdbarch, s390_store_return_value);
2575   /* Amount PC must be decremented by after a breakpoint.  This is
2576      often the number of bytes returned by BREAKPOINT_FROM_PC but not
2577      always.  */
2578   set_gdbarch_decr_pc_after_break (gdbarch, 2);
2579   set_gdbarch_deprecated_pop_frame (gdbarch, s390_pop_frame);
2580   /* Stack grows downward.  */
2581   set_gdbarch_inner_than (gdbarch, core_addr_lessthan);
2582   set_gdbarch_deprecated_max_register_raw_size (gdbarch, 8);
2583   set_gdbarch_deprecated_max_register_virtual_size (gdbarch, 8);
2584   set_gdbarch_breakpoint_from_pc (gdbarch, s390_breakpoint_from_pc);
2585   set_gdbarch_skip_prologue (gdbarch, s390_skip_prologue);
2586   set_gdbarch_deprecated_init_extra_frame_info (gdbarch, s390_init_extra_frame_info);
2587   set_gdbarch_deprecated_init_frame_pc_first (gdbarch, s390_init_frame_pc_first);
2588   set_gdbarch_deprecated_target_read_fp (gdbarch, s390_read_fp);
2589   /* This function that tells us whether the function invocation represented
2590      by FI does not have a frame on the stack associated with it.  If it
2591      does not, FRAMELESS is set to 1, else 0.  */
2592   set_gdbarch_deprecated_frameless_function_invocation (gdbarch, s390_frameless_function_invocation);
2593   /* Return saved PC from a frame */
2594   set_gdbarch_deprecated_frame_saved_pc (gdbarch, s390_frame_saved_pc);
2595   /* DEPRECATED_FRAME_CHAIN takes a frame's nominal address and
2596      produces the frame's chain-pointer. */
2597   set_gdbarch_deprecated_frame_chain (gdbarch, s390_frame_chain);
2598   set_gdbarch_deprecated_saved_pc_after_call (gdbarch, s390_saved_pc_after_call);
2599   set_gdbarch_deprecated_register_byte (gdbarch, s390_register_byte);
2600   set_gdbarch_pc_regnum (gdbarch, S390_PC_REGNUM);
2601   set_gdbarch_sp_regnum (gdbarch, S390_SP_REGNUM);
2602   set_gdbarch_deprecated_fp_regnum (gdbarch, S390_FP_REGNUM);
2603   set_gdbarch_fp0_regnum (gdbarch, S390_FP0_REGNUM);
2604   set_gdbarch_num_regs (gdbarch, S390_NUM_REGS);
2605   set_gdbarch_cannot_fetch_register (gdbarch, s390_cannot_fetch_register);
2606   set_gdbarch_cannot_store_register (gdbarch, s390_cannot_fetch_register);
2607   set_gdbarch_use_struct_convention (gdbarch, s390_use_struct_convention);
2608   set_gdbarch_register_name (gdbarch, s390_register_name);
2609   set_gdbarch_stab_reg_to_regnum (gdbarch, s390_stab_reg_to_regnum);
2610   set_gdbarch_dwarf_reg_to_regnum (gdbarch, s390_stab_reg_to_regnum);
2611   set_gdbarch_dwarf2_reg_to_regnum (gdbarch, s390_stab_reg_to_regnum);
2612   set_gdbarch_deprecated_extract_struct_value_address (gdbarch, s390_cannot_extract_struct_value_address);
2613
2614   /* Parameters for inferior function calls.  */
2615   set_gdbarch_deprecated_pc_in_call_dummy (gdbarch, deprecated_pc_in_call_dummy_at_entry_point);
2616   set_gdbarch_frame_align (gdbarch, s390_frame_align);
2617   set_gdbarch_deprecated_push_arguments (gdbarch, s390_push_arguments);
2618   set_gdbarch_deprecated_save_dummy_frame_tos (gdbarch, generic_save_dummy_frame_tos);
2619   set_gdbarch_deprecated_push_return_address (gdbarch,
2620                                               s390_push_return_address);
2621   set_gdbarch_deprecated_sizeof_call_dummy_words (gdbarch, sizeof (s390_call_dummy_words));
2622   set_gdbarch_deprecated_call_dummy_words (gdbarch, s390_call_dummy_words);
2623
2624   switch (info.bfd_arch_info->mach)
2625     {
2626     case bfd_mach_s390_31:
2627       set_gdbarch_deprecated_register_size (gdbarch, 4);
2628       set_gdbarch_deprecated_register_raw_size (gdbarch, s390_register_raw_size);
2629       set_gdbarch_deprecated_register_virtual_size (gdbarch, s390_register_raw_size);
2630       set_gdbarch_deprecated_register_virtual_type (gdbarch, s390_register_virtual_type);
2631
2632       set_gdbarch_addr_bits_remove (gdbarch, s390_addr_bits_remove);
2633       set_gdbarch_deprecated_register_bytes (gdbarch, S390_REGISTER_BYTES);
2634       break;
2635     case bfd_mach_s390_64:
2636       set_gdbarch_deprecated_register_size (gdbarch, 8);
2637       set_gdbarch_deprecated_register_raw_size (gdbarch, s390x_register_raw_size);
2638       set_gdbarch_deprecated_register_virtual_size (gdbarch, s390x_register_raw_size);
2639       set_gdbarch_deprecated_register_virtual_type (gdbarch, s390x_register_virtual_type);
2640
2641       set_gdbarch_long_bit (gdbarch, 64);
2642       set_gdbarch_long_long_bit (gdbarch, 64);
2643       set_gdbarch_ptr_bit (gdbarch, 64);
2644       set_gdbarch_deprecated_register_bytes (gdbarch, S390X_REGISTER_BYTES);
2645       set_gdbarch_address_class_type_flags (gdbarch,
2646                                             s390_address_class_type_flags);
2647       set_gdbarch_address_class_type_flags_to_name (gdbarch,
2648                                                     s390_address_class_type_flags_to_name);
2649       set_gdbarch_address_class_name_to_type_flags (gdbarch,
2650                                                     s390_address_class_name_to_type_flags);
2651       break;
2652     }
2653
2654   /* Should be using push_dummy_call.  */
2655   set_gdbarch_deprecated_dummy_write_sp (gdbarch, deprecated_write_sp);
2656
2657   set_gdbarch_print_insn (gdbarch, print_insn_s390);
2658
2659   return gdbarch;
2660 }
2661
2662
2663
2664 extern initialize_file_ftype _initialize_s390_tdep; /* -Wmissing-prototypes */
2665
2666 void
2667 _initialize_s390_tdep (void)
2668 {
2669
2670   /* Hook us into the gdbarch mechanism.  */
2671   register_gdbarch_init (bfd_arch_s390, s390_gdbarch_init);
2672 }