依存関係を整理
[YACASL2.git] / src / comet2.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #define _GNU_SOURCE
4 #include <getopt.h>
5
6 #include "exec.h"
7 #include "cmem.h"
8 #include "cerr.h"
9
10 /**
11  * comet2コマンドのオプション
12  */
13 static struct option longopts[] = {
14     {"trace", no_argument, NULL, 't'},
15     {"tracearithmetic", no_argument, NULL, 't'},
16     {"tracelogical", no_argument, NULL, 'T'},
17     {"dump", no_argument, NULL, 'd'},
18     {"memorysize", required_argument, NULL, 'M'},
19     {"clocks", required_argument, NULL, 'C'},
20     {"help", no_argument, NULL, 'h'},
21     {0, 0, 0, 0}
22 };
23
24 /**
25  * comet2コマンドのエラー
26  */
27 static CERR cerr_comet2[] = {
28     { 208, "object file is not specified" },
29 };
30
31 /**
32  * comet2コマンドのメイン
33  */
34 int main(int argc, char *argv[])
35 {
36     int memsize = DEFAULT_MEMSIZE, clocks = DEFAULT_CLOCKS;
37     int opt, status = 0;
38     const char *usage = "Usage: %s [-tTdh] [-M <MEMORYSIZE>] [-C <CLOCKS>] FILE\n";
39
40     cerr_init();
41     addcerrlist(ARRAYSIZE(cerr_comet2), cerr_comet2);   /* エラーリスト作成 */
42
43     /* オプションの処理 */
44     while((opt = getopt_long(argc, argv, "tTdM:C:h", longopts, NULL)) != -1) {
45         switch(opt) {
46         case 't':
47             execmode.trace = true;
48             break;
49         case 'T':
50             execmode.trace = true;
51             execmode.logical = true;
52             break;
53         case 'd':
54             execmode.dump = true;
55             break;
56         case 'M':
57             memsize = atoi(optarg);
58             break;
59         case 'C':
60             clocks = atoi(optarg);
61             break;
62         case 'h':
63             fprintf(stdout, usage, argv[0]);
64             return 0;
65         case '?':
66             fprintf(stderr, usage, argv[0]);
67             exit(-1);
68         }
69     }
70     if(argv[optind] == NULL) {
71         setcerr(208, NULL);    /* object file is not specified */
72         fprintf(stderr, "comet2 error - %d: %s\n", cerr->num, cerr->msg);
73         exit(-1);
74     }
75     /* COMET II仮想マシンのリセット */
76     reset(memsize, clocks);
77     prog->start = 0;
78     if(loadassemble(argv[optind]) == true) {
79         create_code_type();    /* タイプがキーの命令ハッシュ表を作成 */
80         exec();                /* プログラム実行 */
81         free_code_type();      /* タイプがキーの命令ハッシュ表を解放 */
82     }
83     /* COMET II仮想マシンのシャットダウン */
84     shutdown();
85     if(cerr->num > 0) {
86         status = -1;
87     }
88     /* エラーの解放 */
89     freecerr();
90     return status;
91 }