Pristine Ack-5.5
[Ack-5.5.git] / util / ego / share / makecldef.c
1 /* $Id: makecldef.c,v 1.5 1994/06/24 10:30:29 ceriel Exp $ */
2 /*
3  * (c) copyright 1987 by the Vrije Universiteit, Amsterdam, The Netherlands.
4  * See the copyright notice in the ACK home directory, in the file "Copyright".
5  */
6 #include <stdio.h>
7
8 /*  MAKECLASSDEF
9  *
10  * This program is used by several phases of the optimizer
11  * to make the file classdefs.h. It reads two files:
12  *  - the em_mnem,h file, containing the definitions of the
13  *    EM mnemonics
14  *  - the class-file, containing tuples:
15  *    (mnemonic, src_class, res_class)
16  *    where src_class and res_class are integers telling how
17  *    to compute the number of bytes popped and pushed
18  *    by the instruction.
19  * The output (standard output) is a C array.
20  */
21
22
23 #define TRUE  1
24 #define FALSE 0
25
26 convert(mnemfile,classfile)
27         FILE *mnemfile, *classfile;
28 {
29         char mnem1[10], mnem2[10],def[10];
30         int src,res,newcl,opc;
31
32         newcl = TRUE;
33         printf("struct class classtab[] = {\n");
34         printf("\tNOCLASS,\tNOCLASS,\n");
35         /* EM mnemonics start at 1, arrays in C at 0 */
36         for (;;) {
37                 fscanf(mnemfile,"%s%s%d",def,mnem1,&opc);
38                 /* read a line like "#define op_aar 1" */
39                 if (feof(mnemfile)) break;
40                 if (strcmp(def,"#define") != 0) {
41                         error("bad mnemonic file, #define expected");
42                 }
43                 if (newcl) {
44                         fscanf(classfile,"%s%d%d",mnem2,&src,&res);
45                         /* read a line like "op_loc 8 1" */
46                 }
47                 if (feof(classfile) || strcmp(mnem1,mnem2) != 0) {
48                         /* there is no line for this mnemonic, so
49                          * it has no class.
50                          */
51                         printf("\tNOCLASS,\tNOCLASS,\n");
52                         newcl = FALSE;
53                 } else {
54                         printf("\tCLASS%d,\t\tCLASS%d,\n",src,res);
55                         /* print a line like "CLASS8, CLASS1," */
56                         newcl = TRUE;
57                 }
58         }
59         printf("};\n");
60 }
61
62
63
64 error(s)
65         char *s;
66 {
67         fprintf(stderr,"%s\n",s);
68         exit(-1);
69 }
70
71
72 main(argc,argv)
73         int argc;
74         char *argv[];
75 {
76         FILE *f1,*f2;
77
78         if (argc != 3) {
79                 error("usage: makeclassdef mnemfile classfile");
80         }
81         if ((f1 = fopen(argv[1],"r")) == NULL) {
82                 error("cannot open mnemonic file");
83         }
84         if ((f2 = fopen(argv[2],"r")) == NULL) {
85                 error("cannot open class file");
86         }
87         convert(f1,f2);
88         exit(0);
89 }