エラー表をコマンドごとに持つよう内部構造を変更
[YACASL2.git] / src / casl2.c
1 #include "casl2.h"
2 #include "assemble.h"
3 #include "exec.h"
4 #define _GNU_SOURCE
5 #include <getopt.h>
6
7 static struct option longopts[] =
8 {
9     {"source", no_argument, NULL, 's'},
10     {"label", no_argument, NULL, 'l'},
11     {"labelonly", no_argument, NULL, 'L'},
12     {"assembledetail", no_argument, NULL, 'a'},
13     {"assembledetailonly", no_argument, NULL, 'A'},
14     {"assembleout", optional_argument, NULL, 'o'},
15     {"assembleoutonly", optional_argument, NULL, 'O'},
16     {"trace", no_argument, NULL, 't'},
17     {"tracearithmetic", no_argument, NULL, 't'},
18     {"tracelogical", no_argument, NULL, 'T'},
19     {"dump", no_argument, NULL, 'd'},
20     {"memorysize", required_argument, NULL, 'M'},
21     {"clocks", required_argument, NULL, 'C'},
22     {"help", no_argument, NULL, 'h'},
23     {0, 0, 0, 0},
24 };
25
26 ASMODE asmode = {false, false, false, false, false};
27 EXECMODE execmode = {false, false, false};
28
29 /* エラー番号とエラーメッセージ */
30 CERRARRAY cerr[] = {
31     { 101, "label already defined" },
32     { 102, "label table is full" },
33     { 103, "label not found" },
34     { 104, "label length is too long" },
35     { 105, "no command in the line" },
36     { 106, "operand count mismatch" },
37     { 107, "no label in START" },
38     { 108, "not command of operand \"r\"" },
39     { 109, "not command of operand \"r1,r2\"" },
40     { 110, "not command of operand \"r,adr[,x]\"" },
41     { 111, "not command of operand \"adr[,x]\"" },
42     { 112, "not command of no operand" },
43     { 113, "command not defined" },
44     { 114, "not integer" },
45     { 115, "not hex" },
46     { 116, "out of hex range" },
47     { 117, "operand is too many" },
48     { 118, "operand length is too long" },
49     { 119, "out of COMET II memory" },
50     { 120, "GR0 in operand x" },
51     { 121, "cannot get operand token" },
52     { 122, "cannot create hash table" },
53     { 123, "illegal string" },
54     { 124, "more than one character in literal" },
55     { 201, "execute - out of COMET II memory" },
56     { 202, "SVC input - out of Input memory" },
57     { 203, "SVC output - out of COMET II memory" },
58     { 204, "Program Register (PR) - out of COMET II memory" },
59     { 205, "Stack Pointer (SP) - cannot allocate stack buffer" },
60     { 206, "Address - out of COMET II memory" },
61     { 207, "Stack Pointer (SP) - out of COMET II memory" },
62     { 0, NULL },
63 };
64
65 /* 指定されたファイルにアセンブル結果を書込 */
66 void outassemble(const char *file) {
67     FILE *fp;
68     if((fp = fopen(file, "w")) == NULL) {
69         perror(file);
70         exit(-1);
71     }
72     fwrite(memory, sizeof(WORD), endptr, fp);
73     fclose(fp);
74 }
75
76 /* アセンブル結果を書き込むファイルの名前 */
77 const char *objfile_name(const char *str)
78 {
79     const char *default_name = "a.o";
80
81     if(optarg == NULL) {
82         return default_name;
83     } else {
84         return str;
85     }
86 }
87
88 /* casl2コマンド */
89 int main(int argc, char *argv[])
90 {
91     int opt, i;
92     PASS pass;
93     bool status = false;
94     WORD beginptr[argc];
95     char *objfile = NULL;
96     const char *usage =
97         "Usage: %s [-slLaAtTdh] [-oO<OUTFILE>] [-M <memorysize>] [-C <clocks>] FILE ...\n";
98
99     while((opt = getopt_long(argc, argv, "tTdslLao::O::AM:C:h", longopts, NULL)) != -1) {
100         switch(opt) {
101         case 's':
102             (&asmode)->srcmode = true;
103             break;
104         case 'l':
105             (&asmode)->labelmode = true;
106             break;
107         case 'L':
108             (&asmode)->labelmode = true;
109             (&asmode)->onlylabelmode = true;
110             break;
111         case 'a':
112             (&asmode)->asdetailmode = true;
113             break;
114         case 'A':
115             (&asmode)->asdetailmode = true;
116             (&asmode)->onlyassemblemode = true;
117             break;
118         case 'o':
119             objfile = strdup(objfile_name(optarg));
120             break;
121         case 'O':
122             (&asmode)->onlyassemblemode = true;
123             objfile = strdup(objfile_name(optarg));
124             break;
125         case 't':
126             (&execmode)->tracemode = true;
127             break;
128         case 'T':
129             (&execmode)->tracemode = true;
130             (&execmode)->logicalmode = true;
131             break;
132         case 'd':
133             (&execmode)->dumpmode = true;
134             break;
135         case 'M':
136             memsize = atoi(optarg);
137             break;
138         case 'C':
139             clocks = atoi(optarg);
140             break;
141         case 'h':
142             fprintf(stdout, usage, argv[0]);
143             return 0;
144         case '?':
145             fprintf(stderr, usage, argv[0]);
146             exit(-1);
147         }
148     }
149     /* ソースファイルが指定されていない場合は終了 */
150     if(argv[optind] == NULL) {
151         fprintf(stderr, "source file is not specified\n");
152         exit(-1);
153     }
154     /* COMET II仮想マシンのリセット */
155     reset();
156     /* アセンブル。ラベル表作成のため、2回行う */
157     for(pass = FIRST; pass <= SECOND; pass++) {
158         for(i = optind; i < argc; i++) {
159             /* データの格納開始位置 */
160             if(pass == FIRST) {
161                 beginptr[i] = ptr;
162             } else if(pass == SECOND) {
163                 ptr = beginptr[i];
164             }
165             if((&execmode)->tracemode == true || (&execmode)->dumpmode == true ||
166                (&asmode)->srcmode == true || (&asmode)->labelmode == true ||
167                (&asmode)->asdetailmode == true)
168             {
169                 fprintf(stdout, "\nAssemble %s (%d)\n", argv[i], pass);
170             }
171             if((status = assemble(argv[i], pass)) == false) {
172                 freelabel();    /* ラベル表の解放 */
173                 if(cerrno > 0) {
174                     freecerr();    /* エラーの解放 */
175                 }
176                 exit(-1);
177             }
178         }
179         if(pass == FIRST && (&asmode)->labelmode == true) {
180             fprintf(stdout, "\nLabel::::\n");
181             printlabel();
182             if((&asmode)->onlylabelmode == true) {
183                 return 0;
184             }
185         }
186     }
187     freelabel();    /* ラベル表の解放 */
188     if(status == true) {
189         if(objfile != NULL) {
190             outassemble(objfile);
191         }
192         if((&asmode)->onlyassemblemode == false) {
193             exec();    /* プログラム実行 */
194         }
195     }
196     if(cerrno > 0) {
197         freecerr();
198         exit(-1);
199     }
200     return 0;
201 }