Include gdb_assert.h in common-defs.h
[external/binutils.git] / gdb / bfin-tdep.c
1 /* Target-dependent code for Analog Devices Blackfin processor, for GDB.
2
3    Copyright (C) 2005-2014 Free Software Foundation, Inc.
4
5    Contributed by Analog Devices, Inc.
6
7    This file is part of GDB.
8
9    This program is free software; you can redistribute it and/or modify
10    it under the terms of the GNU General Public License as published by
11    the Free Software Foundation; either version 3 of the License, or
12    (at your option) any later version.
13
14    This program is distributed in the hope that it will be useful,
15    but WITHOUT ANY WARRANTY; without even the implied warranty of
16    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17    GNU General Public License for more details.
18
19    You should have received a copy of the GNU General Public License
20    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
21
22 #include "defs.h"
23 #include <string.h>
24 #include "inferior.h"
25 #include "gdbcore.h"
26 #include "arch-utils.h"
27 #include "regcache.h"
28 #include "frame.h"
29 #include "frame-unwind.h"
30 #include "frame-base.h"
31 #include "trad-frame.h"
32 #include "dis-asm.h"
33 #include "sim-regno.h"
34 #include "gdb/sim-bfin.h"
35 #include "dwarf2-frame.h"
36 #include "symtab.h"
37 #include "elf-bfd.h"
38 #include "elf/bfin.h"
39 #include "osabi.h"
40 #include "infcall.h"
41 #include "xml-syscall.h"
42 #include "bfin-tdep.h"
43
44 /* Macros used by prologue functions.  */
45 #define P_LINKAGE                       0xE800
46 #define P_MINUS_SP1                     0x0140
47 #define P_MINUS_SP2                     0x05C0
48 #define P_MINUS_SP3                     0x0540
49 #define P_MINUS_SP4                     0x04C0
50 #define P_SP_PLUS                       0x6C06
51 #define P_P2_LOW                        0xE10A
52 #define P_P2_HIGH                       0XE14A
53 #define P_SP_EQ_SP_PLUS_P2              0X5BB2
54 #define P_SP_EQ_P2_PLUS_SP              0x5B96
55 #define P_MINUS_MINUS_SP_EQ_RETS        0x0167
56
57 /* Macros used for program flow control.  */
58 /* 16 bit instruction, max  */
59 #define P_16_BIT_INSR_MAX               0xBFFF
60 /* 32 bit instruction, min  */
61 #define P_32_BIT_INSR_MIN               0xC000
62 /* 32 bit instruction, max  */
63 #define P_32_BIT_INSR_MAX               0xE801
64 /* jump (preg), 16-bit, min  */
65 #define P_JUMP_PREG_MIN                 0x0050
66 /* jump (preg), 16-bit, max  */
67 #define P_JUMP_PREG_MAX                 0x0057
68 /* jump (pc+preg), 16-bit, min  */
69 #define P_JUMP_PC_PLUS_PREG_MIN         0x0080
70 /* jump (pc+preg), 16-bit, max  */
71 #define P_JUMP_PC_PLUS_PREG_MAX         0x0087
72 /* jump.s pcrel13m2, 16-bit, min  */
73 #define P_JUMP_S_MIN                    0x2000
74 /* jump.s pcrel13m2, 16-bit, max  */
75 #define P_JUMP_S_MAX                    0x2FFF
76 /* jump.l pcrel25m2, 32-bit, min  */
77 #define P_JUMP_L_MIN                    0xE200
78 /* jump.l pcrel25m2, 32-bit, max  */
79 #define P_JUMP_L_MAX                    0xE2FF
80 /* conditional jump pcrel11m2, 16-bit, min  */
81 #define P_IF_CC_JUMP_MIN                0x1800
82 /* conditional jump pcrel11m2, 16-bit, max  */
83 #define P_IF_CC_JUMP_MAX                0x1BFF
84 /* conditional jump(bp) pcrel11m2, 16-bit, min  */
85 #define P_IF_CC_JUMP_BP_MIN             0x1C00
86 /* conditional jump(bp) pcrel11m2, 16-bit, max  */
87 #define P_IF_CC_JUMP_BP_MAX             0x1FFF
88 /* conditional !jump pcrel11m2, 16-bit, min  */
89 #define P_IF_NOT_CC_JUMP_MIN            0x1000
90 /* conditional !jump pcrel11m2, 16-bit, max  */
91 #define P_IF_NOT_CC_JUMP_MAX            0x13FF
92 /* conditional jump(bp) pcrel11m2, 16-bit, min  */
93 #define P_IF_NOT_CC_JUMP_BP_MIN         0x1400
94 /* conditional jump(bp) pcrel11m2, 16-bit, max  */
95 #define P_IF_NOT_CC_JUMP_BP_MAX         0x17FF
96 /* call (preg), 16-bit, min  */
97 #define P_CALL_PREG_MIN                 0x0060
98 /* call (preg), 16-bit, max  */
99 #define P_CALL_PREG_MAX                 0x0067
100 /* call (pc+preg), 16-bit, min  */
101 #define P_CALL_PC_PLUS_PREG_MIN         0x0070
102 /* call (pc+preg), 16-bit, max  */
103 #define P_CALL_PC_PLUS_PREG_MAX         0x0077
104 /* call pcrel25m2, 32-bit, min  */
105 #define P_CALL_MIN                      0xE300
106 /* call pcrel25m2, 32-bit, max  */
107 #define P_CALL_MAX                      0xE3FF
108 /* RTS  */
109 #define P_RTS                           0x0010
110 /* MNOP  */
111 #define P_MNOP                          0xC803
112 /* EXCPT, 16-bit, min  */
113 #define P_EXCPT_MIN                     0x00A0
114 /* EXCPT, 16-bit, max  */
115 #define P_EXCPT_MAX                     0x00AF
116 /* multi instruction mask 1, 16-bit  */
117 #define P_BIT_MULTI_INS_1               0xC000
118 /* multi instruction mask 2, 16-bit  */
119 #define P_BIT_MULTI_INS_2               0x0800
120
121 /* The maximum bytes we search to skip the prologue.  */
122 #define UPPER_LIMIT                     40
123
124 /* ASTAT bits  */
125 #define ASTAT_CC_POS                    5
126 #define ASTAT_CC                        (1 << ASTAT_CC_POS)
127
128 /* Initial value: Register names used in BFIN's ISA documentation.  */
129
130 static const char * const bfin_register_name_strings[] =
131 {
132   "r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7",
133   "p0", "p1", "p2", "p3", "p4", "p5", "sp", "fp",
134   "i0", "i1", "i2", "i3", "m0", "m1", "m2", "m3",
135   "b0", "b1", "b2", "b3", "l0", "l1", "l2", "l3",
136   "a0x", "a0w", "a1x", "a1w", "astat", "rets",
137   "lc0", "lt0", "lb0", "lc1", "lt1", "lb1", "cycles", "cycles2",
138   "usp", "seqstat", "syscfg", "reti", "retx", "retn", "rete",
139   "pc", "cc",
140 };
141
142 #define NUM_BFIN_REGNAMES ARRAY_SIZE (bfin_register_name_strings)
143
144
145 /* In this diagram successive memory locations increase downwards or the
146    stack grows upwards with negative indices.  (PUSH analogy for stack.)
147
148    The top frame is the "frame" of the current function being executed.
149
150      +--------------+ SP    -
151      |  local vars  |       ^
152      +--------------+       |
153      |  save regs   |       |
154      +--------------+ FP    |
155      |   old FP    -|--    top
156      +--------------+  |  frame
157      |    RETS      |  |    |
158      +--------------+  |    |
159      |   param 1    |  |    |
160      |   param 2    |  |    |
161      |    ...       |  |    V
162      +--------------+  |    -
163      |  local vars  |  |    ^
164      +--------------+  |    |
165      |  save regs   |  |    |
166      +--------------+<-     |
167      |   old FP    -|--   next
168      +--------------+  |  frame
169      |    RETS      |  |    |
170      +--------------+  |    |
171      |   param 1    |  |    |
172      |   param 2    |  |    |
173      |    ...       |  |    V
174      +--------------+  |    -
175      |  local vars  |  |    ^
176      +--------------+  |    |
177      |  save regs   |  |    |
178      +--------------+<-  next frame
179      |   old FP     |       |
180      +--------------+       |
181      |    RETS      |       V
182      +--------------+       -
183
184    The frame chain is formed as following:
185
186      FP has the topmost frame.
187      FP + 4 has the previous FP and so on.  */
188
189
190 /* Map from DWARF2 register number to GDB register number.  */
191
192 static const int map_gcc_gdb[] =
193 {
194   BFIN_R0_REGNUM,
195   BFIN_R1_REGNUM,
196   BFIN_R2_REGNUM,
197   BFIN_R3_REGNUM,
198   BFIN_R4_REGNUM,
199   BFIN_R5_REGNUM,
200   BFIN_R6_REGNUM,
201   BFIN_R7_REGNUM,
202   BFIN_P0_REGNUM,
203   BFIN_P1_REGNUM,
204   BFIN_P2_REGNUM,
205   BFIN_P3_REGNUM,
206   BFIN_P4_REGNUM,
207   BFIN_P5_REGNUM,
208   BFIN_SP_REGNUM,
209   BFIN_FP_REGNUM,
210   BFIN_I0_REGNUM,
211   BFIN_I1_REGNUM,
212   BFIN_I2_REGNUM,
213   BFIN_I3_REGNUM,
214   BFIN_B0_REGNUM,
215   BFIN_B1_REGNUM,
216   BFIN_B2_REGNUM,
217   BFIN_B3_REGNUM,
218   BFIN_L0_REGNUM,
219   BFIN_L1_REGNUM,
220   BFIN_L2_REGNUM,
221   BFIN_L3_REGNUM,
222   BFIN_M0_REGNUM,
223   BFIN_M1_REGNUM,
224   BFIN_M2_REGNUM,
225   BFIN_M3_REGNUM,
226   BFIN_A0_DOT_X_REGNUM,
227   BFIN_A1_DOT_X_REGNUM,
228   BFIN_CC_REGNUM,
229   BFIN_RETS_REGNUM,
230   BFIN_RETI_REGNUM,
231   BFIN_RETX_REGNUM,
232   BFIN_RETN_REGNUM,
233   BFIN_RETE_REGNUM,
234   BFIN_ASTAT_REGNUM,
235   BFIN_SEQSTAT_REGNUM,
236   BFIN_USP_REGNUM,
237   BFIN_LT0_REGNUM,
238   BFIN_LT1_REGNUM,
239   BFIN_LC0_REGNUM,
240   BFIN_LC1_REGNUM,
241   BFIN_LB0_REGNUM,
242   BFIN_LB1_REGNUM
243 };
244
245
246 struct bfin_frame_cache
247 {
248   /* Base address.  */
249   CORE_ADDR base;
250   CORE_ADDR sp_offset;
251   CORE_ADDR pc;
252   int frameless_pc_value;
253
254   /* Saved registers.  */
255   CORE_ADDR saved_regs[BFIN_NUM_REGS];
256   CORE_ADDR saved_sp;
257
258   /* Stack space reserved for local variables.  */
259   long locals;
260 };
261
262 /* Allocate and initialize a frame cache.  */
263
264 static struct bfin_frame_cache *
265 bfin_alloc_frame_cache (void)
266 {
267   struct bfin_frame_cache *cache;
268   int i;
269
270   cache = FRAME_OBSTACK_ZALLOC (struct bfin_frame_cache);
271
272   /* Base address.  */
273   cache->base = 0;
274   cache->sp_offset = -4;
275   cache->pc = 0;
276   cache->frameless_pc_value = 0;
277
278   /* Saved registers.  We initialize these to -1 since zero is a valid
279      offset (that's where fp is supposed to be stored).  */
280   for (i = 0; i < BFIN_NUM_REGS; i++)
281     cache->saved_regs[i] = -1;
282
283   /* Frameless until proven otherwise.  */
284   cache->locals = -1;
285
286   return cache;
287 }
288
289 static struct bfin_frame_cache *
290 bfin_frame_cache (struct frame_info *this_frame, void **this_cache)
291 {
292   struct bfin_frame_cache *cache;
293   int i;
294
295   if (*this_cache)
296     return *this_cache;
297
298   cache = bfin_alloc_frame_cache ();
299   *this_cache = cache;
300
301   cache->base = get_frame_register_unsigned (this_frame, BFIN_FP_REGNUM);
302   if (cache->base == 0)
303     return cache;
304
305   /* For normal frames, PC is stored at [FP + 4].  */
306   cache->saved_regs[BFIN_PC_REGNUM] = 4;
307   cache->saved_regs[BFIN_FP_REGNUM] = 0;
308
309   /* Adjust all the saved registers such that they contain addresses
310      instead of offsets.  */
311   for (i = 0; i < BFIN_NUM_REGS; i++)
312     if (cache->saved_regs[i] != -1)
313       cache->saved_regs[i] += cache->base;
314
315   cache->pc = get_frame_func (this_frame) ;
316   if (cache->pc == 0 || cache->pc == get_frame_pc (this_frame))
317     {
318       /* Either there is no prologue (frameless function) or we are at
319          the start of a function.  In short we do not have a frame.
320          PC is stored in rets register.  FP points to previous frame.  */
321
322       cache->saved_regs[BFIN_PC_REGNUM] =
323         get_frame_register_unsigned (this_frame, BFIN_RETS_REGNUM);
324       cache->frameless_pc_value = 1;
325       cache->base = get_frame_register_unsigned (this_frame, BFIN_FP_REGNUM);
326       cache->saved_regs[BFIN_FP_REGNUM] = cache->base;
327       cache->saved_sp = cache->base;
328     }
329   else
330     {
331       cache->frameless_pc_value = 0;
332
333       /* Now that we have the base address for the stack frame we can
334          calculate the value of SP in the calling frame.  */
335       cache->saved_sp = cache->base + 8;
336     }
337
338   return cache;
339 }
340
341 static void
342 bfin_frame_this_id (struct frame_info *this_frame,
343                     void **this_cache,
344                     struct frame_id *this_id)
345 {
346   struct bfin_frame_cache *cache = bfin_frame_cache (this_frame, this_cache);
347
348   /* This marks the outermost frame.  */
349   if (cache->base == 0)
350     return;
351
352   /* See the end of bfin_push_dummy_call.  */
353   *this_id = frame_id_build (cache->base + 8, cache->pc);
354 }
355
356 static struct value *
357 bfin_frame_prev_register (struct frame_info *this_frame,
358                           void **this_cache,
359                           int regnum)
360 {
361   struct gdbarch *gdbarch = get_frame_arch (this_frame);
362   struct bfin_frame_cache *cache = bfin_frame_cache (this_frame, this_cache);
363
364   if (regnum == gdbarch_sp_regnum (gdbarch) && cache->saved_sp)
365     return frame_unwind_got_constant (this_frame, regnum, cache->saved_sp);
366
367   if (regnum < BFIN_NUM_REGS && cache->saved_regs[regnum] != -1)
368     return frame_unwind_got_memory (this_frame, regnum,
369                                     cache->saved_regs[regnum]);
370
371   return frame_unwind_got_register (this_frame, regnum, regnum);
372 }
373
374 static const struct frame_unwind bfin_frame_unwind =
375 {
376   NORMAL_FRAME,
377   default_frame_unwind_stop_reason,
378   bfin_frame_this_id,
379   bfin_frame_prev_register,
380   NULL,
381   default_frame_sniffer
382 };
383
384 /* Check for "[--SP] = <reg>;" insns.  These are appear in function
385    prologues to save misc registers onto the stack.  */
386
387 static int
388 is_minus_minus_sp (int op)
389 {
390   op &= 0xFFC0;
391
392   if ((op == P_MINUS_SP1) || (op == P_MINUS_SP2)
393       || (op == P_MINUS_SP3) || (op == P_MINUS_SP4))
394     return 1;
395
396   return 0;
397 }
398
399 /* Skip all the insns that appear in generated function prologues.  */
400
401 static CORE_ADDR
402 bfin_skip_prologue (struct gdbarch *gdbarch, CORE_ADDR pc)
403 {
404   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
405   int op = read_memory_unsigned_integer (pc, 2, byte_order);
406   CORE_ADDR orig_pc = pc;
407   int done = 0;
408
409   /* The new gcc prologue generates the register saves BEFORE the link
410      or RETS saving instruction.
411      So, our job is to stop either at those instructions or some upper
412      limit saying there is no frame!  */
413
414   while (!done)
415     {
416       if (is_minus_minus_sp (op))
417         {
418           while (is_minus_minus_sp (op))
419             {
420               pc += 2;
421               op = read_memory_unsigned_integer (pc, 2, byte_order);
422             }
423
424           if (op == P_LINKAGE)
425             pc += 4;
426
427           done = 1;
428         }
429       else if (op == P_LINKAGE)
430         {
431           pc += 4;
432           done = 1;
433         }
434       else if (op == P_MINUS_MINUS_SP_EQ_RETS)
435         {
436           pc += 2;
437           done = 1;
438         }
439       else if (op == P_RTS)
440         {
441           done = 1;
442         }
443       else if ((op >= P_JUMP_PREG_MIN && op <= P_JUMP_PREG_MAX)
444                || (op >= P_JUMP_PC_PLUS_PREG_MIN
445                    && op <= P_JUMP_PC_PLUS_PREG_MAX)
446                || (op == P_JUMP_S_MIN && op <= P_JUMP_S_MAX))
447         {
448           done = 1;
449         }
450       else if (pc - orig_pc >= UPPER_LIMIT)
451         {
452           warning (_("Function Prologue not recognised; "
453                      "pc will point to ENTRY_POINT of the function"));
454           pc = orig_pc + 2;
455           done = 1;
456         }
457       else
458         {
459           pc += 2; /* Not a terminating instruction go on.  */
460           op = read_memory_unsigned_integer (pc, 2, byte_order);
461         }
462     }
463
464    /* TODO:
465       Dwarf2 uses entry point value AFTER some register initializations.
466       We should perhaps skip such asssignments as well (R6 = R1, ...).  */
467
468   return pc;
469 }
470
471 /* Return the GDB type object for the "standard" data type of data in
472    register N.  This should be void pointer for P0-P5, SP, FP;
473    void pointer to function for PC; int otherwise.  */
474
475 static struct type *
476 bfin_register_type (struct gdbarch *gdbarch, int regnum)
477 {
478   if ((regnum >= BFIN_P0_REGNUM && regnum <= BFIN_FP_REGNUM)
479       || regnum == BFIN_USP_REGNUM)
480     return builtin_type (gdbarch)->builtin_data_ptr;
481
482   if (regnum == BFIN_PC_REGNUM || regnum == BFIN_RETS_REGNUM
483       || regnum == BFIN_RETI_REGNUM || regnum == BFIN_RETX_REGNUM
484       || regnum == BFIN_RETN_REGNUM || regnum == BFIN_RETE_REGNUM
485       || regnum == BFIN_LT0_REGNUM || regnum == BFIN_LB0_REGNUM
486       || regnum == BFIN_LT1_REGNUM || regnum == BFIN_LB1_REGNUM)
487     return builtin_type (gdbarch)->builtin_func_ptr;
488
489   return builtin_type (gdbarch)->builtin_int32;
490 }
491
492 static CORE_ADDR
493 bfin_push_dummy_call (struct gdbarch *gdbarch,
494                       struct value *function,
495                       struct regcache *regcache,
496                       CORE_ADDR bp_addr,
497                       int nargs,
498                       struct value **args,
499                       CORE_ADDR sp,
500                       int struct_return,
501                       CORE_ADDR struct_addr)
502 {
503   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
504   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
505   gdb_byte buf[4];
506   int i;
507   long reg_r0, reg_r1, reg_r2;
508   int total_len = 0;
509   enum bfin_abi abi = bfin_abi (gdbarch);
510   CORE_ADDR func_addr = find_function_addr (function, NULL);
511
512   for (i = nargs - 1; i >= 0; i--)
513     {
514       struct type *value_type = value_enclosing_type (args[i]);
515
516       total_len += (TYPE_LENGTH (value_type) + 3) & ~3;
517     }
518
519   /* At least twelve bytes of stack space must be allocated for the function's
520      arguments, even for functions that have less than 12 bytes of argument
521      data.  */
522
523   if (total_len < 12)
524     sp -= 12 - total_len;
525
526   /* Push arguments in reverse order.  */
527
528   for (i = nargs - 1; i >= 0; i--)
529     {
530       struct type *value_type = value_enclosing_type (args[i]);
531       struct type *arg_type = check_typedef (value_type);
532       int container_len = (TYPE_LENGTH (value_type) + 3) & ~3;
533
534       sp -= container_len;
535       write_memory (sp, value_contents_writeable (args[i]), container_len);
536     }
537
538   /* Initialize R0, R1, and R2 to the first 3 words of parameters.  */
539
540   reg_r0 = read_memory_integer (sp, 4, byte_order);
541   regcache_cooked_write_unsigned (regcache, BFIN_R0_REGNUM, reg_r0);
542   reg_r1 = read_memory_integer (sp + 4, 4, byte_order);
543   regcache_cooked_write_unsigned (regcache, BFIN_R1_REGNUM, reg_r1);
544   reg_r2 = read_memory_integer (sp + 8, 4, byte_order);
545   regcache_cooked_write_unsigned (regcache, BFIN_R2_REGNUM, reg_r2);
546
547   /* Store struct value address.  */
548
549   if (struct_return)
550     regcache_cooked_write_unsigned (regcache, BFIN_P0_REGNUM, struct_addr);
551
552   /* Set the dummy return value to bp_addr.
553      A dummy breakpoint will be setup to execute the call.  */
554
555   regcache_cooked_write_unsigned (regcache, BFIN_RETS_REGNUM, bp_addr);
556
557   /* Finally, update the stack pointer.  */
558
559   regcache_cooked_write_unsigned (regcache, BFIN_SP_REGNUM, sp);
560
561   return sp;
562 }
563
564 /* Convert DWARF2 register number REG to the appropriate register number
565    used by GDB.  */
566
567 static int
568 bfin_reg_to_regnum (struct gdbarch *gdbarch, int reg)
569 {
570   if (reg > ARRAY_SIZE (map_gcc_gdb))
571     return 0;
572
573   return map_gcc_gdb[reg];
574 }
575
576 /* This function implements the 'breakpoint_from_pc' gdbarch method.
577    It returns a pointer to a string of bytes that encode a breakpoint
578    instruction, stores the length of the string to *lenptr, and
579    adjusts the program counter (if necessary) to point to the actual
580    memory location where the breakpoint should be inserted.  */
581
582 static const unsigned char *
583 bfin_breakpoint_from_pc (struct gdbarch *gdbarch,
584                          CORE_ADDR *pcptr, int *lenptr)
585 {
586   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
587   unsigned short iw;
588   static unsigned char bfin_breakpoint[] = {0xa1, 0x00, 0x00, 0x00};
589   static unsigned char bfin_sim_breakpoint[] = {0x25, 0x00, 0x00, 0x00};
590
591   iw = read_memory_unsigned_integer (*pcptr, 2, byte_order);
592
593   if ((iw & 0xf000) >= 0xc000)
594     /* 32-bit instruction.  */
595     *lenptr = 4;
596   else
597     *lenptr = 2;
598
599   if (strcmp (target_shortname, "sim") == 0)
600     return bfin_sim_breakpoint;
601   else
602     return bfin_breakpoint;
603 }
604
605 static void
606 bfin_extract_return_value (struct type *type,
607                            struct regcache *regs,
608                            gdb_byte *dst)
609 {
610   struct gdbarch *gdbarch = get_regcache_arch (regs);
611   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
612   bfd_byte *valbuf = dst;
613   int len = TYPE_LENGTH (type);
614   ULONGEST tmp;
615   int regno = BFIN_R0_REGNUM;
616
617   gdb_assert (len <= 8);
618
619   while (len > 0)
620     {
621       regcache_cooked_read_unsigned (regs, regno++, &tmp);
622       store_unsigned_integer (valbuf, (len > 4 ? 4 : len), byte_order, tmp);
623       len -= 4;
624       valbuf += 4;
625     }
626 }
627
628 /* Write into appropriate registers a function return value of type
629    TYPE, given in virtual format.  */
630
631 static void
632 bfin_store_return_value (struct type *type,
633                          struct regcache *regs,
634                          const gdb_byte *src)
635 {
636   const bfd_byte *valbuf = src;
637
638   /* Integral values greater than one word are stored in consecutive
639      registers starting with R0.  This will always be a multiple of
640      the register size.  */
641
642   int len = TYPE_LENGTH (type);
643   int regno = BFIN_R0_REGNUM;
644
645   gdb_assert (len <= 8);
646
647   while (len > 0)
648     {
649       regcache_cooked_write (regs, regno++, valbuf);
650       len -= 4;
651       valbuf += 4;
652     }
653 }
654
655 /* Determine, for architecture GDBARCH, how a return value of TYPE
656    should be returned.  If it is supposed to be returned in registers,
657    and READBUF is nonzero, read the appropriate value from REGCACHE,
658    and copy it into READBUF.  If WRITEBUF is nonzero, write the value
659    from WRITEBUF into REGCACHE.  */
660
661 static enum return_value_convention
662 bfin_return_value (struct gdbarch *gdbarch,
663                    struct value *function,
664                    struct type *type,
665                    struct regcache *regcache,
666                    gdb_byte *readbuf,
667                    const gdb_byte *writebuf)
668 {
669   if (TYPE_LENGTH (type) > 8)
670     return RETURN_VALUE_STRUCT_CONVENTION;
671
672   if (readbuf)
673     bfin_extract_return_value (type, regcache, readbuf);
674
675   if (writebuf)
676     bfin_store_return_value (type, regcache, writebuf);
677
678   return RETURN_VALUE_REGISTER_CONVENTION;
679 }
680
681 /* Return the BFIN register name corresponding to register I.  */
682
683 static const char *
684 bfin_register_name (struct gdbarch *gdbarch, int i)
685 {
686   return bfin_register_name_strings[i];
687 }
688
689 static enum register_status
690 bfin_pseudo_register_read (struct gdbarch *gdbarch, struct regcache *regcache,
691                            int regnum, gdb_byte *buffer)
692 {
693   gdb_byte *buf = (gdb_byte *) alloca (MAX_REGISTER_SIZE);
694   enum register_status status;
695
696   if (regnum != BFIN_CC_REGNUM)
697     internal_error (__FILE__, __LINE__,
698                     _("invalid register number %d"), regnum);
699
700   /* Extract the CC bit from the ASTAT register.  */
701   status = regcache_raw_read (regcache, BFIN_ASTAT_REGNUM, buf);
702   if (status == REG_VALID)
703     {
704       buffer[1] = buffer[2] = buffer[3] = 0;
705       buffer[0] = !!(buf[0] & ASTAT_CC);
706     }
707   return status;
708 }
709
710 static void
711 bfin_pseudo_register_write (struct gdbarch *gdbarch, struct regcache *regcache,
712                             int regnum, const gdb_byte *buffer)
713 {
714   gdb_byte *buf = (gdb_byte *) alloca (MAX_REGISTER_SIZE);
715
716   if (regnum != BFIN_CC_REGNUM)
717     internal_error (__FILE__, __LINE__,
718                     _("invalid register number %d"), regnum);
719
720   /* Overlay the CC bit in the ASTAT register.  */
721   regcache_raw_read (regcache, BFIN_ASTAT_REGNUM, buf);
722   buf[0] = (buf[0] & ~ASTAT_CC) | ((buffer[0] & 1) << ASTAT_CC_POS);
723   regcache_raw_write (regcache, BFIN_ASTAT_REGNUM, buf);
724 }
725
726 static CORE_ADDR
727 bfin_frame_base_address (struct frame_info *this_frame, void **this_cache)
728 {
729   struct bfin_frame_cache *cache = bfin_frame_cache (this_frame, this_cache);
730
731   return cache->base;
732 }
733
734 static CORE_ADDR
735 bfin_frame_local_address (struct frame_info *this_frame, void **this_cache)
736 {
737   struct bfin_frame_cache *cache = bfin_frame_cache (this_frame, this_cache);
738
739   return cache->base - 4;
740 }
741
742 static CORE_ADDR
743 bfin_frame_args_address (struct frame_info *this_frame, void **this_cache)
744 {
745   struct bfin_frame_cache *cache = bfin_frame_cache (this_frame, this_cache);
746
747   return cache->base + 8;
748 }
749
750 static const struct frame_base bfin_frame_base =
751 {
752   &bfin_frame_unwind,
753   bfin_frame_base_address,
754   bfin_frame_local_address,
755   bfin_frame_args_address
756 };
757
758 static struct frame_id
759 bfin_dummy_id (struct gdbarch *gdbarch, struct frame_info *this_frame)
760 {
761   CORE_ADDR sp;
762
763   sp = get_frame_register_unsigned (this_frame, BFIN_SP_REGNUM);
764
765   return frame_id_build (sp, get_frame_pc (this_frame));
766 }
767
768 static CORE_ADDR
769 bfin_unwind_pc (struct gdbarch *gdbarch, struct frame_info *next_frame)
770 {
771   return frame_unwind_register_unsigned (next_frame, BFIN_PC_REGNUM);
772 }
773
774 static CORE_ADDR
775 bfin_frame_align (struct gdbarch *gdbarch, CORE_ADDR address)
776 {
777   return (address & ~0x3);
778 }
779
780 enum bfin_abi
781 bfin_abi (struct gdbarch *gdbarch)
782 {
783   return gdbarch_tdep (gdbarch)->bfin_abi;
784 }
785
786 /* Initialize the current architecture based on INFO.  If possible,
787    re-use an architecture from ARCHES, which is a list of
788    architectures already created during this debugging session.
789
790    Called e.g. at program startup, when reading a core file, and when
791    reading a binary file.  */
792
793 static struct gdbarch *
794 bfin_gdbarch_init (struct gdbarch_info info, struct gdbarch_list *arches)
795 {
796   struct gdbarch_tdep *tdep;
797   struct gdbarch *gdbarch;
798   int elf_flags;
799   enum bfin_abi abi;
800
801   /* Extract the ELF flags, if available.  */
802   if (info.abfd && bfd_get_flavour (info.abfd) == bfd_target_elf_flavour)
803     elf_flags = elf_elfheader (info.abfd)->e_flags;
804   else
805     elf_flags = 0;
806
807   abi = BFIN_ABI_FLAT;
808
809   /* If there is already a candidate, use it.  */
810
811   for (arches = gdbarch_list_lookup_by_info (arches, &info);
812        arches != NULL;
813        arches = gdbarch_list_lookup_by_info (arches->next, &info))
814     {
815       if (gdbarch_tdep (arches->gdbarch)->bfin_abi != abi)
816         continue;
817       return arches->gdbarch;
818     }
819
820   tdep = XNEW (struct gdbarch_tdep);
821   gdbarch = gdbarch_alloc (&info, tdep);
822
823   tdep->bfin_abi = abi;
824
825   set_gdbarch_num_regs (gdbarch, BFIN_NUM_REGS);
826   set_gdbarch_pseudo_register_read (gdbarch, bfin_pseudo_register_read);
827   set_gdbarch_pseudo_register_write (gdbarch, bfin_pseudo_register_write);
828   set_gdbarch_num_pseudo_regs (gdbarch, BFIN_NUM_PSEUDO_REGS);
829   set_gdbarch_sp_regnum (gdbarch, BFIN_SP_REGNUM);
830   set_gdbarch_pc_regnum (gdbarch, BFIN_PC_REGNUM);
831   set_gdbarch_ps_regnum (gdbarch, BFIN_ASTAT_REGNUM);
832   set_gdbarch_dwarf2_reg_to_regnum (gdbarch, bfin_reg_to_regnum);
833   set_gdbarch_register_name (gdbarch, bfin_register_name);
834   set_gdbarch_register_type (gdbarch, bfin_register_type);
835   set_gdbarch_dummy_id (gdbarch, bfin_dummy_id);
836   set_gdbarch_push_dummy_call (gdbarch, bfin_push_dummy_call);
837   set_gdbarch_believe_pcc_promotion (gdbarch, 1);
838   set_gdbarch_return_value (gdbarch, bfin_return_value);
839   set_gdbarch_skip_prologue (gdbarch, bfin_skip_prologue);
840   set_gdbarch_inner_than (gdbarch, core_addr_lessthan);
841   set_gdbarch_breakpoint_from_pc (gdbarch, bfin_breakpoint_from_pc);
842   set_gdbarch_decr_pc_after_break (gdbarch, 2);
843   set_gdbarch_frame_args_skip (gdbarch, 8);
844   set_gdbarch_unwind_pc (gdbarch, bfin_unwind_pc);
845   set_gdbarch_frame_align (gdbarch, bfin_frame_align);
846   set_gdbarch_print_insn (gdbarch, print_insn_bfin);
847
848   /* Hook in ABI-specific overrides, if they have been registered.  */
849   gdbarch_init_osabi (info, gdbarch);
850
851   dwarf2_append_unwinders (gdbarch);
852
853   frame_base_set_default (gdbarch, &bfin_frame_base);
854
855   frame_unwind_append_unwinder (gdbarch, &bfin_frame_unwind);
856
857   return gdbarch;
858 }
859
860 /* Provide a prototype to silence -Wmissing-prototypes.  */
861 extern initialize_file_ftype _initialize_bfin_tdep;
862
863 void
864 _initialize_bfin_tdep (void)
865 {
866   register_gdbarch_init (bfd_arch_bfin, bfin_gdbarch_init);
867 }