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