BasicBlockProcessor.java 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. package Processors;
  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.CodeProcessorInterface;
  8. import API.Utils;
  9. import common.Code;
  10. import common.Instruction;
  11. import java.util.ArrayList;
  12. import java.util.HashMap;
  13. import java.util.LinkedHashMap;
  14. import java.util.LinkedList;
  15. import java.util.Map;
  16. import java.util.regex.Pattern;
  17. /**
  18. *
  19. * @author EUGENIO CARVALHO
  20. */
  21. public class BasicBlockProcessor implements CodeProcessorInterface {
  22. // protected LinkedHashMap<String, ArrayList<ArrayList<Integer>>> basicBlocks = new LinkedHashMap<>();
  23. // protected String basicBlockName;
  24. // protected Integer basicBlockCount = 0;
  25. protected BlockBaseGroup group;
  26. protected HashMap<String, BlockBaseGroup> groups = new HashMap<>();
  27. protected Integer nextLeader;
  28. protected LinkedHashMap<Integer, Integer> leaders;
  29. //OK
  30. // protected HashMap<String, BlockBaseOcorrences> blocks = new HashMap<>();
  31. protected BlockBaseOcorrences block;
  32. public static Pattern addresspattern = Pattern.compile("\\_[VTGC].*", Pattern.CASE_INSENSITIVE);
  33. private ArrayList<Integer> leadersList;
  34. public BasicBlockProcessor() {
  35. }
  36. public BlockBaseGroup getBasicBlockGroup(String id) {
  37. return groups.get(id);
  38. }
  39. public HashMap<String, BlockBaseGroup> getGroups() {
  40. return groups;
  41. }
  42. // public HashMap<String, BlockBaseOcorrences> getBlocks() {
  43. // return blocks;
  44. // }
  45. //
  46. // public void setBlocks(HashMap<String, BlockBaseOcorrences> blocks) {
  47. // this.blocks = blocks;
  48. // }
  49. public BlockBaseOcorrences getBlock() {
  50. return block;
  51. }
  52. public void setBlock(BlockBaseOcorrences block) {
  53. this.block = block;
  54. }
  55. public static Pattern getAddresspattern() {
  56. return addresspattern;
  57. }
  58. public static void setAddresspattern(Pattern addresspattern) {
  59. BasicBlockProcessor.addresspattern = addresspattern;
  60. }
  61. // protected void IncrementBasicBlockCount() {
  62. // basicBlockCount++;
  63. // }
  64. @Override
  65. public void Exec(Code c, LinkedHashMap<String, CodeProcessorInterface> cp) throws Exception {
  66. String name = c.Block().getName();
  67. BlockBaseGroup g = new BlockBaseGroup(name);
  68. group = g;
  69. groups.put(name, g);
  70. // block = new BlockBaseOcorrences(name);
  71. // ArrayList<ArrayList<Integer>> bbs = new ArrayList<>();
  72. // ArrayList<Integer> bb = null;
  73. // basicBlockName = name;
  74. // basicBlockCount = 0;
  75. // protected ArrayList<ArrayList<Integer>> basicBlock = new ArrayList<>();
  76. // basicBlocks.put(name, bbs);
  77. // blocks.put(name, block);
  78. leaders = new LinkedHashMap<>();
  79. // block.LastPosition = c.Block().CurrentAddress - 1;
  80. //Step 1. Identify the leaders in the code. Leaders are instructions which come under any of the following 3 categories :
  81. //
  82. //The first instruction is a leader.
  83. //The target of a conditional or an unconditional goto/jump instruction is a leader.
  84. //The instruction that immediately follows a conditional or an unconditional goto/jump instruction is a leader.
  85. Instruction x;
  86. LinkedList<Instruction> instructions = c.Block().Instructions();
  87. int instructionPosition = 0,
  88. fix,
  89. lastLeader = -1,
  90. limit = instructions.size() - 2;
  91. for (int i = 0; i < limit; i++) {
  92. x = instructions.get(i);
  93. fix = 0;
  94. switch (x.Get("type")) {
  95. case "jump":
  96. case "call":
  97. case "branch":
  98. fix = 1;
  99. case "label":
  100. if (lastLeader >= 0) {
  101. leaders.put(lastLeader, instructionPosition);
  102. }
  103. if (!instructions.get(i + 1).in("type", new String[]{"jump", "label"})) {
  104. lastLeader = instructionPosition + fix;
  105. leaders.put(lastLeader, 0);
  106. }
  107. }
  108. if (!x.eq("type", "label")) {
  109. instructionPosition++;
  110. }
  111. }
  112. leaders.put(lastLeader, instructionPosition);
  113. // reseta a posicao da instrução
  114. instructionPosition = 0;
  115. // System.out.println("basicBlock:::::::::::::" + leaders);
  116. leadersList = new ArrayList<>();
  117. for (Map.Entry<Integer, Integer> entry : leaders.entrySet()) {
  118. leadersList.add(entry.getKey());
  119. }
  120. System.out.println("leadersList:" + leadersList);
  121. nextLeader = leadersList.remove(0);
  122. // Registra primeiro bloco basico
  123. RegBasicBlock();
  124. // Atribui o segundo leder como
  125. // block.LastPosition = nextLeader - 1;
  126. for (Instruction inst : instructions) {
  127. switch (inst.Get("type")) {
  128. case "label":
  129. break;
  130. default:
  131. // System.out.println("Ins:(" + instructionPosition + ")(" + nextLeader + ")");
  132. // Registra
  133. if (instructionPosition == nextLeader) {
  134. // block.CloseAllIntervals();
  135. // nextLeader = leadersList.remove(0);
  136. // block.LastPosition = nextLeader - 1;
  137. RegBasicBlock();
  138. }
  139. // Registra os acessos de leitura e escrita dos enderecos da instrucao
  140. group.ParseInstruction(inst, "dst,p1,p2");
  141. instructionPosition++;
  142. }
  143. }
  144. group.Close();
  145. // // Atualiza todos os encerramentos de intervalo para a ultima ocorrencia da variavel;
  146. // block.CloseAllIntervals();
  147. System.out.println("block:" + group);
  148. }
  149. protected void RegBasicBlock() {
  150. // System.out.println("RegBasicBlock:(" + nextLeader + ")(" + ((leaders.Get(nextLeader) - nextLeader) + 1) + ")");
  151. group.RegisterBlock(nextLeader, (leaders.get(nextLeader) - nextLeader) + 1);
  152. if (!leadersList.isEmpty()) {
  153. nextLeader = leadersList.remove(0);
  154. }
  155. }
  156. public static boolean IsAddress(String alias) {
  157. // System.out.println("Is Address:" + alias + "::" + addresspattern.matcher(alias).matches());
  158. return !Utils.Empty(alias) && addresspattern.matcher(alias).matches();
  159. }
  160. }