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