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