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