Gen.java 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  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 target.mips.jun;
  7. import API.BuildParams;
  8. import API.Instruction;
  9. import API.Utils;
  10. import IntermediaryCode.Block;
  11. import IntermediaryCode.Code;
  12. import compiler.IvannosysTargetArch;
  13. import java.util.ArrayList;
  14. import java.util.HashMap;
  15. import java.util.LinkedList;
  16. import java.util.Map;
  17. import java.util.logging.Level;
  18. import java.util.logging.Logger;
  19. import org.json.simple.JSONArray;
  20. import org.json.simple.JSONObject;
  21. import org.json.simple.parser.JSONParser;
  22. import target.mips.Mips;
  23. import target.mips.Registers;
  24. import target.mips.TemplateMips;
  25. import target.mips.UpdateAddressProcessor;
  26. /**
  27. * MipS tranSlation
  28. *
  29. * @author EUGENIO CARVALHO
  30. */
  31. public class Gen extends target.mips.Gen {
  32. public Gen() {
  33. super();
  34. Init();
  35. }
  36. @Override
  37. public IvannosysTargetArch Export() {
  38. try {
  39. Code Target = getTarget(), newCode;
  40. System.out.println("Export do juninho");
  41. Long stackBaseAddress = 0L,
  42. updateBaseAddress = 0L,
  43. size;
  44. String id,
  45. initFunction,
  46. blockName,
  47. format,
  48. cacheBlockSize = BuildParams.Get("cacheBlockSize").get(0),
  49. // base = "\\src\\results\\mipsjun\\";
  50. base = BuildParams.Get("outputDirectory").get(0);
  51. String[] parts;
  52. Block func;
  53. JSONObject profile;
  54. UpdateAddressProcessor uap = new UpdateAddressProcessor();
  55. ArrayList<String> p = BuildParams.Get("profile");
  56. if (!p.isEmpty()) {
  57. JSONArray pobject = (JSONArray) new JSONParser().parse(p.get(0));
  58. for (Object pro : pobject) {
  59. profile = (JSONObject) pro;
  60. id = (String) profile.get("id");
  61. initFunction = (String) profile.get("initFunction");
  62. parts = initFunction.replace(")", "").split("\\(");
  63. blockName = parts[0];
  64. // Busca o bloco inicial referenciado pelo usuario
  65. func = Target.getBlocks().get(blockName);
  66. if (profile.containsKey("stackBaseAddress")) {
  67. stackBaseAddress = (Long) profile.get("stackBaseAddress");
  68. } else {
  69. // System.out.println("stackBaseAddress:(" + stackBaseAddress + ")(" + MaxStackSize(func) + ")");
  70. size = MaxStackSize(func);
  71. updateBaseAddress = stackBaseAddress
  72. - size
  73. - fixCacheBlockSize(size, Long.parseLong(cacheBlockSize));
  74. }
  75. // Cria um novo codigo para armazenas as instruções do core
  76. newCode = NewCode(id);
  77. // Copia o bloco da funcao e todos os blocos que gerem dependencia
  78. CopyBlock(newCode,
  79. func,
  80. parts,
  81. stackBaseAddress,
  82. true);
  83. // Atualiza os enderecos de salto do codigo
  84. uap.Exec(newCode, null);
  85. format = (String) profile.get("filename");
  86. // Utils.WriteFile("\\src\\results\\mipsjun\\" + id + "\\main.dec.txt", code);
  87. SaveMips(newCode, base + "\\" + String.format(format, "mips"));
  88. SaveDec(newCode, base + "\\" + String.format(format, "dec"));
  89. stackBaseAddress = updateBaseAddress;
  90. // System.out.println("value::" + code);
  91. }
  92. // System.out.println("params:" + getTac().ast.getCompileParams().GetLists());
  93. // System.out.println("params:" + p);
  94. }
  95. // } catch (ParseException ex) {
  96. // Logger.getLogger(Gen.class.getName()).log(Level.SEVERE, null, ex);
  97. } catch (Exception ex) {
  98. Logger.getLogger(Gen.class.getName()).log(Level.SEVERE, null, ex);
  99. }
  100. return this;
  101. // return super.Export(); //To change body of generated methods, choose Tools | Templates.
  102. }
  103. protected Long fixCacheBlockSize(Long size, Long sizeOfBlock) {
  104. Long t = sizeOfBlock;
  105. while (true) {
  106. if (sizeOfBlock > size) {
  107. break;
  108. }
  109. sizeOfBlock += t;
  110. }
  111. return sizeOfBlock - size;
  112. }
  113. protected void SaveMips(Code newCode, String filename) throws Exception {
  114. Utils.WriteFile(filename, newCode.getCodeStream());
  115. }
  116. protected void SaveDec(Code newCode, String filename) throws Exception {
  117. String code = "", tmp;
  118. for (Map.Entry<String, Block> x : newCode.stmts.entrySet()) {
  119. for (Instruction instr : x.getValue().Instructions()) {
  120. tmp = instr.G("inst.dec");
  121. if (tmp.equals("")) {
  122. continue;
  123. }
  124. code = code.concat(Long.parseLong(tmp) + "\n");
  125. }
  126. }
  127. Utils.WriteFile(filename, code);
  128. }
  129. protected Code CopyBlock(Code newCode, Block func, String[] funcCall, Long stackBaseAddress, boolean main) throws Exception {
  130. // Instruction Ninst;
  131. int index = 1;
  132. String blockName = funcCall[0];
  133. newCode.OpenBlock(blockName);
  134. Block block = newCode.Block();
  135. // Carrega os argumentos da chamada
  136. if (funcCall.length > 1) {
  137. // System.out.println("Tinha parametro:" + funcCall[1]);
  138. String[] args = funcCall[1].split(",");
  139. int i = 0;
  140. for (String arg : args) {
  141. // Ninst = new Instruction();
  142. // Variaveis.List();
  143. // System.out.println("Variaveis.Get(arg):" + Variaveis.get("main." + args));
  144. block.Add(Mips.Instruction("addiu")
  145. .S("rs", Registers.R_ZERO)
  146. .S("rt", "a" + (i++))
  147. .S("offset", arg));
  148. }
  149. index += args.length;
  150. }
  151. for (Instruction inst : func.Instructions()) {
  152. block.Add(inst.copy());
  153. }
  154. if (main) {
  155. LinkedList<Instruction> instructions = newCode.Block().Instructions();
  156. // Atualiza o offset da pilha
  157. Instruction first = instructions.get(index);
  158. // System.out.println("CopyBlock[" + stackBaseAddress + "||" + first.getLong("offset") + "]");
  159. first.set("offset", first.getLong("offset") + stackBaseAddress);
  160. // System.out.println("instructions.get(0);" + first);
  161. // Atualiza a ultima instrucao de um retorno para ra -> stop
  162. block.Remove(instructions.get(instructions.size() - 1));
  163. block.Add(Mips.Instruction("stop")
  164. .S("comment", "end of programa"));
  165. }
  166. newCode.CloseBlock();
  167. // Adiciona as funcoes que geram dependencias
  168. HashMap<String, Block> tb = getTarget().getBlocks();
  169. ArrayList<String> deps = tb.get(blockName).GetDependences();
  170. if (!deps.isEmpty()) {
  171. for (String dep : deps) {
  172. CopyBlock(newCode, tb.get(dep), new String[]{dep}, 0l, false);
  173. }
  174. }
  175. return newCode;
  176. }
  177. protected Code NewCode(String id) {
  178. return new Code(id)
  179. .Template(new TemplateMips())
  180. .Formats(formatMips);
  181. }
  182. protected Long MaxStackSize(Block blk) {
  183. HashMap<String, Block> tb = getTarget().getBlocks();
  184. int size = blk.Data().Size();
  185. // System.out.println("data:" + blk.Data());
  186. Long childSize = 0L, tmpChildSize;
  187. ArrayList<String> deps = blk.GetDependences();
  188. if (!deps.isEmpty()) {
  189. for (String dep : deps) {
  190. tmpChildSize = MaxStackSize(tb.get(dep));
  191. if (tmpChildSize > childSize) {
  192. childSize = tmpChildSize;
  193. }
  194. }
  195. }
  196. // System.out.println("MaxStackSize:" + size + "|" + childSize);
  197. return size + childSize;
  198. }
  199. }