1*3d8817e4Smiod /* tc-crx.c -- Assembler code for the CRX CPU core.
2*3d8817e4Smiod Copyright 2004 Free Software Foundation, Inc.
3*3d8817e4Smiod
4*3d8817e4Smiod Contributed by Tomer Levi, NSC, Israel.
5*3d8817e4Smiod Originally written for GAS 2.12 by Tomer Levi, NSC, Israel.
6*3d8817e4Smiod Updates, BFDizing, GNUifying and ELF support by Tomer Levi.
7*3d8817e4Smiod
8*3d8817e4Smiod This file is part of GAS, the GNU Assembler.
9*3d8817e4Smiod
10*3d8817e4Smiod GAS is free software; you can redistribute it and/or modify
11*3d8817e4Smiod it under the terms of the GNU General Public License as published by
12*3d8817e4Smiod the Free Software Foundation; either version 2, or (at your option)
13*3d8817e4Smiod any later version.
14*3d8817e4Smiod
15*3d8817e4Smiod GAS is distributed in the hope that it will be useful,
16*3d8817e4Smiod but WITHOUT ANY WARRANTY; without even the implied warranty of
17*3d8817e4Smiod MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18*3d8817e4Smiod GNU General Public License for more details.
19*3d8817e4Smiod
20*3d8817e4Smiod You should have received a copy of the GNU General Public License
21*3d8817e4Smiod along with GAS; see the file COPYING. If not, write to the
22*3d8817e4Smiod Free Software Foundation, 51 Franklin Street - Fifth Floor, Boston,
23*3d8817e4Smiod MA 02110-1301, USA. */
24*3d8817e4Smiod
25*3d8817e4Smiod #include "as.h"
26*3d8817e4Smiod #include "safe-ctype.h"
27*3d8817e4Smiod #include "dwarf2dbg.h"
28*3d8817e4Smiod #include "opcode/crx.h"
29*3d8817e4Smiod #include "elf/crx.h"
30*3d8817e4Smiod
31*3d8817e4Smiod /* Word is considered here as a 16-bit unsigned short int. */
32*3d8817e4Smiod #define WORD_SHIFT 16
33*3d8817e4Smiod
34*3d8817e4Smiod /* Register is 4-bit size. */
35*3d8817e4Smiod #define REG_SIZE 4
36*3d8817e4Smiod
37*3d8817e4Smiod /* Maximum size of a single instruction (in words). */
38*3d8817e4Smiod #define INSN_MAX_SIZE 3
39*3d8817e4Smiod
40*3d8817e4Smiod /* Maximum bits which may be set in a `mask16' operand. */
41*3d8817e4Smiod #define MAX_REGS_IN_MASK16 8
42*3d8817e4Smiod
43*3d8817e4Smiod /* Utility macros for string comparison. */
44*3d8817e4Smiod #define streq(a, b) (strcmp (a, b) == 0)
45*3d8817e4Smiod #define strneq(a, b, c) (strncmp (a, b, c) == 0)
46*3d8817e4Smiod
47*3d8817e4Smiod /* Assign a number NUM, shifted by SHIFT bytes, into a location
48*3d8817e4Smiod pointed by index BYTE of array 'output_opcode'. */
49*3d8817e4Smiod #define CRX_PRINT(BYTE, NUM, SHIFT) output_opcode[BYTE] |= (NUM << SHIFT)
50*3d8817e4Smiod
51*3d8817e4Smiod /* Operand errors. */
52*3d8817e4Smiod typedef enum
53*3d8817e4Smiod {
54*3d8817e4Smiod OP_LEGAL = 0, /* Legal operand. */
55*3d8817e4Smiod OP_OUT_OF_RANGE, /* Operand not within permitted range. */
56*3d8817e4Smiod OP_NOT_EVEN, /* Operand is Odd number, should be even. */
57*3d8817e4Smiod OP_ILLEGAL_DISPU4, /* Operand is not within DISPU4 range. */
58*3d8817e4Smiod OP_ILLEGAL_CST4, /* Operand is not within CST4 range. */
59*3d8817e4Smiod OP_NOT_UPPER_64KB /* Operand is not within the upper 64KB
60*3d8817e4Smiod (0xFFFF0000-0xFFFFFFFF). */
61*3d8817e4Smiod }
62*3d8817e4Smiod op_err;
63*3d8817e4Smiod
64*3d8817e4Smiod /* Opcode mnemonics hash table. */
65*3d8817e4Smiod static struct hash_control *crx_inst_hash;
66*3d8817e4Smiod /* CRX registers hash table. */
67*3d8817e4Smiod static struct hash_control *reg_hash;
68*3d8817e4Smiod /* CRX coprocessor registers hash table. */
69*3d8817e4Smiod static struct hash_control *copreg_hash;
70*3d8817e4Smiod /* Current instruction we're assembling. */
71*3d8817e4Smiod const inst *instruction;
72*3d8817e4Smiod
73*3d8817e4Smiod /* Global variables. */
74*3d8817e4Smiod
75*3d8817e4Smiod /* Array to hold an instruction encoding. */
76*3d8817e4Smiod long output_opcode[2];
77*3d8817e4Smiod
78*3d8817e4Smiod /* Nonzero means a relocatable symbol. */
79*3d8817e4Smiod int relocatable;
80*3d8817e4Smiod
81*3d8817e4Smiod /* A copy of the original instruction (used in error messages). */
82*3d8817e4Smiod char ins_parse[MAX_INST_LEN];
83*3d8817e4Smiod
84*3d8817e4Smiod /* The current processed argument number. */
85*3d8817e4Smiod int cur_arg_num;
86*3d8817e4Smiod
87*3d8817e4Smiod /* Generic assembler global variables which must be defined by all targets. */
88*3d8817e4Smiod
89*3d8817e4Smiod /* Characters which always start a comment. */
90*3d8817e4Smiod const char comment_chars[] = "#";
91*3d8817e4Smiod
92*3d8817e4Smiod /* Characters which start a comment at the beginning of a line. */
93*3d8817e4Smiod const char line_comment_chars[] = "#";
94*3d8817e4Smiod
95*3d8817e4Smiod /* This array holds machine specific line separator characters. */
96*3d8817e4Smiod const char line_separator_chars[] = ";";
97*3d8817e4Smiod
98*3d8817e4Smiod /* Chars that can be used to separate mant from exp in floating point nums. */
99*3d8817e4Smiod const char EXP_CHARS[] = "eE";
100*3d8817e4Smiod
101*3d8817e4Smiod /* Chars that mean this number is a floating point constant as in 0f12.456 */
102*3d8817e4Smiod const char FLT_CHARS[] = "f'";
103*3d8817e4Smiod
104*3d8817e4Smiod /* Target-specific multicharacter options, not const-declared at usage. */
105*3d8817e4Smiod const char *md_shortopts = "";
106*3d8817e4Smiod struct option md_longopts[] =
107*3d8817e4Smiod {
108*3d8817e4Smiod {NULL, no_argument, NULL, 0}
109*3d8817e4Smiod };
110*3d8817e4Smiod size_t md_longopts_size = sizeof (md_longopts);
111*3d8817e4Smiod
112*3d8817e4Smiod /* This table describes all the machine specific pseudo-ops
113*3d8817e4Smiod the assembler has to support. The fields are:
114*3d8817e4Smiod *** Pseudo-op name without dot.
115*3d8817e4Smiod *** Function to call to execute this pseudo-op.
116*3d8817e4Smiod *** Integer arg to pass to the function. */
117*3d8817e4Smiod
118*3d8817e4Smiod const pseudo_typeS md_pseudo_table[] =
119*3d8817e4Smiod {
120*3d8817e4Smiod /* In CRX machine, align is in bytes (not a ptwo boundary). */
121*3d8817e4Smiod {"align", s_align_bytes, 0},
122*3d8817e4Smiod {0, 0, 0}
123*3d8817e4Smiod };
124*3d8817e4Smiod
125*3d8817e4Smiod /* CRX relaxation table. */
126*3d8817e4Smiod const relax_typeS md_relax_table[] =
127*3d8817e4Smiod {
128*3d8817e4Smiod /* bCC */
129*3d8817e4Smiod {0xfa, -0x100, 2, 1}, /* 8 */
130*3d8817e4Smiod {0xfffe, -0x10000, 4, 2}, /* 16 */
131*3d8817e4Smiod {0xfffffffe, -0xfffffffe, 6, 0}, /* 32 */
132*3d8817e4Smiod
133*3d8817e4Smiod /* bal */
134*3d8817e4Smiod {0xfffe, -0x10000, 4, 4}, /* 16 */
135*3d8817e4Smiod {0xfffffffe, -0xfffffffe, 6, 0}, /* 32 */
136*3d8817e4Smiod
137*3d8817e4Smiod /* cmpbr/bcop */
138*3d8817e4Smiod {0xfe, -0x100, 4, 6}, /* 8 */
139*3d8817e4Smiod {0xfffffe, -0x1000000, 6, 0} /* 24 */
140*3d8817e4Smiod };
141*3d8817e4Smiod
142*3d8817e4Smiod static void reset_vars (char *);
143*3d8817e4Smiod static reg get_register (char *);
144*3d8817e4Smiod static copreg get_copregister (char *);
145*3d8817e4Smiod static argtype get_optype (operand_type);
146*3d8817e4Smiod static int get_opbits (operand_type);
147*3d8817e4Smiod static int get_opflags (operand_type);
148*3d8817e4Smiod static int get_number_of_operands (void);
149*3d8817e4Smiod static void parse_operand (char *, ins *);
150*3d8817e4Smiod static int gettrap (char *);
151*3d8817e4Smiod static void handle_LoadStor (char *);
152*3d8817e4Smiod static int get_cinv_parameters (char *);
153*3d8817e4Smiod static long getconstant (long, int);
154*3d8817e4Smiod static op_err check_range (long *, int, unsigned int, int);
155*3d8817e4Smiod static int getreg_image (reg);
156*3d8817e4Smiod static void parse_operands (ins *, char *);
157*3d8817e4Smiod static void parse_insn (ins *, char *);
158*3d8817e4Smiod static void print_operand (int, int, argument *);
159*3d8817e4Smiod static void print_constant (int, int, argument *);
160*3d8817e4Smiod static int exponent2scale (int);
161*3d8817e4Smiod static void mask_reg (int, unsigned short *);
162*3d8817e4Smiod static void process_label_constant (char *, ins *);
163*3d8817e4Smiod static void set_operand (char *, ins *);
164*3d8817e4Smiod static char * preprocess_reglist (char *, int *);
165*3d8817e4Smiod static int assemble_insn (char *, ins *);
166*3d8817e4Smiod static void print_insn (ins *);
167*3d8817e4Smiod static void warn_if_needed (ins *);
168*3d8817e4Smiod static int adjust_if_needed (ins *);
169*3d8817e4Smiod
170*3d8817e4Smiod /* Return the bit size for a given operand. */
171*3d8817e4Smiod
172*3d8817e4Smiod static int
get_opbits(operand_type op)173*3d8817e4Smiod get_opbits (operand_type op)
174*3d8817e4Smiod {
175*3d8817e4Smiod if (op < MAX_OPRD)
176*3d8817e4Smiod return crx_optab[op].bit_size;
177*3d8817e4Smiod else
178*3d8817e4Smiod return 0;
179*3d8817e4Smiod }
180*3d8817e4Smiod
181*3d8817e4Smiod /* Return the argument type of a given operand. */
182*3d8817e4Smiod
183*3d8817e4Smiod static argtype
get_optype(operand_type op)184*3d8817e4Smiod get_optype (operand_type op)
185*3d8817e4Smiod {
186*3d8817e4Smiod if (op < MAX_OPRD)
187*3d8817e4Smiod return crx_optab[op].arg_type;
188*3d8817e4Smiod else
189*3d8817e4Smiod return nullargs;
190*3d8817e4Smiod }
191*3d8817e4Smiod
192*3d8817e4Smiod /* Return the flags of a given operand. */
193*3d8817e4Smiod
194*3d8817e4Smiod static int
get_opflags(operand_type op)195*3d8817e4Smiod get_opflags (operand_type op)
196*3d8817e4Smiod {
197*3d8817e4Smiod if (op < MAX_OPRD)
198*3d8817e4Smiod return crx_optab[op].flags;
199*3d8817e4Smiod else
200*3d8817e4Smiod return 0;
201*3d8817e4Smiod }
202*3d8817e4Smiod
203*3d8817e4Smiod /* Get the core processor register 'reg_name'. */
204*3d8817e4Smiod
205*3d8817e4Smiod static reg
get_register(char * reg_name)206*3d8817e4Smiod get_register (char *reg_name)
207*3d8817e4Smiod {
208*3d8817e4Smiod const reg_entry *reg;
209*3d8817e4Smiod
210*3d8817e4Smiod reg = (const reg_entry *) hash_find (reg_hash, reg_name);
211*3d8817e4Smiod
212*3d8817e4Smiod if (reg != NULL)
213*3d8817e4Smiod return reg->value.reg_val;
214*3d8817e4Smiod else
215*3d8817e4Smiod return nullregister;
216*3d8817e4Smiod }
217*3d8817e4Smiod
218*3d8817e4Smiod /* Get the coprocessor register 'copreg_name'. */
219*3d8817e4Smiod
220*3d8817e4Smiod static copreg
get_copregister(char * copreg_name)221*3d8817e4Smiod get_copregister (char *copreg_name)
222*3d8817e4Smiod {
223*3d8817e4Smiod const reg_entry *copreg;
224*3d8817e4Smiod
225*3d8817e4Smiod copreg = (const reg_entry *) hash_find (copreg_hash, copreg_name);
226*3d8817e4Smiod
227*3d8817e4Smiod if (copreg != NULL)
228*3d8817e4Smiod return copreg->value.copreg_val;
229*3d8817e4Smiod else
230*3d8817e4Smiod return nullcopregister;
231*3d8817e4Smiod }
232*3d8817e4Smiod
233*3d8817e4Smiod /* Round up a section size to the appropriate boundary. */
234*3d8817e4Smiod
235*3d8817e4Smiod valueT
md_section_align(segT seg,valueT val)236*3d8817e4Smiod md_section_align (segT seg, valueT val)
237*3d8817e4Smiod {
238*3d8817e4Smiod /* Round .text section to a multiple of 2. */
239*3d8817e4Smiod if (seg == text_section)
240*3d8817e4Smiod return (val + 1) & ~1;
241*3d8817e4Smiod return val;
242*3d8817e4Smiod }
243*3d8817e4Smiod
244*3d8817e4Smiod /* Parse an operand that is machine-specific (remove '*'). */
245*3d8817e4Smiod
246*3d8817e4Smiod void
md_operand(expressionS * exp)247*3d8817e4Smiod md_operand (expressionS * exp)
248*3d8817e4Smiod {
249*3d8817e4Smiod char c = *input_line_pointer;
250*3d8817e4Smiod
251*3d8817e4Smiod switch (c)
252*3d8817e4Smiod {
253*3d8817e4Smiod case '*':
254*3d8817e4Smiod input_line_pointer++;
255*3d8817e4Smiod expression (exp);
256*3d8817e4Smiod break;
257*3d8817e4Smiod default:
258*3d8817e4Smiod break;
259*3d8817e4Smiod }
260*3d8817e4Smiod }
261*3d8817e4Smiod
262*3d8817e4Smiod /* Reset global variables before parsing a new instruction. */
263*3d8817e4Smiod
264*3d8817e4Smiod static void
reset_vars(char * op)265*3d8817e4Smiod reset_vars (char *op)
266*3d8817e4Smiod {
267*3d8817e4Smiod cur_arg_num = relocatable = 0;
268*3d8817e4Smiod memset (& output_opcode, '\0', sizeof (output_opcode));
269*3d8817e4Smiod
270*3d8817e4Smiod /* Save a copy of the original OP (used in error messages). */
271*3d8817e4Smiod strncpy (ins_parse, op, sizeof ins_parse - 1);
272*3d8817e4Smiod ins_parse [sizeof ins_parse - 1] = 0;
273*3d8817e4Smiod }
274*3d8817e4Smiod
275*3d8817e4Smiod /* This macro decides whether a particular reloc is an entry in a
276*3d8817e4Smiod switch table. It is used when relaxing, because the linker needs
277*3d8817e4Smiod to know about all such entries so that it can adjust them if
278*3d8817e4Smiod necessary. */
279*3d8817e4Smiod
280*3d8817e4Smiod #define SWITCH_TABLE(fix) \
281*3d8817e4Smiod ( (fix)->fx_addsy != NULL \
282*3d8817e4Smiod && (fix)->fx_subsy != NULL \
283*3d8817e4Smiod && S_GET_SEGMENT ((fix)->fx_addsy) == \
284*3d8817e4Smiod S_GET_SEGMENT ((fix)->fx_subsy) \
285*3d8817e4Smiod && S_GET_SEGMENT (fix->fx_addsy) != undefined_section \
286*3d8817e4Smiod && ( (fix)->fx_r_type == BFD_RELOC_CRX_NUM8 \
287*3d8817e4Smiod || (fix)->fx_r_type == BFD_RELOC_CRX_NUM16 \
288*3d8817e4Smiod || (fix)->fx_r_type == BFD_RELOC_CRX_NUM32))
289*3d8817e4Smiod
290*3d8817e4Smiod /* See whether we need to force a relocation into the output file.
291*3d8817e4Smiod This is used to force out switch and PC relative relocations when
292*3d8817e4Smiod relaxing. */
293*3d8817e4Smiod
294*3d8817e4Smiod int
crx_force_relocation(fixS * fix)295*3d8817e4Smiod crx_force_relocation (fixS *fix)
296*3d8817e4Smiod {
297*3d8817e4Smiod if (generic_force_reloc (fix) || SWITCH_TABLE (fix))
298*3d8817e4Smiod return 1;
299*3d8817e4Smiod
300*3d8817e4Smiod return 0;
301*3d8817e4Smiod }
302*3d8817e4Smiod
303*3d8817e4Smiod /* Generate a relocation entry for a fixup. */
304*3d8817e4Smiod
305*3d8817e4Smiod arelent *
tc_gen_reloc(asection * section ATTRIBUTE_UNUSED,fixS * fixP)306*3d8817e4Smiod tc_gen_reloc (asection *section ATTRIBUTE_UNUSED, fixS * fixP)
307*3d8817e4Smiod {
308*3d8817e4Smiod arelent * reloc;
309*3d8817e4Smiod
310*3d8817e4Smiod reloc = xmalloc (sizeof (arelent));
311*3d8817e4Smiod reloc->sym_ptr_ptr = xmalloc (sizeof (asymbol *));
312*3d8817e4Smiod *reloc->sym_ptr_ptr = symbol_get_bfdsym (fixP->fx_addsy);
313*3d8817e4Smiod reloc->address = fixP->fx_frag->fr_address + fixP->fx_where;
314*3d8817e4Smiod reloc->addend = fixP->fx_offset;
315*3d8817e4Smiod
316*3d8817e4Smiod if (fixP->fx_subsy != NULL)
317*3d8817e4Smiod {
318*3d8817e4Smiod if (SWITCH_TABLE (fixP))
319*3d8817e4Smiod {
320*3d8817e4Smiod /* Keep the current difference in the addend. */
321*3d8817e4Smiod reloc->addend = (S_GET_VALUE (fixP->fx_addsy)
322*3d8817e4Smiod - S_GET_VALUE (fixP->fx_subsy) + fixP->fx_offset);
323*3d8817e4Smiod
324*3d8817e4Smiod switch (fixP->fx_r_type)
325*3d8817e4Smiod {
326*3d8817e4Smiod case BFD_RELOC_CRX_NUM8:
327*3d8817e4Smiod fixP->fx_r_type = BFD_RELOC_CRX_SWITCH8;
328*3d8817e4Smiod break;
329*3d8817e4Smiod case BFD_RELOC_CRX_NUM16:
330*3d8817e4Smiod fixP->fx_r_type = BFD_RELOC_CRX_SWITCH16;
331*3d8817e4Smiod break;
332*3d8817e4Smiod case BFD_RELOC_CRX_NUM32:
333*3d8817e4Smiod fixP->fx_r_type = BFD_RELOC_CRX_SWITCH32;
334*3d8817e4Smiod break;
335*3d8817e4Smiod default:
336*3d8817e4Smiod abort ();
337*3d8817e4Smiod break;
338*3d8817e4Smiod }
339*3d8817e4Smiod }
340*3d8817e4Smiod else
341*3d8817e4Smiod {
342*3d8817e4Smiod /* We only resolve difference expressions in the same section. */
343*3d8817e4Smiod as_bad_where (fixP->fx_file, fixP->fx_line,
344*3d8817e4Smiod _("can't resolve `%s' {%s section} - `%s' {%s section}"),
345*3d8817e4Smiod fixP->fx_addsy ? S_GET_NAME (fixP->fx_addsy) : "0",
346*3d8817e4Smiod segment_name (fixP->fx_addsy
347*3d8817e4Smiod ? S_GET_SEGMENT (fixP->fx_addsy)
348*3d8817e4Smiod : absolute_section),
349*3d8817e4Smiod S_GET_NAME (fixP->fx_subsy),
350*3d8817e4Smiod segment_name (S_GET_SEGMENT (fixP->fx_addsy)));
351*3d8817e4Smiod }
352*3d8817e4Smiod }
353*3d8817e4Smiod
354*3d8817e4Smiod assert ((int) fixP->fx_r_type > 0);
355*3d8817e4Smiod reloc->howto = bfd_reloc_type_lookup (stdoutput, fixP->fx_r_type);
356*3d8817e4Smiod
357*3d8817e4Smiod if (reloc->howto == (reloc_howto_type *) NULL)
358*3d8817e4Smiod {
359*3d8817e4Smiod as_bad_where (fixP->fx_file, fixP->fx_line,
360*3d8817e4Smiod _("internal error: reloc %d (`%s') not supported by object file format"),
361*3d8817e4Smiod fixP->fx_r_type,
362*3d8817e4Smiod bfd_get_reloc_code_name (fixP->fx_r_type));
363*3d8817e4Smiod return NULL;
364*3d8817e4Smiod }
365*3d8817e4Smiod assert (!fixP->fx_pcrel == !reloc->howto->pc_relative);
366*3d8817e4Smiod
367*3d8817e4Smiod return reloc;
368*3d8817e4Smiod }
369*3d8817e4Smiod
370*3d8817e4Smiod /* Prepare machine-dependent frags for relaxation. */
371*3d8817e4Smiod
372*3d8817e4Smiod int
md_estimate_size_before_relax(fragS * fragp,asection * seg)373*3d8817e4Smiod md_estimate_size_before_relax (fragS *fragp, asection *seg)
374*3d8817e4Smiod {
375*3d8817e4Smiod /* If symbol is undefined or located in a different section,
376*3d8817e4Smiod select the largest supported relocation. */
377*3d8817e4Smiod relax_substateT subtype;
378*3d8817e4Smiod relax_substateT rlx_state[] = {0, 2,
379*3d8817e4Smiod 3, 4,
380*3d8817e4Smiod 5, 6};
381*3d8817e4Smiod
382*3d8817e4Smiod for (subtype = 0; subtype < ARRAY_SIZE (rlx_state); subtype += 2)
383*3d8817e4Smiod {
384*3d8817e4Smiod if (fragp->fr_subtype == rlx_state[subtype]
385*3d8817e4Smiod && (!S_IS_DEFINED (fragp->fr_symbol)
386*3d8817e4Smiod || seg != S_GET_SEGMENT (fragp->fr_symbol)))
387*3d8817e4Smiod {
388*3d8817e4Smiod fragp->fr_subtype = rlx_state[subtype + 1];
389*3d8817e4Smiod break;
390*3d8817e4Smiod }
391*3d8817e4Smiod }
392*3d8817e4Smiod
393*3d8817e4Smiod if (fragp->fr_subtype >= ARRAY_SIZE (md_relax_table))
394*3d8817e4Smiod abort ();
395*3d8817e4Smiod
396*3d8817e4Smiod return md_relax_table[fragp->fr_subtype].rlx_length;
397*3d8817e4Smiod }
398*3d8817e4Smiod
399*3d8817e4Smiod void
md_convert_frag(bfd * abfd ATTRIBUTE_UNUSED,asection * sec,fragS * fragP)400*3d8817e4Smiod md_convert_frag (bfd *abfd ATTRIBUTE_UNUSED, asection *sec, fragS *fragP)
401*3d8817e4Smiod {
402*3d8817e4Smiod /* 'opcode' points to the start of the instruction, whether
403*3d8817e4Smiod we need to change the instruction's fixed encoding. */
404*3d8817e4Smiod char *opcode = fragP->fr_literal + fragP->fr_fix;
405*3d8817e4Smiod bfd_reloc_code_real_type reloc;
406*3d8817e4Smiod
407*3d8817e4Smiod subseg_change (sec, 0);
408*3d8817e4Smiod
409*3d8817e4Smiod switch (fragP->fr_subtype)
410*3d8817e4Smiod {
411*3d8817e4Smiod case 0:
412*3d8817e4Smiod reloc = BFD_RELOC_CRX_REL8;
413*3d8817e4Smiod break;
414*3d8817e4Smiod case 1:
415*3d8817e4Smiod *opcode = 0x7e;
416*3d8817e4Smiod reloc = BFD_RELOC_CRX_REL16;
417*3d8817e4Smiod break;
418*3d8817e4Smiod case 2:
419*3d8817e4Smiod *opcode = 0x7f;
420*3d8817e4Smiod reloc = BFD_RELOC_CRX_REL32;
421*3d8817e4Smiod break;
422*3d8817e4Smiod case 3:
423*3d8817e4Smiod reloc = BFD_RELOC_CRX_REL16;
424*3d8817e4Smiod break;
425*3d8817e4Smiod case 4:
426*3d8817e4Smiod *++opcode = 0x31;
427*3d8817e4Smiod reloc = BFD_RELOC_CRX_REL32;
428*3d8817e4Smiod break;
429*3d8817e4Smiod case 5:
430*3d8817e4Smiod reloc = BFD_RELOC_CRX_REL8_CMP;
431*3d8817e4Smiod break;
432*3d8817e4Smiod case 6:
433*3d8817e4Smiod *++opcode = 0x31;
434*3d8817e4Smiod reloc = BFD_RELOC_CRX_REL24;
435*3d8817e4Smiod break;
436*3d8817e4Smiod default:
437*3d8817e4Smiod abort ();
438*3d8817e4Smiod break;
439*3d8817e4Smiod }
440*3d8817e4Smiod
441*3d8817e4Smiod fix_new (fragP, fragP->fr_fix,
442*3d8817e4Smiod bfd_get_reloc_size (bfd_reloc_type_lookup (stdoutput, reloc)),
443*3d8817e4Smiod fragP->fr_symbol, fragP->fr_offset, 1, reloc);
444*3d8817e4Smiod fragP->fr_var = 0;
445*3d8817e4Smiod fragP->fr_fix += md_relax_table[fragP->fr_subtype].rlx_length;
446*3d8817e4Smiod }
447*3d8817e4Smiod
448*3d8817e4Smiod /* Process machine-dependent command line options. Called once for
449*3d8817e4Smiod each option on the command line that the machine-independent part of
450*3d8817e4Smiod GAS does not understand. */
451*3d8817e4Smiod
452*3d8817e4Smiod int
md_parse_option(int c ATTRIBUTE_UNUSED,char * arg ATTRIBUTE_UNUSED)453*3d8817e4Smiod md_parse_option (int c ATTRIBUTE_UNUSED, char *arg ATTRIBUTE_UNUSED)
454*3d8817e4Smiod {
455*3d8817e4Smiod return 0;
456*3d8817e4Smiod }
457*3d8817e4Smiod
458*3d8817e4Smiod /* Machine-dependent usage-output. */
459*3d8817e4Smiod
460*3d8817e4Smiod void
md_show_usage(FILE * stream ATTRIBUTE_UNUSED)461*3d8817e4Smiod md_show_usage (FILE *stream ATTRIBUTE_UNUSED)
462*3d8817e4Smiod {
463*3d8817e4Smiod return;
464*3d8817e4Smiod }
465*3d8817e4Smiod
466*3d8817e4Smiod /* Turn a string in input_line_pointer into a floating point constant
467*3d8817e4Smiod of type TYPE, and store the appropriate bytes in *LITP. The number
468*3d8817e4Smiod of LITTLENUMS emitted is stored in *SIZEP. An error message is
469*3d8817e4Smiod returned, or NULL on OK. */
470*3d8817e4Smiod
471*3d8817e4Smiod char *
md_atof(int type,char * litP,int * sizeP)472*3d8817e4Smiod md_atof (int type, char *litP, int *sizeP)
473*3d8817e4Smiod {
474*3d8817e4Smiod int prec;
475*3d8817e4Smiod LITTLENUM_TYPE words[4];
476*3d8817e4Smiod char *t;
477*3d8817e4Smiod int i;
478*3d8817e4Smiod
479*3d8817e4Smiod switch (type)
480*3d8817e4Smiod {
481*3d8817e4Smiod case 'f':
482*3d8817e4Smiod prec = 2;
483*3d8817e4Smiod break;
484*3d8817e4Smiod
485*3d8817e4Smiod case 'd':
486*3d8817e4Smiod prec = 4;
487*3d8817e4Smiod break;
488*3d8817e4Smiod
489*3d8817e4Smiod default:
490*3d8817e4Smiod *sizeP = 0;
491*3d8817e4Smiod return _("bad call to md_atof");
492*3d8817e4Smiod }
493*3d8817e4Smiod
494*3d8817e4Smiod t = atof_ieee (input_line_pointer, type, words);
495*3d8817e4Smiod if (t)
496*3d8817e4Smiod input_line_pointer = t;
497*3d8817e4Smiod
498*3d8817e4Smiod *sizeP = prec * 2;
499*3d8817e4Smiod
500*3d8817e4Smiod if (! target_big_endian)
501*3d8817e4Smiod {
502*3d8817e4Smiod for (i = prec - 1; i >= 0; i--)
503*3d8817e4Smiod {
504*3d8817e4Smiod md_number_to_chars (litP, (valueT) words[i], 2);
505*3d8817e4Smiod litP += 2;
506*3d8817e4Smiod }
507*3d8817e4Smiod }
508*3d8817e4Smiod else
509*3d8817e4Smiod {
510*3d8817e4Smiod for (i = 0; i < prec; i++)
511*3d8817e4Smiod {
512*3d8817e4Smiod md_number_to_chars (litP, (valueT) words[i], 2);
513*3d8817e4Smiod litP += 2;
514*3d8817e4Smiod }
515*3d8817e4Smiod }
516*3d8817e4Smiod
517*3d8817e4Smiod return NULL;
518*3d8817e4Smiod }
519*3d8817e4Smiod
520*3d8817e4Smiod /* Apply a fixS (fixup of an instruction or data that we didn't have
521*3d8817e4Smiod enough info to complete immediately) to the data in a frag.
522*3d8817e4Smiod Since linkrelax is nonzero and TC_LINKRELAX_FIXUP is defined to disable
523*3d8817e4Smiod relaxation of debug sections, this function is called only when
524*3d8817e4Smiod fixuping relocations of debug sections. */
525*3d8817e4Smiod
526*3d8817e4Smiod void
md_apply_fix(fixS * fixP,valueT * valP,segT seg)527*3d8817e4Smiod md_apply_fix (fixS *fixP, valueT *valP, segT seg)
528*3d8817e4Smiod {
529*3d8817e4Smiod valueT val = * valP;
530*3d8817e4Smiod char *buf = fixP->fx_frag->fr_literal + fixP->fx_where;
531*3d8817e4Smiod fixP->fx_offset = 0;
532*3d8817e4Smiod
533*3d8817e4Smiod switch (fixP->fx_r_type)
534*3d8817e4Smiod {
535*3d8817e4Smiod case BFD_RELOC_CRX_NUM8:
536*3d8817e4Smiod bfd_put_8 (stdoutput, (unsigned char) val, buf);
537*3d8817e4Smiod break;
538*3d8817e4Smiod case BFD_RELOC_CRX_NUM16:
539*3d8817e4Smiod bfd_put_16 (stdoutput, val, buf);
540*3d8817e4Smiod break;
541*3d8817e4Smiod case BFD_RELOC_CRX_NUM32:
542*3d8817e4Smiod bfd_put_32 (stdoutput, val, buf);
543*3d8817e4Smiod break;
544*3d8817e4Smiod default:
545*3d8817e4Smiod /* We shouldn't ever get here because linkrelax is nonzero. */
546*3d8817e4Smiod abort ();
547*3d8817e4Smiod break;
548*3d8817e4Smiod }
549*3d8817e4Smiod
550*3d8817e4Smiod fixP->fx_done = 0;
551*3d8817e4Smiod
552*3d8817e4Smiod if (fixP->fx_addsy == NULL
553*3d8817e4Smiod && fixP->fx_pcrel == 0)
554*3d8817e4Smiod fixP->fx_done = 1;
555*3d8817e4Smiod
556*3d8817e4Smiod if (fixP->fx_pcrel == 1
557*3d8817e4Smiod && fixP->fx_addsy != NULL
558*3d8817e4Smiod && S_GET_SEGMENT (fixP->fx_addsy) == seg)
559*3d8817e4Smiod fixP->fx_done = 1;
560*3d8817e4Smiod }
561*3d8817e4Smiod
562*3d8817e4Smiod /* The location from which a PC relative jump should be calculated,
563*3d8817e4Smiod given a PC relative reloc. */
564*3d8817e4Smiod
565*3d8817e4Smiod long
md_pcrel_from(fixS * fixp)566*3d8817e4Smiod md_pcrel_from (fixS *fixp)
567*3d8817e4Smiod {
568*3d8817e4Smiod return fixp->fx_frag->fr_address + fixp->fx_where;
569*3d8817e4Smiod }
570*3d8817e4Smiod
571*3d8817e4Smiod /* This function is called once, at assembler startup time. This should
572*3d8817e4Smiod set up all the tables, etc that the MD part of the assembler needs. */
573*3d8817e4Smiod
574*3d8817e4Smiod void
md_begin(void)575*3d8817e4Smiod md_begin (void)
576*3d8817e4Smiod {
577*3d8817e4Smiod const char *hashret = NULL;
578*3d8817e4Smiod int i = 0;
579*3d8817e4Smiod
580*3d8817e4Smiod /* Set up a hash table for the instructions. */
581*3d8817e4Smiod if ((crx_inst_hash = hash_new ()) == NULL)
582*3d8817e4Smiod as_fatal (_("Virtual memory exhausted"));
583*3d8817e4Smiod
584*3d8817e4Smiod while (crx_instruction[i].mnemonic != NULL)
585*3d8817e4Smiod {
586*3d8817e4Smiod const char *mnemonic = crx_instruction[i].mnemonic;
587*3d8817e4Smiod
588*3d8817e4Smiod hashret = hash_insert (crx_inst_hash, mnemonic,
589*3d8817e4Smiod (PTR) &crx_instruction[i]);
590*3d8817e4Smiod
591*3d8817e4Smiod if (hashret != NULL && *hashret != '\0')
592*3d8817e4Smiod as_fatal (_("Can't hash `%s': %s\n"), crx_instruction[i].mnemonic,
593*3d8817e4Smiod *hashret == 0 ? _("(unknown reason)") : hashret);
594*3d8817e4Smiod
595*3d8817e4Smiod /* Insert unique names into hash table. The CRX instruction set
596*3d8817e4Smiod has many identical opcode names that have different opcodes based
597*3d8817e4Smiod on the operands. This hash table then provides a quick index to
598*3d8817e4Smiod the first opcode with a particular name in the opcode table. */
599*3d8817e4Smiod do
600*3d8817e4Smiod {
601*3d8817e4Smiod ++i;
602*3d8817e4Smiod }
603*3d8817e4Smiod while (crx_instruction[i].mnemonic != NULL
604*3d8817e4Smiod && streq (crx_instruction[i].mnemonic, mnemonic));
605*3d8817e4Smiod }
606*3d8817e4Smiod
607*3d8817e4Smiod /* Initialize reg_hash hash table. */
608*3d8817e4Smiod if ((reg_hash = hash_new ()) == NULL)
609*3d8817e4Smiod as_fatal (_("Virtual memory exhausted"));
610*3d8817e4Smiod
611*3d8817e4Smiod {
612*3d8817e4Smiod const reg_entry *regtab;
613*3d8817e4Smiod
614*3d8817e4Smiod for (regtab = crx_regtab;
615*3d8817e4Smiod regtab < (crx_regtab + NUMREGS); regtab++)
616*3d8817e4Smiod {
617*3d8817e4Smiod hashret = hash_insert (reg_hash, regtab->name, (PTR) regtab);
618*3d8817e4Smiod if (hashret)
619*3d8817e4Smiod as_fatal (_("Internal Error: Can't hash %s: %s"),
620*3d8817e4Smiod regtab->name,
621*3d8817e4Smiod hashret);
622*3d8817e4Smiod }
623*3d8817e4Smiod }
624*3d8817e4Smiod
625*3d8817e4Smiod /* Initialize copreg_hash hash table. */
626*3d8817e4Smiod if ((copreg_hash = hash_new ()) == NULL)
627*3d8817e4Smiod as_fatal (_("Virtual memory exhausted"));
628*3d8817e4Smiod
629*3d8817e4Smiod {
630*3d8817e4Smiod const reg_entry *copregtab;
631*3d8817e4Smiod
632*3d8817e4Smiod for (copregtab = crx_copregtab; copregtab < (crx_copregtab + NUMCOPREGS);
633*3d8817e4Smiod copregtab++)
634*3d8817e4Smiod {
635*3d8817e4Smiod hashret = hash_insert (copreg_hash, copregtab->name, (PTR) copregtab);
636*3d8817e4Smiod if (hashret)
637*3d8817e4Smiod as_fatal (_("Internal Error: Can't hash %s: %s"),
638*3d8817e4Smiod copregtab->name,
639*3d8817e4Smiod hashret);
640*3d8817e4Smiod }
641*3d8817e4Smiod }
642*3d8817e4Smiod /* Set linkrelax here to avoid fixups in most sections. */
643*3d8817e4Smiod linkrelax = 1;
644*3d8817e4Smiod }
645*3d8817e4Smiod
646*3d8817e4Smiod /* Process constants (immediate/absolute)
647*3d8817e4Smiod and labels (jump targets/Memory locations). */
648*3d8817e4Smiod
649*3d8817e4Smiod static void
process_label_constant(char * str,ins * crx_ins)650*3d8817e4Smiod process_label_constant (char *str, ins * crx_ins)
651*3d8817e4Smiod {
652*3d8817e4Smiod char *saved_input_line_pointer;
653*3d8817e4Smiod argument *cur_arg = &crx_ins->arg[cur_arg_num]; /* Current argument. */
654*3d8817e4Smiod
655*3d8817e4Smiod saved_input_line_pointer = input_line_pointer;
656*3d8817e4Smiod input_line_pointer = str;
657*3d8817e4Smiod
658*3d8817e4Smiod expression (&crx_ins->exp);
659*3d8817e4Smiod
660*3d8817e4Smiod switch (crx_ins->exp.X_op)
661*3d8817e4Smiod {
662*3d8817e4Smiod case O_big:
663*3d8817e4Smiod case O_absent:
664*3d8817e4Smiod /* Missing or bad expr becomes absolute 0. */
665*3d8817e4Smiod as_bad (_("missing or invalid displacement expression `%s' taken as 0"),
666*3d8817e4Smiod str);
667*3d8817e4Smiod crx_ins->exp.X_op = O_constant;
668*3d8817e4Smiod crx_ins->exp.X_add_number = 0;
669*3d8817e4Smiod crx_ins->exp.X_add_symbol = (symbolS *) 0;
670*3d8817e4Smiod crx_ins->exp.X_op_symbol = (symbolS *) 0;
671*3d8817e4Smiod /* Fall through. */
672*3d8817e4Smiod
673*3d8817e4Smiod case O_constant:
674*3d8817e4Smiod cur_arg->X_op = O_constant;
675*3d8817e4Smiod cur_arg->constant = crx_ins->exp.X_add_number;
676*3d8817e4Smiod break;
677*3d8817e4Smiod
678*3d8817e4Smiod case O_symbol:
679*3d8817e4Smiod case O_subtract:
680*3d8817e4Smiod case O_add:
681*3d8817e4Smiod cur_arg->X_op = O_symbol;
682*3d8817e4Smiod crx_ins->rtype = BFD_RELOC_NONE;
683*3d8817e4Smiod relocatable = 1;
684*3d8817e4Smiod
685*3d8817e4Smiod switch (cur_arg->type)
686*3d8817e4Smiod {
687*3d8817e4Smiod case arg_cr:
688*3d8817e4Smiod if (IS_INSN_TYPE (LD_STOR_INS_INC))
689*3d8817e4Smiod crx_ins->rtype = BFD_RELOC_CRX_REGREL12;
690*3d8817e4Smiod else if (IS_INSN_TYPE (CSTBIT_INS)
691*3d8817e4Smiod || IS_INSN_TYPE (STOR_IMM_INS))
692*3d8817e4Smiod crx_ins->rtype = BFD_RELOC_CRX_REGREL28;
693*3d8817e4Smiod else
694*3d8817e4Smiod crx_ins->rtype = BFD_RELOC_CRX_REGREL32;
695*3d8817e4Smiod break;
696*3d8817e4Smiod
697*3d8817e4Smiod case arg_idxr:
698*3d8817e4Smiod crx_ins->rtype = BFD_RELOC_CRX_REGREL22;
699*3d8817e4Smiod break;
700*3d8817e4Smiod
701*3d8817e4Smiod case arg_c:
702*3d8817e4Smiod if (IS_INSN_MNEMONIC ("bal") || IS_INSN_TYPE (DCR_BRANCH_INS))
703*3d8817e4Smiod crx_ins->rtype = BFD_RELOC_CRX_REL16;
704*3d8817e4Smiod else if (IS_INSN_TYPE (BRANCH_INS))
705*3d8817e4Smiod crx_ins->rtype = BFD_RELOC_CRX_REL8;
706*3d8817e4Smiod else if (IS_INSN_TYPE (LD_STOR_INS) || IS_INSN_TYPE (STOR_IMM_INS)
707*3d8817e4Smiod || IS_INSN_TYPE (CSTBIT_INS))
708*3d8817e4Smiod crx_ins->rtype = BFD_RELOC_CRX_ABS32;
709*3d8817e4Smiod else if (IS_INSN_TYPE (BRANCH_NEQ_INS))
710*3d8817e4Smiod crx_ins->rtype = BFD_RELOC_CRX_REL4;
711*3d8817e4Smiod else if (IS_INSN_TYPE (CMPBR_INS) || IS_INSN_TYPE (COP_BRANCH_INS))
712*3d8817e4Smiod crx_ins->rtype = BFD_RELOC_CRX_REL8_CMP;
713*3d8817e4Smiod break;
714*3d8817e4Smiod
715*3d8817e4Smiod case arg_ic:
716*3d8817e4Smiod if (IS_INSN_TYPE (ARITH_INS))
717*3d8817e4Smiod crx_ins->rtype = BFD_RELOC_CRX_IMM32;
718*3d8817e4Smiod else if (IS_INSN_TYPE (ARITH_BYTE_INS))
719*3d8817e4Smiod crx_ins->rtype = BFD_RELOC_CRX_IMM16;
720*3d8817e4Smiod break;
721*3d8817e4Smiod default:
722*3d8817e4Smiod break;
723*3d8817e4Smiod }
724*3d8817e4Smiod break;
725*3d8817e4Smiod
726*3d8817e4Smiod default:
727*3d8817e4Smiod cur_arg->X_op = crx_ins->exp.X_op;
728*3d8817e4Smiod break;
729*3d8817e4Smiod }
730*3d8817e4Smiod
731*3d8817e4Smiod input_line_pointer = saved_input_line_pointer;
732*3d8817e4Smiod return;
733*3d8817e4Smiod }
734*3d8817e4Smiod
735*3d8817e4Smiod /* Get the values of the scale to be encoded -
736*3d8817e4Smiod used for the scaled index mode of addressing. */
737*3d8817e4Smiod
738*3d8817e4Smiod static int
exponent2scale(int val)739*3d8817e4Smiod exponent2scale (int val)
740*3d8817e4Smiod {
741*3d8817e4Smiod int exponent;
742*3d8817e4Smiod
743*3d8817e4Smiod /* If 'val' is 0, the following 'for' will be an endless loop. */
744*3d8817e4Smiod if (val == 0)
745*3d8817e4Smiod return 0;
746*3d8817e4Smiod
747*3d8817e4Smiod for (exponent = 0; (val != 1); val >>= 1, exponent++)
748*3d8817e4Smiod ;
749*3d8817e4Smiod
750*3d8817e4Smiod return exponent;
751*3d8817e4Smiod }
752*3d8817e4Smiod
753*3d8817e4Smiod /* Parsing different types of operands
754*3d8817e4Smiod -> constants Immediate/Absolute/Relative numbers
755*3d8817e4Smiod -> Labels Relocatable symbols
756*3d8817e4Smiod -> (rbase) Register base
757*3d8817e4Smiod -> disp(rbase) Register relative
758*3d8817e4Smiod -> disp(rbase)+ Post-increment mode
759*3d8817e4Smiod -> disp(rbase,ridx,scl) Register index mode */
760*3d8817e4Smiod
761*3d8817e4Smiod static void
set_operand(char * operand,ins * crx_ins)762*3d8817e4Smiod set_operand (char *operand, ins * crx_ins)
763*3d8817e4Smiod {
764*3d8817e4Smiod char *operandS; /* Pointer to start of sub-opearand. */
765*3d8817e4Smiod char *operandE; /* Pointer to end of sub-opearand. */
766*3d8817e4Smiod expressionS scale;
767*3d8817e4Smiod int scale_val;
768*3d8817e4Smiod char *input_save, c;
769*3d8817e4Smiod argument *cur_arg = &crx_ins->arg[cur_arg_num]; /* Current argument. */
770*3d8817e4Smiod
771*3d8817e4Smiod /* Initialize pointers. */
772*3d8817e4Smiod operandS = operandE = operand;
773*3d8817e4Smiod
774*3d8817e4Smiod switch (cur_arg->type)
775*3d8817e4Smiod {
776*3d8817e4Smiod case arg_sc: /* Case *+0x18. */
777*3d8817e4Smiod case arg_ic: /* Case $0x18. */
778*3d8817e4Smiod operandS++;
779*3d8817e4Smiod case arg_c: /* Case 0x18. */
780*3d8817e4Smiod /* Set constant. */
781*3d8817e4Smiod process_label_constant (operandS, crx_ins);
782*3d8817e4Smiod
783*3d8817e4Smiod if (cur_arg->type != arg_ic)
784*3d8817e4Smiod cur_arg->type = arg_c;
785*3d8817e4Smiod break;
786*3d8817e4Smiod
787*3d8817e4Smiod case arg_icr: /* Case $0x18(r1). */
788*3d8817e4Smiod operandS++;
789*3d8817e4Smiod case arg_cr: /* Case 0x18(r1). */
790*3d8817e4Smiod /* Set displacement constant. */
791*3d8817e4Smiod while (*operandE != '(')
792*3d8817e4Smiod operandE++;
793*3d8817e4Smiod *operandE = '\0';
794*3d8817e4Smiod process_label_constant (operandS, crx_ins);
795*3d8817e4Smiod operandS = operandE;
796*3d8817e4Smiod case arg_rbase: /* Case (r1). */
797*3d8817e4Smiod operandS++;
798*3d8817e4Smiod /* Set register base. */
799*3d8817e4Smiod while (*operandE != ')')
800*3d8817e4Smiod operandE++;
801*3d8817e4Smiod *operandE = '\0';
802*3d8817e4Smiod if ((cur_arg->r = get_register (operandS)) == nullregister)
803*3d8817e4Smiod as_bad (_("Illegal register `%s' in Instruction `%s'"),
804*3d8817e4Smiod operandS, ins_parse);
805*3d8817e4Smiod
806*3d8817e4Smiod if (cur_arg->type != arg_rbase)
807*3d8817e4Smiod cur_arg->type = arg_cr;
808*3d8817e4Smiod break;
809*3d8817e4Smiod
810*3d8817e4Smiod case arg_idxr:
811*3d8817e4Smiod /* Set displacement constant. */
812*3d8817e4Smiod while (*operandE != '(')
813*3d8817e4Smiod operandE++;
814*3d8817e4Smiod *operandE = '\0';
815*3d8817e4Smiod process_label_constant (operandS, crx_ins);
816*3d8817e4Smiod operandS = ++operandE;
817*3d8817e4Smiod
818*3d8817e4Smiod /* Set register base. */
819*3d8817e4Smiod while ((*operandE != ',') && (! ISSPACE (*operandE)))
820*3d8817e4Smiod operandE++;
821*3d8817e4Smiod *operandE++ = '\0';
822*3d8817e4Smiod if ((cur_arg->r = get_register (operandS)) == nullregister)
823*3d8817e4Smiod as_bad (_("Illegal register `%s' in Instruction `%s'"),
824*3d8817e4Smiod operandS, ins_parse);
825*3d8817e4Smiod
826*3d8817e4Smiod /* Skip leading white space. */
827*3d8817e4Smiod while (ISSPACE (*operandE))
828*3d8817e4Smiod operandE++;
829*3d8817e4Smiod operandS = operandE;
830*3d8817e4Smiod
831*3d8817e4Smiod /* Set register index. */
832*3d8817e4Smiod while ((*operandE != ')') && (*operandE != ','))
833*3d8817e4Smiod operandE++;
834*3d8817e4Smiod c = *operandE;
835*3d8817e4Smiod *operandE++ = '\0';
836*3d8817e4Smiod
837*3d8817e4Smiod if ((cur_arg->i_r = get_register (operandS)) == nullregister)
838*3d8817e4Smiod as_bad (_("Illegal register `%s' in Instruction `%s'"),
839*3d8817e4Smiod operandS, ins_parse);
840*3d8817e4Smiod
841*3d8817e4Smiod /* Skip leading white space. */
842*3d8817e4Smiod while (ISSPACE (*operandE))
843*3d8817e4Smiod operandE++;
844*3d8817e4Smiod operandS = operandE;
845*3d8817e4Smiod
846*3d8817e4Smiod /* Set the scale. */
847*3d8817e4Smiod if (c == ')')
848*3d8817e4Smiod cur_arg->scale = 0;
849*3d8817e4Smiod else
850*3d8817e4Smiod {
851*3d8817e4Smiod while (*operandE != ')')
852*3d8817e4Smiod operandE++;
853*3d8817e4Smiod *operandE = '\0';
854*3d8817e4Smiod
855*3d8817e4Smiod /* Preprocess the scale string. */
856*3d8817e4Smiod input_save = input_line_pointer;
857*3d8817e4Smiod input_line_pointer = operandS;
858*3d8817e4Smiod expression (&scale);
859*3d8817e4Smiod input_line_pointer = input_save;
860*3d8817e4Smiod
861*3d8817e4Smiod scale_val = scale.X_add_number;
862*3d8817e4Smiod
863*3d8817e4Smiod /* Check if the scale value is legal. */
864*3d8817e4Smiod if (scale_val != 1 && scale_val != 2
865*3d8817e4Smiod && scale_val != 4 && scale_val != 8)
866*3d8817e4Smiod as_bad (_("Illegal Scale - `%d'"), scale_val);
867*3d8817e4Smiod
868*3d8817e4Smiod cur_arg->scale = exponent2scale (scale_val);
869*3d8817e4Smiod }
870*3d8817e4Smiod break;
871*3d8817e4Smiod
872*3d8817e4Smiod default:
873*3d8817e4Smiod break;
874*3d8817e4Smiod }
875*3d8817e4Smiod }
876*3d8817e4Smiod
877*3d8817e4Smiod /* Parse a single operand.
878*3d8817e4Smiod operand - Current operand to parse.
879*3d8817e4Smiod crx_ins - Current assembled instruction. */
880*3d8817e4Smiod
881*3d8817e4Smiod static void
parse_operand(char * operand,ins * crx_ins)882*3d8817e4Smiod parse_operand (char *operand, ins * crx_ins)
883*3d8817e4Smiod {
884*3d8817e4Smiod int ret_val;
885*3d8817e4Smiod argument *cur_arg = &crx_ins->arg[cur_arg_num]; /* Current argument. */
886*3d8817e4Smiod
887*3d8817e4Smiod /* Initialize the type to NULL before parsing. */
888*3d8817e4Smiod cur_arg->type = nullargs;
889*3d8817e4Smiod
890*3d8817e4Smiod /* Check whether this is a general processor register. */
891*3d8817e4Smiod if ((ret_val = get_register (operand)) != nullregister)
892*3d8817e4Smiod {
893*3d8817e4Smiod cur_arg->type = arg_r;
894*3d8817e4Smiod cur_arg->r = ret_val;
895*3d8817e4Smiod cur_arg->X_op = O_register;
896*3d8817e4Smiod return;
897*3d8817e4Smiod }
898*3d8817e4Smiod
899*3d8817e4Smiod /* Check whether this is a core [special] coprocessor register. */
900*3d8817e4Smiod if ((ret_val = get_copregister (operand)) != nullcopregister)
901*3d8817e4Smiod {
902*3d8817e4Smiod cur_arg->type = arg_copr;
903*3d8817e4Smiod if (ret_val >= cs0)
904*3d8817e4Smiod cur_arg->type = arg_copsr;
905*3d8817e4Smiod cur_arg->cr = ret_val;
906*3d8817e4Smiod cur_arg->X_op = O_register;
907*3d8817e4Smiod return;
908*3d8817e4Smiod }
909*3d8817e4Smiod
910*3d8817e4Smiod /* Deal with special characters. */
911*3d8817e4Smiod switch (operand[0])
912*3d8817e4Smiod {
913*3d8817e4Smiod case '$':
914*3d8817e4Smiod if (strchr (operand, '(') != NULL)
915*3d8817e4Smiod cur_arg->type = arg_icr;
916*3d8817e4Smiod else
917*3d8817e4Smiod cur_arg->type = arg_ic;
918*3d8817e4Smiod goto set_params;
919*3d8817e4Smiod break;
920*3d8817e4Smiod
921*3d8817e4Smiod case '*':
922*3d8817e4Smiod cur_arg->type = arg_sc;
923*3d8817e4Smiod goto set_params;
924*3d8817e4Smiod break;
925*3d8817e4Smiod
926*3d8817e4Smiod case '(':
927*3d8817e4Smiod cur_arg->type = arg_rbase;
928*3d8817e4Smiod goto set_params;
929*3d8817e4Smiod break;
930*3d8817e4Smiod
931*3d8817e4Smiod default:
932*3d8817e4Smiod break;
933*3d8817e4Smiod }
934*3d8817e4Smiod
935*3d8817e4Smiod if (strchr (operand, '(') != NULL)
936*3d8817e4Smiod {
937*3d8817e4Smiod if (strchr (operand, ',') != NULL
938*3d8817e4Smiod && (strchr (operand, ',') > strchr (operand, '(')))
939*3d8817e4Smiod cur_arg->type = arg_idxr;
940*3d8817e4Smiod else
941*3d8817e4Smiod cur_arg->type = arg_cr;
942*3d8817e4Smiod }
943*3d8817e4Smiod else
944*3d8817e4Smiod cur_arg->type = arg_c;
945*3d8817e4Smiod goto set_params;
946*3d8817e4Smiod
947*3d8817e4Smiod /* Parse an operand according to its type. */
948*3d8817e4Smiod set_params:
949*3d8817e4Smiod cur_arg->constant = 0;
950*3d8817e4Smiod set_operand (operand, crx_ins);
951*3d8817e4Smiod }
952*3d8817e4Smiod
953*3d8817e4Smiod /* Parse the various operands. Each operand is then analyzed to fillup
954*3d8817e4Smiod the fields in the crx_ins data structure. */
955*3d8817e4Smiod
956*3d8817e4Smiod static void
parse_operands(ins * crx_ins,char * operands)957*3d8817e4Smiod parse_operands (ins * crx_ins, char *operands)
958*3d8817e4Smiod {
959*3d8817e4Smiod char *operandS; /* Operands string. */
960*3d8817e4Smiod char *operandH, *operandT; /* Single operand head/tail pointers. */
961*3d8817e4Smiod int allocated = 0; /* Indicates a new operands string was allocated. */
962*3d8817e4Smiod char *operand[MAX_OPERANDS]; /* Separating the operands. */
963*3d8817e4Smiod int op_num = 0; /* Current operand number we are parsing. */
964*3d8817e4Smiod int bracket_flag = 0; /* Indicates a bracket '(' was found. */
965*3d8817e4Smiod int sq_bracket_flag = 0; /* Indicates a square bracket '[' was found. */
966*3d8817e4Smiod
967*3d8817e4Smiod /* Preprocess the list of registers, if necessary. */
968*3d8817e4Smiod operandS = operandH = operandT = (INST_HAS_REG_LIST) ?
969*3d8817e4Smiod preprocess_reglist (operands, &allocated) : operands;
970*3d8817e4Smiod
971*3d8817e4Smiod while (*operandT != '\0')
972*3d8817e4Smiod {
973*3d8817e4Smiod if (*operandT == ',' && bracket_flag != 1 && sq_bracket_flag != 1)
974*3d8817e4Smiod {
975*3d8817e4Smiod *operandT++ = '\0';
976*3d8817e4Smiod operand[op_num++] = strdup (operandH);
977*3d8817e4Smiod operandH = operandT;
978*3d8817e4Smiod continue;
979*3d8817e4Smiod }
980*3d8817e4Smiod
981*3d8817e4Smiod if (*operandT == ' ')
982*3d8817e4Smiod as_bad (_("Illegal operands (whitespace): `%s'"), ins_parse);
983*3d8817e4Smiod
984*3d8817e4Smiod if (*operandT == '(')
985*3d8817e4Smiod bracket_flag = 1;
986*3d8817e4Smiod else if (*operandT == '[')
987*3d8817e4Smiod sq_bracket_flag = 1;
988*3d8817e4Smiod
989*3d8817e4Smiod if (*operandT == ')')
990*3d8817e4Smiod {
991*3d8817e4Smiod if (bracket_flag)
992*3d8817e4Smiod bracket_flag = 0;
993*3d8817e4Smiod else
994*3d8817e4Smiod as_fatal (_("Missing matching brackets : `%s'"), ins_parse);
995*3d8817e4Smiod }
996*3d8817e4Smiod else if (*operandT == ']')
997*3d8817e4Smiod {
998*3d8817e4Smiod if (sq_bracket_flag)
999*3d8817e4Smiod sq_bracket_flag = 0;
1000*3d8817e4Smiod else
1001*3d8817e4Smiod as_fatal (_("Missing matching brackets : `%s'"), ins_parse);
1002*3d8817e4Smiod }
1003*3d8817e4Smiod
1004*3d8817e4Smiod if (bracket_flag == 1 && *operandT == ')')
1005*3d8817e4Smiod bracket_flag = 0;
1006*3d8817e4Smiod else if (sq_bracket_flag == 1 && *operandT == ']')
1007*3d8817e4Smiod sq_bracket_flag = 0;
1008*3d8817e4Smiod
1009*3d8817e4Smiod operandT++;
1010*3d8817e4Smiod }
1011*3d8817e4Smiod
1012*3d8817e4Smiod /* Adding the last operand. */
1013*3d8817e4Smiod operand[op_num++] = strdup (operandH);
1014*3d8817e4Smiod crx_ins->nargs = op_num;
1015*3d8817e4Smiod
1016*3d8817e4Smiod /* Verifying correct syntax of operands (all brackets should be closed). */
1017*3d8817e4Smiod if (bracket_flag || sq_bracket_flag)
1018*3d8817e4Smiod as_fatal (_("Missing matching brackets : `%s'"), ins_parse);
1019*3d8817e4Smiod
1020*3d8817e4Smiod /* Now we parse each operand separately. */
1021*3d8817e4Smiod for (op_num = 0; op_num < crx_ins->nargs; op_num++)
1022*3d8817e4Smiod {
1023*3d8817e4Smiod cur_arg_num = op_num;
1024*3d8817e4Smiod parse_operand (operand[op_num], crx_ins);
1025*3d8817e4Smiod free (operand[op_num]);
1026*3d8817e4Smiod }
1027*3d8817e4Smiod
1028*3d8817e4Smiod if (allocated)
1029*3d8817e4Smiod free (operandS);
1030*3d8817e4Smiod }
1031*3d8817e4Smiod
1032*3d8817e4Smiod /* Get the trap index in dispatch table, given its name.
1033*3d8817e4Smiod This routine is used by assembling the 'excp' instruction. */
1034*3d8817e4Smiod
1035*3d8817e4Smiod static int
gettrap(char * s)1036*3d8817e4Smiod gettrap (char *s)
1037*3d8817e4Smiod {
1038*3d8817e4Smiod const trap_entry *trap;
1039*3d8817e4Smiod
1040*3d8817e4Smiod for (trap = crx_traps; trap < (crx_traps + NUMTRAPS); trap++)
1041*3d8817e4Smiod if (strcasecmp (trap->name, s) == 0)
1042*3d8817e4Smiod return trap->entry;
1043*3d8817e4Smiod
1044*3d8817e4Smiod as_bad (_("Unknown exception: `%s'"), s);
1045*3d8817e4Smiod return 0;
1046*3d8817e4Smiod }
1047*3d8817e4Smiod
1048*3d8817e4Smiod /* Post-Increment instructions, as well as Store-Immediate instructions, are a
1049*3d8817e4Smiod sub-group within load/stor instruction groups.
1050*3d8817e4Smiod Therefore, when parsing a Post-Increment/Store-Immediate insn, we have to
1051*3d8817e4Smiod advance the instruction pointer to the start of that sub-group (that is, up
1052*3d8817e4Smiod to the first instruction of that type).
1053*3d8817e4Smiod Otherwise, the insn will be mistakenly identified as of type LD_STOR_INS. */
1054*3d8817e4Smiod
1055*3d8817e4Smiod static void
handle_LoadStor(char * operands)1056*3d8817e4Smiod handle_LoadStor (char *operands)
1057*3d8817e4Smiod {
1058*3d8817e4Smiod /* Post-Increment instructions precede Store-Immediate instructions in
1059*3d8817e4Smiod CRX instruction table, hence they are handled before.
1060*3d8817e4Smiod This synchronization should be kept. */
1061*3d8817e4Smiod
1062*3d8817e4Smiod /* Assuming Post-Increment insn has the following format :
1063*3d8817e4Smiod 'MNEMONIC DISP(REG)+, REG' (e.g. 'loadw 12(r5)+, r6').
1064*3d8817e4Smiod LD_STOR_INS_INC are the only store insns containing a plus sign (+). */
1065*3d8817e4Smiod if (strstr (operands, ")+") != NULL)
1066*3d8817e4Smiod {
1067*3d8817e4Smiod while (! IS_INSN_TYPE (LD_STOR_INS_INC))
1068*3d8817e4Smiod instruction++;
1069*3d8817e4Smiod return;
1070*3d8817e4Smiod }
1071*3d8817e4Smiod
1072*3d8817e4Smiod /* Assuming Store-Immediate insn has the following format :
1073*3d8817e4Smiod 'MNEMONIC $DISP, ...' (e.g. 'storb $1, 12(r5)').
1074*3d8817e4Smiod STOR_IMM_INS are the only store insns containing a dollar sign ($). */
1075*3d8817e4Smiod if (strstr (operands, "$") != NULL)
1076*3d8817e4Smiod while (! IS_INSN_TYPE (STOR_IMM_INS))
1077*3d8817e4Smiod instruction++;
1078*3d8817e4Smiod }
1079*3d8817e4Smiod
1080*3d8817e4Smiod /* Top level module where instruction parsing starts.
1081*3d8817e4Smiod crx_ins - data structure holds some information.
1082*3d8817e4Smiod operands - holds the operands part of the whole instruction. */
1083*3d8817e4Smiod
1084*3d8817e4Smiod static void
parse_insn(ins * insn,char * operands)1085*3d8817e4Smiod parse_insn (ins *insn, char *operands)
1086*3d8817e4Smiod {
1087*3d8817e4Smiod int i;
1088*3d8817e4Smiod
1089*3d8817e4Smiod /* Handle instructions with no operands. */
1090*3d8817e4Smiod for (i = 0; no_op_insn[i] != NULL; i++)
1091*3d8817e4Smiod {
1092*3d8817e4Smiod if (streq (no_op_insn[i], instruction->mnemonic))
1093*3d8817e4Smiod {
1094*3d8817e4Smiod insn->nargs = 0;
1095*3d8817e4Smiod return;
1096*3d8817e4Smiod }
1097*3d8817e4Smiod }
1098*3d8817e4Smiod
1099*3d8817e4Smiod /* Handle 'excp'/'cinv' instructions. */
1100*3d8817e4Smiod if (IS_INSN_MNEMONIC ("excp") || IS_INSN_MNEMONIC ("cinv"))
1101*3d8817e4Smiod {
1102*3d8817e4Smiod insn->nargs = 1;
1103*3d8817e4Smiod insn->arg[0].type = arg_ic;
1104*3d8817e4Smiod insn->arg[0].constant = IS_INSN_MNEMONIC ("excp") ?
1105*3d8817e4Smiod gettrap (operands) : get_cinv_parameters (operands);
1106*3d8817e4Smiod insn->arg[0].X_op = O_constant;
1107*3d8817e4Smiod return;
1108*3d8817e4Smiod }
1109*3d8817e4Smiod
1110*3d8817e4Smiod /* Handle load/stor unique instructions before parsing. */
1111*3d8817e4Smiod if (IS_INSN_TYPE (LD_STOR_INS))
1112*3d8817e4Smiod handle_LoadStor (operands);
1113*3d8817e4Smiod
1114*3d8817e4Smiod if (operands != NULL)
1115*3d8817e4Smiod parse_operands (insn, operands);
1116*3d8817e4Smiod }
1117*3d8817e4Smiod
1118*3d8817e4Smiod /* Cinv instruction requires special handling. */
1119*3d8817e4Smiod
1120*3d8817e4Smiod static int
get_cinv_parameters(char * operand)1121*3d8817e4Smiod get_cinv_parameters (char * operand)
1122*3d8817e4Smiod {
1123*3d8817e4Smiod char *p = operand;
1124*3d8817e4Smiod int d_used = 0, i_used = 0, u_used = 0, b_used = 0;
1125*3d8817e4Smiod
1126*3d8817e4Smiod while (*++p != ']')
1127*3d8817e4Smiod {
1128*3d8817e4Smiod if (*p == ',' || *p == ' ')
1129*3d8817e4Smiod continue;
1130*3d8817e4Smiod
1131*3d8817e4Smiod if (*p == 'd')
1132*3d8817e4Smiod d_used = 1;
1133*3d8817e4Smiod else if (*p == 'i')
1134*3d8817e4Smiod i_used = 1;
1135*3d8817e4Smiod else if (*p == 'u')
1136*3d8817e4Smiod u_used = 1;
1137*3d8817e4Smiod else if (*p == 'b')
1138*3d8817e4Smiod b_used = 1;
1139*3d8817e4Smiod else
1140*3d8817e4Smiod as_bad (_("Illegal `cinv' parameter: `%c'"), *p);
1141*3d8817e4Smiod }
1142*3d8817e4Smiod
1143*3d8817e4Smiod return ((b_used ? 8 : 0)
1144*3d8817e4Smiod + (d_used ? 4 : 0)
1145*3d8817e4Smiod + (i_used ? 2 : 0)
1146*3d8817e4Smiod + (u_used ? 1 : 0));
1147*3d8817e4Smiod }
1148*3d8817e4Smiod
1149*3d8817e4Smiod /* Retrieve the opcode image of a given register.
1150*3d8817e4Smiod If the register is illegal for the current instruction,
1151*3d8817e4Smiod issue an error. */
1152*3d8817e4Smiod
1153*3d8817e4Smiod static int
getreg_image(reg r)1154*3d8817e4Smiod getreg_image (reg r)
1155*3d8817e4Smiod {
1156*3d8817e4Smiod const reg_entry *reg;
1157*3d8817e4Smiod char *reg_name;
1158*3d8817e4Smiod int is_procreg = 0; /* Nonzero means argument should be processor reg. */
1159*3d8817e4Smiod
1160*3d8817e4Smiod if (((IS_INSN_MNEMONIC ("mtpr")) && (cur_arg_num == 1))
1161*3d8817e4Smiod || ((IS_INSN_MNEMONIC ("mfpr")) && (cur_arg_num == 0)) )
1162*3d8817e4Smiod is_procreg = 1;
1163*3d8817e4Smiod
1164*3d8817e4Smiod /* Check whether the register is in registers table. */
1165*3d8817e4Smiod if (r < MAX_REG)
1166*3d8817e4Smiod reg = &crx_regtab[r];
1167*3d8817e4Smiod /* Check whether the register is in coprocessor registers table. */
1168*3d8817e4Smiod else if (r < MAX_COPREG)
1169*3d8817e4Smiod reg = &crx_copregtab[r-MAX_REG];
1170*3d8817e4Smiod /* Register not found. */
1171*3d8817e4Smiod else
1172*3d8817e4Smiod {
1173*3d8817e4Smiod as_bad (_("Unknown register: `%d'"), r);
1174*3d8817e4Smiod return 0;
1175*3d8817e4Smiod }
1176*3d8817e4Smiod
1177*3d8817e4Smiod reg_name = reg->name;
1178*3d8817e4Smiod
1179*3d8817e4Smiod /* Issue a error message when register is illegal. */
1180*3d8817e4Smiod #define IMAGE_ERR \
1181*3d8817e4Smiod as_bad (_("Illegal register (`%s') in Instruction: `%s'"), \
1182*3d8817e4Smiod reg_name, ins_parse); \
1183*3d8817e4Smiod break;
1184*3d8817e4Smiod
1185*3d8817e4Smiod switch (reg->type)
1186*3d8817e4Smiod {
1187*3d8817e4Smiod case CRX_U_REGTYPE:
1188*3d8817e4Smiod if (is_procreg || (instruction->flags & USER_REG))
1189*3d8817e4Smiod return reg->image;
1190*3d8817e4Smiod else
1191*3d8817e4Smiod IMAGE_ERR;
1192*3d8817e4Smiod
1193*3d8817e4Smiod case CRX_CFG_REGTYPE:
1194*3d8817e4Smiod if (is_procreg)
1195*3d8817e4Smiod return reg->image;
1196*3d8817e4Smiod else
1197*3d8817e4Smiod IMAGE_ERR;
1198*3d8817e4Smiod
1199*3d8817e4Smiod case CRX_R_REGTYPE:
1200*3d8817e4Smiod if (! is_procreg)
1201*3d8817e4Smiod return reg->image;
1202*3d8817e4Smiod else
1203*3d8817e4Smiod IMAGE_ERR;
1204*3d8817e4Smiod
1205*3d8817e4Smiod case CRX_C_REGTYPE:
1206*3d8817e4Smiod case CRX_CS_REGTYPE:
1207*3d8817e4Smiod return reg->image;
1208*3d8817e4Smiod break;
1209*3d8817e4Smiod
1210*3d8817e4Smiod default:
1211*3d8817e4Smiod IMAGE_ERR;
1212*3d8817e4Smiod }
1213*3d8817e4Smiod
1214*3d8817e4Smiod return 0;
1215*3d8817e4Smiod }
1216*3d8817e4Smiod
1217*3d8817e4Smiod /* Routine used to represent integer X using NBITS bits. */
1218*3d8817e4Smiod
1219*3d8817e4Smiod static long
getconstant(long x,int nbits)1220*3d8817e4Smiod getconstant (long x, int nbits)
1221*3d8817e4Smiod {
1222*3d8817e4Smiod /* The following expression avoids overflow if
1223*3d8817e4Smiod 'nbits' is the number of bits in 'bfd_vma'. */
1224*3d8817e4Smiod return (x & ((((1 << (nbits - 1)) - 1) << 1) | 1));
1225*3d8817e4Smiod }
1226*3d8817e4Smiod
1227*3d8817e4Smiod /* Print a constant value to 'output_opcode':
1228*3d8817e4Smiod ARG holds the operand's type and value.
1229*3d8817e4Smiod SHIFT represents the location of the operand to be print into.
1230*3d8817e4Smiod NBITS determines the size (in bits) of the constant. */
1231*3d8817e4Smiod
1232*3d8817e4Smiod static void
print_constant(int nbits,int shift,argument * arg)1233*3d8817e4Smiod print_constant (int nbits, int shift, argument *arg)
1234*3d8817e4Smiod {
1235*3d8817e4Smiod unsigned long mask = 0;
1236*3d8817e4Smiod
1237*3d8817e4Smiod long constant = getconstant (arg->constant, nbits);
1238*3d8817e4Smiod
1239*3d8817e4Smiod switch (nbits)
1240*3d8817e4Smiod {
1241*3d8817e4Smiod case 32:
1242*3d8817e4Smiod case 28:
1243*3d8817e4Smiod case 24:
1244*3d8817e4Smiod case 22:
1245*3d8817e4Smiod /* mask the upper part of the constant, that is, the bits
1246*3d8817e4Smiod going to the lowest byte of output_opcode[0].
1247*3d8817e4Smiod The upper part of output_opcode[1] is always filled,
1248*3d8817e4Smiod therefore it is always masked with 0xFFFF. */
1249*3d8817e4Smiod mask = (1 << (nbits - 16)) - 1;
1250*3d8817e4Smiod /* Divide the constant between two consecutive words :
1251*3d8817e4Smiod 0 1 2 3
1252*3d8817e4Smiod +---------+---------+---------+---------+
1253*3d8817e4Smiod | | X X X X | X X X X | |
1254*3d8817e4Smiod +---------+---------+---------+---------+
1255*3d8817e4Smiod output_opcode[0] output_opcode[1] */
1256*3d8817e4Smiod
1257*3d8817e4Smiod CRX_PRINT (0, (constant >> WORD_SHIFT) & mask, 0);
1258*3d8817e4Smiod CRX_PRINT (1, (constant & 0xFFFF), WORD_SHIFT);
1259*3d8817e4Smiod break;
1260*3d8817e4Smiod
1261*3d8817e4Smiod case 16:
1262*3d8817e4Smiod case 12:
1263*3d8817e4Smiod /* Special case - in arg_cr, the SHIFT represents the location
1264*3d8817e4Smiod of the REGISTER, not the constant, which is itself not shifted. */
1265*3d8817e4Smiod if (arg->type == arg_cr)
1266*3d8817e4Smiod {
1267*3d8817e4Smiod CRX_PRINT (0, constant, 0);
1268*3d8817e4Smiod break;
1269*3d8817e4Smiod }
1270*3d8817e4Smiod
1271*3d8817e4Smiod /* When instruction size is 3 and 'shift' is 16, a 16-bit constant is
1272*3d8817e4Smiod always filling the upper part of output_opcode[1]. If we mistakenly
1273*3d8817e4Smiod write it to output_opcode[0], the constant prefix (that is, 'match')
1274*3d8817e4Smiod will be overriden.
1275*3d8817e4Smiod 0 1 2 3
1276*3d8817e4Smiod +---------+---------+---------+---------+
1277*3d8817e4Smiod | 'match' | | X X X X | |
1278*3d8817e4Smiod +---------+---------+---------+---------+
1279*3d8817e4Smiod output_opcode[0] output_opcode[1] */
1280*3d8817e4Smiod
1281*3d8817e4Smiod if ((instruction->size > 2) && (shift == WORD_SHIFT))
1282*3d8817e4Smiod CRX_PRINT (1, constant, WORD_SHIFT);
1283*3d8817e4Smiod else
1284*3d8817e4Smiod CRX_PRINT (0, constant, shift);
1285*3d8817e4Smiod break;
1286*3d8817e4Smiod
1287*3d8817e4Smiod default:
1288*3d8817e4Smiod CRX_PRINT (0, constant, shift);
1289*3d8817e4Smiod break;
1290*3d8817e4Smiod }
1291*3d8817e4Smiod }
1292*3d8817e4Smiod
1293*3d8817e4Smiod /* Print an operand to 'output_opcode', which later on will be
1294*3d8817e4Smiod printed to the object file:
1295*3d8817e4Smiod ARG holds the operand's type, size and value.
1296*3d8817e4Smiod SHIFT represents the printing location of operand.
1297*3d8817e4Smiod NBITS determines the size (in bits) of a constant operand. */
1298*3d8817e4Smiod
1299*3d8817e4Smiod static void
print_operand(int nbits,int shift,argument * arg)1300*3d8817e4Smiod print_operand (int nbits, int shift, argument *arg)
1301*3d8817e4Smiod {
1302*3d8817e4Smiod switch (arg->type)
1303*3d8817e4Smiod {
1304*3d8817e4Smiod case arg_r:
1305*3d8817e4Smiod CRX_PRINT (0, getreg_image (arg->r), shift);
1306*3d8817e4Smiod break;
1307*3d8817e4Smiod
1308*3d8817e4Smiod case arg_copr:
1309*3d8817e4Smiod if (arg->cr < c0 || arg->cr > c15)
1310*3d8817e4Smiod as_bad (_("Illegal Co-processor register in Instruction `%s' "),
1311*3d8817e4Smiod ins_parse);
1312*3d8817e4Smiod CRX_PRINT (0, getreg_image (arg->cr), shift);
1313*3d8817e4Smiod break;
1314*3d8817e4Smiod
1315*3d8817e4Smiod case arg_copsr:
1316*3d8817e4Smiod if (arg->cr < cs0 || arg->cr > cs15)
1317*3d8817e4Smiod as_bad (_("Illegal Co-processor special register in Instruction `%s' "),
1318*3d8817e4Smiod ins_parse);
1319*3d8817e4Smiod CRX_PRINT (0, getreg_image (arg->cr), shift);
1320*3d8817e4Smiod break;
1321*3d8817e4Smiod
1322*3d8817e4Smiod case arg_idxr:
1323*3d8817e4Smiod /* 16 12 8 6 0
1324*3d8817e4Smiod +--------------------------------+
1325*3d8817e4Smiod | r_base | r_idx | scl| disp |
1326*3d8817e4Smiod +--------------------------------+ */
1327*3d8817e4Smiod CRX_PRINT (0, getreg_image (arg->r), 12);
1328*3d8817e4Smiod CRX_PRINT (0, getreg_image (arg->i_r), 8);
1329*3d8817e4Smiod CRX_PRINT (0, arg->scale, 6);
1330*3d8817e4Smiod case arg_ic:
1331*3d8817e4Smiod case arg_c:
1332*3d8817e4Smiod print_constant (nbits, shift, arg);
1333*3d8817e4Smiod break;
1334*3d8817e4Smiod
1335*3d8817e4Smiod case arg_rbase:
1336*3d8817e4Smiod CRX_PRINT (0, getreg_image (arg->r), shift);
1337*3d8817e4Smiod break;
1338*3d8817e4Smiod
1339*3d8817e4Smiod case arg_cr:
1340*3d8817e4Smiod /* case base_cst4. */
1341*3d8817e4Smiod if (instruction->flags & DISPU4MAP)
1342*3d8817e4Smiod print_constant (nbits, shift + REG_SIZE, arg);
1343*3d8817e4Smiod else
1344*3d8817e4Smiod /* rbase_disps<NN> and other such cases. */
1345*3d8817e4Smiod print_constant (nbits, shift, arg);
1346*3d8817e4Smiod /* Add the register argument to the output_opcode. */
1347*3d8817e4Smiod CRX_PRINT (0, getreg_image (arg->r), shift);
1348*3d8817e4Smiod break;
1349*3d8817e4Smiod
1350*3d8817e4Smiod default:
1351*3d8817e4Smiod break;
1352*3d8817e4Smiod }
1353*3d8817e4Smiod }
1354*3d8817e4Smiod
1355*3d8817e4Smiod /* Retrieve the number of operands for the current assembled instruction. */
1356*3d8817e4Smiod
1357*3d8817e4Smiod static int
get_number_of_operands(void)1358*3d8817e4Smiod get_number_of_operands (void)
1359*3d8817e4Smiod {
1360*3d8817e4Smiod int i;
1361*3d8817e4Smiod
1362*3d8817e4Smiod for (i = 0; instruction->operands[i].op_type && i < MAX_OPERANDS; i++)
1363*3d8817e4Smiod ;
1364*3d8817e4Smiod return i;
1365*3d8817e4Smiod }
1366*3d8817e4Smiod
1367*3d8817e4Smiod /* Verify that the number NUM can be represented in BITS bits (that is,
1368*3d8817e4Smiod within its permitted range), based on the instruction's FLAGS.
1369*3d8817e4Smiod If UPDATE is nonzero, update the value of NUM if necessary.
1370*3d8817e4Smiod Return OP_LEGAL upon success, actual error type upon failure. */
1371*3d8817e4Smiod
1372*3d8817e4Smiod static op_err
check_range(long * num,int bits,int unsigned flags,int update)1373*3d8817e4Smiod check_range (long *num, int bits, int unsigned flags, int update)
1374*3d8817e4Smiod {
1375*3d8817e4Smiod long min, max;
1376*3d8817e4Smiod int retval = OP_LEGAL;
1377*3d8817e4Smiod int bin;
1378*3d8817e4Smiod long upper_64kb = 0xFFFF0000;
1379*3d8817e4Smiod long value = *num;
1380*3d8817e4Smiod
1381*3d8817e4Smiod /* For hosts witah longs bigger than 32-bits make sure that the top
1382*3d8817e4Smiod bits of a 32-bit negative value read in by the parser are set,
1383*3d8817e4Smiod so that the correct comparisons are made. */
1384*3d8817e4Smiod if (value & 0x80000000)
1385*3d8817e4Smiod value |= (-1L << 31);
1386*3d8817e4Smiod
1387*3d8817e4Smiod /* Verify operand value is even. */
1388*3d8817e4Smiod if (flags & OP_EVEN)
1389*3d8817e4Smiod {
1390*3d8817e4Smiod if (value % 2)
1391*3d8817e4Smiod return OP_NOT_EVEN;
1392*3d8817e4Smiod }
1393*3d8817e4Smiod
1394*3d8817e4Smiod if (flags & OP_UPPER_64KB)
1395*3d8817e4Smiod {
1396*3d8817e4Smiod /* Check if value is to be mapped to upper 64 KB memory area. */
1397*3d8817e4Smiod if ((value & upper_64kb) == upper_64kb)
1398*3d8817e4Smiod {
1399*3d8817e4Smiod value -= upper_64kb;
1400*3d8817e4Smiod if (update)
1401*3d8817e4Smiod *num = value;
1402*3d8817e4Smiod }
1403*3d8817e4Smiod else
1404*3d8817e4Smiod return OP_NOT_UPPER_64KB;
1405*3d8817e4Smiod }
1406*3d8817e4Smiod
1407*3d8817e4Smiod if (flags & OP_SHIFT)
1408*3d8817e4Smiod {
1409*3d8817e4Smiod value >>= 1;
1410*3d8817e4Smiod if (update)
1411*3d8817e4Smiod *num = value;
1412*3d8817e4Smiod }
1413*3d8817e4Smiod else if (flags & OP_SHIFT_DEC)
1414*3d8817e4Smiod {
1415*3d8817e4Smiod value = (value >> 1) - 1;
1416*3d8817e4Smiod if (update)
1417*3d8817e4Smiod *num = value;
1418*3d8817e4Smiod }
1419*3d8817e4Smiod
1420*3d8817e4Smiod if (flags & OP_ESC)
1421*3d8817e4Smiod {
1422*3d8817e4Smiod /* 0x7e and 0x7f are reserved escape sequences of dispe9. */
1423*3d8817e4Smiod if (value == 0x7e || value == 0x7f)
1424*3d8817e4Smiod return OP_OUT_OF_RANGE;
1425*3d8817e4Smiod }
1426*3d8817e4Smiod
1427*3d8817e4Smiod if (flags & OP_DISPU4)
1428*3d8817e4Smiod {
1429*3d8817e4Smiod int is_dispu4 = 0;
1430*3d8817e4Smiod
1431*3d8817e4Smiod int mul = (instruction->flags & DISPUB4) ? 1
1432*3d8817e4Smiod : (instruction->flags & DISPUW4) ? 2
1433*3d8817e4Smiod : (instruction->flags & DISPUD4) ? 4 : 0;
1434*3d8817e4Smiod
1435*3d8817e4Smiod for (bin = 0; bin < cst4_maps; bin++)
1436*3d8817e4Smiod {
1437*3d8817e4Smiod if (value == (mul * bin))
1438*3d8817e4Smiod {
1439*3d8817e4Smiod is_dispu4 = 1;
1440*3d8817e4Smiod if (update)
1441*3d8817e4Smiod *num = bin;
1442*3d8817e4Smiod break;
1443*3d8817e4Smiod }
1444*3d8817e4Smiod }
1445*3d8817e4Smiod if (!is_dispu4)
1446*3d8817e4Smiod retval = OP_ILLEGAL_DISPU4;
1447*3d8817e4Smiod }
1448*3d8817e4Smiod else if (flags & OP_CST4)
1449*3d8817e4Smiod {
1450*3d8817e4Smiod int is_cst4 = 0;
1451*3d8817e4Smiod
1452*3d8817e4Smiod for (bin = 0; bin < cst4_maps; bin++)
1453*3d8817e4Smiod {
1454*3d8817e4Smiod if (value == cst4_map[bin])
1455*3d8817e4Smiod {
1456*3d8817e4Smiod is_cst4 = 1;
1457*3d8817e4Smiod if (update)
1458*3d8817e4Smiod *num = bin;
1459*3d8817e4Smiod break;
1460*3d8817e4Smiod }
1461*3d8817e4Smiod }
1462*3d8817e4Smiod if (!is_cst4)
1463*3d8817e4Smiod retval = OP_ILLEGAL_CST4;
1464*3d8817e4Smiod }
1465*3d8817e4Smiod else if (flags & OP_SIGNED)
1466*3d8817e4Smiod {
1467*3d8817e4Smiod max = (1 << (bits - 1)) - 1;
1468*3d8817e4Smiod min = - (1 << (bits - 1));
1469*3d8817e4Smiod if ((value > max) || (value < min))
1470*3d8817e4Smiod retval = OP_OUT_OF_RANGE;
1471*3d8817e4Smiod }
1472*3d8817e4Smiod else if (flags & OP_UNSIGNED)
1473*3d8817e4Smiod {
1474*3d8817e4Smiod max = ((((1 << (bits - 1)) - 1) << 1) | 1);
1475*3d8817e4Smiod min = 0;
1476*3d8817e4Smiod if (((unsigned long) value > (unsigned long) max)
1477*3d8817e4Smiod || ((unsigned long) value < (unsigned long) min))
1478*3d8817e4Smiod retval = OP_OUT_OF_RANGE;
1479*3d8817e4Smiod }
1480*3d8817e4Smiod return retval;
1481*3d8817e4Smiod }
1482*3d8817e4Smiod
1483*3d8817e4Smiod /* Assemble a single instruction:
1484*3d8817e4Smiod INSN is already parsed (that is, all operand values and types are set).
1485*3d8817e4Smiod For instruction to be assembled, we need to find an appropriate template in
1486*3d8817e4Smiod the instruction table, meeting the following conditions:
1487*3d8817e4Smiod 1: Has the same number of operands.
1488*3d8817e4Smiod 2: Has the same operand types.
1489*3d8817e4Smiod 3: Each operand size is sufficient to represent the instruction's values.
1490*3d8817e4Smiod Returns 1 upon success, 0 upon failure. */
1491*3d8817e4Smiod
1492*3d8817e4Smiod static int
assemble_insn(char * mnemonic,ins * insn)1493*3d8817e4Smiod assemble_insn (char *mnemonic, ins *insn)
1494*3d8817e4Smiod {
1495*3d8817e4Smiod /* Type of each operand in the current template. */
1496*3d8817e4Smiod argtype cur_type[MAX_OPERANDS];
1497*3d8817e4Smiod /* Size (in bits) of each operand in the current template. */
1498*3d8817e4Smiod unsigned int cur_size[MAX_OPERANDS];
1499*3d8817e4Smiod /* Flags of each operand in the current template. */
1500*3d8817e4Smiod unsigned int cur_flags[MAX_OPERANDS];
1501*3d8817e4Smiod /* Instruction type to match. */
1502*3d8817e4Smiod unsigned int ins_type;
1503*3d8817e4Smiod /* Boolean flag to mark whether a match was found. */
1504*3d8817e4Smiod int match = 0;
1505*3d8817e4Smiod int i;
1506*3d8817e4Smiod /* Nonzero if an instruction with same number of operands was found. */
1507*3d8817e4Smiod int found_same_number_of_operands = 0;
1508*3d8817e4Smiod /* Nonzero if an instruction with same argument types was found. */
1509*3d8817e4Smiod int found_same_argument_types = 0;
1510*3d8817e4Smiod /* Nonzero if a constant was found within the required range. */
1511*3d8817e4Smiod int found_const_within_range = 0;
1512*3d8817e4Smiod /* Argument number of an operand with invalid type. */
1513*3d8817e4Smiod int invalid_optype = -1;
1514*3d8817e4Smiod /* Argument number of an operand with invalid constant value. */
1515*3d8817e4Smiod int invalid_const = -1;
1516*3d8817e4Smiod /* Operand error (used for issuing various constant error messages). */
1517*3d8817e4Smiod op_err op_error, const_err = OP_LEGAL;
1518*3d8817e4Smiod
1519*3d8817e4Smiod /* Retrieve data (based on FUNC) for each operand of a given instruction. */
1520*3d8817e4Smiod #define GET_CURRENT_DATA(FUNC, ARRAY) \
1521*3d8817e4Smiod for (i = 0; i < insn->nargs; i++) \
1522*3d8817e4Smiod ARRAY[i] = FUNC (instruction->operands[i].op_type)
1523*3d8817e4Smiod
1524*3d8817e4Smiod #define GET_CURRENT_TYPE GET_CURRENT_DATA(get_optype, cur_type)
1525*3d8817e4Smiod #define GET_CURRENT_SIZE GET_CURRENT_DATA(get_opbits, cur_size)
1526*3d8817e4Smiod #define GET_CURRENT_FLAGS GET_CURRENT_DATA(get_opflags, cur_flags)
1527*3d8817e4Smiod
1528*3d8817e4Smiod /* Instruction has no operands -> only copy the constant opcode. */
1529*3d8817e4Smiod if (insn->nargs == 0)
1530*3d8817e4Smiod {
1531*3d8817e4Smiod output_opcode[0] = BIN (instruction->match, instruction->match_bits);
1532*3d8817e4Smiod return 1;
1533*3d8817e4Smiod }
1534*3d8817e4Smiod
1535*3d8817e4Smiod /* In some case, same mnemonic can appear with different instruction types.
1536*3d8817e4Smiod For example, 'storb' is supported with 3 different types :
1537*3d8817e4Smiod LD_STOR_INS, LD_STOR_INS_INC, STOR_IMM_INS.
1538*3d8817e4Smiod We assume that when reaching this point, the instruction type was
1539*3d8817e4Smiod pre-determined. We need to make sure that the type stays the same
1540*3d8817e4Smiod during a search for matching instruction. */
1541*3d8817e4Smiod ins_type = CRX_INS_TYPE(instruction->flags);
1542*3d8817e4Smiod
1543*3d8817e4Smiod while (/* Check that match is still not found. */
1544*3d8817e4Smiod match != 1
1545*3d8817e4Smiod /* Check we didn't get to end of table. */
1546*3d8817e4Smiod && instruction->mnemonic != NULL
1547*3d8817e4Smiod /* Check that the actual mnemonic is still available. */
1548*3d8817e4Smiod && IS_INSN_MNEMONIC (mnemonic)
1549*3d8817e4Smiod /* Check that the instruction type wasn't changed. */
1550*3d8817e4Smiod && IS_INSN_TYPE(ins_type))
1551*3d8817e4Smiod {
1552*3d8817e4Smiod /* Check whether number of arguments is legal. */
1553*3d8817e4Smiod if (get_number_of_operands () != insn->nargs)
1554*3d8817e4Smiod goto next_insn;
1555*3d8817e4Smiod found_same_number_of_operands = 1;
1556*3d8817e4Smiod
1557*3d8817e4Smiod /* Initialize arrays with data of each operand in current template. */
1558*3d8817e4Smiod GET_CURRENT_TYPE;
1559*3d8817e4Smiod GET_CURRENT_SIZE;
1560*3d8817e4Smiod GET_CURRENT_FLAGS;
1561*3d8817e4Smiod
1562*3d8817e4Smiod /* Check for type compatibility. */
1563*3d8817e4Smiod for (i = 0; i < insn->nargs; i++)
1564*3d8817e4Smiod {
1565*3d8817e4Smiod if (cur_type[i] != insn->arg[i].type)
1566*3d8817e4Smiod {
1567*3d8817e4Smiod if (invalid_optype == -1)
1568*3d8817e4Smiod invalid_optype = i + 1;
1569*3d8817e4Smiod goto next_insn;
1570*3d8817e4Smiod }
1571*3d8817e4Smiod }
1572*3d8817e4Smiod found_same_argument_types = 1;
1573*3d8817e4Smiod
1574*3d8817e4Smiod for (i = 0; i < insn->nargs; i++)
1575*3d8817e4Smiod {
1576*3d8817e4Smiod /* Reverse the operand indices for certain opcodes:
1577*3d8817e4Smiod Index 0 -->> 1
1578*3d8817e4Smiod Index 1 -->> 0
1579*3d8817e4Smiod Other index -->> stays the same. */
1580*3d8817e4Smiod int j = instruction->flags & REVERSE_MATCH ?
1581*3d8817e4Smiod i == 0 ? 1 :
1582*3d8817e4Smiod i == 1 ? 0 : i :
1583*3d8817e4Smiod i;
1584*3d8817e4Smiod
1585*3d8817e4Smiod /* Only check range - don't update the constant's value, since the
1586*3d8817e4Smiod current instruction may not be the last we try to match.
1587*3d8817e4Smiod The constant's value will be updated later, right before printing
1588*3d8817e4Smiod it to the object file. */
1589*3d8817e4Smiod if ((insn->arg[j].X_op == O_constant)
1590*3d8817e4Smiod && (op_error = check_range (&insn->arg[j].constant, cur_size[j],
1591*3d8817e4Smiod cur_flags[j], 0)))
1592*3d8817e4Smiod {
1593*3d8817e4Smiod if (invalid_const == -1)
1594*3d8817e4Smiod {
1595*3d8817e4Smiod invalid_const = j + 1;
1596*3d8817e4Smiod const_err = op_error;
1597*3d8817e4Smiod }
1598*3d8817e4Smiod goto next_insn;
1599*3d8817e4Smiod }
1600*3d8817e4Smiod /* For symbols, we make sure the relocation size (which was already
1601*3d8817e4Smiod determined) is sufficient. */
1602*3d8817e4Smiod else if ((insn->arg[j].X_op == O_symbol)
1603*3d8817e4Smiod && ((bfd_reloc_type_lookup (stdoutput, insn->rtype))->bitsize
1604*3d8817e4Smiod > cur_size[j]))
1605*3d8817e4Smiod goto next_insn;
1606*3d8817e4Smiod }
1607*3d8817e4Smiod found_const_within_range = 1;
1608*3d8817e4Smiod
1609*3d8817e4Smiod /* If we got till here -> Full match is found. */
1610*3d8817e4Smiod match = 1;
1611*3d8817e4Smiod break;
1612*3d8817e4Smiod
1613*3d8817e4Smiod /* Try again with next instruction. */
1614*3d8817e4Smiod next_insn:
1615*3d8817e4Smiod instruction++;
1616*3d8817e4Smiod }
1617*3d8817e4Smiod
1618*3d8817e4Smiod if (!match)
1619*3d8817e4Smiod {
1620*3d8817e4Smiod /* We haven't found a match - instruction can't be assembled. */
1621*3d8817e4Smiod if (!found_same_number_of_operands)
1622*3d8817e4Smiod as_bad (_("Incorrect number of operands"));
1623*3d8817e4Smiod else if (!found_same_argument_types)
1624*3d8817e4Smiod as_bad (_("Illegal type of operand (arg %d)"), invalid_optype);
1625*3d8817e4Smiod else if (!found_const_within_range)
1626*3d8817e4Smiod {
1627*3d8817e4Smiod switch (const_err)
1628*3d8817e4Smiod {
1629*3d8817e4Smiod case OP_OUT_OF_RANGE:
1630*3d8817e4Smiod as_bad (_("Operand out of range (arg %d)"), invalid_const);
1631*3d8817e4Smiod break;
1632*3d8817e4Smiod case OP_NOT_EVEN:
1633*3d8817e4Smiod as_bad (_("Operand has odd displacement (arg %d)"), invalid_const);
1634*3d8817e4Smiod break;
1635*3d8817e4Smiod case OP_ILLEGAL_DISPU4:
1636*3d8817e4Smiod as_bad (_("Invalid DISPU4 operand value (arg %d)"), invalid_const);
1637*3d8817e4Smiod break;
1638*3d8817e4Smiod case OP_ILLEGAL_CST4:
1639*3d8817e4Smiod as_bad (_("Invalid CST4 operand value (arg %d)"), invalid_const);
1640*3d8817e4Smiod break;
1641*3d8817e4Smiod case OP_NOT_UPPER_64KB:
1642*3d8817e4Smiod as_bad (_("Operand value is not within upper 64 KB (arg %d)"),
1643*3d8817e4Smiod invalid_const);
1644*3d8817e4Smiod break;
1645*3d8817e4Smiod default:
1646*3d8817e4Smiod as_bad (_("Illegal operand (arg %d)"), invalid_const);
1647*3d8817e4Smiod break;
1648*3d8817e4Smiod }
1649*3d8817e4Smiod }
1650*3d8817e4Smiod
1651*3d8817e4Smiod return 0;
1652*3d8817e4Smiod }
1653*3d8817e4Smiod else
1654*3d8817e4Smiod /* Full match - print the encoding to output file. */
1655*3d8817e4Smiod {
1656*3d8817e4Smiod /* Make further checkings (such that couldn't be made earlier).
1657*3d8817e4Smiod Warn the user if necessary. */
1658*3d8817e4Smiod warn_if_needed (insn);
1659*3d8817e4Smiod
1660*3d8817e4Smiod /* Check whether we need to adjust the instruction pointer. */
1661*3d8817e4Smiod if (adjust_if_needed (insn))
1662*3d8817e4Smiod /* If instruction pointer was adjusted, we need to update
1663*3d8817e4Smiod the size of the current template operands. */
1664*3d8817e4Smiod GET_CURRENT_SIZE;
1665*3d8817e4Smiod
1666*3d8817e4Smiod for (i = 0; i < insn->nargs; i++)
1667*3d8817e4Smiod {
1668*3d8817e4Smiod int j = instruction->flags & REVERSE_MATCH ?
1669*3d8817e4Smiod i == 0 ? 1 :
1670*3d8817e4Smiod i == 1 ? 0 : i :
1671*3d8817e4Smiod i;
1672*3d8817e4Smiod
1673*3d8817e4Smiod /* This time, update constant value before printing it. */
1674*3d8817e4Smiod if ((insn->arg[j].X_op == O_constant)
1675*3d8817e4Smiod && (check_range (&insn->arg[j].constant, cur_size[j],
1676*3d8817e4Smiod cur_flags[j], 1) != OP_LEGAL))
1677*3d8817e4Smiod as_fatal (_("Illegal operand (arg %d)"), j+1);
1678*3d8817e4Smiod }
1679*3d8817e4Smiod
1680*3d8817e4Smiod /* First, copy the instruction's opcode. */
1681*3d8817e4Smiod output_opcode[0] = BIN (instruction->match, instruction->match_bits);
1682*3d8817e4Smiod
1683*3d8817e4Smiod for (i = 0; i < insn->nargs; i++)
1684*3d8817e4Smiod {
1685*3d8817e4Smiod cur_arg_num = i;
1686*3d8817e4Smiod print_operand (cur_size[i], instruction->operands[i].shift,
1687*3d8817e4Smiod &insn->arg[i]);
1688*3d8817e4Smiod }
1689*3d8817e4Smiod }
1690*3d8817e4Smiod
1691*3d8817e4Smiod return 1;
1692*3d8817e4Smiod }
1693*3d8817e4Smiod
1694*3d8817e4Smiod /* Bunch of error checkings.
1695*3d8817e4Smiod The checks are made after a matching instruction was found. */
1696*3d8817e4Smiod
1697*3d8817e4Smiod void
warn_if_needed(ins * insn)1698*3d8817e4Smiod warn_if_needed (ins *insn)
1699*3d8817e4Smiod {
1700*3d8817e4Smiod /* If the post-increment address mode is used and the load/store
1701*3d8817e4Smiod source register is the same as rbase, the result of the
1702*3d8817e4Smiod instruction is undefined. */
1703*3d8817e4Smiod if (IS_INSN_TYPE (LD_STOR_INS_INC))
1704*3d8817e4Smiod {
1705*3d8817e4Smiod /* Enough to verify that one of the arguments is a simple reg. */
1706*3d8817e4Smiod if ((insn->arg[0].type == arg_r) || (insn->arg[1].type == arg_r))
1707*3d8817e4Smiod if (insn->arg[0].r == insn->arg[1].r)
1708*3d8817e4Smiod as_bad (_("Same src/dest register is used (`r%d'), result is undefined"),
1709*3d8817e4Smiod insn->arg[0].r);
1710*3d8817e4Smiod }
1711*3d8817e4Smiod
1712*3d8817e4Smiod /* Some instruction assume the stack pointer as rptr operand.
1713*3d8817e4Smiod Issue an error when the register to be loaded is also SP. */
1714*3d8817e4Smiod if (instruction->flags & NO_SP)
1715*3d8817e4Smiod {
1716*3d8817e4Smiod if (getreg_image (insn->arg[0].r) == getreg_image (sp))
1717*3d8817e4Smiod as_bad (_("`%s' has undefined result"), ins_parse);
1718*3d8817e4Smiod }
1719*3d8817e4Smiod
1720*3d8817e4Smiod /* If the rptr register is specified as one of the registers to be loaded,
1721*3d8817e4Smiod the final contents of rptr are undefined. Thus, we issue an error. */
1722*3d8817e4Smiod if (instruction->flags & NO_RPTR)
1723*3d8817e4Smiod {
1724*3d8817e4Smiod if ((1 << getreg_image (insn->arg[0].r)) & insn->arg[1].constant)
1725*3d8817e4Smiod as_bad (_("Same src/dest register is used (`r%d'), result is undefined"),
1726*3d8817e4Smiod getreg_image (insn->arg[0].r));
1727*3d8817e4Smiod }
1728*3d8817e4Smiod }
1729*3d8817e4Smiod
1730*3d8817e4Smiod /* In some cases, we need to adjust the instruction pointer although a
1731*3d8817e4Smiod match was already found. Here, we gather all these cases.
1732*3d8817e4Smiod Returns 1 if instruction pointer was adjusted, otherwise 0. */
1733*3d8817e4Smiod
1734*3d8817e4Smiod int
adjust_if_needed(ins * insn)1735*3d8817e4Smiod adjust_if_needed (ins *insn)
1736*3d8817e4Smiod {
1737*3d8817e4Smiod int ret_value = 0;
1738*3d8817e4Smiod
1739*3d8817e4Smiod /* Special check for 'addub $0, r0' instruction -
1740*3d8817e4Smiod The opcode '0000 0000 0000 0000' is not allowed. */
1741*3d8817e4Smiod if (IS_INSN_MNEMONIC ("addub"))
1742*3d8817e4Smiod {
1743*3d8817e4Smiod if ((instruction->operands[0].op_type == cst4)
1744*3d8817e4Smiod && instruction->operands[1].op_type == regr)
1745*3d8817e4Smiod {
1746*3d8817e4Smiod if (insn->arg[0].constant == 0 && insn->arg[1].r == r0)
1747*3d8817e4Smiod {
1748*3d8817e4Smiod instruction++;
1749*3d8817e4Smiod ret_value = 1;
1750*3d8817e4Smiod }
1751*3d8817e4Smiod }
1752*3d8817e4Smiod }
1753*3d8817e4Smiod
1754*3d8817e4Smiod /* Optimization: Omit a zero displacement in bit operations,
1755*3d8817e4Smiod saving 2-byte encoding space (e.g., 'cbitw $8, 0(r1)'). */
1756*3d8817e4Smiod if (IS_INSN_TYPE (CSTBIT_INS))
1757*3d8817e4Smiod {
1758*3d8817e4Smiod if ((instruction->operands[1].op_type == rbase_disps12)
1759*3d8817e4Smiod && (insn->arg[1].X_op == O_constant)
1760*3d8817e4Smiod && (insn->arg[1].constant == 0))
1761*3d8817e4Smiod {
1762*3d8817e4Smiod instruction--;
1763*3d8817e4Smiod ret_value = 1;
1764*3d8817e4Smiod }
1765*3d8817e4Smiod }
1766*3d8817e4Smiod
1767*3d8817e4Smiod return ret_value;
1768*3d8817e4Smiod }
1769*3d8817e4Smiod
1770*3d8817e4Smiod /* Set the appropriate bit for register 'r' in 'mask'.
1771*3d8817e4Smiod This indicates that this register is loaded or stored by
1772*3d8817e4Smiod the instruction. */
1773*3d8817e4Smiod
1774*3d8817e4Smiod static void
mask_reg(int r,unsigned short int * mask)1775*3d8817e4Smiod mask_reg (int r, unsigned short int *mask)
1776*3d8817e4Smiod {
1777*3d8817e4Smiod if ((reg)r > (reg)sp)
1778*3d8817e4Smiod {
1779*3d8817e4Smiod as_bad (_("Invalid Register in Register List"));
1780*3d8817e4Smiod return;
1781*3d8817e4Smiod }
1782*3d8817e4Smiod
1783*3d8817e4Smiod *mask |= (1 << r);
1784*3d8817e4Smiod }
1785*3d8817e4Smiod
1786*3d8817e4Smiod /* Preprocess register list - create a 16-bit mask with one bit for each
1787*3d8817e4Smiod of the 16 general purpose registers. If a bit is set, it indicates
1788*3d8817e4Smiod that this register is loaded or stored by the instruction. */
1789*3d8817e4Smiod
1790*3d8817e4Smiod static char *
preprocess_reglist(char * param,int * allocated)1791*3d8817e4Smiod preprocess_reglist (char *param, int *allocated)
1792*3d8817e4Smiod {
1793*3d8817e4Smiod char reg_name[MAX_REGNAME_LEN]; /* Current parsed register name. */
1794*3d8817e4Smiod char *regP; /* Pointer to 'reg_name' string. */
1795*3d8817e4Smiod int reg_counter = 0; /* Count number of parsed registers. */
1796*3d8817e4Smiod unsigned short int mask = 0; /* Mask for 16 general purpose registers. */
1797*3d8817e4Smiod char *new_param; /* New created operands string. */
1798*3d8817e4Smiod char *paramP = param; /* Pointer to original opearands string. */
1799*3d8817e4Smiod char maskstring[10]; /* Array to print the mask as a string. */
1800*3d8817e4Smiod int hi_found = 0, lo_found = 0; /* Boolean flags for hi/lo registers. */
1801*3d8817e4Smiod reg r;
1802*3d8817e4Smiod copreg cr;
1803*3d8817e4Smiod
1804*3d8817e4Smiod /* If 'param' is already in form of a number, no need to preprocess. */
1805*3d8817e4Smiod if (strchr (paramP, '{') == NULL)
1806*3d8817e4Smiod return param;
1807*3d8817e4Smiod
1808*3d8817e4Smiod /* Verifying correct syntax of operand. */
1809*3d8817e4Smiod if (strchr (paramP, '}') == NULL)
1810*3d8817e4Smiod as_fatal (_("Missing matching brackets : `%s'"), ins_parse);
1811*3d8817e4Smiod
1812*3d8817e4Smiod while (*paramP++ != '{');
1813*3d8817e4Smiod
1814*3d8817e4Smiod new_param = (char *)xcalloc (MAX_INST_LEN, sizeof (char));
1815*3d8817e4Smiod *allocated = 1;
1816*3d8817e4Smiod strncpy (new_param, param, paramP - param - 1);
1817*3d8817e4Smiod
1818*3d8817e4Smiod while (*paramP != '}')
1819*3d8817e4Smiod {
1820*3d8817e4Smiod regP = paramP;
1821*3d8817e4Smiod memset (®_name, '\0', sizeof (reg_name));
1822*3d8817e4Smiod
1823*3d8817e4Smiod while (ISALNUM (*paramP))
1824*3d8817e4Smiod paramP++;
1825*3d8817e4Smiod
1826*3d8817e4Smiod strncpy (reg_name, regP, paramP - regP);
1827*3d8817e4Smiod
1828*3d8817e4Smiod /* Coprocessor register c<N>. */
1829*3d8817e4Smiod if (IS_INSN_TYPE (COP_REG_INS))
1830*3d8817e4Smiod {
1831*3d8817e4Smiod if (((cr = get_copregister (reg_name)) == nullcopregister)
1832*3d8817e4Smiod || (crx_copregtab[cr-MAX_REG].type != CRX_C_REGTYPE))
1833*3d8817e4Smiod as_fatal (_("Illegal register `%s' in cop-register list"), reg_name);
1834*3d8817e4Smiod mask_reg (getreg_image (cr - c0), &mask);
1835*3d8817e4Smiod }
1836*3d8817e4Smiod /* Coprocessor Special register cs<N>. */
1837*3d8817e4Smiod else if (IS_INSN_TYPE (COPS_REG_INS))
1838*3d8817e4Smiod {
1839*3d8817e4Smiod if (((cr = get_copregister (reg_name)) == nullcopregister)
1840*3d8817e4Smiod || (crx_copregtab[cr-MAX_REG].type != CRX_CS_REGTYPE))
1841*3d8817e4Smiod as_fatal (_("Illegal register `%s' in cop-special-register list"),
1842*3d8817e4Smiod reg_name);
1843*3d8817e4Smiod mask_reg (getreg_image (cr - cs0), &mask);
1844*3d8817e4Smiod }
1845*3d8817e4Smiod /* User register u<N>. */
1846*3d8817e4Smiod else if (instruction->flags & USER_REG)
1847*3d8817e4Smiod {
1848*3d8817e4Smiod if (streq(reg_name, "uhi"))
1849*3d8817e4Smiod {
1850*3d8817e4Smiod hi_found = 1;
1851*3d8817e4Smiod goto next_inst;
1852*3d8817e4Smiod }
1853*3d8817e4Smiod else if (streq(reg_name, "ulo"))
1854*3d8817e4Smiod {
1855*3d8817e4Smiod lo_found = 1;
1856*3d8817e4Smiod goto next_inst;
1857*3d8817e4Smiod }
1858*3d8817e4Smiod else if (((r = get_register (reg_name)) == nullregister)
1859*3d8817e4Smiod || (crx_regtab[r].type != CRX_U_REGTYPE))
1860*3d8817e4Smiod as_fatal (_("Illegal register `%s' in user register list"), reg_name);
1861*3d8817e4Smiod
1862*3d8817e4Smiod mask_reg (getreg_image (r - u0), &mask);
1863*3d8817e4Smiod }
1864*3d8817e4Smiod /* General purpose register r<N>. */
1865*3d8817e4Smiod else
1866*3d8817e4Smiod {
1867*3d8817e4Smiod if (streq(reg_name, "hi"))
1868*3d8817e4Smiod {
1869*3d8817e4Smiod hi_found = 1;
1870*3d8817e4Smiod goto next_inst;
1871*3d8817e4Smiod }
1872*3d8817e4Smiod else if (streq(reg_name, "lo"))
1873*3d8817e4Smiod {
1874*3d8817e4Smiod lo_found = 1;
1875*3d8817e4Smiod goto next_inst;
1876*3d8817e4Smiod }
1877*3d8817e4Smiod else if (((r = get_register (reg_name)) == nullregister)
1878*3d8817e4Smiod || (crx_regtab[r].type != CRX_R_REGTYPE))
1879*3d8817e4Smiod as_fatal (_("Illegal register `%s' in register list"), reg_name);
1880*3d8817e4Smiod
1881*3d8817e4Smiod mask_reg (getreg_image (r - r0), &mask);
1882*3d8817e4Smiod }
1883*3d8817e4Smiod
1884*3d8817e4Smiod if (++reg_counter > MAX_REGS_IN_MASK16)
1885*3d8817e4Smiod as_bad (_("Maximum %d bits may be set in `mask16' operand"),
1886*3d8817e4Smiod MAX_REGS_IN_MASK16);
1887*3d8817e4Smiod
1888*3d8817e4Smiod next_inst:
1889*3d8817e4Smiod while (!ISALNUM (*paramP) && *paramP != '}')
1890*3d8817e4Smiod paramP++;
1891*3d8817e4Smiod }
1892*3d8817e4Smiod
1893*3d8817e4Smiod if (*++paramP != '\0')
1894*3d8817e4Smiod as_warn (_("rest of line ignored; first ignored character is `%c'"),
1895*3d8817e4Smiod *paramP);
1896*3d8817e4Smiod
1897*3d8817e4Smiod switch (hi_found + lo_found)
1898*3d8817e4Smiod {
1899*3d8817e4Smiod case 0:
1900*3d8817e4Smiod /* At least one register should be specified. */
1901*3d8817e4Smiod if (mask == 0)
1902*3d8817e4Smiod as_bad (_("Illegal `mask16' operand, operation is undefined - `%s'"),
1903*3d8817e4Smiod ins_parse);
1904*3d8817e4Smiod break;
1905*3d8817e4Smiod
1906*3d8817e4Smiod case 1:
1907*3d8817e4Smiod /* HI can't be specified without LO (and vise-versa). */
1908*3d8817e4Smiod as_bad (_("HI/LO registers should be specified together"));
1909*3d8817e4Smiod break;
1910*3d8817e4Smiod
1911*3d8817e4Smiod case 2:
1912*3d8817e4Smiod /* HI/LO registers mustn't be masked with additional registers. */
1913*3d8817e4Smiod if (mask != 0)
1914*3d8817e4Smiod as_bad (_("HI/LO registers should be specified without additional registers"));
1915*3d8817e4Smiod
1916*3d8817e4Smiod default:
1917*3d8817e4Smiod break;
1918*3d8817e4Smiod }
1919*3d8817e4Smiod
1920*3d8817e4Smiod sprintf (maskstring, "$0x%x", mask);
1921*3d8817e4Smiod strcat (new_param, maskstring);
1922*3d8817e4Smiod return new_param;
1923*3d8817e4Smiod }
1924*3d8817e4Smiod
1925*3d8817e4Smiod /* Print the instruction.
1926*3d8817e4Smiod Handle also cases where the instruction is relaxable/relocatable. */
1927*3d8817e4Smiod
1928*3d8817e4Smiod void
print_insn(ins * insn)1929*3d8817e4Smiod print_insn (ins *insn)
1930*3d8817e4Smiod {
1931*3d8817e4Smiod unsigned int i, j, insn_size;
1932*3d8817e4Smiod char *this_frag;
1933*3d8817e4Smiod unsigned short words[4];
1934*3d8817e4Smiod int addr_mod;
1935*3d8817e4Smiod
1936*3d8817e4Smiod /* Arrange the insn encodings in a WORD size array. */
1937*3d8817e4Smiod for (i = 0, j = 0; i < 2; i++)
1938*3d8817e4Smiod {
1939*3d8817e4Smiod words[j++] = (output_opcode[i] >> 16) & 0xFFFF;
1940*3d8817e4Smiod words[j++] = output_opcode[i] & 0xFFFF;
1941*3d8817e4Smiod }
1942*3d8817e4Smiod
1943*3d8817e4Smiod /* Handle relaxtion. */
1944*3d8817e4Smiod if ((instruction->flags & RELAXABLE) && relocatable)
1945*3d8817e4Smiod {
1946*3d8817e4Smiod int relax_subtype;
1947*3d8817e4Smiod
1948*3d8817e4Smiod /* Write the maximal instruction size supported. */
1949*3d8817e4Smiod insn_size = INSN_MAX_SIZE;
1950*3d8817e4Smiod
1951*3d8817e4Smiod /* bCC */
1952*3d8817e4Smiod if (IS_INSN_TYPE (BRANCH_INS))
1953*3d8817e4Smiod relax_subtype = 0;
1954*3d8817e4Smiod /* bal */
1955*3d8817e4Smiod else if (IS_INSN_TYPE (DCR_BRANCH_INS) || IS_INSN_MNEMONIC ("bal"))
1956*3d8817e4Smiod relax_subtype = 3;
1957*3d8817e4Smiod /* cmpbr/bcop */
1958*3d8817e4Smiod else if (IS_INSN_TYPE (CMPBR_INS) || IS_INSN_TYPE (COP_BRANCH_INS))
1959*3d8817e4Smiod relax_subtype = 5;
1960*3d8817e4Smiod else
1961*3d8817e4Smiod abort ();
1962*3d8817e4Smiod
1963*3d8817e4Smiod this_frag = frag_var (rs_machine_dependent, insn_size * 2,
1964*3d8817e4Smiod 4, relax_subtype,
1965*3d8817e4Smiod insn->exp.X_add_symbol,
1966*3d8817e4Smiod insn->exp.X_add_number,
1967*3d8817e4Smiod 0);
1968*3d8817e4Smiod }
1969*3d8817e4Smiod else
1970*3d8817e4Smiod {
1971*3d8817e4Smiod insn_size = instruction->size;
1972*3d8817e4Smiod this_frag = frag_more (insn_size * 2);
1973*3d8817e4Smiod
1974*3d8817e4Smiod /* Handle relocation. */
1975*3d8817e4Smiod if ((relocatable) && (insn->rtype != BFD_RELOC_NONE))
1976*3d8817e4Smiod {
1977*3d8817e4Smiod reloc_howto_type *reloc_howto;
1978*3d8817e4Smiod int size;
1979*3d8817e4Smiod
1980*3d8817e4Smiod reloc_howto = bfd_reloc_type_lookup (stdoutput, insn->rtype);
1981*3d8817e4Smiod
1982*3d8817e4Smiod if (!reloc_howto)
1983*3d8817e4Smiod abort ();
1984*3d8817e4Smiod
1985*3d8817e4Smiod size = bfd_get_reloc_size (reloc_howto);
1986*3d8817e4Smiod
1987*3d8817e4Smiod if (size < 1 || size > 4)
1988*3d8817e4Smiod abort ();
1989*3d8817e4Smiod
1990*3d8817e4Smiod fix_new_exp (frag_now, this_frag - frag_now->fr_literal,
1991*3d8817e4Smiod size, &insn->exp, reloc_howto->pc_relative,
1992*3d8817e4Smiod insn->rtype);
1993*3d8817e4Smiod }
1994*3d8817e4Smiod }
1995*3d8817e4Smiod
1996*3d8817e4Smiod /* Verify a 2-byte code alignment. */
1997*3d8817e4Smiod addr_mod = frag_now_fix () & 1;
1998*3d8817e4Smiod if (frag_now->has_code && frag_now->insn_addr != addr_mod)
1999*3d8817e4Smiod as_bad (_("instruction address is not a multiple of 2"));
2000*3d8817e4Smiod frag_now->insn_addr = addr_mod;
2001*3d8817e4Smiod frag_now->has_code = 1;
2002*3d8817e4Smiod
2003*3d8817e4Smiod /* Write the instruction encoding to frag. */
2004*3d8817e4Smiod for (i = 0; i < insn_size; i++)
2005*3d8817e4Smiod {
2006*3d8817e4Smiod md_number_to_chars (this_frag, (valueT) words[i], 2);
2007*3d8817e4Smiod this_frag += 2;
2008*3d8817e4Smiod }
2009*3d8817e4Smiod }
2010*3d8817e4Smiod
2011*3d8817e4Smiod /* This is the guts of the machine-dependent assembler. OP points to a
2012*3d8817e4Smiod machine dependent instruction. This function is supposed to emit
2013*3d8817e4Smiod the frags/bytes it assembles to. */
2014*3d8817e4Smiod
2015*3d8817e4Smiod void
md_assemble(char * op)2016*3d8817e4Smiod md_assemble (char *op)
2017*3d8817e4Smiod {
2018*3d8817e4Smiod ins crx_ins;
2019*3d8817e4Smiod char *param;
2020*3d8817e4Smiod char c;
2021*3d8817e4Smiod
2022*3d8817e4Smiod /* Reset global variables for a new instruction. */
2023*3d8817e4Smiod reset_vars (op);
2024*3d8817e4Smiod
2025*3d8817e4Smiod /* Strip the mnemonic. */
2026*3d8817e4Smiod for (param = op; *param != 0 && !ISSPACE (*param); param++)
2027*3d8817e4Smiod ;
2028*3d8817e4Smiod c = *param;
2029*3d8817e4Smiod *param++ = '\0';
2030*3d8817e4Smiod
2031*3d8817e4Smiod /* Find the instruction. */
2032*3d8817e4Smiod instruction = (const inst *) hash_find (crx_inst_hash, op);
2033*3d8817e4Smiod if (instruction == NULL)
2034*3d8817e4Smiod {
2035*3d8817e4Smiod as_bad (_("Unknown opcode: `%s'"), op);
2036*3d8817e4Smiod return;
2037*3d8817e4Smiod }
2038*3d8817e4Smiod
2039*3d8817e4Smiod /* Tie dwarf2 debug info to the address at the start of the insn. */
2040*3d8817e4Smiod dwarf2_emit_insn (0);
2041*3d8817e4Smiod
2042*3d8817e4Smiod /* Parse the instruction's operands. */
2043*3d8817e4Smiod parse_insn (&crx_ins, param);
2044*3d8817e4Smiod
2045*3d8817e4Smiod /* Assemble the instruction - return upon failure. */
2046*3d8817e4Smiod if (assemble_insn (op, &crx_ins) == 0)
2047*3d8817e4Smiod return;
2048*3d8817e4Smiod
2049*3d8817e4Smiod /* Print the instruction. */
2050*3d8817e4Smiod print_insn (&crx_ins);
2051*3d8817e4Smiod }
2052