From: j8takagi Date: Mon, 1 Mar 2010 18:57:36 +0000 (+0900) Subject: エラーをリストで格納し、分散して追加するように修正 X-Git-Tag: v0.1~5 X-Git-Url: https://j8takagi.net/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=406fd6490ad76b4573b6ce9f8d322d92f13d27d8;p=YACASL2.git エラーをリストで格納し、分散して追加するように修正 --- diff --git a/include/assemble.h b/include/assemble.h index 7c6b347..4e9203a 100644 --- a/include/assemble.h +++ b/include/assemble.h @@ -77,6 +77,9 @@ typedef enum { SECOND = 1, } PASS; +/* アセンブルのエラー定義 */ +bool addcerrlist_assemble(); + /* プログラム名とラベルに対応するハッシュ値を返す */ unsigned labelhash(const char *prog, const char *label); diff --git a/include/casl2.h b/include/casl2.h index 9a2fc07..3d602c4 100644 --- a/include/casl2.h +++ b/include/casl2.h @@ -78,15 +78,16 @@ typedef enum { NONE = 0, } CMDTYPE; -extern int cmdcodesize; - -/* 命令コードの配列 */ +/* 命令コード配列 */ typedef struct { char *cmd; CMDTYPE type; WORD code; } CMDCODEARRAY; +/* 命令コード配列のサイズ */ +extern int cmdcodesize; + /* 命令コードのハッシュ表 */ typedef struct _CMDCODETAB { struct _CMDCODETAB *next; diff --git a/include/cerr.h b/include/cerr.h index f651c7d..a4d1246 100644 --- a/include/cerr.h +++ b/include/cerr.h @@ -5,6 +5,7 @@ #include #include #include +#include #ifndef ARRAYSIZE #define ARRAYSIZE(array) (sizeof(array)/sizeof(array[0])) @@ -37,7 +38,7 @@ enum { }; /* エラーを追加する */ -void addcerrlist(int cerrc, CERRARRAY cerrv[]); +bool addcerrlist(int cerrc, CERRARRAY cerrv[]); /* エラー番号とエラーメッセージを設定 */ void setcerr(int num, const char *str); diff --git a/include/exec.h b/include/exec.h index 40230b4..7262af5 100644 --- a/include/exec.h +++ b/include/exec.h @@ -16,6 +16,9 @@ typedef struct { } EXECMODE; extern EXECMODE execmode; +/* 実行のエラー定義 */ +bool addcerrlist_exec(); + /* 仮想マシンのリセット */ void reset(); diff --git a/include/word.h b/include/word.h index 6d83ef0..c1efb67 100644 --- a/include/word.h +++ b/include/word.h @@ -10,6 +10,9 @@ #include "cerr.h" +/* wordのエラー定義 */ +bool addcerrlist_word(); + /* WORD - 16ビットデータ型 */ typedef unsigned short WORD; diff --git a/src/assemble.c b/src/assemble.c index 50554a7..ee8449d 100644 --- a/src/assemble.c +++ b/src/assemble.c @@ -13,6 +13,38 @@ WORD lptr; /* 他のプログラムで参照する入口名 */ char *prog; + +/* アセンブルのエラー定義 */ +CERRARRAY cerr_assemble[] = { + { 101, "label already defined" }, + { 102, "label table is full" }, + { 103, "label not found" }, + { 104, "label length is too long" }, + { 105, "no command in the line" }, + { 106, "operand mismatch in assemble command" }, + { 107, "no label in START" }, + { 108, "not command of operand \"r\"" }, + { 109, "not command of operand \"r1,r2\"" }, + { 110, "not command of operand \"r,adr[,x]\"" }, + { 111, "not command of operand \"adr[,x]\"" }, + { 112, "not command of no operand" }, + { 113, "operand too many in COMET II command" }, + { 117, "operand too many in DC" }, + { 118, "operand length too long" }, + { 119, "out of COMET II memory" }, + { 120, "GR0 in operand x" }, + { 121, "cannot get operand token" }, + { 122, "cannot create hash table" }, + { 123, "unclosed quote" }, + { 124, "more than one character in literal" }, + { 125, "not GR in operand x" }, +}; + +bool addcerrlist_assemble() +{ + return addcerrlist(ARRAYSIZE(cerr_assemble), cerr_assemble); +} + /* 汎用レジスタを表す文字列「GR[0-7]」から、レジスタ番号[0-7]をWORD値で返す */ /* 文字列が汎用レジスタを表さない場合は、0xFFFFを返す */ /* is_xがtrueの場合は指標レジスタ。GR0は、COMET IIの仕様により、エラー発生 */ @@ -59,6 +91,7 @@ WORD getadr(const char *prog, const char *str, PASS pass) bool writememory(WORD word, WORD adr, PASS pass) { bool status = false; + /* COMET IIメモリオーバーの場合 */ if(adr >= memsize) { setcerr(119, word2n(adr)); /* out of COMET II memory */ @@ -329,7 +362,7 @@ bool cometcmd(const CMDLINE *cmdl, PASS pass) status = true; } } else { - setcerr(113, cmdl->cmd); /* command not defined */ + setcerr(113, cmdl->cmd); /* operand too many in COMET II command */ return false; } } @@ -389,7 +422,7 @@ bool assembleline(const CMDLINE *cmdl, PASS pass) ; } else if(cerrno == 0) { - setcerr(113, cmdl->cmd); /* command not defined */ + setcerr(113, cmdl->cmd); /* operand too many in COMET II command */ } /* エラーが発生していないか確認 */ if(cerrno == 0) { @@ -412,6 +445,7 @@ bool assemble(const char *file, PASS pass) char *line; FILE *fp; + addcerrlist_assemble(); if((fp = fopen(file, "r")) == NULL) { perror(file); return false; diff --git a/src/casl2.c b/src/casl2.c index 9753c83..5ae4825 100644 --- a/src/casl2.c +++ b/src/casl2.c @@ -23,42 +23,14 @@ static struct option longopts[] = { {0, 0, 0, 0}, }; -/* エラー番号とエラーメッセージ */ +/* casl2のエラー定義 */ CERRARRAY cerr_casl2[] = { - { 101, "label already defined" }, - { 102, "label table is full" }, - { 103, "label not found" }, - { 104, "label length is too long" }, - { 105, "no command in the line" }, - { 106, "operand mismatch in assemble command" }, - { 107, "no label in START" }, - { 108, "not command of operand \"r\"" }, - { 109, "not command of operand \"r1,r2\"" }, - { 110, "not command of operand \"r,adr[,x]\"" }, - { 111, "not command of operand \"adr[,x]\"" }, - { 112, "not command of no operand" }, - { 113, "operand too many in machine command" }, - { 114, "not integer" }, - { 115, "not hex" }, - { 116, "out of hex range" }, - { 117, "operand too many in DC" }, - { 118, "operand length too long" }, - { 119, "out of COMET II memory" }, - { 120, "GR0 in operand x" }, - { 121, "cannot get operand token" }, - { 122, "cannot create hash table" }, - { 123, "unclosed quote" }, - { 124, "more than one character in literal" }, - { 125, "not GR in operand x" }, { 126, "source file is not specified" }, - { 201, "execute - out of COMET II memory" }, - { 202, "SVC input - out of Input memory" }, - { 203, "SVC output - out of COMET II memory" }, - { 204, "Program Register (PR) - out of COMET II memory" }, - { 205, "Stack Pointer (SP) - cannot allocate stack buffer" }, - { 206, "Address - out of COMET II memory" }, - { 207, "Stack Pointer (SP) - out of COMET II memory" }, }; +bool addcerrlist_casl2() +{ + return addcerrlist_casl2(ARRAYSIZE(cerr_casl2), cerr_casl2); +} /* 指定されたファイルにアセンブル結果を書込 */ void outassemble(const char *file) { @@ -144,8 +116,10 @@ int main(int argc, char *argv[]) exit(-1); } } - /* エラーリストにerr_casl2を追加 */ - addcerrlist(ARRAYSIZE(cerr_casl2), cerr_casl2); + + #if 0 + addcerrlist_casl2(); + #endif /* ソースファイルが指定されていない場合は終了 */ if(argv[optind] == NULL) { setcerr(126, NULL); /* source file is not specified */ @@ -182,7 +156,8 @@ int main(int argc, char *argv[]) } } } - freelabel(); /* ラベル表の解放 */ + free_cmdtype_code(); /* 命令表の解放 */ + freelabel(); /* ラベル表の解放 */ if(status == true) { if(objfile != NULL) { outassemble(objfile); diff --git a/src/cerr.c b/src/cerr.c index 662cc66..5443c3e 100644 --- a/src/cerr.c +++ b/src/cerr.c @@ -10,33 +10,37 @@ char *cerrmsg; CERRLIST *cerr; /* エラーリストを作成する */ -void addcerrlist(int newerrc, CERRARRAY newerrv[]) +bool addcerrlist(int newerrc, CERRARRAY newerrv[]) { int i; - CERRLIST *p = cerr, *q; + CERRLIST *p, *q; + assert(newerrc > 0 && newerrv != NULL); if(cerr != NULL) { - for(p = cerr; p != NULL; p = p->next) { - ; + p = cerr; + while (p != NULL) { + q = p; + p = p->next; } - if((p = malloc(sizeof(CERRLIST *))) == NULL) { - fprintf(stderr, "addcerrlist: cannot allocate memory\n"); - exit(-1); + if((p = q->next = malloc(sizeof(CERRLIST *))) == NULL) { + goto addcerrlisterr; } } else if((p = cerr = malloc(sizeof(CERRLIST *))) == NULL) { - fprintf(stderr, "addcerrlist: cannot allocate memory\n"); - exit(-1); + goto addcerrlisterr; } for(i = 0; i < newerrc; i++) { p->err = &(newerrv[i]); if((p->next = malloc(sizeof(CERRLIST *))) == NULL) { - fprintf(stderr, "addcerrlist: cannot allocate memory\n"); - exit(-1); + goto addcerrlisterr; } q = p; p = p->next; } q->next = NULL; + return true; +addcerrlisterr: + fprintf(stderr, "addcerrlist: cannot allocate memory\n"); + exit(-1); } /* エラー番号とエラーメッセージを設定する */ diff --git a/src/comet2.c b/src/comet2.c index e51b0e0..be09f10 100644 --- a/src/comet2.c +++ b/src/comet2.c @@ -15,20 +15,19 @@ static struct option longopts[] = { {0, 0, 0, 0} }; -/* エラー番号とエラーメッセージ */ +/* comet2のエラー定義 */ CERRARRAY cerr_comet2[] = { { 201, "Load object file - full of COMET II memory" }, - { 202, "SVC input - out of Input memory" }, - { 203, "SVC output - out of COMET II memory" }, - { 204, "Program Register (PR) - out of COMET II memory" }, - { 205, "Stack Pointer (SP) - cannot allocate stack buffer" }, - { 206, "Address - out of COMET II memory" }, - { 207, "Stack Pointer (SP) - out of COMET II memory" }, }; +bool addcerrlist_comet2() +{ + return addcerrlist(ARRAYSIZE(cerr_comet2), cerr_comet2); +} /* 指定されたファイルからアセンブル結果を読込 */ bool loadassemble(char *file) { FILE *fp; + if((fp = fopen(file, "r")) == NULL) { perror(file); return false; @@ -48,6 +47,7 @@ int main(int argc, char *argv[]) int opt; const char *usage = "Usage: %s [-tTdh] [-M ] [-C ] FILE\n"; + addcerrlist_comet2(); while((opt = getopt_long(argc, argv, "tTdM:C:h", longopts, NULL)) != -1) { switch(opt) { case 't': @@ -74,8 +74,6 @@ int main(int argc, char *argv[]) exit(-1); } } - /* エラーリストにerr_comet2を追加 */ - addcerrlist(ARRAYSIZE(cerr_comet2), cerr_comet2); reset(); startptr = 0; if(loadassemble(argv[optind]) == true) { diff --git a/src/dumpword.c b/src/dumpword.c index 622a4de..1a8a73e 100644 --- a/src/dumpword.c +++ b/src/dumpword.c @@ -9,12 +9,6 @@ static struct option longopts[] = { {0, 0, 0, 0}, }; -CERRARRAY cerr_dumpword[] = { - { 114, "not integer" }, - { 115, "not hex" }, - { 116, "out of hex range" }, -}; - int main(int argc, char *argv[]) { bool logicalmode = false; /* レジストリの内容を論理値(0〜65535)で表示する場合はtrue */ @@ -40,8 +34,6 @@ int main(int argc, char *argv[]) fprintf(stderr, usage, argv[0]); exit(-1); } - /* エラーリストにerr_casl2を追加 */ - addcerrlist(ARRAYSIZE(cerr_dumpword), cerr_dumpword); /* WORD値に変換 */ word = nh2word(argv[optind]); if(cerrno > 0) { diff --git a/src/exec.c b/src/exec.c index 8f91d48..5e1f56d 100644 --- a/src/exec.c +++ b/src/exec.c @@ -1,6 +1,21 @@ #include "casl2.h" #include "exec.h" +/* 実行のエラー定義 */ +CERRARRAY cerr_exec[] = { + { 202, "SVC input - out of Input memory" }, + { 203, "SVC output - out of COMET II memory" }, + { 204, "Program Register (PR) - out of COMET II memory" }, + { 205, "Stack Pointer (SP) - cannot allocate stack buffer" }, + { 206, "Address - out of COMET II memory" }, + { 207, "Stack Pointer (SP) - out of COMET II memory" }, +}; + +bool addcerrlist_exec() +{ + return addcerrlist(ARRAYSIZE(cerr_exec), cerr_exec); +} + /* 実行モード: trace, logical, dump */ EXECMODE execmode = {false, false, false}; @@ -272,6 +287,7 @@ void exec() char *errpr = malloc(CERRSTRSIZE + 1); clock_t clock_begin, clock_end; + addcerrlist_exec(); if(execmode.trace) { fprintf(stdout, "\nExecuting machine codes\n"); } diff --git a/src/word.c b/src/word.c index 294c4d4..3b30967 100644 --- a/src/word.c +++ b/src/word.c @@ -1,9 +1,21 @@ #include "word.h" +/* wordのエラー定義 */ +CERRARRAY cerr_word[] = { + { 114, "not integer" }, + { 115, "not hex" }, + { 116, "out of hex range" }, +}; +bool addcerrlist_word() +{ + return addcerrlist(ARRAYSIZE(cerr_word), cerr_word); +} + /* 10進数の文字列をWORD値に変換 */ WORD n2word(const char *str) { assert(isdigit(*str) || *str == '-'); + char *check; int n; /* WORD値に変換 */ @@ -23,6 +35,7 @@ WORD n2word(const char *str) WORD h2word(const char *str) { assert(*str == '#'); + WORD word = 0x0; char *check; str++; @@ -42,6 +55,7 @@ WORD h2word(const char *str) /* 10進数または16進数の文字列をWORD値に変換 */ WORD nh2word(const char *str) { + addcerrlist_word(); WORD word; if(!isdigit(*str) && *str != '-' && *str != '#') { setcerr(114, str); /* not integer */ diff --git a/test/integration/casl2/err_113/0.txt b/test/integration/casl2/err_113/0.txt index daf17ec..805646d 100644 --- a/test/integration/casl2/err_113/0.txt +++ b/test/integration/casl2/err_113/0.txt @@ -1,3 +1,3 @@ ../../../../casl2 ../../../../as/err/err_113.casl -Assemble error - 113: LD: operand too many in machine command +Assemble error - 113: LD: operand too many in COMET II command ../../../../as/err/err_113.casl: 2: LD GR1,A,1,2 diff --git a/test/unit/addcerrlist/Makefile b/test/unit/addcerrlist/Makefile new file mode 100644 index 0000000..e742823 --- /dev/null +++ b/test/unit/addcerrlist/Makefile @@ -0,0 +1,3 @@ +UCLASS = COMMON +TESTSRCFILE = addcerrlist.c +include ../TEST.mk diff --git a/test/unit/addcerrlist/addcerrlist.c b/test/unit/addcerrlist/addcerrlist.c new file mode 100644 index 0000000..6228a82 --- /dev/null +++ b/test/unit/addcerrlist/addcerrlist.c @@ -0,0 +1,70 @@ +#include +#include "casl2.h" + +CERRARRAY cerr_0[] = { + { 126, "source file is not specified" }, +}; + +CERRARRAY cerr_1[] = { + { 101, "label already defined" }, + { 102, "label table is full" }, + { 103, "label not found" }, + { 104, "label length is too long" }, + { 105, "no command in the line" }, + { 106, "operand mismatch in assemble command" }, + { 107, "no label in START" }, + { 108, "not command of operand \"r\"" }, + { 109, "not command of operand \"r1,r2\"" }, + { 110, "not command of operand \"r,adr[,x]\"" }, + { 111, "not command of operand \"adr[,x]\"" }, + { 112, "not command of no operand" }, + { 113, "operand too many in COMET II command" }, + { 117, "operand too many in DC" }, + { 118, "operand length too long" }, + { 119, "out of COMET II memory" }, + { 120, "GR0 in operand x" }, + { 121, "cannot get operand token" }, + { 122, "cannot create hash table" }, + { 123, "unclosed quote" }, + { 124, "more than one character in literal" }, + { 125, "not GR in operand x" }, +}; + +CERRARRAY cerr_2[] = { + { 114, "not integer" }, + { 115, "not hex" }, + { 116, "out of hex range" }, +}; + +CERRARRAY cerr_3[] = { + { 114, "not integer" }, + { 115, "not hex" }, + { 116, "out of hex range" }, +}; + +CERRARRAY cerr_4[] = { + { 201, "Load object file - full of COMET II memory" }, +}; + +CERRARRAY cerr_5[] = { + { 202, "SVC input - out of Input memory" }, + { 203, "SVC output - out of COMET II memory" }, + { 204, "Program Register (PR) - out of COMET II memory" }, + { 205, "Stack Pointer (SP) - cannot allocate stack buffer" }, + { 206, "Address - out of COMET II memory" }, + { 207, "Stack Pointer (SP) - out of COMET II memory" }, +}; + +int main(){ + CERRLIST *p; + addcerrlist(ARRAYSIZE(cerr_0), cerr_0); + addcerrlist(ARRAYSIZE(cerr_1), cerr_1); + addcerrlist(ARRAYSIZE(cerr_2), cerr_2); + addcerrlist(ARRAYSIZE(cerr_3), cerr_3); + addcerrlist(ARRAYSIZE(cerr_4), cerr_4); + addcerrlist(ARRAYSIZE(cerr_5), cerr_5); + for(p = cerr; p != NULL; p = p->next) { + printf("%d: %s\n", p->err->num, p->err->msg); + } + return 0; +} diff --git a/test/unit/cerrtest/cerrtest.c b/test/unit/cerrtest/cerrtest.c index 39f2a09..0c8e922 100644 --- a/test/unit/cerrtest/cerrtest.c +++ b/test/unit/cerrtest/cerrtest.c @@ -1,7 +1,6 @@ #include #include "casl2.h" -/* エラー番号とエラーメッセージ */ static CERRARRAY cerr_utest[] = { { 101, "label already defined" }, { 102, "label table is full" }, diff --git a/test/unit/getcmdcode/Makefile b/test/unit/getcmdcode/Makefile index 3c9287a..ab85bfa 100644 --- a/test/unit/getcmdcode/Makefile +++ b/test/unit/getcmdcode/Makefile @@ -1,3 +1,3 @@ UCLASS = COMMON -TESTSRCFILE = ../CERRARRAY.c getcmdcode.c +TESTSRCFILE = getcmdcode.c include ../TEST.mk diff --git a/test/unit/getcmdtype/Makefile b/test/unit/getcmdtype/Makefile index 9bbc2ae..ce4bf22 100644 --- a/test/unit/getcmdtype/Makefile +++ b/test/unit/getcmdtype/Makefile @@ -1,3 +1,3 @@ UCLASS = COMMON -TESTSRCFILE = ../CERRARRAY.c getcmdtype.c +TESTSRCFILE = getcmdtype.c include ../TEST.mk diff --git a/test/unit/getgr/Makefile b/test/unit/getgr/Makefile index 9ae5e12..32db8d8 100644 --- a/test/unit/getgr/Makefile +++ b/test/unit/getgr/Makefile @@ -1,3 +1,3 @@ UCLASS = AS -TESTSRCFILE = ../CERRARRAY.c getgr.c +TESTSRCFILE = getgr.c include ../TEST.mk diff --git a/test/unit/h2word/Makefile b/test/unit/h2word/Makefile index a04ab54..c48a61e 100644 --- a/test/unit/h2word/Makefile +++ b/test/unit/h2word/Makefile @@ -1,3 +1,3 @@ UCLASS = AS -TESTSRCFILE = ../CERRARRAY.c h2word.c +TESTSRCFILE = h2word.c include ../TEST.mk