命令ハッシュ表の作成方法を変更
[YACASL2.git] / src / struct.c
index 4e38f2e..3fb9a30 100644 (file)
@@ -72,6 +72,16 @@ enum {
  */
 static CMDTAB *cmdtype_code[CMDTABSIZE], *code_cmdtype[CMDTABSIZE];
 
+/**
+ * 命令の名前とタイプからハッシュ値を生成する
+ */
+unsigned hash_cmdtype(const char *cmd, CMDTYPE type);
+
+/**
+ * 命令コードからハッシュ値を生成する
+ */
+unsigned hash_code(WORD code);
+
 /**
  * 命令の名前とタイプからハッシュ値を生成する
  */
@@ -98,20 +108,26 @@ unsigned hash_cmdtype(const char *cmd, CMDTYPE type)
 }
 
 /**
- * å\90\8då\89\8dã\81¨ã\82¿ã\82¤ã\83\97ã\81\8cã\82­ã\83¼ã\81®å\91½ä»¤ã\83\8fã\83\83ã\82·ã\83¥è¡¨ã\82\92ä½\9cæ\88\90ã\81\99ã\82\8b
+ * 命令ハッシュ表を作成する
  */
-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);
+            p->next = cmdtype_code[hashval];
+            cmdtype_code[hashval] = p;
+        } else if(hash == HASH_CODE) {
+            hashval = hash_code((&comet2cmd[i])->code);
+            p->next = code_cmdtype[hashval];
+            code_cmdtype[hashval] = p;
+        }
     }
     return true;
 }
@@ -136,16 +152,26 @@ WORD getcmdcode(const char *cmd, CMDTYPE type)
 }
 
 /**
- * å\90\8då\89\8dã\81¨ã\82¿ã\82¤ã\83\97ã\81\8cã\82­ã\83¼ã\81®å\91½ä»¤ã\83\8fã\83\83ã\82·ã\83¥è¡¨ã\82\92解æ\94¾ã\81\99ã\82\8b
+ * 命令ハッシュ表を解放する
  */
-void free_cmdtype_code()
+void free_cmdtable(CMDTAB_HASH hash)
 {
     int i;
     CMDTAB *p, *q;
 
     for(i = 0; i < CMDTABSIZE; i++) {
-        for(p = cmdtype_code[i]; p != NULL; p = q) {
+        if(hash == HASH_CMDTYPE) {
+            p = cmdtype_code[i];
+        } else if(hash == HASH_CODE) {
+            p = code_cmdtype[i];
+        }
+        for( ; p != NULL; p = q) {
             q = p->next;
+            if(p == cmdtype_code[i]) {
+                cmdtype_code[i] = NULL;
+            } else if (p == code_cmdtype[i]) {
+                code_cmdtype[i] = NULL;
+            }
             FREE(p);
         }
     }
@@ -168,25 +194,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;
-}
-
 /**
  * 命令コードから命令の関数ポインタを返す
  */
@@ -250,21 +257,6 @@ char *grstr(WORD word)
     return str;
 }
 
-/**
- * コードがキーの命令ハッシュ表を解放する
- */
-void free_code_cmdtype()
-{
-    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);
-        }
-    }
-}
-
 /**
  * COMET II仮想マシンのリセット
  */