Commit | Line | Data |
---|---|---|
c906108c SS |
1 | /* Target-dependent code for the Matsushita MN10300 for GDB, the GNU debugger. |
2 | Copyright 1996, 1997, 1998 Free Software Foundation, Inc. | |
3 | ||
c5aa993b | 4 | This file is part of GDB. |
c906108c | 5 | |
c5aa993b JM |
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. | |
c906108c | 10 | |
c5aa993b JM |
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. | |
c906108c | 15 | |
c5aa993b JM |
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, | |
19 | Boston, MA 02111-1307, USA. */ | |
c906108c SS |
20 | |
21 | #include "defs.h" | |
22 | #include "frame.h" | |
23 | #include "inferior.h" | |
24 | #include "obstack.h" | |
25 | #include "target.h" | |
26 | #include "value.h" | |
27 | #include "bfd.h" | |
28 | #include "gdb_string.h" | |
29 | #include "gdbcore.h" | |
30 | #include "symfile.h" | |
31 | ||
c2c6d25f | 32 | extern void _initialize_mn10300_tdep (void); |
a14ed312 KB |
33 | static CORE_ADDR mn10300_analyze_prologue (struct frame_info *fi, |
34 | CORE_ADDR pc); | |
c906108c SS |
35 | |
36 | /* Additional info used by the frame */ | |
37 | ||
38 | struct frame_extra_info | |
c5aa993b JM |
39 | { |
40 | int status; | |
41 | int stack_size; | |
42 | }; | |
c906108c | 43 | |
0f71a2f6 | 44 | |
c5aa993b JM |
45 | static char *mn10300_generic_register_names[] = |
46 | {"d0", "d1", "d2", "d3", "a0", "a1", "a2", "a3", | |
47 | "sp", "pc", "mdr", "psw", "lir", "lar", "", "", | |
48 | "", "", "", "", "", "", "", "", | |
49 | "", "", "", "", "", "", "", "fp"}; | |
0f71a2f6 JM |
50 | |
51 | static char **mn10300_register_names = mn10300_generic_register_names; | |
c2d11a7d JM |
52 | static char *am33_register_names[] = |
53 | { | |
54 | "d0", "d1", "d2", "d3", "a0", "a1", "a2", "a3", | |
55 | "sp", "pc", "mdr", "psw", "lir", "lar", "", | |
56 | "r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7", | |
57 | "ssp", "msp", "usp", "mcrh", "mcrl", "mcvf", "", "", ""}; | |
58 | static int am33_mode; | |
0f71a2f6 JM |
59 | |
60 | char * | |
fba45db2 | 61 | mn10300_register_name (int i) |
0f71a2f6 JM |
62 | { |
63 | return mn10300_register_names[i]; | |
64 | } | |
65 | ||
66 | CORE_ADDR | |
fba45db2 | 67 | mn10300_saved_pc_after_call (struct frame_info *fi) |
0f71a2f6 JM |
68 | { |
69 | return read_memory_integer (read_register (SP_REGNUM), 4); | |
70 | } | |
71 | ||
72 | void | |
fba45db2 | 73 | mn10300_extract_return_value (struct type *type, char *regbuf, char *valbuf) |
0f71a2f6 JM |
74 | { |
75 | if (TYPE_CODE (type) == TYPE_CODE_PTR) | |
76 | memcpy (valbuf, regbuf + REGISTER_BYTE (4), TYPE_LENGTH (type)); | |
77 | else | |
78 | memcpy (valbuf, regbuf + REGISTER_BYTE (0), TYPE_LENGTH (type)); | |
79 | } | |
80 | ||
81 | CORE_ADDR | |
fba45db2 | 82 | mn10300_extract_struct_value_address (char *regbuf) |
0f71a2f6 JM |
83 | { |
84 | return extract_address (regbuf + REGISTER_BYTE (4), | |
85 | REGISTER_RAW_SIZE (4)); | |
86 | } | |
87 | ||
88 | void | |
fba45db2 | 89 | mn10300_store_return_value (struct type *type, char *valbuf) |
0f71a2f6 JM |
90 | { |
91 | if (TYPE_CODE (type) == TYPE_CODE_PTR) | |
92 | write_register_bytes (REGISTER_BYTE (4), valbuf, TYPE_LENGTH (type)); | |
93 | else | |
94 | write_register_bytes (REGISTER_BYTE (0), valbuf, TYPE_LENGTH (type)); | |
95 | } | |
96 | ||
a14ed312 | 97 | static struct frame_info *analyze_dummy_frame (CORE_ADDR, CORE_ADDR); |
c906108c | 98 | static struct frame_info * |
fba45db2 | 99 | analyze_dummy_frame (CORE_ADDR pc, CORE_ADDR frame) |
c906108c SS |
100 | { |
101 | static struct frame_info *dummy = NULL; | |
102 | if (dummy == NULL) | |
103 | { | |
104 | dummy = xmalloc (sizeof (struct frame_info)); | |
105 | dummy->saved_regs = xmalloc (SIZEOF_FRAME_SAVED_REGS); | |
106 | dummy->extra_info = xmalloc (sizeof (struct frame_extra_info)); | |
107 | } | |
108 | dummy->next = NULL; | |
109 | dummy->prev = NULL; | |
110 | dummy->pc = pc; | |
111 | dummy->frame = frame; | |
112 | dummy->extra_info->status = 0; | |
113 | dummy->extra_info->stack_size = 0; | |
114 | memset (dummy->saved_regs, '\000', SIZEOF_FRAME_SAVED_REGS); | |
115 | mn10300_analyze_prologue (dummy, 0); | |
116 | return dummy; | |
117 | } | |
118 | ||
119 | /* Values for frame_info.status */ | |
120 | ||
121 | #define MY_FRAME_IN_SP 0x1 | |
122 | #define MY_FRAME_IN_FP 0x2 | |
123 | #define NO_MORE_FRAMES 0x4 | |
124 | ||
125 | ||
126 | /* Should call_function allocate stack space for a struct return? */ | |
127 | int | |
fba45db2 | 128 | mn10300_use_struct_convention (int gcc_p, struct type *type) |
c906108c SS |
129 | { |
130 | return (TYPE_NFIELDS (type) > 1 || TYPE_LENGTH (type) > 8); | |
131 | } | |
132 | ||
133 | /* The breakpoint instruction must be the same size as the smallest | |
134 | instruction in the instruction set. | |
135 | ||
136 | The Matsushita mn10x00 processors have single byte instructions | |
137 | so we need a single byte breakpoint. Matsushita hasn't defined | |
138 | one, so we defined it ourselves. */ | |
139 | ||
140 | unsigned char * | |
fba45db2 | 141 | mn10300_breakpoint_from_pc (CORE_ADDR *bp_addr, int *bp_size) |
c906108c | 142 | { |
c5aa993b JM |
143 | static char breakpoint[] = |
144 | {0xff}; | |
c906108c SS |
145 | *bp_size = 1; |
146 | return breakpoint; | |
147 | } | |
148 | ||
149 | ||
150 | /* Fix fi->frame if it's bogus at this point. This is a helper | |
151 | function for mn10300_analyze_prologue. */ | |
152 | ||
153 | static void | |
fba45db2 | 154 | fix_frame_pointer (struct frame_info *fi, int stack_size) |
c906108c SS |
155 | { |
156 | if (fi && fi->next == NULL) | |
157 | { | |
158 | if (fi->extra_info->status & MY_FRAME_IN_SP) | |
159 | fi->frame = read_sp () - stack_size; | |
160 | else if (fi->extra_info->status & MY_FRAME_IN_FP) | |
161 | fi->frame = read_register (A3_REGNUM); | |
162 | } | |
163 | } | |
164 | ||
165 | ||
166 | /* Set offsets of registers saved by movm instruction. | |
167 | This is a helper function for mn10300_analyze_prologue. */ | |
168 | ||
169 | static void | |
fba45db2 | 170 | set_movm_offsets (struct frame_info *fi, int movm_args) |
c906108c SS |
171 | { |
172 | int offset = 0; | |
173 | ||
174 | if (fi == NULL || movm_args == 0) | |
175 | return; | |
176 | ||
177 | if (movm_args & 0x10) | |
178 | { | |
179 | fi->saved_regs[A3_REGNUM] = fi->frame + offset; | |
180 | offset += 4; | |
181 | } | |
182 | if (movm_args & 0x20) | |
183 | { | |
184 | fi->saved_regs[A2_REGNUM] = fi->frame + offset; | |
185 | offset += 4; | |
186 | } | |
187 | if (movm_args & 0x40) | |
188 | { | |
189 | fi->saved_regs[D3_REGNUM] = fi->frame + offset; | |
190 | offset += 4; | |
191 | } | |
192 | if (movm_args & 0x80) | |
193 | { | |
194 | fi->saved_regs[D2_REGNUM] = fi->frame + offset; | |
195 | offset += 4; | |
196 | } | |
c2d11a7d JM |
197 | if (am33_mode && movm_args & 0x02) |
198 | { | |
199 | fi->saved_regs[E0_REGNUM + 5] = fi->frame + offset; | |
200 | fi->saved_regs[E0_REGNUM + 4] = fi->frame + offset + 4; | |
201 | fi->saved_regs[E0_REGNUM + 3] = fi->frame + offset + 8; | |
202 | fi->saved_regs[E0_REGNUM + 2] = fi->frame + offset + 12; | |
203 | } | |
c906108c SS |
204 | } |
205 | ||
206 | ||
207 | /* The main purpose of this file is dealing with prologues to extract | |
208 | information about stack frames and saved registers. | |
209 | ||
210 | For reference here's how prologues look on the mn10300: | |
211 | ||
c5aa993b JM |
212 | With frame pointer: |
213 | movm [d2,d3,a2,a3],sp | |
214 | mov sp,a3 | |
215 | add <size>,sp | |
c906108c | 216 | |
c5aa993b JM |
217 | Without frame pointer: |
218 | movm [d2,d3,a2,a3],sp (if needed) | |
219 | add <size>,sp | |
c906108c SS |
220 | |
221 | One day we might keep the stack pointer constant, that won't | |
222 | change the code for prologues, but it will make the frame | |
223 | pointerless case much more common. */ | |
c5aa993b | 224 | |
c906108c SS |
225 | /* Analyze the prologue to determine where registers are saved, |
226 | the end of the prologue, etc etc. Return the end of the prologue | |
227 | scanned. | |
228 | ||
229 | We store into FI (if non-null) several tidbits of information: | |
230 | ||
c5aa993b JM |
231 | * stack_size -- size of this stack frame. Note that if we stop in |
232 | certain parts of the prologue/epilogue we may claim the size of the | |
233 | current frame is zero. This happens when the current frame has | |
234 | not been allocated yet or has already been deallocated. | |
c906108c | 235 | |
c5aa993b | 236 | * fsr -- Addresses of registers saved in the stack by this frame. |
c906108c | 237 | |
c5aa993b JM |
238 | * status -- A (relatively) generic status indicator. It's a bitmask |
239 | with the following bits: | |
c906108c | 240 | |
c5aa993b JM |
241 | MY_FRAME_IN_SP: The base of the current frame is actually in |
242 | the stack pointer. This can happen for frame pointerless | |
243 | functions, or cases where we're stopped in the prologue/epilogue | |
244 | itself. For these cases mn10300_analyze_prologue will need up | |
245 | update fi->frame before returning or analyzing the register | |
246 | save instructions. | |
c906108c | 247 | |
c5aa993b JM |
248 | MY_FRAME_IN_FP: The base of the current frame is in the |
249 | frame pointer register ($a2). | |
c906108c | 250 | |
c5aa993b JM |
251 | NO_MORE_FRAMES: Set this if the current frame is "start" or |
252 | if the first instruction looks like mov <imm>,sp. This tells | |
253 | frame chain to not bother trying to unwind past this frame. */ | |
c906108c SS |
254 | |
255 | static CORE_ADDR | |
fba45db2 | 256 | mn10300_analyze_prologue (struct frame_info *fi, CORE_ADDR pc) |
c906108c SS |
257 | { |
258 | CORE_ADDR func_addr, func_end, addr, stop; | |
259 | CORE_ADDR stack_size; | |
260 | int imm_size; | |
261 | unsigned char buf[4]; | |
262 | int status, movm_args = 0; | |
263 | char *name; | |
264 | ||
265 | /* Use the PC in the frame if it's provided to look up the | |
266 | start of this function. */ | |
267 | pc = (fi ? fi->pc : pc); | |
268 | ||
269 | /* Find the start of this function. */ | |
270 | status = find_pc_partial_function (pc, &name, &func_addr, &func_end); | |
271 | ||
272 | /* Do nothing if we couldn't find the start of this function or if we're | |
273 | stopped at the first instruction in the prologue. */ | |
274 | if (status == 0) | |
43ff13b4 JM |
275 | { |
276 | return pc; | |
277 | } | |
c906108c SS |
278 | |
279 | /* If we're in start, then give up. */ | |
280 | if (strcmp (name, "start") == 0) | |
281 | { | |
282 | if (fi != NULL) | |
283 | fi->extra_info->status = NO_MORE_FRAMES; | |
284 | return pc; | |
285 | } | |
286 | ||
287 | /* At the start of a function our frame is in the stack pointer. */ | |
288 | if (fi) | |
289 | fi->extra_info->status = MY_FRAME_IN_SP; | |
290 | ||
291 | /* Get the next two bytes into buf, we need two because rets is a two | |
292 | byte insn and the first isn't enough to uniquely identify it. */ | |
293 | status = read_memory_nobpt (pc, buf, 2); | |
294 | if (status != 0) | |
295 | return pc; | |
296 | ||
297 | /* If we're physically on an "rets" instruction, then our frame has | |
298 | already been deallocated. Note this can also be true for retf | |
299 | and ret if they specify a size of zero. | |
300 | ||
301 | In this case fi->frame is bogus, we need to fix it. */ | |
302 | if (fi && buf[0] == 0xf0 && buf[1] == 0xfc) | |
303 | { | |
304 | if (fi->next == NULL) | |
305 | fi->frame = read_sp (); | |
306 | return fi->pc; | |
307 | } | |
308 | ||
309 | /* Similarly if we're stopped on the first insn of a prologue as our | |
310 | frame hasn't been allocated yet. */ | |
311 | if (fi && fi->pc == func_addr) | |
312 | { | |
313 | if (fi->next == NULL) | |
314 | fi->frame = read_sp (); | |
315 | return fi->pc; | |
316 | } | |
317 | ||
318 | /* Figure out where to stop scanning. */ | |
319 | stop = fi ? fi->pc : func_end; | |
320 | ||
321 | /* Don't walk off the end of the function. */ | |
322 | stop = stop > func_end ? func_end : stop; | |
323 | ||
324 | /* Start scanning on the first instruction of this function. */ | |
325 | addr = func_addr; | |
326 | ||
327 | /* Suck in two bytes. */ | |
328 | status = read_memory_nobpt (addr, buf, 2); | |
329 | if (status != 0) | |
330 | { | |
331 | fix_frame_pointer (fi, 0); | |
332 | return addr; | |
333 | } | |
334 | ||
335 | /* First see if this insn sets the stack pointer; if so, it's something | |
336 | we won't understand, so quit now. */ | |
337 | if (buf[0] == 0xf2 && (buf[1] & 0xf3) == 0xf0) | |
338 | { | |
339 | if (fi) | |
340 | fi->extra_info->status = NO_MORE_FRAMES; | |
341 | return addr; | |
342 | } | |
343 | ||
344 | /* Now look for movm [regs],sp, which saves the callee saved registers. | |
345 | ||
346 | At this time we don't know if fi->frame is valid, so we only note | |
347 | that we encountered a movm instruction. Later, we'll set the entries | |
348 | in fsr.regs as needed. */ | |
349 | if (buf[0] == 0xcf) | |
350 | { | |
351 | /* Extract the register list for the movm instruction. */ | |
352 | status = read_memory_nobpt (addr + 1, buf, 1); | |
353 | movm_args = *buf; | |
354 | ||
355 | addr += 2; | |
356 | ||
357 | /* Quit now if we're beyond the stop point. */ | |
358 | if (addr >= stop) | |
359 | { | |
360 | /* Fix fi->frame since it's bogus at this point. */ | |
361 | if (fi && fi->next == NULL) | |
362 | fi->frame = read_sp (); | |
363 | ||
364 | /* Note if/where callee saved registers were saved. */ | |
365 | set_movm_offsets (fi, movm_args); | |
366 | return addr; | |
367 | } | |
368 | ||
369 | /* Get the next two bytes so the prologue scan can continue. */ | |
370 | status = read_memory_nobpt (addr, buf, 2); | |
371 | if (status != 0) | |
372 | { | |
373 | /* Fix fi->frame since it's bogus at this point. */ | |
374 | if (fi && fi->next == NULL) | |
375 | fi->frame = read_sp (); | |
376 | ||
377 | /* Note if/where callee saved registers were saved. */ | |
378 | set_movm_offsets (fi, movm_args); | |
379 | return addr; | |
380 | } | |
381 | } | |
382 | ||
383 | /* Now see if we set up a frame pointer via "mov sp,a3" */ | |
384 | if (buf[0] == 0x3f) | |
385 | { | |
386 | addr += 1; | |
387 | ||
388 | /* The frame pointer is now valid. */ | |
389 | if (fi) | |
390 | { | |
391 | fi->extra_info->status |= MY_FRAME_IN_FP; | |
392 | fi->extra_info->status &= ~MY_FRAME_IN_SP; | |
393 | } | |
394 | ||
395 | /* Quit now if we're beyond the stop point. */ | |
396 | if (addr >= stop) | |
397 | { | |
398 | /* Fix fi->frame if it's bogus at this point. */ | |
399 | fix_frame_pointer (fi, 0); | |
400 | ||
401 | /* Note if/where callee saved registers were saved. */ | |
402 | set_movm_offsets (fi, movm_args); | |
403 | return addr; | |
404 | } | |
405 | ||
406 | /* Get two more bytes so scanning can continue. */ | |
407 | status = read_memory_nobpt (addr, buf, 2); | |
408 | if (status != 0) | |
409 | { | |
410 | /* Fix fi->frame if it's bogus at this point. */ | |
411 | fix_frame_pointer (fi, 0); | |
412 | ||
413 | /* Note if/where callee saved registers were saved. */ | |
414 | set_movm_offsets (fi, movm_args); | |
415 | return addr; | |
416 | } | |
417 | } | |
c5aa993b | 418 | |
c906108c SS |
419 | /* Next we should allocate the local frame. No more prologue insns |
420 | are found after allocating the local frame. | |
c5aa993b | 421 | |
c906108c | 422 | Search for add imm8,sp (0xf8feXX) |
c5aa993b JM |
423 | or add imm16,sp (0xfafeXXXX) |
424 | or add imm32,sp (0xfcfeXXXXXXXX). | |
425 | ||
c906108c SS |
426 | If none of the above was found, then this prologue has no |
427 | additional stack. */ | |
428 | ||
429 | status = read_memory_nobpt (addr, buf, 2); | |
430 | if (status != 0) | |
431 | { | |
432 | /* Fix fi->frame if it's bogus at this point. */ | |
433 | fix_frame_pointer (fi, 0); | |
434 | ||
435 | /* Note if/where callee saved registers were saved. */ | |
436 | set_movm_offsets (fi, movm_args); | |
437 | return addr; | |
438 | } | |
439 | ||
440 | imm_size = 0; | |
441 | if (buf[0] == 0xf8 && buf[1] == 0xfe) | |
442 | imm_size = 1; | |
443 | else if (buf[0] == 0xfa && buf[1] == 0xfe) | |
444 | imm_size = 2; | |
445 | else if (buf[0] == 0xfc && buf[1] == 0xfe) | |
446 | imm_size = 4; | |
447 | ||
448 | if (imm_size != 0) | |
449 | { | |
450 | /* Suck in imm_size more bytes, they'll hold the size of the | |
451 | current frame. */ | |
452 | status = read_memory_nobpt (addr + 2, buf, imm_size); | |
453 | if (status != 0) | |
454 | { | |
455 | /* Fix fi->frame if it's bogus at this point. */ | |
456 | fix_frame_pointer (fi, 0); | |
457 | ||
458 | /* Note if/where callee saved registers were saved. */ | |
459 | set_movm_offsets (fi, movm_args); | |
460 | return addr; | |
461 | } | |
462 | ||
463 | /* Note the size of the stack in the frame info structure. */ | |
464 | stack_size = extract_signed_integer (buf, imm_size); | |
465 | if (fi) | |
466 | fi->extra_info->stack_size = stack_size; | |
467 | ||
468 | /* We just consumed 2 + imm_size bytes. */ | |
469 | addr += 2 + imm_size; | |
470 | ||
471 | /* No more prologue insns follow, so begin preparation to return. */ | |
472 | /* Fix fi->frame if it's bogus at this point. */ | |
473 | fix_frame_pointer (fi, stack_size); | |
474 | ||
475 | /* Note if/where callee saved registers were saved. */ | |
476 | set_movm_offsets (fi, movm_args); | |
477 | return addr; | |
478 | } | |
479 | ||
480 | /* We never found an insn which allocates local stack space, regardless | |
481 | this is the end of the prologue. */ | |
482 | /* Fix fi->frame if it's bogus at this point. */ | |
483 | fix_frame_pointer (fi, 0); | |
484 | ||
485 | /* Note if/where callee saved registers were saved. */ | |
486 | set_movm_offsets (fi, movm_args); | |
487 | return addr; | |
488 | } | |
c5aa993b | 489 | |
c906108c SS |
490 | /* Function: frame_chain |
491 | Figure out and return the caller's frame pointer given current | |
492 | frame_info struct. | |
493 | ||
494 | We don't handle dummy frames yet but we would probably just return the | |
495 | stack pointer that was in use at the time the function call was made? */ | |
496 | ||
497 | CORE_ADDR | |
fba45db2 | 498 | mn10300_frame_chain (struct frame_info *fi) |
c906108c SS |
499 | { |
500 | struct frame_info *dummy; | |
501 | /* Walk through the prologue to determine the stack size, | |
502 | location of saved registers, end of the prologue, etc. */ | |
503 | if (fi->extra_info->status == 0) | |
c5aa993b | 504 | mn10300_analyze_prologue (fi, (CORE_ADDR) 0); |
c906108c SS |
505 | |
506 | /* Quit now if mn10300_analyze_prologue set NO_MORE_FRAMES. */ | |
507 | if (fi->extra_info->status & NO_MORE_FRAMES) | |
508 | return 0; | |
509 | ||
510 | /* Now that we've analyzed our prologue, determine the frame | |
511 | pointer for our caller. | |
512 | ||
c5aa993b JM |
513 | If our caller has a frame pointer, then we need to |
514 | find the entry value of $a3 to our function. | |
515 | ||
516 | If fsr.regs[A3_REGNUM] is nonzero, then it's at the memory | |
517 | location pointed to by fsr.regs[A3_REGNUM]. | |
c906108c | 518 | |
c5aa993b | 519 | Else it's still in $a3. |
c906108c | 520 | |
c5aa993b JM |
521 | If our caller does not have a frame pointer, then his |
522 | frame base is fi->frame + -caller's stack size. */ | |
c906108c | 523 | |
c906108c SS |
524 | /* The easiest way to get that info is to analyze our caller's frame. |
525 | So we set up a dummy frame and call mn10300_analyze_prologue to | |
526 | find stuff for us. */ | |
527 | dummy = analyze_dummy_frame (FRAME_SAVED_PC (fi), fi->frame); | |
528 | ||
529 | if (dummy->extra_info->status & MY_FRAME_IN_FP) | |
530 | { | |
531 | /* Our caller has a frame pointer. So find the frame in $a3 or | |
532 | in the stack. */ | |
533 | if (fi->saved_regs[A3_REGNUM]) | |
534 | return (read_memory_integer (fi->saved_regs[A3_REGNUM], REGISTER_SIZE)); | |
535 | else | |
536 | return read_register (A3_REGNUM); | |
537 | } | |
538 | else | |
539 | { | |
540 | int adjust = 0; | |
541 | ||
542 | adjust += (fi->saved_regs[D2_REGNUM] ? 4 : 0); | |
543 | adjust += (fi->saved_regs[D3_REGNUM] ? 4 : 0); | |
544 | adjust += (fi->saved_regs[A2_REGNUM] ? 4 : 0); | |
545 | adjust += (fi->saved_regs[A3_REGNUM] ? 4 : 0); | |
c2d11a7d JM |
546 | if (am33_mode) |
547 | { | |
548 | adjust += (fi->saved_regs[E0_REGNUM + 5] ? 4 : 0); | |
549 | adjust += (fi->saved_regs[E0_REGNUM + 4] ? 4 : 0); | |
550 | adjust += (fi->saved_regs[E0_REGNUM + 3] ? 4 : 0); | |
551 | adjust += (fi->saved_regs[E0_REGNUM + 2] ? 4 : 0); | |
552 | } | |
c906108c SS |
553 | |
554 | /* Our caller does not have a frame pointer. So his frame starts | |
c5aa993b JM |
555 | at the base of our frame (fi->frame) + register save space |
556 | + <his size>. */ | |
c906108c SS |
557 | return fi->frame + adjust + -dummy->extra_info->stack_size; |
558 | } | |
559 | } | |
560 | ||
561 | /* Function: skip_prologue | |
562 | Return the address of the first inst past the prologue of the function. */ | |
563 | ||
564 | CORE_ADDR | |
fba45db2 | 565 | mn10300_skip_prologue (CORE_ADDR pc) |
c906108c SS |
566 | { |
567 | /* We used to check the debug symbols, but that can lose if | |
568 | we have a null prologue. */ | |
569 | return mn10300_analyze_prologue (NULL, pc); | |
570 | } | |
571 | ||
572 | ||
573 | /* Function: pop_frame | |
574 | This routine gets called when either the user uses the `return' | |
575 | command, or the call dummy breakpoint gets hit. */ | |
576 | ||
577 | void | |
fba45db2 | 578 | mn10300_pop_frame (struct frame_info *frame) |
c906108c SS |
579 | { |
580 | int regnum; | |
581 | ||
c5aa993b | 582 | if (PC_IN_CALL_DUMMY (frame->pc, frame->frame, frame->frame)) |
c906108c SS |
583 | generic_pop_dummy_frame (); |
584 | else | |
585 | { | |
586 | write_register (PC_REGNUM, FRAME_SAVED_PC (frame)); | |
587 | ||
588 | /* Restore any saved registers. */ | |
589 | for (regnum = 0; regnum < NUM_REGS; regnum++) | |
590 | if (frame->saved_regs[regnum] != 0) | |
591 | { | |
592 | ULONGEST value; | |
593 | ||
594 | value = read_memory_unsigned_integer (frame->saved_regs[regnum], | |
c5aa993b | 595 | REGISTER_RAW_SIZE (regnum)); |
c906108c SS |
596 | write_register (regnum, value); |
597 | } | |
598 | ||
599 | /* Actually cut back the stack. */ | |
600 | write_register (SP_REGNUM, FRAME_FP (frame)); | |
601 | ||
602 | /* Don't we need to set the PC?!? XXX FIXME. */ | |
603 | } | |
604 | ||
605 | /* Throw away any cached frame information. */ | |
606 | flush_cached_frames (); | |
607 | } | |
608 | ||
609 | /* Function: push_arguments | |
610 | Setup arguments for a call to the target. Arguments go in | |
611 | order on the stack. */ | |
612 | ||
613 | CORE_ADDR | |
fba45db2 KB |
614 | mn10300_push_arguments (int nargs, value_ptr *args, CORE_ADDR sp, |
615 | unsigned char struct_return, CORE_ADDR struct_addr) | |
c906108c SS |
616 | { |
617 | int argnum = 0; | |
618 | int len = 0; | |
619 | int stack_offset = 0; | |
620 | int regsused = struct_return ? 1 : 0; | |
621 | ||
622 | /* This should be a nop, but align the stack just in case something | |
623 | went wrong. Stacks are four byte aligned on the mn10300. */ | |
624 | sp &= ~3; | |
625 | ||
626 | /* Now make space on the stack for the args. | |
627 | ||
628 | XXX This doesn't appear to handle pass-by-invisible reference | |
629 | arguments. */ | |
630 | for (argnum = 0; argnum < nargs; argnum++) | |
631 | { | |
632 | int arg_length = (TYPE_LENGTH (VALUE_TYPE (args[argnum])) + 3) & ~3; | |
633 | ||
634 | while (regsused < 2 && arg_length > 0) | |
635 | { | |
636 | regsused++; | |
637 | arg_length -= 4; | |
638 | } | |
639 | len += arg_length; | |
640 | } | |
641 | ||
642 | /* Allocate stack space. */ | |
643 | sp -= len; | |
644 | ||
645 | regsused = struct_return ? 1 : 0; | |
646 | /* Push all arguments onto the stack. */ | |
647 | for (argnum = 0; argnum < nargs; argnum++) | |
648 | { | |
649 | int len; | |
650 | char *val; | |
651 | ||
652 | /* XXX Check this. What about UNIONS? */ | |
653 | if (TYPE_CODE (VALUE_TYPE (*args)) == TYPE_CODE_STRUCT | |
654 | && TYPE_LENGTH (VALUE_TYPE (*args)) > 8) | |
655 | { | |
656 | /* XXX Wrong, we want a pointer to this argument. */ | |
c5aa993b JM |
657 | len = TYPE_LENGTH (VALUE_TYPE (*args)); |
658 | val = (char *) VALUE_CONTENTS (*args); | |
c906108c SS |
659 | } |
660 | else | |
661 | { | |
662 | len = TYPE_LENGTH (VALUE_TYPE (*args)); | |
c5aa993b | 663 | val = (char *) VALUE_CONTENTS (*args); |
c906108c SS |
664 | } |
665 | ||
666 | while (regsused < 2 && len > 0) | |
667 | { | |
668 | write_register (regsused, extract_unsigned_integer (val, 4)); | |
669 | val += 4; | |
670 | len -= 4; | |
671 | regsused++; | |
672 | } | |
673 | ||
674 | while (len > 0) | |
675 | { | |
676 | write_memory (sp + stack_offset, val, 4); | |
677 | len -= 4; | |
678 | val += 4; | |
679 | stack_offset += 4; | |
680 | } | |
681 | ||
682 | args++; | |
683 | } | |
684 | ||
685 | /* Make space for the flushback area. */ | |
686 | sp -= 8; | |
687 | return sp; | |
688 | } | |
689 | ||
690 | /* Function: push_return_address (pc) | |
691 | Set up the return address for the inferior function call. | |
692 | Needed for targets where we don't actually execute a JSR/BSR instruction */ | |
c5aa993b | 693 | |
c906108c | 694 | CORE_ADDR |
fba45db2 | 695 | mn10300_push_return_address (CORE_ADDR pc, CORE_ADDR sp) |
c906108c SS |
696 | { |
697 | unsigned char buf[4]; | |
698 | ||
699 | store_unsigned_integer (buf, 4, CALL_DUMMY_ADDRESS ()); | |
700 | write_memory (sp - 4, buf, 4); | |
701 | return sp - 4; | |
702 | } | |
703 | ||
704 | /* Function: store_struct_return (addr,sp) | |
705 | Store the structure value return address for an inferior function | |
706 | call. */ | |
c5aa993b | 707 | |
c906108c | 708 | CORE_ADDR |
fba45db2 | 709 | mn10300_store_struct_return (CORE_ADDR addr, CORE_ADDR sp) |
c906108c SS |
710 | { |
711 | /* The structure return address is passed as the first argument. */ | |
712 | write_register (0, addr); | |
713 | return sp; | |
714 | } | |
c5aa993b | 715 | |
c906108c SS |
716 | /* Function: frame_saved_pc |
717 | Find the caller of this frame. We do this by seeing if RP_REGNUM | |
718 | is saved in the stack anywhere, otherwise we get it from the | |
719 | registers. If the inner frame is a dummy frame, return its PC | |
720 | instead of RP, because that's where "caller" of the dummy-frame | |
721 | will be found. */ | |
722 | ||
723 | CORE_ADDR | |
fba45db2 | 724 | mn10300_frame_saved_pc (struct frame_info *fi) |
c906108c SS |
725 | { |
726 | int adjust = 0; | |
727 | ||
728 | adjust += (fi->saved_regs[D2_REGNUM] ? 4 : 0); | |
729 | adjust += (fi->saved_regs[D3_REGNUM] ? 4 : 0); | |
730 | adjust += (fi->saved_regs[A2_REGNUM] ? 4 : 0); | |
731 | adjust += (fi->saved_regs[A3_REGNUM] ? 4 : 0); | |
c2d11a7d JM |
732 | if (am33_mode) |
733 | { | |
734 | adjust += (fi->saved_regs[E0_REGNUM + 5] ? 4 : 0); | |
735 | adjust += (fi->saved_regs[E0_REGNUM + 4] ? 4 : 0); | |
736 | adjust += (fi->saved_regs[E0_REGNUM + 3] ? 4 : 0); | |
737 | adjust += (fi->saved_regs[E0_REGNUM + 2] ? 4 : 0); | |
738 | } | |
c906108c SS |
739 | |
740 | return (read_memory_integer (fi->frame + adjust, REGISTER_SIZE)); | |
741 | } | |
742 | ||
c906108c SS |
743 | /* Function: mn10300_init_extra_frame_info |
744 | Setup the frame's frame pointer, pc, and frame addresses for saved | |
745 | registers. Most of the work is done in mn10300_analyze_prologue(). | |
746 | ||
747 | Note that when we are called for the last frame (currently active frame), | |
748 | that fi->pc and fi->frame will already be setup. However, fi->frame will | |
749 | be valid only if this routine uses FP. For previous frames, fi-frame will | |
750 | always be correct. mn10300_analyze_prologue will fix fi->frame if | |
751 | it's not valid. | |
752 | ||
753 | We can be called with the PC in the call dummy under two circumstances. | |
754 | First, during normal backtracing, second, while figuring out the frame | |
755 | pointer just prior to calling the target function (see run_stack_dummy). */ | |
756 | ||
757 | void | |
fba45db2 | 758 | mn10300_init_extra_frame_info (struct frame_info *fi) |
c906108c SS |
759 | { |
760 | if (fi->next) | |
761 | fi->pc = FRAME_SAVED_PC (fi->next); | |
762 | ||
763 | frame_saved_regs_zalloc (fi); | |
764 | fi->extra_info = (struct frame_extra_info *) | |
765 | frame_obstack_alloc (sizeof (struct frame_extra_info)); | |
766 | ||
767 | fi->extra_info->status = 0; | |
768 | fi->extra_info->stack_size = 0; | |
769 | ||
770 | mn10300_analyze_prologue (fi, 0); | |
771 | } | |
772 | ||
773 | /* Function: mn10300_virtual_frame_pointer | |
774 | Return the register that the function uses for a frame pointer, | |
775 | plus any necessary offset to be applied to the register before | |
776 | any frame pointer offsets. */ | |
777 | ||
778 | void | |
fba45db2 | 779 | mn10300_virtual_frame_pointer (CORE_ADDR pc, long *reg, long *offset) |
c906108c SS |
780 | { |
781 | struct frame_info *dummy = analyze_dummy_frame (pc, 0); | |
782 | /* Set up a dummy frame_info, Analyze the prolog and fill in the | |
783 | extra info. */ | |
784 | /* Results will tell us which type of frame it uses. */ | |
785 | if (dummy->extra_info->status & MY_FRAME_IN_SP) | |
786 | { | |
c5aa993b | 787 | *reg = SP_REGNUM; |
c906108c SS |
788 | *offset = -(dummy->extra_info->stack_size); |
789 | } | |
790 | else | |
791 | { | |
c5aa993b | 792 | *reg = A3_REGNUM; |
c906108c SS |
793 | *offset = 0; |
794 | } | |
795 | } | |
c5aa993b | 796 | |
c906108c SS |
797 | /* This can be made more generic later. */ |
798 | static void | |
fba45db2 | 799 | set_machine_hook (char *filename) |
c906108c SS |
800 | { |
801 | int i; | |
802 | ||
803 | if (bfd_get_mach (exec_bfd) == bfd_mach_mn10300 | |
804 | || bfd_get_mach (exec_bfd) == 0) | |
805 | { | |
806 | mn10300_register_names = mn10300_generic_register_names; | |
807 | } | |
808 | ||
c2d11a7d JM |
809 | am33_mode = 0; |
810 | if (bfd_get_mach (exec_bfd) == bfd_mach_am33) | |
811 | { | |
812 | ||
813 | mn10300_register_names = am33_register_names; | |
814 | am33_mode = 1; | |
815 | } | |
c906108c SS |
816 | } |
817 | ||
818 | void | |
fba45db2 | 819 | _initialize_mn10300_tdep (void) |
c906108c SS |
820 | { |
821 | /* printf("_initialize_mn10300_tdep\n"); */ | |
822 | ||
823 | tm_print_insn = print_insn_mn10300; | |
824 | ||
825 | specify_exec_file_hook (set_machine_hook); | |
826 | } |