X-Git-Url: http://j8takagi.net/cgi-bin/gitweb.cgi?p=YACASL2.git;a=blobdiff_plain;f=src%2Fstruct.c;h=f6d4128fbfe91036d0c6d7fbacb6e9dd1000d142;hp=f0f705b8bbf601ff91591391b4cea8deb4d5cc1e;hb=refs%2Ftags%2Fv0.5p1;hpb=4d98d132e119aa98dd7abcb76b6a96ae4723f274 diff --git a/src/struct.c b/src/struct.c index f0f705b..f6d4128 100644 --- a/src/struct.c +++ b/src/struct.c @@ -1,9 +1,4 @@ -#include -#include -#include -#include "hash.h" #include "struct.h" -#include "cmem.h" #include "exec.h" /** @@ -19,7 +14,7 @@ EXECPTR *execptr; /** * システムCOMET IIの命令表 */ -static COMET2CMD comet2cmd[] = { +static const COMET2CMD comet2cmd[] = { { "NOP", NONE, 0x0, nop }, { "LD", R_ADR_X, 0x1000, ld_r_adr_x }, { "ST", R_ADR_X, 0x1100, st }, @@ -75,7 +70,17 @@ enum { /** * ハッシュ表 */ -static CMDTAB *cmdtype_code[CMDTABSIZE], *code_cmdtype[CMDTABSIZE]; +static CMDTAB *cmdtab[HASH_MAX][CMDTABSIZE]; + +/** + * 命令の名前とタイプからハッシュ値を生成する + */ +unsigned hash_cmdtype(const char *cmd, CMDTYPE type); + +/** + * 命令コードからハッシュ値を生成する + */ +unsigned hash_code(WORD code); /** * 命令の名前とタイプからハッシュ値を生成する @@ -103,24 +108,45 @@ unsigned hash_cmdtype(const char *cmd, CMDTYPE type) } /** - * 名前とタイプがキーの命令ハッシュ表を作成する + * 命令ハッシュ表を作成する */ -bool create_cmdtype_code() +bool create_cmdtable(CMDTAB_HASH hash) { CMDTAB *p; unsigned hashval; int i; for(i = 0; i < comet2cmdsize; i++) { - hashval = hash_cmdtype(comet2cmd[i].name, comet2cmd[i].type); /* ハッシュ値の生成 */ - p = malloc_chk(sizeof(CMDTAB), "cmdtype_code"); + p = malloc_chk(sizeof(CMDTAB), "create_cmdtable.p"); p->cmd = &comet2cmd[i]; - p->next = cmdtype_code[hashval]; /* ハッシュ表に値を追加 */ - cmdtype_code[hashval] = p; + if(hash == HASH_CMDTYPE) { + hashval = hash_cmdtype(comet2cmd[i].name, comet2cmd[i].type); + } else if(hash == HASH_CODE) { + hashval = hash_code((&comet2cmd[i])->code); + } + p->next = cmdtab[hash][hashval]; + cmdtab[hash][hashval] = p; } return true; } +/** + * 命令ハッシュ表を解放する + */ +void free_cmdtable(CMDTAB_HASH hash) +{ + int i; + CMDTAB *p, *q; + + for(i = 0; i < CMDTABSIZE; i++) { + for(p = cmdtab[hash][i]; p != NULL; p = q) { + q = p->next; + cmdtab[hash][i] = NULL; + FREE(p); + } + } +} + /** * 命令の名前とタイプから、命令コードを返す\n * 無効な場合は0xFFFFを返す @@ -131,7 +157,7 @@ WORD getcmdcode(const char *cmd, CMDTYPE type) WORD w = 0xFFFF; assert(cmd != NULL); - for(p = cmdtype_code[hash_cmdtype(cmd, type)]; p != NULL; p = p->next) { + for(p = cmdtab[HASH_CMDTYPE][hash_cmdtype(cmd, type)]; p != NULL; p = p->next) { if(strcmp(cmd, p->cmd->name) == 0 && type == p->cmd->type) { w = p->cmd->code; break; @@ -140,22 +166,6 @@ WORD getcmdcode(const char *cmd, CMDTYPE type) return w; } -/** - * 名前とタイプがキーの命令ハッシュ表を解放する - */ -void free_cmdtype_code() -{ - int i; - CMDTAB *p, *q; - - for(i = 0; i < CMDTABSIZE; i++) { - for(p = cmdtype_code[i]; p != NULL; p = q) { - q = p->next; - FREE(p); - } - } -} - /** * 命令コードからハッシュ値を生成する */ @@ -173,25 +183,6 @@ unsigned hash_code(WORD code) return h; } -/** - * コードがキーの命令ハッシュ表を作成する - */ -bool create_code_cmdtype() -{ - CMDTAB *p; - unsigned hashval; - int i; - - for(i = 0; i < comet2cmdsize; i++) { - hashval = hash_code((&comet2cmd[i])->code); /* ハッシュ値の生成 */ - p = malloc_chk(sizeof(CMDTAB), "code_cmdtype"); - p->cmd = &comet2cmd[i]; - p->next = code_cmdtype[hashval]; /* ハッシュ表に値を追加 */ - code_cmdtype[hashval] = p; - } - return true; -} - /** * 命令コードから命令の関数ポインタを返す */ @@ -200,7 +191,7 @@ const void (*getcmdptr(WORD code)) CMDTAB *t; const void *ptr = NULL; - for(t = code_cmdtype[hash_code(code)]; t != NULL; t = t->next) { + for(t = cmdtab[HASH_CODE][hash_code(code)]; t != NULL; t = t->next) { if(code == t->cmd->code) { ptr = t->cmd->ptr; break; @@ -215,9 +206,9 @@ const void (*getcmdptr(WORD code)) CMDTYPE getcmdtype(WORD code) { CMDTAB *t; - CMDTYPE type = 0; + CMDTYPE type = NONE; - for(t = code_cmdtype[hash_code(code)]; t != NULL; t = t->next) { + for(t = cmdtab[HASH_CODE][hash_code(code)]; t != NULL; t = t->next) { if(code == t->cmd->code) { type = t->cmd->type; break; @@ -234,7 +225,7 @@ char *getcmdname(WORD code) CMDTAB *t; char *cmd = NULL; - for(t = code_cmdtype[hash_code(code)]; t != NULL; t = t->next) { + for(t = cmdtab[HASH_CODE][hash_code(code)]; t != NULL; t = t->next) { if(code == t->cmd->code) { cmd = t->cmd->name; break; @@ -244,18 +235,15 @@ char *getcmdname(WORD code) } /** - * コードがキーの命令ハッシュ表を解放する + * 汎用レジスタの番号からレジスタを表す文字列を返す */ -void free_code_cmdtype() + +char *grstr(WORD word) { - int i; - CMDTAB *p, *q; - for(i = 0; i < CMDTABSIZE; i++) { - for(p = code_cmdtype[i]; p != NULL; p = q) { - q = p->next; - FREE(p); - } - } + assert(word <= 7); + char *str = malloc_chk(3 + 1, "grstr.str"); + sprintf(str, "GR%d", word); + return str; } /**