UpdateAddressProcessor.java 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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. offset = (offset - y.getInt("global.position")) / 4;
  56. // System.out.println("Offset:" + offset);
  57. y.S("offset", offset);
  58. }
  59. }
  60. }
  61. // System.out.println("Labels:" + c.labels);
  62. String[] parts;
  63. int index;
  64. // Update address of labels
  65. for (Map.Entry<String, ArrayList<String>> entry : c.labels.entrySet()) {
  66. ArrayList<String> labeldata = entry.getValue();
  67. labeldata.set(0, (Integer.parseInt(labeldata.get(0)) * 4) + "");
  68. try {
  69. parts = labeldata.get(1).split("(\\+)");
  70. if (parts.length > 1) {
  71. index = Integer.parseInt(parts[1]);
  72. labeldata.set(1, parts[0] + "+" + (index * 4));
  73. }
  74. } catch (Exception e) {
  75. }
  76. }
  77. }
  78. }