ソースの書き方を変更
[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     addcerrlist_exec();
43
44     /* オプションの処理 */
45     while((opt = getopt_long(argc, argv, "tTdM:C:h", longopts, NULL)) != -1) {
46         switch(opt) {
47         case 't':
48             execmode.trace = true;
49             break;
50         case 'T':
51             execmode.trace = true;
52             execmode.logical = true;
53             break;
54         case 'd':
55             execmode.dump = true;
56             break;
57         case 'M':
58             memsize = atoi(optarg);
59             break;
60         case 'C':
61             clocks = atoi(optarg);
62             break;
63         case 'h':
64             fprintf(stdout, usage, argv[0]);
65             return 0;
66         case '?':
67             fprintf(stderr, usage, argv[0]);
68             exit(-1);
69         }
70     }
71     if(argv[optind] == NULL) {
72         setcerr(208, NULL);    /* object file is not specified */
73         fprintf(stderr, "comet2 error - %d: %s\n", cerr->num, cerr->msg);
74         exit(-1);
75     }
76     /* COMET II仮想マシンのリセット */
77     reset(memsize, clocks);
78     execptr->start = 0;
79     if(loadassemble(argv[optind]) == true) {
80         create_code_type();    /* タイプがキーの命令ハッシュ表を作成 */
81         exec();                /* プログラム実行 */
82         free_code_type();      /* タイプがキーの命令ハッシュ表を解放 */
83     }
84     /* COMET II仮想マシンのシャットダウン */
85     shutdown();
86     if(cerr->num > 0) {
87         status = -1;
88     }
89     /* エラーの解放 */
90     freecerr();
91     return status;
92 }