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