TargetGen.java 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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.Map;
  12. import java.util.logging.Level;
  13. import java.util.logging.Logger;
  14. /**
  15. *
  16. * @author EUGENIO CARVALHO
  17. */
  18. public abstract class TargetGen implements IvannosysTargetArch {
  19. protected Code target;
  20. protected Code IR;
  21. public TargetGen(String id) {
  22. BaseGenInit(id);
  23. }
  24. public void BaseGenInit(String id) {
  25. target = new Code(id);
  26. }
  27. public Code getTarget() {
  28. return target;
  29. }
  30. public void setTarget(Code target) {
  31. this.target = target;
  32. }
  33. public Code getIR() {
  34. return IR;
  35. }
  36. // public void setTac(Code IR) {
  37. // this.IR = IR;
  38. // }
  39. public IvannosysTargetArch SetTAC(Code tac) {
  40. IR = tac;
  41. target.GData().copy(tac.GData());
  42. return this;
  43. }
  44. protected Instruction Add(Instruction inst) {
  45. target.Block().Add(inst);
  46. return inst;
  47. }
  48. @Override
  49. public IvannosysTargetArch Format() {
  50. try {
  51. String code = target.getCodeStream();
  52. System.out.println("Target[" + target.Name() + "]:\n" + code);
  53. } catch (Exception ex) {
  54. Logger.getLogger(TargetGen.class.getName()).log(Level.SEVERE, null, ex);
  55. }
  56. return this;
  57. }
  58. @Override
  59. public IvannosysTargetArch Compile() {
  60. try {
  61. // Traduz o codigo intermediario para o alvo
  62. Translate();
  63. } catch (Exception ex) {
  64. Logger.getLogger(TargetGen.class.getName()).log(Level.SEVERE, null, ex);
  65. }
  66. return this;
  67. }
  68. protected void Translate() throws Exception {
  69. String id;
  70. IR.BeforeTranslate();
  71. for (Map.Entry<String, Block> b : IR.getBlocks().entrySet()) {
  72. id = b.getKey();
  73. IR.Use(id);
  74. IR.BeforeTranslateBlock();
  75. target.OpenBlock(id);
  76. Prolog(id);
  77. TranslateBlock(b.getValue());
  78. Epilog(id);
  79. target.CloseBlock();
  80. target.UpdatePositions();
  81. target.AfterTranslateBlock();
  82. }
  83. target.AfterTranslate();
  84. }
  85. protected void TranslateBlock(Block b) throws Exception {
  86. // System.out.println("Translate block:" + b.getName() + "::" + b.Instructions().size());
  87. for (Instruction inst : b.Instructions()) {
  88. // System.out.println("Translate: " + inst.G("global.position") + "::read["
  89. // + inst.G("reg.dst.load")
  90. // + "|"
  91. // + inst.G("reg.p1.load")
  92. // + "|"
  93. // + inst.G("reg.p2.load")
  94. // + "] >> write["
  95. // + inst.G("reg.dst.store")
  96. // + "|"
  97. // + inst.G("reg.p1.store")
  98. // + "|"
  99. // + inst.G("reg.p2.store")
  100. // + "]"
  101. // + inst.G("type")
  102. // );
  103. //
  104. switch (inst.G("type")) {
  105. case "label":
  106. if (!inst.G("label").equals(getTarget().Block().getName())) {
  107. TranslateLabel(inst);
  108. }
  109. break;
  110. case "assign":
  111. TranslateAssign(inst);
  112. break;
  113. case "unary":
  114. TranslateUnary(inst);
  115. break;
  116. case "copy":
  117. TranslateCopy(inst);
  118. break;
  119. case "jump":
  120. TranslateJump(inst);
  121. break;
  122. case "call":
  123. TranslateCall(inst);
  124. break;
  125. case "return":
  126. TranslateReturn(inst);
  127. break;
  128. case "push_param":
  129. TranslatePushParam(inst);
  130. break;
  131. case "push_return":
  132. TranslatePushReturn(inst);
  133. break;
  134. case "pop_return":
  135. TranslatePopReturn(inst);
  136. break;
  137. case "pop_param":
  138. TranslatePopParam(inst);
  139. break;
  140. case "branch":
  141. TranslateBranch(inst);
  142. break;
  143. case "indexed_assign":
  144. TranslateIndexedAssignment(inst);
  145. break;
  146. case "pointer_assign":
  147. TranslatePointerAssignment(inst);
  148. break;
  149. case "store":
  150. TranslateStore(inst);
  151. break;
  152. case "load":
  153. TranslateLoad(inst);
  154. break;
  155. default:
  156. throw new Exception(String.format("TAC instruction type '%s' not defined", inst.G("type")));
  157. }
  158. }
  159. }
  160. }