X-Git-Url: http://j8takagi.net/cgi-bin/gitweb.cgi?p=YACASL2.git;a=blobdiff_plain;f=src%2Fcasl2.c;h=14f372cf4b575f2068b31bc9e8e9ff4d62415606;hp=40001ead331321c7f7478ea1d8ddc51a268631d2;hb=a639337a9aa30a059c1695ab1701b8714fd26193;hpb=288d61424576d1aae956ea8e4b5fb89dc25909db diff --git a/src/casl2.c b/src/casl2.c index 40001ea..14f372c 100644 --- a/src/casl2.c +++ b/src/casl2.c @@ -1,10 +1,17 @@ -#include "casl2.h" -#include "assemble.h" -#include "exec.h" +#include +#include + #define _GNU_SOURCE #include -/* casl2コマンドのオプション */ +#include "cmem.h" +#include "cerr.h" +#include "assemble.h" +#include "exec.h" + +/** + * casl2コマンドのオプション + */ static struct option longopts[] = { { "source", no_argument, NULL, 's' }, { "label", no_argument, NULL, 'l' }, @@ -23,35 +30,25 @@ static struct option longopts[] = { { 0, 0, 0, 0 }, }; -/* casl2のエラー定義 */ +/** + * casl2のエラー定義 + */ CERR cerr_casl2[] = { { 126, "no source file" }, }; -bool addcerrlist_casl2() -{ - return addcerrlist(sizeof(cerr_casl2), cerr_casl2); -} - -/* 指定されたファイルにアセンブル結果を書込 */ -void outassemble(const char *file) { - FILE *fp; - - if((fp = fopen(file, "w")) == NULL) { - perror(file); - exit(-1); - } - fwrite(sys->memory, sizeof(WORD), prog->end, fp); - fclose(fp); -} -/* アセンブル結果を書き込むファイルの名前 */ -const char *objfile_name(const char *str) +/** + * アセンブル結果を書き込むファイルの名前 + */ +static const char *objfile_name(const char *str) { const char *default_name = "a.o"; return (str == NULL) ? default_name : str; } -/* casl2コマンドのメイン */ +/** + * casl2コマンドのメイン + */ int main(int argc, char *argv[]) { int memsize = DEFAULT_MEMSIZE, clocks = DEFAULT_CLOCKS; @@ -64,7 +61,9 @@ int main(int argc, char *argv[]) "Usage: %s [-slLaAtTdh] [-oO[]] [-M ] [-C ] FILE1[ FILE2 ...]\n"; cerr_init(); - addcerrlist_casl2(); + addcerrlist(ARRAYSIZE(cerr_casl2), cerr_casl2); + addcerrlist_assemble(); + addcerrlist_exec(); /* オプションの処理 */ while((opt = getopt_long(argc, argv, "tTdslLao::O::AM:C:h", longopts, NULL)) != -1) { switch(opt) { @@ -127,23 +126,24 @@ int main(int argc, char *argv[]) /* アセンブル。ラベル表作成のため、2回行う */ for(pass = FIRST; pass <= SECOND; pass++) { if(pass == FIRST) { - create_cmdtype_code(); /* 命令と命令タイプがキーのハッシュ表を作成 */ - asprop = malloc_chk(sizeof(ASPROP), "asprop"); + create_cmdtype_code(); /* 命令の名前とタイプがキーのハッシュ表を作成 */ + asptr = malloc_chk(sizeof(asptr), "asptr"); /* アセンブル時のプロパティ用の領域確保 */ } for(i = optind; i < argc; i++) { /* データの格納開始位置 */ if(pass == FIRST) { - beginptr[i] = asprop->ptr; + beginptr[i] = asptr->ptr; } else if(pass == SECOND) { - asprop->ptr = beginptr[i]; + asptr->ptr = beginptr[i]; } - asprop->prog = NULL; + asptr->prog = NULL; 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); } } @@ -155,18 +155,21 @@ int main(int argc, char *argv[]) } } if(pass == SECOND) { - free_cmdtype_code(); /* 命令と命令タイプがキーのハッシュ表を解放 */ freelabel(); /* ラベルハッシュ表を解放 */ + free_chk(asptr->prog, "asptr.prog"); /* プログラム名を解放 */ + free_chk(asptr, "asptr"); /* アセンブル時のプロパティを解放 */ + free_cmdtype_code(); /* 命令の名前とタイプがキーのハッシュ表を解放 */ } } if(res == true) { if(objfile != NULL) { outassemble(objfile); + free_chk(objfile, "objfile"); } if(asmode.onlyassemble == false) { - create_code_type(); /* 命令と命令タイプがキーのハッシュ表を作成 */ + create_code_type(); /* 命令のコードとタイプがキーのハッシュ表を作成 */ res = exec(); /* プログラム実行 */ - free_code_type(); /* 命令と命令タイプがキーのハッシュ表を解放 */ + free_code_type(); /* 命令のコードとタイプがキーのハッシュ表を解放 */ } } /* COMET II仮想マシンのシャットダウン */ @@ -174,7 +177,6 @@ int main(int argc, char *argv[]) if(cerr->num > 0) { status = -1; } - /* エラーの解放 */ - freecerr(); + freecerr(); /* エラーの解放 */ return status; }