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