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