-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathm68k.c
3540 lines (3222 loc) · 78.5 KB
/
m68k.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* m68k.c All the m68020 specific stuff in one convenient, huge,
slow to compile, easy to find file.
Copyright (C) 1987 Free Software Foundation, Inc.
This file is part of GAS, the GNU Assembler.
GAS is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 1, or (at your option)
any later version.
GAS is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with GAS; see the file COPYING. If not, write to
the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
#include <ctype.h>
#include "m68k-opcode.h"
#include "as.h"
#include "obstack.h"
#include "frags.h"
#include "struc-symbol.h"
#include "flonum.h"
#include "expr.h"
#include "hash.h"
#include "md.h"
#include "m68k.h"
#ifdef M_SUN
/* This variable contains the value to write out at the beginning of
the a.out file. The 2<<16 means that this is a 68020 file instead
of an old-style 68000 file */
long omagic = 2<<16|OMAGIC; /* Magic byte for header file */
#else
long omagic = OMAGIC;
#endif
/* This array holds the chars that always start a comment. If the
pre-processor is disabled, these aren't very useful */
const char comment_chars[] = "|";
/* This array holds the chars that only start a comment at the beginning of
a line. If the line seems to have the form '# 123 filename'
.line and .file directives will appear in the pre-processed output */
/* Note that input_file.c hand checks for '#' at the beginning of the
first line of the input file. This is because the compiler outputs
#NO_APP at the beginning of its output. */
/* Also note that '/*' will always start a comment */
const char line_comment_chars[] = "#";
/* Chars that can be used to separate mant from exp in floating point nums */
const char EXP_CHARS[] = "eE";
/* Chars that mean this number is a floating point constant */
/* As in 0f12.456 */
/* or 0d1.2345e12 */
const char FLT_CHARS[] = "rRsSfFdDxXeEpP";
/* Also be aware that MAXIMUM_NUMBER_OF_CHARS_FOR_FLOAT may have to be
changed in read.c . Ideally it shouldn't have to know about it at all,
but nothing is ideal around here.
*/
void fix_new();
void install_operand();
void install_gen_operand();
/* Its an arbitrary name: This means I don't approve of it */
/* See flames below */
struct obstack robyn;
#define TAB(x,y) (((x)<<2)+(y))
#define TABTYPE(xy) ((xy) >> 2)
#define BYTE 0
#define SHORT 1
#define LONG 2
#define SZ_UNDEF 3
#define BRANCH 1
#define FBRANCH 2
#define PCREL 3
#define BCC68000 4
#define DBCC 5
#define PCLEA 6
/* BCC68000 is for patching in an extra jmp instruction for long offsets
on the 68000. The 68000 doesn't support long branches with branchs */
/* This table desribes how you change sizes for the various types of variable
size expressions. This version only supports two kinds. */
/* Note that calls to frag_var need to specify the maximum expansion needed */
/* This is currently 10 bytes for DBCC */
/* The fields are:
How far Forward this mode will reach:
How far Backward this mode will reach:
How many bytes this mode will add to the size of the frag
Which mode to go to if the offset won't fit in this one
*/
const relax_typeS
md_relax_table[] = {
{ 1, 1, 0, 0 }, /* First entries aren't used */
{ 1, 1, 0, 0 }, /* For no good reason except */
{ 1, 1, 0, 0 }, /* that the VAX doesn't either */
{ 1, 1, 0, 0 },
{ (127), (-128), 0, TAB(BRANCH,SHORT)},
{ (32767), (-32768), 2, TAB(BRANCH,LONG) },
{ 0, 0, 4, 0 },
{ 1, 1, 0, 0 },
{ 1, 1, 0, 0 }, /* FBRANCH doesn't come BYTE */
{ (32767), (-32768), 2, TAB(FBRANCH,LONG)},
{ 0, 0, 4, 0 },
{ 1, 1, 0, 0 },
{ 1, 1, 0, 0 }, /* PCREL doesn't come BYTE */
{ (32767), (-32768), 2, TAB(PCREL,LONG)},
{ 0, 0, 4, 0 },
{ 1, 1, 0, 0 },
{ (127), (-128), 0, TAB(BCC68000,SHORT)},
{ (32767), (-32768), 2, TAB(BCC68000,LONG) },
{ 0, 0, 6, 0 }, /* jmp long space */
{ 1, 1, 0, 0 },
{ 1, 1, 0, 0 }, /* DBCC doesn't come BYTE */
{ (32767), (-32768), 2, TAB(DBCC,LONG) },
{ 0, 0, 10, 0 }, /* bra/jmp long space */
{ 1, 1, 0, 0 },
{ 1, 1, 0, 0 }, /* PCLEA doesn't come BYTE */
{ 32767, -32768, 2, TAB(PCLEA,LONG) },
{ 0, 0, 6, 0 },
{ 1, 1, 0, 0 },
};
void s_data1(), s_data2(), s_even(), s_space();
void s_proc(), float_cons();
/* These are the machine dependent pseudo-ops. These are included so
the assembler can work on the output from the SUN C compiler, which
generates these.
*/
/* This table describes all the machine specific pseudo-ops the assembler
has to support. The fields are:
pseudo-op name without dot
function to call to execute this pseudo-op
Integer arg to pass to the function
*/
const pseudo_typeS md_pseudo_table[] = {
{ "data1", s_data1, 0 },
{ "data2", s_data2, 0 },
{ "even", s_even, 0 },
{ "skip", s_space, 0 },
{ "proc", s_proc, 0 },
{ 0, 0, 0 }
};
/* #define isbyte(x) ((x)>=-128 && (x)<=127) */
/* #define isword(x) ((x)>=-32768 && (x)<=32767) */
#define issbyte(x) ((x)>=-128 && (x)<=127)
#define isubyte(x) ((x)>=0 && (x)<=255)
#define issword(x) ((x)>=-32768 && (x)<=32767)
#define isuword(x) ((x)>=0 && (x)<=65535)
#define isbyte(x) ((x)>=-128 && (x)<=255)
#define isword(x) ((x)>=-32768 && (x)<=65535)
#define islong(x) (1)
extern char *input_line_pointer;
/* Operands we can parse: (And associated modes)
numb: 8 bit num
numw: 16 bit num
numl: 32 bit num
dreg: data reg 0-7
reg: address or data register
areg: address register
apc: address register, PC, ZPC or empty string
num: 16 or 32 bit num
num2: like num
sz: w or l if omitted, l assumed
scale: 1 2 4 or 8 if omitted, 1 assumed
7.4 IMMED #num --> NUM
0.? DREG dreg --> dreg
1.? AREG areg --> areg
2.? AINDR areg@ --> *(areg)
3.? AINC areg@+ --> *(areg++)
4.? ADEC areg@- --> *(--areg)
5.? AOFF apc@(numw) --> *(apc+numw) -- empty string and ZPC not allowed here
6.? AINDX apc@(num,reg:sz:scale) --> *(apc+num+reg*scale)
6.? AINDX apc@(reg:sz:scale) --> same, with num=0
6.? APODX apc@(num)@(num2,reg:sz:scale) --> *(*(apc+num)+num2+reg*scale)
6.? APODX apc@(num)@(reg:sz:scale) --> same, with num2=0
6.? AMIND apc@(num)@(num2) --> *(*(apc+num)+num2) (previous mode without an index reg)
6.? APRDX apc@(num,reg:sz:scale)@(num2) --> *(*(apc+num+reg*scale)+num2)
6.? APRDX apc@(reg:sz:scale)@(num2) --> same, with num=0
7.0 ABSL num:sz --> *(num)
num --> *(num) (sz L assumed)
*** MSCR otherreg --> Magic
With -l option
5.? AOFF apc@(num) --> *(apc+num) -- empty string and ZPC not allowed here still
examples:
#foo #0x35 #12
d2
a4
a3@
a5@+
a6@-
a2@(12) pc@(14)
a1@(5,d2:w:1) @(45,d6:l:4)
pc@(a2) @(d4)
etc . . .
#name@(numw) -->turn into PC rel mode
apc@(num8,reg:sz:scale) --> *(apc+num8+reg*scale)
*/
#define IMMED 1
#define DREG 2
#define AREG 3
#define AINDR 4
#define ADEC 5
#define AINC 6
#define AOFF 7
#define AINDX 8
#define APODX 9
#define AMIND 10
#define APRDX 11
#define ABSL 12
#define MSCR 13
#define REGLST 14
#define FAIL 0
#define OK 1
/* DATA and ADDR have to be contiguous, so that reg-DATA gives 0-7==data reg,
8-15==addr reg for operands that take both types */
#define DATA 1 /* 1- 8 == data registers 0-7 */
#define ADDR (DATA+8) /* 9-16 == address regs 0-7 */
#define FPREG (ADDR+8) /* 17-24 Eight FP registers */
#define COPNUM (FPREG+8) /* 25-32 Co-processor #1-#8 */
#define PC (COPNUM+8) /* 33 Program counter */
#define ZPC (PC+1) /* 34 Hack for Program space, but 0 addressing */
#define SR (ZPC+1) /* 35 Status Reg */
#define CCR (SR+1) /* 36 Condition code Reg */
/* These have to be in order for the movec instruction to work. */
#define USP (CCR+1) /* 37 User Stack Pointer */
#define ISP (USP+1) /* 38 Interrupt stack pointer */
#define SFC (ISP+1) /* 39 */
#define DFC (SFC+1) /* 40 */
#define CACR (DFC+1) /* 41 */
#define VBR (CACR+1) /* 42 */
#define CAAR (VBR+1) /* 43 */
#define MSP (CAAR+1) /* 44 */
#define FPI (MSP+1) /* 45 */
#define FPS (FPI+1) /* 46 */
#define FPC (FPS+1) /* 47 */
/*
* these defines should be in m68k.c but
* i put them here to keep all the m68851 stuff
* together -rab
* JF--Make sure these #s don't clash with the ones in m68k.c
* That would be BAD.
*/
#define TC (FPC+1) /* 48 */
#define DRP (TC+1) /* 49 */
#define SRP (DRP+1) /* 50 */
#define CRP (SRP+1) /* 51 */
#define CAL (CRP+1) /* 52 */
#define VAL (CAL+1) /* 53 */
#define SCC (VAL+1) /* 54 */
#define AC (SCC+1) /* 55 */
#define BAD (AC+1) /* 56,57,58,59, 60,61,62,63 */
#define BAC (BAD+8) /* 64,65,66,67, 68,69,70,71 */
#define PSR (BAC+8) /* 72 */
#define PCSR (PSR+1) /* 73 */
/* Note that COPNUM==processor #1 -- COPNUM+7==#8, which stores as 000 */
/* I think. . . */
#define SP ADDR+7
/* JF these tables here are for speed at the expense of size */
/* You can replace them with the #if 0 versions if you really
need space and don't mind it running a bit slower */
static char mklower_table[256];
#define mklower(c) (mklower_table[(unsigned char)(c)])
static char notend_table[256];
static char alt_notend_table[256];
#define notend(s) ( !(notend_table[(unsigned char)(*s)] || (*s==':' &&\
alt_notend_table[(unsigned char)(s[1])])))
#if 0
#define mklower(c) (isupper(c) ? tolower(c) : c)
#endif
struct m68k_exp {
char *e_beg;
char *e_end;
expressionS e_exp;
short e_siz; /* 0== default 1==short/byte 2==word 3==long */
};
/* Internal form of an operand. */
struct m68k_op {
char *error; /* Couldn't parse it */
int mode; /* What mode this instruction is in. */
unsigned long int reg; /* Base register */
struct m68k_exp *con1;
int ireg; /* Index register */
int isiz; /* 0==unspec 1==byte(?) 2==short 3==long */
int imul; /* Multipy ireg by this (1,2,4,or 8) */
struct m68k_exp *con2;
};
/* internal form of a 68020 instruction */
struct m68_it {
char *error;
char *args; /* list of opcode info */
int numargs;
int numo; /* Number of shorts in opcode */
short opcode[11];
struct m68k_op operands[6];
int nexp; /* number of exprs in use */
struct m68k_exp exprs[4];
int nfrag; /* Number of frags we have to produce */
struct {
int fragoff; /* Where in the current opcode[] the frag ends */
symbolS *fadd;
long int foff;
int fragty;
} fragb[4];
int nrel; /* Num of reloc strucs in use */
struct {
int n;
symbolS *add,
*sub;
long int off;
char wid;
char pcrel;
} reloc[5]; /* Five is enough??? */
};
struct m68_it the_ins; /* the instruction being assembled */
/* Macros for adding things to the m68_it struct */
#define addword(w) the_ins.opcode[the_ins.numo++]=(w)
/* Like addword, but goes BEFORE general operands */
#define insop(w) {int z;\
for(z=the_ins.numo;z>opcode->m_codenum;--z)\
the_ins.opcode[z]=the_ins.opcode[z-1];\
for(z=0;z<the_ins.nrel;z++)\
the_ins.reloc[z].n+=2;\
the_ins.opcode[opcode->m_codenum]=w;\
the_ins.numo++;\
}
#define add_exp(beg,end) (\
the_ins.exprs[the_ins.nexp].e_beg=beg,\
the_ins.exprs[the_ins.nexp].e_end=end,\
&the_ins.exprs[the_ins.nexp++]\
)
/* The numo+1 kludge is so we can hit the low order byte of the prev word. Blecch*/
#define add_fix(width,exp,pc_rel) {\
the_ins.reloc[the_ins.nrel].n= ((width)=='B') ? (the_ins.numo*2-1) : \
(((width)=='b') ? ((the_ins.numo-1)*2) : (the_ins.numo*2));\
the_ins.reloc[the_ins.nrel].add=adds((exp));\
the_ins.reloc[the_ins.nrel].sub=subs((exp));\
the_ins.reloc[the_ins.nrel].off=offs((exp));\
the_ins.reloc[the_ins.nrel].wid=width;\
the_ins.reloc[the_ins.nrel++].pcrel=pc_rel;\
}
#define add_frag(add,off,type) {\
the_ins.fragb[the_ins.nfrag].fragoff=the_ins.numo;\
the_ins.fragb[the_ins.nfrag].fadd=add;\
the_ins.fragb[the_ins.nfrag].foff=off;\
the_ins.fragb[the_ins.nfrag++].fragty=type;\
}
#define isvar(exp) ((exp) && (adds(exp) || subs(exp)))
#define seg(exp) ((exp)->e_exp.X_seg)
#define adds(exp) ((exp)->e_exp.X_add_symbol)
#define subs(exp) ((exp)->e_exp.X_subtract_symbol)
#define offs(exp) ((exp)->e_exp.X_add_number)
struct m68_incant {
char *m_operands;
unsigned long m_opcode;
short m_opnum;
short m_codenum;
struct m68_incant *m_next;
};
#define getone(x) ((((x)->m_opcode)>>16)&0xffff)
#define gettwo(x) (((x)->m_opcode)&0xffff)
/* JF modified this to handle cases where the first part of a symbol name
looks like a register */
int
m68k_reg_parse(ccp)
register char **ccp;
{
register char c1,
c2,
c3,
c4;
register int n = 0,
ret = FAIL;
c1=mklower(ccp[0][0]);
#ifdef REGISTER_PREFIX
if(c1!=REGISTER_PREFIX)
return FAIL;
c1=mklower(ccp[0][1]);
c2=mklower(ccp[0][2]);
c3=mklower(ccp[0][3]);
c4=mklower(ccp[0][4]);
#else
c2=mklower(ccp[0][1]);
c3=mklower(ccp[0][2]);
c4=mklower(ccp[0][3]);
#endif
switch(c1) {
case 'a':
if(c2>='0' && c2<='7') {
n=2;
ret=ADDR+c2-'0';
}
#ifdef m68851
else if (c2 == 'c') {
n = 2;
ret = AC;
}
#endif
break;
#ifdef m68851
case 'b':
if (c2 == 'a') {
if (c3 == 'd') {
if (c4 >= '0' && c4 <= '7') {
n = 4;
ret = BAD + c4 - '0';
}
}
if (c3 == 'c') {
if (c4 >= '0' && c4 <= '7') {
n = 4;
ret = BAC + c4 - '0';
}
}
}
break;
#endif
case 'c':
#ifdef m68851
if (c2 == 'a' && c3 == 'l') {
n = 3;
ret = CAL;
} else
#endif
/* This supports both CCR and CC as the ccr reg. */
if(c2=='c' && c3=='r') {
n=3;
ret = CCR;
} else if(c2=='c') {
n=2;
ret = CCR;
} else if(c2=='a' && (c3=='a' || c3=='c') && c4=='r') {
n=4;
ret = c3=='a' ? CAAR : CACR;
}
#ifdef m68851
else if (c2 == 'r' && c3 == 'p') {
n = 3;
ret = (CRP);
}
#endif
break;
case 'd':
if(c2>='0' && c2<='7') {
n=2;
ret = DATA+c2-'0';
} else if(c2=='f' && c3=='c') {
n=3;
ret = DFC;
}
#ifdef m68851
else if (c2 == 'r' && c3 == 'p') {
n = 3;
ret = (DRP);
}
#endif
break;
case 'f':
if(c2=='p') {
if(c3>='0' && c3<='7') {
n=3;
ret = FPREG+c3-'0';
if(c4==':')
ccp[0][3]=',';
} else if(c3=='i') {
n=3;
ret = FPI;
} else if(c3=='s') {
n= (c4 == 'r' ? 4 : 3);
ret = FPS;
} else if(c3=='c') {
n= (c4 == 'r' ? 4 : 3);
ret = FPC;
}
}
break;
case 'i':
if(c2=='s' && c3=='p') {
n=3;
ret = ISP;
}
break;
case 'm':
if(c2=='s' && c3=='p') {
n=3;
ret = MSP;
}
break;
case 'p':
if(c2=='c') {
#ifdef m68851
if(c3 == 's' && c4=='r') {
n=4;
ret = (PCSR);
} else
#endif
{
n=2;
ret = PC;
}
}
#ifdef m68851
else if (c2 == 's' && c3 == 'r') {
n = 3;
ret = (PSR);
}
#endif
break;
case 's':
#ifdef m68851
if (c2 == 'c' && c3 == 'c') {
n = 3;
ret = (SCC);
} else if (c2 == 'r' && c3 == 'p') {
n = 3;
ret = (SRP);
} else
#endif
if(c2=='r') {
n=2;
ret = SR;
} else if(c2=='p') {
n=2;
ret = ADDR+7;
} else if(c2=='f' && c3=='c') {
n=3;
ret = SFC;
}
break;
#ifdef m68851
case 't':
if(c2 == 'c') {
n=2;
ret=TC;
}
break;
#endif
case 'u':
if(c2=='s' && c3=='p') {
n=3;
ret = USP;
}
break;
case 'v':
#ifdef m68851
if (c2 == 'a' && c3 == 'l') {
n = 3;
ret = (VAL);
} else
#endif
if(c2=='b' && c3=='r') {
n=3;
ret = VBR;
}
break;
case 'z':
if(c2=='p' && c3=='c') {
n=3;
ret = ZPC;
}
break;
default:
break;
}
if(n) {
#ifdef REGISTER_PREFIX
n++;
#endif
if(isalnum(ccp[0][n]) || ccp[0][n]=='_')
ret=FAIL;
else
ccp[0]+=n;
} else
ret = FAIL;
return ret;
}
#define SKIP_WHITE() { str++; if(*str==' ') str++;}
int
m68k_ip_op(str,opP)
char *str;
register struct m68k_op *opP;
{
char *strend;
long i;
char *parse_index();
if(*str==' ')
str++;
/* Find the end of the string */
if(!*str) {
/* Out of gas */
opP->error="Missing operand";
return FAIL;
}
for(strend=str;*strend;strend++)
;
--strend;
/* Guess what: A constant. Shar and enjoy */
if(*str=='#') {
str++;
opP->con1=add_exp(str,strend);
opP->mode=IMMED;
return OK;
}
i=m68k_reg_parse(&str);
if((i==FAIL || *str!='\0') && *str!='@') {
char *stmp;
char *index();
if(i!=FAIL && (*str=='/' || *str=='-')) {
opP->mode=REGLST;
return get_regs(i,str,opP);
}
if(stmp=index(str,'@')) {
opP->con1=add_exp(str,stmp-1);
if(stmp==strend) {
opP->mode=AINDX;
return OK;
}
stmp++;
if(*stmp++!='(' || *strend--!=')') {
opP->error="Malformed operand";
return FAIL;
}
i=try_index(&stmp,opP);
opP->con2=add_exp(stmp,strend);
if(i==FAIL) opP->mode=AMIND;
else opP->mode=APODX;
return OK;
}
opP->mode=ABSL;
opP->con1=add_exp(str,strend);
return OK;
}
opP->reg=i;
if(*str=='\0') {
if(i>=DATA+0 && i<=DATA+7)
opP->mode=DREG;
else if(i>=ADDR+0 && i<=ADDR+7)
opP->mode=AREG;
else
opP->mode=MSCR;
return OK;
}
if((i<ADDR+0 || i>ADDR+7) && i!=PC && i!=ZPC && i!=FAIL) { /* Can't indirect off non address regs */
opP->error="Invalid indirect register";
return FAIL;
}
if(*str!='@')
abort();
str++;
switch(*str) {
case '\0':
opP->mode=AINDR;
return OK;
case '-':
opP->mode=ADEC;
return OK;
case '+':
opP->mode=AINC;
return OK;
case '(':
str++;
break;
default:
opP->error="Junk after indirect";
return FAIL;
}
/* Some kind of indexing involved. Lets find out how bad it is */
i=try_index(&str,opP);
/* Didn't start with an index reg, maybe its offset or offset,reg */
if(i==FAIL) {
char *beg_str;
beg_str=str;
for(i=1;i;) {
switch(*str++) {
case '\0':
opP->error="Missing )";
return FAIL;
case ',': i=0; break;
case '(': i++; break;
case ')': --i; break;
}
}
/* if(str[-3]==':') {
int siz;
switch(str[-2]) {
case 'b':
case 'B':
siz=1;
break;
case 'w':
case 'W':
siz=2;
break;
case 'l':
case 'L':
siz=3;
break;
default:
opP->error="Specified size isn't :w or :l";
return FAIL;
}
opP->con1=add_exp(beg_str,str-4);
opP->con1->e_siz=siz;
} else */
opP->con1=add_exp(beg_str,str-2);
/* Should be offset,reg */
if(str[-1]==',') {
i=try_index(&str,opP);
if(i==FAIL) {
opP->error="Malformed index reg";
return FAIL;
}
}
}
/* We've now got offset) offset,reg) or reg) */
if(*str=='\0') {
/* Th-the-thats all folks */
if(opP->reg==FAIL) opP->mode=AINDX; /* Other form of indirect */
else if(opP->ireg==FAIL) opP->mode=AOFF;
else opP->mode=AINDX;
return OK;
}
/* Next thing had better be another @ */
if(*str!='@' || str[1]!='(') {
opP->error="junk after indirect";
return FAIL;
}
str+=2;
if(opP->ireg!=FAIL) {
opP->mode=APRDX;
i=try_index(&str,opP);
if(i!=FAIL) {
opP->error="Two index registers! not allowed!";
return FAIL;
}
} else
i=try_index(&str,opP);
if(i==FAIL) {
char *beg_str;
beg_str=str;
for(i=1;i;) {
switch(*str++) {
case '\0':
opP->error="Missing )";
return FAIL;
case ',': i=0; break;
case '(': i++; break;
case ')': --i; break;
}
}
opP->con2=add_exp(beg_str,str-2);
if(str[-1]==',') {
if(opP->ireg!=FAIL) {
opP->error="Can't have two index regs";
return FAIL;
}
i=try_index(&str,opP);
if(i==FAIL) {
opP->error="malformed index reg";
return FAIL;
}
opP->mode=APODX;
} else if(opP->ireg!=FAIL)
opP->mode=APRDX;
else
opP->mode=AMIND;
} else
opP->mode=APODX;
if(*str!='\0') {
opP->error="Junk after indirect";
return FAIL;
}
return OK;
}
int
try_index(s,opP)
char **s;
struct m68k_op *opP;
{
register int i;
char *ss;
#define SKIP_W() { ss++; if(*ss==' ') ss++;}
ss= *s;
/* SKIP_W(); */
i=m68k_reg_parse(&ss);
if(!(i>=DATA+0 && i<=ADDR+7)) { /* if i is not DATA or ADDR reg */
*s=ss;
return FAIL;
}
opP->ireg=i;
/* SKIP_W(); */
if(*ss==')') {
opP->isiz=0;
opP->imul=1;
SKIP_W();
*s=ss;
return OK;
}
if(*ss!=':') {
opP->error="Missing : in index register";
*s=ss;
return FAIL;
}
SKIP_W();
switch(*ss) {
case 'w':
case 'W':
opP->isiz=2;
break;
case 'l':
case 'L':
opP->isiz=3;
break;
default:
opP->error="Index register size spec not :w or :l";
*s=ss;
return FAIL;
}
SKIP_W();
if(*ss==':') {
SKIP_W();
switch(*ss) {
case '1':
case '2':
case '4':
case '8':
opP->imul= *ss-'0';
break;
default:
opP->error="index multiplier not 1, 2, 4 or 8";
*s=ss;
return FAIL;
}
SKIP_W();
} else opP->imul=1;
if(*ss!=')') {
opP->error="Missing )";
*s=ss;
return FAIL;
}
SKIP_W();
*s=ss;
return OK;
}
#ifdef TEST1 /* TEST1 tests m68k_ip_op(), which parses operands */
main()
{
char buf[128];
struct m68k_op thark;
for(;;) {
if(!gets(buf))
break;
bzero(&thark,sizeof(thark));
if(!m68k_ip_op(buf,&thark)) printf("FAIL:");
if(thark.error)
printf("op1 error %s in %s\n",thark.error,buf);
printf("mode %d, reg %d, ",thark.mode,thark.reg);
if(thark.b_const)
printf("Constant: '%.*s',",1+thark.e_const-thark.b_const,thark.b_const);
printf("ireg %d, isiz %d, imul %d ",thark.ireg,thark.isiz,thark.imul);
if(thark.b_iadd)
printf("Iadd: '%.*s'",1+thark.e_iadd-thark.b_iadd,thark.b_iadd);
printf("\n");
}
exit(0);
}
#endif
static struct hash_control* op_hash = NULL; /* handle of the OPCODE hash table
NULL means any use before m68_ip_begin()
will crash */
/*
* m 6 8 _ i p ( )
*
* This converts a string into a 68k instruction.
* The string must be a bare single instruction in sun format
* with RMS-style 68020 indirects
* (example: )
*
* It provides some error messages: at most one fatal error message (which
* stops the scan) and at most one warning message for each operand.
* The 68k instruction is returned in exploded form, since we have no
* knowledge of how you parse (or evaluate) your expressions.
* We do however strip off and decode addressing modes and operation
* mnemonic.
*
* This function's value is a string. If it is not "" then an internal
* logic error was found: read this code to assign meaning to the string.
* No argument string should generate such an error string:
* it means a bug in our code, not in the user's text.
*
* You MUST have called m86_ip_begin() once and m86_ip_end() never before using
* this function.
*/
/* JF this function no longer returns a useful value. Sorry */
void
m68_ip (instring)
char *instring;
{
register char *p;
register struct m68k_op *opP;
register struct m68_incant *opcode;
register char *s;
register int tmpreg,