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