ドキュメントの修正
[YACASL2.git] / src / label.c
index 04a757d..71285a0 100644 (file)
@@ -8,36 +8,52 @@
 #include "hash.h"
 #include "assemble.h"
 
-static int labelcnt = 0;                /* ラベル数 */
-static LABELTAB *labels[LABELTABSIZE];  /* ラベル表 */
+/**
+ * プログラム名とラベルに対応するハッシュ値を返す
+ *
+ * @return ハッシュ値
+ *
+ * @param prog プログラム名
+ * @param label ラベル
+ */
+unsigned labelhash(const char *prog, const char *label);
 
 /**
- * ラベルのエラー定義
+ * ラベルを比較した結果を返す。qsort内で使われる関数
+ *
+ * @return ラベルが同一の場合は0、異なる場合は0以外
+ *
+ * @param *a ラベルa
+ * @param *b ラベルb
  */
-static CERR cerr_label[] = {
-    { 101, "label already defined" },
-    { 102, "label table is full" },
-    { 103, "label not found" },
-};
+int compare_adr(const void *a, const void *b);
 
 /**
- * ラベルのエラーをエラーリストに追加
+ * @brief ラベル数
  */
-void addcerrlist_label()
-{
-    addcerrlist(ARRAYSIZE(cerr_label), cerr_label);
-}
+static int labelcnt = 0;
 
 /**
- * プログラム名とラベルに対応するハッシュ値を返す
+ * @brief ラベル表
  */
+static LABELTAB *labels[LABELTABSIZE];
+
+/**
+ * @brief ラベルのエラー
+ */
+static CERR cerr_label[] = {
+    { 101, "label already defined" },
+    { 102, "label table is full" },
+    { 103, "label not found" },
+};
+
 unsigned labelhash(const char *prog, const char *label)
 {
     HKEY *keys[2];
     int i = 0, j;
     unsigned h;
 
-    if(prog != NULL) {
+    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");
@@ -54,16 +70,24 @@ unsigned labelhash(const char *prog, const char *label)
     return h;
 }
 
-/**
- * プログラム名とラベルに対応するアドレスをラベル表から検索する
- */
+int compare_adr(const void *a, const void *b)
+{
+    return (**(LABELARRAY **)a).adr - (**(LABELARRAY **)b).adr;
+}
+
+/* assemble.hで定義された関数群 */
+void addcerrlist_label()
+{
+    addcerrlist(ARRAYSIZE(cerr_label), cerr_label);
+}
+
 WORD getlabel(const char *prog, const char *label)
 {
-    assert(label != NULL);
+    assert(prog != NULL && label != NULL);
     LABELTAB *p;
 
     for(p = labels[labelhash(prog, label)]; p != NULL; p = p->next) {
-        if((prog == NULL || (p->prog != NULL && strcmp(prog, p->prog) == 0)) &&
+        if((*prog == '\0' || (strcmp(prog, p->prog) == 0)) &&
            strcmp(label, p->label) == 0)
         {
             return p->adr;
@@ -72,9 +96,6 @@ WORD getlabel(const char *prog, const char *label)
     return 0xFFFF;
 }
 
-/**
- * プログラム名、ラベル、アドレスをラベル表に追加する
- */
 bool addlabel(const char *prog, const char *label, WORD adr)
 {
     assert(label != NULL);
@@ -89,11 +110,7 @@ bool addlabel(const char *prog, const char *label, WORD adr)
     /* メモリを確保 */
     p = malloc_chk(sizeof(LABELTAB), "labels.next");
     /* プログラム名を設定 */
-    if(prog == NULL) {
-        p->prog = NULL;
-    } else {
-        p->prog = strdup_chk(prog, "labels.prog");
-    }
+    p->prog = strdup_chk(prog, "labels.prog");
     /* ラベルを設定 */
     p->label = strdup_chk(label, "labels.label");
     /* アドレスを設定 */
@@ -107,48 +124,35 @@ bool addlabel(const char *prog, const char *label, WORD adr)
     return true;
 }
 
-/**
- * ラベルを比較した結果を返す
- */
-int compare_adr(const void *a, const void *b)
-{
-    return (**(LABELARRAY **)a).adr - (**(LABELARRAY **)b).adr;
-}
-
-/**
- * ラベル表を表示する
- */
 void printlabel()
 {
     int i, s = 0;
     LABELTAB *p;
-    LABELARRAY *l[labelcnt];
+    LABELARRAY **l;
 
+    l = calloc_chk(labelcnt, sizeof(LABELARRAY **), "labels");
     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), "l[]");
-            if(p->prog == NULL) {
-                l[s]->prog = NULL;
-            } else {
-                l[s]->prog = strdup_chk(p->prog, "l[].prog");
-            }
-            l[s]->label = strdup_chk(p->label, "l[].label");
+            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;
         }
     }
     qsort(l, s, sizeof(*l), compare_adr);
     for(i = 0; i < s; i++) {
-        if(l[i]->prog != NULL) {
+        if(*(l[i]->prog) != '\0') {
             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);
 }
 
-/**
- * ラベル表を解放する
- */
 void freelabel()
 {
     int i;