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, "no source file" },
30 bool addcerrlist_casl2()
32 return addcerrlist(sizeof(cerr_casl2), cerr_casl2);
35 /* 指定されたファイルにアセンブル結果を書込 */
36 void outassemble(const char *file) {
39 if((fp = fopen(file, "w")) == NULL) {
43 fwrite(sys->memory, sizeof(WORD), prog->end, fp);
47 /* アセンブル結果を書き込むファイルの名前 */
48 const char *objfile_name(const char *str)
50 const char *default_name = "a.o";
51 return (str == NULL) ? default_name : str;
55 int main(int argc, char *argv[])
57 int memsize = DEFAULT_MEMSIZE, clocks = DEFAULT_CLOCKS;
58 int status = 0, opt, i;
64 "Usage: %s [-slLaAtTdh] [-oO[<OBJECTFILE>]] [-M <MEMORYSIZE>] [-C <CLOCKS>] FILE1[ FILE2 ...]\n";
69 while((opt = getopt_long(argc, argv, "tTdslLao::O::AM:C:h", longopts, NULL)) != -1) {
79 asmode.onlylabel = true;
82 asmode.asdetail = true;
85 asmode.asdetail = true;
86 asmode.onlyassemble = true;
89 objfile = strdup_chk(objfile_name(optarg), "objfile");
92 asmode.onlyassemble = true;
93 objfile = strdup_chk(objfile_name(optarg), "objfile");
96 execmode.trace = true;
99 execmode.trace = true;
100 execmode.logical = true;
103 execmode.dump = true;
106 memsize = atoi(optarg);
109 clocks = atoi(optarg);
112 fprintf(stdout, usage, argv[0]);
115 fprintf(stderr, usage, argv[0]);
119 /* ソースファイルが指定されていない場合は終了 */
120 if(argv[optind] == NULL) {
121 setcerr(126, NULL); /* no source file */
122 fprintf(stderr, "CASL2 error - %d: %s\n", cerr->num, cerr->msg);
125 /* COMET II仮想マシンのリセット */
126 reset(memsize, clocks);
127 /* アセンブル。ラベル表作成のため、2回行う */
128 for(pass = FIRST; pass <= SECOND; pass++) {
130 create_cmdtype_code(); /* 命令と命令タイプがキーのハッシュ表を作成 */
131 asprop = malloc_chk(sizeof(ASPROP), "asprop"); /* アセンブル時のプロパティ用の領域確保 */
133 for(i = optind; i < argc; i++) {
136 beginptr[i] = asprop->ptr;
137 } else if(pass == SECOND) {
138 asprop->ptr = beginptr[i];
141 if(execmode.trace == true || execmode.dump == true || asmode.src == true ||
142 asmode.label == true || asmode.asdetail == true)
144 fprintf(stdout, "\nAssemble %s (%d)\n", argv[i], pass);
146 if((res = assemble(argv[i], pass)) == false) {
150 if(pass == FIRST && asmode.label == true) {
151 fprintf(stdout, "\nLabel::::\n");
153 if(asmode.onlylabel == true) {
158 freelabel(); /* ラベルハッシュ表を解放 */
159 free_chk(asprop->prog, "asprop.prog"); /* プログラム名を解放 */
160 free_chk(asprop, "asprop"); /* アセンブル時のプロパティを解放 */
161 free_cmdtype_code(); /* 命令と命令タイプがキーのハッシュ表を解放 */
165 if(objfile != NULL) {
166 outassemble(objfile);
168 if(asmode.onlyassemble == false) {
169 create_code_type(); /* 命令と命令タイプがキーのハッシュ表を作成 */
170 res = exec(); /* プログラム実行 */
171 free_code_type(); /* 命令と命令タイプがキーのハッシュ表を解放 */
174 /* COMET II仮想マシンのシャットダウン */
179 free_chk(objfile, "objfile");