4 /* アセンブルモード: src, label, onlylabel, asdetail, onlyassemble */
5 ASMODE asmode = {false, false, false, false, false};
10 /* リテラル(=付きの値)を格納するポインタ */
17 CERRARRAY cerr_assemble[] = {
18 { 101, "label already defined" },
19 { 102, "label table is full" },
20 { 103, "label not found" },
21 { 104, "label length is too long" },
22 { 105, "no command in the line" },
23 { 106, "operand mismatch in assemble command" },
24 { 107, "no label in START" },
25 { 108, "not command of operand \"r\"" },
26 { 109, "not command of operand \"r1,r2\"" },
27 { 110, "not command of operand \"r,adr[,x]\"" },
28 { 111, "not command of operand \"adr[,x]\"" },
29 { 112, "not command of no operand" },
30 { 113, "operand too many in COMET II command" },
31 { 117, "operand too many in DC" },
32 { 118, "operand length too long" },
33 { 119, "out of COMET II memory" },
34 { 120, "GR0 in operand x" },
35 { 121, "cannot get operand token" },
36 { 122, "cannot create hash table" },
37 { 123, "unclosed quote" },
38 { 124, "more than one character in literal" },
39 { 125, "not GR in operand x" },
42 bool addcerrlist_assemble()
44 return addcerrlist(ARRAYSIZE(cerr_assemble), cerr_assemble);
47 /* 汎用レジスタを表す文字列「GR[0-7]」から、レジスタ番号[0-7]をWORD値で返す */
48 /* 文字列が汎用レジスタを表さない場合は、0xFFFFを返す */
49 /* is_xがtrueの場合は指標レジスタ。GR0は、COMET IIの仕様により、エラー発生 */
50 WORD getgr(const char *str, bool is_x)
54 /* 「GR[0-7]」以外の文字列では、0xFFFFを返して終了 */
55 if(!(strlen(str) == 3 && strncmp(str, "GR", 2) == 0 &&
56 (*(str+2) >= '0' && *(str+2) <= '7')))
60 r = (WORD)(*(str+2) - '0');
61 /* GR0は指標レジスタとして用いることができない */
62 if(is_x == true && r == 0x0) {
63 setcerr(120, NULL); /* GR0 in operand x */
70 /* アドレスには、リテラル/10進定数/16進定数/アドレス定数が含まれる */
71 WORD getadr(const char *prog, const char *str, PASS pass)
75 adr = getliteral(str, pass);
76 } else if((*str == '#') || isdigit(*str) || *str == '-') {
79 if(pass == SECOND && (adr = getlabel(prog, str)) == 0xFFFF) {
81 setcerr(103, str); /* label not found */
88 /* WORD値wordをアドレスadrに書込 */
89 /* 書込に成功した場合はtrue、失敗した場合はfalseを返す */
90 bool writememory(WORD word, WORD adr, PASS pass)
94 /* COMET IIメモリオーバーの場合 */
96 setcerr(119, word2n(adr)); /* out of COMET II memory */
100 if(pass == SECOND && asmode.asdetail == true) {
101 fprintf(stdout, "\t#%04X\t#%04X\n", adr, word);
108 /* 定数の前に等号(=)をつけて記述されるリテラルを返す */
109 /* リテラルには、10進定数/16進定数/文字定数が含まれる */
110 WORD getliteral(const char *str, PASS pass)
114 if(*(++str) == '\'') { /* 文字定数 */
115 writestr(str, true, pass);
117 writememory(nh2word(str), lptr++, pass);
122 /* ' 'で囲まれた文字定数をメモリに書込 */
123 /* DC命令とリテラルで使い、リテラルの場合はリテラル領域に書込 */
124 void writestr(const char *str, bool literal, PASS pass)
126 assert(cerrno == 0 && *str == '\'');
127 const char *p = str + 1;
131 /* 閉じ「'」がないまま文字列が終了した場合 */
133 setcerr(123, str); /* unclosed quote */
136 /* 「'」の場合、次の文字が「'」でない場合は正常終了 */
137 if(*p == '\'' && *(++p) != '\'') {
139 } else if(literal == true && lw == true) {
140 setcerr(124, str); /* more than one character in literal */
143 /*リテラルの場合はリテラル領域に書込 */
144 if(literal == true) {
145 writememory(*(p++), lptr++, pass);
148 writememory(*(p++), ptr++, pass);
153 /* アセンブラ命令DCをメモリに書込 */
154 void writeDC(const char *str, PASS pass)
158 writestr(str, false, pass);
160 if(*str == '#' || isdigit(*str) || *str == '-') {
163 if(pass == SECOND && (adr = getlabel(prog, str)) == 0xFFFF) {
164 setcerr(103, str); /* label not found */
167 writememory(adr, ptr++, pass);
171 /* 命令がアセンブラ命令の場合は処理を実行 */
172 /* 実行に成功した場合はtrue、それ以外の場合はfalseを返す */
173 bool assemblecmd(const CMDLINE *cmdl, PASS pass)
179 { START, 0, 1, "START" },
180 { END, 0, 0, "END" },
181 { DC, 1, OPDSIZE, "DC" },
187 if(strcmp(cmdl->cmd, ascmd[i].cmd) == 0) {
188 if(cmdl->opd->opdc < ascmd[i].opdc_min || cmdl->opd->opdc > ascmd[i].opdc_max) {
189 setcerr(106, NULL); /* operand count mismatch */
192 cmd = ascmd[i].cmdid;
195 } while(ascmd[++i].cmdid != 0);
200 if(cmdl->label == NULL) {
201 setcerr(107, NULL); /* no label in START */
205 prog = strdup(cmdl->label);
206 /* オペランドがある場合、実行開始番地を設定 */
207 if(pass == SECOND && cmdl->opd->opdc == 1) {
208 if((startptr = getlabel(prog, cmdl->opd->opdv[0])) == 0xFFFF) {
209 setcerr(103, cmdl->opd->opdv[0]); /* label not found */
219 /* 実行終了番地と次のプログラムの実行開始番地を設定 */
220 else if(pass == SECOND) {
227 for(i = 0; i < atoi(cmdl->opd->opdv[0]); i++) {
228 writememory(0x0, ptr++, pass);
236 for(i = 0; i < cmdl->opd->opdc; i++) {
237 writeDC(cmdl->opd->opdv[i], pass);
253 /* 命令がマクロ命令の場合はメモリに書込 */
254 /* 書込に成功した場合はtrue、それ以外の場合はfalseを返す */
255 bool macrocmd(const CMDLINE *cmdl, PASS pass)
260 CMDARRAY macrocmd[] = {
262 { OUT, 2, 2, "OUT" },
263 { RPUSH, 0, 0, "RPUSH" },
264 { RPOP, 0, 0, "RPOP" },
269 if(strcmp(cmdl->cmd, macrocmd[i].cmd) == 0) {
270 if(cmdl->opd->opdc < macrocmd[i].opdc_min || cmdl->opd->opdc > macrocmd[i].opdc_max) {
271 setcerr(106, NULL); /* operand count mismatch */
274 cmd = macrocmd[i].cmdid;
277 } while(macrocmd[++i].cmdid != 0);
281 status = writeIN(cmdl->opd->opdv[0], cmdl->opd->opdv[1], pass);
284 status = writeOUT(cmdl->opd->opdv[0], cmdl->opd->opdv[1], pass);
287 status = writeRPUSH(pass);
290 status = writeRPOP(pass);
299 /* 書込に成功した場合はtrue、それ以外の場合はfalseを返す */
300 bool cometcmd(const CMDLINE *cmdl, PASS pass)
302 WORD cmd, adr, r1, r2, x;
306 if(cmdl->opd->opdc == 0) {
307 if((cmd = getcmdcode(cmdl->cmd, NONE)) == 0xFFFF) {
308 setcerr(112, cmdl->cmd); /* not command of no operand */
311 if(writememory(cmd, ptr++, pass) == true) {
316 else if((r1 = getgr(cmdl->opd->opdv[0], false)) != 0xFFFF) {
318 if(cmdl->opd->opdc == 1) {
319 if((cmd = getcmdcode(cmdl->cmd, R_)) == 0xFFFF) {
320 setcerr(108, cmdl->cmd); /* not command of operand "r" */
324 if(writememory(cmd, ptr++, pass) == true) {
328 /* オペランド数2。第2オペランドは汎用レジスタ */
329 else if(cmdl->opd->opdc == 2 && (r2 = getgr(cmdl->opd->opdv[1], false)) != 0xFFFF) {
330 if((cmd = getcmdcode(cmdl->cmd, R1_R2)) == 0xFFFF) {
331 setcerr(109, cmdl->cmd); /* not command of operand "r1,r2" */
334 cmd |= ((r1 << 4) | r2);
335 if(cerrno == 0 && writememory(cmd, ptr++, pass) == true) {
339 /* オペランド数2〜3。第2オペランドはアドレス、 */
340 /* 第3オペランドは指標レジスタとして用いる汎用レジスタ */
341 else if(cmdl->opd->opdc == 2 || cmdl->opd->opdc == 3) {
342 if((cmd = getcmdcode(cmdl->cmd, R_ADR_X_)) == 0xFFFF &&
343 (cmd = getcmdcode(cmdl->cmd, R_ADR_X)) == 0xFFFF)
345 setcerr(110, cmdl->cmd); /* not command of operand "r,adr[,x]" */
350 if(cmdl->opd->opdc == 3) {
351 if((x = getgr(cmdl->opd->opdv[2], true)) == 0xFFFF) {
352 setcerr(125, cmdl->cmd); /* not GR in operand x */
357 adr = getadr(prog, cmdl->opd->opdv[1], pass);
358 writememory(cmd, ptr++, pass);
359 writememory(adr, ptr++, pass);
364 setcerr(113, cmdl->cmd); /* operand too many in COMET II command */
368 /* オペランド数1〜2。第1オペランドはアドレス */
369 else if(cmdl->opd->opdc == 1 || cmdl->opd->opdc == 2) {
370 if((cmd = getcmdcode(cmdl->cmd, ADR_X)) == 0xFFFF) {
371 setcerr(111, cmdl->cmd); /* not command of operand "adr[,x]" */
374 /* オペランド数2の場合、第2オペランドは指標レジスタとして用いる汎用レジスタ */
375 if(cmdl->opd->opdc == 2) {
376 x = getgr(cmdl->opd->opdv[1], true);
382 /* CALLの場合はプログラムの入口名を表すラベルを取得 */
383 /* CALL以外の命令の場合と、プログラムの入口名を取得できない場合は、 */
384 /* 同一プログラム内のラベルを取得 */
385 if(pass == SECOND && cmd == 0x8000) { /* CALL命令 */
386 adr = getlabel(NULL, cmdl->opd->opdv[0]);
388 if(cmd != 0x8000 || (pass == SECOND && adr == 0xFFFF)) {
389 adr = getadr(prog, cmdl->opd->opdv[0], pass);
391 writememory(cmd, ptr++, pass);
392 writememory(adr, ptr++, pass);
401 bool assembleline(const CMDLINE *cmdl, PASS pass)
405 if(cmdl->cmd == NULL){
406 /* ラベルが定義されていて命令がない場合はエラー */
407 if(cmdl->label != NULL) {
408 setcerr(105, NULL); /* no command in the line */
412 else if(cerrno == 0 && assemblecmd(cmdl, pass) == true) {
416 else if(cerrno == 0 && macrocmd(cmdl, pass) == true) {
420 else if(cerrno == 0 && cometcmd(cmdl, pass) == true) {
423 else if(cerrno == 0) {
424 setcerr(113, cmdl->cmd); /* operand too many in COMET II command */
433 void printline(FILE *stream, const char *filename, int lineno, char *line) {
434 fprintf(stream, "%s:%5d:%s", filename, lineno, line);
437 /* 指定された名前のファイルをアセンブル */
439 bool assemble(const char *file, PASS pass)
447 addcerrlist_assemble();
448 if((fp = fopen(file, "r")) == NULL) {
453 cmdl = malloc(sizeof(CMDLINE));
454 line = malloc(LINESIZE + 1);
455 if((line = fgets(line, LINESIZE, fp)) == NULL) {
459 if((pass == FIRST && asmode.src == true) ||
460 (pass == SECOND && asmode.asdetail == true))
462 printline(stdout, file, lineno, line);
464 if((cmdl = linetok(line)) != NULL) {
465 if(pass == FIRST && cmdl->label != NULL) {
466 if(addlabel(prog, cmdl->label, ptr) == false) {
470 if(assembleline(cmdl, pass) == false) {
479 fprintf(stderr, "Assemble error - %d: %s\n", cerrno, cerrmsg);
480 printline(stderr, file, lineno, line);