Debian Codegraph @

src/tree.c

raw symbols SCIP
1 // ACME - a crossassembler for producing 6502/65c02/65816/65ce02 code.
2 // Copyright (C) 1998-2024 Marco Baye
3 // Have a look at "acme.c" for further info
4 //
5 // tree stuff
6 #include "tree.h"
7 #include "config.h"
8 #include "dynabuf.h"
9 #include "global.h"
10 #include "platform.h"
11
12
13 // Functions
14
15 // Compute hash value by exclusive ORing the node's ID string and write
16 // output to struct.
17 // This function is not allowed to change GlobalDynaBuf!
18 static hash_t make_hash(struct ronode *node) {
19 register char byte;
20 register const char *read;
21 register hash_t tmp = 0;
22
23 read = node->id_string;
24 while ((byte = *read++))
25 tmp = ((tmp << 7) | (tmp >> (8 * sizeof(hash_t) - 7))) ^ byte;
26 node->hash_value = tmp;
27 return tmp;
28 }
29
30 // Link a predefined data set to a tree
31 static void add_node_to_tree(struct ronode **tree, struct ronode *node_to_add)
32 {
33 hash_t hash;
34
35 // compute hash value
36 hash = make_hash(node_to_add);
37 // search for NULL pointer to replace
38 while (*tree) {
39 // decide which way to go
40 if (hash > (*tree)->hash_value)
41 tree = &((*tree)->greater_than);
42 else
43 tree = &((*tree)->less_than_or_equal);
44 }
45 *tree = node_to_add; // add new leaf to tree
46 // New nodes are always added as leaves, so there's no need to copy a second
47 // pointer. And because the PREDEF* macros contain NULL as init values, it is
48 // not necessary to clear the new node's greater_than and less_than_or_equal
49 // fields.
50 }
51
52 // Add predefined tree items to given tree. The PREDEF* macros set the hash
53 // to 1 in all entries but the last, and to 0 in the last entry.
54 static void tree_from_list(struct ronode **tree, struct ronode *table_to_add)
55 {
56 //printf("Building tree from list.\n");
57 // Caution when trying to optimise this. :)
58 while (table_to_add->hash_value)
59 add_node_to_tree(tree, table_to_add++);
60 add_node_to_tree(tree, table_to_add);
61 }
62
63 // Search for a given ID string in a given tree.
64 // Compute the hash of the given string and then use that to try to find a
65 // tree item that matches the given data (HashValue and DynaBuf-String).
66 // Store "body" component in node_body and return TRUE.
67 // Return FALSE if no matching item found.
68 int tree_easy_scan(struct ronode *tree, void **node_body, struct dynabuf *dyna_buf)
69 {
70 struct ronode wanted; // temporary storage
71 const char *p1,
72 *p2;
73 char b1,
74 b2;
75 hash_t hash;
76
77 // check if tree is actually ready to use. if not, build it from list.
78 // (list's first item does not contain real data, so "greater_than" is
79 // used to hold pointer to tree root)
80 if (tree->greater_than == NULL)
81 tree_from_list(&tree->greater_than, tree + 1); // real data starts at next list item
82 tree = tree->greater_than; // go from list head to tree root
83 // ok, we're done with this setup stuff.
84 // from now on, "greater_than" really means "greater_than"!
85
86 wanted.id_string = dyna_buf->buffer;
87 hash = make_hash(&wanted);
88 while (tree) {
89 // compare HashValue
90 if (hash > tree->hash_value) {
91 // wanted hash is bigger than current, so go
92 // to tree branch with bigger hashes
93 tree = tree->greater_than;
94 continue;
95 }
96 if (hash == tree->hash_value) {
97 p1 = wanted.id_string;
98 p2 = tree->id_string;
99 do {
100 b1 = *p1++;
101 b2 = *p2++;
102 } while ((b1 == b2) && b1);
103 if (b1 == b2) {
104 // store body data
105 *node_body = tree->body;
106 return TRUE;
107 }
108 }
109 // either the wanted hash is smaller or
110 // it was exact but didn't match
111 tree = tree->less_than_or_equal;
112 }
113 return FALSE ; // indicate failure
114 }
115
116 // Search for a "RAM tree" item. Compute the hash of string in GlobalDynaBuf
117 // and then use that to try to find a tree item that matches the given data
118 // (HashValue, ID_Number, GlobalDynaBuf-String). Save pointer to found tree
119 // item in given location.
120 // If no matching item is found, check the "create" flag. If it is set, create
121 // a new tree item, link to tree, fill with data and store its pointer. If the
122 // "create" flag is zero, store NULL as result.
123 // Returns whether item was created.
124 int tree_hard_scan(struct rwnode **result, struct rwnode **forest, int id_number, boolean create)
125 {
126 struct ronode wanted; // temporary storage
127 struct rwnode **current_node;
128 struct rwnode *new_leaf_node;
129 const char *p1,
130 *p2;
131 char b1,
132 b2;
133 hash_t byte_hash;
134
135 wanted.id_string = GLOBALDYNABUF_CURRENT;
136 // incorporate ID number into hash value
137 byte_hash = make_hash(&wanted) ^ id_number;
138 wanted.hash_value = byte_hash; // correct struct's hash
139 PLATFORM_UINT2CHAR(byte_hash); // transform into byte
140
141 current_node = &(forest[byte_hash]); // point into table
142 while (*current_node) {
143 // compare HashValue
144 if (wanted.hash_value > (*current_node)->hash_value) {
145 // wanted hash is bigger than current, so go
146 // to tree branch with bigger hashes
147 current_node = &((*current_node)->greater_than);
148 continue;
149 }
150 if (wanted.hash_value == (*current_node)->hash_value) {
151 if (id_number == (*current_node)->id_number) {
152 p1 = wanted.id_string;
153 p2 = (*current_node)->id_string;
154 do {
155 b1 = *p1++;
156 b2 = *p2++;
157 } while ((b1 == b2) && b1);
158 if (b1 == b2) {
159 // store node pointer
160 *result = *current_node;
161 // return FALSE because node
162 // was not created
163 return FALSE;
164 }
165 }
166 }
167 // either the wanted hash is smaller or
168 // it was exact but didn't match
169 current_node = &((*current_node)->less_than_or_equal);
170 }
171 // node wasn't found. Check whether to create it
172 if (!create) {
173 *result = NULL; // indicate failure
174 return FALSE; // return FALSE because node was not created
175 }
176 // create new node
177 new_leaf_node = safe_malloc(sizeof(*new_leaf_node));
178 new_leaf_node->greater_than = NULL;
179 new_leaf_node->less_than_or_equal = NULL;
180 new_leaf_node->hash_value = wanted.hash_value;
181 new_leaf_node->id_number = id_number;
182 new_leaf_node->id_string = dynabuf_get_copy(GlobalDynaBuf); // make permanent copy
183 // add new leaf to tree
184 *current_node = new_leaf_node;
185 // store pointer to new node in result location
186 *result = new_leaf_node;
187 return TRUE; // return TRUE because node was created
188 }
189
190 // Call given function for each object of matching type in the given tree.
191 // Calls itself recursively.
192 static void dump_tree(struct rwnode *node, int id_number, void (*fn)(struct rwnode *, FILE *), FILE *env)
193 {
194
195 if (node->id_number == id_number)
196 fn(node, env);
197 if (node->greater_than)
198 dump_tree(node->greater_than, id_number, fn, env);
199 if (node->less_than_or_equal)
200 dump_tree(node->less_than_or_equal, id_number, fn, env);
201 }
202
203 // call dump_tree for each non-zero entry of the given tree table.
204 void tree_dump_forest(struct rwnode **forest, int id_number, void (*fn)(struct rwnode *, FILE *), FILE *env)
205 {
206 int ii;
207
208 for (ii = 255; ii >= 0; --ii) {
209 if (*forest)
210 dump_tree(*forest, id_number, fn, env);
211 ++forest;
212 }
213 }