Api.java 7.2 KB

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