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