1 /* Target-dependent code for FT32.
3 Copyright (C) 2009-2016 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 "opcode/ft32.h"
42 #include "ft32-tdep.h"
43 #include "gdb/sim-ft32.h"
46 #define RAM_BIAS 0x800000 /* Bias added to RAM addresses. */
48 /* Local functions. */
50 extern void _initialize_ft32_tdep (void);
52 /* Use an invalid address -1 as 'not available' marker. */
53 enum { REG_UNAVAIL
= (CORE_ADDR
) (-1) };
55 struct ft32_frame_cache
57 /* Base address of the frame */
59 /* Function this frame belongs to */
61 /* Total size of this frame */
63 /* Saved registers in this frame */
64 CORE_ADDR saved_regs
[FT32_NUM_REGS
];
65 /* Saved SP in this frame */
67 /* Has the new frame been LINKed. */
68 bfd_boolean established
;
71 /* Implement the "frame_align" gdbarch method. */
74 ft32_frame_align (struct gdbarch
*gdbarch
, CORE_ADDR sp
)
76 /* Align to the size of an instruction (so that they can safely be
77 pushed onto the stack. */
82 constexpr gdb_byte ft32_break_insn
[] = { 0x02, 0x00, 0x34, 0x00 };
84 typedef BP_MANIPULATION (ft32_break_insn
) ft32_breakpoint
;
86 /* FT32 register names. */
88 static const char *const ft32_register_names
[] =
91 "r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7",
92 "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15",
93 "r16", "r17", "r18", "r19", "r20", "r21", "r22", "r23",
94 "r24", "r25", "r26", "r27", "r28", "cc",
98 /* Implement the "register_name" gdbarch method. */
101 ft32_register_name (struct gdbarch
*gdbarch
, int reg_nr
)
105 if (reg_nr
>= FT32_NUM_REGS
)
107 return ft32_register_names
[reg_nr
];
110 /* Implement the "register_type" gdbarch method. */
113 ft32_register_type (struct gdbarch
*gdbarch
, int reg_nr
)
115 if (reg_nr
== FT32_PC_REGNUM
)
116 return gdbarch_tdep (gdbarch
)->pc_type
;
117 else if (reg_nr
== FT32_SP_REGNUM
|| reg_nr
== FT32_FP_REGNUM
)
118 return builtin_type (gdbarch
)->builtin_data_ptr
;
120 return builtin_type (gdbarch
)->builtin_int32
;
123 /* Write into appropriate registers a function return value
124 of type TYPE, given in virtual format. */
127 ft32_store_return_value (struct type
*type
, struct regcache
*regcache
,
128 const gdb_byte
*valbuf
)
130 struct gdbarch
*gdbarch
= get_regcache_arch (regcache
);
131 enum bfd_endian byte_order
= gdbarch_byte_order (gdbarch
);
133 int len
= TYPE_LENGTH (type
);
135 /* Things always get returned in RET1_REGNUM, RET2_REGNUM. */
136 regval
= extract_unsigned_integer (valbuf
, len
> 4 ? 4 : len
, byte_order
);
137 regcache_cooked_write_unsigned (regcache
, FT32_R0_REGNUM
, regval
);
140 regval
= extract_unsigned_integer (valbuf
+ 4,
141 len
- 4, byte_order
);
142 regcache_cooked_write_unsigned (regcache
, FT32_R1_REGNUM
, regval
);
146 /* Decode the instructions within the given address range. Decide
147 when we must have reached the end of the function prologue. If a
148 frame_info pointer is provided, fill in its saved_regs etc.
150 Returns the address of the first instruction after the prologue. */
153 ft32_analyze_prologue (CORE_ADDR start_addr
, CORE_ADDR end_addr
,
154 struct ft32_frame_cache
*cache
,
155 struct gdbarch
*gdbarch
)
157 enum bfd_endian byte_order
= gdbarch_byte_order (gdbarch
);
161 struct bound_minimal_symbol msymbol
;
162 const int first_saved_reg
= 13; /* The first saved register. */
163 /* PROLOGS are addresses of the subroutine prologs, PROLOGS[n]
164 is the address of __prolog_$rN.
165 __prolog_$rN pushes registers from 13 through n inclusive.
166 So for example CALL __prolog_$r15 is equivalent to:
170 Note that PROLOGS[0] through PROLOGS[12] are unused. */
171 CORE_ADDR prologs
[32];
173 cache
->saved_regs
[FT32_PC_REGNUM
] = 0;
174 cache
->framesize
= 0;
176 for (regnum
= first_saved_reg
; regnum
< 32; regnum
++)
178 char prolog_symbol
[32];
180 snprintf (prolog_symbol
, sizeof (prolog_symbol
), "__prolog_$r%02d",
182 msymbol
= lookup_minimal_symbol (prolog_symbol
, NULL
, NULL
);
184 prologs
[regnum
] = BMSYMBOL_VALUE_ADDRESS (msymbol
);
189 if (start_addr
>= end_addr
)
192 cache
->established
= 0;
193 for (next_addr
= start_addr
; next_addr
< end_addr
;)
195 inst
= read_memory_unsigned_integer (next_addr
, 4, byte_order
);
197 if (FT32_IS_PUSH (inst
))
199 pushreg
= FT32_PUSH_REG (inst
);
200 cache
->framesize
+= 4;
201 cache
->saved_regs
[FT32_R0_REGNUM
+ pushreg
] = cache
->framesize
;
204 else if (FT32_IS_CALL (inst
))
206 for (regnum
= first_saved_reg
; regnum
< 32; regnum
++)
208 if ((4 * (inst
& 0x3ffff)) == prologs
[regnum
])
210 for (pushreg
= first_saved_reg
; pushreg
<= regnum
;
213 cache
->framesize
+= 4;
214 cache
->saved_regs
[FT32_R0_REGNUM
+ pushreg
] =
225 for (regnum
= FT32_R0_REGNUM
; regnum
< FT32_PC_REGNUM
; regnum
++)
227 if (cache
->saved_regs
[regnum
] != REG_UNAVAIL
)
228 cache
->saved_regs
[regnum
] =
229 cache
->framesize
- cache
->saved_regs
[regnum
];
231 cache
->saved_regs
[FT32_PC_REGNUM
] = cache
->framesize
;
234 if (next_addr
< end_addr
)
236 inst
= read_memory_unsigned_integer (next_addr
, 4, byte_order
);
237 if (FT32_IS_LINK (inst
))
239 cache
->established
= 1;
240 for (regnum
= FT32_R0_REGNUM
; regnum
< FT32_PC_REGNUM
; regnum
++)
242 if (cache
->saved_regs
[regnum
] != REG_UNAVAIL
)
243 cache
->saved_regs
[regnum
] += 4;
245 cache
->saved_regs
[FT32_PC_REGNUM
] = cache
->framesize
+ 4;
246 cache
->saved_regs
[FT32_FP_REGNUM
] = 0;
247 cache
->framesize
+= FT32_LINK_SIZE (inst
);
255 /* Find the end of function prologue. */
258 ft32_skip_prologue (struct gdbarch
*gdbarch
, CORE_ADDR pc
)
260 CORE_ADDR func_addr
= 0, func_end
= 0;
261 const char *func_name
;
263 /* See if we can determine the end of the prologue via the symbol table.
264 If so, then return either PC, or the PC after the prologue, whichever
266 if (find_pc_partial_function (pc
, &func_name
, &func_addr
, &func_end
))
268 CORE_ADDR post_prologue_pc
269 = skip_prologue_using_sal (gdbarch
, func_addr
);
270 if (post_prologue_pc
!= 0)
271 return std::max (pc
, post_prologue_pc
);
274 /* Can't determine prologue from the symbol table, need to examine
276 struct symtab_and_line sal
;
278 struct ft32_frame_cache cache
;
281 memset (&cache
, 0, sizeof cache
);
283 plg_end
= ft32_analyze_prologue (func_addr
,
284 func_end
, &cache
, gdbarch
);
285 /* Found a function. */
286 sym
= lookup_symbol (func_name
, NULL
, VAR_DOMAIN
, NULL
).symbol
;
287 /* Don't use line number debug info for assembly source files. */
288 if ((sym
!= NULL
) && SYMBOL_LANGUAGE (sym
) != language_asm
)
290 sal
= find_pc_line (func_addr
, 0);
291 if (sal
.end
&& sal
.end
< func_end
)
293 /* Found a line number, use it as end of prologue. */
297 /* No useable line symbol. Use result of prologue parsing method. */
302 /* No function symbol -- just return the PC. */
306 /* Implementation of `pointer_to_address' gdbarch method.
308 On FT32 address space zero is RAM, address space 1 is flash.
309 RAM appears at address RAM_BIAS, flash at address 0. */
312 ft32_pointer_to_address (struct gdbarch
*gdbarch
,
313 struct type
*type
, const gdb_byte
*buf
)
315 enum bfd_endian byte_order
= gdbarch_byte_order (gdbarch
);
317 = extract_unsigned_integer (buf
, TYPE_LENGTH (type
), byte_order
);
319 if (TYPE_ADDRESS_CLASS_1 (type
))
322 return addr
| RAM_BIAS
;
325 /* Implementation of `address_class_type_flags' gdbarch method.
327 This method maps DW_AT_address_class attributes to a
328 type_instance_flag_value. */
331 ft32_address_class_type_flags (int byte_size
, int dwarf2_addr_class
)
333 /* The value 1 of the DW_AT_address_class attribute corresponds to the
334 __flash__ qualifier, meaning pointer to data in FT32 program memory.
336 if (dwarf2_addr_class
== 1)
337 return TYPE_INSTANCE_FLAG_ADDRESS_CLASS_1
;
341 /* Implementation of `address_class_type_flags_to_name' gdbarch method.
343 Convert a type_instance_flag_value to an address space qualifier. */
346 ft32_address_class_type_flags_to_name (struct gdbarch
*gdbarch
, int type_flags
)
348 if (type_flags
& TYPE_INSTANCE_FLAG_ADDRESS_CLASS_1
)
354 /* Implementation of `address_class_name_to_type_flags' gdbarch method.
356 Convert an address space qualifier to a type_instance_flag_value. */
359 ft32_address_class_name_to_type_flags (struct gdbarch
*gdbarch
,
363 if (strcmp (name
, "flash") == 0)
365 *type_flags_ptr
= TYPE_INSTANCE_FLAG_ADDRESS_CLASS_1
;
373 /* Implement the "read_pc" gdbarch method. */
376 ft32_read_pc (struct regcache
*regcache
)
380 regcache_cooked_read_unsigned (regcache
, FT32_PC_REGNUM
, &pc
);
384 /* Implement the "write_pc" gdbarch method. */
387 ft32_write_pc (struct regcache
*regcache
, CORE_ADDR val
)
389 regcache_cooked_write_unsigned (regcache
, FT32_PC_REGNUM
, val
);
392 /* Implement the "unwind_sp" gdbarch method. */
395 ft32_unwind_sp (struct gdbarch
*gdbarch
, struct frame_info
*next_frame
)
397 return frame_unwind_register_unsigned (next_frame
, FT32_SP_REGNUM
);
400 /* Given a return value in `regbuf' with a type `valtype',
401 extract and copy its value into `valbuf'. */
404 ft32_extract_return_value (struct type
*type
, struct regcache
*regcache
,
407 struct gdbarch
*gdbarch
= get_regcache_arch (regcache
);
408 enum bfd_endian byte_order
= gdbarch_byte_order (gdbarch
);
409 bfd_byte
*valbuf
= dst
;
410 int len
= TYPE_LENGTH (type
);
413 /* By using store_unsigned_integer we avoid having to do
414 anything special for small big-endian values. */
415 regcache_cooked_read_unsigned (regcache
, FT32_R0_REGNUM
, &tmp
);
416 store_unsigned_integer (valbuf
, (len
> 4 ? len
- 4 : len
), byte_order
, tmp
);
418 /* Ignore return values more than 8 bytes in size because the ft32
419 returns anything more than 8 bytes in the stack. */
422 regcache_cooked_read_unsigned (regcache
, FT32_R1_REGNUM
, &tmp
);
423 store_unsigned_integer (valbuf
+ len
- 4, 4, byte_order
, tmp
);
427 /* Implement the "return_value" gdbarch method. */
429 static enum return_value_convention
430 ft32_return_value (struct gdbarch
*gdbarch
, struct value
*function
,
431 struct type
*valtype
, struct regcache
*regcache
,
432 gdb_byte
*readbuf
, const gdb_byte
*writebuf
)
434 if (TYPE_LENGTH (valtype
) > 8)
435 return RETURN_VALUE_STRUCT_CONVENTION
;
439 ft32_extract_return_value (valtype
, regcache
, readbuf
);
440 if (writebuf
!= NULL
)
441 ft32_store_return_value (valtype
, regcache
, writebuf
);
442 return RETURN_VALUE_REGISTER_CONVENTION
;
446 /* Allocate and initialize a ft32_frame_cache object. */
448 static struct ft32_frame_cache
*
449 ft32_alloc_frame_cache (void)
451 struct ft32_frame_cache
*cache
;
454 cache
= FRAME_OBSTACK_ZALLOC (struct ft32_frame_cache
);
456 for (i
= 0; i
< FT32_NUM_REGS
; ++i
)
457 cache
->saved_regs
[i
] = REG_UNAVAIL
;
462 /* Populate a ft32_frame_cache object for this_frame. */
464 static struct ft32_frame_cache
*
465 ft32_frame_cache (struct frame_info
*this_frame
, void **this_cache
)
467 struct ft32_frame_cache
*cache
;
468 CORE_ADDR current_pc
;
472 return (struct ft32_frame_cache
*) *this_cache
;
474 cache
= ft32_alloc_frame_cache ();
477 cache
->base
= get_frame_register_unsigned (this_frame
, FT32_FP_REGNUM
);
478 if (cache
->base
== 0)
481 cache
->pc
= get_frame_func (this_frame
);
482 current_pc
= get_frame_pc (this_frame
);
485 struct gdbarch
*gdbarch
= get_frame_arch (this_frame
);
487 ft32_analyze_prologue (cache
->pc
, current_pc
, cache
, gdbarch
);
488 if (!cache
->established
)
489 cache
->base
= get_frame_register_unsigned (this_frame
, FT32_SP_REGNUM
);
492 cache
->saved_sp
= cache
->base
- 4;
494 for (i
= 0; i
< FT32_NUM_REGS
; ++i
)
495 if (cache
->saved_regs
[i
] != REG_UNAVAIL
)
496 cache
->saved_regs
[i
] = cache
->base
+ cache
->saved_regs
[i
];
501 /* Implement the "unwind_pc" gdbarch method. */
504 ft32_unwind_pc (struct gdbarch
*gdbarch
, struct frame_info
*next_frame
)
506 return frame_unwind_register_unsigned (next_frame
, FT32_PC_REGNUM
);
509 /* Given a GDB frame, determine the address of the calling function's
510 frame. This will be used to create a new GDB frame struct. */
513 ft32_frame_this_id (struct frame_info
*this_frame
,
514 void **this_prologue_cache
, struct frame_id
*this_id
)
516 struct ft32_frame_cache
*cache
= ft32_frame_cache (this_frame
,
517 this_prologue_cache
);
519 /* This marks the outermost frame. */
520 if (cache
->base
== 0)
523 *this_id
= frame_id_build (cache
->saved_sp
, cache
->pc
);
526 /* Get the value of register regnum in the previous stack frame. */
528 static struct value
*
529 ft32_frame_prev_register (struct frame_info
*this_frame
,
530 void **this_prologue_cache
, int regnum
)
532 struct ft32_frame_cache
*cache
= ft32_frame_cache (this_frame
,
533 this_prologue_cache
);
535 gdb_assert (regnum
>= 0);
537 if (regnum
== FT32_SP_REGNUM
&& cache
->saved_sp
)
538 return frame_unwind_got_constant (this_frame
, regnum
, cache
->saved_sp
);
540 if (regnum
< FT32_NUM_REGS
&& cache
->saved_regs
[regnum
] != REG_UNAVAIL
)
541 return frame_unwind_got_memory (this_frame
, regnum
,
542 RAM_BIAS
| cache
->saved_regs
[regnum
]);
544 return frame_unwind_got_register (this_frame
, regnum
, regnum
);
547 static const struct frame_unwind ft32_frame_unwind
=
550 default_frame_unwind_stop_reason
,
552 ft32_frame_prev_register
,
554 default_frame_sniffer
557 /* Return the base address of this_frame. */
560 ft32_frame_base_address (struct frame_info
*this_frame
, void **this_cache
)
562 struct ft32_frame_cache
*cache
= ft32_frame_cache (this_frame
,
568 static const struct frame_base ft32_frame_base
=
571 ft32_frame_base_address
,
572 ft32_frame_base_address
,
573 ft32_frame_base_address
576 static struct frame_id
577 ft32_dummy_id (struct gdbarch
*gdbarch
, struct frame_info
*this_frame
)
579 CORE_ADDR sp
= get_frame_register_unsigned (this_frame
, FT32_SP_REGNUM
);
581 return frame_id_build (sp
, get_frame_pc (this_frame
));
584 /* Allocate and initialize the ft32 gdbarch object. */
586 static struct gdbarch
*
587 ft32_gdbarch_init (struct gdbarch_info info
, struct gdbarch_list
*arches
)
589 struct gdbarch
*gdbarch
;
590 struct gdbarch_tdep
*tdep
;
591 struct type
*void_type
;
592 struct type
*func_void_type
;
594 /* If there is already a candidate, use it. */
595 arches
= gdbarch_list_lookup_by_info (arches
, &info
);
597 return arches
->gdbarch
;
599 /* Allocate space for the new architecture. */
600 tdep
= XNEW (struct gdbarch_tdep
);
601 gdbarch
= gdbarch_alloc (&info
, tdep
);
603 /* Create a type for PC. We can't use builtin types here, as they may not
605 void_type
= arch_type (gdbarch
, TYPE_CODE_VOID
, 1, "void");
606 func_void_type
= make_function_type (void_type
, NULL
);
607 tdep
->pc_type
= arch_pointer_type (gdbarch
, 4 * TARGET_CHAR_BIT
, NULL
,
609 TYPE_INSTANCE_FLAGS (tdep
->pc_type
) |= TYPE_INSTANCE_FLAG_ADDRESS_CLASS_1
;
611 set_gdbarch_read_pc (gdbarch
, ft32_read_pc
);
612 set_gdbarch_write_pc (gdbarch
, ft32_write_pc
);
613 set_gdbarch_unwind_sp (gdbarch
, ft32_unwind_sp
);
615 set_gdbarch_num_regs (gdbarch
, FT32_NUM_REGS
);
616 set_gdbarch_sp_regnum (gdbarch
, FT32_SP_REGNUM
);
617 set_gdbarch_pc_regnum (gdbarch
, FT32_PC_REGNUM
);
618 set_gdbarch_register_name (gdbarch
, ft32_register_name
);
619 set_gdbarch_register_type (gdbarch
, ft32_register_type
);
621 set_gdbarch_return_value (gdbarch
, ft32_return_value
);
623 set_gdbarch_pointer_to_address (gdbarch
, ft32_pointer_to_address
);
625 set_gdbarch_skip_prologue (gdbarch
, ft32_skip_prologue
);
626 set_gdbarch_inner_than (gdbarch
, core_addr_lessthan
);
627 set_gdbarch_breakpoint_kind_from_pc (gdbarch
, ft32_breakpoint::kind_from_pc
);
628 set_gdbarch_sw_breakpoint_from_kind (gdbarch
, ft32_breakpoint::bp_from_kind
);
629 set_gdbarch_frame_align (gdbarch
, ft32_frame_align
);
631 frame_base_set_default (gdbarch
, &ft32_frame_base
);
633 /* Methods for saving / extracting a dummy frame's ID. The ID's
634 stack address must match the SP value returned by
635 PUSH_DUMMY_CALL, and saved by generic_save_dummy_frame_tos. */
636 set_gdbarch_dummy_id (gdbarch
, ft32_dummy_id
);
638 set_gdbarch_unwind_pc (gdbarch
, ft32_unwind_pc
);
640 set_gdbarch_print_insn (gdbarch
, print_insn_ft32
);
642 /* Hook in ABI-specific overrides, if they have been registered. */
643 gdbarch_init_osabi (info
, gdbarch
);
645 /* Hook in the default unwinders. */
646 frame_unwind_append_unwinder (gdbarch
, &ft32_frame_unwind
);
648 /* Support simple overlay manager. */
649 set_gdbarch_overlay_update (gdbarch
, simple_overlay_update
);
651 set_gdbarch_address_class_type_flags (gdbarch
, ft32_address_class_type_flags
);
652 set_gdbarch_address_class_name_to_type_flags
653 (gdbarch
, ft32_address_class_name_to_type_flags
);
654 set_gdbarch_address_class_type_flags_to_name
655 (gdbarch
, ft32_address_class_type_flags_to_name
);
660 /* Register this machine's init routine. */
663 _initialize_ft32_tdep (void)
665 register_gdbarch_init (bfd_arch_ft32
, ft32_gdbarch_init
);