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