1b438e5b406d0bcef02bc837fcadfac5579ee71e
[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     {"source", no_argument, NULL, 's'},
9     {"label", no_argument, NULL, 'l'},
10     {"labelonly", no_argument, NULL, 'L'},
11     {"assembledetail", no_argument, NULL, 'a'},
12     {"assembledetailonly", no_argument, NULL, 'A'},
13     {"assembleout", optional_argument, NULL, 'o'},
14     {"assembleoutonly", optional_argument, NULL, 'O'},
15     {"trace", no_argument, NULL, 't'},
16     {"tracearithmetic", no_argument, NULL, 't'},
17     {"tracelogical", no_argument, NULL, 'T'},
18     {"dump", no_argument, NULL, 'd'},
19     {"memorysize", required_argument, NULL, 'M'},
20     {"clocks", required_argument, NULL, 'C'},
21     {"help", no_argument, NULL, 'h'},
22     {0, 0, 0, 0},
23 };
24
25 ASMODE asmode = {false, false, false, false, false};
26 EXECMODE execmode = {false, false, false};
27
28 /* エラー番号とエラーメッセージ */
29 CERRARRAY cerr[] = {
30     { 101, "label already defined" },
31     { 102, "label table is full" },
32     { 103, "label not found" },
33     { 104, "label length is too long" },
34     { 105, "no command in the line" },
35     { 106, "operand count mismatch" },
36     { 107, "no label in START" },
37     { 108, "not command of operand \"r\"" },
38     { 109, "not command of operand \"r1,r2\"" },
39     { 110, "not command of operand \"r,adr[,x]\"" },
40     { 111, "not command of operand \"adr[,x]\"" },
41     { 112, "not command of no operand" },
42     { 113, "command not defined" },
43     { 114, "not integer" },
44     { 115, "not hex" },
45     { 116, "out of hex range" },
46     { 117, "operand is too many" },
47     { 118, "operand length is too long" },
48     { 119, "out of COMET II memory" },
49     { 120, "GR0 in operand x" },
50     { 121, "cannot get operand token" },
51     { 122, "cannot create hash table" },
52     { 123, "illegal string" },
53     { 124, "more than one character in literal" },
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(optarg == 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)->srcmode = true;
102             break;
103         case 'l':
104             (&asmode)->labelmode = true;
105             break;
106         case 'L':
107             (&asmode)->labelmode = true;
108             (&asmode)->onlylabelmode = true;
109             break;
110         case 'a':
111             (&asmode)->asdetailmode = true;
112             break;
113         case 'A':
114             (&asmode)->asdetailmode = true;
115             (&asmode)->onlyassemblemode = true;
116             break;
117         case 'o':
118             objfile = strdup(objfile_name(optarg));
119             break;
120         case 'O':
121             (&asmode)->onlyassemblemode = true;
122             objfile = strdup(objfile_name(optarg));
123             break;
124         case 't':
125             (&execmode)->tracemode = true;
126             break;
127         case 'T':
128             (&execmode)->tracemode = true;
129             (&execmode)->logicalmode = true;
130             break;
131         case 'd':
132             (&execmode)->dumpmode = 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         fprintf(stderr, "source file is not specified\n");
151         exit(-1);
152     }
153     /* COMET II仮想マシンのリセット */
154     reset();
155     /* アセンブル。ラベル表作成のため、2回行う */
156     for(pass = FIRST; pass <= SECOND; pass++) {
157         for(i = optind; i < argc; i++) {
158             /* データの格納開始位置 */
159             if(pass == FIRST) {
160                 beginptr[i] = ptr;
161             } else if(pass == SECOND) {
162                 ptr = beginptr[i];
163             }
164             if((&execmode)->tracemode == true || (&execmode)->dumpmode == true ||
165                (&asmode)->srcmode == true || (&asmode)->labelmode == true ||
166                (&asmode)->asdetailmode == true)
167             {
168                 fprintf(stdout, "\nAssemble %s (%d)\n", argv[i], pass);
169             }
170             if((status = assemble(argv[i], pass)) == false) {
171                 freelabel();    /* ラベル表の解放 */
172                 if(cerrno > 0) {
173                     freecerr();    /* エラーの解放 */
174                 }
175                 exit(-1);
176             }
177         }
178         if(pass == FIRST && (&asmode)->labelmode == true) {
179             fprintf(stdout, "\nLabel::::\n");
180             printlabel();
181             if((&asmode)->onlylabelmode == true) {
182                 return 0;
183             }
184         }
185     }
186     freelabel();    /* ラベル表の解放 */
187     if(status == true) {
188         if(objfile != NULL) {
189             outassemble(objfile);
190         }
191         if((&asmode)->onlyassemblemode == false) {
192             exec();    /* プログラム実行 */
193         }
194     }
195     if(cerrno > 0) {
196         freecerr();
197         exit(-1);
198     }
199     return 0;
200 }