aboutsummaryrefslogtreecommitdiff
path: root/opcodes/rx-decode.opc
blob: 084a13f4b0e0b5898a1bb5acc933ad797f768141 (plain)
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
/* -*- c -*- */
/* Copyright (C) 2012-2024 Free Software Foundation, Inc.
   Contributed by Red Hat.
   Written by DJ Delorie.

   This file is part of the GNU opcodes library.

   This library 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, or (at your option)
   any later version.

   It 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, write to the Free Software
   Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
   MA 02110-1301, USA.  */

#include "sysdep.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "ansidecl.h"
#include "opcode/rx.h"
#include "libiberty.h"

#define RX_OPCODE_BIG_ENDIAN 0

typedef struct
{
  RX_Opcode_Decoded * rx;
  int (* getbyte)(void *);
  void * ptr;
  unsigned char * op;
} LocalData;

static int trace = 0;

#define BSIZE 0
#define WSIZE 1
#define LSIZE 2
#define DSIZE 3

/* These are for when the upper bits are "don't care" or "undefined".  */
static int bwl[4] =
{
  RX_Byte,
  RX_Word,
  RX_Long,
  RX_Bad_Size /* Bogus instructions can have a size field set to 3.  */
};

static int sbwl[4] =
{
  RX_SByte,
  RX_SWord,
  RX_Long,
  RX_Bad_Size /* Bogus instructions can have a size field set to 3.  */
};

static int ubw[4] =
{
  RX_UByte,
  RX_UWord,
  RX_Bad_Size,/* Bogus instructions can have a size field set to 2.  */
  RX_Bad_Size /* Bogus instructions can have a size field set to 3.  */
};

static int memex[4] =
{
  RX_SByte,
  RX_SWord,
  RX_Long,
  RX_UWord
};

static int _ld[2] =
{
  RX_Long,
  RX_Double
};

#define ID(x) rx->id = RXO_##x
#define OP(n,t,r,a) (rx->op[n].type = t, \
		     rx->op[n].reg = r,	     \
		     rx->op[n].addend = a )
#define OPs(n,t,r,a,s) (OP (n,t,r,a), \
			rx->op[n].size = s )

/* This is for the BWL and BW bitfields.  */
static int SCALE[] = { 1, 2, 4, 0 };
/* This is for the prefix size enum.  */
static int PSCALE[] = { 4, 1, 1, 1, 2, 2, 2, 3, 4, 8 };

#define GET_SCALE(_indx)  ((unsigned)(_indx) < ARRAY_SIZE (SCALE) ? SCALE[(_indx)] : 0)
#define GET_PSCALE(_indx) ((unsigned)(_indx) < ARRAY_SIZE (PSCALE) ? PSCALE[(_indx)] : 0)

static int flagmap[] = {0, 1, 2, 3, 0, 0, 0, 0,
		       16, 17, 0, 0, 0, 0, 0, 0 };

static int dsp3map[] = { 8, 9, 10, 3, 4, 5, 6, 7 };

/*
 *C	a constant (immediate) c
 *R	A register
 *I	Register indirect, no offset
 *Is	Register indirect, with offset
 *D	standard displacement: type (r,[r],dsp8,dsp16 code), register, BWL code
 *P	standard displacement: type (r,[r]), reg, assumes UByte
 *Pm	memex displacement: type (r,[r]), reg, memex code
 *cc	condition code.  */

#define DC(c)       OP (0, RX_Operand_Immediate, 0, c)
#define DR(r)       OP (0, RX_Operand_Register,  r, 0)
#define DI(r,a)     OP (0, RX_Operand_Indirect,  r, a)
#define DIs(r,a,s)  OP (0, RX_Operand_Indirect,  r, (a) * GET_SCALE (s))
#define DD(t,r,s)   rx_disp (0, t, r, bwl[s], ld);
#define DF(r)       OP (0, RX_Operand_Flag,  flagmap[r], 0)
#define DCR(r)      OP (0, RX_Operand_DoubleCReg, r, 0)
#define DDR(r)      OP (0, RX_Operand_DoubleReg,  r, 0)
#define DDRH(r)     OP (0, RX_Operand_DoubleRegH,  r, 0)
#define DDRL(r)     OP (0, RX_Operand_DoubleRegL,  r, 0)
#define DCND(r)     OP (0, RX_Operand_DoubleCond, r, 0)

#define SC(i)       OP (1, RX_Operand_Immediate, 0, i)
#define SR(r)       OP (1, RX_Operand_Register,  r, 0)
#define SRR(r)      OP (1, RX_Operand_TwoReg,  r, 0)
#define SI(r,a)     OP (1, RX_Operand_Indirect,  r, a)
#define SIs(r,a,s)  OP (1, RX_Operand_Indirect,  r, (a) * GET_SCALE (s))
#define SD(t,r,s)   rx_disp (1, t, r, bwl[s], ld);
#define SP(t,r)     rx_disp (1, t, r, (t!=3) ? RX_UByte : RX_Long, ld); P(t, 1);
#define SPm(t,r,m)  rx_disp (1, t, r, memex[m], ld); rx->op[1].size = memex[m];
#define Scc(cc)     OP (1, RX_Operand_Condition,  cc, 0)
#define SCR(r)      OP (1, RX_Operand_DoubleCReg, r, 0)
#define SDR(r)      OP (1, RX_Operand_DoubleReg,  r, 0)
#define SDRH(r)      OP (1, RX_Operand_DoubleRegH,  r, 0)
#define SDRL(r)      OP (1, RX_Operand_DoubleRegL,  r, 0)

#define S2C(i)      OP (2, RX_Operand_Immediate, 0, i)
#define S2R(r)      OP (2, RX_Operand_Register,  r, 0)
#define S2I(r,a)    OP (2, RX_Operand_Indirect,  r, a)
#define S2Is(r,a,s) OP (2, RX_Operand_Indirect,  r, (a) * GET_SCALE (s))
#define S2D(t,r,s)  rx_disp (2, t, r, bwl[s], ld);
#define S2P(t,r)    rx_disp (2, t, r, (t!=3) ? RX_UByte : RX_Long, ld); P(t, 2);
#define S2Pm(t,r,m) rx_disp (2, t, r, memex[m], ld); rx->op[2].size = memex[m];
#define S2cc(cc)    OP (2, RX_Operand_Condition,  cc, 0)
#define S2DR(r)     OP (2, RX_Operand_DoubleReg,  r, 0)
#define S2CR(r)     OP (2, RX_Operand_DoubleCReg, r, 0)

#define SDD(t,r,s)  rx_disp (1, t, r, bwl, ld);

#define BWL(sz)     rx->op[0].size = rx->op[1].size = rx->op[2].size = rx->size = bwl[sz]
#define sBWL(sz)    rx->op[0].size = rx->op[1].size = rx->op[2].size = rx->size = sbwl[sz]
#define uBW(sz)     rx->op[0].size = rx->op[1].size = rx->op[2].size = rx->size = ubw[sz]
#define P(t, n)	    rx->op[n].size = (t!=3) ? RX_UByte : RX_Long;
#define DL(sz)      rx->op[0].size = rx->op[1].size = rx->op[2].size = rx->size = _ld[sz]

#define F(f) store_flags(rx, f)

#define AU ATTRIBUTE_UNUSED
#define GETBYTE() (ld->op [ld->rx->n_bytes++] = ld->getbyte (ld->ptr))

#define SYNTAX(x) rx->syntax = x

#define UNSUPPORTED() \
  rx->syntax = "*unknown*"

#define IMM(sf)   immediate (sf, 0, ld)
#define IMMex(sf) immediate (sf, 1, ld)

static int
immediate (int sfield, int ex, LocalData * ld)
{
  unsigned long i = 0, j;

  switch (sfield)
    {
#define B ((unsigned long) GETBYTE())
    case 0:
#if RX_OPCODE_BIG_ENDIAN
      i  = B;
      if (ex && (i & 0x80))
	i -= 0x100;
      i <<= 24;
      i |= B << 16;
      i |= B << 8;
      i |= B;
#else
      i = B;
      i |= B << 8;
      i |= B << 16;
      j = B;
      if (ex && (j & 0x80))
	j -= 0x100;
      i |= j << 24;
#endif
      break;
    case 3:
#if RX_OPCODE_BIG_ENDIAN
      i  = B << 16;
      i |= B << 8;
      i |= B;
#else
      i  = B;
      i |= B << 8;
      i |= B << 16;
#endif
      if (ex && (i & 0x800000))
	i -= 0x1000000;
      break;
    case 2:
#if RX_OPCODE_BIG_ENDIAN
      i |= B << 8;
      i |= B;
#else
      i |= B;
      i |= B << 8;
#endif
      if (ex && (i & 0x8000))
	i -= 0x10000;
      break;
    case 1:
      i |= B;
      if (ex && (i & 0x80))
	i -= 0x100;
      break;
    default:
      abort();
    }
  return i;
}

static void
rx_disp (int n, int type, int reg, unsigned int size, LocalData * ld)
{
  int disp;

  ld->rx->op[n].reg = reg;
  switch (type)
    {
    case 3:
      ld->rx->op[n].type = RX_Operand_Register;
      break;
    case 0:
      ld->rx->op[n].type = RX_Operand_Zero_Indirect;
      ld->rx->op[n].addend = 0;
      break;
    case 1:
      ld->rx->op[n].type = RX_Operand_Indirect;
      disp = GETBYTE ();
      ld->rx->op[n].addend = disp * GET_PSCALE (size);
      break;
    case 2:
      ld->rx->op[n].type = RX_Operand_Indirect;
      disp = GETBYTE ();
#if RX_OPCODE_BIG_ENDIAN
      disp = disp * 256 + GETBYTE ();
#else
      disp = disp + GETBYTE () * 256;
#endif
      ld->rx->op[n].addend = disp * GET_PSCALE (size);
      break;
    default:
      abort ();
    }
}

#define xO 8
#define xS 4
#define xZ 2
#define xC 1

#define F_____
#define F___ZC rx->flags_0 = rx->flags_s = xZ|xC;
#define F__SZ_ rx->flags_0 = rx->flags_s = xS|xZ;
#define F__SZC rx->flags_0 = rx->flags_s = xS|xZ|xC;
#define F_0SZC rx->flags_0 = xO|xS|xZ|xC; rx->flags_s = xS|xZ|xC;
#define F_O___ rx->flags_0 = rx->flags_s = xO;
#define F_OS__ rx->flags_0 = rx->flags_s = xO|xS;
#define F_OSZ_ rx->flags_0 = rx->flags_s = xO|xS|xZ;
#define F_OSZC rx->flags_0 = rx->flags_s = xO|xS|xZ|xC;

int
rx_decode_opcode (unsigned long pc AU,
		  RX_Opcode_Decoded * rx,
		  int (* getbyte)(void *),
		  void * ptr)
{
  LocalData lds, * ld = &lds;
  unsigned char op[20] = {0};

  lds.rx = rx;
  lds.getbyte = getbyte;
  lds.ptr = ptr;
  lds.op = op;

  memset (rx, 0, sizeof (*rx));
  BWL(LSIZE);

/** VARY sz 00 01 10 */

/*----------------------------------------------------------------------*/
/* MOV									*/

/** 0111 0101 0100 rdst		mov%s	#%1, %0 */
  ID(mov); DR(rdst); SC(IMM (1)); F_____;

/** 1111 10sd rdst im sz	mov%s	#%1, %0 */
  ID(mov); DD(sd, rdst, sz);
  if ((im == 1 && sz == 0)
      || (im == 2 && sz == 1)
      || (im == 0 && sz == 2))
    {
      BWL (sz);
      SC(IMM(im));
    }
  else
    {
      sBWL (sz);
      SC(IMMex(im));
    }
   F_____;

/** 0110 0110 immm rdst		mov%s	#%1, %0 */
  ID(mov); DR(rdst); SC(immm); F_____;

/** 0011 11sz d dst sppp		mov%s	#%1, %0 */
  ID(mov); sBWL (sz); DIs(dst, d*16+sppp, sz); SC(IMM(1)); F_____;

/** 11sz sd ss rsrc rdst	mov%s	%1, %0 */
  if (sd == 3 && ss == 3 && sz == 2 && rsrc == 0 && rdst == 0)
    {
      ID(nop2);
      SYNTAX ("nop\t; mov.l\tr0, r0");
    }
  else
    {
      ID(mov); sBWL(sz); F_____;
      if ((ss == 3) && (sd != 3))
	{
	  SD(ss, rdst, sz); DD(sd, rsrc, sz);
	}
      else
	{
	  SD(ss, rsrc, sz); DD(sd, rdst, sz);
	}
    }

/** 10sz 1dsp a src b dst	mov%s	%1, %0 */
  ID(mov); sBWL(sz); DR(dst); SIs(src, dsp*4+a*2+b, sz); F_____;

/** 10sz 0dsp a dst b src	mov%s	%1, %0 */
  ID(mov); sBWL(sz); DIs(dst, dsp*4+a*2+b, sz); SR(src); F_____;

/** 1111 1110 01sz isrc bsrc rdst	mov%s	[%1, %2], %0 */
  ID(movbi); sBWL(sz); DR(rdst); SRR(isrc); S2R(bsrc); F_____;

/** 1111 1110 00sz isrc bsrc rdst	mov%s	%0, [%1, %2] */
  ID(movbir); sBWL(sz); DR(rdst); SRR(isrc); S2R(bsrc); F_____;

/** 1111 1110 11sz isrc bsrc rdst	movu%s	[%1, %2], %0 */
  ID(movbi); uBW(sz); DR(rdst); SRR(isrc); S2R(bsrc); F_____;

/** 1111 1101 0010 0p sz rdst rsrc	mov%s	%1, %0 */
  ID(mov); sBWL (sz); SR(rsrc); F_____;
  OP(0, p ? RX_Operand_Predec : RX_Operand_Postinc, rdst, 0);

/** 1111 1101 0010 1p sz rsrc rdst	mov%s	%1, %0 */
  ID(mov); sBWL (sz); DR(rdst); F_____;
  OP(1, p ? RX_Operand_Predec : RX_Operand_Postinc, rsrc, 0);

/** 1011 w dsp a src b dst	movu%s	%1, %0 */
  ID(mov); uBW(w); DR(dst); SIs(src, dsp*4+a*2+b, w); F_____;

/** 0101 1 s ss rsrc rdst	movu%s	%1, %0 */
  ID(mov); uBW(s); SD(ss, rsrc, s); DR(rdst); F_____;

/** 1111 1101 0011 1p sz rsrc rdst	movu%s	%1, %0 */
  ID(mov); uBW (sz); DR(rdst); F_____;
   OP(1, p ? RX_Operand_Predec : RX_Operand_Postinc, rsrc, 0);

/*----------------------------------------------------------------------*/
/* PUSH/POP								*/

/** 0110 1111 dsta dstb		popm	%1-%2 */
  ID(popm); SR(dsta); S2R(dstb); F_____;

/** 0110 1110 dsta dstb		pushm	%1-%2 */
  ID(pushm); SR(dsta); S2R(dstb); F_____;

/** 0111 1110 1011 rdst		pop	%0 */
  ID(mov); OP(1, RX_Operand_Postinc, 0, 0); DR(rdst); F_____;

/** 0111 1110 10sz rsrc		push%s	%1 */
  ID(mov); BWL(sz); OP(0, RX_Operand_Predec, 0, 0); SR(rsrc); F_____;

/** 1111 01ss rsrc 10sz		push%s	%1 */
  ID(mov); BWL(sz); OP(0, RX_Operand_Predec, 0, 0); SD(ss, rsrc, sz); F_____;

/*----------------------------------------------------------------------*/
/* XCHG									*/

/** 1111 1100 0100 00ss rsrc rdst	xchg	%1%S1, %0 */
  ID(xchg); DR(rdst); SP(ss, rsrc);

/** 0000 0110 mx10 00ss 0001 0000 rsrc rdst	xchg	%1%S1, %0 */
  ID(xchg); DR(rdst); SPm(ss, rsrc, mx);

/*----------------------------------------------------------------------*/
/* STZ/STNZ								*/

/** 1111 1101 0111 im00 1110rdst	stz	#%1, %0 */
  ID(stcc); SC(IMMex(im)); DR(rdst); S2cc(RXC_z);

/** 1111 1101 0111 im00 1111rdst	stnz	#%1, %0 */
  ID(stcc); SC(IMMex(im)); DR(rdst); S2cc(RXC_nz);

/*----------------------------------------------------------------------*/
/* RTSD									*/

/** 0110 0111			rtsd	#%1 */
  ID(rtsd); SC(IMM(1) * 4);

/** 0011 1111 rega regb		rtsd	#%1, %2-%0 */
  ID(rtsd); SC(IMM(1) * 4); S2R(rega); DR(regb);

/*----------------------------------------------------------------------*/
/* AND									*/

/** 0110 0100 immm rdst			and	#%1, %0 */
  ID(and); SC(immm); DR(rdst); F__SZ_;

/** 0111 01im 0010 rdst			and	#%1, %0 */
  ID(and); SC(IMMex(im)); DR(rdst); F__SZ_;

/** 0101 00ss rsrc rdst			and	%1%S1, %0 */
  ID(and); SP(ss, rsrc); DR(rdst); F__SZ_;

/** 0000 0110 mx01 00ss rsrc rdst	and	%1%S1, %0 */
  ID(and); SPm(ss, rsrc, mx); DR(rdst); F__SZ_;

/** 1111 1111 0100 rdst srca srcb	and	%2, %1, %0 */
  ID(and); DR(rdst); SR(srcb); S2R(srca); F__SZ_;

/*----------------------------------------------------------------------*/
/* OR									*/

/** 0110 0101 immm rdst			or	#%1, %0 */
  ID(or); SC(immm); DR(rdst); F__SZ_;

/** 0111 01im 0011 rdst			or	#%1, %0 */
  ID(or); SC(IMMex(im)); DR(rdst); F__SZ_;

/** 0101 01ss rsrc rdst			or	%1%S1, %0 */
  ID(or); SP(ss, rsrc); DR(rdst); F__SZ_;

/** 0000 0110 mx01 01ss rsrc rdst			or	%1%S1, %0 */
  ID(or); SPm(ss, rsrc, mx); DR(rdst); F__SZ_;

/** 1111 1111 0101 rdst srca srcb	or	%2, %1, %0 */
  ID(or); DR(rdst); SR(srcb); S2R(srca); F__SZ_;

/*----------------------------------------------------------------------*/
/* XOR									*/

/** 1111 1101 0111 im00 1101rdst	xor	#%1, %0 */
  ID(xor); SC(IMMex(im)); DR(rdst); F__SZ_;

/** 1111 1100 0011 01ss rsrc rdst	xor	%1%S1, %0 */
  ID(xor); SP(ss, rsrc); DR(rdst); F__SZ_;

/** 0000 0110 mx10 00ss 0000 1101 rsrc rdst	xor	%1%S1, %0 */
  ID(xor); SPm(ss, rsrc, mx); DR(rdst); F__SZ_;

/*----------------------------------------------------------------------*/
/* NOT									*/

/** 0111 1110 0000 rdst			not	%0 */
  ID(xor); DR(rdst); SR(rdst); S2C(~0); F__SZ_;

/** 1111 1100 0011 1011 rsrc rdst	not	%1, %0 */
  ID(xor); DR(rdst); SR(rsrc); S2C(~0); F__SZ_;

/*----------------------------------------------------------------------*/
/* TST									*/

/** 1111 1101 0111 im00 1100rdst	tst	#%1, %2 */
  ID(and); SC(IMMex(im)); S2R(rdst); F__SZ_;

/** 1111 1100 0011 00ss rsrc rdst	tst	%1%S1, %2 */
  ID(and); SP(ss, rsrc); S2R(rdst); F__SZ_;

/** 0000 0110 mx10 00ss 0000 1100 rsrc rdst	tst	%1%S1, %2 */
  ID(and); SPm(ss, rsrc, mx); S2R(rdst); F__SZ_;

/*----------------------------------------------------------------------*/
/* NEG									*/

/** 0111 1110 0001 rdst			neg	%0 */
  ID(sub); DR(rdst); SC(0); S2R(rdst); F_OSZC;

/** 1111 1100 0000 0111 rsrc rdst	neg	%2, %0 */
  ID(sub); DR(rdst); SC(0); S2R(rsrc); F_OSZC;

/*----------------------------------------------------------------------*/
/* ADC									*/

/** 1111 1101 0111 im00 0010rdst	adc	#%1, %0 */
  ID(adc); SC(IMMex(im)); DR(rdst); F_OSZC;

/** 1111 1100 0000 1011 rsrc rdst	adc	%1, %0 */
  ID(adc); SR(rsrc); DR(rdst); F_OSZC;

/** 0000 0110 1010 00ss 0000 0010 rsrc rdst	adc	%1%S1, %0 */
  ID(adc); SPm(ss, rsrc, 2); DR(rdst); F_OSZC;

/*----------------------------------------------------------------------*/
/* ADD									*/

/** 0110 0010 immm rdst			add	#%1, %0 */
  ID(add); SC(immm); DR(rdst); F_OSZC;

/** 0100 10ss rsrc rdst			add	%1%S1, %0 */
  ID(add); SP(ss, rsrc); DR(rdst); F_OSZC;

/** 0000 0110 mx00 10ss rsrc rdst	add	%1%S1, %0 */
  ID(add); SPm(ss, rsrc, mx); DR(rdst); F_OSZC;

/** 0111 00im rsrc rdst			add	#%1, %2, %0 */
  ID(add); SC(IMMex(im)); S2R(rsrc); DR(rdst); F_OSZC;

/** 1111 1111 0010 rdst srca srcb	add	%2, %1, %0 */
  ID(add); DR(rdst); SR(srcb); S2R(srca); F_OSZC;

/*----------------------------------------------------------------------*/
/* CMP									*/

/** 0110 0001 immm rdst			cmp	#%2, %1 */
  ID(sub); S2C(immm); SR(rdst); F_OSZC;

/** 0111 01im 0000 rsrc		cmp	#%2, %1%S1 */
  ID(sub); SR(rsrc); S2C(IMMex(im)); F_OSZC;

/** 0111 0101 0101 rsrc			cmp	#%2, %1 */
  ID(sub); SR(rsrc); S2C(IMM(1)); F_OSZC;

/** 0100 01ss rsrc rdst		cmp	%2%S2, %1 */
  ID(sub); S2P(ss, rsrc); SR(rdst); F_OSZC;

/** 0000 0110 mx00 01ss rsrc rdst		cmp	%2%S2, %1 */
  ID(sub); S2Pm(ss, rsrc, mx); SR(rdst); F_OSZC;

/*----------------------------------------------------------------------*/
/* SUB									*/

/** 0110 0000 immm rdst			sub	#%2, %0 */
  ID(sub); S2C(immm); SR(rdst); DR(rdst); F_OSZC;

/** 0100 00ss rsrc rdst			sub	%2%S2, %1 */
  ID(sub); S2P(ss, rsrc); SR(rdst); DR(rdst); F_OSZC;

/** 0000 0110 mx00 00ss rsrc rdst			sub	%2%S2, %1 */
  ID(sub); S2Pm(ss, rsrc, mx); SR(rdst); DR(rdst); F_OSZC;

/** 1111 1111 0000 rdst srca srcb	sub	%2, %1, %0 */
  ID(sub); DR(rdst); SR(srcb); S2R(srca); F_OSZC;

/*----------------------------------------------------------------------*/
/* SBB									*/

/** 1111 1100 0000 0011 rsrc rdst	sbb	%1, %0 */
  ID(sbb); SR (rsrc); DR(rdst); F_OSZC;

  /* FIXME: only supports .L */
/** 0000 0110 mx10 00sp 0000 0000 rsrc rdst	sbb	%1%S1, %0 */
  ID(sbb); SPm(sp, rsrc, mx); DR(rdst); F_OSZC;

/*----------------------------------------------------------------------*/
/* ABS									*/

/** 0111 1110 0010 rdst			abs	%0 */
  ID(abs); DR(rdst); SR(rdst); F_OSZ_;

/** 1111 1100 0000 1111 rsrc rdst	abs	%1, %0 */
  ID(abs); DR(rdst); SR(rsrc); F_OSZ_;

/*----------------------------------------------------------------------*/
/* MAX									*/

/** 1111 1101 0111 im00 0100rdst	max	#%1, %0 */
  int val = IMMex (im);
  if (im == 0 && (unsigned) val == 0x80000000 && rdst == 0)
    {
      ID (nop7);
      SYNTAX("nop\t; max\t#0x80000000, r0");
    }
  else
    {
      ID(max);
    }
  DR(rdst); SC(val);

/** 1111 1100 0001 00ss rsrc rdst	max	%1%S1, %0 */
  if (ss == 3 && rsrc == 0 && rdst == 0)
    {
      ID(nop3);
      SYNTAX("nop\t; max\tr0, r0");
    }
  else
    {
      ID(max); SP(ss, rsrc); DR(rdst);
    }

/** 0000 0110 mx10 00ss 0000 0100 rsrc rdst	max	%1%S1, %0 */
  ID(max); SPm(ss, rsrc, mx); DR(rdst);

/*----------------------------------------------------------------------*/
/* MIN									*/

/** 1111 1101 0111 im00 0101rdst	min	#%1, %0 */
  ID(min); DR(rdst); SC(IMMex(im));

/** 1111 1100 0001 01ss rsrc rdst	min	%1%S1, %0 */
  ID(min); SP(ss, rsrc); DR(rdst);

/** 0000 0110 mx10 00ss 0000 0101 rsrc rdst	min	%1%S1, %0 */
  ID(min); SPm(ss, rsrc, mx); DR(rdst);

/*----------------------------------------------------------------------*/
/* MUL									*/

/** 0110 0011 immm rdst			mul	#%1, %0 */
  if (immm == 1 && rdst == 0)
    {
      ID(nop2);
      SYNTAX ("nop\t; mul\t#1, r0");
    }
  else
    {
      ID(mul);
    }
  DR(rdst); SC(immm); F_____;

/** 0111 01im 0001rdst			mul	#%1, %0 */
  int val = IMMex(im);
  if (val == 1 && rdst == 0)
    {
      SYNTAX("nop\t; mul\t#1, r0");
      switch (im)
	{
	case 2: ID(nop4); break;
	case 3: ID(nop5); break;
	case 0: ID(nop6); break;
	default:
	  ID(mul);
	  SYNTAX("mul	#%1, %0");
	  break;
	}
    }
  else
    {
      ID(mul);
    }
  DR(rdst); SC(val); F_____;

/** 0100 11ss rsrc rdst			mul	%1%S1, %0 */
  ID(mul); SP(ss, rsrc); DR(rdst); F_____;

/** 0000 0110 mx00 11ss rsrc rdst	mul	%1%S1, %0 */
  ID(mul); SPm(ss, rsrc, mx); DR(rdst); F_____;

/** 1111 1111 0011 rdst srca srcb	mul 	%2, %1, %0 */
  ID(mul); DR(rdst); SR(srcb); S2R(srca); F_____;

/*----------------------------------------------------------------------*/
/* EMUL									*/

/** 1111 1101 0111 im00 0110rdst	emul	#%1, %0 */
  ID(emul); DR(rdst); SC(IMMex(im));

/** 1111 1100 0001 10ss rsrc rdst	emul	%1%S1, %0 */
  ID(emul); SP(ss, rsrc); DR(rdst);

/** 0000 0110 mx10 00ss 0000 0110 rsrc rdst	emul	%1%S1, %0 */
  ID(emul); SPm(ss, rsrc, mx); DR(rdst);

/*----------------------------------------------------------------------*/
/* EMULU									*/

/** 1111 1101 0111 im00 0111rdst	emulu	#%1, %0 */
  ID(emulu); DR(rdst); SC(IMMex(im));

/** 1111 1100 0001 11ss rsrc rdst	emulu	%1%S1, %0 */
  ID(emulu); SP(ss, rsrc); DR(rdst);

/** 0000 0110 mx10 00ss 0000 0111 rsrc rdst	emulu	%1%S1, %0 */
  ID(emulu); SPm(ss, rsrc, mx); DR(rdst);

/*----------------------------------------------------------------------*/
/* DIV									*/

/** 1111 1101 0111 im00 1000rdst	div	#%1, %0 */
  ID(div); DR(rdst); SC(IMMex(im)); F_O___;

/** 1111 1100 0010 00ss rsrc rdst	div	%1%S1, %0 */
  ID(div); SP(ss, rsrc); DR(rdst); F_O___;

/** 0000 0110 mx10 00ss 0000 1000 rsrc rdst	div	%1%S1, %0 */
  ID(div); SPm(ss, rsrc, mx); DR(rdst); F_O___;

/*----------------------------------------------------------------------*/
/* DIVU									*/

/** 1111 1101 0111 im00 1001rdst	divu	#%1, %0 */
  ID(divu); DR(rdst); SC(IMMex(im)); F_O___;

/** 1111 1100 0010 01ss rsrc rdst	divu	%1%S1, %0 */
  ID(divu); SP(ss, rsrc); DR(rdst); F_O___;

/** 0000 0110 mx10 00ss 0000 1001 rsrc rdst	divu	%1%S1, %0 */
  ID(divu); SPm(ss, rsrc, mx); DR(rdst); F_O___;

/*----------------------------------------------------------------------*/
/* SHIFT								*/

/** 0110 110i mmmm rdst			shll	#%2, %0 */
  ID(shll); S2C(i*16+mmmm); SR(rdst); DR(rdst); F_OSZC;

/** 1111 1101 0110 0010 rsrc rdst	shll	%2, %0 */
  ID(shll); S2R(rsrc); SR(rdst); DR(rdst); F_OSZC;

/** 1111 1101 110immmm rsrc rdst	shll	#%2, %1, %0 */
  ID(shll); S2C(immmm); SR(rsrc); DR(rdst); F_OSZC;


/** 0110 101i mmmm rdst			shar	#%2, %0 */
  ID(shar); S2C(i*16+mmmm); SR(rdst); DR(rdst); F_0SZC;

/** 1111 1101 0110 0001 rsrc rdst	shar	%2, %0 */
  ID(shar); S2R(rsrc); SR(rdst); DR(rdst); F_0SZC;

/** 1111 1101 101immmm rsrc rdst	shar	#%2, %1, %0 */
  ID(shar); S2C(immmm); SR(rsrc); DR(rdst); F_0SZC;


/** 0110 100i mmmm rdst			shlr	#%2, %0 */
  ID(shlr); S2C(i*16+mmmm); SR(rdst); DR(rdst); F__SZC;

/** 1111 1101 0110 0000 rsrc rdst	shlr	%2, %0 */
  ID(shlr); S2R(rsrc); SR(rdst); DR(rdst); F__SZC;

/** 1111 1101 100immmm rsrc rdst	shlr	#%2, %1, %0 */
  ID(shlr); S2C(immmm); SR(rsrc); DR(rdst); F__SZC;

/*----------------------------------------------------------------------*/
/* ROTATE								*/

/** 0111 1110 0101 rdst			rolc	%0 */
  ID(rolc); DR(rdst); F__SZC;

/** 0111 1110 0100 rdst			rorc	%0 */
  ID(rorc); DR(rdst); F__SZC;

/** 1111 1101 0110 111i mmmm rdst	rotl	#%1, %0 */
  ID(rotl); SC(i*16+mmmm); DR(rdst); F__SZC;

/** 1111 1101 0110 0110 rsrc rdst	rotl	%1, %0 */
  ID(rotl); SR(rsrc); DR(rdst); F__SZC;

/** 1111 1101 0110 110i mmmm rdst	rotr	#%1, %0 */
  ID(rotr); SC(i*16+mmmm); DR(rdst); F__SZC;

/** 1111 1101 0110 0100 rsrc rdst	rotr	%1, %0 */
  ID(rotr); SR(rsrc); DR(rdst); F__SZC;

/** 1111 1101 0110 0101 rsrc rdst	revw	%1, %0 */
  ID(revw); SR(rsrc); DR(rdst);

/** 1111 1101 0110 0111 rsrc rdst	revl	%1, %0 */
  ID(revl); SR(rsrc); DR(rdst);

/*----------------------------------------------------------------------*/
/* BRANCH								*/

/** 0001 n dsp			b%1.s	%a0 */
  ID(branch); Scc(n); DC(pc + dsp3map[dsp]);

/** 0010 cond			b%1.b	%a0 */
  ID(branch); Scc(cond); DC(pc + IMMex (1));

/** 0011 101c			b%1.w	%a0 */
  ID(branch); Scc(c); DC(pc + IMMex (2));


/** 0000 1dsp			bra.s	%a0 */
  ID(branch); DC(pc + dsp3map[dsp]);

/** 0010 1110			bra.b	%a0 */
  ID(branch); DC(pc + IMMex(1));

/** 0011 1000			bra.w	%a0 */
  ID(branch); DC(pc + IMMex(2));

/** 0000 0100			bra.a	%a0 */
  ID(branch); DC(pc + IMMex(3));

/** 0111 1111 0100 rsrc		bra.l	%0 */
  ID(branchrel); DR(rsrc);


/** 0111 1111 0000 rsrc		jmp	%0 */
  ID(branch); DR(rsrc);

/** 0111 1111 0001 rsrc		jsr	%0 */
  ID(jsr); DR(rsrc);

/** 0011 1001			bsr.w	%a0 */
  ID(jsr); DC(pc + IMMex(2));

/** 0000 0101			bsr.a	%a0 */
  ID(jsr); DC(pc + IMMex(3));

/** 0111 1111 0101 rsrc		bsr.l	%0 */
  ID(jsrrel); DR(rsrc);

/** 0000 0010			rts */
  ID(rts);

/*----------------------------------------------------------------------*/
/* NOP								*/

/** 0000 0011			nop */
  ID(nop);

/*----------------------------------------------------------------------*/
/* STRING FUNCTIONS							*/

/** 0111 1111 1000 0011		scmpu */
  ID(scmpu); F___ZC;

/** 0111 1111 1000 0111		smovu */
  ID(smovu);

/** 0111 1111 1000 1011		smovb */
  ID(smovb);

/** 0111 1111 1000 00sz		suntil%s */
  ID(suntil); BWL(sz); F___ZC;

/** 0111 1111 1000 01sz		swhile%s */
  ID(swhile); BWL(sz); F___ZC;

/** 0111 1111 1000 1111		smovf */
  ID(smovf);

/** 0111 1111 1000 10sz		sstr%s */
  ID(sstr); BWL(sz);

/*----------------------------------------------------------------------*/
/* RMPA									*/

/** 0111 1111 1000 11sz		rmpa%s */
  ID(rmpa); BWL(sz); F_OS__;

/*----------------------------------------------------------------------*/
/* HI/LO stuff								*/

/** 1111 1101 0000 a000 srca srcb	mulhi	%1, %2, %0 */
  ID(mulhi); DR(a+32); SR(srca); S2R(srcb); F_____;

/** 1111 1101 0000 a001 srca srcb	mullo	%1, %2, %0 */
  ID(mullo); DR(a+32); SR(srca); S2R(srcb); F_____;

/** 1111 1101 0000 a100 srca srcb	machi	%1, %2, %0 */
  ID(machi); DR(a+32); SR(srca); S2R(srcb); F_____;

/** 1111 1101 0000 a101 srca srcb	maclo	%1, %2, %0 */
  ID(maclo); DR(a+32); SR(srca); S2R(srcb); F_____;

/** 1111 1101 0001 0111 a000 rsrc	mvtachi	%1, %0 */
  ID(mvtachi); DR(a+32); SR(rsrc); F_____;

/** 1111 1101 0001 0111 a001 rsrc	mvtaclo	%1, %0 */
  ID(mvtaclo); DR(a+32); SR(rsrc); F_____;

/** 1111 1101 0001 111i a m00 rdst	mvfachi	#%2, %1, %0 */
  ID(mvfachi); S2C(((i^1)<<1)|m); SR(a+32); DR(rdst); F_____;

/** 1111 1101 0001 111i a m10 rdst	mvfacmi	#%2, %1, %0 */
  ID(mvfacmi); S2C(((i^1)<<1)|m); SR(a+32); DR(rdst); F_____;

/** 1111 1101 0001 111i a m01 rdst	mvfaclo	#%2, %1, %0 */
  ID(mvfaclo); S2C(((i^1)<<1)|m); SR(a+32); DR(rdst); F_____;

/** 1111 1101 0001 1000 a00i 0000	racw	#%1, %0 */
  ID(racw); SC(i+1); DR(a+32); F_____;

/*----------------------------------------------------------------------*/
/* SAT									*/

/** 0111 1110 0011 rdst		sat	%0 */
  ID(sat); DR (rdst);

/** 0111 1111 1001 0011		satr */
  ID(satr);

/*----------------------------------------------------------------------*/
/* FLOAT								*/

/** 1111 1101 0111 0010 0010 rdst	fadd	#%1, %0 */
  ID(fadd); DR(rdst); SC(IMM(0)); F__SZ_;

/** 1111 1100 1000 10sd rsrc rdst	fadd	%1%S1, %0 */
  ID(fadd); DR(rdst); SD(sd, rsrc, LSIZE); F__SZ_;

/** 1111 1101 0111 0010 0001 rdst	fcmp	#%1, %0 */
  ID(fcmp); DR(rdst); SC(IMM(0)); F_OSZ_;

/** 1111 1100 1000 01sd rsrc rdst	fcmp	%1%S1, %0 */
  ID(fcmp); DR(rdst); SD(sd, rsrc, LSIZE); F_OSZ_;

/** 1111 1101 0111 0010 0000 rdst	fsub	#%1, %0 */
  ID(fsub); DR(rdst); SC(IMM(0)); F__SZ_;

/** 1111 1100 1000 00sd rsrc rdst	fsub	%1%S1, %0 */
  ID(fsub); DR(rdst); SD(sd, rsrc, LSIZE); F__SZ_;

/** 1111 1100 1001 01sd rsrc rdst	ftoi	%1%S1, %0 */
  ID(ftoi); DR(rdst); SD(sd, rsrc, LSIZE); F__SZ_;

/** 1111 1101 0111 0010 0011 rdst	fmul	#%1, %0 */
  ID(fmul); DR(rdst); SC(IMM(0)); F__SZ_;

/** 1111 1100 1000 11sd rsrc rdst	fmul	%1%S1, %0 */
  ID(fmul); DR(rdst); SD(sd, rsrc, LSIZE); F__SZ_;

/** 1111 1101 0111 0010 0100 rdst	fdiv	#%1, %0 */
  ID(fdiv); DR(rdst); SC(IMM(0)); F__SZ_;

/** 1111 1100 1001 00sd rsrc rdst	fdiv	%1%S1, %0 */
  ID(fdiv); DR(rdst); SD(sd, rsrc, LSIZE); F__SZ_;

/** 1111 1100 1001 10sd rsrc rdst	round	%1%S1, %0 */
  ID(round); DR(rdst); SD(sd, rsrc, LSIZE); F__SZ_;

/** 1111 1100 0100 01sd rsrc rdst	itof	%1%S1, %0 */
  ID(itof); DR (rdst); SP(sd, rsrc); F__SZ_;

/** 0000 0110 mx10 00sd 0001 0001 rsrc rdst	itof	%1%S1, %0 */
  ID(itof); DR (rdst); SPm(sd, rsrc, mx); F__SZ_;

/*----------------------------------------------------------------------*/
/* BIT OPS								*/

/** 1111 00sd rdst 0bit			bset	#%1, %0%S0 */
  ID(bset); BWL(BSIZE); SC(bit); DD(sd, rdst, BSIZE); F_____;

/** 1111 1100 0110 00sd rdst rsrc	bset	%1, %0%S0 */
  ID(bset); BWL(BSIZE); SR(rsrc); DD(sd, rdst, BSIZE); F_____;
  if (sd == 3) /* bset reg,reg */
    BWL(LSIZE);

/** 0111 100b ittt rdst			bset	#%1, %0 */
  ID(bset); BWL(LSIZE); SC(b*16+ittt); DR(rdst); F_____;


/** 1111 00sd rdst 1bit			bclr	#%1, %0%S0 */
  ID(bclr); BWL(BSIZE); SC(bit); DD(sd, rdst, BSIZE); F_____;

/** 1111 1100 0110 01sd rdst rsrc	bclr	%1, %0%S0 */
  ID(bclr); BWL(BSIZE); SR(rsrc); DD(sd, rdst, BSIZE); F_____;
  if (sd == 3) /* bset reg,reg */
    BWL(LSIZE);

/** 0111 101b ittt rdst			bclr	#%1, %0 */
  ID(bclr); BWL(LSIZE); SC(b*16+ittt); DR(rdst); F_____;


/** 1111 01sd rdst 0bit			btst	#%2, %1%S1 */
  ID(btst); BWL(BSIZE); S2C(bit); SD(sd, rdst, BSIZE); F___ZC;

/** 1111 1100 0110 10sd rdst rsrc	btst	%2, %1%S1 */
  ID(btst); BWL(BSIZE); S2R(rsrc); SD(sd, rdst, BSIZE); F___ZC;
  if (sd == 3) /* bset reg,reg */
    BWL(LSIZE);

/** 0111 110b ittt rdst			btst	#%2, %1 */
  ID(btst); BWL(LSIZE); S2C(b*16+ittt); SR(rdst); F___ZC;


/** 1111 1100 111bit sd rdst 1111	bnot	#%1, %0%S0 */
  ID(bnot); BWL(BSIZE); SC(bit); DD(sd, rdst, BSIZE);

/** 1111 1100 0110 11sd rdst rsrc	bnot	%1, %0%S0 */
  ID(bnot); BWL(BSIZE); SR(rsrc); DD(sd, rdst, BSIZE);
  if (sd == 3) /* bset reg,reg */
    BWL(LSIZE);

/** 1111 1101 111bittt 1111 rdst	bnot	#%1, %0 */
  ID(bnot); BWL(LSIZE); SC(bittt); DR(rdst);


/** 1111 1100 111bit sd rdst cond	bm%2	#%1, %0%S0 */
  ID(bmcc); BWL(BSIZE); S2cc(cond); SC(bit); DD(sd, rdst, BSIZE);

/** 1111 1101 111 bittt cond rdst	bm%2	#%1, %0%S0 */
  ID(bmcc); BWL(LSIZE); S2cc(cond); SC(bittt); DR(rdst);

/*----------------------------------------------------------------------*/
/* CONTROL REGISTERS							*/

/** 0111 1111 1011 rdst			clrpsw	%0 */
  ID(clrpsw); DF(rdst);

/** 0111 1111 1010 rdst			setpsw	%0 */
  ID(setpsw); DF(rdst);

/** 0111 0101 0111 0000 0000 immm	mvtipl	#%1 */
  ID(mvtipl); SC(immm);

/** 0111 1110 111 crdst			popc	%0 */
  ID(mov); OP(1, RX_Operand_Postinc, 0, 0); DR(crdst + 16);

/** 0111 1110 110 crsrc			pushc	%1 */
  ID(mov); OP(0, RX_Operand_Predec, 0, 0); SR(crsrc + 16);

/** 1111 1101 0111 im11 000crdst	mvtc	#%1, %0 */
  ID(mov); SC(IMMex(im)); DR(crdst + 16);

/** 1111 1101 0110 100c rsrc rdst	mvtc	%1, %0 */
  ID(mov); SR(rsrc); DR(c*16+rdst + 16);

/** 1111 1101 0110 101s rsrc rdst	mvfc	%1, %0 */
  ID(mov); SR((s*16+rsrc) + 16); DR(rdst);

/*----------------------------------------------------------------------*/
/* INTERRUPTS								*/

/** 0111 1111 1001 0100		rtfi */
  ID(rtfi);

/** 0111 1111 1001 0101		rte */
  ID(rte);

/** 0000 0000			brk */
  ID(brk);

/** 0000 0001			dbt */
  ID(dbt);

/** 0111 0101 0110 0000		int #%1 */
  ID(int); SC(IMM(1));

/** 0111 1111 1001 0110		wait */
  ID(wait);

/*----------------------------------------------------------------------*/
/* SCcnd								*/

/** 1111 1100 1101 sz sd rdst cond	sc%1%s	%0 */
  ID(sccnd); BWL(sz); DD (sd, rdst, sz); Scc(cond);

/*----------------------------------------------------------------------*/
/* RXv2 enhanced							*/

/** 1111 1101 0010 0111 rdst rsrc	movco	%1, [%0] */
   ID(movco); SR(rsrc); DR(rdst); F_____;

/** 1111 1101 0010 1111 rsrc rdst	movli	[%1], %0 */
   ID(movli); SR(rsrc); DR(rdst); F_____;

/** 1111 1100 0100 1011 rsrc rdst	stz	%1, %0 */
  ID(stcc); SR(rsrc); DR(rdst); S2cc(RXC_z);

/** 1111 1100 0100 1111 rsrc rdst	stnz	%1, %0 */
  ID(stcc); SR(rsrc); DR(rdst); S2cc(RXC_nz);

/** 1111 1101 0000 a111 srca srcb 	emaca	%1, %2, %0 */
  ID(emaca); DR(a+32); SR(srca); S2R(srcb); F_____;

/** 1111 1101 0100 a111 srca srcb 	emsba	%1, %2, %0 */
  ID(emsba); DR(a+32); SR(srca); S2R(srcb); F_____;

/** 1111 1101 0000 a011 srca srcb 	emula	%1, %2, %0 */
  ID(emula); DR(a+32); SR(srca); S2R(srcb); F_____;

/** 1111 1101 0000 a110 srca srcb	maclh	%1, %2, %0 */
  ID(maclh); DR(a+32); SR(srca); S2R(srcb); F_____;

/** 1111 1101 0100 a100 srca srcb	msbhi	%1, %2, %0 */
  ID(msbhi); DR(a+32); SR(srca); S2R(srcb); F_____;

/** 1111 1101 0100 a110 srca srcb	msblh	%1, %2, %0 */
  ID(msblh); DR(a+32); SR(srca); S2R(srcb); F_____;

/** 1111 1101 0100 a101 srca srcb	msblo	%1, %2, %0 */
  ID(msblo); DR(a+32); SR(srca); S2R(srcb); F_____;

/** 1111 1101 0000 a010 srca srcb	mullh	%1, %2, %0 */
  ID(mullh); DR(a+32); SR(srca); S2R(srcb); F_____;

/** 1111 1101 0001 111i a m11 rdst	mvfacgu	#%2, %1, %0 */
  ID(mvfacgu); S2C(((i^1)<<1)|m); SR(a+32); DR(rdst); F_____;

/** 1111 1101 0001 0111 a011 rsrc	mvtacgu	%1, %0 */
  ID(mvtacgu); SR(rsrc); DR(a+32); F_____;

/** 1111 1101 0001 1001 a00i 0000	racl	#%1, %0 */
  ID(racl); SC(i+1); DR(a+32); F_____;

/** 1111 1101 0001 1001 a10i 0000	rdacl	#%1, %0 */
  ID(rdacl); SC(i+1); DR(a+32); F_____;

/** 1111 1101 0001 1000 a10i 0000	rdacw	#%1, %0 */
  ID(rdacw); SC(i+1); DR(a+32); F_____;

/** 1111 1111 1010 rdst srca srcb	fadd	%2, %1, %0 */
  ID(fadd); DR(rdst); SR(srcb); S2R(srca); F__SZ_;

/** 1111 1111 1000 rdst srca srcb	fsub	%2, %1, %0 */
  ID(fsub); DR(rdst); SR(srcb); S2R(srca); F__SZ_;

/** 1111 1111 1011 rdst srca srcb	fmul	%2, %1, %0 */
  ID(fmul); DR(rdst); SR(srcb); S2R(srca); F__SZ_;

/** 1111 1100 1010 00sd rsrc rdst	fsqrt	%1%S1, %0 */
  ID(fsqrt); DR(rdst); SD(sd, rsrc, LSIZE); F__SZ_;

/** 1111 1100 1010 01sd rsrc rdst	ftou	%1%S1, %0 */
  ID(ftou); DR(rdst); SD(sd, rsrc, LSIZE); F__SZ_;

/** 1111 1100 0101 01sd rsrc rdst	utof	%1%S1, %0 */
  ID(utof); DR (rdst); SP(sd, rsrc); F__SZ_;

/** 0000 0110 mx10 00sd 0001 0101 rsrc rdst	utof	%1%S1, %0 */
  ID(utof); DR (rdst); SPm(sd, rsrc, mx); F__SZ_;

/*----------------------------------------------------------------------*/
/* RXv3 enhanced							*/

/** 1111 1111 0110 rdst srca srcb	xor	%2, %1, %0 */
  ID(xor); DR(rdst); SR(srcb); S2R(srca); F__SZ_;

/** 1111 1100 0101 1110 rsrc rdst	bfmov	%bf */
  ID(bfmov); DR(rdst); SR(rsrc); S2C(IMM(2)); F_____;

/** 1111 1100 0101 1010 rsrc rdst	bfmovz	%bf */
  ID(bfmovz); DR(rdst); SR(rsrc); S2C(IMM(2)); F_____;

/** 1111 1101 0111 0110 1101 rsrc 0000 0000 	rstr	%1 */
  ID(rstr); SR(rsrc); F_____;

/** 1111 1101 0111 0110 1111 0000 	rstr	#%1 */
  ID(rstr); SC(IMM(1)); F_____;

/** 1111 1101 0111 0110 1100 rsrc 0000 0000 	save	%1 */
  ID(save); SR(rsrc); F_____;

/** 1111 1101 0111 0110 1110 0000	save	#%1 */
  ID(save); SC(IMM(1)); F_____;

/** 1111 1101 0111 0111 1000 rsrc rdst 001s	dmov%s	%1, %0 */
  ID(dmov); DDRH(rdst); SR(rsrc); DL(s); F_____;

/** 1111 1101 0111 0111 1000 rsrc rdst 0000	dmov.l	%1, %0 */
  ID(dmov); DDRL(rdst); SR(rsrc); F_____;

/** 1111 1101 0111 0101 1000 rdst rsrc 0010	dmov.l	%1, %0 */
  ID(dmov); DR(rdst); SDRH(rsrc); F_____;

/** 1111 1101 0111 0101 1000 rdst rsrc 0000	dmov.l	%1, %0 */
  ID(dmov); DR(rdst); SDRL(rsrc); F_____;

/** 0111 0110 1001 0000 rsrc 1100 rdst 0000	dmov.d	%1, %0 */
  ID(dmov); DDR(rdst); SDR(rsrc); F_____;

/** 1111 1100 0111 1000 rdst 1000 rsrc 0000	dmov.d	%1, %0 */
  ID(dmov); DD(0, rdst, 0); SDR(rsrc); F_____;

/** 1111 1100 0111 10sz rdst 1000	dmov.d	%1, %0 */
  int rsrc;
  rx_disp(0, sz, rdst, RX_Double, ld);
  rsrc = GETBYTE();
  if (rsrc & 0x0f)
    UNSUPPORTED();
  else {
    ID(dmov); SDR(rsrc >> 4); F_____;
  }

/** 1111 1100 1100 1000 rsrc 1000 rdst 0000	dmov.d	%1, %0 */
  ID(dmov); SD(sd, rsrc, 0) ; DDR(rdst); F_____;

/** 1111 1100 1100 10sz rsrc 1000	dmov.d	%1, %0 */
  int rdst;
  rx_disp(1, sz, rsrc, RX_Double, ld);
  rdst = GETBYTE();
  if (rdst & 0x0f)
    UNSUPPORTED();
  else {
    ID(dmov); DDR(rdst >> 4); F_____;
  }

/** 1111 1001 0000 0011 rdst 001s	dmov%s	#%1, %0 */
  ID(dmov); DDRH(rdst); DL(s); SC(IMMex(0)); F_____;

/** 1111 1001 0000 0011 rdst 0000	dmov.l	#%1, %0 */
  ID(dmov); DDRL(rdst); SC(IMMex(0)); F_____;

/** 0111 0101 1011 1000 rdst rnum	dpopm.d	%1-%2 */
  ID(dpopm); SDR(rdst); S2DR(rdst + rnum); F_____;

/** 0111 0101 1010 1000 rdst rnum	dpopm.l	%1-%2 */
  ID(dpopm); SCR(rdst); S2CR(rdst + rnum); F_____;

/** 0111 0101 1011 0000 rdst rnum	dpushm.d	%1-%2 */
  ID(dpushm); SDR(rdst); S2DR(rdst + rnum); F_____;

/** 0111 0101 1010 0000 rdst rnum	dpushm.l	%1-%2 */
  ID(dpushm); SCR(rdst); S2CR(rdst + rnum); F_____;

/** 1111 1101 0111 0101 1000 rdst rsrc 0100	mvfdc	%1, %0 */
  ID(mvfdc); DR(rdst); SCR(rsrc); F_____;

/** 0111 0101 1001 0000 0001 1011	mvfdr */
  ID(mvfdr); F_____;

/** 1111 1101 0111 0111 1000 rdst rsrc 0100	mvtdc	%1, %0 */
  ID(mvtdc); DCR(rdst); SR(rsrc); F_____;

/** 0111 0110 1001 0000 rsrc 1100 rdst 0001	dabs	%1, %0 */
  ID(dabs); DDR(rdst); SDR(rsrc); F_____;

/** 0111 0110 1001 0000 srcb 0000 rdst srca	dadd	%1, %2, %0 */
  ID(dadd); DDR(rdst); SDR(srca); S2DR(srcb); F_____;

/** 0111 0110 1001 0000 srcb 1000 cond srca	dcmp%0	%1, %2 */
  ID(dcmp); DCND(cond); SDR(srca); S2DR(srcb); F_____;

/** 0111 0110 1001 0000 srcb 0101 rdst srca	ddiv	%1, %2, %0 */
  ID(ddiv); DDR(rdst); SDR(srca); S2DR(srcb); F_____;

/** 0111 0110 1001 0000 srcb 0010 rdst srca	dmul	%1, %2, %0 */
  ID(dmul); DDR(rdst); SDR(srca); S2DR(srcb); F_____;

/** 0111 0110 1001 0000 rsrc 1100 rdst 0010	dneg	%1, %0 */
  ID(dneg); DDR(rdst); SDR(rsrc); F_____;

/** 0111 0110 1001 0000 rsrc 1101 rdst 1101	dround	%1, %0 */
  ID(dround); DDR(rdst); SDR(rsrc); F_____;

/** 0111 0110 1001 0000 rsrc 1101 rdst 0000	dsqrt	%1, %0 */
  ID(dsqrt); DDR(rdst); SDR(rsrc); F_____;

/** 0111 0110 1001 0000 srcb 0001 rdst srca	dsub	%1, %2, %0 */
  ID(dsub); DDR(rdst); SDR(srca); S2DR(srcb); F_____;

/** 0111 0110 1001 0000 rsrc 1101 rdst 1100	dtof	%1, %0 */
  ID(dtof); DDR(rdst); SDR(rsrc); F_____;

/** 0111 0110 1001 0000 rsrc 1101 rdst 1000	dtoi	%1, %0 */
  ID(dtoi); DDR(rdst); SDR(rsrc); F_____;

/** 0111 0110 1001 0000 rsrc 1101 rdst 1001	dtou	%1, %0 */
  ID(dtou); DDR(rdst); SDR(rsrc); F_____;

/** 1111 1101 0111 0111 1000 rsrc rdst 1010	ftod	%1, %0 */
  ID(ftod); DDR(rdst); SR(rsrc); F_____;

/** 1111 1101 0111 0111 1000 rsrc rdst 1001	itod	%1, %0 */
  ID(itod); DDR(rdst); SR(rsrc); F_____;

/** 1111 1101 0111 0111 1000 rsrc rdst 1101	utod	%1, %0 */
  ID(dsqrt); DDR(rdst); SR(rsrc); F_____;

/** */

  return rx->n_bytes;
}
a id='n2305' href='#n2305'>2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 2904 2905 2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942 2943 2944 2945 2946 2947 2948 2949 2950 2951 2952 2953 2954 2955 2956 2957 2958 2959 2960 2961 2962 2963 2964 2965 2966 2967 2968 2969 2970 2971 2972 2973 2974 2975 2976 2977 2978 2979 2980 2981 2982 2983 2984 2985 2986 2987 2988 2989 2990 2991 2992 2993 2994 2995 2996 2997 2998 2999 3000 3001 3002 3003 3004 3005 3006 3007 3008 3009 3010 3011 3012 3013 3014 3015 3016 3017 3018 3019 3020 3021 3022 3023 3024 3025 3026 3027 3028 3029 3030 3031 3032 3033 3034 3035 3036 3037 3038 3039 3040 3041 3042 3043 3044 3045 3046 3047 3048 3049 3050 3051 3052 3053 3054 3055 3056 3057 3058 3059 3060 3061 3062 3063 3064 3065 3066 3067 3068 3069 3070 3071 3072 3073 3074 3075 3076 3077 3078 3079 3080 3081 3082 3083 3084 3085 3086 3087 3088 3089 3090 3091 3092 3093 3094 3095 3096 3097 3098 3099 3100 3101 3102 3103 3104 3105 3106 3107 3108 3109 3110 3111 3112 3113 3114 3115 3116 3117 3118 3119 3120 3121 3122 3123 3124 3125 3126 3127 3128 3129 3130 3131 3132 3133 3134 3135 3136 3137 3138 3139 3140 3141 3142 3143 3144 3145 3146 3147 3148 3149 3150 3151 3152 3153 3154 3155 3156 3157 3158 3159 3160 3161 3162 3163 3164 3165 3166 3167 3168 3169 3170 3171 3172 3173 3174 3175 3176 3177 3178 3179 3180 3181 3182 3183 3184 3185 3186 3187 3188 3189 3190 3191 3192 3193 3194 3195 3196 3197 3198 3199 3200 3201 3202 3203 3204 3205 3206 3207 3208 3209 3210 3211 3212 3213 3214 3215 3216 3217 3218 3219 3220 3221 3222 3223 3224 3225 3226 3227 3228 3229 3230 3231 3232 3233 3234 3235 3236 3237 3238 3239 3240 3241 3242 3243 3244 3245 3246 3247 3248 3249 3250 3251 3252 3253 3254 3255 3256 3257 3258 3259 3260 3261 3262 3263 3264 3265 3266 3267 3268 3269 3270 3271 3272 3273 3274 3275 3276 3277 3278 3279 3280 3281 3282 3283 3284 3285 3286 3287 3288 3289 3290 3291 3292 3293 3294 3295 3296 3297 3298 3299 3300 3301 3302 3303 3304 3305 3306 3307 3308 3309 3310 3311 3312 3313 3314 3315 3316 3317 3318 3319 3320 3321 3322 3323 3324 3325 3326 3327 3328 3329 3330 3331 3332 3333 3334 3335 3336 3337 3338 3339 3340 3341 3342 3343 3344 3345 3346 3347 3348 3349 3350 3351 3352 3353 3354 3355 3356 3357 3358 3359 3360 3361 3362 3363 3364 3365 3366 3367 3368 3369 3370 3371 3372 3373 3374 3375 3376 3377 3378 3379 3380 3381 3382 3383 3384 3385 3386 3387 3388 3389 3390 3391
/* BFD back-end for mmo objects (MMIX-specific object-format).
   Copyright (C) 2001-2018 Free Software Foundation, Inc.
   Written by Hans-Peter Nilsson (hp@bitrange.com).
   Infrastructure and other bits originally copied from srec.c and
   binary.c.

   This file is part of BFD, the Binary File Descriptor library.

   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, write to the Free Software
   Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
   MA 02110-1301, USA.  */


/*
SECTION
	mmo backend

	The mmo object format is used exclusively together with Professor
	Donald E.@: Knuth's educational 64-bit processor MMIX.  The simulator
	@command{mmix} which is available at
	@url{http://mmix.cs.hm.edu/src/index.html}
	understands this format.  That package also includes a combined
	assembler and linker called @command{mmixal}.  The mmo format has
	no advantages feature-wise compared to e.g. ELF.  It is a simple
	non-relocatable object format with no support for archives or
	debugging information, except for symbol value information and
	line numbers (which is not yet implemented in BFD).  See
	@url{http://mmix.cs.hm.edu/} for more
	information about MMIX.  The ELF format is used for intermediate
	object files in the BFD implementation.

@c We want to xref the symbol table node.  A feature in "chew"
@c requires that "commands" do not contain spaces in the
@c arguments.  Hence the hyphen in "Symbol-table".
@menu
@* File layout::
@* Symbol-table::
@* mmo section mapping::
@end menu

INODE
File layout, Symbol-table, mmo, mmo
SUBSECTION
	File layout

	The mmo file contents is not partitioned into named sections as
	with e.g.@: ELF.  Memory areas is formed by specifying the
	location of the data that follows.  Only the memory area
	@samp{0x0000@dots{}00} to @samp{0x01ff@dots{}ff} is executable, so
	it is used for code (and constants) and the area
	@samp{0x2000@dots{}00} to @samp{0x20ff@dots{}ff} is used for
	writable data.  @xref{mmo section mapping}.

	There is provision for specifying ``special data'' of 65536
	different types.  We use type 80 (decimal), arbitrarily chosen the
	same as the ELF <<e_machine>> number for MMIX, filling it with
	section information normally found in ELF objects. @xref{mmo
	section mapping}.

	Contents is entered as 32-bit words, xor:ed over previous
	contents, always zero-initialized.  A word that starts with the
	byte @samp{0x98} forms a command called a @samp{lopcode}, where
	the next byte distinguished between the thirteen lopcodes.  The
	two remaining bytes, called the @samp{Y} and @samp{Z} fields, or
	the @samp{YZ} field (a 16-bit big-endian number), are used for
	various purposes different for each lopcode.  As documented in
	@url{http://mmix.cs.hm.edu/doc/mmixal.pdf},
	the lopcodes are:

	@table @code
	@item lop_quote
	0x98000001.  The next word is contents, regardless of whether it
	starts with 0x98 or not.

	@item lop_loc
	0x9801YYZZ, where @samp{Z} is 1 or 2.  This is a location
	directive, setting the location for the next data to the next
	32-bit word (for @math{Z = 1}) or 64-bit word (for @math{Z = 2}),
	plus @math{Y * 2^56}.  Normally @samp{Y} is 0 for the text segment
	and 2 for the data segment.  Beware that the low bits of non-
	tetrabyte-aligned values are silently discarded when being
	automatically incremented and when storing contents (in contrast
	to e.g. its use as current location when followed by lop_fixo
	et al before the next possibly-quoted tetrabyte contents).

	@item lop_skip
	0x9802YYZZ.  Increase the current location by @samp{YZ} bytes.

	@item lop_fixo
	0x9803YYZZ, where @samp{Z} is 1 or 2.  Store the current location
	as 64 bits into the location pointed to by the next 32-bit
	(@math{Z = 1}) or 64-bit (@math{Z = 2}) word, plus @math{Y *
	2^56}.

	@item lop_fixr
	0x9804YYZZ.  @samp{YZ} is stored into the current location plus
	@math{2 - 4 * YZ}.

	@item lop_fixrx
	0x980500ZZ.  @samp{Z} is 16 or 24.  A value @samp{L} derived from
	the following 32-bit word are used in a manner similar to
	@samp{YZ} in lop_fixr: it is xor:ed into the current location
	minus @math{4 * L}.  The first byte of the word is 0 or 1.  If it
	is 1, then @math{L = (@var{lowest 24 bits of word}) - 2^Z}, if 0,
	then @math{L = (@var{lowest 24 bits of word})}.

	@item lop_file
	0x9806YYZZ.  @samp{Y} is the file number, @samp{Z} is count of
	32-bit words.  Set the file number to @samp{Y} and the line
	counter to 0.  The next @math{Z * 4} bytes contain the file name,
	padded with zeros if the count is not a multiple of four.  The
	same @samp{Y} may occur multiple times, but @samp{Z} must be 0 for
	all but the first occurrence.

	@item lop_line
	0x9807YYZZ.  @samp{YZ} is the line number.  Together with
	lop_file, it forms the source location for the next 32-bit word.
	Note that for each non-lopcode 32-bit word, line numbers are
	assumed incremented by one.

	@item lop_spec
	0x9808YYZZ.  @samp{YZ} is the type number.  Data until the next
	lopcode other than lop_quote forms special data of type @samp{YZ}.
	@xref{mmo section mapping}.

	Other types than 80, (or type 80 with a content that does not
	parse) is stored in sections named <<.MMIX.spec_data.@var{n}>>
	where @var{n} is the @samp{YZ}-type.  The flags for such a
	sections say not to allocate or load the data.  The vma is 0.
	Contents of multiple occurrences of special data @var{n} is
	concatenated to the data of the previous lop_spec @var{n}s.  The
	location in data or code at which the lop_spec occurred is lost.

	@item lop_pre
	0x980901ZZ.  The first lopcode in a file.  The @samp{Z} field forms the
	length of header information in 32-bit words, where the first word
	tells the time in seconds since @samp{00:00:00 GMT Jan 1 1970}.

	@item lop_post
	0x980a00ZZ.  @math{Z > 32}.  This lopcode follows after all
	content-generating lopcodes in a program.  The @samp{Z} field
	denotes the value of @samp{rG} at the beginning of the program.
	The following @math{256 - Z} big-endian 64-bit words are loaded
	into global registers @samp{$G} @dots{} @samp{$255}.

	@item lop_stab
	0x980b0000.  The next-to-last lopcode in a program.  Must follow
	immediately after the lop_post lopcode and its data.  After this
	lopcode follows all symbols in a compressed format
	(@pxref{Symbol-table}).

	@item lop_end
	0x980cYYZZ.  The last lopcode in a program.  It must follow the
	lop_stab lopcode and its data.  The @samp{YZ} field contains the
	number of 32-bit words of symbol table information after the
	preceding lop_stab lopcode.
	@end table

	Note that the lopcode "fixups"; <<lop_fixr>>, <<lop_fixrx>> and
	<<lop_fixo>> are not generated by BFD, but are handled.  They are
	generated by <<mmixal>>.

EXAMPLE
	This trivial one-label, one-instruction file:

| :Main TRAP 1,2,3

	can be represented this way in mmo:

| 0x98090101 - lop_pre, one 32-bit word with timestamp.
| <timestamp>
| 0x98010002 - lop_loc, text segment, using a 64-bit address.
|              Note that mmixal does not emit this for the file above.
| 0x00000000 - Address, high 32 bits.
| 0x00000000 - Address, low 32 bits.
| 0x98060002 - lop_file, 2 32-bit words for file-name.
| 0x74657374 - "test"
| 0x2e730000 - ".s\0\0"
| 0x98070001 - lop_line, line 1.
| 0x00010203 - TRAP 1,2,3
| 0x980a00ff - lop_post, setting $255 to 0.
| 0x00000000
| 0x00000000
| 0x980b0000 - lop_stab for ":Main" = 0, serial 1.
| 0x203a4040   @xref{Symbol-table}.
| 0x10404020
| 0x4d206120
| 0x69016e00
| 0x81000000
| 0x980c0005 - lop_end; symbol table contained five 32-bit words.  */

#include "sysdep.h"
#include "bfd.h"
#include "libbfd.h"
#include "libiberty.h"
#include "elf/mmix.h"
#include "opcode/mmix.h"

#define LOP 0x98
#define LOP_QUOTE 0
#define LOP_LOC 1
#define LOP_SKIP 2
#define LOP_FIXO 3
#define LOP_FIXR 4
#define LOP_FIXRX 5
#define LOP_FILE 6
#define LOP_LINE 7
#define LOP_SPEC 8
#define LOP_PRE 9
#define LOP_POST 10
#define LOP_STAB 11
#define LOP_END 12

#define LOP_QUOTE_NEXT ((LOP << 24) | (LOP_QUOTE << 16) | 1)
#define SPEC_DATA_SECTION 80
#define LOP_SPEC_SECTION \
 ((LOP << 24) | (LOP_SPEC << 16) | SPEC_DATA_SECTION)

/* Must be a power of two.  If you change this to be >= 64k, you need a
   new test-case; the ld test b-loc64k.d touches chunk-size problem areas.  */
#define MMO_SEC_CONTENTS_CHUNK_SIZE (1 << 15)

/* An arbitrary number for the maximum length section name size.  */
#define MAX_SECTION_NAME_SIZE (1024 * 1024)

/* A quite arbitrary number for the maximum length section size.  */
#define MAX_ARTIFICIAL_SECTION_SIZE (1024 * 1024 * 1024)

#define MMO3_WCHAR 0x80
#define MMO3_LEFT 0x40
#define MMO3_MIDDLE 0x20
#define MMO3_RIGHT 0x10
#define MMO3_TYPEBITS 0xf
#define MMO3_REGQUAL_BITS 0xf
#define MMO3_UNDEF 2
#define MMO3_DATA 8
#define MMO3_SYMBITS 0x2f

/* Put these everywhere in new code.  */
#define FATAL_DEBUG						\
 _bfd_abort (__FILE__, __LINE__,				\
	     "Internal: Non-debugged code (test-case missing)")

#define BAD_CASE(x)				\
 _bfd_abort (__FILE__, __LINE__,		\
	     "bad case for " #x)

enum mmo_sym_type { mmo_reg_sym, mmo_undef_sym, mmo_data_sym, mmo_abs_sym};

/* When scanning the mmo file, a linked list of mmo_symbol
   structures is built to represent the symbol table (if there is
   one).  */

struct mmo_symbol
  {
    struct mmo_symbol *next;
    char *name;
    bfd_vma value;
    enum mmo_sym_type sym_type;
    unsigned int serno;
  };

struct mmo_data_list_struct
  {
    struct mmo_data_list_struct *next;
    bfd_vma where;
    bfd_size_type size;
    bfd_size_type allocated_size;
    bfd_byte data[1];
  };

typedef struct mmo_data_list_struct mmo_data_list_type;

struct mmo_symbol_trie
  {
    struct mmo_symbol_trie *left;
    struct mmo_symbol_trie *right;
    struct mmo_symbol_trie *middle;

    bfd_byte symchar;

    /* A zero name means there's nothing here.  */
    struct mmo_symbol sym;
  };

/* The mmo tdata information.  */

struct mmo_data_struct
  {
    struct mmo_symbol *symbols;
    struct mmo_symbol *symtail;
    asymbol *csymbols;

    /* File representation of time (NULL) when this file was created.  */
    bfd_byte created[4];

    /* When we're reading bytes recursively, check this occasionally.
       Also holds write errors.  */
    bfd_boolean have_error;

    /* Max symbol length that may appear in the lop_stab table.  Note that
       this table might just hold a subset of symbols for not-really large
       programs, as it can only be 65536 * 4 bytes large.  */
    int max_symbol_length;

    /* Here's the symbol we build in lop_stab.  */
    char *lop_stab_symbol;

    /* Index into lop_stab_symbol for the next character when parsing the
       symbol information.  */
    int symbol_position;

    /* When creating arbitrary sections, we need to count section numbers.  */
    int sec_no;

    /* When writing or reading byte-wise, we need to count the bytes
       within a 32-bit word.  */
    int byte_no;

    /* We also need a buffer to hold the bytes we count reading or writing.  */
    bfd_byte buf[4];

    /* Whether we've calculated symbol consistency requirement yet.  We do this
       when-needed, which must be at some time after all section
       contents is known.  */
    bfd_boolean symbol_consistency_override_calculated;

    /* Whether to consistency-check symbol values, in particular "Main".  */
    bfd_boolean ignore_symbol_consistency;
  };

typedef struct mmo_data_struct tdata_type;

struct mmo_section_data_struct
  {
    mmo_data_list_type *head;
    mmo_data_list_type *tail;
  };

#define mmo_section_data(sec) \
  ((struct mmo_section_data_struct *) (sec)->used_by_bfd)

/* These structures are used in bfd_map_over_sections constructs.  */

/* Used when writing out sections; all but the register contents section
   which is stored in reg_section.  */
struct mmo_write_sec_info
  {
    asection *reg_section;
    bfd_boolean retval;
  };

/* Used when trying to find a section corresponding to addr.  */
struct mmo_find_sec_info
  {
    asection *sec;
    bfd_vma addr;
  };

static bfd_boolean mmo_bfd_copy_private_bfd_data (bfd *, bfd *);
static void mmo_write_section_unless_reg_contents (bfd *, asection *, void *);
static void mmo_find_sec_w_addr (bfd *, asection *, void *);
static void mmo_find_sec_w_addr_grow (bfd *, asection *, void *);
static asection *mmo_make_section (bfd *, const char *);
static void mmo_get_symbol_info (bfd *, asymbol *, symbol_info *);
static void mmo_print_symbol (bfd *, void *, asymbol *,
			      bfd_print_symbol_type);
static void mmo_init (void);
static bfd_boolean mmo_mkobject (bfd *);
static bfd_boolean mmo_scan (bfd *);
static asection *mmo_decide_section (bfd *, bfd_vma);
static asection *mmo_get_generic_spec_data_section (bfd *, int);
static asection *mmo_get_spec_section (bfd *, int);
static INLINE bfd_byte *mmo_get_loc (asection *, bfd_vma, int);
static void mmo_xore_64 (asection *, bfd_vma vma, bfd_vma value);
static void mmo_xore_32 (asection *, bfd_vma vma, unsigned int);
static void mmo_xore_16 (asection *, bfd_vma vma, unsigned int);
static const bfd_target *mmo_object_p (bfd *);
static void mmo_map_set_sizes (bfd *, asection *, void *);
static bfd_boolean mmo_get_symbols (bfd *);
static bfd_boolean mmo_create_symbol (bfd *, const char *, bfd_vma,
				      enum mmo_sym_type, unsigned int);
static bfd_boolean mmo_get_section_contents (bfd *, asection *, void *,
					     file_ptr, bfd_size_type);
static long mmo_get_symtab_upper_bound (bfd *);
static long mmo_canonicalize_symtab (bfd *, asymbol **);
static void mmo_get_symbol_info (bfd *, asymbol *, symbol_info *);
static void mmo_print_symbol (bfd *, void *, asymbol *,
			      bfd_print_symbol_type);
static bfd_boolean mmo_set_section_contents (bfd *, sec_ptr, const void *,
					     file_ptr, bfd_size_type);
static int mmo_sizeof_headers (bfd *, struct bfd_link_info *);
static bfd_boolean mmo_internal_write_header (bfd *);
static bfd_boolean mmo_internal_write_post (bfd *, int, asection *);
static bfd_boolean mmo_internal_add_3_sym (bfd *, struct mmo_symbol_trie *,
					   const struct mmo_symbol *);
static unsigned int mmo_internal_3_length (bfd *, struct mmo_symbol_trie *);
static void mmo_internal_3_dump (bfd *, struct mmo_symbol_trie *);
static void mmo_beb128_out (bfd *, int, int);
static bfd_boolean mmo_internal_write_section (bfd *, asection *);
static void mmo_write_tetra (bfd *, unsigned int);
static void mmo_write_tetra_raw (bfd *, unsigned int);
static void mmo_write_octa (bfd *, bfd_vma);
static void mmo_write_octa_raw (bfd *, bfd_vma);
static bfd_boolean mmo_write_chunk (bfd *, const bfd_byte *, unsigned int);
static bfd_boolean mmo_flush_chunk (bfd *);
static bfd_boolean mmo_write_loc_chunk (bfd *, bfd_vma, const bfd_byte *,
					unsigned int, bfd_vma *);
static bfd_boolean mmo_write_chunk_list (bfd *, mmo_data_list_type *);
static bfd_boolean mmo_write_loc_chunk_list (bfd *, mmo_data_list_type *);
static bfd_boolean mmo_write_symbols_and_terminator (bfd *);
static flagword mmo_sec_flags_from_bfd_flags (flagword);
static flagword bfd_sec_flags_from_mmo_flags (flagword);
static bfd_byte mmo_get_byte (bfd *);
static void mmo_write_byte (bfd *, bfd_byte);
static bfd_boolean mmo_new_section_hook (bfd *, asection *);
static int mmo_sort_mmo_symbols (const void *, const void *);
static bfd_boolean mmo_write_object_contents (bfd *);
static bfd_boolean mmo_write_section_description (bfd *, asection *);
static bfd_boolean mmo_has_leading_or_trailing_zero_tetra_p (bfd *,
							     asection *);

/* Global "const" variables initialized once.  Must not depend on
   particular input or caller; put such things into the bfd or elsewhere.
   Look ma, no static per-invocation data!  */

static
char valid_mmo_symbol_character_set[/* A-Z a-z (we assume consecutive
				       codes; sorry EBCDIC:ers!).  */
				    + 'Z' - 'A' + 1 + 'z' - 'a' + 1
				    /* Digits.  */
				    + 10
				    /* ':' and '_'.  */
				    + 1 + 1
				    /* Codes higher than 126.  */
				    + 256 - 126
				    /* Ending zero.  */
				    + 1];


/* Get section SECNAME or create one if it doesn't exist.  When creating
   one, new memory for the name is allocated.  */

static asection *
mmo_make_section (bfd *abfd, const char *secname)
{
  asection *sec = bfd_get_section_by_name (abfd, secname);

  if (sec == NULL)
    {
      char *newsecname = strdup (secname);

      if (newsecname == NULL)
	{
	  _bfd_error_handler
	    /* xgettext:c-format */
	    (_("%pB: no core to allocate section name %s"),
	     abfd, secname);
	  bfd_set_error (bfd_error_system_call);
	  return NULL;
	}
      sec = bfd_make_section (abfd, newsecname);
    }

  return sec;
}

/* Nothing to do, but keep as a placeholder if we need it.
   Note that state that might differ between bfd:s must not be initialized
   here, nor must it be static.  Add it to tdata information instead.  */

static void
mmo_init (void)
{
  static bfd_boolean inited = FALSE;
  int i = 0;
  int j = 0;
  static const char letters[]
    = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789:_";

  if (inited)
    return;
  inited = TRUE;

  /* Fill in the set of valid symbol characters.  */
  strcpy (valid_mmo_symbol_character_set, letters);
  i = strlen (letters);

  for (j = 126; j < 256; j++)
    valid_mmo_symbol_character_set[i++] = j;
}

/* Check whether an existing file is an mmo file.  */

static const bfd_target *
mmo_object_p (bfd *abfd)
{
  struct stat statbuf;
  bfd_byte b[4];

  mmo_init ();

  if (bfd_stat (abfd, &statbuf) < 0
      || bfd_seek (abfd, (file_ptr) 0, SEEK_SET) != 0
      || bfd_bread (b, 4, abfd) != 4)
    goto bad_final;

  /* All mmo files are a multiple of four bytes long.
     Only recognize version one.  */
  if ((statbuf.st_size % 4) != 0
      || b[0] != LOP || b[1] != LOP_PRE || b[2] != 1)
    goto bad_format;

  /* Get the last 32-bit word.  */
  if (bfd_seek (abfd, (file_ptr) statbuf.st_size - 4, SEEK_SET) != 0
      || bfd_bread (b, 4, abfd) != 4)
    goto bad_final;

  /* Check if the file ends in a lop_end lopcode. */
  if (b[0] != LOP || b[1] != LOP_END || ! mmo_mkobject (abfd))
    goto bad_format;

  /* Compute an upper bound on the max symbol length.  Not really
     important as all of the symbol information can only be 256k.  */
  abfd->tdata.mmo_data->max_symbol_length = (b[2] * 256 + b[3]) * 4;
  abfd->tdata.mmo_data->lop_stab_symbol
    = bfd_malloc (abfd->tdata.mmo_data->max_symbol_length + 1);

  if (abfd->tdata.mmo_data->lop_stab_symbol == NULL)
    {
      _bfd_error_handler
	/* xgettext:c-format */
	(_("%pB: no core to allocate a symbol %d bytes long"),
	 abfd, abfd->tdata.mmo_data->max_symbol_length);
      goto bad_final;
    }

  /* Read in everything.  */
  if (! mmo_scan (abfd))
    goto bad_format_free;

  if (abfd->symcount > 0)
    abfd->flags |= HAS_SYMS;

  /* You'll have to tweak this if you want to use this format for other
     arches (not recommended due to its small-size limitations).  Look at
     the ELF format for how to make it target-generic.  */
  if (! bfd_default_set_arch_mach (abfd, bfd_arch_mmix, 0))
    goto bad_format_free;

  return abfd->xvec;

 bad_format_free:
  free (abfd->tdata.mmo_data->lop_stab_symbol);
 bad_format:
  bfd_set_error (bfd_error_wrong_format);
 bad_final:
  return NULL;
}

/* Set up the mmo tdata information.  */

static bfd_boolean
mmo_mkobject (bfd *abfd)
{
  mmo_init ();

  if (abfd->tdata.mmo_data == NULL)
    {
      time_t created;

      /* All fields are zero-initialized, so we don't have to explicitly
	 initialize most.  */