UpdateAddressProcessor.java 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  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.LinkedHashMap;
  13. import java.util.Map;
  14. /**
  15. *
  16. * @author EUGENIO CARVALHO
  17. */
  18. class UpdateAddressProcessor implements CodeProcessing {
  19. public UpdateAddressProcessor() {
  20. }
  21. /*
  22. (parseInt("2c",10) * 4).toString(16)
  23. */
  24. @Override
  25. public void Exec(Code c, LinkedHashMap<String, CodeProcessing> cp) throws Exception {
  26. for (Map.Entry<String, Block> x : c.getBlocks().entrySet()) {
  27. for (Instruction y : x.getValue().Instructions()) {
  28. // atualiza o indice da instrução de pc + 4
  29. if (y.isNumber("global.position")) {
  30. y.S("block.position", y.getInt("block.position") * 4);
  31. y.S("global.position", y.getInt("global.position") * 4);
  32. } else if (y.eq("type", "label")) {
  33. // atualiza o endereco dos labels
  34. y.S("reference.position", y.getInt("reference.position") * 4);
  35. y.S("global.reference.position", y.getInt("global.reference.position") * 4);
  36. }
  37. }
  38. }
  39. String[] parts;
  40. int index;
  41. // Update address of labels
  42. for (Map.Entry<String, ArrayList<String>> entry : c.labels.entrySet()) {
  43. ArrayList<String> labeldata = entry.getValue();
  44. labeldata.set(0, (Integer.parseInt(labeldata.get(0)) * 4) + "");
  45. try {
  46. parts = labeldata.get(1).split("(\\+)");
  47. if (parts.length > 1) {
  48. index = Integer.parseInt(parts[1]);
  49. labeldata.set(1, parts[0] + "+" + (index * 4));
  50. }
  51. } catch (Exception e) {
  52. }
  53. }
  54. }
  55. }