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