-
Notifications
You must be signed in to change notification settings - Fork 1
/
m682types.c
2904 lines (2819 loc) · 93.2 KB
/
m682types.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
/*
m682types.c - Part of macxx, a cross assembler family for various micro-processors
Copyright (C) 2008 David Shepperd
This program 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 3 of the License, or
(at your option) any later version.
This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
*/
/******************************************************************************
code emitters
There are 20+ different types of instructions in the 68020.
An instruction type may ultimately be one of several different
forms. An instruction type with a number in its name is a basic
type. If a type routine has a letter in its name it is a sub type.
Basic types will, if necessary, call subtypes to finish formating
an instruction.
******************************************************************************/
#include <stdio.h>
#include <ctype.h>
#include "token.h"
#include "m682k.h"
#include "exproper.h"
#include "listctrl.h"
#include "pst_tokens.h"
/* extern unsigned short eafield[]; */
static char ea_to_tag[] = {'b','I','L'};
int typeF( unsigned short opcode, EA *tsea, int bwl, EA *tdea);
typedef enum
{
AMFLG_none= 0x00, /* no special addressing */
AMFLG_autodec= 0x01, /* found a leading minus (autodecrement mode) */
AMFLG_autoinc= 0x02, /* found a trailing plus sign */
AMFLG_bdisr= 0x08, /* bd is a register */
AMFLG_bracket= 0x10, /* found an open square bracket */
AMFLG_openpar= 0x20, /* found an open paren */
AMFLG_preindx= 0x40, /* address mode is preindex indirect */
AMFLG_postindx=0x80 /* address mode is postindex indirect */
} AMflg;
enum gt_arg
{
GTERM_ANY=0, /* allow anything */
GTERM_D=1, /* D register */
GTERM_A=2, /* A register */
GTERM_PC=4 /* PC register */
};
/***********************************************************************************
* Pickup a term. Each item in the operand is parsed and verified against the type
* argument.
*/
static int get_term(EA *amp, int type, EXP_stk *eps, char *msg )
/* At entry:
* amp - ptr to operand struct
* type - type of argument allowed: see GTERM_xxx enum
* eps - ptr to expression stack into which to build the term
* msg - ptr to message string to output if an error occurs
* At exit:
* returns true if the term was valid otherwise returns false
*/
{
int i,treg,err=0;
if (*inp_ptr == ',')
{ /* if we're parked on a comma, then we have a null expression */
eps->ptr = 0;
return 0;
}
if (get_token() == EOL)
{ /* prime the pump */
if (msg) bad_token(tkn_ptr,msg);
return 0;
}
i = exprs(type?0:1,eps); /* anything is legal */
if (i < 0)
{
if (msg) bad_token(tkn_ptr,msg);
return i;
}
if (type == 0) return i; /* anything is legal */
if (i == 0) return i; /* null expression is legal */
if ( i != 1 || eps->stack->expr_code != EXPR_VALUE || eps->forward_reference )
{
if (msg) bad_token(tkn_ptr,msg);
eps->ptr = 1;
eps->stack->expr_code = EXPR_VALUE;
eps->stack->expr_value = (type != 3) ? REG_Dn : REG_An;
return 1; /* pretend it worked */
}
treg = eps->stack->expr_value&~(FORCE_WORD|FORCE_LONG);
switch (type)
{
default: /* any type of register allowed */
case GTERM_D|GTERM_A|GTERM_PC: /* has to be a D, A or PC register */
eps->register_reference = 1; /* force a register reference */
if (treg > REG_An+7 && treg != REG_PC && treg != REG_ZPC)
{
err = 1;
eps->stack->expr_value = REG_Dn; /* change to D0 */
}
break;
case GTERM_A|GTERM_D: /* has to be a D or A register */
eps->register_reference = 1; /* force a register reference */
if (treg > REG_An+7)
{
err = 1;
eps->stack->expr_value = REG_Dn; /* change to D0 */
}
break;
case GTERM_A|GTERM_PC: /* has to be an A or PC register */
eps->register_reference = 1; /* force a register reference */
if (treg < REG_An || (treg > REG_An+7 && treg != REG_PC))
{
err = 1;
eps->stack->expr_value = REG_An; /* change to D0 */
}
break;
case GTERM_A: /* has to be an A register */
eps->register_reference = 1; /* force a register reference */
if (treg < REG_An || treg > REG_An+7)
{
err = 1;
eps->stack->expr_value = REG_An; /* change to D0 */
}
break;
case GTERM_D: /* has to be a D register */
eps->register_reference = 1; /* force a register reference */
if (treg > REG_Dn+7)
{
err = 1;
eps->stack->expr_value = REG_Dn; /* change to D0 */
}
break;
}
if (msg && err)
{
bad_token(tkn_ptr,msg);
return 0;
}
return 1; /* it's ok now */
}
/* An entry from this table is obtained by placing the following data in a 4 bit
* integer and using that value as an index into this array:
* bits 1-0: The size of the outer displacement: 0=illegal,1=null,2=word,3=long
* bit 2: 1 if postindex address mode
* bit 3: 1 if preindex address mode
* Note that the (binary) combinations 0000 and 11xx are illegal */
static unsigned char index_indirect_modes[16] = {
0x00, /* no memory indirection */
0x41, /* memory indirect with null outer displacement */
0x42, /* memory indirect with word outer displacement */
0x43, /* memory indirect with long outer displacement */
0x05, /* indirect postindexed with null outer displacement */
0x05, /* indirect postindexed with null outer displacement */
0x06, /* indirect postindexed with word outer displacement */
0x07, /* indirect postindexed with long outer displacement */
0x01, /* indirect preindexed with null outer displacement */
0x01, /* indirect preindexed with null outer displacement */
0x02, /* indirect preindexed with word outer displacement */
0x03, /* indirect preindexed with long outer displacement */
0x04, /* illegal */
0x04, /* illegal */
0x04, /* illegal */
0x04 /* illegal */
};
void fix_pcr(EA *amp,int size)
{
EXP_stk *eps;
EXPR_struct *exp;
eps = amp->exp[0];
exp = eps->stack + eps->ptr;
if (size > 0)
{
exp->expr_code = EXPR_SEG;
exp->expr_value = current_offset+size;
(exp++)->expr_seg = current_section;
}
else
{
exp->expr_code = EXPR_VALUE;
(exp++)->expr_value = (size == 0) ? 2 : size;
}
exp->expr_code = EXPR_OPER;
exp->expr_value = '-';
eps->ptr += 2;
eps->ptr = compress_expr(eps);
if (list_bin) compress_expr_psuedo(eps);
return;
}
void fixup_ea(EA *amp, int amflg, unsigned long valid, int pcoff)
{
EXP_stk *eps;
EXPR_struct *exp;
int ecv,i;
long tv;
switch (amp->parts)
{
case 0: /* no parts */
amp->extension = 0x0150 | EXT_BASESUPRESS;
amp->mode = E_NDX;
amp->eamode = Ea_NDX;
amp->base_reg = amp->index_reg = 0;
return;
case EAPARTS_BASER: /* only a base register */
amp->mode = E_ATAn;
amp->eamode = Ea_ATAn;
amp->base_reg &= 7; /* base reg can only be 0-7 */
return;
case EAPARTS_BASED: /* base dislacement only */
case EAPARTS_BASER|EAPARTS_BASED: /* base reg and base dislacement only */
eps = amp->exp[0]; /* point to base displacement stack */
exp = eps->stack;
if (amp->base_reg == REG_ZPC || amp->base_reg == REG_PC)
{
if (amp->base_reg == REG_ZPC)
{
amp->base_reg = REG_PC;
amp->parts &= ~EAPARTS_BASER;
}
else
{
fix_pcr(amp,pcoff); /* adjust pc relative */
}
}
if ((amp->parts&EAPARTS_BASER) == 0) amp->extension |= EXT_BASESUPRESS;
tv = exp->expr_value;
ecv = (eps->ptr == 1 && exp->expr_code == EXPR_VALUE && !eps->forward_reference);
if ((valid&(E_NDX|E_PCN)) == 0 || !eps->force_long || (ecv && tv > -32768 && tv < 32768))
{
if (amp->base_reg == REG_PC)
{
amp->mode = E_PCR;
amp->eamode = Ea_PCR;
amp->base_reg = 0;
}
else
{
amp->mode = E_DSP;
amp->eamode = Ea_DSP;
amp->base_reg &= 7; /* base reg can only be 0-7 */
}
eps->tag = 'I'; /* short */
return; /* nothing more to do */
}
if (amp->base_reg == REG_PC)
{
amp->mode = E_PCN;
amp->eamode = Ea_PCN;
amp->base_reg = 0;
}
else
{
amp->mode = E_NDX;
amp->eamode = Ea_NDX;
amp->base_reg &= 7;
}
amp->extension |= 0x0170; /* longword base displacement with no index */
eps->tag = 'L'; /* longword base displacement */
return;
case EAPARTS_BASER|EAPARTS_INDXR:
case EAPARTS_BASED|EAPARTS_INDXR:
case EAPARTS_BASER|EAPARTS_BASED|EAPARTS_INDXR:
if (amp->base_reg == REG_PC)
{
fix_pcr(amp,pcoff);
amp->mode = E_PCN;
amp->eamode = Ea_PCN;
amp->base_reg = 0;
}
else
{
amp->mode = E_NDX;
amp->eamode = Ea_NDX;
amp->base_reg &= 7;
}
eps = amp->exp[0]; /* point to base displacement stack */
exp = eps->stack;
if ((amp->parts&EAPARTS_BASED) != 0)
{
tv = exp->expr_value;
ecv = (eps->ptr == 1 && exp->expr_code == EXPR_VALUE && !eps->forward_reference);
}
else
{
tv = 0;
ecv = 1;
}
amp->extension |= ((amp->index_reg&15)<<EXT_V_INDEX) |
((amp->index_reg&FORCE_LONG) ? EXT_ILONG : 0) |
(amp->index_scale<<EXT_V_SCALE);
if ((amflg&(AMFLG_preindx|AMFLG_postindx)) == 0 && (amp->parts&EAPARTS_BASER) != 0 &&
(eps->force_byte || (ecv && tv > -128 && tv < 128)))
{
amp->extension |= (tv&255);
if (!ecv || tv < -128 || tv > 127)
{
eps->tag = 's'; /* byte displacement expression -> obj file */
}
else
{
eps->ptr = 0; /* else zap the expression, value in extension word */
}
}
else
{
if ((amp->parts&EAPARTS_BASER) == 0) amp->extension |= EXT_BASESUPRESS;
i = (((amflg&AMFLG_preindx) != 0) << 3) | (((amflg&AMFLG_postindx) != 0) << 2);
amp->extension |= index_indirect_modes[i];
if ((!ecv && !eps->force_long) || (ecv && tv > -32768 && tv < 32768))
{
amp->extension |= 0x0120; /* word base displacement */
eps->tag = 'I'; /* signed word */
}
else
{
amp->extension |= 0x0130; /* long base displacement */
eps->tag = 'L'; /* signed longword */
}
}
return;
case EAPARTS_INDXR: /* only an index register */
case EAPARTS_INDXD: /* only an outer displacement */
case EAPARTS_INDXR|EAPARTS_INDXD: /* only an index register and an outer displacement */
case EAPARTS_INDXD|EAPARTS_BASER:
case EAPARTS_INDXD|EAPARTS_BASED:
case EAPARTS_INDXD|EAPARTS_BASER|EAPARTS_BASED:
case EAPARTS_INDXD|EAPARTS_INDXR|EAPARTS_BASER:
case EAPARTS_INDXD|EAPARTS_INDXR|EAPARTS_BASED:
case EAPARTS_INDXD|EAPARTS_INDXR|EAPARTS_BASER|EAPARTS_BASED:
amp->mode = E_NDX; /* assume indexed mode */
amp->eamode = Ea_NDX;
if ((amp->parts&(EAPARTS_BASER|EAPARTS_BASED)) != 0)
{
eps = amp->exp[0]; /* point to base displacement stack */
exp = eps->stack;
if ((amp->parts&EAPARTS_BASER) == 0) amp->extension |= EXT_BASESUPRESS;
if (amp->base_reg == REG_PC)
{
fix_pcr(amp,pcoff);
amp->mode = E_PCN;
amp->eamode = Ea_PCN;
}
tv = exp->expr_value;
ecv = (eps->ptr == 1 && exp->expr_code == EXPR_VALUE && !eps->forward_reference);
if ((amp->parts&EAPARTS_BASED) == 0)
{
ecv = 1;
tv = 0;
}
if ((!ecv && !eps->force_long) || (ecv && tv > -32768 && tv < 32768))
{
if (ecv && tv == 0)
{
amp->extension |= 0x0010; /* null base displacement */
eps->ptr = 0;
}
else
{
amp->extension |= 0x0020; /* else word base displacement */
eps->tag = 'I'; /* short */
}
}
else
{
amp->extension |= 0x0030; /* else it's a longword base displacement */
eps->tag = 'L';
}
}
else
{ /* now fall through to index handler */
amp->extension |= 0x0190; /* no base register or displacement */
}
eps = amp->exp[1]; /* point to outer displacement stack */
exp = eps->stack;
if ((amp->parts&EAPARTS_INDXD) != 0)
{
tv = exp->expr_value;
ecv = (eps->ptr == 1 && exp->expr_code == EXPR_VALUE && eps->forward_reference);
}
else
{
tv = 0;
ecv = 1;
}
if ((!ecv && !eps->force_long) || (ecv && tv > -32768 && tv < 32768))
{
if (ecv && tv == 0)
{
i = 1; /* null displacement */
eps->ptr = 0; /* zap the expression */
}
else
{
i = 2; /* word displacement */
eps->tag = 'I'; /* expression must resolve to signed word */
}
}
else
{
i = 3; /* longword displacment */
eps->tag = 'L'; /* signed longword */
}
if ((amp->parts&EAPARTS_INDXR) != 0)
{
amp->extension |= ((amp->index_reg&15)<<EXT_V_INDEX) |
((amp->index_reg&FORCE_LONG) ? EXT_ILONG : 0) |
(amp->index_scale<<EXT_V_SCALE);
i |= (((amflg&AMFLG_preindx) != 0) << 3) | (((amflg&AMFLG_postindx) != 0) << 2);
}
amp->extension |= index_indirect_modes[i] | 0x0100;
amp->base_reg &= 7;
return;
default:
bad_token((char *)0,"Undecoded address mode");
return;
} /* --switch (parts) */
return;
}
static struct
{
short reg_num;
short movec;
long mode;
} reg_xref[] = {
{REG_SR,0xFFF,E_SR},
{REG_USP,0x800,E_USP},
{REG_SSP,0xFFF,E_SPCL},
{REG_ISP,0x804,E_SPCL},
{REG_MSP,0x803,E_SPCL},
{REG_CCR,0xFFF,E_CCR},
{REG_VBR,0x801,E_SPCL},
{REG_SFC,0x000,E_SPCL},
{REG_DFC,0x001,E_SPCL},
{REG_CACR,0x002,E_SPCL},
{REG_CAAR,0x802,E_SPCL},
{0,0}
};
int get_oneea( EA *amp, int bwl, unsigned long valid, int pcoff)
{
EXP_stk *eps;
EXPR_struct *exp;
AMflg amflg;
char *cp;
int es;
long tv;
amp->mode = E_NUL; /* assume no mode found */
amp->eamode = 0;
amp->base_reg = amp->index_reg = 0;
amp->extension = 0;
amp->num_exprs = 0; /* no expressions at the moment */
amp->parts = 0; /* no address parts found yet */
eps = amp->exp[0];
exp = eps->stack;
if ((cttbl[(int)*inp_ptr]&(CT_EOL|CT_SMC)) != 0) return 0;
amflg = AMFLG_none; /* no special flags present */
if (*inp_ptr == '#')
{ /* immediate? */
++inp_ptr; /* yep, eat the char */
get_token(); /* prime the pump */
exprs(1,eps); /* pickup the expression */
amp->mode = E_IMM;
amp->eamode = Ea_IMM;
eps->tag = ea_to_tag[bwl];
return 1;
}
cp = inp_ptr;
if (get_token() == EOL)
{ /* prime the pump */
bad_token(tkn_ptr,"Expected an address mode here");
return 0;
}
squawk_experr = 0; /* put expression evaluator in special mode */
es = exprs(1,eps); /* get any kind of expression */
squawk_experr = 1; /* put expression evaluator back in normal mode */
if (es < 0 ) return 0; /* it's nfg */
if (eps->register_mask)
{
amp->mode = E_RLST; /* it was a register list */
return 1; /* return success */
}
if (eps->register_reference && eps->paren_cnt == 0)
{ /* if first term a simple register */
if ( eps->ptr != 1 || exp->expr_code != EXPR_VALUE || eps->forward_reference )
{
bad_token(cp,"Register expression must resolve to an absolute value");
eps->ptr = 0;
return 0;
}
tv = exp->expr_value&~(FORCE_WORD|FORCE_LONG); /* register number */
amp->base_reg = tv;
if (eps->autodec || eps->autoinc)
{ /* auto modes */
amp->base_reg = tv&7;
if (tv < REG_An || tv > REG_An+7)
{
bad_token(cp,"Autoinc/autodec modes only valid on A registers");
eps->ptr = 0;
return 0;
}
if (eps->autodec)
{
amp->mode = E_DAN; /* autodec */
amp->eamode = Ea_DAN;
}
else
{
amp->mode = E_ANI; /* autoinc */
amp->eamode = Ea_ANI;
}
return 1;
}
if (eps->paren)
{ /* simple register indirect */
if (tv > REG_An+7 && tv != REG_PC && tv != REG_ZPC)
{
bad_token(cp,"Register indirect only valid with A, D or PC registers");
eps->ptr = 0;
return 0;
}
if (tv >= REG_An && tv <= REG_An+7)
{ /* A reg indirect */
amp->mode = E_ATAn;
amp->eamode = Ea_ATAn;
amp->base_reg = tv&7; /* the base reg is only one of 8 */
return 1;
}
if (tv < REG_An)
{ /* D reg indirect is special */
amp->extension = (tv<<EXT_V_INDEX) | ((exp->expr_value&FORCE_LONG) ? EXT_ILONG:0) |
(eps->register_scale<<EXT_V_SCALE) | 0x0110 | EXT_BASESUPRESS |
EXT_IIS_I0OD;
amp->mode = E_NDX;
amp->eamode = Ea_NDX;
amp->base_reg = 0; /* no base register in this mode */
return 1;
}
amp->extension = 0x0150;
amp->mode = E_PCN;
amp->eamode = Ea_PCN;
amp->base_reg = 0; /* no base register in this mode */
eps->ptr = 0; /* zap the expression */
return 1;
}
if ((exp->expr_value&(FORCE_WORD)) != 0)
{
show_bad_token(cp,"Forced word mode ignored in this context",MSG_WARN);
}
exp->expr_value = tv;
amp->movec_xlate = 0xFFF; /* assume invalid special */
if (tv < REG_An+0)
{
amp->mode = E_Dn; /* D register */
amp->eamode = Ea_Dn;
}
else if (tv < REG_An+8)
{
amp->mode = E_An; /* A register */
amp->eamode = Ea_An;
}
else for (es = 0;reg_xref[es].reg_num;++es)
{
if (reg_xref[es].reg_num == tv)
{
amp->mode = reg_xref[es].mode;
amp->movec_xlate = reg_xref[es].movec;
break;
}
if (reg_xref[es].reg_num==0)
{
bad_token(cp,"Not a valid register in this context");
return 0;
}
}
amp->reg = tv; /* remember the register number */
amp->base_reg &= 7; /* base reg can only be 0-7 */
eps->ptr = 0;
return 1;
}
amp->state = EASTATE_bd; /* assume we're to look for a base displacement */
if (es == 0)
{ /* expr failed. Maybe a '([' or '(,' expression */
if (eps->paren_cnt != 1)
{ /* no parens, then this is illegal */
bad_token(cp,"Expected an address mode expression here");
return 0;
}
if (*inp_ptr == '[')
{ /* if we stopped on a bracket */
amflg |= AMFLG_bracket; /* remember that we have a bracket, but get the bd next */
}
else if (*inp_ptr == ',')
{ /* if we stopped on a comma, then the bd is null */
exp->expr_code = EXPR_VALUE;
exp->expr_value = 0;
eps->ptr = 1; /* 1 term with value of 0 */
amp->parts |= EAPARTS_BASED; /* say we've got a base displacement */
}
else
{
bad_token(inp_ptr,"Invalid address mode syntax");
return 0;
}
}
else
{ /* the first term was found */
if (*inp_ptr != '(')
{ /* if we didn't stop on an open paren... */
if (eps->paren_cnt == 0)
{ /* and there were no imbalanced parens, then we have the whole am */
eps->tag_len = 1;
if (!(edmask&ED_AMA))
{
fix_pcr(amp,pcoff);
amp->mode = E_PCR;
amp->eamode = Ea_PCR;
eps->tag = 'I'; /* displacements are signed words */
}
else
{
amp->mode = E_ABS;
if (!eps->force_long && (eps->force_short || ( eps->ptr == 1
&& exp->expr_code == EXPR_VALUE
&& !eps->forward_reference
&& exp->expr_value >= -32678
&& exp->expr_value < 32768
)
)
)
{
eps->tag = 'I';
amp->eamode = Ea_ABSW;
}
else
{
eps->tag = 'L';
amp->eamode = Ea_ABSL;
}
}
return 1;
}
if (eps->paren_cnt != 1 || *inp_ptr != ',')
{ /* if not exactly 1 unmatched '(' and stop... */
bad_token(cp,"Invalid address mode syntax"); /*...on comma, then syntax is nfg */
return 0;
}
}
if (eps->register_reference)
{ /* the displacement is a register */
inp_ptr = cp; /* backup and prepare to rescan */
exp->expr_value = 0; /* set the displacement to 0 */
eps->register_reference = 0; /* not a register */
eps->ptr = 1;
}
amp->parts |= EAPARTS_BASED; /* say we've got a base displacement */
}
if ((amp->parts&EAPARTS_BASED) != 0)
{
amp->state = EASTATE_br; /* next get the base register */
++amp->num_exprs; /* move expression ptr */
eps = amp->exp[amp->num_exprs]; /* point to next expression stack */
exp = eps->stack;
}
++inp_ptr; /* eat whatever we stopped on */
while ((cttbl[(int)*inp_ptr]&CT_WS) != 0) ++inp_ptr;
cp = inp_ptr; /* remember where we started */
while (amp->state != EASTATE_done)
{ /* until the whole argument is processed */
long treg;
switch (amp->state)
{
case EASTATE_bd: { /* get the base displacement */
es = get_term(amp,0,eps,"Invalid base displacement syntax");
if (es < 0) return 0; /* didn't like the expression */
if (*inp_ptr != ',')
{ /* there'd better be more stuff */
bad_token(cp,"Expected to find a base displacement here");
return 0;
}
if (es == 0)
{ /* if there was a null expression */
exp->expr_code = EXPR_VALUE;
exp->expr_value = 0;
eps->ptr = 1; /* pretend 1 term with value of 0 */
}
amp->parts |= EAPARTS_BASED; /* say we've got a base displacement */
amp->state = EASTATE_br; /* next, pickup base register */
++amp->num_exprs; /* move expression ptr */
eps = amp->exp[amp->num_exprs]; /* point to next expression stack */
exp = eps->stack;
++inp_ptr; /* eat the comma */
continue;
}
case EASTATE_br: { /* the only thing we'll allow next is a br */
es = get_term(amp,GTERM_D|GTERM_A|GTERM_PC,eps,"Base register can only be A or PC");
if (es < 0) return 0; /* he didn't like the expression */
if (es > 0)
{
treg = exp->expr_value&~(FORCE_WORD|FORCE_LONG);
if (treg < REG_An)
{ /* if he used a D reg as the base register */
goto got_indexreg; /* pretend it was an index reg */
}
amp->base_reg = treg;
amp->parts |= EAPARTS_BASER; /* say we found a BASE register */
if (eps->register_scale != 0)
{
bad_token(tkn_ptr,"Scale factor on a BASE register is ignored");
}
}
eps->ptr = 0; /* zap the expression */
if ((amflg&AMFLG_bracket) != 0 && *inp_ptr == ']')
{
amflg |= AMFLG_postindx; /* signal a postindex address mode */
++inp_ptr; /* eat the bracket */
while ((cttbl[(int)*inp_ptr]&CT_WS) != 0) ++inp_ptr; /* skip white space */
cp = inp_ptr; /* remember start of next term */
amflg &= ~AMFLG_bracket; /* brackets not allowed anymore */
}
if (*inp_ptr == ')')
{ /* if this is all there is */
++inp_ptr; /* eat the paren */
while ((cttbl[(int)*inp_ptr]&CT_WS) != 0) ++inp_ptr; /* skip white space */
cp = inp_ptr; /* remember start of next term */
amp->state = EASTATE_done; /* we're done getting parts */
continue;
}
if (*inp_ptr != ',')
{ /* else next thing better be a comma */
bad_token(inp_ptr,"Expected an index register expression here");
return 0;
}
++inp_ptr; /* eat the comma */
while ((cttbl[(int)*inp_ptr]&CT_WS) != 0) ++inp_ptr; /* skip white space */
cp = inp_ptr; /* remember start of next term */
amp->state = EASTATE_ir; /* next go look for an index register */
continue;
}
case EASTATE_ir: {
es = get_term(amp,GTERM_D|GTERM_A,eps,"Index can only be an A or D register");
if (es < 0) return 0; /* he didn't like the expression */
if (es > 0)
{
treg = exp->expr_value&~(FORCE_WORD|FORCE_LONG);
got_indexreg:
amp->index_reg = exp->expr_value;
amp->parts |= EAPARTS_INDXR; /* say we found a BASE register */
amp->index_scale = eps->register_scale;
}
eps->ptr = 0; /* zap the expression */
if ((amflg&AMFLG_bracket) != 0 && *inp_ptr == ']')
{
amflg |= AMFLG_preindx; /* signal a preindex address mode */
++inp_ptr; /* eat the bracket */
while ((cttbl[(int)*inp_ptr]&CT_WS) != 0) ++inp_ptr; /* skip white space */
cp = inp_ptr; /* remember start of next term */
}
if (*inp_ptr == ')')
{ /* if this is all there is */
++inp_ptr; /* eat the paren */
while ((cttbl[(int)*inp_ptr]&CT_WS) != 0) ++inp_ptr; /* skip white space */
cp = inp_ptr; /* remember start of next term */
amp->state = EASTATE_done; /* we're done getting parts */
continue;
}
if (*inp_ptr != ',')
{ /* else next thing better be a comma */
bad_token(inp_ptr,"Expected an index register expression here");
return 0;
}
++inp_ptr; /* eat the comma */
while ((cttbl[(int)*inp_ptr]&CT_WS) != 0) ++inp_ptr; /* skip white space */
cp = inp_ptr; /* remember start of next term */
amp->state = EASTATE_od; /* next go look for an index register */
continue;
}
case EASTATE_od: {
es = get_term(amp,0,eps,"Invalid index displacement syntax");
if (es < 0) return 0;
if (*inp_ptr != ')')
{ /* there'd better not be more stuff */
bad_token(cp,"Expected to find close paren here");
return 0;
}
++inp_ptr; /* eat the close paren */
while ((cttbl[(int)*inp_ptr]&CT_WS) != 0) ++inp_ptr; /* skip white space */
if (es > 0)
{ /* if there was a non-null expression */
amp->parts |= EAPARTS_INDXD; /* say we've got an outer displacement */
++amp->num_exprs; /* move expression ptr */
}
amp->state = EASTATE_done; /* were done */
continue;
}
} /* --switch (state) */
} /* --while (!done) */
fixup_ea(amp,amflg,valid,pcoff);
return 1;
}
static int get_twoea( int bwl, unsigned long valid, int pcoff)
{
if ((cttbl[(int)*inp_ptr]&(CT_EOL|CT_SMC)) == 0)
{
if (get_oneea(&source,bwl,valid,pcoff) > 0)
{
if (*inp_ptr == ',')
{
++inp_ptr;
}
else
{
if ((cttbl[(int)*inp_ptr]&(CT_EOL|CT_SMC)) == 0)
{
show_bad_token(inp_ptr,"Expected a comma here, one assumed",MSG_WARN);
}
else
{
bad_token(inp_ptr,"Expected a second operand");
return 0;
}
}
return get_oneea(&dest,bwl,valid,pcoff);
}
return 0;
}
bad_token((char *)0,"Instruction requires 2 operands");
return 0;
}
static void bad_sourceam( void )
{
EXP_stk *eps;
EXPR_struct *exp;
int i;
bad_token((char *)0,"Invalid address mode for first operand");
source.mode = E_Dn;
source.eamode = Ea_Dn;
source.base_reg = 0;
source.index_reg = 0;
for (i=0;i<2;++i)
{
eps = source.exp[i];
eps->ptr = 0;
exp = eps->stack;
exp->expr_code = EXPR_VALUE;
exp->expr_value = 0;
eps->tag_len = 1;
eps->tag = 'W';
}
f1_eatit();
return;
}
static void bad_destam( void )
{
EXP_stk *eps;
EXPR_struct *exp;
int i;
bad_token((char *)0,"Invalid address mode for second operand");
dest.mode = E_Dn;
dest.eamode = Ea_Dn;
dest.base_reg = dest.index_reg = 0;
for (i=0;i<2;++i)
{
eps = dest.exp[i];
eps->ptr = 0;
exp = eps->stack;
exp->expr_code = EXPR_VALUE;
exp->expr_value = 0;
eps->tag_len = 1;
eps->tag = 'W';
}
f1_eatit();
return;
}
static int immediate_mode( unsigned short opcode, int bwl)
{
if ((source.mode&E_IMM) == 0) bad_sourceam();
EXP0SP->expr_value = opcode | (bwl<<6) | dest.eamode | (dest.base_reg&7);
EXP0.ptr = 1;
source.exp[0]->tag = ea_to_tag[bwl&3];
if (source.exp[0]->ptr < 1) source.exp[0]->ptr = 1;
return 1;
}
/*****************************************************************************
type0
Type 0.
Instructions:
ABCD,CMPM,SBCD,ADDX,SUBX
*****************************************************************************/
int type0( int inst, int bwl)
{
int rm;
static unsigned short optabl0[] = {
0xc100, /* ABCD */
0xb108, /* CMPM */
0x8100, /* SBCD */
0xd100, /* ADDX */
0x9100}; /* SUBX */
if (get_twoea(bwl,E_ANY,2) == 0)
{
rm = 0;
}
else
{
rm = bwl << 6;
if (inst == 2)
{
if (source.mode != E_ANI || dest.mode != E_ANI)
{
bad_token((char *)0,"Source and dest can only be (An)+");
source.mode = dest.mode = E_ANI;
}
}
else
{
if ((source.mode&~(E_Dn|E_DAN)) != 0 ||
(source.mode&(E_Dn|E_DAN)) == 0)
{
bad_sourceam();
}
rm |= (source.mode == E_Dn) ? 0 : 8;
}
if (source.mode != dest.mode)
{
bad_token((char *)0,"Source and dest modes must both be the same");
}
}
/* 9 4 0
* __________________
* | | Rd| |m| Rs|
* ------------------
*/
EXP0SP->expr_value = optabl0[inst-1]|rm|(dest.base_reg<<9)|source.base_reg;
return 1;
}
/******************************************************************************
type1
type 1
instructions:
ADD,SUB,ADDA,SUBA,ADDI,SUBI,ADDQ,SUBQ
******************************************************************************/
int type1( int inst, int bwl)
{
EXP_stk *eps;
EXPR_struct *eptr;
static unsigned short optabl1[] = {
0xD000, /* add */
0xD000, /* adda */
0x0600, /* addi */
0x5000, /* addq */
0x9000, /* sub */
0x9000, /* suba */
0x0400, /* subi */
0x5100 /* subq */
};
if (get_twoea(bwl,E_ANY,2) == 0)
{
EXP0SP->expr_value = optabl1[inst];
EXP0.ptr = 1;
return 0;
}
eps = source.exp[0];
eptr = eps->stack;
if (bwl == 0)
{
if (source.eamode == E_An || dest.eamode == Ea_An)
{
show_bad_token((char *)0,"Byte operations are not allowed on address registers",MSG_WARN);
bwl = 1;
}
}
if ((inst&3) == 0)
{ /* generic add/sub */