12 * プログラム名とラベルに対応するハッシュ値を返す
19 unsigned labelhash(const char *prog, const char *label);
22 * ラベルを比較した結果を返す。qsort内で使われる関数
24 * @return ラベルが同一の場合は0、異なる場合は0以外
29 int compare_adr(const void *a, const void *b);
34 static int labelcnt = 0;
39 static LABELTAB *labels[LABELTABSIZE];
44 static CERR cerr_label[] = {
45 { 101, "label already defined" },
46 { 102, "label table is full" },
47 { 103, "label not found" },
50 unsigned labelhash(const char *prog, const char *label)
57 keys[i] = malloc_chk(sizeof(HKEY), "labelhash.key");
58 keys[i]->type = CHARS;
59 keys[i]->val.s = strdup_chk(prog, "labelhash.key.val");
62 keys[i] = malloc_chk(sizeof(HKEY), "labelhash.key");
63 keys[i]->type = CHARS;
64 keys[i]->val.s = strdup_chk(label, "labelhash.key.val");
65 h = hash(i+1, keys, LABELTABSIZE);
66 for(j = 0; j < i + 1; j++) {
73 int compare_adr(const void *a, const void *b)
75 return (**(LABELARRAY **)a).adr - (**(LABELARRAY **)b).adr;
78 /* assemble.hで定義された関数群 */
79 void addcerrlist_label()
81 addcerrlist(ARRAYSIZE(cerr_label), cerr_label);
84 WORD getlabel(const char *prog, const char *label)
86 assert(prog != NULL && label != NULL);
89 for(p = labels[labelhash(prog, label)]; p != NULL; p = p->next) {
90 if((*prog == '\0' || (strcmp(prog, p->prog) == 0)) &&
91 strcmp(label, p->label) == 0)
99 bool addlabel(const char *prog, const char *label, WORD adr)
101 assert(label != NULL);
105 /* 登録されたラベルを検索。すでに登録されている場合はエラー発生 */
106 if(getlabel(prog, label) != 0xFFFF) {
107 setcerr(101, label); /* label already defined */
111 p = malloc_chk(sizeof(LABELTAB), "labels.next");
113 p->prog = strdup_chk(prog, "labels.prog");
115 p->label = strdup_chk(label, "labels.label");
121 hashval = labelhash(prog, label);
122 p->next = labels[hashval];
133 l = calloc_chk(labelcnt, sizeof(LABELARRAY **), "labels");
134 for(i = 0; i < LABELTABSIZE; i++) {
135 for(p = labels[i]; p != NULL; p = p->next) {
136 assert(p->label != NULL);
137 l[s] = malloc_chk(sizeof(LABELARRAY), "lables");
138 l[s]->prog = strdup_chk(p->prog, "labels.prog");
139 l[s]->label = strdup_chk(p->label, "labels.label");
140 l[s++]->adr = p->adr;
143 qsort(l, s, sizeof(*l), compare_adr);
144 for(i = 0; i < s; i++) {
145 if(*(l[i]->prog) != '\0') {
146 fprintf(stdout, "%s.", l[i]->prog);
148 fprintf(stdout, "%s ---> #%04X\n", l[i]->label, l[i]->adr);
161 for(i = 0; i < LABELTABSIZE; i++) {
162 for(p = labels[i]; p != NULL; p = q) {