AnaliseDependenciaMiddleware.java 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447
  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 analise;
  7. import API.MiddlewareInterface;
  8. import common.Instruction;
  9. import common.Block;
  10. import common.Code;
  11. import java.util.ArrayList;
  12. import java.util.Arrays;
  13. import java.util.HashMap;
  14. import java.util.LinkedHashMap;
  15. import java.util.LinkedHashSet;
  16. import java.util.Map;
  17. /**
  18. *
  19. * @author lucas
  20. */
  21. public class AnaliseDependenciaMiddleware implements MiddlewareInterface {
  22. int id = 0;
  23. protected Code target;
  24. protected Code blocosBasicos = new Code("Blocos.Basicos");
  25. public AnaliseDependenciaMiddleware() {
  26. }
  27. @Override
  28. public void Exec(Code c, LinkedHashMap<String, MiddlewareInterface> cp) throws Exception {
  29. // Block bloco = c.Block();
  30. target = c;
  31. System.out.println("Chamada analise dependencia");
  32. for (Map.Entry<String, Block> entry : target.Blocks().entrySet()) {
  33. Block mips = entry.getValue();
  34. boolean first = true, isLabel, add;
  35. for (Instruction inst : mips.Instructions()) {
  36. isLabel = inst.eq("type", "label");
  37. // inst.Get("inst.bin"); pra acessar os atributos
  38. // inst.getint
  39. if (first || isLabel || inst.eq("type", "J") || inst.in("inst", new String[]{"beq", "bne", "blez", "bgtz", "bgez", "bltz"})) {
  40. if (blocosBasicos.Block() != null) {
  41. if (!isLabel) {
  42. blocosBasicos.Block().Add(inst);
  43. }
  44. // System.out.println("blocosBasicos.Block().instructions > " + blocosBasicos.Block().instructions);
  45. if (blocosBasicos.Block().instructions.size() == 0) {
  46. blocosBasicos.stmts.remove(blocosBasicos.Block().getName());
  47. }
  48. blocosBasicos.CloseBlock();
  49. }
  50. blocosBasicos.OpenBlock("bloco." + (id++));
  51. if (first) {
  52. first = false;
  53. // if (!isLabel) {
  54. blocosBasicos.Block().Add(inst);
  55. // }
  56. }
  57. // } else if (inst.eq("type", "label")) {
  58. // System.out.println("Label:" + inst);
  59. } else {
  60. blocosBasicos.Block().Add(inst);
  61. }
  62. // if (add) {;
  63. // blocosBasicos.Block().Add(inst);
  64. // }
  65. }
  66. // System.out.println("Print mips:" + mips.getName() + "\n" + mips.Instructions());
  67. }
  68. Post();
  69. }
  70. public void Post() {
  71. ArrayList<String> contextTable = new ArrayList<>();
  72. ArrayList<String> listaTrocar = new ArrayList<>();
  73. ArrayList<String> lw_sw_Table = new ArrayList<>();
  74. WriteTable writeBitmapTable = new WriteTable(5);
  75. WriteTable readsTable = new WriteTable(5);
  76. WriteTable nome = new WriteTable(5);
  77. WriteTable lol = new WriteTable(5);
  78. //Map valor, chave do bloco e suas instruções
  79. for (Map.Entry<String, Block> b : blocosBasicos.Blocks().entrySet()) {
  80. System.out.println("Bloco:" + b.getKey() + "\n" + b.getValue().Instructions());
  81. // Pega os valores dos registradores
  82. for (int i = 0; i < b.getValue().Instructions().size(); i++) {
  83. System.out.println(b.getValue().Instructions().get(i).Get("inst") + ": " + "RS: " + b.getValue().Instructions().get(i).Get("rs") + " RT: " + b.getValue().Instructions().get(i).Get("rt") + " RD: " + b.getValue().Instructions().get(i).Get("rd"));
  84. //
  85. contextTable.add(b.getValue().Instructions().get(i).Get("rs"));
  86. contextTable.add(b.getValue().Instructions().get(i).Get("rt"));
  87. contextTable.add(b.getValue().Instructions().get(i).Get("rd"));
  88. for (int k = i + 1; k < b.getValue().Instructions().size(); k++) {
  89. //WAW RT == RT
  90. if ((b.getValue().Instructions().get(i).Get("rt") == b.getValue().Instructions().get(k).Get("rt"))
  91. && (b.getValue().Instructions().get(k).diferente("inst", "beq")
  92. && b.getValue().Instructions().get(k).diferente("inst", "bne")
  93. && b.getValue().Instructions().get(k).diferente("inst", "sb")
  94. && b.getValue().Instructions().get(k).diferente("inst", "sh")
  95. && b.getValue().Instructions().get(k).diferente("inst", "sw")
  96. && b.getValue().Instructions().get(k).diferente("inst", "swcl")
  97. && b.getValue().Instructions().get(k).diferente("inst", "add")
  98. && b.getValue().Instructions().get(k).diferente("inst", "addu")
  99. && b.getValue().Instructions().get(k).diferente("inst", "and")
  100. && b.getValue().Instructions().get(k).diferente("inst", "div")
  101. && b.getValue().Instructions().get(k).diferente("inst", "divu")
  102. && b.getValue().Instructions().get(k).diferente("inst", "mult")
  103. && b.getValue().Instructions().get(k).diferente("inst", "multu")
  104. && b.getValue().Instructions().get(k).diferente("inst", "nor")
  105. && b.getValue().Instructions().get(k).diferente("inst", "or")
  106. && b.getValue().Instructions().get(k).diferente("inst", "sll")
  107. && b.getValue().Instructions().get(k).diferente("inst", "sllv")
  108. && b.getValue().Instructions().get(k).diferente("inst", "slt")
  109. && b.getValue().Instructions().get(k).diferente("inst", "sltu")
  110. && b.getValue().Instructions().get(k).diferente("inst", "sra")
  111. && b.getValue().Instructions().get(k).diferente("inst", "srav")
  112. && b.getValue().Instructions().get(k).diferente("inst", "srl")
  113. && b.getValue().Instructions().get(k).diferente("inst", "srlv")
  114. && b.getValue().Instructions().get(k).diferente("inst", "sub")
  115. && b.getValue().Instructions().get(k).diferente("inst", "subu")
  116. && b.getValue().Instructions().get(k).diferente("inst", "xor"))) {
  117. //Registradores a serem trocados
  118. listaTrocar.add(b.getValue().Instructions().get(k).Get("rt"));
  119. }
  120. //WAW CLASSICO RD == RD
  121. if (b.getValue().Instructions().get(i).Get("rd") == b.getValue().Instructions().get(k).Get("rd")) {
  122. listaTrocar.add(b.getValue().Instructions().get(k).Get("rd"));
  123. }
  124. if (b.getValue().Instructions().get(i).Get("rt") == b.getValue().Instructions().get(k).Get("rd")) {
  125. listaTrocar.add(b.getValue().Instructions().get(k).Get("rd"));
  126. }
  127. //Caso em que o RT nao é destino
  128. if ((b.getValue().Instructions().get(i).Get("rs") == b.getValue().Instructions().get(k).Get("rt"))
  129. && (b.getValue().Instructions().get(k).diferente("inst", "beq")
  130. && b.getValue().Instructions().get(k).diferente("inst", "bne")
  131. && b.getValue().Instructions().get(k).diferente("inst", "sb")
  132. && b.getValue().Instructions().get(k).diferente("inst", "sh")
  133. && b.getValue().Instructions().get(k).diferente("inst", "sw")
  134. && b.getValue().Instructions().get(k).diferente("inst", "swcl")
  135. && b.getValue().Instructions().get(k).diferente("inst", "add")
  136. && b.getValue().Instructions().get(k).diferente("inst", "addu")
  137. && b.getValue().Instructions().get(k).diferente("inst", "and")
  138. && b.getValue().Instructions().get(k).diferente("inst", "div")
  139. && b.getValue().Instructions().get(k).diferente("inst", "divu")
  140. && b.getValue().Instructions().get(k).diferente("inst", "mult")
  141. && b.getValue().Instructions().get(k).diferente("inst", "multu")
  142. && b.getValue().Instructions().get(k).diferente("inst", "nor")
  143. && b.getValue().Instructions().get(k).diferente("inst", "or")
  144. && b.getValue().Instructions().get(k).diferente("inst", "sll")
  145. && b.getValue().Instructions().get(k).diferente("inst", "sllv")
  146. && b.getValue().Instructions().get(k).diferente("inst", "slt")
  147. && b.getValue().Instructions().get(k).diferente("inst", "sltu")
  148. && b.getValue().Instructions().get(k).diferente("inst", "sra")
  149. && b.getValue().Instructions().get(k).diferente("inst", "srav")
  150. && b.getValue().Instructions().get(k).diferente("inst", "srl")
  151. && b.getValue().Instructions().get(k).diferente("inst", "srlv")
  152. && b.getValue().Instructions().get(k).diferente("inst", "sub")
  153. && b.getValue().Instructions().get(k).diferente("inst", "subu")
  154. && b.getValue().Instructions().get(k).diferente("inst", "xor"))) {
  155. listaTrocar.add(b.getValue().Instructions().get(k).Get("rt"));
  156. }
  157. if (b.getValue().Instructions().get(i).Get("rs") == b.getValue().Instructions().get(k).Get("rd")) {
  158. listaTrocar.add(b.getValue().Instructions().get(k).Get("rd"));
  159. }
  160. }
  161. }
  162. //Remove blank da contextTable
  163. contextTable.removeAll(Arrays.asList("", null));
  164. listaTrocar.removeAll(Arrays.asList("", null));
  165. //Remove repetições
  166. listaTrocar = new ArrayList<>(new LinkedHashSet<>(listaTrocar));
  167. for (int i = 0; i < listaTrocar.size(); i++) {
  168. for (int j = contextTable.size() - 1; j >= 0; j--) {
  169. if (listaTrocar.get(i) == contextTable.get(j)) {
  170. if (contextTable.get(j).equals("zero")) {
  171. contextTable.set(j, "r32");
  172. }
  173. if (contextTable.get(j).equals("at")) {
  174. contextTable.set(j, "r33");
  175. }
  176. if (contextTable.get(j).equals("v0")) {
  177. contextTable.set(j, "r34");
  178. }
  179. if (contextTable.get(j).equals("v1")) {
  180. contextTable.set(j, "r35");
  181. }
  182. if (contextTable.get(j).equals("a0")) {
  183. contextTable.set(j, "r36");
  184. }
  185. if (contextTable.get(j).equals("a1")) {
  186. contextTable.set(j, "r37");
  187. }
  188. if (contextTable.get(j).equals("a2")) {
  189. contextTable.set(j, "r38");
  190. }
  191. if (contextTable.get(j).equals("a3")) {
  192. contextTable.set(j, "r39");
  193. }
  194. if (contextTable.get(j).equals("t0")) {
  195. contextTable.set(j, "r40");
  196. }
  197. if (contextTable.get(j).equals("t1")) {
  198. contextTable.set(j, "r41");
  199. }
  200. if (contextTable.get(j).equals("t2")) {
  201. contextTable.set(j, "r42");
  202. }
  203. if (contextTable.get(j).equals("t3")) {
  204. contextTable.set(j, "r43");
  205. }
  206. if (contextTable.get(j).equals("t4")) {
  207. contextTable.set(j, "r44");
  208. }
  209. if (contextTable.get(j).equals("t5")) {
  210. contextTable.set(j, "r45");
  211. }
  212. if (contextTable.get(j).equals("t6")) {
  213. contextTable.set(j, "r46");
  214. }
  215. if (contextTable.get(j).equals("t7")) {
  216. contextTable.set(j, "r47");
  217. }
  218. if (contextTable.get(j).equals("s0")) {
  219. contextTable.set(j, "r48");
  220. }
  221. if (contextTable.get(j).equals("s1")) {
  222. contextTable.set(j, "r49");
  223. }
  224. if (contextTable.get(j).equals("s2")) {
  225. contextTable.set(j, "r50");
  226. }
  227. if (contextTable.get(j).equals("s3")) {
  228. contextTable.set(j, "r51");
  229. }
  230. if (contextTable.get(j).equals("s4")) {
  231. contextTable.set(j, "r52");
  232. }
  233. if (contextTable.get(j).equals("s5")) {
  234. contextTable.set(j, "r53");
  235. }
  236. if (contextTable.get(j).equals("s6")) {
  237. contextTable.set(j, "r54");
  238. }
  239. if (contextTable.get(j).equals("s7")) {
  240. contextTable.set(j, "r55");
  241. }
  242. if (contextTable.get(j).equals("t8")) {
  243. contextTable.set(j, "r56");
  244. }
  245. if (contextTable.get(j).equals("t9")) {
  246. contextTable.set(j, "r57");
  247. }
  248. if (contextTable.get(j).equals("k0")) {
  249. contextTable.set(j, "r58");
  250. }
  251. if (contextTable.get(j).equals("k1")) {
  252. contextTable.set(j, "r59");
  253. }
  254. if (contextTable.get(j).equals("gp")) {
  255. contextTable.set(j, "r60");
  256. }
  257. if (contextTable.get(j).equals("sp")) {
  258. contextTable.set(j, "r61");
  259. }
  260. if (contextTable.get(j).equals("fp")) {
  261. contextTable.set(j, "r62");
  262. }
  263. if (contextTable.get(j).equals("ra")) {
  264. contextTable.set(j, "r63");
  265. }
  266. break;
  267. }
  268. }
  269. }
  270. contextTable = new ArrayList<>(new LinkedHashSet<>(contextTable));
  271. //Para a writeBitmapTable
  272. for (Instruction instrucao : b.getValue().Instructions()) {
  273. switch (instrucao.Get("type")) {
  274. case "I":
  275. //WT
  276. if (instrucao.Get("inst") == "lw" || instrucao.Get("inst") == "sw"
  277. || instrucao.Get("inst") == "lb" || instrucao.Get("inst") == "lbu" || instrucao.Get("inst") == "sh") {
  278. lw_sw_Table.add(instrucao.Get("inst") + " " + instrucao.Get("rt"));
  279. } else {
  280. writeBitmapTable.Add(instrucao.Get("rs"), "", instrucao.Get("rt"));
  281. }
  282. //Gambiarra que mostra o que foi escrito e lido, para gerar a Reads
  283. nome.Add("", "", instrucao.Get("rt") + "<-" + instrucao.Get("inst"));
  284. readsTable.Add("", "", instrucao.Get("rs") + "->" + instrucao.Get("inst"));
  285. break;
  286. case "R":
  287. //WT
  288. if (instrucao.Get("inst") == "lw" || instrucao.Get("inst") == "sw"
  289. || instrucao.Get("inst") == "lb" || instrucao.Get("inst") == "lbu" || instrucao.Get("inst") == "sh") {
  290. lw_sw_Table.add(instrucao.Get("rt"));
  291. } else {
  292. writeBitmapTable.Add(instrucao.Get("rs"), instrucao.Get("rt"), instrucao.Get("rd"));
  293. }
  294. //Gambiarra que mostra o que foi escrito e lido, para gerar a Reads
  295. nome.Add("", "", instrucao.Get("rd") + "<-" + instrucao.Get("inst"));
  296. readsTable.Add("", "", instrucao.Get("rs") + "->" + instrucao.Get("inst"));
  297. //Caso tenha RD
  298. lol.Add("", "", instrucao.Get("rt") + "->" + instrucao.Get("inst"));
  299. break;
  300. }
  301. }
  302. System.out.println("\nContextTable" + contextTable);
  303. System.out.println("\nWriteTable" + writeBitmapTable.Get());
  304. //Para saber qual o registrador trocado em WriteTable, é só olhar a ultima vez que o mesmo
  305. //foi escrito, com excessão das instruções load store
  306. System.out.println("\nTrocar" + listaTrocar);
  307. System.out.println("\nlw_Sw_Resource" + lw_sw_Table);
  308. System.out.println("\n" + "Escrito" + nome.Get());
  309. System.out.println("\nLido" + readsTable.Get());
  310. System.out.println("\nLido" + lol.Get());
  311. lw_sw_Table.clear();
  312. nome.Get().clear();
  313. readsTable.Get().clear();
  314. lol.Get().clear();
  315. writeBitmapTable.Get().clear();
  316. contextTable.clear();
  317. listaTrocar.clear();
  318. System.out.println("================================================\n");
  319. }
  320. }
  321. //classe das ocorrencias
  322. protected class Y {
  323. public HashMap<String, Boolean> wt;
  324. public ArrayList<String> ocorrences;
  325. Y() {
  326. this.wt = new HashMap<>();
  327. this.ocorrences = new ArrayList<>();
  328. }
  329. //para imprimir só as ocorrencias(?)
  330. @Override
  331. public String toString() {
  332. return this.ocorrences.toString();
  333. }
  334. }
  335. protected class WriteTable {
  336. protected int limitPerList;
  337. ArrayList<Y> wt;
  338. public WriteTable(int lpl) {
  339. this.limitPerList = lpl;
  340. this.wt = new ArrayList<>();
  341. }
  342. public ArrayList<Y> Get() {
  343. return this.wt;
  344. }
  345. protected int Has(String rs) {
  346. int pos = 0;
  347. for (Y map : wt) {
  348. if (map.wt.containsKey(rs)) {
  349. return pos;
  350. }
  351. pos++;
  352. }
  353. return -1;
  354. }
  355. public void Add(String rs, String rs2, String rd) {
  356. int h1 = Has(rs);
  357. int h2 = Has(rs2);
  358. if (h1 < 0) {
  359. if (h2 < 0) {
  360. this.AddNewList(rd, 0); // certo
  361. } else {
  362. this.AddNewList(rd, h2 + 1); // certo
  363. }
  364. } else if (h2 < 0) {
  365. this.AddNewList(rd, h1 + 1);
  366. } else {
  367. this.AddNewList(rd, Math.max(h1, h2) + 1);
  368. }
  369. }
  370. protected void AddNewList(String rd, int index) {
  371. Y tmp = new Y();
  372. int next = index + 1;
  373. if (wt.size() > index) {
  374. if (wt.get(index).ocorrences.size() < this.limitPerList) {
  375. tmp = wt.get(index);
  376. } else if (wt.size() > next) {
  377. tmp = wt.get(next);
  378. } else {
  379. wt.add(tmp);
  380. }
  381. } else {
  382. wt.add(tmp);
  383. }
  384. tmp.wt.put(rd, Boolean.TRUE);
  385. tmp.ocorrences.add(rd);
  386. }
  387. }
  388. }