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