12 * ラベルのハッシュ値をセットしたキーを返す
14 * @return ハッシュ値をセットしたキー
18 HKEY *label_hashkey(const char *value);
21 * プログラム名とラベルに対応するハッシュ値を返す
28 unsigned labelhash(const char *prog, const char *label);
31 * ラベルを比較した結果を返す。qsort内で使われる関数
33 * @return ラベルが同一の場合は0、異なる場合は0以外
38 int compare_adr(const void *a, const void *b);
43 static int labelcnt = 0;
48 static LABELTAB *labels[LABELTABSIZE];
53 static CERR cerr_label[] = {
54 { 101, "label already defined" },
55 { 102, "label table is full" },
56 { 103, "label not found" },
59 HKEY *label_hashkey(const char *value) {
62 key = malloc_chk(sizeof(HKEY), "label_hashkey");
64 key->val.s = strdup_chk(value, "label_hashkey.value");
68 unsigned labelhash(const char *prog, const char *label)
75 keys[i++] = label_hashkey(prog);
77 keys[i] = label_hashkey(label);
78 h = hash(i+1, keys, LABELTABSIZE);
79 for(j = 0; j < i + 1; j++) {
86 int compare_adr(const void *a, const void *b)
88 return (**(LABELARRAY **)a).adr - (**(LABELARRAY **)b).adr;
91 /* assemble.hで定義された関数群 */
92 void addcerrlist_label()
94 addcerrlist(ARRAYSIZE(cerr_label), cerr_label);
97 WORD getlabel(const char *prog, const char *label)
99 assert(prog != NULL && label != NULL);
103 for(p = labels[labelhash(prog, label)]; p != NULL; p = p->next) {
105 if((*prog == '\0' || (strcmp(prog, l->prog) == 0)) &&
106 strcmp(label, l->label) == 0)
114 bool addlabel(const char *prog, const char *label, WORD adr)
116 assert(label != NULL);
121 /* 登録されたラベルを検索。すでに登録されている場合はエラー発生 */
122 if(getlabel(prog, label) != 0xFFFF) {
123 setcerr(101, label); /* label already defined */
127 p = malloc_chk(sizeof(LABELTAB), "labels.next");
128 l = p->label = malloc_chk(sizeof(LABELARRAY), "labels.label");
130 l->prog = strdup_chk(prog, "label.prog");
132 l->label = strdup_chk(label, "label.label");
138 hashval = labelhash(prog, label);
139 p->next = labels[hashval];
150 l = calloc_chk(labelcnt, sizeof(LABELARRAY **), "labels");
151 for(i = 0; i < LABELTABSIZE; i++) {
152 for(p = labels[i]; p != NULL; p = p->next) {
153 assert(p->label != NULL);
157 qsort(l, s, sizeof(*l), compare_adr);
158 for(i = 0; i < s; i++) {
159 if(*(l[i]->prog) != '\0') {
160 fprintf(stdout, "%s.", l[i]->prog);
162 fprintf(stdout, "%s ---> #%04X\n", l[i]->label, l[i]->adr);
172 for(i = 0; i < LABELTABSIZE; i++) {
173 for(p = labels[i]; p != NULL; p = q) {
175 FREE(p->label->prog);
176 FREE(p->label->label);