tc-d10v.c revision 1.1 1 1.1 christos /* tc-d10v.c -- Assembler code for the Mitsubishi D10V
2 1.1 christos Copyright 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2005, 2006,
3 1.1 christos 2007, 2009, 2010
4 1.1 christos Free Software Foundation, Inc.
5 1.1 christos
6 1.1 christos This file is part of GAS, the GNU Assembler.
7 1.1 christos
8 1.1 christos GAS is free software; you can redistribute it and/or modify
9 1.1 christos it under the terms of the GNU General Public License as published by
10 1.1 christos the Free Software Foundation; either version 3, or (at your option)
11 1.1 christos any later version.
12 1.1 christos
13 1.1 christos GAS is distributed in the hope that it will be useful,
14 1.1 christos but WITHOUT ANY WARRANTY; without even the implied warranty of
15 1.1 christos MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 1.1 christos GNU General Public License for more details.
17 1.1 christos
18 1.1 christos You should have received a copy of the GNU General Public License
19 1.1 christos along with GAS; see the file COPYING. If not, write to
20 1.1 christos the Free Software Foundation, 51 Franklin Street - Fifth Floor,
21 1.1 christos Boston, MA 02110-1301, USA. */
22 1.1 christos
23 1.1 christos #include "as.h"
24 1.1 christos #include "safe-ctype.h"
25 1.1 christos #include "subsegs.h"
26 1.1 christos #include "opcode/d10v.h"
27 1.1 christos #include "elf/ppc.h"
28 1.1 christos #include "dwarf2dbg.h"
29 1.1 christos
30 1.1 christos const char comment_chars[] = ";";
31 1.1 christos const char line_comment_chars[] = "#";
32 1.1 christos const char line_separator_chars[] = "";
33 1.1 christos const char *md_shortopts = "O";
34 1.1 christos const char EXP_CHARS[] = "eE";
35 1.1 christos const char FLT_CHARS[] = "dD";
36 1.1 christos
37 1.1 christos int Optimizing = 0;
38 1.1 christos
39 1.1 christos #define AT_WORD_P(X) ((X)->X_op == O_right_shift \
40 1.1 christos && (X)->X_op_symbol != NULL \
41 1.1 christos && symbol_constant_p ((X)->X_op_symbol) \
42 1.1 christos && S_GET_VALUE ((X)->X_op_symbol) == AT_WORD_RIGHT_SHIFT)
43 1.1 christos #define AT_WORD_RIGHT_SHIFT 2
44 1.1 christos
45 1.1 christos /* Fixups. */
46 1.1 christos #define MAX_INSN_FIXUPS 5
47 1.1 christos
48 1.1 christos struct d10v_fixup
49 1.1 christos {
50 1.1 christos expressionS exp;
51 1.1 christos int operand;
52 1.1 christos int pcrel;
53 1.1 christos int size;
54 1.1 christos bfd_reloc_code_real_type reloc;
55 1.1 christos };
56 1.1 christos
57 1.1 christos typedef struct _fixups
58 1.1 christos {
59 1.1 christos int fc;
60 1.1 christos struct d10v_fixup fix[MAX_INSN_FIXUPS];
61 1.1 christos struct _fixups *next;
62 1.1 christos } Fixups;
63 1.1 christos
64 1.1 christos static Fixups FixUps[2];
65 1.1 christos static Fixups *fixups;
66 1.1 christos
67 1.1 christos static int do_not_ignore_hash = 0;
68 1.1 christos
69 1.1 christos typedef int packing_type;
70 1.1 christos #define PACK_UNSPEC (0) /* Packing order not specified. */
71 1.1 christos #define PACK_PARALLEL (1) /* "||" */
72 1.1 christos #define PACK_LEFT_RIGHT (2) /* "->" */
73 1.1 christos #define PACK_RIGHT_LEFT (3) /* "<-" */
74 1.1 christos static packing_type etype = PACK_UNSPEC; /* Used by d10v_cleanup. */
75 1.1 christos
76 1.1 christos /* TRUE if instruction swapping warnings should be inhibited.
77 1.1 christos --nowarnswap. */
78 1.1 christos static bfd_boolean flag_warn_suppress_instructionswap;
79 1.1 christos
80 1.1 christos /* TRUE if instruction packing should be performed when --gstabs is specified.
81 1.1 christos --gstabs-packing, --no-gstabs-packing. */
82 1.1 christos static bfd_boolean flag_allow_gstabs_packing = 1;
83 1.1 christos
84 1.1 christos /* Local functions. */
85 1.1 christos
86 1.1 christos enum options
87 1.1 christos {
88 1.1 christos OPTION_NOWARNSWAP = OPTION_MD_BASE,
89 1.1 christos OPTION_GSTABSPACKING,
90 1.1 christos OPTION_NOGSTABSPACKING
91 1.1 christos };
92 1.1 christos
93 1.1 christos struct option md_longopts[] =
94 1.1 christos {
95 1.1 christos {"nowarnswap", no_argument, NULL, OPTION_NOWARNSWAP},
96 1.1 christos {"gstabspacking", no_argument, NULL, OPTION_GSTABSPACKING},
97 1.1 christos {"gstabs-packing", no_argument, NULL, OPTION_GSTABSPACKING},
98 1.1 christos {"nogstabspacking", no_argument, NULL, OPTION_NOGSTABSPACKING},
99 1.1 christos {"no-gstabs-packing", no_argument, NULL, OPTION_NOGSTABSPACKING},
100 1.1 christos {NULL, no_argument, NULL, 0}
101 1.1 christos };
102 1.1 christos
103 1.1 christos size_t md_longopts_size = sizeof (md_longopts);
104 1.1 christos
105 1.1 christos /* Opcode hash table. */
106 1.1 christos static struct hash_control *d10v_hash;
107 1.1 christos
108 1.1 christos /* Do a binary search of the d10v_predefined_registers array to see if
109 1.1 christos NAME is a valid regiter name. Return the register number from the
110 1.1 christos array on success, or -1 on failure. */
111 1.1 christos
112 1.1 christos static int
113 1.1 christos reg_name_search (char *name)
114 1.1 christos {
115 1.1 christos int middle, low, high;
116 1.1 christos int cmp;
117 1.1 christos
118 1.1 christos low = 0;
119 1.1 christos high = d10v_reg_name_cnt () - 1;
120 1.1 christos
121 1.1 christos do
122 1.1 christos {
123 1.1 christos middle = (low + high) / 2;
124 1.1 christos cmp = strcasecmp (name, d10v_predefined_registers[middle].name);
125 1.1 christos if (cmp < 0)
126 1.1 christos high = middle - 1;
127 1.1 christos else if (cmp > 0)
128 1.1 christos low = middle + 1;
129 1.1 christos else
130 1.1 christos return d10v_predefined_registers[middle].value;
131 1.1 christos }
132 1.1 christos while (low <= high);
133 1.1 christos return -1;
134 1.1 christos }
135 1.1 christos
136 1.1 christos /* Check the string at input_line_pointer
137 1.1 christos to see if it is a valid register name. */
138 1.1 christos
139 1.1 christos static int
140 1.1 christos register_name (expressionS *expressionP)
141 1.1 christos {
142 1.1 christos int reg_number;
143 1.1 christos char c, *p = input_line_pointer;
144 1.1 christos
145 1.1 christos while (*p
146 1.1 christos && *p != '\n' && *p != '\r' && *p != ',' && *p != ' ' && *p != ')')
147 1.1 christos p++;
148 1.1 christos
149 1.1 christos c = *p;
150 1.1 christos if (c)
151 1.1 christos *p++ = 0;
152 1.1 christos
153 1.1 christos /* Look to see if it's in the register table. */
154 1.1 christos reg_number = reg_name_search (input_line_pointer);
155 1.1 christos if (reg_number >= 0)
156 1.1 christos {
157 1.1 christos expressionP->X_op = O_register;
158 1.1 christos /* Temporarily store a pointer to the string here. */
159 1.1 christos expressionP->X_op_symbol = (symbolS *) input_line_pointer;
160 1.1 christos expressionP->X_add_number = reg_number;
161 1.1 christos input_line_pointer = p;
162 1.1 christos return 1;
163 1.1 christos }
164 1.1 christos if (c)
165 1.1 christos *(p - 1) = c;
166 1.1 christos return 0;
167 1.1 christos }
168 1.1 christos
169 1.1 christos static int
170 1.1 christos check_range (unsigned long num, int bits, int flags)
171 1.1 christos {
172 1.1 christos long min, max;
173 1.1 christos int retval = 0;
174 1.1 christos
175 1.1 christos /* Don't bother checking 16-bit values. */
176 1.1 christos if (bits == 16)
177 1.1 christos return 0;
178 1.1 christos
179 1.1 christos if (flags & OPERAND_SHIFT)
180 1.1 christos {
181 1.1 christos /* All special shift operands are unsigned and <= 16.
182 1.1 christos We allow 0 for now. */
183 1.1 christos if (num > 16)
184 1.1 christos return 1;
185 1.1 christos else
186 1.1 christos return 0;
187 1.1 christos }
188 1.1 christos
189 1.1 christos if (flags & OPERAND_SIGNED)
190 1.1 christos {
191 1.1 christos /* Signed 3-bit integers are restricted to the (-2, 3) range. */
192 1.1 christos if (flags & RESTRICTED_NUM3)
193 1.1 christos {
194 1.1 christos if ((long) num < -2 || (long) num > 3)
195 1.1 christos retval = 1;
196 1.1 christos }
197 1.1 christos else
198 1.1 christos {
199 1.1 christos max = (1 << (bits - 1)) - 1;
200 1.1 christos min = - (1 << (bits - 1));
201 1.1 christos if (((long) num > max) || ((long) num < min))
202 1.1 christos retval = 1;
203 1.1 christos }
204 1.1 christos }
205 1.1 christos else
206 1.1 christos {
207 1.1 christos max = (1 << bits) - 1;
208 1.1 christos min = 0;
209 1.1 christos if (((long) num > max) || ((long) num < min))
210 1.1 christos retval = 1;
211 1.1 christos }
212 1.1 christos return retval;
213 1.1 christos }
214 1.1 christos
215 1.1 christos void
216 1.1 christos md_show_usage (FILE *stream)
217 1.1 christos {
218 1.1 christos fprintf (stream, _("D10V options:\n\
219 1.1 christos -O Optimize. Will do some operations in parallel.\n\
220 1.1 christos --gstabs-packing Pack adjacent short instructions together even\n\
221 1.1 christos when --gstabs is specified. On by default.\n\
222 1.1 christos --no-gstabs-packing If --gstabs is specified, do not pack adjacent\n\
223 1.1 christos instructions together.\n"));
224 1.1 christos }
225 1.1 christos
226 1.1 christos int
227 1.1 christos md_parse_option (int c, char *arg ATTRIBUTE_UNUSED)
228 1.1 christos {
229 1.1 christos switch (c)
230 1.1 christos {
231 1.1 christos case 'O':
232 1.1 christos /* Optimize. Will attempt to parallelize operations. */
233 1.1 christos Optimizing = 1;
234 1.1 christos break;
235 1.1 christos case OPTION_NOWARNSWAP:
236 1.1 christos flag_warn_suppress_instructionswap = 1;
237 1.1 christos break;
238 1.1 christos case OPTION_GSTABSPACKING:
239 1.1 christos flag_allow_gstabs_packing = 1;
240 1.1 christos break;
241 1.1 christos case OPTION_NOGSTABSPACKING:
242 1.1 christos flag_allow_gstabs_packing = 0;
243 1.1 christos break;
244 1.1 christos default:
245 1.1 christos return 0;
246 1.1 christos }
247 1.1 christos return 1;
248 1.1 christos }
249 1.1 christos
250 1.1 christos symbolS *
251 1.1 christos md_undefined_symbol (char *name ATTRIBUTE_UNUSED)
252 1.1 christos {
253 1.1 christos return 0;
254 1.1 christos }
255 1.1 christos
256 1.1 christos char *
257 1.1 christos md_atof (int type, char *litP, int *sizeP)
258 1.1 christos {
259 1.1 christos return ieee_md_atof (type, litP, sizeP, TRUE);
260 1.1 christos }
261 1.1 christos
262 1.1 christos void
263 1.1 christos md_convert_frag (bfd *abfd ATTRIBUTE_UNUSED,
264 1.1 christos asection *sec ATTRIBUTE_UNUSED,
265 1.1 christos fragS *fragP ATTRIBUTE_UNUSED)
266 1.1 christos {
267 1.1 christos abort ();
268 1.1 christos }
269 1.1 christos
270 1.1 christos valueT
271 1.1 christos md_section_align (asection *seg, valueT addr)
272 1.1 christos {
273 1.1 christos int align = bfd_get_section_alignment (stdoutput, seg);
274 1.1 christos return ((addr + (1 << align) - 1) & (-1 << align));
275 1.1 christos }
276 1.1 christos
277 1.1 christos void
278 1.1 christos md_begin (void)
279 1.1 christos {
280 1.1 christos char *prev_name = "";
281 1.1 christos struct d10v_opcode *opcode;
282 1.1 christos d10v_hash = hash_new ();
283 1.1 christos
284 1.1 christos /* Insert unique names into hash table. The D10v instruction set
285 1.1 christos has many identical opcode names that have different opcodes based
286 1.1 christos on the operands. This hash table then provides a quick index to
287 1.1 christos the first opcode with a particular name in the opcode table. */
288 1.1 christos
289 1.1 christos for (opcode = (struct d10v_opcode *) d10v_opcodes; opcode->name; opcode++)
290 1.1 christos {
291 1.1 christos if (strcmp (prev_name, opcode->name))
292 1.1 christos {
293 1.1 christos prev_name = (char *) opcode->name;
294 1.1 christos hash_insert (d10v_hash, opcode->name, (char *) opcode);
295 1.1 christos }
296 1.1 christos }
297 1.1 christos
298 1.1 christos fixups = &FixUps[0];
299 1.1 christos FixUps[0].next = &FixUps[1];
300 1.1 christos FixUps[1].next = &FixUps[0];
301 1.1 christos }
302 1.1 christos
303 1.1 christos /* Remove the postincrement or postdecrement operator ( '+' or '-' )
304 1.1 christos from an expression. */
305 1.1 christos
306 1.1 christos static int
307 1.1 christos postfix (char *p)
308 1.1 christos {
309 1.1 christos while (*p != '-' && *p != '+')
310 1.1 christos {
311 1.1 christos if (*p == 0 || *p == '\n' || *p == '\r')
312 1.1 christos break;
313 1.1 christos p++;
314 1.1 christos }
315 1.1 christos
316 1.1 christos if (*p == '-')
317 1.1 christos {
318 1.1 christos *p = ' ';
319 1.1 christos return -1;
320 1.1 christos }
321 1.1 christos if (*p == '+')
322 1.1 christos {
323 1.1 christos *p = ' ';
324 1.1 christos return 1;
325 1.1 christos }
326 1.1 christos
327 1.1 christos return 0;
328 1.1 christos }
329 1.1 christos
330 1.1 christos static bfd_reloc_code_real_type
331 1.1 christos get_reloc (struct d10v_operand *op)
332 1.1 christos {
333 1.1 christos int bits = op->bits;
334 1.1 christos
335 1.1 christos if (bits <= 4)
336 1.1 christos return 0;
337 1.1 christos
338 1.1 christos if (op->flags & OPERAND_ADDR)
339 1.1 christos {
340 1.1 christos if (bits == 8)
341 1.1 christos return BFD_RELOC_D10V_10_PCREL_R;
342 1.1 christos else
343 1.1 christos return BFD_RELOC_D10V_18_PCREL;
344 1.1 christos }
345 1.1 christos
346 1.1 christos return BFD_RELOC_16;
347 1.1 christos }
348 1.1 christos
349 1.1 christos /* Parse a string of operands. Return an array of expressions. */
350 1.1 christos
351 1.1 christos static int
352 1.1 christos get_operands (expressionS exp[])
353 1.1 christos {
354 1.1 christos char *p = input_line_pointer;
355 1.1 christos int numops = 0;
356 1.1 christos int post = 0;
357 1.1 christos int uses_at = 0;
358 1.1 christos
359 1.1 christos while (*p)
360 1.1 christos {
361 1.1 christos while (*p == ' ' || *p == '\t' || *p == ',')
362 1.1 christos p++;
363 1.1 christos if (*p == 0 || *p == '\n' || *p == '\r')
364 1.1 christos break;
365 1.1 christos
366 1.1 christos if (*p == '@')
367 1.1 christos {
368 1.1 christos uses_at = 1;
369 1.1 christos
370 1.1 christos p++;
371 1.1 christos exp[numops].X_op = O_absent;
372 1.1 christos if (*p == '(')
373 1.1 christos {
374 1.1 christos p++;
375 1.1 christos exp[numops].X_add_number = OPERAND_ATPAR;
376 1.1 christos }
377 1.1 christos else if (*p == '-')
378 1.1 christos {
379 1.1 christos p++;
380 1.1 christos exp[numops].X_add_number = OPERAND_ATMINUS;
381 1.1 christos }
382 1.1 christos else
383 1.1 christos {
384 1.1 christos exp[numops].X_add_number = OPERAND_ATSIGN;
385 1.1 christos if (*p == '+')
386 1.1 christos {
387 1.1 christos numops++;
388 1.1 christos exp[numops].X_op = O_absent;
389 1.1 christos exp[numops].X_add_number = OPERAND_PLUS;
390 1.1 christos p++;
391 1.1 christos }
392 1.1 christos post = postfix (p);
393 1.1 christos }
394 1.1 christos numops++;
395 1.1 christos continue;
396 1.1 christos }
397 1.1 christos
398 1.1 christos if (*p == ')')
399 1.1 christos {
400 1.1 christos /* Just skip the trailing paren. */
401 1.1 christos p++;
402 1.1 christos continue;
403 1.1 christos }
404 1.1 christos
405 1.1 christos input_line_pointer = p;
406 1.1 christos
407 1.1 christos /* Check to see if it might be a register name. */
408 1.1 christos if (!register_name (&exp[numops]))
409 1.1 christos {
410 1.1 christos /* Parse as an expression. */
411 1.1 christos if (uses_at)
412 1.1 christos {
413 1.1 christos /* Any expression that involves the indirect addressing
414 1.1 christos cannot also involve immediate addressing. Therefore
415 1.1 christos the use of the hash character is illegal. */
416 1.1 christos int save = do_not_ignore_hash;
417 1.1 christos do_not_ignore_hash = 1;
418 1.1 christos
419 1.1 christos expression (&exp[numops]);
420 1.1 christos
421 1.1 christos do_not_ignore_hash = save;
422 1.1 christos }
423 1.1 christos else
424 1.1 christos expression (&exp[numops]);
425 1.1 christos }
426 1.1 christos
427 1.1 christos if (strncasecmp (input_line_pointer, "@word", 5) == 0)
428 1.1 christos {
429 1.1 christos input_line_pointer += 5;
430 1.1 christos if (exp[numops].X_op == O_register)
431 1.1 christos {
432 1.1 christos /* If it looked like a register name but was followed by
433 1.1 christos "@word" then it was really a symbol, so change it to
434 1.1 christos one. */
435 1.1 christos exp[numops].X_op = O_symbol;
436 1.1 christos exp[numops].X_add_symbol =
437 1.1 christos symbol_find_or_make ((char *) exp[numops].X_op_symbol);
438 1.1 christos }
439 1.1 christos
440 1.1 christos /* Check for identifier@word+constant. */
441 1.1 christos if (*input_line_pointer == '-' || *input_line_pointer == '+')
442 1.1 christos {
443 1.1 christos expressionS new_exp;
444 1.1 christos expression (&new_exp);
445 1.1 christos exp[numops].X_add_number = new_exp.X_add_number;
446 1.1 christos }
447 1.1 christos
448 1.1 christos /* Convert expr into a right shift by AT_WORD_RIGHT_SHIFT. */
449 1.1 christos {
450 1.1 christos expressionS new_exp;
451 1.1 christos memset (&new_exp, 0, sizeof new_exp);
452 1.1 christos new_exp.X_add_number = AT_WORD_RIGHT_SHIFT;
453 1.1 christos new_exp.X_op = O_constant;
454 1.1 christos new_exp.X_unsigned = 1;
455 1.1 christos exp[numops].X_op_symbol = make_expr_symbol (&new_exp);
456 1.1 christos exp[numops].X_op = O_right_shift;
457 1.1 christos }
458 1.1 christos
459 1.1 christos know (AT_WORD_P (&exp[numops]));
460 1.1 christos }
461 1.1 christos
462 1.1 christos if (exp[numops].X_op == O_illegal)
463 1.1 christos as_bad (_("illegal operand"));
464 1.1 christos else if (exp[numops].X_op == O_absent)
465 1.1 christos as_bad (_("missing operand"));
466 1.1 christos
467 1.1 christos numops++;
468 1.1 christos p = input_line_pointer;
469 1.1 christos }
470 1.1 christos
471 1.1 christos switch (post)
472 1.1 christos {
473 1.1 christos case -1: /* Postdecrement mode. */
474 1.1 christos exp[numops].X_op = O_absent;
475 1.1 christos exp[numops++].X_add_number = OPERAND_MINUS;
476 1.1 christos break;
477 1.1 christos case 1: /* Postincrement mode. */
478 1.1 christos exp[numops].X_op = O_absent;
479 1.1 christos exp[numops++].X_add_number = OPERAND_PLUS;
480 1.1 christos break;
481 1.1 christos }
482 1.1 christos
483 1.1 christos exp[numops].X_op = 0;
484 1.1 christos return numops;
485 1.1 christos }
486 1.1 christos
487 1.1 christos static unsigned long
488 1.1 christos d10v_insert_operand (unsigned long insn,
489 1.1 christos int op_type,
490 1.1 christos offsetT value,
491 1.1 christos int left,
492 1.1 christos fixS *fix)
493 1.1 christos {
494 1.1 christos int shift, bits;
495 1.1 christos
496 1.1 christos shift = d10v_operands[op_type].shift;
497 1.1 christos if (left)
498 1.1 christos shift += 15;
499 1.1 christos
500 1.1 christos bits = d10v_operands[op_type].bits;
501 1.1 christos
502 1.1 christos /* Truncate to the proper number of bits. */
503 1.1 christos if (check_range (value, bits, d10v_operands[op_type].flags))
504 1.1 christos as_bad_where (fix->fx_file, fix->fx_line,
505 1.1 christos _("operand out of range: %ld"), (long) value);
506 1.1 christos
507 1.1 christos value &= 0x7FFFFFFF >> (31 - bits);
508 1.1 christos insn |= (value << shift);
509 1.1 christos
510 1.1 christos return insn;
511 1.1 christos }
512 1.1 christos
513 1.1 christos /* Take a pointer to the opcode entry in the opcode table and the
514 1.1 christos array of operand expressions. Return the instruction. */
515 1.1 christos
516 1.1 christos static unsigned long
517 1.1 christos build_insn (struct d10v_opcode *opcode,
518 1.1 christos expressionS *opers,
519 1.1 christos unsigned long insn)
520 1.1 christos {
521 1.1 christos int i, bits, shift, flags, format;
522 1.1 christos unsigned long number;
523 1.1 christos
524 1.1 christos /* The insn argument is only used for the DIVS kludge. */
525 1.1 christos if (insn)
526 1.1 christos format = LONG_R;
527 1.1 christos else
528 1.1 christos {
529 1.1 christos insn = opcode->opcode;
530 1.1 christos format = opcode->format;
531 1.1 christos }
532 1.1 christos
533 1.1 christos for (i = 0; opcode->operands[i]; i++)
534 1.1 christos {
535 1.1 christos flags = d10v_operands[opcode->operands[i]].flags;
536 1.1 christos bits = d10v_operands[opcode->operands[i]].bits;
537 1.1 christos shift = d10v_operands[opcode->operands[i]].shift;
538 1.1 christos number = opers[i].X_add_number;
539 1.1 christos
540 1.1 christos if (flags & OPERAND_REG)
541 1.1 christos {
542 1.1 christos number &= REGISTER_MASK;
543 1.1 christos if (format == LONG_L)
544 1.1 christos shift += 15;
545 1.1 christos }
546 1.1 christos
547 1.1 christos if (opers[i].X_op != O_register && opers[i].X_op != O_constant)
548 1.1 christos {
549 1.1 christos /* Now create a fixup. */
550 1.1 christos
551 1.1 christos if (fixups->fc >= MAX_INSN_FIXUPS)
552 1.1 christos as_fatal (_("too many fixups"));
553 1.1 christos
554 1.1 christos if (AT_WORD_P (&opers[i]))
555 1.1 christos {
556 1.1 christos /* Recognize XXX>>1+N aka XXX@word+N as special (AT_WORD). */
557 1.1 christos fixups->fix[fixups->fc].reloc = BFD_RELOC_D10V_18;
558 1.1 christos opers[i].X_op = O_symbol;
559 1.1 christos opers[i].X_op_symbol = NULL; /* Should free it. */
560 1.1 christos /* number is left shifted by AT_WORD_RIGHT_SHIFT so
561 1.1 christos that, it is aligned with the symbol's value. Later,
562 1.1 christos BFD_RELOC_D10V_18 will right shift (symbol_value +
563 1.1 christos X_add_number). */
564 1.1 christos number <<= AT_WORD_RIGHT_SHIFT;
565 1.1 christos opers[i].X_add_number = number;
566 1.1 christos }
567 1.1 christos else
568 1.1 christos {
569 1.1 christos fixups->fix[fixups->fc].reloc =
570 1.1 christos get_reloc ((struct d10v_operand *) &d10v_operands[opcode->operands[i]]);
571 1.1 christos
572 1.1 christos /* Check that an immediate was passed to ops that expect one. */
573 1.1 christos if ((flags & OPERAND_NUM)
574 1.1 christos && (fixups->fix[fixups->fc].reloc == 0))
575 1.1 christos as_bad (_("operand is not an immediate"));
576 1.1 christos }
577 1.1 christos
578 1.1 christos if (fixups->fix[fixups->fc].reloc == BFD_RELOC_16 ||
579 1.1 christos fixups->fix[fixups->fc].reloc == BFD_RELOC_D10V_18)
580 1.1 christos fixups->fix[fixups->fc].size = 2;
581 1.1 christos else
582 1.1 christos fixups->fix[fixups->fc].size = 4;
583 1.1 christos
584 1.1 christos fixups->fix[fixups->fc].exp = opers[i];
585 1.1 christos fixups->fix[fixups->fc].operand = opcode->operands[i];
586 1.1 christos fixups->fix[fixups->fc].pcrel =
587 1.1 christos (flags & OPERAND_ADDR) ? TRUE : FALSE;
588 1.1 christos (fixups->fc)++;
589 1.1 christos }
590 1.1 christos
591 1.1 christos /* Truncate to the proper number of bits. */
592 1.1 christos if ((opers[i].X_op == O_constant) && check_range (number, bits, flags))
593 1.1 christos as_bad (_("operand out of range: %lu"), number);
594 1.1 christos number &= 0x7FFFFFFF >> (31 - bits);
595 1.1 christos insn = insn | (number << shift);
596 1.1 christos }
597 1.1 christos
598 1.1 christos /* kludge: for DIVS, we need to put the operands in twice on the second
599 1.1 christos pass, format is changed to LONG_R to force the second set of operands
600 1.1 christos to not be shifted over 15. */
601 1.1 christos if ((opcode->opcode == OPCODE_DIVS) && (format == LONG_L))
602 1.1 christos insn = build_insn (opcode, opers, insn);
603 1.1 christos
604 1.1 christos return insn;
605 1.1 christos }
606 1.1 christos
607 1.1 christos /* Write out a long form instruction. */
608 1.1 christos
609 1.1 christos static void
610 1.1 christos write_long (unsigned long insn, Fixups *fx)
611 1.1 christos {
612 1.1 christos int i, where;
613 1.1 christos char *f = frag_more (4);
614 1.1 christos
615 1.1 christos dwarf2_emit_insn (4);
616 1.1 christos insn |= FM11;
617 1.1 christos number_to_chars_bigendian (f, insn, 4);
618 1.1 christos
619 1.1 christos for (i = 0; i < fx->fc; i++)
620 1.1 christos {
621 1.1 christos if (fx->fix[i].reloc)
622 1.1 christos {
623 1.1 christos where = f - frag_now->fr_literal;
624 1.1 christos if (fx->fix[i].size == 2)
625 1.1 christos where += 2;
626 1.1 christos
627 1.1 christos if (fx->fix[i].reloc == BFD_RELOC_D10V_18)
628 1.1 christos fx->fix[i].operand |= 4096;
629 1.1 christos
630 1.1 christos fix_new_exp (frag_now,
631 1.1 christos where,
632 1.1 christos fx->fix[i].size,
633 1.1 christos &(fx->fix[i].exp),
634 1.1 christos fx->fix[i].pcrel,
635 1.1 christos fx->fix[i].operand|2048);
636 1.1 christos }
637 1.1 christos }
638 1.1 christos fx->fc = 0;
639 1.1 christos }
640 1.1 christos
641 1.1 christos /* Write out a short form instruction by itself. */
642 1.1 christos
643 1.1 christos static void
644 1.1 christos write_1_short (struct d10v_opcode *opcode,
645 1.1 christos unsigned long insn,
646 1.1 christos Fixups *fx)
647 1.1 christos {
648 1.1 christos char *f = frag_more (4);
649 1.1 christos int i, where;
650 1.1 christos
651 1.1 christos dwarf2_emit_insn (4);
652 1.1 christos if (opcode->exec_type & PARONLY)
653 1.1 christos as_fatal (_("Instruction must be executed in parallel with another instruction."));
654 1.1 christos
655 1.1 christos /* The other container needs to be NOP.
656 1.1 christos According to 4.3.1: for FM=00, sub-instructions performed only by IU
657 1.1 christos cannot be encoded in L-container. */
658 1.1 christos if (opcode->unit == IU)
659 1.1 christos insn |= FM00 | (NOP << 15); /* Right container. */
660 1.1 christos else
661 1.1 christos insn = FM00 | (insn << 15) | NOP; /* Left container. */
662 1.1 christos
663 1.1 christos number_to_chars_bigendian (f, insn, 4);
664 1.1 christos for (i = 0; i < fx->fc; i++)
665 1.1 christos {
666 1.1 christos if (fx->fix[i].reloc)
667 1.1 christos {
668 1.1 christos where = f - frag_now->fr_literal;
669 1.1 christos if (fx->fix[i].size == 2)
670 1.1 christos where += 2;
671 1.1 christos
672 1.1 christos if (fx->fix[i].reloc == BFD_RELOC_D10V_18)
673 1.1 christos fx->fix[i].operand |= 4096;
674 1.1 christos
675 1.1 christos /* If it's an R reloc, we may have to switch it to L. */
676 1.1 christos if ((fx->fix[i].reloc == BFD_RELOC_D10V_10_PCREL_R)
677 1.1 christos && (opcode->unit != IU))
678 1.1 christos fx->fix[i].operand |= 1024;
679 1.1 christos
680 1.1 christos fix_new_exp (frag_now,
681 1.1 christos where,
682 1.1 christos fx->fix[i].size,
683 1.1 christos &(fx->fix[i].exp),
684 1.1 christos fx->fix[i].pcrel,
685 1.1 christos fx->fix[i].operand|2048);
686 1.1 christos }
687 1.1 christos }
688 1.1 christos fx->fc = 0;
689 1.1 christos }
690 1.1 christos
691 1.1 christos /* Determine if there are any resource conflicts among two manually
692 1.1 christos parallelized instructions. Some of this was lifted from parallel_ok. */
693 1.1 christos
694 1.1 christos static void
695 1.1 christos check_resource_conflict (struct d10v_opcode *op1,
696 1.1 christos unsigned long insn1,
697 1.1 christos struct d10v_opcode *op2,
698 1.1 christos unsigned long insn2)
699 1.1 christos {
700 1.1 christos int i, j, flags, mask, shift, regno;
701 1.1 christos unsigned long ins, mod[2];
702 1.1 christos struct d10v_opcode *op;
703 1.1 christos
704 1.1 christos if ((op1->exec_type & SEQ)
705 1.1 christos || ! ((op1->exec_type & PAR) || (op1->exec_type & PARONLY)))
706 1.1 christos {
707 1.1 christos as_warn (_("packing conflict: %s must dispatch sequentially"),
708 1.1 christos op1->name);
709 1.1 christos return;
710 1.1 christos }
711 1.1 christos
712 1.1 christos if ((op2->exec_type & SEQ)
713 1.1 christos || ! ((op2->exec_type & PAR) || (op2->exec_type & PARONLY)))
714 1.1 christos {
715 1.1 christos as_warn (_("packing conflict: %s must dispatch sequentially"),
716 1.1 christos op2->name);
717 1.1 christos return;
718 1.1 christos }
719 1.1 christos
720 1.1 christos /* See if both instructions write to the same resource.
721 1.1 christos
722 1.1 christos The idea here is to create two sets of bitmasks (mod and used) which
723 1.1 christos indicate which registers are modified or used by each instruction.
724 1.1 christos The operation can only be done in parallel if neither instruction
725 1.1 christos modifies the same register. Accesses to control registers and memory
726 1.1 christos are treated as accesses to a single register. So if both instructions
727 1.1 christos write memory or if the first instruction writes memory and the second
728 1.1 christos reads, then they cannot be done in parallel. We treat reads to the PSW
729 1.1 christos (which includes C, F0, and F1) in isolation. So simultaneously writing
730 1.1 christos C and F0 in two different sub-instructions is permitted. */
731 1.1 christos
732 1.1 christos /* The bitmasks (mod and used) look like this (bit 31 = MSB).
733 1.1 christos r0-r15 0-15
734 1.1 christos a0-a1 16-17
735 1.1 christos cr (not psw) 18
736 1.1 christos psw(other) 19
737 1.1 christos mem 20
738 1.1 christos psw(C flag) 21
739 1.1 christos psw(F0 flag) 22 */
740 1.1 christos
741 1.1 christos for (j = 0; j < 2; j++)
742 1.1 christos {
743 1.1 christos if (j == 0)
744 1.1 christos {
745 1.1 christos op = op1;
746 1.1 christos ins = insn1;
747 1.1 christos }
748 1.1 christos else
749 1.1 christos {
750 1.1 christos op = op2;
751 1.1 christos ins = insn2;
752 1.1 christos }
753 1.1 christos mod[j] = 0;
754 1.1 christos if (op->exec_type & BRANCH_LINK)
755 1.1 christos mod[j] |= 1 << 13;
756 1.1 christos
757 1.1 christos for (i = 0; op->operands[i]; i++)
758 1.1 christos {
759 1.1 christos flags = d10v_operands[op->operands[i]].flags;
760 1.1 christos shift = d10v_operands[op->operands[i]].shift;
761 1.1 christos mask = 0x7FFFFFFF >> (31 - d10v_operands[op->operands[i]].bits);
762 1.1 christos if (flags & OPERAND_REG)
763 1.1 christos {
764 1.1 christos regno = (ins >> shift) & mask;
765 1.1 christos if (flags & (OPERAND_ACC0 | OPERAND_ACC1))
766 1.1 christos regno += 16;
767 1.1 christos else if (flags & OPERAND_CONTROL) /* mvtc or mvfc */
768 1.1 christos {
769 1.1 christos if (regno == 0)
770 1.1 christos regno = 19;
771 1.1 christos else
772 1.1 christos regno = 18;
773 1.1 christos }
774 1.1 christos else if (flags & OPERAND_FFLAG)
775 1.1 christos regno = 22;
776 1.1 christos else if (flags & OPERAND_CFLAG)
777 1.1 christos regno = 21;
778 1.1 christos
779 1.1 christos if (flags & OPERAND_DEST
780 1.1 christos /* Auto inc/dec also modifies the register. */
781 1.1 christos || (op->operands[i + 1] != 0
782 1.1 christos && (d10v_operands[op->operands[i + 1]].flags
783 1.1 christos & (OPERAND_PLUS | OPERAND_MINUS)) != 0))
784 1.1 christos {
785 1.1 christos mod[j] |= 1 << regno;
786 1.1 christos if (flags & OPERAND_EVEN)
787 1.1 christos mod[j] |= 1 << (regno + 1);
788 1.1 christos }
789 1.1 christos }
790 1.1 christos else if (flags & OPERAND_ATMINUS)
791 1.1 christos {
792 1.1 christos /* SP implicitly used/modified. */
793 1.1 christos mod[j] |= 1 << 15;
794 1.1 christos }
795 1.1 christos }
796 1.1 christos
797 1.1 christos if (op->exec_type & WMEM)
798 1.1 christos mod[j] |= 1 << 20;
799 1.1 christos else if (op->exec_type & WF0)
800 1.1 christos mod[j] |= 1 << 22;
801 1.1 christos else if (op->exec_type & WCAR)
802 1.1 christos mod[j] |= 1 << 21;
803 1.1 christos }
804 1.1 christos
805 1.1 christos if ((mod[0] & mod[1]) == 0)
806 1.1 christos return;
807 1.1 christos else
808 1.1 christos {
809 1.1 christos unsigned long x;
810 1.1 christos x = mod[0] & mod[1];
811 1.1 christos
812 1.1 christos for (j = 0; j <= 15; j++)
813 1.1 christos if (x & (1 << j))
814 1.1 christos as_warn (_("resource conflict (R%d)"), j);
815 1.1 christos for (j = 16; j <= 17; j++)
816 1.1 christos if (x & (1 << j))
817 1.1 christos as_warn (_("resource conflict (A%d)"), j - 16);
818 1.1 christos if (x & (1 << 19))
819 1.1 christos as_warn (_("resource conflict (PSW)"));
820 1.1 christos if (x & (1 << 21))
821 1.1 christos as_warn (_("resource conflict (C flag)"));
822 1.1 christos if (x & (1 << 22))
823 1.1 christos as_warn (_("resource conflict (F flag)"));
824 1.1 christos }
825 1.1 christos }
826 1.1 christos
827 1.1 christos /* Check 2 instructions and determine if they can be safely
828 1.1 christos executed in parallel. Return 1 if they can be. */
829 1.1 christos
830 1.1 christos static int
831 1.1 christos parallel_ok (struct d10v_opcode *op1,
832 1.1 christos unsigned long insn1,
833 1.1 christos struct d10v_opcode *op2,
834 1.1 christos unsigned long insn2,
835 1.1 christos packing_type exec_type)
836 1.1 christos {
837 1.1 christos int i, j, flags, mask, shift, regno;
838 1.1 christos unsigned long ins, mod[2], used[2];
839 1.1 christos struct d10v_opcode *op;
840 1.1 christos
841 1.1 christos if ((op1->exec_type & SEQ) != 0 || (op2->exec_type & SEQ) != 0
842 1.1 christos || (op1->exec_type & PAR) == 0 || (op2->exec_type & PAR) == 0
843 1.1 christos || (op1->unit == BOTH) || (op2->unit == BOTH)
844 1.1 christos || (op1->unit == IU && op2->unit == IU)
845 1.1 christos || (op1->unit == MU && op2->unit == MU))
846 1.1 christos return 0;
847 1.1 christos
848 1.1 christos /* If this is auto parallelization, and the first instruction is a
849 1.1 christos branch or should not be packed, then don't parallelize. */
850 1.1 christos if (exec_type == PACK_UNSPEC
851 1.1 christos && (op1->exec_type & (ALONE | BRANCH)))
852 1.1 christos return 0;
853 1.1 christos
854 1.1 christos /* The idea here is to create two sets of bitmasks (mod and used)
855 1.1 christos which indicate which registers are modified or used by each
856 1.1 christos instruction. The operation can only be done in parallel if
857 1.1 christos instruction 1 and instruction 2 modify different registers, and
858 1.1 christos the first instruction does not modify registers that the second
859 1.1 christos is using (The second instruction can modify registers that the
860 1.1 christos first is using as they are only written back after the first
861 1.1 christos instruction has completed). Accesses to control registers, PSW,
862 1.1 christos and memory are treated as accesses to a single register. So if
863 1.1 christos both instructions write memory or if the first instruction writes
864 1.1 christos memory and the second reads, then they cannot be done in
865 1.1 christos parallel. Likewise, if the first instruction mucks with the psw
866 1.1 christos and the second reads the PSW (which includes C, F0, and F1), then
867 1.1 christos they cannot operate safely in parallel. */
868 1.1 christos
869 1.1 christos /* The bitmasks (mod and used) look like this (bit 31 = MSB).
870 1.1 christos r0-r15 0-15
871 1.1 christos a0-a1 16-17
872 1.1 christos cr (not psw) 18
873 1.1 christos psw 19
874 1.1 christos mem 20 */
875 1.1 christos
876 1.1 christos for (j = 0; j < 2; j++)
877 1.1 christos {
878 1.1 christos if (j == 0)
879 1.1 christos {
880 1.1 christos op = op1;
881 1.1 christos ins = insn1;
882 1.1 christos }
883 1.1 christos else
884 1.1 christos {
885 1.1 christos op = op2;
886 1.1 christos ins = insn2;
887 1.1 christos }
888 1.1 christos mod[j] = used[j] = 0;
889 1.1 christos if (op->exec_type & BRANCH_LINK)
890 1.1 christos mod[j] |= 1 << 13;
891 1.1 christos
892 1.1 christos for (i = 0; op->operands[i]; i++)
893 1.1 christos {
894 1.1 christos flags = d10v_operands[op->operands[i]].flags;
895 1.1 christos shift = d10v_operands[op->operands[i]].shift;
896 1.1 christos mask = 0x7FFFFFFF >> (31 - d10v_operands[op->operands[i]].bits);
897 1.1 christos if (flags & OPERAND_REG)
898 1.1 christos {
899 1.1 christos regno = (ins >> shift) & mask;
900 1.1 christos if (flags & (OPERAND_ACC0 | OPERAND_ACC1))
901 1.1 christos regno += 16;
902 1.1 christos else if (flags & OPERAND_CONTROL) /* mvtc or mvfc. */
903 1.1 christos {
904 1.1 christos if (regno == 0)
905 1.1 christos regno = 19;
906 1.1 christos else
907 1.1 christos regno = 18;
908 1.1 christos }
909 1.1 christos else if (flags & (OPERAND_FFLAG | OPERAND_CFLAG))
910 1.1 christos regno = 19;
911 1.1 christos
912 1.1 christos if (flags & OPERAND_DEST)
913 1.1 christos {
914 1.1 christos mod[j] |= 1 << regno;
915 1.1 christos if (flags & OPERAND_EVEN)
916 1.1 christos mod[j] |= 1 << (regno + 1);
917 1.1 christos }
918 1.1 christos else
919 1.1 christos {
920 1.1 christos used[j] |= 1 << regno;
921 1.1 christos if (flags & OPERAND_EVEN)
922 1.1 christos used[j] |= 1 << (regno + 1);
923 1.1 christos
924 1.1 christos /* Auto inc/dec also modifies the register. */
925 1.1 christos if (op->operands[i + 1] != 0
926 1.1 christos && (d10v_operands[op->operands[i + 1]].flags
927 1.1 christos & (OPERAND_PLUS | OPERAND_MINUS)) != 0)
928 1.1 christos mod[j] |= 1 << regno;
929 1.1 christos }
930 1.1 christos }
931 1.1 christos else if (flags & OPERAND_ATMINUS)
932 1.1 christos {
933 1.1 christos /* SP implicitly used/modified. */
934 1.1 christos mod[j] |= 1 << 15;
935 1.1 christos used[j] |= 1 << 15;
936 1.1 christos }
937 1.1 christos }
938 1.1 christos if (op->exec_type & RMEM)
939 1.1 christos used[j] |= 1 << 20;
940 1.1 christos else if (op->exec_type & WMEM)
941 1.1 christos mod[j] |= 1 << 20;
942 1.1 christos else if (op->exec_type & RF0)
943 1.1 christos used[j] |= 1 << 19;
944 1.1 christos else if (op->exec_type & WF0)
945 1.1 christos mod[j] |= 1 << 19;
946 1.1 christos else if (op->exec_type & WCAR)
947 1.1 christos mod[j] |= 1 << 19;
948 1.1 christos }
949 1.1 christos if ((mod[0] & mod[1]) == 0 && (mod[0] & used[1]) == 0)
950 1.1 christos return 1;
951 1.1 christos return 0;
952 1.1 christos }
953 1.1 christos
954 1.1 christos /* Expects two short instructions.
955 1.1 christos If possible, writes out both as a single packed instruction.
956 1.1 christos Otherwise, writes out the first one, packed with a NOP.
957 1.1 christos Returns number of instructions not written out. */
958 1.1 christos
959 1.1 christos static int
960 1.1 christos write_2_short (struct d10v_opcode *opcode1,
961 1.1 christos unsigned long insn1,
962 1.1 christos struct d10v_opcode *opcode2,
963 1.1 christos unsigned long insn2,
964 1.1 christos packing_type exec_type,
965 1.1 christos Fixups *fx)
966 1.1 christos {
967 1.1 christos unsigned long insn;
968 1.1 christos char *f;
969 1.1 christos int i, j, where;
970 1.1 christos
971 1.1 christos if ((exec_type != PACK_PARALLEL)
972 1.1 christos && ((opcode1->exec_type & PARONLY) || (opcode2->exec_type & PARONLY)))
973 1.1 christos as_fatal (_("Instruction must be executed in parallel"));
974 1.1 christos
975 1.1 christos if ((opcode1->format & LONG_OPCODE) || (opcode2->format & LONG_OPCODE))
976 1.1 christos as_fatal (_("Long instructions may not be combined."));
977 1.1 christos
978 1.1 christos switch (exec_type)
979 1.1 christos {
980 1.1 christos case PACK_UNSPEC: /* Order not specified. */
981 1.1 christos if (opcode1->exec_type & ALONE)
982 1.1 christos {
983 1.1 christos /* Case of a short branch on a separate GAS line. Pack with NOP. */
984 1.1 christos write_1_short (opcode1, insn1, fx->next);
985 1.1 christos return 1;
986 1.1 christos }
987 1.1 christos if (Optimizing
988 1.1 christos && parallel_ok (opcode1, insn1, opcode2, insn2, exec_type))
989 1.1 christos {
990 1.1 christos /* Parallel. */
991 1.1 christos if (opcode1->unit == IU)
992 1.1 christos insn = FM00 | (insn2 << 15) | insn1;
993 1.1 christos else if (opcode2->unit == MU)
994 1.1 christos insn = FM00 | (insn2 << 15) | insn1;
995 1.1 christos else
996 1.1 christos insn = FM00 | (insn1 << 15) | insn2;
997 1.1 christos }
998 1.1 christos else if (opcode1->unit == IU)
999 1.1 christos /* Reverse sequential with IU opcode1 on right and done first. */
1000 1.1 christos insn = FM10 | (insn2 << 15) | insn1;
1001 1.1 christos else
1002 1.1 christos /* Sequential with non-IU opcode1 on left and done first. */
1003 1.1 christos insn = FM01 | (insn1 << 15) | insn2;
1004 1.1 christos break;
1005 1.1 christos
1006 1.1 christos case PACK_PARALLEL:
1007 1.1 christos if (opcode1->exec_type & SEQ || opcode2->exec_type & SEQ)
1008 1.1 christos as_fatal
1009 1.1 christos (_("One of these instructions may not be executed in parallel."));
1010 1.1 christos if (opcode1->unit == IU)
1011 1.1 christos {
1012 1.1 christos if (opcode2->unit == IU)
1013 1.1 christos as_fatal (_("Two IU instructions may not be executed in parallel"));
1014 1.1 christos if (!flag_warn_suppress_instructionswap)
1015 1.1 christos as_warn (_("Swapping instruction order"));
1016 1.1 christos insn = FM00 | (insn2 << 15) | insn1;
1017 1.1 christos }
1018 1.1 christos else if (opcode2->unit == MU)
1019 1.1 christos {
1020 1.1 christos if (opcode1->unit == MU)
1021 1.1 christos as_fatal (_("Two MU instructions may not be executed in parallel"));
1022 1.1 christos if (!flag_warn_suppress_instructionswap)
1023 1.1 christos as_warn (_("Swapping instruction order"));
1024 1.1 christos insn = FM00 | (insn2 << 15) | insn1;
1025 1.1 christos }
1026 1.1 christos else
1027 1.1 christos insn = FM00 | (insn1 << 15) | insn2;
1028 1.1 christos check_resource_conflict (opcode1, insn1, opcode2, insn2);
1029 1.1 christos break;
1030 1.1 christos
1031 1.1 christos case PACK_LEFT_RIGHT:
1032 1.1 christos if (opcode1->unit != IU)
1033 1.1 christos insn = FM01 | (insn1 << 15) | insn2;
1034 1.1 christos else if (opcode2->unit == MU || opcode2->unit == EITHER)
1035 1.1 christos {
1036 1.1 christos if (!flag_warn_suppress_instructionswap)
1037 1.1 christos as_warn (_("Swapping instruction order"));
1038 1.1 christos insn = FM10 | (insn2 << 15) | insn1;
1039 1.1 christos }
1040 1.1 christos else
1041 1.1 christos as_fatal (_("IU instruction may not be in the left container"));
1042 1.1 christos if (opcode1->exec_type & ALONE)
1043 1.1 christos as_warn (_("Instruction in R container is squashed by flow control instruction in L container."));
1044 1.1 christos break;
1045 1.1 christos
1046 1.1 christos case PACK_RIGHT_LEFT:
1047 1.1 christos if (opcode2->unit != MU)
1048 1.1 christos insn = FM10 | (insn1 << 15) | insn2;
1049 1.1 christos else if (opcode1->unit == IU || opcode1->unit == EITHER)
1050 1.1 christos {
1051 1.1 christos if (!flag_warn_suppress_instructionswap)
1052 1.1 christos as_warn (_("Swapping instruction order"));
1053 1.1 christos insn = FM01 | (insn2 << 15) | insn1;
1054 1.1 christos }
1055 1.1 christos else
1056 1.1 christos as_fatal (_("MU instruction may not be in the right container"));
1057 1.1 christos if (opcode2->exec_type & ALONE)
1058 1.1 christos as_warn (_("Instruction in R container is squashed by flow control instruction in L container."));
1059 1.1 christos break;
1060 1.1 christos
1061 1.1 christos default:
1062 1.1 christos as_fatal (_("unknown execution type passed to write_2_short()"));
1063 1.1 christos }
1064 1.1 christos
1065 1.1 christos f = frag_more (4);
1066 1.1 christos dwarf2_emit_insn (4);
1067 1.1 christos number_to_chars_bigendian (f, insn, 4);
1068 1.1 christos
1069 1.1 christos /* Process fixup chains. fx refers to insn2 when j == 0, and to
1070 1.1 christos insn1 when j == 1. Yes, it's reversed. */
1071 1.1 christos
1072 1.1 christos for (j = 0; j < 2; j++)
1073 1.1 christos {
1074 1.1 christos for (i = 0; i < fx->fc; i++)
1075 1.1 christos {
1076 1.1 christos if (fx->fix[i].reloc)
1077 1.1 christos {
1078 1.1 christos where = f - frag_now->fr_literal;
1079 1.1 christos if (fx->fix[i].size == 2)
1080 1.1 christos where += 2;
1081 1.1 christos
1082 1.1 christos if (fx->fix[i].reloc == BFD_RELOC_D10V_10_PCREL_R
1083 1.1 christos /* A BFD_RELOC_D10V_10_PCREL_R relocation applied to
1084 1.1 christos the instruction in the L container has to be
1085 1.1 christos adjusted to BDF_RELOC_D10V_10_PCREL_L. When
1086 1.1 christos j==0, we're processing insn2's operands, so we
1087 1.1 christos want to mark the operand if insn2 is *not* in the
1088 1.1 christos R container. When j==1, we're processing insn1's
1089 1.1 christos operands, so we want to mark the operand if insn2
1090 1.1 christos *is* in the R container. Note that, if two
1091 1.1 christos instructions are identical, we're never going to
1092 1.1 christos swap them, so the test is safe. */
1093 1.1 christos && j == ((insn & 0x7fff) == insn2))
1094 1.1 christos fx->fix[i].operand |= 1024;
1095 1.1 christos
1096 1.1 christos if (fx->fix[i].reloc == BFD_RELOC_D10V_18)
1097 1.1 christos fx->fix[i].operand |= 4096;
1098 1.1 christos
1099 1.1 christos fix_new_exp (frag_now,
1100 1.1 christos where,
1101 1.1 christos fx->fix[i].size,
1102 1.1 christos &(fx->fix[i].exp),
1103 1.1 christos fx->fix[i].pcrel,
1104 1.1 christos fx->fix[i].operand|2048);
1105 1.1 christos }
1106 1.1 christos }
1107 1.1 christos fx->fc = 0;
1108 1.1 christos fx = fx->next;
1109 1.1 christos }
1110 1.1 christos return 0;
1111 1.1 christos }
1112 1.1 christos
1113 1.1 christos /* This is the main entry point for the machine-dependent assembler.
1114 1.1 christos str points to a machine-dependent instruction. This function is
1115 1.1 christos supposed to emit the frags/bytes it assembles to. For the D10V, it
1116 1.1 christos mostly handles the special VLIW parsing and packing and leaves the
1117 1.1 christos difficult stuff to do_assemble(). */
1118 1.1 christos
1119 1.1 christos static unsigned long prev_insn;
1120 1.1 christos static struct d10v_opcode *prev_opcode = 0;
1121 1.1 christos static subsegT prev_subseg;
1122 1.1 christos static segT prev_seg = 0;;
1123 1.1 christos
1124 1.1 christos /* Find the symbol which has the same name as the register in exp. */
1125 1.1 christos
1126 1.1 christos static symbolS *
1127 1.1 christos find_symbol_matching_register (expressionS *exp)
1128 1.1 christos {
1129 1.1 christos int i;
1130 1.1 christos
1131 1.1 christos if (exp->X_op != O_register)
1132 1.1 christos return NULL;
1133 1.1 christos
1134 1.1 christos /* Find the name of the register. */
1135 1.1 christos for (i = d10v_reg_name_cnt (); i--;)
1136 1.1 christos if (d10v_predefined_registers[i].value == exp->X_add_number)
1137 1.1 christos break;
1138 1.1 christos
1139 1.1 christos if (i < 0)
1140 1.1 christos abort ();
1141 1.1 christos
1142 1.1 christos /* Now see if a symbol has been defined with the same name. */
1143 1.1 christos return symbol_find (d10v_predefined_registers[i].name);
1144 1.1 christos }
1145 1.1 christos
1146 1.1 christos /* Get a pointer to an entry in the opcode table.
1147 1.1 christos The function must look at all opcodes with the same name and use
1148 1.1 christos the operands to choose the correct opcode. */
1149 1.1 christos
1150 1.1 christos static struct d10v_opcode *
1151 1.1 christos find_opcode (struct d10v_opcode *opcode, expressionS myops[])
1152 1.1 christos {
1153 1.1 christos int i, match;
1154 1.1 christos struct d10v_opcode *next_opcode;
1155 1.1 christos
1156 1.1 christos /* Get all the operands and save them as expressions. */
1157 1.1 christos get_operands (myops);
1158 1.1 christos
1159 1.1 christos /* Now see if the operand is a fake. If so, find the correct size
1160 1.1 christos instruction, if possible. */
1161 1.1 christos if (opcode->format == OPCODE_FAKE)
1162 1.1 christos {
1163 1.1 christos int opnum = opcode->operands[0];
1164 1.1 christos int flags;
1165 1.1 christos
1166 1.1 christos if (myops[opnum].X_op == O_register)
1167 1.1 christos {
1168 1.1 christos myops[opnum].X_op = O_symbol;
1169 1.1 christos myops[opnum].X_add_symbol =
1170 1.1 christos symbol_find_or_make ((char *) myops[opnum].X_op_symbol);
1171 1.1 christos myops[opnum].X_add_number = 0;
1172 1.1 christos myops[opnum].X_op_symbol = NULL;
1173 1.1 christos }
1174 1.1 christos
1175 1.1 christos next_opcode = opcode + 1;
1176 1.1 christos
1177 1.1 christos /* If the first operand is supposed to be a register, make sure
1178 1.1 christos we got a valid one. */
1179 1.1 christos flags = d10v_operands[next_opcode->operands[0]].flags;
1180 1.1 christos if (flags & OPERAND_REG)
1181 1.1 christos {
1182 1.1 christos int X_op = myops[0].X_op;
1183 1.1 christos int num = myops[0].X_add_number;
1184 1.1 christos
1185 1.1 christos if (X_op != O_register
1186 1.1 christos || (num & ~flags
1187 1.1 christos & (OPERAND_GPR | OPERAND_ACC0 | OPERAND_ACC1
1188 1.1 christos | OPERAND_FFLAG | OPERAND_CFLAG | OPERAND_CONTROL))
1189 1.1 christos || ((flags & OPERAND_SP) && ! (num & OPERAND_SP)))
1190 1.1 christos {
1191 1.1 christos as_bad (_("bad opcode or operands"));
1192 1.1 christos return 0;
1193 1.1 christos }
1194 1.1 christos }
1195 1.1 christos
1196 1.1 christos if (myops[opnum].X_op == O_constant
1197 1.1 christos || (myops[opnum].X_op == O_symbol
1198 1.1 christos && S_IS_DEFINED (myops[opnum].X_add_symbol)
1199 1.1 christos && (S_GET_SEGMENT (myops[opnum].X_add_symbol) == now_seg)))
1200 1.1 christos {
1201 1.1 christos for (i = 0; opcode->operands[i + 1]; i++)
1202 1.1 christos {
1203 1.1 christos int bits = d10v_operands[next_opcode->operands[opnum]].bits;
1204 1.1 christos
1205 1.1 christos flags = d10v_operands[next_opcode->operands[opnum]].flags;
1206 1.1 christos
1207 1.1 christos if (flags & OPERAND_ADDR)
1208 1.1 christos bits += 2;
1209 1.1 christos
1210 1.1 christos if (myops[opnum].X_op == O_constant)
1211 1.1 christos {
1212 1.1 christos if (!check_range (myops[opnum].X_add_number, bits, flags))
1213 1.1 christos break;
1214 1.1 christos }
1215 1.1 christos else
1216 1.1 christos {
1217 1.1 christos fragS *sym_frag;
1218 1.1 christos fragS *f;
1219 1.1 christos unsigned long current_position;
1220 1.1 christos unsigned long symbol_position;
1221 1.1 christos unsigned long value;
1222 1.1 christos bfd_boolean found_symbol;
1223 1.1 christos
1224 1.1 christos /* Calculate the address of the current instruction
1225 1.1 christos and the address of the symbol. Do this by summing
1226 1.1 christos the offsets of previous frags until we reach the
1227 1.1 christos frag containing the symbol, and the current frag. */
1228 1.1 christos sym_frag = symbol_get_frag (myops[opnum].X_add_symbol);
1229 1.1 christos found_symbol = FALSE;
1230 1.1 christos
1231 1.1 christos current_position =
1232 1.1 christos obstack_next_free (&frchain_now->frch_obstack)
1233 1.1 christos - frag_now->fr_literal;
1234 1.1 christos symbol_position = S_GET_VALUE (myops[opnum].X_add_symbol);
1235 1.1 christos
1236 1.1 christos for (f = frchain_now->frch_root; f; f = f->fr_next)
1237 1.1 christos {
1238 1.1 christos current_position += f->fr_fix + f->fr_offset;
1239 1.1 christos
1240 1.1 christos if (f == sym_frag)
1241 1.1 christos found_symbol = TRUE;
1242 1.1 christos
1243 1.1 christos if (! found_symbol)
1244 1.1 christos symbol_position += f->fr_fix + f->fr_offset;
1245 1.1 christos }
1246 1.1 christos
1247 1.1 christos value = symbol_position;
1248 1.1 christos
1249 1.1 christos if (flags & OPERAND_ADDR)
1250 1.1 christos value -= current_position;
1251 1.1 christos
1252 1.1 christos if (AT_WORD_P (&myops[opnum]))
1253 1.1 christos {
1254 1.1 christos if (bits > 4)
1255 1.1 christos {
1256 1.1 christos bits += 2;
1257 1.1 christos if (!check_range (value, bits, flags))
1258 1.1 christos break;
1259 1.1 christos }
1260 1.1 christos }
1261 1.1 christos else if (!check_range (value, bits, flags))
1262 1.1 christos break;
1263 1.1 christos }
1264 1.1 christos next_opcode++;
1265 1.1 christos }
1266 1.1 christos
1267 1.1 christos if (opcode->operands [i + 1] == 0)
1268 1.1 christos as_fatal (_("value out of range"));
1269 1.1 christos else
1270 1.1 christos opcode = next_opcode;
1271 1.1 christos }
1272 1.1 christos else
1273 1.1 christos /* Not a constant, so use a long instruction. */
1274 1.1 christos opcode += 2;
1275 1.1 christos }
1276 1.1 christos
1277 1.1 christos match = 0;
1278 1.1 christos
1279 1.1 christos /* Now search the opcode table table for one with operands
1280 1.1 christos that matches what we've got. */
1281 1.1 christos while (!match)
1282 1.1 christos {
1283 1.1 christos match = 1;
1284 1.1 christos for (i = 0; opcode->operands[i]; i++)
1285 1.1 christos {
1286 1.1 christos int flags = d10v_operands[opcode->operands[i]].flags;
1287 1.1 christos int X_op = myops[i].X_op;
1288 1.1 christos int num = myops[i].X_add_number;
1289 1.1 christos
1290 1.1 christos if (X_op == 0)
1291 1.1 christos {
1292 1.1 christos match = 0;
1293 1.1 christos break;
1294 1.1 christos }
1295 1.1 christos
1296 1.1 christos if (flags & OPERAND_REG)
1297 1.1 christos {
1298 1.1 christos if ((X_op != O_register)
1299 1.1 christos || (num & ~flags
1300 1.1 christos & (OPERAND_GPR | OPERAND_ACC0 | OPERAND_ACC1
1301 1.1 christos | OPERAND_FFLAG | OPERAND_CFLAG
1302 1.1 christos | OPERAND_CONTROL))
1303 1.1 christos || ((flags & OPERAND_SP) && ! (num & OPERAND_SP)))
1304 1.1 christos {
1305 1.1 christos match = 0;
1306 1.1 christos break;
1307 1.1 christos }
1308 1.1 christos }
1309 1.1 christos
1310 1.1 christos if (((flags & OPERAND_MINUS) && ((X_op != O_absent) || (num != OPERAND_MINUS))) ||
1311 1.1 christos ((flags & OPERAND_PLUS) && ((X_op != O_absent) || (num != OPERAND_PLUS))) ||
1312 1.1 christos ((flags & OPERAND_ATMINUS) && ((X_op != O_absent) || (num != OPERAND_ATMINUS))) ||
1313 1.1 christos ((flags & OPERAND_ATPAR) && ((X_op != O_absent) || (num != OPERAND_ATPAR))) ||
1314 1.1 christos ((flags & OPERAND_ATSIGN) && ((X_op != O_absent) || ((num != OPERAND_ATSIGN) && (num != OPERAND_ATPAR)))))
1315 1.1 christos {
1316 1.1 christos match = 0;
1317 1.1 christos break;
1318 1.1 christos }
1319 1.1 christos
1320 1.1 christos /* Unfortunately, for the indirect operand in instructions such
1321 1.1 christos as ``ldb r1, @(c,r14)'' this function can be passed
1322 1.1 christos X_op == O_register (because 'c' is a valid register name).
1323 1.1 christos However we cannot just ignore the case when X_op == O_register
1324 1.1 christos but flags & OPERAND_REG is null, so we check to see if a symbol
1325 1.1 christos of the same name as the register exists. If the symbol does
1326 1.1 christos exist, then the parser was unable to distinguish the two cases
1327 1.1 christos and we fix things here. (Ref: PR14826) */
1328 1.1 christos
1329 1.1 christos if (!(flags & OPERAND_REG) && (X_op == O_register))
1330 1.1 christos {
1331 1.1 christos symbolS * sym;
1332 1.1 christos
1333 1.1 christos sym = find_symbol_matching_register (& myops[i]);
1334 1.1 christos
1335 1.1 christos if (sym != NULL)
1336 1.1 christos {
1337 1.1 christos myops[i].X_op = X_op = O_symbol;
1338 1.1 christos myops[i].X_add_symbol = sym;
1339 1.1 christos }
1340 1.1 christos else
1341 1.1 christos as_bad
1342 1.1 christos (_("illegal operand - register name found where none expected"));
1343 1.1 christos }
1344 1.1 christos }
1345 1.1 christos
1346 1.1 christos /* We're only done if the operands matched so far AND there
1347 1.1 christos are no more to check. */
1348 1.1 christos if (match && myops[i].X_op == 0)
1349 1.1 christos break;
1350 1.1 christos else
1351 1.1 christos match = 0;
1352 1.1 christos
1353 1.1 christos next_opcode = opcode + 1;
1354 1.1 christos
1355 1.1 christos if (next_opcode->opcode == 0)
1356 1.1 christos break;
1357 1.1 christos
1358 1.1 christos if (strcmp (next_opcode->name, opcode->name))
1359 1.1 christos break;
1360 1.1 christos
1361 1.1 christos opcode = next_opcode;
1362 1.1 christos }
1363 1.1 christos
1364 1.1 christos if (!match)
1365 1.1 christos {
1366 1.1 christos as_bad (_("bad opcode or operands"));
1367 1.1 christos return 0;
1368 1.1 christos }
1369 1.1 christos
1370 1.1 christos /* Check that all registers that are required to be even are.
1371 1.1 christos Also, if any operands were marked as registers, but were really symbols,
1372 1.1 christos fix that here. */
1373 1.1 christos for (i = 0; opcode->operands[i]; i++)
1374 1.1 christos {
1375 1.1 christos if ((d10v_operands[opcode->operands[i]].flags & OPERAND_EVEN) &&
1376 1.1 christos (myops[i].X_add_number & 1))
1377 1.1 christos as_fatal (_("Register number must be EVEN"));
1378 1.1 christos if ((d10v_operands[opcode->operands[i]].flags & OPERAND_NOSP)
1379 1.1 christos && (myops[i].X_add_number & OPERAND_SP))
1380 1.1 christos as_bad (_("Unsupported use of sp"));
1381 1.1 christos if (myops[i].X_op == O_register)
1382 1.1 christos {
1383 1.1 christos if (!(d10v_operands[opcode->operands[i]].flags & OPERAND_REG))
1384 1.1 christos {
1385 1.1 christos myops[i].X_op = O_symbol;
1386 1.1 christos myops[i].X_add_symbol =
1387 1.1 christos symbol_find_or_make ((char *) myops[i].X_op_symbol);
1388 1.1 christos myops[i].X_add_number = 0;
1389 1.1 christos myops[i].X_op_symbol = NULL;
1390 1.1 christos }
1391 1.1 christos }
1392 1.1 christos if ((d10v_operands[opcode->operands[i]].flags & OPERAND_CONTROL)
1393 1.1 christos && (myops[i].X_add_number == OPERAND_CONTROL + 4
1394 1.1 christos || myops[i].X_add_number == OPERAND_CONTROL + 5
1395 1.1 christos || myops[i].X_add_number == OPERAND_CONTROL + 6
1396 1.1 christos || myops[i].X_add_number == OPERAND_CONTROL + 12
1397 1.1 christos || myops[i].X_add_number == OPERAND_CONTROL + 13
1398 1.1 christos || myops[i].X_add_number == OPERAND_CONTROL + 15))
1399 1.1 christos as_warn (_("cr%ld is a reserved control register"),
1400 1.1 christos myops[i].X_add_number - OPERAND_CONTROL);
1401 1.1 christos }
1402 1.1 christos return opcode;
1403 1.1 christos }
1404 1.1 christos
1405 1.1 christos /* Assemble a single instruction.
1406 1.1 christos Return an opcode, or -1 (an invalid opcode) on error. */
1407 1.1 christos
1408 1.1 christos static unsigned long
1409 1.1 christos do_assemble (char *str, struct d10v_opcode **opcode)
1410 1.1 christos {
1411 1.1 christos unsigned char *op_start, *op_end;
1412 1.1 christos char *save;
1413 1.1 christos char name[20];
1414 1.1 christos int nlen = 0;
1415 1.1 christos expressionS myops[6];
1416 1.1 christos
1417 1.1 christos /* Drop leading whitespace. */
1418 1.1 christos while (*str == ' ')
1419 1.1 christos str++;
1420 1.1 christos
1421 1.1 christos /* Find the opcode end. */
1422 1.1 christos for (op_start = op_end = (unsigned char *) str;
1423 1.1 christos *op_end && !is_end_of_line[*op_end] && *op_end != ' ';
1424 1.1 christos op_end++)
1425 1.1 christos {
1426 1.1 christos name[nlen] = TOLOWER (op_start[nlen]);
1427 1.1 christos nlen++;
1428 1.1 christos if (nlen == sizeof (name) - 1)
1429 1.1 christos break;
1430 1.1 christos }
1431 1.1 christos name[nlen] = 0;
1432 1.1 christos
1433 1.1 christos if (nlen == 0)
1434 1.1 christos return -1;
1435 1.1 christos
1436 1.1 christos /* Find the first opcode with the proper name. */
1437 1.1 christos *opcode = (struct d10v_opcode *) hash_find (d10v_hash, name);
1438 1.1 christos if (*opcode == NULL)
1439 1.1 christos return -1;
1440 1.1 christos
1441 1.1 christos save = input_line_pointer;
1442 1.1 christos input_line_pointer = (char *) op_end;
1443 1.1 christos *opcode = find_opcode (*opcode, myops);
1444 1.1 christos if (*opcode == 0)
1445 1.1 christos return -1;
1446 1.1 christos input_line_pointer = save;
1447 1.1 christos
1448 1.1 christos return build_insn ((*opcode), myops, 0);
1449 1.1 christos }
1450 1.1 christos
1451 1.1 christos /* If while processing a fixup, a reloc really needs to be created.
1452 1.1 christos Then it is done here. */
1453 1.1 christos
1454 1.1 christos arelent *
1455 1.1 christos tc_gen_reloc (asection *seg ATTRIBUTE_UNUSED, fixS *fixp)
1456 1.1 christos {
1457 1.1 christos arelent *reloc;
1458 1.1 christos reloc = xmalloc (sizeof (arelent));
1459 1.1 christos reloc->sym_ptr_ptr = xmalloc (sizeof (asymbol *));
1460 1.1 christos *reloc->sym_ptr_ptr = symbol_get_bfdsym (fixp->fx_addsy);
1461 1.1 christos reloc->address = fixp->fx_frag->fr_address + fixp->fx_where;
1462 1.1 christos reloc->howto = bfd_reloc_type_lookup (stdoutput, fixp->fx_r_type);
1463 1.1 christos if (reloc->howto == (reloc_howto_type *) NULL)
1464 1.1 christos {
1465 1.1 christos as_bad_where (fixp->fx_file, fixp->fx_line,
1466 1.1 christos _("reloc %d not supported by object file format"),
1467 1.1 christos (int) fixp->fx_r_type);
1468 1.1 christos return NULL;
1469 1.1 christos }
1470 1.1 christos
1471 1.1 christos if (fixp->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
1472 1.1 christos reloc->address = fixp->fx_offset;
1473 1.1 christos
1474 1.1 christos reloc->addend = 0;
1475 1.1 christos
1476 1.1 christos return reloc;
1477 1.1 christos }
1478 1.1 christos
1479 1.1 christos int
1480 1.1 christos md_estimate_size_before_relax (fragS *fragp ATTRIBUTE_UNUSED,
1481 1.1 christos asection *seg ATTRIBUTE_UNUSED)
1482 1.1 christos {
1483 1.1 christos abort ();
1484 1.1 christos return 0;
1485 1.1 christos }
1486 1.1 christos
1487 1.1 christos long
1488 1.1 christos md_pcrel_from_section (fixS *fixp, segT sec)
1489 1.1 christos {
1490 1.1 christos if (fixp->fx_addsy != (symbolS *) NULL
1491 1.1 christos && (!S_IS_DEFINED (fixp->fx_addsy)
1492 1.1 christos || (S_GET_SEGMENT (fixp->fx_addsy) != sec)))
1493 1.1 christos return 0;
1494 1.1 christos return fixp->fx_frag->fr_address + fixp->fx_where;
1495 1.1 christos }
1496 1.1 christos
1497 1.1 christos void
1498 1.1 christos md_apply_fix (fixS *fixP, valueT *valP, segT seg ATTRIBUTE_UNUSED)
1499 1.1 christos {
1500 1.1 christos char *where;
1501 1.1 christos unsigned long insn;
1502 1.1 christos long value = *valP;
1503 1.1 christos int op_type;
1504 1.1 christos int left = 0;
1505 1.1 christos
1506 1.1 christos if (fixP->fx_addsy == (symbolS *) NULL)
1507 1.1 christos fixP->fx_done = 1;
1508 1.1 christos
1509 1.1 christos /* We don't actually support subtracting a symbol. */
1510 1.1 christos if (fixP->fx_subsy != (symbolS *) NULL)
1511 1.1 christos as_bad_where (fixP->fx_file, fixP->fx_line, _("expression too complex"));
1512 1.1 christos
1513 1.1 christos op_type = fixP->fx_r_type;
1514 1.1 christos if (op_type & 2048)
1515 1.1 christos {
1516 1.1 christos op_type -= 2048;
1517 1.1 christos if (op_type & 1024)
1518 1.1 christos {
1519 1.1 christos op_type -= 1024;
1520 1.1 christos fixP->fx_r_type = BFD_RELOC_D10V_10_PCREL_L;
1521 1.1 christos left = 1;
1522 1.1 christos }
1523 1.1 christos else if (op_type & 4096)
1524 1.1 christos {
1525 1.1 christos op_type -= 4096;
1526 1.1 christos fixP->fx_r_type = BFD_RELOC_D10V_18;
1527 1.1 christos }
1528 1.1 christos else
1529 1.1 christos fixP->fx_r_type =
1530 1.1 christos get_reloc ((struct d10v_operand *) &d10v_operands[op_type]);
1531 1.1 christos }
1532 1.1 christos
1533 1.1 christos /* Fetch the instruction, insert the fully resolved operand
1534 1.1 christos value, and stuff the instruction back again. */
1535 1.1 christos where = fixP->fx_frag->fr_literal + fixP->fx_where;
1536 1.1 christos insn = bfd_getb32 ((unsigned char *) where);
1537 1.1 christos
1538 1.1 christos switch (fixP->fx_r_type)
1539 1.1 christos {
1540 1.1 christos case BFD_RELOC_D10V_10_PCREL_L:
1541 1.1 christos case BFD_RELOC_D10V_10_PCREL_R:
1542 1.1 christos case BFD_RELOC_D10V_18_PCREL:
1543 1.1 christos /* If the fix is relative to a global symbol, not a section
1544 1.1 christos symbol, then ignore the offset.
1545 1.1 christos XXX - Do we have to worry about branches to a symbol + offset ? */
1546 1.1 christos if (fixP->fx_addsy != NULL
1547 1.1 christos && S_IS_EXTERNAL (fixP->fx_addsy) )
1548 1.1 christos {
1549 1.1 christos segT fseg = S_GET_SEGMENT (fixP->fx_addsy);
1550 1.1 christos segment_info_type *segf = seg_info(fseg);
1551 1.1 christos
1552 1.1 christos if ( segf && segf->sym != fixP->fx_addsy)
1553 1.1 christos value = 0;
1554 1.1 christos }
1555 1.1 christos /* Drop through. */
1556 1.1 christos case BFD_RELOC_D10V_18:
1557 1.1 christos /* Instruction addresses are always right-shifted by 2. */
1558 1.1 christos value >>= AT_WORD_RIGHT_SHIFT;
1559 1.1 christos if (fixP->fx_size == 2)
1560 1.1 christos bfd_putb16 ((bfd_vma) value, (unsigned char *) where);
1561 1.1 christos else
1562 1.1 christos {
1563 1.1 christos struct d10v_opcode *rep, *repi;
1564 1.1 christos
1565 1.1 christos rep = (struct d10v_opcode *) hash_find (d10v_hash, "rep");
1566 1.1 christos repi = (struct d10v_opcode *) hash_find (d10v_hash, "repi");
1567 1.1 christos if ((insn & FM11) == FM11
1568 1.1 christos && ((repi != NULL
1569 1.1 christos && (insn & repi->mask) == (unsigned) repi->opcode)
1570 1.1 christos || (rep != NULL
1571 1.1 christos && (insn & rep->mask) == (unsigned) rep->opcode))
1572 1.1 christos && value < 4)
1573 1.1 christos as_fatal
1574 1.1 christos (_("line %d: rep or repi must include at least 4 instructions"),
1575 1.1 christos fixP->fx_line);
1576 1.1 christos insn =
1577 1.1 christos d10v_insert_operand (insn, op_type, (offsetT) value, left, fixP);
1578 1.1 christos bfd_putb32 ((bfd_vma) insn, (unsigned char *) where);
1579 1.1 christos }
1580 1.1 christos break;
1581 1.1 christos case BFD_RELOC_32:
1582 1.1 christos bfd_putb32 ((bfd_vma) value, (unsigned char *) where);
1583 1.1 christos break;
1584 1.1 christos case BFD_RELOC_16:
1585 1.1 christos bfd_putb16 ((bfd_vma) value, (unsigned char *) where);
1586 1.1 christos break;
1587 1.1 christos
1588 1.1 christos case BFD_RELOC_VTABLE_INHERIT:
1589 1.1 christos case BFD_RELOC_VTABLE_ENTRY:
1590 1.1 christos fixP->fx_done = 0;
1591 1.1 christos return;
1592 1.1 christos
1593 1.1 christos default:
1594 1.1 christos as_fatal (_("line %d: unknown relocation type: 0x%x"),
1595 1.1 christos fixP->fx_line, fixP->fx_r_type);
1596 1.1 christos }
1597 1.1 christos }
1598 1.1 christos
1599 1.1 christos /* d10v_cleanup() is called after the assembler has finished parsing
1600 1.1 christos the input file, when a label is read from the input file, or when a
1601 1.1 christos stab directive is output. Because the D10V assembler sometimes
1602 1.1 christos saves short instructions to see if it can package them with the
1603 1.1 christos next instruction, there may be a short instruction that still needs
1604 1.1 christos to be written.
1605 1.1 christos
1606 1.1 christos NOTE: accesses a global, etype.
1607 1.1 christos NOTE: invoked by various macros such as md_cleanup: see. */
1608 1.1 christos
1609 1.1 christos int
1610 1.1 christos d10v_cleanup (void)
1611 1.1 christos {
1612 1.1 christos segT seg;
1613 1.1 christos subsegT subseg;
1614 1.1 christos
1615 1.1 christos /* If cleanup was invoked because the assembler encountered, e.g., a
1616 1.1 christos user label, we write out the pending instruction, if any. If it
1617 1.1 christos was invoked because the assembler is outputting a piece of line
1618 1.1 christos debugging information, though, we write out the pending
1619 1.1 christos instruction only if the --no-gstabs-packing command line switch
1620 1.1 christos has been specified. */
1621 1.1 christos if (prev_opcode
1622 1.1 christos && etype == PACK_UNSPEC
1623 1.1 christos && (! outputting_stabs_line_debug || ! flag_allow_gstabs_packing))
1624 1.1 christos {
1625 1.1 christos seg = now_seg;
1626 1.1 christos subseg = now_subseg;
1627 1.1 christos
1628 1.1 christos if (prev_seg)
1629 1.1 christos subseg_set (prev_seg, prev_subseg);
1630 1.1 christos
1631 1.1 christos write_1_short (prev_opcode, prev_insn, fixups->next);
1632 1.1 christos subseg_set (seg, subseg);
1633 1.1 christos prev_opcode = NULL;
1634 1.1 christos }
1635 1.1 christos return 1;
1636 1.1 christos }
1637 1.1 christos
1638 1.1 christos void
1639 1.1 christos d10v_frob_label (symbolS *lab)
1640 1.1 christos {
1641 1.1 christos d10v_cleanup ();
1642 1.1 christos symbol_set_frag (lab, frag_now);
1643 1.1 christos S_SET_VALUE (lab, (valueT) frag_now_fix ());
1644 1.1 christos dwarf2_emit_label (lab);
1645 1.1 christos }
1646 1.1 christos
1647 1.1 christos /* Like normal .word, except support @word.
1648 1.1 christos Clobbers input_line_pointer, checks end-of-line. */
1649 1.1 christos
1650 1.1 christos static void
1651 1.1 christos d10v_dot_word (int dummy ATTRIBUTE_UNUSED)
1652 1.1 christos {
1653 1.1 christos expressionS exp;
1654 1.1 christos char *p;
1655 1.1 christos
1656 1.1 christos if (is_it_end_of_statement ())
1657 1.1 christos {
1658 1.1 christos demand_empty_rest_of_line ();
1659 1.1 christos return;
1660 1.1 christos }
1661 1.1 christos
1662 1.1 christos do
1663 1.1 christos {
1664 1.1 christos expression (&exp);
1665 1.1 christos if (!strncasecmp (input_line_pointer, "@word", 5))
1666 1.1 christos {
1667 1.1 christos exp.X_add_number = 0;
1668 1.1 christos input_line_pointer += 5;
1669 1.1 christos
1670 1.1 christos p = frag_more (2);
1671 1.1 christos fix_new_exp (frag_now, p - frag_now->fr_literal, 2,
1672 1.1 christos &exp, 0, BFD_RELOC_D10V_18);
1673 1.1 christos }
1674 1.1 christos else
1675 1.1 christos emit_expr (&exp, 2);
1676 1.1 christos }
1677 1.1 christos while (*input_line_pointer++ == ',');
1678 1.1 christos
1679 1.1 christos input_line_pointer--; /* Put terminator back into stream. */
1680 1.1 christos demand_empty_rest_of_line ();
1681 1.1 christos }
1682 1.1 christos
1683 1.1 christos /* Mitsubishi asked that we support some old syntax that apparently
1684 1.1 christos had immediate operands starting with '#'. This is in some of their
1685 1.1 christos sample code but is not documented (although it appears in some
1686 1.1 christos examples in their assembler manual). For now, we'll solve this
1687 1.1 christos compatibility problem by simply ignoring any '#' at the beginning
1688 1.1 christos of an operand. */
1689 1.1 christos
1690 1.1 christos /* Operands that begin with '#' should fall through to here.
1691 1.1 christos From expr.c. */
1692 1.1 christos
1693 1.1 christos void
1694 1.1 christos md_operand (expressionS *expressionP)
1695 1.1 christos {
1696 1.1 christos if (*input_line_pointer == '#' && ! do_not_ignore_hash)
1697 1.1 christos {
1698 1.1 christos input_line_pointer++;
1699 1.1 christos expression (expressionP);
1700 1.1 christos }
1701 1.1 christos }
1702 1.1 christos
1703 1.1 christos bfd_boolean
1704 1.1 christos d10v_fix_adjustable (fixS *fixP)
1705 1.1 christos {
1706 1.1 christos /* We need the symbol name for the VTABLE entries. */
1707 1.1 christos if (fixP->fx_r_type == BFD_RELOC_VTABLE_INHERIT
1708 1.1 christos || fixP->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
1709 1.1 christos return 0;
1710 1.1 christos
1711 1.1 christos return 1;
1712 1.1 christos }
1713 1.1 christos
1714 1.1 christos /* The target specific pseudo-ops which we support. */
1715 1.1 christos const pseudo_typeS md_pseudo_table[] =
1716 1.1 christos {
1717 1.1 christos { "word", d10v_dot_word, 2 },
1718 1.1 christos { NULL, NULL, 0 }
1719 1.1 christos };
1720 1.1 christos
1721 1.1 christos void
1722 1.1 christos md_assemble (char *str)
1723 1.1 christos {
1724 1.1 christos /* etype is saved extype. For multi-line instructions. */
1725 1.1 christos packing_type extype = PACK_UNSPEC; /* Parallel, etc. */
1726 1.1 christos struct d10v_opcode *opcode;
1727 1.1 christos unsigned long insn;
1728 1.1 christos char *str2;
1729 1.1 christos
1730 1.1 christos if (etype == PACK_UNSPEC)
1731 1.1 christos {
1732 1.1 christos /* Look for the special multiple instruction separators. */
1733 1.1 christos str2 = strstr (str, "||");
1734 1.1 christos if (str2)
1735 1.1 christos extype = PACK_PARALLEL;
1736 1.1 christos else
1737 1.1 christos {
1738 1.1 christos str2 = strstr (str, "->");
1739 1.1 christos if (str2)
1740 1.1 christos extype = PACK_LEFT_RIGHT;
1741 1.1 christos else
1742 1.1 christos {
1743 1.1 christos str2 = strstr (str, "<-");
1744 1.1 christos if (str2)
1745 1.1 christos extype = PACK_RIGHT_LEFT;
1746 1.1 christos }
1747 1.1 christos }
1748 1.1 christos
1749 1.1 christos /* str2 points to the separator, if there is one. */
1750 1.1 christos if (str2)
1751 1.1 christos {
1752 1.1 christos *str2 = 0;
1753 1.1 christos
1754 1.1 christos /* If two instructions are present and we already have one saved,
1755 1.1 christos then first write out the saved one. */
1756 1.1 christos d10v_cleanup ();
1757 1.1 christos
1758 1.1 christos /* Assemble first instruction and save it. */
1759 1.1 christos prev_insn = do_assemble (str, &prev_opcode);
1760 1.1 christos prev_seg = now_seg;
1761 1.1 christos prev_subseg = now_subseg;
1762 1.1 christos if (prev_insn == (unsigned long) -1)
1763 1.1 christos as_fatal (_("can't find previous opcode "));
1764 1.1 christos fixups = fixups->next;
1765 1.1 christos str = str2 + 2;
1766 1.1 christos }
1767 1.1 christos }
1768 1.1 christos
1769 1.1 christos insn = do_assemble (str, &opcode);
1770 1.1 christos if (insn == (unsigned long) -1)
1771 1.1 christos {
1772 1.1 christos if (extype != PACK_UNSPEC)
1773 1.1 christos etype = extype;
1774 1.1 christos else
1775 1.1 christos as_bad (_("could not assemble: %s"), str);
1776 1.1 christos return;
1777 1.1 christos }
1778 1.1 christos
1779 1.1 christos if (etype != PACK_UNSPEC)
1780 1.1 christos {
1781 1.1 christos extype = etype;
1782 1.1 christos etype = PACK_UNSPEC;
1783 1.1 christos }
1784 1.1 christos
1785 1.1 christos /* If this is a long instruction, write it and any previous short
1786 1.1 christos instruction. */
1787 1.1 christos if (opcode->format & LONG_OPCODE)
1788 1.1 christos {
1789 1.1 christos if (extype != PACK_UNSPEC)
1790 1.1 christos as_fatal (_("Unable to mix instructions as specified"));
1791 1.1 christos d10v_cleanup ();
1792 1.1 christos write_long (insn, fixups);
1793 1.1 christos prev_opcode = NULL;
1794 1.1 christos return;
1795 1.1 christos }
1796 1.1 christos
1797 1.1 christos if (prev_opcode
1798 1.1 christos && prev_seg
1799 1.1 christos && ((prev_seg != now_seg) || (prev_subseg != now_subseg)))
1800 1.1 christos d10v_cleanup ();
1801 1.1 christos
1802 1.1 christos if (prev_opcode
1803 1.1 christos && (0 == write_2_short (prev_opcode, prev_insn, opcode, insn, extype,
1804 1.1 christos fixups)))
1805 1.1 christos {
1806 1.1 christos /* No instructions saved. */
1807 1.1 christos prev_opcode = NULL;
1808 1.1 christos }
1809 1.1 christos else
1810 1.1 christos {
1811 1.1 christos if (extype != PACK_UNSPEC)
1812 1.1 christos as_fatal (_("Unable to mix instructions as specified"));
1813 1.1 christos /* Save last instruction so it may be packed on next pass. */
1814 1.1 christos prev_opcode = opcode;
1815 1.1 christos prev_insn = insn;
1816 1.1 christos prev_seg = now_seg;
1817 1.1 christos prev_subseg = now_subseg;
1818 1.1 christos fixups = fixups->next;
1819 1.1 christos }
1820 1.1 christos }
1821 1.1 christos
1822