From: j8takagi Date: Sun, 21 Mar 2010 05:37:47 +0000 (+0900) Subject: 変数名の整理 X-Git-Tag: v0.1p15~39 X-Git-Url: http://j8takagi.net/cgi-bin/gitweb.cgi?p=YACASL2.git;a=commitdiff_plain;h=4ee27a568fb9222907a566e59aaefe248f08a8e4 変数名の整理 --- diff --git a/include/assemble.h b/include/assemble.h index 4e9203a..deb83eb 100644 --- a/include/assemble.h +++ b/include/assemble.h @@ -21,16 +21,17 @@ typedef struct { bool asdetail; /* アセンブラ詳細結果を表示する場合はtrue */ bool onlyassemble; /* アセンブルだけを行う場合はtrue */ } ASMODE; -extern ASMODE asmode; -/* 値を格納するポインタ */ -extern WORD ptr; +extern ASMODE asmode; -/* リテラル(=付きの値)を格納するポインタ */ -extern WORD lptr; +/* アセンブル時のプロパティ */ +typedef struct { + WORD ptr; /* 現在のポインタ */ + WORD lptr; /* リテラル(=付きの値)を格納するポインタ */ + char *prog; /* 他のプログラムで参照する入口名 */ +} ASPROP; -/* 他のプログラムで参照する入口名 */ -extern char *prog; +extern ASPROP *asprop; /* アセンブラ命令とマクロ命令を表す番号 */ typedef enum { diff --git a/include/casl2.h b/include/casl2.h index 038f3dd..93d3d1f 100644 --- a/include/casl2.h +++ b/include/casl2.h @@ -19,8 +19,8 @@ /* COMET IIの規格 */ enum { - CMDSIZE = 4, /* 命令の最大文字数 */ - REGSIZE = 8, /* 汎用レジスタの数 */ + CMDSIZE = 4, /* 命令の最大文字数 */ + GRSIZE = 8, /* 汎用レジスタの数。COMET II規格で、7 */ DEFAULT_MEMSIZE = 512, /* デフォルトのメモリ容量。COMET II規格では、65536語 */ DEFAULT_CLOCKS = 5000000, /* デフォルトのクロック周波数。COMET II規格では、未定義 */ }; @@ -28,8 +28,8 @@ enum { /* COMET IIのメモリ */ extern WORD *memory; -/* COMET IIのCPUレジスタ */ -extern WORD GR[REGSIZE], SP, PR, FR; +/* メモリサイズ */ +extern int memsize; /* COMET II フラグのマスク値 */ enum { @@ -38,18 +38,20 @@ enum { ZF = 0x1, /* Zero Flag */ }; -/* メモリーサイズ */ -extern int memsize; +/* COMET IIのCPU */ +typedef struct { + WORD gr[GRSIZE]; /* 汎用レジスタ */ + WORD sp; /* スタックポインタ */ + WORD pr; /* プログラムレジスタ */ + WORD fr; /* フラグレジスタ */ +} CPU; + +/* COMET IIのCPU */ +extern CPU *cpu; /* クロック周波数 */ extern int clocks; -/* 実行開始番地 */ -extern WORD startptr; - -/* 実行終了番地 */ -extern WORD endptr; - /* COMET II 命令 */ /* 命令タイプは、オペランドにより6種類に分類 */ typedef enum { @@ -80,24 +82,32 @@ typedef enum { /* 命令コード配列 */ typedef struct { - char *cmd; + char *name; CMDTYPE type; WORD code; -} CMDCODEARRAY; +} CMD; /* 命令コード配列のサイズ */ -extern int cmdcodesize; +extern int comet2cmdsize; /* 命令コードのハッシュ表 */ -typedef struct _CMDCODETAB { - struct _CMDCODETAB *next; - CMDCODEARRAY *cca; -} CMDCODETAB; +typedef struct _CMDTAB { + struct _CMDTAB *next; + CMD *cmd; +} CMDTAB; -extern CMDCODETAB **cmdtype_code; -extern CMDCODETAB **code_type; +extern CMDTAB **cmdtype_code; +extern CMDTAB **code_type; extern int cmdtabsize; +/* CASL2プログラムのプロパティ */ +typedef struct { + WORD start; /* プログラムの開始番地 */ + WORD end; /* プログラムの終了番地 */ +} PROGPROP; + +extern PROGPROP *progprop; + /* COMET II仮想マシンのリセット */ void reset(); diff --git a/include/cerr.h b/include/cerr.h index a4d1246..7265b0e 100644 --- a/include/cerr.h +++ b/include/cerr.h @@ -11,34 +11,34 @@ #define ARRAYSIZE(array) (sizeof(array)/sizeof(array[0])) #endif -/* エラー番号 */ -extern int cerrno; +/* mallocを実行し、メモリを確保できない場合は */ +/* エラーを出力して終了 */ +void *malloc_chk(size_t size, char *tag); -/* エラーメッセージ */ -extern char *cerrmsg; - -/* エラーコード配列 */ +/* エラーの構造体 */ typedef struct { - int num; - char *msg; -} CERRARRAY; + int num; /* エラー番号 */ + char *msg; /* エラーメッセージ */ +} CERR; + +/* 現在のエラー */ +extern CERR *cerr; -/* エラーコードリスト */ +/* エラーリスト */ typedef struct _CERRLIST { struct _CERRLIST *next; - CERRARRAY *err; + CERR *cerr; } CERRLIST; -/* エラーメッセージ */ -extern CERRLIST *cerr; +extern CERRLIST *cerrlist; enum { CERRSTRSIZE = 10, /* エラーメッセージ中に挿入できる文字列のサイズ */ CERRMSGSIZE = 70, /* エラーメッセージのサイズ */ }; -/* エラーを追加する */ -bool addcerrlist(int cerrc, CERRARRAY cerrv[]); +/* エラーリストを作成・追加する */ +bool addcerrlist(int cerrc, CERR cerrv[]); /* エラー番号とエラーメッセージを設定 */ void setcerr(int num, const char *str); diff --git a/include/exec.h b/include/exec.h index bd06f39..9afde5b 100644 --- a/include/exec.h +++ b/include/exec.h @@ -1,9 +1,6 @@ #ifndef YACASL2_EXEC_INCLUDED #define YACASL2_EXEC_INCLUDED -/* コードから命令のパターンを取得 */ -CMDTYPE getcmdtype(WORD code); - enum { INSIZE = 256 /* CASL IIの、IN命令入力領域 */ }; @@ -14,8 +11,12 @@ typedef struct { bool logical; /* レジストリの内容を論理値(0〜65535)で表示する場合はtrue */ bool dump; /* メモリの内容を表示する場合はtrue */ } EXECMODE; + extern EXECMODE execmode; +/* コードから命令のパターンを取得 */ +CMDTYPE getcmdtype(WORD code); + /* 実行のエラー定義 */ bool addcerrlist_exec(); @@ -23,7 +24,7 @@ bool addcerrlist_exec(); void reset(); /* コードの実行 */ -void exec(); +bool exec(); /* COMET IIのメモリを表示 */ void dumpmemory(); diff --git a/src/assemble.c b/src/assemble.c index 02e655a..c9f1345 100644 --- a/src/assemble.c +++ b/src/assemble.c @@ -4,17 +4,11 @@ /* アセンブルモード: src, label, onlylabel, asdetail, onlyassemble */ ASMODE asmode = {false, false, false, false, false}; -/* 値を格納するポインタ */ -WORD ptr; - -/* リテラル(=付きの値)を格納するポインタ */ -WORD lptr; - -/* 他のプログラムで参照する入口名 */ -char *prog; +/* アセンブル時のプロパティ */ +ASPROP *asprop; /* アセンブルのエラー定義 */ -CERRARRAY cerr_assemble[] = { +CERR cerr_assemble[] = { { 101, "label already defined" }, { 102, "label table is full" }, { 103, "label not found" }, @@ -53,7 +47,7 @@ WORD getgr(const char *str, bool is_x) WORD r; /* 「GR[0-7]」以外の文字列では、0xFFFFを返して終了 */ if(!(strlen(str) == 3 && strncmp(str, "GR", 2) == 0 && - (*(str+2) >= '0' && *(str+2) <= '7'))) + (*(str+2) >= '0' && *(str+2) <= '0' + GRSIZE))) { return 0xFFFF; } @@ -95,7 +89,7 @@ bool writememory(WORD word, WORD adr, PASS pass) if(adr >= memsize) { setcerr(119, word2n(adr)); /* out of COMET II memory */ } - if(cerrno == 0) { + if(cerr->num == 0) { memory[adr] = word; if(pass == SECOND && asmode.asdetail == true) { fprintf(stdout, "\t#%04X\t#%04X\n", adr, word); @@ -109,12 +103,12 @@ bool writememory(WORD word, WORD adr, PASS pass) /* リテラルには、10進定数/16進定数/文字定数が含まれる */ WORD getliteral(const char *str, PASS pass) { - WORD adr = lptr; + WORD adr = asprop->lptr; assert(*str == '='); if(*(++str) == '\'') { /* 文字定数 */ writestr(str, true, pass); } else { - writememory(nh2word(str), lptr++, pass); + writememory(nh2word(str), (asprop->lptr)++, pass); } return adr; } @@ -123,7 +117,7 @@ WORD getliteral(const char *str, PASS pass) /* DC命令とリテラルで使い、リテラルの場合はリテラル領域に書込 */ void writestr(const char *str, bool literal, PASS pass) { - assert(cerrno == 0 && *str == '\''); + assert(cerr->num == 0 && *str == '\''); const char *p = str + 1; bool lw = false; @@ -142,10 +136,10 @@ void writestr(const char *str, bool literal, PASS pass) } /*リテラルの場合はリテラル領域に書込 */ if(literal == true) { - writememory(*(p++), lptr++, pass); + writememory(*(p++), (asprop->lptr)++, pass); lw = true; } else { - writememory(*(p++), ptr++, pass); + writememory(*(p++), (asprop->ptr)++, pass); } } } @@ -160,11 +154,11 @@ void writeDC(const char *str, PASS pass) if(*str == '#' || isdigit(*str) || *str == '-') { adr = nh2word(str); } else { - if(pass == SECOND && (adr = getlabel(prog, str)) == 0xFFFF) { + if(pass == SECOND && (adr = getlabel(asprop->prog, str)) == 0xFFFF) { setcerr(103, str); /* label not found */ } } - writememory(adr, ptr++, pass); + writememory(adr, (asprop->ptr)++, pass); } } @@ -202,10 +196,10 @@ bool assemblecmd(const CMDLINE *cmdl, PASS pass) return false; } /* プログラム名の設定 */ - prog = strdup(cmdl->label); + asprop->prog = strdup(cmdl->label); /* オペランドがある場合、実行開始番地を設定 */ if(pass == SECOND && cmdl->opd->opdc == 1) { - if((startptr = getlabel(prog, cmdl->opd->opdv[0])) == 0xFFFF) { + if((progprop->start = getlabel(asprop->prog, cmdl->opd->opdv[0])) == 0xFFFF) { setcerr(103, cmdl->opd->opdv[0]); /* label not found */ } } @@ -214,19 +208,19 @@ bool assemblecmd(const CMDLINE *cmdl, PASS pass) case END: /* リテラル領域の設定 */ if(pass == FIRST) { - lptr = ptr; + asprop->lptr = asprop->ptr; } /* 実行終了番地と次のプログラムの実行開始番地を設定 */ else if(pass == SECOND) { - endptr = lptr; + progprop->end = asprop->lptr; } - prog = NULL; + asprop->prog = NULL; status = true; break; case DS: for(i = 0; i < atoi(cmdl->opd->opdv[0]); i++) { - writememory(0x0, ptr++, pass); - if(cerrno > 0) { + writememory(0x0, (asprop->ptr)++, pass); + if(cerr->num > 0) { return false; } } @@ -235,7 +229,7 @@ bool assemblecmd(const CMDLINE *cmdl, PASS pass) case DC: for(i = 0; i < cmdl->opd->opdc; i++) { writeDC(cmdl->opd->opdv[i], pass); - if(cerrno > 0) { + if(cerr->num > 0) { return false; } } @@ -244,7 +238,7 @@ bool assemblecmd(const CMDLINE *cmdl, PASS pass) default: return false; } - if(cerrno > 0) { + if(cerr->num > 0) { status = false; } return status; @@ -308,7 +302,7 @@ bool cometcmd(const CMDLINE *cmdl, PASS pass) setcerr(112, cmdl->cmd); /* not command of no operand */ return false; } - if(writememory(cmd, ptr++, pass) == true) { + if(writememory(cmd, (asprop->ptr)++, pass) == true) { status = true; } } @@ -321,7 +315,7 @@ bool cometcmd(const CMDLINE *cmdl, PASS pass) return false; } cmd |= (r1 << 4); - if(writememory(cmd, ptr++, pass) == true) { + if(writememory(cmd, (asprop->ptr)++, pass) == true) { status = true; } } @@ -332,7 +326,7 @@ bool cometcmd(const CMDLINE *cmdl, PASS pass) return false; } cmd |= ((r1 << 4) | r2); - if(cerrno == 0 && writememory(cmd, ptr++, pass) == true) { + if(cerr->num == 0 && writememory(cmd, (asprop->ptr)++, pass) == true) { status = true; } } @@ -354,10 +348,10 @@ bool cometcmd(const CMDLINE *cmdl, PASS pass) } cmd |= x; } - adr = getadr(prog, cmdl->opd->opdv[1], pass); - writememory(cmd, ptr++, pass); - writememory(adr, ptr++, pass); - if(cerrno == 0) { + adr = getadr(asprop->prog, cmdl->opd->opdv[1], pass); + writememory(cmd, (asprop->ptr)++, pass); + writememory(adr, (asprop->ptr)++, pass); + if(cerr->num == 0) { status = true; } } else { @@ -374,7 +368,7 @@ bool cometcmd(const CMDLINE *cmdl, PASS pass) /* オペランド数2の場合、第2オペランドは指標レジスタとして用いる汎用レジスタ */ if(cmdl->opd->opdc == 2) { x = getgr(cmdl->opd->opdv[1], true); - if(cerrno > 0) { + if(cerr->num > 0) { return false; } cmd |= x; @@ -386,11 +380,11 @@ bool cometcmd(const CMDLINE *cmdl, PASS pass) adr = getlabel(NULL, cmdl->opd->opdv[0]); } if(cmd != 0x8000 || (pass == SECOND && adr == 0xFFFF)) { - adr = getadr(prog, cmdl->opd->opdv[0], pass); + adr = getadr(asprop->prog, cmdl->opd->opdv[0], pass); } - writememory(cmd, ptr++, pass); - writememory(adr, ptr++, pass); - if(cerrno == 0) { + writememory(cmd, (asprop->ptr)++, pass); + writememory(adr, (asprop->ptr)++, pass); + if(cerr->num == 0) { status = true; } } @@ -409,22 +403,22 @@ bool assembleline(const CMDLINE *cmdl, PASS pass) } } /* アセンブラ命令の処理 */ - else if(cerrno == 0 && assemblecmd(cmdl, pass) == true) { + else if(cerr->num == 0 && assemblecmd(cmdl, pass) == true) { ; } /* マクロ命令の書込 */ - else if(cerrno == 0 && macrocmd(cmdl, pass) == true) { + else if(cerr->num == 0 && macrocmd(cmdl, pass) == true) { ; } /* 機械語命令の書込 */ - else if(cerrno == 0 && cometcmd(cmdl, pass) == true) { + else if(cerr->num == 0 && cometcmd(cmdl, pass) == true) { ; } - else if(cerrno == 0) { + else if(cerr->num == 0) { setcerr(113, cmdl->cmd); /* operand too many in COMET II command */ } /* エラーが発生していないか確認 */ - if(cerrno == 0) { + if(cerr->num == 0) { status = true; } return status; @@ -450,8 +444,8 @@ bool assemble(const char *file, PASS pass) return false; } for(; ;) { - cmdl = malloc(sizeof(CMDLINE)); - line = malloc(LINESIZE + 1); + cmdl = malloc_chk(sizeof(CMDLINE), "cmdl"); + line = malloc_chk(LINESIZE + 1, "line"); if((line = fgets(line, LINESIZE, fp)) == NULL) { break; } @@ -463,7 +457,7 @@ bool assemble(const char *file, PASS pass) } if((cmdl = linetok(line)) != NULL) { if(pass == FIRST && cmdl->label != NULL) { - if(addlabel(prog, cmdl->label, ptr) == false) { + if(addlabel(asprop->prog, cmdl->label, asprop->ptr) == false) { break; } } @@ -471,12 +465,12 @@ bool assemble(const char *file, PASS pass) break; } } - if(cerrno > 0) { + if(cerr->num > 0) { break; } } - if(cerrno > 0) { - fprintf(stderr, "Assemble error - %d: %s\n", cerrno, cerrmsg); + if(cerr->num > 0) { + fprintf(stderr, "Assemble error - %d: %s\n", cerr->num, cerr->msg); printline(stderr, file, lineno, line); status = false; } diff --git a/src/casl2.c b/src/casl2.c index 493238e..d75650d 100644 --- a/src/casl2.c +++ b/src/casl2.c @@ -24,7 +24,7 @@ static struct option longopts[] = { }; /* casl2のエラー定義 */ -CERRARRAY cerr_casl2[] = { +CERR cerr_casl2[] = { { 126, "source file is not specified" }, }; bool addcerrlist_casl2() @@ -39,7 +39,7 @@ void outassemble(const char *file) { perror(file); exit(-1); } - fwrite(memory, sizeof(WORD), endptr, fp); + fwrite(memory, sizeof(WORD), progprop->end, fp); fclose(fp); } @@ -58,7 +58,7 @@ const char *objfile_name(const char *str) /* casl2コマンドのメイン */ int main(int argc, char *argv[]) { - int opt, i; + int opt, i, retval = 0; PASS pass; bool status = false; WORD beginptr[argc]; @@ -66,6 +66,10 @@ int main(int argc, char *argv[]) const char *usage = "Usage: %s [-slLaAtTdh] [-oO] [-M ] [-C ] FILE ...\n"; + /* エラーの初期化 */ + cerr = malloc_chk(sizeof(CERR), "cerr"); + addcerrlist_casl2(); + /* オプションの処理 */ while((opt = getopt_long(argc, argv, "tTdslLao::O::AM:C:h", longopts, NULL)) != -1) { switch(opt) { case 's': @@ -116,27 +120,26 @@ int main(int argc, char *argv[]) exit(-1); } } - - addcerrlist_casl2(); /* ソースファイルが指定されていない場合は終了 */ if(argv[optind] == NULL) { setcerr(126, NULL); /* source file is not specified */ - fprintf(stderr, "CASL2 error - %d: %s\n", cerrno, cerrmsg); - goto casl2err; + fprintf(stderr, "CASL2 error - %d: %s\n", cerr->num, cerr->msg); + exit(-1); } /* COMET II仮想マシンのリセット */ reset(); /* アセンブル。ラベル表作成のため、2回行う */ for(pass = FIRST; pass <= SECOND; pass++) { - if(pass == FIRST && create_cmdtype_code() == false) { - goto casl2err; + if(pass == FIRST) { + create_cmdtype_code(); /* 命令と命令タイプがキーのハッシュ表を作成 */ + asprop = malloc_chk(sizeof(ASPROP), "asprop"); } for(i = optind; i < argc; i++) { /* データの格納開始位置 */ if(pass == FIRST) { - beginptr[i] = ptr; + beginptr[i] = asprop->ptr; } else if(pass == SECOND) { - ptr = beginptr[i]; + asprop->ptr = beginptr[i]; } if(execmode.trace == true || execmode.dump == true || asmode.src == true || asmode.label == true || asmode.asdetail == true) @@ -144,7 +147,7 @@ int main(int argc, char *argv[]) fprintf(stdout, "\nAssemble %s (%d)\n", argv[i], pass); } if((status = assemble(argv[i], pass)) == false) { - goto casl2err; + exit(-1); } } if(pass == FIRST && asmode.label == true) { @@ -154,24 +157,27 @@ int main(int argc, char *argv[]) return 0; } } + if(pass == SECOND) { + free_cmdtype_code(); /* 命令と命令タイプがキーのハッシュ表を解放 */ + freelabel(); /* ラベルハッシュ表を解放 */ + } } - free_cmdtype_code(); /* 命令表の解放 */ - freelabel(); /* ラベル表の解放 */ if(status == true) { if(objfile != NULL) { outassemble(objfile); } if(asmode.onlyassemble == false) { - exec(); /* プログラム実行 */ + create_code_type(); /* 命令と命令タイプがキーのハッシュ表を作成 */ + status = exec(); /* プログラム実行 */ + free_code_type(); /* 命令と命令タイプがキーのハッシュ表を解放 */ } } /* COMET II仮想マシンのシャットダウン */ shutdown(); - if(cerrno > 0) { - goto casl2err; + if(cerr->num > 0) { + retval = -1; } - return 0; -casl2err: + /* エラーの解放 */ freecerr(); - exit(-1); + return retval; } diff --git a/src/cerr.c b/src/cerr.c index c0146a2..3f7887b 100644 --- a/src/cerr.c +++ b/src/cerr.c @@ -1,77 +1,85 @@ #include "cerr.h" -/* エラー番号 */ -int cerrno = 0; +/* mallocを実行し、メモリを確保できない場合は */ +/* エラーを出力して終了 */ +void *malloc_chk(size_t size, char *tag) +{ + void *p; + + if((p = malloc(size)) == NULL) { + fprintf(stderr, "%s: cannot allocate memory\n", tag); + exit(-1); + } + return p; +} -/* エラーメッセージ */ -char *cerrmsg; +/* 現在のエラー */ +CERR *cerr; /* エラーリスト */ -CERRLIST *cerr; +CERRLIST *cerrlist; -/* エラーリストを作成する */ -bool addcerrlist(int newerrc, CERRARRAY newerrv[]) +/* エラーリストを作成・追加する */ +bool addcerrlist(int newerrc, CERR newerrv[]) { int i; CERRLIST *p, *q; assert(newerrc > 0 && newerrv != NULL); - if(cerr != NULL) { - for(p = cerr; p != NULL; p = p->next) { + if(cerrlist == NULL) { + p = cerrlist = malloc_chk(sizeof(CERRLIST), "cerrlist"); + } else { + for(p = cerrlist; p != NULL; p = p->next) { q = p; } - if((p = q->next = malloc(sizeof(CERRLIST))) == NULL) { - goto addcerrlisterr; - } - } else if((p = cerr = malloc(sizeof(CERRLIST))) == NULL) { - goto addcerrlisterr; + p = q->next = malloc_chk(sizeof(CERRLIST), "cerrlist.next"); } for(i = 0; i < newerrc; i++) { - p->err = &(newerrv[i]); - if((p->next = malloc(sizeof(CERRLIST))) == NULL) { - goto addcerrlisterr; - } + p->cerr = &(newerrv[i]); + p->next = malloc_chk(sizeof(CERRLIST), "cerrlist.next"); q = p; p = p->next; } q->next = NULL; return true; -addcerrlisterr: - fprintf(stderr, "addcerrlist: cannot allocate memory\n"); - exit(-1); } -/* エラー番号とエラーメッセージを設定する */ +/* 現在のエラーを設定する */ void setcerr(int num, const char *str) { - cerrno = num; - cerrmsg = malloc(CERRMSGSIZE + 1); + cerr->num = num; + cerr->msg = malloc_chk(CERRMSGSIZE + 1, "cerr.msg"); if(str != NULL && strlen(str) <= CERRSTRSIZE) { - sprintf(cerrmsg, "%s: %s", str, getcerrmsg(cerrno)); + sprintf(cerr->msg, "%s: %s", str, getcerrmsg(cerr->num)); } else { - strcpy(cerrmsg, getcerrmsg(cerrno)); + strcpy(cerr->msg, getcerrmsg(cerr->num)); } } -/* リストから、エラー番号に対応するメッセージを返す */ +/* エラーリストから、エラー番号に対応するメッセージを返す */ char *getcerrmsg(int num) { CERRLIST *p; - for(p = cerr; p != NULL; p = p->next) { - if(num == p->err->num) { - return p->err->msg; + for(p = cerrlist; p != NULL; p = p->next) { + if(num == p->cerr->num) { + return p->cerr->msg; } } return "unkown error"; } -/* エラーを解放する */ +/* エラーリストと現在のエラーを解放する */ void freecerr() { - assert(cerrno > 0); - cerrno = 0; - if(strlen(cerrmsg) > 0) { - free(cerrmsg); + CERRLIST *p = cerrlist, *q; + + /* エラーリストを解放 */ + while(p != NULL) { + q = p->next; + free(p); + p = q; } + /* 現在のエラーを解放 */ + free(cerr); } diff --git a/src/cmd.c b/src/cmd.c index c108b46..67d7c2a 100644 --- a/src/cmd.c +++ b/src/cmd.c @@ -1,6 +1,6 @@ #include "casl2.h" -CMDCODEARRAY cmdcodearray[] = { +CMD comet2cmd[] = { { "NOP", NONE, 0x0 }, { "LD", R_ADR_X_, 0x1000 }, { "ST", R_ADR_X, 0x1100 }, @@ -41,20 +41,20 @@ CMDCODEARRAY cmdcodearray[] = { { "RET", NONE, 0x8100 }, }; -int cmdcodesize = ARRAYSIZE(cmdcodearray); +int comet2cmdsize = ARRAYSIZE(comet2cmd); int cmdtabsize; -CMDCODETAB **cmdtype_code, **code_type; +CMDTAB **cmdtype_code, **code_type; /* 命令と命令タイプからハッシュ値を生成する */ unsigned hash_cmdtype(const char *cmd, CMDTYPE type) { HKEY *keys[2]; /* 命令をセット */ - keys[0] = malloc(sizeof(HKEY)); + keys[0] = malloc_chk(sizeof(HKEY), "hash_cmdtype.key"); keys[0]->type = CHARS; keys[0]->val.s = strdup(cmd); /* 命令タイプをセット */ - keys[1] = malloc(sizeof(HKEY)); + keys[1] = malloc_chk(sizeof(HKEY), "hash_cmdtype.key"); keys[1]->type = INT; keys[1]->val.i = (int)(type & 070); /* ハッシュ値を返す */ @@ -64,23 +64,25 @@ unsigned hash_cmdtype(const char *cmd, CMDTYPE type) { /* 命令と命令タイプがキーのハッシュ表を作成する */ bool create_cmdtype_code() { - CMDCODETAB *np; + CMDTAB *np; unsigned hashval; int i; - cmdtabsize = cmdcodesize; - cmdtype_code = malloc(cmdtabsize * sizeof(CMDCODETAB *)); - for(i = 0; i < cmdcodesize; i++) { - if((np = malloc(sizeof(CMDCODETAB))) == NULL) { - setcerr(122, NULL); /* cannot create hash table */ - return false; - } + cmdtabsize = comet2cmdsize; + cmdtype_code = malloc_chk(cmdtabsize * sizeof(CMDTAB *), "cmdtype_code"); + for(i = 0; i < cmdtabsize; i++) { + *(cmdtype_code + i) = NULL; + } + for(i = 0; i < comet2cmdsize; i++) { + np = malloc_chk(sizeof(CMDTAB), "cmdtype_code.next"); + np->cmd = NULL; + np->next = NULL; /* ハッシュ値の生成 */ - hashval = hash_cmdtype(cmdcodearray[i].cmd, cmdcodearray[i].type); + hashval = hash_cmdtype(comet2cmd[i].name, comet2cmd[i].type); /* ハッシュ表に値を追加 */ np->next = cmdtype_code[hashval]; cmdtype_code[hashval] = np; - np->cca = &(cmdcodearray[i]); + np->cmd = &(comet2cmd[i]); } return true; } @@ -89,11 +91,12 @@ bool create_cmdtype_code() /* 無効な場合は0xFFFFを返す */ WORD getcmdcode(const char *cmd, CMDTYPE type) { - CMDCODETAB *np; + CMDTAB *np; + assert(cmd != NULL); for(np = cmdtype_code[hash_cmdtype(cmd, type)]; np != NULL; np = np->next){ - if(strcmp(cmd, np->cca->cmd) == 0 && type == np->cca->type) { - return np->cca->code; + if(strcmp(cmd, np->cmd->name) == 0 && type == np->cmd->type) { + return np->cmd->code; } } return 0xFFFF; @@ -103,8 +106,9 @@ WORD getcmdcode(const char *cmd, CMDTYPE type) void free_cmdtype_code() { int i; - CMDCODETAB *np, *nq; - for(i = 0; i < cmdtabsize; i++){ + CMDTAB *np, *nq; + + for(i = 0; i < cmdtabsize; i++) { np = cmdtype_code[i]; while(np != NULL) { nq = np->next; @@ -112,6 +116,7 @@ void free_cmdtype_code() np = nq; } } + free(cmdtype_code); } /* 命令コードからハッシュ値を生成する */ @@ -120,7 +125,7 @@ unsigned hash_code(WORD code) HKEY *keys[1]; /* 命令コードをセット */ - keys[0] = malloc(sizeof(HKEY)); + keys[0] = malloc_chk(sizeof(HKEY), "hash_code.key"); keys[0]->type = INT; keys[0]->val.i = (int)(code >> 8); /* ハッシュ値を返す */ @@ -130,23 +135,25 @@ unsigned hash_code(WORD code) /* 命令コードがキーのハッシュ表を作成する */ bool create_code_type() { - CMDCODETAB *np; + CMDTAB *np; unsigned hashval; int i; - cmdtabsize = hashtabsize(cmdcodesize); - code_type = malloc(cmdtabsize * sizeof(CMDCODETAB *)); - for(i = 0; i < cmdcodesize; i++) { - if((np = malloc(sizeof(CMDCODETAB))) == NULL) { - setcerr(122, NULL); /* cannot create hash table */ - return false; - } + cmdtabsize = comet2cmdsize; + code_type = malloc_chk(cmdtabsize * sizeof(CMDTAB *), "code_type"); + for(i = 0; i < cmdtabsize; i++) { + *(code_type + i) = NULL; + } + for(i = 0; i < comet2cmdsize; i++) { + np = malloc_chk(sizeof(CMDTAB), "code_type.next"); + np->cmd = NULL; + np->next = NULL; /* ハッシュ値の生成 */ - hashval = hash_code((&cmdcodearray[i])->code); + hashval = hash_code((&comet2cmd[i])->code); /* ハッシュ表に値を追加 */ np->next = code_type[hashval]; code_type[hashval] = np; - np->cca = &cmdcodearray[i]; + np->cmd = &comet2cmd[i]; } return true; } @@ -155,10 +162,10 @@ bool create_code_type() /* 無効な場合はNONEを返す */ CMDTYPE getcmdtype(WORD code) { - CMDCODETAB *np; + CMDTAB *np; for(np = code_type[hash_code(code)]; np != NULL; np = np->next) { - if(code == np->cca->code) { - return np->cca->type; + if(code == np->cmd->code) { + return np->cmd->type; } } return NONE; @@ -168,8 +175,8 @@ CMDTYPE getcmdtype(WORD code) void free_code_type() { int i; - CMDCODETAB *np, *nq; - for(i = 0; i < cmdtabsize; i++){ + CMDTAB *np, *nq; + for(i = 0; i < cmdtabsize; i++) { np = code_type[i]; while(np != NULL) { nq = np->next; @@ -177,4 +184,5 @@ void free_code_type() np = nq; } } + free(code_type); } diff --git a/src/comet2.c b/src/comet2.c index f417de4..8233613 100644 --- a/src/comet2.c +++ b/src/comet2.c @@ -16,8 +16,8 @@ static struct option longopts[] = { }; /* comet2のエラー定義 */ -CERRARRAY cerr_comet2[] = { - { 201, "Load object file - full of COMET II memory" }, +CERR cerr_comet2[] = { + { 201, "load object file - full of COMET II memory" }, { 208, "object file is not specified" }, }; bool addcerrlist_comet2() @@ -34,9 +34,11 @@ bool loadassemble(char *file) { perror(file); return false; } - if((endptr = startptr + fread(memory, sizeof(WORD), memsize-startptr, fp)) == memsize) { + progprop->end = progprop->start + + fread(memory, sizeof(WORD), memsize-progprop->start, fp); + if(progprop->end == memsize) { setcerr(201, NULL); /* Load object file - full of COMET II memory */ - fprintf(stderr, "Execute error - %d: %s\n", cerrno, cerrmsg); + fprintf(stderr, "Execute error - %d: %s\n", cerr->num, cerr->msg); status = false; } fclose(fp); @@ -46,10 +48,13 @@ bool loadassemble(char *file) { /* comet2コマンド */ int main(int argc, char *argv[]) { - int opt; + int opt, retval = 0; const char *usage = "Usage: %s [-tTdh] [-M ] [-C ] FILE\n"; + /* エラーの初期化 */ + cerr = malloc_chk(sizeof(CERR), "cerr"); addcerrlist_comet2(); + /* オプションの処理 */ while((opt = getopt_long(argc, argv, "tTdM:C:h", longopts, NULL)) != -1) { switch(opt) { case 't': @@ -78,21 +83,22 @@ int main(int argc, char *argv[]) } if(argv[optind] == NULL) { setcerr(208, NULL); /* object file is not specified */ - fprintf(stderr, "comet2 error - %d: %s\n", cerrno, cerrmsg); - goto comet2err; + fprintf(stderr, "comet2 error - %d: %s\n", cerr->num, cerr->msg); + exit(-1); } reset(); - startptr = 0; + progprop->start = 0; if(loadassemble(argv[optind]) == true) { - exec(); /* プログラム実行 */ + create_code_type(); /* 命令と命令タイプがキーのハッシュ表を作成 */ + exec(); /* プログラム実行 */ + free_code_type(); /* 命令と命令タイプがキーのハッシュ表を解放 */ } /* COMET II仮想マシンのシャットダウン */ shutdown(); - if(cerrno > 0) { - goto comet2err; + if(cerr->num > 0) { + retval = -1; } - return 0; -comet2err: + /* エラーの解放 */ freecerr(); - exit(-1); + return retval; } diff --git a/src/dump.c b/src/dump.c index 51650c9..60ca21d 100644 --- a/src/dump.c +++ b/src/dump.c @@ -7,7 +7,7 @@ void dumpmemory() const int col = 16; int i; /* Header */ - fprintf(stdout, "#%04X: adr :", PR); + fprintf(stdout, "#%04X: adr :", cpu->pr); for(i = 0; i < memsize && i < col; i++) { fprintf(stdout, " %04X", i); } @@ -15,7 +15,7 @@ void dumpmemory() /* Memory */ for(i = 0; i < memsize; i++) { if(i % col == 0) { - fprintf(stdout, "#%04X: %04X: ", PR, i); + fprintf(stdout, "#%04X: %04X: ", cpu->pr, i); } fprintf(stdout, "%04X", memory[i]); if(i > 0 && (i + 1) % col == 0) { @@ -30,11 +30,14 @@ void dumpmemory() void dspregister() { int i; - for(i = 0; i < REGSIZE; i++ ) { - fprintf(stdout, "#%04X: GR%d: ", PR, i); - print_dumpword(GR[i], (&execmode)->logical); + for(i = 0; i < GRSIZE; i++ ) { + fprintf(stdout, "#%04X: GR%d: ", cpu->pr, i); + print_dumpword(cpu->gr[i], (&execmode)->logical); } - fprintf(stdout, "#%04X: SP: %6d = #%04X = %s\n", PR, SP, SP, word2bit(SP)); - fprintf(stdout, "#%04X: PR: %6d = #%04X = %s\n", PR, PR, PR, word2bit(PR)); - fprintf(stdout, "#%04X: FR (OF SF ZF): %s\n", PR, (word2bit(FR)+13)); + fprintf(stdout, "#%04X: SP: %6d = #%04X = %s\n", + cpu->pr, cpu->sp, cpu->sp, word2bit(cpu->sp)); + fprintf(stdout, "#%04X: PR: %6d = #%04X = %s\n", + cpu->pr, cpu->pr, cpu->pr, word2bit(cpu->pr)); + fprintf(stdout, "#%04X: FR (OF SF ZF): %s\n", + cpu->pr, (word2bit(cpu->fr)+13)); } diff --git a/src/dumpword.c b/src/dumpword.c index 1a8a73e..073dd3e 100644 --- a/src/dumpword.c +++ b/src/dumpword.c @@ -16,6 +16,7 @@ int main(int argc, char *argv[]) WORD word; const char *usage = "Usage: %s [-alh] WORD\n"; + cerr = malloc_chk(sizeof(CERR), "cerr"); while((opt = getopt_long(argc, argv, "alh", longopts, NULL)) != -1) { switch(opt) { case 'l': @@ -36,8 +37,8 @@ int main(int argc, char *argv[]) } /* WORD値に変換 */ word = nh2word(argv[optind]); - if(cerrno > 0) { - fprintf(stderr, "Dumpword Error - %d: %s\n", cerrno, cerrmsg); + if(cerr->num > 0) { + fprintf(stderr, "Dumpword Error - %d: %s\n", cerr->num, cerr->msg); exit(-1); } fprintf(stdout, "%6s: ", argv[optind]); diff --git a/src/exec.c b/src/exec.c index 2cd6d8a..07e4fcd 100644 --- a/src/exec.c +++ b/src/exec.c @@ -2,7 +2,7 @@ #include "exec.h" /* 実行のエラー定義 */ -CERRARRAY cerr_exec[] = { +CERR 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" }, @@ -23,10 +23,10 @@ EXECMODE execmode = {false, false, false}; void svcin() { int i; - char *buffer = malloc(INSIZE + 1); + char *buffer = malloc_chk(INSIZE + 1,"svcin.buffer"); if(fgets(buffer, INSIZE, stdin) == NULL) { - memory[GR[1]] = memory[GR[2]] = 0x0; + memory[cpu->gr[1]] = memory[cpu->gr[2]] = 0x0; return; } for(i = 0; i < INSIZE; i++) { @@ -34,13 +34,13 @@ void svcin() --i; break; } - if(GR[1] + i >= memsize - 1) { + if(cpu->gr[1] + i >= memsize - 1) { setcerr(202, NULL); /* SVC input - out of Input memory */ break; } - memory[GR[1]+i] = *(buffer + i); + memory[cpu->gr[1]+i] = *(buffer + i); } - memory[GR[2]] = i + 1; + memory[cpu->gr[2]] = i + 1; } /* 標準出力へ文字データを書出(SVC 2) */ @@ -49,14 +49,14 @@ void svcout() int i; WORD w; - for(i = 0; i < memory[GR[2]]; i++) { - if(GR[1] + i >= memsize - 1) { + for(i = 0; i < memory[cpu->gr[2]]; i++) { + if(cpu->gr[1] + i >= memsize - 1) { setcerr(203, NULL); /* SVC output - out of Comet II memory */ return; } /* 「文字の組」の符号表に記載された文字と、改行(CR)/タブを表示 */ /* それ以外の文字は、「.」で表す */ - if(((w = memory[GR[1]+i]) >= 0x20 && w <= 0x7E) || w == 0xA || w == '\t') { + if(((w = memory[cpu->gr[1]+i]) >= 0x20 && w <= 0x7E) || w == 0xA || w == '\t') { putchar((char)w); } else { putchar('.'); @@ -67,14 +67,14 @@ void svcout() /* ロード/論理積/論理和/排他的論理和のフラグ設定。OFは常に0 */ void setfr(WORD val) { - FR = 0x0; + cpu->fr = 0x0; /* 第15ビットが1のとき、SFは1 */ if((val & 0x8000) > 0x0) { - FR += SF; + cpu->fr += SF; } /* 演算結果が0のとき、ZFは1 */ if(val == 0x0) { - FR += ZF; + cpu->fr += ZF; } } @@ -83,16 +83,16 @@ WORD adda(WORD val0, WORD val1) { WORD res; long temp; - FR = 0x0; + cpu->fr = 0x0; temp = (signed short)val0 + (signed short)val1; if(temp > 32767 || temp < -32768) { - FR += OF; + cpu->fr += OF; } if(((res = (WORD)(temp & 0xFFFF)) & 0x8000) == 0x8000) { - FR += SF; + cpu->fr += SF; } else if(res == 0x0) { - FR += ZF; + cpu->fr += ZF; } return res; } @@ -108,15 +108,15 @@ WORD addl(WORD val0, WORD val1) { long temp; WORD res; - FR = 0x0; + cpu->fr = 0x0; if((temp = val0 + val1) < 0 || temp > 65535) { - FR += OF; + cpu->fr += OF; } if(((res = (WORD)(temp & 0xFFFF)) & 0x8000) == 0x8000) { - FR += SF; + cpu->fr += SF; } else if(res == 0x0) { - FR += ZF; + cpu->fr += ZF; } return res; } @@ -130,22 +130,22 @@ WORD subl(WORD val0, WORD val1) /* 算術比較のフラグ設定。OFは常に0 */ void cpa(WORD val0, WORD val1) { - FR = 0x0; + cpu->fr = 0x0; if((short)val0 < (short)val1) { - FR = SF; + cpu->fr = SF; } else if(val0 == val1) { - FR = ZF; + cpu->fr = ZF; } } /* 論理比較のフラグ設定。OFは常に0 */ void cpl(WORD val0, WORD val1) { - FR = 0x0; + cpu->fr = 0x0; if(val0 < val1) { - FR = SF; + cpu->fr = SF; } else if(val0 == val1) { - FR = ZF; + cpu->fr = ZF; } } @@ -156,7 +156,7 @@ WORD sla(WORD val0, WORD val1) WORD sign, res, last = 0x0; int i; - FR = 0x0; + cpu->fr = 0x0; sign = val0 & 0x8000; res = val0 & 0x7FFF; for(i = 0; i < val1; i++) { @@ -166,15 +166,15 @@ WORD sla(WORD val0, WORD val1) res = sign | (res & 0x7FFF); /* OFに、レジスタから最後に送り出されたビットの値を設定 */ if(last > 0x0) { - FR += OF; + cpu->fr += OF; } /* 符号(第15ビット)が1のとき、SFは1 */ if(sign > 0x0) { - FR += SF; + cpu->fr += SF; } /* 演算結果が0のとき、ZFは1 */ if(res == 0x0) { - FR += ZF; + cpu->fr += ZF; } return res; } @@ -187,7 +187,7 @@ WORD sra(WORD val0, WORD val1) WORD sign, res, last = 0x0; int i; - FR = 0x0; + cpu->fr = 0x0; sign = val0 & 0x8000; res = val0 & 0x7FFF; for(i = 0; i < val1; i++) { @@ -200,15 +200,15 @@ WORD sra(WORD val0, WORD val1) res = sign | res; /* OFに、レジスタから最後に送り出されたビットの値を設定 */ if(last > 0x0) { - FR += OF; + cpu->fr += OF; } /* 符号(第15ビット)が1のとき、SFは1 */ if(sign > 0x0) { - FR += SF; + cpu->fr += SF; } /* 演算結果が0のとき、ZFは1 */ if(res == 0x0) { - FR += ZF; + cpu->fr += ZF; } return res; } @@ -219,22 +219,22 @@ WORD sll(WORD val0, WORD val1) WORD res = val0, last = 0x0; int i; - FR = 0x0; + cpu->fr = 0x0; for(i = 0; i < val1; i++) { last = res & 0x8000; res <<= 1; } /* OFに、レジスタから最後に送り出されたビットの値を設定 */ if(last > 0x0) { - FR += OF; + cpu->fr += OF; } /* 第15ビットが1のとき、SFは1 */ if((res & 0x8000) > 0x0) { - FR += SF; + cpu->fr += SF; } /* 演算結果が0のとき、ZFは1 */ if(res == 0x0) { - FR += ZF; + cpu->fr += ZF; } return res; } @@ -245,105 +245,101 @@ WORD srl(WORD val0, WORD val1) WORD res = val0, last = 0x0; int i; - FR = 0x0; + cpu->fr = 0x0; for(i = 0; i < val1; i++) { last = res & 0x0001; res >>= 1; } /* OFに、レジスタから最後に送り出されたビットの値を設定 */ if(last > 0x0) { - FR += OF; + cpu->fr += OF; } /* 第15ビットが1のとき、SFは1 */ if((res & 0x8000) > 0x0) { - FR += SF; + cpu->fr += SF; } /* 演算結果が0のとき、ZFは1 */ if(res == 0x0) { - FR += ZF; + cpu->fr += ZF; } return res; } /* 仮想マシンCOMET IIでの実行 */ -void exec() +bool exec() { WORD op, r_r1, x_r2, val; CMDTYPE cmdtype; - char *errpr = malloc(CERRSTRSIZE + 1); + char *errpr = malloc_chk(CERRSTRSIZE + 1, "exec.errpr"); clock_t clock_begin, clock_end; addcerrlist_exec(); if(execmode.trace) { fprintf(stdout, "\nExecuting machine codes\n"); } - /* フラグレジスタの初期値設定 */ - FR = 0x0; - SP = memsize; - PR = startptr; - if(create_code_type() == false) { - goto execerr; - } + /* レジスタの初期化 */ + cpu->sp = memsize; + cpu->pr = progprop->start; /* 機械語の実行 */ for (; ; ) { clock_begin = clock(); /* プログラムレジスタのアドレスが主記憶の範囲外の場合はエラー */ - if(PR >= memsize) { - sprintf(errpr, "PR:#%04X", PR); + if(cpu->pr >= memsize) { + sprintf(errpr, "PR:#%04X", cpu->pr); setcerr(204, errpr); /* Program Register (PR) - out of COMET II memory */ } /* スタック領域のアドレスが主記憶の範囲外の場合はエラー */ - if(SP > memsize) { - sprintf(errpr, "PR:#%04X", PR); + if(cpu->sp > memsize) { + sprintf(errpr, "PR:#%04X", cpu->pr); setcerr(207, errpr); /* Stack Pointer (SP) - out of COMET II memory */ } /* スタック領域を確保できない場合はエラー */ - if(SP <= endptr) { - sprintf(errpr, "PR:#%04X", PR); + if(cpu->sp <= progprop->end) { + sprintf(errpr, "PR:#%04X", cpu->pr); setcerr(205, errpr); /* Stack Pointer (SP) - cannot allocate stack buffer */ } /* 命令の取り出し */ - op = memory[PR] & 0xFF00; + op = memory[cpu->pr] & 0xFF00; /* 命令の解読 */ cmdtype = getcmdtype(op); - r_r1 = (memory[PR] >> 4) & 0xF; - x_r2 = memory[PR] & 0xF; + r_r1 = (memory[cpu->pr] >> 4) & 0xF; + x_r2 = memory[cpu->pr] & 0xF; /* エラー発生時は終了 */ - if(cerrno > 0) { + if(cerr->num > 0) { goto execerr; } /* traceオプション指定時、レジスタを出力 */ if(execmode.trace){ - fprintf(stdout, "#%04X: Register::::\n", PR); + fprintf(stdout, "#%04X: Register::::\n", cpu->pr); dspregister(); } /* dumpオプション指定時、メモリを出力 */ if(execmode.dump){ - fprintf(stdout, "#%04X: Memory::::\n", PR); + fprintf(stdout, "#%04X: Memory::::\n", cpu->pr); dumpmemory(); } /* どちらかのオプション指定時、改行を出力 */ if(execmode.dump || execmode.trace) { fprintf(stdout, "\n"); } - PR++; + cpu->pr++; /* オペランドの取り出し */ if(cmdtype == R1_R2) { - assert(x_r2 < REGSIZE); - val = GR[x_r2]; + assert(x_r2 < GRSIZE); + val = cpu->gr[x_r2]; } else if(cmdtype == R_ADR_X || cmdtype == R_ADR_X_ || cmdtype == ADR_X) { - assert(x_r2 < REGSIZE); + assert(x_r2 < GRSIZE); /* 実効アドレス(値または値が示す番地)を取得 */ - val = memory[PR++]; + val = memory[cpu->pr++]; /* 指標アドレスを加算 */ if(x_r2 > 0x0) { - val += GR[x_r2]; + val += cpu->gr[x_r2]; } /* ロード/算術論理演算命令/比較演算命令では、アドレスに格納されている内容を取得 */ if(cmdtype == R_ADR_X_) { if(val >= memsize) { - sprintf(errpr, "PR:#%04X", PR-1); + sprintf(errpr, "PR:#%04X", cpu->pr-1); setcerr(206, errpr); /* Address - out of COMET II memory */ goto execerr; } @@ -360,107 +356,107 @@ void exec() case 0x0: /* NOP */ break; case 0x1000: /* LD */ - setfr(GR[r_r1] = val); + setfr(cpu->gr[r_r1] = val); break; case 0x1100: /* ST */ - memory[val] = GR[r_r1]; + memory[val] = cpu->gr[r_r1]; break; case 0x1200: /* LAD */ - GR[r_r1] = val; + cpu->gr[r_r1] = val; break; case 0x2000: /* ADDA */ - GR[r_r1] = adda(GR[r_r1], val); + cpu->gr[r_r1] = adda(cpu->gr[r_r1], val); break; case 0x2100: /* SUBA */ - GR[r_r1] = suba(GR[r_r1], val); + cpu->gr[r_r1] = suba(cpu->gr[r_r1], val); break; case 0x2200: /* ADDL */ - GR[r_r1] = addl(GR[r_r1], val); + cpu->gr[r_r1] = addl(cpu->gr[r_r1], val); break; case 0x2300: /* SUBL */ - GR[r_r1] = subl(GR[r_r1], val); + cpu->gr[r_r1] = subl(cpu->gr[r_r1], val); break; case 0x3000: /* AND */ - setfr(GR[r_r1] &= val); + setfr(cpu->gr[r_r1] &= val); break; case 0x3100: /* OR */ - setfr(GR[r_r1] |= val); + setfr(cpu->gr[r_r1] |= val); break; case 0x3200: /* XOR */ - setfr(GR[r_r1] ^= val); + setfr(cpu->gr[r_r1] ^= val); break; case 0x4000: /* CPA */ - cpa(GR[r_r1], val); + cpa(cpu->gr[r_r1], val); break; case 0x4100: /* CPL */ - cpl(GR[r_r1], val); + cpl(cpu->gr[r_r1], val); break; case 0x5000: /* SLA */ - GR[r_r1] = sla(GR[r_r1], val); + cpu->gr[r_r1] = sla(cpu->gr[r_r1], val); break; case 0x5100: /* SRA */ - GR[r_r1] = sra(GR[r_r1], val); + cpu->gr[r_r1] = sra(cpu->gr[r_r1], val); break; case 0x5200: /* SLL */ - GR[r_r1] = sll(GR[r_r1], val); + cpu->gr[r_r1] = sll(cpu->gr[r_r1], val); break; case 0x5300: /* SRL */ - GR[r_r1] = srl(GR[r_r1], val); + cpu->gr[r_r1] = srl(cpu->gr[r_r1], val); break; case 0x6100: /* JMI */ - if((FR & SF) > 0) { - PR = val; + if((cpu->fr & SF) > 0) { + cpu->pr = val; } break; case 0x6200: /* JNZ */ - if((FR & ZF) == 0) { - PR = val; + if((cpu->fr & ZF) == 0) { + cpu->pr = val; } break; case 0x6300: /* JZE */ - if((FR & ZF) > 0) { - PR = val; + if((cpu->fr & ZF) > 0) { + cpu->pr = val; } break; case 0x6400: /* JUMP */ - PR = val; + cpu->pr = val; break; case 0x6500: /* JPL */ - if((FR & (SF | ZF)) == 0) { - PR = val; + if((cpu->fr & (SF | ZF)) == 0) { + cpu->pr = val; } break; case 0x6600: /* JOV */ - if((FR & OF) > 0) { - PR = val; + if((cpu->fr & OF) > 0) { + cpu->pr = val; } break; case 0x7000: /* PUSH */ - assert(SP > endptr && SP <= memsize); - memory[--SP] = val; + assert(cpu->sp > progprop->end && cpu->sp <= memsize); + memory[--cpu->sp] = val; break; case 0x7100: /* POP */ - assert(SP > endptr && SP <= memsize); - GR[r_r1] = memory[SP++]; + assert(cpu->sp > progprop->end && cpu->sp <= memsize); + cpu->gr[r_r1] = memory[cpu->sp++]; break; case 0x8000: /* CALL */ - assert(SP > endptr && SP <= memsize); - memory[--SP] = PR; - PR = val; + assert(cpu->sp > progprop->end && cpu->sp <= memsize); + memory[--(cpu->sp)] = cpu->pr; + cpu->pr = val; break; case 0x8100: /* RET */ - assert(SP > endptr && SP <= memsize); - if(SP == memsize) { - return; + assert(cpu->sp > progprop->end && cpu->sp <= memsize); + if(cpu->sp == memsize) { + return false; } else { - PR = memory[SP++]; + cpu->pr = memory[(cpu->sp)++]; break; } case 0xF000: /* SVC */ switch(val) { case 0x0: /* EXIT */ - return; + return true; case 0x1: /* IN */ svcin(); break; @@ -474,10 +470,9 @@ void exec() do { clock_end = clock(); } while(clock_end - clock_begin < CLOCKS_PER_SEC / clocks); - #if 0 - printf("PR:%04X; time: %f\n", PR, (double)((clock_end - clock_begin) * CLOCKS_PER_SEC)); - #endif } + return true; execerr: - fprintf(stderr, "Execute error - %d: %s\n", cerrno, cerrmsg); + fprintf(stderr, "Execute error - %d: %s\n", cerr->num, cerr->msg); + return false; } diff --git a/src/label.c b/src/label.c index c705ad1..a65e293 100644 --- a/src/label.c +++ b/src/label.c @@ -10,11 +10,11 @@ unsigned labelhash(const char *prog, const char *label) HKEY *keys[2]; int i = 0; if(prog != NULL) { - keys[i] = malloc(sizeof(HKEY)); + keys[i] = malloc_chk(sizeof(HKEY), "labelhash.key"); keys[i]->type = CHARS; keys[i++]->val.s = strdup(prog); } - keys[i] = malloc(sizeof(HKEY)); + keys[i] = malloc_chk(sizeof(HKEY), "labelhash.key"); keys[i]->type = CHARS; keys[i]->val.s = strdup(label); /* ハッシュ値を返す */ @@ -49,7 +49,7 @@ bool addlabel(const char *prog, const char *label, WORD adr) return false; } /* メモリを確保 */ - if((np = malloc(sizeof(LABELTAB))) == NULL) { + if((np = malloc_chk(sizeof(LABELTAB), "addlabel.np")) == NULL) { goto cerr102; } /* プログラム名を設定 */ @@ -93,7 +93,7 @@ void printlabel() for(i = 0; i < LABELTABSIZE; i++) { for(np = labels[i]; np != NULL; np = np->next) { assert(np->label != NULL); - ar[asize] = malloc(sizeof(LABELARRAY)); + ar[asize] = malloc_chk(sizeof(LABELARRAY), "ar[asize]"); ar[asize]->prog = (np->prog == NULL ? NULL : strdup(np->prog)); ar[asize]->label = strdup(np->label); ar[asize++]->adr = np->adr; diff --git a/src/macro.c b/src/macro.c index 354e7ec..e41eef9 100644 --- a/src/macro.c +++ b/src/macro.c @@ -14,25 +14,25 @@ bool writeIN(const char *ibuf, const char *len, PASS pass) bool status = false; /* PUSH 0,GR1 */ - writememory(0x7001, ptr++, pass); - writememory(0x0, ptr++, pass); + writememory(0x7001, (asprop->ptr)++, pass); + writememory(0x0, (asprop->ptr)++, pass); /* PUSH 0,GR2 */ - writememory(0x7002, ptr++, pass); - writememory(0x0, ptr++, pass); + writememory(0x7002, (asprop->ptr)++, pass); + writememory(0x0, (asprop->ptr)++, pass); /* LAD GR1,IBUF */ - writememory(0x1210, ptr++, pass); - writememory(getadr(prog, ibuf, pass), ptr++, pass); + writememory(0x1210, (asprop->ptr)++, pass); + writememory(getadr(asprop->prog, ibuf, pass), (asprop->ptr)++, pass); /* LAD GR2,LEN */ - writememory(0x1220, ptr++, pass); - writememory(getadr(prog, len, pass), ptr++, pass); + writememory(0x1220, (asprop->ptr)++, pass); + writememory(getadr(asprop->prog, len, pass), (asprop->ptr)++, pass); /* SVC 1 */ - writememory(0xF000, ptr++, pass); - writememory(0x0001, ptr++, pass); + writememory(0xF000, (asprop->ptr)++, pass); + writememory(0x0001, (asprop->ptr)++, pass); /* POP GR2 */ - writememory(0x7120, ptr++, pass); + writememory(0x7120, (asprop->ptr)++, pass); /* POP GR1 */ - writememory(0x7110, ptr++, pass); - if(cerrno == 0) { + writememory(0x7110, (asprop->ptr)++, pass); + if(cerr->num == 0) { status = true; } return status; @@ -54,44 +54,44 @@ bool writeOUT(const char *obuf, const char *len, PASS pass) bool status = false; /* PUSH 0,GR1 */ - writememory(0x7001, ptr++, pass); - writememory(0x0, ptr++, pass); + writememory(0x7001, (asprop->ptr)++, pass); + writememory(0x0, (asprop->ptr)++, pass); /* PUSH 0,GR2 */ - writememory(0x7002, ptr++, pass); - writememory(0x0, ptr++, pass); + writememory(0x7002, (asprop->ptr)++, pass); + writememory(0x0, (asprop->ptr)++, pass); /* LAD GR1,OBUF */ - writememory(0x1210, ptr++, pass); - writememory(getadr(prog, obuf, pass), ptr++, pass); + writememory(0x1210, (asprop->ptr)++, pass); + writememory(getadr(asprop->prog, obuf, pass), (asprop->ptr)++, pass); /* LAD GR2,OLEN */ - writememory(0x1220, ptr++, pass); - writememory(getadr(prog, len, pass), ptr++, pass); + writememory(0x1220, (asprop->ptr)++, pass); + writememory(getadr(asprop->prog, len, pass), (asprop->ptr)++, pass); /* SVC 2 */ - writememory(0xF000, ptr++, pass); - writememory(0x0002, ptr++, pass); + writememory(0xF000, (asprop->ptr)++, pass); + writememory(0x0002, (asprop->ptr)++, pass); /* LAD GR1,=#A */ - writememory(0x1210, ptr++, pass); + writememory(0x1210, (asprop->ptr)++, pass); if(pass == FIRST) { - ptr++; + (asprop->ptr)++; } else { - writememory(lptr, ptr++, pass); /* リテラルのアドレスを書込 */ + writememory(asprop->lptr, (asprop->ptr)++, pass); /* リテラルのアドレスを書込 */ } - writememory(0xA, lptr++, pass); + writememory(0xA, (asprop->lptr)++, pass); /* LAD GR2,=1 */ - writememory(0x1220, ptr++, pass); + writememory(0x1220, (asprop->ptr)++, pass); if(pass == FIRST) { - ptr++; + (asprop->ptr)++; } else { - writememory(lptr, ptr++, pass); /* リテラルのアドレスを書込 */ + writememory(asprop->lptr, (asprop->ptr)++, pass); /* リテラルのアドレスを書込 */ } - writememory(0x1, lptr++, pass); + writememory(0x1, (asprop->lptr)++, pass); /* SVC 2 */ - writememory(0xF000, ptr++, pass); - writememory(0x0002, ptr++, pass); + writememory(0xF000, (asprop->ptr)++, pass); + writememory(0x0002, (asprop->ptr)++, pass); /* POP GR2 */ - writememory(0x7120, ptr++, pass); + writememory(0x7120, (asprop->ptr)++, pass); /* POP GR1 */ - writememory(0x7110, ptr++, pass); - if(cerrno == 0) { + writememory(0x7110, (asprop->ptr)++, pass); + if(cerr->num == 0) { status = true; } return status; @@ -111,10 +111,10 @@ bool writeRPUSH(PASS pass) { bool status = false; for(i = 1; i <= 7; i++) { - writememory(0x7000 + i, ptr++, pass); /* PUSH GRn */ - writememory(0x0, ptr++, pass); + writememory(0x7000 + i, (asprop->ptr)++, pass); /* PUSH GRn */ + writememory(0x0, (asprop->ptr)++, pass); } - if(cerrno == 0) { + if(cerr->num == 0) { status = true; } return status; @@ -134,9 +134,9 @@ bool writeRPOP(PASS pass) { int i; bool status = false; for(i = 7; i >= 1; i--) { - writememory((0x7100 + (i << 4)), ptr++, pass); /* POP GRn */ + writememory((0x7100 + (i << 4)), (asprop->ptr)++, pass); /* POP GRn */ } - if(cerrno == 0) { + if(cerr->num == 0) { status = true; } return status; diff --git a/src/struct.c b/src/struct.c index 7c3e785..1f62a13 100644 --- a/src/struct.c +++ b/src/struct.c @@ -3,37 +3,40 @@ /* COMET IIのメモリ */ WORD *memory; -/* COMET IIのCPUレジスタ */ -WORD GR[REGSIZE], SP, PR, FR; - -/* メモリーサイズ */ +/* メモリサイズ */ int memsize = DEFAULT_MEMSIZE; +/* COMET IIのCPU */ +CPU *cpu; + /* クロック周波数 */ int clocks = DEFAULT_CLOCKS; -/* 実行開始番地 */ -WORD startptr = 0x0; - -/* 実行終了番地 */ -WORD endptr = 0x0; +/* CASL2プログラムのプロパティ */ +PROGPROP *progprop; /* COMET II仮想マシンのリセット */ void reset() { int i; - for(i = 0; i < REGSIZE; i++) { - GR[i] = 0x0; - } - SP = PR = FR = 0x0; - memory = malloc(memsize * sizeof(WORD)); + /* メモリの初期化 */ + memory = malloc_chk(memsize * sizeof(WORD), "memory"); for(i = 0; i < memsize; i++) { memory[i] = 0x0; } + /* CPUの初期化 */ + cpu = malloc_chk(sizeof(CPU), "cpu"); + for(i = 0; i < GRSIZE; i++) { + cpu->gr[i] = 0x0; + } + cpu->sp = cpu->pr = cpu->fr = 0x0; + /* CASL2プログラムのプロパティ */ + progprop = malloc_chk(sizeof(PROGPROP), "progprop"); } /* COMET II仮想マシンのシャットダウン */ void shutdown() { + free(cpu); free(memory); } diff --git a/src/token.c b/src/token.c index ec86ac5..e8b630b 100644 --- a/src/token.c +++ b/src/token.c @@ -4,7 +4,7 @@ /* 「,」区切りの文字列から、オペランドのトークンを取得 */ OPD *opdtok(const char *str) { - OPD *opd = malloc(sizeof(OPD)); + OPD *opd = malloc_chk(sizeof(OPD), "opd"); char *p, *q, *sepp; int sepc = ',', qcnt = 0; bool quoting = false; @@ -67,7 +67,7 @@ CMDLINE *linetok(const char *line) { char *tokens, *p, *sepp; bool quoting = false; - CMDLINE *cmdl = malloc(sizeof(CMDLINE)); + CMDLINE *cmdl = malloc_chk(sizeof(CMDLINE), "cmdl"); if(line == NULL || strlen(line) == 0) { return NULL; @@ -119,7 +119,7 @@ CMDLINE *linetok(const char *line) p++; } /* オペランドを取得 */ - cmdl->opd = malloc(sizeof(OPD)); + cmdl->opd = malloc_chk(sizeof(OPD), "cmdl->opd"); /* 改行かタブまでの文字列を取得。 「'」で囲まれた文字列に含まれる場合があるため、空白は無視 */ if((sepp = p + strcspn(p, "\t\n")) > p) { diff --git a/src/word.c b/src/word.c index 3b30967..9371758 100644 --- a/src/word.c +++ b/src/word.c @@ -1,11 +1,12 @@ #include "word.h" /* wordのエラー定義 */ -CERRARRAY cerr_word[] = { +CERR cerr_word[] = { { 114, "not integer" }, { 115, "not hex" }, { 116, "out of hex range" }, }; + bool addcerrlist_word() { return addcerrlist(ARRAYSIZE(cerr_word), cerr_word); @@ -72,7 +73,7 @@ WORD nh2word(const char *str) /* WORD値を10進数の文字列に変換 */ char *word2n(WORD word) { - char *p = malloc(6), *q = malloc(6); + char *p = malloc_chk(6, "word2n.p"), *q = malloc_chk(6, "word2n.q"); int i = 0, j; do{ *(p + i++) = word % 10 + '0'; @@ -88,7 +89,7 @@ char *word2n(WORD word) char *word2bit(const WORD word) { WORD mask = 0x8000; - char *bit = malloc(16 + 1), *p; + char *bit = malloc_chk(16 + 1, "word2bit.bit"), *p; p = bit; do { *p++ = (word & mask) ? '1' : '0'; diff --git a/test/integration/comet2/err_201/0.txt b/test/integration/comet2/err_201/0.txt index 7fae8ed..58828f9 100644 --- a/test/integration/comet2/err_201/0.txt +++ b/test/integration/comet2/err_201/0.txt @@ -1,2 +1,2 @@ ../../../../comet2 -M8 a.o -Execute error - 201: Load object file - full of COMET II memory +Execute error - 201: load object file - full of COMET II memory diff --git a/test/unit/CERRARRAY.c b/test/unit/CERRARRAY.c index 9499b67..a20b7c7 100644 --- a/test/unit/CERRARRAY.c +++ b/test/unit/CERRARRAY.c @@ -1,7 +1,7 @@ #include "cerr.h" /* エラー番号とエラーメッセージ */ -CERRARRAY cerr_utest[] = { +CERR cerr_utest[] = { { 101, "label already defined" }, { 102, "label table is full" }, { 103, "label not found" }, diff --git a/test/unit/addcerrlist/addcerrlist.c b/test/unit/addcerrlist/addcerrlist.c index 6228a82..884931c 100644 --- a/test/unit/addcerrlist/addcerrlist.c +++ b/test/unit/addcerrlist/addcerrlist.c @@ -1,11 +1,11 @@ #include #include "casl2.h" -CERRARRAY cerr_0[] = { +CERR cerr_0[] = { { 126, "source file is not specified" }, }; -CERRARRAY cerr_1[] = { +CERR cerr_1[] = { { 101, "label already defined" }, { 102, "label table is full" }, { 103, "label not found" }, @@ -30,23 +30,23 @@ CERRARRAY cerr_1[] = { { 125, "not GR in operand x" }, }; -CERRARRAY cerr_2[] = { +CERR cerr_2[] = { { 114, "not integer" }, { 115, "not hex" }, { 116, "out of hex range" }, }; -CERRARRAY cerr_3[] = { +CERR cerr_3[] = { { 114, "not integer" }, { 115, "not hex" }, { 116, "out of hex range" }, }; -CERRARRAY cerr_4[] = { +CERR cerr_4[] = { { 201, "Load object file - full of COMET II memory" }, }; -CERRARRAY cerr_5[] = { +CERR 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" }, @@ -57,14 +57,15 @@ CERRARRAY cerr_5[] = { 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); + for(p = cerrlist; p != NULL; p = p->next) { + printf("%d: %s\n", p->cerr->num, p->cerr->msg); } return 0; } diff --git a/test/unit/cerrtest/cerrtest.c b/test/unit/cerrtest/cerrtest.c index 0c8e922..8e3ea72 100644 --- a/test/unit/cerrtest/cerrtest.c +++ b/test/unit/cerrtest/cerrtest.c @@ -1,7 +1,7 @@ #include #include "casl2.h" -static CERRARRAY cerr_utest[] = { +static CERR cerr_utest[] = { { 101, "label already defined" }, { 102, "label table is full" }, { 103, "label not found" }, @@ -45,14 +45,14 @@ int main(){ }; const char *str[] = {NULL, "foobar"}; addcerrlist(ARRAYSIZE(cerr_utest), cerr_utest); + /* エラーの初期化 */ + cerr = malloc_chk(sizeof(CERR), "cerr"); for(i = 0; i < ARRAYSIZE(str); i++) { for(j = 0; j < ARRAYSIZE(code); j++) { setcerr(code[j], str[i]); - printf("%d: %s - %d\t%s\n", code[j], str[i], cerrno, cerrmsg); - if(cerrno != 0) { - freecerr(); - } + printf("%d: %s - %d\t%s\n", code[j], str[i], cerr->num, cerr->msg); } } + freecerr(); return 0; } diff --git a/test/unit/getcmdcode/getcmdcode.c b/test/unit/getcmdcode/getcmdcode.c index 335017d..2c5286d 100644 --- a/test/unit/getcmdcode/getcmdcode.c +++ b/test/unit/getcmdcode/getcmdcode.c @@ -23,14 +23,16 @@ int main(){ { "SVC", ADR_X }, { "RET", NONE } }; create_cmdtype_code(); + /* エラーの初期化 */ + cerr = malloc_chk(sizeof(CERR), "cerr"); for(i = 0; i < sizeof(cmdcodelist)/sizeof(cmdcodelist[0]); i++) { code = getcmdcode(cmdcodelist[i].cmd, cmdcodelist[i].type); printf("%s:0%02o ---> #%04X\n", cmdcodelist[i].cmd, cmdcodelist[i].type, code); - if(cerrno != 0) { - printf("\terror - %d: %s", cerrno, cerrmsg); - freecerr(); + if(cerr->num != 0) { + printf("\terror - %d: %s", cerr->num, cerr->msg); } } + freecerr(); free_cmdtype_code(); return 0; } diff --git a/test/unit/print_cmdtype_code/print_cmdtype_code.c b/test/unit/print_cmdtype_code/print_cmdtype_code.c index bb31614..04d311e 100644 --- a/test/unit/print_cmdtype_code/print_cmdtype_code.c +++ b/test/unit/print_cmdtype_code/print_cmdtype_code.c @@ -4,8 +4,8 @@ int compare_code(const void *a, const void *b) { - const CMDCODEARRAY ca = **(const CMDCODEARRAY **)a; - const CMDCODEARRAY cb = **(const CMDCODEARRAY **)b; + const CMDTYPECODE ca = **(const CMDTYPECODE **)a; + const CMDTYPECODE cb = **(const CMDTYPECODE **)b; int diff; if((diff = strcmp(ca.cmd, cb.cmd)) == 0) { return ca.type - cb.type; @@ -19,12 +19,12 @@ void print_cmdtype_code() { int i, j = 0; CMDCODETAB *np; - CMDCODEARRAY **ar; - ar = malloc(sizeof(*ar) * cmdcodesize); + CMDTYPECODE **ar; + ar = malloc(sizeof(*ar) * cmdtypecodesize); for(i = 0; i < cmdtabsize; i++) { np = cmdtype_code[i]; while(np != NULL) { - ar[j++] = np->cca; + ar[j++] = np->cmdtypecode; np = np->next; } } @@ -36,11 +36,16 @@ void print_cmdtype_code() int main() { + /* エラーの初期化 */ + cerr = malloc_chk(sizeof(CERR), "cerr"); + /* ハッシュ表作成 */ create_cmdtype_code(); + /* 命令表の表示 */ print_cmdtype_code(); + /* ハッシュ表解放 */ free_cmdtype_code(); - if(cerrno != 0) { - printf("\terror - %d: %s\n", cerrno, cerrmsg); + if(cerr->num != 0) { + printf("\terror - %d: %s\n", cerr->num, cerr->msg); freecerr(); exit(-1); } diff --git a/test/unit/print_cmdtype_code_hash/print_cmdtype_code.c b/test/unit/print_cmdtype_code_hash/print_cmdtype_code.c index 71d56eb..d075ae4 100644 --- a/test/unit/print_cmdtype_code_hash/print_cmdtype_code.c +++ b/test/unit/print_cmdtype_code_hash/print_cmdtype_code.c @@ -9,18 +9,24 @@ void print_cmdtype_code() np = cmdtype_code[i]; while(np != NULL) { fprintf(stdout, "(%2d) - %s\t0%02o\t#%04X\n", - i, np->cca->cmd, np->cca->type, np->cca->code); + i, np->cmdtypecode->cmd, np->cmdtypecode->type, np->cmdtypecode->code); np = np->next; } } } int main(){ + /* エラーの初期化 */ + cerr = malloc_chk(sizeof(CERR), "cerr"); + /* ハッシュ表作成 */ create_cmdtype_code(); + /* ハッシュ表表示 */ print_cmdtype_code(); + /* ハッシュ表解放 */ free_cmdtype_code(); - if(cerrno != 0) { - printf("\terror - %d: %s\n", cerrno, cerrmsg); + /* エラーの表示 */ + if(cerr->num != 0) { + printf("\terror - %d: %s\n", cerr->num, cerr->msg); freecerr(); exit(-1); } diff --git a/test/unit/print_code_type/print_code_type.c b/test/unit/print_code_type/print_code_type.c index 179160b..a7ba676 100644 --- a/test/unit/print_code_type/print_code_type.c +++ b/test/unit/print_code_type/print_code_type.c @@ -3,7 +3,7 @@ int compare_code(const void *a, const void *b) { - return (**(const CMDCODEARRAY **)a).code - (**(const CMDCODEARRAY **)b).code; + return (**(const CMDTYPECODE **)a).code - (**(const CMDTYPECODE **)b).code; } /* 命令コードがキーのハッシュ表を表示する */ @@ -11,28 +11,34 @@ void print_code_type() { int i, j = 0; CMDCODETAB *np; - CMDCODEARRAY **ar; - ar = malloc(sizeof(*ar) * cmdcodesize); + CMDTYPECODE **ar; + ar = malloc(sizeof(*ar) * cmdtypecodesize); for(i = 0; i < cmdtabsize; i++) { np = code_type[i]; while(np != NULL) { - ar[j++] = np->cca; + ar[j++] = np->cmdtypecode; np = np->next; } } - qsort(ar, cmdcodesize, sizeof(*ar), (int (*)(const void*, const void*))compare_code); - for(i = 0; i < cmdcodesize; i++) { + qsort(ar, cmdtypecodesize, sizeof(*ar), (int (*)(const void*, const void*))compare_code); + for(i = 0; i < cmdtypecodesize; i++) { fprintf(stdout, "#%04X\t0%02o\t%s\n", ar[i]->code, ar[i]->type, ar[i]->cmd); } } int main() { + /* エラーの初期化 */ + cerr = malloc_chk(sizeof(CERR), "cerr"); + /* ハッシュ表作成 */ create_code_type(); + /* 命令表表示 */ print_code_type(); + /* ハッシュ表削除 */ free_code_type(); - if(cerrno != 0) { - printf("\terror - %d: %s\n", cerrno, cerrmsg); + /* エラーの表示 */ + if(cerr->num != 0) { + printf("\terror - %d: %s\n", cerr->num, cerr->msg); freecerr(); exit(-1); } diff --git a/test/unit/print_code_type_hash/print_code_type.c b/test/unit/print_code_type_hash/print_code_type.c index a74fcca..a8860c4 100644 --- a/test/unit/print_code_type_hash/print_code_type.c +++ b/test/unit/print_code_type_hash/print_code_type.c @@ -8,18 +8,25 @@ void print_code_type() CMDCODETAB *np; for(i = 0; i < cmdtabsize; i++){ for(np = code_type[i]; np != NULL; np = np->next) { - fprintf(stdout, "(%2d) - #%04X\t0%02o\t%s\n", i, np->cca->code, np->cca->type, np->cca->cmd); + fprintf(stdout, "(%2d) - #%04X\t0%02o\t%s\n", + i, np->cmdtypecode->code, np->cmdtypecode->type, np->cmdtypecode->cmd); } } } int main() { + /* エラーの初期化 */ + cerr = malloc_chk(sizeof(CERR), "cerr"); + /* ハッシュ表作成 */ create_code_type(); + /* ハッシュ表表示 */ print_code_type(); + /* ハッシュ表解放 */ free_code_type(); - if(cerrno != 0) { - printf("\terror - %d: %s\n", cerrno, cerrmsg); + /* エラーの表示 */ + if(cerr->num != 0) { + printf("\terror - %d: %s\n", cerr->num, cerr->msg); freecerr(); exit(-1); }