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

Last change on this file since 201 was 10, checked in by bird, 22 years ago

Initial revision

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