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 gdbarch_tdep (gdbarch)->pc_type;
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).symbol;
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 /* Implementation of `pointer_to_address' gdbarch method.
275 On FT32 address space zero is RAM, address space 1 is flash.
276 RAM appears at address RAM_BIAS, flash at address 0. */
279 ft32_pointer_to_address (struct gdbarch *gdbarch,
280 struct type *type, const gdb_byte *buf)
282 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
284 = extract_unsigned_integer (buf, TYPE_LENGTH (type), byte_order);
286 if (TYPE_ADDRESS_CLASS_1 (type))
289 return addr | RAM_BIAS;
292 /* Implementation of `address_class_type_flags' gdbarch method.
294 This method maps DW_AT_address_class attributes to a
295 type_instance_flag_value. */
298 ft32_address_class_type_flags (int byte_size, int dwarf2_addr_class)
300 /* The value 1 of the DW_AT_address_class attribute corresponds to the
301 __flash__ qualifier, meaning pointer to data in FT32 program memory.
303 if (dwarf2_addr_class == 1)
304 return TYPE_INSTANCE_FLAG_ADDRESS_CLASS_1;
308 /* Implementation of `address_class_type_flags_to_name' gdbarch method.
310 Convert a type_instance_flag_value to an address space qualifier. */
313 ft32_address_class_type_flags_to_name (struct gdbarch *gdbarch, int type_flags)
315 if (type_flags & TYPE_INSTANCE_FLAG_ADDRESS_CLASS_1)
321 /* Implementation of `address_class_name_to_type_flags' gdbarch method.
323 Convert an address space qualifier to a type_instance_flag_value. */
326 ft32_address_class_name_to_type_flags (struct gdbarch *gdbarch,
330 if (strcmp (name, "flash") == 0)
332 *type_flags_ptr = TYPE_INSTANCE_FLAG_ADDRESS_CLASS_1;
340 /* Implement the "read_pc" gdbarch method. */
343 ft32_read_pc (struct regcache *regcache)
347 regcache_cooked_read_unsigned (regcache, FT32_PC_REGNUM, &pc);
351 /* Implement the "write_pc" gdbarch method. */
354 ft32_write_pc (struct regcache *regcache, CORE_ADDR val)
356 regcache_cooked_write_unsigned (regcache, FT32_PC_REGNUM, val);
359 /* Implement the "unwind_sp" gdbarch method. */
362 ft32_unwind_sp (struct gdbarch *gdbarch, struct frame_info *next_frame)
364 return frame_unwind_register_unsigned (next_frame, FT32_SP_REGNUM);
367 /* Given a return value in `regbuf' with a type `valtype',
368 extract and copy its value into `valbuf'. */
371 ft32_extract_return_value (struct type *type, struct regcache *regcache,
374 struct gdbarch *gdbarch = get_regcache_arch (regcache);
375 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
376 bfd_byte *valbuf = dst;
377 int len = TYPE_LENGTH (type);
380 /* By using store_unsigned_integer we avoid having to do
381 anything special for small big-endian values. */
382 regcache_cooked_read_unsigned (regcache, FT32_R0_REGNUM, &tmp);
383 store_unsigned_integer (valbuf, (len > 4 ? len - 4 : len), byte_order, tmp);
385 /* Ignore return values more than 8 bytes in size because the ft32
386 returns anything more than 8 bytes in the stack. */
389 regcache_cooked_read_unsigned (regcache, FT32_R1_REGNUM, &tmp);
390 store_unsigned_integer (valbuf + len - 4, 4, byte_order, tmp);
394 /* Implement the "return_value" gdbarch method. */
396 static enum return_value_convention
397 ft32_return_value (struct gdbarch *gdbarch, struct value *function,
398 struct type *valtype, struct regcache *regcache,
399 gdb_byte *readbuf, const gdb_byte *writebuf)
401 if (TYPE_LENGTH (valtype) > 8)
402 return RETURN_VALUE_STRUCT_CONVENTION;
406 ft32_extract_return_value (valtype, regcache, readbuf);
407 if (writebuf != NULL)
408 ft32_store_return_value (valtype, regcache, writebuf);
409 return RETURN_VALUE_REGISTER_CONVENTION;
413 /* Allocate and initialize a ft32_frame_cache object. */
415 static struct ft32_frame_cache *
416 ft32_alloc_frame_cache (void)
418 struct ft32_frame_cache *cache;
421 cache = FRAME_OBSTACK_ZALLOC (struct ft32_frame_cache);
423 for (i = 0; i < FT32_NUM_REGS; ++i)
424 cache->saved_regs[i] = REG_UNAVAIL;
429 /* Populate a ft32_frame_cache object for this_frame. */
431 static struct ft32_frame_cache *
432 ft32_frame_cache (struct frame_info *this_frame, void **this_cache)
434 struct ft32_frame_cache *cache;
435 CORE_ADDR current_pc;
439 return (struct ft32_frame_cache *) *this_cache;
441 cache = ft32_alloc_frame_cache ();
444 cache->base = get_frame_register_unsigned (this_frame, FT32_FP_REGNUM);
445 if (cache->base == 0)
448 cache->pc = get_frame_func (this_frame);
449 current_pc = get_frame_pc (this_frame);
452 struct gdbarch *gdbarch = get_frame_arch (this_frame);
454 ft32_analyze_prologue (cache->pc, current_pc, cache, gdbarch);
455 if (!cache->established)
456 cache->base = get_frame_register_unsigned (this_frame, FT32_SP_REGNUM);
459 cache->saved_sp = cache->base - 4;
461 for (i = 0; i < FT32_NUM_REGS; ++i)
462 if (cache->saved_regs[i] != REG_UNAVAIL)
463 cache->saved_regs[i] = cache->base + cache->saved_regs[i];
468 /* Implement the "unwind_pc" gdbarch method. */
471 ft32_unwind_pc (struct gdbarch *gdbarch, struct frame_info *next_frame)
473 return frame_unwind_register_unsigned (next_frame, FT32_PC_REGNUM);
476 /* Given a GDB frame, determine the address of the calling function's
477 frame. This will be used to create a new GDB frame struct. */
480 ft32_frame_this_id (struct frame_info *this_frame,
481 void **this_prologue_cache, struct frame_id *this_id)
483 struct ft32_frame_cache *cache = ft32_frame_cache (this_frame,
484 this_prologue_cache);
486 /* This marks the outermost frame. */
487 if (cache->base == 0)
490 *this_id = frame_id_build (cache->saved_sp, cache->pc);
493 /* Get the value of register regnum in the previous stack frame. */
495 static struct value *
496 ft32_frame_prev_register (struct frame_info *this_frame,
497 void **this_prologue_cache, int regnum)
499 struct ft32_frame_cache *cache = ft32_frame_cache (this_frame,
500 this_prologue_cache);
502 gdb_assert (regnum >= 0);
504 if (regnum == FT32_SP_REGNUM && cache->saved_sp)
505 return frame_unwind_got_constant (this_frame, regnum, cache->saved_sp);
507 if (regnum < FT32_NUM_REGS && cache->saved_regs[regnum] != REG_UNAVAIL)
508 return frame_unwind_got_memory (this_frame, regnum,
509 RAM_BIAS | cache->saved_regs[regnum]);
511 return frame_unwind_got_register (this_frame, regnum, regnum);
514 static const struct frame_unwind ft32_frame_unwind =
517 default_frame_unwind_stop_reason,
519 ft32_frame_prev_register,
521 default_frame_sniffer
524 /* Return the base address of this_frame. */
527 ft32_frame_base_address (struct frame_info *this_frame, void **this_cache)
529 struct ft32_frame_cache *cache = ft32_frame_cache (this_frame,
535 static const struct frame_base ft32_frame_base =
538 ft32_frame_base_address,
539 ft32_frame_base_address,
540 ft32_frame_base_address
543 static struct frame_id
544 ft32_dummy_id (struct gdbarch *gdbarch, struct frame_info *this_frame)
546 CORE_ADDR sp = get_frame_register_unsigned (this_frame, FT32_SP_REGNUM);
548 return frame_id_build (sp, get_frame_pc (this_frame));
551 /* Allocate and initialize the ft32 gdbarch object. */
553 static struct gdbarch *
554 ft32_gdbarch_init (struct gdbarch_info info, struct gdbarch_list *arches)
556 struct gdbarch *gdbarch;
557 struct gdbarch_tdep *tdep;
558 struct type *void_type;
559 struct type *func_void_type;
561 /* If there is already a candidate, use it. */
562 arches = gdbarch_list_lookup_by_info (arches, &info);
564 return arches->gdbarch;
566 /* Allocate space for the new architecture. */
567 tdep = XNEW (struct gdbarch_tdep);
568 gdbarch = gdbarch_alloc (&info, tdep);
570 /* Create a type for PC. We can't use builtin types here, as they may not
572 void_type = arch_type (gdbarch, TYPE_CODE_VOID, 1, "void");
573 func_void_type = make_function_type (void_type, NULL);
574 tdep->pc_type = arch_type (gdbarch, TYPE_CODE_PTR, 4, NULL);
575 TYPE_TARGET_TYPE (tdep->pc_type) = func_void_type;
576 TYPE_UNSIGNED (tdep->pc_type) = 1;
577 TYPE_INSTANCE_FLAGS (tdep->pc_type) |= TYPE_INSTANCE_FLAG_ADDRESS_CLASS_1;
579 set_gdbarch_read_pc (gdbarch, ft32_read_pc);
580 set_gdbarch_write_pc (gdbarch, ft32_write_pc);
581 set_gdbarch_unwind_sp (gdbarch, ft32_unwind_sp);
583 set_gdbarch_num_regs (gdbarch, FT32_NUM_REGS);
584 set_gdbarch_sp_regnum (gdbarch, FT32_SP_REGNUM);
585 set_gdbarch_pc_regnum (gdbarch, FT32_PC_REGNUM);
586 set_gdbarch_register_name (gdbarch, ft32_register_name);
587 set_gdbarch_register_type (gdbarch, ft32_register_type);
589 set_gdbarch_return_value (gdbarch, ft32_return_value);
591 set_gdbarch_pointer_to_address (gdbarch, ft32_pointer_to_address);
593 set_gdbarch_skip_prologue (gdbarch, ft32_skip_prologue);
594 set_gdbarch_inner_than (gdbarch, core_addr_lessthan);
595 set_gdbarch_breakpoint_from_pc (gdbarch, ft32_breakpoint_from_pc);
596 set_gdbarch_frame_align (gdbarch, ft32_frame_align);
598 frame_base_set_default (gdbarch, &ft32_frame_base);
600 /* Methods for saving / extracting a dummy frame's ID. The ID's
601 stack address must match the SP value returned by
602 PUSH_DUMMY_CALL, and saved by generic_save_dummy_frame_tos. */
603 set_gdbarch_dummy_id (gdbarch, ft32_dummy_id);
605 set_gdbarch_unwind_pc (gdbarch, ft32_unwind_pc);
607 set_gdbarch_print_insn (gdbarch, print_insn_ft32);
609 /* Hook in ABI-specific overrides, if they have been registered. */
610 gdbarch_init_osabi (info, gdbarch);
612 /* Hook in the default unwinders. */
613 frame_unwind_append_unwinder (gdbarch, &ft32_frame_unwind);
615 /* Support simple overlay manager. */
616 set_gdbarch_overlay_update (gdbarch, simple_overlay_update);
618 set_gdbarch_address_class_type_flags (gdbarch, ft32_address_class_type_flags);
619 set_gdbarch_address_class_name_to_type_flags
620 (gdbarch, ft32_address_class_name_to_type_flags);
621 set_gdbarch_address_class_type_flags_to_name
622 (gdbarch, ft32_address_class_type_flags_to_name);
627 /* Register this machine's init routine. */
630 _initialize_ft32_tdep (void)
632 register_gdbarch_init (bfd_arch_ft32, ft32_gdbarch_init);