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