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