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