Block.java 4.4 KB

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