UpdateAddressProcessor.java 3.1 KB

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