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