Api.java 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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 inited = false;
  19. public static void Init(IvannosysCompiler compiler) throws Exception {
  20. if (inited) {
  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 tipos*/
  28. Types._init();
  29. // Registra os processadores de codigo padrão
  30. Middleware.Add("ir.clear.labels", new middlewares.RemoveUnusedLabelsMiddleware());
  31. Middleware.Add("ir.basic.blocks", new middlewares.BasicBlockMiddleware());
  32. Middleware.Add("ir.o.load_store", new middlewares.LoadStoreMiddleware());
  33. Middleware.Add("ir.o.VariablesConstants", new middlewares.VariablesConstantsMiddleware());
  34. Middleware.Add("ir.o.DeadInstructions", new middlewares.DeadInstructionsMiddleware());
  35. Middleware.Add("ir.o.copies", new middlewares.CodeOtimizadorMiddleware(true));
  36. // Processadores do alvo mips
  37. Middleware.Add("mips.copy.dep", new targets.mips.MipsCopyDepsMiddleware());
  38. Middleware.Add("mips.register.alloc", new targets.mips.MipsRegisterAllocMiddleware());
  39. Middleware.Add("mips.o.L1", new targets.mips.MipsOtimizationMiddleware());
  40. Middleware.Add("mips.update.address", new targets.mips.MipsUpdateAddressMiddleware());
  41. // Middleware.Add("print.data", (c, cp) -> {
  42. //
  43. // System.out.println("@@@@@@DATA_LAYOUT:\n\n"
  44. // + c.Block().GlobalContext()
  45. // + c.Block().Context().toString());
  46. //
  47. // });
  48. // Middleware.On("IR", "close.block", "print.data");
  49. String middlelist = Utils.Join(new String[]{
  50. // Remove labels não referenciadas
  51. // "ir.clear.labels",
  52. // Remove copias
  53. "ir.o.copies",
  54. // Propagacao de constantes
  55. // + "ir.o.VariablesConstants,"
  56. // Remove instruções que não tem impacto algum na execusao
  57. // + "ir.o.DeadInstructions,"
  58. "ir.basic.blocks", // Extrai os blocos basicos de uma subrotina
  59. "ir.o.load_store", // Remove loads e stores desnecessarios
  60. // "ir.basic.blocks", // Extrai os blsocos basicos de uma subrotina
  61. }, ",");
  62. // Define os processadores padrão da IR
  63. Middleware.On("IR", "close.block", middlelist);
  64. /*Inicializando importes*/
  65. Imports._init(compiler);
  66. // Constantes._init();
  67. Interfaces._init();
  68. /*Inicializando funcoes*/
  69. Functions._init();
  70. /*Inicializando parametros de compilacao*/
  71. BuildParams._init();
  72. /*Inicializando Variables*/
  73. Variables._init();
  74. inited = true;
  75. }
  76. /**
  77. * Retorna o numero_da_linha + ':' + numero_da_coluna + '|' nome_arquivo
  78. *
  79. * @param n
  80. * @return
  81. */
  82. public static String getFormatedPosition(Node n) {
  83. return "[" + n.Get("line") + ":" + n.Get("col") + "|" + n.Get("file") + "\t]";
  84. }
  85. /**
  86. * Retorna numero de ocorrenciaS de um Padrão dentro de uma String
  87. *
  88. * @param pattern
  89. * @param input
  90. * @return
  91. */
  92. public static int countRegexOcorrences(String pattern, String input) {
  93. Matcher matcher = Pattern.compile(pattern).matcher(input);
  94. int count = 0;
  95. while (matcher.find()) {
  96. count++;
  97. }
  98. return count;
  99. }
  100. public static int countChatOcorrece(String input, char c) {
  101. int charCount = 0;
  102. for (int i = 0; i < input.length(); i++) {
  103. if (input.charAt(i) == c) {
  104. charCount++;
  105. }
  106. }
  107. return charCount;
  108. }
  109. /**
  110. * Retorna valor de um indice em um vetor unidirecional. <br>
  111. * var[valor] -> valor;
  112. *
  113. * @param var
  114. * @return
  115. */
  116. public static String getIndice(String var) {
  117. if (var.contains("[")) {
  118. return var.split("\\[")[1].replace("]", "");
  119. }
  120. return "";
  121. }
  122. public static String num2bin(int num, int SizeBits) {
  123. String binario = Integer.toBinaryString(num);
  124. String paded = Utils.Pad(SizeBits, binario);
  125. return Utils.cut(SizeBits, paded);
  126. }
  127. public static String num2bin(String num, int SizeBits) {
  128. int val = Integer.parseInt(num);
  129. return num2bin(val, SizeBits);
  130. }
  131. // public static String reg2bin5(String reg) throws Exception {
  132. // return reg2Bin(reg, 5);
  133. // }
  134. // public static String reg2Bin(String reg, int SizeBits) throws Exception {
  135. // if (reg.equals("")) {
  136. // return num2bin(0, SizeBits);
  137. // }
  138. //// System.out.print("Valor do reg:" + reg);
  139. // int val = Allocation.reg2Int(reg);
  140. //// System.out.println(":val:" + val);
  141. // return num2bin(val, SizeBits);
  142. // }
  143. public static String bin2Hex(String bin, int size) {
  144. // System.out.println("bin:[" + bin + "]");
  145. if (bin.equals("")) {
  146. return "falha";
  147. }
  148. return String.format("%" + size + "X", Long.parseLong(bin, 2));
  149. }
  150. public static String implode(char delimiter, ArrayList<String> elements) {
  151. String tmp = elements.toString();
  152. return tmp.substring(1, tmp.length() - 1).replace(", ", "" + delimiter);
  153. }
  154. public static boolean IsValue(String param) {
  155. if (param != null) {
  156. if (Utils.IsNumber(param)) {
  157. return true;
  158. } else if (param.toLowerCase().matches("(true|false)")) {
  159. return true;
  160. }
  161. }
  162. return false;
  163. }
  164. public static String Value(String v) {
  165. String vt = v.toLowerCase();
  166. if (vt.matches("(true|false)")) {
  167. return vt.equals("true") ? "1" : "0";
  168. } else {
  169. return v;
  170. }
  171. }
  172. public static String clearID(String id) {
  173. return id.replace("*", "").replace("&", "");
  174. }
  175. public static void Update() throws Exception {
  176. Types.Update();
  177. }
  178. public static void List() {
  179. BuildParams.List();
  180. Types.List();
  181. Interfaces.List();
  182. Variables.List();
  183. Functions.List();
  184. }
  185. }