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