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