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