]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - gas/config/tc-arc.c
* config/tc-arc.c (delay_slot_type): New function.
[thirdparty/binutils-gdb.git] / gas / config / tc-arc.c
1 /* tc-arc.c -- Assembler for the ARC
2 Copyright (C) 1994, 1995 Free Software Foundation, Inc.
3 Contributed by Doug Evans (dje@cygnus.com).
4
5 This file is part of GAS, the GNU Assembler.
6
7 GAS is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
10 any later version.
11
12 GAS is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with GAS; see the file COPYING. If not, write to
19 the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
20
21 #include <stdio.h>
22 #include <ctype.h>
23 #include "as.h"
24 #include "subsegs.h"
25 #include "opcode/arc.h"
26 #include "elf/arc.h"
27
28 extern int arc_get_mach PARAMS ((char *));
29
30 static arc_insn arc_insert_operand PARAMS ((arc_insn,
31 const struct arc_operand *, int,
32 const struct arc_operand_value *,
33 offsetT, char *, unsigned int));
34 static int delay_slot_type PARAMS ((arc_insn));
35 static void arc_common PARAMS ((int));
36 static void arc_cpu PARAMS ((int));
37 /*static void arc_rename PARAMS ((int));*/
38 static int get_arc_exp_reloc_type PARAMS ((int, int, expressionS *,
39 expressionS *));
40
41 const pseudo_typeS md_pseudo_table[] =
42 {
43 { "align", s_align_bytes, 0 }, /* Defaulting is invalid (0) */
44 { "common", arc_common, 0 },
45 /*{ "hword", cons, 2 }, - already exists */
46 { "word", cons, 4 },
47 /*{ "xword", cons, 8 },*/
48 { "cpu", arc_cpu, 0 },
49 /*{ "rename", arc_rename, 0 },*/
50 { NULL, 0, 0 },
51 };
52
53 /* This array holds the chars that always start a comment. If the
54 pre-processor is disabled, these aren't very useful */
55 const char comment_chars[] = "#;";
56
57 /* This array holds the chars that only start a comment at the beginning of
58 a line. If the line seems to have the form '# 123 filename'
59 .line and .file directives will appear in the pre-processed output */
60 /* Note that input_file.c hand checks for '#' at the beginning of the
61 first line of the input file. This is because the compiler outputs
62 #NO_APP at the beginning of its output. */
63 /* Also note that comments started like this one will always
64 work if '/' isn't otherwise defined. */
65 const char line_comment_chars[] = "#";
66
67 const char line_separator_chars[] = "";
68
69 /* Chars that can be used to separate mant from exp in floating point nums */
70 const char EXP_CHARS[] = "eE";
71
72 /* Chars that mean this number is a floating point constant */
73 /* As in 0f12.456 */
74 /* or 0d1.2345e12 */
75 const char FLT_CHARS[] = "rRsSfFdD";
76
77 /* Byte order. */
78 extern int target_big_endian;
79 const char *arc_target_format = DEFAULT_TARGET_FORMAT;
80 static int byte_order;
81
82 /* One of bfd_mach_arc_xxx. */
83 static int arc_mach_type = bfd_mach_arc_base;
84
85 /* Non-zero if the cpu type has been explicitly specified. */
86 static int mach_type_specified_p = 0;
87
88 /* Non-zero if opcode tables have been initialized.
89 A .cpu command must appear before any instructions. */
90 static int cpu_tables_init_p = 0;
91
92 static struct hash_control *arc_suffix_hash = NULL;
93 \f
94 const char *md_shortopts = "";
95 struct option md_longopts[] =
96 {
97 #define OPTION_EB (OPTION_MD_BASE + 0)
98 {"EB", no_argument, NULL, OPTION_EB},
99 #define OPTION_EL (OPTION_MD_BASE + 1)
100 {"EL", no_argument, NULL, OPTION_EL},
101 { NULL, no_argument, NULL, 0 }
102 };
103 size_t md_longopts_size = sizeof (md_longopts);
104
105 /*
106 * md_parse_option
107 *
108 * Invocation line includes a switch not recognized by the base assembler.
109 * See if it's a processor-specific option.
110 */
111
112 int
113 md_parse_option (c, arg)
114 int c;
115 char *arg;
116 {
117 switch (c)
118 {
119 case OPTION_EB:
120 byte_order = BIG_ENDIAN;
121 arc_target_format = "elf32-bigarc";
122 break;
123 case OPTION_EL:
124 byte_order = LITTLE_ENDIAN;
125 arc_target_format = "elf32-littlearc";
126 break;
127 default:
128 return 0;
129 }
130 return 1;
131 }
132
133 void
134 md_show_usage (stream)
135 FILE *stream;
136 {
137 fprintf (stream, "\
138 ARC options:\n\
139 -EB generate big endian output\n\
140 -EL generate little endian output\n");
141 }
142
143 /* This function is called once, at assembler startup time. It should
144 set up all the tables, etc. that the MD part of the assembler will need.
145 Opcode selection is defered until later because we might see a .cpu
146 command. */
147
148 void
149 md_begin ()
150 {
151 /* The endianness can be chosen "at the factory". */
152 target_big_endian = byte_order == BIG_ENDIAN;
153
154 if (!bfd_set_arch_mach (stdoutput, bfd_arch_arc, arc_mach_type))
155 as_warn ("could not set architecture and machine");
156
157 /* Assume the base cpu. This call is necessary because we need to
158 initialize `arc_operand_map' which may be needed before we see the
159 first insn. */
160 arc_opcode_init_tables (arc_get_opcode_mach (bfd_mach_arc_base,
161 target_big_endian));
162 }
163
164 /* Initialize the various opcode and operand tables.
165 MACH is one of bfd_mach_arc_xxx. */
166
167 static void
168 init_opcode_tables (mach)
169 int mach;
170 {
171 register unsigned int i;
172 char *last;
173
174 if ((arc_suffix_hash = hash_new ()) == NULL)
175 as_fatal ("virtual memory exhausted");
176
177 if (!bfd_set_arch_mach (stdoutput, bfd_arch_arc, mach))
178 as_warn ("could not set architecture and machine");
179
180 /* This initializes a few things in arc-opc.c that we need.
181 This must be called before the various arc_xxx_supported fns. */
182 arc_opcode_init_tables (arc_get_opcode_mach (mach, target_big_endian));
183
184 /* Only put the first entry of each equivalently named suffix in the
185 table. */
186 last = "";
187 for (i = 0; i < arc_suffixes_count; i++)
188 {
189 if (! arc_opval_supported (&arc_suffixes[i]))
190 continue;
191 if (strcmp (arc_suffixes[i].name, last) != 0)
192 hash_insert (arc_suffix_hash, arc_suffixes[i].name, (PTR) (arc_suffixes + i));
193 last = arc_suffixes[i].name;
194 }
195
196 /* Since registers don't have a prefix, we put them in the symbol table so
197 they can't be used as symbols. This also simplifies argument parsing as
198 we can let gas parse registers for us. The recorded register number is
199 the index in `arc_reg_names'. */
200 for (i = 0; i < arc_reg_names_count; i++)
201 {
202 if (! arc_opval_supported (&arc_reg_names[i]))
203 continue;
204 /* Use symbol_create here instead of symbol_new so we don't try to
205 output registers into the object file's symbol table. */
206 symbol_table_insert (symbol_create (arc_reg_names[i].name, reg_section,
207 i, &zero_address_frag));
208 }
209
210 /* Tell `s_cpu' it's too late. */
211 cpu_tables_init_p = 1;
212 }
213 \f
214 /* Insert an operand value into an instruction.
215 If REG is non-NULL, it is a register number and ignore VAL. */
216
217 static arc_insn
218 arc_insert_operand (insn, operand, mods, reg, val, file, line)
219 arc_insn insn;
220 const struct arc_operand *operand;
221 int mods;
222 const struct arc_operand_value *reg;
223 offsetT val;
224 char *file;
225 unsigned int line;
226 {
227 if (operand->bits != 32)
228 {
229 long min, max;
230 offsetT test;
231
232 if ((operand->flags & ARC_OPERAND_SIGNED) != 0)
233 {
234 if ((operand->flags & ARC_OPERAND_SIGNOPT) != 0)
235 max = (1 << operand->bits) - 1;
236 else
237 max = (1 << (operand->bits - 1)) - 1;
238 min = - (1 << (operand->bits - 1));
239 }
240 else
241 {
242 max = (1 << operand->bits) - 1;
243 min = 0;
244 }
245
246 if ((operand->flags & ARC_OPERAND_NEGATIVE) != 0)
247 test = - val;
248 else
249 test = val;
250
251 if (test < (offsetT) min || test > (offsetT) max)
252 {
253 const char *err =
254 "operand out of range (%s not between %ld and %ld)";
255 char buf[100];
256
257 sprint_value (buf, test);
258 if (file == (char *) NULL)
259 as_warn (err, buf, min, max);
260 else
261 as_warn_where (file, line, err, buf, min, max);
262 }
263 }
264
265 if (operand->insert)
266 {
267 const char *errmsg;
268
269 errmsg = NULL;
270 insn = (*operand->insert) (insn, operand, mods, reg, (long) val, &errmsg);
271 if (errmsg != (const char *) NULL)
272 as_warn (errmsg);
273 }
274 else
275 insn |= (((long) val & ((1 << operand->bits) - 1))
276 << operand->shift);
277
278 return insn;
279 }
280
281 /* Return the delay slot type for INSN (or -1 if it isn't a branch). */
282 /* ??? This is a quick hack. May wish to later generalize this concept
283 to record all suffixes the insn contains (for various warnings, etc.). */
284
285 static int
286 delay_slot_type (arc_insn insn)
287 {
288 if ((insn >> 27) >= 4 && (insn >> 27) <= 7)
289 return (insn & 0x60) >> 5;
290 return -1;
291 }
292
293 /* We need to keep a list of fixups. We can't simply generate them as
294 we go, because that would require us to first create the frag, and
295 that would screw up references to ``.''. */
296
297 struct arc_fixup
298 {
299 /* index into `arc_operands' */
300 int opindex;
301 expressionS exp;
302 };
303
304 #define MAX_INSN_FIXUPS 5
305
306 /* This routine is called for each instruction to be assembled. */
307
308 void
309 md_assemble (str)
310 char *str;
311 {
312 const struct arc_opcode *opcode;
313 char *start;
314 arc_insn insn;
315 static int init_tables_p = 0;
316
317 /* Opcode table initialization is deferred until here because we have to
318 wait for a possible .cpu command. */
319 if (!init_tables_p)
320 {
321 init_opcode_tables (arc_mach_type);
322 init_tables_p = 1;
323 }
324
325 /* Skip leading white space. */
326 while (isspace (*str))
327 str++;
328
329 /* The instructions are stored in lists hashed by the first letter (though
330 we needn't care how they're hashed). Get the first in the list. */
331
332 opcode = arc_opcode_asm_list (str);
333
334 /* Keep looking until we find a match. */
335
336 start = str;
337 for ( ; opcode != NULL; opcode = ARC_OPCODE_NEXT_ASM (opcode))
338 {
339 int past_opcode_p;
340 char *syn;
341 struct arc_fixup fixups[MAX_INSN_FIXUPS];
342 int fc,limm_reloc_p;
343
344 /* Is this opcode supported by the selected cpu? */
345 if (! arc_opcode_supported (opcode))
346 continue;
347
348 /* Scan the syntax string. If it doesn't match, try the next one. */
349
350 arc_opcode_init_insert ();
351 insn = opcode->value;
352 fc = 0;
353 past_opcode_p = 0;
354
355 /* Used as a sanity check. If we need a limm reloc, make sure we ask
356 for an extra 4 bytes from frag_more. */
357 limm_reloc_p = 0;
358
359 /* We don't check for (*str != '\0') here because we want to parse
360 any trailing fake arguments in the syntax string. */
361 for (str = start, syn = opcode->syntax; *syn != '\0'; )
362 {
363 int mods;
364 const struct arc_operand *operand;
365
366 /* Non operand chars must match exactly. */
367 if (*syn != '%' || *++syn == '%')
368 {
369 /* Handle '+' specially as we want to allow "ld r0,[sp-4]". */
370 /* ??? The syntax has changed to [sp,-4]. */
371 if (0 && *syn == '+' && *str == '-')
372 {
373 /* Skip over syn's +, but leave str's - alone.
374 That makes the case identical to "ld r0,[sp+-4]". */
375 ++syn;
376 }
377 else if (*str == *syn)
378 {
379 if (*syn == ' ')
380 past_opcode_p = 1;
381 ++syn;
382 ++str;
383 }
384 else
385 break;
386 continue;
387 }
388
389 /* We have an operand. Pick out any modifiers. */
390 mods = 0;
391 while (ARC_MOD_P (arc_operands[arc_operand_map[*syn]].flags))
392 {
393 mods |= arc_operands[arc_operand_map[*syn]].flags & ARC_MOD_BITS;
394 ++syn;
395 }
396 operand = arc_operands + arc_operand_map[*syn];
397 if (operand->fmt == 0)
398 as_fatal ("unknown syntax format character `%c'", *syn);
399
400 if (operand->flags & ARC_OPERAND_FAKE)
401 {
402 const char *errmsg = NULL;
403 if (operand->insert)
404 {
405 insn = (*operand->insert) (insn, operand, mods, NULL, 0, &errmsg);
406 /* If we get an error, go on to try the next insn. */
407 if (errmsg)
408 break;
409 }
410 ++syn;
411 }
412 /* Are we finished with suffixes? */
413 else if (!past_opcode_p)
414 {
415 int found;
416 char c;
417 char *s,*t;
418 const struct arc_operand_value *suf,*suffix,*suffix_end;
419
420 if (!(operand->flags & ARC_OPERAND_SUFFIX))
421 abort ();
422
423 /* If we're at a space in the input string, we want to skip the
424 remaining suffixes. There may be some fake ones though, so
425 just go on to try the next one. */
426 if (*str == ' ')
427 {
428 ++syn;
429 continue;
430 }
431
432 s = str;
433 if (mods & ARC_MOD_DOT)
434 {
435 if (*s != '.')
436 break;
437 ++s;
438 }
439 else
440 {
441 /* This can happen in "b.nd foo" and we're currently looking
442 for "%q" (ie: a condition code suffix). */
443 if (*s == '.')
444 {
445 ++syn;
446 continue;
447 }
448 }
449
450 /* Pick the suffix out and look it up via the hash table. */
451 for (t = s; *t && isalpha (*t); ++t)
452 continue;
453 c = *t;
454 *t = '\0';
455 suf = hash_find (arc_suffix_hash, s);
456 *t = c;
457 if (!suf)
458 {
459 /* This can happen in "blle foo" and we're currently using
460 the template "b%q%.n %j". The "bl" insn occurs later in
461 the table so "lle" isn't an illegal suffix. */
462 break;
463 }
464
465 /* Is it the right type? Note that the same character is used
466 several times, so we have to examine all of them. This is
467 relatively efficient as equivalent entries are kept
468 together. If it's not the right type, don't increment `str'
469 so we try the next one in the series. */
470 found = 0;
471 suffix_end = arc_suffixes + arc_suffixes_count;
472 for (suffix = suf;
473 suffix < suffix_end && strcmp (suffix->name, suf->name) == 0;
474 suffix++)
475 {
476 if (arc_operands[suffix->type].fmt == *syn)
477 {
478 /* Insert the suffix's value into the insn. */
479 if (operand->insert)
480 insn = (*operand->insert) (insn, operand,
481 mods, NULL, suffix->value,
482 NULL);
483 else
484 insn |= suffix->value << operand->shift;
485
486 str = t;
487 found = 1;
488 break;
489 }
490 }
491 ++syn;
492 if (!found)
493 /* There's nothing to do except go on to try the next one.
494 ??? This test can be deleted when we're done. */
495 ;
496 }
497 else
498 /* This is either a register or an expression of some kind. */
499 {
500 char c;
501 char *hold;
502 const struct arc_operand_value *reg = NULL;
503 long value = 0;
504 expressionS exp;
505
506 if (operand->flags & ARC_OPERAND_SUFFIX)
507 abort ();
508
509 /* Is there anything left to parse?
510 We don't check for this at the top because we want to parse
511 any trailing fake arguments in the syntax string. */
512 if (*str == '\0')
513 break;
514 #if 0
515 /* Is this a syntax character? Eg: is there a '[' present when
516 there shouldn't be? */
517 if (!isalnum (*str)
518 /* '.' as in ".LLC0" */
519 && *str != '.'
520 /* '_' as in "_print" */
521 && *str != '_'
522 /* '-' as in "[fp,-4]" */
523 && *str != '-'
524 /* '%' as in "%ia(_func)" */
525 && *str != '%')
526 break;
527 #endif
528
529 /* Parse the operand. */
530 hold = input_line_pointer;
531 input_line_pointer = str;
532 expression (&exp);
533 str = input_line_pointer;
534 input_line_pointer = hold;
535
536 if (exp.X_op == O_illegal)
537 as_bad ("illegal operand");
538 else if (exp.X_op == O_absent)
539 as_bad ("missing operand");
540 else if (exp.X_op == O_constant)
541 {
542 value = exp.X_add_number;
543 }
544 else if (exp.X_op == O_register)
545 {
546 reg = arc_reg_names + exp.X_add_number;
547 }
548 else
549 {
550 /* We need to generate a fixup for this expression. */
551 if (fc >= MAX_INSN_FIXUPS)
552 as_fatal ("too many fixups");
553 fixups[fc].exp = exp;
554
555 /* If this is a register constant (IE: one whose
556 register value gets stored as 61-63) then this
557 must be a limm. We don't support shimm relocs. */
558 /* ??? This bit could use some cleaning up.
559 Referencing the format chars like this goes
560 against style. */
561 #define IS_REG_OPERAND(o) ((o) == 'a' || (o) == 'b' || (o) == 'c')
562 if (IS_REG_OPERAND (*syn))
563 {
564 const char *junk;
565
566 fixups[fc].opindex = arc_operand_map['L'];
567 limm_reloc_p = 1;
568 /* Tell insert_reg we need a limm. This is
569 needed because the value at this point is
570 zero, a shimm. */
571 /* ??? We need a cleaner interface than this. */
572 (*arc_operands[arc_operand_map['Q']].insert)
573 (insn, operand, mods, reg, 0L, &junk);
574 }
575 else
576 fixups[fc].opindex = arc_operand_map[*syn];
577 ++fc;
578 value = 0;
579 }
580
581 /* Insert the register or expression into the instruction. */
582 if (operand->insert)
583 {
584 const char *errmsg = NULL;
585 insn = (*operand->insert) (insn, operand, mods,
586 reg, (long) value, &errmsg);
587 #if 0
588 if (errmsg != (const char *) NULL)
589 as_warn (errmsg);
590 #endif
591 /* FIXME: We want to try shimm insns for limm ones. But if
592 the constant won't fit, we must go on to try the next
593 possibility. Where do we issue warnings for constants
594 that are too big then? At present, we'll flag the insn
595 as unrecognizable! Maybe have the "bad instruction"
596 error message include our `errmsg'? */
597 if (errmsg != (const char *) NULL)
598 break;
599 }
600 else
601 insn |= (value & ((1 << operand->bits) - 1)) << operand->shift;
602
603 ++syn;
604 }
605 }
606
607 /* If we're at the end of the syntax string, we're done. */
608 if (*syn == '\0')
609 {
610 int i;
611 char *f;
612 long limm, limm_p;
613
614 /* For the moment we assume a valid `str' can only contain blanks
615 now. IE: We needn't try again with a longer version of the
616 insn and it is assumed that longer versions of insns appear
617 before shorter ones (eg: lsr r2,r3,1 vs lsr r2,r3). */
618
619 while (isspace (*str))
620 ++str;
621
622 if (*str != '\0')
623 as_bad ("junk at end of line: `%s'", str);
624
625 /* Is there a limm value? */
626 limm_p = arc_opcode_limm_p (&limm);
627
628 /* Putting an insn with a limm value in a delay slot is supposed to
629 be legal, but let's warn the user anyway. Ditto for 8 byte jumps
630 with delay slots. */
631 {
632 static int in_delay_slot_p = 0;
633 int this_insn_delay_slot_type = delay_slot_type (insn);
634
635 if (in_delay_slot_p && limm_p)
636 as_warn ("8 byte instruction in delay slot");
637 if (this_insn_delay_slot_type > 0 && limm_p)
638 as_warn ("8 byte jump instruction with delay slot");
639 in_delay_slot_p = (this_insn_delay_slot_type > 0) && !limm_p;
640 }
641
642 /* Write out the instruction.
643 It is important to fetch enough space in one call to `frag_more'.
644 We use (f - frag_now->fr_literal) to compute where we are and we
645 don't want frag_now to change between calls. */
646 if (limm_p)
647 {
648 f = frag_more (8);
649 md_number_to_chars (f, insn, 4);
650 md_number_to_chars (f + 4, limm, 4);
651 }
652 else if (limm_reloc_p)
653 {
654 /* We need a limm reloc, but the tables think we don't. */
655 abort ();
656 }
657 else
658 {
659 f = frag_more (4);
660 md_number_to_chars (f, insn, 4);
661 }
662
663 /* Create any fixups. */
664 for (i = 0; i < fc; ++i)
665 {
666 int op_type, reloc_type;
667 expressionS exptmp;
668 const struct arc_operand *operand;
669
670 /* Create a fixup for this operand.
671 At this point we do not use a bfd_reloc_code_real_type for
672 operands residing in the insn, but instead just use the
673 operand index. This lets us easily handle fixups for any
674 operand type, although that is admittedly not a very exciting
675 feature. We pick a BFD reloc type in md_apply_fix.
676
677 Limm values (4 byte immediate "constants") must be treated
678 normally because they're not part of the actual insn word
679 and thus the insertion routines don't handle them. */
680
681 if (arc_operands[fixups[i].opindex].flags & ARC_OPERAND_LIMM)
682 {
683 op_type = fixups[i].opindex;
684 /* FIXME: can we add this data to the operand table? */
685 if (op_type == arc_operand_map['L'])
686 reloc_type = BFD_RELOC_32;
687 else if (op_type == arc_operand_map['J'])
688 reloc_type = BFD_RELOC_ARC_B26;
689 else
690 abort ();
691 reloc_type = get_arc_exp_reloc_type (1, reloc_type,
692 &fixups[i].exp,
693 &exptmp);
694 }
695 else
696 {
697 op_type = get_arc_exp_reloc_type (0, fixups[i].opindex,
698 &fixups[i].exp, &exptmp);
699 reloc_type = op_type + (int) BFD_RELOC_UNUSED;
700 }
701 operand = &arc_operands[op_type];
702 fix_new_exp (frag_now,
703 ((f - frag_now->fr_literal)
704 + (operand->flags & ARC_OPERAND_LIMM ? 4 : 0)), 4,
705 &exptmp,
706 (operand->flags & ARC_OPERAND_RELATIVE_BRANCH) != 0,
707 (bfd_reloc_code_real_type) reloc_type);
708 }
709
710 /* All done. */
711 return;
712 }
713
714 /* Try the next entry. */
715 }
716
717 as_bad ("bad instruction `%s'", start);
718 }
719 \f
720 /* ??? This was copied from tc-sparc.c, I think. Is it necessary? */
721
722 static void
723 arc_common (ignore)
724 int ignore;
725 {
726 char *name;
727 char c;
728 char *p;
729 int temp, size;
730 symbolS *symbolP;
731
732 name = input_line_pointer;
733 c = get_symbol_end ();
734 /* just after name is now '\0' */
735 p = input_line_pointer;
736 *p = c;
737 SKIP_WHITESPACE ();
738 if (*input_line_pointer != ',')
739 {
740 as_bad ("expected comma after symbol-name");
741 ignore_rest_of_line ();
742 return;
743 }
744 input_line_pointer++; /* skip ',' */
745 if ((temp = get_absolute_expression ()) < 0)
746 {
747 as_bad (".COMMon length (%d.) <0! Ignored.", temp);
748 ignore_rest_of_line ();
749 return;
750 }
751 size = temp;
752 *p = 0;
753 symbolP = symbol_find_or_make (name);
754 *p = c;
755 if (S_IS_DEFINED (symbolP))
756 {
757 as_bad ("ignoring attempt to re-define symbol");
758 ignore_rest_of_line ();
759 return;
760 }
761 if (S_GET_VALUE (symbolP) != 0)
762 {
763 if (S_GET_VALUE (symbolP) != size)
764 {
765 as_warn ("Length of .comm \"%s\" is already %ld. Not changed to %d.",
766 S_GET_NAME (symbolP), (long) S_GET_VALUE (symbolP), size);
767 }
768 }
769 assert (symbolP->sy_frag == &zero_address_frag);
770 if (*input_line_pointer != ',')
771 {
772 as_bad ("expected comma after common length");
773 ignore_rest_of_line ();
774 return;
775 }
776 input_line_pointer++;
777 SKIP_WHITESPACE ();
778 if (*input_line_pointer != '"')
779 {
780 temp = get_absolute_expression ();
781 if (temp < 0)
782 {
783 temp = 0;
784 as_warn ("Common alignment negative; 0 assumed");
785 }
786 if (symbolP->local)
787 {
788 segT old_sec;
789 int old_subsec;
790 char *p;
791 int align;
792
793 allocate_bss:
794 old_sec = now_seg;
795 old_subsec = now_subseg;
796 align = temp;
797 record_alignment (bss_section, align);
798 subseg_set (bss_section, 0);
799 if (align)
800 frag_align (align, 0);
801 if (S_GET_SEGMENT (symbolP) == bss_section)
802 symbolP->sy_frag->fr_symbol = 0;
803 symbolP->sy_frag = frag_now;
804 p = frag_var (rs_org, 1, 1, (relax_substateT) 0, symbolP, size,
805 (char *) 0);
806 *p = 0;
807 S_SET_SEGMENT (symbolP, bss_section);
808 S_CLEAR_EXTERNAL (symbolP);
809 subseg_set (old_sec, old_subsec);
810 }
811 else
812 {
813 allocate_common:
814 S_SET_VALUE (symbolP, (valueT) size);
815 S_SET_ALIGN (symbolP, temp);
816 S_SET_EXTERNAL (symbolP);
817 /* should be common, but this is how gas does it for now */
818 S_SET_SEGMENT (symbolP, bfd_und_section_ptr);
819 }
820 }
821 else
822 {
823 input_line_pointer++;
824 /* ??? Some say data, some say bss. */
825 if (strncmp (input_line_pointer, ".bss\"", 5)
826 && strncmp (input_line_pointer, ".data\"", 6))
827 {
828 input_line_pointer--;
829 goto bad_common_segment;
830 }
831 while (*input_line_pointer++ != '"')
832 ;
833 goto allocate_common;
834 }
835 demand_empty_rest_of_line ();
836 return;
837
838 {
839 bad_common_segment:
840 p = input_line_pointer;
841 while (*p && *p != '\n')
842 p++;
843 c = *p;
844 *p = '\0';
845 as_bad ("bad .common segment %s", input_line_pointer + 1);
846 *p = c;
847 input_line_pointer = p;
848 ignore_rest_of_line ();
849 return;
850 }
851 }
852
853 /* Select the cpu we're assembling for. */
854
855 static void
856 arc_cpu (ignore)
857 int ignore;
858 {
859 int mach;
860 char c;
861 char *cpu;
862
863 /* If an instruction has already been seen, it's too late. */
864 if (cpu_tables_init_p)
865 {
866 as_bad (".cpu command must appear before any instructions");
867 ignore_rest_of_line ();
868 return;
869 }
870
871 cpu = input_line_pointer;
872 c = get_symbol_end ();
873 mach = arc_get_mach (cpu);
874 *input_line_pointer = c;
875 if (mach == -1)
876 goto bad_cpu;
877
878 demand_empty_rest_of_line ();
879
880 /* The cpu may have been selected on the command line.
881 The choices must match. */
882 /* ??? This was a command line option early on. It's gone now, but
883 leave this in. */
884 if (mach_type_specified_p && mach != arc_mach_type)
885 as_bad (".cpu conflicts with previous value");
886 else
887 {
888 arc_mach_type = mach;
889 mach_type_specified_p = 1;
890 if (!bfd_set_arch_mach (stdoutput, bfd_arch_arc, mach))
891 as_warn ("could not set architecture and machine");
892 }
893 return;
894
895 bad_cpu:
896 as_bad ("bad .cpu op");
897 ignore_rest_of_line ();
898 }
899
900 #if 0
901 /* The .rename pseudo-op. This is used by gcc to implement
902 -mmangle-cpu-libgcc. */
903
904 static void
905 arc_rename (ignore)
906 int ignore;
907 {
908 char *name,*new;
909 char c;
910 symbolS *sym;
911 int len;
912
913 name = input_line_pointer;
914 c = get_symbol_end ();
915 sym = symbol_find_or_make (name);
916 *input_line_pointer = c;
917
918 if (*input_line_pointer != ',')
919 {
920 as_bad ("missing rename string");
921 ignore_rest_of_line ();
922 return;
923 }
924 ++input_line_pointer;
925 SKIP_WHITESPACE ();
926
927 name = input_line_pointer;
928 c = get_symbol_end ();
929 if (*name == '\0')
930 {
931 *input_line_pointer = c;
932 as_bad ("invalid symbol to rename to");
933 ignore_rest_of_line ();
934 return;
935 }
936 new = (char *) xmalloc (strlen (name) + 1);
937 strcpy (new, name);
938 *input_line_pointer = c;
939 sym->sy_tc.real_name = new;
940
941 demand_empty_rest_of_line ();
942 }
943 #endif
944 \f
945 /* Turn a string in input_line_pointer into a floating point constant of type
946 type, and store the appropriate bytes in *litP. The number of LITTLENUMS
947 emitted is stored in *sizeP.
948 An error message is returned, or NULL on OK. */
949
950 /* Equal to MAX_PRECISION in atof-ieee.c */
951 #define MAX_LITTLENUMS 6
952
953 char *
954 md_atof (type, litP, sizeP)
955 char type;
956 char *litP;
957 int *sizeP;
958 {
959 int prec;
960 LITTLENUM_TYPE words[MAX_LITTLENUMS];
961 LITTLENUM_TYPE *wordP;
962 char *t;
963 char *atof_ieee ();
964
965 switch (type)
966 {
967 case 'f':
968 case 'F':
969 prec = 2;
970 break;
971
972 case 'd':
973 case 'D':
974 prec = 4;
975 break;
976
977 default:
978 *sizeP = 0;
979 return "bad call to md_atof";
980 }
981
982 t = atof_ieee (input_line_pointer, type, words);
983 if (t)
984 input_line_pointer = t;
985 *sizeP = prec * sizeof (LITTLENUM_TYPE);
986 for (wordP = words; prec--;)
987 {
988 md_number_to_chars (litP, (valueT) (*wordP++), sizeof (LITTLENUM_TYPE));
989 litP += sizeof (LITTLENUM_TYPE);
990 }
991
992 return NULL;
993 }
994
995 /* Write a value out to the object file, using the appropriate
996 endianness. */
997
998 void
999 md_number_to_chars (buf, val, n)
1000 char *buf;
1001 valueT val;
1002 int n;
1003 {
1004 if (target_big_endian)
1005 number_to_chars_bigendian (buf, val, n);
1006 else
1007 number_to_chars_littleendian (buf, val, n);
1008 }
1009
1010 /* Round up a section size to the appropriate boundary. */
1011
1012 valueT
1013 md_section_align (segment, size)
1014 segT segment;
1015 valueT size;
1016 {
1017 int align = bfd_get_section_alignment (stdoutput, segment);
1018
1019 return ((size + (1 << align) - 1) & (-1 << align));
1020 }
1021
1022 /* We don't have any form of relaxing. */
1023
1024 int
1025 md_estimate_size_before_relax (fragp, seg)
1026 fragS *fragp;
1027 asection *seg;
1028 {
1029 abort ();
1030 }
1031
1032 /* Convert a machine dependent frag. We never generate these. */
1033
1034 void
1035 md_convert_frag (abfd, sec, fragp)
1036 bfd *abfd;
1037 asection *sec;
1038 fragS *fragp;
1039 {
1040 abort ();
1041 }
1042
1043 /* Parse an operand that is machine-specific.
1044
1045 The ARC has a special %-op to adjust addresses so they're usable in
1046 branches. The "st" is short for the STatus register.
1047 ??? Later expand this to take a flags value too.
1048
1049 ??? We can't create new expression types so we map the %-op's onto the
1050 existing syntax. This means that the user could use the chosen syntax
1051 to achieve the same effect. Perhaps put a special cookie in X_add_number
1052 to mark the expression as special. */
1053
1054 void
1055 md_operand (expressionP)
1056 expressionS *expressionP;
1057 {
1058 char *p = input_line_pointer;
1059
1060 if (*p == '%' && strncmp (p, "%st(", 4) == 0)
1061 {
1062 input_line_pointer += 4;
1063 expression (expressionP);
1064 if (*input_line_pointer != ')')
1065 {
1066 as_bad ("missing ')' in %-op");
1067 return;
1068 }
1069 ++input_line_pointer;
1070 if (expressionP->X_op == O_symbol
1071 && expressionP->X_add_number == 0
1072 /* I think this test is unnecessary but just as a sanity check... */
1073 && expressionP->X_op_symbol == NULL)
1074 {
1075 expressionS two;
1076
1077 expressionP->X_op = O_right_shift;
1078 two.X_op = O_constant;
1079 two.X_add_symbol = two.X_op_symbol = NULL;
1080 two.X_add_number = 2;
1081 expressionP->X_op_symbol = make_expr_symbol (&two);
1082 }
1083 /* allow %st(sym1-sym2) */
1084 else if (expressionP->X_op == O_subtract
1085 && expressionP->X_add_symbol != NULL
1086 && expressionP->X_op_symbol != NULL
1087 && expressionP->X_add_number == 0)
1088 {
1089 expressionS two;
1090
1091 expressionP->X_add_symbol = make_expr_symbol (expressionP);
1092 expressionP->X_op = O_right_shift;
1093 two.X_op = O_constant;
1094 two.X_add_symbol = two.X_op_symbol = NULL;
1095 two.X_add_number = 2;
1096 expressionP->X_op_symbol = make_expr_symbol (&two);
1097 }
1098 else
1099 {
1100 as_bad ("expression too complex for %%st");
1101 return;
1102 }
1103 }
1104 }
1105
1106 /* We have no need to default values of symbols.
1107 We could catch register names here, but that is handled by inserting
1108 them all in the symbol table to begin with. */
1109
1110 symbolS *
1111 md_undefined_symbol (name)
1112 char *name;
1113 {
1114 return 0;
1115 }
1116 \f
1117 /* Functions concerning expressions. */
1118
1119 /* Parse a .byte, .word, etc. expression.
1120
1121 Values for the status register are specified with %st(label).
1122 `label' will be right shifted by 2. */
1123
1124 void
1125 arc_parse_cons_expression (exp, nbytes)
1126 expressionS *exp;
1127 int nbytes;
1128 {
1129 expr (0, exp);
1130 }
1131
1132 /* Record a fixup for a cons expression. */
1133
1134 void
1135 arc_cons_fix_new (frag, where, nbytes, exp)
1136 fragS *frag;
1137 int where;
1138 int nbytes;
1139 expressionS *exp;
1140 {
1141 if (nbytes == 4)
1142 {
1143 int reloc_type;
1144 expressionS exptmp;
1145
1146 /* This may be a special ARC reloc (eg: %st()). */
1147 reloc_type = get_arc_exp_reloc_type (1, BFD_RELOC_32, exp, &exptmp);
1148 fix_new_exp (frag, where, nbytes, &exptmp, 0, reloc_type);
1149 }
1150 else
1151 {
1152 fix_new_exp (frag, where, nbytes, exp, 0,
1153 nbytes == 2 ? BFD_RELOC_16
1154 : nbytes == 8 ? BFD_RELOC_64
1155 : BFD_RELOC_32);
1156 }
1157 }
1158 \f
1159 /* Functions concerning relocs. */
1160
1161 /* The location from which a PC relative jump should be calculated,
1162 given a PC relative reloc. */
1163
1164 long
1165 md_pcrel_from (fixP)
1166 fixS *fixP;
1167 {
1168 if (fixP->fx_addsy != (symbolS *) NULL
1169 && ! S_IS_DEFINED (fixP->fx_addsy))
1170 /* Return offset from PC to delay slot. Offsets are from there. */
1171 return 4;
1172
1173 /* Return the address of the delay slot. */
1174 return fixP->fx_frag->fr_address + fixP->fx_where + fixP->fx_size;
1175 }
1176
1177 /* Compute the reloc type of an expression.
1178 The possibly modified expression is stored in EXPNEW.
1179
1180 This is used to convert the expressions generated by the %-op's into
1181 the appropriate operand type. It is called for both data in instructions
1182 (operands) and data outside instructions (variables, debugging info, etc.).
1183
1184 Currently supported %-ops:
1185
1186 %st(symbol): represented as "symbol >> 2"
1187 "st" is short for STatus as in the status register (pc)
1188
1189 DEFAULT_TYPE is the type to use if no special processing is required.
1190
1191 DATA_P is non-zero for data or limm values, zero for insn operands.
1192 Remember that the opcode "insertion fns" cannot be used on data, they're
1193 only for inserting operands into insns. They also can't be used for limm
1194 values as the insertion routines don't handle limm values. When called for
1195 insns we return fudged reloc types (real_value - BFD_RELOC_UNUSED). When
1196 called for data or limm values we use real reloc types. */
1197
1198 static int
1199 get_arc_exp_reloc_type (data_p, default_type, exp, expnew)
1200 int data_p;
1201 int default_type;
1202 expressionS *exp;
1203 expressionS *expnew;
1204 {
1205 /* If the expression is "symbol >> 2" we must change it to just "symbol",
1206 as fix_new_exp can't handle it. Similarily for (symbol - symbol) >> 2.
1207 That's ok though. What's really going on here is that we're using
1208 ">> 2" as a special syntax for specifying BFD_RELOC_ARC_B26. */
1209
1210 if (exp->X_op == O_right_shift
1211 && exp->X_op_symbol != NULL
1212 && exp->X_op_symbol->sy_value.X_op == O_constant
1213 && exp->X_op_symbol->sy_value.X_add_number == 2
1214 && exp->X_add_number == 0)
1215 {
1216 if (exp->X_add_symbol != NULL
1217 && (exp->X_add_symbol->sy_value.X_op == O_constant
1218 || exp->X_add_symbol->sy_value.X_op == O_symbol))
1219 {
1220 *expnew = *exp;
1221 expnew->X_op = O_symbol;
1222 expnew->X_op_symbol = NULL;
1223 return data_p ? BFD_RELOC_ARC_B26 : arc_operand_map['J'];
1224 }
1225 else if (exp->X_add_symbol != NULL
1226 && exp->X_add_symbol->sy_value.X_op == O_subtract)
1227 {
1228 *expnew = exp->X_add_symbol->sy_value;
1229 return data_p ? BFD_RELOC_ARC_B26 : arc_operand_map['J'];
1230 }
1231 }
1232
1233 *expnew = *exp;
1234 return default_type;
1235 }
1236
1237 /* Apply a fixup to the object code. This is called for all the
1238 fixups we generated by the call to fix_new_exp, above. In the call
1239 above we used a reloc code which was the largest legal reloc code
1240 plus the operand index. Here we undo that to recover the operand
1241 index. At this point all symbol values should be fully resolved,
1242 and we attempt to completely resolve the reloc. If we can not do
1243 that, we determine the correct reloc code and put it back in the fixup. */
1244
1245 int
1246 md_apply_fix (fixP, valueP)
1247 fixS *fixP;
1248 valueT *valueP;
1249 {
1250 /*char *buf = fixP->fx_where + fixP->fx_frag->fr_literal;*/
1251 valueT value;
1252
1253 /* FIXME FIXME FIXME: The value we are passed in *valueP includes
1254 the symbol values. Since we are using BFD_ASSEMBLER, if we are
1255 doing this relocation the code in write.c is going to call
1256 bfd_perform_relocation, which is also going to use the symbol
1257 value. That means that if the reloc is fully resolved we want to
1258 use *valueP since bfd_perform_relocation is not being used.
1259 However, if the reloc is not fully resolved we do not want to use
1260 *valueP, and must use fx_offset instead. However, if the reloc
1261 is PC relative, we do want to use *valueP since it includes the
1262 result of md_pcrel_from. This is confusing. */
1263
1264 if (fixP->fx_addsy == (symbolS *) NULL)
1265 {
1266 value = *valueP;
1267 fixP->fx_done = 1;
1268 }
1269 else if (fixP->fx_pcrel)
1270 value = *valueP;
1271 else
1272 {
1273 value = fixP->fx_offset;
1274 if (fixP->fx_subsy != (symbolS *) NULL)
1275 {
1276 if (S_GET_SEGMENT (fixP->fx_subsy) == absolute_section)
1277 value -= S_GET_VALUE (fixP->fx_subsy);
1278 else
1279 {
1280 /* We can't actually support subtracting a symbol. */
1281 as_bad_where (fixP->fx_file, fixP->fx_line,
1282 "expression too complex");
1283 }
1284 }
1285 }
1286
1287 if ((int) fixP->fx_r_type >= (int) BFD_RELOC_UNUSED)
1288 {
1289 int opindex;
1290 const struct arc_operand *operand;
1291 char *where;
1292 arc_insn insn;
1293
1294 opindex = (int) fixP->fx_r_type - (int) BFD_RELOC_UNUSED;
1295
1296 operand = &arc_operands[opindex];
1297
1298 /* Fetch the instruction, insert the fully resolved operand
1299 value, and stuff the instruction back again. */
1300 where = fixP->fx_frag->fr_literal + fixP->fx_where;
1301 if (target_big_endian)
1302 insn = bfd_getb32 ((unsigned char *) where);
1303 else
1304 insn = bfd_getl32 ((unsigned char *) where);
1305 insn = arc_insert_operand (insn, operand, -1, NULL, (offsetT) value,
1306 fixP->fx_file, fixP->fx_line);
1307 if (target_big_endian)
1308 bfd_putb32 ((bfd_vma) insn, (unsigned char *) where);
1309 else
1310 bfd_putl32 ((bfd_vma) insn, (unsigned char *) where);
1311
1312 if (fixP->fx_done)
1313 {
1314 /* Nothing else to do here. */
1315 return 1;
1316 }
1317
1318 /* Determine a BFD reloc value based on the operand information.
1319 We are only prepared to turn a few of the operands into relocs.
1320 !!! Note that we can't handle limm values here. Since we're using
1321 implicit addends the addend must be inserted into the instruction,
1322 however, the opcode insertion routines currently do nothing with
1323 limm values. */
1324 if (operand->fmt == 'B')
1325 {
1326 assert ((operand->flags & ARC_OPERAND_RELATIVE_BRANCH) != 0
1327 && operand->bits == 20
1328 && operand->shift == 7);
1329 fixP->fx_r_type = BFD_RELOC_ARC_B22_PCREL;
1330 }
1331 else if (0 && operand->fmt == 'J')
1332 {
1333 assert ((operand->flags & ARC_OPERAND_ABSOLUTE_BRANCH) != 0
1334 && operand->bits == 24
1335 && operand->shift == 32);
1336 fixP->fx_r_type = BFD_RELOC_ARC_B26;
1337 }
1338 else if (0 && operand->fmt == 'L')
1339 {
1340 assert ((operand->flags & ARC_OPERAND_LIMM) != 0
1341 && operand->bits == 32
1342 && operand->shift == 32);
1343 fixP->fx_r_type = BFD_RELOC_32;
1344 }
1345 else
1346 {
1347 as_bad_where (fixP->fx_file, fixP->fx_line,
1348 "unresolved expression that must be resolved");
1349 fixP->fx_done = 1;
1350 return 1;
1351 }
1352 }
1353 else
1354 {
1355 switch (fixP->fx_r_type)
1356 {
1357 case BFD_RELOC_8:
1358 md_number_to_chars (fixP->fx_frag->fr_literal + fixP->fx_where,
1359 value, 1);
1360 break;
1361 case BFD_RELOC_16:
1362 md_number_to_chars (fixP->fx_frag->fr_literal + fixP->fx_where,
1363 value, 2);
1364 break;
1365 case BFD_RELOC_32:
1366 md_number_to_chars (fixP->fx_frag->fr_literal + fixP->fx_where,
1367 value, 4);
1368 break;
1369 #if 0
1370 case BFD_RELOC_64:
1371 md_number_to_chars (fixP->fx_frag->fr_literal + fixP->fx_where,
1372 value, 8);
1373 break;
1374 #endif
1375 case BFD_RELOC_ARC_B26:
1376 /* If !fixP->fx_done then `value' is an implicit addend.
1377 We must shift it right by 2 in this case as well because the
1378 linker performs the relocation and then adds this in (as opposed
1379 to adding this in and then shifting right by 2). */
1380 value >>= 2;
1381 md_number_to_chars (fixP->fx_frag->fr_literal + fixP->fx_where,
1382 value, 4);
1383 break;
1384 default:
1385 abort ();
1386 }
1387 }
1388
1389 fixP->fx_addnumber = value;
1390
1391 return 1;
1392 }
1393
1394 /* Translate internal representation of relocation info to BFD target
1395 format. */
1396
1397 arelent *
1398 tc_gen_reloc (section, fixP)
1399 asection *section;
1400 fixS *fixP;
1401 {
1402 arelent *reloc;
1403
1404 reloc = (arelent *) bfd_alloc_by_size_t (stdoutput, sizeof (arelent));
1405
1406 reloc->sym_ptr_ptr = &fixP->fx_addsy->bsym;
1407 reloc->address = fixP->fx_frag->fr_address + fixP->fx_where;
1408 reloc->howto = bfd_reloc_type_lookup (stdoutput, fixP->fx_r_type);
1409 if (reloc->howto == (reloc_howto_type *) NULL)
1410 {
1411 as_bad_where (fixP->fx_file, fixP->fx_line,
1412 "internal error: can't export reloc type %d (`%s')",
1413 fixP->fx_r_type, bfd_get_reloc_code_name (fixP->fx_r_type));
1414 return NULL;
1415 }
1416 reloc->addend = fixP->fx_addnumber;
1417
1418 assert (!fixP->fx_pcrel == !reloc->howto->pc_relative);
1419
1420 return reloc;
1421 }
1422 \f
1423 /* Frobbers. */
1424
1425 #if 0
1426 /* Set the real name if the .rename pseudo-op was used.
1427 Return 1 if the symbol should not be included in the symbol table. */
1428
1429 int
1430 arc_frob_symbol (sym)
1431 symbolS *sym;
1432 {
1433 if (sym->sy_tc.real_name != (char *) NULL)
1434 S_SET_NAME (sym, sym->sy_tc.real_name);
1435
1436 return 0;
1437 }
1438 #endif