BlockBaseOcorrences.java 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  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 middlewares;
  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) throws Exception {
  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<>());
  65. }
  66. // System.out.println("Open interval:(" + alias + "):" + Position + ":" + LastPosition);
  67. intervals.get(alias).add(new Interval(Position, LastPosition, Position));
  68. }
  69. protected void CloseAllIntervals() {
  70. LastPosition = Position - 1;
  71. // System.out.println("//////// CloseAllIntervals(" + leader + "):" + LastPosition);
  72. // Para cada variavel
  73. for (Map.Entry<String, LinkedList<Interval>> x : intervals.entrySet()) {
  74. // Pega o ultimo intervalo
  75. interval = x.getValue().peekLast();
  76. // System.out.println("Check interval("
  77. // + interval.Alive(Position)
  78. // + "): "
  79. // + x.getKey()
  80. // + " > "
  81. // + Position
  82. // + " > "
  83. // + interval);
  84. if (interval.Alive(LastPosition)) {
  85. // System.out.println("Interval:" + x.getKey() + ":open:" + ocorrences.get(x.getKey()).peekLast());
  86. interval.Close();
  87. }
  88. }
  89. }
  90. public void Register(Instruction x, String attr) throws Exception {
  91. String alias = x.Get(attr);
  92. ArrayList<String> aliases;
  93. if (!BasicBlockMiddleware.IsAddress(alias)) {
  94. return;
  95. }
  96. aliases = new ArrayList<>();
  97. alias = ToAliase(x, attr);
  98. aliases.add(alias);
  99. String index = attr + ".indice";
  100. if (x.Has(index) && !x.isNumber(index)) {
  101. aliases.add(x.Get(index));
  102. }
  103. for (String a : aliases) {
  104. if (!ocorrences.containsKey(a)) {
  105. ocorrences.put(a, new LinkedList<>());
  106. }
  107. ocorrences.get(a).add(Position);
  108. if (!Alive(a)) {
  109. OpenInterval(a);
  110. }
  111. // System.out.println("Register:a, Position," + a + ":" + Position);
  112. Get(a, Position).setLastOcorrence(Position);
  113. }
  114. }
  115. public Interval Last(String addr) {
  116. LinkedList<Interval> itvls = Get(addr);
  117. if (itvls != null) {
  118. return itvls.peekLast();
  119. }
  120. return null;
  121. }
  122. public Interval Get(String var, int i) throws Exception {
  123. LinkedList<Interval> intervls = Get(var);
  124. if (intervls != null) {
  125. // System.out.println("Encontrei o intervalo de " + var);
  126. for (Interval inter : intervls) {
  127. // System.out.println("Interval:" + inter + ":" + i);
  128. if (inter.Inside(i)) {
  129. return inter;
  130. }
  131. }
  132. }
  133. // else {
  134. // System.out.println("Encontrei o intervalo de " + var);
  135. // }
  136. return null;
  137. // throw new CompileException("Intervalo não definido para a variável %s na posição %d", var, i);
  138. }
  139. // Busca por um intervalo expirado ou que esteja disponivel para ser subdividido
  140. // Retorna a primeira parte intervalo que foi dividido
  141. public Interval Spill(int position) throws Exception {
  142. // System.out.println("Spill:[" + position + "]{");
  143. HashMap<String, Interval> is = new HashMap<>();
  144. Interval inter = null;
  145. String var = "";
  146. for (Map.Entry<String, LinkedList<Interval>> I : intervals.entrySet()) {
  147. var = I.getKey();
  148. inter = Get(var, position);
  149. // Tem intervalo nessa posicao agora deve verificar se ela não ocorre nessa instrucao;
  150. if (inter != null && !ocorrences.get(var).contains(position)) {
  151. is.put(var, inter);
  152. }
  153. }
  154. inter = null;
  155. // System.out.println("Spill disp: (position:" + position + ") " + is.size());
  156. // is Contem todos os intervalos que podem ser divididos;
  157. // o intervalo escolhido é o que possui a ocorrencia mais longe
  158. int pos = 0, pos1, interpos;
  159. for (Map.Entry<String, Interval> x : is.entrySet()) {
  160. pos1 = FirstAfter(ocorrences.get(x.getKey()), position);
  161. if (pos1 > pos) {
  162. pos = pos1;
  163. var = x.getKey();
  164. inter = x.getValue();
  165. }
  166. }
  167. // System.out.println("Spill inter: " + var + "|" + inter);
  168. // Se encontrou um intervalo para ser dividido executa a divisao
  169. if (inter != null) {
  170. LinkedList<Interval> lintervals = intervals.get(var);
  171. interpos = lintervals.indexOf(inter);
  172. lintervals.add(interpos + 1, new Interval(pos, inter.getEnd(), pos));
  173. inter.Close(position);
  174. }
  175. // System.out.println("End spill}");
  176. return inter;
  177. }
  178. protected Integer FirstAfter(LinkedList<Integer> get, int position) {
  179. for (Integer integer : get) {
  180. if (integer > position) {
  181. return integer;
  182. }
  183. }
  184. return 0;
  185. }
  186. protected static Pattern operators = Pattern.compile("(\\&|\\*)");
  187. public String ToAliase(Instruction x, String attr) {
  188. String alias = x.Get(attr);
  189. String op = x.Get("op");
  190. // System.out.println("ToAliase:" + x);
  191. boolean isdst = attr.equals("dst");
  192. if (isdst && x.Has("dst.pointer")) {
  193. // System.out.println("Has Pointer:");
  194. alias = "*" + alias;
  195. } else if (!isdst && operators.matcher(op).matches() && x.eq("type", "pointer_assign")) {
  196. alias = op + alias;
  197. }
  198. // System.out.println("Aliase:" + alias + ":" + attr + ":" + x);
  199. return alias;
  200. }
  201. @Override
  202. public String toString() {
  203. StringBuilder buffer = new StringBuilder("Block: " + name + " - Leader: " + leader + " - Size: " + Position + "\n");
  204. buffer.append("Inst/Var ");
  205. // for (int i = 0; i <= Position; i++) {
  206. for (int i = 0; i < Position; i++) {
  207. buffer.append(Utils.Pad(i + "", " ", "-", 3));
  208. }
  209. buffer.append("\n");
  210. String append, var, reg;
  211. Interval inter;
  212. try {
  213. for (Map.Entry<String, LinkedList<Integer>> x : ocorrences.entrySet()) {
  214. var = x.getKey();
  215. buffer.append(Utils.Pad(var, " ", "-", 11));
  216. // for (int i = 0; i <= Position; i++) {
  217. for (int i = 0; i < Position; i++) {
  218. // System.out.println("OCOR:" +);
  219. reg = "";
  220. append = "---";
  221. if (x.getValue().contains(i)) {
  222. // append = Get(var, i).getRegister() + "##"; // Ocorrencia da variavel
  223. append = "###"; // Ocorrencia da variavel
  224. } else if (Inside(var, i)) {
  225. append = "+++"; // dentro do intervalo
  226. }
  227. inter = Get(x.getKey(), i);
  228. if (inter != null) {
  229. reg = inter.getRegister();
  230. }
  231. buffer.append(reg + append.substring(0, append.length() - reg.length()));
  232. }
  233. buffer.append("\n");
  234. }
  235. } catch (Exception ex) {
  236. // Logger.getLogger(BlockBaseOcorrences.class.getName()).log(Level.SEVERE, null, ex);
  237. }
  238. return buffer.toString();
  239. }
  240. public int getLastPosition() {
  241. return getLeader() + Position;
  242. }
  243. public int getSize() {
  244. return Position;
  245. }
  246. }
  247. // protected void Register(Instruction x, String attr) {
  248. // String alias = x.Get(attr);
  249. // if (!BasicBlockMiddleware.IsAddress(alias)) {
  250. // return;
  251. // }
  252. // alias = ToAliase(x, attr);
  253. //
  254. //// System.out.println("Register x:" + x);
  255. // ArrayList<String> aliases = new ArrayList<>();
  256. //
  257. // aliases.add(alias);
  258. // String index = attr + ".indice";
  259. // if (x.Has(index) && !x.IsNumber(index)) {
  260. // aliases.add(x.Get(index));
  261. // }
  262. // for (String a : aliases) {
  263. // if (!ocorrences.containsKey(a)) {
  264. // ocorrences.put(a, new LinkedList<Integer>());
  265. // }
  266. //
  267. // ocorrences.Get(a).add(Position);
  268. // if (!Alive(a)) {
  269. // OpenInterval(a);
  270. // }
  271. // }
  272. // }