OpenCores
URL https://opencores.org/ocsvn/scarts/scarts/trunk

Subversion Repositories scarts

[/] [scarts/] [trunk/] [toolchain/] [scarts-binutils/] [binutils-2.19.1/] [gas/] [config/] [tc-mmix.c] - Blame information for rev 6

Details | Compare with Previous | View Log

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

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.