Gen.java 8.4 KB

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