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