Update all uses of md_apply_fix to use md_apply_fix3. Make it a void function.
[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 void
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;
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;
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;
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
2546 /* A bsearch function for looking up a value against offsets for GREG
2547    definitions.  */
2548
2549 static int
2550 cmp_greg_val_greg_symbol_fixes (p1, p2)
2551      const PTR p1;
2552      const PTR p2;
2553 {
2554   offsetT val1 = *(offsetT *) p1;
2555   offsetT val2 = ((struct mmix_symbol_greg_fixes *) p2)->offs;
2556
2557   if (val1 >= val2 && val1 < val2 + 255)
2558     return 0;
2559
2560   if (val1 > val2)
2561     return 1;
2562
2563   return -1;
2564 }
2565
2566 /* Generate a machine-dependent relocation.  */
2567
2568 arelent *
2569 tc_gen_reloc (section, fixP)
2570      asection *section ATTRIBUTE_UNUSED;
2571      fixS *fixP;
2572 {
2573   bfd_signed_vma val
2574     = fixP->fx_offset + (fixP->fx_addsy ? S_GET_VALUE (fixP->fx_addsy) : 0);
2575   arelent *relP;
2576   bfd_reloc_code_real_type code = BFD_RELOC_NONE;
2577   char *buf  = fixP->fx_where + fixP->fx_frag->fr_literal;
2578   symbolS *addsy = fixP->fx_addsy;
2579   asection *addsec = addsy == NULL ? NULL : S_GET_SEGMENT (addsy);
2580   bfd_vma addend = fixP->fx_offset;
2581   asymbol *baddsy = addsy != NULL ? symbol_get_bfdsym (addsy) : NULL;
2582
2583   /* A single " LOCAL expression" in the wrong section will not work when
2584      linking to MMO; relocations for zero-content sections are then
2585      ignored.  Normally, relocations would modify section contents, and
2586      you'd never think or be able to do something like that.  The
2587      relocation resulting from a LOCAL directive doesn't have an obvious
2588      and mandatory location.  I can't figure out a way to do this better
2589      than just helping the user around this limitation here; hopefully the
2590      code using the local expression is around.  Putting the LOCAL
2591      semantics in a relocation still seems right; a section didn't do.  */
2592   if (bfd_section_size (section->owner, section) == 0)
2593     as_bad_where
2594       (fixP->fx_file, fixP->fx_line,
2595        fixP->fx_r_type == BFD_RELOC_MMIX_LOCAL
2596        /* The BFD_RELOC_MMIX_LOCAL-specific message is supposed to be
2597           user-friendly, though a little bit non-substantial.  */
2598        ? _("directive LOCAL must be placed in code or data")
2599        : _("internal confusion: relocation in a section without contents"));
2600
2601   /* FIXME: Range tests for all these.  */
2602   switch (fixP->fx_r_type)
2603     {
2604     case BFD_RELOC_64:
2605     case BFD_RELOC_32:
2606     case BFD_RELOC_24:
2607     case BFD_RELOC_16:
2608     case BFD_RELOC_8:
2609       code = fixP->fx_r_type;
2610
2611       if (addsy == NULL
2612           || bfd_is_abs_section (S_GET_SEGMENT (addsy)))
2613         {
2614           /* Resolve this reloc now, as md_apply_fix3 would have done (not
2615              called if -linkrelax).  There is no point in keeping a reloc
2616              to an absolute symbol.  No reloc that is subject to
2617              relaxation must be to an absolute symbol; difference
2618              involving symbols in a specific section must be signalled as
2619              an error if the relaxing cannot be expressed; having a reloc
2620              to the resolved (now absolute) value does not help.  */
2621           md_number_to_chars (buf, val, fixP->fx_size);
2622           return NULL;
2623         }
2624       break;
2625
2626     case BFD_RELOC_64_PCREL:
2627     case BFD_RELOC_32_PCREL:
2628     case BFD_RELOC_24_PCREL:
2629     case BFD_RELOC_16_PCREL:
2630     case BFD_RELOC_8_PCREL:
2631     case BFD_RELOC_MMIX_LOCAL:
2632     case BFD_RELOC_VTABLE_INHERIT:
2633     case BFD_RELOC_VTABLE_ENTRY:
2634     case BFD_RELOC_MMIX_GETA:
2635     case BFD_RELOC_MMIX_GETA_1:
2636     case BFD_RELOC_MMIX_GETA_2:
2637     case BFD_RELOC_MMIX_GETA_3:
2638     case BFD_RELOC_MMIX_CBRANCH:
2639     case BFD_RELOC_MMIX_CBRANCH_J:
2640     case BFD_RELOC_MMIX_CBRANCH_1:
2641     case BFD_RELOC_MMIX_CBRANCH_2:
2642     case BFD_RELOC_MMIX_CBRANCH_3:
2643     case BFD_RELOC_MMIX_PUSHJ:
2644     case BFD_RELOC_MMIX_PUSHJ_1:
2645     case BFD_RELOC_MMIX_PUSHJ_2:
2646     case BFD_RELOC_MMIX_PUSHJ_3:
2647     case BFD_RELOC_MMIX_JMP:
2648     case BFD_RELOC_MMIX_JMP_1:
2649     case BFD_RELOC_MMIX_JMP_2:
2650     case BFD_RELOC_MMIX_JMP_3:
2651     case BFD_RELOC_MMIX_ADDR19:
2652     case BFD_RELOC_MMIX_ADDR27:
2653       code = fixP->fx_r_type;
2654       break;
2655
2656     case BFD_RELOC_MMIX_REG_OR_BYTE:
2657       /* If we have this kind of relocation to an unknown symbol or to the
2658          register contents section (that is, to a register), then we can't
2659          resolve the relocation here.  */
2660       if (addsy != NULL
2661           && (bfd_is_und_section (S_GET_SEGMENT (addsy))
2662               || strcmp (bfd_get_section_name (addsec->owner, addsec),
2663                          MMIX_REG_CONTENTS_SECTION_NAME) == 0))
2664         {
2665           code = fixP->fx_r_type;
2666           break;
2667         }
2668
2669       /* If the relocation is not to the register section or to the
2670          absolute section (a numeric value), then we have an error.  */
2671       if (addsy != NULL
2672           && (S_GET_SEGMENT (addsy) != real_reg_section
2673               || val > 255
2674               || val < 0)
2675           && ! bfd_is_abs_section (S_GET_SEGMENT (addsy)))
2676         goto badop;
2677
2678       /* Set the "immediate" bit of the insn if this relocation is to Z
2679          field when the value is a numeric value, i.e. not a register.  */
2680       if ((fixP->fx_where & 3) == 3
2681           && (addsy == NULL
2682               || S_GET_SEGMENT (addsy) == absolute_section))
2683         buf[-3] |= IMM_OFFSET_BIT;
2684
2685       buf[0] = val;
2686       return NULL;
2687
2688     case BFD_RELOC_MMIX_BASE_PLUS_OFFSET:
2689       if (addsy != NULL
2690           &&  strcmp (bfd_get_section_name (addsec->owner, addsec),
2691                       MMIX_REG_CONTENTS_SECTION_NAME) == 0)
2692         {
2693           /* This changed into a register; the relocation is for the
2694              register-contents section.  The constant part remains zero.  */
2695           code = BFD_RELOC_MMIX_REG;
2696           break;
2697         }
2698
2699       /* If we've found out that this was indeed a register, then replace
2700          with the register number.  The constant part is already zero.
2701
2702          If we encounter any other defined symbol, then we must find a
2703          suitable register and emit a reloc.  */
2704       if (addsy == NULL
2705           || S_GET_SEGMENT (addsy) != real_reg_section)
2706         {
2707           struct mmix_symbol_gregs *gregs;
2708           struct mmix_symbol_greg_fixes *fix;
2709
2710           if (S_IS_DEFINED (addsy))
2711             {
2712               if (! symbol_section_p (addsy)
2713                   && ! bfd_is_abs_section (S_GET_SEGMENT (addsy)))
2714                 as_fatal (_("internal: BFD_RELOC_MMIX_BASE_PLUS_OFFSET not resolved to section"));
2715
2716               /* If this is an absolute symbol sufficiently near
2717                  lowest_data_loc, then we canonicalize on the data
2718                  section.  Note that val is signed here; we may subtract
2719                  lowest_data_loc which is unsigned.  Careful with those
2720                  comparisons.  */
2721               if (lowest_data_loc != (bfd_vma) -1
2722                   && (bfd_vma) val + 256 > lowest_data_loc
2723                   && bfd_is_abs_section (S_GET_SEGMENT (addsy)))
2724                 {
2725                   val -= (offsetT) lowest_data_loc;
2726                   addsy = section_symbol (data_section);
2727                 }
2728               /* Likewise text section.  */
2729               else if (lowest_text_loc != (bfd_vma) -1
2730                        && (bfd_vma) val + 256 > lowest_text_loc
2731                        && bfd_is_abs_section (S_GET_SEGMENT (addsy)))
2732                 {
2733                   val -= (offsetT) lowest_text_loc;
2734                   addsy = section_symbol (text_section);
2735                 }
2736             }
2737
2738           gregs = *symbol_get_tc (addsy);
2739
2740           /* If that symbol does not have any associated GREG definitions,
2741              we can't do anything.  FIXME: implement allocate-on-demand in
2742              the linker.  */
2743           if (gregs == NULL
2744               || (fix = bsearch (&val, gregs->greg_fixes, gregs->n_gregs,
2745                                  sizeof (gregs->greg_fixes[0]),
2746                                  cmp_greg_val_greg_symbol_fixes)) == NULL
2747               /* The register must not point *after* the address we want.  */
2748               || fix->offs > val
2749               /* Neither must the register point more than 255 bytes
2750                  before the address we want.  */
2751               || fix->offs + 255 < val)
2752             {
2753               as_bad_where (fixP->fx_file, fixP->fx_line,
2754                             _("no suitable GREG definition for operands"));
2755               return NULL;
2756             }
2757           else
2758             {
2759               /* Transform the base-plus-offset reloc for the actual area
2760                  to a reloc for the register with the address of the area.
2761                  Put addend for register in Z operand.  */
2762               buf[1] = val - fix->offs;
2763               code = BFD_RELOC_MMIX_REG;
2764               baddsy
2765                 = (bfd_get_section_by_name (stdoutput,
2766                                             MMIX_REG_CONTENTS_SECTION_NAME)
2767                    ->symbol);
2768
2769               addend = fix->fix->fx_frag->fr_address + fix->fix->fx_where;
2770             }
2771         }
2772       else if (S_GET_VALUE (addsy) > 255)
2773         as_bad_where (fixP->fx_file, fixP->fx_line,
2774                       _("invalid operands"));
2775       else
2776         {
2777           *buf = val;
2778           return NULL;
2779         }
2780       break;
2781
2782     case BFD_RELOC_MMIX_REG:
2783       if (addsy != NULL
2784           && (bfd_is_und_section (S_GET_SEGMENT (addsy))
2785               || strcmp (bfd_get_section_name (addsec->owner, addsec),
2786                          MMIX_REG_CONTENTS_SECTION_NAME) == 0))
2787         {
2788           code = fixP->fx_r_type;
2789           break;
2790         }
2791
2792       if (addsy != NULL
2793           && (S_GET_SEGMENT (addsy) != real_reg_section
2794               || val > 255
2795               || val < 0)
2796           && ! bfd_is_und_section (S_GET_SEGMENT (addsy)))
2797         /* Drop through to error message.  */
2798         ;
2799       else
2800         {
2801           buf[0] = val;
2802           return NULL;
2803         }
2804       /* FALLTHROUGH. */
2805
2806       /* The others are supposed to be handled by md_apply_fix3.
2807          FIXME: ... which isn't called when -linkrelax.  Move over
2808          md_apply_fix3 code here for everything reasonable.  */
2809     badop:
2810     default:
2811       as_bad_where
2812         (fixP->fx_file, fixP->fx_line,
2813          _("operands were not reducible at assembly-time"));
2814
2815       /* Unmark this symbol as used in a reloc, so we don't bump into a BFD
2816          assert when trying to output reg_section.  FIXME: A gas bug.  */
2817       if (addsy)
2818         symbol_clear_used_in_reloc (addsy);
2819       return NULL;
2820     }
2821
2822   relP = (arelent *) xmalloc (sizeof (arelent));
2823   assert (relP != 0);
2824   relP->sym_ptr_ptr = (asymbol **) xmalloc (sizeof (asymbol *));
2825   *relP->sym_ptr_ptr = baddsy;
2826   relP->address = fixP->fx_frag->fr_address + fixP->fx_where;
2827
2828   relP->addend = addend;
2829
2830   /* If this had been a.out, we would have had a kludge for weak symbols
2831      here.  */
2832
2833   relP->howto = bfd_reloc_type_lookup (stdoutput, code);
2834   if (! relP->howto)
2835     {
2836       const char *name;
2837
2838       name = S_GET_NAME (addsy);
2839       if (name == NULL)
2840         name = _("<unknown>");
2841       as_fatal (_("cannot generate relocation type for symbol %s, code %s"),
2842                 name, bfd_get_reloc_code_name (code));
2843     }
2844
2845   return relP;
2846 }
2847
2848 /* Do some reformatting of a line.  FIXME: We could transform a mmixal
2849    line into traditional (GNU?) format, unless #NO_APP, and get rid of all
2850    ugly labels_without_colons etc.  */
2851
2852 void
2853 mmix_handle_mmixal ()
2854 {
2855   char *s0 = input_line_pointer;
2856   char *s;
2857   char *label = NULL;
2858   char c;
2859
2860   if (pending_label != NULL)
2861     as_fatal (_("internal: unhandled label %s"), pending_label);
2862
2863   if (mmix_gnu_syntax)
2864     return;
2865
2866   /* If the first character is a '.', then it's a pseudodirective, not a
2867      label.  Make GAS not handle label-without-colon on this line.  We
2868      also don't do mmixal-specific stuff on this line.  */
2869   if (input_line_pointer[0] == '.')
2870     {
2871       label_without_colon_this_line = 0;
2872       return;
2873     }
2874
2875   /* Don't handle empty lines here.  */
2876   while (1)
2877     {
2878       if (*s0 == 0 || is_end_of_line [(unsigned int) *s0])
2879         return;
2880
2881       if (! ISSPACE (*s0))
2882         break;
2883
2884       s0++;
2885     }
2886
2887   /* If we're on a line with a label, check if it's a mmixal fb-label.
2888      Save an indicator and skip the label; it must be set only after all
2889      fb-labels of expressions are evaluated.  */
2890   if (ISDIGIT (input_line_pointer[0])
2891       && input_line_pointer[1] == 'H'
2892       && ISSPACE (input_line_pointer[2]))
2893     {
2894       char *s;
2895       current_fb_label = input_line_pointer[0] - '0';
2896
2897       /* We have to skip the label, but also preserve the newlineness of
2898          the previous character, since the caller checks that.  It's a
2899          mess we blame on the caller.  */
2900       input_line_pointer[1] = input_line_pointer[-1];
2901       input_line_pointer += 2;
2902
2903       s = input_line_pointer;
2904       while (*s && ISSPACE (*s) && ! is_end_of_line[(unsigned int) *s])
2905         s++;
2906
2907       /* For errors emitted here, the book-keeping is off by one; the
2908          caller is about to bump the counters.  Adjust the error messages.  */
2909       if (is_end_of_line [(unsigned int) *s])
2910         {
2911           char *name;
2912           unsigned int line;
2913           as_where (&name, &line);
2914           as_bad_where (name, line + 1,
2915                         _("[0-9]H labels may not appear alone on a line"));
2916           current_fb_label = -1;
2917         }
2918       if (*s == '.')
2919         {
2920           char *name;
2921           unsigned int line;
2922           as_where (&name, &line);
2923           as_bad_where (name, line + 1,
2924                         _("[0-9]H labels do not mix with dot-pseudos"));
2925           current_fb_label = -1;
2926         }
2927     }
2928   else
2929     {
2930       current_fb_label = -1;
2931       if (is_name_beginner (input_line_pointer[0]))
2932         label = input_line_pointer;
2933     }
2934
2935   s0 = input_line_pointer;
2936   /* Skip over label. */
2937   while (*s0 && is_part_of_name (*s0))
2938     s0++;
2939
2940   /* Remove trailing ":" off labels, as they'd otherwise be considered
2941      part of the name.  But don't do it for local labels.  */
2942   if (s0 != input_line_pointer && s0[-1] == ':'
2943       && (s0 - 2 != input_line_pointer
2944           || ! ISDIGIT (s0[-2])))
2945     s0[-1] = ' ';
2946   else if (label != NULL)
2947     {
2948       /* For labels that don't end in ":", we save it so we can later give
2949          it the same alignment and address as the associated instruction.  */
2950
2951       /* Make room for the label including the ending nul.  */
2952       int len_0 = s0 - label + 1;
2953
2954       /* Save this label on the MMIX symbol obstack.  Saving it on an
2955          obstack is needless for "IS"-pseudos, but it's harmless and we
2956          avoid a little code-cluttering.  */
2957       obstack_grow (&mmix_sym_obstack, label, len_0);
2958       pending_label = obstack_finish (&mmix_sym_obstack);
2959       pending_label[len_0 - 1] = 0;
2960     }
2961
2962   while (*s0 && ISSPACE (*s0) && ! is_end_of_line [(unsigned int) *s0])
2963     s0++;
2964
2965   if (pending_label != NULL && is_end_of_line [(unsigned int) *s0])
2966     /* Whoops, this was actually a lone label on a line.  Like :-ended
2967        labels, we don't attach such labels to the next instruction or
2968        pseudo.  */
2969     pending_label = NULL;
2970
2971   /* Find local labels of operands.  Look for "[0-9][FB]" where the
2972      characters before and after are not part of words.  Break if a single
2973      or double quote is seen anywhere.  It means we can't have local
2974      labels as part of list with mixed quoted and unquoted members for
2975      mmixal compatibility but we can't have it all.  For the moment.
2976      Replace the '<N>B' or '<N>F' with MAGIC_FB_BACKWARD_CHAR<N> and
2977      MAGIC_FB_FORWARD_CHAR<N> respectively.  */
2978
2979   /* First make sure we don't have any of the magic characters on the line
2980      appearing as input.  */
2981   s = s0;
2982   while (*s)
2983     {
2984       c = *s++;
2985       if (is_end_of_line [(unsigned int) c])
2986         break;
2987       if (c == MAGIC_FB_BACKWARD_CHAR || c == MAGIC_FB_FORWARD_CHAR)
2988         as_bad (_("invalid characters in input"));
2989     }
2990
2991   /* Scan again, this time looking for ';' after operands.  */
2992   s = s0;
2993
2994   /* Skip the insn.  */
2995   while (*s
2996          && ! ISSPACE (*s)
2997          && *s != ';'
2998          && ! is_end_of_line[(unsigned int) *s])
2999     s++;
3000
3001   /* Skip the spaces after the insn.  */
3002   while (*s
3003          && ISSPACE (*s)
3004          && *s != ';'
3005          && ! is_end_of_line[(unsigned int) *s])
3006     s++;
3007
3008   /* Skip the operands.  While doing this, replace [0-9][BF] with
3009      (MAGIC_FB_BACKWARD_CHAR|MAGIC_FB_FORWARD_CHAR)[0-9].  */
3010   while ((c = *s) != 0
3011          && ! ISSPACE (c)
3012          && c != ';'
3013          && ! is_end_of_line[(unsigned int) c])
3014     {
3015       if (c == '"')
3016         {
3017           s++;
3018
3019           /* FIXME: Test-case for semi-colon in string.  */
3020           while (*s
3021                  && *s != '"'
3022                  && (! is_end_of_line [(unsigned int) *s] || *s == ';'))
3023             s++;
3024
3025           if (*s == '"')
3026             s++;
3027         }
3028       else if (ISDIGIT (c))
3029         {
3030           if ((s[1] != 'B' && s[1] != 'F')
3031               || is_part_of_name (s[-1])
3032               || is_part_of_name (s[2]))
3033             s++;
3034           else
3035             {
3036               s[0] = (s[1] == 'B'
3037                       ? MAGIC_FB_BACKWARD_CHAR : MAGIC_FB_FORWARD_CHAR);
3038               s[1] = c;
3039             }
3040         }
3041       else
3042         s++;
3043     }
3044
3045   /* Skip any spaces after the operands.  */
3046   while (*s
3047          && ISSPACE (*s)
3048          && *s != ';'
3049          && !is_end_of_line[(unsigned int) *s])
3050     s++;
3051
3052   /* If we're now looking at a semi-colon, then it's an end-of-line
3053      delimiter.  */
3054   mmix_next_semicolon_is_eoln = (*s == ';');
3055
3056   /* Make IS into an EQU by replacing it with "= ".  Only match upper-case
3057      though; let lower-case be a syntax error.  */
3058   s = s0;
3059   if (s[0] == 'I' && s[1] == 'S' && ISSPACE (s[2]))
3060     {
3061       *s = '=';
3062       s[1] = ' ';
3063
3064       /* Since labels can start without ":", we have to handle "X IS 42"
3065          in full here, or "X" will be parsed as a label to be set at ".".  */
3066       input_line_pointer = s;
3067
3068       /* Right after this function ends, line numbers will be bumped if
3069          input_line_pointer[-1] = '\n'.  We want accurate line numbers for
3070          the equals call, so we bump them before the call, and make sure
3071          they aren't bumped afterwards.  */
3072       bump_line_counters ();
3073
3074       /* A fb-label is valid as an IS-label.  */
3075       if (current_fb_label >= 0)
3076         {
3077           char *fb_name;
3078
3079           /* We need to save this name on our symbol obstack, since the
3080              string we got in fb_label_name is volatile and will change
3081              with every call to fb_label_name, like those resulting from
3082              parsing the IS-operand.  */
3083           fb_name = fb_label_name (current_fb_label, 1);
3084           obstack_grow (&mmix_sym_obstack, fb_name, strlen (fb_name) + 1);
3085           equals (obstack_finish (&mmix_sym_obstack), 0);
3086           fb_label_instance_inc (current_fb_label);
3087           current_fb_label = -1;
3088         }
3089       else
3090         {
3091           if (pending_label == NULL)
3092             as_bad (_("empty label field for IS"));
3093           else
3094             equals (pending_label, 0);
3095           pending_label = NULL;
3096         }
3097
3098       /* For mmixal, we can have comments without a comment-start
3099          character.   */
3100       mmix_handle_rest_of_empty_line ();
3101       input_line_pointer--;
3102
3103       input_line_pointer[-1] = ' ';
3104     }
3105   else if (s[0] == 'G'
3106            && s[1] == 'R'
3107            && strncmp (s, "GREG", 4) == 0
3108            && (ISSPACE (s[4]) || is_end_of_line[(unsigned char) s[4]]))
3109     {
3110       input_line_pointer = s + 4;
3111
3112       /* Right after this function ends, line numbers will be bumped if
3113          input_line_pointer[-1] = '\n'.  We want accurate line numbers for
3114          the s_greg call, so we bump them before the call, and make sure
3115          they aren't bumped afterwards.  */
3116       bump_line_counters ();
3117
3118       /* A fb-label is valid as a GREG-label.  */
3119       if (current_fb_label >= 0)
3120         {
3121           char *fb_name;
3122
3123           /* We need to save this name on our symbol obstack, since the
3124              string we got in fb_label_name is volatile and will change
3125              with every call to fb_label_name, like those resulting from
3126              parsing the IS-operand.  */
3127           fb_name = fb_label_name (current_fb_label, 1);
3128
3129           /* Make sure we save the canonical name and don't get bitten by
3130              prefixes.  */
3131           obstack_1grow (&mmix_sym_obstack, ':');
3132           obstack_grow (&mmix_sym_obstack, fb_name, strlen (fb_name) + 1);
3133           mmix_greg_internal (obstack_finish (&mmix_sym_obstack));
3134           fb_label_instance_inc (current_fb_label);
3135           current_fb_label = -1;
3136         }
3137       else
3138         mmix_greg_internal (pending_label);
3139
3140       /* Back up before the end-of-line marker that was skipped in
3141          mmix_greg_internal.  */
3142       input_line_pointer--;
3143       input_line_pointer[-1] = ' ';
3144
3145       pending_label = NULL;
3146     }
3147   else if (pending_label != NULL)
3148     {
3149       input_line_pointer += strlen (pending_label);
3150
3151       /* See comment above about getting line numbers bumped.  */
3152       input_line_pointer[-1] = '\n';
3153     }
3154 }
3155
3156 /* Give the value of an fb-label rewritten as in mmix_handle_mmixal, when
3157    parsing an expression.
3158
3159    On valid calls, input_line_pointer points at a MAGIC_FB_BACKWARD_CHAR
3160    or MAGIC_FB_BACKWARD_CHAR, followed by an ascii digit for the label.
3161    We fill in the label as an expression.  */
3162
3163 void
3164 mmix_fb_label (expP)
3165      expressionS *expP;
3166 {
3167   symbolS *sym;
3168   char *fb_internal_name;
3169
3170   /* This doesn't happen when not using mmixal syntax.  */
3171   if (mmix_gnu_syntax
3172       || (input_line_pointer[0] != MAGIC_FB_BACKWARD_CHAR
3173           && input_line_pointer[0] != MAGIC_FB_FORWARD_CHAR))
3174     return;
3175
3176   /* The current backward reference has augmentation 0.  A forward
3177      reference has augmentation 1, unless it's the same as a fb-label on
3178      _this_ line, in which case we add one more so we don't refer to it.
3179      This is the semantics of mmixal; it differs to that of common
3180      fb-labels which refer to a here-label on the current line as a
3181      backward reference.  */
3182   fb_internal_name
3183     = fb_label_name (input_line_pointer[1] - '0',
3184                      (input_line_pointer[0] == MAGIC_FB_FORWARD_CHAR ? 1 : 0)
3185                      + ((input_line_pointer[1] - '0' == current_fb_label
3186                          && input_line_pointer[0] == MAGIC_FB_FORWARD_CHAR)
3187                         ? 1 : 0));
3188
3189   input_line_pointer += 2;
3190   sym = symbol_find_or_make (fb_internal_name);
3191
3192   /* We don't have to clean up unrelated fields here; we just do what the
3193      expr machinery does, but *not* just what it does for [0-9][fb], since
3194      we need to treat those as ordinary symbols sometimes; see testcases
3195      err-byte2.s and fb-2.s.  */
3196   if (S_GET_SEGMENT (sym) == absolute_section)
3197     {
3198       expP->X_op = O_constant;
3199       expP->X_add_number = S_GET_VALUE (sym);
3200     }
3201   else
3202     {
3203       expP->X_op = O_symbol;
3204       expP->X_add_symbol = sym;
3205       expP->X_add_number = 0;
3206     }
3207 }
3208
3209 /* See whether we need to force a relocation into the output file.
3210    This is used to force out switch and PC relative relocations when
3211    relaxing.  */
3212
3213 int
3214 mmix_force_relocation (fixP)
3215      fixS * fixP;
3216 {
3217   if (fixP->fx_r_type == BFD_RELOC_MMIX_LOCAL
3218       || fixP->fx_r_type == BFD_RELOC_VTABLE_INHERIT
3219       || fixP->fx_r_type == BFD_RELOC_VTABLE_ENTRY
3220       || fixP->fx_r_type == BFD_RELOC_MMIX_BASE_PLUS_OFFSET)
3221     return 1;
3222
3223   /* FIXME: This is dubious.  Handling of weak symbols should have been
3224      caught before we get here.  */
3225   if ((fixP->fx_addsy && S_IS_WEAK (fixP->fx_addsy)))
3226     return 1;
3227
3228   if (linkrelax)
3229     return 1;
3230
3231   /* All our pcrel relocations are must-keep.  Note that md_apply_fix3 is
3232      called *after* this, and will handle getting rid of the presumed
3233      reloc; a relocation isn't *forced* other than to be handled by
3234      md_apply_fix3 (or tc_gen_reloc if linkrelax).  */
3235   if (fixP->fx_pcrel)
3236     return 1;
3237
3238   return 0;
3239 }
3240
3241 /* The location from which a PC relative jump should be calculated,
3242    given a PC relative reloc.  */
3243
3244 long
3245 md_pcrel_from_section (fixP, sec)
3246      fixS * fixP;
3247      segT   sec;
3248 {
3249   if (fixP->fx_addsy != (symbolS *) NULL
3250       && (! S_IS_DEFINED (fixP->fx_addsy)
3251           || S_GET_SEGMENT (fixP->fx_addsy) != sec))
3252     {
3253       /* The symbol is undefined (or is defined but not in this section).
3254          Let the linker figure it out.  */
3255       return 0;
3256     }
3257
3258   return (fixP->fx_frag->fr_address + fixP->fx_where);
3259 }
3260
3261 /* Adjust the symbol table.  We make reg_section relative to the real
3262    register section.
3263
3264    FIXME: There's a gas bug; should be fixed when the reg_section symbol
3265    is "accidentally" saved for relocs which are really fixups that will be
3266    fixed up.  */
3267
3268 void
3269 mmix_adjust_symtab ()
3270 {
3271   symbolS *sym;
3272   symbolS *prevsym;
3273   symbolS *regsec = section_symbol (reg_section);
3274   segT realregsec = NULL;
3275
3276   for (prevsym = sym = symbol_rootP;
3277        sym != NULL;
3278        prevsym = sym, sym = symbol_next (sym))
3279     if (S_GET_SEGMENT (sym) == reg_section)
3280       {
3281         if (sym == regsec
3282             || (!S_IS_EXTERN (sym) && !symbol_used_in_reloc_p (sym)))
3283           {
3284             symbol_remove (sym, &symbol_rootP, &symbol_lastP);
3285
3286             /* We make one extra turn, or we'll lose the next symbol.  We
3287                assume that the symbol we remove is not the symbol root
3288                (.text normally is).  */
3289             sym = prevsym;
3290           }
3291         else
3292           {
3293             /* Change section to the *real* register section, so it gets
3294                proper treatment when writing it out.  Only do this for
3295                global symbols.  This also means we don't have to check for
3296                $0..$255.  */
3297             if (realregsec == NULL)
3298               realregsec
3299                 = bfd_make_section_old_way (stdoutput, MMIX_REG_SECTION_NAME);
3300
3301             S_SET_SEGMENT (sym, realregsec);
3302           }
3303       }
3304 }
3305
3306 /* This is the expansion of LABELS_WITHOUT_COLONS.
3307    We let md_start_line_hook tweak label_without_colon_this_line, and then
3308    this function returns the tweaked value, and sets it to 1 for the next
3309    line.  FIXME: Very, very brittle.  Not sure it works the way I
3310    thought at the time I first wrote this.  */
3311
3312 int
3313 mmix_label_without_colon_this_line ()
3314 {
3315   int retval = label_without_colon_this_line;
3316
3317   if (! mmix_gnu_syntax)
3318     label_without_colon_this_line = 1;
3319
3320   return retval;
3321 }
3322
3323 /* This is the expansion of md_relax_frag.  We go through the ordinary
3324    relax table function except when the frag is for a GREG.  Then we have
3325    to check whether there's another GREG by the same value that we can
3326    join with.  */
3327
3328 long
3329 mmix_md_relax_frag (seg, fragP, stretch)
3330      segT seg;
3331      fragS *fragP;
3332      long stretch;
3333 {
3334   if (fragP->fr_subtype != STATE_GREG_DEF
3335       && fragP->fr_subtype != STATE_GREG_UNDF)
3336     return relax_frag (seg, fragP, stretch);
3337
3338   /* If we're defined, we don't grow.  */
3339   if (fragP->fr_subtype == STATE_GREG_DEF)
3340     return 0;
3341
3342   as_fatal (_("internal: unexpected relax type %d:%d"),
3343             fragP->fr_type, fragP->fr_subtype);
3344   return 0;
3345 }
3346
3347 /* Various things we punt until all input is seen.  */
3348
3349 void
3350 mmix_md_end ()
3351 {
3352   fragS *fragP;
3353   symbolS *mainsym;
3354   int i;
3355
3356   /* The first frag of GREG:s going into the register contents section.  */
3357   fragS *mmix_reg_contents_frags = NULL;
3358
3359   /* Reset prefix.  All labels reachable at this point must be
3360      canonicalized.  */
3361   mmix_current_prefix = NULL;
3362
3363   if (doing_bspec)
3364     as_bad_where (bspec_file, bspec_line, _("BSPEC without ESPEC."));
3365
3366   /* Emit the low LOC setting of .text.  */
3367   if (text_has_contents && lowest_text_loc != (bfd_vma) -1)
3368     {
3369       symbolS *symbolP;
3370       char locsymbol[sizeof (":") - 1
3371                     + sizeof (MMIX_LOC_SECTION_START_SYMBOL_PREFIX) - 1
3372                     + sizeof (".text")];
3373
3374       /* An exercise in non-ISO-C-ness, this one.  */
3375       sprintf (locsymbol, ":%s%s", MMIX_LOC_SECTION_START_SYMBOL_PREFIX,
3376                ".text");
3377       symbolP
3378         = symbol_new (locsymbol, absolute_section, lowest_text_loc,
3379                       &zero_address_frag);
3380       S_SET_EXTERNAL (symbolP);
3381     }
3382
3383   /* Ditto .data.  */
3384   if (data_has_contents && lowest_data_loc != (bfd_vma) -1)
3385     {
3386       symbolS *symbolP;
3387       char locsymbol[sizeof (":") - 1
3388                     + sizeof (MMIX_LOC_SECTION_START_SYMBOL_PREFIX) - 1
3389                     + sizeof (".data")];
3390
3391       sprintf (locsymbol, ":%s%s", MMIX_LOC_SECTION_START_SYMBOL_PREFIX,
3392                ".data");
3393       symbolP
3394         = symbol_new (locsymbol, absolute_section, lowest_data_loc,
3395                       &zero_address_frag);
3396       S_SET_EXTERNAL (symbolP);
3397     }
3398
3399   /* Unless GNU syntax mode, set "Main" to be a function, so the
3400      disassembler doesn't get confused when we write truly
3401      mmixal-compatible code (and don't use .type).  Similarly set it
3402      global (regardless of -globalize-symbols), so the linker sees it as
3403      the start symbol in ELF mode.  */
3404   mainsym = symbol_find (MMIX_START_SYMBOL_NAME);
3405   if (mainsym != NULL && ! mmix_gnu_syntax)
3406     {
3407       symbol_get_bfdsym (mainsym)->flags |= BSF_FUNCTION;
3408       S_SET_EXTERNAL (mainsym);
3409     }
3410
3411   if (n_of_raw_gregs != 0)
3412     {
3413       /* Emit GREGs.  They are collected in order of appearance, but must
3414          be emitted in opposite order to both have section address regno*8
3415          and the same allocation order (within a file) as mmixal.  */
3416       segT this_segment = now_seg;
3417       subsegT this_subsegment = now_subseg;
3418       asection *regsec
3419         = bfd_make_section_old_way (stdoutput,
3420                                     MMIX_REG_CONTENTS_SECTION_NAME);
3421       subseg_set (regsec, 0);
3422
3423       /* Finally emit the initialization-value.  Emit a variable frag, which
3424          we'll fix in md_estimate_size_before_relax.  We set the initializer
3425          for the tc_frag_data field to NULL, so we can use that field for
3426          relaxation purposes.  */
3427       mmix_opcode_frag = NULL;
3428
3429       frag_grow (0);
3430       mmix_reg_contents_frags = frag_now;
3431
3432       for (i = n_of_raw_gregs - 1; i >= 0; i--)
3433         {
3434           if (mmix_raw_gregs[i].label != NULL)
3435             /* There's a symbol.  Let it refer to this location in the
3436                register contents section.  The symbol must be globalized
3437                separately.  */
3438             colon (mmix_raw_gregs[i].label);
3439
3440           frag_var (rs_machine_dependent, 8, 0, STATE_GREG_UNDF,
3441                     make_expr_symbol (&mmix_raw_gregs[i].exp), 0, NULL);
3442         }
3443
3444       subseg_set (this_segment, this_subsegment);
3445     }
3446
3447   /* Iterate over frags resulting from GREGs and move those that evidently
3448      have the same value together and point one to another.
3449
3450      This works in time O(N^2) but since the upper bound for non-error use
3451      is 223, it's best to keep this simpler algorithm.  */
3452   for (fragP = mmix_reg_contents_frags; fragP != NULL; fragP = fragP->fr_next)
3453     {
3454       fragS **fpp;
3455       fragS *fp = NULL;
3456       fragS *osymfrag;
3457       offsetT osymval;
3458       expressionS *oexpP;
3459       symbolS *symbolP = fragP->fr_symbol;
3460
3461       if (fragP->fr_type != rs_machine_dependent
3462           || fragP->fr_subtype != STATE_GREG_UNDF)
3463         continue;
3464
3465       /* Whatever the outcome, we will have this GREG judged merged or
3466          non-merged.  Since the tc_frag_data is NULL at this point, we
3467          default to non-merged.  */
3468       fragP->fr_subtype = STATE_GREG_DEF;
3469
3470       /* If we're not supposed to merge GREG definitions, then just don't
3471          look for equivalents.  */
3472       if (! merge_gregs)
3473         continue;
3474
3475       osymval = (offsetT) S_GET_VALUE (symbolP);
3476       osymfrag = symbol_get_frag (symbolP);
3477
3478       /* If the symbol isn't defined, we can't say that another symbol
3479          equals this frag, then.  FIXME: We can look at the "deepest"
3480          defined name; if a = c and b = c then obviously a == b.  */
3481       if (! S_IS_DEFINED (symbolP))
3482         continue;
3483
3484       oexpP = symbol_get_value_expression (fragP->fr_symbol);
3485
3486       /* If the initialization value is zero, then we must not merge them.  */
3487       if (oexpP->X_op == O_constant && osymval == 0)
3488         continue;
3489
3490       /* Iterate through the frags downward this one.  If we find one that
3491          has the same non-zero value, move it to after this one and point
3492          to it as the equivalent.  */
3493       for (fpp = &fragP->fr_next; *fpp != NULL; fpp = &fpp[0]->fr_next)
3494         {
3495           fp = *fpp;
3496
3497           if (fp->fr_type != rs_machine_dependent
3498               || fp->fr_subtype != STATE_GREG_UNDF)
3499             continue;
3500
3501           /* Calling S_GET_VALUE may simplify the symbol, changing from
3502              expr_section etc. so call it first.  */
3503           if ((offsetT) S_GET_VALUE (fp->fr_symbol) == osymval
3504               && symbol_get_frag (fp->fr_symbol) == osymfrag)
3505             {
3506               /* Move the frag links so the one we found equivalent comes
3507                  after the current one, carefully considering that
3508                  sometimes fpp == &fragP->fr_next and the moves must be a
3509                  NOP then.  */
3510               *fpp = fp->fr_next;
3511               fp->fr_next = fragP->fr_next;
3512               fragP->fr_next = fp;
3513               break;
3514             }
3515         }
3516
3517       if (*fpp != NULL)
3518         fragP->tc_frag_data = fp;
3519     }
3520 }
3521
3522 /* qsort function for mmix_symbol_gregs.  */
3523
3524 static int
3525 cmp_greg_symbol_fixes (parg, qarg)
3526      const PTR parg;
3527      const PTR qarg;
3528 {
3529   const struct mmix_symbol_greg_fixes *p
3530     = (const struct mmix_symbol_greg_fixes *) parg;
3531   const struct mmix_symbol_greg_fixes *q
3532     = (const struct mmix_symbol_greg_fixes *) qarg;
3533
3534   return p->offs > q->offs ? 1 : p->offs < q->offs ? -1 : 0;
3535 }
3536
3537 /* Collect GREG definitions from mmix_gregs and hang them as lists sorted
3538    on increasing offsets onto each section symbol or undefined symbol.
3539
3540    Also, remove the register convenience section so it doesn't get output
3541    as an ELF section.  */
3542
3543 void
3544 mmix_frob_file ()
3545 {
3546   int i;
3547   struct mmix_symbol_gregs *all_greg_symbols[MAX_GREGS];
3548   int n_greg_symbols = 0;
3549
3550   /* Collect all greg fixups and decorate each corresponding symbol with
3551      the greg fixups for it.  */
3552   for (i = 0; i < n_of_cooked_gregs; i++)
3553     {
3554       offsetT offs;
3555       symbolS *sym;
3556       struct mmix_symbol_gregs *gregs;
3557       fixS *fixP;
3558
3559       fixP = mmix_gregs[i];
3560       know (fixP->fx_r_type == BFD_RELOC_64);
3561
3562       /* This case isn't doable in general anyway, methinks.  */
3563       if (fixP->fx_subsy != NULL)
3564         {
3565           as_bad_where (fixP->fx_file, fixP->fx_line,
3566                         _("GREG expression too complicated"));
3567           continue;
3568         }
3569
3570       sym = fixP->fx_addsy;
3571       offs = (offsetT) fixP->fx_offset;
3572
3573       /* If the symbol is defined, then it must be resolved to a section
3574          symbol at this time, or else we don't know how to handle it.  */
3575       if (S_IS_DEFINED (sym))
3576         {
3577           if (! symbol_section_p (sym)
3578               && ! bfd_is_abs_section (S_GET_SEGMENT (sym)))
3579             as_fatal (_("internal: GREG expression not resolved to section"));
3580
3581           offs += S_GET_VALUE (sym);
3582         }
3583
3584       /* If this is an absolute symbol sufficiently near lowest_data_loc,
3585          then we canonicalize on the data section.  Note that offs is
3586          signed here; we may subtract lowest_data_loc which is unsigned.
3587          Careful with those comparisons.  */
3588       if (lowest_data_loc != (bfd_vma) -1
3589           && (bfd_vma) offs + 256 > lowest_data_loc
3590           && bfd_is_abs_section (S_GET_SEGMENT (sym)))
3591         {
3592           offs -= (offsetT) lowest_data_loc;
3593           sym = section_symbol (data_section);
3594         }
3595       /* Likewise text section.  */
3596       else if (lowest_text_loc != (bfd_vma) -1
3597                && (bfd_vma) offs + 256 > lowest_text_loc
3598                && bfd_is_abs_section (S_GET_SEGMENT (sym)))
3599         {
3600           offs -= (offsetT) lowest_text_loc;
3601           sym = section_symbol (text_section);
3602         }
3603
3604       gregs = *symbol_get_tc (sym);
3605
3606       if (gregs == NULL)
3607         {
3608           gregs = xmalloc (sizeof (*gregs));
3609           gregs->n_gregs = 0;
3610           symbol_set_tc (sym, &gregs);
3611           all_greg_symbols[n_greg_symbols++] = gregs;
3612         }
3613
3614       gregs->greg_fixes[gregs->n_gregs].fix = fixP;
3615       gregs->greg_fixes[gregs->n_gregs++].offs = offs;
3616     }
3617
3618   /* For each symbol having a GREG definition, sort those definitions on
3619      offset.  */
3620   for (i = 0; i < n_greg_symbols; i++)
3621     qsort (all_greg_symbols[i]->greg_fixes, all_greg_symbols[i]->n_gregs,
3622            sizeof (all_greg_symbols[i]->greg_fixes[0]), cmp_greg_symbol_fixes);
3623
3624   if (real_reg_section != NULL)
3625     {
3626       asection **secpp;
3627
3628       /* FIXME: Pass error state gracefully.  */
3629       if (bfd_get_section_flags (stdoutput, real_reg_section) & SEC_HAS_CONTENTS)
3630         as_fatal (_("register section has contents\n"));
3631
3632       /* FIXME: This does not seem like the proper way to kill a section,
3633          but it's the way it's done elsewhere, like elf64-alpha.c.  */
3634       /* Really remove the section.  */
3635       for (secpp = &stdoutput->sections;
3636            *secpp != real_reg_section;
3637            secpp = &(*secpp)->next)
3638         ;
3639       *secpp = (*secpp)->next;
3640       --stdoutput->section_count;
3641     }
3642
3643 }
3644
3645 /* Provide an expression for a built-in name provided when-used.
3646    Either a symbol that is a handler; living in 0x10*[1..8] and having
3647    name [DVWIOUZX]_Handler, or a mmixal built-in symbol.
3648
3649    If the name isn't a built-in name and parsed into *EXPP, return zero.  */
3650
3651 int
3652 mmix_parse_predefined_name (name, expP)
3653      char *name;
3654      expressionS *expP;
3655 {
3656   char *canon_name;
3657   char *handler_charp;
3658   const char handler_chars[] = "DVWIOUZX";
3659   symbolS *symp;
3660
3661   if (! predefined_syms)
3662     return 0;
3663
3664   canon_name = tc_canonicalize_symbol_name (name);
3665
3666   if (canon_name[1] == '_'
3667       && strcmp (canon_name + 2, "Handler") == 0
3668       && (handler_charp = strchr (handler_chars, *canon_name)) != NULL)
3669     {
3670       /* If the symbol doesn't exist, provide one relative to the .text
3671          section.
3672
3673          FIXME: We should provide separate sections, mapped in the linker
3674          script.  */
3675       symp = symbol_find (name);
3676       if (symp == NULL)
3677         symp = symbol_new (name, text_section,
3678                            0x10 * (handler_charp + 1 - handler_chars),
3679                            &zero_address_frag);
3680     }
3681   else
3682     {
3683       /* These symbols appear when referenced; needed for
3684          mmixal-compatible programs.  */
3685       unsigned int i;
3686
3687       static const struct
3688       {
3689         const char *name;
3690         valueT val;
3691       } predefined_abs_syms[] =
3692         {
3693           {"Data_Segment", (valueT) 0x20 << 56},
3694           {"Pool_Segment", (valueT) 0x40 << 56},
3695           {"Stack_Segment", (valueT) 0x60 << 56},
3696           {"StdIn", 0},
3697           {"StdOut", 1},
3698           {"StdErr", 2},
3699           {"TextRead", 0},
3700           {"TextWrite", 1},
3701           {"BinaryRead", 2},
3702           {"BinaryWrite", 3},
3703           {"BinaryReadWrite", 4},
3704           {"Halt", 0},
3705           {"Fopen", 1},
3706           {"Fclose", 2},
3707           {"Fread", 3},
3708           {"Fgets", 4},
3709           {"Fgetws", 5},
3710           {"Fwrite", 6},
3711           {"Fputs", 7},
3712           {"Fputws", 8},
3713           {"Fseek", 9},
3714           {"Ftell", 10},
3715           {"D_BIT", 0x80},
3716           {"V_BIT", 0x40},
3717           {"W_BIT", 0x20},
3718           {"I_BIT", 0x10},
3719           {"O_BIT", 0x08},
3720           {"U_BIT", 0x04},
3721           {"Z_BIT", 0x02},
3722           {"X_BIT", 0x01},
3723           {"Inf", 0x7ff00000}
3724         };
3725
3726       /* If it's already in the symbol table, we shouldn't do anything.  */
3727       symp = symbol_find (name);
3728       if (symp != NULL)
3729         return 0;
3730
3731       for (i = 0;
3732            i < sizeof (predefined_abs_syms)/sizeof (predefined_abs_syms[0]);
3733            i++)
3734         if (strcmp (canon_name, predefined_abs_syms[i].name) == 0)
3735           {
3736             symbol_table_insert (symbol_new (predefined_abs_syms[i].name,
3737                                              absolute_section,
3738                                              predefined_abs_syms[i].val,
3739                                              &zero_address_frag));
3740
3741             /* Let gas find the symbol we just created, through its
3742                ordinary lookup.  */
3743             return 0;
3744           }
3745
3746       /* Not one of those symbols.  Let gas handle it.  */
3747       return 0;
3748     }
3749
3750   expP->X_op = O_symbol;
3751   expP->X_add_number = 0;
3752   expP->X_add_symbol = symp;
3753   expP->X_op_symbol = NULL;
3754
3755   return 1;
3756 }
3757
3758 /* Worker for mmix_frob_file_before_adjust.  */
3759
3760 static void
3761 mmix_frob_local_reloc (abfd, sec, xxx)
3762      bfd *abfd ATTRIBUTE_UNUSED;
3763      asection *sec;
3764      PTR xxx ATTRIBUTE_UNUSED;
3765 {
3766   segment_info_type *seginfo = seg_info (sec);
3767   fixS *fixp;
3768
3769   if (seginfo == NULL)
3770     return;
3771
3772   for (fixp = seginfo->fix_root; fixp; fixp = fixp->fx_next)
3773     if (! fixp->fx_done && fixp->fx_addsy != NULL)
3774       {
3775         symbolS *sym = fixp->fx_addsy;
3776         asection *section = S_GET_SEGMENT (sym);
3777
3778         if (section == reg_section
3779             && fixp->fx_r_type == BFD_RELOC_MMIX_LOCAL)
3780           {
3781             /* If the register is marked global, we don't need to replace
3782                with the *real* register section since that will be done
3783                when the symbol is changed.  */
3784             if (! S_IS_EXTERNAL (sym))
3785               /* If it's a local symbol, we replace it with an anonymous
3786                  one with the same constant value.  */
3787               fixp->fx_addsy = expr_build_uconstant (S_GET_VALUE (sym));
3788           }
3789       }
3790 }
3791
3792 /* Change fixups for register symbols for BFD_MMIX_LOCAL to be for an
3793    absolute symbol.  */
3794
3795 void
3796 mmix_frob_file_before_adjust ()
3797 {
3798   return;
3799   bfd_map_over_sections (stdoutput, mmix_frob_local_reloc, (char *) 0);
3800 }
3801
3802 /* Just check that we don't have a BSPEC/ESPEC pair active when changing
3803    sections "normally", and get knowledge about alignment from the new
3804    section.  */
3805
3806 void
3807 mmix_md_elf_section_change_hook ()
3808 {
3809   if (doing_bspec)
3810     as_bad (_("section change from within a BSPEC/ESPEC pair is not supported"));
3811
3812   last_alignment = bfd_get_section_alignment (now_seg->owner, now_seg);
3813   want_unaligned = 0;
3814 }
3815
3816 /* The LOC worker.  This is like s_org, but we have to support changing
3817    section too.   */
3818
3819 static void
3820 s_loc (ignore)
3821      int ignore ATTRIBUTE_UNUSED;
3822 {
3823   segT section;
3824   expressionS exp;
3825   char *p;
3826   symbolS *sym;
3827   offsetT off;
3828
3829   /* Must not have a BSPEC in progress. */
3830   if (doing_bspec)
3831     {
3832       as_bad (_("directive LOC from within a BSPEC/ESPEC pair is not supported"));
3833       return;
3834     }
3835
3836   section = expression (&exp);
3837
3838   if (exp.X_op == O_illegal
3839       || exp.X_op == O_absent
3840       || exp.X_op == O_big
3841       || section == undefined_section)
3842     {
3843       as_bad (_("invalid LOC expression"));
3844       return;
3845     }
3846
3847   if (section == absolute_section)
3848     {
3849       /* Translate a constant into a suitable section.  */
3850
3851       if (exp.X_add_number < ((offsetT) 0x20 << 56))
3852         {
3853           /* Lower than Data_Segment - assume it's .text.  */
3854           section = text_section;
3855
3856           /* Save the lowest seen location, so we can pass on this
3857              information to the linker.  We don't actually org to this
3858              location here, we just pass on information to the linker so
3859              it can put the code there for us.  */
3860
3861           /* If there was already a loc (that has to be set lower than
3862              this one), we org at (this - lower).  There's an implicit
3863              "LOC 0" before any entered code.  FIXME: handled by spurious
3864              settings of text_has_contents.  */
3865           if (exp.X_add_number < 0
3866               || exp.X_add_number < (offsetT) lowest_text_loc)
3867             {
3868               as_bad (_("LOC expression stepping backwards is not supported"));
3869               exp.X_op = O_absent;
3870             }
3871           else
3872             {
3873               if (text_has_contents && lowest_text_loc == (bfd_vma) -1)
3874                 lowest_text_loc = 0;
3875
3876               if (lowest_text_loc == (bfd_vma) -1)
3877                 {
3878                   lowest_text_loc = exp.X_add_number;
3879
3880                   /* We want only to change the section, not set an offset.  */
3881                   exp.X_op = O_absent;
3882                 }
3883               else
3884                 exp.X_add_number -= lowest_text_loc;
3885             }
3886         }
3887       else
3888         {
3889           /* Do the same for the .data section.  */
3890           section = data_section;
3891
3892           if (exp.X_add_number < (offsetT) lowest_data_loc)
3893             {
3894               as_bad (_("LOC expression stepping backwards is not supported"));
3895               exp.X_op = O_absent;
3896             }
3897           else
3898             {
3899               if (data_has_contents && lowest_data_loc == (bfd_vma) -1)
3900                 lowest_data_loc = (bfd_vma) 0x20 << 56;
3901
3902               if (lowest_data_loc == (bfd_vma) -1)
3903                 {
3904                   lowest_data_loc = exp.X_add_number;
3905
3906                   /* We want only to change the section, not set an offset.  */
3907                   exp.X_op = O_absent;
3908                 }
3909               else
3910                 exp.X_add_number -= lowest_data_loc;
3911             }
3912         }
3913     }
3914
3915   if (section != now_seg)
3916     {
3917       obj_elf_section_change_hook ();
3918       subseg_set (section, 0);
3919
3920       /* Call our section change hooks using the official hook.  */
3921       md_elf_section_change_hook ();
3922     }
3923
3924   if (exp.X_op != O_absent)
3925     {
3926       if (exp.X_op != O_constant && exp.X_op != O_symbol)
3927         {
3928           /* Handle complex expressions.  */
3929           sym = make_expr_symbol (&exp);
3930           off = 0;
3931         }
3932       else
3933         {
3934           sym = exp.X_add_symbol;
3935           off = exp.X_add_number;
3936         }
3937
3938       p = frag_var (rs_org, 1, 1, (relax_substateT) 0, sym, off, (char *) 0);
3939       *p = 0;
3940     }
3941
3942   mmix_handle_rest_of_empty_line ();
3943 }
3944
3945 /* The BYTE worker.  We have to support sequences of mixed "strings",
3946    numbers and other constant "first-pass" reducible expressions separated
3947    by comma.  */
3948
3949 static void
3950 mmix_byte ()
3951 {
3952   unsigned int c;
3953   char *start;
3954
3955   if (now_seg == text_section)
3956     text_has_contents = 1;
3957   else if (now_seg == data_section)
3958     data_has_contents = 1;
3959
3960   do
3961     {
3962       SKIP_WHITESPACE ();
3963       switch (*input_line_pointer)
3964         {
3965         case '\"':
3966           ++input_line_pointer;
3967           start = input_line_pointer;
3968           while (is_a_char (c = next_char_of_string ()))
3969             {
3970               FRAG_APPEND_1_CHAR (c);
3971             }
3972
3973           if (input_line_pointer[-1] != '\"')
3974             {
3975               /* We will only get here in rare cases involving #NO_APP,
3976                  where the unterminated string is not recognized by the
3977                  preformatting pass.  */
3978               as_bad (_("unterminated string"));
3979               mmix_discard_rest_of_line ();
3980               return;
3981             }
3982           break;
3983
3984         default:
3985           {
3986             expressionS exp;
3987             segT expseg = expression (&exp);
3988
3989             /* We have to allow special register names as constant numbers.  */
3990             if ((expseg != absolute_section && expseg != reg_section)
3991                 || (exp.X_op != O_constant
3992                     && (exp.X_op != O_register
3993                         || exp.X_add_number <= 255)))
3994               {
3995                 as_bad (_("BYTE expression not a pure number"));
3996                 mmix_discard_rest_of_line ();
3997                 return;
3998               }
3999             else if ((exp.X_add_number > 255 && exp.X_op != O_register)
4000                      || exp.X_add_number < 0)
4001               {
4002                 /* Note that mmixal does not allow negative numbers in
4003                    BYTE sequences, so neither should we.  */
4004                 as_bad (_("BYTE expression not in the range 0..255"));
4005                 mmix_discard_rest_of_line ();
4006                 return;
4007               }
4008
4009             FRAG_APPEND_1_CHAR (exp.X_add_number);
4010           }
4011           break;
4012         }
4013
4014       SKIP_WHITESPACE ();
4015       c = *input_line_pointer++;
4016     }
4017   while (c == ',');
4018
4019   input_line_pointer--;
4020
4021   if (mmix_gnu_syntax)
4022     demand_empty_rest_of_line ();
4023   else
4024     {
4025       mmix_discard_rest_of_line ();
4026       /* Do like demand_empty_rest_of_line and step over the end-of-line
4027          boundary.  */
4028       input_line_pointer++;
4029     }
4030
4031   /* Make sure we align for the next instruction.  */
4032   last_alignment = 0;
4033 }
4034
4035 /* Like cons_worker, but we have to ignore "naked comments", not barf on
4036    them.  Implements WYDE, TETRA and OCTA.  We're a little bit more
4037    lenient than mmix_byte but FIXME: they should eventually merge.  */
4038
4039 static void
4040 mmix_cons (nbytes)
4041      int nbytes;
4042 {
4043   expressionS exp;
4044   char *start;
4045
4046   /* If we don't have any contents, then it's ok to have a specified start
4047      address that is not a multiple of the max data size.  We will then
4048      align it as necessary when we get here.  Otherwise, it's a fatal sin.  */
4049   if (now_seg == text_section)
4050     {
4051       if (lowest_text_loc != (bfd_vma) -1
4052           && (lowest_text_loc & (nbytes - 1)) != 0)
4053         {
4054           if (text_has_contents)
4055             as_bad (_("data item with alignment larger than location"));
4056           else if (want_unaligned)
4057             as_bad (_("unaligned data at an absolute location is not supported"));
4058
4059           lowest_text_loc &= ~((bfd_vma) nbytes - 1);
4060           lowest_text_loc += (bfd_vma) nbytes;
4061         }
4062
4063       text_has_contents = 1;
4064     }
4065   else if (now_seg == data_section)
4066     {
4067       if (lowest_data_loc != (bfd_vma) -1
4068           && (lowest_data_loc & (nbytes - 1)) != 0)
4069         {
4070           if (data_has_contents)
4071             as_bad (_("data item with alignment larger than location"));
4072           else if (want_unaligned)
4073             as_bad (_("unaligned data at an absolute location is not supported"));
4074
4075           lowest_data_loc &= ~((bfd_vma) nbytes - 1);
4076           lowest_data_loc += (bfd_vma) nbytes;
4077         }
4078
4079       data_has_contents = 1;
4080     }
4081
4082   /* Always align these unless asked not to (valid for the current pseudo).  */
4083   if (! want_unaligned)
4084     {
4085       last_alignment = nbytes == 2 ? 1 : (nbytes == 4 ? 2 : 3);
4086       frag_align (last_alignment, 0, 0);
4087       record_alignment (now_seg, last_alignment);
4088     }
4089
4090   /* For mmixal compatibility, a label for an instruction (and emitting
4091      pseudo) refers to the _aligned_ address.  So we have to emit the
4092      label here.  */
4093   if (current_fb_label >= 0)
4094     colon (fb_label_name (current_fb_label, 1));
4095   else if (pending_label != NULL)
4096     {
4097       colon (pending_label);
4098       pending_label = NULL;
4099     }
4100
4101   SKIP_WHITESPACE ();
4102
4103   if (is_end_of_line [(unsigned int) *input_line_pointer])
4104     {
4105       /* Default to zero if the expression was absent.  */
4106
4107       exp.X_op = O_constant;
4108       exp.X_add_number = 0;
4109       exp.X_unsigned = 0;
4110       exp.X_add_symbol = NULL;
4111       exp.X_op_symbol = NULL;
4112       emit_expr (&exp, (unsigned int) nbytes);
4113     }
4114   else
4115     do
4116       {
4117         unsigned int c;
4118
4119         switch (*input_line_pointer)
4120           {
4121             /* We support strings here too; each character takes up nbytes
4122                bytes.  */
4123           case '\"':
4124             ++input_line_pointer;
4125             start = input_line_pointer;
4126             while (is_a_char (c = next_char_of_string ()))
4127               {
4128                 exp.X_op = O_constant;
4129                 exp.X_add_number = c;
4130                 exp.X_unsigned = 1;
4131                 emit_expr (&exp, (unsigned int) nbytes);
4132               }
4133
4134             if (input_line_pointer[-1] != '\"')
4135               {
4136                 /* We will only get here in rare cases involving #NO_APP,
4137                    where the unterminated string is not recognized by the
4138                    preformatting pass.  */
4139                 as_bad (_("unterminated string"));
4140                 mmix_discard_rest_of_line ();
4141                 return;
4142               }
4143             break;
4144
4145           default:
4146             {
4147               expression (&exp);
4148               emit_expr (&exp, (unsigned int) nbytes);
4149               SKIP_WHITESPACE ();
4150             }
4151             break;
4152           }
4153       }
4154     while (*input_line_pointer++ == ',');
4155
4156   input_line_pointer--;         /* Put terminator back into stream.  */
4157
4158   mmix_handle_rest_of_empty_line ();
4159
4160   /* We don't need to step up the counter for the current_fb_label here;
4161      that's handled by the caller.  */
4162 }
4163
4164 /* The md_do_align worker.  At present, we just record an alignment to
4165    nullify the automatic alignment we do for WYDE, TETRA and OCTA, as gcc
4166    does not use the unaligned macros when attribute packed is used.
4167    Arguably this is a GCC bug.  */
4168
4169 void
4170 mmix_md_do_align (n, fill, len, max)
4171      int n;
4172      char *fill ATTRIBUTE_UNUSED;
4173      int len ATTRIBUTE_UNUSED;
4174      int max ATTRIBUTE_UNUSED;
4175 {
4176   last_alignment = n;
4177   want_unaligned = n == 0;
4178 }