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