X-Git-Url: http://j8takagi.net/cgi-bin/gitweb.cgi?p=YACASL2.git;a=blobdiff_plain;f=src%2Fcasl2.c;h=37aee61e73466254e2dc19dc66f80a8d5cce1846;hp=d17922ca607d618dc77109e159c669611af73319;hb=f9ad3a081e1777bb6d892af65d0e4090cb31bbfa;hpb=d1f82970bf7d41db2fea11b08cd8e308f6cb8138 diff --git a/src/casl2.c b/src/casl2.c index d17922c..37aee61 100644 --- a/src/casl2.c +++ b/src/casl2.c @@ -4,10 +4,10 @@ #define _GNU_SOURCE #include +#include "cmem.h" +#include "cerr.h" #include "assemble.h" #include "exec.h" -#include "cerr.h" -#include "cmem.h" /** * casl2コマンドのオプション @@ -33,35 +33,85 @@ static struct option longopts[] = { /** * casl2のエラー定義 */ -static CERR cerr_casl2[] = { +CERR cerr_casl2[] = { { 126, "no source file" }, }; +/** + * CASL IIのエラーをエラーリストに追加 + */ +void addcerrlist_casl2() +{ + addcerrlist(ARRAYSIZE(cerr_casl2), cerr_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; } +/** + * 指定された1つまたは複数のファイルを2回アセンブル + */ +void assemble(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); + } + if(assemblefile(filev[i], pass) == false) { + 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"; cerr_init(); - addcerrlist(sizeof(cerr_casl2), cerr_casl2); + addcerrlist_casl2(); + addcerrlist_assemble(); + addcerrlist_exec(); /* オプションの処理 */ while((opt = getopt_long(argc, argv, "tTdslLao::O::AM:C:h", longopts, NULL)) != -1) { switch(opt) { @@ -119,62 +169,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(); /* 命令の名前とタイプがキーのハッシュ表を作成 */ - 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); /* アセンブル */ + 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(); +casl2fin: + shutdown(); /* 仮想マシンCOMET IIのシャットダウン */ + status = (cerr->num == 0) ? 0 : -1; + freecerr(); /* エラーの解放 */ return status; }