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