sim: ft32: correct simulation of MEMCPY and MEMSET
[deliverable/binutils-gdb.git] / gdb / ft32-tdep.c
CommitLineData
49d45b20
JB
1/* Target-dependent code for FT32.
2
3 Copyright (C) 2009-2015 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
86feccb9 40#include "opcode/ft32.h"
41
49d45b20
JB
42#include "ft32-tdep.h"
43#include "gdb/sim-ft32.h"
44
45#define RAM_BIAS 0x800000 /* Bias added to RAM addresses. */
46
47/* Local functions. */
48
49extern void _initialize_ft32_tdep (void);
50
51/* Use an invalid address -1 as 'not available' marker. */
52enum { REG_UNAVAIL = (CORE_ADDR) (-1) };
53
54struct ft32_frame_cache
55{
56 /* Base address of the frame */
57 CORE_ADDR base;
58 /* Function this frame belongs to */
59 CORE_ADDR pc;
60 /* Total size of this frame */
61 LONGEST framesize;
62 /* Saved registers in this frame */
63 CORE_ADDR saved_regs[FT32_NUM_REGS];
64 /* Saved SP in this frame */
65 CORE_ADDR saved_sp;
66 /* Has the new frame been LINKed. */
67 bfd_boolean established;
68};
69
70/* Implement the "frame_align" gdbarch method. */
71
72static CORE_ADDR
73ft32_frame_align (struct gdbarch *gdbarch, CORE_ADDR sp)
74{
75 /* Align to the size of an instruction (so that they can safely be
76 pushed onto the stack. */
77 return sp & ~1;
78}
79
80/* Implement the "breakpoint_from_pc" gdbarch method. */
81
82static const unsigned char *
83ft32_breakpoint_from_pc (struct gdbarch *gdbarch,
84 CORE_ADDR *pcptr, int *lenptr)
85{
86 static const gdb_byte breakpoint[] = { 0x02, 0x00, 0x34, 0x00 };
87
88 *lenptr = sizeof (breakpoint);
89 return breakpoint;
90}
91
92/* FT32 register names. */
93
94static const char *const ft32_register_names[] =
95{
96 "fp", "sp",
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",
101 "pc"
102};
103
104/* Implement the "register_name" gdbarch method. */
105
106static const char *
107ft32_register_name (struct gdbarch *gdbarch, int reg_nr)
108{
109 if (reg_nr < 0)
110 return NULL;
111 if (reg_nr >= FT32_NUM_REGS)
112 return NULL;
113 return ft32_register_names[reg_nr];
114}
115
116/* Implement the "register_type" gdbarch method. */
117
118static struct type *
119ft32_register_type (struct gdbarch *gdbarch, int reg_nr)
120{
121 if (reg_nr == FT32_PC_REGNUM)
623fb775 122 return gdbarch_tdep (gdbarch)->pc_type;
49d45b20
JB
123 else if (reg_nr == FT32_SP_REGNUM || reg_nr == FT32_FP_REGNUM)
124 return builtin_type (gdbarch)->builtin_data_ptr;
125 else
126 return builtin_type (gdbarch)->builtin_int32;
127}
128
129/* Write into appropriate registers a function return value
130 of type TYPE, given in virtual format. */
131
132static void
133ft32_store_return_value (struct type *type, struct regcache *regcache,
134 const gdb_byte *valbuf)
135{
136 struct gdbarch *gdbarch = get_regcache_arch (regcache);
137 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
138 CORE_ADDR regval;
139 int len = TYPE_LENGTH (type);
140
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);
144 if (len > 4)
145 {
146 regval = extract_unsigned_integer (valbuf + 4,
147 len - 4, byte_order);
148 regcache_cooked_write_unsigned (regcache, FT32_R1_REGNUM, regval);
149 }
150}
151
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.
155
156 Returns the address of the first instruction after the prologue. */
157
49d45b20
JB
158static CORE_ADDR
159ft32_analyze_prologue (CORE_ADDR start_addr, CORE_ADDR end_addr,
160 struct ft32_frame_cache *cache,
161 struct gdbarch *gdbarch)
162{
163 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
164 CORE_ADDR next_addr;
165 ULONGEST inst, inst2;
166 LONGEST offset;
0bcf3dd6 167 int regnum;
49d45b20
JB
168
169 cache->saved_regs[FT32_PC_REGNUM] = 0;
170 cache->framesize = 0;
171
172 if (start_addr >= end_addr)
0bcf3dd6 173 return end_addr;
49d45b20
JB
174
175 cache->established = 0;
0bcf3dd6 176 for (next_addr = start_addr; next_addr < end_addr; )
49d45b20
JB
177 {
178 inst = read_memory_unsigned_integer (next_addr, 4, byte_order);
179
86feccb9 180 if (FT32_IS_PUSH (inst))
49d45b20 181 {
0bcf3dd6 182 regnum = FT32_R0_REGNUM + FT32_PUSH_REG (inst);
49d45b20 183 cache->framesize += 4;
0bcf3dd6 184 cache->saved_regs[regnum] = cache->framesize;
49d45b20
JB
185 next_addr += 4;
186 }
187 else
188 break;
189 }
190 for (regnum = FT32_R0_REGNUM; regnum < FT32_PC_REGNUM; regnum++)
191 {
192 if (cache->saved_regs[regnum] != REG_UNAVAIL)
0bcf3dd6 193 cache->saved_regs[regnum] = cache->framesize - cache->saved_regs[regnum];
49d45b20
JB
194 }
195 cache->saved_regs[FT32_PC_REGNUM] = cache->framesize;
196
197 /* It is a LINK? */
198 if (next_addr < end_addr)
199 {
200 inst = read_memory_unsigned_integer (next_addr, 4, byte_order);
86feccb9 201 if (FT32_IS_LINK (inst))
49d45b20
JB
202 {
203 cache->established = 1;
204 for (regnum = FT32_R0_REGNUM; regnum < FT32_PC_REGNUM; regnum++)
205 {
206 if (cache->saved_regs[regnum] != REG_UNAVAIL)
207 cache->saved_regs[regnum] += 4;
208 }
209 cache->saved_regs[FT32_PC_REGNUM] = cache->framesize + 4;
210 cache->saved_regs[FT32_FP_REGNUM] = 0;
86feccb9 211 cache->framesize += FT32_LINK_SIZE (inst);
49d45b20
JB
212 next_addr += 4;
213 }
214 }
215
216 return next_addr;
217}
218
219/* Find the end of function prologue. */
220
221static CORE_ADDR
222ft32_skip_prologue (struct gdbarch *gdbarch, CORE_ADDR pc)
223{
224 CORE_ADDR func_addr = 0, func_end = 0;
225 const char *func_name;
226
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
229 is greater. */
230 if (find_pc_partial_function (pc, &func_name, &func_addr, &func_end))
231 {
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);
236 else
237 {
238 /* Can't determine prologue from the symbol table, need to examine
239 instructions. */
240 struct symtab_and_line sal;
241 struct symbol *sym;
242 struct ft32_frame_cache cache;
243 CORE_ADDR plg_end;
244
245 memset (&cache, 0, sizeof cache);
246
247 plg_end = ft32_analyze_prologue (func_addr,
248 func_end, &cache, gdbarch);
249 /* Found a function. */
835a09d9 250 sym = lookup_symbol (func_name, NULL, VAR_DOMAIN, NULL).symbol;
49d45b20
JB
251 /* Don't use line number debug info for assembly source files. */
252 if ((sym != NULL) && SYMBOL_LANGUAGE (sym) != language_asm)
253 {
254 sal = find_pc_line (func_addr, 0);
255 if (sal.end && sal.end < func_end)
256 {
257 /* Found a line number, use it as end of prologue. */
258 return sal.end;
259 }
260 }
261 /* No useable line symbol. Use result of prologue parsing method. */
262 return plg_end;
263 }
264 }
265
266 /* No function symbol -- just return the PC. */
267 return pc;
268}
269
623fb775 270/* Implementation of `pointer_to_address' gdbarch method.
271
272 On FT32 address space zero is RAM, address space 1 is flash.
273 RAM appears at address RAM_BIAS, flash at address 0. */
274
275static CORE_ADDR
276ft32_pointer_to_address (struct gdbarch *gdbarch,
277 struct type *type, const gdb_byte *buf)
278{
279 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
280 CORE_ADDR addr
281 = extract_unsigned_integer (buf, TYPE_LENGTH (type), byte_order);
282
283 if (TYPE_ADDRESS_CLASS_1 (type))
284 return addr;
285 else
286 return addr | RAM_BIAS;
287}
288
289/* Implementation of `address_class_type_flags' gdbarch method.
290
291 This method maps DW_AT_address_class attributes to a
292 type_instance_flag_value. */
293
294static int
295ft32_address_class_type_flags (int byte_size, int dwarf2_addr_class)
296{
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.
299 */
300 if (dwarf2_addr_class == 1)
301 return TYPE_INSTANCE_FLAG_ADDRESS_CLASS_1;
302 return 0;
303}
304
305/* Implementation of `address_class_type_flags_to_name' gdbarch method.
306
307 Convert a type_instance_flag_value to an address space qualifier. */
308
309static const char*
310ft32_address_class_type_flags_to_name (struct gdbarch *gdbarch, int type_flags)
311{
312 if (type_flags & TYPE_INSTANCE_FLAG_ADDRESS_CLASS_1)
313 return "flash";
314 else
315 return NULL;
316}
317
318/* Implementation of `address_class_name_to_type_flags' gdbarch method.
319
320 Convert an address space qualifier to a type_instance_flag_value. */
321
322static int
323ft32_address_class_name_to_type_flags (struct gdbarch *gdbarch,
324 const char* name,
325 int *type_flags_ptr)
326{
327 if (strcmp (name, "flash") == 0)
328 {
329 *type_flags_ptr = TYPE_INSTANCE_FLAG_ADDRESS_CLASS_1;
330 return 1;
331 }
332 else
333 return 0;
334}
335
336
49d45b20
JB
337/* Implement the "read_pc" gdbarch method. */
338
339static CORE_ADDR
340ft32_read_pc (struct regcache *regcache)
341{
342 ULONGEST pc;
343
344 regcache_cooked_read_unsigned (regcache, FT32_PC_REGNUM, &pc);
345 return pc;
346}
347
348/* Implement the "write_pc" gdbarch method. */
349
350static void
351ft32_write_pc (struct regcache *regcache, CORE_ADDR val)
352{
353 regcache_cooked_write_unsigned (regcache, FT32_PC_REGNUM, val);
354}
355
356/* Implement the "unwind_sp" gdbarch method. */
357
358static CORE_ADDR
359ft32_unwind_sp (struct gdbarch *gdbarch, struct frame_info *next_frame)
360{
361 return frame_unwind_register_unsigned (next_frame, FT32_SP_REGNUM);
362}
363
364/* Given a return value in `regbuf' with a type `valtype',
365 extract and copy its value into `valbuf'. */
366
367static void
368ft32_extract_return_value (struct type *type, struct regcache *regcache,
369 gdb_byte *dst)
370{
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);
375 ULONGEST tmp;
376
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);
381
382 /* Ignore return values more than 8 bytes in size because the ft32
383 returns anything more than 8 bytes in the stack. */
384 if (len > 4)
385 {
386 regcache_cooked_read_unsigned (regcache, FT32_R1_REGNUM, &tmp);
387 store_unsigned_integer (valbuf + len - 4, 4, byte_order, tmp);
388 }
389}
390
391/* Implement the "return_value" gdbarch method. */
392
393static enum return_value_convention
394ft32_return_value (struct gdbarch *gdbarch, struct value *function,
395 struct type *valtype, struct regcache *regcache,
396 gdb_byte *readbuf, const gdb_byte *writebuf)
397{
398 if (TYPE_LENGTH (valtype) > 8)
399 return RETURN_VALUE_STRUCT_CONVENTION;
400 else
401 {
402 if (readbuf != NULL)
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;
407 }
408}
409
410/* Allocate and initialize a ft32_frame_cache object. */
411
412static struct ft32_frame_cache *
413ft32_alloc_frame_cache (void)
414{
415 struct ft32_frame_cache *cache;
416 int i;
417
418 cache = FRAME_OBSTACK_ZALLOC (struct ft32_frame_cache);
419
420 for (i = 0; i < FT32_NUM_REGS; ++i)
421 cache->saved_regs[i] = REG_UNAVAIL;
422
423 return cache;
424}
425
426/* Populate a ft32_frame_cache object for this_frame. */
427
428static struct ft32_frame_cache *
429ft32_frame_cache (struct frame_info *this_frame, void **this_cache)
430{
431 struct ft32_frame_cache *cache;
432 CORE_ADDR current_pc;
433 int i;
434
435 if (*this_cache)
9a3c8263 436 return (struct ft32_frame_cache *) *this_cache;
49d45b20
JB
437
438 cache = ft32_alloc_frame_cache ();
439 *this_cache = cache;
440
441 cache->base = get_frame_register_unsigned (this_frame, FT32_FP_REGNUM);
442 if (cache->base == 0)
443 return cache;
444
445 cache->pc = get_frame_func (this_frame);
446 current_pc = get_frame_pc (this_frame);
447 if (cache->pc)
448 {
449 struct gdbarch *gdbarch = get_frame_arch (this_frame);
450
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);
454 }
455
456 cache->saved_sp = cache->base - 4;
457
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];
461
462 return cache;
463}
464
465/* Implement the "unwind_pc" gdbarch method. */
466
467static CORE_ADDR
468ft32_unwind_pc (struct gdbarch *gdbarch, struct frame_info *next_frame)
469{
470 return frame_unwind_register_unsigned (next_frame, FT32_PC_REGNUM);
471}
472
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. */
475
476static void
477ft32_frame_this_id (struct frame_info *this_frame,
478 void **this_prologue_cache, struct frame_id *this_id)
479{
480 struct ft32_frame_cache *cache = ft32_frame_cache (this_frame,
481 this_prologue_cache);
482
483 /* This marks the outermost frame. */
484 if (cache->base == 0)
485 return;
486
487 *this_id = frame_id_build (cache->saved_sp, cache->pc);
488}
489
490/* Get the value of register regnum in the previous stack frame. */
491
492static struct value *
493ft32_frame_prev_register (struct frame_info *this_frame,
494 void **this_prologue_cache, int regnum)
495{
496 struct ft32_frame_cache *cache = ft32_frame_cache (this_frame,
497 this_prologue_cache);
498
499 gdb_assert (regnum >= 0);
500
501 if (regnum == FT32_SP_REGNUM && cache->saved_sp)
502 return frame_unwind_got_constant (this_frame, regnum, cache->saved_sp);
503
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]);
507
508 return frame_unwind_got_register (this_frame, regnum, regnum);
509}
510
511static const struct frame_unwind ft32_frame_unwind =
512{
513 NORMAL_FRAME,
514 default_frame_unwind_stop_reason,
515 ft32_frame_this_id,
516 ft32_frame_prev_register,
517 NULL,
518 default_frame_sniffer
519};
520
521/* Return the base address of this_frame. */
522
523static CORE_ADDR
524ft32_frame_base_address (struct frame_info *this_frame, void **this_cache)
525{
526 struct ft32_frame_cache *cache = ft32_frame_cache (this_frame,
527 this_cache);
528
529 return cache->base;
530}
531
532static const struct frame_base ft32_frame_base =
533{
534 &ft32_frame_unwind,
535 ft32_frame_base_address,
536 ft32_frame_base_address,
537 ft32_frame_base_address
538};
539
540static struct frame_id
541ft32_dummy_id (struct gdbarch *gdbarch, struct frame_info *this_frame)
542{
543 CORE_ADDR sp = get_frame_register_unsigned (this_frame, FT32_SP_REGNUM);
544
545 return frame_id_build (sp, get_frame_pc (this_frame));
546}
547
548/* Allocate and initialize the ft32 gdbarch object. */
549
550static struct gdbarch *
551ft32_gdbarch_init (struct gdbarch_info info, struct gdbarch_list *arches)
552{
553 struct gdbarch *gdbarch;
554 struct gdbarch_tdep *tdep;
623fb775 555 struct type *void_type;
556 struct type *func_void_type;
49d45b20
JB
557
558 /* If there is already a candidate, use it. */
559 arches = gdbarch_list_lookup_by_info (arches, &info);
560 if (arches != NULL)
561 return arches->gdbarch;
562
563 /* Allocate space for the new architecture. */
564 tdep = XNEW (struct gdbarch_tdep);
565 gdbarch = gdbarch_alloc (&info, tdep);
566
623fb775 567 /* Create a type for PC. We can't use builtin types here, as they may not
568 be defined. */
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;
575
49d45b20
JB
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);
579
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);
585
586 set_gdbarch_return_value (gdbarch, ft32_return_value);
587
623fb775 588 set_gdbarch_pointer_to_address (gdbarch, ft32_pointer_to_address);
589
49d45b20
JB
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);
594
595 frame_base_set_default (gdbarch, &ft32_frame_base);
596
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);
601
602 set_gdbarch_unwind_pc (gdbarch, ft32_unwind_pc);
603
604 set_gdbarch_print_insn (gdbarch, print_insn_ft32);
605
606 /* Hook in ABI-specific overrides, if they have been registered. */
607 gdbarch_init_osabi (info, gdbarch);
608
609 /* Hook in the default unwinders. */
610 frame_unwind_append_unwinder (gdbarch, &ft32_frame_unwind);
611
612 /* Support simple overlay manager. */
613 set_gdbarch_overlay_update (gdbarch, simple_overlay_update);
614
623fb775 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);
620
49d45b20
JB
621 return gdbarch;
622}
623
624/* Register this machine's init routine. */
625
626void
627_initialize_ft32_tdep (void)
628{
629 register_gdbarch_init (bfd_arch_ft32, ft32_gdbarch_init);
630}
This page took 0.089623 seconds and 4 git commands to generate.