1 /* Target-dependent code for FT32.
3 Copyright (C) 2009-2015 Free Software Foundation, Inc.
5 This file is part of GDB.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
22 #include "frame-unwind.h"
23 #include "frame-base.h"
34 #include "arch-utils.h"
36 #include "trad-frame.h"
40 #include "ft32-tdep.h"
41 #include "gdb/sim-ft32.h"
43 #define RAM_BIAS 0x800000 /* Bias added to RAM addresses. */
45 /* Local functions. */
47 extern void _initialize_ft32_tdep (void);
49 /* Use an invalid address -1 as 'not available' marker. */
50 enum { REG_UNAVAIL
= (CORE_ADDR
) (-1) };
52 struct ft32_frame_cache
54 /* Base address of the frame */
56 /* Function this frame belongs to */
58 /* Total size of this frame */
60 /* Saved registers in this frame */
61 CORE_ADDR saved_regs
[FT32_NUM_REGS
];
62 /* Saved SP in this frame */
64 /* Has the new frame been LINKed. */
65 bfd_boolean established
;
68 /* Implement the "frame_align" gdbarch method. */
71 ft32_frame_align (struct gdbarch
*gdbarch
, CORE_ADDR sp
)
73 /* Align to the size of an instruction (so that they can safely be
74 pushed onto the stack. */
78 /* Implement the "breakpoint_from_pc" gdbarch method. */
80 static const unsigned char *
81 ft32_breakpoint_from_pc (struct gdbarch
*gdbarch
,
82 CORE_ADDR
*pcptr
, int *lenptr
)
84 static const gdb_byte breakpoint
[] = { 0x02, 0x00, 0x34, 0x00 };
86 *lenptr
= sizeof (breakpoint
);
90 /* FT32 register names. */
92 static const char *const ft32_register_names
[] =
95 "r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7",
96 "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15",
97 "r16", "r17", "r18", "r19", "r20", "r21", "r22", "r23",
98 "r24", "r25", "r26", "r27", "r28", "cc",
102 /* Implement the "register_name" gdbarch method. */
105 ft32_register_name (struct gdbarch
*gdbarch
, int reg_nr
)
109 if (reg_nr
>= FT32_NUM_REGS
)
111 return ft32_register_names
[reg_nr
];
114 /* Implement the "register_type" gdbarch method. */
117 ft32_register_type (struct gdbarch
*gdbarch
, int reg_nr
)
119 if (reg_nr
== FT32_PC_REGNUM
)
120 return builtin_type (gdbarch
)->builtin_func_ptr
;
121 else if (reg_nr
== FT32_SP_REGNUM
|| reg_nr
== FT32_FP_REGNUM
)
122 return builtin_type (gdbarch
)->builtin_data_ptr
;
124 return builtin_type (gdbarch
)->builtin_int32
;
127 /* Write into appropriate registers a function return value
128 of type TYPE, given in virtual format. */
131 ft32_store_return_value (struct type
*type
, struct regcache
*regcache
,
132 const gdb_byte
*valbuf
)
134 struct gdbarch
*gdbarch
= get_regcache_arch (regcache
);
135 enum bfd_endian byte_order
= gdbarch_byte_order (gdbarch
);
137 int len
= TYPE_LENGTH (type
);
139 /* Things always get returned in RET1_REGNUM, RET2_REGNUM. */
140 regval
= extract_unsigned_integer (valbuf
, len
> 4 ? 4 : len
, byte_order
);
141 regcache_cooked_write_unsigned (regcache
, FT32_R0_REGNUM
, regval
);
144 regval
= extract_unsigned_integer (valbuf
+ 4,
145 len
- 4, byte_order
);
146 regcache_cooked_write_unsigned (regcache
, FT32_R1_REGNUM
, regval
);
150 /* Decode the instructions within the given address range. Decide
151 when we must have reached the end of the function prologue. If a
152 frame_info pointer is provided, fill in its saved_regs etc.
154 Returns the address of the first instruction after the prologue. */
156 #define IS_PUSH(inst) (((inst) & 0xfff00000) == 0x84000000)
157 #define PUSH_REG(inst) (FT32_R0_REGNUM + (((inst) >> 15) & 0x1f))
158 #define IS_LINK(inst) (((inst) & 0xffff0000) == 0x95d00000)
159 #define LINK_SIZE(inst) ((inst) & 0xffff)
162 ft32_analyze_prologue (CORE_ADDR start_addr
, CORE_ADDR end_addr
,
163 struct ft32_frame_cache
*cache
,
164 struct gdbarch
*gdbarch
)
166 enum bfd_endian byte_order
= gdbarch_byte_order (gdbarch
);
168 ULONGEST inst
, inst2
;
172 cache
->saved_regs
[FT32_PC_REGNUM
] = 0;
173 cache
->framesize
= 0;
175 if (start_addr
>= end_addr
)
178 cache
->established
= 0;
179 for (next_addr
= start_addr
; next_addr
< end_addr
; )
181 inst
= read_memory_unsigned_integer (next_addr
, 4, byte_order
);
185 regnum
= PUSH_REG (inst
);
186 cache
->framesize
+= 4;
187 cache
->saved_regs
[regnum
] = cache
->framesize
;
193 for (regnum
= FT32_R0_REGNUM
; regnum
< FT32_PC_REGNUM
; regnum
++)
195 if (cache
->saved_regs
[regnum
] != REG_UNAVAIL
)
196 cache
->saved_regs
[regnum
] = cache
->framesize
- cache
->saved_regs
[regnum
];
198 cache
->saved_regs
[FT32_PC_REGNUM
] = cache
->framesize
;
201 if (next_addr
< end_addr
)
203 inst
= read_memory_unsigned_integer (next_addr
, 4, byte_order
);
206 cache
->established
= 1;
207 for (regnum
= FT32_R0_REGNUM
; regnum
< FT32_PC_REGNUM
; regnum
++)
209 if (cache
->saved_regs
[regnum
] != REG_UNAVAIL
)
210 cache
->saved_regs
[regnum
] += 4;
212 cache
->saved_regs
[FT32_PC_REGNUM
] = cache
->framesize
+ 4;
213 cache
->saved_regs
[FT32_FP_REGNUM
] = 0;
214 cache
->framesize
+= LINK_SIZE (inst
);
222 /* Find the end of function prologue. */
225 ft32_skip_prologue (struct gdbarch
*gdbarch
, CORE_ADDR pc
)
227 CORE_ADDR func_addr
= 0, func_end
= 0;
228 const char *func_name
;
230 /* See if we can determine the end of the prologue via the symbol table.
231 If so, then return either PC, or the PC after the prologue, whichever
233 if (find_pc_partial_function (pc
, &func_name
, &func_addr
, &func_end
))
235 CORE_ADDR post_prologue_pc
236 = skip_prologue_using_sal (gdbarch
, func_addr
);
237 if (post_prologue_pc
!= 0)
238 return max (pc
, post_prologue_pc
);
241 /* Can't determine prologue from the symbol table, need to examine
243 struct symtab_and_line sal
;
245 struct ft32_frame_cache cache
;
248 memset (&cache
, 0, sizeof cache
);
250 plg_end
= ft32_analyze_prologue (func_addr
,
251 func_end
, &cache
, gdbarch
);
252 /* Found a function. */
253 sym
= lookup_symbol (func_name
, NULL
, VAR_DOMAIN
, NULL
);
254 /* Don't use line number debug info for assembly source files. */
255 if ((sym
!= NULL
) && SYMBOL_LANGUAGE (sym
) != language_asm
)
257 sal
= find_pc_line (func_addr
, 0);
258 if (sal
.end
&& sal
.end
< func_end
)
260 /* Found a line number, use it as end of prologue. */
264 /* No useable line symbol. Use result of prologue parsing method. */
269 /* No function symbol -- just return the PC. */
273 /* Implement the "read_pc" gdbarch method. */
276 ft32_read_pc (struct regcache
*regcache
)
280 regcache_cooked_read_unsigned (regcache
, FT32_PC_REGNUM
, &pc
);
284 /* Implement the "write_pc" gdbarch method. */
287 ft32_write_pc (struct regcache
*regcache
, CORE_ADDR val
)
289 regcache_cooked_write_unsigned (regcache
, FT32_PC_REGNUM
, val
);
292 /* Implement the "unwind_sp" gdbarch method. */
295 ft32_unwind_sp (struct gdbarch
*gdbarch
, struct frame_info
*next_frame
)
297 return frame_unwind_register_unsigned (next_frame
, FT32_SP_REGNUM
);
300 /* Given a return value in `regbuf' with a type `valtype',
301 extract and copy its value into `valbuf'. */
304 ft32_extract_return_value (struct type
*type
, struct regcache
*regcache
,
307 struct gdbarch
*gdbarch
= get_regcache_arch (regcache
);
308 enum bfd_endian byte_order
= gdbarch_byte_order (gdbarch
);
309 bfd_byte
*valbuf
= dst
;
310 int len
= TYPE_LENGTH (type
);
313 /* By using store_unsigned_integer we avoid having to do
314 anything special for small big-endian values. */
315 regcache_cooked_read_unsigned (regcache
, FT32_R0_REGNUM
, &tmp
);
316 store_unsigned_integer (valbuf
, (len
> 4 ? len
- 4 : len
), byte_order
, tmp
);
318 /* Ignore return values more than 8 bytes in size because the ft32
319 returns anything more than 8 bytes in the stack. */
322 regcache_cooked_read_unsigned (regcache
, FT32_R1_REGNUM
, &tmp
);
323 store_unsigned_integer (valbuf
+ len
- 4, 4, byte_order
, tmp
);
327 /* Implement the "return_value" gdbarch method. */
329 static enum return_value_convention
330 ft32_return_value (struct gdbarch
*gdbarch
, struct value
*function
,
331 struct type
*valtype
, struct regcache
*regcache
,
332 gdb_byte
*readbuf
, const gdb_byte
*writebuf
)
334 if (TYPE_LENGTH (valtype
) > 8)
335 return RETURN_VALUE_STRUCT_CONVENTION
;
339 ft32_extract_return_value (valtype
, regcache
, readbuf
);
340 if (writebuf
!= NULL
)
341 ft32_store_return_value (valtype
, regcache
, writebuf
);
342 return RETURN_VALUE_REGISTER_CONVENTION
;
346 /* Allocate and initialize a ft32_frame_cache object. */
348 static struct ft32_frame_cache
*
349 ft32_alloc_frame_cache (void)
351 struct ft32_frame_cache
*cache
;
354 cache
= FRAME_OBSTACK_ZALLOC (struct ft32_frame_cache
);
356 for (i
= 0; i
< FT32_NUM_REGS
; ++i
)
357 cache
->saved_regs
[i
] = REG_UNAVAIL
;
362 /* Populate a ft32_frame_cache object for this_frame. */
364 static struct ft32_frame_cache
*
365 ft32_frame_cache (struct frame_info
*this_frame
, void **this_cache
)
367 struct ft32_frame_cache
*cache
;
368 CORE_ADDR current_pc
;
374 cache
= ft32_alloc_frame_cache ();
377 cache
->base
= get_frame_register_unsigned (this_frame
, FT32_FP_REGNUM
);
378 if (cache
->base
== 0)
381 cache
->pc
= get_frame_func (this_frame
);
382 current_pc
= get_frame_pc (this_frame
);
385 struct gdbarch
*gdbarch
= get_frame_arch (this_frame
);
387 ft32_analyze_prologue (cache
->pc
, current_pc
, cache
, gdbarch
);
388 if (!cache
->established
)
389 cache
->base
= get_frame_register_unsigned (this_frame
, FT32_SP_REGNUM
);
392 cache
->saved_sp
= cache
->base
- 4;
394 for (i
= 0; i
< FT32_NUM_REGS
; ++i
)
395 if (cache
->saved_regs
[i
] != REG_UNAVAIL
)
396 cache
->saved_regs
[i
] = cache
->base
+ cache
->saved_regs
[i
];
401 /* Implement the "unwind_pc" gdbarch method. */
404 ft32_unwind_pc (struct gdbarch
*gdbarch
, struct frame_info
*next_frame
)
406 return frame_unwind_register_unsigned (next_frame
, FT32_PC_REGNUM
);
409 /* Given a GDB frame, determine the address of the calling function's
410 frame. This will be used to create a new GDB frame struct. */
413 ft32_frame_this_id (struct frame_info
*this_frame
,
414 void **this_prologue_cache
, struct frame_id
*this_id
)
416 struct ft32_frame_cache
*cache
= ft32_frame_cache (this_frame
,
417 this_prologue_cache
);
419 /* This marks the outermost frame. */
420 if (cache
->base
== 0)
423 *this_id
= frame_id_build (cache
->saved_sp
, cache
->pc
);
426 /* Get the value of register regnum in the previous stack frame. */
428 static struct value
*
429 ft32_frame_prev_register (struct frame_info
*this_frame
,
430 void **this_prologue_cache
, int regnum
)
432 struct ft32_frame_cache
*cache
= ft32_frame_cache (this_frame
,
433 this_prologue_cache
);
435 gdb_assert (regnum
>= 0);
437 if (regnum
== FT32_SP_REGNUM
&& cache
->saved_sp
)
438 return frame_unwind_got_constant (this_frame
, regnum
, cache
->saved_sp
);
440 if (regnum
< FT32_NUM_REGS
&& cache
->saved_regs
[regnum
] != REG_UNAVAIL
)
441 return frame_unwind_got_memory (this_frame
, regnum
,
442 RAM_BIAS
| cache
->saved_regs
[regnum
]);
444 return frame_unwind_got_register (this_frame
, regnum
, regnum
);
447 static const struct frame_unwind ft32_frame_unwind
=
450 default_frame_unwind_stop_reason
,
452 ft32_frame_prev_register
,
454 default_frame_sniffer
457 /* Return the base address of this_frame. */
460 ft32_frame_base_address (struct frame_info
*this_frame
, void **this_cache
)
462 struct ft32_frame_cache
*cache
= ft32_frame_cache (this_frame
,
468 static const struct frame_base ft32_frame_base
=
471 ft32_frame_base_address
,
472 ft32_frame_base_address
,
473 ft32_frame_base_address
476 static struct frame_id
477 ft32_dummy_id (struct gdbarch
*gdbarch
, struct frame_info
*this_frame
)
479 CORE_ADDR sp
= get_frame_register_unsigned (this_frame
, FT32_SP_REGNUM
);
481 return frame_id_build (sp
, get_frame_pc (this_frame
));
484 /* Allocate and initialize the ft32 gdbarch object. */
486 static struct gdbarch
*
487 ft32_gdbarch_init (struct gdbarch_info info
, struct gdbarch_list
*arches
)
489 struct gdbarch
*gdbarch
;
490 struct gdbarch_tdep
*tdep
;
492 /* If there is already a candidate, use it. */
493 arches
= gdbarch_list_lookup_by_info (arches
, &info
);
495 return arches
->gdbarch
;
497 /* Allocate space for the new architecture. */
498 tdep
= XNEW (struct gdbarch_tdep
);
499 gdbarch
= gdbarch_alloc (&info
, tdep
);
501 set_gdbarch_read_pc (gdbarch
, ft32_read_pc
);
502 set_gdbarch_write_pc (gdbarch
, ft32_write_pc
);
503 set_gdbarch_unwind_sp (gdbarch
, ft32_unwind_sp
);
505 set_gdbarch_num_regs (gdbarch
, FT32_NUM_REGS
);
506 set_gdbarch_sp_regnum (gdbarch
, FT32_SP_REGNUM
);
507 set_gdbarch_pc_regnum (gdbarch
, FT32_PC_REGNUM
);
508 set_gdbarch_register_name (gdbarch
, ft32_register_name
);
509 set_gdbarch_register_type (gdbarch
, ft32_register_type
);
511 set_gdbarch_return_value (gdbarch
, ft32_return_value
);
513 set_gdbarch_skip_prologue (gdbarch
, ft32_skip_prologue
);
514 set_gdbarch_inner_than (gdbarch
, core_addr_lessthan
);
515 set_gdbarch_breakpoint_from_pc (gdbarch
, ft32_breakpoint_from_pc
);
516 set_gdbarch_frame_align (gdbarch
, ft32_frame_align
);
518 frame_base_set_default (gdbarch
, &ft32_frame_base
);
520 /* Methods for saving / extracting a dummy frame's ID. The ID's
521 stack address must match the SP value returned by
522 PUSH_DUMMY_CALL, and saved by generic_save_dummy_frame_tos. */
523 set_gdbarch_dummy_id (gdbarch
, ft32_dummy_id
);
525 set_gdbarch_unwind_pc (gdbarch
, ft32_unwind_pc
);
527 set_gdbarch_print_insn (gdbarch
, print_insn_ft32
);
529 /* Hook in ABI-specific overrides, if they have been registered. */
530 gdbarch_init_osabi (info
, gdbarch
);
532 /* Hook in the default unwinders. */
533 frame_unwind_append_unwinder (gdbarch
, &ft32_frame_unwind
);
535 /* Support simple overlay manager. */
536 set_gdbarch_overlay_update (gdbarch
, simple_overlay_update
);
541 /* Register this machine's init routine. */
544 _initialize_ft32_tdep (void)
546 register_gdbarch_init (bfd_arch_ft32
, ft32_gdbarch_init
);