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