ソースの推敲
[YACASL2.git] / src / label.c
index 71285a0..f69bd45 100644 (file)
@@ -1,13 +1,14 @@
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-#include <assert.h>
-
-#include "cerr.h"
-#include "cmem.h"
-#include "hash.h"
 #include "assemble.h"
 
+/**
+ * ラベルのハッシュ値をセットしたキーを返す
+ *
+ * @return ハッシュ値をセットしたキー
+ *
+ * @param value 値
+ */
+HKEY *label_hashkey(const char *value);
+
 /**
  * プログラム名とラベルに対応するハッシュ値を返す
  *
@@ -47,21 +48,25 @@ static CERR cerr_label[] = {
     { 103, "label not found" },
 };
 
+HKEY *label_hashkey(const char *value) {
+    HKEY *key;
+
+    key = malloc_chk(sizeof(HKEY), "label_hashkey");
+    key->type = CHARS;
+    key->val.s = strdup_chk(value, "label_hashkey.value");
+    return key;
+}
+
 unsigned labelhash(const char *prog, const char *label)
 {
     HKEY *keys[2];
     int i = 0, j;
     unsigned h;
 
-    if(*prog != '\0') {
-        keys[i] = malloc_chk(sizeof(HKEY), "labelhash.key");
-        keys[i]->type = CHARS;
-        keys[i]->val.s = strdup_chk(prog, "labelhash.key.val");
-        i++;
+    if(prog[0]) {
+        keys[i++] = label_hashkey(prog);
     }
-    keys[i] = malloc_chk(sizeof(HKEY), "labelhash.key");
-    keys[i]->type = CHARS;
-    keys[i]->val.s = strdup_chk(label, "labelhash.key.val");
+    keys[i] = label_hashkey(label);
     h = hash(i+1, keys, LABELTABSIZE);
     for(j = 0; j < i + 1; j++) {
         FREE(keys[j]->val.s);
@@ -85,12 +90,14 @@ WORD getlabel(const char *prog, const char *label)
 {
     assert(prog != NULL && label != NULL);
     LABELTAB *p;
+    LABELARRAY *l;
 
     for(p = labels[labelhash(prog, label)]; p != NULL; p = p->next) {
-        if((*prog == '\0' || (strcmp(prog, p->prog) == 0)) &&
-           strcmp(label, p->label) == 0)
+        l = p->label;
+        if((!prog[0] || (strcmp(prog, l->prog) == 0)) &&
+           strcmp(label, l->label) == 0)
         {
-            return p->adr;
+            return l->adr;
         }
     }
     return 0xFFFF;
@@ -100,7 +107,8 @@ bool addlabel(const char *prog, const char *label, WORD adr)
 {
     assert(label != NULL);
     LABELTAB *p;
-    unsigned hashval;
+    LABELARRAY *l;
+    unsigned h;
 
     /* 登録されたラベルを検索。すでに登録されている場合はエラー発生 */
     if(getlabel(prog, label) != 0xFFFF) {
@@ -109,18 +117,18 @@ bool addlabel(const char *prog, const char *label, WORD adr)
     }
     /* メモリを確保 */
     p = malloc_chk(sizeof(LABELTAB), "labels.next");
+    l = p->label = malloc_chk(sizeof(LABELARRAY), "labels.label");
     /* プログラム名を設定 */
-    p->prog = strdup_chk(prog, "labels.prog");
+    l->prog = strdup_chk(prog, "label.prog");
     /* ラベルを設定 */
-    p->label = strdup_chk(label, "labels.label");
+    l->label = strdup_chk(label, "label.label");
     /* アドレスを設定 */
-    p->adr = adr;
+    l->adr = adr;
     /* ラベル数を設定 */
     labelcnt++;
     /* ハッシュ表へ追加 */
-    hashval = labelhash(prog, label);
-    p->next = labels[hashval];
-    labels[hashval] = p;
+    p->next = labels[h = labelhash(prog, label)];
+    labels[h] = p;
     return true;
 }
 
@@ -134,21 +142,15 @@ void printlabel()
     for(i = 0; i < LABELTABSIZE; i++) {
         for(p = labels[i]; p != NULL; p = p->next) {
             assert(p->label != NULL);
-            l[s] = malloc_chk(sizeof(LABELARRAY), "lables");
-            l[s]->prog = strdup_chk(p->prog, "labels.prog");
-            l[s]->label = strdup_chk(p->label, "labels.label");
-            l[s++]->adr = p->adr;
+            l[s++] = p->label;
         }
     }
     qsort(l, s, sizeof(*l), compare_adr);
     for(i = 0; i < s; i++) {
-        if(*(l[i]->prog) != '\0') {
+        if(*(l[i]->prog)) {
             fprintf(stdout, "%s.", l[i]->prog);
         }
         fprintf(stdout, "%s ---> #%04X\n", l[i]->label, l[i]->adr);
-        FREE(l[i]->prog);
-        FREE(l[i]->label);
-        FREE(l[i]);
     }
     FREE(l);
 }
@@ -161,9 +163,11 @@ void freelabel()
     for(i = 0; i < LABELTABSIZE; i++) {
         for(p = labels[i]; p != NULL; p = q) {
             q = p->next;
-            FREE(p->prog);
+            FREE(p->label->prog);
+            FREE(p->label->label);
             FREE(p->label);
             FREE(p);
         }
+        labels[i] = NULL;
     }
 }