xref: /openbsd-src/gnu/usr.bin/binutils/opcodes/openrisc-dis.c (revision cf2f2c5620d6d9a4fd01930983c4b9a1f76d7aa3)
1d2201f2fSdrahn /* Disassembler interface for targets using CGEN. -*- C -*-
2d2201f2fSdrahn    CGEN: Cpu tools GENerator
3d2201f2fSdrahn 
4d2201f2fSdrahn THIS FILE IS MACHINE GENERATED WITH CGEN.
5d2201f2fSdrahn - the resultant file is machine generated, cgen-dis.in isn't
6d2201f2fSdrahn 
7d2201f2fSdrahn Copyright 1996, 1997, 1998, 1999, 2000, 2001, 2002
8d2201f2fSdrahn Free Software Foundation, Inc.
9d2201f2fSdrahn 
10d2201f2fSdrahn This file is part of the GNU Binutils and GDB, the GNU debugger.
11d2201f2fSdrahn 
12d2201f2fSdrahn This program is free software; you can redistribute it and/or modify
13d2201f2fSdrahn it under the terms of the GNU General Public License as published by
14d2201f2fSdrahn the Free Software Foundation; either version 2, or (at your option)
15d2201f2fSdrahn any later version.
16d2201f2fSdrahn 
17d2201f2fSdrahn This program is distributed in the hope that it will be useful,
18d2201f2fSdrahn but WITHOUT ANY WARRANTY; without even the implied warranty of
19d2201f2fSdrahn MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20d2201f2fSdrahn GNU General Public License for more details.
21d2201f2fSdrahn 
22d2201f2fSdrahn You should have received a copy of the GNU General Public License
23d2201f2fSdrahn along with this program; if not, write to the Free Software Foundation, Inc.,
24d2201f2fSdrahn 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
25d2201f2fSdrahn 
26d2201f2fSdrahn /* ??? Eventually more and more of this stuff can go to cpu-independent files.
27d2201f2fSdrahn    Keep that in mind.  */
28d2201f2fSdrahn 
29d2201f2fSdrahn #include "sysdep.h"
30d2201f2fSdrahn #include <stdio.h>
31d2201f2fSdrahn #include "ansidecl.h"
32d2201f2fSdrahn #include "dis-asm.h"
33d2201f2fSdrahn #include "bfd.h"
34d2201f2fSdrahn #include "symcat.h"
35d2201f2fSdrahn #include "libiberty.h"
36d2201f2fSdrahn #include "openrisc-desc.h"
37d2201f2fSdrahn #include "openrisc-opc.h"
38d2201f2fSdrahn #include "opintl.h"
39d2201f2fSdrahn 
40d2201f2fSdrahn /* Default text to print if an instruction isn't recognized.  */
41d2201f2fSdrahn #define UNKNOWN_INSN_MSG _("*unknown*")
42d2201f2fSdrahn 
43d2201f2fSdrahn static void print_normal
44*cf2f2c56Smiod   (CGEN_CPU_DESC, void *, long, unsigned int, bfd_vma, int);
45d2201f2fSdrahn static void print_address
46*cf2f2c56Smiod   (CGEN_CPU_DESC, void *, bfd_vma, unsigned int, bfd_vma, int);
47d2201f2fSdrahn static void print_keyword
48*cf2f2c56Smiod   (CGEN_CPU_DESC, void *, CGEN_KEYWORD *, long, unsigned int);
49d2201f2fSdrahn static void print_insn_normal
50*cf2f2c56Smiod   (CGEN_CPU_DESC, void *, const CGEN_INSN *, CGEN_FIELDS *, bfd_vma, int);
51d2201f2fSdrahn static int print_insn
52*cf2f2c56Smiod   (CGEN_CPU_DESC, bfd_vma,  disassemble_info *, char *, unsigned);
53d2201f2fSdrahn static int default_print_insn
54*cf2f2c56Smiod   (CGEN_CPU_DESC, bfd_vma, disassemble_info *);
55d2201f2fSdrahn static int read_insn
56*cf2f2c56Smiod   (CGEN_CPU_DESC, bfd_vma, disassemble_info *, char *, int, CGEN_EXTRACT_INFO *,
57*cf2f2c56Smiod    unsigned long *);
58d2201f2fSdrahn 
59d2201f2fSdrahn /* -- disassembler routines inserted here */
60d2201f2fSdrahn 
61d2201f2fSdrahn 
62d2201f2fSdrahn void openrisc_cgen_print_operand
63d2201f2fSdrahn   PARAMS ((CGEN_CPU_DESC, int, PTR, CGEN_FIELDS *,
64d2201f2fSdrahn            void const *, bfd_vma, int));
65d2201f2fSdrahn 
66d2201f2fSdrahn /* Main entry point for printing operands.
67d2201f2fSdrahn    XINFO is a `void *' and not a `disassemble_info *' to not put a requirement
68d2201f2fSdrahn    of dis-asm.h on cgen.h.
69d2201f2fSdrahn 
70d2201f2fSdrahn    This function is basically just a big switch statement.  Earlier versions
71d2201f2fSdrahn    used tables to look up the function to use, but
72d2201f2fSdrahn    - if the table contains both assembler and disassembler functions then
73d2201f2fSdrahn      the disassembler contains much of the assembler and vice-versa,
74d2201f2fSdrahn    - there's a lot of inlining possibilities as things grow,
75d2201f2fSdrahn    - using a switch statement avoids the function call overhead.
76d2201f2fSdrahn 
77d2201f2fSdrahn    This function could be moved into `print_insn_normal', but keeping it
78d2201f2fSdrahn    separate makes clear the interface between `print_insn_normal' and each of
79d2201f2fSdrahn    the handlers.  */
80d2201f2fSdrahn 
81d2201f2fSdrahn void
openrisc_cgen_print_operand(cd,opindex,xinfo,fields,attrs,pc,length)82d2201f2fSdrahn openrisc_cgen_print_operand (cd, opindex, xinfo, fields, attrs, pc, length)
83d2201f2fSdrahn      CGEN_CPU_DESC cd;
84d2201f2fSdrahn      int opindex;
85d2201f2fSdrahn      PTR xinfo;
86d2201f2fSdrahn      CGEN_FIELDS *fields;
87d2201f2fSdrahn      void const *attrs ATTRIBUTE_UNUSED;
88d2201f2fSdrahn      bfd_vma pc;
89d2201f2fSdrahn      int length;
90d2201f2fSdrahn {
91d2201f2fSdrahn  disassemble_info *info = (disassemble_info *) xinfo;
92d2201f2fSdrahn 
93d2201f2fSdrahn   switch (opindex)
94d2201f2fSdrahn     {
95d2201f2fSdrahn     case OPENRISC_OPERAND_ABS_26 :
96d2201f2fSdrahn       print_address (cd, info, fields->f_abs26, 0|(1<<CGEN_OPERAND_ABS_ADDR), pc, length);
97d2201f2fSdrahn       break;
98d2201f2fSdrahn     case OPENRISC_OPERAND_DISP_26 :
99d2201f2fSdrahn       print_address (cd, info, fields->f_disp26, 0|(1<<CGEN_OPERAND_PCREL_ADDR), pc, length);
100d2201f2fSdrahn       break;
101d2201f2fSdrahn     case OPENRISC_OPERAND_HI16 :
102d2201f2fSdrahn       print_normal (cd, info, fields->f_simm16, 0|(1<<CGEN_OPERAND_SIGNED)|(1<<CGEN_OPERAND_SIGN_OPT), pc, length);
103d2201f2fSdrahn       break;
104d2201f2fSdrahn     case OPENRISC_OPERAND_LO16 :
105d2201f2fSdrahn       print_normal (cd, info, fields->f_lo16, 0|(1<<CGEN_OPERAND_SIGNED)|(1<<CGEN_OPERAND_SIGN_OPT), pc, length);
106d2201f2fSdrahn       break;
107d2201f2fSdrahn     case OPENRISC_OPERAND_OP_F_23 :
108d2201f2fSdrahn       print_normal (cd, info, fields->f_op4, 0, pc, length);
109d2201f2fSdrahn       break;
110d2201f2fSdrahn     case OPENRISC_OPERAND_OP_F_3 :
111d2201f2fSdrahn       print_normal (cd, info, fields->f_op5, 0, pc, length);
112d2201f2fSdrahn       break;
113d2201f2fSdrahn     case OPENRISC_OPERAND_RA :
114d2201f2fSdrahn       print_keyword (cd, info, & openrisc_cgen_opval_h_gr, fields->f_r2, 0);
115d2201f2fSdrahn       break;
116d2201f2fSdrahn     case OPENRISC_OPERAND_RB :
117d2201f2fSdrahn       print_keyword (cd, info, & openrisc_cgen_opval_h_gr, fields->f_r3, 0);
118d2201f2fSdrahn       break;
119d2201f2fSdrahn     case OPENRISC_OPERAND_RD :
120d2201f2fSdrahn       print_keyword (cd, info, & openrisc_cgen_opval_h_gr, fields->f_r1, 0);
121d2201f2fSdrahn       break;
122d2201f2fSdrahn     case OPENRISC_OPERAND_SIMM_16 :
123d2201f2fSdrahn       print_normal (cd, info, fields->f_simm16, 0|(1<<CGEN_OPERAND_SIGNED), pc, length);
124d2201f2fSdrahn       break;
125d2201f2fSdrahn     case OPENRISC_OPERAND_UI16NC :
126d2201f2fSdrahn       print_normal (cd, info, fields->f_i16nc, 0|(1<<CGEN_OPERAND_SIGNED)|(1<<CGEN_OPERAND_SIGN_OPT)|(1<<CGEN_OPERAND_VIRTUAL), pc, length);
127d2201f2fSdrahn       break;
128d2201f2fSdrahn     case OPENRISC_OPERAND_UIMM_16 :
129d2201f2fSdrahn       print_normal (cd, info, fields->f_uimm16, 0, pc, length);
130d2201f2fSdrahn       break;
131d2201f2fSdrahn     case OPENRISC_OPERAND_UIMM_5 :
132d2201f2fSdrahn       print_normal (cd, info, fields->f_uimm5, 0, pc, length);
133d2201f2fSdrahn       break;
134d2201f2fSdrahn 
135d2201f2fSdrahn     default :
136d2201f2fSdrahn       /* xgettext:c-format */
137d2201f2fSdrahn       fprintf (stderr, _("Unrecognized field %d while printing insn.\n"),
138d2201f2fSdrahn 	       opindex);
139d2201f2fSdrahn     abort ();
140d2201f2fSdrahn   }
141d2201f2fSdrahn }
142d2201f2fSdrahn 
143d2201f2fSdrahn cgen_print_fn * const openrisc_cgen_print_handlers[] =
144d2201f2fSdrahn {
145d2201f2fSdrahn   print_insn_normal,
146d2201f2fSdrahn };
147d2201f2fSdrahn 
148d2201f2fSdrahn 
149d2201f2fSdrahn void
openrisc_cgen_init_dis(cd)150d2201f2fSdrahn openrisc_cgen_init_dis (cd)
151d2201f2fSdrahn      CGEN_CPU_DESC cd;
152d2201f2fSdrahn {
153d2201f2fSdrahn   openrisc_cgen_init_opcode_table (cd);
154d2201f2fSdrahn   openrisc_cgen_init_ibld_table (cd);
155d2201f2fSdrahn   cd->print_handlers = & openrisc_cgen_print_handlers[0];
156d2201f2fSdrahn   cd->print_operand = openrisc_cgen_print_operand;
157d2201f2fSdrahn }
158d2201f2fSdrahn 
159d2201f2fSdrahn 
160d2201f2fSdrahn /* Default print handler.  */
161d2201f2fSdrahn 
162d2201f2fSdrahn static void
print_normal(CGEN_CPU_DESC cd ATTRIBUTE_UNUSED,void * dis_info,long value,unsigned int attrs,bfd_vma pc ATTRIBUTE_UNUSED,int length ATTRIBUTE_UNUSED)163*cf2f2c56Smiod print_normal (CGEN_CPU_DESC cd ATTRIBUTE_UNUSED,
164*cf2f2c56Smiod 	      void *dis_info,
165*cf2f2c56Smiod 	      long value,
166*cf2f2c56Smiod 	      unsigned int attrs,
167*cf2f2c56Smiod 	      bfd_vma pc ATTRIBUTE_UNUSED,
168*cf2f2c56Smiod 	      int length ATTRIBUTE_UNUSED)
169d2201f2fSdrahn {
170d2201f2fSdrahn   disassemble_info *info = (disassemble_info *) dis_info;
171d2201f2fSdrahn 
172d2201f2fSdrahn #ifdef CGEN_PRINT_NORMAL
173d2201f2fSdrahn   CGEN_PRINT_NORMAL (cd, info, value, attrs, pc, length);
174d2201f2fSdrahn #endif
175d2201f2fSdrahn 
176d2201f2fSdrahn   /* Print the operand as directed by the attributes.  */
177d2201f2fSdrahn   if (CGEN_BOOL_ATTR (attrs, CGEN_OPERAND_SEM_ONLY))
178d2201f2fSdrahn     ; /* nothing to do */
179d2201f2fSdrahn   else if (CGEN_BOOL_ATTR (attrs, CGEN_OPERAND_SIGNED))
180d2201f2fSdrahn     (*info->fprintf_func) (info->stream, "%ld", value);
181d2201f2fSdrahn   else
182d2201f2fSdrahn     (*info->fprintf_func) (info->stream, "0x%lx", value);
183d2201f2fSdrahn }
184d2201f2fSdrahn 
185d2201f2fSdrahn /* Default address handler.  */
186d2201f2fSdrahn 
187d2201f2fSdrahn static void
print_address(CGEN_CPU_DESC cd ATTRIBUTE_UNUSED,void * dis_info,bfd_vma value,unsigned int attrs,bfd_vma pc ATTRIBUTE_UNUSED,int length ATTRIBUTE_UNUSED)188*cf2f2c56Smiod print_address (CGEN_CPU_DESC cd ATTRIBUTE_UNUSED,
189*cf2f2c56Smiod 	       void *dis_info,
190*cf2f2c56Smiod 	       bfd_vma value,
191*cf2f2c56Smiod 	       unsigned int attrs,
192*cf2f2c56Smiod 	       bfd_vma pc ATTRIBUTE_UNUSED,
193*cf2f2c56Smiod 	       int length ATTRIBUTE_UNUSED)
194d2201f2fSdrahn {
195d2201f2fSdrahn   disassemble_info *info = (disassemble_info *) dis_info;
196d2201f2fSdrahn 
197d2201f2fSdrahn #ifdef CGEN_PRINT_ADDRESS
198d2201f2fSdrahn   CGEN_PRINT_ADDRESS (cd, info, value, attrs, pc, length);
199d2201f2fSdrahn #endif
200d2201f2fSdrahn 
201d2201f2fSdrahn   /* Print the operand as directed by the attributes.  */
202d2201f2fSdrahn   if (CGEN_BOOL_ATTR (attrs, CGEN_OPERAND_SEM_ONLY))
203d2201f2fSdrahn     ; /* nothing to do */
204d2201f2fSdrahn   else if (CGEN_BOOL_ATTR (attrs, CGEN_OPERAND_PCREL_ADDR))
205d2201f2fSdrahn     (*info->print_address_func) (value, info);
206d2201f2fSdrahn   else if (CGEN_BOOL_ATTR (attrs, CGEN_OPERAND_ABS_ADDR))
207d2201f2fSdrahn     (*info->print_address_func) (value, info);
208d2201f2fSdrahn   else if (CGEN_BOOL_ATTR (attrs, CGEN_OPERAND_SIGNED))
209d2201f2fSdrahn     (*info->fprintf_func) (info->stream, "%ld", (long) value);
210d2201f2fSdrahn   else
211d2201f2fSdrahn     (*info->fprintf_func) (info->stream, "0x%lx", (long) value);
212d2201f2fSdrahn }
213d2201f2fSdrahn 
214d2201f2fSdrahn /* Keyword print handler.  */
215d2201f2fSdrahn 
216d2201f2fSdrahn static void
print_keyword(CGEN_CPU_DESC cd ATTRIBUTE_UNUSED,void * dis_info,CGEN_KEYWORD * keyword_table,long value,unsigned int attrs ATTRIBUTE_UNUSED)217*cf2f2c56Smiod print_keyword (CGEN_CPU_DESC cd ATTRIBUTE_UNUSED,
218*cf2f2c56Smiod 	       void *dis_info,
219*cf2f2c56Smiod 	       CGEN_KEYWORD *keyword_table,
220*cf2f2c56Smiod 	       long value,
221*cf2f2c56Smiod 	       unsigned int attrs ATTRIBUTE_UNUSED)
222d2201f2fSdrahn {
223d2201f2fSdrahn   disassemble_info *info = (disassemble_info *) dis_info;
224d2201f2fSdrahn   const CGEN_KEYWORD_ENTRY *ke;
225d2201f2fSdrahn 
226d2201f2fSdrahn   ke = cgen_keyword_lookup_value (keyword_table, value);
227d2201f2fSdrahn   if (ke != NULL)
228d2201f2fSdrahn     (*info->fprintf_func) (info->stream, "%s", ke->name);
229d2201f2fSdrahn   else
230d2201f2fSdrahn     (*info->fprintf_func) (info->stream, "???");
231d2201f2fSdrahn }
232d2201f2fSdrahn 
233d2201f2fSdrahn /* Default insn printer.
234d2201f2fSdrahn 
235*cf2f2c56Smiod    DIS_INFO is defined as `void *' so the disassembler needn't know anything
236d2201f2fSdrahn    about disassemble_info.  */
237d2201f2fSdrahn 
238d2201f2fSdrahn static void
print_insn_normal(CGEN_CPU_DESC cd,void * dis_info,const CGEN_INSN * insn,CGEN_FIELDS * fields,bfd_vma pc,int length)239*cf2f2c56Smiod print_insn_normal (CGEN_CPU_DESC cd,
240*cf2f2c56Smiod 		   void *dis_info,
241*cf2f2c56Smiod 		   const CGEN_INSN *insn,
242*cf2f2c56Smiod 		   CGEN_FIELDS *fields,
243*cf2f2c56Smiod 		   bfd_vma pc,
244*cf2f2c56Smiod 		   int length)
245d2201f2fSdrahn {
246d2201f2fSdrahn   const CGEN_SYNTAX *syntax = CGEN_INSN_SYNTAX (insn);
247d2201f2fSdrahn   disassemble_info *info = (disassemble_info *) dis_info;
248d2201f2fSdrahn   const CGEN_SYNTAX_CHAR_TYPE *syn;
249d2201f2fSdrahn 
250d2201f2fSdrahn   CGEN_INIT_PRINT (cd);
251d2201f2fSdrahn 
252d2201f2fSdrahn   for (syn = CGEN_SYNTAX_STRING (syntax); *syn; ++syn)
253d2201f2fSdrahn     {
254d2201f2fSdrahn       if (CGEN_SYNTAX_MNEMONIC_P (*syn))
255d2201f2fSdrahn 	{
256d2201f2fSdrahn 	  (*info->fprintf_func) (info->stream, "%s", CGEN_INSN_MNEMONIC (insn));
257d2201f2fSdrahn 	  continue;
258d2201f2fSdrahn 	}
259d2201f2fSdrahn       if (CGEN_SYNTAX_CHAR_P (*syn))
260d2201f2fSdrahn 	{
261d2201f2fSdrahn 	  (*info->fprintf_func) (info->stream, "%c", CGEN_SYNTAX_CHAR (*syn));
262d2201f2fSdrahn 	  continue;
263d2201f2fSdrahn 	}
264d2201f2fSdrahn 
265d2201f2fSdrahn       /* We have an operand.  */
266d2201f2fSdrahn       openrisc_cgen_print_operand (cd, CGEN_SYNTAX_FIELD (*syn), info,
267d2201f2fSdrahn 				 fields, CGEN_INSN_ATTRS (insn), pc, length);
268d2201f2fSdrahn     }
269d2201f2fSdrahn }
270d2201f2fSdrahn 
271d2201f2fSdrahn /* Subroutine of print_insn. Reads an insn into the given buffers and updates
272d2201f2fSdrahn    the extract info.
273d2201f2fSdrahn    Returns 0 if all is well, non-zero otherwise.  */
274d2201f2fSdrahn 
275d2201f2fSdrahn static int
read_insn(CGEN_CPU_DESC cd ATTRIBUTE_UNUSED,bfd_vma pc,disassemble_info * info,char * buf,int buflen,CGEN_EXTRACT_INFO * ex_info,unsigned long * insn_value)276*cf2f2c56Smiod read_insn (CGEN_CPU_DESC cd ATTRIBUTE_UNUSED,
277*cf2f2c56Smiod 	   bfd_vma pc,
278*cf2f2c56Smiod 	   disassemble_info *info,
279*cf2f2c56Smiod 	   char *buf,
280*cf2f2c56Smiod 	   int buflen,
281*cf2f2c56Smiod 	   CGEN_EXTRACT_INFO *ex_info,
282*cf2f2c56Smiod 	   unsigned long *insn_value)
283d2201f2fSdrahn {
284d2201f2fSdrahn   int status = (*info->read_memory_func) (pc, buf, buflen, info);
285d2201f2fSdrahn   if (status != 0)
286d2201f2fSdrahn     {
287d2201f2fSdrahn       (*info->memory_error_func) (status, pc, info);
288d2201f2fSdrahn       return -1;
289d2201f2fSdrahn     }
290d2201f2fSdrahn 
291d2201f2fSdrahn   ex_info->dis_info = info;
292d2201f2fSdrahn   ex_info->valid = (1 << buflen) - 1;
293d2201f2fSdrahn   ex_info->insn_bytes = buf;
294d2201f2fSdrahn 
295d2201f2fSdrahn   *insn_value = bfd_get_bits (buf, buflen * 8, info->endian == BFD_ENDIAN_BIG);
296d2201f2fSdrahn   return 0;
297d2201f2fSdrahn }
298d2201f2fSdrahn 
299d2201f2fSdrahn /* Utility to print an insn.
300d2201f2fSdrahn    BUF is the base part of the insn, target byte order, BUFLEN bytes long.
301d2201f2fSdrahn    The result is the size of the insn in bytes or zero for an unknown insn
302d2201f2fSdrahn    or -1 if an error occurs fetching data (memory_error_func will have
303d2201f2fSdrahn    been called).  */
304d2201f2fSdrahn 
305d2201f2fSdrahn static int
print_insn(CGEN_CPU_DESC cd,bfd_vma pc,disassemble_info * info,char * buf,unsigned int buflen)306*cf2f2c56Smiod print_insn (CGEN_CPU_DESC cd,
307*cf2f2c56Smiod 	    bfd_vma pc,
308*cf2f2c56Smiod 	    disassemble_info *info,
309*cf2f2c56Smiod 	    char *buf,
310*cf2f2c56Smiod 	    unsigned int buflen)
311d2201f2fSdrahn {
312d2201f2fSdrahn   CGEN_INSN_INT insn_value;
313d2201f2fSdrahn   const CGEN_INSN_LIST *insn_list;
314d2201f2fSdrahn   CGEN_EXTRACT_INFO ex_info;
315d2201f2fSdrahn   int basesize;
316d2201f2fSdrahn 
317d2201f2fSdrahn   /* Extract base part of instruction, just in case CGEN_DIS_* uses it. */
318d2201f2fSdrahn   basesize = cd->base_insn_bitsize < buflen * 8 ?
319d2201f2fSdrahn                                      cd->base_insn_bitsize : buflen * 8;
320d2201f2fSdrahn   insn_value = cgen_get_insn_value (cd, buf, basesize);
321d2201f2fSdrahn 
322d2201f2fSdrahn 
323d2201f2fSdrahn   /* Fill in ex_info fields like read_insn would.  Don't actually call
324d2201f2fSdrahn      read_insn, since the incoming buffer is already read (and possibly
325d2201f2fSdrahn      modified a la m32r).  */
326d2201f2fSdrahn   ex_info.valid = (1 << buflen) - 1;
327d2201f2fSdrahn   ex_info.dis_info = info;
328d2201f2fSdrahn   ex_info.insn_bytes = buf;
329d2201f2fSdrahn 
330d2201f2fSdrahn   /* The instructions are stored in hash lists.
331d2201f2fSdrahn      Pick the first one and keep trying until we find the right one.  */
332d2201f2fSdrahn 
333d2201f2fSdrahn   insn_list = CGEN_DIS_LOOKUP_INSN (cd, buf, insn_value);
334d2201f2fSdrahn   while (insn_list != NULL)
335d2201f2fSdrahn     {
336d2201f2fSdrahn       const CGEN_INSN *insn = insn_list->insn;
337d2201f2fSdrahn       CGEN_FIELDS fields;
338d2201f2fSdrahn       int length;
339d2201f2fSdrahn       unsigned long insn_value_cropped;
340d2201f2fSdrahn 
341d2201f2fSdrahn #ifdef CGEN_VALIDATE_INSN_SUPPORTED
342d2201f2fSdrahn       /* Not needed as insn shouldn't be in hash lists if not supported.  */
343d2201f2fSdrahn       /* Supported by this cpu?  */
344d2201f2fSdrahn       if (! openrisc_cgen_insn_supported (cd, insn))
345d2201f2fSdrahn         {
346d2201f2fSdrahn           insn_list = CGEN_DIS_NEXT_INSN (insn_list);
347d2201f2fSdrahn 	  continue;
348d2201f2fSdrahn         }
349d2201f2fSdrahn #endif
350d2201f2fSdrahn 
351d2201f2fSdrahn       /* Basic bit mask must be correct.  */
352d2201f2fSdrahn       /* ??? May wish to allow target to defer this check until the extract
353d2201f2fSdrahn 	 handler.  */
354d2201f2fSdrahn 
355d2201f2fSdrahn       /* Base size may exceed this instruction's size.  Extract the
356d2201f2fSdrahn          relevant part from the buffer. */
357d2201f2fSdrahn       if ((unsigned) (CGEN_INSN_BITSIZE (insn) / 8) < buflen &&
358d2201f2fSdrahn 	  (unsigned) (CGEN_INSN_BITSIZE (insn) / 8) <= sizeof (unsigned long))
359d2201f2fSdrahn 	insn_value_cropped = bfd_get_bits (buf, CGEN_INSN_BITSIZE (insn),
360d2201f2fSdrahn 					   info->endian == BFD_ENDIAN_BIG);
361d2201f2fSdrahn       else
362d2201f2fSdrahn 	insn_value_cropped = insn_value;
363d2201f2fSdrahn 
364d2201f2fSdrahn       if ((insn_value_cropped & CGEN_INSN_BASE_MASK (insn))
365d2201f2fSdrahn 	  == CGEN_INSN_BASE_VALUE (insn))
366d2201f2fSdrahn 	{
367d2201f2fSdrahn 	  /* Printing is handled in two passes.  The first pass parses the
368d2201f2fSdrahn 	     machine insn and extracts the fields.  The second pass prints
369d2201f2fSdrahn 	     them.  */
370d2201f2fSdrahn 
371d2201f2fSdrahn 	  /* Make sure the entire insn is loaded into insn_value, if it
372d2201f2fSdrahn 	     can fit.  */
373d2201f2fSdrahn 	  if (((unsigned) CGEN_INSN_BITSIZE (insn) > cd->base_insn_bitsize) &&
374d2201f2fSdrahn 	      (unsigned) (CGEN_INSN_BITSIZE (insn) / 8) <= sizeof (unsigned long))
375d2201f2fSdrahn 	    {
376d2201f2fSdrahn 	      unsigned long full_insn_value;
377d2201f2fSdrahn 	      int rc = read_insn (cd, pc, info, buf,
378d2201f2fSdrahn 				  CGEN_INSN_BITSIZE (insn) / 8,
379d2201f2fSdrahn 				  & ex_info, & full_insn_value);
380d2201f2fSdrahn 	      if (rc != 0)
381d2201f2fSdrahn 		return rc;
382d2201f2fSdrahn 	      length = CGEN_EXTRACT_FN (cd, insn)
383d2201f2fSdrahn 		(cd, insn, &ex_info, full_insn_value, &fields, pc);
384d2201f2fSdrahn 	    }
385d2201f2fSdrahn 	  else
386d2201f2fSdrahn 	    length = CGEN_EXTRACT_FN (cd, insn)
387d2201f2fSdrahn 	      (cd, insn, &ex_info, insn_value_cropped, &fields, pc);
388d2201f2fSdrahn 
389d2201f2fSdrahn 	  /* length < 0 -> error */
390d2201f2fSdrahn 	  if (length < 0)
391d2201f2fSdrahn 	    return length;
392d2201f2fSdrahn 	  if (length > 0)
393d2201f2fSdrahn 	    {
394d2201f2fSdrahn 	      CGEN_PRINT_FN (cd, insn) (cd, info, insn, &fields, pc, length);
395d2201f2fSdrahn 	      /* length is in bits, result is in bytes */
396d2201f2fSdrahn 	      return length / 8;
397d2201f2fSdrahn 	    }
398d2201f2fSdrahn 	}
399d2201f2fSdrahn 
400d2201f2fSdrahn       insn_list = CGEN_DIS_NEXT_INSN (insn_list);
401d2201f2fSdrahn     }
402d2201f2fSdrahn 
403d2201f2fSdrahn   return 0;
404d2201f2fSdrahn }
405d2201f2fSdrahn 
406d2201f2fSdrahn /* Default value for CGEN_PRINT_INSN.
407d2201f2fSdrahn    The result is the size of the insn in bytes or zero for an unknown insn
408d2201f2fSdrahn    or -1 if an error occured fetching bytes.  */
409d2201f2fSdrahn 
410d2201f2fSdrahn #ifndef CGEN_PRINT_INSN
411d2201f2fSdrahn #define CGEN_PRINT_INSN default_print_insn
412d2201f2fSdrahn #endif
413d2201f2fSdrahn 
414d2201f2fSdrahn static int
default_print_insn(CGEN_CPU_DESC cd,bfd_vma pc,disassemble_info * info)415*cf2f2c56Smiod default_print_insn (CGEN_CPU_DESC cd, bfd_vma pc, disassemble_info *info)
416d2201f2fSdrahn {
417d2201f2fSdrahn   char buf[CGEN_MAX_INSN_SIZE];
418d2201f2fSdrahn   int buflen;
419d2201f2fSdrahn   int status;
420d2201f2fSdrahn 
421d2201f2fSdrahn   /* Attempt to read the base part of the insn.  */
422d2201f2fSdrahn   buflen = cd->base_insn_bitsize / 8;
423d2201f2fSdrahn   status = (*info->read_memory_func) (pc, buf, buflen, info);
424d2201f2fSdrahn 
425d2201f2fSdrahn   /* Try again with the minimum part, if min < base.  */
426d2201f2fSdrahn   if (status != 0 && (cd->min_insn_bitsize < cd->base_insn_bitsize))
427d2201f2fSdrahn     {
428d2201f2fSdrahn       buflen = cd->min_insn_bitsize / 8;
429d2201f2fSdrahn       status = (*info->read_memory_func) (pc, buf, buflen, info);
430d2201f2fSdrahn     }
431d2201f2fSdrahn 
432d2201f2fSdrahn   if (status != 0)
433d2201f2fSdrahn     {
434d2201f2fSdrahn       (*info->memory_error_func) (status, pc, info);
435d2201f2fSdrahn       return -1;
436d2201f2fSdrahn     }
437d2201f2fSdrahn 
438d2201f2fSdrahn   return print_insn (cd, pc, info, buf, buflen);
439d2201f2fSdrahn }
440d2201f2fSdrahn 
441d2201f2fSdrahn /* Main entry point.
442d2201f2fSdrahn    Print one instruction from PC on INFO->STREAM.
443d2201f2fSdrahn    Return the size of the instruction (in bytes).  */
444d2201f2fSdrahn 
445d2201f2fSdrahn typedef struct cpu_desc_list {
446d2201f2fSdrahn   struct cpu_desc_list *next;
447d2201f2fSdrahn   int isa;
448d2201f2fSdrahn   int mach;
449d2201f2fSdrahn   int endian;
450d2201f2fSdrahn   CGEN_CPU_DESC cd;
451d2201f2fSdrahn } cpu_desc_list;
452d2201f2fSdrahn 
453d2201f2fSdrahn int
print_insn_openrisc(bfd_vma pc,disassemble_info * info)454*cf2f2c56Smiod print_insn_openrisc (bfd_vma pc, disassemble_info *info)
455d2201f2fSdrahn {
456d2201f2fSdrahn   static cpu_desc_list *cd_list = 0;
457d2201f2fSdrahn   cpu_desc_list *cl = 0;
458d2201f2fSdrahn   static CGEN_CPU_DESC cd = 0;
459d2201f2fSdrahn   static int prev_isa;
460d2201f2fSdrahn   static int prev_mach;
461d2201f2fSdrahn   static int prev_endian;
462d2201f2fSdrahn   int length;
463d2201f2fSdrahn   int isa,mach;
464d2201f2fSdrahn   int endian = (info->endian == BFD_ENDIAN_BIG
465d2201f2fSdrahn 		? CGEN_ENDIAN_BIG
466d2201f2fSdrahn 		: CGEN_ENDIAN_LITTLE);
467d2201f2fSdrahn   enum bfd_architecture arch;
468d2201f2fSdrahn 
469d2201f2fSdrahn   /* ??? gdb will set mach but leave the architecture as "unknown" */
470d2201f2fSdrahn #ifndef CGEN_BFD_ARCH
471d2201f2fSdrahn #define CGEN_BFD_ARCH bfd_arch_openrisc
472d2201f2fSdrahn #endif
473d2201f2fSdrahn   arch = info->arch;
474d2201f2fSdrahn   if (arch == bfd_arch_unknown)
475d2201f2fSdrahn     arch = CGEN_BFD_ARCH;
476d2201f2fSdrahn 
477d2201f2fSdrahn   /* There's no standard way to compute the machine or isa number
478d2201f2fSdrahn      so we leave it to the target.  */
479d2201f2fSdrahn #ifdef CGEN_COMPUTE_MACH
480d2201f2fSdrahn   mach = CGEN_COMPUTE_MACH (info);
481d2201f2fSdrahn #else
482d2201f2fSdrahn   mach = info->mach;
483d2201f2fSdrahn #endif
484d2201f2fSdrahn 
485d2201f2fSdrahn #ifdef CGEN_COMPUTE_ISA
486d2201f2fSdrahn   isa = CGEN_COMPUTE_ISA (info);
487d2201f2fSdrahn #else
488d2201f2fSdrahn   isa = info->insn_sets;
489d2201f2fSdrahn #endif
490d2201f2fSdrahn 
491d2201f2fSdrahn   /* If we've switched cpu's, try to find a handle we've used before */
492d2201f2fSdrahn   if (cd
493d2201f2fSdrahn       && (isa != prev_isa
494d2201f2fSdrahn 	  || mach != prev_mach
495d2201f2fSdrahn 	  || endian != prev_endian))
496d2201f2fSdrahn     {
497d2201f2fSdrahn       cd = 0;
498d2201f2fSdrahn       for (cl = cd_list; cl; cl = cl->next)
499d2201f2fSdrahn 	{
500d2201f2fSdrahn 	  if (cl->isa == isa &&
501d2201f2fSdrahn 	      cl->mach == mach &&
502d2201f2fSdrahn 	      cl->endian == endian)
503d2201f2fSdrahn 	    {
504d2201f2fSdrahn 	      cd = cl->cd;
505d2201f2fSdrahn 	      break;
506d2201f2fSdrahn 	    }
507d2201f2fSdrahn 	}
508d2201f2fSdrahn     }
509d2201f2fSdrahn 
510d2201f2fSdrahn   /* If we haven't initialized yet, initialize the opcode table.  */
511d2201f2fSdrahn   if (! cd)
512d2201f2fSdrahn     {
513d2201f2fSdrahn       const bfd_arch_info_type *arch_type = bfd_lookup_arch (arch, mach);
514d2201f2fSdrahn       const char *mach_name;
515d2201f2fSdrahn 
516d2201f2fSdrahn       if (!arch_type)
517d2201f2fSdrahn 	abort ();
518d2201f2fSdrahn       mach_name = arch_type->printable_name;
519d2201f2fSdrahn 
520d2201f2fSdrahn       prev_isa = isa;
521d2201f2fSdrahn       prev_mach = mach;
522d2201f2fSdrahn       prev_endian = endian;
523d2201f2fSdrahn       cd = openrisc_cgen_cpu_open (CGEN_CPU_OPEN_ISAS, prev_isa,
524d2201f2fSdrahn 				 CGEN_CPU_OPEN_BFDMACH, mach_name,
525d2201f2fSdrahn 				 CGEN_CPU_OPEN_ENDIAN, prev_endian,
526d2201f2fSdrahn 				 CGEN_CPU_OPEN_END);
527d2201f2fSdrahn       if (!cd)
528d2201f2fSdrahn 	abort ();
529d2201f2fSdrahn 
530d2201f2fSdrahn       /* save this away for future reference */
531d2201f2fSdrahn       cl = xmalloc (sizeof (struct cpu_desc_list));
532d2201f2fSdrahn       cl->cd = cd;
533d2201f2fSdrahn       cl->isa = isa;
534d2201f2fSdrahn       cl->mach = mach;
535d2201f2fSdrahn       cl->endian = endian;
536d2201f2fSdrahn       cl->next = cd_list;
537d2201f2fSdrahn       cd_list = cl;
538d2201f2fSdrahn 
539d2201f2fSdrahn       openrisc_cgen_init_dis (cd);
540d2201f2fSdrahn     }
541d2201f2fSdrahn 
542d2201f2fSdrahn   /* We try to have as much common code as possible.
543d2201f2fSdrahn      But at this point some targets need to take over.  */
544d2201f2fSdrahn   /* ??? Some targets may need a hook elsewhere.  Try to avoid this,
545d2201f2fSdrahn      but if not possible try to move this hook elsewhere rather than
546d2201f2fSdrahn      have two hooks.  */
547d2201f2fSdrahn   length = CGEN_PRINT_INSN (cd, pc, info);
548d2201f2fSdrahn   if (length > 0)
549d2201f2fSdrahn     return length;
550d2201f2fSdrahn   if (length < 0)
551d2201f2fSdrahn     return -1;
552d2201f2fSdrahn 
553d2201f2fSdrahn   (*info->fprintf_func) (info->stream, UNKNOWN_INSN_MSG);
554d2201f2fSdrahn   return cd->default_insn_bitsize / 8;
555d2201f2fSdrahn }
556