ファイル読み込みなどでの失敗時の動作を設定
[YACASL2.git] / src / assemble.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include <ctype.h>
5 #include <assert.h>
6 #include <errno.h>
7
8 #include "assemble.h"
9 #include "cerr.h"
10
11 /**
12  * アセンブルモード: src, label, onlylabel, asdetail, onlyassemble
13  */
14 ASMODE asmode = {false, false, false, false, false};
15
16 /**
17  * アセンブル時の、現在およびリテラルのアドレスとプログラム入口名: ptr, lptr, prog
18  */
19 ASPTR *asptr;
20
21 /**
22  * アセンブルのエラー定義
23  */
24 static CERR cerr_assemble[] = {
25     { 106, "operand mismatch in CASL II command" },
26     { 107, "no label in START" },
27     { 108, "not command of operand \"r\"" },
28     { 109, "not command of operand \"r1,r2\"" },
29     { 110, "not command of operand \"r,adr[,x]\"" },
30     { 111, "not command of operand \"adr[,x]\"" },
31     { 112, "not command of no operand" },
32     { 113, "operand too many in COMET II command" },
33     { 119, "out of COMET II memory" },
34     { 120, "GR0 in operand x" },
35     { 122, "cannot create hash table" },
36     { 124, "more than one character in literal" },
37     { 125, "not GR in operand x" },
38 };
39
40 void printline(FILE *stream, const char *filename, int lineno, char *line);
41
42 WORD getadr(const char *prog, const char *str, PASS pass);
43
44 WORD getgr(const char *str, bool is_x);
45
46 WORD getliteral(const char *str, PASS pass);
47
48 void writememory(WORD word, WORD adr, PASS pass);
49
50 void writestr(const char *str, bool literal, PASS pass);
51
52 void writedc(const char *str, PASS pass);
53
54 void assemble_start(const CMDLINE *cmdl, PASS pass);
55
56 void assemble_ds(const CMDLINE *cmdl, PASS pass);
57
58 void assemble_end(const CMDLINE *cmdl, PASS pass);
59
60 void assemble_dc(const CMDLINE *cmdl, PASS pass);
61
62 void assemble_in(const CMDLINE *cmdl, PASS pass);
63
64 void assemble_out(const CMDLINE *cmdl, PASS pass);
65
66 void assemble_rpush(const CMDLINE *cmdl, PASS pass);
67
68 void assemble_rpop(const CMDLINE *cmdl, PASS pass);
69
70 bool casl2cmd(CMD *cmdtbl, const CMDLINE *cmdl, PASS pass);
71
72 bool assemble_comet2cmd(const CMDLINE *cmdl, PASS pass);
73
74 bool assembletok(const CMDLINE *cmdl, PASS pass);
75
76 bool assembleline(const char *line, PASS pass);
77
78 /**
79  * アセンブラ命令
80  */
81 static CMD ascmd[] = {
82     { "START", assemble_start },
83     { "END", assemble_end },
84     { "DS", assemble_ds },
85     { "DC", assemble_dc },
86     { "", NULL }
87 };
88
89 /**
90  * マクロ命令
91  */
92 static CMD macrocmd[] = {
93     { "OUT", assemble_out },
94     { "IN", assemble_in },
95     { "RPUSH", assemble_rpush },
96     { "RPOP", assemble_rpop },
97     { "", NULL }
98 };
99
100 /**
101  * アセンブルのエラーをエラーリストに追加
102  */
103 void addcerrlist_assemble()
104 {
105     addcerrlist_tok();
106     addcerrlist_word();
107     addcerrlist_label();
108     addcerrlist(ARRAYSIZE(cerr_assemble), cerr_assemble);
109 }
110
111 /**
112  * ファイルストリームの現在行を番号付きで表示する
113  */
114 void printline(FILE *stream, const char *filename, int lineno, char *line)
115 {
116     fprintf(stream, "%s:%5d:%s", filename, lineno, line);
117 }
118
119 /**
120  * アドレスを返す\n
121  * アドレスには、リテラル/10進定数/16進定数/アドレス定数が含まれる
122  */
123 WORD getadr(const char *prog, const char *str, PASS pass)
124 {
125     WORD adr = 0x0;
126
127     if(*str == '=') {
128         adr = getliteral(str, pass);
129     } else if(isdigit(*str) || *str == '-' || *str == '#') {
130         adr = nh2word(str);
131     } else {
132         if(pass == SECOND) {
133             if((adr = getlabel(prog, str)) == 0xFFFF) {
134                 setcerr(103, str);    /* label not found */
135             }
136         }
137     }
138     return adr;
139 }
140
141 /**
142  * 汎用レジスタを表す文字列 "GR[0-7]" から、レジスタ番号 [0-7] をWORD値で返す\n
143  * 文字列が汎用レジスタを表さない場合は、0xFFFFを返す\n
144  * is_xがtrueの場合は指標レジスタ。GR0が指定された場合は、COMET IIの仕様によりエラー発生
145  */
146 WORD getgr(const char *str, bool is_x)
147 {
148     WORD r;
149
150     /*  "GR[0-7]" 以外の文字列では、0xFFFFを返して終了 */
151     if(!(strlen(str) == 3 && strncmp(str, "GR", 2) == 0 &&
152          (*(str+2) >= '0' && *(str+2) <= '0' + (GRSIZE - 1))))
153     {
154         return 0xFFFF;
155     }
156     r = (WORD)(*(str+2) - '0');
157     /* GR0は指標レジスタとして用いることができない */
158     if(is_x == true && r == 0x0) {
159         setcerr(120, "");    /* GR0 in operand x */
160         return 0x0;
161     }
162     return r;
163 }
164
165 /**
166  * 定数の前に等号(=)をつけて記述されるリテラルを返す\n
167  * リテラルには、10進定数/16進定数/文字定数が含まれる
168  */
169 WORD getliteral(const char *str, PASS pass)
170 {
171     assert(*str == '=');
172     WORD adr = asptr->lptr;
173
174     if(*(++str) == '\'') {    /* 文字定数 */
175         writestr(str, true, pass);
176     } else {
177         writememory(nh2word(str), (asptr->lptr)++, pass);
178     }
179     return adr;
180 }
181
182 /**
183  * アドレス値をメモリに書き込む
184  */
185 void writememory(WORD word, WORD adr, PASS pass)
186 {
187     /* メモリオーバーの場合、エラー発生 */
188     if(adr >= sys->memsize) {
189         setcerr(119, word2n(adr));    /* out of COMET II memory */
190         return;
191     }
192     (sys->memory)[adr] = word;
193     if(pass == SECOND && asmode.asdetail == true) {
194         fprintf(stdout, "\t#%04X\t#%04X\n", adr, word);
195     }
196 }
197
198 /**
199  * 文字をメモリに書き込む
200  */
201 void writestr(const char *str, bool literal, PASS pass)
202 {
203     assert(*str == '\'');
204     const char *p = str + 1;
205     bool lw = false;
206
207     for(; ;) {
208         /* 閉じ「'」がないまま文字列が終了した場合 */
209         if(*p == '\0') {
210             setcerr(123, str);    /* unclosed quote */
211             break;
212         }
213         /* 「'」の場合、次の文字が「'」でない場合は正常終了 */
214         if(*p == '\'' && *(++p) != '\'') {
215             break;
216         } else if(literal == true && lw == true) {
217             setcerr(124, str);    /* more than one character in literal */
218             break;
219         }
220         /*リテラルの場合はリテラル領域に書込 */
221         if(literal == true) {
222             writememory(*(p++), (asptr->lptr)++, pass);
223             lw = true;
224         } else {
225             writememory(*(p++), (asptr->ptr)++, pass);
226         }
227     }
228 }
229
230 /**
231  * DC命令を書込
232  */
233 void writedc(const char *str, PASS pass)
234 {
235     WORD adr = 0x0;
236
237     if(*str == '\'') {
238         writestr(str, false, pass);
239     } else {
240         if(*str == '#' || isdigit(*str) || *str == '-') {
241             adr = nh2word(str);
242         } else {
243             if(pass == SECOND && (adr = getlabel(asptr->prog, str)) == 0xFFFF) {
244                 setcerr(103, str);    /* label not found */
245             }
246         }
247         writememory(adr, (asptr->ptr)++, pass);
248     }
249 }
250
251 /**
252  * アセンブラ命令STARTの処理
253  * \relates casl2cmd
254  */
255 void assemble_start(const CMDLINE *cmdl, PASS pass)
256 {
257     if(cmdl->opd->opdc > 1) {
258         setcerr(106, "");    /* operand count mismatch */
259         return;
260     }
261     if(*(cmdl->label) == '\0') {
262         setcerr(107, "");    /* no label in START */
263         return;
264     }
265     /* プログラム名の設定 */
266     strcpy(asptr->prog, cmdl->label);
267     /* オペランドがある場合、実行開始アドレスを設定 */
268     if(pass == SECOND && cmdl->opd->opdv[0] != NULL) {
269         if((execptr->start = getlabel(asptr->prog, cmdl->opd->opdv[0])) == 0xFFFF) {
270             setcerr(103, cmdl->opd->opdv[0]);    /* label not found */
271         }
272     }
273 }
274
275 /**
276  * アセンブラ命令ENDの処理
277  * \relates casl2cmd
278  */
279 void assemble_end(const CMDLINE *cmdl, PASS pass)
280 {
281     if(cmdl->opd->opdc > 0) {
282         setcerr(106, "");    /* operand count mismatch */
283         return;
284     }
285     /* 1回目のアセンブルの場合は、リテラル領域開始アドレスを設定 */
286     if(pass == FIRST) {
287         asptr->lptr = asptr->ptr;
288     }
289     /* 2回目のアセンブルの場合は、リテラル領域終了アドレスを実行終了アドレスとして設定 */
290     else if(pass == SECOND) {
291         execptr->end = asptr->lptr;
292     }
293     *(asptr->prog) = '\0';
294 }
295
296 /**
297  * アセンブラ命令DSの処理
298  * \relates casl2cmd
299  */
300 void assemble_ds(const CMDLINE *cmdl, PASS pass)
301 {
302     int i;
303     if(cmdl->opd->opdc != 1) {
304         setcerr(106, "");    /* operand count mismatch */
305         return;
306     }
307     for(i = 0; i < atoi(cmdl->opd->opdv[0]); i++) {
308         writememory(0x0, (asptr->ptr)++, pass);
309         if(cerr->num > 0) {
310             break;
311         }
312     }
313 }
314
315 /**
316  * アセンブラ命令DCの処理
317  * \relates casl2cmd
318  */
319 void assemble_dc(const CMDLINE *cmdl, PASS pass)
320 {
321     int i;
322     if(cmdl->opd->opdc == 0 || cmdl->opd->opdc >= OPDSIZE) {
323         setcerr(106, "");    /* operand count mismatch */
324         return;
325     }
326     for(i = 0; i < cmdl->opd->opdc; i++) {
327         writedc(cmdl->opd->opdv[i], pass);
328         if(cerr->num > 0) {
329             break;
330         }
331     }
332 }
333
334 /**
335  * マクロ命令 "IN IBUF,LEN" をアセンブル\n
336  * \code
337  *      PUSH 0,GR1
338  *      PUSH 0,GR2
339  *      LAD GR1,IBUF
340  *      LAD GR2,LEN
341  *      SVC 1
342  *      POP GR2
343  *      POP GR1
344  * \endcode
345  * \relates casl2cmd
346  */
347 void assemble_in(const CMDLINE *cmdl, PASS pass)
348 {
349     char *line = malloc_chk(LINESIZE + 1, "assemble_in.line");
350     if(cmdl->opd->opdc == 0 || cmdl->opd->opdc > 2) {
351         setcerr(106, "");    /* operand count mismatch */
352         return;
353     }
354     assembleline("    PUSH 0,GR1", pass);
355     assembleline("    PUSH 0,GR2", pass);
356     sprintf(line, "    LAD GR1,%s", cmdl->opd->opdv[0]);
357     assembleline(line, pass);
358     sprintf(line, "    LAD GR2,%s", cmdl->opd->opdv[1]);
359     assembleline(line, pass);
360     assembleline("    SVC 1", pass);
361     assembleline("    POP GR2", pass);
362     assembleline("    POP GR1", pass);
363     FREE(line);
364 }
365
366 /**
367  * マクロ命令 "OUT OBUF,LEN" をアセンブル\n
368  * \code
369  *      PUSH 0,GR1
370  *      PUSH 0,GR2
371  *      LAD GR1,OBUF
372  *      LAD GR2,LEN
373  *      SVC 2
374  *      LAD GR1,=#A
375  *      LAD GR2,=1
376  *      SVC 2
377  *      POP GR2
378  *      POP GR1
379  * \endcode
380  * \relates casl2cmd
381  */
382 void assemble_out(const CMDLINE *cmdl, PASS pass)
383 {
384     char *line = malloc_chk(LINESIZE + 1, "assemble_out.line");
385     if(cmdl->opd->opdc == 0 || cmdl->opd->opdc > 2) {
386         setcerr(106, "");    /* operand count mismatch */
387         return;
388     }
389     assembleline("    PUSH 0,GR1", pass);
390     assembleline("    PUSH 0,GR2", pass);
391     sprintf(line, "    LAD GR1,%s", cmdl->opd->opdv[0]);
392     assembleline(line, pass);
393     sprintf(line, "    LAD GR2,%s", cmdl->opd->opdv[1]);
394     assembleline(line, pass);
395     assembleline("    SVC 2", pass);
396     assembleline("    LAD GR1,=#A", pass);
397     assembleline("    LAD GR2,=1", pass);
398     assembleline("    SVC 2", pass);
399     assembleline("    POP GR2", pass);
400     assembleline("    POP GR1", pass);
401     FREE(line);
402 }
403
404 /**
405  * マクロ命令 "RPUSH" をメモリに書き込む
406  * \code
407  *       PUSH 0,GR1
408  *       PUSH 0,GR2
409  *       PUSH 0,GR3
410  *       PUSH 0,GR4
411  *       PUSH 0,GR5
412  *       PUSH 0,GR6
413  *       PUSH 0,GR7
414  * \endcode
415  * \relates casl2cmd
416  */
417 void assemble_rpush(const CMDLINE *cmdl, PASS pass)
418 {
419     int i;
420     char *line = malloc_chk(LINESIZE + 1, "assemble_rpush.line");
421     if(cmdl->opd->opdc > 0) {
422         setcerr(106, "");    /* operand count mismatch */
423         return;
424     }
425     for(i = 1; i <= GRSIZE-1; i++) {
426         sprintf(line, "    PUSH 0,GR%d", i);
427         assembleline(line, pass);
428     }
429     FREE(line);
430 }
431
432 /**
433  * マクロ命令 "RPOP" をメモリに書き込む\n
434  * \code
435  *      POP GR7
436  *      POP GR6
437  *      POP GR5
438  *      POP GR4
439  *      POP GR3
440  *      POP GR3
441  *      POP GR2
442  *      POP GR1
443  * \endcode
444  * \relates casl2cmd
445  */
446 void assemble_rpop(const CMDLINE *cmdl, PASS pass)
447 {
448     int i;
449     char *line = malloc_chk(LINESIZE + 1, "assemble_rpop.line");
450     if(cmdl->opd->opdc > 0) {
451         setcerr(106, "");    /* operand count mismatch */
452         return;
453     }
454     for(i = GRSIZE-1; i >= 1; i--) {
455         sprintf(line, "    POP GR%d", i);
456         assembleline(line, pass);
457     }
458     FREE(line);
459 }
460
461 /**
462  * アセンブラ言語CASL IIの命令を処理\n
463  * 命令が表で定義されている場合はtrue、それ以外の場合はfalseを返す\n
464  * エラー発生時は、cerrを設定\n
465  * 関数へのポインタで呼び出す関数は、Class Reference 参照
466  * \class casl2cmd
467  */
468 bool casl2cmd(CMD *cmdtbl, const CMDLINE *cmdl, PASS pass)
469 {
470     int i;
471     void (*cmdptr)();
472     for(i = 0; *(cmdtbl[i].name) != '\0'; i++) {
473         if(strcmp(cmdl->cmd, cmdtbl[i].name) == 0) {
474             cmdptr = cmdtbl[i].ptr;
475             (*cmdptr)(cmdl, pass);
476             return true;
477         }
478     }
479     return false;
480 }
481
482 /**
483  * システムCOMET IIの命令をアセンブル\n
484  * アセンブルに成功した場合はtrue、失敗した場合はfalseを返す
485  */
486 bool assemble_comet2cmd(const CMDLINE *cmdl, PASS pass)
487 {
488     WORD cmd, r_r1, x_r2, adr;
489
490     /* オペランドなし */
491     if(cmdl->opd->opdc == 0) {
492         if((cmd = getcmdcode(cmdl->cmd, NONE)) == 0xFFFF) {
493             setcerr(112, cmdl->cmd);    /* not command of no operand */
494             return false;
495         }
496         writememory(cmd, (asptr->ptr)++, pass);
497     }
498     /* 第1オペランドは汎用レジスタ */
499     else if((r_r1 = getgr(cmdl->opd->opdv[0], false)) != 0xFFFF) {
500         /* オペランド数1 */
501         if(cmdl->opd->opdc == 1) {
502             if((cmd = getcmdcode(cmdl->cmd, R_)) == 0xFFFF) {
503                 setcerr(108, cmdl->cmd);    /* not command of operand "r" */
504                 return false;
505             }
506             cmd |= (r_r1 << 4);
507             writememory(cmd, (asptr->ptr)++, pass);
508         }
509         /* オペランド数2。第2オペランドは汎用レジスタ */
510         else if(cmdl->opd->opdc == 2 && (x_r2 = getgr(cmdl->opd->opdv[1], false)) != 0xFFFF) {
511             if((cmd = getcmdcode(cmdl->cmd, R1_R2)) == 0xFFFF) {
512                 setcerr(109, cmdl->cmd);    /* not command of operand "r1,r2" */
513                 return false;
514             }
515             cmd |= ((r_r1 << 4) | x_r2);               /* 第1オペランド、第2オペランドともに汎用レジスタ */
516             /* メモリへの書き込み */
517             writememory(cmd, (asptr->ptr)++, pass);
518         }
519         /* オペランド数2または3 */
520         else if(cmdl->opd->opdc == 2 || cmdl->opd->opdc == 3) {
521             if((cmd = getcmdcode(cmdl->cmd, R_ADR_X)) == 0xFFFF) {
522                 setcerr(110, cmdl->cmd);    /* not command of operand "r,adr[,x]" */
523                 return false;
524             }
525             cmd |= (r_r1 << 4);                    /* 第1オペランドは汎用レジスタ */
526             /* オペランド数3の場合 */
527             if(cmdl->opd->opdc == 3) {             /* 第3オペランドは指標レジスタとして用いる汎用レジスタ */
528                 if((x_r2 = getgr(cmdl->opd->opdv[2], true)) == 0xFFFF) {
529                     setcerr(125, cmdl->cmd);    /* not GR in operand x */
530                     return false;
531                 }
532                 cmd |= x_r2;
533             }
534             adr = getadr(asptr->prog, cmdl->opd->opdv[1], pass); /* 第2オペランドはアドレス */
535             /* メモリへの書き込み */
536             writememory(cmd, (asptr->ptr)++, pass);
537             writememory(adr, (asptr->ptr)++, pass);
538         } else {
539             setcerr(113, cmdl->cmd);    /* operand too many in COMET II command */
540             return false;
541         }
542     }
543     /* オペランド数1または2。第1オペランドはアドレス */
544     else if(cmdl->opd->opdc == 1 || cmdl->opd->opdc == 2) {
545         if((cmd = getcmdcode(cmdl->cmd, ADR_X)) == 0xFFFF) {
546             setcerr(111, cmdl->cmd);    /* not command of operand "adr[,x]" */
547             return false;
548         }
549         /* オペランド数2の場合 */
550         if(cmdl->opd->opdc == 2) {             /* 第2オペランドは指標レジスタとして用いる汎用レジスタ */
551             x_r2 = getgr(cmdl->opd->opdv[1], true);
552             if(cerr->num > 0) {
553                 return false;
554             }
555             cmd |= x_r2;
556         }
557         /* CALLの場合はプログラムの入口名を表すラベルを取得 */
558         /* CALL以外の命令の場合と、プログラムの入口名を取得できない場合は、 */
559         /* 同一プログラム内のラベルを取得 */
560         if(pass == SECOND && cmd == 0x8000) {        /* CALL命令 */
561             adr = getlabel("", cmdl->opd->opdv[0]);
562         }
563         if(cmd != 0x8000 || (pass == SECOND && adr == 0xFFFF)) {
564             adr = getadr(asptr->prog, cmdl->opd->opdv[0], pass);
565         }
566         /* メモリへの書込 */
567         writememory(cmd, (asptr->ptr)++, pass);
568         writememory(adr, (asptr->ptr)++, pass);
569     }
570     return (cerr->num == 0) ? true : false;
571 }
572
573 /**
574  * トークンをアセンブル
575  */
576 bool assembletok(const CMDLINE *cmdl, PASS pass)
577 {
578     /* 命令がない場合 */
579     if(*(cmdl->cmd) == '\0') {
580         return true;
581     }
582     /* アセンブラ命令またはマクロ命令の書込 */
583     if(casl2cmd(ascmd, cmdl, pass) == false && casl2cmd(macrocmd, cmdl, pass) == false) {
584         /* 機械語命令の書込 */
585         if(assemble_comet2cmd(cmdl, pass) == false) {
586             if(cerr->num == 0) {
587                 setcerr(113, cmdl->cmd);    /* operand too many in COMET II command */
588             }
589         }
590     }
591     return (cerr->num == 0) ? true : false;
592 }
593
594 /**
595  * 1行をアセンブル\n
596  * passが1の場合はラベルを登録し、2の場合はラベルからアドレスを読み込む
597  */
598 bool assembleline(const char *line, PASS pass)
599 {
600     CMDLINE *cmdl;
601     bool stat = true;
602     int i;
603
604     cmdl = linetok(line);
605     stat = (cerr->num == 0) ? true : false;
606     if(cmdl != NULL) {
607         if(stat == true) {
608             if(pass == FIRST && *(cmdl->label) != '\0') {
609                 stat = addlabel(asptr->prog, cmdl->label, asptr->ptr);
610             }
611         }
612         if(stat == true) {
613             stat = assembletok(cmdl, pass);
614         }
615         FREE(cmdl->label);
616         if(cmdl->opd != NULL) {
617             for(i = 0; i < cmdl->opd->opdc; i++) {
618                 FREE(cmdl->opd->opdv[i]);
619             }
620         }
621         FREE(cmdl->opd);
622         FREE(cmdl->cmd);
623     }
624     FREE(cmdl);
625     return stat;
626 }
627
628 /**
629  * 指定された名前のファイルをアセンブル\n
630  * アセンブル完了時はtrue、エラー発生時はfalseを返す
631  */
632 bool assemblefile(const char *file, PASS pass)
633 {
634     int lineno = 1;
635     char *line;
636     FILE *fp;
637
638     if((fp = fopen(file, "r")) == NULL) {
639         cerr->num = errno;
640         perror(file);
641         return false;
642     }
643     for(line = malloc_chk(LINESIZE + 1, "assemble.line"); fgets(line, LINESIZE, fp); lineno++) {
644         if((pass == FIRST && asmode.src == true) || (pass == SECOND && asmode.asdetail == true)) {
645             printline(stdout, file, lineno, line);
646         }
647         if(assembleline(line, pass) == false) {
648             break;
649         }
650     }
651     if(cerr->num > 0) {
652         fprintf(stderr, "Assemble error - %d: %s\n", cerr->num, cerr->msg);
653         printline(stderr, file, lineno, line);
654     }
655     FREE(line);
656     fclose(fp);
657     return (cerr->num == 0) ? true : false;
658 }
659
660 /**
661  * 引数で指定したファイルにアセンブル結果を書込
662  */
663 void outassemble(const char *file)
664 {
665     FILE *fp;
666
667     if((fp = fopen(file, "w")) == NULL) {
668         perror(file);
669         exit(1);
670     }
671     fwrite(sys->memory, sizeof(WORD), execptr->end, fp);
672     fclose(fp);
673 }