7 static struct option longopts[] = {
8 {"source", no_argument, NULL, 's'},
9 {"label", no_argument, NULL, 'l'},
10 {"labelonly", no_argument, NULL, 'L'},
11 {"assembledetail", no_argument, NULL, 'a'},
12 {"assembledetailonly", no_argument, NULL, 'A'},
13 {"assembleout", optional_argument, NULL, 'o'},
14 {"assembleoutonly", optional_argument, NULL, 'O'},
15 {"trace", no_argument, NULL, 't'},
16 {"tracearithmetic", no_argument, NULL, 't'},
17 {"tracelogical", no_argument, NULL, 'T'},
18 {"dump", no_argument, NULL, 'd'},
19 {"memorysize", required_argument, NULL, 'M'},
20 {"clocks", required_argument, NULL, 'C'},
21 {"help", no_argument, NULL, 'h'},
25 ASMODE asmode = {false, false, false, false, false};
26 EXECMODE execmode = {false, false, false};
30 { 101, "label already defined" },
31 { 102, "label table is full" },
32 { 103, "label not found" },
33 { 104, "label length is too long" },
34 { 105, "no command in the line" },
35 { 106, "operand count mismatch" },
36 { 107, "no label in START" },
37 { 108, "not command of operand \"r\"" },
38 { 109, "not command of operand \"r1,r2\"" },
39 { 110, "not command of operand \"r,adr[,x]\"" },
40 { 111, "not command of operand \"adr[,x]\"" },
41 { 112, "not command of no operand" },
42 { 113, "command not defined" },
43 { 114, "not integer" },
45 { 116, "out of hex range" },
46 { 117, "operand is too many" },
47 { 118, "operand length is too long" },
48 { 119, "out of COMET II memory" },
49 { 120, "GR0 in operand x" },
50 { 121, "cannot get operand token" },
51 { 122, "cannot create hash table" },
52 { 123, "illegal string" },
53 { 124, "more than one character in literal" },
54 { 201, "execute - out of COMET II memory" },
55 { 202, "SVC input - out of Input memory" },
56 { 203, "SVC output - out of COMET II memory" },
57 { 204, "Program Register (PR) - out of COMET II memory" },
58 { 205, "Stack Pointer (SP) - cannot allocate stack buffer" },
59 { 206, "Address - out of COMET II memory" },
60 { 207, "Stack Pointer (SP) - out of COMET II memory" },
64 /* 指定されたファイルにアセンブル結果を書込 */
65 void outassemble(const char *file) {
67 if((fp = fopen(file, "w")) == NULL) {
71 fwrite(memory, sizeof(WORD), endptr, fp);
75 /* アセンブル結果を書き込むファイルの名前 */
76 const char *objfile_name(const char *str)
78 const char *default_name = "a.o";
88 int main(int argc, char *argv[])
96 "Usage: %s [-slLaAtTdh] [-oO<OUTFILE>] [-M <memorysize>] [-C <clocks>] FILE ...\n";
98 while((opt = getopt_long(argc, argv, "tTdslLao::O::AM:C:h", longopts, NULL)) != -1) {
101 (&asmode)->srcmode = true;
104 (&asmode)->labelmode = true;
107 (&asmode)->labelmode = true;
108 (&asmode)->onlylabelmode = true;
111 (&asmode)->asdetailmode = true;
114 (&asmode)->asdetailmode = true;
115 (&asmode)->onlyassemblemode = true;
118 objfile = strdup(objfile_name(optarg));
121 (&asmode)->onlyassemblemode = true;
122 objfile = strdup(objfile_name(optarg));
125 (&execmode)->tracemode = true;
128 (&execmode)->tracemode = true;
129 (&execmode)->logicalmode = true;
132 (&execmode)->dumpmode = true;
135 memsize = atoi(optarg);
138 clocks = atoi(optarg);
141 fprintf(stdout, usage, argv[0]);
144 fprintf(stderr, usage, argv[0]);
148 /* ソースファイルが指定されていない場合は終了 */
149 if(argv[optind] == NULL) {
150 fprintf(stderr, "source file is not specified\n");
153 /* COMET II仮想マシンのリセット */
155 /* アセンブル。ラベル表作成のため、2回行う */
156 for(pass = FIRST; pass <= SECOND; pass++) {
157 for(i = optind; i < argc; i++) {
161 } else if(pass == SECOND) {
164 if((&execmode)->tracemode == true || (&execmode)->dumpmode == true ||
165 (&asmode)->srcmode == true || (&asmode)->labelmode == true ||
166 (&asmode)->asdetailmode == true)
168 fprintf(stdout, "\nAssemble %s (%d)\n", argv[i], pass);
170 if((status = assemble(argv[i], pass)) == false) {
171 freelabel(); /* ラベル表の解放 */
173 freecerr(); /* エラーの解放 */
178 if(pass == FIRST && (&asmode)->labelmode == true) {
179 fprintf(stdout, "\nLabel::::\n");
181 if((&asmode)->onlylabelmode == true) {
186 freelabel(); /* ラベル表の解放 */
188 if(objfile != NULL) {
189 outassemble(objfile);
191 if((&asmode)->onlyassemblemode == false) {
192 exec(); /* プログラム実行 */