Block.java 4.2 KB

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