Block.java 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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 IntermediaryCode;
  7. import API.Instruction;
  8. import java.util.HashMap;
  9. import java.util.LinkedList;
  10. public class Block {
  11. public static int uniq = 0;
  12. public LinkedList<Instruction> instructions = new LinkedList<>();
  13. protected String name;
  14. protected Code code;
  15. protected HashMap<String, Instruction> labels = new HashMap<>();
  16. protected DataFrame Data;
  17. public int BaseAddress = 0;
  18. public int CurrentAddress = 0;
  19. public boolean HasCall;
  20. public int nonLabelCount;
  21. Block(String name, Code c) {
  22. // System.out.println("Open block:" + name + ":" + c.PosicaoLabel);
  23. code = c;
  24. BaseAddress = c.Position();
  25. // BaseAddress = c.PosicaoLabel;
  26. this.name = name;
  27. Data = new DataFrame(name, code.GData());
  28. // labels = new HashMap<>();
  29. // Data = new LinkedHashMap<>();
  30. // constants = new LinkedHashMap<>();
  31. // reverseName = new HashMap<>();
  32. // pointers = new HashMap<>();
  33. // ocorrencesPerBlocks = new LinkedHashMap<>();
  34. }
  35. public LinkedList<Instruction> Instructions() {
  36. return this.instructions;
  37. }
  38. public boolean HasCall() {
  39. return HasCall;
  40. }
  41. public Block Add(Instruction nr) {
  42. int pos = CurrentAddress;
  43. if (!nr.eq("type", "label")) {
  44. // nr.S("reference.position", pos);
  45. // } else {
  46. // nr.S("block.position", pos);
  47. // nr.S("block.position.origin", pos);
  48. CurrentAddress++;
  49. }
  50. // System.out.println("Add:" + nr);
  51. this.instructions.add(nr);
  52. return this;
  53. }
  54. public Block Remove(Instruction instruction) throws Exception {
  55. // System.out.println("Remove indtruction do block:" + instruction);
  56. this.instructions.remove(instruction);
  57. return this;
  58. }
  59. public String getName() {
  60. return this.name;
  61. }
  62. /**
  63. * Retorna o grupo de variaveiS de eScopo locaiS
  64. *
  65. * @return
  66. */
  67. public DataFrame Data() {
  68. return this.Data;
  69. }
  70. /**
  71. * Retorna o grupo de variaveiS de eScopo global
  72. *
  73. * @return
  74. */
  75. public DataFrame GData() {
  76. return this.code.GData();
  77. }
  78. public DataFrame Data(DataFrame d) {
  79. return this.Data.copy(d);
  80. }
  81. public void Close() throws Exception {
  82. Update();
  83. }
  84. public void Update() throws Exception {
  85. CurrentAddress = 0;
  86. // System.out.println("Update:" + instructions.size());
  87. for (Instruction n : instructions) {
  88. // System.out.println("Update->:" + CurrentAddress + ":" + code.Position());
  89. if (!n.eq("type", "label")) {
  90. n.set("block.position", CurrentAddress)
  91. .set("block.position.origin", CurrentAddress++)
  92. .set("global.position", code.PositionInc());
  93. } else {
  94. n.set("reference.position", CurrentAddress)
  95. .set("global.reference.position", code.Position());
  96. code.RegisterLabelAddress(n.G("label"));
  97. }
  98. }
  99. // System.out.println("}");
  100. }
  101. public int LastAddress() {
  102. return Instructions().size() - 1;
  103. }
  104. public Instruction RemoveLast() {
  105. System.out.println("Remove indtruction do block:");
  106. return instructions.removeLast();
  107. }
  108. public Block ReplaceVar(String find, String replace) {
  109. // System.out.println("Replace " + find + " -> " + replace);
  110. for (Instruction inst : instructions) {
  111. for (String p : new String[]{"dst", "p1", "p2"}) {
  112. if (inst.eq(p, find)) {
  113. inst.S(p, replace);
  114. }
  115. }
  116. }
  117. return this;
  118. }
  119. public int CountInstructions() {
  120. return nonLabelCount;
  121. }
  122. }