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 { 101, "label already defined" },
29 { 102, "label table is full" },
30 { 103, "label not found" },
31 { 104, "label length is too long" },
32 { 105, "no command in the line" },
33 { 106, "operand count mismatch" },
34 { 107, "no label in START" },
35 { 108, "not command of operand \"r\"" },
36 { 109, "not command of operand \"r1,r2\"" },
37 { 110, "not command of operand \"r,adr[,x]\"" },
38 { 111, "not command of operand \"adr[,x]\"" },
39 { 112, "not command of no operand" },
40 { 113, "command not defined" },
41 { 114, "not integer" },
43 { 116, "out of hex range" },
44 { 117, "operand is too many" },
45 { 118, "operand length is too long" },
46 { 119, "out of COMET II memory" },
47 { 120, "GR0 in operand x" },
48 { 121, "cannot get operand token" },
49 { 122, "cannot create hash table" },
50 { 123, "unclosed quote" },
51 { 124, "more than one character in literal" },
52 { 125, "not GR in operand x" },
53 { 201, "execute - out of COMET II memory" },
54 { 202, "SVC input - out of Input memory" },
55 { 203, "SVC output - out of COMET II memory" },
56 { 204, "Program Register (PR) - out of COMET II memory" },
57 { 205, "Stack Pointer (SP) - cannot allocate stack buffer" },
58 { 206, "Address - out of COMET II memory" },
59 { 207, "Stack Pointer (SP) - out of COMET II memory" },
63 /* 指定されたファイルにアセンブル結果を書込 */
64 void outassemble(const char *file) {
66 if((fp = fopen(file, "w")) == NULL) {
70 fwrite(memory, sizeof(WORD), endptr, fp);
74 /* アセンブル結果を書き込むファイルの名前 */
75 const char *objfile_name(const char *str)
77 const char *default_name = "a.o";
87 int main(int argc, char *argv[])
95 "Usage: %s [-slLaAtTdh] [-oO<OUTFILE>] [-M <memorysize>] [-C <clocks>] FILE ...\n";
97 while((opt = getopt_long(argc, argv, "tTdslLao::O::AM:C:h", longopts, NULL)) != -1) {
100 (&asmode)->src = true;
103 (&asmode)->label = true;
106 (&asmode)->label = true;
107 (&asmode)->onlylabel = true;
110 (&asmode)->asdetail = true;
113 (&asmode)->asdetail = true;
114 (&asmode)->onlyassemble = true;
117 objfile = strdup(objfile_name(optarg));
120 (&asmode)->onlyassemble = true;
121 objfile = strdup(objfile_name(optarg));
124 (&execmode)->trace = true;
127 (&execmode)->trace = true;
128 (&execmode)->logical = true;
131 (&execmode)->dump = true;
134 memsize = atoi(optarg);
137 clocks = atoi(optarg);
140 fprintf(stdout, usage, argv[0]);
143 fprintf(stderr, usage, argv[0]);
147 /* ソースファイルが指定されていない場合は終了 */
148 if(argv[optind] == NULL) {
149 fprintf(stderr, "source file is not specified\n");
152 /* COMET II仮想マシンのリセット */
154 /* アセンブル。ラベル表作成のため、2回行う */
155 for(pass = FIRST; pass <= SECOND; pass++) {
156 for(i = optind; i < argc; i++) {
160 } else if(pass == SECOND) {
163 if((&execmode)->trace == true || (&execmode)->dump == true ||
164 (&asmode)->src == true || (&asmode)->label == true ||
165 (&asmode)->asdetail == true)
167 fprintf(stdout, "\nAssemble %s (%d)\n", argv[i], pass);
169 if((status = assemble(argv[i], pass)) == false) {
170 freelabel(); /* ラベル表の解放 */
172 freecerr(); /* エラーの解放 */
177 if(pass == FIRST && (&asmode)->label == true) {
178 fprintf(stdout, "\nLabel::::\n");
180 if((&asmode)->onlylabel == true) {
185 freelabel(); /* ラベル表の解放 */
187 if(objfile != NULL) {
188 outassemble(objfile);
190 if((&asmode)->onlyassemble == false) {
191 exec(); /* プログラム実行 */