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 "opcode/ft32.h"
42 #include "ft32-tdep.h"
43 #include "gdb/sim-ft32.h"
45 #define RAM_BIAS 0x800000 /* Bias added to RAM addresses. */
47 /* Local functions. */
49 extern void _initialize_ft32_tdep (void);
51 /* Use an invalid address -1 as 'not available' marker. */
52 enum { REG_UNAVAIL
= (CORE_ADDR
) (-1) };
54 struct ft32_frame_cache
56 /* Base address of the frame */
58 /* Function this frame belongs to */
60 /* Total size of this frame */
62 /* Saved registers in this frame */
63 CORE_ADDR saved_regs
[FT32_NUM_REGS
];
64 /* Saved SP in this frame */
66 /* Has the new frame been LINKed. */
67 bfd_boolean established
;
70 /* Implement the "frame_align" gdbarch method. */
73 ft32_frame_align (struct gdbarch
*gdbarch
, CORE_ADDR sp
)
75 /* Align to the size of an instruction (so that they can safely be
76 pushed onto the stack. */
80 /* Implement the "breakpoint_from_pc" gdbarch method. */
82 static const unsigned char *
83 ft32_breakpoint_from_pc (struct gdbarch
*gdbarch
,
84 CORE_ADDR
*pcptr
, int *lenptr
)
86 static const gdb_byte breakpoint
[] = { 0x02, 0x00, 0x34, 0x00 };
88 *lenptr
= sizeof (breakpoint
);
92 /* FT32 register names. */
94 static const char *const ft32_register_names
[] =
97 "r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7",
98 "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15",
99 "r16", "r17", "r18", "r19", "r20", "r21", "r22", "r23",
100 "r24", "r25", "r26", "r27", "r28", "cc",
104 /* Implement the "register_name" gdbarch method. */
107 ft32_register_name (struct gdbarch
*gdbarch
, int reg_nr
)
111 if (reg_nr
>= FT32_NUM_REGS
)
113 return ft32_register_names
[reg_nr
];
116 /* Implement the "register_type" gdbarch method. */
119 ft32_register_type (struct gdbarch
*gdbarch
, int reg_nr
)
121 if (reg_nr
== FT32_PC_REGNUM
)
122 return gdbarch_tdep (gdbarch
)->pc_type
;
123 else if (reg_nr
== FT32_SP_REGNUM
|| reg_nr
== FT32_FP_REGNUM
)
124 return builtin_type (gdbarch
)->builtin_data_ptr
;
126 return builtin_type (gdbarch
)->builtin_int32
;
129 /* Write into appropriate registers a function return value
130 of type TYPE, given in virtual format. */
133 ft32_store_return_value (struct type
*type
, struct regcache
*regcache
,
134 const gdb_byte
*valbuf
)
136 struct gdbarch
*gdbarch
= get_regcache_arch (regcache
);
137 enum bfd_endian byte_order
= gdbarch_byte_order (gdbarch
);
139 int len
= TYPE_LENGTH (type
);
141 /* Things always get returned in RET1_REGNUM, RET2_REGNUM. */
142 regval
= extract_unsigned_integer (valbuf
, len
> 4 ? 4 : len
, byte_order
);
143 regcache_cooked_write_unsigned (regcache
, FT32_R0_REGNUM
, regval
);
146 regval
= extract_unsigned_integer (valbuf
+ 4,
147 len
- 4, byte_order
);
148 regcache_cooked_write_unsigned (regcache
, FT32_R1_REGNUM
, regval
);
152 /* Decode the instructions within the given address range. Decide
153 when we must have reached the end of the function prologue. If a
154 frame_info pointer is provided, fill in its saved_regs etc.
156 Returns the address of the first instruction after the prologue. */
159 ft32_analyze_prologue (CORE_ADDR start_addr
, CORE_ADDR end_addr
,
160 struct ft32_frame_cache
*cache
,
161 struct gdbarch
*gdbarch
)
163 enum bfd_endian byte_order
= gdbarch_byte_order (gdbarch
);
165 ULONGEST inst
, inst2
;
169 cache
->saved_regs
[FT32_PC_REGNUM
] = 0;
170 cache
->framesize
= 0;
172 if (start_addr
>= end_addr
)
175 cache
->established
= 0;
176 for (next_addr
= start_addr
; next_addr
< end_addr
; )
178 inst
= read_memory_unsigned_integer (next_addr
, 4, byte_order
);
180 if (FT32_IS_PUSH (inst
))
182 regnum
= FT32_R0_REGNUM
+ FT32_PUSH_REG (inst
);
183 cache
->framesize
+= 4;
184 cache
->saved_regs
[regnum
] = cache
->framesize
;
190 for (regnum
= FT32_R0_REGNUM
; regnum
< FT32_PC_REGNUM
; regnum
++)
192 if (cache
->saved_regs
[regnum
] != REG_UNAVAIL
)
193 cache
->saved_regs
[regnum
] = cache
->framesize
- cache
->saved_regs
[regnum
];
195 cache
->saved_regs
[FT32_PC_REGNUM
] = cache
->framesize
;
198 if (next_addr
< end_addr
)
200 inst
= read_memory_unsigned_integer (next_addr
, 4, byte_order
);
201 if (FT32_IS_LINK (inst
))
203 cache
->established
= 1;
204 for (regnum
= FT32_R0_REGNUM
; regnum
< FT32_PC_REGNUM
; regnum
++)
206 if (cache
->saved_regs
[regnum
] != REG_UNAVAIL
)
207 cache
->saved_regs
[regnum
] += 4;
209 cache
->saved_regs
[FT32_PC_REGNUM
] = cache
->framesize
+ 4;
210 cache
->saved_regs
[FT32_FP_REGNUM
] = 0;
211 cache
->framesize
+= FT32_LINK_SIZE (inst
);
219 /* Find the end of function prologue. */
222 ft32_skip_prologue (struct gdbarch
*gdbarch
, CORE_ADDR pc
)
224 CORE_ADDR func_addr
= 0, func_end
= 0;
225 const char *func_name
;
227 /* See if we can determine the end of the prologue via the symbol table.
228 If so, then return either PC, or the PC after the prologue, whichever
230 if (find_pc_partial_function (pc
, &func_name
, &func_addr
, &func_end
))
232 CORE_ADDR post_prologue_pc
233 = skip_prologue_using_sal (gdbarch
, func_addr
);
234 if (post_prologue_pc
!= 0)
235 return max (pc
, post_prologue_pc
);
238 /* Can't determine prologue from the symbol table, need to examine
240 struct symtab_and_line sal
;
242 struct ft32_frame_cache cache
;
245 memset (&cache
, 0, sizeof cache
);
247 plg_end
= ft32_analyze_prologue (func_addr
,
248 func_end
, &cache
, gdbarch
);
249 /* Found a function. */
250 sym
= lookup_symbol (func_name
, NULL
, VAR_DOMAIN
, NULL
).symbol
;
251 /* Don't use line number debug info for assembly source files. */
252 if ((sym
!= NULL
) && SYMBOL_LANGUAGE (sym
) != language_asm
)
254 sal
= find_pc_line (func_addr
, 0);
255 if (sal
.end
&& sal
.end
< func_end
)
257 /* Found a line number, use it as end of prologue. */
261 /* No useable line symbol. Use result of prologue parsing method. */
266 /* No function symbol -- just return the PC. */
270 /* Implementation of `pointer_to_address' gdbarch method.
272 On FT32 address space zero is RAM, address space 1 is flash.
273 RAM appears at address RAM_BIAS, flash at address 0. */
276 ft32_pointer_to_address (struct gdbarch
*gdbarch
,
277 struct type
*type
, const gdb_byte
*buf
)
279 enum bfd_endian byte_order
= gdbarch_byte_order (gdbarch
);
281 = extract_unsigned_integer (buf
, TYPE_LENGTH (type
), byte_order
);
283 if (TYPE_ADDRESS_CLASS_1 (type
))
286 return addr
| RAM_BIAS
;
289 /* Implementation of `address_class_type_flags' gdbarch method.
291 This method maps DW_AT_address_class attributes to a
292 type_instance_flag_value. */
295 ft32_address_class_type_flags (int byte_size
, int dwarf2_addr_class
)
297 /* The value 1 of the DW_AT_address_class attribute corresponds to the
298 __flash__ qualifier, meaning pointer to data in FT32 program memory.
300 if (dwarf2_addr_class
== 1)
301 return TYPE_INSTANCE_FLAG_ADDRESS_CLASS_1
;
305 /* Implementation of `address_class_type_flags_to_name' gdbarch method.
307 Convert a type_instance_flag_value to an address space qualifier. */
310 ft32_address_class_type_flags_to_name (struct gdbarch
*gdbarch
, int type_flags
)
312 if (type_flags
& TYPE_INSTANCE_FLAG_ADDRESS_CLASS_1
)
318 /* Implementation of `address_class_name_to_type_flags' gdbarch method.
320 Convert an address space qualifier to a type_instance_flag_value. */
323 ft32_address_class_name_to_type_flags (struct gdbarch
*gdbarch
,
327 if (strcmp (name
, "flash") == 0)
329 *type_flags_ptr
= TYPE_INSTANCE_FLAG_ADDRESS_CLASS_1
;
337 /* Implement the "read_pc" gdbarch method. */
340 ft32_read_pc (struct regcache
*regcache
)
344 regcache_cooked_read_unsigned (regcache
, FT32_PC_REGNUM
, &pc
);
348 /* Implement the "write_pc" gdbarch method. */
351 ft32_write_pc (struct regcache
*regcache
, CORE_ADDR val
)
353 regcache_cooked_write_unsigned (regcache
, FT32_PC_REGNUM
, val
);
356 /* Implement the "unwind_sp" gdbarch method. */
359 ft32_unwind_sp (struct gdbarch
*gdbarch
, struct frame_info
*next_frame
)
361 return frame_unwind_register_unsigned (next_frame
, FT32_SP_REGNUM
);
364 /* Given a return value in `regbuf' with a type `valtype',
365 extract and copy its value into `valbuf'. */
368 ft32_extract_return_value (struct type
*type
, struct regcache
*regcache
,
371 struct gdbarch
*gdbarch
= get_regcache_arch (regcache
);
372 enum bfd_endian byte_order
= gdbarch_byte_order (gdbarch
);
373 bfd_byte
*valbuf
= dst
;
374 int len
= TYPE_LENGTH (type
);
377 /* By using store_unsigned_integer we avoid having to do
378 anything special for small big-endian values. */
379 regcache_cooked_read_unsigned (regcache
, FT32_R0_REGNUM
, &tmp
);
380 store_unsigned_integer (valbuf
, (len
> 4 ? len
- 4 : len
), byte_order
, tmp
);
382 /* Ignore return values more than 8 bytes in size because the ft32
383 returns anything more than 8 bytes in the stack. */
386 regcache_cooked_read_unsigned (regcache
, FT32_R1_REGNUM
, &tmp
);
387 store_unsigned_integer (valbuf
+ len
- 4, 4, byte_order
, tmp
);
391 /* Implement the "return_value" gdbarch method. */
393 static enum return_value_convention
394 ft32_return_value (struct gdbarch
*gdbarch
, struct value
*function
,
395 struct type
*valtype
, struct regcache
*regcache
,
396 gdb_byte
*readbuf
, const gdb_byte
*writebuf
)
398 if (TYPE_LENGTH (valtype
) > 8)
399 return RETURN_VALUE_STRUCT_CONVENTION
;
403 ft32_extract_return_value (valtype
, regcache
, readbuf
);
404 if (writebuf
!= NULL
)
405 ft32_store_return_value (valtype
, regcache
, writebuf
);
406 return RETURN_VALUE_REGISTER_CONVENTION
;
410 /* Allocate and initialize a ft32_frame_cache object. */
412 static struct ft32_frame_cache
*
413 ft32_alloc_frame_cache (void)
415 struct ft32_frame_cache
*cache
;
418 cache
= FRAME_OBSTACK_ZALLOC (struct ft32_frame_cache
);
420 for (i
= 0; i
< FT32_NUM_REGS
; ++i
)
421 cache
->saved_regs
[i
] = REG_UNAVAIL
;
426 /* Populate a ft32_frame_cache object for this_frame. */
428 static struct ft32_frame_cache
*
429 ft32_frame_cache (struct frame_info
*this_frame
, void **this_cache
)
431 struct ft32_frame_cache
*cache
;
432 CORE_ADDR current_pc
;
436 return (struct ft32_frame_cache
*) *this_cache
;
438 cache
= ft32_alloc_frame_cache ();
441 cache
->base
= get_frame_register_unsigned (this_frame
, FT32_FP_REGNUM
);
442 if (cache
->base
== 0)
445 cache
->pc
= get_frame_func (this_frame
);
446 current_pc
= get_frame_pc (this_frame
);
449 struct gdbarch
*gdbarch
= get_frame_arch (this_frame
);
451 ft32_analyze_prologue (cache
->pc
, current_pc
, cache
, gdbarch
);
452 if (!cache
->established
)
453 cache
->base
= get_frame_register_unsigned (this_frame
, FT32_SP_REGNUM
);
456 cache
->saved_sp
= cache
->base
- 4;
458 for (i
= 0; i
< FT32_NUM_REGS
; ++i
)
459 if (cache
->saved_regs
[i
] != REG_UNAVAIL
)
460 cache
->saved_regs
[i
] = cache
->base
+ cache
->saved_regs
[i
];
465 /* Implement the "unwind_pc" gdbarch method. */
468 ft32_unwind_pc (struct gdbarch
*gdbarch
, struct frame_info
*next_frame
)
470 return frame_unwind_register_unsigned (next_frame
, FT32_PC_REGNUM
);
473 /* Given a GDB frame, determine the address of the calling function's
474 frame. This will be used to create a new GDB frame struct. */
477 ft32_frame_this_id (struct frame_info
*this_frame
,
478 void **this_prologue_cache
, struct frame_id
*this_id
)
480 struct ft32_frame_cache
*cache
= ft32_frame_cache (this_frame
,
481 this_prologue_cache
);
483 /* This marks the outermost frame. */
484 if (cache
->base
== 0)
487 *this_id
= frame_id_build (cache
->saved_sp
, cache
->pc
);
490 /* Get the value of register regnum in the previous stack frame. */
492 static struct value
*
493 ft32_frame_prev_register (struct frame_info
*this_frame
,
494 void **this_prologue_cache
, int regnum
)
496 struct ft32_frame_cache
*cache
= ft32_frame_cache (this_frame
,
497 this_prologue_cache
);
499 gdb_assert (regnum
>= 0);
501 if (regnum
== FT32_SP_REGNUM
&& cache
->saved_sp
)
502 return frame_unwind_got_constant (this_frame
, regnum
, cache
->saved_sp
);
504 if (regnum
< FT32_NUM_REGS
&& cache
->saved_regs
[regnum
] != REG_UNAVAIL
)
505 return frame_unwind_got_memory (this_frame
, regnum
,
506 RAM_BIAS
| cache
->saved_regs
[regnum
]);
508 return frame_unwind_got_register (this_frame
, regnum
, regnum
);
511 static const struct frame_unwind ft32_frame_unwind
=
514 default_frame_unwind_stop_reason
,
516 ft32_frame_prev_register
,
518 default_frame_sniffer
521 /* Return the base address of this_frame. */
524 ft32_frame_base_address (struct frame_info
*this_frame
, void **this_cache
)
526 struct ft32_frame_cache
*cache
= ft32_frame_cache (this_frame
,
532 static const struct frame_base ft32_frame_base
=
535 ft32_frame_base_address
,
536 ft32_frame_base_address
,
537 ft32_frame_base_address
540 static struct frame_id
541 ft32_dummy_id (struct gdbarch
*gdbarch
, struct frame_info
*this_frame
)
543 CORE_ADDR sp
= get_frame_register_unsigned (this_frame
, FT32_SP_REGNUM
);
545 return frame_id_build (sp
, get_frame_pc (this_frame
));
548 /* Allocate and initialize the ft32 gdbarch object. */
550 static struct gdbarch
*
551 ft32_gdbarch_init (struct gdbarch_info info
, struct gdbarch_list
*arches
)
553 struct gdbarch
*gdbarch
;
554 struct gdbarch_tdep
*tdep
;
555 struct type
*void_type
;
556 struct type
*func_void_type
;
558 /* If there is already a candidate, use it. */
559 arches
= gdbarch_list_lookup_by_info (arches
, &info
);
561 return arches
->gdbarch
;
563 /* Allocate space for the new architecture. */
564 tdep
= XNEW (struct gdbarch_tdep
);
565 gdbarch
= gdbarch_alloc (&info
, tdep
);
567 /* Create a type for PC. We can't use builtin types here, as they may not
569 void_type
= arch_type (gdbarch
, TYPE_CODE_VOID
, 1, "void");
570 func_void_type
= make_function_type (void_type
, NULL
);
571 tdep
->pc_type
= arch_type (gdbarch
, TYPE_CODE_PTR
, 4, NULL
);
572 TYPE_TARGET_TYPE (tdep
->pc_type
) = func_void_type
;
573 TYPE_UNSIGNED (tdep
->pc_type
) = 1;
574 TYPE_INSTANCE_FLAGS (tdep
->pc_type
) |= TYPE_INSTANCE_FLAG_ADDRESS_CLASS_1
;
576 set_gdbarch_read_pc (gdbarch
, ft32_read_pc
);
577 set_gdbarch_write_pc (gdbarch
, ft32_write_pc
);
578 set_gdbarch_unwind_sp (gdbarch
, ft32_unwind_sp
);
580 set_gdbarch_num_regs (gdbarch
, FT32_NUM_REGS
);
581 set_gdbarch_sp_regnum (gdbarch
, FT32_SP_REGNUM
);
582 set_gdbarch_pc_regnum (gdbarch
, FT32_PC_REGNUM
);
583 set_gdbarch_register_name (gdbarch
, ft32_register_name
);
584 set_gdbarch_register_type (gdbarch
, ft32_register_type
);
586 set_gdbarch_return_value (gdbarch
, ft32_return_value
);
588 set_gdbarch_pointer_to_address (gdbarch
, ft32_pointer_to_address
);
590 set_gdbarch_skip_prologue (gdbarch
, ft32_skip_prologue
);
591 set_gdbarch_inner_than (gdbarch
, core_addr_lessthan
);
592 set_gdbarch_breakpoint_from_pc (gdbarch
, ft32_breakpoint_from_pc
);
593 set_gdbarch_frame_align (gdbarch
, ft32_frame_align
);
595 frame_base_set_default (gdbarch
, &ft32_frame_base
);
597 /* Methods for saving / extracting a dummy frame's ID. The ID's
598 stack address must match the SP value returned by
599 PUSH_DUMMY_CALL, and saved by generic_save_dummy_frame_tos. */
600 set_gdbarch_dummy_id (gdbarch
, ft32_dummy_id
);
602 set_gdbarch_unwind_pc (gdbarch
, ft32_unwind_pc
);
604 set_gdbarch_print_insn (gdbarch
, print_insn_ft32
);
606 /* Hook in ABI-specific overrides, if they have been registered. */
607 gdbarch_init_osabi (info
, gdbarch
);
609 /* Hook in the default unwinders. */
610 frame_unwind_append_unwinder (gdbarch
, &ft32_frame_unwind
);
612 /* Support simple overlay manager. */
613 set_gdbarch_overlay_update (gdbarch
, simple_overlay_update
);
615 set_gdbarch_address_class_type_flags (gdbarch
, ft32_address_class_type_flags
);
616 set_gdbarch_address_class_name_to_type_flags
617 (gdbarch
, ft32_address_class_name_to_type_flags
);
618 set_gdbarch_address_class_type_flags_to_name
619 (gdbarch
, ft32_address_class_type_flags_to_name
);
624 /* Register this machine's init routine. */
627 _initialize_ft32_tdep (void)
629 register_gdbarch_init (bfd_arch_ft32
, ft32_gdbarch_init
);