TargetGen.java 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  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 common.Block;
  8. import common.Code;
  9. import common.Instruction;
  10. import common.IvannosysTargetArch;
  11. import java.util.ArrayList;
  12. import java.util.HashMap;
  13. import java.util.LinkedList;
  14. import java.util.Map;
  15. import java.util.Set;
  16. import java.util.logging.Level;
  17. import java.util.logging.Logger;
  18. /**
  19. *
  20. * @author EUGENIO CARVALHO
  21. */
  22. public abstract class TargetGen implements IvannosysTargetArch {
  23. protected HashMap<String, ExportInterface> exportMethods = new HashMap<>();
  24. protected Code target;
  25. protected Code IR;
  26. protected Instruction prev = null, next = null;
  27. public TargetGen(String id) {
  28. BaseGenInit(id);
  29. }
  30. public void BaseGenInit(String id) {
  31. target = new Code(id);
  32. }
  33. public Code getTarget() {
  34. return target;
  35. }
  36. public void setTarget(Code target) {
  37. this.target = target;
  38. }
  39. public Code getIR() {
  40. return IR;
  41. }
  42. public ExportInterface GetExportMethod(String id) throws Exception {
  43. id = id.trim();
  44. if (!exportMethods.containsKey(id)) {
  45. Set<String> set = exportMethods.keySet();
  46. String[] valids = new String[set.size()];
  47. set.toArray(valids);
  48. throw new Exception(String.format(
  49. "O método de exportação '%s' não foi definido no alvo '%s'. Métodos disponiveis: %s",
  50. id,
  51. target.Name(),
  52. Utils.Join(valids, ", ")));
  53. }
  54. return exportMethods.get(id);
  55. }
  56. public HashMap<String, ExportInterface> GetExportMethods() {
  57. return exportMethods;
  58. }
  59. public IvannosysTargetArch SetTAC(Code tac) throws Exception {
  60. // Verifica se o codigo gerado vai ser util caso contrario aborta o processo;
  61. ArrayList<String> methods = BuildParams.Get("export");
  62. if (methods.isEmpty()) {
  63. throw new Exception(String.format("Nenhum método de exportação definido para o alvo '%s'", target.Name()));
  64. }
  65. IR = tac;
  66. target.GlobalData().copy(tac.GlobalData());
  67. target.Parent = IR;
  68. return this;
  69. }
  70. protected Instruction Add(Instruction inst) {
  71. target.Block().Add(inst);
  72. return inst;
  73. }
  74. public TargetGen AddExportOption(String id, ExportInterface exp) {
  75. exportMethods.put(id.trim(), exp);
  76. return this;
  77. }
  78. @Override
  79. public IvannosysTargetArch Format() {
  80. try {
  81. String code = target.getCodeStream();
  82. System.out.println("Target[" + target.Name() + "]:\n" + code);
  83. } catch (Exception ex) {
  84. Logger.getLogger(TargetGen.class.getName()).log(Level.SEVERE, null, ex);
  85. }
  86. return this;
  87. }
  88. @Override
  89. public IvannosysTargetArch Compile() {
  90. try {
  91. // Traduz o codigo intermediario para o alvo
  92. Translate();
  93. } catch (Exception ex) {
  94. Logger.getLogger(TargetGen.class.getName()).log(Level.SEVERE, null, ex);
  95. }
  96. return this;
  97. }
  98. protected void Translate() throws Exception {
  99. String targetID = target.Name(), id;
  100. target.Parent = IR;
  101. CodeProcessor.Trigger(target, targetID, "BeforeTranslate");
  102. for (Map.Entry<String, Block> b : IR.getBlocks().entrySet()) {
  103. id = b.getKey();
  104. IR.Use(id);
  105. target.OpenBlock(id);
  106. CodeProcessor.Trigger(target, targetID, "BeforeTranslateBlock");
  107. Prolog(id);
  108. TranslateBlock(b.getValue());
  109. Epilog(id);
  110. target.CloseBlock();
  111. target.UpdatePositions();
  112. CodeProcessor.Trigger(target, targetID, "AfterTranslateBlock");
  113. }
  114. CodeProcessor.Trigger(target, targetID, "AfterTranslate");
  115. }
  116. @Override
  117. public IvannosysTargetArch Export() throws Exception {
  118. ArrayList<String> methods = BuildParams.Get("export");
  119. for (String method : methods) {
  120. GetExportMethod(method).Exec(IR, target);
  121. }
  122. return this;
  123. }
  124. protected void TranslateBlock(Block b) throws Exception {
  125. // System.out.println("Translate block:" + b.getName() + "::" + b.Instructions().size());
  126. LinkedList<Instruction> instructions = b.Instructions();
  127. Integer index = 0, limit = instructions.size(), nextIndex;
  128. for (Instruction inst : instructions) {
  129. nextIndex = index + 1;
  130. next = (nextIndex < limit) ? instructions.get(nextIndex) : null;
  131. // System.out.println("Translate: " + inst.Get("global.position") + "::read["
  132. // + inst.Get("reg.dst.load")
  133. // + "|"
  134. // + inst.Get("reg.p1.load")
  135. // + "|"
  136. // + inst.Get("reg.p2.load")
  137. // + "] >> write["
  138. // + inst.Get("reg.dst.store")
  139. // + "|"
  140. // + inst.Get("reg.p1.store")
  141. // + "|"
  142. // + inst.Get("reg.p2.store")
  143. // + "]"
  144. // + inst.Get("type")
  145. // );
  146. //
  147. switch (inst.Get("type")) {
  148. case "label":
  149. if (!inst.Get("label").equals(getTarget().Block().getName())) {
  150. TranslateLabel(inst);
  151. }
  152. break;
  153. case "assign":
  154. TranslateAssign(inst);
  155. break;
  156. case "unary":
  157. TranslateUnary(inst);
  158. break;
  159. case "copy":
  160. TranslateCopy(inst);
  161. break;
  162. case "jump":
  163. TranslateJump(inst);
  164. break;
  165. case "call":
  166. TranslateCall(inst);
  167. break;
  168. case "return":
  169. TranslateReturn(inst);
  170. break;
  171. case "push_param":
  172. TranslatePushParam(inst);
  173. break;
  174. case "push_return":
  175. TranslatePushReturn(inst);
  176. break;
  177. case "pop_return":
  178. TranslatePopReturn(inst);
  179. break;
  180. case "pop_param":
  181. TranslatePopParam(inst);
  182. break;
  183. case "branch":
  184. TranslateBranch(inst);
  185. break;
  186. case "indexed_assign":
  187. TranslateIndexedAssignment(inst);
  188. break;
  189. case "pointer_assign":
  190. TranslatePointerAssignment(inst);
  191. break;
  192. case "store":
  193. TranslateStore(inst);
  194. break;
  195. case "load":
  196. TranslateLoad(inst);
  197. break;
  198. default:
  199. throw new Exception(String.format("TAC instruction type '%s' not defined", inst.Get("type")));
  200. }
  201. prev = inst;
  202. index++;
  203. }
  204. }
  205. protected Instruction Next() {
  206. return next;
  207. }
  208. protected Instruction Prev() {
  209. return prev;
  210. }
  211. }