s/Linux/.../
[external/binutils.git] / gdb / s390-tdep.c
1 /* Target-dependent code for GDB, the GNU debugger.
2
3    Copyright 2001, 2002 Free Software Foundation, Inc.
4
5    Contributed by D.J. Barrow (djbarrow@de.ibm.com,barrow_dj@yahoo.com)
6    for IBM Deutschland Entwicklung GmbH, IBM Corporation.
7
8    This file is part of GDB.
9
10    This program is free software; you can redistribute it and/or modify
11    it under the terms of the GNU General Public License as published by
12    the Free Software Foundation; either version 2 of the License, or
13    (at your option) any later version.
14
15    This program is distributed in the hope that it will be useful,
16    but WITHOUT ANY WARRANTY; without even the implied warranty of
17    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18    GNU General Public License for more details.
19
20    You should have received a copy of the GNU General Public License
21    along with this program; if not, write to the Free Software
22    Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
23    02111-1307, USA.  */
24
25 #define S390_TDEP               /* for special macros in tm-s390.h */
26 #include <defs.h>
27 #include "arch-utils.h"
28 #include "frame.h"
29 #include "inferior.h"
30 #include "symtab.h"
31 #include "target.h"
32 #include "gdbcore.h"
33 #include "gdbcmd.h"
34 #include "symfile.h"
35 #include "objfiles.h"
36 #include "tm.h"
37 #include "../bfd/bfd.h"
38 #include "floatformat.h"
39 #include "regcache.h"
40 #include "value.h"
41 #include "gdb_assert.h"
42
43
44
45
46 /* Number of bytes of storage in the actual machine representation
47    for register N.  */
48 int
49 s390_register_raw_size (int reg_nr)
50 {
51   if (S390_FP0_REGNUM <= reg_nr
52       && reg_nr < S390_FP0_REGNUM + S390_NUM_FPRS)
53     return S390_FPR_SIZE;
54   else
55     return 4;
56 }
57
58 int
59 s390x_register_raw_size (int reg_nr)
60 {
61   return (reg_nr == S390_FPC_REGNUM)
62     || (reg_nr >= S390_FIRST_ACR && reg_nr <= S390_LAST_ACR) ? 4 : 8;
63 }
64
65 int
66 s390_cannot_fetch_register (int regno)
67 {
68   return (regno >= S390_FIRST_CR && regno < (S390_FIRST_CR + 9)) ||
69     (regno >= (S390_FIRST_CR + 12) && regno <= S390_LAST_CR);
70 }
71
72 int
73 s390_register_byte (int reg_nr)
74 {
75   if (reg_nr <= S390_GP_LAST_REGNUM)
76     return reg_nr * S390_GPR_SIZE;
77   if (reg_nr <= S390_LAST_ACR)
78     return S390_ACR0_OFFSET + (((reg_nr) - S390_FIRST_ACR) * S390_ACR_SIZE);
79   if (reg_nr <= S390_LAST_CR)
80     return S390_CR0_OFFSET + (((reg_nr) - S390_FIRST_CR) * S390_CR_SIZE);
81   if (reg_nr == S390_FPC_REGNUM)
82     return S390_FPC_OFFSET;
83   else
84     return S390_FP0_OFFSET + (((reg_nr) - S390_FP0_REGNUM) * S390_FPR_SIZE);
85 }
86
87 #ifndef GDBSERVER
88 #define S390_MAX_INSTR_SIZE (6)
89 #define S390_SYSCALL_OPCODE (0x0a)
90 #define S390_SYSCALL_SIZE   (2)
91 #define S390_SIGCONTEXT_SREGS_OFFSET (8)
92 #define S390X_SIGCONTEXT_SREGS_OFFSET (8)
93 #define S390_SIGREGS_FP0_OFFSET       (144)
94 #define S390X_SIGREGS_FP0_OFFSET      (216)
95 #define S390_UC_MCONTEXT_OFFSET    (256)
96 #define S390X_UC_MCONTEXT_OFFSET   (344)
97 #define S390_STACK_FRAME_OVERHEAD  (GDB_TARGET_IS_ESAME ? 160:96)
98 #define S390_SIGNAL_FRAMESIZE  (GDB_TARGET_IS_ESAME ? 160:96)
99 #define s390_NR_sigreturn          119
100 #define s390_NR_rt_sigreturn       173
101
102
103
104 struct frame_extra_info
105 {
106   int initialised;
107   int good_prologue;
108   CORE_ADDR function_start;
109   CORE_ADDR skip_prologue_function_start;
110   CORE_ADDR saved_pc_valid;
111   CORE_ADDR saved_pc;
112   CORE_ADDR sig_fixed_saved_pc_valid;
113   CORE_ADDR sig_fixed_saved_pc;
114   CORE_ADDR frame_pointer_saved_pc;     /* frame pointer needed for alloca */
115   CORE_ADDR stack_bought;       /* amount we decrement the stack pointer by */
116   CORE_ADDR sigcontext;
117 };
118
119
120 static CORE_ADDR s390_frame_saved_pc_nofix (struct frame_info *fi);
121
122 int
123 s390_readinstruction (bfd_byte instr[], CORE_ADDR at,
124                       struct disassemble_info *info)
125 {
126   int instrlen;
127
128   static int s390_instrlen[] = {
129     2,
130     4,
131     4,
132     6
133   };
134   if ((*info->read_memory_func) (at, &instr[0], 2, info))
135     return -1;
136   instrlen = s390_instrlen[instr[0] >> 6];
137   if (instrlen > 2)
138     {
139       if ((*info->read_memory_func) (at + 2, &instr[2], instrlen - 2, info))
140         return -1;
141     }
142   return instrlen;
143 }
144
145 static void
146 s390_memset_extra_info (struct frame_extra_info *fextra_info)
147 {
148   memset (fextra_info, 0, sizeof (struct frame_extra_info));
149 }
150
151
152
153 char *
154 s390_register_name (int reg_nr)
155 {
156   static char *register_names[] = {
157     "pswm", "pswa",
158     "r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7",
159     "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15",
160     "acr0", "acr1", "acr2", "acr3", "acr4", "acr5", "acr6", "acr7",
161     "acr8", "acr9", "acr10", "acr11", "acr12", "acr13", "acr14", "acr15",
162     "cr0", "cr1", "cr2", "cr3", "cr4", "cr5", "cr6", "cr7",
163     "cr8", "cr9", "cr10", "cr11", "cr12", "cr13", "cr14", "cr15",
164     "fpc",
165     "f0", "f1", "f2", "f3", "f4", "f5", "f6", "f7",
166     "f8", "f9", "f10", "f11", "f12", "f13", "f14", "f15"
167   };
168
169   if (reg_nr <= S390_LAST_REGNUM)
170     return register_names[reg_nr];
171   else
172     return NULL;
173 }
174
175
176
177
178 int
179 s390_stab_reg_to_regnum (int regno)
180 {
181   return regno >= 64 ? S390_PSWM_REGNUM - 64 :
182     regno >= 48 ? S390_FIRST_ACR - 48 :
183     regno >= 32 ? S390_FIRST_CR - 32 :
184     regno <= 15 ? (regno + 2) :
185     S390_FP0_REGNUM + ((regno - 16) & 8) + (((regno - 16) & 3) << 1) +
186     (((regno - 16) & 4) >> 2);
187 }
188
189
190 /* Return true if REGIDX is the number of a register used to pass
191      arguments, false otherwise.  */
192 static int
193 is_arg_reg (int regidx)
194 {
195   return 2 <= regidx && regidx <= 6;
196 }
197
198
199 /* s390_get_frame_info based on Hartmuts
200    prologue definition in
201    gcc-2.8.1/config/l390/linux.c 
202
203    It reads one instruction at a time & based on whether
204    it looks like prologue code or not it makes a decision on
205    whether the prologue is over, there are various state machines
206    in the code to determine if the prologue code is possilby valid.
207    
208    This is done to hopefully allow the code survive minor revs of
209    calling conventions.
210
211  */
212
213 int
214 s390_get_frame_info (CORE_ADDR pc, struct frame_extra_info *fextra_info,
215                      struct frame_info *fi, int init_extra_info)
216 {
217 #define CONST_POOL_REGIDX 13
218 #define GOT_REGIDX        12
219   bfd_byte instr[S390_MAX_INSTR_SIZE];
220   CORE_ADDR test_pc = pc, test_pc2;
221   CORE_ADDR orig_sp = 0, save_reg_addr = 0, *saved_regs = NULL;
222   int valid_prologue, good_prologue = 0;
223   int gprs_saved[S390_NUM_GPRS];
224   int fprs_saved[S390_NUM_FPRS];
225   int regidx, instrlen;
226   int const_pool_state;
227   int varargs_state;
228   int loop_cnt, gdb_gpr_store, gdb_fpr_store;
229   int offset, expected_offset;
230   int err = 0;
231   disassemble_info info;
232
233   /* Have we seen an instruction initializing the frame pointer yet?
234      If we've seen an `lr %r11, %r15', then frame_pointer_found is
235      non-zero, and frame_pointer_regidx == 11.  Otherwise,
236      frame_pointer_found is zero and frame_pointer_regidx is 15,
237      indicating that we're using the stack pointer as our frame
238      pointer.  */
239   int frame_pointer_found = 0;
240   int frame_pointer_regidx = 0xf;
241
242   /* What we've seen so far regarding saving the back chain link:
243      0 -- nothing yet; sp still has the same value it had at the entry
244           point.  Since not all functions allocate frames, this is a
245           valid state for the prologue to finish in.
246      1 -- We've saved the original sp in some register other than the
247           frame pointer (hard-coded to be %r11, yuck).
248           save_link_regidx is the register we saved it in.
249      2 -- We've seen the initial `bras' instruction of the sequence for
250           reserving more than 32k of stack:
251                 bras %rX, .+8
252                 .long N
253                 s %r15, 0(%rX)
254           where %rX is not the constant pool register.
255           subtract_sp_regidx is %rX, and fextra_info->stack_bought is N.
256      3 -- We've reserved space for a new stack frame.  This means we
257           either saw a simple `ahi %r15,-N' in state 1, or the final
258           `s %r15, ...' in state 2.
259      4 -- The frame and link are now fully initialized.  We've
260           reserved space for the new stack frame, and stored the old
261           stack pointer captured in the back chain pointer field.  */
262   int save_link_state = 0;
263   int save_link_regidx, subtract_sp_regidx;
264
265   /* What we've seen so far regarding r12 --- the GOT (Global Offset
266      Table) pointer.  We expect to see `l %r12, N(%r13)', which loads
267      r12 with the offset from the constant pool to the GOT, and then
268      an `ar %r12, %r13', which adds the constant pool address,
269      yielding the GOT's address.  Here's what got_state means:
270      0 -- seen nothing
271      1 -- seen `l %r12, N(%r13)', but no `ar'
272      2 -- seen load and add, so GOT pointer is totally initialized
273      When got_state is 1, then got_load_addr is the address of the
274      load instruction, and got_load_len is the length of that
275      instruction.  */
276   int got_state= 0;
277   CORE_ADDR got_load_addr = 0, got_load_len = 0;
278
279   const_pool_state = varargs_state = 0;
280
281   memset (gprs_saved, 0, sizeof (gprs_saved));
282   memset (fprs_saved, 0, sizeof (fprs_saved));
283   info.read_memory_func = dis_asm_read_memory;
284
285   save_link_regidx = subtract_sp_regidx = 0;
286   if (fextra_info)
287     {
288       if (fi && fi->frame)
289         {
290           orig_sp = fi->frame;
291           if (! init_extra_info && fextra_info->initialised)
292             orig_sp += fextra_info->stack_bought;
293           saved_regs = fi->saved_regs;
294         }
295       if (init_extra_info || !fextra_info->initialised)
296         {
297           s390_memset_extra_info (fextra_info);
298           fextra_info->function_start = pc;
299           fextra_info->initialised = 1;
300         }
301     }
302   instrlen = 0;
303   do
304     {
305       valid_prologue = 0;
306       test_pc += instrlen;
307       /* add the previous instruction len */
308       instrlen = s390_readinstruction (instr, test_pc, &info);
309       if (instrlen < 0)
310         {
311           good_prologue = 0;
312           err = -1;
313           break;
314         }
315       /* We probably are in a glibc syscall */
316       if (instr[0] == S390_SYSCALL_OPCODE && test_pc == pc)
317         {
318           good_prologue = 1;
319           if (saved_regs && fextra_info && fi->next && fi->next->extra_info
320               && fi->next->extra_info->sigcontext)
321             {
322               /* We are backtracing from a signal handler */
323               save_reg_addr = fi->next->extra_info->sigcontext +
324                 REGISTER_BYTE (S390_GP0_REGNUM);
325               for (regidx = 0; regidx < S390_NUM_GPRS; regidx++)
326                 {
327                   saved_regs[S390_GP0_REGNUM + regidx] = save_reg_addr;
328                   save_reg_addr += S390_GPR_SIZE;
329                 }
330               save_reg_addr = fi->next->extra_info->sigcontext +
331                 (GDB_TARGET_IS_ESAME ? S390X_SIGREGS_FP0_OFFSET :
332                  S390_SIGREGS_FP0_OFFSET);
333               for (regidx = 0; regidx < S390_NUM_FPRS; regidx++)
334                 {
335                   saved_regs[S390_FP0_REGNUM + regidx] = save_reg_addr;
336                   save_reg_addr += S390_FPR_SIZE;
337                 }
338             }
339           break;
340         }
341       if (save_link_state == 0)
342         {
343           /* check for a stack relative STMG or STM */
344           if (((GDB_TARGET_IS_ESAME &&
345                 ((instr[0] == 0xeb) && (instr[5] == 0x24))) ||
346                (instr[0] == 0x90)) && ((instr[2] >> 4) == 0xf))
347             {
348               regidx = (instr[1] >> 4);
349               if (regidx < 6)
350                 varargs_state = 1;
351               offset = ((instr[2] & 0xf) << 8) + instr[3];
352               expected_offset =
353                 S390_GPR6_STACK_OFFSET + (S390_GPR_SIZE * (regidx - 6));
354               if (offset != expected_offset)
355                 {
356                   good_prologue = 0;
357                   break;
358                 }
359               if (saved_regs)
360                 save_reg_addr = orig_sp + offset;
361               for (; regidx <= (instr[1] & 0xf); regidx++)
362                 {
363                   if (gprs_saved[regidx])
364                     {
365                       good_prologue = 0;
366                       break;
367                     }
368                   good_prologue = 1;
369                   gprs_saved[regidx] = 1;
370                   if (saved_regs)
371                     {
372                       saved_regs[S390_GP0_REGNUM + regidx] = save_reg_addr;
373                       save_reg_addr += S390_GPR_SIZE;
374                     }
375                 }
376               valid_prologue = 1;
377               continue;
378             }
379         }
380       /* check for a stack relative STG or ST */
381       if ((save_link_state == 0 || save_link_state == 3) &&
382           ((GDB_TARGET_IS_ESAME &&
383             ((instr[0] == 0xe3) && (instr[5] == 0x24))) ||
384            (instr[0] == 0x50)) && ((instr[2] >> 4) == 0xf))
385         {
386           regidx = instr[1] >> 4;
387           offset = ((instr[2] & 0xf) << 8) + instr[3];
388           if (offset == 0)
389             {
390               if (save_link_state == 3 && regidx == save_link_regidx)
391                 {
392                   save_link_state = 4;
393                   valid_prologue = 1;
394                   continue;
395                 }
396               else
397                 break;
398             }
399           if (regidx < 6)
400             varargs_state = 1;
401           expected_offset =
402             S390_GPR6_STACK_OFFSET + (S390_GPR_SIZE * (regidx - 6));
403           if (offset != expected_offset)
404             {
405               good_prologue = 0;
406               break;
407             }
408           if (gprs_saved[regidx])
409             {
410               good_prologue = 0;
411               break;
412             }
413           good_prologue = 1;
414           gprs_saved[regidx] = 1;
415           if (saved_regs)
416             {
417               save_reg_addr = orig_sp + offset;
418               saved_regs[S390_GP0_REGNUM + regidx] = save_reg_addr;
419             }
420           valid_prologue = 1;
421           continue;
422         }
423
424       /* Check for an fp-relative STG, ST, or STM.  This is probably
425           spilling an argument from a register out into a stack slot.
426           This could be a user instruction, but if we haven't included
427           any other suspicious instructions in the prologue, this
428           could only be an initializing store, which isn't too bad to
429           skip.  The consequences of not including arg-to-stack spills
430           are more serious, though --- you don't see the proper values
431           of the arguments.  */
432       if ((save_link_state == 3 || save_link_state == 4)
433           && ((instr[0] == 0x50      /* st %rA, D(%rX,%rB) */
434                && (instr[1] & 0xf) == 0 /* %rX is zero, no index reg */
435                && is_arg_reg ((instr[1] >> 4) & 0xf)
436                && ((instr[2] >> 4) & 0xf) == frame_pointer_regidx)
437               || (instr[0] == 0x90 /* stm %rA, %rB, D(%rC) */
438                   && is_arg_reg ((instr[1] >> 4) & 0xf)
439                   && is_arg_reg (instr[1] & 0xf)
440                   && ((instr[2] >> 4) & 0xf) == frame_pointer_regidx)))
441         {
442           valid_prologue = 1;
443           continue;
444         }
445
446       /* check for STD */
447       if (instr[0] == 0x60 && (instr[2] >> 4) == 0xf)
448         {
449           regidx = instr[1] >> 4;
450           if (regidx == 0 || regidx == 2)
451             varargs_state = 1;
452           if (fprs_saved[regidx])
453             {
454               good_prologue = 0;
455               break;
456             }
457           fprs_saved[regidx] = 1;
458           if (saved_regs)
459             {
460               save_reg_addr = orig_sp + (((instr[2] & 0xf) << 8) + instr[3]);
461               saved_regs[S390_FP0_REGNUM + regidx] = save_reg_addr;
462             }
463           valid_prologue = 1;
464           continue;
465         }
466
467
468       if (const_pool_state == 0)
469         {
470
471           if (GDB_TARGET_IS_ESAME)
472             {
473               /* Check for larl CONST_POOL_REGIDX,offset on ESAME */
474               if ((instr[0] == 0xc0)
475                   && (instr[1] == (CONST_POOL_REGIDX << 4)))
476                 {
477                   const_pool_state = 2;
478                   valid_prologue = 1;
479                   continue;
480                 }
481             }
482           else
483             {
484               /* Check for BASR gpr13,gpr0 used to load constant pool pointer to r13 in old compiler */
485               if (instr[0] == 0xd && (instr[1] & 0xf) == 0
486                   && ((instr[1] >> 4) == CONST_POOL_REGIDX))
487                 {
488                   const_pool_state = 1;
489                   valid_prologue = 1;
490                   continue;
491                 }
492             }
493           /* Check for new fangled bras %r13,newpc to load new constant pool */
494           /* embedded in code, older pre abi compilers also emitted this stuff.  */
495           if ((instr[0] == 0xa7) && ((instr[1] & 0xf) == 0x5) &&
496               ((instr[1] >> 4) == CONST_POOL_REGIDX)
497               && ((instr[2] & 0x80) == 0))
498             {
499               const_pool_state = 2;
500               test_pc +=
501                 (((((instr[2] & 0xf) << 8) + instr[3]) << 1) - instrlen);
502               valid_prologue = 1;
503               continue;
504             }
505         }
506       /* Check for AGHI or AHI CONST_POOL_REGIDX,val */
507       if (const_pool_state == 1 && (instr[0] == 0xa7) &&
508           ((GDB_TARGET_IS_ESAME &&
509             (instr[1] == ((CONST_POOL_REGIDX << 4) | 0xb))) ||
510            (instr[1] == ((CONST_POOL_REGIDX << 4) | 0xa))))
511         {
512           const_pool_state = 2;
513           valid_prologue = 1;
514           continue;
515         }
516       /* Check for LGR or LR gprx,15 */
517       if ((GDB_TARGET_IS_ESAME &&
518            instr[0] == 0xb9 && instr[1] == 0x04 && (instr[3] & 0xf) == 0xf) ||
519           (instr[0] == 0x18 && (instr[1] & 0xf) == 0xf))
520         {
521           if (GDB_TARGET_IS_ESAME)
522             regidx = instr[3] >> 4;
523           else
524             regidx = instr[1] >> 4;
525           if (save_link_state == 0 && regidx != 0xb)
526             {
527               /* Almost defintely code for
528                  decrementing the stack pointer 
529                  ( i.e. a non leaf function 
530                  or else leaf with locals ) */
531               save_link_regidx = regidx;
532               save_link_state = 1;
533               valid_prologue = 1;
534               continue;
535             }
536           /* We use this frame pointer for alloca
537              unfortunately we need to assume its gpr11
538              otherwise we would need a smarter prologue
539              walker. */
540           if (!frame_pointer_found && regidx == 0xb)
541             {
542               frame_pointer_regidx = 0xb;
543               frame_pointer_found = 1;
544               if (fextra_info)
545                 fextra_info->frame_pointer_saved_pc = test_pc;
546               valid_prologue = 1;
547               continue;
548             }
549         }
550       /* Check for AHI or AGHI gpr15,val */
551       if (save_link_state == 1 && (instr[0] == 0xa7) &&
552           ((GDB_TARGET_IS_ESAME && (instr[1] == 0xfb)) || (instr[1] == 0xfa)))
553         {
554           if (fextra_info)
555             fextra_info->stack_bought =
556               -extract_signed_integer (&instr[2], 2);
557           save_link_state = 3;
558           valid_prologue = 1;
559           continue;
560         }
561       /* Alternatively check for the complex construction for
562          buying more than 32k of stack
563          BRAS gprx,.+8
564          long val
565          s    %r15,0(%gprx)  gprx currently r1 */
566       if ((save_link_state == 1) && (instr[0] == 0xa7)
567           && ((instr[1] & 0xf) == 0x5) && (instr[2] == 0)
568           && (instr[3] == 0x4) && ((instr[1] >> 4) != CONST_POOL_REGIDX))
569         {
570           subtract_sp_regidx = instr[1] >> 4;
571           save_link_state = 2;
572           if (fextra_info)
573             target_read_memory (test_pc + instrlen,
574                                 (char *) &fextra_info->stack_bought,
575                                 sizeof (fextra_info->stack_bought));
576           test_pc += 4;
577           valid_prologue = 1;
578           continue;
579         }
580       if (save_link_state == 2 && instr[0] == 0x5b
581           && instr[1] == 0xf0 &&
582           instr[2] == (subtract_sp_regidx << 4) && instr[3] == 0)
583         {
584           save_link_state = 3;
585           valid_prologue = 1;
586           continue;
587         }
588       /* check for LA gprx,offset(15) used for varargs */
589       if ((instr[0] == 0x41) && ((instr[2] >> 4) == 0xf) &&
590           ((instr[1] & 0xf) == 0))
591         {
592           /* some code uses gpr7 to point to outgoing args */
593           if (((instr[1] >> 4) == 7) && (save_link_state == 0) &&
594               ((instr[2] & 0xf) == 0)
595               && (instr[3] == S390_STACK_FRAME_OVERHEAD))
596             {
597               valid_prologue = 1;
598               continue;
599             }
600           if (varargs_state == 1)
601             {
602               varargs_state = 2;
603               valid_prologue = 1;
604               continue;
605             }
606         }
607       /* Check for a GOT load */
608
609       if (GDB_TARGET_IS_ESAME)
610         {
611           /* Check for larl  GOT_REGIDX, on ESAME */
612           if ((got_state == 0) && (instr[0] == 0xc0)
613               && (instr[1] == (GOT_REGIDX << 4)))
614             {
615               got_state = 2;
616               valid_prologue = 1;
617               continue;
618             }
619         }
620       else
621         {
622           /* check for l GOT_REGIDX,x(CONST_POOL_REGIDX) */
623           if (got_state == 0 && const_pool_state == 2 && instr[0] == 0x58
624               && (instr[2] == (CONST_POOL_REGIDX << 4))
625               && ((instr[1] >> 4) == GOT_REGIDX))
626             {
627               got_state = 1;
628               got_load_addr = test_pc;
629               got_load_len = instrlen;
630               valid_prologue = 1;
631               continue;
632             }
633           /* Check for subsequent ar got_regidx,basr_regidx */
634           if (got_state == 1 && instr[0] == 0x1a &&
635               instr[1] == ((GOT_REGIDX << 4) | CONST_POOL_REGIDX))
636             {
637               got_state = 2;
638               valid_prologue = 1;
639               continue;
640             }
641         }
642     }
643   while (valid_prologue && good_prologue);
644   if (good_prologue)
645     {
646       /* If this function doesn't reference the global offset table,
647          then the compiler may use r12 for other things.  If the last
648          instruction we saw was a load of r12 from the constant pool,
649          with no subsequent add to make the address PC-relative, then
650          the load was probably a genuine body instruction; don't treat
651          it as part of the prologue.  */
652       if (got_state == 1
653           && got_load_addr + got_load_len == test_pc)
654         {
655           test_pc = got_load_addr;
656           instrlen = got_load_len;
657         }
658         
659       good_prologue = (((const_pool_state == 0) || (const_pool_state == 2)) &&
660                        ((save_link_state == 0) || (save_link_state == 4)) &&
661                        ((varargs_state == 0) || (varargs_state == 2)));
662     }
663   if (fextra_info)
664     {
665       fextra_info->good_prologue = good_prologue;
666       fextra_info->skip_prologue_function_start =
667         (good_prologue ? test_pc : pc);
668     }
669   if (saved_regs)
670     /* The SP's element of the saved_regs array holds the old SP,
671        not the address at which it is saved.  */
672     saved_regs[S390_SP_REGNUM] = orig_sp;
673   return err;
674 }
675
676
677 int
678 s390_check_function_end (CORE_ADDR pc)
679 {
680   bfd_byte instr[S390_MAX_INSTR_SIZE];
681   disassemble_info info;
682   int regidx, instrlen;
683
684   info.read_memory_func = dis_asm_read_memory;
685   instrlen = s390_readinstruction (instr, pc, &info);
686   if (instrlen < 0)
687     return -1;
688   /* check for BR */
689   if (instrlen != 2 || instr[0] != 07 || (instr[1] >> 4) != 0xf)
690     return 0;
691   regidx = instr[1] & 0xf;
692   /* Check for LMG or LG */
693   instrlen =
694     s390_readinstruction (instr, pc - (GDB_TARGET_IS_ESAME ? 6 : 4), &info);
695   if (instrlen < 0)
696     return -1;
697   if (GDB_TARGET_IS_ESAME)
698     {
699
700       if (instrlen != 6 || instr[0] != 0xeb || instr[5] != 0x4)
701         return 0;
702     }
703   else if (instrlen != 4 || instr[0] != 0x98)
704     {
705       return 0;
706     }
707   if ((instr[2] >> 4) != 0xf)
708     return 0;
709   if (regidx == 14)
710     return 1;
711   instrlen = s390_readinstruction (instr, pc - (GDB_TARGET_IS_ESAME ? 12 : 8),
712                                    &info);
713   if (instrlen < 0)
714     return -1;
715   if (GDB_TARGET_IS_ESAME)
716     {
717       /* Check for LG */
718       if (instrlen != 6 || instr[0] != 0xe3 || instr[5] != 0x4)
719         return 0;
720     }
721   else
722     {
723       /* Check for L */
724       if (instrlen != 4 || instr[0] != 0x58)
725         return 0;
726     }
727   if (instr[2] >> 4 != 0xf)
728     return 0;
729   if (instr[1] >> 4 != regidx)
730     return 0;
731   return 1;
732 }
733
734 static CORE_ADDR
735 s390_sniff_pc_function_start (CORE_ADDR pc, struct frame_info *fi)
736 {
737   CORE_ADDR function_start, test_function_start;
738   int loop_cnt, err, function_end;
739   struct frame_extra_info fextra_info;
740   function_start = get_pc_function_start (pc);
741
742   if (function_start == 0)
743     {
744       test_function_start = pc;
745       if (test_function_start & 1)
746         return 0;               /* This has to be bogus */
747       loop_cnt = 0;
748       do
749         {
750
751           err =
752             s390_get_frame_info (test_function_start, &fextra_info, fi, 1);
753           loop_cnt++;
754           test_function_start -= 2;
755           function_end = s390_check_function_end (test_function_start);
756         }
757       while (!(function_end == 1 || err || loop_cnt >= 4096 ||
758                (fextra_info.good_prologue)));
759       if (fextra_info.good_prologue)
760         function_start = fextra_info.function_start;
761       else if (function_end == 1)
762         function_start = test_function_start;
763     }
764   return function_start;
765 }
766
767
768
769 CORE_ADDR
770 s390_function_start (struct frame_info *fi)
771 {
772   CORE_ADDR function_start = 0;
773
774   if (fi->extra_info && fi->extra_info->initialised)
775     function_start = fi->extra_info->function_start;
776   else if (fi->pc)
777     function_start = get_pc_function_start (fi->pc);
778   return function_start;
779 }
780
781
782
783
784 int
785 s390_frameless_function_invocation (struct frame_info *fi)
786 {
787   struct frame_extra_info fextra_info, *fextra_info_ptr;
788   int frameless = 0;
789
790   if (fi->next == NULL)         /* no may be frameless */
791     {
792       if (fi->extra_info)
793         fextra_info_ptr = fi->extra_info;
794       else
795         {
796           fextra_info_ptr = &fextra_info;
797           s390_get_frame_info (s390_sniff_pc_function_start (fi->pc, fi),
798                                fextra_info_ptr, fi, 1);
799         }
800       frameless = ((fextra_info_ptr->stack_bought == 0));
801     }
802   return frameless;
803
804 }
805
806
807 static int
808 s390_is_sigreturn (CORE_ADDR pc, struct frame_info *sighandler_fi,
809                    CORE_ADDR *sregs, CORE_ADDR *sigcaller_pc)
810 {
811   bfd_byte instr[S390_MAX_INSTR_SIZE];
812   disassemble_info info;
813   int instrlen;
814   CORE_ADDR scontext;
815   int retval = 0;
816   CORE_ADDR orig_sp;
817   CORE_ADDR temp_sregs;
818
819   scontext = temp_sregs = 0;
820
821   info.read_memory_func = dis_asm_read_memory;
822   instrlen = s390_readinstruction (instr, pc, &info);
823   if (sigcaller_pc)
824     *sigcaller_pc = 0;
825   if (((instrlen == S390_SYSCALL_SIZE) &&
826        (instr[0] == S390_SYSCALL_OPCODE)) &&
827       ((instr[1] == s390_NR_sigreturn) || (instr[1] == s390_NR_rt_sigreturn)))
828     {
829       if (sighandler_fi)
830         {
831           if (s390_frameless_function_invocation (sighandler_fi))
832             orig_sp = sighandler_fi->frame;
833           else
834             orig_sp = ADDR_BITS_REMOVE ((CORE_ADDR)
835                                         read_memory_integer (sighandler_fi->
836                                                              frame,
837                                                              S390_GPR_SIZE));
838           if (orig_sp && sigcaller_pc)
839             {
840               scontext = orig_sp + S390_SIGNAL_FRAMESIZE;
841               if (pc == scontext && instr[1] == s390_NR_rt_sigreturn)
842                 {
843                   /* We got a new style rt_signal */
844                   /* get address of read ucontext->uc_mcontext */
845                   temp_sregs = orig_sp + (GDB_TARGET_IS_ESAME ?
846                                           S390X_UC_MCONTEXT_OFFSET :
847                                           S390_UC_MCONTEXT_OFFSET);
848                 }
849               else
850                 {
851                   /* read sigcontext->sregs */
852                   temp_sregs = ADDR_BITS_REMOVE ((CORE_ADDR)
853                                                  read_memory_integer (scontext
854                                                                       +
855                                                                       (GDB_TARGET_IS_ESAME
856                                                                        ?
857                                                                        S390X_SIGCONTEXT_SREGS_OFFSET
858                                                                        :
859                                                                        S390_SIGCONTEXT_SREGS_OFFSET),
860                                                                       S390_GPR_SIZE));
861
862                 }
863               /* read sigregs->psw.addr */
864               *sigcaller_pc =
865                 ADDR_BITS_REMOVE ((CORE_ADDR)
866                                   read_memory_integer (temp_sregs +
867                                                        REGISTER_BYTE
868                                                        (S390_PC_REGNUM),
869                                                        S390_PSW_ADDR_SIZE));
870             }
871         }
872       retval = 1;
873     }
874   if (sregs)
875     *sregs = temp_sregs;
876   return retval;
877 }
878
879 /*
880   We need to do something better here but this will keep us out of trouble
881   for the moment.
882   For some reason the blockframe.c calls us with fi->next->fromleaf
883   so this seems of little use to us. */
884 void
885 s390_init_frame_pc_first (int next_fromleaf, struct frame_info *fi)
886 {
887   CORE_ADDR sigcaller_pc;
888
889   fi->pc = 0;
890   if (next_fromleaf)
891     {
892       fi->pc = ADDR_BITS_REMOVE (read_register (S390_RETADDR_REGNUM));
893       /* fix signal handlers */
894     }
895   else if (fi->next && fi->next->pc)
896     fi->pc = s390_frame_saved_pc_nofix (fi->next);
897   if (fi->pc && fi->next && fi->next->frame &&
898       s390_is_sigreturn (fi->pc, fi->next, NULL, &sigcaller_pc))
899     {
900       fi->pc = sigcaller_pc;
901     }
902
903 }
904
905 void
906 s390_init_extra_frame_info (int fromleaf, struct frame_info *fi)
907 {
908   fi->extra_info = frame_obstack_alloc (sizeof (struct frame_extra_info));
909   if (fi->pc)
910     s390_get_frame_info (s390_sniff_pc_function_start (fi->pc, fi),
911                          fi->extra_info, fi, 1);
912   else
913     s390_memset_extra_info (fi->extra_info);
914 }
915
916 /* If saved registers of frame FI are not known yet, read and cache them.
917    &FEXTRA_INFOP contains struct frame_extra_info; TDATAP can be NULL,
918    in which case the framedata are read.  */
919
920 void
921 s390_frame_init_saved_regs (struct frame_info *fi)
922 {
923
924   int quick;
925
926   if (fi->saved_regs == NULL)
927     {
928       /* zalloc memsets the saved regs */
929       frame_saved_regs_zalloc (fi);
930       if (fi->pc)
931         {
932           quick = (fi->extra_info && fi->extra_info->initialised
933                    && fi->extra_info->good_prologue);
934           s390_get_frame_info (quick ? fi->extra_info->function_start :
935                                s390_sniff_pc_function_start (fi->pc, fi),
936                                fi->extra_info, fi, !quick);
937         }
938     }
939 }
940
941
942
943 CORE_ADDR
944 s390_frame_args_address (struct frame_info *fi)
945 {
946
947   /* Apparently gdb already knows gdb_args_offset itself */
948   return fi->frame;
949 }
950
951
952 static CORE_ADDR
953 s390_frame_saved_pc_nofix (struct frame_info *fi)
954 {
955   if (fi->extra_info && fi->extra_info->saved_pc_valid)
956     return fi->extra_info->saved_pc;
957
958   if (generic_find_dummy_frame (fi->pc, fi->frame))
959     return generic_read_register_dummy (fi->pc, fi->frame, S390_PC_REGNUM);
960
961   s390_frame_init_saved_regs (fi);
962   if (fi->extra_info)
963     {
964       fi->extra_info->saved_pc_valid = 1;
965       if (fi->extra_info->good_prologue
966           && fi->saved_regs[S390_RETADDR_REGNUM])
967         fi->extra_info->saved_pc
968           = ADDR_BITS_REMOVE (read_memory_integer
969                               (fi->saved_regs[S390_RETADDR_REGNUM],
970                                S390_GPR_SIZE));
971       else
972         fi->extra_info->saved_pc
973           = ADDR_BITS_REMOVE (read_register (S390_RETADDR_REGNUM));
974       return fi->extra_info->saved_pc;
975     }
976   return 0;
977 }
978
979 CORE_ADDR
980 s390_frame_saved_pc (struct frame_info *fi)
981 {
982   CORE_ADDR saved_pc = 0, sig_pc;
983
984   if (fi->extra_info && fi->extra_info->sig_fixed_saved_pc_valid)
985     return fi->extra_info->sig_fixed_saved_pc;
986   saved_pc = s390_frame_saved_pc_nofix (fi);
987
988   if (fi->extra_info)
989     {
990       fi->extra_info->sig_fixed_saved_pc_valid = 1;
991       if (saved_pc)
992         {
993           if (s390_is_sigreturn (saved_pc, fi, NULL, &sig_pc))
994             saved_pc = sig_pc;
995         }
996       fi->extra_info->sig_fixed_saved_pc = saved_pc;
997     }
998   return saved_pc;
999 }
1000
1001
1002
1003
1004 /* We want backtraces out of signal handlers so we don't
1005    set thisframe->signal_handler_caller to 1 */
1006
1007 CORE_ADDR
1008 s390_frame_chain (struct frame_info *thisframe)
1009 {
1010   CORE_ADDR prev_fp = 0;
1011
1012   if (thisframe->prev && thisframe->prev->frame)
1013     prev_fp = thisframe->prev->frame;
1014   else if (generic_find_dummy_frame (thisframe->pc, thisframe->frame))
1015     return generic_read_register_dummy (thisframe->pc, thisframe->frame,
1016                                         S390_SP_REGNUM);
1017   else
1018     {
1019       int sigreturn = 0;
1020       CORE_ADDR sregs = 0;
1021       struct frame_extra_info prev_fextra_info;
1022
1023       memset (&prev_fextra_info, 0, sizeof (prev_fextra_info));
1024       if (thisframe->pc)
1025         {
1026           CORE_ADDR saved_pc, sig_pc;
1027
1028           saved_pc = s390_frame_saved_pc_nofix (thisframe);
1029           if (saved_pc)
1030             {
1031               if ((sigreturn =
1032                    s390_is_sigreturn (saved_pc, thisframe, &sregs, &sig_pc)))
1033                 saved_pc = sig_pc;
1034               s390_get_frame_info (s390_sniff_pc_function_start
1035                                    (saved_pc, NULL), &prev_fextra_info, NULL,
1036                                    1);
1037             }
1038         }
1039       if (sigreturn)
1040         {
1041           /* read sigregs,regs.gprs[11 or 15] */
1042           prev_fp = read_memory_integer (sregs +
1043                                          REGISTER_BYTE (S390_GP0_REGNUM +
1044                                                         (prev_fextra_info.
1045                                                          frame_pointer_saved_pc
1046                                                          ? 11 : 15)),
1047                                          S390_GPR_SIZE);
1048           thisframe->extra_info->sigcontext = sregs;
1049         }
1050       else
1051         {
1052           if (thisframe->saved_regs)
1053             {
1054               int regno;
1055
1056               if (prev_fextra_info.frame_pointer_saved_pc
1057                   && thisframe->saved_regs[S390_FRAME_REGNUM])
1058                 regno = S390_FRAME_REGNUM;
1059               else
1060                 regno = S390_SP_REGNUM;
1061
1062               if (thisframe->saved_regs[regno])
1063                 {
1064                   /* The SP's entry of `saved_regs' is special.  */
1065                   if (regno == S390_SP_REGNUM)
1066                     prev_fp = thisframe->saved_regs[regno];
1067                   else
1068                     prev_fp =
1069                       read_memory_integer (thisframe->saved_regs[regno],
1070                                            S390_GPR_SIZE);
1071                 }
1072             }
1073         }
1074     }
1075   return ADDR_BITS_REMOVE (prev_fp);
1076 }
1077
1078 /*
1079   Whether struct frame_extra_info is actually needed I'll have to figure
1080   out as our frames are similar to rs6000 there is a possibility
1081   i386 dosen't need it. */
1082
1083
1084
1085 /* a given return value in `regbuf' with a type `valtype', extract and copy its
1086    value into `valbuf' */
1087 void
1088 s390_extract_return_value (struct type *valtype, char *regbuf, char *valbuf)
1089 {
1090   /* floats and doubles are returned in fpr0. fpr's have a size of 8 bytes.
1091      We need to truncate the return value into float size (4 byte) if
1092      necessary. */
1093   int len = TYPE_LENGTH (valtype);
1094
1095   if (TYPE_CODE (valtype) == TYPE_CODE_FLT)
1096     memcpy (valbuf, &regbuf[REGISTER_BYTE (S390_FP0_REGNUM)], len);
1097   else
1098     {
1099       int offset = 0;
1100       /* return value is copied starting from r2. */
1101       if (TYPE_LENGTH (valtype) < S390_GPR_SIZE)
1102         offset = S390_GPR_SIZE - TYPE_LENGTH (valtype);
1103       memcpy (valbuf,
1104               regbuf + REGISTER_BYTE (S390_GP0_REGNUM + 2) + offset,
1105               TYPE_LENGTH (valtype));
1106     }
1107 }
1108
1109
1110 static char *
1111 s390_promote_integer_argument (struct type *valtype, char *valbuf,
1112                                char *reg_buff, int *arglen)
1113 {
1114   char *value = valbuf;
1115   int len = TYPE_LENGTH (valtype);
1116
1117   if (len < S390_GPR_SIZE)
1118     {
1119       /* We need to upgrade this value to a register to pass it correctly */
1120       int idx, diff = S390_GPR_SIZE - len, negative =
1121         (!TYPE_UNSIGNED (valtype) && value[0] & 0x80);
1122       for (idx = 0; idx < S390_GPR_SIZE; idx++)
1123         {
1124           reg_buff[idx] = (idx < diff ? (negative ? 0xff : 0x0) :
1125                            value[idx - diff]);
1126         }
1127       value = reg_buff;
1128       *arglen = S390_GPR_SIZE;
1129     }
1130   else
1131     {
1132       if (len & (S390_GPR_SIZE - 1))
1133         {
1134           fprintf_unfiltered (gdb_stderr,
1135                               "s390_promote_integer_argument detected an argument not "
1136                               "a multiple of S390_GPR_SIZE & greater than S390_GPR_SIZE "
1137                               "we might not deal with this correctly.\n");
1138         }
1139       *arglen = len;
1140     }
1141
1142   return (value);
1143 }
1144
1145 void
1146 s390_store_return_value (struct type *valtype, char *valbuf)
1147 {
1148   int arglen;
1149   char *reg_buff = alloca (max (S390_FPR_SIZE, REGISTER_SIZE)), *value;
1150
1151   if (TYPE_CODE (valtype) == TYPE_CODE_FLT)
1152     {
1153       if (TYPE_LENGTH (valtype) == 4
1154           || TYPE_LENGTH (valtype) == 8)
1155         write_register_bytes (REGISTER_BYTE (S390_FP0_REGNUM), valbuf,
1156                               TYPE_LENGTH (valtype));
1157       else
1158         error ("GDB is unable to return `long double' values "
1159                "on this architecture.");
1160     }
1161   else
1162     {
1163       value =
1164         s390_promote_integer_argument (valtype, valbuf, reg_buff, &arglen);
1165       /* Everything else is returned in GPR2 and up. */
1166       write_register_bytes (REGISTER_BYTE (S390_GP0_REGNUM + 2), value,
1167                             arglen);
1168     }
1169 }
1170 static int
1171 gdb_print_insn_s390 (bfd_vma memaddr, disassemble_info * info)
1172 {
1173   bfd_byte instrbuff[S390_MAX_INSTR_SIZE];
1174   int instrlen, cnt;
1175
1176   instrlen = s390_readinstruction (instrbuff, (CORE_ADDR) memaddr, info);
1177   if (instrlen < 0)
1178     {
1179       (*info->memory_error_func) (instrlen, memaddr, info);
1180       return -1;
1181     }
1182   for (cnt = 0; cnt < instrlen; cnt++)
1183     info->fprintf_func (info->stream, "%02X ", instrbuff[cnt]);
1184   for (cnt = instrlen; cnt < S390_MAX_INSTR_SIZE; cnt++)
1185     info->fprintf_func (info->stream, "   ");
1186   instrlen = print_insn_s390 (memaddr, info);
1187   return instrlen;
1188 }
1189
1190
1191
1192 /* Not the most efficent code in the world */
1193 int
1194 s390_fp_regnum ()
1195 {
1196   int regno = S390_SP_REGNUM;
1197   struct frame_extra_info fextra_info;
1198
1199   CORE_ADDR pc = ADDR_BITS_REMOVE (read_register (S390_PC_REGNUM));
1200
1201   s390_get_frame_info (s390_sniff_pc_function_start (pc, NULL), &fextra_info,
1202                        NULL, 1);
1203   if (fextra_info.frame_pointer_saved_pc)
1204     regno = S390_FRAME_REGNUM;
1205   return regno;
1206 }
1207
1208 CORE_ADDR
1209 s390_read_fp ()
1210 {
1211   return read_register (s390_fp_regnum ());
1212 }
1213
1214
1215 void
1216 s390_write_fp (CORE_ADDR val)
1217 {
1218   write_register (s390_fp_regnum (), val);
1219 }
1220
1221
1222 static void
1223 s390_pop_frame_regular (struct frame_info *frame)
1224 {
1225   int regnum;
1226
1227   write_register (S390_PC_REGNUM, FRAME_SAVED_PC (frame));
1228
1229   /* Restore any saved registers.  */
1230   if (frame->saved_regs)
1231     {
1232       for (regnum = 0; regnum < NUM_REGS; regnum++)
1233         if (frame->saved_regs[regnum] != 0)
1234           {
1235             ULONGEST value;
1236             
1237             value = read_memory_unsigned_integer (frame->saved_regs[regnum],
1238                                                   REGISTER_RAW_SIZE (regnum));
1239             write_register (regnum, value);
1240           }
1241
1242       /* Actually cut back the stack.  Remember that the SP's element of
1243          saved_regs is the old SP itself, not the address at which it is
1244          saved.  */
1245       write_register (S390_SP_REGNUM, frame->saved_regs[S390_SP_REGNUM]);
1246     }
1247
1248   /* Throw away any cached frame information.  */
1249   flush_cached_frames ();
1250 }
1251
1252
1253 /* Destroy the innermost (Top-Of-Stack) stack frame, restoring the 
1254    machine state that was in effect before the frame was created. 
1255    Used in the contexts of the "return" command, and of 
1256    target function calls from the debugger.  */
1257 void
1258 s390_pop_frame ()
1259 {
1260   /* This function checks for and handles generic dummy frames, and
1261      calls back to our function for ordinary frames.  */
1262   generic_pop_current_frame (s390_pop_frame_regular);
1263 }
1264
1265
1266 /* Return non-zero if TYPE is an integer-like type, zero otherwise.
1267    "Integer-like" types are those that should be passed the way
1268    integers are: integers, enums, ranges, characters, and booleans.  */
1269 static int
1270 is_integer_like (struct type *type)
1271 {
1272   enum type_code code = TYPE_CODE (type);
1273
1274   return (code == TYPE_CODE_INT
1275           || code == TYPE_CODE_ENUM
1276           || code == TYPE_CODE_RANGE
1277           || code == TYPE_CODE_CHAR
1278           || code == TYPE_CODE_BOOL);
1279 }
1280
1281
1282 /* Return non-zero if TYPE is a pointer-like type, zero otherwise.
1283    "Pointer-like" types are those that should be passed the way
1284    pointers are: pointers and references.  */
1285 static int
1286 is_pointer_like (struct type *type)
1287 {
1288   enum type_code code = TYPE_CODE (type);
1289
1290   return (code == TYPE_CODE_PTR
1291           || code == TYPE_CODE_REF);
1292 }
1293
1294
1295 /* Return non-zero if TYPE is a `float singleton' or `double
1296    singleton', zero otherwise.
1297
1298    A `T singleton' is a struct type with one member, whose type is
1299    either T or a `T singleton'.  So, the following are all float
1300    singletons:
1301
1302    struct { float x };
1303    struct { struct { float x; } x; };
1304    struct { struct { struct { float x; } x; } x; };
1305
1306    ... and so on.
1307
1308    WHY THE HECK DO WE CARE ABOUT THIS???  Well, it turns out that GCC
1309    passes all float singletons and double singletons as if they were
1310    simply floats or doubles.  This is *not* what the ABI says it
1311    should do.  */
1312 static int
1313 is_float_singleton (struct type *type)
1314 {
1315   return (TYPE_CODE (type) == TYPE_CODE_STRUCT
1316           && TYPE_NFIELDS (type) == 1
1317           && (TYPE_CODE (TYPE_FIELD_TYPE (type, 0)) == TYPE_CODE_FLT
1318               || is_float_singleton (TYPE_FIELD_TYPE (type, 0))));
1319 }
1320
1321
1322 /* Return non-zero if TYPE is a struct-like type, zero otherwise.
1323    "Struct-like" types are those that should be passed as structs are:
1324    structs and unions.
1325
1326    As an odd quirk, not mentioned in the ABI, GCC passes float and
1327    double singletons as if they were a plain float, double, etc.  (The
1328    corresponding union types are handled normally.)  So we exclude
1329    those types here.  *shrug* */
1330 static int
1331 is_struct_like (struct type *type)
1332 {
1333   enum type_code code = TYPE_CODE (type);
1334
1335   return (code == TYPE_CODE_UNION
1336           || (code == TYPE_CODE_STRUCT && ! is_float_singleton (type)));
1337 }
1338
1339
1340 /* Return non-zero if TYPE is a float-like type, zero otherwise.
1341    "Float-like" types are those that should be passed as
1342    floating-point values are.
1343
1344    You'd think this would just be floats, doubles, long doubles, etc.
1345    But as an odd quirk, not mentioned in the ABI, GCC passes float and
1346    double singletons as if they were a plain float, double, etc.  (The
1347    corresponding union types are handled normally.)  So we exclude
1348    those types here.  *shrug* */
1349 static int
1350 is_float_like (struct type *type)
1351 {
1352   return (TYPE_CODE (type) == TYPE_CODE_FLT
1353           || is_float_singleton (type));
1354 }
1355
1356
1357 /* Return non-zero if TYPE is considered a `DOUBLE_OR_FLOAT', as
1358    defined by the parameter passing conventions described in the
1359    "GNU/Linux for S/390 ELF Application Binary Interface Supplement".
1360    Otherwise, return zero.  */
1361 static int
1362 is_double_or_float (struct type *type)
1363 {
1364   return (is_float_like (type)
1365           && (TYPE_LENGTH (type) == 4
1366               || TYPE_LENGTH (type) == 8));
1367 }
1368
1369
1370 /* Return non-zero if TYPE is considered a `SIMPLE_ARG', as defined by
1371    the parameter passing conventions described in the "GNU/Linux for
1372    S/390 ELF Application Binary Interface Supplement".  Return zero
1373    otherwise.  */
1374 static int
1375 is_simple_arg (struct type *type)
1376 {
1377   unsigned length = TYPE_LENGTH (type);
1378
1379   /* This is almost a direct translation of the ABI's language, except
1380      that we have to exclude 8-byte structs; those are DOUBLE_ARGs.  */
1381   return ((is_integer_like (type) && length <= 4)
1382           || is_pointer_like (type)
1383           || (is_struct_like (type) && length != 8)
1384           || (is_float_like (type) && length == 16));
1385 }
1386
1387
1388 /* Return non-zero if TYPE should be passed as a pointer to a copy,
1389    zero otherwise.  TYPE must be a SIMPLE_ARG, as recognized by
1390    `is_simple_arg'.  */
1391 static int
1392 pass_by_copy_ref (struct type *type)
1393 {
1394   unsigned length = TYPE_LENGTH (type);
1395
1396   return ((is_struct_like (type) && length != 1 && length != 2 && length != 4)
1397           || (is_float_like (type) && length == 16));
1398 }
1399
1400
1401 /* Return ARG, a `SIMPLE_ARG', sign-extended or zero-extended to a full
1402    word as required for the ABI.  */
1403 static LONGEST
1404 extend_simple_arg (struct value *arg)
1405 {
1406   struct type *type = VALUE_TYPE (arg);
1407
1408   /* Even structs get passed in the least significant bits of the
1409      register / memory word.  It's not really right to extract them as
1410      an integer, but it does take care of the extension.  */
1411   if (TYPE_UNSIGNED (type))
1412     return extract_unsigned_integer (VALUE_CONTENTS (arg),
1413                                      TYPE_LENGTH (type));
1414   else
1415     return extract_signed_integer (VALUE_CONTENTS (arg),
1416                                    TYPE_LENGTH (type));
1417 }
1418
1419
1420 /* Return non-zero if TYPE is a `DOUBLE_ARG', as defined by the
1421    parameter passing conventions described in the "GNU/Linux for S/390
1422    ELF Application Binary Interface Supplement".  Return zero
1423    otherwise.  */
1424 static int
1425 is_double_arg (struct type *type)
1426 {
1427   unsigned length = TYPE_LENGTH (type);
1428
1429   return ((is_integer_like (type)
1430            || is_struct_like (type))
1431           && length == 8);
1432 }
1433
1434
1435 /* Round ADDR up to the next N-byte boundary.  N must be a power of
1436    two.  */
1437 static CORE_ADDR
1438 round_up (CORE_ADDR addr, int n)
1439 {
1440   /* Check that N is really a power of two.  */
1441   gdb_assert (n && (n & (n-1)) == 0);
1442   return ((addr + n - 1) & -n);
1443 }
1444
1445
1446 /* Round ADDR down to the next N-byte boundary.  N must be a power of
1447    two.  */
1448 static CORE_ADDR
1449 round_down (CORE_ADDR addr, int n)
1450 {
1451   /* Check that N is really a power of two.  */
1452   gdb_assert (n && (n & (n-1)) == 0);
1453   return (addr & -n);
1454 }
1455
1456
1457 /* Return the alignment required by TYPE.  */
1458 static int
1459 alignment_of (struct type *type)
1460 {
1461   int alignment;
1462
1463   if (is_integer_like (type)
1464       || is_pointer_like (type)
1465       || TYPE_CODE (type) == TYPE_CODE_FLT)
1466     alignment = TYPE_LENGTH (type);
1467   else if (TYPE_CODE (type) == TYPE_CODE_STRUCT
1468            || TYPE_CODE (type) == TYPE_CODE_UNION)
1469     {
1470       int i;
1471
1472       alignment = 1;
1473       for (i = 0; i < TYPE_NFIELDS (type); i++)
1474         {
1475           int field_alignment = alignment_of (TYPE_FIELD_TYPE (type, i));
1476
1477           if (field_alignment > alignment)
1478             alignment = field_alignment;
1479         }
1480     }
1481   else
1482     alignment = 1;
1483
1484   /* Check that everything we ever return is a power of two.  Lots of
1485      code doesn't want to deal with aligning things to arbitrary
1486      boundaries.  */
1487   gdb_assert ((alignment & (alignment - 1)) == 0);
1488
1489   return alignment;
1490 }
1491
1492
1493 /* Put the actual parameter values pointed to by ARGS[0..NARGS-1] in
1494    place to be passed to a function, as specified by the "GNU/Linux
1495    for S/390 ELF Application Binary Interface Supplement".
1496
1497    SP is the current stack pointer.  We must put arguments, links,
1498    padding, etc. whereever they belong, and return the new stack
1499    pointer value.
1500    
1501    If STRUCT_RETURN is non-zero, then the function we're calling is
1502    going to return a structure by value; STRUCT_ADDR is the address of
1503    a block we've allocated for it on the stack.
1504
1505    Our caller has taken care of any type promotions needed to satisfy
1506    prototypes or the old K&R argument-passing rules.  */
1507 CORE_ADDR
1508 s390_push_arguments (int nargs, struct value **args, CORE_ADDR sp,
1509                      int struct_return, CORE_ADDR struct_addr)
1510 {
1511   int i;
1512   int pointer_size = (TARGET_PTR_BIT / TARGET_CHAR_BIT);
1513
1514   /* The number of arguments passed by reference-to-copy.  */
1515   int num_copies;
1516
1517   /* If the i'th argument is passed as a reference to a copy, then
1518      copy_addr[i] is the address of the copy we made.  */
1519   CORE_ADDR *copy_addr = alloca (nargs * sizeof (CORE_ADDR));
1520
1521   /* Build the reference-to-copy area.  */
1522   num_copies = 0;
1523   for (i = 0; i < nargs; i++)
1524     {
1525       struct value *arg = args[i];
1526       struct type *type = VALUE_TYPE (arg);
1527       unsigned length = TYPE_LENGTH (type);
1528
1529       if (is_simple_arg (type)
1530           && pass_by_copy_ref (type))
1531         {
1532           sp -= length;
1533           sp = round_down (sp, alignment_of (type));
1534           write_memory (sp, VALUE_CONTENTS (arg), length);
1535           copy_addr[i] = sp;
1536           num_copies++;
1537         }
1538     }
1539
1540   /* Reserve space for the parameter area.  As a conservative
1541      simplification, we assume that everything will be passed on the
1542      stack.  */
1543   {
1544     int i;
1545
1546     for (i = 0; i < nargs; i++)
1547       {
1548         struct value *arg = args[i];
1549         struct type *type = VALUE_TYPE (arg);
1550         int length = TYPE_LENGTH (type);
1551         
1552         sp = round_down (sp, alignment_of (type));
1553
1554         /* SIMPLE_ARG values get extended to 32 bits.  Assume every
1555            argument is.  */
1556         if (length < 4) length = 4;
1557         sp -= length;
1558       }
1559   }
1560
1561   /* Include space for any reference-to-copy pointers.  */
1562   sp = round_down (sp, pointer_size);
1563   sp -= num_copies * pointer_size;
1564     
1565   /* After all that, make sure it's still aligned on an eight-byte
1566      boundary.  */
1567   sp = round_down (sp, 8);
1568
1569   /* Finally, place the actual parameters, working from SP towards
1570      higher addresses.  The code above is supposed to reserve enough
1571      space for this.  */
1572   {
1573     int fr = 0;
1574     int gr = 2;
1575     CORE_ADDR starg = sp;
1576
1577     for (i = 0; i < nargs; i++)
1578       {
1579         struct value *arg = args[i];
1580         struct type *type = VALUE_TYPE (arg);
1581         
1582         if (is_double_or_float (type)
1583             && fr <= 2)
1584           {
1585             /* When we store a single-precision value in an FP register,
1586                it occupies the leftmost bits.  */
1587             write_register_bytes (REGISTER_BYTE (S390_FP0_REGNUM + fr),
1588                                   VALUE_CONTENTS (arg),
1589                                   TYPE_LENGTH (type));
1590             fr += 2;
1591           }
1592         else if (is_simple_arg (type)
1593                  && gr <= 6)
1594           {
1595             /* Do we need to pass a pointer to our copy of this
1596                argument?  */
1597             if (pass_by_copy_ref (type))
1598               write_register (S390_GP0_REGNUM + gr, copy_addr[i]);
1599             else
1600               write_register (S390_GP0_REGNUM + gr, extend_simple_arg (arg));
1601
1602             gr++;
1603           }
1604         else if (is_double_arg (type)
1605                  && gr <= 5)
1606           {
1607             write_register_gen (S390_GP0_REGNUM + gr,
1608                                 VALUE_CONTENTS (arg));
1609             write_register_gen (S390_GP0_REGNUM + gr + 1,
1610                                 VALUE_CONTENTS (arg) + 4);
1611             gr += 2;
1612           }
1613         else
1614           {
1615             /* The `OTHER' case.  */
1616             enum type_code code = TYPE_CODE (type);
1617             unsigned length = TYPE_LENGTH (type);
1618             
1619             /* If we skipped r6 because we couldn't fit a DOUBLE_ARG
1620                in it, then don't go back and use it again later.  */
1621             if (is_double_arg (type) && gr == 6)
1622               gr = 7;
1623
1624             if (is_simple_arg (type))
1625               {
1626                 /* Simple args are always either extended to 32 bits,
1627                    or pointers.  */
1628                 starg = round_up (starg, 4);
1629
1630                 /* Do we need to pass a pointer to our copy of this
1631                    argument?  */
1632                 if (pass_by_copy_ref (type))
1633                   write_memory_signed_integer (starg, pointer_size,
1634                                                copy_addr[i]);
1635                 else
1636                   /* Simple args are always extended to 32 bits.  */
1637                   write_memory_signed_integer (starg, 4,
1638                                                extend_simple_arg (arg));
1639                 starg += 4;
1640               }
1641             else
1642               {
1643                 /* You'd think we should say:
1644                    starg = round_up (starg, alignment_of (type));
1645                    Unfortunately, GCC seems to simply align the stack on
1646                    a four-byte boundary, even when passing doubles.  */
1647                 starg = round_up (starg, 4);
1648                 write_memory (starg, VALUE_CONTENTS (arg), length);
1649                 starg += length;
1650               }
1651           }
1652       }
1653   }
1654
1655   /* Allocate the standard frame areas: the register save area, the
1656      word reserved for the compiler (which seems kind of meaningless),
1657      and the back chain pointer.  */
1658   sp -= 96;
1659
1660   /* Write the back chain pointer into the first word of the stack
1661      frame.  This will help us get backtraces from within functions
1662      called from GDB.  */
1663   write_memory_unsigned_integer (sp, (TARGET_PTR_BIT / TARGET_CHAR_BIT),
1664                                  read_fp ());
1665
1666   return sp;
1667 }
1668
1669
1670 static int
1671 s390_use_struct_convention (int gcc_p, struct type *value_type)
1672 {
1673   enum type_code code = TYPE_CODE (value_type);
1674
1675   return (code == TYPE_CODE_STRUCT
1676           || code == TYPE_CODE_UNION);
1677 }
1678
1679
1680 /* Return the GDB type object for the "standard" data type
1681    of data in register N.  */
1682 struct type *
1683 s390_register_virtual_type (int regno)
1684 {
1685   if (S390_FP0_REGNUM <= regno && regno < S390_FP0_REGNUM + S390_NUM_FPRS)
1686     return builtin_type_double;
1687   else
1688     return builtin_type_int;
1689 }
1690
1691
1692 struct type *
1693 s390x_register_virtual_type (int regno)
1694 {
1695   return (regno == S390_FPC_REGNUM) ||
1696     (regno >= S390_FIRST_ACR && regno <= S390_LAST_ACR) ? builtin_type_int :
1697     (regno >= S390_FP0_REGNUM) ? builtin_type_double : builtin_type_long;
1698 }
1699
1700
1701
1702 void
1703 s390_store_struct_return (CORE_ADDR addr, CORE_ADDR sp)
1704 {
1705   write_register (S390_GP0_REGNUM + 2, addr);
1706 }
1707
1708
1709
1710 static unsigned char *
1711 s390_breakpoint_from_pc (CORE_ADDR *pcptr, int *lenptr)
1712 {
1713   static unsigned char breakpoint[] = { 0x0, 0x1 };
1714
1715   *lenptr = sizeof (breakpoint);
1716   return breakpoint;
1717 }
1718
1719 /* Advance PC across any function entry prologue instructions to reach some
1720    "real" code.  */
1721 CORE_ADDR
1722 s390_skip_prologue (CORE_ADDR pc)
1723 {
1724   struct frame_extra_info fextra_info;
1725
1726   s390_get_frame_info (pc, &fextra_info, NULL, 1);
1727   return fextra_info.skip_prologue_function_start;
1728 }
1729
1730 /* Immediately after a function call, return the saved pc.
1731    Can't go through the frames for this because on some machines
1732    the new frame is not set up until the new function executes
1733    some instructions.  */
1734 CORE_ADDR
1735 s390_saved_pc_after_call (struct frame_info *frame)
1736 {
1737   return ADDR_BITS_REMOVE (read_register (S390_RETADDR_REGNUM));
1738 }
1739
1740 static CORE_ADDR
1741 s390_addr_bits_remove (CORE_ADDR addr)
1742 {
1743   return (addr) & 0x7fffffff;
1744 }
1745
1746
1747 static CORE_ADDR
1748 s390_push_return_address (CORE_ADDR pc, CORE_ADDR sp)
1749 {
1750   write_register (S390_RETADDR_REGNUM, CALL_DUMMY_ADDRESS ());
1751   return sp;
1752 }
1753
1754 struct gdbarch *
1755 s390_gdbarch_init (struct gdbarch_info info, struct gdbarch_list *arches)
1756 {
1757   static LONGEST s390_call_dummy_words[] = { 0 };
1758   struct gdbarch *gdbarch;
1759   struct gdbarch_tdep *tdep;
1760   int elf_flags;
1761
1762   /* First see if there is already a gdbarch that can satisfy the request.  */
1763   arches = gdbarch_list_lookup_by_info (arches, &info);
1764   if (arches != NULL)
1765     return arches->gdbarch;
1766
1767   /* None found: is the request for a s390 architecture? */
1768   if (info.bfd_arch_info->arch != bfd_arch_s390)
1769     return NULL;                /* No; then it's not for us.  */
1770
1771   /* Yes: create a new gdbarch for the specified machine type.  */
1772   gdbarch = gdbarch_alloc (&info, NULL);
1773
1774   set_gdbarch_believe_pcc_promotion (gdbarch, 0);
1775   set_gdbarch_char_signed (gdbarch, 0);
1776
1777   set_gdbarch_frame_args_skip (gdbarch, 0);
1778   set_gdbarch_frame_args_address (gdbarch, s390_frame_args_address);
1779   set_gdbarch_frame_chain (gdbarch, s390_frame_chain);
1780   set_gdbarch_frame_init_saved_regs (gdbarch, s390_frame_init_saved_regs);
1781   set_gdbarch_frame_locals_address (gdbarch, s390_frame_args_address);
1782   /* We can't do this */
1783   set_gdbarch_frame_num_args (gdbarch, frame_num_args_unknown);
1784   set_gdbarch_store_struct_return (gdbarch, s390_store_struct_return);
1785   set_gdbarch_extract_return_value (gdbarch, s390_extract_return_value);
1786   set_gdbarch_store_return_value (gdbarch, s390_store_return_value);
1787   /* Amount PC must be decremented by after a breakpoint.
1788      This is often the number of bytes in BREAKPOINT
1789      but not always.  */
1790   set_gdbarch_decr_pc_after_break (gdbarch, 2);
1791   set_gdbarch_pop_frame (gdbarch, s390_pop_frame);
1792   /* Stack grows downward.  */
1793   set_gdbarch_inner_than (gdbarch, core_addr_lessthan);
1794   /* Offset from address of function to start of its code.
1795      Zero on most machines.  */
1796   set_gdbarch_function_start_offset (gdbarch, 0);
1797   set_gdbarch_max_register_raw_size (gdbarch, 8);
1798   set_gdbarch_max_register_virtual_size (gdbarch, 8);
1799   set_gdbarch_breakpoint_from_pc (gdbarch, s390_breakpoint_from_pc);
1800   set_gdbarch_skip_prologue (gdbarch, s390_skip_prologue);
1801   set_gdbarch_init_extra_frame_info (gdbarch, s390_init_extra_frame_info);
1802   set_gdbarch_init_frame_pc_first (gdbarch, s390_init_frame_pc_first);
1803   set_gdbarch_read_fp (gdbarch, s390_read_fp);
1804   set_gdbarch_write_fp (gdbarch, s390_write_fp);
1805   /* This function that tells us whether the function invocation represented
1806      by FI does not have a frame on the stack associated with it.  If it
1807      does not, FRAMELESS is set to 1, else 0.  */
1808   set_gdbarch_frameless_function_invocation (gdbarch,
1809                                              s390_frameless_function_invocation);
1810   /* Return saved PC from a frame */
1811   set_gdbarch_frame_saved_pc (gdbarch, s390_frame_saved_pc);
1812   /* FRAME_CHAIN takes a frame's nominal address
1813      and produces the frame's chain-pointer. */
1814   set_gdbarch_frame_chain (gdbarch, s390_frame_chain);
1815   set_gdbarch_saved_pc_after_call (gdbarch, s390_saved_pc_after_call);
1816   set_gdbarch_register_byte (gdbarch, s390_register_byte);
1817   set_gdbarch_pc_regnum (gdbarch, S390_PC_REGNUM);
1818   set_gdbarch_sp_regnum (gdbarch, S390_SP_REGNUM);
1819   set_gdbarch_fp_regnum (gdbarch, S390_FP_REGNUM);
1820   set_gdbarch_fp0_regnum (gdbarch, S390_FP0_REGNUM);
1821   set_gdbarch_num_regs (gdbarch, S390_NUM_REGS);
1822   set_gdbarch_cannot_fetch_register (gdbarch, s390_cannot_fetch_register);
1823   set_gdbarch_cannot_store_register (gdbarch, s390_cannot_fetch_register);
1824   set_gdbarch_get_saved_register (gdbarch, generic_get_saved_register);
1825   set_gdbarch_use_struct_convention (gdbarch, s390_use_struct_convention);
1826   set_gdbarch_frame_chain_valid (gdbarch, func_frame_chain_valid);
1827   set_gdbarch_register_name (gdbarch, s390_register_name);
1828   set_gdbarch_stab_reg_to_regnum (gdbarch, s390_stab_reg_to_regnum);
1829   set_gdbarch_dwarf_reg_to_regnum (gdbarch, s390_stab_reg_to_regnum);
1830   set_gdbarch_dwarf2_reg_to_regnum (gdbarch, s390_stab_reg_to_regnum);
1831   set_gdbarch_extract_struct_value_address
1832     (gdbarch, generic_cannot_extract_struct_value_address);
1833
1834   /* Parameters for inferior function calls.  */
1835   set_gdbarch_call_dummy_p (gdbarch, 1);
1836   set_gdbarch_use_generic_dummy_frames (gdbarch, 1);
1837   set_gdbarch_call_dummy_length (gdbarch, 0);
1838   set_gdbarch_call_dummy_location (gdbarch, AT_ENTRY_POINT);
1839   set_gdbarch_call_dummy_address (gdbarch, entry_point_address);
1840   set_gdbarch_call_dummy_start_offset (gdbarch, 0);
1841   set_gdbarch_pc_in_call_dummy (gdbarch, pc_in_call_dummy_at_entry_point);
1842   set_gdbarch_push_dummy_frame (gdbarch, generic_push_dummy_frame);
1843   set_gdbarch_push_arguments (gdbarch, s390_push_arguments);
1844   set_gdbarch_save_dummy_frame_tos (gdbarch, generic_save_dummy_frame_tos);
1845   set_gdbarch_call_dummy_breakpoint_offset_p (gdbarch, 1);
1846   set_gdbarch_call_dummy_breakpoint_offset (gdbarch, 0);
1847   set_gdbarch_call_dummy_stack_adjust_p (gdbarch, 0);
1848   set_gdbarch_fix_call_dummy (gdbarch, generic_fix_call_dummy);
1849   set_gdbarch_push_return_address (gdbarch, s390_push_return_address);
1850   set_gdbarch_sizeof_call_dummy_words (gdbarch,
1851                                        sizeof (s390_call_dummy_words));
1852   set_gdbarch_call_dummy_words (gdbarch, s390_call_dummy_words);
1853   set_gdbarch_coerce_float_to_double (gdbarch,
1854                                       standard_coerce_float_to_double);
1855
1856   switch (info.bfd_arch_info->mach)
1857     {
1858     case bfd_mach_s390_31:
1859       set_gdbarch_register_size (gdbarch, 4);
1860       set_gdbarch_register_raw_size (gdbarch, s390_register_raw_size);
1861       set_gdbarch_register_virtual_size (gdbarch, s390_register_raw_size);
1862       set_gdbarch_register_virtual_type (gdbarch, s390_register_virtual_type);
1863
1864       set_gdbarch_addr_bits_remove (gdbarch, s390_addr_bits_remove);
1865       set_gdbarch_register_bytes (gdbarch, S390_REGISTER_BYTES);
1866       break;
1867     case bfd_mach_s390_64:
1868       set_gdbarch_register_size (gdbarch, 8);
1869       set_gdbarch_register_raw_size (gdbarch, s390x_register_raw_size);
1870       set_gdbarch_register_virtual_size (gdbarch, s390x_register_raw_size);
1871       set_gdbarch_register_virtual_type (gdbarch,
1872                                          s390x_register_virtual_type);
1873
1874       set_gdbarch_long_bit (gdbarch, 64);
1875       set_gdbarch_long_long_bit (gdbarch, 64);
1876       set_gdbarch_ptr_bit (gdbarch, 64);
1877       set_gdbarch_register_bytes (gdbarch, S390X_REGISTER_BYTES);
1878       break;
1879     }
1880
1881   return gdbarch;
1882 }
1883
1884
1885
1886 void
1887 _initialize_s390_tdep ()
1888 {
1889
1890   /* Hook us into the gdbarch mechanism.  */
1891   register_gdbarch_init (bfd_arch_s390, s390_gdbarch_init);
1892   if (!tm_print_insn)           /* Someone may have already set it */
1893     tm_print_insn = gdb_print_insn_s390;
1894 }
1895
1896 #endif /* GDBSERVER */