CodeGen.c
上传用户:wqdrylgs
上传日期:2007-02-09
资源大小:65k
文件大小:18k
源码类别:

汇编语言

开发平台:

WINDOWS

  1. #include <stdlib.h>
  2. #include "globals.h"
  3. #include "parse.h"
  4. #include "symtab.h"
  5. static int label_num = 0;
  6. TreeNode * Fun;
  7. void GenLabel(char lab[LABEL_SIZE])
  8. {
  9. static int i=1;
  10. char temp[LABEL_SIZE];
  11. strcpy(lab,"L");
  12. _itoa(i,temp,10);
  13. strcat(lab,temp);
  14. i++;
  15. }
  16. void StackSegmentBegin(void)
  17. {
  18. fprintf(inter_code,";*******************************************************n");
  19. fprintf(inter_code,"Stack_segtsegmentn");
  20. fprintf(inter_code,"tdwt1024 dup(?)n");
  21. fprintf(inter_code,"ttostlabeltwordn");
  22. fprintf(inter_code,"Stack_segtendsn");
  23. fprintf(inter_code,";*******************************************************n");
  24. fprintf(inter_code,"Data_segtsegmentn");
  25. }
  26. void StackSegmentEnd(void)
  27. {
  28. fprintf(inter_code,"Data_segtendsn");
  29. fprintf(inter_code,";*******************************************************n");
  30. }
  31. void CodeSegmentBegin(void)
  32. {
  33. fprintf(inter_code,"Code_segtsegmentn");
  34. fprintf(inter_code,"tassumetCS:Code_seg, DS:Data_seg, SS:Stack_segn");
  35. }
  36. void CodeSegmentEnd(void)
  37. {
  38. fprintf(inter_code,"Code_segtendsn");
  39. fprintf(inter_code,"tendtstartn");
  40. fprintf(inter_code,";*******************************************************n");
  41. }
  42. void VarDec_ini(char *name, int value)//local variable declaration with initial value
  43. {
  44. int os;
  45. ValEntry * pEntry = malloc(sizeof(ValEntry));
  46. if(pTable ->nestlevel == 0)
  47. fprintf(inter_code,"t%stdwt%dn",name,value);
  48. else
  49. {
  50. Lookup_Var(pTable, pTable ->funEntry, name, pEntry);
  51. os = pEntry ->offset;
  52. fprintf(inter_code,"tPUSHtAXn");//为该变量占个位置,迫使sp移动
  53. fprintf(inter_code,"tPUSHtAXn");//store value
  54. fprintf(inter_code,"tMOVtAX , %dn",value);
  55. fprintf(inter_code,"tPUSHtBPn");//calculate variable address
  56. fprintf(inter_code,"tADDtBP , %dn",os);
  57. fprintf(inter_code,"tMOVtSS:[BP] , AXn",os);//store value
  58. fprintf(inter_code,"tPOPtBPn");
  59. fprintf(inter_code,"tPOPtAXn");
  60. }
  61. }
  62. void VarDec(char *name)//local variable declaration without initial value
  63. {
  64. if(pTable ->nestlevel == 0)
  65. fprintf(inter_code,"t%stdwt?n",name);
  66. else
  67. fprintf(inter_code,"tPUSHtAXn");//为该变量占个位置,迫使sp移动
  68. }
  69. void ArrDec(char *name, int entitynum)
  70. {
  71. int i;
  72. if(pTable ->nestlevel == 0)
  73. fprintf(inter_code,"t%stdwt%d dup(?)n",name,entitynum);//to data file
  74. else
  75. for(i = 0; i < entitynum; i ++)
  76. fprintf(inter_code,"tPUSHtAXn");//为该变量占个位置,迫使sp移动
  77. }
  78. void ent(char *name, int para, int retvalue)
  79. {
  80. fprintf(inter_code,"t%stproctfarn",name);//to code file
  81. if(!strcmp("main",name)){//main function
  82. fprintf(inter_code,"start:n");
  83. fprintf(inter_code,"tMOVtAX , Stack_segn");
  84. fprintf(inter_code,"tMOVtSS , AXn");
  85. fprintf(inter_code,"tMOVtSP , offset tosnn");
  86. fprintf(inter_code,"tPUSHtDSn");
  87. fprintf(inter_code,"tSUBtAX , AXn");
  88. fprintf(inter_code,"tPUSHtAXnn");
  89. fprintf(inter_code,"tMOVtAX , Data_segn");
  90. fprintf(inter_code,"tMOVtDS , AXn");
  91. }
  92. fprintf(inter_code,"tPUSHtAXn");
  93. fprintf(inter_code,"tPUSHtBXn");
  94. fprintf(inter_code,"tPUSHtCXn");
  95. fprintf(inter_code,"tPUSHtDXn");
  96. fprintf(inter_code,"tPUSHtBPn");
  97. fprintf(inter_code,"tMOVtBP , SPn");//begin to accept local variables
  98. }
  99. //--------------------------------------------------
  100. void adi(void)
  101. {
  102. fprintf(inter_code,"tPOPtBXn");
  103. fprintf(inter_code,"tPOPtAXn");
  104. fprintf(inter_code,"tADDtAX , BXn");
  105. fprintf(inter_code,"tPUSHtAXn");
  106. }
  107. void andi(void)
  108. {
  109. fprintf(inter_code,"tPOPtBXn");
  110. fprintf(inter_code,"tPOPtAXn");
  111. fprintf(inter_code,"tANDtAX , BXn");
  112. fprintf(inter_code,"tPUSHtAXn");
  113. }
  114. void compound_enter(void)
  115. {
  116. }
  117. void compound_exit(void)
  118. {
  119. pTable = pTable->parent;
  120. }
  121. void cup(char *name, int paranum) //function call end, 'int' is the number of actual parameters
  122. {
  123. fprintf(inter_code,"tCALLt%sn",name);
  124. }
  125. void divi(void)
  126. {
  127. fprintf(inter_code,"tPOPtBXn");
  128. fprintf(inter_code,"tPOPtAXn");
  129. fprintf(inter_code,"tPUSHtDXn");
  130. fprintf(inter_code,"tIDIVtBXn");
  131. fprintf(inter_code,"tPOPtDXn");
  132. fprintf(inter_code,"tPUSHtAXn");
  133. }
  134. void equi(void)
  135. {
  136. fprintf(inter_code,"tPOPtBXn");
  137. fprintf(inter_code,"tPOPtAXn");
  138. fprintf(inter_code,"tCMPtAX , BXn");
  139. fprintf(inter_code,"tJEtLabel%dn",label_num);
  140. fprintf(inter_code,"tMOVtAX , 0n");
  141. fprintf(inter_code,"tPUSHtAXn");
  142. fprintf(inter_code,"tJMPtLabel%dn",label_num + 1);
  143. fprintf(inter_code,"Label%d:",label_num);
  144. fprintf(inter_code,"tMOVtAX , 1n");
  145. fprintf(inter_code,"tPUSHtAXn");
  146. fprintf(inter_code,"Label%d:",label_num + 1);
  147. label_num += 2;
  148. }
  149. void leave(char *name)
  150. {
  151. fprintf(inter_code,"t%stendpn",name);
  152. }
  153. void fjp(char*label)
  154. {
  155. fprintf(inter_code,"tPOPtAXn");
  156. fprintf(inter_code,"tCMPtAX , 0n");
  157. fprintf(inter_code,"tJEt%sn",label);
  158. }
  159. void gorei(void)
  160. {
  161. fprintf(inter_code,"tPOPtBXn");
  162. fprintf(inter_code,"tPOPtAXn");
  163. fprintf(inter_code,"tCMPtAX , BXn");
  164. fprintf(inter_code,"tJGEtLabel%dn",label_num);
  165. fprintf(inter_code,"tMOVtAX , 0n");
  166. fprintf(inter_code,"tPUSHtAXn");
  167. fprintf(inter_code,"tJMPtLabel%dn",label_num+1);
  168. fprintf(inter_code,"Label%d:",label_num);
  169. fprintf(inter_code,"tMOVtAX , 1n");
  170. fprintf(inter_code,"tPUSHtAXn");
  171. fprintf(inter_code,"Label%d:",label_num+1);
  172. label_num += 2;
  173. }
  174. void greater(void)
  175. {
  176. fprintf(inter_code,"tPOPtBXn");
  177. fprintf(inter_code,"tPOPtAXn");
  178. fprintf(inter_code,"tCMPtAX , BXn");
  179. fprintf(inter_code,"tJGtLabel%dn",label_num);
  180. fprintf(inter_code,"tMOVtAX , 0n");
  181. fprintf(inter_code,"tPUSHtAXn");
  182. fprintf(inter_code,"tJMPtLabel%dn",label_num+1);
  183. fprintf(inter_code,"Label%d:",label_num);
  184. fprintf(inter_code,"tMOVtAX , 1n");
  185. fprintf(inter_code,"tPUSHtAXn");
  186. fprintf(inter_code,"Label%d:",label_num+1);
  187. label_num += 2;
  188. }
  189. void ind(char *name)
  190. {
  191. if(pTable ->nestlevel == 0){
  192. fprintf(inter_code,"tPOPtBXn");
  193. fprintf(inter_code,"tPUSHtDS:[BX]n");
  194. }
  195. else{
  196. fprintf(inter_code,"tPOPtBXn");
  197. fprintf(inter_code,"tPUSHtBPn");
  198. fprintf(inter_code,"tMOVtBP , BXn");
  199. fprintf(inter_code,"tPUSHtSS:[BP]n");
  200. fprintf(inter_code,"tPOPtBPn");
  201. }
  202. }
  203. void ixa_elem_size(void)
  204. {
  205. fprintf(inter_code,"tPOPtAXn");
  206. fprintf(inter_code,"tPOPtBXn");
  207. fprintf(inter_code,"tSHLtAX , %dn",EXP_VARIABLE_LENGTH);//AX * VARIABLE_LENGTH
  208. fprintf(inter_code,"tADDtAX , BXn");
  209. fprintf(inter_code,"tPUSHtAXn");
  210. }
  211. void lab(char *label)
  212. {
  213. fprintf(inter_code,"%s :n",label);
  214. }
  215. void ldl(char value)
  216. {
  217. fprintf(inter_code,"tMOVtAX , %dn",(int)value);
  218. fprintf(inter_code,"tPUSHtAXn");
  219. }
  220. void lda(char *name)//load address
  221. {
  222. int addr = 0;
  223. ValEntry * pEntry = malloc(sizeof(ValEntry));
  224. if(pTable ->nestlevel == 0){
  225. fprintf(inter_code,"tLEAtAX , %sn",name);
  226. fprintf(inter_code,"tPUSHtAXn");
  227. }
  228. else{
  229. Lookup_Var(pTable, pTable ->funEntry, name, pEntry);
  230. addr = pEntry->offset;
  231. fprintf(inter_code,"tMOVtAX , BPn");
  232. fprintf(inter_code,"tADDtAX , %dn",addr);
  233. fprintf(inter_code,"tPUSHtAXn");
  234. }
  235. }
  236. void ldc(int value)//load constant
  237. {
  238. fprintf(inter_code,"tMOVtAX , %dn",value);
  239. fprintf(inter_code,"tPUSHtAXn");
  240. }
  241. void less(void)
  242. {
  243. fprintf(inter_code,"tPOPtBXn");
  244. fprintf(inter_code,"tPOPtAXn");
  245. fprintf(inter_code,"tCMPtAX , BXn");
  246. fprintf(inter_code,"tJLtLabel%dn",label_num);
  247. fprintf(inter_code,"tMOVtAX , 0n");
  248. fprintf(inter_code,"tPUSHtAXn");
  249. fprintf(inter_code,"tJMPtLabel%dn",label_num+1);
  250. fprintf(inter_code,"Label%d:",label_num);
  251. fprintf(inter_code,"tMOVtAX , 1n");
  252. fprintf(inter_code,"tPUSHtAXn");
  253. fprintf(inter_code,"Label%d:",label_num+1);
  254. label_num += 2;
  255. }
  256. void lod(char *name)
  257. {
  258. int addr = 0;
  259. ValEntry * pEntry = malloc(sizeof(ValEntry));
  260. Lookup_Var(pTable, pTable ->funEntry, name, pEntry);
  261. if(pTable ->nestlevel == 0){
  262. fprintf(inter_code,"tMOVtAX , %sn",name);
  263. fprintf(inter_code,"tPUSHtAXn");
  264. }
  265. else{
  266. Lookup_Var(pTable,pTable ->funEntry, name,pEntry);
  267. addr = pEntry ->offset;
  268. fprintf(inter_code,"tPUSHtBPn");
  269. fprintf(inter_code,"tADDtBP , %dn",addr);
  270. fprintf(inter_code,"tMOVtAX , SS:[BP]n");
  271. fprintf(inter_code,"tPOPtBPn");
  272. fprintf(inter_code,"tPUSHtAXn");
  273. }
  274. }
  275. void lorei(void)
  276. {
  277. fprintf(inter_code,"tPOPtBXn");
  278. fprintf(inter_code,"tPOPtAXn");
  279. fprintf(inter_code,"tCMPtAX , BXn");
  280. fprintf(inter_code,"tJLEtLabel%dn",label_num);
  281. fprintf(inter_code,"tMOVtAX , 0n");
  282. fprintf(inter_code,"tPUSHtAXn");
  283. fprintf(inter_code,"tJMPtLabel%dn",label_num+1);
  284. fprintf(inter_code,"Label%d:",label_num);
  285. fprintf(inter_code,"tMOVtAX , 1n");
  286. fprintf(inter_code,"tPUSHtAXn");
  287. fprintf(inter_code,"Label%d:",label_num+1);
  288. label_num += 2;
  289. }
  290. void mst(char *name)
  291. {
  292. }
  293. void multi(void)
  294. {
  295. fprintf(inter_code,"tPOPtBXn");
  296. fprintf(inter_code,"tPOPtAXn");
  297. fprintf(inter_code,"tPUSHtDXn");
  298. fprintf(inter_code,"tIMULtBXn");
  299. fprintf(inter_code,"tPOPtDXn");
  300. fprintf(inter_code,"tPUSHtAXn");
  301. }
  302. void neqi(void)
  303. {
  304. fprintf(inter_code,"tPOPtBXn");
  305. fprintf(inter_code,"tPOPtAXn");
  306. fprintf(inter_code,"tCMPtAX , BXn");
  307. fprintf(inter_code,"tJNEtLabel%dn",label_num);
  308. fprintf(inter_code,"tMOVtAX , 0n");
  309. fprintf(inter_code,"tPUSHtAXn");
  310. fprintf(inter_code,"tJMPtLabel%dn",label_num+1);
  311. fprintf(inter_code,"Label%d:",label_num);
  312. fprintf(inter_code,"tMOVtAX , 1n");
  313. fprintf(inter_code,"tPUSHtAXn");
  314. fprintf(inter_code,"Label%d:",label_num+1);
  315. label_num += 2;
  316. }
  317. void ori(void)
  318. {
  319. fprintf(inter_code,"tPOPtBXn");
  320. fprintf(inter_code,"tPOPtAXn");
  321. fprintf(inter_code,"tORtAX , BXn");
  322. fprintf(inter_code,"tPUSHtAXn");
  323. }
  324. void pop(void)
  325. {
  326. fprintf(inter_code,"tPOPtAXn");
  327. }
  328. void ret(int r,char * name)
  329. {
  330. int addr = 0;
  331. FunEntry * pEntry = malloc(sizeof(FunEntry));
  332. if(r){//if the function has a return value
  333. fprintf(inter_code,"tPOPtAXn");//get the return value from the top of the stack
  334. pEntry = Lookup_Fun(name);
  335. addr = pEntry ->ret_val;
  336. fprintf(inter_code,"tPUSHtBPn");
  337. fprintf(inter_code,"tADDtBP , %dn",addr);
  338. fprintf(inter_code,"tMOVtSS:[BP] , AXn");
  339. fprintf(inter_code,"tPOPtBPn");
  340. }
  341. fprintf(inter_code,"tMOVtSP , BPn");//release local parameters
  342. fprintf(inter_code,"tPOPtBPn");//pop bp
  343. fprintf(inter_code,"tPOPtDXn");
  344. fprintf(inter_code,"tPOPtCXn");
  345. fprintf(inter_code,"tPOPtBXn");
  346. fprintf(inter_code,"tPOPtAXn");
  347. fprintf(inter_code,"tRETn");
  348. }
  349. void stn(char *name)
  350. {
  351. if(pTable ->nestlevel == 0){
  352. fprintf(inter_code,"tPOPtBXn");//get the value from the top of the stack
  353. fprintf(inter_code,"tPOPtAXn");//get the actual address at STACK segment
  354. fprintf(inter_code,"tMOVtDS:[AX] , BXn");//load value
  355. fprintf(inter_code,"tPUSHtBXn");
  356. }
  357. else{
  358. fprintf(inter_code,"tPOPtBXn");//get the value from the top of the stack
  359. fprintf(inter_code,"tPOPtAXn");//get the actual address at STACK segment
  360. fprintf(inter_code,"tPUSHtBPn");
  361. fprintf(inter_code,"tMOVtBP , AXn");
  362. fprintf(inter_code,"tMOVtSS:[BP] , BXn");//load value
  363. fprintf(inter_code,"tPOPtBPn");
  364. fprintf(inter_code,"tPUSHtBXn");
  365. }
  366. }
  367. void subi(void)
  368. {
  369. fprintf(inter_code,"tPOPtDXn");
  370. fprintf(inter_code,"tPOPtCXn");
  371. fprintf(inter_code,"tSUBtCX , DXn");
  372. fprintf(inter_code,"tPUSHtCXn");
  373. }
  374. void ujp(char *label)
  375. {
  376. fprintf(inter_code,"tJMPt%sn",label);
  377. }
  378. void write(void)
  379. {
  380. fprintf(inter_code,"tWRITEtproctfarn");//to code file
  381. fprintf(inter_code,"tPUSHtAXn");
  382. fprintf(inter_code,"tPUSHtBXn");
  383. fprintf(inter_code,"tPUSHtCXn");
  384. fprintf(inter_code,"tPUSHtDXn");
  385. fprintf(inter_code,"tPUSHtBPn");
  386. fprintf(inter_code,"tMOVtBP , SPn");//begin to accept local variables
  387. fprintf(inter_code,"tMOVtBX , [BP + 14]n");
  388. fprintf(inter_code,"tMOVtCX , 10000n");
  389. fprintf(inter_code,"tCMPtBX , CXn");
  390. fprintf(inter_code,"tJLtw_label1n");
  391. fprintf(inter_code,"tCALLtWRITE_SUBn");
  392. fprintf(inter_code,"w_label1:n");
  393. fprintf(inter_code,"tMOVtCX , 1000n");
  394. fprintf(inter_code,"tCMPtBX , CXn");
  395. fprintf(inter_code,"tJLtw_label2n");
  396. fprintf(inter_code,"tCALLtWRITE_SUBn");
  397. fprintf(inter_code,"w_label2:n");
  398. fprintf(inter_code,"tMOVtCX , 100n");
  399. fprintf(inter_code,"tCMPtBX , CXn");
  400. fprintf(inter_code,"tJLtw_label3n");
  401. fprintf(inter_code,"tCALLtWRITE_SUBn");
  402. fprintf(inter_code,"w_label3:n");
  403. fprintf(inter_code,"tMOVtCX , 10n");
  404. fprintf(inter_code,"tCMPtBX , CXn");
  405. fprintf(inter_code,"tJLtw_label4n");
  406. fprintf(inter_code,"tCALLtWRITE_SUBn");
  407. fprintf(inter_code,"w_label4:n");
  408. fprintf(inter_code,"tMOVtCX , 1n");
  409. fprintf(inter_code,"tCALLtWRITE_SUBn");
  410. fprintf(inter_code,"tMOVtSP , BPn");//release local parameters
  411. fprintf(inter_code,"tPOPtBPn");//pop bp
  412. fprintf(inter_code,"tPOPtDXn");
  413. fprintf(inter_code,"tPOPtCXn");
  414. fprintf(inter_code,"tPOPtBXn");
  415. fprintf(inter_code,"tPOPtAXn");
  416. fprintf(inter_code,"tRETn");
  417. fprintf(inter_code,"tWRITEtendpnn");
  418. fprintf(inter_code,"tWRITE_SUBtproctnearn");
  419. fprintf(inter_code,"tMOVtAX , BXn");
  420. fprintf(inter_code,"tMOVtDX , 0n");
  421. fprintf(inter_code,"tDIVtCXn");
  422. fprintf(inter_code,"tMOVtBX , DXn");
  423. fprintf(inter_code,"tMOVtDL , ALn");
  424. fprintf(inter_code,"tADDtDL , '0'n");
  425. fprintf(inter_code,"tMOVtAH , 02hn");
  426. fprintf(inter_code,"tINTt21hn");
  427. fprintf(inter_code,"tRETn");
  428. fprintf(inter_code,"tWRITE_SUBtendpnn");
  429. }
  430. void CodeGen_TreeNode(TreeNode * node)
  431. {
  432. TreeNode * temp;
  433. int count;
  434. while (node != NULL) 
  435. {
  436. switch(node ->nodekind){
  437. case Dec:
  438. switch(node-> kind.dec){
  439. case FunDecK:
  440. // more code to be filled
  441. break;
  442. case FunDefK:
  443. Fun = node;
  444. count = 0;
  445. temp = node-> child[0];
  446. while(temp)
  447. {
  448. count ++;
  449. temp = temp-> sibling;
  450. }
  451. ent(node ->attr.name, count, (node -> type != Void)? TRUE:FALSE);
  452. temp = node -> child[0]; // Param
  453. if(temp)
  454. CodeGen_TreeNode(temp);
  455. CodeGen_TreeNode(node ->child[1]); // comp_dec
  456. if(node -> type == Void)
  457. ret(FALSE,node ->attr.name);
  458. else
  459. ret(TRUE,node ->attr.name);
  460. leave(node ->attr.name);
  461. break;
  462. case VarK:
  463. temp = node -> child[0];
  464. if(node ->kind.exp == NumK)
  465. while(temp)
  466. {
  467. if(temp -> nodekind == Stmt) // AssignK ini
  468. VarDec_ini(temp ->child[0] ->attr.name, temp->child[1]->attr.val.i);
  469. else
  470. {
  471. if(temp ->child[0] == NULL)
  472. VarDec(temp ->attr.name);
  473. else
  474. ArrDec(temp ->attr.name, temp ->child[0] ->attr.val.i);
  475. }
  476. temp = temp ->sibling;
  477. }
  478. break;
  479. case CompK:
  480. pTable = node ->attr.table;
  481. temp = node ->child[0];
  482. if(temp) // Dec
  483. CodeGen_TreeNode(temp);
  484. temp = node ->child[1];
  485. if(temp) //stmt
  486. CodeGen_TreeNode(temp);
  487. compound_exit();
  488. break;
  489. case ParamK:
  490. break;
  491. default:
  492. break;
  493. }
  494. break;
  495. case Stmt:
  496. switch(node ->kind.stmt){
  497. case IfK:
  498. temp = node ->child[0];
  499. GenLabel(pTable ->lab1);
  500. GenLabel(pTable ->lab2);
  501. if(temp ->kind.exp == NumK) // optimize
  502. {
  503. if(temp -> attr.val.i == 0)
  504. ujp(pTable ->lab1);
  505. }
  506. else 
  507. {
  508. CodeGen_TreeNode(temp);
  509. fjp(pTable ->lab1);
  510. }
  511. pTable = node ->child[1] ->attr.table;
  512. if(node ->child[1])
  513. CodeGen_TreeNode(node ->child[1]);
  514. if(node ->child[2]) 
  515. ujp(pTable ->lab2);
  516. lab(pTable ->lab1);
  517. if(node ->child[2]) 
  518. {
  519. pTable = node ->child[2] ->attr.table;
  520. CodeGen_TreeNode(node ->child[2]);
  521. lab(pTable ->lab2);
  522. }
  523. break;
  524. case WhileK:
  525. GenLabel(pTable ->lab1);
  526. GenLabel(pTable ->lab2);
  527. lab(pTable ->lab1);
  528. temp = node ->child[0];
  529. if(temp ->kind.exp == NumK) // optimize
  530. {
  531. if(temp ->attr.val.i == 0)
  532. ujp(pTable ->lab2);
  533. }
  534. else
  535. {
  536. CodeGen_TreeNode(temp);
  537. fjp(pTable ->lab2);
  538. }
  539. if(node -> child[1])
  540. CodeGen_TreeNode(node -> child[1]);
  541. ujp(pTable ->lab1);
  542. lab(pTable ->lab2);
  543. break;
  544. case CallK:
  545. mst(node -> attr.name);
  546. temp = node ->child[0];
  547. CodeGen_TreeNode(temp);
  548. cup(node ->attr.name, count);
  549. while(temp) // Param
  550. {
  551. pop();
  552. temp = temp -> sibling;
  553. }
  554. if(node ->call_stmt == 1)
  555. pop();
  556. break;
  557. case ReturnK:
  558. temp = node -> child[0];
  559. if(temp)
  560. {
  561. CodeGen_TreeNode(temp);
  562. ret(TRUE,pTable ->funEntry ->name);
  563. }
  564. else ret(FALSE,pTable ->funEntry ->name);
  565. break;
  566. case BreakK:
  567. ujp(pTable ->parent ->lab2);
  568. break;
  569.         case ContinueK:
  570. ujp(pTable ->parent ->lab1);
  571. break;
  572. case AssignK:
  573. temp = node -> child[0];
  574. lda(temp ->attr.name);
  575. if(temp -> child[0])  //Arr
  576. {
  577. CodeGen_TreeNode(temp -> child[0]);
  578. ixa_elem_size();
  579. }
  580. CodeGen_TreeNode(node -> child[1]);
  581. stn(node -> child[0] ->attr.name);
  582. pop();
  583. break;
  584. default:
  585. break;
  586. }
  587. break;
  588. case Exp:
  589. switch(node -> kind.exp){
  590. case OpK:
  591. if(node -> child[0])
  592. CodeGen_TreeNode(node -> child[0]);
  593. if(node -> child[1])
  594. CodeGen_TreeNode(node -> child[1]);
  595. switch(node -> attr.op) {
  596. case PLUS: adi(); break;
  597. case SUB: subi(); break;
  598. case MUT: multi(); break;
  599. case DIV: divi(); break;
  600. case EQ: equi(); break;
  601. case NEQ: neqi(); break;
  602. case LE: lorei(); break;
  603. case GE: gorei(); break;
  604. case LT: less(); break;
  605. case GT: greater(); break;
  606. case AND: andi(); break;
  607. case OR: ori(); break;
  608. default: yyerror("Unknown Operator!");
  609. break;
  610. }
  611. break;
  612. case FnumK:
  613. //  more code to be filled
  614. break;
  615. case NumK:
  616. ldc(node -> attr.val.i);
  617. break;
  618. case CharK:
  619. // ldc(node -> attr.val.i);
  620. break;
  621. case IdK:
  622. lod(node -> attr.name);
  623. break;
  624. case NotK:
  625. break;
  626. default:
  627. break;
  628. }
  629. break;
  630. default:
  631. break;
  632. }
  633.     node = node->sibling;
  634. }
  635. }