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