| 1 | // |
| 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" |
| 9 | #include "dynabuf.h" |
| 10 | #include "global.h" |
| 11 | #include "tree.h" |
| 12 | #include "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_t make_hash(node_t* node) { |
| 21 | register char byte; |
| 22 | register const char* read; |
| 23 | register hash_t tmp = 0; |
| 24 | |
| 25 | read = node->id_string; |
| 26 | while((byte = *read++)) |
| 27 | tmp = ((tmp << 7) | (tmp >> (8 * sizeof(hash_t) - 7))) ^ byte; |
| 28 | node->hash_value = tmp; |
| 29 | return(tmp); |
| 30 | } |
| 31 | |
| 32 | // Link a predefined data set to a tree |
| 33 | void add_node_to_tree(node_t** tree, node_t* node_to_add) { |
| 34 | hash_t hash; |
| 35 | |
| 36 | // compute hash value |
| 37 | hash = make_hash(node_to_add); |
| 38 | while(*tree) { |
| 39 | // compare HashValue |
| 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 HashValue |
| 53 | // to 1 in all entries but the last. The last entry contains 0. |
| 54 | void Tree_add_table(node_t** tree, node_t* table_to_add) { |
| 55 | // Caution when trying to optimise this. :) |
| 56 | while(table_to_add->hash_value) |
| 57 | add_node_to_tree(tree, table_to_add++); |
| 58 | add_node_to_tree(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 | bool Tree_easy_scan(node_t* tree, void** node_body, struct dynabuf_t* dyna_buf) { |
| 67 | node_t wanted;// temporary storage |
| 68 | const char *p1, |
| 69 | *p2; |
| 70 | char b1, |
| 71 | b2; |
| 72 | hash_t hash; |
| 73 | |
| 74 | wanted.id_string = dyna_buf->buffer; |
| 75 | hash = make_hash(&wanted); |
| 76 | while(tree) { |
| 77 | // compare HashValue |
| 78 | if(hash > tree->hash_value) { |
| 79 | // wanted hash is bigger than current, so go |
| 80 | // to tree branch with bigger hashes |
| 81 | tree = tree->greater_than; |
| 82 | continue; |
| 83 | } |
| 84 | if(hash == tree->hash_value) { |
| 85 | p1 = wanted.id_string; |
| 86 | p2 = tree->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->body; |
| 94 | return(TRUE); |
| 95 | } |
| 96 | } |
| 97 | // either the wanted hash is smaller or |
| 98 | // it was exact but didn't match |
| 99 | tree = tree->less_than_or_equal; |
| 100 | } |
| 101 | return(FALSE); // 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 | bool Tree_hard_scan(node_ra_t** result, node_ra_t** forest, int id_number, bool create) { |
| 113 | node_t wanted; // temporary storage |
| 114 | node_ra_t **current_node; |
| 115 | node_ra_t *new_leaf_node; |
| 116 | const char *p1, |
| 117 | *p2; |
| 118 | char b1, |
| 119 | b2; |
| 120 | hash_t byte_hash; |
| 121 | |
| 122 | wanted.id_string = GLOBALDYNABUF_CURRENT; |
| 123 | // incorporate ID number into hash value |
| 124 | byte_hash = make_hash(&wanted) ^ id_number; |
| 125 | wanted.hash_value = byte_hash;// correct struct's hash |
| 126 | PLATFORM_UINT2CHAR(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_value > (*current_node)->hash_value) { |
| 132 | // wanted hash is bigger than current, so go |
| 133 | // to tree branch with bigger hashes |
| 134 | current_node = &((*current_node)->greater_than); |
| 135 | continue; |
| 136 | } |
| 137 | if(wanted.hash_value == (*current_node)->hash_value) { |
| 138 | if(id_number == (*current_node)->id_number) { |
| 139 | p1 = wanted.id_string; |
| 140 | p2 = (*current_node)->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(FALSE); |
| 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_equal); |
| 157 | } |
| 158 | // node wasn't found. Check whether to create it |
| 159 | if(create == FALSE) { |
| 160 | *result = NULL;// indicate failure |
| 161 | return(FALSE);// return FALSE because node was not created |
| 162 | } |
| 163 | // create new node |
| 164 | new_leaf_node = safe_malloc(sizeof(node_ra_t)); |
| 165 | new_leaf_node->greater_than = NULL; |
| 166 | new_leaf_node->less_than_or_equal = NULL; |
| 167 | new_leaf_node->hash_value = wanted.hash_value; |
| 168 | new_leaf_node->id_number = id_number; |
| 169 | new_leaf_node->id_string = DynaBuf_get_copy(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(TRUE);// 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_tree(node_ra_t* node, int id_number, void (*fn)(node_ra_t*, FILE*), FILE* env) { |
| 180 | |
| 181 | if(node->id_number == id_number) |
| 182 | fn(node, env); |
| 183 | if(node->greater_than) |
| 184 | dump_tree(node->greater_than, id_number, fn, env); |
| 185 | if(node->less_than_or_equal) |
| 186 | dump_tree(node->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_forest(node_ra_t** forest, int id_number, void (*fn)(node_ra_t*, FILE*), FILE* env) { |
| 191 | int i; |
| 192 | |
| 193 | for(i = 255; i >= 0; i--) { |
| 194 | if(*forest) |
| 195 | dump_tree(*forest, id_number, fn, env); |
| 196 | forest++; |
| 197 | } |
| 198 | } |