00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025 #include <string.h>
00026 #include <stdio.h>
00027 #include <stdlib.h>
00028 #include <ctype.h>
00029 #include <stdarg.h>
00030 #include <math.h>
00031 #include "globals.h"
00032 #include "types.h"
00033 #include "errors.h"
00034 #include "ast_util.h"
00035 #include "odin_util.h"
00036 #include "util.h"
00037
00038
00039
00040
00041 ast_node_t* create_node_w_type(ids id, int line_number, int file_number)
00042 {
00043 static long unique_count = 0;
00044
00045
00046
00047 ast_node_t* new_node;
00048
00049 new_node = (ast_node_t*)calloc(1, sizeof(ast_node_t));
00050 oassert(new_node != NULL);
00051 new_node->type = id;
00052
00053 new_node->children = NULL;
00054 new_node->num_children = 0;
00055
00056 new_node->line_number = line_number;
00057 new_node->file_number = file_number;
00058 new_node->unique_count = unique_count++;
00059
00060 new_node->far_tag = 0;
00061 new_node->high_number = 0;
00062
00063 return new_node;
00064 }
00065
00066
00067
00068
00069
00070 void free_child_in_tree(ast_node_t *from, int idx_removal)
00071 {
00072 ast_node_t *child = from->children[idx_removal];
00073 int i;
00074
00075 if ((child == NULL) || (child->shared_node == TRUE))
00076 return;
00077
00078
00079 for (i = 0; i < child->num_children; i++)
00080 {
00081 free_child_in_tree(child->children[i], i);
00082 }
00083
00084 if (child->children != NULL)
00085 free(child->children);
00086
00087 switch(child->type)
00088 {
00089 case IDENTIFIERS:
00090 if (child->types.identifier != NULL)
00091 free(child->types.identifier);
00092 break;
00093 case NUMBERS:
00094 if (child->types.number.number != NULL)
00095 free(child->types.number.number);
00096 break;
00097 default:
00098 break;
00099 }
00100
00101 free(child);
00102 from->children[idx_removal] = NULL;
00103 }
00104
00105
00106
00107
00108 void free_ast_node(ast_node_t *child)
00109 {
00110 int i;
00111
00112 if ((child == NULL) || (child->shared_node == TRUE))
00113 return;
00114
00115
00116 for (i = 0; i < child->num_children; i++)
00117 {
00118 free_child_in_tree(child, i);
00119 }
00120
00121 if (child->children != NULL)
00122 free(child->children);
00123
00124 switch(child->type)
00125 {
00126 case IDENTIFIERS:
00127 if (child->types.identifier != NULL)
00128 free(child->types.identifier);
00129 break;
00130 case NUMBERS:
00131 if (child->types.number.number != NULL)
00132 free(child->types.number.number);
00133 break;
00134 default:
00135 break;
00136 }
00137
00138 free(child);
00139 }
00140
00141
00142
00143
00144 void free_ast_node_only(ast_node_t *child)
00145 {
00146 if (child->children != NULL)
00147 free(child->children);
00148
00149 switch(child->type)
00150 {
00151 case IDENTIFIERS:
00152 if (child->types.identifier != NULL)
00153 free(child->types.identifier);
00154 break;
00155 case NUMBERS:
00156 if (child->types.number.number != NULL)
00157 free(child->types.number.number);
00158 break;
00159 default:
00160 break;
00161 }
00162
00163 free(child);
00164 }
00165
00166
00167
00168
00169 ast_node_t* create_tree_node_id(char* string, int line_number, int file_number)
00170 {
00171 ast_node_t* new_node = create_node_w_type(IDENTIFIERS, line_number, current_parse_file);
00172 new_node->types.identifier = string;
00173
00174 return new_node;
00175 }
00176
00177
00178
00179
00180 ast_node_t *create_tree_node_long_long_number(long long number, int line_number, int file_number)
00181 {
00182 ast_node_t* new_node = create_node_w_type(NUMBERS, line_number, current_parse_file);
00183 new_node->types.number.base = LONG_LONG;
00184 new_node->types.number.value = number;
00185
00186 return new_node;
00187 }
00188
00189
00190
00191
00192 ast_node_t *create_tree_node_number(char* number, int line_number, int file_number)
00193 {
00194 ast_node_t* new_node = create_node_w_type(NUMBERS, line_number, current_parse_file);
00195 char *string_pointer = number;
00196 int index_string_pointer = 0;
00197 char *temp_string;
00198 short flag_constant_decimal = FALSE;
00199
00200
00201 for (string_pointer=number; *string_pointer; string_pointer++)
00202 {
00203 if (*string_pointer == '\'')
00204 {
00205 break;
00206 }
00207 index_string_pointer++;
00208 }
00209
00210 if (index_string_pointer == strlen(number))
00211 {
00212 flag_constant_decimal = TRUE;
00213
00214 new_node->types.number.base = DEC;
00215
00216 string_pointer = number;
00217
00218
00219 new_node->types.number.size = strlen((string_pointer));
00220
00221 new_node->types.number.number = strdup((string_pointer));
00222 }
00223 else
00224 {
00225
00226 switch(tolower(*(string_pointer+1)))
00227 {
00228 case 'd':
00229 new_node->types.number.base = DEC;
00230 break;
00231 case 'h':
00232 new_node->types.number.base = HEX;
00233 break;
00234 case 'o':
00235 new_node->types.number.base = OCT;
00236 break;
00237 case 'b':
00238 new_node->types.number.base = BIN;
00239 break;
00240 default:
00241 printf("Not a number\n");
00242 oassert(FALSE);
00243 }
00244
00245
00246 temp_string = strdup(number);
00247 temp_string[index_string_pointer] = '\0';
00248
00249 new_node->types.number.size = atoi(temp_string);
00250 free(temp_string);
00251
00252
00253 string_pointer += 2;
00254
00255
00256 new_node->types.number.number = strdup((string_pointer));
00257 }
00258
00259
00260 if (flag_constant_decimal == FALSE)
00261 {
00262
00263 new_node->types.number.binary_size = new_node->types.number.size;
00264 }
00265 else
00266 {
00267
00268 if (strcmp(new_node->types.number.number, "0") != 0)
00269 {
00270 new_node->types.number.binary_size = ceil((log(convert_dec_string_of_size_to_long(new_node->types.number.number, new_node->types.number.size)+1))/log(2));
00271 }
00272 else
00273 {
00274 new_node->types.number.binary_size = 1;
00275 }
00276 }
00277
00278 switch (new_node->types.number.base)
00279 {
00280 case(DEC):
00281 new_node->types.number.value = convert_dec_string_of_size_to_long(new_node->types.number.number, new_node->types.number.size);
00282 new_node->types.number.binary_string = convert_long_to_bit_string(new_node->types.number.value, new_node->types.number.binary_size);
00283 break;
00284 case(HEX):
00285 new_node->types.number.value = convert_hex_string_of_size_to_long(new_node->types.number.number, strlen(new_node->types.number.number));
00286 new_node->types.number.binary_string = convert_long_to_bit_string(new_node->types.number.value, new_node->types.number.binary_size);
00287 break;
00288 case(OCT):
00289 new_node->types.number.value = convert_oct_string_of_size_to_long(new_node->types.number.number, strlen(new_node->types.number.number));
00290 new_node->types.number.binary_string = convert_long_to_bit_string(new_node->types.number.value, new_node->types.number.binary_size);
00291 break;
00292 case(BIN):
00293 new_node->types.number.value = convert_binary_string_of_size_to_long(new_node->types.number.number, strlen(new_node->types.number.number));
00294 new_node->types.number.binary_string = convert_long_to_bit_string(new_node->types.number.value, new_node->types.number.binary_size);
00295 break;
00296 }
00297
00298 return new_node;
00299 }
00300
00301
00302
00303
00304 void allocate_children_to_node(ast_node_t* node, int num_children, ...)
00305 {
00306 va_list ap;
00307 int i;
00308
00309
00310 node->children = (ast_node_t**)malloc(sizeof(ast_node_t*)*num_children);
00311 node->num_children = num_children;
00312
00313
00314 va_start(ap, num_children);
00315
00316 for (i = 0; i < num_children; i++)
00317 {
00318 node->children[i] = va_arg(ap, ast_node_t*);
00319 }
00320 }
00321
00322
00323
00324
00325 void add_child_to_node(ast_node_t* node, ast_node_t *child)
00326 {
00327
00328 node->children = (ast_node_t**)realloc(node->children, sizeof(ast_node_t*)*(node->num_children+1));
00329 node->num_children ++;
00330 node->children[node->num_children-1] = child;
00331 }
00332
00333
00334
00335
00336 int get_range(ast_node_t* first_node)
00337 {
00338 long temp_value;
00339
00340 if (first_node->children[1] != NULL)
00341 {
00342
00343 oassert(first_node->children[2] != NULL);
00344 oassert((first_node->children[1]->type == NUMBERS) && (first_node->children[2]->type == NUMBERS));
00345 if(first_node->children[1]->types.number.value < first_node->children[2]->types.number.value)
00346 {
00347
00348 temp_value = first_node->children[1]->types.number.value;
00349 first_node->children[1]->types.number.value = first_node->children[2]->types.number.value;
00350 first_node->children[2]->types.number.value = temp_value;
00351 }
00352
00353 return abs(first_node->children[1]->types.number.value - first_node->children[2]->types.number.value + 1);
00354 }
00355 return -1;
00356 }
00357
00358
00359
00360
00361
00362 void make_concat_into_list_of_strings(ast_node_t *concat_top)
00363 {
00364 int i, j;
00365
00366 concat_top->types.concat.num_bit_strings = 0;
00367 concat_top->types.concat.bit_strings = NULL;
00368
00369
00370 for (i = 0; i < concat_top->num_children; i++)
00371 {
00372 if (concat_top->children[i]->type == CONCATENATE)
00373 {
00374 make_concat_into_list_of_strings(concat_top->children[i]);
00375 }
00376 }
00377
00378 for (i = 0; i < concat_top->num_children; i++)
00379 {
00380 if (concat_top->children[i]->type == IDENTIFIERS)
00381 {
00382 char *temp_string = make_full_ref_name(NULL, NULL, NULL, concat_top->children[i]->types.identifier, -1);
00383 long sc_spot;
00384
00385 if ((sc_spot = sc_lookup_string(local_symbol_table_sc, temp_string)) == -1)
00386 {
00387 error_message(NETLIST_ERROR, concat_top->line_number, concat_top->file_number, "Missing declaration of this symbol %s\n", temp_string);
00388 }
00389 free(temp_string);
00390
00391 if (((ast_node_t*)local_symbol_table_sc->data[sc_spot])->children[1] == NULL)
00392 {
00393 concat_top->types.concat.num_bit_strings ++;
00394 concat_top->types.concat.bit_strings = (char**)realloc(concat_top->types.concat.bit_strings, sizeof(char*)*(concat_top->types.concat.num_bit_strings));
00395 concat_top->types.concat.bit_strings[concat_top->types.concat.num_bit_strings-1] = get_name_of_pin_at_bit(concat_top->children[i], -1);
00396 }
00397 else if (((ast_node_t*)local_symbol_table_sc->data[sc_spot])->children[3] == NULL)
00398 {
00399
00400 for (j = ((ast_node_t*)local_symbol_table_sc->data[sc_spot])->children[1]->types.number.value - ((ast_node_t*)local_symbol_table_sc->data[sc_spot])->children[2]->types.number.value; j >= 0; j--)
00401 {
00402 concat_top->types.concat.num_bit_strings ++;
00403 concat_top->types.concat.bit_strings = (char**)realloc(concat_top->types.concat.bit_strings, sizeof(char*)*(concat_top->types.concat.num_bit_strings));
00404 concat_top->types.concat.bit_strings[concat_top->types.concat.num_bit_strings-1] = get_name_of_pin_at_bit(concat_top->children[i], j);
00405 }
00406 }
00407 else if (((ast_node_t*)local_symbol_table_sc->data[sc_spot])->children[3] != NULL)
00408 {
00409 oassert(FALSE);
00410 }
00411
00412 }
00413 else if (concat_top->children[i]->type == ARRAY_REF)
00414 {
00415 concat_top->types.concat.num_bit_strings ++;
00416 concat_top->types.concat.bit_strings = (char**)realloc(concat_top->types.concat.bit_strings, sizeof(char*)*(concat_top->types.concat.num_bit_strings));
00417 concat_top->types.concat.bit_strings[concat_top->types.concat.num_bit_strings-1] = get_name_of_pin_at_bit(concat_top->children[i], 0);
00418 }
00419 else if (concat_top->children[i]->type == RANGE_REF)
00420 {
00421 oassert(concat_top->children[i]->children[1]->types.number.value >= concat_top->children[i]->children[2]->types.number.value);
00422
00423 for (j = concat_top->children[i]->children[1]->types.number.value - concat_top->children[i]->children[2]->types.number.value; j >= 0; j--)
00424 {
00425 concat_top->types.concat.num_bit_strings ++;
00426 concat_top->types.concat.bit_strings = (char**)realloc(concat_top->types.concat.bit_strings, sizeof(char*)*(concat_top->types.concat.num_bit_strings));
00427 concat_top->types.concat.bit_strings[concat_top->types.concat.num_bit_strings-1] = get_name_of_pin_at_bit(concat_top->children[i], ((concat_top->children[i]->children[1]->types.number.value - concat_top->children[i]->children[2]->types.number.value))-j);
00428 }
00429 }
00430 else if (concat_top->children[i]->type == NUMBERS)
00431 {
00432 if(concat_top->children[i]->types.number.base == DEC)
00433 {
00434 error_message(NETLIST_ERROR, concat_top->line_number, concat_top->file_number, "Concatenation can't include decimal numbers due to conflict on bits\n");
00435 }
00436
00437
00438 for (j = 0; j < concat_top->children[i]->types.number.binary_size; j++)
00439 {
00440 concat_top->types.concat.num_bit_strings ++;
00441 concat_top->types.concat.bit_strings = (char**)realloc(concat_top->types.concat.bit_strings, sizeof(char*)*(concat_top->types.concat.num_bit_strings));
00442 concat_top->types.concat.bit_strings[concat_top->types.concat.num_bit_strings-1] = get_name_of_pin_at_bit(concat_top->children[i], j);
00443 }
00444 }
00445 else if (concat_top->children[i]->type == CONCATENATE)
00446 {
00447
00448 for (j = 0; j < concat_top->children[i]->types.concat.num_bit_strings; j++)
00449 {
00450 concat_top->types.concat.num_bit_strings ++;
00451 concat_top->types.concat.bit_strings = (char**)realloc(concat_top->types.concat.bit_strings, sizeof(char*)*(concat_top->types.concat.num_bit_strings));
00452 concat_top->types.concat.bit_strings[concat_top->types.concat.num_bit_strings-1] = get_name_of_pin_at_bit(concat_top->children[i], j);
00453 }
00454 }
00455 }
00456 }
00457
00458
00459
00460
00461
00462 char *get_name_of_var_declare_at_bit(ast_node_t *var_declare, int bit)
00463 {
00464 char *return_string;
00465
00466
00467 if (var_declare->children[1] == NULL)
00468 {
00469 oassert(bit == 0);
00470 return_string = make_full_ref_name(NULL, NULL, NULL, var_declare->children[0]->types.identifier, -1);
00471 }
00472 else if (var_declare->children[3] == NULL)
00473 {
00474 return_string = make_full_ref_name(NULL, NULL, NULL, var_declare->children[0]->types.identifier, var_declare->children[2]->types.number.value+bit);
00475 }
00476 else if (var_declare->children[3] != NULL)
00477 {
00478
00479 oassert(FALSE);
00480 }
00481
00482 return return_string;
00483 }
00484
00485
00486
00487
00488
00489 char *get_name_of_pin_at_bit(ast_node_t *var_node, int bit)
00490 {
00491 char *return_string;
00492
00493 if (var_node->type == ARRAY_REF)
00494 {
00495 return_string = make_full_ref_name(NULL, NULL, NULL, var_node->children[0]->types.identifier, (int)var_node->children[1]->types.number.value);
00496 }
00497 else if (var_node->type == RANGE_REF)
00498 {
00499 oassert((var_node->children[2]->types.number.value+bit <= var_node->children[1]->types.number.value) && (var_node->children[2]->types.number.value+bit >= var_node->children[2]->types.number.value));
00500 return_string = make_full_ref_name(NULL, NULL, NULL, var_node->children[0]->types.identifier, var_node->children[2]->types.number.value+bit);
00501 }
00502 else if ((var_node->type == IDENTIFIERS) && (bit == -1))
00503 {
00504 return_string = make_full_ref_name(NULL, NULL, NULL, var_node->types.identifier, -1);
00505 }
00506 else if (var_node->type == IDENTIFIERS)
00507 {
00508 long sc_spot;
00509 int pin_index;
00510
00511 if ((sc_spot = sc_lookup_string(local_symbol_table_sc, var_node->types.identifier)) == -1)
00512 {
00513 error_message(NETLIST_ERROR, var_node->line_number, var_node->file_number, "Missing declaration of this symbol %s\n", var_node->types.identifier);
00514 }
00515
00516 if (((ast_node_t*)local_symbol_table_sc->data[sc_spot])->children[1] == NULL)
00517 {
00518 pin_index = bit;
00519 }
00520 else if (((ast_node_t*)local_symbol_table_sc->data[sc_spot])->children[3] == NULL)
00521 {
00522 pin_index = ((ast_node_t*)local_symbol_table_sc->data[sc_spot])->children[2]->types.number.value + bit;
00523 }
00524 else
00525 oassert(FALSE);
00526
00527 return_string = make_full_ref_name(NULL, NULL, NULL, var_node->types.identifier, pin_index);
00528 }
00529 else if (var_node->type == NUMBERS)
00530 {
00531 if (bit == -1)
00532 bit = 0;
00533
00534 oassert(bit < var_node->types.number.binary_size);
00535 if (var_node->types.number.binary_string[var_node->types.number.binary_size-bit-1] == '1')
00536 {
00537 return_string = (char*)malloc(sizeof(char)*11+1);
00538 sprintf(return_string, "ONE_VCC_CNS");
00539 }
00540 else if (var_node->types.number.binary_string[var_node->types.number.binary_size-bit-1] == '0')
00541 {
00542 return_string = (char*)malloc(sizeof(char)*13+1);
00543 sprintf(return_string, "ZERO_GND_ZERO");
00544 }
00545 else
00546 {
00547 oassert(FALSE);
00548 }
00549 }
00550 else if (var_node->type == CONCATENATE)
00551 {
00552 if (var_node->types.concat.num_bit_strings == 0)
00553 {
00554 oassert(FALSE);
00555 }
00556 else
00557 {
00558 if (var_node->types.concat.num_bit_strings == -1)
00559 {
00560
00561 make_concat_into_list_of_strings(var_node);
00562 }
00563
00564 return_string = (char*)malloc(sizeof(char)*strlen(var_node->types.concat.bit_strings[bit])+1);
00565 sprintf(return_string, "%s", var_node->types.concat.bit_strings[bit]);
00566 }
00567 }
00568 else
00569 {
00570 oassert(FALSE);
00571 }
00572
00573 return return_string;
00574 }
00575
00576
00577
00578
00579
00580
00581 char_list_t *get_name_of_pins(ast_node_t *var_node)
00582 {
00583 char **return_string;
00584 char_list_t *return_list = (char_list_t*)malloc(sizeof(char_list_t));
00585
00586 int i;
00587 int width;
00588
00589 if (var_node->type == ARRAY_REF)
00590 {
00591 width = 1;
00592 return_string = (char**)malloc(sizeof(char*));
00593 return_string[0] = make_full_ref_name(NULL, NULL, NULL, var_node->children[0]->types.identifier, (int)var_node->children[1]->types.number.value);
00594 }
00595 else if (var_node->type == RANGE_REF)
00596 {
00597 width = (var_node->children[1]->types.number.value - var_node->children[2]->types.number.value+1);
00598 return_string = (char**)malloc(sizeof(char*)*width);
00599 for (i = 0; i < width; i++)
00600 {
00601 return_string[i] = make_full_ref_name(NULL, NULL, NULL, var_node->children[0]->types.identifier, var_node->children[2]->types.number.value+i);
00602 }
00603 }
00604 else if (var_node->type == IDENTIFIERS)
00605 {
00606
00607 long sc_spot;
00608 char *temp_string = make_full_ref_name(NULL, NULL, NULL, var_node->types.identifier, -1);
00609
00610 if ((sc_spot = sc_lookup_string(local_symbol_table_sc, temp_string)) == -1)
00611 {
00612 error_message(NETLIST_ERROR, var_node->line_number, var_node->file_number, "Missing declaration of this symbol %s\n", temp_string);
00613 }
00614 free(temp_string);
00615
00616 if (((ast_node_t*)local_symbol_table_sc->data[sc_spot])->children[1] == NULL)
00617 {
00618 width = 1;
00619 return_string = (char**)malloc(sizeof(char*)*width);
00620 return_string[0] = make_full_ref_name(NULL, NULL, NULL, var_node->types.identifier, -1);
00621 }
00622 else if (((ast_node_t*)local_symbol_table_sc->data[sc_spot])->children[3] == NULL)
00623 {
00624 int index = 0;
00625 width = ((ast_node_t*)local_symbol_table_sc->data[sc_spot])->children[1]->types.number.value - ((ast_node_t*)local_symbol_table_sc->data[sc_spot])->children[2]->types.number.value + 1;
00626 return_string = (char**)malloc(sizeof(char*)*width);
00627 for (i = 0; i < width; i++)
00628
00629 {
00630 return_string[index] = make_full_ref_name(NULL, NULL, NULL, var_node->types.identifier, i+((ast_node_t*)local_symbol_table_sc->data[sc_spot])->children[2]->types.number.value);
00631 index++;
00632 }
00633 }
00634 else if (((ast_node_t*)local_symbol_table_sc->data[sc_spot])->children[3] != NULL)
00635 {
00636 oassert(FALSE);
00637 }
00638 }
00639 else if (var_node->type == NUMBERS)
00640 {
00641 width = var_node->types.number.binary_size;
00642 return_string = (char**)malloc(sizeof(char*)*width);
00643 for (i = 0; i < width; i++)
00644 {
00645
00646 if (var_node->types.number.binary_string[var_node->types.number.binary_size-i-1] == '1')
00647 {
00648 return_string[i] = (char*)malloc(sizeof(char)*11+1);
00649 sprintf(return_string[i], "ONE_VCC_CNS");
00650 }
00651 else if (var_node->types.number.binary_string[var_node->types.number.binary_size-i-1] == '0')
00652 {
00653 return_string[i] = (char*)malloc(sizeof(char)*13+1);
00654 sprintf(return_string[i], "ZERO_GND_ZERO");
00655 }
00656 else
00657 {
00658 oassert(FALSE);
00659 }
00660 }
00661 }
00662 else if (var_node->type == CONCATENATE)
00663 {
00664 if (var_node->types.concat.num_bit_strings == 0)
00665 {
00666 oassert(FALSE);
00667 }
00668 else
00669 {
00670 if (var_node->types.concat.num_bit_strings == -1)
00671 {
00672
00673 make_concat_into_list_of_strings(var_node);
00674 }
00675
00676 width = var_node->types.concat.num_bit_strings;
00677 return_string = (char**)malloc(sizeof(char*)*width);
00678 for (i = 0; i < width; i++)
00679 {
00680 return_string[i] = (char*)malloc(sizeof(char)*strlen(var_node->types.concat.bit_strings[var_node->types.concat.num_bit_strings-i-1])+1);
00681 sprintf(return_string[i], "%s", var_node->types.concat.bit_strings[var_node->types.concat.num_bit_strings-i-1]);
00682 }
00683 }
00684 }
00685 else
00686 {
00687 oassert(FALSE);
00688 }
00689
00690 return_list->strings = return_string;
00691 return_list->num_strings = width;
00692
00693 return return_list;
00694 }
00695
00696
00697
00698
00699 char_list_t *get_name_of_pins_with_prefix(ast_node_t *var_node, char *instance_name_prefix)
00700 {
00701 int i;
00702 char_list_t *return_list;
00703
00704
00705 return_list = get_name_of_pins(var_node);
00706
00707 for (i = 0; i < return_list->num_strings; i++)
00708 {
00709 return_list->strings[i] = make_full_ref_name(instance_name_prefix, NULL, NULL, return_list->strings[i], -1);
00710 }
00711
00712 return return_list;
00713 }