X-Git-Url: http://j8takagi.net/cgi-bin/gitweb.cgi?p=YACASL2.git;a=blobdiff_plain;f=src%2Fassemble.c;h=fc7aed16418af9bbb5c7c45a676e5f3bfc5887ca;hp=d6cb4f226ed416326c4bad6f252c2b9b5a22efac;hb=f9ad3a081e1777bb6d892af65d0e4090cb31bbfa;hpb=a639337a9aa30a059c1695ab1701b8714fd26193 diff --git a/src/assemble.c b/src/assemble.c index d6cb4f2..fc7aed1 100644 --- a/src/assemble.c +++ b/src/assemble.c @@ -13,7 +13,7 @@ ASMODE asmode = {false, false, false, false, false}; /** - * アセンブルのプロパティ: ptr, lptr, *prog + * アセンブル時の、現在およびリテラルのアドレスとプログラム入口名: ptr, lptr, prog */ ASPTR *asptr; @@ -80,6 +80,7 @@ WORD getgr(const char *str, bool is_x) { assert(str != NULL); WORD r; + /* 「GR[0-7]」以外の文字列では、0xFFFFを返して終了 */ if(!(strlen(str) == 3 && strncmp(str, "GR", 2) == 0 && (*(str+2) >= '0' && *(str+2) <= '0' + (GRSIZE - 1)))) @@ -101,8 +102,9 @@ WORD getgr(const char *str, bool is_x) */ WORD getliteral(const char *str, PASS pass) { - WORD adr = asptr->lptr; assert(*str == '='); + WORD adr = asptr->lptr; + if(*(++str) == '\'') { /* 文字定数 */ writestr(str, true, pass); } else { @@ -148,7 +150,7 @@ bool assemblecmd(const CMDLINE *cmdl, PASS pass) } /* プログラム名の設定 */ asptr->prog = strdup_chk(cmdl->label, "asptr.prog"); - /* オペランドがある場合、実行開始番地を設定 */ + /* オペランドがある場合、実行開始アドレスを設定 */ if(pass == SECOND && cmdl->opd->opdc == 1) { if((execptr->start = getlabel(asptr->prog, cmdl->opd->opdv[0])) == 0xFFFF) { setcerr(103, cmdl->opd->opdv[0]); /* label not found */ @@ -157,16 +159,15 @@ bool assemblecmd(const CMDLINE *cmdl, PASS pass) status = true; break; case END: - /* 1回目のアセンブルの場合は、リテラル領域開始番地を設定 */ + /* 1回目のアセンブルの場合は、リテラル領域開始アドレスを設定 */ if(pass == FIRST) { asptr->lptr = asptr->ptr; } - /* 2回目のアセンブルの場合は、リテラル領域終了番地を実行終了番地として設定 */ + /* 2回目のアセンブルの場合は、リテラル領域終了アドレスを実行終了アドレスとして設定 */ else if(pass == SECOND) { execptr->end = asptr->lptr; } - /* プログラム名のクリア */ - asptr->prog = NULL; + FREE(asptr->prog); status = true; break; case DS: @@ -204,8 +205,8 @@ bool assemblecmd(const CMDLINE *cmdl, PASS pass) bool macrocmd(const CMDLINE *cmdl, PASS pass) { int i = 0; - MACROCMDID cmdid; - MACROCMD macrocmd[] = { + MACROCMDID cmdid = 0; + MACROCMD macrocmd[] = { { IN, 2, 2, "IN" }, { OUT, 2, 2, "OUT" }, { RPUSH, 0, 0, "RPUSH" }, @@ -256,7 +257,7 @@ bool macrocmd(const CMDLINE *cmdl, PASS pass) */ void writeIN(const char *ibuf, const char *len, PASS pass) { - char *line = malloc_chk(LINESIZE+1, "writeIN.line"); + char *line = malloc_chk(LINESIZE + 1, "writeIN.line"); assembleline(" PUSH 0,GR1", pass); assembleline(" PUSH 0,GR2", pass); @@ -268,7 +269,7 @@ void writeIN(const char *ibuf, const char *len, PASS pass) assembleline(" POP GR2", pass); assembleline(" POP GR1", pass); - free_chk(line, "writeIN.line"); + FREE(line); } /** @@ -286,7 +287,7 @@ void writeIN(const char *ibuf, const char *len, PASS pass) */ void writeOUT(const char *obuf, const char *len, PASS pass) { - char *line = malloc_chk(LINESIZE+1, "writeOUT.line"); + char *line = malloc_chk(LINESIZE + 1, "writeOUT.line"); assembleline(" PUSH 0,GR1", pass); assembleline(" PUSH 0,GR2", pass); @@ -300,7 +301,7 @@ void writeOUT(const char *obuf, const char *len, PASS pass) assembleline(" SVC 2", pass); assembleline(" POP GR2", pass); assembleline(" POP GR1", pass); - free_chk(line, "writeOUT.line"); + FREE(line); } /** マクロ命令「RPUSH」をメモリに書き込む @@ -315,13 +316,13 @@ void writeOUT(const char *obuf, const char *len, PASS pass) void writeRPUSH(PASS pass) { int i; - char *line = malloc_chk(LINESIZE+1, "writeRPUSH.line"); + char *line = malloc_chk(LINESIZE + 1, "writeRPUSH.line"); for(i = 1; i <= GRSIZE-1; i++) { sprintf(line, " PUSH 0,GR%d", i); assembleline(line, pass); } - free_chk(line, "writeRPUSH.line"); + FREE(line); } /** @@ -338,13 +339,13 @@ void writeRPUSH(PASS pass) void writeRPOP(PASS pass) { int i; - char *line = malloc_chk(LINESIZE+1, "writeRPOP.line"); + char *line = malloc_chk(LINESIZE + 1, "writeRPOP.line"); for(i = GRSIZE-1; i >= 1; i--) { sprintf(line, " POP GR%d", i); assembleline(line, pass); } - free_chk(line, "writeRPOP.line"); + FREE(line); } /** @@ -393,9 +394,7 @@ bool cometcmd(const CMDLINE *cmdl, PASS pass) /* オペランド数2または3。第2オペランドはアドレス、 */ /* 第3オペランドは指標レジスタとして用いる汎用レジスタ */ else if(cmdl->opd->opdc == 2 || cmdl->opd->opdc == 3) { - if((cmd = getcmdcode(cmdl->cmd, R_ADR_X_)) == 0xFFFF && - (cmd = getcmdcode(cmdl->cmd, R_ADR_X)) == 0xFFFF) - { + if((cmd = getcmdcode(cmdl->cmd, R_ADR_X)) == 0xFFFF) { setcerr(110, cmdl->cmd); /* not command of operand "r,adr[,x]" */ return false; } @@ -477,7 +476,7 @@ bool writememory(WORD word, WORD adr, PASS pass) */ void writestr(const char *str, bool literal, PASS pass) { - assert(cerr->num == 0 && *str == '\''); + assert(*str == '\''); const char *p = str + 1; bool lw = false; @@ -510,6 +509,7 @@ void writestr(const char *str, bool literal, PASS pass) void writeDC(const char *str, PASS pass) { WORD adr = 0x0; + if(*str == '\'') { writestr(str, false, pass); } else { @@ -525,17 +525,15 @@ void writeDC(const char *str, PASS pass) } /** - * 1行をアセンブル + * トークンをアセンブル */ bool assembletok(const CMDLINE *cmdl, PASS pass) { bool status = false; + /* 命令がない場合 */ if(cmdl->cmd == NULL){ - /* ラベルが定義されていて命令がない場合はエラー */ - if(cmdl->label != NULL) { - setcerr(105, NULL); /* no command in the line */ - } + ; } /* アセンブラ命令の処理 */ else if(cerr->num == 0 && assemblecmd(cmdl, pass) == true) { @@ -574,6 +572,7 @@ void printline(FILE *stream, const char *filename, int lineno, char *line) WORD getadr(const char *prog, const char *str, PASS pass) { WORD adr = 0x0; + if(*str == '=') { adr = getliteral(str, pass); } else if(isdigit(*str) || *str == '-' || *str == '#') { @@ -595,18 +594,31 @@ WORD getadr(const char *prog, const char *str, PASS pass) bool assembleline(const char *line, PASS pass) { CMDLINE *cmdl; + bool status = true; + int i; - if((cmdl = linetok(line)) != NULL) { - if(pass == FIRST && cmdl->label != NULL) { - if(addlabel(asptr->prog, cmdl->label, asptr->ptr) == false) { - return false; + cmdl = linetok(line); + status = (cerr->num == 0) ? true : false; + if(cmdl != NULL) { + if(status == true) { + if(pass == FIRST && cmdl->label != NULL) { + status = addlabel(asptr->prog, cmdl->label, asptr->ptr); } + if(status == true) { + status = assembletok(cmdl, pass); + } + FREE(cmdl->label); } - if(assembletok(cmdl, pass) == false) { - return false; + if(cmdl->opd != NULL) { + for(i = 0; i < cmdl->opd->opdc; i++) { + FREE(cmdl->opd->opdv[i]); + } } + FREE(cmdl->opd); + FREE(cmdl->cmd); } - return true; + FREE(cmdl); + return status; } /** @@ -616,18 +628,18 @@ void addcerrlist_assemble() { addcerrlist_tok(); addcerrlist_word(); + addcerrlist_label(); addcerrlist(ARRAYSIZE(cerr_assemble), cerr_assemble); } /** * 指定された名前のファイルをアセンブル - * 2回実行される + * 1回目ではラベルを登録し、2回目ではラベルからアドレスを読み込む + * アセンブル完了時はtrue、エラー発生時はfalseを返す */ -bool assemble(const char *file, PASS pass) +bool assemblefile(const char *file, PASS pass) { int lineno = 0; - bool status = true; - CMDLINE *cmdl; char *line; FILE *fp; @@ -635,12 +647,8 @@ bool assemble(const char *file, PASS pass) perror(file); return false; } - for(; ;) { - cmdl = malloc_chk(sizeof(CMDLINE), "cmdl"); - line = malloc_chk(LINESIZE + 1, "line"); - if((line = fgets(line, LINESIZE, fp)) == NULL) { - break; - } + line = malloc_chk(LINESIZE + 1, "assemble.line"); + while(fgets(line, LINESIZE, fp)) { lineno++; if((pass == FIRST && asmode.src == true) || (pass == SECOND && asmode.asdetail == true)) @@ -650,19 +658,14 @@ bool assemble(const char *file, PASS pass) if(assembleline(line, pass) == false) { break; } - if(cerr->num > 0) { - break; - } - free_chk(line, "line"); - free_chk(cmdl, "cmdl"); } if(cerr->num > 0) { fprintf(stderr, "Assemble error - %d: %s\n", cerr->num, cerr->msg); printline(stderr, file, lineno, line); - status = false; } + FREE(line); fclose(fp); - return status; + return (cerr->num == 0) ? true : false; } /**