1 |
6 |
jlechner |
@c Copyright (C) 2000, 2009 Red Hat, Inc.
|
2 |
|
|
@c This file is part of the CGEN manual.
|
3 |
|
|
@c For copying conditions, see the file cgen.texi.
|
4 |
|
|
|
5 |
|
|
@node Opcodes
|
6 |
|
|
@chapter Opcodes support
|
7 |
|
|
@cindex Opcodes support
|
8 |
|
|
|
9 |
|
|
Opcodes support comes in the form of machine generated opcode tables as
|
10 |
|
|
well as supporting routines.
|
11 |
|
|
|
12 |
|
|
@menu
|
13 |
|
|
* Generated files:: List of generated files
|
14 |
|
|
* The .opc file:: Target specific C code
|
15 |
|
|
* Special assembler parsing needs:: Support for unusual syntax
|
16 |
|
|
@end menu
|
17 |
|
|
|
18 |
|
|
@node Generated files
|
19 |
|
|
@section Generated files
|
20 |
|
|
|
21 |
|
|
The basic interface is defined by
|
22 |
|
|
@file{include/opcode/cgen.h} which is included by the machine generated
|
23 |
|
|
@file{<arch>-desc.h}. @file{opcode/cgen.h} can stand on its own for the
|
24 |
|
|
target independent stuff, but to get target specific parts of the
|
25 |
|
|
interface use @file{<arch>-desc.h}.
|
26 |
|
|
|
27 |
|
|
The generated files are:
|
28 |
|
|
|
29 |
|
|
@table @file
|
30 |
|
|
@item <arch>-desc.h
|
31 |
|
|
Defines macros, enums, and types used to describe the chip.
|
32 |
|
|
@item <arch>-desc.c
|
33 |
|
|
Tables of various things describing the chip.
|
34 |
|
|
This does not include assembler syntax nor semantic information.
|
35 |
|
|
@item <arch>-ibld.c
|
36 |
|
|
Routines for constructing and deconstructing instructions.
|
37 |
|
|
@item <arch>-opc.h
|
38 |
|
|
Declarations necessary for assembly/disassembly that aren't used
|
39 |
|
|
elsewhere and thus left out of @file{<arch>-desc.h}.
|
40 |
|
|
@item <arch>-opc.c
|
41 |
|
|
Assembler syntax tables.
|
42 |
|
|
@item <arch>-asm.c
|
43 |
|
|
Assembler support routines.
|
44 |
|
|
@item <arch>-dis.c
|
45 |
|
|
Disassembler support routines.
|
46 |
|
|
@item <arch>-opinst.c
|
47 |
|
|
Operand instance tables.
|
48 |
|
|
These describe which hardware elements are read and which are written
|
49 |
|
|
for each instruction. This file isn't generated for all architectures,
|
50 |
|
|
only ones that can make use of the data. For example the M32R uses them
|
51 |
|
|
to emit warnings if the output of one parallel instruction is the input
|
52 |
|
|
of another, and to control creating parallel instructions during optimizing
|
53 |
|
|
assembly.
|
54 |
|
|
@end table
|
55 |
|
|
|
56 |
|
|
@node The .opc file
|
57 |
|
|
@section The .opc file
|
58 |
|
|
|
59 |
|
|
Files with suffix @file{.opc} (e.g. @file{m32r.opc}) contain target
|
60 |
|
|
specific C code that accompanies the cpu description file.
|
61 |
|
|
The @file{.opc} file is split into 4 sections:
|
62 |
|
|
|
63 |
|
|
@itemize @minus
|
64 |
|
|
@item opc.h
|
65 |
|
|
|
66 |
|
|
This section contains additions to the generated @file{$target-opc.h} file.
|
67 |
|
|
|
68 |
|
|
Typically defined here are these macros:
|
69 |
|
|
|
70 |
|
|
@itemize @bullet
|
71 |
|
|
@item #define CGEN_DIS_HASH_SIZE N
|
72 |
|
|
|
73 |
|
|
Specifies the size of the hash table to use during disassembly.
|
74 |
|
|
A hash table is built of the selected mach's instructions in order to
|
75 |
|
|
speed up disassembly.
|
76 |
|
|
@item #define CGEN_DIS_HASH(buffer, value)
|
77 |
|
|
|
78 |
|
|
Given BUFFER, a pointer to the instruction being disassembled and
|
79 |
|
|
VALUE, the value of the instruction as a host integer, return an
|
80 |
|
|
index into the hash chain for the instruction. The result must be
|
81 |
|
|
in the range 0 to CGEN_DIS_HASH_SIZE-1.
|
82 |
|
|
|
83 |
|
|
VALUE is only usable if all instructions fit in a portable integer (32 bits).
|
84 |
|
|
|
85 |
|
|
N.B. The result must depend on opcode portions of the instruction only.
|
86 |
|
|
Normally one wants to use between 6 and 8 bits of opcode info for the hash
|
87 |
|
|
table. However, some instruction sets don't use the same set of bits
|
88 |
|
|
for all insns. Certainly they'll have at least one opcode bit in common
|
89 |
|
|
with all insns, but beyond that it can vary. Here's a possible definition
|
90 |
|
|
for sparc.
|
91 |
|
|
|
92 |
|
|
@example
|
93 |
|
|
#undef CGEN_DIS_HASH_SIZE
|
94 |
|
|
#define CGEN_DIS_HASH_SIZE 256
|
95 |
|
|
#undef CGEN_DIS_HASH
|
96 |
|
|
extern const unsigned int sparc_cgen_opcode_bits[];
|
97 |
|
|
#define CGEN_DIS_HASH(buffer, insn) \
|
98 |
|
|
((((insn) >> 24) & 0xc0) \
|
99 |
|
|
| (((insn) & sparc_cgen_opcode_bits[((insn) >> 30) & 3]) >> 19))
|
100 |
|
|
@end example
|
101 |
|
|
|
102 |
|
|
@code{sparc_cgen_opcode_bits} would be defined in the @samp{asm.c} section as
|
103 |
|
|
|
104 |
|
|
@example
|
105 |
|
|
/* It is important that we only look at insn code bits
|
106 |
|
|
as that is how the opcode table is hashed.
|
107 |
|
|
OPCODE_BITS is a table of valid bits for each of the
|
108 |
|
|
main types (0,1,2,3). */
|
109 |
|
|
const unsigned int sparc_cgen_opcode_bits[4] = @{
|
110 |
|
|
0x01c00000, 0x0, 0x01f80000, 0x01f80000
|
111 |
|
|
@};
|
112 |
|
|
@end example
|
113 |
|
|
@end itemize
|
114 |
|
|
|
115 |
|
|
@item opc.c
|
116 |
|
|
|
117 |
|
|
@item asm.c
|
118 |
|
|
|
119 |
|
|
This section contains additions to the generated @file{$target-asm.c} file.
|
120 |
|
|
Typically defined here are functions used by operands with a @code{parse}
|
121 |
|
|
define-operand handler spec.
|
122 |
|
|
|
123 |
|
|
@item dis.c
|
124 |
|
|
|
125 |
|
|
This section contains additions to the generated @file{$target-dis.c} file.
|
126 |
|
|
|
127 |
|
|
Typically defined here these macros:
|
128 |
|
|
|
129 |
|
|
@itemize @bullet
|
130 |
|
|
@item #define CGEN_PRINT_NORMAL(cd, info, value, attrs, pc, length)
|
131 |
|
|
@item #define CGEN_PRINT_ADDRESS(cd, info, value, attrs, pc, length)
|
132 |
|
|
@item #define CGEN_PRINT_INSN function_name
|
133 |
|
|
@c FIXME: should be CGEN_PRINT_INSN(cd, pc, info)
|
134 |
|
|
@item #define CGEN_BFD_ARCH bfd_arch_<name>
|
135 |
|
|
@item #define CGEN_COMPUTE_ISA(info)
|
136 |
|
|
@end itemize
|
137 |
|
|
|
138 |
|
|
@end itemize
|
139 |
|
|
|
140 |
|
|
@node Special assembler parsing needs
|
141 |
|
|
@section Special assembler parsing needs
|
142 |
|
|
|
143 |
|
|
Often parsing of assembly instructions requires more than what
|
144 |
|
|
a program-generated assembler can handle. For example one version
|
145 |
|
|
of an instruction may only accept certain registers, rather than
|
146 |
|
|
the entire set.
|
147 |
|
|
|
148 |
|
|
Here's an example taken from the @samp{m32r} architecture.
|
149 |
|
|
|
150 |
|
|
32 bit addresses are built up with a two instruction sequence: one to
|
151 |
|
|
load the high 16 bits of a register, and another to @code{or}-in the
|
152 |
|
|
lower 16 bits.
|
153 |
|
|
|
154 |
|
|
@example
|
155 |
|
|
seth r0,high(some_symbol)
|
156 |
|
|
or3 r0,r0,low(some_symbol)
|
157 |
|
|
@end example
|
158 |
|
|
|
159 |
|
|
When assembling, special code must be called to recognize the
|
160 |
|
|
@code{high} and @code{low} pseudo-ops and generate the appropriate
|
161 |
|
|
relocations. This is indicated by specifying a "parse handler" for
|
162 |
|
|
the operand in question. Here is the @code{define-operand}
|
163 |
|
|
for the lower 16 bit operand.
|
164 |
|
|
|
165 |
|
|
@example
|
166 |
|
|
(define-operand
|
167 |
|
|
(name ulo16)
|
168 |
|
|
(comment "16 bit unsigned immediate, for low()")
|
169 |
|
|
(attrs)
|
170 |
|
|
(type h-ulo16)
|
171 |
|
|
(index f-uimm16)
|
172 |
|
|
(handlers (parse "ulo16"))
|
173 |
|
|
)
|
174 |
|
|
@end example
|
175 |
|
|
|
176 |
|
|
The generated parser will call a function named @code{parse_ulo16}
|
177 |
|
|
for the immediate operand of the @code{or3} instruction.
|
178 |
|
|
The name of the function is constructed by prepended "parse_" to the
|
179 |
|
|
argument of the @code{parse} spec.
|
180 |
|
|
|
181 |
|
|
@example
|
182 |
|
|
errmsg = parse_ulo16 (cd, strp, M32R_OPERAND_ULO16, &fields->f_uimm16);
|
183 |
|
|
@end example
|
184 |
|
|
|
185 |
|
|
But where does one put the @code{parse_ulo16} function?
|
186 |
|
|
Answer: in the @samp{asm.c} section of @file{m32r.opc}.
|