]> Git Repo - binutils.git/blob - gdb/v850-tdep.c
PR 8511, keep new test cases
[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_unsigned_integer (current_pc, 2);
90
91       if ((insn & 0xffe0) == ((SP_REGNUM << 11) | 0x0240)) /* add <imm>,sp */
92         frameoffset = ((insn & 0x1f) ^ 0x10) - 0x10;
93       else if (insn == ((SP_REGNUM << 11) | 0x0600 | SP_REGNUM)) /* addi <imm>,sp,sp */
94           frameoffset = read_memory_integer (current_pc + 2, 2);
95       else if (insn == ((FP_REGNUM << 11) | 0x0000 | 12)) /* mov r12,r2 */
96         framereg = FP_REGNUM;   /* Setting up fp */
97       else if ((insn & 0x07ff) == (0x0760 | SP_REGNUM)) /* st.w <reg>,<offset>[sp] */
98         {
99           reg = (insn >> 11) & 0x1f;    /* Extract <reg> */
100
101           insn = read_memory_integer (current_pc + 2, 2) & ~1;
102
103           fi->fsr.regs[reg] = insn + frameoffset;
104         }
105       else if ((insn & 0x07ff) == (0x0760 | FP_REGNUM)) /* st.w <reg>,<offset>[fp] */
106         {
107           reg = (insn >> 11) & 0x1f;    /* Extract <reg> */
108
109           insn = read_memory_integer (current_pc + 2, 2) & ~1;
110
111           fi->fsr.regs[reg] = insn;
112         }
113
114       if ((insn & 0x0780) >= 0x0600) /* Four byte instruction? */
115         current_pc += 2;
116     }
117
118   if (PC_IN_CALL_DUMMY (fi->pc, NULL, NULL))
119     fi->frame = dummy_frame_stack->sp;
120   else if (!fi->next && framereg == SP_REGNUM)
121     fi->frame = read_register (framereg) - frameoffset;
122
123   for (reg = 0; reg < NUM_REGS; reg++)
124     if (fi->fsr.regs[reg] != 0)
125       fi->fsr.regs[reg] += fi->frame;
126 }
127
128 /* Find the caller of this frame.  We do this by seeing if RP_REGNUM is saved
129    in the stack anywhere, otherwise we get it from the registers. */
130
131 CORE_ADDR
132 v850_find_callers_reg (fi, regnum)
133      struct frame_info *fi;
134      int regnum;
135 {
136   /* XXX - Won't work if multiple dummy frames are active */
137   if (PC_IN_CALL_DUMMY (fi->pc, NULL, NULL))
138     switch (regnum)
139       {
140       case SP_REGNUM:
141         return dummy_frame_stack->sp;
142         break;
143       case FP_REGNUM:
144         return dummy_frame_stack->fp;
145         break;
146       case RP_REGNUM:
147         return dummy_frame_stack->pc;
148         break;
149       case PC_REGNUM:
150         return dummy_frame_stack->pc;
151         break;
152       }
153
154   for (; fi; fi = fi->next)
155     if (fi->fsr.regs[regnum] != 0)
156       return read_memory_integer (fi->fsr.regs[regnum], 4);
157
158   return read_register (regnum);
159 }
160
161 CORE_ADDR
162 v850_frame_chain (fi)
163      struct frame_info *fi;
164 {
165   CORE_ADDR callers_pc, callers_sp;
166   CORE_ADDR func_addr, prologue_end, current_pc;
167   int frameoffset;
168
169   /* First, find out who called us */
170
171   callers_pc = FRAME_SAVED_PC (fi);
172
173   if (PC_IN_CALL_DUMMY (callers_pc, NULL, NULL))
174     return dummy_frame_stack->sp; /* XXX Won't work if multiple dummy frames on stack! */
175
176   /* Next, figure out where his prologue is.  */
177
178   if (find_pc_partial_function (callers_pc, NULL, &func_addr, NULL))
179     {
180       struct symtab_and_line sal;
181
182       /* Stop when the caller is the entry point function */
183       if (func_addr == entry_point_address ())
184         return 0;
185
186       sal = find_pc_line (func_addr, 0);
187
188       if (sal.line == 0)
189         prologue_end = callers_pc;
190       else
191         prologue_end = sal.end;
192     }
193   else
194     prologue_end = func_addr + 100; /* We're in the boondocks */
195
196   prologue_end = min (prologue_end, callers_pc);
197
198   /* Now, figure out the frame location of the caller by examining his prologue.
199      We're looking for either a load of the frame pointer register, or a stack
200      adjustment. */
201
202   frameoffset = 0;
203
204   for (current_pc = func_addr; current_pc < prologue_end; current_pc += 2)
205     {
206       int insn;
207
208       insn = read_memory_unsigned_integer (current_pc, 2);
209
210       if ((insn & 0xffe0) == ((SP_REGNUM << 11) | 0x0240)) /* add <imm>,sp */
211         frameoffset = ((insn & 0x1f) ^ 0x10) - 0x10;
212       else if (insn == ((SP_REGNUM << 11) | 0x0600 | SP_REGNUM)) /* addi <imm>,sp,sp */
213         frameoffset = read_memory_integer (current_pc + 2, 2);
214       else if (insn == ((FP_REGNUM << 11) | 0x0000 | 12)) /* mov r12,r2 */
215         return v850_find_callers_reg (fi, FP_REGNUM); /* It's using a frame pointer reg */
216
217       if ((insn & 0x0780) >= 0x0600) /* Four byte instruction? */
218         current_pc += 2;
219     }
220
221   return fi->frame - frameoffset;
222 }
223
224 CORE_ADDR
225 v850_skip_prologue (pc)
226      CORE_ADDR pc;
227 {
228   CORE_ADDR func_addr, func_end;
229
230   /* See what the symbol table says */
231
232   if (find_pc_partial_function (pc, NULL, &func_addr, &func_end))
233     {
234       struct symtab_and_line sal;
235
236       sal = find_pc_line (func_addr, 0);
237
238       if (sal.line != 0 && sal.end < func_end)
239         return sal.end;
240       else
241         /* Either there's no line info, or the line after the prologue is after
242            the end of the function.  In this case, there probably isn't a
243            prologue.  */
244         return pc;
245     }
246
247 /* We can't find the start of this function, so there's nothing we can do. */
248   return pc;
249 }
250
251 /* All we do here is record SP and FP on the call dummy stack */
252
253 void
254 v850_push_dummy_frame ()
255 {
256   struct dummy_frame *dummy_frame;
257
258   dummy_frame = xmalloc (sizeof (struct dummy_frame));
259
260   dummy_frame->fp = read_register (FP_REGNUM);
261   dummy_frame->sp = read_register (SP_REGNUM);
262   dummy_frame->rp = read_register (RP_REGNUM);
263   dummy_frame->pc = read_register (PC_REGNUM);
264   dummy_frame->next = dummy_frame_stack;
265   dummy_frame_stack = dummy_frame;
266 }
267
268 int
269 v850_pc_in_call_dummy (pc)
270      CORE_ADDR pc;
271 {
272   return dummy_frame_stack
273          && pc >= CALL_DUMMY_ADDRESS ()
274          && pc <= CALL_DUMMY_ADDRESS () + DECR_PC_AFTER_BREAK;
275 }
276
277 struct frame_info *
278 v850_pop_frame (frame)
279      struct frame_info *frame;
280 {
281   int regnum;
282
283   if (PC_IN_CALL_DUMMY (frame->pc, NULL, NULL))
284     {
285       struct dummy_frame *dummy_frame;
286       
287       dummy_frame = dummy_frame_stack;
288       if (!dummy_frame)
289         error ("Can't pop dummy frame!");
290
291       dummy_frame_stack = dummy_frame->next;
292
293       write_register (FP_REGNUM, dummy_frame->fp);
294       write_register (SP_REGNUM, dummy_frame->sp);
295       write_register (RP_REGNUM, dummy_frame->rp);
296       write_register (PC_REGNUM, dummy_frame->pc);
297
298       free (dummy_frame);
299
300       flush_cached_frames ();
301
302       return NULL;
303     }
304
305   write_register (PC_REGNUM, FRAME_SAVED_PC (frame));
306
307   for (regnum = 0; regnum < NUM_REGS; regnum++)
308     if (frame->fsr.regs[regnum] != 0)
309       write_register (regnum, read_memory_integer (frame->fsr.regs[regnum], 4));
310
311   write_register (SP_REGNUM, FRAME_FP (frame));
312   flush_cached_frames ();
313
314   return NULL;
315 }
316
317 /* Put arguments in the right places, and setup return address register (RP) to
318    point at a convenient place to put a breakpoint.  First four args go in
319    R6->R9, subsequent args go into sp + 16 -> sp + ...  Structs are passed by
320    reference.  64 bit quantities (doubles and long longs) may be split between
321    the regs and the stack.  When calling a function that returns a struct, a
322    pointer to the struct is passed in as a secret first argument (always in R6).
323
324    By the time we get here, stack space has been allocated for the args, but
325    not for the struct return pointer.  */
326
327 CORE_ADDR
328 v850_push_arguments (nargs, args, sp, struct_return, struct_addr)
329      int nargs;
330      value_ptr *args;
331      CORE_ADDR sp;
332      unsigned char struct_return;
333      CORE_ADDR struct_addr;
334 {
335   int argreg;
336   int argnum;
337
338   argreg = ARG0_REGNUM;
339
340   if (struct_return)
341     {
342       write_register (argreg++, struct_addr);
343       sp -= 4;
344     }
345
346   for (argnum = 0; argnum < nargs; argnum++)
347     {
348       int len;
349       char *val;
350       char valbuf[4];
351
352       if (TYPE_CODE (VALUE_TYPE (*args)) == TYPE_CODE_STRUCT
353           && TYPE_LENGTH (VALUE_TYPE (*args)) > 8)
354         {
355           store_address (valbuf, 4, VALUE_ADDRESS (*args));
356           len = 4;
357           val = valbuf;
358         }
359       else
360         {
361           len = TYPE_LENGTH (VALUE_TYPE (*args));
362           val = (char *)VALUE_CONTENTS (*args);
363         }
364
365       while (len > 0)
366         if  (argreg <= ARGLAST_REGNUM)
367           {
368             CORE_ADDR regval;
369
370             regval = extract_address (val, REGISTER_RAW_SIZE (argreg));
371             write_register (argreg, regval);
372
373             len -= REGISTER_RAW_SIZE (argreg);
374             val += REGISTER_RAW_SIZE (argreg);
375             argreg++;
376           }
377         else
378           {
379             write_memory (sp + argnum * 4, val, 4);
380
381             len -= 4;
382             val += 4;
383           }
384       args++;
385     }
386
387   write_register (RP_REGNUM, entry_point_address ());
388
389   return sp;
390 }
391 \f
392 void
393 _initialize_sparc_tdep ()
394 {
395   tm_print_insn = print_insn_v850;
396 }
This page took 0.045732 seconds and 4 git commands to generate.