引数の変更
[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 /* 指定されたファイルにCOMET II仮想メモリ(アセンブル結果)を書込 */
8 void outassemble(char *file) {
9     FILE *fp;
10     if((fp = fopen(file, "w")) == NULL) {
11         perror(file);
12         return;
13     }
14     fwrite(memory, sizeof(WORD), endptr, fp);
15     fclose(fp);
16 }
17
18 static struct option longopts[] = {
19     {"source", no_argument, NULL, 's'},
20     {"label", no_argument, NULL, 'l'},
21     {"labelonly", no_argument, NULL, 'L'},
22     {"assembledetail", no_argument, NULL, 'a'},
23     {"assembledetailonly", no_argument, NULL, 'A'},
24     {"assembleout", optional_argument, NULL, 'o'},
25     {"assembleoutonly", optional_argument, NULL, 'O'},
26     {"trace", no_argument, NULL, 't'},
27     {"tracearithmetic", no_argument, NULL, 't'},
28     {"tracelogical", no_argument, NULL, 'T'},
29     {"dump", no_argument, NULL, 'd'},
30     {"memsize", required_argument, NULL, 'M'},
31     {"clocks", required_argument, NULL, 'C'},
32     {"help", no_argument, NULL, 'h'},
33     {0, 0, 0, 0},
34 };
35
36 int main(int argc, char *argv[])
37 {
38     int opt, i;
39     PASS pass;
40     bool status = false;
41     WORD beginptr[argc];
42     char *objfile = NULL;
43     const char *default_objfile = "a.o";
44     const char *usage = "Usage: %s [-slLaAtTdh] [-oO<OUTFILE>] [-M <memsize>] [-C <clocks>] FILE ...\n";
45
46     while((opt = getopt_long(argc, argv, "tTdslLao::O::AM:C:h", longopts, NULL)) != -1) {
47         switch(opt) {
48         case 's':
49             srcmode = true;
50             break;
51         case 'l':
52             labelmode = true;
53             break;
54         case 'L':
55             onlylabelmode = true;
56             break;
57         case 'a':
58             asdetailmode = true;
59             break;
60         case 'A':
61             onlyassemblemode = true;
62             asdetailmode = true;
63             break;
64         case 'o':
65             if(optarg == NULL) {
66                 objfile = strdup(default_objfile);
67             } else {
68                 objfile = strdup(optarg);
69             }
70             break;
71         case 'O':
72             onlyassemblemode = true;
73             if(optarg == NULL) {
74                 objfile = strdup(default_objfile);
75             } else {
76                 objfile = strdup(optarg);
77             }
78             break;
79         case 't':
80             tracemode = true;
81             break;
82         case 'T':
83             tracemode = true;
84             logicalmode = true;
85             break;
86         case 'd':
87             dumpmode = true;
88             break;
89         case 'M':
90             memsize = atoi(optarg);
91             break;
92         case 'C':
93             clocks = atoi(optarg);
94             break;
95         case 'h':
96             fprintf(stdout, usage, argv[0]);
97             exit(-1);
98         case '?':
99             fprintf(stderr, usage, argv[0]);
100             exit(-1);
101         }
102     }
103     if(argv[optind] == NULL) {
104         fprintf(stderr, "source file is not specified\n");
105         exit(-1);
106     }
107     /* ソースファイルが指定されていない場合は終了 */
108     reset();
109     /* アセンブル。ラベル表作成のため、2回行う */
110     for(pass = FIRST; pass <= SECOND; pass++) {
111         for(i = optind; i < argc; i++) {
112             /* データの格納開始位置 */
113             if(pass == FIRST) {
114                 beginptr[i] = ptr;
115             } else if(pass == SECOND) {
116                 ptr = beginptr[i];
117             }
118             if(tracemode == true || dumpmode == true || srcmode == true ||
119                labelmode == true || asdetailmode == true) {
120                 fprintf(stdout, "\nAssemble %s (%d)\n", argv[i], pass);
121             }
122             if((status = assemble(argv[i], pass)) == false) {
123                 freelabel();    /* ラベル表の解放 */
124                 if(cerrno > 0) {
125                     freecerr();    /* エラーの解放 */
126                 }
127                 exit(-1);
128             }
129         }
130         if(pass == FIRST && (labelmode == true || onlylabelmode == true)) {
131             fprintf(stdout, "\nLabel::::\n");
132             printlabel();
133             if(onlylabelmode == true) {
134                 return 0;
135             }
136         }
137     }
138     freelabel();    /* ラベル表の解放 */
139     if(status == true) {
140         if(objfile != NULL) {
141             outassemble(objfile);
142         }
143         if(onlyassemblemode == false) {
144             exec();    /* プログラム実行 */
145         }
146     }
147     if(cerrno > 0) {
148         freecerr();
149         exit(-1);
150     }
151     return 0;
152 }