File: mktables.c

package info (click to toggle)
dosemu-freedos 1%3A0.0.b9r5a%2Betch.1-0etch1
  • links: PTS
  • area: contrib
  • in suites: etch
  • size: 19,744 kB
  • ctags: 23,279
  • sloc: ansic: 143,864; asm: 20,397; makefile: 3,868; perl: 1,106; yacc: 690; sh: 553; pascal: 297; xml: 150; cpp: 67
file content (1182 lines) | stat: -rw-r--r-- 27,898 bytes parent folder | download | duplicates (2)
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
/*
 *	This program reads in the various data files, performs some checks,
 *	and spits out the tables needed for assembly and disassembly.
 *
 *	The input files are:
 *		instr.set	Instruction set
 *		instr.key	Translation from one- or two-character keys to
 *				operand list types
 *		instr.ord	Ordering relations to enforce on the operands
 *
 *	The output tables are merged into the debug source file debug.asm.
 */

#ifndef	DOS
#ifdef	__MSDOS__
#define	DOS	1
#else
#define	DOS	0
#endif
#endif

#include <stdlib.h>
#if	! DOS
#include <unistd.h>
#endif
#include <stdio.h>
#include <stdarg.h>
#include <string.h>
#include <ctype.h>
#include <errno.h>

#if	DOS
#define	CRLF	"\n"
#define	bzero(a, b)	memset(a, 0, b)
#else
#define	CRLF	"\r\n"
#define	cdecl
#endif

#define	MARKLINE1	";-@@-@@-@@-Do not edit these tables!  " \
	"They have been automatically generated." CRLF
#define	MARKLINE2	";-@@-@@-@@-End of auto-generated tables.  " \
	"You may edit below this point." CRLF

#define	MAX_OL_TYPES	80
#define	MAX_N_ORDS	30
#define	LINELEN		132
#define	MAX_ASM_TAB	2048
#define	MAX_MNRECS	400
#define	MAX_SAVED_MNEMS	10
#define	MAX_SLASH_ENTRIES 20
#define	MAX_STAR_ENTRIES 15
#define	MAX_LOCKTAB_ENTRIES 50
#define	MAX_AGROUP_ENTRIES 14
#define	MSHIFT		12	/* number of bits below machine type */

typedef	char	Boolean;
#define	True	1
#define	False	0

#define	NUMBER(x)	(sizeof(x) / sizeof(*x))

char		line[LINELEN];
const char	*filename;
int		lineno;

int	n_keys		= 0;
struct keytab {
	short	key;
	short	value;
	short	width;
};

int	n_ol_types	= 0;
struct keytab	olkeydict[MAX_OL_TYPES];
char		*olnames[MAX_OL_TYPES];
int		oloffset[MAX_OL_TYPES];

int	n_ords		= 0;
struct keytab	*keyord1[MAX_N_ORDS];
struct keytab	*keyord2[MAX_N_ORDS];
Boolean		ordsmall[MAX_OL_TYPES];

/*
 *	Equates for the assembler table.
 *	These should be the same as in debug.asm.
 */

#define	ASM_END		0xff
#define	ASM_DB		0xfe
#define	ASM_DW		0xfd
#define	ASM_DD		0xfc
#define	ASM_ORG		0xfb
#define	ASM_WAIT 	0xfa
#define	ASM_D32		0xf9
#define	ASM_AAX		0xf8
#define	ASM_SEG		0xf7
#define	ASM_LOCKREP	0xf6
#define	ASM_LOCKABLE	0xf5
#define	ASM_MACH6	0xf4
#define	ASM_MACH0	0xee

int	n_asm_tab	= 0;
unsigned char	asmtab[MAX_ASM_TAB];

struct mnrec {
	struct mnrec	*next;
	char		*string;
	short		len;
	short		offset;
	short		asmoffset;
};

int		num_mnrecs;
struct mnrec	mnlist[MAX_MNRECS];
struct mnrec	*mnhead;

int	n_saved_mnems		= 0;
int	saved_mnem[MAX_SAVED_MNEMS];

int	n_slash_entries;
int	slashtab_seq[MAX_SLASH_ENTRIES];
int	slashtab_mn[MAX_SLASH_ENTRIES];

int	n_star_entries;
int	startab_seq[MAX_STAR_ENTRIES];
int	startab_mn[MAX_STAR_ENTRIES];

int	n_locktab;
int	locktab[MAX_LOCKTAB_ENTRIES];

int	n_agroups;
int	agroup_i[MAX_AGROUP_ENTRIES];
int	agroup_inf[MAX_AGROUP_ENTRIES];

volatile void
fail(const char *message, ...)
{
	va_list	args;

	va_start(args, message);
	vfprintf(stderr, message, args);
	va_end(args);
	putc('\n', stderr);
	exit(1);
}

FILE *
openread(const char *path)
{
	FILE	*f;

	f = fopen(path, "r");
	if (f == NULL) {
	    perror(path);
	    exit(1);
	}
	filename = path;
	lineno = 0;
	return f;
}

volatile void
linenofail(const char *message, ...)
{
	va_list args;

	fprintf(stderr, "Line %d of `%s':  ", lineno, filename);
	va_start(args, message);
	vfprintf(stderr, message, args);
	va_end(args);
	putc('\n', stderr);
	exit(1);
}

void *
xmalloc(unsigned int len, const char *why)
{
	void	*ptr	= malloc(len);

	if (ptr == NULL) fail("Cannot allocate %u bytes for %s", len, why);
	return ptr;
}

Boolean
getline(FILE *ff)
{
	int	n;

	for (;;) {
	    if (fgets(line, LINELEN, ff) == NULL) return False;
	    ++lineno;
	    if (line[0] == '#') continue;
	    n = strlen(line) - 1;
	    if (n < 0 || line[n] != '\n')
		linenofail("too long.");
	    if (n > 0 && line[n-1] == '\r') --n;
	    if (n == 0) continue;
	    line[n] = '\0';
	    return True;
	}
}

short
getkey(char **pp)
{
	short	key;
	char	*p	= *pp;

	if (*p == ' ' || *p == '\t' || *p == ';' || *p == '\0')
	    linenofail("key expected");
	key = *p++;
	if (*p != ' ' && *p != '\t' && *p != ';' && *p != '\0') {
	    key = (key << 8) | *p++;
	    if (*p != ' ' && *p != '\t' && *p != ';' && *p != '\0')
		linenofail("key too long");
	}
	*pp = p;
	return key;
}

/*
 *	Mark the given key pointer as small, as well as anything smaller than
 *	it (according to instr.ord).
 */

void
marksmall(struct keytab *kp)
{
	int	i;

	ordsmall[kp - olkeydict] = True;
	for (i = 0; i < n_ords; ++i)
	    if (keyord2[i] == kp)
		marksmall(keyord1[i]);
}

/*
 *	Add a byte to the assembler table (asmtab).
 *	The format of this table is described in a long comment in debug.asm,
 *	somewhere within the mini-assembler.
 */

void
add_to_asmtab(unsigned char byte)
{
	if (n_asm_tab >= MAX_ASM_TAB)
	    linenofail("Assembler table overflow.");
	asmtab[n_asm_tab++] = byte;
}


unsigned char
getmachine(char **pp)
{
	char		*p	= *pp;
	unsigned char	value;

	if (*p != ';') return 0;
	++p;
	if (*p < '0' || *p > '6')
	    linenofail("bad machine type");
	value = *p++ - '0';
	add_to_asmtab(ASM_MACH0 + value);
	*pp = p;
	return value;
}

struct keytab *
lookupkey(short key)
{
	struct keytab *kp;

	for (kp = olkeydict; kp < olkeydict + NUMBER(olkeydict); ++kp)
	    if (key == kp->key) return kp;
	linenofail("can't find key");
}

char *
skipwhite(char *p)
{
	while (*p == ' ' || *p == '\t') ++p;
	return p;
}

/*
 *	Data and setup stuff for the disassembler processing.
 */

/*	Data on coprocessor groups */

unsigned int	fpgrouptab[]	= {0xd9e8, 0xd9f0, 0xd9f8};

#define	NGROUPS		9

#define	GROUP(i)	(256 + 8 * ((i) - 1))
#define	COPR(i)		(256 + 8 * NGROUPS + 16 * (i))
#define	FPGROUP(i)	(256 + 8 * NGROUPS + 16 * 8 + 8 * (i))
#define	SPARSE_BASE	(256 + 8 * NGROUPS + 16 * 8 \
			    + 8 * NUMBER(fpgrouptab))

/* #define OPILLEGAL	0 */
#define	OPTWOBYTE	2
#define	OPGROUP		4
#define	OPCOPR		6
#define	OPFPGROUP	8
#define	OPPREFIX	10
#define	OPSIMPLE	12
#define	OPTYPES		12	/* op types start here (includes simple ops) */

#define	PRESEG		1	/* these should be the same as in debug.asm */
#define	PREREP		2
#define	PREREPZ		4
#define	PRELOCK		8
#define	PRE32D		0x10
#define	PRE32A		0x20

/*
 *	For sparsely filled parts of the opcode map, we have counterparts
 *	to the above, which are compressed in a simple way.
 */

/*	Sparse coprocessor groups */

unsigned int	sp_fpgrouptab[]	= {0xd9d0, 0xd9e0, 0xdae8, 0xdbe0, 
				   0xded8, 0xdfe0};

#define	NSGROUPS	5

#define	SGROUP(i)	(SPARSE_BASE + 256 + 8 * ((i) - 1))
#define	SFPGROUP(i)	(SPARSE_BASE + 256 + 8 * NSGROUPS + 8 * (i))
#define	NOPS		(SPARSE_BASE + 256 + 8 * NSGROUPS \
			+ 8 * NUMBER(sp_fpgrouptab))

int		optype[NOPS];
int		opinfo[NOPS];
unsigned char	opmach[NOPS];

/*
 *	Here are the tables for the main processor groups.
 */

struct {
	int	seq;	/* sequence number of the group */
	int	info;	/* which group number it is */
}
	grouptab[]	= {
		{0x80, GROUP(1)},	/* Intel group 1 */
		{0x81, GROUP(1)},
		{0x83, GROUP(2)},
		{0xd0, GROUP(3)},	/* Intel group 2 */
		{0xd1, GROUP(3)},
		{0xd2, GROUP(4)},
		{0xd3, GROUP(4)},
		{0xc0, GROUP(5)},	/* Intel group 2a */
		{0xc1, GROUP(5)},
		{0xf6, GROUP(6)},	/* Intel group 3 */
		{0xf7, GROUP(6)},
		{0xff, GROUP(7)},	/* Intel group 5 */
		{SPARSE_BASE + 0x00, GROUP(8)},		/* Intel group 6 */
		{SPARSE_BASE + 0x01, GROUP(9)}};	/* Intel group 7 */

/* #define	NGROUPS	9 (this was done above) */

struct {	/* sparse groups */
	int	seq;	/* sequence number of the group */
	int	info;	/* which group number it is */
}
	sp_grouptab[]	= {
		{0xfe, SGROUP(1)},		/* Intel group 4 */
		{SPARSE_BASE+0xba, SGROUP(2)},	/* Intel group 8 */
		{SPARSE_BASE+0xc7, SGROUP(3)},	/* Intel group 9 */
		{0x8f, SGROUP(4)},		/* Not an Intel group */
		{0xc6, SGROUP(5)},		/* Not an Intel group */
		{0xc7, SGROUP(5)}};

/* #define	NSGROUPS	5 (this was done above) */

/*
 *	Creates an entry in the disassembler lookup table
 */

void
entertable(int i, int type, int info)
{
	if (optype[i] != 0)
	    linenofail("Duplicate information for index %d", i);
	optype[i] = type;
	opinfo[i] = info;
}

/*
 *	Get a hex nybble from the input line or fail.
 */

int
getnybble(char c)
{
	if (c >= '0' && c <= '9') return c - '0';
	if (c >= 'a' && c <= 'f') return c - 'a' + 10;
	linenofail("Hex digit expected instead of `%c'", c);
}

/*
 *	Get a hex byte from the input line and update the pointer accordingly.
 */

int
getbyte(char **pp)
{
	char	*p = *pp;
	int	answer;

	answer = getnybble(*p++);
	answer = (answer << 4) | getnybble(*p++);
	*pp = p;
	return answer;
}

/*
 *	Get a `/r' descriptor from the input line and update the pointer
 *	accordingly.
 */

int
getslash(char **pp)
{
	char	*p = *pp;
	int	answer;

	if (*p != '/') linenofail("`/' expected");
	++p;
	if (*p < '0' || *p > '7') linenofail("Octal digit expected");
	answer = *p - '0';
	++p;
	*pp = p;
	return answer;
}

int
entermn(char *str, char *str_end)
{
	char	*p;

	if (num_mnrecs >= MAX_MNRECS)
	    linenofail("Too many mnemonics");

	if (*str == '+') {
	    if (n_saved_mnems >= MAX_SAVED_MNEMS)
		linenofail("Too many mnemonics to save");
	    saved_mnem[n_saved_mnems++] = num_mnrecs;
	    ++str;
	}

	p = xmalloc(str_end - str + 1, "mnemonic name");
	mnlist[num_mnrecs].string = p;
	mnlist[num_mnrecs].len = str_end - str;
	while (str < str_end) *p++ = toupper(*str++);
	*p = '\0';
	mnlist[num_mnrecs].asmoffset = n_asm_tab;
	return num_mnrecs++;
}

/*
 *	Merge sort the indicated range of mnemonic records.
 */

struct mnrec *
mn_sort(struct mnrec *start, int len)
{
	struct mnrec	*p1, *p2, *answer;
	struct mnrec	**headpp;
	int		i;

	i = len / 2;
	if (i == 0)
	    return start;

	p1 = mn_sort(start, i);
	p2 = mn_sort(start + i, len - i);
	headpp = &answer;
	for (;;)
	    if (strcmp(p1->string, p2->string) < 0) {
		*headpp = p1;
		headpp = &p1->next;
		p1 = *headpp;
		if (p1 == NULL) {
		    *headpp = p2;
		    break;
		}
	    }
	    else {
		*headpp = p2;
		headpp = &p2->next;
		p2 = *headpp;
		if (p2 == NULL) {
		    *headpp = p1;
		    break;
		}
	    }

	return answer;
}

/*
 *	This reads the main file, "instr.set".
 */

void
read_is(FILE *f1)
{
	int	i;

	entertable(0x0f, OPTWOBYTE, SPARSE_BASE);
	entertable(0x26, OPPREFIX, PRESEG | (0 << 8));	/* seg es */
	entertable(0x2e, OPPREFIX, PRESEG | (1 << 8));	/* seg cs */
	entertable(0x36, OPPREFIX, PRESEG | (2 << 8));	/* seg ss */
	entertable(0x3e, OPPREFIX, PRESEG | (3 << 8));	/* seg ds */
	entertable(0x64, OPPREFIX, PRESEG | (4 << 8));	/* seg fs */
	entertable(0x65, OPPREFIX, PRESEG | (5 << 8));	/* seg gs */
	entertable(0xf2, OPPREFIX, PREREP);		/* other prefixes */
	entertable(0xf3, OPPREFIX, PREREP | PREREPZ);
	entertable(0xf0, OPPREFIX, PRELOCK);
	entertable(0x66, OPPREFIX, PRE32D);
	entertable(0x67, OPPREFIX, PRE32A);
	opmach[0x64] = opmach[0x65] = opmach[0x66] = opmach[0x67] = 3;
	
	for (i = 0; i < NUMBER(grouptab); ++i)
	    entertable(grouptab[i].seq, OPGROUP, grouptab[i].info);
	for (i = 0; i < NUMBER(sp_grouptab); ++i)
	    entertable(sp_grouptab[i].seq, OPGROUP, sp_grouptab[i].info);
	for (i = 0; i < 8; ++i)
	    entertable(0xd8 + i, OPCOPR, COPR(i));
	for (i = 0; i < NUMBER(fpgrouptab); ++i) {
	    unsigned int j = fpgrouptab[i];
	    unsigned int k = (j >> 8) - 0xd8;

	    if (k > 8 || (j & 0xff) < 0xc0)
		fail("Bad value for fpgrouptab[%d]", i);
	    entertable(COPR(k) + 8 + (((j & 0xff) - 0xc0) >> 3),
		OPFPGROUP, FPGROUP(i));
	}
	for (i = 0; i < NUMBER(sp_fpgrouptab); ++i) {
	    unsigned int j = sp_fpgrouptab[i];
	    unsigned int k = (j >> 8) - 0xd8;

	    if (k > 8 || (j & 0xff) < 0xc0)
		fail("Bad value for sp_fpgrouptab[%d]", i);
	    entertable(COPR(k) + 8 + (((j & 0xff) - 0xc0) >> 3),
		OPFPGROUP, SFPGROUP(i));
	}
	while (getline(f1)) {	/* loop over lines in the file */
	    int mnem;
	    int mn_alt;
	    char *p, *p0, *pslash, *pstar;
	    Boolean asm_only_line;
	    unsigned char atab_addendum;

	    asm_only_line = False;
	    p0 = line;
	    if (line[0] == '_') {
		asm_only_line = True;
		++p0;
	    }
	    atab_addendum = '\0';
	    if (*p0 == '^') {
		static	const	unsigned char	uptab[]	=
					{ASM_AAX, ASM_DB, ASM_DW,
					 ASM_DD, ASM_ORG, ASM_D32};

		++p0;
		atab_addendum = uptab[*p0++ - '0'];
	    }
	    p = strchr(p0, ' ');
	    if (p == NULL) p = p0 + strlen(p0);
	    pslash = memchr(p0, '/', p - p0);
	    if (pslash != NULL) {
		mnem = entermn(p0, pslash);
		++mnlist[mnem].asmoffset;	/* this one isn't 32 bit */
		++pslash;
		mn_alt = entermn(pslash, p);
		add_to_asmtab(ASM_D32);
	    }
	    else {
		pstar = memchr(p0, '*', p - p0);
		if (pstar != NULL) {
		    mn_alt = entermn(p0, pstar);	/* note the reversal */
		    ++pstar;
		    add_to_asmtab(ASM_WAIT);
		    mnem = entermn(pstar, p);
		}
		else
		    mnem = entermn(p0, p);
	    }
	    if (atab_addendum != '\0') add_to_asmtab(atab_addendum);

	    atab_addendum = ASM_END;
	    bzero(ordsmall, n_keys * sizeof(Boolean));
	    while (*p == ' ') {		/* loop over instruction variants */
		Boolean		lockable;
		Boolean		asm_only;
		Boolean		dis_only;
		unsigned char	machine;
		unsigned long	atab_inf;
		unsigned short	atab_key;
		unsigned char	atab_xtra	= 0;

		while (*p == ' ') ++p;
		asm_only = asm_only_line;
		dis_only = False;
		if (*p == '_') {	/* if assembler only */
		    ++p;
		    asm_only = True;
		}
		else if (*p == 'D') {	/* if disassembler only */
		    ++p;
		    dis_only = True;
		}
		lockable = False;
		if (*p == 'L') {
		    ++p;
		    lockable = True;
		    add_to_asmtab(ASM_LOCKABLE);
		}
		atab_inf = i = getbyte(&p);
		if (i == 0x0f) {
		    i = getbyte(&p);
		    atab_inf = 256 + i;
		    i += SPARSE_BASE;
		}
		if (optype[i] == OPGROUP) {
		    int j = getslash(&p);
		    int k;

		    for (k = 0;; ++k) {
			if (k >= n_agroups) {
			    if (++n_agroups > MAX_AGROUP_ENTRIES)
				linenofail("Too many agroup entries");
			    agroup_i[k] = i;
			    agroup_inf[k] = atab_inf;
			    break;
			}
			if (agroup_i[k] == i)
			    break;
		    }
		    atab_inf = 256 + 256 + 64 + 8 * k + j;
		    i = opinfo[i] + j;
		}
		if (optype[i] == OPCOPR) {
		    if (*p == '/') {
			int	j	= getslash(&p);

			atab_inf = 256 + 256 + j * 8 + (i - 0xd8);
			i = opinfo[i] + j;
		    }
		    else {
			atab_xtra = getbyte(&p);
			if (atab_xtra < 0xc0)
			    linenofail("Bad second escape byte");
			i = opinfo[i] + 8 + ((atab_xtra - 0xc0) >> 3);
			if (optype[i] == OPFPGROUP)
			    i = opinfo[i] + (atab_xtra & 7);
		    }
		}
		switch (*p++) {
		    case '.':
			machine = getmachine(&p);
			if (!asm_only) {
			    entertable(i, OPSIMPLE, mnem);
			    opmach[i] = machine;
			}
			atab_key = 0;
			/* none of these are lockable */
			break;
		    case '*':	/* lock or rep... prefix */
			add_to_asmtab(ASM_LOCKREP);
			add_to_asmtab(atab_inf);	/* special case */
			atab_addendum = '\0';
			break;
		    case '&':	/* segment prefix */
			add_to_asmtab(ASM_SEG);
			add_to_asmtab(atab_inf);	/* special case */
			atab_addendum = '\0';
			break;
		    case ':': {
			    struct keytab *kp = lookupkey(getkey(&p));
			    int width = kp->width;
			    int j;

			    machine = getmachine(&p);
			    if (dis_only)
				atab_addendum = '\0';
			    else {
				if (ordsmall[kp - olkeydict])
				    linenofail("Variants out of order.");
				marksmall(kp);
			    }
			    atab_key = kp->value + 1;
			    if ((i >= 256 && i < SPARSE_BASE)
				    || i >= SPARSE_BASE + 256) {
				if (width > 2)
				    linenofail("width failure");
				width = 1;
			    }
			    if (i & (width - 1))
				linenofail("width alignment failure");
			    if (!asm_only)
				for (j = (i == 0x90); j < width; ++j) {
				    /*    ^^^^^^^^^  kludge for NOP instr. */
				    entertable(i|j, oloffset[kp->value], mnem);
				    opmach[i | j] = machine;
				    if (lockable) {
					if (n_locktab >= MAX_LOCKTAB_ENTRIES)
					    linenofail("Too many lockable "
					      "instructions");
					locktab[n_locktab] = i | j;
					++n_locktab;
				    }
				}
			}
			break;
		    default:
			linenofail("Syntax error.");
		}
		if (atab_addendum != '\0') {
		    atab_inf = atab_inf * (unsigned short) (n_ol_types + 1)
		      + atab_key;
		    add_to_asmtab(atab_inf >> 8);
		    if ((atab_inf >> 8) >= ASM_MACH0)
			fail("Assembler table is too busy");
		    add_to_asmtab(atab_inf);
		    if (atab_xtra != 0)
			add_to_asmtab(atab_xtra);
		}
		if (pslash != NULL) {
		    if (n_slash_entries >= MAX_SLASH_ENTRIES)
			linenofail("Too many slash entries");
		    slashtab_seq[n_slash_entries] = i;
		    slashtab_mn[n_slash_entries] = mn_alt;
		    ++n_slash_entries;
		}
		else if (pstar != NULL) {
		    if (n_star_entries >= MAX_STAR_ENTRIES)
			linenofail("Too many star entries");
		    startab_seq[n_star_entries] = i;
		    startab_mn[n_star_entries] = mn_alt;
		    ++n_star_entries;
		}
	    }
	    if (*p != '\0')
		linenofail("Syntax error.");
	    if (atab_addendum != '\0')
		add_to_asmtab(atab_addendum);	/* ASM_END, if applicable */
	}
}

/*
 *	Print everything onto the file.
 */

struct inforec {	/* strings to put into comment fields */
	int	seqno;
	char	*string;
}
	tblcomments[] = {
		{0, "main opcode part"},
		{GROUP(1), "Intel group 1"},
		{GROUP(3), "Intel group 2"},
		{GROUP(5), "Intel group 2a"},
		{GROUP(6), "Intel group 3"},
		{GROUP(7), "Intel group 5"},
		{GROUP(8), "Intel group 6"},
		{GROUP(9), "Intel group 7"},
		{COPR(0), "Coprocessor d8"},
		{COPR(1), "Coprocessor d9"},
		{COPR(2), "Coprocessor da"},
		{COPR(3), "Coprocessor db"},
		{COPR(4), "Coprocessor dc"},
		{COPR(5), "Coprocessor dd"},
		{COPR(6), "Coprocessor de"},
		{COPR(7), "Coprocessor df"},
		{FPGROUP(0), "Coprocessor groups"},
		{-1, NULL}};

void
put_dw(FILE *f2, const char *label, int *datap, int n)
{
	const char	*initstr;
	int		i;

	fputs(label,f2);
	while (n > 0) {
	    initstr = "\tdw\t";
	    for (i = (n <= 8 ? n : 8); i > 0; --i) {
		fputs(initstr, f2);
		initstr = ",";
		fprintf(f2, "%5d", *datap++);
	    }
	    fputs(CRLF, f2);
	    n -= 8;
	}
}

void
dumptables(FILE *f2)
{
	int		offset;
	struct mnrec	*mnp;
	int		colsleft;
	char		*auxstr;
	struct inforec	*tblptr;
	int		i;
	int		j;
	int		k;

	/*
	 * Dump out asmtab.
	 */

	auxstr = CRLF ";\tMain data table for the assembler." CRLF CRLF
	  "asmtab\tdb\t";
	colsleft = 16;
	for (i = 0; i < n_asm_tab; ++i) {
	    fprintf(f2, "%s%d", auxstr, asmtab[i]);
	    auxstr = ",";
	    if (--colsleft <= 0) {
		auxstr = CRLF "\tdb\t";
		colsleft = 16;
	    }
	}

	/*
	 * Dump out agroup_inf.
	 */

	auxstr = CRLF CRLF ";\tData on groups (for the assembler)." CRLF CRLF
	  "agroups\tdw\t";
	for (i = 0; i < n_agroups; ++i) {
	    fprintf(f2, "%s%d", auxstr, agroup_inf[i]);
	    auxstr = ",";
	}

	/*
	 * Sort the mnemonics alphabetically, compute their offsets,
	 * and print out the table.
	 */

	if (num_mnrecs == 0)
	    fail("No assembler mnemonics!");
	mnhead = mn_sort(mnlist, num_mnrecs);
	offset = 0;
	auxstr = CRLF CRLF ";\tThis is the list of assembler mnemonics." CRLF
	  CRLF "mnlist\tdb\t";
	colsleft = 80 - 15;
	for (mnp = mnhead; mnp != NULL; mnp = mnp->next) {
	    mnp->offset = offset + 2;
	    offset += mnp->len + 3;
	    if (colsleft < mnp->len + 13) {
		auxstr = CRLF "\tdb\t";
		colsleft = 80 - 15;
	    }
	    colsleft -= mnp->len + 13;
	    fprintf(f2, "%s%d,%d,\"%s\",0", auxstr, mnp->asmoffset / 255 + 1,
		mnp->asmoffset % 255 + 1, mnp->string);
	    auxstr = ",";
	}
	fputs(CRLF "end_mnlist:" CRLF, f2);
	if (offset >= (1 << MSHIFT)) {
	    fprintf(stderr, "%d bytes of mnemonics.  That's too many.\n",
		offset);
	    exit(1);
	}

	/*
	 * Print out mnemonics we want to refer to.
	 */

	fputs(CRLF ";\tThese are equates to refer to the above mnemonics."
		CRLF CRLF, f2);
	for (i = 0; i < n_saved_mnems; ++i) {
	    mnp = mnlist + saved_mnem[i];
	    fprintf(f2, "MNEM_%s\tEQU\t%d" CRLF, mnp->string, mnp->offset);
	}

	/*
	 * Print out ASMMOD and the opindex array.
	 */

	fprintf(f2, CRLF ";\tNumber of entries in the following array." CRLF
	  CRLF "ASMMOD\tEQU\t%d" CRLF, n_ol_types + 1);

	auxstr = CRLF ";\tThis is an array of indices into the oplists array "
	  "(below)." CRLF
	  ";\tIt is used by the assembler to save space." CRLF CRLF
	  "opindex\tdb\t0,";
	colsleft = 16;
	for (i = 0; i < n_ol_types; ++i) {
	    fprintf(f2, "%s%d", auxstr, oloffset[i] - OPTYPES);
	    auxstr = ",";
	    if (--colsleft <= 0) {
		auxstr = CRLF "\tdb\t";
		colsleft = 16;
	    }
	}

	/*
	 * Print out oplists[]
	 */

	fputs(CRLF CRLF ";\tThese are the lists of operands "
	    "for the various instruction types." CRLF CRLF
	    "oplists\tdb\t0\t;simple instruction" CRLF, f2);
	for (i = 0; i < n_ol_types; ++i)
	    fprintf(f2, "\tdb\t%s, 0" CRLF, olnames[i]);
	fprintf(f2, CRLF "OPTYPES_BASE\tEQU\t%d" CRLF, OPTYPES);
	fprintf(f2, CRLF "OPLIST_Z\tEQU\t%d" CRLF,
	    oloffset[lookupkey('z')->value]);
	fprintf(f2, "OPLIST_ES\tEQU\t%d" CRLF,
	    oloffset[lookupkey(('E' << 8) | 'S')->value]);

	/*
	 * Print out optype[]
	 */

	auxstr = CRLF ";\tHere is the compressed table of the opcode types."
	    CRLF CRLF "optypes\tdb\t";
	tblptr = tblcomments;

	for (i = 0; i < SPARSE_BASE; i += 8) {
	    for (j = 0; j < 8; ++j) {
		fputs(auxstr, f2);
		fprintf(f2, "%3d", optype[i + j]);
		auxstr = ",";
	    }
	    fprintf(f2, "\t; %02x - %02x", i, i + 7);
	    if (i == tblptr->seqno)
		fprintf(f2, " (%s)", (tblptr++)->string);
	    auxstr = CRLF "\tdb\t";
	}
	auxstr = CRLF ";\tThe rest of these are squeezed." CRLF "\tdb\t 0,";
	colsleft = 7;
	for (i = SPARSE_BASE; i < NOPS; ++i)
	    if ((j = optype[i]) != 0) {
		if (--colsleft < 0) {
		    colsleft = 7;
		    auxstr = CRLF "\tdb\t";
		}
		fputs(auxstr, f2);
		fprintf(f2, "%3d", j);
		auxstr = ",";
	    }
	fputs(CRLF, f2);

	/*
	 * Print out opinfo[]
	 */

	fputs(CRLF ";\tAnd here is the compressed table of additional "
	    "information." CRLF CRLF "opinfo", f2);

	for (i = 0; i < SPARSE_BASE; i += 8) {
	    auxstr = "\tdw\t";
	    for (j = 0; j < 8; ++j) {
		fputs(auxstr, f2);
		k = opinfo[i + j];
		if (optype[i + j] >= OPTYPES)
		    k = mnlist[k].offset;
		fprintf(f2, "0%04xh", k | (opmach[i + j] << MSHIFT));
		auxstr = ",";
	    }
	    fprintf(f2, "\t; %02x" CRLF, i);
	}
	auxstr = ";\tThe rest of these are squeezed." CRLF "\tdw\t     0,";
	colsleft = 7;
	for (i = SPARSE_BASE; i < NOPS; ++i)
	    if ((j = optype[i]) != 0) {
		if (--colsleft < 0) {
		    colsleft = 7;
		    auxstr = CRLF "\tdw\t";
		}
		fputs(auxstr, f2);
		k = opinfo[i];
		if (j >= OPTYPES) k = mnlist[k].offset;
		fprintf(f2, "0%04xh", k | (opmach[i] << MSHIFT));
		auxstr = ",";
	    }
	fputs(CRLF, f2);

	/*
	 * Print out sqztab
	 */

	fputs(CRLF ";\tThis table converts unsqueezed numbers to squeezed."
	    CRLF CRLF "sqztab", f2);

	k = 0;
	for (i = SPARSE_BASE; i < NOPS; i += 8) {
	    auxstr = "\tdb\t";
	    for (j = 0; j < 8; ++j) {
		fprintf(f2, "%s%3d", auxstr, optype[i + j] == 0 ? 0 : ++k);
		auxstr = ",";
	    }
	    fputs(CRLF, f2);
	}

	/*
	 * Print out the cleanup tables.
	 */

	fputs(CRLF ";\tThis is the table of mnemonics that change in the "
		"presence of a WAIT" CRLF ";\tinstruction." CRLF CRLF, f2);
	put_dw(f2, "wtab1", startab_seq, n_star_entries);
	for (i = 0; i < n_star_entries; ++i)
	    startab_mn[i] = mnlist[startab_mn[i]].offset;
	put_dw(f2, "wtab2", startab_mn, n_star_entries);
	fprintf(f2, "N_WTAB\tequ\t%d" CRLF, n_star_entries);

	fputs(CRLF ";\tThis is the table for operands which have a different "
		"mnemonic for" CRLF ";\ttheir 32 bit versions." CRLF CRLF, f2);
	put_dw(f2, "ltab1", slashtab_seq, n_slash_entries);
	for (i = 0; i < n_slash_entries; ++i)
	    slashtab_mn[i] = mnlist[slashtab_mn[i]].offset;
	put_dw(f2, "ltab2", slashtab_mn, n_slash_entries);
	fprintf(f2, "N_LTAB\tequ\t%d" CRLF, n_slash_entries);

	fputs(CRLF ";\tThis is the table of lockable instructions" CRLF CRLF,
	    f2);
	put_dw(f2, "locktab", locktab, n_locktab);
	fprintf(f2, "N_LOCK\tequ\t%d" CRLF, n_locktab);

	/*
	 * Print out miscellaneous equates.
	 */

	fprintf(f2, CRLF ";\tEquates used in the assembly-language code." CRLF
	    CRLF "SPARSE_BASE\tequ\t%d" CRLF
	    CRLF "SFPGROUP3\tequ\t%d" CRLF CRLF, SPARSE_BASE, SFPGROUP(3));
}

int cdecl
main() {
	FILE	*f1;
	FILE	*f2;
	int	offset;

	/*
	 * Read in the key dictionary.
	 */

	f1 = openread("instr.key");
	offset = OPTYPES + 1;
	while (getline(f1)) {
	    char *p = line;
	    int i;

	    if (n_keys >= MAX_OL_TYPES)
		fail("Too many keys.");
	    olkeydict[n_keys].key = getkey(&p);
	    p = skipwhite(p);
	    for (i = 0;; ++i) {
		if (i >= n_ol_types) {
		    char *q = xmalloc(strlen(p) + 1, "operand type name");

		    strcpy(q, p);
		    if (n_ol_types >= MAX_OL_TYPES)
			fail("Too many operand list types.");
		    olnames[n_ol_types] = q;
		    oloffset[n_ol_types] = offset;
		    for (;;) {
			++offset;
			q = strchr(q, ',');
			if (q == NULL) break;
			++q;
		    }
		    ++offset;
		    ++n_ol_types;
		}
		if (strcmp(p, olnames[i]) == 0)
		    break;
	    }
	    olkeydict[n_keys].value = i;
	    olkeydict[n_keys].width = 1;
	    if (strstr(p, "OP_ALL") != NULL)
		olkeydict[n_keys].width = 2;
	    else if (strstr(p, "OP_R_ADD") != NULL)
		olkeydict[n_keys].width = 8;
	    ++n_keys;
	}
	fclose(f1);
	if (offset >= 256) {
	    fprintf(stderr, "%d bytes of operand lists.  That's too many.\n",
		offset);
	    exit(1);
	}

	/*
	 * Read in the ordering relations.
	 */

	f1 = openread("instr.ord");
	while (getline(f1)) {
	    char *p = line;

	    if (n_ords >= MAX_N_ORDS)
		fail ("Too many ordering restrictions.");
	    keyord1[n_ords] = lookupkey(getkey(&p));
	    p = skipwhite(p);
	    keyord2[n_ords] = lookupkey(getkey(&p));
	    if (*p != '\0')
		fail("Syntax error in ordering file.");
	    ++n_ords;
	}
	fclose(f1);

	/*
	 * Do the main processing.
	 */

	f1 = openread("instr.set");
	read_is(f1);
	fclose(f1);

	/*
	 * Write the file.
	 */

	f1 = openread("debug.asm");
	f2 = fopen("debug.tmp", "w");
	if (f2 == NULL) {
	    perror("debug.tmp");
	    exit(1);
	}

	do {
	    if (fgets(line, LINELEN, f1) == NULL) {
		fputs("Couldn't find beginning marker line\n", stderr);
		exit(1);
	    }
	    fputs(line, f2);
	}
	while (strcmp(line, MARKLINE1) != 0);

	dumptables(f2);

	do {
	    if (fgets(line, LINELEN, f1) == NULL) {
		fputs("Couldn't find ending marker line\n", stderr);
		exit(1);
	    }
	}
	while (strcmp(line, MARKLINE2) != 0);

	do
	    fputs(line, f2);
	while (fgets(line, LINELEN, f1) != NULL);

	fclose(f1);    
	fclose(f2);

	/*
	 * Move the file to its original position.
	 */

#if	DOS
	if (unlink("debug.old") == -1 && errno != ENOFILE) {
	    perror("delete debug.old");
	    return 1;
	}
#endif
	if (rename("debug.asm", "debug.old") == -1) {
	    perror("rename debug.asm -> debug.old");
	    return 1;
	}
	if (rename("debug.tmp", "debug.asm") == -1) {
	    perror("rename debug.tmp -> debug.asm");
	    return 1;
	}

	puts("Done.");

	return 0;
}