11 static int labelcnt = 0; /* ラベル数 */
12 static LABELTAB *labels[LABELTABSIZE]; /* ラベル表 */
15 * プログラム名とラベルに対応するハッシュ値を返す
17 unsigned labelhash(const char *prog, const char *label)
23 keys[i] = malloc_chk(sizeof(HKEY), "labelhash.key[]");
24 keys[i]->type = CHARS;
25 keys[i]->val.s = strdup_chk(prog, "labelhash.key[].val");
27 keys[i] = malloc_chk(sizeof(HKEY), "labelhash.key[]");
28 keys[i]->type = CHARS;
29 keys[i]->val.s = strdup_chk(label, "labelhash.key[].val");
31 return hash(i+1, keys, LABELTABSIZE);
35 * プログラム名とラベルに対応するアドレスをラベル表から検索する
37 WORD getlabel(const char *prog, const char *label)
39 assert(label != NULL);
42 for(np = labels[labelhash(prog, label)]; np != NULL; np = np->next) {
43 if((prog == NULL || (np->prog != NULL && strcmp(prog, np->prog) == 0)) &&
44 strcmp(label, np->label) == 0)
53 * プログラム名、ラベル、アドレスをラベル表に追加する
55 bool addlabel(const char *prog, const char *label, WORD adr)
57 assert(label != NULL);
61 /* 登録されたラベルを検索。すでに登録されている場合はエラー発生 */
62 if(getlabel(prog, label) != 0xFFFF) {
63 setcerr(101, label); /* label already defined */
67 np = malloc_chk(sizeof(LABELTAB), "labels.next");
72 np->prog = strdup_chk(prog, "labels.prog");
75 np->label = strdup_chk(label, "labels.label");
81 hashval = labelhash(prog, label);
82 np->next = labels[hashval];
90 int compare_adr(const void *a, const void *b)
92 return (**(LABELARRAY **)a).adr - (**(LABELARRAY **)b).adr;
102 LABELARRAY *ar[labelcnt];
104 for(i = 0; i < LABELTABSIZE; i++) {
105 for(np = labels[i]; np != NULL; np = np->next) {
106 assert(np->label != NULL);
107 ar[asize] = malloc_chk(sizeof(LABELARRAY), "ar[]");
108 if(np->prog == NULL) {
109 ar[asize]->prog = NULL;
111 ar[asize]->prog = strdup_chk(np->prog, "ar[].prog");
113 ar[asize]->label = strdup_chk(np->label, "ar[].label");
114 ar[asize++]->adr = np->adr;
117 qsort(ar, asize, sizeof(*ar), compare_adr);
118 for(i = 0; i < asize; i++) {
119 if(ar[i]->prog != NULL) {
120 fprintf(stdout, "%s.", ar[i]->prog);
122 fprintf(stdout, "%s ---> #%04X\n", ar[i]->label, ar[i]->adr);
134 for(i = 0; i < LABELTABSIZE; i++) {
135 for(np = labels[i]; np != NULL; np = nq) {
137 if(np->prog != NULL) {
138 free_chk(np->prog, "np.prog");
140 free_chk(np->label, "np.label");