11 static int labelcnt = 0; /* ラベル数 */
12 static LABELTAB *labels[LABELTABSIZE]; /* ラベル表 */
15 static unsigned labelhash(const char *prog, const char *label);
17 static int compare_adr(const void *a, const void *b);
21 * プログラム名とラベルに対応するハッシュ値を返す
23 unsigned labelhash(const char *prog, const char *label)
29 keys[i] = malloc_chk(sizeof(HKEY), "labelhash.key[]");
30 keys[i]->type = CHARS;
31 keys[i]->val.s = strdup_chk(prog, "labelhash.key[].val");
33 keys[i] = malloc_chk(sizeof(HKEY), "labelhash.key[]");
34 keys[i]->type = CHARS;
35 keys[i]->val.s = strdup_chk(label, "labelhash.key[].val");
37 return hash(i+1, keys, LABELTABSIZE);
43 WORD getlabel(const char *prog, const char *label)
45 assert(label != NULL);
48 for(np = labels[labelhash(prog, label)]; np != NULL; np = np->next) {
49 if((prog == NULL || (np->prog != NULL && strcmp(prog, np->prog) == 0)) &&
50 strcmp(label, np->label) == 0)
59 * プログラム名、ラベル、アドレスをラベル表に追加する
61 bool addlabel(const char *prog, const char *label, WORD adr)
63 assert(label != NULL);
67 /* 登録されたラベルを検索。すでに登録されている場合はエラー発生 */
68 if(getlabel(prog, label) != 0xFFFF) {
69 setcerr(101, label); /* label already defined */
73 np = malloc_chk(sizeof(LABELTAB), "labels.next");
78 np->prog = strdup_chk(prog, "labels.prog");
81 np->label = strdup_chk(label, "labels.label");
87 hashval = labelhash(prog, label);
88 np->next = labels[hashval];
96 int compare_adr(const void *a, const void *b)
98 return (**(LABELARRAY **)a).adr - (**(LABELARRAY **)b).adr;
108 LABELARRAY *ar[labelcnt];
110 for(i = 0; i < LABELTABSIZE; i++) {
111 for(np = labels[i]; np != NULL; np = np->next) {
112 assert(np->label != NULL);
113 ar[asize] = malloc_chk(sizeof(LABELARRAY), "ar[]");
114 if(np->prog == NULL) {
115 ar[asize]->prog = NULL;
117 ar[asize]->prog = strdup_chk(np->prog, "ar[].prog");
119 ar[asize]->label = strdup_chk(np->label, "ar[].label");
120 ar[asize++]->adr = np->adr;
123 qsort(ar, asize, sizeof(*ar), compare_adr);
124 for(i = 0; i < asize; i++) {
125 if(ar[i]->prog != NULL) {
126 fprintf(stdout, "%s.", ar[i]->prog);
128 fprintf(stdout, "%s ---> #%04X\n", ar[i]->label, ar[i]->adr);
140 for(i = 0; i < LABELTABSIZE; i++) {
141 for(np = labels[i]; np != NULL; np = nq) {
143 if(np->prog != NULL) {
144 free_chk(np->prog, "np.prog");
146 free_chk(np->label, "np.label");