X-Git-Url: http://j8takagi.net/cgi-bin/gitweb.cgi?p=YACASL2.git;a=blobdiff_plain;f=src%2Fcmd.c;h=fb9ae6fafe8315caf1cdffb6ddbcf50beb35ef03;hp=add169acb46948e2fe47ba0460d059acc201ef79;hb=c8548a1d9afb3c175d824d5024d2288941829180;hpb=bf31ea99380e3ee2198f61f6510ebe6e4a3961f5 diff --git a/src/cmd.c b/src/cmd.c index add169a..fb9ae6f 100644 --- a/src/cmd.c +++ b/src/cmd.c @@ -1,6 +1,14 @@ -#include "casl2.h" - -CMD comet2cmd[] = { +#include +#include +#include +#include "hash.h" +#include "struct.h" +#include "cmem.h" + +/** + * 機械語命令のリスト + */ +static CMD comet2cmd[] = { { "NOP", NONE, 0x0 }, { "LD", R_ADR_X_, 0x1000 }, { "ST", R_ADR_X, 0x1100 }, @@ -41,20 +49,34 @@ CMD comet2cmd[] = { { "RET", NONE, 0x8100 }, }; -int comet2cmdsize = ARRAYSIZE(comet2cmd); -int cmdtabsize; -CMDTAB **cmdtype_code, **code_type; - -/* 命令と命令タイプからハッシュ値を生成する */ -unsigned hash_cmdtype(const char *cmd, CMDTYPE type) { +/** + * 命令コード配列のサイズ + */ +static int comet2cmdsize = ARRAYSIZE(comet2cmd); + +/** + * 命令表のサイズ + */ +static int cmdtabsize; + +/** + * ハッシュ表 + */ +static CMDTAB **cmdtype_code, **code_type; + +/** + * 命令の名前とタイプからハッシュ値を生成する + */ +unsigned hash_cmdtype(const char *cmd, CMDTYPE type) +{ HKEY *keys[2]; unsigned hashval; - /* 命令をセット */ + /* 命令名を設定 */ keys[0] = malloc_chk(sizeof(HKEY), "hash_cmdtype.keys[0]"); keys[0]->type = CHARS; keys[0]->val.s = strdup_chk(cmd, "keys[0].val.s"); - /* 命令タイプをセット */ + /* 命令タイプを設定 */ keys[1] = malloc_chk(sizeof(HKEY), "hash_cmdtype.keys[1]"); keys[1]->type = INT; keys[1]->val.i = (int)(type & 070); @@ -67,70 +89,69 @@ unsigned hash_cmdtype(const char *cmd, CMDTYPE type) { return hashval; } -/* 命令と命令タイプがキーのハッシュ表を作成する */ +/** + * 名前とタイプがキーの命令ハッシュ表を作成する + */ bool create_cmdtype_code() { - CMDTAB *np; + CMDTAB *p; unsigned hashval; int i; cmdtabsize = comet2cmdsize; cmdtype_code = calloc_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(comet2cmd[i].name, comet2cmd[i].type); - /* ハッシュ表に値を追加 */ - np->next = cmdtype_code[hashval]; - cmdtype_code[hashval] = np; - np->cmd = &(comet2cmd[i]); + p = malloc_chk(sizeof(CMDTAB), "cmdtype_code.p"); + hashval = hash_cmdtype(comet2cmd[i].name, comet2cmd[i].type); /* ハッシュ値の生成 */ + p->next = cmdtype_code[hashval]; /* ハッシュ表に値を追加 */ + p->cmd = &comet2cmd[i]; + cmdtype_code[hashval] = p; } return true; } -/* 命令と命令タイプから、命令コードを取得する */ -/* 無効な場合は0xFFFFを返す */ +/** + * 命令の名前とタイプから、命令コードを返す + * 無効な場合は0xFFFFを返す + */ WORD getcmdcode(const char *cmd, CMDTYPE type) { - CMDTAB *np; + CMDTAB *p; assert(cmd != NULL); - for(np = cmdtype_code[hash_cmdtype(cmd, type)]; np != NULL; np = np->next){ - if(strcmp(cmd, np->cmd->name) == 0 && type == np->cmd->type) { - return np->cmd->code; + for(p = cmdtype_code[hash_cmdtype(cmd, type)]; p != NULL; p = p->next){ + if(strcmp(cmd, p->cmd->name) == 0 && type == p->cmd->type) { + return p->cmd->code; } } return 0xFFFF; } -/* 命令と命令タイプがキーのハッシュ表を解放する */ +/** + * 名前とタイプがキーの命令ハッシュ表を解放する + */ void free_cmdtype_code() { int i; - CMDTAB *np, *nq; + CMDTAB *p, *q; for(i = 0; i < cmdtabsize; i++) { - np = cmdtype_code[i]; - while(np != NULL) { - nq = np->next; - free_chk(np, "free_cmdtype_code.np"); - np = nq; + for(p = cmdtype_code[i]; p != NULL; p = q) { + q = p->next; + free_chk(p, "free_cmdtype_code.p"); } } free_chk(cmdtype_code, "cmdtype_code"); } -/* 命令コードからハッシュ値を生成する */ +/** + * 命令コードからハッシュ値を生成する + */ unsigned hash_code(WORD code) { HKEY *keys[1]; - /* 命令コードをセット */ + /* 命令コードを設定 */ keys[0] = malloc_chk(sizeof(HKEY), "hash_code.key"); keys[0]->type = INT; keys[0]->val.i = (int)(code >> 8); @@ -138,57 +159,56 @@ unsigned hash_code(WORD code) return hash(1, keys, cmdtabsize); } -/* 命令コードがキーのハッシュ表を作成する */ +/** + * コードがキーの命令ハッシュ表を作成する + */ bool create_code_type() { - CMDTAB *np; + CMDTAB *p; unsigned hashval; int i; cmdtabsize = comet2cmdsize; code_type = calloc_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((&comet2cmd[i])->code); - /* ハッシュ表に値を追加 */ - np->next = code_type[hashval]; - code_type[hashval] = np; - np->cmd = &comet2cmd[i]; + p = malloc_chk(sizeof(CMDTAB), "code_type.p"); + hashval = hash_code((&comet2cmd[i])->code); /* ハッシュ値の生成 */ + p->next = code_type[hashval]; /* ハッシュ表に値を追加 */ + p->cmd = &comet2cmd[i]; + code_type[hashval] = p; } return true; } -/* 命令コードから命令タイプを取得する */ -/* 無効な場合はNONEを返す */ +/** + * 命令コードから命令タイプを返す + * 無効な場合はNOTCMDを返す + */ CMDTYPE getcmdtype(WORD code) { - CMDTAB *np; - for(np = code_type[hash_code(code)]; np != NULL; np = np->next) { - if(code == np->cmd->code) { - return np->cmd->type; + CMDTAB *p; + for(p = code_type[hash_code(code)]; p != NULL; p = p->next) { + if(code == p->cmd->code) { + return p->cmd->type; } } - return NONE; + return NOTCMD; } -/* 命令コードがキーのハッシュ表を解放する */ +/** + * コードがキーの命令ハッシュ表を解放する + */ void free_code_type() { int i; - CMDTAB *np, *nq; + CMDTAB *p, *q; for(i = 0; i < cmdtabsize; i++) { - np = code_type[i]; - while(np != NULL) { - nq = np->next; - free(np); - np = nq; + p = code_type[i]; + while(p != NULL) { + q = p->next; + free_chk(p, "code_type.p"); + p = q; } } - free(code_type); + free_chk(code_type, "code_type"); }