7 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'},
26 ASMODE asmode = {false, false, false, false, false};
27 EXECMODE execmode = {false, false, false};
31 { 101, "label already defined" },
32 { 102, "label table is full" },
33 { 103, "label not found" },
34 { 104, "label length is too long" },
35 { 105, "no command in the line" },
36 { 106, "operand count mismatch" },
37 { 107, "no label in START" },
38 { 108, "not command of operand \"r\"" },
39 { 109, "not command of operand \"r1,r2\"" },
40 { 110, "not command of operand \"r,adr[,x]\"" },
41 { 111, "not command of operand \"adr[,x]\"" },
42 { 112, "not command of no operand" },
43 { 113, "command not defined" },
44 { 114, "not integer" },
46 { 116, "out of hex range" },
47 { 117, "operand is too many" },
48 { 118, "operand length is too long" },
49 { 119, "out of COMET II memory" },
50 { 120, "GR0 in operand x" },
51 { 121, "cannot get operand token" },
52 { 122, "cannot create hash table" },
53 { 123, "illegal string" },
54 { 124, "more than one character in literal" },
55 { 201, "execute - out of COMET II memory" },
56 { 202, "SVC input - out of Input memory" },
57 { 203, "SVC output - out of COMET II memory" },
58 { 204, "Program Register (PR) - out of COMET II memory" },
59 { 205, "Stack Pointer (SP) - cannot allocate stack buffer" },
60 { 206, "Address - out of COMET II memory" },
61 { 207, "Stack Pointer (SP) - out of COMET II memory" },
65 /* 指定されたファイルにアセンブル結果を書込 */
66 void outassemble(const char *file) {
68 if((fp = fopen(file, "w")) == NULL) {
72 fwrite(memory, sizeof(WORD), endptr, fp);
76 /* アセンブル結果を書き込むファイルの名前 */
77 const char *objfile_name(const char *str)
79 const char *default_name = "a.o";
89 int main(int argc, char *argv[])
97 "Usage: %s [-slLaAtTdh] [-oO<OUTFILE>] [-M <memorysize>] [-C <clocks>] FILE ...\n";
99 while((opt = getopt_long(argc, argv, "tTdslLao::O::AM:C:h", longopts, NULL)) != -1) {
102 (&asmode)->srcmode = true;
105 (&asmode)->labelmode = true;
108 (&asmode)->labelmode = true;
109 (&asmode)->onlylabelmode = true;
112 (&asmode)->asdetailmode = true;
115 (&asmode)->asdetailmode = true;
116 (&asmode)->onlyassemblemode = true;
119 objfile = strdup(objfile_name(optarg));
122 (&asmode)->onlyassemblemode = true;
123 objfile = strdup(objfile_name(optarg));
126 (&execmode)->tracemode = true;
129 (&execmode)->tracemode = true;
130 (&execmode)->logicalmode = true;
133 (&execmode)->dumpmode = true;
136 memsize = atoi(optarg);
139 clocks = atoi(optarg);
142 fprintf(stdout, usage, argv[0]);
145 fprintf(stderr, usage, argv[0]);
149 /* ソースファイルが指定されていない場合は終了 */
150 if(argv[optind] == NULL) {
151 fprintf(stderr, "source file is not specified\n");
154 /* COMET II仮想マシンのリセット */
156 /* アセンブル。ラベル表作成のため、2回行う */
157 for(pass = FIRST; pass <= SECOND; pass++) {
158 for(i = optind; i < argc; i++) {
162 } else if(pass == SECOND) {
165 if((&execmode)->tracemode == true || (&execmode)->dumpmode == true ||
166 (&asmode)->srcmode == true || (&asmode)->labelmode == true ||
167 (&asmode)->asdetailmode == true)
169 fprintf(stdout, "\nAssemble %s (%d)\n", argv[i], pass);
171 if((status = assemble(argv[i], pass)) == false) {
172 freelabel(); /* ラベル表の解放 */
174 freecerr(); /* エラーの解放 */
179 if(pass == FIRST && (&asmode)->labelmode == true) {
180 fprintf(stdout, "\nLabel::::\n");
182 if((&asmode)->onlylabelmode == true) {
187 freelabel(); /* ラベル表の解放 */
189 if(objfile != NULL) {
190 outassemble(objfile);
192 if((&asmode)->onlyassemblemode == false) {
193 exec(); /* プログラム実行 */