X-Git-Url: http://j8takagi.net/cgi-bin/gitweb.cgi?p=YACASL2.git;a=blobdiff_plain;f=src%2Flabel.c;h=385922ddc26167fe8c8bcd85d10f677d046929d5;hp=2c4214e087d6bd86ccf97efd355f1516a9dbf8b5;hb=86e559d164166966a797a1e5855871d48e087ddd;hpb=d1f82970bf7d41db2fea11b08cd8e308f6cb8138 diff --git a/src/label.c b/src/label.c index 2c4214e..385922d 100644 --- a/src/label.c +++ b/src/label.c @@ -1,68 +1,114 @@ -#include -#include -#include -#include - -#include "cerr.h" -#include "cmem.h" -#include "hash.h" #include "assemble.h" -static int labelcnt = 0; /* ラベル数 */ -static LABELTAB *labels[LABELTABSIZE]; /* ラベル表 */ +/** + * ラベルのハッシュ値をセットしたキーを返す + * + * @return ハッシュ値をセットしたキー + * + * @param value 値 + */ +HKEY *label_hashkey(const char *value); + +/** + * プログラム名とラベルに対応するハッシュ値を返す + * + * @return ハッシュ値 + * + * @param prog プログラム名 + * @param label ラベル + */ +unsigned labelhash(const char *prog, const char *label); -#ifndef UNITTEST -static unsigned labelhash(const char *prog, const char *label); +/** + * ラベルを比較した結果を返す。qsort内で使われる関数 + * + * @return ラベルが同一の場合は0、異なる場合は0以外 + * + * @param *a ラベルa + * @param *b ラベルb + */ +int compare_adr(const void *a, const void *b); -static int compare_adr(const void *a, const void *b); -#endif +/** + * @brief ラベル数 + */ +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" }, +}; + +HKEY *label_hashkey(const char *value) { + HKEY *key = NULL; + + 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]; + HKEY *keys[2] = {NULL}; int i = 0; + unsigned h = 0; - if(prog != NULL) { - keys[i] = malloc_chk(sizeof(HKEY), "labelhash.key[]"); - keys[i]->type = CHARS; - keys[i]->val.s = strdup_chk(prog, "labelhash.key[].val"); + 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"); - /* ハッシュ値を返す */ - return hash(i+1, keys, LABELTABSIZE); + keys[i] = label_hashkey(label); + h = hash(i+1, keys, LABELTABSIZE); + for(int j = 0; j < i + 1; j++) { + FREE(keys[j]->val.s); + FREE(keys[j]); + } + 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); - LABELTAB *np; + assert(prog != NULL && label != NULL); + LABELTAB *p = NULL; + LABELARRAY *l = NULL; - for(np = labels[labelhash(prog, label)]; np != NULL; np = np->next) { - if((prog == NULL || (np->prog != NULL && strcmp(prog, np->prog) == 0)) && - strcmp(label, np->label) == 0) + for(p = labels[labelhash(prog, label)]; p != NULL; p = p->next) { + l = p->label; + if((!prog[0] || (strcmp(prog, l->prog) == 0)) && + strcmp(label, l->label) == 0) { - return np->adr; + return l->adr; } } return 0xFFFF; } -/** - * プログラム名、ラベル、アドレスをラベル表に追加する - */ bool addlabel(const char *prog, const char *label, WORD adr) { assert(label != NULL); - LABELTAB *np; - unsigned hashval; + LABELTAB *p = NULL; + LABELARRAY *l = NULL; + unsigned h = 0; /* 登録されたラベルを検索。すでに登録されている場合はエラー発生 */ if(getlabel(prog, label) != 0xFFFF) { @@ -70,81 +116,59 @@ bool addlabel(const char *prog, const char *label, WORD adr) return false; } /* メモリを確保 */ - np = malloc_chk(sizeof(LABELTAB), "labels.next"); + p = malloc_chk(sizeof(LABELTAB), "labels.next"); + l = p->label = malloc_chk(sizeof(LABELARRAY), "labels.label"); /* プログラム名を設定 */ - if(prog == NULL) { - np->prog = NULL; - } else { - np->prog = strdup_chk(prog, "labels.prog"); - } + l->prog = strdup_chk(prog, "label.prog"); /* ラベルを設定 */ - np->label = strdup_chk(label, "labels.label"); + l->label = strdup_chk(label, "label.label"); /* アドレスを設定 */ - np->adr = adr; + l->adr = adr; /* ラベル数を設定 */ labelcnt++; /* ハッシュ表へ追加 */ - hashval = labelhash(prog, label); - np->next = labels[hashval]; - labels[hashval] = np; + p->next = labels[h = labelhash(prog, label)]; + labels[h] = p; return true; } -/** - * ラベルを比較した結果を返す - */ -int compare_adr(const void *a, const void *b) -{ - return (**(LABELARRAY **)a).adr - (**(LABELARRAY **)b).adr; -} - -/** - * ラベル表を表示する - */ void printlabel() { - int i, asize = 0; - LABELTAB *np; - LABELARRAY *ar[labelcnt]; + int s = 0; + LABELTAB *p = NULL; + LABELARRAY **l = {NULL}; - for(i = 0; i < LABELTABSIZE; i++) { - for(np = labels[i]; np != NULL; np = np->next) { - assert(np->label != NULL); - ar[asize] = malloc_chk(sizeof(LABELARRAY), "ar[]"); - if(np->prog == NULL) { - ar[asize]->prog = NULL; - } else { - ar[asize]->prog = strdup_chk(np->prog, "ar[].prog"); - } - ar[asize]->label = strdup_chk(np->label, "ar[].label"); - ar[asize++]->adr = np->adr; + l = calloc_chk(labelcnt, sizeof(LABELARRAY **), "labels"); + for(int i = 0; i < LABELTABSIZE; i++) { + for(p = labels[i]; p != NULL; p = p->next) { + assert(p->label != NULL); + l[s++] = p->label; } } - qsort(ar, asize, sizeof(*ar), compare_adr); - for(i = 0; i < asize; i++) { - if(ar[i]->prog != NULL) { - fprintf(stdout, "%s.", ar[i]->prog); + qsort(l, s, sizeof(*l), compare_adr); + for(int i = 0; i < s; i++) { + if(l[i]->prog[0]) { + fprintf(stdout, "%s.", l[i]->prog); } - fprintf(stdout, "%s ---> #%04X\n", ar[i]->label, ar[i]->adr); + fprintf(stdout, "%s ---> #%04X\n", l[i]->label, l[i]->adr); } + FREE(l); } -/** - * ラベル表を解放する - */ void freelabel() { int i; - LABELTAB *np, *nq; + LABELTAB *p = NULL; + LABELTAB *q = NULL; for(i = 0; i < LABELTABSIZE; i++) { - for(np = labels[i]; np != NULL; np = nq) { - nq = np->next; - if(np->prog != NULL) { - free_chk(np->prog, "np.prog"); - } - free_chk(np->label, "np.label"); - free_chk(np, "np"); + for(p = labels[i]; p != NULL; p = q) { + q = p->next; + FREE(p->label->prog); + FREE(p->label->label); + FREE(p->label); + FREE(p); } + labels[i] = NULL; } }