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