DataLayout.java 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  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 API.Types;
  8. import API.Utils;
  9. import ast.Node;
  10. import java.util.LinkedHashMap;
  11. import java.util.Map;
  12. import java.util.regex.Matcher;
  13. import java.util.regex.Pattern;
  14. /**
  15. *
  16. * @author Eugenio
  17. */
  18. public class DataLayout {
  19. protected String ID;
  20. protected LinkedHashMap<String, Node> values = new LinkedHashMap<>();
  21. // address guarda o endereço de cada variavel
  22. protected LinkedHashMap<String, DataEntry> entrys = new LinkedHashMap<>();
  23. protected LinkedHashMap<String, Integer> addressMap = new LinkedHashMap<>();
  24. // Quantidade minima de bytes que
  25. protected Integer minWordSize = 1, // 8 bits
  26. // Quantidade maximo de bytes que corresponde a uma palavra
  27. maxWordSize = 4; // 32 bits
  28. protected boolean initialized = false;
  29. protected Pattern normalId = Pattern.compile("(?<id>[\\w_]+)(\\[(?<index>\\w+)\\])?");
  30. //Endereço base do conjunto
  31. public Long BaseAddress = 0L;
  32. // public int offsetctrl = 0;
  33. // protected DataLayout global = null;
  34. // Guarda a quantidade de palavras do frame
  35. // É atualizado sempre que o mapeamento dos endereços sofre alteração.
  36. protected Long FrameSize = 0L;
  37. protected Long FP = 0L;
  38. public DataLayout(String ID) {
  39. this.ID = ID;
  40. }
  41. public void SetInitialized(boolean init) {
  42. initialized = init;
  43. }
  44. public Long Size() {
  45. return FrameSize;
  46. }
  47. @Override
  48. public String toString() {
  49. StringBuilder s = new StringBuilder("Data Frame <" + ID + ">:" + Size() + "\n");
  50. DataEntry value;
  51. for (Map.Entry<String, DataEntry> x : entrys.entrySet()) {
  52. value = x.getValue();
  53. s.append(x.getKey())
  54. .append(": ")
  55. .append(value.toString())
  56. .append("\n");
  57. }
  58. return s.toString();
  59. }
  60. protected String baseName(String id) {
  61. Matcher m = normalId.matcher(id);
  62. if (m.find()) {
  63. id = m.group("id");
  64. }
  65. return id;
  66. }
  67. protected String normalize(String id) {
  68. String shift = "0", index;
  69. Matcher m = normalId.matcher(id);
  70. if (m.find()) {
  71. id = m.group("id");
  72. index = m.group("index");
  73. if (Utils.IsNumber(index)) {
  74. shift = index;
  75. }
  76. }
  77. if (!id.contains(".")) {
  78. id += "." + shift;
  79. }
  80. // System.out.println("Normalize:" + id + "::" + or);
  81. return id;
  82. }
  83. public void Replace(String dst, String ori) {
  84. String rest;
  85. LinkedHashMap<String, Node> add = new LinkedHashMap<>();
  86. for (Map.Entry<String, Node> x : this.values.entrySet()) {
  87. rest = x.getKey().replace(dst, "");
  88. if (rest.equals("") || rest.substring(0, 1).equals(".")) {
  89. add.put(ori + rest, x.getValue());
  90. } else {
  91. add.put(x.getKey(), x.getValue());
  92. }
  93. }
  94. this.values = add;
  95. }
  96. void Remove(String dst) throws Exception {
  97. String alias;
  98. DataEntry de;
  99. Boolean updateFP = false;
  100. Long diff = 0L;
  101. for (Map.Entry<String, DataEntry> entry : entrys.entrySet()) {
  102. alias = entry.getKey();
  103. de = entry.getValue();
  104. if (alias.equals(dst)) {
  105. diff = de.getSize();
  106. FrameSize -= diff;
  107. entrys.remove(de);
  108. updateFP = true;
  109. }
  110. if (updateFP) {
  111. de.SetAddress(de.getAddress() - diff);
  112. }
  113. }
  114. FP -= diff;
  115. System.out.println("Remove:" + dst + "\n" + values);
  116. }
  117. // Pra baixo OK
  118. public Long Shift(String id) throws Exception {
  119. Long shift = 0L;
  120. Matcher m = normalId.matcher(id);
  121. if (m.find()) {
  122. String index = m.group("index");
  123. if (Utils.IsNumber(index)) {
  124. shift = Long.parseLong(index);
  125. }
  126. }
  127. return shift;
  128. }
  129. public Offset Offset(String id) throws Exception {
  130. String var = baseName(id);
  131. Long offset = 0L, shift = Shift(id);
  132. // if (id.contains("_G") && global != null) {
  133. // return global.Offset(id);
  134. // }
  135. try {
  136. offset = entrys.get(var).getAddress();
  137. // System.out.println("Offset from " + var);
  138. // System.out.println("Offset:" + id + ":\n" + offset);
  139. } catch (Exception e) {
  140. // System.out.println("address(" + id + ")[" + frameSize + "].:" + address + values);
  141. // System.out.println("ERROR:" + e.getMessage());
  142. throw new Exception(String.format("Invalid offset %s in context %s.", var, ID));
  143. }
  144. return new Offset(offset, shift);
  145. }
  146. public DataEntry Add(String alias, String type, Integer numElements) throws Exception {
  147. if (entrys.containsKey(alias)) {
  148. throw new Exception("A variavel '%s' previamente definida no frame.");
  149. }
  150. Long size = AllocSizeOf(type) * numElements;
  151. DataEntry de = new DataEntry(size);
  152. de.SetAddress(FP);
  153. // Atualiza o frame pointer
  154. FP += size;
  155. FrameSize += size;
  156. entrys.put(alias, de);
  157. return de;
  158. }
  159. protected Long AllocSizeOf(String type) throws Exception {
  160. // Se o tipo for um ponteiro retorna o tamanho maximo de uma palavra em bytes
  161. if (type.contains("*")) {
  162. return maxWordSize.longValue();
  163. }
  164. Integer size = Types.Size(type), i = maxWordSize;
  165. while (i < size) {
  166. i += maxWordSize;
  167. }
  168. return i.longValue();
  169. }
  170. public boolean contains(String id) {
  171. return entrys.get(id) != null;
  172. }
  173. }
  174. // System.out.println("Value:" + value);
  175. // s.append(x.getKey())
  176. // .append(": ")
  177. // .append(value.Get("ID"))
  178. // .append("\tsize:")
  179. // .append(value.Get("size"))
  180. // .append("\tvalue:")
  181. // .append(value.Get("default.value"))
  182. // .append("\n");
  183. // break;
  184. // protected LinkedList<Node> values = new LinkedList<>();//Map< String, String> Copy = new TreeMap<>();
  185. // protected HashMap<String, Integer> size = new LinkedHashMap<>();
  186. // protected HashMap<String, Integer> type = new LinkedHashMap<>();
  187. // protected HashMap<String, Integer> _offset_ = new LinkedHashMap<>();
  188. // protected static AbstractSyntaxTree ast;
  189. // public static int TYPE_ARGUMENT = 0;
  190. // public static int TYPE_VAR = 1;
  191. // public static int TYPE_RETURN = 2;
  192. // public static int TYPE_STACK = 3;
  193. //
  194. // /**
  195. // * Formata oS haSh para a impreSSão no metoglobalo toString
  196. // *
  197. // * @param hash
  198. // * @return
  199. // */
  200. // protected String hashToString(HashMap<String, Node> hash) {
  201. // String r = "";
  202. // for (Map.Entry<String, Node> entry : hash.entrySet()) {
  203. // String ID = entry.getKey();
  204. // Node value = entry.getValue();
  205. //// int type = this.type.Get(ID);
  206. // r += "{" + ID + ":" + value + "},";
  207. // }
  208. // return r;
  209. // }
  210. // public DataLayout copy(DataLayout data) {
  211. // String k;
  212. // for (Map.Entry<String, Node> x : data.values().entrySet()) {
  213. // System.out.println("Copy:" + x.getKey() + "-" + x.getValue());
  214. // k = x.getKey();
  215. // values.put(k, x.getValue());
  216. // address.put(k, data.address.get(x));
  217. // }
  218. // frameSize = data.frameSize;
  219. // return this;
  220. // }
  221. //
  222. // public void Set(String vname, Node copy) throws Exception {
  223. // values.put(
  224. // vname,
  225. // copy
  226. // );
  227. // address.put(vname, frameSize);
  228. // frameSize += copy.getInt("size");
  229. // }
  230. // public DataLayout Add(String alias, Node var, int elements) throws Exception {
  231. // // System.out.println("Add global to frame:" + var);
  232. //// int vSize = 1;
  233. //// int size = var.getInt("size"), vSize = 1;
  234. //// boolean isArray = var.eq("array", "true");
  235. //// if (isArray) {
  236. //// vSize = Tipos.Size(var.Get("type"));
  237. //// }
  238. // ArrayList<String> defaults = var.getList("default.values");
  239. //
  240. // Integer defSize = defaults.size();
  241. // String vname;
  242. // Node copy;
  243. // for (int i = 0; i < elements; i++) {
  244. // vname = alias + "." + i;
  245. // copy = var.copy().Set("default.value", (i < defSize ? defaults.get(i) : "0"));
  246. // Set(vname, copy);
  247. //// values.put(
  248. //// vname,
  249. //// Copy
  250. //// );
  251. ////
  252. //// address.put(vname, frameSize);
  253. //// frameSize += Copy.getInt("size");
  254. // }
  255. //
  256. // return this;
  257. // }
  258. //
  259. // public LinkedHashMap<String, Node> values() {
  260. // return values;
  261. // }
  262. //
  263. // public boolean has(String id) {
  264. // return values.containsKey(id);
  265. //// for (Map.Entry<String, Node> x : values.entrySet()) {
  266. //// if (x.getKey().equals(id)) {
  267. //// return true;
  268. //// }
  269. //// }
  270. //// return false;
  271. // }
  272. // public static void setAbstractSyntaxTree(AbstractSyntaxTree ast) {
  273. // DataLayout.ast = ast;
  274. // }