BaseBlockProcessor.java 6.3 KB

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