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' },
37 { 126, "no source file" },
41 * CASL IIのエラーをエラーリストに追加
43 void addcerrlist_casl2()
45 addcerrlist(ARRAYSIZE(cerr_casl2), cerr_casl2);
51 const char *objfile_name(const char *str)
53 const char *default_name = "a.o";
54 return (str == NULL) ? default_name : str;
58 * 指定された1つまたは複数のファイルを2回アセンブル
60 void assemble(int filec, char *filev[])
66 create_cmdtype_code(); /* 命令の名前とタイプがキーのハッシュ表を作成 */
67 asptr = malloc_chk(sizeof(ASPTR), "asptr"); /* アセンブル時のプロパティ用の領域確保 */
68 asptr->prog = malloc_chk(LABELSIZE + 1, "asptr.prog");
70 /* アセンブル。ラベル表作成のため、2回行う */
71 for(pass = FIRST; pass <= SECOND; pass++) {
72 for(i = 0; i < filec; i++) {
76 } else if(pass == SECOND) {
79 if(execmode.trace == true || execmode.dump == true ||
80 asmode.src == true || asmode.label == true || asmode.asdetail == true)
82 fprintf(stdout, "\nAssemble %s (%d)\n", filev[i], pass);
85 if(assemblefile(filev[i], pass) == false) {
89 if(pass == FIRST && asmode.label == true) {
90 fprintf(stdout, "\nLabel::::\n");
92 if(asmode.onlylabel == true) {
98 freelabel(); /* ラベルハッシュ表を解放 */
99 free_cmdtype_code(); /* 命令の名前とタイプがキーのハッシュ表を解放 */
100 FREE(asptr->prog); /* アセンブル時のプロパティを解放 */
107 int main(int argc, char *argv[])
109 int memsize = DEFAULT_MEMSIZE, clocks = DEFAULT_CLOCKS, opt, i, stat;
111 char *objfile = NULL;
113 "Usage: %s [-slLaAtTdh] [-oO[<OBJECTFILE>]] [-M <MEMORYSIZE>] [-C <CLOCKS>] FILE1[ FILE2 ...]\n";
117 addcerrlist_assemble();
120 while((opt = getopt_long(argc, argv, "tTdslLao::O::AM:C:h", longopts, NULL)) != -1) {
130 asmode.onlylabel = true;
133 asmode.asdetail = true;
136 asmode.asdetail = true;
137 asmode.onlyassemble = true;
140 objfile = strdup_chk(objfile_name(optarg), "objfile");
143 asmode.onlyassemble = true;
144 objfile = strdup_chk(objfile_name(optarg), "objfile");
147 execmode.trace = true;
150 execmode.trace = true;
151 execmode.logical = true;
154 execmode.dump = true;
157 memsize = atoi(optarg);
160 clocks = atoi(optarg);
163 fprintf(stdout, usage, argv[0]);
166 fprintf(stderr, usage, argv[0]);
170 /* ソースファイルが指定されていない場合は終了 */
171 if(argv[optind] == NULL) {
172 setcerr(126, ""); /* no source file */
173 fprintf(stderr, "CASL2 error - %d: %s\n", cerr->num, cerr->msg);
176 reset(memsize, clocks); /* 仮想マシンCOMET IIのリセット */
177 for(i = 0; i < argc - optind; i++) { /* 引数からファイル名配列を取得 */
178 af[i] = argv[optind + i];
180 assemble(i, af); /* アセンブル */
181 if(asmode.onlylabel == true || cerr->num > 0) {
184 /* オブジェクトファイル名が指定されている場合は、アセンブル結果をオブジェクトファイルに出力 */
185 if(objfile != NULL) {
186 outassemble(objfile);
189 /* onlyassembleモード以外の場合、仮想マシンCOMET IIを実行 */
190 if(asmode.onlyassemble == false) {
191 exec(); /* 仮想マシンCOMET IIの実行 */
194 shutdown(); /* 仮想マシンCOMET IIのシャットダウン */
195 stat = (cerr->num == 0) ? 0 : 1;
196 freecerr(); /* エラーの解放 */