* read.c (mri_comment_field): New function.
[binutils-gdb.git] / gas / config / tc-m68k.c
1 /* tc-m68k.c -- Assemble for the m68k family
2 Copyright (C) 1987, 91, 92, 93, 94, 1995 Free Software Foundation, Inc.
3
4 This file is part of GAS, the GNU Assembler.
5
6 GAS is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
10
11 GAS is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with GAS; see the file COPYING. If not, write to the Free
18 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
19 02111-1307, USA. */
20
21 #include <ctype.h>
22 #define NO_RELOC 0
23 #include "as.h"
24 #include "obstack.h"
25 #include "subsegs.h"
26
27 #include "opcode/m68k.h"
28 #include "m68k-parse.h"
29
30 /* This array holds the chars that always start a comment. If the
31 pre-processor is disabled, these aren't very useful */
32 #if defined (OBJ_ELF) || defined (TE_DELTA)
33 const char comment_chars[] = "|#";
34 #else
35 const char comment_chars[] = "|";
36 #endif
37
38 /* This array holds the chars that only start a comment at the beginning of
39 a line. If the line seems to have the form '# 123 filename'
40 .line and .file directives will appear in the pre-processed output */
41 /* Note that input_file.c hand checks for '#' at the beginning of the
42 first line of the input file. This is because the compiler outputs
43 #NO_APP at the beginning of its output. */
44 /* Also note that comments like this one will always work. */
45 const char line_comment_chars[] = "#";
46
47 const char line_separator_chars[] = "";
48
49 /* Chars that can be used to separate mant from exp in floating point nums */
50 CONST char EXP_CHARS[] = "eE";
51
52 /* Chars that mean this number is a floating point constant, as
53 in "0f12.456" or "0d1.2345e12". */
54
55 CONST char FLT_CHARS[] = "rRsSfFdDxXeEpP";
56
57 /* Also be aware that MAXIMUM_NUMBER_OF_CHARS_FOR_FLOAT may have to be
58 changed in read.c . Ideally it shouldn't have to know about it at all,
59 but nothing is ideal around here. */
60
61 const int md_reloc_size = 8; /* Size of relocation record */
62
63 /* Are we trying to generate PIC code? If so, absolute references
64 ought to be made into linkage table references or pc-relative
65 references. */
66 int flag_want_pic;
67
68 static int flag_short_refs; /* -l option */
69 static int flag_long_jumps; /* -S option */
70
71 #ifdef REGISTER_PREFIX_OPTIONAL
72 int flag_reg_prefix_optional = REGISTER_PREFIX_OPTIONAL;
73 #else
74 int flag_reg_prefix_optional;
75 #endif
76
77 /* The floating point coprocessor to use by default. */
78 static enum m68k_register m68k_float_copnum = COP1;
79
80 /* If this is non-zero, then references to number(%pc) will be taken
81 to refer to number, rather than to %pc + number. */
82 static int m68k_abspcadd;
83
84 /* If this is non-zero, then the quick forms of the move, add, and sub
85 instructions are used when possible. */
86 static int m68k_quick = 1;
87
88 /* If this is non-zero, then if the size is not specified for a base
89 or outer displacement, the assembler assumes that the size should
90 be 32 bits. */
91 static int m68k_rel32 = 1;
92
93 /* Its an arbitrary name: This means I don't approve of it */
94 /* See flames below */
95 static struct obstack robyn;
96
97 #define TAB(x,y) (((x)<<2)+(y))
98 #define TABTYPE(xy) ((xy) >> 2)
99 #define BYTE 0
100 #define SHORT 1
101 #define LONG 2
102 #define SZ_UNDEF 3
103 #undef BRANCH
104 /* Case `g' except when BCC68000 is applicable. */
105 #define ABRANCH 1
106 /* Coprocessor branches. */
107 #define FBRANCH 2
108 /* Mode 7.2 -- program counter indirect with (16-bit) displacement,
109 supported on all cpus. Widens to 32-bit absolute. */
110 #define PCREL 3
111 /* For inserting an extra jmp instruction with long offset on 68000,
112 for expanding conditional branches. (Not bsr or bra.) Since the
113 68000 doesn't support 32-bit displacements for conditional
114 branches, we fake it by reversing the condition and branching
115 around a jmp with an absolute long operand. */
116 #define BCC68000 4
117 /* For the DBcc "instructions". If the displacement requires 32 bits,
118 the branch-around-a-jump game is played here too. */
119 #define DBCC 5
120 /* Not currently used? */
121 #define PCLEA 6
122 /* Mode AINDX (apc-relative) using PC, with variable target, might fit
123 in 16 or 8 bits. */
124 #define PCINDEX 7
125
126 struct m68k_incant
127 {
128 const char *m_operands;
129 unsigned long m_opcode;
130 short m_opnum;
131 short m_codenum;
132 int m_arch;
133 struct m68k_incant *m_next;
134 };
135
136 #define getone(x) ((((x)->m_opcode)>>16)&0xffff)
137 #define gettwo(x) (((x)->m_opcode)&0xffff)
138
139 static const enum m68k_register m68000_control_regs[] = { 0 };
140 static const enum m68k_register m68010_control_regs[] = {
141 SFC, DFC, USP, VBR,
142 0
143 };
144 static const enum m68k_register m68020_control_regs[] = {
145 SFC, DFC, USP, VBR, CACR, CAAR, MSP, ISP,
146 0
147 };
148 static const enum m68k_register m68040_control_regs[] = {
149 SFC, DFC, CACR, TC, ITT0, ITT1, DTT0, DTT1,
150 USP, VBR, MSP, ISP, MMUSR, URP, SRP,
151 0
152 };
153 static const enum m68k_register m68060_control_regs[] = {
154 SFC, DFC, CACR, TC, ITT0, ITT1, DTT0, DTT1, BUSCR,
155 USP, VBR, URP, SRP, PCR,
156 0
157 };
158 #define cpu32_control_regs m68010_control_regs
159
160 static const enum m68k_register *control_regs;
161
162 /* internal form of a 68020 instruction */
163 struct m68k_it
164 {
165 const char *error;
166 const char *args; /* list of opcode info */
167 int numargs;
168
169 int numo; /* Number of shorts in opcode */
170 short opcode[11];
171
172 struct m68k_op operands[6];
173
174 int nexp; /* number of exprs in use */
175 struct m68k_exp exprs[4];
176
177 int nfrag; /* Number of frags we have to produce */
178 struct
179 {
180 int fragoff; /* Where in the current opcode the frag ends */
181 symbolS *fadd;
182 long foff;
183 int fragty;
184 }
185 fragb[4];
186
187 int nrel; /* Num of reloc strucs in use */
188 struct
189 {
190 int n;
191 expressionS exp;
192 char wid;
193 char pcrel;
194 /* In a pc relative address the difference between the address
195 of the offset and the address that the offset is relative
196 to. This depends on the addressing mode. Basically this
197 is the value to put in the offset field to address the
198 first byte of the offset, without regarding the special
199 significance of some values (in the branch instruction, for
200 example). */
201 int pcrel_fix;
202 }
203 reloc[5]; /* Five is enough??? */
204 };
205
206 #define cpu_of_arch(x) ((x) & m68000up)
207 #define float_of_arch(x) ((x) & mfloat)
208 #define mmu_of_arch(x) ((x) & mmmu)
209
210 static struct m68k_it the_ins; /* the instruction being assembled */
211
212 #define op(ex) ((ex)->exp.X_op)
213 #define adds(ex) ((ex)->exp.X_add_symbol)
214 #define subs(ex) ((ex)->exp.X_op_symbol)
215 #define offs(ex) ((ex)->exp.X_add_number)
216
217 /* Macros for adding things to the m68k_it struct */
218
219 #define addword(w) the_ins.opcode[the_ins.numo++]=(w)
220
221 /* Like addword, but goes BEFORE general operands */
222 static void
223 insop (w, opcode)
224 int w;
225 struct m68k_incant *opcode;
226 {
227 int z;
228 for(z=the_ins.numo;z>opcode->m_codenum;--z)
229 the_ins.opcode[z]=the_ins.opcode[z-1];
230 for(z=0;z<the_ins.nrel;z++)
231 the_ins.reloc[z].n+=2;
232 for (z = 0; z < the_ins.nfrag; z++)
233 the_ins.fragb[z].fragoff++;
234 the_ins.opcode[opcode->m_codenum]=w;
235 the_ins.numo++;
236 }
237
238 /* The numo+1 kludge is so we can hit the low order byte of the prev word.
239 Blecch. */
240 static void
241 add_fix (width, exp, pc_rel, pc_fix)
242 char width;
243 struct m68k_exp *exp;
244 int pc_rel;
245 int pc_fix;
246 {
247 the_ins.reloc[the_ins.nrel].n = (((width)=='B')
248 ? (the_ins.numo*2-1)
249 : (((width)=='b')
250 ? (the_ins.numo*2+1)
251 : (the_ins.numo*2)));
252 the_ins.reloc[the_ins.nrel].exp = exp->exp;
253 the_ins.reloc[the_ins.nrel].wid = width;
254 the_ins.reloc[the_ins.nrel].pcrel_fix = pc_fix;
255 the_ins.reloc[the_ins.nrel++].pcrel = pc_rel;
256 }
257
258 /* Cause an extra frag to be generated here, inserting up to 10 bytes
259 (that value is chosen in the frag_var call in md_assemble). TYPE
260 is the subtype of the frag to be generated; its primary type is
261 rs_machine_dependent.
262
263 The TYPE parameter is also used by md_convert_frag_1 and
264 md_estimate_size_before_relax. The appropriate type of fixup will
265 be emitted by md_convert_frag_1.
266
267 ADD becomes the FR_SYMBOL field of the frag, and OFF the FR_OFFSET. */
268 static void
269 add_frag(add,off,type)
270 symbolS *add;
271 long off;
272 int type;
273 {
274 the_ins.fragb[the_ins.nfrag].fragoff=the_ins.numo;
275 the_ins.fragb[the_ins.nfrag].fadd=add;
276 the_ins.fragb[the_ins.nfrag].foff=off;
277 the_ins.fragb[the_ins.nfrag++].fragty=type;
278 }
279
280 #define isvar(ex) \
281 (op (ex) != O_constant && op (ex) != O_big)
282
283 static char *crack_operand PARAMS ((char *str, struct m68k_op *opP));
284 static int get_num PARAMS ((struct m68k_exp *exp, int ok));
285 static int reverse_16_bits PARAMS ((int in));
286 static int reverse_8_bits PARAMS ((int in));
287 static void install_gen_operand PARAMS ((int mode, int val));
288 static void install_operand PARAMS ((int mode, int val));
289 static void s_bss PARAMS ((int));
290 static void s_data1 PARAMS ((int));
291 static void s_data2 PARAMS ((int));
292 static void s_even PARAMS ((int));
293 static void s_proc PARAMS ((int));
294 static void mri_chip PARAMS ((void));
295 static void s_chip PARAMS ((int));
296 static void s_fopt PARAMS ((int));
297 static void s_opt PARAMS ((int));
298 static void s_reg PARAMS ((int));
299 static void s_restore PARAMS ((int));
300 static void s_save PARAMS ((int));
301 static void s_mri_if PARAMS ((int));
302 static void s_mri_else PARAMS ((int));
303 static void s_mri_endi PARAMS ((int));
304 static void s_mri_break PARAMS ((int));
305 static void s_mri_next PARAMS ((int));
306 static void s_mri_for PARAMS ((int));
307 static void s_mri_endf PARAMS ((int));
308 static void s_mri_repeat PARAMS ((int));
309 static void s_mri_until PARAMS ((int));
310 static void s_mri_while PARAMS ((int));
311 static void s_mri_endw PARAMS ((int));
312
313 static int current_architecture;
314
315 struct m68k_cpu {
316 unsigned long arch;
317 const char *name;
318 };
319
320 static const struct m68k_cpu archs[] = {
321 { m68000, "68000" },
322 { m68010, "68010" },
323 { m68020, "68020" },
324 { m68030, "68030" },
325 { m68040, "68040" },
326 { m68060, "68060" },
327 { cpu32, "cpu32" },
328 { m68881, "68881" },
329 { m68851, "68851" },
330 /* Aliases (effectively, so far as gas is concerned) for the above
331 cpus. */
332 { m68020, "68k" },
333 { m68000, "68302" },
334 { m68000, "68008" },
335 { m68000, "68ec000" },
336 { m68000, "68hc000" },
337 { m68000, "68hc001" },
338 { m68020, "68ec020" },
339 { m68030, "68ec030" },
340 { m68040, "68ec040" },
341 { cpu32, "68330" },
342 { cpu32, "68331" },
343 { cpu32, "68332" },
344 { cpu32, "68333" },
345 { cpu32, "68340" },
346 { cpu32, "68360" },
347 { m68881, "68882" },
348 };
349
350 static const int n_archs = sizeof (archs) / sizeof (archs[0]);
351
352 /* BCC68000 is for patching in an extra jmp instruction for long offsets
353 on the 68000. The 68000 doesn't support long branches with branchs */
354
355 /* This table desribes how you change sizes for the various types of variable
356 size expressions. This version only supports two kinds. */
357
358 /* Note that calls to frag_var need to specify the maximum expansion
359 needed; this is currently 10 bytes for DBCC. */
360
361 /* The fields are:
362 How far Forward this mode will reach:
363 How far Backward this mode will reach:
364 How many bytes this mode will add to the size of the frag
365 Which mode to go to if the offset won't fit in this one
366 */
367 relax_typeS md_relax_table[] =
368 {
369 {1, 1, 0, 0}, /* First entries aren't used */
370 {1, 1, 0, 0}, /* For no good reason except */
371 {1, 1, 0, 0}, /* that the VAX doesn't either */
372 {1, 1, 0, 0},
373
374 {(127), (-128), 0, TAB (ABRANCH, SHORT)},
375 {(32767), (-32768), 2, TAB (ABRANCH, LONG)},
376 {0, 0, 4, 0},
377 {1, 1, 0, 0},
378
379 {1, 1, 0, 0}, /* FBRANCH doesn't come BYTE */
380 {(32767), (-32768), 2, TAB (FBRANCH, LONG)},
381 {0, 0, 4, 0},
382 {1, 1, 0, 0},
383
384 {1, 1, 0, 0}, /* PCREL doesn't come BYTE */
385 {(32767), (-32768), 2, TAB (PCREL, LONG)},
386 {0, 0, 4, 0},
387 {1, 1, 0, 0},
388
389 {(127), (-128), 0, TAB (BCC68000, SHORT)},
390 {(32767), (-32768), 2, TAB (BCC68000, LONG)},
391 {0, 0, 6, 0}, /* jmp long space */
392 {1, 1, 0, 0},
393
394 {1, 1, 0, 0}, /* DBCC doesn't come BYTE */
395 {(32767), (-32768), 2, TAB (DBCC, LONG)},
396 {0, 0, 10, 0}, /* bra/jmp long space */
397 {1, 1, 0, 0},
398
399 {1, 1, 0, 0}, /* PCLEA doesn't come BYTE */
400 {32767, -32768, 2, TAB (PCLEA, LONG)},
401 {0, 0, 6, 0},
402 {1, 1, 0, 0},
403
404 /* For, e.g., jmp pcrel indexed. */
405 {125, -130, 0, TAB (PCINDEX, SHORT)},
406 {32765, -32770, 2, TAB (PCINDEX, LONG)},
407 {0, 0, 4, 0},
408 {1, 1, 0, 0},
409 };
410
411 /* These are the machine dependent pseudo-ops. These are included so
412 the assembler can work on the output from the SUN C compiler, which
413 generates these.
414 */
415
416 /* This table describes all the machine specific pseudo-ops the assembler
417 has to support. The fields are:
418 pseudo-op name without dot
419 function to call to execute this pseudo-op
420 Integer arg to pass to the function
421 */
422 const pseudo_typeS md_pseudo_table[] =
423 {
424 {"data1", s_data1, 0},
425 {"data2", s_data2, 0},
426 {"bss", s_bss, 0},
427 {"even", s_even, 0},
428 {"skip", s_space, 0},
429 {"proc", s_proc, 0},
430 #ifdef TE_SUN3
431 {"align", s_align_bytes, 0},
432 #endif
433 #ifdef OBJ_ELF
434 {"swbeg", s_ignore, 0},
435 #endif
436
437 /* The following pseudo-ops are supported for MRI compatibility. */
438 {"chip", s_chip, 0},
439 {"comline", s_space, 1},
440 {"fopt", s_fopt, 0},
441 {"mask2", s_ignore, 0},
442 {"opt", s_opt, 0},
443 {"reg", s_reg, 0},
444 {"restore", s_restore, 0},
445 {"save", s_save, 0},
446
447 {"if", s_mri_if, 0},
448 {"if.b", s_mri_if, 'b'},
449 {"if.w", s_mri_if, 'w'},
450 {"if.l", s_mri_if, 'l'},
451 {"else", s_mri_else, 0},
452 {"else.s", s_mri_else, 's'},
453 {"else.l", s_mri_else, 'l'},
454 {"endi", s_mri_endi, 0},
455 {"break", s_mri_break, 0},
456 {"break.s", s_mri_break, 's'},
457 {"break.l", s_mri_break, 'l'},
458 {"next", s_mri_next, 0},
459 {"next.s", s_mri_next, 's'},
460 {"next.l", s_mri_next, 'l'},
461 {"for", s_mri_for, 0},
462 {"for.b", s_mri_for, 'b'},
463 {"for.w", s_mri_for, 'w'},
464 {"for.l", s_mri_for, 'l'},
465 {"endf", s_mri_endf, 0},
466 {"repeat", s_mri_repeat, 0},
467 {"until", s_mri_until, 0},
468 {"until.b", s_mri_until, 'b'},
469 {"until.w", s_mri_until, 'w'},
470 {"until.l", s_mri_until, 'l'},
471 {"while", s_mri_while, 0},
472 {"while.b", s_mri_while, 'b'},
473 {"while.w", s_mri_while, 'w'},
474 {"while.l", s_mri_while, 'l'},
475 {"endw", s_mri_endw, 0},
476
477 {0, 0, 0}
478 };
479
480
481 /* The mote pseudo ops are put into the opcode table, since they
482 don't start with a . they look like opcodes to gas.
483 */
484 extern void obj_coff_section ();
485
486 CONST pseudo_typeS mote_pseudo_table[] =
487 {
488
489 {"dcl", cons, 4},
490 {"dc", cons, 2},
491 {"dcw", cons, 2},
492 {"dcb", cons, 1},
493
494 {"dsl", s_space, 4},
495 {"ds", s_space, 2},
496 {"dsw", s_space, 2},
497 {"dsb", s_space, 1},
498
499 {"xdef", s_globl, 0},
500 {"align", s_align_ptwo, 0},
501 #ifdef M68KCOFF
502 {"sect", obj_coff_section, 0},
503 {"section", obj_coff_section, 0},
504 #endif
505 {0, 0, 0}
506 };
507
508 #define issbyte(x) ((x)>=-128 && (x)<=127)
509 #define isubyte(x) ((x)>=0 && (x)<=255)
510 #define issword(x) ((x)>=-32768 && (x)<=32767)
511 #define isuword(x) ((x)>=0 && (x)<=65535)
512
513 #define isbyte(x) ((x)>= -255 && (x)<=255)
514 #define isword(x) ((x)>=-32768 && (x)<=65535)
515 #define islong(x) (1)
516
517 extern char *input_line_pointer;
518
519 static char mklower_table[256];
520 #define mklower(c) (mklower_table[(unsigned char)(c)])
521 static char notend_table[256];
522 static char alt_notend_table[256];
523 #define notend(s) \
524 (! (notend_table[(unsigned char) *s] \
525 || (*s == ':' \
526 && alt_notend_table[(unsigned char) s[1]])))
527
528 #if defined (M68KCOFF) && !defined (BFD_ASSEMBLER)
529
530 #ifdef NO_PCREL_RELOCS
531
532 int
533 make_pcrel_absolute(fixP, add_number)
534 fixS *fixP;
535 long *add_number;
536 {
537 register unsigned char *opcode = fixP->fx_frag->fr_opcode;
538
539 /* rewrite the PC relative instructions to absolute address ones.
540 * these are rumoured to be faster, and the apollo linker refuses
541 * to deal with the PC relative relocations.
542 */
543 if (opcode[0] == 0x60 && opcode[1] == 0xff) /* BRA -> JMP */
544 {
545 opcode[0] = 0x4e;
546 opcode[1] = 0xf9;
547 }
548 else if (opcode[0] == 0x61 && opcode[1] == 0xff) /* BSR -> JSR */
549 {
550 opcode[0] = 0x4e;
551 opcode[1] = 0xb9;
552 }
553 else
554 as_fatal ("Unknown PC relative instruction");
555 *add_number -= 4;
556 return 0;
557 }
558
559 #endif /* NO_PCREL_RELOCS */
560
561 short
562 tc_coff_fix2rtype (fixP)
563 fixS *fixP;
564 {
565 if (fixP->fx_tcbit && fixP->fx_size == 4)
566 return R_RELLONG_NEG;
567 #ifdef NO_PCREL_RELOCS
568 know (fixP->fx_pcrel == 0);
569 return (fixP->fx_size == 1 ? R_RELBYTE
570 : fixP->fx_size == 2 ? R_DIR16
571 : R_DIR32);
572 #else
573 return (fixP->fx_pcrel ?
574 (fixP->fx_size == 1 ? R_PCRBYTE :
575 fixP->fx_size == 2 ? R_PCRWORD :
576 R_PCRLONG) :
577 (fixP->fx_size == 1 ? R_RELBYTE :
578 fixP->fx_size == 2 ? R_RELWORD :
579 R_RELLONG));
580 #endif
581 }
582
583 #endif
584
585 #ifdef BFD_ASSEMBLER
586
587 arelent *
588 tc_gen_reloc (section, fixp)
589 asection *section;
590 fixS *fixp;
591 {
592 arelent *reloc;
593 bfd_reloc_code_real_type code;
594
595 if (fixP->fx_tcbit)
596 abort ();
597
598 #define F(SZ,PCREL) (((SZ) << 1) + (PCREL))
599 switch (F (fixp->fx_size, fixp->fx_pcrel))
600 {
601 #define MAP(SZ,PCREL,TYPE) case F(SZ,PCREL): code = (TYPE); break
602 MAP (1, 0, BFD_RELOC_8);
603 MAP (2, 0, BFD_RELOC_16);
604 MAP (4, 0, BFD_RELOC_32);
605 MAP (1, 1, BFD_RELOC_8_PCREL);
606 MAP (2, 1, BFD_RELOC_16_PCREL);
607 MAP (4, 1, BFD_RELOC_32_PCREL);
608 default:
609 abort ();
610 }
611
612 reloc = (arelent *) bfd_alloc_by_size_t (stdoutput, sizeof (arelent));
613 assert (reloc != 0);
614 reloc->sym_ptr_ptr = &fixp->fx_addsy->bsym;
615 reloc->address = fixp->fx_frag->fr_address + fixp->fx_where;
616 if (fixp->fx_pcrel)
617 reloc->addend = fixp->fx_addnumber;
618 else
619 reloc->addend = 0;
620
621 reloc->howto = bfd_reloc_type_lookup (stdoutput, code);
622 assert (reloc->howto != 0);
623
624 return reloc;
625 }
626
627 #endif /* BFD_ASSEMBLER */
628
629 /* Handle of the OPCODE hash table. NULL means any use before
630 m68k_ip_begin() will crash. */
631 static struct hash_control *op_hash;
632 \f
633 /* Assemble an m68k instruction. */
634
635 void
636 m68k_ip (instring)
637 char *instring;
638 {
639 register char *p;
640 register struct m68k_op *opP;
641 register struct m68k_incant *opcode;
642 register const char *s;
643 register int tmpreg = 0, baseo = 0, outro = 0, nextword;
644 char *pdot, *pdotmove;
645 enum m68k_size siz1, siz2;
646 char c;
647 int losing;
648 int opsfound;
649 char *crack_operand ();
650 LITTLENUM_TYPE words[6];
651 LITTLENUM_TYPE *wordp;
652 unsigned long ok_arch = 0;
653
654 if (*instring == ' ')
655 instring++; /* skip leading whitespace */
656
657 /* Scan up to end of operation-code, which MUST end in end-of-string
658 or exactly 1 space. */
659 pdot = 0;
660 for (p = instring; *p != '\0'; p++)
661 {
662 if (*p == ' ')
663 break;
664 if (*p == '.')
665 pdot = p;
666 }
667
668 if (p == instring)
669 {
670 the_ins.error = "No operator";
671 return;
672 }
673
674 /* p now points to the end of the opcode name, probably whitespace.
675 Make sure the name is null terminated by clobbering the
676 whitespace, look it up in the hash table, then fix it back.
677 Remove a dot, first, since the opcode tables have none. */
678 if (pdot != NULL)
679 {
680 for (pdotmove = pdot; pdotmove < p; pdotmove++)
681 *pdotmove = pdotmove[1];
682 p--;
683 }
684
685 c = *p;
686 *p = '\0';
687 opcode = (struct m68k_incant *) hash_find (op_hash, instring);
688 *p = c;
689
690 if (pdot != NULL)
691 {
692 for (pdotmove = p; pdotmove > pdot; pdotmove--)
693 *pdotmove = pdotmove[-1];
694 *pdot = '.';
695 ++p;
696 }
697
698 if (opcode == NULL)
699 {
700 the_ins.error = "Unknown operator";
701 return;
702 }
703
704 /* found a legitimate opcode, start matching operands */
705 while (*p == ' ')
706 ++p;
707
708 if (opcode->m_operands == 0)
709 {
710 char *old = input_line_pointer;
711 *old = '\n';
712 input_line_pointer = p;
713 /* Ahh - it's a motorola style psuedo op */
714 mote_pseudo_table[opcode->m_opnum].poc_handler
715 (mote_pseudo_table[opcode->m_opnum].poc_val);
716 input_line_pointer = old;
717 *old = 0;
718
719 return;
720 }
721
722 if (flag_mri && opcode->m_opnum == 0)
723 {
724 /* In MRI mode, random garbage is allowed after an instruction
725 which accepts no operands. */
726 the_ins.args = opcode->m_operands;
727 the_ins.numargs = opcode->m_opnum;
728 the_ins.numo = opcode->m_codenum;
729 the_ins.opcode[0] = getone (opcode);
730 the_ins.opcode[1] = gettwo (opcode);
731 return;
732 }
733
734 for (opP = &the_ins.operands[0]; *p; opP++)
735 {
736 p = crack_operand (p, opP);
737
738 if (opP->error)
739 {
740 the_ins.error = opP->error;
741 return;
742 }
743 }
744
745 opsfound = opP - &the_ins.operands[0];
746
747 /* This ugly hack is to support the floating pt opcodes in their
748 standard form. Essentially, we fake a first enty of type COP#1 */
749 if (opcode->m_operands[0] == 'I')
750 {
751 int n;
752
753 for (n = opsfound; n > 0; --n)
754 the_ins.operands[n] = the_ins.operands[n - 1];
755
756 memset ((char *) (&the_ins.operands[0]), '\0',
757 sizeof (the_ins.operands[0]));
758 the_ins.operands[0].mode = CONTROL;
759 the_ins.operands[0].reg = m68k_float_copnum;
760 opsfound++;
761 }
762
763 /* We've got the operands. Find an opcode that'll accept them */
764 for (losing = 0;;)
765 {
766 /* If we didn't get the right number of ops, or we have no
767 common model with this pattern then reject this pattern. */
768
769 if (opsfound != opcode->m_opnum
770 || ((opcode->m_arch & current_architecture) == 0))
771 {
772 ++losing;
773 ok_arch |= opcode->m_arch;
774 }
775 else
776 {
777 for (s = opcode->m_operands, opP = &the_ins.operands[0];
778 *s && !losing;
779 s += 2, opP++)
780 {
781 /* Warning: this switch is huge! */
782 /* I've tried to organize the cases into this order:
783 non-alpha first, then alpha by letter. Lower-case
784 goes directly before uppercase counterpart. */
785 /* Code with multiple case ...: gets sorted by the lowest
786 case ... it belongs to. I hope this makes sense. */
787 switch (*s)
788 {
789 case '!':
790 switch (opP->mode)
791 {
792 case IMMED:
793 case DREG:
794 case AREG:
795 case FPREG:
796 case CONTROL:
797 case AINC:
798 case ADEC:
799 case REGLST:
800 losing++;
801 break;
802 default:
803 break;
804 }
805 break;
806
807 case '`':
808 switch (opP->mode)
809 {
810 case IMMED:
811 case DREG:
812 case AREG:
813 case FPREG:
814 case CONTROL:
815 case AINC:
816 case REGLST:
817 case AINDR:
818 losing++;
819 break;
820 default:
821 break;
822 }
823 break;
824
825 case '#':
826 if (opP->mode != IMMED)
827 losing++;
828 else if (s[1] == 'b'
829 && ! isvar (&opP->disp)
830 && (opP->disp.exp.X_op != O_constant
831 || ! isbyte (opP->disp.exp.X_add_number)))
832 losing++;
833 else if (s[1] == 'w'
834 && ! isvar (&opP->disp)
835 && (opP->disp.exp.X_op != O_constant
836 || ! isword (opP->disp.exp.X_add_number)))
837 losing++;
838 break;
839
840 case '^':
841 case 'T':
842 if (opP->mode != IMMED)
843 losing++;
844 break;
845
846 case '$':
847 if (opP->mode == AREG
848 || opP->mode == CONTROL
849 || opP->mode == FPREG
850 || opP->mode == IMMED
851 || opP->mode == REGLST
852 || (opP->mode != ABSL
853 && (opP->reg == PC
854 || opP->reg == ZPC)))
855 losing++;
856 break;
857
858 case '%':
859 if (opP->mode == CONTROL
860 || opP->mode == FPREG
861 || opP->mode == REGLST
862 || (opP->mode != ABSL
863 && opP->mode != IMMED
864 && (opP->reg == PC
865 || opP->reg == ZPC)))
866 losing++;
867 break;
868
869 case '&':
870 switch (opP->mode)
871 {
872 case DREG:
873 case AREG:
874 case FPREG:
875 case CONTROL:
876 case IMMED:
877 case AINC:
878 case ADEC:
879 case REGLST:
880 losing++;
881 break;
882 case ABSL:
883 break;
884 default:
885 if (opP->reg == PC
886 || opP->reg == ZPC)
887 losing++;
888 break;
889 }
890 break;
891
892 case '*':
893 if (opP->mode == CONTROL
894 || opP->mode == FPREG
895 || opP->mode == REGLST)
896 losing++;
897 break;
898
899 case '+':
900 if (opP->mode != AINC)
901 losing++;
902 break;
903
904 case '-':
905 if (opP->mode != ADEC)
906 losing++;
907 break;
908
909 case '/':
910 switch (opP->mode)
911 {
912 case AREG:
913 case CONTROL:
914 case FPREG:
915 case AINC:
916 case ADEC:
917 case IMMED:
918 case REGLST:
919 losing++;
920 break;
921 default:
922 break;
923 }
924 break;
925
926 case ';':
927 switch (opP->mode)
928 {
929 case AREG:
930 case CONTROL:
931 case FPREG:
932 case REGLST:
933 losing++;
934 break;
935 default:
936 break;
937 }
938 break;
939
940 case '?':
941 switch (opP->mode)
942 {
943 case AREG:
944 case CONTROL:
945 case FPREG:
946 case AINC:
947 case ADEC:
948 case IMMED:
949 case REGLST:
950 losing++;
951 break;
952 case ABSL:
953 break;
954 default:
955 if (opP->reg == PC || opP->reg == ZPC)
956 losing++;
957 break;
958 }
959 break;
960
961 case '@':
962 switch (opP->mode)
963 {
964 case AREG:
965 case CONTROL:
966 case FPREG:
967 case IMMED:
968 case REGLST:
969 losing++;
970 break;
971 default:
972 break;
973 }
974 break;
975
976 case '~': /* For now! (JF FOO is this right?) */
977 switch (opP->mode)
978 {
979 case DREG:
980 case AREG:
981 case CONTROL:
982 case FPREG:
983 case IMMED:
984 case REGLST:
985 losing++;
986 break;
987 case ABSL:
988 break;
989 default:
990 if (opP->reg == PC
991 || opP->reg == ZPC)
992 losing++;
993 break;
994 }
995 break;
996
997 case '3':
998 if (opP->mode != CONTROL
999 || (opP->reg != TT0 && opP->reg != TT1))
1000 losing++;
1001 break;
1002
1003 case 'A':
1004 if (opP->mode != AREG)
1005 losing++;
1006 break;
1007
1008 case 'a':
1009 if (opP->mode != AINDR)
1010 ++losing;
1011 break;
1012
1013 case 'B': /* FOO */
1014 if (opP->mode != ABSL
1015 || (flag_long_jumps
1016 && strncmp (instring, "jbsr", 4) == 0))
1017 losing++;
1018 break;
1019
1020 case 'C':
1021 if (opP->mode != CONTROL || opP->reg != CCR)
1022 losing++;
1023 break;
1024
1025 case 'd':
1026 if (opP->mode != DISP
1027 || opP->reg < ADDR0
1028 || opP->reg > ADDR7)
1029 losing++;
1030 break;
1031
1032 case 'D':
1033 if (opP->mode != DREG)
1034 losing++;
1035 break;
1036
1037 case 'F':
1038 if (opP->mode != FPREG)
1039 losing++;
1040 break;
1041
1042 case 'I':
1043 if (opP->mode != CONTROL
1044 || opP->reg < COP0
1045 || opP->reg > COP7)
1046 losing++;
1047 break;
1048
1049 case 'J':
1050 if (opP->mode != CONTROL
1051 || opP->reg < USP
1052 || opP->reg > last_movec_reg)
1053 losing++;
1054 else
1055 {
1056 const enum m68k_register *rp;
1057 for (rp = control_regs; *rp; rp++)
1058 if (*rp == opP->reg)
1059 break;
1060 if (*rp == 0)
1061 losing++;
1062 }
1063 break;
1064
1065 case 'k':
1066 if (opP->mode != IMMED)
1067 losing++;
1068 break;
1069
1070 case 'l':
1071 case 'L':
1072 if (opP->mode == DREG
1073 || opP->mode == AREG
1074 || opP->mode == FPREG)
1075 {
1076 if (s[1] == '8')
1077 losing++;
1078 else
1079 {
1080 switch (opP->mode)
1081 {
1082 case DREG:
1083 opP->mask = 1 << (opP->reg - DATA0);
1084 break;
1085 case AREG:
1086 opP->mask = 1 << (opP->reg - ADDR0 + 8);
1087 break;
1088 case FPREG:
1089 opP->mask = 1 << (opP->reg - FP0 + 16);
1090 break;
1091 default:
1092 abort ();
1093 }
1094 opP->mode = REGLST;
1095 }
1096 }
1097 else if (opP->mode == CONTROL)
1098 {
1099 if (s[1] != '8')
1100 losing++;
1101 else
1102 {
1103 switch (opP->reg)
1104 {
1105 case FPI:
1106 opP->mask = 1 << 24;
1107 break;
1108 case FPS:
1109 opP->mask = 1 << 25;
1110 break;
1111 case FPC:
1112 opP->mask = 1 << 26;
1113 break;
1114 default:
1115 losing++;
1116 break;
1117 }
1118 opP->mode = REGLST;
1119 }
1120 }
1121 else if (opP->mode == ABSL
1122 && opP->disp.size == SIZE_UNSPEC
1123 && opP->disp.exp.X_op == O_constant)
1124 {
1125 /* This is what the MRI REG pseudo-op generates. */
1126 opP->mode = REGLST;
1127 opP->mask = opP->disp.exp.X_add_number;
1128 }
1129 else if (opP->mode != REGLST)
1130 losing++;
1131 else if (s[1] == '8' && (opP->mask & 0x0ffffff) != 0)
1132 losing++;
1133 else if (s[1] == '3' && (opP->mask & 0x7000000) != 0)
1134 losing++;
1135 break;
1136
1137 case 'M':
1138 if (opP->mode != IMMED)
1139 losing++;
1140 else if (opP->disp.exp.X_op != O_constant
1141 || ! issbyte (opP->disp.exp.X_add_number))
1142 losing++;
1143 else if (! m68k_quick
1144 && instring[3] != 'q'
1145 && instring[4] != 'q')
1146 losing++;
1147 break;
1148
1149 case 'O':
1150 if (opP->mode != DREG && opP->mode != IMMED)
1151 losing++;
1152 break;
1153
1154 case 'Q':
1155 if (opP->mode != IMMED)
1156 losing++;
1157 else if (opP->disp.exp.X_op != O_constant
1158 || opP->disp.exp.X_add_number < 1
1159 || opP->disp.exp.X_add_number > 8)
1160 losing++;
1161 else if (! m68k_quick
1162 && (strncmp (instring, "add", 3) == 0
1163 || strncmp (instring, "sub", 3) == 0)
1164 && instring[3] != 'q')
1165 losing++;
1166 break;
1167
1168 case 'R':
1169 if (opP->mode != DREG && opP->mode != AREG)
1170 losing++;
1171 break;
1172
1173 case 'r':
1174 if (opP->mode != AINDR
1175 && (opP->mode != BASE
1176 || (opP->reg != 0
1177 && opP->reg != ZADDR0)
1178 || opP->disp.exp.X_op != O_absent
1179 || ((opP->index.reg < DATA0
1180 || opP->index.reg > DATA7)
1181 && (opP->index.reg < ADDR0
1182 || opP->index.reg > ADDR7))
1183 || opP->index.size != SIZE_UNSPEC
1184 || opP->index.scale != 1))
1185 losing++;
1186 break;
1187
1188 case 's':
1189 if (opP->mode != CONTROL
1190 || ! (opP->reg == FPI
1191 || opP->reg == FPS
1192 || opP->reg == FPC))
1193 losing++;
1194 break;
1195
1196 case 'S':
1197 if (opP->mode != CONTROL || opP->reg != SR)
1198 losing++;
1199 break;
1200
1201 case 't':
1202 if (opP->mode != IMMED)
1203 losing++;
1204 else if (opP->disp.exp.X_op != O_constant
1205 || opP->disp.exp.X_add_number < 0
1206 || opP->disp.exp.X_add_number > 7)
1207 losing++;
1208 break;
1209
1210 case 'U':
1211 if (opP->mode != CONTROL || opP->reg != USP)
1212 losing++;
1213 break;
1214
1215 /* JF these are out of order. We could put them
1216 in order if we were willing to put up with
1217 bunches of #ifdef m68851s in the code.
1218
1219 Don't forget that you need these operands
1220 to use 68030 MMU instructions. */
1221 #ifndef NO_68851
1222 /* Memory addressing mode used by pflushr */
1223 case '|':
1224 if (opP->mode == CONTROL
1225 || opP->mode == FPREG
1226 || opP->mode == DREG
1227 || opP->mode == AREG
1228 || opP->mode == REGLST)
1229 losing++;
1230 break;
1231
1232 case 'f':
1233 if (opP->mode != CONTROL
1234 || (opP->reg != SFC && opP->reg != DFC))
1235 losing++;
1236 break;
1237
1238 case 'P':
1239 if (opP->mode != CONTROL
1240 || (opP->reg != TC
1241 && opP->reg != CAL
1242 && opP->reg != VAL
1243 && opP->reg != SCC
1244 && opP->reg != AC))
1245 losing++;
1246 break;
1247
1248 case 'V':
1249 if (opP->mode != CONTROL
1250 || opP->reg != VAL)
1251 losing++;
1252 break;
1253
1254 case 'W':
1255 if (opP->mode != CONTROL
1256 || (opP->reg != DRP
1257 && opP->reg != SRP
1258 && opP->reg != CRP))
1259 losing++;
1260 break;
1261
1262 case 'X':
1263 if (opP->mode != CONTROL
1264 || (!(opP->reg >= BAD && opP->reg <= BAD + 7)
1265 && !(opP->reg >= BAC && opP->reg <= BAC + 7)))
1266 losing++;
1267 break;
1268
1269 case 'Y':
1270 if (opP->mode != CONTROL || opP->reg != PSR)
1271 losing++;
1272 break;
1273
1274 case 'Z':
1275 if (opP->mode != CONTROL || opP->reg != PCSR)
1276 losing++;
1277 break;
1278 #endif
1279 case 'c':
1280 if (opP->mode != CONTROL
1281 || (opP->reg != NC
1282 && opP->reg != IC
1283 && opP->reg != DC
1284 && opP->reg != BC))
1285 {
1286 losing++;
1287 } /* not a cache specifier. */
1288 break;
1289
1290 case '_':
1291 if (opP->mode != ABSL)
1292 ++losing;
1293 break;
1294
1295 default:
1296 abort ();
1297 } /* switch on type of operand */
1298
1299 if (losing)
1300 break;
1301 } /* for each operand */
1302 } /* if immediately wrong */
1303
1304 if (!losing)
1305 {
1306 break;
1307 } /* got it. */
1308
1309 opcode = opcode->m_next;
1310
1311 if (!opcode)
1312 {
1313 if (ok_arch
1314 && !(ok_arch & current_architecture))
1315 {
1316 char buf[200], *cp;
1317 int len;
1318 strcpy (buf,
1319 "invalid instruction for this architecture; needs ");
1320 cp = buf + strlen (buf);
1321 switch (ok_arch)
1322 {
1323 case mfloat:
1324 strcpy (cp, "fpu (68040, 68060 or 68881/68882)");
1325 break;
1326 case mmmu:
1327 strcpy (cp, "mmu (68030 or 68851)");
1328 break;
1329 case m68020up:
1330 strcpy (cp, "68020 or higher");
1331 break;
1332 case m68000up:
1333 strcpy (cp, "68000 or higher");
1334 break;
1335 case m68010up:
1336 strcpy (cp, "68010 or higher");
1337 break;
1338 default:
1339 {
1340 int got_one = 0, idx;
1341 for (idx = 0; idx < sizeof (archs) / sizeof (archs[0]);
1342 idx++)
1343 {
1344 if (archs[idx].arch & ok_arch)
1345 {
1346 if (got_one)
1347 {
1348 strcpy (cp, " or ");
1349 cp += strlen (cp);
1350 }
1351 got_one = 1;
1352 strcpy (cp, archs[idx].name);
1353 cp += strlen (cp);
1354 }
1355 }
1356 }
1357 }
1358 len = cp - buf + 1;
1359 cp = malloc (len);
1360 strcpy (cp, buf);
1361 the_ins.error = cp;
1362 }
1363 else
1364 the_ins.error = "operands mismatch";
1365 return;
1366 } /* Fell off the end */
1367
1368 losing = 0;
1369 }
1370
1371 /* now assemble it */
1372
1373 the_ins.args = opcode->m_operands;
1374 the_ins.numargs = opcode->m_opnum;
1375 the_ins.numo = opcode->m_codenum;
1376 the_ins.opcode[0] = getone (opcode);
1377 the_ins.opcode[1] = gettwo (opcode);
1378
1379 for (s = the_ins.args, opP = &the_ins.operands[0]; *s; s += 2, opP++)
1380 {
1381 /* This switch is a doozy.
1382 Watch the first step; its a big one! */
1383 switch (s[0])
1384 {
1385
1386 case '*':
1387 case '~':
1388 case '%':
1389 case ';':
1390 case '@':
1391 case '!':
1392 case '&':
1393 case '$':
1394 case '?':
1395 case '/':
1396 case '`':
1397 #ifndef NO_68851
1398 case '|':
1399 #endif
1400 switch (opP->mode)
1401 {
1402 case IMMED:
1403 tmpreg = 0x3c; /* 7.4 */
1404 if (strchr ("bwl", s[1]))
1405 nextword = get_num (&opP->disp, 80);
1406 else
1407 nextword = get_num (&opP->disp, 0);
1408 if (isvar (&opP->disp))
1409 add_fix (s[1], &opP->disp, 0, 0);
1410 switch (s[1])
1411 {
1412 case 'b':
1413 if (!isbyte (nextword))
1414 opP->error = "operand out of range";
1415 addword (nextword);
1416 baseo = 0;
1417 break;
1418 case 'w':
1419 if (!isword (nextword))
1420 opP->error = "operand out of range";
1421 addword (nextword);
1422 baseo = 0;
1423 break;
1424 case 'l':
1425 addword (nextword >> 16);
1426 addword (nextword);
1427 baseo = 0;
1428 break;
1429
1430 case 'f':
1431 baseo = 2;
1432 outro = 8;
1433 break;
1434 case 'F':
1435 baseo = 4;
1436 outro = 11;
1437 break;
1438 case 'x':
1439 baseo = 6;
1440 outro = 15;
1441 break;
1442 case 'p':
1443 baseo = 6;
1444 outro = -1;
1445 break;
1446 default:
1447 abort ();
1448 }
1449 if (!baseo)
1450 break;
1451
1452 /* We gotta put out some float */
1453 if (op (&opP->disp) != O_big)
1454 {
1455 valueT val;
1456 int gencnt;
1457
1458 /* Can other cases happen here? */
1459 if (op (&opP->disp) != O_constant)
1460 abort ();
1461
1462 val = (valueT) offs (&opP->disp);
1463 gencnt = 0;
1464 do
1465 {
1466 generic_bignum[gencnt] = (LITTLENUM_TYPE) val;
1467 val >>= LITTLENUM_NUMBER_OF_BITS;
1468 ++gencnt;
1469 }
1470 while (val != 0);
1471 offs (&opP->disp) = gencnt;
1472 }
1473 if (offs (&opP->disp) > 0)
1474 {
1475 if (offs (&opP->disp) > baseo)
1476 {
1477 as_warn ("Bignum too big for %c format; truncated",
1478 s[1]);
1479 offs (&opP->disp) = baseo;
1480 }
1481 baseo -= offs (&opP->disp);
1482 while (baseo--)
1483 addword (0);
1484 for (wordp = generic_bignum + offs (&opP->disp) - 1;
1485 offs (&opP->disp)--;
1486 --wordp)
1487 addword (*wordp);
1488 break;
1489 }
1490 gen_to_words (words, baseo, (long) outro);
1491 for (wordp = words; baseo--; wordp++)
1492 addword (*wordp);
1493 break;
1494 case DREG:
1495 tmpreg = opP->reg - DATA; /* 0.dreg */
1496 break;
1497 case AREG:
1498 tmpreg = 0x08 + opP->reg - ADDR; /* 1.areg */
1499 break;
1500 case AINDR:
1501 tmpreg = 0x10 + opP->reg - ADDR; /* 2.areg */
1502 break;
1503 case ADEC:
1504 tmpreg = 0x20 + opP->reg - ADDR; /* 4.areg */
1505 break;
1506 case AINC:
1507 tmpreg = 0x18 + opP->reg - ADDR; /* 3.areg */
1508 break;
1509 case DISP:
1510
1511 nextword = get_num (&opP->disp, 80);
1512
1513 if (opP->reg == PC
1514 && ! isvar (&opP->disp)
1515 && m68k_abspcadd)
1516 {
1517 opP->disp.exp.X_op = O_symbol;
1518 #ifndef BFD_ASSEMBLER
1519 opP->disp.exp.X_add_symbol = &abs_symbol;
1520 #else
1521 opP->disp.exp.X_add_symbol =
1522 section_symbol (absolute_section);
1523 #endif
1524 }
1525
1526 /* Force into index mode. Hope this works */
1527
1528 /* We do the first bit for 32-bit displacements, and the
1529 second bit for 16 bit ones. It is possible that we
1530 should make the default be WORD instead of LONG, but
1531 I think that'd break GCC, so we put up with a little
1532 inefficiency for the sake of working output. */
1533
1534 if (!issword (nextword)
1535 || (isvar (&opP->disp)
1536 && ((opP->disp.size == SIZE_UNSPEC
1537 && flag_short_refs == 0
1538 && cpu_of_arch (current_architecture) >= m68020)
1539 || opP->disp.size == SIZE_LONG)))
1540 {
1541 if (opP->reg == PC)
1542 tmpreg = 0x3B; /* 7.3 */
1543 else
1544 tmpreg = 0x30 + opP->reg - ADDR; /* 6.areg */
1545 if (isvar (&opP->disp))
1546 {
1547 if (opP->reg == PC)
1548 {
1549 #if 0
1550 addword (0x0170);
1551 add_fix ('l', &opP->disp, 1, 2);
1552 addword (0), addword (0);
1553 #else
1554 add_frag (adds (&opP->disp),
1555 offs (&opP->disp),
1556 TAB (PCLEA, SZ_UNDEF));
1557 #endif
1558 break;
1559 }
1560 else
1561 {
1562 addword (0x0170);
1563 add_fix ('l', &opP->disp, 0, 0);
1564 }
1565 }
1566 else
1567 addword (0x0170);
1568 addword (nextword >> 16);
1569 }
1570 else
1571 {
1572 if (opP->reg == PC)
1573 tmpreg = 0x3A; /* 7.2 */
1574 else
1575 tmpreg = 0x28 + opP->reg - ADDR; /* 5.areg */
1576
1577 if (isvar (&opP->disp))
1578 {
1579 if (opP->reg == PC)
1580 {
1581 add_fix ('w', &opP->disp, 1, 0);
1582 }
1583 else
1584 add_fix ('w', &opP->disp, 0, 0);
1585 }
1586 }
1587 addword (nextword);
1588 break;
1589
1590 case POST:
1591 case PRE:
1592 case BASE:
1593 nextword = 0;
1594 baseo = get_num (&opP->disp, 80);
1595 if (opP->mode == POST || opP->mode == PRE)
1596 outro = get_num (&opP->odisp, 80);
1597 /* Figure out the `addressing mode'.
1598 Also turn on the BASE_DISABLE bit, if needed. */
1599 if (opP->reg == PC || opP->reg == ZPC)
1600 {
1601 tmpreg = 0x3b; /* 7.3 */
1602 if (opP->reg == ZPC)
1603 nextword |= 0x80;
1604 }
1605 else if (opP->reg == 0)
1606 {
1607 nextword |= 0x80;
1608 tmpreg = 0x30; /* 6.garbage */
1609 }
1610 else if (opP->reg >= ZADDR0 && opP->reg <= ZADDR7)
1611 {
1612 nextword |= 0x80;
1613 tmpreg = 0x30 + opP->reg - ZADDR0;
1614 }
1615 else
1616 tmpreg = 0x30 + opP->reg - ADDR; /* 6.areg */
1617
1618 siz1 = opP->disp.size;
1619 if (opP->mode == POST || opP->mode == PRE)
1620 siz2 = opP->odisp.size;
1621 else
1622 siz2 = SIZE_UNSPEC;
1623
1624 /* Index register stuff */
1625 if (opP->index.reg != 0
1626 && opP->index.reg >= DATA
1627 && opP->index.reg <= ADDR7)
1628 {
1629 nextword |= (opP->index.reg - DATA) << 12;
1630
1631 if (opP->index.size == SIZE_UNSPEC
1632 || opP->index.size == SIZE_LONG)
1633 nextword |= 0x800;
1634
1635 if (cpu_of_arch (current_architecture) < m68020)
1636 {
1637 if (opP->index.scale != 1)
1638 {
1639 opP->error =
1640 "scale factor invalid on this architecture; needs 68020 or higher";
1641 }
1642 }
1643
1644 switch (opP->index.scale)
1645 {
1646 case 1:
1647 break;
1648 case 2:
1649 nextword |= 0x200;
1650 break;
1651 case 4:
1652 nextword |= 0x400;
1653 break;
1654 case 8:
1655 nextword |= 0x600;
1656 break;
1657 default:
1658 abort ();
1659 }
1660 /* IF its simple,
1661 GET US OUT OF HERE! */
1662
1663 /* Must be INDEX, with an index register. Address
1664 register cannot be ZERO-PC, and either :b was
1665 forced, or we know it will fit. For a 68000 or
1666 68010, force this mode anyways, because the
1667 larger modes aren't supported. */
1668 if (opP->mode == BASE
1669 && ((opP->reg >= ADDR0
1670 && opP->reg <= ADDR7)
1671 || opP->reg == PC))
1672 {
1673 if (siz1 == SIZE_BYTE
1674 || cpu_of_arch (current_architecture) < m68020
1675 || (siz1 == SIZE_UNSPEC
1676 && ! isvar (&opP->disp)
1677 && issbyte (baseo)))
1678 {
1679 nextword += baseo & 0xff;
1680 addword (nextword);
1681 if (isvar (&opP->disp))
1682 {
1683 /* Do a byte relocation. If it doesn't
1684 fit (possible on m68000) let the
1685 fixup processing complain later. */
1686 if (opP->reg == PC)
1687 add_fix ('B', &opP->disp, 1, 1);
1688 else
1689 add_fix ('B', &opP->disp, 0, 0);
1690 }
1691 else if (siz1 != SIZE_BYTE)
1692 {
1693 if (siz1 != SIZE_UNSPEC)
1694 as_warn ("Forcing byte displacement");
1695 if (! issbyte (baseo))
1696 opP->error = "byte displacement out of range";
1697 }
1698
1699 break;
1700 }
1701 else if (siz1 == SIZE_UNSPEC
1702 && opP->reg == PC
1703 && isvar (&opP->disp)
1704 && subs (&opP->disp) == NULL)
1705 {
1706 nextword += baseo & 0xff;
1707 addword (nextword);
1708 add_frag (adds (&opP->disp), offs (&opP->disp),
1709 TAB (PCINDEX, SZ_UNDEF));
1710
1711 break;
1712 }
1713 }
1714 }
1715 else
1716 {
1717 nextword |= 0x40; /* No index reg */
1718 if (opP->index.reg >= ZDATA0
1719 && opP->index.reg <= ZDATA7)
1720 nextword |= (opP->index.reg - ZDATA0) << 12;
1721 else if (opP->index.reg >= ZADDR0
1722 || opP->index.reg <= ZADDR7)
1723 nextword |= (opP->index.reg - ZADDR0 + 8) << 12;
1724 }
1725
1726 /* It isn't simple. */
1727
1728 if (cpu_of_arch (current_architecture) < m68020)
1729 opP->error =
1730 "invalid operand mode for this architecture; needs 68020 or higher";
1731
1732 nextword |= 0x100;
1733 /* If the guy specified a width, we assume that it is
1734 wide enough. Maybe it isn't. If so, we lose. */
1735 switch (siz1)
1736 {
1737 case SIZE_UNSPEC:
1738 if (isvar (&opP->disp)
1739 ? m68k_rel32
1740 : ! issword (baseo))
1741 {
1742 siz1 = SIZE_LONG;
1743 nextword |= 0x30;
1744 }
1745 else if (! isvar (&opP->disp) && baseo == 0)
1746 nextword |= 0x10;
1747 else
1748 {
1749 nextword |= 0x20;
1750 siz1 = SIZE_WORD;
1751 }
1752 break;
1753 case SIZE_BYTE:
1754 as_warn (":b not permitted; defaulting to :w");
1755 /* Fall through. */
1756 case SIZE_WORD:
1757 nextword |= 0x20;
1758 break;
1759 case SIZE_LONG:
1760 nextword |= 0x30;
1761 break;
1762 }
1763
1764 /* Figure out innner displacement stuff */
1765 if (opP->mode == POST || opP->mode == PRE)
1766 {
1767 switch (siz2)
1768 {
1769 case SIZE_UNSPEC:
1770 if (isvar (&opP->odisp)
1771 ? m68k_rel32
1772 : ! issword (outro))
1773 {
1774 siz2 = SIZE_LONG;
1775 nextword |= 0x3;
1776 }
1777 else if (! isvar (&opP->odisp) && outro == 0)
1778 nextword |= 0x1;
1779 else
1780 {
1781 nextword |= 0x2;
1782 siz2 = SIZE_WORD;
1783 }
1784 break;
1785 case 1:
1786 as_warn (":b not permitted; defaulting to :w");
1787 /* Fall through. */
1788 case 2:
1789 nextword |= 0x2;
1790 break;
1791 case 3:
1792 nextword |= 0x3;
1793 break;
1794 }
1795 if (opP->mode == POST
1796 && (nextword & 0x40) == 0)
1797 nextword |= 0x04;
1798 }
1799 addword (nextword);
1800
1801 if (siz1 != SIZE_UNSPEC && isvar (&opP->disp))
1802 {
1803 if (opP->reg == PC || opP->reg == ZPC)
1804 add_fix (siz1 == SIZE_LONG ? 'l' : 'w', &opP->disp, 1, 2);
1805 else
1806 add_fix (siz1 == SIZE_LONG ? 'l' : 'w', &opP->disp, 0, 0);
1807 }
1808 if (siz1 == SIZE_LONG)
1809 addword (baseo >> 16);
1810 if (siz1 != SIZE_UNSPEC)
1811 addword (baseo);
1812
1813 if (siz2 != SIZE_UNSPEC && isvar (&opP->odisp))
1814 add_fix (siz2 == SIZE_LONG ? 'l' : 'w', &opP->odisp, 0, 0);
1815 if (siz2 == SIZE_LONG)
1816 addword (outro >> 16);
1817 if (siz2 != SIZE_UNSPEC)
1818 addword (outro);
1819
1820 break;
1821
1822 case ABSL:
1823 nextword = get_num (&opP->disp, 80);
1824 switch (opP->disp.size)
1825 {
1826 default:
1827 abort ();
1828 case SIZE_UNSPEC:
1829 if (!isvar (&opP->disp) && issword (offs (&opP->disp)))
1830 {
1831 tmpreg = 0x38; /* 7.0 */
1832 addword (nextword);
1833 break;
1834 }
1835 /* Don't generate pc relative code on 68010 and
1836 68000. */
1837 if (isvar (&opP->disp)
1838 && !subs (&opP->disp)
1839 && adds (&opP->disp)
1840 && S_GET_SEGMENT (adds (&opP->disp)) == now_seg
1841 && cpu_of_arch (current_architecture) >= m68020
1842 && !flag_long_jumps
1843 && !strchr ("~%&$?", s[0]))
1844 {
1845 tmpreg = 0x3A; /* 7.2 */
1846 add_frag (adds (&opP->disp),
1847 offs (&opP->disp),
1848 TAB (PCREL, SZ_UNDEF));
1849 break;
1850 }
1851 /* Fall through into long */
1852 case SIZE_LONG:
1853 if (isvar (&opP->disp))
1854 add_fix ('l', &opP->disp, 0, 0);
1855
1856 tmpreg = 0x39;/* 7.1 mode */
1857 addword (nextword >> 16);
1858 addword (nextword);
1859 break;
1860
1861 case SIZE_WORD: /* Word */
1862 if (isvar (&opP->disp))
1863 add_fix ('w', &opP->disp, 0, 0);
1864
1865 tmpreg = 0x38;/* 7.0 mode */
1866 addword (nextword);
1867 break;
1868 }
1869 break;
1870 case CONTROL:
1871 case FPREG:
1872 default:
1873 as_bad ("unknown/incorrect operand");
1874 /* abort(); */
1875 }
1876 install_gen_operand (s[1], tmpreg);
1877 break;
1878
1879 case '#':
1880 case '^':
1881 switch (s[1])
1882 { /* JF: I hate floating point! */
1883 case 'j':
1884 tmpreg = 70;
1885 break;
1886 case '8':
1887 tmpreg = 20;
1888 break;
1889 case 'C':
1890 tmpreg = 50;
1891 break;
1892 case '3':
1893 default:
1894 tmpreg = 80;
1895 break;
1896 }
1897 tmpreg = get_num (&opP->disp, tmpreg);
1898 if (isvar (&opP->disp))
1899 add_fix (s[1], &opP->disp, 0, 0);
1900 switch (s[1])
1901 {
1902 case 'b': /* Danger: These do no check for
1903 certain types of overflow.
1904 user beware! */
1905 if (!isbyte (tmpreg))
1906 opP->error = "out of range";
1907 insop (tmpreg, opcode);
1908 if (isvar (&opP->disp))
1909 the_ins.reloc[the_ins.nrel - 1].n = (opcode->m_codenum) * 2;
1910 break;
1911 case 'w':
1912 if (!isword (tmpreg))
1913 opP->error = "out of range";
1914 insop (tmpreg, opcode);
1915 if (isvar (&opP->disp))
1916 the_ins.reloc[the_ins.nrel - 1].n = (opcode->m_codenum) * 2;
1917 break;
1918 case 'l':
1919 /* Because of the way insop works, we put these two out
1920 backwards. */
1921 insop (tmpreg, opcode);
1922 insop (tmpreg >> 16, opcode);
1923 if (isvar (&opP->disp))
1924 the_ins.reloc[the_ins.nrel - 1].n = (opcode->m_codenum) * 2;
1925 break;
1926 case '3':
1927 tmpreg &= 0xFF;
1928 case '8':
1929 case 'C':
1930 install_operand (s[1], tmpreg);
1931 break;
1932 default:
1933 abort ();
1934 }
1935 break;
1936
1937 case '+':
1938 case '-':
1939 case 'A':
1940 case 'a':
1941 install_operand (s[1], opP->reg - ADDR);
1942 break;
1943
1944 case 'B':
1945 tmpreg = get_num (&opP->disp, 80);
1946 switch (s[1])
1947 {
1948 case 'B':
1949 /* The pc_fix argument winds up in fx_pcrel_adjust,
1950 which is a char, and may therefore be unsigned. We
1951 want to pass -1, but we pass 64 instead, and convert
1952 back in md_pcrel_from. */
1953 add_fix ('B', &opP->disp, 1, 64);
1954 break;
1955 case 'W':
1956 add_fix ('w', &opP->disp, 1, 0);
1957 addword (0);
1958 break;
1959 case 'L':
1960 long_branch:
1961 if (cpu_of_arch (current_architecture) < m68020)
1962 as_warn ("Can't use long branches on 68000/68010");
1963 the_ins.opcode[the_ins.numo - 1] |= 0xff;
1964 add_fix ('l', &opP->disp, 1, 0);
1965 addword (0);
1966 addword (0);
1967 break;
1968 case 'g':
1969 if (subs (&opP->disp)) /* We can't relax it */
1970 goto long_branch;
1971
1972 /* This could either be a symbol, or an absolute
1973 address. No matter, the frag hacking will finger it
1974 out. Not quite: it can't switch from BRANCH to
1975 BCC68000 for the case where opnd is absolute (it
1976 needs to use the 68000 hack since no conditional abs
1977 jumps). */
1978 if (((cpu_of_arch (current_architecture) < m68020)
1979 || (0 == adds (&opP->disp)))
1980 && (the_ins.opcode[0] >= 0x6200)
1981 && (the_ins.opcode[0] <= 0x6f00))
1982 add_frag (adds (&opP->disp), offs (&opP->disp),
1983 TAB (BCC68000, SZ_UNDEF));
1984 else
1985 add_frag (adds (&opP->disp), offs (&opP->disp),
1986 TAB (ABRANCH, SZ_UNDEF));
1987 break;
1988 case 'w':
1989 if (isvar (&opP->disp))
1990 {
1991 #if 1
1992 /* check for DBcc instruction */
1993 if ((the_ins.opcode[0] & 0xf0f8) == 0x50c8)
1994 {
1995 /* size varies if patch */
1996 /* needed for long form */
1997 add_frag (adds (&opP->disp), offs (&opP->disp),
1998 TAB (DBCC, SZ_UNDEF));
1999 break;
2000 }
2001 #endif
2002 add_fix ('w', &opP->disp, 1, 0);
2003 }
2004 addword (0);
2005 break;
2006 case 'C': /* Fixed size LONG coproc branches */
2007 add_fix ('l', &opP->disp, 1, 0);
2008 addword (0);
2009 addword (0);
2010 break;
2011 case 'c': /* Var size Coprocesssor branches */
2012 if (subs (&opP->disp))
2013 {
2014 add_fix ('l', &opP->disp, 1, 0);
2015 add_frag ((symbolS *) 0, (long) 0, TAB (FBRANCH, LONG));
2016 }
2017 else if (adds (&opP->disp))
2018 add_frag (adds (&opP->disp), offs (&opP->disp),
2019 TAB (FBRANCH, SZ_UNDEF));
2020 else
2021 {
2022 /* add_frag((symbolS *) 0, offs(&opP->disp),
2023 TAB(FBRANCH,SHORT)); */
2024 the_ins.opcode[the_ins.numo - 1] |= 0x40;
2025 add_fix ('l', &opP->disp, 1, 0);
2026 addword (0);
2027 addword (0);
2028 }
2029 break;
2030 default:
2031 abort ();
2032 }
2033 break;
2034
2035 case 'C': /* Ignore it */
2036 break;
2037
2038 case 'd': /* JF this is a kludge */
2039 install_operand ('s', opP->reg - ADDR);
2040 tmpreg = get_num (&opP->disp, 80);
2041 if (!issword (tmpreg))
2042 {
2043 as_warn ("Expression out of range, using 0");
2044 tmpreg = 0;
2045 }
2046 addword (tmpreg);
2047 break;
2048
2049 case 'D':
2050 install_operand (s[1], opP->reg - DATA);
2051 break;
2052
2053 case 'F':
2054 install_operand (s[1], opP->reg - FP0);
2055 break;
2056
2057 case 'I':
2058 tmpreg = opP->reg - COP0;
2059 install_operand (s[1], tmpreg);
2060 break;
2061
2062 case 'J': /* JF foo */
2063 switch (opP->reg)
2064 {
2065 case SFC:
2066 tmpreg = 0x000;
2067 break;
2068 case DFC:
2069 tmpreg = 0x001;
2070 break;
2071 case CACR:
2072 tmpreg = 0x002;
2073 break;
2074 case TC:
2075 tmpreg = 0x003;
2076 break;
2077 case ITT0:
2078 tmpreg = 0x004;
2079 break;
2080 case ITT1:
2081 tmpreg = 0x005;
2082 break;
2083 case DTT0:
2084 tmpreg = 0x006;
2085 break;
2086 case DTT1:
2087 tmpreg = 0x007;
2088 break;
2089 case BUSCR:
2090 tmpreg = 0x008;
2091 break;
2092
2093 case USP:
2094 tmpreg = 0x800;
2095 break;
2096 case VBR:
2097 tmpreg = 0x801;
2098 break;
2099 case CAAR:
2100 tmpreg = 0x802;
2101 break;
2102 case MSP:
2103 tmpreg = 0x803;
2104 break;
2105 case ISP:
2106 tmpreg = 0x804;
2107 break;
2108 case MMUSR:
2109 tmpreg = 0x805;
2110 break;
2111 case URP:
2112 tmpreg = 0x806;
2113 break;
2114 case SRP:
2115 tmpreg = 0x807;
2116 break;
2117 case PCR:
2118 tmpreg = 0x808;
2119 break;
2120 default:
2121 abort ();
2122 }
2123 install_operand (s[1], tmpreg);
2124 break;
2125
2126 case 'k':
2127 tmpreg = get_num (&opP->disp, 55);
2128 install_operand (s[1], tmpreg & 0x7f);
2129 break;
2130
2131 case 'l':
2132 tmpreg = opP->mask;
2133 if (s[1] == 'w')
2134 {
2135 if (tmpreg & 0x7FF0000)
2136 as_bad ("Floating point register in register list");
2137 insop (reverse_16_bits (tmpreg), opcode);
2138 }
2139 else
2140 {
2141 if (tmpreg & 0x700FFFF)
2142 as_bad ("Wrong register in floating-point reglist");
2143 install_operand (s[1], reverse_8_bits (tmpreg >> 16));
2144 }
2145 break;
2146
2147 case 'L':
2148 tmpreg = opP->mask;
2149 if (s[1] == 'w')
2150 {
2151 if (tmpreg & 0x7FF0000)
2152 as_bad ("Floating point register in register list");
2153 insop (tmpreg, opcode);
2154 }
2155 else if (s[1] == '8')
2156 {
2157 if (tmpreg & 0x0FFFFFF)
2158 as_bad ("incorrect register in reglist");
2159 install_operand (s[1], tmpreg >> 24);
2160 }
2161 else
2162 {
2163 if (tmpreg & 0x700FFFF)
2164 as_bad ("wrong register in floating-point reglist");
2165 else
2166 install_operand (s[1], tmpreg >> 16);
2167 }
2168 break;
2169
2170 case 'M':
2171 install_operand (s[1], get_num (&opP->disp, 60));
2172 break;
2173
2174 case 'O':
2175 tmpreg = ((opP->mode == DREG)
2176 ? 0x20 + opP->reg - DATA
2177 : (get_num (&opP->disp, 40) & 0x1F));
2178 install_operand (s[1], tmpreg);
2179 break;
2180
2181 case 'Q':
2182 tmpreg = get_num (&opP->disp, 10);
2183 if (tmpreg == 8)
2184 tmpreg = 0;
2185 install_operand (s[1], tmpreg);
2186 break;
2187
2188 case 'R':
2189 /* This depends on the fact that ADDR registers are eight
2190 more than their corresponding DATA regs, so the result
2191 will have the ADDR_REG bit set */
2192 install_operand (s[1], opP->reg - DATA);
2193 break;
2194
2195 case 'r':
2196 if (opP->mode == AINDR)
2197 install_operand (s[1], opP->reg - DATA);
2198 else
2199 install_operand (s[1], opP->index.reg - DATA);
2200 break;
2201
2202 case 's':
2203 if (opP->reg == FPI)
2204 tmpreg = 0x1;
2205 else if (opP->reg == FPS)
2206 tmpreg = 0x2;
2207 else if (opP->reg == FPC)
2208 tmpreg = 0x4;
2209 else
2210 abort ();
2211 install_operand (s[1], tmpreg);
2212 break;
2213
2214 case 'S': /* Ignore it */
2215 break;
2216
2217 case 'T':
2218 install_operand (s[1], get_num (&opP->disp, 30));
2219 break;
2220
2221 case 'U': /* Ignore it */
2222 break;
2223
2224 case 'c':
2225 switch (opP->reg)
2226 {
2227 case NC:
2228 tmpreg = 0;
2229 break;
2230 case DC:
2231 tmpreg = 1;
2232 break;
2233 case IC:
2234 tmpreg = 2;
2235 break;
2236 case BC:
2237 tmpreg = 3;
2238 break;
2239 default:
2240 as_fatal ("failed sanity check");
2241 } /* switch on cache token */
2242 install_operand (s[1], tmpreg);
2243 break;
2244 #ifndef NO_68851
2245 /* JF: These are out of order, I fear. */
2246 case 'f':
2247 switch (opP->reg)
2248 {
2249 case SFC:
2250 tmpreg = 0;
2251 break;
2252 case DFC:
2253 tmpreg = 1;
2254 break;
2255 default:
2256 abort ();
2257 }
2258 install_operand (s[1], tmpreg);
2259 break;
2260
2261 case 'P':
2262 switch (opP->reg)
2263 {
2264 case TC:
2265 tmpreg = 0;
2266 break;
2267 case CAL:
2268 tmpreg = 4;
2269 break;
2270 case VAL:
2271 tmpreg = 5;
2272 break;
2273 case SCC:
2274 tmpreg = 6;
2275 break;
2276 case AC:
2277 tmpreg = 7;
2278 break;
2279 default:
2280 abort ();
2281 }
2282 install_operand (s[1], tmpreg);
2283 break;
2284
2285 case 'V':
2286 if (opP->reg == VAL)
2287 break;
2288 abort ();
2289
2290 case 'W':
2291 switch (opP->reg)
2292 {
2293 case DRP:
2294 tmpreg = 1;
2295 break;
2296 case SRP:
2297 tmpreg = 2;
2298 break;
2299 case CRP:
2300 tmpreg = 3;
2301 break;
2302 default:
2303 abort ();
2304 }
2305 install_operand (s[1], tmpreg);
2306 break;
2307
2308 case 'X':
2309 switch (opP->reg)
2310 {
2311 case BAD:
2312 case BAD + 1:
2313 case BAD + 2:
2314 case BAD + 3:
2315 case BAD + 4:
2316 case BAD + 5:
2317 case BAD + 6:
2318 case BAD + 7:
2319 tmpreg = (4 << 10) | ((opP->reg - BAD) << 2);
2320 break;
2321
2322 case BAC:
2323 case BAC + 1:
2324 case BAC + 2:
2325 case BAC + 3:
2326 case BAC + 4:
2327 case BAC + 5:
2328 case BAC + 6:
2329 case BAC + 7:
2330 tmpreg = (5 << 10) | ((opP->reg - BAC) << 2);
2331 break;
2332
2333 default:
2334 abort ();
2335 }
2336 install_operand (s[1], tmpreg);
2337 break;
2338 case 'Y':
2339 know (opP->reg == PSR);
2340 break;
2341 case 'Z':
2342 know (opP->reg == PCSR);
2343 break;
2344 #endif /* m68851 */
2345 case '3':
2346 switch (opP->reg)
2347 {
2348 case TT0:
2349 tmpreg = 2;
2350 break;
2351 case TT1:
2352 tmpreg = 3;
2353 break;
2354 default:
2355 abort ();
2356 }
2357 install_operand (s[1], tmpreg);
2358 break;
2359 case 't':
2360 tmpreg = get_num (&opP->disp, 20);
2361 install_operand (s[1], tmpreg);
2362 break;
2363 case '_': /* used only for move16 absolute 32-bit address */
2364 tmpreg = get_num (&opP->disp, 80);
2365 addword (tmpreg >> 16);
2366 addword (tmpreg & 0xFFFF);
2367 break;
2368 default:
2369 abort ();
2370 }
2371 }
2372
2373 /* By the time whe get here (FINALLY) the_ins contains the complete
2374 instruction, ready to be emitted. . . */
2375 }
2376
2377 static int
2378 reverse_16_bits (in)
2379 int in;
2380 {
2381 int out = 0;
2382 int n;
2383
2384 static int mask[16] =
2385 {
2386 0x0001, 0x0002, 0x0004, 0x0008, 0x0010, 0x0020, 0x0040, 0x0080,
2387 0x0100, 0x0200, 0x0400, 0x0800, 0x1000, 0x2000, 0x4000, 0x8000
2388 };
2389 for (n = 0; n < 16; n++)
2390 {
2391 if (in & mask[n])
2392 out |= mask[15 - n];
2393 }
2394 return out;
2395 } /* reverse_16_bits() */
2396
2397 static int
2398 reverse_8_bits (in)
2399 int in;
2400 {
2401 int out = 0;
2402 int n;
2403
2404 static int mask[8] =
2405 {
2406 0x0001, 0x0002, 0x0004, 0x0008, 0x0010, 0x0020, 0x0040, 0x0080,
2407 };
2408
2409 for (n = 0; n < 8; n++)
2410 {
2411 if (in & mask[n])
2412 out |= mask[7 - n];
2413 }
2414 return out;
2415 } /* reverse_8_bits() */
2416
2417 /* Cause an extra frag to be generated here, inserting up to 10 bytes
2418 (that value is chosen in the frag_var call in md_assemble). TYPE
2419 is the subtype of the frag to be generated; its primary type is
2420 rs_machine_dependent.
2421
2422 The TYPE parameter is also used by md_convert_frag_1 and
2423 md_estimate_size_before_relax. The appropriate type of fixup will
2424 be emitted by md_convert_frag_1.
2425
2426 ADD becomes the FR_SYMBOL field of the frag, and OFF the FR_OFFSET. */
2427 static void
2428 install_operand (mode, val)
2429 int mode;
2430 int val;
2431 {
2432 switch (mode)
2433 {
2434 case 's':
2435 the_ins.opcode[0] |= val & 0xFF; /* JF FF is for M kludge */
2436 break;
2437 case 'd':
2438 the_ins.opcode[0] |= val << 9;
2439 break;
2440 case '1':
2441 the_ins.opcode[1] |= val << 12;
2442 break;
2443 case '2':
2444 the_ins.opcode[1] |= val << 6;
2445 break;
2446 case '3':
2447 the_ins.opcode[1] |= val;
2448 break;
2449 case '4':
2450 the_ins.opcode[2] |= val << 12;
2451 break;
2452 case '5':
2453 the_ins.opcode[2] |= val << 6;
2454 break;
2455 case '6':
2456 /* DANGER! This is a hack to force cas2l and cas2w cmds to be
2457 three words long! */
2458 the_ins.numo++;
2459 the_ins.opcode[2] |= val;
2460 break;
2461 case '7':
2462 the_ins.opcode[1] |= val << 7;
2463 break;
2464 case '8':
2465 the_ins.opcode[1] |= val << 10;
2466 break;
2467 #ifndef NO_68851
2468 case '9':
2469 the_ins.opcode[1] |= val << 5;
2470 break;
2471 #endif
2472
2473 case 't':
2474 the_ins.opcode[1] |= (val << 10) | (val << 7);
2475 break;
2476 case 'D':
2477 the_ins.opcode[1] |= (val << 12) | val;
2478 break;
2479 case 'g':
2480 the_ins.opcode[0] |= val = 0xff;
2481 break;
2482 case 'i':
2483 the_ins.opcode[0] |= val << 9;
2484 break;
2485 case 'C':
2486 the_ins.opcode[1] |= val;
2487 break;
2488 case 'j':
2489 the_ins.opcode[1] |= val;
2490 the_ins.numo++; /* What a hack */
2491 break;
2492 case 'k':
2493 the_ins.opcode[1] |= val << 4;
2494 break;
2495 case 'b':
2496 case 'w':
2497 case 'l':
2498 break;
2499 case 'e':
2500 the_ins.opcode[0] |= (val << 6);
2501 break;
2502 case 'L':
2503 the_ins.opcode[1] = (val >> 16);
2504 the_ins.opcode[2] = val & 0xffff;
2505 break;
2506 case 'c':
2507 default:
2508 as_fatal ("failed sanity check.");
2509 }
2510 } /* install_operand() */
2511
2512 static void
2513 install_gen_operand (mode, val)
2514 int mode;
2515 int val;
2516 {
2517 switch (mode)
2518 {
2519 case 's':
2520 the_ins.opcode[0] |= val;
2521 break;
2522 case 'd':
2523 /* This is a kludge!!! */
2524 the_ins.opcode[0] |= (val & 0x07) << 9 | (val & 0x38) << 3;
2525 break;
2526 case 'b':
2527 case 'w':
2528 case 'l':
2529 case 'f':
2530 case 'F':
2531 case 'x':
2532 case 'p':
2533 the_ins.opcode[0] |= val;
2534 break;
2535 /* more stuff goes here */
2536 default:
2537 as_fatal ("failed sanity check.");
2538 }
2539 } /* install_gen_operand() */
2540
2541 /*
2542 * verify that we have some number of paren pairs, do m68k_ip_op(), and
2543 * then deal with the bitfield hack.
2544 */
2545
2546 static char *
2547 crack_operand (str, opP)
2548 register char *str;
2549 register struct m68k_op *opP;
2550 {
2551 register int parens;
2552 register int c;
2553 register char *beg_str;
2554 int inquote = 0;
2555
2556 if (!str)
2557 {
2558 return str;
2559 }
2560 beg_str = str;
2561 for (parens = 0; *str && (parens > 0 || inquote || notend (str)); str++)
2562 {
2563 if (! inquote)
2564 {
2565 if (*str == '(')
2566 parens++;
2567 else if (*str == ')')
2568 {
2569 if (!parens)
2570 { /* ERROR */
2571 opP->error = "Extra )";
2572 return str;
2573 }
2574 --parens;
2575 }
2576 }
2577 if (flag_mri && *str == '\'')
2578 inquote = ! inquote;
2579 }
2580 if (!*str && parens)
2581 { /* ERROR */
2582 opP->error = "Missing )";
2583 return str;
2584 }
2585 c = *str;
2586 *str = '\0';
2587 if (m68k_ip_op (beg_str, opP) != 0)
2588 {
2589 *str = c;
2590 return str;
2591 }
2592 *str = c;
2593 if (c == '}')
2594 c = *++str; /* JF bitfield hack */
2595 if (c)
2596 {
2597 c = *++str;
2598 if (!c)
2599 as_bad ("Missing operand");
2600 }
2601 return str;
2602 }
2603
2604 /* This is the guts of the machine-dependent assembler. STR points to a
2605 machine dependent instruction. This function is supposed to emit
2606 the frags/bytes it assembles to.
2607 */
2608
2609 void
2610 insert_reg (regname, regnum)
2611 char *regname;
2612 int regnum;
2613 {
2614 char buf[100];
2615 int i;
2616
2617 #ifdef REGISTER_PREFIX
2618 if (!flag_reg_prefix_optional)
2619 {
2620 buf[0] = REGISTER_PREFIX;
2621 strcpy (buf + 1, regname);
2622 regname = buf;
2623 }
2624 #endif
2625
2626 symbol_table_insert (symbol_new (regname, reg_section, regnum,
2627 &zero_address_frag));
2628
2629 for (i = 0; regname[i]; i++)
2630 buf[i] = islower (regname[i]) ? toupper (regname[i]) : regname[i];
2631 buf[i] = '\0';
2632
2633 symbol_table_insert (symbol_new (buf, reg_section, regnum,
2634 &zero_address_frag));
2635 }
2636
2637 struct init_entry
2638 {
2639 const char *name;
2640 int number;
2641 };
2642
2643 static const struct init_entry init_table[] =
2644 {
2645 { "d0", DATA0 },
2646 { "d1", DATA1 },
2647 { "d2", DATA2 },
2648 { "d3", DATA3 },
2649 { "d4", DATA4 },
2650 { "d5", DATA5 },
2651 { "d6", DATA6 },
2652 { "d7", DATA7 },
2653 { "a0", ADDR0 },
2654 { "a1", ADDR1 },
2655 { "a2", ADDR2 },
2656 { "a3", ADDR3 },
2657 { "a4", ADDR4 },
2658 { "a5", ADDR5 },
2659 { "a6", ADDR6 },
2660 { "fp", ADDR6 },
2661 { "a7", ADDR7 },
2662 { "sp", ADDR7 },
2663 { "ssp", ADDR7 },
2664 { "fp0", FP0 },
2665 { "fp1", FP1 },
2666 { "fp2", FP2 },
2667 { "fp3", FP3 },
2668 { "fp4", FP4 },
2669 { "fp5", FP5 },
2670 { "fp6", FP6 },
2671 { "fp7", FP7 },
2672 { "fpi", FPI },
2673 { "fpiar", FPI },
2674 { "fpc", FPI },
2675 { "fps", FPS },
2676 { "fpsr", FPS },
2677 { "fpc", FPC },
2678 { "fpcr", FPC },
2679 { "control", FPC },
2680 { "status", FPS },
2681 { "iaddr", FPI },
2682
2683 { "cop0", COP0 },
2684 { "cop1", COP1 },
2685 { "cop2", COP2 },
2686 { "cop3", COP3 },
2687 { "cop4", COP4 },
2688 { "cop5", COP5 },
2689 { "cop6", COP6 },
2690 { "cop7", COP7 },
2691 { "pc", PC },
2692 { "zpc", ZPC },
2693 { "sr", SR },
2694
2695 { "ccr", CCR },
2696 { "cc", CCR },
2697
2698 { "usp", USP },
2699 { "isp", ISP },
2700 { "sfc", SFC },
2701 { "sfcr", SFC },
2702 { "dfc", DFC },
2703 { "dfcr", DFC },
2704 { "cacr", CACR },
2705 { "caar", CAAR },
2706
2707 { "vbr", VBR },
2708
2709 { "msp", MSP },
2710 { "itt0", ITT0 },
2711 { "itt1", ITT1 },
2712 { "dtt0", DTT0 },
2713 { "dtt1", DTT1 },
2714 { "mmusr", MMUSR },
2715 { "tc", TC },
2716 { "srp", SRP },
2717 { "urp", URP },
2718 { "buscr", BUSCR },
2719 { "pcr", PCR },
2720
2721 { "ac", AC },
2722 { "bc", BC },
2723 { "cal", CAL },
2724 { "crp", CRP },
2725 { "drp", DRP },
2726 { "pcsr", PCSR },
2727 { "psr", PSR },
2728 { "scc", SCC },
2729 { "val", VAL },
2730 { "bad0", BAD0 },
2731 { "bad1", BAD1 },
2732 { "bad2", BAD2 },
2733 { "bad3", BAD3 },
2734 { "bad4", BAD4 },
2735 { "bad5", BAD5 },
2736 { "bad6", BAD6 },
2737 { "bad7", BAD7 },
2738 { "bac0", BAC0 },
2739 { "bac1", BAC1 },
2740 { "bac2", BAC2 },
2741 { "bac3", BAC3 },
2742 { "bac4", BAC4 },
2743 { "bac5", BAC5 },
2744 { "bac6", BAC6 },
2745 { "bac7", BAC7 },
2746
2747 { "ic", IC },
2748 { "dc", DC },
2749 { "nc", NC },
2750
2751 { "tt0", TT0 },
2752 { "tt1", TT1 },
2753 /* 68ec030 versions of same */
2754 { "ac0", TT0 },
2755 { "ac1", TT1 },
2756 /* 68ec030 access control unit, identical to 030 MMU status reg */
2757 { "acusr", PSR },
2758
2759 /* Suppressed data and address registers. */
2760 { "zd0", ZDATA0 },
2761 { "zd1", ZDATA1 },
2762 { "zd2", ZDATA2 },
2763 { "zd3", ZDATA3 },
2764 { "zd4", ZDATA4 },
2765 { "zd5", ZDATA5 },
2766 { "zd6", ZDATA6 },
2767 { "zd7", ZDATA7 },
2768 { "za0", ZADDR0 },
2769 { "za1", ZADDR1 },
2770 { "za2", ZADDR2 },
2771 { "za3", ZADDR3 },
2772 { "za4", ZADDR4 },
2773 { "za5", ZADDR5 },
2774 { "za6", ZADDR6 },
2775 { "za7", ZADDR7 },
2776
2777 { 0, 0 }
2778 };
2779
2780 void
2781 init_regtable ()
2782 {
2783 int i;
2784 for (i = 0; init_table[i].name; i++)
2785 insert_reg (init_table[i].name, init_table[i].number);
2786 }
2787
2788 static int no_68851, no_68881;
2789
2790 #ifdef OBJ_AOUT
2791 /* a.out machine type. Default to 68020. */
2792 int m68k_aout_machtype = 2;
2793 #endif
2794
2795 void
2796 md_assemble (str)
2797 char *str;
2798 {
2799 const char *er;
2800 short *fromP;
2801 char *toP = NULL;
2802 int m, n = 0;
2803 char *to_beg_P;
2804 int shorts_this_frag;
2805 fixS *fixP;
2806
2807 /* In MRI mode, the instruction and operands are separated by a
2808 space. Anything following the operands is a comment. The label
2809 has already been removed. */
2810 if (flag_mri)
2811 {
2812 char *s;
2813 int fields = 0;
2814 int infield = 0;
2815 int inquote = 0;
2816
2817 for (s = str; *s != '\0'; s++)
2818 {
2819 if ((*s == ' ' || *s == '\t') && ! inquote)
2820 {
2821 if (infield)
2822 {
2823 ++fields;
2824 if (fields >= 2)
2825 {
2826 *s = '\0';
2827 break;
2828 }
2829 infield = 0;
2830 }
2831 }
2832 else
2833 {
2834 if (! infield)
2835 infield = 1;
2836 if (*s == '\'')
2837 inquote = ! inquote;
2838 }
2839 }
2840 }
2841
2842 memset ((char *) (&the_ins), '\0', sizeof (the_ins));
2843 m68k_ip (str);
2844 er = the_ins.error;
2845 if (!er)
2846 {
2847 for (n = 0; n < the_ins.numargs; n++)
2848 if (the_ins.operands[n].error)
2849 {
2850 er = the_ins.operands[n].error;
2851 break;
2852 }
2853 }
2854 if (er)
2855 {
2856 as_bad ("%s -- statement `%s' ignored", er, str);
2857 return;
2858 }
2859
2860 if (the_ins.nfrag == 0)
2861 {
2862 /* No frag hacking involved; just put it out */
2863 toP = frag_more (2 * the_ins.numo);
2864 fromP = &the_ins.opcode[0];
2865 for (m = the_ins.numo; m; --m)
2866 {
2867 md_number_to_chars (toP, (long) (*fromP), 2);
2868 toP += 2;
2869 fromP++;
2870 }
2871 /* put out symbol-dependent info */
2872 for (m = 0; m < the_ins.nrel; m++)
2873 {
2874 switch (the_ins.reloc[m].wid)
2875 {
2876 case 'B':
2877 n = 1;
2878 break;
2879 case 'b':
2880 n = 1;
2881 break;
2882 case '3':
2883 n = 2;
2884 break;
2885 case 'w':
2886 n = 2;
2887 break;
2888 case 'l':
2889 n = 4;
2890 break;
2891 default:
2892 as_fatal ("Don't know how to figure width of %c in md_assemble()",
2893 the_ins.reloc[m].wid);
2894 }
2895
2896 fixP = fix_new_exp (frag_now,
2897 ((toP - frag_now->fr_literal)
2898 - the_ins.numo * 2 + the_ins.reloc[m].n),
2899 n,
2900 &the_ins.reloc[m].exp,
2901 the_ins.reloc[m].pcrel,
2902 NO_RELOC);
2903 fixP->fx_pcrel_adjust = the_ins.reloc[m].pcrel_fix;
2904 }
2905 return;
2906 }
2907
2908 /* There's some frag hacking */
2909 for (n = 0, fromP = &the_ins.opcode[0]; n < the_ins.nfrag; n++)
2910 {
2911 int wid;
2912
2913 if (n == 0)
2914 wid = 2 * the_ins.fragb[n].fragoff;
2915 else
2916 wid = 2 * (the_ins.numo - the_ins.fragb[n - 1].fragoff);
2917 toP = frag_more (wid);
2918 to_beg_P = toP;
2919 shorts_this_frag = 0;
2920 for (m = wid / 2; m; --m)
2921 {
2922 md_number_to_chars (toP, (long) (*fromP), 2);
2923 toP += 2;
2924 fromP++;
2925 shorts_this_frag++;
2926 }
2927 for (m = 0; m < the_ins.nrel; m++)
2928 {
2929 if ((the_ins.reloc[m].n) >= 2 * shorts_this_frag)
2930 {
2931 the_ins.reloc[m].n -= 2 * shorts_this_frag;
2932 break;
2933 }
2934 wid = the_ins.reloc[m].wid;
2935 if (wid == 0)
2936 continue;
2937 the_ins.reloc[m].wid = 0;
2938 wid = (wid == 'b') ? 1 : (wid == 'w') ? 2 : (wid == 'l') ? 4 : 4000;
2939
2940 fixP = fix_new_exp (frag_now,
2941 ((toP - frag_now->fr_literal)
2942 - the_ins.numo * 2 + the_ins.reloc[m].n),
2943 wid,
2944 &the_ins.reloc[m].exp,
2945 the_ins.reloc[m].pcrel,
2946 NO_RELOC);
2947 fixP->fx_pcrel_adjust = the_ins.reloc[m].pcrel_fix;
2948 }
2949 (void) frag_var (rs_machine_dependent, 10, 0,
2950 (relax_substateT) (the_ins.fragb[n].fragty),
2951 the_ins.fragb[n].fadd, the_ins.fragb[n].foff, to_beg_P);
2952 }
2953 n = (the_ins.numo - the_ins.fragb[n - 1].fragoff);
2954 shorts_this_frag = 0;
2955 if (n)
2956 {
2957 toP = frag_more (n * sizeof (short));
2958 while (n--)
2959 {
2960 md_number_to_chars (toP, (long) (*fromP), 2);
2961 toP += 2;
2962 fromP++;
2963 shorts_this_frag++;
2964 }
2965 }
2966 for (m = 0; m < the_ins.nrel; m++)
2967 {
2968 int wid;
2969
2970 wid = the_ins.reloc[m].wid;
2971 if (wid == 0)
2972 continue;
2973 the_ins.reloc[m].wid = 0;
2974 wid = (wid == 'b') ? 1 : (wid == 'w') ? 2 : (wid == 'l') ? 4 : 4000;
2975
2976 fixP = fix_new_exp (frag_now,
2977 ((the_ins.reloc[m].n + toP - frag_now->fr_literal)
2978 - shorts_this_frag * 2),
2979 wid,
2980 &the_ins.reloc[m].exp,
2981 the_ins.reloc[m].pcrel,
2982 NO_RELOC);
2983 fixP->fx_pcrel_adjust = the_ins.reloc[m].pcrel_fix;
2984 }
2985 }
2986
2987 void
2988 md_begin ()
2989 {
2990 /*
2991 * md_begin -- set up hash tables with 68000 instructions.
2992 * similar to what the vax assembler does. ---phr
2993 */
2994 /* RMS claims the thing to do is take the m68k-opcode.h table, and make
2995 a copy of it at runtime, adding in the information we want but isn't
2996 there. I think it'd be better to have an awk script hack the table
2997 at compile time. Or even just xstr the table and use it as-is. But
2998 my lord ghod hath spoken, so we do it this way. Excuse the ugly var
2999 names. */
3000
3001 register const struct m68k_opcode *ins;
3002 register struct m68k_incant *hack, *slak;
3003 register const char *retval = 0; /* empty string, or error msg text */
3004 register unsigned int i;
3005 register char c;
3006
3007 if (flag_mri)
3008 {
3009 flag_reg_prefix_optional = 1;
3010 m68k_abspcadd = 1;
3011 m68k_rel32 = 0;
3012 }
3013
3014 op_hash = hash_new ();
3015
3016 obstack_begin (&robyn, 4000);
3017 for (i = 0; i < m68k_numopcodes; i++)
3018 {
3019 hack = slak = (struct m68k_incant *) obstack_alloc (&robyn, sizeof (struct m68k_incant));
3020 do
3021 {
3022 ins = &m68k_opcodes[i];
3023 /* We *could* ignore insns that don't match our arch here
3024 but just leaving them out of the hash. */
3025 slak->m_operands = ins->args;
3026 slak->m_opnum = strlen (slak->m_operands) / 2;
3027 slak->m_arch = ins->arch;
3028 slak->m_opcode = ins->opcode;
3029 /* This is kludgey */
3030 slak->m_codenum = ((ins->match) & 0xffffL) ? 2 : 1;
3031 if (i + 1 != m68k_numopcodes
3032 && !strcmp (ins->name, m68k_opcodes[i + 1].name))
3033 {
3034 slak->m_next = (struct m68k_incant *) obstack_alloc (&robyn, sizeof (struct m68k_incant));
3035 i++;
3036 }
3037 else
3038 slak->m_next = 0;
3039 slak = slak->m_next;
3040 }
3041 while (slak);
3042
3043 retval = hash_insert (op_hash, ins->name, (char *) hack);
3044 if (retval)
3045 as_fatal ("Internal Error: Can't hash %s: %s", ins->name, retval);
3046 }
3047
3048 for (i = 0; i < m68k_numaliases; i++)
3049 {
3050 const char *name = m68k_opcode_aliases[i].primary;
3051 const char *alias = m68k_opcode_aliases[i].alias;
3052 PTR val = hash_find (op_hash, name);
3053 if (!val)
3054 as_fatal ("Internal Error: Can't find %s in hash table", name);
3055 retval = hash_insert (op_hash, alias, val);
3056 if (retval)
3057 as_fatal ("Internal Error: Can't hash %s: %s", alias, retval);
3058 }
3059
3060 /* In MRI mode, all unsized branches are variable sized. Normally,
3061 they are word sized. */
3062 if (flag_mri)
3063 {
3064 static struct m68k_opcode_alias mri_aliases[] =
3065 {
3066 { "bhi", "jhi", },
3067 { "bls", "jls", },
3068 { "bcc", "jcc", },
3069 { "bcs", "jcs", },
3070 { "bne", "jne", },
3071 { "beq", "jeq", },
3072 { "bvc", "jvc", },
3073 { "bvs", "jvs", },
3074 { "bpl", "jpl", },
3075 { "bmi", "jmi", },
3076 { "bge", "jge", },
3077 { "blt", "jlt", },
3078 { "bgt", "jgt", },
3079 { "ble", "jle", },
3080 { "bra", "jra", },
3081 { "bsr", "jbsr", },
3082 };
3083
3084 for (i = 0; i < sizeof mri_aliases / sizeof mri_aliases[0]; i++)
3085 {
3086 const char *name = mri_aliases[i].primary;
3087 const char *alias = mri_aliases[i].alias;
3088 PTR val = hash_find (op_hash, name);
3089 if (!val)
3090 as_fatal ("Internal Error: Can't find %s in hash table", name);
3091 retval = hash_jam (op_hash, alias, val);
3092 if (retval)
3093 as_fatal ("Internal Error: Can't hash %s: %s", alias, retval);
3094 }
3095 }
3096
3097 for (i = 0; i < sizeof (mklower_table); i++)
3098 mklower_table[i] = (isupper (c = (char) i)) ? tolower (c) : c;
3099
3100 for (i = 0; i < sizeof (notend_table); i++)
3101 {
3102 notend_table[i] = 0;
3103 alt_notend_table[i] = 0;
3104 }
3105 notend_table[','] = 1;
3106 notend_table['{'] = 1;
3107 notend_table['}'] = 1;
3108 alt_notend_table['a'] = 1;
3109 alt_notend_table['A'] = 1;
3110 alt_notend_table['d'] = 1;
3111 alt_notend_table['D'] = 1;
3112 alt_notend_table['#'] = 1;
3113 alt_notend_table['&'] = 1;
3114 alt_notend_table['f'] = 1;
3115 alt_notend_table['F'] = 1;
3116 #ifdef REGISTER_PREFIX
3117 alt_notend_table[REGISTER_PREFIX] = 1;
3118 #endif
3119
3120 /* We need to put '(' in alt_notend_table to handle
3121 cas2 %d0:%d2,%d3:%d4,(%a0):(%a1)
3122 */
3123 alt_notend_table['('] = 1;
3124
3125 /* We need to put '@' in alt_notend_table to handle
3126 cas2 %d0:%d2,%d3:%d4,@(%d0):@(%d1)
3127 */
3128 alt_notend_table['@'] = 1;
3129
3130 #ifndef MIT_SYNTAX_ONLY
3131 /* Insert pseudo ops, these have to go into the opcode table since
3132 gas expects pseudo ops to start with a dot */
3133 {
3134 int n = 0;
3135 while (mote_pseudo_table[n].poc_name)
3136 {
3137 hack = (struct m68k_incant *)
3138 obstack_alloc (&robyn, sizeof (struct m68k_incant));
3139 hash_insert (op_hash,
3140 mote_pseudo_table[n].poc_name, (char *) hack);
3141 hack->m_operands = 0;
3142 hack->m_opnum = n;
3143 n++;
3144 }
3145 }
3146 #endif
3147
3148 init_regtable ();
3149 }
3150
3151 void
3152 m68k_init_after_args ()
3153 {
3154 if (cpu_of_arch (current_architecture) == 0)
3155 {
3156 int i;
3157 const char *default_cpu = TARGET_CPU;
3158
3159 if (*default_cpu == 'm')
3160 default_cpu++;
3161 for (i = 0; i < n_archs; i++)
3162 if (strcasecmp (default_cpu, archs[i].name) == 0)
3163 break;
3164 if (i == n_archs)
3165 {
3166 as_bad ("unrecognized default cpu `%s' ???", TARGET_CPU);
3167 current_architecture |= m68020;
3168 }
3169 else
3170 current_architecture |= archs[i].arch;
3171 }
3172 /* Permit m68881 specification with all cpus; those that can't work
3173 with a coprocessor could be doing emulation. */
3174 if (current_architecture & m68851)
3175 {
3176 if (current_architecture & m68040)
3177 {
3178 as_warn ("68040 and 68851 specified; mmu instructions may assemble incorrectly");
3179 }
3180 }
3181 /* What other incompatibilities could we check for? */
3182
3183 /* Toss in some default assumptions about coprocessors. */
3184 if (!no_68881
3185 && (cpu_of_arch (current_architecture)
3186 /* Can CPU32 have a 68881 coprocessor?? */
3187 & (m68020 | m68030 | cpu32)))
3188 {
3189 current_architecture |= m68881;
3190 }
3191 if (!no_68851
3192 && (cpu_of_arch (current_architecture) & m68020up) != 0
3193 && (cpu_of_arch (current_architecture) & m68040up) == 0)
3194 {
3195 current_architecture |= m68851;
3196 }
3197 if (no_68881 && (current_architecture & m68881))
3198 as_bad ("options for 68881 and no-68881 both given");
3199 if (no_68851 && (current_architecture & m68851))
3200 as_bad ("options for 68851 and no-68851 both given");
3201
3202 #ifdef OBJ_AOUT
3203 /* Work out the magic number. This isn't very general. */
3204 if (current_architecture & m68000)
3205 m68k_aout_machtype = 0;
3206 else if (current_architecture & m68010)
3207 m68k_aout_machtype = 1;
3208 else if (current_architecture & m68020)
3209 m68k_aout_machtype = 2;
3210 else
3211 m68k_aout_machtype = 2;
3212 #endif
3213
3214 /* Note which set of "movec" control registers is available. */
3215 switch (cpu_of_arch (current_architecture))
3216 {
3217 case m68000:
3218 control_regs = m68000_control_regs;
3219 break;
3220 case m68010:
3221 control_regs = m68010_control_regs;
3222 break;
3223 case m68020:
3224 case m68030:
3225 control_regs = m68020_control_regs;
3226 break;
3227 case m68040:
3228 control_regs = m68040_control_regs;
3229 break;
3230 case m68060:
3231 control_regs = m68060_control_regs;
3232 break;
3233 case cpu32:
3234 control_regs = cpu32_control_regs;
3235 break;
3236 default:
3237 abort ();
3238 }
3239
3240 if (cpu_of_arch (current_architecture) < m68020)
3241 md_relax_table[TAB (PCINDEX, BYTE)].rlx_more = 0;
3242 }
3243
3244 /* Equal to MAX_PRECISION in atof-ieee.c */
3245 #define MAX_LITTLENUMS 6
3246
3247 /* Turn a string in input_line_pointer into a floating point constant
3248 of type type, and store the appropriate bytes in *litP. The number
3249 of LITTLENUMS emitted is stored in *sizeP . An error message is
3250 returned, or NULL on OK. */
3251
3252 char *
3253 md_atof (type, litP, sizeP)
3254 char type;
3255 char *litP;
3256 int *sizeP;
3257 {
3258 int prec;
3259 LITTLENUM_TYPE words[MAX_LITTLENUMS];
3260 LITTLENUM_TYPE *wordP;
3261 char *t;
3262 char *atof_ieee ();
3263
3264 switch (type)
3265 {
3266 case 'f':
3267 case 'F':
3268 case 's':
3269 case 'S':
3270 prec = 2;
3271 break;
3272
3273 case 'd':
3274 case 'D':
3275 case 'r':
3276 case 'R':
3277 prec = 4;
3278 break;
3279
3280 case 'x':
3281 case 'X':
3282 prec = 6;
3283 break;
3284
3285 case 'p':
3286 case 'P':
3287 prec = 6;
3288 break;
3289
3290 default:
3291 *sizeP = 0;
3292 return "Bad call to MD_ATOF()";
3293 }
3294 t = atof_ieee (input_line_pointer, type, words);
3295 if (t)
3296 input_line_pointer = t;
3297
3298 *sizeP = prec * sizeof (LITTLENUM_TYPE);
3299 for (wordP = words; prec--;)
3300 {
3301 md_number_to_chars (litP, (long) (*wordP++), sizeof (LITTLENUM_TYPE));
3302 litP += sizeof (LITTLENUM_TYPE);
3303 }
3304 return 0;
3305 }
3306
3307 void
3308 md_number_to_chars (buf, val, n)
3309 char *buf;
3310 valueT val;
3311 int n;
3312 {
3313 number_to_chars_bigendian (buf, val, n);
3314 }
3315
3316 static void
3317 md_apply_fix_2 (fixP, val)
3318 fixS *fixP;
3319 offsetT val;
3320 {
3321 addressT upper_limit;
3322 offsetT lower_limit;
3323
3324 /* This is unnecessary but it convinces the native rs6000 compiler
3325 to generate the code we want. */
3326 char *buf = fixP->fx_frag->fr_literal;
3327 buf += fixP->fx_where;
3328 /* end ibm compiler workaround */
3329
3330 if (val & 0x80000000)
3331 val |= ~(addressT)0x7fffffff;
3332 else
3333 val &= 0x7fffffff;
3334
3335 switch (fixP->fx_size)
3336 {
3337 /* The cast to offsetT below are necessary to make code correct for
3338 machines where ints are smaller than offsetT */
3339 case 1:
3340 *buf++ = val;
3341 upper_limit = 0x7f;
3342 lower_limit = - (offsetT) 0x80;
3343 break;
3344 case 2:
3345 *buf++ = (val >> 8);
3346 *buf++ = val;
3347 upper_limit = 0x7fff;
3348 lower_limit = - (offsetT) 0x8000;
3349 break;
3350 case 4:
3351 *buf++ = (val >> 24);
3352 *buf++ = (val >> 16);
3353 *buf++ = (val >> 8);
3354 *buf++ = val;
3355 upper_limit = 0x7fffffff;
3356 lower_limit = - (offsetT) 0x7fffffff - 1; /* avoid constant overflow */
3357 break;
3358 default:
3359 BAD_CASE (fixP->fx_size);
3360 }
3361
3362 /* Fix up a negative reloc. */
3363 if (fixP->fx_addsy == NULL && fixP->fx_subsy != NULL)
3364 {
3365 fixP->fx_addsy = fixP->fx_subsy;
3366 fixP->fx_subsy = NULL;
3367 fixP->fx_tcbit = 1;
3368 }
3369
3370 /* For non-pc-relative values, it's conceivable we might get something
3371 like "0xff" for a byte field. So extend the upper part of the range
3372 to accept such numbers. We arbitrarily disallow "-0xff" or "0xff+0xff",
3373 so that we can do any range checking at all. */
3374 if (!fixP->fx_pcrel)
3375 upper_limit = upper_limit * 2 + 1;
3376
3377 if ((addressT) val > upper_limit
3378 && (val > 0 || val < lower_limit))
3379 as_bad_where (fixP->fx_file, fixP->fx_line, "value out of range");
3380
3381 /* A one byte PC-relative reloc means a short branch. We can't use
3382 a short branch with a value of 0 or -1, because those indicate
3383 different opcodes (branches with longer offsets). */
3384 if (fixP->fx_pcrel
3385 && fixP->fx_size == 1
3386 && (fixP->fx_addsy == NULL
3387 || S_IS_DEFINED (fixP->fx_addsy))
3388 && (val == 0 || val == -1))
3389 as_bad_where (fixP->fx_file, fixP->fx_line, "invalid byte branch offset");
3390 }
3391
3392 #ifdef BFD_ASSEMBLER
3393 int
3394 md_apply_fix (fixP, valp)
3395 fixS *fixP;
3396 valueT *valp;
3397 {
3398 md_apply_fix_2 (fixP, (addressT) *valp);
3399 return 1;
3400 }
3401 #else
3402 void md_apply_fix (fixP, val)
3403 fixS *fixP;
3404 long val;
3405 {
3406 md_apply_fix_2 (fixP, (addressT) val);
3407 }
3408 #endif
3409
3410 /* *fragP has been relaxed to its final size, and now needs to have
3411 the bytes inside it modified to conform to the new size There is UGLY
3412 MAGIC here. ..
3413 */
3414 void
3415 md_convert_frag_1 (fragP)
3416 register fragS *fragP;
3417 {
3418 long disp;
3419 long ext = 0;
3420 fixS *fixP;
3421
3422 /* Address in object code of the displacement. */
3423 register int object_address = fragP->fr_fix + fragP->fr_address;
3424
3425 /* Address in gas core of the place to store the displacement. */
3426 /* This convinces the native rs6000 compiler to generate the code we
3427 want. */
3428 register char *buffer_address = fragP->fr_literal;
3429 buffer_address += fragP->fr_fix;
3430 /* end ibm compiler workaround */
3431
3432 /* The displacement of the address, from current location. */
3433 disp = fragP->fr_symbol ? S_GET_VALUE (fragP->fr_symbol) : 0;
3434 disp = (disp + fragP->fr_offset) - object_address;
3435
3436 #ifdef BFD_ASSEMBLER
3437 disp += fragP->fr_symbol->sy_frag->fr_address;
3438 #endif
3439
3440 switch (fragP->fr_subtype)
3441 {
3442 case TAB (BCC68000, BYTE):
3443 case TAB (ABRANCH, BYTE):
3444 know (issbyte (disp));
3445 if (disp == 0)
3446 as_bad ("short branch with zero offset: use :w");
3447 fragP->fr_opcode[1] = disp;
3448 ext = 0;
3449 break;
3450 case TAB (DBCC, SHORT):
3451 know (issword (disp));
3452 ext = 2;
3453 break;
3454 case TAB (BCC68000, SHORT):
3455 case TAB (ABRANCH, SHORT):
3456 know (issword (disp));
3457 fragP->fr_opcode[1] = 0x00;
3458 ext = 2;
3459 break;
3460 case TAB (ABRANCH, LONG):
3461 if (cpu_of_arch (current_architecture) < m68020)
3462 {
3463 if (fragP->fr_opcode[0] == 0x61)
3464 /* BSR */
3465 {
3466 fragP->fr_opcode[0] = 0x4E;
3467 fragP->fr_opcode[1] = (char) 0xB9; /* JBSR with ABSL LONG offset */
3468
3469 fix_new (fragP,
3470 fragP->fr_fix,
3471 4,
3472 fragP->fr_symbol,
3473 fragP->fr_offset,
3474 0,
3475 NO_RELOC);
3476
3477 fragP->fr_fix += 4;
3478 ext = 0;
3479 }
3480 /* BRA */
3481 else if (fragP->fr_opcode[0] == 0x60)
3482 {
3483 fragP->fr_opcode[0] = 0x4E;
3484 fragP->fr_opcode[1] = (char) 0xF9; /* JMP with ABSL LONG offset */
3485 fix_new (fragP, fragP->fr_fix, 4, fragP->fr_symbol,
3486 fragP->fr_offset, 0, NO_RELOC);
3487 fragP->fr_fix += 4;
3488 ext = 0;
3489 }
3490 else
3491 {
3492 as_bad ("Long branch offset not supported.");
3493 }
3494 }
3495 else
3496 {
3497 fragP->fr_opcode[1] = (char) 0xff;
3498 ext = 4;
3499 }
3500 break;
3501 case TAB (BCC68000, LONG):
3502 /* only Bcc 68000 instructions can come here */
3503 /* change bcc into b!cc/jmp absl long */
3504 fragP->fr_opcode[0] ^= 0x01; /* invert bcc */
3505 fragP->fr_opcode[1] = 0x6;/* branch offset = 6 */
3506
3507 /* JF: these used to be fr_opcode[2,3], but they may be in a
3508 different frag, in which case refering to them is a no-no.
3509 Only fr_opcode[0,1] are guaranteed to work. */
3510 *buffer_address++ = 0x4e; /* put in jmp long (0x4ef9) */
3511 *buffer_address++ = (char) 0xf9;
3512 fragP->fr_fix += 2; /* account for jmp instruction */
3513 fix_new (fragP, fragP->fr_fix, 4, fragP->fr_symbol,
3514 fragP->fr_offset, 0, NO_RELOC);
3515 fragP->fr_fix += 4;
3516 ext = 0;
3517 break;
3518 case TAB (DBCC, LONG):
3519 /* only DBcc 68000 instructions can come here */
3520 /* change dbcc into dbcc/jmp absl long */
3521 /* JF: these used to be fr_opcode[2-7], but that's wrong */
3522 *buffer_address++ = 0x00; /* branch offset = 4 */
3523 *buffer_address++ = 0x04;
3524 *buffer_address++ = 0x60; /* put in bra pc+6 */
3525 *buffer_address++ = 0x06;
3526 *buffer_address++ = 0x4e; /* put in jmp long (0x4ef9) */
3527 *buffer_address++ = (char) 0xf9;
3528
3529 fragP->fr_fix += 6; /* account for bra/jmp instructions */
3530 fix_new (fragP, fragP->fr_fix, 4, fragP->fr_symbol,
3531 fragP->fr_offset, 0, NO_RELOC);
3532 fragP->fr_fix += 4;
3533 ext = 0;
3534 break;
3535 case TAB (FBRANCH, SHORT):
3536 know ((fragP->fr_opcode[1] & 0x40) == 0);
3537 ext = 2;
3538 break;
3539 case TAB (FBRANCH, LONG):
3540 fragP->fr_opcode[1] |= 0x40; /* Turn on LONG bit */
3541 ext = 4;
3542 break;
3543 case TAB (PCREL, SHORT):
3544 ext = 2;
3545 break;
3546 case TAB (PCREL, LONG):
3547 /* The thing to do here is force it to ABSOLUTE LONG, since
3548 PCREL is really trying to shorten an ABSOLUTE address anyway */
3549 /* JF FOO This code has not been tested */
3550 fix_new (fragP, fragP->fr_fix, 4, fragP->fr_symbol, fragP->fr_offset,
3551 0, NO_RELOC);
3552 if ((fragP->fr_opcode[1] & 0x3F) != 0x3A)
3553 as_bad ("Internal error (long PC-relative operand) for insn 0x%04x at 0x%lx",
3554 (unsigned) fragP->fr_opcode[0],
3555 (unsigned long) fragP->fr_address);
3556 fragP->fr_opcode[1] &= ~0x3F;
3557 fragP->fr_opcode[1] |= 0x39; /* Mode 7.1 */
3558 fragP->fr_fix += 4;
3559 ext = 0;
3560 break;
3561 case TAB (PCLEA, SHORT):
3562 fix_new (fragP, (int) (fragP->fr_fix), 2, fragP->fr_symbol,
3563 fragP->fr_offset, 1, NO_RELOC);
3564 fragP->fr_opcode[1] &= ~0x3F;
3565 fragP->fr_opcode[1] |= 0x3A; /* 072 - mode 7.2 */
3566 ext = 2;
3567 break;
3568 case TAB (PCLEA, LONG):
3569 fixP = fix_new (fragP, (int) (fragP->fr_fix) + 2, 4, fragP->fr_symbol,
3570 fragP->fr_offset, 1, NO_RELOC);
3571 fixP->fx_pcrel_adjust = 2;
3572 /* Already set to mode 7.3; this indicates: PC indirect with
3573 suppressed index, 32-bit displacement. */
3574 *buffer_address++ = 0x01;
3575 *buffer_address++ = 0x70;
3576 fragP->fr_fix += 2;
3577 ext = 4;
3578 break;
3579
3580 case TAB (PCINDEX, BYTE):
3581 disp += 2;
3582 if (!issbyte (disp))
3583 {
3584 as_bad ("displacement doesn't fit in one byte");
3585 disp = 0;
3586 }
3587 assert (fragP->fr_fix >= 2);
3588 buffer_address[-2] &= ~1;
3589 buffer_address[-1] = disp;
3590 ext = 0;
3591 break;
3592 case TAB (PCINDEX, SHORT):
3593 disp += 2;
3594 assert (issword (disp));
3595 assert (fragP->fr_fix >= 2);
3596 buffer_address[-2] |= 0x1;
3597 buffer_address[-1] = 0x20;
3598 fixP = fix_new (fragP, (int) (fragP->fr_fix), 2, fragP->fr_symbol,
3599 fragP->fr_offset, (fragP->fr_opcode[1] & 077) == 073,
3600 NO_RELOC);
3601 fixP->fx_pcrel_adjust = 2;
3602 ext = 2;
3603 break;
3604 case TAB (PCINDEX, LONG):
3605 disp += 2;
3606 fixP = fix_new (fragP, (int) (fragP->fr_fix), 4, fragP->fr_symbol,
3607 fragP->fr_offset, (fragP->fr_opcode[1] & 077) == 073,
3608 NO_RELOC);
3609 fixP->fx_pcrel_adjust = 2;
3610 assert (fragP->fr_fix >= 2);
3611 buffer_address[-2] |= 0x1;
3612 buffer_address[-1] = 0x30;
3613 ext = 4;
3614 break;
3615 }
3616
3617 if (ext)
3618 {
3619 md_number_to_chars (buffer_address, (long) disp, (int) ext);
3620 fragP->fr_fix += ext;
3621 }
3622 }
3623
3624 #ifndef BFD_ASSEMBLER
3625
3626 void
3627 md_convert_frag (headers, sec, fragP)
3628 object_headers *headers;
3629 segT sec;
3630 fragS *fragP;
3631 {
3632 md_convert_frag_1 (fragP);
3633 }
3634
3635 #else
3636
3637 void
3638 md_convert_frag (abfd, sec, fragP)
3639 bfd *abfd;
3640 segT sec;
3641 fragS *fragP;
3642 {
3643 md_convert_frag_1 (fragP);
3644 }
3645 #endif
3646
3647 /* Force truly undefined symbols to their maximum size, and generally set up
3648 the frag list to be relaxed
3649 */
3650 int
3651 md_estimate_size_before_relax (fragP, segment)
3652 register fragS *fragP;
3653 segT segment;
3654 {
3655 int old_fix;
3656 register char *buffer_address = fragP->fr_fix + fragP->fr_literal;
3657
3658 old_fix = fragP->fr_fix;
3659
3660 /* handle SZ_UNDEF first, it can be changed to BYTE or SHORT */
3661 switch (fragP->fr_subtype)
3662 {
3663
3664 case TAB (ABRANCH, SZ_UNDEF):
3665 {
3666 if ((fragP->fr_symbol != NULL) /* Not absolute */
3667 && S_GET_SEGMENT (fragP->fr_symbol) == segment)
3668 {
3669 fragP->fr_subtype = TAB (TABTYPE (fragP->fr_subtype), BYTE);
3670 break;
3671 }
3672 else if ((fragP->fr_symbol == 0) || (cpu_of_arch (current_architecture) < m68020))
3673 {
3674 /* On 68000, or for absolute value, switch to abs long */
3675 /* FIXME, we should check abs val, pick short or long */
3676 if (fragP->fr_opcode[0] == 0x61)
3677 {
3678 fragP->fr_opcode[0] = 0x4E;
3679 fragP->fr_opcode[1] = (char) 0xB9; /* JBSR with ABSL LONG offset */
3680 fix_new (fragP, fragP->fr_fix, 4,
3681 fragP->fr_symbol, fragP->fr_offset, 0, NO_RELOC);
3682 fragP->fr_fix += 4;
3683 frag_wane (fragP);
3684 }
3685 else if (fragP->fr_opcode[0] == 0x60)
3686 {
3687 fragP->fr_opcode[0] = 0x4E;
3688 fragP->fr_opcode[1] = (char) 0xF9; /* JMP with ABSL LONG offset */
3689 fix_new (fragP, fragP->fr_fix, 4,
3690 fragP->fr_symbol, fragP->fr_offset, 0, NO_RELOC);
3691 fragP->fr_fix += 4;
3692 frag_wane (fragP);
3693 }
3694 else
3695 {
3696 as_warn ("Long branch offset to extern symbol not supported.");
3697 }
3698 }
3699 else
3700 { /* Symbol is still undefined. Make it simple */
3701 fix_new (fragP, (int) (fragP->fr_fix), 4, fragP->fr_symbol,
3702 fragP->fr_offset, 1, NO_RELOC);
3703 fragP->fr_fix += 4;
3704 fragP->fr_opcode[1] = (char) 0xff;
3705 frag_wane (fragP);
3706 break;
3707 }
3708
3709 break;
3710 } /* case TAB(ABRANCH,SZ_UNDEF) */
3711
3712 case TAB (FBRANCH, SZ_UNDEF):
3713 {
3714 if (S_GET_SEGMENT (fragP->fr_symbol) == segment || flag_short_refs)
3715 {
3716 fragP->fr_subtype = TAB (FBRANCH, SHORT);
3717 fragP->fr_var += 2;
3718 }
3719 else
3720 {
3721 fix_new (fragP, (int) fragP->fr_fix, 4, fragP->fr_symbol,
3722 fragP->fr_offset, 1, NO_RELOC);
3723 fragP->fr_fix += 4;
3724 fragP->fr_opcode[1] |= 0x40; /* Turn on LONG bit */
3725 frag_wane (fragP);
3726 }
3727 break;
3728 } /* TAB(FBRANCH,SZ_UNDEF) */
3729
3730 case TAB (PCREL, SZ_UNDEF):
3731 {
3732 if (S_GET_SEGMENT (fragP->fr_symbol) == segment
3733 || flag_short_refs
3734 || cpu_of_arch (current_architecture) < m68020)
3735 {
3736 fragP->fr_subtype = TAB (PCREL, SHORT);
3737 fragP->fr_var += 2;
3738 }
3739 else
3740 {
3741 fragP->fr_subtype = TAB (PCREL, LONG);
3742 fragP->fr_var += 4;
3743 }
3744 break;
3745 } /* TAB(PCREL,SZ_UNDEF) */
3746
3747 case TAB (BCC68000, SZ_UNDEF):
3748 {
3749 if ((fragP->fr_symbol != NULL)
3750 && S_GET_SEGMENT (fragP->fr_symbol) == segment)
3751 {
3752 fragP->fr_subtype = TAB (BCC68000, BYTE);
3753 break;
3754 }
3755 /* only Bcc 68000 instructions can come here */
3756 /* change bcc into b!cc/jmp absl long */
3757 fragP->fr_opcode[0] ^= 0x01; /* invert bcc */
3758 if (flag_short_refs)
3759 {
3760 fragP->fr_opcode[1] = 0x04; /* branch offset = 6 */
3761 /* JF: these were fr_opcode[2,3] */
3762 buffer_address[0] = 0x4e; /* put in jmp long (0x4ef9) */
3763 buffer_address[1] = (char) 0xf8;
3764 fragP->fr_fix += 2; /* account for jmp instruction */
3765 fix_new (fragP, fragP->fr_fix, 2, fragP->fr_symbol,
3766 fragP->fr_offset, 0, NO_RELOC);
3767 fragP->fr_fix += 2;
3768 }
3769 else
3770 {
3771 fragP->fr_opcode[1] = 0x06; /* branch offset = 6 */
3772 /* JF: these were fr_opcode[2,3] */
3773 buffer_address[0] = 0x4e; /* put in jmp long (0x4ef9) */
3774 buffer_address[1] = (char) 0xf9;
3775 fragP->fr_fix += 2; /* account for jmp instruction */
3776 fix_new (fragP, fragP->fr_fix, 4, fragP->fr_symbol,
3777 fragP->fr_offset, 0, NO_RELOC);
3778 fragP->fr_fix += 4;
3779 }
3780 frag_wane (fragP);
3781 break;
3782 } /* case TAB(BCC68000,SZ_UNDEF) */
3783
3784 case TAB (DBCC, SZ_UNDEF):
3785 {
3786 if (fragP->fr_symbol != NULL && S_GET_SEGMENT (fragP->fr_symbol) == segment)
3787 {
3788 fragP->fr_subtype = TAB (DBCC, SHORT);
3789 fragP->fr_var += 2;
3790 break;
3791 }
3792 /* only DBcc 68000 instructions can come here */
3793 /* change dbcc into dbcc/jmp absl long */
3794 /* JF: these used to be fr_opcode[2-4], which is wrong. */
3795 buffer_address[0] = 0x00; /* branch offset = 4 */
3796 buffer_address[1] = 0x04;
3797 buffer_address[2] = 0x60; /* put in bra pc + ... */
3798
3799 if (flag_short_refs)
3800 {
3801 /* JF: these were fr_opcode[5-7] */
3802 buffer_address[3] = 0x04; /* plus 4 */
3803 buffer_address[4] = 0x4e; /* Put in Jump Word */
3804 buffer_address[5] = (char) 0xf8;
3805 fragP->fr_fix += 6; /* account for bra/jmp instruction */
3806 fix_new (fragP, fragP->fr_fix, 2, fragP->fr_symbol,
3807 fragP->fr_offset, 0, NO_RELOC);
3808 fragP->fr_fix += 2;
3809 }
3810 else
3811 {
3812 /* JF: these were fr_opcode[5-7] */
3813 buffer_address[3] = 0x06; /* Plus 6 */
3814 buffer_address[4] = 0x4e; /* put in jmp long (0x4ef9) */
3815 buffer_address[5] = (char) 0xf9;
3816 fragP->fr_fix += 6; /* account for bra/jmp instruction */
3817 fix_new (fragP, fragP->fr_fix, 4, fragP->fr_symbol,
3818 fragP->fr_offset, 0, NO_RELOC);
3819 fragP->fr_fix += 4;
3820 }
3821
3822 frag_wane (fragP);
3823 break;
3824 } /* case TAB(DBCC,SZ_UNDEF) */
3825
3826 case TAB (PCLEA, SZ_UNDEF):
3827 {
3828 if ((S_GET_SEGMENT (fragP->fr_symbol)) == segment
3829 || flag_short_refs
3830 || cpu_of_arch (current_architecture) < m68020)
3831 {
3832 fragP->fr_subtype = TAB (PCLEA, SHORT);
3833 fragP->fr_var += 2;
3834 }
3835 else
3836 {
3837 fragP->fr_subtype = TAB (PCLEA, LONG);
3838 fragP->fr_var += 6;
3839 }
3840 break;
3841 } /* TAB(PCLEA,SZ_UNDEF) */
3842
3843 case TAB (PCINDEX, SZ_UNDEF):
3844 if (S_GET_SEGMENT (fragP->fr_symbol) == segment
3845 || cpu_of_arch (current_architecture) < m68020)
3846 {
3847 fragP->fr_subtype = TAB (PCINDEX, BYTE);
3848 }
3849 else
3850 {
3851 fragP->fr_subtype = TAB (PCINDEX, LONG);
3852 fragP->fr_var += 4;
3853 }
3854 break;
3855
3856 default:
3857 break;
3858 }
3859
3860 /* now that SZ_UNDEF are taken care of, check others */
3861 switch (fragP->fr_subtype)
3862 {
3863 case TAB (BCC68000, BYTE):
3864 case TAB (ABRANCH, BYTE):
3865 /* We can't do a short jump to the next instruction,
3866 so we force word mode. */
3867 if (fragP->fr_symbol && S_GET_VALUE (fragP->fr_symbol) == 0 &&
3868 fragP->fr_symbol->sy_frag == fragP->fr_next)
3869 {
3870 fragP->fr_subtype = TAB (TABTYPE (fragP->fr_subtype), SHORT);
3871 fragP->fr_var += 2;
3872 }
3873 break;
3874 default:
3875 break;
3876 }
3877 return fragP->fr_var + fragP->fr_fix - old_fix;
3878 }
3879
3880 #if defined(OBJ_AOUT) | defined(OBJ_BOUT)
3881 /* the bit-field entries in the relocation_info struct plays hell
3882 with the byte-order problems of cross-assembly. So as a hack,
3883 I added this mach. dependent ri twiddler. Ugly, but it gets
3884 you there. -KWK */
3885 /* on m68k: first 4 bytes are normal unsigned long, next three bytes
3886 are symbolnum, most sig. byte first. Last byte is broken up with
3887 bit 7 as pcrel, bits 6 & 5 as length, bit 4 as pcrel, and the lower
3888 nibble as nuthin. (on Sun 3 at least) */
3889 /* Translate the internal relocation information into target-specific
3890 format. */
3891 #ifdef comment
3892 void
3893 md_ri_to_chars (the_bytes, ri)
3894 char *the_bytes;
3895 struct reloc_info_generic *ri;
3896 {
3897 /* this is easy */
3898 md_number_to_chars (the_bytes, ri->r_address, 4);
3899 /* now the fun stuff */
3900 the_bytes[4] = (ri->r_symbolnum >> 16) & 0x0ff;
3901 the_bytes[5] = (ri->r_symbolnum >> 8) & 0x0ff;
3902 the_bytes[6] = ri->r_symbolnum & 0x0ff;
3903 the_bytes[7] = (((ri->r_pcrel << 7) & 0x80) | ((ri->r_length << 5) & 0x60) |
3904 ((ri->r_extern << 4) & 0x10));
3905 }
3906
3907 #endif /* comment */
3908
3909 #ifndef BFD_ASSEMBLER
3910 void
3911 tc_aout_fix_to_chars (where, fixP, segment_address_in_file)
3912 char *where;
3913 fixS *fixP;
3914 relax_addressT segment_address_in_file;
3915 {
3916 /*
3917 * In: length of relocation (or of address) in chars: 1, 2 or 4.
3918 * Out: GNU LD relocation length code: 0, 1, or 2.
3919 */
3920
3921 static CONST unsigned char nbytes_r_length[] = {42, 0, 1, 42, 2};
3922 long r_symbolnum;
3923
3924 know (fixP->fx_addsy != NULL);
3925
3926 md_number_to_chars (where,
3927 fixP->fx_frag->fr_address + fixP->fx_where - segment_address_in_file,
3928 4);
3929
3930 r_symbolnum = (S_IS_DEFINED (fixP->fx_addsy)
3931 ? S_GET_TYPE (fixP->fx_addsy)
3932 : fixP->fx_addsy->sy_number);
3933
3934 where[4] = (r_symbolnum >> 16) & 0x0ff;
3935 where[5] = (r_symbolnum >> 8) & 0x0ff;
3936 where[6] = r_symbolnum & 0x0ff;
3937 where[7] = (((fixP->fx_pcrel << 7) & 0x80) | ((nbytes_r_length[fixP->fx_size] << 5) & 0x60) |
3938 (((!S_IS_DEFINED (fixP->fx_addsy)) << 4) & 0x10));
3939 }
3940 #endif
3941
3942 #endif /* OBJ_AOUT or OBJ_BOUT */
3943
3944 #ifndef WORKING_DOT_WORD
3945 CONST int md_short_jump_size = 4;
3946 CONST int md_long_jump_size = 6;
3947
3948 void
3949 md_create_short_jump (ptr, from_addr, to_addr, frag, to_symbol)
3950 char *ptr;
3951 addressT from_addr, to_addr;
3952 fragS *frag;
3953 symbolS *to_symbol;
3954 {
3955 valueT offset;
3956
3957 offset = to_addr - (from_addr + 2);
3958
3959 md_number_to_chars (ptr, (valueT) 0x6000, 2);
3960 md_number_to_chars (ptr + 2, (valueT) offset, 2);
3961 }
3962
3963 void
3964 md_create_long_jump (ptr, from_addr, to_addr, frag, to_symbol)
3965 char *ptr;
3966 addressT from_addr, to_addr;
3967 fragS *frag;
3968 symbolS *to_symbol;
3969 {
3970 valueT offset;
3971
3972 if (cpu_of_arch (current_architecture) < m68020)
3973 {
3974 offset = to_addr - S_GET_VALUE (to_symbol);
3975 md_number_to_chars (ptr, (valueT) 0x4EF9, 2);
3976 md_number_to_chars (ptr + 2, (valueT) offset, 4);
3977 fix_new (frag, (ptr + 2) - frag->fr_literal, 4, to_symbol, (offsetT) 0,
3978 0, NO_RELOC);
3979 }
3980 else
3981 {
3982 offset = to_addr - (from_addr + 2);
3983 md_number_to_chars (ptr, (valueT) 0x60ff, 2);
3984 md_number_to_chars (ptr + 2, (valueT) offset, 4);
3985 }
3986 }
3987
3988 #endif
3989
3990 /* Different values of OK tell what its OK to return. Things that
3991 aren't OK are an error (what a shock, no?)
3992
3993 0: Everything is OK
3994 10: Absolute 1:8 only
3995 20: Absolute 0:7 only
3996 30: absolute 0:15 only
3997 40: Absolute 0:31 only
3998 50: absolute 0:127 only
3999 55: absolute -64:63 only
4000 60: absolute -128:127 only
4001 70: absolute 0:4095 only
4002 80: No bignums
4003
4004 */
4005
4006 static int
4007 get_num (exp, ok)
4008 struct m68k_exp *exp;
4009 int ok;
4010 {
4011 if (exp->exp.X_op == O_absent)
4012 {
4013 /* Do the same thing the VAX asm does */
4014 op (exp) = O_constant;
4015 adds (exp) = 0;
4016 subs (exp) = 0;
4017 offs (exp) = 0;
4018 if (ok == 10)
4019 {
4020 as_warn ("expression out of range: defaulting to 1");
4021 offs (exp) = 1;
4022 }
4023 }
4024 else if (exp->exp.X_op == O_constant)
4025 {
4026 switch (ok)
4027 {
4028 case 10:
4029 if (offs (exp) < 1 || offs (exp) > 8)
4030 {
4031 as_warn ("expression out of range: defaulting to 1");
4032 offs (exp) = 1;
4033 }
4034 break;
4035 case 20:
4036 if (offs (exp) < 0 || offs (exp) > 7)
4037 goto outrange;
4038 break;
4039 case 30:
4040 if (offs (exp) < 0 || offs (exp) > 15)
4041 goto outrange;
4042 break;
4043 case 40:
4044 if (offs (exp) < 0 || offs (exp) > 32)
4045 goto outrange;
4046 break;
4047 case 50:
4048 if (offs (exp) < 0 || offs (exp) > 127)
4049 goto outrange;
4050 break;
4051 case 55:
4052 if (offs (exp) < -64 || offs (exp) > 63)
4053 goto outrange;
4054 break;
4055 case 60:
4056 if (offs (exp) < -128 || offs (exp) > 127)
4057 goto outrange;
4058 break;
4059 case 70:
4060 if (offs (exp) < 0 || offs (exp) > 4095)
4061 {
4062 outrange:
4063 as_warn ("expression out of range: defaulting to 0");
4064 offs (exp) = 0;
4065 }
4066 break;
4067 default:
4068 break;
4069 }
4070 }
4071 else if (exp->exp.X_op == O_big)
4072 {
4073 if (offs (exp) <= 0 /* flonum */
4074 && (ok == 80 /* no bignums */
4075 || (ok > 10 /* small-int ranges including 0 ok */
4076 /* If we have a flonum zero, a zero integer should
4077 do as well (e.g., in moveq). */
4078 && generic_floating_point_number.exponent == 0
4079 && generic_floating_point_number.low[0] == 0)))
4080 {
4081 /* HACK! Turn it into a long */
4082 LITTLENUM_TYPE words[6];
4083
4084 gen_to_words (words, 2, 8L); /* These numbers are magic! */
4085 op (exp) = O_constant;
4086 adds (exp) = 0;
4087 subs (exp) = 0;
4088 offs (exp) = words[1] | (words[0] << 16);
4089 }
4090 else if (ok != 0)
4091 {
4092 op (exp) = O_constant;
4093 adds (exp) = 0;
4094 subs (exp) = 0;
4095 offs (exp) = (ok == 10) ? 1 : 0;
4096 as_warn ("Can't deal with expression; defaulting to %ld",
4097 offs (exp));
4098 }
4099 }
4100 else
4101 {
4102 if (ok >= 10 && ok <= 70)
4103 {
4104 op (exp) = O_constant;
4105 adds (exp) = 0;
4106 subs (exp) = 0;
4107 offs (exp) = (ok == 10) ? 1 : 0;
4108 as_warn ("Can't deal with expression; defaulting to %ld",
4109 offs (exp));
4110 }
4111 }
4112
4113 if (exp->size != SIZE_UNSPEC)
4114 {
4115 switch (exp->size)
4116 {
4117 case SIZE_UNSPEC:
4118 case SIZE_LONG:
4119 break;
4120 case SIZE_BYTE:
4121 if (!isbyte (offs (exp)))
4122 as_warn ("expression doesn't fit in BYTE");
4123 break;
4124 case SIZE_WORD:
4125 if (!isword (offs (exp)))
4126 as_warn ("expression doesn't fit in WORD");
4127 break;
4128 }
4129 }
4130
4131 return offs (exp);
4132 }
4133
4134 /* These are the back-ends for the various machine dependent pseudo-ops. */
4135 void demand_empty_rest_of_line (); /* Hate those extra verbose names */
4136
4137 static void
4138 s_data1 (ignore)
4139 int ignore;
4140 {
4141 subseg_set (data_section, 1);
4142 demand_empty_rest_of_line ();
4143 }
4144
4145 static void
4146 s_data2 (ignore)
4147 int ignore;
4148 {
4149 subseg_set (data_section, 2);
4150 demand_empty_rest_of_line ();
4151 }
4152
4153 static void
4154 s_bss (ignore)
4155 int ignore;
4156 {
4157 /* We don't support putting frags in the BSS segment, we fake it
4158 by marking in_bss, then looking at s_skip for clues. */
4159
4160 subseg_set (bss_section, 0);
4161 demand_empty_rest_of_line ();
4162 }
4163
4164 static void
4165 s_even (ignore)
4166 int ignore;
4167 {
4168 register int temp;
4169 register long temp_fill;
4170
4171 temp = 1; /* JF should be 2? */
4172 temp_fill = get_absolute_expression ();
4173 if (!need_pass_2) /* Never make frag if expect extra pass. */
4174 frag_align (temp, (int) temp_fill);
4175 demand_empty_rest_of_line ();
4176 }
4177
4178 static void
4179 s_proc (ignore)
4180 int ignore;
4181 {
4182 demand_empty_rest_of_line ();
4183 }
4184 \f
4185 /* Pseudo-ops handled for MRI compatibility. */
4186
4187 /* Handle an MRI style chip specification. */
4188
4189 static void
4190 mri_chip ()
4191 {
4192 char *s;
4193 char c;
4194 int i;
4195
4196 s = input_line_pointer;
4197 c = get_symbol_end ();
4198 for (i = 0; i < n_archs; i++)
4199 if (strcasecmp (s, archs[i].name) == 0)
4200 break;
4201 if (i >= n_archs)
4202 {
4203 as_bad ("%s: unrecognized processor name", s);
4204 *input_line_pointer = c;
4205 ignore_rest_of_line ();
4206 return;
4207 }
4208 *input_line_pointer = c;
4209
4210 if (*input_line_pointer == '/')
4211 current_architecture = 0;
4212 else
4213 current_architecture &= m68881 | m68851;
4214 current_architecture |= archs[i].arch;
4215
4216 while (*input_line_pointer == '/')
4217 {
4218 ++input_line_pointer;
4219 s = input_line_pointer;
4220 c = get_symbol_end ();
4221 if (strcmp (s, "68881") == 0)
4222 current_architecture |= m68881;
4223 else if (strcmp (s, "68851") == 0)
4224 current_architecture |= m68851;
4225 *input_line_pointer = c;
4226 }
4227 }
4228
4229 /* The MRI CHIP pseudo-op. */
4230
4231 static void
4232 s_chip (ignore)
4233 int ignore;
4234 {
4235 char *stop = NULL;
4236 char stopc;
4237
4238 if (flag_mri)
4239 stop = mri_comment_field (&stopc);
4240 mri_chip ();
4241 if (flag_mri)
4242 mri_comment_end (stop, stopc);
4243 demand_empty_rest_of_line ();
4244 }
4245
4246 /* The MRI FOPT pseudo-op. */
4247
4248 static void
4249 s_fopt (ignore)
4250 int ignore;
4251 {
4252 SKIP_WHITESPACE ();
4253
4254 if (strncasecmp (input_line_pointer, "ID=", 3) == 0)
4255 {
4256 int temp;
4257
4258 input_line_pointer += 3;
4259 temp = get_absolute_expression ();
4260 if (temp < 0 || temp > 7)
4261 as_bad ("bad coprocessor id");
4262 else
4263 m68k_float_copnum = COP0 + temp;
4264 }
4265 else
4266 {
4267 as_bad ("unrecognized fopt option");
4268 ignore_rest_of_line ();
4269 return;
4270 }
4271
4272 demand_empty_rest_of_line ();
4273 }
4274
4275 /* The structure used to handle the MRI OPT pseudo-op. */
4276
4277 struct opt_action
4278 {
4279 /* The name of the option. */
4280 const char *name;
4281
4282 /* If this is not NULL, just call this function. The first argument
4283 is the ARG field of this structure, the second argument is
4284 whether the option was negated. */
4285 void (*pfn) PARAMS ((int arg, int on));
4286
4287 /* If this is not NULL, and the PFN field is NULL, set the variable
4288 this points to. Set it to the ARG field if the option was not
4289 negated, and the NOTARG field otherwise. */
4290 int *pvar;
4291
4292 /* The value to pass to PFN or to assign to *PVAR. */
4293 int arg;
4294
4295 /* The value to assign to *PVAR if the option is negated. If PFN is
4296 NULL, and PVAR is not NULL, and ARG and NOTARG are the same, then
4297 the option may not be negated. */
4298 int notarg;
4299 };
4300
4301 /* The table used to handle the MRI OPT pseudo-op. */
4302
4303 static void skip_to_comma PARAMS ((int, int));
4304 static void opt_nest PARAMS ((int, int));
4305 static void opt_chip PARAMS ((int, int));
4306 static void opt_list PARAMS ((int, int));
4307 static void opt_list_symbols PARAMS ((int, int));
4308
4309 static const struct opt_action opt_table[] =
4310 {
4311 { "abspcadd", 0, &m68k_abspcadd, 1, 0 },
4312
4313 /* We do relaxing, so there is little use for these options. */
4314 { "b", 0, 0, 0, 0 },
4315 { "brs", 0, 0, 0, 0 },
4316 { "brb", 0, 0, 0, 0 },
4317 { "brl", 0, 0, 0, 0 },
4318 { "brw", 0, 0, 0, 0 },
4319
4320 { "c", 0, 0, 0, 0 },
4321 { "cex", 0, 0, 0, 0 },
4322 { "case", 0, &symbols_case_sensitive, 1, 0 },
4323 { "cl", 0, 0, 0, 0 },
4324 { "cre", 0, 0, 0, 0 },
4325 { "d", 0, &flag_keep_locals, 1, 0 },
4326 { "e", 0, 0, 0, 0 },
4327 { "f", 0, &flag_short_refs, 1, 0 },
4328 { "frs", 0, &flag_short_refs, 1, 0 },
4329 { "frl", 0, &flag_short_refs, 0, 1 },
4330 { "g", 0, 0, 0, 0 },
4331 { "i", 0, 0, 0, 0 },
4332 { "m", 0, 0, 0, 0 },
4333 { "mex", 0, 0, 0, 0 },
4334 { "mc", 0, 0, 0, 0 },
4335 { "md", 0, 0, 0, 0 },
4336 { "nest", opt_nest, 0, 0, 0 },
4337 { "next", skip_to_comma, 0, 0, 0 },
4338 { "o", 0, 0, 0, 0 },
4339 { "old", 0, 0, 0, 0 },
4340 { "op", skip_to_comma, 0, 0, 0 },
4341 { "pco", 0, 0, 0, 0 },
4342 { "p", opt_chip, 0, 0, 0 },
4343 { "pcr", 0, 0, 0, 0 },
4344 { "pcs", 0, 0, 0, 0 },
4345 { "r", 0, 0, 0, 0 },
4346 { "quick", 0, &m68k_quick, 1, 0 },
4347 { "rel32", 0, &m68k_rel32, 1, 0 },
4348 { "s", opt_list, 0, 0, 0 },
4349 { "t", opt_list_symbols, 0, 0, 0 },
4350 { "w", 0, &flag_no_warnings, 0, 1 },
4351 { "x", 0, 0, 0, 0 }
4352 };
4353
4354 #define OPTCOUNT (sizeof opt_table / sizeof opt_table[0])
4355
4356 /* The MRI OPT pseudo-op. */
4357
4358 static void
4359 s_opt (ignore)
4360 int ignore;
4361 {
4362 do
4363 {
4364 int t;
4365 char *s;
4366 char c;
4367 int i;
4368 const struct opt_action *o;
4369
4370 SKIP_WHITESPACE ();
4371
4372 t = 1;
4373 if (*input_line_pointer == '-')
4374 {
4375 ++input_line_pointer;
4376 t = 0;
4377 }
4378 else if (strncasecmp (input_line_pointer, "NO", 2) == 0)
4379 {
4380 input_line_pointer += 2;
4381 t = 0;
4382 }
4383
4384 s = input_line_pointer;
4385 c = get_symbol_end ();
4386
4387 for (i = 0, o = opt_table; i < OPTCOUNT; i++, o++)
4388 {
4389 if (strcasecmp (s, o->name) == 0)
4390 {
4391 if (o->pfn)
4392 {
4393 /* Restore input_line_pointer now in case the option
4394 takes arguments. */
4395 *input_line_pointer = c;
4396 (*o->pfn) (o->arg, t);
4397 }
4398 else if (o->pvar != NULL)
4399 {
4400 if (! t && o->arg == o->notarg)
4401 as_bad ("option `%s' may not be negated", s);
4402 *input_line_pointer = c;
4403 *o->pvar = t ? o->arg : o->notarg;
4404 }
4405 else
4406 *input_line_pointer = c;
4407 break;
4408 }
4409 }
4410 if (i >= OPTCOUNT)
4411 {
4412 as_bad ("option `%s' not recognized", s);
4413 *input_line_pointer = c;
4414 }
4415 }
4416 while (*input_line_pointer++ == ',');
4417
4418 /* Move back to terminating character. */
4419 --input_line_pointer;
4420 demand_empty_rest_of_line ();
4421 }
4422
4423 /* Skip ahead to a comma. This is used for OPT options which we do
4424 not suppor tand which take arguments. */
4425
4426 static void
4427 skip_to_comma (arg, on)
4428 int arg;
4429 int on;
4430 {
4431 while (*input_line_pointer != ','
4432 && ! is_end_of_line[(unsigned char) *input_line_pointer])
4433 ++input_line_pointer;
4434 }
4435
4436 /* Handle the OPT NEST=depth option. */
4437
4438 static void
4439 opt_nest (arg, on)
4440 int arg;
4441 int on;
4442 {
4443 if (*input_line_pointer != '=')
4444 {
4445 as_bad ("bad format of OPT NEST=depth");
4446 return;
4447 }
4448
4449 ++input_line_pointer;
4450 max_macro_nest = get_absolute_expression ();
4451 }
4452
4453 /* Handle the OPT P=chip option. */
4454
4455 static void
4456 opt_chip (arg, on)
4457 int arg;
4458 int on;
4459 {
4460 if (*input_line_pointer != '=')
4461 {
4462 /* This is just OPT P, which we do not support. */
4463 return;
4464 }
4465
4466 ++input_line_pointer;
4467 mri_chip ();
4468 }
4469
4470 /* Handle the OPT S option. */
4471
4472 static void
4473 opt_list (arg, on)
4474 int arg;
4475 int on;
4476 {
4477 listing_list (on);
4478 }
4479
4480 /* Handle the OPT T option. */
4481
4482 static void
4483 opt_list_symbols (arg, on)
4484 int arg;
4485 int on;
4486 {
4487 if (on)
4488 listing |= LISTING_SYMBOLS;
4489 else
4490 listing &=~ LISTING_SYMBOLS;
4491 }
4492
4493 /* Handle the MRI REG pseudo-op. */
4494
4495 static void
4496 s_reg (ignore)
4497 int ignore;
4498 {
4499 char *s;
4500 int c;
4501 struct m68k_op rop;
4502 unsigned long mask;
4503 char *stop = NULL;
4504 char stopc;
4505
4506 if (line_label == NULL)
4507 {
4508 as_bad ("missing label");
4509 ignore_rest_of_line ();
4510 return;
4511 }
4512
4513 if (flag_mri)
4514 stop = mri_comment_field (&stopc);
4515
4516 SKIP_WHITESPACE ();
4517
4518 s = input_line_pointer;
4519 while (isalnum ((unsigned char) *input_line_pointer)
4520 #ifdef REGISTER_PREFIX
4521 || *input_line_pointer == REGISTER_PREFIX
4522 #endif
4523 || *input_line_pointer == '/'
4524 || *input_line_pointer == '-')
4525 ++input_line_pointer;
4526 c = *input_line_pointer;
4527 *input_line_pointer = '\0';
4528
4529 if (m68k_ip_op (s, &rop) != 0)
4530 {
4531 if (rop.error == NULL)
4532 as_bad ("bad register list");
4533 else
4534 as_bad ("bad register list: %s", rop.error);
4535 *input_line_pointer = c;
4536 ignore_rest_of_line ();
4537 return;
4538 }
4539
4540 *input_line_pointer = c;
4541
4542 if (rop.mode == REGLST)
4543 mask = rop.mask;
4544 else if (rop.mode == DREG)
4545 mask = 1 << (rop.reg - DATA0);
4546 else if (rop.mode == AREG)
4547 mask = 1 << (rop.reg - ADDR0 + 8);
4548 else if (rop.mode == FPREG)
4549 mask = 1 << (rop.reg - FP0 + 16);
4550 else if (rop.mode == CONTROL
4551 && rop.reg == FPI)
4552 mask = 1 << 24;
4553 else if (rop.mode == CONTROL
4554 && rop.reg == FPS)
4555 mask = 1 << 25;
4556 else if (rop.mode == CONTROL
4557 && rop.reg == FPC)
4558 mask = 1 << 26;
4559 else
4560 {
4561 as_bad ("bad register list");
4562 ignore_rest_of_line ();
4563 return;
4564 }
4565
4566 S_SET_SEGMENT (line_label, absolute_section);
4567 S_SET_VALUE (line_label, mask);
4568 line_label->sy_frag = &zero_address_frag;
4569
4570 if (flag_mri)
4571 mri_comment_end (stop, stopc);
4572
4573 demand_empty_rest_of_line ();
4574 }
4575
4576 /* This structure is used for the MRI SAVE and RESTORE pseudo-ops. */
4577
4578 struct save_opts
4579 {
4580 struct save_opts *next;
4581 int abspcadd;
4582 int symbols_case_sensitive;
4583 int keep_locals;
4584 int short_refs;
4585 int architecture;
4586 int quick;
4587 int rel32;
4588 int listing;
4589 int no_warnings;
4590 /* FIXME: We don't save OPT S. */
4591 };
4592
4593 /* This variable holds the stack of saved options. */
4594
4595 static struct save_opts *save_stack;
4596
4597 /* The MRI SAVE pseudo-op. */
4598
4599 static void
4600 s_save (ignore)
4601 int ignore;
4602 {
4603 struct save_opts *s;
4604
4605 s = (struct save_opts *) xmalloc (sizeof (struct save_opts));
4606 s->abspcadd = m68k_abspcadd;
4607 s->symbols_case_sensitive = symbols_case_sensitive;
4608 s->keep_locals = flag_keep_locals;
4609 s->short_refs = flag_short_refs;
4610 s->architecture = current_architecture;
4611 s->quick = m68k_quick;
4612 s->rel32 = m68k_rel32;
4613 s->listing = listing;
4614 s->no_warnings = flag_no_warnings;
4615
4616 s->next = save_stack;
4617 save_stack = s;
4618
4619 demand_empty_rest_of_line ();
4620 }
4621
4622 /* The MRI RESTORE pseudo-op. */
4623
4624 static void
4625 s_restore (ignore)
4626 int ignore;
4627 {
4628 struct save_opts *s;
4629
4630 if (save_stack == NULL)
4631 {
4632 as_bad ("restore without save");
4633 ignore_rest_of_line ();
4634 return;
4635 }
4636
4637 s = save_stack;
4638 save_stack = s->next;
4639
4640 m68k_abspcadd = s->abspcadd;
4641 symbols_case_sensitive = s->symbols_case_sensitive;
4642 flag_keep_locals = s->keep_locals;
4643 flag_short_refs = s->short_refs;
4644 current_architecture = s->architecture;
4645 m68k_quick = s->quick;
4646 m68k_rel32 = s->rel32;
4647 listing = s->listing;
4648 flag_no_warnings = s->no_warnings;
4649
4650 free (s);
4651
4652 demand_empty_rest_of_line ();
4653 }
4654
4655 /* Types of MRI structured control directives. */
4656
4657 enum mri_control_type
4658 {
4659 mri_for,
4660 mri_if,
4661 mri_repeat,
4662 mri_while
4663 };
4664
4665 /* This structure is used to stack the MRI structured control
4666 directives. */
4667
4668 struct mri_control_info
4669 {
4670 /* The directive within which this one is enclosed. */
4671 struct mri_control_info *outer;
4672
4673 /* The type of directive. */
4674 enum mri_control_type type;
4675
4676 /* Whether an ELSE has been in an IF. */
4677 int else_seen;
4678
4679 /* The add or sub statement at the end of a FOR. */
4680 char *incr;
4681
4682 /* The label of the top of a FOR or REPEAT loop. */
4683 char *top;
4684
4685 /* The label to jump to for the next iteration, or the else
4686 expression of a conditional. */
4687 char *next;
4688
4689 /* The label to jump to to break out of the loop, or the label past
4690 the end of a conditional. */
4691 char *bottom;
4692 };
4693
4694 /* The stack of MRI structured control directives. */
4695
4696 static struct mri_control_info *mri_control_stack;
4697
4698 /* The current MRI structured control directive index number, used to
4699 generate label names. */
4700
4701 static int mri_control_index;
4702
4703 /* Some function prototypes. */
4704
4705 static char *mri_control_label PARAMS ((void));
4706 static struct mri_control_info *push_mri_control
4707 PARAMS ((enum mri_control_type));
4708 static void pop_mri_control PARAMS ((void));
4709 static int parse_mri_condition PARAMS ((int *));
4710 static int parse_mri_control_operand
4711 PARAMS ((int *, const char **, const char **, const char **, const char **));
4712 static int swap_mri_condition PARAMS ((int));
4713 static int reverse_mri_condition PARAMS ((int));
4714 static void build_mri_control_operand
4715 PARAMS ((int, int, const char *, const char *, const char *, const char *,
4716 const char *, const char *, int));
4717 static void parse_mri_control_expression
4718 PARAMS ((char *, int, const char *, const char *, int));
4719
4720 /* Generate a new MRI label structured control directive label name. */
4721
4722 static char *
4723 mri_control_label ()
4724 {
4725 char *n;
4726
4727 n = (char *) xmalloc (20);
4728 sprintf (n, "%smc%d", FAKE_LABEL_NAME, mri_control_index);
4729 ++mri_control_index;
4730 return n;
4731 }
4732
4733 /* Create a new MRI structured control directive. */
4734
4735 static struct mri_control_info *
4736 push_mri_control (type)
4737 enum mri_control_type type;
4738 {
4739 struct mri_control_info *n;
4740
4741 n = (struct mri_control_info *) xmalloc (sizeof (struct mri_control_info));
4742
4743 n->type = type;
4744 n->else_seen = 0;
4745 if (type == mri_if || type == mri_while)
4746 n->top = NULL;
4747 else
4748 n->top = mri_control_label ();
4749 n->next = mri_control_label ();
4750 n->bottom = mri_control_label ();
4751
4752 n->outer = mri_control_stack;
4753 mri_control_stack = n;
4754
4755 return n;
4756 }
4757
4758 /* Pop off the stack of MRI structured control directives. */
4759
4760 static void
4761 pop_mri_control ()
4762 {
4763 struct mri_control_info *n;
4764
4765 n = mri_control_stack;
4766 mri_control_stack = n->outer;
4767 if (n->top != NULL)
4768 free (n->top);
4769 free (n->next);
4770 free (n->bottom);
4771 free (n);
4772 }
4773
4774 /* Recognize a condition code in an MRI structured control expression. */
4775
4776 static int
4777 parse_mri_condition (pcc)
4778 int *pcc;
4779 {
4780 char c1, c2;
4781
4782 know (*input_line_pointer == '<');
4783
4784 ++input_line_pointer;
4785 c1 = *input_line_pointer++;
4786 c2 = *input_line_pointer++;
4787
4788 if (*input_line_pointer != '>')
4789 {
4790 as_bad ("syntax error in structured control directive");
4791 return 0;
4792 }
4793
4794 ++input_line_pointer;
4795 SKIP_WHITESPACE ();
4796
4797 if (isupper (c1))
4798 c1 = tolower (c1);
4799 if (isupper (c2))
4800 c2 = tolower (c2);
4801
4802 *pcc = (c1 << 8) | c2;
4803
4804 return 1;
4805 }
4806
4807 /* Parse a single operand in an MRI structured control expression. */
4808
4809 static int
4810 parse_mri_control_operand (pcc, leftstart, leftstop, rightstart, rightstop)
4811 int *pcc;
4812 const char **leftstart;
4813 const char **leftstop;
4814 const char **rightstart;
4815 const char **rightstop;
4816 {
4817 char *s;
4818
4819 SKIP_WHITESPACE ();
4820
4821 *pcc = -1;
4822 *leftstart = NULL;
4823 *leftstop = NULL;
4824 *rightstart = NULL;
4825 *rightstop = NULL;
4826
4827 if (*input_line_pointer == '<')
4828 {
4829 /* It's just a condition code. */
4830 return parse_mri_condition (pcc);
4831 }
4832
4833 /* Look ahead for the condition code. */
4834 for (s = input_line_pointer; *s != '\0'; ++s)
4835 {
4836 if (*s == '<' && s[1] != '\0' && s[2] != '\0' && s[3] == '>')
4837 break;
4838 }
4839 if (*s == '\0')
4840 {
4841 as_bad ("missing condition code in structured control directive");
4842 return 0;
4843 }
4844
4845 *leftstart = input_line_pointer;
4846 *leftstop = s;
4847 if (*leftstop > *leftstart
4848 && ((*leftstop)[-1] == ' ' || (*leftstop)[-1] == '\t'))
4849 --*leftstop;
4850
4851 input_line_pointer = s;
4852 if (! parse_mri_condition (pcc))
4853 return 0;
4854
4855 /* Look ahead for AND or OR or end of line. */
4856 for (s = input_line_pointer; *s != '\0'; ++s)
4857 {
4858 if ((strncasecmp (s, "AND", 3) == 0
4859 && (s[3] == '.' || ! is_part_of_name (s[3])))
4860 || (strncasecmp (s, "OR", 2) == 0
4861 && (s[2] == '.' || ! is_part_of_name (s[2]))))
4862 break;
4863 }
4864
4865 *rightstart = input_line_pointer;
4866 *rightstop = s;
4867 if (*rightstop > *rightstart
4868 && ((*rightstop)[-1] == ' ' || (*rightstop)[-1] == '\t'))
4869 --*rightstop;
4870
4871 input_line_pointer = s;
4872
4873 return 1;
4874 }
4875
4876 #define MCC(b1, b2) (((b1) << 8) | (b2))
4877
4878 /* Swap the sense of a condition. This changes the condition so that
4879 it generates the same result when the operands are swapped. */
4880
4881 static int
4882 swap_mri_condition (cc)
4883 int cc;
4884 {
4885 switch (cc)
4886 {
4887 case MCC ('h', 'i'): return MCC ('c', 's');
4888 case MCC ('l', 's'): return MCC ('c', 'c');
4889 case MCC ('c', 'c'): return MCC ('l', 's');
4890 case MCC ('c', 's'): return MCC ('h', 'i');
4891 case MCC ('p', 'l'): return MCC ('m', 'i');
4892 case MCC ('m', 'i'): return MCC ('p', 'l');
4893 case MCC ('g', 'e'): return MCC ('l', 'e');
4894 case MCC ('l', 't'): return MCC ('g', 't');
4895 case MCC ('g', 't'): return MCC ('l', 't');
4896 case MCC ('l', 'e'): return MCC ('g', 'e');
4897 }
4898 return cc;
4899 }
4900
4901 /* Reverse the sense of a condition. */
4902
4903 static int
4904 reverse_mri_condition (cc)
4905 int cc;
4906 {
4907 switch (cc)
4908 {
4909 case MCC ('h', 'i'): return MCC ('l', 's');
4910 case MCC ('l', 's'): return MCC ('h', 'i');
4911 case MCC ('c', 'c'): return MCC ('c', 's');
4912 case MCC ('c', 's'): return MCC ('c', 'c');
4913 case MCC ('n', 'e'): return MCC ('e', 'q');
4914 case MCC ('e', 'q'): return MCC ('n', 'e');
4915 case MCC ('v', 'c'): return MCC ('v', 's');
4916 case MCC ('v', 's'): return MCC ('v', 'c');
4917 case MCC ('p', 'l'): return MCC ('m', 'i');
4918 case MCC ('m', 'i'): return MCC ('p', 'l');
4919 case MCC ('g', 'e'): return MCC ('l', 't');
4920 case MCC ('l', 't'): return MCC ('g', 'e');
4921 case MCC ('g', 't'): return MCC ('l', 'e');
4922 case MCC ('l', 'e'): return MCC ('g', 't');
4923 }
4924 return cc;
4925 }
4926
4927 /* Build an MRI structured control expression. This generates test
4928 and branch instructions. It goes to TRUELAB if the condition is
4929 true, and to FALSELAB if the condition is false. Exactly one of
4930 TRUELAB and FALSELAB will be NULL, meaning to fall through. QUAL
4931 is the size qualifier for the expression. EXTENT is the size to
4932 use for the branch. */
4933
4934 static void
4935 build_mri_control_operand (qual, cc, leftstart, leftstop, rightstart,
4936 rightstop, truelab, falselab, extent)
4937 int qual;
4938 int cc;
4939 const char *leftstart;
4940 const char *leftstop;
4941 const char *rightstart;
4942 const char *rightstop;
4943 const char *truelab;
4944 const char *falselab;
4945 int extent;
4946 {
4947 char *buf;
4948 char *s;
4949
4950 /* The 68k can't do a general comparision with an immediate operand
4951 on the right hand side. */
4952 if (rightstart != NULL && *rightstart == '#')
4953 {
4954 const char *temp;
4955
4956 cc = swap_mri_condition (cc);
4957 temp = leftstart;
4958 leftstart = rightstart;
4959 rightstart = temp;
4960 temp = leftstop;
4961 leftstop = rightstop;
4962 rightstop = temp;
4963 }
4964
4965 if (truelab == NULL)
4966 {
4967 cc = reverse_mri_condition (cc);
4968 truelab = falselab;
4969 }
4970
4971 if (leftstart != NULL)
4972 {
4973 buf = (char *) xmalloc (20
4974 + (leftstop - leftstart)
4975 + (rightstop - rightstart));
4976 s = buf;
4977 *s++ = 'c';
4978 *s++ = 'm';
4979 *s++ = 'p';
4980 if (qual != '\0')
4981 *s++ = qual;
4982 *s++ = ' ';
4983 memcpy (s, leftstart, leftstop - leftstart);
4984 s += leftstop - leftstart;
4985 *s++ = ',';
4986 memcpy (s, rightstart, rightstop - rightstart);
4987 s += rightstop - rightstart;
4988 *s = '\0';
4989 md_assemble (buf);
4990 free (buf);
4991 }
4992
4993 buf = (char *) xmalloc (20 + strlen (truelab));
4994 s = buf;
4995 *s++ = 'b';
4996 *s++ = cc >> 8;
4997 *s++ = cc & 0xff;
4998 if (extent != '\0')
4999 *s++ = extent;
5000 *s++ = ' ';
5001 strcpy (s, truelab);
5002 md_assemble (buf);
5003 free (buf);
5004 }
5005
5006 /* Parse an MRI structured control expression. This generates test
5007 and branch instructions. STOP is where the expression ends. It
5008 goes to TRUELAB if the condition is true, and to FALSELAB if the
5009 condition is false. Exactly one of TRUELAB and FALSELAB will be
5010 NULL, meaning to fall through. QUAL is the size qualifier for the
5011 expression. EXTENT is the size to use for the branch. */
5012
5013 static void
5014 parse_mri_control_expression (stop, qual, truelab, falselab, extent)
5015 char *stop;
5016 int qual;
5017 const char *truelab;
5018 const char *falselab;
5019 int extent;
5020 {
5021 int c;
5022 int cc;
5023 const char *leftstart;
5024 const char *leftstop;
5025 const char *rightstart;
5026 const char *rightstop;
5027
5028 c = *stop;
5029 *stop = '\0';
5030
5031 if (! parse_mri_control_operand (&cc, &leftstart, &leftstop,
5032 &rightstart, &rightstop))
5033 {
5034 *stop = c;
5035 return;
5036 }
5037
5038 if (strncasecmp (input_line_pointer, "AND", 3) == 0)
5039 {
5040 const char *flab;
5041
5042 if (falselab != NULL)
5043 flab = falselab;
5044 else
5045 flab = mri_control_label ();
5046
5047 build_mri_control_operand (qual, cc, leftstart, leftstop, rightstart,
5048 rightstop, (const char *) NULL, flab, extent);
5049
5050 input_line_pointer += 3;
5051 if (*input_line_pointer != '.'
5052 || input_line_pointer[1] == '\0')
5053 qual = '\0';
5054 else
5055 {
5056 qual = input_line_pointer[1];
5057 input_line_pointer += 2;
5058 }
5059
5060 if (! parse_mri_control_operand (&cc, &leftstart, &leftstop,
5061 &rightstart, &rightstop))
5062 {
5063 *stop = c;
5064 return;
5065 }
5066
5067 build_mri_control_operand (qual, cc, leftstart, leftstop, rightstart,
5068 rightstop, truelab, falselab, extent);
5069
5070 if (falselab == NULL)
5071 colon (flab);
5072 }
5073 else if (strncasecmp (input_line_pointer, "OR", 2) == 0)
5074 {
5075 const char *tlab;
5076
5077 if (truelab != NULL)
5078 tlab = truelab;
5079 else
5080 tlab = mri_control_label ();
5081
5082 build_mri_control_operand (qual, cc, leftstart, leftstop, rightstart,
5083 rightstop, tlab, (const char *) NULL, extent);
5084
5085 input_line_pointer += 2;
5086 if (*input_line_pointer != '.'
5087 || input_line_pointer[1] == '\0')
5088 qual = '\0';
5089 else
5090 {
5091 qual = input_line_pointer[1];
5092 input_line_pointer += 2;
5093 }
5094
5095 if (! parse_mri_control_operand (&cc, &leftstart, &leftstop,
5096 &rightstart, &rightstop))
5097 {
5098 *stop = c;
5099 return;
5100 }
5101
5102 build_mri_control_operand (qual, cc, leftstart, leftstop, rightstart,
5103 rightstop, truelab, falselab, extent);
5104
5105 if (truelab == NULL)
5106 colon (tlab);
5107 }
5108 else
5109 {
5110 build_mri_control_operand (qual, cc, leftstart, leftstop, rightstart,
5111 rightstop, truelab, falselab, extent);
5112 }
5113
5114 *stop = c;
5115 if (input_line_pointer != stop)
5116 as_bad ("syntax error in structured control directive");
5117 }
5118
5119 /* Handle the MRI IF pseudo-op. This may be a structured control
5120 directive, or it may be a regular assembler conditional, depending
5121 on its operands. */
5122
5123 static void
5124 s_mri_if (qual)
5125 int qual;
5126 {
5127 char *s;
5128 int c;
5129 struct mri_control_info *n;
5130
5131 /* A structured control directive must end with THEN with an
5132 optional qualifier. */
5133 s = input_line_pointer;
5134 while (! is_end_of_line[(unsigned char) *s])
5135 ++s;
5136 --s;
5137 while (s > input_line_pointer && (*s == ' ' || *s == '\t'))
5138 --s;
5139
5140 if (s - input_line_pointer > 1
5141 && s[-1] == '.')
5142 s -= 2;
5143
5144 if (s - input_line_pointer < 3
5145 || strncasecmp (s - 3, "THEN", 4) != 0)
5146 {
5147 if (qual != '\0')
5148 {
5149 as_bad ("missing then");
5150 ignore_rest_of_line ();
5151 return;
5152 }
5153
5154 /* It's a conditional. */
5155 s_if (O_ne);
5156 return;
5157 }
5158
5159 /* Since this might be a conditional if, this pseudo-op will be
5160 called even if we are supported to be ignoring input. Double
5161 check now. Clobber *input_line_pointer so that ignore_input
5162 thinks that this is not a special pseudo-op. */
5163 c = *input_line_pointer;
5164 *input_line_pointer = 0;
5165 if (ignore_input ())
5166 {
5167 *input_line_pointer = c;
5168 while (! is_end_of_line[(unsigned char) *input_line_pointer])
5169 ++input_line_pointer;
5170 demand_empty_rest_of_line ();
5171 return;
5172 }
5173 *input_line_pointer = c;
5174
5175 n = push_mri_control (mri_if);
5176
5177 parse_mri_control_expression (s - 3, qual, (const char *) NULL,
5178 n->next, s[1] == '.' ? s[2] : '\0');
5179
5180 if (s[1] == '.')
5181 input_line_pointer = s + 3;
5182 else
5183 input_line_pointer = s + 1;
5184
5185 demand_empty_rest_of_line ();
5186 }
5187
5188 /* Handle the MRI else pseudo-op. If we are currently doing an MRI
5189 structured IF, associate the ELSE with the IF. Otherwise, assume
5190 it is a conditional else. */
5191
5192 static void
5193 s_mri_else (qual)
5194 int qual;
5195 {
5196 int c;
5197 char *buf;
5198 char q[2];
5199
5200 if (qual == '\0'
5201 && (mri_control_stack == NULL
5202 || mri_control_stack->type != mri_if
5203 || mri_control_stack->else_seen))
5204 {
5205 s_else (0);
5206 return;
5207 }
5208
5209 c = *input_line_pointer;
5210 *input_line_pointer = 0;
5211 if (ignore_input ())
5212 {
5213 *input_line_pointer = c;
5214 while (! is_end_of_line[(unsigned char) *input_line_pointer])
5215 ++input_line_pointer;
5216 demand_empty_rest_of_line ();
5217 return;
5218 }
5219 *input_line_pointer = c;
5220
5221 if (mri_control_stack == NULL
5222 || mri_control_stack->type != mri_if
5223 || mri_control_stack->else_seen)
5224 {
5225 as_bad ("else without matching if");
5226 ignore_rest_of_line ();
5227 return;
5228 }
5229
5230 mri_control_stack->else_seen = 1;
5231
5232 buf = (char *) xmalloc (20 + strlen (mri_control_stack->bottom));
5233 q[0] = qual;
5234 q[1] = '\0';
5235 sprintf (buf, "bra%s %s", q, mri_control_stack->bottom);
5236 md_assemble (buf);
5237 free (buf);
5238
5239 colon (mri_control_stack->next);
5240
5241 demand_empty_rest_of_line ();
5242 }
5243
5244 /* Handle the MRI ENDI pseudo-op. */
5245
5246 static void
5247 s_mri_endi (ignore)
5248 int ignore;
5249 {
5250 if (mri_control_stack == NULL
5251 || mri_control_stack->type != mri_if)
5252 {
5253 as_bad ("endi without matching if");
5254 ignore_rest_of_line ();
5255 return;
5256 }
5257
5258 /* ignore_input will not return true for ENDI, so we don't need to
5259 worry about checking it again here. */
5260
5261 if (! mri_control_stack->else_seen)
5262 colon (mri_control_stack->next);
5263 colon (mri_control_stack->bottom);
5264
5265 pop_mri_control ();
5266
5267 demand_empty_rest_of_line ();
5268 }
5269
5270 /* Handle the MRI BREAK pseudo-op. */
5271
5272 static void
5273 s_mri_break (extent)
5274 int extent;
5275 {
5276 struct mri_control_info *n;
5277 char *buf;
5278 char ex[2];
5279
5280 n = mri_control_stack;
5281 while (n != NULL
5282 && n->type != mri_for
5283 && n->type != mri_repeat
5284 && n->type != mri_while)
5285 n = n->outer;
5286 if (n == NULL)
5287 {
5288 as_bad ("break outside of structured loop");
5289 ignore_rest_of_line ();
5290 return;
5291 }
5292
5293 buf = (char *) xmalloc (20 + strlen (n->bottom));
5294 ex[0] = extent;
5295 ex[1] = '\0';
5296 sprintf (buf, "bra%s %s", ex, n->bottom);
5297 md_assemble (buf);
5298 free (buf);
5299
5300 demand_empty_rest_of_line ();
5301 }
5302
5303 /* Handle the MRI NEXT pseudo-op. */
5304
5305 static void
5306 s_mri_next (extent)
5307 int extent;
5308 {
5309 struct mri_control_info *n;
5310 char *buf;
5311 char ex[2];
5312
5313 n = mri_control_stack;
5314 while (n != NULL
5315 && n->type != mri_for
5316 && n->type != mri_repeat
5317 && n->type != mri_while)
5318 n = n->outer;
5319 if (n == NULL)
5320 {
5321 as_bad ("next outside of structured loop");
5322 ignore_rest_of_line ();
5323 return;
5324 }
5325
5326 buf = (char *) xmalloc (20 + strlen (n->next));
5327 ex[0] = extent;
5328 ex[1] = '\0';
5329 sprintf (buf, "bra%s %s", ex, n->next);
5330 md_assemble (buf);
5331 free (buf);
5332
5333 demand_empty_rest_of_line ();
5334 }
5335
5336 /* Handle the MRI FOR pseudo-op. */
5337
5338 static void
5339 s_mri_for (qual)
5340 int qual;
5341 {
5342 const char *varstart, *varstop;
5343 const char *initstart, *initstop;
5344 const char *endstart, *endstop;
5345 const char *bystart, *bystop;
5346 int up;
5347 int by;
5348 int extent;
5349 struct mri_control_info *n;
5350 char *buf;
5351 char *s;
5352 char ex[2];
5353
5354 /* The syntax is
5355 FOR.q var = init { TO | DOWNTO } end [ BY by ] DO.e
5356 */
5357
5358 SKIP_WHITESPACE ();
5359 varstart = input_line_pointer;
5360
5361 /* Look for the '='. */
5362 while (! is_end_of_line[(unsigned char) *input_line_pointer]
5363 && *input_line_pointer != '=')
5364 ++input_line_pointer;
5365 if (*input_line_pointer != '=')
5366 {
5367 as_bad ("missing =");
5368 ignore_rest_of_line ();
5369 return;
5370 }
5371
5372 varstop = input_line_pointer;
5373 if (varstop > varstart
5374 && (varstop[-1] == ' ' || varstop[-1] == '\t'))
5375 --varstop;
5376
5377 ++input_line_pointer;
5378
5379 initstart = input_line_pointer;
5380
5381 /* Look for TO or DOWNTO. */
5382 up = 1;
5383 initstop = NULL;
5384 while (! is_end_of_line[(unsigned char) *input_line_pointer])
5385 {
5386 if (strncasecmp (input_line_pointer, "TO", 2) == 0
5387 && ! is_part_of_name (input_line_pointer[2]))
5388 {
5389 initstop = input_line_pointer;
5390 input_line_pointer += 2;
5391 break;
5392 }
5393 if (strncasecmp (input_line_pointer, "DOWNTO", 6) == 0
5394 && ! is_part_of_name (input_line_pointer[6]))
5395 {
5396 initstop = input_line_pointer;
5397 up = 0;
5398 input_line_pointer += 6;
5399 break;
5400 }
5401 ++input_line_pointer;
5402 }
5403 if (initstop == NULL)
5404 {
5405 as_bad ("missing to or downto");
5406 ignore_rest_of_line ();
5407 return;
5408 }
5409 if (initstop > initstart
5410 && (initstop[-1] == ' ' || initstop[-1] == '\t'))
5411 --initstop;
5412
5413 SKIP_WHITESPACE ();
5414 endstart = input_line_pointer;
5415
5416 /* Look for BY or DO. */
5417 by = 0;
5418 endstop = NULL;
5419 while (! is_end_of_line[(unsigned char) *input_line_pointer])
5420 {
5421 if (strncasecmp (input_line_pointer, "BY", 2) == 0
5422 && ! is_part_of_name (input_line_pointer[2]))
5423 {
5424 endstop = input_line_pointer;
5425 by = 1;
5426 input_line_pointer += 2;
5427 break;
5428 }
5429 if (strncasecmp (input_line_pointer, "DO", 2) == 0
5430 && (input_line_pointer[2] == '.'
5431 || ! is_part_of_name (input_line_pointer[2])))
5432 {
5433 endstop = input_line_pointer;
5434 input_line_pointer += 2;
5435 break;
5436 }
5437 ++input_line_pointer;
5438 }
5439 if (endstop == NULL)
5440 {
5441 as_bad ("missing do");
5442 ignore_rest_of_line ();
5443 return;
5444 }
5445 if (endstop > endstart
5446 && (endstop[-1] == ' ' || endstop[-1] == '\t'))
5447 --endstop;
5448
5449 if (! by)
5450 {
5451 bystart = "#1";
5452 bystop = bystart + 2;
5453 }
5454 else
5455 {
5456 SKIP_WHITESPACE ();
5457 bystart = input_line_pointer;
5458
5459 /* Look for DO. */
5460 bystop = NULL;
5461 while (! is_end_of_line[(unsigned char) *input_line_pointer])
5462 {
5463 if (strncasecmp (input_line_pointer, "DO", 2) == 0
5464 && (input_line_pointer[2] == '.'
5465 || ! is_part_of_name (input_line_pointer[2])))
5466 {
5467 bystop = input_line_pointer;
5468 input_line_pointer += 2;
5469 break;
5470 }
5471 ++input_line_pointer;
5472 }
5473 if (bystop == NULL)
5474 {
5475 as_bad ("missing do");
5476 ignore_rest_of_line ();
5477 return;
5478 }
5479 if (bystop > bystart
5480 && (bystop[-1] == ' ' || bystop[-1] == '\t'))
5481 --bystop;
5482 }
5483
5484 if (*input_line_pointer != '.')
5485 extent = '\0';
5486 else
5487 {
5488 extent = input_line_pointer[1];
5489 input_line_pointer += 2;
5490 }
5491
5492 /* We have fully parsed the FOR operands. Now build the loop. */
5493
5494 n = push_mri_control (mri_for);
5495
5496 buf = (char *) xmalloc (50 + (input_line_pointer - varstart));
5497
5498 /* move init,var */
5499 s = buf;
5500 *s++ = 'm';
5501 *s++ = 'o';
5502 *s++ = 'v';
5503 *s++ = 'e';
5504 if (qual != '\0')
5505 *s++ = qual;
5506 *s++ = ' ';
5507 memcpy (s, initstart, initstop - initstart);
5508 s += initstop - initstart;
5509 *s++ = ',';
5510 memcpy (s, varstart, varstop - varstart);
5511 s += varstop - varstart;
5512 *s = '\0';
5513 md_assemble (buf);
5514
5515 colon (n->top);
5516
5517 /* cmp end,var */
5518 s = buf;
5519 *s++ = 'c';
5520 *s++ = 'm';
5521 *s++ = 'p';
5522 if (qual != '\0')
5523 *s++ = qual;
5524 *s++ = ' ';
5525 memcpy (s, endstart, endstop - endstart);
5526 s += endstop - endstart;
5527 *s++ = ',';
5528 memcpy (s, varstart, varstop - varstart);
5529 s += varstop - varstart;
5530 *s = '\0';
5531 md_assemble (buf);
5532
5533 /* bcc bottom */
5534 ex[0] = extent;
5535 ex[1] = '\0';
5536 if (up)
5537 sprintf (buf, "blt%s %s", ex, n->bottom);
5538 else
5539 sprintf (buf, "bgt%s %s", ex, n->bottom);
5540 md_assemble (buf);
5541
5542 /* Put together the add or sub instruction used by ENDF. */
5543 s = buf;
5544 if (up)
5545 strcpy (s, "add");
5546 else
5547 strcpy (s, "sub");
5548 s += 3;
5549 if (qual != '\0')
5550 *s++ = qual;
5551 *s++ = ' ';
5552 memcpy (s, bystart, bystop - bystart);
5553 s += bystop - bystart;
5554 *s++ = ',';
5555 memcpy (s, varstart, varstop - varstart);
5556 s += varstop - varstart;
5557 *s = '\0';
5558 n->incr = buf;
5559
5560 demand_empty_rest_of_line ();
5561 }
5562
5563 /* Handle the MRI ENDF pseudo-op. */
5564
5565 static void
5566 s_mri_endf (ignore)
5567 int ignore;
5568 {
5569 if (mri_control_stack == NULL
5570 || mri_control_stack->type != mri_for)
5571 {
5572 as_bad ("endf without for");
5573 ignore_rest_of_line ();
5574 return;
5575 }
5576
5577 colon (mri_control_stack->next);
5578
5579 md_assemble (mri_control_stack->incr);
5580
5581 sprintf (mri_control_stack->incr, "bra %s", mri_control_stack->top);
5582 md_assemble (mri_control_stack->incr);
5583
5584 free (mri_control_stack->incr);
5585
5586 colon (mri_control_stack->bottom);
5587
5588 pop_mri_control ();
5589
5590 demand_empty_rest_of_line ();
5591 }
5592
5593 /* Handle the MRI REPEAT pseudo-op. */
5594
5595 static void
5596 s_mri_repeat (ignore)
5597 int ignore;
5598 {
5599 struct mri_control_info *n;
5600
5601 n = push_mri_control (mri_repeat);
5602 colon (n->top);
5603 demand_empty_rest_of_line ();
5604 }
5605
5606 /* Handle the MRI UNTIL pseudo-op. */
5607
5608 static void
5609 s_mri_until (qual)
5610 int qual;
5611 {
5612 char *s;
5613
5614 if (mri_control_stack == NULL
5615 || mri_control_stack->type != mri_repeat)
5616 {
5617 as_bad ("until without repeat");
5618 ignore_rest_of_line ();
5619 return;
5620 }
5621
5622 colon (mri_control_stack->next);
5623
5624 for (s = input_line_pointer; ! is_end_of_line[(unsigned char) *s]; s++)
5625 ;
5626
5627 parse_mri_control_expression (s, qual, (const char *) NULL,
5628 mri_control_stack->top, '\0');
5629
5630 colon (mri_control_stack->bottom);
5631
5632 input_line_pointer = s;
5633
5634 demand_empty_rest_of_line ();
5635 }
5636
5637 /* Handle the MRI WHILE pseudo-op. */
5638
5639 static void
5640 s_mri_while (qual)
5641 int qual;
5642 {
5643 char *s;
5644
5645 struct mri_control_info *n;
5646
5647 s = input_line_pointer;
5648 while (! is_end_of_line[(unsigned char) *s])
5649 s++;
5650 --s;
5651 while (*s == ' ' || *s == '\t')
5652 --s;
5653 if (s - input_line_pointer > 1
5654 && s[-1] == '.')
5655 s -= 2;
5656 if (s - input_line_pointer < 2
5657 || strncasecmp (s - 1, "DO", 2) != 0)
5658 {
5659 as_bad ("missing do");
5660 ignore_rest_of_line ();
5661 return;
5662 }
5663
5664 n = push_mri_control (mri_while);
5665
5666 colon (n->next);
5667
5668 parse_mri_control_expression (s - 1, qual, (const char *) NULL, n->bottom,
5669 s[1] == '.' ? s[2] : '\0');
5670
5671 input_line_pointer = s + 1;
5672 if (*input_line_pointer == '.')
5673 input_line_pointer += 2;
5674
5675 demand_empty_rest_of_line ();
5676 }
5677
5678 /* Handle the MRI ENDW pseudo-op. */
5679
5680 static void
5681 s_mri_endw (ignore)
5682 int ignore;
5683 {
5684 char *buf;
5685
5686 if (mri_control_stack == NULL
5687 || mri_control_stack->type != mri_while)
5688 {
5689 as_bad ("endw without while");
5690 ignore_rest_of_line ();
5691 return;
5692 }
5693
5694 buf = (char *) xmalloc (20 + strlen (mri_control_stack->next));
5695 sprintf (buf, "bra %s", mri_control_stack->next);
5696 md_assemble (buf);
5697 free (buf);
5698
5699 colon (mri_control_stack->bottom);
5700
5701 pop_mri_control ();
5702
5703 demand_empty_rest_of_line ();
5704 }
5705 \f
5706 /*
5707 * md_parse_option
5708 * Invocation line includes a switch not recognized by the base assembler.
5709 * See if it's a processor-specific option. These are:
5710 *
5711 * -[A]m[c]68000, -[A]m[c]68008, -[A]m[c]68010, -[A]m[c]68020, -[A]m[c]68030, -[A]m[c]68040
5712 * -[A]m[c]68881, -[A]m[c]68882, -[A]m[c]68851
5713 * Select the architecture. Instructions or features not
5714 * supported by the selected architecture cause fatal
5715 * errors. More than one may be specified. The default is
5716 * -m68020 -m68851 -m68881. Note that -m68008 is a synonym
5717 * for -m68000, and -m68882 is a synonym for -m68881.
5718 * -[A]m[c]no-68851, -[A]m[c]no-68881
5719 * Don't accept 688?1 instructions. (The "c" is kind of silly,
5720 * so don't use or document it, but that's the way the parsing
5721 * works).
5722 *
5723 * -pic Indicates PIC.
5724 * -k Indicates PIC. (Sun 3 only.)
5725 *
5726 */
5727
5728 #ifdef OBJ_ELF
5729 CONST char *md_shortopts = "lSA:m:kQ:V";
5730 #else
5731 CONST char *md_shortopts = "lSA:m:k";
5732 #endif
5733
5734 struct option md_longopts[] = {
5735 #define OPTION_PIC (OPTION_MD_BASE)
5736 {"pic", no_argument, NULL, OPTION_PIC},
5737 #define OPTION_REGISTER_PREFIX_OPTIONAL (OPTION_MD_BASE + 1)
5738 {"register-prefix-optional", no_argument, NULL,
5739 OPTION_REGISTER_PREFIX_OPTIONAL},
5740 {NULL, no_argument, NULL, 0}
5741 };
5742 size_t md_longopts_size = sizeof(md_longopts);
5743
5744 int
5745 md_parse_option (c, arg)
5746 int c;
5747 char *arg;
5748 {
5749 switch (c)
5750 {
5751 case 'l': /* -l means keep external to 2 bit offset
5752 rather than 16 bit one */
5753 flag_short_refs = 1;
5754 break;
5755
5756 case 'S': /* -S means that jbsr's always turn into
5757 jsr's. */
5758 flag_long_jumps = 1;
5759 break;
5760
5761 case 'A':
5762 if (*arg == 'm')
5763 arg++;
5764 /* intentional fall-through */
5765 case 'm':
5766
5767 if (arg[0] == 'n' && arg[1] == 'o' && arg[2] == '-')
5768 {
5769 int i;
5770 unsigned long arch;
5771 const char *oarg = arg;
5772
5773 arg += 3;
5774 if (*arg == 'm')
5775 {
5776 arg++;
5777 if (arg[0] == 'c' && arg[1] == '6')
5778 arg++;
5779 }
5780 for (i = 0; i < n_archs; i++)
5781 if (!strcmp (arg, archs[i].name))
5782 break;
5783 if (i == n_archs)
5784 {
5785 unknown:
5786 as_bad ("unrecognized option `%s'", oarg);
5787 return 0;
5788 }
5789 arch = archs[i].arch;
5790 if (arch == m68881)
5791 no_68881 = 1;
5792 else if (arch == m68851)
5793 no_68851 = 1;
5794 else
5795 goto unknown;
5796 }
5797 else
5798 {
5799 int i;
5800
5801 if (arg[0] == 'c' && arg[1] == '6')
5802 arg++;
5803
5804 for (i = 0; i < n_archs; i++)
5805 if (!strcmp (arg, archs[i].name))
5806 {
5807 unsigned long arch = archs[i].arch;
5808 if (cpu_of_arch (arch))
5809 /* It's a cpu spec. */
5810 {
5811 current_architecture &= ~m68000up;
5812 current_architecture |= arch;
5813 }
5814 else if (arch == m68881)
5815 {
5816 current_architecture |= m68881;
5817 no_68881 = 0;
5818 }
5819 else if (arch == m68851)
5820 {
5821 current_architecture |= m68851;
5822 no_68851 = 0;
5823 }
5824 else
5825 /* ??? */
5826 abort ();
5827 break;
5828 }
5829 if (i == n_archs)
5830 {
5831 as_bad ("unrecognized architecture specification `%s'", arg);
5832 return 0;
5833 }
5834 }
5835 break;
5836
5837 case OPTION_PIC:
5838 case 'k':
5839 flag_want_pic = 1;
5840 break; /* -pic, Position Independent Code */
5841
5842 case OPTION_REGISTER_PREFIX_OPTIONAL:
5843 flag_reg_prefix_optional = 1;
5844 break;
5845
5846 case 'Q':
5847 case 'V':
5848 break;
5849
5850 default:
5851 return 0;
5852 }
5853
5854 return 1;
5855 }
5856
5857 void
5858 md_show_usage (stream)
5859 FILE *stream;
5860 {
5861 fprintf(stream, "\
5862 680X0 options:\n\
5863 -l use 1 word for refs to undefined symbols [default 2]\n\
5864 -m68000 | -m68008 | -m68010 | -m68020 | -m68030 | -m68040 | -m68060\n\
5865 | -m68302 | -m68331 | -m68332 | -m68333 | -m68340 | -m68360\n\
5866 | -mcpu32\n\
5867 specify variant of 680X0 architecture [default 68020]\n\
5868 -m68881 | -m68882 | -mno-68881 | -mno-68882\n\
5869 target has/lacks floating-point coprocessor\n\
5870 [default yes for 68020, 68030, and cpu32]\n");
5871 fprintf(stream, "\
5872 -m68851 | -mno-68851\n\
5873 target has/lacks memory-management unit coprocessor\n\
5874 [default yes for 68020 and up]\n\
5875 -pic, -k generate position independent code\n\
5876 -S turn jbsr into jsr\n\
5877 --register-prefix-optional\n\
5878 recognize register names without prefix character\n");
5879 }
5880 \f
5881 #ifdef TEST2
5882
5883 /* TEST2: Test md_assemble() */
5884 /* Warning, this routine probably doesn't work anymore */
5885
5886 main ()
5887 {
5888 struct m68k_it the_ins;
5889 char buf[120];
5890 char *cp;
5891 int n;
5892
5893 m68k_ip_begin ();
5894 for (;;)
5895 {
5896 if (!gets (buf) || !*buf)
5897 break;
5898 if (buf[0] == '|' || buf[1] == '.')
5899 continue;
5900 for (cp = buf; *cp; cp++)
5901 if (*cp == '\t')
5902 *cp = ' ';
5903 if (is_label (buf))
5904 continue;
5905 memset (&the_ins, '\0', sizeof (the_ins));
5906 m68k_ip (&the_ins, buf);
5907 if (the_ins.error)
5908 {
5909 printf ("Error %s in %s\n", the_ins.error, buf);
5910 }
5911 else
5912 {
5913 printf ("Opcode(%d.%s): ", the_ins.numo, the_ins.args);
5914 for (n = 0; n < the_ins.numo; n++)
5915 printf (" 0x%x", the_ins.opcode[n] & 0xffff);
5916 printf (" ");
5917 print_the_insn (&the_ins.opcode[0], stdout);
5918 (void) putchar ('\n');
5919 }
5920 for (n = 0; n < strlen (the_ins.args) / 2; n++)
5921 {
5922 if (the_ins.operands[n].error)
5923 {
5924 printf ("op%d Error %s in %s\n", n, the_ins.operands[n].error, buf);
5925 continue;
5926 }
5927 printf ("mode %d, reg %d, ", the_ins.operands[n].mode, the_ins.operands[n].reg);
5928 if (the_ins.operands[n].b_const)
5929 printf ("Constant: '%.*s', ", 1 + the_ins.operands[n].e_const - the_ins.operands[n].b_const, the_ins.operands[n].b_const);
5930 printf ("ireg %d, isiz %d, imul %d, ", the_ins.operands[n].ireg, the_ins.operands[n].isiz, the_ins.operands[n].imul);
5931 if (the_ins.operands[n].b_iadd)
5932 printf ("Iadd: '%.*s',", 1 + the_ins.operands[n].e_iadd - the_ins.operands[n].b_iadd, the_ins.operands[n].b_iadd);
5933 (void) putchar ('\n');
5934 }
5935 }
5936 m68k_ip_end ();
5937 return 0;
5938 }
5939
5940 is_label (str)
5941 char *str;
5942 {
5943 while (*str == ' ')
5944 str++;
5945 while (*str && *str != ' ')
5946 str++;
5947 if (str[-1] == ':' || str[1] == '=')
5948 return 1;
5949 return 0;
5950 }
5951
5952 #endif
5953
5954 /* Possible states for relaxation:
5955
5956 0 0 branch offset byte (bra, etc)
5957 0 1 word
5958 0 2 long
5959
5960 1 0 indexed offsets byte a0@(32,d4:w:1) etc
5961 1 1 word
5962 1 2 long
5963
5964 2 0 two-offset index word-word a0@(32,d4)@(45) etc
5965 2 1 word-long
5966 2 2 long-word
5967 2 3 long-long
5968
5969 */
5970
5971 /* We have no need to default values of symbols. */
5972
5973 /* ARGSUSED */
5974 symbolS *
5975 md_undefined_symbol (name)
5976 char *name;
5977 {
5978 return 0;
5979 }
5980
5981 /* Round up a section size to the appropriate boundary. */
5982 valueT
5983 md_section_align (segment, size)
5984 segT segment;
5985 valueT size;
5986 {
5987 return size; /* Byte alignment is fine */
5988 }
5989
5990 /* Exactly what point is a PC-relative offset relative TO?
5991 On the 68k, it is relative to the address of the first extension
5992 word. The difference between the addresses of the offset and the
5993 first extension word is stored in fx_pcrel_adjust. */
5994 long
5995 md_pcrel_from (fixP)
5996 fixS *fixP;
5997 {
5998 int adjust;
5999
6000 /* Because fx_pcrel_adjust is a char, and may be unsigned, we store
6001 -1 as 64. */
6002 adjust = fixP->fx_pcrel_adjust;
6003 if (adjust == 64)
6004 adjust = -1;
6005 return fixP->fx_where + fixP->fx_frag->fr_address - adjust;
6006 }
6007
6008 #ifndef BFD_ASSEMBLER
6009 /*ARGSUSED*/
6010 void
6011 tc_coff_symbol_emit_hook (ignore)
6012 symbolS *ignore;
6013 {
6014 }
6015
6016 int
6017 tc_coff_sizemachdep (frag)
6018 fragS *frag;
6019 {
6020 switch (frag->fr_subtype & 0x3)
6021 {
6022 case BYTE:
6023 return 1;
6024 case SHORT:
6025 return 2;
6026 case LONG:
6027 return 4;
6028 default:
6029 abort ();
6030 return 0;
6031 }
6032 }
6033 #endif
6034
6035 /* end of tc-m68k.c */