Api.java 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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. return var.split("\\[")[1].replace("]", "");
  118. }
  119. public static String num2bin(int num, int SizeBits) {
  120. String binario = Integer.toBinaryString(num);
  121. String paded = Utils.Pad(SizeBits, binario);
  122. return Utils.cut(SizeBits, paded);
  123. }
  124. public static String num2bin(String num, int SizeBits) {
  125. int val = Integer.parseInt(num);
  126. return num2bin(val, SizeBits);
  127. }
  128. // public static String reg2bin5(String reg) throws Exception {
  129. // return reg2Bin(reg, 5);
  130. // }
  131. // public static String reg2Bin(String reg, int SizeBits) throws Exception {
  132. // if (reg.equals("")) {
  133. // return num2bin(0, SizeBits);
  134. // }
  135. //// System.out.print("Valor do reg:" + reg);
  136. // int val = Allocation.reg2Int(reg);
  137. //// System.out.println(":val:" + val);
  138. // return num2bin(val, SizeBits);
  139. // }
  140. public static String bin2Hex(String bin, int size) {
  141. // System.out.println("bin:[" + bin + "]");
  142. if (bin.equals("")) {
  143. return "falha";
  144. }
  145. return String.format("%" + size + "X", Long.parseLong(bin, 2));
  146. }
  147. public static String implode(char delimiter, ArrayList<String> elements) {
  148. String tmp = elements.toString();
  149. return tmp.substring(1, tmp.length() - 1).replace(", ", "" + delimiter);
  150. }
  151. public static boolean IsValue(String param) {
  152. if (Utils.IsNumber(param)) {
  153. return true;
  154. } else if (param.toLowerCase().matches("(true|false)")) {
  155. return true;
  156. }
  157. return false;
  158. }
  159. public static String Value(String v) {
  160. String vt = v.toLowerCase();
  161. if (vt.matches("(true|false)")) {
  162. return vt.equals("true") ? "1" : "0";
  163. } else {
  164. return v;
  165. }
  166. }
  167. public static String clearID(String id) {
  168. return id.replace("*", "").replace("&", "");
  169. }
  170. public static void Update() throws Exception {
  171. Types.Update();
  172. }
  173. public static void List() {
  174. BuildParams.List();
  175. Types.List();
  176. Interfaces.List();
  177. Variables.List();
  178. Functions.List();
  179. }
  180. }