Advertisement
Guest User

Untitled

a guest
May 23rd, 2018
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 22.74 KB | None | 0 0
  1. import org.antlr.runtime.tree.Tree;
  2.  
  3. import java.util.ArrayList;
  4.  
  5. public class GenCode {
  6.  
  7. private Node tds;
  8. private Tree t;
  9. private String code;
  10. private int compteurif;
  11. private int compteurwhile;
  12.  
  13.  
  14. public GenCode(Node tds, Tree t) {
  15. this.tds = tds;
  16. this.t = t;
  17. this.compteurif = 0;
  18. this.compteurwhile = 0;
  19. this.code = "";
  20. this.code += this.genFichierMain();
  21.  
  22. this.genererCodefct(tds);
  23.  
  24.  
  25. }
  26.  
  27. public void genererCodefct(Node n) {
  28. if (n.getId().equals("ROOT")) {
  29.  
  30. } else if (n.getId().equals("main")) {
  31. this.code += this.genCodeMainHead();
  32. this.code += this.genDeclarationVariable(n);
  33. this.parcourtArbre(n, n.getT(), 0, 0);
  34. this.code += this.genCodeMainTail();
  35. } else if (!n.getId().contains("If") && !n.getId().contains("While")) {
  36. this.code += this.genFctHead(n);
  37. this.parcourtArbre(n, n.getT(), 0, 0);
  38. this.code += this.genFctTail(n);
  39. }
  40. for (int y = 0; y < n.getFils().size(); y++) {
  41.  
  42.  
  43. this.genererCodefct(n.getFils().get(y));
  44. }
  45. }
  46.  
  47. public void parcourtArbre(Node tds, Tree t, int dec, int deb) {
  48.  
  49. int res;
  50. DeclarationSymbole sb;
  51. int j;
  52. if (t != null) {
  53. for (int i = deb; i < t.getChildCount() - dec; i++) {
  54. Tree tfils = t.getChild(i);
  55.  
  56. switch (tfils.getText()) {
  57.  
  58. case "LET":
  59.  
  60. int depl1;
  61. int depl2;
  62. for (j = 0; j < tfils.getChildCount(); j++) {
  63.  
  64.  
  65. if (estUnEntier(tfils.getChild(j).getText())) {
  66. sb = (DeclarationSymbole) tds.getSymbole(tfils.getChild(j - 1).getText());
  67. this.code += this.genAffectationVal(sb, Integer.parseInt(tfils.getChild(j).getText()));
  68. } else if (tfils.getChild(j).getText().equals("+")) {
  69. if (!estUnEntier(tfils.getChild(j).getChild(0).getText())) {
  70. sb = (DeclarationSymbole) tds.getSymbole(tfils.getChild(j).getChild(0).getText());
  71. depl1 = sb.getDepl();
  72. } else {
  73. depl1 = 1;
  74. this.genValdansRegistre(Integer.parseInt(tfils.getChild(j).getChild(0).getText()),1);
  75. }
  76. if (!estUnEntier(tfils.getChild(j).getChild(1).getText())) {
  77. sb = (DeclarationSymbole) tds.getSymbole(tfils.getChild(j).getChild(1).getText());
  78. depl2 = sb.getDepl();
  79. } else {
  80. depl2 = 1;
  81. this.genValdansRegistre(Integer.parseInt(tfils.getChild(j).getChild(1).getText()),1);
  82. }
  83. this.code += this.genOp(-depl1, -depl2, "+");
  84. sb = (DeclarationSymbole) tds.getSymbole(tfils.getChild(j - 1).getText());
  85. //res = Integer.parseInt(tfils.getChild(j).getChild(0).getText())+Integer.parseInt(tfils.getChild(j).getChild(1).getText()) ;
  86. this.code += this.genRegistredansVariable(sb.depl, 1);
  87. } else if (tfils.getChild(j).getText().equals("-")) {
  88. if (!estUnEntier(tfils.getChild(j).getChild(0).getText())) {
  89. sb = (DeclarationSymbole) tds.getSymbole(tfils.getChild(j).getChild(0).getText());
  90. depl1 = sb.getDepl();
  91. } else {
  92. depl1 = 1;
  93. this.genValdansRegistre(Integer.parseInt(tfils.getChild(j).getChild(0).getText()),1);
  94. }
  95. if (!estUnEntier(tfils.getChild(j).getChild(1).getText())) {
  96. sb = (DeclarationSymbole) tds.getSymbole(tfils.getChild(j).getChild(1).getText());
  97. depl2 = sb.getDepl();
  98. } else {
  99. depl2 = 1;
  100. this.genValdansRegistre(Integer.parseInt(tfils.getChild(j).getChild(1).getText()),1);
  101. }
  102. this.code += this.genOp(-depl1, -depl2, "-");
  103. sb = (DeclarationSymbole) tds.getSymbole(tfils.getChild(j - 1).getText());
  104. //res = Integer.parseInt(tfils.getChild(j).getChild(0).getText())+Integer.parseInt(tfils.getChild(j).getChild(1).getText()) ;
  105. this.code += this.genRegistredansVariable(sb.depl, 1);
  106. } else if (tfils.getChild(j).getText().equals("*")) {
  107. if (!estUnEntier(tfils.getChild(j).getChild(0).getText())) {
  108. sb = (DeclarationSymbole) tds.getSymbole(tfils.getChild(j).getChild(0).getText());
  109. depl1 = sb.getDepl();
  110. } else {
  111. depl1 = 1;
  112. this.genValdansRegistre(Integer.parseInt(tfils.getChild(j).getChild(0).getText()),1);
  113. }
  114. if (!estUnEntier(tfils.getChild(j).getChild(1).getText())) {
  115. sb = (DeclarationSymbole) tds.getSymbole(tfils.getChild(j).getChild(1).getText());
  116. depl2 = sb.getDepl();
  117. } else {
  118. depl2 = 1;
  119. this.genValdansRegistre(Integer.parseInt(tfils.getChild(j).getChild(1).getText()),1);
  120. }
  121. this.code += this.genOp(-depl1, -depl2, "*");
  122. sb = (DeclarationSymbole) tds.getSymbole(tfils.getChild(j - 1).getText());
  123. //res = Integer.parseInt(tfils.getChild(j).getChild(0).getText())+Integer.parseInt(tfils.getChild(j).getChild(1).getText()) ;
  124. this.code += this.genRegistredansVariable(sb.depl, 1);
  125. } else if (tfils.getChild(j).getText().equals("/")) {
  126. if (!estUnEntier(tfils.getChild(j).getChild(0).getText())) {
  127. sb = (DeclarationSymbole) tds.getSymbole(tfils.getChild(j).getChild(0).getText());
  128. depl1 = sb.getDepl();
  129. } else {
  130. depl1 = 1;
  131. this.genValdansRegistre(Integer.parseInt(tfils.getChild(j).getChild(0).getText()),1);
  132. }
  133. if (!estUnEntier(tfils.getChild(j).getChild(1).getText())) {
  134. sb = (DeclarationSymbole) tds.getSymbole(tfils.getChild(j).getChild(1).getText());
  135. depl2 = sb.getDepl();
  136. } else {
  137. depl2 = 1;
  138. this.genValdansRegistre(Integer.parseInt(tfils.getChild(j).getChild(1).getText()),1);
  139. }
  140. this.code += this.genOp(-depl1, -depl2, "/");
  141. sb = (DeclarationSymbole) tds.getSymbole(tfils.getChild(j - 1).getText());
  142. //res = Integer.parseInt(tfils.getChild(j).getChild(0).getText())+Integer.parseInt(tfils.getChild(j).getChild(1).getText()) ;
  143. this.code += this.genRegistredansVariable(sb.depl, 1);
  144. }
  145.  
  146. }
  147. parcourtArbre(tds, tfils, 0, 0);
  148.  
  149.  
  150. break;
  151. case "RETURN":
  152.  
  153. sb = (DeclarationSymbole) tds.getSymbole(tfils.getChild(0).getText());
  154. this.code += this.genFctReturn(sb.depl);
  155. break;
  156.  
  157. case "IF":
  158. this.compteurif += 1;
  159. // exemple x == 6
  160. // la condition ici ==
  161. System.out.println(tfils.getChild(0));
  162. // x
  163. System.out.println(tfils.getChild(0).getChild(0));
  164. // 6
  165. System.out.println(tfils.getChild(0).getChild(1));
  166. // BLOC DE CODE
  167. System.out.println(tfils.getChild(1));
  168. System.out.println(tfils.getChild(1).getChild(0));
  169. this.code += this.genIfEg(tfils.getChild(0).getChild(0).getText(), tfils.getChild(0).getChild(1).getText(), tfils.getChild(0).getText());
  170. this.code += "if" + this.compteurif + " \n";
  171. parcourtArbre(tds, tfils.getChild(1), 0, 0);
  172. this.code += "finif" + compteurif + " \n";
  173. break;
  174.  
  175. case "WHILE":
  176. this.compteurwhile += 1;
  177. this.code += "Debwhile" + this.compteurwhile + " \n";
  178. this.code += this.genWhile(tfils.getChild(0).getChild(0).getText(), tfils.getChild(0).getChild(1).getText(), tfils.getChild(0).getText());
  179. this.code += "while" + this.compteurwhile + " \n";
  180. parcourtArbre(tds, tfils, 2, 0);
  181. this.code += " BMP Debwhile" + compteurwhile + "-$-2\n";
  182. this.code += "finwhile" + compteurwhile + " \n";
  183. for (int l = 0; l < tfils.getChildCount(); l++) {
  184. System.out.println("Ici : " + tfils.getChild(l).getChild(0).getText());
  185. }
  186. parcourtArbre(tds, tfils, 0, tfils.getChildCount() - 2);
  187. break;
  188.  
  189. case "PRINT":
  190.  
  191. sb = (DeclarationSymbole) tds.getSymbole(tfils.getChild(0).getText());
  192. this.code += this.genCodePrintInt(sb.depl);
  193. break;
  194.  
  195. case "APPLFUNCT":
  196. ArrayList<Integer> atrib = new ArrayList<Integer>();
  197. if (tfils.getChild(1).getText().equals("PARAMS")) {
  198. for (int h = 0; h < tfils.getChild(1).getChildCount(); h++) {
  199. System.out.println("test" + tfils.getChild(1).getChild(h));
  200. sb = (DeclarationSymbole) tds.getSymbole(tfils.getChild(1).getChild(h).getText());
  201. atrib.add(sb.depl);
  202. }
  203. }
  204. this.code += this.genAplFct(tfils.getChild(0).getText(), atrib);
  205. sb = (DeclarationSymbole) tds.getSymbole(tfils.getParent().getChild(1).getText());
  206. this.code += genRegistredansVariable(sb.depl, 0);
  207. break;
  208.  
  209. default:
  210. parcourtArbre(tds, tfils, 0, 0);
  211. }
  212. }
  213. }
  214. }
  215.  
  216. public String genFichierMain() {
  217. return "// FICHIER HEAD \n" +
  218. "sp equ r15\n" +
  219. "wr equ r14\n" +
  220. "bp equ r13\n" +
  221. "SP equ r15\n" +
  222. "WR equ r14\n" +
  223. "BP equ r13\n" +
  224. "EXIT_EXC EQU 64\n" +
  225. "READ_EXC EQU 65\n" +
  226. "WRITE_EXC EQU 66\n" +
  227. "NUL equ 0 \n" +
  228. "NULL equ 0 \n" +
  229. "NIL equ 0\n" +
  230. "STACK_ADRS equ 0x1000\n" +
  231. "LOAD_ADRS equ 0xFE00\n" +
  232. " org LOAD_ADRS\n" +
  233. " start main_\n" +
  234. "ASCII_PLUS equ 43\n" +
  235. "ASCII_SPACE equ 32\n" +
  236. "ASCII_MINUS equ 45\n" +
  237. "ASCII_ZERO equ 48\n" +
  238. "printint_ NOP\n" +
  239. " ADQ -2,SP\n" +
  240. " LDW R1,SP\n" +
  241. " LDW R2,#0\n" +
  242. " STB R2,(SP)\n" +
  243. " ADQ -1,SP\n" +
  244. " LDW R2,#10\n" +
  245. " STB R2,(SP)\n" +
  246. " ADQ -1,SP\n" +
  247. " LDQ ASCII_PLUS, R5\n" +
  248. " TST R0\n" +
  249. " BGE POSIT-$-2\n" +
  250. " NEG R0, R0\n" +
  251. " LDQ ASCII_MINUS, R5\n" +
  252. "POSIT NOP\n" +
  253. " LDW R3,#10\n" +
  254. "CONV NOP\n" +
  255. " LDW R4,R0\n" +
  256. " DIV R4,R3,R0\n" +
  257. " LDQ ASCII_ZERO,R2\n" +
  258. " ADD R2,R4,R2\n" +
  259. " STB R2,(SP)\n" +
  260. " ADQ -1,SP\n" +
  261. " TST R0\n" +
  262. " JEQ #END_P-$-2\n" +
  263. " JMP #CONV-$-2\n" +
  264. "END_P NOP\n" +
  265. " STB R5,(SP)\n" +
  266. " ADQ -1,SP\n" +
  267.  
  268. " LDW R0,SP\n" +
  269. " ADQ 1,R0\n" +
  270. " TRP #WRITE_EXC\n" +
  271. " LDW SP,R1\n" +
  272. " ADQ 2,SP\n" +
  273. " JEA (WR)\n" +
  274. "print_ LDQ 0,R1\n" +
  275. " ADQ -2, SP\n" +
  276. " STW BP, (SP)\n" +
  277. " LDW BP, SP\n" +
  278. " SUB SP, R1, SP\n" +
  279. " LDW R0, BP\n" +
  280. " ADQ 4,R0\n" +
  281. " LDW R0, (R0)\n" +
  282. " LDW WR, #WRITE_EXC\n" +
  283. " TRP WR\n" +
  284. " LDW SP, BP\n" +
  285. " LDW BP, (SP)\n" +
  286. " ADQ 2,SP\n" +
  287. " LDW WR, (SP)\n" +
  288. " ADQ 2, SP\n" +
  289. " JEA (WR)\n";
  290.  
  291. }
  292.  
  293. public String genCodeMainHead() {
  294. return "// MAIN HEAD \n" +
  295. "main_ ldw sp, #STACK_ADRS\n" +
  296. " ldw bp, #NIL\n";
  297.  
  298. }
  299.  
  300.  
  301. public String genAplFct(String nom, ArrayList<Integer> atrib) {
  302.  
  303. FonctionSymbole fs = (FonctionSymbole) tds.getSymbole(nom);
  304. String arg = "";
  305. for (int i = 0; i < atrib.size(); i++) {
  306.  
  307. arg +=
  308. "//CHARGE " + atrib.get(i) + " \n" +
  309. " LDW WR, BP\n" +
  310. " ADQ -" + atrib.get(i) + ", WR\n" +
  311. " LDW R1, (WR)\n" +
  312. "//EMPILE " + atrib.get(i) + " \n" +
  313. " ADQ -2, SP \n" +
  314. " STW R1, (SP) \n";
  315.  
  316.  
  317. }
  318. return arg +
  319. " // APL FCT " + nom + "\n" +
  320. " LDW R1, #" + nom + "\n" +
  321. " MPC WR\n" +
  322. " ADQ 8, WR\n" +
  323. " ADQ -2, SP\n" +
  324. " STW WR, (SP)\n" +
  325. " JEA(R1)\n" +
  326. " ADQ 2*2, SP\n";
  327.  
  328. }
  329.  
  330. public String genCodeMainTail() {
  331.  
  332. return "// MAIN TAIL \n" +
  333. " ldw SP, BP\n" +
  334. " ldw BP, (SP)\n" +
  335. " ADQ 2, SP\n" +
  336. " TRP #EXIT_EXC \n" +
  337. " JEA @main_\n";
  338.  
  339. }
  340.  
  341. public String genCodePrintString(String g) {
  342. return "// PRINT STRING \n" +
  343. "TOTO string \"" + g + "\"\n" +
  344. " LDW R0, #TOTO\n" +
  345. " STW R0, -(SP)\n" +
  346. " JSR @print_\n";
  347. }
  348.  
  349. public String genCodePrintInt(int depl) {
  350. String depla = Integer.toString(depl);
  351. return "// PRINT INT \n" +
  352. this.genVariabledansRegistre(depl, 0) +
  353.  
  354. " LDW R1, #printint_\n" + // appel de la fct
  355. " MPC WR\n" +
  356. " ADQ 8,WR\n" +
  357. " ADQ -2,SP\n" +
  358. " STW WR, (SP)\n" +
  359. " JEA (R1)\n";
  360. }
  361.  
  362. public String genFctHead(Node n) {
  363. return "// FCT HEAD ( " + n.getId() + " )\n" +
  364. n.getId() + " LDQ " + n.getDepl() + ", R1\n" +
  365. " ADQ -2, SP\n" +
  366. " STW BP, (SP) \n" +
  367. " LDW BP, SP \n" +
  368. " SUB SP, R1, SP\n";
  369.  
  370. }
  371.  
  372. public String genFctReturn(int depl) {
  373. return "// FCT RETURN \n" +
  374. " LDW WR, BP\n" +
  375. " ADQ -" + depl + ", WR \n" +
  376. " LDW R0, (WR)\n";
  377.  
  378.  
  379. }
  380.  
  381. public String genFctTail(Node n) {
  382. return "// FCT TAIL ( " + n.getId() + " )\n" +
  383. " LDW SP, BP\n" +
  384. " LDW BP, (SP)\n" +
  385. " ADQ 2, SP \n" +
  386. // retour appelant
  387. " LDW WR, (SP)\n" +
  388. " ADQ 2, SP \n" +
  389. " JEA (WR) \n";
  390.  
  391. }
  392.  
  393.  
  394. public String genDeclarationVariable(Node n) {
  395. String code = "";
  396. int depla = n.getDepl();
  397. // calcul deplacement
  398.  
  399.  
  400. if (depla > 0) {
  401. code += "// DECLARATION VAR \n" +
  402. " LDQ " + Integer.toString(depla) + ", R1\n" +
  403. " ADQ -2, SP\n" +
  404. " STW BP, (SP)\n" +
  405. " LDW BP, SP\n" +
  406. " SUB SP, R1, SP\n";
  407. return code;
  408. } else {
  409. return "";
  410. }
  411. }
  412.  
  413. public String genValdansRegistre(int val,int reg){
  414. return "// Store " + val + "dans \n" +
  415. " LDW R"+reg+", #" + Integer.toString(val) + " \n" ;
  416. }
  417.  
  418. public String genAffectationVal(DeclarationSymbole vs, int val) {
  419. return "// AFFECTATION " + vs.nom + " -> " + val + "\n" +
  420. " LDW R1, #" + Integer.toString(val) + " \n" +
  421. " LDW WR, BP\n" +
  422. " ADQ -" + vs.getDepl() + ", WR\n" +
  423. " STW R1, (WR)\n";
  424. }
  425.  
  426. public String genVariabledansRegistre(int depl, int numr) {
  427. // return le code asm mettant la valeur de la var de deplacement depl dans le registre de num numr
  428. return "// AFFECTATION du registre " + numr + " \n" +
  429. " LDW WR, BP\n" +
  430. " ADQ -" + depl + ",WR\n" +
  431. " LDW R" + numr + ", (WR)\n";
  432. }
  433.  
  434. public String genRegistredansVariable(int depl, int numr) {
  435. // return le code asm mettant la valeur de la var de deplacement depl dans le registre de num numr
  436. return "// AFFECTATION du registre " + numr + " \n" +
  437. " LDW WR, BP\n" +
  438. " ADQ -" + depl + ",WR\n" +
  439. " STW R" + numr + ", (WR)\n";
  440. }
  441.  
  442. public String genOp(int depl1, int depl2, String op) {
  443. // return le code asm mettant la valeur de la var de deplacement depl dans le registre de num numr
  444. String ope = "";
  445. switch (op) {
  446. case ("+"):
  447. ope = "ADD R1,R2,R1";
  448. break;
  449. case ("-"):
  450. ope = "SUB R1,R2,R1";
  451. break;
  452. case ("*"):
  453. ope = "MUL R1,R2,R1";
  454. break;
  455. case ("/"):
  456. ope = "DIV R1,R2,R1";
  457. break;
  458. default:
  459. break;
  460. }
  461. return "// OP ARITHM " + op + " \n" +
  462. " LDW WR, BP\n" +
  463. " ADQ " + depl1 + ",WR\n" +
  464. " LDW R1, (WR)\n" +
  465. " LDW WR, BP\n" +
  466. " ADQ " + depl2 + ",WR\n" +
  467. " LDW R2, (WR)\n" +
  468. " " + ope + "\n";
  469. }
  470.  
  471. public String genIfEg(String val, String n, String eq) {
  472. String s = "//IF " + val + eq + n + " \n" +
  473. " LDW R6, #" + n + " \n" +
  474. " CMP R1, R6\n";
  475. if (eq.equals("==")) {
  476. s += " BEQ " + "if" + compteurif + "-$-2\n" +
  477. " BNE finif" + compteurif + "-$-2\n";
  478. } else if (eq.equals(">=")) {
  479. s += " BAE " + "if" + compteurif + "-$-2\n" +
  480. " BBL finif" + compteurif + "-$-2\n";
  481. } else if (eq.equals("<=")) {
  482. s += " BBE " + "if" + compteurif + "-$-2\n" +
  483. " BAB finif" + compteurif + "-$-2\n";
  484. } else if (eq.equals(">")) {
  485. s += " BAB " + "if" + compteurif + "-$-2\n" +
  486. " BBE finif" + compteurif + "-$-2\n";
  487. } else if (eq.equals("<")) {
  488. s += " BBL " + "if" + compteurif + "-$-2\n" +
  489. " BAE finif" + compteurif + "-$-2\n";
  490. } else if (eq.equals("!=")) {
  491. s += " BNE " + "if" + compteurif + "-$-2\n" +
  492. " BEQ finif" + compteurif + "-$-2\n";
  493. }
  494. return s;
  495. }
  496.  
  497. public String genWhile(String val, String n, String eq) {
  498. String s = "//WHILE " + val + eq + n + " \n" +
  499. " LDW R6, #" + n + " \n" +
  500. " CMP R1, R6\n";
  501. if (eq.equals("==")) {
  502. s += " BEQ " + "while" + compteurwhile + "-$-2\n" +
  503. " BNE finwhile" + compteurwhile + "-$-2\n";
  504. } else if (eq.equals(">=")) {
  505. s += " BAE " + "while" + compteurwhile + "-$-2\n" +
  506. " BBL finwhile" + compteurwhile + "-$-2\n";
  507. } else if (eq.equals("<=")) {
  508. s += " BBE " + "while" + compteurwhile + "-$-2\n" +
  509. " BAB finwhile" + compteurwhile + "-$-2\n";
  510. } else if (eq.equals(">")) {
  511. s += " BAB " + "while" + compteurwhile + "-$-2\n" +
  512. " BBE finwhile" + compteurwhile + "-$-2\n";
  513. } else if (eq.equals("<")) {
  514. s += " BBL " + "while" + compteurwhile + "-$-2\n" +
  515. " BAE finwhile" + compteurwhile + "-$-2\n";
  516. } else if (eq.equals("!=")) {
  517. s += " BNE " + "while" + compteurwhile + "-$-2\n" +
  518. " BEQ finwhile" + compteurwhile + "-$-2\n";
  519. }
  520. return s;
  521. }
  522.  
  523. public String getCode() {
  524. return code;
  525. }
  526.  
  527. public void setCode(String code) {
  528. this.code = code;
  529. }
  530.  
  531.  
  532. public boolean estUnEntier(String chaine) {
  533. try {
  534. Integer.parseInt(chaine);
  535. } catch (NumberFormatException e) {
  536. return false;
  537. }
  538.  
  539. return true;
  540. }
  541.  
  542.  
  543. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement