8 static struct option longopts[] = {
9 {"source", no_argument, NULL, 's'},
10 {"label", no_argument, NULL, 'l'},
11 {"labelonly", no_argument, NULL, 'L'},
12 {"assembledetail", no_argument, NULL, 'a'},
13 {"assembledetailonly", no_argument, NULL, 'A'},
14 {"assembleout", optional_argument, NULL, 'o'},
15 {"assembleoutonly", optional_argument, NULL, 'O'},
16 {"trace", no_argument, NULL, 't'},
17 {"tracearithmetic", no_argument, NULL, 't'},
18 {"tracelogical", no_argument, NULL, 'T'},
19 {"dump", no_argument, NULL, 'd'},
20 {"memorysize", required_argument, NULL, 'M'},
21 {"clocks", required_argument, NULL, 'C'},
22 {"help", no_argument, NULL, 'h'},
28 { 126, "source file is not specified" },
30 bool addcerrlist_casl2()
32 return addcerrlist(sizeof(cerr_casl2), cerr_casl2);
35 /* 指定されたファイルにアセンブル結果を書込 */
36 void outassemble(const char *file) {
38 if((fp = fopen(file, "w")) == NULL) {
42 fwrite(memory, sizeof(WORD), progprop->end, fp);
46 /* アセンブル結果を書き込むファイルの名前 */
47 const char *objfile_name(const char *str)
49 const char *default_name = "a.o";
59 int main(int argc, char *argv[])
61 int opt, i, retval = 0;
67 "Usage: %s [-slLaAtTdh] [-oO<OBJECTFILE>] [-M <MEMORYSIZE>] [-C <CLOCKS>] FILE ...\n";
70 cerr = malloc_chk(sizeof(CERR), "cerr");
73 while((opt = getopt_long(argc, argv, "tTdslLao::O::AM:C:h", longopts, NULL)) != -1) {
83 asmode.onlylabel = true;
86 asmode.asdetail = true;
89 asmode.asdetail = true;
90 asmode.onlyassemble = true;
93 objfile = strdup(objfile_name(optarg));
96 asmode.onlyassemble = true;
97 objfile = strdup(objfile_name(optarg));
100 execmode.trace = true;
103 execmode.trace = true;
104 execmode.logical = true;
107 execmode.dump = true;
110 memsize = atoi(optarg);
113 clocks = atoi(optarg);
116 fprintf(stdout, usage, argv[0]);
119 fprintf(stderr, usage, argv[0]);
123 /* ソースファイルが指定されていない場合は終了 */
124 if(argv[optind] == NULL) {
125 setcerr(126, NULL); /* source file is not specified */
126 fprintf(stderr, "CASL2 error - %d: %s\n", cerr->num, cerr->msg);
129 /* COMET II仮想マシンのリセット */
131 /* アセンブル。ラベル表作成のため、2回行う */
132 for(pass = FIRST; pass <= SECOND; pass++) {
134 create_cmdtype_code(); /* 命令と命令タイプがキーのハッシュ表を作成 */
135 asprop = malloc_chk(sizeof(ASPROP), "asprop");
137 for(i = optind; i < argc; i++) {
140 beginptr[i] = asprop->ptr;
141 } else if(pass == SECOND) {
142 asprop->ptr = beginptr[i];
144 if(execmode.trace == true || execmode.dump == true || asmode.src == true ||
145 asmode.label == true || asmode.asdetail == true)
147 fprintf(stdout, "\nAssemble %s (%d)\n", argv[i], pass);
149 if((status = assemble(argv[i], pass)) == false) {
153 if(pass == FIRST && asmode.label == true) {
154 fprintf(stdout, "\nLabel::::\n");
156 if(asmode.onlylabel == true) {
161 free_cmdtype_code(); /* 命令と命令タイプがキーのハッシュ表を解放 */
162 freelabel(); /* ラベルハッシュ表を解放 */
166 if(objfile != NULL) {
167 outassemble(objfile);
169 if(asmode.onlyassemble == false) {
170 create_code_type(); /* 命令と命令タイプがキーのハッシュ表を作成 */
171 status = exec(); /* プログラム実行 */
172 free_code_type(); /* 命令と命令タイプがキーのハッシュ表を解放 */
175 /* COMET II仮想マシンのシャットダウン */