11 * アセンブルモード: src, label, onlylabel, asdetail, onlyassemble
13 ASMODE asmode = {false, false, false, false, false};
23 static CERR cerr_assemble[] = {
24 { 101, "label already defined" },
25 { 102, "label table is full" },
26 { 103, "label not found" },
27 { 104, "label length is too long" },
28 { 105, "no command in the line" },
29 { 106, "operand mismatch in assemble command" },
30 { 107, "no label in START" },
31 { 108, "not command of operand \"r\"" },
32 { 109, "not command of operand \"r1,r2\"" },
33 { 110, "not command of operand \"r,adr[,x]\"" },
34 { 111, "not command of operand \"adr[,x]\"" },
35 { 112, "not command of no operand" },
36 { 113, "operand too many in COMET II command" },
37 { 117, "operand too many in DC" },
38 { 118, "operand length too long" },
39 { 119, "out of COMET II memory" },
40 { 120, "GR0 in operand x" },
41 { 121, "cannot get operand token" },
42 { 122, "cannot create hash table" },
43 { 123, "unclosed quote" },
44 { 124, "more than one character in literal" },
45 { 125, "not GR in operand x" },
49 static WORD getadr(const char *prog, const char *str, PASS pass);
51 static WORD getgr(const char *str, bool is_x);
53 static WORD getliteral(const char *str, PASS pass);
55 static bool assemblecmd(const CMDLINE *cmdl, PASS pass);
57 static bool macrocmd(const CMDLINE *cmdl, PASS pass);
59 static bool writeIN(const char *ibuf, const char *len, PASS pass);
61 static bool writeOUT(const char *obuf, const char *len, PASS pass);
63 static bool writeRPUSH(PASS pass);
65 static bool writeRPOP(PASS pass);
67 static bool cometcmd(const CMDLINE *cmdl, PASS pass);
69 static bool writememory(WORD word, WORD adr, PASS pass);
71 static void writestr(const char *str, bool literal, PASS pass);
73 static void writeDC(const char *str, PASS pass);
75 static bool assembleline(const CMDLINE *cmdl, PASS pass);
77 static void printline(FILE *stream, const char *filename, int lineno, char *line);
81 * 汎用レジスタを表す文字列「GR[0-7]」から、レジスタ番号[0-7]をWORD値で返す
82 * 文字列が汎用レジスタを表さない場合は、0xFFFFを返す
83 * is_xがtrueの場合は指標レジスタ。GR0が指定された場合は、COMET IIの仕様によりエラー発生
85 WORD getgr(const char *str, bool is_x)
89 /* 「GR[0-7]」以外の文字列では、0xFFFFを返して終了 */
90 if(!(strlen(str) == 3 && strncmp(str, "GR", 2) == 0 &&
91 (*(str+2) >= '0' && *(str+2) <= '0' + (GRSIZE - 1))))
95 r = (WORD)(*(str+2) - '0');
96 /* GR0は指標レジスタとして用いることができない */
97 if(is_x == true && r == 0x0) {
98 setcerr(120, NULL); /* GR0 in operand x */
105 * 定数の前に等号(=)をつけて記述されるリテラルを返す
106 * リテラルには、10進定数/16進定数/文字定数が含まれる
108 WORD getliteral(const char *str, PASS pass)
110 WORD adr = asprop->lptr;
112 if(*(++str) == '\'') { /* 文字定数 */
113 writestr(str, true, pass);
115 writememory(nh2word(str), (asprop->lptr)++, pass);
122 * 実行に成功した場合はtrue、それ以外の場合はfalseを返す
124 bool assemblecmd(const CMDLINE *cmdl, PASS pass)
130 { START, 0, 1, "START" },
131 { END, 0, 0, "END" },
132 { DC, 1, OPDSIZE, "DC" },
138 if(strcmp(cmdl->cmd, ascmd[i].cmd) == 0) {
139 if(cmdl->opd->opdc < ascmd[i].opdc_min || cmdl->opd->opdc > ascmd[i].opdc_max) {
140 setcerr(106, NULL); /* operand count mismatch */
143 cmdid = ascmd[i].cmdid;
146 } while(ascmd[++i].cmdid != 0);
151 if(cmdl->label == NULL) {
152 setcerr(107, NULL); /* no label in START */
156 asprop->prog = strdup_chk(cmdl->label, "asprop.prog");
157 /* オペランドがある場合、実行開始番地を設定 */
158 if(pass == SECOND && cmdl->opd->opdc == 1) {
159 if((prog->start = getlabel(asprop->prog, cmdl->opd->opdv[0])) == 0xFFFF) {
160 setcerr(103, cmdl->opd->opdv[0]); /* label not found */
168 asprop->lptr = asprop->ptr;
170 /* 実行終了番地と次のプログラムの実行開始番地を設定 */
171 else if(pass == SECOND) {
172 prog->end = asprop->lptr;
178 for(i = 0; i < atoi(cmdl->opd->opdv[0]); i++) {
179 writememory(0x0, (asprop->ptr)++, pass);
187 for(i = 0; i < cmdl->opd->opdc; i++) {
188 writeDC(cmdl->opd->opdv[i], pass);
207 * 書込に成功した場合はtrue、それ以外の場合はfalseを返す
209 bool macrocmd(const CMDLINE *cmdl, PASS pass)
214 MACROCMD macrocmd[] = {
216 { OUT, 2, 2, "OUT" },
217 { RPUSH, 0, 0, "RPUSH" },
218 { RPOP, 0, 0, "RPOP" },
223 if(strcmp(cmdl->cmd, macrocmd[i].cmd) == 0) {
224 if(cmdl->opd->opdc < macrocmd[i].opdc_min ||
225 cmdl->opd->opdc > macrocmd[i].opdc_max)
227 setcerr(106, NULL); /* operand count mismatch */
230 cmdid = macrocmd[i].cmdid;
233 } while(macrocmd[++i].cmdid != 0);
237 status = writeIN(cmdl->opd->opdv[0], cmdl->opd->opdv[1], pass);
240 status = writeOUT(cmdl->opd->opdv[0], cmdl->opd->opdv[1], pass);
243 status = writeRPUSH(pass);
246 status = writeRPOP(pass);
255 * マクロ命令「IN IBUF,LEN」をメモリに書込
264 bool writeIN(const char *ibuf, const char *len, PASS pass)
269 writememory(0x7001, (asprop->ptr)++, pass);
270 writememory(0x0, (asprop->ptr)++, pass);
272 writememory(0x7002, (asprop->ptr)++, pass);
273 writememory(0x0, (asprop->ptr)++, pass);
275 writememory(0x1210, (asprop->ptr)++, pass);
276 writememory(getadr(asprop->prog, ibuf, pass), (asprop->ptr)++, pass);
278 writememory(0x1220, (asprop->ptr)++, pass);
279 writememory(getadr(asprop->prog, len, pass), (asprop->ptr)++, pass);
281 writememory(0xF000, (asprop->ptr)++, pass);
282 writememory(0x0001, (asprop->ptr)++, pass);
284 writememory(0x7120, (asprop->ptr)++, pass);
286 writememory(0x7110, (asprop->ptr)++, pass);
294 * マクロ命令「OUT OBUF,LEN」をメモリに書込
306 bool writeOUT(const char *obuf, const char *len, PASS pass)
311 writememory(0x7001, (asprop->ptr)++, pass);
312 writememory(0x0, (asprop->ptr)++, pass);
314 writememory(0x7002, (asprop->ptr)++, pass);
315 writememory(0x0, (asprop->ptr)++, pass);
317 writememory(0x1210, (asprop->ptr)++, pass);
318 writememory(getadr(asprop->prog, obuf, pass), (asprop->ptr)++, pass);
320 writememory(0x1220, (asprop->ptr)++, pass);
321 writememory(getadr(asprop->prog, len, pass), (asprop->ptr)++, pass);
323 writememory(0xF000, (asprop->ptr)++, pass);
324 writememory(0x0002, (asprop->ptr)++, pass);
326 writememory(0x1210, (asprop->ptr)++, pass);
330 writememory(asprop->lptr, (asprop->ptr)++, pass); /* リテラルのアドレスを書込 */
332 writememory(0xA, (asprop->lptr)++, pass);
334 writememory(0x1220, (asprop->ptr)++, pass);
338 writememory(asprop->lptr, (asprop->ptr)++, pass); /* リテラルのアドレスを書込 */
340 writememory(0x1, (asprop->lptr)++, pass);
342 writememory(0xF000, (asprop->ptr)++, pass);
343 writememory(0x0002, (asprop->ptr)++, pass);
345 writememory(0x7120, (asprop->ptr)++, pass);
347 writememory(0x7110, (asprop->ptr)++, pass);
354 /** マクロ命令「RPUSH」をメモリに書き込む
363 bool writeRPUSH(PASS pass)
368 for(i = 1; i <= 7; i++) {
369 writememory(0x7000 + i, (asprop->ptr)++, pass); /* PUSH GRn */
370 writememory(0x0, (asprop->ptr)++, pass);
379 * マクロ命令「RPOP」をメモリに書き込む
389 bool writeRPOP(PASS pass)
393 for(i = 7; i >= 1; i--) {
394 writememory((0x7100 + (i << 4)), (asprop->ptr)++, pass); /* POP GRn */
404 * 書込に成功した場合はtrue、それ以外の場合はfalseを返す
406 bool cometcmd(const CMDLINE *cmdl, PASS pass)
408 WORD cmd, adr, r1, r2, x;
412 if(cmdl->opd->opdc == 0) {
413 if((cmd = getcmdcode(cmdl->cmd, NONE)) == 0xFFFF) {
414 setcerr(112, cmdl->cmd); /* not command of no operand */
417 if(writememory(cmd, (asprop->ptr)++, pass) == true) {
422 else if((r1 = getgr(cmdl->opd->opdv[0], false)) != 0xFFFF) {
424 if(cmdl->opd->opdc == 1) {
425 if((cmd = getcmdcode(cmdl->cmd, R_)) == 0xFFFF) {
426 setcerr(108, cmdl->cmd); /* not command of operand "r" */
430 if(writememory(cmd, (asprop->ptr)++, pass) == true) {
434 /* オペランド数2。第2オペランドは汎用レジスタ */
435 else if(cmdl->opd->opdc == 2 && (r2 = getgr(cmdl->opd->opdv[1], false)) != 0xFFFF) {
436 if((cmd = getcmdcode(cmdl->cmd, R1_R2)) == 0xFFFF) {
437 setcerr(109, cmdl->cmd); /* not command of operand "r1,r2" */
440 cmd |= ((r1 << 4) | r2);
441 if(cerr->num == 0 && writememory(cmd, (asprop->ptr)++, pass) == true) {
445 /* オペランド数2〜3。第2オペランドはアドレス、 */
446 /* 第3オペランドは指標レジスタとして用いる汎用レジスタ */
447 else if(cmdl->opd->opdc == 2 || cmdl->opd->opdc == 3) {
448 if((cmd = getcmdcode(cmdl->cmd, R_ADR_X_)) == 0xFFFF &&
449 (cmd = getcmdcode(cmdl->cmd, R_ADR_X)) == 0xFFFF)
451 setcerr(110, cmdl->cmd); /* not command of operand "r,adr[,x]" */
456 if(cmdl->opd->opdc == 3) {
457 if((x = getgr(cmdl->opd->opdv[2], true)) == 0xFFFF) {
458 setcerr(125, cmdl->cmd); /* not GR in operand x */
463 adr = getadr(asprop->prog, cmdl->opd->opdv[1], pass);
464 writememory(cmd, (asprop->ptr)++, pass);
465 writememory(adr, (asprop->ptr)++, pass);
470 setcerr(113, cmdl->cmd); /* operand too many in COMET II command */
474 /* オペランド数1〜2。第1オペランドはアドレス */
475 else if(cmdl->opd->opdc == 1 || cmdl->opd->opdc == 2) {
476 if((cmd = getcmdcode(cmdl->cmd, ADR_X)) == 0xFFFF) {
477 setcerr(111, cmdl->cmd); /* not command of operand "adr[,x]" */
480 /* オペランド数2の場合、第2オペランドは指標レジスタとして用いる汎用レジスタ */
481 if(cmdl->opd->opdc == 2) {
482 x = getgr(cmdl->opd->opdv[1], true);
488 /* CALLの場合はプログラムの入口名を表すラベルを取得 */
489 /* CALL以外の命令の場合と、プログラムの入口名を取得できない場合は、 */
490 /* 同一プログラム内のラベルを取得 */
491 if(pass == SECOND && cmd == 0x8000) { /* CALL命令 */
492 adr = getlabel(NULL, cmdl->opd->opdv[0]);
494 if(cmd != 0x8000 || (pass == SECOND && adr == 0xFFFF)) {
495 adr = getadr(asprop->prog, cmdl->opd->opdv[0], pass);
497 writememory(cmd, (asprop->ptr)++, pass);
498 writememory(adr, (asprop->ptr)++, pass);
507 * COMET IIのメモリにアドレス値を書き込む
509 bool writememory(WORD word, WORD adr, PASS pass)
513 /* COMET IIメモリオーバーの場合 */
514 if(adr >= sys->memsize) {
515 setcerr(119, word2n(adr)); /* out of COMET II memory */
518 (sys->memory)[adr] = word;
519 if(pass == SECOND && asmode.asdetail == true) {
520 fprintf(stdout, "\t#%04X\t#%04X\n", adr, word);
530 void writestr(const char *str, bool literal, PASS pass)
532 assert(cerr->num == 0 && *str == '\'');
533 const char *p = str + 1;
537 /* 閉じ「'」がないまま文字列が終了した場合 */
539 setcerr(123, str); /* unclosed quote */
542 /* 「'」の場合、次の文字が「'」でない場合は正常終了 */
543 if(*p == '\'' && *(++p) != '\'') {
545 } else if(literal == true && lw == true) {
546 setcerr(124, str); /* more than one character in literal */
549 /*リテラルの場合はリテラル領域に書込 */
550 if(literal == true) {
551 writememory(*(p++), (asprop->lptr)++, pass);
554 writememory(*(p++), (asprop->ptr)++, pass);
562 void writeDC(const char *str, PASS pass)
566 writestr(str, false, pass);
568 if(*str == '#' || isdigit(*str) || *str == '-') {
571 if(pass == SECOND && (adr = getlabel(asprop->prog, str)) == 0xFFFF) {
572 setcerr(103, str); /* label not found */
575 writememory(adr, (asprop->ptr)++, pass);
582 bool assembleline(const CMDLINE *cmdl, PASS pass)
586 if(cmdl->cmd == NULL){
587 /* ラベルが定義されていて命令がない場合はエラー */
588 if(cmdl->label != NULL) {
589 setcerr(105, NULL); /* no command in the line */
593 else if(cerr->num == 0 && assemblecmd(cmdl, pass) == true) {
597 else if(cerr->num == 0 && macrocmd(cmdl, pass) == true) {
601 else if(cerr->num == 0 && cometcmd(cmdl, pass) == true) {
604 else if(cerr->num == 0) {
605 setcerr(113, cmdl->cmd); /* operand too many in COMET II command */
615 * ファイルストリームの現在行を番号付きで表示する
617 void printline(FILE *stream, const char *filename, int lineno, char *line)
619 fprintf(stream, "%s:%5d:%s", filename, lineno, line);
624 * アドレスには、リテラル/10進定数/16進定数/アドレス定数が含まれる
626 WORD getadr(const char *prog, const char *str, PASS pass)
630 adr = getliteral(str, pass);
631 } else if(isdigit(*str) || *str == '-' || *str == '#') {
634 if(pass == SECOND && (adr = getlabel(prog, str)) == 0xFFFF) {
636 setcerr(103, str); /* label not found */
647 bool assemble(const char *file, PASS pass)
655 addcerrlist(ARRAYSIZE(cerr_assemble), cerr_assemble);
656 if((fp = fopen(file, "r")) == NULL) {
661 cmdl = malloc_chk(sizeof(CMDLINE), "cmdl");
662 line = malloc_chk(LINESIZE + 1, "line");
663 if((line = fgets(line, LINESIZE, fp)) == NULL) {
667 if((pass == FIRST && asmode.src == true) ||
668 (pass == SECOND && asmode.asdetail == true))
670 printline(stdout, file, lineno, line);
672 if((cmdl = linetok(line)) != NULL) {
673 if(pass == FIRST && cmdl->label != NULL) {
674 if(addlabel(asprop->prog, cmdl->label, asprop->ptr) == false) {
678 if(assembleline(cmdl, pass) == false) {
685 free_chk(line, "line");
686 free_chk(cmdl, "cmdl");
689 fprintf(stderr, "Assemble error - %d: %s\n", cerr->num, cerr->msg);
690 printline(stderr, file, lineno, line);
698 * 引数で指定したファイルにアセンブル結果を書込
700 void outassemble(const char *file)
704 if((fp = fopen(file, "w")) == NULL) {
708 fwrite(sys->memory, sizeof(WORD), prog->end, fp);