a45532bf8091a44e54e5c7f6c68140e113cd2878
[platform/upstream/binutils.git] / gdb / spu-tdep.c
1 /* SPU target-dependent code for GDB, the GNU debugger.
2    Copyright (C) 2006, 2007 Free Software Foundation, Inc.
3
4    Contributed by Ulrich Weigand <uweigand@de.ibm.com>.
5    Based on a port by Sid Manning <sid@us.ibm.com>.
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 2 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, write to the Free Software
21    Foundation, Inc., 51 Franklin Street, Fifth Floor,
22    Boston, MA 02110-1301, USA.  */
23
24 #include "defs.h"
25 #include "arch-utils.h"
26 #include "gdbtypes.h"
27 #include "gdbcmd.h"
28 #include "gdbcore.h"
29 #include "gdb_string.h"
30 #include "gdb_assert.h"
31 #include "frame.h"
32 #include "frame-unwind.h"
33 #include "frame-base.h"
34 #include "trad-frame.h"
35 #include "symtab.h"
36 #include "symfile.h"
37 #include "value.h"
38 #include "inferior.h"
39 #include "dis-asm.h"
40 #include "objfiles.h"
41 #include "language.h"
42 #include "regcache.h"
43 #include "reggroups.h"
44 #include "floatformat.h"
45
46 #include "spu-tdep.h"
47
48 /* SPU-specific vector type.  */
49 struct type *spu_builtin_type_vec128;
50
51 /* Registers.  */
52
53 static const char *
54 spu_register_name (int reg_nr)
55 {
56   static char *register_names[] = 
57     {
58       "r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7",
59       "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15",
60       "r16", "r17", "r18", "r19", "r20", "r21", "r22", "r23",
61       "r24", "r25", "r26", "r27", "r28", "r29", "r30", "r31",
62       "r32", "r33", "r34", "r35", "r36", "r37", "r38", "r39",
63       "r40", "r41", "r42", "r43", "r44", "r45", "r46", "r47",
64       "r48", "r49", "r50", "r51", "r52", "r53", "r54", "r55",
65       "r56", "r57", "r58", "r59", "r60", "r61", "r62", "r63",
66       "r64", "r65", "r66", "r67", "r68", "r69", "r70", "r71",
67       "r72", "r73", "r74", "r75", "r76", "r77", "r78", "r79",
68       "r80", "r81", "r82", "r83", "r84", "r85", "r86", "r87",
69       "r88", "r89", "r90", "r91", "r92", "r93", "r94", "r95",
70       "r96", "r97", "r98", "r99", "r100", "r101", "r102", "r103",
71       "r104", "r105", "r106", "r107", "r108", "r109", "r110", "r111",
72       "r112", "r113", "r114", "r115", "r116", "r117", "r118", "r119",
73       "r120", "r121", "r122", "r123", "r124", "r125", "r126", "r127",
74       "id", "pc", "sp"
75     };
76
77   if (reg_nr < 0)
78     return NULL;
79   if (reg_nr >= sizeof register_names / sizeof *register_names)
80     return NULL;
81
82   return register_names[reg_nr];
83 }
84
85 static struct type *
86 spu_register_type (struct gdbarch *gdbarch, int reg_nr)
87 {
88   if (reg_nr < SPU_NUM_GPRS)
89     return spu_builtin_type_vec128;
90
91   switch (reg_nr)
92     {
93     case SPU_ID_REGNUM:
94       return builtin_type_uint32;
95
96     case SPU_PC_REGNUM:
97       return builtin_type_void_func_ptr;
98
99     case SPU_SP_REGNUM:
100       return builtin_type_void_data_ptr;
101
102     default:
103       internal_error (__FILE__, __LINE__, "invalid regnum");
104     }
105 }
106
107 /* Pseudo registers for preferred slots - stack pointer.  */
108
109 static void
110 spu_pseudo_register_read (struct gdbarch *gdbarch, struct regcache *regcache,
111                           int regnum, gdb_byte *buf)
112 {
113   gdb_byte reg[16];
114
115   switch (regnum)
116     {
117     case SPU_SP_REGNUM:
118       regcache_raw_read (regcache, SPU_RAW_SP_REGNUM, reg);
119       memcpy (buf, reg, 4);
120       break;
121
122     default:
123       internal_error (__FILE__, __LINE__, _("invalid regnum"));
124     }
125 }
126
127 static void
128 spu_pseudo_register_write (struct gdbarch *gdbarch, struct regcache *regcache,
129                            int regnum, const gdb_byte *buf)
130 {
131   gdb_byte reg[16];
132
133   switch (regnum)
134     {
135     case SPU_SP_REGNUM:
136       regcache_raw_read (regcache, SPU_RAW_SP_REGNUM, reg);
137       memcpy (reg, buf, 4);
138       regcache_raw_write (regcache, SPU_RAW_SP_REGNUM, reg);
139       break;
140
141     default:
142       internal_error (__FILE__, __LINE__, _("invalid regnum"));
143     }
144 }
145
146 /* Value conversion -- access scalar values at the preferred slot.  */
147
148 static struct value *
149 spu_value_from_register (struct type *type, int regnum,
150                          struct frame_info *frame)
151 {
152   struct value *value = default_value_from_register (type, regnum, frame);
153   int len = TYPE_LENGTH (type);
154
155   if (regnum < SPU_NUM_GPRS && len < 16)
156     {
157       int preferred_slot = len < 4 ? 4 - len : 0;
158       set_value_offset (value, preferred_slot);
159     }
160
161   return value;
162 }
163
164 /* Register groups.  */
165
166 static int
167 spu_register_reggroup_p (struct gdbarch *gdbarch, int regnum,
168                          struct reggroup *group)
169 {
170   /* Registers displayed via 'info regs'.  */
171   if (group == general_reggroup)
172     return 1;
173
174   /* Registers displayed via 'info float'.  */
175   if (group == float_reggroup)
176     return 0;
177
178   /* Registers that need to be saved/restored in order to
179      push or pop frames.  */
180   if (group == save_reggroup || group == restore_reggroup)
181     return 1;
182
183   return default_register_reggroup_p (gdbarch, regnum, group);
184 }
185
186
187 /* Decoding SPU instructions.  */
188
189 enum
190   {
191     op_lqd   = 0x34,
192     op_lqx   = 0x3c4,
193     op_lqa   = 0x61,
194     op_lqr   = 0x67,
195     op_stqd  = 0x24,
196     op_stqx  = 0x144,
197     op_stqa  = 0x41,
198     op_stqr  = 0x47,
199
200     op_il    = 0x081,
201     op_ila   = 0x21,
202     op_a     = 0x0c0,
203     op_ai    = 0x1c,
204
205     op_selb  = 0x4,
206
207     op_br    = 0x64,
208     op_bra   = 0x60,
209     op_brsl  = 0x66,
210     op_brasl = 0x62,
211     op_brnz  = 0x42,
212     op_brz   = 0x40,
213     op_brhnz = 0x46,
214     op_brhz  = 0x44,
215     op_bi    = 0x1a8,
216     op_bisl  = 0x1a9,
217     op_biz   = 0x128,
218     op_binz  = 0x129,
219     op_bihz  = 0x12a,
220     op_bihnz = 0x12b,
221   };
222
223 static int
224 is_rr (unsigned int insn, int op, int *rt, int *ra, int *rb)
225 {
226   if ((insn >> 21) == op)
227     {
228       *rt = insn & 127;
229       *ra = (insn >> 7) & 127;
230       *rb = (insn >> 14) & 127;
231       return 1;
232     }
233
234   return 0;
235 }
236
237 static int
238 is_rrr (unsigned int insn, int op, int *rt, int *ra, int *rb, int *rc)
239 {
240   if ((insn >> 28) == op)
241     {
242       *rt = (insn >> 21) & 127;
243       *ra = (insn >> 7) & 127;
244       *rb = (insn >> 14) & 127;
245       *rc = insn & 127;
246       return 1;
247     }
248
249   return 0;
250 }
251
252 static int
253 is_ri7 (unsigned int insn, int op, int *rt, int *ra, int *i7)
254 {
255   if ((insn >> 21) == op)
256     {
257       *rt = insn & 127;
258       *ra = (insn >> 7) & 127;
259       *i7 = (((insn >> 14) & 127) ^ 0x40) - 0x40;
260       return 1;
261     }
262
263   return 0;
264 }
265
266 static int
267 is_ri10 (unsigned int insn, int op, int *rt, int *ra, int *i10)
268 {
269   if ((insn >> 24) == op)
270     {
271       *rt = insn & 127;
272       *ra = (insn >> 7) & 127;
273       *i10 = (((insn >> 14) & 0x3ff) ^ 0x200) - 0x200;
274       return 1;
275     }
276
277   return 0;
278 }
279
280 static int
281 is_ri16 (unsigned int insn, int op, int *rt, int *i16)
282 {
283   if ((insn >> 23) == op)
284     {
285       *rt = insn & 127;
286       *i16 = (((insn >> 7) & 0xffff) ^ 0x8000) - 0x8000;
287       return 1;
288     }
289
290   return 0;
291 }
292
293 static int
294 is_ri18 (unsigned int insn, int op, int *rt, int *i18)
295 {
296   if ((insn >> 25) == op)
297     {
298       *rt = insn & 127;
299       *i18 = (((insn >> 7) & 0x3ffff) ^ 0x20000) - 0x20000;
300       return 1;
301     }
302
303   return 0;
304 }
305
306 static int
307 is_branch (unsigned int insn, int *offset, int *reg)
308 {
309   int rt, i7, i16;
310
311   if (is_ri16 (insn, op_br, &rt, &i16)
312       || is_ri16 (insn, op_brsl, &rt, &i16)
313       || is_ri16 (insn, op_brnz, &rt, &i16)
314       || is_ri16 (insn, op_brz, &rt, &i16)
315       || is_ri16 (insn, op_brhnz, &rt, &i16)
316       || is_ri16 (insn, op_brhz, &rt, &i16))
317     {
318       *reg = SPU_PC_REGNUM;
319       *offset = i16 << 2;
320       return 1;
321     }
322
323   if (is_ri16 (insn, op_bra, &rt, &i16)
324       || is_ri16 (insn, op_brasl, &rt, &i16))
325     {
326       *reg = -1;
327       *offset = i16 << 2;
328       return 1;
329     }
330
331   if (is_ri7 (insn, op_bi, &rt, reg, &i7)
332       || is_ri7 (insn, op_bisl, &rt, reg, &i7)
333       || is_ri7 (insn, op_biz, &rt, reg, &i7)
334       || is_ri7 (insn, op_binz, &rt, reg, &i7)
335       || is_ri7 (insn, op_bihz, &rt, reg, &i7)
336       || is_ri7 (insn, op_bihnz, &rt, reg, &i7))
337     {
338       *offset = 0;
339       return 1;
340     }
341
342   return 0;
343 }
344
345
346 /* Prolog parsing.  */
347
348 struct spu_prologue_data
349   {
350     /* Stack frame size.  -1 if analysis was unsuccessful.  */
351     int size;
352
353     /* How to find the CFA.  The CFA is equal to SP at function entry.  */
354     int cfa_reg;
355     int cfa_offset;
356
357     /* Offset relative to CFA where a register is saved.  -1 if invalid.  */
358     int reg_offset[SPU_NUM_GPRS];
359   };
360
361 static CORE_ADDR
362 spu_analyze_prologue (CORE_ADDR start_pc, CORE_ADDR end_pc,
363                       struct spu_prologue_data *data)
364 {
365   int found_sp = 0;
366   int found_fp = 0;
367   int found_lr = 0;
368   int reg_immed[SPU_NUM_GPRS];
369   gdb_byte buf[16];
370   CORE_ADDR prolog_pc = start_pc;
371   CORE_ADDR pc;
372   int i;
373
374
375   /* Initialize DATA to default values.  */
376   data->size = -1;
377
378   data->cfa_reg = SPU_RAW_SP_REGNUM;
379   data->cfa_offset = 0;
380
381   for (i = 0; i < SPU_NUM_GPRS; i++)
382     data->reg_offset[i] = -1;
383
384   /* Set up REG_IMMED array.  This is non-zero for a register if we know its
385      preferred slot currently holds this immediate value.  */
386   for (i = 0; i < SPU_NUM_GPRS; i++)
387       reg_immed[i] = 0;
388
389   /* Scan instructions until the first branch.
390
391      The following instructions are important prolog components:
392
393         - The first instruction to set up the stack pointer.
394         - The first instruction to set up the frame pointer.
395         - The first instruction to save the link register.
396
397      We return the instruction after the latest of these three,
398      or the incoming PC if none is found.  The first instruction
399      to set up the stack pointer also defines the frame size.
400
401      Note that instructions saving incoming arguments to their stack
402      slots are not counted as important, because they are hard to
403      identify with certainty.  This should not matter much, because
404      arguments are relevant only in code compiled with debug data,
405      and in such code the GDB core will advance until the first source
406      line anyway, using SAL data.
407
408      For purposes of stack unwinding, we analyze the following types
409      of instructions in addition:
410
411       - Any instruction adding to the current frame pointer.
412       - Any instruction loading an immediate constant into a register.
413       - Any instruction storing a register onto the stack.
414
415      These are used to compute the CFA and REG_OFFSET output.  */
416
417   for (pc = start_pc; pc < end_pc; pc += 4)
418     {
419       unsigned int insn;
420       int rt, ra, rb, rc, immed;
421
422       if (target_read_memory (pc, buf, 4))
423         break;
424       insn = extract_unsigned_integer (buf, 4);
425
426       /* AI is the typical instruction to set up a stack frame.
427          It is also used to initialize the frame pointer.  */
428       if (is_ri10 (insn, op_ai, &rt, &ra, &immed))
429         {
430           if (rt == data->cfa_reg && ra == data->cfa_reg)
431             data->cfa_offset -= immed;
432
433           if (rt == SPU_RAW_SP_REGNUM && ra == SPU_RAW_SP_REGNUM
434               && !found_sp)
435             {
436               found_sp = 1;
437               prolog_pc = pc + 4;
438
439               data->size = -immed;
440             }
441           else if (rt == SPU_FP_REGNUM && ra == SPU_RAW_SP_REGNUM
442                    && !found_fp)
443             {
444               found_fp = 1;
445               prolog_pc = pc + 4;
446
447               data->cfa_reg = SPU_FP_REGNUM;
448               data->cfa_offset -= immed;
449             }
450         }
451
452       /* A is used to set up stack frames of size >= 512 bytes.
453          If we have tracked the contents of the addend register,
454          we can handle this as well.  */
455       else if (is_rr (insn, op_a, &rt, &ra, &rb))
456         {
457           if (rt == data->cfa_reg && ra == data->cfa_reg)
458             {
459               if (reg_immed[rb] != 0)
460                 data->cfa_offset -= reg_immed[rb];
461               else
462                 data->cfa_reg = -1;  /* We don't know the CFA any more.  */
463             }
464
465           if (rt == SPU_RAW_SP_REGNUM && ra == SPU_RAW_SP_REGNUM
466               && !found_sp)
467             {
468               found_sp = 1;
469               prolog_pc = pc + 4;
470
471               if (reg_immed[rb] != 0)
472                 data->size = -reg_immed[rb];
473             }
474         }
475
476       /* We need to track IL and ILA used to load immediate constants
477          in case they are later used as input to an A instruction.  */
478       else if (is_ri16 (insn, op_il, &rt, &immed))
479         {
480           reg_immed[rt] = immed;
481
482           if (rt == SPU_RAW_SP_REGNUM && !found_sp)
483             found_sp = 1;
484         }
485
486       else if (is_ri18 (insn, op_ila, &rt, &immed))
487         {
488           reg_immed[rt] = immed & 0x3ffff;
489
490           if (rt == SPU_RAW_SP_REGNUM && !found_sp)
491             found_sp = 1;
492         }
493
494       /* STQD is used to save registers to the stack.  */
495       else if (is_ri10 (insn, op_stqd, &rt, &ra, &immed))
496         {
497           if (ra == data->cfa_reg)
498             data->reg_offset[rt] = data->cfa_offset - (immed << 4);
499
500           if (ra == data->cfa_reg && rt == SPU_LR_REGNUM
501               && !found_lr)
502             {
503               found_lr = 1;
504               prolog_pc = pc + 4;
505             }
506         }
507
508       /* _start uses SELB to set up the stack pointer.  */
509       else if (is_rrr (insn, op_selb, &rt, &ra, &rb, &rc))
510         {
511           if (rt == SPU_RAW_SP_REGNUM && !found_sp)
512             found_sp = 1;
513         }
514
515       /* We terminate if we find a branch.  */
516       else if (is_branch (insn, &immed, &ra))
517         break;
518     }
519
520
521   /* If we successfully parsed until here, and didn't find any instruction
522      modifying SP, we assume we have a frameless function.  */
523   if (!found_sp)
524     data->size = 0;
525
526   /* Return cooked instead of raw SP.  */
527   if (data->cfa_reg == SPU_RAW_SP_REGNUM)
528     data->cfa_reg = SPU_SP_REGNUM;
529
530   return prolog_pc;
531 }
532
533 /* Return the first instruction after the prologue starting at PC.  */
534 static CORE_ADDR
535 spu_skip_prologue (CORE_ADDR pc)
536 {
537   struct spu_prologue_data data;
538   return spu_analyze_prologue (pc, (CORE_ADDR)-1, &data);
539 }
540
541 /* Return the frame pointer in use at address PC.  */
542 static void
543 spu_virtual_frame_pointer (CORE_ADDR pc, int *reg, LONGEST *offset)
544 {
545   struct spu_prologue_data data;
546   spu_analyze_prologue (pc, (CORE_ADDR)-1, &data);
547
548   if (data.size != -1 && data.cfa_reg != -1)
549     {
550       /* The 'frame pointer' address is CFA minus frame size.  */
551       *reg = data.cfa_reg;
552       *offset = data.cfa_offset - data.size;
553     }
554   else
555     {
556       /* ??? We don't really know ... */
557       *reg = SPU_SP_REGNUM;
558       *offset = 0;
559     }
560 }
561
562 /* Return true if we are in the function's epilogue, i.e. after the
563    instruction that destroyed the function's stack frame.
564
565    1) scan forward from the point of execution:
566        a) If you find an instruction that modifies the stack pointer
567           or transfers control (except a return), execution is not in
568           an epilogue, return.
569        b) Stop scanning if you find a return instruction or reach the
570           end of the function or reach the hard limit for the size of
571           an epilogue.
572    2) scan backward from the point of execution:
573         a) If you find an instruction that modifies the stack pointer,
574             execution *is* in an epilogue, return.
575         b) Stop scanning if you reach an instruction that transfers
576            control or the beginning of the function or reach the hard
577            limit for the size of an epilogue.  */
578
579 static int
580 spu_in_function_epilogue_p (struct gdbarch *gdbarch, CORE_ADDR pc)
581 {
582   CORE_ADDR scan_pc, func_start, func_end, epilogue_start, epilogue_end;
583   bfd_byte buf[4];
584   unsigned int insn;
585   int rt, ra, rb, rc, immed;
586
587   /* Find the search limits based on function boundaries and hard limit.
588      We assume the epilogue can be up to 64 instructions long.  */
589
590   const int spu_max_epilogue_size = 64 * 4;
591
592   if (!find_pc_partial_function (pc, NULL, &func_start, &func_end))
593     return 0;
594
595   if (pc - func_start < spu_max_epilogue_size)
596     epilogue_start = func_start;
597   else
598     epilogue_start = pc - spu_max_epilogue_size;
599
600   if (func_end - pc < spu_max_epilogue_size)
601     epilogue_end = func_end;
602   else
603     epilogue_end = pc + spu_max_epilogue_size;
604
605   /* Scan forward until next 'bi $0'.  */
606
607   for (scan_pc = pc; scan_pc < epilogue_end; scan_pc += 4)
608     {
609       if (target_read_memory (scan_pc, buf, 4))
610         return 0;
611       insn = extract_unsigned_integer (buf, 4);
612
613       if (is_branch (insn, &immed, &ra))
614         {
615           if (immed == 0 && ra == SPU_LR_REGNUM)
616             break;
617
618           return 0;
619         }
620
621       if (is_ri10 (insn, op_ai, &rt, &ra, &immed)
622           || is_rr (insn, op_a, &rt, &ra, &rb)
623           || is_ri10 (insn, op_lqd, &rt, &ra, &immed))
624         {
625           if (rt == SPU_RAW_SP_REGNUM)
626             return 0;
627         }
628     }
629
630   if (scan_pc >= epilogue_end)
631     return 0;
632
633   /* Scan backward until adjustment to stack pointer (R1).  */
634
635   for (scan_pc = pc - 4; scan_pc >= epilogue_start; scan_pc -= 4)
636     {
637       if (target_read_memory (scan_pc, buf, 4))
638         return 0;
639       insn = extract_unsigned_integer (buf, 4);
640
641       if (is_branch (insn, &immed, &ra))
642         return 0;
643
644       if (is_ri10 (insn, op_ai, &rt, &ra, &immed)
645           || is_rr (insn, op_a, &rt, &ra, &rb)
646           || is_ri10 (insn, op_lqd, &rt, &ra, &immed))
647         {
648           if (rt == SPU_RAW_SP_REGNUM)
649             return 1;
650         }
651     }
652
653   return 0;
654 }
655
656
657 /* Normal stack frames.  */
658
659 struct spu_unwind_cache
660 {
661   CORE_ADDR func;
662   CORE_ADDR frame_base;
663   CORE_ADDR local_base;
664
665   struct trad_frame_saved_reg *saved_regs;
666 };
667
668 static struct spu_unwind_cache *
669 spu_frame_unwind_cache (struct frame_info *next_frame,
670                         void **this_prologue_cache)
671 {
672   struct spu_unwind_cache *info;
673   struct spu_prologue_data data;
674
675   if (*this_prologue_cache)
676     return *this_prologue_cache;
677
678   info = FRAME_OBSTACK_ZALLOC (struct spu_unwind_cache);
679   *this_prologue_cache = info;
680   info->saved_regs = trad_frame_alloc_saved_regs (next_frame);
681   info->frame_base = 0;
682   info->local_base = 0;
683
684   /* Find the start of the current function, and analyze its prologue.  */
685   info->func = frame_func_unwind (next_frame, NORMAL_FRAME);
686   if (info->func == 0)
687     {
688       /* Fall back to using the current PC as frame ID.  */
689       info->func = frame_pc_unwind (next_frame);
690       data.size = -1;
691     }
692   else
693     spu_analyze_prologue (info->func, frame_pc_unwind (next_frame), &data);
694
695
696   /* If successful, use prologue analysis data.  */
697   if (data.size != -1 && data.cfa_reg != -1)
698     {
699       CORE_ADDR cfa;
700       int i;
701       gdb_byte buf[16];
702
703       /* Determine CFA via unwound CFA_REG plus CFA_OFFSET.  */
704       frame_unwind_register (next_frame, data.cfa_reg, buf);
705       cfa = extract_unsigned_integer (buf, 4) + data.cfa_offset;
706
707       /* Call-saved register slots.  */
708       for (i = 0; i < SPU_NUM_GPRS; i++)
709         if (i == SPU_LR_REGNUM
710             || (i >= SPU_SAVED1_REGNUM && i <= SPU_SAVEDN_REGNUM))
711           if (data.reg_offset[i] != -1)
712             info->saved_regs[i].addr = cfa - data.reg_offset[i];
713
714       /* The previous PC comes from the link register.  */
715       if (trad_frame_addr_p (info->saved_regs, SPU_LR_REGNUM))
716         info->saved_regs[SPU_PC_REGNUM] = info->saved_regs[SPU_LR_REGNUM];
717       else
718         info->saved_regs[SPU_PC_REGNUM].realreg = SPU_LR_REGNUM;
719
720       /* The previous SP is equal to the CFA.  */
721       trad_frame_set_value (info->saved_regs, SPU_SP_REGNUM, cfa);
722
723       /* Frame bases.  */
724       info->frame_base = cfa;
725       info->local_base = cfa - data.size;
726     }
727
728   /* Otherwise, fall back to reading the backchain link.  */
729   else
730     {
731       CORE_ADDR reg, backchain;
732
733       /* Get the backchain.  */
734       reg = frame_unwind_register_unsigned (next_frame, SPU_SP_REGNUM);
735       backchain = read_memory_unsigned_integer (reg, 4);
736
737       /* A zero backchain terminates the frame chain.  Also, sanity
738          check against the local store size limit.  */
739       if (backchain != 0 && backchain < SPU_LS_SIZE)
740         {
741           /* Assume the link register is saved into its slot.  */
742           if (backchain + 16 < SPU_LS_SIZE)
743             info->saved_regs[SPU_LR_REGNUM].addr = backchain + 16;
744
745           /* This will also be the previous PC.  */
746           if (trad_frame_addr_p (info->saved_regs, SPU_LR_REGNUM))
747             info->saved_regs[SPU_PC_REGNUM] = info->saved_regs[SPU_LR_REGNUM];
748           else
749             info->saved_regs[SPU_PC_REGNUM].realreg = SPU_LR_REGNUM;
750
751           /* The previous SP will equal the backchain value.  */
752           trad_frame_set_value (info->saved_regs, SPU_SP_REGNUM, backchain);
753
754           /* Frame bases.  */
755           info->frame_base = backchain;
756           info->local_base = reg;
757         }
758     }
759  
760   return info;
761 }
762
763 static void
764 spu_frame_this_id (struct frame_info *next_frame,
765                    void **this_prologue_cache, struct frame_id *this_id)
766 {
767   struct spu_unwind_cache *info =
768     spu_frame_unwind_cache (next_frame, this_prologue_cache);
769
770   if (info->frame_base == 0)
771     return;
772
773   *this_id = frame_id_build (info->frame_base, info->func);
774 }
775
776 static void
777 spu_frame_prev_register (struct frame_info *next_frame,
778                          void **this_prologue_cache,
779                          int regnum, int *optimizedp,
780                          enum lval_type *lvalp, CORE_ADDR * addrp,
781                          int *realnump, gdb_byte *bufferp)
782 {
783   struct spu_unwind_cache *info
784     = spu_frame_unwind_cache (next_frame, this_prologue_cache);
785
786   /* Special-case the stack pointer.  */
787   if (regnum == SPU_RAW_SP_REGNUM)
788     regnum = SPU_SP_REGNUM;
789
790   trad_frame_get_prev_register (next_frame, info->saved_regs, regnum,
791                                 optimizedp, lvalp, addrp, realnump, bufferp);
792 }
793
794 static const struct frame_unwind spu_frame_unwind = {
795   NORMAL_FRAME,
796   spu_frame_this_id,
797   spu_frame_prev_register
798 };
799
800 const struct frame_unwind *
801 spu_frame_sniffer (struct frame_info *next_frame)
802 {
803   return &spu_frame_unwind;
804 }
805
806 static CORE_ADDR
807 spu_frame_base_address (struct frame_info *next_frame, void **this_cache)
808 {
809   struct spu_unwind_cache *info
810     = spu_frame_unwind_cache (next_frame, this_cache);
811   return info->local_base;
812 }
813
814 static const struct frame_base spu_frame_base = {
815   &spu_frame_unwind,
816   spu_frame_base_address,
817   spu_frame_base_address,
818   spu_frame_base_address
819 };
820
821 static CORE_ADDR
822 spu_unwind_pc (struct gdbarch *gdbarch, struct frame_info *next_frame)
823 {
824   CORE_ADDR pc = frame_unwind_register_unsigned (next_frame, SPU_PC_REGNUM);
825   /* Mask off interrupt enable bit.  */
826   return pc & -4;
827 }
828
829 static CORE_ADDR
830 spu_unwind_sp (struct gdbarch *gdbarch, struct frame_info *next_frame)
831 {
832   return frame_unwind_register_unsigned (next_frame, SPU_SP_REGNUM);
833 }
834
835 static CORE_ADDR
836 spu_read_pc (ptid_t ptid)
837 {
838   CORE_ADDR pc = read_register_pid (SPU_PC_REGNUM, ptid);
839   /* Mask off interrupt enable bit.  */
840   return pc & -4;
841 }
842
843 static void
844 spu_write_pc (CORE_ADDR pc, ptid_t ptid)
845 {
846   /* Keep interrupt enabled state unchanged.  */
847   CORE_ADDR old_pc = read_register_pid (SPU_PC_REGNUM, ptid);
848   write_register_pid (SPU_PC_REGNUM, (pc & -4) | (old_pc & 3), ptid);
849 }
850
851
852 /* Function calling convention.  */
853
854 static CORE_ADDR
855 spu_frame_align (struct gdbarch *gdbarch, CORE_ADDR sp)
856 {
857   return sp & ~15;
858 }
859
860 static int
861 spu_scalar_value_p (struct type *type)
862 {
863   switch (TYPE_CODE (type))
864     {
865     case TYPE_CODE_INT:
866     case TYPE_CODE_ENUM:
867     case TYPE_CODE_RANGE:
868     case TYPE_CODE_CHAR:
869     case TYPE_CODE_BOOL:
870     case TYPE_CODE_PTR:
871     case TYPE_CODE_REF:
872       return TYPE_LENGTH (type) <= 16;
873
874     default:
875       return 0;
876     }
877 }
878
879 static void
880 spu_value_to_regcache (struct regcache *regcache, int regnum,
881                        struct type *type, const gdb_byte *in)
882 {
883   int len = TYPE_LENGTH (type);
884
885   if (spu_scalar_value_p (type))
886     {
887       int preferred_slot = len < 4 ? 4 - len : 0;
888       regcache_cooked_write_part (regcache, regnum, preferred_slot, len, in);
889     }
890   else
891     {
892       while (len >= 16)
893         {
894           regcache_cooked_write (regcache, regnum++, in);
895           in += 16;
896           len -= 16;
897         }
898
899       if (len > 0)
900         regcache_cooked_write_part (regcache, regnum, 0, len, in);
901     }
902 }
903
904 static void
905 spu_regcache_to_value (struct regcache *regcache, int regnum,
906                        struct type *type, gdb_byte *out)
907 {
908   int len = TYPE_LENGTH (type);
909
910   if (spu_scalar_value_p (type))
911     {
912       int preferred_slot = len < 4 ? 4 - len : 0;
913       regcache_cooked_read_part (regcache, regnum, preferred_slot, len, out);
914     }
915   else
916     {
917       while (len >= 16)
918         {
919           regcache_cooked_read (regcache, regnum++, out);
920           out += 16;
921           len -= 16;
922         }
923
924       if (len > 0)
925         regcache_cooked_read_part (regcache, regnum, 0, len, out);
926     }
927 }
928
929 static CORE_ADDR
930 spu_push_dummy_call (struct gdbarch *gdbarch, struct value *function,
931                      struct regcache *regcache, CORE_ADDR bp_addr,
932                      int nargs, struct value **args, CORE_ADDR sp,
933                      int struct_return, CORE_ADDR struct_addr)
934 {
935   int i;
936   int regnum = SPU_ARG1_REGNUM;
937   int stack_arg = -1;
938   gdb_byte buf[16];
939
940   /* Set the return address.  */
941   memset (buf, 0, sizeof buf);
942   store_unsigned_integer (buf, 4, bp_addr);
943   regcache_cooked_write (regcache, SPU_LR_REGNUM, buf);
944
945   /* If STRUCT_RETURN is true, then the struct return address (in
946      STRUCT_ADDR) will consume the first argument-passing register.
947      Both adjust the register count and store that value.  */
948   if (struct_return)
949     {
950       memset (buf, 0, sizeof buf);
951       store_unsigned_integer (buf, 4, struct_addr);
952       regcache_cooked_write (regcache, regnum++, buf);
953     }
954
955   /* Fill in argument registers.  */
956   for (i = 0; i < nargs; i++)
957     {
958       struct value *arg = args[i];
959       struct type *type = check_typedef (value_type (arg));
960       const gdb_byte *contents = value_contents (arg);
961       int len = TYPE_LENGTH (type);
962       int n_regs = align_up (len, 16) / 16;
963
964       /* If the argument doesn't wholly fit into registers, it and
965          all subsequent arguments go to the stack.  */
966       if (regnum + n_regs - 1 > SPU_ARGN_REGNUM)
967         {
968           stack_arg = i;
969           break;
970         }
971
972       spu_value_to_regcache (regcache, regnum, type, contents);
973       regnum += n_regs;
974     }
975
976   /* Overflow arguments go to the stack.  */
977   if (stack_arg != -1)
978     {
979       CORE_ADDR ap;
980
981       /* Allocate all required stack size.  */
982       for (i = stack_arg; i < nargs; i++)
983         {
984           struct type *type = check_typedef (value_type (args[i]));
985           sp -= align_up (TYPE_LENGTH (type), 16);
986         }
987
988       /* Fill in stack arguments.  */
989       ap = sp;
990       for (i = stack_arg; i < nargs; i++)
991         {
992           struct value *arg = args[i];
993           struct type *type = check_typedef (value_type (arg));
994           int len = TYPE_LENGTH (type);
995           int preferred_slot;
996           
997           if (spu_scalar_value_p (type))
998             preferred_slot = len < 4 ? 4 - len : 0;
999           else
1000             preferred_slot = 0;
1001
1002           target_write_memory (ap + preferred_slot, value_contents (arg), len);
1003           ap += align_up (TYPE_LENGTH (type), 16);
1004         }
1005     }
1006
1007   /* Allocate stack frame header.  */
1008   sp -= 32;
1009
1010   /* Finally, update the SP register.  */
1011   regcache_cooked_write_unsigned (regcache, SPU_SP_REGNUM, sp);
1012
1013   return sp;
1014 }
1015
1016 static struct frame_id
1017 spu_unwind_dummy_id (struct gdbarch *gdbarch, struct frame_info *next_frame)
1018 {
1019   return frame_id_build (spu_unwind_sp (gdbarch, next_frame),
1020                          spu_unwind_pc (gdbarch, next_frame));
1021 }
1022
1023 /* Function return value access.  */
1024
1025 static enum return_value_convention
1026 spu_return_value (struct gdbarch *gdbarch, struct type *type,
1027                   struct regcache *regcache, gdb_byte *out, const gdb_byte *in)
1028 {
1029   enum return_value_convention rvc;
1030
1031   if (TYPE_LENGTH (type) <= (SPU_ARGN_REGNUM - SPU_ARG1_REGNUM + 1) * 16)
1032     rvc = RETURN_VALUE_REGISTER_CONVENTION;
1033   else
1034     rvc = RETURN_VALUE_STRUCT_CONVENTION;
1035
1036   if (in)
1037     {
1038       switch (rvc)
1039         {
1040         case RETURN_VALUE_REGISTER_CONVENTION:
1041           spu_value_to_regcache (regcache, SPU_ARG1_REGNUM, type, in);
1042           break;
1043
1044         case RETURN_VALUE_STRUCT_CONVENTION:
1045           error ("Cannot set function return value.");
1046           break;
1047         }
1048     }
1049   else if (out)
1050     {
1051       switch (rvc)
1052         {
1053         case RETURN_VALUE_REGISTER_CONVENTION:
1054           spu_regcache_to_value (regcache, SPU_ARG1_REGNUM, type, out);
1055           break;
1056
1057         case RETURN_VALUE_STRUCT_CONVENTION:
1058           error ("Function return value unknown.");
1059           break;
1060         }
1061     }
1062
1063   return rvc;
1064 }
1065
1066
1067 /* Breakpoints.  */
1068
1069 static const gdb_byte *
1070 spu_breakpoint_from_pc (CORE_ADDR * pcptr, int *lenptr)
1071 {
1072   static const gdb_byte breakpoint[] = { 0x00, 0x00, 0x3f, 0xff };
1073
1074   *lenptr = sizeof breakpoint;
1075   return breakpoint;
1076 }
1077
1078
1079 /* Software single-stepping support.  */
1080
1081 int
1082 spu_software_single_step (struct regcache *regcache)
1083 {
1084   CORE_ADDR pc, next_pc;
1085   unsigned int insn;
1086   int offset, reg;
1087   gdb_byte buf[4];
1088
1089   regcache_cooked_read (regcache, SPU_PC_REGNUM, buf);
1090   /* Mask off interrupt enable bit.  */
1091   pc = extract_unsigned_integer (buf, 4) & -4;
1092
1093   if (target_read_memory (pc, buf, 4))
1094     return 1;
1095   insn = extract_unsigned_integer (buf, 4);
1096
1097   /* Next sequential instruction is at PC + 4, except if the current
1098      instruction is a PPE-assisted call, in which case it is at PC + 8.
1099      Wrap around LS limit to be on the safe side.  */
1100   if ((insn & 0xffffff00) == 0x00002100)
1101     next_pc = (pc + 8) & (SPU_LS_SIZE - 1);
1102   else
1103     next_pc = (pc + 4) & (SPU_LS_SIZE - 1);
1104
1105   insert_single_step_breakpoint (next_pc);
1106
1107   if (is_branch (insn, &offset, &reg))
1108     {
1109       CORE_ADDR target = offset;
1110
1111       if (reg == SPU_PC_REGNUM)
1112         target += pc;
1113       else if (reg != -1)
1114         {
1115           regcache_cooked_read_part (regcache, reg, 0, 4, buf);
1116           target += extract_unsigned_integer (buf, 4) & -4;
1117         }
1118
1119       target = target & (SPU_LS_SIZE - 1);
1120       if (target != next_pc)
1121         insert_single_step_breakpoint (target);
1122     }
1123
1124   return 1;
1125 }
1126
1127
1128 /* Set up gdbarch struct.  */
1129
1130 static struct gdbarch *
1131 spu_gdbarch_init (struct gdbarch_info info, struct gdbarch_list *arches)
1132 {
1133   struct gdbarch *gdbarch;
1134
1135   /* Find a candidate among the list of pre-declared architectures.  */
1136   arches = gdbarch_list_lookup_by_info (arches, &info);
1137   if (arches != NULL)
1138     return arches->gdbarch;
1139
1140   /* Is is for us?  */
1141   if (info.bfd_arch_info->mach != bfd_mach_spu)
1142     return NULL;
1143
1144   /* Yes, create a new architecture.  */
1145   gdbarch = gdbarch_alloc (&info, NULL);
1146
1147   /* Disassembler.  */
1148   set_gdbarch_print_insn (gdbarch, print_insn_spu);
1149
1150   /* Registers.  */
1151   set_gdbarch_num_regs (gdbarch, SPU_NUM_REGS);
1152   set_gdbarch_num_pseudo_regs (gdbarch, SPU_NUM_PSEUDO_REGS);
1153   set_gdbarch_sp_regnum (gdbarch, SPU_SP_REGNUM);
1154   set_gdbarch_pc_regnum (gdbarch, SPU_PC_REGNUM);
1155   set_gdbarch_read_pc (gdbarch, spu_read_pc);
1156   set_gdbarch_write_pc (gdbarch, spu_write_pc);
1157   set_gdbarch_register_name (gdbarch, spu_register_name);
1158   set_gdbarch_register_type (gdbarch, spu_register_type);
1159   set_gdbarch_pseudo_register_read (gdbarch, spu_pseudo_register_read);
1160   set_gdbarch_pseudo_register_write (gdbarch, spu_pseudo_register_write);
1161   set_gdbarch_value_from_register (gdbarch, spu_value_from_register);
1162   set_gdbarch_register_reggroup_p (gdbarch, spu_register_reggroup_p);
1163
1164   /* Data types.  */
1165   set_gdbarch_char_signed (gdbarch, 0);
1166   set_gdbarch_ptr_bit (gdbarch, 32);
1167   set_gdbarch_addr_bit (gdbarch, 32);
1168   set_gdbarch_short_bit (gdbarch, 16);
1169   set_gdbarch_int_bit (gdbarch, 32);
1170   set_gdbarch_long_bit (gdbarch, 32);
1171   set_gdbarch_long_long_bit (gdbarch, 64);
1172   set_gdbarch_float_bit (gdbarch, 32);
1173   set_gdbarch_double_bit (gdbarch, 64);
1174   set_gdbarch_long_double_bit (gdbarch, 64);
1175   set_gdbarch_float_format (gdbarch, floatformats_ieee_single);
1176   set_gdbarch_double_format (gdbarch, floatformats_ieee_double);
1177   set_gdbarch_long_double_format (gdbarch, floatformats_ieee_double);
1178
1179   /* Inferior function calls.  */
1180   set_gdbarch_call_dummy_location (gdbarch, ON_STACK);
1181   set_gdbarch_frame_align (gdbarch, spu_frame_align);
1182   set_gdbarch_push_dummy_call (gdbarch, spu_push_dummy_call);
1183   set_gdbarch_unwind_dummy_id (gdbarch, spu_unwind_dummy_id);
1184   set_gdbarch_return_value (gdbarch, spu_return_value);
1185
1186   /* Frame handling.  */
1187   set_gdbarch_inner_than (gdbarch, core_addr_lessthan);
1188   frame_unwind_append_sniffer (gdbarch, spu_frame_sniffer);
1189   frame_base_set_default (gdbarch, &spu_frame_base);
1190   set_gdbarch_unwind_pc (gdbarch, spu_unwind_pc);
1191   set_gdbarch_unwind_sp (gdbarch, spu_unwind_sp);
1192   set_gdbarch_virtual_frame_pointer (gdbarch, spu_virtual_frame_pointer);
1193   set_gdbarch_frame_args_skip (gdbarch, 0);
1194   set_gdbarch_skip_prologue (gdbarch, spu_skip_prologue);
1195   set_gdbarch_in_function_epilogue_p (gdbarch, spu_in_function_epilogue_p);
1196
1197   /* Breakpoints.  */
1198   set_gdbarch_decr_pc_after_break (gdbarch, 4);
1199   set_gdbarch_breakpoint_from_pc (gdbarch, spu_breakpoint_from_pc);
1200   set_gdbarch_cannot_step_breakpoint (gdbarch, 1);
1201   set_gdbarch_software_single_step (gdbarch, spu_software_single_step);
1202
1203   return gdbarch;
1204 }
1205
1206 /* Implement a SPU-specific vector type as replacement
1207    for __gdb_builtin_type_vec128.  */
1208 static void
1209 spu_init_vector_type (void)
1210 {
1211   struct type *type;
1212
1213   type = init_composite_type ("__spu_builtin_type_vec128", TYPE_CODE_UNION);
1214   append_composite_type_field (type, "uint128", builtin_type_int128);
1215   append_composite_type_field (type, "v2_int64", builtin_type_v2_int64);
1216   append_composite_type_field (type, "v4_int32", builtin_type_v4_int32);
1217   append_composite_type_field (type, "v8_int16", builtin_type_v8_int16);
1218   append_composite_type_field (type, "v16_int8", builtin_type_v16_int8);
1219   append_composite_type_field (type, "v2_double", builtin_type_v2_double);
1220   append_composite_type_field (type, "v4_float", builtin_type_v4_float);
1221
1222   TYPE_FLAGS (type) |= TYPE_FLAG_VECTOR;
1223   TYPE_NAME (type) = "spu_builtin_type_vec128";
1224   spu_builtin_type_vec128 = type;
1225 }
1226
1227 void
1228 _initialize_spu_tdep (void)
1229 {
1230   register_gdbarch_init (bfd_arch_spu, spu_gdbarch_init);
1231
1232   spu_init_vector_type ();
1233 }