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