X-Git-Url: http://j8takagi.net/cgi-bin/gitweb.cgi?p=YACASL2.git;a=blobdiff_plain;f=src%2Fcasl2.c;h=fb4290a9e4f33e0f47b0f644c3bb58d6d52023b5;hp=7569f92a06acde918f7d347171a98ffdfe9bef3b;hb=2f0b91ab1a54973a084e1609b46a1432c8b8e334;hpb=555c5e8b851becc08ba661a9cb19f617d5a00c12 diff --git a/src/casl2.c b/src/casl2.c index 7569f92..fb4290a 100644 --- a/src/casl2.c +++ b/src/casl2.c @@ -1,10 +1,25 @@ -#include "casl2.h" +#include "package.h" #include "assemble.h" -#include "exec.h" -#define _GNU_SOURCE -#include -/* casl2コマンドのオプション */ +/** + * @brief CASL IIのエラーをエラーリストに追加 + * + * @return なし + */ +void addcerrlist_casl2(); + +/** + * @brief アセンブル結果を書き込むファイルの名前 + * + * @return ファイル名 + * + * @param *str ファイル名 + */ +const char *objfile_name(const char *str); + +/** + * @brief casl2コマンドのオプション + */ static struct option longopts[] = { { "source", no_argument, NULL, 's' }, { "label", no_argument, NULL, 'l' }, @@ -17,56 +32,50 @@ static struct option longopts[] = { { "tracearithmetic", no_argument, NULL, 't' }, { "tracelogical", no_argument, NULL, 'T' }, { "dump", no_argument, NULL, 'd' }, + { "monitor", no_argument, NULL, 'm' }, { "memorysize", required_argument, NULL, 'M' }, { "clocks", required_argument, NULL, 'C' }, + { "version", no_argument, NULL, 'v' }, { "help", no_argument, NULL, 'h' }, { 0, 0, 0, 0 }, }; -/* casl2のエラー定義 */ +/** + * @brief 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); +void addcerrlist_casl2() +{ + addcerrlist(ARRAYSIZE(cerr_casl2), cerr_casl2); } -/* アセンブル結果を書き込むファイルの名前 */ const char *objfile_name(const char *str) { const char *default_name = "a.o"; return (str == NULL) ? default_name : str; } -/* casl2コマンドのメイン */ +/** + * @brief casl2コマンドのメイン + * + * @return 正常終了時は0、エラー発生時は1 + * + * @param argc コマンドライン引数の数 + * @param *argv[] コマンドライン引数の配列 + */ 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]; - char *objfile = NULL; + 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"; const char *usage = - "Usage: %s [-slLaAtTdh] [-oO[]] [-M ] [-C ] FILE1[ FILE2 ...]\n"; + "Usage: %s [-slLaAtTdmvh] [-oO[]] [-M ] [-C ] FILE1[ FILE2 ...]\n"; - cerr_init(); - addcerrlist_casl2(); /* オプションの処理 */ - while((opt = getopt_long(argc, argv, "tTdslLao::O::AM:C:h", longopts, NULL)) != -1) { + while((opt = getopt_long(argc, argv, "tTdslLmao::O::AM:C:vh", longopts, NULL)) != -1) { switch(opt) { case 's': asmode.src = true; @@ -102,82 +111,60 @@ int main(int argc, char *argv[]) case 'd': execmode.dump = true; break; + case 'm': + execmode.step = true; + break; case 'M': memsize = atoi(optarg); break; case 'C': clocks = atoi(optarg); break; + case 'v': + fprintf(stdout, cmdversion, version); + return 0; case 'h': fprintf(stdout, usage, argv[0]); return 0; case '?': fprintf(stderr, usage, argv[0]); - exit(-1); + exit(1); } } + + /* エラーの定義 */ + cerr_init(); + addcerrlist_casl2(); + addcerrlist_assemble(); + addcerrlist_exec(); + /* ソースファイルが指定されていない場合は終了 */ if(argv[optind] == NULL) { - setcerr(126, NULL); /* no source file */ + setcerr(126, ""); /* no source file */ fprintf(stderr, "CASL2 error - %d: %s\n", cerr->num, cerr->msg); - exit(-1); + freecerr(); /* エラーの解放 */ + exit(1); } - /* COMET II仮想マシンのリセット */ - reset(memsize, clocks); - /* アセンブル。ラベル表作成のため、2回行う */ - for(pass = FIRST; pass <= SECOND; pass++) { - if(pass == FIRST) { - create_cmdtype_code(); /* 命令と命令タイプがキーのハッシュ表を作成 */ - asprop = malloc_chk(sizeof(ASPROP), "asprop"); /* アセンブル時のプロパティ用の領域確保 */ - } - for(i = optind; i < argc; i++) { - /* データの格納開始位置 */ - if(pass == FIRST) { - beginptr[i] = asprop->ptr; - } else if(pass == SECOND) { - asprop->ptr = beginptr[i]; - } - asprop->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) { - exit(-1); - } - } - if(pass == FIRST && asmode.label == true) { - fprintf(stdout, "\nLabel::::\n"); - printlabel(); - if(asmode.onlylabel == true) { - return 0; - } - } - if(pass == SECOND) { - freelabel(); /* ラベルハッシュ表を解放 */ - free_chk(asprop->prog, "asprop.prog"); /* プログラム名を解放 */ - free_chk(asprop, "asprop"); /* アセンブル時のプロパティを解放 */ - free_cmdtype_code(); /* 命令と命令タイプがキーのハッシュ表を解放 */ - } + 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); - } - if(asmode.onlyassemble == false) { - create_code_type(); /* 命令と命令タイプがキーのハッシュ表を作成 */ - res = exec(); /* プログラム実行 */ - free_code_type(); /* 命令と命令タイプがキーのハッシュ表を解放 */ - } + assemble(i, af, 0); /* アセンブル */ + 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の実行 */ } - free_chk(objfile, "objfile"); - /* エラーの解放 */ - freecerr(); - return status; +casl2fin: + shutdown(); /* 仮想マシンCOMET IIのシャットダウン */ + stat = (cerr->num == 0) ? 0 : 1; + freecerr(); /* エラーの解放 */ + return stat; }