Year 2 compilers coureswork

main.c 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <ctype.h>
  4. #include <string.h>
  5. #include "nodes.h"
  6. #include "C.tab.h"
  7. #include "types.h"
  8. #include "list.h"
  9. int asprintf(char **strp, const char *fmt, ...);
  10. extern TOKEN* lookup_token(char*);
  11. /*
  12. * Existing problems/todo:
  13. * return from control block, need a "return address" to jump to -- call levels
  14. * --multiple assignments `int x,y,z = 1`--
  15. * new block scope for if statements?
  16. * arguments for functions
  17. * difference between tilde and semicolon
  18. * while loops
  19. * control flow `continue` and `break` linked to return address sort of? call levels
  20. */
  21. TOKEN* gen_tmp(void) {
  22. char* tmp;
  23. static char letter = 'a';
  24. static char num = 0;
  25. if (num == 100) {
  26. num = 0;
  27. if (++letter == 'r') letter++;
  28. }
  29. if (letter == '{') exit(1);
  30. asprintf(&tmp, "\\%c%d", letter, num);
  31. num++;
  32. return lookup_token(tmp);
  33. }
  34. char *named(int t)
  35. {
  36. static char b[100];
  37. if (isgraph(t) || t==' ') {
  38. sprintf(b, "%c", t);
  39. return b;
  40. }
  41. switch (t) {
  42. default: return "???";
  43. case IDENTIFIER:
  44. return "id";
  45. case CONSTANT:
  46. return "constant";
  47. case STRING_LITERAL:
  48. return "string";
  49. case LE_OP:
  50. return "<=";
  51. case GE_OP:
  52. return ">=";
  53. case EQ_OP:
  54. return "==";
  55. case NE_OP:
  56. return "!=";
  57. case EXTERN:
  58. return "extern";
  59. case AUTO:
  60. return "auto";
  61. case INT:
  62. return "int";
  63. case VOID:
  64. return "void";
  65. case APPLY:
  66. return "apply";
  67. case LEAF:
  68. return "leaf";
  69. case IF:
  70. return "if";
  71. case ELSE:
  72. return "else";
  73. case WHILE:
  74. return "while";
  75. case CONTINUE:
  76. return "continue";
  77. case BREAK:
  78. return "break";
  79. case RETURN:
  80. return "return";
  81. }
  82. }
  83. void print_leaf(NODE *tree, int level)
  84. {
  85. TOKEN *t = (TOKEN *)tree;
  86. int i;
  87. for (i=0; i<level; i++) putchar(' ');
  88. if (t->type == CONSTANT) printf("%d\n", t->value);
  89. else if (t->type == STRING_LITERAL) printf("\"%s\"\n", t->lexeme);
  90. else if (t) puts(t->lexeme);
  91. }
  92. void print_tree0(NODE *tree, int level)
  93. {
  94. int i;
  95. if (tree==NULL) return;
  96. if (tree->type==LEAF) {
  97. print_leaf(tree->left, level);
  98. }
  99. else {
  100. for(i=0; i<level; i++) putchar(' ');
  101. printf("%s\n", named(tree->type));
  102. /* if (tree->type=='~') { */
  103. /* for(i=0; i<level+2; i++) putchar(' '); */
  104. /* printf("%p\n", tree->left); */
  105. /* } */
  106. /* else */
  107. print_tree0(tree->left, level+2);
  108. print_tree0(tree->right, level+2);
  109. }
  110. }
  111. void print_tree(NODE *tree)
  112. {
  113. print_tree0(tree, 0);
  114. }
  115. // void add_function_to_env(NODE *tree, ENV *env_ptr) {
  116. // BIND* existing;
  117. // NODE* func_name = tree->left->right->left->left;
  118. // TOKEN* name_token = (TOKEN*) func_name;
  119. // if ((existing = find_name_in_env(name_token->lexeme, env_ptr)) == NULL) {
  120. // printf("Added function name %s to environment with value: \n", name_token->lexeme);
  121. // print_tree(tree);
  122. // if (env_ptr->bindings == NULL) {
  123. // env_ptr->bindings = create_list(create_binding(name_token->lexeme, tree, env_ptr), NULL);
  124. // } else {
  125. // append_list(env_ptr->bindings, create_binding(name_token->lexeme, tree, env_ptr));
  126. // }
  127. // } else {
  128. // printf("Updating function name %s with value: \n", name_token->lexeme);
  129. // print_tree(tree);
  130. // existing->tree = tree;
  131. // existing->env = env_ptr;
  132. // }
  133. // }
  134. // void add_var_to_env(NODE *tree, ENV *env_ptr) {
  135. // BIND* existing;
  136. // NODE* var_name = tree->left->left;
  137. // TOKEN* name_token = (TOKEN*) var_name;
  138. // TOKEN* tok = new_token(INT);
  139. // tok->value = recursive_interpret(tree->right, env_ptr);
  140. // asprintf(&tok->lexeme, "%d", tok->value);
  141. // if ((existing = find_name_in_env(name_token->lexeme, env_ptr)) == NULL) {
  142. // printf("Added variable name %s to environment with value: %s\n", name_token->lexeme, tok->lexeme);
  143. // if (env_ptr->bindings == NULL) {
  144. // env_ptr->bindings = create_list(create_binding(name_token->lexeme, (NODE*) tok, NULL), NULL);
  145. // } else {
  146. // append_list(env_ptr->bindings, create_binding(name_token->lexeme, (NODE*) tok, NULL));
  147. // }
  148. // } else {
  149. // printf("Updating variable name %s with value: %s\n", name_token->lexeme, tok->lexeme);
  150. // existing->tree = (NODE *) tok;
  151. // }
  152. // }
  153. void new_tac(TACS* tacs, TOKEN* dst, TOKEN* src, TOKEN* tgt, int op) {
  154. if (tacs->list == NULL) {
  155. TAC_T* tac;
  156. tac = create_tac(op, src, tgt, dst);
  157. tacs->list = new_tac_list(tac, NULL);
  158. } else {
  159. add_tac(tacs->list, op, src, tgt, dst);
  160. }
  161. }
  162. char* stok(TOKEN* tok) {
  163. return tok->lexeme;
  164. }
  165. void print_tac(TLIST* tac_list) {
  166. TLIST *ptr = tac_list;
  167. while (ptr != NULL) {
  168. int op = ptr->elem->op;
  169. if (op == '=') printf("%s := %s\n", stok(ptr->elem->dst), stok(ptr->elem->src));
  170. if (op == '+') printf("%s := %s + %s\n", stok(ptr->elem->dst), stok(ptr->elem->src), stok(ptr->elem->tgt));
  171. if (op == '-') printf("%s := %s - %s\n", stok(ptr->elem->dst), stok(ptr->elem->src), stok(ptr->elem->tgt));
  172. if (op == '/') printf("%s := %s / %s\n", stok(ptr->elem->dst), stok(ptr->elem->src), stok(ptr->elem->tgt));
  173. if (op == '*') printf("%s := %s * %s\n", stok(ptr->elem->dst), stok(ptr->elem->src), stok(ptr->elem->tgt));
  174. if (op == '%') printf("%s := %s %% %s\n", stok(ptr->elem->dst), stok(ptr->elem->src), stok(ptr->elem->tgt));
  175. if (op == RETURN) printf("ret %s\n", stok(ptr->elem->src));
  176. ptr = ptr->next;
  177. }
  178. }
  179. TOKEN* build_tac(NODE *tree, TACS* tac) {
  180. if (tree == NULL) return NULL;
  181. int type = tree->type;
  182. if (type == LEAF) {
  183. return (TOKEN *) tree->left;
  184. } else if (type == '=') {
  185. TOKEN* dst = (TOKEN*) tree->left->left;
  186. TOKEN* result = build_tac(tree->right, tac);
  187. new_tac(tac, dst, result, NULL, tree->type);
  188. } else if (type=='+' || type=='-' || type=='*' || type=='/' || type =='%') {
  189. TOKEN* dst = gen_tmp();
  190. TOKEN* src = build_tac(tree->left, tac);
  191. TOKEN* tgt = build_tac(tree->right, tac);
  192. new_tac(tac, dst, src, tgt, tree->type);
  193. return dst;
  194. } else if (type == RETURN) {
  195. TOKEN* src = build_tac(tree->left, tac);
  196. new_tac(tac, NULL, src, NULL, RETURN);
  197. } else {
  198. build_tac(tree->left, tac);
  199. build_tac(tree->right, tac);
  200. }
  201. }
  202. int tmp_regs[10] = {8, 9, 10, 11, 12, 13, 14, 15, 24, 25};
  203. int svd_regs[8] = {16, 17, 18, 19, 20, 21, 22, 23};
  204. int tmp_cntr = 0;
  205. VARLOC** vartab;
  206. int is_imm(TOKEN *t) {
  207. if (t->type==CONSTANT) return 1;
  208. else return 0;
  209. }
  210. int find_var_idx(TOKEN* var) {
  211. int i;
  212. for (i = 0; i < 8; i++) {
  213. if (vartab[i]->var == var) {
  214. return vartab[i]->loc_idx;
  215. }
  216. }
  217. return -1;
  218. }
  219. int store_var(TOKEN* name) {
  220. int i;
  221. for (i = 0; i < 8; i++) {
  222. if (vartab[i]->var == NULL) {
  223. vartab[i]->var = name;
  224. vartab[i]->loc_idx = svd_regs[i];
  225. return vartab[i]->loc_idx;
  226. } else if (name == vartab[i]->var) {
  227. vartab[i]->loc_idx = svd_regs[i];
  228. return svd_regs[i];
  229. }
  230. }
  231. printf("bugger\n");
  232. exit(1);
  233. }
  234. void init_reg() {
  235. int i;
  236. vartab = (VARLOC**) malloc(8 * sizeof(VARLOC*));
  237. for (i = 0; i < 8; i++) {
  238. vartab[i] = (VARLOC*) malloc(sizeof(VARLOC));
  239. vartab[i]->var = NULL;
  240. vartab[i]->loc_idx = -1;
  241. }
  242. }
  243. int alloc_register(TOKEN* val) {
  244. int idx;
  245. int imm = is_imm(val);
  246. if (imm) {
  247. if (val->value <= 32767 && val->value >= -32767) {
  248. printf("ori $%d, $0, %d\n", tmp_regs[tmp_cntr], val->value);
  249. idx = tmp_regs[tmp_cntr++];
  250. } else {
  251. printf("li $%d, %d\n", tmp_regs[tmp_cntr], val->value);
  252. idx = tmp_regs[tmp_cntr++];
  253. }
  254. } else {
  255. idx = find_var_idx(val);
  256. }
  257. if (tmp_cntr == 10) tmp_cntr = 0;
  258. return idx;
  259. }
  260. void compile_tac(TLIST* tacs) {
  261. printf("=====\n");
  262. printf("main:\n");
  263. init_reg();
  264. while(tacs != NULL) {
  265. TAC_T* t = tacs->elem;
  266. if (t->op == '=') {
  267. int src_idx = alloc_register(t->src);
  268. printf("or $%d, $0, $%d\n", store_var(t->dst), src_idx);
  269. }
  270. if (t->op == '+') {
  271. int tgt_idx = alloc_register(t->tgt);
  272. int src_idx = alloc_register(t->src);
  273. printf("add $%d, $%d, $%d\n", store_var(t->dst), src_idx, tgt_idx);
  274. }
  275. if (t->op == '-') {
  276. int tgt_idx = alloc_register(t->tgt);
  277. int src_idx = alloc_register(t->src);
  278. printf("sub $%d, $%d, $%d\n", store_var(t->dst), src_idx, tgt_idx);
  279. }
  280. if (t->op == '*') {
  281. int tgt_idx = alloc_register(t->tgt);
  282. int src_idx = alloc_register(t->src);
  283. printf("mult $%d, $%d\n", src_idx, tgt_idx);
  284. printf("mflo $%d\n", store_var(t->dst));
  285. }
  286. if (t->op == '/') {
  287. int tgt_idx = alloc_register(t->tgt);
  288. int src_idx = alloc_register(t->src);
  289. printf("div $%d, $%d\n", src_idx, tgt_idx);
  290. printf("mflo $%d\n", store_var(t->dst));
  291. }
  292. if (t->op == '%') {
  293. int tgt_idx = alloc_register(t->tgt);
  294. int src_idx = alloc_register(t->src);
  295. printf("div $%d, $%d\n", src_idx, tgt_idx);
  296. printf("mfhi $%d\n", store_var(t->dst));
  297. }
  298. if (t->op == RETURN) {
  299. int src_idx = alloc_register(t->src);
  300. printf("or $k0, $0, $%d\n", src_idx);
  301. }
  302. tacs = tacs->next;
  303. }
  304. printf("ori $v0, 10\n");
  305. printf("syscall\n");
  306. }
  307. // ENV* cons_global_env(NODE *tree) {
  308. // ENV* global = create_new_function_env(NULL);
  309. // recursive_interpret(tree, global);
  310. // return global;
  311. // }
  312. void interpret_tree(NODE *tree) {
  313. // ENV* global_env = cons_global_env(tree);
  314. // BIND* ref_main = find_name_in_env("main", global_env);
  315. // if (ref_main == NULL) {
  316. // printf("Could not find main, cannot run!\n");
  317. // exit(1);
  318. // }
  319. // print ref_main to make sure we really got it
  320. // printf("Located %s, ready to run!\n", ref_main->name);
  321. // printf("%d\n", recursive_interpret(ref_main->tree->right, ref_main->env));
  322. TACS* gen_tac = (TACS*) malloc(sizeof(TACS));
  323. build_tac(tree, gen_tac);
  324. print_tac(gen_tac->list);
  325. compile_tac(gen_tac->list);
  326. }
  327. extern int yydebug;
  328. /* extern NODE* yyparse(void); */
  329. extern NODE* ans;
  330. extern void init_symbtable(void);
  331. int main(int argc, char** argv)
  332. {
  333. NODE* tree;
  334. if (argc>1 && strcmp(argv[1],"-d")==0) yydebug = 1;
  335. init_symbtable();
  336. printf("--C COMPILER\n");
  337. yyparse();
  338. tree = ans;
  339. printf("parse finished\n");
  340. print_tree(tree);
  341. interpret_tree(tree);
  342. // int i;
  343. // for (i = 0; i < 2600; i++) printf("%s\n", gen_tmp());
  344. return 0;
  345. }