Api.java 8.6 KB

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