MultCoreJun.java 8.1 KB

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