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