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