OSDN Git Service

Print moxie addresses nicely.
[pf3gnuchains/pf3gnuchains4x.git] / opcodes / moxie-dis.c
1 /* Disassemble moxie instructions.
2    Copyright 2009
3    Free Software Foundation, Inc.
4
5    This file is part of the GNU opcodes library.
6
7    This library 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, or (at your option)
10    any later version.
11
12    It is distributed in the hope that it will be useful, but WITHOUT
13    ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
14    or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public
15    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, write to the Free Software
19    Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
20    MA 02110-1301, USA.  */
21
22 #include <stdio.h>
23 #include "sysdep.h"
24 #define STATIC_TABLE
25 #define DEFINE_TABLE
26
27 #include "opcode/moxie.h"
28 #include "dis-asm.h"
29
30 static fprintf_ftype fpr;
31 static void *stream;
32
33 /* Macros to extract operands from the instruction word.  */
34 #define OP_A(i) ((i >> 4) & 0xf)
35 #define OP_B(i) (i & 0xf)
36
37 static const char * reg_names[16] =
38   { "$fp", "$sp", "$r0", "$r1", "$r2", "$r3", "$r4", "$r5",
39     "$r6", "$r7", "$r8", "$r9", "$r10", "$r11", "$r12", "$r13" };
40
41 int
42 print_insn_moxie (bfd_vma addr, struct disassemble_info * info)
43 {
44   int length = 2;
45   int status;
46   stream = info->stream;
47   const moxie_opc_info_t * opcode;
48   bfd_byte buffer[4];
49   unsigned short iword;
50   fpr = info->fprintf_func;
51
52   if ((status = info->read_memory_func (addr, buffer, 2, info)))
53     goto fail;
54   iword = bfd_getb16 (buffer);
55
56   /* Form 1 instructions have the high bit set to 0.  */
57   if ((iword & (1<<15)) == 0)
58     {
59       /* Extract the Form 1 opcode.  */
60       opcode = &moxie_form1_opc_info[iword >> 8];
61       switch (opcode->itype)
62         {
63         case MOXIE_F1_NARG:
64           fpr (stream, "%s", opcode->name);
65           break;
66         case MOXIE_F1_A:
67           fpr (stream, "%s\t%s", opcode->name,
68                reg_names[OP_A(iword)]);
69           break;
70         case MOXIE_F1_AB:
71           fpr (stream, "%s\t%s, %s", opcode->name,
72                reg_names[OP_A(iword)],
73                reg_names[OP_B(iword)]);
74           break;
75         case MOXIE_F1_A4:
76           {
77             unsigned imm;
78             if ((status = info->read_memory_func (addr + 2, buffer, 4, info)))
79               goto fail;
80             imm = bfd_getb32 (buffer);
81             fpr (stream, "%s\t%s, 0x%x", opcode->name,
82                  reg_names[OP_A(iword)], imm);
83             length = 6;
84           }
85           break;
86         case MOXIE_F1_4:
87           {
88             unsigned imm;
89             if ((status = info->read_memory_func (addr + 2, buffer, 4, info)))
90               goto fail;
91             imm = bfd_getb32 (buffer);
92             fpr (stream, "%s\t0x%x", opcode->name, imm);
93             length = 6;
94           }
95           break;
96         case MOXIE_F1_M:
97           {
98             unsigned imm;
99             if ((status = info->read_memory_func (addr + 2, buffer, 4, info)))
100               goto fail;
101             imm = bfd_getb32 (buffer);
102             fpr (stream, "%s\t", opcode->name);
103             info->print_address_func ((bfd_vma) imm, info);
104             length = 6;
105           }
106           break;
107         case MOXIE_F1_AiB:
108           fpr (stream, "%s\t(%s), %s", opcode->name,
109                reg_names[OP_A(iword)], reg_names[OP_B(iword)]);
110           break;
111         case MOXIE_F1_ABi:
112           fpr (stream, "%s\t%s, (%s)", opcode->name,
113                reg_names[OP_A(iword)], reg_names[OP_B(iword)]);
114           break;
115         case MOXIE_F1_4A:
116           {
117             unsigned imm;
118             if ((status = info->read_memory_func (addr + 2, buffer, 4, info)))
119               goto fail;
120             imm = bfd_getb32 (buffer);
121             fpr (stream, "%s\t0x%x, %s",
122                  opcode->name, imm, reg_names[OP_A(iword)]);
123             length = 6;
124           }
125           break;
126         case MOXIE_F1_AiB4:
127           {
128             unsigned imm;
129             if ((status = info->read_memory_func (addr+2, buffer, 4, info)))
130               goto fail;
131             imm = bfd_getb32 (buffer);
132             fpr (stream, "%s\t0x%x(%s), %s", opcode->name,
133                  imm,
134                  reg_names[OP_A(iword)],
135                  reg_names[OP_B(iword)]);
136             length = 6;
137           }
138           break;
139         case MOXIE_F1_ABi4:
140           {
141             unsigned imm;
142             if ((status = info->read_memory_func (addr+2, buffer, 4, info)))
143               goto fail;
144             imm = bfd_getb32 (buffer);
145             fpr (stream, "%s\t%s, 0x%x(%s)",
146                  opcode->name,
147                  reg_names[OP_A(iword)],
148                  imm,
149                  reg_names[OP_B(iword)]);
150             length = 6;
151           }
152           break;
153         default:
154           abort ();
155         }
156     }
157   else if ((iword & (1<<14)) == 0)
158     {
159       /* Extract the Form 2 opcode.  */
160       opcode = &moxie_form2_opc_info[(iword >> 12) & 3];
161       switch (opcode->itype)
162         {
163         case MOXIE_F2_A8V:
164           fpr (stream, "%s\t%s, 0x%x",
165                opcode->name,
166                reg_names[(iword >> 8) & 0xf],
167                iword & ((1 << 8) - 1));
168           break;
169         case MOXIE_F2_NARG:
170           fpr (stream, "%s", opcode->name);
171           break;
172         default:
173           abort();
174         }
175     }
176   else
177     {
178       /* Extract the Form 3 opcode.  */
179       opcode = &moxie_form2_opc_info[(iword >> 12) & 3];
180       switch (opcode->itype)
181         {
182         case MOXIE_F3_NARG:
183           fpr (stream, "%s", opcode->name);
184           break;
185         default:
186           abort();
187         }
188     }
189
190   return length;
191
192  fail:
193   info->memory_error_func (status, addr, info);
194   return -1;
195 }