source: trunk/src/binutils/opcodes/cgen-dis.in@ 1036

Last change on this file since 1036 was 610, checked in by bird, 22 years ago

This commit was generated by cvs2svn to compensate for changes in r609,
which included commits to RCS files with non-trunk default branches.

  • Property cvs2svn:cvs-rev set to 1.1.1.2
  • Property svn:eol-style set to native
  • Property svn:executable set to *
File size: 13.2 KB
Line 
1/* Disassembler interface for targets using CGEN. -*- C -*-
2 CGEN: Cpu tools GENerator
3
4THIS FILE IS MACHINE GENERATED WITH CGEN.
5- the resultant file is machine generated, cgen-dis.in isn't
6
7Copyright 1996, 1997, 1998, 1999, 2000, 2001, 2002
8Free Software Foundation, Inc.
9
10This file is part of the GNU Binutils and GDB, the GNU debugger.
11
12This program is free software; you can redistribute it and/or modify
13it under the terms of the GNU General Public License as published by
14the Free Software Foundation; either version 2, or (at your option)
15any later version.
16
17This program is distributed in the hope that it will be useful,
18but WITHOUT ANY WARRANTY; without even the implied warranty of
19MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20GNU General Public License for more details.
21
22You should have received a copy of the GNU General Public License
23along with this program; if not, write to the Free Software Foundation, Inc.,
2459 Temple Place - Suite 330, Boston, MA 02111-1307, 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 "@prefix@-desc.h"
37#include "@prefix@-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
43static void print_normal
44 PARAMS ((CGEN_CPU_DESC, PTR, long, unsigned int, bfd_vma, int));
45static void print_address
46 PARAMS ((CGEN_CPU_DESC, PTR, bfd_vma, unsigned int, bfd_vma, int));
47static void print_keyword
48 PARAMS ((CGEN_CPU_DESC, PTR, CGEN_KEYWORD *, long, unsigned int));
49static void print_insn_normal
50 PARAMS ((CGEN_CPU_DESC, PTR, const CGEN_INSN *, CGEN_FIELDS *,
51 bfd_vma, int));
52static int print_insn
53 PARAMS ((CGEN_CPU_DESC, bfd_vma, disassemble_info *, char *, unsigned));
54static int default_print_insn
55 PARAMS ((CGEN_CPU_DESC, bfd_vma, disassemble_info *));
56static int read_insn
57 PARAMS ((CGEN_CPU_DESC, bfd_vma, disassemble_info *, char *, int,
58 CGEN_EXTRACT_INFO *, unsigned long *));
59
60
61/* -- disassembler routines inserted here */
62
63
64/* Default print handler. */
65
66static void
67print_normal (cd, dis_info, value, attrs, pc, length)
68 CGEN_CPU_DESC cd ATTRIBUTE_UNUSED;
69 PTR dis_info;
70 long value;
71 unsigned int attrs;
72 bfd_vma pc ATTRIBUTE_UNUSED;
73 int length ATTRIBUTE_UNUSED;
74{
75 disassemble_info *info = (disassemble_info *) dis_info;
76
77#ifdef CGEN_PRINT_NORMAL
78 CGEN_PRINT_NORMAL (cd, info, value, attrs, pc, length);
79#endif
80
81 /* Print the operand as directed by the attributes. */
82 if (CGEN_BOOL_ATTR (attrs, CGEN_OPERAND_SEM_ONLY))
83 ; /* nothing to do */
84 else if (CGEN_BOOL_ATTR (attrs, CGEN_OPERAND_SIGNED))
85 (*info->fprintf_func) (info->stream, "%ld", value);
86 else
87 (*info->fprintf_func) (info->stream, "0x%lx", value);
88}
89
90/* Default address handler. */
91
92static void
93print_address (cd, dis_info, value, attrs, pc, length)
94 CGEN_CPU_DESC cd ATTRIBUTE_UNUSED;
95 PTR dis_info;
96 bfd_vma value;
97 unsigned int attrs;
98 bfd_vma pc ATTRIBUTE_UNUSED;
99 int length ATTRIBUTE_UNUSED;
100{
101 disassemble_info *info = (disassemble_info *) dis_info;
102
103#ifdef CGEN_PRINT_ADDRESS
104 CGEN_PRINT_ADDRESS (cd, info, value, attrs, pc, length);
105#endif
106
107 /* Print the operand as directed by the attributes. */
108 if (CGEN_BOOL_ATTR (attrs, CGEN_OPERAND_SEM_ONLY))
109 ; /* nothing to do */
110 else if (CGEN_BOOL_ATTR (attrs, CGEN_OPERAND_PCREL_ADDR))
111 (*info->print_address_func) (value, info);
112 else if (CGEN_BOOL_ATTR (attrs, CGEN_OPERAND_ABS_ADDR))
113 (*info->print_address_func) (value, info);
114 else if (CGEN_BOOL_ATTR (attrs, CGEN_OPERAND_SIGNED))
115 (*info->fprintf_func) (info->stream, "%ld", (long) value);
116 else
117 (*info->fprintf_func) (info->stream, "0x%lx", (long) value);
118}
119
120/* Keyword print handler. */
121
122static void
123print_keyword (cd, dis_info, keyword_table, value, attrs)
124 CGEN_CPU_DESC cd ATTRIBUTE_UNUSED;
125 PTR dis_info;
126 CGEN_KEYWORD *keyword_table;
127 long value;
128 unsigned int attrs ATTRIBUTE_UNUSED;
129{
130 disassemble_info *info = (disassemble_info *) dis_info;
131 const CGEN_KEYWORD_ENTRY *ke;
132
133 ke = cgen_keyword_lookup_value (keyword_table, value);
134 if (ke != NULL)
135 (*info->fprintf_func) (info->stream, "%s", ke->name);
136 else
137 (*info->fprintf_func) (info->stream, "???");
138}
139
140
141/* Default insn printer.
142
143 DIS_INFO is defined as `PTR' so the disassembler needn't know anything
144 about disassemble_info. */
145
146static void
147print_insn_normal (cd, dis_info, insn, fields, pc, length)
148 CGEN_CPU_DESC cd;
149 PTR dis_info;
150 const CGEN_INSN *insn;
151 CGEN_FIELDS *fields;
152 bfd_vma pc;
153 int length;
154{
155 const CGEN_SYNTAX *syntax = CGEN_INSN_SYNTAX (insn);
156 disassemble_info *info = (disassemble_info *) dis_info;
157 const CGEN_SYNTAX_CHAR_TYPE *syn;
158
159 CGEN_INIT_PRINT (cd);
160
161 for (syn = CGEN_SYNTAX_STRING (syntax); *syn; ++syn)
162 {
163 if (CGEN_SYNTAX_MNEMONIC_P (*syn))
164 {
165 (*info->fprintf_func) (info->stream, "%s", CGEN_INSN_MNEMONIC (insn));
166 continue;
167 }
168 if (CGEN_SYNTAX_CHAR_P (*syn))
169 {
170 (*info->fprintf_func) (info->stream, "%c", CGEN_SYNTAX_CHAR (*syn));
171 continue;
172 }
173
174 /* We have an operand. */
175 @arch@_cgen_print_operand (cd, CGEN_SYNTAX_FIELD (*syn), info,
176 fields, CGEN_INSN_ATTRS (insn), pc, length);
177 }
178}
179
180
181/* Subroutine of print_insn. Reads an insn into the given buffers and updates
182 the extract info.
183 Returns 0 if all is well, non-zero otherwise. */
184
185static int
186read_insn (cd, pc, info, buf, buflen, ex_info, insn_value)
187 CGEN_CPU_DESC cd ATTRIBUTE_UNUSED;
188 bfd_vma pc;
189 disassemble_info *info;
190 char *buf;
191 int buflen;
192 CGEN_EXTRACT_INFO *ex_info;
193 unsigned long *insn_value;
194{
195 int status = (*info->read_memory_func) (pc, buf, buflen, info);
196 if (status != 0)
197 {
198 (*info->memory_error_func) (status, pc, info);
199 return -1;
200 }
201
202 ex_info->dis_info = info;
203 ex_info->valid = (1 << buflen) - 1;
204 ex_info->insn_bytes = buf;
205
206 *insn_value = bfd_get_bits (buf, buflen * 8, info->endian == BFD_ENDIAN_BIG);
207 return 0;
208}
209
210/* Utility to print an insn.
211 BUF is the base part of the insn, target byte order, BUFLEN bytes long.
212 The result is the size of the insn in bytes or zero for an unknown insn
213 or -1 if an error occurs fetching data (memory_error_func will have
214 been called). */
215
216static int
217print_insn (cd, pc, info, buf, buflen)
218 CGEN_CPU_DESC cd;
219 bfd_vma pc;
220 disassemble_info *info;
221 char *buf;
222 unsigned int buflen;
223{
224 CGEN_INSN_INT insn_value;
225 const CGEN_INSN_LIST *insn_list;
226 CGEN_EXTRACT_INFO ex_info;
227 int basesize;
228
229 /* Extract base part of instruction, just in case CGEN_DIS_* uses it. */
230 basesize = cd->base_insn_bitsize < buflen * 8 ?
231 cd->base_insn_bitsize : buflen * 8;
232 insn_value = cgen_get_insn_value (cd, buf, basesize);
233
234
235 /* Fill in ex_info fields like read_insn would. Don't actually call
236 read_insn, since the incoming buffer is already read (and possibly
237 modified a la m32r). */
238 ex_info.valid = (1 << buflen) - 1;
239 ex_info.dis_info = info;
240 ex_info.insn_bytes = buf;
241
242 /* The instructions are stored in hash lists.
243 Pick the first one and keep trying until we find the right one. */
244
245 insn_list = CGEN_DIS_LOOKUP_INSN (cd, buf, insn_value);
246 while (insn_list != NULL)
247 {
248 const CGEN_INSN *insn = insn_list->insn;
249 CGEN_FIELDS fields;
250 int length;
251 unsigned long insn_value_cropped;
252
253#ifdef CGEN_VALIDATE_INSN_SUPPORTED
254 /* Not needed as insn shouldn't be in hash lists if not supported. */
255 /* Supported by this cpu? */
256 if (! @arch@_cgen_insn_supported (cd, insn))
257 {
258 insn_list = CGEN_DIS_NEXT_INSN (insn_list);
259 continue;
260 }
261#endif
262
263 /* Basic bit mask must be correct. */
264 /* ??? May wish to allow target to defer this check until the extract
265 handler. */
266
267 /* Base size may exceed this instruction's size. Extract the
268 relevant part from the buffer. */
269 if ((unsigned) (CGEN_INSN_BITSIZE (insn) / 8) < buflen &&
270 (unsigned) (CGEN_INSN_BITSIZE (insn) / 8) <= sizeof (unsigned long))
271 insn_value_cropped = bfd_get_bits (buf, CGEN_INSN_BITSIZE (insn),
272 info->endian == BFD_ENDIAN_BIG);
273 else
274 insn_value_cropped = insn_value;
275
276 if ((insn_value_cropped & CGEN_INSN_BASE_MASK (insn))
277 == CGEN_INSN_BASE_VALUE (insn))
278 {
279 /* Printing is handled in two passes. The first pass parses the
280 machine insn and extracts the fields. The second pass prints
281 them. */
282
283 /* Make sure the entire insn is loaded into insn_value, if it
284 can fit. */
285 if (((unsigned) CGEN_INSN_BITSIZE (insn) > cd->base_insn_bitsize) &&
286 (unsigned) (CGEN_INSN_BITSIZE (insn) / 8) <= sizeof (unsigned long))
287 {
288 unsigned long full_insn_value;
289 int rc = read_insn (cd, pc, info, buf,
290 CGEN_INSN_BITSIZE (insn) / 8,
291 & ex_info, & full_insn_value);
292 if (rc != 0)
293 return rc;
294 length = CGEN_EXTRACT_FN (cd, insn)
295 (cd, insn, &ex_info, full_insn_value, &fields, pc);
296 }
297 else
298 length = CGEN_EXTRACT_FN (cd, insn)
299 (cd, insn, &ex_info, insn_value_cropped, &fields, pc);
300
301 /* length < 0 -> error */
302 if (length < 0)
303 return length;
304 if (length > 0)
305 {
306 CGEN_PRINT_FN (cd, insn) (cd, info, insn, &fields, pc, length);
307 /* length is in bits, result is in bytes */
308 return length / 8;
309 }
310 }
311
312 insn_list = CGEN_DIS_NEXT_INSN (insn_list);
313 }
314
315 return 0;
316}
317
318/* Default value for CGEN_PRINT_INSN.
319 The result is the size of the insn in bytes or zero for an unknown insn
320 or -1 if an error occured fetching bytes. */
321
322#ifndef CGEN_PRINT_INSN
323#define CGEN_PRINT_INSN default_print_insn
324#endif
325
326static int
327default_print_insn (cd, pc, info)
328 CGEN_CPU_DESC cd;
329 bfd_vma pc;
330 disassemble_info *info;
331{
332 char buf[CGEN_MAX_INSN_SIZE];
333 int buflen;
334 int status;
335
336 /* Attempt to read the base part of the insn. */
337 buflen = cd->base_insn_bitsize / 8;
338 status = (*info->read_memory_func) (pc, buf, buflen, info);
339
340 /* Try again with the minimum part, if min < base. */
341 if (status != 0 && (cd->min_insn_bitsize < cd->base_insn_bitsize))
342 {
343 buflen = cd->min_insn_bitsize / 8;
344 status = (*info->read_memory_func) (pc, buf, buflen, info);
345 }
346
347 if (status != 0)
348 {
349 (*info->memory_error_func) (status, pc, info);
350 return -1;
351 }
352
353 return print_insn (cd, pc, info, buf, buflen);
354}
355
356/* Main entry point.
357 Print one instruction from PC on INFO->STREAM.
358 Return the size of the instruction (in bytes). */
359
360typedef struct cpu_desc_list {
361 struct cpu_desc_list *next;
362 int isa;
363 int mach;
364 int endian;
365 CGEN_CPU_DESC cd;
366} cpu_desc_list;
367
368int
369print_insn_@arch@ (pc, info)
370 bfd_vma pc;
371 disassemble_info *info;
372{
373 static cpu_desc_list *cd_list = 0;
374 cpu_desc_list *cl = 0;
375 static CGEN_CPU_DESC cd = 0;
376 static int prev_isa;
377 static int prev_mach;
378 static int prev_endian;
379 int length;
380 int isa,mach;
381 int endian = (info->endian == BFD_ENDIAN_BIG
382 ? CGEN_ENDIAN_BIG
383 : CGEN_ENDIAN_LITTLE);
384 enum bfd_architecture arch;
385
386 /* ??? gdb will set mach but leave the architecture as "unknown" */
387#ifndef CGEN_BFD_ARCH
388#define CGEN_BFD_ARCH bfd_arch_@arch@
389#endif
390 arch = info->arch;
391 if (arch == bfd_arch_unknown)
392 arch = CGEN_BFD_ARCH;
393
394 /* There's no standard way to compute the machine or isa number
395 so we leave it to the target. */
396#ifdef CGEN_COMPUTE_MACH
397 mach = CGEN_COMPUTE_MACH (info);
398#else
399 mach = info->mach;
400#endif
401
402#ifdef CGEN_COMPUTE_ISA
403 isa = CGEN_COMPUTE_ISA (info);
404#else
405 isa = info->insn_sets;
406#endif
407
408 /* If we've switched cpu's, try to find a handle we've used before */
409 if (cd
410 && (isa != prev_isa
411 || mach != prev_mach
412 || endian != prev_endian))
413 {
414 cd = 0;
415 for (cl = cd_list; cl; cl = cl->next)
416 {
417 if (cl->isa == isa &&
418 cl->mach == mach &&
419 cl->endian == endian)
420 {
421 cd = cl->cd;
422 break;
423 }
424 }
425 }
426
427 /* If we haven't initialized yet, initialize the opcode table. */
428 if (! cd)
429 {
430 const bfd_arch_info_type *arch_type = bfd_lookup_arch (arch, mach);
431 const char *mach_name;
432
433 if (!arch_type)
434 abort ();
435 mach_name = arch_type->printable_name;
436
437 prev_isa = isa;
438 prev_mach = mach;
439 prev_endian = endian;
440 cd = @arch@_cgen_cpu_open (CGEN_CPU_OPEN_ISAS, prev_isa,
441 CGEN_CPU_OPEN_BFDMACH, mach_name,
442 CGEN_CPU_OPEN_ENDIAN, prev_endian,
443 CGEN_CPU_OPEN_END);
444 if (!cd)
445 abort ();
446
447 /* save this away for future reference */
448 cl = xmalloc (sizeof (struct cpu_desc_list));
449 cl->cd = cd;
450 cl->isa = isa;
451 cl->mach = mach;
452 cl->endian = endian;
453 cl->next = cd_list;
454 cd_list = cl;
455
456 @arch@_cgen_init_dis (cd);
457 }
458
459 /* We try to have as much common code as possible.
460 But at this point some targets need to take over. */
461 /* ??? Some targets may need a hook elsewhere. Try to avoid this,
462 but if not possible try to move this hook elsewhere rather than
463 have two hooks. */
464 length = CGEN_PRINT_INSN (cd, pc, info);
465 if (length > 0)
466 return length;
467 if (length < 0)
468 return -1;
469
470 (*info->fprintf_func) (info->stream, UNKNOWN_INSN_MSG);
471 return cd->default_insn_bitsize / 8;
472}
Note: See TracBrowser for help on using the repository browser.