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