|
| 1 | +/* Standard libraries */ |
| 2 | +#include <stdlib.h> |
| 3 | +#include <string.h> |
| 4 | + |
| 5 | +/* Odin_II libraries */ |
| 6 | +#include "globals.h" |
| 7 | +#include "types.h" |
| 8 | +#include "ast_util.h" |
| 9 | +#include "parse_making_ast.h" |
| 10 | +#include "odin_util.h" |
| 11 | + |
| 12 | +/* This files header */ |
| 13 | +#include "ast_loop_unroll.h" |
| 14 | + |
| 15 | +/* |
| 16 | + * (function: unroll_loops) |
| 17 | + */ |
| 18 | +void unroll_loops() |
| 19 | +{ |
| 20 | + /* preprocess each module */ |
| 21 | + size_t i; |
| 22 | + for (i = 0; i < num_modules; i++) |
| 23 | + { |
| 24 | + ast_node_t* module = for_preprocessor(ast_modules[i]); |
| 25 | + if(module != ast_modules[i]) |
| 26 | + free_whole_tree(ast_modules[i]); |
| 27 | + ast_modules[i] = module; |
| 28 | + } |
| 29 | +} |
| 30 | + |
| 31 | +/* |
| 32 | + * (function: for_preprocessor) |
| 33 | + */ |
| 34 | +ast_node_t* for_preprocessor(ast_node_t* node) |
| 35 | +{ |
| 36 | + if(!node) |
| 37 | + return nullptr; |
| 38 | + /* If this is a for node, something has gone wrong */ |
| 39 | + oassert(!is_for_node(node)) |
| 40 | + |
| 41 | + /* If this node has for loops as children, replace them */ |
| 42 | + bool for_loops = false; |
| 43 | + for(int i=0; i<node->num_children && !for_loops; i++){ |
| 44 | + for_loops = is_for_node(node->children[i]); |
| 45 | + } |
| 46 | + ast_node_t* new_node = for_loops ? replace_fors(node) : node; |
| 47 | + |
| 48 | + /* Run this function recursively on the children */ |
| 49 | + for(int i=0; i<new_node->num_children; i++){ |
| 50 | + ast_node_t* new_child = for_preprocessor(new_node->children[i]); |
| 51 | + |
| 52 | + /* Cleanup replaced child */ |
| 53 | + if(new_node->children[i] != new_child){ |
| 54 | + free_whole_tree(new_node->children[i]); |
| 55 | + new_node->children[i] = new_child; |
| 56 | + } |
| 57 | + } |
| 58 | + return new_node; |
| 59 | +} |
| 60 | + |
| 61 | +/* |
| 62 | + * (function: replace_fors) |
| 63 | + */ |
| 64 | +ast_node_t* replace_fors(ast_node_t* node) |
| 65 | +{ |
| 66 | + oassert(!is_for_node(node)); |
| 67 | + oassert(node != nullptr); |
| 68 | + |
| 69 | + ast_node_t* new_node = ast_node_deep_copy(node); |
| 70 | + if(!new_node) |
| 71 | + return nullptr; |
| 72 | + |
| 73 | + /* process children one at a time */ |
| 74 | + for(int i=0; i<new_node->num_children; i++){ |
| 75 | + /* unroll `for` children */ |
| 76 | + if(is_for_node(new_node->children[i])){ |
| 77 | + ast_node_t* unrolled_for = resolve_for(new_node->children[i]); |
| 78 | + oassert(unrolled_for != nullptr); |
| 79 | + free_whole_tree(new_node->children[i]); |
| 80 | + new_node->children[i] = unrolled_for; |
| 81 | + } |
| 82 | + } |
| 83 | + return new_node; |
| 84 | +} |
| 85 | + |
| 86 | +/* |
| 87 | + * (function: resolve_for) |
| 88 | + */ |
| 89 | +ast_node_t* resolve_for(ast_node_t* node) |
| 90 | +{ |
| 91 | + oassert(is_for_node(node)); |
| 92 | + oassert(node != nullptr); |
| 93 | + ast_node_t* body_parent = nullptr; |
| 94 | + |
| 95 | + ast_node_t* pre = node->children[0]; |
| 96 | + ast_node_t* cond = node->children[1]; |
| 97 | + ast_node_t* post = node->children[2]; |
| 98 | + ast_node_t* body = node->children[3]; |
| 99 | + |
| 100 | + ast_node_t* value = 0; |
| 101 | + if(resolve_pre_condition(pre, &value)) |
| 102 | + { |
| 103 | + error_message(PARSE_ERROR, pre->line_number, pre->file_number, "Unsupported pre-condition node in for loop"); |
| 104 | + } |
| 105 | + |
| 106 | + int error_code = 0; |
| 107 | + condition_function cond_func = resolve_condition(cond, pre->children[0], &error_code); |
| 108 | + if(error_code) |
| 109 | + { |
| 110 | + error_message(PARSE_ERROR, cond->line_number, cond->file_number, "Unsupported condition node in for loop"); |
| 111 | + } |
| 112 | + |
| 113 | + post_condition_function post_func = resolve_post_condition(post, pre->children[0], &error_code); |
| 114 | + if(error_code) |
| 115 | + { |
| 116 | + error_message(PARSE_ERROR, post->line_number, post->file_number, "Unsupported post-condition node in for loop"); |
| 117 | + } |
| 118 | + |
| 119 | + while(cond_func(value->types.number.value)) |
| 120 | + { |
| 121 | + ast_node_t* new_body = dup_and_fill_body(body, pre->children[0], &value, &error_code); |
| 122 | + if(error_code) |
| 123 | + { |
| 124 | + error_message(PARSE_ERROR, pre->line_number, pre->file_number, "Unsupported pre-condition node in for loop"); |
| 125 | + } |
| 126 | + value->types.number.value = post_func(value->types.number.value); |
| 127 | + body_parent = body_parent ? newList_entry(body_parent, new_body) : newList(BLOCK, new_body); |
| 128 | + } |
| 129 | + |
| 130 | + return body_parent; |
| 131 | +} |
| 132 | + |
| 133 | +/* |
| 134 | + * (function: resolve_pre_condition) |
| 135 | + * return 0 if the first value of the variable set |
| 136 | + * in the pre condition of a `for` node has been put in location |
| 137 | + * pointed to by the number pointer. |
| 138 | + * |
| 139 | + * return a non-zero number on failure. |
| 140 | + * define failure constants in header. |
| 141 | + */ |
| 142 | +int resolve_pre_condition(ast_node_t* node, ast_node_t** number_node) |
| 143 | +{ |
| 144 | + /* Add new for loop support here. Keep current work in the TODO |
| 145 | + * Currently supporting: |
| 146 | + * TODO: |
| 147 | + * for(VAR = NUM; VAR {<, >, ==, <=, >=} NUM; VAR = VAR {+, -, *, /} NUM) STATEMENT |
| 148 | + */ |
| 149 | + if( node == nullptr || |
| 150 | + node->type != BLOCKING_STATEMENT || |
| 151 | + node->num_children != 2 || |
| 152 | + node->children[1] == nullptr || |
| 153 | + node->children[1]->type != NUMBERS) |
| 154 | + { |
| 155 | + return UNSUPPORTED_PRE_CONDITION_NODE; |
| 156 | + } |
| 157 | + *number_node = ast_node_deep_copy(node->children[1]); |
| 158 | + return 0; |
| 159 | +} |
| 160 | + |
| 161 | +/* |
| 162 | + * (function: resolve_condition) |
| 163 | + * return a lambda which tests the loop condition for a given value |
| 164 | + */ |
| 165 | +condition_function resolve_condition(ast_node_t* node, ast_node_t* symbol, int* error_code) |
| 166 | +{ |
| 167 | + /* Add new for loop support here. Keep current work in the TODO |
| 168 | + * Currently supporting: |
| 169 | + * TODO: |
| 170 | + * for(VAR = NUM; VAR {<, >, ==, !=, <=, >=} NUM; VAR = VAR {+, -, *, /} NUM) STATEMENT |
| 171 | + */ |
| 172 | + if( node->type != BINARY_OPERATION || |
| 173 | + !( node->types.operation.op == LT || |
| 174 | + node->types.operation.op == GT || |
| 175 | + node->types.operation.op == LOGICAL_EQUAL || |
| 176 | + node->types.operation.op == NOT_EQUAL || |
| 177 | + node->types.operation.op == LTE || |
| 178 | + node->types.operation.op == GTE ) || |
| 179 | + node->num_children != 2 || |
| 180 | + node->children[1] == nullptr || |
| 181 | + node->children[1]->type != NUMBERS || |
| 182 | + node->children[0] == nullptr || |
| 183 | + node->children[0]->type != IDENTIFIERS || |
| 184 | + strcmp(node->children[0]->types.identifier, symbol->types.identifier)) |
| 185 | + { |
| 186 | + *error_code = UNSUPPORTED_CONDITION_NODE; |
| 187 | + return nullptr; |
| 188 | + } |
| 189 | + *error_code = 0; |
| 190 | + return [=](long long value) { |
| 191 | + switch(node->types.operation.op){ |
| 192 | + case LT: |
| 193 | + return value < node->children[1]->types.number.value; |
| 194 | + case GT: |
| 195 | + return value > node->children[1]->types.number.value; |
| 196 | + case NOT_EQUAL: |
| 197 | + return value != node->children[1]->types.number.value; |
| 198 | + case LOGICAL_EQUAL: |
| 199 | + return value == node->children[1]->types.number.value; |
| 200 | + case LTE: |
| 201 | + return value <= node->children[1]->types.number.value; |
| 202 | + case GTE: |
| 203 | + return value >= node->children[1]->types.number.value; |
| 204 | + default: |
| 205 | + return false; |
| 206 | + } |
| 207 | + }; |
| 208 | +} |
| 209 | + |
| 210 | +/* |
| 211 | + * (function: resolve_post_condition) |
| 212 | + * return a lambda which gives the next value |
| 213 | + * of the loop variable given the current value |
| 214 | + * of the loop variable |
| 215 | + */ |
| 216 | +post_condition_function resolve_post_condition(ast_node_t* assignment, ast_node_t* symbol, int* error_code) |
| 217 | +{ |
| 218 | + /* Add new for loop support here. Keep current work in the TODO |
| 219 | + * Currently supporting: |
| 220 | + * TODO: |
| 221 | + * for(VAR = NUM; VAR {<, >, ==, <=, >=} NUM; VAR = VAR {+, -, *, /} NUM) STATEMENT |
| 222 | + */ |
| 223 | + ast_node_t* node = nullptr; |
| 224 | + if( assignment != nullptr && |
| 225 | + assignment->type == BLOCKING_STATEMENT && |
| 226 | + assignment->num_children == 2 && |
| 227 | + assignment->children[0] != nullptr && |
| 228 | + assignment->children[1] != nullptr) |
| 229 | + { |
| 230 | + node = assignment->children[1]; |
| 231 | + } |
| 232 | + if( node == nullptr || |
| 233 | + node->type != BINARY_OPERATION || |
| 234 | + !( node->types.operation.op == ADD || |
| 235 | + node->types.operation.op == MINUS || |
| 236 | + node->types.operation.op == MULTIPLY || |
| 237 | + node->types.operation.op == DIVIDE) || |
| 238 | + node->num_children != 2 || |
| 239 | + node->children[1] == nullptr || |
| 240 | + node->children[1]->type != NUMBERS || |
| 241 | + node->children[0] == nullptr || |
| 242 | + node->children[0]->type != IDENTIFIERS || |
| 243 | + strcmp(node->children[0]->types.identifier, symbol->types.identifier)) |
| 244 | + { |
| 245 | + *error_code = UNSUPPORTED_POST_CONDITION_NODE; |
| 246 | + return nullptr; |
| 247 | + } |
| 248 | + *error_code = 0; |
| 249 | + return [=](long long value) { |
| 250 | + switch(node->types.operation.op){ |
| 251 | + case ADD: |
| 252 | + return value + node->children[1]->types.number.value; |
| 253 | + case MINUS: |
| 254 | + return value - node->children[1]->types.number.value; |
| 255 | + case MULTIPLY: |
| 256 | + return value * node->children[1]->types.number.value; |
| 257 | + case DIVIDE: |
| 258 | + return value / node->children[1]->types.number.value; |
| 259 | + default: |
| 260 | + return 0ll; |
| 261 | + } |
| 262 | + }; |
| 263 | +} |
| 264 | + |
| 265 | +ast_node_t* dup_and_fill_body(ast_node_t* body, ast_node_t* symbol, ast_node_t** value, int* error_code) |
| 266 | +{ |
| 267 | + ast_node_t* copy = ast_node_deep_copy(body); |
| 268 | + for(int i = 0; i<copy->num_children; i++){ |
| 269 | + ast_node_t* child = copy->children[i]; |
| 270 | + oassert(child); |
| 271 | + if(child->type == IDENTIFIERS){ |
| 272 | + if(!strcmp(child->types.identifier, symbol->types.identifier)){ |
| 273 | + ast_node_t* new_num = ast_node_deep_copy(*value); |
| 274 | + free_whole_tree(child); |
| 275 | + copy->children[i] = new_num; |
| 276 | + } |
| 277 | + } else if(child->num_children > 0){ |
| 278 | + copy->children[i] = dup_and_fill_body(child, symbol, value, error_code); |
| 279 | + oassert(copy->children[i]); |
| 280 | + free_whole_tree(child); |
| 281 | + } |
| 282 | + } |
| 283 | + return copy; |
| 284 | +} |
0 commit comments