From: j8takagi Date: Thu, 7 Apr 2011 02:49:35 +0000 (+0900) Subject: アセンブルと実行の流れを整理 X-Git-Tag: v0.1p35~5 X-Git-Url: http://j8takagi.net/cgi-bin/gitweb.cgi?p=YACASL2.git;a=commitdiff_plain;h=93f3bbbc043d2883610bd979556e0bae42e5c8b2 アセンブルと実行の流れを整理 --- diff --git a/include/assemble.h b/include/assemble.h index 68e86b0..5d187c4 100644 --- a/include/assemble.h +++ b/include/assemble.h @@ -180,7 +180,7 @@ void addcerrlist_assemble(); * 指定された名前のファイルをアセンブル * 2回実行される */ -bool assemble(const char *file, PASS pass); +void assemble(const char *file, PASS pass); /** * 引数で指定したファイルにアセンブル結果を書込 diff --git a/include/exec.h b/include/exec.h index 1750c96..2f4118f 100644 --- a/include/exec.h +++ b/include/exec.h @@ -239,7 +239,7 @@ void svc(); /** * COMET II仮想マシンの実行 */ -bool exec(); +void exec(); /** * COMET IIのメモリを表示 diff --git a/src/assemble.c b/src/assemble.c index 67664ef..366ccf9 100644 --- a/src/assemble.c +++ b/src/assemble.c @@ -636,7 +636,7 @@ void addcerrlist_assemble() * 指定された名前のファイルをアセンブル * 2回実行される */ -bool assemble(const char *file, PASS pass) +void assemble(const char *file, PASS pass) { int lineno = 0; bool status = true; @@ -645,7 +645,8 @@ bool assemble(const char *file, PASS pass) if((fp = fopen(file, "r")) == NULL) { perror(file); - return false; + setcerr(127, NULL); + return; } while(fgets(line, LINESIZE, fp)) { lineno++; @@ -665,7 +666,6 @@ bool assemble(const char *file, PASS pass) } FREE(line); fclose(fp); - return status; } /** diff --git a/src/casl2.c b/src/casl2.c index c272d05..541992a 100644 --- a/src/casl2.c +++ b/src/casl2.c @@ -48,22 +48,63 @@ void addcerrlist_casl2() /** * アセンブル結果を書き込むファイルの名前 */ -static const char *objfile_name(const char *str) +const char *objfile_name(const char *str) { const char *default_name = "a.o"; return (str == NULL) ? default_name : str; } +/** + * アセンブルを実行 + */ +void doassemble(int filec, char *filev[]) +{ + int i; + PASS pass; + WORD bp[filec]; + + create_cmdtype_code(); /* 命令の名前とタイプがキーのハッシュ表を作成 */ + asptr = malloc_chk(sizeof(ASPTR), "asptr"); /* アセンブル時のプロパティ用の領域確保 */ + /* アセンブル。ラベル表作成のため、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); + } + assemble(filev[i], pass); + if(cerr->num > 0) { + goto assemblefin; + } + } + if(pass == FIRST && asmode.label == true) { + fprintf(stdout, "\nLabel::::\n"); + printlabel(); + if(asmode.onlylabel == true) { + break; + } + } + } +assemblefin: + freelabel(); /* ラベルハッシュ表を解放 */ + free_cmdtype_code(); /* 命令の名前とタイプがキーのハッシュ表を解放 */ + FREE(asptr); /* アセンブル時のプロパティを解放 */ +} + /** * casl2コマンドのメイン */ int main(int argc, char *argv[]) { - int memsize = DEFAULT_MEMSIZE, clocks = DEFAULT_CLOCKS; - int status = 0, opt, i; - PASS pass; - bool res = false; - WORD beginptr[argc]; + int memsize = DEFAULT_MEMSIZE, clocks = DEFAULT_CLOCKS, opt, i, status; + char *af[argc]; char *objfile = NULL; const char *usage = "Usage: %s [-slLaAtTdh] [-oO[]] [-M ] [-C ] FILE1[ FILE2 ...]\n"; @@ -129,60 +170,26 @@ int main(int argc, char *argv[]) fprintf(stderr, "CASL2 error - %d: %s\n", cerr->num, cerr->msg); exit(-1); } - /* COMET II仮想マシンのリセット */ - reset(memsize, clocks); - /* アセンブル。ラベル表作成のため、2回行う */ - for(pass = FIRST; pass <= SECOND; pass++) { - if(pass == FIRST) { - create_cmdtype_code(); /* 命令の名前とタイプがキーのハッシュ表を作成 */ - asptr = malloc_chk(sizeof(ASPTR), "asptr"); /* アセンブル時のプロパティ用の領域確保 */ - } - for(i = optind; i < argc; i++) { - /* データの格納開始位置 */ - if(pass == FIRST) { - beginptr[i] = asptr->ptr; - } else if(pass == SECOND) { - asptr->ptr = beginptr[i]; - } - 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((res = assemble(argv[i], pass)) == false) { - freecerr(); /* エラーの解放 */ - exit(-1); - } - } - if(pass == FIRST && asmode.label == true) { - fprintf(stdout, "\nLabel::::\n"); - printlabel(); - if(asmode.onlylabel == true) { - return 0; - } - } - if(pass == SECOND) { - freelabel(); /* ラベルハッシュ表を解放 */ - free_cmdtype_code(); /* 命令の名前とタイプがキーのハッシュ表を解放 */ - FREE(asptr); /* アセンブル時のプロパティを解放 */ - } + reset(memsize, clocks); /* 仮想マシンCOMET IIのリセット */ + for(i = 0; i < argc - optind; i++) { /* 引数からファイル名配列を取得 */ + af[i] = argv[optind + i]; } - if(res == true) { - if(objfile != NULL) { - outassemble(objfile); - FREE(objfile); - } - if(asmode.onlyassemble == false) { - create_code_type(); /* 命令のコードとタイプがキーのハッシュ表を作成 */ - res = exec(); /* プログラム実行 */ - free_code_type(); /* 命令のコードとタイプがキーのハッシュ表を解放 */ - } + doassemble(i, af); /* アセンブル */ + if(asmode.onlylabel == true || cerr->num > 0) { + goto casl2fin; + } + /* オブジェクトファイル名が指定されている場合は、アセンブル結果をオブジェクトファイルに出力 */ + if(objfile != NULL) { + outassemble(objfile); + FREE(objfile); } - /* COMET II仮想マシンのシャットダウン */ - shutdown(); - if(cerr->num > 0) { - status = -1; + /* onlyassembleモード以外の場合、仮想マシンCOMET IIを実行 */ + if(asmode.onlyassemble == false) { + exec(); /* 仮想マシンCOMET IIの実行 */ } - freecerr(); /* エラーの解放 */ +casl2fin: + shutdown(); /* 仮想マシンCOMET IIのシャットダウン */ + status = (cerr->num == 0) ? 0 : -1; + freecerr(); /* エラーの解放 */ return status; } diff --git a/src/comet2.c b/src/comet2.c index af4bfb8..f6d9fb1 100644 --- a/src/comet2.c +++ b/src/comet2.c @@ -70,15 +70,11 @@ int main(int argc, char *argv[]) reset(memsize, clocks); execptr->start = 0; if(loadassemble(argv[optind]) == true) { - create_code_type(); /* タイプがキーの命令ハッシュ表を作成 */ exec(); /* プログラム実行 */ - free_code_type(); /* タイプがキーの命令ハッシュ表を解放 */ } /* COMET II仮想マシンのシャットダウン */ shutdown(); - if(cerr->num > 0) { - status = -1; - } + status = (cerr->num == 0) ? 0 : -1; /* エラーの解放 */ freecerr(); return status; diff --git a/src/exec.c b/src/exec.c index 744f27c..61faee3 100644 --- a/src/exec.c +++ b/src/exec.c @@ -831,23 +831,24 @@ void svc() /** * 仮想マシンCOMET IIの実行 */ -bool exec() +void exec() { clock_t clock_begin, clock_end; void (*cmdptr)(); + create_code_type(); /* 命令のコードとタイプがキーのハッシュ表を作成 */ if(execmode.trace == true) { fprintf(stdout, "\nExecuting machine codes\n"); } /* 機械語の実行 */ for (sys->cpu->pr = execptr->start; ; ) { - clock_begin = clock(); /* クロック周波数設定のため、実行開始時間を格納 */ - if(execmode.dump || execmode.trace) { /* traceまたはdumpオプション指定時、改行を出力 */ - if(execmode.trace){ /* traceオプション指定時、レジスタを出力 */ + clock_begin = clock(); /* クロック周波数設定のため、実行開始時間を格納 */ + if(execmode.dump || execmode.trace) { /* traceまたはdumpオプション指定時、改行を出力 */ + if(execmode.trace) { /* traceオプション指定時、レジスタを出力 */ fprintf(stdout, "#%04X: Register::::\n", sys->cpu->pr); dspregister(); } - if(execmode.dump){ /* dumpオプション指定時、メモリを出力 */ + if(execmode.dump) { /* dumpオプション指定時、メモリを出力 */ fprintf(stdout, "#%04X: Memory::::\n", sys->cpu->pr); dumpmemory(); } @@ -862,19 +863,19 @@ bool exec() } else if(sys->cpu->sp > sys->memsize) { setcerr(203, pr2str(sys->cpu->pr)); /* Stack Pointer (SP) - stack underflow */ } - goto execerr; + goto execfin; } /* コードから命令を取得 */ /* 取得できない場合はエラー終了 */ if((cmdptr = getcmdptr(sys->memory[sys->cpu->pr] & 0xFF00)) == NULL) { setcerr(204, pr2str(sys->cpu->pr)); /* OP in word #1 - not command code */ - goto execerr; + goto execfin; } /* 命令の実行 */ (*cmdptr)(); /* エラー発生時はエラー終了 */ if(cerr->num > 0) { - goto execerr; + goto execfin; } /* 終了フラグがtrueの場合は、正常終了 */ if(execptr->stop == true) { @@ -885,8 +886,9 @@ bool exec() clock_end = clock(); } while(clock_end - clock_begin < CLOCKS_PER_SEC / sys->clocks); } - return true; -execerr: - fprintf(stderr, "Execute error - %d: %s\n", cerr->num, cerr->msg); - return false; +execfin: + free_code_type(); /* 命令のコードとタイプがキーのハッシュ表を解放 */ + if(cerr->num > 0) { + fprintf(stderr, "Execute error - %d: %s\n", cerr->num, cerr->msg); + } }