Update/correct copyright notices.
[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 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
34
35 static char *v850_generic_reg_names[] = REGISTER_NAMES;
36
37 static char *v850e_reg_names[] =
38 {
39   "r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7",
40   "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15",
41   "r16", "r17", "r18", "r19", "r20", "r21", "r22", "r23",
42   "r24", "r25", "r26", "r27", "r28", "r29", "r30", "r31",
43   "eipc", "eipsw", "fepc", "fepsw", "ecr", "psw", "sr6", "sr7",
44   "sr8", "sr9", "sr10", "sr11", "sr12", "sr13", "sr14", "sr15",
45   "ctpc", "ctpsw", "dbpc", "dbpsw", "ctbp", "sr21", "sr22", "sr23",
46   "sr24", "sr25", "sr26", "sr27", "sr28", "sr29", "sr30", "sr31",
47   "pc", "fp"
48 };
49
50 char **v850_register_names = v850_generic_reg_names;
51
52 struct
53   {
54     char **regnames;
55     int mach;
56   }
57 v850_processor_type_table[] =
58 {
59   {
60     v850_generic_reg_names, bfd_mach_v850
61   }
62   ,
63   {
64     v850e_reg_names, bfd_mach_v850e
65   }
66   ,
67   {
68     v850e_reg_names, bfd_mach_v850ea
69   }
70   ,
71   {
72     NULL, 0
73   }
74 };
75
76 /* Info gleaned from scanning a function's prologue.  */
77
78 struct pifsr                    /* Info about one saved reg */
79   {
80     int framereg;               /* Frame reg (SP or FP) */
81     int offset;                 /* Offset from framereg */
82     int cur_frameoffset;        /* Current frameoffset */
83     int reg;                    /* Saved register number */
84   };
85
86 struct prologue_info
87   {
88     int framereg;
89     int frameoffset;
90     int start_function;
91     struct pifsr *pifsrs;
92   };
93
94 static CORE_ADDR v850_scan_prologue (CORE_ADDR pc, struct prologue_info *fs);
95
96
97 /* Should call_function allocate stack space for a struct return?  */
98 int
99 v850_use_struct_convention (int gcc_p, struct type *type)
100 {
101   return (TYPE_NFIELDS (type) > 1 || TYPE_LENGTH (type) > 4);
102 }
103 \f
104
105
106 /* Structure for mapping bits in register lists to register numbers. */
107 struct reg_list
108 {
109   long mask;
110   int regno;
111 };
112
113 /* Helper function for v850_scan_prologue to handle prepare instruction. */
114
115 static void
116 handle_prepare (int insn, int insn2, CORE_ADDR * current_pc_ptr,
117                 struct prologue_info *pi, struct pifsr **pifsr_ptr)
118 {
119   CORE_ADDR current_pc = *current_pc_ptr;
120   struct pifsr *pifsr = *pifsr_ptr;
121   long next = insn2 & 0xffff;
122   long list12 = ((insn & 1) << 16) + (next & 0xffe0);
123   long offset = (insn & 0x3e) << 1;
124   static struct reg_list reg_table[] =
125   {
126     {0x00800, 20},              /* r20 */
127     {0x00400, 21},              /* r21 */
128     {0x00200, 22},              /* r22 */
129     {0x00100, 23},              /* r23 */
130     {0x08000, 24},              /* r24 */
131     {0x04000, 25},              /* r25 */
132     {0x02000, 26},              /* r26 */
133     {0x01000, 27},              /* r27 */
134     {0x00080, 28},              /* r28 */
135     {0x00040, 29},              /* r29 */
136     {0x10000, 30},              /* ep */
137     {0x00020, 31},              /* lp */
138     {0, 0}                      /* end of table */
139   };
140   int i;
141
142   if ((next & 0x1f) == 0x0b)    /* skip imm16 argument */
143     current_pc += 2;
144   else if ((next & 0x1f) == 0x13)       /* skip imm16 argument */
145     current_pc += 2;
146   else if ((next & 0x1f) == 0x1b)       /* skip imm32 argument */
147     current_pc += 4;
148
149   /* Calculate the total size of the saved registers, and add it
150      it to the immediate value used to adjust SP. */
151   for (i = 0; reg_table[i].mask != 0; i++)
152     if (list12 & reg_table[i].mask)
153       offset += REGISTER_RAW_SIZE (regtable[i].regno);
154   pi->frameoffset -= offset;
155
156   /* Calculate the offsets of the registers relative to the value
157      the SP will have after the registers have been pushed and the
158      imm5 value has been subtracted from it. */
159   if (pifsr)
160     {
161       for (i = 0; reg_table[i].mask != 0; i++)
162         {
163           if (list12 & reg_table[i].mask)
164             {
165               int reg = reg_table[i].regno;
166               offset -= REGISTER_RAW_SIZE (reg);
167               pifsr->reg = reg;
168               pifsr->offset = offset;
169               pifsr->cur_frameoffset = pi->frameoffset;
170 #ifdef DEBUG
171               printf_filtered ("\tSaved register r%d, offset %d", reg, pifsr->offset);
172 #endif
173               pifsr++;
174             }
175         }
176     }
177 #ifdef DEBUG
178   printf_filtered ("\tfound ctret after regsave func");
179 #endif
180
181   /* Set result parameters. */
182   *current_pc_ptr = current_pc;
183   *pifsr_ptr = pifsr;
184 }
185
186
187 /* Helper function for v850_scan_prologue to handle pushm/pushl instructions.
188    FIXME: the SR bit of the register list is not supported; must check
189    that the compiler does not ever generate this bit. */
190
191 static void
192 handle_pushm (int insn, int insn2, struct prologue_info *pi,
193               struct pifsr **pifsr_ptr)
194 {
195   struct pifsr *pifsr = *pifsr_ptr;
196   long list12 = ((insn & 0x0f) << 16) + (insn2 & 0xfff0);
197   long offset = 0;
198   static struct reg_list pushml_reg_table[] =
199   {
200     {0x80000, PS_REGNUM},       /* PSW */
201     {0x40000, 1},               /* r1 */
202     {0x20000, 2},               /* r2 */
203     {0x10000, 3},               /* r3 */
204     {0x00800, 4},               /* r4 */
205     {0x00400, 5},               /* r5 */
206     {0x00200, 6},               /* r6 */
207     {0x00100, 7},               /* r7 */
208     {0x08000, 8},               /* r8 */
209     {0x04000, 9},               /* r9 */
210     {0x02000, 10},              /* r10 */
211     {0x01000, 11},              /* r11 */
212     {0x00080, 12},              /* r12 */
213     {0x00040, 13},              /* r13 */
214     {0x00020, 14},              /* r14 */
215     {0x00010, 15},              /* r15 */
216     {0, 0}                      /* end of table */
217   };
218   static struct reg_list pushmh_reg_table[] =
219   {
220     {0x80000, 16},              /* r16 */
221     {0x40000, 17},              /* r17 */
222     {0x20000, 18},              /* r18 */
223     {0x10000, 19},              /* r19 */
224     {0x00800, 20},              /* r20 */
225     {0x00400, 21},              /* r21 */
226     {0x00200, 22},              /* r22 */
227     {0x00100, 23},              /* r23 */
228     {0x08000, 24},              /* r24 */
229     {0x04000, 25},              /* r25 */
230     {0x02000, 26},              /* r26 */
231     {0x01000, 27},              /* r27 */
232     {0x00080, 28},              /* r28 */
233     {0x00040, 29},              /* r29 */
234     {0x00010, 30},              /* r30 */
235     {0x00020, 31},              /* r31 */
236     {0, 0}                      /* end of table */
237   };
238   struct reg_list *reg_table;
239   int i;
240
241   /* Is this a pushml or a pushmh? */
242   if ((insn2 & 7) == 1)
243     reg_table = pushml_reg_table;
244   else
245     reg_table = pushmh_reg_table;
246
247   /* Calculate the total size of the saved registers, and add it
248      it to the immediate value used to adjust SP. */
249   for (i = 0; reg_table[i].mask != 0; i++)
250     if (list12 & reg_table[i].mask)
251       offset += REGISTER_RAW_SIZE (regtable[i].regno);
252   pi->frameoffset -= offset;
253
254   /* Calculate the offsets of the registers relative to the value
255      the SP will have after the registers have been pushed and the
256      imm5 value is subtracted from it. */
257   if (pifsr)
258     {
259       for (i = 0; reg_table[i].mask != 0; i++)
260         {
261           if (list12 & reg_table[i].mask)
262             {
263               int reg = reg_table[i].regno;
264               offset -= REGISTER_RAW_SIZE (reg);
265               pifsr->reg = reg;
266               pifsr->offset = offset;
267               pifsr->cur_frameoffset = pi->frameoffset;
268 #ifdef DEBUG
269               printf_filtered ("\tSaved register r%d, offset %d", reg, pifsr->offset);
270 #endif
271               pifsr++;
272             }
273         }
274     }
275 #ifdef DEBUG
276   printf_filtered ("\tfound ctret after regsave func");
277 #endif
278
279   /* Set result parameters. */
280   *pifsr_ptr = pifsr;
281 }
282 \f
283
284
285
286 /* Function: scan_prologue
287    Scan the prologue of the function that contains PC, and record what
288    we find in PI.  PI->fsr must be zeroed by the called.  Returns the
289    pc after the prologue.  Note that the addresses saved in pi->fsr
290    are actually just frame relative (negative offsets from the frame
291    pointer).  This is because we don't know the actual value of the
292    frame pointer yet.  In some circumstances, the frame pointer can't
293    be determined till after we have scanned the prologue.  */
294
295 static CORE_ADDR
296 v850_scan_prologue (CORE_ADDR pc, struct prologue_info *pi)
297 {
298   CORE_ADDR func_addr, prologue_end, current_pc;
299   struct pifsr *pifsr, *pifsr_tmp;
300   int fp_used;
301   int ep_used;
302   int reg;
303   CORE_ADDR save_pc, save_end;
304   int regsave_func_p;
305   int r12_tmp;
306
307   /* First, figure out the bounds of the prologue so that we can limit the
308      search to something reasonable.  */
309
310   if (find_pc_partial_function (pc, NULL, &func_addr, NULL))
311     {
312       struct symtab_and_line sal;
313
314       sal = find_pc_line (func_addr, 0);
315
316       if (func_addr == entry_point_address ())
317         pi->start_function = 1;
318       else
319         pi->start_function = 0;
320
321 #if 0
322       if (sal.line == 0)
323         prologue_end = pc;
324       else
325         prologue_end = sal.end;
326 #else
327       prologue_end = pc;
328 #endif
329     }
330   else
331     {                           /* We're in the boondocks */
332       func_addr = pc - 100;
333       prologue_end = pc;
334     }
335
336   prologue_end = min (prologue_end, pc);
337
338   /* Now, search the prologue looking for instructions that setup fp, save
339      rp, adjust sp and such.  We also record the frame offset of any saved
340      registers. */
341
342   pi->frameoffset = 0;
343   pi->framereg = SP_REGNUM;
344   fp_used = 0;
345   ep_used = 0;
346   pifsr = pi->pifsrs;
347   regsave_func_p = 0;
348   save_pc = 0;
349   save_end = 0;
350   r12_tmp = 0;
351
352 #ifdef DEBUG
353   printf_filtered ("Current_pc = 0x%.8lx, prologue_end = 0x%.8lx\n",
354                    (long) func_addr, (long) prologue_end);
355 #endif
356
357   for (current_pc = func_addr; current_pc < prologue_end;)
358     {
359       int insn, insn2;
360
361 #ifdef DEBUG
362       printf_filtered ("0x%.8lx ", (long) current_pc);
363       (*tm_print_insn) (current_pc, &tm_print_insn_info);
364 #endif
365
366       insn = read_memory_unsigned_integer (current_pc, 2);
367       current_pc += 2;
368       if ((insn & 0x0780) >= 0x0600)    /* Four byte instruction? */
369         {
370           insn2 = read_memory_unsigned_integer (current_pc, 2);
371           current_pc += 2;
372         }
373
374       if ((insn & 0xffc0) == ((10 << 11) | 0x0780) && !regsave_func_p)
375         {                       /* jarl <func>,10 */
376           long low_disp = insn2 & ~(long) 1;
377           long disp = (((((insn & 0x3f) << 16) + low_disp)
378                         & ~(long) 1) ^ 0x00200000) - 0x00200000;
379
380           save_pc = current_pc;
381           save_end = prologue_end;
382           regsave_func_p = 1;
383           current_pc += disp - 4;
384           prologue_end = (current_pc
385                           + (2 * 3)     /* moves to/from ep */
386                           + 4   /* addi <const>,sp,sp */
387                           + 2   /* jmp [r10] */
388                           + (2 * 12)    /* sst.w to save r2, r20-r29, r31 */
389                           + 20);        /* slop area */
390
391 #ifdef DEBUG
392           printf_filtered ("\tfound jarl <func>,r10, disp = %ld, low_disp = %ld, new pc = 0x%.8lx\n",
393                            disp, low_disp, (long) current_pc + 2);
394 #endif
395           continue;
396         }
397       else if ((insn & 0xffc0) == 0x0200 && !regsave_func_p)
398         {                       /* callt <imm6> */
399           long ctbp = read_register (CTBP_REGNUM);
400           long adr = ctbp + ((insn & 0x3f) << 1);
401
402           save_pc = current_pc;
403           save_end = prologue_end;
404           regsave_func_p = 1;
405           current_pc = ctbp + (read_memory_unsigned_integer (adr, 2) & 0xffff);
406           prologue_end = (current_pc
407                           + (2 * 3)     /* prepare list2,imm5,sp/imm */
408                           + 4   /* ctret */
409                           + 20);        /* slop area */
410
411 #ifdef DEBUG
412           printf_filtered ("\tfound callt,  ctbp = 0x%.8lx, adr = %.8lx, new pc = 0x%.8lx\n",
413                            ctbp, adr, (long) current_pc);
414 #endif
415           continue;
416         }
417       else if ((insn & 0xffc0) == 0x0780)       /* prepare list2,imm5 */
418         {
419           handle_prepare (insn, insn2, &current_pc, pi, &pifsr);
420           continue;
421         }
422       else if (insn == 0x07e0 && regsave_func_p && insn2 == 0x0144)
423         {                       /* ctret after processing register save function */
424           current_pc = save_pc;
425           prologue_end = save_end;
426           regsave_func_p = 0;
427 #ifdef DEBUG
428           printf_filtered ("\tfound ctret after regsave func");
429 #endif
430           continue;
431         }
432       else if ((insn & 0xfff0) == 0x07e0 && (insn2 & 5) == 1)
433         {                       /* pushml, pushmh */
434           handle_pushm (insn, insn2, pi, &pifsr);
435           continue;
436         }
437       else if ((insn & 0xffe0) == 0x0060 && regsave_func_p)
438         {                       /* jmp after processing register save function */
439           current_pc = save_pc;
440           prologue_end = save_end;
441           regsave_func_p = 0;
442 #ifdef DEBUG
443           printf_filtered ("\tfound jmp after regsave func");
444 #endif
445           continue;
446         }
447       else if ((insn & 0x07c0) == 0x0780        /* jarl or jr */
448                || (insn & 0xffe0) == 0x0060     /* jmp */
449                || (insn & 0x0780) == 0x0580)    /* branch */
450         {
451 #ifdef DEBUG
452           printf_filtered ("\n");
453 #endif
454           break;                /* Ran into end of prologue */
455         }
456
457       else if ((insn & 0xffe0) == ((SP_REGNUM << 11) | 0x0240))         /* add <imm>,sp */
458         pi->frameoffset += ((insn & 0x1f) ^ 0x10) - 0x10;
459       else if (insn == ((SP_REGNUM << 11) | 0x0600 | SP_REGNUM))        /* addi <imm>,sp,sp */
460         pi->frameoffset += insn2;
461       else if (insn == ((FP_RAW_REGNUM << 11) | 0x0000 | SP_REGNUM))    /* mov sp,fp */
462         {
463           fp_used = 1;
464           pi->framereg = FP_RAW_REGNUM;
465         }
466
467       else if (insn == ((R12_REGNUM << 11) | 0x0640 | R0_REGNUM))       /* movhi hi(const),r0,r12 */
468         r12_tmp = insn2 << 16;
469       else if (insn == ((R12_REGNUM << 11) | 0x0620 | R12_REGNUM))      /* movea lo(const),r12,r12 */
470         r12_tmp += insn2;
471       else if (insn == ((SP_REGNUM << 11) | 0x01c0 | R12_REGNUM) && r12_tmp)    /* add r12,sp */
472         pi->frameoffset = r12_tmp;
473       else if (insn == ((EP_REGNUM << 11) | 0x0000 | SP_REGNUM))        /* mov sp,ep */
474         ep_used = 1;
475       else if (insn == ((EP_REGNUM << 11) | 0x0000 | R1_REGNUM))        /* mov r1,ep */
476         ep_used = 0;
477       else if (((insn & 0x07ff) == (0x0760 | SP_REGNUM)         /* st.w <reg>,<offset>[sp] */
478                 || (fp_used
479                     && (insn & 0x07ff) == (0x0760 | FP_RAW_REGNUM)))    /* st.w <reg>,<offset>[fp] */
480                && pifsr
481                && (((reg = (insn >> 11) & 0x1f) >= SAVE1_START_REGNUM && reg <= SAVE1_END_REGNUM)
482                    || (reg >= SAVE2_START_REGNUM && reg <= SAVE2_END_REGNUM)
483                  || (reg >= SAVE3_START_REGNUM && reg <= SAVE3_END_REGNUM)))
484         {
485           pifsr->reg = reg;
486           pifsr->offset = insn2 & ~1;
487           pifsr->cur_frameoffset = pi->frameoffset;
488 #ifdef DEBUG
489           printf_filtered ("\tSaved register r%d, offset %d", reg, pifsr->offset);
490 #endif
491           pifsr++;
492         }
493
494       else if (ep_used          /* sst.w <reg>,<offset>[ep] */
495                && ((insn & 0x0781) == 0x0501)
496                && pifsr
497                && (((reg = (insn >> 11) & 0x1f) >= SAVE1_START_REGNUM && reg <= SAVE1_END_REGNUM)
498                    || (reg >= SAVE2_START_REGNUM && reg <= SAVE2_END_REGNUM)
499                  || (reg >= SAVE3_START_REGNUM && reg <= SAVE3_END_REGNUM)))
500         {
501           pifsr->reg = reg;
502           pifsr->offset = (insn & 0x007e) << 1;
503           pifsr->cur_frameoffset = pi->frameoffset;
504 #ifdef DEBUG
505           printf_filtered ("\tSaved register r%d, offset %d", reg, pifsr->offset);
506 #endif
507           pifsr++;
508         }
509
510 #ifdef DEBUG
511       printf_filtered ("\n");
512 #endif
513     }
514
515   if (pifsr)
516     pifsr->framereg = 0;        /* Tie off last entry */
517
518   /* Fix up any offsets to the final offset.  If a frame pointer was created, use it
519      instead of the stack pointer.  */
520   for (pifsr_tmp = pi->pifsrs; pifsr_tmp && pifsr_tmp != pifsr; pifsr_tmp++)
521     {
522       pifsr_tmp->offset -= pi->frameoffset - pifsr_tmp->cur_frameoffset;
523       pifsr_tmp->framereg = pi->framereg;
524
525 #ifdef DEBUG
526       printf_filtered ("Saved register r%d, offset = %d, framereg = r%d\n",
527                     pifsr_tmp->reg, pifsr_tmp->offset, pifsr_tmp->framereg);
528 #endif
529     }
530
531 #ifdef DEBUG
532   printf_filtered ("Framereg = r%d, frameoffset = %d\n", pi->framereg, pi->frameoffset);
533 #endif
534
535   return current_pc;
536 }
537
538 /* Function: init_extra_frame_info
539    Setup the frame's frame pointer, pc, and frame addresses for saved
540    registers.  Most of the work is done in scan_prologue().
541
542    Note that when we are called for the last frame (currently active frame),
543    that fi->pc and fi->frame will already be setup.  However, fi->frame will
544    be valid only if this routine uses FP.  For previous frames, fi-frame will
545    always be correct (since that is derived from v850_frame_chain ()).
546
547    We can be called with the PC in the call dummy under two circumstances.
548    First, during normal backtracing, second, while figuring out the frame
549    pointer just prior to calling the target function (see run_stack_dummy).  */
550
551 void
552 v850_init_extra_frame_info (struct frame_info *fi)
553 {
554   struct prologue_info pi;
555   struct pifsr pifsrs[NUM_REGS + 1], *pifsr;
556
557   if (fi->next)
558     fi->pc = FRAME_SAVED_PC (fi->next);
559
560   memset (fi->fsr.regs, '\000', sizeof fi->fsr.regs);
561
562   /* The call dummy doesn't save any registers on the stack, so we can return
563      now.  */
564   if (PC_IN_CALL_DUMMY (fi->pc, fi->frame, fi->frame))
565     return;
566
567   pi.pifsrs = pifsrs;
568
569   v850_scan_prologue (fi->pc, &pi);
570
571   if (!fi->next && pi.framereg == SP_REGNUM)
572     fi->frame = read_register (pi.framereg) - pi.frameoffset;
573
574   for (pifsr = pifsrs; pifsr->framereg; pifsr++)
575     {
576       fi->fsr.regs[pifsr->reg] = pifsr->offset + fi->frame;
577
578       if (pifsr->framereg == SP_REGNUM)
579         fi->fsr.regs[pifsr->reg] += pi.frameoffset;
580     }
581 }
582
583 /* Function: frame_chain
584    Figure out the frame prior to FI.  Unfortunately, this involves
585    scanning the prologue of the caller, which will also be done
586    shortly by v850_init_extra_frame_info.  For the dummy frame, we
587    just return the stack pointer that was in use at the time the
588    function call was made.  */
589
590 CORE_ADDR
591 v850_frame_chain (struct frame_info *fi)
592 {
593   struct prologue_info pi;
594   CORE_ADDR callers_pc, fp;
595
596   /* First, find out who called us */
597   callers_pc = FRAME_SAVED_PC (fi);
598   /* If caller is a call-dummy, then our FP bears no relation to his FP! */
599   fp = v850_find_callers_reg (fi, FP_RAW_REGNUM);
600   if (PC_IN_CALL_DUMMY (callers_pc, fp, fp))
601     return fp;                  /* caller is call-dummy: return oldest value of FP */
602
603   /* Caller is NOT a call-dummy, so everything else should just work.
604      Even if THIS frame is a call-dummy! */
605   pi.pifsrs = NULL;
606
607   v850_scan_prologue (callers_pc, &pi);
608
609   if (pi.start_function)
610     return 0;                   /* Don't chain beyond the start function */
611
612   if (pi.framereg == FP_RAW_REGNUM)
613     return v850_find_callers_reg (fi, pi.framereg);
614
615   return fi->frame - pi.frameoffset;
616 }
617
618 /* Function: find_callers_reg
619    Find REGNUM on the stack.  Otherwise, it's in an active register.
620    One thing we might want to do here is to check REGNUM against the
621    clobber mask, and somehow flag it as invalid if it isn't saved on
622    the stack somewhere.  This would provide a graceful failure mode
623    when trying to get the value of caller-saves registers for an inner
624    frame.  */
625
626 CORE_ADDR
627 v850_find_callers_reg (struct frame_info *fi, int regnum)
628 {
629   for (; fi; fi = fi->next)
630     if (PC_IN_CALL_DUMMY (fi->pc, fi->frame, fi->frame))
631       return generic_read_register_dummy (fi->pc, fi->frame, regnum);
632     else if (fi->fsr.regs[regnum] != 0)
633       return read_memory_unsigned_integer (fi->fsr.regs[regnum],
634                                            REGISTER_RAW_SIZE (regnum));
635
636   return read_register (regnum);
637 }
638
639 /* Function: skip_prologue
640    Return the address of the first code past the prologue of the function.  */
641
642 CORE_ADDR
643 v850_skip_prologue (CORE_ADDR pc)
644 {
645   CORE_ADDR func_addr, func_end;
646
647   /* See what the symbol table says */
648
649   if (find_pc_partial_function (pc, NULL, &func_addr, &func_end))
650     {
651       struct symtab_and_line sal;
652
653       sal = find_pc_line (func_addr, 0);
654
655       if (sal.line != 0 && sal.end < func_end)
656         return sal.end;
657       else
658         /* Either there's no line info, or the line after the prologue is after
659            the end of the function.  In this case, there probably isn't a
660            prologue.  */
661         return pc;
662     }
663
664 /* We can't find the start of this function, so there's nothing we can do. */
665   return pc;
666 }
667
668 /* Function: pop_frame
669    This routine gets called when either the user uses the `return'
670    command, or the call dummy breakpoint gets hit.  */
671
672 void
673 v850_pop_frame (struct frame_info *frame)
674 {
675   int regnum;
676
677   if (PC_IN_CALL_DUMMY (frame->pc, frame->frame, frame->frame))
678     generic_pop_dummy_frame ();
679   else
680     {
681       write_register (PC_REGNUM, FRAME_SAVED_PC (frame));
682
683       for (regnum = 0; regnum < NUM_REGS; regnum++)
684         if (frame->fsr.regs[regnum] != 0)
685           write_register (regnum,
686                       read_memory_unsigned_integer (frame->fsr.regs[regnum],
687                                                REGISTER_RAW_SIZE (regnum)));
688
689       write_register (SP_REGNUM, FRAME_FP (frame));
690     }
691
692   flush_cached_frames ();
693 }
694
695 /* Function: push_arguments
696    Setup arguments and RP for a call to the target.  First four args
697    go in R6->R9, subsequent args go into sp + 16 -> sp + ...  Structs
698    are passed by reference.  64 bit quantities (doubles and long
699    longs) may be split between the regs and the stack.  When calling a
700    function that returns a struct, a pointer to the struct is passed
701    in as a secret first argument (always in R6).
702
703    Stack space for the args has NOT been allocated: that job is up to us.
704  */
705
706 CORE_ADDR
707 v850_push_arguments (int nargs, value_ptr *args, CORE_ADDR sp,
708                      unsigned char struct_return, CORE_ADDR struct_addr)
709 {
710   int argreg;
711   int argnum;
712   int len = 0;
713   int stack_offset;
714
715   /* First, just for safety, make sure stack is aligned */
716   sp &= ~3;
717
718   /* Now make space on the stack for the args. */
719   for (argnum = 0; argnum < nargs; argnum++)
720     len += ((TYPE_LENGTH (VALUE_TYPE (args[argnum])) + 3) & ~3);
721   sp -= len;                    /* possibly over-allocating, but it works... */
722   /* (you might think we could allocate 16 bytes */
723   /* less, but the ABI seems to use it all! )  */
724   argreg = ARG0_REGNUM;
725
726   /* the struct_return pointer occupies the first parameter-passing reg */
727   if (struct_return)
728     write_register (argreg++, struct_addr);
729
730   stack_offset = 16;
731   /* The offset onto the stack at which we will start copying parameters
732      (after the registers are used up) begins at 16 rather than at zero.
733      I don't really know why, that's just the way it seems to work.  */
734
735   /* Now load as many as possible of the first arguments into
736      registers, and push the rest onto the stack.  There are 16 bytes
737      in four registers available.  Loop thru args from first to last.  */
738   for (argnum = 0; argnum < nargs; argnum++)
739     {
740       int len;
741       char *val;
742       char valbuf[REGISTER_RAW_SIZE (ARG0_REGNUM)];
743
744       if (TYPE_CODE (VALUE_TYPE (*args)) == TYPE_CODE_STRUCT
745           && TYPE_LENGTH (VALUE_TYPE (*args)) > 8)
746         {
747           store_address (valbuf, 4, VALUE_ADDRESS (*args));
748           len = 4;
749           val = valbuf;
750         }
751       else
752         {
753           len = TYPE_LENGTH (VALUE_TYPE (*args));
754           val = (char *) VALUE_CONTENTS (*args);
755         }
756
757       while (len > 0)
758         if (argreg <= ARGLAST_REGNUM)
759           {
760             CORE_ADDR regval;
761
762             regval = extract_address (val, REGISTER_RAW_SIZE (argreg));
763             write_register (argreg, regval);
764
765             len -= REGISTER_RAW_SIZE (argreg);
766             val += REGISTER_RAW_SIZE (argreg);
767             argreg++;
768           }
769         else
770           {
771             write_memory (sp + stack_offset, val, 4);
772
773             len -= 4;
774             val += 4;
775             stack_offset += 4;
776           }
777       args++;
778     }
779   return sp;
780 }
781
782 /* Function: push_return_address (pc)
783    Set up the return address for the inferior function call.
784    Needed for targets where we don't actually execute a JSR/BSR instruction */
785
786 CORE_ADDR
787 v850_push_return_address (CORE_ADDR pc, CORE_ADDR sp)
788 {
789   write_register (RP_REGNUM, CALL_DUMMY_ADDRESS ());
790   return sp;
791 }
792
793 /* Function: frame_saved_pc 
794    Find the caller of this frame.  We do this by seeing if RP_REGNUM
795    is saved in the stack anywhere, otherwise we get it from the
796    registers.  If the inner frame is a dummy frame, return its PC
797    instead of RP, because that's where "caller" of the dummy-frame
798    will be found.  */
799
800 CORE_ADDR
801 v850_frame_saved_pc (struct frame_info *fi)
802 {
803   if (PC_IN_CALL_DUMMY (fi->pc, fi->frame, fi->frame))
804     return generic_read_register_dummy (fi->pc, fi->frame, PC_REGNUM);
805   else
806     return v850_find_callers_reg (fi, RP_REGNUM);
807 }
808
809
810 /* Function: fix_call_dummy
811    Pokes the callee function's address into the CALL_DUMMY assembly stub.
812    Assumes that the CALL_DUMMY looks like this:
813    jarl <offset24>, r31
814    trap
815  */
816
817 int
818 v850_fix_call_dummy (char *dummy, CORE_ADDR sp, CORE_ADDR fun, int nargs,
819                      value_ptr *args, struct type *type, int gcc_p)
820 {
821   long offset24;
822
823   offset24 = (long) fun - (long) entry_point_address ();
824   offset24 &= 0x3fffff;
825   offset24 |= 0xff800000;       /* jarl <offset24>, r31 */
826
827   store_unsigned_integer ((unsigned int *) &dummy[2], 2, offset24 & 0xffff);
828   store_unsigned_integer ((unsigned int *) &dummy[0], 2, offset24 >> 16);
829   return 0;
830 }
831
832 /* Change the register names based on the current machine type. */
833
834 static int
835 v850_target_architecture_hook (const bfd_arch_info_type *ap)
836 {
837   int i, j;
838
839   if (ap->arch != bfd_arch_v850)
840     return 0;
841
842   for (i = 0; v850_processor_type_table[i].regnames != NULL; i++)
843     {
844       if (v850_processor_type_table[i].mach == ap->mach)
845         {
846           v850_register_names = v850_processor_type_table[i].regnames;
847           tm_print_insn_info.mach = ap->mach;
848           return 1;
849         }
850     }
851
852   internal_error (__FILE__, __LINE__,
853                   "Architecture `%s' unrecognized", ap->printable_name);
854 }
855
856 void
857 _initialize_v850_tdep (void)
858 {
859   tm_print_insn = print_insn_v850;
860   target_architecture_hook = v850_target_architecture_hook;
861 }