Registers.java 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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
  5. varsin the editor.
  6. */
  7. package targets.mips;
  8. import java.util.LinkedList;
  9. /**
  10. *
  11. * @author Eugenio
  12. */
  13. public class Registers {
  14. protected int arg = 0;
  15. public String Arg() {
  16. return "a" + (arg++);
  17. }
  18. public void ResetArgs() {
  19. arg = 0;
  20. }
  21. protected final LinkedList<String> avaliableSaved = new LinkedList<String>() {
  22. {
  23. for (int i = 0; i < 8; i++) {
  24. add("s" + i);
  25. }
  26. }
  27. };
  28. protected final LinkedList<String> avaliableTemp = new LinkedList<String>() {
  29. {
  30. for (int i = 0; i < 9; i++) {
  31. add("t" + i);
  32. }
  33. }
  34. };
  35. protected final LinkedList<String> usedSaved = new LinkedList<>();
  36. protected final LinkedList<String> usedTemp = new LinkedList<>();
  37. public Registers() {
  38. }
  39. public boolean Free(String reg) throws Exception {
  40. return exec(reg, "free") != null;
  41. }
  42. public String Lock(String reg) throws Exception {
  43. return exec(reg, "lock");
  44. }
  45. private String exec(String reg, String act) throws Exception {
  46. // System.out.println("Exec:" + reg + ":" + act);
  47. LinkedList<String> u, a;
  48. String r = null;
  49. switch (reg.substring(0, 1)) {
  50. case "t":
  51. u = usedTemp;
  52. a = avaliableTemp;
  53. break;
  54. case "s":
  55. u = usedSaved;
  56. a = avaliableSaved;
  57. break;
  58. default:
  59. throw new Exception("Try " + act + " register '%s'");
  60. }
  61. switch (act) {
  62. case "free":
  63. if (!u.isEmpty()) {
  64. // System.out.println("Try remove(" + reg + ") from " + u);
  65. // Encontra o registrador usado e coloca como disponivel
  66. int index = u.indexOf(reg);
  67. if (index >= 0) {
  68. r = u.remove(index);
  69. a.add(r);
  70. }
  71. }
  72. break;
  73. case "lock":
  74. if (!a.isEmpty()) {
  75. // Pega o primeiro registrador disponivel e marca como usado
  76. r = a.removeFirst();
  77. u.add(r);
  78. }
  79. break;
  80. }
  81. return r;
  82. }
  83. }