Api.java 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  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 IntermediaryCode.Allocation;
  8. import ast.Node;
  9. import compiler.IvannosysCompiler;
  10. import java.util.ArrayList;
  11. import java.util.regex.Matcher;
  12. import java.util.regex.Pattern;
  13. /**
  14. *
  15. * @author Eugenio
  16. */
  17. public class Api {
  18. protected static boolean inicializada = false;
  19. public static void Init(IvannosysCompiler compiler) throws Exception {
  20. if (inicializada) {
  21. return;
  22. }
  23. BuildParams.Set("display.PARSETREE", "false");
  24. BuildParams.Set("display.ERRORS", "false");
  25. BuildParams.Set("display.AST", "true");
  26. BuildParams.Set("mode", "developement");
  27. /*Inicializando importes*/
  28. /*Inicializando constantes*/
  29. // Constantes.add("FALSE", "bool", "0", "global");
  30. // Constantes.add("false", "bool", "0", "global");
  31. // Constantes.add("TRUE", "bool", "1", "global");
  32. // Constantes.add("true", "bool", "1", "global");
  33. // Constantes.add("null", "bool", "0", "global");
  34. // Constantes.add("NULL", "bool", "0", "global");
  35. // Constantes.add("__BASE_HEAP__", "int", "0", "global");
  36. // Constantes.add("__MAX_MEM__", "int", "1024", "global");
  37. /*Inicializando tipos*/
  38. Types._init();
  39. Types.Add(Types.INTEGER, new Node(Types.INTEGER)
  40. .S("type", Types.INTEGER)
  41. .S("size", 1)
  42. .S("file", "std")
  43. .S("scope", "")
  44. .S("primitive", "true")
  45. .S("class", "type")
  46. .S("public", "true")
  47. );
  48. Types.Add(Types.BOOLEAN, new Node(Types.BOOLEAN)
  49. .S("type", Types.BOOLEAN)
  50. .S("size", 1)
  51. .S("file", "std")
  52. .S("scope", "")
  53. .S("primitive", "true")
  54. .S("class", "type")
  55. .S("public", "true")
  56. );
  57. Types.Add(Types.CHAR, new Node(Types.CHAR)
  58. .S("type", Types.CHAR)
  59. .S("size", 1)
  60. .S("file", "std")
  61. .S("scope", "")
  62. .S("primitive", "true")
  63. .S("class", "type")
  64. .S("public", "true")
  65. );
  66. // Registra os processadores de codigo padrão
  67. CodeProcessor.Add("ir.clear.labels", new Processors.RemoveUnusedLabelsProcessor());
  68. CodeProcessor.Add("ir.basic.blocks", new Processors.BasicBlockProcessor());
  69. CodeProcessor.Add("ir.o.load_store", new Processors.LoadStoreProcessor());
  70. CodeProcessor.Add("ir.o.VariablesConstants", new Processors.VariablesConstantsProcessor());
  71. CodeProcessor.Add("ir.o.DeadInstructions", new Processors.DeadInstructionsProcessor());
  72. CodeProcessor.Add("ir.o.copies", new Processors.CodeOtimizadorProcessor(true));
  73. // Processadores do alvo mips
  74. CodeProcessor.Add("mips.copy.dep", new targets.mips.MipsCopyDepsProcessor());
  75. CodeProcessor.Add("mips.register.alloc", new targets.mips.MipsAllocatorProcessor());
  76. CodeProcessor.Add("mips.o.L1", new targets.mips.MipsOtimizationProcessor());
  77. CodeProcessor.Add("mips.update.address", new targets.mips.MipsUpdateAddressProcessor());
  78. // Define os processadores padrão da IR
  79. CodeProcessor.On(
  80. "IR",
  81. "CloseBlock",
  82. // Remove labels não referenciadas
  83. "ir.clear.labels,"
  84. // Remove copias
  85. + "ir.o.copies,"
  86. // Propagacao de constantes
  87. // + "ir.o.VariablesConstants,"
  88. // Remove instruções que não tem impacto algum na execusao
  89. // + "ir.o.DeadInstructions,"
  90. // Extrai os blocos basicos de uma subrotina
  91. + "ir.basic.blocks,"
  92. // Remove loads e stores desnecessarios
  93. // + "ir.o.load_store"
  94. );
  95. Imports._init(compiler);
  96. // Constantes._init();
  97. // Types.add(Types.UNDEFINED, 0);
  98. // Types.add(Types.VOID, 0);
  99. // Types.add("string", 1);
  100. // Types.add("thread", 1);
  101. // Types.add("reference", 1);
  102. Interfaces._init();
  103. /*Inicializando funcoes*/
  104. Functions._init();
  105. /*Inicializando parametros de compilacao*/
  106. BuildParams._init();
  107. // HashMap<String, String> print = new HashMap<>();
  108. // Functions.add("print", "void", print, "program");
  109. //
  110. // HashMap<String, String> read = new HashMap<>();
  111. // Functions.add("read", "void", print, "program");
  112. //
  113. // HashMap<String, String> sync = new HashMap<>();
  114. // Functions.add("sync", "void", sync, "program");
  115. //
  116. // HashMap<String, String> exec = new HashMap<>();
  117. // Functions.add("exec", "void", exec, "program");
  118. //
  119. // HashMap<String, String> synexec = new HashMap<>();
  120. // Functions.add("synexec", "void", synexec, "program");
  121. /*Inicializando Traps*/
  122. // Traps._init();
  123. /*Inicializando Threads*/
  124. // Threads._init();
  125. /*Inicializando Variaveis*/
  126. Variaveis._init();
  127. /*Variavel global de acesso a structs das threads definidas no programa*/
  128. // int cod = Variaveis.add("__THREADS__", "magica", "program");
  129. // Node var = new Node("__THREADS__");
  130. // var.S("class", "var").S("tipo", "magica*").S("escopo", "program");
  131. // Variaveis.var(cod, var);
  132. inicializada = true;
  133. }
  134. /**
  135. * Retorna o numero_da_linha + ':' + numero_da_coluna + '|' nome_arquivo
  136. *
  137. * @param n
  138. * @return
  139. */
  140. public static String getFormatedPosition(Node n) {
  141. return "[" + n.G("line") + ":" + n.G("col") + "|" + n.G("file") + "\t]";
  142. }
  143. /**
  144. * Retorna numero de ocorrenciaS de um Padrão dentro de uma String
  145. *
  146. * @param pattern
  147. * @param input
  148. * @return
  149. */
  150. public static int countRegexOcorrences(String pattern, String input) {
  151. Matcher matcher = Pattern.compile(pattern).matcher(input);
  152. int count = 0;
  153. while (matcher.find()) {
  154. count++;
  155. }
  156. return count;
  157. }
  158. public static int countChatOcorrece(String input, char c) {
  159. int charCount = 0;
  160. for (int i = 0; i < input.length(); i++) {
  161. if (input.charAt(i) == c) {
  162. charCount++;
  163. }
  164. }
  165. return charCount;
  166. }
  167. /**
  168. * Retorna valor de um indice em um vetor unidirecional. <br>
  169. * var[valor] -> valor;
  170. *
  171. * @param var
  172. * @return
  173. */
  174. public static String getIndice(String var) {
  175. return var.split("\\[")[1].replace("]", "");
  176. }
  177. public static String num2bin(int num, int SizeBits) {
  178. String binario = Integer.toBinaryString(num);
  179. String paded = Utils.pad(SizeBits, binario);
  180. return Utils.cut(SizeBits, paded);
  181. }
  182. public static String num2bin(String num, int SizeBits) {
  183. int val = Integer.parseInt(num);
  184. return num2bin(val, SizeBits);
  185. }
  186. public static String reg2bin5(String reg) throws Exception {
  187. return reg2Bin(reg, 5);
  188. }
  189. public static String reg2Bin(String reg, int SizeBits) throws Exception {
  190. if (reg.equals("")) {
  191. return num2bin(0, SizeBits);
  192. }
  193. // System.out.print("Valor do reg:" + reg);
  194. int val = Allocation.reg2Int(reg);
  195. // System.out.println(":val:" + val);
  196. return num2bin(val, SizeBits);
  197. }
  198. public static String bin2Hex(String bin, int size) {
  199. // System.out.println("bin:[" + bin + "]");
  200. if (bin.equals("")) {
  201. return "falha";
  202. }
  203. return String.format("%" + size + "X", Long.parseLong(bin, 2));
  204. }
  205. public static String implode(char delimiter, ArrayList<String> elements) {
  206. String tmp = elements.toString();
  207. return tmp.substring(1, tmp.length() - 1).replace(", ", "" + delimiter);
  208. }
  209. public static boolean IsValue(String param) {
  210. if (Utils.isNumber(param)) {
  211. return true;
  212. } else if (param.toLowerCase().matches("(true|false)")) {
  213. return true;
  214. }
  215. return false;
  216. }
  217. public static String Value(String v) {
  218. String vt = v.toLowerCase();
  219. if (vt.matches("(true|false)")) {
  220. return vt.equals("true") ? "1" : "0";
  221. } else {
  222. return v;
  223. }
  224. }
  225. public static String clearID(String id) {
  226. return id.replace("*", "").replace("&", "");
  227. }
  228. }