]> Git Repo - binutils.git/blob - gas/config/tc-m68k.c
typo in a comment.
[binutils.git] / gas / config / tc-m68k.c
1 /* m68k.c  All the m68020 specific stuff in one convenient, huge,
2    slow to compile, easy to find file.
3    Copyright (C) 1987, 1991 Free Software Foundation, Inc.
4
5 This file is part of GAS, the GNU Assembler.
6
7 GAS is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
10 any later version.
11
12 GAS is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with GAS; see the file COPYING.  If not, write to
19 the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
20
21 #include <ctype.h>
22
23 #include "as.h"
24
25 #include "obstack.h"
26
27  /* note that this file includes real declarations and thus can only be included by one source file per executable. */
28 #include "m68k-opcode.h"
29 #ifdef TE_SUN
30 /* This variable contains the value to write out at the beginning of
31    the a.out file.  The 2<<16 means that this is a 68020 file instead
32    of an old-style 68000 file */
33
34 long omagic = 2<<16|OMAGIC;     /* Magic byte for header file */
35 #else
36 long omagic = OMAGIC;
37 #endif
38
39 /* This array holds the chars that always start a comment.  If the
40    pre-processor is disabled, these aren't very useful */
41 const char comment_chars[] = "|";
42
43 /* This array holds the chars that only start a comment at the beginning of
44    a line.  If the line seems to have the form '# 123 filename'
45    .line and .file directives will appear in the pre-processed output */
46 /* Note that input_file.c hand checks for '#' at the beginning of the
47    first line of the input file.  This is because the compiler outputs
48    #NO_APP at the beginning of its output. */
49 /* Also note that comments like this one will always work. */
50 const char line_comment_chars[] = "#";
51
52 /* Chars that can be used to separate mant from exp in floating point nums */
53 const char EXP_CHARS[] = "eE";
54
55 /* Chars that mean this number is a floating point constant */
56 /* As in 0f12.456 */
57 /* or    0d1.2345e12 */
58
59 const char FLT_CHARS[] = "rRsSfFdDxXeEpP";
60
61 /* Also be aware that MAXIMUM_NUMBER_OF_CHARS_FOR_FLOAT may have to be
62    changed in read.c .  Ideally it shouldn't have to know about it at all,
63    but nothing is ideal around here.
64  */
65
66 int md_reloc_size = 8;          /* Size of relocation record */
67
68 /* Its an arbitrary name:  This means I don't approve of it */
69 /* See flames below */
70 static struct obstack robyn;
71
72 #define TAB(x,y)        (((x)<<2)+(y))
73 #define TABTYPE(xy)     ((xy) >> 2)
74 #define BYTE            0
75 #define SHORT           1
76 #define LONG            2
77 #define SZ_UNDEF        3
78
79 #define BRANCH          1
80 #define FBRANCH         2
81 #define PCREL           3
82 #define BCC68000        4
83 #define DBCC            5
84 #define PCLEA           6
85
86 /* Operands we can parse:  (And associated modes)
87
88 numb:   8 bit num
89 numw:   16 bit num
90 numl:   32 bit num
91 dreg:   data reg 0-7
92 reg:    address or data register
93 areg:   address register
94 apc:    address register, PC, ZPC or empty string
95 num:    16 or 32 bit num
96 num2:   like num
97 sz:     w or l          if omitted, l assumed
98 scale:  1 2 4 or 8      if omitted, 1 assumed
99
100 7.4 IMMED #num                          --> NUM
101 0.? DREG  dreg                          --> dreg
102 1.? AREG  areg                          --> areg
103 2.? AINDR areg@                         --> *(areg)
104 3.? AINC  areg@+                        --> *(areg++)
105 4.? ADEC  areg@-                        --> *(--areg)
106 5.? AOFF  apc@(numw)                    --> *(apc+numw) -- empty string and ZPC not allowed here
107 6.? AINDX apc@(num,reg:sz:scale)        --> *(apc+num+reg*scale)
108 6.? AINDX apc@(reg:sz:scale)            --> same, with num=0
109 6.? APODX apc@(num)@(num2,reg:sz:scale) --> *(*(apc+num)+num2+reg*scale)
110 6.? APODX apc@(num)@(reg:sz:scale)      --> same, with num2=0
111 6.? AMIND apc@(num)@(num2)              --> *(*(apc+num)+num2) (previous mode without an index reg)
112 6.? APRDX apc@(num,reg:sz:scale)@(num2) --> *(*(apc+num+reg*scale)+num2)
113 6.? APRDX apc@(reg:sz:scale)@(num2)     --> same, with num=0
114 7.0 ABSL  num:sz                        --> *(num)
115           num                           --> *(num) (sz L assumed)
116 *** MSCR  otherreg                      --> Magic
117 With -l option
118 5.? AOFF  apc@(num)                     --> *(apc+num) -- empty string and ZPC not allowed here still
119
120 examples:
121         #foo    #0x35   #12
122         d2
123         a4
124         a3@
125         a5@+
126         a6@-
127         a2@(12) pc@(14)
128         a1@(5,d2:w:1)   @(45,d6:l:4)
129         pc@(a2)         @(d4)
130         etc . . .
131
132
133 #name@(numw)    -->turn into PC rel mode
134 apc@(num8,reg:sz:scale)         --> *(apc+num8+reg*scale)
135
136 */
137
138 enum operand_type {
139     IMMED = 1,
140     DREG,
141     AREG,
142     AINDR,
143     ADEC,
144     AINC,
145     AOFF,
146     AINDX,
147     APODX,
148     AMIND,
149     APRDX,
150     ABSL,
151     MSCR,
152     REGLST,
153 };
154
155
156 struct m68k_exp {
157         char    *e_beg;
158         char    *e_end;
159         expressionS e_exp;
160         short   e_siz;          /* 0== default 1==short/byte 2==word 3==long */
161 };
162
163 /* DATA and ADDR have to be contiguous, so that reg-DATA gives 0-7==data reg,
164    8-15==addr reg for operands that take both types */
165
166 enum _register {
167     DATA = 1,           /*   1- 8 == data registers 0-7 */
168     DATA0 = DATA,
169     DATA1,
170     DATA2,
171     DATA3,
172     DATA4,
173     DATA5,
174     DATA6,
175     DATA7,
176     
177     ADDR,
178     ADDR0 = ADDR,
179     ADDR1,
180     ADDR2,
181     ADDR3,
182     ADDR4,
183     ADDR5,
184     ADDR6,
185     ADDR7,
186
187 /* Note that COPNUM==processor #1 -- COPNUM+7==#8, which stores as 000 */
188 /* I think. . .  */
189
190     SP = ADDR7,
191
192     FPREG, /* Eight FP registers */
193     FP0 = FPREG,
194     FP1,
195     FP2,
196     FP3,
197     FP4,
198     FP5,
199     FP6,
200     FP7,
201     COPNUM = (FPREG+8), /* Co-processor #1-#8 */
202     COP0 = COPNUM,
203     COP1,
204     COP2,
205     COP3,
206     COP4,
207     COP5,
208     COP6,
209     COP7,
210     PC, /* Program counter */
211     ZPC, /* Hack for Program space, but 0 addressing */
212     SR, /* Status Reg */
213     CCR, /* Condition code Reg */
214
215 /* These have to be in order for the movec instruction to work. */
216     USP,        /*  User Stack Pointer */
217     ISP,        /*  Interrupt stack pointer */
218     SFC,
219     DFC,
220     CACR,
221     VBR,
222     CAAR,
223     MSP,
224     ITT0,
225     ITT1,
226     DTT0,
227     DTT1,
228     MMUSR,
229     TC,
230     SRP,
231     URP,
232 /* end of movec ordering constraints */
233
234     FPI,
235     FPS,
236     FPC,
237
238     DRP,
239     CRP,
240     CAL,
241     VAL,
242     SCC,
243     AC,
244     BAD,
245     BAD0 = BAD,
246     BAD1,
247     BAD2,
248     BAD3,
249     BAD4,
250     BAD5,
251     BAD6,
252     BAD7,
253     BAC,
254     BAC0 = BAC,
255     BAC1,
256     BAC2,
257     BAC3,
258     BAC4,
259     BAC5,
260     BAC6,
261     BAC7,
262     PSR,
263     PCSR,
264
265     IC, /* instruction cache token */
266     DC, /* data cache token */
267     NC, /* no cache token */
268     BC, /* both caches token */
269
270 };
271
272 /* Internal form of an operand.  */
273 struct m68k_op {
274         char    *error;         /* Couldn't parse it */
275         enum operand_type mode; /* What mode this instruction is in.  */
276         enum _register reg;             /* Base register */
277         struct m68k_exp *con1;
278         int     ireg;           /* Index register */
279         int     isiz;           /* 0==unspec  1==byte(?)  2==short  3==long  */
280         int     imul;           /* Multipy ireg by this (1,2,4,or 8) */
281         struct  m68k_exp *con2;
282 };
283
284 /* internal form of a 68020 instruction */
285 struct m68k_it {
286         char    *error;
287         char    *args;          /* list of opcode info */
288         int     numargs;
289
290         int     numo;           /* Number of shorts in opcode */
291         short   opcode[11];
292
293         struct m68k_op operands[6];
294
295         int     nexp;           /* number of exprs in use */
296         struct m68k_exp exprs[4];
297
298         int     nfrag;          /* Number of frags we have to produce */
299         struct {
300                 int fragoff;    /* Where in the current opcode[] the frag ends */
301                 symbolS *fadd;
302                 long foff;
303                 int fragty;
304         } fragb[4];
305
306         int     nrel;           /* Num of reloc strucs in use */
307         struct  {
308                 int     n;
309                 symbolS *add,
310                         *sub;
311                 long off;
312                 char    wid;
313                 char    pcrel;
314         } reloc[5];             /* Five is enough??? */
315 };
316
317 static struct m68k_it the_ins;          /* the instruction being assembled */
318 static enum m68k_architecture max_arch_this_insn;
319
320 /* Macros for adding things to the m68k_it struct */
321
322 #define addword(w)      the_ins.opcode[the_ins.numo++]=(w)
323
324 /* Like addword, but goes BEFORE general operands */
325 #define insop(w)        {int z;\
326  for(z=the_ins.numo;z>opcode->m_codenum;--z)\
327    the_ins.opcode[z]=the_ins.opcode[z-1];\
328  for(z=0;z<the_ins.nrel;z++)\
329    the_ins.reloc[z].n+=2;\
330  the_ins.opcode[opcode->m_codenum]=w;\
331  the_ins.numo++;\
332 }
333
334
335 #define add_exp(beg,end) (\
336         the_ins.exprs[the_ins.nexp].e_beg=beg,\
337         the_ins.exprs[the_ins.nexp].e_end=end,\
338         &the_ins.exprs[the_ins.nexp++]\
339 )
340
341
342 /* The numo+1 kludge is so we can hit the low order byte of the prev word. Blecch*/
343 #define add_fix(width,exp,pc_rel) {\
344         the_ins.reloc[the_ins.nrel].n= ((width)=='B') ? (the_ins.numo*2-1) : \
345                 (((width)=='b') ? ((the_ins.numo-1)*2) : (the_ins.numo*2));\
346         the_ins.reloc[the_ins.nrel].add=adds((exp));\
347         the_ins.reloc[the_ins.nrel].sub=subs((exp));\
348         the_ins.reloc[the_ins.nrel].off=offs((exp));\
349         the_ins.reloc[the_ins.nrel].wid=width;\
350         the_ins.reloc[the_ins.nrel++].pcrel=pc_rel;\
351 }
352
353 #define add_frag(add,off,type)  {\
354         the_ins.fragb[the_ins.nfrag].fragoff=the_ins.numo;\
355         the_ins.fragb[the_ins.nfrag].fadd=add;\
356         the_ins.fragb[the_ins.nfrag].foff=off;\
357         the_ins.fragb[the_ins.nfrag++].fragty=type;\
358 }
359
360 #define isvar(exp)      ((exp) && (adds(exp) || subs(exp)))
361
362 #define seg(exp)        ((exp)->e_exp.X_seg)
363 #define adds(exp)       ((exp)->e_exp.X_add_symbol)
364 #define subs(exp)       ((exp)->e_exp.X_subtract_symbol)
365 #define offs(exp)       ((exp)->e_exp.X_add_number)
366
367
368 struct m68k_incant {
369         char *m_operands;
370         unsigned long m_opcode;
371         short m_opnum;
372         short m_codenum;
373         enum m68k_architecture m_arch;
374         struct m68k_incant *m_next;
375 };
376
377 #define getone(x)       ((((x)->m_opcode)>>16)&0xffff)
378 #define gettwo(x)       (((x)->m_opcode)&0xffff)
379
380
381 #ifdef __STDC__
382
383 static char *crack_operand(char *str, struct m68k_op *opP);
384 static int get_num(struct m68k_exp *exp, int ok);
385 static int get_regs(int i, char *str, struct m68k_op *opP);
386 static int reverse_16_bits(int in);
387 static int reverse_8_bits(int in);
388 static int try_index(char **s, struct m68k_op *opP);
389 static void install_gen_operand(int mode, int val);
390 static void install_operand(int mode, int val);
391 static void s_bss(void);
392 static void s_data1(void);
393 static void s_data2(void);
394 static void s_even(void);
395 static void s_proc(void);
396
397 #else /* __STDC__ */
398
399 static char *crack_operand();
400 static int get_num();
401 static int get_regs();
402 static int reverse_16_bits();
403 static int reverse_8_bits();
404 static int try_index();
405 static void install_gen_operand();
406 static void install_operand();
407 static void s_bss();
408 static void s_data1();
409 static void s_data2();
410 static void s_even();
411 static void s_proc();
412
413 #endif /* __STDC__ */
414
415 static enum m68k_architecture current_architecture = m68020
416 #ifndef NO_68881
417     | m68881
418 #endif
419 #ifndef NO_68851
420     | m68851
421 #endif
422     ;
423
424 /* BCC68000 is for patching in an extra jmp instruction for long offsets
425    on the 68000.  The 68000 doesn't support long branches with branchs */
426
427 /* This table desribes how you change sizes for the various types of variable
428    size expressions.  This version only supports two kinds. */
429
430 /* Note that calls to frag_var need to specify the maximum expansion needed */
431 /* This is currently 10 bytes for DBCC */
432
433 /* The fields are:
434         How far Forward this mode will reach:
435         How far Backward this mode will reach:
436         How many bytes this mode will add to the size of the frag
437         Which mode to go to if the offset won't fit in this one
438  */
439 const relax_typeS
440 md_relax_table[] = {
441 { 1,            1,              0,      0 },    /* First entries aren't used */
442 { 1,            1,              0,      0 },    /* For no good reason except */
443 { 1,            1,              0,      0 },    /* that the VAX doesn't either */
444 { 1,            1,              0,      0 },
445
446 { (127),        (-128),         0,      TAB(BRANCH,SHORT)},
447 { (32767),      (-32768),       2,      TAB(BRANCH,LONG) },
448 { 0,            0,              4,      0 },
449 { 1,            1,              0,      0 },
450
451 { 1,            1,              0,      0 },    /* FBRANCH doesn't come BYTE */
452 { (32767),      (-32768),       2,      TAB(FBRANCH,LONG)},
453 { 0,            0,              4,      0 },
454 { 1,            1,              0,      0 },
455
456 { 1,            1,              0,      0 },    /* PCREL doesn't come BYTE */
457 { (32767),      (-32768),       2,      TAB(PCREL,LONG)},
458 { 0,            0,              4,      0 },
459 { 1,            1,              0,      0 },
460
461 { (127),        (-128),         0,      TAB(BCC68000,SHORT)},
462 { (32767),      (-32768),       2,      TAB(BCC68000,LONG) },
463 { 0,            0,              6,      0 },    /* jmp long space */
464 { 1,            1,              0,      0 },
465
466 { 1,            1,              0,      0 },    /* DBCC doesn't come BYTE */
467 { (32767),      (-32768),       2,      TAB(DBCC,LONG) },
468 { 0,            0,              10,     0 },    /* bra/jmp long space */
469 { 1,            1,              0,      0 },
470
471 { 1,            1,              0,      0 },    /* PCLEA doesn't come BYTE */
472 { 32767,        -32768,         2,      TAB(PCLEA,LONG) },
473 { 0,            0,              6,      0 },
474 { 1,            1,              0,      0 },
475
476 };
477
478 /* These are the machine dependent pseudo-ops.  These are included so
479    the assembler can work on the output from the SUN C compiler, which
480    generates these.
481  */
482
483 /* This table describes all the machine specific pseudo-ops the assembler
484    has to support.  The fields are:
485           pseudo-op name without dot
486           function to call to execute this pseudo-op
487           Integer arg to pass to the function
488  */
489 const pseudo_typeS md_pseudo_table[] = {
490         { "data1",      s_data1,        0       },
491         { "data2",      s_data2,        0       },
492         { "bss",        s_bss,          0       },
493         { "even",       s_even,         0       },
494         { "skip",       s_space,        0       },
495         { "proc",       s_proc,         0       },
496         { 0,            0,              0       }
497 };
498
499
500 /* #define isbyte(x)    ((x)>=-128 && (x)<=127) */
501 /* #define isword(x)    ((x)>=-32768 && (x)<=32767) */
502
503 #define issbyte(x)      ((x)>=-128 && (x)<=127)
504 #define isubyte(x)      ((x)>=0 && (x)<=255)
505 #define issword(x)      ((x)>=-32768 && (x)<=32767)
506 #define isuword(x)      ((x)>=0 && (x)<=65535)
507
508 #define isbyte(x)       ((x)>=-128 && (x)<=255)
509 #define isword(x)       ((x)>=-32768 && (x)<=65535)
510 #define islong(x)       (1)
511
512 extern char *input_line_pointer;
513
514 enum {
515     FAIL = 0,
516     OK = 1,
517 };
518
519 /* JF these tables here are for speed at the expense of size */
520 /* You can replace them with the #if 0 versions if you really
521    need space and don't mind it running a bit slower */
522
523 static char mklower_table[256];
524 #define mklower(c) (mklower_table[(unsigned char)(c)])
525 static char notend_table[256];
526 static char alt_notend_table[256];
527 #define notend(s) ( !(notend_table[(unsigned char)(*s)] || (*s==':' &&\
528  alt_notend_table[(unsigned char)(s[1])])))
529
530 #if 0
531 #define mklower(c)      (isupper(c) ? tolower(c) : c)
532 #endif
533
534
535 /* JF modified this to handle cases where the first part of a symbol name
536    looks like a register */
537
538 /*
539  * m68k_reg_parse() := if it looks like a register, return it's token &
540  * advance the pointer.
541  */
542
543 enum _register m68k_reg_parse(ccp)
544 register char **ccp;
545 {
546 #ifndef MAX_REG_NAME_LEN
547 #define MAX_REG_NAME_LEN (6)
548 #endif /* MAX_REG_NAME_LEN */
549         register char c[MAX_REG_NAME_LEN];
550         char *p, *q;
551         register int n = 0,
552                 ret = FAIL;
553
554         c[0] = mklower(ccp[0][0]);
555 #ifdef REGISTER_PREFIX
556         if (c[0] != REGISTER_PREFIX) {
557                 return(FAIL);
558         } /* need prefix */
559 #endif
560
561         for (p = c, q = ccp[0]; p < c + MAX_REG_NAME_LEN && *q != 0; ++p, ++q) {
562                 *p = mklower(*q);
563         } /* downcase */
564
565         switch(c[0]) {
566         case 'a':
567                 if(c[1]>='0' && c[1]<='7') {
568                         n=2;
569                         ret=ADDR+c[1]-'0';
570                 }
571 #ifndef NO_68851
572                 else if (c[1] == 'c') {
573                         n = 2;
574                         ret = AC;
575                 }
576 #endif
577                 break;
578 #ifndef NO_68851
579         case 'b':
580                 if (c[1] == 'a') {
581                         if (c[2] == 'd') {
582                                 if (c[3] >= '0' && c[3] <= '7') {
583                                         n = 4;
584                                         ret = BAD + c[3] - '0';
585                                 }
586                         } /* BAD */
587                         if (c[2] == 'c') {
588                                 if (c[3] >= '0' && c[3] <= '7') {
589                                         n = 4;
590                                         ret = BAC + c[3] - '0';
591                                 }
592                         } /* BAC */
593                 } else if (c[1] == 'c') {
594                         n = 2;
595                         ret = BC;
596                 } /* BC */
597                 break;
598 #endif
599         case 'c':
600 #ifndef NO_68851
601                 if (c[1] == 'a' && c[2] == 'l') {
602                         n = 3;
603                         ret = CAL;
604                 } else
605 #endif
606                         /* This supports both CCR and CC as the ccr reg. */
607                 if(c[1]=='c' && c[2]=='r') {
608                         n=3;
609                         ret = CCR;
610                 } else if(c[1]=='c') {
611                         n=2;
612                         ret = CCR;
613                 } else if(c[1]=='a' && (c[2]=='a' || c[2]=='c') && c[3]=='r') {
614                         n=4;
615                         ret = c[2]=='a' ? CAAR : CACR;
616                 }
617 #ifndef NO_68851
618                 else if (c[1] == 'r' && c[2] == 'p') {
619                         n = 3;
620                         ret = (CRP);
621                 }
622 #endif
623                 break;
624         case 'd':
625                 if (c[1] >= '0' && c[1] <= '7') {
626                         n = 2;
627                         ret = DATA + c[1] - '0';
628                 } else if (c[1] == 'f' && c[2] == 'c') {
629                         n = 3;
630                         ret = DFC;
631                 } else if (c[1] == 'c') {
632                         n = 2;
633                 ret = DC;
634                 } else if (c[1] == 't' && c[2] == 't') {
635                         if ('0' <= c[3] && c[3] <= '1') {
636                                 n = 4;
637                                 ret = DTT0 + (c[3] - '0');
638                         } /* DTT[01] */
639                 }
640 #ifndef NO_68851
641                 else if (c[1] == 'r' && c[2] == 'p') {
642                         n = 3;
643                         ret = (DRP);
644                 }
645 #endif
646                 break;
647         case 'f':
648                 if(c[1]=='p') {
649                         if(c[2]>='0' && c[2]<='7') {
650                                 n=3;
651                                 ret = FPREG+c[2]-'0';
652                                 if(c[3]==':')
653                                         ccp[0][3]=',';
654                         } else if(c[2]=='i') {
655                                 n=3;
656                                 ret = FPI;
657                         } else if(c[2]=='s') {
658                                 n= (c[3] == 'r' ? 4 : 3);
659                                 ret = FPS;
660                         } else if(c[2]=='c') {
661                                 n= (c[3] == 'r' ? 4 : 3);
662                                 ret = FPC;
663                         }
664                 }
665                 break;
666         case 'i':
667                 if (c[1] == 's' && c[2] == 'p') {
668                         n = 3;
669                         ret = ISP;
670                 } else if (c[1] == 'c') {
671                         n = 2;
672                         ret = IC;
673                 } else if (c[1] == 't' && c[2] == 't') {
674                         if ('0' <= c[3] && c[3] <= '1') {
675                                 n = 4;
676                                 ret = ITT0 + (c[3] - '0');
677                         } /* ITT[01] */
678                 }
679                 break;
680         case 'm':
681                 if (c[1] == 's' && c[2] == 'p') {
682                         n = 3;
683                         ret = MSP;
684                 } else if (c[1] == 'm' && c[2] == 'u' && c[3] == 's' && c[4] == 'r') {
685                         n = 5;
686                         ret = MMUSR;
687                 }
688                 break;
689         case 'n':
690                 if (c[1] == 'c') {
691                         n = 2;
692                         ret = NC;
693                 }
694                 break;
695         case 'p':
696                 if(c[1]=='c') {
697 #ifndef NO_68851
698                         if(c[2] == 's' && c[3]=='r') {
699                                 n=4;
700                                 ret = (PCSR);
701                         } else
702 #endif
703                         {
704                                 n=2;
705                                 ret = PC;
706                         }
707                 }
708 #ifndef NO_68851
709                 else if (c[1] == 's' && c[2] == 'r') {
710                         n = 3;
711                         ret = (PSR);
712                 }
713 #endif
714                 break;
715         case 's':
716 #ifndef NO_68851
717                 if (c[1] == 'c' && c[2] == 'c') {
718                         n = 3;
719                         ret = (SCC);
720                 } else 
721 #endif
722                 if (c[1] == 'r') {
723                         if (c[2] == 'p') {
724                                 n = 3;
725                                 ret = SRP;
726                         } else {
727                                 n = 2;
728                                 ret = SR;
729                         } /* srp else sr */
730                 } else if (c[1] == 'p') {
731                         n = 2;
732                         ret = SP;
733                 } else if (c[1] == 'f' && c[2] == 'c') {
734                         n = 3;
735                         ret = SFC;
736                 }
737                 break;
738         case 't':
739                 if (c[1] == 'c') {
740                         n = 2;
741                         ret = TC;
742                 }
743                 break;
744         case 'u':
745                 if (c[1] == 's' && c[2] == 'p') {
746                         n=3;
747                         ret = USP;
748                 } else if (c[1] == 'r' && c[2] == 'p') {
749                         n = 3;
750                         ret = URP;
751                 }
752                 break;
753         case 'v':
754 #ifndef NO_68851
755                 if (c[1] == 'a' && c[2] == 'l') {
756                         n = 3;
757                         ret = (VAL);
758                 } else
759 #endif
760                 if(c[1]=='b' && c[2]=='r') {
761                         n=3;
762                         ret = VBR;
763                 }
764                 break;
765         case 'z':
766                 if(c[1]=='p' && c[2]=='c') {
767                         n=3;
768                         ret = ZPC;
769                 }
770                 break;
771         default:
772                 break;
773         }
774         if(n) {
775 #ifdef REGISTER_PREFIX
776                 n++;
777 #endif
778                 if(isalnum(ccp[0][n]) || ccp[0][n]=='_')
779                         ret=FAIL;
780                 else
781                         ccp[0]+=n;
782         } else
783                 ret = FAIL;
784         return ret;
785 }
786
787 #define SKIP_WHITE()    { str++; if(*str==' ') str++;}
788
789 /*
790  * m68k_ip_op := '#' + <anything>
791  *      | <register> + range_sep + get_regs
792  *      ;
793  * 
794  * range_sep := '/' | '-' ;
795  *
796  * SKIP_WHITE := <empty> | ' ' ;
797  *
798  */
799
800 int
801 m68k_ip_op(str,opP)
802 char *str;
803 register struct m68k_op *opP;
804 {
805         char    *strend;
806         long    i;
807         char    *parse_index();
808
809         if (*str==' ') {
810                 str++;
811         } /* Find the beginning of the string */
812
813         if(!*str) {
814                 opP->error="Missing operand";
815                 return FAIL;
816         } /* Out of gas */
817
818         for(strend = str; *strend; strend++) ;;
819
820         --strend;
821                 
822         if(*str=='#') {
823                 str++;
824                 opP->con1=add_exp(str,strend);
825                 opP->mode=IMMED;
826                 return OK;
827         } /* Guess what:  A constant.  Shar and enjoy */
828
829         i = m68k_reg_parse(&str);
830
831         /* is a register, is exactly a register, and is followed by '@' */
832
833         if((i==FAIL || *str!='\0') && *str!='@') {
834                 char *stmp;
835
836                 if(i!=FAIL && (*str=='/' || *str=='-')) {
837                         opP->mode=REGLST;
838                         return get_regs(i,str,opP);
839                 }
840                 if ((stmp=strchr(str,'@')) != '\0') {
841                         opP->con1=add_exp(str,stmp-1);
842                         if(stmp==strend) {
843                                 opP->mode=AINDX;
844                                 return OK;
845                         }
846                         stmp++;
847                         if(*stmp++!='(' || *strend--!=')') {
848                                 opP->error="Malformed operand";
849                                 return FAIL;
850                         }
851                         i=try_index(&stmp,opP);
852                         opP->con2=add_exp(stmp,strend);
853                         
854                         if (i == FAIL) {
855                                 opP->mode=AMIND;
856                                 if (max_arch_this_insn < m68020) {
857                                         max_arch_this_insn = m68020;
858                                 } /* bump arch */
859                         } else {
860                                 opP->mode=APODX;
861                                 if (max_arch_this_insn < m68020) {
862                                         max_arch_this_insn = m68020;
863                                 } /* bump arch */
864                         }
865                         return OK;
866                 } /* if there's an '@' */
867                 opP->mode=ABSL;
868                 opP->con1=add_exp(str,strend);
869                 return OK;
870         } /* not a register, not exactly a register, or no '@' */
871
872         opP->reg=i;
873
874         if (*str=='\0') {
875                 if(i>=DATA+0 && i<=DATA+7)
876                         opP->mode=DREG;
877                 else if(i>=ADDR+0 && i<=ADDR+7)
878                         opP->mode=AREG;
879                 else
880                         opP->mode=MSCR;
881                 return OK;
882         }
883
884         if((i<ADDR+0 || i>ADDR+7) && i!=PC && i!=ZPC && i!=FAIL) {      /* Can't indirect off non address regs */
885                 opP->error="Invalid indirect register";
886                 return FAIL;
887         }
888         know(*str == '@');
889
890         str++;
891         switch(*str) {
892         case '\0':
893                 opP->mode=AINDR;
894                 return OK;
895         case '-':
896                 opP->mode=ADEC;
897                 return OK;
898         case '+':
899                 opP->mode=AINC;
900                 return OK;
901         case '(':
902                 str++;
903                 break;
904         default:
905                 opP->error="Junk after indirect";
906                 return FAIL;
907         }
908                 /* Some kind of indexing involved.  Lets find out how bad it is */
909         i=try_index(&str,opP);
910                 /* Didn't start with an index reg, maybe its offset or offset,reg */
911         if(i==FAIL) {
912                 char *beg_str;
913
914                 beg_str=str;
915                 for(i=1;i;) {
916                         switch(*str++) {
917                         case '\0':
918                                 opP->error="Missing )";
919                                 return FAIL;
920                         case ',': i=0; break;
921                         case '(': i++; break;
922                         case ')': --i; break;
923                         }
924                 }
925                 /* if(str[-3]==':') {
926                         int siz;
927
928                         switch(str[-2]) {
929                         case 'b':
930                         case 'B':
931                                 siz=1;
932                                 break;
933                         case 'w':
934                         case 'W':
935                                 siz=2;
936                                 break;
937                         case 'l':
938                         case 'L':
939                                 siz=3;
940                                 break;
941                         default:
942                                 opP->error="Specified size isn't :w or :l";
943                                 return FAIL;
944                         }
945                         opP->con1=add_exp(beg_str,str-4);
946                         opP->con1->e_siz=siz;
947                 } else */
948                         opP->con1=add_exp(beg_str,str-2);
949                         /* Should be offset,reg */
950                 if(str[-1]==',') {
951                         i=try_index(&str,opP);
952                         if(i==FAIL) {
953                                 opP->error="Malformed index reg";
954                                 return FAIL;
955                         }
956                 }
957         }
958                 /* We've now got offset)   offset,reg)   or    reg) */
959
960         if(*str=='\0') {
961                 /* Th-the-thats all folks */
962                 if(opP->reg==FAIL) opP->mode=AINDX;     /* Other form of indirect */
963                 else if(opP->ireg==FAIL) opP->mode=AOFF;
964                 else opP->mode=AINDX;
965                 return OK;
966         }
967                 /* Next thing had better be another @ */
968         if(*str!='@' || str[1]!='(') {
969                 opP->error="junk after indirect";
970                 return FAIL;
971         }
972         str+=2;
973         if(opP->ireg!=FAIL) {
974                 opP->mode=APRDX;
975                 
976                 if (max_arch_this_insn < m68020) {
977                         max_arch_this_insn = m68020;
978                 } /* bump arch */
979                 
980                 i=try_index(&str,opP);
981                 if(i!=FAIL) {
982                         opP->error="Two index registers!  not allowed!";
983                         return FAIL;
984                 }
985         } else
986                 i=try_index(&str,opP);
987
988         if (i == FAIL) {
989                 char *beg_str;
990
991                 beg_str=str;
992                 for(i=1;i;) {
993                         switch(*str++) {
994                         case '\0':
995                                 opP->error="Missing )";
996                                 return FAIL;
997                         case ',': i=0; break;
998                         case '(': i++; break;
999                         case ')': --i; break;
1000                         }
1001                 }
1002                 opP->con2=add_exp(beg_str,str-2);
1003                 if(str[-1]==',') {
1004                         if(opP->ireg!=FAIL) {
1005                                 opP->error="Can't have two index regs";
1006                                 return FAIL;
1007                         }
1008                         i=try_index(&str,opP);
1009                         if(i==FAIL) {
1010                                 opP->error="malformed index reg";
1011                                 return FAIL;
1012                         }
1013                         opP->mode=APODX;
1014                         if (max_arch_this_insn < m68020) {
1015                                 max_arch_this_insn = m68020;
1016                         } /* bump arch */
1017                 } else if(opP->ireg!=FAIL) {
1018                         opP->mode=APRDX;
1019                         
1020                         if (max_arch_this_insn < m68020) {
1021                                 max_arch_this_insn = m68020;
1022                         } /* bump arch */
1023                 } else {
1024                         opP->mode=AMIND;
1025                         
1026                         if (max_arch_this_insn < m68020) {
1027                                 max_arch_this_insn = m68020;
1028                         } /* bump arch */
1029                 }
1030         } else {
1031                 opP->mode=APODX;
1032                 if (max_arch_this_insn < m68020) {
1033                         max_arch_this_insn = m68020;
1034                 } /* bump arch */
1035         }
1036
1037         if(*str!='\0') {
1038                 opP->error="Junk after indirect";
1039                 return FAIL;
1040         }
1041         return OK;
1042 }
1043
1044 /*
1045  * 
1046  * try_index := data_or_address_register + ')' + SKIP_W
1047  *      | data_or_address_register + ':' + SKIP_W + size_spec + SKIP_W + multiplier + ')' + SKIP_W
1048  *
1049  * multiplier := <empty>
1050  *      | ':' + multiplier_number
1051  *      ;
1052  *
1053  * multiplier_number := '1' | '2' | '4' | '8' ;
1054  *
1055  * size_spec := 'l' | 'L' | 'w' | 'W' ;
1056  *
1057  * SKIP_W := <empty> | ' ' ;
1058  *
1059  */
1060
1061 static int try_index(s,opP)
1062 char **s;
1063 struct m68k_op *opP;
1064 {
1065         register int    i;
1066         char    *ss;
1067 #define SKIP_W()        { ss++; if (*ss==' ') ss++;}
1068
1069         ss= *s;
1070         /* SKIP_W(); */
1071         i=m68k_reg_parse(&ss);
1072         if(!(i>=DATA+0 && i<=ADDR+7)) { /* if i is not DATA or ADDR reg */
1073                 *s=ss;
1074                 return FAIL;
1075         }
1076         opP->ireg=i;
1077         /* SKIP_W(); */
1078         if(*ss==')') {
1079                 opP->isiz=0;
1080                 opP->imul=1;
1081                 SKIP_W();
1082                 *s=ss;
1083                 return OK;
1084         }
1085         if(*ss!=':') {
1086                 opP->error="Missing : in index register";
1087                 *s=ss;
1088                 return FAIL;
1089         }
1090         SKIP_W();
1091         switch(*ss) {
1092         case 'w':
1093         case 'W':
1094                 opP->isiz=2;
1095                 break;
1096         case 'l':
1097         case 'L':
1098                 opP->isiz=3;
1099                 break;
1100         default:
1101                 opP->error="Index register size spec not :w or :l";
1102                 *s=ss;
1103                 return FAIL;
1104         }
1105         SKIP_W();
1106         if(*ss==':') {
1107                 SKIP_W();
1108                 switch(*ss) {
1109                 case '1':
1110                 case '2':
1111                 case '4':
1112                 case '8':
1113                         opP->imul= *ss-'0';
1114                         break;
1115                 default:
1116                         opP->error="index multiplier not 1, 2, 4 or 8";
1117                         *s=ss;
1118                         return FAIL;
1119                 }
1120                 SKIP_W();
1121         } else opP->imul=1;
1122         if(*ss!=')') {
1123                 opP->error="Missing )";
1124                 *s=ss;
1125                 return FAIL;
1126         }
1127         SKIP_W();
1128         *s=ss;
1129         return OK;
1130 } /* try_index() */
1131
1132 #ifdef TEST1    /* TEST1 tests m68k_ip_op(), which parses operands */
1133 main()
1134 {
1135         char buf[128];
1136         struct m68k_op thark;
1137
1138         for(;;) {
1139                 if(!gets(buf))
1140                         break;
1141                 bzero(&thark,sizeof(thark));
1142                 if(!m68k_ip_op(buf,&thark)) printf("FAIL:");
1143                 if(thark.error)
1144                         printf("op1 error %s in %s\n",thark.error,buf);
1145                 printf("mode %d, reg %d, ",thark.mode,thark.reg);
1146                 if(thark.b_const)
1147                         printf("Constant: '%.*s',",1+thark.e_const-thark.b_const,thark.b_const);
1148                 printf("ireg %d, isiz %d, imul %d ",thark.ireg,thark.isiz,thark.imul);
1149                 if(thark.b_iadd)
1150                         printf("Iadd: '%.*s'",1+thark.e_iadd-thark.b_iadd,thark.b_iadd);
1151                 printf("\n");
1152         }
1153         exit(0);
1154 }
1155
1156 #endif
1157
1158
1159 static struct hash_control*   op_hash = NULL;   /* handle of the OPCODE hash table
1160                                    NULL means any use before m68k_ip_begin()
1161                                    will crash */
1162
1163 \f
1164 /*
1165  *              m 6 8 k _ i p ( )
1166  *
1167  * This converts a string into a 68k instruction.
1168  * The string must be a bare single instruction in sun format
1169  * with RMS-style 68020 indirects
1170  *  (example:  )
1171  *
1172  * It provides some error messages: at most one fatal error message (which
1173  * stops the scan) and at most one warning message for each operand.
1174  * The 68k instruction is returned in exploded form, since we have no
1175  * knowledge of how you parse (or evaluate) your expressions.
1176  * We do however strip off and decode addressing modes and operation
1177  * mnemonic.
1178  *
1179  * This function's value is a string. If it is not "" then an internal
1180  * logic error was found: read this code to assign meaning to the string.
1181  * No argument string should generate such an error string:
1182  * it means a bug in our code, not in the user's text.
1183  *
1184  * You MUST have called m68k_ip_begin() once and m86_ip_end() never before using
1185  * this function.
1186  */
1187
1188 /* JF this function no longer returns a useful value.  Sorry */
1189 void m68k_ip (instring)
1190 char *instring;
1191 {
1192         register char *p;
1193         register struct m68k_op *opP;
1194         register struct m68k_incant *opcode;
1195         register char *s;
1196         register int tmpreg = 0,
1197         baseo = 0,
1198         outro = 0,
1199         nextword;
1200         int     siz1,
1201         siz2;
1202         char    c;
1203         int     losing;
1204         int     opsfound;
1205         char    *crack_operand();
1206         LITTLENUM_TYPE words[6];
1207         LITTLENUM_TYPE *wordp;
1208         
1209         max_arch_this_insn = m68000;
1210         
1211         if (*instring == ' ')
1212             instring++;                 /* skip leading whitespace */
1213         
1214         /* Scan up to end of operation-code, which MUST end in end-of-string
1215            or exactly 1 space. */
1216         for (p = instring; *p != '\0'; p++)
1217             if (*p == ' ')
1218                 break;
1219         
1220         
1221         if (p == instring) {
1222                 the_ins.error = "No operator";
1223                 the_ins.opcode[0] = NULL;
1224                 /* the_ins.numo=1; */
1225                 return;
1226         }
1227         
1228         /* p now points to the end of the opcode name, probably whitespace.
1229            make sure the name is null terminated by clobbering the whitespace,
1230            look it up in the hash table, then fix it back. */   
1231         c = *p;
1232         *p = '\0';
1233         opcode = (struct m68k_incant *)hash_find (op_hash, instring);
1234         *p = c;
1235         
1236         if (opcode == NULL) {
1237                 the_ins.error = "Unknown operator";
1238                 the_ins.opcode[0] = NULL;
1239                 /* the_ins.numo=1; */
1240                 return;
1241         }
1242         
1243         /* found a legitimate opcode, start matching operands */
1244         while (*p == ' ') ++p;
1245         
1246         for(opP = &the_ins.operands[0]; *p; opP++) {
1247                 
1248                 p = crack_operand(p, opP);
1249                 
1250                 if (opP->error) {
1251                         the_ins.error=opP->error;
1252                         return;
1253                 }
1254         }
1255         
1256         opsfound=opP- &the_ins.operands[0];
1257         
1258         /* This ugly hack is to support the floating pt opcodes in their standard form */
1259         /* Essentially, we fake a first enty of type COP#1 */
1260         if (opcode->m_operands[0]=='I') {
1261                 int     n;
1262                 
1263                 for(n=opsfound;n>0;--n)
1264                     the_ins.operands[n]=the_ins.operands[n-1];
1265                 
1266                 /* bcopy((char *)(&the_ins.operands[0]),(char *)(&the_ins.operands[1]),opsfound*sizeof(the_ins.operands[0])); */
1267                 bzero((char *)(&the_ins.operands[0]),sizeof(the_ins.operands[0]));
1268                 the_ins.operands[0].mode=MSCR;
1269                 the_ins.operands[0].reg=COPNUM;         /* COP #1 */
1270                 opsfound++;
1271         }
1272         
1273         /* We've got the operands.  Find an opcode that'll accept them */
1274         for (losing = 0; ; ) {
1275                 /* if we didn't get the right number of ops, or either
1276                    the modes of our args or this op line itself are out
1277                    of order... */
1278                 
1279                 if ((opsfound != opcode->m_opnum)
1280                     || ((max_arch_this_insn > current_architecture)
1281                         || (opcode->m_arch > current_architecture))) {
1282                         ++losing;
1283                         
1284                 } else {
1285                         for (s=opcode->m_operands, opP = &the_ins.operands[0]; *s && !losing; s += 2, opP++) {
1286                                 /* Warning: this switch is huge! */
1287                                 /* I've tried to organize the cases into  this order:
1288                                    non-alpha first, then alpha by letter.  lower-case goes directly
1289                                    before uppercase counterpart. */
1290                                 /* Code with multiple case ...: gets sorted by the lowest case ...
1291                                    it belongs to.  I hope this makes sense. */
1292                                 switch(*s) {
1293                                 case '!':
1294                                         if(opP->mode==MSCR || opP->mode==IMMED ||
1295                                            opP->mode==DREG || opP->mode==AREG || opP->mode==AINC || opP->mode==ADEC || opP->mode==REGLST)
1296                                             losing++;
1297                                         break;
1298                                         
1299                                 case '#':
1300                                         if(opP->mode!=IMMED)
1301                                             losing++;
1302                                         else {
1303                                                 long t;
1304                                                 
1305                                                 t=get_num(opP->con1,80);
1306                                                 if(s[1]=='b' && !isbyte(t))
1307                                                     losing++;
1308                                                 else if(s[1]=='w' && !isword(t))
1309                                                     losing++;
1310                                         }
1311                                         break;
1312                                         
1313                                 case '^':
1314                                 case 'T':
1315                                         if(opP->mode!=IMMED)
1316                                             losing++;
1317                                         break;
1318                                         
1319                                 case '$':
1320                                         if(opP->mode==MSCR || opP->mode==AREG ||
1321                                            opP->mode==IMMED || opP->reg==PC || opP->reg==ZPC || opP->mode==REGLST)
1322                                             losing++;
1323                                         break;
1324                                         
1325                                 case '%':
1326                                         if(opP->mode==MSCR || opP->reg==PC ||
1327                                            opP->reg==ZPC || opP->mode==REGLST)
1328                                             losing++;
1329                                         break;
1330                                         
1331                                         
1332                                 case '&':
1333                                         if(opP->mode==MSCR || opP->mode==DREG ||
1334                                            opP->mode==AREG || opP->mode==IMMED || opP->reg==PC || opP->reg==ZPC ||
1335                                            opP->mode==AINC || opP->mode==ADEC || opP->mode==REGLST)
1336                                             losing++;
1337                                         break;
1338                                         
1339                                 case '*':
1340                                         if(opP->mode==MSCR || opP->mode==REGLST)
1341                                             losing++;
1342                                         break;
1343                                         
1344                                 case '+':
1345                                         if(opP->mode!=AINC)
1346                                             losing++;
1347                                         break;
1348                                         
1349                                 case '-':
1350                                         if(opP->mode!=ADEC)
1351                                             losing++;
1352                                         break;
1353                                         
1354                                 case '/':
1355                                         if(opP->mode==MSCR || opP->mode==AREG ||
1356                                            opP->mode==AINC || opP->mode==ADEC || opP->mode==IMMED || opP->mode==REGLST)
1357                                             losing++;
1358                                         break;
1359                                         
1360                                 case ';':
1361                                         if(opP->mode==MSCR || opP->mode==AREG || opP->mode==REGLST)
1362                                             losing++;
1363                                         break;
1364                                         
1365                                 case '?':
1366                                         if(opP->mode==MSCR || opP->mode==AREG ||
1367                                            opP->mode==AINC || opP->mode==ADEC || opP->mode==IMMED || opP->reg==PC ||
1368                                            opP->reg==ZPC || opP->mode==REGLST)
1369                                             losing++;
1370                                         break;
1371                                         
1372                                 case '@':
1373                                         if(opP->mode==MSCR || opP->mode==AREG ||
1374                                            opP->mode==IMMED || opP->mode==REGLST)
1375                                             losing++;
1376                                         break;
1377                                         
1378                                 case '~':               /* For now! (JF FOO is this right?) */
1379                                         if(opP->mode==MSCR || opP->mode==DREG ||
1380                                            opP->mode==AREG || opP->mode==IMMED || opP->reg==PC || opP->reg==ZPC || opP->mode==REGLST)
1381                                             losing++;
1382                                         break;
1383                                         
1384                                 case 'A':
1385                                         if(opP->mode!=AREG)
1386                                             losing++;
1387                                         break;
1388                                 case 'a':
1389                                         if (opP->mode != AINDR) {
1390                                                 ++losing;
1391                                         } /* if not address register indirect */
1392                                         break;
1393                                 case 'B':       /* FOO */
1394                                         if(opP->mode!=ABSL || (flagseen['S'] && instring[0] == 'j'
1395                                                                && instring[1] == 'b'
1396                                                                && instring[2] == 's'
1397                                                                && instring[3] == 'r'))
1398                                             losing++;
1399                                         break;
1400                                         
1401                                 case 'C':
1402                                         if(opP->mode!=MSCR || opP->reg!=CCR)
1403                                             losing++;
1404                                         break;
1405                                         
1406                                 case 'd':       /* FOO This mode is a KLUDGE!! */
1407                                         if(opP->mode!=AOFF && (opP->mode!=ABSL ||
1408                                                                opP->con1->e_beg[0]!='(' || opP->con1->e_end[0]!=')'))
1409                                             losing++;
1410                                         break;
1411                                         
1412                                 case 'D':
1413                                         if(opP->mode!=DREG)
1414                                             losing++;
1415                                         break;
1416                                         
1417                                 case 'F':
1418                                         if(opP->mode!=MSCR || opP->reg<(FPREG+0) || opP->reg>(FPREG+7))
1419                                             losing++;
1420                                         break;
1421                                         
1422                                 case 'I':
1423                                         if(opP->mode!=MSCR || opP->reg<COPNUM ||
1424                                            opP->reg>=COPNUM+7)
1425                                             losing++;
1426                                         break;
1427                                         
1428                                 case 'J':
1429                                         if (opP->mode != MSCR
1430                                             || opP->reg < USP
1431                                             || opP->reg > URP
1432                                             || (current_architecture & m68000up) < m68010 /* before 68010 had none */
1433                                             || ((current_architecture & m68020up) == 0
1434                                                 && opP->reg != SFC
1435                                                 && opP->reg != DFC
1436                                                 && opP->reg != USP
1437                                                 && opP->reg != VBR) /* 68010's had only these */
1438                                             || ((current_architecture & m68040) == 0
1439                                                 && opP->reg != SFC
1440                                                 && opP->reg != DFC
1441                                                 && opP->reg != USP
1442                                                 && opP->reg != VBR
1443                                                 && opP->reg != CACR
1444                                                 && opP->reg != CAAR
1445                                                 && opP->reg != MSP
1446                                                 && opP->reg != ISP) /* 680[23]0's have only these */
1447                                             || ((current_architecture & m68040) /* 68040 has all but this */
1448                                                 && opP->reg == CAAR)) {
1449                                                 losing++;
1450                                         } /* doesn't cut it */
1451                                         break;
1452                                         
1453                                 case 'k':
1454                                         if(opP->mode!=IMMED)
1455                                             losing++;
1456                                         break;
1457                                         
1458                                 case 'l':
1459                                 case 'L':
1460                                         if(opP->mode==DREG || opP->mode==AREG || opP->mode==FPREG) {
1461                                                 if(s[1]=='8')
1462                                                     losing++;
1463                                                 else {
1464                                                         opP->mode=REGLST;
1465                                                         opP->reg=1<<(opP->reg-DATA);
1466                                                 }
1467                                         } else if(opP->mode!=REGLST) {
1468                                                 losing++;
1469                                         } else if(s[1]=='8' && opP->reg&0x0FFffFF)
1470                                             losing++;
1471                                         else if(s[1]=='3' && opP->reg&0x7000000)
1472                                             losing++;
1473                                         break;
1474                                         
1475                                 case 'M':
1476                                         if(opP->mode!=IMMED)
1477                                             losing++;
1478                                         else {
1479                                                 long t;
1480                                                 
1481                                                 t=get_num(opP->con1,80);
1482                                                 if(!issbyte(t) || isvar(opP->con1))
1483                                                     losing++;
1484                                         }
1485                                         break;
1486                                         
1487                                 case 'O':
1488                                         if(opP->mode!=DREG && opP->mode!=IMMED)
1489                                             losing++;
1490                                         break;
1491                                         
1492                                 case 'Q':
1493                                         if(opP->mode!=IMMED)
1494                                             losing++;
1495                                         else {
1496                                                 long t;
1497                                                 
1498                                                 t=get_num(opP->con1,80);
1499                                                 if(t<1 || t>8 || isvar(opP->con1))
1500                                                     losing++;
1501                                         }
1502                                         break;
1503                                         
1504                                 case 'R':
1505                                         if(opP->mode!=DREG && opP->mode!=AREG)
1506                                             losing++;
1507                                         break;
1508                                         
1509                                 case 's':
1510                                         if(opP->mode!=MSCR || !(opP->reg==FPI || opP->reg==FPS || opP->reg==FPC))
1511                                             losing++;
1512                                         break;
1513                                         
1514                                 case 'S':
1515                                         if(opP->mode!=MSCR || opP->reg!=SR)
1516                                             losing++;
1517                                         break;
1518                                         
1519                                 case 'U':
1520                                         if(opP->mode!=MSCR || opP->reg!=USP)
1521                                             losing++;
1522                                         break;
1523                                         
1524                                         /* JF these are out of order.  We could put them
1525                                            in order if we were willing to put up with
1526                                            bunches of #ifdef m68851s in the code */
1527 #ifndef NO_68851
1528                                         /* Memory addressing mode used by pflushr */
1529                                 case '|':
1530                                         if(opP->mode==MSCR || opP->mode==DREG ||
1531                                            opP->mode==AREG || opP->mode==REGLST)
1532                                             losing++;
1533                                         break;
1534                                         
1535                                 case 'f':
1536                                         if (opP->mode != MSCR || (opP->reg != SFC && opP->reg != DFC))
1537                                             losing++;
1538                                         break;
1539                                         
1540                                 case 'P':
1541                                         if (opP->mode != MSCR || (opP->reg != TC && opP->reg != CAL &&
1542                                                                   opP->reg != VAL && opP->reg != SCC && opP->reg != AC))
1543                                             losing++;
1544                                         break;
1545                                         
1546                                 case 'V':
1547                                         if (opP->reg != VAL)
1548                                             losing++;
1549                                         break;
1550                                         
1551                                 case 'W':
1552                                         if (opP->mode != MSCR || (opP->reg != DRP && opP->reg != SRP &&
1553                                                                   opP->reg != CRP))
1554                                             losing++;
1555                                         break;
1556                                         
1557                                 case 'X':
1558                                         if (opP->mode != MSCR ||
1559                                             (!(opP->reg >= BAD && opP->reg <= BAD+7) &&
1560                                              !(opP->reg >= BAC && opP->reg <= BAC+7)))
1561                                             losing++;
1562                                         break;
1563                                         
1564                                 case 'Y':
1565                                         if (opP->reg != PSR)
1566                                             losing++;
1567                                         break;
1568                                         
1569                                 case 'Z':
1570                                         if (opP->reg != PCSR)
1571                                             losing++;
1572                                         break;
1573 #endif
1574                                 case 'c':
1575                                         if (opP->reg != NC
1576                                             && opP->reg != IC
1577                                             && opP->reg != DC
1578                                             && opP->reg != BC) {
1579                                                 losing++;
1580                                         } /* not a cache specifier. */
1581                                         break;
1582
1583                                 case '_':
1584                                         if (opP->mode != ABSL) {
1585                                                 ++losing;
1586                                         } /* not absolute */
1587                                         break;
1588
1589                                 default:
1590                                         as_fatal("Internal error:  Operand mode %c unknown in line %s of file \"%s\"",
1591                                                  *s, __LINE__, __FILE__);
1592                                 } /* switch on type of operand */
1593                                 
1594                                 if (losing) break;
1595                         } /* for each operand */
1596                 } /* if immediately wrong */
1597                 
1598                 if (!losing) {
1599                         break;
1600                 } /* got it. */
1601                 
1602                 opcode = opcode->m_next;
1603                 
1604                 if (!opcode) {
1605                         the_ins.error = "instruction/operands mismatch";
1606                         return;
1607                 } /* Fell off the end */
1608                 
1609                 losing = 0;
1610         }
1611         
1612         
1613         the_ins.args=opcode->m_operands;
1614         the_ins.numargs=opcode->m_opnum;
1615         the_ins.numo=opcode->m_codenum;
1616         the_ins.opcode[0]=getone(opcode);
1617         the_ins.opcode[1]=gettwo(opcode);
1618         
1619         for (s = the_ins.args, opP = &the_ins.operands[0]; *s; s += 2, opP++) {
1620                 /* This switch is a doozy.
1621                    Watch the first step; its a big one! */
1622                 switch(s[0]) {
1623                         
1624                 case '*':
1625                 case '~':
1626                 case '%':
1627                 case ';':
1628                 case '@':
1629                 case '!':
1630                 case '&':
1631                 case '$':
1632                 case '?':
1633                 case '/':
1634 #ifndef NO_68851
1635                 case '|':
1636 #endif
1637                         switch(opP->mode) {
1638                         case IMMED:
1639                                 tmpreg=0x3c;    /* 7.4 */
1640                                 if (strchr("bwl",s[1])) nextword=get_num(opP->con1,80);
1641                                 else nextword=nextword=get_num(opP->con1,0);
1642                                 if(isvar(opP->con1))
1643                                     add_fix(s[1],opP->con1,0);
1644                                 switch(s[1]) {
1645                                 case 'b':
1646                                         if(!isbyte(nextword))
1647                                             opP->error="operand out of range";
1648                                         addword(nextword);
1649                                         baseo=0;
1650                                         break;
1651                                 case 'w':
1652                                         if(!isword(nextword))
1653                                             opP->error="operand out of range";
1654                                         addword(nextword);
1655                                         baseo=0;
1656                                         break;
1657                                 case 'l':
1658                                         addword(nextword>>16);
1659                                         addword(nextword);
1660                                         baseo=0;
1661                                         break;
1662                                         
1663                                 case 'f':
1664                                         baseo=2;
1665                                         outro=8;
1666                                         break;
1667                                 case 'F':
1668                                         baseo=4;
1669                                         outro=11;
1670                                         break;
1671                                 case 'x':
1672                                         baseo=6;
1673                                         outro=15;
1674                                         break;
1675                                 case 'p':
1676                                         baseo=6;
1677                                         outro= -1;
1678                                         break;
1679                                 default:
1680                                         as_fatal("Internal error:  Can't decode %c%c in line %s of file \"%s\"",
1681                                                  *s, s[1], __LINE__, __FILE__);
1682                                 }
1683                                 if(!baseo)
1684                                     break;
1685                                 
1686                                 /* We gotta put out some float */
1687                                 if(seg(opP->con1)!=SEG_BIG) {
1688                                         int_to_gen(nextword);
1689                                         gen_to_words(words,baseo,(long int)outro);
1690                                         for(wordp=words;baseo--;wordp++)
1691                                             addword(*wordp);
1692                                         break;
1693                                 }               /* Its BIG */
1694                                 if(offs(opP->con1)>0) {
1695                                         as_warn("Bignum assumed to be binary bit-pattern");
1696                                         if(offs(opP->con1)>baseo) {
1697                                                 as_warn("Bignum too big for %c format; truncated",s[1]);
1698                                                 offs(opP->con1)=baseo;
1699                                         }
1700                                         baseo-=offs(opP->con1);
1701                                         for(wordp=generic_bignum+offs(opP->con1)-1;offs(opP->con1)--;--wordp)
1702                                             addword(*wordp);
1703                                         while(baseo--)
1704                                             addword(0);
1705                                         break;
1706                                 }
1707                                 gen_to_words(words,baseo,(long)outro);
1708                                 for (wordp=words;baseo--;wordp++)
1709                                     addword(*wordp);
1710                                 break;
1711                         case DREG:
1712                                 tmpreg=opP->reg-DATA; /* 0.dreg */
1713                                 break;
1714                         case AREG:
1715                                 tmpreg=0x08+opP->reg-ADDR; /* 1.areg */
1716                                 break;
1717                         case AINDR:
1718                                 tmpreg=0x10+opP->reg-ADDR; /* 2.areg */
1719                                 break;
1720                         case ADEC:
1721                                 tmpreg=0x20+opP->reg-ADDR; /* 4.areg */
1722                                 break;
1723                         case AINC:
1724                                 tmpreg=0x18+opP->reg-ADDR; /* 3.areg */
1725                                 break;
1726                         case AOFF:
1727                                 
1728                                 nextword=get_num(opP->con1,80);
1729                                 /* Force into index mode.  Hope this works */
1730                                 
1731                                 /* We do the first bit for 32-bit displacements,
1732                                    and the second bit for 16 bit ones.  It is
1733                                    possible that we should make the default be
1734                                    WORD instead of LONG, but I think that'd
1735                                    break GCC, so we put up with a little
1736                                    inefficiency for the sake of working output.
1737                                    */
1738                                 
1739                                 if(   !issword(nextword)
1740                                    || (   isvar(opP->con1)
1741                                        && (  (   opP->con1->e_siz==0
1742                                               && flagseen['l']==0)
1743                                            || opP->con1->e_siz==3))) {
1744                                         
1745                                         if(opP->reg==PC)
1746                                             tmpreg=0x3B;        /* 7.3 */
1747                                         else
1748                                             tmpreg=0x30+opP->reg-ADDR;  /* 6.areg */
1749                                         if(isvar(opP->con1)) {
1750                                                 if(opP->reg==PC) {
1751                                                         add_frag(adds(opP->con1),
1752                                                                  offs(opP->con1),
1753                                                                  TAB(PCLEA,SZ_UNDEF));
1754                                                         break;
1755                                                 } else {
1756                                                         addword(0x0170);
1757                                                         add_fix('l',opP->con1,1);
1758                                                 }
1759                                         } else
1760                                             addword(0x0170);
1761                                         addword(nextword>>16);
1762                                 } else {
1763                                         if(opP->reg==PC)
1764                                             tmpreg=0x3A; /* 7.2 */
1765                                         else
1766                                             tmpreg=0x28+opP->reg-ADDR; /* 5.areg */
1767                                         
1768                                         if(isvar(opP->con1)) {
1769                                                 if(opP->reg==PC) {
1770                                                         add_fix('w',opP->con1,1);
1771                                                 } else
1772                                                     add_fix('w',opP->con1,0);
1773                                         }
1774                                 }
1775                                 addword(nextword);
1776                                 break;
1777                                 
1778                         case APODX:
1779                         case AMIND:
1780                         case APRDX:
1781                                 if (max_arch_this_insn < m68020) {
1782                                         max_arch_this_insn = m68020;
1783                                 } /* bump arch */
1784                                 /* intentional fall-through */
1785                         case AINDX:
1786                                 nextword=0;
1787                                 baseo=get_num(opP->con1,80);
1788                                 outro=get_num(opP->con2,80);
1789                                 /* Figure out the 'addressing mode' */
1790                                 /* Also turn on the BASE_DISABLE bit, if needed */
1791                                 if(opP->reg==PC || opP->reg==ZPC) {
1792                                         tmpreg=0x3b; /* 7.3 */
1793                                         if(opP->reg==ZPC)
1794                                             nextword|=0x80;
1795                                 } else if(opP->reg==FAIL) {
1796                                         nextword|=0x80;
1797                                         tmpreg=0x30;    /* 6.garbage */
1798                                 } else tmpreg=0x30+opP->reg-ADDR; /* 6.areg */
1799                                 
1800                                 siz1= (opP->con1) ? opP->con1->e_siz : 0;
1801                                 siz2= (opP->con2) ? opP->con2->e_siz : 0;
1802                                 
1803                                 /* Index register stuff */
1804                                 if(opP->ireg>=DATA+0 && opP->ireg<=ADDR+7) {
1805                                         nextword|=(opP->ireg-DATA)<<12;
1806                                         
1807                                         if(opP->isiz==0 || opP->isiz==3)
1808                                             nextword|=0x800;
1809                                         switch(opP->imul) {
1810                                         case 1: break;
1811                                         case 2: nextword|=0x200; break;
1812                                         case 4: nextword|=0x400; break;
1813                                         case 8: nextword|=0x600; break;
1814                                         default: as_fatal("failed sanity check.");
1815                                         }
1816                                         /* IF its simple,
1817                                            GET US OUT OF HERE! */
1818                                         
1819                                         /* Must be INDEX, with an index
1820                                            register.  Address register
1821                                            cannot be ZERO-PC, and either
1822                                            :b was forced, or we know
1823                                            it will fit */
1824                                         if(   opP->mode==AINDX
1825                                            && opP->reg!=FAIL
1826                                            && opP->reg!=ZPC
1827                                            && (   siz1==1
1828                                                || (   issbyte(baseo)
1829                                                    && !isvar(opP->con1)))) {
1830                                                 nextword +=baseo&0xff;
1831                                                 addword(nextword);
1832                                                 if(isvar(opP->con1))
1833                                                     add_fix('B',opP->con1,0);
1834                                                 break;
1835                                         }
1836                                 } else
1837                                     nextword|=0x40;     /* No index reg */
1838                                 
1839                                 /* It aint simple */
1840                                 nextword|=0x100;
1841                                 /* If the guy specified a width, we assume that
1842                                    it is wide enough.  Maybe it isn't.  Ifso, we lose
1843                                    */
1844                                 switch(siz1) {
1845                                 case 0:
1846                                         if(isvar(opP->con1) || !issword(baseo)) {
1847                                                 siz1=3;
1848                                                 nextword|=0x30;
1849                                         } else if(baseo==0)
1850                                             nextword|=0x10;
1851                                         else {  
1852                                                 nextword|=0x20;
1853                                                 siz1=2;
1854                                         }
1855                                         break;
1856                                 case 1:
1857                                         as_warn("Byte dispacement won't work.  Defaulting to :w");
1858                                 case 2:
1859                                         nextword|=0x20;
1860                                         break;
1861                                 case 3:
1862                                         nextword|=0x30;
1863                                         break;
1864                                 }
1865                                 
1866                                 /* Figure out innner displacement stuff */
1867                                 if(opP->mode!=AINDX) {
1868                                         switch(siz2) {
1869                                         case 0:
1870                                                 if(isvar(opP->con2) || !issword(outro)) {
1871                                                         siz2=3;
1872                                                         nextword|=0x3;
1873                                                 } else if(outro==0)
1874                                                     nextword|=0x1;
1875                                                 else {  
1876                                                         nextword|=0x2;
1877                                                         siz2=2;
1878                                                 }
1879                                                 break;
1880                                         case 1:
1881                                                 as_warn("Byte dispacement won't work.  Defaulting to :w");
1882                                         case 2:
1883                                                 nextword|=0x2;
1884                                                 break;
1885                                         case 3:
1886                                                 nextword|=0x3;
1887                                                 break;
1888                                         }
1889                                         if(opP->mode==APODX) nextword|=0x04;
1890                                         else if(opP->mode==AMIND) nextword|=0x40;
1891                                 }
1892                                 addword(nextword);
1893                                 
1894                                 if(isvar(opP->con1)) {
1895                                         if(opP->reg==PC || opP->reg==ZPC) {
1896                                                 add_fix(siz1==3 ? 'l' : 'w',opP->con1,1);
1897                                                 opP->con1->e_exp.X_add_number+=6;
1898                                         } else
1899                                             add_fix(siz1==3 ? 'l' : 'w',opP->con1,0);
1900                                 }
1901                                 if(siz1==3)
1902                                     addword(baseo>>16);
1903                                 if(siz1)
1904                                     addword(baseo);
1905                                 
1906                                 if(isvar(opP->con2)) {
1907                                         if(opP->reg==PC || opP->reg==ZPC) {
1908                                                 add_fix(siz2==3 ? 'l' : 'w',opP->con2,1);
1909                                                 opP->con1->e_exp.X_add_number+=6;
1910                                         } else
1911                                             add_fix(siz2==3 ? 'l' : 'w',opP->con2,0);
1912                                 }
1913                                 if(siz2==3)
1914                                     addword(outro>>16);
1915                                 if(siz2)
1916                                     addword(outro);
1917                                 
1918                                 break;
1919                                 
1920                         case ABSL:
1921                                 nextword=get_num(opP->con1,80);
1922                                 switch(opP->con1->e_siz) {
1923                                 default:
1924                                         as_warn("Unknown size for absolute reference");
1925                                 case 0:
1926                                         if(!isvar(opP->con1) && issword(offs(opP->con1))) {
1927                                                 tmpreg=0x38; /* 7.0 */
1928                                                 addword(nextword);
1929                                                 break;
1930                                         }
1931                                         /* Don't generate pc relative code
1932                                            on 68010 and 68000 */
1933                                         if(isvar(opP->con1)
1934                                            && !subs(opP->con1)
1935                                            && seg(opP->con1) == SEG_TEXT
1936                                            && now_seg == SEG_TEXT
1937                                            && (current_architecture & m68000up) <= m68010
1938                                            && !flagseen['S']
1939                                            && !strchr("~%&$?", s[0])) {
1940                                                 tmpreg=0x3A; /* 7.2 */
1941                                                 add_frag(adds(opP->con1),
1942                                                          offs(opP->con1),
1943                                                          TAB(PCREL,SZ_UNDEF));
1944                                                 break;
1945                                         }
1946                                 case 3:         /* Fall through into long */
1947                                         if(isvar(opP->con1))
1948                                             add_fix('l',opP->con1,0);
1949                                         
1950                                         tmpreg=0x39;    /* 7.1 mode */
1951                                         addword(nextword>>16);
1952                                         addword(nextword);
1953                                         break;
1954                                         
1955                                 case 2:         /* Word */
1956                                         if(isvar(opP->con1))
1957                                             add_fix('w',opP->con1,0);
1958                                         
1959                                         tmpreg=0x38;    /* 7.0 mode */
1960                                         addword(nextword);
1961                                         break;
1962                                 }
1963                                 break;
1964                         case MSCR:
1965                         default:
1966                                 as_bad("unknown/incorrect operand");
1967                                 /* abort(); */
1968                         }
1969                         install_gen_operand(s[1],tmpreg);
1970                         break;
1971                         
1972                 case '#':
1973                 case '^':
1974                         switch(s[1]) {  /* JF: I hate floating point! */
1975                         case 'j':
1976                                 tmpreg=70;
1977                                 break;
1978                         case '8':
1979                                 tmpreg=20;
1980                                 break;
1981                         case 'C':
1982                                 tmpreg=50;
1983                                 break;
1984                         case '3':
1985                         default:
1986                                 tmpreg=80;
1987                                 break;
1988                         }
1989                         tmpreg=get_num(opP->con1,tmpreg);
1990                         if(isvar(opP->con1))
1991                             add_fix(s[1],opP->con1,0);
1992                         switch(s[1]) {
1993                         case 'b':       /* Danger:  These do no check for
1994                                            certain types of overflow.
1995                                            user beware! */
1996                                 if(!isbyte(tmpreg))
1997                                     opP->error="out of range";
1998                                 insop(tmpreg);
1999                                 if(isvar(opP->con1))
2000                                     the_ins.reloc[the_ins.nrel-1].n=(opcode->m_codenum)*2;
2001                                 break;
2002                         case 'w':
2003                                 if(!isword(tmpreg))
2004                                     opP->error="out of range";
2005                                 insop(tmpreg);
2006                                 if(isvar(opP->con1))
2007                                     the_ins.reloc[the_ins.nrel-1].n=(opcode->m_codenum)*2;
2008                                 break;
2009                         case 'l':
2010                                 insop(tmpreg);          /* Because of the way insop works, we put these two out backwards */
2011                                 insop(tmpreg>>16);
2012                                 if(isvar(opP->con1))
2013                                     the_ins.reloc[the_ins.nrel-1].n=(opcode->m_codenum)*2;
2014                                 break;
2015                         case '3':
2016                                 tmpreg&=0xFF;
2017                         case '8':
2018                         case 'C':
2019                                 install_operand(s[1],tmpreg);
2020                                 break;
2021                         default:
2022                                 as_fatal("Internal error:  Unknown mode #%c in line %s of file \"%s\"", s[1], __LINE__, __FILE__);
2023                         }
2024                         break;
2025                         
2026                 case '+':
2027                 case '-':
2028                 case 'A':
2029                 case 'a':
2030                         install_operand(s[1],opP->reg-ADDR);
2031                         break;
2032                         
2033                 case 'B':
2034                         tmpreg=get_num(opP->con1,80);
2035                         switch(s[1]) {
2036                         case 'B':
2037                                 /* Needs no offsetting */
2038                                 add_fix('B',opP->con1,1);
2039                                 break;
2040                         case 'W':
2041                                 /* Offset the displacement to be relative to byte disp location */
2042                                 opP->con1->e_exp.X_add_number+=2;
2043                                 add_fix('w',opP->con1,1);
2044                                 addword(0);
2045                                 break;
2046                         case 'L':
2047                         long_branch:
2048                                 if(current_architecture <= m68010)      /* 68000 or 010 */
2049                                     as_warn("Can't use long branches on 68000/68010");
2050                                 the_ins.opcode[the_ins.numo-1]|=0xff;
2051                                 /* Offset the displacement to be relative to byte disp location */
2052                                 opP->con1->e_exp.X_add_number+=4;
2053                                 add_fix('l',opP->con1,1);
2054                                 addword(0);
2055                                 addword(0);
2056                                 break;
2057                         case 'g':
2058                                 if(subs(opP->con1))      /* We can't relax it */
2059                                     goto long_branch;
2060                                 
2061                                 /* This could either be a symbol, or an
2062                                    absolute address.  No matter, the
2063                                    frag hacking will finger it out.
2064                                    Not quite: it can't switch from
2065                                    BRANCH to BCC68000 for the case
2066                                    where opnd is absolute (it needs
2067                                    to use the 68000 hack since no
2068                                    conditional abs jumps).  */
2069                                 if (((current_architecture <= m68010) || (0==adds(opP->con1)))
2070                                     && (the_ins.opcode[0] >= 0x6200)
2071                                     && (the_ins.opcode[0] <= 0x6f00)) {
2072                                         add_frag(adds(opP->con1),offs(opP->con1),TAB(BCC68000,SZ_UNDEF));
2073                                 } else {
2074                                         add_frag(adds(opP->con1),offs(opP->con1),TAB(BRANCH,SZ_UNDEF));
2075                                 }
2076                                 break;
2077                         case 'w':
2078                                 if(isvar(opP->con1)) {
2079                                         /* check for DBcc instruction */
2080                                         if ((the_ins.opcode[0] & 0xf0f8) ==0x50c8) {
2081                                                 /* size varies if patch */
2082                                                 /* needed for long form */
2083                                                 add_frag(adds(opP->con1),offs(opP->con1),TAB(DBCC,SZ_UNDEF));
2084                                                 break;
2085                                         }
2086                                         
2087                                         /* Don't ask! */
2088                                         opP->con1->e_exp.X_add_number+=2;
2089                                         add_fix('w',opP->con1,1);
2090                                 }
2091                                 addword(0);
2092                                 break;
2093                         case 'C':               /* Fixed size LONG coproc branches */
2094                                 the_ins.opcode[the_ins.numo-1]|=0x40;
2095                                 /* Offset the displacement to be relative to byte disp location */
2096                                 /* Coproc branches don't have a byte disp option, but they are
2097                                    compatible with the ordinary branches, which do... */
2098                                 opP->con1->e_exp.X_add_number+=4;
2099                                 add_fix('l',opP->con1,1);
2100                                 addword(0);
2101                                 addword(0);
2102                                 break;
2103                         case 'c':               /* Var size Coprocesssor branches */
2104                                 if(subs(opP->con1)) {
2105                                         add_fix('l',opP->con1,1);
2106                                         add_frag((symbolS *)0,(long)0,TAB(FBRANCH,LONG));
2107                                 } else if(adds(opP->con1)) {
2108                                         add_frag(adds(opP->con1),offs(opP->con1),TAB(FBRANCH,SZ_UNDEF));
2109                                 } else {
2110                                         /* add_frag((symbolS *)0,offs(opP->con1),TAB(FBRANCH,SHORT)); */
2111                                         the_ins.opcode[the_ins.numo-1]|=0x40;
2112                                         add_fix('l',opP->con1,1);
2113                                         addword(0);
2114                                         addword(4);
2115                                 }
2116                                 break;
2117                         default:
2118                                 as_fatal("Internal error:  operand type B%c unknown in line %s of file \"%s\"",
2119                                          s[1], __LINE__, __FILE__);
2120                         }
2121                         break;
2122                         
2123                 case 'C':               /* Ignore it */
2124                         break;
2125                         
2126                 case 'd':               /* JF this is a kludge */
2127                         if(opP->mode==AOFF) {
2128                                 install_operand('s',opP->reg-ADDR);
2129                         } else {
2130                                 char *tmpP;
2131                                 
2132                                 tmpP=opP->con1->e_end-2;
2133                                 opP->con1->e_beg++;
2134                                 opP->con1->e_end-=4;    /* point to the , */
2135                                 baseo=m68k_reg_parse(&tmpP);
2136                                 if(baseo<ADDR+0 || baseo>ADDR+7) {
2137                                         as_bad("Unknown address reg, using A0");
2138                                         baseo=0;
2139                                 } else baseo-=ADDR;
2140                                 install_operand('s',baseo);
2141                         }
2142                         tmpreg=get_num(opP->con1,80);
2143                         if(!issword(tmpreg)) {
2144                                 as_warn("Expression out of range, using 0");
2145                                 tmpreg=0;
2146                         }
2147                         addword(tmpreg);
2148                         break;
2149                         
2150                 case 'D':
2151                         install_operand(s[1],opP->reg-DATA);
2152                         break;
2153                         
2154                 case 'F':
2155                         install_operand(s[1],opP->reg-FPREG);
2156                         break;
2157                         
2158                 case 'I':
2159                         tmpreg=1+opP->reg-COPNUM;
2160                         if(tmpreg==8)
2161                             tmpreg=0;
2162                         install_operand(s[1],tmpreg);
2163                         break;
2164                         
2165                 case 'J':               /* JF foo */
2166                         switch(opP->reg) {
2167                         case SFC:   tmpreg=0x000; break;
2168                         case DFC:   tmpreg=0x001; break;
2169                         case CACR:  tmpreg=0x002; break;
2170                         case TC:    tmpreg=0x003; break;
2171                         case ITT0:  tmpreg=0x004; break;
2172                         case ITT1:  tmpreg=0x005; break;
2173                         case DTT0:  tmpreg=0x006; break;
2174                         case DTT1:  tmpreg=0x007; break;
2175
2176                         case USP:   tmpreg=0x800; break;
2177                         case VBR:   tmpreg=0x801; break;
2178                         case CAAR:  tmpreg=0x802; break;
2179                         case MSP:   tmpreg=0x803; break;
2180                         case ISP:   tmpreg=0x804; break;
2181                         case MMUSR: tmpreg=0x805; break;
2182                         case URP:   tmpreg=0x806; break;
2183                         case SRP:   tmpreg=0x807; break;
2184                         default:
2185                                 as_fatal("failed sanity check.");
2186                         }
2187                         install_operand(s[1],tmpreg);
2188                         break;
2189                         
2190                 case 'k':
2191                         tmpreg=get_num(opP->con1,55);
2192                         install_operand(s[1],tmpreg&0x7f);
2193                         break;
2194                         
2195                 case 'l':
2196                         tmpreg=opP->reg;
2197                         if(s[1]=='w') {
2198                                 if(tmpreg&0x7FF0000)
2199                                     as_bad("Floating point register in register list");
2200                                 insop(reverse_16_bits(tmpreg));
2201                         } else {
2202                                 if(tmpreg&0x700FFFF)
2203                                     as_bad("Wrong register in floating-point reglist");
2204                                 install_operand(s[1],reverse_8_bits(tmpreg>>16));
2205                         }
2206                         break;
2207                         
2208                 case 'L':
2209                         tmpreg=opP->reg;
2210                         if(s[1]=='w') {
2211                                 if(tmpreg&0x7FF0000)
2212                                     as_bad("Floating point register in register list");
2213                                 insop(tmpreg);
2214                         } else if(s[1]=='8') {
2215                                 if(tmpreg&0x0FFFFFF)
2216                                     as_bad("incorrect register in reglist");
2217                                 install_operand(s[1],tmpreg>>24);
2218                         } else {
2219                                 if(tmpreg&0x700FFFF)
2220                                     as_bad("wrong register in floating-point reglist");
2221                                 else
2222                                     install_operand(s[1],tmpreg>>16);
2223                         }
2224                         break;
2225                         
2226                 case 'M':
2227                         install_operand(s[1],get_num(opP->con1,60));
2228                         break;
2229                         
2230                 case 'O':
2231                         tmpreg= (opP->mode==DREG)
2232                             ? 0x20+opP->reg-DATA
2233                                 : (get_num(opP->con1,40)&0x1F);
2234                         install_operand(s[1],tmpreg);
2235                         break;
2236                         
2237                 case 'Q':
2238                         tmpreg=get_num(opP->con1,10);
2239                         if(tmpreg==8)
2240                             tmpreg=0;
2241                         install_operand(s[1],tmpreg);
2242                         break;
2243                         
2244                 case 'R':
2245                         /* This depends on the fact that ADDR registers are
2246                            eight more than their corresponding DATA regs, so
2247                            the result will have the ADDR_REG bit set */
2248                         install_operand(s[1],opP->reg-DATA);
2249                         break;
2250                         
2251                 case 's':
2252                         if(opP->reg==FPI) tmpreg=0x1;
2253                         else if(opP->reg==FPS) tmpreg=0x2;
2254                         else if(opP->reg==FPC) tmpreg=0x4;
2255                         else as_fatal("failed sanity check.");
2256                         install_operand(s[1],tmpreg);
2257                         break;
2258                         
2259                 case 'S':       /* Ignore it */
2260                         break;
2261                         
2262                 case 'T':
2263                         install_operand(s[1],get_num(opP->con1,30));
2264                         break;
2265                         
2266                 case 'U':       /* Ignore it */
2267                         break;
2268                         
2269                 case 'c':
2270                         switch (opP->reg) {
2271                         case NC: tmpreg = 0; break;
2272                         case DC: tmpreg = 1; break;
2273                         case IC: tmpreg = 2; break;
2274                         case BC: tmpreg = 3; break;
2275                         default:
2276                                 as_fatal("failed sanity check");
2277                         } /* switch on cache token */
2278                         install_operand(s[1], tmpreg);
2279                         break;
2280 #ifndef NO_68851
2281                         /* JF: These are out of order, I fear. */
2282                 case 'f':
2283                         switch (opP->reg) {
2284                         case SFC:
2285                                 tmpreg=0;
2286                                 break;
2287                         case DFC:
2288                                 tmpreg=1;
2289                                 break;
2290                         default:
2291                                 as_fatal("failed sanity check.");
2292                         }
2293                         install_operand(s[1],tmpreg);
2294                         break;
2295                         
2296                 case 'P':
2297                         switch(opP->reg) {
2298                         case TC:
2299                                 tmpreg=0;
2300                                 break;
2301                         case CAL:
2302                                 tmpreg=4;
2303                                 break;
2304                         case VAL:
2305                                 tmpreg=5;
2306                                 break;
2307                         case SCC:
2308                                 tmpreg=6;
2309                                 break;
2310                         case AC:
2311                                 tmpreg=7;
2312                                 break;
2313                         default:
2314                                 as_fatal("failed sanity check.");
2315                         }
2316                         install_operand(s[1],tmpreg);
2317                         break;
2318                         
2319                 case 'V':
2320                         if (opP->reg == VAL)
2321                             break;
2322                         as_fatal("failed sanity check.");
2323                         
2324                 case 'W':
2325                         switch(opP->reg) {
2326                                 
2327                         case DRP:
2328                                 tmpreg=1;
2329                                 break;
2330                         case SRP:
2331                                 tmpreg=2;
2332                                 break;
2333                         case CRP:
2334                                 tmpreg=3;
2335                                 break;
2336                         default:
2337                                 as_fatal("failed sanity check.");
2338                         }
2339                         install_operand(s[1],tmpreg);
2340                         break;
2341                         
2342                 case 'X':
2343                         switch (opP->reg) {
2344                         case BAD: case BAD+1: case BAD+2: case BAD+3:
2345                         case BAD+4: case BAD+5: case BAD+6: case BAD+7:
2346                                 tmpreg = (4 << 10) | ((opP->reg - BAD) << 2);
2347                                 break;
2348                                 
2349                         case BAC: case BAC+1: case BAC+2: case BAC+3:
2350                         case BAC+4: case BAC+5: case BAC+6: case BAC+7:
2351                                 tmpreg = (5 << 10) | ((opP->reg - BAC) << 2);
2352                                 break;
2353                                 
2354                         default:
2355                                 as_fatal("failed sanity check.");
2356                         }
2357                         install_operand(s[1], tmpreg);
2358                         break;
2359                 case 'Y':
2360                         know(opP->reg == PSR);
2361                         break;
2362                 case 'Z':
2363                         know(opP->reg == PCSR);
2364                         break;
2365 #endif /* m68851 */
2366                 case '_':
2367                         tmpreg=get_num(opP->con1,80);
2368                         install_operand(s[1], tmpreg);
2369                         break;
2370                 default:
2371                         as_fatal("Internal error:  Operand type %c unknown in line %s of file \"%s\"", s[0], __LINE__, __FILE__);
2372                 }
2373         }
2374         /* By the time whe get here (FINALLY) the_ins contains the complete
2375            instruction, ready to be emitted. . . */
2376 } /* m68k_ip() */
2377
2378 /*
2379  * get_regs := '/' + ?
2380  *      | '-' + <register>
2381  *      | '-' + <register> + ?
2382  *      | <empty>
2383  *      ;
2384  *
2385
2386  * The idea here must be to scan in a set of registers but I don't
2387  * understand it.  Looks awfully sloppy to me but I don't have any doc on
2388  * this format so...
2389
2390  * 
2391  *
2392  */
2393
2394 static int get_regs(i,str,opP)
2395 int i;
2396 struct m68k_op *opP;
2397 char *str;
2398 {
2399         /*                           26, 25, 24, 23-16,  15-8, 0-7 */
2400         /* Low order 24 bits encoded fpc,fps,fpi,fp7-fp0,a7-a0,d7-d0 */
2401         unsigned long cur_regs = 0;
2402         int     reg1,
2403                 reg2;
2404
2405 #define ADD_REG(x)      {     if(x==FPI) cur_regs|=(1<<24);\
2406                          else if(x==FPS) cur_regs|=(1<<25);\
2407                          else if(x==FPC) cur_regs|=(1<<26);\
2408                          else cur_regs|=(1<<(x-1));  }
2409
2410         reg1=i;
2411         for(;;) {
2412                 if(*str=='/') {
2413                         ADD_REG(reg1);
2414                         str++;
2415                 } else if(*str=='-') {
2416                         str++;
2417                         reg2=m68k_reg_parse(&str);
2418                         if(reg2<DATA || reg2>=FPREG+8 || reg1==FPI || reg1==FPS || reg1==FPC) {
2419                                 opP->error="unknown register in register list";
2420                                 return FAIL;
2421                         }
2422                         while(reg1<=reg2) {
2423                                 ADD_REG(reg1);
2424                                 reg1++;
2425                         }
2426                         if(*str=='\0')
2427                                 break;
2428                 } else if(*str=='\0') {
2429                         ADD_REG(reg1);
2430                         break;
2431                 } else {
2432                         opP->error="unknow character in register list";
2433                         return FAIL;
2434                 }
2435 /* DJA -- Bug Fix.  Did't handle d1-d2/a1 until the following instruction was added */
2436                 if (*str=='/')
2437                   str ++;
2438                 reg1=m68k_reg_parse(&str);
2439                 if((reg1<DATA || reg1>=FPREG+8) && !(reg1==FPI || reg1==FPS || reg1==FPC)) {
2440                         opP->error="unknown register in register list";
2441                         return FAIL;
2442                 }
2443         }
2444         opP->reg=cur_regs;
2445         return OK;
2446 } /* get_regs() */
2447
2448 static int reverse_16_bits(in)
2449 int in;
2450 {
2451         int out=0;
2452         int n;
2453
2454         static int mask[16] = {
2455 0x0001,0x0002,0x0004,0x0008,0x0010,0x0020,0x0040,0x0080,
2456 0x0100,0x0200,0x0400,0x0800,0x1000,0x2000,0x4000,0x8000
2457         };
2458         for(n=0;n<16;n++) {
2459                 if(in&mask[n])
2460                         out|=mask[15-n];
2461         }
2462         return out;
2463 } /* reverse_16_bits() */
2464
2465 static int reverse_8_bits(in)
2466 int in;
2467 {
2468         int out=0;
2469         int n;
2470
2471         static int mask[8] = {
2472 0x0001,0x0002,0x0004,0x0008,0x0010,0x0020,0x0040,0x0080,
2473         };
2474
2475         for(n=0;n<8;n++) {
2476                 if(in&mask[n])
2477                         out|=mask[7-n];
2478         }
2479         return out;
2480 } /* reverse_8_bits() */
2481
2482 static void install_operand(mode,val)
2483 int mode;
2484 int val;
2485 {
2486         switch(mode) {
2487         case 's':
2488                 the_ins.opcode[0]|=val & 0xFF;  /* JF FF is for M kludge */
2489                 break;
2490         case 'd':
2491                 the_ins.opcode[0]|=val<<9;
2492                 break;
2493         case '1':
2494                 the_ins.opcode[1]|=val<<12;
2495                 break;
2496         case '2':
2497                 the_ins.opcode[1]|=val<<6;
2498                 break;
2499         case '3':
2500                 the_ins.opcode[1]|=val;
2501                 break;
2502         case '4':
2503                 the_ins.opcode[2]|=val<<12;
2504                 break;
2505         case '5':
2506                 the_ins.opcode[2]|=val<<6;
2507                 break;
2508         case '6':
2509                         /* DANGER!  This is a hack to force cas2l and cas2w cmds
2510                            to be three words long! */
2511                 the_ins.numo++;
2512                 the_ins.opcode[2]|=val;
2513                 break;
2514         case '7':
2515                 the_ins.opcode[1]|=val<<7;
2516                 break;
2517         case '8':
2518                 the_ins.opcode[1]|=val<<10;
2519                 break;
2520 #ifndef NO_68851
2521         case '9':
2522                 the_ins.opcode[1]|=val<<5;
2523                 break;
2524 #endif
2525
2526         case 't':
2527                 the_ins.opcode[1]|=(val<<10)|(val<<7);
2528                 break;
2529         case 'D':
2530                 the_ins.opcode[1]|=(val<<12)|val;
2531                 break;
2532         case 'g':
2533                 the_ins.opcode[0]|=val=0xff;
2534                 break;
2535         case 'i':
2536                 the_ins.opcode[0]|=val<<9;
2537                 break;
2538         case 'C':
2539                 the_ins.opcode[1]|=val;
2540                 break;
2541         case 'j':
2542                 the_ins.opcode[1]|=val;
2543                 the_ins.numo++;         /* What a hack */
2544                 break;
2545         case 'k':
2546                 the_ins.opcode[1]|=val<<4;
2547                 break;
2548         case 'b':
2549         case 'w':
2550         case 'l':
2551                 break;
2552         case 'e':
2553                 the_ins.opcode[0] |= (val << 6);
2554                 break;
2555         case 'L':
2556                 the_ins.opcode[1] = (val >> 16);
2557                 the_ins.opcode[2] = val & 0xffff;
2558                 break;
2559         case 'c':
2560         default:
2561                 as_fatal("failed sanity check.");
2562         }
2563 } /* install_operand() */
2564
2565 static void install_gen_operand(mode,val)
2566 int mode;
2567 int val;
2568 {
2569         switch(mode) {
2570         case 's':
2571                 the_ins.opcode[0]|=val;
2572                 break;
2573         case 'd':
2574                         /* This is a kludge!!! */
2575                 the_ins.opcode[0]|=(val&0x07)<<9|(val&0x38)<<3;
2576                 break;
2577         case 'b':
2578         case 'w':
2579         case 'l':
2580         case 'f':
2581         case 'F':
2582         case 'x':
2583         case 'p':
2584                 the_ins.opcode[0]|=val;
2585                 break;
2586                 /* more stuff goes here */
2587         default:
2588                 as_fatal("failed sanity check.");
2589         }
2590 } /* install_gen_operand() */
2591
2592 /*
2593  * verify that we have some number of paren pairs, do m68k_ip_op(), and
2594  * then deal with the bitfield hack.
2595  */
2596
2597 static char *crack_operand(str,opP)
2598 register char *str;
2599 register struct m68k_op *opP;
2600 {
2601         register int parens;
2602         register int c;
2603         register char *beg_str;
2604
2605         if(!str) {
2606                 return str;
2607         }
2608         beg_str=str;
2609         for(parens=0;*str && (parens>0 || notend(str));str++) {
2610                 if(*str=='(') parens++;
2611                 else if(*str==')') {
2612                         if(!parens) {           /* ERROR */
2613                                 opP->error="Extra )";
2614                                 return str;
2615                         }
2616                         --parens;
2617                 }
2618         }
2619         if(!*str && parens) {           /* ERROR */
2620                 opP->error="Missing )";
2621                 return str;
2622         }
2623         c= *str;
2624         *str='\0';
2625         if(m68k_ip_op(beg_str,opP)==FAIL) {
2626                 *str=c;
2627                 return str;
2628         }
2629         *str=c;
2630         if(c=='}')
2631                 c= *++str;              /* JF bitfield hack */
2632         if(c) {
2633                 c= *++str;
2634                 if(!c)
2635                         as_bad("Missing operand");
2636         }
2637         return str;
2638 }
2639
2640 /* See the comment up above where the #define notend(... is */
2641 #if 0
2642 notend(s)
2643 char *s;
2644 {
2645         if(*s==',') return 0;
2646         if(*s=='{' || *s=='}')
2647                 return 0;
2648         if(*s!=':') return 1;
2649                 /* This kludge here is for the division cmd, which is a kludge */
2650         if(index("aAdD#",s[1])) return 0;
2651         return 1;
2652 }
2653 #endif
2654
2655 /* This is the guts of the machine-dependent assembler.  STR points to a
2656    machine dependent instruction.  This function is supposed to emit
2657    the frags/bytes it assembles to.
2658  */
2659 void
2660 md_assemble(str)
2661 char *str;
2662 {
2663         char *er;
2664         short   *fromP;
2665         char    *toP = NULL;
2666         int     m,n = 0;
2667         char    *to_beg_P;
2668         int     shorts_this_frag;
2669
2670         bzero((char *)(&the_ins),sizeof(the_ins));      /* JF for paranoia sake */
2671         m68k_ip(str);
2672         er=the_ins.error;
2673         if(!er) {
2674                 for(n=the_ins.numargs;n;--n)
2675                         if(the_ins.operands[n].error) {
2676                                 er=the_ins.operands[n].error;
2677                                 break;
2678                         }
2679         }
2680         if(er) {
2681                 as_bad("\"%s\" -- Statement '%s' ignored",er,str);
2682                 return;
2683         }
2684
2685         if(the_ins.nfrag==0) {  /* No frag hacking involved; just put it out */
2686                 toP=frag_more(2*the_ins.numo);
2687                 fromP= &the_ins.opcode[0];
2688                 for(m=the_ins.numo;m;--m) {
2689                         md_number_to_chars(toP,(long)(*fromP),2);
2690                         toP+=2;
2691                         fromP++;
2692                 }
2693                         /* put out symbol-dependent info */
2694                 for(m=0;m<the_ins.nrel;m++) {
2695                         switch(the_ins.reloc[m].wid) {
2696                         case 'B':
2697                                 n=1;
2698                                 break;
2699                         case 'b':
2700                                 n=1;
2701                                 break;
2702                         case '3':
2703                                 n=2;
2704                                 break;
2705                         case 'w':
2706                                 n=2;
2707                                 break;
2708                         case 'l':
2709                                 n=4;
2710                                 break;
2711                         default:
2712                                 as_fatal("Don't know how to figure width of %c in md_assemble()",the_ins.reloc[m].wid);
2713                         }
2714
2715                         fix_new(frag_now,
2716                                 (toP-frag_now->fr_literal)-the_ins.numo*2+the_ins.reloc[m].n,
2717                                 n,
2718                                 the_ins.reloc[m].add,
2719                                 the_ins.reloc[m].sub,
2720                                 the_ins.reloc[m].off,
2721                                 the_ins.reloc[m].pcrel,
2722                                 NO_RELOC);
2723                 }
2724                 return;
2725         }
2726
2727                 /* There's some frag hacking */
2728         for(n=0,fromP= &the_ins.opcode[0];n<the_ins.nfrag;n++) {
2729                 int wid;
2730
2731                 if(n==0) wid=2*the_ins.fragb[n].fragoff;
2732                 else wid=2*(the_ins.numo-the_ins.fragb[n-1].fragoff);
2733                 toP=frag_more(wid);
2734                 to_beg_P=toP;
2735                 shorts_this_frag=0;
2736                 for(m=wid/2;m;--m) {
2737                         md_number_to_chars(toP,(long)(*fromP),2);
2738                         toP+=2;
2739                         fromP++;
2740                         shorts_this_frag++;
2741                 }
2742                 for(m=0;m<the_ins.nrel;m++) {
2743                         if((the_ins.reloc[m].n)>= 2*shorts_this_frag /* 2*the_ins.fragb[n].fragoff */) {
2744                                 the_ins.reloc[m].n-= 2*shorts_this_frag /* 2*the_ins.fragb[n].fragoff */;
2745                                 break;
2746                         }
2747                         wid=the_ins.reloc[m].wid;
2748                         if(wid==0)
2749                                 continue;
2750                         the_ins.reloc[m].wid=0;
2751                         wid = (wid=='b') ? 1 : (wid=='w') ? 2 : (wid=='l') ? 4 : 4000;
2752
2753                         fix_new(frag_now,
2754                             (toP-frag_now->fr_literal)-the_ins.numo*2+the_ins.reloc[m].n,
2755                             wid,
2756                             the_ins.reloc[m].add,
2757                             the_ins.reloc[m].sub,
2758                             the_ins.reloc[m].off,
2759                             the_ins.reloc[m].pcrel,
2760                                 NO_RELOC);
2761                 }
2762                 know(the_ins.fragb[n].fadd);
2763                 (void)frag_var(rs_machine_dependent,10,0,(relax_substateT)(the_ins.fragb[n].fragty),
2764  the_ins.fragb[n].fadd,the_ins.fragb[n].foff,to_beg_P);
2765         }
2766         n=(the_ins.numo-the_ins.fragb[n-1].fragoff);
2767         shorts_this_frag=0;
2768         if(n) {
2769                 toP=frag_more(n*sizeof(short));
2770                 while(n--) {
2771                         md_number_to_chars(toP,(long)(*fromP),2);
2772                         toP+=2;
2773                         fromP++;
2774                         shorts_this_frag++;
2775                 }
2776         }
2777         for(m=0;m<the_ins.nrel;m++) {
2778                 int wid;
2779
2780                 wid=the_ins.reloc[m].wid;
2781                 if(wid==0)
2782                         continue;
2783                 the_ins.reloc[m].wid=0;
2784                 wid = (wid=='b') ? 1 : (wid=='w') ? 2 : (wid=='l') ? 4 : 4000;
2785
2786                 fix_new(frag_now,
2787                     (the_ins.reloc[m].n + toP-frag_now->fr_literal)-/* the_ins.numo */ shorts_this_frag*2,
2788                     wid,
2789                     the_ins.reloc[m].add,
2790                     the_ins.reloc[m].sub,
2791                     the_ins.reloc[m].off,
2792                     the_ins.reloc[m].pcrel,
2793                                 NO_RELOC);
2794         }
2795 }
2796
2797 /* This function is called once, at assembler startup time.  This should
2798    set up all the tables, etc that the MD part of the assembler needs
2799  */
2800 void
2801 md_begin()
2802 {
2803 /*
2804  * md_begin -- set up hash tables with 68000 instructions.
2805  * similar to what the vax assembler does.  ---phr
2806  */
2807         /* RMS claims the thing to do is take the m68k-opcode.h table, and make
2808            a copy of it at runtime, adding in the information we want but isn't
2809            there.  I think it'd be better to have an awk script hack the table
2810            at compile time.  Or even just xstr the table and use it as-is.  But
2811            my lord ghod hath spoken, so we do it this way.  Excuse the ugly var
2812            names.  */
2813
2814         register const struct m68k_opcode *ins;
2815         register struct m68k_incant *hack,
2816                 *slak;
2817         register char *retval = 0;              /* empty string, or error msg text */
2818         register unsigned int i;
2819         register char c;
2820
2821         if ((op_hash = hash_new()) == NULL)
2822                 as_fatal("Virtual memory exhausted");
2823
2824         obstack_begin(&robyn,4000);
2825         for (ins = m68k_opcodes; ins < endop; ins++) {
2826                 hack=slak=(struct m68k_incant *)obstack_alloc(&robyn,sizeof(struct m68k_incant));
2827                 do {
2828                         /* we *could* ignore insns that don't match our
2829                            arch here but just leaving them out of the
2830                            hash. */
2831                         slak->m_operands=ins->args;
2832                         slak->m_opnum=strlen(slak->m_operands)/2;
2833                         slak->m_arch = ins->arch;
2834                         slak->m_opcode=ins->opcode;
2835                                 /* This is kludgey */
2836                         slak->m_codenum=((ins->match)&0xffffL) ? 2 : 1;
2837                         if((ins+1)!=endop && !strcmp(ins->name,(ins+1)->name)) {
2838                                 slak->m_next=(struct m68k_incant *) obstack_alloc(&robyn,sizeof(struct m68k_incant));
2839                                 ins++;
2840                         } else
2841                                 slak->m_next=0;
2842                         slak=slak->m_next;
2843                 } while(slak);
2844
2845                 retval = hash_insert (op_hash, ins->name,(char *)hack);
2846                         /* Didn't his mommy tell him about null pointers? */
2847                 if(retval && *retval)
2848                         as_fatal("Internal Error:  Can't hash %s: %s",ins->name,retval);
2849         }
2850
2851         for (i = 0; i < sizeof(mklower_table) ; i++)
2852                 mklower_table[i] = (isupper(c = (char) i)) ? tolower(c) : c;
2853
2854         for (i = 0 ; i < sizeof(notend_table) ; i++) {
2855                 notend_table[i] = 0;
2856                 alt_notend_table[i] = 0;
2857         }
2858         notend_table[','] = 1;
2859         notend_table['{'] = 1;
2860         notend_table['}'] = 1;
2861         alt_notend_table['a'] = 1;
2862         alt_notend_table['A'] = 1;
2863         alt_notend_table['d'] = 1;
2864         alt_notend_table['D'] = 1;
2865         alt_notend_table['#'] = 1;
2866         alt_notend_table['f'] = 1;
2867         alt_notend_table['F'] = 1;
2868 #ifdef REGISTER_PREFIX
2869         alt_notend_table[REGISTER_PREFIX] = 1;
2870 #endif
2871 }
2872
2873 #if 0
2874 #define notend(s) ((*s == ',' || *s == '}' || *s == '{' \
2875                    || (*s == ':' && strchr("aAdD#", s[1]))) \
2876                ? 0 : 1)
2877 #endif
2878
2879 /* This funciton is called once, before the assembler exits.  It is
2880    supposed to do any final cleanup for this part of the assembler.
2881  */
2882 void
2883 md_end()
2884 {
2885 }
2886
2887 /* Equal to MAX_PRECISION in atof-ieee.c */
2888 #define MAX_LITTLENUMS 6
2889
2890 /* Turn a string in input_line_pointer into a floating point constant of type
2891    type, and store the appropriate bytes in *litP.  The number of LITTLENUMS
2892    emitted is stored in *sizeP .  An error message is returned, or NULL on OK.
2893  */
2894 char *
2895 md_atof(type,litP,sizeP)
2896 char type;
2897 char *litP;
2898 int *sizeP;
2899 {
2900         int     prec;
2901         LITTLENUM_TYPE words[MAX_LITTLENUMS];
2902         LITTLENUM_TYPE *wordP;
2903         char    *t;
2904         char    *atof_ieee();
2905
2906         switch(type) {
2907         case 'f':
2908         case 'F':
2909         case 's':
2910         case 'S':
2911                 prec = 2;
2912                 break;
2913
2914         case 'd':
2915         case 'D':
2916         case 'r':
2917         case 'R':
2918                 prec = 4;
2919                 break;
2920
2921         case 'x':
2922         case 'X':
2923                 prec = 6;
2924                 break;
2925
2926         case 'p':
2927         case 'P':
2928                 prec = 6;
2929                 break;
2930
2931         default:
2932                 *sizeP=0;
2933                 return "Bad call to MD_ATOF()";
2934         }
2935         t=atof_ieee(input_line_pointer,type,words);
2936         if(t)
2937                 input_line_pointer=t;
2938
2939         *sizeP=prec * sizeof(LITTLENUM_TYPE);
2940         for(wordP=words;prec--;) {
2941                 md_number_to_chars(litP,(long)(*wordP++),sizeof(LITTLENUM_TYPE));
2942                 litP+=sizeof(LITTLENUM_TYPE);
2943         }
2944         return "";      /* Someone should teach Dean about null pointers */
2945 }
2946
2947 /* Turn an integer of n bytes (in val) into a stream of bytes appropriate
2948    for use in the a.out file, and stores them in the array pointed to by buf.
2949    This knows about the endian-ness of the target machine and does
2950    THE RIGHT THING, whatever it is.  Possible values for n are 1 (byte)
2951    2 (short) and 4 (long)  Floating numbers are put out as a series of
2952    LITTLENUMS (shorts, here at least)
2953  */
2954 void
2955 md_number_to_chars(buf,val,n)
2956 char    *buf;
2957 long    val;
2958 int n;
2959 {
2960         switch(n) {
2961         case 1:
2962                 *buf++=val;
2963                 break;
2964         case 2:
2965                 *buf++=(val>>8);
2966                 *buf++=val;
2967                 break;
2968         case 4:
2969                 *buf++=(val>>24);
2970                 *buf++=(val>>16);
2971                 *buf++=(val>>8);
2972                 *buf++=val;
2973                 break;
2974         default:
2975                 as_fatal("failed sanity check.");
2976         }
2977 }
2978
2979 void
2980 md_apply_fix(fixP, val)
2981         fixS *fixP;
2982         long val;
2983 {
2984         char *buf = fixP->fx_where + fixP->fx_frag->fr_literal;
2985
2986         switch(fixP->fx_size) {
2987         case 1:
2988                 *buf++=val;
2989                 break;
2990         case 2:
2991                 *buf++=(val>>8);
2992                 *buf++=val;
2993                 break;
2994         case 4:
2995                 *buf++=(val>>24);
2996                 *buf++=(val>>16);
2997                 *buf++=(val>>8);
2998                 *buf++=val;
2999                 break;
3000         default:
3001                 BAD_CASE (fixP->fx_size);
3002         }
3003 }
3004
3005
3006 /* *fragP has been relaxed to its final size, and now needs to have
3007    the bytes inside it modified to conform to the new size  There is UGLY
3008    MAGIC here. ..
3009  */
3010 void
3011 md_convert_frag(headers, fragP)
3012 object_headers *headers;
3013 register fragS *fragP;
3014 {
3015   long disp;
3016   long ext = 0;
3017
3018   /* Address in object code of the displacement.  */
3019   register int object_address = fragP -> fr_fix + fragP -> fr_address;
3020
3021 #ifdef IBM_COMPILER_SUX
3022  /* This is wrong but it convinces the native rs6000 compiler to
3023     generate the code we want. */
3024   register char *buffer_address = fragP -> fr_literal;
3025   buffer_address += fragP -> fr_fix;
3026 #else /* IBM_COMPILER_SUX */
3027   /* Address in gas core of the place to store the displacement.  */
3028   register char *buffer_address = fragP->fr_fix + fragP->fr_literal;
3029 #endif /* IBM_COMPILER_SUX */
3030
3031   /* No longer true:   know(fragP->fr_symbol); */
3032
3033   /* The displacement of the address, from current location.  */
3034   disp = fragP->fr_symbol ? S_GET_VALUE(fragP->fr_symbol) : 0;
3035   disp = (disp + fragP->fr_offset) - object_address;
3036
3037   switch(fragP->fr_subtype) {
3038   case TAB(BCC68000,BYTE):
3039   case TAB(BRANCH,BYTE):
3040     know(issbyte(disp));
3041     if(disp==0)
3042       as_bad("short branch with zero offset: use :w");
3043     fragP->fr_opcode[1]=disp;
3044     ext=0;
3045     break;
3046   case TAB(DBCC,SHORT):
3047     know(issword(disp));
3048     ext=2;
3049     break;
3050   case TAB(BCC68000,SHORT):
3051   case TAB(BRANCH,SHORT):
3052     know(issword(disp));
3053     fragP->fr_opcode[1]=0x00;
3054     ext=2;
3055     break;
3056   case TAB(BRANCH,LONG):
3057     if (current_architecture <= m68010) {
3058       if (fragP->fr_opcode[0]==0x61) {
3059         fragP->fr_opcode[0]= 0x4E;
3060         fragP->fr_opcode[1]= 0xB9;      /* JBSR with ABSL LONG offset */
3061         subseg_change(SEG_TEXT, 0);
3062
3063         fix_new(fragP,
3064                 fragP->fr_fix,
3065                 4,
3066                 fragP->fr_symbol,
3067                 0,
3068                 fragP->fr_offset,
3069                 0,
3070                 NO_RELOC);
3071
3072         fragP->fr_fix+=4;
3073         ext=0;
3074       } else if (fragP->fr_opcode[0]==0x60) {
3075         fragP->fr_opcode[0]= 0x4E;
3076         fragP->fr_opcode[1]= 0xF9;      /* JMP  with ABSL LONG offset */
3077         subseg_change(SEG_TEXT, 0);
3078         fix_new(fragP, fragP->fr_fix, 4, fragP->fr_symbol, 0, fragP->fr_offset,0,
3079                                 NO_RELOC);
3080         fragP->fr_fix+=4;
3081         ext=0;
3082       } else {
3083         as_bad("Long branch offset not supported.");
3084       }
3085     } else {
3086       fragP->fr_opcode[1]=0xff;
3087       ext=4;
3088     }
3089     break;
3090   case TAB(BCC68000,LONG):
3091         /* only Bcc 68000 instructions can come here */
3092         /* change bcc into b!cc/jmp absl long */
3093         fragP->fr_opcode[0] ^= 0x01; /* invert bcc */
3094         fragP->fr_opcode[1] = 0x6;   /* branch offset = 6 */
3095
3096         /* JF: these used to be fr_opcode[2,3], but they may be in a
3097            different frag, in which case refering to them is a no-no.
3098            Only fr_opcode[0,1] are guaranteed to work. */
3099         *buffer_address++ = 0x4e;  /* put in jmp long (0x4ef9) */ 
3100         *buffer_address++ = 0xf9;  
3101         fragP->fr_fix += 2;          /* account for jmp instruction */
3102         subseg_change(SEG_TEXT,0);
3103         fix_new(fragP, fragP->fr_fix, 4, fragP->fr_symbol, 0, 
3104                                          fragP->fr_offset,0,
3105                                 NO_RELOC);
3106         fragP->fr_fix += 4;
3107         ext=0;
3108         break;
3109   case TAB(DBCC,LONG):
3110         /* only DBcc 68000 instructions can come here */
3111         /* change dbcc into dbcc/jmp absl long */
3112         /* JF: these used to be fr_opcode[2-7], but that's wrong */
3113         *buffer_address++ = 0x00;  /* branch offset = 4 */
3114         *buffer_address++ = 0x04;  
3115         *buffer_address++ = 0x60;  /* put in bra pc+6 */ 
3116         *buffer_address++ = 0x06;  
3117         *buffer_address++ = 0x4e;  /* put in jmp long (0x4ef9) */ 
3118         *buffer_address++ = 0xf9;  
3119
3120         fragP->fr_fix += 6;          /* account for bra/jmp instructions */
3121         subseg_change(SEG_TEXT,0);
3122         fix_new(fragP, fragP->fr_fix, 4, fragP->fr_symbol, 0, 
3123                                          fragP->fr_offset,0,
3124                                 NO_RELOC);
3125         fragP->fr_fix += 4;
3126         ext=0;
3127     break;
3128   case TAB(FBRANCH,SHORT):
3129     know((fragP->fr_opcode[1]&0x40)==0);
3130     ext=2;
3131     break;
3132   case TAB(FBRANCH,LONG):
3133     fragP->fr_opcode[1]|=0x40;  /* Turn on LONG bit */
3134     ext=4;
3135     break;
3136   case TAB(PCREL,SHORT):
3137     ext=2;
3138     break;
3139   case TAB(PCREL,LONG):
3140     /* The thing to do here is force it to ABSOLUTE LONG, since
3141        PCREL is really trying to shorten an ABSOLUTE address anyway */
3142     /* JF FOO This code has not been tested */
3143     subseg_change(SEG_TEXT,0);
3144     fix_new(fragP, fragP->fr_fix, 4, fragP->fr_symbol, 0, fragP->fr_offset, 0, NO_RELOC);
3145     if((fragP->fr_opcode[1] & 0x3F) != 0x3A)
3146         as_bad("Internal error (long PC-relative operand) for insn 0x%04lx at 0x%lx",
3147                 fragP->fr_opcode[0],fragP->fr_address);
3148     fragP->fr_opcode[1]&= ~0x3F;
3149     fragP->fr_opcode[1]|=0x39;  /* Mode 7.1 */
3150     fragP->fr_fix+=4;
3151     /* md_number_to_chars(buffer_address,
3152                        (long)(fragP->fr_symbol->sy_value + fragP->fr_offset),
3153                        4); */
3154     ext=0;
3155     break;
3156   case TAB(PCLEA,SHORT):
3157     subseg_change(SEG_TEXT,0);
3158     fix_new(fragP,(int)(fragP->fr_fix),2,fragP->fr_symbol,(symbolS *)0,fragP->fr_offset,1,
3159                                 NO_RELOC);
3160     fragP->fr_opcode[1] &= ~0x3F;
3161     fragP->fr_opcode[1] |= 0x3A;
3162     ext=2;
3163     break;
3164   case TAB(PCLEA,LONG):
3165     subseg_change(SEG_TEXT,0);
3166     fix_new(fragP,(int)(fragP->fr_fix)+2,4,fragP->fr_symbol,(symbolS *)0,fragP->fr_offset+2,1,
3167                                 NO_RELOC);
3168     *buffer_address++ = 0x01;
3169     *buffer_address++ = 0x70;
3170     fragP->fr_fix+=2;
3171     /* buffer_address+=2; */
3172     ext=4;
3173     break;
3174
3175   } /* switch on subtype */
3176
3177   if (ext) {
3178           md_number_to_chars(buffer_address, (long) disp, (int) ext);
3179           fragP->fr_fix += ext;
3180 /*        H_SET_TEXT_SIZE(headers, H_GET_TEXT_SIZE(headers) + ext); */
3181   } /* if extending */
3182
3183   return;
3184 } /* md_convert_frag() */
3185
3186 /* Force truly undefined symbols to their maximum size, and generally set up
3187    the frag list to be relaxed
3188  */
3189 int md_estimate_size_before_relax(fragP, segment)
3190 register fragS *fragP;
3191 segT segment;
3192 {
3193         int     old_fix;
3194         register char *buffer_address = fragP->fr_fix + fragP->fr_literal;
3195
3196         old_fix = fragP->fr_fix;
3197
3198         /* handle SZ_UNDEF first, it can be changed to BYTE or SHORT */
3199         switch(fragP->fr_subtype) {
3200
3201         case TAB(BCC68000,SZ_UNDEF): {
3202                 if((fragP->fr_symbol != NULL)
3203                    && S_GET_SEGMENT(fragP->fr_symbol) == segment) {
3204                         fragP->fr_subtype=TAB(BCC68000,BYTE);
3205                         break;
3206                 }
3207                 /* only Bcc 68000 instructions can come here */
3208                 /* change bcc into b!cc/jmp absl long */
3209                 fragP->fr_opcode[0] ^= 0x01; /* invert bcc */
3210                 if(flagseen['l']) {
3211                         fragP->fr_opcode[1] = 0x04;   /* branch offset = 6 */
3212                         /* JF: these were fr_opcode[2,3] */
3213                         buffer_address[0] = 0x4e;  /* put in jmp long (0x4ef9) */ 
3214                         buffer_address[1] = 0xf8;
3215                         fragP->fr_fix += 2;          /* account for jmp instruction */
3216                         subseg_change(SEG_TEXT,0);
3217                         fix_new(fragP, fragP->fr_fix, 2, fragP->fr_symbol, 0, 
3218                                                          fragP->fr_offset, 0, NO_RELOC);
3219                         fragP->fr_fix += 2;
3220                 } else {
3221                         fragP->fr_opcode[1] = 0x06;   /* branch offset = 6 */
3222                         /* JF: these were fr_opcode[2,3] */
3223                         buffer_address[2] = 0x4e;  /* put in jmp long (0x4ef9) */ 
3224                         buffer_address[3] = 0xf9;
3225                         fragP->fr_fix += 2;          /* account for jmp instruction */
3226                         subseg_change(SEG_TEXT,0);
3227                         fix_new(fragP, fragP->fr_fix, 4, fragP->fr_symbol, 0, 
3228                                                          fragP->fr_offset, 0, NO_RELOC);
3229                         fragP->fr_fix += 4;
3230                 }
3231                 frag_wane(fragP);
3232                 break;
3233         } /* case TAB(BCC68000,SZ_UNDEF) */
3234
3235         case TAB(DBCC,SZ_UNDEF): {
3236                 if (fragP->fr_symbol != NULL && S_GET_SEGMENT(fragP->fr_symbol) == segment) {
3237                         fragP->fr_subtype=TAB(DBCC,SHORT);
3238                         fragP->fr_var+=2;
3239                         break;
3240                 }
3241                 /* only DBcc 68000 instructions can come here */
3242                 /* change dbcc into dbcc/jmp absl long */
3243                 /* JF: these used to be fr_opcode[2-4], which is wrong. */
3244                 buffer_address[0] = 0x00;  /* branch offset = 4 */
3245                 buffer_address[1] = 0x04;  
3246                 buffer_address[2] = 0x60;  /* put in bra pc + ... */
3247  
3248                 if(flagseen['l']) {
3249                         /* JF: these were fr_opcode[5-7] */
3250                         buffer_address[3] = 0x04; /* plus 4 */
3251                         buffer_address[4] = 0x4e;/* Put in Jump Word */
3252                         buffer_address[5] = 0xf8;
3253                         fragP->fr_fix += 6;       /* account for bra/jmp instruction */
3254                         subseg_change(SEG_TEXT,0);
3255                         fix_new(fragP, fragP->fr_fix, 2, fragP->fr_symbol, 0, 
3256                                                          fragP->fr_offset, 0, NO_RELOC);
3257                         fragP->fr_fix += 2;
3258                 } else {
3259                         /* JF: these were fr_opcode[5-7] */
3260                         buffer_address[3] = 0x06;  /* Plus 6 */
3261                         buffer_address[4] = 0x4e;  /* put in jmp long (0x4ef9) */ 
3262                         buffer_address[5] = 0xf9;  
3263                         fragP->fr_fix += 6;       /* account for bra/jmp instruction */
3264                         subseg_change(SEG_TEXT,0);
3265                         fix_new(fragP, fragP->fr_fix, 4, fragP->fr_symbol, 0, 
3266                                                          fragP->fr_offset, 0, NO_RELOC);
3267                         fragP->fr_fix += 4;
3268                 }
3269
3270                 frag_wane(fragP);
3271                 break;
3272         } /* case TAB(DBCC,SZ_UNDEF) */
3273
3274         case TAB(BRANCH,SZ_UNDEF): {
3275                 if((fragP->fr_symbol != NULL)   /* Not absolute */
3276                    && S_GET_SEGMENT(fragP->fr_symbol) == segment) {
3277                         fragP->fr_subtype=TAB(TABTYPE(fragP->fr_subtype),BYTE);
3278                         break;
3279                 } else if((fragP->fr_symbol == 0) || (current_architecture <= m68010)) {
3280                         /* On 68000, or for absolute value, switch to abs long */
3281                         /* FIXME, we should check abs val, pick short or long */
3282                         if(fragP->fr_opcode[0]==0x61) {
3283                                 fragP->fr_opcode[0]= 0x4E;
3284                                 fragP->fr_opcode[1]= 0xB9;      /* JBSR with ABSL LONG offset */
3285                                 subseg_change(SEG_TEXT, 0);
3286                                 fix_new(fragP, fragP->fr_fix, 4, 
3287                                         fragP->fr_symbol, 0, fragP->fr_offset, 0, NO_RELOC);
3288                                 fragP->fr_fix+=4;
3289                                 frag_wane(fragP);
3290                         } else if(fragP->fr_opcode[0]==0x60) {
3291                                 fragP->fr_opcode[0]= 0x4E;
3292                                 fragP->fr_opcode[1]= 0xF9;  /* JMP  with ABSL LONG offset */
3293                                 subseg_change(SEG_TEXT, 0);
3294                                 fix_new(fragP, fragP->fr_fix, 4, 
3295                                         fragP->fr_symbol, 0, fragP->fr_offset, 0, NO_RELOC);
3296                                 fragP->fr_fix+=4;
3297                                 frag_wane(fragP);
3298                         } else {
3299                                 as_warn("Long branch offset to extern symbol not supported.");
3300                         }
3301                 } else {        /* Symbol is still undefined.  Make it simple */
3302                         fix_new(fragP, (int)(fragP->fr_fix), 4, fragP->fr_symbol,
3303                                 (symbolS *)0, fragP->fr_offset+4, 1, NO_RELOC);
3304                         fragP->fr_fix+=4;
3305                         fragP->fr_opcode[1]=0xff;
3306                         frag_wane(fragP);
3307                         break;
3308                 }
3309
3310                 break;
3311         } /* case TAB(BRANCH,SZ_UNDEF) */
3312
3313         case TAB(PCLEA,SZ_UNDEF): {
3314                 if ((S_GET_SEGMENT(fragP->fr_symbol))==segment || flagseen['l']) {
3315                         fragP->fr_subtype=TAB(PCLEA,SHORT);
3316                         fragP->fr_var+=2;
3317                 } else {
3318                         fragP->fr_subtype=TAB(PCLEA,LONG);
3319                         fragP->fr_var+=6;
3320                 }
3321                 break;
3322         } /* TAB(PCLEA,SZ_UNDEF) */
3323
3324         case TAB(PCREL,SZ_UNDEF): {
3325                 if(S_GET_SEGMENT(fragP->fr_symbol) == segment || flagseen['l']) {
3326                         fragP->fr_subtype = TAB(PCREL,SHORT);
3327                         fragP->fr_var += 2;
3328                 } else {
3329                         fragP->fr_subtype = TAB(PCREL,LONG);
3330                         fragP->fr_var += 4;
3331                 }
3332                 break;
3333         } /* TAB(PCREL,SZ_UNDEF) */
3334
3335         default:
3336                 break;
3337
3338         } /* switch on subtype looking for SZ_UNDEF's. */
3339
3340         /* now that SZ_UNDEF are taken care of, check others */
3341         switch(fragP->fr_subtype) {
3342         case TAB(BCC68000,BYTE):
3343         case TAB(BRANCH,BYTE):
3344                         /* We can't do a short jump to the next instruction,
3345                            so we force word mode.  */
3346                 if (fragP->fr_symbol && S_GET_VALUE(fragP->fr_symbol)==0 &&
3347  fragP->fr_symbol->sy_frag==fragP->fr_next) {
3348                         fragP->fr_subtype=TAB(TABTYPE(fragP->fr_subtype),SHORT);
3349                         fragP->fr_var+=2;
3350                 }
3351                 break;
3352         default:
3353                 break;
3354         }
3355         return fragP->fr_var + fragP->fr_fix - old_fix;
3356 }
3357
3358 #if defined(OBJ_AOUT) | defined(OBJ_BOUT)
3359 /* the bit-field entries in the relocation_info struct plays hell 
3360    with the byte-order problems of cross-assembly.  So as a hack,
3361    I added this mach. dependent ri twiddler.  Ugly, but it gets
3362    you there. -KWK */
3363 /* on m68k: first 4 bytes are normal unsigned long, next three bytes
3364 are symbolnum, most sig. byte first.  Last byte is broken up with
3365 bit 7 as pcrel, bits 6 & 5 as length, bit 4 as pcrel, and the lower
3366 nibble as nuthin. (on Sun 3 at least) */
3367 /* Translate the internal relocation information into target-specific
3368    format. */
3369 #ifdef comment
3370 void
3371 md_ri_to_chars(the_bytes, ri)
3372      char *the_bytes;
3373      struct reloc_info_generic *ri;
3374 {
3375   /* this is easy */
3376   md_number_to_chars(the_bytes, ri->r_address, 4);
3377   /* now the fun stuff */
3378   the_bytes[4] = (ri->r_symbolnum >> 16) & 0x0ff;
3379   the_bytes[5] = (ri->r_symbolnum >> 8) & 0x0ff;
3380   the_bytes[6] = ri->r_symbolnum & 0x0ff;
3381   the_bytes[7] = (((ri->r_pcrel << 7)  & 0x80) | ((ri->r_length << 5) & 0x60) | 
3382     ((ri->r_extern << 4)  & 0x10)); 
3383 }
3384 #endif /* comment */
3385
3386 void tc_aout_fix_to_chars(where, fixP, segment_address_in_file)
3387 char *where;
3388 fixS *fixP;
3389 relax_addressT segment_address_in_file;
3390 {
3391         /*
3392          * In: length of relocation (or of address) in chars: 1, 2 or 4.
3393          * Out: GNU LD relocation length code: 0, 1, or 2.
3394          */
3395         
3396         static unsigned char nbytes_r_length [] = { 42, 0, 1, 42, 2 };
3397         
3398         long r_extern;
3399         long r_symbolnum;
3400         
3401         /* this is easy */
3402         md_number_to_chars(where,
3403                            fixP->fx_frag->fr_address + fixP->fx_where - segment_address_in_file,
3404                            4);
3405         
3406         /* now the fun stuff */
3407         if (S_GET_TYPE(fixP->fx_addsy) == N_UNDF) {
3408                 r_extern        = 1;
3409                 r_symbolnum     = fixP->fx_addsy->sy_number;
3410         } else {
3411                 r_extern        = 0;
3412                 r_symbolnum     = S_GET_TYPE(fixP->fx_addsy);
3413         }
3414         
3415         where[4] = (r_symbolnum >> 16) & 0x0ff;
3416         where[5] = (r_symbolnum >> 8) & 0x0ff;
3417         where[6] = r_symbolnum & 0x0ff;
3418         where[7] = (((fixP->fx_pcrel << 7)  & 0x80) | ((nbytes_r_length[fixP->fx_size] << 5) & 0x60) | 
3419                     ((r_extern << 4)  & 0x10)); 
3420         
3421         return;
3422 } /* tc_aout_fix_to_chars() */
3423
3424 #endif /* OBJ_AOUT or OBJ_BOUT */
3425
3426 #ifndef WORKING_DOT_WORD
3427 const int md_short_jump_size = 4;
3428 const int md_long_jump_size = 6;
3429
3430 void
3431 md_create_short_jump(ptr,from_addr,to_addr,frag,to_symbol)
3432 char    *ptr;
3433 long    from_addr,
3434         to_addr;
3435 fragS   *frag;
3436 symbolS *to_symbol;
3437 {
3438         long offset;
3439
3440         offset = to_addr - (from_addr+2);
3441
3442         md_number_to_chars(ptr  ,(long)0x6000,2);
3443         md_number_to_chars(ptr+2,(long)offset,2);
3444 }
3445
3446 void
3447 md_create_long_jump(ptr,from_addr,to_addr,frag,to_symbol)
3448 char    *ptr;
3449 long    from_addr,
3450         to_addr;
3451 fragS   *frag;
3452 symbolS *to_symbol;
3453 {
3454         long offset;
3455
3456         if (current_architecture <= m68010) {
3457                 offset=to_addr-S_GET_VALUE(to_symbol);
3458                 md_number_to_chars(ptr  ,(long)0x4EF9,2);
3459                 md_number_to_chars(ptr+2,(long)offset,4);
3460                 fix_new(frag,(ptr+2)-frag->fr_literal,4,to_symbol,(symbolS *)0,(long)0,0,
3461                                 NO_RELOC);
3462         } else {
3463                 offset=to_addr - (from_addr+2);
3464                 md_number_to_chars(ptr  ,(long)0x60ff,2);
3465                 md_number_to_chars(ptr+2,(long)offset,4);
3466         }
3467 }
3468
3469 #endif
3470 /* Different values of OK tell what its OK to return.  Things that aren't OK are an error (what a shock, no?)
3471
3472         0:  Everything is OK
3473         10:  Absolute 1:8       only
3474         20:  Absolute 0:7       only
3475         30:  absolute 0:15      only
3476         40:  Absolute 0:31      only
3477         50:  absolute 0:127     only
3478         55:  absolute -64:63    only
3479         60:  absolute -128:127  only
3480         70:  absolute 0:4095    only
3481         80:  No bignums
3482
3483 */
3484
3485 static int get_num(exp,ok)
3486 struct m68k_exp *exp;
3487 int ok;
3488 {
3489 #ifdef TEST2
3490         long    l = 0;
3491
3492         if(!exp->e_beg)
3493                 return 0;
3494         if(*exp->e_beg=='0') {
3495                 if(exp->e_beg[1]=='x')
3496                         sscanf(exp->e_beg+2,"%x",&l);
3497                 else
3498                         sscanf(exp->e_beg+1,"%O",&l);
3499                 return l;
3500         }
3501         return atol(exp->e_beg);
3502 #else
3503         char    *save_in;
3504         char    c_save;
3505
3506         if(!exp) {
3507                 /* Can't do anything */
3508                 return 0;
3509         }
3510         if(!exp->e_beg || !exp->e_end) {
3511                 seg(exp)=SEG_ABSOLUTE;
3512                 adds(exp)=0;
3513                 subs(exp)=0;
3514                 offs(exp)= (ok==10) ? 1 : 0;
3515                 as_warn("Null expression defaults to %ld",offs(exp));
3516                 return 0;
3517         }
3518
3519         exp->e_siz=0;
3520         if(/* ok!=80 && */exp->e_end[-1]==':' && (exp->e_end-exp->e_beg)>=2) {
3521                 switch(exp->e_end[0]) {
3522                 case 's':
3523                 case 'S':
3524                 case 'b':
3525                 case 'B':
3526                         exp->e_siz=1;
3527                         break;
3528                 case 'w':
3529                 case 'W':
3530                         exp->e_siz=2;
3531                         break;
3532                 case 'l':
3533                 case 'L':
3534                         exp->e_siz=3;
3535                         break;
3536                 default:
3537                         as_bad("Unknown size for expression \"%c\"",exp->e_end[0]);
3538                 }
3539                 exp->e_end-=2;
3540         }
3541         c_save=exp->e_end[1];
3542         exp->e_end[1]='\0';
3543         save_in=input_line_pointer;
3544         input_line_pointer=exp->e_beg;
3545         switch(expression(&(exp->e_exp))) {
3546         case SEG_PASS1:
3547                 seg(exp)=SEG_ABSOLUTE;
3548                 adds(exp)=0;
3549                 subs(exp)=0;
3550                 offs(exp)= (ok==10) ? 1 : 0;
3551                 as_warn("Unknown expression: '%s' defaulting to %d",exp->e_beg,offs(exp));
3552                 break;
3553
3554         case SEG_ABSENT:
3555                 /* Do the same thing the VAX asm does */
3556                 seg(exp)=SEG_ABSOLUTE;
3557                 adds(exp)=0;
3558                 subs(exp)=0;
3559                 offs(exp)=0;
3560                 if(ok==10) {
3561                         as_warn("expression out of range: defaulting to 1");
3562                         offs(exp)=1;
3563                 }
3564                 break;
3565         case SEG_ABSOLUTE:
3566                 switch(ok) {
3567                 case 10:
3568                         if(offs(exp)<1 || offs(exp)>8) {
3569                                 as_warn("expression out of range: defaulting to 1");
3570                                 offs(exp)=1;
3571                         }
3572                         break;
3573                 case 20:
3574                         if(offs(exp)<0 || offs(exp)>7)
3575                                 goto outrange;
3576                         break;
3577                 case 30:
3578                         if(offs(exp)<0 || offs(exp)>15)
3579                                 goto outrange;
3580                         break;
3581                 case 40:
3582                         if(offs(exp)<0 || offs(exp)>32)
3583                                 goto outrange;
3584                         break;
3585                 case 50:
3586                         if(offs(exp)<0 || offs(exp)>127)
3587                                 goto outrange;
3588                         break;
3589                 case 55:
3590                         if(offs(exp)<-64 || offs(exp)>63)
3591                                 goto outrange;
3592                         break;
3593                 case 60:
3594                         if(offs(exp)<-128 || offs(exp)>127)
3595                                 goto outrange;
3596                         break;
3597                 case 70:
3598                         if(offs(exp)<0 || offs(exp)>4095) {
3599                         outrange:
3600                                 as_warn("expression out of range: defaulting to 0");
3601                                 offs(exp)=0;
3602                         }
3603                         break;
3604                 default:
3605                         break;
3606                 }
3607                 break;
3608         case SEG_TEXT:
3609         case SEG_DATA:
3610         case SEG_BSS:
3611         case SEG_UNKNOWN:
3612         case SEG_DIFFERENCE:
3613                 if(ok>=10 && ok<=70) {
3614                         seg(exp)=SEG_ABSOLUTE;
3615                         adds(exp)=0;
3616                         subs(exp)=0;
3617                         offs(exp)= (ok==10) ? 1 : 0;
3618                         as_warn("Can't deal with expression \"%s\": defaulting to %ld",exp->e_beg,offs(exp));
3619                 }
3620                 break;
3621         case SEG_BIG:
3622                 if(ok==80 && offs(exp)<0) {     /* HACK! Turn it into a long */
3623                         LITTLENUM_TYPE words[6];
3624
3625                         gen_to_words(words,2,8L);/* These numbers are magic! */
3626                         seg(exp)=SEG_ABSOLUTE;
3627                         adds(exp)=0;
3628                         subs(exp)=0;
3629                         offs(exp)=words[1]|(words[0]<<16);
3630                 } else if(ok!=0) {
3631                         seg(exp)=SEG_ABSOLUTE;
3632                         adds(exp)=0;
3633                         subs(exp)=0;
3634                         offs(exp)= (ok==10) ? 1 : 0;
3635                         as_warn("Can't deal with expression \"%s\": defaulting to %ld",exp->e_beg,offs(exp));
3636                 }
3637                 break;
3638         default:
3639                 as_fatal("failed sanity check.");
3640         }
3641         if(input_line_pointer!=exp->e_end+1)
3642                 as_bad("Ignoring junk after expression");
3643         exp->e_end[1]=c_save;
3644         input_line_pointer=save_in;
3645         if(exp->e_siz) {
3646                 switch(exp->e_siz) {
3647                 case 1:
3648                         if(!isbyte(offs(exp)))
3649                                 as_warn("expression doesn't fit in BYTE");
3650                         break;
3651                 case 2:
3652                         if(!isword(offs(exp)))
3653                                 as_warn("expression doesn't fit in WORD");
3654                         break;
3655                 }
3656         }
3657         return offs(exp);
3658 #endif
3659 } /* get_num() */
3660
3661 /* These are the back-ends for the various machine dependent pseudo-ops.  */
3662 void demand_empty_rest_of_line();       /* Hate those extra verbose names */
3663
3664 static void s_data1() {
3665         subseg_new(SEG_DATA,1);
3666         demand_empty_rest_of_line();
3667 } /* s_data1() */
3668
3669 static void s_data2() {
3670         subseg_new(SEG_DATA,2);
3671         demand_empty_rest_of_line();
3672 } /* s_data2() */
3673
3674 static void s_bss() {
3675         /* We don't support putting frags in the BSS segment, but we
3676            can put them into initialized data for now... */
3677         subseg_new(SEG_DATA,255);       /* FIXME-SOON */
3678         demand_empty_rest_of_line();
3679 } /* s_bss() */
3680
3681 static void s_even() {
3682         register int temp;
3683         register long temp_fill;
3684
3685         temp = 1;               /* JF should be 2? */
3686         temp_fill = get_absolute_expression ();
3687         if ( ! need_pass_2 ) /* Never make frag if expect extra pass. */
3688                 frag_align (temp, (int)temp_fill);
3689         demand_empty_rest_of_line();
3690 } /* s_even() */
3691
3692 static void s_proc() {
3693         demand_empty_rest_of_line();
3694 } /* s_proc() */
3695
3696 /* s_space is defined in read.c .skip is simply an alias to it. */
3697
3698 /*
3699  * md_parse_option
3700  *      Invocation line includes a switch not recognized by the base assembler.
3701  *      See if it's a processor-specific option.  These are:
3702  *
3703  *      -[A]m[c]68000, -[A]m[c]68008, -[A]m[c]68010, -[A]m[c]68020, -[A]m[c]68030, -[A]m[c]68040
3704  *      -[A]m[c]68881, -[A]m[c]68882, -[A]m[c]68851
3705  *              Select the architecture.  Instructions or features not
3706  *              supported by the selected architecture cause fatal
3707  *              errors.  More than one may be specified.  The default is
3708  *              -m68020 -m68851 -m68881.  Note that -m68008 is a synonym
3709  *              for -m68000, and -m68882 is a synonym for -m68881.
3710  *
3711  */
3712
3713 int md_parse_option(argP,cntP,vecP)
3714 char **argP;
3715 int *cntP;
3716 char ***vecP;
3717 {
3718         switch(**argP) {
3719         case 'l':       /* -l means keep external to 2 bit offset
3720                            rather than 16 bit one */
3721                 break;
3722
3723         case 'S': /* -S means that jbsr's always turn into jsr's.  */
3724                 break;
3725
3726         case 'A':
3727                 (*argP)++;
3728                 /* intentional fall-through */
3729         case 'm':
3730                 (*argP)++;
3731
3732                 if (**argP=='c') {
3733                         (*argP)++;
3734                 } /* allow an optional "c" */
3735
3736                 if (!strcmp(*argP, "68000")
3737                     || !strcmp(*argP, "68008")) {
3738                         current_architecture |= m68000;
3739                 } else if (!strcmp(*argP, "68010")) {
3740 #ifdef TE_SUN
3741                         omagic= 1<<16|OMAGIC;
3742 #endif
3743                         current_architecture |= m68010;
3744
3745                 } else if (!strcmp(*argP, "68020")) { 
3746                         current_architecture |= m68020;
3747
3748                 } else if (!strcmp(*argP, "68030")) { 
3749                         current_architecture |= m68030;
3750
3751                 } else if (!strcmp(*argP, "68040")) { 
3752                         current_architecture |= m68040;
3753
3754 #ifndef NO_68881
3755                 } else if (!strcmp(*argP, "68881")
3756                            || !strcmp(*argP, "68882")) { 
3757                         current_architecture |= m68040;
3758
3759 #endif /* NO_68881 */
3760 #ifndef NO_68851
3761                 } else if (!strcmp(*argP,"68851")) { 
3762                         current_architecture |= m68040;
3763
3764 #endif /* NO_68851 */
3765                 } else {
3766                         as_warn("Unknown architecture, \"%s\". option ignored", *argP);
3767                 } /* switch on architecture */
3768
3769                 while(**argP) (*argP)++;
3770
3771                 break;
3772
3773         case 'p':
3774                 if (!strcmp(*argP,"pic")) {
3775                         (*argP) += 3;
3776                         break;          /* -pic, Position Independent Code */
3777                 } else {
3778                         return(0);
3779                 } /* pic or not */
3780
3781         default:
3782                 return 0;
3783         }
3784         return 1;
3785 }
3786
3787
3788 #ifdef TEST2
3789
3790 /* TEST2:  Test md_assemble() */
3791 /* Warning, this routine probably doesn't work anymore */
3792
3793 main()
3794 {
3795         struct m68k_it the_ins;
3796         char buf[120];
3797         char *cp;
3798         int     n;
3799
3800         m68k_ip_begin();
3801         for(;;) {
3802                 if(!gets(buf) || !*buf)
3803                         break;
3804                 if(buf[0]=='|' || buf[1]=='.')
3805                         continue;
3806                 for(cp=buf;*cp;cp++)
3807                         if(*cp=='\t')
3808                                 *cp=' ';
3809                 if(is_label(buf))
3810                         continue;
3811                 bzero(&the_ins,sizeof(the_ins));
3812                 m68k_ip(&the_ins,buf);
3813                 if(the_ins.error) {
3814                         printf("Error %s in %s\n",the_ins.error,buf);
3815                 } else {
3816                         printf("Opcode(%d.%s): ",the_ins.numo,the_ins.args);
3817                         for(n=0;n<the_ins.numo;n++)
3818                                 printf(" 0x%x",the_ins.opcode[n]&0xffff);
3819                         printf("    ");
3820                         print_the_insn(&the_ins.opcode[0],stdout);
3821                         (void)putchar('\n');
3822                 }
3823                 for(n=0;n<strlen(the_ins.args)/2;n++) {
3824                         if(the_ins.operands[n].error) {
3825                                 printf("op%d Error %s in %s\n",n,the_ins.operands[n].error,buf);
3826                                 continue;
3827                         }
3828                         printf("mode %d, reg %d, ",the_ins.operands[n].mode,the_ins.operands[n].reg);
3829                         if(the_ins.operands[n].b_const)
3830                                 printf("Constant: '%.*s', ",1+the_ins.operands[n].e_const-the_ins.operands[n].b_const,the_ins.operands[n].b_const);
3831                         printf("ireg %d, isiz %d, imul %d, ",the_ins.operands[n].ireg,the_ins.operands[n].isiz,the_ins.operands[n].imul);
3832                         if(the_ins.operands[n].b_iadd)
3833                                 printf("Iadd: '%.*s',",1+the_ins.operands[n].e_iadd-the_ins.operands[n].b_iadd,the_ins.operands[n].b_iadd);
3834                         (void)putchar('\n');
3835                 }
3836         }
3837         m68k_ip_end();
3838         return 0;
3839 }
3840
3841 is_label(str)
3842 char *str;
3843 {
3844         while(*str==' ')
3845                 str++;
3846         while(*str && *str!=' ')
3847                 str++;
3848         if(str[-1]==':' || str[1]=='=')
3849                 return 1;
3850         return 0;
3851 }
3852
3853 #endif
3854
3855 /* Possible states for relaxation:
3856
3857 0 0     branch offset   byte    (bra, etc)
3858 0 1                     word
3859 0 2                     long
3860
3861 1 0     indexed offsets byte    a0@(32,d4:w:1) etc
3862 1 1                     word
3863 1 2                     long
3864
3865 2 0     two-offset index word-word a0@(32,d4)@(45) etc
3866 2 1                     word-long
3867 2 2                     long-word
3868 2 3                     long-long
3869
3870 */
3871
3872
3873
3874 #ifdef DONTDEF
3875 abort()
3876 {
3877         printf("ABORT!\n");
3878         exit(12);
3879 }
3880
3881 char *index(s,c)
3882 char *s;
3883 {
3884         while(*s!=c) {
3885                 if(!*s) return 0;
3886                 s++;
3887         }
3888         return s;
3889 }
3890
3891 bzero(s,n)
3892 char *s;
3893 {
3894         while(n--)
3895                 *s++=0;
3896 }
3897
3898 print_frags()
3899 {
3900         fragS *fragP;
3901         extern fragS *text_frag_root;
3902
3903         for(fragP=text_frag_root;fragP;fragP=fragP->fr_next) {
3904                 printf("addr %lu  next 0x%x  fix %ld  var %ld  symbol 0x%x  offset %ld\n",
3905  fragP->fr_address,fragP->fr_next,fragP->fr_fix,fragP->fr_var,fragP->fr_symbol,fragP->fr_offset);
3906                 printf("opcode 0x%x  type %d  subtype %d\n\n",fragP->fr_opcode,fragP->fr_type,fragP->fr_subtype);
3907         }
3908         fflush(stdout);
3909         return 0;
3910 }
3911 #endif
3912
3913 #ifdef DONTDEF
3914 /*VARARGS1*/
3915 panic(format,args)
3916 char *format;
3917 {
3918         fputs("Internal error:",stderr);
3919         _doprnt(format,&args,stderr);
3920         (void)putc('\n',stderr);
3921         as_where();
3922         abort();
3923 }
3924 #endif
3925
3926 /* We have no need to default values of symbols.  */
3927
3928 /* ARGSUSED */
3929 symbolS *
3930 md_undefined_symbol (name)
3931      char *name;
3932 {
3933   return 0;
3934 }
3935
3936 /* Parse an operand that is machine-specific.  
3937    We just return without modifying the expression if we have nothing
3938    to do.  */
3939
3940 /* ARGSUSED */
3941 void
3942 md_operand (expressionP)
3943      expressionS *expressionP;
3944 {
3945 }
3946
3947 /* Round up a section size to the appropriate boundary.  */
3948 long
3949 md_section_align (segment, size)
3950      segT segment;
3951      long size;
3952 {
3953   return size;          /* Byte alignment is fine */
3954 }
3955
3956 /* Exactly what point is a PC-relative offset relative TO?
3957    On the 68k, they're relative to the address of the offset, plus
3958    its size. (??? Is this right?  FIXME-SOON!) */
3959 long
3960 md_pcrel_from (fixP)
3961      fixS *fixP;
3962 {
3963   return fixP->fx_size + fixP->fx_where + fixP->fx_frag->fr_address;
3964 }
3965
3966 /*
3967  * Local Variables:
3968  * comment-column: 0
3969  * fill-column: 131
3970  * End:
3971  */
3972
3973 /* end of tc-m68k.c */
This page took 0.320737 seconds and 4 git commands to generate.