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