* valprint.c (print_longest): Don't lose upper bits
[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
23da411a
SG
31/* Info gleaned from scanning a function's prologue. */
32
a638512f
SG
33struct pifsr /* Info about one saved reg */
34{
35 int framereg; /* Frame reg (SP or FP) */
36 int offset; /* Offset from framereg */
37 int reg; /* Saved register number */
38};
39
23da411a 40struct prologue_info
e5a2ac8b 41{
e5a2ac8b 42 int framereg;
23da411a
SG
43 int frameoffset;
44 int start_function;
a638512f 45 struct pifsr *pifsrs;
23da411a 46};
e5a2ac8b 47
dc1b349d
MS
48static CORE_ADDR v850_scan_prologue PARAMS ((CORE_ADDR pc,
49 struct prologue_info *fs));
23da411a 50\f
dc1b349d
MS
51/* Function: scan_prologue
52 Scan the prologue of the function that contains PC, and record what
53 we find in PI. PI->fsr must be zeroed by the called. Returns the
54 pc after the prologue. Note that the addresses saved in pi->fsr
55 are actually just frame relative (negative offsets from the frame
56 pointer). This is because we don't know the actual value of the
57 frame pointer yet. In some circumstances, the frame pointer can't
58 be determined till after we have scanned the prologue. */
23da411a
SG
59
60static CORE_ADDR
dc1b349d 61v850_scan_prologue (pc, pi)
23da411a
SG
62 CORE_ADDR pc;
63 struct prologue_info *pi;
64{
65 CORE_ADDR func_addr, prologue_end, current_pc;
a638512f 66 struct pifsr *pifsr;
23da411a 67 int fp_used;
b71f8719 68 int ep_used;
e5a2ac8b
SG
69
70 /* First, figure out the bounds of the prologue so that we can limit the
71 search to something reasonable. */
72
23da411a 73 if (find_pc_partial_function (pc, NULL, &func_addr, NULL))
e5a2ac8b 74 {
23da411a
SG
75 struct symtab_and_line sal;
76
e5a2ac8b
SG
77 sal = find_pc_line (func_addr, 0);
78
23da411a
SG
79 if (func_addr == entry_point_address ())
80 pi->start_function = 1;
81 else
82 pi->start_function = 0;
83
a638512f 84#if 0
ac954805 85 if (sal.line == 0)
23da411a 86 prologue_end = pc;
ac954805
SG
87 else
88 prologue_end = sal.end;
a638512f
SG
89#else
90 prologue_end = pc;
91#endif
e5a2ac8b
SG
92 }
93 else
23da411a
SG
94 { /* We're in the boondocks */
95 func_addr = pc - 100;
96 prologue_end = pc;
97 }
e5a2ac8b 98
23da411a 99 prologue_end = min (prologue_end, pc);
e5a2ac8b
SG
100
101 /* Now, search the prologue looking for instructions that setup fp, save
23da411a
SG
102 rp, adjust sp and such. We also record the frame offset of any saved
103 registers. */
e5a2ac8b 104
23da411a
SG
105 pi->frameoffset = 0;
106 pi->framereg = SP_REGNUM;
107 fp_used = 0;
b71f8719 108 ep_used = 0;
a638512f 109 pifsr = pi->pifsrs;
e5a2ac8b
SG
110
111 for (current_pc = func_addr; current_pc < prologue_end; current_pc += 2)
112 {
113 int insn;
114
6420594b 115 insn = read_memory_unsigned_integer (current_pc, 2);
e5a2ac8b 116
a638512f
SG
117 if ((insn & 0x07c0) == 0x0780 /* jarl or jr */
118 || (insn & 0xffe0) == 0x0060 /* jmp */
119 || (insn & 0x0780) == 0x0580) /* branch */
120 break; /* Ran into end of prologue */
e5a2ac8b 121 if ((insn & 0xffe0) == ((SP_REGNUM << 11) | 0x0240)) /* add <imm>,sp */
23da411a 122 pi->frameoffset = ((insn & 0x1f) ^ 0x10) - 0x10;
e5a2ac8b 123 else if (insn == ((SP_REGNUM << 11) | 0x0600 | SP_REGNUM)) /* addi <imm>,sp,sp */
23da411a
SG
124 pi->frameoffset = read_memory_integer (current_pc + 2, 2);
125 else if (insn == ((FP_REGNUM << 11) | 0x0000 | 12)) /* mov r12,fp */
e5a2ac8b 126 {
23da411a
SG
127 fp_used = 1;
128 pi->framereg = FP_REGNUM;
129 }
b71f8719
MM
130 else if (insn == 0xf003) /* mov sp,ep */
131 ep_used = 1;
132 else if (insn == 0xf001) /* mov r1,ep */
133 ep_used = 0;
134 else if (((insn & 0x07ff) == (0x0760 | SP_REGNUM) /* st.w <reg>,<offset>[sp] */
135 || (fp_used
136 && (insn & 0x07ff) == (0x0760 | FP_REGNUM))) /* st.w <reg>,<offset>[fp] */
137 && pifsr)
138 {
139 pifsr->framereg = insn & 0x1f;
140 pifsr->reg = (insn >> 11) & 0x1f; /* Extract <reg> */
141 pifsr->offset = read_memory_integer (current_pc + 2, 2) & ~1;
142 pifsr++;
143 }
144
145 else if (ep_used /* sst.w <reg>,<offset>[ep] */
146 && ((insn & 0x0781) == 0x0501)
147 && pifsr)
148 {
149 pifsr->framereg = 3;
150 pifsr->reg = (insn >> 11) & 0x1f; /* Extract <reg> */
151 pifsr->offset = (insn & 0x007e) << 2;
152 pifsr++;
153 }
6420594b
SG
154
155 if ((insn & 0x0780) >= 0x0600) /* Four byte instruction? */
156 current_pc += 2;
e5a2ac8b
SG
157 }
158
a638512f
SG
159 if (pifsr)
160 pifsr->framereg = 0; /* Tie off last entry */
161
23da411a 162 return current_pc;
e5a2ac8b
SG
163}
164
dc1b349d
MS
165/* Function: init_extra_frame_info
166 Setup the frame's frame pointer, pc, and frame addresses for saved
167 registers. Most of the work is done in scan_prologue().
e5a2ac8b 168
23da411a
SG
169 Note that when we are called for the last frame (currently active frame),
170 that fi->pc and fi->frame will already be setup. However, fi->frame will
171 be valid only if this routine uses FP. For previous frames, fi-frame will
172 always be correct (since that is derived from v850_frame_chain ()).
173
174 We can be called with the PC in the call dummy under two circumstances.
175 First, during normal backtracing, second, while figuring out the frame
dc1b349d 176 pointer just prior to calling the target function (see run_stack_dummy). */
23da411a
SG
177
178void
179v850_init_extra_frame_info (fi)
e5a2ac8b 180 struct frame_info *fi;
e5a2ac8b 181{
23da411a 182 struct prologue_info pi;
a638512f 183 struct pifsr pifsrs[NUM_REGS + 1], *pifsr;
23da411a
SG
184 int reg;
185
186 if (fi->next)
187 fi->pc = FRAME_SAVED_PC (fi->next);
188
189 memset (fi->fsr.regs, '\000', sizeof fi->fsr.regs);
190
191 /* The call dummy doesn't save any registers on the stack, so we can return
192 now. */
dc1b349d 193 if (PC_IN_CALL_DUMMY (fi->pc, fi->frame, fi->frame))
23da411a 194 return;
ac954805 195
a638512f 196 pi.pifsrs = pifsrs;
e5a2ac8b 197
dc1b349d 198 v850_scan_prologue (fi->pc, &pi);
23da411a 199
a638512f
SG
200 if (!fi->next && pi.framereg == SP_REGNUM)
201 fi->frame = read_register (pi.framereg) - pi.frameoffset;
23da411a 202
a638512f
SG
203 for (pifsr = pifsrs; pifsr->framereg; pifsr++)
204 {
205 fi->fsr.regs[pifsr->reg] = pifsr->offset + fi->frame;
206
207 if (pifsr->framereg == SP_REGNUM)
208 fi->fsr.regs[pifsr->reg] += pi.frameoffset;
209 }
e5a2ac8b
SG
210}
211
dc1b349d
MS
212/* Function: frame_chain
213 Figure out the frame prior to FI. Unfortunately, this involves
214 scanning the prologue of the caller, which will also be done
215 shortly by v850_init_extra_frame_info. For the dummy frame, we
216 just return the stack pointer that was in use at the time the
217 function call was made. */
23da411a 218
e5a2ac8b
SG
219CORE_ADDR
220v850_frame_chain (fi)
221 struct frame_info *fi;
222{
23da411a 223 struct prologue_info pi;
dc1b349d 224 CORE_ADDR callers_pc, fp;
e5a2ac8b
SG
225
226 /* First, find out who called us */
ac954805 227 callers_pc = FRAME_SAVED_PC (fi);
dc1b349d
MS
228 /* If caller is a call-dummy, then our FP bears no relation to his FP! */
229 fp = v850_find_callers_reg (fi, FP_REGNUM);
230 if (PC_IN_CALL_DUMMY(callers_pc, fp, fp))
231 return fp; /* caller is call-dummy: return oldest value of FP */
ac954805 232
dc1b349d
MS
233 /* Caller is NOT a call-dummy, so everything else should just work.
234 Even if THIS frame is a call-dummy! */
a638512f 235 pi.pifsrs = NULL;
e5a2ac8b 236
dc1b349d 237 v850_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
dc1b349d
MS
248/* Function: find_callers_reg
249 Find REGNUM on the stack. Otherwise, it's in an active register.
250 One thing we might want to do here is to check REGNUM against the
251 clobber mask, and somehow flag it as invalid if it isn't saved on
252 the stack somewhere. This would provide a graceful failure mode
253 when trying to get the value of caller-saves registers for an inner
254 frame. */
e5a2ac8b 255
23da411a
SG
256CORE_ADDR
257v850_find_callers_reg (fi, regnum)
258 struct frame_info *fi;
259 int regnum;
260{
23da411a 261 for (; fi; fi = fi->next)
dc1b349d
MS
262 if (PC_IN_CALL_DUMMY (fi->pc, fi->frame, fi->frame))
263 return generic_read_register_dummy (fi->pc, fi->frame, regnum);
23da411a 264 else if (fi->fsr.regs[regnum] != 0)
dc1b349d
MS
265 return read_memory_unsigned_integer (fi->fsr.regs[regnum],
266 REGISTER_RAW_SIZE(regnum));
e5a2ac8b 267
23da411a 268 return read_register (regnum);
e5a2ac8b
SG
269}
270
dc1b349d
MS
271/* Function: skip_prologue
272 Return the address of the first code past the prologue of the function. */
273
e5a2ac8b
SG
274CORE_ADDR
275v850_skip_prologue (pc)
276 CORE_ADDR pc;
277{
278 CORE_ADDR func_addr, func_end;
279
280 /* See what the symbol table says */
281
282 if (find_pc_partial_function (pc, NULL, &func_addr, &func_end))
283 {
284 struct symtab_and_line sal;
285
286 sal = find_pc_line (func_addr, 0);
287
ac954805 288 if (sal.line != 0 && sal.end < func_end)
e5a2ac8b
SG
289 return sal.end;
290 else
ac954805
SG
291 /* Either there's no line info, or the line after the prologue is after
292 the end of the function. In this case, there probably isn't a
293 prologue. */
e5a2ac8b
SG
294 return pc;
295 }
296
297/* We can't find the start of this function, so there's nothing we can do. */
298 return pc;
299}
300
dc1b349d
MS
301/* Function: pop_frame
302 This routine gets called when either the user uses the `return'
303 command, or the call dummy breakpoint gets hit. */
ac954805
SG
304
305void
e5a2ac8b
SG
306v850_pop_frame (frame)
307 struct frame_info *frame;
308{
309 int regnum;
310
dc1b349d
MS
311 if (PC_IN_CALL_DUMMY(frame->pc, frame->frame, frame->frame))
312 generic_pop_dummy_frame ();
23da411a
SG
313 else
314 {
315 write_register (PC_REGNUM, FRAME_SAVED_PC (frame));
ac954805 316
23da411a
SG
317 for (regnum = 0; regnum < NUM_REGS; regnum++)
318 if (frame->fsr.regs[regnum] != 0)
319 write_register (regnum,
dc1b349d
MS
320 read_memory_unsigned_integer (frame->fsr.regs[regnum],
321 REGISTER_RAW_SIZE(regnum)));
e5a2ac8b 322
23da411a
SG
323 write_register (SP_REGNUM, FRAME_FP (frame));
324 }
e5a2ac8b 325
e5a2ac8b 326 flush_cached_frames ();
e5a2ac8b 327}
ac954805 328
dc1b349d
MS
329/* Function: push_arguments
330 Setup arguments and RP for a call to the target. First four args
331 go in R6->R9, subsequent args go into sp + 16 -> sp + ... Structs
332 are passed by reference. 64 bit quantities (doubles and long
333 longs) may be split between the regs and the stack. When calling a
334 function that returns a struct, a pointer to the struct is passed
335 in as a secret first argument (always in R6).
ac954805 336
dc1b349d
MS
337 Stack space for the args has NOT been allocated: that job is up to us.
338 */
ac954805
SG
339
340CORE_ADDR
341v850_push_arguments (nargs, args, sp, struct_return, struct_addr)
342 int nargs;
343 value_ptr *args;
344 CORE_ADDR sp;
345 unsigned char struct_return;
346 CORE_ADDR struct_addr;
347{
348 int argreg;
349 int argnum;
dc1b349d
MS
350 int len = 0;
351 int stack_offset;
ac954805 352
dc1b349d
MS
353 /* First, just for safety, make sure stack is aligned */
354 sp &= ~3;
355
356 /* Now make space on the stack for the args. */
357 for (argnum = 0; argnum < nargs; argnum++)
358 len += ((TYPE_LENGTH(VALUE_TYPE(args[argnum])) + 3) & ~3);
359 sp -= len; /* possibly over-allocating, but it works... */
360 /* (you might think we could allocate 16 bytes */
361 /* less, but the ABI seems to use it all! ) */
687f4e23 362 argreg = ARG0_REGNUM;
ac954805 363
dc1b349d 364 /* the struct_return pointer occupies the first parameter-passing reg */
ac954805 365 if (struct_return)
ac954805 366 write_register (argreg++, struct_addr);
ac954805 367
dc1b349d
MS
368 stack_offset = 16;
369 /* The offset onto the stack at which we will start copying parameters
370 (after the registers are used up) begins at 16 rather than at zero.
371 I don't really know why, that's just the way it seems to work. */
372
373 /* Now load as many as possible of the first arguments into
374 registers, and push the rest onto the stack. There are 16 bytes
375 in four registers available. Loop thru args from first to last. */
ac954805
SG
376 for (argnum = 0; argnum < nargs; argnum++)
377 {
378 int len;
379 char *val;
dc1b349d 380 char valbuf[REGISTER_RAW_SIZE(ARG0_REGNUM)];
ac954805
SG
381
382 if (TYPE_CODE (VALUE_TYPE (*args)) == TYPE_CODE_STRUCT
383 && TYPE_LENGTH (VALUE_TYPE (*args)) > 8)
384 {
385 store_address (valbuf, 4, VALUE_ADDRESS (*args));
386 len = 4;
387 val = valbuf;
388 }
389 else
390 {
391 len = TYPE_LENGTH (VALUE_TYPE (*args));
392 val = (char *)VALUE_CONTENTS (*args);
393 }
394
395 while (len > 0)
687f4e23 396 if (argreg <= ARGLAST_REGNUM)
ac954805
SG
397 {
398 CORE_ADDR regval;
399
400 regval = extract_address (val, REGISTER_RAW_SIZE (argreg));
401 write_register (argreg, regval);
402
403 len -= REGISTER_RAW_SIZE (argreg);
404 val += REGISTER_RAW_SIZE (argreg);
405 argreg++;
406 }
407 else
408 {
dc1b349d 409 write_memory (sp + stack_offset, val, 4);
ac954805
SG
410
411 len -= 4;
412 val += 4;
dc1b349d 413 stack_offset += 4;
ac954805
SG
414 }
415 args++;
416 }
dc1b349d
MS
417 return sp;
418}
ac954805 419
dc1b349d
MS
420/* Function: push_return_address (pc)
421 Set up the return address for the inferior function call.
422 Needed for targets where we don't actually execute a JSR/BSR instruction */
423
dc1b349d
MS
424CORE_ADDR
425v850_push_return_address (pc, sp)
426 CORE_ADDR pc;
427 CORE_ADDR sp;
428{
409f64ae 429 write_register (RP_REGNUM, CALL_DUMMY_ADDRESS ());
ac954805
SG
430 return sp;
431}
dc1b349d
MS
432
433/* Function: frame_saved_pc
434 Find the caller of this frame. We do this by seeing if RP_REGNUM
435 is saved in the stack anywhere, otherwise we get it from the
436 registers. If the inner frame is a dummy frame, return its PC
437 instead of RP, because that's where "caller" of the dummy-frame
438 will be found. */
439
440CORE_ADDR
441v850_frame_saved_pc (fi)
442 struct frame_info *fi;
443{
444 if (PC_IN_CALL_DUMMY(fi->pc, fi->frame, fi->frame))
445 return generic_read_register_dummy(fi->pc, fi->frame, PC_REGNUM);
446 else
447 return v850_find_callers_reg (fi, RP_REGNUM);
448}
449
450void
451get_saved_register (raw_buffer, optimized, addrp, frame, regnum, lval)
452 char *raw_buffer;
453 int *optimized;
454 CORE_ADDR *addrp;
455 struct frame_info *frame;
456 int regnum;
457 enum lval_type *lval;
458{
459 generic_get_saved_register (raw_buffer, optimized, addrp,
460 frame, regnum, lval);
461}
462
463
464/* Function: fix_call_dummy
465 Pokes the callee function's address into the CALL_DUMMY assembly stub.
466 Assumes that the CALL_DUMMY looks like this:
467 jarl <offset24>, r31
468 trap
469 */
470
471int
472v850_fix_call_dummy (dummy, sp, fun, nargs, args, type, gcc_p)
473 char *dummy;
474 CORE_ADDR sp;
475 CORE_ADDR fun;
476 int nargs;
477 value_ptr *args;
478 struct type *type;
479 int gcc_p;
480{
481 long offset24;
dc1b349d 482
409f64ae 483 offset24 = (long) fun - (long) entry_point_address ();
dc1b349d
MS
484 offset24 &= 0x3fffff;
485 offset24 |= 0xff800000; /* jarl <offset24>, r31 */
486
487 store_unsigned_integer ((unsigned int *)&dummy[2], 2, offset24 & 0xffff);
488 store_unsigned_integer ((unsigned int *)&dummy[0], 2, offset24 >> 16);
489 return 0;
490}
491
e2810631 492void
dc1b349d 493_initialize_v850_tdep ()
e2810631
SG
494{
495 tm_print_insn = print_insn_v850;
496}
This page took 0.056539 seconds and 4 git commands to generate.