From: j8takagi Date: Sun, 17 Feb 2019 13:36:10 +0000 (+0900) Subject: ソースコードの推敲 X-Git-Tag: v0.5p9~15 X-Git-Url: https://j8takagi.net/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=bbd97e8f26bfbb3d4b4688bea469062d82aaa764;p=YACASL2.git ソースコードの推敲 --- diff --git a/src/assemble.c b/src/assemble.c index daee475..10ecb41 100644 --- a/src/assemble.c +++ b/src/assemble.c @@ -386,34 +386,28 @@ 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[0] == '\'') { - p++; - if(p[0] != '\'') { - 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[0], (asptr->lptr)++, pass); + writememory(str[i], (asptr->lptr)++, pass); lw = true; } else { - writememory(p[0], (asptr->ptr)++, pass); + writememory(str[i], (asptr->ptr)++, pass); } - p++; } } @@ -424,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) { diff --git a/src/disassemble.c b/src/disassemble.c index a97a042..2904e79 100644 --- a/src/disassemble.c +++ b/src/disassemble.c @@ -93,8 +93,8 @@ void disassemble_puts_code(int ascol, WORD pradr, int wordc, WORD wordv[]) } void disassemble_cmd_adr_x(CMDTYPE cmdtype, const char *cmdname, WORD word, WORD adr, WORD pradr) { - WORD x; - char *g; + WORD x = 0; + char *g = NULL; int cnt = 0; cnt += fprintf(stdout, " %-7s ", cmdname); if(cmdtype == R_ADR_X) { @@ -111,7 +111,7 @@ void disassemble_cmd_adr_x(CMDTYPE cmdtype, const char *cmdname, WORD word, WORD void disassemble_cmd_r(CMDTYPE cmdtype, const char *cmdname, WORD word, WORD pradr) { - char *g, *g1, *g2; + char *g = NULL, *g1 = NULL, *g2 = NULL; int cnt = 0; cnt += fprintf(stdout, " %-7s ", cmdname); if(cmdtype == R1_R2) { @@ -192,13 +192,15 @@ bool disassemble_file(const char *file) fprintf(stdout, "MAIN START\n"); for(word = fgetword(fp); !feof(fp); i++, word = fgetword(fp)) { - cmdname = getcmdname(cmd = word & 0xFF00); + cmd = word & 0xFF00; + cmdname = getcmdname(cmd); cmdtype = getcmdtype(cmd); if(word == 0){ if(inst == true) { /* プログラム領域の場合 */ disassemble_cmd_r(NONE, "nop", 0, i); } else { /* データ領域の場合 */ - if((zcnt = zero_data_cnt(fp)) == 1) { /* 1つだけの0はDCとみなす */ + zcnt = zero_data_cnt(fp); + if(zcnt == 1) { /* 1つだけの0はDCとみなす */ disassemble_dc(0, i); } else { /* 連続する0はDSとみなす */ disassemble_ds(zcnt, i); @@ -225,29 +227,29 @@ bool disassemble_file(const char *file) void disassemble_memory(WORD start, WORD end) { - WORD i, word, cmd; + WORD i, cmd; CMDTYPE cmdtype = 0; char *cmdname; bool inst = true; for(i = start; i <= end; i++) { - word = sys->memory[i]; - cmdname = getcmdname(cmd = word & 0xFF00); + cmd = sys->memory[i] & 0xFF00; + cmdname = getcmdname(cmd); cmdtype = getcmdtype(cmd); - if(word == 0) { + if(sys->memory[i] == 0) { if(inst == true) { /* プログラム領域の場合 */ disassemble_cmd_r(NONE, "nop", 0, i); } else { /* データ領域の場合。メモリーでは、DC 0とみなす */ disassemble_dc(0, i); } } else if(cmd == 0) { - disassemble_dc(word, i); + disassemble_dc(sys->memory[i], i); } else { if(cmdtype == R_ADR_X || cmdtype == ADR_X) { - disassemble_cmd_adr_x(cmdtype, cmdname, word, sys->memory[i+1], i); + disassemble_cmd_adr_x(cmdtype, cmdname, sys->memory[i], sys->memory[i+1], i); i++; } else { - disassemble_cmd_r(cmdtype, cmdname, word, i); + disassemble_cmd_r(cmdtype, cmdname, sys->memory[i], i); } inst = (cmd != 0x8100) ? true : false; } diff --git a/src/word.c b/src/word.c index 74a2786..df953c5 100644 --- a/src/word.c +++ b/src/word.c @@ -36,7 +36,7 @@ static CERR cerr_word[] = { WORD n2word(const char *str) { - assert(isdigit(*str) || *str == '-'); + assert(isdigit(str[0]) || str[0] == '-'); char *check; int n; @@ -55,7 +55,7 @@ WORD n2word(const char *str) WORD h2word(const char *str) { - assert(*str == '#'); + assert(str[0] == '#'); WORD w = 0x0; char *check; @@ -104,18 +104,19 @@ char *word2n(WORD word) enum { MAXLEN = 5, /* WORD値を10進数で表したときの最大桁数 */ }; - char *p = malloc_chk(MAXLEN + 1, "word2n.p"), *digit = malloc_chk(MAXLEN + 1, "word2n.digit"); + char *n = malloc_chk(MAXLEN + 1, "word2n.n"), tmp; int i = 0, j; do{ - p[i++] = word % 10 + '0'; + n[i++] = word % 10 + '0'; } while((word /= 10) > 0); for(j = 0; j < i; j++) { - digit[j] = p[(i-1)-j]; + tmp = n[j]; + n[j] = n[(i-1)-j]; + n[(i-1)-j] = tmp; } - digit[j] = '\0'; - FREE(p); - return digit; + n[j] = '\0'; + return n; } char *word2bit(const WORD word) @@ -136,7 +137,7 @@ char *word2bit(const WORD word) void print_dumpword(WORD word, bool logicalmode) { - char *bit = word2bit(word); + const char *bit = word2bit(word); if(logicalmode == true) { fprintf(stdout, "%6d", word); @@ -152,5 +153,4 @@ void print_dumpword(WORD word, bool logicalmode) } else if(word == '\t') { fprintf(stdout, " = \'\\t\'"); } - FREE(bit); }