Api.java 6.4 KB

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