X-Git-Url: http://j8takagi.net/cgi-bin/gitweb.cgi?p=YACASL2.git;a=blobdiff_plain;f=src%2Fassemble.c;h=5e18000aa39bc01200b77268eba2cd7f7d6d8057;hp=68a3457670c5b961748e5d48bd4206a6d9c0fac8;hb=d650cc4148ccd23f940ac60050c36c89897c168f;hpb=0dc92ef3806ab8fd215b220472fe78d234b481aa diff --git a/src/assemble.c b/src/assemble.c index 68a3457..5e18000 100644 --- a/src/assemble.c +++ b/src/assemble.c @@ -1,12 +1,4 @@ -#include -#include -#include -#include -#include -#include - #include "assemble.h" -#include "cerr.h" /** * @brief ファイルストリームの現在行を番号付きで表示する @@ -328,9 +320,9 @@ WORD getadr(const char *prog, const char *str, PASS pass) { WORD adr = 0x0; - if(*str == '=') { + if(str[0] == '=') { adr = getliteral(str, pass); - } else if(isdigit(*str) || *str == '-' || *str == '#') { + } else if(isdigit(str[0]) || str[0] == '-' || str[0] == '#') { adr = nh2word(str); } else { if(pass == SECOND) { @@ -347,12 +339,13 @@ WORD grword(const char *str, bool is_x) WORD r; /* "GR[0-7]" 以外の文字列では、0xFFFFを返して終了 */ - if(!(strlen(str) == 3 && strncmp(str, "GR", 2) == 0 && - (*(str+2) >= '0' && *(str+2) <= '0' + (GRSIZE - 1)))) + if(strlen(str) != 3 || + strncmp(str, "GR", 2) != 0 || + str[2] < '0' || str[2] > '0' + (GRSIZE - 1)) { return 0xFFFF; } - r = (WORD)(*(str+2) - '0'); + r = (WORD)(str[2] - '0'); /* GR0は指標レジスタとして用いることができない */ if(is_x == true && r == 0x0) { setcerr(120, ""); /* GR0 in operand x */ @@ -363,10 +356,11 @@ WORD grword(const char *str, bool is_x) WORD getliteral(const char *str, PASS pass) { - assert(*str == '='); + assert(str[0] == '='); WORD adr = asptr->lptr; - if(*(++str) == '\'') { /* 文字定数 */ + str++; + if(str[0] == '\'') { /* 文字定数 */ writestr(str, true, pass); } else { writememory(nh2word(str), (asptr->lptr)++, pass); @@ -392,29 +386,27 @@ void writememory(WORD word, WORD adr, PASS pass) void writestr(const char *str, bool literal, PASS pass) { - assert(*str == '\''); - const char *p = str + 1; + assert(str[0] == '\''); + int i; bool lw = false; - for(; ;) { - /* 閉じ「'」がないまま文字列が終了した場合 */ - if(*p == '\0') { + /* 「'」の場合、1文字スキップし、次の文字が「'」でなければ正常終了 */ + for(i = 1; str[i] != '\'' || str[++i] == '\''; i++) { + /* 「'」が閉じないまま文字列が終了した場合はエラー */ + if(!str[i]) { setcerr(123, str); /* unclosed quote */ break; } - /* 「'」の場合、次の文字が「'」でない場合は正常終了 */ - if(*p == '\'' && *(++p) != '\'') { - break; - } else if(literal == true && lw == true) { + if(literal == true && lw == true) { setcerr(124, str); /* more than one character in literal */ break; } /*リテラルの場合はリテラル領域に書込 */ if(literal == true) { - writememory(*(p++), (asptr->lptr)++, pass); + writememory(str[i], (asptr->lptr)++, pass); lw = true; } else { - writememory(*(p++), (asptr->ptr)++, pass); + writememory(str[i], (asptr->ptr)++, pass); } } } @@ -426,7 +418,7 @@ void writedc(const char *str, PASS pass) if(*str == '\'') { writestr(str, false, pass); } else { - if(*str == '#' || isdigit(*str) || *str == '-') { + if(str[0] == '#' || isdigit(str[0]) || str[0] == '-') { adr = nh2word(str); } else { if(pass == SECOND && (adr = getlabel(asptr->prog, str)) == 0xFFFF) { @@ -443,17 +435,15 @@ void assemble_start(const CMDLINE *cmdl, PASS pass) setcerr(106, ""); /* operand count mismatch */ return; } - if(*(cmdl->label) == '\0') { + if(!cmdl->label[0]) { setcerr(107, ""); /* no label in START */ return; } /* プログラム名の設定 */ strcpy(asptr->prog, cmdl->label); - /* オペランドがある場合、実行開始アドレスを設定 */ - if(pass == SECOND && cmdl->opd->opdv[0] != NULL) { - if((execptr->start = getlabel(asptr->prog, cmdl->opd->opdv[0])) == 0xFFFF) { - setcerr(103, cmdl->opd->opdv[0]); /* label not found */ - } + /* オペランドがある場合、書き込みと実行の開始アドレスを設定 */ + if(cmdl->opd->opdv[0] != NULL) { + asptr->ptr = execptr->start = getadr(asptr->prog, cmdl->opd->opdv[0], pass); } } @@ -471,7 +461,7 @@ void assemble_end(const CMDLINE *cmdl, PASS pass) else if(pass == SECOND) { execptr->end = asptr->lptr; } - *(asptr->prog) = '\0'; + strcpy(asptr->prog, ""); } void assemble_ds(const CMDLINE *cmdl, PASS pass) @@ -579,7 +569,7 @@ bool casl2cmd(CMD *cmdtbl, const CMDLINE *cmdl, PASS pass) { int i; void (*cmdptr)(); - for(i = 0; *(cmdtbl[i].name) != '\0'; i++) { + for(i = 0; cmdtbl[i].name[0]; i++) { if(strcmp(cmdl->cmd, cmdtbl[i].name) == 0) { cmdptr = cmdtbl[i].ptr; (*cmdptr)(cmdl, pass); @@ -679,7 +669,7 @@ bool assemble_comet2cmd(const CMDLINE *cmdl, PASS pass) bool assembletok(const CMDLINE *cmdl, PASS pass) { /* 命令がない場合 */ - if(*(cmdl->cmd) == '\0') { + if(!cmdl->cmd[0]) { return true; } /* アセンブラ命令またはマクロ命令の書込 */ @@ -704,7 +694,7 @@ bool assembleline(const char *line, PASS pass) stat = (cerr->num == 0) ? true : false; if(cmdl != NULL) { if(stat == true) { - if(pass == FIRST && *(cmdl->label) != '\0') { + if(pass == FIRST && cmdl->label[0]) { stat = addlabel(asptr->prog, cmdl->label, asptr->ptr); } } @@ -757,6 +747,51 @@ bool assemblefile(const char *file, PASS pass) return (cerr->num == 0) ? true : false; } +bool assemble(int filec, char *filev[], WORD adr) +{ + int i; + PASS pass; + WORD bp[filec]; + bool stat = false; + + asptr = malloc_chk(sizeof(ASPTR), "asptr"); /* アセンブル時のプロパティ用の領域確保 */ + asptr->prog = malloc_chk(LABELSIZE + 1, "asptr.prog"); + asptr->ptr = adr; + /* アセンブル。ラベル表作成のため、2回行う */ + for(pass = FIRST; pass <= SECOND; pass++) { + for(i = 0; i < filec; i++) { + /* データの格納開始位置 */ + if(pass == FIRST) { + bp[i] = asptr->ptr; + } else if(pass == SECOND) { + asptr->ptr = bp[i]; + } + if(execmode.trace == true || execmode.dump == true || + asmode.src == true || asmode.label == true || asmode.asdetail == true) + { + fprintf(stdout, "\nAssemble %s (%d)\n", filev[i], pass); + } + /* ファイルをアセンブル */ + stat = assemblefile(filev[i], pass); + if(stat == false) { + goto asfin; + } + } + if(pass == FIRST && asmode.label == true) { + fprintf(stdout, "\nLabel::::\n"); + printlabel(); + if(asmode.onlylabel == true) { + break; + } + } + } +asfin: + freelabel(); /* ラベルハッシュ表を解放 */ + FREE(asptr->prog); /* アセンブル時のプロパティを解放 */ + FREE(asptr); + return stat; +} + /* assemble.hで定義された関数群 */ void addcerrlist_assemble() {