ソースの書き方を変更
[YACASL2.git] / src / cmd.c
index 258c491..1b01d63 100644 (file)
--- a/src/cmd.c
+++ b/src/cmd.c
@@ -8,7 +8,7 @@
 /**
  * 機械語命令のリスト
  */
-CMD comet2cmd[] = {
+static CMD comet2cmd[] = {
     { "NOP", NONE, 0x0 },
     { "LD", R_ADR_X_, 0x1000 },
     { "ST", R_ADR_X, 0x1100 },
@@ -55,7 +55,7 @@ CMD comet2cmd[] = {
 static int comet2cmdsize = ARRAYSIZE(comet2cmd);
 
 /**
- * 命令表のサイズ
+ * ハッシュ表のサイズ
  */
 static int cmdtabsize;
 
@@ -64,12 +64,6 @@ 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
-
 /**
  * 命令の名前とタイプからハッシュ値を生成する
  */
@@ -100,25 +94,18 @@ unsigned hash_cmdtype(const char *cmd, CMDTYPE type)
  */
 bool create_cmdtype_code()
 {
-    CMDTAB *np;
+    CMDTAB *p;
     unsigned hashval;
     int i;
 
-    cmdtabsize = comet2cmdsize;
+    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), "create_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;
 }
@@ -129,15 +116,17 @@ bool create_cmdtype_code()
  */
 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;
 }
 
 /**
@@ -146,17 +135,14 @@ WORD getcmdcode(const char *cmd, CMDTYPE type)
 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");
         }
     }
-    free_chk(cmdtype_code, "cmdtype_code");
 }
 
 /**
@@ -179,42 +165,38 @@ unsigned hash_code(WORD code)
  */
 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];
+        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;
+    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;
 }
 
 /**
@@ -223,14 +205,11 @@ CMDTYPE getcmdtype(WORD code)
 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_chk(p, "code_type");
         }
     }
-    free_chk(code_type, "code_type");
 }