symbol_table.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  1. /*
  2. * Andrea Di Biagio
  3. * Politecnico di Milano, 2007
  4. *
  5. * symbol_table.c
  6. * Formal Languages & Compilers Machine, 2007/2008
  7. *
  8. */
  9. #include <stdlib.h>
  10. #include <assert.h>
  11. #include "symbol_table.h"
  12. #include "axe_debug.h"
  13. #include "collections.h"
  14. #include "axe_labels.h"
  15. /* symbol table */
  16. struct t_symbol_table
  17. {
  18. t_list *symbols;
  19. };
  20. static int symCompare (void *symA, void *symB);
  21. static int regCompare (void *symA, void *symB);
  22. static t_symbol * getSymFromID(t_symbol_table *table, char *ID);
  23. static t_symbol * getSymFromLocation(t_symbol_table *table, int location);
  24. char * getIDfromLocation(t_symbol_table *table, int location, int *errorcode)
  25. {
  26. t_symbol *sym;
  27. /* preconditions */
  28. if (table == NULL) {
  29. if (errorcode != NULL)
  30. (* errorcode) = SY_TABLE_NOT_INITIALIZED;
  31. return NULL;
  32. }
  33. if (location == REG_INVALID) {
  34. if (errorcode != NULL)
  35. (* errorcode) = SY_INVALID_REQUEST;
  36. return NULL;
  37. }
  38. /* retrieve the symbol from the location info. */
  39. sym = getSymFromLocation(table, location);
  40. if (sym == NULL)
  41. return NULL;
  42. /* postconditions */
  43. assert(sym->ID != NULL);
  44. return sym->ID;
  45. }
  46. int getTypeFromID(t_symbol_table *table, char *ID, int type)
  47. {
  48. t_symbol *found;
  49. /* test the preconditions */
  50. if (ID == NULL)
  51. return SY_LOCATION_UNSPECIFIED;
  52. /* search for the symbol with the given ID */
  53. found = getSymFromID(table, ID);
  54. /* test the postconditions */
  55. if (found == NULL)
  56. return SY_LOCATION_UNSPECIFIED;
  57. /* return the value of the `reg_location' field for the found symbol */
  58. return found->reg_location;
  59. }
  60. int getLocation(t_symbol_table *table, char *ID, int *errorcode)
  61. {
  62. t_symbol *found;
  63. /* test the preconditions */
  64. if (ID == NULL)
  65. {
  66. if (errorcode != NULL)
  67. {
  68. (* errorcode) = SY_INVALID_REQUEST;
  69. return SY_LOCATION_UNSPECIFIED;
  70. }
  71. }
  72. /* search for the symbol with the given ID */
  73. found = getSymFromID(table, ID);
  74. /* test the postconditions */
  75. if (found == NULL)
  76. {
  77. if (errorcode != NULL)
  78. {
  79. (* errorcode) = SY_UNDEFINED;
  80. return SY_LOCATION_UNSPECIFIED;
  81. }
  82. }
  83. if (errorcode != NULL)
  84. (* errorcode) = SY_TABLE_OK;
  85. /* return the value of the `reg_location' field for the found symbol */
  86. return found->reg_location;
  87. }
  88. int setLocation(t_symbol_table *table, char *ID, int reg)
  89. {
  90. t_symbol *found;
  91. /* test the preconditions */
  92. if (ID == NULL)
  93. return SY_INVALID_REQUEST;
  94. /* search for the symbol with the given ID */
  95. found = getSymFromID(table, ID);
  96. /* test the postconditions */
  97. if (found == NULL)
  98. return SY_UNDEFINED;
  99. /* set the `reg_location' field for the found symbol */
  100. found->reg_location = reg;
  101. return SY_TABLE_OK;
  102. }
  103. /* initialize the symbol table */
  104. t_symbol_table * initialize_sy_table()
  105. {
  106. t_symbol_table *result;
  107. /* create an instance of `t_symbol_tabel' */
  108. result = (t_symbol_table *) malloc(sizeof(t_symbol_table));
  109. if (result == NULL)
  110. return NULL;
  111. /* initialize the internal data associated with the symbol table */
  112. result->symbols = NULL;
  113. /* return the symbol table */
  114. return result;
  115. }
  116. int finalize_sy_table(t_symbol_table *table)
  117. {
  118. t_list *current_symbol;
  119. if (table == NULL)
  120. return SY_TABLE_NOT_INITIALIZED;
  121. /* initialize the value of current_symbol */
  122. current_symbol = table->symbols;
  123. while (current_symbol != NULL)
  124. {
  125. /* free the symbol */
  126. free(LDATA(current_symbol));
  127. /* select the new symbol */
  128. current_symbol = LNEXT(current_symbol);
  129. }
  130. /* deallocate memory for the sy_table */
  131. freeList(table->symbols);
  132. /* update the global variable sy_table */
  133. table->symbols = NULL;
  134. /* free the memory slot associated with the symbol table */
  135. free(table);
  136. return SY_TABLE_OK;
  137. }
  138. static int regCompare (void *symA, void *symB)
  139. {
  140. t_symbol *sA;
  141. t_symbol *sB;
  142. /* preconditions */
  143. if (symA == NULL)
  144. {
  145. if (symB == NULL)
  146. return 1;
  147. return 0;
  148. }
  149. if (symB == NULL)
  150. return 0;
  151. sA = (t_symbol *) symA;
  152. sB = (t_symbol *) symB;
  153. return (sA->reg_location == sB->reg_location);
  154. }
  155. /* Function used when a compare is needed between two labels */
  156. int symCompare (void *symA, void *symB)
  157. {
  158. t_symbol *sA;
  159. t_symbol *sB;
  160. /* preconditions */
  161. if (symA == NULL)
  162. {
  163. if (symB == NULL)
  164. return 1;
  165. return 0;
  166. }
  167. if (symB == NULL)
  168. return 0;
  169. sA = (t_symbol *) symA;
  170. sB = (t_symbol *) symB;
  171. assert(sA->ID != NULL);
  172. assert(sB->ID != NULL);
  173. return (!strcmp(sA->ID, sB->ID));
  174. }
  175. /* put a symbol into the symbol table */
  176. int putSym(t_symbol_table *table, char *ID, int type)
  177. {
  178. t_symbol pattern;
  179. t_symbol *new_symbol;
  180. if (table == NULL)
  181. return SY_TABLE_NOT_INITIALIZED;
  182. if (table->symbols == NULL)
  183. {
  184. /* initialize pattern */
  185. pattern.ID = ID;
  186. /* verify if the symbol is valid */
  187. if ( CustomfindElement(table->symbols
  188. , &pattern, symCompare) != NULL)
  189. {
  190. /* symbol already defined */
  191. return SY_ALREADY_DEFINED;
  192. }
  193. }
  194. /* add the new symbol to the symbol table */
  195. new_symbol = (t_symbol *) malloc(sizeof(t_symbol));
  196. /* verify if new_symbol is a valid pointer */
  197. if (new_symbol == NULL)
  198. {
  199. /* out of memory error */
  200. return SY_MEMALLOC_ERROR;
  201. }
  202. /* initialize the new symbol */
  203. new_symbol->ID = ID;
  204. new_symbol->type = type;
  205. new_symbol->reg_location = SY_LOCATION_UNSPECIFIED;
  206. /* add the new symbol to the symbol table */
  207. table->symbols = addElement(table->symbols, new_symbol, -1);
  208. return SY_TABLE_OK;
  209. }
  210. t_symbol * getSymFromLocation(t_symbol_table *table, int location)
  211. {
  212. t_symbol pattern;
  213. t_symbol *symbol_found;
  214. t_list *l_element;
  215. /* preconditions */
  216. if (table == NULL)
  217. return NULL;
  218. /* initialize pattern */
  219. pattern.reg_location = location;
  220. /* search for a symbol with the given ID */
  221. l_element = CustomfindElement(table->symbols, &pattern, regCompare);
  222. /* postconditions */
  223. if (l_element == NULL)
  224. return NULL;
  225. /* retrieve the symbol information */
  226. symbol_found = (t_symbol *) LDATA(l_element);
  227. /* return the symbol */
  228. return symbol_found;
  229. }
  230. /* retrieve informations about a symbol */
  231. t_symbol * getSymFromID(t_symbol_table *table, char *ID)
  232. {
  233. t_symbol pattern;
  234. t_symbol *symbol_found;
  235. t_list *l_element;
  236. /* preconditions */
  237. if (table == NULL)
  238. return NULL;
  239. /* initialize pattern */
  240. pattern.ID = ID;
  241. /* search for a symbol with the given ID */
  242. l_element = CustomfindElement(table->symbols, &pattern, symCompare);
  243. /* postconditions */
  244. if (l_element == NULL)
  245. return NULL;
  246. /* retrieve the symbol information */
  247. symbol_found = (t_symbol *) LDATA(l_element);
  248. /* return the symbol */
  249. return symbol_found;
  250. }
  251. #ifndef NDEBUG
  252. /* This function print out to the file `fout' the content of the
  253. * symbol table given as input. The resulting text is formatted in
  254. * the following way: <ID> -- <TYPE> -- <REGISTER> */
  255. void printSymbolTable(t_symbol_table *table, FILE *fout)
  256. {
  257. t_list *current_element;
  258. t_symbol *current_symbol;
  259. /* preconditions */
  260. if (table == NULL)
  261. return;
  262. if (fout == NULL)
  263. fout = stdin;
  264. fprintf(fout, "--------------------------------\n");
  265. fprintf(fout, " SYMBOL TABLE\n");
  266. fprintf(fout, "--------------------------------\n");
  267. fprintf(fout, "NUMBER OF SYMBOLS : %d \n"
  268. , getLength(table->symbols));
  269. fprintf(fout, "--------------------------------\n\n");
  270. /* initialize the value of current_symbol */
  271. current_element = table->symbols;
  272. while (current_element != NULL)
  273. {
  274. current_symbol = (t_symbol *) LDATA(current_element);
  275. fprintf(fout, "ID : %s\t;; TYPE : %s\t;;", current_symbol->ID
  276. , dataTypeToString(current_symbol->type) );
  277. if (current_symbol->reg_location == SY_LOCATION_UNSPECIFIED)
  278. fprintf(fout, " LOCATION : [UNSPECIFIED] \n");
  279. else
  280. fprintf(fout, " LOCATION : R%d \n", current_symbol->reg_location);
  281. /* select the new symbol */
  282. current_element = LNEXT(current_element);
  283. }
  284. }
  285. #endif