Gen.java 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. /*
  2. * To change this license header, choose License Headers in Project Properties.
  3. * To change this template file, choose Tools | Templates
  4. * and open the template in the editor.
  5. */
  6. package API;
  7. import IntermediaryCode.Block;
  8. import IntermediaryCode.Code;
  9. import IntermediaryCode.CodeProcessing;
  10. import compiler.IvannosysTargetArch;
  11. import java.util.LinkedHashMap;
  12. import java.util.Map;
  13. import java.util.logging.Level;
  14. import java.util.logging.Logger;
  15. /**
  16. *
  17. * @author EUGENIO CARVALHO
  18. */
  19. public abstract class Gen implements IvannosysTargetArch {
  20. protected Code target;
  21. protected Code tac;
  22. protected LinkedHashMap<String, CodeProcessing> beforeTranslate = new LinkedHashMap<>();
  23. protected LinkedHashMap<String, CodeProcessing> beforeTranslateBlock = new LinkedHashMap<>();
  24. protected LinkedHashMap<String, CodeProcessing> afterTranslate = new LinkedHashMap<>();
  25. protected LinkedHashMap<String, CodeProcessing> afterTranslateBlock = new LinkedHashMap<>();
  26. public Gen(String id) {
  27. target = new Code(id);
  28. }
  29. public Code getTarget() {
  30. return target;
  31. }
  32. public void setTarget(Code target) {
  33. this.target = target;
  34. }
  35. public Code getTac() {
  36. return tac;
  37. }
  38. // public void setTac(Code tac) {
  39. // this.tac = tac;
  40. // }
  41. public IvannosysTargetArch SetTAC(Code tac) {
  42. this.tac = tac;
  43. target.GData().copy(tac.GData());
  44. return this;
  45. }
  46. protected Instruction Add(Instruction inst) {
  47. target.Block().Add(inst);
  48. return inst;
  49. }
  50. @Override
  51. public IvannosysTargetArch Format() {
  52. try {
  53. String code = target.getCodeStream();
  54. System.out.println("Target[" + target.Name() + "]:\n" + code);
  55. } catch (Exception ex) {
  56. Logger.getLogger(Gen.class.getName()).log(Level.SEVERE, null, ex);
  57. }
  58. return this;
  59. }
  60. @Override
  61. public IvannosysTargetArch Compile() {
  62. try {
  63. // Traduz o codigo intermediario para o alvo
  64. Translate();
  65. } catch (Exception ex) {
  66. Logger.getLogger(Gen.class.getName()).log(Level.SEVERE, null, ex);
  67. }
  68. return this;
  69. }
  70. protected void Translate() throws Exception {
  71. String id;
  72. BeforeTranslate();
  73. for (Map.Entry<String, Block> b : tac.getBlocks().entrySet()) {
  74. id = b.getKey();
  75. tac.Use(id);
  76. BeforeTranslateBlock();
  77. // AllocatorRegister.Alloc(tac);
  78. target.OpenBlock(id);
  79. Prolog(id);
  80. TranslateBlock(b.getValue());
  81. Epilog(id);
  82. target.CloseBlock();
  83. target.UpdatePositions();
  84. AfterTranslateBlock();
  85. }
  86. AfterTranslate();
  87. }
  88. public Gen BeforeTranslate(String id, CodeProcessing processor) {
  89. beforeTranslate.put(id, processor);
  90. return this;
  91. }
  92. public Gen BeforeTranslateBlock(String id, CodeProcessing processor) {
  93. beforeTranslateBlock.put(id, processor);
  94. return this;
  95. }
  96. public Gen AfterTranslate(String id, CodeProcessing processor) {
  97. afterTranslate.put(id, processor);
  98. return this;
  99. }
  100. public Gen AfterTranslateBlock(String id, CodeProcessing processor) {
  101. afterTranslateBlock.put(id, processor);
  102. return this;
  103. }
  104. protected Gen BeforeTranslate() throws Exception {
  105. for (Map.Entry<String, CodeProcessing> codeProcessing : beforeTranslate.entrySet()) {
  106. codeProcessing.getValue().Exec(tac, beforeTranslate);
  107. }
  108. return this;
  109. }
  110. protected Gen BeforeTranslateBlock() throws Exception {
  111. for (Map.Entry<String, CodeProcessing> codeProcessing : beforeTranslateBlock.entrySet()) {
  112. codeProcessing.getValue().Exec(tac, beforeTranslateBlock);
  113. }
  114. return this;
  115. }
  116. protected Gen AfterTranslate() throws Exception {
  117. // System.out.println("AfterTranslate:" + afterTranslate.entrySet().size());
  118. for (Map.Entry<String, CodeProcessing> codeProcessing : afterTranslate.entrySet()) {
  119. codeProcessing.getValue().Exec(target, afterTranslate);
  120. }
  121. return this;
  122. }
  123. protected Gen AfterTranslateBlock() throws Exception {
  124. for (Map.Entry<String, CodeProcessing> codeProcessing : afterTranslateBlock.entrySet()) {
  125. codeProcessing.getValue().Exec(target, afterTranslateBlock);
  126. }
  127. return this;
  128. }
  129. protected void TranslateBlock(Block b) throws Exception {
  130. // System.out.println("Translate block:" + b.getName() + "::" + b.Instructions().size());
  131. for (Instruction inst : b.Instructions()) {
  132. // System.out.println("Translate: " + inst.G("global.position") + "::read["
  133. // + inst.G("reg.dst.load")
  134. // + "|"
  135. // + inst.G("reg.p1.load")
  136. // + "|"
  137. // + inst.G("reg.p2.load")
  138. // + "] >> write["
  139. // + inst.G("reg.dst.store")
  140. // + "|"
  141. // + inst.G("reg.p1.store")
  142. // + "|"
  143. // + inst.G("reg.p2.store")
  144. // + "]"
  145. // + inst.G("type")
  146. // );
  147. //
  148. switch (inst.G("type")) {
  149. case "label":
  150. if (!inst.G("label").equals(getTarget().Block().getName())) {
  151. TranslateLabel(inst);
  152. }
  153. break;
  154. case "assign":
  155. TranslateAssign(inst);
  156. break;
  157. case "unary":
  158. TranslateUnary(inst);
  159. break;
  160. case "copy":
  161. TranslateCopy(inst);
  162. break;
  163. case "jump":
  164. TranslateJump(inst);
  165. break;
  166. case "call":
  167. TranslateCall(inst);
  168. break;
  169. case "return":
  170. TranslateReturn(inst);
  171. break;
  172. case "push_param":
  173. TranslatePushParam(inst);
  174. break;
  175. case "push_return":
  176. TranslatePushReturn(inst);
  177. break;
  178. case "pop_return":
  179. TranslatePopReturn(inst);
  180. break;
  181. case "pop_param":
  182. TranslatePopParam(inst);
  183. break;
  184. case "branch":
  185. TranslateBranch(inst);
  186. break;
  187. case "indexed_assign":
  188. TranslateIndexedAssignment(inst);
  189. break;
  190. case "pointer_assignment":
  191. TranslatePointerAssignment(inst);
  192. break;
  193. default:
  194. throw new Exception(String.format("TAC instruction type '%s' not defined", inst.G("type")));
  195. }
  196. }
  197. }
  198. }
  199. // protected void Exec() {
  200. // StringBuilder S = new StringBuilder();
  201. // for (Map.Entry<String, Block> entry : tac.getBlocks().entrySet()) {
  202. // TranslateBlock(entry.getValue());
  203. //
  204. // S.append("<" + entry.getKey() + ">:\n");
  205. //
  206. // for (Instruction registro : entry.getValue()) {
  207. // String line = registro.G("line");
  208. // spacer = registro.G("sentenca").equals("jump") ? "\t" : "";
  209. // inst = fm.Format(registro);
  210. // registro.S("mipsInstruction", inst);
  211. // S += line + ". " + spacer + "\t" + inst + "\t\t-- ";
  212. // S += registro.G("inst3end") + "\n";
  213. // }
  214. // }
  215. // }
  216. // StringBuilder S = new StringBuilder();
  217. // for (Map.Entry<String, Block> entry : tac.stmts.entrySet()) {
  218. // S.append("<" + entry.getKey() + ">:\n");
  219. //
  220. // for (Instruction registro : entry.getValue()) {
  221. // String line = registro.G("line");
  222. // spacer = registro.G("sentenca").equals("jump") ? "\t" : "";
  223. // inst = fm.Format(registro);
  224. // registro.S("mipsInstruction", inst);
  225. // S += line + ". " + spacer + "\t" + inst + "\t\t-- ";
  226. // S += registro.G("inst3end") + "\n";
  227. // }
  228. // }