MipsUpdateAddressMiddleware.java 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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 targets.mips;
  7. import API.MiddlewareInterface;
  8. import common.Block;
  9. import common.Code;
  10. import common.Instruction;
  11. import common.Label;
  12. import java.util.HashMap;
  13. import java.util.LinkedHashMap;
  14. import java.util.Map;
  15. /**
  16. *
  17. * @author EUGENIO CARVALHO
  18. */
  19. public class MipsUpdateAddressMiddleware implements MiddlewareInterface {
  20. protected HashMap<String, Boolean> updateOffsetShift = new HashMap<String, Boolean>() {
  21. {
  22. put("beq", true);
  23. put("bne", true);
  24. put("blez", true);
  25. put("bltz", true);
  26. put("bgez", true);
  27. put("bgtz", true);
  28. }
  29. };
  30. public MipsUpdateAddressMiddleware() {
  31. }
  32. @Override
  33. public void Exec(Code c, LinkedHashMap<String, MiddlewareInterface> cp) throws Exception {
  34. Long offset = 0L;
  35. Label label;
  36. for (Map.Entry<String, Block> x : c.Blocks().entrySet()) {
  37. for (Instruction inst : x.getValue().Instructions()) {
  38. // Atualziza o offset das instruições de branch
  39. if (!updateOffsetShift.containsKey(inst.Get("inst"))) {
  40. continue;
  41. }
  42. // System.out.println("UPDATE ADDRESS: " + inst.Get("label") + " c.labels:" + c.labels);
  43. label = c.labels.get(inst.Get("label"));
  44. // offset = Integer.parseInt(def.get(0));
  45. // offset = Integer.parseInt(def.get(1).split(""));
  46. // System.out.println("Offset:" + +"----" +);
  47. // offset = Integer.parseInt(def.get(0));
  48. // offset = ((offset - inst.GetInt("global.position")) / 4) - 1;
  49. // System.out.println("LABEL:"
  50. // + label.GetGlobalPosition()
  51. // + "/"
  52. // + Utils.FormatNum(label.GetGlobalPosition(), "hex")
  53. // + "/"
  54. // + inst.GetInt("global.position")
  55. // + "/"
  56. // + Utils.FormatNum(inst.GetInt("global.position"), "hex")
  57. // );
  58. offset = ((label.GetGlobalPosition() - inst.GetInt("global.position")) / 4) - 1;
  59. // System.out.println("OffsetCalculado:" + offset);
  60. inst.Set("offset", offset);
  61. }
  62. }
  63. // System.out.println("Labels:" + c.labels);
  64. // String[] parts;
  65. // int index;
  66. // Update address of labels
  67. // for (Map.Entry<String, ArrayList<String>> entry : c.labels.entrySet()) {
  68. // ArrayList<String> labeldata = entry.getValue();
  69. // labeldata.set(0, (Integer.parseInt(labeldata.get(0)) * 4) + "");
  70. // try {
  71. // parts = labeldata.get(1).split("(\\+)");
  72. // if (parts.length > 1) {
  73. // index = Integer.parseInt(parts[1]);
  74. // labeldata.set(1, parts[0] + "+" + (index * 4));
  75. // }
  76. // } catch (Exception e) {
  77. // }
  78. // }
  79. }
  80. }
  81. // atualiza o indice da instrução de pc + 4
  82. // if (inst.isNumber("global.position")) {
  83. // inst.Set("block.position", inst.GetInt("block.position") * 4);
  84. // inst.Set("global.position", inst.GetInt("global.position") * 4);
  85. // } else if (inst.eq("type", "label")) {
  86. // // atualiza o endereco dos labels
  87. // inst.Set("reference.position", inst.GetInt("reference.position") * 4);
  88. // inst.Set("global.reference.position", inst.GetInt("global.reference.position") * 4);
  89. // }