1 /* Target-dependent code for FT32.
3 Copyright (C) 2009-2017 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 /* Use an invalid address -1 as 'not available' marker. */
49 enum { REG_UNAVAIL
= (CORE_ADDR
) (-1) };
51 struct ft32_frame_cache
53 /* Base address of the frame */
55 /* Function this frame belongs to */
57 /* Total size of this frame */
59 /* Saved registers in this frame */
60 CORE_ADDR saved_regs
[FT32_NUM_REGS
];
61 /* Saved SP in this frame */
63 /* Has the new frame been LINKed. */
64 bfd_boolean established
;
67 /* Implement the "frame_align" gdbarch method. */
70 ft32_frame_align (struct gdbarch
*gdbarch
, CORE_ADDR sp
)
72 /* Align to the size of an instruction (so that they can safely be
73 pushed onto the stack. */
78 constexpr gdb_byte ft32_break_insn
[] = { 0x02, 0x00, 0x34, 0x00 };
80 typedef BP_MANIPULATION (ft32_break_insn
) ft32_breakpoint
;
82 /* FT32 register names. */
84 static const char *const ft32_register_names
[] =
87 "r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7",
88 "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15",
89 "r16", "r17", "r18", "r19", "r20", "r21", "r22", "r23",
90 "r24", "r25", "r26", "r27", "r28", "cc",
94 /* Implement the "register_name" gdbarch method. */
97 ft32_register_name (struct gdbarch
*gdbarch
, int reg_nr
)
101 if (reg_nr
>= FT32_NUM_REGS
)
103 return ft32_register_names
[reg_nr
];
106 /* Implement the "register_type" gdbarch method. */
109 ft32_register_type (struct gdbarch
*gdbarch
, int reg_nr
)
111 if (reg_nr
== FT32_PC_REGNUM
)
112 return gdbarch_tdep (gdbarch
)->pc_type
;
113 else if (reg_nr
== FT32_SP_REGNUM
|| reg_nr
== FT32_FP_REGNUM
)
114 return builtin_type (gdbarch
)->builtin_data_ptr
;
116 return builtin_type (gdbarch
)->builtin_int32
;
119 /* Write into appropriate registers a function return value
120 of type TYPE, given in virtual format. */
123 ft32_store_return_value (struct type
*type
, struct regcache
*regcache
,
124 const gdb_byte
*valbuf
)
126 struct gdbarch
*gdbarch
= regcache
->arch ();
127 enum bfd_endian byte_order
= gdbarch_byte_order (gdbarch
);
129 int len
= TYPE_LENGTH (type
);
131 /* Things always get returned in RET1_REGNUM, RET2_REGNUM. */
132 regval
= extract_unsigned_integer (valbuf
, len
> 4 ? 4 : len
, byte_order
);
133 regcache_cooked_write_unsigned (regcache
, FT32_R0_REGNUM
, regval
);
136 regval
= extract_unsigned_integer (valbuf
+ 4,
137 len
- 4, byte_order
);
138 regcache_cooked_write_unsigned (regcache
, FT32_R1_REGNUM
, regval
);
142 /* Fetch a single 32-bit instruction from address a. If memory contains
143 a compressed instruction pair, return the expanded instruction. */
146 ft32_fetch_instruction (CORE_ADDR a
, int *isize
,
147 enum bfd_endian byte_order
)
152 CORE_ADDR a4
= a
& ~3;
153 inst
= read_code_unsigned_integer (a4
, 4, byte_order
);
154 *isize
= ft32_decode_shortcode (a4
, inst
, sc
) ? 2 : 4;
156 return sc
[1 & (a
>> 1)];
161 /* Decode the instructions within the given address range. Decide
162 when we must have reached the end of the function prologue. If a
163 frame_info pointer is provided, fill in its saved_regs etc.
165 Returns the address of the first instruction after the prologue. */
168 ft32_analyze_prologue (CORE_ADDR start_addr
, CORE_ADDR end_addr
,
169 struct ft32_frame_cache
*cache
,
170 struct gdbarch
*gdbarch
)
172 enum bfd_endian byte_order
= gdbarch_byte_order (gdbarch
);
177 struct bound_minimal_symbol msymbol
;
178 const int first_saved_reg
= 13; /* The first saved register. */
179 /* PROLOGS are addresses of the subroutine prologs, PROLOGS[n]
180 is the address of __prolog_$rN.
181 __prolog_$rN pushes registers from 13 through n inclusive.
182 So for example CALL __prolog_$r15 is equivalent to:
186 Note that PROLOGS[0] through PROLOGS[12] are unused. */
187 CORE_ADDR prologs
[32];
189 cache
->saved_regs
[FT32_PC_REGNUM
] = 0;
190 cache
->framesize
= 0;
192 for (regnum
= first_saved_reg
; regnum
< 32; regnum
++)
194 char prolog_symbol
[32];
196 snprintf (prolog_symbol
, sizeof (prolog_symbol
), "__prolog_$r%02d",
198 msymbol
= lookup_minimal_symbol (prolog_symbol
, NULL
, NULL
);
200 prologs
[regnum
] = BMSYMBOL_VALUE_ADDRESS (msymbol
);
205 if (start_addr
>= end_addr
)
208 cache
->established
= 0;
209 for (next_addr
= start_addr
; next_addr
< end_addr
; next_addr
+= isize
)
211 inst
= ft32_fetch_instruction (next_addr
, &isize
, byte_order
);
213 if (FT32_IS_PUSH (inst
))
215 pushreg
= FT32_PUSH_REG (inst
);
216 cache
->framesize
+= 4;
217 cache
->saved_regs
[FT32_R0_REGNUM
+ pushreg
] = cache
->framesize
;
219 else if (FT32_IS_CALL (inst
))
221 for (regnum
= first_saved_reg
; regnum
< 32; regnum
++)
223 if ((4 * (inst
& 0x3ffff)) == prologs
[regnum
])
225 for (pushreg
= first_saved_reg
; pushreg
<= regnum
;
228 cache
->framesize
+= 4;
229 cache
->saved_regs
[FT32_R0_REGNUM
+ pushreg
] =
239 for (regnum
= FT32_R0_REGNUM
; regnum
< FT32_PC_REGNUM
; regnum
++)
241 if (cache
->saved_regs
[regnum
] != REG_UNAVAIL
)
242 cache
->saved_regs
[regnum
] =
243 cache
->framesize
- cache
->saved_regs
[regnum
];
245 cache
->saved_regs
[FT32_PC_REGNUM
] = cache
->framesize
;
248 if (next_addr
< end_addr
)
250 inst
= ft32_fetch_instruction (next_addr
, &isize
, byte_order
);
251 if (FT32_IS_LINK (inst
))
253 cache
->established
= 1;
254 for (regnum
= FT32_R0_REGNUM
; regnum
< FT32_PC_REGNUM
; regnum
++)
256 if (cache
->saved_regs
[regnum
] != REG_UNAVAIL
)
257 cache
->saved_regs
[regnum
] += 4;
259 cache
->saved_regs
[FT32_PC_REGNUM
] = cache
->framesize
+ 4;
260 cache
->saved_regs
[FT32_FP_REGNUM
] = 0;
261 cache
->framesize
+= FT32_LINK_SIZE (inst
);
269 /* Find the end of function prologue. */
272 ft32_skip_prologue (struct gdbarch
*gdbarch
, CORE_ADDR pc
)
274 CORE_ADDR func_addr
= 0, func_end
= 0;
275 const char *func_name
;
277 /* See if we can determine the end of the prologue via the symbol table.
278 If so, then return either PC, or the PC after the prologue, whichever
280 if (find_pc_partial_function (pc
, &func_name
, &func_addr
, &func_end
))
282 CORE_ADDR post_prologue_pc
283 = skip_prologue_using_sal (gdbarch
, func_addr
);
284 if (post_prologue_pc
!= 0)
285 return std::max (pc
, post_prologue_pc
);
288 /* Can't determine prologue from the symbol table, need to examine
290 struct symtab_and_line sal
;
292 struct ft32_frame_cache cache
;
295 memset (&cache
, 0, sizeof cache
);
297 plg_end
= ft32_analyze_prologue (func_addr
,
298 func_end
, &cache
, gdbarch
);
299 /* Found a function. */
300 sym
= lookup_symbol (func_name
, NULL
, VAR_DOMAIN
, NULL
).symbol
;
301 /* Don't use line number debug info for assembly source files. */
302 if ((sym
!= NULL
) && SYMBOL_LANGUAGE (sym
) != language_asm
)
304 sal
= find_pc_line (func_addr
, 0);
305 if (sal
.end
&& sal
.end
< func_end
)
307 /* Found a line number, use it as end of prologue. */
311 /* No useable line symbol. Use result of prologue parsing method. */
316 /* No function symbol -- just return the PC. */
320 /* Implementation of `pointer_to_address' gdbarch method.
322 On FT32 address space zero is RAM, address space 1 is flash.
323 RAM appears at address RAM_BIAS, flash at address 0. */
326 ft32_pointer_to_address (struct gdbarch
*gdbarch
,
327 struct type
*type
, const gdb_byte
*buf
)
329 enum bfd_endian byte_order
= gdbarch_byte_order (gdbarch
);
331 = extract_unsigned_integer (buf
, TYPE_LENGTH (type
), byte_order
);
333 if (TYPE_ADDRESS_CLASS_1 (type
))
336 return addr
| RAM_BIAS
;
339 /* Implementation of `address_class_type_flags' gdbarch method.
341 This method maps DW_AT_address_class attributes to a
342 type_instance_flag_value. */
345 ft32_address_class_type_flags (int byte_size
, int dwarf2_addr_class
)
347 /* The value 1 of the DW_AT_address_class attribute corresponds to the
348 __flash__ qualifier, meaning pointer to data in FT32 program memory.
350 if (dwarf2_addr_class
== 1)
351 return TYPE_INSTANCE_FLAG_ADDRESS_CLASS_1
;
355 /* Implementation of `address_class_type_flags_to_name' gdbarch method.
357 Convert a type_instance_flag_value to an address space qualifier. */
360 ft32_address_class_type_flags_to_name (struct gdbarch
*gdbarch
, int type_flags
)
362 if (type_flags
& TYPE_INSTANCE_FLAG_ADDRESS_CLASS_1
)
368 /* Implementation of `address_class_name_to_type_flags' gdbarch method.
370 Convert an address space qualifier to a type_instance_flag_value. */
373 ft32_address_class_name_to_type_flags (struct gdbarch
*gdbarch
,
377 if (strcmp (name
, "flash") == 0)
379 *type_flags_ptr
= TYPE_INSTANCE_FLAG_ADDRESS_CLASS_1
;
387 /* Implement the "read_pc" gdbarch method. */
390 ft32_read_pc (struct regcache
*regcache
)
394 regcache_cooked_read_unsigned (regcache
, FT32_PC_REGNUM
, &pc
);
398 /* Implement the "write_pc" gdbarch method. */
401 ft32_write_pc (struct regcache
*regcache
, CORE_ADDR val
)
403 regcache_cooked_write_unsigned (regcache
, FT32_PC_REGNUM
, val
);
406 /* Implement the "unwind_sp" gdbarch method. */
409 ft32_unwind_sp (struct gdbarch
*gdbarch
, struct frame_info
*next_frame
)
411 return frame_unwind_register_unsigned (next_frame
, FT32_SP_REGNUM
);
414 /* Given a return value in `regbuf' with a type `valtype',
415 extract and copy its value into `valbuf'. */
418 ft32_extract_return_value (struct type
*type
, struct regcache
*regcache
,
421 struct gdbarch
*gdbarch
= regcache
->arch ();
422 enum bfd_endian byte_order
= gdbarch_byte_order (gdbarch
);
423 bfd_byte
*valbuf
= dst
;
424 int len
= TYPE_LENGTH (type
);
427 /* By using store_unsigned_integer we avoid having to do
428 anything special for small big-endian values. */
429 regcache_cooked_read_unsigned (regcache
, FT32_R0_REGNUM
, &tmp
);
430 store_unsigned_integer (valbuf
, (len
> 4 ? len
- 4 : len
), byte_order
, tmp
);
432 /* Ignore return values more than 8 bytes in size because the ft32
433 returns anything more than 8 bytes in the stack. */
436 regcache_cooked_read_unsigned (regcache
, FT32_R1_REGNUM
, &tmp
);
437 store_unsigned_integer (valbuf
+ len
- 4, 4, byte_order
, tmp
);
441 /* Implement the "return_value" gdbarch method. */
443 static enum return_value_convention
444 ft32_return_value (struct gdbarch
*gdbarch
, struct value
*function
,
445 struct type
*valtype
, struct regcache
*regcache
,
446 gdb_byte
*readbuf
, const gdb_byte
*writebuf
)
448 if (TYPE_LENGTH (valtype
) > 8)
449 return RETURN_VALUE_STRUCT_CONVENTION
;
453 ft32_extract_return_value (valtype
, regcache
, readbuf
);
454 if (writebuf
!= NULL
)
455 ft32_store_return_value (valtype
, regcache
, writebuf
);
456 return RETURN_VALUE_REGISTER_CONVENTION
;
460 /* Allocate and initialize a ft32_frame_cache object. */
462 static struct ft32_frame_cache
*
463 ft32_alloc_frame_cache (void)
465 struct ft32_frame_cache
*cache
;
468 cache
= FRAME_OBSTACK_ZALLOC (struct ft32_frame_cache
);
470 for (i
= 0; i
< FT32_NUM_REGS
; ++i
)
471 cache
->saved_regs
[i
] = REG_UNAVAIL
;
476 /* Populate a ft32_frame_cache object for this_frame. */
478 static struct ft32_frame_cache
*
479 ft32_frame_cache (struct frame_info
*this_frame
, void **this_cache
)
481 struct ft32_frame_cache
*cache
;
482 CORE_ADDR current_pc
;
486 return (struct ft32_frame_cache
*) *this_cache
;
488 cache
= ft32_alloc_frame_cache ();
491 cache
->base
= get_frame_register_unsigned (this_frame
, FT32_FP_REGNUM
);
492 if (cache
->base
== 0)
495 cache
->pc
= get_frame_func (this_frame
);
496 current_pc
= get_frame_pc (this_frame
);
499 struct gdbarch
*gdbarch
= get_frame_arch (this_frame
);
501 ft32_analyze_prologue (cache
->pc
, current_pc
, cache
, gdbarch
);
502 if (!cache
->established
)
503 cache
->base
= get_frame_register_unsigned (this_frame
, FT32_SP_REGNUM
);
506 cache
->saved_sp
= cache
->base
- 4;
508 for (i
= 0; i
< FT32_NUM_REGS
; ++i
)
509 if (cache
->saved_regs
[i
] != REG_UNAVAIL
)
510 cache
->saved_regs
[i
] = cache
->base
+ cache
->saved_regs
[i
];
515 /* Implement the "unwind_pc" gdbarch method. */
518 ft32_unwind_pc (struct gdbarch
*gdbarch
, struct frame_info
*next_frame
)
520 return frame_unwind_register_unsigned (next_frame
, FT32_PC_REGNUM
);
523 /* Given a GDB frame, determine the address of the calling function's
524 frame. This will be used to create a new GDB frame struct. */
527 ft32_frame_this_id (struct frame_info
*this_frame
,
528 void **this_prologue_cache
, struct frame_id
*this_id
)
530 struct ft32_frame_cache
*cache
= ft32_frame_cache (this_frame
,
531 this_prologue_cache
);
533 /* This marks the outermost frame. */
534 if (cache
->base
== 0)
537 *this_id
= frame_id_build (cache
->saved_sp
, cache
->pc
);
540 /* Get the value of register regnum in the previous stack frame. */
542 static struct value
*
543 ft32_frame_prev_register (struct frame_info
*this_frame
,
544 void **this_prologue_cache
, int regnum
)
546 struct ft32_frame_cache
*cache
= ft32_frame_cache (this_frame
,
547 this_prologue_cache
);
549 gdb_assert (regnum
>= 0);
551 if (regnum
== FT32_SP_REGNUM
&& cache
->saved_sp
)
552 return frame_unwind_got_constant (this_frame
, regnum
, cache
->saved_sp
);
554 if (regnum
< FT32_NUM_REGS
&& cache
->saved_regs
[regnum
] != REG_UNAVAIL
)
555 return frame_unwind_got_memory (this_frame
, regnum
,
556 RAM_BIAS
| cache
->saved_regs
[regnum
]);
558 return frame_unwind_got_register (this_frame
, regnum
, regnum
);
561 static const struct frame_unwind ft32_frame_unwind
=
564 default_frame_unwind_stop_reason
,
566 ft32_frame_prev_register
,
568 default_frame_sniffer
571 /* Return the base address of this_frame. */
574 ft32_frame_base_address (struct frame_info
*this_frame
, void **this_cache
)
576 struct ft32_frame_cache
*cache
= ft32_frame_cache (this_frame
,
582 static const struct frame_base ft32_frame_base
=
585 ft32_frame_base_address
,
586 ft32_frame_base_address
,
587 ft32_frame_base_address
590 static struct frame_id
591 ft32_dummy_id (struct gdbarch
*gdbarch
, struct frame_info
*this_frame
)
593 CORE_ADDR sp
= get_frame_register_unsigned (this_frame
, FT32_SP_REGNUM
);
595 return frame_id_build (sp
, get_frame_pc (this_frame
));
598 /* Allocate and initialize the ft32 gdbarch object. */
600 static struct gdbarch
*
601 ft32_gdbarch_init (struct gdbarch_info info
, struct gdbarch_list
*arches
)
603 struct gdbarch
*gdbarch
;
604 struct gdbarch_tdep
*tdep
;
605 struct type
*void_type
;
606 struct type
*func_void_type
;
608 /* If there is already a candidate, use it. */
609 arches
= gdbarch_list_lookup_by_info (arches
, &info
);
611 return arches
->gdbarch
;
613 /* Allocate space for the new architecture. */
614 tdep
= XCNEW (struct gdbarch_tdep
);
615 gdbarch
= gdbarch_alloc (&info
, tdep
);
617 /* Create a type for PC. We can't use builtin types here, as they may not
619 void_type
= arch_type (gdbarch
, TYPE_CODE_VOID
, TARGET_CHAR_BIT
, "void");
620 func_void_type
= make_function_type (void_type
, NULL
);
621 tdep
->pc_type
= arch_pointer_type (gdbarch
, 4 * TARGET_CHAR_BIT
, NULL
,
623 TYPE_INSTANCE_FLAGS (tdep
->pc_type
) |= TYPE_INSTANCE_FLAG_ADDRESS_CLASS_1
;
625 set_gdbarch_read_pc (gdbarch
, ft32_read_pc
);
626 set_gdbarch_write_pc (gdbarch
, ft32_write_pc
);
627 set_gdbarch_unwind_sp (gdbarch
, ft32_unwind_sp
);
629 set_gdbarch_num_regs (gdbarch
, FT32_NUM_REGS
);
630 set_gdbarch_sp_regnum (gdbarch
, FT32_SP_REGNUM
);
631 set_gdbarch_pc_regnum (gdbarch
, FT32_PC_REGNUM
);
632 set_gdbarch_register_name (gdbarch
, ft32_register_name
);
633 set_gdbarch_register_type (gdbarch
, ft32_register_type
);
635 set_gdbarch_return_value (gdbarch
, ft32_return_value
);
637 set_gdbarch_pointer_to_address (gdbarch
, ft32_pointer_to_address
);
639 set_gdbarch_skip_prologue (gdbarch
, ft32_skip_prologue
);
640 set_gdbarch_inner_than (gdbarch
, core_addr_lessthan
);
641 set_gdbarch_breakpoint_kind_from_pc (gdbarch
, ft32_breakpoint::kind_from_pc
);
642 set_gdbarch_sw_breakpoint_from_kind (gdbarch
, ft32_breakpoint::bp_from_kind
);
643 set_gdbarch_frame_align (gdbarch
, ft32_frame_align
);
645 frame_base_set_default (gdbarch
, &ft32_frame_base
);
647 /* Methods for saving / extracting a dummy frame's ID. The ID's
648 stack address must match the SP value returned by
649 PUSH_DUMMY_CALL, and saved by generic_save_dummy_frame_tos. */
650 set_gdbarch_dummy_id (gdbarch
, ft32_dummy_id
);
652 set_gdbarch_unwind_pc (gdbarch
, ft32_unwind_pc
);
654 /* Hook in ABI-specific overrides, if they have been registered. */
655 gdbarch_init_osabi (info
, gdbarch
);
657 /* Hook in the default unwinders. */
658 frame_unwind_append_unwinder (gdbarch
, &ft32_frame_unwind
);
660 /* Support simple overlay manager. */
661 set_gdbarch_overlay_update (gdbarch
, simple_overlay_update
);
663 set_gdbarch_address_class_type_flags (gdbarch
, ft32_address_class_type_flags
);
664 set_gdbarch_address_class_name_to_type_flags
665 (gdbarch
, ft32_address_class_name_to_type_flags
);
666 set_gdbarch_address_class_type_flags_to_name
667 (gdbarch
, ft32_address_class_type_flags_to_name
);
672 /* Register this machine's init routine. */
675 _initialize_ft32_tdep (void)
677 register_gdbarch_init (bfd_arch_ft32
, ft32_gdbarch_init
);