* buildsym.c (finish_block): Treat LOC_BASEREG_ARG and
[platform/upstream/binutils.git] / gdb / v850-tdep.c
1 /* Target-dependent code for the NEC V850 for GDB, the GNU debugger.
2    Copyright 1996, 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, Boston, MA 02111-1307, USA.  */
19
20 #include "defs.h"
21 #include "frame.h"
22 #include "inferior.h"
23 #include "obstack.h"
24 #include "target.h"
25 #include "value.h"
26 #include "bfd.h"
27 #include "gdb_string.h"
28 #include "gdbcore.h"
29
30 struct dummy_frame
31 {
32   struct dummy_frame *next;
33
34   CORE_ADDR fp;
35   CORE_ADDR sp;
36   CORE_ADDR rp;
37   CORE_ADDR pc;
38 };
39
40 static struct dummy_frame *dummy_frame_stack = NULL;
41 \f
42 /* This function actually figures out the frame address for a given pc and
43    sp.  This is tricky on the v850 because we only use an explicit frame
44    pointer when using alloca().  The only reliable way to get this info is to
45    examine the prologue.
46 */
47
48 void
49 v850_init_extra_frame_info (fi)
50      struct frame_info *fi;
51 {
52   struct symtab_and_line sal;
53   CORE_ADDR func_addr, prologue_end, current_pc;
54   int reg;
55   int frameoffset;
56   int framereg;
57
58   if (fi->next)
59     fi->pc = FRAME_SAVED_PC (fi->next);
60
61   /* First, figure out the bounds of the prologue so that we can limit the
62      search to something reasonable.  */
63
64   if (find_pc_partial_function (fi->pc, NULL, &func_addr, NULL))
65     {
66       sal = find_pc_line (func_addr, 0);
67
68       if (sal.line == 0)
69         prologue_end = fi->pc;
70       else
71         prologue_end = sal.end;
72     }
73   else
74     prologue_end = func_addr + 100; /* We're in the boondocks */
75
76   prologue_end = min (prologue_end, fi->pc);
77
78   /* Now, search the prologue looking for instructions that setup fp, save
79      rp, adjust sp and such. */ 
80
81   framereg = SP_REGNUM;
82   frameoffset = 0;
83   memset (fi->fsr.regs, '\000', sizeof fi->fsr.regs);
84
85   for (current_pc = func_addr; current_pc < prologue_end; current_pc += 2)
86     {
87       int insn;
88
89       insn = read_memory_integer (current_pc, 2);
90
91       if ((insn & 0xffe0) == ((SP_REGNUM << 11) | 0x0240)) /* add <imm>,sp */
92         frameoffset = (insn & 0x1f) | ~0x1f;
93       else if (insn == ((SP_REGNUM << 11) | 0x0600 | SP_REGNUM)) /* addi <imm>,sp,sp */
94         {
95           current_pc += 2;
96
97           frameoffset = read_memory_integer (current_pc, 2);
98         }
99       else if (insn == ((FP_REGNUM << 11) | 0x0000 | 12)) /* mov r12,r2 */
100         framereg = FP_REGNUM;   /* Setting up fp */
101       else if ((insn & 0x07ff) == (0x0760 | SP_REGNUM)) /* st.w <reg>,<offset>[sp] */
102         {
103           reg = (insn >> 11) & 0x1f;    /* Extract <reg> */
104           current_pc += 2;
105
106           insn = read_memory_integer (current_pc, 2) & ~1;
107
108           fi->fsr.regs[reg] = insn + frameoffset;
109         }
110       else if ((insn & 0x07ff) == (0x0760 | FP_REGNUM)) /* st.w <reg>,<offset>[fp] */
111         {
112           reg = (insn >> 11) & 0x1f;    /* Extract <reg> */
113           current_pc += 2;
114
115           insn = read_memory_integer (current_pc, 2) & ~1;
116
117           fi->fsr.regs[reg] = insn;
118         }
119     }
120
121   if (PC_IN_CALL_DUMMY (fi->pc, NULL, NULL))
122     fi->frame = dummy_frame_stack->sp;
123   else if (!fi->next && framereg == SP_REGNUM)
124     fi->frame = read_register (framereg) - frameoffset;
125
126   for (reg = 0; reg < NUM_REGS; reg++)
127     if (fi->fsr.regs[reg] != 0)
128       fi->fsr.regs[reg] += fi->frame;
129 }
130
131 /* Find the caller of this frame.  We do this by seeing if RP_REGNUM is saved
132    in the stack anywhere, otherwise we get it from the registers. */
133
134 CORE_ADDR
135 v850_find_callers_reg (fi, regnum)
136      struct frame_info *fi;
137      int regnum;
138 {
139   /* XXX - Won't work if multiple dummy frames are active */
140   if (PC_IN_CALL_DUMMY (fi->pc, NULL, NULL))
141     switch (regnum)
142       {
143       case SP_REGNUM:
144         return dummy_frame_stack->sp;
145         break;
146       case FP_REGNUM:
147         return dummy_frame_stack->fp;
148         break;
149       case RP_REGNUM:
150         return dummy_frame_stack->pc;
151         break;
152       case PC_REGNUM:
153         return dummy_frame_stack->pc;
154         break;
155       }
156
157   for (; fi; fi = fi->next)
158     if (fi->fsr.regs[regnum] != 0)
159       return read_memory_integer (fi->fsr.regs[regnum], 4);
160
161   return read_register (regnum);
162 }
163
164 CORE_ADDR
165 v850_frame_chain (fi)
166      struct frame_info *fi;
167 {
168   CORE_ADDR callers_pc, callers_sp;
169   CORE_ADDR func_addr, prologue_end, current_pc;
170   int frameoffset;
171
172   /* First, find out who called us */
173
174   callers_pc = FRAME_SAVED_PC (fi);
175
176   if (PC_IN_CALL_DUMMY (callers_pc, NULL, NULL))
177     return dummy_frame_stack->sp; /* XXX Won't work if multiple dummy frames on stack! */
178
179   /* Next, figure out where his prologue is.  */
180
181   if (find_pc_partial_function (callers_pc, NULL, &func_addr, NULL))
182     {
183       struct symtab_and_line sal;
184
185       /* Stop when the caller is the entry point function */
186       if (func_addr == entry_point_address ())
187         return 0;
188
189       sal = find_pc_line (func_addr, 0);
190
191       if (sal.line == 0)
192         prologue_end = callers_pc;
193       else
194         prologue_end = sal.end;
195     }
196   else
197     prologue_end = func_addr + 100; /* We're in the boondocks */
198
199   prologue_end = min (prologue_end, callers_pc);
200
201   /* Now, figure out the frame location of the caller by examining his prologue.
202      We're looking for either a load of the frame pointer register, or a stack
203      adjustment. */
204
205   frameoffset = 0;
206
207   for (current_pc = func_addr; current_pc < prologue_end; current_pc += 2)
208     {
209       int insn;
210
211       insn = read_memory_integer (current_pc, 2);
212
213       if ((insn & 0xffe0) == ((SP_REGNUM << 11) | 0x0240)) /* add <imm>,sp */
214         frameoffset = (insn & 0x1f) | ~0x1f;
215       else if (insn == ((SP_REGNUM << 11) | 0x0600 | SP_REGNUM)) /* addi <imm>,sp,sp */
216         {
217           current_pc += 2;
218
219           frameoffset = read_memory_integer (current_pc, 2);
220         }
221       else if (insn == ((FP_REGNUM << 11) | 0x0000 | 12)) /* mov r12,r2 */
222         return v850_find_callers_reg (fi, FP_REGNUM); /* It's using a frame pointer reg */
223       else if ((insn & 0x07ff) == (0x0760 | SP_REGNUM)) /* st.w <reg>,<offset>[sp] */
224         current_pc += 2;
225       else if ((insn & 0x07ff) == (0x0760 | FP_REGNUM)) /* st.w <reg>,<offset>[fp] */
226         current_pc += 2;
227     }
228
229   return fi->frame - frameoffset;
230 }
231
232 CORE_ADDR
233 v850_skip_prologue (pc)
234      CORE_ADDR pc;
235 {
236   CORE_ADDR func_addr, func_end;
237
238   /* See what the symbol table says */
239
240   if (find_pc_partial_function (pc, NULL, &func_addr, &func_end))
241     {
242       struct symtab_and_line sal;
243
244       sal = find_pc_line (func_addr, 0);
245
246       if (sal.line != 0 && sal.end < func_end)
247         return sal.end;
248       else
249         /* Either there's no line info, or the line after the prologue is after
250            the end of the function.  In this case, there probably isn't a
251            prologue.  */
252         return pc;
253     }
254
255 /* We can't find the start of this function, so there's nothing we can do. */
256   return pc;
257 }
258
259 /* All we do here is record SP and FP on the call dummy stack */
260
261 void
262 v850_push_dummy_frame ()
263 {
264   struct dummy_frame *dummy_frame;
265
266   dummy_frame = xmalloc (sizeof (struct dummy_frame));
267
268   dummy_frame->fp = read_register (FP_REGNUM);
269   dummy_frame->sp = read_register (SP_REGNUM);
270   dummy_frame->rp = read_register (RP_REGNUM);
271   dummy_frame->pc = read_register (PC_REGNUM);
272   dummy_frame->next = dummy_frame_stack;
273   dummy_frame_stack = dummy_frame;
274 }
275
276 int
277 v850_pc_in_call_dummy (pc)
278      CORE_ADDR pc;
279 {
280   return dummy_frame_stack
281          && pc >= CALL_DUMMY_ADDRESS ()
282          && pc <= CALL_DUMMY_ADDRESS () + DECR_PC_AFTER_BREAK;
283 }
284
285 struct frame_info *
286 v850_pop_frame (frame)
287      struct frame_info *frame;
288 {
289   int regnum;
290
291   if (PC_IN_CALL_DUMMY (frame->pc, NULL, NULL))
292     {
293       struct dummy_frame *dummy_frame;
294       
295       dummy_frame = dummy_frame_stack;
296       if (!dummy_frame)
297         error ("Can't pop dummy frame!");
298
299       dummy_frame_stack = dummy_frame->next;
300
301       write_register (FP_REGNUM, dummy_frame->fp);
302       write_register (SP_REGNUM, dummy_frame->sp);
303       write_register (RP_REGNUM, dummy_frame->rp);
304       write_register (PC_REGNUM, dummy_frame->pc);
305
306       free (dummy_frame);
307
308       flush_cached_frames ();
309
310       return NULL;
311     }
312
313   write_register (PC_REGNUM, FRAME_SAVED_PC (frame));
314
315   for (regnum = 0; regnum < NUM_REGS; regnum++)
316     if (frame->fsr.regs[regnum] != 0)
317       write_register (regnum, read_memory_integer (frame->fsr.regs[regnum], 4));
318
319   write_register (SP_REGNUM, FRAME_FP (frame));
320   flush_cached_frames ();
321
322   return NULL;
323 }
324
325 /* Put arguments in the right places, and setup return address register (RP) to
326    point at a convenient place to put a breakpoint.  First four args go in
327    R6->R9, subsequent args go into sp + 16 -> sp + ...  Structs are passed by
328    reference.  64 bit quantities (doubles and long longs) may be split between
329    the regs and the stack.  When calling a function that returns a struct, a
330    pointer to the struct is passed in as a secret first argument (always in R6).
331
332    By the time we get here, stack space has been allocated for the args, but
333    not for the struct return pointer.  */
334
335 CORE_ADDR
336 v850_push_arguments (nargs, args, sp, struct_return, struct_addr)
337      int nargs;
338      value_ptr *args;
339      CORE_ADDR sp;
340      unsigned char struct_return;
341      CORE_ADDR struct_addr;
342 {
343   int argreg;
344   int argnum;
345
346   argreg = 6;
347
348   if (struct_return)
349     {
350       write_register (argreg++, struct_addr);
351       sp -= 4;
352     }
353
354   for (argnum = 0; argnum < nargs; argnum++)
355     {
356       int len;
357       char *val;
358       char valbuf[4];
359
360       if (TYPE_CODE (VALUE_TYPE (*args)) == TYPE_CODE_STRUCT
361           && TYPE_LENGTH (VALUE_TYPE (*args)) > 8)
362         {
363           store_address (valbuf, 4, VALUE_ADDRESS (*args));
364           len = 4;
365           val = valbuf;
366         }
367       else
368         {
369           len = TYPE_LENGTH (VALUE_TYPE (*args));
370           val = (char *)VALUE_CONTENTS (*args);
371         }
372
373       while (len > 0)
374         if  (argreg <= 9)
375           {
376             CORE_ADDR regval;
377
378             regval = extract_address (val, REGISTER_RAW_SIZE (argreg));
379             write_register (argreg, regval);
380
381             len -= REGISTER_RAW_SIZE (argreg);
382             val += REGISTER_RAW_SIZE (argreg);
383             argreg++;
384           }
385         else
386           {
387             write_memory (sp + argnum * 4, val, 4);
388
389             len -= 4;
390             val += 4;
391           }
392       args++;
393     }
394
395   write_register (RP_REGNUM, entry_point_address ());
396
397   return sp;
398 }
399 \f
400 void
401 _initialize_sparc_tdep ()
402 {
403   tm_print_insn = print_insn_v850;
404 }