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