Commit | Line | Data |
---|---|---|
20be272b CV |
1 | /* Target-dependent code for the IQ2000 architecture, for GDB, the GNU |
2 | Debugger. | |
3 | ||
0fb0cc75 JB |
4 | Copyright (C) 2000, 2004, 2005, 2007, 2008, 2009 |
5 | Free Software Foundation, Inc. | |
20be272b CV |
6 | |
7 | Contributed by Red Hat. | |
8 | ||
9 | This file is part of GDB. | |
10 | ||
11 | This program is free software; you can redistribute it and/or modify | |
12 | it under the terms of the GNU General Public License as published by | |
a9762ec7 | 13 | the Free Software Foundation; either version 3 of the License, or |
20be272b CV |
14 | (at your option) any later version. |
15 | ||
16 | This program is distributed in the hope that it will be useful, | |
17 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
18 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
19 | GNU General Public License for more details. | |
20 | ||
21 | You should have received a copy of the GNU General Public License | |
a9762ec7 | 22 | along with this program. If not, see <http://www.gnu.org/licenses/>. */ |
20be272b CV |
23 | |
24 | #include "defs.h" | |
25 | #include "frame.h" | |
26 | #include "frame-base.h" | |
27 | #include "frame-unwind.h" | |
28 | #include "dwarf2-frame.h" | |
29 | #include "gdbtypes.h" | |
30 | #include "value.h" | |
31 | #include "dis-asm.h" | |
32 | #include "gdb_string.h" | |
33 | #include "arch-utils.h" | |
34 | #include "regcache.h" | |
35 | #include "osabi.h" | |
36 | #include "gdbcore.h" | |
37 | ||
38 | enum gdb_regnum | |
39 | { | |
40 | E_R0_REGNUM, E_R1_REGNUM, E_R2_REGNUM, E_R3_REGNUM, | |
41 | E_R4_REGNUM, E_R5_REGNUM, E_R6_REGNUM, E_R7_REGNUM, | |
42 | E_R8_REGNUM, E_R9_REGNUM, E_R10_REGNUM, E_R11_REGNUM, | |
43 | E_R12_REGNUM, E_R13_REGNUM, E_R14_REGNUM, E_R15_REGNUM, | |
44 | E_R16_REGNUM, E_R17_REGNUM, E_R18_REGNUM, E_R19_REGNUM, | |
45 | E_R20_REGNUM, E_R21_REGNUM, E_R22_REGNUM, E_R23_REGNUM, | |
46 | E_R24_REGNUM, E_R25_REGNUM, E_R26_REGNUM, E_R27_REGNUM, | |
47 | E_R28_REGNUM, E_R29_REGNUM, E_R30_REGNUM, E_R31_REGNUM, | |
48 | E_PC_REGNUM, | |
49 | E_LR_REGNUM = E_R31_REGNUM, /* Link register. */ | |
50 | E_SP_REGNUM = E_R29_REGNUM, /* Stack pointer. */ | |
51 | E_FP_REGNUM = E_R27_REGNUM, /* Frame pointer. */ | |
52 | E_FN_RETURN_REGNUM = E_R2_REGNUM, /* Function return value register. */ | |
53 | E_1ST_ARGREG = E_R4_REGNUM, /* 1st function arg register. */ | |
54 | E_LAST_ARGREG = E_R11_REGNUM, /* Last function arg register. */ | |
55 | E_NUM_REGS = E_PC_REGNUM + 1 | |
56 | }; | |
57 | ||
58 | /* Use an invalid address value as 'not available' marker. */ | |
59 | enum { REG_UNAVAIL = (CORE_ADDR) -1 }; | |
60 | ||
61 | struct iq2000_frame_cache | |
62 | { | |
63 | /* Base address. */ | |
64 | CORE_ADDR base; | |
65 | CORE_ADDR pc; | |
66 | LONGEST framesize; | |
67 | int using_fp; | |
68 | CORE_ADDR saved_sp; | |
69 | CORE_ADDR saved_regs [E_NUM_REGS]; | |
70 | }; | |
71 | ||
72 | /* Harvard methods: */ | |
73 | ||
74 | static CORE_ADDR | |
75 | insn_ptr_from_addr (CORE_ADDR addr) /* CORE_ADDR to target pointer. */ | |
76 | { | |
77 | return addr & 0x7fffffffL; | |
78 | } | |
79 | ||
80 | static CORE_ADDR | |
81 | insn_addr_from_ptr (CORE_ADDR ptr) /* target_pointer to CORE_ADDR. */ | |
82 | { | |
83 | return (ptr & 0x7fffffffL) | 0x80000000L; | |
84 | } | |
85 | ||
86 | /* Function: pointer_to_address | |
87 | Convert a target pointer to an address in host (CORE_ADDR) format. */ | |
88 | ||
89 | static CORE_ADDR | |
9898f801 UW |
90 | iq2000_pointer_to_address (struct gdbarch *gdbarch, |
91 | struct type * type, const gdb_byte * buf) | |
20be272b | 92 | { |
e17a4113 | 93 | enum bfd_endian byte_order = gdbarch_byte_order (gdbarch); |
20be272b | 94 | enum type_code target = TYPE_CODE (TYPE_TARGET_TYPE (type)); |
e17a4113 UW |
95 | CORE_ADDR addr |
96 | = extract_unsigned_integer (buf, TYPE_LENGTH (type), byte_order); | |
20be272b CV |
97 | |
98 | if (target == TYPE_CODE_FUNC | |
99 | || target == TYPE_CODE_METHOD | |
876cecd0 | 100 | || TYPE_CODE_SPACE (TYPE_TARGET_TYPE (type))) |
20be272b CV |
101 | addr = insn_addr_from_ptr (addr); |
102 | ||
103 | return addr; | |
104 | } | |
105 | ||
106 | /* Function: address_to_pointer | |
107 | Convert a host-format address (CORE_ADDR) into a target pointer. */ | |
108 | ||
109 | static void | |
9898f801 UW |
110 | iq2000_address_to_pointer (struct gdbarch *gdbarch, |
111 | struct type *type, gdb_byte *buf, CORE_ADDR addr) | |
20be272b | 112 | { |
e17a4113 | 113 | enum bfd_endian byte_order = gdbarch_byte_order (gdbarch); |
20be272b CV |
114 | enum type_code target = TYPE_CODE (TYPE_TARGET_TYPE (type)); |
115 | ||
116 | if (target == TYPE_CODE_FUNC || target == TYPE_CODE_METHOD) | |
117 | addr = insn_ptr_from_addr (addr); | |
e17a4113 | 118 | store_unsigned_integer (buf, TYPE_LENGTH (type), byte_order, addr); |
20be272b CV |
119 | } |
120 | ||
121 | /* Real register methods: */ | |
122 | ||
123 | /* Function: register_name | |
124 | Returns the name of the iq2000 register number N. */ | |
125 | ||
126 | static const char * | |
d93859e2 | 127 | iq2000_register_name (struct gdbarch *gdbarch, int regnum) |
20be272b CV |
128 | { |
129 | static const char * names[E_NUM_REGS] = | |
130 | { | |
131 | "r0", "r1", "r2", "r3", "r4", | |
132 | "r5", "r6", "r7", "r8", "r9", | |
133 | "r10", "r11", "r12", "r13", "r14", | |
134 | "r15", "r16", "r17", "r18", "r19", | |
135 | "r20", "r21", "r22", "r23", "r24", | |
136 | "r25", "r26", "r27", "r28", "r29", | |
137 | "r30", "r31", | |
138 | "pc" | |
139 | }; | |
140 | if (regnum < 0 || regnum >= E_NUM_REGS) | |
141 | return NULL; | |
142 | return names[regnum]; | |
143 | } | |
144 | ||
145 | /* Prologue analysis methods: */ | |
146 | ||
147 | /* ADDIU insn (001001 rs(5) rt(5) imm(16)). */ | |
148 | #define INSN_IS_ADDIU(X) (((X) & 0xfc000000) == 0x24000000) | |
149 | #define ADDIU_REG_SRC(X) (((X) & 0x03e00000) >> 21) | |
150 | #define ADDIU_REG_TGT(X) (((X) & 0x001f0000) >> 16) | |
151 | #define ADDIU_IMMEDIATE(X) ((signed short) ((X) & 0x0000ffff)) | |
152 | ||
153 | /* "MOVE" (OR) insn (000000 rs(5) rt(5) rd(5) 00000 100101). */ | |
154 | #define INSN_IS_MOVE(X) (((X) & 0xffe007ff) == 0x00000025) | |
155 | #define MOVE_REG_SRC(X) (((X) & 0x001f0000) >> 16) | |
156 | #define MOVE_REG_TGT(X) (((X) & 0x0000f800) >> 11) | |
157 | ||
158 | /* STORE WORD insn (101011 rs(5) rt(5) offset(16)). */ | |
159 | #define INSN_IS_STORE_WORD(X) (((X) & 0xfc000000) == 0xac000000) | |
160 | #define SW_REG_INDEX(X) (((X) & 0x03e00000) >> 21) | |
161 | #define SW_REG_SRC(X) (((X) & 0x001f0000) >> 16) | |
162 | #define SW_OFFSET(X) ((signed short) ((X) & 0x0000ffff)) | |
163 | ||
164 | /* Function: find_last_line_symbol | |
165 | ||
166 | Given an address range, first find a line symbol corresponding to | |
167 | the starting address. Then find the last line symbol within the | |
168 | range that has a line number less than or equal to the first line. | |
169 | ||
170 | For optimized code with code motion, this finds the last address | |
171 | for the lowest-numbered line within the address range. */ | |
172 | ||
173 | static struct symtab_and_line | |
174 | find_last_line_symbol (CORE_ADDR start, CORE_ADDR end, int notcurrent) | |
175 | { | |
176 | struct symtab_and_line sal = find_pc_line (start, notcurrent); | |
177 | struct symtab_and_line best_sal = sal; | |
178 | ||
179 | if (sal.pc == 0 || sal.line == 0 || sal.end == 0) | |
180 | return sal; | |
181 | ||
182 | do | |
183 | { | |
184 | if (sal.line && sal.line <= best_sal.line) | |
185 | best_sal = sal; | |
186 | sal = find_pc_line (sal.end, notcurrent); | |
187 | } | |
188 | while (sal.pc && sal.pc < end); | |
189 | ||
190 | return best_sal; | |
191 | } | |
192 | ||
193 | /* Function: scan_prologue | |
194 | Decode the instructions within the given address range. | |
195 | Decide when we must have reached the end of the function prologue. | |
196 | If a frame_info pointer is provided, fill in its prologue information. | |
197 | ||
198 | Returns the address of the first instruction after the prologue. */ | |
199 | ||
200 | static CORE_ADDR | |
e17a4113 UW |
201 | iq2000_scan_prologue (struct gdbarch *gdbarch, |
202 | CORE_ADDR scan_start, | |
20be272b CV |
203 | CORE_ADDR scan_end, |
204 | struct frame_info *fi, | |
205 | struct iq2000_frame_cache *cache) | |
206 | { | |
e17a4113 | 207 | enum bfd_endian byte_order = gdbarch_byte_order (gdbarch); |
20be272b CV |
208 | struct symtab_and_line sal; |
209 | CORE_ADDR pc; | |
210 | CORE_ADDR loop_end; | |
211 | int found_store_lr = 0; | |
212 | int found_decr_sp = 0; | |
213 | int srcreg; | |
214 | int tgtreg; | |
215 | signed short offset; | |
216 | ||
217 | if (scan_end == (CORE_ADDR) 0) | |
218 | { | |
219 | loop_end = scan_start + 100; | |
220 | sal.end = sal.pc = 0; | |
221 | } | |
222 | else | |
223 | { | |
224 | loop_end = scan_end; | |
225 | if (fi) | |
226 | sal = find_last_line_symbol (scan_start, scan_end, 0); | |
227 | } | |
228 | ||
229 | /* Saved registers: | |
230 | We first have to save the saved register's offset, and | |
231 | only later do we compute its actual address. Since the | |
232 | offset can be zero, we must first initialize all the | |
233 | saved regs to minus one (so we can later distinguish | |
234 | between one that's not saved, and one that's saved at zero). */ | |
235 | for (srcreg = 0; srcreg < E_NUM_REGS; srcreg ++) | |
236 | cache->saved_regs[srcreg] = -1; | |
237 | cache->using_fp = 0; | |
238 | cache->framesize = 0; | |
239 | ||
240 | for (pc = scan_start; pc < loop_end; pc += 4) | |
241 | { | |
e17a4113 | 242 | LONGEST insn = read_memory_unsigned_integer (pc, 4, byte_order); |
20be272b CV |
243 | /* Skip any instructions writing to (sp) or decrementing the |
244 | SP. */ | |
245 | if ((insn & 0xffe00000) == 0xac200000) | |
246 | { | |
247 | /* sw using SP/%1 as base. */ | |
248 | /* LEGACY -- from assembly-only port. */ | |
249 | tgtreg = ((insn >> 16) & 0x1f); | |
250 | if (tgtreg >= 0 && tgtreg < E_NUM_REGS) | |
251 | cache->saved_regs[tgtreg] = -((signed short) (insn & 0xffff)); | |
252 | ||
253 | if (tgtreg == E_LR_REGNUM) | |
254 | found_store_lr = 1; | |
255 | continue; | |
256 | } | |
257 | ||
258 | if ((insn & 0xffff8000) == 0x20218000) | |
259 | { | |
260 | /* addi %1, %1, -N == addi %sp, %sp, -N */ | |
261 | /* LEGACY -- from assembly-only port */ | |
262 | found_decr_sp = 1; | |
263 | cache->framesize = -((signed short) (insn & 0xffff)); | |
264 | continue; | |
265 | } | |
266 | ||
267 | if (INSN_IS_ADDIU (insn)) | |
268 | { | |
269 | srcreg = ADDIU_REG_SRC (insn); | |
270 | tgtreg = ADDIU_REG_TGT (insn); | |
271 | offset = ADDIU_IMMEDIATE (insn); | |
272 | if (srcreg == E_SP_REGNUM && tgtreg == E_SP_REGNUM) | |
273 | cache->framesize = -offset; | |
274 | continue; | |
275 | } | |
276 | ||
277 | if (INSN_IS_STORE_WORD (insn)) | |
278 | { | |
279 | srcreg = SW_REG_SRC (insn); | |
280 | tgtreg = SW_REG_INDEX (insn); | |
281 | offset = SW_OFFSET (insn); | |
282 | ||
283 | if (tgtreg == E_SP_REGNUM || tgtreg == E_FP_REGNUM) | |
284 | { | |
285 | /* "push" to stack (via SP or FP reg) */ | |
286 | if (cache->saved_regs[srcreg] == -1) /* Don't save twice. */ | |
287 | cache->saved_regs[srcreg] = offset; | |
288 | continue; | |
289 | } | |
290 | } | |
291 | ||
292 | if (INSN_IS_MOVE (insn)) | |
293 | { | |
294 | srcreg = MOVE_REG_SRC (insn); | |
295 | tgtreg = MOVE_REG_TGT (insn); | |
296 | ||
297 | if (srcreg == E_SP_REGNUM && tgtreg == E_FP_REGNUM) | |
298 | { | |
299 | /* Copy sp to fp. */ | |
300 | cache->using_fp = 1; | |
301 | continue; | |
302 | } | |
303 | } | |
304 | ||
305 | /* Unknown instruction encountered in frame. Bail out? | |
306 | 1) If we have a subsequent line symbol, we can keep going. | |
307 | 2) If not, we need to bail out and quit scanning instructions. */ | |
308 | ||
309 | if (fi && sal.end && (pc < sal.end)) /* Keep scanning. */ | |
310 | continue; | |
311 | else /* bail */ | |
312 | break; | |
313 | } | |
314 | ||
315 | return pc; | |
316 | } | |
317 | ||
318 | static void | |
319 | iq2000_init_frame_cache (struct iq2000_frame_cache *cache) | |
320 | { | |
321 | int i; | |
322 | ||
323 | cache->base = 0; | |
324 | cache->framesize = 0; | |
325 | cache->using_fp = 0; | |
326 | cache->saved_sp = 0; | |
327 | for (i = 0; i < E_NUM_REGS; i++) | |
328 | cache->saved_regs[i] = -1; | |
329 | } | |
330 | ||
331 | /* Function: iq2000_skip_prologue | |
332 | If the input address is in a function prologue, | |
333 | returns the address of the end of the prologue; | |
334 | else returns the input address. | |
335 | ||
336 | Note: the input address is likely to be the function start, | |
337 | since this function is mainly used for advancing a breakpoint | |
338 | to the first line, or stepping to the first line when we have | |
339 | stepped into a function call. */ | |
340 | ||
341 | static CORE_ADDR | |
6093d2eb | 342 | iq2000_skip_prologue (struct gdbarch *gdbarch, CORE_ADDR pc) |
20be272b CV |
343 | { |
344 | CORE_ADDR func_addr = 0 , func_end = 0; | |
345 | ||
346 | if (find_pc_partial_function (pc, NULL, & func_addr, & func_end)) | |
347 | { | |
348 | struct symtab_and_line sal; | |
349 | struct iq2000_frame_cache cache; | |
350 | ||
351 | /* Found a function. */ | |
352 | sal = find_pc_line (func_addr, 0); | |
353 | if (sal.end && sal.end < func_end) | |
354 | /* Found a line number, use it as end of prologue. */ | |
355 | return sal.end; | |
356 | ||
357 | /* No useable line symbol. Use prologue parsing method. */ | |
358 | iq2000_init_frame_cache (&cache); | |
e17a4113 | 359 | return iq2000_scan_prologue (gdbarch, func_addr, func_end, NULL, &cache); |
20be272b CV |
360 | } |
361 | ||
362 | /* No function symbol -- just return the PC. */ | |
363 | return (CORE_ADDR) pc; | |
364 | } | |
365 | ||
366 | static struct iq2000_frame_cache * | |
94afd7a6 | 367 | iq2000_frame_cache (struct frame_info *this_frame, void **this_cache) |
20be272b | 368 | { |
e17a4113 | 369 | struct gdbarch *gdbarch = get_frame_arch (this_frame); |
20be272b CV |
370 | struct iq2000_frame_cache *cache; |
371 | CORE_ADDR current_pc; | |
372 | int i; | |
373 | ||
374 | if (*this_cache) | |
375 | return *this_cache; | |
376 | ||
377 | cache = FRAME_OBSTACK_ZALLOC (struct iq2000_frame_cache); | |
378 | iq2000_init_frame_cache (cache); | |
379 | *this_cache = cache; | |
380 | ||
94afd7a6 | 381 | cache->base = get_frame_register_unsigned (this_frame, E_FP_REGNUM); |
20be272b CV |
382 | //if (cache->base == 0) |
383 | //return cache; | |
384 | ||
94afd7a6 | 385 | current_pc = get_frame_pc (this_frame); |
20be272b CV |
386 | find_pc_partial_function (current_pc, NULL, &cache->pc, NULL); |
387 | if (cache->pc != 0) | |
e17a4113 | 388 | iq2000_scan_prologue (gdbarch, cache->pc, current_pc, this_frame, cache); |
20be272b | 389 | if (!cache->using_fp) |
94afd7a6 | 390 | cache->base = get_frame_register_unsigned (this_frame, E_SP_REGNUM); |
20be272b CV |
391 | |
392 | cache->saved_sp = cache->base + cache->framesize; | |
393 | ||
394 | for (i = 0; i < E_NUM_REGS; i++) | |
395 | if (cache->saved_regs[i] != -1) | |
396 | cache->saved_regs[i] += cache->base; | |
397 | ||
398 | return cache; | |
399 | } | |
400 | ||
94afd7a6 UW |
401 | static struct value * |
402 | iq2000_frame_prev_register (struct frame_info *this_frame, void **this_cache, | |
403 | int regnum) | |
20be272b | 404 | { |
94afd7a6 UW |
405 | struct iq2000_frame_cache *cache = iq2000_frame_cache (this_frame, this_cache); |
406 | ||
20be272b | 407 | if (regnum == E_SP_REGNUM && cache->saved_sp) |
94afd7a6 | 408 | return frame_unwind_got_constant (this_frame, regnum, cache->saved_sp); |
20be272b CV |
409 | |
410 | if (regnum == E_PC_REGNUM) | |
411 | regnum = E_LR_REGNUM; | |
412 | ||
413 | if (regnum < E_NUM_REGS && cache->saved_regs[regnum] != -1) | |
94afd7a6 UW |
414 | return frame_unwind_got_memory (this_frame, regnum, |
415 | cache->saved_regs[regnum]); | |
20be272b | 416 | |
94afd7a6 | 417 | return frame_unwind_got_register (this_frame, regnum, regnum); |
20be272b CV |
418 | } |
419 | ||
420 | static void | |
94afd7a6 | 421 | iq2000_frame_this_id (struct frame_info *this_frame, void **this_cache, |
20be272b CV |
422 | struct frame_id *this_id) |
423 | { | |
94afd7a6 | 424 | struct iq2000_frame_cache *cache = iq2000_frame_cache (this_frame, this_cache); |
20be272b CV |
425 | |
426 | /* This marks the outermost frame. */ | |
427 | if (cache->base == 0) | |
428 | return; | |
429 | ||
430 | *this_id = frame_id_build (cache->saved_sp, cache->pc); | |
431 | } | |
432 | ||
433 | static const struct frame_unwind iq2000_frame_unwind = { | |
434 | NORMAL_FRAME, | |
435 | iq2000_frame_this_id, | |
94afd7a6 UW |
436 | iq2000_frame_prev_register, |
437 | NULL, | |
438 | default_frame_sniffer | |
20be272b CV |
439 | }; |
440 | ||
20be272b CV |
441 | static CORE_ADDR |
442 | iq2000_unwind_sp (struct gdbarch *gdbarch, struct frame_info *next_frame) | |
443 | { | |
444 | return frame_unwind_register_unsigned (next_frame, E_SP_REGNUM); | |
445 | } | |
446 | ||
447 | static CORE_ADDR | |
448 | iq2000_unwind_pc (struct gdbarch *gdbarch, struct frame_info *next_frame) | |
449 | { | |
450 | return frame_unwind_register_unsigned (next_frame, E_PC_REGNUM); | |
451 | } | |
452 | ||
453 | static struct frame_id | |
94afd7a6 | 454 | iq2000_dummy_id (struct gdbarch *gdbarch, struct frame_info *this_frame) |
20be272b | 455 | { |
94afd7a6 UW |
456 | CORE_ADDR sp = get_frame_register_unsigned (this_frame, E_SP_REGNUM); |
457 | return frame_id_build (sp, get_frame_pc (this_frame)); | |
20be272b CV |
458 | } |
459 | ||
460 | static CORE_ADDR | |
94afd7a6 | 461 | iq2000_frame_base_address (struct frame_info *this_frame, void **this_cache) |
20be272b | 462 | { |
94afd7a6 | 463 | struct iq2000_frame_cache *cache = iq2000_frame_cache (this_frame, this_cache); |
20be272b CV |
464 | |
465 | return cache->base; | |
466 | } | |
467 | ||
468 | static const struct frame_base iq2000_frame_base = { | |
469 | &iq2000_frame_unwind, | |
470 | iq2000_frame_base_address, | |
471 | iq2000_frame_base_address, | |
472 | iq2000_frame_base_address | |
473 | }; | |
474 | ||
475 | static const unsigned char * | |
67d57894 MD |
476 | iq2000_breakpoint_from_pc (struct gdbarch *gdbarch, CORE_ADDR *pcptr, |
477 | int *lenptr) | |
20be272b CV |
478 | { |
479 | static const unsigned char big_breakpoint[] = { 0x00, 0x00, 0x00, 0x0d }; | |
480 | static const unsigned char little_breakpoint[] = { 0x0d, 0x00, 0x00, 0x00 }; | |
481 | ||
482 | if ((*pcptr & 3) != 0) | |
483 | error ("breakpoint_from_pc: invalid breakpoint address 0x%lx", | |
484 | (long) *pcptr); | |
485 | ||
486 | *lenptr = 4; | |
67d57894 MD |
487 | return (gdbarch_byte_order (gdbarch) |
488 | == BFD_ENDIAN_BIG) ? big_breakpoint : little_breakpoint; | |
20be272b CV |
489 | } |
490 | ||
491 | /* Target function return value methods: */ | |
492 | ||
493 | /* Function: store_return_value | |
494 | Copy the function return value from VALBUF into the | |
495 | proper location for a function return. */ | |
496 | ||
497 | static void | |
498 | iq2000_store_return_value (struct type *type, struct regcache *regcache, | |
499 | const void *valbuf) | |
500 | { | |
501 | int len = TYPE_LENGTH (type); | |
502 | int regno = E_FN_RETURN_REGNUM; | |
503 | ||
504 | while (len > 0) | |
505 | { | |
506 | char buf[4]; | |
507 | int size = len % 4 ?: 4; | |
508 | ||
509 | memset (buf, 0, 4); | |
510 | memcpy (buf + 4 - size, valbuf, size); | |
511 | regcache_raw_write (regcache, regno++, buf); | |
512 | len -= size; | |
513 | valbuf = ((char *) valbuf) + size; | |
514 | } | |
515 | } | |
516 | ||
517 | /* Function: use_struct_convention | |
518 | Returns non-zero if the given struct type will be returned using | |
519 | a special convention, rather than the normal function return method. */ | |
520 | ||
521 | static int | |
522 | iq2000_use_struct_convention (struct type *type) | |
523 | { | |
524 | return ((TYPE_CODE (type) == TYPE_CODE_STRUCT) | |
525 | || (TYPE_CODE (type) == TYPE_CODE_UNION)) | |
526 | && TYPE_LENGTH (type) > 8; | |
527 | } | |
528 | ||
529 | /* Function: extract_return_value | |
530 | Copy the function's return value into VALBUF. | |
531 | This function is called only in the context of "target function calls", | |
532 | ie. when the debugger forces a function to be called in the child, and | |
533 | when the debugger forces a function to return prematurely via the | |
534 | "return" command. */ | |
535 | ||
536 | static void | |
537 | iq2000_extract_return_value (struct type *type, struct regcache *regcache, | |
538 | void *valbuf) | |
539 | { | |
e17a4113 UW |
540 | struct gdbarch *gdbarch = get_regcache_arch (regcache); |
541 | enum bfd_endian byte_order = gdbarch_byte_order (gdbarch); | |
542 | ||
20be272b CV |
543 | /* If the function's return value is 8 bytes or less, it is |
544 | returned in a register, and if larger than 8 bytes, it is | |
545 | returned in a stack location which is pointed to by the same | |
546 | register. */ | |
20be272b CV |
547 | int len = TYPE_LENGTH (type); |
548 | ||
549 | if (len <= (2 * 4)) | |
550 | { | |
551 | int regno = E_FN_RETURN_REGNUM; | |
552 | ||
553 | /* Return values of <= 8 bytes are returned in | |
554 | FN_RETURN_REGNUM. */ | |
555 | while (len > 0) | |
556 | { | |
557 | ULONGEST tmp; | |
558 | int size = len % 4 ?: 4; | |
559 | ||
560 | /* By using store_unsigned_integer we avoid having to | |
561 | do anything special for small big-endian values. */ | |
562 | regcache_cooked_read_unsigned (regcache, regno++, &tmp); | |
e17a4113 | 563 | store_unsigned_integer (valbuf, size, byte_order, tmp); |
20be272b CV |
564 | len -= size; |
565 | valbuf = ((char *) valbuf) + size; | |
566 | } | |
567 | } | |
568 | else | |
569 | { | |
570 | /* Return values > 8 bytes are returned in memory, | |
571 | pointed to by FN_RETURN_REGNUM. */ | |
ec20a626 UW |
572 | ULONGEST return_buffer; |
573 | regcache_cooked_read_unsigned (regcache, E_FN_RETURN_REGNUM, | |
574 | &return_buffer); | |
20be272b CV |
575 | read_memory (return_buffer, valbuf, TYPE_LENGTH (type)); |
576 | } | |
577 | } | |
578 | ||
579 | static enum return_value_convention | |
c055b101 CV |
580 | iq2000_return_value (struct gdbarch *gdbarch, struct type *func_type, |
581 | struct type *type, struct regcache *regcache, | |
ec20a626 | 582 | gdb_byte *readbuf, const gdb_byte *writebuf) |
20be272b CV |
583 | { |
584 | if (iq2000_use_struct_convention (type)) | |
585 | return RETURN_VALUE_STRUCT_CONVENTION; | |
586 | if (writebuf) | |
587 | iq2000_store_return_value (type, regcache, writebuf); | |
588 | else if (readbuf) | |
589 | iq2000_extract_return_value (type, regcache, readbuf); | |
590 | return RETURN_VALUE_REGISTER_CONVENTION; | |
591 | } | |
592 | ||
593 | /* Function: register_virtual_type | |
594 | Returns the default type for register N. */ | |
595 | ||
596 | static struct type * | |
597 | iq2000_register_type (struct gdbarch *gdbarch, int regnum) | |
598 | { | |
df4df182 | 599 | return builtin_type (gdbarch)->builtin_int32; |
20be272b CV |
600 | } |
601 | ||
602 | static CORE_ADDR | |
603 | iq2000_frame_align (struct gdbarch *ignore, CORE_ADDR sp) | |
604 | { | |
605 | /* This is the same frame alignment used by gcc. */ | |
606 | return ((sp + 7) & ~7); | |
607 | } | |
608 | ||
609 | /* Convenience function to check 8-byte types for being a scalar type | |
610 | or a struct with only one long long or double member. */ | |
611 | static int | |
612 | iq2000_pass_8bytetype_by_address (struct type *type) | |
613 | { | |
614 | struct type *ftype; | |
615 | ||
616 | /* Skip typedefs. */ | |
617 | while (TYPE_CODE (type) == TYPE_CODE_TYPEDEF) | |
618 | type = TYPE_TARGET_TYPE (type); | |
619 | /* Non-struct and non-union types are always passed by value. */ | |
620 | if (TYPE_CODE (type) != TYPE_CODE_STRUCT | |
621 | && TYPE_CODE (type) != TYPE_CODE_UNION) | |
622 | return 0; | |
623 | /* Structs with more than 1 field are always passed by address. */ | |
624 | if (TYPE_NFIELDS (type) != 1) | |
625 | return 1; | |
626 | /* Get field type. */ | |
627 | ftype = (TYPE_FIELDS (type))[0].type; | |
628 | /* The field type must have size 8, otherwise pass by address. */ | |
629 | if (TYPE_LENGTH (ftype) != 8) | |
630 | return 1; | |
631 | /* Skip typedefs of field type. */ | |
632 | while (TYPE_CODE (ftype) == TYPE_CODE_TYPEDEF) | |
633 | ftype = TYPE_TARGET_TYPE (ftype); | |
634 | /* If field is int or float, pass by value. */ | |
635 | if (TYPE_CODE (ftype) == TYPE_CODE_FLT | |
636 | || TYPE_CODE (ftype) == TYPE_CODE_INT) | |
637 | return 0; | |
638 | /* Everything else, pass by address. */ | |
639 | return 1; | |
640 | } | |
641 | ||
642 | static CORE_ADDR | |
643 | iq2000_push_dummy_call (struct gdbarch *gdbarch, struct value *function, | |
644 | struct regcache *regcache, CORE_ADDR bp_addr, | |
645 | int nargs, struct value **args, CORE_ADDR sp, | |
646 | int struct_return, CORE_ADDR struct_addr) | |
647 | { | |
e17a4113 | 648 | enum bfd_endian byte_order = gdbarch_byte_order (gdbarch); |
20be272b CV |
649 | const bfd_byte *val; |
650 | bfd_byte buf[4]; | |
651 | struct type *type; | |
652 | int i, argreg, typelen, slacklen; | |
653 | int stackspace = 0; | |
654 | /* Used to copy struct arguments into the stack. */ | |
655 | CORE_ADDR struct_ptr; | |
656 | ||
657 | /* First determine how much stack space we will need. */ | |
658 | for (i = 0, argreg = E_1ST_ARGREG + (struct_return != 0); i < nargs; i++) | |
659 | { | |
660 | type = value_type (args[i]); | |
661 | typelen = TYPE_LENGTH (type); | |
662 | if (typelen <= 4) | |
663 | { | |
664 | /* Scalars of up to 4 bytes, | |
665 | structs of up to 4 bytes, and | |
666 | pointers. */ | |
667 | if (argreg <= E_LAST_ARGREG) | |
668 | argreg++; | |
669 | else | |
670 | stackspace += 4; | |
671 | } | |
672 | else if (typelen == 8 && !iq2000_pass_8bytetype_by_address (type)) | |
673 | { | |
674 | /* long long, | |
675 | double, and possibly | |
676 | structs with a single field of long long or double. */ | |
677 | if (argreg <= E_LAST_ARGREG - 1) | |
678 | { | |
679 | /* 8-byte arg goes into a register pair | |
680 | (must start with an even-numbered reg) */ | |
681 | if (((argreg - E_1ST_ARGREG) % 2) != 0) | |
682 | argreg ++; | |
683 | argreg += 2; | |
684 | } | |
685 | else | |
686 | { | |
687 | argreg = E_LAST_ARGREG + 1; /* no more argregs. */ | |
688 | /* 8-byte arg goes on stack, must be 8-byte aligned. */ | |
689 | stackspace = ((stackspace + 7) & ~7); | |
690 | stackspace += 8; | |
691 | } | |
692 | } | |
693 | else | |
694 | { | |
695 | /* Structs are passed as pointer to a copy of the struct. | |
696 | So we need room on the stack for a copy of the struct | |
697 | plus for the argument pointer. */ | |
698 | if (argreg <= E_LAST_ARGREG) | |
699 | argreg++; | |
700 | else | |
701 | stackspace += 4; | |
702 | /* Care for 8-byte alignment of structs saved on stack. */ | |
703 | stackspace += ((typelen + 7) & ~7); | |
704 | } | |
705 | } | |
706 | ||
707 | /* Now copy params, in ascending order, into their assigned location | |
708 | (either in a register or on the stack). */ | |
709 | ||
710 | sp -= (sp % 8); /* align */ | |
711 | struct_ptr = sp; | |
712 | sp -= stackspace; | |
713 | sp -= (sp % 8); /* align again */ | |
714 | stackspace = 0; | |
715 | ||
716 | argreg = E_1ST_ARGREG; | |
717 | if (struct_return) | |
718 | { | |
719 | /* A function that returns a struct will consume one argreg to do so. | |
720 | */ | |
721 | regcache_cooked_write_unsigned (regcache, argreg++, struct_addr); | |
722 | } | |
723 | ||
724 | for (i = 0; i < nargs; i++) | |
725 | { | |
726 | type = value_type (args[i]); | |
727 | typelen = TYPE_LENGTH (type); | |
728 | val = value_contents (args[i]); | |
729 | if (typelen <= 4) | |
730 | { | |
731 | /* Char, short, int, float, pointer, and structs <= four bytes. */ | |
732 | slacklen = (4 - (typelen % 4)) % 4; | |
733 | memset (buf, 0, sizeof (buf)); | |
734 | memcpy (buf + slacklen, val, typelen); | |
735 | if (argreg <= E_LAST_ARGREG) | |
736 | { | |
737 | /* Passed in a register. */ | |
738 | regcache_raw_write (regcache, argreg++, buf); | |
739 | } | |
740 | else | |
741 | { | |
742 | /* Passed on the stack. */ | |
743 | write_memory (sp + stackspace, buf, 4); | |
744 | stackspace += 4; | |
745 | } | |
746 | } | |
747 | else if (typelen == 8 && !iq2000_pass_8bytetype_by_address (type)) | |
748 | { | |
749 | /* (long long), (double), or struct consisting of | |
750 | a single (long long) or (double). */ | |
751 | if (argreg <= E_LAST_ARGREG - 1) | |
752 | { | |
753 | /* 8-byte arg goes into a register pair | |
754 | (must start with an even-numbered reg) */ | |
755 | if (((argreg - E_1ST_ARGREG) % 2) != 0) | |
756 | argreg++; | |
757 | regcache_raw_write (regcache, argreg++, val); | |
758 | regcache_raw_write (regcache, argreg++, val + 4); | |
759 | } | |
760 | else | |
761 | { | |
762 | /* 8-byte arg goes on stack, must be 8-byte aligned. */ | |
763 | argreg = E_LAST_ARGREG + 1; /* no more argregs. */ | |
764 | stackspace = ((stackspace + 7) & ~7); | |
765 | write_memory (sp + stackspace, val, typelen); | |
766 | stackspace += 8; | |
767 | } | |
768 | } | |
769 | else | |
770 | { | |
771 | /* Store struct beginning at the upper end of the previously | |
772 | computed stack space. Then store the address of the struct | |
773 | using the usual rules for a 4 byte value. */ | |
774 | struct_ptr -= ((typelen + 7) & ~7); | |
775 | write_memory (struct_ptr, val, typelen); | |
776 | if (argreg <= E_LAST_ARGREG) | |
777 | regcache_cooked_write_unsigned (regcache, argreg++, struct_ptr); | |
778 | else | |
779 | { | |
e17a4113 | 780 | store_unsigned_integer (buf, 4, byte_order, struct_ptr); |
20be272b CV |
781 | write_memory (sp + stackspace, buf, 4); |
782 | stackspace += 4; | |
783 | } | |
784 | } | |
785 | } | |
786 | ||
787 | /* Store return address. */ | |
788 | regcache_cooked_write_unsigned (regcache, E_LR_REGNUM, bp_addr); | |
789 | ||
790 | /* Update stack pointer. */ | |
791 | regcache_cooked_write_unsigned (regcache, E_SP_REGNUM, sp); | |
792 | ||
793 | /* And that should do it. Return the new stack pointer. */ | |
794 | return sp; | |
795 | } | |
796 | ||
797 | /* Function: gdbarch_init | |
798 | Initializer function for the iq2000 gdbarch vector. | |
799 | Called by gdbarch. Sets up the gdbarch vector(s) for this target. */ | |
800 | ||
801 | static struct gdbarch * | |
802 | iq2000_gdbarch_init (struct gdbarch_info info, struct gdbarch_list *arches) | |
803 | { | |
804 | struct gdbarch *gdbarch; | |
805 | ||
806 | /* Look up list for candidates - only one. */ | |
807 | arches = gdbarch_list_lookup_by_info (arches, &info); | |
808 | if (arches != NULL) | |
809 | return arches->gdbarch; | |
810 | ||
811 | gdbarch = gdbarch_alloc (&info, NULL); | |
812 | ||
813 | set_gdbarch_num_regs (gdbarch, E_NUM_REGS); | |
814 | set_gdbarch_num_pseudo_regs (gdbarch, 0); | |
815 | set_gdbarch_sp_regnum (gdbarch, E_SP_REGNUM); | |
816 | set_gdbarch_pc_regnum (gdbarch, E_PC_REGNUM); | |
817 | set_gdbarch_register_name (gdbarch, iq2000_register_name); | |
818 | set_gdbarch_address_to_pointer (gdbarch, iq2000_address_to_pointer); | |
819 | set_gdbarch_pointer_to_address (gdbarch, iq2000_pointer_to_address); | |
820 | set_gdbarch_ptr_bit (gdbarch, 4 * TARGET_CHAR_BIT); | |
821 | set_gdbarch_short_bit (gdbarch, 2 * TARGET_CHAR_BIT); | |
822 | set_gdbarch_int_bit (gdbarch, 4 * TARGET_CHAR_BIT); | |
823 | set_gdbarch_long_bit (gdbarch, 4 * TARGET_CHAR_BIT); | |
824 | set_gdbarch_long_long_bit (gdbarch, 8 * TARGET_CHAR_BIT); | |
825 | set_gdbarch_float_bit (gdbarch, 4 * TARGET_CHAR_BIT); | |
826 | set_gdbarch_double_bit (gdbarch, 8 * TARGET_CHAR_BIT); | |
827 | set_gdbarch_long_double_bit (gdbarch, 8 * TARGET_CHAR_BIT); | |
8da61cc4 DJ |
828 | set_gdbarch_float_format (gdbarch, floatformats_ieee_single); |
829 | set_gdbarch_double_format (gdbarch, floatformats_ieee_double); | |
830 | set_gdbarch_long_double_format (gdbarch, floatformats_ieee_double); | |
20be272b CV |
831 | set_gdbarch_return_value (gdbarch, iq2000_return_value); |
832 | set_gdbarch_breakpoint_from_pc (gdbarch, iq2000_breakpoint_from_pc); | |
833 | set_gdbarch_frame_args_skip (gdbarch, 0); | |
834 | set_gdbarch_skip_prologue (gdbarch, iq2000_skip_prologue); | |
835 | set_gdbarch_inner_than (gdbarch, core_addr_lessthan); | |
836 | set_gdbarch_print_insn (gdbarch, print_insn_iq2000); | |
837 | set_gdbarch_register_type (gdbarch, iq2000_register_type); | |
838 | set_gdbarch_frame_align (gdbarch, iq2000_frame_align); | |
839 | set_gdbarch_unwind_sp (gdbarch, iq2000_unwind_sp); | |
840 | set_gdbarch_unwind_pc (gdbarch, iq2000_unwind_pc); | |
94afd7a6 | 841 | set_gdbarch_dummy_id (gdbarch, iq2000_dummy_id); |
20be272b CV |
842 | frame_base_set_default (gdbarch, &iq2000_frame_base); |
843 | set_gdbarch_push_dummy_call (gdbarch, iq2000_push_dummy_call); | |
844 | ||
845 | gdbarch_init_osabi (info, gdbarch); | |
846 | ||
94afd7a6 UW |
847 | dwarf2_append_unwinders (gdbarch); |
848 | frame_unwind_append_unwinder (gdbarch, &iq2000_frame_unwind); | |
20be272b CV |
849 | |
850 | return gdbarch; | |
851 | } | |
852 | ||
853 | /* Function: _initialize_iq2000_tdep | |
854 | Initializer function for the iq2000 module. | |
855 | Called by gdb at start-up. */ | |
856 | ||
63807e1d PA |
857 | /* Provide a prototype to silence -Wmissing-prototypes. */ |
858 | extern initialize_file_ftype _initialize_iq2000_tdep; | |
859 | ||
20be272b CV |
860 | void |
861 | _initialize_iq2000_tdep (void) | |
862 | { | |
863 | register_gdbarch_init (bfd_arch_iq2000, iq2000_gdbarch_init); | |
864 | } |