BlockOcorrences.java 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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 IntermediaryCode;
  7. import API.Instruction;
  8. import API.Utils;
  9. import java.util.ArrayList;
  10. import java.util.HashMap;
  11. import java.util.LinkedHashMap;
  12. import java.util.LinkedList;
  13. import java.util.Map;
  14. import java.util.regex.Pattern;
  15. /**
  16. *
  17. * @author EUGENIO CARVALHO
  18. */
  19. public class BlockOcorrences {
  20. protected LinkedHashMap<String, LinkedList<Integer>> ocorrences = new LinkedHashMap<>();
  21. protected LinkedHashMap<String, LinkedList<Interval>> intervals = new LinkedHashMap<>();
  22. protected int Position = 0;
  23. protected int LastPosition = 0;
  24. protected Interval interval;
  25. protected Pattern addresspattern;
  26. protected String name;
  27. public BlockOcorrences(String name) {
  28. this.name = name;
  29. }
  30. protected void CloseAllIntervals() {
  31. // System.out.println("CloseAllIntervals:");
  32. for (Map.Entry<String, LinkedList<Interval>> x : intervals.entrySet()) {
  33. interval = x.getValue().peekLast();
  34. // System.out.println("Check interval:" + x.getKey() + Position + ":" + interval);
  35. if (interval.Alive(Position)) {
  36. // System.out.println("Interval:" + x.getKey() + ":open:" + ocorrences.G(x.getKey()).peekLast());
  37. interval.Close(ocorrences.get(x.getKey()).peekLast());
  38. }
  39. }
  40. }
  41. protected void Register(Instruction x, String attr) {
  42. String alias = x.G(attr);
  43. if (!OcorrenceFinderProcessor.IsAddress(alias)) {
  44. return;
  45. }
  46. alias = ToAliase(x, attr);
  47. // System.out.println("Register x:" + x);
  48. ArrayList<String> aliases = new ArrayList<>();
  49. aliases.add(alias);
  50. String index = attr + ".indice";
  51. if (x.Has(index) && !x.isNumber(index)) {
  52. aliases.add(x.G(index));
  53. }
  54. for (String a : aliases) {
  55. if (!ocorrences.containsKey(a)) {
  56. ocorrences.put(a, new LinkedList<Integer>());
  57. }
  58. ocorrences.get(a).add(Position);
  59. if (!Alive(a)) {
  60. OpenInterval(a);
  61. }
  62. }
  63. }
  64. protected boolean Alive(String alias) {
  65. if (intervals.containsKey(alias)) {
  66. return Position <= intervals.get(alias).peekLast().End;
  67. }
  68. return false;
  69. }
  70. protected void OpenInterval(String alias) {
  71. if (!intervals.containsKey(alias)) {
  72. intervals.put(alias, new LinkedList<Interval>());
  73. }
  74. // System.out.println("Open interval:(" + alias + "):" + Position + ":" + LastPosition);
  75. intervals.get(alias).add(new Interval(Position, LastPosition));
  76. }
  77. @Override
  78. public String toString() {
  79. StringBuilder buffer = new StringBuilder("Block: " + name + "\n");
  80. buffer.append("Inst/Var ");
  81. for (int i = 0; i <= LastPosition; i++) {
  82. buffer.append(Utils.Pad(i + "", " ", "-", 3));
  83. }
  84. buffer.append("\n");
  85. String append, var, reg;
  86. Interval inter;
  87. for (Map.Entry<String, LinkedList<Integer>> x : ocorrences.entrySet()) {
  88. var = x.getKey();
  89. buffer.append(Utils.Pad(var, " ", "-", 11));
  90. for (int i = 0; i <= LastPosition; i++) {
  91. // System.out.println("OCOR:" +);
  92. reg = "";
  93. append = "---";
  94. if (x.getValue().contains(i)) {
  95. // append = Get(var, i).getRegister() + "##"; // Ocorrencia da variavel
  96. append = "###"; // Ocorrencia da variavel
  97. } else if (Inside(var, i)) {
  98. append = "+++"; // dentro do intervalo
  99. }
  100. inter = Get(x.getKey(), i);
  101. if (inter != null) {
  102. reg = inter.getRegister();
  103. }
  104. buffer.append(reg + append.substring(0, append.length() - reg.length()));
  105. }
  106. buffer.append("\n");
  107. }
  108. return buffer.toString();
  109. }
  110. public boolean Inside(String var, int i) {
  111. return Get(var, i) != null;
  112. }
  113. public LinkedList<Interval> Get(String var) {
  114. if (!intervals.containsKey(var)) {
  115. return null;
  116. }
  117. return intervals.get(var);
  118. }
  119. public Interval Last(String addr) {
  120. LinkedList<Interval> itvls = Get(addr);
  121. if (itvls != null) {
  122. return itvls.peekLast();
  123. }
  124. return null;
  125. }
  126. public Interval Get(String var, int i) {
  127. LinkedList<Interval> intervls = Get(var);
  128. if (intervls != null) {
  129. for (Interval inter : intervls) {
  130. if (inter.Inside(i)) {
  131. return inter;
  132. }
  133. }
  134. }
  135. return null;
  136. }
  137. // Busca por um intervalo expirado ou que esteja disponivel para ser subdividido
  138. // Retorna a primeira parte intervalo que foi dividido
  139. public Interval Spill(int position) {
  140. // System.out.println("Spill:[" + position + "]{");
  141. HashMap<String, Interval> is = new HashMap<>();
  142. Interval inter = null;
  143. String var = "";
  144. for (Map.Entry<String, LinkedList<Interval>> I : intervals.entrySet()) {
  145. var = I.getKey();
  146. inter = Get(var, position);
  147. System.out.println("Interval:" + inter);
  148. // Tem intervalo nessa posicao agora deve verificar se ela não ocorre nessa instrucao;
  149. if (inter != null && !ocorrences.get(var).contains(position)) {
  150. is.put(var, inter);
  151. }
  152. }
  153. inter = null;
  154. // System.out.println("Spill disp:" + is.size());
  155. // is Contem todos os intervalos que podem ser divididos;
  156. // o intervalo escolhido é o que possui a ocorrencia mais longe
  157. int pos = 0, pos1, interpos;
  158. for (Map.Entry<String, Interval> x : is.entrySet()) {
  159. pos1 = FirstAfter(ocorrences.get(x.getKey()), position);
  160. if (pos1 > pos) {
  161. pos = pos1;
  162. var = x.getKey();
  163. inter = x.getValue();
  164. }
  165. }
  166. // System.out.println("Spill inter:" + var + "|" + inter);
  167. // Se encontrou um intervalo para ser dividido executa a divisao
  168. if (inter != null) {
  169. LinkedList<Interval> lintervals = intervals.get(var);
  170. interpos = lintervals.indexOf(inter);
  171. lintervals.add(interpos + 1, new Interval(pos, inter.getEnd()));
  172. inter.Close(position);
  173. }
  174. // System.out.println("End spill}");
  175. return inter;
  176. }
  177. protected Integer FirstAfter(LinkedList<Integer> get, int position) {
  178. for (Integer integer : get) {
  179. if (integer > position) {
  180. return integer;
  181. }
  182. }
  183. return 0;
  184. }
  185. protected static Pattern operators = Pattern.compile("(\\&|\\*)");
  186. public String ToAliase(Instruction x, String attr) {
  187. String alias = x.G(attr);
  188. String op = x.G("op");
  189. // System.out.println("ToAliase:" + x);
  190. boolean isdst = attr.equals("dst");
  191. if (isdst && x.Has("dst.pointer")) {
  192. // System.out.println("Has Pointer:");
  193. alias = "*" + alias;
  194. } else if (!isdst && operators.matcher(op).matches() && x.eq("type", "pointer_assignment")) {
  195. alias = op + alias;
  196. }
  197. // System.out.println("Aliase:" + alias + ":" + attr + ":" + x);
  198. return alias;
  199. }
  200. }