2003-01-07 Andrew Cagney <cagney@redhat.com>
[deliverable/binutils-gdb.git] / gdb / mcore-tdep.c
1 /* Target-machine dependent code for Motorola MCore for GDB, the GNU debugger
2 Copyright 1999, 2000, 2001, 2002, 2003 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 "symtab.h"
23 #include "value.h"
24 #include "gdbcmd.h"
25 #include "regcache.h"
26 #include "symfile.h"
27 #include "gdbcore.h"
28 #include "inferior.h"
29 #include "arch-utils.h"
30 #include "gdb_string.h"
31
32 /* Functions declared and used only in this file */
33
34 static CORE_ADDR mcore_analyze_prologue (struct frame_info *fi, CORE_ADDR pc, int skip_prologue);
35
36 static struct frame_info *analyze_dummy_frame (CORE_ADDR pc, CORE_ADDR frame);
37
38 static int get_insn (CORE_ADDR pc);
39
40 /* Functions exported from this file */
41
42 int mcore_use_struct_convention (int gcc_p, struct type *type);
43
44 void _initialize_mcore (void);
45
46 void mcore_init_extra_frame_info (int fromleaf, struct frame_info *fi);
47
48 CORE_ADDR mcore_frame_saved_pc (struct frame_info *fi);
49
50 CORE_ADDR mcore_find_callers_reg (struct frame_info *fi, int regnum);
51
52 CORE_ADDR mcore_frame_args_address (struct frame_info *fi);
53
54 CORE_ADDR mcore_frame_locals_address (struct frame_info *fi);
55
56 CORE_ADDR mcore_push_return_address (CORE_ADDR pc, CORE_ADDR sp);
57
58 CORE_ADDR mcore_push_arguments (int nargs, struct value ** args, CORE_ADDR sp,
59 int struct_return, CORE_ADDR struct_addr);
60
61 void mcore_pop_frame ();
62
63 CORE_ADDR mcore_skip_prologue (CORE_ADDR pc);
64
65 CORE_ADDR mcore_frame_chain (struct frame_info *fi);
66
67 const unsigned char *mcore_breakpoint_from_pc (CORE_ADDR * bp_addr, int *bp_size);
68
69 int mcore_use_struct_convention (int gcc_p, struct type *type);
70
71 void mcore_store_return_value (struct type *type, char *valbuf);
72
73 CORE_ADDR mcore_extract_struct_value_address (char *regbuf);
74
75 void mcore_extract_return_value (struct type *type, char *regbuf, char *valbuf);
76
77 #ifdef MCORE_DEBUG
78 int mcore_debug = 0;
79 #endif
80
81
82 /* All registers are 4 bytes long. */
83 #define MCORE_REG_SIZE 4
84 #define MCORE_NUM_REGS 65
85
86 /* Some useful register numbers. */
87 #define PR_REGNUM 15
88 #define FIRST_ARGREG 2
89 #define LAST_ARGREG 7
90 #define RETVAL_REGNUM 2
91
92
93 /* Additional info that we use for managing frames */
94 struct frame_extra_info
95 {
96 /* A generic status word */
97 int status;
98
99 /* Size of this frame */
100 int framesize;
101
102 /* The register that is acting as a frame pointer, if
103 it is being used. This is undefined if status
104 does not contain the flag MY_FRAME_IN_FP. */
105 int fp_regnum;
106 };
107
108 /* frame_extra_info status flags */
109
110 /* The base of the current frame is actually in the stack pointer.
111 This happens when there is no frame pointer (MCore ABI does not
112 require a frame pointer) or when we're stopped in the prologue or
113 epilogue itself. In these cases, mcore_analyze_prologue will need
114 to update fi->frame before returning or analyzing the register
115 save instructions. */
116 #define MY_FRAME_IN_SP 0x1
117
118 /* The base of the current frame is in a frame pointer register.
119 This register is noted in frame_extra_info->fp_regnum.
120
121 Note that the existence of an FP might also indicate that the
122 function has called alloca. */
123 #define MY_FRAME_IN_FP 0x2
124
125 /* This flag is set to indicate that this frame is the top-most
126 frame. This tells frame chain not to bother trying to unwind
127 beyond this frame. */
128 #define NO_MORE_FRAMES 0x4
129
130 /* Instruction macros used for analyzing the prologue */
131 #define IS_SUBI0(x) (((x) & 0xfe0f) == 0x2400) /* subi r0,oimm5 */
132 #define IS_STM(x) (((x) & 0xfff0) == 0x0070) /* stm rf-r15,r0 */
133 #define IS_STWx0(x) (((x) & 0xf00f) == 0x9000) /* stw rz,(r0,disp) */
134 #define IS_STWxy(x) (((x) & 0xf000) == 0x9000) /* stw rx,(ry,disp) */
135 #define IS_MOVx0(x) (((x) & 0xfff0) == 0x1200) /* mov rn,r0 */
136 #define IS_LRW1(x) (((x) & 0xff00) == 0x7100) /* lrw r1,literal */
137 #define IS_MOVI1(x) (((x) & 0xf80f) == 0x6001) /* movi r1,imm7 */
138 #define IS_BGENI1(x) (((x) & 0xfe0f) == 0x3201) /* bgeni r1,imm5 */
139 #define IS_BMASKI1(x) (((x) & 0xfe0f) == 0x2C01) /* bmaski r1,imm5 */
140 #define IS_ADDI1(x) (((x) & 0xfe0f) == 0x2001) /* addi r1,oimm5 */
141 #define IS_SUBI1(x) (((x) & 0xfe0f) == 0x2401) /* subi r1,oimm5 */
142 #define IS_RSUBI1(x) (((x) & 0xfe0f) == 0x2801) /* rsubi r1,imm5 */
143 #define IS_NOT1(x) (((x) & 0xffff) == 0x01f1) /* not r1 */
144 #define IS_ROTLI1(x) (((x) & 0xfe0f) == 0x3801) /* rotli r1,imm5 */
145 #define IS_BSETI1(x) (((x) & 0xfe0f) == 0x3401) /* bseti r1,imm5 */
146 #define IS_BCLRI1(x) (((x) & 0xfe0f) == 0x3001) /* bclri r1,imm5 */
147 #define IS_IXH1(x) (((x) & 0xffff) == 0x1d11) /* ixh r1,r1 */
148 #define IS_IXW1(x) (((x) & 0xffff) == 0x1511) /* ixw r1,r1 */
149 #define IS_SUB01(x) (((x) & 0xffff) == 0x0510) /* subu r0,r1 */
150 #define IS_RTS(x) (((x) & 0xffff) == 0x00cf) /* jmp r15 */
151
152 #define IS_R1_ADJUSTER(x) \
153 (IS_ADDI1(x) || IS_SUBI1(x) || IS_ROTLI1(x) || IS_BSETI1(x) \
154 || IS_BCLRI1(x) || IS_RSUBI1(x) || IS_NOT1(x) \
155 || IS_IXH1(x) || IS_IXW1(x))
156 \f
157
158 #ifdef MCORE_DEBUG
159 static void
160 mcore_dump_insn (char *commnt, CORE_ADDR pc, int insn)
161 {
162 if (mcore_debug)
163 {
164 printf_filtered ("MCORE: %s %08x %08x ",
165 commnt, (unsigned int) pc, (unsigned int) insn);
166 TARGET_PRINT_INSN (pc, &tm_print_insn_info);
167 printf_filtered ("\n");
168 }
169 }
170 #define mcore_insn_debug(args) { if (mcore_debug) printf_filtered args; }
171 #else /* !MCORE_DEBUG */
172 #define mcore_dump_insn(a,b,c) {}
173 #define mcore_insn_debug(args) {}
174 #endif
175
176
177 static struct type *
178 mcore_register_virtual_type (int regnum)
179 {
180 if (regnum < 0 || regnum >= MCORE_NUM_REGS)
181 internal_error (__FILE__, __LINE__,
182 "mcore_register_virtual_type: illegal register number %d",
183 regnum);
184 else
185 return builtin_type_int;
186 }
187
188 static int
189 mcore_register_byte (int regnum)
190 {
191 if (regnum < 0 || regnum >= MCORE_NUM_REGS)
192 internal_error (__FILE__, __LINE__,
193 "mcore_register_byte: illegal register number %d",
194 regnum);
195 else
196 return (regnum * MCORE_REG_SIZE);
197 }
198
199 static int
200 mcore_register_size (int regnum)
201 {
202
203 if (regnum < 0 || regnum >= MCORE_NUM_REGS)
204 internal_error (__FILE__, __LINE__,
205 "mcore_register_size: illegal register number %d",
206 regnum);
207 else
208 return MCORE_REG_SIZE;
209 }
210
211 /* The registers of the Motorola MCore processors */
212
213 static const char *
214 mcore_register_name (int regnum)
215 {
216
217 static char *register_names[] = {
218 "r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7",
219 "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15",
220 "ar0", "ar1", "ar2", "ar3", "ar4", "ar5", "ar6", "ar7",
221 "ar8", "ar9", "ar10", "ar11", "ar12", "ar13", "ar14", "ar15",
222 "psr", "vbr", "epsr", "fpsr", "epc", "fpc", "ss0", "ss1",
223 "ss2", "ss3", "ss4", "gcr", "gsr", "cr13", "cr14", "cr15",
224 "cr16", "cr17", "cr18", "cr19", "cr20", "cr21", "cr22", "cr23",
225 "cr24", "cr25", "cr26", "cr27", "cr28", "cr29", "cr30", "cr31",
226 "pc"
227 };
228
229 if (regnum < 0 ||
230 regnum >= sizeof (register_names) / sizeof (register_names[0]))
231 internal_error (__FILE__, __LINE__,
232 "mcore_register_name: illegal register number %d",
233 regnum);
234 else
235 return register_names[regnum];
236 }
237
238 /* Given the address at which to insert a breakpoint (BP_ADDR),
239 what will that breakpoint be?
240
241 For MCore, we have a breakpoint instruction. Since all MCore
242 instructions are 16 bits, this is all we need, regardless of
243 address. bpkt = 0x0000 */
244
245 const unsigned char *
246 mcore_breakpoint_from_pc (CORE_ADDR * bp_addr, int *bp_size)
247 {
248 static char breakpoint[] =
249 {0x00, 0x00};
250 *bp_size = 2;
251 return breakpoint;
252 }
253
254 static CORE_ADDR
255 mcore_saved_pc_after_call (struct frame_info *frame)
256 {
257 return read_register (PR_REGNUM);
258 }
259
260 /* This is currently handled by init_extra_frame_info. */
261 static void
262 mcore_frame_init_saved_regs (struct frame_info *frame)
263 {
264
265 }
266
267 /* This is currently handled by mcore_push_arguments */
268 static void
269 mcore_store_struct_return (CORE_ADDR addr, CORE_ADDR sp)
270 {
271
272 }
273
274 static int
275 mcore_reg_struct_has_addr (int gcc_p, struct type *type)
276 {
277 return 0;
278 }
279
280
281 /* Helper function for several routines below. This funtion simply
282 sets up a fake, aka dummy, frame (not a _call_ dummy frame) that
283 we can analyze with mcore_analyze_prologue. */
284
285 static struct frame_info *
286 analyze_dummy_frame (CORE_ADDR pc, CORE_ADDR frame)
287 {
288 static struct frame_info *dummy = NULL;
289
290 if (dummy == NULL)
291 {
292 struct frame_extra_info *extra_info;
293 dummy = deprecated_frame_xmalloc ();
294 dummy->saved_regs = (CORE_ADDR *) xmalloc (SIZEOF_FRAME_SAVED_REGS);
295 extra_info = XMALLOC (struct frame_extra_info);
296 deprecated_set_frame_extra_info_hack (dummy, extra_info);
297 }
298
299 deprecated_set_frame_next_hack (dummy, NULL);
300 deprecated_set_frame_prev_hack (dummy, NULL);
301 deprecated_update_frame_pc_hack (dummy, pc);
302 deprecated_update_frame_base_hack (dummy, frame);
303 dummy->extra_info->status = 0;
304 dummy->extra_info->framesize = 0;
305 memset (get_frame_saved_regs (dummy), '\000', SIZEOF_FRAME_SAVED_REGS);
306 mcore_analyze_prologue (dummy, 0, 0);
307 return dummy;
308 }
309
310 /* Function prologues on the Motorola MCore processors consist of:
311
312 - adjustments to the stack pointer (r1 used as scratch register)
313 - store word/multiples that use r0 as the base address
314 - making a copy of r0 into another register (a "frame" pointer)
315
316 Note that the MCore really doesn't have a real frame pointer.
317 Instead, the compiler may copy the SP into a register (usually
318 r8) to act as an arg pointer. For our target-dependent purposes,
319 the frame info's "frame" member will be the beginning of the
320 frame. The SP could, in fact, point below this.
321
322 The prologue ends when an instruction fails to meet either of
323 the first two criteria or when an FP is made. We make a special
324 exception for gcc. When compiling unoptimized code, gcc will
325 setup stack slots. We need to make sure that we skip the filling
326 of these stack slots as much as possible. This is only done
327 when SKIP_PROLOGUE is set, so that it does not mess up
328 backtraces. */
329
330 /* Analyze the prologue of frame FI to determine where registers are saved,
331 the end of the prologue, etc. Return the address of the first line
332 of "real" code (i.e., the end of the prologue). */
333
334 static CORE_ADDR
335 mcore_analyze_prologue (struct frame_info *fi, CORE_ADDR pc, int skip_prologue)
336 {
337 CORE_ADDR func_addr, func_end, addr, stop;
338 CORE_ADDR stack_size;
339 int insn, rn;
340 int status;
341 int fp_regnum = 0; /* dummy, valid when (flags & MY_FRAME_IN_FP) */
342 int flags;
343 int framesize;
344 int register_offsets[NUM_REGS];
345 char *name;
346
347 /* If provided, use the PC in the frame to look up the
348 start of this function. */
349 pc = (fi == NULL ? pc : get_frame_pc (fi));
350
351 /* Find the start of this function. */
352 status = find_pc_partial_function (pc, &name, &func_addr, &func_end);
353
354 /* If the start of this function could not be found or if the debbuger
355 is stopped at the first instruction of the prologue, do nothing. */
356 if (status == 0)
357 return pc;
358
359 /* If the debugger is entry function, give up. */
360 if (func_addr == entry_point_address ())
361 {
362 if (fi != NULL)
363 fi->extra_info->status |= NO_MORE_FRAMES;
364 return pc;
365 }
366
367 /* At the start of a function, our frame is in the stack pointer. */
368 flags = MY_FRAME_IN_SP;
369
370 /* Start decoding the prologue. We start by checking two special cases:
371
372 1. We're about to return
373 2. We're at the first insn of the prologue.
374
375 If we're about to return, our frame has already been deallocated.
376 If we are stopped at the first instruction of a prologue,
377 then our frame has not yet been set up. */
378
379 /* Get the first insn from memory (all MCore instructions are 16 bits) */
380 mcore_insn_debug (("MCORE: starting prologue decoding\n"));
381 insn = get_insn (pc);
382 mcore_dump_insn ("got 1: ", pc, insn);
383
384 /* Check for return. */
385 if (fi != NULL && IS_RTS (insn))
386 {
387 mcore_insn_debug (("MCORE: got jmp r15"));
388 if (fi->next == NULL)
389 deprecated_update_frame_base_hack (fi, read_sp ());
390 return get_frame_pc (fi);
391 }
392
393 /* Check for first insn of prologue */
394 if (fi != NULL && get_frame_pc (fi) == func_addr)
395 {
396 if (fi->next == NULL)
397 deprecated_update_frame_base_hack (fi, read_sp ());
398 return get_frame_pc (fi);
399 }
400
401 /* Figure out where to stop scanning */
402 stop = (fi ? get_frame_pc (fi) : func_end);
403
404 /* Don't walk off the end of the function */
405 stop = (stop > func_end ? func_end : stop);
406
407 /* REGISTER_OFFSETS will contain offsets, from the top of the frame
408 (NOT the frame pointer), for the various saved registers or -1
409 if the register is not saved. */
410 for (rn = 0; rn < NUM_REGS; rn++)
411 register_offsets[rn] = -1;
412
413 /* Analyze the prologue. Things we determine from analyzing the
414 prologue include:
415 * the size of the frame
416 * where saved registers are located (and which are saved)
417 * FP used? */
418 mcore_insn_debug (("MCORE: Scanning prologue: func_addr=0x%x, stop=0x%x\n",
419 (unsigned int) func_addr, (unsigned int) stop));
420
421 framesize = 0;
422 for (addr = func_addr; addr < stop; addr += 2)
423 {
424 /* Get next insn */
425 insn = get_insn (addr);
426 mcore_dump_insn ("got 2: ", addr, insn);
427
428 if (IS_SUBI0 (insn))
429 {
430 int offset = 1 + ((insn >> 4) & 0x1f);
431 mcore_insn_debug (("MCORE: got subi r0,%d; continuing\n", offset));
432 framesize += offset;
433 continue;
434 }
435 else if (IS_STM (insn))
436 {
437 /* Spill register(s) */
438 int offset;
439 int start_register;
440
441 /* BIG WARNING! The MCore ABI does not restrict functions
442 to taking only one stack allocation. Therefore, when
443 we save a register, we record the offset of where it was
444 saved relative to the current framesize. This will
445 then give an offset from the SP upon entry to our
446 function. Remember, framesize is NOT constant until
447 we're done scanning the prologue. */
448 start_register = (insn & 0xf);
449 mcore_insn_debug (("MCORE: got stm r%d-r15,(r0)\n", start_register));
450
451 for (rn = start_register, offset = 0; rn <= 15; rn++, offset += 4)
452 {
453 register_offsets[rn] = framesize - offset;
454 mcore_insn_debug (("MCORE: r%d saved at 0x%x (offset %d)\n", rn,
455 register_offsets[rn], offset));
456 }
457 mcore_insn_debug (("MCORE: continuing\n"));
458 continue;
459 }
460 else if (IS_STWx0 (insn))
461 {
462 /* Spill register: see note for IS_STM above. */
463 int imm;
464
465 rn = (insn >> 8) & 0xf;
466 imm = (insn >> 4) & 0xf;
467 register_offsets[rn] = framesize - (imm << 2);
468 mcore_insn_debug (("MCORE: r%d saved at offset 0x%x\n", rn, register_offsets[rn]));
469 mcore_insn_debug (("MCORE: continuing\n"));
470 continue;
471 }
472 else if (IS_MOVx0 (insn))
473 {
474 /* We have a frame pointer, so this prologue is over. Note
475 the register which is acting as the frame pointer. */
476 flags |= MY_FRAME_IN_FP;
477 flags &= ~MY_FRAME_IN_SP;
478 fp_regnum = insn & 0xf;
479 mcore_insn_debug (("MCORE: Found a frame pointer: r%d\n", fp_regnum));
480
481 /* If we found an FP, we're at the end of the prologue. */
482 mcore_insn_debug (("MCORE: end of prologue\n"));
483 if (skip_prologue)
484 continue;
485
486 /* If we're decoding prologue, stop here. */
487 addr += 2;
488 break;
489 }
490 else if (IS_STWxy (insn) && (flags & MY_FRAME_IN_FP) && ((insn & 0xf) == fp_regnum))
491 {
492 /* Special case. Skip over stack slot allocs, too. */
493 mcore_insn_debug (("MCORE: push arg onto stack.\n"));
494 continue;
495 }
496 else if (IS_LRW1 (insn) || IS_MOVI1 (insn)
497 || IS_BGENI1 (insn) || IS_BMASKI1 (insn))
498 {
499 int adjust = 0;
500 int offset = 0;
501 int insn2;
502
503 mcore_insn_debug (("MCORE: looking at large frame\n"));
504 if (IS_LRW1 (insn))
505 {
506 adjust =
507 read_memory_integer ((addr + 2 + ((insn & 0xff) << 2)) & 0xfffffffc, 4);
508 }
509 else if (IS_MOVI1 (insn))
510 adjust = (insn >> 4) & 0x7f;
511 else if (IS_BGENI1 (insn))
512 adjust = 1 << ((insn >> 4) & 0x1f);
513 else /* IS_BMASKI (insn) */
514 adjust = (1 << (adjust >> 4) & 0x1f) - 1;
515
516 mcore_insn_debug (("MCORE: base framesize=0x%x\n", adjust));
517
518 /* May have zero or more insns which modify r1 */
519 mcore_insn_debug (("MCORE: looking for r1 adjusters...\n"));
520 offset = 2;
521 insn2 = get_insn (addr + offset);
522 while (IS_R1_ADJUSTER (insn2))
523 {
524 int imm;
525
526 imm = (insn2 >> 4) & 0x1f;
527 mcore_dump_insn ("got 3: ", addr + offset, insn);
528 if (IS_ADDI1 (insn2))
529 {
530 adjust += (imm + 1);
531 mcore_insn_debug (("MCORE: addi r1,%d\n", imm + 1));
532 }
533 else if (IS_SUBI1 (insn2))
534 {
535 adjust -= (imm + 1);
536 mcore_insn_debug (("MCORE: subi r1,%d\n", imm + 1));
537 }
538 else if (IS_RSUBI1 (insn2))
539 {
540 adjust = imm - adjust;
541 mcore_insn_debug (("MCORE: rsubi r1,%d\n", imm + 1));
542 }
543 else if (IS_NOT1 (insn2))
544 {
545 adjust = ~adjust;
546 mcore_insn_debug (("MCORE: not r1\n"));
547 }
548 else if (IS_ROTLI1 (insn2))
549 {
550 adjust <<= imm;
551 mcore_insn_debug (("MCORE: rotli r1,%d\n", imm + 1));
552 }
553 else if (IS_BSETI1 (insn2))
554 {
555 adjust |= (1 << imm);
556 mcore_insn_debug (("MCORE: bseti r1,%d\n", imm));
557 }
558 else if (IS_BCLRI1 (insn2))
559 {
560 adjust &= ~(1 << imm);
561 mcore_insn_debug (("MCORE: bclri r1,%d\n", imm));
562 }
563 else if (IS_IXH1 (insn2))
564 {
565 adjust *= 3;
566 mcore_insn_debug (("MCORE: ix.h r1,r1\n"));
567 }
568 else if (IS_IXW1 (insn2))
569 {
570 adjust *= 5;
571 mcore_insn_debug (("MCORE: ix.w r1,r1\n"));
572 }
573
574 offset += 2;
575 insn2 = get_insn (addr + offset);
576 };
577
578 mcore_insn_debug (("MCORE: done looking for r1 adjusters\n"));
579
580 /* If the next insn adjusts the stack pointer, we keep everything;
581 if not, we scrap it and we've found the end of the prologue. */
582 if (IS_SUB01 (insn2))
583 {
584 addr += offset;
585 framesize += adjust;
586 mcore_insn_debug (("MCORE: found stack adjustment of 0x%x bytes.\n", adjust));
587 mcore_insn_debug (("MCORE: skipping to new address 0x%x\n", addr));
588 mcore_insn_debug (("MCORE: continuing\n"));
589 continue;
590 }
591
592 /* None of these instructions are prologue, so don't touch
593 anything. */
594 mcore_insn_debug (("MCORE: no subu r1,r0, NOT altering framesize.\n"));
595 break;
596 }
597
598 /* This is not a prologue insn, so stop here. */
599 mcore_insn_debug (("MCORE: insn is not a prologue insn -- ending scan\n"));
600 break;
601 }
602
603 mcore_insn_debug (("MCORE: done analyzing prologue\n"));
604 mcore_insn_debug (("MCORE: prologue end = 0x%x\n", addr));
605
606 /* Save everything we have learned about this frame into FI. */
607 if (fi != NULL)
608 {
609 fi->extra_info->framesize = framesize;
610 fi->extra_info->fp_regnum = fp_regnum;
611 fi->extra_info->status = flags;
612
613 /* Fix the frame pointer. When gcc uses r8 as a frame pointer,
614 it is really an arg ptr. We adjust fi->frame to be a "real"
615 frame pointer. */
616 if (fi->next == NULL)
617 {
618 if (fi->extra_info->status & MY_FRAME_IN_SP)
619 deprecated_update_frame_base_hack (fi, read_sp () + framesize);
620 else
621 deprecated_update_frame_base_hack (fi, read_register (fp_regnum) + framesize);
622 }
623
624 /* Note where saved registers are stored. The offsets in REGISTER_OFFSETS
625 are computed relative to the top of the frame. */
626 for (rn = 0; rn < NUM_REGS; rn++)
627 {
628 if (register_offsets[rn] >= 0)
629 {
630 get_frame_saved_regs (fi)[rn] = fi->frame - register_offsets[rn];
631 mcore_insn_debug (("Saved register %s stored at 0x%08x, value=0x%08x\n",
632 mcore_register_names[rn], fi->saved_regs[rn],
633 read_memory_integer (fi->saved_regs[rn], 4)));
634 }
635 }
636 }
637
638 /* Return addr of first non-prologue insn. */
639 return addr;
640 }
641
642 /* Given a GDB frame, determine the address of the calling function's
643 frame. This will be used to create a new GDB frame struct, and
644 then INIT_EXTRA_FRAME_INFO and DEPRECATED_INIT_FRAME_PC will be
645 called for the new frame. */
646
647 CORE_ADDR
648 mcore_frame_chain (struct frame_info * fi)
649 {
650 struct frame_info *dummy;
651 CORE_ADDR callers_addr;
652
653 /* Analyze the prologue of this function. */
654 if (fi->extra_info->status == 0)
655 mcore_analyze_prologue (fi, 0, 0);
656
657 /* If mcore_analyze_prologue set NO_MORE_FRAMES, quit now. */
658 if (fi->extra_info->status & NO_MORE_FRAMES)
659 return 0;
660
661 /* Now that we've analyzed our prologue, we can start to ask
662 for information about our caller. The easiest way to do
663 this is to analyze our caller's prologue.
664
665 If our caller has a frame pointer, then we need to find
666 the value of that register upon entry to our frame.
667 This value is either in fi->saved_regs[rn] if it's saved,
668 or it's still in a register.
669
670 If our caller does not have a frame pointer, then his frame base
671 is <our base> + -<caller's frame size>. */
672 dummy = analyze_dummy_frame (FRAME_SAVED_PC (fi), fi->frame);
673
674 if (dummy->extra_info->status & MY_FRAME_IN_FP)
675 {
676 int fp = dummy->extra_info->fp_regnum;
677
678 /* Our caller has a frame pointer. */
679 if (get_frame_saved_regs (fi)[fp] != 0)
680 {
681 /* The "FP" was saved on the stack. Don't forget to adjust
682 the "FP" with the framesize to get a real FP. */
683 callers_addr = read_memory_integer (get_frame_saved_regs (fi)[fp], REGISTER_SIZE)
684 + dummy->extra_info->framesize;
685 }
686 else
687 {
688 /* It's still in the register. Don't forget to adjust
689 the "FP" with the framesize to get a real FP. */
690 callers_addr = read_register (fp) + dummy->extra_info->framesize;
691 }
692 }
693 else
694 {
695 /* Our caller does not have a frame pointer. */
696 callers_addr = fi->frame + dummy->extra_info->framesize;
697 }
698
699 return callers_addr;
700 }
701
702 /* Skip the prologue of the function at PC. */
703
704 CORE_ADDR
705 mcore_skip_prologue (CORE_ADDR pc)
706 {
707 CORE_ADDR func_addr, func_end;
708 struct symtab_and_line sal;
709
710 /* If we have line debugging information, then the end of the
711 prologue should be the first assembly instruction of the first
712 source line */
713 if (find_pc_partial_function (pc, NULL, &func_addr, &func_end))
714 {
715 sal = find_pc_line (func_addr, 0);
716 if (sal.end && sal.end < func_end)
717 return sal.end;
718 }
719
720 return mcore_analyze_prologue (NULL, pc, 1);
721 }
722
723 /* Return the address at which function arguments are offset. */
724 CORE_ADDR
725 mcore_frame_args_address (struct frame_info * fi)
726 {
727 return fi->frame - fi->extra_info->framesize;
728 }
729
730 CORE_ADDR
731 mcore_frame_locals_address (struct frame_info * fi)
732 {
733 return fi->frame - fi->extra_info->framesize;
734 }
735
736 /* Return the frame pointer in use at address PC. */
737
738 void
739 mcore_virtual_frame_pointer (CORE_ADDR pc, int *reg, LONGEST *offset)
740 {
741 struct frame_info *dummy = analyze_dummy_frame (pc, 0);
742 if (dummy->extra_info->status & MY_FRAME_IN_SP)
743 {
744 *reg = SP_REGNUM;
745 *offset = 0;
746 }
747 else
748 {
749 *reg = dummy->extra_info->fp_regnum;
750 *offset = 0;
751 }
752 }
753
754 /* Find the value of register REGNUM in frame FI. */
755
756 CORE_ADDR
757 mcore_find_callers_reg (struct frame_info *fi, int regnum)
758 {
759 for (; fi != NULL; fi = fi->next)
760 {
761 if (DEPRECATED_PC_IN_CALL_DUMMY (get_frame_pc (fi), fi->frame, fi->frame))
762 return deprecated_read_register_dummy (get_frame_pc (fi), fi->frame, regnum);
763 else if (get_frame_saved_regs (fi)[regnum] != 0)
764 return read_memory_integer (get_frame_saved_regs (fi)[regnum],
765 REGISTER_SIZE);
766 }
767
768 return read_register (regnum);
769 }
770
771 /* Find the saved pc in frame FI. */
772
773 CORE_ADDR
774 mcore_frame_saved_pc (struct frame_info * fi)
775 {
776
777 if (DEPRECATED_PC_IN_CALL_DUMMY (get_frame_pc (fi), fi->frame, fi->frame))
778 return deprecated_read_register_dummy (get_frame_pc (fi), fi->frame, PC_REGNUM);
779 else
780 return mcore_find_callers_reg (fi, PR_REGNUM);
781 }
782 \f
783 /* INFERIOR FUNCTION CALLS */
784
785 /* This routine gets called when either the user uses the "return"
786 command, or the call dummy breakpoint gets hit. */
787
788 void
789 mcore_pop_frame (void)
790 {
791 int rn;
792 struct frame_info *fi = get_current_frame ();
793
794 if (DEPRECATED_PC_IN_CALL_DUMMY (get_frame_pc (fi), fi->frame, fi->frame))
795 generic_pop_dummy_frame ();
796 else
797 {
798 /* Write out the PC we saved. */
799 write_register (PC_REGNUM, FRAME_SAVED_PC (fi));
800
801 /* Restore any saved registers. */
802 for (rn = 0; rn < NUM_REGS; rn++)
803 {
804 if (get_frame_saved_regs (fi)[rn] != 0)
805 {
806 ULONGEST value;
807
808 value = read_memory_unsigned_integer (get_frame_saved_regs (fi)[rn],
809 REGISTER_SIZE);
810 write_register (rn, value);
811 }
812 }
813
814 /* Actually cut back the stack. */
815 write_register (SP_REGNUM, get_frame_base (fi));
816 }
817
818 /* Finally, throw away any cached frame information. */
819 flush_cached_frames ();
820 }
821
822 /* Setup arguments and PR for a call to the target. First six arguments
823 go in FIRST_ARGREG -> LAST_ARGREG, subsequent args go on to the stack.
824
825 * Types with lengths greater than REGISTER_SIZE may not be split
826 between registers and the stack, and they must start in an even-numbered
827 register. Subsequent args will go onto the stack.
828
829 * Structs may be split between registers and stack, left-aligned.
830
831 * If the function returns a struct which will not fit into registers (it's
832 more than eight bytes), we must allocate for that, too. Gdb will tell
833 us where this buffer is (STRUCT_ADDR), and we simply place it into
834 FIRST_ARGREG, since the MCORE treats struct returns (of less than eight
835 bytes) as hidden first arguments. */
836
837 CORE_ADDR
838 mcore_push_arguments (int nargs, struct value **args, CORE_ADDR sp,
839 int struct_return, CORE_ADDR struct_addr)
840 {
841 int argreg;
842 int argnum;
843 struct stack_arg
844 {
845 int len;
846 char *val;
847 }
848 *stack_args;
849 int nstack_args = 0;
850
851 stack_args = (struct stack_arg *) alloca (nargs * sizeof (struct stack_arg));
852
853 argreg = FIRST_ARGREG;
854
855 /* Align the stack. This is mostly a nop, but not always. It will be needed
856 if we call a function which has argument overflow. */
857 sp &= ~3;
858
859 /* If this function returns a struct which does not fit in the
860 return registers, we must pass a buffer to the function
861 which it can use to save the return value. */
862 if (struct_return)
863 write_register (argreg++, struct_addr);
864
865 /* FIXME: what about unions? */
866 for (argnum = 0; argnum < nargs; argnum++)
867 {
868 char *val = (char *) VALUE_CONTENTS (args[argnum]);
869 int len = TYPE_LENGTH (VALUE_TYPE (args[argnum]));
870 struct type *type = VALUE_TYPE (args[argnum]);
871 int olen;
872
873 mcore_insn_debug (("MCORE PUSH: argreg=%d; len=%d; %s\n",
874 argreg, len, TYPE_CODE (type) == TYPE_CODE_STRUCT ? "struct" : "not struct"));
875 /* Arguments larger than a register must start in an even
876 numbered register. */
877 olen = len;
878
879 if (TYPE_CODE (type) != TYPE_CODE_STRUCT && len > REGISTER_SIZE && argreg % 2)
880 {
881 mcore_insn_debug (("MCORE PUSH: %d > REGISTER_SIZE: and %s is not even\n",
882 len, mcore_register_names[argreg]));
883 argreg++;
884 }
885
886 if ((argreg <= LAST_ARGREG && len <= (LAST_ARGREG - argreg + 1) * REGISTER_SIZE)
887 || (TYPE_CODE (type) == TYPE_CODE_STRUCT))
888 {
889 /* Something that will fit entirely into registers (or a struct
890 which may be split between registers and stack). */
891 mcore_insn_debug (("MCORE PUSH: arg %d going into regs\n", argnum));
892
893 if (TYPE_CODE (type) == TYPE_CODE_STRUCT && olen < REGISTER_SIZE)
894 {
895 /* Small structs must be right aligned within the register,
896 the most significant bits are undefined. */
897 write_register (argreg, extract_unsigned_integer (val, len));
898 argreg++;
899 len = 0;
900 }
901
902 while (len > 0 && argreg <= LAST_ARGREG)
903 {
904 write_register (argreg, extract_unsigned_integer (val, REGISTER_SIZE));
905 argreg++;
906 val += REGISTER_SIZE;
907 len -= REGISTER_SIZE;
908 }
909
910 /* Any remainder for the stack is noted below... */
911 }
912 else if (TYPE_CODE (VALUE_TYPE (args[argnum])) != TYPE_CODE_STRUCT
913 && len > REGISTER_SIZE)
914 {
915 /* All subsequent args go onto the stack. */
916 mcore_insn_debug (("MCORE PUSH: does not fit into regs, going onto stack\n"));
917 argnum = LAST_ARGREG + 1;
918 }
919
920 if (len > 0)
921 {
922 /* Note that this must be saved onto the stack */
923 mcore_insn_debug (("MCORE PUSH: adding arg %d to stack\n", argnum));
924 stack_args[nstack_args].val = val;
925 stack_args[nstack_args].len = len;
926 nstack_args++;
927 }
928
929 }
930
931 /* We're done with registers and stack allocation. Now do the actual
932 stack pushes. */
933 while (nstack_args--)
934 {
935 sp -= stack_args[nstack_args].len;
936 write_memory (sp, stack_args[nstack_args].val, stack_args[nstack_args].len);
937 }
938
939 /* Return adjusted stack pointer. */
940 return sp;
941 }
942
943 /* Store the return address for the call dummy. For MCore, we've
944 opted to use generic call dummies, so we simply store the
945 CALL_DUMMY_ADDRESS into the PR register (r15). */
946
947 CORE_ADDR
948 mcore_push_return_address (CORE_ADDR pc, CORE_ADDR sp)
949 {
950 write_register (PR_REGNUM, CALL_DUMMY_ADDRESS ());
951 return sp;
952 }
953
954 /* Setting/getting return values from functions.
955
956 The Motorola MCore processors use r2/r3 to return anything
957 not larger than 32 bits. Everything else goes into a caller-
958 supplied buffer, which is passed in via a hidden first
959 argument.
960
961 For gdb, this leaves us two routes, based on what
962 USE_STRUCT_CONVENTION (mcore_use_struct_convention) returns.
963 If this macro returns 1, gdb will call STORE_STRUCT_RETURN and
964 EXTRACT_STRUCT_VALUE_ADDRESS.
965
966 If USE_STRUCT_CONVENTION retruns 0, then gdb uses STORE_RETURN_VALUE
967 and EXTRACT_RETURN_VALUE to store/fetch the functions return value. */
968
969 /* Should we use EXTRACT_STRUCT_VALUE_ADDRESS instead of
970 EXTRACT_RETURN_VALUE? GCC_P is true if compiled with gcc
971 and TYPE is the type (which is known to be struct, union or array). */
972
973 int
974 mcore_use_struct_convention (int gcc_p, struct type *type)
975 {
976 return (TYPE_LENGTH (type) > 8);
977 }
978
979 /* Where is the return value saved? For MCore, a pointer to
980 this buffer was passed as a hidden first argument, so
981 just return that address. */
982
983 CORE_ADDR
984 mcore_extract_struct_value_address (char *regbuf)
985 {
986 return extract_address (regbuf + REGISTER_BYTE (FIRST_ARGREG), REGISTER_SIZE);
987 }
988
989 /* Given a function which returns a value of type TYPE, extract the
990 the function's return value and place the result into VALBUF.
991 REGBUF is the register contents of the target. */
992
993 void
994 mcore_extract_return_value (struct type *type, char *regbuf, char *valbuf)
995 {
996 /* Copy the return value (starting) in RETVAL_REGNUM to VALBUF. */
997 /* Only getting the first byte! if len = 1, we need the last byte of
998 the register, not the first. */
999 memcpy (valbuf, regbuf + REGISTER_BYTE (RETVAL_REGNUM) +
1000 (TYPE_LENGTH (type) < 4 ? 4 - TYPE_LENGTH (type) : 0), TYPE_LENGTH (type));
1001 }
1002
1003 /* Store the return value in VALBUF (of type TYPE) where the caller
1004 expects to see it.
1005
1006 Values less than 32 bits are stored in r2, right justified and
1007 sign or zero extended.
1008
1009 Values between 32 and 64 bits are stored in r2 (most
1010 significant word) and r3 (least significant word, left justified).
1011 Note that this includes structures of less than eight bytes, too. */
1012
1013 void
1014 mcore_store_return_value (struct type *type, char *valbuf)
1015 {
1016 int value_size;
1017 int return_size;
1018 int offset;
1019 char *zeros;
1020
1021 value_size = TYPE_LENGTH (type);
1022
1023 /* Return value fits into registers. */
1024 return_size = (value_size + REGISTER_SIZE - 1) & ~(REGISTER_SIZE - 1);
1025 offset = REGISTER_BYTE (RETVAL_REGNUM) + (return_size - value_size);
1026 zeros = alloca (return_size);
1027 memset (zeros, 0, return_size);
1028
1029 deprecated_write_register_bytes (REGISTER_BYTE (RETVAL_REGNUM), zeros,
1030 return_size);
1031 deprecated_write_register_bytes (offset, valbuf, value_size);
1032 }
1033
1034 /* Initialize our target-dependent "stuff" for this newly created frame.
1035
1036 This includes allocating space for saved registers and analyzing
1037 the prologue of this frame. */
1038
1039 void
1040 mcore_init_extra_frame_info (int fromleaf, struct frame_info *fi)
1041 {
1042 if (fi && fi->next)
1043 deprecated_update_frame_pc_hack (fi, FRAME_SAVED_PC (fi->next));
1044
1045 frame_saved_regs_zalloc (fi);
1046
1047 frame_extra_info_zalloc (fi, sizeof (struct frame_extra_info));
1048 fi->extra_info->status = 0;
1049 fi->extra_info->framesize = 0;
1050
1051 if (DEPRECATED_PC_IN_CALL_DUMMY (get_frame_pc (fi), fi->frame, fi->frame))
1052 {
1053 /* We need to setup fi->frame here because run_stack_dummy gets it wrong
1054 by assuming it's always FP. */
1055 deprecated_update_frame_base_hack (fi, deprecated_read_register_dummy (get_frame_pc (fi), fi->frame, SP_REGNUM));
1056 }
1057 else
1058 mcore_analyze_prologue (fi, 0, 0);
1059 }
1060
1061 /* Get an insturction from memory. */
1062
1063 static int
1064 get_insn (CORE_ADDR pc)
1065 {
1066 char buf[4];
1067 int status = read_memory_nobpt (pc, buf, 2);
1068 if (status != 0)
1069 return 0;
1070
1071 return extract_unsigned_integer (buf, 2);
1072 }
1073
1074 static struct gdbarch *
1075 mcore_gdbarch_init (struct gdbarch_info info, struct gdbarch_list *arches)
1076 {
1077 static LONGEST call_dummy_words[7] = { };
1078 struct gdbarch_tdep *tdep = NULL;
1079 struct gdbarch *gdbarch;
1080
1081 /* find a candidate among the list of pre-declared architectures. */
1082 arches = gdbarch_list_lookup_by_info (arches, &info);
1083 if (arches != NULL)
1084 return (arches->gdbarch);
1085
1086 gdbarch = gdbarch_alloc (&info, 0);
1087
1088 /* NOTE: cagney/2002-12-06: This can be deleted when this arch is
1089 ready to unwind the PC first (see frame.c:get_prev_frame()). */
1090 set_gdbarch_deprecated_init_frame_pc (gdbarch, init_frame_pc_default);
1091
1092 /* Registers: */
1093
1094 /* All registers are 32 bits */
1095 set_gdbarch_register_size (gdbarch, MCORE_REG_SIZE);
1096 set_gdbarch_max_register_raw_size (gdbarch, MCORE_REG_SIZE);
1097 set_gdbarch_max_register_virtual_size (gdbarch, MCORE_REG_SIZE);
1098 set_gdbarch_register_name (gdbarch, mcore_register_name);
1099 set_gdbarch_register_virtual_type (gdbarch, mcore_register_virtual_type);
1100 set_gdbarch_register_virtual_size (gdbarch, mcore_register_size);
1101 set_gdbarch_register_raw_size (gdbarch, mcore_register_size);
1102 set_gdbarch_register_byte (gdbarch, mcore_register_byte);
1103 set_gdbarch_register_bytes (gdbarch, MCORE_REG_SIZE * MCORE_NUM_REGS);
1104 set_gdbarch_num_regs (gdbarch, MCORE_NUM_REGS);
1105 set_gdbarch_pc_regnum (gdbarch, 64);
1106 set_gdbarch_sp_regnum (gdbarch, 0);
1107 set_gdbarch_fp_regnum (gdbarch, 0);
1108
1109 /* Call Dummies: */
1110
1111 set_gdbarch_call_dummy_p (gdbarch, 1);
1112 set_gdbarch_call_dummy_words (gdbarch, call_dummy_words);
1113 set_gdbarch_sizeof_call_dummy_words (gdbarch, 0);
1114 set_gdbarch_call_dummy_start_offset (gdbarch, 0);
1115 set_gdbarch_call_dummy_breakpoint_offset_p (gdbarch, 1);
1116 set_gdbarch_call_dummy_breakpoint_offset (gdbarch, 0);
1117 set_gdbarch_fix_call_dummy (gdbarch, generic_fix_call_dummy);
1118 set_gdbarch_call_dummy_address (gdbarch, entry_point_address);
1119 set_gdbarch_save_dummy_frame_tos (gdbarch, generic_save_dummy_frame_tos);
1120 set_gdbarch_call_dummy_stack_adjust_p (gdbarch, 0);
1121 set_gdbarch_saved_pc_after_call (gdbarch, mcore_saved_pc_after_call);
1122 set_gdbarch_function_start_offset (gdbarch, 0);
1123 set_gdbarch_decr_pc_after_break (gdbarch, 0);
1124 set_gdbarch_breakpoint_from_pc (gdbarch, mcore_breakpoint_from_pc);
1125 set_gdbarch_push_return_address (gdbarch, mcore_push_return_address);
1126 set_gdbarch_push_dummy_frame (gdbarch, generic_push_dummy_frame);
1127 set_gdbarch_push_arguments (gdbarch, mcore_push_arguments);
1128 set_gdbarch_call_dummy_length (gdbarch, 0);
1129
1130 /* Frames: */
1131
1132 set_gdbarch_init_extra_frame_info (gdbarch, mcore_init_extra_frame_info);
1133 set_gdbarch_frame_chain (gdbarch, mcore_frame_chain);
1134 set_gdbarch_frame_init_saved_regs (gdbarch, mcore_frame_init_saved_regs);
1135 set_gdbarch_frame_saved_pc (gdbarch, mcore_frame_saved_pc);
1136 set_gdbarch_deprecated_store_return_value (gdbarch, mcore_store_return_value);
1137 set_gdbarch_deprecated_extract_return_value (gdbarch,
1138 mcore_extract_return_value);
1139 set_gdbarch_store_struct_return (gdbarch, mcore_store_struct_return);
1140 set_gdbarch_deprecated_extract_struct_value_address (gdbarch,
1141 mcore_extract_struct_value_address);
1142 set_gdbarch_skip_prologue (gdbarch, mcore_skip_prologue);
1143 set_gdbarch_frame_args_skip (gdbarch, 0);
1144 set_gdbarch_frame_args_address (gdbarch, mcore_frame_args_address);
1145 set_gdbarch_frame_locals_address (gdbarch, mcore_frame_locals_address);
1146 set_gdbarch_frame_num_args (gdbarch, frame_num_args_unknown);
1147 set_gdbarch_pop_frame (gdbarch, mcore_pop_frame);
1148 set_gdbarch_virtual_frame_pointer (gdbarch, mcore_virtual_frame_pointer);
1149
1150 /* Misc.: */
1151
1152 /* Stack grows down. */
1153 set_gdbarch_inner_than (gdbarch, core_addr_lessthan);
1154 set_gdbarch_use_struct_convention (gdbarch, mcore_use_struct_convention);
1155 set_gdbarch_believe_pcc_promotion (gdbarch, 1);
1156 /* MCore will never pass a sturcture by reference. It will always be split
1157 between registers and stack. */
1158 set_gdbarch_reg_struct_has_addr (gdbarch, mcore_reg_struct_has_addr);
1159
1160 return gdbarch;
1161 }
1162
1163 static void
1164 mcore_dump_tdep (struct gdbarch *current_gdbarch, struct ui_file *file)
1165 {
1166
1167 }
1168
1169 void
1170 _initialize_mcore_tdep (void)
1171 {
1172 extern int print_insn_mcore (bfd_vma, disassemble_info *);
1173 gdbarch_register (bfd_arch_mcore, mcore_gdbarch_init, mcore_dump_tdep);
1174 tm_print_insn = print_insn_mcore;
1175
1176 #ifdef MCORE_DEBUG
1177 add_show_from_set (add_set_cmd ("mcoredebug", no_class,
1178 var_boolean, (char *) &mcore_debug,
1179 "Set mcore debugging.\n", &setlist),
1180 &showlist);
1181 #endif
1182 }
This page took 0.064882 seconds and 5 git commands to generate.