BasicBlockMiddleware.java 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. package middlewares;
  2. /*
  3. * To change this license header, choose License Headers in Project Properties.
  4. * To change this template file, choose Tools | Templates
  5. * and open the template in the editor.
  6. */
  7. import API.MiddlewareInterface;
  8. import API.Utils;
  9. import common.Block;
  10. import common.Code;
  11. import common.Instruction;
  12. import java.util.ArrayList;
  13. import java.util.HashMap;
  14. import java.util.Iterator;
  15. import java.util.LinkedHashMap;
  16. import java.util.LinkedList;
  17. import java.util.Objects;
  18. import java.util.regex.Pattern;
  19. /**
  20. *
  21. * @author EUGENIO CARVALHO
  22. */
  23. public class BasicBlockMiddleware implements MiddlewareInterface {
  24. protected static Integer nextLeader;
  25. protected static BlockBaseGroup group;
  26. protected static HashMap<String, BlockBaseGroup> groups = new HashMap<>();
  27. protected static LinkedHashMap<Integer, Integer> leaders;
  28. //OK
  29. protected static Block block;
  30. protected static ArrayList<Integer> leadersList;
  31. public static Pattern addresspattern = Pattern.compile("\\_[VSTGC].*", Pattern.CASE_INSENSITIVE);
  32. protected Integer instructionPosition;
  33. protected Integer lastLeader;
  34. protected LinkedList<Instruction> instructions;
  35. protected Instruction instruction;
  36. protected String[] ignore = "label,alloc".split(","),
  37. nextIsJump = "jump,label".split(",");
  38. public BasicBlockMiddleware() {
  39. }
  40. protected void SetLeader(Integer position, Integer fix) {
  41. // Se existe um leader anterior, atualiza a ultima instrução do bloco basico
  42. if (lastLeader >= 0) {
  43. leaders.put(lastLeader, (fix == 0 ? (instructionPosition - 1) : instructionPosition));
  44. }
  45. // Marca o proximo leader
  46. // Se a proxima instrução não foir um jump ou label
  47. if (instructions.get(position + 1).in("type", nextIsJump)) {
  48. return;
  49. }
  50. // Seta o novo leader
  51. lastLeader = instructionPosition + fix;
  52. leaders.put(lastLeader, 0);
  53. }
  54. @Override
  55. public void Exec(Code c, LinkedHashMap<String, MiddlewareInterface> cp) throws Exception {
  56. String name = c.Block().getName();
  57. BlockBaseGroup g = new BlockBaseGroup(name);
  58. block = c.Block();
  59. group = g;
  60. groups.put(name, g);
  61. instructionPosition = 0;
  62. lastLeader = -1;
  63. leaders = new LinkedHashMap<>();
  64. //Step 1. Identify the leaders in the code. Leaders are instructions which come under any of the following 3 categories :
  65. //The first instruction is a leader.
  66. //The target of a conditional or an unconditional goto/jump instruction is a leader.
  67. //The instruction that immediately follows a conditional or an unconditional goto/jump instruction is a leader.
  68. instructions = block.Instructions();
  69. // Remove da quantidade o label inicial e final
  70. Integer limit = instructions.size() - 1;
  71. for (int i = 0; i < limit; i++) {
  72. instruction = instructions.get(i);
  73. // Correcao quando for um salto
  74. switch (instruction.Get("type")) {
  75. // Ignora a instrução de alocacao de memoria
  76. case "alloc":
  77. continue;
  78. case "label":
  79. SetLeader(i, 0);
  80. break;
  81. // Cria um novo bloco basico
  82. case "jump":
  83. case "call":
  84. case "branch":
  85. SetLeader(i, 1);
  86. }
  87. // Se a instrução não faz parte do grupo de instrucoes
  88. // de controle incrementa a posicao do contador
  89. if (!instruction.in("type", ignore)) {
  90. instructionPosition++;
  91. }
  92. }
  93. leaders.put(lastLeader, instructionPosition);
  94. VerifyOcorrences();
  95. System.out.println(group);
  96. }
  97. protected void RegBasicBlock() {
  98. // System.out.println("leaders:" + leaders);
  99. // System.out.println("RegBasicBlock:(" + nextLeader + ")(" + ((leaders.Get(nextLeader) - nextLeader) + 1) + ")");
  100. group.RegisterBlock(nextLeader, (leaders.get(nextLeader) - nextLeader));
  101. if (!leadersList.isEmpty()) {
  102. nextLeader = leadersList.remove(0);
  103. }
  104. }
  105. public static boolean IsAddress(String alias) {
  106. return !Utils.Empty(alias) && addresspattern.matcher(alias).matches();
  107. }
  108. public BlockBaseGroup getBasicBlockGroup(String id) {
  109. return groups.get(id);
  110. }
  111. public HashMap<String, BlockBaseGroup> getGroups() {
  112. return groups;
  113. }
  114. public static Pattern getAddresspattern() {
  115. return addresspattern;
  116. }
  117. public static void setAddresspattern(Pattern addresspattern) {
  118. BasicBlockMiddleware.addresspattern = addresspattern;
  119. }
  120. protected void VerifyOcorrences() throws Exception {
  121. // reseta a posicao da instrução
  122. instructionPosition = 0;
  123. leadersList = new ArrayList<>();
  124. leaders.entrySet().forEach((entry) -> {
  125. leadersList.add(entry.getKey());
  126. });
  127. System.out.println("leadersList:" + leadersList);
  128. nextLeader = leadersList.remove(0);
  129. // Registra primeiro bloco basico
  130. RegBasicBlock();
  131. // Atribui o segundo leder como
  132. // block.LastPosition = nextLeader - 1;
  133. // System.out.println("$$$$$$$$$$$$$$$$$$$$$$$ Quantidade de instrucoes:" + instructions.size());
  134. // Integer global = 0;
  135. for (Iterator<Instruction> it = instructions.iterator(); it.hasNext();) {
  136. instruction = it.next();
  137. if (!instruction.in("type", ignore)) {
  138. // Registra
  139. // System.out.println("Ins:(" + instructionPosition + ")(" + nextLeader + ")");
  140. if (Objects.equals(instructionPosition, nextLeader)) {
  141. // System.out.println("#####################Registrei um novo bloco###############");
  142. // block.CloseAllIntervals();
  143. // nextLeader = leadersList.remove(0);
  144. // block.LastPosition = nextLeader - 1;
  145. RegBasicBlock();
  146. }
  147. // Registra os acessos de leitura e escrita dos enderecos da instrucao
  148. group.ParseInstruction(instruction, "dst,p1,p2");
  149. // System.out.println("Instruction >>>>>>>> :" + instructionPosition + " real:" + (global));
  150. instructionPosition++;
  151. }
  152. // global++;
  153. }
  154. // Finaliza o grupo de blocos
  155. group.Close();
  156. // // Atualiza todos os encerramentos de intervalo para a ultima ocorrencia da variavel;
  157. // block.CloseAllIntervals();
  158. // System.out.println("block:" + groups);
  159. }
  160. }
  161. // block = new BlockBaseOcorrences(name);
  162. // ArrayList<ArrayList<Integer>> bbs = new ArrayList<>();
  163. // ArrayList<Integer> bb = null;
  164. // basicBlockName = name;
  165. // basicBlockCount = 0;
  166. // protected ArrayList<ArrayList<Integer>> basicBlock = new ArrayList<>();
  167. // basicBlocks.put(name, bbs);
  168. // blocks.put(name, block);