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