MifFactory.java 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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 Output;
  7. import API.Api;
  8. import API.Traps;
  9. import remove.FormatadorMips;
  10. import remove.MipsCodeGen;
  11. import IntermediaryCode.TestInstruction;
  12. import common.Instruction;
  13. import API.Utils;
  14. import java.io.IOException;
  15. import java.util.ArrayList;
  16. import java.util.HashMap;
  17. import java.util.Map;
  18. /**
  19. *
  20. * @author Eugenio
  21. */
  22. public class MifFactory {
  23. protected MipsCodeGen code;
  24. protected FormatadorMips fm;
  25. protected ArrayList<Instruction> blocoAtual;
  26. protected ArrayList<Instruction> bloco;
  27. protected boolean hex = false;
  28. private final TestInstruction testIntruction;
  29. protected Mif mifAtual;
  30. public MifFactory(MipsCodeGen code) {
  31. this.code = code;
  32. testIntruction = new TestInstruction();
  33. // hex = true;
  34. // fm = new FormatadorMips();
  35. // fm.setLabels(code.labels);
  36. }
  37. public void usarBloco(String nome) {
  38. code.use(nome);
  39. }
  40. public void criarTodos() throws Exception {
  41. for (Map.Entry<String, ArrayList<Instruction>> bloco : code.blocks.entrySet()) {
  42. criar(bloco.getKey());
  43. }
  44. }
  45. public Mif gerarMif(ArrayList<Instruction> bloco, Mif mif, String path) throws IOException {
  46. blocoAtual = new ArrayList<Instruction>(bloco);
  47. _gerarMif(blocoAtual, mif, path, 0);
  48. return mif;
  49. }
  50. protected void _gerarMif(ArrayList<Instruction> lista, Mif mif, String path, int count) throws IOException {
  51. try {
  52. ArrayList<Instruction> tmp = new ArrayList<>(lista);
  53. for (Instruction instrucao : tmp) {
  54. mif.Add(extrair(instrucao));
  55. lista.remove(instrucao);
  56. }
  57. } catch (Exception e) {
  58. System.err.println(e.getMessage());
  59. e.printStackTrace();
  60. _gerarMif(lista, new Mif(mif.size), path, count + 1);
  61. }
  62. mif.save(path + "_" + count);
  63. }
  64. public MipsCodeGen code() {
  65. return code;
  66. }
  67. public Mif criarCodeStream() throws Exception {
  68. Mif mif = new Mif();
  69. mif.setHexMode(hex);
  70. for (Map.Entry<String, ArrayList<Instruction>> bloco : code.blocks.entrySet()) {
  71. for (Instruction instrucao : bloco.getValue()) {
  72. mif.Add(extrair(instrucao));
  73. }
  74. }
  75. System.out.println("mif[]:\n" + mif);
  76. return mif;
  77. }
  78. public Mif criarDataStream() throws Exception {
  79. System.out.println("Gerando memória de dados............................");
  80. Mif mif = new Mif(1024);
  81. mif.setHexMode(hex);
  82. // Offset of = code.getCodigoTresEnderecos().gOffset;
  83. // System.out.println("of:" + of);
  84. // HashMap<String, String> valores = of.getValoresInicializados();
  85. // Instruction r;
  86. // String original;
  87. // String endereco;
  88. // for (Map.Entry<String, String> entry : valores.entrySet()) {
  89. // String var = entry.getKey();
  90. // String valor = entry.getValue();
  91. // original = Code.reverseLabel(var);
  92. //// r.S("text", num2bin(valor, 32) + ";\t-- " + formatVar(var) + " -> " + original);
  93. // endereco = var.split(",")[0];
  94. //// completarComZero(mif);
  95. //
  96. // mif.Add(endereco, criarLinha(Api.num2bin(valor, 32) + ";\t-- " + formatVar(var) + " -> " + original));
  97. // }
  98. // System.out.println(mif);
  99. return mif;
  100. }
  101. protected Instruction criarLinha(String linha) {
  102. Instruction r = new Instruction();
  103. r.S("text", linha);
  104. return r;
  105. }
  106. public void criar(String nome) throws Exception {
  107. usarBloco(nome);
  108. bloco = code.current;
  109. Mif mif = new Mif();
  110. mif.setHexMode(hex);
  111. for (Instruction registro : bloco) {
  112. mif.Add(extrair(registro));
  113. }
  114. System.out.println("mif[" + nome + "]:\n" + mif);
  115. }
  116. protected String formatVar(String var) {
  117. String parts[] = var.split("\\.");
  118. if (parts.length > 1) {
  119. return parts[0] + "[" + parts[1] + "]";
  120. }
  121. return parts[0];
  122. }
  123. protected Instruction extrair(Instruction r) throws Exception {
  124. Instruction r2 = new Instruction();
  125. r2.copiar("type", r);
  126. r2.copiar("codop", r);
  127. r2.S("comment", ";\t-- " + r.G("mipsInstruction"));
  128. switch (r.G("type")) {
  129. case "R":
  130. _R(r, r2);
  131. break;
  132. case "I":
  133. _I(r, r2);
  134. break;
  135. case "J":
  136. _J(r, r2);
  137. break;
  138. }
  139. return r2;
  140. }
  141. protected void _R(Instruction r, Instruction r2) throws Exception {
  142. // System.out.println("R:" + r);
  143. r2.S("rs", Api.reg2bin5(r.G("rs")));
  144. r2.S("rt", Api.reg2bin5(r.get("rt", "zero")));
  145. r2.S("rd", Api.reg2bin5(r.get("rd", "zero")));
  146. r2.S("sa", Api.num2bin(r.get("sa", "0"), 5));
  147. // r2.S("func", num2bin(r.G("func", "0"), 6));
  148. r2.S("func", r.get("func", "0"));
  149. testIntruction.testar(r2, "R");
  150. }
  151. protected void _I(Instruction r, Instruction r2) throws Exception {
  152. // System.out.println("I:" + r);
  153. if (r.isNumber("rs")) {
  154. r2.S("rs", Api.num2bin(r.G("rs"), 5));
  155. } else {
  156. r2.S("rs", Api.reg2bin5(r.G("rs")));
  157. }
  158. if (r.isNumber("rt")) {
  159. r.S("rd", r.G("rt"));
  160. r2.S("rt", Api.reg2bin5("zero"));
  161. // System.out.println("entrei no numerico:" + r.G("rt"));
  162. } else {
  163. r2.S("rt", Api.reg2bin5(r.G("rt")));
  164. }
  165. r2.S("constant", Api.num2bin(r.G("rd"), 16));
  166. testIntruction.testar(r2, "I");
  167. // System.out.println("Iadd:" + r2);
  168. }
  169. protected void _J(Instruction r, Instruction r2) throws Exception {
  170. // System.out.println("J:" + r);
  171. String target;
  172. String label = r.G("rs");
  173. if (r.isNumber("rs")) {
  174. target = Api.num2bin(label, 26);
  175. } else {
  176. target = Api.reg2Bin(label, 26);
  177. }
  178. r2.S("target", target);
  179. testIntruction.testar(r2, "J");
  180. }
  181. public ArrayList<Mif> criarTraps(boolean save) throws Exception {
  182. Mif mif = null;
  183. ArrayList<Mif> mifs = new ArrayList<Mif>();
  184. String blocoName = "";
  185. for (Map.Entry<String, ArrayList<Instruction>> bloco : code.blocks.entrySet()) {
  186. mif = new Mif();
  187. blocoName = bloco.getKey();
  188. if (!Traps.isTrap(blocoName)) {
  189. continue;
  190. }
  191. for (Instruction instrucao : bloco.getValue()) {
  192. mif.Add(extrair(instrucao));
  193. }
  194. mifs.add(mif);
  195. if (save) {
  196. mif.save(blocoName + ".mif");
  197. }
  198. }
  199. return mifs;
  200. }
  201. }