Sort includes for files gdb/[a-f]*.[chyl].
[deliverable/binutils-gdb.git] / gdb / ft32-tdep.c
CommitLineData
49d45b20
JB
1/* Target-dependent code for FT32.
2
42a4f53d 3 Copyright (C) 2009-2019 Free Software Foundation, Inc.
49d45b20
JB
4
5 This file is part of GDB.
6
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.
11
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.
16
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/>. */
19
20#include "defs.h"
d55e5aa6
TT
21
22/* Standard C++ includes. */
23#include <algorithm>
24
25/* Local non-gdb includes. */
26#include "arch-utils.h"
27#include "dis-asm.h"
49d45b20 28#include "frame-base.h"
d55e5aa6
TT
29#include "frame-unwind.h"
30#include "frame.h"
31#include "ft32-tdep.h"
32#include "gdb/sim-ft32.h"
49d45b20
JB
33#include "gdbcmd.h"
34#include "gdbcore.h"
d55e5aa6 35#include "gdbtypes.h"
49d45b20 36#include "inferior.h"
d55e5aa6 37#include "language.h"
49d45b20 38#include "objfiles.h"
d55e5aa6 39#include "opcode/ft32.h"
49d45b20 40#include "osabi.h"
d55e5aa6 41#include "record.h"
49d45b20 42#include "regcache.h"
d55e5aa6
TT
43#include "symfile.h"
44#include "symtab.h"
49d45b20 45#include "trad-frame.h"
d55e5aa6 46#include "value.h"
49d45b20
JB
47
48#define RAM_BIAS 0x800000 /* Bias added to RAM addresses. */
49
49d45b20
JB
50/* Use an invalid address -1 as 'not available' marker. */
51enum { REG_UNAVAIL = (CORE_ADDR) (-1) };
52
53struct ft32_frame_cache
54{
55 /* Base address of the frame */
56 CORE_ADDR base;
57 /* Function this frame belongs to */
58 CORE_ADDR pc;
59 /* Total size of this frame */
60 LONGEST framesize;
61 /* Saved registers in this frame */
62 CORE_ADDR saved_regs[FT32_NUM_REGS];
63 /* Saved SP in this frame */
64 CORE_ADDR saved_sp;
65 /* Has the new frame been LINKed. */
66 bfd_boolean established;
67};
68
69/* Implement the "frame_align" gdbarch method. */
70
71static CORE_ADDR
72ft32_frame_align (struct gdbarch *gdbarch, CORE_ADDR sp)
73{
74 /* Align to the size of an instruction (so that they can safely be
75 pushed onto the stack. */
76 return sp & ~1;
77}
78
49d45b20 79
04180708 80constexpr gdb_byte ft32_break_insn[] = { 0x02, 0x00, 0x34, 0x00 };
49d45b20 81
04180708 82typedef BP_MANIPULATION (ft32_break_insn) ft32_breakpoint;
49d45b20
JB
83
84/* FT32 register names. */
85
86static const char *const ft32_register_names[] =
87{
88 "fp", "sp",
89 "r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7",
90 "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15",
91 "r16", "r17", "r18", "r19", "r20", "r21", "r22", "r23",
92 "r24", "r25", "r26", "r27", "r28", "cc",
93 "pc"
94};
95
96/* Implement the "register_name" gdbarch method. */
97
98static const char *
99ft32_register_name (struct gdbarch *gdbarch, int reg_nr)
100{
101 if (reg_nr < 0)
102 return NULL;
103 if (reg_nr >= FT32_NUM_REGS)
104 return NULL;
105 return ft32_register_names[reg_nr];
106}
107
108/* Implement the "register_type" gdbarch method. */
109
110static struct type *
111ft32_register_type (struct gdbarch *gdbarch, int reg_nr)
112{
113 if (reg_nr == FT32_PC_REGNUM)
623fb775 114 return gdbarch_tdep (gdbarch)->pc_type;
49d45b20
JB
115 else if (reg_nr == FT32_SP_REGNUM || reg_nr == FT32_FP_REGNUM)
116 return builtin_type (gdbarch)->builtin_data_ptr;
117 else
118 return builtin_type (gdbarch)->builtin_int32;
119}
120
121/* Write into appropriate registers a function return value
122 of type TYPE, given in virtual format. */
123
124static void
125ft32_store_return_value (struct type *type, struct regcache *regcache,
126 const gdb_byte *valbuf)
127{
ac7936df 128 struct gdbarch *gdbarch = regcache->arch ();
49d45b20
JB
129 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
130 CORE_ADDR regval;
131 int len = TYPE_LENGTH (type);
132
133 /* Things always get returned in RET1_REGNUM, RET2_REGNUM. */
134 regval = extract_unsigned_integer (valbuf, len > 4 ? 4 : len, byte_order);
135 regcache_cooked_write_unsigned (regcache, FT32_R0_REGNUM, regval);
136 if (len > 4)
137 {
138 regval = extract_unsigned_integer (valbuf + 4,
139 len - 4, byte_order);
140 regcache_cooked_write_unsigned (regcache, FT32_R1_REGNUM, regval);
141 }
142}
143
dcc31d28
JB
144/* Fetch a single 32-bit instruction from address a. If memory contains
145 a compressed instruction pair, return the expanded instruction. */
146
147static ULONGEST
148ft32_fetch_instruction (CORE_ADDR a, int *isize,
149 enum bfd_endian byte_order)
150{
151 unsigned int sc[2];
152 ULONGEST inst;
153
154 CORE_ADDR a4 = a & ~3;
155 inst = read_code_unsigned_integer (a4, 4, byte_order);
156 *isize = ft32_decode_shortcode (a4, inst, sc) ? 2 : 4;
157 if (*isize == 2)
158 return sc[1 & (a >> 1)];
159 else
160 return inst;
161}
162
49d45b20
JB
163/* Decode the instructions within the given address range. Decide
164 when we must have reached the end of the function prologue. If a
165 frame_info pointer is provided, fill in its saved_regs etc.
166
167 Returns the address of the first instruction after the prologue. */
168
49d45b20
JB
169static CORE_ADDR
170ft32_analyze_prologue (CORE_ADDR start_addr, CORE_ADDR end_addr,
171 struct ft32_frame_cache *cache,
172 struct gdbarch *gdbarch)
173{
174 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
175 CORE_ADDR next_addr;
870f88f7 176 ULONGEST inst;
dcc31d28 177 int isize = 0;
ae4e2501 178 int regnum, pushreg;
179 struct bound_minimal_symbol msymbol;
180 const int first_saved_reg = 13; /* The first saved register. */
181 /* PROLOGS are addresses of the subroutine prologs, PROLOGS[n]
182 is the address of __prolog_$rN.
183 __prolog_$rN pushes registers from 13 through n inclusive.
184 So for example CALL __prolog_$r15 is equivalent to:
185 PUSH $r13
186 PUSH $r14
187 PUSH $r15
188 Note that PROLOGS[0] through PROLOGS[12] are unused. */
189 CORE_ADDR prologs[32];
49d45b20
JB
190
191 cache->saved_regs[FT32_PC_REGNUM] = 0;
192 cache->framesize = 0;
193
ae4e2501 194 for (regnum = first_saved_reg; regnum < 32; regnum++)
195 {
196 char prolog_symbol[32];
197
198 snprintf (prolog_symbol, sizeof (prolog_symbol), "__prolog_$r%02d",
199 regnum);
200 msymbol = lookup_minimal_symbol (prolog_symbol, NULL, NULL);
201 if (msymbol.minsym)
202 prologs[regnum] = BMSYMBOL_VALUE_ADDRESS (msymbol);
203 else
204 prologs[regnum] = 0;
205 }
206
49d45b20 207 if (start_addr >= end_addr)
ae4e2501 208 return end_addr;
49d45b20
JB
209
210 cache->established = 0;
dcc31d28 211 for (next_addr = start_addr; next_addr < end_addr; next_addr += isize)
49d45b20 212 {
dcc31d28 213 inst = ft32_fetch_instruction (next_addr, &isize, byte_order);
49d45b20 214
86feccb9 215 if (FT32_IS_PUSH (inst))
49d45b20 216 {
ae4e2501 217 pushreg = FT32_PUSH_REG (inst);
49d45b20 218 cache->framesize += 4;
ae4e2501 219 cache->saved_regs[FT32_R0_REGNUM + pushreg] = cache->framesize;
49d45b20 220 }
ae4e2501 221 else if (FT32_IS_CALL (inst))
222 {
223 for (regnum = first_saved_reg; regnum < 32; regnum++)
224 {
225 if ((4 * (inst & 0x3ffff)) == prologs[regnum])
226 {
227 for (pushreg = first_saved_reg; pushreg <= regnum;
228 pushreg++)
229 {
230 cache->framesize += 4;
231 cache->saved_regs[FT32_R0_REGNUM + pushreg] =
232 cache->framesize;
233 }
ae4e2501 234 }
235 }
236 break;
237 }
49d45b20
JB
238 else
239 break;
240 }
241 for (regnum = FT32_R0_REGNUM; regnum < FT32_PC_REGNUM; regnum++)
242 {
243 if (cache->saved_regs[regnum] != REG_UNAVAIL)
ae4e2501 244 cache->saved_regs[regnum] =
245 cache->framesize - cache->saved_regs[regnum];
49d45b20
JB
246 }
247 cache->saved_regs[FT32_PC_REGNUM] = cache->framesize;
248
249 /* It is a LINK? */
250 if (next_addr < end_addr)
251 {
dcc31d28 252 inst = ft32_fetch_instruction (next_addr, &isize, byte_order);
86feccb9 253 if (FT32_IS_LINK (inst))
49d45b20
JB
254 {
255 cache->established = 1;
256 for (regnum = FT32_R0_REGNUM; regnum < FT32_PC_REGNUM; regnum++)
257 {
258 if (cache->saved_regs[regnum] != REG_UNAVAIL)
259 cache->saved_regs[regnum] += 4;
260 }
261 cache->saved_regs[FT32_PC_REGNUM] = cache->framesize + 4;
262 cache->saved_regs[FT32_FP_REGNUM] = 0;
86feccb9 263 cache->framesize += FT32_LINK_SIZE (inst);
dcc31d28 264 next_addr += isize;
49d45b20
JB
265 }
266 }
267
268 return next_addr;
269}
270
271/* Find the end of function prologue. */
272
273static CORE_ADDR
274ft32_skip_prologue (struct gdbarch *gdbarch, CORE_ADDR pc)
275{
276 CORE_ADDR func_addr = 0, func_end = 0;
277 const char *func_name;
278
279 /* See if we can determine the end of the prologue via the symbol table.
280 If so, then return either PC, or the PC after the prologue, whichever
281 is greater. */
282 if (find_pc_partial_function (pc, &func_name, &func_addr, &func_end))
283 {
284 CORE_ADDR post_prologue_pc
285 = skip_prologue_using_sal (gdbarch, func_addr);
286 if (post_prologue_pc != 0)
325fac50 287 return std::max (pc, post_prologue_pc);
49d45b20
JB
288 else
289 {
290 /* Can't determine prologue from the symbol table, need to examine
291 instructions. */
292 struct symtab_and_line sal;
293 struct symbol *sym;
294 struct ft32_frame_cache cache;
295 CORE_ADDR plg_end;
296
297 memset (&cache, 0, sizeof cache);
298
299 plg_end = ft32_analyze_prologue (func_addr,
300 func_end, &cache, gdbarch);
301 /* Found a function. */
835a09d9 302 sym = lookup_symbol (func_name, NULL, VAR_DOMAIN, NULL).symbol;
49d45b20
JB
303 /* Don't use line number debug info for assembly source files. */
304 if ((sym != NULL) && SYMBOL_LANGUAGE (sym) != language_asm)
305 {
306 sal = find_pc_line (func_addr, 0);
307 if (sal.end && sal.end < func_end)
308 {
309 /* Found a line number, use it as end of prologue. */
310 return sal.end;
311 }
312 }
313 /* No useable line symbol. Use result of prologue parsing method. */
314 return plg_end;
315 }
316 }
317
318 /* No function symbol -- just return the PC. */
319 return pc;
320}
321
623fb775 322/* Implementation of `pointer_to_address' gdbarch method.
323
324 On FT32 address space zero is RAM, address space 1 is flash.
325 RAM appears at address RAM_BIAS, flash at address 0. */
326
327static CORE_ADDR
328ft32_pointer_to_address (struct gdbarch *gdbarch,
329 struct type *type, const gdb_byte *buf)
330{
331 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
332 CORE_ADDR addr
333 = extract_unsigned_integer (buf, TYPE_LENGTH (type), byte_order);
334
335 if (TYPE_ADDRESS_CLASS_1 (type))
336 return addr;
337 else
338 return addr | RAM_BIAS;
339}
340
341/* Implementation of `address_class_type_flags' gdbarch method.
342
343 This method maps DW_AT_address_class attributes to a
344 type_instance_flag_value. */
345
346static int
347ft32_address_class_type_flags (int byte_size, int dwarf2_addr_class)
348{
349 /* The value 1 of the DW_AT_address_class attribute corresponds to the
350 __flash__ qualifier, meaning pointer to data in FT32 program memory.
351 */
352 if (dwarf2_addr_class == 1)
353 return TYPE_INSTANCE_FLAG_ADDRESS_CLASS_1;
354 return 0;
355}
356
357/* Implementation of `address_class_type_flags_to_name' gdbarch method.
358
359 Convert a type_instance_flag_value to an address space qualifier. */
360
361static const char*
362ft32_address_class_type_flags_to_name (struct gdbarch *gdbarch, int type_flags)
363{
364 if (type_flags & TYPE_INSTANCE_FLAG_ADDRESS_CLASS_1)
365 return "flash";
366 else
367 return NULL;
368}
369
370/* Implementation of `address_class_name_to_type_flags' gdbarch method.
371
372 Convert an address space qualifier to a type_instance_flag_value. */
373
374static int
375ft32_address_class_name_to_type_flags (struct gdbarch *gdbarch,
376 const char* name,
377 int *type_flags_ptr)
378{
379 if (strcmp (name, "flash") == 0)
380 {
381 *type_flags_ptr = TYPE_INSTANCE_FLAG_ADDRESS_CLASS_1;
382 return 1;
383 }
384 else
385 return 0;
386}
387
49d45b20
JB
388/* Given a return value in `regbuf' with a type `valtype',
389 extract and copy its value into `valbuf'. */
390
391static void
392ft32_extract_return_value (struct type *type, struct regcache *regcache,
393 gdb_byte *dst)
394{
ac7936df 395 struct gdbarch *gdbarch = regcache->arch ();
49d45b20
JB
396 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
397 bfd_byte *valbuf = dst;
398 int len = TYPE_LENGTH (type);
399 ULONGEST tmp;
400
401 /* By using store_unsigned_integer we avoid having to do
402 anything special for small big-endian values. */
403 regcache_cooked_read_unsigned (regcache, FT32_R0_REGNUM, &tmp);
404 store_unsigned_integer (valbuf, (len > 4 ? len - 4 : len), byte_order, tmp);
405
406 /* Ignore return values more than 8 bytes in size because the ft32
407 returns anything more than 8 bytes in the stack. */
408 if (len > 4)
409 {
410 regcache_cooked_read_unsigned (regcache, FT32_R1_REGNUM, &tmp);
411 store_unsigned_integer (valbuf + len - 4, 4, byte_order, tmp);
412 }
413}
414
415/* Implement the "return_value" gdbarch method. */
416
417static enum return_value_convention
418ft32_return_value (struct gdbarch *gdbarch, struct value *function,
419 struct type *valtype, struct regcache *regcache,
420 gdb_byte *readbuf, const gdb_byte *writebuf)
421{
422 if (TYPE_LENGTH (valtype) > 8)
423 return RETURN_VALUE_STRUCT_CONVENTION;
424 else
425 {
426 if (readbuf != NULL)
427 ft32_extract_return_value (valtype, regcache, readbuf);
428 if (writebuf != NULL)
429 ft32_store_return_value (valtype, regcache, writebuf);
430 return RETURN_VALUE_REGISTER_CONVENTION;
431 }
432}
433
434/* Allocate and initialize a ft32_frame_cache object. */
435
436static struct ft32_frame_cache *
437ft32_alloc_frame_cache (void)
438{
439 struct ft32_frame_cache *cache;
440 int i;
441
442 cache = FRAME_OBSTACK_ZALLOC (struct ft32_frame_cache);
443
444 for (i = 0; i < FT32_NUM_REGS; ++i)
445 cache->saved_regs[i] = REG_UNAVAIL;
446
447 return cache;
448}
449
450/* Populate a ft32_frame_cache object for this_frame. */
451
452static struct ft32_frame_cache *
453ft32_frame_cache (struct frame_info *this_frame, void **this_cache)
454{
455 struct ft32_frame_cache *cache;
456 CORE_ADDR current_pc;
457 int i;
458
459 if (*this_cache)
9a3c8263 460 return (struct ft32_frame_cache *) *this_cache;
49d45b20
JB
461
462 cache = ft32_alloc_frame_cache ();
463 *this_cache = cache;
464
465 cache->base = get_frame_register_unsigned (this_frame, FT32_FP_REGNUM);
466 if (cache->base == 0)
467 return cache;
468
469 cache->pc = get_frame_func (this_frame);
470 current_pc = get_frame_pc (this_frame);
471 if (cache->pc)
472 {
473 struct gdbarch *gdbarch = get_frame_arch (this_frame);
474
475 ft32_analyze_prologue (cache->pc, current_pc, cache, gdbarch);
476 if (!cache->established)
477 cache->base = get_frame_register_unsigned (this_frame, FT32_SP_REGNUM);
478 }
479
480 cache->saved_sp = cache->base - 4;
481
482 for (i = 0; i < FT32_NUM_REGS; ++i)
483 if (cache->saved_regs[i] != REG_UNAVAIL)
484 cache->saved_regs[i] = cache->base + cache->saved_regs[i];
485
486 return cache;
487}
488
49d45b20
JB
489/* Given a GDB frame, determine the address of the calling function's
490 frame. This will be used to create a new GDB frame struct. */
491
492static void
493ft32_frame_this_id (struct frame_info *this_frame,
494 void **this_prologue_cache, struct frame_id *this_id)
495{
496 struct ft32_frame_cache *cache = ft32_frame_cache (this_frame,
497 this_prologue_cache);
498
499 /* This marks the outermost frame. */
500 if (cache->base == 0)
501 return;
502
503 *this_id = frame_id_build (cache->saved_sp, cache->pc);
504}
505
506/* Get the value of register regnum in the previous stack frame. */
507
508static struct value *
509ft32_frame_prev_register (struct frame_info *this_frame,
510 void **this_prologue_cache, int regnum)
511{
512 struct ft32_frame_cache *cache = ft32_frame_cache (this_frame,
513 this_prologue_cache);
514
515 gdb_assert (regnum >= 0);
516
517 if (regnum == FT32_SP_REGNUM && cache->saved_sp)
518 return frame_unwind_got_constant (this_frame, regnum, cache->saved_sp);
519
520 if (regnum < FT32_NUM_REGS && cache->saved_regs[regnum] != REG_UNAVAIL)
521 return frame_unwind_got_memory (this_frame, regnum,
522 RAM_BIAS | cache->saved_regs[regnum]);
523
524 return frame_unwind_got_register (this_frame, regnum, regnum);
525}
526
527static const struct frame_unwind ft32_frame_unwind =
528{
529 NORMAL_FRAME,
530 default_frame_unwind_stop_reason,
531 ft32_frame_this_id,
532 ft32_frame_prev_register,
533 NULL,
534 default_frame_sniffer
535};
536
537/* Return the base address of this_frame. */
538
539static CORE_ADDR
540ft32_frame_base_address (struct frame_info *this_frame, void **this_cache)
541{
542 struct ft32_frame_cache *cache = ft32_frame_cache (this_frame,
543 this_cache);
544
545 return cache->base;
546}
547
548static const struct frame_base ft32_frame_base =
549{
550 &ft32_frame_unwind,
551 ft32_frame_base_address,
552 ft32_frame_base_address,
553 ft32_frame_base_address
554};
555
49d45b20
JB
556/* Allocate and initialize the ft32 gdbarch object. */
557
558static struct gdbarch *
559ft32_gdbarch_init (struct gdbarch_info info, struct gdbarch_list *arches)
560{
561 struct gdbarch *gdbarch;
562 struct gdbarch_tdep *tdep;
623fb775 563 struct type *void_type;
564 struct type *func_void_type;
49d45b20
JB
565
566 /* If there is already a candidate, use it. */
567 arches = gdbarch_list_lookup_by_info (arches, &info);
568 if (arches != NULL)
569 return arches->gdbarch;
570
571 /* Allocate space for the new architecture. */
cdd238da 572 tdep = XCNEW (struct gdbarch_tdep);
49d45b20
JB
573 gdbarch = gdbarch_alloc (&info, tdep);
574
623fb775 575 /* Create a type for PC. We can't use builtin types here, as they may not
576 be defined. */
77b7c781 577 void_type = arch_type (gdbarch, TYPE_CODE_VOID, TARGET_CHAR_BIT, "void");
623fb775 578 func_void_type = make_function_type (void_type, NULL);
88dfca6c
UW
579 tdep->pc_type = arch_pointer_type (gdbarch, 4 * TARGET_CHAR_BIT, NULL,
580 func_void_type);
623fb775 581 TYPE_INSTANCE_FLAGS (tdep->pc_type) |= TYPE_INSTANCE_FLAG_ADDRESS_CLASS_1;
582
49d45b20
JB
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);
588
589 set_gdbarch_return_value (gdbarch, ft32_return_value);
590
623fb775 591 set_gdbarch_pointer_to_address (gdbarch, ft32_pointer_to_address);
592
49d45b20
JB
593 set_gdbarch_skip_prologue (gdbarch, ft32_skip_prologue);
594 set_gdbarch_inner_than (gdbarch, core_addr_lessthan);
04180708
YQ
595 set_gdbarch_breakpoint_kind_from_pc (gdbarch, ft32_breakpoint::kind_from_pc);
596 set_gdbarch_sw_breakpoint_from_kind (gdbarch, ft32_breakpoint::bp_from_kind);
49d45b20
JB
597 set_gdbarch_frame_align (gdbarch, ft32_frame_align);
598
599 frame_base_set_default (gdbarch, &ft32_frame_base);
600
49d45b20
JB
601 /* Hook in ABI-specific overrides, if they have been registered. */
602 gdbarch_init_osabi (info, gdbarch);
603
604 /* Hook in the default unwinders. */
605 frame_unwind_append_unwinder (gdbarch, &ft32_frame_unwind);
606
607 /* Support simple overlay manager. */
608 set_gdbarch_overlay_update (gdbarch, simple_overlay_update);
609
623fb775 610 set_gdbarch_address_class_type_flags (gdbarch, ft32_address_class_type_flags);
611 set_gdbarch_address_class_name_to_type_flags
612 (gdbarch, ft32_address_class_name_to_type_flags);
613 set_gdbarch_address_class_type_flags_to_name
614 (gdbarch, ft32_address_class_type_flags_to_name);
615
49d45b20
JB
616 return gdbarch;
617}
618
619/* Register this machine's init routine. */
620
621void
622_initialize_ft32_tdep (void)
623{
624 register_gdbarch_init (bfd_arch_ft32, ft32_gdbarch_init);
625}
This page took 0.377989 seconds and 4 git commands to generate.