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