1 ; Instruction builder support.
2 ; Copyright (C) 2000, 2001, 2005, 2009 Red Hat, Inc.
3 ; This file is part of CGEN.
5 ; Instruction field support.
7 (define (/gen-fget-switch)
8 (logit 2 "Generating field get switch ...\n")
11 int @arch@_cgen_get_int_operand (CGEN_CPU_DESC, int, const CGEN_FIELDS *);
12 bfd_vma @arch@_cgen_get_vma_operand (CGEN_CPU_DESC, int, const CGEN_FIELDS *);
14 /* Getting values from cgen_fields is handled by a collection of functions.
15 They are distinguished by the type of the VALUE argument they return.
16 TODO: floating point, inlining support, remove cases where result type
20 @arch@_cgen_get_int_operand (CGEN_CPU_DESC cd ATTRIBUTE_UNUSED,
22 const CGEN_FIELDS * fields)
32 /* xgettext:c-format */
33 fprintf (stderr, _(\"Unrecognized field %d while getting int operand.\\n\"),
42 @arch@_cgen_get_vma_operand (CGEN_CPU_DESC cd ATTRIBUTE_UNUSED,
44 const CGEN_FIELDS * fields)
54 /* xgettext:c-format */
55 fprintf (stderr, _(\"Unrecognized field %d while getting vma operand.\\n\"),
65 (define (/gen-fset-switch)
66 (logit 2 "Generating field set switch ...\n")
69 void @arch@_cgen_set_int_operand (CGEN_CPU_DESC, int, CGEN_FIELDS *, int);
70 void @arch@_cgen_set_vma_operand (CGEN_CPU_DESC, int, CGEN_FIELDS *, bfd_vma);
72 /* Stuffing values in cgen_fields is handled by a collection of functions.
73 They are distinguished by the type of the VALUE argument they accept.
74 TODO: floating point, inlining support, remove cases where argument type
78 @arch@_cgen_set_int_operand (CGEN_CPU_DESC cd ATTRIBUTE_UNUSED,
89 /* xgettext:c-format */
90 fprintf (stderr, _(\"Unrecognized field %d while setting int operand.\\n\"),
97 @arch@_cgen_set_vma_operand (CGEN_CPU_DESC cd ATTRIBUTE_UNUSED,
108 /* xgettext:c-format */
109 fprintf (stderr, _(\"Unrecognized field %d while setting vma operand.\\n\"),
117 ; Utilities of cgen-ibld.h.
119 ; Return a list of operands the assembler syntax uses.
120 ; This is a subset of the fields of the insn.
122 (define (ifmt-opcode-operands ifmt)
124 (find (lambda (elm) (not (number? (ifld-get-value elm))))
125 (ifmt-ifields ifmt)))
128 ; Subroutine of gen-insn-builders to generate the builder for one insn.
131 (define (gen-insn-builder insn)
132 (let* ((ifmt (insn-ifmt insn))
133 (operands (ifmt-opcode-operands ifmt))
134 (length (ifmt-length ifmt)))
138 "#define @ARCH@_IBLD_"
139 (string-upcase (gen-sym insn))
141 (gen-c-args (map gen-sym operands))
146 (define (gen-insn-builders)
149 /* For each insn there is an @ARCH@_IBLD_<NAME> macro that builds the
150 instruction in the supplied buffer. For architectures where it's
151 possible to represent all machine codes as host integer values it
152 would be nicer to have these return the instruction rather than store
153 it in BUF. For consistency with variable length ISA's this does not. */
156 (lambda () (string-write-map gen-insn-builder (current-insn-list)))
160 ; Generate the C code for dealing with operands.
162 (define (/gen-insert-switch)
163 (logit 2 "Generating insert switch ...\n")
166 const char * @arch@_cgen_insert_operand
167 (CGEN_CPU_DESC, int, CGEN_FIELDS *, CGEN_INSN_BYTES_PTR, bfd_vma);
169 /* Main entry point for operand insertion.
171 This function is basically just a big switch statement. Earlier versions
172 used tables to look up the function to use, but
173 - if the table contains both assembler and disassembler functions then
174 the disassembler contains much of the assembler and vice-versa,
175 - there's a lot of inlining possibilities as things grow,
176 - using a switch statement avoids the function call overhead.
178 This function could be moved into `parse_insn_normal', but keeping it
179 separate makes clear the interface between `parse_insn_normal' and each of
180 the handlers. It's also needed by GAS to insert operands that couldn't be
181 resolved during parsing. */
184 @arch@_cgen_insert_operand (CGEN_CPU_DESC cd,
186 CGEN_FIELDS * fields,
187 CGEN_INSN_BYTES_PTR buffer,
188 bfd_vma pc ATTRIBUTE_UNUSED)
190 const char * errmsg = NULL;
191 unsigned int total_length = CGEN_FIELDS_BITSIZE (fields);
199 /* xgettext:c-format */
200 fprintf (stderr, _(\"Unrecognized field %d while building insn.\\n\"),
209 (define (/gen-extract-switch)
210 (logit 2 "Generating extract switch ...\n")
213 int @arch@_cgen_extract_operand
214 (CGEN_CPU_DESC, int, CGEN_EXTRACT_INFO *, CGEN_INSN_INT, CGEN_FIELDS *, bfd_vma);
216 /* Main entry point for operand extraction.
217 The result is <= 0 for error, >0 for success.
218 ??? Actual values aren't well defined right now.
220 This function is basically just a big switch statement. Earlier versions
221 used tables to look up the function to use, but
222 - if the table contains both assembler and disassembler functions then
223 the disassembler contains much of the assembler and vice-versa,
224 - there's a lot of inlining possibilities as things grow,
225 - using a switch statement avoids the function call overhead.
227 This function could be moved into `print_insn_normal', but keeping it
228 separate makes clear the interface between `print_insn_normal' and each of
232 @arch@_cgen_extract_operand (CGEN_CPU_DESC cd,
234 CGEN_EXTRACT_INFO *ex_info,
235 CGEN_INSN_INT insn_value,
236 CGEN_FIELDS * fields,
239 /* Assume success (for those operands that are nops). */
241 unsigned int total_length = CGEN_FIELDS_BITSIZE (fields);
246 (gen-switch 'extract)
249 /* xgettext:c-format */
250 fprintf (stderr, _(\"Unrecognized field %d while decoding insn.\\n\"),
259 ; Utilities of cgen-ibld.in.
261 ; Emit a function to call to initialize the ibld tables.
263 (define (/gen-ibld-init-fn)
266 /* Function to call before using the instruction builder tables. */
269 @arch@_cgen_init_ibld_table (CGEN_CPU_DESC cd)
271 cd->insert_handlers = & @arch@_cgen_insert_handlers[0];
272 cd->extract_handlers = & @arch@_cgen_extract_handlers[0];
274 cd->insert_operand = @arch@_cgen_insert_operand;
275 cd->extract_operand = @arch@_cgen_extract_operand;
277 cd->get_int_operand = @arch@_cgen_get_int_operand;
278 cd->set_int_operand = @arch@_cgen_set_int_operand;
279 cd->get_vma_operand = @arch@_cgen_get_vma_operand;
280 cd->set_vma_operand = @arch@_cgen_set_vma_operand;
286 ; Generate the C header for building instructions.
288 (define (cgen-ibld.h)
289 (logit 1 "Generating " (current-arch-name) "-ibld.h ...\n")
291 (gen-c-copyright "Instruction builder for @arch@."
292 CURRENT-COPYRIGHT CURRENT-PACKAGE)
294 #ifndef @ARCH@_IBLD_H
295 #define @ARCH@_IBLD_H
298 (lambda () (gen-extra-ibld.h (opc-file-path) (current-arch-name)))
302 #endif /* @ARCH@_IBLD_H */
307 ; Generate the C support for building instructions.
309 (define (cgen-ibld.in)
310 (logit 1 "Generating " (current-arch-name) "-ibld.in ...\n")
312 ; No need for copyright, appended to file with one.
316 (lambda () (gen-handler-table "insert" opc-insert-handlers))
317 (lambda () (gen-handler-table "extract" opc-extract-handlers))