MipsUpdateAddressProcessor.java 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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.CodeProcessorInterface;
  8. import common.Block;
  9. import common.Code;
  10. import common.Instruction;
  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. public class MipsUpdateAddressProcessor implements CodeProcessorInterface {
  20. protected HashMap<String, Boolean> updateOffsetShift;/*
  21. public MipsUpdateAddressProcessor() {
  22. }
  23. (parseInt("2c",10) * 4).toString(16)
  24. */
  25. public MipsUpdateAddressProcessor() {
  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, CodeProcessorInterface> cp) throws Exception {
  39. Integer offset;
  40. for (Map.Entry<String, Block> x : c.getBlocks().entrySet()) {
  41. for (Instruction inst : x.getValue().Instructions()) {
  42. // atualiza o indice da instrução de pc + 4
  43. if (inst.isNumber("global.position")) {
  44. inst.Set("block.position", inst.getInt("block.position") * 4);
  45. inst.Set("global.position", inst.getInt("global.position") * 4);
  46. } else if (inst.eq("type", "label")) {
  47. // atualiza o endereco dos labels
  48. inst.Set("reference.position", inst.getInt("reference.position") * 4);
  49. inst.Set("global.reference.position", inst.getInt("global.reference.position") * 4);
  50. }
  51. // Atualziza o offset das instruições de branch
  52. if (updateOffsetShift.containsKey(inst.Get("inst"))) {
  53. // System.out.println("UPDATE ADDRESS: " + inst.Get("label") + " c.labels:" + c.labels);
  54. offset = Integer.parseInt(c.labels.get(inst.Get("label")).get(0)) * 4;
  55. // System.out.println("Offset:" + offset + "--" + y.getInt("global.position"));
  56. offset = ((offset - inst.getInt("global.position")) / 4) - 1;
  57. // System.out.println("OffsetCalculado:" + offset);
  58. inst.Set("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. }