ModuleSchedulingMiddleware.java 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430
  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.HashMap;
  13. import java.util.Iterator;
  14. import java.util.LinkedHashMap;
  15. import java.util.LinkedList;
  16. import java.util.Map;
  17. /**
  18. *
  19. * @author lucas
  20. */
  21. public class ModuleSchedulingMiddleware implements MiddlewareInterface {
  22. protected Code target;
  23. protected Integer NumeroElementosDeProcessamento = 5;
  24. protected HashMap<String, String> instructionMap = new HashMap<String, String>() {
  25. {
  26. put("I", "rt");
  27. put("R", "rd");
  28. }
  29. };
  30. protected LinkedHashMap<String, Block> loops = new LinkedHashMap<>();
  31. protected LinkedHashMap<String, ArrayList<No>> graphs = new LinkedHashMap<>();
  32. private String loopId;
  33. private Block loopInstructions;
  34. public ModuleSchedulingMiddleware() {
  35. }
  36. @Override
  37. public void Exec(Code c, LinkedHashMap<String, MiddlewareInterface> cp) throws Exception {
  38. // Block bloco = c.Block();
  39. System.out.println("Running Module Scheduling");
  40. target = c;
  41. for (Map.Entry<String, Block> entry : c.Blocks().entrySet()) {
  42. ExtractLoopInstructions(entry.getValue());
  43. }
  44. ConstruirGrafoDeDependencia();
  45. // Join();
  46. System.out.println("Loops\n:" + loops.keySet());
  47. // for (Map.Entry<String, Block> entry : loops.entrySet()) {
  48. // System.out.println("Loop " + entry.getKey());
  49. // System.out.println("F:" + entry.getValue().Instructions().getFirst());
  50. // System.out.println("L:" + entry.getValue().Instructions().getLast());
  51. // };
  52. System.out.println("Detalhe dos grafos:");
  53. for (Map.Entry<String, ArrayList<No>> entry : graphs.entrySet()) {
  54. String key = entry.getKey();
  55. ArrayList<No> value = entry.getValue();
  56. System.out.println("Loop " + key + " (" + value.size() + ")");
  57. for (No no : value) {
  58. System.out.println(">>" + no.Dump(""));
  59. }
  60. }
  61. }
  62. protected void ExtractLoopInstructions(Block block) {
  63. String loopId, lastLoopId = "";
  64. HashMap<String, Boolean> removed = new HashMap<>();
  65. boolean ne;
  66. for (Instruction inst : block.Instructions()) {
  67. loopId = inst.Get("inloop");
  68. // System.out.println("loopId(" + inst.Get("global.position") + "):" + loopId);
  69. // Se a instrucao não esta dentro de um loop
  70. // Se é um loop mais externo e foi marcado como removido
  71. // A instrucao não é adicionada
  72. if (loopId.equals("") || removed.containsKey(loopId)) {
  73. continue;
  74. }
  75. // Se for a primeira ocorrencia cria um bloco para o loop
  76. if (!loops.containsKey(loopId)) {
  77. // System.out.println("Add loop " + loopId);
  78. loops.put(loopId, new Block(loopId));
  79. }
  80. // Adiciona a instrucao no loop atual
  81. loops.get(loopId).Add(inst.copy());
  82. // Se o id do loop anterior for mais externo que o atual remove o anterior
  83. // E marca como removido para evitar de adicionar instrucoes nele
  84. if (!loopId.equals(lastLoopId)) {
  85. if (!lastLoopId.equals("") && loopId.contains(lastLoopId)) {
  86. loops.remove(lastLoopId);
  87. removed.put(lastLoopId, Boolean.TRUE);
  88. }
  89. // Atualiza o ultimo id
  90. lastLoopId = loopId;
  91. }
  92. }
  93. }
  94. protected void ConstruirGrafoDeDependencia() {
  95. // Block loopInstructions;
  96. for (Map.Entry<String, Block> entry : loops.entrySet()) {
  97. loopId = entry.getKey();
  98. loopInstructions = entry.getValue();
  99. if (!graphs.containsKey(loopId)) {
  100. graphs.put(loopId, new ArrayList<>());
  101. }
  102. ContruirGrafoPara();
  103. }
  104. }
  105. // protected void Join() {
  106. // No next;
  107. // for (Map.Entry<String, ArrayList<No>> entry : graphs.entrySet()) {
  108. // for (No no : entry.getValue()) {
  109. // if (no.root) {
  110. // MarcarFilhosComoNaoRoot(no);
  111. // }
  112. // }
  113. // // Remove nos não root
  114. // for (Iterator<No> iterator = entry.getValue().iterator(); iterator.hasNext();) {
  115. // if (!iterator.next().root) {
  116. // iterator.remove();
  117. // }
  118. // }
  119. // }
  120. // }
  121. protected void MarcarFilhosComoNaoRoot(No n) {
  122. for (No dep : n.deps) {
  123. dep.root = false;
  124. MarcarFilhosComoNaoRoot(dep);
  125. }
  126. // n.deps.stream().map((dep) -> {
  127. // dep.root = false;
  128. // return dep;
  129. // }).forEachOrdered((dep) -> {
  130. // MarcarFilhosComoNaoRoot(dep);
  131. // });
  132. }
  133. protected void ContruirGrafoPara() {
  134. LinkedList<Instruction> instructions = loopInstructions.Instructions();
  135. Instruction instruction;
  136. for (int i = loopInstructions.Instructions().size() - 1; i >= 0; i--) {
  137. instruction = instructions.get(i);
  138. if (instruction.eq("visited", "true")) {
  139. continue;
  140. }
  141. EncontreDependencias(
  142. instruction,
  143. i - 1,
  144. GetNo(instruction, true)
  145. );
  146. }
  147. }
  148. protected void EncontreDependencias(
  149. Instruction instruction,
  150. Integer after,
  151. No n
  152. ) {
  153. LinkedList<Instruction> instructions = loopInstructions.Instructions();
  154. Instruction tmp;
  155. String dst;
  156. No no;
  157. instruction.eq("visited", "true");
  158. ArrayList<String> sources = new ArrayList<>();
  159. switch (instruction.Get("type")) {
  160. case "R":
  161. dst = instruction.Get("rt");
  162. if (!"zerogpfp".contains(dst)) {
  163. sources.add(dst);
  164. }
  165. // System.out.println("ENCONTRA R:'" + dst + "'" + "zerogpfp".contains(dst));
  166. case "I":
  167. if (instruction.eq("inst", "sw")) {
  168. sources.add(instruction.Get("rt"));
  169. }
  170. dst = instruction.Get("rs");
  171. if (!dst.equals("") && !"zerogpfp".contains(dst)) {
  172. sources.add(dst);
  173. }
  174. // System.out.println("ENCONTRA I:'" + dst + "'" + "zerogpfp".contains(dst));
  175. break;
  176. }
  177. if (sources.isEmpty()) {
  178. return;
  179. }
  180. //
  181. // System.out.println("In("
  182. // + instruction.Get("global.position")
  183. // + ") bd (" + sources + ") after {" + after
  184. // );
  185. String next;
  186. for (int i = after; i >= 0; i--) {
  187. // Se encontrou todas as dependencias, sai do loop
  188. if (sources.isEmpty()) {
  189. // System.out.println("Encontrei todas as dependecias de " + instruction.Get("global.position"));
  190. break;
  191. }
  192. tmp = instructions.get(i);
  193. if (tmp.eq("type", "label")) {
  194. continue;
  195. }
  196. dst = tmp.Get(instructionMap.get(tmp.Get("type")));
  197. for (Iterator<String> iterator = sources.iterator(); iterator.hasNext();) {
  198. next = iterator.next();
  199. // System.out.println(
  200. // "procurando em ( "
  201. // + tmp.Get("global.position")
  202. // + " ) dst( " + dst + " )"
  203. // + next
  204. // + ">" + dst.equals(next)
  205. // );
  206. if (dst.equals(next)) {
  207. // no =;
  208. // no.index = i;
  209. // Adiciona um no no grafo com a intrucao dependete
  210. n.deps.add(GetNo(tmp, false));
  211. iterator.remove();
  212. break;
  213. }
  214. }
  215. }
  216. // Busca recursiva pela dependencia das outras intrucoes
  217. }
  218. protected HashMap<String, No> instructionNodes = new HashMap<>();
  219. protected No GetNo(Instruction tmp, boolean root) {
  220. String key = tmp.Get("global.position");
  221. boolean create = false;
  222. if (!instructionNodes.containsKey(key)) {
  223. create = true;
  224. No n = new No(tmp, root);
  225. if (root) {
  226. graphs.get(loopId).add(n);
  227. }
  228. instructionNodes.put(key, n);
  229. }
  230. // System.out.println("GetNo:" + key + " (" + create + ")" + root);
  231. return instructionNodes.get(key);
  232. }
  233. }
  234. // 20: 0666763252 addiu sp,sp,-12 -- prolog| push stack frame
  235. // 24: 0001962017 addu fp,zero,sp -- prolog|copy fp ← sp
  236. // 28: 0605028353 addiu s0,zero,1 .0 -- copy _V1 ← 1
  237. // 32: 2949644288 sw s0,fp,0 .1 -- store content of s0 in _V1
  238. // 36: 0605093888 addiu s1,zero,0 .2 -- copy _V2 ← 0
  239. // 40: 2949709828 sw s1,fp,4 .3 -- store content of s1 in _V2
  240. // 44: 0134217789 j f4 <preenche+0xe0> .4 -- jump to preenche+_i3
  241. // 48: 0000000000 sll zero,zero,0 .4 -- Nop
  242. // 52: 0605159424 addiu s2,zero,0 .5 -- copy _V3 ← 0
  243. // 56: 2949775368 sw s2,fp,8 .6 -- store content of s2 in _V3
  244. // 60: 0134217779 j cc <preenche+0xb8> .7 -- jump to preenche+_i7
  245. // 64: 0000000000 sll zero,zero,0 .7 -- Nop
  246. // 68: 2412969988 lw s3,fp,4 .8 -- load content from _V2 in s3
  247. // 72: 0000000000 sll zero,zero,0 .8 -- Nop
  248. // 76: 0001267745 addu t3,zero,s3 .9 -- copy _T4 ← _V2
  249. // 80: 0000745536 sll t4,t3,1 .10 -- _T5 = _T4 << 1
  250. // 84: 2413035528 lw s4,fp,8 .11 -- load content from _V3 in s4
  251. // 88: 0000000000 sll zero,zero,0 .11 -- Nop
  252. // 92: 0001337377 addu t5,zero,s4 .12 -- copy _T7 ← _V3
  253. // 96: 0026042401 addu t4,t4,t5 .13 -- _T5 = _T5 + _T7
  254. //100: 0000815232 sll t6,t4,2 .14 -- _T11 = _T5 << 2
  255. //104: 2413101056 lw s5,fp,0 .15 -- load content from _V1 in s5
  256. //108: 0000000000 sll zero,zero,0 .15 -- Nop
  257. //112: 0059666465 addu t6,gp,t6 .16
  258. //116: 2916417536 sw s5,t6,0 .16 -- store content of t6 in _G13[_T11]
  259. //120: 2412969988 lw s3,fp,4 .17 -- load content from _V2 in s3
  260. //124: 0000000000 sll zero,zero,0 .17 -- Nop
  261. //128: 0001275937 addu t7,zero,s3 .18 -- copy _T14 ← _V2
  262. //132: 0001032256 sll t8,t7,1 .19 -- _T15 = _T14 << 1
  263. //136: 2413035528 lw s4,fp,8 .20 -- load content from _V3 in s4
  264. //140: 0000000000 sll zero,zero,0 .20 -- Nop
  265. //144: 0001327137 addu t0,zero,s4 .21 -- copy _T17 ← _V3
  266. //148: 0050905121 addu t8,t8,t0 .22 -- _T15 = _T15 + _T17
  267. //152: 0001591424 sll t1,t8,2 .23 -- _T21 = _T15 << 2
  268. //156: 2413101056 lw s5,fp,0 .24 -- load content from _V1 in s5
  269. //160: 0000000000 sll zero,zero,0 .24 -- Nop
  270. //164: 0059328545 addu t1,gp,t1 .25
  271. //168: 2905931792 sw s5,t1,16 .25 -- store content of t1 in _G23[_T21]
  272. //172: 2413101056 lw s5,fp,0 .26 -- load content from _V1 in s5
  273. //176: 0000000000 sll zero,zero,0 .26 -- Nop
  274. //180: 0649396225 addiu s5,s5,1 .27 -- _V1 = _V1 + 1
  275. //184: 2949971968 sw s5,fp,0 .28 -- store content of s5 in _V1
  276. //188: 2412773384 lw s0,fp,8 .29 -- load content from _V3 in s0
  277. //192: 0000000000 sll zero,zero,0 .29 -- Nop
  278. //196: 0638582785 addiu s0,s0,1 .30 -- _V3 = _V3 + 1
  279. //200: 2949644296 sw s0,fp,8 .31 -- store content of s0 in _V3
  280. //204: 2412838920 lw s1,fp,8 .32 -- load content from _V3 in s1
  281. //208: 0000000000 sll zero,zero,0 .32 -- Nop
  282. //212: 0604831746 addiu t5,zero,2 .33
  283. //216: 0036507683 subu v0,s1,t5 .33
  284. //220: 0071368665 bltz v0,44 <preenche+0x30> .33 -- branch if register < 0
  285. //224: 0000000000 sll zero,zero,0 .33 -- Nop
  286. //228: 2412904452 lw s2,fp,4 .34 -- load content from _V2 in s2
  287. //232: 0000000000 sll zero,zero,0 .34 -- Nop
  288. //236: 0642908161 addiu s2,s2,1 .35 -- _V2 = _V2 + 1
  289. //240: 2949775364 sw s2,fp,4 .36 -- store content of s2 in _V2
  290. //244: 2413166596 lw s6,fp,4 .37 -- load content from _V2 in s6
  291. //248: 0000000000 sll zero,zero,0 .37 -- Nop
  292. //252: 0604897282 addiu t6,zero,2 .38
  293. //256: 0047058979 subu v0,s6,t6 .38
  294. //260: 0071368651 bltz v0,34 <preenche+0x20> .38 -- branch if register < 0
  295. //264: 0000000000 sll zero,zero,0 .38 -- Nop
  296. //268: 0666697740 addiu sp,sp,12 -- epilog| pop stack frame
  297. //272: 0001962017 addu fp,zero,sp -- epilog| pop stack frame
  298. //276: 0065011720 jr ra -- epilog| return
  299. //280: 0666763248 addiu sp,sp,-16 -- prolog| push stack frame
  300. //284: 0001962017 addu fp,zero,sp -- prolog|copy fp ← sp
  301. //288: 0605028352 addiu s0,zero,0 .0 -- copy _V24 ← 0
  302. //292: 2949644288 sw s0,fp,0 .1 -- store content of s0 in _V24
  303. //296: 0605093888 addiu s1,zero,0 .2 -- copy _V25 ← 0
  304. //300: 2949709828 sw s1,fp,4 .3 -- store content of s1 in _V25
  305. //304: 0134217883 j 26c <multiplica+0x154> .4 -- jump to multiplica+_i11
  306. //308: 0000000000 sll zero,zero,0 .4 -- Nop
  307. //312: 0605159424 addiu s2,zero,0 .5 -- copy _V26 ← 0
  308. //316: 2949775368 sw s2,fp,8 .6 -- store content of s2 in _V26
  309. //320: 0134217873 j 244 <multiplica+0x12c> .7 -- jump to multiplica+_i15
  310. //324: 0000000000 sll zero,zero,0 .7 -- Nop
  311. //328: 0605224960 addiu s3,zero,0 .8 -- copy _V27 ← 0
  312. //332: 2949840908 sw s3,fp,12 .9 -- store content of s3 in _V27
  313. //336: 0134217848 j 1e0 <multiplica+0xc8> .10 -- jump to multiplica+_i19
  314. //340: 0000000000 sll zero,zero,0 .10 -- Nop
  315. //344: 2413035524 lw s4,fp,4 .11 -- load content from _V25 in s4
  316. //348: 0000000000 sll zero,zero,0 .11 -- Nop
  317. //352: 0001335329 addu t4,zero,s4 .12 -- copy _T28 ← _V25
  318. //356: 0000813120 sll t5,t4,1 .13 -- _T29 = _T28 << 1
  319. //360: 2413101068 lw s5,fp,12 .14 -- load content from _V27 in s5
  320. //364: 0000000000 sll zero,zero,0 .14 -- Nop
  321. //368: 0001404961 addu t6,zero,s5 .15 -- copy _T31 ← _V27
  322. //372: 0028207137 addu t5,t5,t6 .16 -- _T29 = _T29 + _T31
  323. //376: 0000882816 sll t7,t5,2 .17 -- _T35 = _T29 << 2
  324. //380: 0059734049 addu t7,gp,t7 .18
  325. //384: 2381840384 lw t8,t7,0 .18
  326. //388: 2413101068 lw s5,fp,12 .19 -- load content from _V27 in s5
  327. //392: 0000000000 sll zero,zero,0 .19 -- Nop
  328. //396: 0001392673 addu t0,zero,s5 .20 -- copy _T38 ← _V27
  329. //400: 0000542784 sll t1,t0,1 .21 -- _T39 = _T38 << 1
  330. //404: 2413232136 lw s7,fp,8 .22 -- load content from _V26 in s7
  331. //408: 0000000000 sll zero,zero,0 .22 -- Nop
  332. //412: 0001527841 addu t2,zero,s7 .23 -- copy _T41 ← _V26
  333. //416: 0019548193 addu t1,t1,t2 .24 -- _T39 = _T39 + _T41
  334. //420: 0000612480 sll t3,t1,2 .25 -- _T45 = _T39 << 2
  335. //424: 0059463713 addu t3,gp,t3 .26
  336. //428: 2372927504 lw s0,t3,16 .26
  337. //432: 0000000000 sll zero,zero,0 .26 -- Nop
  338. //436: 0001073185 addu t4,zero,s0 .26 -- copy t4 ← s0
  339. //440: 0051146776 mult t8,t4 .27 -- _T48 = _T37 * _T47
  340. //444: 0000028690 mflo t6 .27
  341. //448: 2412838912 lw s1,fp,0 .28 -- load content from _V24 in s1
  342. //452: 0000000000 sll zero,zero,0 .28 -- Nop
  343. //456: 0036603937 addu s1,s1,t6 .29 -- _V24 = _V24 + _T48
  344. //460: 2949709824 sw s1,fp,0 .30 -- store content of s1 in _V24
  345. //464: 2412904460 lw s2,fp,12 .31 -- load content from _V27 in s2
  346. //468: 0000000000 sll zero,zero,0 .31 -- Nop
  347. //472: 0642908161 addiu s2,s2,1 .32 -- _V27 = _V27 + 1
  348. //476: 2949775372 sw s2,fp,12 .33 -- store content of s2 in _V27
  349. //480: 2412969996 lw s3,fp,12 .34 -- load content from _V27 in s3
  350. //484: 0000000000 sll zero,zero,0 .34 -- Nop
  351. //488: 0604962818 addiu t7,zero,2 .35
  352. //492: 0040833059 subu v0,s3,t7 .35
  353. //496: 0071368665 bltz v0,158 <multiplica+0x40> .35 -- branch if register < 0
  354. //500: 0000000000 sll zero,zero,0 .35 -- Nop
  355. //504: 2413035524 lw s4,fp,4 .36 -- load content from _V25 in s4
  356. //508: 0000000000 sll zero,zero,0 .36 -- Nop
  357. //512: 0001327137 addu t0,zero,s4 .37 -- copy _T50 ← _V25
  358. //516: 0000544832 sll t2,t0,1 .38 -- _T51 = _T50 << 1
  359. //520: 2413166600 lw s6,fp,8 .39 -- load content from _V26 in s6
  360. //524: 0000000000 sll zero,zero,0 .39 -- Nop
  361. //528: 0001460257 addu t1,zero,s6 .40 -- copy _T53 ← _V26
  362. //532: 0021581857 addu t2,t2,t1 .41 -- _T51 = _T51 + _T53
  363. //536: 0000678016 sll t3,t2,2 .42 -- _T57 = _T51 << 2
  364. //540: 2413101056 lw s5,fp,0 .43 -- load content from _V24 in s5
  365. //544: 0000000000 sll zero,zero,0 .43 -- Nop
  366. //548: 0059463713 addu t3,gp,t3 .44
  367. //552: 2910126112 sw s5,t3,32 .44 -- store content of t3 in _G59[_T57]
  368. //556: 0605356032 addiu s5,zero,0 .45 -- copy _V24 ← 0
  369. //560: 2949971968 sw s5,fp,0 .46 -- store content of s5 in _V24
  370. //564: 2412773384 lw s0,fp,8 .47 -- load content from _V26 in s0
  371. //568: 0000000000 sll zero,zero,0 .47 -- Nop
  372. //572: 0638582785 addiu s0,s0,1 .48 -- _V26 = _V26 + 1
  373. //576: 2949644296 sw s0,fp,8 .49 -- store content of s0 in _V26
  374. //580: 2412838920 lw s1,fp,8 .50 -- load content from _V26 in s1
  375. //584: 0000000000 sll zero,zero,0 .50 -- Nop
  376. //588: 0604897282 addiu t6,zero,2 .51
  377. //592: 0036573219 subu v0,s1,t6 .51
  378. //596: 0071368636 bltz v0,148 <multiplica+0x30> .51 -- branch if register < 0
  379. //600: 0000000000 sll zero,zero,0 .51 -- Nop
  380. //604: 2412904452 lw s2,fp,4 .52 -- load content from _V25 in s2
  381. //608: 0000000000 sll zero,zero,0 .52 -- Nop
  382. //612: 0642908161 addiu s2,s2,1 .53 -- _V25 = _V25 + 1
  383. //616: 2949775364 sw s2,fp,4 .54 -- store content of s2 in _V25
  384. //620: 2412969988 lw s3,fp,4 .55 -- load content from _V25 in s3
  385. //624: 0000000000 sll zero,zero,0 .55 -- Nop
  386. //628: 0604962818 addiu t7,zero,2 .56
  387. //632: 0040833059 subu v0,s3,t7 .56
  388. //636: 0071368622 bltz v0,138 <multiplica+0x20> .56 -- branch if register < 0
  389. //640: 0000000000 sll zero,zero,0 .56 -- Nop
  390. //644: 0666697744 addiu sp,sp,16 -- epilog| pop stack frame
  391. //648: 0001962017 addu fp,zero,sp -- epilog| pop stack frame
  392. //652: 0065011720 jr ra