* elfread.c (elf_symtab_read): Allocate correct number of tail
[deliverable/binutils-gdb.git] / gdb / frv-tdep.c
CommitLineData
456f8b9d 1/* Target-dependent code for the Fujitsu FR-V, for GDB, the GNU Debugger.
1e698235 2 Copyright 2002, 2003 Free Software Foundation, Inc.
456f8b9d
DB
3
4 This file is part of GDB.
5
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA. */
20
21#include "defs.h"
8baa6f92 22#include "gdb_string.h"
456f8b9d
DB
23#include "inferior.h"
24#include "symfile.h" /* for entry_point_address */
25#include "gdbcore.h"
26#include "arch-utils.h"
27#include "regcache.h"
8baa6f92 28#include "frame.h"
1cb761c7
KB
29#include "frame-unwind.h"
30#include "frame-base.h"
8baa6f92 31#include "trad-frame.h"
dcc6aaff 32#include "dis-asm.h"
456f8b9d
DB
33
34extern void _initialize_frv_tdep (void);
35
36static gdbarch_init_ftype frv_gdbarch_init;
37
38static gdbarch_register_name_ftype frv_register_name;
456f8b9d 39static gdbarch_breakpoint_from_pc_ftype frv_breakpoint_from_pc;
46a16dba 40static gdbarch_adjust_breakpoint_address_ftype frv_gdbarch_adjust_breakpoint_address;
456f8b9d 41static gdbarch_skip_prologue_ftype frv_skip_prologue;
456f8b9d
DB
42static gdbarch_deprecated_extract_return_value_ftype frv_extract_return_value;
43static gdbarch_deprecated_extract_struct_value_address_ftype frv_extract_struct_value_address;
456f8b9d 44static gdbarch_frameless_function_invocation_ftype frv_frameless_function_invocation;
aed7f26a
MS
45static gdbarch_deprecated_push_arguments_ftype frv_push_arguments;
46static gdbarch_deprecated_saved_pc_after_call_ftype frv_saved_pc_after_call;
456f8b9d
DB
47
48/* Register numbers. You can change these as needed, but don't forget
49 to update the simulator accordingly. */
50enum {
51 /* The total number of registers we know exist. */
52 frv_num_regs = 147,
53
54 /* Register numbers 0 -- 63 are always reserved for general-purpose
55 registers. The chip at hand may have less. */
56 first_gpr_regnum = 0,
57 sp_regnum = 1,
58 fp_regnum = 2,
59 struct_return_regnum = 3,
60 last_gpr_regnum = 63,
61
62 /* Register numbers 64 -- 127 are always reserved for floating-point
63 registers. The chip at hand may have less. */
64 first_fpr_regnum = 64,
65 last_fpr_regnum = 127,
66
67 /* Register numbers 128 on up are always reserved for special-purpose
68 registers. */
69 first_spr_regnum = 128,
70 pc_regnum = 128,
71 psr_regnum = 129,
72 ccr_regnum = 130,
73 cccr_regnum = 131,
74 tbr_regnum = 135,
75 brr_regnum = 136,
76 dbar0_regnum = 137,
77 dbar1_regnum = 138,
78 dbar2_regnum = 139,
79 dbar3_regnum = 140,
80 lr_regnum = 145,
81 lcr_regnum = 146,
82 last_spr_regnum = 146
83};
84
85static LONGEST frv_call_dummy_words[] =
86{0};
87
88
1cb761c7 89struct frv_unwind_cache /* was struct frame_extra_info */
456f8b9d 90 {
1cb761c7
KB
91 /* The previous frame's inner-most stack address. Used as this
92 frame ID's stack_addr. */
93 CORE_ADDR prev_sp;
456f8b9d 94
1cb761c7
KB
95 /* The frame's base, optionally used by the high-level debug info. */
96 CORE_ADDR base;
8baa6f92
KB
97
98 /* Table indicating the location of each and every register. */
99 struct trad_frame_saved_reg *saved_regs;
456f8b9d
DB
100 };
101
102
103/* A structure describing a particular variant of the FRV.
104 We allocate and initialize one of these structures when we create
105 the gdbarch object for a variant.
106
107 At the moment, all the FR variants we support differ only in which
108 registers are present; the portable code of GDB knows that
109 registers whose names are the empty string don't exist, so the
110 `register_names' array captures all the per-variant information we
111 need.
112
113 in the future, if we need to have per-variant maps for raw size,
114 virtual type, etc., we should replace register_names with an array
115 of structures, each of which gives all the necessary info for one
116 register. Don't stick parallel arrays in here --- that's so
117 Fortran. */
118struct gdbarch_tdep
119{
120 /* How many general-purpose registers does this variant have? */
121 int num_gprs;
122
123 /* How many floating-point registers does this variant have? */
124 int num_fprs;
125
126 /* How many hardware watchpoints can it support? */
127 int num_hw_watchpoints;
128
129 /* How many hardware breakpoints can it support? */
130 int num_hw_breakpoints;
131
132 /* Register names. */
133 char **register_names;
134};
135
136#define CURRENT_VARIANT (gdbarch_tdep (current_gdbarch))
137
138
139/* Allocate a new variant structure, and set up default values for all
140 the fields. */
141static struct gdbarch_tdep *
5ae5f592 142new_variant (void)
456f8b9d
DB
143{
144 struct gdbarch_tdep *var;
145 int r;
146 char buf[20];
147
148 var = xmalloc (sizeof (*var));
149 memset (var, 0, sizeof (*var));
150
151 var->num_gprs = 64;
152 var->num_fprs = 64;
153 var->num_hw_watchpoints = 0;
154 var->num_hw_breakpoints = 0;
155
156 /* By default, don't supply any general-purpose or floating-point
157 register names. */
158 var->register_names = (char **) xmalloc (frv_num_regs * sizeof (char *));
159 for (r = 0; r < frv_num_regs; r++)
160 var->register_names[r] = "";
161
162 /* Do, however, supply default names for the special-purpose
163 registers. */
164 for (r = first_spr_regnum; r <= last_spr_regnum; ++r)
165 {
166 sprintf (buf, "x%d", r);
167 var->register_names[r] = xstrdup (buf);
168 }
169
170 var->register_names[pc_regnum] = "pc";
171 var->register_names[lr_regnum] = "lr";
172 var->register_names[lcr_regnum] = "lcr";
173
174 var->register_names[psr_regnum] = "psr";
175 var->register_names[ccr_regnum] = "ccr";
176 var->register_names[cccr_regnum] = "cccr";
177 var->register_names[tbr_regnum] = "tbr";
178
179 /* Debug registers. */
180 var->register_names[brr_regnum] = "brr";
181 var->register_names[dbar0_regnum] = "dbar0";
182 var->register_names[dbar1_regnum] = "dbar1";
183 var->register_names[dbar2_regnum] = "dbar2";
184 var->register_names[dbar3_regnum] = "dbar3";
185
186 return var;
187}
188
189
190/* Indicate that the variant VAR has NUM_GPRS general-purpose
191 registers, and fill in the names array appropriately. */
192static void
193set_variant_num_gprs (struct gdbarch_tdep *var, int num_gprs)
194{
195 int r;
196
197 var->num_gprs = num_gprs;
198
199 for (r = 0; r < num_gprs; ++r)
200 {
201 char buf[20];
202
203 sprintf (buf, "gr%d", r);
204 var->register_names[first_gpr_regnum + r] = xstrdup (buf);
205 }
206}
207
208
209/* Indicate that the variant VAR has NUM_FPRS floating-point
210 registers, and fill in the names array appropriately. */
211static void
212set_variant_num_fprs (struct gdbarch_tdep *var, int num_fprs)
213{
214 int r;
215
216 var->num_fprs = num_fprs;
217
218 for (r = 0; r < num_fprs; ++r)
219 {
220 char buf[20];
221
222 sprintf (buf, "fr%d", r);
223 var->register_names[first_fpr_regnum + r] = xstrdup (buf);
224 }
225}
226
227
228static const char *
229frv_register_name (int reg)
230{
231 if (reg < 0)
232 return "?toosmall?";
233 if (reg >= frv_num_regs)
234 return "?toolarge?";
235
236 return CURRENT_VARIANT->register_names[reg];
237}
238
456f8b9d 239static struct type *
7f398216 240frv_register_type (struct gdbarch *gdbarch, int reg)
456f8b9d
DB
241{
242 if (reg >= 64 && reg <= 127)
243 return builtin_type_float;
244 else
245 return builtin_type_int;
246}
247
248static int
249frv_register_byte (int reg)
250{
251 return (reg * 4);
252}
253
254static const unsigned char *
255frv_breakpoint_from_pc (CORE_ADDR *pcptr, int *lenp)
256{
257 static unsigned char breakpoint[] = {0xc0, 0x70, 0x00, 0x01};
258 *lenp = sizeof (breakpoint);
259 return breakpoint;
260}
261
46a16dba
KB
262/* Define the maximum number of instructions which may be packed into a
263 bundle (VLIW instruction). */
264static const int max_instrs_per_bundle = 8;
265
266/* Define the size (in bytes) of an FR-V instruction. */
267static const int frv_instr_size = 4;
268
269/* Adjust a breakpoint's address to account for the FR-V architecture's
270 constraint that a break instruction must not appear as any but the
271 first instruction in the bundle. */
272static CORE_ADDR
273frv_gdbarch_adjust_breakpoint_address (struct gdbarch *gdbarch, CORE_ADDR bpaddr)
274{
275 int count = max_instrs_per_bundle;
276 CORE_ADDR addr = bpaddr - frv_instr_size;
277 CORE_ADDR func_start = get_pc_function_start (bpaddr);
278
279 /* Find the end of the previous packing sequence. This will be indicated
280 by either attempting to access some inaccessible memory or by finding
281 an instruction word whose packing bit is set to one. */
282 while (count-- > 0 && addr >= func_start)
283 {
284 char instr[frv_instr_size];
285 int status;
286
287 status = read_memory_nobpt (addr, instr, sizeof instr);
288
289 if (status != 0)
290 break;
291
292 /* This is a big endian architecture, so byte zero will have most
293 significant byte. The most significant bit of this byte is the
294 packing bit. */
295 if (instr[0] & 0x80)
296 break;
297
298 addr -= frv_instr_size;
299 }
300
301 if (count > 0)
302 bpaddr = addr + frv_instr_size;
303
304 return bpaddr;
305}
306
456f8b9d
DB
307
308/* Return true if REG is a caller-saves ("scratch") register,
309 false otherwise. */
310static int
311is_caller_saves_reg (int reg)
312{
313 return ((4 <= reg && reg <= 7)
314 || (14 <= reg && reg <= 15)
315 || (32 <= reg && reg <= 47));
316}
317
318
319/* Return true if REG is a callee-saves register, false otherwise. */
320static int
321is_callee_saves_reg (int reg)
322{
323 return ((16 <= reg && reg <= 31)
324 || (48 <= reg && reg <= 63));
325}
326
327
328/* Return true if REG is an argument register, false otherwise. */
329static int
330is_argument_reg (int reg)
331{
332 return (8 <= reg && reg <= 13);
333}
334
335
336/* Scan an FR-V prologue, starting at PC, until frame->PC.
337 If FRAME is non-zero, fill in its saved_regs with appropriate addresses.
338 We assume FRAME's saved_regs array has already been allocated and cleared.
339 Return the first PC value after the prologue.
340
341 Note that, for unoptimized code, we almost don't need this function
342 at all; all arguments and locals live on the stack, so we just need
343 the FP to find everything. The catch: structures passed by value
344 have their addresses living in registers; they're never spilled to
345 the stack. So if you ever want to be able to get to these
346 arguments in any frame but the top, you'll need to do this serious
347 prologue analysis. */
348static CORE_ADDR
1cb761c7
KB
349frv_analyze_prologue (CORE_ADDR pc, struct frame_info *next_frame,
350 struct frv_unwind_cache *info)
456f8b9d
DB
351{
352 /* When writing out instruction bitpatterns, we use the following
353 letters to label instruction fields:
354 P - The parallel bit. We don't use this.
355 J - The register number of GRj in the instruction description.
356 K - The register number of GRk in the instruction description.
357 I - The register number of GRi.
358 S - a signed imediate offset.
359 U - an unsigned immediate offset.
360
361 The dots below the numbers indicate where hex digit boundaries
362 fall, to make it easier to check the numbers. */
363
364 /* Non-zero iff we've seen the instruction that initializes the
365 frame pointer for this function's frame. */
366 int fp_set = 0;
367
368 /* If fp_set is non_zero, then this is the distance from
369 the stack pointer to frame pointer: fp = sp + fp_offset. */
370 int fp_offset = 0;
371
372 /* Total size of frame prior to any alloca operations. */
373 int framesize = 0;
374
1cb761c7
KB
375 /* Flag indicating if lr has been saved on the stack. */
376 int lr_saved_on_stack = 0;
377
456f8b9d
DB
378 /* The number of the general-purpose register we saved the return
379 address ("link register") in, or -1 if we haven't moved it yet. */
380 int lr_save_reg = -1;
381
1cb761c7
KB
382 /* Offset (from sp) at which lr has been saved on the stack. */
383
384 int lr_sp_offset = 0;
456f8b9d
DB
385
386 /* If gr_saved[i] is non-zero, then we've noticed that general
387 register i has been saved at gr_sp_offset[i] from the stack
388 pointer. */
389 char gr_saved[64];
390 int gr_sp_offset[64];
391
392 memset (gr_saved, 0, sizeof (gr_saved));
393
1cb761c7 394 while (! next_frame || pc < frame_pc_unwind (next_frame))
456f8b9d
DB
395 {
396 LONGEST op = read_memory_integer (pc, 4);
397
398 /* The tests in this chain of ifs should be in order of
399 decreasing selectivity, so that more particular patterns get
400 to fire before less particular patterns. */
401
402 /* Setting the FP from the SP:
403 ori sp, 0, fp
404 P 000010 0100010 000001 000000000000 = 0x04881000
405 0 111111 1111111 111111 111111111111 = 0x7fffffff
406 . . . . . . . .
407 We treat this as part of the prologue. */
408 if ((op & 0x7fffffff) == 0x04881000)
409 {
410 fp_set = 1;
411 fp_offset = 0;
412 }
413
414 /* Move the link register to the scratch register grJ, before saving:
415 movsg lr, grJ
416 P 000100 0000011 010000 000111 JJJJJJ = 0x080d01c0
417 0 111111 1111111 111111 111111 000000 = 0x7fffffc0
418 . . . . . . . .
419 We treat this as part of the prologue. */
420 else if ((op & 0x7fffffc0) == 0x080d01c0)
421 {
422 int gr_j = op & 0x3f;
423
424 /* If we're moving it to a scratch register, that's fine. */
425 if (is_caller_saves_reg (gr_j))
426 lr_save_reg = gr_j;
427 /* Otherwise it's not a prologue instruction that we
428 recognize. */
429 else
430 break;
431 }
432
433 /* To save multiple callee-saves registers on the stack, at
434 offset zero:
435
436 std grK,@(sp,gr0)
437 P KKKKKK 0000011 000001 000011 000000 = 0x000c10c0
438 0 000000 1111111 111111 111111 111111 = 0x01ffffff
439
440 stq grK,@(sp,gr0)
441 P KKKKKK 0000011 000001 000100 000000 = 0x000c1100
442 0 000000 1111111 111111 111111 111111 = 0x01ffffff
443 . . . . . . . .
444 We treat this as part of the prologue, and record the register's
445 saved address in the frame structure. */
446 else if ((op & 0x01ffffff) == 0x000c10c0
447 || (op & 0x01ffffff) == 0x000c1100)
448 {
449 int gr_k = ((op >> 25) & 0x3f);
450 int ope = ((op >> 6) & 0x3f);
451 int count;
452 int i;
453
454 /* Is it an std or an stq? */
455 if (ope == 0x03)
456 count = 2;
457 else
458 count = 4;
459
460 /* Is it really a callee-saves register? */
461 if (is_callee_saves_reg (gr_k))
462 {
463 for (i = 0; i < count; i++)
464 {
465 gr_saved[gr_k + i] = 1;
466 gr_sp_offset[gr_k + i] = 4 * i;
467 }
468 }
469 else
470 /* It's not a prologue instruction. */
471 break;
472 }
473
474 /* Adjusting the stack pointer. (The stack pointer is GR1.)
475 addi sp, S, sp
476 P 000001 0010000 000001 SSSSSSSSSSSS = 0x02401000
477 0 111111 1111111 111111 000000000000 = 0x7ffff000
478 . . . . . . . .
479 We treat this as part of the prologue. */
480 else if ((op & 0x7ffff000) == 0x02401000)
481 {
482 /* Sign-extend the twelve-bit field.
483 (Isn't there a better way to do this?) */
484 int s = (((op & 0xfff) - 0x800) & 0xfff) - 0x800;
485
486 framesize -= s;
487 }
488
489 /* Setting the FP to a constant distance from the SP:
490 addi sp, S, fp
491 P 000010 0010000 000001 SSSSSSSSSSSS = 0x04401000
492 0 111111 1111111 111111 000000000000 = 0x7ffff000
493 . . . . . . . .
494 We treat this as part of the prologue. */
495 else if ((op & 0x7ffff000) == 0x04401000)
496 {
497 /* Sign-extend the twelve-bit field.
498 (Isn't there a better way to do this?) */
499 int s = (((op & 0xfff) - 0x800) & 0xfff) - 0x800;
500 fp_set = 1;
501 fp_offset = s;
502 }
503
504 /* To spill an argument register to a scratch register:
505 ori GRi, 0, GRk
506 P KKKKKK 0100010 IIIIII 000000000000 = 0x00880000
507 0 000000 1111111 000000 111111111111 = 0x01fc0fff
508 . . . . . . . .
509 For the time being, we treat this as a prologue instruction,
510 assuming that GRi is an argument register. This one's kind
511 of suspicious, because it seems like it could be part of a
512 legitimate body instruction. But we only come here when the
513 source info wasn't helpful, so we have to do the best we can.
514 Hopefully once GCC and GDB agree on how to emit line number
515 info for prologues, then this code will never come into play. */
516 else if ((op & 0x01fc0fff) == 0x00880000)
517 {
518 int gr_i = ((op >> 12) & 0x3f);
519
520 /* If the source isn't an arg register, then this isn't a
521 prologue instruction. */
522 if (! is_argument_reg (gr_i))
523 break;
524 }
525
526 /* To spill 16-bit values to the stack:
527 sthi GRk, @(fp, s)
528 P KKKKKK 1010001 000010 SSSSSSSSSSSS = 0x01442000
529 0 000000 1111111 111111 000000000000 = 0x01fff000
530 . . . . . . . .
531 And for 8-bit values, we use STB instructions.
532 stbi GRk, @(fp, s)
533 P KKKKKK 1010000 000010 SSSSSSSSSSSS = 0x01402000
534 0 000000 1111111 111111 000000000000 = 0x01fff000
535 . . . . . . . .
536 We check that GRk is really an argument register, and treat
537 all such as part of the prologue. */
538 else if ( (op & 0x01fff000) == 0x01442000
539 || (op & 0x01fff000) == 0x01402000)
540 {
541 int gr_k = ((op >> 25) & 0x3f);
542
543 if (! is_argument_reg (gr_k))
544 break; /* Source isn't an arg register. */
545 }
546
547 /* To save multiple callee-saves register on the stack, at a
548 non-zero offset:
549
550 stdi GRk, @(sp, s)
551 P KKKKKK 1010011 000001 SSSSSSSSSSSS = 0x014c1000
552 0 000000 1111111 111111 000000000000 = 0x01fff000
553 . . . . . . . .
554 stqi GRk, @(sp, s)
555 P KKKKKK 1010100 000001 SSSSSSSSSSSS = 0x01501000
556 0 000000 1111111 111111 000000000000 = 0x01fff000
557 . . . . . . . .
558 We treat this as part of the prologue, and record the register's
559 saved address in the frame structure. */
560 else if ((op & 0x01fff000) == 0x014c1000
561 || (op & 0x01fff000) == 0x01501000)
562 {
563 int gr_k = ((op >> 25) & 0x3f);
564 int count;
565 int i;
566
567 /* Is it a stdi or a stqi? */
568 if ((op & 0x01fff000) == 0x014c1000)
569 count = 2;
570 else
571 count = 4;
572
573 /* Is it really a callee-saves register? */
574 if (is_callee_saves_reg (gr_k))
575 {
576 /* Sign-extend the twelve-bit field.
577 (Isn't there a better way to do this?) */
578 int s = (((op & 0xfff) - 0x800) & 0xfff) - 0x800;
579
580 for (i = 0; i < count; i++)
581 {
582 gr_saved[gr_k + i] = 1;
583 gr_sp_offset[gr_k + i] = s + (4 * i);
584 }
585 }
586 else
587 /* It's not a prologue instruction. */
588 break;
589 }
590
591 /* Storing any kind of integer register at any constant offset
592 from any other register.
593
594 st GRk, @(GRi, gr0)
595 P KKKKKK 0000011 IIIIII 000010 000000 = 0x000c0080
596 0 000000 1111111 000000 111111 111111 = 0x01fc0fff
597 . . . . . . . .
598 sti GRk, @(GRi, d12)
599 P KKKKKK 1010010 IIIIII SSSSSSSSSSSS = 0x01480000
600 0 000000 1111111 000000 000000000000 = 0x01fc0000
601 . . . . . . . .
602 These could be almost anything, but a lot of prologue
603 instructions fall into this pattern, so let's decode the
604 instruction once, and then work at a higher level. */
605 else if (((op & 0x01fc0fff) == 0x000c0080)
606 || ((op & 0x01fc0000) == 0x01480000))
607 {
608 int gr_k = ((op >> 25) & 0x3f);
609 int gr_i = ((op >> 12) & 0x3f);
610 int offset;
611
612 /* Are we storing with gr0 as an offset, or using an
613 immediate value? */
614 if ((op & 0x01fc0fff) == 0x000c0080)
615 offset = 0;
616 else
617 offset = (((op & 0xfff) - 0x800) & 0xfff) - 0x800;
618
619 /* If the address isn't relative to the SP or FP, it's not a
620 prologue instruction. */
621 if (gr_i != sp_regnum && gr_i != fp_regnum)
622 break;
623
624 /* Saving the old FP in the new frame (relative to the SP). */
625 if (gr_k == fp_regnum && gr_i == sp_regnum)
1cb761c7
KB
626 {
627 gr_saved[fp_regnum] = 1;
628 gr_sp_offset[fp_regnum] = offset;
629 }
456f8b9d
DB
630
631 /* Saving callee-saves register(s) on the stack, relative to
632 the SP. */
633 else if (gr_i == sp_regnum
634 && is_callee_saves_reg (gr_k))
635 {
636 gr_saved[gr_k] = 1;
1cb761c7
KB
637 if (gr_i == sp_regnum)
638 gr_sp_offset[gr_k] = offset;
639 else
640 gr_sp_offset[gr_k] = offset + fp_offset;
456f8b9d
DB
641 }
642
643 /* Saving the scratch register holding the return address. */
644 else if (lr_save_reg != -1
645 && gr_k == lr_save_reg)
1cb761c7
KB
646 {
647 lr_saved_on_stack = 1;
648 if (gr_i == sp_regnum)
649 lr_sp_offset = offset;
650 else
651 lr_sp_offset = offset + fp_offset;
652 }
456f8b9d
DB
653
654 /* Spilling int-sized arguments to the stack. */
655 else if (is_argument_reg (gr_k))
656 ;
657
658 /* It's not a store instruction we recognize, so this must
659 be the end of the prologue. */
660 else
661 break;
662 }
663
664 /* It's not any instruction we recognize, so this must be the end
665 of the prologue. */
666 else
667 break;
668
669 pc += 4;
670 }
671
1cb761c7 672 if (next_frame && info)
456f8b9d 673 {
1cb761c7
KB
674 int i;
675 ULONGEST this_base;
456f8b9d
DB
676
677 /* If we know the relationship between the stack and frame
678 pointers, record the addresses of the registers we noticed.
679 Note that we have to do this as a separate step at the end,
680 because instructions may save relative to the SP, but we need
681 their addresses relative to the FP. */
682 if (fp_set)
1cb761c7
KB
683 frame_unwind_unsigned_register (next_frame, fp_regnum, &this_base);
684 else
685 frame_unwind_unsigned_register (next_frame, sp_regnum, &this_base);
456f8b9d 686
1cb761c7
KB
687 for (i = 0; i < 64; i++)
688 if (gr_saved[i])
689 info->saved_regs[i].addr = this_base - fp_offset + gr_sp_offset[i];
456f8b9d 690
1cb761c7
KB
691 info->prev_sp = this_base - fp_offset + framesize;
692 info->base = this_base;
693
694 /* If LR was saved on the stack, record its location. */
695 if (lr_saved_on_stack)
696 info->saved_regs[lr_regnum].addr = this_base - fp_offset + lr_sp_offset;
697
698 /* The call instruction moves the caller's PC in the callee's LR.
699 Since this is an unwind, do the reverse. Copy the location of LR
700 into PC (the address / regnum) so that a request for PC will be
701 converted into a request for the LR. */
702 info->saved_regs[pc_regnum] = info->saved_regs[lr_regnum];
703
704 /* Save the previous frame's computed SP value. */
705 trad_frame_set_value (info->saved_regs, sp_regnum, info->prev_sp);
456f8b9d
DB
706 }
707
708 return pc;
709}
710
711
712static CORE_ADDR
713frv_skip_prologue (CORE_ADDR pc)
714{
715 CORE_ADDR func_addr, func_end, new_pc;
716
717 new_pc = pc;
718
719 /* If the line table has entry for a line *within* the function
720 (i.e., not in the prologue, and not past the end), then that's
721 our location. */
722 if (find_pc_partial_function (pc, NULL, &func_addr, &func_end))
723 {
724 struct symtab_and_line sal;
725
726 sal = find_pc_line (func_addr, 0);
727
728 if (sal.line != 0 && sal.end < func_end)
729 {
730 new_pc = sal.end;
731 }
732 }
733
734 /* The FR-V prologue is at least five instructions long (twenty bytes).
735 If we didn't find a real source location past that, then
736 do a full analysis of the prologue. */
737 if (new_pc < pc + 20)
1cb761c7 738 new_pc = frv_analyze_prologue (pc, 0, 0);
456f8b9d
DB
739
740 return new_pc;
741}
742
1cb761c7
KB
743
744static struct frv_unwind_cache *
745frv_frame_unwind_cache (struct frame_info *next_frame,
746 void **this_prologue_cache)
456f8b9d 747{
1cb761c7
KB
748 struct gdbarch *gdbarch = get_frame_arch (next_frame);
749 CORE_ADDR pc;
750 ULONGEST prev_sp;
751 ULONGEST this_base;
752 struct frv_unwind_cache *info;
8baa6f92 753
1cb761c7
KB
754 if ((*this_prologue_cache))
755 return (*this_prologue_cache);
456f8b9d 756
1cb761c7
KB
757 info = FRAME_OBSTACK_ZALLOC (struct frv_unwind_cache);
758 (*this_prologue_cache) = info;
759 info->saved_regs = trad_frame_alloc_saved_regs (next_frame);
456f8b9d 760
1cb761c7
KB
761 /* Prologue analysis does the rest... */
762 frv_analyze_prologue (frame_func_unwind (next_frame), next_frame, info);
456f8b9d 763
1cb761c7 764 return info;
456f8b9d
DB
765}
766
456f8b9d
DB
767static void
768frv_extract_return_value (struct type *type, char *regbuf, char *valbuf)
769{
770 memcpy (valbuf, (regbuf
771 + frv_register_byte (8)
772 + (TYPE_LENGTH (type) < 4 ? 4 - TYPE_LENGTH (type) : 0)),
773 TYPE_LENGTH (type));
774}
775
776static CORE_ADDR
777frv_extract_struct_value_address (char *regbuf)
778{
8baa6f92
KB
779 return extract_unsigned_integer (regbuf +
780 frv_register_byte (struct_return_regnum),
7c0b4a20 781 4);
456f8b9d
DB
782}
783
784static void
785frv_store_struct_return (CORE_ADDR addr, CORE_ADDR sp)
786{
787 write_register (struct_return_regnum, addr);
788}
789
790static int
791frv_frameless_function_invocation (struct frame_info *frame)
792{
793 return frameless_look_for_prologue (frame);
794}
795
1cb761c7
KB
796static CORE_ADDR
797frv_frame_align (struct gdbarch *gdbarch, CORE_ADDR sp)
456f8b9d 798{
1cb761c7 799 /* Require dword alignment. */
5b03f266 800 return align_down (sp, 8);
456f8b9d
DB
801}
802
456f8b9d 803static CORE_ADDR
1cb761c7
KB
804frv_push_dummy_call (struct gdbarch *gdbarch, CORE_ADDR func_addr,
805 struct regcache *regcache, CORE_ADDR bp_addr,
806 int nargs, struct value **args, CORE_ADDR sp,
807 int struct_return, CORE_ADDR struct_addr)
456f8b9d
DB
808{
809 int argreg;
810 int argnum;
811 char *val;
812 char valbuf[4];
813 struct value *arg;
814 struct type *arg_type;
815 int len;
816 enum type_code typecode;
817 CORE_ADDR regval;
818 int stack_space;
819 int stack_offset;
820
821#if 0
822 printf("Push %d args at sp = %x, struct_return=%d (%x)\n",
823 nargs, (int) sp, struct_return, struct_addr);
824#endif
825
826 stack_space = 0;
827 for (argnum = 0; argnum < nargs; ++argnum)
5b03f266 828 stack_space += align_up (TYPE_LENGTH (VALUE_TYPE (args[argnum])), 4);
456f8b9d
DB
829
830 stack_space -= (6 * 4);
831 if (stack_space > 0)
832 sp -= stack_space;
833
834 /* Make sure stack is dword aligned. */
5b03f266 835 sp = align_down (sp, 8);
456f8b9d
DB
836
837 stack_offset = 0;
838
839 argreg = 8;
840
841 if (struct_return)
1cb761c7
KB
842 regcache_cooked_write_unsigned (regcache, struct_return_regnum,
843 struct_addr);
456f8b9d
DB
844
845 for (argnum = 0; argnum < nargs; ++argnum)
846 {
847 arg = args[argnum];
848 arg_type = check_typedef (VALUE_TYPE (arg));
849 len = TYPE_LENGTH (arg_type);
850 typecode = TYPE_CODE (arg_type);
851
852 if (typecode == TYPE_CODE_STRUCT || typecode == TYPE_CODE_UNION)
853 {
fbd9dcd3 854 store_unsigned_integer (valbuf, 4, VALUE_ADDRESS (arg));
456f8b9d
DB
855 typecode = TYPE_CODE_PTR;
856 len = 4;
857 val = valbuf;
858 }
859 else
860 {
861 val = (char *) VALUE_CONTENTS (arg);
862 }
863
864 while (len > 0)
865 {
866 int partial_len = (len < 4 ? len : 4);
867
868 if (argreg < 14)
869 {
7c0b4a20 870 regval = extract_unsigned_integer (val, partial_len);
456f8b9d
DB
871#if 0
872 printf(" Argnum %d data %x -> reg %d\n",
873 argnum, (int) regval, argreg);
874#endif
1cb761c7 875 regcache_cooked_write_unsigned (regcache, argreg, regval);
456f8b9d
DB
876 ++argreg;
877 }
878 else
879 {
880#if 0
881 printf(" Argnum %d data %x -> offset %d (%x)\n",
882 argnum, *((int *)val), stack_offset, (int) (sp + stack_offset));
883#endif
884 write_memory (sp + stack_offset, val, partial_len);
5b03f266 885 stack_offset += align_up (partial_len, 4);
456f8b9d
DB
886 }
887 len -= partial_len;
888 val += partial_len;
889 }
890 }
456f8b9d 891
1cb761c7
KB
892 /* Set the return address. For the frv, the return breakpoint is
893 always at BP_ADDR. */
894 regcache_cooked_write_unsigned (regcache, lr_regnum, bp_addr);
895
896 /* Finally, update the SP register. */
897 regcache_cooked_write_unsigned (regcache, sp_regnum, sp);
898
456f8b9d
DB
899 return sp;
900}
901
902static void
903frv_store_return_value (struct type *type, char *valbuf)
904{
905 int length = TYPE_LENGTH (type);
906 int reg8_offset = frv_register_byte (8);
907
908 if (length <= 4)
73937e03
AC
909 deprecated_write_register_bytes (reg8_offset + (4 - length), valbuf,
910 length);
456f8b9d 911 else if (length == 8)
73937e03 912 deprecated_write_register_bytes (reg8_offset, valbuf, length);
456f8b9d
DB
913 else
914 internal_error (__FILE__, __LINE__,
915 "Don't know how to return a %d-byte value.", length);
916}
917
456f8b9d 918
456f8b9d
DB
919/* Hardware watchpoint / breakpoint support for the FR500
920 and FR400. */
921
922int
923frv_check_watch_resources (int type, int cnt, int ot)
924{
925 struct gdbarch_tdep *var = CURRENT_VARIANT;
926
927 /* Watchpoints not supported on simulator. */
928 if (strcmp (target_shortname, "sim") == 0)
929 return 0;
930
931 if (type == bp_hardware_breakpoint)
932 {
933 if (var->num_hw_breakpoints == 0)
934 return 0;
935 else if (cnt <= var->num_hw_breakpoints)
936 return 1;
937 }
938 else
939 {
940 if (var->num_hw_watchpoints == 0)
941 return 0;
942 else if (ot)
943 return -1;
944 else if (cnt <= var->num_hw_watchpoints)
945 return 1;
946 }
947 return -1;
948}
949
950
951CORE_ADDR
5ae5f592 952frv_stopped_data_address (void)
456f8b9d
DB
953{
954 CORE_ADDR brr, dbar0, dbar1, dbar2, dbar3;
955
956 brr = read_register (brr_regnum);
957 dbar0 = read_register (dbar0_regnum);
958 dbar1 = read_register (dbar1_regnum);
959 dbar2 = read_register (dbar2_regnum);
960 dbar3 = read_register (dbar3_regnum);
961
962 if (brr & (1<<11))
963 return dbar0;
964 else if (brr & (1<<10))
965 return dbar1;
966 else if (brr & (1<<9))
967 return dbar2;
968 else if (brr & (1<<8))
969 return dbar3;
970 else
971 return 0;
972}
973
1cb761c7
KB
974static CORE_ADDR
975frv_unwind_pc (struct gdbarch *gdbarch, struct frame_info *next_frame)
976{
977 return frame_unwind_register_unsigned (next_frame, pc_regnum);
978}
979
980/* Given a GDB frame, determine the address of the calling function's
981 frame. This will be used to create a new GDB frame struct. */
982
983static void
984frv_frame_this_id (struct frame_info *next_frame,
985 void **this_prologue_cache, struct frame_id *this_id)
986{
987 struct frv_unwind_cache *info
988 = frv_frame_unwind_cache (next_frame, this_prologue_cache);
989 CORE_ADDR base;
990 CORE_ADDR func;
991 struct minimal_symbol *msym_stack;
992 struct frame_id id;
993
994 /* The FUNC is easy. */
995 func = frame_func_unwind (next_frame);
996
997 /* This is meant to halt the backtrace at "_start". Make sure we
998 don't halt it at a generic dummy frame. */
0f4d7c52 999 if (inside_entry_func (func))
1cb761c7
KB
1000 return;
1001
1002 /* Check if the stack is empty. */
1003 msym_stack = lookup_minimal_symbol ("_stack", NULL, NULL);
1004 if (msym_stack && info->base == SYMBOL_VALUE_ADDRESS (msym_stack))
1005 return;
1006
1007 /* Hopefully the prologue analysis either correctly determined the
1008 frame's base (which is the SP from the previous frame), or set
1009 that base to "NULL". */
1010 base = info->prev_sp;
1011 if (base == 0)
1012 return;
1013
1014 id = frame_id_build (base, func);
1015
1016 /* Check that we're not going round in circles with the same frame
1017 ID (but avoid applying the test to sentinel frames which do go
1018 round in circles). Can't use frame_id_eq() as that doesn't yet
1019 compare the frame's PC value. */
1020 if (frame_relative_level (next_frame) >= 0
1021 && get_frame_type (next_frame) != DUMMY_FRAME
1022 && frame_id_eq (get_frame_id (next_frame), id))
1023 return;
1024
1025 (*this_id) = id;
1026}
1027
1028static void
1029frv_frame_prev_register (struct frame_info *next_frame,
1030 void **this_prologue_cache,
1031 int regnum, int *optimizedp,
1032 enum lval_type *lvalp, CORE_ADDR *addrp,
1033 int *realnump, void *bufferp)
1034{
1035 struct frv_unwind_cache *info
1036 = frv_frame_unwind_cache (next_frame, this_prologue_cache);
1037 trad_frame_prev_register (next_frame, info->saved_regs, regnum,
1038 optimizedp, lvalp, addrp, realnump, bufferp);
1039}
1040
1041static const struct frame_unwind frv_frame_unwind = {
1042 NORMAL_FRAME,
1043 frv_frame_this_id,
1044 frv_frame_prev_register
1045};
1046
1047static const struct frame_unwind *
1048frv_frame_sniffer (struct frame_info *next_frame)
1049{
1050 return &frv_frame_unwind;
1051}
1052
1053static CORE_ADDR
1054frv_frame_base_address (struct frame_info *next_frame, void **this_cache)
1055{
1056 struct frv_unwind_cache *info
1057 = frv_frame_unwind_cache (next_frame, this_cache);
1058 return info->base;
1059}
1060
1061static const struct frame_base frv_frame_base = {
1062 &frv_frame_unwind,
1063 frv_frame_base_address,
1064 frv_frame_base_address,
1065 frv_frame_base_address
1066};
1067
1068static CORE_ADDR
1069frv_unwind_sp (struct gdbarch *gdbarch, struct frame_info *next_frame)
1070{
1071 return frame_unwind_register_unsigned (next_frame, sp_regnum);
1072}
1073
1074
1075/* Assuming NEXT_FRAME->prev is a dummy, return the frame ID of that
1076 dummy frame. The frame ID's base needs to match the TOS value
1077 saved by save_dummy_frame_tos(), and the PC match the dummy frame's
1078 breakpoint. */
1079
1080static struct frame_id
1081frv_unwind_dummy_id (struct gdbarch *gdbarch, struct frame_info *next_frame)
1082{
1083 return frame_id_build (frv_unwind_sp (gdbarch, next_frame),
1084 frame_pc_unwind (next_frame));
1085}
1086
1087
456f8b9d
DB
1088static struct gdbarch *
1089frv_gdbarch_init (struct gdbarch_info info, struct gdbarch_list *arches)
1090{
1091 struct gdbarch *gdbarch;
1092 struct gdbarch_tdep *var;
1093
1094 /* Check to see if we've already built an appropriate architecture
1095 object for this executable. */
1096 arches = gdbarch_list_lookup_by_info (arches, &info);
1097 if (arches)
1098 return arches->gdbarch;
1099
1100 /* Select the right tdep structure for this variant. */
1101 var = new_variant ();
1102 switch (info.bfd_arch_info->mach)
1103 {
1104 case bfd_mach_frv:
1105 case bfd_mach_frvsimple:
1106 case bfd_mach_fr500:
1107 case bfd_mach_frvtomcat:
1108 set_variant_num_gprs (var, 64);
1109 set_variant_num_fprs (var, 64);
1110 break;
1111
1112 case bfd_mach_fr400:
1113 set_variant_num_gprs (var, 32);
1114 set_variant_num_fprs (var, 32);
1115 break;
1116
1117 default:
1118 /* Never heard of this variant. */
1119 return 0;
1120 }
1121
1122 gdbarch = gdbarch_alloc (&info, var);
1123
1124 set_gdbarch_short_bit (gdbarch, 16);
1125 set_gdbarch_int_bit (gdbarch, 32);
1126 set_gdbarch_long_bit (gdbarch, 32);
1127 set_gdbarch_long_long_bit (gdbarch, 64);
1128 set_gdbarch_float_bit (gdbarch, 32);
1129 set_gdbarch_double_bit (gdbarch, 64);
1130 set_gdbarch_long_double_bit (gdbarch, 64);
1131 set_gdbarch_ptr_bit (gdbarch, 32);
1132
1133 set_gdbarch_num_regs (gdbarch, frv_num_regs);
1134 set_gdbarch_sp_regnum (gdbarch, sp_regnum);
0ba6dca9 1135 set_gdbarch_deprecated_fp_regnum (gdbarch, fp_regnum);
456f8b9d
DB
1136 set_gdbarch_pc_regnum (gdbarch, pc_regnum);
1137
1138 set_gdbarch_register_name (gdbarch, frv_register_name);
9c04cab7 1139 set_gdbarch_deprecated_register_byte (gdbarch, frv_register_byte);
7f398216 1140 set_gdbarch_register_type (gdbarch, frv_register_type);
456f8b9d
DB
1141
1142 set_gdbarch_skip_prologue (gdbarch, frv_skip_prologue);
1143 set_gdbarch_breakpoint_from_pc (gdbarch, frv_breakpoint_from_pc);
46a16dba 1144 set_gdbarch_adjust_breakpoint_address (gdbarch, frv_gdbarch_adjust_breakpoint_address);
456f8b9d 1145
456f8b9d
DB
1146 set_gdbarch_frame_args_skip (gdbarch, 0);
1147 set_gdbarch_frameless_function_invocation (gdbarch, frv_frameless_function_invocation);
1148
1fd35568 1149 set_gdbarch_use_struct_convention (gdbarch, always_use_struct_convention);
456f8b9d
DB
1150 set_gdbarch_deprecated_extract_return_value (gdbarch, frv_extract_return_value);
1151
4183d812 1152 set_gdbarch_deprecated_store_struct_return (gdbarch, frv_store_struct_return);
ebba8386 1153 set_gdbarch_deprecated_store_return_value (gdbarch, frv_store_return_value);
456f8b9d
DB
1154 set_gdbarch_deprecated_extract_struct_value_address (gdbarch, frv_extract_struct_value_address);
1155
1cb761c7
KB
1156 /* Frame stuff. */
1157 set_gdbarch_unwind_pc (gdbarch, frv_unwind_pc);
1158 set_gdbarch_unwind_sp (gdbarch, frv_unwind_sp);
1159 set_gdbarch_frame_align (gdbarch, frv_frame_align);
1160 frame_unwind_append_sniffer (gdbarch, frv_frame_sniffer);
1161 frame_base_set_default (gdbarch, &frv_frame_base);
456f8b9d 1162
1cb761c7
KB
1163 /* Settings for calling functions in the inferior. */
1164 set_gdbarch_push_dummy_call (gdbarch, frv_push_dummy_call);
1165 set_gdbarch_unwind_dummy_id (gdbarch, frv_unwind_dummy_id);
456f8b9d
DB
1166
1167 /* Settings that should be unnecessary. */
1168 set_gdbarch_inner_than (gdbarch, core_addr_lessthan);
1169
456f8b9d 1170 set_gdbarch_write_pc (gdbarch, generic_target_write_pc);
456f8b9d 1171
456f8b9d
DB
1172 set_gdbarch_decr_pc_after_break (gdbarch, 0);
1173 set_gdbarch_function_start_offset (gdbarch, 0);
456f8b9d
DB
1174
1175 set_gdbarch_remote_translate_xfer_address
aed7f26a 1176 (gdbarch, generic_remote_translate_xfer_address);
456f8b9d
DB
1177
1178 /* Hardware watchpoint / breakpoint support. */
1179 switch (info.bfd_arch_info->mach)
1180 {
1181 case bfd_mach_frv:
1182 case bfd_mach_frvsimple:
1183 case bfd_mach_fr500:
1184 case bfd_mach_frvtomcat:
1185 /* fr500-style hardware debugging support. */
1186 var->num_hw_watchpoints = 4;
1187 var->num_hw_breakpoints = 4;
1188 break;
1189
1190 case bfd_mach_fr400:
1191 /* fr400-style hardware debugging support. */
1192 var->num_hw_watchpoints = 2;
1193 var->num_hw_breakpoints = 4;
1194 break;
1195
1196 default:
1197 /* Otherwise, assume we don't have hardware debugging support. */
1198 var->num_hw_watchpoints = 0;
1199 var->num_hw_breakpoints = 0;
1200 break;
1201 }
1202
36482093
AC
1203 set_gdbarch_print_insn (gdbarch, print_insn_frv);
1204
456f8b9d
DB
1205 return gdbarch;
1206}
1207
1208void
1209_initialize_frv_tdep (void)
1210{
1211 register_gdbarch_init (bfd_arch_frv, frv_gdbarch_init);
456f8b9d 1212}
This page took 0.185695 seconds and 4 git commands to generate.