アセンブルと実行の流れを整理
[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  * アセンブルを実行
59  */
60 void doassemble(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     /* アセンブル。ラベル表作成のため、2回行う */
69     for(pass = FIRST; pass <= SECOND; pass++) {
70         for(i = 0; i < filec; i++) {
71             /* データの格納開始位置 */
72             if(pass == FIRST) {
73                 bp[i] = asptr->ptr;
74             } else if(pass == SECOND) {
75                 asptr->ptr = bp[i];
76             }
77             if(execmode.trace == true || execmode.dump == true || asmode.src == true ||
78                asmode.label == true || asmode.asdetail == true)
79             {
80                 fprintf(stdout, "\nAssemble %s (%d)\n", filev[i], pass);
81             }
82             assemble(filev[i], pass);
83             if(cerr->num > 0) {
84                 goto assemblefin;
85             }
86         }
87         if(pass == FIRST && asmode.label == true) {
88             fprintf(stdout, "\nLabel::::\n");
89             printlabel();
90             if(asmode.onlylabel == true) {
91                 break;
92             }
93         }
94     }
95 assemblefin:
96     freelabel();                                  /* ラベルハッシュ表を解放 */
97     free_cmdtype_code();                          /* 命令の名前とタイプがキーのハッシュ表を解放 */
98     FREE(asptr);                                  /* アセンブル時のプロパティを解放 */
99 }
100
101 /**
102  *  casl2コマンドのメイン
103  */
104 int main(int argc, char *argv[])
105 {
106     int memsize = DEFAULT_MEMSIZE, clocks = DEFAULT_CLOCKS, opt, i, status;
107     char *af[argc];
108     char *objfile = NULL;
109     const char *usage =
110         "Usage: %s [-slLaAtTdh] [-oO[<OBJECTFILE>]] [-M <MEMORYSIZE>] [-C <CLOCKS>] FILE1[ FILE2  ...]\n";
111
112     cerr_init();
113     addcerrlist_casl2();
114     addcerrlist_assemble();
115     addcerrlist_exec();
116     /* オプションの処理 */
117     while((opt = getopt_long(argc, argv, "tTdslLao::O::AM:C:h", longopts, NULL)) != -1) {
118         switch(opt) {
119         case 's':
120             asmode.src = true;
121             break;
122         case 'l':
123             asmode.label = true;
124             break;
125         case 'L':
126             asmode.label = true;
127             asmode.onlylabel = true;
128             break;
129         case 'a':
130             asmode.asdetail = true;
131             break;
132         case 'A':
133             asmode.asdetail = true;
134             asmode.onlyassemble = true;
135             break;
136         case 'o':
137             objfile = strdup_chk(objfile_name(optarg), "objfile");
138             break;
139         case 'O':
140             asmode.onlyassemble = true;
141             objfile = strdup_chk(objfile_name(optarg), "objfile");
142             break;
143         case 't':
144             execmode.trace = true;
145             break;
146         case 'T':
147             execmode.trace = true;
148             execmode.logical = true;
149             break;
150         case 'd':
151             execmode.dump = true;
152             break;
153         case 'M':
154             memsize = atoi(optarg);
155             break;
156         case 'C':
157             clocks = atoi(optarg);
158             break;
159         case 'h':
160             fprintf(stdout, usage, argv[0]);
161             return 0;
162         case '?':
163             fprintf(stderr, usage, argv[0]);
164             exit(-1);
165         }
166     }
167     /* ソースファイルが指定されていない場合は終了 */
168     if(argv[optind] == NULL) {
169         setcerr(126, NULL);    /* no source file */
170         fprintf(stderr, "CASL2 error - %d: %s\n", cerr->num, cerr->msg);
171         exit(-1);
172     }
173     reset(memsize, clocks);                        /* 仮想マシンCOMET IIのリセット */
174     for(i = 0; i < argc - optind; i++) {           /* 引数からファイル名配列を取得 */
175         af[i] = argv[optind + i];
176     }
177     doassemble(i, af);                             /* アセンブル */
178     if(asmode.onlylabel == true || cerr->num > 0) {
179         goto casl2fin;
180     }
181     /* オブジェクトファイル名が指定されている場合は、アセンブル結果をオブジェクトファイルに出力 */
182     if(objfile != NULL) {
183         outassemble(objfile);
184         FREE(objfile);
185     }
186     /* onlyassembleモード以外の場合、仮想マシンCOMET IIを実行 */
187     if(asmode.onlyassemble == false) {
188         exec();                                    /* 仮想マシンCOMET IIの実行 */
189     }
190 casl2fin:
191     shutdown();                                    /* 仮想マシンCOMET IIのシャットダウン */
192     status = (cerr->num == 0) ? 0 : -1;
193     freecerr();                                    /* エラーの解放 */
194     return status;
195 }