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