From: j8takagi Date: Sun, 28 Feb 2010 01:32:40 +0000 (+0900) Subject: コマンドハッシュ表を複数回作成していた構造バグを修正 X-Git-Tag: v0.1~10 X-Git-Url: https://j8takagi.net/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=e24285f0509319319aef28a188b7c01ba7e22bf1;p=YACASL2.git コマンドハッシュ表を複数回作成していた構造バグを修正 --- diff --git a/include/casl2.h b/include/casl2.h index 96363ea..23607fd 100644 --- a/include/casl2.h +++ b/include/casl2.h @@ -93,8 +93,8 @@ typedef struct _CMDCODETAB { CMDCODEARRAY *cca; } CMDCODETAB; -extern CMDCODETAB *cmdtype_code[]; -extern CMDCODETAB *code_type[]; +extern CMDCODETAB **cmdtype_code; +extern CMDCODETAB **code_type; /* 命令と命令タイプがキーのハッシュ表を作成する */ bool create_cmdtype_code(); diff --git a/src/assemble.c b/src/assemble.c index 9fbfa96..616b256 100644 --- a/src/assemble.c +++ b/src/assemble.c @@ -27,7 +27,7 @@ WORD getgr(const char *str, bool is_x) return 0xFFFF; } r = (WORD)(*(str+2) - '0'); - /* 指標レジスタとして用いることはできない */ + /* GR0は指標レジスタとして用いることができない */ if(is_x == true && r == 0x0) { setcerr(120, NULL); /* GR0 in operand x */ return 0x0; @@ -412,9 +412,6 @@ bool assemble(const char *file, PASS pass) char *line; FILE *fp; - if(pass == FIRST && create_cmdtype_code() == false) { - return false; - } if((fp = fopen(file, "r")) == NULL) { perror(file); return false; diff --git a/src/casl2.c b/src/casl2.c index c3a5550..03c61fc 100644 --- a/src/casl2.c +++ b/src/casl2.c @@ -50,6 +50,7 @@ CERRARRAY cerr[] = { { 123, "unclosed quote" }, { 124, "more than one character in literal" }, { 125, "not GR in operand x" }, + { 126, "source file is not specified" }, { 201, "execute - out of COMET II memory" }, { 202, "SVC input - out of Input memory" }, { 203, "SVC output - out of COMET II memory" }, @@ -97,38 +98,38 @@ int main(int argc, char *argv[]) while((opt = getopt_long(argc, argv, "tTdslLao::O::AM:C:h", longopts, NULL)) != -1) { switch(opt) { case 's': - (&asmode)->src = true; + asmode.src = true; break; case 'l': - (&asmode)->label = true; + asmode.label = true; break; case 'L': - (&asmode)->label = true; - (&asmode)->onlylabel = true; + asmode.label = true; + asmode.onlylabel = true; break; case 'a': - (&asmode)->asdetail = true; + asmode.asdetail = true; break; case 'A': - (&asmode)->asdetail = true; - (&asmode)->onlyassemble = true; + asmode.asdetail = true; + asmode.onlyassemble = true; break; case 'o': objfile = strdup(objfile_name(optarg)); break; case 'O': - (&asmode)->onlyassemble = true; + asmode.onlyassemble = true; objfile = strdup(objfile_name(optarg)); break; case 't': - (&execmode)->trace = true; + execmode.trace = true; break; case 'T': - (&execmode)->trace = true; - (&execmode)->logical = true; + execmode.trace = true; + execmode.logical = true; break; case 'd': - (&execmode)->dump = true; + execmode.dump = true; break; case 'M': memsize = atoi(optarg); @@ -146,13 +147,16 @@ int main(int argc, char *argv[]) } /* ソースファイルが指定されていない場合は終了 */ if(argv[optind] == NULL) { - fprintf(stderr, "source file is not specified\n"); - exit(-1); + setcerr(126, NULL); /* source file is not specified */ + goto casl2err; } /* COMET II仮想マシンのリセット */ reset(); /* アセンブル。ラベル表作成のため、2回行う */ for(pass = FIRST; pass <= SECOND; pass++) { + if(pass == FIRST && create_cmdtype_code() == false) { + goto casl2err; + } for(i = optind; i < argc; i++) { /* データの格納開始位置 */ if(pass == FIRST) { @@ -160,24 +164,19 @@ int main(int argc, char *argv[]) } else if(pass == SECOND) { ptr = beginptr[i]; } - if((&execmode)->trace == true || (&execmode)->dump == true || - (&asmode)->src == true || (&asmode)->label == true || - (&asmode)->asdetail == true) + if(execmode.trace == true || execmode.dump == true || asmode.src == true || + asmode.label == true || asmode.asdetail == true) { fprintf(stdout, "\nAssemble %s (%d)\n", argv[i], pass); } if((status = assemble(argv[i], pass)) == false) { - freelabel(); /* ラベル表の解放 */ - if(cerrno > 0) { - freecerr(); /* エラーの解放 */ - } exit(-1); } } - if(pass == FIRST && (&asmode)->label == true) { + if(pass == FIRST && asmode.label == true) { fprintf(stdout, "\nLabel::::\n"); printlabel(); - if((&asmode)->onlylabel == true) { + if(asmode.onlylabel == true) { return 0; } } @@ -187,7 +186,7 @@ int main(int argc, char *argv[]) if(objfile != NULL) { outassemble(objfile); } - if((&asmode)->onlyassemble == false) { + if(asmode.onlyassemble == false) { exec(); /* プログラム実行 */ } } @@ -196,4 +195,7 @@ int main(int argc, char *argv[]) exit(-1); } return 0; +casl2err: + fprintf(stderr, "Casl2 error - %d: %s\n", cerrno, cerrmsg); + exit(-1); } diff --git a/src/cmd.c b/src/cmd.c index d684d06..7c1e803 100644 --- a/src/cmd.c +++ b/src/cmd.c @@ -42,7 +42,8 @@ CMDCODEARRAY cmdcodearray[] = { }; int cmdcodesize = ARRAYSIZE(cmdcodearray); -CMDCODETAB *cmdtype_code[ARRAYSIZE(cmdcodearray)], *code_type[ARRAYSIZE(cmdcodearray)]; +int hashtabsize; +CMDCODETAB **cmdtype_code, **code_type; /* 命令と命令タイプからハッシュ値を生成する */ unsigned hash_cmdtype(const char *cmd, CMDTYPE type) { @@ -67,8 +68,10 @@ bool create_cmdtype_code() unsigned hashval; int i; + hashtabsize = cmdcodesize; + cmdtype_code = malloc(cmdcodesize * sizeof(CMDCODETAB *)); for(i = 0; i < cmdcodesize; i++) { - np = malloc(sizeof(CMDCODETAB *)); + np = malloc(sizeof(CMDCODETAB)); if(np == NULL) { setcerr(122, NULL); /* cannot create hash table */ return false; @@ -132,8 +135,10 @@ bool create_code_type() unsigned hashval; int i; + hashtabsize = cmdcodesize; + code_type = malloc(cmdcodesize * sizeof(CMDCODETAB *)); for(i = 0; i < cmdcodesize; i++) { - if((np = malloc(sizeof(CMDCODETAB *))) == NULL) { + if((np = malloc(sizeof(CMDCODETAB))) == NULL) { setcerr(122, NULL); /* cannot create hash table */ return false; } diff --git a/src/exec.c b/src/exec.c index 9f01dbd..3e4b1b9 100644 --- a/src/exec.c +++ b/src/exec.c @@ -269,7 +269,7 @@ void exec() { WORD op, r_r1, x_r2, val; CMDTYPE cmdtype; - char *errpr = malloc(8); + char *errpr = malloc(CERRSTRSIZE + 1); clock_t clock_begin, clock_end; if((&execmode)->trace) { @@ -466,7 +466,9 @@ void exec() do { clock_end = clock(); } while(clock_end - clock_begin < CLOCKS_PER_SEC / clocks); -/* printf("PR:%04X; time: %f\n", PR, (double)((clock_end - clock_begin) * CLOCKS_PER_SEC)); */ + #if 0 + printf("PR:%04X; time: %f\n", PR, (double)((clock_end - clock_begin) * CLOCKS_PER_SEC)); + #endif } execerr: fprintf(stderr, "Execute error - %d: %s\n", cerrno, cerrmsg); diff --git a/src/label.c b/src/label.c index 120223c..8d3aa4b 100644 --- a/src/label.c +++ b/src/label.c @@ -75,8 +75,8 @@ int compare_adr(const void *a, const void *b) void printlabel() { int i, asize = 0; - LABELTAB *np = malloc(sizeof(LABELTAB *)); - LABELARRAY **ar = malloc(labelcnt * sizeof(LABELARRAY **)); + LABELTAB *np; + LABELARRAY *ar[labelcnt]; for(i = 0; i < LABELTABSIZE; i++) { for(np = labels[i]; np != NULL; np = np->next) {