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