X-Git-Url: http://j8takagi.net/cgi-bin/gitweb.cgi?p=YACASL2.git;a=blobdiff_plain;f=src%2Fcasl2.c;h=37aee61e73466254e2dc19dc66f80a8d5cce1846;hp=2d17acd23ee8e287e047f4a5d3dde2413d9ae9e2;hb=f9ad3a081e1777bb6d892af65d0e4090cb31bbfa;hpb=1e636c95bf237645b6c9117e3eb64aa2d8aa4c90 diff --git a/src/casl2.c b/src/casl2.c index 2d17acd..37aee61 100644 --- a/src/casl2.c +++ b/src/casl2.c @@ -1,122 +1,194 @@ -#include "casl2.h" -#include "assemble.h" -#include "exec.h" +#include +#include + #define _GNU_SOURCE #include -/* 指定されたファイルにCOMET II仮想メモリ(アセンブル結果)を書込 */ -void outassemble(char *file) { - FILE *fp; - if((fp = fopen(file, "w")) == NULL) { - perror(file); - return; - } - fwrite(memory, sizeof(WORD), endptr, fp); - fclose(fp); -} +#include "cmem.h" +#include "cerr.h" +#include "assemble.h" +#include "exec.h" +/** + * casl2コマンドのオプション + */ static struct option longopts[] = { - {"trace", no_argument, NULL, 't'}, - {"tracearithmetic", no_argument, NULL, 't'}, - {"tracelogical", no_argument, NULL, 'T'}, - {"dump", no_argument, NULL, 'd'}, - {"source", no_argument, NULL, 's'}, - {"label", no_argument, NULL, 'l'}, - {"assembledetail", no_argument, NULL, 'a'}, - {"onlyassemble", optional_argument, NULL, 'o'}, - {"assembledetailonly", no_argument, NULL, 'A'}, - {"help", no_argument, NULL, 'h'}, - {0, 0, 0, 0} + { "source", no_argument, NULL, 's' }, + { "label", no_argument, NULL, 'l' }, + { "labelonly", no_argument, NULL, 'L' }, + { "assembledetail", no_argument, NULL, 'a' }, + { "assembledetailonly", no_argument, NULL, 'A' }, + { "assembleout", optional_argument, NULL, 'o' }, + { "assembleoutonly", optional_argument, NULL, 'O' }, + { "trace", no_argument, NULL, 't' }, + { "tracearithmetic", no_argument, NULL, 't' }, + { "tracelogical", no_argument, NULL, 'T' }, + { "dump", no_argument, NULL, 'd' }, + { "memorysize", required_argument, NULL, 'M' }, + { "clocks", required_argument, NULL, 'C' }, + { "help", no_argument, NULL, 'h' }, + { 0, 0, 0, 0 }, }; -int main(int argc, char *argv[]) +/** + * casl2のエラー定義 + */ +CERR cerr_casl2[] = { + { 126, "no source file" }, +}; + +/** + * CASL IIのエラーをエラーリストに追加 + */ +void addcerrlist_casl2() { - int opt, i; + 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; +} + +/** + * 指定された1つまたは複数のファイルを2回アセンブル + */ +void assemble(int filec, char *filev[]) +{ + int i; PASS pass; - bool status = false; - WORD beginptr[argc]; + 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, opt, i, status; + char *af[argc]; char *objfile = NULL; - const char *usage = "Usage: %s [-tTdslaAh] [-o ] FILE ...\n"; + const char *usage = + "Usage: %s [-slLaAtTdh] [-oO[]] [-M ] [-C ] FILE1[ FILE2 ...]\n"; - while((opt = getopt_long(argc, argv, "tTdslao:Ah", longopts, NULL)) != -1) { + cerr_init(); + addcerrlist_casl2(); + addcerrlist_assemble(); + addcerrlist_exec(); + /* オプションの処理 */ + while((opt = getopt_long(argc, argv, "tTdslLao::O::AM:C:h", longopts, NULL)) != -1) { switch(opt) { - case 't': - tracemode = true; - break; - case 'T': - tracemode = true; - logicalmode = true; - break; - case 'd': - dumpmode = true; - break; case 's': - srcmode = true; + asmode.src = true; break; case 'l': - labelmode = true; + asmode.label = true; + break; + case 'L': + asmode.label = true; + asmode.onlylabel = true; break; case 'a': - asdetailmode = true; + asmode.asdetail = true; + break; + case 'A': + asmode.asdetail = true; + asmode.onlyassemble = true; break; case 'o': - onlyassemblemode = true; - if(optarg != NULL) { - objfile = strdup(optarg); - } + objfile = strdup_chk(objfile_name(optarg), "objfile"); break; - case 'A': - onlyassemblemode = true; - objfile = NULL; - asdetailmode = true; + case 'O': + asmode.onlyassemble = true; + objfile = strdup_chk(objfile_name(optarg), "objfile"); + break; + case 't': + execmode.trace = true; + break; + case 'T': + execmode.trace = true; + execmode.logical = true; + break; + case 'd': + execmode.dump = true; + break; + case 'M': + memsize = atoi(optarg); + break; + case 'C': + clocks = atoi(optarg); break; case 'h': fprintf(stdout, usage, argv[0]); - exit(-1); + return 0; case '?': fprintf(stderr, usage, argv[0]); exit(-1); } } - /* アセンブル。ラベル表作成のため、2回行う */ - for(pass = FIRST; pass <= SECOND; pass++) { - for(i = optind; i < argc; i++) { - /* データの格納開始位置 */ - if(pass == FIRST) { - beginptr[i] = ptr; - } else if(pass == SECOND) { - ptr = beginptr[i]; - } - if(tracemode == true || dumpmode == true || srcmode == true || - labelmode == true || asdetailmode == 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 && labelmode == true) { - fprintf(stdout, "\nLabel::::\n"); - printlabel(); - } + /* ソースファイルが指定されていない場合は終了 */ + if(argv[optind] == NULL) { + setcerr(126, NULL); /* no source file */ + fprintf(stderr, "CASL2 error - %d: %s\n", cerr->num, cerr->msg); + exit(-1); } - freelabel(); /* ラベル表の解放 */ - if(status == true) { - if(onlyassemblemode) { - if(objfile != NULL) { - outassemble(objfile); - } - } else { - exec(); /* プログラム実行 */ - } + reset(memsize, clocks); /* 仮想マシンCOMET IIのリセット */ + for(i = 0; i < argc - optind; i++) { /* 引数からファイル名配列を取得 */ + af[i] = argv[optind + i]; } - if(cerrno > 0) { - freecerr(); - exit(-1); + assemble(i, af); /* アセンブル */ + if(asmode.onlylabel == true || cerr->num > 0) { + goto casl2fin; + } + /* オブジェクトファイル名が指定されている場合は、アセンブル結果をオブジェクトファイルに出力 */ + if(objfile != NULL) { + outassemble(objfile); + FREE(objfile); + } + /* onlyassembleモード以外の場合、仮想マシンCOMET IIを実行 */ + if(asmode.onlyassemble == false) { + exec(); /* 仮想マシンCOMET IIの実行 */ } - return 0; +casl2fin: + shutdown(); /* 仮想マシンCOMET IIのシャットダウン */ + status = (cerr->num == 0) ? 0 : -1; + freecerr(); /* エラーの解放 */ + return status; }