]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - gdb/v850-tdep.c
* v850-tdep.c (E_NUM_OF_V850_REGS, E_NUM_OF_V850E_REGS): Fix
[thirdparty/binutils-gdb.git] / gdb / v850-tdep.c
1 /* Target-dependent code for the NEC V850 for GDB, the GNU debugger.
2
3 Copyright (C) 1996, 1998-2005, 2007-2012 Free Software Foundation,
4 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 3 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, see <http://www.gnu.org/licenses/>. */
20
21 #include "defs.h"
22 #include "frame.h"
23 #include "frame-base.h"
24 #include "trad-frame.h"
25 #include "frame-unwind.h"
26 #include "dwarf2-frame.h"
27 #include "gdbtypes.h"
28 #include "inferior.h"
29 #include "gdb_string.h"
30 #include "gdb_assert.h"
31 #include "gdbcore.h"
32 #include "arch-utils.h"
33 #include "regcache.h"
34 #include "dis-asm.h"
35 #include "osabi.h"
36
37 enum
38 {
39 E_R0_REGNUM,
40 E_R1_REGNUM,
41 E_R2_REGNUM,
42 E_R3_REGNUM, E_SP_REGNUM = E_R3_REGNUM,
43 E_R4_REGNUM,
44 E_R5_REGNUM,
45 E_R6_REGNUM, E_ARG0_REGNUM = E_R6_REGNUM,
46 E_R7_REGNUM,
47 E_R8_REGNUM,
48 E_R9_REGNUM, E_ARGLAST_REGNUM = E_R9_REGNUM,
49 E_R10_REGNUM, E_V0_REGNUM = E_R10_REGNUM,
50 E_R11_REGNUM, E_V1_REGNUM = E_R11_REGNUM,
51 E_R12_REGNUM,
52 E_R13_REGNUM,
53 E_R14_REGNUM,
54 E_R15_REGNUM,
55 E_R16_REGNUM,
56 E_R17_REGNUM,
57 E_R18_REGNUM,
58 E_R19_REGNUM,
59 E_R20_REGNUM,
60 E_R21_REGNUM,
61 E_R22_REGNUM,
62 E_R23_REGNUM,
63 E_R24_REGNUM,
64 E_R25_REGNUM,
65 E_R26_REGNUM,
66 E_R27_REGNUM,
67 E_R28_REGNUM,
68 E_R29_REGNUM, E_FP_REGNUM = E_R29_REGNUM,
69 E_R30_REGNUM, E_EP_REGNUM = E_R30_REGNUM,
70 E_R31_REGNUM, E_LP_REGNUM = E_R31_REGNUM,
71 E_R32_REGNUM, E_SR0_REGNUM = E_R32_REGNUM,
72 E_R33_REGNUM,
73 E_R34_REGNUM,
74 E_R35_REGNUM,
75 E_R36_REGNUM,
76 E_R37_REGNUM, E_PS_REGNUM = E_R37_REGNUM,
77 E_R38_REGNUM,
78 E_R39_REGNUM,
79 E_R40_REGNUM,
80 E_R41_REGNUM,
81 E_R42_REGNUM,
82 E_R43_REGNUM,
83 E_R44_REGNUM,
84 E_R45_REGNUM,
85 E_R46_REGNUM,
86 E_R47_REGNUM,
87 E_R48_REGNUM,
88 E_R49_REGNUM,
89 E_R50_REGNUM,
90 E_R51_REGNUM,
91 E_R52_REGNUM, E_CTBP_REGNUM = E_R52_REGNUM,
92 E_R53_REGNUM,
93 E_R54_REGNUM,
94 E_R55_REGNUM,
95 E_R56_REGNUM,
96 E_R57_REGNUM,
97 E_R58_REGNUM,
98 E_R59_REGNUM,
99 E_R60_REGNUM,
100 E_R61_REGNUM,
101 E_R62_REGNUM,
102 E_R63_REGNUM,
103 E_R64_REGNUM, E_PC_REGNUM = E_R64_REGNUM,
104 E_R65_REGNUM,
105 E_NUM_OF_V850_REGS,
106 E_NUM_OF_V850E_REGS = E_NUM_OF_V850_REGS,
107
108 /* mpu0 system registers */
109 E_R66_REGNUM = E_NUM_OF_V850_REGS,
110 E_R67_REGNUM,
111 E_R68_REGNUM,
112 E_R69_REGNUM,
113 E_R70_REGNUM,
114 E_R71_REGNUM,
115 E_R72_REGNUM,
116 E_R73_REGNUM,
117 E_R74_REGNUM,
118 E_R75_REGNUM,
119 E_R76_REGNUM,
120 E_R77_REGNUM,
121 E_R78_REGNUM,
122 E_R79_REGNUM,
123 E_R80_REGNUM,
124 E_R81_REGNUM,
125 E_R82_REGNUM,
126 E_R83_REGNUM,
127 E_R84_REGNUM,
128 E_R85_REGNUM,
129 E_R86_REGNUM,
130 E_R87_REGNUM,
131 E_R88_REGNUM,
132 E_R89_REGNUM,
133 E_R90_REGNUM,
134 E_R91_REGNUM,
135 E_R92_REGNUM,
136 E_R93_REGNUM,
137
138 /* mpu1 system registers */
139
140 E_R94_REGNUM,
141 E_R95_REGNUM,
142 E_R96_REGNUM,
143 E_R97_REGNUM,
144 E_R98_REGNUM,
145 E_R99_REGNUM,
146 E_R100_REGNUM,
147 E_R101_REGNUM,
148 E_R102_REGNUM,
149 E_R103_REGNUM,
150 E_R104_REGNUM,
151 E_R105_REGNUM,
152 E_R106_REGNUM,
153 E_R107_REGNUM,
154 E_R108_REGNUM,
155 E_R109_REGNUM,
156 E_R110_REGNUM,
157 E_R111_REGNUM,
158 E_R112_REGNUM,
159 E_R113_REGNUM,
160 E_R114_REGNUM,
161 E_R115_REGNUM,
162 E_R116_REGNUM,
163 E_R117_REGNUM,
164 E_R118_REGNUM,
165 E_R119_REGNUM,
166 E_R120_REGNUM,
167 E_R121_REGNUM,
168
169 /* fpu system registers */
170 E_R122_REGNUM,
171 E_R123_REGNUM,
172 E_R124_REGNUM,
173 E_R125_REGNUM,
174 E_R126_REGNUM,
175 E_R127_REGNUM,
176 E_R128_REGNUM, E_FPSR_REGNUM = E_R128_REGNUM,
177 E_R129_REGNUM, E_FPEPC_REGNUM = E_R129_REGNUM,
178 E_R130_REGNUM, E_FPST_REGNUM = E_R130_REGNUM,
179 E_R131_REGNUM, E_FPCC_REGNUM = E_R131_REGNUM,
180 E_R132_REGNUM, E_FPCFG_REGNUM = E_R132_REGNUM,
181 E_R133_REGNUM,
182 E_R134_REGNUM,
183 E_R135_REGNUM,
184 E_R136_REGNUM,
185 E_R137_REGNUM,
186 E_R138_REGNUM,
187 E_R139_REGNUM,
188 E_R140_REGNUM,
189 E_R141_REGNUM,
190 E_R142_REGNUM,
191 E_R143_REGNUM,
192 E_R144_REGNUM,
193 E_R145_REGNUM,
194 E_R146_REGNUM,
195 E_R147_REGNUM,
196 E_R148_REGNUM,
197 E_NUM_REGS
198 };
199
200 enum
201 {
202 v850_reg_size = 4
203 };
204
205 /* Size of return datatype which fits into all return registers. */
206 enum
207 {
208 E_MAX_RETTYPE_SIZE_IN_REGS = 2 * v850_reg_size
209 };
210
211 struct v850_frame_cache
212 {
213 /* Base address. */
214 CORE_ADDR base;
215 LONGEST sp_offset;
216 CORE_ADDR pc;
217
218 /* Flag showing that a frame has been created in the prologue code. */
219 int uses_fp;
220
221 /* Saved registers. */
222 struct trad_frame_saved_reg *saved_regs;
223 };
224
225 /* Info gleaned from scanning a function's prologue. */
226 struct pifsr /* Info about one saved register. */
227 {
228 int offset; /* Offset from sp or fp. */
229 int cur_frameoffset; /* Current frameoffset. */
230 int reg; /* Saved register number. */
231 };
232
233 static const char *
234 v850_register_name (struct gdbarch *gdbarch, int regnum)
235 {
236 static const char *v850_reg_names[] =
237 { "r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7",
238 "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15",
239 "r16", "r17", "r18", "r19", "r20", "r21", "r22", "r23",
240 "r24", "r25", "r26", "r27", "r28", "r29", "r30", "r31",
241 "eipc", "eipsw", "fepc", "fepsw", "ecr", "psw", "sr6", "sr7",
242 "sr8", "sr9", "sr10", "sr11", "sr12", "sr13", "sr14", "sr15",
243 "sr16", "sr17", "sr18", "sr19", "sr20", "sr21", "sr22", "sr23",
244 "sr24", "sr25", "sr26", "sr27", "sr28", "sr29", "sr30", "sr31",
245 "pc", "fp"
246 };
247 if (regnum < 0 || regnum > E_NUM_OF_V850_REGS)
248 return NULL;
249 return v850_reg_names[regnum];
250 }
251
252 static const char *
253 v850e_register_name (struct gdbarch *gdbarch, int regnum)
254 {
255 static const char *v850e_reg_names[] =
256 {
257 "r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7",
258 "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15",
259 "r16", "r17", "r18", "r19", "r20", "r21", "r22", "r23",
260 "r24", "r25", "r26", "r27", "r28", "r29", "r30", "r31",
261 "eipc", "eipsw", "fepc", "fepsw", "ecr", "psw", "sr6", "sr7",
262 "sr8", "sr9", "sr10", "sr11", "sr12", "sr13", "sr14", "sr15",
263 "ctpc", "ctpsw", "dbpc", "dbpsw", "ctbp", "sr21", "sr22", "sr23",
264 "sr24", "sr25", "sr26", "sr27", "sr28", "sr29", "sr30", "sr31",
265 "pc", "fp"
266 };
267 if (regnum < 0 || regnum > E_NUM_OF_V850E_REGS)
268 return NULL;
269 return v850e_reg_names[regnum];
270 }
271
272 static const char *
273 v850e2_register_name (struct gdbarch *gdbarch, int regnum)
274 {
275 static const char *v850e2_reg_names[] =
276 {
277 "r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7",
278 "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15",
279 "r16", "r17", "r18", "r19", "r20", "r21", "r22", "r23",
280 "r24", "r25", "r26", "r27", "r28", "r29", "r30", "r31",
281
282 "eipc", "eipsw", "fepc", "fepsw", "ecr", "psw", "sr6", "sr7",
283 "sr8", "sr9", "sr10", "sr11", "sr12", "sr13", "sr14", "sr15",
284 "ctpc", "ctpsw", "dbpc", "dbpsw", "ctbp", "sr21", "sr22", "sr23",
285 "sr24", "sr25", "sr26", "sr27", "sr28", "sr29", "sr30", "sr31",
286 "pc", "fp"
287
288 /* mpu0 system registers */
289 "vip", "sr33", "sr34", "sr35", "vmecr", "vmtid", "vmadr", "sr39",
290 "vpecr", "vptid", "vpadr", "sr43", "vdecr", "vdtid", "sr46", "sr47",
291 "sr48", "sr49", "sr50", "sr51", "sr52", "sr53", "sr54", "sr55",
292 "sr56", "sr57", "sr58", "sr59",
293
294 /* mpu1 system registers */
295 "mpm", "mpc", "tid", "ppa", "ppm", "ppc", "dcc", "dcv0",
296 "dcv1", "sr69", "spal", "spau", "ipa0l", "ipa0u", "ipa1l", "ipa1u",
297 "iap2l", "ipa2u", "ipa3l", "ipa3u", "dpa0l", "dpa0u", "dpa1l", "dpa1u",
298 "dpa2l", "dpa2u", "dpa3l", "dpa3u",
299
300 /* fpu system registers */
301 "sr88", "sr89", "sr90", "sr91", "sr92", "sr93", "fpsr", "fpepc",
302 "fpst", "fpcc", "fpcfg", "sr99", "sr100", "sr101", "sr102", "sr103",
303 "sr104", "sr105", "sr106", "sr107", "sr108", "sr109", "sr110", "sr111",
304 "sr112", "sr113", "sr114", "sr115"
305 };
306 if (regnum < 0 || regnum >= E_NUM_REGS)
307 return NULL;
308 return v850e2_reg_names[regnum];
309 }
310
311 /* Returns the default type for register N. */
312
313 static struct type *
314 v850_register_type (struct gdbarch *gdbarch, int regnum)
315 {
316 if (regnum == E_PC_REGNUM)
317 return builtin_type (gdbarch)->builtin_func_ptr;
318 return builtin_type (gdbarch)->builtin_int32;
319 }
320
321 static int
322 v850_type_is_scalar (struct type *t)
323 {
324 return (TYPE_CODE (t) != TYPE_CODE_STRUCT
325 && TYPE_CODE (t) != TYPE_CODE_UNION
326 && TYPE_CODE (t) != TYPE_CODE_ARRAY);
327 }
328
329 /* Should call_function allocate stack space for a struct return? */
330 static int
331 v850_use_struct_convention (struct type *type)
332 {
333 int i;
334 struct type *fld_type, *tgt_type;
335
336 /* 1. The value is greater than 8 bytes -> returned by copying. */
337 if (TYPE_LENGTH (type) > 8)
338 return 1;
339
340 /* 2. The value is a single basic type -> returned in register. */
341 if (v850_type_is_scalar (type))
342 return 0;
343
344 /* The value is a structure or union with a single element and that
345 element is either a single basic type or an array of a single basic
346 type whose size is greater than or equal to 4 -> returned in register. */
347 if ((TYPE_CODE (type) == TYPE_CODE_STRUCT
348 || TYPE_CODE (type) == TYPE_CODE_UNION)
349 && TYPE_NFIELDS (type) == 1)
350 {
351 fld_type = TYPE_FIELD_TYPE (type, 0);
352 if (v850_type_is_scalar (fld_type) && TYPE_LENGTH (fld_type) >= 4)
353 return 0;
354
355 if (TYPE_CODE (fld_type) == TYPE_CODE_ARRAY)
356 {
357 tgt_type = TYPE_TARGET_TYPE (fld_type);
358 if (v850_type_is_scalar (tgt_type) && TYPE_LENGTH (tgt_type) >= 4)
359 return 0;
360 }
361 }
362
363 /* The value is a structure whose first element is an integer or a float,
364 and which contains no arrays of more than two elements -> returned in
365 register. */
366 if (TYPE_CODE (type) == TYPE_CODE_STRUCT
367 && v850_type_is_scalar (TYPE_FIELD_TYPE (type, 0))
368 && TYPE_LENGTH (TYPE_FIELD_TYPE (type, 0)) == 4)
369 {
370 for (i = 1; i < TYPE_NFIELDS (type); ++i)
371 {
372 fld_type = TYPE_FIELD_TYPE (type, 0);
373 if (TYPE_CODE (fld_type) == TYPE_CODE_ARRAY)
374 {
375 tgt_type = TYPE_TARGET_TYPE (fld_type);
376 if (TYPE_LENGTH (fld_type) >= 0 && TYPE_LENGTH (tgt_type) >= 0
377 && TYPE_LENGTH (fld_type) / TYPE_LENGTH (tgt_type) > 2)
378 return 1;
379 }
380 }
381 return 0;
382 }
383
384 /* The value is a union which contains at least one field which
385 would be returned in registers according to these rules ->
386 returned in register. */
387 if (TYPE_CODE (type) == TYPE_CODE_UNION)
388 {
389 for (i = 0; i < TYPE_NFIELDS (type); ++i)
390 {
391 fld_type = TYPE_FIELD_TYPE (type, 0);
392 if (!v850_use_struct_convention (fld_type))
393 return 0;
394 }
395 }
396
397 return 1;
398 }
399
400 /* Structure for mapping bits in register lists to register numbers. */
401 struct reg_list
402 {
403 long mask;
404 int regno;
405 };
406
407 /* Helper function for v850_scan_prologue to handle prepare instruction. */
408
409 static void
410 v850_handle_prepare (int insn, int insn2, CORE_ADDR * current_pc_ptr,
411 struct v850_frame_cache *pi, struct pifsr **pifsr_ptr)
412 {
413 CORE_ADDR current_pc = *current_pc_ptr;
414 struct pifsr *pifsr = *pifsr_ptr;
415 long next = insn2 & 0xffff;
416 long list12 = ((insn & 1) << 16) + (next & 0xffe0);
417 long offset = (insn & 0x3e) << 1;
418 static struct reg_list reg_table[] =
419 {
420 {0x00800, 20}, /* r20 */
421 {0x00400, 21}, /* r21 */
422 {0x00200, 22}, /* r22 */
423 {0x00100, 23}, /* r23 */
424 {0x08000, 24}, /* r24 */
425 {0x04000, 25}, /* r25 */
426 {0x02000, 26}, /* r26 */
427 {0x01000, 27}, /* r27 */
428 {0x00080, 28}, /* r28 */
429 {0x00040, 29}, /* r29 */
430 {0x10000, 30}, /* ep */
431 {0x00020, 31}, /* lp */
432 {0, 0} /* end of table */
433 };
434 int i;
435
436 if ((next & 0x1f) == 0x0b) /* skip imm16 argument */
437 current_pc += 2;
438 else if ((next & 0x1f) == 0x13) /* skip imm16 argument */
439 current_pc += 2;
440 else if ((next & 0x1f) == 0x1b) /* skip imm32 argument */
441 current_pc += 4;
442
443 /* Calculate the total size of the saved registers, and add it to the
444 immediate value used to adjust SP. */
445 for (i = 0; reg_table[i].mask != 0; i++)
446 if (list12 & reg_table[i].mask)
447 offset += v850_reg_size;
448 pi->sp_offset -= offset;
449
450 /* Calculate the offsets of the registers relative to the value the SP
451 will have after the registers have been pushed and the imm5 value has
452 been subtracted from it. */
453 if (pifsr)
454 {
455 for (i = 0; reg_table[i].mask != 0; i++)
456 {
457 if (list12 & reg_table[i].mask)
458 {
459 int reg = reg_table[i].regno;
460 offset -= v850_reg_size;
461 pifsr->reg = reg;
462 pifsr->offset = offset;
463 pifsr->cur_frameoffset = pi->sp_offset;
464 pifsr++;
465 }
466 }
467 }
468
469 /* Set result parameters. */
470 *current_pc_ptr = current_pc;
471 *pifsr_ptr = pifsr;
472 }
473
474
475 /* Helper function for v850_scan_prologue to handle pushm/pushl instructions.
476 The SR bit of the register list is not supported. gcc does not generate
477 this bit. */
478
479 static void
480 v850_handle_pushm (int insn, int insn2, struct v850_frame_cache *pi,
481 struct pifsr **pifsr_ptr)
482 {
483 struct pifsr *pifsr = *pifsr_ptr;
484 long list12 = ((insn & 0x0f) << 16) + (insn2 & 0xfff0);
485 long offset = 0;
486 static struct reg_list pushml_reg_table[] =
487 {
488 {0x80000, E_PS_REGNUM}, /* PSW */
489 {0x40000, 1}, /* r1 */
490 {0x20000, 2}, /* r2 */
491 {0x10000, 3}, /* r3 */
492 {0x00800, 4}, /* r4 */
493 {0x00400, 5}, /* r5 */
494 {0x00200, 6}, /* r6 */
495 {0x00100, 7}, /* r7 */
496 {0x08000, 8}, /* r8 */
497 {0x04000, 9}, /* r9 */
498 {0x02000, 10}, /* r10 */
499 {0x01000, 11}, /* r11 */
500 {0x00080, 12}, /* r12 */
501 {0x00040, 13}, /* r13 */
502 {0x00020, 14}, /* r14 */
503 {0x00010, 15}, /* r15 */
504 {0, 0} /* end of table */
505 };
506 static struct reg_list pushmh_reg_table[] =
507 {
508 {0x80000, 16}, /* r16 */
509 {0x40000, 17}, /* r17 */
510 {0x20000, 18}, /* r18 */
511 {0x10000, 19}, /* r19 */
512 {0x00800, 20}, /* r20 */
513 {0x00400, 21}, /* r21 */
514 {0x00200, 22}, /* r22 */
515 {0x00100, 23}, /* r23 */
516 {0x08000, 24}, /* r24 */
517 {0x04000, 25}, /* r25 */
518 {0x02000, 26}, /* r26 */
519 {0x01000, 27}, /* r27 */
520 {0x00080, 28}, /* r28 */
521 {0x00040, 29}, /* r29 */
522 {0x00010, 30}, /* r30 */
523 {0x00020, 31}, /* r31 */
524 {0, 0} /* end of table */
525 };
526 struct reg_list *reg_table;
527 int i;
528
529 /* Is this a pushml or a pushmh? */
530 if ((insn2 & 7) == 1)
531 reg_table = pushml_reg_table;
532 else
533 reg_table = pushmh_reg_table;
534
535 /* Calculate the total size of the saved registers, and add it to the
536 immediate value used to adjust SP. */
537 for (i = 0; reg_table[i].mask != 0; i++)
538 if (list12 & reg_table[i].mask)
539 offset += v850_reg_size;
540 pi->sp_offset -= offset;
541
542 /* Calculate the offsets of the registers relative to the value the SP
543 will have after the registers have been pushed and the imm5 value is
544 subtracted from it. */
545 if (pifsr)
546 {
547 for (i = 0; reg_table[i].mask != 0; i++)
548 {
549 if (list12 & reg_table[i].mask)
550 {
551 int reg = reg_table[i].regno;
552 offset -= v850_reg_size;
553 pifsr->reg = reg;
554 pifsr->offset = offset;
555 pifsr->cur_frameoffset = pi->sp_offset;
556 pifsr++;
557 }
558 }
559 }
560
561 /* Set result parameters. */
562 *pifsr_ptr = pifsr;
563 }
564
565 /* Helper function to evaluate if register is one of the "save" registers.
566 This allows to simplify conditionals in v850_analyze_prologue a lot. */
567
568 static int
569 v850_is_save_register (int reg)
570 {
571 /* The caller-save registers are R2, R20 - R29 and R31. All other
572 registers are either special purpose (PC, SP), argument registers,
573 or just considered free for use in the caller. */
574 return reg == E_R2_REGNUM
575 || (reg >= E_R20_REGNUM && reg <= E_R29_REGNUM)
576 || reg == E_R31_REGNUM;
577 }
578
579 /* Scan the prologue of the function that contains PC, and record what
580 we find in PI. Returns the pc after the prologue. Note that the
581 addresses saved in frame->saved_regs are just frame relative (negative
582 offsets from the frame pointer). This is because we don't know the
583 actual value of the frame pointer yet. In some circumstances, the
584 frame pointer can't be determined till after we have scanned the
585 prologue. */
586
587 static CORE_ADDR
588 v850_analyze_prologue (struct gdbarch *gdbarch,
589 CORE_ADDR func_addr, CORE_ADDR pc,
590 struct v850_frame_cache *pi, ULONGEST ctbp)
591 {
592 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
593 CORE_ADDR prologue_end, current_pc;
594 struct pifsr pifsrs[E_NUM_REGS + 1];
595 struct pifsr *pifsr, *pifsr_tmp;
596 int fp_used;
597 int ep_used;
598 int reg;
599 CORE_ADDR save_pc, save_end;
600 int regsave_func_p;
601 int r12_tmp;
602
603 memset (&pifsrs, 0, sizeof pifsrs);
604 pifsr = &pifsrs[0];
605
606 prologue_end = pc;
607
608 /* Now, search the prologue looking for instructions that setup fp, save
609 rp, adjust sp and such. We also record the frame offset of any saved
610 registers. */
611
612 pi->sp_offset = 0;
613 pi->uses_fp = 0;
614 ep_used = 0;
615 regsave_func_p = 0;
616 save_pc = 0;
617 save_end = 0;
618 r12_tmp = 0;
619
620 for (current_pc = func_addr; current_pc < prologue_end;)
621 {
622 int insn;
623 int insn2 = -1; /* dummy value */
624
625 insn = read_memory_integer (current_pc, 2, byte_order);
626 current_pc += 2;
627 if ((insn & 0x0780) >= 0x0600) /* Four byte instruction? */
628 {
629 insn2 = read_memory_integer (current_pc, 2, byte_order);
630 current_pc += 2;
631 }
632
633 if ((insn & 0xffc0) == ((10 << 11) | 0x0780) && !regsave_func_p)
634 { /* jarl <func>,10 */
635 long low_disp = insn2 & ~(long) 1;
636 long disp = (((((insn & 0x3f) << 16) + low_disp)
637 & ~(long) 1) ^ 0x00200000) - 0x00200000;
638
639 save_pc = current_pc;
640 save_end = prologue_end;
641 regsave_func_p = 1;
642 current_pc += disp - 4;
643 prologue_end = (current_pc
644 + (2 * 3) /* moves to/from ep */
645 + 4 /* addi <const>,sp,sp */
646 + 2 /* jmp [r10] */
647 + (2 * 12) /* sst.w to save r2, r20-r29, r31 */
648 + 20); /* slop area */
649 }
650 else if ((insn & 0xffc0) == 0x0200 && !regsave_func_p)
651 { /* callt <imm6> */
652 long adr = ctbp + ((insn & 0x3f) << 1);
653
654 save_pc = current_pc;
655 save_end = prologue_end;
656 regsave_func_p = 1;
657 current_pc = ctbp + (read_memory_unsigned_integer (adr, 2, byte_order)
658 & 0xffff);
659 prologue_end = (current_pc
660 + (2 * 3) /* prepare list2,imm5,sp/imm */
661 + 4 /* ctret */
662 + 20); /* slop area */
663 continue;
664 }
665 else if ((insn & 0xffc0) == 0x0780) /* prepare list2,imm5 */
666 {
667 v850_handle_prepare (insn, insn2, &current_pc, pi, &pifsr);
668 continue;
669 }
670 else if (insn == 0x07e0 && regsave_func_p && insn2 == 0x0144)
671 { /* ctret after processing register save. */
672 current_pc = save_pc;
673 prologue_end = save_end;
674 regsave_func_p = 0;
675 continue;
676 }
677 else if ((insn & 0xfff0) == 0x07e0 && (insn2 & 5) == 1)
678 { /* pushml, pushmh */
679 v850_handle_pushm (insn, insn2, pi, &pifsr);
680 continue;
681 }
682 else if ((insn & 0xffe0) == 0x0060 && regsave_func_p)
683 { /* jmp after processing register save. */
684 current_pc = save_pc;
685 prologue_end = save_end;
686 regsave_func_p = 0;
687 continue;
688 }
689 else if ((insn & 0x07c0) == 0x0780 /* jarl or jr */
690 || (insn & 0xffe0) == 0x0060 /* jmp */
691 || (insn & 0x0780) == 0x0580) /* branch */
692 {
693 break; /* Ran into end of prologue. */
694 }
695
696 else if ((insn & 0xffe0) == ((E_SP_REGNUM << 11) | 0x0240))
697 /* add <imm>,sp */
698 pi->sp_offset += ((insn & 0x1f) ^ 0x10) - 0x10;
699 else if (insn == ((E_SP_REGNUM << 11) | 0x0600 | E_SP_REGNUM))
700 /* addi <imm>,sp,sp */
701 pi->sp_offset += insn2;
702 else if (insn == ((E_FP_REGNUM << 11) | 0x0000 | E_SP_REGNUM))
703 /* mov sp,fp */
704 pi->uses_fp = 1;
705 else if (insn == ((E_R12_REGNUM << 11) | 0x0640 | E_R0_REGNUM))
706 /* movhi hi(const),r0,r12 */
707 r12_tmp = insn2 << 16;
708 else if (insn == ((E_R12_REGNUM << 11) | 0x0620 | E_R12_REGNUM))
709 /* movea lo(const),r12,r12 */
710 r12_tmp += insn2;
711 else if (insn == ((E_SP_REGNUM << 11) | 0x01c0 | E_R12_REGNUM) && r12_tmp)
712 /* add r12,sp */
713 pi->sp_offset += r12_tmp;
714 else if (insn == ((E_EP_REGNUM << 11) | 0x0000 | E_SP_REGNUM))
715 /* mov sp,ep */
716 ep_used = 1;
717 else if (insn == ((E_EP_REGNUM << 11) | 0x0000 | E_R1_REGNUM))
718 /* mov r1,ep */
719 ep_used = 0;
720 else if (((insn & 0x07ff) == (0x0760 | E_SP_REGNUM)
721 || (pi->uses_fp
722 && (insn & 0x07ff) == (0x0760 | E_FP_REGNUM)))
723 && pifsr
724 && v850_is_save_register (reg = (insn >> 11) & 0x1f))
725 {
726 /* st.w <reg>,<offset>[sp] or st.w <reg>,<offset>[fp] */
727 pifsr->reg = reg;
728 pifsr->offset = insn2 & ~1;
729 pifsr->cur_frameoffset = pi->sp_offset;
730 pifsr++;
731 }
732 else if (ep_used
733 && ((insn & 0x0781) == 0x0501)
734 && pifsr
735 && v850_is_save_register (reg = (insn >> 11) & 0x1f))
736 {
737 /* sst.w <reg>,<offset>[ep] */
738 pifsr->reg = reg;
739 pifsr->offset = (insn & 0x007e) << 1;
740 pifsr->cur_frameoffset = pi->sp_offset;
741 pifsr++;
742 }
743 }
744
745 /* Fix up any offsets to the final offset. If a frame pointer was created,
746 use it instead of the stack pointer. */
747 for (pifsr_tmp = pifsrs; pifsr_tmp != pifsr; pifsr_tmp++)
748 {
749 pifsr_tmp->offset -= pi->sp_offset - pifsr_tmp->cur_frameoffset;
750 pi->saved_regs[pifsr_tmp->reg].addr = pifsr_tmp->offset;
751 }
752
753 return current_pc;
754 }
755
756 /* Return the address of the first code past the prologue of the function. */
757
758 static CORE_ADDR
759 v850_skip_prologue (struct gdbarch *gdbarch, CORE_ADDR pc)
760 {
761 CORE_ADDR func_addr, func_end;
762
763 /* See what the symbol table says. */
764
765 if (find_pc_partial_function (pc, NULL, &func_addr, &func_end))
766 {
767 struct symtab_and_line sal;
768
769 sal = find_pc_line (func_addr, 0);
770 if (sal.line != 0 && sal.end < func_end)
771 return sal.end;
772
773 /* Either there's no line info, or the line after the prologue is after
774 the end of the function. In this case, there probably isn't a
775 prologue. */
776 return pc;
777 }
778
779 /* We can't find the start of this function, so there's nothing we
780 can do. */
781 return pc;
782 }
783
784 static CORE_ADDR
785 v850_frame_align (struct gdbarch *ignore, CORE_ADDR sp)
786 {
787 return sp & ~3;
788 }
789
790 /* Setup arguments and LP for a call to the target. First four args
791 go in R6->R9, subsequent args go into sp + 16 -> sp + ... Structs
792 are passed by reference. 64 bit quantities (doubles and long longs)
793 may be split between the regs and the stack. When calling a function
794 that returns a struct, a pointer to the struct is passed in as a secret
795 first argument (always in R6).
796
797 Stack space for the args has NOT been allocated: that job is up to us. */
798
799 static CORE_ADDR
800 v850_push_dummy_call (struct gdbarch *gdbarch,
801 struct value *function,
802 struct regcache *regcache,
803 CORE_ADDR bp_addr,
804 int nargs,
805 struct value **args,
806 CORE_ADDR sp,
807 int struct_return,
808 CORE_ADDR struct_addr)
809 {
810 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
811 int argreg;
812 int argnum;
813 int len = 0;
814 int stack_offset;
815
816 /* The offset onto the stack at which we will start copying parameters
817 (after the registers are used up) begins at 16 rather than at zero.
818 That's how the ABI is defined, though there's no indication that these
819 16 bytes are used for anything, not even for saving incoming
820 argument registers. */
821 stack_offset = 16;
822
823 /* Now make space on the stack for the args. */
824 for (argnum = 0; argnum < nargs; argnum++)
825 len += ((TYPE_LENGTH (value_type (args[argnum])) + 3) & ~3);
826 sp -= len + stack_offset;
827
828 argreg = E_ARG0_REGNUM;
829 /* The struct_return pointer occupies the first parameter register. */
830 if (struct_return)
831 regcache_cooked_write_unsigned (regcache, argreg++, struct_addr);
832
833 /* Now load as many as possible of the first arguments into
834 registers, and push the rest onto the stack. There are 16 bytes
835 in four registers available. Loop thru args from first to last. */
836 for (argnum = 0; argnum < nargs; argnum++)
837 {
838 int len;
839 gdb_byte *val;
840 gdb_byte valbuf[v850_reg_size];
841
842 if (!v850_type_is_scalar (value_type (*args))
843 && TYPE_LENGTH (value_type (*args)) > E_MAX_RETTYPE_SIZE_IN_REGS)
844 {
845 store_unsigned_integer (valbuf, 4, byte_order,
846 value_address (*args));
847 len = 4;
848 val = valbuf;
849 }
850 else
851 {
852 len = TYPE_LENGTH (value_type (*args));
853 val = (gdb_byte *) value_contents (*args);
854 }
855
856 while (len > 0)
857 if (argreg <= E_ARGLAST_REGNUM)
858 {
859 CORE_ADDR regval;
860
861 regval = extract_unsigned_integer (val, v850_reg_size, byte_order);
862 regcache_cooked_write_unsigned (regcache, argreg, regval);
863
864 len -= v850_reg_size;
865 val += v850_reg_size;
866 argreg++;
867 }
868 else
869 {
870 write_memory (sp + stack_offset, val, 4);
871
872 len -= 4;
873 val += 4;
874 stack_offset += 4;
875 }
876 args++;
877 }
878
879 /* Store return address. */
880 regcache_cooked_write_unsigned (regcache, E_LP_REGNUM, bp_addr);
881
882 /* Update stack pointer. */
883 regcache_cooked_write_unsigned (regcache, E_SP_REGNUM, sp);
884
885 return sp;
886 }
887
888 static void
889 v850_extract_return_value (struct type *type, struct regcache *regcache,
890 gdb_byte *valbuf)
891 {
892 struct gdbarch *gdbarch = get_regcache_arch (regcache);
893 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
894 int len = TYPE_LENGTH (type);
895
896 if (len <= v850_reg_size)
897 {
898 ULONGEST val;
899
900 regcache_cooked_read_unsigned (regcache, E_V0_REGNUM, &val);
901 store_unsigned_integer (valbuf, len, byte_order, val);
902 }
903 else if (len <= 2 * v850_reg_size)
904 {
905 int i, regnum = E_V0_REGNUM;
906 gdb_byte buf[v850_reg_size];
907 for (i = 0; len > 0; i += 4, len -= 4)
908 {
909 regcache_raw_read (regcache, regnum++, buf);
910 memcpy (valbuf + i, buf, len > 4 ? 4 : len);
911 }
912 }
913 }
914
915 static void
916 v850_store_return_value (struct type *type, struct regcache *regcache,
917 const gdb_byte *valbuf)
918 {
919 struct gdbarch *gdbarch = get_regcache_arch (regcache);
920 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
921 int len = TYPE_LENGTH (type);
922
923 if (len <= v850_reg_size)
924 regcache_cooked_write_unsigned
925 (regcache, E_V0_REGNUM,
926 extract_unsigned_integer (valbuf, len, byte_order));
927 else if (len <= 2 * v850_reg_size)
928 {
929 int i, regnum = E_V0_REGNUM;
930 for (i = 0; i < len; i += 4)
931 regcache_raw_write (regcache, regnum++, valbuf + i);
932 }
933 }
934
935 static enum return_value_convention
936 v850_return_value (struct gdbarch *gdbarch, struct type *func_type,
937 struct type *type, struct regcache *regcache,
938 gdb_byte *readbuf, const gdb_byte *writebuf)
939 {
940 if (v850_use_struct_convention (type))
941 return RETURN_VALUE_STRUCT_CONVENTION;
942 if (writebuf)
943 v850_store_return_value (type, regcache, writebuf);
944 else if (readbuf)
945 v850_extract_return_value (type, regcache, readbuf);
946 return RETURN_VALUE_REGISTER_CONVENTION;
947 }
948
949 const static unsigned char *
950 v850_breakpoint_from_pc (struct gdbarch *gdbarch, CORE_ADDR *pcptr, int *lenptr)
951 {
952 static unsigned char breakpoint[] = { 0x85, 0x05 };
953 *lenptr = sizeof (breakpoint);
954 return breakpoint;
955 }
956
957 static struct v850_frame_cache *
958 v850_alloc_frame_cache (struct frame_info *this_frame)
959 {
960 struct v850_frame_cache *cache;
961 int i;
962
963 cache = FRAME_OBSTACK_ZALLOC (struct v850_frame_cache);
964 cache->saved_regs = trad_frame_alloc_saved_regs (this_frame);
965
966 /* Base address. */
967 cache->base = 0;
968 cache->sp_offset = 0;
969 cache->pc = 0;
970
971 /* Frameless until proven otherwise. */
972 cache->uses_fp = 0;
973
974 return cache;
975 }
976
977 static struct v850_frame_cache *
978 v850_frame_cache (struct frame_info *this_frame, void **this_cache)
979 {
980 struct gdbarch *gdbarch = get_frame_arch (this_frame);
981 struct v850_frame_cache *cache;
982 CORE_ADDR current_pc;
983 int i;
984
985 if (*this_cache)
986 return *this_cache;
987
988 cache = v850_alloc_frame_cache (this_frame);
989 *this_cache = cache;
990
991 /* In principle, for normal frames, fp holds the frame pointer,
992 which holds the base address for the current stack frame.
993 However, for functions that don't need it, the frame pointer is
994 optional. For these "frameless" functions the frame pointer is
995 actually the frame pointer of the calling frame. */
996 cache->base = get_frame_register_unsigned (this_frame, E_FP_REGNUM);
997 if (cache->base == 0)
998 return cache;
999
1000 cache->pc = get_frame_func (this_frame);
1001 current_pc = get_frame_pc (this_frame);
1002 if (cache->pc != 0)
1003 {
1004 ULONGEST ctbp;
1005 ctbp = get_frame_register_unsigned (this_frame, E_CTBP_REGNUM);
1006 v850_analyze_prologue (gdbarch, cache->pc, current_pc, cache, ctbp);
1007 }
1008
1009 if (!cache->uses_fp)
1010 {
1011 /* We didn't find a valid frame, which means that CACHE->base
1012 currently holds the frame pointer for our calling frame. If
1013 we're at the start of a function, or somewhere half-way its
1014 prologue, the function's frame probably hasn't been fully
1015 setup yet. Try to reconstruct the base address for the stack
1016 frame by looking at the stack pointer. For truly "frameless"
1017 functions this might work too. */
1018 cache->base = get_frame_register_unsigned (this_frame, E_SP_REGNUM);
1019 }
1020
1021 /* Now that we have the base address for the stack frame we can
1022 calculate the value of sp in the calling frame. */
1023 trad_frame_set_value (cache->saved_regs, E_SP_REGNUM,
1024 cache->base - cache->sp_offset);
1025
1026 /* Adjust all the saved registers such that they contain addresses
1027 instead of offsets. */
1028 for (i = 0; i < gdbarch_num_regs (gdbarch); i++)
1029 if (trad_frame_addr_p (cache->saved_regs, i))
1030 cache->saved_regs[i].addr += cache->base;
1031
1032 /* The call instruction moves the caller's PC in the callee's LP.
1033 Since this is an unwind, do the reverse. Copy the location of LP
1034 into PC (the address / regnum) so that a request for PC will be
1035 converted into a request for the LP. */
1036
1037 cache->saved_regs[E_PC_REGNUM] = cache->saved_regs[E_LP_REGNUM];
1038
1039 return cache;
1040 }
1041
1042
1043 static struct value *
1044 v850_frame_prev_register (struct frame_info *this_frame,
1045 void **this_cache, int regnum)
1046 {
1047 struct v850_frame_cache *cache = v850_frame_cache (this_frame, this_cache);
1048
1049 gdb_assert (regnum >= 0);
1050
1051 return trad_frame_get_prev_register (this_frame, cache->saved_regs, regnum);
1052 }
1053
1054 static void
1055 v850_frame_this_id (struct frame_info *this_frame, void **this_cache,
1056 struct frame_id *this_id)
1057 {
1058 struct v850_frame_cache *cache = v850_frame_cache (this_frame, this_cache);
1059
1060 /* This marks the outermost frame. */
1061 if (cache->base == 0)
1062 return;
1063
1064 *this_id = frame_id_build (cache->saved_regs[E_SP_REGNUM].addr, cache->pc);
1065 }
1066
1067 static const struct frame_unwind v850_frame_unwind = {
1068 NORMAL_FRAME,
1069 default_frame_unwind_stop_reason,
1070 v850_frame_this_id,
1071 v850_frame_prev_register,
1072 NULL,
1073 default_frame_sniffer
1074 };
1075
1076 static CORE_ADDR
1077 v850_unwind_sp (struct gdbarch *gdbarch, struct frame_info *next_frame)
1078 {
1079 return frame_unwind_register_unsigned (next_frame,
1080 gdbarch_sp_regnum (gdbarch));
1081 }
1082
1083 static CORE_ADDR
1084 v850_unwind_pc (struct gdbarch *gdbarch, struct frame_info *next_frame)
1085 {
1086 return frame_unwind_register_unsigned (next_frame,
1087 gdbarch_pc_regnum (gdbarch));
1088 }
1089
1090 static struct frame_id
1091 v850_dummy_id (struct gdbarch *gdbarch, struct frame_info *this_frame)
1092 {
1093 CORE_ADDR sp = get_frame_register_unsigned (this_frame,
1094 gdbarch_sp_regnum (gdbarch));
1095 return frame_id_build (sp, get_frame_pc (this_frame));
1096 }
1097
1098 static CORE_ADDR
1099 v850_frame_base_address (struct frame_info *this_frame, void **this_cache)
1100 {
1101 struct v850_frame_cache *cache = v850_frame_cache (this_frame, this_cache);
1102
1103 return cache->base;
1104 }
1105
1106 static const struct frame_base v850_frame_base = {
1107 &v850_frame_unwind,
1108 v850_frame_base_address,
1109 v850_frame_base_address,
1110 v850_frame_base_address
1111 };
1112
1113 static struct gdbarch *
1114 v850_gdbarch_init (struct gdbarch_info info, struct gdbarch_list *arches)
1115 {
1116 struct gdbarch *gdbarch;
1117
1118 /* Change the register names based on the current machine type. */
1119 if (info.bfd_arch_info->arch != bfd_arch_v850)
1120 return NULL;
1121
1122 gdbarch = gdbarch_alloc (&info, NULL);
1123
1124 switch (info.bfd_arch_info->mach)
1125 {
1126 case bfd_mach_v850:
1127 set_gdbarch_register_name (gdbarch, v850_register_name);
1128 set_gdbarch_num_regs (gdbarch, E_NUM_OF_V850_REGS);
1129 break;
1130 case bfd_mach_v850e:
1131 case bfd_mach_v850e1:
1132 set_gdbarch_register_name (gdbarch, v850e_register_name);
1133 set_gdbarch_num_regs (gdbarch, E_NUM_OF_V850E_REGS);
1134 break;
1135 case bfd_mach_v850e2:
1136 case bfd_mach_v850e2v3:
1137 set_gdbarch_register_name (gdbarch, v850e2_register_name);
1138 set_gdbarch_num_regs (gdbarch, E_NUM_REGS);
1139 break;
1140 }
1141
1142 set_gdbarch_num_pseudo_regs (gdbarch, 0);
1143 set_gdbarch_sp_regnum (gdbarch, E_SP_REGNUM);
1144 set_gdbarch_pc_regnum (gdbarch, E_PC_REGNUM);
1145 set_gdbarch_fp0_regnum (gdbarch, -1);
1146
1147 set_gdbarch_register_type (gdbarch, v850_register_type);
1148
1149 set_gdbarch_char_signed (gdbarch, 1);
1150 set_gdbarch_short_bit (gdbarch, 2 * TARGET_CHAR_BIT);
1151 set_gdbarch_int_bit (gdbarch, 4 * TARGET_CHAR_BIT);
1152 set_gdbarch_long_bit (gdbarch, 4 * TARGET_CHAR_BIT);
1153 set_gdbarch_long_long_bit (gdbarch, 8 * TARGET_CHAR_BIT);
1154
1155 set_gdbarch_float_bit (gdbarch, 4 * TARGET_CHAR_BIT);
1156 set_gdbarch_double_bit (gdbarch, 8 * TARGET_CHAR_BIT);
1157 set_gdbarch_long_double_bit (gdbarch, 8 * TARGET_CHAR_BIT);
1158
1159 set_gdbarch_ptr_bit (gdbarch, 4 * TARGET_CHAR_BIT);
1160 set_gdbarch_addr_bit (gdbarch, 4 * TARGET_CHAR_BIT);
1161
1162 set_gdbarch_inner_than (gdbarch, core_addr_lessthan);
1163 set_gdbarch_breakpoint_from_pc (gdbarch, v850_breakpoint_from_pc);
1164
1165 set_gdbarch_return_value (gdbarch, v850_return_value);
1166 set_gdbarch_push_dummy_call (gdbarch, v850_push_dummy_call);
1167 set_gdbarch_skip_prologue (gdbarch, v850_skip_prologue);
1168
1169 set_gdbarch_print_insn (gdbarch, print_insn_v850);
1170
1171 set_gdbarch_frame_align (gdbarch, v850_frame_align);
1172 set_gdbarch_unwind_sp (gdbarch, v850_unwind_sp);
1173 set_gdbarch_unwind_pc (gdbarch, v850_unwind_pc);
1174 set_gdbarch_dummy_id (gdbarch, v850_dummy_id);
1175 frame_base_set_default (gdbarch, &v850_frame_base);
1176
1177 /* Hook in ABI-specific overrides, if they have been registered. */
1178 gdbarch_init_osabi (info, gdbarch);
1179
1180 dwarf2_append_unwinders (gdbarch);
1181 frame_unwind_append_unwinder (gdbarch, &v850_frame_unwind);
1182
1183 return gdbarch;
1184 }
1185
1186 extern initialize_file_ftype _initialize_v850_tdep; /* -Wmissing-prototypes */
1187
1188 void
1189 _initialize_v850_tdep (void)
1190 {
1191 register_gdbarch_init (bfd_arch_v850, v850_gdbarch_init);
1192 }