Commit | Line | Data |
---|---|---|
c906108c SS |
1 | /* Target-machine dependent code for the Intel 960 |
2 | Copyright 1991, 1992, 1993, 1994, 1995 Free Software Foundation, Inc. | |
3 | Contributed by Intel Corporation. | |
4 | examine_prologue and other parts contributed by Wind River Systems. | |
5 | ||
c5aa993b | 6 | This file is part of GDB. |
c906108c | 7 | |
c5aa993b JM |
8 | This program is free software; you can redistribute it and/or modify |
9 | it under the terms of the GNU General Public License as published by | |
10 | the Free Software Foundation; either version 2 of the License, or | |
11 | (at your option) any later version. | |
c906108c | 12 | |
c5aa993b JM |
13 | This program is distributed in the hope that it will be useful, |
14 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
16 | GNU General Public License for more details. | |
c906108c | 17 | |
c5aa993b JM |
18 | You should have received a copy of the GNU General Public License |
19 | along with this program; if not, write to the Free Software | |
20 | Foundation, Inc., 59 Temple Place - Suite 330, | |
21 | Boston, MA 02111-1307, USA. */ | |
c906108c SS |
22 | |
23 | #include "defs.h" | |
24 | #include "symtab.h" | |
25 | #include "value.h" | |
26 | #include "frame.h" | |
27 | #include "floatformat.h" | |
28 | #include "target.h" | |
29 | #include "gdbcore.h" | |
2acceee2 | 30 | #include "inferior.h" |
c906108c | 31 | |
a14ed312 KB |
32 | static CORE_ADDR next_insn (CORE_ADDR memaddr, |
33 | unsigned int *pword1, unsigned int *pword2); | |
c906108c SS |
34 | |
35 | /* Does the specified function use the "struct returning" convention | |
36 | or the "value returning" convention? The "value returning" convention | |
37 | almost invariably returns the entire value in registers. The | |
38 | "struct returning" convention often returns the entire value in | |
39 | memory, and passes a pointer (out of or into the function) saying | |
40 | where the value (is or should go). | |
41 | ||
42 | Since this sometimes depends on whether it was compiled with GCC, | |
43 | this is also an argument. This is used in call_function to build a | |
44 | stack, and in value_being_returned to print return values. | |
45 | ||
46 | On i960, a structure is returned in registers g0-g3, if it will fit. | |
47 | If it's more than 16 bytes long, g13 pointed to it on entry. */ | |
48 | ||
49 | int | |
fba45db2 | 50 | i960_use_struct_convention (int gcc_p, struct type *type) |
c906108c SS |
51 | { |
52 | return (TYPE_LENGTH (type) > 16); | |
53 | } | |
54 | ||
55 | /* gdb960 is always running on a non-960 host. Check its characteristics. | |
56 | This routine must be called as part of gdb initialization. */ | |
57 | ||
58 | static void | |
fba45db2 | 59 | check_host (void) |
c906108c | 60 | { |
c5aa993b | 61 | int i; |
c906108c | 62 | |
c5aa993b JM |
63 | static struct typestruct |
64 | { | |
65 | int hostsize; /* Size of type on host */ | |
66 | int i960size; /* Size of type on i960 */ | |
67 | char *typename; /* Name of type, for error msg */ | |
68 | } | |
69 | types[] = | |
70 | { | |
71 | { | |
72 | sizeof (short), 2, "short" | |
73 | } | |
74 | , | |
75 | { | |
76 | sizeof (int), 4, "int" | |
77 | } | |
78 | , | |
79 | { | |
80 | sizeof (long), 4, "long" | |
81 | } | |
82 | , | |
83 | { | |
84 | sizeof (float), 4, "float" | |
85 | } | |
86 | , | |
87 | { | |
88 | sizeof (double), 8, "double" | |
89 | } | |
90 | , | |
91 | { | |
92 | sizeof (char *), 4, "pointer" | |
93 | } | |
94 | , | |
95 | }; | |
96 | #define TYPELEN (sizeof(types) / sizeof(struct typestruct)) | |
c906108c | 97 | |
c5aa993b JM |
98 | /* Make sure that host type sizes are same as i960 |
99 | */ | |
100 | for (i = 0; i < TYPELEN; i++) | |
101 | { | |
102 | if (types[i].hostsize != types[i].i960size) | |
103 | { | |
104 | printf_unfiltered ("sizeof(%s) != %d: PROCEED AT YOUR OWN RISK!\n", | |
105 | types[i].typename, types[i].i960size); | |
c906108c | 106 | } |
c5aa993b JM |
107 | |
108 | } | |
c906108c SS |
109 | } |
110 | \f | |
111 | /* Examine an i960 function prologue, recording the addresses at which | |
112 | registers are saved explicitly by the prologue code, and returning | |
113 | the address of the first instruction after the prologue (but not | |
114 | after the instruction at address LIMIT, as explained below). | |
115 | ||
116 | LIMIT places an upper bound on addresses of the instructions to be | |
117 | examined. If the prologue code scan reaches LIMIT, the scan is | |
118 | aborted and LIMIT is returned. This is used, when examining the | |
119 | prologue for the current frame, to keep examine_prologue () from | |
120 | claiming that a given register has been saved when in fact the | |
121 | instruction that saves it has not yet been executed. LIMIT is used | |
122 | at other times to stop the scan when we hit code after the true | |
123 | function prologue (e.g. for the first source line) which might | |
124 | otherwise be mistaken for function prologue. | |
125 | ||
126 | The format of the function prologue matched by this routine is | |
127 | derived from examination of the source to gcc960 1.21, particularly | |
128 | the routine i960_function_prologue (). A "regular expression" for | |
129 | the function prologue is given below: | |
130 | ||
131 | (lda LRn, g14 | |
c5aa993b JM |
132 | mov g14, g[0-7] |
133 | (mov 0, g14) | (lda 0, g14))? | |
c906108c SS |
134 | |
135 | (mov[qtl]? g[0-15], r[4-15])* | |
136 | ((addo [1-31], sp, sp) | (lda n(sp), sp))? | |
137 | (st[qtl]? g[0-15], n(fp))* | |
138 | ||
139 | (cmpobne 0, g14, LFn | |
c5aa993b JM |
140 | mov sp, g14 |
141 | lda 0x30(sp), sp | |
142 | LFn: stq g0, (g14) | |
143 | stq g4, 0x10(g14) | |
144 | stq g8, 0x20(g14))? | |
c906108c SS |
145 | |
146 | (st g14, n(fp))? | |
147 | (mov g13,r[4-15])? | |
c5aa993b | 148 | */ |
c906108c SS |
149 | |
150 | /* Macros for extracting fields from i960 instructions. */ | |
151 | ||
152 | #define BITMASK(pos, width) (((0x1 << (width)) - 1) << (pos)) | |
153 | #define EXTRACT_FIELD(val, pos, width) ((val) >> (pos) & BITMASK (0, width)) | |
154 | ||
155 | #define REG_SRC1(insn) EXTRACT_FIELD (insn, 0, 5) | |
156 | #define REG_SRC2(insn) EXTRACT_FIELD (insn, 14, 5) | |
157 | #define REG_SRCDST(insn) EXTRACT_FIELD (insn, 19, 5) | |
158 | #define MEM_SRCDST(insn) EXTRACT_FIELD (insn, 19, 5) | |
159 | #define MEMA_OFFSET(insn) EXTRACT_FIELD (insn, 0, 12) | |
160 | ||
161 | /* Fetch the instruction at ADDR, returning 0 if ADDR is beyond LIM or | |
162 | is not the address of a valid instruction, the address of the next | |
163 | instruction beyond ADDR otherwise. *PWORD1 receives the first word | |
164 | of the instruction, and (for two-word instructions), *PWORD2 receives | |
165 | the second. */ | |
166 | ||
167 | #define NEXT_PROLOGUE_INSN(addr, lim, pword1, pword2) \ | |
168 | (((addr) < (lim)) ? next_insn (addr, pword1, pword2) : 0) | |
169 | ||
170 | static CORE_ADDR | |
fba45db2 KB |
171 | examine_prologue (register CORE_ADDR ip, register CORE_ADDR limit, |
172 | CORE_ADDR frame_addr, struct frame_saved_regs *fsr) | |
c906108c SS |
173 | { |
174 | register CORE_ADDR next_ip; | |
175 | register int src, dst; | |
176 | register unsigned int *pcode; | |
177 | unsigned int insn1, insn2; | |
178 | int size; | |
179 | int within_leaf_prologue; | |
180 | CORE_ADDR save_addr; | |
c5aa993b JM |
181 | static unsigned int varargs_prologue_code[] = |
182 | { | |
183 | 0x3507a00c, /* cmpobne 0x0, g14, LFn */ | |
184 | 0x5cf01601, /* mov sp, g14 */ | |
185 | 0x8c086030, /* lda 0x30(sp), sp */ | |
186 | 0xb2879000, /* LFn: stq g0, (g14) */ | |
187 | 0xb2a7a010, /* stq g4, 0x10(g14) */ | |
188 | 0xb2c7a020 /* stq g8, 0x20(g14) */ | |
189 | }; | |
c906108c SS |
190 | |
191 | /* Accept a leaf procedure prologue code fragment if present. | |
192 | Note that ip might point to either the leaf or non-leaf | |
193 | entry point; we look for the non-leaf entry point first: */ | |
194 | ||
195 | within_leaf_prologue = 0; | |
196 | if ((next_ip = NEXT_PROLOGUE_INSN (ip, limit, &insn1, &insn2)) | |
c5aa993b JM |
197 | && ((insn1 & 0xfffff000) == 0x8cf00000 /* lda LRx, g14 (MEMA) */ |
198 | || (insn1 & 0xfffffc60) == 0x8cf03000)) /* lda LRx, g14 (MEMB) */ | |
c906108c SS |
199 | { |
200 | within_leaf_prologue = 1; | |
201 | next_ip = NEXT_PROLOGUE_INSN (next_ip, limit, &insn1, &insn2); | |
202 | } | |
203 | ||
204 | /* Now look for the prologue code at a leaf entry point: */ | |
205 | ||
206 | if (next_ip | |
c5aa993b | 207 | && (insn1 & 0xff87ffff) == 0x5c80161e /* mov g14, gx */ |
c906108c SS |
208 | && REG_SRCDST (insn1) <= G0_REGNUM + 7) |
209 | { | |
210 | within_leaf_prologue = 1; | |
211 | if ((next_ip = NEXT_PROLOGUE_INSN (next_ip, limit, &insn1, &insn2)) | |
c5aa993b JM |
212 | && (insn1 == 0x8cf00000 /* lda 0, g14 */ |
213 | || insn1 == 0x5cf01e00)) /* mov 0, g14 */ | |
c906108c SS |
214 | { |
215 | ip = next_ip; | |
216 | next_ip = NEXT_PROLOGUE_INSN (ip, limit, &insn1, &insn2); | |
217 | within_leaf_prologue = 0; | |
218 | } | |
219 | } | |
220 | ||
221 | /* If something that looks like the beginning of a leaf prologue | |
222 | has been seen, but the remainder of the prologue is missing, bail. | |
223 | We don't know what we've got. */ | |
224 | ||
225 | if (within_leaf_prologue) | |
226 | return (ip); | |
c5aa993b | 227 | |
c906108c SS |
228 | /* Accept zero or more instances of "mov[qtl]? gx, ry", where y >= 4. |
229 | This may cause us to mistake the moving of a register | |
230 | parameter to a local register for the saving of a callee-saved | |
231 | register, but that can't be helped, since with the | |
232 | "-fcall-saved" flag, any register can be made callee-saved. */ | |
233 | ||
234 | while (next_ip | |
235 | && (insn1 & 0xfc802fb0) == 0x5c000610 | |
236 | && (dst = REG_SRCDST (insn1)) >= (R0_REGNUM + 4)) | |
237 | { | |
238 | src = REG_SRC1 (insn1); | |
239 | size = EXTRACT_FIELD (insn1, 24, 2) + 1; | |
240 | save_addr = frame_addr + ((dst - R0_REGNUM) * 4); | |
241 | while (size--) | |
242 | { | |
243 | fsr->regs[src++] = save_addr; | |
244 | save_addr += 4; | |
245 | } | |
246 | ip = next_ip; | |
247 | next_ip = NEXT_PROLOGUE_INSN (ip, limit, &insn1, &insn2); | |
248 | } | |
249 | ||
250 | /* Accept an optional "addo n, sp, sp" or "lda n(sp), sp". */ | |
251 | ||
252 | if (next_ip && | |
253 | ((insn1 & 0xffffffe0) == 0x59084800 /* addo n, sp, sp */ | |
254 | || (insn1 & 0xfffff000) == 0x8c086000 /* lda n(sp), sp (MEMA) */ | |
255 | || (insn1 & 0xfffffc60) == 0x8c087400)) /* lda n(sp), sp (MEMB) */ | |
256 | { | |
257 | ip = next_ip; | |
258 | next_ip = NEXT_PROLOGUE_INSN (ip, limit, &insn1, &insn2); | |
259 | } | |
260 | ||
261 | /* Accept zero or more instances of "st[qtl]? gx, n(fp)". | |
262 | This may cause us to mistake the copying of a register | |
263 | parameter to the frame for the saving of a callee-saved | |
264 | register, but that can't be helped, since with the | |
265 | "-fcall-saved" flag, any register can be made callee-saved. | |
266 | We can, however, refuse to accept a save of register g14, | |
267 | since that is matched explicitly below. */ | |
268 | ||
269 | while (next_ip && | |
c5aa993b JM |
270 | ((insn1 & 0xf787f000) == 0x9287e000 /* stl? gx, n(fp) (MEMA) */ |
271 | || (insn1 & 0xf787fc60) == 0x9287f400 /* stl? gx, n(fp) (MEMB) */ | |
272 | || (insn1 & 0xef87f000) == 0xa287e000 /* st[tq] gx, n(fp) (MEMA) */ | |
273 | || (insn1 & 0xef87fc60) == 0xa287f400) /* st[tq] gx, n(fp) (MEMB) */ | |
c906108c SS |
274 | && ((src = MEM_SRCDST (insn1)) != G14_REGNUM)) |
275 | { | |
276 | save_addr = frame_addr + ((insn1 & BITMASK (12, 1)) | |
277 | ? insn2 : MEMA_OFFSET (insn1)); | |
278 | size = (insn1 & BITMASK (29, 1)) ? ((insn1 & BITMASK (28, 1)) ? 4 : 3) | |
c5aa993b | 279 | : ((insn1 & BITMASK (27, 1)) ? 2 : 1); |
c906108c SS |
280 | while (size--) |
281 | { | |
282 | fsr->regs[src++] = save_addr; | |
283 | save_addr += 4; | |
284 | } | |
285 | ip = next_ip; | |
286 | next_ip = NEXT_PROLOGUE_INSN (ip, limit, &insn1, &insn2); | |
287 | } | |
288 | ||
289 | /* Accept the varargs prologue code if present. */ | |
290 | ||
291 | size = sizeof (varargs_prologue_code) / sizeof (int); | |
292 | pcode = varargs_prologue_code; | |
293 | while (size-- && next_ip && *pcode++ == insn1) | |
294 | { | |
295 | ip = next_ip; | |
296 | next_ip = NEXT_PROLOGUE_INSN (ip, limit, &insn1, &insn2); | |
297 | } | |
298 | ||
299 | /* Accept an optional "st g14, n(fp)". */ | |
300 | ||
301 | if (next_ip && | |
c5aa993b JM |
302 | ((insn1 & 0xfffff000) == 0x92f7e000 /* st g14, n(fp) (MEMA) */ |
303 | || (insn1 & 0xfffffc60) == 0x92f7f400)) /* st g14, n(fp) (MEMB) */ | |
c906108c SS |
304 | { |
305 | fsr->regs[G14_REGNUM] = frame_addr + ((insn1 & BITMASK (12, 1)) | |
c5aa993b | 306 | ? insn2 : MEMA_OFFSET (insn1)); |
c906108c SS |
307 | ip = next_ip; |
308 | next_ip = NEXT_PROLOGUE_INSN (ip, limit, &insn1, &insn2); | |
309 | } | |
310 | ||
311 | /* Accept zero or one instance of "mov g13, ry", where y >= 4. | |
312 | This is saving the address where a struct should be returned. */ | |
313 | ||
314 | if (next_ip | |
315 | && (insn1 & 0xff802fbf) == 0x5c00061d | |
316 | && (dst = REG_SRCDST (insn1)) >= (R0_REGNUM + 4)) | |
317 | { | |
318 | save_addr = frame_addr + ((dst - R0_REGNUM) * 4); | |
c5aa993b | 319 | fsr->regs[G0_REGNUM + 13] = save_addr; |
c906108c | 320 | ip = next_ip; |
c5aa993b | 321 | #if 0 /* We'll need this once there is a subsequent instruction examined. */ |
c906108c SS |
322 | next_ip = NEXT_PROLOGUE_INSN (ip, limit, &insn1, &insn2); |
323 | #endif | |
324 | } | |
325 | ||
326 | return (ip); | |
327 | } | |
328 | ||
329 | /* Given an ip value corresponding to the start of a function, | |
330 | return the ip of the first instruction after the function | |
331 | prologue. */ | |
332 | ||
333 | CORE_ADDR | |
5c678a12 | 334 | i960_skip_prologue (CORE_ADDR ip) |
c906108c SS |
335 | { |
336 | struct frame_saved_regs saved_regs_dummy; | |
337 | struct symtab_and_line sal; | |
338 | CORE_ADDR limit; | |
339 | ||
340 | sal = find_pc_line (ip, 0); | |
341 | limit = (sal.end) ? sal.end : 0xffffffff; | |
342 | ||
343 | return (examine_prologue (ip, limit, (CORE_ADDR) 0, &saved_regs_dummy)); | |
344 | } | |
345 | ||
346 | /* Put here the code to store, into a struct frame_saved_regs, | |
347 | the addresses of the saved registers of frame described by FRAME_INFO. | |
348 | This includes special registers such as pc and fp saved in special | |
349 | ways in the stack frame. sp is even more special: | |
350 | the address we return for it IS the sp for the next frame. | |
351 | ||
352 | We cache the result of doing this in the frame_obstack, since it is | |
353 | fairly expensive. */ | |
354 | ||
355 | void | |
fba45db2 | 356 | frame_find_saved_regs (struct frame_info *fi, struct frame_saved_regs *fsr) |
c906108c SS |
357 | { |
358 | register CORE_ADDR next_addr; | |
359 | register CORE_ADDR *saved_regs; | |
360 | register int regnum; | |
361 | register struct frame_saved_regs *cache_fsr; | |
362 | CORE_ADDR ip; | |
363 | struct symtab_and_line sal; | |
364 | CORE_ADDR limit; | |
365 | ||
366 | if (!fi->fsr) | |
367 | { | |
368 | cache_fsr = (struct frame_saved_regs *) | |
369 | frame_obstack_alloc (sizeof (struct frame_saved_regs)); | |
370 | memset (cache_fsr, '\0', sizeof (struct frame_saved_regs)); | |
371 | fi->fsr = cache_fsr; | |
372 | ||
373 | /* Find the start and end of the function prologue. If the PC | |
c5aa993b JM |
374 | is in the function prologue, we only consider the part that |
375 | has executed already. */ | |
376 | ||
c906108c SS |
377 | ip = get_pc_function_start (fi->pc); |
378 | sal = find_pc_line (ip, 0); | |
c5aa993b | 379 | limit = (sal.end && sal.end < fi->pc) ? sal.end : fi->pc; |
c906108c SS |
380 | |
381 | examine_prologue (ip, limit, fi->frame, cache_fsr); | |
382 | ||
383 | /* Record the addresses at which the local registers are saved. | |
c5aa993b JM |
384 | Strictly speaking, we should only do this for non-leaf procedures, |
385 | but no one will ever look at these values if it is a leaf procedure, | |
386 | since local registers are always caller-saved. */ | |
c906108c SS |
387 | |
388 | next_addr = (CORE_ADDR) fi->frame; | |
389 | saved_regs = cache_fsr->regs; | |
390 | for (regnum = R0_REGNUM; regnum <= R15_REGNUM; regnum++) | |
391 | { | |
392 | *saved_regs++ = next_addr; | |
393 | next_addr += 4; | |
394 | } | |
395 | ||
396 | cache_fsr->regs[FP_REGNUM] = cache_fsr->regs[PFP_REGNUM]; | |
397 | } | |
398 | ||
399 | *fsr = *fi->fsr; | |
400 | ||
401 | /* Fetch the value of the sp from memory every time, since it | |
402 | is conceivable that it has changed since the cache was flushed. | |
403 | This unfortunately undoes much of the savings from caching the | |
404 | saved register values. I suggest adding an argument to | |
405 | get_frame_saved_regs () specifying the register number we're | |
406 | interested in (or -1 for all registers). This would be passed | |
407 | through to FRAME_FIND_SAVED_REGS (), permitting more efficient | |
408 | computation of saved register addresses (e.g., on the i960, | |
409 | we don't have to examine the prologue to find local registers). | |
c5aa993b | 410 | -- markf@wrs.com |
c906108c SS |
411 | FIXME, we don't need to refetch this, since the cache is cleared |
412 | every time the child process is restarted. If GDB itself | |
413 | modifies SP, it has to clear the cache by hand (does it?). -gnu */ | |
414 | ||
415 | fsr->regs[SP_REGNUM] = read_memory_integer (fsr->regs[SP_REGNUM], 4); | |
416 | } | |
417 | ||
418 | /* Return the address of the argument block for the frame | |
419 | described by FI. Returns 0 if the address is unknown. */ | |
420 | ||
421 | CORE_ADDR | |
fba45db2 | 422 | frame_args_address (struct frame_info *fi, int must_be_correct) |
c906108c SS |
423 | { |
424 | struct frame_saved_regs fsr; | |
425 | CORE_ADDR ap; | |
426 | ||
427 | /* If g14 was saved in the frame by the function prologue code, return | |
428 | the saved value. If the frame is current and we are being sloppy, | |
429 | return the value of g14. Otherwise, return zero. */ | |
430 | ||
431 | get_frame_saved_regs (fi, &fsr); | |
432 | if (fsr.regs[G14_REGNUM]) | |
c5aa993b | 433 | ap = read_memory_integer (fsr.regs[G14_REGNUM], 4); |
c906108c SS |
434 | else |
435 | { | |
436 | if (must_be_correct) | |
c5aa993b | 437 | return 0; /* Don't cache this result */ |
c906108c SS |
438 | if (get_next_frame (fi)) |
439 | ap = 0; | |
440 | else | |
441 | ap = read_register (G14_REGNUM); | |
442 | if (ap == 0) | |
443 | ap = fi->frame; | |
444 | } | |
445 | fi->arg_pointer = ap; /* Cache it for next time */ | |
446 | return ap; | |
447 | } | |
448 | ||
449 | /* Return the address of the return struct for the frame | |
450 | described by FI. Returns 0 if the address is unknown. */ | |
451 | ||
452 | CORE_ADDR | |
fba45db2 | 453 | frame_struct_result_address (struct frame_info *fi) |
c906108c SS |
454 | { |
455 | struct frame_saved_regs fsr; | |
456 | CORE_ADDR ap; | |
457 | ||
458 | /* If the frame is non-current, check to see if g14 was saved in the | |
459 | frame by the function prologue code; return the saved value if so, | |
460 | zero otherwise. If the frame is current, return the value of g14. | |
461 | ||
462 | FIXME, shouldn't this use the saved value as long as we are past | |
463 | the function prologue, and only use the current value if we have | |
464 | no saved value and are at TOS? -- gnu@cygnus.com */ | |
465 | ||
466 | if (get_next_frame (fi)) | |
467 | { | |
468 | get_frame_saved_regs (fi, &fsr); | |
469 | if (fsr.regs[G13_REGNUM]) | |
c5aa993b | 470 | ap = read_memory_integer (fsr.regs[G13_REGNUM], 4); |
c906108c SS |
471 | else |
472 | ap = 0; | |
473 | } | |
474 | else | |
475 | ap = read_register (G13_REGNUM); | |
476 | ||
477 | return ap; | |
478 | } | |
479 | ||
480 | /* Return address to which the currently executing leafproc will return, | |
5c678a12 KB |
481 | or 0 if IP, the value of the instruction pointer from the currently |
482 | executing function, is not in a leafproc (or if we can't tell if it | |
483 | is). | |
c5aa993b | 484 | |
5c678a12 | 485 | Do this by finding the starting address of the routine in which IP lies. |
c906108c SS |
486 | If the instruction there is "mov g14, gx" (where x is in [0,7]), this |
487 | is a leafproc and the return address is in register gx. Well, this is | |
488 | true unless the return address points at a RET instruction in the current | |
489 | procedure, which indicates that we have a 'dual entry' routine that | |
490 | has been entered through the CALL entry point. */ | |
491 | ||
492 | CORE_ADDR | |
5c678a12 | 493 | leafproc_return (CORE_ADDR ip) |
c906108c SS |
494 | { |
495 | register struct minimal_symbol *msymbol; | |
496 | char *p; | |
497 | int dst; | |
498 | unsigned int insn1, insn2; | |
499 | CORE_ADDR return_addr; | |
500 | ||
501 | if ((msymbol = lookup_minimal_symbol_by_pc (ip)) != NULL) | |
502 | { | |
c5aa993b | 503 | if ((p = strchr (SYMBOL_NAME (msymbol), '.')) && STREQ (p, ".lf")) |
c906108c SS |
504 | { |
505 | if (next_insn (SYMBOL_VALUE_ADDRESS (msymbol), &insn1, &insn2) | |
c5aa993b | 506 | && (insn1 & 0xff87ffff) == 0x5c80161e /* mov g14, gx */ |
c906108c SS |
507 | && (dst = REG_SRCDST (insn1)) <= G0_REGNUM + 7) |
508 | { | |
509 | /* Get the return address. If the "mov g14, gx" | |
c5aa993b JM |
510 | instruction hasn't been executed yet, read |
511 | the return address from g14; otherwise, read it | |
512 | from the register into which g14 was moved. */ | |
c906108c SS |
513 | |
514 | return_addr = | |
c5aa993b JM |
515 | read_register ((ip == SYMBOL_VALUE_ADDRESS (msymbol)) |
516 | ? G14_REGNUM : dst); | |
c906108c SS |
517 | |
518 | /* We know we are in a leaf procedure, but we don't know | |
c5aa993b JM |
519 | whether the caller actually did a "bal" to the ".lf" |
520 | entry point, or a normal "call" to the non-leaf entry | |
521 | point one instruction before. In the latter case, the | |
522 | return address will be the address of a "ret" | |
523 | instruction within the procedure itself. We test for | |
524 | this below. */ | |
c906108c SS |
525 | |
526 | if (!next_insn (return_addr, &insn1, &insn2) | |
c5aa993b JM |
527 | || (insn1 & 0xff000000) != 0xa000000 /* ret */ |
528 | || lookup_minimal_symbol_by_pc (return_addr) != msymbol) | |
c906108c SS |
529 | return (return_addr); |
530 | } | |
531 | } | |
532 | } | |
c5aa993b | 533 | |
c906108c SS |
534 | return (0); |
535 | } | |
536 | ||
537 | /* Immediately after a function call, return the saved pc. | |
538 | Can't go through the frames for this because on some machines | |
539 | the new frame is not set up until the new function executes | |
540 | some instructions. | |
541 | On the i960, the frame *is* set up immediately after the call, | |
542 | unless the function is a leaf procedure. */ | |
543 | ||
544 | CORE_ADDR | |
fba45db2 | 545 | saved_pc_after_call (struct frame_info *frame) |
c906108c SS |
546 | { |
547 | CORE_ADDR saved_pc; | |
548 | ||
549 | saved_pc = leafproc_return (get_frame_pc (frame)); | |
550 | if (!saved_pc) | |
551 | saved_pc = FRAME_SAVED_PC (frame); | |
552 | ||
553 | return saved_pc; | |
554 | } | |
555 | ||
556 | /* Discard from the stack the innermost frame, | |
557 | restoring all saved registers. */ | |
558 | ||
559 | void | |
d4f3574e | 560 | i960_pop_frame (void) |
c906108c SS |
561 | { |
562 | register struct frame_info *current_fi, *prev_fi; | |
563 | register int i; | |
564 | CORE_ADDR save_addr; | |
565 | CORE_ADDR leaf_return_addr; | |
566 | struct frame_saved_regs fsr; | |
567 | char local_regs_buf[16 * 4]; | |
568 | ||
569 | current_fi = get_current_frame (); | |
570 | ||
571 | /* First, undo what the hardware does when we return. | |
572 | If this is a non-leaf procedure, restore local registers from | |
573 | the save area in the calling frame. Otherwise, load the return | |
574 | address obtained from leafproc_return () into the rip. */ | |
575 | ||
576 | leaf_return_addr = leafproc_return (current_fi->pc); | |
577 | if (!leaf_return_addr) | |
578 | { | |
579 | /* Non-leaf procedure. Restore local registers, incl IP. */ | |
580 | prev_fi = get_prev_frame (current_fi); | |
581 | read_memory (prev_fi->frame, local_regs_buf, sizeof (local_regs_buf)); | |
c5aa993b JM |
582 | write_register_bytes (REGISTER_BYTE (R0_REGNUM), local_regs_buf, |
583 | sizeof (local_regs_buf)); | |
c906108c SS |
584 | |
585 | /* Restore frame pointer. */ | |
586 | write_register (FP_REGNUM, prev_fi->frame); | |
587 | } | |
588 | else | |
589 | { | |
590 | /* Leaf procedure. Just restore the return address into the IP. */ | |
591 | write_register (RIP_REGNUM, leaf_return_addr); | |
592 | } | |
593 | ||
594 | /* Now restore any global regs that the current function had saved. */ | |
595 | get_frame_saved_regs (current_fi, &fsr); | |
596 | for (i = G0_REGNUM; i < G14_REGNUM; i++) | |
597 | { | |
7a292a7a SS |
598 | save_addr = fsr.regs[i]; |
599 | if (save_addr != 0) | |
c906108c SS |
600 | write_register (i, read_memory_integer (save_addr, 4)); |
601 | } | |
602 | ||
603 | /* Flush the frame cache, create a frame for the new innermost frame, | |
604 | and make it the current frame. */ | |
605 | ||
606 | flush_cached_frames (); | |
607 | } | |
608 | ||
609 | /* Given a 960 stop code (fault or trace), return the signal which | |
610 | corresponds. */ | |
611 | ||
612 | enum target_signal | |
fba45db2 | 613 | i960_fault_to_signal (int fault) |
c906108c SS |
614 | { |
615 | switch (fault) | |
616 | { | |
c5aa993b JM |
617 | case 0: |
618 | return TARGET_SIGNAL_BUS; /* parallel fault */ | |
619 | case 1: | |
620 | return TARGET_SIGNAL_UNKNOWN; | |
621 | case 2: | |
622 | return TARGET_SIGNAL_ILL; /* operation fault */ | |
623 | case 3: | |
624 | return TARGET_SIGNAL_FPE; /* arithmetic fault */ | |
625 | case 4: | |
626 | return TARGET_SIGNAL_FPE; /* floating point fault */ | |
c906108c SS |
627 | |
628 | /* constraint fault. This appears not to distinguish between | |
c5aa993b JM |
629 | a range constraint fault (which should be SIGFPE) and a privileged |
630 | fault (which should be SIGILL). */ | |
631 | case 5: | |
632 | return TARGET_SIGNAL_ILL; | |
c906108c | 633 | |
c5aa993b JM |
634 | case 6: |
635 | return TARGET_SIGNAL_SEGV; /* virtual memory fault */ | |
c906108c SS |
636 | |
637 | /* protection fault. This is for an out-of-range argument to | |
c5aa993b JM |
638 | "calls". I guess it also could be SIGILL. */ |
639 | case 7: | |
640 | return TARGET_SIGNAL_SEGV; | |
641 | ||
642 | case 8: | |
643 | return TARGET_SIGNAL_BUS; /* machine fault */ | |
644 | case 9: | |
645 | return TARGET_SIGNAL_BUS; /* structural fault */ | |
646 | case 0xa: | |
647 | return TARGET_SIGNAL_ILL; /* type fault */ | |
648 | case 0xb: | |
649 | return TARGET_SIGNAL_UNKNOWN; /* reserved fault */ | |
650 | case 0xc: | |
651 | return TARGET_SIGNAL_BUS; /* process fault */ | |
652 | case 0xd: | |
653 | return TARGET_SIGNAL_SEGV; /* descriptor fault */ | |
654 | case 0xe: | |
655 | return TARGET_SIGNAL_BUS; /* event fault */ | |
656 | case 0xf: | |
657 | return TARGET_SIGNAL_UNKNOWN; /* reserved fault */ | |
658 | case 0x10: | |
659 | return TARGET_SIGNAL_TRAP; /* single-step trace */ | |
660 | case 0x11: | |
661 | return TARGET_SIGNAL_TRAP; /* branch trace */ | |
662 | case 0x12: | |
663 | return TARGET_SIGNAL_TRAP; /* call trace */ | |
664 | case 0x13: | |
665 | return TARGET_SIGNAL_TRAP; /* return trace */ | |
666 | case 0x14: | |
667 | return TARGET_SIGNAL_TRAP; /* pre-return trace */ | |
668 | case 0x15: | |
669 | return TARGET_SIGNAL_TRAP; /* supervisor call trace */ | |
670 | case 0x16: | |
671 | return TARGET_SIGNAL_TRAP; /* breakpoint trace */ | |
672 | default: | |
673 | return TARGET_SIGNAL_UNKNOWN; | |
c906108c SS |
674 | } |
675 | } | |
676 | ||
677 | /****************************************/ | |
c5aa993b | 678 | /* MEM format */ |
c906108c SS |
679 | /****************************************/ |
680 | ||
c5aa993b JM |
681 | struct tabent |
682 | { | |
683 | char *name; | |
684 | char numops; | |
c906108c SS |
685 | }; |
686 | ||
5c678a12 KB |
687 | /* Return instruction length, either 4 or 8. When NOPRINT is non-zero |
688 | (TRUE), don't output any text. (Actually, as implemented, if NOPRINT | |
689 | is 0, abort() is called.) */ | |
690 | ||
691 | static int | |
692 | mem (unsigned long memaddr, unsigned long word1, unsigned long word2, | |
693 | int noprint) | |
c906108c | 694 | { |
c5aa993b JM |
695 | int i, j; |
696 | int len; | |
697 | int mode; | |
698 | int offset; | |
699 | const char *reg1, *reg2, *reg3; | |
700 | ||
701 | /* This lookup table is too sparse to make it worth typing in, but not | |
702 | * so large as to make a sparse array necessary. We allocate the | |
703 | * table at runtime, initialize all entries to empty, and copy the | |
704 | * real ones in from an initialization table. | |
705 | * | |
706 | * NOTE: In this table, the meaning of 'numops' is: | |
707 | * 1: single operand | |
708 | * 2: 2 operands, load instruction | |
709 | * -2: 2 operands, store instruction | |
710 | */ | |
711 | static struct tabent *mem_tab = NULL; | |
c906108c SS |
712 | /* Opcodes of 0x8X, 9X, aX, bX, and cX must be in the table. */ |
713 | #define MEM_MIN 0x80 | |
714 | #define MEM_MAX 0xcf | |
715 | #define MEM_SIZ ((MEM_MAX-MEM_MIN+1) * sizeof(struct tabent)) | |
716 | ||
c5aa993b JM |
717 | static struct |
718 | { | |
719 | int opcode; | |
720 | char *name; | |
721 | char numops; | |
722 | } | |
723 | mem_init[] = | |
724 | { | |
725 | 0x80, "ldob", 2, | |
726 | 0x82, "stob", -2, | |
727 | 0x84, "bx", 1, | |
728 | 0x85, "balx", 2, | |
729 | 0x86, "callx", 1, | |
730 | 0x88, "ldos", 2, | |
731 | 0x8a, "stos", -2, | |
732 | 0x8c, "lda", 2, | |
733 | 0x90, "ld", 2, | |
734 | 0x92, "st", -2, | |
735 | 0x98, "ldl", 2, | |
736 | 0x9a, "stl", -2, | |
737 | 0xa0, "ldt", 2, | |
738 | 0xa2, "stt", -2, | |
739 | 0xb0, "ldq", 2, | |
740 | 0xb2, "stq", -2, | |
741 | 0xc0, "ldib", 2, | |
742 | 0xc2, "stib", -2, | |
743 | 0xc8, "ldis", 2, | |
744 | 0xca, "stis", -2, | |
745 | 0, NULL, 0 | |
746 | }; | |
747 | ||
748 | if (mem_tab == NULL) | |
749 | { | |
750 | mem_tab = (struct tabent *) xmalloc (MEM_SIZ); | |
751 | memset (mem_tab, '\0', MEM_SIZ); | |
752 | for (i = 0; mem_init[i].opcode != 0; i++) | |
753 | { | |
754 | j = mem_init[i].opcode - MEM_MIN; | |
755 | mem_tab[j].name = mem_init[i].name; | |
756 | mem_tab[j].numops = mem_init[i].numops; | |
c906108c | 757 | } |
c5aa993b | 758 | } |
c906108c | 759 | |
c5aa993b JM |
760 | i = ((word1 >> 24) & 0xff) - MEM_MIN; |
761 | mode = (word1 >> 10) & 0xf; | |
c906108c | 762 | |
c5aa993b JM |
763 | if ((mem_tab[i].name != NULL) /* Valid instruction */ |
764 | && ((mode == 5) || (mode >= 12))) | |
765 | { /* With 32-bit displacement */ | |
766 | len = 8; | |
767 | } | |
768 | else | |
769 | { | |
770 | len = 4; | |
771 | } | |
c906108c | 772 | |
c5aa993b JM |
773 | if (noprint) |
774 | { | |
775 | return len; | |
776 | } | |
777 | abort (); | |
c906108c SS |
778 | } |
779 | ||
780 | /* Read the i960 instruction at 'memaddr' and return the address of | |
781 | the next instruction after that, or 0 if 'memaddr' is not the | |
782 | address of a valid instruction. The first word of the instruction | |
783 | is stored at 'pword1', and the second word, if any, is stored at | |
784 | 'pword2'. */ | |
785 | ||
786 | static CORE_ADDR | |
fba45db2 | 787 | next_insn (CORE_ADDR memaddr, unsigned int *pword1, unsigned int *pword2) |
c906108c SS |
788 | { |
789 | int len; | |
790 | char buf[8]; | |
791 | ||
792 | /* Read the two (potential) words of the instruction at once, | |
793 | to eliminate the overhead of two calls to read_memory (). | |
794 | FIXME: Loses if the first one is readable but the second is not | |
795 | (e.g. last word of the segment). */ | |
796 | ||
797 | read_memory (memaddr, buf, 8); | |
798 | *pword1 = extract_unsigned_integer (buf, 4); | |
799 | *pword2 = extract_unsigned_integer (buf + 4, 4); | |
800 | ||
c5aa993b | 801 | /* Divide instruction set into classes based on high 4 bits of opcode */ |
c906108c SS |
802 | |
803 | switch ((*pword1 >> 28) & 0xf) | |
804 | { | |
805 | case 0x0: | |
c5aa993b | 806 | case 0x1: /* ctrl */ |
c906108c SS |
807 | |
808 | case 0x2: | |
c5aa993b | 809 | case 0x3: /* cobr */ |
c906108c SS |
810 | |
811 | case 0x5: | |
812 | case 0x6: | |
c5aa993b | 813 | case 0x7: /* reg */ |
c906108c SS |
814 | len = 4; |
815 | break; | |
816 | ||
817 | case 0x8: | |
818 | case 0x9: | |
819 | case 0xa: | |
820 | case 0xb: | |
821 | case 0xc: | |
822 | len = mem (memaddr, *pword1, *pword2, 1); | |
823 | break; | |
824 | ||
c5aa993b | 825 | default: /* invalid instruction */ |
c906108c SS |
826 | len = 0; |
827 | break; | |
828 | } | |
829 | ||
830 | if (len) | |
831 | return memaddr + len; | |
832 | else | |
833 | return 0; | |
834 | } | |
835 | ||
836 | /* 'start_frame' is a variable in the MON960 runtime startup routine | |
837 | that contains the frame pointer of the 'start' routine (the routine | |
838 | that calls 'main'). By reading its contents out of remote memory, | |
839 | we can tell where the frame chain ends: backtraces should halt before | |
840 | they display this frame. */ | |
841 | ||
842 | int | |
fba45db2 | 843 | mon960_frame_chain_valid (CORE_ADDR chain, struct frame_info *curframe) |
c906108c | 844 | { |
c5aa993b JM |
845 | struct symbol *sym; |
846 | struct minimal_symbol *msymbol; | |
847 | ||
848 | /* crtmon960.o is an assembler module that is assumed to be linked | |
849 | * first in an i80960 executable. It contains the true entry point; | |
850 | * it performs startup up initialization and then calls 'main'. | |
851 | * | |
852 | * 'sf' is the name of a variable in crtmon960.o that is set | |
853 | * during startup to the address of the first frame. | |
854 | * | |
855 | * 'a' is the address of that variable in 80960 memory. | |
856 | */ | |
857 | static char sf[] = "start_frame"; | |
858 | CORE_ADDR a; | |
859 | ||
860 | ||
861 | chain &= ~0x3f; /* Zero low 6 bits because previous frame pointers | |
862 | contain return status info in them. */ | |
863 | if (chain == 0) | |
864 | { | |
865 | return 0; | |
866 | } | |
c906108c | 867 | |
c5aa993b JM |
868 | sym = lookup_symbol (sf, 0, VAR_NAMESPACE, (int *) NULL, |
869 | (struct symtab **) NULL); | |
870 | if (sym != 0) | |
871 | { | |
872 | a = SYMBOL_VALUE (sym); | |
873 | } | |
874 | else | |
875 | { | |
876 | msymbol = lookup_minimal_symbol (sf, NULL, NULL); | |
877 | if (msymbol == NULL) | |
878 | return 0; | |
879 | a = SYMBOL_VALUE_ADDRESS (msymbol); | |
880 | } | |
c906108c | 881 | |
c5aa993b | 882 | return (chain != read_memory_integer (a, 4)); |
c906108c SS |
883 | } |
884 | ||
2acceee2 | 885 | |
c906108c | 886 | void |
fba45db2 | 887 | _initialize_i960_tdep (void) |
c906108c SS |
888 | { |
889 | check_host (); | |
890 | ||
891 | tm_print_insn = print_insn_i960; | |
892 | } |