Block.java 4.3 KB

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