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" },
43 static const char *objfile_name(const char *str)
45 const char *default_name = "a.o";
46 return (str == NULL) ? default_name : str;
52 int main(int argc, char *argv[])
54 int memsize = DEFAULT_MEMSIZE, clocks = DEFAULT_CLOCKS;
55 int status = 0, opt, i;
61 "Usage: %s [-slLaAtTdh] [-oO[<OBJECTFILE>]] [-M <MEMORYSIZE>] [-C <CLOCKS>] FILE1[ FILE2 ...]\n";
64 addcerrlist(ARRAYSIZE(cerr_casl2), cerr_casl2);
65 addcerrlist_assemble();
68 while((opt = getopt_long(argc, argv, "tTdslLao::O::AM:C:h", longopts, NULL)) != -1) {
78 asmode.onlylabel = true;
81 asmode.asdetail = true;
84 asmode.asdetail = true;
85 asmode.onlyassemble = true;
88 objfile = strdup_chk(objfile_name(optarg), "objfile");
91 asmode.onlyassemble = true;
92 objfile = strdup_chk(objfile_name(optarg), "objfile");
95 execmode.trace = true;
98 execmode.trace = true;
99 execmode.logical = true;
102 execmode.dump = true;
105 memsize = atoi(optarg);
108 clocks = atoi(optarg);
111 fprintf(stdout, usage, argv[0]);
114 fprintf(stderr, usage, argv[0]);
118 /* ソースファイルが指定されていない場合は終了 */
119 if(argv[optind] == NULL) {
120 setcerr(126, NULL); /* no source file */
121 fprintf(stderr, "CASL2 error - %d: %s\n", cerr->num, cerr->msg);
124 /* COMET II仮想マシンのリセット */
125 reset(memsize, clocks);
126 /* アセンブル。ラベル表作成のため、2回行う */
127 for(pass = FIRST; pass <= SECOND; pass++) {
129 create_cmdtype_code(); /* 命令の名前とタイプがキーのハッシュ表を作成 */
130 asptr = malloc_chk(sizeof(asptr), "asptr"); /* アセンブル時のプロパティ用の領域確保 */
132 for(i = optind; i < argc; i++) {
135 beginptr[i] = asptr->ptr;
136 } else if(pass == SECOND) {
137 asptr->ptr = beginptr[i];
140 if(execmode.trace == true || execmode.dump == true || asmode.src == true ||
141 asmode.label == true || asmode.asdetail == true)
143 fprintf(stdout, "\nAssemble %s (%d)\n", argv[i], pass);
145 if((res = assemble(argv[i], pass)) == false) {
146 freecerr(); /* エラーの解放 */
150 if(pass == FIRST && asmode.label == true) {
151 fprintf(stdout, "\nLabel::::\n");
153 if(asmode.onlylabel == true) {
158 freelabel(); /* ラベルハッシュ表を解放 */
159 free_chk(asptr->prog, "asptr.prog"); /* プログラム名を解放 */
160 free_chk(asptr, "asptr"); /* アセンブル時のプロパティを解放 */
161 free_cmdtype_code(); /* 命令の名前とタイプがキーのハッシュ表を解放 */
165 if(objfile != NULL) {
166 outassemble(objfile);
167 free_chk(objfile, "objfile");
169 if(asmode.onlyassemble == false) {
170 create_code_type(); /* 命令のコードとタイプがキーのハッシュ表を作成 */
171 res = exec(); /* プログラム実行 */
172 free_code_type(); /* 命令のコードとタイプがキーのハッシュ表を解放 */
175 /* COMET II仮想マシンのシャットダウン */
180 freecerr(); /* エラーの解放 */