X-Git-Url: http://j8takagi.net/cgi-bin/gitweb.cgi?p=YACASL2.git;a=blobdiff_plain;f=src%2Flabel.c;h=69a37dcfd293c8cca34704c6b79c64e67d951345;hp=1b9b806d4e4d2ee96eb1d8f8291917e5c8c7d8ed;hb=a9c0bfff368938e5f162835115daff1de1787a01;hpb=555c5e8b851becc08ba661a9cb19f617d5a00c12 diff --git a/src/label.c b/src/label.c index 1b9b806..69a37dc 100644 --- a/src/label.c +++ b/src/label.c @@ -1,48 +1,84 @@ -#include "casl2.h" +#include +#include +#include +#include + +#include "cerr.h" +#include "cmem.h" +#include "hash.h" #include "assemble.h" -int labelcnt = 0; /* ラベル数 */ -LABELTAB *labels[LABELTABSIZE]; /* ラベル表 */ +static int labelcnt = 0; /* ラベル数 */ +static LABELTAB *labels[LABELTABSIZE]; /* ラベル表 */ + +/** + * ラベルのエラー定義 + */ +static CERR cerr_label[] = { + { 101, "label already defined" }, + { 102, "label table is full" }, + { 103, "label not found" }, +}; -/* プログラム名とラベルに対応するハッシュ値を返す */ +/** + * ラベルのエラーをエラーリストに追加 + */ +void addcerrlist_label() +{ + addcerrlist(ARRAYSIZE(cerr_label), cerr_label); +} + +/** + * プログラム名とラベルに対応するハッシュ値を返す + */ unsigned labelhash(const char *prog, const char *label) { HKEY *keys[2]; - int i = 0; + int i = 0, j; + unsigned h; if(prog != NULL) { - keys[i] = malloc_chk(sizeof(HKEY), "labelhash.key[]"); + keys[i] = malloc_chk(sizeof(HKEY), "labelhash.key"); keys[i]->type = CHARS; - keys[i]->val.s = strdup_chk(prog, "labelhash.key[].val"); + keys[i]->val.s = strdup_chk(prog, "labelhash.key.val"); + i++; } - keys[i] = malloc_chk(sizeof(HKEY), "labelhash.key[]"); + 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]->val.s = strdup_chk(label, "labelhash.key.val"); + h = hash(i+1, keys, LABELTABSIZE); + for(j = 0; j < i + 1; j++) { + FREE(keys[j]->val.s); + FREE(keys[j]); + } + return h; } -/* ラベル表からアドレスを検索する */ +/** + * プログラム名とラベルに対応するアドレスをラベル表から検索する + */ WORD getlabel(const char *prog, const char *label) { assert(label != NULL); - LABELTAB *np; + LABELTAB *p; - 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) { + if((prog == NULL || (p->prog != NULL && strcmp(prog, p->prog) == 0)) && + strcmp(label, p->label) == 0) { - return np->adr; + return p->adr; } } return 0xFFFF; } -/* プログラム名、ラベル、アドレスをラベル表に追加する */ +/** + * プログラム名、ラベル、アドレスをラベル表に追加する + */ bool addlabel(const char *prog, const char *label, WORD adr) { assert(label != NULL); - LABELTAB *np; + LABELTAB *p; unsigned hashval; /* 登録されたラベルを検索。すでに登録されている場合はエラー発生 */ @@ -51,74 +87,80 @@ 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"); /* プログラム名を設定 */ if(prog == NULL) { - np->prog = NULL; + p->prog = NULL; } else { - np->prog = strdup_chk(prog, "labels.prog"); + p->prog = strdup_chk(prog, "labels.prog"); } /* ラベルを設定 */ - np->label = strdup_chk(label, "labels.label"); + p->label = strdup_chk(label, "labels.label"); /* アドレスを設定 */ - np->adr = adr; + p->adr = adr; /* ラベル数を設定 */ labelcnt++; /* ハッシュ表へ追加 */ hashval = labelhash(prog, label); - np->next = labels[hashval]; - labels[hashval] = np; + p->next = labels[hashval]; + labels[hashval] = 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 i, s = 0; + LABELTAB *p; + LABELARRAY **l; + l = calloc_chk(labelcnt, sizeof(LABELARRAY **), "labels"); 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; + for(p = labels[i]; p != NULL; p = p->next) { + assert(p->label != NULL); + l[s] = malloc_chk(sizeof(LABELARRAY), "lables"); + if(p->prog == NULL) { + l[s]->prog = NULL; } else { - ar[asize]->prog = strdup_chk(np->prog, "ar[].prog"); + l[s]->prog = strdup_chk(p->prog, "labels.prog"); } - ar[asize]->label = strdup_chk(np->label, "ar[].label"); - ar[asize++]->adr = np->adr; + l[s]->label = strdup_chk(p->label, "labels.label"); + l[s++]->adr = p->adr; } } - 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(i = 0; i < s; i++) { + if(l[i]->prog != NULL) { + 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); } } -/* ラベル表を解放する */ +/** + * ラベル表を解放する + */ void freelabel() { int i; - LABELTAB *np, *nq; + LABELTAB *p, *q; for(i = 0; i < LABELTABSIZE; i++) { - for(np = labels[i]; np != NULL; np = nq) { - nq = np->next; - if(np->prog != NULL) { - free(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->prog); + FREE(p->label); + FREE(p); } } }