1 /* tc-mmix.c -- Assembler for Don Knuth's MMIX.
2 Copyright (C) 2001, 2002 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)
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, 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, 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. */
34 #include "opcode/mmix.h"
35 #include "safe-ctype.h"
36 #include "dwarf2dbg.h"
39 /* Something to describe what we need to do with a fixup before output,
40 for example assert something of what it became or make a relocation. */
42 enum mmix_fixup_action
46 mmix_fixup_register_or_adjust_for_byte
49 static int get_spec_regno PARAMS ((char *));
50 static int get_operands PARAMS ((int, char *, expressionS[]));
51 static int get_putget_operands
52 PARAMS ((struct mmix_opcode *, char *, expressionS[]));
53 static void s_prefix PARAMS ((int));
54 static void s_greg PARAMS ((int));
55 static void s_loc PARAMS ((int));
56 static void s_bspec PARAMS ((int));
57 static void s_espec PARAMS ((int));
58 static void mmix_s_local PARAMS ((int));
59 static void mmix_greg_internal PARAMS ((char *));
60 static void mmix_set_geta_branch_offset PARAMS ((char *, offsetT value));
61 static void mmix_set_jmp_offset PARAMS ((char *, offsetT));
62 static void mmix_fill_nops PARAMS ((char *, int));
63 static int cmp_greg_symbol_fixes PARAMS ((const PTR, const PTR));
64 static int cmp_greg_val_greg_symbol_fixes
65 PARAMS ((const PTR p1, const PTR p2));
66 static void mmix_handle_rest_of_empty_line PARAMS ((void));
67 static void mmix_discard_rest_of_line PARAMS ((void));
68 static void mmix_byte PARAMS ((void));
69 static void mmix_cons PARAMS ((int));
71 /* Continue the tradition of symbols.c; use control characters to enforce
72 magic. These are used when replacing e.g. 8F and 8B so we can handle
73 such labels correctly with the common parser hooks. */
74 #define MAGIC_FB_BACKWARD_CHAR '\003'
75 #define MAGIC_FB_FORWARD_CHAR '\004'
77 /* Copy the location of a frag to a fix. */
78 #define COPY_FR_WHERE_TO_FX(FRAG, FIX) \
81 (FIX)->fx_file = (FRAG)->fr_file; \
82 (FIX)->fx_line = (FRAG)->fr_line; \
86 const char *md_shortopts = "x";
87 static int current_fb_label = -1;
88 static char *pending_label = NULL;
90 static bfd_vma lowest_text_loc = (bfd_vma) -1;
91 static int text_has_contents = 0;
93 /* The alignment of the previous instruction, and a boolean for whether we
94 want to avoid aligning the next WYDE, TETRA, OCTA or insn. */
95 static int last_alignment = 0;
96 static int want_unaligned = 0;
98 static bfd_vma lowest_data_loc = (bfd_vma) -1;
99 static int data_has_contents = 0;
101 /* The fragS of the instruction being assembled. Only valid from within
103 fragS *mmix_opcode_frag = NULL;
105 /* Raw GREGs as appearing in input. These may be fewer than the number
107 static int n_of_raw_gregs = 0;
112 } mmix_raw_gregs[MAX_GREGS];
114 /* Fixups for all unique GREG registers. We store the fixups here in
115 md_convert_frag, then we use the array to convert
116 BFD_RELOC_MMIX_BASE_PLUS_OFFSET fixups in tc_gen_reloc. The index is
117 just a running number and is not supposed to be correlated to a
119 static fixS *mmix_gregs[MAX_GREGS];
120 static int n_of_cooked_gregs = 0;
122 /* Pointing to the register section we use for output. */
123 static asection *real_reg_section;
125 /* For each symbol; unknown or section symbol, we keep a list of GREG
126 definitions sorted on increasing offset. It seems no use keeping count
127 to allocate less room than the maximum number of gregs when we've found
128 one for a section or symbol. */
129 struct mmix_symbol_gregs
132 struct mmix_symbol_greg_fixes
136 /* A signed type, since we may have GREGs pointing slightly before the
137 contents of a section. */
139 } greg_fixes[MAX_GREGS];
142 /* Should read insert a colon on something that starts in column 0 on
144 static int label_without_colon_this_line = 1;
146 /* Should we expand operands for external symbols? */
147 static int expand_op = 1;
149 /* Should we warn when expanding operands? FIXME: test-cases for when -x
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 /* Do we know that the next semicolon is at the end of the operands field
174 (in mmixal mode; constant 1 in GNU mode)? */
175 int mmix_next_semicolon_is_eoln = 1;
177 /* Do we have a BSPEC in progress? */
178 static int doing_bspec = 0;
179 static char *bspec_file;
180 static unsigned int bspec_line;
182 struct option md_longopts[] =
184 #define OPTION_RELAX (OPTION_MD_BASE)
185 #define OPTION_NOEXPAND (OPTION_RELAX + 1)
186 #define OPTION_NOMERGEGREG (OPTION_NOEXPAND + 1)
187 #define OPTION_NOSYMS (OPTION_NOMERGEGREG + 1)
188 #define OPTION_GNU_SYNTAX (OPTION_NOSYMS + 1)
189 #define OPTION_GLOBALIZE_SYMBOLS (OPTION_GNU_SYNTAX + 1)
190 #define OPTION_FIXED_SPEC_REGS (OPTION_GLOBALIZE_SYMBOLS + 1)
191 #define OPTION_LINKER_ALLOCATED_GREGS (OPTION_FIXED_SPEC_REGS + 1)
192 {"linkrelax", no_argument, NULL, OPTION_RELAX},
193 {"no-expand", no_argument, NULL, OPTION_NOEXPAND},
194 {"no-merge-gregs", no_argument, NULL, OPTION_NOMERGEGREG},
195 {"no-predefined-syms", no_argument, NULL, OPTION_NOSYMS},
196 {"gnu-syntax", no_argument, NULL, OPTION_GNU_SYNTAX},
197 {"globalize-symbols", no_argument, NULL, OPTION_GLOBALIZE_SYMBOLS},
198 {"fixed-special-register-names", no_argument, NULL,
199 OPTION_FIXED_SPEC_REGS},
200 {"linker-allocated-gregs", no_argument, NULL,
201 OPTION_LINKER_ALLOCATED_GREGS},
202 {NULL, no_argument, NULL, 0}
205 size_t md_longopts_size = sizeof (md_longopts);
207 static struct hash_control *mmix_opcode_hash;
209 /* We use these when implementing the PREFIX pseudo. */
210 char *mmix_current_prefix;
211 struct obstack mmix_sym_obstack;
214 /* For MMIX, we encode the relax_substateT:s (in e.g. fr_substate) as one
215 bit length, and the relax-type shifted on top of that. There seems to
216 be no point in making the relaxation more fine-grained; the linker does
217 that better and we might interfere by changing non-optimal relaxations
218 into other insns that cannot be relaxed as easily.
220 Groups for MMIX relaxing:
223 extra length: zero or three insns.
226 extra length: zero or five insns.
229 extra length: zero or four insns.
232 extra length: zero or four insns. */
234 #define STATE_GETA (1)
235 #define STATE_BCC (2)
236 #define STATE_PUSHJ (3)
237 #define STATE_JMP (4)
238 #define STATE_GREG (5)
240 /* No fine-grainedness here. */
241 #define STATE_LENGTH_MASK (1)
243 #define STATE_ZERO (0)
244 #define STATE_MAX (1)
246 /* More descriptive name for convenience. */
247 /* FIXME: We should start on something different, not MAX. */
248 #define STATE_UNDF STATE_MAX
250 /* FIXME: For GREG, we must have other definitions; UNDF == MAX isn't
251 appropriate; we need it the other way round. This value together with
252 fragP->tc_frag_data shows what state the frag is in: tc_frag_data
253 non-NULL means 0, NULL means 8 bytes. */
254 #define STATE_GREG_UNDF ENCODE_RELAX (STATE_GREG, STATE_ZERO)
255 #define STATE_GREG_DEF ENCODE_RELAX (STATE_GREG, STATE_MAX)
257 /* These displacements are relative to the adress following the opcode
258 word of the instruction. The catch-all states have zero for "reach"
259 and "next" entries. */
261 #define GETA_0F (65536 * 4 - 8)
262 #define GETA_0B (-65536 * 4 - 4)
264 #define GETA_MAX_LEN 4 * 4
268 #define BCC_0F GETA_0F
269 #define BCC_0B GETA_0B
271 #define BCC_MAX_LEN 6 * 4
272 #define BCC_5F GETA_3F
273 #define BCC_5B GETA_3B
275 #define PUSHJ_0F GETA_0F
276 #define PUSHJ_0B GETA_0B
278 #define PUSHJ_MAX_LEN 5 * 4
279 #define PUSHJ_4F GETA_3F
280 #define PUSHJ_4B GETA_3B
282 #define JMP_0F (65536 * 256 * 4 - 8)
283 #define JMP_0B (-65536 * 256 * 4 - 4)
285 #define JMP_MAX_LEN 5 * 4
289 #define RELAX_ENCODE_SHIFT 1
290 #define ENCODE_RELAX(what, length) (((what) << RELAX_ENCODE_SHIFT) + (length))
292 const relax_typeS mmix_relax_table[] =
294 /* Error sentinel (0, 0). */
301 {GETA_0F, GETA_0B, 0, ENCODE_RELAX (STATE_GETA, STATE_MAX)},
305 GETA_MAX_LEN - 4, 0},
308 {BCC_0F, BCC_0B, 0, ENCODE_RELAX (STATE_BCC, STATE_MAX)},
315 {PUSHJ_0F, PUSHJ_0B, 0, ENCODE_RELAX (STATE_PUSHJ, STATE_MAX)},
319 PUSHJ_MAX_LEN - 4, 0},
322 {JMP_0F, JMP_0B, 0, ENCODE_RELAX (STATE_JMP, STATE_MAX)},
328 /* GREG (5, 0), (5, 1), though the table entry isn't used. */
329 {0, 0, 0, 0}, {0, 0, 0, 0}
332 const pseudo_typeS md_pseudo_table[] =
334 /* Support " .greg sym,expr" syntax. */
337 /* Support " .bspec expr" syntax. */
338 {"bspec", s_bspec, 1},
340 /* Support " .espec" syntax. */
341 {"espec", s_espec, 1},
343 /* Support " .local $45" syntax. */
344 {"local", mmix_s_local, 1},
346 /* Support DWARF2 debugging info. */
347 {"file", (void (*) PARAMS ((int))) dwarf2_directive_file, 0},
348 {"loc", dwarf2_directive_loc, 0},
353 const char mmix_comment_chars[] = "%!";
355 /* A ':' is a valid symbol character in mmixal. It's the prefix
356 delimiter, but other than that, it works like a symbol character,
357 except that we strip one off at the beginning of symbols. An '@' is a
358 symbol by itself (for the current location); space around it must not
360 const char mmix_symbol_chars[] = ":@";
362 const char line_comment_chars[] = "*#";
364 const char line_separator_chars[] = ";";
366 const char mmix_exp_chars[] = "eE";
368 const char mmix_flt_chars[] = "rf";
371 /* Fill in the offset-related part of GETA or Bcc. */
374 mmix_set_geta_branch_offset (opcodep, value)
385 md_number_to_chars (opcodep + 2, value, 2);
388 /* Fill in the offset-related part of JMP. */
391 mmix_set_jmp_offset (opcodep, value)
397 value += 65536 * 256 * 4;
402 md_number_to_chars (opcodep + 1, value, 3);
405 /* Fill in NOP:s for the expanded part of GETA/JMP/Bcc/PUSHJ. */
408 mmix_fill_nops (opcodep, n)
414 for (i = 0; i < n; i++)
415 md_number_to_chars (opcodep + i * 4, SWYM_INSN_BYTE << 24, 4);
418 /* See macro md_parse_name in tc-mmix.h. */
421 mmix_current_location (fn, exp)
422 void (*fn) PARAMS ((expressionS *));
430 /* Get up to three operands, filling them into the exp array.
431 General idea and code stolen from the tic80 port. */
434 get_operands (max_operands, s, exp)
443 while (nextchar == ',')
445 /* Skip leading whitespace */
446 while (*p == ' ' || *p == '\t')
449 /* Check to see if we have any operands left to parse */
450 if (*p == 0 || *p == '\n' || *p == '\r')
454 else if (numexp == max_operands)
456 /* This seems more sane than saying "too many operands". We'll
457 get here only if the trailing trash starts with a comma. */
458 as_bad (_("invalid operands"));
459 mmix_discard_rest_of_line ();
463 /* Begin operand parsing at the current scan point. */
465 input_line_pointer = p;
466 expression (&exp[numexp]);
468 if (exp[numexp].X_op == O_illegal)
470 as_bad (_("invalid operands"));
472 else if (exp[numexp].X_op == O_absent)
474 as_bad (_("missing operand"));
478 p = input_line_pointer;
480 /* Skip leading whitespace */
481 while (*p == ' ' || *p == '\t')
486 /* If we allow "naked" comments, ignore the rest of the line. */
489 mmix_handle_rest_of_empty_line ();
490 input_line_pointer--;
493 /* Mark the end of the valid operands with an illegal expression. */
494 exp[numexp].X_op = O_illegal;
499 /* Get the value of a special register, or -1 if the name does not match
500 one. NAME is a null-terminated string. */
503 get_spec_regno (name)
514 /* Well, it's a short array and we'll most often just match the first
516 for (i = 0; mmix_spec_regs[i].name != NULL; i++)
517 if (strcmp (name, mmix_spec_regs[i].name) == 0)
518 return mmix_spec_regs[i].number;
523 /* For GET and PUT, parse the register names "manually", so we don't use
526 get_putget_operands (insn, operands, exp)
527 struct mmix_opcode *insn;
531 expressionS *expp_reg;
532 expressionS *expp_sreg;
534 char *sregend = operands;
539 /* Skip leading whitespace */
540 while (*p == ' ' || *p == '\t')
543 input_line_pointer = p;
545 if (insn->operands == mmix_operands_get)
550 expression (expp_reg);
552 p = input_line_pointer;
554 /* Skip whitespace */
555 while (*p == ' ' || *p == '\t')
562 /* Skip whitespace */
563 while (*p == ' ' || *p == '\t')
566 input_line_pointer = sregp;
567 c = get_symbol_end ();
568 sregend = input_line_pointer;
576 /* Initialize to error state in case we'll never call expression on
578 expp_reg->X_op = O_illegal;
581 c = get_symbol_end ();
582 sregend = p = input_line_pointer;
585 /* Skip whitespace */
586 while (*p == ' ' || *p == '\t')
593 /* Skip whitespace */
594 while (*p == ' ' || *p == '\t')
597 input_line_pointer = p;
598 expression (expp_reg);
603 regno = get_spec_regno (sregp);
606 /* Let the caller issue errors; we've made sure the operands are
608 if (expp_reg->X_op != O_illegal
609 && expp_reg->X_op != O_absent
612 expp_sreg->X_op = O_register;
613 expp_sreg->X_add_number = regno + 256;
619 /* Handle MMIX-specific option. */
622 md_parse_option (c, arg)
624 char *arg ATTRIBUTE_UNUSED;
629 warn_on_expansion = 0;
630 allocate_undefined_gregs_in_linker = 1;
637 case OPTION_NOEXPAND:
641 case OPTION_NOMERGEGREG:
647 equated_spec_regs = 0;
650 case OPTION_GNU_SYNTAX:
652 label_without_colon_this_line = 0;
655 case OPTION_GLOBALIZE_SYMBOLS:
656 mmix_globalize_symbols = 1;
659 case OPTION_FIXED_SPEC_REGS:
660 equated_spec_regs = 0;
663 case OPTION_LINKER_ALLOCATED_GREGS:
664 allocate_undefined_gregs_in_linker = 1;
674 /* Display MMIX-specific help text. */
677 md_show_usage (stream)
680 fprintf (stream, _(" MMIX-specific command line options:\n"));
681 fprintf (stream, _("\
682 -fixed-special-register-names\n\
683 Allow only the original special register names.\n"));
684 fprintf (stream, _("\
685 -globalize-symbols Make all symbols global.\n"));
686 fprintf (stream, _("\
687 -gnu-syntax Turn off mmixal syntax compatibility.\n"));
688 fprintf (stream, _("\
689 -relax Create linker relaxable code.\n"));
690 fprintf (stream, _("\
691 -no-predefined-syms Do not provide mmixal built-in constants.\n\
692 Implies -fixed-special-register-names.\n"));
693 fprintf (stream, _("\
694 -no-expand Do not expand GETA, branches, PUSHJ or JUMP\n\
695 into multiple instructions.\n"));
696 fprintf (stream, _("\
697 -no-merge-gregs Do not merge GREG definitions with nearby values.\n"));
698 fprintf (stream, _("\
699 -linker-allocated-gregs If there's no suitable GREG definition for the\
700 operands of an instruction, let the linker resolve.\n"));
701 fprintf (stream, _("\
702 -x Do not warn when an operand to GETA, a branch,\n\
703 PUSHJ or JUMP is not known to be within range.\n\
704 The linker will catch any errors. Implies\n\
705 -linker-allocated-gregs."));
708 /* Step to end of line, but don't step over the end of the line. */
711 mmix_discard_rest_of_line ()
713 while (*input_line_pointer
714 && (! is_end_of_line[(unsigned char) *input_line_pointer]
715 || TC_EOL_IN_INSN (input_line_pointer)))
716 input_line_pointer++;
719 /* Act as demand_empty_rest_of_line if we're in strict GNU syntax mode,
720 otherwise just ignore the rest of the line (and skip the end-of-line
724 mmix_handle_rest_of_empty_line ()
727 demand_empty_rest_of_line ();
730 mmix_discard_rest_of_line ();
731 input_line_pointer++;
735 /* Initialize GAS MMIX specifics. */
741 const struct mmix_opcode *opcode;
743 /* We assume nobody will use this, so don't allocate any room. */
744 obstack_begin (&mmix_sym_obstack, 0);
746 /* This will break the day the "lex" thingy changes. For now, it's the
747 only way to make ':' part of a name, and a name beginner. */
748 lex_type[':'] = (LEX_NAME | LEX_BEGIN_NAME);
750 mmix_opcode_hash = hash_new ();
753 = bfd_make_section_old_way (stdoutput, MMIX_REG_SECTION_NAME);
755 for (opcode = mmix_opcodes; opcode->name; opcode++)
756 hash_insert (mmix_opcode_hash, opcode->name, (char *) opcode);
758 /* We always insert the ordinary registers 0..255 as registers. */
759 for (i = 0; i < 256; i++)
763 /* Alternatively, we could diddle with '$' and the following number,
764 but keeping the registers as symbols helps keep parsing simple. */
765 sprintf (buf, "$%d", i);
766 symbol_table_insert (symbol_new (buf, reg_section, i,
767 &zero_address_frag));
770 /* Insert mmixal built-in names if allowed. */
773 for (i = 0; mmix_spec_regs[i].name != NULL; i++)
774 symbol_table_insert (symbol_new (mmix_spec_regs[i].name,
776 mmix_spec_regs[i].number + 256,
777 &zero_address_frag));
779 /* FIXME: Perhaps these should be recognized as specials; as field
780 names for those instructions. */
781 symbol_table_insert (symbol_new ("ROUND_CURRENT", reg_section, 512,
782 &zero_address_frag));
783 symbol_table_insert (symbol_new ("ROUND_OFF", reg_section, 512 + 1,
784 &zero_address_frag));
785 symbol_table_insert (symbol_new ("ROUND_UP", reg_section, 512 + 2,
786 &zero_address_frag));
787 symbol_table_insert (symbol_new ("ROUND_DOWN", reg_section, 512 + 3,
788 &zero_address_frag));
789 symbol_table_insert (symbol_new ("ROUND_NEAR", reg_section, 512 + 4,
790 &zero_address_frag));
794 /* Assemble one insn in STR. */
800 char *operands = str;
801 char modified_char = 0;
802 struct mmix_opcode *instruction;
803 fragS *opc_fragP = NULL;
804 int max_operands = 3;
806 /* Note that the struct frag member fr_literal in frags.h is char[], so
807 I have to make this a plain char *. */
808 /* unsigned */ char *opcodep = NULL;
813 /* Move to end of opcode. */
815 is_part_of_name (*operands);
819 if (ISSPACE (*operands))
821 modified_char = *operands;
825 instruction = (struct mmix_opcode *) hash_find (mmix_opcode_hash, str);
826 if (instruction == NULL)
828 as_bad (_("unknown opcode: `%s'"), str);
830 /* Avoid "unhandled label" errors. */
831 pending_label = NULL;
835 /* Put back the character after the opcode. */
836 if (modified_char != 0)
837 operands[-1] = modified_char;
839 input_line_pointer = operands;
841 /* Is this a mmixal pseudodirective? */
842 if (instruction->type == mmix_type_pseudo)
844 /* For mmixal compatibility, a label for an instruction (and
845 emitting pseudo) refers to the _aligned_ address. We emit the
846 label here for the pseudos that don't handle it themselves. When
847 having an fb-label, emit it here, and increment the counter after
849 switch (instruction->operands)
851 case mmix_operands_loc:
852 case mmix_operands_byte:
853 case mmix_operands_prefix:
854 case mmix_operands_local:
855 case mmix_operands_bspec:
856 case mmix_operands_espec:
857 if (current_fb_label >= 0)
858 colon (fb_label_name (current_fb_label, 1));
859 else if (pending_label != NULL)
861 colon (pending_label);
862 pending_label = NULL;
870 /* Some of the pseudos emit contents, others don't. Set a
871 contents-emitted flag when we emit something into .text */
872 switch (instruction->operands)
874 case mmix_operands_loc:
879 case mmix_operands_byte:
884 case mmix_operands_wyde:
889 case mmix_operands_tetra:
894 case mmix_operands_octa:
899 case mmix_operands_prefix:
904 case mmix_operands_local:
909 case mmix_operands_bspec:
914 case mmix_operands_espec:
920 BAD_CASE (instruction->operands);
923 /* These are all working like the pseudo functions in read.c:s_...,
924 in that they step over the end-of-line marker at the end of the
925 line. We don't want that here. */
926 input_line_pointer--;
928 /* Step up the fb-label counter if there was a definition on this
930 if (current_fb_label >= 0)
932 fb_label_instance_inc (current_fb_label);
933 current_fb_label = -1;
936 /* Reset any don't-align-next-datum request, unless this was a LOC
938 if (instruction->operands != mmix_operands_loc)
944 /* Not a pseudo; we *will* emit contents. */
945 if (now_seg == data_section)
947 if (lowest_data_loc != (bfd_vma) -1 && (lowest_data_loc & 3) != 0)
949 if (data_has_contents)
950 as_bad (_("specified location wasn't TETRA-aligned"));
951 else if (want_unaligned)
952 as_bad (_("unaligned data at an absolute location is not supported"));
954 lowest_data_loc &= ~(bfd_vma) 3;
955 lowest_data_loc += 4;
958 data_has_contents = 1;
960 else if (now_seg == text_section)
962 if (lowest_text_loc != (bfd_vma) -1 && (lowest_text_loc & 3) != 0)
964 if (text_has_contents)
965 as_bad (_("specified location wasn't TETRA-aligned"));
966 else if (want_unaligned)
967 as_bad (_("unaligned data at an absolute location is not supported"));
969 lowest_text_loc &= ~(bfd_vma) 3;
970 lowest_text_loc += 4;
973 text_has_contents = 1;
976 /* After a sequence of BYTEs or WYDEs, we need to get to instruction
977 alignment. For other pseudos, a ".p2align 2" is supposed to be
978 inserted by the user. */
979 if (last_alignment < 2 && ! want_unaligned)
981 frag_align (2, 0, 0);
982 record_alignment (now_seg, 2);
986 /* Reset any don't-align-next-datum request. */
989 /* For mmixal compatibility, a label for an instruction (and emitting
990 pseudo) refers to the _aligned_ address. So we have to emit the
992 if (pending_label != NULL)
994 colon (pending_label);
995 pending_label = NULL;
998 /* We assume that mmix_opcodes keeps having unique mnemonics for each
999 opcode, so we don't have to iterate over more than one opcode; if the
1000 syntax does not match, then there's a syntax error. */
1002 /* Operands have little or no context and are all comma-separated; it is
1003 easier to parse each expression first. */
1004 switch (instruction->operands)
1006 case mmix_operands_reg_yz:
1007 case mmix_operands_pop:
1008 case mmix_operands_regaddr:
1009 case mmix_operands_pushj:
1010 case mmix_operands_get:
1011 case mmix_operands_put:
1012 case mmix_operands_set:
1013 case mmix_operands_save:
1014 case mmix_operands_unsave:
1018 case mmix_operands_sync:
1019 case mmix_operands_jmp:
1020 case mmix_operands_resume:
1024 /* The original 3 is fine for the rest. */
1029 /* If this is GET or PUT, and we don't do allow those names to be
1030 equated, we need to parse the names ourselves, so we don't pick up a
1031 user label instead of the special register. */
1032 if (! equated_spec_regs
1033 && (instruction->operands == mmix_operands_get
1034 || instruction->operands == mmix_operands_put))
1035 n_operands = get_putget_operands (instruction, operands, exp);
1037 n_operands = get_operands (max_operands, operands, exp);
1039 /* If there's a fb-label on the current line, set that label. This must
1040 be done *after* evaluating expressions of operands, since neither a
1041 "1B" nor a "1F" refers to "1H" on the same line. */
1042 if (current_fb_label >= 0)
1044 fb_label_instance_inc (current_fb_label);
1045 colon (fb_label_name (current_fb_label, 0));
1046 current_fb_label = -1;
1049 /* We also assume that the length of the instruction is at least 4, the
1050 size of an unexpanded instruction. We need a self-contained frag
1051 since we want the relocation to point to the instruction, not the
1054 opcodep = frag_more (4);
1055 mmix_opcode_frag = opc_fragP = frag_now;
1056 frag_now->fr_opcode = opcodep;
1058 /* Mark start of insn for DWARF2 debug features. */
1059 if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
1060 dwarf2_emit_insn (4);
1062 md_number_to_chars (opcodep, instruction->match, 4);
1064 switch (instruction->operands)
1066 case mmix_operands_jmp:
1067 if (n_operands == 0 && ! mmix_gnu_syntax)
1068 /* Zeros are in place - nothing needs to be done when we have no
1072 /* Add a frag for a JMP relaxation; we need room for max four
1073 extra instructions. We don't do any work around here to check if
1074 we can determine the offset right away. */
1075 if (n_operands != 1 || exp[0].X_op == O_register)
1077 as_bad (_("invalid operand to opcode %s: `%s'"),
1078 instruction->name, operands);
1083 frag_var (rs_machine_dependent, 4 * 4, 0,
1084 ENCODE_RELAX (STATE_JMP, STATE_UNDF),
1085 exp[0].X_add_symbol,
1086 exp[0].X_add_number,
1089 fix_new_exp (opc_fragP, opcodep - opc_fragP->fr_literal, 4,
1090 exp + 0, 1, BFD_RELOC_MMIX_ADDR27);
1093 case mmix_operands_pushj:
1094 /* We take care of PUSHJ in full here. */
1096 || ((exp[0].X_op == O_constant || exp[0].X_op == O_register)
1097 && (exp[0].X_add_number > 255 || exp[0].X_add_number < 0)))
1099 as_bad (_("invalid operands to opcode %s: `%s'"),
1100 instruction->name, operands);
1104 if (exp[0].X_op == O_register || exp[0].X_op == O_constant)
1105 opcodep[1] = exp[0].X_add_number;
1107 fix_new_exp (opc_fragP, opcodep - opc_fragP->fr_literal + 1,
1108 1, exp + 0, 0, BFD_RELOC_MMIX_REG_OR_BYTE);
1111 frag_var (rs_machine_dependent, PUSHJ_MAX_LEN - 4, 0,
1112 ENCODE_RELAX (STATE_PUSHJ, STATE_UNDF),
1113 exp[1].X_add_symbol,
1114 exp[1].X_add_number,
1117 fix_new_exp (opc_fragP, opcodep - opc_fragP->fr_literal, 4,
1118 exp + 1, 1, BFD_RELOC_MMIX_ADDR19);
1121 case mmix_operands_regaddr:
1122 /* GETA/branch: Add a frag for relaxation. We don't do any work
1123 around here to check if we can determine the offset right away. */
1124 if (n_operands != 2 || exp[1].X_op == O_register)
1126 as_bad (_("invalid operands to opcode %s: `%s'"),
1127 instruction->name, operands);
1132 fix_new_exp (opc_fragP, opcodep - opc_fragP->fr_literal, 4,
1133 exp + 1, 1, BFD_RELOC_MMIX_ADDR19);
1134 else if (instruction->type == mmix_type_condbranch)
1135 frag_var (rs_machine_dependent, BCC_MAX_LEN - 4, 0,
1136 ENCODE_RELAX (STATE_BCC, STATE_UNDF),
1137 exp[1].X_add_symbol,
1138 exp[1].X_add_number,
1141 frag_var (rs_machine_dependent, GETA_MAX_LEN - 4, 0,
1142 ENCODE_RELAX (STATE_GETA, STATE_UNDF),
1143 exp[1].X_add_symbol,
1144 exp[1].X_add_number,
1152 switch (instruction->operands)
1154 case mmix_operands_regs:
1155 /* We check the number of operands here, since we're in a
1156 FALLTHROUGH sequence in the next switch. */
1157 if (n_operands != 3 || exp[2].X_op == O_constant)
1159 as_bad (_("invalid operands to opcode %s: `%s'"),
1160 instruction->name, operands);
1164 case mmix_operands_regs_z:
1165 if (n_operands != 3)
1167 as_bad (_("invalid operands to opcode %s: `%s'"),
1168 instruction->name, operands);
1172 case mmix_operands_reg_yz:
1173 case mmix_operands_roundregs_z:
1174 case mmix_operands_roundregs:
1175 case mmix_operands_regs_z_opt:
1176 case mmix_operands_neg:
1177 case mmix_operands_regaddr:
1178 case mmix_operands_get:
1179 case mmix_operands_set:
1180 case mmix_operands_save:
1182 || (exp[0].X_op == O_register && exp[0].X_add_number > 255))
1184 as_bad (_("invalid operands to opcode %s: `%s'"),
1185 instruction->name, operands);
1189 if (exp[0].X_op == O_register)
1190 opcodep[1] = exp[0].X_add_number;
1192 fix_new_exp (opc_fragP, opcodep - opc_fragP->fr_literal + 1,
1193 1, exp + 0, 0, BFD_RELOC_MMIX_REG);
1200 /* A corresponding once-over for those who take an 8-bit constant as
1201 their first operand. */
1202 switch (instruction->operands)
1204 case mmix_operands_pushgo:
1205 /* PUSHGO: X is a constant, but can be expressed as a register.
1206 We handle X here and use the common machinery of T,X,3,$ for
1207 the rest of the operands. */
1209 || ((exp[0].X_op == O_constant || exp[0].X_op == O_register)
1210 && (exp[0].X_add_number > 255 || exp[0].X_add_number < 0)))
1212 as_bad (_("invalid operands to opcode %s: `%s'"),
1213 instruction->name, operands);
1216 else if (exp[0].X_op == O_constant || exp[0].X_op == O_register)
1217 opcodep[1] = exp[0].X_add_number;
1219 fix_new_exp (opc_fragP, opcodep - opc_fragP->fr_literal + 1,
1220 1, exp + 0, 0, BFD_RELOC_MMIX_REG_OR_BYTE);
1223 case mmix_operands_pop:
1224 if ((n_operands == 0 || n_operands == 1) && ! mmix_gnu_syntax)
1227 case mmix_operands_x_regs_z:
1229 || (exp[0].X_op == O_constant
1230 && (exp[0].X_add_number > 255
1231 || exp[0].X_add_number < 0)))
1233 as_bad (_("invalid operands to opcode %s: `%s'"),
1234 instruction->name, operands);
1238 if (exp[0].X_op == O_constant)
1239 opcodep[1] = exp[0].X_add_number;
1241 /* FIXME: This doesn't bring us unsignedness checking. */
1242 fix_new_exp (opc_fragP, opcodep - opc_fragP->fr_literal + 1,
1243 1, exp + 0, 0, BFD_RELOC_8);
1248 /* Handle the rest. */
1249 switch (instruction->operands)
1251 case mmix_operands_set:
1252 /* SET: Either two registers, "$X,$Y", with Z field as zero, or
1253 "$X,YZ", meaning change the opcode to SETL. */
1255 || (exp[1].X_op == O_constant
1256 && (exp[1].X_add_number > 0xffff || exp[1].X_add_number < 0)))
1258 as_bad (_("invalid operands to opcode %s: `%s'"),
1259 instruction->name, operands);
1263 if (exp[1].X_op == O_constant)
1265 /* There's an ambiguity with "SET $0,Y" when Y isn't defined
1266 yet. To keep things simple, we assume that Y is then a
1267 register, and only change the opcode if Y is defined at this
1270 There's no compatibility problem with mmixal, since it emits
1271 errors if the field is not defined at this point. */
1272 md_number_to_chars (opcodep, SETL_INSN_BYTE, 1);
1274 opcodep[2] = (exp[1].X_add_number >> 8) & 255;
1275 opcodep[3] = exp[1].X_add_number & 255;
1279 case mmix_operands_x_regs_z:
1280 /* SYNCD: "X,$Y,$Z|Z". */
1282 case mmix_operands_regs:
1283 /* Three registers, $X,$Y,$Z. */
1285 case mmix_operands_regs_z:
1286 /* Operands "$X,$Y,$Z|Z", number of arguments checked above. */
1288 case mmix_operands_pushgo:
1289 /* Operands "$X|X,$Y,$Z|Z", optional Z. */
1291 case mmix_operands_regs_z_opt:
1292 /* Operands "$X,$Y,$Z|Z", with $Z|Z being optional, default 0. Any
1293 operands not completely decided yet are postponed to later in
1294 assembly (but not until link-time yet). */
1296 if ((n_operands != 2 && n_operands != 3)
1297 || (exp[1].X_op == O_register && exp[1].X_add_number > 255)
1299 && ((exp[2].X_op == O_register
1300 && exp[2].X_add_number > 255
1302 || (exp[2].X_op == O_constant
1303 && (exp[2].X_add_number > 255
1304 || exp[2].X_add_number < 0)))))
1306 as_bad (_("invalid operands to opcode %s: `%s'"),
1307 instruction->name, operands);
1311 if (n_operands == 2)
1315 /* The last operand is immediate whenever we see just two
1317 opcodep[0] |= IMM_OFFSET_BIT;
1319 /* Now, we could either have an implied "0" as the Z operand, or
1320 it could be the constant of a "base address plus offset". It
1321 depends on whether it is allowed; only memory operations, as
1322 signified by instruction->type and "T" and "X" operand types,
1323 and it depends on whether we find a register in the second
1325 if (exp[1].X_op == O_register && exp[1].X_add_number <= 255)
1327 /* A zero then; all done. */
1328 opcodep[2] = exp[1].X_add_number;
1332 /* Not known as a register. Is base address plus offset
1333 allowed, or can we assume that it is a register anyway? */
1334 if ((instruction->operands != mmix_operands_regs_z_opt
1335 && instruction->operands != mmix_operands_x_regs_z
1336 && instruction->operands != mmix_operands_pushgo)
1337 || (instruction->type != mmix_type_memaccess_octa
1338 && instruction->type != mmix_type_memaccess_tetra
1339 && instruction->type != mmix_type_memaccess_wyde
1340 && instruction->type != mmix_type_memaccess_byte
1341 && instruction->type != mmix_type_memaccess_block
1342 && instruction->type != mmix_type_jsr
1343 && instruction->type != mmix_type_branch))
1345 fix_new_exp (opc_fragP, opcodep - opc_fragP->fr_literal + 2,
1346 1, exp + 1, 0, BFD_RELOC_MMIX_REG);
1350 /* To avoid getting a NULL add_symbol for constants and then
1351 catching a SEGV in write_relocs since it doesn't handle
1352 constants well for relocs other than PC-relative, we need to
1353 pass expressions as symbols and use fix_new, not fix_new_exp. */
1354 sym = make_expr_symbol (exp + 1);
1356 /* Now we know it can be a "base address plus offset". Add
1357 proper fixup types so we can handle this later, when we've
1358 parsed everything. */
1359 fix_new (opc_fragP, opcodep - opc_fragP->fr_literal + 2,
1360 8, sym, 0, 0, BFD_RELOC_MMIX_BASE_PLUS_OFFSET);
1364 if (exp[1].X_op == O_register)
1365 opcodep[2] = exp[1].X_add_number;
1367 fix_new_exp (opc_fragP, opcodep - opc_fragP->fr_literal + 2,
1368 1, exp + 1, 0, BFD_RELOC_MMIX_REG);
1370 /* In mmixal compatibility mode, we allow special registers as
1371 constants for the Z operand. They have 256 added to their
1372 register numbers, so the right thing will happen if we just treat
1373 those as constants. */
1374 if (exp[2].X_op == O_register && exp[2].X_add_number <= 255)
1375 opcodep[3] = exp[2].X_add_number;
1376 else if (exp[2].X_op == O_constant
1377 || (exp[2].X_op == O_register && exp[2].X_add_number > 255))
1379 opcodep[3] = exp[2].X_add_number;
1380 opcodep[0] |= IMM_OFFSET_BIT;
1383 fix_new_exp (opc_fragP, opcodep - opc_fragP->fr_literal + 3,
1385 (instruction->operands == mmix_operands_set
1386 || instruction->operands == mmix_operands_regs)
1387 ? BFD_RELOC_MMIX_REG : BFD_RELOC_MMIX_REG_OR_BYTE);
1390 case mmix_operands_pop:
1391 /* POP, one eight and one 16-bit operand. */
1392 if (n_operands == 0 && ! mmix_gnu_syntax)
1394 if (n_operands == 1 && ! mmix_gnu_syntax)
1395 goto a_single_24_bit_number_operand;
1397 case mmix_operands_reg_yz:
1398 /* A register and a 16-bit unsigned number. */
1400 || exp[1].X_op == O_register
1401 || (exp[1].X_op == O_constant
1402 && (exp[1].X_add_number > 0xffff || exp[1].X_add_number < 0)))
1404 as_bad (_("invalid operands to opcode %s: `%s'"),
1405 instruction->name, operands);
1409 if (exp[1].X_op == O_constant)
1411 opcodep[2] = (exp[1].X_add_number >> 8) & 255;
1412 opcodep[3] = exp[1].X_add_number & 255;
1415 /* FIXME: This doesn't bring us unsignedness checking. */
1416 fix_new_exp (opc_fragP, opcodep - opc_fragP->fr_literal + 2,
1417 2, exp + 1, 0, BFD_RELOC_16);
1420 case mmix_operands_jmp:
1421 /* A JMP. Everyhing is already done. */
1424 case mmix_operands_roundregs:
1425 /* Two registers with optional rounding mode or constant in between. */
1426 if ((n_operands == 3 && exp[2].X_op == O_constant)
1427 || (n_operands == 2 && exp[1].X_op == O_constant))
1429 as_bad (_("invalid operands to opcode %s: `%s'"),
1430 instruction->name, operands);
1434 case mmix_operands_roundregs_z:
1435 /* Like FLOT, "$X,ROUND_MODE,$Z|Z", but the rounding mode is
1436 optional and can be the corresponding constant. */
1438 /* Which exp index holds the second operand (not the rounding
1440 int op2no = n_operands - 1;
1442 if ((n_operands != 2 && n_operands != 3)
1443 || ((exp[op2no].X_op == O_register
1444 && exp[op2no].X_add_number > 255)
1445 || (exp[op2no].X_op == O_constant
1446 && (exp[op2no].X_add_number > 255
1447 || exp[op2no].X_add_number < 0)))
1449 /* We don't allow for the rounding mode to be deferred; it
1450 must be determined in the "first pass". It cannot be a
1451 symbol equated to a rounding mode, but defined after
1453 && ((exp[1].X_op == O_register
1454 && exp[1].X_add_number < 512)
1455 || (exp[1].X_op == O_constant
1456 && exp[1].X_add_number < 0
1457 && exp[1].X_add_number > 4)
1458 || (exp[1].X_op != O_register
1459 && exp[1].X_op != O_constant))))
1461 as_bad (_("invalid operands to opcode %s: `%s'"),
1462 instruction->name, operands);
1466 /* Add rounding mode if present. */
1467 if (n_operands == 3)
1468 opcodep[2] = exp[1].X_add_number & 255;
1470 if (exp[op2no].X_op == O_register)
1471 opcodep[3] = exp[op2no].X_add_number;
1472 else if (exp[op2no].X_op == O_constant)
1474 opcodep[3] = exp[op2no].X_add_number;
1475 opcodep[0] |= IMM_OFFSET_BIT;
1478 fix_new_exp (opc_fragP, opcodep - opc_fragP->fr_literal + 3,
1480 instruction->operands == mmix_operands_roundregs
1481 ? BFD_RELOC_MMIX_REG
1482 : BFD_RELOC_MMIX_REG_OR_BYTE);
1486 case mmix_operands_sync:
1487 a_single_24_bit_number_operand:
1489 || exp[0].X_op == O_register
1490 || (exp[0].X_op == O_constant
1491 && (exp[0].X_add_number > 0xffffff || exp[0].X_add_number < 0)))
1493 as_bad (_("invalid operands to opcode %s: `%s'"),
1494 instruction->name, operands);
1498 if (exp[0].X_op == O_constant)
1500 opcodep[1] = (exp[0].X_add_number >> 16) & 255;
1501 opcodep[2] = (exp[0].X_add_number >> 8) & 255;
1502 opcodep[3] = exp[0].X_add_number & 255;
1505 /* FIXME: This doesn't bring us unsignedness checking. */
1506 fix_new_exp (opc_fragP, opcodep - opc_fragP->fr_literal + 1,
1507 3, exp + 0, 0, BFD_RELOC_24);
1510 case mmix_operands_neg:
1511 /* Operands "$X,Y,$Z|Z"; NEG or NEGU. Y is optional, 0 is default. */
1513 if ((n_operands != 3 && n_operands != 2)
1514 || (n_operands == 3 && exp[1].X_op == O_register)
1515 || ((exp[1].X_op == O_constant || exp[1].X_op == O_register)
1516 && (exp[1].X_add_number > 255 || exp[1].X_add_number < 0))
1518 && ((exp[2].X_op == O_register && exp[2].X_add_number > 255)
1519 || (exp[2].X_op == O_constant
1520 && (exp[2].X_add_number > 255
1521 || exp[2].X_add_number < 0)))))
1523 as_bad (_("invalid operands to opcode %s: `%s'"),
1524 instruction->name, operands);
1528 if (n_operands == 2)
1530 if (exp[1].X_op == O_register)
1531 opcodep[3] = exp[1].X_add_number;
1532 else if (exp[1].X_op == O_constant)
1534 opcodep[3] = exp[1].X_add_number;
1535 opcodep[0] |= IMM_OFFSET_BIT;
1538 fix_new_exp (opc_fragP, opcodep - opc_fragP->fr_literal + 3,
1539 1, exp + 1, 0, BFD_RELOC_MMIX_REG_OR_BYTE);
1543 if (exp[1].X_op == O_constant)
1544 opcodep[2] = exp[1].X_add_number;
1546 fix_new_exp (opc_fragP, opcodep - opc_fragP->fr_literal + 2,
1547 1, exp + 1, 0, BFD_RELOC_8);
1549 if (exp[2].X_op == O_register)
1550 opcodep[3] = exp[2].X_add_number;
1551 else if (exp[2].X_op == O_constant)
1553 opcodep[3] = exp[2].X_add_number;
1554 opcodep[0] |= IMM_OFFSET_BIT;
1557 fix_new_exp (opc_fragP, opcodep - opc_fragP->fr_literal + 3,
1558 1, exp + 2, 0, BFD_RELOC_MMIX_REG_OR_BYTE);
1561 case mmix_operands_regaddr:
1562 /* A GETA/branch-type. */
1565 case mmix_operands_get:
1566 /* "$X,spec_reg"; GET.
1567 Like with rounding modes, we demand that the special register or
1568 symbol is already defined when we get here at the point of use. */
1570 || (exp[1].X_op == O_register
1571 && (exp[1].X_add_number < 256 || exp[1].X_add_number >= 512))
1572 || (exp[1].X_op == O_constant
1573 && (exp[1].X_add_number < 0 || exp[1].X_add_number > 256))
1574 || (exp[1].X_op != O_constant && exp[1].X_op != O_register))
1576 as_bad (_("invalid operands to opcode %s: `%s'"),
1577 instruction->name, operands);
1581 opcodep[3] = exp[1].X_add_number - 256;
1584 case mmix_operands_put:
1585 /* "spec_reg,$Z|Z"; PUT. */
1587 || (exp[0].X_op == O_register
1588 && (exp[0].X_add_number < 256 || exp[0].X_add_number >= 512))
1589 || (exp[0].X_op == O_constant
1590 && (exp[0].X_add_number < 0 || exp[0].X_add_number > 256))
1591 || (exp[0].X_op != O_constant && exp[0].X_op != O_register))
1593 as_bad (_("invalid operands to opcode %s: `%s'"),
1594 instruction->name, operands);
1598 opcodep[1] = exp[0].X_add_number - 256;
1600 /* Note that the Y field is zero. */
1602 if (exp[1].X_op == O_register)
1603 opcodep[3] = exp[1].X_add_number;
1604 else if (exp[1].X_op == O_constant)
1606 opcodep[3] = exp[1].X_add_number;
1607 opcodep[0] |= IMM_OFFSET_BIT;
1610 fix_new_exp (opc_fragP, opcodep - opc_fragP->fr_literal + 3,
1611 1, exp + 1, 0, BFD_RELOC_MMIX_REG_OR_BYTE);
1614 case mmix_operands_save:
1617 || exp[1].X_op != O_constant
1618 || exp[1].X_add_number != 0)
1620 as_bad (_("invalid operands to opcode %s: `%s'"),
1621 instruction->name, operands);
1626 case mmix_operands_unsave:
1627 if (n_operands < 2 && ! mmix_gnu_syntax)
1629 if (n_operands == 1)
1631 if (exp[0].X_op == O_register)
1632 opcodep[3] = exp[0].X_add_number;
1634 fix_new_exp (opc_fragP, opcodep - opc_fragP->fr_literal + 3,
1635 1, exp, 0, BFD_RELOC_MMIX_REG);
1640 /* "0,$Z"; UNSAVE. */
1642 || exp[0].X_op != O_constant
1643 || exp[0].X_add_number != 0
1644 || exp[1].X_op == O_constant
1645 || (exp[1].X_op == O_register
1646 && exp[1].X_add_number > 255))
1648 as_bad (_("invalid operands to opcode %s: `%s'"),
1649 instruction->name, operands);
1653 if (exp[1].X_op == O_register)
1654 opcodep[3] = exp[1].X_add_number;
1656 fix_new_exp (opc_fragP, opcodep - opc_fragP->fr_literal + 3,
1657 1, exp + 1, 0, BFD_RELOC_MMIX_REG);
1660 case mmix_operands_xyz_opt:
1661 /* SWYM, TRIP, TRAP: zero, one, two or three operands. */
1662 if (n_operands == 0 && ! mmix_gnu_syntax)
1663 /* Zeros are in place - nothing needs to be done for zero
1664 operands. We don't allow this in GNU syntax mode, because it
1665 was believed that the risk of missing to supply an operand is
1666 higher than the benefit of not having to specify a zero. */
1668 else if (n_operands == 1 && exp[0].X_op != O_register)
1670 if (exp[0].X_op == O_constant)
1672 if (exp[0].X_add_number > 255*255*255
1673 || exp[0].X_add_number < 0)
1675 as_bad (_("invalid operands to opcode %s: `%s'"),
1676 instruction->name, operands);
1681 opcodep[1] = (exp[0].X_add_number >> 16) & 255;
1682 opcodep[2] = (exp[0].X_add_number >> 8) & 255;
1683 opcodep[3] = exp[0].X_add_number & 255;
1687 fix_new_exp (opc_fragP, opcodep - opc_fragP->fr_literal + 1,
1688 3, exp, 0, BFD_RELOC_24);
1690 else if (n_operands == 2
1691 && exp[0].X_op != O_register
1692 && exp[1].X_op != O_register)
1696 if (exp[0].X_op == O_constant)
1698 if (exp[0].X_add_number > 255
1699 || exp[0].X_add_number < 0)
1701 as_bad (_("invalid operands to opcode %s: `%s'"),
1702 instruction->name, operands);
1706 opcodep[1] = exp[0].X_add_number & 255;
1709 fix_new_exp (opc_fragP, opcodep - opc_fragP->fr_literal + 1,
1710 1, exp, 0, BFD_RELOC_8);
1712 if (exp[1].X_op == O_constant)
1714 if (exp[1].X_add_number > 255*255
1715 || exp[1].X_add_number < 0)
1717 as_bad (_("invalid operands to opcode %s: `%s'"),
1718 instruction->name, operands);
1723 opcodep[2] = (exp[1].X_add_number >> 8) & 255;
1724 opcodep[3] = exp[1].X_add_number & 255;
1728 fix_new_exp (opc_fragP, opcodep - opc_fragP->fr_literal + 2,
1729 2, exp + 1, 0, BFD_RELOC_16);
1731 else if (n_operands == 3
1732 && exp[0].X_op != O_register
1733 && exp[1].X_op != O_register
1734 && exp[2].X_op != O_register)
1736 /* Three operands. */
1738 if (exp[0].X_op == O_constant)
1740 if (exp[0].X_add_number > 255
1741 || exp[0].X_add_number < 0)
1743 as_bad (_("invalid operands to opcode %s: `%s'"),
1744 instruction->name, operands);
1748 opcodep[1] = exp[0].X_add_number & 255;
1751 fix_new_exp (opc_fragP, opcodep - opc_fragP->fr_literal + 1,
1752 1, exp, 0, BFD_RELOC_8);
1754 if (exp[1].X_op == O_constant)
1756 if (exp[1].X_add_number > 255
1757 || exp[1].X_add_number < 0)
1759 as_bad (_("invalid operands to opcode %s: `%s'"),
1760 instruction->name, operands);
1764 opcodep[2] = exp[1].X_add_number & 255;
1767 fix_new_exp (opc_fragP, opcodep - opc_fragP->fr_literal + 2,
1768 1, exp + 1, 0, BFD_RELOC_8);
1770 if (exp[2].X_op == O_constant)
1772 if (exp[2].X_add_number > 255
1773 || exp[2].X_add_number < 0)
1775 as_bad (_("invalid operands to opcode %s: `%s'"),
1776 instruction->name, operands);
1780 opcodep[3] = exp[2].X_add_number & 255;
1783 fix_new_exp (opc_fragP, opcodep - opc_fragP->fr_literal + 3,
1784 1, exp + 2, 0, BFD_RELOC_8);
1786 else if (n_operands <= 3
1787 && (strcmp (instruction->name, "trip") == 0
1788 || strcmp (instruction->name, "trap") == 0))
1790 /* The meaning of operands to TRIP and TRAP are not defined, so
1791 we add combinations not handled above here as we find them. */
1792 if (n_operands == 3)
1794 /* Don't require non-register operands. Always generate
1795 fixups, so we don't have to copy lots of code and create
1796 maintanance problems. TRIP is supposed to be a rare
1797 instruction, so the overhead should not matter. We
1798 aren't allowed to fix_new_exp for an expression which is
1799 an O_register at this point, however. */
1800 if (exp[0].X_op == O_register)
1801 opcodep[1] = exp[0].X_add_number;
1803 fix_new_exp (opc_fragP, opcodep - opc_fragP->fr_literal + 1,
1804 1, exp, 0, BFD_RELOC_MMIX_REG_OR_BYTE);
1805 if (exp[1].X_op == O_register)
1806 opcodep[2] = exp[1].X_add_number;
1808 fix_new_exp (opc_fragP, opcodep - opc_fragP->fr_literal + 2,
1809 1, exp + 1, 0, BFD_RELOC_MMIX_REG_OR_BYTE);
1810 if (exp[2].X_op == O_register)
1811 opcodep[3] = exp[2].X_add_number;
1813 fix_new_exp (opc_fragP, opcodep - opc_fragP->fr_literal + 3,
1814 1, exp + 2, 0, BFD_RELOC_MMIX_REG_OR_BYTE);
1816 else if (n_operands == 2)
1818 if (exp[0].X_op == O_register)
1819 opcodep[2] = exp[0].X_add_number;
1821 fix_new_exp (opc_fragP, opcodep - opc_fragP->fr_literal + 2,
1822 1, exp, 0, BFD_RELOC_MMIX_REG_OR_BYTE);
1823 if (exp[1].X_op == O_register)
1824 opcodep[3] = exp[1].X_add_number;
1826 fix_new_exp (opc_fragP, opcodep - opc_fragP->fr_literal + 3,
1827 1, exp + 1, 0, BFD_RELOC_MMIX_REG_OR_BYTE);
1831 as_bad (_("unsupported operands to %s: `%s'"),
1832 instruction->name, operands);
1838 as_bad (_("invalid operands to opcode %s: `%s'"),
1839 instruction->name, operands);
1844 case mmix_operands_resume:
1845 if (n_operands == 0 && ! mmix_gnu_syntax)
1849 || exp[0].X_op == O_register
1850 || (exp[0].X_op == O_constant
1851 && (exp[0].X_add_number < 0
1852 || exp[0].X_add_number > 255)))
1854 as_bad (_("invalid operands to opcode %s: `%s'"),
1855 instruction->name, operands);
1859 if (exp[0].X_op == O_constant)
1860 opcodep[3] = exp[0].X_add_number;
1862 fix_new_exp (opc_fragP, opcodep - opc_fragP->fr_literal + 3,
1863 1, exp + 0, 0, BFD_RELOC_8);
1866 case mmix_operands_pushj:
1867 /* All is done for PUSHJ already. */
1871 BAD_CASE (instruction->operands);
1875 /* For the benefit of insns that start with a digit, we assemble by way of
1876 tc_unrecognized_line too, through this function. */
1879 mmix_assemble_return_nonzero (str)
1882 int last_error_count = had_errors ();
1886 /* Normal instruction handling downcases, so we must too. */
1887 while (ISALNUM (*s2))
1889 if (ISUPPER ((unsigned char) *s2))
1890 *s2 = TOLOWER (*s2);
1894 /* Cut the line for sake of the assembly. */
1895 for (s2 = str; *s2 && *s2 != '\n'; s2++)
1903 return had_errors () == last_error_count;
1906 /* The PREFIX pseudo. */
1910 int unused ATTRIBUTE_UNUSED;
1917 p = input_line_pointer;
1919 c = get_symbol_end ();
1921 /* Reseting prefix? */
1922 if (*p == ':' && p[1] == 0)
1923 mmix_current_prefix = NULL;
1926 /* Put this prefix on the mmix symbols obstack. We could malloc and
1927 free it separately, but then we'd have to worry about that.
1928 People using up memory on prefixes have other problems. */
1929 obstack_grow (&mmix_sym_obstack, p, strlen (p) + 1);
1930 p = obstack_finish (&mmix_sym_obstack);
1932 /* Accumulate prefixes, and strip a leading ':'. */
1933 if (mmix_current_prefix != NULL || *p == ':')
1934 p = mmix_prefix_name (p);
1936 mmix_current_prefix = p;
1939 *input_line_pointer = c;
1941 mmix_handle_rest_of_empty_line ();
1944 /* We implement prefixes by using the tc_canonicalize_symbol_name hook,
1945 and store each prefixed name on a (separate) obstack. This means that
1946 the name is on the "notes" obstack in non-prefixed form and on the
1947 mmix_sym_obstack in prefixed form, but currently it is not worth
1948 rewriting the whole GAS symbol handling to improve "hooking" to avoid
1949 that. (It might be worth a rewrite for other reasons, though). */
1952 mmix_prefix_name (shortname)
1955 if (*shortname == ':')
1956 return shortname + 1;
1958 if (mmix_current_prefix == NULL)
1959 as_fatal (_("internal: mmix_prefix_name but empty prefix"));
1961 if (*shortname == '$')
1964 obstack_grow (&mmix_sym_obstack, mmix_current_prefix,
1965 strlen (mmix_current_prefix));
1966 obstack_grow (&mmix_sym_obstack, shortname, strlen (shortname) + 1);
1967 return obstack_finish (&mmix_sym_obstack);
1970 /* The GREG pseudo. At LABEL, we have the name of a symbol that we
1971 want to make a register symbol, and which should be initialized with
1972 the value in the expression at INPUT_LINE_POINTER (defaulting to 0).
1973 Either and (perhaps less meaningful) both may be missing. LABEL must
1974 be persistent, perhaps allocated on an obstack. */
1977 mmix_greg_internal (label)
1980 expressionS *expP = &mmix_raw_gregs[n_of_raw_gregs].exp;
1982 /* Don't set the section to register contents section before the
1983 expression has been parsed; it may refer to the current position. */
1986 /* FIXME: Check that no expression refers to the register contents
1987 section. May need to be done in elf64-mmix.c. */
1988 if (expP->X_op == O_absent)
1990 /* Default to zero if the expression was absent. */
1991 expP->X_op = O_constant;
1992 expP->X_add_number = 0;
1993 expP->X_unsigned = 0;
1994 expP->X_add_symbol = NULL;
1995 expP->X_op_symbol = NULL;
1998 /* We must handle prefixes here, as we save the labels and expressions
1999 to be output later. */
2000 mmix_raw_gregs[n_of_raw_gregs].label
2001 = mmix_current_prefix == NULL ? label : mmix_prefix_name (label);
2003 if (n_of_raw_gregs == MAX_GREGS - 1)
2004 as_bad (_("too many GREG registers allocated (max %d)"), MAX_GREGS);
2008 mmix_handle_rest_of_empty_line ();
2011 /* The ".greg label,expr" worker. */
2015 int unused ATTRIBUTE_UNUSED;
2019 p = input_line_pointer;
2021 /* This will skip over what can be a symbol and zero out the next
2022 character, which we assume is a ',' or other meaningful delimiter.
2023 What comes after that is the initializer expression for the
2025 c = get_symbol_end ();
2027 if (! is_end_of_line[(unsigned char) c])
2028 input_line_pointer++;
2032 /* The label must be persistent; it's not used until after all input
2034 obstack_grow (&mmix_sym_obstack, p, strlen (p) + 1);
2035 mmix_greg_internal (obstack_finish (&mmix_sym_obstack));
2038 mmix_greg_internal (NULL);
2041 /* The "BSPEC expr" worker. */
2045 int unused ATTRIBUTE_UNUSED;
2049 char secname[sizeof (MMIX_OTHER_SPEC_SECTION_PREFIX) + 20]
2050 = MMIX_OTHER_SPEC_SECTION_PREFIX;
2054 /* Get a constant expression which we can evaluate *now*. Supporting
2055 more complex (though assembly-time computable) expressions is
2056 feasible but Too Much Work for something of unknown usefulness like
2058 expsec = expression (&exp);
2059 mmix_handle_rest_of_empty_line ();
2061 /* Check that we don't have another BSPEC in progress. */
2064 as_bad (_("BSPEC already active. Nesting is not supported."));
2068 if (exp.X_op != O_constant
2069 || expsec != absolute_section
2070 || exp.X_add_number < 0
2071 || exp.X_add_number > 65535)
2073 as_bad (_("invalid BSPEC expression"));
2074 exp.X_add_number = 0;
2077 n = (int) exp.X_add_number;
2079 sprintf (secname + strlen (MMIX_OTHER_SPEC_SECTION_PREFIX), "%d", n);
2080 sec = bfd_get_section_by_name (stdoutput, secname);
2083 /* We need a non-volatile name as it will be stored in the section
2085 char *newsecname = xstrdup (secname);
2086 sec = bfd_make_section (stdoutput, newsecname);
2089 as_fatal (_("can't create section %s"), newsecname);
2091 if (!bfd_set_section_flags (stdoutput, sec,
2092 bfd_get_section_flags (stdoutput, sec)
2094 as_fatal (_("can't set section flags for section %s"), newsecname);
2097 /* Tell ELF about the pending section change. */
2098 obj_elf_section_change_hook ();
2099 subseg_set (sec, 0);
2101 /* Save position for missing ESPEC. */
2102 as_where (&bspec_file, &bspec_line);
2107 /* The "ESPEC" worker. */
2111 int unused ATTRIBUTE_UNUSED;
2113 /* First, check that we *do* have a BSPEC in progress. */
2116 as_bad (_("ESPEC without preceding BSPEC"));
2120 mmix_handle_rest_of_empty_line ();
2123 /* When we told ELF about the section change in s_bspec, it stored the
2124 previous section for us so we can get at it with the equivalent of a
2125 .previous pseudo. */
2126 obj_elf_previous (0);
2129 /* The " .local expr" and " local expr" worker. We make a BFD_MMIX_LOCAL
2130 relocation against the current position against the expression.
2131 Implementing this by means of contents in a section lost. */
2134 mmix_s_local (unused)
2135 int unused ATTRIBUTE_UNUSED;
2139 /* Don't set the section to register contents section before the
2140 expression has been parsed; it may refer to the current position in
2141 some contorted way. */
2144 if (exp.X_op == O_absent)
2146 as_bad (_("missing local expression"));
2149 else if (exp.X_op == O_register)
2151 /* fix_new_exp doesn't like O_register. Should be configurable.
2152 We're fine with a constant here, though. */
2153 exp.X_op = O_constant;
2156 fix_new_exp (frag_now, 0, 0, &exp, 0, BFD_RELOC_MMIX_LOCAL);
2157 mmix_handle_rest_of_empty_line ();
2160 /* Set fragP->fr_var to the initial guess of the size of a relaxable insn
2161 and return it. Sizes of other instructions are not known. This
2162 function may be called multiple times. */
2165 md_estimate_size_before_relax (fragP, segment)
2171 #define HANDLE_RELAXABLE(state) \
2172 case ENCODE_RELAX (state, STATE_UNDF): \
2173 if (fragP->fr_symbol != NULL \
2174 && S_GET_SEGMENT (fragP->fr_symbol) == segment \
2175 && !S_IS_WEAK (fragP->fr_symbol)) \
2177 /* The symbol lies in the same segment - a relaxable case. */ \
2179 = ENCODE_RELAX (state, STATE_ZERO); \
2183 switch (fragP->fr_subtype)
2185 HANDLE_RELAXABLE (STATE_GETA);
2186 HANDLE_RELAXABLE (STATE_BCC);
2187 HANDLE_RELAXABLE (STATE_PUSHJ);
2188 HANDLE_RELAXABLE (STATE_JMP);
2190 case ENCODE_RELAX (STATE_GETA, STATE_ZERO):
2191 case ENCODE_RELAX (STATE_BCC, STATE_ZERO):
2192 case ENCODE_RELAX (STATE_PUSHJ, STATE_ZERO):
2193 case ENCODE_RELAX (STATE_JMP, STATE_ZERO):
2194 /* When relaxing a section for the second time, we don't need to do
2195 anything except making sure that fr_var is set right. */
2198 case STATE_GREG_DEF:
2199 length = fragP->tc_frag_data != NULL ? 0 : 8;
2200 fragP->fr_var = length;
2202 /* Don't consult the relax_table; it isn't valid for this
2208 BAD_CASE (fragP->fr_subtype);
2211 length = mmix_relax_table[fragP->fr_subtype].rlx_length;
2212 fragP->fr_var = length;
2217 /* Turn a string in input_line_pointer into a floating point constant of type
2218 type, and store the appropriate bytes in *litP. The number of LITTLENUMS
2219 emitted is stored in *sizeP . An error message is returned, or NULL on
2223 md_atof (type, litP, sizeP)
2229 LITTLENUM_TYPE words[4];
2235 /* FIXME: Having 'f' in mmix_flt_chars (and here) makes it
2236 problematic to also have a forward reference in an expression.
2237 The testsuite wants it, and it's customary.
2238 We'll deal with the real problems when they come; we share the
2239 problem with most other ports. */
2249 return _("bad call to md_atof");
2252 t = atof_ieee (input_line_pointer, type, words);
2254 input_line_pointer = t;
2258 for (i = 0; i < prec; i++)
2260 md_number_to_chars (litP, (valueT) words[i], 2);
2266 /* Convert variable-sized frags into one or more fixups. */
2269 md_convert_frag (abfd, sec, fragP)
2270 bfd *abfd ATTRIBUTE_UNUSED;
2271 segT sec ATTRIBUTE_UNUSED;
2274 /* Pointer to first byte in variable-sized part of the frag. */
2277 /* Pointer to first opcode byte in frag. */
2280 /* Size in bytes of variable-sized part of frag. */
2281 int var_part_size = 0;
2283 /* This is part of *fragP. It contains all information about addresses
2284 and offsets to varying parts. */
2286 unsigned long var_part_offset;
2288 /* This is the frag for the opcode. It, rather than fragP, must be used
2289 when emitting a frag for the opcode. */
2290 fragS *opc_fragP = fragP->tc_frag_data;
2293 /* Where, in file space, does addr point? */
2294 bfd_vma target_address;
2295 bfd_vma opcode_address;
2297 know (fragP->fr_type == rs_machine_dependent);
2299 var_part_offset = fragP->fr_fix;
2300 var_partp = fragP->fr_literal + var_part_offset;
2301 opcodep = fragP->fr_opcode;
2303 symbolP = fragP->fr_symbol;
2306 = ((symbolP ? S_GET_VALUE (symbolP) : 0) + fragP->fr_offset);
2308 /* The opcode that would be extended is the last four "fixed" bytes. */
2309 opcode_address = fragP->fr_address + fragP->fr_fix - 4;
2311 switch (fragP->fr_subtype)
2313 case ENCODE_RELAX (STATE_GETA, STATE_ZERO):
2314 case ENCODE_RELAX (STATE_BCC, STATE_ZERO):
2315 case ENCODE_RELAX (STATE_PUSHJ, STATE_ZERO):
2316 mmix_set_geta_branch_offset (opcodep, target_address - opcode_address);
2320 = fix_new (opc_fragP, opcodep - opc_fragP->fr_literal, 4,
2321 fragP->fr_symbol, fragP->fr_offset, 1,
2322 BFD_RELOC_MMIX_ADDR19);
2323 COPY_FR_WHERE_TO_FX (fragP, tmpfixP);
2328 case ENCODE_RELAX (STATE_JMP, STATE_ZERO):
2329 mmix_set_jmp_offset (opcodep, target_address - opcode_address);
2333 = fix_new (opc_fragP, opcodep - opc_fragP->fr_literal, 4,
2334 fragP->fr_symbol, fragP->fr_offset, 1,
2335 BFD_RELOC_MMIX_ADDR27);
2336 COPY_FR_WHERE_TO_FX (fragP, tmpfixP);
2341 case STATE_GREG_DEF:
2342 if (fragP->tc_frag_data == NULL)
2345 = fix_new (fragP, var_partp - fragP->fr_literal, 8,
2346 fragP->fr_symbol, fragP->fr_offset, 0, BFD_RELOC_64);
2347 COPY_FR_WHERE_TO_FX (fragP, tmpfixP);
2348 mmix_gregs[n_of_cooked_gregs++] = tmpfixP;
2355 #define HANDLE_MAX_RELOC(state, reloc) \
2356 case ENCODE_RELAX (state, STATE_MAX): \
2358 = mmix_relax_table[ENCODE_RELAX (state, STATE_MAX)].rlx_length; \
2359 mmix_fill_nops (var_partp, var_part_size / 4); \
2360 if (warn_on_expansion) \
2361 as_warn_where (fragP->fr_file, fragP->fr_line, \
2362 _("operand out of range, instruction expanded")); \
2363 tmpfixP = fix_new (fragP, var_partp - fragP->fr_literal - 4, 8, \
2364 fragP->fr_symbol, fragP->fr_offset, 1, reloc); \
2365 COPY_FR_WHERE_TO_FX (fragP, tmpfixP); \
2368 HANDLE_MAX_RELOC (STATE_GETA, BFD_RELOC_MMIX_GETA);
2369 HANDLE_MAX_RELOC (STATE_BCC, BFD_RELOC_MMIX_CBRANCH);
2370 HANDLE_MAX_RELOC (STATE_PUSHJ, BFD_RELOC_MMIX_PUSHJ);
2371 HANDLE_MAX_RELOC (STATE_JMP, BFD_RELOC_MMIX_JMP);
2374 BAD_CASE (fragP->fr_subtype);
2378 fragP->fr_fix += var_part_size;
2382 /* Applies the desired value to the specified location.
2383 Also sets up addends for RELA type relocations.
2384 Stolen from tc-mcore.c.
2386 Note that this function isn't called when linkrelax != 0. */
2389 md_apply_fix3 (fixP, valP, segment)
2394 char *buf = fixP->fx_where + fixP->fx_frag->fr_literal;
2395 /* Note: use offsetT because it is signed, valueT is unsigned. */
2396 offsetT val = (offsetT) * valP;
2398 = (fixP->fx_addsy == NULL
2399 ? absolute_section : S_GET_SEGMENT (fixP->fx_addsy));
2401 /* If the fix is relative to a symbol which is not defined, or, (if
2402 pcrel), not in the same segment as the fix, we cannot resolve it
2404 if (fixP->fx_addsy != NULL
2405 && (! S_IS_DEFINED (fixP->fx_addsy)
2406 || S_IS_WEAK (fixP->fx_addsy)
2407 || (fixP->fx_pcrel && symsec != segment)
2408 || (! fixP->fx_pcrel
2409 && symsec != absolute_section
2410 && ((fixP->fx_r_type != BFD_RELOC_MMIX_REG
2411 && fixP->fx_r_type != BFD_RELOC_MMIX_REG_OR_BYTE)
2412 || symsec != reg_section))))
2417 else if (fixP->fx_r_type == BFD_RELOC_MMIX_LOCAL
2418 || fixP->fx_r_type == BFD_RELOC_VTABLE_INHERIT
2419 || fixP->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
2421 /* These are never "fixed". */
2426 /* We assume every other relocation is "fixed". */
2429 switch (fixP->fx_r_type)
2436 case BFD_RELOC_64_PCREL:
2437 case BFD_RELOC_32_PCREL:
2438 case BFD_RELOC_24_PCREL:
2439 case BFD_RELOC_16_PCREL:
2440 case BFD_RELOC_8_PCREL:
2441 md_number_to_chars (buf, val, fixP->fx_size);
2444 case BFD_RELOC_MMIX_ADDR19:
2447 /* This shouldn't happen. */
2448 BAD_CASE (fixP->fx_r_type);
2452 case BFD_RELOC_MMIX_GETA:
2453 case BFD_RELOC_MMIX_CBRANCH:
2454 case BFD_RELOC_MMIX_PUSHJ:
2455 /* If this fixup is out of range, punt to the linker to emit an
2456 error. This should only happen with -no-expand. */
2457 if (val < -(((offsetT) 1 << 19)/2)
2458 || val >= ((offsetT) 1 << 19)/2 - 1
2461 if (warn_on_expansion)
2462 as_warn_where (fixP->fx_file, fixP->fx_line,
2463 _("operand out of range"));
2467 mmix_set_geta_branch_offset (buf, val);
2470 case BFD_RELOC_MMIX_ADDR27:
2473 /* This shouldn't happen. */
2474 BAD_CASE (fixP->fx_r_type);
2478 case BFD_RELOC_MMIX_JMP:
2479 /* If this fixup is out of range, punt to the linker to emit an
2480 error. This should only happen with -no-expand. */
2481 if (val < -(((offsetT) 1 << 27)/2)
2482 || val >= ((offsetT) 1 << 27)/2 - 1
2485 if (warn_on_expansion)
2486 as_warn_where (fixP->fx_file, fixP->fx_line,
2487 _("operand out of range"));
2491 mmix_set_jmp_offset (buf, val);
2494 case BFD_RELOC_MMIX_REG_OR_BYTE:
2495 if (fixP->fx_addsy != NULL
2496 && (S_GET_SEGMENT (fixP->fx_addsy) != reg_section
2497 || S_GET_VALUE (fixP->fx_addsy) > 255)
2498 && S_GET_SEGMENT (fixP->fx_addsy) != absolute_section)
2500 as_bad_where (fixP->fx_file, fixP->fx_line,
2501 _("invalid operands"));
2502 /* We don't want this "symbol" appearing in output, because
2509 /* If this reloc is for a Z field, we need to adjust
2510 the opcode if we got a constant here.
2511 FIXME: Can we make this more robust? */
2513 if ((fixP->fx_where & 3) == 3
2514 && (fixP->fx_addsy == NULL
2515 || S_GET_SEGMENT (fixP->fx_addsy) == absolute_section))
2516 buf[-3] |= IMM_OFFSET_BIT;
2519 case BFD_RELOC_MMIX_REG:
2520 if (fixP->fx_addsy == NULL
2521 || S_GET_SEGMENT (fixP->fx_addsy) != reg_section
2522 || S_GET_VALUE (fixP->fx_addsy) > 255)
2524 as_bad_where (fixP->fx_file, fixP->fx_line,
2525 _("invalid operands"));
2532 case BFD_RELOC_MMIX_BASE_PLUS_OFFSET:
2533 /* These are never "fixed". */
2537 case BFD_RELOC_MMIX_PUSHJ_1:
2538 case BFD_RELOC_MMIX_PUSHJ_2:
2539 case BFD_RELOC_MMIX_PUSHJ_3:
2540 case BFD_RELOC_MMIX_CBRANCH_J:
2541 case BFD_RELOC_MMIX_CBRANCH_1:
2542 case BFD_RELOC_MMIX_CBRANCH_2:
2543 case BFD_RELOC_MMIX_CBRANCH_3:
2544 case BFD_RELOC_MMIX_GETA_1:
2545 case BFD_RELOC_MMIX_GETA_2:
2546 case BFD_RELOC_MMIX_GETA_3:
2547 case BFD_RELOC_MMIX_JMP_1:
2548 case BFD_RELOC_MMIX_JMP_2:
2549 case BFD_RELOC_MMIX_JMP_3:
2551 BAD_CASE (fixP->fx_r_type);
2556 /* Make sure that for completed fixups we have the value around for
2557 use by e.g. mmix_frob_file. */
2558 fixP->fx_offset = val;
2561 /* A bsearch function for looking up a value against offsets for GREG
2565 cmp_greg_val_greg_symbol_fixes (p1, p2)
2569 offsetT val1 = *(offsetT *) p1;
2570 offsetT val2 = ((struct mmix_symbol_greg_fixes *) p2)->offs;
2572 if (val1 >= val2 && val1 < val2 + 255)
2581 /* Generate a machine-dependent relocation. */
2584 tc_gen_reloc (section, fixP)
2585 asection *section ATTRIBUTE_UNUSED;
2590 + (fixP->fx_addsy != NULL
2591 && !S_IS_WEAK (fixP->fx_addsy)
2592 && !S_IS_COMMON (fixP->fx_addsy)
2593 ? S_GET_VALUE (fixP->fx_addsy) : 0);
2595 bfd_reloc_code_real_type code = BFD_RELOC_NONE;
2596 char *buf = fixP->fx_where + fixP->fx_frag->fr_literal;
2597 symbolS *addsy = fixP->fx_addsy;
2598 asection *addsec = addsy == NULL ? NULL : S_GET_SEGMENT (addsy);
2599 asymbol *baddsy = addsy != NULL ? symbol_get_bfdsym (addsy) : NULL;
2601 = val - (baddsy == NULL || S_IS_COMMON (addsy) || S_IS_WEAK (addsy)
2602 ? 0 : bfd_asymbol_value (baddsy));
2604 /* A single " LOCAL expression" in the wrong section will not work when
2605 linking to MMO; relocations for zero-content sections are then
2606 ignored. Normally, relocations would modify section contents, and
2607 you'd never think or be able to do something like that. The
2608 relocation resulting from a LOCAL directive doesn't have an obvious
2609 and mandatory location. I can't figure out a way to do this better
2610 than just helping the user around this limitation here; hopefully the
2611 code using the local expression is around. Putting the LOCAL
2612 semantics in a relocation still seems right; a section didn't do. */
2613 if (bfd_section_size (section->owner, section) == 0)
2615 (fixP->fx_file, fixP->fx_line,
2616 fixP->fx_r_type == BFD_RELOC_MMIX_LOCAL
2617 /* The BFD_RELOC_MMIX_LOCAL-specific message is supposed to be
2618 user-friendly, though a little bit non-substantial. */
2619 ? _("directive LOCAL must be placed in code or data")
2620 : _("internal confusion: relocation in a section without contents"));
2622 /* FIXME: Range tests for all these. */
2623 switch (fixP->fx_r_type)
2630 code = fixP->fx_r_type;
2632 if (addsy == NULL || bfd_is_abs_section (addsec))
2634 /* Resolve this reloc now, as md_apply_fix3 would have done (not
2635 called if -linkrelax). There is no point in keeping a reloc
2636 to an absolute symbol. No reloc that is subject to
2637 relaxation must be to an absolute symbol; difference
2638 involving symbols in a specific section must be signalled as
2639 an error if the relaxing cannot be expressed; having a reloc
2640 to the resolved (now absolute) value does not help. */
2641 md_number_to_chars (buf, val, fixP->fx_size);
2646 case BFD_RELOC_64_PCREL:
2647 case BFD_RELOC_32_PCREL:
2648 case BFD_RELOC_24_PCREL:
2649 case BFD_RELOC_16_PCREL:
2650 case BFD_RELOC_8_PCREL:
2651 case BFD_RELOC_MMIX_LOCAL:
2652 case BFD_RELOC_VTABLE_INHERIT:
2653 case BFD_RELOC_VTABLE_ENTRY:
2654 case BFD_RELOC_MMIX_GETA:
2655 case BFD_RELOC_MMIX_GETA_1:
2656 case BFD_RELOC_MMIX_GETA_2:
2657 case BFD_RELOC_MMIX_GETA_3:
2658 case BFD_RELOC_MMIX_CBRANCH:
2659 case BFD_RELOC_MMIX_CBRANCH_J:
2660 case BFD_RELOC_MMIX_CBRANCH_1:
2661 case BFD_RELOC_MMIX_CBRANCH_2:
2662 case BFD_RELOC_MMIX_CBRANCH_3:
2663 case BFD_RELOC_MMIX_PUSHJ:
2664 case BFD_RELOC_MMIX_PUSHJ_1:
2665 case BFD_RELOC_MMIX_PUSHJ_2:
2666 case BFD_RELOC_MMIX_PUSHJ_3:
2667 case BFD_RELOC_MMIX_JMP:
2668 case BFD_RELOC_MMIX_JMP_1:
2669 case BFD_RELOC_MMIX_JMP_2:
2670 case BFD_RELOC_MMIX_JMP_3:
2671 case BFD_RELOC_MMIX_ADDR19:
2672 case BFD_RELOC_MMIX_ADDR27:
2673 code = fixP->fx_r_type;
2676 case BFD_RELOC_MMIX_REG_OR_BYTE:
2677 /* If we have this kind of relocation to an unknown symbol or to the
2678 register contents section (that is, to a register), then we can't
2679 resolve the relocation here. */
2681 && (bfd_is_und_section (addsec)
2682 || strcmp (bfd_get_section_name (addsec->owner, addsec),
2683 MMIX_REG_CONTENTS_SECTION_NAME) == 0))
2685 code = fixP->fx_r_type;
2689 /* If the relocation is not to the register section or to the
2690 absolute section (a numeric value), then we have an error. */
2692 && (S_GET_SEGMENT (addsy) != real_reg_section
2695 && ! bfd_is_abs_section (addsec))
2698 /* Set the "immediate" bit of the insn if this relocation is to Z
2699 field when the value is a numeric value, i.e. not a register. */
2700 if ((fixP->fx_where & 3) == 3
2701 && (addsy == NULL || bfd_is_abs_section (addsec)))
2702 buf[-3] |= IMM_OFFSET_BIT;
2707 case BFD_RELOC_MMIX_BASE_PLUS_OFFSET:
2709 && strcmp (bfd_get_section_name (addsec->owner, addsec),
2710 MMIX_REG_CONTENTS_SECTION_NAME) == 0)
2712 /* This changed into a register; the relocation is for the
2713 register-contents section. The constant part remains zero. */
2714 code = BFD_RELOC_MMIX_REG;
2718 /* If we've found out that this was indeed a register, then replace
2719 with the register number. The constant part is already zero.
2721 If we encounter any other defined symbol, then we must find a
2722 suitable register and emit a reloc. */
2723 if (addsy == NULL || addsec != real_reg_section)
2725 struct mmix_symbol_gregs *gregs;
2726 struct mmix_symbol_greg_fixes *fix;
2728 if (S_IS_DEFINED (addsy)
2729 && !bfd_is_com_section (addsec)
2730 && !S_IS_WEAK (addsy))
2732 if (! symbol_section_p (addsy) && ! bfd_is_abs_section (addsec))
2733 as_fatal (_("internal: BFD_RELOC_MMIX_BASE_PLUS_OFFSET not resolved to section"));
2735 /* If this is an absolute symbol sufficiently near
2736 lowest_data_loc, then we canonicalize on the data
2737 section. Note that val is signed here; we may subtract
2738 lowest_data_loc which is unsigned. Careful with those
2740 if (lowest_data_loc != (bfd_vma) -1
2741 && (bfd_vma) val + 256 > lowest_data_loc
2742 && bfd_is_abs_section (addsec))
2744 val -= (offsetT) lowest_data_loc;
2745 addsy = section_symbol (data_section);
2747 /* Likewise text section. */
2748 else if (lowest_text_loc != (bfd_vma) -1
2749 && (bfd_vma) val + 256 > lowest_text_loc
2750 && bfd_is_abs_section (addsec))
2752 val -= (offsetT) lowest_text_loc;
2753 addsy = section_symbol (text_section);
2757 gregs = *symbol_get_tc (addsy);
2759 /* If that symbol does not have any associated GREG definitions,
2760 we can't do anything. */
2762 || (fix = bsearch (&val, gregs->greg_fixes, gregs->n_gregs,
2763 sizeof (gregs->greg_fixes[0]),
2764 cmp_greg_val_greg_symbol_fixes)) == NULL
2765 /* The register must not point *after* the address we want. */
2767 /* Neither must the register point more than 255 bytes
2768 before the address we want. */
2769 || fix->offs + 255 < val)
2771 /* We can either let the linker allocate GREGs
2772 automatically, or emit an error. */
2773 if (allocate_undefined_gregs_in_linker)
2775 /* The values in baddsy and addend are right. */
2776 code = fixP->fx_r_type;
2780 as_bad_where (fixP->fx_file, fixP->fx_line,
2781 _("no suitable GREG definition for operands"));
2786 /* Transform the base-plus-offset reloc for the actual area
2787 to a reloc for the register with the address of the area.
2788 Put addend for register in Z operand. */
2789 buf[1] = val - fix->offs;
2790 code = BFD_RELOC_MMIX_REG;
2792 = (bfd_get_section_by_name (stdoutput,
2793 MMIX_REG_CONTENTS_SECTION_NAME)
2796 addend = fix->fix->fx_frag->fr_address + fix->fix->fx_where;
2799 else if (S_GET_VALUE (addsy) > 255)
2800 as_bad_where (fixP->fx_file, fixP->fx_line,
2801 _("invalid operands"));
2809 case BFD_RELOC_MMIX_REG:
2811 && (bfd_is_und_section (addsec)
2812 || strcmp (bfd_get_section_name (addsec->owner, addsec),
2813 MMIX_REG_CONTENTS_SECTION_NAME) == 0))
2815 code = fixP->fx_r_type;
2820 && (addsec != real_reg_section
2823 && ! bfd_is_und_section (addsec))
2824 /* Drop through to error message. */
2833 /* The others are supposed to be handled by md_apply_fix3.
2834 FIXME: ... which isn't called when -linkrelax. Move over
2835 md_apply_fix3 code here for everything reasonable. */
2839 (fixP->fx_file, fixP->fx_line,
2840 _("operands were not reducible at assembly-time"));
2842 /* Unmark this symbol as used in a reloc, so we don't bump into a BFD
2843 assert when trying to output reg_section. FIXME: A gas bug. */
2844 fixP->fx_addsy = NULL;
2848 relP = (arelent *) xmalloc (sizeof (arelent));
2850 relP->sym_ptr_ptr = (asymbol **) xmalloc (sizeof (asymbol *));
2851 *relP->sym_ptr_ptr = baddsy;
2852 relP->address = fixP->fx_frag->fr_address + fixP->fx_where;
2854 relP->addend = addend;
2856 /* If this had been a.out, we would have had a kludge for weak symbols
2859 relP->howto = bfd_reloc_type_lookup (stdoutput, code);
2864 name = S_GET_NAME (addsy);
2866 name = _("<unknown>");
2867 as_fatal (_("cannot generate relocation type for symbol %s, code %s"),
2868 name, bfd_get_reloc_code_name (code));
2874 /* Do some reformatting of a line. FIXME: We could transform a mmixal
2875 line into traditional (GNU?) format, unless #NO_APP, and get rid of all
2876 ugly labels_without_colons etc. */
2879 mmix_handle_mmixal ()
2881 char *s0 = input_line_pointer;
2886 if (pending_label != NULL)
2887 as_fatal (_("internal: unhandled label %s"), pending_label);
2889 if (mmix_gnu_syntax)
2892 /* If the first character is a '.', then it's a pseudodirective, not a
2893 label. Make GAS not handle label-without-colon on this line. We
2894 also don't do mmixal-specific stuff on this line. */
2895 if (input_line_pointer[0] == '.')
2897 label_without_colon_this_line = 0;
2901 /* Don't handle empty lines here. */
2904 if (*s0 == 0 || is_end_of_line[(unsigned int) *s0])
2907 if (! ISSPACE (*s0))
2913 /* If we're on a line with a label, check if it's a mmixal fb-label.
2914 Save an indicator and skip the label; it must be set only after all
2915 fb-labels of expressions are evaluated. */
2916 if (ISDIGIT (input_line_pointer[0])
2917 && input_line_pointer[1] == 'H'
2918 && ISSPACE (input_line_pointer[2]))
2921 current_fb_label = input_line_pointer[0] - '0';
2923 /* We have to skip the label, but also preserve the newlineness of
2924 the previous character, since the caller checks that. It's a
2925 mess we blame on the caller. */
2926 input_line_pointer[1] = input_line_pointer[-1];
2927 input_line_pointer += 2;
2929 s = input_line_pointer;
2930 while (*s && ISSPACE (*s) && ! is_end_of_line[(unsigned int) *s])
2933 /* For errors emitted here, the book-keeping is off by one; the
2934 caller is about to bump the counters. Adjust the error messages. */
2935 if (is_end_of_line[(unsigned int) *s])
2939 as_where (&name, &line);
2940 as_bad_where (name, line + 1,
2941 _("[0-9]H labels may not appear alone on a line"));
2942 current_fb_label = -1;
2948 as_where (&name, &line);
2949 as_bad_where (name, line + 1,
2950 _("[0-9]H labels do not mix with dot-pseudos"));
2951 current_fb_label = -1;
2956 current_fb_label = -1;
2957 if (is_name_beginner (input_line_pointer[0]))
2958 label = input_line_pointer;
2961 s0 = input_line_pointer;
2962 /* Skip over label. */
2963 while (*s0 && is_part_of_name (*s0))
2966 /* Remove trailing ":" off labels, as they'd otherwise be considered
2967 part of the name. But don't do it for local labels. */
2968 if (s0 != input_line_pointer && s0[-1] == ':'
2969 && (s0 - 2 != input_line_pointer
2970 || ! ISDIGIT (s0[-2])))
2972 else if (label != NULL)
2974 /* For labels that don't end in ":", we save it so we can later give
2975 it the same alignment and address as the associated instruction. */
2977 /* Make room for the label including the ending nul. */
2978 int len_0 = s0 - label + 1;
2980 /* Save this label on the MMIX symbol obstack. Saving it on an
2981 obstack is needless for "IS"-pseudos, but it's harmless and we
2982 avoid a little code-cluttering. */
2983 obstack_grow (&mmix_sym_obstack, label, len_0);
2984 pending_label = obstack_finish (&mmix_sym_obstack);
2985 pending_label[len_0 - 1] = 0;
2988 while (*s0 && ISSPACE (*s0) && ! is_end_of_line[(unsigned int) *s0])
2991 if (pending_label != NULL && is_end_of_line[(unsigned int) *s0])
2992 /* Whoops, this was actually a lone label on a line. Like :-ended
2993 labels, we don't attach such labels to the next instruction or
2995 pending_label = NULL;
2997 /* Find local labels of operands. Look for "[0-9][FB]" where the
2998 characters before and after are not part of words. Break if a single
2999 or double quote is seen anywhere. It means we can't have local
3000 labels as part of list with mixed quoted and unquoted members for
3001 mmixal compatibility but we can't have it all. For the moment.
3002 Replace the '<N>B' or '<N>F' with MAGIC_FB_BACKWARD_CHAR<N> and
3003 MAGIC_FB_FORWARD_CHAR<N> respectively. */
3005 /* First make sure we don't have any of the magic characters on the line
3006 appearing as input. */
3011 if (is_end_of_line[(unsigned int) c])
3013 if (c == MAGIC_FB_BACKWARD_CHAR || c == MAGIC_FB_FORWARD_CHAR)
3014 as_bad (_("invalid characters in input"));
3017 /* Scan again, this time looking for ';' after operands. */
3020 /* Skip the insn. */
3024 && ! is_end_of_line[(unsigned int) *s])
3027 /* Skip the spaces after the insn. */
3031 && ! is_end_of_line[(unsigned int) *s])
3034 /* Skip the operands. While doing this, replace [0-9][BF] with
3035 (MAGIC_FB_BACKWARD_CHAR|MAGIC_FB_FORWARD_CHAR)[0-9]. */
3036 while ((c = *s) != 0
3039 && ! is_end_of_line[(unsigned int) c])
3045 /* FIXME: Test-case for semi-colon in string. */
3048 && (! is_end_of_line[(unsigned int) *s] || *s == ';'))
3054 else if (ISDIGIT (c))
3056 if ((s[1] != 'B' && s[1] != 'F')
3057 || is_part_of_name (s[-1])
3058 || is_part_of_name (s[2]))
3063 ? MAGIC_FB_BACKWARD_CHAR : MAGIC_FB_FORWARD_CHAR);
3071 /* Skip any spaces after the operands. */
3075 && !is_end_of_line[(unsigned int) *s])
3078 /* If we're now looking at a semi-colon, then it's an end-of-line
3080 mmix_next_semicolon_is_eoln = (*s == ';');
3082 /* Make IS into an EQU by replacing it with "= ". Only match upper-case
3083 though; let lower-case be a syntax error. */
3085 if (s[0] == 'I' && s[1] == 'S' && ISSPACE (s[2]))
3090 /* Since labels can start without ":", we have to handle "X IS 42"
3091 in full here, or "X" will be parsed as a label to be set at ".". */
3092 input_line_pointer = s;
3094 /* Right after this function ends, line numbers will be bumped if
3095 input_line_pointer[-1] = '\n'. We want accurate line numbers for
3096 the equals call, so we bump them before the call, and make sure
3097 they aren't bumped afterwards. */
3098 bump_line_counters ();
3100 /* A fb-label is valid as an IS-label. */
3101 if (current_fb_label >= 0)
3105 /* We need to save this name on our symbol obstack, since the
3106 string we got in fb_label_name is volatile and will change
3107 with every call to fb_label_name, like those resulting from
3108 parsing the IS-operand. */
3109 fb_name = fb_label_name (current_fb_label, 1);
3110 obstack_grow (&mmix_sym_obstack, fb_name, strlen (fb_name) + 1);
3111 equals (obstack_finish (&mmix_sym_obstack), 0);
3112 fb_label_instance_inc (current_fb_label);
3113 current_fb_label = -1;
3117 if (pending_label == NULL)
3118 as_bad (_("empty label field for IS"));
3120 equals (pending_label, 0);
3121 pending_label = NULL;
3124 /* For mmixal, we can have comments without a comment-start
3126 mmix_handle_rest_of_empty_line ();
3127 input_line_pointer--;
3129 input_line_pointer[-1] = ' ';
3131 else if (s[0] == 'G'
3133 && strncmp (s, "GREG", 4) == 0
3134 && (ISSPACE (s[4]) || is_end_of_line[(unsigned char) s[4]]))
3136 input_line_pointer = s + 4;
3138 /* Right after this function ends, line numbers will be bumped if
3139 input_line_pointer[-1] = '\n'. We want accurate line numbers for
3140 the s_greg call, so we bump them before the call, and make sure
3141 they aren't bumped afterwards. */
3142 bump_line_counters ();
3144 /* A fb-label is valid as a GREG-label. */
3145 if (current_fb_label >= 0)
3149 /* We need to save this name on our symbol obstack, since the
3150 string we got in fb_label_name is volatile and will change
3151 with every call to fb_label_name, like those resulting from
3152 parsing the IS-operand. */
3153 fb_name = fb_label_name (current_fb_label, 1);
3155 /* Make sure we save the canonical name and don't get bitten by
3157 obstack_1grow (&mmix_sym_obstack, ':');
3158 obstack_grow (&mmix_sym_obstack, fb_name, strlen (fb_name) + 1);
3159 mmix_greg_internal (obstack_finish (&mmix_sym_obstack));
3160 fb_label_instance_inc (current_fb_label);
3161 current_fb_label = -1;
3164 mmix_greg_internal (pending_label);
3166 /* Back up before the end-of-line marker that was skipped in
3167 mmix_greg_internal. */
3168 input_line_pointer--;
3169 input_line_pointer[-1] = ' ';
3171 pending_label = NULL;
3173 else if (pending_label != NULL)
3175 input_line_pointer += strlen (pending_label);
3177 /* See comment above about getting line numbers bumped. */
3178 input_line_pointer[-1] = '\n';
3182 /* Give the value of an fb-label rewritten as in mmix_handle_mmixal, when
3183 parsing an expression.
3185 On valid calls, input_line_pointer points at a MAGIC_FB_BACKWARD_CHAR
3186 or MAGIC_FB_BACKWARD_CHAR, followed by an ascii digit for the label.
3187 We fill in the label as an expression. */
3190 mmix_fb_label (expP)
3194 char *fb_internal_name;
3196 /* This doesn't happen when not using mmixal syntax. */
3198 || (input_line_pointer[0] != MAGIC_FB_BACKWARD_CHAR
3199 && input_line_pointer[0] != MAGIC_FB_FORWARD_CHAR))
3202 /* The current backward reference has augmentation 0. A forward
3203 reference has augmentation 1, unless it's the same as a fb-label on
3204 _this_ line, in which case we add one more so we don't refer to it.
3205 This is the semantics of mmixal; it differs to that of common
3206 fb-labels which refer to a here-label on the current line as a
3207 backward reference. */
3209 = fb_label_name (input_line_pointer[1] - '0',
3210 (input_line_pointer[0] == MAGIC_FB_FORWARD_CHAR ? 1 : 0)
3211 + ((input_line_pointer[1] - '0' == current_fb_label
3212 && input_line_pointer[0] == MAGIC_FB_FORWARD_CHAR)
3215 input_line_pointer += 2;
3216 sym = symbol_find_or_make (fb_internal_name);
3218 /* We don't have to clean up unrelated fields here; we just do what the
3219 expr machinery does, but *not* just what it does for [0-9][fb], since
3220 we need to treat those as ordinary symbols sometimes; see testcases
3221 err-byte2.s and fb-2.s. */
3222 if (S_GET_SEGMENT (sym) == absolute_section)
3224 expP->X_op = O_constant;
3225 expP->X_add_number = S_GET_VALUE (sym);
3229 expP->X_op = O_symbol;
3230 expP->X_add_symbol = sym;
3231 expP->X_add_number = 0;
3235 /* See whether we need to force a relocation into the output file.
3236 This is used to force out switch and PC relative relocations when
3240 mmix_force_relocation (fixP)
3243 if (fixP->fx_r_type == BFD_RELOC_MMIX_LOCAL
3244 || fixP->fx_r_type == BFD_RELOC_VTABLE_INHERIT
3245 || fixP->fx_r_type == BFD_RELOC_VTABLE_ENTRY
3246 || fixP->fx_r_type == BFD_RELOC_MMIX_BASE_PLUS_OFFSET)
3252 /* All our pcrel relocations are must-keep. Note that md_apply_fix3 is
3253 called *after* this, and will handle getting rid of the presumed
3254 reloc; a relocation isn't *forced* other than to be handled by
3255 md_apply_fix3 (or tc_gen_reloc if linkrelax). */
3259 return S_FORCE_RELOC (fixP->fx_addsy);
3262 /* The location from which a PC relative jump should be calculated,
3263 given a PC relative reloc. */
3266 md_pcrel_from_section (fixP, sec)
3270 if (fixP->fx_addsy != (symbolS *) NULL
3271 && (! S_IS_DEFINED (fixP->fx_addsy)
3272 || S_GET_SEGMENT (fixP->fx_addsy) != sec))
3274 /* The symbol is undefined (or is defined but not in this section).
3275 Let the linker figure it out. */
3279 return (fixP->fx_frag->fr_address + fixP->fx_where);
3282 /* Adjust the symbol table. We make reg_section relative to the real
3283 register section. */
3286 mmix_adjust_symtab ()
3289 symbolS *regsec = section_symbol (reg_section);
3291 for (sym = symbol_rootP; sym != NULL; sym = symbol_next (sym))
3292 if (S_GET_SEGMENT (sym) == reg_section)
3296 if (S_IS_EXTERN (sym) || symbol_used_in_reloc_p (sym))
3298 symbol_remove (sym, &symbol_rootP, &symbol_lastP);
3301 /* Change section to the *real* register section, so it gets
3302 proper treatment when writing it out. Only do this for
3303 global symbols. This also means we don't have to check for
3305 S_SET_SEGMENT (sym, real_reg_section);
3309 /* This is the expansion of LABELS_WITHOUT_COLONS.
3310 We let md_start_line_hook tweak label_without_colon_this_line, and then
3311 this function returns the tweaked value, and sets it to 1 for the next
3312 line. FIXME: Very, very brittle. Not sure it works the way I
3313 thought at the time I first wrote this. */
3316 mmix_label_without_colon_this_line ()
3318 int retval = label_without_colon_this_line;
3320 if (! mmix_gnu_syntax)
3321 label_without_colon_this_line = 1;
3326 /* This is the expansion of md_relax_frag. We go through the ordinary
3327 relax table function except when the frag is for a GREG. Then we have
3328 to check whether there's another GREG by the same value that we can
3332 mmix_md_relax_frag (seg, fragP, stretch)
3337 if (fragP->fr_subtype != STATE_GREG_DEF
3338 && fragP->fr_subtype != STATE_GREG_UNDF)
3339 return relax_frag (seg, fragP, stretch);
3341 /* If we're defined, we don't grow. */
3342 if (fragP->fr_subtype == STATE_GREG_DEF)
3345 as_fatal (_("internal: unexpected relax type %d:%d"),
3346 fragP->fr_type, fragP->fr_subtype);
3350 /* Various things we punt until all input is seen. */
3359 /* The first frag of GREG:s going into the register contents section. */
3360 fragS *mmix_reg_contents_frags = NULL;
3362 /* Reset prefix. All labels reachable at this point must be
3364 mmix_current_prefix = NULL;
3367 as_bad_where (bspec_file, bspec_line, _("BSPEC without ESPEC."));
3369 /* Emit the low LOC setting of .text. */
3370 if (text_has_contents && lowest_text_loc != (bfd_vma) -1)
3373 char locsymbol[sizeof (":") - 1
3374 + sizeof (MMIX_LOC_SECTION_START_SYMBOL_PREFIX) - 1
3375 + sizeof (".text")];
3377 /* An exercise in non-ISO-C-ness, this one. */
3378 sprintf (locsymbol, ":%s%s", MMIX_LOC_SECTION_START_SYMBOL_PREFIX,
3381 = symbol_new (locsymbol, absolute_section, lowest_text_loc,
3382 &zero_address_frag);
3383 S_SET_EXTERNAL (symbolP);
3387 if (data_has_contents && lowest_data_loc != (bfd_vma) -1)
3390 char locsymbol[sizeof (":") - 1
3391 + sizeof (MMIX_LOC_SECTION_START_SYMBOL_PREFIX) - 1
3392 + sizeof (".data")];
3394 sprintf (locsymbol, ":%s%s", MMIX_LOC_SECTION_START_SYMBOL_PREFIX,
3397 = symbol_new (locsymbol, absolute_section, lowest_data_loc,
3398 &zero_address_frag);
3399 S_SET_EXTERNAL (symbolP);
3402 /* Unless GNU syntax mode, set "Main" to be a function, so the
3403 disassembler doesn't get confused when we write truly
3404 mmixal-compatible code (and don't use .type). Similarly set it
3405 global (regardless of -globalize-symbols), so the linker sees it as
3406 the start symbol in ELF mode. */
3407 mainsym = symbol_find (MMIX_START_SYMBOL_NAME);
3408 if (mainsym != NULL && ! mmix_gnu_syntax)
3410 symbol_get_bfdsym (mainsym)->flags |= BSF_FUNCTION;
3411 S_SET_EXTERNAL (mainsym);
3414 if (n_of_raw_gregs != 0)
3416 /* Emit GREGs. They are collected in order of appearance, but must
3417 be emitted in opposite order to both have section address regno*8
3418 and the same allocation order (within a file) as mmixal. */
3419 segT this_segment = now_seg;
3420 subsegT this_subsegment = now_subseg;
3422 = bfd_make_section_old_way (stdoutput,
3423 MMIX_REG_CONTENTS_SECTION_NAME);
3424 subseg_set (regsec, 0);
3426 /* Finally emit the initialization-value. Emit a variable frag, which
3427 we'll fix in md_estimate_size_before_relax. We set the initializer
3428 for the tc_frag_data field to NULL, so we can use that field for
3429 relaxation purposes. */
3430 mmix_opcode_frag = NULL;
3433 mmix_reg_contents_frags = frag_now;
3435 for (i = n_of_raw_gregs - 1; i >= 0; i--)
3437 if (mmix_raw_gregs[i].label != NULL)
3438 /* There's a symbol. Let it refer to this location in the
3439 register contents section. The symbol must be globalized
3441 colon (mmix_raw_gregs[i].label);
3443 frag_var (rs_machine_dependent, 8, 0, STATE_GREG_UNDF,
3444 make_expr_symbol (&mmix_raw_gregs[i].exp), 0, NULL);
3447 subseg_set (this_segment, this_subsegment);
3450 /* Iterate over frags resulting from GREGs and move those that evidently
3451 have the same value together and point one to another.
3453 This works in time O(N^2) but since the upper bound for non-error use
3454 is 223, it's best to keep this simpler algorithm. */
3455 for (fragP = mmix_reg_contents_frags; fragP != NULL; fragP = fragP->fr_next)
3462 symbolS *symbolP = fragP->fr_symbol;
3464 if (fragP->fr_type != rs_machine_dependent
3465 || fragP->fr_subtype != STATE_GREG_UNDF)
3468 /* Whatever the outcome, we will have this GREG judged merged or
3469 non-merged. Since the tc_frag_data is NULL at this point, we
3470 default to non-merged. */
3471 fragP->fr_subtype = STATE_GREG_DEF;
3473 /* If we're not supposed to merge GREG definitions, then just don't
3474 look for equivalents. */
3478 osymval = (offsetT) S_GET_VALUE (symbolP);
3479 osymfrag = symbol_get_frag (symbolP);
3481 /* If the symbol isn't defined, we can't say that another symbol
3482 equals this frag, then. FIXME: We can look at the "deepest"
3483 defined name; if a = c and b = c then obviously a == b. */
3484 if (! S_IS_DEFINED (symbolP))
3487 oexpP = symbol_get_value_expression (fragP->fr_symbol);
3489 /* If the initialization value is zero, then we must not merge them. */
3490 if (oexpP->X_op == O_constant && osymval == 0)
3493 /* Iterate through the frags downward this one. If we find one that
3494 has the same non-zero value, move it to after this one and point
3495 to it as the equivalent. */
3496 for (fpp = &fragP->fr_next; *fpp != NULL; fpp = &fpp[0]->fr_next)
3500 if (fp->fr_type != rs_machine_dependent
3501 || fp->fr_subtype != STATE_GREG_UNDF)
3504 /* Calling S_GET_VALUE may simplify the symbol, changing from
3505 expr_section etc. so call it first. */
3506 if ((offsetT) S_GET_VALUE (fp->fr_symbol) == osymval
3507 && symbol_get_frag (fp->fr_symbol) == osymfrag)
3509 /* Move the frag links so the one we found equivalent comes
3510 after the current one, carefully considering that
3511 sometimes fpp == &fragP->fr_next and the moves must be a
3514 fp->fr_next = fragP->fr_next;
3515 fragP->fr_next = fp;
3521 fragP->tc_frag_data = fp;
3525 /* qsort function for mmix_symbol_gregs. */
3528 cmp_greg_symbol_fixes (parg, qarg)
3532 const struct mmix_symbol_greg_fixes *p
3533 = (const struct mmix_symbol_greg_fixes *) parg;
3534 const struct mmix_symbol_greg_fixes *q
3535 = (const struct mmix_symbol_greg_fixes *) qarg;
3537 return p->offs > q->offs ? 1 : p->offs < q->offs ? -1 : 0;
3540 /* Collect GREG definitions from mmix_gregs and hang them as lists sorted
3541 on increasing offsets onto each section symbol or undefined symbol.
3543 Also, remove the register convenience section so it doesn't get output
3544 as an ELF section. */
3550 struct mmix_symbol_gregs *all_greg_symbols[MAX_GREGS];
3551 int n_greg_symbols = 0;
3553 /* Collect all greg fixups and decorate each corresponding symbol with
3554 the greg fixups for it. */
3555 for (i = 0; i < n_of_cooked_gregs; i++)
3559 struct mmix_symbol_gregs *gregs;
3562 fixP = mmix_gregs[i];
3563 know (fixP->fx_r_type == BFD_RELOC_64);
3565 /* This case isn't doable in general anyway, methinks. */
3566 if (fixP->fx_subsy != NULL)
3568 as_bad_where (fixP->fx_file, fixP->fx_line,
3569 _("GREG expression too complicated"));
3573 sym = fixP->fx_addsy;
3574 offs = (offsetT) fixP->fx_offset;
3576 /* If the symbol is defined, then it must be resolved to a section
3577 symbol at this time, or else we don't know how to handle it. */
3578 if (S_IS_DEFINED (sym)
3579 && !bfd_is_com_section (S_GET_SEGMENT (sym))
3580 && !S_IS_WEAK (sym))
3582 if (! symbol_section_p (sym)
3583 && ! bfd_is_abs_section (S_GET_SEGMENT (sym)))
3584 as_fatal (_("internal: GREG expression not resolved to section"));
3586 offs += S_GET_VALUE (sym);
3589 /* If this is an absolute symbol sufficiently near lowest_data_loc,
3590 then we canonicalize on the data section. Note that offs is
3591 signed here; we may subtract lowest_data_loc which is unsigned.
3592 Careful with those comparisons. */
3593 if (lowest_data_loc != (bfd_vma) -1
3594 && (bfd_vma) offs + 256 > lowest_data_loc
3595 && bfd_is_abs_section (S_GET_SEGMENT (sym)))
3597 offs -= (offsetT) lowest_data_loc;
3598 sym = section_symbol (data_section);
3600 /* Likewise text section. */
3601 else if (lowest_text_loc != (bfd_vma) -1
3602 && (bfd_vma) offs + 256 > lowest_text_loc
3603 && bfd_is_abs_section (S_GET_SEGMENT (sym)))
3605 offs -= (offsetT) lowest_text_loc;
3606 sym = section_symbol (text_section);
3609 gregs = *symbol_get_tc (sym);
3613 gregs = xmalloc (sizeof (*gregs));
3615 symbol_set_tc (sym, &gregs);
3616 all_greg_symbols[n_greg_symbols++] = gregs;
3619 gregs->greg_fixes[gregs->n_gregs].fix = fixP;
3620 gregs->greg_fixes[gregs->n_gregs++].offs = offs;
3623 /* For each symbol having a GREG definition, sort those definitions on
3625 for (i = 0; i < n_greg_symbols; i++)
3626 qsort (all_greg_symbols[i]->greg_fixes, all_greg_symbols[i]->n_gregs,
3627 sizeof (all_greg_symbols[i]->greg_fixes[0]), cmp_greg_symbol_fixes);
3629 if (real_reg_section != NULL)
3633 /* FIXME: Pass error state gracefully. */
3634 if (bfd_get_section_flags (stdoutput, real_reg_section) & SEC_HAS_CONTENTS)
3635 as_fatal (_("register section has contents\n"));
3637 /* Really remove the section. */
3638 for (secpp = &stdoutput->sections;
3639 *secpp != real_reg_section;
3640 secpp = &(*secpp)->next)
3642 bfd_section_list_remove (stdoutput, secpp);
3643 --stdoutput->section_count;
3648 /* Provide an expression for a built-in name provided when-used.
3649 Either a symbol that is a handler; living in 0x10*[1..8] and having
3650 name [DVWIOUZX]_Handler, or a mmixal built-in symbol.
3652 If the name isn't a built-in name and parsed into *EXPP, return zero. */
3655 mmix_parse_predefined_name (name, expP)
3660 char *handler_charp;
3661 const char handler_chars[] = "DVWIOUZX";
3664 if (! predefined_syms)
3667 canon_name = tc_canonicalize_symbol_name (name);
3669 if (canon_name[1] == '_'
3670 && strcmp (canon_name + 2, "Handler") == 0
3671 && (handler_charp = strchr (handler_chars, *canon_name)) != NULL)
3673 /* If the symbol doesn't exist, provide one relative to the .text
3676 FIXME: We should provide separate sections, mapped in the linker
3678 symp = symbol_find (name);
3680 symp = symbol_new (name, text_section,
3681 0x10 * (handler_charp + 1 - handler_chars),
3682 &zero_address_frag);
3686 /* These symbols appear when referenced; needed for
3687 mmixal-compatible programs. */
3694 } predefined_abs_syms[] =
3696 {"Data_Segment", (valueT) 0x20 << 56},
3697 {"Pool_Segment", (valueT) 0x40 << 56},
3698 {"Stack_Segment", (valueT) 0x60 << 56},
3706 {"BinaryReadWrite", 4},
3729 /* If it's already in the symbol table, we shouldn't do anything. */
3730 symp = symbol_find (name);
3735 i < sizeof (predefined_abs_syms) / sizeof (predefined_abs_syms[0]);
3737 if (strcmp (canon_name, predefined_abs_syms[i].name) == 0)
3739 symbol_table_insert (symbol_new (predefined_abs_syms[i].name,
3741 predefined_abs_syms[i].val,
3742 &zero_address_frag));
3744 /* Let gas find the symbol we just created, through its
3749 /* Not one of those symbols. Let gas handle it. */
3753 expP->X_op = O_symbol;
3754 expP->X_add_number = 0;
3755 expP->X_add_symbol = symp;
3756 expP->X_op_symbol = NULL;
3761 /* Just check that we don't have a BSPEC/ESPEC pair active when changing
3762 sections "normally", and get knowledge about alignment from the new
3766 mmix_md_elf_section_change_hook ()
3769 as_bad (_("section change from within a BSPEC/ESPEC pair is not supported"));
3771 last_alignment = bfd_get_section_alignment (now_seg->owner, now_seg);
3775 /* The LOC worker. This is like s_org, but we have to support changing
3780 int ignore ATTRIBUTE_UNUSED;
3788 /* Must not have a BSPEC in progress. */
3791 as_bad (_("directive LOC from within a BSPEC/ESPEC pair is not supported"));
3795 section = expression (&exp);
3797 if (exp.X_op == O_illegal
3798 || exp.X_op == O_absent
3799 || exp.X_op == O_big
3800 || section == undefined_section)
3802 as_bad (_("invalid LOC expression"));
3806 if (section == absolute_section)
3808 /* Translate a constant into a suitable section. */
3810 if (exp.X_add_number < ((offsetT) 0x20 << 56))
3812 /* Lower than Data_Segment - assume it's .text. */
3813 section = text_section;
3815 /* Save the lowest seen location, so we can pass on this
3816 information to the linker. We don't actually org to this
3817 location here, we just pass on information to the linker so
3818 it can put the code there for us. */
3820 /* If there was already a loc (that has to be set lower than
3821 this one), we org at (this - lower). There's an implicit
3822 "LOC 0" before any entered code. FIXME: handled by spurious
3823 settings of text_has_contents. */
3824 if (exp.X_add_number < 0
3825 || exp.X_add_number < (offsetT) lowest_text_loc)
3827 as_bad (_("LOC expression stepping backwards is not supported"));
3828 exp.X_op = O_absent;
3832 if (text_has_contents && lowest_text_loc == (bfd_vma) -1)
3833 lowest_text_loc = 0;
3835 if (lowest_text_loc == (bfd_vma) -1)
3837 lowest_text_loc = exp.X_add_number;
3839 /* We want only to change the section, not set an offset. */
3840 exp.X_op = O_absent;
3843 exp.X_add_number -= lowest_text_loc;
3848 /* Do the same for the .data section. */
3849 section = data_section;
3851 if (exp.X_add_number < (offsetT) lowest_data_loc)
3853 as_bad (_("LOC expression stepping backwards is not supported"));
3854 exp.X_op = O_absent;
3858 if (data_has_contents && lowest_data_loc == (bfd_vma) -1)
3859 lowest_data_loc = (bfd_vma) 0x20 << 56;
3861 if (lowest_data_loc == (bfd_vma) -1)
3863 lowest_data_loc = exp.X_add_number;
3865 /* We want only to change the section, not set an offset. */
3866 exp.X_op = O_absent;
3869 exp.X_add_number -= lowest_data_loc;
3874 if (section != now_seg)
3876 obj_elf_section_change_hook ();
3877 subseg_set (section, 0);
3879 /* Call our section change hooks using the official hook. */
3880 md_elf_section_change_hook ();
3883 if (exp.X_op != O_absent)
3885 if (exp.X_op != O_constant && exp.X_op != O_symbol)
3887 /* Handle complex expressions. */
3888 sym = make_expr_symbol (&exp);
3893 sym = exp.X_add_symbol;
3894 off = exp.X_add_number;
3897 p = frag_var (rs_org, 1, 1, (relax_substateT) 0, sym, off, (char *) 0);
3901 mmix_handle_rest_of_empty_line ();
3904 /* The BYTE worker. We have to support sequences of mixed "strings",
3905 numbers and other constant "first-pass" reducible expressions separated
3914 if (now_seg == text_section)
3915 text_has_contents = 1;
3916 else if (now_seg == data_section)
3917 data_has_contents = 1;
3922 switch (*input_line_pointer)
3925 ++input_line_pointer;
3926 start = input_line_pointer;
3927 while (is_a_char (c = next_char_of_string ()))
3929 FRAG_APPEND_1_CHAR (c);
3932 if (input_line_pointer[-1] != '\"')
3934 /* We will only get here in rare cases involving #NO_APP,
3935 where the unterminated string is not recognized by the
3936 preformatting pass. */
3937 as_bad (_("unterminated string"));
3938 mmix_discard_rest_of_line ();
3946 segT expseg = expression (&exp);
3948 /* We have to allow special register names as constant numbers. */
3949 if ((expseg != absolute_section && expseg != reg_section)
3950 || (exp.X_op != O_constant
3951 && (exp.X_op != O_register
3952 || exp.X_add_number <= 255)))
3954 as_bad (_("BYTE expression not a pure number"));
3955 mmix_discard_rest_of_line ();
3958 else if ((exp.X_add_number > 255 && exp.X_op != O_register)
3959 || exp.X_add_number < 0)
3961 /* Note that mmixal does not allow negative numbers in
3962 BYTE sequences, so neither should we. */
3963 as_bad (_("BYTE expression not in the range 0..255"));
3964 mmix_discard_rest_of_line ();
3968 FRAG_APPEND_1_CHAR (exp.X_add_number);
3974 c = *input_line_pointer++;
3978 input_line_pointer--;
3980 if (mmix_gnu_syntax)
3981 demand_empty_rest_of_line ();
3984 mmix_discard_rest_of_line ();
3985 /* Do like demand_empty_rest_of_line and step over the end-of-line
3987 input_line_pointer++;
3990 /* Make sure we align for the next instruction. */
3994 /* Like cons_worker, but we have to ignore "naked comments", not barf on
3995 them. Implements WYDE, TETRA and OCTA. We're a little bit more
3996 lenient than mmix_byte but FIXME: they should eventually merge. */
4005 /* If we don't have any contents, then it's ok to have a specified start
4006 address that is not a multiple of the max data size. We will then
4007 align it as necessary when we get here. Otherwise, it's a fatal sin. */
4008 if (now_seg == text_section)
4010 if (lowest_text_loc != (bfd_vma) -1
4011 && (lowest_text_loc & (nbytes - 1)) != 0)
4013 if (text_has_contents)
4014 as_bad (_("data item with alignment larger than location"));
4015 else if (want_unaligned)
4016 as_bad (_("unaligned data at an absolute location is not supported"));
4018 lowest_text_loc &= ~((bfd_vma) nbytes - 1);
4019 lowest_text_loc += (bfd_vma) nbytes;
4022 text_has_contents = 1;
4024 else if (now_seg == data_section)
4026 if (lowest_data_loc != (bfd_vma) -1
4027 && (lowest_data_loc & (nbytes - 1)) != 0)
4029 if (data_has_contents)
4030 as_bad (_("data item with alignment larger than location"));
4031 else if (want_unaligned)
4032 as_bad (_("unaligned data at an absolute location is not supported"));
4034 lowest_data_loc &= ~((bfd_vma) nbytes - 1);
4035 lowest_data_loc += (bfd_vma) nbytes;
4038 data_has_contents = 1;
4041 /* Always align these unless asked not to (valid for the current pseudo). */
4042 if (! want_unaligned)
4044 last_alignment = nbytes == 2 ? 1 : (nbytes == 4 ? 2 : 3);
4045 frag_align (last_alignment, 0, 0);
4046 record_alignment (now_seg, last_alignment);
4049 /* For mmixal compatibility, a label for an instruction (and emitting
4050 pseudo) refers to the _aligned_ address. So we have to emit the
4052 if (current_fb_label >= 0)
4053 colon (fb_label_name (current_fb_label, 1));
4054 else if (pending_label != NULL)
4056 colon (pending_label);
4057 pending_label = NULL;
4062 if (is_end_of_line[(unsigned int) *input_line_pointer])
4064 /* Default to zero if the expression was absent. */
4066 exp.X_op = O_constant;
4067 exp.X_add_number = 0;
4069 exp.X_add_symbol = NULL;
4070 exp.X_op_symbol = NULL;
4071 emit_expr (&exp, (unsigned int) nbytes);
4078 switch (*input_line_pointer)
4080 /* We support strings here too; each character takes up nbytes
4083 ++input_line_pointer;
4084 start = input_line_pointer;
4085 while (is_a_char (c = next_char_of_string ()))
4087 exp.X_op = O_constant;
4088 exp.X_add_number = c;
4090 emit_expr (&exp, (unsigned int) nbytes);
4093 if (input_line_pointer[-1] != '\"')
4095 /* We will only get here in rare cases involving #NO_APP,
4096 where the unterminated string is not recognized by the
4097 preformatting pass. */
4098 as_bad (_("unterminated string"));
4099 mmix_discard_rest_of_line ();
4107 emit_expr (&exp, (unsigned int) nbytes);
4113 while (*input_line_pointer++ == ',');
4115 input_line_pointer--; /* Put terminator back into stream. */
4117 mmix_handle_rest_of_empty_line ();
4119 /* We don't need to step up the counter for the current_fb_label here;
4120 that's handled by the caller. */
4123 /* The md_do_align worker. At present, we just record an alignment to
4124 nullify the automatic alignment we do for WYDE, TETRA and OCTA, as gcc
4125 does not use the unaligned macros when attribute packed is used.
4126 Arguably this is a GCC bug. */
4129 mmix_md_do_align (n, fill, len, max)
4131 char *fill ATTRIBUTE_UNUSED;
4132 int len ATTRIBUTE_UNUSED;
4133 int max ATTRIBUTE_UNUSED;
4136 want_unaligned = n == 0;