From: j8takagi Date: Sun, 28 Feb 2010 07:54:57 +0000 (+0900) Subject: 命令ハッシュ表周辺の調整 X-Git-Tag: v0.1~9 X-Git-Url: https://j8takagi.net/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=2b0ff8e75f11c5fb58e16e95d8195d6f1252c97c;p=YACASL2.git 命令ハッシュ表周辺の調整 実験の結果、命令ハッシュ表のサイズはcmdcodesize(=38) --- diff --git a/include/casl2.h b/include/casl2.h index 23607fd..9a2fc07 100644 --- a/include/casl2.h +++ b/include/casl2.h @@ -95,6 +95,7 @@ typedef struct _CMDCODETAB { extern CMDCODETAB **cmdtype_code; extern CMDCODETAB **code_type; +extern int cmdtabsize; /* 命令と命令タイプがキーのハッシュ表を作成する */ bool create_cmdtype_code(); diff --git a/include/hash.h b/include/hash.h index 9821b44..caac5f1 100644 --- a/include/hash.h +++ b/include/hash.h @@ -1,6 +1,10 @@ #ifndef YACASL2_HASH_INCLUDED #define YACASL2_HASH_INCLUDED +#ifndef ARRAYSIZE +#define ARRAYSIZE(array) (sizeof(array)/sizeof(array[0])) +#endif + /* ハッシュ共用体の型 */ typedef enum { CHARS = 0, @@ -16,6 +20,9 @@ typedef struct { } val; } HKEY; +/* ハッシュ表のサイズを決めるため、引数の数値未満で最大の素数を返す */ +int hashtabsize(int size); + /* ハッシュ値を取得する */ unsigned hash(int keyc, HKEY *keyv[], int tabsize); diff --git a/src/cmd.c b/src/cmd.c index 7c1e803..6bb749f 100644 --- a/src/cmd.c +++ b/src/cmd.c @@ -42,7 +42,7 @@ CMDCODEARRAY cmdcodearray[] = { }; int cmdcodesize = ARRAYSIZE(cmdcodearray); -int hashtabsize; +int cmdtabsize; CMDCODETAB **cmdtype_code, **code_type; /* 命令と命令タイプからハッシュ値を生成する */ @@ -58,7 +58,7 @@ unsigned hash_cmdtype(const char *cmd, CMDTYPE type) { keys[1]->type = INT; keys[1]->val.i = (int)(type & 070); /* ハッシュ値を返す */ - return hash(2, keys, cmdcodesize); + return hash(2, keys, cmdtabsize); } /* 命令と命令タイプがキーのハッシュ表を作成する */ @@ -68,8 +68,8 @@ bool create_cmdtype_code() unsigned hashval; int i; - hashtabsize = cmdcodesize; - cmdtype_code = malloc(cmdcodesize * sizeof(CMDCODETAB *)); + cmdtabsize = cmdcodesize; + cmdtype_code = malloc(cmdtabsize * sizeof(CMDCODETAB *)); for(i = 0; i < cmdcodesize; i++) { np = malloc(sizeof(CMDCODETAB)); if(np == NULL) { @@ -105,7 +105,7 @@ void free_cmdtype_code() { int i; CMDCODETAB *np, *nq; - for(i = 0; i < cmdcodesize; i++){ + for(i = 0; i < cmdtabsize; i++){ np = cmdtype_code[i]; while(np != NULL) { nq = np->next; @@ -125,7 +125,7 @@ unsigned hash_code(WORD code) keys[0]->type = INT; keys[0]->val.i = (int)(code >> 8); /* ハッシュ値を返す */ - return hash(1, keys, cmdcodesize); + return hash(1, keys, cmdtabsize); } /* 命令コードがキーのハッシュ表を作成する */ @@ -135,8 +135,8 @@ bool create_code_type() unsigned hashval; int i; - hashtabsize = cmdcodesize; - code_type = malloc(cmdcodesize * sizeof(CMDCODETAB *)); + 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 */ @@ -170,7 +170,7 @@ void free_code_type() { int i; CMDCODETAB *np, *nq; - for(i = 0; i < cmdcodesize; i++){ + for(i = 0; i < cmdtabsize; i++){ np = code_type[i]; while(np != NULL) { nq = np->next; diff --git a/src/exec.c b/src/exec.c index 3e4b1b9..17ac66b 100644 --- a/src/exec.c +++ b/src/exec.c @@ -264,7 +264,7 @@ void reset() } } -/* コードの実行 */ +/* 仮想マシンCOMET IIでの実行 */ void exec() { WORD op, r_r1, x_r2, val; @@ -272,7 +272,7 @@ void exec() char *errpr = malloc(CERRSTRSIZE + 1); clock_t clock_begin, clock_end; - if((&execmode)->trace) { + if(execmode.trace) { fprintf(stdout, "\nExecuting machine codes\n"); } /* フラグレジスタの初期値設定 */ @@ -300,7 +300,9 @@ void exec() sprintf(errpr, "PR:#%04X", PR); setcerr(205, errpr); /* Stack Pointer (SP) - cannot allocate stack buffer */ } + /* 命令の取り出し */ op = memory[PR] & 0xFF00; + /* 命令の解読 */ cmdtype = getcmdtype(op); r_r1 = (memory[PR] >> 4) & 0xF; x_r2 = memory[PR] & 0xF; @@ -308,19 +310,19 @@ void exec() if(cerrno > 0) { goto execerr; } - if((&execmode)->trace){ + if(execmode.trace){ fprintf(stdout, "#%04X: Register::::\n", PR); dspregister(); } - if((&execmode)->dump){ + if(execmode.dump){ fprintf(stdout, "#%04X: Memory::::\n", PR); dumpmemory(); } - if((&execmode)->dump || (&execmode)->trace) { + if(execmode.dump || execmode.trace) { fprintf(stdout, "\n"); } PR++; - /* 処理対象の値を取得 */ + /* オペランドの取り出し */ if(cmdtype == R1_R2) { assert(x_r2 < REGSIZE); val = GR[x_r2]; @@ -346,7 +348,7 @@ void exec() if(op >= 0x1000 && op <= 0x4FFF) { op &= 0xFB00; } - /* 命令ごとの処理を実行 */ + /* 命令の実行 */ switch(op) { case 0x0: /* NOP */ diff --git a/src/hash.c b/src/hash.c index 54f38ab..f63b3f5 100644 --- a/src/hash.c +++ b/src/hash.c @@ -1,5 +1,22 @@ #include "hash.h" +/* ハッシュ表のサイズを決めるため、引数の数値より大きい最小の素数を返す */ +int hashtabsize(int size) +{ + int i; + const int prime[] = + {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, + 31, 37, 41, 43, 47, 53, 59, 61, 67, + 71, 73, 79, 83, 89, 97, + }; + for(i = 0; i < ARRAYSIZE(prime); i++) { + if(i > 0 && prime[i] >= size) { + break; + } + } + return prime[i]; +} + /* ハッシュ値を取得する */ unsigned hash(int keyc, HKEY *keyv[], int tabsize) { diff --git a/test/unit/hash/0.txt b/test/unit/hash/0.txt index f21e3f4..bc8892a 100644 --- a/test/unit/hash/0.txt +++ b/test/unit/hash/0.txt @@ -1,3 +1,3 @@ HASH VALUE: 20 HASH VALUE: 11 -HASH VALUE: 1 +HASH VALUE: 2 diff --git a/test/unit/print_cmdtype_code/print_cmdtype_code.c b/test/unit/print_cmdtype_code/print_cmdtype_code.c index a2dcf86..bb31614 100644 --- a/test/unit/print_cmdtype_code/print_cmdtype_code.c +++ b/test/unit/print_cmdtype_code/print_cmdtype_code.c @@ -21,7 +21,7 @@ void print_cmdtype_code() CMDCODETAB *np; CMDCODEARRAY **ar; ar = malloc(sizeof(*ar) * cmdcodesize); - for(i = 0; i < cmdcodesize; i++) { + for(i = 0; i < cmdtabsize; i++) { np = cmdtype_code[i]; while(np != NULL) { ar[j++] = np->cca; 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 b10db02..71d56eb 100644 --- a/test/unit/print_cmdtype_code_hash/print_cmdtype_code.c +++ b/test/unit/print_cmdtype_code_hash/print_cmdtype_code.c @@ -5,7 +5,7 @@ void print_cmdtype_code() { int i; CMDCODETAB *np; - for(i = 0; i < cmdcodesize; i++){ + for(i = 0; i < cmdtabsize; i++){ np = cmdtype_code[i]; while(np != NULL) { fprintf(stdout, "(%2d) - %s\t0%02o\t#%04X\n", diff --git a/test/unit/print_code_type/print_code_type.c b/test/unit/print_code_type/print_code_type.c index 51abfd3..179160b 100644 --- a/test/unit/print_code_type/print_code_type.c +++ b/test/unit/print_code_type/print_code_type.c @@ -13,7 +13,7 @@ void print_code_type() CMDCODETAB *np; CMDCODEARRAY **ar; ar = malloc(sizeof(*ar) * cmdcodesize); - for(i = 0; i < cmdcodesize; i++) { + for(i = 0; i < cmdtabsize; i++) { np = code_type[i]; while(np != NULL) { ar[j++] = np->cca; 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 c081888..a74fcca 100644 --- a/test/unit/print_code_type_hash/print_code_type.c +++ b/test/unit/print_code_type_hash/print_code_type.c @@ -6,7 +6,7 @@ void print_code_type() { int i; CMDCODETAB *np; - for(i = 0; i < cmdcodesize; i++){ + 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); }