2004-02-17 Ulrich Weigand <Ulrich.Weigand@de.ibm.com>
[external/binutils.git] / gdb / s390-tdep.c
1 /* Target-dependent code for GDB, the GNU debugger.
2
3    Copyright 2001, 2002, 2003, 2004 Free Software Foundation, Inc.
4
5    Contributed by D.J. Barrow (djbarrow@de.ibm.com,barrow_dj@yahoo.com)
6    for IBM Deutschland Entwicklung GmbH, IBM Corporation.
7
8    This file is part of GDB.
9
10    This program is free software; you can redistribute it and/or modify
11    it under the terms of the GNU General Public License as published by
12    the Free Software Foundation; either version 2 of the License, or
13    (at your option) any later version.
14
15    This program is distributed in the hope that it will be useful,
16    but WITHOUT ANY WARRANTY; without even the implied warranty of
17    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18    GNU General Public License for more details.
19
20    You should have received a copy of the GNU General Public License
21    along with this program; if not, write to the Free Software
22    Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
23    02111-1307, USA.  */
24
25 #include "defs.h"
26 #include "arch-utils.h"
27 #include "frame.h"
28 #include "inferior.h"
29 #include "symtab.h"
30 #include "target.h"
31 #include "gdbcore.h"
32 #include "gdbcmd.h"
33 #include "objfiles.h"
34 #include "tm.h"
35 #include "../bfd/bfd.h"
36 #include "floatformat.h"
37 #include "regcache.h"
38 #include "trad-frame.h"
39 #include "frame-base.h"
40 #include "frame-unwind.h"
41 #include "reggroups.h"
42 #include "regset.h"
43 #include "value.h"
44 #include "gdb_assert.h"
45 #include "dis-asm.h"
46
47 #include "s390-tdep.h"
48
49
50 /* The tdep structure.  */
51
52 struct gdbarch_tdep
53 {
54   /* ABI version.  */
55   enum { ABI_LINUX_S390, ABI_LINUX_ZSERIES } abi;
56
57   /* Core file register sets.  */
58   const struct regset *gregset;
59   int sizeof_gregset;
60
61   const struct regset *fpregset;
62   int sizeof_fpregset;
63 };
64
65
66 /* Register information.  */
67
68 struct s390_register_info
69 {
70   char *name;
71   struct type **type;
72 };
73
74 static struct s390_register_info s390_register_info[S390_NUM_TOTAL_REGS] = 
75 {
76   /* Program Status Word.  */
77   { "pswm", &builtin_type_long },
78   { "pswa", &builtin_type_long },
79
80   /* General Purpose Registers.  */
81   { "r0", &builtin_type_long },
82   { "r1", &builtin_type_long },
83   { "r2", &builtin_type_long },
84   { "r3", &builtin_type_long },
85   { "r4", &builtin_type_long },
86   { "r5", &builtin_type_long },
87   { "r6", &builtin_type_long },
88   { "r7", &builtin_type_long },
89   { "r8", &builtin_type_long },
90   { "r9", &builtin_type_long },
91   { "r10", &builtin_type_long },
92   { "r11", &builtin_type_long },
93   { "r12", &builtin_type_long },
94   { "r13", &builtin_type_long },
95   { "r14", &builtin_type_long },
96   { "r15", &builtin_type_long },
97
98   /* Access Registers.  */
99   { "acr0", &builtin_type_int },
100   { "acr1", &builtin_type_int },
101   { "acr2", &builtin_type_int },
102   { "acr3", &builtin_type_int },
103   { "acr4", &builtin_type_int },
104   { "acr5", &builtin_type_int },
105   { "acr6", &builtin_type_int },
106   { "acr7", &builtin_type_int },
107   { "acr8", &builtin_type_int },
108   { "acr9", &builtin_type_int },
109   { "acr10", &builtin_type_int },
110   { "acr11", &builtin_type_int },
111   { "acr12", &builtin_type_int },
112   { "acr13", &builtin_type_int },
113   { "acr14", &builtin_type_int },
114   { "acr15", &builtin_type_int },
115
116   /* Floating Point Control Word.  */
117   { "fpc", &builtin_type_int },
118
119   /* Floating Point Registers.  */
120   { "f0", &builtin_type_double },
121   { "f1", &builtin_type_double },
122   { "f2", &builtin_type_double },
123   { "f3", &builtin_type_double },
124   { "f4", &builtin_type_double },
125   { "f5", &builtin_type_double },
126   { "f6", &builtin_type_double },
127   { "f7", &builtin_type_double },
128   { "f8", &builtin_type_double },
129   { "f9", &builtin_type_double },
130   { "f10", &builtin_type_double },
131   { "f11", &builtin_type_double },
132   { "f12", &builtin_type_double },
133   { "f13", &builtin_type_double },
134   { "f14", &builtin_type_double },
135   { "f15", &builtin_type_double },
136
137   /* Pseudo registers.  */
138   { "pc", &builtin_type_void_func_ptr },
139   { "cc", &builtin_type_int },
140 };
141
142 /* Return the name of register REGNUM.  */
143 static const char *
144 s390_register_name (int regnum)
145 {
146   gdb_assert (regnum >= 0 && regnum < S390_NUM_TOTAL_REGS);
147   return s390_register_info[regnum].name;
148 }
149
150 /* Return the GDB type object for the "standard" data type of data in
151    register REGNUM. */
152 static struct type *
153 s390_register_type (struct gdbarch *gdbarch, int regnum)
154 {
155   gdb_assert (regnum >= 0 && regnum < S390_NUM_TOTAL_REGS);
156   return *s390_register_info[regnum].type;
157 }
158
159 /* DWARF Register Mapping.  */
160
161 static int s390_dwarf_regmap[] =
162 {
163   /* General Purpose Registers.  */
164   S390_R0_REGNUM, S390_R1_REGNUM, S390_R2_REGNUM, S390_R3_REGNUM,
165   S390_R4_REGNUM, S390_R5_REGNUM, S390_R6_REGNUM, S390_R7_REGNUM,
166   S390_R8_REGNUM, S390_R9_REGNUM, S390_R10_REGNUM, S390_R11_REGNUM,
167   S390_R12_REGNUM, S390_R13_REGNUM, S390_R14_REGNUM, S390_R15_REGNUM,
168
169   /* Floating Point Registers.  */
170   S390_F0_REGNUM, S390_F2_REGNUM, S390_F4_REGNUM, S390_F6_REGNUM,
171   S390_F1_REGNUM, S390_F3_REGNUM, S390_F5_REGNUM, S390_F7_REGNUM,
172   S390_F8_REGNUM, S390_F10_REGNUM, S390_F12_REGNUM, S390_F14_REGNUM,
173   S390_F9_REGNUM, S390_F11_REGNUM, S390_F13_REGNUM, S390_F15_REGNUM,
174
175   /* Control Registers (not mapped).  */
176   -1, -1, -1, -1, -1, -1, -1, -1, 
177   -1, -1, -1, -1, -1, -1, -1, -1, 
178
179   /* Access Registers.  */
180   S390_A0_REGNUM, S390_A1_REGNUM, S390_A2_REGNUM, S390_A3_REGNUM,
181   S390_A4_REGNUM, S390_A5_REGNUM, S390_A6_REGNUM, S390_A7_REGNUM,
182   S390_A8_REGNUM, S390_A9_REGNUM, S390_A10_REGNUM, S390_A11_REGNUM,
183   S390_A12_REGNUM, S390_A13_REGNUM, S390_A14_REGNUM, S390_A15_REGNUM,
184
185   /* Program Status Word.  */
186   S390_PSWM_REGNUM,
187   S390_PSWA_REGNUM
188 };
189
190 /* Convert DWARF register number REG to the appropriate register
191    number used by GDB.  */
192 static int
193 s390_dwarf_reg_to_regnum (int reg)
194 {
195   int regnum = -1;
196
197   if (reg >= 0 || reg < ARRAY_SIZE (s390_dwarf_regmap))
198     regnum = s390_dwarf_regmap[reg];
199
200   if (regnum == -1)
201     warning ("Unmapped DWARF Register #%d encountered\n", reg);
202
203   return regnum;
204 }
205
206 /* Pseudo registers - PC and condition code.  */
207
208 static void
209 s390_pseudo_register_read (struct gdbarch *gdbarch, struct regcache *regcache,
210                            int regnum, void *buf)
211 {
212   ULONGEST val;
213
214   switch (regnum)
215     {
216     case S390_PC_REGNUM:
217       regcache_raw_read_unsigned (regcache, S390_PSWA_REGNUM, &val);
218       store_unsigned_integer (buf, 4, val & 0x7fffffff);
219       break;
220
221     case S390_CC_REGNUM:
222       regcache_raw_read_unsigned (regcache, S390_PSWM_REGNUM, &val);
223       store_unsigned_integer (buf, 4, (val >> 12) & 3);
224       break;
225
226     default:
227       internal_error (__FILE__, __LINE__, "invalid regnum");
228     }
229 }
230
231 static void
232 s390_pseudo_register_write (struct gdbarch *gdbarch, struct regcache *regcache,
233                             int regnum, const void *buf)
234 {
235   ULONGEST val, psw;
236
237   switch (regnum)
238     {
239     case S390_PC_REGNUM:
240       val = extract_unsigned_integer (buf, 4);
241       regcache_raw_read_unsigned (regcache, S390_PSWA_REGNUM, &psw);
242       psw = (psw & 0x80000000) | (val & 0x7fffffff);
243       regcache_raw_write_unsigned (regcache, S390_PSWA_REGNUM, psw);
244       break;
245
246     case S390_CC_REGNUM:
247       val = extract_unsigned_integer (buf, 4);
248       regcache_raw_read_unsigned (regcache, S390_PSWM_REGNUM, &psw);
249       psw = (psw & ~((ULONGEST)3 << 12)) | ((val & 3) << 12);
250       regcache_raw_write_unsigned (regcache, S390_PSWM_REGNUM, psw);
251       break;
252
253     default:
254       internal_error (__FILE__, __LINE__, "invalid regnum");
255     }
256 }
257
258 static void
259 s390x_pseudo_register_read (struct gdbarch *gdbarch, struct regcache *regcache,
260                             int regnum, void *buf)
261 {
262   ULONGEST val;
263
264   switch (regnum)
265     {
266     case S390_PC_REGNUM:
267       regcache_raw_read (regcache, S390_PSWA_REGNUM, buf);
268       break;
269
270     case S390_CC_REGNUM:
271       regcache_raw_read_unsigned (regcache, S390_PSWM_REGNUM, &val);
272       store_unsigned_integer (buf, 4, (val >> 44) & 3);
273       break;
274
275     default:
276       internal_error (__FILE__, __LINE__, "invalid regnum");
277     }
278 }
279
280 static void
281 s390x_pseudo_register_write (struct gdbarch *gdbarch, struct regcache *regcache,
282                              int regnum, const void *buf)
283 {
284   ULONGEST val, psw;
285
286   switch (regnum)
287     {
288     case S390_PC_REGNUM:
289       regcache_raw_write (regcache, S390_PSWA_REGNUM, buf);
290       break;
291
292     case S390_CC_REGNUM:
293       val = extract_unsigned_integer (buf, 4);
294       regcache_raw_read_unsigned (regcache, S390_PSWM_REGNUM, &psw);
295       psw = (psw & ~((ULONGEST)3 << 44)) | ((val & 3) << 44);
296       regcache_raw_write_unsigned (regcache, S390_PSWM_REGNUM, psw);
297       break;
298
299     default:
300       internal_error (__FILE__, __LINE__, "invalid regnum");
301     }
302 }
303
304 /* 'float' values are stored in the upper half of floating-point
305    registers, even though we are otherwise a big-endian platform.  */
306
307 static int
308 s390_convert_register_p (int regno, struct type *type)
309 {
310   return (regno >= S390_F0_REGNUM && regno <= S390_F15_REGNUM)
311          && TYPE_LENGTH (type) < 8;
312 }
313
314 static void
315 s390_register_to_value (struct frame_info *frame, int regnum,
316                         struct type *valtype, void *out)
317 {
318   char in[8];
319   int len = TYPE_LENGTH (valtype);
320   gdb_assert (len < 8);
321
322   get_frame_register (frame, regnum, in);
323   memcpy (out, in, len);
324 }
325
326 static void
327 s390_value_to_register (struct frame_info *frame, int regnum,
328                         struct type *valtype, const void *in)
329 {
330   char out[8];
331   int len = TYPE_LENGTH (valtype);
332   gdb_assert (len < 8);
333
334   memset (out, 0, 8);
335   memcpy (out, in, len);
336   put_frame_register (frame, regnum, out);
337 }
338
339 /* Register groups.  */
340
341 static int
342 s390_register_reggroup_p (struct gdbarch *gdbarch, int regnum,
343                           struct reggroup *group)
344 {
345   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
346
347   /* Registers displayed via 'info regs'.  */
348   if (group == general_reggroup)
349     return (regnum >= S390_R0_REGNUM && regnum <= S390_R15_REGNUM)
350            || regnum == S390_PC_REGNUM
351            || regnum == S390_CC_REGNUM;
352
353   /* Registers displayed via 'info float'.  */
354   if (group == float_reggroup)
355     return (regnum >= S390_F0_REGNUM && regnum <= S390_F15_REGNUM)
356            || regnum == S390_FPC_REGNUM;
357
358   /* Registers that need to be saved/restored in order to
359      push or pop frames.  */
360   if (group == save_reggroup || group == restore_reggroup)
361     return regnum != S390_PSWM_REGNUM && regnum != S390_PSWA_REGNUM;
362
363   return default_register_reggroup_p (gdbarch, regnum, group);
364 }
365
366
367 /* Core file register sets.  */
368
369 int s390_regmap_gregset[S390_NUM_REGS] =
370 {
371   /* Program Status Word.  */
372   0x00, 0x04,
373   /* General Purpose Registers.  */
374   0x08, 0x0c, 0x10, 0x14,
375   0x18, 0x1c, 0x20, 0x24,
376   0x28, 0x2c, 0x30, 0x34,
377   0x38, 0x3c, 0x40, 0x44,
378   /* Access Registers.  */
379   0x48, 0x4c, 0x50, 0x54,
380   0x58, 0x5c, 0x60, 0x64,
381   0x68, 0x6c, 0x70, 0x74,
382   0x78, 0x7c, 0x80, 0x84,
383   /* Floating Point Control Word.  */
384   -1,
385   /* Floating Point Registers.  */
386   -1, -1, -1, -1, -1, -1, -1, -1,
387   -1, -1, -1, -1, -1, -1, -1, -1,
388 };
389
390 int s390x_regmap_gregset[S390_NUM_REGS] =
391 {
392   0x00, 0x08,
393   /* General Purpose Registers.  */
394   0x10, 0x18, 0x20, 0x28,
395   0x30, 0x38, 0x40, 0x48,
396   0x50, 0x58, 0x60, 0x68,
397   0x70, 0x78, 0x80, 0x88,
398   /* Access Registers.  */
399   0x90, 0x94, 0x98, 0x9c,
400   0xa0, 0xa4, 0xa8, 0xac,
401   0xb0, 0xb4, 0xb8, 0xbc,
402   0xc0, 0xc4, 0xc8, 0xcc,
403   /* Floating Point Control Word.  */
404   -1,
405   /* Floating Point Registers.  */
406   -1, -1, -1, -1, -1, -1, -1, -1,
407   -1, -1, -1, -1, -1, -1, -1, -1,
408 };
409
410 int s390_regmap_fpregset[S390_NUM_REGS] =
411 {
412   /* Program Status Word.  */
413   -1, -1,
414   /* General Purpose Registers.  */
415   -1, -1, -1, -1, -1, -1, -1, -1,
416   -1, -1, -1, -1, -1, -1, -1, -1,
417   /* Access Registers.  */
418   -1, -1, -1, -1, -1, -1, -1, -1,
419   -1, -1, -1, -1, -1, -1, -1, -1,
420   /* Floating Point Control Word.  */
421   0x00,
422   /* Floating Point Registers.  */
423   0x08, 0x10, 0x18, 0x20,
424   0x28, 0x30, 0x38, 0x40,
425   0x48, 0x50, 0x58, 0x60,
426   0x68, 0x70, 0x78, 0x80,
427 };
428
429 /* Supply register REGNUM from the register set REGSET to register cache 
430    REGCACHE.  If REGNUM is -1, do this for all registers in REGSET.  */
431 static void
432 s390_supply_regset (const struct regset *regset, struct regcache *regcache,
433                     int regnum, const void *regs, size_t len)
434 {
435   const int *offset = regset->descr;
436   int i;
437
438   for (i = 0; i < S390_NUM_REGS; i++)
439     {
440       if ((regnum == i || regnum == -1) && offset[i] != -1)
441         regcache_raw_supply (regcache, i, (const char *)regs + offset[i]);
442     }
443 }
444
445 static const struct regset s390_gregset = {
446   s390_regmap_gregset, 
447   s390_supply_regset
448 };
449
450 static const struct regset s390x_gregset = {
451   s390x_regmap_gregset, 
452   s390_supply_regset
453 };
454
455 static const struct regset s390_fpregset = {
456   s390_regmap_fpregset, 
457   s390_supply_regset
458 };
459
460 /* Return the appropriate register set for the core section identified
461    by SECT_NAME and SECT_SIZE.  */
462 const struct regset *
463 s390_regset_from_core_section (struct gdbarch *gdbarch,
464                                const char *sect_name, size_t sect_size)
465 {
466   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
467
468   if (strcmp (sect_name, ".reg") == 0 && sect_size == tdep->sizeof_gregset)
469     return tdep->gregset;
470
471   if (strcmp (sect_name, ".reg2") == 0 && sect_size == tdep->sizeof_fpregset)
472     return tdep->fpregset;
473
474   return NULL;
475 }
476
477
478 /* Prologue analysis.  */
479
480 /* When we analyze a prologue, we're really doing 'abstract
481    interpretation' or 'pseudo-evaluation': running the function's code
482    in simulation, but using conservative approximations of the values
483    it would have when it actually runs.  For example, if our function
484    starts with the instruction:
485
486       ahi r1, 42     # add halfword immediate 42 to r1
487
488    we don't know exactly what value will be in r1 after executing this
489    instruction, but we do know it'll be 42 greater than its original
490    value.
491
492    If we then see an instruction like:
493
494       ahi r1, 22     # add halfword immediate 22 to r1
495
496    we still don't know what r1's value is, but again, we can say it is
497    now 64 greater than its original value.
498
499    If the next instruction were:
500
501       lr r2, r1      # set r2 to r1's value
502
503    then we can say that r2's value is now the original value of r1
504    plus 64.  And so on.
505
506    Of course, this can only go so far before it gets unreasonable.  If
507    we wanted to be able to say anything about the value of r1 after
508    the instruction:
509
510       xr r1, r3      # exclusive-or r1 and r3, place result in r1
511
512    then things would get pretty complex.  But remember, we're just
513    doing a conservative approximation; if exclusive-or instructions
514    aren't relevant to prologues, we can just say r1's value is now
515    'unknown'.  We can ignore things that are too complex, if that loss
516    of information is acceptable for our application.
517
518    Once you've reached an instruction that you don't know how to
519    simulate, you stop.  Now you examine the state of the registers and
520    stack slots you've kept track of.  For example:
521
522    - To see how large your stack frame is, just check the value of sp;
523      if it's the original value of sp minus a constant, then that
524      constant is the stack frame's size.  If the sp's value has been
525      marked as 'unknown', then that means the prologue has done
526      something too complex for us to track, and we don't know the
527      frame size.
528
529    - To see whether we've saved the SP in the current frame's back
530      chain slot, we just check whether the current value of the back
531      chain stack slot is the original value of the sp.
532
533    Sure, this takes some work.  But prologue analyzers aren't
534    quick-and-simple pattern patching to recognize a few fixed prologue
535    forms any more; they're big, hairy functions.  Along with inferior
536    function calls, prologue analysis accounts for a substantial
537    portion of the time needed to stabilize a GDB port.  So I think
538    it's worthwhile to look for an approach that will be easier to
539    understand and maintain.  In the approach used here:
540
541    - It's easier to see that the analyzer is correct: you just see
542      whether the analyzer properly (albiet conservatively) simulates
543      the effect of each instruction.
544
545    - It's easier to extend the analyzer: you can add support for new
546      instructions, and know that you haven't broken anything that
547      wasn't already broken before.
548
549    - It's orthogonal: to gather new information, you don't need to
550      complicate the code for each instruction.  As long as your domain
551      of conservative values is already detailed enough to tell you
552      what you need, then all the existing instruction simulations are
553      already gathering the right data for you.
554
555    A 'struct prologue_value' is a conservative approximation of the
556    real value the register or stack slot will have.  */
557
558 struct prologue_value {
559
560   /* What sort of value is this?  This determines the interpretation
561      of subsequent fields.  */
562   enum {
563
564     /* We don't know anything about the value.  This is also used for
565        values we could have kept track of, when doing so would have
566        been too complex and we don't want to bother.  The bottom of
567        our lattice.  */
568     pv_unknown,
569
570     /* A known constant.  K is its value.  */
571     pv_constant,
572
573     /* The value that register REG originally had *UPON ENTRY TO THE
574        FUNCTION*, plus K.  If K is zero, this means, obviously, just
575        the value REG had upon entry to the function.  REG is a GDB
576        register number.  Before we start interpreting, we initialize
577        every register R to { pv_register, R, 0 }.  */
578     pv_register,
579
580   } kind;
581
582   /* The meanings of the following fields depend on 'kind'; see the
583      comments for the specific 'kind' values.  */
584   int reg;
585   CORE_ADDR k;
586 };
587
588
589 /* Set V to be unknown.  */
590 static void
591 pv_set_to_unknown (struct prologue_value *v)
592 {
593   v->kind = pv_unknown;
594 }
595
596
597 /* Set V to the constant K.  */
598 static void
599 pv_set_to_constant (struct prologue_value *v, CORE_ADDR k)
600 {
601   v->kind = pv_constant;
602   v->k = k;
603 }
604
605
606 /* Set V to the original value of register REG, plus K.  */
607 static void
608 pv_set_to_register (struct prologue_value *v, int reg, CORE_ADDR k)
609 {
610   v->kind = pv_register;
611   v->reg = reg;
612   v->k = k;
613 }
614
615
616 /* If one of *A and *B is a constant, and the other isn't, swap the
617    pointers as necessary to ensure that *B points to the constant.
618    This can reduce the number of cases we need to analyze in the
619    functions below.  */
620 static void
621 pv_constant_last (struct prologue_value **a,
622                   struct prologue_value **b)
623 {
624   if ((*a)->kind == pv_constant
625       && (*b)->kind != pv_constant)
626     {
627       struct prologue_value *temp = *a;
628       *a = *b;
629       *b = temp;
630     }
631 }
632
633
634 /* Set SUM to the sum of A and B.  SUM, A, and B may point to the same
635    'struct prologue_value' object.  */
636 static void
637 pv_add (struct prologue_value *sum,
638         struct prologue_value *a,
639         struct prologue_value *b)
640 {
641   pv_constant_last (&a, &b);
642
643   /* We can handle adding constants to registers, and other constants.  */
644   if (b->kind == pv_constant
645       && (a->kind == pv_register
646           || a->kind == pv_constant))
647     {
648       sum->kind = a->kind;
649       sum->reg = a->reg;    /* not meaningful if a is pv_constant, but
650                                harmless */
651       sum->k = a->k + b->k;
652     }
653
654   /* Anything else we don't know how to add.  We don't have a
655      representation for, say, the sum of two registers, or a multiple
656      of a register's value (adding a register to itself).  */
657   else
658     sum->kind = pv_unknown;
659 }
660
661
662 /* Add the constant K to V.  */
663 static void
664 pv_add_constant (struct prologue_value *v, CORE_ADDR k)
665 {
666   struct prologue_value pv_k;
667
668   /* Rather than thinking of all the cases we can and can't handle,
669      we'll just let pv_add take care of that for us.  */
670   pv_set_to_constant (&pv_k, k);
671   pv_add (v, v, &pv_k);
672 }
673
674
675 /* Subtract B from A, and put the result in DIFF.
676
677    This isn't quite the same as negating B and adding it to A, since
678    we don't have a representation for the negation of anything but a
679    constant.  For example, we can't negate { pv_register, R1, 10 },
680    but we do know that { pv_register, R1, 10 } minus { pv_register,
681    R1, 5 } is { pv_constant, <ignored>, 5 }.
682
683    This means, for example, that we can subtract two stack addresses;
684    they're both relative to the original SP.  Since the frame pointer
685    is set based on the SP, its value will be the original SP plus some
686    constant (probably zero), so we can use its value just fine.  */
687 static void
688 pv_subtract (struct prologue_value *diff,
689              struct prologue_value *a,
690              struct prologue_value *b)
691 {
692   pv_constant_last (&a, &b);
693
694   /* We can subtract a constant from another constant, or from a
695      register.  */
696   if (b->kind == pv_constant
697       && (a->kind == pv_register
698           || a->kind == pv_constant))
699     {
700       diff->kind = a->kind;
701       diff->reg = a->reg;    /* not always meaningful, but harmless */
702       diff->k = a->k - b->k;
703     }
704
705   /* We can subtract a register from itself, yielding a constant.  */
706   else if (a->kind == pv_register
707            && b->kind == pv_register
708            && a->reg == b->reg)
709     {
710       diff->kind = pv_constant;
711       diff->k = a->k - b->k;
712     }
713
714   /* We don't know how to subtract anything else.  */
715   else
716     diff->kind = pv_unknown;
717 }
718
719
720 /* Set AND to the logical and of A and B.  */
721 static void
722 pv_logical_and (struct prologue_value *and,
723                 struct prologue_value *a,
724                 struct prologue_value *b)
725 {
726   pv_constant_last (&a, &b);
727
728   /* We can 'and' two constants.  */
729   if (a->kind == pv_constant
730       && b->kind == pv_constant)
731     {
732       and->kind = pv_constant;
733       and->k = a->k & b->k;
734     }
735
736   /* We can 'and' anything with the constant zero.  */
737   else if (b->kind == pv_constant
738            && b->k == 0)
739     {
740       and->kind = pv_constant;
741       and->k = 0;
742     }
743   
744   /* We can 'and' anything with ~0.  */
745   else if (b->kind == pv_constant
746            && b->k == ~ (CORE_ADDR) 0)
747     *and = *a;
748
749   /* We can 'and' a register with itself.  */
750   else if (a->kind == pv_register
751            && b->kind == pv_register
752            && a->reg == b->reg
753            && a->k == b->k)
754     *and = *a;
755
756   /* Otherwise, we don't know.  */
757   else
758     pv_set_to_unknown (and);
759 }
760
761
762 /* Return non-zero iff A and B are identical expressions.
763
764    This is not the same as asking if the two values are equal; the
765    result of such a comparison would have to be a pv_boolean, and
766    asking whether two 'unknown' values were equal would give you
767    pv_maybe.  Same for comparing, say, { pv_register, R1, 0 } and {
768    pv_register, R2, 0}.  Instead, this is asking whether the two
769    representations are the same.  */
770 static int
771 pv_is_identical (struct prologue_value *a,
772                  struct prologue_value *b)
773 {
774   if (a->kind != b->kind)
775     return 0;
776
777   switch (a->kind)
778     {
779     case pv_unknown:
780       return 1;
781     case pv_constant:
782       return (a->k == b->k);
783     case pv_register:
784       return (a->reg == b->reg && a->k == b->k);
785     default:
786       gdb_assert (0);
787     }
788 }
789
790
791 /* Return non-zero if A is the original value of register number R
792    plus K, zero otherwise.  */
793 static int
794 pv_is_register (struct prologue_value *a, int r, CORE_ADDR k)
795 {
796   return (a->kind == pv_register
797           && a->reg == r
798           && a->k == k);
799 }
800
801
802 /* A prologue-value-esque boolean type, including "maybe", when we
803    can't figure out whether something is true or not.  */
804 enum pv_boolean {
805   pv_maybe,
806   pv_definite_yes,
807   pv_definite_no,
808 };
809
810
811 /* Decide whether a reference to SIZE bytes at ADDR refers exactly to
812    an element of an array.  The array starts at ARRAY_ADDR, and has
813    ARRAY_LEN values of ELT_SIZE bytes each.  If ADDR definitely does
814    refer to an array element, set *I to the index of the referenced
815    element in the array, and return pv_definite_yes.  If it definitely
816    doesn't, return pv_definite_no.  If we can't tell, return pv_maybe.
817
818    If the reference does touch the array, but doesn't fall exactly on
819    an element boundary, or doesn't refer to the whole element, return
820    pv_maybe.  */
821 static enum pv_boolean
822 pv_is_array_ref (struct prologue_value *addr,
823                  CORE_ADDR size,
824                  struct prologue_value *array_addr,
825                  CORE_ADDR array_len, 
826                  CORE_ADDR elt_size,
827                  int *i)
828 {
829   struct prologue_value offset;
830
831   /* Note that, since ->k is a CORE_ADDR, and CORE_ADDR is unsigned,
832      if addr is *before* the start of the array, then this isn't going
833      to be negative...  */
834   pv_subtract (&offset, addr, array_addr);
835
836   if (offset.kind == pv_constant)
837     {
838       /* This is a rather odd test.  We want to know if the SIZE bytes
839          at ADDR don't overlap the array at all, so you'd expect it to
840          be an || expression: "if we're completely before || we're
841          completely after".  But with unsigned arithmetic, things are
842          different: since it's a number circle, not a number line, the
843          right values for offset.k are actually one contiguous range.  */
844       if (offset.k <= -size
845           && offset.k >= array_len * elt_size)
846         return pv_definite_no;
847       else if (offset.k % elt_size != 0
848                || size != elt_size)
849         return pv_maybe;
850       else
851         {
852           *i = offset.k / elt_size;
853           return pv_definite_yes;
854         }
855     }
856   else
857     return pv_maybe;
858 }
859
860
861
862 /* Decoding S/390 instructions.  */
863
864 /* Named opcode values for the S/390 instructions we recognize.  Some
865    instructions have their opcode split across two fields; those are the
866    op1_* and op2_* enums.  */
867 enum
868   {
869     op1_lhi  = 0xa7,   op2_lhi  = 0x08,
870     op1_lghi = 0xa7,   op2_lghi = 0x09,
871     op_lr    = 0x18,
872     op_lgr   = 0xb904,
873     op_l     = 0x58,
874     op1_ly   = 0xe3,   op2_ly   = 0x58,
875     op1_lg   = 0xe3,   op2_lg   = 0x04,
876     op_lm    = 0x98,
877     op1_lmy  = 0xeb,   op2_lmy  = 0x98,
878     op1_lmg  = 0xeb,   op2_lmg  = 0x04,
879     op_st    = 0x50,
880     op1_sty  = 0xe3,   op2_sty  = 0x50,
881     op1_stg  = 0xe3,   op2_stg  = 0x24,
882     op_std   = 0x60,
883     op_stm   = 0x90,
884     op1_stmy = 0xeb,   op2_stmy = 0x90,
885     op1_stmg = 0xeb,   op2_stmg = 0x24,
886     op1_aghi = 0xa7,   op2_aghi = 0x0b,
887     op1_ahi  = 0xa7,   op2_ahi  = 0x0a,
888     op_ar    = 0x1a,
889     op_agr   = 0xb908,
890     op_a     = 0x5a,
891     op1_ay   = 0xe3,   op2_ay   = 0x5a,
892     op1_ag   = 0xe3,   op2_ag   = 0x08,
893     op_sr    = 0x1b,
894     op_sgr   = 0xb909,
895     op_s     = 0x5b,
896     op1_sy   = 0xe3,   op2_sy   = 0x5b,
897     op1_sg   = 0xe3,   op2_sg   = 0x09,
898     op_nr    = 0x14,
899     op_ngr   = 0xb980,
900     op_la    = 0x41,
901     op1_lay  = 0xe3,   op2_lay  = 0x71,
902     op1_larl = 0xc0,   op2_larl = 0x00,
903     op_basr  = 0x0d,
904     op_bas   = 0x4d,
905     op_bcr   = 0x07,
906     op_bc    = 0x0d,
907     op1_bras = 0xa7,   op2_bras = 0x05,
908     op1_brasl= 0xc0,   op2_brasl= 0x05,
909     op1_brc  = 0xa7,   op2_brc  = 0x04,
910     op1_brcl = 0xc0,   op2_brcl = 0x04,
911   };
912
913
914 /* Read a single instruction from address AT.  */
915
916 #define S390_MAX_INSTR_SIZE 6
917 static int
918 s390_readinstruction (bfd_byte instr[], CORE_ADDR at)
919 {
920   static int s390_instrlen[] = { 2, 4, 4, 6 };
921   int instrlen;
922
923   if (read_memory_nobpt (at, &instr[0], 2))
924     return -1;
925   instrlen = s390_instrlen[instr[0] >> 6];
926   if (instrlen > 2)
927     {
928       if (read_memory_nobpt (at + 2, &instr[2], instrlen - 2))
929         return -1;
930     }
931   return instrlen;
932 }
933
934
935 /* The functions below are for recognizing and decoding S/390
936    instructions of various formats.  Each of them checks whether INSN
937    is an instruction of the given format, with the specified opcodes.
938    If it is, it sets the remaining arguments to the values of the
939    instruction's fields, and returns a non-zero value; otherwise, it
940    returns zero.
941
942    These functions' arguments appear in the order they appear in the
943    instruction, not in the machine-language form.  So, opcodes always
944    come first, even though they're sometimes scattered around the
945    instructions.  And displacements appear before base and extension
946    registers, as they do in the assembly syntax, not at the end, as
947    they do in the machine language.  */
948 static int
949 is_ri (bfd_byte *insn, int op1, int op2, unsigned int *r1, int *i2)
950 {
951   if (insn[0] == op1 && (insn[1] & 0xf) == op2)
952     {
953       *r1 = (insn[1] >> 4) & 0xf;
954       /* i2 is a 16-bit signed quantity.  */
955       *i2 = (((insn[2] << 8) | insn[3]) ^ 0x8000) - 0x8000;
956       return 1;
957     }
958   else
959     return 0;
960 }
961
962
963 static int
964 is_ril (bfd_byte *insn, int op1, int op2,
965         unsigned int *r1, int *i2)
966 {
967   if (insn[0] == op1 && (insn[1] & 0xf) == op2)
968     {
969       *r1 = (insn[1] >> 4) & 0xf;
970       /* i2 is a signed quantity.  If the host 'int' is 32 bits long,
971          no sign extension is necessary, but we don't want to assume
972          that.  */
973       *i2 = (((insn[2] << 24)
974               | (insn[3] << 16)
975               | (insn[4] << 8)
976               | (insn[5])) ^ 0x80000000) - 0x80000000;
977       return 1;
978     }
979   else
980     return 0;
981 }
982
983
984 static int
985 is_rr (bfd_byte *insn, int op, unsigned int *r1, unsigned int *r2)
986 {
987   if (insn[0] == op)
988     {
989       *r1 = (insn[1] >> 4) & 0xf;
990       *r2 = insn[1] & 0xf;
991       return 1;
992     }
993   else
994     return 0;
995 }
996
997
998 static int
999 is_rre (bfd_byte *insn, int op, unsigned int *r1, unsigned int *r2)
1000 {
1001   if (((insn[0] << 8) | insn[1]) == op)
1002     {
1003       /* Yes, insn[3].  insn[2] is unused in RRE format.  */
1004       *r1 = (insn[3] >> 4) & 0xf;
1005       *r2 = insn[3] & 0xf;
1006       return 1;
1007     }
1008   else
1009     return 0;
1010 }
1011
1012
1013 static int
1014 is_rs (bfd_byte *insn, int op,
1015        unsigned int *r1, unsigned int *r3, unsigned int *d2, unsigned int *b2)
1016 {
1017   if (insn[0] == op)
1018     {
1019       *r1 = (insn[1] >> 4) & 0xf;
1020       *r3 = insn[1] & 0xf;
1021       *b2 = (insn[2] >> 4) & 0xf;
1022       *d2 = ((insn[2] & 0xf) << 8) | insn[3];
1023       return 1;
1024     }
1025   else
1026     return 0;
1027 }
1028
1029
1030 static int
1031 is_rsy (bfd_byte *insn, int op1, int op2,
1032         unsigned int *r1, unsigned int *r3, unsigned int *d2, unsigned int *b2)
1033 {
1034   if (insn[0] == op1
1035       && insn[5] == op2)
1036     {
1037       *r1 = (insn[1] >> 4) & 0xf;
1038       *r3 = insn[1] & 0xf;
1039       *b2 = (insn[2] >> 4) & 0xf;
1040       /* The 'long displacement' is a 20-bit signed integer.  */
1041       *d2 = ((((insn[2] & 0xf) << 8) | insn[3] | (insn[4] << 12)) 
1042                 ^ 0x80000) - 0x80000;
1043       return 1;
1044     }
1045   else
1046     return 0;
1047 }
1048
1049
1050 static int
1051 is_rx (bfd_byte *insn, int op,
1052        unsigned int *r1, unsigned int *d2, unsigned int *x2, unsigned int *b2)
1053 {
1054   if (insn[0] == op)
1055     {
1056       *r1 = (insn[1] >> 4) & 0xf;
1057       *x2 = insn[1] & 0xf;
1058       *b2 = (insn[2] >> 4) & 0xf;
1059       *d2 = ((insn[2] & 0xf) << 8) | insn[3];
1060       return 1;
1061     }
1062   else
1063     return 0;
1064 }
1065
1066
1067 static int
1068 is_rxy (bfd_byte *insn, int op1, int op2,
1069         unsigned int *r1, unsigned int *d2, unsigned int *x2, unsigned int *b2)
1070 {
1071   if (insn[0] == op1
1072       && insn[5] == op2)
1073     {
1074       *r1 = (insn[1] >> 4) & 0xf;
1075       *x2 = insn[1] & 0xf;
1076       *b2 = (insn[2] >> 4) & 0xf;
1077       /* The 'long displacement' is a 20-bit signed integer.  */
1078       *d2 = ((((insn[2] & 0xf) << 8) | insn[3] | (insn[4] << 12)) 
1079                 ^ 0x80000) - 0x80000;
1080       return 1;
1081     }
1082   else
1083     return 0;
1084 }
1085
1086
1087 /* Set ADDR to the effective address for an X-style instruction, like:
1088
1089         L R1, D2(X2, B2)
1090
1091    Here, X2 and B2 are registers, and D2 is a signed 20-bit
1092    constant; the effective address is the sum of all three.  If either
1093    X2 or B2 are zero, then it doesn't contribute to the sum --- this
1094    means that r0 can't be used as either X2 or B2.
1095
1096    GPR is an array of general register values, indexed by GPR number,
1097    not GDB register number.  */
1098 static void
1099 compute_x_addr (struct prologue_value *addr, 
1100                 struct prologue_value *gpr,
1101                 int d2, unsigned int x2, unsigned int b2)
1102 {
1103   /* We can't just add stuff directly in addr; it might alias some of
1104      the registers we need to read.  */
1105   struct prologue_value result;
1106
1107   pv_set_to_constant (&result, d2);
1108   if (x2)
1109     pv_add (&result, &result, &gpr[x2]);
1110   if (b2)
1111     pv_add (&result, &result, &gpr[b2]);
1112
1113   *addr = result;
1114 }
1115
1116
1117 /* The number of GPR and FPR spill slots in an S/390 stack frame.  We
1118    track general-purpose registers r2 -- r15, and floating-point
1119    registers f0, f2, f4, and f6.  */
1120 #define S390_NUM_SPILL_SLOTS (14 + 4)
1121 #define S390_NUM_GPRS 16
1122 #define S390_NUM_FPRS 16
1123
1124 struct s390_prologue_data {
1125
1126   /* The size of a GPR or FPR.  */
1127   int gpr_size;
1128   int fpr_size;
1129
1130   /* The general-purpose registers.  */
1131   struct prologue_value gpr[S390_NUM_GPRS];
1132
1133   /* The floating-point registers.  */
1134   struct prologue_value fpr[S390_NUM_FPRS];
1135
1136   /* The register spill stack slots in the caller's frame ---
1137      general-purpose registers r2 through r15, and floating-point
1138      registers.  spill[i] is where gpr i+2 gets spilled;
1139      spill[(14, 15, 16, 17)] is where (f0, f2, f4, f6) get spilled.  */
1140   struct prologue_value spill[S390_NUM_SPILL_SLOTS];
1141
1142   /* The value of the back chain slot.  This is only valid if the stack
1143      pointer is known to be less than its original value --- that is,
1144      if we have indeed allocated space on the stack.  */
1145   struct prologue_value back_chain;
1146 };
1147
1148
1149 /* If the SIZE bytes at ADDR are a stack slot we're actually tracking,
1150    return pv_definite_yes and set *STACK to point to the slot.  If
1151    we're sure that they are not any of our stack slots, then return
1152    pv_definite_no.  Otherwise, return pv_maybe.
1153
1154    DATA describes our current state (registers and stack slots).  */
1155 static enum pv_boolean
1156 s390_on_stack (struct prologue_value *addr,
1157                CORE_ADDR size,
1158                struct s390_prologue_data *data,
1159                struct prologue_value **stack)
1160 {
1161   struct prologue_value gpr_spill_addr;
1162   struct prologue_value fpr_spill_addr;
1163   struct prologue_value back_chain_addr;  
1164   int i;
1165   enum pv_boolean b;
1166
1167   /* Construct the addresses of the spill arrays and the back chain.  */
1168   pv_set_to_register (&gpr_spill_addr, S390_SP_REGNUM, 2 * data->gpr_size);
1169   pv_set_to_register (&fpr_spill_addr, S390_SP_REGNUM, 16 * data->gpr_size);
1170   back_chain_addr = data->gpr[S390_SP_REGNUM - S390_R0_REGNUM];
1171
1172   /* We have to check for GPR and FPR references using two separate
1173      calls to pv_is_array_ref, since the GPR and FPR spill slots are
1174      different sizes.  (SPILL is an array, but the thing it tracks
1175      isn't really an array.)  */
1176
1177   /* Was it a reference to the GPR spill array?  */
1178   b = pv_is_array_ref (addr, size, &gpr_spill_addr, 14, data->gpr_size, &i);
1179   if (b == pv_definite_yes)
1180     {
1181       *stack = &data->spill[i];
1182       return pv_definite_yes;
1183     }
1184   if (b == pv_maybe)
1185     return pv_maybe;
1186
1187   /* Was it a reference to the FPR spill array?  */
1188   b = pv_is_array_ref (addr, size, &fpr_spill_addr, 4, data->fpr_size, &i);
1189   if (b == pv_definite_yes)
1190     {
1191       *stack = &data->spill[14 + i];
1192       return pv_definite_yes;
1193     }
1194   if (b == pv_maybe)
1195     return pv_maybe;
1196
1197   /* Was it a reference to the back chain?
1198      This isn't quite right.  We ought to check whether we have
1199      actually allocated any new frame at all.  */
1200   b = pv_is_array_ref (addr, size, &back_chain_addr, 1, data->gpr_size, &i);
1201   if (b == pv_definite_yes)
1202     {
1203       *stack = &data->back_chain;
1204       return pv_definite_yes;
1205     }
1206   if (b == pv_maybe)
1207     return pv_maybe;
1208
1209   /* All the above queries returned definite 'no's.  */
1210   return pv_definite_no;
1211 }
1212
1213
1214 /* Do a SIZE-byte store of VALUE to ADDR.  */
1215 static void
1216 s390_store (struct prologue_value *addr,
1217             CORE_ADDR size,
1218             struct prologue_value *value,
1219             struct s390_prologue_data *data)
1220 {
1221   struct prologue_value *stack;
1222
1223   /* We can do it if it's definitely a reference to something on the stack.  */
1224   if (s390_on_stack (addr, size, data, &stack) == pv_definite_yes)
1225     {
1226       *stack = *value;
1227       return;
1228     }
1229
1230   /* Note: If s390_on_stack returns pv_maybe, you might think we should
1231      forget our cached values, as any of those might have been hit.
1232
1233      However, we make the assumption that --since the fields we track
1234      are save areas private to compiler, and never directly exposed to 
1235      the user-- every access to our data is explicit.  Hence, every 
1236      memory access we cannot follow can't hit our data.  */
1237 }
1238
1239 /* Do a SIZE-byte load from ADDR into VALUE.  */
1240 static void
1241 s390_load (struct prologue_value *addr,
1242            CORE_ADDR size,
1243            struct prologue_value *value,
1244            struct s390_prologue_data *data)
1245 {
1246   struct prologue_value *stack;
1247
1248   /* If it's a load from an in-line constant pool, then we can
1249      simulate that, under the assumption that the code isn't
1250      going to change between the time the processor actually
1251      executed it creating the current frame, and the time when
1252      we're analyzing the code to unwind past that frame.  */
1253   if (addr->kind == pv_constant)
1254     {
1255       struct section_table *secp;
1256       secp = target_section_by_addr (&current_target, addr->k);
1257       if (secp != NULL
1258           && (bfd_get_section_flags (secp->bfd, secp->the_bfd_section)
1259               & SEC_READONLY))
1260         {
1261           pv_set_to_constant (value, read_memory_integer (addr->k, size));
1262           return;
1263         }
1264     }
1265
1266   /* If it's definitely a reference to something on the stack, 
1267      we can do that.  */
1268   if (s390_on_stack (addr, size, data, &stack) == pv_definite_yes)
1269     {
1270       *value = *stack;
1271       return;
1272     }
1273
1274   /* Otherwise, we don't know the value.  */
1275   pv_set_to_unknown (value);
1276 }
1277             
1278
1279 /* Analyze the prologue of the function starting at START_PC,
1280    continuing at most until CURRENT_PC.  Initialize DATA to
1281    hold all information we find out about the state of the registers
1282    and stack slots.  Return the address of the instruction after
1283    the last one that changed the SP, FP, or back chain; or zero
1284    on error.  */
1285 static CORE_ADDR
1286 s390_analyze_prologue (struct gdbarch *gdbarch,
1287                        CORE_ADDR start_pc,
1288                        CORE_ADDR current_pc,
1289                        struct s390_prologue_data *data)
1290 {
1291   int word_size = gdbarch_ptr_bit (gdbarch) / 8;
1292
1293   /* Our return value:
1294      The address of the instruction after the last one that changed
1295      the SP, FP, or back chain;  zero if we got an error trying to 
1296      read memory.  */
1297   CORE_ADDR result = start_pc;
1298
1299   /* The current PC for our abstract interpretation.  */
1300   CORE_ADDR pc;
1301
1302   /* The address of the next instruction after that.  */
1303   CORE_ADDR next_pc;
1304   
1305   /* Set up everything's initial value.  */
1306   {
1307     int i;
1308
1309     /* For the purpose of prologue tracking, we consider the GPR size to
1310        be equal to the ABI word size, even if it is actually larger
1311        (i.e. when running a 32-bit binary under a 64-bit kernel).  */
1312     data->gpr_size = word_size;
1313     data->fpr_size = 8;
1314
1315     for (i = 0; i < S390_NUM_GPRS; i++)
1316       pv_set_to_register (&data->gpr[i], S390_R0_REGNUM + i, 0);
1317
1318     for (i = 0; i < S390_NUM_FPRS; i++)
1319       pv_set_to_register (&data->fpr[i], S390_F0_REGNUM + i, 0);
1320
1321     for (i = 0; i < S390_NUM_SPILL_SLOTS; i++)
1322       pv_set_to_unknown (&data->spill[i]);
1323
1324     pv_set_to_unknown (&data->back_chain);
1325   }
1326
1327   /* Start interpreting instructions, until we hit the frame's
1328      current PC or the first branch instruction.  */
1329   for (pc = start_pc; pc > 0 && pc < current_pc; pc = next_pc)
1330     {
1331       bfd_byte insn[S390_MAX_INSTR_SIZE];
1332       int insn_len = s390_readinstruction (insn, pc);
1333
1334       /* Fields for various kinds of instructions.  */
1335       unsigned int b2, r1, r2, x2, r3;
1336       int i2, d2;
1337
1338       /* The values of SP, FP, and back chain before this instruction,
1339          for detecting instructions that change them.  */
1340       struct prologue_value pre_insn_sp, pre_insn_fp, pre_insn_back_chain;
1341
1342       /* If we got an error trying to read the instruction, report it.  */
1343       if (insn_len < 0)
1344         {
1345           result = 0;
1346           break;
1347         }
1348
1349       next_pc = pc + insn_len;
1350
1351       pre_insn_sp = data->gpr[S390_SP_REGNUM - S390_R0_REGNUM];
1352       pre_insn_fp = data->gpr[S390_FRAME_REGNUM - S390_R0_REGNUM];
1353       pre_insn_back_chain = data->back_chain;
1354
1355       /* LHI r1, i2 --- load halfword immediate */
1356       if (word_size == 4
1357           && is_ri (insn, op1_lhi, op2_lhi, &r1, &i2))
1358         pv_set_to_constant (&data->gpr[r1], i2);
1359
1360       /* LGHI r1, i2 --- load halfword immediate (64-bit version) */
1361       else if (word_size == 8
1362                && is_ri (insn, op1_lghi, op2_lghi, &r1, &i2))
1363         pv_set_to_constant (&data->gpr[r1], i2);
1364
1365       /* LR r1, r2 --- load from register */
1366       else if (word_size == 4
1367                && is_rr (insn, op_lr, &r1, &r2))
1368         data->gpr[r1] = data->gpr[r2];
1369
1370       /* LGR r1, r2 --- load from register (64-bit version) */
1371       else if (word_size == 8
1372                && is_rre (insn, op_lgr, &r1, &r2))
1373         data->gpr[r1] = data->gpr[r2];
1374
1375       /* L r1, d2(x2, b2) --- load */
1376       else if (word_size == 4
1377                && is_rx (insn, op_l, &r1, &d2, &x2, &b2))
1378         {
1379           struct prologue_value addr;
1380
1381           compute_x_addr (&addr, data->gpr, d2, x2, b2);
1382           s390_load (&addr, 4, &data->gpr[r1], data);
1383         }
1384
1385       /* LY r1, d2(x2, b2) --- load (long-displacement version) */
1386       else if (word_size == 4
1387                && is_rxy (insn, op1_ly, op2_ly, &r1, &d2, &x2, &b2))
1388         {
1389           struct prologue_value addr;
1390
1391           compute_x_addr (&addr, data->gpr, d2, x2, b2);
1392           s390_load (&addr, 4, &data->gpr[r1], data);
1393         }
1394
1395       /* LG r1, d2(x2, b2) --- load (64-bit version) */
1396       else if (word_size == 8
1397                && is_rxy (insn, op1_lg, op2_lg, &r1, &d2, &x2, &b2))
1398         {
1399           struct prologue_value addr;
1400
1401           compute_x_addr (&addr, data->gpr, d2, x2, b2);
1402           s390_load (&addr, 8, &data->gpr[r1], data);
1403         }
1404
1405       /* ST r1, d2(x2, b2) --- store */
1406       else if (word_size == 4
1407                && is_rx (insn, op_st, &r1, &d2, &x2, &b2))
1408         {
1409           struct prologue_value addr;
1410
1411           compute_x_addr (&addr, data->gpr, d2, x2, b2);
1412           s390_store (&addr, 4, &data->gpr[r1], data);
1413         }
1414
1415       /* STY r1, d2(x2, b2) --- store (long-displacement version) */
1416       else if (word_size == 4
1417                && is_rxy (insn, op1_sty, op2_sty, &r1, &d2, &x2, &b2))
1418         {
1419           struct prologue_value addr;
1420
1421           compute_x_addr (&addr, data->gpr, d2, x2, b2);
1422           s390_store (&addr, 4, &data->gpr[r1], data);
1423         }
1424
1425       /* STG r1, d2(x2, b2) --- store (64-bit version) */
1426       else if (word_size == 8
1427                && is_rxy (insn, op1_stg, op2_stg, &r1, &d2, &x2, &b2))
1428         {
1429           struct prologue_value addr;
1430
1431           compute_x_addr (&addr, data->gpr, d2, x2, b2);
1432           s390_store (&addr, 8, &data->gpr[r1], data);
1433         }
1434
1435       /* STD r1, d2(x2,b2) --- store floating-point register  */
1436       else if (is_rx (insn, op_std, &r1, &d2, &x2, &b2))
1437         {
1438           struct prologue_value addr;
1439
1440           compute_x_addr (&addr, data->gpr, d2, x2, b2);
1441           s390_store (&addr, 8, &data->fpr[r1], data);
1442         }
1443
1444       /* STM r1, r3, d2(b2) --- store multiple */
1445       else if (word_size == 4
1446                && is_rs (insn, op_stm, &r1, &r3, &d2, &b2))
1447         {
1448           int regnum;
1449           int offset;
1450           struct prologue_value addr;
1451
1452           for (regnum = r1, offset = 0;
1453                regnum <= r3;
1454                regnum++, offset += 4)
1455             {
1456               compute_x_addr (&addr, data->gpr, d2 + offset, 0, b2);
1457               s390_store (&addr, 4, &data->gpr[regnum], data);
1458             }
1459         }
1460
1461       /* STMY r1, r3, d2(b2) --- store multiple (long-displacement version) */
1462       else if (word_size == 4
1463                && is_rsy (insn, op1_stmy, op2_stmy, &r1, &r3, &d2, &b2))
1464         {
1465           int regnum;
1466           int offset;
1467           struct prologue_value addr;
1468
1469           for (regnum = r1, offset = 0;
1470                regnum <= r3;
1471                regnum++, offset += 4)
1472             {
1473               compute_x_addr (&addr, data->gpr, d2 + offset, 0, b2);
1474               s390_store (&addr, 4, &data->gpr[regnum], data);
1475             }
1476         }
1477
1478       /* STMG r1, r3, d2(b2) --- store multiple (64-bit version) */
1479       else if (word_size == 8
1480                && is_rsy (insn, op1_stmg, op2_stmg, &r1, &r3, &d2, &b2))
1481         {
1482           int regnum;
1483           int offset;
1484           struct prologue_value addr;
1485
1486           for (regnum = r1, offset = 0;
1487                regnum <= r3;
1488                regnum++, offset += 8)
1489             {
1490               compute_x_addr (&addr, data->gpr, d2 + offset, 0, b2);
1491               s390_store (&addr, 8, &data->gpr[regnum], data);
1492             }
1493         }
1494
1495       /* AHI r1, i2 --- add halfword immediate */
1496       else if (word_size == 4
1497                && is_ri (insn, op1_ahi, op2_ahi, &r1, &i2))
1498         pv_add_constant (&data->gpr[r1], i2);
1499
1500       /* AGHI r1, i2 --- add halfword immediate (64-bit version) */
1501       else if (word_size == 8
1502                && is_ri (insn, op1_aghi, op2_aghi, &r1, &i2))
1503         pv_add_constant (&data->gpr[r1], i2);
1504
1505       /* AR r1, r2 -- add register */
1506       else if (word_size == 4
1507                && is_rr (insn, op_ar, &r1, &r2))
1508         pv_add (&data->gpr[r1], &data->gpr[r1], &data->gpr[r2]);
1509
1510       /* AGR r1, r2 -- add register (64-bit version) */
1511       else if (word_size == 8
1512                && is_rre (insn, op_agr, &r1, &r2))
1513         pv_add (&data->gpr[r1], &data->gpr[r1], &data->gpr[r2]);
1514
1515       /* A r1, d2(x2, b2) -- add */
1516       else if (word_size == 4
1517                && is_rx (insn, op_a, &r1, &d2, &x2, &b2))
1518         {
1519           struct prologue_value addr;
1520           struct prologue_value value;
1521
1522           compute_x_addr (&addr, data->gpr, d2, x2, b2);
1523           s390_load (&addr, 4, &value, data);
1524         
1525           pv_add (&data->gpr[r1], &data->gpr[r1], &value);
1526         }
1527
1528       /* AY r1, d2(x2, b2) -- add (long-displacement version) */
1529       else if (word_size == 4
1530                && is_rxy (insn, op1_ay, op2_ay, &r1, &d2, &x2, &b2))
1531         {
1532           struct prologue_value addr;
1533           struct prologue_value value;
1534
1535           compute_x_addr (&addr, data->gpr, d2, x2, b2);
1536           s390_load (&addr, 4, &value, data);
1537         
1538           pv_add (&data->gpr[r1], &data->gpr[r1], &value);
1539         }
1540
1541       /* AG r1, d2(x2, b2) -- add (64-bit version) */
1542       else if (word_size == 8
1543                && is_rxy (insn, op1_ag, op2_ag, &r1, &d2, &x2, &b2))
1544         {
1545           struct prologue_value addr;
1546           struct prologue_value value;
1547
1548           compute_x_addr (&addr, data->gpr, d2, x2, b2);
1549           s390_load (&addr, 8, &value, data);
1550         
1551           pv_add (&data->gpr[r1], &data->gpr[r1], &value);
1552         }
1553
1554       /* SR r1, r2 -- subtract register */
1555       else if (word_size == 4
1556                && is_rr (insn, op_sr, &r1, &r2))
1557         pv_subtract (&data->gpr[r1], &data->gpr[r1], &data->gpr[r2]);
1558
1559       /* SGR r1, r2 -- subtract register (64-bit version) */
1560       else if (word_size == 8
1561                && is_rre (insn, op_sgr, &r1, &r2))
1562         pv_subtract (&data->gpr[r1], &data->gpr[r1], &data->gpr[r2]);
1563
1564       /* S r1, d2(x2, b2) -- subtract */
1565       else if (word_size == 4
1566                && is_rx (insn, op_s, &r1, &d2, &x2, &b2))
1567         {
1568           struct prologue_value addr;
1569           struct prologue_value value;
1570
1571           compute_x_addr (&addr, data->gpr, d2, x2, b2);
1572           s390_load (&addr, 4, &value, data);
1573         
1574           pv_subtract (&data->gpr[r1], &data->gpr[r1], &value);
1575         }
1576
1577       /* SY r1, d2(x2, b2) -- subtract (long-displacement version) */
1578       else if (word_size == 4
1579                && is_rxy (insn, op1_sy, op2_sy, &r1, &d2, &x2, &b2))
1580         {
1581           struct prologue_value addr;
1582           struct prologue_value value;
1583
1584           compute_x_addr (&addr, data->gpr, d2, x2, b2);
1585           s390_load (&addr, 4, &value, data);
1586         
1587           pv_subtract (&data->gpr[r1], &data->gpr[r1], &value);
1588         }
1589
1590       /* SG r1, d2(x2, b2) -- subtract (64-bit version) */
1591       else if (word_size == 8
1592                && is_rxy (insn, op1_sg, op2_sg, &r1, &d2, &x2, &b2))
1593         {
1594           struct prologue_value addr;
1595           struct prologue_value value;
1596
1597           compute_x_addr (&addr, data->gpr, d2, x2, b2);
1598           s390_load (&addr, 8, &value, data);
1599         
1600           pv_subtract (&data->gpr[r1], &data->gpr[r1], &value);
1601         }
1602
1603       /* NR r1, r2 --- logical and */
1604       else if (word_size == 4
1605                && is_rr (insn, op_nr, &r1, &r2))
1606         pv_logical_and (&data->gpr[r1], &data->gpr[r1], &data->gpr[r2]);
1607
1608       /* NGR r1, r2 >--- logical and (64-bit version) */
1609       else if (word_size == 8
1610                && is_rre (insn, op_ngr, &r1, &r2))
1611         pv_logical_and (&data->gpr[r1], &data->gpr[r1], &data->gpr[r2]);
1612
1613       /* LA r1, d2(x2, b2) --- load address */
1614       else if (is_rx (insn, op_la, &r1, &d2, &x2, &b2))
1615         compute_x_addr (&data->gpr[r1], data->gpr, d2, x2, b2);
1616
1617       /* LAY r1, d2(x2, b2) --- load address (long-displacement version) */
1618       else if (is_rxy (insn, op1_lay, op2_lay, &r1, &d2, &x2, &b2))
1619         compute_x_addr (&data->gpr[r1], data->gpr, d2, x2, b2);
1620
1621       /* LARL r1, i2 --- load address relative long */
1622       else if (is_ril (insn, op1_larl, op2_larl, &r1, &i2))
1623         pv_set_to_constant (&data->gpr[r1], pc + i2 * 2);
1624
1625       /* BASR r1, 0 --- branch and save
1626          Since r2 is zero, this saves the PC in r1, but doesn't branch.  */
1627       else if (is_rr (insn, op_basr, &r1, &r2)
1628                && r2 == 0)
1629         pv_set_to_constant (&data->gpr[r1], next_pc);
1630
1631       /* BRAS r1, i2 --- branch relative and save */
1632       else if (is_ri (insn, op1_bras, op2_bras, &r1, &i2))
1633         {
1634           pv_set_to_constant (&data->gpr[r1], next_pc);
1635           next_pc = pc + i2 * 2;
1636
1637           /* We'd better not interpret any backward branches.  We'll
1638              never terminate.  */
1639           if (next_pc <= pc)
1640             break;
1641         }
1642
1643       /* Terminate search when hitting any other branch instruction.  */
1644       else if (is_rr (insn, op_basr, &r1, &r2)
1645                || is_rx (insn, op_bas, &r1, &d2, &x2, &b2)
1646                || is_rr (insn, op_bcr, &r1, &r2)
1647                || is_rx (insn, op_bc, &r1, &d2, &x2, &b2)
1648                || is_ri (insn, op1_brc, op2_brc, &r1, &i2)
1649                || is_ril (insn, op1_brcl, op2_brcl, &r1, &i2)
1650                || is_ril (insn, op1_brasl, op2_brasl, &r2, &i2))
1651         break;
1652
1653       else
1654         /* An instruction we don't know how to simulate.  The only
1655            safe thing to do would be to set every value we're tracking
1656            to 'unknown'.  Instead, we'll be optimistic: we assume that
1657            we *can* interpret every instruction that the compiler uses
1658            to manipulate any of the data we're interested in here --
1659            then we can just ignore anything else.  */
1660         ;
1661
1662       /* Record the address after the last instruction that changed
1663          the FP, SP, or backlink.  Ignore instructions that changed
1664          them back to their original values --- those are probably
1665          restore instructions.  (The back chain is never restored,
1666          just popped.)  */
1667       {
1668         struct prologue_value *sp = &data->gpr[S390_SP_REGNUM - S390_R0_REGNUM];
1669         struct prologue_value *fp = &data->gpr[S390_FRAME_REGNUM - S390_R0_REGNUM];
1670         
1671         if ((! pv_is_identical (&pre_insn_sp, sp)
1672              && ! pv_is_register (sp, S390_SP_REGNUM, 0))
1673             || (! pv_is_identical (&pre_insn_fp, fp)
1674                 && ! pv_is_register (fp, S390_FRAME_REGNUM, 0))
1675             || ! pv_is_identical (&pre_insn_back_chain, &data->back_chain))
1676           result = next_pc;
1677       }
1678     }
1679
1680   return result;
1681 }
1682
1683 /* Advance PC across any function entry prologue instructions to reach 
1684    some "real" code.  */
1685 static CORE_ADDR
1686 s390_skip_prologue (CORE_ADDR pc)
1687 {
1688   struct s390_prologue_data data;
1689   CORE_ADDR skip_pc;
1690   skip_pc = s390_analyze_prologue (current_gdbarch, pc, (CORE_ADDR)-1, &data);
1691   return skip_pc ? skip_pc : pc;
1692 }
1693
1694 /* Return true if we are in the functin's epilogue, i.e. after the
1695    instruction that destroyed the function's stack frame.  */
1696 static int
1697 s390_in_function_epilogue_p (struct gdbarch *gdbarch, CORE_ADDR pc)
1698 {
1699   int word_size = gdbarch_ptr_bit (gdbarch) / 8;
1700
1701   /* In frameless functions, there's not frame to destroy and thus
1702      we don't care about the epilogue.
1703
1704      In functions with frame, the epilogue sequence is a pair of
1705      a LM-type instruction that restores (amongst others) the
1706      return register %r14 and the stack pointer %r15, followed
1707      by a branch 'br %r14' --or equivalent-- that effects the
1708      actual return.
1709
1710      In that situation, this function needs to return 'true' in
1711      exactly one case: when pc points to that branch instruction.
1712
1713      Thus we try to disassemble the one instructions immediately
1714      preceeding pc and check whether it is an LM-type instruction
1715      modifying the stack pointer.
1716
1717      Note that disassembling backwards is not reliable, so there
1718      is a slight chance of false positives here ...  */
1719
1720   bfd_byte insn[6];
1721   unsigned int r1, r3, b2;
1722   int d2;
1723
1724   if (word_size == 4
1725       && !read_memory_nobpt (pc - 4, insn, 4)
1726       && is_rs (insn, op_lm, &r1, &r3, &d2, &b2)
1727       && r3 == S390_SP_REGNUM - S390_R0_REGNUM)
1728     return 1;
1729
1730   if (word_size == 4
1731       && !read_memory_nobpt (pc - 6, insn, 6)
1732       && is_rsy (insn, op1_lmy, op2_lmy, &r1, &r3, &d2, &b2)
1733       && r3 == S390_SP_REGNUM - S390_R0_REGNUM)
1734     return 1;
1735
1736   if (word_size == 8
1737       && !read_memory_nobpt (pc - 6, insn, 6)
1738       && is_rsy (insn, op1_lmg, op2_lmg, &r1, &r3, &d2, &b2)
1739       && r3 == S390_SP_REGNUM - S390_R0_REGNUM)
1740     return 1;
1741
1742   return 0;
1743 }
1744
1745
1746 /* Normal stack frames.  */
1747
1748 struct s390_unwind_cache {
1749
1750   CORE_ADDR func;
1751   CORE_ADDR frame_base;
1752   CORE_ADDR local_base;
1753
1754   struct trad_frame_saved_reg *saved_regs;
1755 };
1756
1757 static int
1758 s390_prologue_frame_unwind_cache (struct frame_info *next_frame,
1759                                   struct s390_unwind_cache *info)
1760 {
1761   struct gdbarch *gdbarch = get_frame_arch (next_frame);
1762   int word_size = gdbarch_ptr_bit (gdbarch) / 8;
1763   struct s390_prologue_data data;
1764   struct prologue_value *fp = &data.gpr[S390_FRAME_REGNUM - S390_R0_REGNUM];
1765   struct prologue_value *sp = &data.gpr[S390_SP_REGNUM - S390_R0_REGNUM];
1766   int slot_num;
1767   CORE_ADDR slot_addr;
1768   CORE_ADDR func;
1769   CORE_ADDR result;
1770   ULONGEST reg;
1771   CORE_ADDR prev_sp;
1772   int frame_pointer;
1773   int size;
1774
1775   /* Try to find the function start address.  If we can't find it, we don't
1776      bother searching for it -- with modern compilers this would be mostly
1777      pointless anyway.  Trust that we'll either have valid DWARF-2 CFI data
1778      or else a valid backchain ...  */
1779   func = frame_func_unwind (next_frame);
1780   if (!func)
1781     return 0;
1782
1783   /* Try to analyze the prologue.  */
1784   result = s390_analyze_prologue (gdbarch, func,
1785                                   frame_pc_unwind (next_frame), &data);
1786   if (!result)
1787     return 0;
1788
1789   /* If this was successful, we should have found the instruction that
1790      sets the stack pointer register to the previous value of the stack 
1791      pointer minus the frame size.  */
1792   if (sp->kind != pv_register || sp->reg != S390_SP_REGNUM)
1793     return 0;
1794
1795   /* A frame size of zero at this point can mean either a real 
1796      frameless function, or else a failure to find the prologue.
1797      Perform some sanity checks to verify we really have a 
1798      frameless function.  */
1799   if (sp->k == 0)
1800     {
1801       /* If the next frame is a NORMAL_FRAME, this frame *cannot* have frame 
1802          size zero.  This is only possible if the next frame is a sentinel 
1803          frame, a dummy frame, or a signal trampoline frame.  */
1804       if (get_frame_type (next_frame) == NORMAL_FRAME
1805           /* For some reason, sentinel frames are NORMAL_FRAMEs
1806              -- but they have negative frame level.  */
1807           && frame_relative_level (next_frame) >= 0)
1808         return 0;
1809
1810       /* If we really have a frameless function, %r14 must be valid
1811          -- in particular, it must point to a different function.  */
1812       reg = frame_unwind_register_unsigned (next_frame, S390_RETADDR_REGNUM);
1813       reg = gdbarch_addr_bits_remove (gdbarch, reg) - 1;
1814       if (get_pc_function_start (reg) == func)
1815         {
1816           /* However, there is one case where it *is* valid for %r14
1817              to point to the same function -- if this is a recursive
1818              call, and we have stopped in the prologue *before* the
1819              stack frame was allocated.
1820
1821              Recognize this case by looking ahead a bit ...  */
1822
1823           struct s390_prologue_data data2;
1824           struct prologue_value *sp = &data2.gpr[S390_SP_REGNUM - S390_R0_REGNUM];
1825
1826           if (!(s390_analyze_prologue (gdbarch, func, (CORE_ADDR)-1, &data2)
1827                 && sp->kind == pv_register
1828                 && sp->reg == S390_SP_REGNUM
1829                 && sp->k != 0))
1830             return 0;
1831         }
1832     }
1833
1834
1835   /* OK, we've found valid prologue data.  */
1836   size = -sp->k;
1837
1838   /* If the frame pointer originally also holds the same value
1839      as the stack pointer, we're probably using it.  If it holds
1840      some other value -- even a constant offset -- it is most
1841      likely used as temp register.  */
1842   if (pv_is_identical (sp, fp))
1843     frame_pointer = S390_FRAME_REGNUM;
1844   else
1845     frame_pointer = S390_SP_REGNUM;
1846
1847   /* If we've detected a function with stack frame, we'll still have to 
1848      treat it as frameless if we're currently within the function epilog 
1849      code at a point where the frame pointer has already been restored.  
1850      This can only happen in an innermost frame.  */
1851   if (size > 0
1852       && (get_frame_type (next_frame) != NORMAL_FRAME
1853           || frame_relative_level (next_frame) < 0))
1854     {
1855       /* See the comment in s390_in_function_epilogue_p on why this is
1856          not completely reliable ...  */
1857       if (s390_in_function_epilogue_p (gdbarch, frame_pc_unwind (next_frame)))
1858         {
1859           memset (&data, 0, sizeof (data));
1860           size = 0;
1861           frame_pointer = S390_SP_REGNUM;
1862         }
1863     }
1864
1865   /* Once we know the frame register and the frame size, we can unwind
1866      the current value of the frame register from the next frame, and
1867      add back the frame size to arrive that the previous frame's 
1868      stack pointer value.  */
1869   prev_sp = frame_unwind_register_unsigned (next_frame, frame_pointer) + size;
1870
1871   /* Scan the spill array; if a spill slot says it holds the
1872      original value of some register, then record that slot's
1873      address as the place that register was saved.  */
1874
1875   /* Slots for %r2 .. %r15.  */
1876   for (slot_num = 0, slot_addr = prev_sp + 2 * data.gpr_size;
1877        slot_num < 14;
1878        slot_num++, slot_addr += data.gpr_size)
1879     {
1880       struct prologue_value *slot = &data.spill[slot_num];
1881
1882       if (slot->kind == pv_register
1883           && slot->k == 0)
1884         info->saved_regs[slot->reg].addr = slot_addr;
1885     }
1886
1887   /* Slots for %f0 .. %f6.  */
1888   for (slot_num = 14, slot_addr = prev_sp + 16 * data.gpr_size;
1889        slot_num < S390_NUM_SPILL_SLOTS;
1890        slot_num++, slot_addr += data.fpr_size)
1891     {
1892       struct prologue_value *slot = &data.spill[slot_num];
1893
1894       if (slot->kind == pv_register
1895           && slot->k == 0)
1896         info->saved_regs[slot->reg].addr = slot_addr;
1897     }
1898
1899   /* Function return will set PC to %r14.  */
1900   info->saved_regs[S390_PC_REGNUM] = info->saved_regs[S390_RETADDR_REGNUM];
1901
1902   /* In frameless functions, we unwind simply by moving the return
1903      address to the PC.  However, if we actually stored to the
1904      save area, use that -- we might only think the function frameless
1905      because we're in the middle of the prologue ...  */
1906   if (size == 0
1907       && !trad_frame_addr_p (info->saved_regs, S390_PC_REGNUM))
1908     {
1909       info->saved_regs[S390_PC_REGNUM].realreg = S390_RETADDR_REGNUM;
1910     }
1911
1912   /* Another sanity check: unless this is a frameless function,
1913      we should have found spill slots for SP and PC.
1914      If not, we cannot unwind further -- this happens e.g. in
1915      libc's thread_start routine.  */
1916   if (size > 0)
1917     {
1918       if (!trad_frame_addr_p (info->saved_regs, S390_SP_REGNUM)
1919           || !trad_frame_addr_p (info->saved_regs, S390_PC_REGNUM))
1920         prev_sp = -1;
1921     }
1922
1923   /* We use the current value of the frame register as local_base,
1924      and the top of the register save area as frame_base.  */
1925   if (prev_sp != -1)
1926     {
1927       info->frame_base = prev_sp + 16*word_size + 32;
1928       info->local_base = prev_sp - size;
1929     }
1930
1931   info->func = func;
1932   return 1;
1933 }
1934
1935 static void
1936 s390_backchain_frame_unwind_cache (struct frame_info *next_frame,
1937                                    struct s390_unwind_cache *info)
1938 {
1939   struct gdbarch *gdbarch = get_frame_arch (next_frame);
1940   int word_size = gdbarch_ptr_bit (gdbarch) / 8;
1941   CORE_ADDR backchain;
1942   ULONGEST reg;
1943   LONGEST sp;
1944
1945   /* Get the backchain.  */
1946   reg = frame_unwind_register_unsigned (next_frame, S390_SP_REGNUM);
1947   backchain = read_memory_unsigned_integer (reg, word_size);
1948
1949   /* A zero backchain terminates the frame chain.  As additional
1950      sanity check, let's verify that the spill slot for SP in the
1951      save area pointed to by the backchain in fact links back to
1952      the save area.  */
1953   if (backchain != 0
1954       && safe_read_memory_integer (backchain + 15*word_size, word_size, &sp)
1955       && (CORE_ADDR)sp == backchain)
1956     {
1957       /* We don't know which registers were saved, but it will have
1958          to be at least %r14 and %r15.  This will allow us to continue
1959          unwinding, but other prev-frame registers may be incorrect ...  */
1960       info->saved_regs[S390_SP_REGNUM].addr = backchain + 15*word_size;
1961       info->saved_regs[S390_RETADDR_REGNUM].addr = backchain + 14*word_size;
1962
1963       /* Function return will set PC to %r14.  */
1964       info->saved_regs[S390_PC_REGNUM] = info->saved_regs[S390_RETADDR_REGNUM];
1965
1966       /* We use the current value of the frame register as local_base,
1967          and the top of the register save area as frame_base.  */
1968       info->frame_base = backchain + 16*word_size + 32;
1969       info->local_base = reg;
1970     }
1971
1972   info->func = frame_pc_unwind (next_frame);
1973 }
1974
1975 static struct s390_unwind_cache *
1976 s390_frame_unwind_cache (struct frame_info *next_frame,
1977                          void **this_prologue_cache)
1978 {
1979   struct s390_unwind_cache *info;
1980   if (*this_prologue_cache)
1981     return *this_prologue_cache;
1982
1983   info = FRAME_OBSTACK_ZALLOC (struct s390_unwind_cache);
1984   *this_prologue_cache = info;
1985   info->saved_regs = trad_frame_alloc_saved_regs (next_frame);
1986   info->func = -1;
1987   info->frame_base = -1;
1988   info->local_base = -1;
1989
1990   /* Try to use prologue analysis to fill the unwind cache.
1991      If this fails, fall back to reading the stack backchain.  */
1992   if (!s390_prologue_frame_unwind_cache (next_frame, info))
1993     s390_backchain_frame_unwind_cache (next_frame, info);
1994
1995   return info;
1996 }
1997
1998 static void
1999 s390_frame_this_id (struct frame_info *next_frame,
2000                     void **this_prologue_cache,
2001                     struct frame_id *this_id)
2002 {
2003   struct s390_unwind_cache *info
2004     = s390_frame_unwind_cache (next_frame, this_prologue_cache);
2005
2006   if (info->frame_base == -1)
2007     return;
2008
2009   *this_id = frame_id_build (info->frame_base, info->func);
2010 }
2011
2012 static void
2013 s390_frame_prev_register (struct frame_info *next_frame,
2014                           void **this_prologue_cache,
2015                           int regnum, int *optimizedp,
2016                           enum lval_type *lvalp, CORE_ADDR *addrp,
2017                           int *realnump, void *bufferp)
2018 {
2019   struct s390_unwind_cache *info
2020     = s390_frame_unwind_cache (next_frame, this_prologue_cache);
2021   trad_frame_prev_register (next_frame, info->saved_regs, regnum,
2022                             optimizedp, lvalp, addrp, realnump, bufferp);
2023 }
2024
2025 static const struct frame_unwind s390_frame_unwind = {
2026   NORMAL_FRAME,
2027   s390_frame_this_id,
2028   s390_frame_prev_register
2029 };
2030
2031 static const struct frame_unwind *
2032 s390_frame_sniffer (struct frame_info *next_frame)
2033 {
2034   return &s390_frame_unwind;
2035 }
2036
2037
2038 /* PLT stub stack frames.  */
2039
2040 struct s390_pltstub_unwind_cache {
2041
2042   CORE_ADDR frame_base;
2043   struct trad_frame_saved_reg *saved_regs;
2044 };
2045
2046 static struct s390_pltstub_unwind_cache *
2047 s390_pltstub_frame_unwind_cache (struct frame_info *next_frame,
2048                                  void **this_prologue_cache)
2049 {
2050   struct gdbarch *gdbarch = get_frame_arch (next_frame);
2051   int word_size = gdbarch_ptr_bit (gdbarch) / 8;
2052   struct s390_pltstub_unwind_cache *info;
2053   ULONGEST reg;
2054
2055   if (*this_prologue_cache)
2056     return *this_prologue_cache;
2057
2058   info = FRAME_OBSTACK_ZALLOC (struct s390_pltstub_unwind_cache);
2059   *this_prologue_cache = info;
2060   info->saved_regs = trad_frame_alloc_saved_regs (next_frame);
2061
2062   /* The return address is in register %r14.  */
2063   info->saved_regs[S390_PC_REGNUM].realreg = S390_RETADDR_REGNUM;
2064
2065   /* Retrieve stack pointer and determine our frame base.  */
2066   reg = frame_unwind_register_unsigned (next_frame, S390_SP_REGNUM);
2067   info->frame_base = reg + 16*word_size + 32;
2068
2069   return info;
2070 }
2071
2072 static void
2073 s390_pltstub_frame_this_id (struct frame_info *next_frame,
2074                             void **this_prologue_cache,
2075                             struct frame_id *this_id)
2076 {
2077   struct s390_pltstub_unwind_cache *info
2078     = s390_pltstub_frame_unwind_cache (next_frame, this_prologue_cache);
2079   *this_id = frame_id_build (info->frame_base, frame_pc_unwind (next_frame));
2080 }
2081
2082 static void
2083 s390_pltstub_frame_prev_register (struct frame_info *next_frame,
2084                                   void **this_prologue_cache,
2085                                   int regnum, int *optimizedp,
2086                                   enum lval_type *lvalp, CORE_ADDR *addrp,
2087                                   int *realnump, void *bufferp)
2088 {
2089   struct s390_pltstub_unwind_cache *info
2090     = s390_pltstub_frame_unwind_cache (next_frame, this_prologue_cache);
2091   trad_frame_prev_register (next_frame, info->saved_regs, regnum,
2092                             optimizedp, lvalp, addrp, realnump, bufferp);
2093 }
2094
2095 static const struct frame_unwind s390_pltstub_frame_unwind = {
2096   NORMAL_FRAME,
2097   s390_pltstub_frame_this_id,
2098   s390_pltstub_frame_prev_register
2099 };
2100
2101 static const struct frame_unwind *
2102 s390_pltstub_frame_sniffer (struct frame_info *next_frame)
2103 {
2104   if (!in_plt_section (frame_pc_unwind (next_frame), NULL))
2105     return NULL;
2106
2107   return &s390_pltstub_frame_unwind;
2108 }
2109
2110
2111 /* Signal trampoline stack frames.  */
2112
2113 struct s390_sigtramp_unwind_cache {
2114   CORE_ADDR frame_base;
2115   struct trad_frame_saved_reg *saved_regs;
2116 };
2117
2118 static struct s390_sigtramp_unwind_cache *
2119 s390_sigtramp_frame_unwind_cache (struct frame_info *next_frame,
2120                                   void **this_prologue_cache)
2121 {
2122   struct gdbarch *gdbarch = get_frame_arch (next_frame);
2123   int word_size = gdbarch_ptr_bit (gdbarch) / 8;
2124   struct s390_sigtramp_unwind_cache *info;
2125   ULONGEST this_sp, prev_sp;
2126   CORE_ADDR next_ra, next_cfa, sigreg_ptr;
2127   int i;
2128
2129   if (*this_prologue_cache)
2130     return *this_prologue_cache;
2131
2132   info = FRAME_OBSTACK_ZALLOC (struct s390_sigtramp_unwind_cache);
2133   *this_prologue_cache = info;
2134   info->saved_regs = trad_frame_alloc_saved_regs (next_frame);
2135
2136   this_sp = frame_unwind_register_unsigned (next_frame, S390_SP_REGNUM);
2137   next_ra = frame_pc_unwind (next_frame);
2138   next_cfa = this_sp + 16*word_size + 32;
2139
2140   /* New-style RT frame:
2141         retcode + alignment (8 bytes)
2142         siginfo (128 bytes)
2143         ucontext (contains sigregs at offset 5 words)  */
2144   if (next_ra == next_cfa)
2145     {
2146       sigreg_ptr = next_cfa + 8 + 128 + 5*word_size;
2147     }
2148
2149   /* Old-style RT frame and all non-RT frames:
2150         old signal mask (8 bytes)
2151         pointer to sigregs  */
2152   else
2153     {
2154       sigreg_ptr = read_memory_unsigned_integer (next_cfa + 8, word_size);
2155     }
2156
2157   /* The sigregs structure looks like this:
2158             long   psw_mask;
2159             long   psw_addr;
2160             long   gprs[16];
2161             int    acrs[16];
2162             int    fpc;
2163             int    __pad;
2164             double fprs[16];  */
2165
2166   /* Let's ignore the PSW mask, it will not be restored anyway.  */
2167   sigreg_ptr += word_size;
2168
2169   /* Next comes the PSW address.  */
2170   info->saved_regs[S390_PC_REGNUM].addr = sigreg_ptr;
2171   sigreg_ptr += word_size;
2172
2173   /* Then the GPRs.  */
2174   for (i = 0; i < 16; i++)
2175     {
2176       info->saved_regs[S390_R0_REGNUM + i].addr = sigreg_ptr;
2177       sigreg_ptr += word_size;
2178     }
2179
2180   /* Then the ACRs.  */
2181   for (i = 0; i < 16; i++)
2182     {
2183       info->saved_regs[S390_A0_REGNUM + i].addr = sigreg_ptr;
2184       sigreg_ptr += 4;
2185     }
2186
2187   /* The floating-point control word.  */
2188   info->saved_regs[S390_FPC_REGNUM].addr = sigreg_ptr;
2189   sigreg_ptr += 8;
2190
2191   /* And finally the FPRs.  */
2192   for (i = 0; i < 16; i++)
2193     {
2194       info->saved_regs[S390_F0_REGNUM + i].addr = sigreg_ptr;
2195       sigreg_ptr += 8;
2196     }
2197
2198   /* Restore the previous frame's SP.  */
2199   prev_sp = read_memory_unsigned_integer (
2200                         info->saved_regs[S390_SP_REGNUM].addr,
2201                         word_size);
2202
2203   /* Determine our frame base.  */
2204   info->frame_base = prev_sp + 16*word_size + 32;
2205
2206   return info;
2207 }
2208
2209 static void
2210 s390_sigtramp_frame_this_id (struct frame_info *next_frame,
2211                              void **this_prologue_cache,
2212                              struct frame_id *this_id)
2213 {
2214   struct s390_sigtramp_unwind_cache *info
2215     = s390_sigtramp_frame_unwind_cache (next_frame, this_prologue_cache);
2216   *this_id = frame_id_build (info->frame_base, frame_pc_unwind (next_frame));
2217 }
2218
2219 static void
2220 s390_sigtramp_frame_prev_register (struct frame_info *next_frame,
2221                                    void **this_prologue_cache,
2222                                    int regnum, int *optimizedp,
2223                                    enum lval_type *lvalp, CORE_ADDR *addrp,
2224                                    int *realnump, void *bufferp)
2225 {
2226   struct s390_sigtramp_unwind_cache *info
2227     = s390_sigtramp_frame_unwind_cache (next_frame, this_prologue_cache);
2228   trad_frame_prev_register (next_frame, info->saved_regs, regnum,
2229                             optimizedp, lvalp, addrp, realnump, bufferp);
2230 }
2231
2232 static const struct frame_unwind s390_sigtramp_frame_unwind = {
2233   SIGTRAMP_FRAME,
2234   s390_sigtramp_frame_this_id,
2235   s390_sigtramp_frame_prev_register
2236 };
2237
2238 static const struct frame_unwind *
2239 s390_sigtramp_frame_sniffer (struct frame_info *next_frame)
2240 {
2241   CORE_ADDR pc = frame_pc_unwind (next_frame);
2242   bfd_byte sigreturn[2];
2243
2244   if (read_memory_nobpt (pc, sigreturn, 2))
2245     return NULL;
2246
2247   if (sigreturn[0] != 0x0a /* svc */)
2248     return NULL;
2249
2250   if (sigreturn[1] != 119 /* sigreturn */
2251       && sigreturn[1] != 173 /* rt_sigreturn */)
2252     return NULL;
2253   
2254   return &s390_sigtramp_frame_unwind;
2255 }
2256
2257
2258 /* Frame base handling.  */
2259
2260 static CORE_ADDR
2261 s390_frame_base_address (struct frame_info *next_frame, void **this_cache)
2262 {
2263   struct s390_unwind_cache *info
2264     = s390_frame_unwind_cache (next_frame, this_cache);
2265   return info->frame_base;
2266 }
2267
2268 static CORE_ADDR
2269 s390_local_base_address (struct frame_info *next_frame, void **this_cache)
2270 {
2271   struct s390_unwind_cache *info
2272     = s390_frame_unwind_cache (next_frame, this_cache);
2273   return info->local_base;
2274 }
2275
2276 static const struct frame_base s390_frame_base = {
2277   &s390_frame_unwind,
2278   s390_frame_base_address,
2279   s390_local_base_address,
2280   s390_local_base_address
2281 };
2282
2283 static CORE_ADDR
2284 s390_unwind_pc (struct gdbarch *gdbarch, struct frame_info *next_frame)
2285 {
2286   ULONGEST pc;
2287   pc = frame_unwind_register_unsigned (next_frame, S390_PC_REGNUM);
2288   return gdbarch_addr_bits_remove (gdbarch, pc);
2289 }
2290
2291 static CORE_ADDR
2292 s390_unwind_sp (struct gdbarch *gdbarch, struct frame_info *next_frame)
2293 {
2294   ULONGEST sp;
2295   sp = frame_unwind_register_unsigned (next_frame, S390_SP_REGNUM);
2296   return gdbarch_addr_bits_remove (gdbarch, sp);
2297 }
2298
2299
2300 /* Dummy function calls.  */
2301
2302 /* Return non-zero if TYPE is an integer-like type, zero otherwise.
2303    "Integer-like" types are those that should be passed the way
2304    integers are: integers, enums, ranges, characters, and booleans.  */
2305 static int
2306 is_integer_like (struct type *type)
2307 {
2308   enum type_code code = TYPE_CODE (type);
2309
2310   return (code == TYPE_CODE_INT
2311           || code == TYPE_CODE_ENUM
2312           || code == TYPE_CODE_RANGE
2313           || code == TYPE_CODE_CHAR
2314           || code == TYPE_CODE_BOOL);
2315 }
2316
2317 /* Return non-zero if TYPE is a pointer-like type, zero otherwise.
2318    "Pointer-like" types are those that should be passed the way
2319    pointers are: pointers and references.  */
2320 static int
2321 is_pointer_like (struct type *type)
2322 {
2323   enum type_code code = TYPE_CODE (type);
2324
2325   return (code == TYPE_CODE_PTR
2326           || code == TYPE_CODE_REF);
2327 }
2328
2329
2330 /* Return non-zero if TYPE is a `float singleton' or `double
2331    singleton', zero otherwise.
2332
2333    A `T singleton' is a struct type with one member, whose type is
2334    either T or a `T singleton'.  So, the following are all float
2335    singletons:
2336
2337    struct { float x };
2338    struct { struct { float x; } x; };
2339    struct { struct { struct { float x; } x; } x; };
2340
2341    ... and so on.
2342
2343    All such structures are passed as if they were floats or doubles,
2344    as the (revised) ABI says.  */
2345 static int
2346 is_float_singleton (struct type *type)
2347 {
2348   if (TYPE_CODE (type) == TYPE_CODE_STRUCT && TYPE_NFIELDS (type) == 1)
2349     {
2350       struct type *singleton_type = TYPE_FIELD_TYPE (type, 0);
2351       CHECK_TYPEDEF (singleton_type);
2352
2353       return (TYPE_CODE (singleton_type) == TYPE_CODE_FLT
2354               || is_float_singleton (singleton_type));
2355     }
2356
2357   return 0;
2358 }
2359
2360
2361 /* Return non-zero if TYPE is a struct-like type, zero otherwise.
2362    "Struct-like" types are those that should be passed as structs are:
2363    structs and unions.
2364
2365    As an odd quirk, not mentioned in the ABI, GCC passes float and
2366    double singletons as if they were a plain float, double, etc.  (The
2367    corresponding union types are handled normally.)  So we exclude
2368    those types here.  *shrug* */
2369 static int
2370 is_struct_like (struct type *type)
2371 {
2372   enum type_code code = TYPE_CODE (type);
2373
2374   return (code == TYPE_CODE_UNION
2375           || (code == TYPE_CODE_STRUCT && ! is_float_singleton (type)));
2376 }
2377
2378
2379 /* Return non-zero if TYPE is a float-like type, zero otherwise.
2380    "Float-like" types are those that should be passed as
2381    floating-point values are.
2382
2383    You'd think this would just be floats, doubles, long doubles, etc.
2384    But as an odd quirk, not mentioned in the ABI, GCC passes float and
2385    double singletons as if they were a plain float, double, etc.  (The
2386    corresponding union types are handled normally.)  So we include
2387    those types here.  *shrug* */
2388 static int
2389 is_float_like (struct type *type)
2390 {
2391   return (TYPE_CODE (type) == TYPE_CODE_FLT
2392           || is_float_singleton (type));
2393 }
2394
2395
2396 static int
2397 is_power_of_two (unsigned int n)
2398 {
2399   return ((n & (n - 1)) == 0);
2400 }
2401
2402 /* Return non-zero if TYPE should be passed as a pointer to a copy,
2403    zero otherwise.  */
2404 static int
2405 s390_function_arg_pass_by_reference (struct type *type)
2406 {
2407   unsigned length = TYPE_LENGTH (type);
2408   if (length > 8)
2409     return 1;
2410
2411   /* FIXME: All complex and vector types are also returned by reference.  */
2412   return is_struct_like (type) && !is_power_of_two (length);
2413 }
2414
2415 /* Return non-zero if TYPE should be passed in a float register
2416    if possible.  */
2417 static int
2418 s390_function_arg_float (struct type *type)
2419 {
2420   unsigned length = TYPE_LENGTH (type);
2421   if (length > 8)
2422     return 0;
2423
2424   return is_float_like (type);
2425 }
2426
2427 /* Return non-zero if TYPE should be passed in an integer register
2428    (or a pair of integer registers) if possible.  */
2429 static int
2430 s390_function_arg_integer (struct type *type)
2431 {
2432   unsigned length = TYPE_LENGTH (type);
2433   if (length > 8)
2434     return 0;
2435
2436    return is_integer_like (type)
2437           || is_pointer_like (type)
2438           || (is_struct_like (type) && is_power_of_two (length));
2439 }
2440
2441 /* Return ARG, a `SIMPLE_ARG', sign-extended or zero-extended to a full
2442    word as required for the ABI.  */
2443 static LONGEST
2444 extend_simple_arg (struct value *arg)
2445 {
2446   struct type *type = VALUE_TYPE (arg);
2447
2448   /* Even structs get passed in the least significant bits of the
2449      register / memory word.  It's not really right to extract them as
2450      an integer, but it does take care of the extension.  */
2451   if (TYPE_UNSIGNED (type))
2452     return extract_unsigned_integer (VALUE_CONTENTS (arg),
2453                                      TYPE_LENGTH (type));
2454   else
2455     return extract_signed_integer (VALUE_CONTENTS (arg),
2456                                    TYPE_LENGTH (type));
2457 }
2458
2459
2460 /* Return the alignment required by TYPE.  */
2461 static int
2462 alignment_of (struct type *type)
2463 {
2464   int alignment;
2465
2466   if (is_integer_like (type)
2467       || is_pointer_like (type)
2468       || TYPE_CODE (type) == TYPE_CODE_FLT)
2469     alignment = TYPE_LENGTH (type);
2470   else if (TYPE_CODE (type) == TYPE_CODE_STRUCT
2471            || TYPE_CODE (type) == TYPE_CODE_UNION)
2472     {
2473       int i;
2474
2475       alignment = 1;
2476       for (i = 0; i < TYPE_NFIELDS (type); i++)
2477         {
2478           int field_alignment = alignment_of (TYPE_FIELD_TYPE (type, i));
2479
2480           if (field_alignment > alignment)
2481             alignment = field_alignment;
2482         }
2483     }
2484   else
2485     alignment = 1;
2486
2487   /* Check that everything we ever return is a power of two.  Lots of
2488      code doesn't want to deal with aligning things to arbitrary
2489      boundaries.  */
2490   gdb_assert ((alignment & (alignment - 1)) == 0);
2491
2492   return alignment;
2493 }
2494
2495
2496 /* Put the actual parameter values pointed to by ARGS[0..NARGS-1] in
2497    place to be passed to a function, as specified by the "GNU/Linux
2498    for S/390 ELF Application Binary Interface Supplement".
2499
2500    SP is the current stack pointer.  We must put arguments, links,
2501    padding, etc. whereever they belong, and return the new stack
2502    pointer value.
2503    
2504    If STRUCT_RETURN is non-zero, then the function we're calling is
2505    going to return a structure by value; STRUCT_ADDR is the address of
2506    a block we've allocated for it on the stack.
2507
2508    Our caller has taken care of any type promotions needed to satisfy
2509    prototypes or the old K&R argument-passing rules.  */
2510 static CORE_ADDR
2511 s390_push_dummy_call (struct gdbarch *gdbarch, CORE_ADDR func_addr,
2512                       struct regcache *regcache, CORE_ADDR bp_addr,
2513                       int nargs, struct value **args, CORE_ADDR sp,
2514                       int struct_return, CORE_ADDR struct_addr)
2515 {
2516   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
2517   int word_size = gdbarch_ptr_bit (gdbarch) / 8;
2518   ULONGEST orig_sp;
2519   int i;
2520
2521   /* If the i'th argument is passed as a reference to a copy, then
2522      copy_addr[i] is the address of the copy we made.  */
2523   CORE_ADDR *copy_addr = alloca (nargs * sizeof (CORE_ADDR));
2524
2525   /* Build the reference-to-copy area.  */
2526   for (i = 0; i < nargs; i++)
2527     {
2528       struct value *arg = args[i];
2529       struct type *type = VALUE_TYPE (arg);
2530       unsigned length = TYPE_LENGTH (type);
2531
2532       if (s390_function_arg_pass_by_reference (type))
2533         {
2534           sp -= length;
2535           sp = align_down (sp, alignment_of (type));
2536           write_memory (sp, VALUE_CONTENTS (arg), length);
2537           copy_addr[i] = sp;
2538         }
2539     }
2540
2541   /* Reserve space for the parameter area.  As a conservative
2542      simplification, we assume that everything will be passed on the
2543      stack.  Since every argument larger than 8 bytes will be 
2544      passed by reference, we use this simple upper bound.  */
2545   sp -= nargs * 8;
2546
2547   /* After all that, make sure it's still aligned on an eight-byte
2548      boundary.  */
2549   sp = align_down (sp, 8);
2550
2551   /* Finally, place the actual parameters, working from SP towards
2552      higher addresses.  The code above is supposed to reserve enough
2553      space for this.  */
2554   {
2555     int fr = 0;
2556     int gr = 2;
2557     CORE_ADDR starg = sp;
2558
2559     /* A struct is returned using general register 2.  */
2560     if (struct_return)
2561       {
2562         regcache_cooked_write_unsigned (regcache, S390_R0_REGNUM + gr,
2563                                         struct_addr);
2564         gr++;
2565       }
2566
2567     for (i = 0; i < nargs; i++)
2568       {
2569         struct value *arg = args[i];
2570         struct type *type = VALUE_TYPE (arg);
2571         unsigned length = TYPE_LENGTH (type);
2572
2573         if (s390_function_arg_pass_by_reference (type))
2574           {
2575             if (gr <= 6)
2576               {
2577                 regcache_cooked_write_unsigned (regcache, S390_R0_REGNUM + gr,
2578                                                 copy_addr[i]);
2579                 gr++;
2580               }
2581             else
2582               {
2583                 write_memory_unsigned_integer (starg, word_size, copy_addr[i]);
2584                 starg += word_size;
2585               }
2586           }
2587         else if (s390_function_arg_float (type))
2588           {
2589             /* The GNU/Linux for S/390 ABI uses FPRs 0 and 2 to pass arguments,
2590                the GNU/Linux for zSeries ABI uses 0, 2, 4, and 6.  */
2591             if (fr <= (tdep->abi == ABI_LINUX_S390 ? 2 : 6))
2592               {
2593                 /* When we store a single-precision value in an FP register,
2594                    it occupies the leftmost bits.  */
2595                 regcache_cooked_write_part (regcache, S390_F0_REGNUM + fr,
2596                                             0, length, VALUE_CONTENTS (arg));
2597                 fr += 2;
2598               }
2599             else
2600               {
2601                 /* When we store a single-precision value in a stack slot,
2602                    it occupies the rightmost bits.  */
2603                 starg = align_up (starg + length, word_size);
2604                 write_memory (starg - length, VALUE_CONTENTS (arg), length);
2605               }
2606           }
2607         else if (s390_function_arg_integer (type) && length <= word_size)
2608           {
2609             if (gr <= 6)
2610               {
2611                 /* Integer arguments are always extended to word size.  */
2612                 regcache_cooked_write_signed (regcache, S390_R0_REGNUM + gr,
2613                                               extend_simple_arg (arg));
2614                 gr++;
2615               }
2616             else
2617               {
2618                 /* Integer arguments are always extended to word size.  */
2619                 write_memory_signed_integer (starg, word_size,
2620                                              extend_simple_arg (arg));
2621                 starg += word_size;
2622               }
2623           }
2624         else if (s390_function_arg_integer (type) && length == 2*word_size)
2625           {
2626             if (gr <= 5)
2627               {
2628                 regcache_cooked_write (regcache, S390_R0_REGNUM + gr,
2629                                        VALUE_CONTENTS (arg));
2630                 regcache_cooked_write (regcache, S390_R0_REGNUM + gr + 1,
2631                                        VALUE_CONTENTS (arg) + word_size);
2632                 gr += 2;
2633               }
2634             else
2635               {
2636                 /* If we skipped r6 because we couldn't fit a DOUBLE_ARG
2637                    in it, then don't go back and use it again later.  */
2638                 gr = 7;
2639
2640                 write_memory (starg, VALUE_CONTENTS (arg), length);
2641                 starg += length;
2642               }
2643           }
2644         else
2645           internal_error (__FILE__, __LINE__, "unknown argument type");
2646       }
2647   }
2648
2649   /* Allocate the standard frame areas: the register save area, the
2650      word reserved for the compiler (which seems kind of meaningless),
2651      and the back chain pointer.  */
2652   sp -= 16*word_size + 32;
2653
2654   /* Write the back chain pointer into the first word of the stack
2655      frame.  This is needed to unwind across a dummy frame.  */
2656   regcache_cooked_read_unsigned (regcache, S390_SP_REGNUM, &orig_sp);
2657   write_memory_unsigned_integer (sp, word_size, orig_sp);
2658
2659   /* Store return address.  */
2660   regcache_cooked_write_unsigned (regcache, S390_RETADDR_REGNUM, bp_addr);
2661   
2662   /* Store updated stack pointer.  */
2663   regcache_cooked_write_unsigned (regcache, S390_SP_REGNUM, sp);
2664
2665   /* We need to return the 'stack part' of the frame ID,
2666      which is actually the top of the register save area
2667      allocated on the original stack.  */
2668   return orig_sp + 16*word_size + 32;
2669 }
2670
2671 /* Assuming NEXT_FRAME->prev is a dummy, return the frame ID of that
2672    dummy frame.  The frame ID's base needs to match the TOS value
2673    returned by push_dummy_call, and the PC match the dummy frame's
2674    breakpoint.  */
2675 static struct frame_id
2676 s390_unwind_dummy_id (struct gdbarch *gdbarch, struct frame_info *next_frame)
2677 {
2678   int word_size = gdbarch_ptr_bit (gdbarch) / 8;
2679   CORE_ADDR this_sp = s390_unwind_sp (gdbarch, next_frame);
2680   CORE_ADDR prev_sp = read_memory_unsigned_integer (this_sp, word_size);
2681
2682   return frame_id_build (prev_sp + 16*word_size + 32,
2683                          frame_pc_unwind (next_frame));
2684 }
2685
2686 static CORE_ADDR
2687 s390_frame_align (struct gdbarch *gdbarch, CORE_ADDR addr)
2688 {
2689   /* Both the 32- and 64-bit ABI's say that the stack pointer should
2690      always be aligned on an eight-byte boundary.  */
2691   return (addr & -8);
2692 }
2693
2694
2695 /* Function return value access.  */
2696
2697 static enum return_value_convention
2698 s390_return_value_convention (struct gdbarch *gdbarch, struct type *type)
2699 {
2700   int length = TYPE_LENGTH (type);
2701   if (length > 8)
2702     return RETURN_VALUE_STRUCT_CONVENTION;
2703
2704   switch (TYPE_CODE (type))
2705     {
2706     case TYPE_CODE_STRUCT:
2707     case TYPE_CODE_UNION:
2708     case TYPE_CODE_ARRAY:
2709       return RETURN_VALUE_STRUCT_CONVENTION;
2710
2711     default:
2712       return RETURN_VALUE_REGISTER_CONVENTION;
2713     }
2714 }
2715
2716 static enum return_value_convention
2717 s390_return_value (struct gdbarch *gdbarch, struct type *type, 
2718                    struct regcache *regcache, void *out, const void *in)
2719 {
2720   int word_size = gdbarch_ptr_bit (gdbarch) / 8;
2721   int length = TYPE_LENGTH (type);
2722   enum return_value_convention rvc = 
2723                         s390_return_value_convention (gdbarch, type);
2724   if (in)
2725     {
2726       switch (rvc)
2727         {
2728         case RETURN_VALUE_REGISTER_CONVENTION:
2729           if (TYPE_CODE (type) == TYPE_CODE_FLT)
2730             {
2731               /* When we store a single-precision value in an FP register,
2732                  it occupies the leftmost bits.  */
2733               regcache_cooked_write_part (regcache, S390_F0_REGNUM, 
2734                                           0, length, in);
2735             }
2736           else if (length <= word_size)
2737             {
2738               /* Integer arguments are always extended to word size.  */
2739               if (TYPE_UNSIGNED (type))
2740                 regcache_cooked_write_unsigned (regcache, S390_R2_REGNUM,
2741                         extract_unsigned_integer (in, length));
2742               else
2743                 regcache_cooked_write_signed (regcache, S390_R2_REGNUM,
2744                         extract_signed_integer (in, length));
2745             }
2746           else if (length == 2*word_size)
2747             {
2748               regcache_cooked_write (regcache, S390_R2_REGNUM, in);
2749               regcache_cooked_write (regcache, S390_R3_REGNUM,
2750                                      (const char *)in + word_size);
2751             }
2752           else
2753             internal_error (__FILE__, __LINE__, "invalid return type");
2754           break;
2755
2756         case RETURN_VALUE_STRUCT_CONVENTION:
2757           error ("Cannot set function return value.");
2758           break;
2759         }
2760     }
2761   else if (out)
2762     {
2763       switch (rvc)
2764         {
2765         case RETURN_VALUE_REGISTER_CONVENTION:
2766           if (TYPE_CODE (type) == TYPE_CODE_FLT)
2767             {
2768               /* When we store a single-precision value in an FP register,
2769                  it occupies the leftmost bits.  */
2770               regcache_cooked_read_part (regcache, S390_F0_REGNUM, 
2771                                          0, length, out);
2772             }
2773           else if (length <= word_size)
2774             {
2775               /* Integer arguments occupy the rightmost bits.  */
2776               regcache_cooked_read_part (regcache, S390_R2_REGNUM, 
2777                                          word_size - length, length, out);
2778             }
2779           else if (length == 2*word_size)
2780             {
2781               regcache_cooked_read (regcache, S390_R2_REGNUM, out);
2782               regcache_cooked_read (regcache, S390_R3_REGNUM,
2783                                     (char *)out + word_size);
2784             }
2785           else
2786             internal_error (__FILE__, __LINE__, "invalid return type");
2787           break;
2788
2789         case RETURN_VALUE_STRUCT_CONVENTION:
2790           error ("Function return value unknown.");
2791           break;
2792         }
2793     }
2794
2795   return rvc;
2796 }
2797
2798
2799 /* Breakpoints.  */
2800
2801 static const unsigned char *
2802 s390_breakpoint_from_pc (CORE_ADDR *pcptr, int *lenptr)
2803 {
2804   static unsigned char breakpoint[] = { 0x0, 0x1 };
2805
2806   *lenptr = sizeof (breakpoint);
2807   return breakpoint;
2808 }
2809
2810
2811 /* Address handling.  */
2812
2813 static CORE_ADDR
2814 s390_addr_bits_remove (CORE_ADDR addr)
2815 {
2816   return addr & 0x7fffffff;
2817 }
2818
2819 static int
2820 s390_address_class_type_flags (int byte_size, int dwarf2_addr_class)
2821 {
2822   if (byte_size == 4)
2823     return TYPE_FLAG_ADDRESS_CLASS_1;
2824   else
2825     return 0;
2826 }
2827
2828 static const char *
2829 s390_address_class_type_flags_to_name (struct gdbarch *gdbarch, int type_flags)
2830 {
2831   if (type_flags & TYPE_FLAG_ADDRESS_CLASS_1)
2832     return "mode32";
2833   else
2834     return NULL;
2835 }
2836
2837 static int
2838 s390_address_class_name_to_type_flags (struct gdbarch *gdbarch, const char *name,
2839                                        int *type_flags_ptr)
2840 {
2841   if (strcmp (name, "mode32") == 0)
2842     {
2843       *type_flags_ptr = TYPE_FLAG_ADDRESS_CLASS_1;
2844       return 1;
2845     }
2846   else
2847     return 0;
2848 }
2849
2850
2851 /* Set up gdbarch struct.  */
2852
2853 static struct gdbarch *
2854 s390_gdbarch_init (struct gdbarch_info info, struct gdbarch_list *arches)
2855 {
2856   struct gdbarch *gdbarch;
2857   struct gdbarch_tdep *tdep;
2858
2859   /* First see if there is already a gdbarch that can satisfy the request.  */
2860   arches = gdbarch_list_lookup_by_info (arches, &info);
2861   if (arches != NULL)
2862     return arches->gdbarch;
2863
2864   /* None found: is the request for a s390 architecture? */
2865   if (info.bfd_arch_info->arch != bfd_arch_s390)
2866     return NULL;                /* No; then it's not for us.  */
2867
2868   /* Yes: create a new gdbarch for the specified machine type.  */
2869   tdep = XCALLOC (1, struct gdbarch_tdep);
2870   gdbarch = gdbarch_alloc (&info, tdep);
2871
2872   set_gdbarch_believe_pcc_promotion (gdbarch, 0);
2873   set_gdbarch_char_signed (gdbarch, 0);
2874
2875   /* Amount PC must be decremented by after a breakpoint.  This is
2876      often the number of bytes returned by BREAKPOINT_FROM_PC but not
2877      always.  */
2878   set_gdbarch_decr_pc_after_break (gdbarch, 2);
2879   /* Stack grows downward.  */
2880   set_gdbarch_inner_than (gdbarch, core_addr_lessthan);
2881   set_gdbarch_breakpoint_from_pc (gdbarch, s390_breakpoint_from_pc);
2882   set_gdbarch_skip_prologue (gdbarch, s390_skip_prologue);
2883   set_gdbarch_in_function_epilogue_p (gdbarch, s390_in_function_epilogue_p);
2884
2885   set_gdbarch_pc_regnum (gdbarch, S390_PC_REGNUM);
2886   set_gdbarch_sp_regnum (gdbarch, S390_SP_REGNUM);
2887   set_gdbarch_fp0_regnum (gdbarch, S390_F0_REGNUM);
2888   set_gdbarch_num_regs (gdbarch, S390_NUM_REGS);
2889   set_gdbarch_num_pseudo_regs (gdbarch, S390_NUM_PSEUDO_REGS);
2890   set_gdbarch_register_name (gdbarch, s390_register_name);
2891   set_gdbarch_register_type (gdbarch, s390_register_type);
2892   set_gdbarch_stab_reg_to_regnum (gdbarch, s390_dwarf_reg_to_regnum);
2893   set_gdbarch_dwarf_reg_to_regnum (gdbarch, s390_dwarf_reg_to_regnum);
2894   set_gdbarch_dwarf2_reg_to_regnum (gdbarch, s390_dwarf_reg_to_regnum);
2895   set_gdbarch_convert_register_p (gdbarch, s390_convert_register_p);
2896   set_gdbarch_register_to_value (gdbarch, s390_register_to_value);
2897   set_gdbarch_value_to_register (gdbarch, s390_value_to_register);
2898   set_gdbarch_register_reggroup_p (gdbarch, s390_register_reggroup_p);
2899   set_gdbarch_regset_from_core_section (gdbarch,
2900                                         s390_regset_from_core_section);
2901
2902   /* Inferior function calls.  */
2903   set_gdbarch_push_dummy_call (gdbarch, s390_push_dummy_call);
2904   set_gdbarch_unwind_dummy_id (gdbarch, s390_unwind_dummy_id);
2905   set_gdbarch_frame_align (gdbarch, s390_frame_align);
2906   set_gdbarch_return_value (gdbarch, s390_return_value);
2907
2908   /* Frame handling.  */
2909   set_gdbarch_in_solib_call_trampoline (gdbarch, in_plt_section);
2910   frame_unwind_append_sniffer (gdbarch, s390_pltstub_frame_sniffer);
2911   frame_unwind_append_sniffer (gdbarch, s390_sigtramp_frame_sniffer);
2912   frame_unwind_append_sniffer (gdbarch, s390_frame_sniffer);
2913   frame_base_set_default (gdbarch, &s390_frame_base);
2914   set_gdbarch_unwind_pc (gdbarch, s390_unwind_pc);
2915   set_gdbarch_unwind_sp (gdbarch, s390_unwind_sp);
2916
2917   switch (info.bfd_arch_info->mach)
2918     {
2919     case bfd_mach_s390_31:
2920       tdep->abi = ABI_LINUX_S390;
2921
2922       tdep->gregset = &s390_gregset;
2923       tdep->sizeof_gregset = s390_sizeof_gregset;
2924       tdep->fpregset = &s390_fpregset;
2925       tdep->sizeof_fpregset = s390_sizeof_fpregset;
2926
2927       set_gdbarch_addr_bits_remove (gdbarch, s390_addr_bits_remove);
2928       set_gdbarch_pseudo_register_read (gdbarch, s390_pseudo_register_read);
2929       set_gdbarch_pseudo_register_write (gdbarch, s390_pseudo_register_write);
2930       break;
2931     case bfd_mach_s390_64:
2932       tdep->abi = ABI_LINUX_ZSERIES;
2933
2934       tdep->gregset = &s390x_gregset;
2935       tdep->sizeof_gregset = s390x_sizeof_gregset;
2936       tdep->fpregset = &s390_fpregset;
2937       tdep->sizeof_fpregset = s390_sizeof_fpregset;
2938
2939       set_gdbarch_long_bit (gdbarch, 64);
2940       set_gdbarch_long_long_bit (gdbarch, 64);
2941       set_gdbarch_ptr_bit (gdbarch, 64);
2942       set_gdbarch_pseudo_register_read (gdbarch, s390x_pseudo_register_read);
2943       set_gdbarch_pseudo_register_write (gdbarch, s390x_pseudo_register_write);
2944       set_gdbarch_address_class_type_flags (gdbarch,
2945                                             s390_address_class_type_flags);
2946       set_gdbarch_address_class_type_flags_to_name (gdbarch,
2947                                                     s390_address_class_type_flags_to_name);
2948       set_gdbarch_address_class_name_to_type_flags (gdbarch,
2949                                                     s390_address_class_name_to_type_flags);
2950       break;
2951     }
2952
2953   set_gdbarch_print_insn (gdbarch, print_insn_s390);
2954
2955   return gdbarch;
2956 }
2957
2958
2959
2960 extern initialize_file_ftype _initialize_s390_tdep; /* -Wmissing-prototypes */
2961
2962 void
2963 _initialize_s390_tdep (void)
2964 {
2965
2966   /* Hook us into the gdbarch mechanism.  */
2967   register_gdbarch_init (bfd_arch_s390, s390_gdbarch_init);
2968 }