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