BlockBaseOcorrences.java 8.8 KB

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