Types.java 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  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 API;
  7. import ast.Node;
  8. import java.util.ArrayList;
  9. import java.util.HashMap;
  10. import java.util.Map;
  11. import java.util.Set;
  12. import java.util.TreeMap;
  13. /**
  14. *
  15. * @author Eugenio
  16. */
  17. public class Types {
  18. // public static AbstractSyntaxTree ast;
  19. public static HashMap<String, Node> _types = new HashMap<>();
  20. // Tamanho em bytes de cada tipo
  21. public static HashMap<String, Integer> _sizes = new HashMap<>();
  22. public static HashMap<String, Integer> primitiveTypes = new HashMap<>();
  23. public static HashMap<String, HashMap<String, String>> _propList = new HashMap<>();
  24. public static HashMap<String, Integer> _offsetList = new HashMap<>();
  25. public static HashMap<String, TreeMap<Integer, ArrayList<String>>> baseLayoutMeta = new HashMap<>();
  26. // Tamanho de um ponteiro em bytes
  27. public static Integer PTR_SIZE = 4;
  28. public static HashMap<String, Integer> getSizes() {
  29. return _sizes;
  30. }
  31. public static void _init() throws Exception {
  32. HashMap<String, Integer> types = new HashMap<String, Integer>() {
  33. {
  34. //Numeric types
  35. // uint8 the set of all unsigned 8-bit integers (0 to 255)
  36. // uint16 the set of all unsigned 16-bit integers (0 to 65535)
  37. // uint32 the set of all unsigned 32-bit integers (0 to 4294967295)
  38. // uint64 the set of all unsigned 64-bit integers (0 to 18446744073709551615)
  39. // int8 the set of all signed 8-bit integers (-128 to 127)
  40. // int16 the set of all signed 16-bit integers (-32768 to 32767)
  41. // int32 the set of all signed 32-bit integers (-2147483648 to 2147483647)
  42. // int64 the set of all signed 64-bit integers (-9223372036854775808 to 9223372036854775807)
  43. // float32 the set of all IEEE-754 32-bit floating-point numbers
  44. // float64 the set of all IEEE-754 64-bit floating-point numbers
  45. // complex64 the set of all complex numbers with float32 real and imaginary parts
  46. // complex128 the set of all complex numbers with float64 real and imaginary parts
  47. // byte alias for uint8
  48. // rune alias for int32
  49. put("byte", 1);
  50. put("uint8", 1);
  51. put("uint16", 2);
  52. put("uint32", 4);
  53. put("uint64", 8);
  54. put("int8", 1);
  55. put("int16", 2);
  56. put("int32", 4);
  57. put("int64", 8);
  58. put("float32", 4);
  59. put("float64", 8);
  60. put("complex64", 8);
  61. put("complex128", 16);
  62. put("bool", 1);
  63. put("char", 1);
  64. }
  65. };
  66. String type;
  67. for (Map.Entry<String, Integer> entry : types.entrySet()) {
  68. type = entry.getKey();
  69. Add(type, new Node(type)
  70. .Set("size", entry.getValue())
  71. .Set("file", "std")
  72. .Set("scope", "")
  73. .Set("primitive", "true")
  74. .Set("block", "true") // Diz que não deve editar o tipo
  75. .Set("class", "type")
  76. .Set("public", "true")
  77. );
  78. }
  79. }
  80. // Deve ser chamada no fim da leitura do listner para atuaizar os tamanhos dos tipos
  81. public static void Update() throws Exception {
  82. Node def;
  83. for (Map.Entry<String, Node> entry : _types.entrySet()) {
  84. Node type = entry.getValue();
  85. if (type.eq("block", "true")) {
  86. continue;
  87. }
  88. // Completa a definicao da struct importando os atributos herdados
  89. for (Node t : type.findAll("type", "class", 1)) {
  90. def = Get(t.Get("type"));
  91. for (Node attr : def.findAll("attr", "class")) {
  92. attr.Set("path", def.getText());
  93. type.addFilho(attr);
  94. }
  95. }
  96. LayoutType(type);
  97. }
  98. }
  99. protected static void LayoutType(Node type) throws Exception {
  100. // System.out.println("type:" + type);
  101. String id, t = type.Get("type");
  102. Integer size,
  103. // Atualiza o tamanho to tipo
  104. structSize = 0;
  105. if (baseLayoutMeta.containsKey(t)) {
  106. return;
  107. }
  108. TreeMap<Integer, ArrayList<String>> layout = new TreeMap<>();
  109. baseLayoutMeta.put(t, layout);
  110. for (Node attr : type.findAll("attr", "class", 1)) {
  111. id = attr.Get("value");
  112. size = Size(attr.find("type").Get("type"));
  113. if (!layout.containsKey(size)) {
  114. layout.put(size, new ArrayList<>());
  115. }
  116. layout.get(size).add(id);
  117. switch (attr.Get("pointer")) {
  118. case "*":
  119. structSize += PTR_SIZE;
  120. break;
  121. default:
  122. structSize += Size(attr.childrens().get(0).Get("type"));
  123. }
  124. }
  125. // System.out.println("map:" + layout);
  126. _sizes.put(type.getText(), structSize);
  127. }
  128. protected static void LayoutType(String type) {
  129. }
  130. // protected static void UpdateSize(Node type) throws Exception {
  131. //
  132. // for (Node attr : type.findAll("attr", "class")) {
  133. //
  134. // switch (attr.Get("pointer")) {
  135. // case "*":
  136. // size += PTR_SIZE;
  137. // break;
  138. // default:
  139. // size += Size(attr.childrens().get(0).Get("type"));
  140. // }
  141. //
  142. // }
  143. // _sizes.put(type.getText(), structSize);
  144. // }
  145. /**
  146. * Adiciona um tipo a linguagem
  147. *
  148. * @param id
  149. * @param type
  150. * @throws java.lang.Exception
  151. */
  152. public static void Add(String id, Node type) throws Exception {
  153. if (_types.containsKey(id)) {
  154. throw new Exception(String.format("Tipo '%s' previamente definido na linha %s", id, _types.get(id).Get("line", "")));
  155. }
  156. if (!type.Has("public")) {
  157. type.Set("public", Base.IsPublic(id) ? "true" : "false");
  158. }
  159. type.Set("type", id)
  160. .Set("value", id);
  161. if (type.Has("size")) {
  162. _sizes.put(id, type.GetInt("size"));
  163. }
  164. _types.put(id, type);
  165. // Adiciona ao controle de tipos primitivos
  166. if (type.eq("primitive", "true")) {
  167. getPrimitiveTypes().put(type.Get("value"), 1);
  168. }
  169. }
  170. /**
  171. * Retorna verdadeiro Se o tipo exiStir
  172. *
  173. * @param type
  174. * @return true Se o tipo exiSte
  175. * @throws Exception caSo tipo não exiSta
  176. */
  177. public static Node Get(String id) throws Exception {
  178. if (!_types.containsKey(id)) {
  179. throw new Exception(String.format("Tipo '%s' não definido.", id));
  180. }
  181. return _types.get(id);
  182. }
  183. public static boolean Defined(String id) {
  184. return _types.containsKey(id);
  185. }
  186. public static void List() {
  187. System.out.println("Types:\n\t" + _sizes);
  188. }
  189. public static Integer Size(String type) throws Exception {
  190. if (!_sizes.containsKey(type)) {
  191. Node t = Get(type);
  192. LayoutType(t);
  193. }
  194. return _sizes.get(type);
  195. }
  196. public static Integer Shift(String type, ArrayList<String> path) throws Exception {
  197. return Shift(type, path.get(path.size() - 1));
  198. }
  199. public static Integer Shift(String type, String attrib) throws Exception {
  200. Node T = Get(type);
  201. String attrType;
  202. Integer shift = 0;
  203. // System.out.println("Shift header:" + type + ":" + attrib + ":" + T);
  204. // System.out.println("Shift:{");
  205. for (Node attr : T.findAll("attr", "class", 1)) {
  206. attrType = attr.childrens().get(0).Get("type");
  207. // System.out.println("Shift:" + attr.getText() + "[" + attrib + "]");
  208. if (!attr.getText().equals(attrib)) {
  209. shift += Size(attrType);
  210. //incrementar e buscar no proximo
  211. continue;
  212. }
  213. break;
  214. }
  215. // System.out.println("}");
  216. return shift;
  217. }
  218. public static HashMap<String, Integer> getPrimitiveTypes() {
  219. return primitiveTypes;
  220. }
  221. public static boolean Primitive(String type) {
  222. return primitiveTypes.containsKey(type);
  223. }
  224. public static String[] getPrimitiveTypesList() {
  225. Set<String> set = primitiveTypes.keySet();
  226. String[] ret = new String[set.size()];
  227. Integer i = 0;
  228. for (String s : set) {
  229. ret[i++] = s;
  230. }
  231. return ret;
  232. }
  233. }