* Sanitization fixes to retain new files.
[binutils-gdb.git] / gdb / v850-tdep.c
blob082472405e7713bfccf5146490664f270ab34817
1 /* Target-dependent code for the NEC V850 for GDB, the GNU debugger.
2 Copyright 1996, Free Software Foundation, Inc.
4 This file is part of GDB.
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.
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.
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. */
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 #include "symfile.h"
31 /* Info gleaned from scanning a function's prologue. */
33 struct pifsr /* Info about one saved reg */
35 int framereg; /* Frame reg (SP or FP) */
36 int offset; /* Offset from framereg */
37 int cur_frameoffset; /* Current frameoffset */
38 int reg; /* Saved register number */
41 struct prologue_info
43 int framereg;
44 int frameoffset;
45 int start_function;
46 struct pifsr *pifsrs;
49 static CORE_ADDR v850_scan_prologue PARAMS ((CORE_ADDR pc,
50 struct prologue_info *fs));
53 /* Should call_function allocate stack space for a struct return? */
54 int
55 v850_use_struct_convention (gcc_p, type)
56 int gcc_p;
57 struct type *type;
59 return (TYPE_NFIELDS (type) > 1 || TYPE_LENGTH (type) > 4);
63 /* Function: scan_prologue
64 Scan the prologue of the function that contains PC, and record what
65 we find in PI. PI->fsr must be zeroed by the called. Returns the
66 pc after the prologue. Note that the addresses saved in pi->fsr
67 are actually just frame relative (negative offsets from the frame
68 pointer). This is because we don't know the actual value of the
69 frame pointer yet. In some circumstances, the frame pointer can't
70 be determined till after we have scanned the prologue. */
72 static CORE_ADDR
73 v850_scan_prologue (pc, pi)
74 CORE_ADDR pc;
75 struct prologue_info *pi;
77 CORE_ADDR func_addr, prologue_end, current_pc;
78 struct pifsr *pifsr, *pifsr_tmp;
79 int fp_used;
80 int ep_used;
81 int reg;
82 CORE_ADDR save_pc, save_end;
83 int regsave_func_p;
84 int current_sp_size;
85 int r12_tmp;
87 /* First, figure out the bounds of the prologue so that we can limit the
88 search to something reasonable. */
90 if (find_pc_partial_function (pc, NULL, &func_addr, NULL))
92 struct symtab_and_line sal;
94 sal = find_pc_line (func_addr, 0);
96 if (func_addr == entry_point_address ())
97 pi->start_function = 1;
98 else
99 pi->start_function = 0;
101 #if 0
102 if (sal.line == 0)
103 prologue_end = pc;
104 else
105 prologue_end = sal.end;
106 #else
107 prologue_end = pc;
108 #endif
110 else
111 { /* We're in the boondocks */
112 func_addr = pc - 100;
113 prologue_end = pc;
116 prologue_end = min (prologue_end, pc);
118 /* Now, search the prologue looking for instructions that setup fp, save
119 rp, adjust sp and such. We also record the frame offset of any saved
120 registers. */
122 pi->frameoffset = 0;
123 pi->framereg = SP_REGNUM;
124 fp_used = 0;
125 ep_used = 0;
126 pifsr = pi->pifsrs;
127 regsave_func_p = 0;
128 save_pc = 0;
129 save_end = 0;
130 r12_tmp = 0;
132 #ifdef DEBUG
133 printf_filtered ("Current_pc = 0x%.8lx, prologue_end = 0x%.8lx\n",
134 (long)func_addr, (long)prologue_end);
135 #endif
137 for (current_pc = func_addr; current_pc < prologue_end; current_pc += 2)
139 int insn;
141 #ifdef DEBUG
142 printf_filtered ("0x%.8lx ", (long)current_pc);
143 (*tm_print_insn) (current_pc, &tm_print_insn_info);
144 #endif
146 insn = read_memory_unsigned_integer (current_pc, 2);
148 if ((insn & 0xffc0) == ((10 << 11) | 0x0780) && !regsave_func_p)
149 { /* jarl <func>,10 */
150 long low_disp = read_memory_unsigned_integer (current_pc + 2, 2) & ~ (long) 1;
151 long disp = (((((insn & 0x3f) << 16) + low_disp)
152 & ~ (long) 1) ^ 0x00200000) - 0x00200000;
154 save_pc = current_pc;
155 save_end = prologue_end;
156 regsave_func_p = 1;
157 current_pc += disp - 2;
158 prologue_end = (current_pc
159 + (2 * 3) /* moves to/from ep */
160 + 4 /* addi <const>,sp,sp */
161 + 2 /* jmp [r10] */
162 + (2 * 12) /* sst.w to save r2, r20-r29, r31 */
163 + 20); /* slop area */
165 #ifdef DEBUG
166 printf_filtered ("\tfound jarl <func>,r10, disp = %ld, low_disp = %ld, new pc = 0x%.8lx\n",
167 disp, low_disp, (long)current_pc + 2);
168 #endif
169 continue;
171 else if ((insn & 0xffe0) == 0x0060 && regsave_func_p)
172 { /* jmp after processing register save function */
173 current_pc = save_pc + 2;
174 prologue_end = save_end;
175 regsave_func_p = 0;
176 #ifdef DEBUG
177 printf_filtered ("\tfound jmp after regsave func");
178 #endif
180 else if ((insn & 0x07c0) == 0x0780 /* jarl or jr */
181 || (insn & 0xffe0) == 0x0060 /* jmp */
182 || (insn & 0x0780) == 0x0580) /* branch */
184 #ifdef DEBUG
185 printf_filtered ("\n");
186 #endif
187 break; /* Ran into end of prologue */
190 else if ((insn & 0xffe0) == ((SP_REGNUM << 11) | 0x0240)) /* add <imm>,sp */
191 pi->frameoffset += ((insn & 0x1f) ^ 0x10) - 0x10;
192 else if (insn == ((SP_REGNUM << 11) | 0x0600 | SP_REGNUM)) /* addi <imm>,sp,sp */
193 pi->frameoffset += read_memory_integer (current_pc + 2, 2);
194 else if (insn == ((FP_RAW_REGNUM << 11) | 0x0000 | SP_REGNUM)) /* mov sp,fp */
196 fp_used = 1;
197 pi->framereg = FP_RAW_REGNUM;
200 else if (insn == ((R12_REGNUM << 11) | 0x0640 | R0_REGNUM)) /* movhi hi(const),r0,r12 */
201 r12_tmp = read_memory_integer (current_pc + 2, 2) << 16;
202 else if (insn == ((R12_REGNUM << 11) | 0x0620 | R12_REGNUM)) /* movea lo(const),r12,r12 */
203 r12_tmp += read_memory_integer (current_pc + 2, 2);
204 else if (insn == ((SP_REGNUM << 11) | 0x01c0 | R12_REGNUM) && r12_tmp) /* add r12,sp */
205 pi->frameoffset = r12_tmp;
206 else if (insn == ((EP_REGNUM << 11) | 0x0000 | SP_REGNUM)) /* mov sp,ep */
207 ep_used = 1;
208 else if (insn == ((EP_REGNUM << 11) | 0x0000 | R1_REGNUM)) /* mov r1,ep */
209 ep_used = 0;
210 else if (((insn & 0x07ff) == (0x0760 | SP_REGNUM) /* st.w <reg>,<offset>[sp] */
211 || (fp_used
212 && (insn & 0x07ff) == (0x0760 | FP_RAW_REGNUM))) /* st.w <reg>,<offset>[fp] */
213 && pifsr
214 && (((reg = (insn >> 11) & 0x1f) >= SAVE1_START_REGNUM && reg <= SAVE1_END_REGNUM)
215 || (reg >= SAVE2_START_REGNUM && reg <= SAVE2_END_REGNUM)
216 || (reg >= SAVE3_START_REGNUM && reg <= SAVE3_END_REGNUM)))
218 pifsr->reg = reg;
219 pifsr->offset = read_memory_integer (current_pc + 2, 2) & ~1;
220 pifsr->cur_frameoffset = pi->frameoffset;
221 #ifdef DEBUG
222 printf_filtered ("\tSaved register r%d, offset %d", reg, pifsr->offset);
223 #endif
224 pifsr++;
227 else if (ep_used /* sst.w <reg>,<offset>[ep] */
228 && ((insn & 0x0781) == 0x0501)
229 && pifsr
230 && (((reg = (insn >> 11) & 0x1f) >= SAVE1_START_REGNUM && reg <= SAVE1_END_REGNUM)
231 || (reg >= SAVE2_START_REGNUM && reg <= SAVE2_END_REGNUM)
232 || (reg >= SAVE3_START_REGNUM && reg <= SAVE3_END_REGNUM)))
234 pifsr->reg = reg;
235 pifsr->offset = (insn & 0x007e) << 1;
236 pifsr->cur_frameoffset = pi->frameoffset;
237 #ifdef DEBUG
238 printf_filtered ("\tSaved register r%d, offset %d", reg, pifsr->offset);
239 #endif
240 pifsr++;
243 if ((insn & 0x0780) >= 0x0600) /* Four byte instruction? */
244 current_pc += 2;
246 #ifdef DEBUG
247 printf_filtered ("\n");
248 #endif
251 if (pifsr)
252 pifsr->framereg = 0; /* Tie off last entry */
254 /* Fix up any offsets to the final offset. If a frame pointer was created, use it
255 instead of the stack pointer. */
256 for (pifsr_tmp = pi->pifsrs; pifsr_tmp && pifsr_tmp != pifsr; pifsr_tmp++)
258 pifsr_tmp->offset -= pi->frameoffset - pifsr_tmp->cur_frameoffset;
259 pifsr_tmp->framereg = pi->framereg;
261 #ifdef DEBUG
262 printf_filtered ("Saved register r%d, offset = %d, framereg = r%d\n",
263 pifsr_tmp->reg, pifsr_tmp->offset, pifsr_tmp->framereg);
264 #endif
267 #ifdef DEBUG
268 printf_filtered ("Framereg = r%d, frameoffset = %d\n", pi->framereg, pi->frameoffset);
269 #endif
271 return current_pc;
274 /* Function: init_extra_frame_info
275 Setup the frame's frame pointer, pc, and frame addresses for saved
276 registers. Most of the work is done in scan_prologue().
278 Note that when we are called for the last frame (currently active frame),
279 that fi->pc and fi->frame will already be setup. However, fi->frame will
280 be valid only if this routine uses FP. For previous frames, fi-frame will
281 always be correct (since that is derived from v850_frame_chain ()).
283 We can be called with the PC in the call dummy under two circumstances.
284 First, during normal backtracing, second, while figuring out the frame
285 pointer just prior to calling the target function (see run_stack_dummy). */
287 void
288 v850_init_extra_frame_info (fi)
289 struct frame_info *fi;
291 struct prologue_info pi;
292 struct pifsr pifsrs[NUM_REGS + 1], *pifsr;
293 int reg;
295 if (fi->next)
296 fi->pc = FRAME_SAVED_PC (fi->next);
298 memset (fi->fsr.regs, '\000', sizeof fi->fsr.regs);
300 /* The call dummy doesn't save any registers on the stack, so we can return
301 now. */
302 if (PC_IN_CALL_DUMMY (fi->pc, fi->frame, fi->frame))
303 return;
305 pi.pifsrs = pifsrs;
307 v850_scan_prologue (fi->pc, &pi);
309 if (!fi->next && pi.framereg == SP_REGNUM)
310 fi->frame = read_register (pi.framereg) - pi.frameoffset;
312 for (pifsr = pifsrs; pifsr->framereg; pifsr++)
314 fi->fsr.regs[pifsr->reg] = pifsr->offset + fi->frame;
316 if (pifsr->framereg == SP_REGNUM)
317 fi->fsr.regs[pifsr->reg] += pi.frameoffset;
321 /* Function: frame_chain
322 Figure out the frame prior to FI. Unfortunately, this involves
323 scanning the prologue of the caller, which will also be done
324 shortly by v850_init_extra_frame_info. For the dummy frame, we
325 just return the stack pointer that was in use at the time the
326 function call was made. */
328 CORE_ADDR
329 v850_frame_chain (fi)
330 struct frame_info *fi;
332 struct prologue_info pi;
333 CORE_ADDR callers_pc, fp;
335 /* First, find out who called us */
336 callers_pc = FRAME_SAVED_PC (fi);
337 /* If caller is a call-dummy, then our FP bears no relation to his FP! */
338 fp = v850_find_callers_reg (fi, FP_RAW_REGNUM);
339 if (PC_IN_CALL_DUMMY(callers_pc, fp, fp))
340 return fp; /* caller is call-dummy: return oldest value of FP */
342 /* Caller is NOT a call-dummy, so everything else should just work.
343 Even if THIS frame is a call-dummy! */
344 pi.pifsrs = NULL;
346 v850_scan_prologue (callers_pc, &pi);
348 if (pi.start_function)
349 return 0; /* Don't chain beyond the start function */
351 if (pi.framereg == FP_RAW_REGNUM)
352 return v850_find_callers_reg (fi, pi.framereg);
354 return fi->frame - pi.frameoffset;
357 /* Function: find_callers_reg
358 Find REGNUM on the stack. Otherwise, it's in an active register.
359 One thing we might want to do here is to check REGNUM against the
360 clobber mask, and somehow flag it as invalid if it isn't saved on
361 the stack somewhere. This would provide a graceful failure mode
362 when trying to get the value of caller-saves registers for an inner
363 frame. */
365 CORE_ADDR
366 v850_find_callers_reg (fi, regnum)
367 struct frame_info *fi;
368 int regnum;
370 for (; fi; fi = fi->next)
371 if (PC_IN_CALL_DUMMY (fi->pc, fi->frame, fi->frame))
372 return generic_read_register_dummy (fi->pc, fi->frame, regnum);
373 else if (fi->fsr.regs[regnum] != 0)
374 return read_memory_unsigned_integer (fi->fsr.regs[regnum],
375 REGISTER_RAW_SIZE(regnum));
377 return read_register (regnum);
380 /* Function: skip_prologue
381 Return the address of the first code past the prologue of the function. */
383 CORE_ADDR
384 v850_skip_prologue (pc)
385 CORE_ADDR pc;
387 CORE_ADDR func_addr, func_end;
389 /* See what the symbol table says */
391 if (find_pc_partial_function (pc, NULL, &func_addr, &func_end))
393 struct symtab_and_line sal;
395 sal = find_pc_line (func_addr, 0);
397 if (sal.line != 0 && sal.end < func_end)
398 return sal.end;
399 else
400 /* Either there's no line info, or the line after the prologue is after
401 the end of the function. In this case, there probably isn't a
402 prologue. */
403 return pc;
406 /* We can't find the start of this function, so there's nothing we can do. */
407 return pc;
410 /* Function: pop_frame
411 This routine gets called when either the user uses the `return'
412 command, or the call dummy breakpoint gets hit. */
414 void
415 v850_pop_frame (frame)
416 struct frame_info *frame;
418 int regnum;
420 if (PC_IN_CALL_DUMMY(frame->pc, frame->frame, frame->frame))
421 generic_pop_dummy_frame ();
422 else
424 write_register (PC_REGNUM, FRAME_SAVED_PC (frame));
426 for (regnum = 0; regnum < NUM_REGS; regnum++)
427 if (frame->fsr.regs[regnum] != 0)
428 write_register (regnum,
429 read_memory_unsigned_integer (frame->fsr.regs[regnum],
430 REGISTER_RAW_SIZE(regnum)));
432 write_register (SP_REGNUM, FRAME_FP (frame));
435 flush_cached_frames ();
438 /* Function: push_arguments
439 Setup arguments and RP for a call to the target. First four args
440 go in R6->R9, subsequent args go into sp + 16 -> sp + ... Structs
441 are passed by reference. 64 bit quantities (doubles and long
442 longs) may be split between the regs and the stack. When calling a
443 function that returns a struct, a pointer to the struct is passed
444 in as a secret first argument (always in R6).
446 Stack space for the args has NOT been allocated: that job is up to us.
449 CORE_ADDR
450 v850_push_arguments (nargs, args, sp, struct_return, struct_addr)
451 int nargs;
452 value_ptr *args;
453 CORE_ADDR sp;
454 unsigned char struct_return;
455 CORE_ADDR struct_addr;
457 int argreg;
458 int argnum;
459 int len = 0;
460 int stack_offset;
462 /* First, just for safety, make sure stack is aligned */
463 sp &= ~3;
465 /* Now make space on the stack for the args. */
466 for (argnum = 0; argnum < nargs; argnum++)
467 len += ((TYPE_LENGTH(VALUE_TYPE(args[argnum])) + 3) & ~3);
468 sp -= len; /* possibly over-allocating, but it works... */
469 /* (you might think we could allocate 16 bytes */
470 /* less, but the ABI seems to use it all! ) */
471 argreg = ARG0_REGNUM;
473 /* the struct_return pointer occupies the first parameter-passing reg */
474 if (struct_return)
475 write_register (argreg++, struct_addr);
477 stack_offset = 16;
478 /* The offset onto the stack at which we will start copying parameters
479 (after the registers are used up) begins at 16 rather than at zero.
480 I don't really know why, that's just the way it seems to work. */
482 /* Now load as many as possible of the first arguments into
483 registers, and push the rest onto the stack. There are 16 bytes
484 in four registers available. Loop thru args from first to last. */
485 for (argnum = 0; argnum < nargs; argnum++)
487 int len;
488 char *val;
489 char valbuf[REGISTER_RAW_SIZE(ARG0_REGNUM)];
491 if (TYPE_CODE (VALUE_TYPE (*args)) == TYPE_CODE_STRUCT
492 && TYPE_LENGTH (VALUE_TYPE (*args)) > 8)
494 store_address (valbuf, 4, VALUE_ADDRESS (*args));
495 len = 4;
496 val = valbuf;
498 else
500 len = TYPE_LENGTH (VALUE_TYPE (*args));
501 val = (char *)VALUE_CONTENTS (*args);
504 while (len > 0)
505 if (argreg <= ARGLAST_REGNUM)
507 CORE_ADDR regval;
509 regval = extract_address (val, REGISTER_RAW_SIZE (argreg));
510 write_register (argreg, regval);
512 len -= REGISTER_RAW_SIZE (argreg);
513 val += REGISTER_RAW_SIZE (argreg);
514 argreg++;
516 else
518 write_memory (sp + stack_offset, val, 4);
520 len -= 4;
521 val += 4;
522 stack_offset += 4;
524 args++;
526 return sp;
529 /* Function: push_return_address (pc)
530 Set up the return address for the inferior function call.
531 Needed for targets where we don't actually execute a JSR/BSR instruction */
533 CORE_ADDR
534 v850_push_return_address (pc, sp)
535 CORE_ADDR pc;
536 CORE_ADDR sp;
538 write_register (RP_REGNUM, CALL_DUMMY_ADDRESS ());
539 return sp;
542 /* Function: frame_saved_pc
543 Find the caller of this frame. We do this by seeing if RP_REGNUM
544 is saved in the stack anywhere, otherwise we get it from the
545 registers. If the inner frame is a dummy frame, return its PC
546 instead of RP, because that's where "caller" of the dummy-frame
547 will be found. */
549 CORE_ADDR
550 v850_frame_saved_pc (fi)
551 struct frame_info *fi;
553 if (PC_IN_CALL_DUMMY(fi->pc, fi->frame, fi->frame))
554 return generic_read_register_dummy(fi->pc, fi->frame, PC_REGNUM);
555 else
556 return v850_find_callers_reg (fi, RP_REGNUM);
559 void
560 get_saved_register (raw_buffer, optimized, addrp, frame, regnum, lval)
561 char *raw_buffer;
562 int *optimized;
563 CORE_ADDR *addrp;
564 struct frame_info *frame;
565 int regnum;
566 enum lval_type *lval;
568 generic_get_saved_register (raw_buffer, optimized, addrp,
569 frame, regnum, lval);
573 /* Function: fix_call_dummy
574 Pokes the callee function's address into the CALL_DUMMY assembly stub.
575 Assumes that the CALL_DUMMY looks like this:
576 jarl <offset24>, r31
577 trap
581 v850_fix_call_dummy (dummy, sp, fun, nargs, args, type, gcc_p)
582 char *dummy;
583 CORE_ADDR sp;
584 CORE_ADDR fun;
585 int nargs;
586 value_ptr *args;
587 struct type *type;
588 int gcc_p;
590 long offset24;
592 offset24 = (long) fun - (long) entry_point_address ();
593 offset24 &= 0x3fffff;
594 offset24 |= 0xff800000; /* jarl <offset24>, r31 */
596 store_unsigned_integer ((unsigned int *)&dummy[2], 2, offset24 & 0xffff);
597 store_unsigned_integer ((unsigned int *)&dummy[0], 2, offset24 >> 16);
598 return 0;
601 void
602 _initialize_v850_tdep ()
604 tm_print_insn = print_insn_v850;