From e24285f0509319319aef28a188b7c01ba7e22bf1 Mon Sep 17 00:00:00 2001 From: j8takagi Date: Sun, 28 Feb 2010 10:32:40 +0900 Subject: [PATCH] =?utf8?q?=E3=82=B3=E3=83=9E=E3=83=B3=E3=83=89=E3=83=8F?= =?utf8?q?=E3=83=83=E3=82=B7=E3=83=A5=E8=A1=A8=E3=82=92=E8=A4=87=E6=95=B0?= =?utf8?q?=E5=9B=9E=E4=BD=9C=E6=88=90=E3=81=97=E3=81=A6=E3=81=84=E3=81=9F?= =?utf8?q?=E6=A7=8B=E9=80=A0=E3=83=90=E3=82=B0=E3=82=92=E4=BF=AE=E6=AD=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- include/casl2.h | 4 ++-- src/assemble.c | 5 +---- src/casl2.c | 50 +++++++++++++++++++++++++------------------------ src/cmd.c | 11 ++++++++--- src/exec.c | 6 ++++-- src/label.c | 4 ++-- 6 files changed, 43 insertions(+), 37 deletions(-) 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) { -- 2.18.0