PageRenderTime 47ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 1ms

/kernel/symbol_table.c

https://bitbucket.org/raghavanSanthanam/cgdl
C | 622 lines | 510 code | 90 blank | 22 comment | 113 complexity | f1379911ee294752228f7effb7ddd984 MD5 | raw file
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include "symbol_table.h"
  5. #define __DEBUG__
  6. #define NO_EXIT_ON_COMPILE_TIME_ERRORS
  7. #ifdef NO_EXIT_ON_COMPILE_TIME_ERRORS
  8. #define exit(exit_code)
  9. #endif // NO_EXIT_ON_COMPILE_TIME_ERRORS
  10. #ifdef __DEBUG__
  11. #define TOUCH() do {\
  12. printf("%s()\n", __func__);\
  13. } while ( 0 )
  14. #else
  15. #define TOUCH()
  16. #endif
  17. extern char *strdup(const char *s);
  18. extern bool comp_type_member_seen;
  19. extern bool comp_type_decln_seen;
  20. extern struct comp_type *last_seen_comp_type;
  21. extern char *src_file;
  22. extern int cur_comp_type_nesting_level;
  23. extern int active_block;
  24. extern bool last_member_is_comp_type;
  25. extern struct comp_type *last_seen_comp_type;
  26. struct comp_type_member *last_member;
  27. static struct comp_type_list *blocks_comp_type_lists[ EMax_nested_blocks +
  28. /* Global */1 ];
  29. static struct comp_type_list *comp_type_list_tails[ EMax_nested_blocks +
  30. /* Global */1 ];
  31. static struct symbol_list *blocks_symbol_lists[ EMax_nested_blocks +
  32. /* Global */1 ];
  33. static struct symbol_list *symbol_list_tails[ EMax_nested_blocks +
  34. /* Global */1 ];
  35. static struct func_type_list *global_func_type_list = NULL;
  36. static struct func_type_list *global_func_type_list_tail = NULL;
  37. static inline bool is_func_type_defined(struct func_type *f)
  38. {
  39. TOUCH();
  40. bool defined = false;
  41. struct func_type_list *t = global_func_type_list;
  42. while ( t ) {
  43. if ( !strcmp(t->f->name, f->name) ) {
  44. defined = true;
  45. }
  46. t = t->next;
  47. }
  48. return defined;
  49. }
  50. void display_func_types(void)
  51. {
  52. TOUCH();
  53. struct func_type_list *l = global_func_type_list;
  54. printf("Function types:\n");
  55. printf("- Return type - Name - Parameter list -\n");
  56. while ( l ) {
  57. struct func_type *f = l->f;
  58. printf("- %s - %s - %s -\n", f->ret_type, f->name,
  59. f->parameter_list);
  60. l = l->next;
  61. }
  62. }
  63. void add_func_type(struct func_type *f, unsigned long long int src_line)
  64. {
  65. TOUCH();
  66. static int t = 0;
  67. printf("%s called for %d time", __func__, ++t);
  68. if ( f ) {
  69. if ( !is_func_type_defined(f) ) {
  70. struct func_type_list *l = malloc(sizeof(*l));
  71. if ( l ) {
  72. l->f = f;
  73. l->next = NULL;
  74. }
  75. if ( global_func_type_list_tail ) {
  76. global_func_type_list_tail->next = l;
  77. } else {
  78. global_func_type_list = l;
  79. }
  80. global_func_type_list_tail = l;
  81. } else {
  82. struct term_or_nonterm tmp = { 0 };
  83. tmp.src_line = src_line; // Don't bother about other
  84. // fields.
  85. void *trans_node = &tmp; // To allow below macro
  86. // think it has a trans_node indeed
  87. // to extract the src_line from --
  88. // uniformity concern!
  89. COMPILATION_ERROR("Refinition of function %s\n",
  90. f->name);
  91. }
  92. }
  93. }
  94. void add_comp_type(char *t, unsigned long long int src_line)
  95. {
  96. TOUCH();
  97. if ( !t ) {
  98. return; // Huh, too much of indentation otherwise! :P
  99. }
  100. if ( cur_comp_type_nesting_level == EMax_comp_type_nesting_limit ) {
  101. struct term_or_nonterm tmp = { 0 };
  102. tmp.src_line = src_line; // Don't bother about other
  103. // fields.
  104. void *trans_node = &tmp; // To allow below macro
  105. // think it has a trans_node indeed
  106. // to extract the src_line from --
  107. // uniformity concern!
  108. COMPILATION_ERROR
  109. ("Sorry! Maximum nesting limit %d exceeded!\n",
  110. EMax_comp_type_nesting_limit);
  111. }
  112. if ( comp_type_decln_seen ) {
  113. if ( last_seen_comp_type ) {
  114. if ( !strcmp(last_seen_comp_type->tag, t) ) {
  115. struct term_or_nonterm tmp = { 0 };
  116. tmp.src_line = src_line; // Don't bother
  117. // about other fields.
  118. void *trans_node = &tmp; // To allow below macro
  119. // think it has a trans_node indeed
  120. // to extract the src_line from --
  121. // uniformity concern!
  122. COMPILATION_ERROR("%s already "
  123. "defined as %s\n",
  124. t, last_seen_comp_type->tag);
  125. }
  126. }
  127. } else {
  128. struct comp_type_list *l =
  129. blocks_comp_type_lists[ active_block ];
  130. while ( l ) {
  131. if ( !strcmp(l->c->tag, t) ) {
  132. struct term_or_nonterm tmp = { 0 };
  133. tmp.src_line = src_line; // Don't bother
  134. // about other fields.
  135. void *trans_node = &tmp; // To allow below macro
  136. // think it has a trans_node indeed
  137. // to extract the src_line from --
  138. // uniformity concern!
  139. COMPILATION_ERROR("%s already "
  140. "defined as %s\n", t,
  141. l->c->tag);
  142. }
  143. l = l->next;
  144. }
  145. }
  146. struct comp_type *c = malloc(sizeof(*c));
  147. if ( c ) {
  148. c->tag = strdup(t);
  149. c->m = NULL;
  150. c->inst = NULL;
  151. if ( !comp_type_member_seen) {
  152. struct comp_type_list *l = malloc(sizeof(*l));
  153. if ( l ) {
  154. l->c = c;
  155. l->next = NULL;
  156. if ( comp_type_list_tails[ active_block ] ) {
  157. comp_type_list_tails
  158. [ active_block ]->next = l;
  159. } else {
  160. blocks_comp_type_lists
  161. [ active_block ] = l;
  162. }
  163. comp_type_list_tails[ active_block ] = l;
  164. last_seen_comp_type = c;
  165. cur_comp_type_nesting_level++;
  166. } else {
  167. free(c->tag);
  168. c->tag = NULL;
  169. free(c);
  170. c = NULL;
  171. }
  172. } else {
  173. struct comp_type_member *m = malloc(sizeof(*m));
  174. if ( m ) {
  175. m->type = EComps;
  176. m->f.c = c;
  177. m->next = NULL;
  178. if ( last_member ) {
  179. last_member->next = m;
  180. } else {
  181. last_member = last_seen_comp_type->m = m;
  182. }
  183. cur_comp_type_nesting_level++;
  184. } else {
  185. free(c->tag);
  186. c->tag = NULL;
  187. free(c);
  188. c = NULL;
  189. }
  190. }
  191. }
  192. }
  193. inline bool is_var_defined(char *v)
  194. {
  195. TOUCH();
  196. bool defined = false;
  197. int i = active_block;
  198. while ( i >= 0 ) {
  199. struct symbol_list *l = blocks_symbol_lists[ i ];
  200. while ( l ) {
  201. if ( !strcmp(l->s->n, v) ) {
  202. defined = true;
  203. break;
  204. }
  205. l = l->next;
  206. }
  207. if ( defined ) {
  208. break;
  209. }
  210. i--;
  211. }
  212. return defined;
  213. }
  214. static inline bool is_var_defined_already_in_current_scope(char *v, char **e_t)
  215. {
  216. TOUCH();
  217. bool defined = false;
  218. struct symbol_list *l = blocks_symbol_lists[ active_block ];
  219. while ( l ) {
  220. if ( !strcmp(l->s->n, v) ) {
  221. *e_t = l->s->t;
  222. defined = true;
  223. break;
  224. }
  225. l = l->next;
  226. }
  227. return defined;
  228. }
  229. static void display_comp_type(struct comp_type *c, int p_tabs)
  230. {
  231. TOUCH();
  232. int n_tabs = 1;
  233. printf("record(%p) %s {\n", (void *)c, c->tag);
  234. struct comp_type_member *m = c->m;
  235. while ( m ) {
  236. if ( m->type == ESymbol ) {
  237. int t = n_tabs + p_tabs;
  238. while ( t-- ) {
  239. putchar('\t');
  240. }
  241. printf("%s %s;\n", m->f.s->t, m->f.s->n);
  242. } else if ( m->type == EComps ) {
  243. n_tabs++;
  244. int t = n_tabs + p_tabs;
  245. while ( t-- ) {
  246. putchar('\t');
  247. }
  248. display_comp_type(m->f.c, n_tabs);
  249. n_tabs--;
  250. }
  251. m = m->next;
  252. }
  253. printf("};\n");
  254. }
  255. void display_current_block_symbol_table(void)
  256. {
  257. TOUCH();
  258. printf("active_block : %d\n", active_block);
  259. struct symbol_list *l = blocks_symbol_lists[ active_block ];
  260. printf("l : %p\n", (void *)l);
  261. printf("Symbol list for the block : %d\n", active_block);
  262. printf("| type | name |\n");
  263. while ( l ) {
  264. printf("| %s | %s |\n", l->s->t, l->s->n);
  265. l = l->next;
  266. }
  267. printf("Comp type list for the block : %d\n", active_block);
  268. struct comp_type_list *c = blocks_comp_type_lists[ active_block ];
  269. if ( c ) {
  270. while ( c ) {
  271. int n_tabs = 0;
  272. display_comp_type(c->c, n_tabs);
  273. c = c->next;
  274. }
  275. }
  276. }
  277. void display_current_and_enlcosing_block_symbol_table(void)
  278. {
  279. TOUCH();
  280. int b = active_block;
  281. while ( b >= 0 ) {
  282. printf("Symbol list for the block : %d\n", b);
  283. printf("| type | name |\n");
  284. struct symbol_list *l = blocks_symbol_lists[ b ];
  285. while ( l ) {
  286. printf("| %s | %s |\n", l->s->t, l->s->n);
  287. l = l->next;
  288. }
  289. b--;
  290. }
  291. }
  292. void display_all_blocks_symbol_table(void)
  293. {
  294. TOUCH();
  295. int i = 0;
  296. while ( i < EMax_nested_blocks ) {
  297. printf("Symbol list for the block : %d\n", i);
  298. printf("| type | name |\n");
  299. struct symbol_list *l = blocks_symbol_lists[ i ];
  300. while ( l ) {
  301. printf("| %s | %s |\n", l->s->t, l->s->n);
  302. l = l->next;
  303. }
  304. i++;
  305. }
  306. }
  307. void free_symbol_list(int b)
  308. {
  309. TOUCH();
  310. if ( b >= 0 && b < EMax_nested_blocks ) {
  311. struct symbol_list *l = blocks_symbol_lists[ b ];
  312. while ( l ) {
  313. struct symbol_list *next = l->next;
  314. free(l->s);
  315. l->s = NULL;
  316. free(l);
  317. l = next;
  318. }
  319. blocks_symbol_lists[ b ] = NULL;
  320. symbol_list_tails[ b ] = NULL;
  321. }
  322. }
  323. void add_symbol(struct unified_type *t,
  324. unsigned long long int src_line)
  325. {
  326. TOUCH();
  327. if ( t ) {
  328. char *v = t->type == EComp ? t->u.c->tag : t->u.s->n;
  329. char *e_t = NULL;
  330. if ( is_var_defined_already_in_current_scope(v, &e_t) ) {
  331. struct term_or_nonterm tmp = { 0 };
  332. tmp.src_line = src_line; // Don't bother about other
  333. // fields.
  334. void *trans_node = &tmp; // To allow below macro
  335. // think it has a trans_node indeed
  336. // to extract the src_line from --
  337. // uniformity concern!
  338. COMPILATION_ERROR("%s already defined as %s\n", v, e_t);
  339. }
  340. if ( last_member_is_comp_type && last_seen_comp_type ) {
  341. last_seen_comp_type->inst = t->u.c->inst;
  342. return; // Duh, I am not gonna indent any more
  343. // deep! :P
  344. }
  345. struct symbol *sym = malloc(sizeof(*sym));
  346. if ( sym ) {
  347. char *n = t->type == EComp ? t->u.c->inst : t->u.s->n;
  348. strncpy(sym->n, n, ESignificant_identifier_len);
  349. printf("sym->n : %s\n", sym->n);
  350. sym->t = strdup(t->type == EComp ? t->u.c->tag : t->u.s->t);
  351. struct symbol_list *le = malloc(sizeof(*le));
  352. if ( !comp_type_decln_seen ) {
  353. if ( le ) {
  354. le->s = sym;
  355. le->next = NULL;
  356. if (symbol_list_tails
  357. [ active_block ] ) {
  358. symbol_list_tails
  359. [ active_block ]->next = le;
  360. printf("1: Symbol added!\n");
  361. printf("after adding - blocks_symbol_lists"
  362. "[ active_block ] : %p\n",
  363. (void *)blocks_symbol_lists
  364. [ active_block ]);
  365. } else {
  366. blocks_symbol_lists
  367. [ active_block ] = le;
  368. printf("2: Symbol added!\n");
  369. }
  370. symbol_list_tails
  371. [ active_block ] = le;
  372. } else {
  373. free(sym->t);
  374. sym->t = NULL;
  375. free(sym);
  376. sym = NULL;
  377. }
  378. } else {
  379. struct comp_type_member *m =
  380. malloc(sizeof(*m));
  381. if ( m ) {
  382. m->type = ESymbol;
  383. m->f.s = sym;
  384. m->next = NULL;
  385. if ( last_member ) {
  386. last_member->next = m;
  387. } else {
  388. last_member =
  389. last_seen_comp_type
  390. ->m = m;
  391. }
  392. last_member = m;
  393. } else {
  394. free(sym->t);
  395. sym->t = NULL;
  396. free(sym);
  397. sym = NULL;
  398. }
  399. }
  400. }
  401. }
  402. }
  403. // Intended for checking user defined type.
  404. inline bool is_type_defined(char *n)
  405. {
  406. TOUCH();
  407. bool defined = false;
  408. if ( n ) {
  409. struct comp_type_list *l =
  410. blocks_comp_type_lists[ active_block ];
  411. while ( l ) {
  412. if ( !strcmp(l->c->inst, n) ) {
  413. defined = true;
  414. break;
  415. }
  416. l = l->next;
  417. }
  418. }
  419. return defined;
  420. }
  421. inline bool is_comp_type_member_defined(char *m_name,
  422. struct comp_type *c)
  423. {
  424. TOUCH();
  425. if ( !m_name || !c ) {
  426. return false;
  427. }
  428. bool defined = false;
  429. struct comp_type_member *m = c->m;
  430. while ( m ) {
  431. if ( (m->f.s->n && !strcmp(m->f.s->n, m_name)) ||
  432. (m->f.c->inst && !strcmp(m->f.c->inst, m_name)) ) {
  433. defined = true;
  434. break;
  435. }
  436. m = m->next;
  437. }
  438. return defined;
  439. }
  440. struct unified_type *type_of(char *v, struct unified_type *parent)
  441. {
  442. TOUCH();
  443. if ( !v ) {
  444. return NULL;
  445. }
  446. struct unified_type *u = NULL;
  447. if ( !parent ) {
  448. bool defined = false;
  449. int i = active_block;
  450. while ( i >= 0 ) {
  451. struct symbol_list *l = blocks_symbol_lists[ i ];
  452. while ( l ) {
  453. printf("Block[ %d ] : %s\n", i, l->s->n);
  454. if ( !strcmp(l->s->n, v) ) {
  455. u = malloc(sizeof(*u));
  456. if ( u ) {
  457. u->type = EBasic;
  458. u->u.s = l->s;
  459. }
  460. defined = true;
  461. break;
  462. }
  463. l = l->next;
  464. }
  465. if ( defined ) {
  466. break;
  467. }
  468. i--;
  469. }
  470. } else {
  471. if ( parent->type == EComp ) {
  472. struct comp_type_member *m = parent->u.c->m;
  473. while ( m ) {
  474. if ( m->type == EComp &&
  475. !strcmp(m->f.c->inst, v) ) {
  476. u = malloc(sizeof(*u));
  477. if ( u ) {
  478. u->type = EComp;
  479. u->u.c = m->f.c;
  480. }
  481. } else if ( m->type == EBasic &&
  482. !strcmp(m->f.s->n, v) ) {
  483. u = malloc(sizeof(*u));
  484. if ( u ) {
  485. u->type = EBasic;
  486. u->u.s = m->f.s;
  487. }
  488. }
  489. m = m->next;
  490. }
  491. }
  492. }
  493. return u;
  494. }
  495. struct unified_type *unification(char *t, char *v, int type)
  496. {
  497. TOUCH();
  498. struct unified_type *u = NULL;
  499. if ( !t || (!v && type == EBasic) ) {
  500. return u;
  501. }
  502. u = malloc(sizeof(*u));
  503. if ( u ) {
  504. u->type = type;
  505. if ( type == EBasic ) {
  506. u->u.s = malloc(sizeof(*u->u.s));
  507. if ( u->u.s ) {
  508. strcpy(u->u.s->n, v);
  509. u->u.s->t = strdup(t);
  510. }
  511. } else if ( type == EComp ) {
  512. u->u.c = malloc(sizeof(*u->u.c));
  513. if ( u->u.c ) {
  514. u->u.c->tag = strdup(t);
  515. if ( v ) {
  516. u->u.c->inst = strdup(v);
  517. }
  518. u->u.c->m = NULL;
  519. }
  520. }
  521. }
  522. return u;
  523. }
  524. void free_symbol_lists(void)
  525. {
  526. TOUCH();
  527. int i = 0;
  528. while ( i < EMax_nested_blocks + 1 ) {
  529. free_symbol_list(i);
  530. i++;
  531. }
  532. }