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