ee8449d0b33cfb6fee5048529994b2f56a63c510
[YACASL2.git] / src / assemble.c
1 #include "casl2.h"
2 #include "assemble.h"
3
4 /* アセンブルモード: src, label, onlylabel, asdetail, onlyassemble */
5 ASMODE asmode = {false, false, false, false, false};
6
7 /* 値を格納するポインタ */
8 WORD ptr;
9
10 /* リテラル(=付きの値)を格納するポインタ */
11 WORD lptr;
12
13 /* 他のプログラムで参照する入口名 */
14 char *prog;
15
16
17 /* アセンブルのエラー定義 */
18 CERRARRAY cerr_assemble[] = {
19     { 101, "label already defined" },
20     { 102, "label table is full" },
21     { 103, "label not found" },
22     { 104, "label length is too long" },
23     { 105, "no command in the line" },
24     { 106, "operand mismatch in assemble 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     { 117, "operand too many in DC" },
33     { 118, "operand length too long" },
34     { 119, "out of COMET II memory" },
35     { 120, "GR0 in operand x" },
36     { 121, "cannot get operand token" },
37     { 122, "cannot create hash table" },
38     { 123, "unclosed quote" },
39     { 124, "more than one character in literal" },
40     { 125, "not GR in operand x" },
41 };
42
43 bool addcerrlist_assemble()
44 {
45     return addcerrlist(ARRAYSIZE(cerr_assemble), cerr_assemble);
46 }
47
48 /* 汎用レジスタを表す文字列「GR[0-7]」から、レジスタ番号[0-7]をWORD値で返す */
49 /* 文字列が汎用レジスタを表さない場合は、0xFFFFを返す */
50 /* is_xがtrueの場合は指標レジスタ。GR0は、COMET IIの仕様により、エラー発生 */
51 WORD getgr(const char *str, bool is_x)
52 {
53     assert(str != NULL);
54     WORD r;
55     /* 「GR[0-7]」以外の文字列では、0xFFFFを返して終了 */
56     if(!(strlen(str) == 3 && strncmp(str, "GR", 2) == 0 &&
57          (*(str+2) >= '0' && *(str+2) <= '7')))
58     {
59         return 0xFFFF;
60     }
61     r = (WORD)(*(str+2) - '0');
62     /* GR0は指標レジスタとして用いることができない */
63     if(is_x == true && r == 0x0) {
64         setcerr(120, NULL);    /* GR0 in operand x */
65         return 0x0;
66     }
67     return r;
68 }
69
70 /* アドレスを返す */
71 /* アドレスには、リテラル/10進定数/16進定数/アドレス定数が含まれる */
72 WORD getadr(const char *prog, const char *str, PASS pass)
73 {
74     WORD adr = 0x0;
75     if(*str == '=') {
76         adr = getliteral(str, pass);
77     } else if((*str == '#') || isdigit(*str) || *str == '-') {
78         adr = nh2word(str);
79     } else {
80         if(pass == SECOND && (adr = getlabel(prog, str)) == 0xFFFF) {
81             if(prog != NULL) {
82                 setcerr(103, str);    /* label not found */
83             }
84         }
85     }
86     return adr;
87 }
88
89 /* WORD値wordをアドレスadrに書込 */
90 /* 書込に成功した場合はtrue、失敗した場合はfalseを返す */
91 bool writememory(WORD word, WORD adr, PASS pass)
92 {
93     bool status = false;
94
95     /* COMET IIメモリオーバーの場合 */
96     if(adr >= memsize) {
97         setcerr(119, word2n(adr));    /* out of COMET II memory */
98     }
99     if(cerrno == 0) {
100         memory[adr] = word;
101         if(pass == SECOND && asmode.asdetail == true) {
102             fprintf(stdout, "\t#%04X\t#%04X\n", adr, word);
103         }
104         status = true;
105     }
106     return status;
107 }
108
109 /* 定数の前に等号(=)をつけて記述されるリテラルを返す */
110 /* リテラルには、10進定数/16進定数/文字定数が含まれる */
111 WORD getliteral(const char *str, PASS pass)
112 {
113     WORD adr = lptr;
114     assert(*str == '=');
115     if(*(++str) == '\'') {    /* 文字定数 */
116         writestr(str, true, pass);
117     } else {
118         writememory(nh2word(str), lptr++, pass);
119     }
120     return adr;
121 }
122
123 /* ' 'で囲まれた文字定数をメモリに書込 */
124 /* DC命令とリテラルで使い、リテラルの場合はリテラル領域に書込 */
125 void writestr(const char *str, bool literal, PASS pass)
126 {
127     assert(cerrno == 0 && *str == '\'');
128     const char *p = str + 1;
129     bool lw = false;
130
131     for(; ;) {
132         /* 閉じ「'」がないまま文字列が終了した場合 */
133         if(*p == '\0') {
134             setcerr(123, str);    /* unclosed quote */
135             break;
136         }
137         /* 「'」の場合、次の文字が「'」でない場合は正常終了 */
138         if(*p == '\'' && *(++p) != '\'') {
139             break;
140         } else if(literal == true && lw == true) {
141             setcerr(124, str);    /* more than one character in literal */
142             break;
143         }
144         /*リテラルの場合はリテラル領域に書込 */
145         if(literal == true) {
146             writememory(*(p++), lptr++, pass);
147             lw = true;
148         } else {
149             writememory(*(p++), ptr++, pass);
150         }
151     }
152 }
153
154 /* アセンブラ命令DCをメモリに書込 */
155 void writeDC(const char *str, PASS pass)
156 {
157     WORD adr = 0x0;
158     if(*str == '\'') {
159         writestr(str, false, pass);
160     } else {
161         if(*str == '#' || isdigit(*str) || *str == '-') {
162             adr = nh2word(str);
163         } else {
164             if(pass == SECOND && (adr = getlabel(prog, str)) == 0xFFFF) {
165                 setcerr(103, str);    /* label not found */
166             }
167         }
168         writememory(adr, ptr++, pass);
169     }
170 }
171
172 /* 命令がアセンブラ命令の場合は処理を実行 */
173 /* 実行に成功した場合はtrue、それ以外の場合はfalseを返す */
174 bool assemblecmd(const CMDLINE *cmdl, PASS pass)
175 {
176     int i = 0;
177     CASLCMD cmd = 0;
178     bool status = false;
179     CMDARRAY ascmd[] = {
180         { START, 0, 1, "START" },
181         { END, 0, 0, "END" },
182         { DC, 1, OPDSIZE, "DC" },
183         { DS, 1, 1, "DS" },
184         { 0, 0, 0, NULL }
185     };
186
187     do {
188         if(strcmp(cmdl->cmd, ascmd[i].cmd) == 0) {
189             if(cmdl->opd->opdc < ascmd[i].opdc_min || cmdl->opd->opdc > ascmd[i].opdc_max) {
190                 setcerr(106, NULL);    /* operand count mismatch */
191                 return false;
192             }
193             cmd = ascmd[i].cmdid;
194             break;
195         }
196     } while(ascmd[++i].cmdid != 0);
197     /* アセンブラ命令 */
198     switch(cmd)
199     {
200     case START:
201         if(cmdl->label == NULL) {
202             setcerr(107, NULL);    /* no label in START */
203             return false;
204         }
205         /* プログラム名の設定 */
206         prog = strdup(cmdl->label);
207         /* オペランドがある場合、実行開始番地を設定 */
208         if(pass == SECOND && cmdl->opd->opdc == 1) {
209             if((startptr = getlabel(prog, cmdl->opd->opdv[0])) == 0xFFFF) {
210                 setcerr(103, cmdl->opd->opdv[0]);    /* label not found */
211             }
212         }
213         status = true;
214         break;
215     case END:
216         /* リテラル領域の設定 */
217         if(pass == FIRST) {
218             lptr = ptr;
219         }
220         /* 実行終了番地と次のプログラムの実行開始番地を設定 */
221         else if(pass == SECOND) {
222             endptr = lptr;
223         }
224         prog = NULL;
225         status = true;
226         break;
227     case DS:
228         for(i = 0; i < atoi(cmdl->opd->opdv[0]); i++) {
229             writememory(0x0, ptr++, pass);
230             if(cerrno > 0) {
231                 return false;
232             }
233         }
234         status = true;
235         break;
236     case DC:
237         for(i = 0; i < cmdl->opd->opdc; i++) {
238             writeDC(cmdl->opd->opdv[i], pass);
239             if(cerrno > 0) {
240                 return false;
241             }
242         }
243         status = true;
244         break;
245     default:
246         return false;
247     }
248     if(cerrno > 0) {
249         status = false;
250     }
251     return status;
252 }
253
254 /* 命令がマクロ命令の場合はメモリに書込 */
255 /* 書込に成功した場合はtrue、それ以外の場合はfalseを返す */
256 bool macrocmd(const CMDLINE *cmdl, PASS pass)
257 {
258     int i = 0;
259     CASLCMD cmd;
260     bool status = false;
261     CMDARRAY macrocmd[] = {
262         { IN, 2, 2, "IN" },
263         { OUT, 2, 2, "OUT" },
264         { RPUSH, 0, 0, "RPUSH" },
265         { RPOP, 0, 0, "RPOP" },
266         { 0, 0, 0, NULL }
267     };
268
269     do {
270         if(strcmp(cmdl->cmd, macrocmd[i].cmd) == 0) {
271             if(cmdl->opd->opdc < macrocmd[i].opdc_min || cmdl->opd->opdc > macrocmd[i].opdc_max) {
272                 setcerr(106, NULL);    /* operand count mismatch */
273                 return false;
274             }
275             cmd = macrocmd[i].cmdid;
276             break;
277         }
278     } while(macrocmd[++i].cmdid != 0);
279     switch(cmd)
280     {
281     case IN:
282         status = writeIN(cmdl->opd->opdv[0], cmdl->opd->opdv[1], pass);
283         break;
284     case OUT:
285         status = writeOUT(cmdl->opd->opdv[0], cmdl->opd->opdv[1], pass);
286         break;
287     case RPUSH:
288         status = writeRPUSH(pass);
289         break;
290     case RPOP:
291         status = writeRPOP(pass);
292         break;
293     default:
294         return false;
295     }
296     return status;
297 }
298
299 /* 機械語命令の書込 */
300 /* 書込に成功した場合はtrue、それ以外の場合はfalseを返す */
301 bool cometcmd(const CMDLINE *cmdl, PASS pass)
302 {
303     WORD cmd, adr, r1, r2, x;
304     bool status = false;
305
306     /* オペランドなし */
307     if(cmdl->opd->opdc == 0) {
308         if((cmd = getcmdcode(cmdl->cmd, NONE)) == 0xFFFF) {
309             setcerr(112, cmdl->cmd);    /* not command of no operand */
310             return false;
311         }
312         if(writememory(cmd, ptr++, pass) == true) {
313             status = true;
314         }
315     }
316     /* 第1オペランドは汎用レジスタ */
317     else if((r1 = getgr(cmdl->opd->opdv[0], false)) != 0xFFFF) {
318         /* オペランド数1 */
319         if(cmdl->opd->opdc == 1) {
320             if((cmd = getcmdcode(cmdl->cmd, R_)) == 0xFFFF) {
321                 setcerr(108, cmdl->cmd);    /* not command of operand "r" */
322                 return false;
323             }
324             cmd |= (r1 << 4);
325             if(writememory(cmd, ptr++, pass) == true) {
326                 status = true;
327             }
328         }
329         /* オペランド数2。第2オペランドは汎用レジスタ */
330         else if(cmdl->opd->opdc == 2 && (r2 = getgr(cmdl->opd->opdv[1], false)) != 0xFFFF) {
331             if((cmd = getcmdcode(cmdl->cmd, R1_R2)) == 0xFFFF) {
332                 setcerr(109, cmdl->cmd);    /* not command of operand "r1,r2" */
333                 return false;
334             }
335             cmd |= ((r1 << 4) | r2);
336             if(cerrno == 0 && writememory(cmd, ptr++, pass) == true) {
337                 status = true;
338             }
339         }
340         /* オペランド数2〜3。第2オペランドはアドレス、
341            第3オペランドは指標レジスタとして用いる汎用レジスタ */
342         else if(cmdl->opd->opdc == 2 || cmdl->opd->opdc == 3) {
343             if((cmd = getcmdcode(cmdl->cmd, R_ADR_X_)) == 0xFFFF &&
344                (cmd = getcmdcode(cmdl->cmd, R_ADR_X)) == 0xFFFF)
345             {
346                 setcerr(110, cmdl->cmd);    /* not command of operand "r,adr[,x]" */
347                 return false;
348             }
349             cmd |= (r1 << 4);
350             /* オペランド数3 */
351             if(cmdl->opd->opdc == 3) {
352                 if((x = getgr(cmdl->opd->opdv[2], true)) == 0xFFFF) {
353                     setcerr(125, cmdl->cmd);    /* not GR in operand x */
354                     return false;
355                 }
356                 cmd |= x;
357             }
358             adr = getadr(prog, cmdl->opd->opdv[1], pass);
359             writememory(cmd, ptr++, pass);
360             writememory(adr, ptr++, pass);
361             if(cerrno == 0) {
362                 status = true;
363             }
364         } else {
365             setcerr(113, cmdl->cmd);    /* operand too many in COMET II command */
366             return false;
367         }
368     }
369     /* オペランド数1〜2。第1オペランドはアドレス */
370     else if(cmdl->opd->opdc == 1 || cmdl->opd->opdc == 2) {
371         if((cmd = getcmdcode(cmdl->cmd, ADR_X)) == 0xFFFF) {
372             setcerr(111, cmdl->cmd);    /* not command of operand "adr[,x]" */
373             return false;
374         }
375         /* オペランド数2の場合、第2オペランドは指標レジスタとして用いる汎用レジスタ */
376         if(cmdl->opd->opdc == 2) {
377             x = getgr(cmdl->opd->opdv[1], true);
378             if(cerrno > 0) {
379                 return false;
380             }
381             cmd |= x;
382         }
383         /* CALLの場合はプログラムの入口名を表すラベルを取得 */
384         /* CALL以外の命令の場合と、プログラムの入口名を取得できない場合は、 */
385         /* 同一プログラム内のラベルを取得 */
386         if(pass == SECOND && cmd == 0x8000) {        /* CALL命令 */
387             adr = getlabel(NULL, cmdl->opd->opdv[0]);
388         }
389         if(cmd != 0x8000 || (pass == SECOND && adr == 0xFFFF)) {
390             adr = getadr(prog, cmdl->opd->opdv[0], pass);
391         }
392         writememory(cmd, ptr++, pass);
393         writememory(adr, ptr++, pass);
394         if(cerrno == 0) {
395             status = true;
396         }
397     }
398     return status;
399 }
400
401 /* 命令行を1行アセンブルする */
402 bool assembleline(const CMDLINE *cmdl, PASS pass)
403 {
404     bool status = false;
405     /* 命令がない場合 */
406     if(cmdl->cmd == NULL){
407         /* ラベルが定義されていて命令がない場合はエラー */
408         if(cmdl->label != NULL) {
409             setcerr(105, NULL);    /* no command in the line */
410         }
411     }
412     /* アセンブラ命令の処理 */
413     else if(cerrno == 0 && assemblecmd(cmdl, pass) == true) {
414         ;
415     }
416     /* マクロ命令の書込 */
417     else if(cerrno == 0 && macrocmd(cmdl, pass) == true) {
418         ;
419     }
420     /* 機械語命令の書込 */
421     else if(cerrno == 0 && cometcmd(cmdl, pass) == true) {
422         ;
423     }
424     else if(cerrno == 0) {
425         setcerr(113, cmdl->cmd);    /* operand too many in COMET II command */
426     }
427     /* エラーが発生していないか確認 */
428     if(cerrno == 0) {
429         status = true;
430     }
431     return status;
432 }
433
434 void printline(FILE *stream, const char *filename, int lineno, char *line) {
435     fprintf(stream, "%s:%5d:%s", filename, lineno, line);
436 }
437
438 /* 指定された名前のファイルをアセンブル */
439 /* 2回実行される */
440 bool assemble(const char *file, PASS pass)
441 {
442     int lineno = 0;
443     bool status = true;
444     CMDLINE *cmdl;
445     char *line;
446     FILE *fp;
447
448     addcerrlist_assemble();
449     if((fp = fopen(file, "r")) == NULL) {
450         perror(file);
451         return false;
452     }
453     for(; ;) {
454         cmdl = malloc(sizeof(CMDLINE));
455         line = malloc(LINESIZE + 1);
456         if((line = fgets(line, LINESIZE, fp)) == NULL) {
457             break;
458         }
459         lineno++;
460         if((pass == FIRST && asmode.src == true) ||
461            (pass == SECOND && asmode.asdetail == true))
462         {
463             printline(stdout, file, lineno, line);
464         }
465         if((cmdl = linetok(line)) != NULL) {
466             if(pass == FIRST && cmdl->label != NULL) {
467                 if(addlabel(prog, cmdl->label, ptr) == false) {
468                     break;
469                 }
470             }
471             if(assembleline(cmdl, pass) == false) {
472                 break;
473             }
474         }
475         if(cerrno > 0) {
476             break;
477         }
478     }
479     if(cerrno > 0) {
480         fprintf(stderr, "Assemble error - %d: %s\n", cerrno, cerrmsg);
481         printline(stderr, file, lineno, line);
482         status = false;
483     }
484     fclose(fp);
485     return status;
486 }