Add MMIX support
[external/binutils.git] / gas / config / tc-mmix.c
1 /* tc-mmix.c -- Assembler for Don Knuth's MMIX.
2    Copyright (C) 2001 Free Software Foundation.
3
4    This file is part of GAS, the GNU Assembler.
5
6    GAS is free software; you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 2, or (at your option)
9    any later version.
10
11    GAS is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
15
16    You should have received a copy of the GNU General Public License
17    along with GAS; see the file COPYING.  If not, write to
18    the Free Software Foundation, 59 Temple Place - Suite 330,
19    Boston, MA 02111-1307, USA.  */
20
21 /* Knuth's assembler mmixal does not provide a relocatable format; mmo is
22    to be considered a final link-format.  In the final link, we make mmo,
23    but for relocatable files, we use ELF.
24
25    One goal is to provide a superset of what mmixal does, including
26    compatible syntax, but the main purpose is to serve GCC.  */
27
28
29 #include <stdio.h>
30 #include "as.h"
31 #include "subsegs.h"
32 #include "bfd.h"
33 #include "elf/mmix.h"
34 #include "opcode/mmix.h"
35 #include "safe-ctype.h"
36 #include "dwarf2dbg.h"
37 #include "obstack.h"
38
39 /* Something to describe what we need to do with a fixup before output,
40    for example assert something of what it became or make a relocation.  */
41
42 enum mmix_fixup_action
43  {
44    mmix_fixup_byte,
45    mmix_fixup_register,
46    mmix_fixup_register_or_adjust_for_byte
47  };
48
49 static int get_spec_regno PARAMS ((char *));
50 static int get_operands PARAMS ((int, char *, expressionS[]));
51 static int get_putget_operands
52   PARAMS ((struct mmix_opcode *, char *, expressionS[]));
53 static void s_prefix PARAMS ((int));
54 static void s_greg PARAMS ((int));
55 static void s_loc PARAMS ((int));
56 static void s_bspec PARAMS ((int));
57 static void s_espec PARAMS ((int));
58 static void mmix_s_local PARAMS ((int));
59 static void mmix_greg_internal PARAMS ((char *));
60 static void mmix_set_geta_branch_offset PARAMS ((char *, offsetT value));
61 static void mmix_set_jmp_offset PARAMS ((char *, offsetT));
62 static void mmix_fill_nops PARAMS ((char *, int));
63 static int cmp_greg_symbol_fixes PARAMS ((const PTR, const PTR));
64 static int cmp_greg_val_greg_symbol_fixes
65   PARAMS ((const PTR p1, const PTR p2));
66 static void mmix_handle_rest_of_empty_line PARAMS ((void));
67 static void mmix_discard_rest_of_line PARAMS ((void));
68 static void mmix_byte PARAMS ((void));
69 static void mmix_cons PARAMS ((int));
70 static void mmix_frob_local_reloc PARAMS ((bfd *, asection *, PTR));
71
72 /* Continue the tradition of symbols.c; use control characters to enforce
73    magic.  These are used when replacing e.g. 8F and 8B so we can handle
74    such labels correctly with the common parser hooks.  */
75 #define MAGIC_FB_BACKWARD_CHAR '\003'
76 #define MAGIC_FB_FORWARD_CHAR '\004'
77
78 /* Copy the location of a frag to a fix.  */
79 #define COPY_FR_WHERE_TO_FX(FRAG, FIX)          \
80  do                                             \
81    {                                            \
82      (FIX)->fx_file = (FRAG)->fr_file;          \
83      (FIX)->fx_line = (FRAG)->fr_line;          \
84    }                                            \
85  while (0)
86
87 const char *md_shortopts = "x";
88 static int current_fb_label = -1;
89 static char *pending_label = NULL;
90
91 static bfd_vma lowest_text_loc = (bfd_vma) -1;
92 static int text_has_contents = 0;
93
94 /* The alignment of the previous instruction, and a boolean for whether we
95    want to avoid aligning the next WYDE, TETRA, OCTA or insn.  */
96 static int last_alignment = 0;
97 static int want_unaligned = 0;
98
99 static bfd_vma lowest_data_loc = (bfd_vma) -1;
100 static int data_has_contents = 0;
101
102 /* The fragS of the instruction being assembled.  Only valid from within
103    md_assemble.  */
104 fragS *mmix_opcode_frag = NULL;
105
106 /* Raw GREGs as appearing in input.  These may be fewer than the number
107    after relaxing.  */
108 static int n_of_raw_gregs = 0;
109 static struct
110  {
111    char *label;
112    expressionS exp;
113  } mmix_raw_gregs[MAX_GREGS];
114
115 /* Fixups for all unique GREG registers.  We store the fixups here in
116    md_convert_frag, then we use the array to convert
117    BFD_RELOC_MMIX_BASE_PLUS_OFFSET fixups in tc_gen_reloc.  The index is
118    just a running number and is not supposed to be correlated to a
119    register number.  */
120 static fixS *mmix_gregs[MAX_GREGS];
121 static int n_of_cooked_gregs = 0;
122
123 /* Pointing to the register section we use for output.  */
124 static asection *real_reg_section;
125
126 /* For each symbol; unknown or section symbol, we keep a list of GREG
127    definitions sorted on increasing offset.  It seems no use keeping count
128    to allocate less room than the maximum number of gregs when we've found
129    one for a section or symbol.  */
130 struct mmix_symbol_gregs
131  {
132    int n_gregs;
133    struct mmix_symbol_greg_fixes
134    {
135      fixS *fix;
136
137      /* A signed type, since we may have GREGs pointing slightly before the
138         contents of a section.  */
139      offsetT offs;
140    } greg_fixes[MAX_GREGS];
141  };
142
143 /* Should read insert a colon on something that starts in column 0 on
144    this line?  */
145 static int label_without_colon_this_line = 1;
146
147 /* Should we expand operands for external symbols?  */
148 static int expand_op = 1;
149
150 /* Should we warn when expanding operands?  FIXME: test-cases for when -x
151    is absent.  */
152 static int warn_on_expansion = 1;
153
154 /* Should we merge non-zero GREG register definitions?  */
155 static int merge_gregs = 1;
156
157 /* Should we emit built-in symbols?  */
158 static int predefined_syms = 1;
159
160 /* Should we anything but the listed special register name (e.g. equated
161    symbols)?  */
162 static int equated_spec_regs = 1;
163
164 /* Do we require standard GNU syntax?  */
165 int mmix_gnu_syntax = 0;
166
167 /* Do we globalize all symbols?  */
168 int mmix_globalize_symbols = 0;
169
170 /* Do we know that the next semicolon is at the end of the operands field
171    (in mmixal mode; constant 1 in GNU mode)? */
172 int mmix_next_semicolon_is_eoln = 1;
173
174 /* Do we have a BSPEC in progress?  */
175 static int doing_bspec = 0;
176 static char *bspec_file;
177 static unsigned int bspec_line;
178
179 struct option md_longopts[] =
180  {
181 #define OPTION_RELAX  (OPTION_MD_BASE)
182 #define OPTION_NOEXPAND  (OPTION_RELAX + 1)
183 #define OPTION_NOMERGEGREG  (OPTION_NOEXPAND + 1)
184 #define OPTION_NOSYMS  (OPTION_NOMERGEGREG + 1)
185 #define OPTION_GNU_SYNTAX  (OPTION_NOSYMS + 1)
186 #define OPTION_GLOBALIZE_SYMBOLS  (OPTION_GNU_SYNTAX + 1)
187 #define OPTION_FIXED_SPEC_REGS  (OPTION_GLOBALIZE_SYMBOLS + 1)
188    {"linkrelax", no_argument, NULL, OPTION_RELAX},
189    {"no-expand", no_argument, NULL, OPTION_NOEXPAND},
190    {"no-merge-gregs", no_argument, NULL, OPTION_NOMERGEGREG},
191    {"no-predefined-syms", no_argument, NULL, OPTION_NOSYMS},
192    {"gnu-syntax", no_argument, NULL, OPTION_GNU_SYNTAX},
193    {"globalize-symbols", no_argument, NULL, OPTION_GLOBALIZE_SYMBOLS},
194    {"fixed-special-register-names", no_argument, NULL,
195     OPTION_FIXED_SPEC_REGS},
196    {NULL, no_argument, NULL, 0}
197  };
198
199 size_t md_longopts_size = sizeof (md_longopts);
200
201 static struct hash_control *mmix_opcode_hash;
202
203 /* We use these when implementing the PREFIX pseudo.  */
204 char *mmix_current_prefix;
205 struct obstack mmix_sym_obstack;
206
207
208 /* For MMIX, we encode the relax_substateT:s (in e.g. fr_substate) as one
209    bit length, and the relax-type shifted on top of that.  There seems to
210    be no point in making the relaxation more fine-grained; the linker does
211    that better and we might interfere by changing non-optimal relaxations
212    into other insns that cannot be relaxed as easily.
213
214    Groups for MMIX relaxing:
215
216    1. GETA
217       extra length: zero or three insns.
218
219    2. Bcc
220       extra length: zero or five insns.
221
222    3. PUSHJ
223       extra length: zero or four insns.
224
225    4. JMP
226       extra length: zero or four insns.  */
227
228 #define STATE_GETA      (1)
229 #define STATE_BCC       (2)
230 #define STATE_PUSHJ     (3)
231 #define STATE_JMP       (4)
232 #define STATE_GREG      (5)
233
234 /* No fine-grainedness here.  */
235 #define STATE_LENGTH_MASK           (1)
236
237 #define STATE_ZERO                  (0)
238 #define STATE_MAX                   (1)
239
240 /* More descriptive name for convenience.  */
241 /* FIXME: We should start on something different, not MAX.  */
242 #define STATE_UNDF                  STATE_MAX
243
244 /* FIXME: For GREG, we must have other definitions; UNDF == MAX isn't
245    appropriate; we need it the other way round.  This value together with
246    fragP->tc_frag_data shows what state the frag is in: tc_frag_data
247    non-NULL means 0, NULL means 8 bytes.  */
248 #define STATE_GREG_UNDF ENCODE_RELAX (STATE_GREG, STATE_ZERO)
249 #define STATE_GREG_DEF ENCODE_RELAX (STATE_GREG, STATE_MAX)
250
251 /* These displacements are relative to the adress following the opcode
252    word of the instruction.  The catch-all states have zero for "reach"
253    and "next" entries.  */
254
255 #define GETA_0F (65536 * 4 - 8)
256 #define GETA_0B (-65536 * 4 - 4)
257
258 #define GETA_MAX_LEN 4*4
259 #define GETA_3F 0
260 #define GETA_3B 0
261
262 #define BCC_0F GETA_0F
263 #define BCC_0B GETA_0B
264
265 #define BCC_MAX_LEN 6*4
266 #define BCC_5F GETA_3F
267 #define BCC_5B GETA_3B
268
269 #define PUSHJ_0F GETA_0F
270 #define PUSHJ_0B GETA_0B
271
272 #define PUSHJ_MAX_LEN 5*4
273 #define PUSHJ_4F GETA_3F
274 #define PUSHJ_4B GETA_3B
275
276 #define JMP_0F (65536 * 256 * 4 - 8)
277 #define JMP_0B (-65536 * 256 * 4 - 4)
278
279 #define JMP_MAX_LEN 5*4
280 #define JMP_4F 0
281 #define JMP_4B 0
282
283 #define RELAX_ENCODE_SHIFT 1
284 #define ENCODE_RELAX(what, length) (((what) << RELAX_ENCODE_SHIFT) + (length))
285
286 const relax_typeS mmix_relax_table[] =
287  {
288    /* Error sentinel (0, 0).  */
289    {1,          1,              0,      0},
290
291    /* Unused (0, 1).  */
292    {1,          1,              0,      0},
293
294    /* GETA (1, 0).  */
295    {GETA_0F,    GETA_0B,        0,      ENCODE_RELAX (STATE_GETA, STATE_MAX)},
296
297    /* GETA (1, 1).  */
298    {GETA_3F,    GETA_3B,
299                 GETA_MAX_LEN - 4,       0},
300
301    /* BCC (2, 0).  */
302    {BCC_0F,     BCC_0B,         0,      ENCODE_RELAX (STATE_BCC, STATE_MAX)},
303
304    /* BCC (2, 1).  */
305    {BCC_5F,     BCC_5B,
306                 BCC_MAX_LEN - 4,        0},
307
308    /* PUSHJ (3, 0).  */
309    {PUSHJ_0F,   PUSHJ_0B,       0,      ENCODE_RELAX (STATE_PUSHJ, STATE_MAX)},
310
311    /* PUSHJ (3, 1).  */
312    {PUSHJ_4F,   PUSHJ_4B,
313                 PUSHJ_MAX_LEN - 4,      0},
314
315    /* JMP (4, 0).  */
316    {JMP_0F,     JMP_0B,         0,      ENCODE_RELAX (STATE_JMP, STATE_MAX)},
317
318    /* JMP (4, 1).  */
319    {JMP_4F,     JMP_4B,
320                 JMP_MAX_LEN - 4,        0},
321
322    /* GREG (5, 0), (5, 1), though the table entry isn't used.  */
323    {0, 0, 0, 0}, {0, 0, 0, 0}
324 };
325
326 const pseudo_typeS md_pseudo_table[] =
327  {
328    /* Support " .greg sym,expr" syntax.  */
329    {"greg", s_greg, 0},
330
331    /* Support " .bspec expr" syntax.  */
332    {"bspec", s_bspec, 1},
333
334    /* Support " .espec" syntax.  */
335    {"espec", s_espec, 1},
336
337    /* Support " .local $45" syntax.  */
338    {"local", mmix_s_local, 1},
339
340    /* Support DWARF2 debugging info.  */
341    {"file", dwarf2_directive_file, 0},
342    {"loc", dwarf2_directive_loc, 0},
343
344    {NULL, 0, 0}
345  };
346
347 const char mmix_comment_chars[] = "%!";
348
349 /* A ':' is a valid symbol character in mmixal.  It's the prefix
350    delimiter, but other than that, it works like a symbol character,
351    except that we strip one off at the beginning of symbols.  An '@' is a
352    symbol by itself (for the current location); space around it must not
353    be stripped.  */
354 const char mmix_symbol_chars[] = ":@";
355
356 const char line_comment_chars[] = "*#";
357
358 const char line_separator_chars[] = ";";
359
360 const char mmix_exp_chars[] = "eE";
361
362 const char mmix_flt_chars[] = "rf";
363
364
365 /* Fill in the offset-related part of GETA or Bcc.  */
366
367 static void
368 mmix_set_geta_branch_offset (opcodep, value)
369      char *opcodep;
370      offsetT value;
371 {
372   if (value < 0)
373     {
374       value += 65536 * 4;
375       opcodep[0] |= 1;
376     }
377
378   value /= 4;
379   md_number_to_chars (opcodep + 2, value, 2);
380 }
381
382 /* Fill in the offset-related part of JMP.  */
383
384 static void
385 mmix_set_jmp_offset (opcodep, value)
386      char *opcodep;
387      offsetT value;
388 {
389   if (value < 0)
390     {
391       value += 65536 * 256 * 4;
392       opcodep[0] |= 1;
393     }
394
395   value /= 4;
396   md_number_to_chars (opcodep + 1, value, 3);
397 }
398
399 /* Fill in NOP:s for the expanded part of GETA/JMP/Bcc/PUSHJ.  */
400
401 static void
402 mmix_fill_nops (opcodep, n)
403      char *opcodep;
404      int n;
405 {
406   int i;
407
408   for (i = 0; i < n; i++)
409     md_number_to_chars (opcodep + i*4, SWYM_INSN_BYTE << 24, 4);
410 }
411
412 /* See macro md_parse_name in tc-mmix.h.  */
413
414 int
415 mmix_current_location (fn, exp)
416      void (*fn) PARAMS ((expressionS *));
417      expressionS *exp;
418 {
419   (*fn) (exp);
420
421   return 1;
422 }
423
424 /* Get up to three operands, filling them into the exp array.
425    General idea and code stolen from the tic80 port.  */
426
427 static int
428 get_operands (max_operands, s, exp)
429      int max_operands;
430      char *s;
431      expressionS exp[];
432 {
433   char *p = s;
434   int numexp = 0;
435   int nextchar = ',';
436
437   while (nextchar == ',')
438     {
439       /* Skip leading whitespace */
440       while (*p == ' ' || *p == '\t')
441         p++;
442
443       /* Check to see if we have any operands left to parse */
444       if (*p == 0 || *p == '\n' || *p == '\r')
445         {
446           break;
447         }
448       else if (numexp == max_operands)
449         {
450           /* This seems more sane than saying "too many operands".  We'll
451              get here only if the trailing trash starts with a comma.  */
452           as_bad (_("invalid operands"));
453           mmix_discard_rest_of_line ();
454           return 0;
455         }
456
457       /* Begin operand parsing at the current scan point. */
458
459       input_line_pointer = p;
460       expression (&exp[numexp]);
461
462       if (exp[numexp].X_op == O_illegal)
463         {
464           as_bad (_("invalid operands"));
465         }
466       else if (exp[numexp].X_op == O_absent)
467         {
468           as_bad (_("missing operand"));
469         }
470
471       numexp++;
472       p = input_line_pointer;
473
474       /* Skip leading whitespace */
475       while (*p == ' ' || *p == '\t')
476         p++;
477       nextchar = *p++;
478     }
479
480   /* If we allow "naked" comments, ignore the rest of the line.  */
481   if (nextchar != ',')
482     {
483       mmix_handle_rest_of_empty_line ();
484       input_line_pointer--;
485     }
486
487   /* Mark the end of the valid operands with an illegal expression. */
488   exp[numexp].X_op = O_illegal;
489
490   return (numexp);
491 }
492
493 /* Get the value of a special register, or -1 if the name does not match
494    one.  NAME is a null-terminated string.  */
495
496 static int
497 get_spec_regno (name)
498      char *name;
499 {
500   int i;
501
502   if (name == NULL)
503     return -1;
504
505   if (*name == ':')
506     name++;
507
508   /* Well, it's a short array and we'll most often just match the first
509      entry, rJ.  */
510   for (i = 0; mmix_spec_regs[i].name != NULL; i++)
511     if (strcmp (name, mmix_spec_regs[i].name) == 0)
512       return mmix_spec_regs[i].number;
513
514   return -1;
515 }
516
517 /* For GET and PUT, parse the register names "manually", so we don't use
518    user labels.  */
519 static int
520 get_putget_operands (insn, operands, exp)
521      struct mmix_opcode *insn;
522      char *operands;
523      expressionS exp[];
524 {
525   expressionS *expp_reg;
526   expressionS *expp_sreg;
527   char *sregp = NULL;
528   char *sregend = operands;
529   char *p = operands;
530   char c = *sregend;
531   int regno;
532
533   /* Skip leading whitespace */
534   while (*p == ' ' || *p == '\t')
535     p++;
536
537   input_line_pointer = p;
538
539   if (insn->operands == mmix_operands_get)
540     {
541       expp_reg = &exp[0];
542       expp_sreg = &exp[1];
543
544       expression (expp_reg);
545
546       p = input_line_pointer;
547
548       /* Skip whitespace */
549       while (*p == ' ' || *p == '\t')
550         p++;
551
552       if (*p == ',')
553         {
554           p++;
555
556           /* Skip whitespace */
557           while (*p == ' ' || *p == '\t')
558             p++;
559           sregp = p;
560           input_line_pointer = sregp;
561           c = get_symbol_end ();
562           sregend = input_line_pointer;
563         }
564     }
565   else
566     {
567       expp_sreg = &exp[0];
568       expp_reg = &exp[1];
569
570       /* Initialize to error state in case we'll never call expression on
571          this operand.  */
572       expp_reg->X_op = O_illegal;
573
574       sregp = p;
575       c = get_symbol_end ();
576       sregend = p = input_line_pointer;
577       *p = c;
578
579       /* Skip whitespace */
580       while (*p == ' ' || *p == '\t')
581         p++;
582
583       if (*p == ',')
584         {
585           p++;
586
587           /* Skip whitespace */
588           while (*p == ' ' || *p == '\t')
589             p++;
590
591           input_line_pointer = p;
592           expression (expp_reg);
593         }
594       *sregend = 0;
595     }
596
597   regno = get_spec_regno (sregp);
598   *sregend = c;
599
600   /* Let the caller issue errors; we've made sure the operands are
601      invalid.  */
602   if (expp_reg->X_op != O_illegal
603       && expp_reg->X_op != O_absent
604       && regno != -1)
605     {
606       expp_sreg->X_op = O_register;
607       expp_sreg->X_add_number = regno + 256;
608     }
609
610   return 2;
611 }
612
613 /* Handle MMIX-specific option.  */
614
615 int
616 md_parse_option (c, arg)
617      int c;
618      char *arg ATTRIBUTE_UNUSED;
619 {
620   switch (c)
621     {
622     case 'x':
623       warn_on_expansion = 0;
624       break;
625
626     case OPTION_RELAX:
627       linkrelax = 1;
628       break;
629
630     case OPTION_NOEXPAND:
631       expand_op = 0;
632       break;
633
634     case OPTION_NOMERGEGREG:
635       merge_gregs = 0;
636       break;
637
638     case OPTION_NOSYMS:
639       predefined_syms = 0;
640       equated_spec_regs = 0;
641       break;
642
643     case OPTION_GNU_SYNTAX:
644       mmix_gnu_syntax = 1;
645       label_without_colon_this_line = 0;
646       break;
647
648     case OPTION_GLOBALIZE_SYMBOLS:
649       mmix_globalize_symbols = 1;
650       break;
651
652     case OPTION_FIXED_SPEC_REGS:
653       equated_spec_regs = 0;
654       break;
655
656     default:
657       return 0;
658     }
659
660   return 1;
661 }
662
663 /* Display MMIX-specific help text.  */
664
665 void
666 md_show_usage (stream)
667      FILE * stream;
668 {
669   fprintf (stream, _(" MMIX-specific command line options:\n"));
670   fprintf (stream, _("\
671   -fixed-special-register-names\n\
672                           Allow only the original special register names.\n"));
673   fprintf (stream, _("\
674   -globalize-symbols      Make all symbols global.\n"));
675   fprintf (stream, _("\
676   -gnu-syntax             Turn off mmixal syntax compatibility.\n"));
677   fprintf (stream, _("\
678   -relax                  Create linker relaxable code.\n"));
679   fprintf (stream, _("\
680   -no-predefined-syms     Do not provide mmixal built-in constants.\n\
681                           Implies -fixed-special-register-names.\n"));
682   fprintf (stream, _("\
683   -no-expand              Do not expand GETA, branches, PUSHJ or JUMP\n\
684                           into multiple instructions.\n"));
685   fprintf (stream, _("\
686   -no-merge-gregs         Do not merge GREG definitions with nearby values.\n"));
687   fprintf (stream, _("\
688   -x                      Do not warn when an operand to GETA, a branch,\n\
689                           PUSHJ or JUMP is not known to be within range.\n\
690                           The linker will catch any errors.\n"));
691 }
692
693 /* Step to end of line, but don't step over the end of the line.  */
694
695 static void
696 mmix_discard_rest_of_line ()
697 {
698   while (*input_line_pointer
699          && (! is_end_of_line [(unsigned char) *input_line_pointer]
700              || TC_EOL_IN_INSN (input_line_pointer)))
701     input_line_pointer++;
702 }
703
704 /* Act as demand_empty_rest_of_line if we're in strict GNU syntax mode,
705    otherwise just ignore the rest of the line (and skip the end-of-line
706    delimiter).  */
707
708 static void
709 mmix_handle_rest_of_empty_line ()
710 {
711   if (mmix_gnu_syntax)
712     demand_empty_rest_of_line ();
713   else
714     {
715       mmix_discard_rest_of_line ();
716       input_line_pointer++;
717     }
718 }
719
720 /* Initialize GAS MMIX specifics.  */
721
722 void
723 mmix_md_begin ()
724 {
725   int i;
726   const struct mmix_opcode *opcode;
727
728   /* We assume nobody will use this, so don't allocate any room.  */
729   obstack_begin (&mmix_sym_obstack, 0);
730
731   /* This will break the day the "lex" thingy changes.  For now, it's the
732      only way to make ':' part of a name, and a name beginner.  */
733   lex_type [':'] = (LEX_NAME | LEX_BEGIN_NAME);
734
735   mmix_opcode_hash = hash_new ();
736
737   real_reg_section
738     = bfd_make_section_old_way (stdoutput, MMIX_REG_SECTION_NAME);
739
740   for (opcode = mmix_opcodes; opcode->name; opcode++)
741     hash_insert (mmix_opcode_hash, opcode->name, (char *) opcode);
742
743   /* We always insert the ordinary registers 0..255 as registers.  */
744   for (i = 0; i < 256; i++)
745     {
746       char buf[5];
747
748       /* Alternatively, we could diddle with '$' and the following number,
749          but keeping the registers as symbols helps keep parsing simple.  */
750       sprintf (buf, "$%d", i);
751       symbol_table_insert (symbol_new (buf, reg_section, i,
752                                        &zero_address_frag));
753     }
754
755   /* Insert mmixal built-in names if allowed.  */
756   if (predefined_syms)
757     {
758       for (i = 0; mmix_spec_regs[i].name != NULL; i++)
759         symbol_table_insert (symbol_new (mmix_spec_regs[i].name,
760                                          reg_section,
761                                          mmix_spec_regs[i].number + 256,
762                                          &zero_address_frag));
763
764       /* FIXME: Perhaps these should be recognized as specials; as field
765          names for those instructions.  */
766       symbol_table_insert (symbol_new ("ROUND_CURRENT", reg_section, 512,
767                                        &zero_address_frag));
768       symbol_table_insert (symbol_new ("ROUND_OFF", reg_section, 512 + 1,
769                                        &zero_address_frag));
770       symbol_table_insert (symbol_new ("ROUND_UP", reg_section, 512 + 2,
771                                        &zero_address_frag));
772       symbol_table_insert (symbol_new ("ROUND_DOWN", reg_section, 512 + 3,
773                                        &zero_address_frag));
774       symbol_table_insert (symbol_new ("ROUND_NEAR", reg_section, 512 + 4,
775                                        &zero_address_frag));
776     }
777 }
778
779 /* Assemble one insn in STR.  */
780
781 void
782 md_assemble (str)
783      char *str;
784 {
785   char *operands = str;
786   char modified_char = 0;
787   struct mmix_opcode *instruction;
788   fragS *opc_fragP = NULL;
789   int max_operands = 3;
790
791   /* Note that the struct frag member fr_literal in frags.h is char[], so
792      I have to make this a plain char *.  */
793   /* unsigned */ char *opcodep = NULL;
794
795   expressionS exp[4];
796   int n_operands = 0;
797
798   /* Move to end of opcode.  */
799   for (operands = str;
800        is_part_of_name (*operands);
801        ++operands)
802     ;
803
804   if (ISSPACE (*operands))
805     {
806       modified_char = *operands;
807       *operands++ = '\0';
808     }
809
810   instruction = (struct mmix_opcode *) hash_find (mmix_opcode_hash, str);
811   if (instruction == NULL)
812     {
813       as_bad (_("unknown opcode: `%s'"), str);
814
815       /* Avoid "unhandled label" errors.  */
816       pending_label = NULL;
817       return;
818     }
819
820   /* Put back the character after the opcode.  */
821   if (modified_char != 0)
822     operands[-1] = modified_char;
823
824   input_line_pointer = operands;
825
826   /* Is this a mmixal pseudodirective?  */
827   if (instruction->type == mmix_type_pseudo)
828     {
829       /* For mmixal compatibility, a label for an instruction (and
830          emitting pseudo) refers to the _aligned_ address.  We emit the
831          label here for the pseudos that don't handle it themselves.  When
832          having an fb-label, emit it here, and increment the counter after
833          the pseudo.  */
834       switch (instruction->operands)
835         {
836         case mmix_operands_loc:
837         case mmix_operands_byte:
838         case mmix_operands_prefix:
839         case mmix_operands_local:
840         case mmix_operands_bspec:
841         case mmix_operands_espec:
842           if (current_fb_label >= 0)
843             colon (fb_label_name (current_fb_label, 1));
844           else if (pending_label != NULL)
845             {
846               colon (pending_label);
847               pending_label = NULL;
848             }
849           break;
850
851         default:
852           break;
853         }
854
855       /* Some of the pseudos emit contents, others don't.  Set a
856          contents-emitted flag when we emit something into .text   */
857       switch (instruction->operands)
858         {
859         case mmix_operands_loc:
860           /* LOC */
861           s_loc (0);
862           break;
863
864         case mmix_operands_byte:
865           /* BYTE */
866           mmix_byte ();
867           break;
868
869         case mmix_operands_wyde:
870           /* WYDE */
871           mmix_cons (2);
872           break;
873
874         case mmix_operands_tetra:
875           /* TETRA */
876           mmix_cons (4);
877           break;
878
879         case mmix_operands_octa:
880           /* OCTA */
881           mmix_cons (8);
882           break;
883
884         case mmix_operands_prefix:
885           /* PREFIX */
886           s_prefix (0);
887           break;
888
889         case mmix_operands_local:
890           /* LOCAL */
891           mmix_s_local (0);
892           break;
893
894         case mmix_operands_bspec:
895           /* BSPEC */
896           s_bspec (0);
897           break;
898
899         case mmix_operands_espec:
900           /* ESPEC */
901           s_espec (0);
902           break;
903
904         default:
905           BAD_CASE (instruction->operands);
906         }
907
908       /* These are all working like the pseudo functions in read.c:s_...,
909          in that they step over the end-of-line marker at the end of the
910          line.  We don't want that here.  */
911       input_line_pointer--;
912
913       /* Step up the fb-label counter if there was a definition on this
914          line.  */
915       if (current_fb_label >= 0)
916         {
917           fb_label_instance_inc (current_fb_label);
918           current_fb_label = -1;
919         }
920
921       /* Reset any don't-align-next-datum request, unless this was a LOC
922          directive.  */
923       if (instruction->operands != mmix_operands_loc)
924         want_unaligned = 0;
925
926       return;
927     }
928
929   /* Not a pseudo; we *will* emit contents.  */
930   if (now_seg == data_section)
931     {
932       if (lowest_data_loc != (bfd_vma) -1 && (lowest_data_loc & 3) != 0)
933         {
934           if (data_has_contents)
935             as_bad (_("specified location wasn't TETRA-aligned"));
936           else if (want_unaligned)
937             as_bad (_("unaligned data at an absolute location is not supported"));
938
939           lowest_data_loc &= ~(bfd_vma) 3;
940           lowest_data_loc += 4;
941         }
942
943       data_has_contents = 1;
944     }
945   else if (now_seg == text_section)
946     {
947       if (lowest_text_loc != (bfd_vma) -1 && (lowest_text_loc & 3) != 0)
948         {
949           if (text_has_contents)
950             as_bad (_("specified location wasn't TETRA-aligned"));
951           else if (want_unaligned)
952             as_bad (_("unaligned data at an absolute location is not supported"));
953
954           lowest_text_loc &= ~(bfd_vma) 3;
955           lowest_text_loc += 4;
956         }
957
958       text_has_contents = 1;
959     }
960
961   /* After a sequence of BYTEs or WYDEs, we need to get to instruction
962      alignment.  For other pseudos, a ".p2align 2" is supposed to be
963      inserted by the user.  */
964   if (last_alignment < 2 && ! want_unaligned)
965     {
966       frag_align (2, 0, 0);
967       record_alignment (now_seg, 2);
968       last_alignment = 2;
969     }
970   else
971     /* Reset any don't-align-next-datum request.  */
972     want_unaligned = 0;
973
974   /* For mmixal compatibility, a label for an instruction (and emitting
975      pseudo) refers to the _aligned_ address.  So we have to emit the
976      label here.  */
977   if (pending_label != NULL)
978     {
979       colon (pending_label);
980       pending_label = NULL;
981     }
982
983   /* We assume that mmix_opcodes keeps having unique mnemonics for each
984      opcode, so we don't have to iterate over more than one opcode; if the
985      syntax does not match, then there's a syntax error.  */
986
987   /* Operands have little or no context and are all comma-separated; it is
988      easier to parse each expression first.   */
989   switch (instruction->operands)
990     {
991     case mmix_operands_reg_yz:
992     case mmix_operands_pop:
993     case mmix_operands_regaddr:
994     case mmix_operands_pushj:
995     case mmix_operands_get:
996     case mmix_operands_put:
997     case mmix_operands_set:
998     case mmix_operands_save:
999     case mmix_operands_unsave:
1000       max_operands = 2;
1001       break;
1002
1003     case mmix_operands_sync:
1004     case mmix_operands_jmp:
1005     case mmix_operands_resume:
1006       max_operands = 1;
1007       break;
1008
1009       /* The original 3 is fine for the rest.  */
1010     default:
1011       break;
1012     }
1013
1014   /* If this is GET or PUT, and we don't do allow those names to be
1015      equated, we need to parse the names ourselves, so we don't pick up a
1016      user label instead of the special register.  */
1017   if (! equated_spec_regs
1018       && (instruction->operands == mmix_operands_get
1019           || instruction->operands == mmix_operands_put))
1020     n_operands = get_putget_operands (instruction, operands, exp);
1021   else
1022     n_operands = get_operands (max_operands, operands, exp);
1023
1024   /* If there's a fb-label on the current line, set that label.  This must
1025      be done *after* evaluating expressions of operands, since neither a
1026      "1B" nor a "1F" refers to "1H" on the same line.  */
1027   if (current_fb_label >= 0)
1028     {
1029       fb_label_instance_inc (current_fb_label);
1030       colon (fb_label_name (current_fb_label, 0));
1031       current_fb_label = -1;
1032     }
1033
1034   /* We also assume that the length of the instruction is determinable
1035      from the first format character.  Currently *all* the information is
1036      in the first character.  We need a self-contained frag since we want
1037      the relocation to point to the instruction, not the variant part.  */
1038
1039   opcodep = frag_more (4);
1040   mmix_opcode_frag = opc_fragP = frag_now;
1041   frag_now->fr_opcode = opcodep;
1042
1043   /* Mark start of insn for DWARF2 debug features.  */
1044   if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
1045     dwarf2_emit_insn (4);
1046
1047   md_number_to_chars (opcodep, instruction->match, 4);
1048
1049   switch (instruction->operands)
1050     {
1051     case mmix_operands_jmp:
1052       if (n_operands == 0 && ! mmix_gnu_syntax)
1053         /* Zeros are in place - nothing needs to be done when we have no
1054            operands.  */
1055         break;
1056
1057       /* Add a frag for a JMP relaxation; we need room for max four
1058          extra instructions.  We don't do any work around here to check if
1059          we can determine the offset right away.  */
1060       if (n_operands != 1 || exp[0].X_op == O_register)
1061         {
1062           as_bad (_("invalid operand to opcode %s: `%s'"),
1063                   instruction->name, operands);
1064           return;
1065         }
1066
1067       if (expand_op)
1068         frag_var (rs_machine_dependent, 4*4, 0,
1069                   ENCODE_RELAX (STATE_JMP, STATE_UNDF),
1070                   exp[0].X_add_symbol,
1071                   exp[0].X_add_number,
1072                   opcodep);
1073       else
1074         fix_new_exp (opc_fragP, opcodep - opc_fragP->fr_literal, 4,
1075                      exp + 0, 1, BFD_RELOC_MMIX_ADDR27);
1076       break;
1077
1078     case mmix_operands_pushj:
1079       /* We take care of PUSHJ in full here.  */
1080       if (n_operands != 2
1081           || ((exp[0].X_op == O_constant || exp[0].X_op == O_register)
1082               && (exp[0].X_add_number > 255 || exp[0].X_add_number < 0)))
1083         {
1084           as_bad (_("invalid operands to opcode %s: `%s'"),
1085                   instruction->name, operands);
1086           return;
1087         }
1088
1089       if (exp[0].X_op == O_register || exp[0].X_op == O_constant)
1090         opcodep[1] = exp[0].X_add_number;
1091       else
1092         fix_new_exp (opc_fragP, opcodep - opc_fragP->fr_literal + 1,
1093                      1, exp + 0, 0, BFD_RELOC_MMIX_REG_OR_BYTE);
1094
1095       if (expand_op)
1096         frag_var (rs_machine_dependent, PUSHJ_MAX_LEN - 4, 0,
1097                   ENCODE_RELAX (STATE_PUSHJ, STATE_UNDF),
1098                   exp[1].X_add_symbol,
1099                   exp[1].X_add_number,
1100                   opcodep);
1101       else
1102         fix_new_exp (opc_fragP, opcodep - opc_fragP->fr_literal, 4,
1103                      exp + 1, 1, BFD_RELOC_MMIX_ADDR19);
1104       break;
1105
1106     case mmix_operands_regaddr:
1107       /* GETA/branch: Add a frag for relaxation.  We don't do any work
1108          around here to check if we can determine the offset right away.  */
1109       if (n_operands != 2 || exp[1].X_op == O_register)
1110         {
1111           as_bad (_("invalid operands to opcode %s: `%s'"),
1112                   instruction->name, operands);
1113           return;
1114         }
1115
1116       if (! expand_op)
1117         fix_new_exp (opc_fragP, opcodep - opc_fragP->fr_literal, 4,
1118                      exp + 1, 1, BFD_RELOC_MMIX_ADDR19);
1119       else if (instruction->type == mmix_type_condbranch)
1120         frag_var (rs_machine_dependent, BCC_MAX_LEN - 4, 0,
1121                   ENCODE_RELAX (STATE_BCC, STATE_UNDF),
1122                   exp[1].X_add_symbol,
1123                   exp[1].X_add_number,
1124                   opcodep);
1125       else
1126         frag_var (rs_machine_dependent, GETA_MAX_LEN - 4, 0,
1127                   ENCODE_RELAX (STATE_GETA, STATE_UNDF),
1128                   exp[1].X_add_symbol,
1129                   exp[1].X_add_number,
1130                   opcodep);
1131       break;
1132
1133     default:
1134       break;
1135     }
1136
1137   switch (instruction->operands)
1138     {
1139     case mmix_operands_regs:
1140       /* We check the number of operands here, since we're in a
1141          FALLTHROUGH sequence in the next switch.  */
1142       if (n_operands != 3 || exp[2].X_op == O_constant)
1143         {
1144           as_bad (_("invalid operands to opcode %s: `%s'"),
1145                   instruction->name, operands);
1146           return;
1147         }
1148       /* FALLTHROUGH.  */
1149     case mmix_operands_regs_z:
1150       if (n_operands != 3)
1151         {
1152           as_bad (_("invalid operands to opcode %s: `%s'"),
1153                   instruction->name, operands);
1154           return;
1155         }
1156       /* FALLTHROUGH.  */
1157     case mmix_operands_reg_yz:
1158     case mmix_operands_roundregs_z:
1159     case mmix_operands_roundregs:
1160     case mmix_operands_regs_z_opt:
1161     case mmix_operands_neg:
1162     case mmix_operands_regaddr:
1163     case mmix_operands_get:
1164     case mmix_operands_set:
1165     case mmix_operands_save:
1166       if (n_operands < 1
1167           || (exp[0].X_op == O_register && exp[0].X_add_number > 255))
1168         {
1169           as_bad (_("invalid operands to opcode %s: `%s'"),
1170                   instruction->name, operands);
1171           return;
1172         }
1173
1174       if (exp[0].X_op == O_register)
1175         opcodep[1] = exp[0].X_add_number;
1176       else
1177         fix_new_exp (opc_fragP, opcodep - opc_fragP->fr_literal + 1,
1178                      1, exp + 0, 0, BFD_RELOC_MMIX_REG);
1179       break;
1180
1181     default:
1182       ;
1183     }
1184
1185   /* A corresponding once-over for those who take an 8-bit constant as
1186      their first operand.  */
1187   switch (instruction->operands)
1188     {
1189     case mmix_operands_pushgo:
1190       /* PUSHGO: X is a constant, but can be expressed as a register.
1191          We handle X here and use the common machinery of T,X,3,$ for
1192          the rest of the operands.  */
1193       if (n_operands < 2
1194           || ((exp[0].X_op == O_constant || exp[0].X_op == O_register)
1195               && (exp[0].X_add_number > 255 || exp[0].X_add_number < 0)))
1196         {
1197           as_bad (_("invalid operands to opcode %s: `%s'"),
1198                   instruction->name, operands);
1199           return;
1200         }
1201       else if (exp[0].X_op == O_constant || exp[0].X_op == O_register)
1202         opcodep[1] = exp[0].X_add_number;
1203       else
1204         fix_new_exp (opc_fragP, opcodep - opc_fragP->fr_literal + 1,
1205                      1, exp + 0, 0, BFD_RELOC_MMIX_REG_OR_BYTE);
1206       break;
1207
1208     case mmix_operands_pop:
1209       if ((n_operands == 0 || n_operands == 1) && ! mmix_gnu_syntax)
1210         break;
1211       /* FALLTHROUGH.  */
1212     case mmix_operands_x_regs_z:
1213       if (n_operands < 1
1214           || (exp[0].X_op == O_constant
1215               && (exp[0].X_add_number > 255
1216                   || exp[0].X_add_number < 0)))
1217         {
1218           as_bad (_("invalid operands to opcode %s: `%s'"),
1219                   instruction->name, operands);
1220           return;
1221         }
1222
1223       if (exp[0].X_op == O_constant)
1224         opcodep[1] = exp[0].X_add_number;
1225       else
1226         /* FIXME: This doesn't bring us unsignedness checking.  */
1227         fix_new_exp (opc_fragP, opcodep - opc_fragP->fr_literal + 1,
1228                      1, exp + 0, 0, BFD_RELOC_8);
1229     default:
1230       ;
1231     }
1232
1233   /* Handle the rest.  */
1234   switch (instruction->operands)
1235     {
1236     case mmix_operands_set:
1237       /* SET: Either two registers, "$X,$Y", with Z field as zero, or
1238          "$X,YZ", meaning change the opcode to SETL.  */
1239       if (n_operands != 2
1240           || (exp[1].X_op == O_constant
1241               && (exp[1].X_add_number > 0xffff || exp[1].X_add_number < 0)))
1242         {
1243           as_bad (_("invalid operands to opcode %s: `%s'"),
1244                   instruction->name, operands);
1245           return;
1246         }
1247
1248       if (exp[1].X_op == O_constant)
1249         {
1250           /* There's an ambiguity with "SET $0,Y" when Y isn't defined
1251              yet.  To keep things simple, we assume that Y is then a
1252              register, and only change the opcode if Y is defined at this
1253              point.
1254
1255              There's no compatibility problem with mmixal, since it emits
1256              errors if the field is not defined at this point.  */
1257           md_number_to_chars (opcodep, SETL_INSN_BYTE, 1);
1258
1259           opcodep[2] = (exp[1].X_add_number >> 8) & 255;
1260           opcodep[3] = exp[1].X_add_number & 255;
1261           break;
1262         }
1263       /* FALLTHROUGH.  */
1264     case mmix_operands_x_regs_z:
1265       /* SYNCD: "X,$Y,$Z|Z".  */
1266       /* FALLTHROUGH.  */
1267     case mmix_operands_regs:
1268       /* Three registers, $X,$Y,$Z. */
1269       /* FALLTHROUGH.  */
1270     case mmix_operands_regs_z:
1271       /* Operands "$X,$Y,$Z|Z", number of arguments checked above.  */
1272       /* FALLTHROUGH.  */
1273     case mmix_operands_pushgo:
1274       /* Operands "$X|X,$Y,$Z|Z", optional Z.  */
1275       /* FALLTHROUGH.  */
1276     case mmix_operands_regs_z_opt:
1277       /* Operands "$X,$Y,$Z|Z", with $Z|Z being optional, default 0.  Any
1278          operands not completely decided yet are postponed to later in
1279          assembly (but not until link-time yet).  */
1280
1281       if ((n_operands != 2 && n_operands != 3)
1282           || (exp[1].X_op == O_register && exp[1].X_add_number > 255)
1283           || (n_operands == 3
1284               && ((exp[2].X_op == O_register
1285                    && exp[2].X_add_number > 255
1286                    && mmix_gnu_syntax)
1287                   || (exp[2].X_op == O_constant
1288                       && (exp[2].X_add_number > 255
1289                           || exp[2].X_add_number < 0)))))
1290         {
1291           as_bad (_("invalid operands to opcode %s: `%s'"),
1292                   instruction->name, operands);
1293           return;
1294         }
1295
1296       if (n_operands == 2)
1297         {
1298           symbolS *sym;
1299
1300           /* The last operand is immediate whenever we see just two
1301              operands.  */
1302           opcodep[0] |= IMM_OFFSET_BIT;
1303
1304           /* Now, we could either have an implied "0" as the Z operand, or
1305              it could be the constant of a "base address plus offset".  It
1306              depends on whether it is allowed; only memory operations, as
1307              signified by instruction->type and "T" and "X" operand types,
1308              and it depends on whether we find a register in the second
1309              operand, exp[1].  */
1310           if (exp[1].X_op == O_register && exp[1].X_add_number <= 255)
1311             {
1312               /* A zero then; all done.  */
1313               opcodep[2] = exp[1].X_add_number;
1314               break;
1315             }
1316
1317           /* Not known as a register.  Is base address plus offset
1318              allowed, or can we assume that it is a register anyway?  */
1319           if ((instruction->operands != mmix_operands_regs_z_opt
1320                && instruction->operands != mmix_operands_x_regs_z
1321                && instruction->operands != mmix_operands_pushgo)
1322               || (instruction->type != mmix_type_memaccess_octa
1323                   && instruction->type != mmix_type_memaccess_tetra
1324                   && instruction->type != mmix_type_memaccess_wyde
1325                   && instruction->type != mmix_type_memaccess_byte
1326                   && instruction->type != mmix_type_memaccess_block
1327                   && instruction->type != mmix_type_jsr
1328                   && instruction->type != mmix_type_branch))
1329             {
1330               fix_new_exp (opc_fragP, opcodep - opc_fragP->fr_literal + 2,
1331                            1, exp + 1, 0, BFD_RELOC_MMIX_REG);
1332               break;
1333             }
1334
1335           /* To avoid getting a NULL add_symbol for constants and then
1336              catching a SEGV in write_relocs since it doesn't handle
1337              constants well for relocs other than PC-relative, we need to
1338              pass expressions as symbols and use fix_new, not fix_new_exp.  */
1339           sym = make_expr_symbol (exp + 1);
1340
1341           /* Now we know it can be a "base address plus offset".  Add
1342              proper fixup types so we can handle this later, when we've
1343              parsed everything.  */
1344           fix_new (opc_fragP, opcodep - opc_fragP->fr_literal + 2,
1345                    8, sym, 0, 0, BFD_RELOC_MMIX_BASE_PLUS_OFFSET);
1346           break;
1347         }
1348
1349       if (exp[1].X_op == O_register)
1350         opcodep[2] = exp[1].X_add_number;
1351       else
1352         fix_new_exp (opc_fragP, opcodep - opc_fragP->fr_literal + 2,
1353                      1, exp + 1, 0, BFD_RELOC_MMIX_REG);
1354
1355       /* In mmixal compatibility mode, we allow special registers as
1356          constants for the Z operand.  They have 256 added to their
1357          register numbers, so the right thing will happen if we just treat
1358          those as constants.  */
1359       if (exp[2].X_op == O_register && exp[2].X_add_number <= 255)
1360         opcodep[3] = exp[2].X_add_number;
1361       else if (exp[2].X_op == O_constant
1362                || (exp[2].X_op == O_register && exp[2].X_add_number > 255))
1363         {
1364           opcodep[3] = exp[2].X_add_number;
1365           opcodep[0] |= IMM_OFFSET_BIT;
1366         }
1367       else
1368         fix_new_exp (opc_fragP, opcodep - opc_fragP->fr_literal + 3,
1369                      1, exp + 2, 0,
1370                      (instruction->operands == mmix_operands_set
1371                       || instruction->operands == mmix_operands_regs)
1372                      ? BFD_RELOC_MMIX_REG : BFD_RELOC_MMIX_REG_OR_BYTE);
1373       break;
1374
1375     case mmix_operands_pop:
1376       /* POP, one eight and one 16-bit operand.  */
1377       if (n_operands == 0 && ! mmix_gnu_syntax)
1378         break;
1379       if (n_operands == 1 && ! mmix_gnu_syntax)
1380         goto a_single_24_bit_number_operand;
1381       /* FALLTHROUGH.  */
1382     case mmix_operands_reg_yz:
1383       /* A register and a 16-bit unsigned number.  */
1384       if (n_operands != 2
1385           || exp[1].X_op == O_register
1386           || (exp[1].X_op == O_constant
1387               && (exp[1].X_add_number > 0xffff || exp[1].X_add_number < 0)))
1388         {
1389           as_bad (_("invalid operands to opcode %s: `%s'"),
1390                   instruction->name, operands);
1391           return;
1392         }
1393
1394       if (exp[1].X_op == O_constant)
1395         {
1396           opcodep[2] = (exp[1].X_add_number >> 8) & 255;
1397           opcodep[3] = exp[1].X_add_number & 255;
1398         }
1399       else
1400         /* FIXME: This doesn't bring us unsignedness checking.  */
1401         fix_new_exp (opc_fragP, opcodep - opc_fragP->fr_literal + 2,
1402                      2, exp + 1, 0, BFD_RELOC_16);
1403       break;
1404
1405     case mmix_operands_jmp:
1406       /* A JMP.  Everyhing is already done.  */
1407       break;
1408
1409     case mmix_operands_roundregs:
1410       /* Two registers with optional rounding mode or constant in between.  */
1411       if ((n_operands == 3 && exp[2].X_op == O_constant)
1412           || (n_operands == 2 && exp[1].X_op == O_constant))
1413         {
1414           as_bad (_("invalid operands to opcode %s: `%s'"),
1415                   instruction->name, operands);
1416           return;
1417         }
1418       /* FALLTHROUGH.  */
1419     case mmix_operands_roundregs_z:
1420       /* Like FLOT, "$X,ROUND_MODE,$Z|Z", but the rounding mode is
1421          optional and can be the corresponding constant.  */
1422       {
1423         /* Which exp index holds the second operand (not the rounding
1424            mode).  */
1425         int op2no = n_operands - 1;
1426
1427         if ((n_operands != 2 && n_operands != 3)
1428             || ((exp[op2no].X_op == O_register
1429                  && exp[op2no].X_add_number > 255)
1430                 || (exp[op2no].X_op == O_constant
1431                     && (exp[op2no].X_add_number > 255
1432                         || exp[op2no].X_add_number < 0)))
1433             || (n_operands == 3
1434                 /* We don't allow for the rounding mode to be deferred; it
1435                    must be determined in the "first pass".  It cannot be a
1436                    symbol equated to a rounding mode, but defined after
1437                    the first use.  */
1438                 && ((exp[1].X_op == O_register
1439                      && exp[1].X_add_number < 512)
1440                     || (exp[1].X_op == O_constant
1441                         && exp[1].X_add_number < 0
1442                         && exp[1].X_add_number > 4)
1443                     || (exp[1].X_op != O_register
1444                         && exp[1].X_op != O_constant))))
1445           {
1446             as_bad (_("invalid operands to opcode %s: `%s'"),
1447                     instruction->name, operands);
1448             return;
1449           }
1450
1451         /* Add rounding mode if present.  */
1452         if (n_operands == 3)
1453           opcodep[2] = exp[1].X_add_number & 255;
1454
1455         if (exp[op2no].X_op == O_register)
1456           opcodep[3] = exp[op2no].X_add_number;
1457         else if (exp[op2no].X_op == O_constant)
1458           {
1459             opcodep[3] = exp[op2no].X_add_number;
1460             opcodep[0] |= IMM_OFFSET_BIT;
1461           }
1462         else
1463           fix_new_exp (opc_fragP, opcodep - opc_fragP->fr_literal + 3,
1464                        1, exp + op2no, 0,
1465                        instruction->operands == mmix_operands_roundregs
1466                        ? BFD_RELOC_MMIX_REG
1467                        : BFD_RELOC_MMIX_REG_OR_BYTE);
1468         break;
1469       }
1470
1471     case mmix_operands_sync:
1472     a_single_24_bit_number_operand:
1473     if (n_operands != 1
1474         || exp[0].X_op == O_register
1475         || (exp[0].X_op == O_constant
1476             && (exp[0].X_add_number > 0xffffff || exp[0].X_add_number < 0)))
1477       {
1478         as_bad (_("invalid operands to opcode %s: `%s'"),
1479                 instruction->name, operands);
1480         return;
1481       }
1482
1483     if (exp[0].X_op == O_constant)
1484       {
1485         opcodep[1] = (exp[0].X_add_number >> 16) & 255;
1486         opcodep[2] = (exp[0].X_add_number >> 8) & 255;
1487         opcodep[3] = exp[0].X_add_number & 255;
1488       }
1489     else
1490       /* FIXME: This doesn't bring us unsignedness checking.  */
1491       fix_new_exp (opc_fragP, opcodep - opc_fragP->fr_literal + 1,
1492                    3, exp + 0, 0, BFD_RELOC_24);
1493     break;
1494
1495     case mmix_operands_neg:
1496       /* Operands "$X,Y,$Z|Z"; NEG or NEGU.  Y is optional, 0 is default.  */
1497
1498       if ((n_operands != 3 && n_operands != 2)
1499           || (n_operands == 3 && exp[1].X_op == O_register)
1500           || ((exp[1].X_op == O_constant || exp[1].X_op == O_register)
1501               && (exp[1].X_add_number > 255 || exp[1].X_add_number < 0))
1502           || (n_operands == 3
1503               && ((exp[2].X_op == O_register && exp[2].X_add_number > 255)
1504                   || (exp[2].X_op == O_constant
1505                       && (exp[2].X_add_number > 255
1506                           || exp[2].X_add_number < 0)))))
1507         {
1508           as_bad (_("invalid operands to opcode %s: `%s'"),
1509                   instruction->name, operands);
1510           return;
1511         }
1512
1513       if (n_operands == 2)
1514         {
1515           if (exp[1].X_op == O_register)
1516             opcodep[3] = exp[1].X_add_number;
1517           else if (exp[1].X_op == O_constant)
1518             {
1519               opcodep[3] = exp[1].X_add_number;
1520               opcodep[0] |= IMM_OFFSET_BIT;
1521             }
1522           else
1523             fix_new_exp (opc_fragP, opcodep - opc_fragP->fr_literal + 3,
1524                          1, exp + 1, 0, BFD_RELOC_MMIX_REG_OR_BYTE);
1525           break;
1526         }
1527
1528       if (exp[1].X_op == O_constant)
1529         opcodep[2] = exp[1].X_add_number;
1530       else
1531         fix_new_exp (opc_fragP, opcodep - opc_fragP->fr_literal + 2,
1532                      1, exp + 1, 0, BFD_RELOC_8);
1533
1534       if (exp[2].X_op == O_register)
1535         opcodep[3] = exp[2].X_add_number;
1536       else if (exp[2].X_op == O_constant)
1537         {
1538           opcodep[3] = exp[2].X_add_number;
1539           opcodep[0] |= IMM_OFFSET_BIT;
1540         }
1541       else
1542         fix_new_exp (opc_fragP, opcodep - opc_fragP->fr_literal + 3,
1543                      1, exp + 2, 0, BFD_RELOC_MMIX_REG_OR_BYTE);
1544       break;
1545
1546     case mmix_operands_regaddr:
1547       /* A GETA/branch-type. */
1548       break;
1549
1550     case mmix_operands_get:
1551       /* "$X,spec_reg"; GET.
1552          Like with rounding modes, we demand that the special register or
1553          symbol is already defined when we get here at the point of use.  */
1554       if (n_operands != 2
1555           || (exp[1].X_op == O_register
1556               && (exp[1].X_add_number < 256 || exp[1].X_add_number >= 512))
1557           || (exp[1].X_op == O_constant
1558               && (exp[1].X_add_number < 0 || exp[1].X_add_number > 256))
1559           || (exp[1].X_op != O_constant && exp[1].X_op != O_register))
1560         {
1561           as_bad (_("invalid operands to opcode %s: `%s'"),
1562                   instruction->name, operands);
1563           return;
1564         }
1565
1566       opcodep[3] = exp[1].X_add_number - 256;
1567       break;
1568
1569     case mmix_operands_put:
1570       /* "spec_reg,$Z|Z"; PUT.  */
1571       if (n_operands != 2
1572           || (exp[0].X_op == O_register
1573               && (exp[0].X_add_number < 256 || exp[0].X_add_number >= 512))
1574           || (exp[0].X_op == O_constant
1575               && (exp[0].X_add_number < 0 || exp[0].X_add_number > 256))
1576           || (exp[0].X_op != O_constant && exp[0].X_op != O_register))
1577         {
1578           as_bad (_("invalid operands to opcode %s: `%s'"),
1579                   instruction->name, operands);
1580           return;
1581         }
1582
1583       opcodep[1] = exp[0].X_add_number - 256;
1584
1585       /* Note that the Y field is zero.  */
1586
1587       if (exp[1].X_op == O_register)
1588         opcodep[3] = exp[1].X_add_number;
1589       else if (exp[1].X_op == O_constant)
1590         {
1591           opcodep[3] = exp[1].X_add_number;
1592           opcodep[0] |= IMM_OFFSET_BIT;
1593         }
1594       else
1595         fix_new_exp (opc_fragP, opcodep - opc_fragP->fr_literal + 3,
1596                      1, exp + 1, 0, BFD_RELOC_MMIX_REG_OR_BYTE);
1597       break;
1598
1599     case mmix_operands_save:
1600       /* "$X,0"; SAVE.  */
1601       if (n_operands != 2
1602           || exp[1].X_op != O_constant
1603           || exp[1].X_add_number != 0)
1604         {
1605           as_bad (_("invalid operands to opcode %s: `%s'"),
1606                   instruction->name, operands);
1607           return;
1608         }
1609       break;
1610
1611     case mmix_operands_unsave:
1612       if (n_operands < 2 && ! mmix_gnu_syntax)
1613         {
1614           if (n_operands == 1)
1615             {
1616               if (exp[0].X_op == O_register)
1617                 opcodep[3] = exp[0].X_add_number;
1618               else
1619                 fix_new_exp (opc_fragP, opcodep - opc_fragP->fr_literal + 3,
1620                              1, exp, 0, BFD_RELOC_MMIX_REG);
1621             }
1622           break;
1623         }
1624
1625       /* "0,$Z"; UNSAVE. */
1626       if (n_operands != 2
1627           || exp[0].X_op != O_constant
1628           || exp[0].X_add_number != 0
1629           || exp[1].X_op == O_constant
1630           || (exp[1].X_op == O_register
1631               && exp[1].X_add_number > 255))
1632         {
1633           as_bad (_("invalid operands to opcode %s: `%s'"),
1634                   instruction->name, operands);
1635           return;
1636         }
1637
1638       if (exp[1].X_op == O_register)
1639         opcodep[3] = exp[1].X_add_number;
1640       else
1641         fix_new_exp (opc_fragP, opcodep - opc_fragP->fr_literal + 3,
1642                      1, exp + 1, 0, BFD_RELOC_MMIX_REG);
1643       break;
1644
1645     case mmix_operands_xyz_opt:
1646       /* SWYM, TRIP, TRAP: zero, one, two or three operands.  */
1647       if (n_operands == 0 && ! mmix_gnu_syntax)
1648         /* Zeros are in place - nothing needs to be done for zero
1649            operands.  We don't allow this in GNU syntax mode, because it
1650            was believed that the risk of missing to supply an operand is
1651            higher than the benefit of not having to specify a zero.  */
1652         ;
1653       else if (n_operands == 1 && exp[0].X_op != O_register)
1654         {
1655           if (exp[0].X_op == O_constant)
1656             {
1657               if (exp[0].X_add_number > 255*255*255
1658                   || exp[0].X_add_number < 0)
1659                 {
1660                   as_bad (_("invalid operands to opcode %s: `%s'"),
1661                           instruction->name, operands);
1662                   return;
1663                 }
1664               else
1665                 {
1666                   opcodep[1] = (exp[0].X_add_number >> 16) & 255;
1667                   opcodep[2] = (exp[0].X_add_number >> 8) & 255;
1668                   opcodep[3] = exp[0].X_add_number & 255;
1669                 }
1670             }
1671           else
1672             fix_new_exp (opc_fragP, opcodep - opc_fragP->fr_literal + 1,
1673                          3, exp, 0, BFD_RELOC_24);
1674         }
1675       else if (n_operands == 2
1676                && exp[0].X_op != O_register
1677                && exp[1].X_op != O_register)
1678         {
1679           /* Two operands.  */
1680
1681           if (exp[0].X_op == O_constant)
1682             {
1683               if (exp[0].X_add_number > 255
1684                   || exp[0].X_add_number < 0)
1685                 {
1686                   as_bad (_("invalid operands to opcode %s: `%s'"),
1687                           instruction->name, operands);
1688                   return;
1689                 }
1690               else
1691                 opcodep[1] = exp[0].X_add_number & 255;
1692             }
1693           else
1694             fix_new_exp (opc_fragP, opcodep - opc_fragP->fr_literal + 1,
1695                          1, exp, 0, BFD_RELOC_8);
1696
1697           if (exp[1].X_op == O_constant)
1698             {
1699               if (exp[1].X_add_number > 255*255
1700                   || exp[1].X_add_number < 0)
1701                 {
1702                   as_bad (_("invalid operands to opcode %s: `%s'"),
1703                           instruction->name, operands);
1704                   return;
1705                 }
1706               else
1707                 {
1708                   opcodep[2] = (exp[1].X_add_number >> 8) & 255;
1709                   opcodep[3] = exp[1].X_add_number & 255;
1710                 }
1711             }
1712           else
1713             fix_new_exp (opc_fragP, opcodep - opc_fragP->fr_literal + 2,
1714                          2, exp + 1, 0, BFD_RELOC_16);
1715         }
1716       else if (n_operands == 3
1717                && exp[0].X_op != O_register
1718                && exp[1].X_op != O_register
1719                && exp[2].X_op != O_register)
1720         {
1721           /* Three operands.  */
1722
1723           if (exp[0].X_op == O_constant)
1724             {
1725               if (exp[0].X_add_number > 255
1726                   || exp[0].X_add_number < 0)
1727                 {
1728                   as_bad (_("invalid operands to opcode %s: `%s'"),
1729                           instruction->name, operands);
1730                   return;
1731                 }
1732               else
1733                 opcodep[1] = exp[0].X_add_number & 255;
1734             }
1735           else
1736             fix_new_exp (opc_fragP, opcodep - opc_fragP->fr_literal + 1,
1737                          1, exp, 0, BFD_RELOC_8);
1738
1739           if (exp[1].X_op == O_constant)
1740             {
1741               if (exp[1].X_add_number > 255
1742                   || exp[1].X_add_number < 0)
1743                 {
1744                   as_bad (_("invalid operands to opcode %s: `%s'"),
1745                           instruction->name, operands);
1746                   return;
1747                 }
1748               else
1749                 opcodep[2] = exp[1].X_add_number & 255;
1750             }
1751           else
1752             fix_new_exp (opc_fragP, opcodep - opc_fragP->fr_literal + 2,
1753                          1, exp + 1, 0, BFD_RELOC_8);
1754
1755           if (exp[2].X_op == O_constant)
1756             {
1757               if (exp[2].X_add_number > 255
1758                   || exp[2].X_add_number < 0)
1759                 {
1760                   as_bad (_("invalid operands to opcode %s: `%s'"),
1761                           instruction->name, operands);
1762                   return;
1763                 }
1764               else
1765                 opcodep[3] = exp[2].X_add_number & 255;
1766             }
1767           else
1768             fix_new_exp (opc_fragP, opcodep - opc_fragP->fr_literal + 3,
1769                          1, exp + 2, 0, BFD_RELOC_8);
1770         }
1771       else if (n_operands <= 3
1772                && (strcmp (instruction->name, "trip") == 0
1773                    || strcmp (instruction->name, "trap") == 0))
1774         {
1775           /* The meaning of operands to TRIP and TRAP are not defined, so
1776              we add combinations not handled above here as we find them.  */
1777           if (n_operands == 3)
1778             {
1779               /* Don't require non-register operands.  Always generate
1780                  fixups, so we don't have to copy lots of code and create
1781                  maintanance problems.  TRIP is supposed to be a rare
1782                  instruction, so the overhead should not matter.  We
1783                  aren't allowed to fix_new_exp for an expression which is
1784                  an  O_register at this point, however.  */
1785               if (exp[0].X_op == O_register)
1786                 opcodep[1] = exp[0].X_add_number;
1787               else
1788                 fix_new_exp (opc_fragP, opcodep - opc_fragP->fr_literal + 1,
1789                              1, exp, 0, BFD_RELOC_MMIX_REG_OR_BYTE);
1790               if (exp[1].X_op == O_register)
1791                 opcodep[2] = exp[1].X_add_number;
1792               else
1793                 fix_new_exp (opc_fragP, opcodep - opc_fragP->fr_literal + 2,
1794                              1, exp + 1, 0, BFD_RELOC_MMIX_REG_OR_BYTE);
1795               if (exp[2].X_op == O_register)
1796                 opcodep[3] = exp[2].X_add_number;
1797               else
1798                 fix_new_exp (opc_fragP, opcodep - opc_fragP->fr_literal + 3,
1799                              1, exp + 2, 0, BFD_RELOC_MMIX_REG_OR_BYTE);
1800             }
1801           else if (n_operands == 2)
1802             {
1803               if (exp[0].X_op == O_register)
1804                 opcodep[2] = exp[0].X_add_number;
1805               else
1806                 fix_new_exp (opc_fragP, opcodep - opc_fragP->fr_literal + 2,
1807                              1, exp, 0, BFD_RELOC_MMIX_REG_OR_BYTE);
1808               if (exp[1].X_op == O_register)
1809                 opcodep[3] = exp[1].X_add_number;
1810               else
1811                 fix_new_exp (opc_fragP, opcodep - opc_fragP->fr_literal + 3,
1812                              1, exp + 1, 0, BFD_RELOC_MMIX_REG_OR_BYTE);
1813             }
1814           else
1815             {
1816               as_bad (_("unsupported operands to %s: `%s'"),
1817                       instruction->name, operands);
1818               return;
1819             }
1820         }
1821       else
1822         {
1823           as_bad (_("invalid operands to opcode %s: `%s'"),
1824                   instruction->name, operands);
1825           return;
1826         }
1827       break;
1828
1829     case mmix_operands_resume:
1830       if (n_operands == 0 && ! mmix_gnu_syntax)
1831         break;
1832
1833       if (n_operands != 1
1834           || exp[0].X_op == O_register
1835           || (exp[0].X_op == O_constant
1836               && (exp[0].X_add_number < 0
1837                   || exp[0].X_add_number > 255)))
1838         {
1839           as_bad (_("invalid operands to opcode %s: `%s'"),
1840                   instruction->name, operands);
1841           return;
1842         }
1843
1844       if (exp[0].X_op == O_constant)
1845         opcodep[3] = exp[0].X_add_number;
1846       else
1847         fix_new_exp (opc_fragP, opcodep - opc_fragP->fr_literal + 3,
1848                      1, exp + 0, 0, BFD_RELOC_8);
1849       break;
1850
1851     case mmix_operands_pushj:
1852       /* All is done for PUSHJ already.  */
1853       break;
1854
1855     default:
1856       BAD_CASE (instruction->operands);
1857     }
1858 }
1859
1860 /* For the benefit of insns that start with a digit, we assemble by way of
1861    tc_unrecognized_line too, through this function.  */
1862
1863 int
1864 mmix_assemble_return_nonzero (str)
1865      char  *str;
1866 {
1867   int last_error_count = had_errors ();
1868   char *s2 = str;
1869   char c;
1870
1871   /* Normal instruction handling downcases, so we must too.  */
1872   while (ISALNUM (*s2))
1873     {
1874       if (ISUPPER ((unsigned char) *s2))
1875         *s2 = TOLOWER (*s2);
1876       s2++;
1877     }
1878
1879   /* Cut the line for sake of the assembly.  */
1880   for (s2 = str; *s2 && *s2 != '\n'; s2++)
1881     ;
1882
1883   c = *s2;
1884   *s2 = 0;
1885   md_assemble (str);
1886   *s2 = c;
1887
1888   return had_errors () == last_error_count;
1889 }
1890
1891 /* The PREFIX pseudo.  */
1892
1893 static void
1894 s_prefix (unused)
1895      int unused ATTRIBUTE_UNUSED;
1896 {
1897   char *p;
1898   int c;
1899
1900   SKIP_WHITESPACE ();
1901
1902   p = input_line_pointer;
1903
1904   c = get_symbol_end ();
1905
1906   /* Reseting prefix?  */
1907   if (*p == ':' && p[1] == 0)
1908     mmix_current_prefix = NULL;
1909   else
1910     {
1911       /* Put this prefix on the mmix symbols obstack.  We could malloc and
1912          free it separately, but then we'd have to worry about that.
1913          People using up memory on prefixes have other problems.  */
1914       obstack_grow (&mmix_sym_obstack, p, strlen (p) + 1);
1915       p = obstack_finish (&mmix_sym_obstack);
1916
1917       /* Accumulate prefixes, and strip a leading ':'.  */
1918       if (mmix_current_prefix != NULL || *p == ':')
1919         p = mmix_prefix_name (p);
1920
1921       mmix_current_prefix = p;
1922     }
1923
1924   *input_line_pointer = c;
1925
1926   mmix_handle_rest_of_empty_line ();
1927 }
1928
1929 /* We implement prefixes by using the tc_canonicalize_symbol_name hook,
1930    and store each prefixed name on a (separate) obstack.  This means that
1931    the name is on the "notes" obstack in non-prefixed form and on the
1932    mmix_sym_obstack in prefixed form, but currently it is not worth
1933    rewriting the whole GAS symbol handling to improve "hooking" to avoid
1934    that.  (It might be worth a rewrite for other reasons, though).  */
1935
1936 char *
1937 mmix_prefix_name (shortname)
1938      char *shortname;
1939 {
1940   if (*shortname == ':')
1941     return shortname + 1;
1942
1943   if (mmix_current_prefix == NULL)
1944     as_fatal (_("internal: mmix_prefix_name but empty prefix"));
1945
1946   if (*shortname == '$')
1947     return shortname;
1948
1949   obstack_grow (&mmix_sym_obstack, mmix_current_prefix,
1950                 strlen (mmix_current_prefix));
1951   obstack_grow (&mmix_sym_obstack, shortname, strlen (shortname) + 1);
1952   return obstack_finish (&mmix_sym_obstack);
1953 }
1954
1955 /* The GREG pseudo.  At LABEL, we have the name of a symbol that we
1956    want to make a register symbol, and which should be initialized with
1957    the value in the expression at INPUT_LINE_POINTER (defaulting to 0).
1958    Either and (perhaps less meaningful) both may be missing.  LABEL must
1959    be persistent, perhaps allocated on an obstack.  */
1960
1961 static void
1962 mmix_greg_internal (label)
1963      char *label;
1964 {
1965   expressionS *expP = &mmix_raw_gregs[n_of_raw_gregs].exp;
1966
1967   /* Don't set the section to register contents section before the
1968      expression has been parsed; it may refer to the current position.  */
1969   expression (expP);
1970
1971   /* FIXME: Check that no expression refers to the register contents
1972      section.  May need to be done in elf64-mmix.c.  */
1973   if (expP->X_op == O_absent)
1974     {
1975       /* Default to zero if the expression was absent.  */
1976       expP->X_op = O_constant;
1977       expP->X_add_number = 0;
1978       expP->X_unsigned = 0;
1979       expP->X_add_symbol = NULL;
1980       expP->X_op_symbol = NULL;
1981     }
1982
1983   /* We must handle prefixes here, as we save the labels and expressions
1984      to be output later.  */
1985   mmix_raw_gregs[n_of_raw_gregs].label
1986     = mmix_current_prefix == NULL ? label : mmix_prefix_name (label);
1987
1988   if (n_of_raw_gregs == MAX_GREGS - 1)
1989     as_bad (_("too many GREG registers allocated (max %d)"), MAX_GREGS);
1990   else
1991     n_of_raw_gregs++;
1992
1993   mmix_handle_rest_of_empty_line ();
1994 }
1995
1996 /* The ".greg label,expr" worker.  */
1997
1998 static void
1999 s_greg (unused)
2000      int unused ATTRIBUTE_UNUSED;
2001 {
2002   char *p;
2003   char c;
2004   p = input_line_pointer;
2005
2006   /* This will skip over what can be a symbol and zero out the next
2007      character, which we assume is a ',' or other meaningful delimiter.
2008      What comes after that is the initializer expression for the
2009      register.  */
2010   c = get_symbol_end ();
2011
2012   if (! is_end_of_line [(unsigned char) c])
2013     input_line_pointer++;
2014
2015   if (*p)
2016     {
2017       /* The label must be persistent; it's not used until after all input
2018          has been seen.  */
2019       obstack_grow (&mmix_sym_obstack, p, strlen (p) + 1);
2020       mmix_greg_internal (obstack_finish (&mmix_sym_obstack));
2021     }
2022   else
2023     mmix_greg_internal (NULL);
2024 }
2025
2026 /* The "BSPEC expr" worker.  */
2027
2028 static void
2029 s_bspec (unused)
2030      int unused ATTRIBUTE_UNUSED;
2031 {
2032   asection *expsec;
2033   asection *sec;
2034   char secname[sizeof (MMIX_OTHER_SPEC_SECTION_PREFIX) + 20]
2035     = MMIX_OTHER_SPEC_SECTION_PREFIX;
2036   expressionS exp;
2037   int n;
2038
2039   /* Get a constant expression which we can evaluate *now*.  Supporting
2040      more complex (though assembly-time computable) expressions is
2041      feasible but Too Much Work for something of unknown usefulness like
2042      BSPEC-ESPEC.  */
2043   expsec = expression (&exp);
2044   mmix_handle_rest_of_empty_line ();
2045
2046   /* Check that we don't have another BSPEC in progress.  */
2047   if (doing_bspec)
2048     {
2049       as_bad (_("BSPEC already active.  Nesting is not supported."));
2050       return;
2051     }
2052
2053   if (exp.X_op != O_constant
2054       || expsec != absolute_section
2055       || exp.X_add_number < 0
2056       || exp.X_add_number > 65535)
2057     {
2058       as_bad (_("invalid BSPEC expression"));
2059       exp.X_add_number = 0;
2060     }
2061
2062   n = (int) exp.X_add_number;
2063
2064   sprintf (secname + strlen (MMIX_OTHER_SPEC_SECTION_PREFIX), "%d", n);
2065   sec = bfd_get_section_by_name (stdoutput, secname);
2066   if (sec == NULL)
2067     {
2068       /* We need a non-volatile name as it will be stored in the section
2069          struct.  */
2070       char *newsecname = xstrdup (secname);
2071       sec = bfd_make_section (stdoutput, newsecname);
2072
2073       if (sec == NULL)
2074         as_fatal (_("can't create section %s"), newsecname);
2075
2076       if (!bfd_set_section_flags (stdoutput, sec,
2077                                   bfd_get_section_flags (stdoutput, sec)
2078                                   | SEC_READONLY))
2079         as_fatal (_("can't set section flags for section %s"), newsecname);
2080     }
2081
2082   /* Tell ELF about the pending section change.  */
2083   obj_elf_section_change_hook ();
2084   subseg_set (sec, 0);
2085
2086   /* Save position for missing ESPEC.  */
2087   as_where (&bspec_file, &bspec_line);
2088
2089   doing_bspec = 1;
2090 }
2091
2092 /* The "ESPEC" worker.  */
2093
2094 static void
2095 s_espec (unused)
2096      int unused ATTRIBUTE_UNUSED;
2097 {
2098   /* First, check that we *do* have a BSPEC in progress.  */
2099   if (! doing_bspec)
2100     {
2101       as_bad (_("ESPEC without preceding BSPEC"));
2102       return;
2103     }
2104
2105   mmix_handle_rest_of_empty_line ();
2106   doing_bspec = 0;
2107
2108   /* When we told ELF about the section change in s_bspec, it stored the
2109      previous section for us so we can get at it with the equivalent of a
2110      .previous pseudo.  */
2111   obj_elf_previous (0);
2112 }
2113
2114 /* The " .local expr" and " local expr" worker.  We make a BFD_MMIX_LOCAL
2115    relocation against the current position against the expression.
2116    Implementing this by means of contents in a section lost.  */
2117
2118 static void
2119 mmix_s_local (unused)
2120      int unused ATTRIBUTE_UNUSED;
2121 {
2122   expressionS exp;
2123
2124   /* Don't set the section to register contents section before the
2125      expression has been parsed; it may refer to the current position in
2126      some contorted way.  */
2127   expression (&exp);
2128
2129   if (exp.X_op == O_absent)
2130     {
2131       as_bad (_("missing local expression"));
2132       return;
2133     }
2134   else if (exp.X_op == O_register)
2135     {
2136       /* fix_new_exp doesn't like O_register.  Should be configurable.
2137          We're fine with a constant here, though.  */
2138       exp.X_op = O_constant;
2139     }
2140
2141   fix_new_exp (frag_now, 0, 0, &exp, 0, BFD_RELOC_MMIX_LOCAL);
2142   mmix_handle_rest_of_empty_line ();
2143 }
2144
2145 /* Set fragP->fr_var to the initial guess of the size of a relaxable insn
2146    and return it.  Sizes of other instructions are not known.  This
2147    function may be called multiple times.  */
2148
2149 int
2150 md_estimate_size_before_relax (fragP, segment)
2151      fragS *fragP;
2152      segT    segment;
2153 {
2154   int length;
2155
2156 #define HANDLE_RELAXABLE(state)                                         \
2157  case ENCODE_RELAX (state, STATE_UNDF):                                 \
2158    if (fragP->fr_symbol != NULL                                         \
2159        && S_GET_SEGMENT (fragP->fr_symbol) == segment)                  \
2160      {                                                                  \
2161        /* The symbol lies in the same segment - a relaxable case.  */   \
2162        fragP->fr_subtype                                                \
2163          = ENCODE_RELAX (state, STATE_ZERO);                            \
2164      }                                                                  \
2165    break;
2166
2167   switch (fragP->fr_subtype)
2168     {
2169       HANDLE_RELAXABLE (STATE_GETA);
2170       HANDLE_RELAXABLE (STATE_BCC);
2171       HANDLE_RELAXABLE (STATE_PUSHJ);
2172       HANDLE_RELAXABLE (STATE_JMP);
2173
2174     case ENCODE_RELAX (STATE_GETA, STATE_ZERO):
2175     case ENCODE_RELAX (STATE_BCC, STATE_ZERO):
2176     case ENCODE_RELAX (STATE_PUSHJ, STATE_ZERO):
2177     case ENCODE_RELAX (STATE_JMP, STATE_ZERO):
2178       /* When relaxing a section for the second time, we don't need to do
2179          anything except making sure that fr_var is set right.  */
2180       break;
2181
2182     case STATE_GREG_DEF:
2183       length = fragP->tc_frag_data != NULL ? 0 : 8;
2184       fragP->fr_var = length;
2185
2186       /* Don't consult the relax_table; it isn't valid for this
2187          relaxation.  */
2188       return length;
2189       break;
2190
2191     default:
2192       BAD_CASE (fragP->fr_subtype);
2193     }
2194
2195   length = mmix_relax_table[fragP->fr_subtype].rlx_length;
2196   fragP->fr_var = length;
2197
2198   return length;
2199 }
2200
2201 /* Turn a string in input_line_pointer into a floating point constant of type
2202    type, and store the appropriate bytes in *litP.  The number of LITTLENUMS
2203    emitted is stored in *sizeP .  An error message is returned, or NULL on
2204    OK.  */
2205
2206 char *
2207 md_atof (type, litP, sizeP)
2208      int type;
2209      char *litP;
2210      int *sizeP;
2211 {
2212   int prec;
2213   LITTLENUM_TYPE words[4];
2214   char *t;
2215   int i;
2216
2217   switch (type)
2218     {
2219       /* FIXME: Having 'f' in mmix_flt_chars (and here) makes it
2220          problematic to also have a forward reference in an expression.
2221          The testsuite wants it, and it's customary.
2222          We'll deal with the real problems when they come; we share the
2223          problem with most other ports.  */
2224     case 'f':
2225     case 'r':
2226       prec = 2;
2227       break;
2228     case 'd':
2229       prec = 4;
2230       break;
2231     default:
2232       *sizeP = 0;
2233       return _("bad call to md_atof");
2234     }
2235
2236   t = atof_ieee (input_line_pointer, type, words);
2237   if (t)
2238     input_line_pointer = t;
2239
2240   *sizeP = prec * 2;
2241
2242   for (i = 0; i < prec; i++)
2243     {
2244       md_number_to_chars (litP, (valueT) words[i], 2);
2245           litP += 2;
2246     }
2247   return NULL;
2248 }
2249
2250 /* Convert variable-sized frags into one or more fixups.  */
2251
2252 void
2253 md_convert_frag (abfd, sec, fragP)
2254      bfd *abfd ATTRIBUTE_UNUSED;
2255      segT sec ATTRIBUTE_UNUSED;
2256      fragS *fragP;
2257 {
2258   /* Pointer to first byte in variable-sized part of the frag.  */
2259   char *var_partp;
2260
2261   /* Pointer to first opcode byte in frag.  */
2262   char *opcodep;
2263
2264   /* Size in bytes of variable-sized part of frag.  */
2265   int var_part_size = 0;
2266
2267   /* This is part of *fragP.  It contains all information about addresses
2268      and offsets to varying parts.  */
2269   symbolS *symbolP;
2270   unsigned long var_part_offset;
2271
2272   /* This is the frag for the opcode.  It, rather than fragP, must be used
2273      when emitting a frag for the opcode.  */
2274   fragS *opc_fragP = fragP->tc_frag_data;
2275   fixS *tmpfixP;
2276
2277   /* Where, in file space, does addr point?  */
2278   bfd_vma target_address;
2279   bfd_vma opcode_address;
2280
2281   know (fragP->fr_type == rs_machine_dependent);
2282
2283   var_part_offset = fragP->fr_fix;
2284   var_partp = fragP->fr_literal + var_part_offset;
2285   opcodep = fragP->fr_opcode;
2286
2287   symbolP = fragP->fr_symbol;
2288
2289   target_address
2290     = ((symbolP ? S_GET_VALUE (symbolP) : 0) + fragP->fr_offset);
2291
2292   /* The opcode that would be extended is the last four "fixed" bytes.  */
2293   opcode_address = fragP->fr_address + fragP->fr_fix - 4;
2294
2295   switch (fragP->fr_subtype)
2296   {
2297   case ENCODE_RELAX (STATE_GETA, STATE_ZERO):
2298   case ENCODE_RELAX (STATE_BCC, STATE_ZERO):
2299   case ENCODE_RELAX (STATE_PUSHJ, STATE_ZERO):
2300     mmix_set_geta_branch_offset (opcodep, target_address - opcode_address);
2301     if (linkrelax)
2302       {
2303         tmpfixP
2304           = fix_new (opc_fragP, opcodep - opc_fragP->fr_literal, 4,
2305                      fragP->fr_symbol, fragP->fr_offset, 1,
2306                      BFD_RELOC_MMIX_ADDR19);
2307         COPY_FR_WHERE_TO_FX (fragP, tmpfixP);
2308       }
2309     var_part_size = 0;
2310     break;
2311
2312   case ENCODE_RELAX (STATE_JMP, STATE_ZERO):
2313     mmix_set_jmp_offset (opcodep, target_address - opcode_address);
2314     if (linkrelax)
2315       {
2316         tmpfixP
2317           = fix_new (opc_fragP, opcodep - opc_fragP->fr_literal, 4,
2318                      fragP->fr_symbol, fragP->fr_offset, 1,
2319                      BFD_RELOC_MMIX_ADDR27);
2320         COPY_FR_WHERE_TO_FX (fragP, tmpfixP);
2321       }
2322     var_part_size = 0;
2323     break;
2324
2325   case STATE_GREG_DEF:
2326     if (fragP->tc_frag_data == NULL)
2327       {
2328         tmpfixP
2329           = fix_new (fragP, var_partp - fragP->fr_literal, 8,
2330                      fragP->fr_symbol, fragP->fr_offset, 0, BFD_RELOC_64);
2331         COPY_FR_WHERE_TO_FX (fragP, tmpfixP);
2332         mmix_gregs[n_of_cooked_gregs++] = tmpfixP;
2333         var_part_size = 8;
2334       }
2335     else
2336       var_part_size = 0;
2337     break;
2338
2339 #define HANDLE_MAX_RELOC(state, reloc)                                  \
2340   case ENCODE_RELAX (state, STATE_MAX):                                 \
2341     var_part_size                                                       \
2342       = mmix_relax_table[ENCODE_RELAX (state, STATE_MAX)].rlx_length;   \
2343     mmix_fill_nops (var_partp, var_part_size / 4);                      \
2344     if (warn_on_expansion)                                              \
2345       as_warn_where (fragP->fr_file, fragP->fr_line,                    \
2346                      _("operand out of range, instruction expanded"));  \
2347     tmpfixP = fix_new (fragP, var_partp - fragP->fr_literal - 4, 8,     \
2348                        fragP->fr_symbol, fragP->fr_offset, 1, reloc);   \
2349     COPY_FR_WHERE_TO_FX (fragP, tmpfixP);                               \
2350     break
2351
2352   HANDLE_MAX_RELOC (STATE_GETA, BFD_RELOC_MMIX_GETA);
2353   HANDLE_MAX_RELOC (STATE_BCC, BFD_RELOC_MMIX_CBRANCH);
2354   HANDLE_MAX_RELOC (STATE_PUSHJ, BFD_RELOC_MMIX_PUSHJ);
2355   HANDLE_MAX_RELOC (STATE_JMP, BFD_RELOC_MMIX_JMP);
2356
2357   default:
2358     BAD_CASE (fragP->fr_subtype);
2359     break;
2360   }
2361
2362   fragP->fr_fix += var_part_size;
2363   fragP->fr_var = 0;
2364 }
2365
2366 /* Applies the desired value to the specified location.
2367    Also sets up addends for RELA type relocations.
2368    Stolen from tc-mcore.c.
2369
2370    Note that this function isn't called when linkrelax != 0.  */
2371
2372 int
2373 md_apply_fix3 (fixP, valp, segment)
2374      fixS *   fixP;
2375      valueT * valp;
2376      segT     segment;
2377 {
2378   char *buf  = fixP->fx_where + fixP->fx_frag->fr_literal;
2379   /* Note: use offsetT because it is signed, valueT is unsigned.  */
2380   offsetT val  = (offsetT) * valp;
2381   segT symsec
2382     = (fixP->fx_addsy == NULL
2383        ? absolute_section : S_GET_SEGMENT (fixP->fx_addsy));
2384
2385   /* If the fix is relative to a symbol which is not defined, or, (if
2386      pcrel), not in the same segment as the fix, we cannot resolve it
2387      here.  */
2388   if (fixP->fx_addsy != NULL
2389       && (! S_IS_DEFINED (fixP->fx_addsy)
2390           || S_IS_WEAK (fixP->fx_addsy)
2391           || (fixP->fx_pcrel && symsec != segment)
2392           || (! fixP->fx_pcrel
2393               && symsec != absolute_section
2394               && ((fixP->fx_r_type != BFD_RELOC_MMIX_REG
2395                    && fixP->fx_r_type != BFD_RELOC_MMIX_REG_OR_BYTE)
2396                   || (symsec != reg_section
2397                       && symsec != real_reg_section)))))
2398     {
2399       fixP->fx_done = 0;
2400       return 0;
2401     }
2402   else if (fixP->fx_r_type == BFD_RELOC_MMIX_LOCAL
2403            || fixP->fx_r_type == BFD_RELOC_VTABLE_INHERIT
2404            || fixP->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
2405     {
2406       /* These are never "fixed".  */
2407       fixP->fx_done = 0;
2408       return 0;
2409     }
2410   else
2411     /* We assume every other relocation is "fixed".  */
2412     fixP->fx_done = 1;
2413
2414   switch (fixP->fx_r_type)
2415     {
2416     case BFD_RELOC_64:
2417     case BFD_RELOC_32:
2418     case BFD_RELOC_24:
2419     case BFD_RELOC_16:
2420     case BFD_RELOC_8:
2421     case BFD_RELOC_64_PCREL:
2422     case BFD_RELOC_32_PCREL:
2423     case BFD_RELOC_24_PCREL:
2424     case BFD_RELOC_16_PCREL:
2425     case BFD_RELOC_8_PCREL:
2426       md_number_to_chars (buf, val, fixP->fx_size);
2427       break;
2428
2429     case BFD_RELOC_MMIX_ADDR19:
2430       if (expand_op)
2431         {
2432           /* This shouldn't happen.  */
2433           BAD_CASE (fixP->fx_r_type);
2434           break;
2435         }
2436       /* FALLTHROUGH.  */
2437     case BFD_RELOC_MMIX_GETA:
2438     case BFD_RELOC_MMIX_CBRANCH:
2439     case BFD_RELOC_MMIX_PUSHJ:
2440       /* If this fixup is out of range, punt to the linker to emit an
2441          error.  This should only happen with -no-expand.  */
2442       if (val < -(((offsetT) 1 << 19)/2)
2443           || val >= ((offsetT) 1 << 19)/2 - 1
2444           || (val & 3) != 0)
2445         {
2446           if (warn_on_expansion)
2447             as_warn_where (fixP->fx_file, fixP->fx_line,
2448                            _("operand out of range"));
2449           fixP->fx_done = 0;
2450           val = 0;
2451         }
2452       mmix_set_geta_branch_offset (buf, val);
2453       break;
2454
2455     case BFD_RELOC_MMIX_ADDR27:
2456       if (expand_op)
2457         {
2458           /* This shouldn't happen.  */
2459           BAD_CASE (fixP->fx_r_type);
2460           break;
2461         }
2462       /* FALLTHROUGH.  */
2463     case BFD_RELOC_MMIX_JMP:
2464       /* If this fixup is out of range, punt to the linker to emit an
2465          error.  This should only happen with -no-expand.  */
2466       if (val < -(((offsetT) 1 << 27)/2)
2467           || val >= ((offsetT) 1 << 27)/2 - 1
2468           || (val & 3) != 0)
2469         {
2470           if (warn_on_expansion)
2471             as_warn_where (fixP->fx_file, fixP->fx_line,
2472                            _("operand out of range"));
2473           fixP->fx_done = 0;
2474           val = 0;
2475         }
2476       mmix_set_jmp_offset (buf, val);
2477       break;
2478
2479     case BFD_RELOC_MMIX_REG_OR_BYTE:
2480       if (fixP->fx_addsy != NULL
2481           && (S_GET_SEGMENT (fixP->fx_addsy) != real_reg_section
2482               || S_GET_VALUE (fixP->fx_addsy) > 255)
2483           && S_GET_SEGMENT (fixP->fx_addsy) != absolute_section)
2484         as_bad_where (fixP->fx_file, fixP->fx_line,
2485                       _("invalid operands"));
2486       buf[0] = val;
2487
2488       /* If this reloc is for a Z field, we need to adjust
2489          the opcode if we got a constant here.
2490          FIXME: Can we make this more robust?  */
2491
2492       if ((fixP->fx_where & 3) == 3
2493           && (fixP->fx_addsy == NULL
2494               || S_GET_SEGMENT (fixP->fx_addsy) == absolute_section))
2495         buf[-3] |= IMM_OFFSET_BIT;
2496
2497       /* We don't want this "symbol" appearing in output, because that
2498          will fail.  */
2499       if (fixP->fx_addsy
2500           && S_GET_SEGMENT (fixP->fx_addsy) == real_reg_section)
2501         symbol_clear_used_in_reloc (fixP->fx_addsy);
2502       break;
2503
2504     case BFD_RELOC_MMIX_REG:
2505       if (fixP->fx_addsy == NULL
2506           || S_GET_SEGMENT (fixP->fx_addsy) != real_reg_section
2507           || S_GET_VALUE (fixP->fx_addsy) > 255)
2508         as_bad_where (fixP->fx_file, fixP->fx_line,
2509                       _("invalid operands"));
2510       *buf = val;
2511
2512       if (fixP->fx_addsy
2513           && S_GET_SEGMENT (fixP->fx_addsy) == real_reg_section)
2514         symbol_clear_used_in_reloc (fixP->fx_addsy);
2515       break;
2516
2517     case BFD_RELOC_MMIX_BASE_PLUS_OFFSET:
2518       /* These are never "fixed".  */
2519       fixP->fx_done = 0;
2520       return 0;
2521
2522     case BFD_RELOC_MMIX_PUSHJ_1:
2523     case BFD_RELOC_MMIX_PUSHJ_2:
2524     case BFD_RELOC_MMIX_PUSHJ_3:
2525     case BFD_RELOC_MMIX_CBRANCH_J:
2526     case BFD_RELOC_MMIX_CBRANCH_1:
2527     case BFD_RELOC_MMIX_CBRANCH_2:
2528     case BFD_RELOC_MMIX_CBRANCH_3:
2529     case BFD_RELOC_MMIX_GETA_1:
2530     case BFD_RELOC_MMIX_GETA_2:
2531     case BFD_RELOC_MMIX_GETA_3:
2532     case BFD_RELOC_MMIX_JMP_1:
2533     case BFD_RELOC_MMIX_JMP_2:
2534     case BFD_RELOC_MMIX_JMP_3:
2535     default:
2536       BAD_CASE (fixP->fx_r_type);
2537       break;
2538     }
2539
2540   if (fixP->fx_done)
2541     /* Make sure that for completed fixups we have the value around for
2542        use by e.g. mmix_frob_file.  */
2543     fixP->fx_offset = val;
2544
2545   return 0; /* Return value is ignored.  */
2546 }
2547
2548 /* A bsearch function for looking up a value against offsets for GREG
2549    definitions.  */
2550
2551 static int
2552 cmp_greg_val_greg_symbol_fixes (p1, p2)
2553      const PTR p1;
2554      const PTR p2;
2555 {
2556   offsetT val1 = *(offsetT *) p1;
2557   offsetT val2 = ((struct mmix_symbol_greg_fixes *) p2)->offs;
2558
2559   if (val1 >= val2 && val1 < val2 + 255)
2560     return 0;
2561
2562   if (val1 > val2)
2563     return 1;
2564
2565   return -1;
2566 }
2567
2568 /* Generate a machine-dependent relocation.  */
2569
2570 arelent *
2571 tc_gen_reloc (section, fixP)
2572      asection *section ATTRIBUTE_UNUSED;
2573      fixS *fixP;
2574 {
2575   bfd_signed_vma val
2576     = fixP->fx_offset + (fixP->fx_addsy ? S_GET_VALUE (fixP->fx_addsy) : 0);
2577   arelent *relP;
2578   bfd_reloc_code_real_type code = BFD_RELOC_NONE;
2579   char *buf  = fixP->fx_where + fixP->fx_frag->fr_literal;
2580   symbolS *addsy = fixP->fx_addsy;
2581   asection *addsec = addsy == NULL ? NULL : S_GET_SEGMENT (addsy);
2582   bfd_vma addend = fixP->fx_offset;
2583   asymbol *baddsy = addsy != NULL ? symbol_get_bfdsym (addsy) : NULL;
2584
2585   /* A single " LOCAL expression" in the wrong section will not work when
2586      linking to MMO; relocations for zero-content sections are then
2587      ignored.  Normally, relocations would modify section contents, and
2588      you'd never think or be able to do something like that.  The
2589      relocation resulting from a LOCAL directive doesn't have an obvious
2590      and mandatory location.  I can't figure out a way to do this better
2591      than just helping the user around this limitation here; hopefully the
2592      code using the local expression is around.  Putting the LOCAL
2593      semantics in a relocation still seems right; a section didn't do.  */
2594   if (bfd_section_size (section->owner, section) == 0)
2595     as_bad_where
2596       (fixP->fx_file, fixP->fx_line,
2597        fixP->fx_r_type == BFD_RELOC_MMIX_LOCAL
2598        /* The BFD_RELOC_MMIX_LOCAL-specific message is supposed to be
2599           user-friendly, though a little bit non-substantial.  */
2600        ? _("directive LOCAL must be placed in code or data")
2601        : _("internal confusion: relocation in a section without contents"));
2602
2603   /* FIXME: Range tests for all these.  */
2604   switch (fixP->fx_r_type)
2605     {
2606     case BFD_RELOC_64:
2607     case BFD_RELOC_32:
2608     case BFD_RELOC_24:
2609     case BFD_RELOC_16:
2610     case BFD_RELOC_8:
2611       code = fixP->fx_r_type;
2612
2613       if (addsy == NULL
2614           || bfd_is_abs_section (S_GET_SEGMENT (addsy)))
2615         {
2616           /* Resolve this reloc now, as md_apply_fix3 would have done (not
2617              called if -linkrelax).  There is no point in keeping a reloc
2618              to an absolute symbol.  No reloc that is subject to
2619              relaxation must be to an absolute symbol; difference
2620              involving symbols in a specific section must be signalled as
2621              an error if the relaxing cannot be expressed; having a reloc
2622              to the resolved (now absolute) value does not help.  */
2623           md_number_to_chars (buf, val, fixP->fx_size);
2624           return NULL;
2625         }
2626       break;
2627
2628     case BFD_RELOC_64_PCREL:
2629     case BFD_RELOC_32_PCREL:
2630     case BFD_RELOC_24_PCREL:
2631     case BFD_RELOC_16_PCREL:
2632     case BFD_RELOC_8_PCREL:
2633     case BFD_RELOC_MMIX_LOCAL:
2634     case BFD_RELOC_VTABLE_INHERIT:
2635     case BFD_RELOC_VTABLE_ENTRY:
2636     case BFD_RELOC_MMIX_GETA:
2637     case BFD_RELOC_MMIX_GETA_1:
2638     case BFD_RELOC_MMIX_GETA_2:
2639     case BFD_RELOC_MMIX_GETA_3:
2640     case BFD_RELOC_MMIX_CBRANCH:
2641     case BFD_RELOC_MMIX_CBRANCH_J:
2642     case BFD_RELOC_MMIX_CBRANCH_1:
2643     case BFD_RELOC_MMIX_CBRANCH_2:
2644     case BFD_RELOC_MMIX_CBRANCH_3:
2645     case BFD_RELOC_MMIX_PUSHJ:
2646     case BFD_RELOC_MMIX_PUSHJ_1:
2647     case BFD_RELOC_MMIX_PUSHJ_2:
2648     case BFD_RELOC_MMIX_PUSHJ_3:
2649     case BFD_RELOC_MMIX_JMP:
2650     case BFD_RELOC_MMIX_JMP_1:
2651     case BFD_RELOC_MMIX_JMP_2:
2652     case BFD_RELOC_MMIX_JMP_3:
2653     case BFD_RELOC_MMIX_ADDR19:
2654     case BFD_RELOC_MMIX_ADDR27:
2655       code = fixP->fx_r_type;
2656       break;
2657
2658     case BFD_RELOC_MMIX_REG_OR_BYTE:
2659       /* If we have this kind of relocation to an unknown symbol or to the
2660          register contents section (that is, to a register), then we can't
2661          resolve the relocation here.  */
2662       if (addsy != NULL
2663           && (bfd_is_und_section (S_GET_SEGMENT (addsy))
2664               || strcmp (bfd_get_section_name (addsec->owner, addsec),
2665                          MMIX_REG_CONTENTS_SECTION_NAME) == 0))
2666         {
2667           code = fixP->fx_r_type;
2668           break;
2669         }
2670
2671       /* If the relocation is not to the register section or to the
2672          absolute section (a numeric value), then we have an error.  */
2673       if (addsy != NULL
2674           && (S_GET_SEGMENT (addsy) != real_reg_section
2675               || val > 255
2676               || val < 0)
2677           && ! bfd_is_abs_section (S_GET_SEGMENT (addsy)))
2678         goto badop;
2679
2680       /* Set the "immediate" bit of the insn if this relocation is to Z
2681          field when the value is a numeric value, i.e. not a register.  */
2682       if ((fixP->fx_where & 3) == 3
2683           && (addsy == NULL
2684               || S_GET_SEGMENT (addsy) == absolute_section))
2685         buf[-3] |= IMM_OFFSET_BIT;
2686
2687       buf[0] = val;
2688       return NULL;
2689
2690     case BFD_RELOC_MMIX_BASE_PLUS_OFFSET:
2691       if (addsy != NULL
2692           &&  strcmp (bfd_get_section_name (addsec->owner, addsec),
2693                       MMIX_REG_CONTENTS_SECTION_NAME) == 0)
2694         {
2695           /* This changed into a register; the relocation is for the
2696              register-contents section.  The constant part remains zero.  */
2697           code = BFD_RELOC_MMIX_REG;
2698           break;
2699         }
2700
2701       /* If we've found out that this was indeed a register, then replace
2702          with the register number.  The constant part is already zero.
2703
2704          If we encounter any other defined symbol, then we must find a
2705          suitable register and emit a reloc.  */
2706       if (addsy == NULL
2707           || S_GET_SEGMENT (addsy) != real_reg_section)
2708         {
2709           struct mmix_symbol_gregs *gregs;
2710           struct mmix_symbol_greg_fixes *fix;
2711
2712           if (S_IS_DEFINED (addsy))
2713             {
2714               if (! symbol_section_p (addsy)
2715                   && ! bfd_is_abs_section (S_GET_SEGMENT (addsy)))
2716                 as_fatal (_("internal: BFD_RELOC_MMIX_BASE_PLUS_OFFSET not resolved to section"));
2717
2718               /* If this is an absolute symbol sufficiently near
2719                  lowest_data_loc, then we canonicalize on the data
2720                  section.  Note that val is signed here; we may subtract
2721                  lowest_data_loc which is unsigned.  Careful with those
2722                  comparisons.  */
2723               if (lowest_data_loc != (bfd_vma) -1
2724                   && (bfd_vma) val + 256 > lowest_data_loc
2725                   && bfd_is_abs_section (S_GET_SEGMENT (addsy)))
2726                 {
2727                   val -= (offsetT) lowest_data_loc;
2728                   addsy = section_symbol (data_section);
2729                 }
2730               /* Likewise text section.  */
2731               else if (lowest_text_loc != (bfd_vma) -1
2732                        && (bfd_vma) val + 256 > lowest_text_loc
2733                        && bfd_is_abs_section (S_GET_SEGMENT (addsy)))
2734                 {
2735                   val -= (offsetT) lowest_text_loc;
2736                   addsy = section_symbol (text_section);
2737                 }
2738             }
2739
2740           gregs = *symbol_get_tc (addsy);
2741
2742           /* If that symbol does not have any associated GREG definitions,
2743              we can't do anything.  FIXME: implement allocate-on-demand in
2744              the linker.  */
2745           if (gregs == NULL
2746               || (fix = bsearch (&val, gregs->greg_fixes, gregs->n_gregs,
2747                                  sizeof (gregs->greg_fixes[0]),
2748                                  cmp_greg_val_greg_symbol_fixes)) == NULL
2749               /* The register must not point *after* the address we want.  */
2750               || fix->offs > val
2751               /* Neither must the register point more than 255 bytes
2752                  before the address we want.  */
2753               || fix->offs + 255 < val)
2754             {
2755               as_bad_where (fixP->fx_file, fixP->fx_line,
2756                             _("no suitable GREG definition for operands"));
2757               return NULL;
2758             }
2759           else
2760             {
2761               /* Transform the base-plus-offset reloc for the actual area
2762                  to a reloc for the register with the address of the area.
2763                  Put addend for register in Z operand.  */
2764               buf[1] = val - fix->offs;
2765               code = BFD_RELOC_MMIX_REG;
2766               baddsy
2767                 = (bfd_get_section_by_name (stdoutput,
2768                                             MMIX_REG_CONTENTS_SECTION_NAME)
2769                    ->symbol);
2770
2771               addend = fix->fix->fx_frag->fr_address + fix->fix->fx_where;
2772             }
2773         }
2774       else if (S_GET_VALUE (addsy) > 255)
2775         as_bad_where (fixP->fx_file, fixP->fx_line,
2776                       _("invalid operands"));
2777       else
2778         {
2779           *buf = val;
2780           return NULL;
2781         }
2782       break;
2783
2784     case BFD_RELOC_MMIX_REG:
2785       if (addsy != NULL
2786           && (bfd_is_und_section (S_GET_SEGMENT (addsy))
2787               || strcmp (bfd_get_section_name (addsec->owner, addsec),
2788                          MMIX_REG_CONTENTS_SECTION_NAME) == 0))
2789         {
2790           code = fixP->fx_r_type;
2791           break;
2792         }
2793
2794       if (addsy != NULL
2795           && (S_GET_SEGMENT (addsy) != real_reg_section
2796               || val > 255
2797               || val < 0)
2798           && ! bfd_is_und_section (S_GET_SEGMENT (addsy)))
2799         /* Drop through to error message.  */
2800         ;
2801       else
2802         {
2803           buf[0] = val;
2804           return NULL;
2805         }
2806       /* FALLTHROUGH. */
2807
2808       /* The others are supposed to be handled by md_apply_fix3.
2809          FIXME: ... which isn't called when -linkrelax.  Move over
2810          md_apply_fix3 code here for everything reasonable.  */
2811     badop:
2812     default:
2813       as_bad_where
2814         (fixP->fx_file, fixP->fx_line,
2815          _("operands were not reducible at assembly-time"));
2816
2817       /* Unmark this symbol as used in a reloc, so we don't bump into a BFD
2818          assert when trying to output reg_section.  FIXME: A gas bug.  */
2819       if (addsy)
2820         symbol_clear_used_in_reloc (addsy);
2821       return NULL;
2822     }
2823
2824   relP = (arelent *) xmalloc (sizeof (arelent));
2825   assert (relP != 0);
2826   relP->sym_ptr_ptr = (asymbol **) xmalloc (sizeof (asymbol *));
2827   *relP->sym_ptr_ptr = baddsy;
2828   relP->address = fixP->fx_frag->fr_address + fixP->fx_where;
2829
2830   relP->addend = addend;
2831
2832   /* If this had been a.out, we would have had a kludge for weak symbols
2833      here.  */
2834
2835   relP->howto = bfd_reloc_type_lookup (stdoutput, code);
2836   if (! relP->howto)
2837     {
2838       const char *name;
2839
2840       name = S_GET_NAME (addsy);
2841       if (name == NULL)
2842         name = _("<unknown>");
2843       as_fatal (_("cannot generate relocation type for symbol %s, code %s"),
2844                 name, bfd_get_reloc_code_name (code));
2845     }
2846
2847   return relP;
2848 }
2849
2850 /* Do some reformatting of a line.  FIXME: We could transform a mmixal
2851    line into traditional (GNU?) format, unless #NO_APP, and get rid of all
2852    ugly labels_without_colons etc.  */
2853
2854 void
2855 mmix_handle_mmixal ()
2856 {
2857   char *s0 = input_line_pointer;
2858   char *s;
2859   char *label = NULL;
2860   char c;
2861
2862   if (pending_label != NULL)
2863     as_fatal (_("internal: unhandled label %s"), pending_label);
2864
2865   if (mmix_gnu_syntax)
2866     return;
2867
2868   /* If the first character is a '.', then it's a pseudodirective, not a
2869      label.  Make GAS not handle label-without-colon on this line.  We
2870      also don't do mmixal-specific stuff on this line.  */
2871   if (input_line_pointer[0] == '.')
2872     {
2873       label_without_colon_this_line = 0;
2874       return;
2875     }
2876
2877   /* Don't handle empty lines here.  */
2878   while (1)
2879     {
2880       if (*s0 == 0 || is_end_of_line [(unsigned int) *s0])
2881         return;
2882
2883       if (! ISSPACE (*s0))
2884         break;
2885
2886       s0++;
2887     }
2888
2889   /* If we're on a line with a label, check if it's a mmixal fb-label.
2890      Save an indicator and skip the label; it must be set only after all
2891      fb-labels of expressions are evaluated.  */
2892   if (ISDIGIT (input_line_pointer[0])
2893       && input_line_pointer[1] == 'H'
2894       && ISSPACE (input_line_pointer[2]))
2895     {
2896       char *s;
2897       current_fb_label = input_line_pointer[0] - '0';
2898
2899       /* We have to skip the label, but also preserve the newlineness of
2900          the previous character, since the caller checks that.  It's a
2901          mess we blame on the caller.  */
2902       input_line_pointer[1] = input_line_pointer[-1];
2903       input_line_pointer += 2;
2904
2905       s = input_line_pointer;
2906       while (*s && ISSPACE (*s) && ! is_end_of_line[(unsigned int) *s])
2907         s++;
2908
2909       /* For errors emitted here, the book-keeping is off by one; the
2910          caller is about to bump the counters.  Adjust the error messages.  */
2911       if (is_end_of_line [(unsigned int) *s])
2912         {
2913           char *name;
2914           unsigned int line;
2915           as_where (&name, &line);
2916           as_bad_where (name, line + 1,
2917                         _("[0-9]H labels may not appear alone on a line"));
2918           current_fb_label = -1;
2919         }
2920       if (*s == '.')
2921         {
2922           char *name;
2923           unsigned int line;
2924           as_where (&name, &line);
2925           as_bad_where (name, line + 1,
2926                         _("[0-9]H labels do not mix with dot-pseudos"));
2927           current_fb_label = -1;
2928         }
2929     }
2930   else
2931     {
2932       current_fb_label = -1;
2933       if (is_name_beginner (input_line_pointer[0]))
2934         label = input_line_pointer;
2935     }
2936
2937   s0 = input_line_pointer;
2938   /* Skip over label. */
2939   while (*s0 && is_part_of_name (*s0))
2940     s0++;
2941
2942   /* Remove trailing ":" off labels, as they'd otherwise be considered
2943      part of the name.  But don't do it for local labels.  */
2944   if (s0 != input_line_pointer && s0[-1] == ':'
2945       && (s0 - 2 != input_line_pointer
2946           || ! ISDIGIT (s0[-2])))
2947     s0[-1] = ' ';
2948   else if (label != NULL)
2949     {
2950       /* For labels that don't end in ":", we save it so we can later give
2951          it the same alignment and address as the associated instruction.  */
2952
2953       /* Make room for the label including the ending nul.  */
2954       int len_0 = s0 - label + 1;
2955
2956       /* Save this label on the MMIX symbol obstack.  Saving it on an
2957          obstack is needless for "IS"-pseudos, but it's harmless and we
2958          avoid a little code-cluttering.  */
2959       obstack_grow (&mmix_sym_obstack, label, len_0);
2960       pending_label = obstack_finish (&mmix_sym_obstack);
2961       pending_label[len_0 - 1] = 0;
2962     }
2963
2964   while (*s0 && ISSPACE (*s0) && ! is_end_of_line [(unsigned int) *s0])
2965     s0++;
2966
2967   if (pending_label != NULL && is_end_of_line [(unsigned int) *s0])
2968     /* Whoops, this was actually a lone label on a line.  Like :-ended
2969        labels, we don't attach such labels to the next instruction or
2970        pseudo.  */
2971     pending_label = NULL;
2972
2973   /* Find local labels of operands.  Look for "[0-9][FB]" where the
2974      characters before and after are not part of words.  Break if a single
2975      or double quote is seen anywhere.  It means we can't have local
2976      labels as part of list with mixed quoted and unquoted members for
2977      mmixal compatibility but we can't have it all.  For the moment.
2978      Replace the '<N>B' or '<N>F' with MAGIC_FB_BACKWARD_CHAR<N> and
2979      MAGIC_FB_FORWARD_CHAR<N> respectively.  */
2980
2981   /* First make sure we don't have any of the magic characters on the line
2982      appearing as input.  */
2983   s = s0;
2984   while (*s)
2985     {
2986       c = *s++;
2987       if (is_end_of_line [(unsigned int) c])
2988         break;
2989       if (c == MAGIC_FB_BACKWARD_CHAR || c == MAGIC_FB_FORWARD_CHAR)
2990         as_bad (_("invalid characters in input"));
2991     }
2992
2993   /* Scan again, this time looking for ';' after operands.  */
2994   s = s0;
2995
2996   /* Skip the insn.  */
2997   while (*s
2998          && ! ISSPACE (*s)
2999          && *s != ';'
3000          && ! is_end_of_line[(unsigned int) *s])
3001     s++;
3002
3003   /* Skip the spaces after the insn.  */
3004   while (*s
3005          && ISSPACE (*s)
3006          && *s != ';'
3007          && ! is_end_of_line[(unsigned int) *s])
3008     s++;
3009
3010   /* Skip the operands.  While doing this, replace [0-9][BF] with
3011      (MAGIC_FB_BACKWARD_CHAR|MAGIC_FB_FORWARD_CHAR)[0-9].  */
3012   while ((c = *s) != 0
3013          && ! ISSPACE (c)
3014          && c != ';'
3015          && ! is_end_of_line[(unsigned int) c])
3016     {
3017       if (c == '"')
3018         {
3019           s++;
3020
3021           /* FIXME: Test-case for semi-colon in string.  */
3022           while (*s
3023                  && *s != '"'
3024                  && (! is_end_of_line [(unsigned int) *s] || *s == ';'))
3025             s++;
3026
3027           if (*s == '"')
3028             s++;
3029         }
3030       else if (ISDIGIT (c))
3031         {
3032           if ((s[1] != 'B' && s[1] != 'F')
3033               || is_part_of_name (s[-1])
3034               || is_part_of_name (s[2]))
3035             s++;
3036           else
3037             {
3038               s[0] = (s[1] == 'B'
3039                       ? MAGIC_FB_BACKWARD_CHAR : MAGIC_FB_FORWARD_CHAR);
3040               s[1] = c;
3041             }
3042         }
3043       else
3044         s++;
3045     }
3046
3047   /* Skip any spaces after the operands.  */
3048   while (*s
3049          && ISSPACE (*s)
3050          && *s != ';'
3051          && !is_end_of_line[(unsigned int) *s])
3052     s++;
3053
3054   /* If we're now looking at a semi-colon, then it's an end-of-line
3055      delimiter.  */
3056   mmix_next_semicolon_is_eoln = (*s == ';');
3057
3058   /* Make IS into an EQU by replacing it with "= ".  Only match upper-case
3059      though; let lower-case be a syntax error.  */
3060   s = s0;
3061   if (s[0] == 'I' && s[1] == 'S' && ISSPACE (s[2]))
3062     {
3063       *s = '=';
3064       s[1] = ' ';
3065
3066       /* Since labels can start without ":", we have to handle "X IS 42"
3067          in full here, or "X" will be parsed as a label to be set at ".".  */
3068       input_line_pointer = s;
3069
3070       /* Right after this function ends, line numbers will be bumped if
3071          input_line_pointer[-1] = '\n'.  We want accurate line numbers for
3072          the equals call, so we bump them before the call, and make sure
3073          they aren't bumped afterwards.  */
3074       bump_line_counters ();
3075
3076       /* A fb-label is valid as an IS-label.  */
3077       if (current_fb_label >= 0)
3078         {
3079           char *fb_name;
3080
3081           /* We need to save this name on our symbol obstack, since the
3082              string we got in fb_label_name is volatile and will change
3083              with every call to fb_label_name, like those resulting from
3084              parsing the IS-operand.  */
3085           fb_name = fb_label_name (current_fb_label, 1);
3086           obstack_grow (&mmix_sym_obstack, fb_name, strlen (fb_name) + 1);
3087           equals (obstack_finish (&mmix_sym_obstack), 0);
3088           fb_label_instance_inc (current_fb_label);
3089           current_fb_label = -1;
3090         }
3091       else
3092         {
3093           if (pending_label == NULL)
3094             as_bad (_("empty label field for IS"));
3095           else
3096             equals (pending_label, 0);
3097           pending_label = NULL;
3098         }
3099
3100       /* For mmixal, we can have comments without a comment-start
3101          character.   */
3102       mmix_handle_rest_of_empty_line ();
3103       input_line_pointer--;
3104
3105       input_line_pointer[-1] = ' ';
3106     }
3107   else if (s[0] == 'G'
3108            && s[1] == 'R'
3109            && strncmp (s, "GREG", 4) == 0
3110            && (ISSPACE (s[4]) || is_end_of_line[(unsigned char) s[4]]))
3111     {
3112       input_line_pointer = s + 4;
3113
3114       /* Right after this function ends, line numbers will be bumped if
3115          input_line_pointer[-1] = '\n'.  We want accurate line numbers for
3116          the s_greg call, so we bump them before the call, and make sure
3117          they aren't bumped afterwards.  */
3118       bump_line_counters ();
3119
3120       /* A fb-label is valid as a GREG-label.  */
3121       if (current_fb_label >= 0)
3122         {
3123           char *fb_name;
3124
3125           /* We need to save this name on our symbol obstack, since the
3126              string we got in fb_label_name is volatile and will change
3127              with every call to fb_label_name, like those resulting from
3128              parsing the IS-operand.  */
3129           fb_name = fb_label_name (current_fb_label, 1);
3130
3131           /* Make sure we save the canonical name and don't get bitten by
3132              prefixes.  */
3133           obstack_1grow (&mmix_sym_obstack, ':');
3134           obstack_grow (&mmix_sym_obstack, fb_name, strlen (fb_name) + 1);
3135           mmix_greg_internal (obstack_finish (&mmix_sym_obstack));
3136           fb_label_instance_inc (current_fb_label);
3137           current_fb_label = -1;
3138         }
3139       else
3140         mmix_greg_internal (pending_label);
3141
3142       /* Back up before the end-of-line marker that was skipped in
3143          mmix_greg_internal.  */
3144       input_line_pointer--;
3145       input_line_pointer[-1] = ' ';
3146
3147       pending_label = NULL;
3148     }
3149   else if (pending_label != NULL)
3150     {
3151       input_line_pointer += strlen (pending_label);
3152
3153       /* See comment above about getting line numbers bumped.  */
3154       input_line_pointer[-1] = '\n';
3155     }
3156 }
3157
3158 /* Give the value of an fb-label rewritten as in mmix_handle_mmixal, when
3159    parsing an expression.
3160
3161    On valid calls, input_line_pointer points at a MAGIC_FB_BACKWARD_CHAR
3162    or MAGIC_FB_BACKWARD_CHAR, followed by an ascii digit for the label.
3163    We fill in the label as an expression.  */
3164
3165 void
3166 mmix_fb_label (expP)
3167      expressionS *expP;
3168 {
3169   symbolS *sym;
3170   char *fb_internal_name;
3171
3172   /* This doesn't happen when not using mmixal syntax.  */
3173   if (mmix_gnu_syntax
3174       || (input_line_pointer[0] != MAGIC_FB_BACKWARD_CHAR
3175           && input_line_pointer[0] != MAGIC_FB_FORWARD_CHAR))
3176     return;
3177
3178   /* The current backward reference has augmentation 0.  A forward
3179      reference has augmentation 1, unless it's the same as a fb-label on
3180      _this_ line, in which case we add one more so we don't refer to it.
3181      This is the semantics of mmixal; it differs to that of common
3182      fb-labels which refer to a here-label on the current line as a
3183      backward reference.  */
3184   fb_internal_name
3185     = fb_label_name (input_line_pointer[1] - '0',
3186                      (input_line_pointer[0] == MAGIC_FB_FORWARD_CHAR ? 1 : 0)
3187                      + ((input_line_pointer[1] - '0' == current_fb_label
3188                          && input_line_pointer[0] == MAGIC_FB_FORWARD_CHAR)
3189                         ? 1 : 0));
3190
3191   input_line_pointer += 2;
3192   sym = symbol_find_or_make (fb_internal_name);
3193
3194   /* We don't have to clean up unrelated fields here; we just do what the
3195      expr machinery does, but *not* just what it does for [0-9][fb], since
3196      we need to treat those as ordinary symbols sometimes; see testcases
3197      err-byte2.s and fb-2.s.  */
3198   if (S_GET_SEGMENT (sym) == absolute_section)
3199     {
3200       expP->X_op = O_constant;
3201       expP->X_add_number = S_GET_VALUE (sym);
3202     }
3203   else
3204     {
3205       expP->X_op = O_symbol;
3206       expP->X_add_symbol = sym;
3207       expP->X_add_number = 0;
3208     }
3209 }
3210
3211 /* See whether we need to force a relocation into the output file.
3212    This is used to force out switch and PC relative relocations when
3213    relaxing.  */
3214
3215 int
3216 mmix_force_relocation (fixP)
3217      fixS * fixP;
3218 {
3219   if (fixP->fx_r_type == BFD_RELOC_MMIX_LOCAL
3220       || fixP->fx_r_type == BFD_RELOC_VTABLE_INHERIT
3221       || fixP->fx_r_type == BFD_RELOC_VTABLE_ENTRY
3222       || fixP->fx_r_type == BFD_RELOC_MMIX_BASE_PLUS_OFFSET)
3223     return 1;
3224
3225   /* FIXME: This is dubious.  Handling of weak symbols should have been
3226      caught before we get here.  */
3227   if ((fixP->fx_addsy && S_IS_WEAK (fixP->fx_addsy)))
3228     return 1;
3229
3230   if (linkrelax)
3231     return 1;
3232
3233   /* All our pcrel relocations are must-keep.  Note that md_apply_fix3 is
3234      called *after* this, and will handle getting rid of the presumed
3235      reloc; a relocation isn't *forced* other than to be handled by
3236      md_apply_fix3 (or tc_gen_reloc if linkrelax).  */
3237   if (fixP->fx_pcrel)
3238     return 1;
3239
3240   return 0;
3241 }
3242
3243 /* The location from which a PC relative jump should be calculated,
3244    given a PC relative reloc.  */
3245
3246 long
3247 md_pcrel_from_section (fixP, sec)
3248      fixS * fixP;
3249      segT   sec;
3250 {
3251   if (fixP->fx_addsy != (symbolS *) NULL
3252       && (! S_IS_DEFINED (fixP->fx_addsy)
3253           || S_GET_SEGMENT (fixP->fx_addsy) != sec))
3254     {
3255       /* The symbol is undefined (or is defined but not in this section).
3256          Let the linker figure it out.  */
3257       return 0;
3258     }
3259
3260   return (fixP->fx_frag->fr_address + fixP->fx_where);
3261 }
3262
3263 /* Adjust the symbol table.  We make reg_section relative to the real
3264    register section.
3265
3266    FIXME: There's a gas bug; should be fixed when the reg_section symbol
3267    is "accidentally" saved for relocs which are really fixups that will be
3268    fixed up.  */
3269
3270 void
3271 mmix_adjust_symtab ()
3272 {
3273   symbolS *sym;
3274   symbolS *prevsym;
3275   symbolS *regsec = section_symbol (reg_section);
3276   segT realregsec = NULL;
3277
3278   for (prevsym = sym = symbol_rootP;
3279        sym != NULL;
3280        prevsym = sym, sym = symbol_next (sym))
3281     if (S_GET_SEGMENT (sym) == reg_section)
3282       {
3283         if (sym == regsec
3284             || (!S_IS_EXTERN (sym) && !symbol_used_in_reloc_p (sym)))
3285           {
3286             symbol_remove (sym, &symbol_rootP, &symbol_lastP);
3287
3288             /* We make one extra turn, or we'll lose the next symbol.  We
3289                assume that the symbol we remove is not the symbol root
3290                (.text normally is).  */
3291             sym = prevsym;
3292           }
3293         else
3294           {
3295             /* Change section to the *real* register section, so it gets
3296                proper treatment when writing it out.  Only do this for
3297                global symbols.  This also means we don't have to check for
3298                $0..$255.  */
3299             if (realregsec == NULL)
3300               realregsec
3301                 = bfd_make_section_old_way (stdoutput, MMIX_REG_SECTION_NAME);
3302
3303             S_SET_SEGMENT (sym, realregsec);
3304           }
3305       }
3306 }
3307
3308 /* This is the expansion of LABELS_WITHOUT_COLONS.
3309    We let md_start_line_hook tweak label_without_colon_this_line, and then
3310    this function returns the tweaked value, and sets it to 1 for the next
3311    line.  FIXME: Very, very brittle.  Not sure it works the way I
3312    thought at the time I first wrote this.  */
3313
3314 int
3315 mmix_label_without_colon_this_line ()
3316 {
3317   int retval = label_without_colon_this_line;
3318
3319   if (! mmix_gnu_syntax)
3320     label_without_colon_this_line = 1;
3321
3322   return retval;
3323 }
3324
3325 /* This is the expansion of md_relax_frag.  We go through the ordinary
3326    relax table function except when the frag is for a GREG.  Then we have
3327    to check whether there's another GREG by the same value that we can
3328    join with.  */
3329
3330 long
3331 mmix_md_relax_frag (seg, fragP, stretch)
3332      segT seg;
3333      fragS *fragP;
3334      long stretch;
3335 {
3336   if (fragP->fr_subtype != STATE_GREG_DEF
3337       && fragP->fr_subtype != STATE_GREG_UNDF)
3338     return relax_frag (seg, fragP, stretch);
3339
3340   /* If we're defined, we don't grow.  */
3341   if (fragP->fr_subtype == STATE_GREG_DEF)
3342     return 0;
3343
3344   as_fatal (_("internal: unexpected relax type %d:%d"),
3345             fragP->fr_type, fragP->fr_subtype);
3346   return 0;
3347 }
3348
3349 /* Various things we punt until all input is seen.  */
3350
3351 void
3352 mmix_md_end ()
3353 {
3354   fragS *fragP;
3355   symbolS *mainsym;
3356   int i;
3357
3358   /* The first frag of GREG:s going into the register contents section.  */
3359   fragS *mmix_reg_contents_frags = NULL;
3360
3361   /* Reset prefix.  All labels reachable at this point must be
3362      canonicalized.  */
3363   mmix_current_prefix = NULL;
3364
3365   if (doing_bspec)
3366     as_bad_where (bspec_file, bspec_line, _("BSPEC without ESPEC."));
3367
3368   /* Emit the low LOC setting of .text.  */
3369   if (text_has_contents && lowest_text_loc != (bfd_vma) -1)
3370     {
3371       symbolS *symbolP;
3372       char locsymbol[sizeof (":") - 1
3373                     + sizeof (MMIX_LOC_SECTION_START_SYMBOL_PREFIX) - 1
3374                     + sizeof (".text")];
3375
3376       /* An exercise in non-ISO-C-ness, this one.  */
3377       sprintf (locsymbol, ":%s%s", MMIX_LOC_SECTION_START_SYMBOL_PREFIX,
3378                ".text");
3379       symbolP
3380         = symbol_new (locsymbol, absolute_section, lowest_text_loc,
3381                       &zero_address_frag);
3382       S_SET_EXTERNAL (symbolP);
3383     }
3384
3385   /* Ditto .data.  */
3386   if (data_has_contents && lowest_data_loc != (bfd_vma) -1)
3387     {
3388       symbolS *symbolP;
3389       char locsymbol[sizeof (":") - 1
3390                     + sizeof (MMIX_LOC_SECTION_START_SYMBOL_PREFIX) - 1
3391                     + sizeof (".data")];
3392
3393       sprintf (locsymbol, ":%s%s", MMIX_LOC_SECTION_START_SYMBOL_PREFIX,
3394                ".data");
3395       symbolP
3396         = symbol_new (locsymbol, absolute_section, lowest_data_loc,
3397                       &zero_address_frag);
3398       S_SET_EXTERNAL (symbolP);
3399     }
3400
3401   /* Unless GNU syntax mode, set "Main" to be a function, so the
3402      disassembler doesn't get confused when we write truly
3403      mmixal-compatible code (and don't use .type).  Similarly set it
3404      global (regardless of -globalize-symbols), so the linker sees it as
3405      the start symbol in ELF mode.  */
3406   mainsym = symbol_find (MMIX_START_SYMBOL_NAME);
3407   if (mainsym != NULL && ! mmix_gnu_syntax)
3408     {
3409       symbol_get_bfdsym (mainsym)->flags |= BSF_FUNCTION;
3410       S_SET_EXTERNAL (mainsym);
3411     }
3412
3413   if (n_of_raw_gregs != 0)
3414     {
3415       /* Emit GREGs.  They are collected in order of appearance, but must
3416          be emitted in opposite order to both have section address regno*8
3417          and the same allocation order (within a file) as mmixal.  */
3418       segT this_segment = now_seg;
3419       subsegT this_subsegment = now_subseg;
3420       asection *regsec
3421         = bfd_make_section_old_way (stdoutput,
3422                                     MMIX_REG_CONTENTS_SECTION_NAME);
3423       subseg_set (regsec, 0);
3424
3425       /* Finally emit the initialization-value.  Emit a variable frag, which
3426          we'll fix in md_estimate_size_before_relax.  We set the initializer
3427          for the tc_frag_data field to NULL, so we can use that field for
3428          relaxation purposes.  */
3429       mmix_opcode_frag = NULL;
3430
3431       frag_grow (0);
3432       mmix_reg_contents_frags = frag_now;
3433
3434       for (i = n_of_raw_gregs - 1; i >= 0; i--)
3435         {
3436           if (mmix_raw_gregs[i].label != NULL)
3437             /* There's a symbol.  Let it refer to this location in the
3438                register contents section.  The symbol must be globalized
3439                separately.  */
3440             colon (mmix_raw_gregs[i].label);
3441
3442           frag_var (rs_machine_dependent, 8, 0, STATE_GREG_UNDF,
3443                     make_expr_symbol (&mmix_raw_gregs[i].exp), 0, NULL);
3444         }
3445
3446       subseg_set (this_segment, this_subsegment);
3447     }
3448
3449   /* Iterate over frags resulting from GREGs and move those that evidently
3450      have the same value together and point one to another.
3451
3452      This works in time O(N^2) but since the upper bound for non-error use
3453      is 223, it's best to keep this simpler algorithm.  */
3454   for (fragP = mmix_reg_contents_frags; fragP != NULL; fragP = fragP->fr_next)
3455     {
3456       fragS **fpp;
3457       fragS *fp = NULL;
3458       fragS *osymfrag;
3459       offsetT osymval;
3460       expressionS *oexpP;
3461       symbolS *symbolP = fragP->fr_symbol;
3462
3463       if (fragP->fr_type != rs_machine_dependent
3464           || fragP->fr_subtype != STATE_GREG_UNDF)
3465         continue;
3466
3467       /* Whatever the outcome, we will have this GREG judged merged or
3468          non-merged.  Since the tc_frag_data is NULL at this point, we
3469          default to non-merged.  */
3470       fragP->fr_subtype = STATE_GREG_DEF;
3471
3472       /* If we're not supposed to merge GREG definitions, then just don't
3473          look for equivalents.  */
3474       if (! merge_gregs)
3475         continue;
3476
3477       osymval = (offsetT) S_GET_VALUE (symbolP);
3478       osymfrag = symbol_get_frag (symbolP);
3479
3480       /* If the symbol isn't defined, we can't say that another symbol
3481          equals this frag, then.  FIXME: We can look at the "deepest"
3482          defined name; if a = c and b = c then obviously a == b.  */
3483       if (! S_IS_DEFINED (symbolP))
3484         continue;
3485
3486       oexpP = symbol_get_value_expression (fragP->fr_symbol);
3487
3488       /* If the initialization value is zero, then we must not merge them.  */
3489       if (oexpP->X_op == O_constant && osymval == 0)
3490         continue;
3491
3492       /* Iterate through the frags downward this one.  If we find one that
3493          has the same non-zero value, move it to after this one and point
3494          to it as the equivalent.  */
3495       for (fpp = &fragP->fr_next; *fpp != NULL; fpp = &fpp[0]->fr_next)
3496         {
3497           fp = *fpp;
3498
3499           if (fp->fr_type != rs_machine_dependent
3500               || fp->fr_subtype != STATE_GREG_UNDF)
3501             continue;
3502
3503           /* Calling S_GET_VALUE may simplify the symbol, changing from
3504              expr_section etc. so call it first.  */
3505           if ((offsetT) S_GET_VALUE (fp->fr_symbol) == osymval
3506               && symbol_get_frag (fp->fr_symbol) == osymfrag)
3507             {
3508               /* Move the frag links so the one we found equivalent comes
3509                  after the current one, carefully considering that
3510                  sometimes fpp == &fragP->fr_next and the moves must be a
3511                  NOP then.  */
3512               *fpp = fp->fr_next;
3513               fp->fr_next = fragP->fr_next;
3514               fragP->fr_next = fp;
3515               break;
3516             }
3517         }
3518
3519       if (*fpp != NULL)
3520         fragP->tc_frag_data = fp;
3521     }
3522 }
3523
3524 /* qsort function for mmix_symbol_gregs.  */
3525
3526 static int
3527 cmp_greg_symbol_fixes (parg, qarg)
3528      const PTR parg;
3529      const PTR qarg;
3530 {
3531   const struct mmix_symbol_greg_fixes *p
3532     = (const struct mmix_symbol_greg_fixes *) parg;
3533   const struct mmix_symbol_greg_fixes *q
3534     = (const struct mmix_symbol_greg_fixes *) qarg;
3535
3536   return p->offs > q->offs ? 1 : p->offs < q->offs ? -1 : 0;
3537 }
3538
3539 /* Collect GREG definitions from mmix_gregs and hang them as lists sorted
3540    on increasing offsets onto each section symbol or undefined symbol.
3541
3542    Also, remove the register convenience section so it doesn't get output
3543    as an ELF section.  */
3544
3545 void
3546 mmix_frob_file ()
3547 {
3548   int i;
3549   struct mmix_symbol_gregs *all_greg_symbols[MAX_GREGS];
3550   int n_greg_symbols = 0;
3551
3552   /* Collect all greg fixups and decorate each corresponding symbol with
3553      the greg fixups for it.  */
3554   for (i = 0; i < n_of_cooked_gregs; i++)
3555     {
3556       offsetT offs;
3557       symbolS *sym;
3558       struct mmix_symbol_gregs *gregs;
3559       fixS *fixP;
3560
3561       fixP = mmix_gregs[i];
3562       know (fixP->fx_r_type == BFD_RELOC_64);
3563
3564       /* This case isn't doable in general anyway, methinks.  */
3565       if (fixP->fx_subsy != NULL)
3566         {
3567           as_bad_where (fixP->fx_file, fixP->fx_line,
3568                         _("GREG expression too complicated"));
3569           continue;
3570         }
3571
3572       sym = fixP->fx_addsy;
3573       offs = (offsetT) fixP->fx_offset;
3574
3575       /* If the symbol is defined, then it must be resolved to a section
3576          symbol at this time, or else we don't know how to handle it.  */
3577       if (S_IS_DEFINED (sym))
3578         {
3579           if (! symbol_section_p (sym)
3580               && ! bfd_is_abs_section (S_GET_SEGMENT (sym)))
3581             as_fatal (_("internal: GREG expression not resolved to section"));
3582
3583           offs += S_GET_VALUE (sym);
3584         }
3585
3586       /* If this is an absolute symbol sufficiently near lowest_data_loc,
3587          then we canonicalize on the data section.  Note that offs is
3588          signed here; we may subtract lowest_data_loc which is unsigned.
3589          Careful with those comparisons.  */
3590       if (lowest_data_loc != (bfd_vma) -1
3591           && (bfd_vma) offs + 256 > lowest_data_loc
3592           && bfd_is_abs_section (S_GET_SEGMENT (sym)))
3593         {
3594           offs -= (offsetT) lowest_data_loc;
3595           sym = section_symbol (data_section);
3596         }
3597       /* Likewise text section.  */
3598       else if (lowest_text_loc != (bfd_vma) -1
3599                && (bfd_vma) offs + 256 > lowest_text_loc
3600                && bfd_is_abs_section (S_GET_SEGMENT (sym)))
3601         {
3602           offs -= (offsetT) lowest_text_loc;
3603           sym = section_symbol (text_section);
3604         }
3605
3606       gregs = *symbol_get_tc (sym);
3607
3608       if (gregs == NULL)
3609         {
3610           gregs = xmalloc (sizeof (*gregs));
3611           gregs->n_gregs = 0;
3612           symbol_set_tc (sym, &gregs);
3613           all_greg_symbols[n_greg_symbols++] = gregs;
3614         }
3615
3616       gregs->greg_fixes[gregs->n_gregs].fix = fixP;
3617       gregs->greg_fixes[gregs->n_gregs++].offs = offs;
3618     }
3619
3620   /* For each symbol having a GREG definition, sort those definitions on
3621      offset.  */
3622   for (i = 0; i < n_greg_symbols; i++)
3623     qsort (all_greg_symbols[i]->greg_fixes, all_greg_symbols[i]->n_gregs,
3624            sizeof (all_greg_symbols[i]->greg_fixes[0]), cmp_greg_symbol_fixes);
3625
3626   if (real_reg_section != NULL)
3627     {
3628       asection **secpp;
3629
3630       /* FIXME: Pass error state gracefully.  */
3631       if (bfd_get_section_flags (stdoutput, real_reg_section) & SEC_HAS_CONTENTS)
3632         as_fatal (_("register section has contents\n"));
3633
3634       /* FIXME: This does not seem like the proper way to kill a section,
3635          but it's the way it's done elsewhere, like elf64-alpha.c.  */
3636       /* Really remove the section.  */
3637       for (secpp = &stdoutput->sections;
3638            *secpp != real_reg_section;
3639            secpp = &(*secpp)->next)
3640         ;
3641       *secpp = (*secpp)->next;
3642       --stdoutput->section_count;
3643     }
3644
3645 }
3646
3647 /* Provide an expression for a built-in name provided when-used.
3648    Either a symbol that is a handler; living in 0x10*[1..8] and having
3649    name [DVWIOUZX]_Handler, or a mmixal built-in symbol.
3650
3651    If the name isn't a built-in name and parsed into *EXPP, return zero.  */
3652
3653 int
3654 mmix_parse_predefined_name (name, expP)
3655      char *name;
3656      expressionS *expP;
3657 {
3658   char *canon_name;
3659   char *handler_charp;
3660   const char handler_chars[] = "DVWIOUZX";
3661   symbolS *symp;
3662
3663   if (! predefined_syms)
3664     return 0;
3665
3666   canon_name = tc_canonicalize_symbol_name (name);
3667
3668   if (canon_name[1] == '_'
3669       && strcmp (canon_name + 2, "Handler") == 0
3670       && (handler_charp = strchr (handler_chars, *canon_name)) != NULL)
3671     {
3672       /* If the symbol doesn't exist, provide one relative to the .text
3673          section.
3674
3675          FIXME: We should provide separate sections, mapped in the linker
3676          script.  */
3677       symp = symbol_find (name);
3678       if (symp == NULL)
3679         symp = symbol_new (name, text_section,
3680                            0x10 * (handler_charp + 1 - handler_chars),
3681                            &zero_address_frag);
3682     }
3683   else
3684     {
3685       /* These symbols appear when referenced; needed for
3686          mmixal-compatible programs.  */
3687       unsigned int i;
3688
3689       static const struct
3690       {
3691         const char *name;
3692         valueT val;
3693       } predefined_abs_syms[] =
3694         {
3695           {"Data_Segment", (valueT) 0x20 << 56},
3696           {"Pool_Segment", (valueT) 0x40 << 56},
3697           {"Stack_Segment", (valueT) 0x60 << 56},
3698           {"StdIn", 0},
3699           {"StdOut", 1},
3700           {"StdErr", 2},
3701           {"TextRead", 0},
3702           {"TextWrite", 1},
3703           {"BinaryRead", 2},
3704           {"BinaryWrite", 3},
3705           {"BinaryReadWrite", 4},
3706           {"Halt", 0},
3707           {"Fopen", 1},
3708           {"Fclose", 2},
3709           {"Fread", 3},
3710           {"Fgets", 4},
3711           {"Fgetws", 5},
3712           {"Fwrite", 6},
3713           {"Fputs", 7},
3714           {"Fputws", 8},
3715           {"Fseek", 9},
3716           {"Ftell", 10},
3717           {"D_BIT", 0x80},
3718           {"V_BIT", 0x40},
3719           {"W_BIT", 0x20},
3720           {"I_BIT", 0x10},
3721           {"O_BIT", 0x08},
3722           {"U_BIT", 0x04},
3723           {"Z_BIT", 0x02},
3724           {"X_BIT", 0x01},
3725           {"Inf", 0x7ff00000}
3726         };
3727
3728       /* If it's already in the symbol table, we shouldn't do anything.  */
3729       symp = symbol_find (name);
3730       if (symp != NULL)
3731         return 0;
3732
3733       for (i = 0;
3734            i < sizeof (predefined_abs_syms)/sizeof (predefined_abs_syms[0]);
3735            i++)
3736         if (strcmp (canon_name, predefined_abs_syms[i].name) == 0)
3737           {
3738             symbol_table_insert (symbol_new (predefined_abs_syms[i].name,
3739                                              absolute_section,
3740                                              predefined_abs_syms[i].val,
3741                                              &zero_address_frag));
3742
3743             /* Let gas find the symbol we just created, through its
3744                ordinary lookup.  */
3745             return 0;
3746           }
3747
3748       /* Not one of those symbols.  Let gas handle it.  */
3749       return 0;
3750     }
3751
3752   expP->X_op = O_symbol;
3753   expP->X_add_number = 0;
3754   expP->X_add_symbol = symp;
3755   expP->X_op_symbol = NULL;
3756
3757   return 1;
3758 }
3759
3760 /* Worker for mmix_frob_file_before_adjust.  */
3761
3762 static void
3763 mmix_frob_local_reloc (abfd, sec, xxx)
3764      bfd *abfd ATTRIBUTE_UNUSED;
3765      asection *sec;
3766      PTR xxx ATTRIBUTE_UNUSED;
3767 {
3768   segment_info_type *seginfo = seg_info (sec);
3769   fixS *fixp;
3770
3771   if (seginfo == NULL)
3772     return;
3773
3774   for (fixp = seginfo->fix_root; fixp; fixp = fixp->fx_next)
3775     if (! fixp->fx_done && fixp->fx_addsy != NULL)
3776       {
3777         symbolS *sym = fixp->fx_addsy;
3778         asection *section = S_GET_SEGMENT (sym);
3779
3780         if (section == reg_section
3781             && fixp->fx_r_type == BFD_RELOC_MMIX_LOCAL)
3782           {
3783             /* If the register is marked global, we don't need to replace
3784                with the *real* register section since that will be done
3785                when the symbol is changed.  */
3786             if (! S_IS_EXTERNAL (sym))
3787               /* If it's a local symbol, we replace it with an anonymous
3788                  one with the same constant value.  */
3789               fixp->fx_addsy = expr_build_uconstant (S_GET_VALUE (sym));
3790           }
3791       }
3792 }
3793
3794 /* Change fixups for register symbols for BFD_MMIX_LOCAL to be for an
3795    absolute symbol.  */
3796
3797 void
3798 mmix_frob_file_before_adjust ()
3799 {
3800   return;
3801   bfd_map_over_sections (stdoutput, mmix_frob_local_reloc, (char *) 0);
3802 }
3803
3804 /* Just check that we don't have a BSPEC/ESPEC pair active when changing
3805    sections "normally", and get knowledge about alignment from the new
3806    section.  */
3807
3808 void
3809 mmix_md_elf_section_change_hook ()
3810 {
3811   if (doing_bspec)
3812     as_bad (_("section change from within a BSPEC/ESPEC pair is not supported"));
3813
3814   last_alignment = bfd_get_section_alignment (now_seg->owner, now_seg);
3815   want_unaligned = 0;
3816 }
3817
3818 /* The LOC worker.  This is like s_org, but we have to support changing
3819    section too.   */
3820
3821 static void
3822 s_loc (ignore)
3823      int ignore ATTRIBUTE_UNUSED;
3824 {
3825   segT section;
3826   expressionS exp;
3827   char *p;
3828   symbolS *sym;
3829   offsetT off;
3830
3831   /* Must not have a BSPEC in progress. */
3832   if (doing_bspec)
3833     {
3834       as_bad (_("directive LOC from within a BSPEC/ESPEC pair is not supported"));
3835       return;
3836     }
3837
3838   section = expression (&exp);
3839
3840   if (exp.X_op == O_illegal
3841       || exp.X_op == O_absent
3842       || exp.X_op == O_big
3843       || section == undefined_section)
3844     {
3845       as_bad (_("invalid LOC expression"));
3846       return;
3847     }
3848
3849   if (section == absolute_section)
3850     {
3851       /* Translate a constant into a suitable section.  */
3852
3853       if (exp.X_add_number < ((offsetT) 0x20 << 56))
3854         {
3855           /* Lower than Data_Segment - assume it's .text.  */
3856           section = text_section;
3857
3858           /* Save the lowest seen location, so we can pass on this
3859              information to the linker.  We don't actually org to this
3860              location here, we just pass on information to the linker so
3861              it can put the code there for us.  */
3862
3863           /* If there was already a loc (that has to be set lower than
3864              this one), we org at (this - lower).  There's an implicit
3865              "LOC 0" before any entered code.  FIXME: handled by spurious
3866              settings of text_has_contents.  */
3867           if (exp.X_add_number < 0
3868               || exp.X_add_number < (offsetT) lowest_text_loc)
3869             {
3870               as_bad (_("LOC expression stepping backwards is not supported"));
3871               exp.X_op = O_absent;
3872             }
3873           else
3874             {
3875               if (text_has_contents && lowest_text_loc == (bfd_vma) -1)
3876                 lowest_text_loc = 0;
3877
3878               if (lowest_text_loc == (bfd_vma) -1)
3879                 {
3880                   lowest_text_loc = exp.X_add_number;
3881
3882                   /* We want only to change the section, not set an offset.  */
3883                   exp.X_op = O_absent;
3884                 }
3885               else
3886                 exp.X_add_number -= lowest_text_loc;
3887             }
3888         }
3889       else
3890         {
3891           /* Do the same for the .data section.  */
3892           section = data_section;
3893
3894           if (exp.X_add_number < (offsetT) lowest_data_loc)
3895             {
3896               as_bad (_("LOC expression stepping backwards is not supported"));
3897               exp.X_op = O_absent;
3898             }
3899           else
3900             {
3901               if (data_has_contents && lowest_data_loc == (bfd_vma) -1)
3902                 lowest_data_loc = (bfd_vma) 0x20 << 56;
3903
3904               if (lowest_data_loc == (bfd_vma) -1)
3905                 {
3906                   lowest_data_loc = exp.X_add_number;
3907
3908                   /* We want only to change the section, not set an offset.  */
3909                   exp.X_op = O_absent;
3910                 }
3911               else
3912                 exp.X_add_number -= lowest_data_loc;
3913             }
3914         }
3915     }
3916
3917   if (section != now_seg)
3918     {
3919       obj_elf_section_change_hook ();
3920       subseg_set (section, 0);
3921
3922       /* Call our section change hooks using the official hook.  */
3923       md_elf_section_change_hook ();
3924     }
3925
3926   if (exp.X_op != O_absent)
3927     {
3928       if (exp.X_op != O_constant && exp.X_op != O_symbol)
3929         {
3930           /* Handle complex expressions.  */
3931           sym = make_expr_symbol (&exp);
3932           off = 0;
3933         }
3934       else
3935         {
3936           sym = exp.X_add_symbol;
3937           off = exp.X_add_number;
3938         }
3939
3940       p = frag_var (rs_org, 1, 1, (relax_substateT) 0, sym, off, (char *) 0);
3941       *p = 0;
3942     }
3943
3944   mmix_handle_rest_of_empty_line ();
3945 }
3946
3947 /* The BYTE worker.  We have to support sequences of mixed "strings",
3948    numbers and other constant "first-pass" reducible expressions separated
3949    by comma.  */
3950
3951 static void
3952 mmix_byte ()
3953 {
3954   unsigned int c;
3955   char *start;
3956
3957   if (now_seg == text_section)
3958     text_has_contents = 1;
3959   else if (now_seg == data_section)
3960     data_has_contents = 1;
3961
3962   do
3963     {
3964       SKIP_WHITESPACE ();
3965       switch (*input_line_pointer)
3966         {
3967         case '\"':
3968           ++input_line_pointer;
3969           start = input_line_pointer;
3970           while (is_a_char (c = next_char_of_string ()))
3971             {
3972               FRAG_APPEND_1_CHAR (c);
3973             }
3974
3975           if (input_line_pointer[-1] != '\"')
3976             {
3977               /* We will only get here in rare cases involving #NO_APP,
3978                  where the unterminated string is not recognized by the
3979                  preformatting pass.  */
3980               as_bad (_("unterminated string"));
3981               mmix_discard_rest_of_line ();
3982               return;
3983             }
3984           break;
3985
3986         default:
3987           {
3988             expressionS exp;
3989             segT expseg = expression (&exp);
3990
3991             /* We have to allow special register names as constant numbers.  */
3992             if ((expseg != absolute_section && expseg != reg_section)
3993                 || (exp.X_op != O_constant
3994                     && (exp.X_op != O_register
3995                         || exp.X_add_number <= 255)))
3996               {
3997                 as_bad (_("BYTE expression not a pure number"));
3998                 mmix_discard_rest_of_line ();
3999                 return;
4000               }
4001             else if ((exp.X_add_number > 255 && exp.X_op != O_register)
4002                      || exp.X_add_number < 0)
4003               {
4004                 /* Note that mmixal does not allow negative numbers in
4005                    BYTE sequences, so neither should we.  */
4006                 as_bad (_("BYTE expression not in the range 0..255"));
4007                 mmix_discard_rest_of_line ();
4008                 return;
4009               }
4010
4011             FRAG_APPEND_1_CHAR (exp.X_add_number);
4012           }
4013           break;
4014         }
4015
4016       SKIP_WHITESPACE ();
4017       c = *input_line_pointer++;
4018     }
4019   while (c == ',');
4020
4021   input_line_pointer--;
4022
4023   if (mmix_gnu_syntax)
4024     demand_empty_rest_of_line ();
4025   else
4026     {
4027       mmix_discard_rest_of_line ();
4028       /* Do like demand_empty_rest_of_line and step over the end-of-line
4029          boundary.  */
4030       input_line_pointer++;
4031     }
4032
4033   /* Make sure we align for the next instruction.  */
4034   last_alignment = 0;
4035 }
4036
4037 /* Like cons_worker, but we have to ignore "naked comments", not barf on
4038    them.  Implements WYDE, TETRA and OCTA.  We're a little bit more
4039    lenient than mmix_byte but FIXME: they should eventually merge.  */
4040
4041 static void
4042 mmix_cons (nbytes)
4043      int nbytes;
4044 {
4045   expressionS exp;
4046   char *start;
4047
4048   /* If we don't have any contents, then it's ok to have a specified start
4049      address that is not a multiple of the max data size.  We will then
4050      align it as necessary when we get here.  Otherwise, it's a fatal sin.  */
4051   if (now_seg == text_section)
4052     {
4053       if (lowest_text_loc != (bfd_vma) -1
4054           && (lowest_text_loc & (nbytes - 1)) != 0)
4055         {
4056           if (text_has_contents)
4057             as_bad (_("data item with alignment larger than location"));
4058           else if (want_unaligned)
4059             as_bad (_("unaligned data at an absolute location is not supported"));
4060
4061           lowest_text_loc &= ~((bfd_vma) nbytes - 1);
4062           lowest_text_loc += (bfd_vma) nbytes;
4063         }
4064
4065       text_has_contents = 1;
4066     }
4067   else if (now_seg == data_section)
4068     {
4069       if (lowest_data_loc != (bfd_vma) -1
4070           && (lowest_data_loc & (nbytes - 1)) != 0)
4071         {
4072           if (data_has_contents)
4073             as_bad (_("data item with alignment larger than location"));
4074           else if (want_unaligned)
4075             as_bad (_("unaligned data at an absolute location is not supported"));
4076
4077           lowest_data_loc &= ~((bfd_vma) nbytes - 1);
4078           lowest_data_loc += (bfd_vma) nbytes;
4079         }
4080
4081       data_has_contents = 1;
4082     }
4083
4084   /* Always align these unless asked not to (valid for the current pseudo).  */
4085   if (! want_unaligned)
4086     {
4087       last_alignment = nbytes == 2 ? 1 : (nbytes == 4 ? 2 : 3);
4088       frag_align (last_alignment, 0, 0);
4089       record_alignment (now_seg, last_alignment);
4090     }
4091
4092   /* For mmixal compatibility, a label for an instruction (and emitting
4093      pseudo) refers to the _aligned_ address.  So we have to emit the
4094      label here.  */
4095   if (current_fb_label >= 0)
4096     colon (fb_label_name (current_fb_label, 1));
4097   else if (pending_label != NULL)
4098     {
4099       colon (pending_label);
4100       pending_label = NULL;
4101     }
4102
4103   SKIP_WHITESPACE ();
4104
4105   if (is_end_of_line [(unsigned int) *input_line_pointer])
4106     {
4107       /* Default to zero if the expression was absent.  */
4108
4109       exp.X_op = O_constant;
4110       exp.X_add_number = 0;
4111       exp.X_unsigned = 0;
4112       exp.X_add_symbol = NULL;
4113       exp.X_op_symbol = NULL;
4114       emit_expr (&exp, (unsigned int) nbytes);
4115     }
4116   else
4117     do
4118       {
4119         unsigned int c;
4120
4121         switch (*input_line_pointer)
4122           {
4123             /* We support strings here too; each character takes up nbytes
4124                bytes.  */
4125           case '\"':
4126             ++input_line_pointer;
4127             start = input_line_pointer;
4128             while (is_a_char (c = next_char_of_string ()))
4129               {
4130                 exp.X_op = O_constant;
4131                 exp.X_add_number = c;
4132                 exp.X_unsigned = 1;
4133                 emit_expr (&exp, (unsigned int) nbytes);
4134               }
4135
4136             if (input_line_pointer[-1] != '\"')
4137               {
4138                 /* We will only get here in rare cases involving #NO_APP,
4139                    where the unterminated string is not recognized by the
4140                    preformatting pass.  */
4141                 as_bad (_("unterminated string"));
4142                 mmix_discard_rest_of_line ();
4143                 return;
4144               }
4145             break;
4146
4147           default:
4148             {
4149               expression (&exp);
4150               emit_expr (&exp, (unsigned int) nbytes);
4151               SKIP_WHITESPACE ();
4152             }
4153             break;
4154           }
4155       }
4156     while (*input_line_pointer++ == ',');
4157
4158   input_line_pointer--;         /* Put terminator back into stream.  */
4159
4160   mmix_handle_rest_of_empty_line ();
4161
4162   /* We don't need to step up the counter for the current_fb_label here;
4163      that's handled by the caller.  */
4164 }
4165
4166 /* The md_do_align worker.  At present, we just record an alignment to
4167    nullify the automatic alignment we do for WYDE, TETRA and OCTA, as gcc
4168    does not use the unaligned macros when attribute packed is used.
4169    Arguably this is a GCC bug.  */
4170
4171 void
4172 mmix_md_do_align (n, fill, len, max)
4173      int n;
4174      char *fill ATTRIBUTE_UNUSED;
4175      int len ATTRIBUTE_UNUSED;
4176      int max ATTRIBUTE_UNUSED;
4177 {
4178   last_alignment = n;
4179   want_unaligned = n == 0;
4180 }