tc-arc.c revision 1.1.1.2 1 /* tc-arc.c -- Assembler for the ARC
2 Copyright 1994, 1995, 1997, 1999, 2000, 2001, 2002, 2003, 2004, 2005,
3 2006, 2007, 2009, 2011 Free Software Foundation, Inc.
4 Contributed by Doug Evans (dje (at) cygnus.com).
5
6 This file is part of GAS, the GNU Assembler.
7
8 GAS is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3, or (at your option)
11 any later version.
12
13 GAS is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with GAS; see the file COPYING. If not, write to the Free
20 Software Foundation, 51 Franklin Street - Fifth Floor, Boston, MA
21 02110-1301, USA. */
22
23 #include "as.h"
24 #include "struc-symbol.h"
25 #include "safe-ctype.h"
26 #include "subsegs.h"
27 #include "opcode/arc.h"
28 #include "../opcodes/arc-ext.h"
29 #include "elf/arc.h"
30 #include "dwarf2dbg.h"
31
32 const struct suffix_classes
33 {
34 char *name;
35 int len;
36 } suffixclass[] =
37 {
38 { "SUFFIX_COND|SUFFIX_FLAG",23 },
39 { "SUFFIX_FLAG", 11 },
40 { "SUFFIX_COND", 11 },
41 { "SUFFIX_NONE", 11 }
42 };
43
44 #define MAXSUFFIXCLASS (sizeof (suffixclass) / sizeof (struct suffix_classes))
45
46 const struct syntax_classes
47 {
48 char *name;
49 int len;
50 int s_class;
51 } syntaxclass[] =
52 {
53 { "SYNTAX_3OP|OP1_MUST_BE_IMM", 26, SYNTAX_3OP|OP1_MUST_BE_IMM|SYNTAX_VALID },
54 { "OP1_MUST_BE_IMM|SYNTAX_3OP", 26, OP1_MUST_BE_IMM|SYNTAX_3OP|SYNTAX_VALID },
55 { "SYNTAX_2OP|OP1_IMM_IMPLIED", 26, SYNTAX_2OP|OP1_IMM_IMPLIED|SYNTAX_VALID },
56 { "OP1_IMM_IMPLIED|SYNTAX_2OP", 26, OP1_IMM_IMPLIED|SYNTAX_2OP|SYNTAX_VALID },
57 { "SYNTAX_3OP", 10, SYNTAX_3OP|SYNTAX_VALID },
58 { "SYNTAX_2OP", 10, SYNTAX_2OP|SYNTAX_VALID }
59 };
60
61 #define MAXSYNTAXCLASS (sizeof (syntaxclass) / sizeof (struct syntax_classes))
62
63 /* This array holds the chars that always start a comment. If the
64 pre-processor is disabled, these aren't very useful. */
65 const char comment_chars[] = "#;";
66
67 /* This array holds the chars that only start a comment at the beginning of
68 a line. If the line seems to have the form '# 123 filename'
69 .line and .file directives will appear in the pre-processed output */
70 /* Note that input_file.c hand checks for '#' at the beginning of the
71 first line of the input file. This is because the compiler outputs
72 #NO_APP at the beginning of its output. */
73 /* Also note that comments started like this one will always
74 work if '/' isn't otherwise defined. */
75 const char line_comment_chars[] = "#";
76
77 const char line_separator_chars[] = "";
78
79 /* Chars that can be used to separate mant from exp in floating point nums. */
80 const char EXP_CHARS[] = "eE";
81
82 /* Chars that mean this number is a floating point constant
83 As in 0f12.456 or 0d1.2345e12. */
84 const char FLT_CHARS[] = "rRsSfFdD";
85
86 /* Byte order. */
87 extern int target_big_endian;
88 const char *arc_target_format = DEFAULT_TARGET_FORMAT;
89 static int byte_order = DEFAULT_BYTE_ORDER;
90
91 static segT arcext_section;
92
93 /* One of bfd_mach_arc_n. */
94 static int arc_mach_type = bfd_mach_arc_6;
95
96 /* Non-zero if the cpu type has been explicitly specified. */
97 static int mach_type_specified_p = 0;
98
99 /* Non-zero if opcode tables have been initialized.
100 A .option command must appear before any instructions. */
101 static int cpu_tables_init_p = 0;
102
103 static struct hash_control *arc_suffix_hash = NULL;
104
105 const char *md_shortopts = "";
107
108 enum options
109 {
110 OPTION_EB = OPTION_MD_BASE,
111 OPTION_EL,
112 OPTION_ARC5,
113 OPTION_ARC6,
114 OPTION_ARC7,
115 OPTION_ARC8,
116 OPTION_ARC
117 };
118
119 struct option md_longopts[] =
120 {
121 { "EB", no_argument, NULL, OPTION_EB },
122 { "EL", no_argument, NULL, OPTION_EL },
123 { "marc5", no_argument, NULL, OPTION_ARC5 },
124 { "pre-v6", no_argument, NULL, OPTION_ARC5 },
125 { "marc6", no_argument, NULL, OPTION_ARC6 },
126 { "marc7", no_argument, NULL, OPTION_ARC7 },
127 { "marc8", no_argument, NULL, OPTION_ARC8 },
128 { "marc", no_argument, NULL, OPTION_ARC },
129 { NULL, no_argument, NULL, 0 }
130 };
131 size_t md_longopts_size = sizeof (md_longopts);
132
133 #define IS_SYMBOL_OPERAND(o) \
134 ((o) == 'b' || (o) == 'c' || (o) == 's' || (o) == 'o' || (o) == 'O')
135
136 struct arc_operand_value *get_ext_suffix (char *s);
137
138 /* Invocation line includes a switch not recognized by the base assembler.
139 See if it's a processor-specific option. */
140
141 int
142 md_parse_option (int c, char *arg ATTRIBUTE_UNUSED)
143 {
144 switch (c)
145 {
146 case OPTION_ARC5:
147 arc_mach_type = bfd_mach_arc_5;
148 break;
149 case OPTION_ARC:
150 case OPTION_ARC6:
151 arc_mach_type = bfd_mach_arc_6;
152 break;
153 case OPTION_ARC7:
154 arc_mach_type = bfd_mach_arc_7;
155 break;
156 case OPTION_ARC8:
157 arc_mach_type = bfd_mach_arc_8;
158 break;
159 case OPTION_EB:
160 byte_order = BIG_ENDIAN;
161 arc_target_format = "elf32-bigarc";
162 break;
163 case OPTION_EL:
164 byte_order = LITTLE_ENDIAN;
165 arc_target_format = "elf32-littlearc";
166 break;
167 default:
168 return 0;
169 }
170 return 1;
171 }
172
173 void
174 md_show_usage (FILE *stream)
175 {
176 fprintf (stream, "\
177 ARC Options:\n\
178 -marc[5|6|7|8] select processor variant (default arc%d)\n\
179 -EB assemble code for a big endian cpu\n\
180 -EL assemble code for a little endian cpu\n", arc_mach_type + 5);
181 }
182
183 /* This function is called once, at assembler startup time. It should
184 set up all the tables, etc. that the MD part of the assembler will need.
185 Opcode selection is deferred until later because we might see a .option
186 command. */
187
188 void
189 md_begin (void)
190 {
191 /* The endianness can be chosen "at the factory". */
192 target_big_endian = byte_order == BIG_ENDIAN;
193
194 if (!bfd_set_arch_mach (stdoutput, bfd_arch_arc, arc_mach_type))
195 as_warn (_("could not set architecture and machine"));
196
197 /* This call is necessary because we need to initialize `arc_operand_map'
198 which may be needed before we see the first insn. */
199 arc_opcode_init_tables (arc_get_opcode_mach (arc_mach_type,
200 target_big_endian));
201 }
202
203 /* Initialize the various opcode and operand tables.
204 MACH is one of bfd_mach_arc_xxx. */
205
206 static void
207 init_opcode_tables (int mach)
208 {
209 int i;
210 char *last;
211
212 if ((arc_suffix_hash = hash_new ()) == NULL)
213 as_fatal (_("virtual memory exhausted"));
214
215 if (!bfd_set_arch_mach (stdoutput, bfd_arch_arc, mach))
216 as_warn (_("could not set architecture and machine"));
217
218 /* This initializes a few things in arc-opc.c that we need.
219 This must be called before the various arc_xxx_supported fns. */
220 arc_opcode_init_tables (arc_get_opcode_mach (mach, target_big_endian));
221
222 /* Only put the first entry of each equivalently named suffix in the
223 table. */
224 last = "";
225 for (i = 0; i < arc_suffixes_count; i++)
226 {
227 if (strcmp (arc_suffixes[i].name, last) != 0)
228 hash_insert (arc_suffix_hash, arc_suffixes[i].name, (void *) (arc_suffixes + i));
229 last = arc_suffixes[i].name;
230 }
231
232 /* Since registers don't have a prefix, we put them in the symbol table so
233 they can't be used as symbols. This also simplifies argument parsing as
234 we can let gas parse registers for us. The recorded register number is
235 the address of the register's entry in arc_reg_names.
236
237 If the register name is already in the table, then the existing
238 definition is assumed to be from an .ExtCoreRegister pseudo-op. */
239
240 for (i = 0; i < arc_reg_names_count; i++)
241 {
242 if (symbol_find (arc_reg_names[i].name))
243 continue;
244 /* Use symbol_create here instead of symbol_new so we don't try to
245 output registers into the object file's symbol table. */
246 symbol_table_insert (symbol_create (arc_reg_names[i].name,
247 reg_section,
248 (valueT) &arc_reg_names[i],
249 &zero_address_frag));
250 }
251
252 /* Tell `.option' it's too late. */
253 cpu_tables_init_p = 1;
254 }
255
256 /* Insert an operand value into an instruction.
258 If REG is non-NULL, it is a register number and ignore VAL. */
259
260 static arc_insn
261 arc_insert_operand (arc_insn insn,
262 const struct arc_operand *operand,
263 int mods,
264 const struct arc_operand_value *reg,
265 offsetT val,
266 char *file,
267 unsigned int line)
268 {
269 if (operand->bits != 32)
270 {
271 long min, max;
272 offsetT test;
273
274 if ((operand->flags & ARC_OPERAND_SIGNED) != 0)
275 {
276 if ((operand->flags & ARC_OPERAND_SIGNOPT) != 0)
277 max = (1 << operand->bits) - 1;
278 else
279 max = (1 << (operand->bits - 1)) - 1;
280 min = - (1 << (operand->bits - 1));
281 }
282 else
283 {
284 max = (1 << operand->bits) - 1;
285 min = 0;
286 }
287
288 if ((operand->flags & ARC_OPERAND_NEGATIVE) != 0)
289 test = - val;
290 else
291 test = val;
292
293 if (test < (offsetT) min || test > (offsetT) max)
294 as_warn_value_out_of_range (_("operand"), test, (offsetT) min, (offsetT) max, file, line);
295 }
296
297 if (operand->insert)
298 {
299 const char *errmsg;
300
301 errmsg = NULL;
302 insn = (*operand->insert) (insn, operand, mods, reg, (long) val, &errmsg);
303 if (errmsg != (const char *) NULL)
304 as_warn ("%s", errmsg);
305 }
306 else
307 insn |= (((long) val & ((1 << operand->bits) - 1))
308 << operand->shift);
309
310 return insn;
311 }
312
313 /* We need to keep a list of fixups. We can't simply generate them as
314 we go, because that would require us to first create the frag, and
315 that would screw up references to ``.''. */
316
317 struct arc_fixup
318 {
319 /* index into `arc_operands' */
320 int opindex;
321 expressionS exp;
322 };
323
324 #define MAX_FIXUPS 5
325
326 #define MAX_SUFFIXES 5
327
328 /* Compute the reloc type of an expression.
329 The possibly modified expression is stored in EXPNEW.
330
331 This is used to convert the expressions generated by the %-op's into
332 the appropriate operand type. It is called for both data in instructions
333 (operands) and data outside instructions (variables, debugging info, etc.).
334
335 Currently supported %-ops:
336
337 %st(symbol): represented as "symbol >> 2"
338 "st" is short for STatus as in the status register (pc)
339
340 DEFAULT_TYPE is the type to use if no special processing is required.
341
342 DATA_P is non-zero for data or limm values, zero for insn operands.
343 Remember that the opcode "insertion fns" cannot be used on data, they're
344 only for inserting operands into insns. They also can't be used for limm
345 values as the insertion routines don't handle limm values. When called for
346 insns we return fudged reloc types (real_value - BFD_RELOC_UNUSED). When
347 called for data or limm values we use real reloc types. */
348
349 static int
350 get_arc_exp_reloc_type (int data_p,
351 int default_type,
352 expressionS *exp,
353 expressionS *expnew)
354 {
355 /* If the expression is "symbol >> 2" we must change it to just "symbol",
356 as fix_new_exp can't handle it. Similarly for (symbol - symbol) >> 2.
357 That's ok though. What's really going on here is that we're using
358 ">> 2" as a special syntax for specifying BFD_RELOC_ARC_B26. */
359
360 if (exp->X_op == O_right_shift
361 && exp->X_op_symbol != NULL
362 && exp->X_op_symbol->sy_value.X_op == O_constant
363 && exp->X_op_symbol->sy_value.X_add_number == 2
364 && exp->X_add_number == 0)
365 {
366 if (exp->X_add_symbol != NULL
367 && (exp->X_add_symbol->sy_value.X_op == O_constant
368 || exp->X_add_symbol->sy_value.X_op == O_symbol))
369 {
370 *expnew = *exp;
371 expnew->X_op = O_symbol;
372 expnew->X_op_symbol = NULL;
373 return data_p ? BFD_RELOC_ARC_B26 : arc_operand_map['J'];
374 }
375 else if (exp->X_add_symbol != NULL
376 && exp->X_add_symbol->sy_value.X_op == O_subtract)
377 {
378 *expnew = exp->X_add_symbol->sy_value;
379 return data_p ? BFD_RELOC_ARC_B26 : arc_operand_map['J'];
380 }
381 }
382
383 *expnew = *exp;
384 return default_type;
385 }
386
387 static int
389 arc_set_ext_seg (void)
390 {
391 if (!arcext_section)
392 {
393 arcext_section = subseg_new (".arcextmap", 0);
394 bfd_set_section_flags (stdoutput, arcext_section,
395 SEC_READONLY | SEC_HAS_CONTENTS);
396 }
397 else
398 subseg_set (arcext_section, 0);
399 return 1;
400 }
401
402 static void
403 arc_extoper (int opertype)
404 {
405 char *name;
406 char *mode;
407 char c;
408 char *p;
409 int imode = 0;
410 int number;
411 struct arc_ext_operand_value *ext_oper;
412 symbolS *symbolP;
413
414 segT old_sec;
415 int old_subsec;
416
417 name = input_line_pointer;
418 c = get_symbol_end ();
419 name = xstrdup (name);
420
421 p = name;
422 while (*p)
423 {
424 *p = TOLOWER (*p);
425 p++;
426 }
427
428 /* just after name is now '\0' */
429 p = input_line_pointer;
430 *p = c;
431 SKIP_WHITESPACE ();
432
433 if (*input_line_pointer != ',')
434 {
435 as_bad (_("expected comma after operand name"));
436 ignore_rest_of_line ();
437 free (name);
438 return;
439 }
440
441 input_line_pointer++; /* skip ',' */
442 number = get_absolute_expression ();
443
444 if (number < 0)
445 {
446 as_bad (_("negative operand number %d"), number);
447 ignore_rest_of_line ();
448 free (name);
449 return;
450 }
451
452 if (opertype)
453 {
454 SKIP_WHITESPACE ();
455
456 if (*input_line_pointer != ',')
457 {
458 as_bad (_("expected comma after register-number"));
459 ignore_rest_of_line ();
460 free (name);
461 return;
462 }
463
464 input_line_pointer++; /* skip ',' */
465 mode = input_line_pointer;
466
467 if (!strncmp (mode, "r|w", 3))
468 {
469 imode = 0;
470 input_line_pointer += 3;
471 }
472 else
473 {
474 if (!strncmp (mode, "r", 1))
475 {
476 imode = ARC_REGISTER_READONLY;
477 input_line_pointer += 1;
478 }
479 else
480 {
481 if (strncmp (mode, "w", 1))
482 {
483 as_bad (_("invalid mode"));
484 ignore_rest_of_line ();
485 free (name);
486 return;
487 }
488 else
489 {
490 imode = ARC_REGISTER_WRITEONLY;
491 input_line_pointer += 1;
492 }
493 }
494 }
495 SKIP_WHITESPACE ();
496 if (1 == opertype)
497 {
498 if (*input_line_pointer != ',')
499 {
500 as_bad (_("expected comma after register-mode"));
501 ignore_rest_of_line ();
502 free (name);
503 return;
504 }
505
506 input_line_pointer++; /* skip ',' */
507
508 if (!strncmp (input_line_pointer, "cannot_shortcut", 15))
509 {
510 imode |= arc_get_noshortcut_flag ();
511 input_line_pointer += 15;
512 }
513 else
514 {
515 if (strncmp (input_line_pointer, "can_shortcut", 12))
516 {
517 as_bad (_("shortcut designator invalid"));
518 ignore_rest_of_line ();
519 free (name);
520 return;
521 }
522 else
523 {
524 input_line_pointer += 12;
525 }
526 }
527 }
528 }
529
530 if ((opertype == 1) && number > 60)
531 {
532 as_bad (_("core register value (%d) too large"), number);
533 ignore_rest_of_line ();
534 free (name);
535 return;
536 }
537
538 if ((opertype == 0) && number > 31)
539 {
540 as_bad (_("condition code value (%d) too large"), number);
541 ignore_rest_of_line ();
542 free (name);
543 return;
544 }
545
546 ext_oper = (struct arc_ext_operand_value *)
547 xmalloc (sizeof (struct arc_ext_operand_value));
548
549 if (opertype)
550 {
551 /* If the symbol already exists, point it at the new definition. */
552 if ((symbolP = symbol_find (name)))
553 {
554 if (S_GET_SEGMENT (symbolP) == reg_section)
555 S_SET_VALUE (symbolP, (valueT) &ext_oper->operand);
556 else
557 {
558 as_bad (_("attempt to override symbol: %s"), name);
559 ignore_rest_of_line ();
560 free (name);
561 free (ext_oper);
562 return;
563 }
564 }
565 else
566 {
567 /* If its not there, add it. */
568 symbol_table_insert (symbol_create (name, reg_section,
569 (valueT) &ext_oper->operand,
570 &zero_address_frag));
571 }
572 }
573
574 ext_oper->operand.name = name;
575 ext_oper->operand.value = number;
576 ext_oper->operand.type = arc_operand_type (opertype);
577 ext_oper->operand.flags = imode;
578
579 ext_oper->next = arc_ext_operands;
580 arc_ext_operands = ext_oper;
581
582 /* OK, now that we know what this operand is, put a description in
583 the arc extension section of the output file. */
584
585 old_sec = now_seg;
586 old_subsec = now_subseg;
587
588 arc_set_ext_seg ();
589
590 switch (opertype)
591 {
592 case 0:
593 p = frag_more (1);
594 *p = 3 + strlen (name) + 1;
595 p = frag_more (1);
596 *p = EXT_COND_CODE;
597 p = frag_more (1);
598 *p = number;
599 p = frag_more (strlen (name) + 1);
600 strcpy (p, name);
601 break;
602 case 1:
603 p = frag_more (1);
604 *p = 3 + strlen (name) + 1;
605 p = frag_more (1);
606 *p = EXT_CORE_REGISTER;
607 p = frag_more (1);
608 *p = number;
609 p = frag_more (strlen (name) + 1);
610 strcpy (p, name);
611 break;
612 case 2:
613 p = frag_more (1);
614 *p = 6 + strlen (name) + 1;
615 p = frag_more (1);
616 *p = EXT_AUX_REGISTER;
617 p = frag_more (1);
618 *p = number >> 24 & 0xff;
619 p = frag_more (1);
620 *p = number >> 16 & 0xff;
621 p = frag_more (1);
622 *p = number >> 8 & 0xff;
623 p = frag_more (1);
624 *p = number & 0xff;
625 p = frag_more (strlen (name) + 1);
626 strcpy (p, name);
627 break;
628 default:
629 as_bad (_("invalid opertype"));
630 ignore_rest_of_line ();
631 free (name);
632 return;
633 break;
634 }
635
636 subseg_set (old_sec, old_subsec);
637
638 /* Enter all registers into the symbol table. */
639
640 demand_empty_rest_of_line ();
641 }
642
643 static void
644 arc_extinst (int ignore ATTRIBUTE_UNUSED)
645 {
646 char syntax[129];
647 char *name;
648 char *p;
649 char c;
650 int suffixcode = -1;
651 int opcode, subopcode;
652 int i;
653 int s_class = 0;
654 int name_len;
655 struct arc_opcode *ext_op;
656
657 segT old_sec;
658 int old_subsec;
659
660 name = input_line_pointer;
661 c = get_symbol_end ();
662 name = xstrdup (name);
663 strcpy (syntax, name);
664 name_len = strlen (name);
665
666 /* just after name is now '\0' */
667 p = input_line_pointer;
668 *p = c;
669
670 SKIP_WHITESPACE ();
671
672 if (*input_line_pointer != ',')
673 {
674 as_bad (_("expected comma after operand name"));
675 ignore_rest_of_line ();
676 return;
677 }
678
679 input_line_pointer++; /* skip ',' */
680 opcode = get_absolute_expression ();
681
682 SKIP_WHITESPACE ();
683
684 if (*input_line_pointer != ',')
685 {
686 as_bad (_("expected comma after opcode"));
687 ignore_rest_of_line ();
688 return;
689 }
690
691 input_line_pointer++; /* skip ',' */
692 subopcode = get_absolute_expression ();
693
694 if (subopcode < 0)
695 {
696 as_bad (_("negative subopcode %d"), subopcode);
697 ignore_rest_of_line ();
698 return;
699 }
700
701 if (subopcode)
702 {
703 if (3 != opcode)
704 {
705 as_bad (_("subcode value found when opcode not equal 0x03"));
706 ignore_rest_of_line ();
707 return;
708 }
709 else
710 {
711 if (subopcode < 0x09 || subopcode == 0x3f)
712 {
713 as_bad (_("invalid subopcode %d"), subopcode);
714 ignore_rest_of_line ();
715 return;
716 }
717 }
718 }
719
720 SKIP_WHITESPACE ();
721
722 if (*input_line_pointer != ',')
723 {
724 as_bad (_("expected comma after subopcode"));
725 ignore_rest_of_line ();
726 return;
727 }
728
729 input_line_pointer++; /* skip ',' */
730
731 for (i = 0; i < (int) MAXSUFFIXCLASS; i++)
732 {
733 if (!strncmp (suffixclass[i].name,input_line_pointer, suffixclass[i].len))
734 {
735 suffixcode = i;
736 input_line_pointer += suffixclass[i].len;
737 break;
738 }
739 }
740
741 if (-1 == suffixcode)
742 {
743 as_bad (_("invalid suffix class"));
744 ignore_rest_of_line ();
745 return;
746 }
747
748 SKIP_WHITESPACE ();
749
750 if (*input_line_pointer != ',')
751 {
752 as_bad (_("expected comma after suffix class"));
753 ignore_rest_of_line ();
754 return;
755 }
756
757 input_line_pointer++; /* skip ',' */
758
759 for (i = 0; i < (int) MAXSYNTAXCLASS; i++)
760 {
761 if (!strncmp (syntaxclass[i].name,input_line_pointer, syntaxclass[i].len))
762 {
763 s_class = syntaxclass[i].s_class;
764 input_line_pointer += syntaxclass[i].len;
765 break;
766 }
767 }
768
769 if (0 == (SYNTAX_VALID & s_class))
770 {
771 as_bad (_("invalid syntax class"));
772 ignore_rest_of_line ();
773 return;
774 }
775
776 if ((0x3 == opcode) & (s_class & SYNTAX_3OP))
777 {
778 as_bad (_("opcode 0x3 and SYNTAX_3OP invalid"));
779 ignore_rest_of_line ();
780 return;
781 }
782
783 switch (suffixcode)
784 {
785 case 0:
786 strcat (syntax, "%.q%.f ");
787 break;
788 case 1:
789 strcat (syntax, "%.f ");
790 break;
791 case 2:
792 strcat (syntax, "%.q ");
793 break;
794 case 3:
795 strcat (syntax, " ");
796 break;
797 default:
798 as_bad (_("unknown suffix class"));
799 ignore_rest_of_line ();
800 return;
801 break;
802 };
803
804 strcat (syntax, ((opcode == 0x3) ? "%a,%b" : ((s_class & SYNTAX_3OP) ? "%a,%b,%c" : "%b,%c")));
805 if (suffixcode < 2)
806 strcat (syntax, "%F");
807 strcat (syntax, "%S%L");
808
809 ext_op = (struct arc_opcode *) xmalloc (sizeof (struct arc_opcode));
810 ext_op->syntax = xstrdup (syntax);
811
812 ext_op->mask = I (-1) | ((0x3 == opcode) ? C (-1) : 0);
813 ext_op->value = I (opcode) | ((0x3 == opcode) ? C (subopcode) : 0);
814 ext_op->flags = s_class;
815 ext_op->next_asm = arc_ext_opcodes;
816 ext_op->next_dis = arc_ext_opcodes;
817 arc_ext_opcodes = ext_op;
818
819 /* OK, now that we know what this inst is, put a description in the
820 arc extension section of the output file. */
821
822 old_sec = now_seg;
823 old_subsec = now_subseg;
824
825 arc_set_ext_seg ();
826
827 p = frag_more (1);
828 *p = 5 + name_len + 1;
829 p = frag_more (1);
830 *p = EXT_INSTRUCTION;
831 p = frag_more (1);
832 *p = opcode;
833 p = frag_more (1);
834 *p = subopcode;
835 p = frag_more (1);
836 *p = (s_class & (OP1_MUST_BE_IMM | OP1_IMM_IMPLIED) ? IGNORE_FIRST_OPD : 0);
837 p = frag_more (name_len);
838 strncpy (p, syntax, name_len);
839 p = frag_more (1);
840 *p = '\0';
841
842 subseg_set (old_sec, old_subsec);
843
844 demand_empty_rest_of_line ();
845 }
846
847 static void
848 arc_common (int localScope)
849 {
850 char *name;
851 char c;
852 char *p;
853 int align, size;
854 symbolS *symbolP;
855
856 name = input_line_pointer;
857 c = get_symbol_end ();
858 /* just after name is now '\0' */
859 p = input_line_pointer;
860 *p = c;
861 SKIP_WHITESPACE ();
862
863 if (*input_line_pointer != ',')
864 {
865 as_bad (_("expected comma after symbol name"));
866 ignore_rest_of_line ();
867 return;
868 }
869
870 input_line_pointer++; /* skip ',' */
871 size = get_absolute_expression ();
872
873 if (size < 0)
874 {
875 as_bad (_("negative symbol length"));
876 ignore_rest_of_line ();
877 return;
878 }
879
880 *p = 0;
881 symbolP = symbol_find_or_make (name);
882 *p = c;
883
884 if (S_IS_DEFINED (symbolP) && ! S_IS_COMMON (symbolP))
885 {
886 as_bad (_("ignoring attempt to re-define symbol"));
887 ignore_rest_of_line ();
888 return;
889 }
890 if (((int) S_GET_VALUE (symbolP) != 0) \
891 && ((int) S_GET_VALUE (symbolP) != size))
892 {
893 as_warn (_("length of symbol \"%s\" already %ld, ignoring %d"),
894 S_GET_NAME (symbolP), (long) S_GET_VALUE (symbolP), size);
895 }
896 gas_assert (symbolP->sy_frag == &zero_address_frag);
897
898 /* Now parse the alignment field. This field is optional for
899 local and global symbols. Default alignment is zero. */
900 if (*input_line_pointer == ',')
901 {
902 input_line_pointer++;
903 align = get_absolute_expression ();
904 if (align < 0)
905 {
906 align = 0;
907 as_warn (_("assuming symbol alignment of zero"));
908 }
909 }
910 else
911 align = 0;
912
913 if (localScope != 0)
914 {
915 segT old_sec;
916 int old_subsec;
917 char *pfrag;
918
919 old_sec = now_seg;
920 old_subsec = now_subseg;
921 record_alignment (bss_section, align);
922 subseg_set (bss_section, 0); /* ??? subseg_set (bss_section, 1); ??? */
923
924 if (align)
925 /* Do alignment. */
926 frag_align (align, 0, 0);
927
928 /* Detach from old frag. */
929 if (S_GET_SEGMENT (symbolP) == bss_section)
930 symbolP->sy_frag->fr_symbol = NULL;
931
932 symbolP->sy_frag = frag_now;
933 pfrag = frag_var (rs_org, 1, 1, (relax_substateT) 0, symbolP,
934 (offsetT) size, (char *) 0);
935 *pfrag = 0;
936
937 S_SET_SIZE (symbolP, size);
938 S_SET_SEGMENT (symbolP, bss_section);
939 S_CLEAR_EXTERNAL (symbolP);
940 symbol_get_obj (symbolP)->local = 1;
941 subseg_set (old_sec, old_subsec);
942 }
943 else
944 {
945 S_SET_VALUE (symbolP, (valueT) size);
946 S_SET_ALIGN (symbolP, align);
947 S_SET_EXTERNAL (symbolP);
948 S_SET_SEGMENT (symbolP, bfd_com_section_ptr);
949 }
950
951 symbolP->bsym->flags |= BSF_OBJECT;
952
953 demand_empty_rest_of_line ();
954 }
955
956 /* Select the cpu we're assembling for. */
958
959 static void
960 arc_option (int ignore ATTRIBUTE_UNUSED)
961 {
962 extern int arc_get_mach (char *);
963 int mach;
964 char c;
965 char *cpu;
966
967 cpu = input_line_pointer;
968 c = get_symbol_end ();
969 mach = arc_get_mach (cpu);
970 *input_line_pointer = c;
971
972 /* If an instruction has already been seen, it's too late. */
973 if (cpu_tables_init_p)
974 {
975 as_bad (_("\".option\" directive must appear before any instructions"));
976 ignore_rest_of_line ();
977 return;
978 }
979
980 if (mach == -1)
981 goto bad_cpu;
982
983 if (mach_type_specified_p && mach != arc_mach_type)
984 {
985 as_bad (_("\".option\" directive conflicts with initial definition"));
986 ignore_rest_of_line ();
987 return;
988 }
989 else
990 {
991 /* The cpu may have been selected on the command line. */
992 if (mach != arc_mach_type)
993 as_warn (_("\".option\" directive overrides command-line (default) value"));
994 arc_mach_type = mach;
995 if (!bfd_set_arch_mach (stdoutput, bfd_arch_arc, mach))
996 as_fatal (_("could not set architecture and machine"));
997 mach_type_specified_p = 1;
998 }
999 demand_empty_rest_of_line ();
1000 return;
1001
1002 bad_cpu:
1003 as_bad (_("invalid identifier for \".option\""));
1004 ignore_rest_of_line ();
1005 }
1006
1007 char *
1009 md_atof (int type, char *litP, int *sizeP)
1010 {
1011 return ieee_md_atof (type, litP, sizeP, TRUE);
1012 }
1013
1014 /* Write a value out to the object file, using the appropriate
1015 endianness. */
1016
1017 void
1018 md_number_to_chars (char *buf, valueT val, int n)
1019 {
1020 if (target_big_endian)
1021 number_to_chars_bigendian (buf, val, n);
1022 else
1023 number_to_chars_littleendian (buf, val, n);
1024 }
1025
1026 /* Round up a section size to the appropriate boundary. */
1027
1028 valueT
1029 md_section_align (segT segment, valueT size)
1030 {
1031 int align = bfd_get_section_alignment (stdoutput, segment);
1032
1033 return ((size + (1 << align) - 1) & (-1 << align));
1034 }
1035
1036 /* We don't have any form of relaxing. */
1037
1038 int
1039 md_estimate_size_before_relax (fragS *fragp ATTRIBUTE_UNUSED,
1040 asection *seg ATTRIBUTE_UNUSED)
1041 {
1042 as_fatal (_("relaxation not supported\n"));
1043 return 1;
1044 }
1045
1046 /* Convert a machine dependent frag. We never generate these. */
1047
1048 void
1049 md_convert_frag (bfd *abfd ATTRIBUTE_UNUSED,
1050 asection *sec ATTRIBUTE_UNUSED,
1051 fragS *fragp ATTRIBUTE_UNUSED)
1052 {
1053 abort ();
1054 }
1055
1056 static void
1057 arc_code_symbol (expressionS *expressionP)
1058 {
1059 if (expressionP->X_op == O_symbol && expressionP->X_add_number == 0)
1060 {
1061 expressionS two;
1062
1063 expressionP->X_op = O_right_shift;
1064 expressionP->X_add_symbol->sy_value.X_op = O_constant;
1065 two.X_op = O_constant;
1066 two.X_add_symbol = two.X_op_symbol = NULL;
1067 two.X_add_number = 2;
1068 expressionP->X_op_symbol = make_expr_symbol (&two);
1069 }
1070 /* Allow %st(sym1-sym2) */
1071 else if (expressionP->X_op == O_subtract
1072 && expressionP->X_add_symbol != NULL
1073 && expressionP->X_op_symbol != NULL
1074 && expressionP->X_add_number == 0)
1075 {
1076 expressionS two;
1077
1078 expressionP->X_add_symbol = make_expr_symbol (expressionP);
1079 expressionP->X_op = O_right_shift;
1080 two.X_op = O_constant;
1081 two.X_add_symbol = two.X_op_symbol = NULL;
1082 two.X_add_number = 2;
1083 expressionP->X_op_symbol = make_expr_symbol (&two);
1084 }
1085 else
1086 as_bad (_("expression too complex code symbol"));
1087 }
1088
1089 /* Parse an operand that is machine-specific.
1090
1091 The ARC has a special %-op to adjust addresses so they're usable in
1092 branches. The "st" is short for the STatus register.
1093 ??? Later expand this to take a flags value too.
1094
1095 ??? We can't create new expression types so we map the %-op's onto the
1096 existing syntax. This means that the user could use the chosen syntax
1097 to achieve the same effect. */
1098
1099 void
1100 md_operand (expressionS *expressionP)
1101 {
1102 char *p = input_line_pointer;
1103
1104 if (*p != '%')
1105 return;
1106
1107 if (strncmp (p, "%st(", 4) == 0)
1108 {
1109 input_line_pointer += 4;
1110 expression (expressionP);
1111 if (*input_line_pointer != ')')
1112 {
1113 as_bad (_("missing ')' in %%-op"));
1114 return;
1115 }
1116 ++input_line_pointer;
1117 arc_code_symbol (expressionP);
1118 }
1119 else
1120 {
1121 /* It could be a register. */
1122 int i, l;
1123 struct arc_ext_operand_value *ext_oper = arc_ext_operands;
1124 p++;
1125
1126 while (ext_oper)
1127 {
1128 l = strlen (ext_oper->operand.name);
1129 if (!strncmp (p, ext_oper->operand.name, l) && !ISALNUM (*(p + l)))
1130 {
1131 input_line_pointer += l + 1;
1132 expressionP->X_op = O_register;
1133 expressionP->X_add_number = (offsetT) &ext_oper->operand;
1134 return;
1135 }
1136 ext_oper = ext_oper->next;
1137 }
1138 for (i = 0; i < arc_reg_names_count; i++)
1139 {
1140 l = strlen (arc_reg_names[i].name);
1141 if (!strncmp (p, arc_reg_names[i].name, l) && !ISALNUM (*(p + l)))
1142 {
1143 input_line_pointer += l + 1;
1144 expressionP->X_op = O_register;
1145 expressionP->X_add_number = (offsetT) &arc_reg_names[i];
1146 break;
1147 }
1148 }
1149 }
1150 }
1151
1152 /* We have no need to default values of symbols.
1153 We could catch register names here, but that is handled by inserting
1154 them all in the symbol table to begin with. */
1155
1156 symbolS *
1157 md_undefined_symbol (char *name ATTRIBUTE_UNUSED)
1158 {
1159 return 0;
1160 }
1161
1162 /* Functions concerning expressions. */
1164
1165 /* Parse a .byte, .word, etc. expression.
1166
1167 Values for the status register are specified with %st(label).
1168 `label' will be right shifted by 2. */
1169
1170 void
1171 arc_parse_cons_expression (expressionS *exp,
1172 unsigned int nbytes ATTRIBUTE_UNUSED)
1173 {
1174 char *p = input_line_pointer;
1175 int code_symbol_fix = 0;
1176
1177 for (; ! is_end_of_line[(unsigned char) *p]; p++)
1178 if (*p == '@' && !strncmp (p, "@h30", 4))
1179 {
1180 code_symbol_fix = 1;
1181 strcpy (p, "; ");
1182 }
1183 expression_and_evaluate (exp);
1184 if (code_symbol_fix)
1185 {
1186 arc_code_symbol (exp);
1187 input_line_pointer = p;
1188 }
1189 }
1190
1191 /* Record a fixup for a cons expression. */
1192
1193 void
1194 arc_cons_fix_new (fragS *frag,
1195 int where,
1196 int nbytes,
1197 expressionS *exp)
1198 {
1199 if (nbytes == 4)
1200 {
1201 int reloc_type;
1202 expressionS exptmp;
1203
1204 /* This may be a special ARC reloc (eg: %st()). */
1205 reloc_type = get_arc_exp_reloc_type (1, BFD_RELOC_32, exp, &exptmp);
1206 fix_new_exp (frag, where, nbytes, &exptmp, 0,
1207 (enum bfd_reloc_code_real) reloc_type);
1208 }
1209 else
1210 {
1211 fix_new_exp (frag, where, nbytes, exp, 0,
1212 nbytes == 2 ? BFD_RELOC_16
1213 : nbytes == 8 ? BFD_RELOC_64
1214 : BFD_RELOC_32);
1215 }
1216 }
1217
1218 /* Functions concerning relocs. */
1220
1221 /* The location from which a PC relative jump should be calculated,
1222 given a PC relative reloc. */
1223
1224 long
1225 md_pcrel_from (fixS *fixP)
1226 {
1227 /* Return the address of the delay slot. */
1228 return fixP->fx_frag->fr_address + fixP->fx_where + fixP->fx_size;
1229 }
1230
1231 /* Apply a fixup to the object code. This is called for all the
1232 fixups we generated by the call to fix_new_exp, above. In the call
1233 above we used a reloc code which was the largest legal reloc code
1234 plus the operand index. Here we undo that to recover the operand
1235 index. At this point all symbol values should be fully resolved,
1236 and we attempt to completely resolve the reloc. If we can not do
1237 that, we determine the correct reloc code and put it back in the fixup. */
1238
1239 void
1240 md_apply_fix (fixS *fixP, valueT * valP, segT seg)
1241 {
1242 valueT value = * valP;
1243
1244 if (fixP->fx_addsy == (symbolS *) NULL)
1245 fixP->fx_done = 1;
1246
1247 else if (fixP->fx_pcrel)
1248 {
1249 /* Hack around bfd_install_relocation brain damage. */
1250 if (S_GET_SEGMENT (fixP->fx_addsy) != seg)
1251 value += md_pcrel_from (fixP);
1252 }
1253
1254 /* We can't actually support subtracting a symbol. */
1255 if (fixP->fx_subsy != NULL)
1256 as_bad_where (fixP->fx_file, fixP->fx_line, _("expression too complex"));
1257
1258 if ((int) fixP->fx_r_type >= (int) BFD_RELOC_UNUSED)
1259 {
1260 int opindex;
1261 const struct arc_operand *operand;
1262 char *where;
1263 arc_insn insn;
1264
1265 opindex = (int) fixP->fx_r_type - (int) BFD_RELOC_UNUSED;
1266
1267 operand = &arc_operands[opindex];
1268
1269 /* Fetch the instruction, insert the fully resolved operand
1270 value, and stuff the instruction back again. */
1271 where = fixP->fx_frag->fr_literal + fixP->fx_where;
1272 if (target_big_endian)
1273 insn = bfd_getb32 ((unsigned char *) where);
1274 else
1275 insn = bfd_getl32 ((unsigned char *) where);
1276 insn = arc_insert_operand (insn, operand, -1, NULL, (offsetT) value,
1277 fixP->fx_file, fixP->fx_line);
1278 if (target_big_endian)
1279 bfd_putb32 ((bfd_vma) insn, (unsigned char *) where);
1280 else
1281 bfd_putl32 ((bfd_vma) insn, (unsigned char *) where);
1282
1283 if (fixP->fx_done)
1284 /* Nothing else to do here. */
1285 return;
1286
1287 /* Determine a BFD reloc value based on the operand information.
1288 We are only prepared to turn a few of the operands into relocs.
1289 !!! Note that we can't handle limm values here. Since we're using
1290 implicit addends the addend must be inserted into the instruction,
1291 however, the opcode insertion routines currently do nothing with
1292 limm values. */
1293 if (operand->fmt == 'B')
1294 {
1295 gas_assert ((operand->flags & ARC_OPERAND_RELATIVE_BRANCH) != 0
1296 && operand->bits == 20
1297 && operand->shift == 7);
1298 fixP->fx_r_type = BFD_RELOC_ARC_B22_PCREL;
1299 }
1300 else if (operand->fmt == 'J')
1301 {
1302 gas_assert ((operand->flags & ARC_OPERAND_ABSOLUTE_BRANCH) != 0
1303 && operand->bits == 24
1304 && operand->shift == 32);
1305 fixP->fx_r_type = BFD_RELOC_ARC_B26;
1306 }
1307 else if (operand->fmt == 'L')
1308 {
1309 gas_assert ((operand->flags & ARC_OPERAND_LIMM) != 0
1310 && operand->bits == 32
1311 && operand->shift == 32);
1312 fixP->fx_r_type = BFD_RELOC_32;
1313 }
1314 else
1315 {
1316 as_bad_where (fixP->fx_file, fixP->fx_line,
1317 _("unresolved expression that must be resolved"));
1318 fixP->fx_done = 1;
1319 return;
1320 }
1321 }
1322 else
1323 {
1324 switch (fixP->fx_r_type)
1325 {
1326 case BFD_RELOC_8:
1327 md_number_to_chars (fixP->fx_frag->fr_literal + fixP->fx_where,
1328 value, 1);
1329 break;
1330 case BFD_RELOC_16:
1331 md_number_to_chars (fixP->fx_frag->fr_literal + fixP->fx_where,
1332 value, 2);
1333 break;
1334 case BFD_RELOC_32:
1335 md_number_to_chars (fixP->fx_frag->fr_literal + fixP->fx_where,
1336 value, 4);
1337 break;
1338 case BFD_RELOC_ARC_B26:
1339 /* If !fixP->fx_done then `value' is an implicit addend.
1340 We must shift it right by 2 in this case as well because the
1341 linker performs the relocation and then adds this in (as opposed
1342 to adding this in and then shifting right by 2). */
1343 value >>= 2;
1344 md_number_to_chars (fixP->fx_frag->fr_literal + fixP->fx_where,
1345 value, 4);
1346 break;
1347 default:
1348 abort ();
1349 }
1350 }
1351 }
1352
1353 /* Translate internal representation of relocation info to BFD target
1354 format. */
1355
1356 arelent *
1357 tc_gen_reloc (asection *section ATTRIBUTE_UNUSED,
1358 fixS *fixP)
1359 {
1360 arelent *reloc;
1361
1362 reloc = (arelent *) xmalloc (sizeof (arelent));
1363 reloc->sym_ptr_ptr = (asymbol **) xmalloc (sizeof (asymbol *));
1364
1365 *reloc->sym_ptr_ptr = symbol_get_bfdsym (fixP->fx_addsy);
1366 reloc->address = fixP->fx_frag->fr_address + fixP->fx_where;
1367 reloc->howto = bfd_reloc_type_lookup (stdoutput, fixP->fx_r_type);
1368 if (reloc->howto == (reloc_howto_type *) NULL)
1369 {
1370 as_bad_where (fixP->fx_file, fixP->fx_line,
1371 _("internal error: can't export reloc type %d (`%s')"),
1372 fixP->fx_r_type,
1373 bfd_get_reloc_code_name (fixP->fx_r_type));
1374 return NULL;
1375 }
1376
1377 gas_assert (!fixP->fx_pcrel == !reloc->howto->pc_relative);
1378
1379 /* Set addend to account for PC being advanced one insn before the
1380 target address is computed. */
1381
1382 reloc->addend = (fixP->fx_pcrel ? -4 : 0);
1383
1384 return reloc;
1385 }
1386
1387 const pseudo_typeS md_pseudo_table[] =
1388 {
1389 { "align", s_align_bytes, 0 }, /* Defaulting is invalid (0). */
1390 { "comm", arc_common, 0 },
1391 { "common", arc_common, 0 },
1392 { "lcomm", arc_common, 1 },
1393 { "lcommon", arc_common, 1 },
1394 { "2byte", cons, 2 },
1395 { "half", cons, 2 },
1396 { "short", cons, 2 },
1397 { "3byte", cons, 3 },
1398 { "4byte", cons, 4 },
1399 { "word", cons, 4 },
1400 { "option", arc_option, 0 },
1401 { "cpu", arc_option, 0 },
1402 { "block", s_space, 0 },
1403 { "extcondcode", arc_extoper, 0 },
1404 { "extcoreregister", arc_extoper, 1 },
1405 { "extauxregister", arc_extoper, 2 },
1406 { "extinstruction", arc_extinst, 0 },
1407 { NULL, 0, 0 },
1408 };
1409
1410 /* This routine is called for each instruction to be assembled. */
1411
1412 void
1413 md_assemble (char *str)
1414 {
1415 const struct arc_opcode *opcode;
1416 const struct arc_opcode *std_opcode;
1417 struct arc_opcode *ext_opcode;
1418 char *start;
1419 const char *last_errmsg = 0;
1420 arc_insn insn;
1421 static int init_tables_p = 0;
1422
1423 /* Opcode table initialization is deferred until here because we have to
1424 wait for a possible .option command. */
1425 if (!init_tables_p)
1426 {
1427 init_opcode_tables (arc_mach_type);
1428 init_tables_p = 1;
1429 }
1430
1431 /* Skip leading white space. */
1432 while (ISSPACE (*str))
1433 str++;
1434
1435 /* The instructions are stored in lists hashed by the first letter (though
1436 we needn't care how they're hashed). Get the first in the list. */
1437
1438 ext_opcode = arc_ext_opcodes;
1439 std_opcode = arc_opcode_lookup_asm (str);
1440
1441 /* Keep looking until we find a match. */
1442 start = str;
1443 for (opcode = (ext_opcode ? ext_opcode : std_opcode);
1444 opcode != NULL;
1445 opcode = (ARC_OPCODE_NEXT_ASM (opcode)
1446 ? ARC_OPCODE_NEXT_ASM (opcode)
1447 : (ext_opcode ? ext_opcode = NULL, std_opcode : NULL)))
1448 {
1449 int past_opcode_p, fc, num_suffixes;
1450 int fix_up_at = 0;
1451 char *syn;
1452 struct arc_fixup fixups[MAX_FIXUPS];
1453 /* Used as a sanity check. If we need a limm reloc, make sure we ask
1454 for an extra 4 bytes from frag_more. */
1455 int limm_reloc_p;
1456 int ext_suffix_p;
1457 const struct arc_operand_value *insn_suffixes[MAX_SUFFIXES];
1458
1459 /* Is this opcode supported by the selected cpu? */
1460 if (! arc_opcode_supported (opcode))
1461 continue;
1462
1463 /* Scan the syntax string. If it doesn't match, try the next one. */
1464 arc_opcode_init_insert ();
1465 insn = opcode->value;
1466 fc = 0;
1467 past_opcode_p = 0;
1468 num_suffixes = 0;
1469 limm_reloc_p = 0;
1470 ext_suffix_p = 0;
1471
1472 /* We don't check for (*str != '\0') here because we want to parse
1473 any trailing fake arguments in the syntax string. */
1474 for (str = start, syn = opcode->syntax; *syn != '\0';)
1475 {
1476 int mods;
1477 const struct arc_operand *operand;
1478
1479 /* Non operand chars must match exactly. */
1480 if (*syn != '%' || *++syn == '%')
1481 {
1482 if (*str == *syn)
1483 {
1484 if (*syn == ' ')
1485 past_opcode_p = 1;
1486 ++syn;
1487 ++str;
1488 }
1489 else
1490 break;
1491 continue;
1492 }
1493
1494 /* We have an operand. Pick out any modifiers. */
1495 mods = 0;
1496 while (ARC_MOD_P (arc_operands[arc_operand_map[(int) *syn]].flags))
1497 {
1498 mods |= arc_operands[arc_operand_map[(int) *syn]].flags & ARC_MOD_BITS;
1499 ++syn;
1500 }
1501 operand = arc_operands + arc_operand_map[(int) *syn];
1502 if (operand->fmt == 0)
1503 as_fatal (_("unknown syntax format character `%c'"), *syn);
1504
1505 if (operand->flags & ARC_OPERAND_FAKE)
1506 {
1507 const char *errmsg = NULL;
1508 if (operand->insert)
1509 {
1510 insn = (*operand->insert) (insn, operand, mods, NULL, 0, &errmsg);
1511 if (errmsg != (const char *) NULL)
1512 {
1513 last_errmsg = errmsg;
1514 if (operand->flags & ARC_OPERAND_ERROR)
1515 {
1516 as_bad ("%s", errmsg);
1517 return;
1518 }
1519 else if (operand->flags & ARC_OPERAND_WARN)
1520 as_warn ("%s", errmsg);
1521 break;
1522 }
1523 if (limm_reloc_p
1524 && (operand->flags && operand->flags & ARC_OPERAND_LIMM)
1525 && (operand->flags &
1526 (ARC_OPERAND_ABSOLUTE_BRANCH | ARC_OPERAND_ADDRESS)))
1527 {
1528 fixups[fix_up_at].opindex = arc_operand_map[operand->fmt];
1529 }
1530 }
1531 ++syn;
1532 }
1533 /* Are we finished with suffixes? */
1534 else if (!past_opcode_p)
1535 {
1536 int found;
1537 char c;
1538 char *s, *t;
1539 const struct arc_operand_value *suf, *suffix_end;
1540 const struct arc_operand_value *suffix = NULL;
1541
1542 if (!(operand->flags & ARC_OPERAND_SUFFIX))
1543 abort ();
1544
1545 /* If we're at a space in the input string, we want to skip the
1546 remaining suffixes. There may be some fake ones though, so
1547 just go on to try the next one. */
1548 if (*str == ' ')
1549 {
1550 ++syn;
1551 continue;
1552 }
1553
1554 s = str;
1555 if (mods & ARC_MOD_DOT)
1556 {
1557 if (*s != '.')
1558 break;
1559 ++s;
1560 }
1561 else
1562 {
1563 /* This can happen in "b.nd foo" and we're currently looking
1564 for "%q" (ie: a condition code suffix). */
1565 if (*s == '.')
1566 {
1567 ++syn;
1568 continue;
1569 }
1570 }
1571
1572 /* Pick the suffix out and look it up via the hash table. */
1573 for (t = s; *t && ISALNUM (*t); ++t)
1574 continue;
1575 c = *t;
1576 *t = '\0';
1577 if ((suf = get_ext_suffix (s)))
1578 ext_suffix_p = 1;
1579 else
1580 suf = (const struct arc_operand_value *)
1581 hash_find (arc_suffix_hash, s);
1582 if (!suf)
1583 {
1584 /* This can happen in "blle foo" and we're currently using
1585 the template "b%q%.n %j". The "bl" insn occurs later in
1586 the table so "lle" isn't an illegal suffix. */
1587 *t = c;
1588 break;
1589 }
1590
1591 /* Is it the right type? Note that the same character is used
1592 several times, so we have to examine all of them. This is
1593 relatively efficient as equivalent entries are kept
1594 together. If it's not the right type, don't increment `str'
1595 so we try the next one in the series. */
1596 found = 0;
1597 if (ext_suffix_p && arc_operands[suf->type].fmt == *syn)
1598 {
1599 /* Insert the suffix's value into the insn. */
1600 *t = c;
1601 if (operand->insert)
1602 insn = (*operand->insert) (insn, operand,
1603 mods, NULL, suf->value,
1604 NULL);
1605 else
1606 insn |= suf->value << operand->shift;
1607 suffix = suf;
1608 str = t;
1609 found = 1;
1610 }
1611 else
1612 {
1613 *t = c;
1614 suffix_end = arc_suffixes + arc_suffixes_count;
1615 for (suffix = suf;
1616 suffix < suffix_end && strcmp (suffix->name, suf->name) == 0;
1617 ++suffix)
1618 {
1619 if (arc_operands[suffix->type].fmt == *syn)
1620 {
1621 /* Insert the suffix's value into the insn. */
1622 if (operand->insert)
1623 insn = (*operand->insert) (insn, operand,
1624 mods, NULL, suffix->value,
1625 NULL);
1626 else
1627 insn |= suffix->value << operand->shift;
1628
1629 str = t;
1630 found = 1;
1631 break;
1632 }
1633 }
1634 }
1635 ++syn;
1636 if (!found)
1637 /* Wrong type. Just go on to try next insn entry. */
1638 ;
1639 else
1640 {
1641 if (num_suffixes == MAX_SUFFIXES)
1642 as_bad (_("too many suffixes"));
1643 else
1644 insn_suffixes[num_suffixes++] = suffix;
1645 }
1646 }
1647 else
1648 /* This is either a register or an expression of some kind. */
1649 {
1650 char *hold;
1651 const struct arc_operand_value *reg = NULL;
1652 long value = 0;
1653 expressionS exp;
1654
1655 if (operand->flags & ARC_OPERAND_SUFFIX)
1656 abort ();
1657
1658 /* Is there anything left to parse?
1659 We don't check for this at the top because we want to parse
1660 any trailing fake arguments in the syntax string. */
1661 if (is_end_of_line[(unsigned char) *str])
1662 break;
1663
1664 /* Parse the operand. */
1665 hold = input_line_pointer;
1666 input_line_pointer = str;
1667 expression (&exp);
1668 str = input_line_pointer;
1669 input_line_pointer = hold;
1670
1671 if (exp.X_op == O_illegal)
1672 as_bad (_("illegal operand"));
1673 else if (exp.X_op == O_absent)
1674 as_bad (_("missing operand"));
1675 else if (exp.X_op == O_constant)
1676 value = exp.X_add_number;
1677 else if (exp.X_op == O_register)
1678 reg = (struct arc_operand_value *) exp.X_add_number;
1679 #define IS_REG_DEST_OPERAND(o) ((o) == 'a')
1680 else if (IS_REG_DEST_OPERAND (*syn))
1681 as_bad (_("symbol as destination register"));
1682 else
1683 {
1684 if (!strncmp (str, "@h30", 4))
1685 {
1686 arc_code_symbol (&exp);
1687 str += 4;
1688 }
1689 /* We need to generate a fixup for this expression. */
1690 if (fc >= MAX_FIXUPS)
1691 as_fatal (_("too many fixups"));
1692 fixups[fc].exp = exp;
1693 /* We don't support shimm relocs. break here to force
1694 the assembler to output a limm. */
1695 #define IS_REG_SHIMM_OFFSET(o) ((o) == 'd')
1696 if (IS_REG_SHIMM_OFFSET (*syn))
1697 break;
1698 /* If this is a register constant (IE: one whose
1699 register value gets stored as 61-63) then this
1700 must be a limm. */
1701 /* ??? This bit could use some cleaning up.
1702 Referencing the format chars like this goes
1703 against style. */
1704 if (IS_SYMBOL_OPERAND (*syn))
1705 {
1706 const char *junk;
1707 limm_reloc_p = 1;
1708 /* Save this, we don't yet know what reloc to use. */
1709 fix_up_at = fc;
1710 /* Tell insert_reg we need a limm. This is
1711 needed because the value at this point is
1712 zero, a shimm. */
1713 /* ??? We need a cleaner interface than this. */
1714 (*arc_operands[arc_operand_map['Q']].insert)
1715 (insn, operand, mods, reg, 0L, &junk);
1716 }
1717 else
1718 fixups[fc].opindex = arc_operand_map[(int) *syn];
1719 ++fc;
1720 value = 0;
1721 }
1722
1723 /* Insert the register or expression into the instruction. */
1724 if (operand->insert)
1725 {
1726 const char *errmsg = NULL;
1727 insn = (*operand->insert) (insn, operand, mods,
1728 reg, (long) value, &errmsg);
1729 if (errmsg != (const char *) NULL)
1730 {
1731 last_errmsg = errmsg;
1732 if (operand->flags & ARC_OPERAND_ERROR)
1733 {
1734 as_bad ("%s", errmsg);
1735 return;
1736 }
1737 else if (operand->flags & ARC_OPERAND_WARN)
1738 as_warn ("%s", errmsg);
1739 break;
1740 }
1741 }
1742 else
1743 insn |= (value & ((1 << operand->bits) - 1)) << operand->shift;
1744
1745 ++syn;
1746 }
1747 }
1748
1749 /* If we're at the end of the syntax string, we're done. */
1750 /* FIXME: try to move this to a separate function. */
1751 if (*syn == '\0')
1752 {
1753 int i;
1754 char *f;
1755 long limm, limm_p;
1756
1757 /* For the moment we assume a valid `str' can only contain blanks
1758 now. IE: We needn't try again with a longer version of the
1759 insn and it is assumed that longer versions of insns appear
1760 before shorter ones (eg: lsr r2,r3,1 vs lsr r2,r3). */
1761
1762 while (ISSPACE (*str))
1763 ++str;
1764
1765 if (!is_end_of_line[(unsigned char) *str])
1766 as_bad (_("junk at end of line: `%s'"), str);
1767
1768 /* Is there a limm value? */
1769 limm_p = arc_opcode_limm_p (&limm);
1770
1771 /* Perform various error and warning tests. */
1772
1773 {
1774 static int in_delay_slot_p = 0;
1775 static int prev_insn_needs_cc_nop_p = 0;
1776 /* delay slot type seen */
1777 int delay_slot_type = ARC_DELAY_NONE;
1778 /* conditional execution flag seen */
1779 int conditional = 0;
1780 /* 1 if condition codes are being set */
1781 int cc_set_p = 0;
1782 /* 1 if conditional branch, including `b' "branch always" */
1783 int cond_branch_p = opcode->flags & ARC_OPCODE_COND_BRANCH;
1784
1785 for (i = 0; i < num_suffixes; ++i)
1786 {
1787 switch (arc_operands[insn_suffixes[i]->type].fmt)
1788 {
1789 case 'n':
1790 delay_slot_type = insn_suffixes[i]->value;
1791 break;
1792 case 'q':
1793 conditional = insn_suffixes[i]->value;
1794 break;
1795 case 'f':
1796 cc_set_p = 1;
1797 break;
1798 }
1799 }
1800
1801 /* Putting an insn with a limm value in a delay slot is supposed to
1802 be legal, but let's warn the user anyway. Ditto for 8 byte
1803 jumps with delay slots. */
1804 if (in_delay_slot_p && limm_p)
1805 as_warn (_("8 byte instruction in delay slot"));
1806 if (delay_slot_type != ARC_DELAY_NONE
1807 && limm_p && arc_insn_not_jl (insn)) /* except for jl addr */
1808 as_warn (_("8 byte jump instruction with delay slot"));
1809 in_delay_slot_p = (delay_slot_type != ARC_DELAY_NONE) && !limm_p;
1810
1811 /* Warn when a conditional branch immediately follows a set of
1812 the condition codes. Note that this needn't be done if the
1813 insn that sets the condition codes uses a limm. */
1814 if (cond_branch_p && conditional != 0 /* 0 = "always" */
1815 && prev_insn_needs_cc_nop_p && arc_mach_type == bfd_mach_arc_5)
1816 as_warn (_("conditional branch follows set of flags"));
1817 prev_insn_needs_cc_nop_p =
1818 /* FIXME: ??? not required:
1819 (delay_slot_type != ARC_DELAY_NONE) && */
1820 cc_set_p && !limm_p;
1821 }
1822
1823 /* Write out the instruction.
1824 It is important to fetch enough space in one call to `frag_more'.
1825 We use (f - frag_now->fr_literal) to compute where we are and we
1826 don't want frag_now to change between calls. */
1827 if (limm_p)
1828 {
1829 f = frag_more (8);
1830 md_number_to_chars (f, insn, 4);
1831 md_number_to_chars (f + 4, limm, 4);
1832 dwarf2_emit_insn (8);
1833 }
1834 else if (limm_reloc_p)
1835 /* We need a limm reloc, but the tables think we don't. */
1836 abort ();
1837 else
1838 {
1839 f = frag_more (4);
1840 md_number_to_chars (f, insn, 4);
1841 dwarf2_emit_insn (4);
1842 }
1843
1844 /* Create any fixups. */
1845 for (i = 0; i < fc; ++i)
1846 {
1847 int op_type, reloc_type;
1848 expressionS exptmp;
1849 const struct arc_operand *operand;
1850
1851 /* Create a fixup for this operand.
1852 At this point we do not use a bfd_reloc_code_real_type for
1853 operands residing in the insn, but instead just use the
1854 operand index. This lets us easily handle fixups for any
1855 operand type, although that is admittedly not a very exciting
1856 feature. We pick a BFD reloc type in md_apply_fix.
1857
1858 Limm values (4 byte immediate "constants") must be treated
1859 normally because they're not part of the actual insn word
1860 and thus the insertion routines don't handle them. */
1861
1862 if (arc_operands[fixups[i].opindex].flags & ARC_OPERAND_LIMM)
1863 {
1864 /* Modify the fixup addend as required by the cpu. */
1865 fixups[i].exp.X_add_number += arc_limm_fixup_adjust (insn);
1866 op_type = fixups[i].opindex;
1867 /* FIXME: can we add this data to the operand table? */
1868 if (op_type == arc_operand_map['L']
1869 || op_type == arc_operand_map['s']
1870 || op_type == arc_operand_map['o']
1871 || op_type == arc_operand_map['O'])
1872 reloc_type = BFD_RELOC_32;
1873 else if (op_type == arc_operand_map['J'])
1874 reloc_type = BFD_RELOC_ARC_B26;
1875 else
1876 abort ();
1877 reloc_type = get_arc_exp_reloc_type (1, reloc_type,
1878 &fixups[i].exp,
1879 &exptmp);
1880 }
1881 else
1882 {
1883 op_type = get_arc_exp_reloc_type (0, fixups[i].opindex,
1884 &fixups[i].exp, &exptmp);
1885 reloc_type = op_type + (int) BFD_RELOC_UNUSED;
1886 }
1887 operand = &arc_operands[op_type];
1888 fix_new_exp (frag_now,
1889 ((f - frag_now->fr_literal)
1890 + (operand->flags & ARC_OPERAND_LIMM ? 4 : 0)), 4,
1891 &exptmp,
1892 (operand->flags & ARC_OPERAND_RELATIVE_BRANCH) != 0,
1893 (bfd_reloc_code_real_type) reloc_type);
1894 }
1895 return;
1896 }
1897 }
1898
1899 if (NULL == last_errmsg)
1900 as_bad (_("bad instruction `%s'"), start);
1901 else
1902 as_bad ("%s", last_errmsg);
1903 }
1904