Treat all unknown auxv tags on FreeBSD as unknown.
[external/binutils.git] / gdb / csky-tdep.c
1 /* Target-dependent code for the CSKY architecture, for GDB.
2
3    Copyright (C) 2010-2018 Free Software Foundation, Inc.
4
5    Contributed by C-SKY Microsystems and Mentor Graphics.
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 "gdb_assert.h"
24 #include "frame.h"
25 #include "inferior.h"
26 #include "symtab.h"
27 #include "value.h"
28 #include "gdbcmd.h"
29 #include "language.h"
30 #include "gdbcore.h"
31 #include "symfile.h"
32 #include "objfiles.h"
33 #include "gdbtypes.h"
34 #include "target.h"
35 #include "arch-utils.h"
36 #include "regcache.h"
37 #include "osabi.h"
38 #include "block.h"
39 #include "reggroups.h"
40 #include "elf/csky.h"
41 #include "elf-bfd.h"
42 #include "symcat.h"
43 #include "sim-regno.h"
44 #include "dis-asm.h"
45 #include "frame-unwind.h"
46 #include "frame-base.h"
47 #include "trad-frame.h"
48 #include "infcall.h"
49 #include "floatformat.h"
50 #include "remote.h"
51 #include "target-descriptions.h"
52 #include "dwarf2-frame.h"
53 #include "user-regs.h"
54 #include "valprint.h"
55 #include "reggroups.h"
56 #include "csky-tdep.h"
57 #include "regset.h"
58 #include "block.h"
59 #include "opcode/csky.h"
60 #include <algorithm>
61 #include <vector>
62
63 /* Control debugging information emitted in this file.  */
64 static int csky_debug = 0;
65
66 static struct reggroup *cr_reggroup;
67 static struct reggroup *fr_reggroup;
68 static struct reggroup *vr_reggroup;
69 static struct reggroup *mmu_reggroup;
70 static struct reggroup *prof_reggroup;
71
72 /* Convenience function to print debug messages in prologue analysis.  */
73
74 static void
75 print_savedreg_msg (int regno, int offsets[], bool print_continuing)
76 {
77   fprintf_unfiltered (gdb_stdlog, "csky: r%d saved at offset 0x%x\n",
78                       regno, offsets[regno]);
79   if (print_continuing)
80     fprintf_unfiltered (gdb_stdlog, "csky: continuing\n");
81 }
82
83 /*  Check whether the instruction at ADDR is 16-bit or not.  */
84
85 static int
86 csky_pc_is_csky16 (struct gdbarch *gdbarch, CORE_ADDR addr)
87 {
88   gdb_byte target_mem[2];
89   int status;
90   unsigned int insn;
91   int ret = 1;
92   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
93
94   status = target_read_memory (addr, target_mem, 2);
95   /* Assume a 16-bit instruction if we can't read memory.  */
96   if (status)
97     return 1;
98
99   /* Get instruction from memory.  */
100   insn = extract_unsigned_integer (target_mem, 2, byte_order);
101   if ((insn & CSKY_32_INSN_MASK) == CSKY_32_INSN_MASK)
102     ret = 0;
103   else if (insn == CSKY_BKPT_INSN)
104     {
105       /* Check for 32-bit bkpt instruction which is all 0.  */
106       status = target_read_memory (addr + 2, target_mem, 2);
107       if (status)
108         return 1;
109
110       insn = extract_unsigned_integer (target_mem, 2, byte_order);
111       if (insn == CSKY_BKPT_INSN)
112         ret = 0;
113     }
114   return ret;
115 }
116
117 /* Get one instruction at ADDR and store it in INSN.  Return 2 for
118    a 16-bit instruction or 4 for a 32-bit instruction.  */
119
120 static int
121 csky_get_insn (struct gdbarch *gdbarch, CORE_ADDR addr, unsigned int *insn)
122 {
123   gdb_byte target_mem[2];
124   unsigned int insn_type;
125   int status;
126   int insn_len = 2;
127   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
128
129   status = target_read_memory (addr, target_mem, 2);
130   if (status)
131     memory_error (TARGET_XFER_E_IO, addr);
132
133   insn_type = extract_unsigned_integer (target_mem, 2, byte_order);
134   if (CSKY_32_INSN_MASK == (insn_type & CSKY_32_INSN_MASK))
135     {
136       status = target_read_memory (addr + 2, target_mem, 2);
137       if (status)
138         memory_error (TARGET_XFER_E_IO, addr);
139       insn_type = ((insn_type << 16)
140                    | extract_unsigned_integer (target_mem, 2, byte_order));
141       insn_len = 4;
142     }
143   *insn = insn_type;
144   return insn_len;
145 }
146
147 /* Implement the read_pc gdbarch method.  */
148
149 static CORE_ADDR
150 csky_read_pc (readable_regcache *regcache)
151 {
152   ULONGEST pc;
153   regcache->cooked_read (CSKY_PC_REGNUM, &pc);
154   return pc;
155 }
156
157 /* Implement the write_pc gdbarch method.  */
158
159 static void
160 csky_write_pc (regcache *regcache, CORE_ADDR val)
161 {
162   regcache_cooked_write_unsigned (regcache, CSKY_PC_REGNUM, val);
163 }
164
165 /* Implement the unwind_sp gdbarch method.  */
166
167 static CORE_ADDR
168 csky_unwind_sp (struct gdbarch *gdbarch, struct frame_info *next_frame)
169 {
170   return frame_unwind_register_unsigned (next_frame, CSKY_SP_REGNUM);
171 }
172
173 /* C-Sky ABI register names.  */
174
175 static const char *csky_register_names[] =
176 {
177   /* General registers 0 - 31.  */
178   "r0",  "r1",  "r2",  "r3",  "r4",  "r5",  "r6",  "r7",
179   "r8",  "r9",  "r10", "r11", "r12", "r13", "r14", "r15",
180   "r16", "r17", "r18", "r19", "r20", "r21", "r22", "r23",
181   "r24", "r25", "r26", "r27", "r28", "r29", "r30", "r31",
182
183   /* DSP hilo registers 36 and 37.  */
184   "",      "",    "",     "",     "hi",    "lo",   "",    "",
185
186   /* FPU/VPU general registers 40 - 71.  */
187   "fr0", "fr1", "fr2",  "fr3",  "fr4",  "fr5",  "fr6",  "fr7",
188   "fr8", "fr9", "fr10", "fr11", "fr12", "fr13", "fr14", "fr15",
189   "vr0", "vr1", "vr2",  "vr3",  "vr4",  "vr5",  "vr6",  "vr7",
190   "vr8", "vr9", "vr10", "vr11", "vr12", "vr13", "vr14", "vr15",
191
192   /* Program counter 72.  */
193   "pc",
194
195   /* Optional registers (ar) 73 - 88.  */
196   "ar0", "ar1", "ar2",  "ar3",  "ar4",  "ar5",  "ar6",  "ar7",
197   "ar8", "ar9", "ar10", "ar11", "ar12", "ar13", "ar14", "ar15",
198
199   /* Control registers (cr) 89 - 119.  */
200   "psr",  "vbr",  "epsr", "fpsr", "epc",  "fpc",  "ss0",  "ss1",
201   "ss2",  "ss3",  "ss4",  "gcr",  "gsr",  "cr13", "cr14", "cr15",
202   "cr16", "cr17", "cr18", "cr19", "cr20", "cr21", "cr22", "cr23",
203   "cr24", "cr25", "cr26", "cr27", "cr28", "cr29", "cr30", "cr31",
204
205   /* FPU/VPU control registers 121 ~ 123.  */
206   /* User sp 127.  */
207   "fid", "fcr", "fesr", "", "", "", "usp",
208
209   /* MMU control registers: 128 - 136.  */
210   "mcr0", "mcr2", "mcr3", "mcr4", "mcr6", "mcr8", "mcr29", "mcr30",
211   "mcr31", "", "", "",
212
213   /* Profiling control registers 140 - 143.  */
214   /* Profiling software general registers 144 - 157.  */
215   "profcr0",  "profcr1",  "profcr2",  "profcr3",  "profsgr0",  "profsgr1",
216   "profsgr2", "profsgr3", "profsgr4", "profsgr5", "profsgr6",  "profsgr7",
217   "profsgr8", "profsgr9", "profsgr10","profsgr11","profsgr12", "profsgr13",
218   "",    "",
219
220   /* Profiling architecture general registers 160 - 174.  */
221   "profagr0", "profagr1", "profagr2", "profagr3", "profagr4", "profagr5",
222   "profagr6", "profagr7", "profagr8", "profagr9", "profagr10","profagr11",
223   "profagr12","profagr13","profagr14", "",
224
225   /* Profiling extension general registers 176 - 188.  */
226   "profxgr0", "profxgr1", "profxgr2", "profxgr3", "profxgr4", "profxgr5",
227   "profxgr6", "profxgr7", "profxgr8", "profxgr9", "profxgr10","profxgr11",
228   "profxgr12",
229
230   /* Control registers in bank1.  */
231   "", "", "", "", "", "", "", "",
232   "", "", "", "", "", "", "", "",
233   "cp1cr16", "cp1cr17", "cp1cr18", "cp1cr19", "cp1cr20", "", "", "",
234   "", "", "", "", "", "", "", "",
235
236   /* Control registers in bank3 (ICE).  */
237   "sepsr", "sevbr", "seepsr", "", "seepc", "", "nsssp", "seusp",
238   "sedcr", "", "", "", "", "", "", "",
239   "", "", "", "", "", "", "", "",
240   "", "", "", "", "", "", "", ""
241 };
242
243 /* Implement the register_name gdbarch method.  */
244
245 static const char *
246 csky_register_name (struct gdbarch *gdbarch, int reg_nr)
247 {
248   if (tdesc_has_registers (gdbarch_target_desc (gdbarch)))
249     return tdesc_register_name (gdbarch, reg_nr);
250
251   if (reg_nr < 0)
252     return NULL;
253
254   if (reg_nr >= gdbarch_num_regs (gdbarch))
255     return NULL;
256
257   return csky_register_names[reg_nr];
258 }
259
260 /* Construct vector type for vrx registers.  */
261
262 static struct type *
263 csky_vector_type (struct gdbarch *gdbarch)
264 {
265   const struct builtin_type *bt = builtin_type (gdbarch);
266
267   struct type *t;
268
269   t = arch_composite_type (gdbarch, "__gdb_builtin_type_vec128i",
270                            TYPE_CODE_UNION);
271
272   append_composite_type_field (t, "u32",
273                                init_vector_type (bt->builtin_int32, 4));
274   append_composite_type_field (t, "u16",
275                                init_vector_type (bt->builtin_int16, 8));
276   append_composite_type_field (t, "u8",
277                                init_vector_type (bt->builtin_int8, 16));
278
279   TYPE_VECTOR (t) = 1;
280   TYPE_NAME (t) = "builtin_type_vec128i";
281
282   return t;
283 }
284
285 /* Return the GDB type object for the "standard" data type
286    of data in register N.  */
287
288 static struct type *
289 csky_register_type (struct gdbarch *gdbarch, int reg_nr)
290 {
291   /* PC, EPC, FPC is a text pointer.  */
292   if ((reg_nr == CSKY_PC_REGNUM)  || (reg_nr == CSKY_EPC_REGNUM)
293       || (reg_nr == CSKY_FPC_REGNUM))
294     return builtin_type (gdbarch)->builtin_func_ptr;
295
296   /* VBR is a data pointer.  */
297   if (reg_nr == CSKY_VBR_REGNUM)
298     return builtin_type (gdbarch)->builtin_data_ptr;
299
300   /* Float register has 64 bits, and only in ck810.  */
301   if ((reg_nr >=CSKY_FR0_REGNUM) && (reg_nr <= CSKY_FR0_REGNUM + 15))
302       return arch_float_type (gdbarch, 64, "builtin_type_csky_ext",
303                               floatformats_ieee_double);
304
305   /* Vector register has 128 bits, and only in ck810.  */
306   if ((reg_nr >= CSKY_VR0_REGNUM) && (reg_nr <= CSKY_VR0_REGNUM + 15))
307     return csky_vector_type (gdbarch);
308
309   /* Profiling general register has 48 bits, we use 64bit.  */
310   if ((reg_nr >= CSKY_PROFGR_REGNUM) && (reg_nr <= CSKY_PROFGR_REGNUM + 44))
311     return builtin_type (gdbarch)->builtin_uint64;
312
313   if (reg_nr == CSKY_SP_REGNUM)
314     return builtin_type (gdbarch)->builtin_data_ptr;
315
316   /* Others are 32 bits.  */
317   return builtin_type (gdbarch)->builtin_int32;
318 }
319
320 /* Data structure to marshall items in a dummy stack frame when
321    calling a function in the inferior.  */
322
323 struct stack_item
324 {
325   stack_item (int len_, const gdb_byte *data_)
326   : len (len_), data (data_)
327   {}
328
329   int len;
330   const gdb_byte *data;
331 };
332
333 /* Implement the push_dummy_call gdbarch method.  */
334
335 static CORE_ADDR
336 csky_push_dummy_call (struct gdbarch *gdbarch, struct value *function,
337                       struct regcache *regcache, CORE_ADDR bp_addr,
338                       int nargs, struct value **args, CORE_ADDR sp,
339                       int struct_return, CORE_ADDR struct_addr)
340 {
341   int argnum;
342   int argreg = CSKY_ABI_A0_REGNUM;
343   int last_arg_regnum = CSKY_ABI_LAST_ARG_REGNUM;
344   int need_dummy_stack = 0;
345   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
346   std::vector<stack_item> stack_items;
347
348   /* Set the return address.  For CSKY, the return breakpoint is
349      always at BP_ADDR.  */
350   regcache_cooked_write_unsigned (regcache, CSKY_LR_REGNUM, bp_addr);
351
352   /* The struct_return pointer occupies the first parameter
353      passing register.  */
354   if (struct_return)
355     {
356       if (csky_debug)
357         {
358           fprintf_unfiltered (gdb_stdlog,
359                               "csky: struct return in %s = %s\n",
360                               gdbarch_register_name (gdbarch, argreg),
361                               paddress (gdbarch, struct_addr));
362         }
363       regcache_cooked_write_unsigned (regcache, argreg, struct_addr);
364       argreg++;
365     }
366
367   /* Put parameters into argument registers in REGCACHE.
368      In ABI argument registers are r0 through r3.  */
369   for (argnum = 0; argnum < nargs; argnum++)
370     {
371       int len;
372       struct type *arg_type;
373       const gdb_byte *val;
374
375       arg_type = check_typedef (value_type (args[argnum]));
376       len = TYPE_LENGTH (arg_type);
377       val = value_contents (args[argnum]);
378
379       /* Copy the argument to argument registers or the dummy stack.
380          Large arguments are split between registers and stack.
381
382          If len < 4, there is no need to worry about endianness since
383          the arguments will always be stored in the low address.  */
384       if (len < 4)
385         {
386           CORE_ADDR regval
387             = extract_unsigned_integer (val, len, byte_order);
388           regcache_cooked_write_unsigned (regcache, argreg, regval);
389           argreg++;
390         }
391       else
392         {
393           while (len > 0)
394             {
395               int partial_len = len < 4 ? len : 4;
396               if (argreg <= last_arg_regnum)
397                 {
398                   /* The argument is passed in an argument register.  */
399                   CORE_ADDR regval
400                     = extract_unsigned_integer (val, partial_len,
401                                                 byte_order);
402                   if (byte_order == BFD_ENDIAN_BIG)
403                     regval <<= (4 - partial_len) * 8;
404
405                   /* Put regval into register in REGCACHE.  */
406                   regcache_cooked_write_unsigned (regcache, argreg,
407                                                   regval);
408                   argreg++;
409                 }
410               else
411                 {
412                   /* The argument should be pushed onto the dummy stack.  */
413                   stack_items.emplace_back (4, val);
414                   need_dummy_stack += 4;
415                 }
416               len -= partial_len;
417               val += partial_len;
418             }
419         }
420     }
421
422   /* Transfer the dummy stack frame to the target.  */
423   std::vector<stack_item>::reverse_iterator iter;
424   for (iter = stack_items.rbegin (); iter != stack_items.rend (); ++iter)
425     {
426       sp -= iter->len;
427       write_memory (sp, iter->data, iter->len);
428     }
429
430   /* Finally, update the SP register.  */
431   regcache_cooked_write_unsigned (regcache, CSKY_SP_REGNUM, sp);
432   return sp;
433 }
434
435 /* Implement the return_value gdbarch method.  */
436
437 static enum return_value_convention
438 csky_return_value (struct gdbarch *gdbarch, struct value *function,
439                    struct type *valtype, struct regcache *regcache,
440                    gdb_byte *readbuf, const gdb_byte *writebuf)
441 {
442   CORE_ADDR regval;
443   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
444   int len = TYPE_LENGTH (valtype);
445   unsigned int ret_regnum = CSKY_RET_REGNUM;
446
447   /* Csky abi specifies that return values larger than 8 bytes
448      are put on the stack.  */
449   if (len > 8)
450     return RETURN_VALUE_STRUCT_CONVENTION;
451   else
452     {
453       if (readbuf != NULL)
454         {
455           ULONGEST tmp;
456           /* By using store_unsigned_integer we avoid having to do
457              anything special for small big-endian values.  */
458           regcache->cooked_read (ret_regnum, &tmp);
459           store_unsigned_integer (readbuf, (len > 4 ? 4 : len),
460                                   byte_order, tmp);
461           if (len > 4)
462             {
463               regcache->cooked_read (ret_regnum + 1, &tmp);
464               store_unsigned_integer (readbuf + 4,  4, byte_order, tmp);
465             }
466         }
467       if (writebuf != NULL)
468         {
469           regval = extract_unsigned_integer (writebuf, len > 4 ? 4 : len,
470                                              byte_order);
471           regcache_cooked_write_unsigned (regcache, ret_regnum, regval);
472           if (len > 4)
473             {
474               regval = extract_unsigned_integer ((gdb_byte *) writebuf + 4,
475                                                  4, byte_order);
476               regcache_cooked_write_unsigned (regcache, ret_regnum + 1,
477                                               regval);
478             }
479
480         }
481       return RETURN_VALUE_REGISTER_CONVENTION;
482     }
483 }
484
485 /* Implement the frame_align gdbarch method.
486
487    Adjust the address downward (direction of stack growth) so that it
488    is correctly aligned for a new stack frame.  */
489
490 static CORE_ADDR
491 csky_frame_align (struct gdbarch *gdbarch, CORE_ADDR addr)
492 {
493   return align_down (addr, 4);
494 }
495
496 /* Unwind cache used for gdbarch fallback unwinder.  */
497
498 struct csky_unwind_cache
499 {
500   /* The stack pointer at the time this frame was created; i.e. the
501      caller's stack pointer when this function was called.  It is used
502      to identify this frame.  */
503   CORE_ADDR prev_sp;
504
505   /* The frame base for this frame is just prev_sp - frame size.
506      FRAMESIZE is the distance from the frame pointer to the
507      initial stack pointer.  */
508   int framesize;
509
510   /* The register used to hold the frame pointer for this frame.  */
511   int framereg;
512
513   /* Saved register offsets.  */
514   struct trad_frame_saved_reg *saved_regs;
515 };
516
517 /* Do prologue analysis, returning the PC of the first instruction
518    after the function prologue.  */
519
520 static CORE_ADDR
521 csky_analyze_prologue (struct gdbarch *gdbarch,
522                        CORE_ADDR start_pc,
523                        CORE_ADDR limit_pc,
524                        CORE_ADDR end_pc,
525                        struct frame_info *this_frame,
526                        struct csky_unwind_cache *this_cache,
527                        lr_type_t lr_type)
528 {
529   CORE_ADDR addr;
530   unsigned int insn, rn;
531   int framesize = 0;
532   int stacksize = 0;
533   int register_offsets[CSKY_NUM_GREGS_SAVED_GREGS];
534   int insn_len;
535   /* For adjusting fp.  */
536   int is_fp_saved = 0;
537   int adjust_fp = 0;
538
539   /* REGISTER_OFFSETS will contain offsets from the top of the frame
540      (NOT the frame pointer) for the various saved registers, or -1
541      if the register is not saved.  */
542   for (rn = 0; rn < CSKY_NUM_GREGS_SAVED_GREGS; rn++)
543     register_offsets[rn] = -1;
544
545   /* Analyze the prologue.  Things we determine from analyzing the
546      prologue include the size of the frame and which registers are
547      saved (and where).  */
548   if (csky_debug)
549     {
550       fprintf_unfiltered (gdb_stdlog,
551                           "csky: Scanning prologue: start_pc = 0x%x,"
552                           "limit_pc = 0x%x\n", (unsigned int) start_pc,
553                           (unsigned int) limit_pc);
554     }
555
556   /* Default to 16 bit instruction.  */
557   insn_len = 2;
558   stacksize = 0;
559   for (addr = start_pc; addr < limit_pc; addr += insn_len)
560     {
561       /* Get next insn.  */
562       insn_len = csky_get_insn (gdbarch, addr, &insn);
563
564       /* Check if 32 bit.  */
565       if (insn_len == 4)
566         {
567           /* subi32 sp,sp oimm12.  */
568           if (CSKY_32_IS_SUBI0 (insn))
569             {
570               /* Got oimm12.  */
571               int offset = CSKY_32_SUBI_IMM (insn);
572               if (csky_debug)
573                 {
574                   fprintf_unfiltered (gdb_stdlog,
575                                       "csky: got subi sp,%d; continuing\n",
576                                       offset);
577                 }
578               stacksize += offset;
579               continue;
580             }
581           /* stm32 ry-rz,(sp).  */
582           else if (CSKY_32_IS_STMx0 (insn))
583             {
584               /* Spill register(s).  */
585               int start_register;
586               int reg_count;
587               int offset;
588
589               /* BIG WARNING! The CKCore ABI does not restrict functions
590                  to taking only one stack allocation.  Therefore, when
591                  we save a register, we record the offset of where it was
592                  saved relative to the current stacksize.  This will
593                  then give an offset from the SP upon entry to our
594                  function.  Remember, stacksize is NOT constant until
595                  we're done scanning the prologue.  */
596               start_register = CSKY_32_STM_VAL_REGNUM (insn);
597               reg_count = CSKY_32_STM_SIZE (insn);
598               if (csky_debug)
599                 {
600                   fprintf_unfiltered (gdb_stdlog,
601                                       "csky: got stm r%d-r%d,(sp)\n",
602                                       start_register,
603                                       start_register + reg_count);
604                 }
605
606               for (rn = start_register, offset = 0;
607                    rn <= start_register + reg_count;
608                    rn++, offset += 4)
609                 {
610                   register_offsets[rn] = stacksize - offset;
611                   if (csky_debug)
612                     {
613                       fprintf_unfiltered (gdb_stdlog,
614                                           "csky: r%d saved at 0x%x"
615                                           " (offset %d)\n",
616                                           rn, register_offsets[rn],
617                                           offset);
618                     }
619                 }
620               if (csky_debug)
621                 fprintf_unfiltered (gdb_stdlog, "csky: continuing\n");
622               continue;
623             }
624           /* stw ry,(sp,disp).  */
625           else if (CSKY_32_IS_STWx0 (insn))
626             {
627               /* Spill register: see note for IS_STM above.  */
628               int disp;
629
630               rn = CSKY_32_ST_VAL_REGNUM (insn);
631               disp = CSKY_32_ST_OFFSET (insn);
632               register_offsets[rn] = stacksize - disp;
633               if (csky_debug)
634                 print_savedreg_msg (rn, register_offsets, true);
635               continue;
636             }
637           else if (CSKY_32_IS_MOV_FP_SP (insn))
638             {
639               /* SP is saved to FP reg, means code afer prologue may
640                  modify SP.  */
641               is_fp_saved = 1;
642               adjust_fp = stacksize;
643               continue;
644             }
645           else if (CSKY_32_IS_MFCR_EPSR (insn))
646             {
647               unsigned int insn2;
648               addr += 4;
649               int mfcr_regnum = insn & 0x1f;
650               insn_len = csky_get_insn (gdbarch, addr, &insn2);
651               if (insn_len == 2)
652                 {
653                   int stw_regnum = (insn2 >> 5) & 0x7;
654                   if (CSKY_16_IS_STWx0 (insn2) && (mfcr_regnum == stw_regnum))
655                     {
656                       int offset;
657
658                       /* CSKY_EPSR_REGNUM.  */
659                       rn  = CSKY_NUM_GREGS;
660                       offset = CSKY_16_STWx0_OFFSET (insn2);
661                       register_offsets[rn] = stacksize - offset;
662                       if (csky_debug)
663                         print_savedreg_msg (rn, register_offsets, true);
664                       continue;
665                     }
666                   break;
667                 }
668               else
669                 {
670                   /* INSN_LEN == 4.  */
671                   int stw_regnum = (insn2 >> 21) & 0x1f;
672                   if (CSKY_32_IS_STWx0 (insn2) && (mfcr_regnum == stw_regnum))
673                     {
674                       int offset;
675
676                       /* CSKY_EPSR_REGNUM.  */
677                       rn  = CSKY_NUM_GREGS;
678                       offset = CSKY_32_ST_OFFSET (insn2);
679                       register_offsets[rn] = framesize - offset;
680                       if (csky_debug)
681                         print_savedreg_msg (rn, register_offsets, true);
682                       continue;
683                     }
684                   break;
685                 }
686             }
687           else if (CSKY_32_IS_MFCR_FPSR (insn))
688             {
689               unsigned int insn2;
690               addr += 4;
691               int mfcr_regnum = insn & 0x1f;
692               insn_len = csky_get_insn (gdbarch, addr, &insn2);
693               if (insn_len == 2)
694                 {
695                   int stw_regnum = (insn2 >> 5) & 0x7;
696                   if (CSKY_16_IS_STWx0 (insn2) && (mfcr_regnum
697                                                  == stw_regnum))
698                     {
699                       int offset;
700
701                       /* CSKY_FPSR_REGNUM.  */
702                       rn  = CSKY_NUM_GREGS + 1;
703                       offset = CSKY_16_STWx0_OFFSET (insn2);
704                       register_offsets[rn] = stacksize - offset;
705                       if (csky_debug)
706                         print_savedreg_msg (rn, register_offsets, true);
707                       continue;
708                     }
709                   break;
710                 }
711               else
712                 {
713                   /* INSN_LEN == 4.  */
714                   int stw_regnum = (insn2 >> 21) & 0x1f;
715                   if (CSKY_32_IS_STWx0 (insn2) && (mfcr_regnum == stw_regnum))
716                     {
717                       int offset;
718
719                       /* CSKY_FPSR_REGNUM.  */
720                       rn  = CSKY_NUM_GREGS + 1;
721                       offset = CSKY_32_ST_OFFSET (insn2);
722                       register_offsets[rn] = framesize - offset;
723                       if (csky_debug)
724                         print_savedreg_msg (rn, register_offsets, true);
725                       continue;
726                     }
727                   break;
728                 }
729             }
730           else if (CSKY_32_IS_MFCR_EPC (insn))
731             {
732               unsigned int insn2;
733               addr += 4;
734               int mfcr_regnum = insn & 0x1f;
735               insn_len = csky_get_insn (gdbarch, addr, &insn2);
736               if (insn_len == 2)
737                 {
738                   int stw_regnum = (insn2 >> 5) & 0x7;
739                   if (CSKY_16_IS_STWx0 (insn2) && (mfcr_regnum == stw_regnum))
740                     {
741                       int offset;
742
743                       /* CSKY_EPC_REGNUM.  */
744                       rn  = CSKY_NUM_GREGS + 2;
745                       offset = CSKY_16_STWx0_OFFSET (insn2);
746                       register_offsets[rn] = stacksize - offset;
747                       if (csky_debug)
748                         print_savedreg_msg (rn, register_offsets, true);
749                       continue;
750                     }
751                   break;
752                 }
753               else
754                 {
755                   /* INSN_LEN == 4.  */
756                   int stw_regnum = (insn2 >> 21) & 0x1f;
757                   if (CSKY_32_IS_STWx0 (insn2) && (mfcr_regnum == stw_regnum))
758                     {
759                       int offset;
760
761                       /* CSKY_EPC_REGNUM.  */
762                       rn  = CSKY_NUM_GREGS + 2;
763                       offset = CSKY_32_ST_OFFSET (insn2);
764                       register_offsets[rn] = framesize - offset;
765                       if (csky_debug)
766                         print_savedreg_msg (rn, register_offsets, true);
767                       continue;
768                     }
769                   break;
770                 }
771             }
772           else if (CSKY_32_IS_MFCR_FPC (insn))
773             {
774               unsigned int insn2;
775               addr += 4;
776               int mfcr_regnum = insn & 0x1f;
777               insn_len = csky_get_insn (gdbarch, addr, &insn2);
778               if (insn_len == 2)
779                 {
780                   int stw_regnum = (insn2 >> 5) & 0x7;
781                   if (CSKY_16_IS_STWx0 (insn2) && (mfcr_regnum == stw_regnum))
782                     {
783                       int offset;
784
785                       /* CSKY_FPC_REGNUM.  */
786                       rn  = CSKY_NUM_GREGS + 3;
787                       offset = CSKY_16_STWx0_OFFSET (insn2);
788                       register_offsets[rn] = stacksize - offset;
789                       if (csky_debug)
790                         print_savedreg_msg (rn, register_offsets, true);
791                       continue;
792                     }
793                   break;
794                 }
795               else
796                 {
797                   /* INSN_LEN == 4.  */
798                   int stw_regnum = (insn2 >> 21) & 0x1f;
799                   if (CSKY_32_IS_STWx0 (insn2) && (mfcr_regnum == stw_regnum))
800                     {
801                       int offset;
802
803                       /* CSKY_FPC_REGNUM.  */
804                       rn  = CSKY_NUM_GREGS + 3;
805                       offset = CSKY_32_ST_OFFSET (insn2);
806                       register_offsets[rn] = framesize - offset;
807                       if (csky_debug)
808                         print_savedreg_msg (rn, register_offsets, true);
809                       continue;
810                     }
811                   break;
812                 }
813             }
814           else if (CSKY_32_IS_PUSH (insn))
815             {
816               /* Push for 32_bit.  */
817               int offset = 0;
818               if (CSKY_32_IS_PUSH_R29 (insn))
819                 {
820                   stacksize += 4;
821                   register_offsets[29] = stacksize;
822                   if (csky_debug)
823                     print_savedreg_msg (29, register_offsets, false);
824                   offset += 4;
825                 }
826               if (CSKY_32_PUSH_LIST2 (insn))
827                 {
828                   int num = CSKY_32_PUSH_LIST2 (insn);
829                   int tmp = 0;
830                   stacksize += num * 4;
831                   offset += num * 4;
832                   if (csky_debug)
833                     {
834                       fprintf_unfiltered (gdb_stdlog,
835                                           "csky: push regs_array: r16-r%d\n",
836                                           16 + num - 1);
837                     }
838                   for (rn = 16; rn <= 16 + num - 1; rn++)
839                     {
840                        register_offsets[rn] = stacksize - tmp;
841                        if (csky_debug)
842                          {
843                            fprintf_unfiltered (gdb_stdlog,
844                                                "csky: r%d saved at 0x%x"
845                                                " (offset %d)\n", rn,
846                                                register_offsets[rn], tmp);
847                          }
848                        tmp += 4;
849                     }
850                 }
851               if (CSKY_32_IS_PUSH_R15 (insn))
852                 {
853                   stacksize += 4;
854                   register_offsets[15] = stacksize;
855                   if (csky_debug)
856                     print_savedreg_msg (15, register_offsets, false);
857                   offset += 4;
858                 }
859               if (CSKY_32_PUSH_LIST1 (insn))
860                 {
861                   int num = CSKY_32_PUSH_LIST1 (insn);
862                   int tmp = 0;
863                   stacksize += num * 4;
864                   offset += num * 4;
865                   if (csky_debug)
866                     {
867                       fprintf_unfiltered (gdb_stdlog,
868                                           "csky: push regs_array: r4-r%d\n",
869                                           4 + num - 1);
870                     }
871                   for (rn = 4; rn <= 4 + num - 1; rn++)
872                     {
873                        register_offsets[rn] = stacksize - tmp;
874                        if (csky_debug)
875                          {
876                            fprintf_unfiltered (gdb_stdlog,
877                                                "csky: r%d saved at 0x%x"
878                                                " (offset %d)\n", rn,
879                                                register_offsets[rn], tmp);
880                          }
881                         tmp += 4;
882                     }
883                 }
884
885               framesize = stacksize;
886               if (csky_debug)
887                 fprintf_unfiltered (gdb_stdlog, "csky: continuing\n");
888               continue;
889             }
890           else if (CSKY_32_IS_LRW4 (insn) || CSKY_32_IS_MOVI4 (insn)
891                    || CSKY_32_IS_MOVIH4 (insn) || CSKY_32_IS_BMASKI4 (insn))
892             {
893               int adjust = 0;
894               int offset = 0;
895               unsigned int insn2;
896
897               if (csky_debug)
898                 {
899                   fprintf_unfiltered (gdb_stdlog,
900                                       "csky: looking at large frame\n");
901                 }
902               if (CSKY_32_IS_LRW4 (insn))
903                 {
904                   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
905                   int literal_addr = (addr + ((insn & 0xffff) << 2))
906                                      & 0xfffffffc;
907                   adjust = read_memory_unsigned_integer (literal_addr, 4,
908                                                          byte_order);
909                 }
910               else if (CSKY_32_IS_MOVI4 (insn))
911                 adjust = (insn  & 0xffff);
912               else if (CSKY_32_IS_MOVIH4 (insn))
913                 adjust = (insn & 0xffff) << 16;
914               else
915                 {
916                   /* CSKY_32_IS_BMASKI4 (insn).  */
917                   adjust = (1 << (((insn & 0x3e00000) >> 21) + 1)) - 1;
918                 }
919
920               if (csky_debug)
921                 {
922                   fprintf_unfiltered (gdb_stdlog,
923                                       "csky: base stacksize=0x%x\n", adjust);
924
925                   /* May have zero or more insns which modify r4.  */
926                   fprintf_unfiltered (gdb_stdlog,
927                                       "csky: looking for r4 adjusters...\n");
928                 }
929
930               offset = 4;
931               insn_len = csky_get_insn (gdbarch, addr + offset, &insn2);
932               while (CSKY_IS_R4_ADJUSTER (insn2))
933                 {
934                   if (CSKY_32_IS_ADDI4 (insn2))
935                     {
936                       int imm = (insn2 & 0xfff) + 1;
937                       adjust += imm;
938                       if (csky_debug)
939                         {
940                           fprintf_unfiltered (gdb_stdlog,
941                                               "csky: addi r4,%d\n", imm);
942                         }
943                     }
944                   else if (CSKY_32_IS_SUBI4 (insn2))
945                     {
946                       int imm = (insn2 & 0xfff) + 1;
947                       adjust -= imm;
948                       if (csky_debug)
949                         {
950                           fprintf_unfiltered (gdb_stdlog,
951                                               "csky: subi r4,%d\n", imm);
952                         }
953                     }
954                   else if (CSKY_32_IS_NOR4 (insn2))
955                     {
956                       adjust = ~adjust;
957                       if (csky_debug)
958                         {
959                           fprintf_unfiltered (gdb_stdlog,
960                                               "csky: nor r4,r4,r4\n");
961                         }
962                     }
963                   else if (CSKY_32_IS_ROTLI4 (insn2))
964                     {
965                       int imm = ((insn2 >> 21) & 0x1f);
966                       int temp = adjust >> (32 - imm);
967                       adjust <<= imm;
968                       adjust |= temp;
969                       if (csky_debug)
970                         {
971                           fprintf_unfiltered (gdb_stdlog,
972                                               "csky: rotli r4,r4,%d\n", imm);
973                         }
974                     }
975                   else if (CSKY_32_IS_LISI4 (insn2))
976                     {
977                       int imm = ((insn2 >> 21) & 0x1f);
978                       adjust <<= imm;
979                       if (csky_debug)
980                         {
981                           fprintf_unfiltered (gdb_stdlog,
982                                               "csky: lsli r4,r4,%d\n", imm);
983                         }
984                     }
985                   else if (CSKY_32_IS_BSETI4 (insn2))
986                     {
987                       int imm = ((insn2 >> 21) & 0x1f);
988                       adjust |= (1 << imm);
989                       if (csky_debug)
990                         {
991                           fprintf_unfiltered (gdb_stdlog,
992                                               "csky: bseti r4,r4 %d\n", imm);
993                         }
994                     }
995                   else if (CSKY_32_IS_BCLRI4 (insn2))
996                     {
997                       int imm = ((insn2 >> 21) & 0x1f);
998                       adjust &= ~(1 << imm);
999                       if (csky_debug)
1000                         {
1001                           fprintf_unfiltered (gdb_stdlog,
1002                                               "csky: bclri r4,r4 %d\n", imm);
1003                         }
1004                     }
1005                   else if (CSKY_32_IS_IXH4 (insn2))
1006                     {
1007                       adjust *= 3;
1008                       if (csky_debug)
1009                         {
1010                           fprintf_unfiltered (gdb_stdlog,
1011                                               "csky: ixh r4,r4,r4\n");
1012                         }
1013                     }
1014                   else if (CSKY_32_IS_IXW4 (insn2))
1015                     {
1016                       adjust *= 5;
1017                       if (csky_debug)
1018                         {
1019                           fprintf_unfiltered (gdb_stdlog,
1020                                               "csky: ixw r4,r4,r4\n");
1021                         }
1022                     }
1023                   else if (CSKY_16_IS_ADDI4 (insn2))
1024                     {
1025                       int imm = (insn2 & 0xff) + 1;
1026                       adjust += imm;
1027                       if (csky_debug)
1028                         {
1029                           fprintf_unfiltered (gdb_stdlog,
1030                                               "csky: addi r4,%d\n", imm);
1031                         }
1032                     }
1033                   else if (CSKY_16_IS_SUBI4 (insn2))
1034                     {
1035                       int imm = (insn2 & 0xff) + 1;
1036                       adjust -= imm;
1037                       if (csky_debug)
1038                         {
1039                           fprintf_unfiltered (gdb_stdlog,
1040                                               "csky: subi r4,%d\n", imm);
1041                         }
1042                     }
1043                   else if (CSKY_16_IS_NOR4 (insn2))
1044                     {
1045                       adjust = ~adjust;
1046                       if (csky_debug)
1047                         {
1048                           fprintf_unfiltered (gdb_stdlog,
1049                                               "csky: nor r4,r4\n");
1050                         }
1051                     }
1052                   else if (CSKY_16_IS_BSETI4 (insn2))
1053                     {
1054                       int imm = (insn2 & 0x1f);
1055                       adjust |= (1 << imm);
1056                       if (csky_debug)
1057                         {
1058                           fprintf_unfiltered (gdb_stdlog,
1059                                               "csky: bseti r4, %d\n", imm);
1060                         }
1061                     }
1062                   else if (CSKY_16_IS_BCLRI4 (insn2))
1063                     {
1064                       int imm = (insn2 & 0x1f);
1065                       adjust &= ~(1 << imm);
1066                       if (csky_debug)
1067                         {
1068                           fprintf_unfiltered (gdb_stdlog,
1069                                               "csky: bclri r4, %d\n", imm);
1070                         }
1071                     }
1072                   else if (CSKY_16_IS_LSLI4 (insn2))
1073                     {
1074                       int imm = (insn2 & 0x1f);
1075                       adjust <<= imm;
1076                       if (csky_debug)
1077                         {
1078                           fprintf_unfiltered (gdb_stdlog,
1079                                               "csky: lsli r4,r4, %d\n", imm);
1080                         }
1081                     }
1082
1083                   offset += insn_len;
1084                   insn_len =  csky_get_insn (gdbarch, addr + offset, &insn2);
1085                 };
1086
1087               if (csky_debug)
1088                 {
1089                   fprintf_unfiltered (gdb_stdlog, "csky: done looking for"
1090                                       " r4 adjusters\n");
1091                 }
1092
1093               /* If the next insn adjusts the stack pointer, we keep
1094                  everything; if not, we scrap it and we've found the
1095                  end of the prologue.  */
1096               if (CSKY_IS_SUBU4 (insn2))
1097                 {
1098                   addr += offset;
1099                   stacksize += adjust;
1100                   if (csky_debug)
1101                     {
1102                       fprintf_unfiltered (gdb_stdlog,
1103                                           "csky: found stack adjustment of"
1104                                           " 0x%x bytes.\n", adjust);
1105                       fprintf_unfiltered (gdb_stdlog,
1106                                           "csky: skipping to new address %s\n",
1107                                           core_addr_to_string_nz (addr));
1108                       fprintf_unfiltered (gdb_stdlog,
1109                                           "csky: continuing\n");
1110                     }
1111                   continue;
1112                 }
1113
1114               /* None of these instructions are prologue, so don't touch
1115                  anything.  */
1116               if (csky_debug)
1117                 {
1118                   fprintf_unfiltered (gdb_stdlog,
1119                                       "csky: no subu sp,sp,r4; NOT altering"
1120                                       " stacksize.\n");
1121                 }
1122               break;
1123             }
1124         }
1125       else
1126         {
1127           /* insn_len != 4.  */
1128
1129           /* subi.sp sp,disp.  */
1130           if (CSKY_16_IS_SUBI0 (insn))
1131             {
1132               int offset = CSKY_16_SUBI_IMM (insn);
1133               if (csky_debug)
1134                 {
1135                   fprintf_unfiltered (gdb_stdlog,
1136                                       "csky: got subi r0,%d; continuing\n",
1137                                       offset);
1138                 }
1139               stacksize += offset;
1140               continue;
1141             }
1142           /* stw.16 rz,(sp,disp).  */
1143           else if (CSKY_16_IS_STWx0 (insn))
1144             {
1145               /* Spill register: see note for IS_STM above.  */
1146               int disp;
1147
1148               rn = CSKY_16_ST_VAL_REGNUM (insn);
1149               disp = CSKY_16_ST_OFFSET (insn);
1150               register_offsets[rn] = stacksize - disp;
1151               if (csky_debug)
1152                 print_savedreg_msg (rn, register_offsets, true);
1153               continue;
1154             }
1155           else if (CSKY_16_IS_MOV_FP_SP (insn))
1156             {
1157               /* SP is saved to FP reg, means prologue may modify SP.  */
1158               is_fp_saved = 1;
1159               adjust_fp = stacksize;
1160               continue;
1161             }
1162           else if (CSKY_16_IS_PUSH (insn))
1163             {
1164               /* Push for 16_bit.  */
1165               int offset = 0;
1166               if (CSKY_16_IS_PUSH_R15 (insn))
1167                 {
1168                   stacksize += 4;
1169                   register_offsets[15] = stacksize;
1170                   if (csky_debug)
1171                     print_savedreg_msg (15, register_offsets, false);
1172                   offset += 4;
1173                  }
1174               if (CSKY_16_PUSH_LIST1 (insn))
1175                 {
1176                   int num = CSKY_16_PUSH_LIST1 (insn);
1177                   int tmp = 0;
1178                   stacksize += num * 4;
1179                   offset += num * 4;
1180                   if (csky_debug)
1181                     {
1182                       fprintf_unfiltered (gdb_stdlog,
1183                                           "csky: push regs_array: r4-r%d\n",
1184                                           4 + num - 1);
1185                     }
1186                   for (rn = 4; rn <= 4 + num - 1; rn++)
1187                     {
1188                        register_offsets[rn] = stacksize - tmp;
1189                        if (csky_debug)
1190                          {
1191                            fprintf_unfiltered (gdb_stdlog,
1192                                                "csky: r%d saved at 0x%x"
1193                                                " (offset %d)\n", rn,
1194                                                register_offsets[rn], offset);
1195                          }
1196                        tmp += 4;
1197                     }
1198                 }
1199
1200               framesize = stacksize;
1201               if (csky_debug)
1202                 fprintf_unfiltered (gdb_stdlog, "csky: continuing\n");
1203               continue;
1204             }
1205           else if (CSKY_16_IS_LRW4 (insn) || CSKY_16_IS_MOVI4 (insn))
1206             {
1207               int adjust = 0;
1208               unsigned int insn2;
1209
1210               if (csky_debug)
1211                 {
1212                   fprintf_unfiltered (gdb_stdlog,
1213                                       "csky: looking at large frame\n");
1214                 }
1215               if (CSKY_16_IS_LRW4 (insn))
1216                 {
1217                   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
1218                   int offset = ((insn & 0x300) >> 3) | (insn & 0x1f);
1219                   int literal_addr = (addr + ( offset << 2)) & 0xfffffffc;
1220                   adjust = read_memory_unsigned_integer (literal_addr, 4,
1221                                                          byte_order);
1222                 }
1223               else
1224                 {
1225                   /* CSKY_16_IS_MOVI4 (insn).  */
1226                   adjust = (insn  & 0xff);
1227                 }
1228
1229               if (csky_debug)
1230                 {
1231                   fprintf_unfiltered (gdb_stdlog,
1232                                       "csky: base stacksize=0x%x\n", adjust);
1233                 }
1234
1235               /* May have zero or more instructions which modify r4.  */
1236               if (csky_debug)
1237                 {
1238                   fprintf_unfiltered (gdb_stdlog,
1239                                       "csky: looking for r4 adjusters...\n");
1240                 }
1241               int offset = 2;
1242               insn_len = csky_get_insn (gdbarch, addr + offset, &insn2);
1243               while (CSKY_IS_R4_ADJUSTER (insn2))
1244                 {
1245                   if (CSKY_32_IS_ADDI4 (insn2))
1246                     {
1247                       int imm = (insn2 & 0xfff) + 1;
1248                       adjust += imm;
1249                       if (csky_debug)
1250                         {
1251                           fprintf_unfiltered (gdb_stdlog,
1252                                               "csky: addi r4,%d\n", imm);
1253                         }
1254                     }
1255                   else if (CSKY_32_IS_SUBI4 (insn2))
1256                     {
1257                       int imm = (insn2 & 0xfff) + 1;
1258                       adjust -= imm;
1259                       if (csky_debug)
1260                         {
1261                           fprintf_unfiltered (gdb_stdlog,
1262                                               "csky: subi r4,%d\n", imm);
1263                         }
1264                     }
1265                   else if (CSKY_32_IS_NOR4 (insn2))
1266                     {
1267                       adjust = ~adjust;
1268                       if (csky_debug)
1269                         {
1270                           fprintf_unfiltered (gdb_stdlog,
1271                                               "csky: nor r4,r4,r4\n");
1272                         }
1273                     }
1274                   else if (CSKY_32_IS_ROTLI4 (insn2))
1275                     {
1276                       int imm = ((insn2 >> 21) & 0x1f);
1277                       int temp = adjust >> (32 - imm);
1278                       adjust <<= imm;
1279                       adjust |= temp;
1280                       if (csky_debug)
1281                         {
1282                           fprintf_unfiltered (gdb_stdlog,
1283                                               "csky: rotli r4,r4,%d\n", imm);
1284                         }
1285                     }
1286                   else if (CSKY_32_IS_LISI4 (insn2))
1287                     {
1288                       int imm = ((insn2 >> 21) & 0x1f);
1289                       adjust <<= imm;
1290                       if (csky_debug)
1291                         {
1292                           fprintf_unfiltered (gdb_stdlog,
1293                                               "csky: lsli r4,r4,%d\n", imm);
1294                         }
1295                     }
1296                   else if (CSKY_32_IS_BSETI4 (insn2))
1297                     {
1298                       int imm = ((insn2 >> 21) & 0x1f);
1299                       adjust |= (1 << imm);
1300                       if (csky_debug)
1301                         {
1302                           fprintf_unfiltered (gdb_stdlog,
1303                                               "csky: bseti r4,r4 %d\n", imm);
1304                         }
1305                     }
1306                   else if (CSKY_32_IS_BCLRI4 (insn2))
1307                     {
1308                       int imm = ((insn2 >> 21) & 0x1f);
1309                       adjust &= ~(1 << imm);
1310                       if (csky_debug)
1311                         {
1312                           fprintf_unfiltered (gdb_stdlog,
1313                                               "csky: bclri r4,r4 %d\n", imm);
1314                         }
1315                     }
1316                   else if (CSKY_32_IS_IXH4 (insn2))
1317                     {
1318                       adjust *= 3;
1319                       if (csky_debug)
1320                         {
1321                           fprintf_unfiltered (gdb_stdlog,
1322                                               "csky: ixh r4,r4,r4\n");
1323                         }
1324                     }
1325                   else if (CSKY_32_IS_IXW4 (insn2))
1326                     {
1327                       adjust *= 5;
1328                       if (csky_debug)
1329                         {
1330                           fprintf_unfiltered (gdb_stdlog,
1331                                               "csky: ixw r4,r4,r4\n");
1332                         }
1333                     }
1334                   else if (CSKY_16_IS_ADDI4 (insn2))
1335                     {
1336                       int imm = (insn2 & 0xff) + 1;
1337                       adjust += imm;
1338                       if (csky_debug)
1339                         {
1340                           fprintf_unfiltered (gdb_stdlog,
1341                                               "csky: addi r4,%d\n", imm);
1342                         }
1343                     }
1344                   else if (CSKY_16_IS_SUBI4 (insn2))
1345                     {
1346                       int imm = (insn2 & 0xff) + 1;
1347                       adjust -= imm;
1348                       if (csky_debug)
1349                         {
1350                           fprintf_unfiltered (gdb_stdlog,
1351                                               "csky: subi r4,%d\n", imm);
1352                         }
1353                     }
1354                   else if (CSKY_16_IS_NOR4 (insn2))
1355                     {
1356                       adjust = ~adjust;
1357                       if (csky_debug)
1358                         {
1359                           fprintf_unfiltered (gdb_stdlog,
1360                                               "csky: nor r4,r4\n");
1361                         }
1362                     }
1363                   else if (CSKY_16_IS_BSETI4 (insn2))
1364                     {
1365                       int imm = (insn2 & 0x1f);
1366                       adjust |= (1 << imm);
1367                       if (csky_debug)
1368                         {
1369                           fprintf_unfiltered (gdb_stdlog,
1370                                               "csky: bseti r4, %d\n", imm);
1371                         }
1372                     }
1373                   else if (CSKY_16_IS_BCLRI4 (insn2))
1374                     {
1375                       int imm = (insn2 & 0x1f);
1376                       adjust &= ~(1 << imm);
1377                       if (csky_debug)
1378                         {
1379                           fprintf_unfiltered (gdb_stdlog,
1380                                               "csky: bclri r4, %d\n", imm);
1381                         }
1382                     }
1383                   else if (CSKY_16_IS_LSLI4 (insn2))
1384                     {
1385                       int imm = (insn2 & 0x1f);
1386                       adjust <<= imm;
1387                       if (csky_debug)
1388                         {
1389                           fprintf_unfiltered (gdb_stdlog,
1390                                               "csky: lsli r4,r4, %d\n", imm);
1391                         }
1392                     }
1393
1394                   offset += insn_len;
1395                   insn_len = csky_get_insn (gdbarch, addr + offset, &insn2);
1396                 };
1397
1398               if (csky_debug)
1399                 {
1400                   fprintf_unfiltered (gdb_stdlog, "csky: "
1401                                       "done looking for r4 adjusters\n");
1402                 }
1403
1404               /* If the next instruction adjusts the stack pointer, we keep
1405                  everything; if not, we scrap it and we've found the end
1406                  of the prologue.  */
1407               if (CSKY_IS_SUBU4 (insn2))
1408                 {
1409                   addr += offset;
1410                   stacksize += adjust;
1411                   if (csky_debug)
1412                     {
1413                       fprintf_unfiltered (gdb_stdlog, "csky: "
1414                                           "found stack adjustment of 0x%x"
1415                                           " bytes.\n", adjust);
1416                       fprintf_unfiltered (gdb_stdlog, "csky: "
1417                                           "skipping to new address %s\n",
1418                                           core_addr_to_string_nz (addr));
1419                       fprintf_unfiltered (gdb_stdlog, "csky: continuing\n");
1420                     }
1421                   continue;
1422                 }
1423
1424               /* None of these instructions are prologue, so don't touch
1425                  anything.  */
1426               if (csky_debug)
1427                 {
1428                   fprintf_unfiltered (gdb_stdlog, "csky: no subu sp,r4; "
1429                                       "NOT altering stacksize.\n");
1430                 }
1431               break;
1432             }
1433         }
1434
1435       /* This is not a prologue instruction, so stop here.  */
1436       if (csky_debug)
1437         {
1438           fprintf_unfiltered (gdb_stdlog, "csky: insn is not a prologue"
1439                               " insn -- ending scan\n");
1440         }
1441       break;
1442     }
1443
1444   if (this_cache)
1445     {
1446       CORE_ADDR unwound_fp;
1447       enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
1448       this_cache->framesize = framesize;
1449
1450       if (is_fp_saved)
1451         {
1452           this_cache->framereg = CSKY_FP_REGNUM;
1453           unwound_fp = get_frame_register_unsigned (this_frame,
1454                                                     this_cache->framereg);
1455           this_cache->prev_sp = unwound_fp + adjust_fp;
1456         }
1457       else
1458         {
1459           this_cache->framereg = CSKY_SP_REGNUM;
1460           unwound_fp = get_frame_register_unsigned (this_frame,
1461                                                     this_cache->framereg);
1462           this_cache->prev_sp = unwound_fp + stacksize;
1463         }
1464
1465       /* Note where saved registers are stored.  The offsets in
1466          REGISTER_OFFSETS are computed relative to the top of the frame.  */
1467       for (rn = 0; rn < CSKY_NUM_GREGS; rn++)
1468         {
1469           if (register_offsets[rn] >= 0)
1470             {
1471               this_cache->saved_regs[rn].addr
1472                 = this_cache->prev_sp - register_offsets[rn];
1473               if (csky_debug)
1474                 {
1475                   CORE_ADDR rn_value = read_memory_unsigned_integer (
1476                     this_cache->saved_regs[rn].addr, 4, byte_order);
1477                   fprintf_unfiltered (gdb_stdlog, "Saved register %s "
1478                                       "stored at 0x%08lx, value=0x%08lx\n",
1479                                       csky_register_names[rn],
1480                                       (unsigned long)
1481                                         this_cache->saved_regs[rn].addr,
1482                                       (unsigned long) rn_value);
1483                 }
1484             }
1485         }
1486       if (lr_type == LR_TYPE_EPC)
1487         {
1488           /* rte || epc .  */
1489           this_cache->saved_regs[CSKY_PC_REGNUM]
1490             = this_cache->saved_regs[CSKY_EPC_REGNUM];
1491         }
1492       else if (lr_type == LR_TYPE_FPC)
1493         {
1494           /* rfi || fpc .  */
1495           this_cache->saved_regs[CSKY_PC_REGNUM]
1496             = this_cache->saved_regs[CSKY_FPC_REGNUM];
1497         }
1498       else
1499         {
1500           this_cache->saved_regs[CSKY_PC_REGNUM]
1501             = this_cache->saved_regs[CSKY_LR_REGNUM];
1502         }
1503     }
1504
1505   return addr;
1506 }
1507
1508 /* Detect whether PC is at a point where the stack frame has been
1509    destroyed.  */
1510
1511 static int
1512 csky_stack_frame_destroyed_p (struct gdbarch *gdbarch, CORE_ADDR pc)
1513 {
1514   unsigned int insn;
1515   CORE_ADDR addr;
1516   CORE_ADDR func_start, func_end;
1517
1518   if (!find_pc_partial_function (pc, NULL, &func_start, &func_end))
1519     return 0;
1520
1521   bool fp_saved = false;
1522   int insn_len;
1523   for (addr = func_start; addr < func_end; addr += insn_len)
1524     {
1525       /* Get next insn.  */
1526       insn_len = csky_get_insn (gdbarch, addr, &insn);
1527
1528       if (insn_len == 2)
1529         {
1530           /* Is sp is saved to fp.  */
1531           if (CSKY_16_IS_MOV_FP_SP (insn))
1532             fp_saved = true;
1533           /* If sp was saved to fp and now being restored from
1534              fp then it indicates the start of epilog.  */
1535           else if (fp_saved && CSKY_16_IS_MOV_SP_FP (insn))
1536             return pc >= addr;
1537         }
1538     }
1539   return 0;
1540 }
1541
1542 /* Implement the skip_prologue gdbarch hook.  */
1543
1544 static CORE_ADDR
1545 csky_skip_prologue (struct gdbarch *gdbarch, CORE_ADDR pc)
1546 {
1547   CORE_ADDR func_addr, func_end;
1548   struct symtab_and_line sal;
1549   const int default_search_limit = 128;
1550
1551   /* See if we can find the end of the prologue using the symbol table.  */
1552   if (find_pc_partial_function (pc, NULL, &func_addr, &func_end))
1553     {
1554       CORE_ADDR post_prologue_pc
1555         = skip_prologue_using_sal (gdbarch, func_addr);
1556
1557       if (post_prologue_pc != 0)
1558         return std::max (pc, post_prologue_pc);
1559     }
1560   else
1561     func_end = pc + default_search_limit;
1562
1563   /* Find the end of prologue.  Default lr_type.  */
1564   return csky_analyze_prologue (gdbarch, pc, func_end, func_end,
1565                                 NULL, NULL, LR_TYPE_R15);
1566 }
1567
1568 /* Implement the breakpoint_kind_from_pc gdbarch method.  */
1569
1570 static int
1571 csky_breakpoint_kind_from_pc (struct gdbarch *gdbarch, CORE_ADDR *pcptr)
1572 {
1573   if (csky_pc_is_csky16 (gdbarch, *pcptr))
1574     return CSKY_INSN_SIZE16;
1575   else
1576     return CSKY_INSN_SIZE32;
1577 }
1578
1579 /* Implement the sw_breakpoint_from_kind gdbarch method.  */
1580
1581 static const gdb_byte *
1582 csky_sw_breakpoint_from_kind (struct gdbarch *gdbarch, int kind, int *size)
1583 {
1584   *size = kind;
1585   if (kind == CSKY_INSN_SIZE16)
1586     {
1587       static gdb_byte csky_16_breakpoint[] = { 0, 0 };
1588       return csky_16_breakpoint;
1589     }
1590   else
1591     {
1592       static gdb_byte csky_32_breakpoint[] = { 0, 0, 0, 0 };
1593       return csky_32_breakpoint;
1594     }
1595 }
1596
1597 /* Implement the memory_insert_breakpoint gdbarch method.  */
1598
1599 static int
1600 csky_memory_insert_breakpoint (struct gdbarch *gdbarch,
1601                                struct bp_target_info *bp_tgt)
1602 {
1603   int val;
1604   const unsigned char *bp;
1605   gdb_byte bp_write_record1[] = { 0, 0, 0, 0 };
1606   gdb_byte bp_write_record2[] = { 0, 0, 0, 0 };
1607   gdb_byte bp_record[] = { 0, 0, 0, 0 };
1608
1609   /* Sanity-check bp_address.  */
1610   if (bp_tgt->reqstd_address % 2)
1611     warning (_("Invalid breakpoint address 0x%x is an odd number."),
1612              (unsigned int) bp_tgt->reqstd_address);
1613   scoped_restore restore_memory
1614     = make_scoped_restore_show_memory_breakpoints (1);
1615
1616   /* Determine appropriate breakpoint_kind for this address.  */
1617   bp_tgt->kind = csky_breakpoint_kind_from_pc (gdbarch,
1618                                                &bp_tgt->reqstd_address);
1619
1620   /* Save the memory contents.  */
1621   bp_tgt->shadow_len = bp_tgt->kind;
1622
1623   /* Fill bp_tgt->placed_address.  */
1624   bp_tgt->placed_address = bp_tgt->reqstd_address;
1625
1626   if (bp_tgt->kind == CSKY_INSN_SIZE16)
1627     {
1628       if ((bp_tgt->reqstd_address % 4) == 0)
1629         {
1630           /* Read two bytes.  */
1631           val = target_read_memory (bp_tgt->reqstd_address,
1632                                     bp_tgt->shadow_contents, 2);
1633           if (val)
1634             return val;
1635
1636           /* Read two bytes.  */
1637           val = target_read_memory (bp_tgt->reqstd_address + 2,
1638                                     bp_record, 2);
1639           if (val)
1640             return val;
1641
1642           /* Write the breakpoint.  */
1643           bp_write_record1[2] = bp_record[0];
1644           bp_write_record1[3] = bp_record[1];
1645           bp = bp_write_record1;
1646           val = target_write_raw_memory (bp_tgt->reqstd_address, bp,
1647                                          CSKY_WR_BKPT_MODE);
1648         }
1649       else
1650         {
1651           val = target_read_memory (bp_tgt->reqstd_address,
1652                                     bp_tgt->shadow_contents, 2);
1653           if (val)
1654             return val;
1655
1656           val = target_read_memory (bp_tgt->reqstd_address - 2,
1657                                     bp_record, 2);
1658           if (val)
1659             return val;
1660
1661           /* Write the breakpoint.  */
1662           bp_write_record1[0] = bp_record[0];
1663           bp_write_record1[1] = bp_record[1];
1664           bp = bp_write_record1;
1665           val = target_write_raw_memory (bp_tgt->reqstd_address - 2,
1666                                          bp, CSKY_WR_BKPT_MODE);
1667         }
1668     }
1669   else
1670     {
1671       if (bp_tgt->placed_address % 4 == 0)
1672         {
1673           val = target_read_memory (bp_tgt->reqstd_address,
1674                                     bp_tgt->shadow_contents,
1675                                     CSKY_WR_BKPT_MODE);
1676           if (val)
1677             return val;
1678
1679           /* Write the breakpoint.  */
1680           bp = bp_write_record1;
1681           val = target_write_raw_memory (bp_tgt->reqstd_address,
1682                                          bp, CSKY_WR_BKPT_MODE);
1683         }
1684       else
1685         {
1686           val = target_read_memory (bp_tgt->reqstd_address,
1687                                     bp_tgt->shadow_contents,
1688                                     CSKY_WR_BKPT_MODE);
1689           if (val)
1690             return val;
1691
1692           val = target_read_memory (bp_tgt->reqstd_address - 2,
1693                                     bp_record, 2);
1694           if (val)
1695             return val;
1696
1697           val = target_read_memory (bp_tgt->reqstd_address + 4,
1698                                     bp_record + 2, 2);
1699           if (val)
1700             return val;
1701
1702           bp_write_record1[0] = bp_record[0];
1703           bp_write_record1[1] = bp_record[1];
1704           bp_write_record2[2] = bp_record[2];
1705           bp_write_record2[3] = bp_record[3];
1706
1707           /* Write the breakpoint.  */
1708           bp = bp_write_record1;
1709           val = target_write_raw_memory (bp_tgt->reqstd_address - 2, bp,
1710                                          CSKY_WR_BKPT_MODE);
1711           if (val)
1712             return val;
1713
1714           /* Write the breakpoint.  */
1715           bp = bp_write_record2;
1716           val = target_write_raw_memory (bp_tgt->reqstd_address + 2, bp,
1717                                          CSKY_WR_BKPT_MODE);
1718         }
1719     }
1720   return val;
1721 }
1722
1723 /* Restore the breakpoint shadow_contents to the target.  */
1724
1725 static int
1726 csky_memory_remove_breakpoint (struct gdbarch *gdbarch,
1727                                struct bp_target_info *bp_tgt)
1728 {
1729   int val;
1730   gdb_byte bp_record[] = { 0, 0, 0, 0, 0, 0, 0, 0 };
1731   /* Different for shadow_len 2 or 4.  */
1732   if (bp_tgt->shadow_len == 2)
1733     {
1734       /* Do word-sized writes on word-aligned boundaries and read
1735          padding bytes as necessary.  */
1736       if (bp_tgt->reqstd_address % 4 == 0)
1737         {
1738           val = target_read_memory (bp_tgt->reqstd_address + 2,
1739                                     bp_record + 2, 2);
1740           if (val)
1741             return val;
1742           bp_record[0] = bp_tgt->shadow_contents[0];
1743           bp_record[1] = bp_tgt->shadow_contents[1];
1744           return target_write_raw_memory (bp_tgt->reqstd_address,
1745                                           bp_record, CSKY_WR_BKPT_MODE);
1746         }
1747       else
1748         {
1749           val = target_read_memory (bp_tgt->reqstd_address - 2,
1750                                     bp_record, 2);
1751           if (val)
1752             return val;
1753           bp_record[2] = bp_tgt->shadow_contents[0];
1754           bp_record[3] = bp_tgt->shadow_contents[1];
1755           return target_write_raw_memory (bp_tgt->reqstd_address - 2,
1756                                           bp_record, CSKY_WR_BKPT_MODE);
1757         }
1758     }
1759   else
1760     {
1761       /* Do word-sized writes on word-aligned boundaries and read
1762          padding bytes as necessary.  */
1763       if (bp_tgt->placed_address % 4 == 0)
1764         {
1765           return target_write_raw_memory (bp_tgt->reqstd_address,
1766                                           bp_tgt->shadow_contents,
1767                                           CSKY_WR_BKPT_MODE);
1768         }
1769       else
1770         {
1771           val = target_read_memory (bp_tgt->reqstd_address - 2,
1772                                     bp_record, 2);
1773           if (val)
1774             return val;
1775           val = target_read_memory (bp_tgt->reqstd_address + 4,
1776                                     bp_record+6, 2);
1777           if (val)
1778             return val;
1779
1780           bp_record[2] = bp_tgt->shadow_contents[0];
1781           bp_record[3] = bp_tgt->shadow_contents[1];
1782           bp_record[4] = bp_tgt->shadow_contents[2];
1783           bp_record[5] = bp_tgt->shadow_contents[3];
1784
1785           return target_write_raw_memory (bp_tgt->reqstd_address - 2,
1786                                           bp_record,
1787                                           CSKY_WR_BKPT_MODE * 2);
1788         }
1789     }
1790 }
1791
1792 /* Determine link register type.  */
1793
1794 static lr_type_t
1795 csky_analyze_lr_type (struct gdbarch *gdbarch,
1796                       CORE_ADDR start_pc, CORE_ADDR end_pc)
1797 {
1798   CORE_ADDR addr;
1799   unsigned int insn, insn_len;
1800   insn_len = 2;
1801
1802   for (addr = start_pc; addr < end_pc; addr += insn_len)
1803     {
1804       insn_len = csky_get_insn (gdbarch, addr, &insn);
1805       if (insn_len == 4)
1806         {
1807           if (CSKY_32_IS_MFCR_EPSR (insn) || CSKY_32_IS_MFCR_EPC (insn)
1808               || CSKY_32_IS_RTE (insn))
1809             return LR_TYPE_EPC;
1810         }
1811       else if (CSKY_32_IS_MFCR_FPSR (insn) || CSKY_32_IS_MFCR_FPC (insn)
1812                || CSKY_32_IS_RFI (insn))
1813         return LR_TYPE_FPC;
1814       else if (CSKY_32_IS_JMP (insn) || CSKY_32_IS_BR (insn)
1815                || CSKY_32_IS_JMPIX (insn) || CSKY_32_IS_JMPI (insn))
1816         return LR_TYPE_R15;
1817       else
1818         {
1819           /* 16 bit instruction.  */
1820           if (CSKY_16_IS_JMP (insn) || CSKY_16_IS_BR (insn)
1821               || CSKY_16_IS_JMPIX (insn))
1822             return LR_TYPE_R15;
1823         }
1824     }
1825     return LR_TYPE_R15;
1826 }
1827
1828 /* Heuristic unwinder.  */
1829
1830 static struct csky_unwind_cache *
1831 csky_frame_unwind_cache (struct frame_info *this_frame)
1832 {
1833   CORE_ADDR prologue_start, prologue_end, func_end, prev_pc, block_addr;
1834   struct csky_unwind_cache *cache;
1835   const struct block *bl;
1836   unsigned long func_size = 0;
1837   struct gdbarch *gdbarch = get_frame_arch (this_frame);
1838   unsigned int sp_regnum = CSKY_SP_REGNUM;
1839
1840   /* Default lr type is r15.  */
1841   lr_type_t lr_type = LR_TYPE_R15;
1842
1843   cache = FRAME_OBSTACK_ZALLOC (struct csky_unwind_cache);
1844   cache->saved_regs = trad_frame_alloc_saved_regs (this_frame);
1845
1846   /* Assume there is no frame until proven otherwise.  */
1847   cache->framereg = sp_regnum;
1848
1849   cache->framesize = 0;
1850
1851   prev_pc = get_frame_pc (this_frame);
1852   block_addr = get_frame_address_in_block (this_frame);
1853   if (find_pc_partial_function (block_addr, NULL, &prologue_start,
1854                                 &func_end) == 0)
1855     /* We couldn't find a function containing block_addr, so bail out
1856        and hope for the best.  */
1857     return cache;
1858
1859   /* Get the (function) symbol matching prologue_start.  */
1860   bl = block_for_pc (prologue_start);
1861   if (bl != NULL)
1862     func_size = bl->endaddr - bl->startaddr;
1863   else
1864     {
1865       struct bound_minimal_symbol msymbol
1866         = lookup_minimal_symbol_by_pc (prologue_start);
1867       if (msymbol.minsym != NULL)
1868         func_size = MSYMBOL_SIZE (msymbol.minsym);
1869     }
1870
1871   /* If FUNC_SIZE is 0 we may have a special-case use of lr
1872      e.g. exception or interrupt.  */
1873   if (func_size == 0)
1874     lr_type = csky_analyze_lr_type (gdbarch, prologue_start, func_end);
1875
1876   prologue_end = std::min (func_end, prev_pc);
1877
1878   /* Analyze the function prologue.  */
1879   csky_analyze_prologue (gdbarch, prologue_start, prologue_end,
1880                             func_end, this_frame, cache, lr_type);
1881
1882   /* gdbarch_sp_regnum contains the value and not the address.  */
1883   trad_frame_set_value (cache->saved_regs, sp_regnum, cache->prev_sp);
1884   return cache;
1885 }
1886
1887 /* Implement the unwind_pc gdbarch method.  */
1888
1889 static CORE_ADDR
1890 csky_unwind_pc (struct gdbarch *gdbarch, struct frame_info *next_frame)
1891 {
1892   return frame_unwind_register_unsigned (next_frame, CSKY_PC_REGNUM);
1893 }
1894
1895 /* Implement the this_id function for the normal unwinder.  */
1896
1897 static void
1898 csky_frame_this_id (struct frame_info *this_frame,
1899                     void **this_prologue_cache, struct frame_id *this_id)
1900 {
1901   struct csky_unwind_cache *cache;
1902   struct frame_id id;
1903
1904   if (*this_prologue_cache == NULL)
1905     *this_prologue_cache = csky_frame_unwind_cache (this_frame);
1906   cache = (struct csky_unwind_cache *) *this_prologue_cache;
1907
1908   /* This marks the outermost frame.  */
1909   if (cache->prev_sp == 0)
1910     return;
1911
1912   id = frame_id_build (cache->prev_sp, get_frame_func (this_frame));
1913   *this_id = id;
1914 }
1915
1916 /* Implement the prev_register function for the normal unwinder.  */
1917
1918 static struct value *
1919 csky_frame_prev_register (struct frame_info *this_frame,
1920                           void **this_prologue_cache, int regnum)
1921 {
1922   struct csky_unwind_cache *cache;
1923
1924   if (*this_prologue_cache == NULL)
1925     *this_prologue_cache = csky_frame_unwind_cache (this_frame);
1926   cache = (struct csky_unwind_cache *) *this_prologue_cache;
1927
1928   return trad_frame_get_prev_register (this_frame, cache->saved_regs,
1929                                        regnum);
1930 }
1931
1932 /* Data structures for the normal prologue-analysis-based
1933    unwinder.  */
1934
1935 static const struct frame_unwind csky_unwind_cache = {
1936   NORMAL_FRAME,
1937   default_frame_unwind_stop_reason,
1938   csky_frame_this_id,
1939   csky_frame_prev_register,
1940   NULL,
1941   default_frame_sniffer,
1942   NULL,
1943   NULL
1944 };
1945
1946
1947
1948 static int
1949 csky_stub_unwind_sniffer (const struct frame_unwind *self,
1950                          struct frame_info *this_frame,
1951                          void **this_prologue_cache)
1952 {
1953   CORE_ADDR addr_in_block;
1954
1955   addr_in_block = get_frame_address_in_block (this_frame);
1956
1957   if (find_pc_partial_function (addr_in_block, NULL, NULL, NULL) == 0
1958       || in_plt_section (addr_in_block))
1959     return 1;
1960
1961   return 0;
1962 }
1963
1964 static struct csky_unwind_cache *
1965 csky_make_stub_cache (struct frame_info *this_frame)
1966 {
1967   struct csky_unwind_cache *cache;
1968
1969   cache = FRAME_OBSTACK_ZALLOC (struct csky_unwind_cache);
1970   cache->saved_regs = trad_frame_alloc_saved_regs (this_frame);
1971   cache->prev_sp = get_frame_register_unsigned (this_frame, CSKY_SP_REGNUM);
1972
1973   return cache;
1974 }
1975
1976 static void
1977 csky_stub_this_id (struct frame_info *this_frame,
1978                   void **this_cache,
1979                   struct frame_id *this_id)
1980 {
1981   struct csky_unwind_cache *cache;
1982
1983   if (*this_cache == NULL)
1984     *this_cache = csky_make_stub_cache (this_frame);
1985   cache = (struct csky_unwind_cache *) *this_cache;
1986
1987   /* Our frame ID for a stub frame is the current SP and LR.  */
1988   *this_id = frame_id_build (cache->prev_sp, get_frame_pc (this_frame));
1989 }
1990
1991 static struct value *
1992 csky_stub_prev_register (struct frame_info *this_frame,
1993                             void **this_cache,
1994                             int prev_regnum)
1995 {
1996   struct csky_unwind_cache *cache;
1997
1998   if (*this_cache == NULL)
1999     *this_cache = csky_make_stub_cache (this_frame);
2000   cache = (struct csky_unwind_cache *) *this_cache;
2001
2002   /* If we are asked to unwind the PC, then return the LR.  */
2003   if (prev_regnum == CSKY_PC_REGNUM)
2004     {
2005       CORE_ADDR lr;
2006
2007       lr = frame_unwind_register_unsigned (this_frame, CSKY_LR_REGNUM);
2008       return frame_unwind_got_constant (this_frame, prev_regnum, lr);
2009     }
2010
2011   if (prev_regnum == CSKY_SP_REGNUM)
2012     return frame_unwind_got_constant (this_frame, prev_regnum, cache->prev_sp);
2013
2014   return trad_frame_get_prev_register (this_frame, cache->saved_regs,
2015                                        prev_regnum);
2016 }
2017
2018 struct frame_unwind csky_stub_unwind = {
2019   NORMAL_FRAME,
2020   default_frame_unwind_stop_reason,
2021   csky_stub_this_id,
2022   csky_stub_prev_register,
2023   NULL,
2024   csky_stub_unwind_sniffer
2025 };
2026
2027 /* Implement the this_base, this_locals, and this_args hooks
2028    for the normal unwinder.  */
2029
2030 static CORE_ADDR
2031 csky_frame_base_address (struct frame_info *this_frame, void **this_cache)
2032 {
2033   struct csky_unwind_cache *cache;
2034
2035   if (*this_cache == NULL)
2036     *this_cache = csky_frame_unwind_cache (this_frame);
2037   cache = (struct csky_unwind_cache *) *this_cache;
2038
2039   return cache->prev_sp - cache->framesize;
2040 }
2041
2042 static const struct frame_base csky_frame_base = {
2043   &csky_unwind_cache,
2044   csky_frame_base_address,
2045   csky_frame_base_address,
2046   csky_frame_base_address
2047 };
2048
2049 /* Implement the dummy_id gdbarch method.  The frame ID's base
2050    needs to match the TOS value saved by save_dummy_frame_tos,
2051    and the PC should match the dummy frame's breakpoint.  */
2052
2053 static struct frame_id
2054 csky_dummy_id (struct gdbarch *gdbarch, struct frame_info *this_frame)
2055 {
2056   unsigned int sp_regnum = CSKY_SP_REGNUM;
2057
2058   CORE_ADDR sp = get_frame_register_unsigned (this_frame, sp_regnum);
2059   return frame_id_build (sp, get_frame_pc (this_frame));
2060 }
2061
2062 /* Initialize register access method.  */
2063
2064 static void
2065 csky_dwarf2_frame_init_reg (struct gdbarch *gdbarch, int regnum,
2066                             struct dwarf2_frame_state_reg *reg,
2067                             struct frame_info *this_frame)
2068 {
2069   if (regnum == gdbarch_pc_regnum (gdbarch))
2070     reg->how = DWARF2_FRAME_REG_RA;
2071   else if (regnum == gdbarch_sp_regnum (gdbarch))
2072     reg->how = DWARF2_FRAME_REG_CFA;
2073 }
2074
2075 /* Create csky register groups.  */
2076
2077 static void
2078 csky_init_reggroup ()
2079 {
2080   cr_reggroup = reggroup_new ("cr", USER_REGGROUP);
2081   fr_reggroup = reggroup_new ("fr", USER_REGGROUP);
2082   vr_reggroup = reggroup_new ("vr", USER_REGGROUP);
2083   mmu_reggroup = reggroup_new ("mmu", USER_REGGROUP);
2084   prof_reggroup = reggroup_new ("profiling", USER_REGGROUP);
2085 }
2086
2087 /* Add register groups into reggroup list.  */
2088
2089 static void
2090 csky_add_reggroups (struct gdbarch *gdbarch)
2091 {
2092   reggroup_add (gdbarch, all_reggroup);
2093   reggroup_add (gdbarch, general_reggroup);
2094   reggroup_add (gdbarch, cr_reggroup);
2095   reggroup_add (gdbarch, fr_reggroup);
2096   reggroup_add (gdbarch, vr_reggroup);
2097   reggroup_add (gdbarch, mmu_reggroup);
2098   reggroup_add (gdbarch, prof_reggroup);
2099 }
2100
2101 /* Return the groups that a CSKY register can be categorised into.  */
2102
2103 static int
2104 csky_register_reggroup_p (struct gdbarch *gdbarch, int regnum,
2105                           struct reggroup *reggroup)
2106 {
2107   int raw_p;
2108
2109   if (gdbarch_register_name (gdbarch, regnum) == NULL
2110       || gdbarch_register_name (gdbarch, regnum)[0] == '\0')
2111     return 0;
2112
2113   if (reggroup == all_reggroup)
2114     return 1;
2115
2116   raw_p = regnum < gdbarch_num_regs (gdbarch);
2117   if (reggroup == save_reggroup || reggroup == restore_reggroup)
2118     return raw_p;
2119
2120   if (((regnum >= CSKY_R0_REGNUM) && (regnum <= CSKY_R0_REGNUM + 31))
2121       && (reggroup == general_reggroup))
2122     return 1;
2123
2124   if (((regnum == CSKY_PC_REGNUM)
2125        || ((regnum >= CSKY_CR0_REGNUM)
2126            && (regnum <= CSKY_CR0_REGNUM + 30)))
2127       && (reggroup == cr_reggroup))
2128     return 2;
2129
2130   if ((((regnum >= CSKY_VR0_REGNUM) && (regnum <= CSKY_VR0_REGNUM + 15))
2131        || ((regnum >= CSKY_VCR0_REGNUM)
2132            && (regnum <= CSKY_VCR0_REGNUM + 2)))
2133       && (reggroup == vr_reggroup))
2134     return 3;
2135
2136   if (((regnum >= CSKY_MMU_REGNUM) && (regnum <= CSKY_MMU_REGNUM + 8))
2137       && (reggroup == mmu_reggroup))
2138     return 4;
2139
2140   if (((regnum >= CSKY_PROFCR_REGNUM)
2141        && (regnum <= CSKY_PROFCR_REGNUM + 48))
2142       && (reggroup == prof_reggroup))
2143     return 5;
2144
2145   if ((((regnum >= CSKY_FR0_REGNUM) && (regnum <= CSKY_FR0_REGNUM + 15))
2146        || ((regnum >= CSKY_VCR0_REGNUM) && (regnum <= CSKY_VCR0_REGNUM + 2)))
2147       && (reggroup == fr_reggroup))
2148     return 6;
2149
2150   return 0;
2151 }
2152
2153 /* Implement the dwarf2_reg_to_regnum gdbarch method.  */
2154
2155 static int
2156 csky_dwarf_reg_to_regnum (struct gdbarch *gdbarch, int dw_reg)
2157 {
2158   if (dw_reg < 0 || dw_reg >= CSKY_NUM_REGS)
2159     return -1;
2160   return dw_reg;
2161 }
2162
2163 /* Override interface for command: info register.  */
2164
2165 static void
2166 csky_print_registers_info (struct gdbarch *gdbarch, struct ui_file *file,
2167                            struct frame_info *frame, int regnum, int all)
2168 {
2169   /* Call default print_registers_info function.  */
2170   default_print_registers_info (gdbarch, file, frame, regnum, all);
2171
2172   /* For command: info register.  */
2173   if (regnum == -1 && all == 0)
2174     {
2175       default_print_registers_info (gdbarch, file, frame,
2176                                     CSKY_PC_REGNUM, 0);
2177       default_print_registers_info (gdbarch, file, frame,
2178                                     CSKY_EPC_REGNUM, 0);
2179       default_print_registers_info (gdbarch, file, frame,
2180                                     CSKY_CR0_REGNUM, 0);
2181       default_print_registers_info (gdbarch, file, frame,
2182                                     CSKY_EPSR_REGNUM, 0);
2183     }
2184   return;
2185 }
2186
2187 /* Initialize the current architecture based on INFO.  If possible,
2188    re-use an architecture from ARCHES, which is a list of
2189    architectures already created during this debugging session.
2190
2191    Called at program startup, when reading a core file, and when
2192    reading a binary file.  */
2193
2194 static struct gdbarch *
2195 csky_gdbarch_init (struct gdbarch_info info, struct gdbarch_list *arches)
2196 {
2197   struct gdbarch *gdbarch;
2198   struct gdbarch_tdep *tdep;
2199
2200   /* Find a candidate among the list of pre-declared architectures.  */
2201   arches = gdbarch_list_lookup_by_info (arches, &info);
2202   if (arches != NULL)
2203     return arches->gdbarch;
2204
2205   /* None found, create a new architecture from the information
2206      provided.  */
2207   tdep = XCNEW (struct gdbarch_tdep);
2208   gdbarch = gdbarch_alloc (&info, tdep);
2209
2210   /* Target data types.  */
2211   set_gdbarch_ptr_bit (gdbarch, 32);
2212   set_gdbarch_addr_bit (gdbarch, 32);
2213   set_gdbarch_short_bit (gdbarch, 16);
2214   set_gdbarch_int_bit (gdbarch, 32);
2215   set_gdbarch_long_bit (gdbarch, 32);
2216   set_gdbarch_long_long_bit (gdbarch, 64);
2217   set_gdbarch_float_bit (gdbarch, 32);
2218   set_gdbarch_double_bit (gdbarch, 64);
2219   set_gdbarch_float_format (gdbarch, floatformats_ieee_single);
2220   set_gdbarch_double_format (gdbarch, floatformats_ieee_double);
2221
2222   /* Information about the target architecture.  */
2223   set_gdbarch_return_value (gdbarch, csky_return_value);
2224   set_gdbarch_breakpoint_kind_from_pc (gdbarch, csky_breakpoint_kind_from_pc);
2225   set_gdbarch_sw_breakpoint_from_kind (gdbarch, csky_sw_breakpoint_from_kind);
2226
2227   /* Register architecture.  */
2228   set_gdbarch_num_regs (gdbarch, CSKY_NUM_REGS);
2229   set_gdbarch_pc_regnum (gdbarch, CSKY_PC_REGNUM);
2230   set_gdbarch_sp_regnum (gdbarch, CSKY_SP_REGNUM);
2231   set_gdbarch_register_name (gdbarch, csky_register_name);
2232   set_gdbarch_register_type (gdbarch, csky_register_type);
2233   set_gdbarch_read_pc (gdbarch, csky_read_pc);
2234   set_gdbarch_write_pc (gdbarch, csky_write_pc);
2235   set_gdbarch_print_registers_info (gdbarch, csky_print_registers_info);
2236   csky_add_reggroups (gdbarch);
2237   set_gdbarch_register_reggroup_p (gdbarch, csky_register_reggroup_p);
2238   set_gdbarch_stab_reg_to_regnum (gdbarch, csky_dwarf_reg_to_regnum);
2239   set_gdbarch_dwarf2_reg_to_regnum (gdbarch, csky_dwarf_reg_to_regnum);
2240   dwarf2_frame_set_init_reg (gdbarch, csky_dwarf2_frame_init_reg);
2241
2242   /* Functions to analyze frames.  */
2243   frame_base_set_default (gdbarch, &csky_frame_base);
2244   set_gdbarch_skip_prologue (gdbarch, csky_skip_prologue);
2245   set_gdbarch_inner_than (gdbarch, core_addr_lessthan);
2246   set_gdbarch_frame_align (gdbarch, csky_frame_align);
2247   set_gdbarch_stack_frame_destroyed_p (gdbarch, csky_stack_frame_destroyed_p);
2248
2249   /* Functions to access frame data.  */
2250   set_gdbarch_unwind_pc (gdbarch, csky_unwind_pc);
2251   set_gdbarch_unwind_sp (gdbarch, csky_unwind_sp);
2252
2253   /* Functions handling dummy frames.  */
2254   set_gdbarch_push_dummy_call (gdbarch, csky_push_dummy_call);
2255   set_gdbarch_dummy_id (gdbarch, csky_dummy_id);
2256
2257   /* Frame unwinders.  Use DWARF debug info if available,
2258      otherwise use our own unwinder.  */
2259   dwarf2_append_unwinders (gdbarch);
2260   frame_unwind_append_unwinder (gdbarch, &csky_stub_unwind);
2261   frame_unwind_append_unwinder (gdbarch, &csky_unwind_cache);
2262
2263   /* Breakpoints.  */
2264   set_gdbarch_memory_insert_breakpoint (gdbarch,
2265                                         csky_memory_insert_breakpoint);
2266   set_gdbarch_memory_remove_breakpoint (gdbarch,
2267                                         csky_memory_remove_breakpoint);
2268
2269   /* Hook in ABI-specific overrides, if they have been registered.  */
2270   gdbarch_init_osabi (info, gdbarch);
2271
2272   /* Support simple overlay manager.  */
2273   set_gdbarch_overlay_update (gdbarch, simple_overlay_update);
2274   set_gdbarch_char_signed (gdbarch, 0);
2275   return gdbarch;
2276 }
2277
2278 void
2279 _initialize_csky_tdep (void)
2280 {
2281
2282   register_gdbarch_init (bfd_arch_csky, csky_gdbarch_init);
2283
2284   csky_init_reggroup ();
2285
2286   /* Allow debugging this file's internals.  */
2287   add_setshow_boolean_cmd ("csky", class_maintenance, &csky_debug,
2288                            _("Set C-Sky debugging."),
2289                            _("Show C-Sky debugging."),
2290                            _("When on, C-Sky specific debugging is enabled."),
2291                            NULL,
2292                            NULL,
2293                            &setdebuglist, &showdebuglist);
2294 }