4 /* アセンブルモード: src, label, onlylabel, asdetail, onlyassemble */
5 ASMODE asmode = {false, false, false, false, false};
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" },
36 bool addcerrlist_assemble()
38 return addcerrlist(ARRAYSIZE(cerr_assemble), cerr_assemble);
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)
48 /* 「GR[0-7]」以外の文字列では、0xFFFFを返して終了 */
49 if(!(strlen(str) == 3 && strncmp(str, "GR", 2) == 0 &&
50 (*(str+2) >= '0' && *(str+2) <= '0' + (GRSIZE - 1))))
54 r = (WORD)(*(str+2) - '0');
55 /* GR0は指標レジスタとして用いることができない */
56 if(is_x == true && r == 0x0) {
57 setcerr(120, NULL); /* GR0 in operand x */
64 /* アドレスには、リテラル/10進定数/16進定数/アドレス定数が含まれる */
65 WORD getadr(const char *prog, const char *str, PASS pass)
69 adr = getliteral(str, pass);
70 } else if(isdigit(*str) || *str == '-' || *str == '#') {
73 if(pass == SECOND && (adr = getlabel(prog, str)) == 0xFFFF) {
75 setcerr(103, str); /* label not found */
82 /* WORD値wordをアドレスadrに書込 */
83 /* 書込に成功した場合はtrue、失敗した場合はfalseを返す */
84 bool writememory(WORD word, WORD adr, PASS pass)
88 /* COMET IIメモリオーバーの場合 */
89 if(adr >= sys->memsize) {
90 setcerr(119, word2n(adr)); /* out of COMET II memory */
93 (sys->memory)[adr] = word;
94 if(pass == SECOND && asmode.asdetail == true) {
95 fprintf(stdout, "\t#%04X\t#%04X\n", adr, word);
102 /* 定数の前に等号(=)をつけて記述されるリテラルを返す */
103 /* リテラルには、10進定数/16進定数/文字定数が含まれる */
104 WORD getliteral(const char *str, PASS pass)
106 WORD adr = asprop->lptr;
108 if(*(++str) == '\'') { /* 文字定数 */
109 writestr(str, true, pass);
111 writememory(nh2word(str), (asprop->lptr)++, pass);
116 /* ' 'で囲まれた文字定数をメモリに書込 */
117 /* DC命令とリテラルで使い、リテラルの場合はリテラル領域に書込 */
118 void writestr(const char *str, bool literal, PASS pass)
120 assert(cerr->num == 0 && *str == '\'');
121 const char *p = str + 1;
125 /* 閉じ「'」がないまま文字列が終了した場合 */
127 setcerr(123, str); /* unclosed quote */
130 /* 「'」の場合、次の文字が「'」でない場合は正常終了 */
131 if(*p == '\'' && *(++p) != '\'') {
133 } else if(literal == true && lw == true) {
134 setcerr(124, str); /* more than one character in literal */
137 /*リテラルの場合はリテラル領域に書込 */
138 if(literal == true) {
139 writememory(*(p++), (asprop->lptr)++, pass);
142 writememory(*(p++), (asprop->ptr)++, pass);
147 /* アセンブラ命令DCをメモリに書込 */
148 void writeDC(const char *str, PASS pass)
152 writestr(str, false, pass);
154 if(*str == '#' || isdigit(*str) || *str == '-') {
157 if(pass == SECOND && (adr = getlabel(asprop->prog, str)) == 0xFFFF) {
158 setcerr(103, str); /* label not found */
161 writememory(adr, (asprop->ptr)++, pass);
165 /* 命令がアセンブラ命令の場合は処理を実行 */
166 /* 実行に成功した場合はtrue、それ以外の場合はfalseを返す */
167 bool assemblecmd(const CMDLINE *cmdl, PASS pass)
173 { START, 0, 1, "START" },
174 { END, 0, 0, "END" },
175 { DC, 1, OPDSIZE, "DC" },
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 */
186 cmdid = ascmd[i].cmdid;
189 } while(ascmd[++i].cmdid != 0);
194 if(cmdl->label == NULL) {
195 setcerr(107, NULL); /* no label in START */
199 asprop->prog = strdup_chk(cmdl->label, "asprop.prog");
200 /* オペランドがある場合、実行開始番地を設定 */
201 if(pass == SECOND && cmdl->opd->opdc == 1) {
202 if((prog->start = getlabel(asprop->prog, cmdl->opd->opdv[0])) == 0xFFFF) {
203 setcerr(103, cmdl->opd->opdv[0]); /* label not found */
211 asprop->lptr = asprop->ptr;
213 /* 実行終了番地と次のプログラムの実行開始番地を設定 */
214 else if(pass == SECOND) {
215 prog->end = asprop->lptr;
221 for(i = 0; i < atoi(cmdl->opd->opdv[0]); i++) {
222 writememory(0x0, (asprop->ptr)++, pass);
230 for(i = 0; i < cmdl->opd->opdc; i++) {
231 writeDC(cmdl->opd->opdv[i], pass);
247 /* 命令がマクロ命令の場合はメモリに書込 */
248 /* 書込に成功した場合はtrue、それ以外の場合はfalseを返す */
249 bool macrocmd(const CMDLINE *cmdl, PASS pass)
254 MACROCMD macrocmd[] = {
256 { OUT, 2, 2, "OUT" },
257 { RPUSH, 0, 0, "RPUSH" },
258 { RPOP, 0, 0, "RPOP" },
263 if(strcmp(cmdl->cmd, macrocmd[i].cmd) == 0) {
264 if(cmdl->opd->opdc < macrocmd[i].opdc_min ||
265 cmdl->opd->opdc > macrocmd[i].opdc_max)
267 setcerr(106, NULL); /* operand count mismatch */
270 cmdid = macrocmd[i].cmdid;
273 } while(macrocmd[++i].cmdid != 0);
277 status = writeIN(cmdl->opd->opdv[0], cmdl->opd->opdv[1], pass);
280 status = writeOUT(cmdl->opd->opdv[0], cmdl->opd->opdv[1], pass);
283 status = writeRPUSH(pass);
286 status = writeRPOP(pass);
295 /* 書込に成功した場合はtrue、それ以外の場合はfalseを返す */
296 bool cometcmd(const CMDLINE *cmdl, PASS pass)
298 WORD cmd, adr, r1, r2, x;
302 if(cmdl->opd->opdc == 0) {
303 if((cmd = getcmdcode(cmdl->cmd, NONE)) == 0xFFFF) {
304 setcerr(112, cmdl->cmd); /* not command of no operand */
307 if(writememory(cmd, (asprop->ptr)++, pass) == true) {
312 else if((r1 = getgr(cmdl->opd->opdv[0], false)) != 0xFFFF) {
314 if(cmdl->opd->opdc == 1) {
315 if((cmd = getcmdcode(cmdl->cmd, R_)) == 0xFFFF) {
316 setcerr(108, cmdl->cmd); /* not command of operand "r" */
320 if(writememory(cmd, (asprop->ptr)++, pass) == true) {
324 /* オペランド数2。第2オペランドは汎用レジスタ */
325 else if(cmdl->opd->opdc == 2 && (r2 = getgr(cmdl->opd->opdv[1], false)) != 0xFFFF) {
326 if((cmd = getcmdcode(cmdl->cmd, R1_R2)) == 0xFFFF) {
327 setcerr(109, cmdl->cmd); /* not command of operand "r1,r2" */
330 cmd |= ((r1 << 4) | r2);
331 if(cerr->num == 0 && writememory(cmd, (asprop->ptr)++, pass) == true) {
335 /* オペランド数2〜3。第2オペランドはアドレス、 */
336 /* 第3オペランドは指標レジスタとして用いる汎用レジスタ */
337 else if(cmdl->opd->opdc == 2 || cmdl->opd->opdc == 3) {
338 if((cmd = getcmdcode(cmdl->cmd, R_ADR_X_)) == 0xFFFF &&
339 (cmd = getcmdcode(cmdl->cmd, R_ADR_X)) == 0xFFFF)
341 setcerr(110, cmdl->cmd); /* not command of operand "r,adr[,x]" */
346 if(cmdl->opd->opdc == 3) {
347 if((x = getgr(cmdl->opd->opdv[2], true)) == 0xFFFF) {
348 setcerr(125, cmdl->cmd); /* not GR in operand x */
353 adr = getadr(asprop->prog, cmdl->opd->opdv[1], pass);
354 writememory(cmd, (asprop->ptr)++, pass);
355 writememory(adr, (asprop->ptr)++, pass);
360 setcerr(113, cmdl->cmd); /* operand too many in COMET II command */
364 /* オペランド数1〜2。第1オペランドはアドレス */
365 else if(cmdl->opd->opdc == 1 || cmdl->opd->opdc == 2) {
366 if((cmd = getcmdcode(cmdl->cmd, ADR_X)) == 0xFFFF) {
367 setcerr(111, cmdl->cmd); /* not command of operand "adr[,x]" */
370 /* オペランド数2の場合、第2オペランドは指標レジスタとして用いる汎用レジスタ */
371 if(cmdl->opd->opdc == 2) {
372 x = getgr(cmdl->opd->opdv[1], true);
378 /* CALLの場合はプログラムの入口名を表すラベルを取得 */
379 /* CALL以外の命令の場合と、プログラムの入口名を取得できない場合は、 */
380 /* 同一プログラム内のラベルを取得 */
381 if(pass == SECOND && cmd == 0x8000) { /* CALL命令 */
382 adr = getlabel(NULL, cmdl->opd->opdv[0]);
384 if(cmd != 0x8000 || (pass == SECOND && adr == 0xFFFF)) {
385 adr = getadr(asprop->prog, cmdl->opd->opdv[0], pass);
387 writememory(cmd, (asprop->ptr)++, pass);
388 writememory(adr, (asprop->ptr)++, pass);
397 bool assembleline(const CMDLINE *cmdl, PASS pass)
401 if(cmdl->cmd == NULL){
402 /* ラベルが定義されていて命令がない場合はエラー */
403 if(cmdl->label != NULL) {
404 setcerr(105, NULL); /* no command in the line */
408 else if(cerr->num == 0 && assemblecmd(cmdl, pass) == true) {
412 else if(cerr->num == 0 && macrocmd(cmdl, pass) == true) {
416 else if(cerr->num == 0 && cometcmd(cmdl, pass) == true) {
419 else if(cerr->num == 0) {
420 setcerr(113, cmdl->cmd); /* operand too many in COMET II command */
429 void printline(FILE *stream, const char *filename, int lineno, char *line) {
430 fprintf(stream, "%s:%5d:%s", filename, lineno, line);
433 /* 指定された名前のファイルをアセンブル */
435 bool assemble(const char *file, PASS pass)
443 addcerrlist_assemble();
444 if((fp = fopen(file, "r")) == NULL) {
449 cmdl = malloc_chk(sizeof(CMDLINE), "cmdl");
450 line = malloc_chk(LINESIZE + 1, "line");
451 if((line = fgets(line, LINESIZE, fp)) == NULL) {
455 if((pass == FIRST && asmode.src == true) ||
456 (pass == SECOND && asmode.asdetail == true))
458 printline(stdout, file, lineno, line);
460 if((cmdl = linetok(line)) != NULL) {
461 if(pass == FIRST && cmdl->label != NULL) {
462 if(addlabel(asprop->prog, cmdl->label, asprop->ptr) == false) {
466 if(assembleline(cmdl, pass) == false) {
473 free_chk(line, "line");
474 free_chk(cmdl, "cmdl");
477 fprintf(stderr, "Assemble error - %d: %s\n", cerr->num, cerr->msg);
478 printline(stderr, file, lineno, line);