2004-06-20 Andrew Cagney <cagney@gnu.org>
[deliverable/binutils-gdb.git] / gdb / v850-tdep.c
1 /* Target-dependent code for the NEC V850 for GDB, the GNU debugger.
2
3 Copyright 1996, 1998, 1999, 2000, 2001, 2002, 2003, 2004 Free
4 Software Foundation, Inc.
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,
21 Boston, MA 02111-1307, USA. */
22
23 #include "defs.h"
24 #include "frame.h"
25 #include "inferior.h"
26 #include "target.h"
27 #include "value.h"
28 #include "bfd.h"
29 #include "gdb_string.h"
30 #include "gdbcore.h"
31 #include "objfiles.h"
32 #include "arch-utils.h"
33 #include "regcache.h"
34 #include "symtab.h"
35 #include "dis-asm.h"
36
37 struct gdbarch_tdep
38 {
39 /* gdbarch target dependent data here. Currently unused for v850. */
40 };
41
42 /* Extra info which is saved in each frame_info. */
43 struct frame_extra_info
44 {
45 };
46
47 enum {
48 E_R0_REGNUM,
49 E_R1_REGNUM,
50 E_R2_REGNUM, E_SAVE1_START_REGNUM = E_R2_REGNUM, E_SAVE1_END_REGNUM = E_R2_REGNUM,
51 E_R3_REGNUM, E_SP_REGNUM = E_R3_REGNUM,
52 E_R4_REGNUM,
53 E_R5_REGNUM,
54 E_R6_REGNUM, E_ARG0_REGNUM = E_R6_REGNUM,
55 E_R7_REGNUM,
56 E_R8_REGNUM,
57 E_R9_REGNUM, E_ARGLAST_REGNUM = E_R9_REGNUM,
58 E_R10_REGNUM, E_V0_REGNUM = E_R10_REGNUM,
59 E_R11_REGNUM, E_V1_REGNUM = E_R11_REGNUM,
60 E_R12_REGNUM,
61 E_R13_REGNUM,
62 E_R14_REGNUM,
63 E_R15_REGNUM,
64 E_R16_REGNUM,
65 E_R17_REGNUM,
66 E_R18_REGNUM,
67 E_R19_REGNUM,
68 E_R20_REGNUM, E_SAVE2_START_REGNUM = E_R20_REGNUM,
69 E_R21_REGNUM,
70 E_R22_REGNUM,
71 E_R23_REGNUM,
72 E_R24_REGNUM,
73 E_R25_REGNUM,
74 E_R26_REGNUM,
75 E_R27_REGNUM,
76 E_R28_REGNUM,
77 E_R29_REGNUM, E_SAVE2_END_REGNUM = E_R29_REGNUM, E_FP_RAW_REGNUM = E_R29_REGNUM,
78 E_R30_REGNUM, E_EP_REGNUM = E_R30_REGNUM,
79 E_R31_REGNUM, E_SAVE3_START_REGNUM = E_R31_REGNUM, E_SAVE3_END_REGNUM = E_R31_REGNUM, E_RP_REGNUM = E_R31_REGNUM,
80 E_R32_REGNUM, E_SR0_REGNUM = E_R32_REGNUM,
81 E_R33_REGNUM,
82 E_R34_REGNUM,
83 E_R35_REGNUM,
84 E_R36_REGNUM,
85 E_R37_REGNUM, E_PS_REGNUM = E_R37_REGNUM,
86 E_R38_REGNUM,
87 E_R39_REGNUM,
88 E_R40_REGNUM,
89 E_R41_REGNUM,
90 E_R42_REGNUM,
91 E_R43_REGNUM,
92 E_R44_REGNUM,
93 E_R45_REGNUM,
94 E_R46_REGNUM,
95 E_R47_REGNUM,
96 E_R48_REGNUM,
97 E_R49_REGNUM,
98 E_R50_REGNUM,
99 E_R51_REGNUM,
100 E_R52_REGNUM, E_CTBP_REGNUM = E_R52_REGNUM,
101 E_R53_REGNUM,
102 E_R54_REGNUM,
103 E_R55_REGNUM,
104 E_R56_REGNUM,
105 E_R57_REGNUM,
106 E_R58_REGNUM,
107 E_R59_REGNUM,
108 E_R60_REGNUM,
109 E_R61_REGNUM,
110 E_R62_REGNUM,
111 E_R63_REGNUM,
112 E_R64_REGNUM, E_PC_REGNUM = E_R64_REGNUM,
113 E_R65_REGNUM, E_FP_REGNUM = E_R65_REGNUM,
114 E_NUM_REGS
115 };
116
117 enum
118 {
119 v850_reg_size = 4
120 };
121
122 /* Size of all registers as a whole. */
123 enum
124 {
125 E_ALL_REGS_SIZE = (E_NUM_REGS) * v850_reg_size
126 };
127
128 /* Size of return datatype which fits into all return registers. */
129 enum
130 {
131 E_MAX_RETTYPE_SIZE_IN_REGS = 2 * v850_reg_size
132 };
133
134 static LONGEST call_dummy_nil[] = {0};
135
136 static char *v850_generic_reg_names[] =
137 { "r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7",
138 "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15",
139 "r16", "r17", "r18", "r19", "r20", "r21", "r22", "r23",
140 "r24", "r25", "r26", "r27", "r28", "r29", "r30", "r31",
141 "eipc", "eipsw", "fepc", "fepsw", "ecr", "psw", "sr6", "sr7",
142 "sr8", "sr9", "sr10", "sr11", "sr12", "sr13", "sr14", "sr15",
143 "sr16", "sr17", "sr18", "sr19", "sr20", "sr21", "sr22", "sr23",
144 "sr24", "sr25", "sr26", "sr27", "sr28", "sr29", "sr30", "sr31",
145 "pc", "fp"
146 };
147
148 static char *v850e_reg_names[] =
149 {
150 "r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7",
151 "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15",
152 "r16", "r17", "r18", "r19", "r20", "r21", "r22", "r23",
153 "r24", "r25", "r26", "r27", "r28", "r29", "r30", "r31",
154 "eipc", "eipsw", "fepc", "fepsw", "ecr", "psw", "sr6", "sr7",
155 "sr8", "sr9", "sr10", "sr11", "sr12", "sr13", "sr14", "sr15",
156 "ctpc", "ctpsw", "dbpc", "dbpsw", "ctbp", "sr21", "sr22", "sr23",
157 "sr24", "sr25", "sr26", "sr27", "sr28", "sr29", "sr30", "sr31",
158 "pc", "fp"
159 };
160
161 char **v850_register_names = v850_generic_reg_names;
162
163 struct
164 {
165 char **regnames;
166 int mach;
167 }
168 v850_processor_type_table[] =
169 {
170 {
171 v850_generic_reg_names, bfd_mach_v850
172 }
173 ,
174 {
175 v850e_reg_names, bfd_mach_v850e
176 }
177 ,
178 {
179 v850e_reg_names, bfd_mach_v850e1
180 }
181 ,
182 {
183 NULL, 0
184 }
185 };
186
187 /* Info gleaned from scanning a function's prologue. */
188
189 struct pifsr /* Info about one saved reg */
190 {
191 int framereg; /* Frame reg (SP or FP) */
192 int offset; /* Offset from framereg */
193 int cur_frameoffset; /* Current frameoffset */
194 int reg; /* Saved register number */
195 };
196
197 struct prologue_info
198 {
199 int framereg;
200 int frameoffset;
201 int start_function;
202 struct pifsr *pifsrs;
203 };
204
205 static CORE_ADDR v850_scan_prologue (CORE_ADDR pc, struct prologue_info *fs);
206
207 /* Function: v850_register_name
208 Returns the name of the v850/v850e register N. */
209
210 static const char *
211 v850_register_name (int regnum)
212 {
213 if (regnum < 0 || regnum >= E_NUM_REGS)
214 internal_error (__FILE__, __LINE__,
215 "v850_register_name: illegal register number %d",
216 regnum);
217 else
218 return v850_register_names[regnum];
219
220 }
221
222 /* Function: v850_register_byte
223 Returns the byte position in the register cache for register N. */
224
225 static int
226 v850_register_byte (int regnum)
227 {
228 if (regnum < 0 || regnum >= E_NUM_REGS)
229 internal_error (__FILE__, __LINE__,
230 "v850_register_byte: illegal register number %d",
231 regnum);
232 else
233 return regnum * v850_reg_size;
234 }
235
236 /* Function: v850_register_raw_size
237 Returns the number of bytes occupied by the register on the target. */
238
239 static int
240 v850_register_raw_size (int regnum)
241 {
242 if (regnum < 0 || regnum >= E_NUM_REGS)
243 internal_error (__FILE__, __LINE__,
244 "v850_register_raw_size: illegal register number %d",
245 regnum);
246 /* Only the PC has 4 Byte, all other registers 2 Byte. */
247 else
248 return v850_reg_size;
249 }
250
251 /* Function: v850_reg_virtual_type
252 Returns the default type for register N. */
253
254 static struct type *
255 v850_reg_virtual_type (int regnum)
256 {
257 if (regnum < 0 || regnum >= E_NUM_REGS)
258 internal_error (__FILE__, __LINE__,
259 "v850_register_virtual_type: illegal register number %d",
260 regnum);
261 else if (regnum == E_PC_REGNUM)
262 return builtin_type_uint32;
263 else
264 return builtin_type_int32;
265 }
266
267 static int
268 v850_type_is_scalar (struct type *t)
269 {
270 return (TYPE_CODE (t) != TYPE_CODE_STRUCT
271 && TYPE_CODE (t) != TYPE_CODE_UNION
272 && TYPE_CODE (t) != TYPE_CODE_ARRAY);
273 }
274
275 /* Should call_function allocate stack space for a struct return? */
276 static int
277 v850_use_struct_convention (int gcc_p, struct type *type)
278 {
279 /* According to ABI:
280 * return TYPE_LENGTH (type) > 8);
281 */
282
283 /* Current implementation in gcc: */
284
285 int i;
286 struct type *fld_type, *tgt_type;
287
288 /* 1. The value is greater than 8 bytes -> returned by copying */
289 if (TYPE_LENGTH (type) > 8)
290 return 1;
291
292 /* 2. The value is a single basic type -> returned in register */
293 if (v850_type_is_scalar (type))
294 return 0;
295
296 /* The value is a structure or union with a single element
297 * and that element is either a single basic type or an array of
298 * a single basic type whoes size is greater than or equal to 4
299 * -> returned in register */
300 if ((TYPE_CODE (type) == TYPE_CODE_STRUCT
301 || TYPE_CODE (type) == TYPE_CODE_UNION)
302 && TYPE_NFIELDS (type) == 1)
303 {
304 fld_type = TYPE_FIELD_TYPE (type, 0);
305 if (v850_type_is_scalar (fld_type) && TYPE_LENGTH (fld_type) >= 4)
306 return 0;
307
308 if (TYPE_CODE (fld_type) == TYPE_CODE_ARRAY)
309 {
310 tgt_type = TYPE_TARGET_TYPE (fld_type);
311 if (v850_type_is_scalar (tgt_type) && TYPE_LENGTH (tgt_type) >= 4)
312 return 0;
313 }
314 }
315
316 /* The value is a structure whose first element is an integer or
317 * a float, and which contains no arrays of more than two elements
318 * -> returned in register */
319 if (TYPE_CODE (type) == TYPE_CODE_STRUCT
320 && v850_type_is_scalar (TYPE_FIELD_TYPE (type, 0))
321 && TYPE_LENGTH (TYPE_FIELD_TYPE (type, 0)) == 4)
322 {
323 for (i = 1; i < TYPE_NFIELDS (type); ++i)
324 {
325 fld_type = TYPE_FIELD_TYPE (type, 0);
326 if (TYPE_CODE (fld_type) == TYPE_CODE_ARRAY)
327 {
328 tgt_type = TYPE_TARGET_TYPE (fld_type);
329 if (TYPE_LENGTH (fld_type) >= 0 && TYPE_LENGTH (tgt_type) >= 0
330 && TYPE_LENGTH (fld_type) / TYPE_LENGTH (tgt_type) > 2)
331 return 1;
332 }
333 }
334 return 0;
335 }
336
337 /* The value is a union which contains at least one field which
338 * would be returned in registers according to these rules
339 * -> returned in register */
340 if (TYPE_CODE (type) == TYPE_CODE_UNION)
341 {
342 for (i = 0; i < TYPE_NFIELDS (type); ++i)
343 {
344 fld_type = TYPE_FIELD_TYPE (type, 0);
345 if (!v850_use_struct_convention (0, fld_type))
346 return 0;
347 }
348 }
349
350 return 1;
351 }
352 \f
353
354
355 /* Structure for mapping bits in register lists to register numbers. */
356 struct reg_list
357 {
358 long mask;
359 int regno;
360 };
361
362 /* Helper function for v850_scan_prologue to handle prepare instruction. */
363
364 static void
365 handle_prepare (int insn, int insn2, CORE_ADDR * current_pc_ptr,
366 struct prologue_info *pi, struct pifsr **pifsr_ptr)
367 {
368 CORE_ADDR current_pc = *current_pc_ptr;
369 struct pifsr *pifsr = *pifsr_ptr;
370 long next = insn2 & 0xffff;
371 long list12 = ((insn & 1) << 16) + (next & 0xffe0);
372 long offset = (insn & 0x3e) << 1;
373 static struct reg_list reg_table[] =
374 {
375 {0x00800, 20}, /* r20 */
376 {0x00400, 21}, /* r21 */
377 {0x00200, 22}, /* r22 */
378 {0x00100, 23}, /* r23 */
379 {0x08000, 24}, /* r24 */
380 {0x04000, 25}, /* r25 */
381 {0x02000, 26}, /* r26 */
382 {0x01000, 27}, /* r27 */
383 {0x00080, 28}, /* r28 */
384 {0x00040, 29}, /* r29 */
385 {0x10000, 30}, /* ep */
386 {0x00020, 31}, /* lp */
387 {0, 0} /* end of table */
388 };
389 int i;
390
391 if ((next & 0x1f) == 0x0b) /* skip imm16 argument */
392 current_pc += 2;
393 else if ((next & 0x1f) == 0x13) /* skip imm16 argument */
394 current_pc += 2;
395 else if ((next & 0x1f) == 0x1b) /* skip imm32 argument */
396 current_pc += 4;
397
398 /* Calculate the total size of the saved registers, and add it
399 it to the immediate value used to adjust SP. */
400 for (i = 0; reg_table[i].mask != 0; i++)
401 if (list12 & reg_table[i].mask)
402 offset += v850_register_raw_size (reg_table[i].regno);
403 pi->frameoffset -= offset;
404
405 /* Calculate the offsets of the registers relative to the value
406 the SP will have after the registers have been pushed and the
407 imm5 value has been subtracted from it. */
408 if (pifsr)
409 {
410 for (i = 0; reg_table[i].mask != 0; i++)
411 {
412 if (list12 & reg_table[i].mask)
413 {
414 int reg = reg_table[i].regno;
415 offset -= v850_register_raw_size (reg);
416 pifsr->reg = reg;
417 pifsr->offset = offset;
418 pifsr->cur_frameoffset = pi->frameoffset;
419 #ifdef DEBUG
420 printf_filtered ("\tSaved register r%d, offset %d", reg, pifsr->offset);
421 #endif
422 pifsr++;
423 }
424 }
425 }
426 #ifdef DEBUG
427 printf_filtered ("\tfound ctret after regsave func");
428 #endif
429
430 /* Set result parameters. */
431 *current_pc_ptr = current_pc;
432 *pifsr_ptr = pifsr;
433 }
434
435
436 /* Helper function for v850_scan_prologue to handle pushm/pushl instructions.
437 FIXME: the SR bit of the register list is not supported; must check
438 that the compiler does not ever generate this bit. */
439
440 static void
441 handle_pushm (int insn, int insn2, struct prologue_info *pi,
442 struct pifsr **pifsr_ptr)
443 {
444 struct pifsr *pifsr = *pifsr_ptr;
445 long list12 = ((insn & 0x0f) << 16) + (insn2 & 0xfff0);
446 long offset = 0;
447 static struct reg_list pushml_reg_table[] =
448 {
449 {0x80000, E_PS_REGNUM}, /* PSW */
450 {0x40000, 1}, /* r1 */
451 {0x20000, 2}, /* r2 */
452 {0x10000, 3}, /* r3 */
453 {0x00800, 4}, /* r4 */
454 {0x00400, 5}, /* r5 */
455 {0x00200, 6}, /* r6 */
456 {0x00100, 7}, /* r7 */
457 {0x08000, 8}, /* r8 */
458 {0x04000, 9}, /* r9 */
459 {0x02000, 10}, /* r10 */
460 {0x01000, 11}, /* r11 */
461 {0x00080, 12}, /* r12 */
462 {0x00040, 13}, /* r13 */
463 {0x00020, 14}, /* r14 */
464 {0x00010, 15}, /* r15 */
465 {0, 0} /* end of table */
466 };
467 static struct reg_list pushmh_reg_table[] =
468 {
469 {0x80000, 16}, /* r16 */
470 {0x40000, 17}, /* r17 */
471 {0x20000, 18}, /* r18 */
472 {0x10000, 19}, /* r19 */
473 {0x00800, 20}, /* r20 */
474 {0x00400, 21}, /* r21 */
475 {0x00200, 22}, /* r22 */
476 {0x00100, 23}, /* r23 */
477 {0x08000, 24}, /* r24 */
478 {0x04000, 25}, /* r25 */
479 {0x02000, 26}, /* r26 */
480 {0x01000, 27}, /* r27 */
481 {0x00080, 28}, /* r28 */
482 {0x00040, 29}, /* r29 */
483 {0x00010, 30}, /* r30 */
484 {0x00020, 31}, /* r31 */
485 {0, 0} /* end of table */
486 };
487 struct reg_list *reg_table;
488 int i;
489
490 /* Is this a pushml or a pushmh? */
491 if ((insn2 & 7) == 1)
492 reg_table = pushml_reg_table;
493 else
494 reg_table = pushmh_reg_table;
495
496 /* Calculate the total size of the saved registers, and add it
497 it to the immediate value used to adjust SP. */
498 for (i = 0; reg_table[i].mask != 0; i++)
499 if (list12 & reg_table[i].mask)
500 offset += v850_register_raw_size (reg_table[i].regno);
501 pi->frameoffset -= offset;
502
503 /* Calculate the offsets of the registers relative to the value
504 the SP will have after the registers have been pushed and the
505 imm5 value is subtracted from it. */
506 if (pifsr)
507 {
508 for (i = 0; reg_table[i].mask != 0; i++)
509 {
510 if (list12 & reg_table[i].mask)
511 {
512 int reg = reg_table[i].regno;
513 offset -= v850_register_raw_size (reg);
514 pifsr->reg = reg;
515 pifsr->offset = offset;
516 pifsr->cur_frameoffset = pi->frameoffset;
517 #ifdef DEBUG
518 printf_filtered ("\tSaved register r%d, offset %d", reg, pifsr->offset);
519 #endif
520 pifsr++;
521 }
522 }
523 }
524 #ifdef DEBUG
525 printf_filtered ("\tfound ctret after regsave func");
526 #endif
527
528 /* Set result parameters. */
529 *pifsr_ptr = pifsr;
530 }
531 \f
532
533
534
535 /* Function: scan_prologue
536 Scan the prologue of the function that contains PC, and record what
537 we find in PI. Returns the pc after the prologue. Note that the
538 addresses saved in frame->saved_regs are just frame relative (negative
539 offsets from the frame pointer). This is because we don't know the
540 actual value of the frame pointer yet. In some circumstances, the
541 frame pointer can't be determined till after we have scanned the
542 prologue. */
543
544 static CORE_ADDR
545 v850_scan_prologue (CORE_ADDR pc, struct prologue_info *pi)
546 {
547 CORE_ADDR func_addr, prologue_end, current_pc;
548 struct pifsr *pifsr, *pifsr_tmp;
549 int fp_used;
550 int ep_used;
551 int reg;
552 CORE_ADDR save_pc, save_end;
553 int regsave_func_p;
554 int r12_tmp;
555
556 /* First, figure out the bounds of the prologue so that we can limit the
557 search to something reasonable. */
558
559 if (find_pc_partial_function (pc, NULL, &func_addr, NULL))
560 {
561 struct symtab_and_line sal;
562
563 sal = find_pc_line (func_addr, 0);
564
565 if (func_addr == entry_point_address ())
566 pi->start_function = 1;
567 else
568 pi->start_function = 0;
569
570 #if 0
571 if (sal.line == 0)
572 prologue_end = pc;
573 else
574 prologue_end = sal.end;
575 #else
576 prologue_end = pc;
577 #endif
578 }
579 else
580 { /* We're in the boondocks */
581 func_addr = pc - 100;
582 prologue_end = pc;
583 }
584
585 prologue_end = min (prologue_end, pc);
586
587 /* Now, search the prologue looking for instructions that setup fp, save
588 rp, adjust sp and such. We also record the frame offset of any saved
589 registers. */
590
591 pi->frameoffset = 0;
592 pi->framereg = E_SP_REGNUM;
593 fp_used = 0;
594 ep_used = 0;
595 pifsr = pi->pifsrs;
596 regsave_func_p = 0;
597 save_pc = 0;
598 save_end = 0;
599 r12_tmp = 0;
600
601 #ifdef DEBUG
602 printf_filtered ("Current_pc = 0x%.8lx, prologue_end = 0x%.8lx\n",
603 (long) func_addr, (long) prologue_end);
604 #endif
605
606 for (current_pc = func_addr; current_pc < prologue_end;)
607 {
608 int insn;
609 int insn2 = -1; /* dummy value */
610
611 #ifdef DEBUG
612 fprintf_filtered (gdb_stdlog, "0x%.8lx ", (long) current_pc);
613 gdb_print_insn (current_pc, gdb_stdlog);
614 #endif
615
616 insn = read_memory_unsigned_integer (current_pc, 2);
617 current_pc += 2;
618 if ((insn & 0x0780) >= 0x0600) /* Four byte instruction? */
619 {
620 insn2 = read_memory_unsigned_integer (current_pc, 2);
621 current_pc += 2;
622 }
623
624 if ((insn & 0xffc0) == ((10 << 11) | 0x0780) && !regsave_func_p)
625 { /* jarl <func>,10 */
626 long low_disp = insn2 & ~(long) 1;
627 long disp = (((((insn & 0x3f) << 16) + low_disp)
628 & ~(long) 1) ^ 0x00200000) - 0x00200000;
629
630 save_pc = current_pc;
631 save_end = prologue_end;
632 regsave_func_p = 1;
633 current_pc += disp - 4;
634 prologue_end = (current_pc
635 + (2 * 3) /* moves to/from ep */
636 + 4 /* addi <const>,sp,sp */
637 + 2 /* jmp [r10] */
638 + (2 * 12) /* sst.w to save r2, r20-r29, r31 */
639 + 20); /* slop area */
640
641 #ifdef DEBUG
642 printf_filtered ("\tfound jarl <func>,r10, disp = %ld, low_disp = %ld, new pc = 0x%.8lx\n",
643 disp, low_disp, (long) current_pc + 2);
644 #endif
645 continue;
646 }
647 else if ((insn & 0xffc0) == 0x0200 && !regsave_func_p)
648 { /* callt <imm6> */
649 long ctbp = read_register (E_CTBP_REGNUM);
650 long adr = ctbp + ((insn & 0x3f) << 1);
651
652 save_pc = current_pc;
653 save_end = prologue_end;
654 regsave_func_p = 1;
655 current_pc = ctbp + (read_memory_unsigned_integer (adr, 2) & 0xffff);
656 prologue_end = (current_pc
657 + (2 * 3) /* prepare list2,imm5,sp/imm */
658 + 4 /* ctret */
659 + 20); /* slop area */
660
661 #ifdef DEBUG
662 printf_filtered ("\tfound callt, ctbp = 0x%.8lx, adr = %.8lx, new pc = 0x%.8lx\n",
663 ctbp, adr, (long) current_pc);
664 #endif
665 continue;
666 }
667 else if ((insn & 0xffc0) == 0x0780) /* prepare list2,imm5 */
668 {
669 handle_prepare (insn, insn2, &current_pc, pi, &pifsr);
670 continue;
671 }
672 else if (insn == 0x07e0 && regsave_func_p && insn2 == 0x0144)
673 { /* ctret after processing register save function */
674 current_pc = save_pc;
675 prologue_end = save_end;
676 regsave_func_p = 0;
677 #ifdef DEBUG
678 printf_filtered ("\tfound ctret after regsave func");
679 #endif
680 continue;
681 }
682 else if ((insn & 0xfff0) == 0x07e0 && (insn2 & 5) == 1)
683 { /* pushml, pushmh */
684 handle_pushm (insn, insn2, pi, &pifsr);
685 continue;
686 }
687 else if ((insn & 0xffe0) == 0x0060 && regsave_func_p)
688 { /* jmp after processing register save function */
689 current_pc = save_pc;
690 prologue_end = save_end;
691 regsave_func_p = 0;
692 #ifdef DEBUG
693 printf_filtered ("\tfound jmp after regsave func");
694 #endif
695 continue;
696 }
697 else if ((insn & 0x07c0) == 0x0780 /* jarl or jr */
698 || (insn & 0xffe0) == 0x0060 /* jmp */
699 || (insn & 0x0780) == 0x0580) /* branch */
700 {
701 #ifdef DEBUG
702 printf_filtered ("\n");
703 #endif
704 break; /* Ran into end of prologue */
705 }
706
707 else if ((insn & 0xffe0) == ((E_SP_REGNUM << 11) | 0x0240)) /* add <imm>,sp */
708 pi->frameoffset += ((insn & 0x1f) ^ 0x10) - 0x10;
709 else if (insn == ((E_SP_REGNUM << 11) | 0x0600 | E_SP_REGNUM)) /* addi <imm>,sp,sp */
710 pi->frameoffset += insn2;
711 else if (insn == ((E_FP_RAW_REGNUM << 11) | 0x0000 | E_SP_REGNUM)) /* mov sp,fp */
712 {
713 fp_used = 1;
714 pi->framereg = E_FP_RAW_REGNUM;
715 }
716
717 else if (insn == ((E_R12_REGNUM << 11) | 0x0640 | E_R0_REGNUM)) /* movhi hi(const),r0,r12 */
718 r12_tmp = insn2 << 16;
719 else if (insn == ((E_R12_REGNUM << 11) | 0x0620 | E_R12_REGNUM)) /* movea lo(const),r12,r12 */
720 r12_tmp += insn2;
721 else if (insn == ((E_SP_REGNUM << 11) | 0x01c0 | E_R12_REGNUM) && r12_tmp) /* add r12,sp */
722 pi->frameoffset = r12_tmp;
723 else if (insn == ((E_EP_REGNUM << 11) | 0x0000 | E_SP_REGNUM)) /* mov sp,ep */
724 ep_used = 1;
725 else if (insn == ((E_EP_REGNUM << 11) | 0x0000 | E_R1_REGNUM)) /* mov r1,ep */
726 ep_used = 0;
727 else if (((insn & 0x07ff) == (0x0760 | E_SP_REGNUM) /* st.w <reg>,<offset>[sp] */
728 || (fp_used
729 && (insn & 0x07ff) == (0x0760 | E_FP_RAW_REGNUM))) /* st.w <reg>,<offset>[fp] */
730 && pifsr
731 && (((reg = (insn >> 11) & 0x1f) >= E_SAVE1_START_REGNUM && reg <= E_SAVE1_END_REGNUM)
732 || (reg >= E_SAVE2_START_REGNUM && reg <= E_SAVE2_END_REGNUM)
733 || (reg >= E_SAVE3_START_REGNUM && reg <= E_SAVE3_END_REGNUM)))
734 {
735 pifsr->reg = reg;
736 pifsr->offset = insn2 & ~1;
737 pifsr->cur_frameoffset = pi->frameoffset;
738 #ifdef DEBUG
739 printf_filtered ("\tSaved register r%d, offset %d", reg, pifsr->offset);
740 #endif
741 pifsr++;
742 }
743
744 else if (ep_used /* sst.w <reg>,<offset>[ep] */
745 && ((insn & 0x0781) == 0x0501)
746 && pifsr
747 && (((reg = (insn >> 11) & 0x1f) >= E_SAVE1_START_REGNUM && reg <= E_SAVE1_END_REGNUM)
748 || (reg >= E_SAVE2_START_REGNUM && reg <= E_SAVE2_END_REGNUM)
749 || (reg >= E_SAVE3_START_REGNUM && reg <= E_SAVE3_END_REGNUM)))
750 {
751 pifsr->reg = reg;
752 pifsr->offset = (insn & 0x007e) << 1;
753 pifsr->cur_frameoffset = pi->frameoffset;
754 #ifdef DEBUG
755 printf_filtered ("\tSaved register r%d, offset %d", reg, pifsr->offset);
756 #endif
757 pifsr++;
758 }
759
760 #ifdef DEBUG
761 printf_filtered ("\n");
762 #endif
763 }
764
765 if (pifsr)
766 pifsr->framereg = 0; /* Tie off last entry */
767
768 /* Fix up any offsets to the final offset. If a frame pointer was created, use it
769 instead of the stack pointer. */
770 for (pifsr_tmp = pi->pifsrs; pifsr_tmp && pifsr_tmp != pifsr; pifsr_tmp++)
771 {
772 pifsr_tmp->offset -= pi->frameoffset - pifsr_tmp->cur_frameoffset;
773 pifsr_tmp->framereg = pi->framereg;
774
775 #ifdef DEBUG
776 printf_filtered ("Saved register r%d, offset = %d, framereg = r%d\n",
777 pifsr_tmp->reg, pifsr_tmp->offset, pifsr_tmp->framereg);
778 #endif
779 }
780
781 #ifdef DEBUG
782 printf_filtered ("Framereg = r%d, frameoffset = %d\n", pi->framereg, pi->frameoffset);
783 #endif
784
785 return current_pc;
786 }
787
788 /* Function: find_callers_reg
789 Find REGNUM on the stack. Otherwise, it's in an active register.
790 One thing we might want to do here is to check REGNUM against the
791 clobber mask, and somehow flag it as invalid if it isn't saved on
792 the stack somewhere. This would provide a graceful failure mode
793 when trying to get the value of caller-saves registers for an inner
794 frame. */
795
796 static CORE_ADDR
797 v850_find_callers_reg (struct frame_info *fi, int regnum)
798 {
799 for (; fi; fi = get_next_frame (fi))
800 if (deprecated_pc_in_call_dummy (get_frame_pc (fi)))
801 return deprecated_read_register_dummy (get_frame_pc (fi),
802 get_frame_base (fi), regnum);
803 else if (deprecated_get_frame_saved_regs (fi)[regnum] != 0)
804 return read_memory_unsigned_integer (deprecated_get_frame_saved_regs (fi)[regnum],
805 v850_register_raw_size (regnum));
806
807 return read_register (regnum);
808 }
809
810 /* Function: frame_chain
811 Figure out the frame prior to FI. Unfortunately, this involves
812 scanning the prologue of the caller, which will also be done
813 shortly by v850_init_extra_frame_info. For the dummy frame, we
814 just return the stack pointer that was in use at the time the
815 function call was made. */
816
817 static CORE_ADDR
818 v850_frame_chain (struct frame_info *fi)
819 {
820 struct prologue_info pi;
821 CORE_ADDR callers_pc, fp;
822
823 /* First, find out who called us */
824 callers_pc = DEPRECATED_FRAME_SAVED_PC (fi);
825 /* If caller is a call-dummy, then our FP bears no relation to his FP! */
826 fp = v850_find_callers_reg (fi, E_FP_RAW_REGNUM);
827 if (deprecated_pc_in_call_dummy (callers_pc))
828 return fp; /* caller is call-dummy: return oldest value of FP */
829
830 /* Caller is NOT a call-dummy, so everything else should just work.
831 Even if THIS frame is a call-dummy! */
832 pi.pifsrs = NULL;
833
834 v850_scan_prologue (callers_pc, &pi);
835
836 if (pi.start_function)
837 return 0; /* Don't chain beyond the start function */
838
839 if (pi.framereg == E_FP_RAW_REGNUM)
840 return v850_find_callers_reg (fi, pi.framereg);
841
842 return get_frame_base (fi) - pi.frameoffset;
843 }
844
845 /* Function: skip_prologue
846 Return the address of the first code past the prologue of the function. */
847
848 static CORE_ADDR
849 v850_skip_prologue (CORE_ADDR pc)
850 {
851 CORE_ADDR func_addr, func_end;
852
853 /* See what the symbol table says */
854
855 if (find_pc_partial_function (pc, NULL, &func_addr, &func_end))
856 {
857 struct symtab_and_line sal;
858
859 sal = find_pc_line (func_addr, 0);
860
861 if (sal.line != 0 && sal.end < func_end)
862 return sal.end;
863 else
864 /* Either there's no line info, or the line after the prologue is after
865 the end of the function. In this case, there probably isn't a
866 prologue. */
867 return pc;
868 }
869
870 /* We can't find the start of this function, so there's nothing we can do. */
871 return pc;
872 }
873
874 /* Function: pop_frame
875 This routine gets called when either the user uses the `return'
876 command, or the call dummy breakpoint gets hit. */
877
878 static void
879 v850_pop_frame (void)
880 {
881 struct frame_info *frame = get_current_frame ();
882 int regnum;
883
884 if (deprecated_pc_in_call_dummy (get_frame_pc (frame)))
885 deprecated_pop_dummy_frame ();
886 else
887 {
888 write_register (E_PC_REGNUM, DEPRECATED_FRAME_SAVED_PC (frame));
889
890 for (regnum = 0; regnum < E_NUM_REGS; regnum++)
891 if (deprecated_get_frame_saved_regs (frame)[regnum] != 0)
892 write_register (regnum,
893 read_memory_unsigned_integer (deprecated_get_frame_saved_regs (frame)[regnum],
894 v850_register_raw_size (regnum)));
895
896 write_register (E_SP_REGNUM, get_frame_base (frame));
897 }
898
899 flush_cached_frames ();
900 }
901
902 /* Function: push_arguments
903 Setup arguments and RP for a call to the target. First four args
904 go in R6->R9, subsequent args go into sp + 16 -> sp + ... Structs
905 are passed by reference. 64 bit quantities (doubles and long
906 longs) may be split between the regs and the stack. When calling a
907 function that returns a struct, a pointer to the struct is passed
908 in as a secret first argument (always in R6).
909
910 Stack space for the args has NOT been allocated: that job is up to us.
911 */
912
913 static CORE_ADDR
914 v850_push_arguments (int nargs, struct value **args, CORE_ADDR sp,
915 int struct_return, CORE_ADDR struct_addr)
916 {
917 int argreg;
918 int argnum;
919 int len = 0;
920 int stack_offset;
921
922 /* First, just for safety, make sure stack is aligned */
923 sp &= ~3;
924
925 /* The offset onto the stack at which we will start copying parameters
926 (after the registers are used up) begins at 16 rather than at zero.
927 I don't really know why, that's just the way it seems to work. */
928 stack_offset = 16;
929
930 /* Now make space on the stack for the args. */
931 for (argnum = 0; argnum < nargs; argnum++)
932 len += ((TYPE_LENGTH (VALUE_TYPE (args[argnum])) + 3) & ~3);
933 sp -= len + stack_offset; /* possibly over-allocating, but it works... */
934 /* (you might think we could allocate 16 bytes */
935 /* less, but the ABI seems to use it all! ) */
936
937 argreg = E_ARG0_REGNUM;
938 /* the struct_return pointer occupies the first parameter-passing reg */
939 if (struct_return)
940 argreg++;
941
942 /* Now load as many as possible of the first arguments into
943 registers, and push the rest onto the stack. There are 16 bytes
944 in four registers available. Loop thru args from first to last. */
945 for (argnum = 0; argnum < nargs; argnum++)
946 {
947 int len;
948 char *val;
949 char valbuf[v850_register_raw_size (E_ARG0_REGNUM)];
950
951 if (!v850_type_is_scalar (VALUE_TYPE (*args))
952 && TYPE_LENGTH (VALUE_TYPE (*args)) > E_MAX_RETTYPE_SIZE_IN_REGS)
953 {
954 store_unsigned_integer (valbuf, 4, VALUE_ADDRESS (*args));
955 len = 4;
956 val = valbuf;
957 }
958 else
959 {
960 len = TYPE_LENGTH (VALUE_TYPE (*args));
961 val = (char *) VALUE_CONTENTS (*args);
962 }
963
964 while (len > 0)
965 if (argreg <= E_ARGLAST_REGNUM)
966 {
967 CORE_ADDR regval;
968
969 regval = extract_unsigned_integer (val, v850_register_raw_size (argreg));
970 write_register (argreg, regval);
971
972 len -= v850_register_raw_size (argreg);
973 val += v850_register_raw_size (argreg);
974 argreg++;
975 }
976 else
977 {
978 write_memory (sp + stack_offset, val, 4);
979
980 len -= 4;
981 val += 4;
982 stack_offset += 4;
983 }
984 args++;
985 }
986 return sp;
987 }
988
989 /* Function: push_return_address (pc)
990 Set up the return address for the inferior function call.
991 Needed for targets where we don't actually execute a JSR/BSR instruction */
992
993 static CORE_ADDR
994 v850_push_return_address (CORE_ADDR pc, CORE_ADDR sp)
995 {
996 write_register (E_RP_REGNUM, entry_point_address ());
997 return sp;
998 }
999
1000 /* Function: frame_saved_pc
1001 Find the caller of this frame. We do this by seeing if E_RP_REGNUM
1002 is saved in the stack anywhere, otherwise we get it from the
1003 registers. If the inner frame is a dummy frame, return its PC
1004 instead of RP, because that's where "caller" of the dummy-frame
1005 will be found. */
1006
1007 static CORE_ADDR
1008 v850_frame_saved_pc (struct frame_info *fi)
1009 {
1010 if (deprecated_pc_in_call_dummy (get_frame_pc (fi)))
1011 return deprecated_read_register_dummy (get_frame_pc (fi),
1012 get_frame_base (fi), E_PC_REGNUM);
1013 else
1014 return v850_find_callers_reg (fi, E_RP_REGNUM);
1015 }
1016
1017
1018 static CORE_ADDR
1019 v850_saved_pc_after_call (struct frame_info *ignore)
1020 {
1021 return read_register (E_RP_REGNUM);
1022 }
1023
1024 static void
1025 v850_extract_return_value (struct type *type, char *regbuf, char *valbuf)
1026 {
1027 CORE_ADDR return_buffer;
1028
1029 if (!v850_use_struct_convention (0, type))
1030 {
1031 /* Scalar return values of <= 8 bytes are returned in
1032 E_V0_REGNUM to E_V1_REGNUM. */
1033 memcpy (valbuf,
1034 &regbuf[DEPRECATED_REGISTER_BYTE (E_V0_REGNUM)],
1035 TYPE_LENGTH (type));
1036 }
1037 else
1038 {
1039 /* Aggregates and return values > 8 bytes are returned in memory,
1040 pointed to by R6. */
1041 return_buffer =
1042 extract_unsigned_integer (regbuf + DEPRECATED_REGISTER_BYTE (E_V0_REGNUM),
1043 DEPRECATED_REGISTER_RAW_SIZE (E_V0_REGNUM));
1044
1045 read_memory (return_buffer, valbuf, TYPE_LENGTH (type));
1046 }
1047 }
1048
1049 const static unsigned char *
1050 v850_breakpoint_from_pc (CORE_ADDR *pcptr, int *lenptr)
1051 {
1052 static unsigned char breakpoint[] = { 0x85, 0x05 };
1053 *lenptr = sizeof (breakpoint);
1054 return breakpoint;
1055 }
1056
1057 static void
1058 v850_store_return_value (struct type *type, char *valbuf)
1059 {
1060 CORE_ADDR return_buffer;
1061
1062 if (!v850_use_struct_convention (0, type))
1063 deprecated_write_register_bytes (DEPRECATED_REGISTER_BYTE (E_V0_REGNUM), valbuf,
1064 TYPE_LENGTH (type));
1065 else
1066 {
1067 return_buffer = read_register (E_V0_REGNUM);
1068 write_memory (return_buffer, valbuf, TYPE_LENGTH (type));
1069 }
1070 }
1071
1072 static void
1073 v850_frame_init_saved_regs (struct frame_info *fi)
1074 {
1075 struct prologue_info pi;
1076 struct pifsr pifsrs[E_NUM_REGS + 1], *pifsr;
1077 CORE_ADDR func_addr, func_end;
1078
1079 if (!deprecated_get_frame_saved_regs (fi))
1080 {
1081 frame_saved_regs_zalloc (fi);
1082
1083 /* The call dummy doesn't save any registers on the stack, so we
1084 can return now. */
1085 if (deprecated_pc_in_call_dummy (get_frame_pc (fi)))
1086 return;
1087
1088 /* Find the beginning of this function, so we can analyze its
1089 prologue. */
1090 if (find_pc_partial_function (get_frame_pc (fi), NULL, &func_addr, &func_end))
1091 {
1092 pi.pifsrs = pifsrs;
1093
1094 v850_scan_prologue (get_frame_pc (fi), &pi);
1095
1096 if (!get_next_frame (fi) && pi.framereg == E_SP_REGNUM)
1097 deprecated_update_frame_base_hack (fi, read_register (pi.framereg) - pi.frameoffset);
1098
1099 for (pifsr = pifsrs; pifsr->framereg; pifsr++)
1100 {
1101 deprecated_get_frame_saved_regs (fi)[pifsr->reg] = pifsr->offset + get_frame_base (fi);
1102
1103 if (pifsr->framereg == E_SP_REGNUM)
1104 deprecated_get_frame_saved_regs (fi)[pifsr->reg] += pi.frameoffset;
1105 }
1106 }
1107 /* Else we're out of luck (can't debug completely stripped code).
1108 FIXME. */
1109 }
1110 }
1111
1112 /* Function: init_extra_frame_info
1113 Setup the frame's frame pointer, pc, and frame addresses for saved
1114 registers. Most of the work is done in scan_prologue().
1115
1116 Note that when we are called for the last frame (currently active frame),
1117 that get_frame_pc (fi) and fi->frame will already be setup. However, fi->frame will
1118 be valid only if this routine uses FP. For previous frames, fi-frame will
1119 always be correct (since that is derived from v850_frame_chain ()).
1120
1121 We can be called with the PC in the call dummy under two
1122 circumstances. First, during normal backtracing, second, while
1123 figuring out the frame pointer just prior to calling the target
1124 function (see call_function_by_hand). */
1125
1126 static void
1127 v850_init_extra_frame_info (int fromleaf, struct frame_info *fi)
1128 {
1129 struct prologue_info pi;
1130
1131 if (get_next_frame (fi))
1132 deprecated_update_frame_pc_hack (fi, DEPRECATED_FRAME_SAVED_PC (get_next_frame (fi)));
1133
1134 v850_frame_init_saved_regs (fi);
1135 }
1136
1137 static void
1138 v850_store_struct_return (CORE_ADDR addr, CORE_ADDR sp)
1139 {
1140 write_register (E_ARG0_REGNUM, addr);
1141 }
1142
1143 static CORE_ADDR
1144 v850_target_read_fp (void)
1145 {
1146 return read_register (E_FP_RAW_REGNUM);
1147 }
1148
1149 static struct gdbarch *
1150 v850_gdbarch_init (struct gdbarch_info info, struct gdbarch_list *arches)
1151 {
1152 struct gdbarch_tdep *tdep = NULL;
1153 struct gdbarch *gdbarch;
1154 int i;
1155
1156 /* find a candidate among the list of pre-declared architectures. */
1157 arches = gdbarch_list_lookup_by_info (arches, &info);
1158 if (arches != NULL)
1159 return (arches->gdbarch);
1160
1161 #if 0
1162 tdep = (struct gdbarch_tdep *) xmalloc (sizeof (struct gdbarch_tdep));
1163 #endif
1164
1165 /* Change the register names based on the current machine type. */
1166 if (info.bfd_arch_info->arch != bfd_arch_v850)
1167 return 0;
1168
1169 gdbarch = gdbarch_alloc (&info, 0);
1170
1171 /* NOTE: cagney/2002-12-06: This can be deleted when this arch is
1172 ready to unwind the PC first (see frame.c:get_prev_frame()). */
1173 set_gdbarch_deprecated_init_frame_pc (gdbarch, deprecated_init_frame_pc_default);
1174
1175 for (i = 0; v850_processor_type_table[i].regnames != NULL; i++)
1176 {
1177 if (v850_processor_type_table[i].mach == info.bfd_arch_info->mach)
1178 {
1179 v850_register_names = v850_processor_type_table[i].regnames;
1180 break;
1181 }
1182 }
1183
1184 /*
1185 * Basic register fields and methods.
1186 */
1187 set_gdbarch_num_regs (gdbarch, E_NUM_REGS);
1188 set_gdbarch_num_pseudo_regs (gdbarch, 0);
1189 set_gdbarch_sp_regnum (gdbarch, E_SP_REGNUM);
1190 set_gdbarch_deprecated_fp_regnum (gdbarch, E_FP_REGNUM);
1191 set_gdbarch_pc_regnum (gdbarch, E_PC_REGNUM);
1192 set_gdbarch_register_name (gdbarch, v850_register_name);
1193 set_gdbarch_deprecated_register_size (gdbarch, v850_reg_size);
1194 set_gdbarch_deprecated_register_bytes (gdbarch, E_ALL_REGS_SIZE);
1195 set_gdbarch_deprecated_register_byte (gdbarch, v850_register_byte);
1196 set_gdbarch_deprecated_register_raw_size (gdbarch, v850_register_raw_size);
1197 set_gdbarch_deprecated_register_virtual_size (gdbarch, v850_register_raw_size);
1198 set_gdbarch_deprecated_register_virtual_type (gdbarch, v850_reg_virtual_type);
1199
1200 set_gdbarch_deprecated_target_read_fp (gdbarch, v850_target_read_fp);
1201
1202 /*
1203 * Frame Info
1204 */
1205 set_gdbarch_deprecated_frame_init_saved_regs (gdbarch, v850_frame_init_saved_regs);
1206 set_gdbarch_deprecated_init_extra_frame_info (gdbarch, v850_init_extra_frame_info);
1207 set_gdbarch_deprecated_frame_chain (gdbarch, v850_frame_chain);
1208 set_gdbarch_deprecated_saved_pc_after_call (gdbarch, v850_saved_pc_after_call);
1209 set_gdbarch_deprecated_frame_saved_pc (gdbarch, v850_frame_saved_pc);
1210 set_gdbarch_skip_prologue (gdbarch, v850_skip_prologue);
1211
1212 /*
1213 * Miscelany
1214 */
1215 /* Stack grows up. */
1216 set_gdbarch_inner_than (gdbarch, core_addr_lessthan);
1217
1218 /*
1219 * Call Dummies
1220 *
1221 * These values and methods are used when gdb calls a target function. */
1222 set_gdbarch_deprecated_push_return_address (gdbarch, v850_push_return_address);
1223 set_gdbarch_deprecated_extract_return_value (gdbarch, v850_extract_return_value);
1224 set_gdbarch_deprecated_push_arguments (gdbarch, v850_push_arguments);
1225 set_gdbarch_deprecated_pop_frame (gdbarch, v850_pop_frame);
1226 set_gdbarch_deprecated_store_struct_return (gdbarch, v850_store_struct_return);
1227 set_gdbarch_deprecated_store_return_value (gdbarch, v850_store_return_value);
1228 set_gdbarch_deprecated_use_struct_convention (gdbarch, v850_use_struct_convention);
1229 set_gdbarch_breakpoint_from_pc (gdbarch, v850_breakpoint_from_pc);
1230
1231 set_gdbarch_int_bit (gdbarch, 4 * TARGET_CHAR_BIT);
1232 set_gdbarch_ptr_bit (gdbarch, 4 * TARGET_CHAR_BIT);
1233 set_gdbarch_addr_bit (gdbarch, 4 * TARGET_CHAR_BIT);
1234 set_gdbarch_long_double_bit (gdbarch, 8 * TARGET_CHAR_BIT);
1235
1236 /* Should be using push_dummy_call. */
1237 set_gdbarch_deprecated_dummy_write_sp (gdbarch, deprecated_write_sp);
1238
1239 set_gdbarch_print_insn (gdbarch, print_insn_v850);
1240
1241 return gdbarch;
1242 }
1243
1244 extern initialize_file_ftype _initialize_v850_tdep; /* -Wmissing-prototypes */
1245
1246 void
1247 _initialize_v850_tdep (void)
1248 {
1249 register_gdbarch_init (bfd_arch_v850, v850_gdbarch_init);
1250 }
This page took 0.0552 seconds and 5 git commands to generate.