11 * アセンブルモード: src, label, onlylabel, asdetail, onlyassemble
13 ASMODE asmode = {false, false, false, false, false};
16 * アセンブルのプロパティ: ptr, lptr, *prog
23 static CERR cerr_assemble[] = {
24 { 101, "label already defined" },
25 { 102, "label table is full" },
26 { 103, "label not found" },
27 { 106, "operand mismatch in assemble command" },
28 { 107, "no label in START" },
29 { 108, "not command of operand \"r\"" },
30 { 109, "not command of operand \"r1,r2\"" },
31 { 110, "not command of operand \"r,adr[,x]\"" },
32 { 111, "not command of operand \"adr[,x]\"" },
33 { 112, "not command of no operand" },
34 { 113, "operand too many in COMET II command" },
35 { 119, "out of COMET II memory" },
36 { 120, "GR0 in operand x" },
37 { 122, "cannot create hash table" },
38 { 124, "more than one character in literal" },
39 { 125, "not GR in operand x" },
42 WORD getadr(const char *prog, const char *str, PASS pass);
44 WORD getgr(const char *str, bool is_x);
46 WORD getliteral(const char *str, PASS pass);
48 bool assemblecmd(const CMDLINE *cmdl, PASS pass);
50 bool macrocmd(const CMDLINE *cmdl, PASS pass);
52 void writeIN(const char *ibuf, const char *len, PASS pass);
54 void writeOUT(const char *obuf, const char *len, PASS pass);
56 void writeRPUSH(PASS pass);
58 void writeRPOP(PASS pass);
60 bool cometcmd(const CMDLINE *cmdl, PASS pass);
62 bool writememory(WORD word, WORD adr, PASS pass);
64 void writestr(const char *str, bool literal, PASS pass);
66 void writeDC(const char *str, PASS pass);
68 bool assembletok(const CMDLINE *cmdl, PASS pass);
70 bool assembleline(const char *line, PASS pass);
72 void printline(FILE *stream, const char *filename, int lineno, char *line);
75 * 汎用レジスタを表す文字列「GR[0-7]」から、レジスタ番号[0-7]をWORD値で返す
76 * 文字列が汎用レジスタを表さない場合は、0xFFFFを返す
77 * is_xがtrueの場合は指標レジスタ。GR0が指定された場合は、COMET IIの仕様によりエラー発生
79 WORD getgr(const char *str, bool is_x)
83 /* 「GR[0-7]」以外の文字列では、0xFFFFを返して終了 */
84 if(!(strlen(str) == 3 && strncmp(str, "GR", 2) == 0 &&
85 (*(str+2) >= '0' && *(str+2) <= '0' + (GRSIZE - 1))))
89 r = (WORD)(*(str+2) - '0');
90 /* GR0は指標レジスタとして用いることができない */
91 if(is_x == true && r == 0x0) {
92 setcerr(120, NULL); /* GR0 in operand x */
99 * 定数の前に等号(=)をつけて記述されるリテラルを返す
100 * リテラルには、10進定数/16進定数/文字定数が含まれる
102 WORD getliteral(const char *str, PASS pass)
104 WORD adr = asptr->lptr;
106 if(*(++str) == '\'') { /* 文字定数 */
107 writestr(str, true, pass);
109 writememory(nh2word(str), (asptr->lptr)++, pass);
116 * 実行に成功した場合はtrue、それ以外の場合はfalseを返す
118 bool assemblecmd(const CMDLINE *cmdl, PASS pass)
124 { START, 0, 1, "START" },
125 { END, 0, 0, "END" },
126 { DC, 1, OPDSIZE, "DC" },
132 if(strcmp(cmdl->cmd, ascmd[i].cmd) == 0) {
133 if(cmdl->opd->opdc < ascmd[i].opdc_min || cmdl->opd->opdc > ascmd[i].opdc_max) {
134 setcerr(106, NULL); /* operand count mismatch */
137 cmdid = ascmd[i].cmdid;
140 } while(ascmd[++i].cmdid != 0);
145 if(cmdl->label == NULL) {
146 setcerr(107, NULL); /* no label in START */
150 asptr->prog = strdup_chk(cmdl->label, "asptr.prog");
151 /* オペランドがある場合、実行開始アドレスを設定 */
152 if(pass == SECOND && cmdl->opd->opdc == 1) {
153 if((execptr->start = getlabel(asptr->prog, cmdl->opd->opdv[0])) == 0xFFFF) {
154 setcerr(103, cmdl->opd->opdv[0]); /* label not found */
160 /* 1回目のアセンブルの場合は、リテラル領域開始アドレスを設定 */
162 asptr->lptr = asptr->ptr;
164 /* 2回目のアセンブルの場合は、リテラル領域終了アドレスを実行終了アドレスとして設定 */
165 else if(pass == SECOND) {
166 execptr->end = asptr->lptr;
173 for(i = 0; i < atoi(cmdl->opd->opdv[0]); i++) {
174 writememory(0x0, (asptr->ptr)++, pass);
182 for(i = 0; i < cmdl->opd->opdc; i++) {
183 writeDC(cmdl->opd->opdv[i], pass);
202 * 書込に成功した場合はtrue、それ以外の場合はfalseを返す
204 bool macrocmd(const CMDLINE *cmdl, PASS pass)
207 MACROCMDID cmdid = 0;
208 MACROCMD macrocmd[] = {
210 { OUT, 2, 2, "OUT" },
211 { RPUSH, 0, 0, "RPUSH" },
212 { RPOP, 0, 0, "RPOP" },
217 if(strcmp(cmdl->cmd, macrocmd[i].cmd) == 0) {
218 if(cmdl->opd->opdc < macrocmd[i].opdc_min ||
219 cmdl->opd->opdc > macrocmd[i].opdc_max)
221 setcerr(106, NULL); /* operand count mismatch */
224 cmdid = macrocmd[i].cmdid;
227 } while(macrocmd[++i].cmdid != 0);
231 writeIN(cmdl->opd->opdv[0], cmdl->opd->opdv[1], pass);
234 writeOUT(cmdl->opd->opdv[0], cmdl->opd->opdv[1], pass);
248 * マクロ命令「IN IBUF,LEN」をメモリに書込
257 void writeIN(const char *ibuf, const char *len, PASS pass)
259 char *line = malloc_chk(LINESIZE+1, "writeIN.line");
261 assembleline(" PUSH 0,GR1", pass);
262 assembleline(" PUSH 0,GR2", pass);
263 sprintf(line, " LAD GR1,%s", ibuf);
264 assembleline(line, pass);
265 sprintf(line, " LAD GR2,%s", len);
266 assembleline(line, pass);
267 assembleline(" SVC 1", pass);
268 assembleline(" POP GR2", pass);
269 assembleline(" POP GR1", pass);
271 free_chk(line, "writeIN.line");
275 * マクロ命令「OUT OBUF,LEN」をメモリに書込
287 void writeOUT(const char *obuf, const char *len, PASS pass)
289 char *line = malloc_chk(LINESIZE+1, "writeOUT.line");
291 assembleline(" PUSH 0,GR1", pass);
292 assembleline(" PUSH 0,GR2", pass);
293 sprintf(line, " LAD GR1,%s", obuf);
294 assembleline(line, pass);
295 sprintf(line, " LAD GR2,%s", len);
296 assembleline(line, pass);
297 assembleline(" SVC 2", pass);
298 assembleline(" LAD GR1,=#A", pass);
299 assembleline(" LAD GR2,=1", pass);
300 assembleline(" SVC 2", pass);
301 assembleline(" POP GR2", pass);
302 assembleline(" POP GR1", pass);
303 free_chk(line, "writeOUT.line");
306 /** マクロ命令「RPUSH」をメモリに書き込む
315 void writeRPUSH(PASS pass)
318 char *line = malloc_chk(LINESIZE+1, "writeRPUSH.line");
320 for(i = 1; i <= GRSIZE-1; i++) {
321 sprintf(line, " PUSH 0,GR%d", i);
322 assembleline(line, pass);
324 free_chk(line, "writeRPUSH.line");
328 * マクロ命令「RPOP」をメモリに書き込む
338 void writeRPOP(PASS pass)
341 char *line = malloc_chk(LINESIZE+1, "writeRPOP.line");
343 for(i = GRSIZE-1; i >= 1; i--) {
344 sprintf(line, " POP GR%d", i);
345 assembleline(line, pass);
347 free_chk(line, "writeRPOP.line");
352 * 書込に、成功した場合はtrue、失敗した場合はfalse、を返す
354 bool cometcmd(const CMDLINE *cmdl, PASS pass)
356 WORD cmd, adr, r1, r2, x;
360 if(cmdl->opd->opdc == 0) {
361 if((cmd = getcmdcode(cmdl->cmd, NONE)) == 0xFFFF) {
362 setcerr(112, cmdl->cmd); /* not command of no operand */
365 if(writememory(cmd, (asptr->ptr)++, pass) == true) {
370 else if((r1 = getgr(cmdl->opd->opdv[0], false)) != 0xFFFF) {
372 if(cmdl->opd->opdc == 1) {
373 if((cmd = getcmdcode(cmdl->cmd, R_)) == 0xFFFF) {
374 setcerr(108, cmdl->cmd); /* not command of operand "r" */
378 if(writememory(cmd, (asptr->ptr)++, pass) == true) {
382 /* オペランド数2。第2オペランドは汎用レジスタ */
383 else if(cmdl->opd->opdc == 2 && (r2 = getgr(cmdl->opd->opdv[1], false)) != 0xFFFF) {
384 if((cmd = getcmdcode(cmdl->cmd, R1_R2)) == 0xFFFF) {
385 setcerr(109, cmdl->cmd); /* not command of operand "r1,r2" */
388 cmd |= ((r1 << 4) | r2);
389 if(cerr->num == 0 && writememory(cmd, (asptr->ptr)++, pass) == true) {
393 /* オペランド数2または3。第2オペランドはアドレス、 */
394 /* 第3オペランドは指標レジスタとして用いる汎用レジスタ */
395 else if(cmdl->opd->opdc == 2 || cmdl->opd->opdc == 3) {
396 if((cmd = getcmdcode(cmdl->cmd, R_ADR_X_)) == 0xFFFF &&
397 (cmd = getcmdcode(cmdl->cmd, R_ADR_X)) == 0xFFFF)
399 setcerr(110, cmdl->cmd); /* not command of operand "r,adr[,x]" */
404 if(cmdl->opd->opdc == 3) {
405 if((x = getgr(cmdl->opd->opdv[2], true)) == 0xFFFF) {
406 setcerr(125, cmdl->cmd); /* not GR in operand x */
411 adr = getadr(asptr->prog, cmdl->opd->opdv[1], pass);
412 writememory(cmd, (asptr->ptr)++, pass);
413 writememory(adr, (asptr->ptr)++, pass);
418 setcerr(113, cmdl->cmd); /* operand too many in COMET II command */
422 /* オペランド数1または2。第1オペランドはアドレス */
423 else if(cmdl->opd->opdc == 1 || cmdl->opd->opdc == 2) {
424 if((cmd = getcmdcode(cmdl->cmd, ADR_X)) == 0xFFFF) {
425 setcerr(111, cmdl->cmd); /* not command of operand "adr[,x]" */
428 /* オペランド数2の場合、第2オペランドは指標レジスタとして用いる汎用レジスタ */
429 if(cmdl->opd->opdc == 2) {
430 x = getgr(cmdl->opd->opdv[1], true);
436 /* CALLの場合はプログラムの入口名を表すラベルを取得 */
437 /* CALL以外の命令の場合と、プログラムの入口名を取得できない場合は、 */
438 /* 同一プログラム内のラベルを取得 */
439 if(pass == SECOND && cmd == 0x8000) { /* CALL命令 */
440 adr = getlabel(NULL, cmdl->opd->opdv[0]);
442 if(cmd != 0x8000 || (pass == SECOND && adr == 0xFFFF)) {
443 adr = getadr(asptr->prog, cmdl->opd->opdv[0], pass);
445 writememory(cmd, (asptr->ptr)++, pass);
446 writememory(adr, (asptr->ptr)++, pass);
455 * COMET IIのメモリにアドレス値を書き込む
457 bool writememory(WORD word, WORD adr, PASS pass)
461 /* COMET IIメモリオーバーの場合 */
462 if(adr >= sys->memsize) {
463 setcerr(119, word2n(adr)); /* out of COMET II memory */
466 (sys->memory)[adr] = word;
467 if(pass == SECOND && asmode.asdetail == true) {
468 fprintf(stdout, "\t#%04X\t#%04X\n", adr, word);
478 void writestr(const char *str, bool literal, PASS pass)
480 assert(cerr->num == 0 && *str == '\'');
481 const char *p = str + 1;
485 /* 閉じ「'」がないまま文字列が終了した場合 */
487 setcerr(123, str); /* unclosed quote */
490 /* 「'」の場合、次の文字が「'」でない場合は正常終了 */
491 if(*p == '\'' && *(++p) != '\'') {
493 } else if(literal == true && lw == true) {
494 setcerr(124, str); /* more than one character in literal */
497 /*リテラルの場合はリテラル領域に書込 */
498 if(literal == true) {
499 writememory(*(p++), (asptr->lptr)++, pass);
502 writememory(*(p++), (asptr->ptr)++, pass);
510 void writeDC(const char *str, PASS pass)
514 writestr(str, false, pass);
516 if(*str == '#' || isdigit(*str) || *str == '-') {
519 if(pass == SECOND && (adr = getlabel(asptr->prog, str)) == 0xFFFF) {
520 setcerr(103, str); /* label not found */
523 writememory(adr, (asptr->ptr)++, pass);
530 bool assembletok(const CMDLINE *cmdl, PASS pass)
534 if(cmdl->cmd == NULL){
535 /* ラベルが定義されていて命令がない場合はエラー */
536 if(cmdl->label != NULL) {
537 setcerr(105, NULL); /* no command in the line */
541 else if(cerr->num == 0 && assemblecmd(cmdl, pass) == true) {
545 else if(cerr->num == 0 && macrocmd(cmdl, pass) == true) {
549 else if(cerr->num == 0 && cometcmd(cmdl, pass) == true) {
552 else if(cerr->num == 0) {
553 setcerr(113, cmdl->cmd); /* operand too many in COMET II command */
563 * ファイルストリームの現在行を番号付きで表示する
565 void printline(FILE *stream, const char *filename, int lineno, char *line)
567 fprintf(stream, "%s:%5d:%s", filename, lineno, line);
572 * アドレスには、リテラル/10進定数/16進定数/アドレス定数が含まれる
574 WORD getadr(const char *prog, const char *str, PASS pass)
578 adr = getliteral(str, pass);
579 } else if(isdigit(*str) || *str == '-' || *str == '#') {
582 if(pass == SECOND && (adr = getlabel(prog, str)) == 0xFFFF) {
584 setcerr(103, str); /* label not found */
595 bool assembleline(const char *line, PASS pass)
599 if((cmdl = linetok(line)) != NULL) {
600 if(pass == FIRST && cmdl->label != NULL) {
601 if(addlabel(asptr->prog, cmdl->label, asptr->ptr) == false) {
605 if(assembletok(cmdl, pass) == false) {
613 * アセンブルのエラーをエラーリストに追加
615 void addcerrlist_assemble()
619 addcerrlist(ARRAYSIZE(cerr_assemble), cerr_assemble);
626 bool assemble(const char *file, PASS pass)
634 if((fp = fopen(file, "r")) == NULL) {
639 cmdl = malloc_chk(sizeof(CMDLINE), "cmdl");
640 line = malloc_chk(LINESIZE + 1, "line");
641 if((line = fgets(line, LINESIZE, fp)) == NULL) {
645 if((pass == FIRST && asmode.src == true) ||
646 (pass == SECOND && asmode.asdetail == true))
648 printline(stdout, file, lineno, line);
650 if(assembleline(line, pass) == false) {
656 free_chk(line, "line");
657 free_chk(cmdl, "cmdl");
660 fprintf(stderr, "Assemble error - %d: %s\n", cerr->num, cerr->msg);
661 printline(stderr, file, lineno, line);
669 * 引数で指定したファイルにアセンブル結果を書込
671 void outassemble(const char *file)
675 if((fp = fopen(file, "w")) == NULL) {
679 fwrite(sys->memory, sizeof(WORD), execptr->end, fp);