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