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' },
36 static CERR cerr_casl2[] = {
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(sizeof(cerr_casl2), cerr_casl2);
66 while((opt = getopt_long(argc, argv, "tTdslLao::O::AM:C:h", longopts, NULL)) != -1) {
76 asmode.onlylabel = true;
79 asmode.asdetail = true;
82 asmode.asdetail = true;
83 asmode.onlyassemble = true;
86 objfile = strdup_chk(objfile_name(optarg), "objfile");
89 asmode.onlyassemble = true;
90 objfile = strdup_chk(objfile_name(optarg), "objfile");
93 execmode.trace = true;
96 execmode.trace = true;
97 execmode.logical = true;
100 execmode.dump = true;
103 memsize = atoi(optarg);
106 clocks = atoi(optarg);
109 fprintf(stdout, usage, argv[0]);
112 fprintf(stderr, usage, argv[0]);
116 /* ソースファイルが指定されていない場合は終了 */
117 if(argv[optind] == NULL) {
118 setcerr(126, NULL); /* no source file */
119 fprintf(stderr, "CASL2 error - %d: %s\n", cerr->num, cerr->msg);
122 /* COMET II仮想マシンのリセット */
123 reset(memsize, clocks);
124 /* アセンブル。ラベル表作成のため、2回行う */
125 for(pass = FIRST; pass <= SECOND; pass++) {
127 create_cmdtype_code(); /* 命令の名前とタイプがキーのハッシュ表を作成 */
128 asprop = malloc_chk(sizeof(ASPROP), "asprop"); /* アセンブル時のプロパティ用の領域確保 */
130 for(i = optind; i < argc; i++) {
133 beginptr[i] = asprop->ptr;
134 } else if(pass == SECOND) {
135 asprop->ptr = beginptr[i];
138 if(execmode.trace == true || execmode.dump == true || asmode.src == true ||
139 asmode.label == true || asmode.asdetail == true)
141 fprintf(stdout, "\nAssemble %s (%d)\n", argv[i], pass);
143 if((res = assemble(argv[i], pass)) == false) {
147 if(pass == FIRST && asmode.label == true) {
148 fprintf(stdout, "\nLabel::::\n");
150 if(asmode.onlylabel == true) {
155 freelabel(); /* ラベルハッシュ表を解放 */
156 free_chk(asprop->prog, "asprop.prog"); /* プログラム名を解放 */
157 free_chk(asprop, "asprop"); /* アセンブル時のプロパティを解放 */
158 free_cmdtype_code(); /* 命令の名前とタイプがキーのハッシュ表を解放 */
162 if(objfile != NULL) {
163 outassemble(objfile);
165 if(asmode.onlyassemble == false) {
166 create_code_type(); /* 命令のコードとタイプがキーのハッシュ表を作成 */
167 res = exec(); /* プログラム実行 */
168 free_code_type(); /* 命令のコードとタイプがキーのハッシュ表を解放 */
171 /* COMET II仮想マシンのシャットダウン */
176 free_chk(objfile, "objfile");