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