From 93c7fbb80b79236d96ef53dc18cf7e32760fa613 Mon Sep 17 00:00:00 2001 From: j8takagi Date: Wed, 27 Feb 2019 14:38:58 +0900 Subject: [PATCH] =?utf8?q?=E3=82=BD=E3=83=BC=E3=82=B9=E3=81=AE=E6=8E=A8?= =?utf8?q?=E6=95=B2?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit 変数の宣言は、各変数1行で。初期値を必ず設定。 ループ変数は、ブロックで設定 --- src/assemble.c | 63 ++++++++++++++++++++++++--------------------- src/casl2.c | 36 +++++++++++++++++--------- src/casl2rev.c | 8 +++--- src/cerr.c | 21 ++++++++------- src/cmem.c | 9 +++---- src/comet2.c | 9 ++++--- src/comet2monitor.c | 9 ++++--- src/disassemble.c | 34 +++++++++++++++--------- src/dump.c | 9 ++++--- src/dumpword.c | 8 +++--- src/exec.c | 44 ++++++++++++++++--------------- src/hash.c | 5 ++-- src/label.c | 35 +++++++++++++------------ src/monitor.c | 34 ++++++++++++------------ src/struct.c | 39 ++++++++++++++-------------- src/word.c | 9 ++++--- 16 files changed, 206 insertions(+), 166 deletions(-) diff --git a/src/assemble.c b/src/assemble.c index 5e18000..e982082 100644 --- a/src/assemble.c +++ b/src/assemble.c @@ -307,7 +307,7 @@ static CMD macrocmd[] = { { "", NULL } }; -ASPTR *asptr; +ASPTR *asptr = NULL; ASMODE asmode = {false, false, false, false, false}; @@ -318,7 +318,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; + WORD adr = 0; if(str[0] == '=') { adr = getliteral(str, pass); @@ -336,7 +336,7 @@ WORD getadr(const char *prog, const char *str, PASS pass) WORD grword(const char *str, bool is_x) { - WORD r; + WORD r = 0; /* "GR[0-7]" 以外の文字列では、0xFFFFを返して終了 */ if(strlen(str) != 3 || @@ -349,7 +349,7 @@ WORD grword(const char *str, bool is_x) /* GR0は指標レジスタとして用いることができない */ if(is_x == true && r == 0x0) { setcerr(120, ""); /* GR0 in operand x */ - return 0x0; + return 0; } return r; } @@ -370,7 +370,7 @@ WORD getliteral(const char *str, PASS pass) void writememory(WORD word, WORD adr, PASS pass) { - char *n; + char *n = NULL; /* メモリオーバーの場合、エラー発生 */ if(adr >= sys->memsize) { @@ -387,11 +387,10 @@ void writememory(WORD word, WORD adr, PASS pass) void writestr(const char *str, bool literal, PASS pass) { assert(str[0] == '\''); - int i; bool lw = false; /* 「'」の場合、1文字スキップし、次の文字が「'」でなければ正常終了 */ - for(i = 1; str[i] != '\'' || str[++i] == '\''; i++) { + for(int i = 1; str[i] != '\'' || str[++i] == '\''; i++) { /* 「'」が閉じないまま文字列が終了した場合はエラー */ if(!str[i]) { setcerr(123, str); /* unclosed quote */ @@ -466,12 +465,11 @@ void assemble_end(const CMDLINE *cmdl, PASS pass) void assemble_ds(const CMDLINE *cmdl, PASS pass) { - int i; if(cmdl->opd->opdc != 1) { setcerr(106, ""); /* operand count mismatch */ return; } - for(i = 0; i < atoi(cmdl->opd->opdv[0]); i++) { + for(int i = 0; i < atoi(cmdl->opd->opdv[0]); i++) { writememory(0x0, (asptr->ptr)++, pass); if(cerr->num > 0) { break; @@ -481,12 +479,11 @@ void assemble_ds(const CMDLINE *cmdl, PASS pass) void assemble_dc(const CMDLINE *cmdl, PASS pass) { - int i; if(cmdl->opd->opdc == 0 || cmdl->opd->opdc >= OPDSIZE) { setcerr(106, ""); /* operand count mismatch */ return; } - for(i = 0; i < cmdl->opd->opdc; i++) { + for(int i = 0; i < cmdl->opd->opdc; i++) { writedc(cmdl->opd->opdv[i], pass); if(cerr->num > 0) { break; @@ -496,7 +493,9 @@ void assemble_dc(const CMDLINE *cmdl, PASS pass) void assemble_in(const CMDLINE *cmdl, PASS pass) { - char *line = malloc_chk(LINESIZE + 1, "assemble_in.line"); + char *line = NULL; + + line = malloc_chk(LINESIZE + 1, "assemble_in.line"); if(cmdl->opd->opdc == 0 || cmdl->opd->opdc > 2) { setcerr(106, ""); /* operand count mismatch */ return; @@ -515,7 +514,9 @@ void assemble_in(const CMDLINE *cmdl, PASS pass) void assemble_out(const CMDLINE *cmdl, PASS pass) { - char *line = malloc_chk(LINESIZE + 1, "assemble_out.line"); + char *line = NULL; + + line = malloc_chk(LINESIZE + 1, "assemble_out.line"); if(cmdl->opd->opdc == 0 || cmdl->opd->opdc > 2) { setcerr(106, ""); /* operand count mismatch */ return; @@ -537,13 +538,14 @@ void assemble_out(const CMDLINE *cmdl, PASS pass) void assemble_rpush(const CMDLINE *cmdl, PASS pass) { - int i; - char *line = malloc_chk(LINESIZE + 1, "assemble_rpush.line"); + char *line = NULL; + + line = malloc_chk(LINESIZE + 1, "assemble_rpush.line"); if(cmdl->opd->opdc > 0) { setcerr(106, ""); /* operand count mismatch */ return; } - for(i = 1; i <= GRSIZE-1; i++) { + for(int i = 1; i <= GRSIZE-1; i++) { sprintf(line, " PUSH 0,GR%d", i); assembleline(line, pass); } @@ -552,13 +554,14 @@ void assemble_rpush(const CMDLINE *cmdl, PASS pass) void assemble_rpop(const CMDLINE *cmdl, PASS pass) { - int i; - char *line = malloc_chk(LINESIZE + 1, "assemble_rpop.line"); + char *line = NULL; + + line = malloc_chk(LINESIZE + 1, "assemble_rpop.line"); if(cmdl->opd->opdc > 0) { setcerr(106, ""); /* operand count mismatch */ return; } - for(i = GRSIZE-1; i >= 1; i--) { + for(int i = GRSIZE-1; i >= 1; i--) { sprintf(line, " POP GR%d", i); assembleline(line, pass); } @@ -567,9 +570,9 @@ void assemble_rpop(const CMDLINE *cmdl, PASS pass) bool casl2cmd(CMD *cmdtbl, const CMDLINE *cmdl, PASS pass) { - int i; - void (*cmdptr)(); - for(i = 0; cmdtbl[i].name[0]; i++) { + void (*cmdptr)() = NULL; + + for(int i = 0; cmdtbl[i].name[0]; i++) { if(strcmp(cmdl->cmd, cmdtbl[i].name) == 0) { cmdptr = cmdtbl[i].ptr; (*cmdptr)(cmdl, pass); @@ -581,7 +584,10 @@ bool casl2cmd(CMD *cmdtbl, const CMDLINE *cmdl, PASS pass) bool assemble_comet2cmd(const CMDLINE *cmdl, PASS pass) { - WORD cmd, r_r1, x_r2, adr; + WORD cmd = 0; + WORD r_r1 = 0; + WORD x_r2 = 0; + WORD adr = 0; /* オペランドなし */ if(cmdl->opd->opdc == 0) { @@ -686,9 +692,8 @@ bool assembletok(const CMDLINE *cmdl, PASS pass) bool assembleline(const char *line, PASS pass) { - CMDLINE *cmdl; + CMDLINE *cmdl = NULL; bool stat = true; - int i; cmdl = linetok(line); stat = (cerr->num == 0) ? true : false; @@ -703,7 +708,7 @@ bool assembleline(const char *line, PASS pass) } FREE(cmdl->label); if(cmdl->opd != NULL) { - for(i = 0; i < cmdl->opd->opdc; i++) { + for(int i = 0; i < cmdl->opd->opdc; i++) { FREE(cmdl->opd->opdv[i]); } } @@ -722,8 +727,8 @@ bool assembleline(const char *line, PASS pass) bool assemblefile(const char *file, PASS pass) { int lineno = 1; - char *line; - FILE *fp; + char *line = NULL; + FILE *fp = NULL; if((fp = fopen(file, "r")) == NULL) { cerr->num = errno; @@ -803,7 +808,7 @@ void addcerrlist_assemble() void outassemble(const char *file) { - FILE *fp; + FILE *fp = NULL; if((fp = fopen(file, "w")) == NULL) { perror(file); diff --git a/src/casl2.c b/src/casl2.c index 6c17d50..e64105d 100644 --- a/src/casl2.c +++ b/src/casl2.c @@ -15,7 +15,7 @@ void addcerrlist_casl2(); * * @param *str ファイル名 */ -const char *objfile_name(const char *str); +char *objfile_name(const char *str); /** * @brief casl2コマンドのオプション @@ -53,10 +53,13 @@ void addcerrlist_casl2() addcerrlist(ARRAYSIZE(cerr_casl2), cerr_casl2); } -const char *objfile_name(const char *str) +char *objfile_name(const char *name) { const char *default_name = "a.o"; - return (str == NULL) ? default_name : str; + return strdup_chk( + (name == NULL || !name[0]) ? default_name : name, + "objfile_name" + ); } /** @@ -69,9 +72,15 @@ const char *objfile_name(const char *str) */ int main(int argc, char *argv[]) { - int memsize = DEFAULT_MEMSIZE, clocks = DEFAULT_CLOCKS, opt, i, stat = 0; - char *af[argc], *objfile = NULL; - const char *version = PACKAGE_VERSION, *cmdversion = "casl2 of YACASL2 version %s\n"; + int memsize = DEFAULT_MEMSIZE; + int clocks = DEFAULT_CLOCKS; + int opt = 0; + int stat = 0; + int asfilecnt = 0; + char **asfile = NULL; + char *objfile = NULL; + const char *version = PACKAGE_VERSION; + const char *cmdversion = "casl2 of YACASL2 version %s\n"; const char *usage = "Usage: %s [-slLaAtTdmvh] [-oO[]] [-M ] [-C ] FILE1[ FILE2 ...]\n"; @@ -102,11 +111,11 @@ int main(int argc, char *argv[]) asmode.onlyassemble = true; break; case 'o': - objfile = strdup_chk(objfile_name(optarg), "objfile"); + objfile = objfile_name(optarg); break; case 'O': asmode.onlyassemble = true; - objfile = strdup_chk(objfile_name(optarg), "objfile"); + objfile = objfile_name(optarg); break; case 't': execmode.trace = true; @@ -148,11 +157,13 @@ int main(int argc, char *argv[]) } create_cmdtable(HASH_CMDTYPE); /* 命令の名前とタイプがキーのハッシュ表を作成 */ reset(memsize, clocks); /* 仮想マシンCOMET IIのリセット */ - for(i = 0; i < argc - optind; i++) { /* 引数からファイル名配列を取得 */ - af[i] = argv[optind + i]; + asfilecnt = argc - optind; + asfile = calloc_chk(asfilecnt, sizeof(char *), "asfile"); + for(int i = 0; i < asfilecnt; i++) { /* 引数からファイル名配列を取得 */ + asfile[i] = argv[optind + i]; } /* アセンブル */ - if(assemble(i, af, 0) == false || asmode.onlylabel == true) { + if(assemble(asfilecnt, asfile, 0) == false || asmode.onlylabel == true) { goto shutdown; } /* オブジェクトファイル名が指定されている場合は、アセンブル結果をオブジェクトファイルに出力 */ @@ -166,8 +177,9 @@ int main(int argc, char *argv[]) shutdown: shutdown(); /* 仮想マシンCOMET IIのシャットダウン */ casl2fin: - free_cmdtable(HASH_CMDTYPE); FREE(objfile); + FREE(asfile); + free_cmdtable(HASH_CMDTYPE); if(cerr->num > 0) { stat = 1; } diff --git a/src/casl2rev.c b/src/casl2rev.c index e4d381e..af99991 100644 --- a/src/casl2rev.c +++ b/src/casl2rev.c @@ -20,8 +20,10 @@ static struct option longopts[] = { */ int main(int argc, char *argv[]) { - int opt, stat = 0; - const char *version = PACKAGE_VERSION, *cmdversion = "disassemble of YACASL2 version %s\n"; + int opt = 0; + int stat = 0; + const char *version = PACKAGE_VERSION; + const char *cmdversion = "disassemble of YACASL2 version %s\n"; const char *usage = "Usage: %s [-vh] FILE\n"; /* エラーの定義 */ @@ -47,7 +49,7 @@ int main(int argc, char *argv[]) if(argv[optind] == NULL) { setcerr(211, ""); /* object file not specified */ fprintf(stderr, "disassemble error - %d: %s\n", cerr->num, cerr->msg); - exit(1); + goto casl2revfin; } disassemble_file(argv[optind]); /* プログラム実行 */ casl2revfin: diff --git a/src/cerr.c b/src/cerr.c index cd2ace9..455ab5b 100644 --- a/src/cerr.c +++ b/src/cerr.c @@ -6,32 +6,32 @@ void cerr_init() cerr->num = 0; } -CERR *cerr; +CERR *cerr = NULL; CERRLIST *cerrlist = NULL; void addcerrlist(int cerrc, CERR cerrv[]) { - int i; - CERRLIST *p = NULL, *q = malloc_chk(sizeof(CERRLIST), "cerrlist"); + CERRLIST *stat = NULL; + CERRLIST *p = NULL; assert(cerrc > 0 && cerrv != NULL); - for(i = 0; i < cerrc; i++) { + for(int i = 0; i < cerrc; i++) { if(p == NULL) { - p = q; + stat = p = malloc_chk(sizeof(CERRLIST), "cerrlist"); } else { - p = p->next = malloc_chk(sizeof(CERRLIST), "cerrlist.next"); + p = p->next = malloc_chk(sizeof(CERRLIST), "cerrlist->next"); } p->cerr = &cerrv[i]; p->next = NULL; } p->next = cerrlist; - cerrlist = q; + cerrlist = stat; } void printcerrlist() { - CERRLIST *p; + CERRLIST *p = NULL; if(cerrlist == NULL) { puts("error list is null."); @@ -57,7 +57,7 @@ void setcerr(int num, const char *str) char *getcerrmsg(int num) { - CERRLIST *p; + CERRLIST *p = NULL; char *msg = "unknown error"; for(p = cerrlist; p != NULL; p = p->next) { @@ -71,7 +71,8 @@ char *getcerrmsg(int num) void freecerr() { - CERRLIST *p = cerrlist, *q; + CERRLIST *p = NULL; + CERRLIST *q = NULL; /* 現在のエラーメッセージを解放 */ FREE(cerr->msg); diff --git a/src/cmem.c b/src/cmem.c index 722c707..48bc632 100644 --- a/src/cmem.c +++ b/src/cmem.c @@ -49,20 +49,17 @@ char *strndup_chk(const char *s, size_t len, const char *tag) char *strip_end(char *s) { - int i; - i = strlen(s) - 1; - while(i > 0 && (s[i] == '\n' || s[i] == ' ' || s[i] == '\t')) { - s[i--] = '\0'; + for(int i = strlen(s) - 1; i > 0 && (s[i] == '\n' || s[i] == ' ' || s[i] == '\t'); i--) { + s[i] = '\0'; } return s; } char *strip_casl2_comment(char *s) { - int i; bool quoting = false; - for(i = 0; s[i]; i++) { + for(int i = 0; s[i]; i++) { /* 「'」で囲まれた文字列の場合。「''」は無視 */ if(s[i] == '\'' && s[i+1] != '\'' && (quoting == false || s[i-1] != '\'')) { quoting = !quoting; diff --git a/src/comet2.c b/src/comet2.c index 36fb5ed..c821096 100644 --- a/src/comet2.c +++ b/src/comet2.c @@ -40,9 +40,12 @@ void addcerrlist_comet2() */ int main(int argc, char *argv[]) { - int memsize = DEFAULT_MEMSIZE, clocks = DEFAULT_CLOCKS; - int opt, stat = 0; - const char *version = PACKAGE_VERSION, *cmdversion = "comet2 of YACASL2 version %s\n"; + int memsize = DEFAULT_MEMSIZE; + int clocks = DEFAULT_CLOCKS; + int opt = 0; + int stat = 0; + const char *version = PACKAGE_VERSION; + const char *cmdversion = "comet2 of YACASL2 version %s\n"; const char *usage = "Usage: %s [-tTdmvh] [-M ] [-C ] FILE\n"; /* エラーの定義 */ diff --git a/src/comet2monitor.c b/src/comet2monitor.c index 1087285..8d83948 100644 --- a/src/comet2monitor.c +++ b/src/comet2monitor.c @@ -34,9 +34,12 @@ void addcerrlist_comet2monitor() */ int main(int argc, char *argv[]) { - int memsize = DEFAULT_MEMSIZE, clocks = DEFAULT_CLOCKS; - int opt, stat = 0; - const char *version = PACKAGE_VERSION, *cmdversion = "comet2monitor: COMET II machine code monitor of YACASL2 version %s\n"; + int memsize = DEFAULT_MEMSIZE; + int clocks = DEFAULT_CLOCKS; + int opt = 0; + int stat = 0; + const char *version = PACKAGE_VERSION; + const char *cmdversion = "comet2monitor: COMET II machine code monitor of YACASL2 version %s\n"; const char *usage = "Usage: %s [-vh] [-M ] [-C ]\n"; /* エラーの定義 */ diff --git a/src/disassemble.c b/src/disassemble.c index 2904e79..dc40f1d 100644 --- a/src/disassemble.c +++ b/src/disassemble.c @@ -81,8 +81,7 @@ void disassemble_dc(WORD word, WORD pradr); void disassemble_puts_code(int ascol, WORD pradr, int wordc, WORD wordv[]) { - int i; - for(i = 0; i < codecol-ascol; i++){ + for(int i = 0; i < codecol-ascol; i++){ fprintf(stdout, " "); } if(wordc == 1) { @@ -96,6 +95,7 @@ void disassemble_cmd_adr_x(CMDTYPE cmdtype, const char *cmdname, WORD word, WORD WORD x = 0; char *g = NULL; int cnt = 0; + cnt += fprintf(stdout, " %-7s ", cmdname); if(cmdtype == R_ADR_X) { cnt += fprintf(stdout, "%s,", g = grstr((word & 0x00F0) >> 4)); @@ -111,8 +111,11 @@ 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 = NULL, *g1 = NULL, *g2 = NULL; + char *g = NULL; + char *g1 = NULL; + char *g2 = NULL; int cnt = 0; + cnt += fprintf(stdout, " %-7s ", cmdname); if(cmdtype == R1_R2) { g1 = grstr((word & 0x00F0) >> 4); @@ -131,6 +134,7 @@ void disassemble_cmd_r(CMDTYPE cmdtype, const char *cmdname, WORD word, WORD pra void disassemble_dc(WORD word, WORD pradr) { int cnt = 0; + cnt = fprintf(stdout, " DC %-5d ", word); disassemble_puts_code(cnt, pradr, 1, (WORD []){word}); fprintf(stdout, " ::" ); @@ -139,10 +143,11 @@ void disassemble_dc(WORD word, WORD pradr) void disassemble_ds(WORD wcnt, WORD pradr) { - int cnt = 0, i; + int cnt = 0; + cnt = fprintf(stdout, " DS %-5d ", wcnt); disassemble_puts_code(cnt, pradr, 1, (WORD []){0}); - for(i = 0; i < wcnt - 1; i++) { + for(int i = 0; i < wcnt - 1; i++) { fprintf(stdout, "\n"); disassemble_puts_code(0, pradr+1, 1, (WORD []){0}); } @@ -162,7 +167,9 @@ void fungetword(FILE *stream) WORD zero_data_cnt(FILE *stream) { - WORD cnt = 0, word = 0; + WORD cnt = 0; + WORD word = 0; + while(!feof(stream) && word == 0) { word = fgetword(stream); cnt++; @@ -176,10 +183,13 @@ WORD zero_data_cnt(FILE *stream) bool disassemble_file(const char *file) { bool stat = true; - FILE *fp; - WORD i = 0, word, cmd, zcnt; + FILE *fp = NULL; + WORD i = 0; + WORD word = 0; + WORD cmd = 0; + WORD zcnt = 0; CMDTYPE cmdtype = 0; - char *cmdname; + const char *cmdname = NULL; bool inst = true; assert(file != NULL); @@ -227,12 +237,12 @@ bool disassemble_file(const char *file) void disassemble_memory(WORD start, WORD end) { - WORD i, cmd; + WORD cmd = 0; CMDTYPE cmdtype = 0; - char *cmdname; + const char *cmdname = NULL; bool inst = true; - for(i = start; i <= end; i++) { + for(int i = start; i <= end; i++) { cmd = sys->memory[i] & 0xFF00; cmdname = getcmdname(cmd); cmdtype = getcmdtype(cmd); diff --git a/src/dump.c b/src/dump.c index d371f73..858235b 100644 --- a/src/dump.c +++ b/src/dump.c @@ -36,9 +36,10 @@ void dumpmemory(WORD start, WORD end) void dspregister() { - int i; - char *sp, *pr, *fr; - for(i = 0; i < GRSIZE; i++ ) { + char *sp = NULL; + char *pr = NULL; + char *fr = NULL; + for(int i = 0; i < GRSIZE; i++ ) { fprintf(stdout, "#%04X: GR%d: ", sys->cpu->pr, i); print_dumpword(sys->cpu->gr[i], execmode.logical); fprintf(stdout, "\n"); @@ -48,7 +49,7 @@ void dspregister() fprintf(stdout, "#%04X: PR: %6d = #%04X = %s\n", sys->cpu->pr, sys->cpu->pr, sys->cpu->pr, pr = word2bit(sys->cpu->pr)); fprintf(stdout, "#%04X: FR (OF SF ZF): %s\n", - sys->cpu->pr, ((fr = word2bit(sys->cpu->fr)) + 13)); + sys->cpu->pr, ((fr = word2bit(sys->cpu->fr)) + 13)); /* FRは末尾3けたを表示 */ FREE(sp); FREE(pr); diff --git a/src/dumpword.c b/src/dumpword.c index b81d47c..3909890 100644 --- a/src/dumpword.c +++ b/src/dumpword.c @@ -23,9 +23,11 @@ static struct option longopts[] = { int main(int argc, char *argv[]) { bool logicalmode = false; /* レジストリの内容を論理値(0から65535)で表示する場合はtrue */ - int opt, stat = 0; - WORD word; - const char *version = PACKAGE_VERSION, *cmdversion = "dumpword of YACASL2 version %s\n"; + int opt = 0; + int stat = 0; + WORD word = 0; + const char *version = PACKAGE_VERSION; + const char *cmdversion = "dumpword of YACASL2 version %s\n"; const char *usage = "Usage: %s [-alh] WORD\n"; /* エラーの定義 */ diff --git a/src/exec.c b/src/exec.c index 99a01d7..56e58fd 100644 --- a/src/exec.c +++ b/src/exec.c @@ -161,8 +161,8 @@ void setfr(WORD adr) WORD get_r_r1(WORD oprx) { - WORD r; - char *s; + WORD r = 0; + char *s = NULL; if((r = ((oprx & 0x00F0) >>4)) > GRSIZE - 1) { setcerr(205, s = pr2str(sys->cpu->pr)); /* r/r1 in word #1 - not GR */ @@ -174,8 +174,8 @@ WORD get_r_r1(WORD oprx) WORD get_x_r2(WORD oprx) { - WORD x; - char *s; + WORD x = 0; + char *s = NULL; if((x = (oprx & 0x000F)) > GRSIZE - 1) { setcerr(206, s = pr2str(sys->cpu->pr)); /* r/r1 in word #1 - not GR */ @@ -196,8 +196,8 @@ WORD get_adr_x(WORD adr, WORD oprx) WORD get_val_adr_x(WORD adr, WORD oprx) { - WORD a; - char *s; + WORD a = 0; + char *s = NULL; if((a = get_adr_x(adr, oprx)) >= sys->memsize) { setcerr(207, s = pr2str(sys->cpu->pr + 1)); /* address in word #2 - out of memory */ @@ -215,8 +215,8 @@ void addcerrlist_exec() WORD loadassemble(const char *file, WORD start) { - FILE *fp; - WORD end; + FILE *fp = NULL; + WORD end = 0; assert(file != NULL); if((fp = fopen(file, "rb")) == NULL) { @@ -695,8 +695,8 @@ void push() void pop() { assert(sys->cpu->sp > execptr->end); - WORD w; - char *s; + WORD w = 0; + char *s = NULL; if(sys->cpu->sp >= sys->memsize) { setcerr(203, s = pr2str(sys->cpu->pr)); /* Stack Pointer (SP) - stack underflow */ @@ -711,11 +711,11 @@ void pop() void call() { assert(sys->cpu->sp > execptr->end && sys->cpu->sp <= sys->memsize); - WORD w[2]; - w[0] = sys->memory[sys->cpu->pr]; - w[1] = sys->memory[sys->cpu->pr + 1]; sys->memory[--(sys->cpu->sp)] = sys->cpu->pr + 1; - sys->cpu->pr = get_adr_x(w[1], w[0]); + sys->cpu->pr = get_adr_x( + sys->memory[sys->cpu->pr + 1], + sys->memory[sys->cpu->pr] + ); } void ret() @@ -730,10 +730,11 @@ void ret() void svc() { - WORD w[2]; - w[0] = sys->memory[sys->cpu->pr]; - w[1] = sys->memory[sys->cpu->pr + 1]; - switch(get_adr_x(w[1], w[0])) + switch(get_adr_x( + sys->memory[sys->cpu->pr + 1], + sys->memory[sys->cpu->pr] + ) + ) { case 0x0: /* STOP */ execptr->stop = true; @@ -750,9 +751,10 @@ void svc() void exec() { - clock_t clock_begin, clock_end; - void (*cmdptr)(); - char *s; + clock_t clock_begin = 0; + clock_t clock_end = 0; + void (*cmdptr)() = NULL; + char *s = NULL; const char *monmsg = "COMET II machine code monitor. Type ? for help.\n"; create_cmdtable(HASH_CODE); /* 命令のコードとタイプがキーのハッシュ表を作成 */ diff --git a/src/hash.c b/src/hash.c index f6a4558..f4e8b9f 100644 --- a/src/hash.c +++ b/src/hash.c @@ -2,16 +2,15 @@ unsigned hash(int keyc, HKEY *keyv[], int tabsize) { - int i, j; unsigned hashval = 0; enum { HASHNUM = 31 }; - for(i = 0; i < keyc; i++) { + for(int i = 0; i < keyc; i++) { switch(keyv[i]->type) { case CHARS: - for(j = 0; keyv[i]->val.s[j]; j++) { + for(int j = 0; keyv[i]->val.s[j]; j++) { hashval = keyv[i]->val.s[j] + HASHNUM * hashval; } break; diff --git a/src/label.c b/src/label.c index 9d34f77..385922d 100644 --- a/src/label.c +++ b/src/label.c @@ -49,26 +49,26 @@ static CERR cerr_label[] = { }; HKEY *label_hashkey(const char *value) { - HKEY *key; + HKEY *key = NULL; key = malloc_chk(sizeof(HKEY), "label_hashkey"); key->type = CHARS; - key->val.s = strdup_chk(value, "label_hashkey.value"); + key->val.s = strdup_chk(value, "label_hashkey->value"); return key; } unsigned labelhash(const char *prog, const char *label) { - HKEY *keys[2]; - int i = 0, j; - unsigned h; + HKEY *keys[2] = {NULL}; + int i = 0; + unsigned h = 0; if(prog[0]) { keys[i++] = label_hashkey(prog); } keys[i] = label_hashkey(label); h = hash(i+1, keys, LABELTABSIZE); - for(j = 0; j < i + 1; j++) { + for(int j = 0; j < i + 1; j++) { FREE(keys[j]->val.s); FREE(keys[j]); } @@ -89,8 +89,8 @@ void addcerrlist_label() WORD getlabel(const char *prog, const char *label) { assert(prog != NULL && label != NULL); - LABELTAB *p; - LABELARRAY *l; + LABELTAB *p = NULL; + LABELARRAY *l = NULL; for(p = labels[labelhash(prog, label)]; p != NULL; p = p->next) { l = p->label; @@ -106,9 +106,9 @@ WORD getlabel(const char *prog, const char *label) bool addlabel(const char *prog, const char *label, WORD adr) { assert(label != NULL); - LABELTAB *p; - LABELARRAY *l; - unsigned h; + LABELTAB *p = NULL; + LABELARRAY *l = NULL; + unsigned h = 0; /* 登録されたラベルを検索。すでに登録されている場合はエラー発生 */ if(getlabel(prog, label) != 0xFFFF) { @@ -134,19 +134,19 @@ bool addlabel(const char *prog, const char *label, WORD adr) void printlabel() { - int i, s = 0; - LABELTAB *p; - LABELARRAY **l; + int s = 0; + LABELTAB *p = NULL; + LABELARRAY **l = {NULL}; l = calloc_chk(labelcnt, sizeof(LABELARRAY **), "labels"); - for(i = 0; i < LABELTABSIZE; i++) { + for(int i = 0; i < LABELTABSIZE; i++) { for(p = labels[i]; p != NULL; p = p->next) { assert(p->label != NULL); l[s++] = p->label; } } qsort(l, s, sizeof(*l), compare_adr); - for(i = 0; i < s; i++) { + for(int i = 0; i < s; i++) { if(l[i]->prog[0]) { fprintf(stdout, "%s.", l[i]->prog); } @@ -158,7 +158,8 @@ void printlabel() void freelabel() { int i; - LABELTAB *p, *q; + LABELTAB *p = NULL; + LABELTAB *q = NULL; for(i = 0; i < LABELTABSIZE; i++) { for(p = labels[i]; p != NULL; p = q) { diff --git a/src/monitor.c b/src/monitor.c index d02a435..4fc26e7 100644 --- a/src/monitor.c +++ b/src/monitor.c @@ -24,7 +24,7 @@ unsigned adrhash(WORD adr) bool getbps(WORD adr) { - BPSLIST *p; + BPSLIST *p = NULL; for(p = bps[adrhash(adr)]; p != NULL; p = p->next) { if(p->adr == adr) { @@ -36,8 +36,8 @@ bool getbps(WORD adr) bool addbps(WORD adr) { - BPSLIST *p; - unsigned h; + BPSLIST *p = NULL; + unsigned h = 0; /* 登録されたラベルを検索。すでに登録されている場合は終了 */ if(getbps(adr) == true) { @@ -56,8 +56,9 @@ bool addbps(WORD adr) bool delbps(WORD adr) { - BPSLIST *p, *q; - unsigned h; + BPSLIST *p = NULL; + BPSLIST *q = NULL; + unsigned h = 0; bool res = false; p = bps[h = adrhash(adr)]; @@ -88,7 +89,7 @@ bool delbps(WORD adr) void listbps() { int i, cnt = 0; - BPSLIST *p; + BPSLIST *p = NULL; fprintf(stdout, "List of breakpoints\n"); for(i = 0; i < BPSTABSIZE; i++) { @@ -104,7 +105,8 @@ void listbps() void freebpslist(BPSLIST *head) { - BPSLIST *p, *q; + BPSLIST *p = NULL; + BPSLIST *q = NULL; for(p = head; p != NULL; p = q) { q = p->next; FREE(p); @@ -113,8 +115,7 @@ void freebpslist(BPSLIST *head) void freebps() { - int i; - for(i = 0; i < BPSTABSIZE; i++) { + for(int i = 0; i < BPSTABSIZE; i++) { freebpslist(bps[i]); bps[i] = NULL; } @@ -123,8 +124,9 @@ void freebps() MONARGS *monargstok(const char *str) { MONARGS *args = malloc_chk(sizeof(MONARGS), "args"); - char *tok, *p, sepc = ' '; - int i = 0; + char *tok = NULL; + char *p = NULL; + char sepc = ' '; args->argc = 0; if(!str || !str[0]) { @@ -132,11 +134,10 @@ MONARGS *monargstok(const char *str) } tok = p = strdup_chk(str, "argstok.p"); do { - i = strcspn(p, " "); + int i = strcspn(p, " "); sepc = p[i]; args->argv[(args->argc)++] = strndup_chk(p, i, "args->argv[]"); p += i + 1; - i = 0; } while(sepc == ' '); FREE(tok); return args; @@ -144,8 +145,9 @@ MONARGS *monargstok(const char *str) MONCMDLINE *monlinetok(const char *line) { - char *tokens, *p; - int i; + char *tokens = NULL; + char *p = NULL; + int i = 0; MONCMDLINE *moncmdl = NULL; if(!line[0] || line[0] == '\n') { @@ -387,7 +389,7 @@ int monquit() void monitor() { char *buf = NULL; - MONCMDLINE *moncmdl; + MONCMDLINE *moncmdl = NULL; MONCMDTYPE cmdtype = MONREPEAT; do { diff --git a/src/struct.c b/src/struct.c index 72c1c9c..bb482f8 100644 --- a/src/struct.c +++ b/src/struct.c @@ -4,12 +4,12 @@ /** * COMET IIの仮想実行マシンシステム */ -SYSTEM *sys; +SYSTEM *sys = NULL; /** * プログラム実行時の開始と終了のアドレス */ -EXECPTR *execptr; +EXECPTR *execptr = NULL; /** * システムCOMET IIの命令表 @@ -70,7 +70,7 @@ enum { /** * ハッシュ表 */ -static CMDTAB *cmdtab[HASH_MAX][CMDTABSIZE]; +static CMDTAB *cmdtab[HASH_MAX][CMDTABSIZE] = {NULL}; /** * 命令の名前とタイプからハッシュ値を生成する @@ -87,8 +87,8 @@ unsigned hash_code(WORD code); */ unsigned hash_cmdtype(const char *cmd, CMDTYPE type) { - HKEY *keys[2]; - unsigned hashval; + HKEY *keys[2] = {NULL}; + unsigned hashval = 0; /* 命令名を設定 */ keys[0] = malloc_chk(sizeof(HKEY), "hash_cmdtype.keys[0]"); @@ -114,9 +114,8 @@ bool create_cmdtable(CMDTAB_HASH hash) { CMDTAB *p = NULL; unsigned hashval; - int i; - for(i = 0; i < comet2cmdsize; i++) { + for(int i = 0; i < comet2cmdsize; i++) { p = malloc_chk(sizeof(CMDTAB), "create_cmdtable.p"); p->cmd = &comet2cmd[i]; if(hash == HASH_CMDTYPE) { @@ -135,10 +134,10 @@ bool create_cmdtable(CMDTAB_HASH hash) */ void free_cmdtable(CMDTAB_HASH hash) { - int i; - CMDTAB *p, *q; + CMDTAB *p = NULL; + CMDTAB *q = NULL; - for(i = 0; i < CMDTABSIZE; i++) { + for(int i = 0; i < CMDTABSIZE; i++) { for(p = cmdtab[hash][i]; p != NULL; p = q) { q = p->next; FREE(p); @@ -153,7 +152,7 @@ void free_cmdtable(CMDTAB_HASH hash) */ WORD getcmdcode(const char *cmd, CMDTYPE type) { - CMDTAB *p; + CMDTAB *p = NULL; WORD w = 0xFFFF; assert(cmd != NULL); @@ -171,8 +170,8 @@ WORD getcmdcode(const char *cmd, CMDTYPE type) */ unsigned hash_code(WORD code) { - HKEY *keys[1]; - unsigned h; + HKEY *keys[1] = {NULL}; + unsigned h = 0; /* 命令コードを設定 */ keys[0] = malloc_chk(sizeof(HKEY), "hash_code.key"); @@ -188,7 +187,7 @@ unsigned hash_code(WORD code) */ const void (*getcmdptr(WORD code)) { - CMDTAB *t; + CMDTAB *t = NULL; const void *ptr = NULL; for(t = cmdtab[HASH_CODE][hash_code(code)]; t != NULL; t = t->next) { @@ -205,7 +204,7 @@ const void (*getcmdptr(WORD code)) */ CMDTYPE getcmdtype(WORD code) { - CMDTAB *t; + CMDTAB *t = NULL; CMDTYPE type = NONE; for(t = cmdtab[HASH_CODE][hash_code(code)]; t != NULL; t = t->next) { @@ -222,7 +221,7 @@ CMDTYPE getcmdtype(WORD code) */ char *getcmdname(WORD code) { - CMDTAB *t; + CMDTAB *t = NULL; char *cmd = NULL; for(t = cmdtab[HASH_CODE][hash_code(code)]; t != NULL; t = t->next) { @@ -241,7 +240,9 @@ char *getcmdname(WORD code) char *grstr(WORD word) { assert(word <= 7); - char *str = malloc_chk(3 + 1, "grstr.str"); + char *str = NULL; + + str = malloc_chk(3 + 1, "grstr.str"); sprintf(str, "GR%d", word); return str; } @@ -251,8 +252,6 @@ char *grstr(WORD word) */ void reset(int memsize, int clocks) { - int i; - sys = malloc_chk(sizeof(SYSTEM), "sys"); /* メモリサイズを設定 */ sys->memsize = memsize; @@ -262,7 +261,7 @@ void reset(int memsize, int clocks) sys->memory = calloc_chk(sys->memsize, sizeof(WORD), "memory"); /* CPUを初期化 */ sys->cpu = malloc_chk(sizeof(CPU), "cpu"); - for(i = 0; i < GRSIZE; i++) { /* 汎用レジスタ */ + for(int i = 0; i < GRSIZE; i++) { /* 汎用レジスタ */ sys->cpu->gr[i] = 0x0; } sys->cpu->sp = sys->memsize; /* スタックポインタ */ diff --git a/src/word.c b/src/word.c index e11b730..bb564cf 100644 --- a/src/word.c +++ b/src/word.c @@ -29,7 +29,7 @@ WORD n2word(const char *str) { assert(isdigit(str[0]) || str[0] == '-'); - char *check; + char *check = NULL; int n; /* WORD値に変換 */ n = strtol(str, &check, 10); @@ -48,8 +48,8 @@ WORD h2word(const char *str) { assert(str[0] == '#'); - WORD w = 0x0; - char *check; + WORD w = 0; + char *check = NULL; str++; if(*str == '-' || strlen(str) > 4) { setcerr(116, str-1); /* out of hex range */ @@ -140,9 +140,10 @@ char *word2bit(const WORD word) MAXLEN = 16, /* WORD値を2進数で表したときの最大桁数 */ }; WORD mask = 0x8000; - char *bit = malloc_chk(MAXLEN + 1, "word2bit.bit"); + char *bit = NULL; int i = 0; + bit = malloc_chk(MAXLEN + 1, "word2bit.bit"); do { bit[i++] = (word & mask) ? '1' : '0'; } while((mask >>= 1) > 0); -- 2.18.0