c76c3a7590ee6a5651c186d0573c875a26705d55
[YACASL2.git] / src / label.c
1 #include "casl2.h"
2 #include "assemble.h"
3
4 int labelcnt = 0;                /* ラベル数 */
5 LABELTAB *labels[LABELTABSIZE];  /* ラベル表 */
6
7 /* プログラム名とラベルに対応するハッシュ値を返す */
8 unsigned labelhash(const char *prog, const char *label)
9 {
10     HKEY *keys[2];
11     int i = 0;
12
13     if(prog != NULL) {
14         keys[i] = malloc_chk(sizeof(HKEY), "labelhash.key[]");
15         keys[i]->type = CHARS;
16         keys[i]->val.s = strdup_chk(prog, "labelhash.key[].val");
17     }
18     keys[i] = malloc_chk(sizeof(HKEY), "labelhash.key[]");
19     keys[i]->type = CHARS;
20     keys[i]->val.s = strdup_chk(label, "labelhash.key[].val");
21     /* ハッシュ値を返す */
22     return hash(i+1, keys, LABELTABSIZE);
23 }
24
25 /* ラベル表からアドレスを検索する */
26 WORD getlabel(const char *prog, const char *label)
27 {
28     assert(label != NULL);
29     LABELTAB *np;
30
31     for(np = labels[labelhash(prog, label)]; np != NULL; np = np->next) {
32         if((prog == NULL || (np->prog != NULL && strcmp(prog, np->prog) == 0)) &&
33            strcmp(label, np->label) == 0)
34         {
35             return np->adr;
36         }
37     }
38     return 0xFFFF;
39 }
40
41 /* プログラム名、ラベル、アドレスをラベル表に追加する */
42 bool addlabel(const char *prog, const char *label, WORD adr)
43 {
44     assert(label != NULL);
45     LABELTAB *np;
46     unsigned hashval;
47
48     /* 登録されたラベルを検索。すでに登録されている場合はエラー発生 */
49     if(getlabel(prog, label) != 0xFFFF) {
50         setcerr(101, label);    /* label already defined */
51         return false;
52     }
53     /* メモリを確保 */
54     np = malloc_chk(sizeof(LABELTAB), "labels.next");
55     /* プログラム名を設定 */
56     if(prog == NULL) {
57         np->prog = NULL;
58     } else {
59         np->prog = strdup_chk(prog, "labels.prog");
60     }
61     /* ラベルを設定 */
62     np->label = strdup_chk(label, "labels.label");
63     /* アドレスを設定 */
64     np->adr = adr;
65     /* ラベル数を設定 */
66     labelcnt++;
67     /* ハッシュ表へ追加 */
68     hashval = labelhash(prog, label);
69     np->next = labels[hashval];
70     labels[hashval] = np;
71     return true;
72 }
73
74 int compare_adr(const void *a, const void *b)
75 {
76     return (**(LABELARRAY **)a).adr - (**(LABELARRAY **)b).adr;
77 }
78
79 /* ラベル表を表示する */
80 void printlabel()
81 {
82     int i, asize = 0;
83     LABELTAB *np;
84     LABELARRAY *ar[labelcnt];
85
86     for(i = 0; i < LABELTABSIZE; i++) {
87         for(np = labels[i]; np != NULL; np = np->next) {
88             assert(np->label != NULL);
89             ar[asize] = malloc_chk(sizeof(LABELARRAY), "ar[]");
90             if(np->prog == NULL) {
91                 ar[asize]->prog = NULL;
92             } else {
93                 ar[asize]->prog = strdup_chk(np->prog, "ar[].prog");
94             }
95             ar[asize]->label = strdup_chk(np->label, "ar[].label");
96             ar[asize++]->adr = np->adr;
97         }
98     }
99     qsort(ar, asize, sizeof(*ar), compare_adr);
100     for(i = 0; i < asize; i++) {
101         if(ar[i]->prog != NULL) {
102             fprintf(stdout, "%s.", ar[i]->prog);
103         }
104         fprintf(stdout, "%s ---> #%04X\n", ar[i]->label, ar[i]->adr);
105     }
106 }
107
108 /* ラベル表を解放する */
109 void freelabel()
110 {
111     int i;
112     LABELTAB *np, *nq;
113
114     for(i = 0; i < LABELTABSIZE; i++) {
115         for(np = labels[i]; np != NULL; np = nq) {
116             nq = np->next;
117             if(np->prog != NULL) {
118                 free(np->prog);
119             }
120             free(np->label);
121             free(np);
122         }
123     }
124 }