/**
* 機械語命令のリスト
*/
-CMD comet2cmd[] = {
+static CMD comet2cmd[] = {
{ "NOP", NONE, 0x0 },
{ "LD", R_ADR_X_, 0x1000 },
{ "ST", R_ADR_X, 0x1100 },
static int comet2cmdsize = ARRAYSIZE(comet2cmd);
/**
- * 命令表のサイズ
+ * ハッシュ表のサイズ
*/
static int cmdtabsize;
*/
static CMDTAB **cmdtype_code, **code_type;
-#ifndef UNITTEST
-static unsigned hash_cmdtype(const char *cmd, CMDTYPE type);
-
-static unsigned hash_code(WORD code);
-#endif
-
/**
* 命令の名前とタイプからハッシュ値を生成する
*/
keys[1]->val.i = (int)(type & 070);
/* ハッシュ値の計算 */
hashval = hash(2, keys, cmdtabsize);
- free_chk(keys[0]->val.s, "keys[0].val.s");
- free_chk(keys[0], "keys[0]");
- free_chk(keys[1], "keys[1]");
+ FREE(keys[0]->val.s);
+ FREE(keys[0]);
+ FREE(keys[1]);
/* ハッシュ値を返す */
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;
- }
+ cmdtabsize = comet2cmdsize; /* ハッシュ表のサイズ */
+ cmdtype_code = calloc_chk(cmdtabsize, sizeof(CMDTAB **), "cmdtype_code");
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]);
+ hashval = hash_cmdtype(comet2cmd[i].name, comet2cmd[i].type); /* ハッシュ値の生成 */
+ p = malloc_chk(sizeof(CMDTAB), "cmdtype_code");
+ p->cmd = &comet2cmd[i];
+ p->next = cmdtype_code[hashval]; /* ハッシュ表に値を追加 */
+ cmdtype_code[hashval] = p;
}
return true;
}
*/
WORD getcmdcode(const char *cmd, CMDTYPE type)
{
- CMDTAB *np;
+ CMDTAB *p;
+ WORD w = 0xFFFF;
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) {
+ w = p->cmd->code;
+ break;
}
}
- return 0xFFFF;
+ return w;
}
/**
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(p);
}
}
- free_chk(cmdtype_code, "cmdtype_code");
}
/**
unsigned hash_code(WORD code)
{
HKEY *keys[1];
+ unsigned h;
/* 命令コードを設定 */
keys[0] = malloc_chk(sizeof(HKEY), "hash_code.key");
keys[0]->type = INT;
keys[0]->val.i = (int)(code >> 8);
- /* ハッシュ値を返す */
- return hash(1, keys, cmdtabsize);
+ h = hash(1, keys, cmdtabsize);
+ FREE(keys[0]);
+ return h;
}
/**
*/
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;
- }
+ cmdtabsize = comet2cmdsize; /* ハッシュ表のサイズ */
+ code_type = calloc_chk(comet2cmdsize, sizeof(CMDTAB **), "code_type");
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];
+ hashval = hash_code((&comet2cmd[i])->code); /* ハッシュ値の生成 */
+ p = malloc_chk(sizeof(CMDTAB), "code_type");
+ p->cmd = &comet2cmd[i];
+ p->next = code_type[hashval]; /* ハッシュ表に値を追加 */
+ 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;
+ CMDTYPE t = NOTCMD;
+
+ for(p = code_type[hash_code(code)]; p != NULL; p = p->next) {
+ if(code == p->cmd->code) {
+ t = p->cmd->type;
+ break;
}
}
- return NONE;
+ return t;
}
/**
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_chk(np, "np");
- np = nq;
+ for(p = code_type[i]; p != NULL; p = q) {
+ q = p->next;
+ FREE(p);
}
}
- free_chk(code_type, "code_type");
}