BasicBlockMiddleware.java 6.6 KB

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