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