11 * アセンブルモード: src, label, onlylabel, asdetail, onlyassemble
13 ASMODE asmode = {false, false, false, false, false};
16 * アセンブル時の、現在およびリテラルのアドレスとプログラム入口名: ptr, lptr, prog
23 static CERR cerr_assemble[] = {
24 { 106, "operand mismatch in CASL II command" },
25 { 107, "no label in START" },
26 { 108, "not command of operand \"r\"" },
27 { 109, "not command of operand \"r1,r2\"" },
28 { 110, "not command of operand \"r,adr[,x]\"" },
29 { 111, "not command of operand \"adr[,x]\"" },
30 { 112, "not command of no operand" },
31 { 113, "operand too many in COMET II command" },
32 { 119, "out of COMET II memory" },
33 { 120, "GR0 in operand x" },
34 { 122, "cannot create hash table" },
35 { 124, "more than one character in literal" },
36 { 125, "not GR in operand x" },
39 void printline(FILE *stream, const char *filename, int lineno, char *line);
41 WORD getadr(const char *prog, const char *str, PASS pass);
43 WORD getgr(const char *str, bool is_x);
45 WORD getliteral(const char *str, PASS pass);
47 void writememory(WORD word, WORD adr, PASS pass);
49 void writestr(const char *str, bool literal, PASS pass);
51 void writedc(const char *str, PASS pass);
53 void assemble_start(const CMDLINE *cmdl, PASS pass);
55 void assemble_ds(const CMDLINE *cmdl, PASS pass);
57 void assemble_end(const CMDLINE *cmdl, PASS pass);
59 void assemble_dc(const CMDLINE *cmdl, PASS pass);
61 void assemble_in(const CMDLINE *cmdl, PASS pass);
63 void assemble_out(const CMDLINE *cmdl, PASS pass);
65 void assemble_rpush(const CMDLINE *cmdl, PASS pass);
67 void assemble_rpop(const CMDLINE *cmdl, PASS pass);
69 bool casl2cmd(CMD *cmdtbl, const CMDLINE *cmdl, PASS pass);
71 bool assemble_comet2cmd(const CMDLINE *cmdl, PASS pass);
73 bool assembletok(const CMDLINE *cmdl, PASS pass);
75 bool assembleline(const char *line, PASS pass);
80 static CMD ascmd[] = {
81 { "START", assemble_start },
82 { "END", assemble_end },
83 { "DS", assemble_ds },
84 { "DC", assemble_dc },
91 static CMD macrocmd[] = {
92 { "OUT", assemble_out },
93 { "IN", assemble_in },
94 { "RPUSH", assemble_rpush },
95 { "RPOP", assemble_rpop },
100 * アセンブルのエラーをエラーリストに追加
102 void addcerrlist_assemble()
107 addcerrlist(ARRAYSIZE(cerr_assemble), cerr_assemble);
111 * ファイルストリームの現在行を番号付きで表示する
113 void printline(FILE *stream, const char *filename, int lineno, char *line)
115 fprintf(stream, "%s:%5d:%s", filename, lineno, line);
120 * アドレスには、リテラル/10進定数/16進定数/アドレス定数が含まれる
122 WORD getadr(const char *prog, const char *str, PASS pass)
127 adr = getliteral(str, pass);
128 } else if(isdigit(*str) || *str == '-' || *str == '#') {
132 if((adr = getlabel(prog, str)) == 0xFFFF) {
133 setcerr(103, str); /* label not found */
141 * 汎用レジスタを表す文字列 "GR[0-7]" から、レジスタ番号 [0-7] をWORD値で返す\n
142 * 文字列が汎用レジスタを表さない場合は、0xFFFFを返す\n
143 * is_xがtrueの場合は指標レジスタ。GR0が指定された場合は、COMET IIの仕様によりエラー発生
145 WORD getgr(const char *str, bool is_x)
149 /* "GR[0-7]" 以外の文字列では、0xFFFFを返して終了 */
150 if(!(strlen(str) == 3 && strncmp(str, "GR", 2) == 0 &&
151 (*(str+2) >= '0' && *(str+2) <= '0' + (GRSIZE - 1))))
155 r = (WORD)(*(str+2) - '0');
156 /* GR0は指標レジスタとして用いることができない */
157 if(is_x == true && r == 0x0) {
158 setcerr(120, ""); /* GR0 in operand x */
165 * 定数の前に等号(=)をつけて記述されるリテラルを返す\n
166 * リテラルには、10進定数/16進定数/文字定数が含まれる
168 WORD getliteral(const char *str, PASS pass)
171 WORD adr = asptr->lptr;
173 if(*(++str) == '\'') { /* 文字定数 */
174 writestr(str, true, pass);
176 writememory(nh2word(str), (asptr->lptr)++, pass);
184 void writememory(WORD word, WORD adr, PASS pass)
186 /* メモリオーバーの場合、エラー発生 */
187 if(adr >= sys->memsize) {
188 setcerr(119, word2n(adr)); /* out of COMET II memory */
191 (sys->memory)[adr] = word;
192 if(pass == SECOND && asmode.asdetail == true) {
193 fprintf(stdout, "\t#%04X\t#%04X\n", adr, word);
200 void writestr(const char *str, bool literal, PASS pass)
202 assert(*str == '\'');
203 const char *p = str + 1;
207 /* 閉じ「'」がないまま文字列が終了した場合 */
209 setcerr(123, str); /* unclosed quote */
212 /* 「'」の場合、次の文字が「'」でない場合は正常終了 */
213 if(*p == '\'' && *(++p) != '\'') {
215 } else if(literal == true && lw == true) {
216 setcerr(124, str); /* more than one character in literal */
219 /*リテラルの場合はリテラル領域に書込 */
220 if(literal == true) {
221 writememory(*(p++), (asptr->lptr)++, pass);
224 writememory(*(p++), (asptr->ptr)++, pass);
232 void writedc(const char *str, PASS pass)
237 writestr(str, false, pass);
239 if(*str == '#' || isdigit(*str) || *str == '-') {
242 if(pass == SECOND && (adr = getlabel(asptr->prog, str)) == 0xFFFF) {
243 setcerr(103, str); /* label not found */
246 writememory(adr, (asptr->ptr)++, pass);
254 void assemble_start(const CMDLINE *cmdl, PASS pass)
256 if(cmdl->opd->opdc > 1) {
257 setcerr(106, ""); /* operand count mismatch */
260 if(*(cmdl->label) == '\0') {
261 setcerr(107, ""); /* no label in START */
265 strcpy(asptr->prog, cmdl->label);
266 /* オペランドがある場合、実行開始アドレスを設定 */
267 if(pass == SECOND && cmdl->opd->opdv[0] != NULL) {
268 if((execptr->start = getlabel(asptr->prog, cmdl->opd->opdv[0])) == 0xFFFF) {
269 setcerr(103, cmdl->opd->opdv[0]); /* label not found */
278 void assemble_end(const CMDLINE *cmdl, PASS pass)
280 if(cmdl->opd->opdc > 0) {
281 setcerr(106, ""); /* operand count mismatch */
284 /* 1回目のアセンブルの場合は、リテラル領域開始アドレスを設定 */
286 asptr->lptr = asptr->ptr;
288 /* 2回目のアセンブルの場合は、リテラル領域終了アドレスを実行終了アドレスとして設定 */
289 else if(pass == SECOND) {
290 execptr->end = asptr->lptr;
292 *(asptr->prog) = '\0';
299 void assemble_ds(const CMDLINE *cmdl, PASS pass)
302 if(cmdl->opd->opdc != 1) {
303 setcerr(106, ""); /* operand count mismatch */
306 for(i = 0; i < atoi(cmdl->opd->opdv[0]); i++) {
307 writememory(0x0, (asptr->ptr)++, pass);
318 void assemble_dc(const CMDLINE *cmdl, PASS pass)
321 if(cmdl->opd->opdc == 0 || cmdl->opd->opdc >= OPDSIZE) {
322 setcerr(106, ""); /* operand count mismatch */
325 for(i = 0; i < cmdl->opd->opdc; i++) {
326 writedc(cmdl->opd->opdv[i], pass);
334 * マクロ命令 "IN IBUF,LEN" をアセンブル\n
346 void assemble_in(const CMDLINE *cmdl, PASS pass)
348 char *line = malloc_chk(LINESIZE + 1, "assemble_in.line");
349 if(cmdl->opd->opdc == 0 || cmdl->opd->opdc > 2) {
350 setcerr(106, ""); /* operand count mismatch */
353 assembleline(" PUSH 0,GR1", pass);
354 assembleline(" PUSH 0,GR2", pass);
355 sprintf(line, " LAD GR1,%s", cmdl->opd->opdv[0]);
356 assembleline(line, pass);
357 sprintf(line, " LAD GR2,%s", cmdl->opd->opdv[1]);
358 assembleline(line, pass);
359 assembleline(" SVC 1", pass);
360 assembleline(" POP GR2", pass);
361 assembleline(" POP GR1", pass);
366 * マクロ命令 "OUT OBUF,LEN" をアセンブル\n
381 void assemble_out(const CMDLINE *cmdl, PASS pass)
383 char *line = malloc_chk(LINESIZE + 1, "assemble_out.line");
384 if(cmdl->opd->opdc == 0 || cmdl->opd->opdc > 2) {
385 setcerr(106, ""); /* operand count mismatch */
388 assembleline(" PUSH 0,GR1", pass);
389 assembleline(" PUSH 0,GR2", pass);
390 sprintf(line, " LAD GR1,%s", cmdl->opd->opdv[0]);
391 assembleline(line, pass);
392 sprintf(line, " LAD GR2,%s", cmdl->opd->opdv[1]);
393 assembleline(line, pass);
394 assembleline(" SVC 2", pass);
395 assembleline(" LAD GR1,=#A", pass);
396 assembleline(" LAD GR2,=1", pass);
397 assembleline(" SVC 2", pass);
398 assembleline(" POP GR2", pass);
399 assembleline(" POP GR1", pass);
404 * マクロ命令 "RPUSH" をメモリに書き込む
416 void assemble_rpush(const CMDLINE *cmdl, PASS pass)
419 char *line = malloc_chk(LINESIZE + 1, "assemble_rpush.line");
420 if(cmdl->opd->opdc > 0) {
421 setcerr(106, ""); /* operand count mismatch */
424 for(i = 1; i <= GRSIZE-1; i++) {
425 sprintf(line, " PUSH 0,GR%d", i);
426 assembleline(line, pass);
432 * マクロ命令 "RPOP" をメモリに書き込む\n
445 void assemble_rpop(const CMDLINE *cmdl, PASS pass)
448 char *line = malloc_chk(LINESIZE + 1, "assemble_rpop.line");
449 if(cmdl->opd->opdc > 0) {
450 setcerr(106, ""); /* operand count mismatch */
453 for(i = GRSIZE-1; i >= 1; i--) {
454 sprintf(line, " POP GR%d", i);
455 assembleline(line, pass);
461 * アセンブラ言語CASL IIの命令を処理\n
462 * 命令が表で定義されている場合はtrue、それ以外の場合はfalseを返す\n
464 * 関数へのポインタで呼び出す関数は、Class Reference 参照
467 bool casl2cmd(CMD *cmdtbl, const CMDLINE *cmdl, PASS pass)
471 for(i = 0; *(cmdtbl[i].name) != '\0'; i++) {
472 if(strcmp(cmdl->cmd, cmdtbl[i].name) == 0) {
473 cmdptr = cmdtbl[i].ptr;
474 (*cmdptr)(cmdl, pass);
482 * システムCOMET IIの命令をアセンブル\n
483 * アセンブルに成功した場合はtrue、失敗した場合はfalseを返す
485 bool assemble_comet2cmd(const CMDLINE *cmdl, PASS pass)
487 WORD cmd, r_r1, x_r2, adr;
490 if(cmdl->opd->opdc == 0) {
491 if((cmd = getcmdcode(cmdl->cmd, NONE)) == 0xFFFF) {
492 setcerr(112, cmdl->cmd); /* not command of no operand */
495 writememory(cmd, (asptr->ptr)++, pass);
498 else if((r_r1 = getgr(cmdl->opd->opdv[0], false)) != 0xFFFF) {
500 if(cmdl->opd->opdc == 1) {
501 if((cmd = getcmdcode(cmdl->cmd, R_)) == 0xFFFF) {
502 setcerr(108, cmdl->cmd); /* not command of operand "r" */
506 writememory(cmd, (asptr->ptr)++, pass);
508 /* オペランド数2。第2オペランドは汎用レジスタ */
509 else if(cmdl->opd->opdc == 2 && (x_r2 = getgr(cmdl->opd->opdv[1], false)) != 0xFFFF) {
510 if((cmd = getcmdcode(cmdl->cmd, R1_R2)) == 0xFFFF) {
511 setcerr(109, cmdl->cmd); /* not command of operand "r1,r2" */
514 cmd |= ((r_r1 << 4) | x_r2); /* 第1オペランド、第2オペランドともに汎用レジスタ */
516 writememory(cmd, (asptr->ptr)++, pass);
519 else if(cmdl->opd->opdc == 2 || cmdl->opd->opdc == 3) {
520 if((cmd = getcmdcode(cmdl->cmd, R_ADR_X)) == 0xFFFF) {
521 setcerr(110, cmdl->cmd); /* not command of operand "r,adr[,x]" */
524 cmd |= (r_r1 << 4); /* 第1オペランドは汎用レジスタ */
526 if(cmdl->opd->opdc == 3) { /* 第3オペランドは指標レジスタとして用いる汎用レジスタ */
527 if((x_r2 = getgr(cmdl->opd->opdv[2], true)) == 0xFFFF) {
528 setcerr(125, cmdl->cmd); /* not GR in operand x */
533 adr = getadr(asptr->prog, cmdl->opd->opdv[1], pass); /* 第2オペランドはアドレス */
535 writememory(cmd, (asptr->ptr)++, pass);
536 writememory(adr, (asptr->ptr)++, pass);
538 setcerr(113, cmdl->cmd); /* operand too many in COMET II command */
542 /* オペランド数1または2。第1オペランドはアドレス */
543 else if(cmdl->opd->opdc == 1 || cmdl->opd->opdc == 2) {
544 if((cmd = getcmdcode(cmdl->cmd, ADR_X)) == 0xFFFF) {
545 setcerr(111, cmdl->cmd); /* not command of operand "adr[,x]" */
549 if(cmdl->opd->opdc == 2) { /* 第2オペランドは指標レジスタとして用いる汎用レジスタ */
550 x_r2 = getgr(cmdl->opd->opdv[1], true);
556 /* CALLの場合はプログラムの入口名を表すラベルを取得 */
557 /* CALL以外の命令の場合と、プログラムの入口名を取得できない場合は、 */
558 /* 同一プログラム内のラベルを取得 */
559 if(pass == SECOND && cmd == 0x8000) { /* CALL命令 */
560 adr = getlabel("", cmdl->opd->opdv[0]);
562 if(cmd != 0x8000 || (pass == SECOND && adr == 0xFFFF)) {
563 adr = getadr(asptr->prog, cmdl->opd->opdv[0], pass);
566 writememory(cmd, (asptr->ptr)++, pass);
567 writememory(adr, (asptr->ptr)++, pass);
569 return (cerr->num == 0) ? true : false;
575 bool assembletok(const CMDLINE *cmdl, PASS pass)
578 if(*(cmdl->cmd) == '\0') {
581 /* アセンブラ命令またはマクロ命令の書込 */
582 if(casl2cmd(ascmd, cmdl, pass) == false && casl2cmd(macrocmd, cmdl, pass) == false) {
584 if(assemble_comet2cmd(cmdl, pass) == false) {
586 setcerr(113, cmdl->cmd); /* operand too many in COMET II command */
590 return (cerr->num == 0) ? true : false;
595 * passが1の場合はラベルを登録し、2の場合はラベルからアドレスを読み込む
597 bool assembleline(const char *line, PASS pass)
603 cmdl = linetok(line);
604 stat = (cerr->num == 0) ? true : false;
607 if(pass == FIRST && *(cmdl->label) != '\0') {
608 stat = addlabel(asptr->prog, cmdl->label, asptr->ptr);
612 stat = assembletok(cmdl, pass);
615 if(cmdl->opd != NULL) {
616 for(i = 0; i < cmdl->opd->opdc; i++) {
617 FREE(cmdl->opd->opdv[i]);
628 * 指定された名前のファイルをアセンブル\n
629 * アセンブル完了時はtrue、エラー発生時はfalseを返す
631 bool assemblefile(const char *file, PASS pass)
637 if((fp = fopen(file, "r")) == NULL) {
641 for(line = malloc_chk(LINESIZE + 1, "assemble.line"); fgets(line, LINESIZE, fp); lineno++) {
642 if((pass == FIRST && asmode.src == true) || (pass == SECOND && asmode.asdetail == true)) {
643 printline(stdout, file, lineno, line);
645 if(assembleline(line, pass) == false) {
650 fprintf(stderr, "Assemble error - %d: %s\n", cerr->num, cerr->msg);
651 printline(stderr, file, lineno, line);
655 return (cerr->num == 0) ? true : false;
659 * 引数で指定したファイルにアセンブル結果を書込
661 void outassemble(const char *file)
665 if((fp = fopen(file, "w")) == NULL) {
669 fwrite(sys->memory, sizeof(WORD), execptr->end, fp);