gdbarch software_single_step frame_info to regcache: moxie
[external/binutils.git] / gdb / moxie-tdep.c
1 /* Target-dependent code for Moxie.
2
3    Copyright (C) 2009-2016 Free Software Foundation, Inc.
4
5    This file is part of GDB.
6
7    This program is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 3 of the License, or
10    (at your option) any later version.
11
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16
17    You should have received a copy of the GNU General Public License
18    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
19
20 #include "defs.h"
21 #include "frame.h"
22 #include "frame-unwind.h"
23 #include "frame-base.h"
24 #include "symtab.h"
25 #include "gdbtypes.h"
26 #include "gdbcmd.h"
27 #include "gdbcore.h"
28 #include "value.h"
29 #include "inferior.h"
30 #include "symfile.h"
31 #include "objfiles.h"
32 #include "osabi.h"
33 #include "language.h"
34 #include "arch-utils.h"
35 #include "regcache.h"
36 #include "trad-frame.h"
37 #include "dis-asm.h"
38 #include "record.h"
39 #include "record-full.h"
40
41 #include "moxie-tdep.h"
42 #include <algorithm>
43
44 /* Local functions.  */
45
46 extern void _initialize_moxie_tdep (void);
47
48 /* Use an invalid address value as 'not available' marker.  */
49 enum { REG_UNAVAIL = (CORE_ADDR) -1 };
50
51 struct moxie_frame_cache
52 {
53   /* Base address.  */
54   CORE_ADDR base;
55   CORE_ADDR pc;
56   LONGEST framesize;
57   CORE_ADDR saved_regs[MOXIE_NUM_REGS];
58   CORE_ADDR saved_sp;
59 };
60
61 /* Implement the "frame_align" gdbarch method.  */
62
63 static CORE_ADDR
64 moxie_frame_align (struct gdbarch *gdbarch, CORE_ADDR sp)
65 {
66   /* Align to the size of an instruction (so that they can safely be
67      pushed onto the stack.  */
68   return sp & ~1;
69 }
70
71 constexpr gdb_byte moxie_break_insn[] = { 0x35, 0x00 };
72
73 typedef BP_MANIPULATION (moxie_break_insn) moxie_breakpoint;
74
75 /* Moxie register names.  */
76
77 char *moxie_register_names[] = {
78   "$fp",  "$sp",  "$r0",  "$r1",  "$r2",
79   "$r3",  "$r4",  "$r5", "$r6", "$r7",
80   "$r8", "$r9", "$r10", "$r11", "$r12",
81   "$r13", "$pc", "$cc" };
82
83 /* Implement the "register_name" gdbarch method.  */
84
85 static const char *
86 moxie_register_name (struct gdbarch *gdbarch, int reg_nr)
87 {
88   if (reg_nr < 0)
89     return NULL;
90   if (reg_nr >= MOXIE_NUM_REGS)
91     return NULL;
92   return moxie_register_names[reg_nr];
93 }
94
95 /* Implement the "register_type" gdbarch method.  */
96
97 static struct type *
98 moxie_register_type (struct gdbarch *gdbarch, int reg_nr)
99 {
100   if (reg_nr == MOXIE_PC_REGNUM)
101     return  builtin_type (gdbarch)->builtin_func_ptr;
102   else if (reg_nr == MOXIE_SP_REGNUM || reg_nr == MOXIE_FP_REGNUM)
103     return builtin_type (gdbarch)->builtin_data_ptr;
104   else
105     return builtin_type (gdbarch)->builtin_int32;
106 }
107
108 /* Write into appropriate registers a function return value
109    of type TYPE, given in virtual format.  */
110
111 static void
112 moxie_store_return_value (struct type *type, struct regcache *regcache,
113                          const gdb_byte *valbuf)
114 {
115   struct gdbarch *gdbarch = get_regcache_arch (regcache);
116   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
117   CORE_ADDR regval;
118   int len = TYPE_LENGTH (type);
119
120   /* Things always get returned in RET1_REGNUM, RET2_REGNUM.  */
121   regval = extract_unsigned_integer (valbuf, len > 4 ? 4 : len, byte_order);
122   regcache_cooked_write_unsigned (regcache, RET1_REGNUM, regval);
123   if (len > 4)
124     {
125       regval = extract_unsigned_integer (valbuf + 4, len - 4, byte_order);
126       regcache_cooked_write_unsigned (regcache, RET1_REGNUM + 1, regval);
127     }
128 }
129
130 /* Decode the instructions within the given address range.  Decide
131    when we must have reached the end of the function prologue.  If a
132    frame_info pointer is provided, fill in its saved_regs etc.
133
134    Returns the address of the first instruction after the prologue.  */
135
136 static CORE_ADDR
137 moxie_analyze_prologue (CORE_ADDR start_addr, CORE_ADDR end_addr,
138                         struct moxie_frame_cache *cache,
139                         struct gdbarch *gdbarch)
140 {
141   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
142   CORE_ADDR next_addr;
143   ULONGEST inst, inst2;
144   LONGEST offset;
145   int regnum;
146
147   /* Record where the jsra instruction saves the PC and FP.  */
148   cache->saved_regs[MOXIE_PC_REGNUM] = -4;
149   cache->saved_regs[MOXIE_FP_REGNUM] = 0;
150   cache->framesize = 0;
151
152   if (start_addr >= end_addr)
153     return end_addr;
154
155   for (next_addr = start_addr; next_addr < end_addr; )
156     {
157       inst = read_memory_unsigned_integer (next_addr, 2, byte_order);
158
159       /* Match "push $sp $rN" where N is between 0 and 13 inclusive.  */
160       if (inst >= 0x0612 && inst <= 0x061f)
161         {
162           regnum = inst & 0x000f;
163           cache->framesize += 4;
164           cache->saved_regs[regnum] = cache->framesize;
165           next_addr += 2;
166         }
167       else
168         break;
169     }
170
171   inst = read_memory_unsigned_integer (next_addr, 2, byte_order);
172
173   /* Optional stack allocation for args and local vars <= 4
174      byte.  */
175   if (inst == 0x01e0)          /* ldi.l $r12, X */
176     {
177       offset = read_memory_integer (next_addr + 2, 4, byte_order);
178       inst2 = read_memory_unsigned_integer (next_addr + 6, 2, byte_order);
179       
180       if (inst2 == 0x291e)     /* sub.l $sp, $r12 */
181         {
182           cache->framesize += offset;
183         }
184       
185       return (next_addr + 8);
186     }
187   else if ((inst & 0xff00) == 0x9100)   /* dec $sp, X */
188     {
189       cache->framesize += (inst & 0x00ff);
190       next_addr += 2;
191
192       while (next_addr < end_addr)
193         {
194           inst = read_memory_unsigned_integer (next_addr, 2, byte_order);
195           if ((inst & 0xff00) != 0x9100) /* no more dec $sp, X */
196             break;
197           cache->framesize += (inst & 0x00ff);
198           next_addr += 2;
199         }
200     }
201
202   return next_addr;
203 }
204
205 /* Find the end of function prologue.  */
206
207 static CORE_ADDR
208 moxie_skip_prologue (struct gdbarch *gdbarch, CORE_ADDR pc)
209 {
210   CORE_ADDR func_addr = 0, func_end = 0;
211   const char *func_name;
212
213   /* See if we can determine the end of the prologue via the symbol table.
214      If so, then return either PC, or the PC after the prologue, whichever
215      is greater.  */
216   if (find_pc_partial_function (pc, &func_name, &func_addr, &func_end))
217     {
218       CORE_ADDR post_prologue_pc
219         = skip_prologue_using_sal (gdbarch, func_addr);
220       if (post_prologue_pc != 0)
221         return std::max (pc, post_prologue_pc);
222       else
223         {
224           /* Can't determine prologue from the symbol table, need to examine
225              instructions.  */
226           struct symtab_and_line sal;
227           struct symbol *sym;
228           struct moxie_frame_cache cache;
229           CORE_ADDR plg_end;
230           
231           memset (&cache, 0, sizeof cache);
232           
233           plg_end = moxie_analyze_prologue (func_addr, 
234                                             func_end, &cache, gdbarch);
235           /* Found a function.  */
236           sym = lookup_symbol (func_name, NULL, VAR_DOMAIN, NULL).symbol;
237           /* Don't use line number debug info for assembly source
238              files.  */
239           if (sym && SYMBOL_LANGUAGE (sym) != language_asm)
240             {
241               sal = find_pc_line (func_addr, 0);
242               if (sal.end && sal.end < func_end)
243                 {
244                   /* Found a line number, use it as end of
245                      prologue.  */
246                   return sal.end;
247                 }
248             }
249           /* No useable line symbol.  Use result of prologue parsing
250              method.  */
251           return plg_end;
252         }
253     }
254
255   /* No function symbol -- just return the PC.  */
256   return (CORE_ADDR) pc;
257 }
258
259 struct moxie_unwind_cache
260 {
261   /* The previous frame's inner most stack address.  Used as this
262      frame ID's stack_addr.  */
263   CORE_ADDR prev_sp;
264   /* The frame's base, optionally used by the high-level debug info.  */
265   CORE_ADDR base;
266   int size;
267   /* How far the SP and r13 (FP) have been offset from the start of
268      the stack frame (as defined by the previous frame's stack
269      pointer).  */
270   LONGEST sp_offset;
271   LONGEST r13_offset;
272   int uses_frame;
273   /* Table indicating the location of each and every register.  */
274   struct trad_frame_saved_reg *saved_regs;
275 };
276
277 /* Read an unsigned integer from the inferior, and adjust
278    endianess.  */
279 static ULONGEST
280 moxie_process_readu (CORE_ADDR addr, gdb_byte *buf,
281                      int length, enum bfd_endian byte_order)
282 {
283   if (target_read_memory (addr, buf, length))
284     {
285       if (record_debug)
286         printf_unfiltered (_("Process record: error reading memory at "
287                              "addr 0x%s len = %d.\n"),
288                            paddress (target_gdbarch (), addr), length);
289       return -1;
290     }
291
292   return extract_unsigned_integer (buf, length, byte_order);
293 }
294
295
296 /* Helper macro to extract the signed 10-bit offset from a 16-bit
297    branch instruction.  */
298 #define INST2OFFSET(o) ((((signed short)((o & ((1<<10)-1))<<6))>>6)<<1)
299
300 /* Insert a single step breakpoint.  */
301
302 static VEC (CORE_ADDR) *
303 moxie_software_single_step (struct frame_info *frame)
304 {
305   struct regcache *regcache = get_current_regcache ();
306   struct gdbarch *gdbarch = get_regcache_arch (regcache);
307   CORE_ADDR addr;
308   gdb_byte buf[4];
309   uint16_t inst;
310   uint32_t tmpu32;
311   ULONGEST fp;
312   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
313   VEC (CORE_ADDR) *next_pcs = NULL;
314
315   addr = regcache_read_pc (regcache);
316
317   inst = (uint16_t) moxie_process_readu (addr, buf, 2, byte_order);
318
319   /* Decode instruction.  */
320   if (inst & (1 << 15))
321     {
322       if (inst & (1 << 14))
323         {
324           /* This is a Form 3 instruction.  */
325           int opcode = (inst >> 10 & 0xf);
326
327           switch (opcode)
328             {
329             case 0x00: /* beq */
330             case 0x01: /* bne */
331             case 0x02: /* blt */
332             case 0x03: /* bgt */
333             case 0x04: /* bltu */
334             case 0x05: /* bgtu */
335             case 0x06: /* bge */
336             case 0x07: /* ble */
337             case 0x08: /* bgeu */
338             case 0x09: /* bleu */
339               /* Insert breaks on both branches, because we can't currently tell
340                  which way things will go.  */
341               VEC_safe_push (CORE_ADDR, next_pcs, addr + 2);
342               VEC_safe_push (CORE_ADDR, next_pcs,
343                              addr + 2 + INST2OFFSET(inst));
344               break;
345             default:
346               {
347                 /* Do nothing.  */
348                 break;
349               }
350             }
351         }
352       else
353         {
354           /* This is a Form 2 instruction.  They are all 16 bits.  */
355           VEC_safe_push (CORE_ADDR, next_pcs, addr + 2);
356         }
357     }
358   else
359     {
360       /* This is a Form 1 instruction.  */
361       int opcode = inst >> 8;
362
363       switch (opcode)
364         {
365           /* 16-bit instructions.  */
366         case 0x00: /* bad */
367         case 0x02: /* mov (register-to-register) */
368         case 0x05: /* add.l */
369         case 0x06: /* push */
370         case 0x07: /* pop */
371         case 0x0a: /* ld.l (register indirect) */
372         case 0x0b: /* st.l */
373         case 0x0e: /* cmp */
374         case 0x0f: /* nop */
375         case 0x10: /* sex.b */
376         case 0x11: /* sex.s */
377         case 0x12: /* zex.b */
378         case 0x13: /* zex.s */
379         case 0x14: /* umul.x */
380         case 0x15: /* mul.x */
381         case 0x16:
382         case 0x17:
383         case 0x18:
384         case 0x1c: /* ld.b (register indirect) */
385         case 0x1e: /* st.b */
386         case 0x21: /* ld.s (register indirect) */
387         case 0x23: /* st.s */
388         case 0x26: /* and */
389         case 0x27: /* lshr */
390         case 0x28: /* ashl */
391         case 0x29: /* sub.l */
392         case 0x2a: /* neg */
393         case 0x2b: /* or */
394         case 0x2c: /* not */
395         case 0x2d: /* ashr */
396         case 0x2e: /* xor */
397         case 0x2f: /* mul.l */
398         case 0x31: /* div.l */
399         case 0x32: /* udiv.l */
400         case 0x33: /* mod.l */
401         case 0x34: /* umod.l */
402           VEC_safe_push (CORE_ADDR, next_pcs, addr + 2);
403           break;
404
405           /* 32-bit instructions.  */
406         case 0x0c: /* ldo.l */
407         case 0x0d: /* sto.l */
408         case 0x36: /* ldo.b */
409         case 0x37: /* sto.b */
410         case 0x38: /* ldo.s */
411         case 0x39: /* sto.s */
412           VEC_safe_push (CORE_ADDR, next_pcs, addr + 4);
413           break;
414
415           /* 48-bit instructions.  */
416         case 0x01: /* ldi.l (immediate) */
417         case 0x08: /* lda.l */
418         case 0x09: /* sta.l */
419         case 0x1b: /* ldi.b (immediate) */
420         case 0x1d: /* lda.b */
421         case 0x1f: /* sta.b */
422         case 0x20: /* ldi.s (immediate) */
423         case 0x22: /* lda.s */
424         case 0x24: /* sta.s */
425           VEC_safe_push (CORE_ADDR, next_pcs, addr + 6);
426           break;
427
428           /* Control flow instructions.  */
429         case 0x03: /* jsra */
430         case 0x1a: /* jmpa */
431           VEC_safe_push (CORE_ADDR, next_pcs,
432                          moxie_process_readu (addr + 2, buf, 4, byte_order));
433           break;
434
435         case 0x04: /* ret */
436           regcache_cooked_read_unsigned (regcache, MOXIE_FP_REGNUM, &fp);
437           VEC_safe_push (CORE_ADDR, next_pcs,
438                          moxie_process_readu (fp + 4, buf, 4, byte_order));
439           break;
440
441         case 0x19: /* jsr */
442         case 0x25: /* jmp */
443           regcache_raw_read (regcache,
444                              (inst >> 4) & 0xf, (gdb_byte *) & tmpu32);
445           VEC_safe_push (CORE_ADDR, next_pcs, tmpu32);
446           break;
447
448         case 0x30: /* swi */
449         case 0x35: /* brk */
450           /* Unsupported, for now.  */
451           break;
452         }
453     }
454
455   return next_pcs;
456 }
457
458 /* Implement the "read_pc" gdbarch method.  */
459
460 static CORE_ADDR
461 moxie_read_pc (struct regcache *regcache)
462 {
463   ULONGEST pc;
464
465   regcache_cooked_read_unsigned (regcache, MOXIE_PC_REGNUM, &pc);
466   return pc;
467 }
468
469 /* Implement the "write_pc" gdbarch method.  */
470
471 static void
472 moxie_write_pc (struct regcache *regcache, CORE_ADDR val)
473 {
474   regcache_cooked_write_unsigned (regcache, MOXIE_PC_REGNUM, val);
475 }
476
477 /* Implement the "unwind_sp" gdbarch method.  */
478
479 static CORE_ADDR
480 moxie_unwind_sp (struct gdbarch *gdbarch, struct frame_info *next_frame)
481 {
482   return frame_unwind_register_unsigned (next_frame, MOXIE_SP_REGNUM);
483 }
484
485 /* Given a return value in `regbuf' with a type `valtype', 
486    extract and copy its value into `valbuf'.  */
487
488 static void
489 moxie_extract_return_value (struct type *type, struct regcache *regcache,
490                             gdb_byte *dst)
491 {
492   struct gdbarch *gdbarch = get_regcache_arch (regcache);
493   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
494   int len = TYPE_LENGTH (type);
495   ULONGEST tmp;
496
497   /* By using store_unsigned_integer we avoid having to do
498      anything special for small big-endian values.  */
499   regcache_cooked_read_unsigned (regcache, RET1_REGNUM, &tmp);
500   store_unsigned_integer (dst, (len > 4 ? len - 4 : len), byte_order, tmp);
501
502   /* Ignore return values more than 8 bytes in size because the moxie
503      returns anything more than 8 bytes in the stack.  */
504   if (len > 4)
505     {
506       regcache_cooked_read_unsigned (regcache, RET1_REGNUM + 1, &tmp);
507       store_unsigned_integer (dst + len - 4, 4, byte_order, tmp);
508     }
509 }
510
511 /* Implement the "return_value" gdbarch method.  */
512
513 static enum return_value_convention
514 moxie_return_value (struct gdbarch *gdbarch, struct value *function,
515                    struct type *valtype, struct regcache *regcache,
516                    gdb_byte *readbuf, const gdb_byte *writebuf)
517 {
518   if (TYPE_LENGTH (valtype) > 8)
519     return RETURN_VALUE_STRUCT_CONVENTION;
520   else
521     {
522       if (readbuf != NULL)
523         moxie_extract_return_value (valtype, regcache, readbuf);
524       if (writebuf != NULL)
525         moxie_store_return_value (valtype, regcache, writebuf);
526       return RETURN_VALUE_REGISTER_CONVENTION;
527     }
528 }
529
530 /* Allocate and initialize a moxie_frame_cache object.  */
531
532 static struct moxie_frame_cache *
533 moxie_alloc_frame_cache (void)
534 {
535   struct moxie_frame_cache *cache;
536   int i;
537
538   cache = FRAME_OBSTACK_ZALLOC (struct moxie_frame_cache);
539
540   cache->base = 0;
541   cache->saved_sp = 0;
542   cache->pc = 0;
543   cache->framesize = 0;
544   for (i = 0; i < MOXIE_NUM_REGS; ++i)
545     cache->saved_regs[i] = REG_UNAVAIL;
546
547   return cache;
548 }
549
550 /* Populate a moxie_frame_cache object for this_frame.  */
551
552 static struct moxie_frame_cache *
553 moxie_frame_cache (struct frame_info *this_frame, void **this_cache)
554 {
555   struct moxie_frame_cache *cache;
556   CORE_ADDR current_pc;
557   int i;
558
559   if (*this_cache)
560     return (struct moxie_frame_cache *) *this_cache;
561
562   cache = moxie_alloc_frame_cache ();
563   *this_cache = cache;
564
565   cache->base = get_frame_register_unsigned (this_frame, MOXIE_FP_REGNUM);
566   if (cache->base == 0)
567     return cache;
568
569   cache->pc = get_frame_func (this_frame);
570   current_pc = get_frame_pc (this_frame);
571   if (cache->pc)
572     {
573       struct gdbarch *gdbarch = get_frame_arch (this_frame);
574       moxie_analyze_prologue (cache->pc, current_pc, cache, gdbarch);
575     }
576
577   cache->saved_sp = cache->base - cache->framesize;
578
579   for (i = 0; i < MOXIE_NUM_REGS; ++i)
580     if (cache->saved_regs[i] != REG_UNAVAIL)
581       cache->saved_regs[i] = cache->base - cache->saved_regs[i];
582
583   return cache;
584 }
585
586 /* Implement the "unwind_pc" gdbarch method.  */
587
588 static CORE_ADDR
589 moxie_unwind_pc (struct gdbarch *gdbarch, struct frame_info *next_frame)
590 {
591   return frame_unwind_register_unsigned (next_frame, MOXIE_PC_REGNUM);
592 }
593
594 /* Given a GDB frame, determine the address of the calling function's
595    frame.  This will be used to create a new GDB frame struct.  */
596
597 static void
598 moxie_frame_this_id (struct frame_info *this_frame,
599                     void **this_prologue_cache, struct frame_id *this_id)
600 {
601   struct moxie_frame_cache *cache = moxie_frame_cache (this_frame,
602                                                    this_prologue_cache);
603
604   /* This marks the outermost frame.  */
605   if (cache->base == 0)
606     return;
607
608   *this_id = frame_id_build (cache->saved_sp, cache->pc);
609 }
610
611 /* Get the value of register regnum in the previous stack frame.  */
612
613 static struct value *
614 moxie_frame_prev_register (struct frame_info *this_frame,
615                           void **this_prologue_cache, int regnum)
616 {
617   struct moxie_frame_cache *cache = moxie_frame_cache (this_frame,
618                                                    this_prologue_cache);
619
620   gdb_assert (regnum >= 0);
621
622   if (regnum == MOXIE_SP_REGNUM && cache->saved_sp)
623     return frame_unwind_got_constant (this_frame, regnum, cache->saved_sp);
624
625   if (regnum < MOXIE_NUM_REGS && cache->saved_regs[regnum] != REG_UNAVAIL)
626     return frame_unwind_got_memory (this_frame, regnum,
627                                     cache->saved_regs[regnum]);
628
629   return frame_unwind_got_register (this_frame, regnum, regnum);
630 }
631
632 static const struct frame_unwind moxie_frame_unwind = {
633   NORMAL_FRAME,
634   default_frame_unwind_stop_reason,
635   moxie_frame_this_id,
636   moxie_frame_prev_register,
637   NULL,
638   default_frame_sniffer
639 };
640
641 /* Return the base address of this_frame.  */
642
643 static CORE_ADDR
644 moxie_frame_base_address (struct frame_info *this_frame, void **this_cache)
645 {
646   struct moxie_frame_cache *cache = moxie_frame_cache (this_frame,
647                                                        this_cache);
648
649   return cache->base;
650 }
651
652 static const struct frame_base moxie_frame_base = {
653   &moxie_frame_unwind,
654   moxie_frame_base_address,
655   moxie_frame_base_address,
656   moxie_frame_base_address
657 };
658
659 static struct frame_id
660 moxie_dummy_id (struct gdbarch *gdbarch, struct frame_info *this_frame)
661 {
662   CORE_ADDR sp = get_frame_register_unsigned (this_frame, MOXIE_SP_REGNUM);
663
664   return frame_id_build (sp, get_frame_pc (this_frame));
665 }
666
667 /* Parse the current instruction and record the values of the registers and
668    memory that will be changed in current instruction to "record_arch_list".
669    Return -1 if something wrong.  */
670
671 static int
672 moxie_process_record (struct gdbarch *gdbarch, struct regcache *regcache,
673                       CORE_ADDR addr)
674 {
675   gdb_byte buf[4];
676   uint16_t inst;
677   uint32_t tmpu32;
678   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
679
680   if (record_debug > 1)
681     fprintf_unfiltered (gdb_stdlog, "Process record: moxie_process_record "
682                                     "addr = 0x%s\n",
683                         paddress (target_gdbarch (), addr));
684
685   inst = (uint16_t) moxie_process_readu (addr, buf, 2, byte_order);
686
687   /* Decode instruction.  */
688   if (inst & (1 << 15))
689     {
690       if (inst & (1 << 14))
691         {
692           /* This is a Form 3 instruction.  */
693           int opcode = (inst >> 10 & 0xf);
694           
695           switch (opcode)
696             {
697             case 0x00: /* beq */
698             case 0x01: /* bne */
699             case 0x02: /* blt */
700             case 0x03: /* bgt */
701             case 0x04: /* bltu */
702             case 0x05: /* bgtu */
703             case 0x06: /* bge */
704             case 0x07: /* ble */
705             case 0x08: /* bgeu */
706             case 0x09: /* bleu */
707               /* Do nothing.  */
708               break;
709             default:
710               {
711                 /* Do nothing.  */
712                 break;
713               }
714             }
715         }
716       else
717         {
718           /* This is a Form 2 instruction.  */
719           int opcode = (inst >> 12 & 0x3);
720           switch (opcode)
721             {
722             case 0x00: /* inc */
723             case 0x01: /* dec */
724             case 0x02: /* gsr */
725               {
726                 int reg = (inst >> 8) & 0xf;
727                 if (record_full_arch_list_add_reg (regcache, reg))
728                   return -1;
729               }
730               break;
731             case 0x03: /* ssr */
732               {
733                 /* Do nothing until GDB learns about moxie's special
734                    registers.  */
735               }
736               break;
737             default:
738               /* Do nothing.  */
739               break;
740             }
741         }
742     }
743   else
744     {
745       /* This is a Form 1 instruction.  */
746       int opcode = inst >> 8;
747
748       switch (opcode)
749         {
750         case 0x00: /* nop */
751           /* Do nothing.  */
752           break;
753         case 0x01: /* ldi.l (immediate) */
754         case 0x02: /* mov (register-to-register) */
755           {
756             int reg = (inst >> 4) & 0xf;
757             if (record_full_arch_list_add_reg (regcache, reg))
758               return -1;
759           }
760           break;
761         case 0x03: /* jsra */
762           {
763             regcache_raw_read (regcache, 
764                                MOXIE_SP_REGNUM, (gdb_byte *) & tmpu32);
765             tmpu32 = extract_unsigned_integer ((gdb_byte *) & tmpu32, 
766                                                4, byte_order);
767             if (record_full_arch_list_add_reg (regcache, MOXIE_FP_REGNUM)
768                 || (record_full_arch_list_add_reg (regcache,
769                                                    MOXIE_SP_REGNUM))
770                 || record_full_arch_list_add_mem (tmpu32 - 12, 12))
771               return -1;
772           }
773           break;
774         case 0x04: /* ret */
775           {
776             if (record_full_arch_list_add_reg (regcache, MOXIE_FP_REGNUM)
777                 || (record_full_arch_list_add_reg (regcache,
778                                                    MOXIE_SP_REGNUM)))
779               return -1;
780           }
781           break;
782         case 0x05: /* add.l */
783           {
784             int reg = (inst >> 4) & 0xf;
785             if (record_full_arch_list_add_reg (regcache, reg))
786               return -1;
787           }
788           break;
789         case 0x06: /* push */
790           {
791             int reg = (inst >> 4) & 0xf;
792             regcache_raw_read (regcache, reg, (gdb_byte *) & tmpu32);
793             tmpu32 = extract_unsigned_integer ((gdb_byte *) & tmpu32, 
794                                                4, byte_order);
795             if (record_full_arch_list_add_reg (regcache, reg)
796                 || record_full_arch_list_add_mem (tmpu32 - 4, 4))
797               return -1;
798           }
799           break;
800         case 0x07: /* pop */
801           {
802             int a = (inst >> 4) & 0xf;
803             int b = inst & 0xf;
804             if (record_full_arch_list_add_reg (regcache, a)
805                 || record_full_arch_list_add_reg (regcache, b))
806               return -1;
807           }
808           break;
809         case 0x08: /* lda.l */
810           {
811             int reg = (inst >> 4) & 0xf;
812             if (record_full_arch_list_add_reg (regcache, reg))
813               return -1;
814           }
815           break;
816         case 0x09: /* sta.l */
817           {
818             tmpu32 = (uint32_t) moxie_process_readu (addr+2, buf, 
819                                                      4, byte_order);
820             if (record_full_arch_list_add_mem (tmpu32, 4))
821               return -1;
822           }
823           break;
824         case 0x0a: /* ld.l (register indirect) */
825           {
826             int reg = (inst >> 4) & 0xf;
827             if (record_full_arch_list_add_reg (regcache, reg))
828               return -1;
829           }
830           break;
831         case 0x0b: /* st.l */
832           {
833             int reg = (inst >> 4) & 0xf;
834             regcache_raw_read (regcache, reg, (gdb_byte *) & tmpu32);
835             tmpu32 = extract_unsigned_integer ((gdb_byte *) & tmpu32, 
836                                                4, byte_order);
837             if (record_full_arch_list_add_mem (tmpu32, 4))
838               return -1;
839           }
840           break;
841         case 0x0c: /* ldo.l */
842           {
843             int reg = (inst >> 4) & 0xf;
844             if (record_full_arch_list_add_reg (regcache, reg))
845               return -1;
846           }
847           break;
848         case 0x0d: /* sto.l */
849           {
850             int reg = (inst >> 4) & 0xf;
851             uint32_t offset = (((int16_t) moxie_process_readu (addr+2, buf, 2,
852                                                                byte_order)) << 16 ) >> 16;
853             regcache_raw_read (regcache, reg, (gdb_byte *) & tmpu32);
854             tmpu32 = extract_unsigned_integer ((gdb_byte *) & tmpu32, 
855                                                4, byte_order);
856             tmpu32 += offset;
857             if (record_full_arch_list_add_mem (tmpu32, 4))
858               return -1;
859           }
860           break;
861         case 0x0e: /* cmp */
862           {
863             if (record_full_arch_list_add_reg (regcache, MOXIE_CC_REGNUM))
864               return -1;
865           }
866           break;
867         case 0x0f: /* nop */
868           {
869             /* Do nothing.  */
870             break;
871           }
872         case 0x10: /* sex.b */
873         case 0x11: /* sex.s */
874         case 0x12: /* zex.b */
875         case 0x13: /* zex.s */
876         case 0x14: /* umul.x */
877         case 0x15: /* mul.x */
878           {
879             int reg = (inst >> 4) & 0xf;
880             if (record_full_arch_list_add_reg (regcache, reg))
881               return -1;
882           }
883           break;
884         case 0x16:
885         case 0x17:
886         case 0x18:
887           {
888             /* Do nothing.  */
889             break;
890           }
891         case 0x19: /* jsr */
892           {
893             regcache_raw_read (regcache, 
894                                MOXIE_SP_REGNUM, (gdb_byte *) & tmpu32);
895             tmpu32 = extract_unsigned_integer ((gdb_byte *) & tmpu32, 
896                                                4, byte_order);
897             if (record_full_arch_list_add_reg (regcache, MOXIE_FP_REGNUM)
898                 || (record_full_arch_list_add_reg (regcache,
899                                                    MOXIE_SP_REGNUM))
900                 || record_full_arch_list_add_mem (tmpu32 - 12, 12))
901               return -1;
902           }
903           break;
904         case 0x1a: /* jmpa */
905           {
906             /* Do nothing.  */
907           }
908           break;
909         case 0x1b: /* ldi.b (immediate) */
910         case 0x1c: /* ld.b (register indirect) */
911         case 0x1d: /* lda.b */
912           {
913             int reg = (inst >> 4) & 0xf;
914             if (record_full_arch_list_add_reg (regcache, reg))
915               return -1;
916           }
917           break;
918         case 0x1e: /* st.b */
919           {
920             int reg = (inst >> 4) & 0xf;
921             regcache_raw_read (regcache, reg, (gdb_byte *) & tmpu32);
922             tmpu32 = extract_unsigned_integer ((gdb_byte *) & tmpu32, 
923                                                4, byte_order);
924             if (record_full_arch_list_add_mem (tmpu32, 1))
925               return -1;
926           }
927           break;
928         case 0x1f: /* sta.b */
929           {
930             tmpu32 = moxie_process_readu (addr+2, buf, 4, byte_order);
931             if (record_full_arch_list_add_mem (tmpu32, 1))
932               return -1;
933           }
934           break;
935         case 0x20: /* ldi.s (immediate) */
936         case 0x21: /* ld.s (register indirect) */
937         case 0x22: /* lda.s */
938           {
939             int reg = (inst >> 4) & 0xf;
940             if (record_full_arch_list_add_reg (regcache, reg))
941               return -1;
942           }
943           break;
944         case 0x23: /* st.s */
945           {
946             int reg = (inst >> 4) & 0xf;
947             regcache_raw_read (regcache, reg, (gdb_byte *) & tmpu32);
948             tmpu32 = extract_unsigned_integer ((gdb_byte *) & tmpu32, 
949                                                4, byte_order);
950             if (record_full_arch_list_add_mem (tmpu32, 2))
951               return -1;
952           }
953           break;
954         case 0x24: /* sta.s */
955           {
956             tmpu32 = moxie_process_readu (addr+2, buf, 4, byte_order);
957             if (record_full_arch_list_add_mem (tmpu32, 2))
958               return -1;
959           }
960           break;
961         case 0x25: /* jmp */
962           {
963             /* Do nothing.  */
964           }
965           break;
966         case 0x26: /* and */
967         case 0x27: /* lshr */
968         case 0x28: /* ashl */
969         case 0x29: /* sub */
970         case 0x2a: /* neg */
971         case 0x2b: /* or */
972         case 0x2c: /* not */
973         case 0x2d: /* ashr */
974         case 0x2e: /* xor */
975         case 0x2f: /* mul */
976           {
977             int reg = (inst >> 4) & 0xf;
978             if (record_full_arch_list_add_reg (regcache, reg))
979               return -1;
980           }
981           break;
982         case 0x30: /* swi */
983           {
984             /* We currently implement support for libgloss' 
985                system calls.  */
986
987             int inum = moxie_process_readu (addr+2, buf, 4, byte_order);
988
989             switch (inum)
990               {
991               case 0x1: /* SYS_exit */
992                 {
993                   /* Do nothing.  */
994                 }
995                 break;
996               case 0x2: /* SYS_open */
997                 {
998                   if (record_full_arch_list_add_reg (regcache, RET1_REGNUM))
999                     return -1;
1000                 }
1001                 break;
1002               case 0x4: /* SYS_read */
1003                 {
1004                   uint32_t length, ptr;
1005
1006                   /* Read buffer pointer is in $r1.  */
1007                   regcache_raw_read (regcache, 3, (gdb_byte *) & ptr);
1008                   ptr = extract_unsigned_integer ((gdb_byte *) & ptr, 
1009                                                   4, byte_order);
1010
1011                   /* String length is at 0x12($fp).  */
1012                   regcache_raw_read (regcache, 
1013                                      MOXIE_FP_REGNUM, (gdb_byte *) & tmpu32);
1014                   tmpu32 = extract_unsigned_integer ((gdb_byte *) & tmpu32, 
1015                                                      4, byte_order);
1016                   length = moxie_process_readu (tmpu32+20, buf, 4, byte_order);
1017
1018                   if (record_full_arch_list_add_mem (ptr, length))
1019                     return -1;
1020                 }
1021                 break;
1022               case 0x5: /* SYS_write */
1023                 {
1024                   if (record_full_arch_list_add_reg (regcache, RET1_REGNUM))
1025                     return -1;
1026                 }
1027                 break;
1028               default:
1029                 break;
1030               }
1031           }
1032           break;
1033         case 0x31: /* div.l */
1034         case 0x32: /* udiv.l */
1035         case 0x33: /* mod.l */
1036         case 0x34: /* umod.l */
1037           {
1038             int reg = (inst >> 4) & 0xf;
1039             if (record_full_arch_list_add_reg (regcache, reg))
1040               return -1;
1041           }
1042           break;
1043         case 0x35: /* brk */
1044           /* Do nothing.  */
1045           break;
1046         case 0x36: /* ldo.b */
1047           {
1048             int reg = (inst >> 4) & 0xf;
1049             if (record_full_arch_list_add_reg (regcache, reg))
1050               return -1;
1051           }
1052           break;
1053         case 0x37: /* sto.b */
1054           {
1055             int reg = (inst >> 4) & 0xf;
1056             uint32_t offset = (((int16_t) moxie_process_readu (addr+2, buf, 2,
1057                                                                byte_order)) << 16 ) >> 16;
1058             regcache_raw_read (regcache, reg, (gdb_byte *) & tmpu32);
1059             tmpu32 = extract_unsigned_integer ((gdb_byte *) & tmpu32, 
1060                                                4, byte_order);
1061             tmpu32 += offset;
1062             if (record_full_arch_list_add_mem (tmpu32, 1))
1063               return -1;
1064           }
1065           break;
1066         case 0x38: /* ldo.s */
1067           {
1068             int reg = (inst >> 4) & 0xf;
1069             if (record_full_arch_list_add_reg (regcache, reg))
1070               return -1;
1071           }
1072           break;
1073         case 0x39: /* sto.s */
1074           {
1075             int reg = (inst >> 4) & 0xf;
1076             uint32_t offset = (((int16_t) moxie_process_readu (addr+2, buf, 2,
1077                                                                byte_order)) << 16 ) >> 16;
1078             regcache_raw_read (regcache, reg, (gdb_byte *) & tmpu32);
1079             tmpu32 = extract_unsigned_integer ((gdb_byte *) & tmpu32, 
1080                                                4, byte_order);
1081             tmpu32 += offset;
1082             if (record_full_arch_list_add_mem (tmpu32, 2))
1083               return -1;
1084           }
1085           break;
1086         default:
1087           /* Do nothing.  */
1088           break;
1089         }
1090     }
1091
1092   if (record_full_arch_list_add_reg (regcache, MOXIE_PC_REGNUM))
1093     return -1;
1094   if (record_full_arch_list_add_end ())
1095     return -1;
1096   return 0;
1097 }
1098
1099 /* Allocate and initialize the moxie gdbarch object.  */
1100
1101 static struct gdbarch *
1102 moxie_gdbarch_init (struct gdbarch_info info, struct gdbarch_list *arches)
1103 {
1104   struct gdbarch *gdbarch;
1105   struct gdbarch_tdep *tdep;
1106
1107   /* If there is already a candidate, use it.  */
1108   arches = gdbarch_list_lookup_by_info (arches, &info);
1109   if (arches != NULL)
1110     return arches->gdbarch;
1111
1112   /* Allocate space for the new architecture.  */
1113   tdep = XNEW (struct gdbarch_tdep);
1114   gdbarch = gdbarch_alloc (&info, tdep);
1115
1116   set_gdbarch_read_pc (gdbarch, moxie_read_pc);
1117   set_gdbarch_write_pc (gdbarch, moxie_write_pc);
1118   set_gdbarch_unwind_sp (gdbarch, moxie_unwind_sp);
1119
1120   set_gdbarch_num_regs (gdbarch, MOXIE_NUM_REGS);
1121   set_gdbarch_sp_regnum (gdbarch, MOXIE_SP_REGNUM);
1122   set_gdbarch_pc_regnum (gdbarch, MOXIE_PC_REGNUM);
1123   set_gdbarch_register_name (gdbarch, moxie_register_name);
1124   set_gdbarch_register_type (gdbarch, moxie_register_type);
1125
1126   set_gdbarch_return_value (gdbarch, moxie_return_value);
1127
1128   set_gdbarch_skip_prologue (gdbarch, moxie_skip_prologue);
1129   set_gdbarch_inner_than (gdbarch, core_addr_lessthan);
1130   set_gdbarch_breakpoint_kind_from_pc (gdbarch,
1131                                        moxie_breakpoint::kind_from_pc);
1132   set_gdbarch_sw_breakpoint_from_kind (gdbarch,
1133                                        moxie_breakpoint::bp_from_kind);
1134   set_gdbarch_frame_align (gdbarch, moxie_frame_align);
1135
1136   frame_base_set_default (gdbarch, &moxie_frame_base);
1137
1138   /* Methods for saving / extracting a dummy frame's ID.  The ID's
1139      stack address must match the SP value returned by
1140      PUSH_DUMMY_CALL, and saved by generic_save_dummy_frame_tos.  */
1141   set_gdbarch_dummy_id (gdbarch, moxie_dummy_id);
1142
1143   set_gdbarch_unwind_pc (gdbarch, moxie_unwind_pc);
1144
1145   set_gdbarch_print_insn (gdbarch, print_insn_moxie);
1146
1147   /* Hook in ABI-specific overrides, if they have been registered.  */
1148   gdbarch_init_osabi (info, gdbarch);
1149
1150   /* Hook in the default unwinders.  */
1151   frame_unwind_append_unwinder (gdbarch, &moxie_frame_unwind);
1152
1153   /* Single stepping.  */
1154   set_gdbarch_software_single_step (gdbarch, moxie_software_single_step);
1155
1156   /* Support simple overlay manager.  */
1157   set_gdbarch_overlay_update (gdbarch, simple_overlay_update);
1158
1159   /* Support reverse debugging.  */
1160   set_gdbarch_process_record (gdbarch, moxie_process_record);
1161
1162   return gdbarch;
1163 }
1164
1165 /* Register this machine's init routine.  */
1166
1167 void
1168 _initialize_moxie_tdep (void)
1169 {
1170   register_gdbarch_init (bfd_arch_moxie, moxie_gdbarch_init);
1171 }