* Makefile.in (LIBCOMMON): Define.
[deliverable/binutils-gdb.git] / gdb / v850-tdep.c
CommitLineData
e2810631 1/* Target-dependent code for the NEC V850 for GDB, the GNU debugger.
ac954805 2 Copyright 1996, Free Software Foundation, Inc.
e2810631
SG
3
4This file is part of GDB.
5
6This program is free software; you can redistribute it and/or modify
7it under the terms of the GNU General Public License as published by
8the Free Software Foundation; either version 2 of the License, or
9(at your option) any later version.
10
11This program is distributed in the hope that it will be useful,
12but WITHOUT ANY WARRANTY; without even the implied warranty of
13MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14GNU General Public License for more details.
15
16You should have received a copy of the GNU General Public License
17along with this program; if not, write to the Free Software
18Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
19
e2810631
SG
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"
e2810631 28#include "gdbcore.h"
23da411a
SG
29#include "symfile.h"
30
31/* Dummy frame. This saves the processor state just prior to setting up the
32 inferior function call. On most targets, the registers are saved on the
33 target stack, but that really slows down function calls. */
e5a2ac8b 34
ac954805
SG
35struct dummy_frame
36{
37 struct dummy_frame *next;
38
23da411a 39 char regs[REGISTER_BYTES];
ac954805
SG
40};
41
42static struct dummy_frame *dummy_frame_stack = NULL;
e5a2ac8b 43
23da411a
SG
44static CORE_ADDR read_register_dummy PARAMS ((int regno));
45
46/* Info gleaned from scanning a function's prologue. */
47
a638512f
SG
48struct pifsr /* Info about one saved reg */
49{
50 int framereg; /* Frame reg (SP or FP) */
51 int offset; /* Offset from framereg */
52 int reg; /* Saved register number */
53};
54
23da411a 55struct prologue_info
e5a2ac8b 56{
e5a2ac8b 57 int framereg;
23da411a
SG
58 int frameoffset;
59 int start_function;
a638512f 60 struct pifsr *pifsrs;
23da411a 61};
e5a2ac8b 62
23da411a
SG
63static CORE_ADDR scan_prologue PARAMS ((CORE_ADDR pc, struct prologue_info *fs));
64\f
65/* Scan the prologue of the function that contains PC, and record what we find
66 in PI. PI->fsr must be zeroed by the called. Returns the pc after the
67 prologue. Note that the addresses saved in pi->fsr are actually just frame
68 relative (negative offsets from the frame pointer). This is because we
69 don't know the actual value of the frame pointer yet. In some
70 circumstances, the frame pointer can't be determined till after we have
71 scanned the prologue. */
72
73static CORE_ADDR
74scan_prologue (pc, pi)
75 CORE_ADDR pc;
76 struct prologue_info *pi;
77{
78 CORE_ADDR func_addr, prologue_end, current_pc;
a638512f 79 struct pifsr *pifsr;
23da411a 80 int fp_used;
e5a2ac8b
SG
81
82 /* First, figure out the bounds of the prologue so that we can limit the
83 search to something reasonable. */
84
23da411a 85 if (find_pc_partial_function (pc, NULL, &func_addr, NULL))
e5a2ac8b 86 {
23da411a
SG
87 struct symtab_and_line sal;
88
e5a2ac8b
SG
89 sal = find_pc_line (func_addr, 0);
90
23da411a
SG
91 if (func_addr == entry_point_address ())
92 pi->start_function = 1;
93 else
94 pi->start_function = 0;
95
a638512f 96#if 0
ac954805 97 if (sal.line == 0)
23da411a 98 prologue_end = pc;
ac954805
SG
99 else
100 prologue_end = sal.end;
a638512f
SG
101#else
102 prologue_end = pc;
103#endif
e5a2ac8b
SG
104 }
105 else
23da411a
SG
106 { /* We're in the boondocks */
107 func_addr = pc - 100;
108 prologue_end = pc;
109 }
e5a2ac8b 110
23da411a 111 prologue_end = min (prologue_end, pc);
e5a2ac8b
SG
112
113 /* Now, search the prologue looking for instructions that setup fp, save
23da411a
SG
114 rp, adjust sp and such. We also record the frame offset of any saved
115 registers. */
e5a2ac8b 116
23da411a
SG
117 pi->frameoffset = 0;
118 pi->framereg = SP_REGNUM;
119 fp_used = 0;
a638512f 120 pifsr = pi->pifsrs;
e5a2ac8b
SG
121
122 for (current_pc = func_addr; current_pc < prologue_end; current_pc += 2)
123 {
124 int insn;
125
6420594b 126 insn = read_memory_unsigned_integer (current_pc, 2);
e5a2ac8b 127
a638512f
SG
128 if ((insn & 0x07c0) == 0x0780 /* jarl or jr */
129 || (insn & 0xffe0) == 0x0060 /* jmp */
130 || (insn & 0x0780) == 0x0580) /* branch */
131 break; /* Ran into end of prologue */
e5a2ac8b 132 if ((insn & 0xffe0) == ((SP_REGNUM << 11) | 0x0240)) /* add <imm>,sp */
23da411a 133 pi->frameoffset = ((insn & 0x1f) ^ 0x10) - 0x10;
e5a2ac8b 134 else if (insn == ((SP_REGNUM << 11) | 0x0600 | SP_REGNUM)) /* addi <imm>,sp,sp */
23da411a
SG
135 pi->frameoffset = read_memory_integer (current_pc + 2, 2);
136 else if (insn == ((FP_REGNUM << 11) | 0x0000 | 12)) /* mov r12,fp */
e5a2ac8b 137 {
23da411a
SG
138 fp_used = 1;
139 pi->framereg = FP_REGNUM;
140 }
141 else if ((insn & 0x07ff) == (0x0760 | SP_REGNUM) /* st.w <reg>,<offset>[sp] */
142 || (fp_used
143 && (insn & 0x07ff) == (0x0760 | FP_REGNUM))) /* st.w <reg>,<offset>[fp] */
a638512f 144 if (pifsr)
23da411a 145 {
a638512f
SG
146 pifsr->framereg = insn & 0x1f;
147 pifsr->reg = (insn >> 11) & 0x1f; /* Extract <reg> */
e5a2ac8b 148
a638512f 149 pifsr->offset = read_memory_integer (current_pc + 2, 2) & ~1;
e5a2ac8b 150
a638512f 151 pifsr++;
23da411a 152 }
6420594b
SG
153
154 if ((insn & 0x0780) >= 0x0600) /* Four byte instruction? */
155 current_pc += 2;
e5a2ac8b
SG
156 }
157
a638512f
SG
158 if (pifsr)
159 pifsr->framereg = 0; /* Tie off last entry */
160
23da411a 161 return current_pc;
e5a2ac8b
SG
162}
163
23da411a
SG
164/* Setup the frame frame pointer, pc, and frame addresses for saved registers.
165 Most of the work is done in scan_prologue().
e5a2ac8b 166
23da411a
SG
167 Note that when we are called for the last frame (currently active frame),
168 that fi->pc and fi->frame will already be setup. However, fi->frame will
169 be valid only if this routine uses FP. For previous frames, fi-frame will
170 always be correct (since that is derived from v850_frame_chain ()).
171
172 We can be called with the PC in the call dummy under two circumstances.
173 First, during normal backtracing, second, while figuring out the frame
174 pointer just prior to calling the target function (see run_stack_dummy).
175 */
176
177void
178v850_init_extra_frame_info (fi)
e5a2ac8b 179 struct frame_info *fi;
e5a2ac8b 180{
23da411a 181 struct prologue_info pi;
a638512f 182 struct pifsr pifsrs[NUM_REGS + 1], *pifsr;
23da411a
SG
183 int reg;
184
185 if (fi->next)
186 fi->pc = FRAME_SAVED_PC (fi->next);
187
188 memset (fi->fsr.regs, '\000', sizeof fi->fsr.regs);
189
190 /* The call dummy doesn't save any registers on the stack, so we can return
191 now. */
ac954805 192 if (PC_IN_CALL_DUMMY (fi->pc, NULL, NULL))
23da411a
SG
193 {
194 /* We need to setup fi->frame here because run_stack_dummy gets it wrong
195 by assuming it's always FP. */
196 fi->frame = read_register_dummy (SP_REGNUM);
197 return;
198 }
ac954805 199
a638512f 200 pi.pifsrs = pifsrs;
e5a2ac8b 201
23da411a
SG
202 scan_prologue (fi->pc, &pi);
203
a638512f
SG
204 if (!fi->next && pi.framereg == SP_REGNUM)
205 fi->frame = read_register (pi.framereg) - pi.frameoffset;
23da411a 206
a638512f
SG
207 for (pifsr = pifsrs; pifsr->framereg; pifsr++)
208 {
209 fi->fsr.regs[pifsr->reg] = pifsr->offset + fi->frame;
210
211 if (pifsr->framereg == SP_REGNUM)
212 fi->fsr.regs[pifsr->reg] += pi.frameoffset;
213 }
e5a2ac8b
SG
214}
215
23da411a
SG
216/* Figure out the frame prior to FI. Unfortunately, this involves scanning the
217 prologue of the caller, which will also be done shortly by
218 v850_init_extra_frame_info. For the dummy frame, we just return the stack
219 pointer that was in use at the time the function call was made. */
220
e5a2ac8b
SG
221CORE_ADDR
222v850_frame_chain (fi)
223 struct frame_info *fi;
224{
23da411a
SG
225 CORE_ADDR callers_pc;
226 struct prologue_info pi;
e5a2ac8b
SG
227
228 /* First, find out who called us */
229
ac954805
SG
230 callers_pc = FRAME_SAVED_PC (fi);
231
232 if (PC_IN_CALL_DUMMY (callers_pc, NULL, NULL))
23da411a 233 return read_register_dummy (SP_REGNUM); /* XXX Won't work if multiple dummy frames on stack! */
e5a2ac8b 234
a638512f 235 pi.pifsrs = NULL;
e5a2ac8b 236
23da411a 237 scan_prologue (callers_pc, &pi);
e5a2ac8b 238
23da411a
SG
239 if (pi.start_function)
240 return 0; /* Don't chain beyond the start function */
ac954805 241
23da411a
SG
242 if (pi.framereg == FP_REGNUM)
243 return v850_find_callers_reg (fi, pi.framereg);
e5a2ac8b 244
23da411a
SG
245 return fi->frame - pi.frameoffset;
246}
e5a2ac8b 247
23da411a
SG
248/* Find REGNUM on the stack. Otherwise, it's in an active register. One thing
249 we might want to do here is to check REGNUM against the clobber mask, and
250 somehow flag it as invalid if it isn't saved on the stack somewhere. This
251 would provide a graceful failure mode when trying to get the value of
252 caller-saves registers for an inner frame. */
e5a2ac8b 253
23da411a
SG
254CORE_ADDR
255v850_find_callers_reg (fi, regnum)
256 struct frame_info *fi;
257 int regnum;
258{
259 /* XXX - Won't work if multiple dummy frames are active */
260 /* When the caller requests RP from the dummy frame, we return PC because
261 that's where the previous routine appears to have done a call from. */
262 if (PC_IN_CALL_DUMMY (fi->pc, NULL, NULL))
263 if (regnum == RP_REGNUM)
264 regnum = PC_REGNUM;
6420594b 265
23da411a
SG
266 for (; fi; fi = fi->next)
267 if (PC_IN_CALL_DUMMY (fi->pc, NULL, NULL))
268 return read_register_dummy (regnum);
269 else if (fi->fsr.regs[regnum] != 0)
270 return read_memory_integer (fi->fsr.regs[regnum], 4);
e5a2ac8b 271
23da411a 272 return read_register (regnum);
e5a2ac8b
SG
273}
274
275CORE_ADDR
276v850_skip_prologue (pc)
277 CORE_ADDR pc;
278{
279 CORE_ADDR func_addr, func_end;
280
281 /* See what the symbol table says */
282
283 if (find_pc_partial_function (pc, NULL, &func_addr, &func_end))
284 {
285 struct symtab_and_line sal;
286
287 sal = find_pc_line (func_addr, 0);
288
ac954805 289 if (sal.line != 0 && sal.end < func_end)
e5a2ac8b
SG
290 return sal.end;
291 else
ac954805
SG
292 /* Either there's no line info, or the line after the prologue is after
293 the end of the function. In this case, there probably isn't a
294 prologue. */
e5a2ac8b
SG
295 return pc;
296 }
297
298/* We can't find the start of this function, so there's nothing we can do. */
299 return pc;
300}
301
23da411a
SG
302/* Save all the registers on the dummy frame stack. Most ports save the
303 registers on the target stack. This results in lots of unnecessary memory
304 references, which are slow when debugging via a serial line. Instead, we
305 save all the registers internally, and never write them to the stack. The
306 registers get restored when the called function returns to the entry point,
307 where a breakpoint is laying in wait. */
ac954805
SG
308
309void
310v850_push_dummy_frame ()
311{
312 struct dummy_frame *dummy_frame;
313
314 dummy_frame = xmalloc (sizeof (struct dummy_frame));
315
23da411a
SG
316 read_register_bytes (0, dummy_frame->regs, REGISTER_BYTES);
317
ac954805
SG
318 dummy_frame->next = dummy_frame_stack;
319 dummy_frame_stack = dummy_frame;
320}
321
23da411a
SG
322/* Read registers from the topmost dummy frame. */
323
324CORE_ADDR
325read_register_dummy (regno)
326 int regno;
327{
328 return extract_address (&dummy_frame_stack->regs[REGISTER_BYTE (regno)],
329 REGISTER_RAW_SIZE(regno));
330}
331
ac954805
SG
332int
333v850_pc_in_call_dummy (pc)
334 CORE_ADDR pc;
335{
336 return dummy_frame_stack
337 && pc >= CALL_DUMMY_ADDRESS ()
338 && pc <= CALL_DUMMY_ADDRESS () + DECR_PC_AFTER_BREAK;
339}
340
23da411a
SG
341/* This routine gets called when either the user uses the `return' command, or
342 the call dummy breakpoint gets hit. */
343
e5a2ac8b
SG
344struct frame_info *
345v850_pop_frame (frame)
346 struct frame_info *frame;
347{
348 int regnum;
349
ac954805
SG
350 if (PC_IN_CALL_DUMMY (frame->pc, NULL, NULL))
351 {
352 struct dummy_frame *dummy_frame;
353
354 dummy_frame = dummy_frame_stack;
355 if (!dummy_frame)
356 error ("Can't pop dummy frame!");
357
358 dummy_frame_stack = dummy_frame->next;
359
23da411a 360 write_register_bytes (0, dummy_frame->regs, REGISTER_BYTES);
ac954805
SG
361
362 free (dummy_frame);
ac954805 363 }
23da411a
SG
364 else
365 {
366 write_register (PC_REGNUM, FRAME_SAVED_PC (frame));
ac954805 367
23da411a
SG
368 for (regnum = 0; regnum < NUM_REGS; regnum++)
369 if (frame->fsr.regs[regnum] != 0)
370 write_register (regnum,
371 read_memory_integer (frame->fsr.regs[regnum], 4));
e5a2ac8b 372
23da411a
SG
373 write_register (SP_REGNUM, FRAME_FP (frame));
374 }
e5a2ac8b 375
e5a2ac8b
SG
376 flush_cached_frames ();
377
378 return NULL;
379}
ac954805 380
23da411a 381/* Setup arguments and RP for a call to the target. First four args go in
ac954805
SG
382 R6->R9, subsequent args go into sp + 16 -> sp + ... Structs are passed by
383 reference. 64 bit quantities (doubles and long longs) may be split between
384 the regs and the stack. When calling a function that returns a struct, a
385 pointer to the struct is passed in as a secret first argument (always in R6).
386
387 By the time we get here, stack space has been allocated for the args, but
388 not for the struct return pointer. */
389
390CORE_ADDR
391v850_push_arguments (nargs, args, sp, struct_return, struct_addr)
392 int nargs;
393 value_ptr *args;
394 CORE_ADDR sp;
395 unsigned char struct_return;
396 CORE_ADDR struct_addr;
397{
398 int argreg;
399 int argnum;
400
687f4e23 401 argreg = ARG0_REGNUM;
ac954805
SG
402
403 if (struct_return)
404 {
405 write_register (argreg++, struct_addr);
406 sp -= 4;
407 }
408
409 for (argnum = 0; argnum < nargs; argnum++)
410 {
411 int len;
412 char *val;
413 char valbuf[4];
414
415 if (TYPE_CODE (VALUE_TYPE (*args)) == TYPE_CODE_STRUCT
416 && TYPE_LENGTH (VALUE_TYPE (*args)) > 8)
417 {
418 store_address (valbuf, 4, VALUE_ADDRESS (*args));
419 len = 4;
420 val = valbuf;
421 }
422 else
423 {
424 len = TYPE_LENGTH (VALUE_TYPE (*args));
425 val = (char *)VALUE_CONTENTS (*args);
426 }
427
428 while (len > 0)
687f4e23 429 if (argreg <= ARGLAST_REGNUM)
ac954805
SG
430 {
431 CORE_ADDR regval;
432
433 regval = extract_address (val, REGISTER_RAW_SIZE (argreg));
434 write_register (argreg, regval);
435
436 len -= REGISTER_RAW_SIZE (argreg);
437 val += REGISTER_RAW_SIZE (argreg);
438 argreg++;
439 }
440 else
441 {
442 write_memory (sp + argnum * 4, val, 4);
443
444 len -= 4;
445 val += 4;
446 }
447 args++;
448 }
449
450 write_register (RP_REGNUM, entry_point_address ());
451
452 return sp;
453}
e2810631
SG
454\f
455void
456_initialize_sparc_tdep ()
457{
458 tm_print_insn = print_insn_v850;
459}
This page took 0.049793 seconds and 4 git commands to generate.