BlockBaseOcorrences.java 8.9 KB

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