79d3e0224ddb22149816daa918d11c07495f7eff
[YACASL2.git] / src / casl2.c
1 #include <stdio.h>
2 #include <stdlib.h>
3
4 #define _GNU_SOURCE
5 #include <getopt.h>
6
7 #include "cmem.h"
8 #include "cerr.h"
9 #include "assemble.h"
10 #include "exec.h"
11
12 /**
13  * casl2コマンドのオプション
14  */
15 static struct option longopts[] = {
16     { "source", no_argument, NULL, 's' },
17     { "label", no_argument, NULL, 'l' },
18     { "labelonly", no_argument, NULL, 'L' },
19     { "assembledetail", no_argument, NULL, 'a' },
20     { "assembledetailonly", no_argument, NULL, 'A' },
21     { "assembleout", optional_argument, NULL, 'o' },
22     { "assembleoutonly", optional_argument, NULL, 'O' },
23     { "trace", no_argument, NULL, 't' },
24     { "tracearithmetic", no_argument, NULL, 't' },
25     { "tracelogical", no_argument, NULL, 'T' },
26     { "dump", no_argument, NULL, 'd' },
27     { "memorysize", required_argument, NULL, 'M' },
28     { "clocks", required_argument, NULL, 'C' },
29     { "help", no_argument, NULL, 'h' },
30     { 0, 0, 0, 0 },
31 };
32
33 /**
34  * casl2のエラー定義
35  */
36 CERR cerr_casl2[] = {
37     { 126, "no source file" },
38 };
39
40 /**
41  * CASL IIのエラーをエラーリストに追加
42  */
43 void addcerrlist_casl2()
44 {
45     addcerrlist(ARRAYSIZE(cerr_casl2), cerr_casl2);
46 }
47
48 /**
49  * アセンブル結果を書き込むファイルの名前
50  */
51 const char *objfile_name(const char *str)
52 {
53     const char *default_name = "a.o";
54     return (str == NULL) ? default_name : str;
55 }
56
57 /**
58  * 指定された1つまたは複数のファイルを2回アセンブル
59  */
60 void assemble(int filec, char *filev[])
61 {
62     int i;
63     PASS pass;
64     WORD bp[filec];
65
66     create_cmdtype_code();                         /* 命令の名前とタイプがキーのハッシュ表を作成 */
67     asptr = malloc_chk(sizeof(ASPTR), "asptr");    /* アセンブル時のプロパティ用の領域確保 */
68     asptr->prog = malloc_chk(LABELSIZE + 1, "asptr.prog");
69     asptr->ptr = 0;
70     /* アセンブル。ラベル表作成のため、2回行う */
71     for(pass = FIRST; pass <= SECOND; pass++) {
72         for(i = 0; i < filec; i++) {
73             /* データの格納開始位置 */
74             if(pass == FIRST) {
75                 bp[i] = asptr->ptr;
76             } else if(pass == SECOND) {
77                 asptr->ptr = bp[i];
78             }
79             if(execmode.trace == true || execmode.dump == true ||
80                asmode.src == true || asmode.label == true || asmode.asdetail == true)
81             {
82                 fprintf(stdout, "\nAssemble %s (%d)\n", filev[i], pass);
83             }
84             /* ファイルをアセンブル */
85             if(assemblefile(filev[i], pass) == false) {
86                 goto asfin;
87             }
88         }
89         if(pass == FIRST && asmode.label == true) {
90             fprintf(stdout, "\nLabel::::\n");
91             printlabel();
92             if(asmode.onlylabel == true) {
93                 break;
94             }
95         }
96     }
97 asfin:
98     freelabel();                                  /* ラベルハッシュ表を解放 */
99     free_cmdtype_code();                          /* 命令の名前とタイプがキーのハッシュ表を解放 */
100     FREE(asptr->prog);                            /* アセンブル時のプロパティを解放 */
101     FREE(asptr);
102 }
103
104 /**
105  *  casl2コマンドのメイン
106  */
107 int main(int argc, char *argv[])
108 {
109     int memsize = DEFAULT_MEMSIZE, clocks = DEFAULT_CLOCKS, opt, i, stat;
110     char *af[argc];
111     char *objfile = NULL;
112     const char *usage =
113         "Usage: %s [-slLaAtTdh] [-oO[<OBJECTFILE>]] [-M <MEMORYSIZE>] [-C <CLOCKS>] FILE1[ FILE2  ...]\n";
114
115     cerr_init();
116     addcerrlist_casl2();
117     addcerrlist_assemble();
118     addcerrlist_exec();
119     /* オプションの処理 */
120     while((opt = getopt_long(argc, argv, "tTdslLao::O::AM:C:h", longopts, NULL)) != -1) {
121         switch(opt) {
122         case 's':
123             asmode.src = true;
124             break;
125         case 'l':
126             asmode.label = true;
127             break;
128         case 'L':
129             asmode.label = true;
130             asmode.onlylabel = true;
131             break;
132         case 'a':
133             asmode.asdetail = true;
134             break;
135         case 'A':
136             asmode.asdetail = true;
137             asmode.onlyassemble = true;
138             break;
139         case 'o':
140             objfile = strdup_chk(objfile_name(optarg), "objfile");
141             break;
142         case 'O':
143             asmode.onlyassemble = true;
144             objfile = strdup_chk(objfile_name(optarg), "objfile");
145             break;
146         case 't':
147             execmode.trace = true;
148             break;
149         case 'T':
150             execmode.trace = true;
151             execmode.logical = true;
152             break;
153         case 'd':
154             execmode.dump = true;
155             break;
156         case 'M':
157             memsize = atoi(optarg);
158             break;
159         case 'C':
160             clocks = atoi(optarg);
161             break;
162         case 'h':
163             fprintf(stdout, usage, argv[0]);
164             return 0;
165         case '?':
166             fprintf(stderr, usage, argv[0]);
167             exit(1);
168         }
169     }
170     /* ソースファイルが指定されていない場合は終了 */
171     if(argv[optind] == NULL) {
172         setcerr(126, "");    /* no source file */
173         fprintf(stderr, "CASL2 error - %d: %s\n", cerr->num, cerr->msg);
174         exit(1);
175     }
176     reset(memsize, clocks);                        /* 仮想マシンCOMET IIのリセット */
177     for(i = 0; i < argc - optind; i++) {           /* 引数からファイル名配列を取得 */
178         af[i] = argv[optind + i];
179     }
180     assemble(i, af);                               /* アセンブル */
181     if(asmode.onlylabel == true || cerr->num > 0) {
182         goto casl2fin;
183     }
184     /* オブジェクトファイル名が指定されている場合は、アセンブル結果をオブジェクトファイルに出力 */
185     if(objfile != NULL) {
186         outassemble(objfile);
187         FREE(objfile);
188     }
189     /* onlyassembleモード以外の場合、仮想マシンCOMET IIを実行 */
190     if(asmode.onlyassemble == false) {
191         exec();                                    /* 仮想マシンCOMET IIの実行 */
192     }
193 casl2fin:
194     shutdown();                                    /* 仮想マシンCOMET IIのシャットダウン */
195     stat = (cerr->num == 0) ? 0 : 1;
196     freecerr();                                    /* エラーの解放 */
197     return stat;
198 }