Debian Codegraph @

src/asm/tree.c

raw symbols SCIP
1 //cxx . . $ `<file>/src/asm/tree.c`/
2 // ACME - a crossassembler for producing 6502/65c02/65816 code.
3 // Copyright (C) 1998-2006 Marco Baye
4 // Have a look at "acme.c" for further info
5 //
6 // Tree stuff
7
8 #include "config.h"cxx . . $ `<file>/src/asm/config.h`/
9 #include "dynabuf.h"cxx . . $ `<file>/src/asm/dynabuf.h`/
10 #include "global.h"cxx . . $ `<file>/src/asm/global.h`/
11 #include "tree.h"cxx . . $ `<file>/src/asm/tree.h`/
12 #include "platform.h"cxx . . $ `<file>/src/asm/platform.h`/
13
14
15 // Functions
16
17 // Compute hash value by exclusive ORing the node's ID string and write
18 // output to struct.
19 // This function is not allowed to change GlobalDynaBuf!
20 hash_tcxx . . $ hash_t# make_hashcxx . . $ make_hash(1aa65588baae3834).(node_tcxx . . $ node_t#* node) {
21 register char byte;
22 register const char* read;
23 register hash_tcxx . . $ hash_t# tmp = 0;
24
25 read = node->id_stringcxx . . $ node_t#id_string.;
26 while((byte = *read++))
27 tmp = ((tmp << 7) | (tmp >> (8 * sizeof(hash_tcxx . . $ hash_t#) - 7))) ^ byte;
28 node->hash_valuecxx . . $ node_t#hash_value. = tmp;
29 return(tmp);
30 }
31
32 // Link a predefined data set to a tree
33 void add_node_to_treecxx . . $ add_node_to_tree(90ba9e887489f1c).(node_tcxx . . $ node_t#** tree, node_tcxx . . $ node_t#* node_to_add) {
34 hash_tcxx . . $ hash_t# hash;
35
36 // compute hash value
37 hash = make_hashcxx . . $ make_hash(1aa65588baae3834).(node_to_add);
38 while(*tree) {
39 // compare HashValue
40 if(hash > (*tree)->hash_valuecxx . . $ node_t#hash_value.)
41 tree = &((*tree)->greater_thancxx . . $ node_t#greater_than.);
42 else
43 tree = &((*tree)->less_than_or_equalcxx . . $ node_t#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 HashValue
53 // to 1 in all entries but the last. The last entry contains 0.
54 void Tree_add_tablecxx . . $ Tree_add_table(90ba9e887489f1c).(node_tcxx . . $ node_t#** tree, node_tcxx . . $ node_t#* table_to_add) {
55 // Caution when trying to optimise this. :)
56 while(table_to_add->hash_valuecxx . . $ node_t#hash_value.)
57 add_node_to_treecxx . . $ add_node_to_tree(90ba9e887489f1c).(tree, table_to_add++);
58 add_node_to_treecxx . . $ add_node_to_tree(90ba9e887489f1c).(tree, table_to_add);
59 }
60
61 // Search for a given ID string in a given tree.
62 // Compute the hash of the given string and then use that to try to find a
63 // tree item that matches the given data (HashValue and DynaBuf-String).
64 // Store "Body" component in NodeBody and return TRUE.
65 // Return FALSE if no matching item found.
66 boolcxx . . $ `<external>/13015445228558014418/stdbool.h:36:9`! Tree_easy_scancxx . . $ Tree_easy_scan(7f10be3e8224db50).(node_tcxx . . $ node_t#* tree, void** node_body, struct dynabuf_tcxx . . $ dynabuf_t#* dyna_buf) {
67 node_tcxx . . $ node_t# wanted;// temporary storage
68 const char *p1,
69 *p2;
70 char b1,
71 b2;
72 hash_tcxx . . $ hash_t# hash;
73
74 wanted.id_stringcxx . . $ node_t#id_string. = dyna_buf->buffercxx . . $ dynabuf_t#buffer.;
75 hash = make_hashcxx . . $ make_hash(1aa65588baae3834).(&wanted);
76 while(tree) {
77 // compare HashValue
78 if(hash > tree->hash_valuecxx . . $ node_t#hash_value.) {
79 // wanted hash is bigger than current, so go
80 // to tree branch with bigger hashes
81 tree = tree->greater_thancxx . . $ node_t#greater_than.;
82 continue;
83 }
84 if(hash == tree->hash_valuecxx . . $ node_t#hash_value.) {
85 p1 = wanted.id_stringcxx . . $ node_t#id_string.;
86 p2 = tree->id_stringcxx . . $ node_t#id_string.;
87 do {
88 b1 = *p1++;
89 b2 = *p2++;
90 } while((b1 == b2) && b1);
91 if(b1 == b2) {
92 // store body data
93 *node_body = tree->bodycxx . . $ node_t#body.;
94 return(TRUEcxx . . $ `src/asm/config.h:55:9`!);
95 }
96 }
97 // either the wanted hash is smaller or
98 // it was exact but didn't match
99 tree = tree->less_than_or_equalcxx . . $ node_t#less_than_or_equal.;
100 }
101 return(FALSEcxx . . $ `src/asm/config.h:54:9`!); // indicate failure
102 }
103
104 // Search for a "RAM tree" item. Compute the hash of string in GlobalDynaBuf
105 // and then use that to try to find a tree item that matches the given data
106 // (HashValue, ID_Number, GlobalDynaBuf-String). Save pointer to found tree
107 // item in given location.
108 // If no matching item is found, check the "Create" flag. If it is set, create
109 // a new tree item, link to tree, fill with data and store its pointer. If the
110 // "Create" flag is clear, store NULL as result.
111 // Returns whether item was created.
112 boolcxx . . $ `<external>/13015445228558014418/stdbool.h:36:9`! Tree_hard_scancxx . . $ Tree_hard_scan(fbd309352d4134f6).(node_ra_tcxx . . $ node_ra_t#** result, node_ra_tcxx . . $ node_ra_t#** forest, int id_number, boolcxx . . $ `<external>/13015445228558014418/stdbool.h:36:9`! create) {
113 node_tcxx . . $ node_t# wanted; // temporary storage
114 node_ra_tcxx . . $ node_ra_t# **current_node;
115 node_ra_tcxx . . $ node_ra_t# *new_leaf_node;
116 const char *p1,
117 *p2;
118 char b1,
119 b2;
120 hash_tcxx . . $ hash_t# byte_hash;
121
122 wanted.id_stringcxx . . $ node_t#id_string. = GLOBALDYNABUF_CURRENTcxx . . $ `src/asm/dynabuf.h:23:9`!;
123 // incorporate ID number into hash value
124 byte_hash = make_hashcxx . . $ make_hash(1aa65588baae3834).(&wanted) ^ id_number;
125 wanted.hash_valuecxx . . $ node_t#hash_value. = byte_hash;// correct struct's hash
126 PLATFORM_UINT2CHARcxx . . $ `src/asm/_std.h:33:9`!(byte_hash);// transform into byte
127
128 current_node = &(forest[byte_hash]);// point into table
129 while(*current_node) {
130 // compare HashValue
131 if(wanted.hash_valuecxx . . $ node_t#hash_value. > (*current_node)->hash_valuecxx . . $ node_ra_t#hash_value.) {
132 // wanted hash is bigger than current, so go
133 // to tree branch with bigger hashes
134 current_node = &((*current_node)->greater_thancxx . . $ node_ra_t#greater_than.);
135 continue;
136 }
137 if(wanted.hash_valuecxx . . $ node_t#hash_value. == (*current_node)->hash_valuecxx . . $ node_ra_t#hash_value.) {
138 if(id_number == (*current_node)->id_numbercxx . . $ node_ra_t#id_number.) {
139 p1 = wanted.id_stringcxx . . $ node_t#id_string.;
140 p2 = (*current_node)->id_stringcxx . . $ node_ra_t#id_string.;
141 do {
142 b1 = *p1++;
143 b2 = *p2++;
144 } while((b1 == b2) && b1);
145 if(b1 == b2) {
146 // store node pointer
147 *result = *current_node;
148 // return FALSE because node
149 // was not created
150 return(FALSEcxx . . $ `src/asm/config.h:54:9`!);
151 }
152 }
153 }
154 // either the wanted hash is smaller or
155 // it was exact but didn't match
156 current_node = &((*current_node)->less_than_or_equalcxx . . $ node_ra_t#less_than_or_equal.);
157 }
158 // node wasn't found. Check whether to create it
159 if(create == FALSEcxx . . $ `src/asm/config.h:54:9`!) {
160 *result = NULLcxx . . $ `<external>/16391990256953433862/stddef.h:419:9`!;// indicate failure
161 return(FALSEcxx . . $ `src/asm/config.h:54:9`!);// return FALSE because node was not created
162 }
163 // create new node
164 new_leaf_node = safe_malloccxx . . $ safe_malloc(bfcfa4d6b7f7ef64).(sizeof(node_ra_tcxx . . $ node_ra_t#));
165 new_leaf_node->greater_thancxx . . $ node_ra_t#greater_than. = NULLcxx . . $ `<external>/16391990256953433862/stddef.h:419:9`!;
166 new_leaf_node->less_than_or_equalcxx . . $ node_ra_t#less_than_or_equal. = NULLcxx . . $ `<external>/16391990256953433862/stddef.h:419:9`!;
167 new_leaf_node->hash_valuecxx . . $ node_ra_t#hash_value. = wanted.hash_valuecxx . . $ node_t#hash_value.;
168 new_leaf_node->id_numbercxx . . $ node_ra_t#id_number. = id_number;
169 new_leaf_node->id_stringcxx . . $ node_ra_t#id_string. = DynaBuf_get_copycxx . . $ DynaBuf_get_copy(d7229e135d075701).(GlobalDynaBufcxx . . $ GlobalDynaBuf.);// make permanent copy
170 // add new leaf to tree
171 *current_node = new_leaf_node;
172 // store pointer to new node in result location
173 *result = new_leaf_node;
174 return(TRUEcxx . . $ `src/asm/config.h:55:9`!);// return TRUE because node was created
175 }
176
177 // Call given function for each object of matching type in the given tree.
178 // Calls itself recursively.
179 void dump_treecxx . . $ dump_tree(52d7678808d9f68b).(node_ra_tcxx . . $ node_ra_t#* node, int id_number, void (*fn)(node_ra_tcxx . . $ node_ra_t#*, FILEcxx . . $ FILE#*), FILEcxx . . $ FILE#* env) {
180
181 if(node->id_numbercxx . . $ node_ra_t#id_number. == id_number)
182 fn(node, env);
183 if(node->greater_thancxx . . $ node_ra_t#greater_than.)
184 dump_treecxx . . $ dump_tree(52d7678808d9f68b).(node->greater_thancxx . . $ node_ra_t#greater_than., id_number, fn, env);
185 if(node->less_than_or_equalcxx . . $ node_ra_t#less_than_or_equal.)
186 dump_treecxx . . $ dump_tree(52d7678808d9f68b).(node->less_than_or_equalcxx . . $ node_ra_t#less_than_or_equal., id_number, fn, env);
187 }
188
189 // Calls Tree_dump_tree for each non-zero entry of the given tree table.
190 void Tree_dump_forestcxx . . $ Tree_dump_forest(dc7737009984b828).(node_ra_tcxx . . $ node_ra_t#** forest, int id_number, void (*fn)(node_ra_tcxx . . $ node_ra_t#*, FILEcxx . . $ FILE#*), FILEcxx . . $ FILE#* env) {
191 int i;
192
193 for(i = 255; i >= 0; i--) {
194 if(*forest)
195 dump_treecxx . . $ dump_tree(52d7678808d9f68b).(*forest, id_number, fn, env);
196 forest++;
197 }
198 }