MipsTemplate.java 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  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 targets.mips;
  7. import API.Api;
  8. import API.Utils;
  9. import common.RegistroBase;
  10. import java.util.ArrayList;
  11. import java.util.HashMap;
  12. import templates.FunctionInterface;
  13. import templates.Functions;
  14. /**
  15. *
  16. * @author EUGENIO CARVALHO
  17. */
  18. public class MipsTemplate implements templates.TemplateDescription {
  19. public HashMap<String, String> Formats() throws Exception {
  20. String base = "{[HEX(global.position,G.addressLen,' ')]': '}{[INSTDEC()]}{[T(1)]}{[inst]' '}",
  21. end = "{[T(1)]}{'.'[tac.position]'.'}{'--'[comment]}";
  22. return new HashMap<String, String>() {
  23. {
  24. put("S", base + end);
  25. put("label", "' '{[HEX(global.reference.position,8,0)]}{[INSTDEC()]}{' <'[label]'>:'");
  26. // Template para instrucoes do tipo J
  27. put("J", base + "{[rs]}{[LM(label,'hex')]}" + end);
  28. // Template para instrucoes do tipo R
  29. put("R0", base + "{[rd]','}{[rs]','}{[rt]}" + end);
  30. put("R1", base + "{[rs]','}{[rt]}" + end);
  31. put("R2", base + "{[rs]}" + end);
  32. put("R3", base + "{[rt]','}{[rs]}" + end);
  33. put("R4", base + "{[rd]','}{[rt]','}{[sa]} " + end);
  34. put("R5", base + "{[rd]}" + end);
  35. // Template para instrucoes do tipo I
  36. put("I0", base + "{[rt]','}{[rs]','}{[LM(label,'hex')]|[offset]|[rd]}" + end); //[rs:sp,rt:7,rd:sp]
  37. // SW,LW
  38. put("I1", base + "{[rt]','}{[OFFSET(offset,rs)]}" + end);
  39. // Templates avulsos
  40. put("PSEUDO", base);
  41. // Templates para instrucoes binarias
  42. put("bin.R", "{[codop]}{[RBIN(rs,5)]}{[RBIN(rt,5)]}{[RBIN(rd,5)]}{[BIN(sa,5)]}{[func]}");
  43. put("bin.I", "{[codop]}{[RBIN(rs,5)]}{[RBIN(rt,5)]}{[BIN(offset,16)]}");
  44. put("bin.J", "{[codop]}{[target]}");
  45. put("bin.S", "{[txt]}");
  46. }
  47. };
  48. }
  49. public HashMap<String, FunctionInterface> Functions() throws Exception {
  50. return new HashMap<String, FunctionInterface>() {
  51. {
  52. put("OFFSET", (ctx, args) -> args.get(0) + "(" + args.get(1) + ")");
  53. put("INSTHEX", (ctx, args) -> MipsTemplate.ConvertBin("hex", ctx, args));
  54. put("INSTDEC", (ctx, args) -> MipsTemplate.ConvertBin("dec", ctx, args));
  55. put("INSTBIN", (ctx, args) -> MipsTemplate.INSTBIN(ctx, args));
  56. put("RBIN", (ctx, args) -> {
  57. String reg = args.get(0);
  58. if (reg.equals("")) {
  59. reg = "0";
  60. }
  61. if (!Utils.IsNumber(reg)) {
  62. reg = Mips.Register(reg);
  63. }
  64. return Api.num2bin(reg, Integer.parseInt(args.get(1)));
  65. });
  66. put("LM", (ctx, args) -> {
  67. if (args.get(0).equals("")) {
  68. return "";
  69. }
  70. String label = args.get(0).trim();
  71. if (label.equals("")) {
  72. return label;
  73. }
  74. // System.out.println("RUN LM: " + args + ctx);
  75. String prefix = "G.";
  76. String format = (args.size() > 1) ? args.get(1) : "dec",
  77. // Corresponde ao nome do label que marca o inicio do bloco
  78. base = ctx.Get(prefix + "label." + label).split("\\+")[0],
  79. // Corresponde ao endereco do label base (decimal)
  80. baseAddress = ctx.Get(prefix + base),
  81. // Corresponde a posicao do label destino (decimal)
  82. laddress = ctx.Get(prefix + label);
  83. // System.out.printf("TemplateListener:{%s\n %s\n %s\n %s\n}\n", base, baseAddress, label, laddress);
  84. int value = Integer.parseInt(laddress);
  85. int fromStart = value - Integer.parseInt(baseAddress);
  86. switch (ctx.Get("type")) {
  87. case "I": // (bne...) Salta para posicao em relacao ao pc atual
  88. // System.out.println("TemplateListenerLabel::" + base + "|" + laddress + "|" + baseAddress + "|\n" + ctx);
  89. // System.out.println("I Branch:{{\n" + base
  90. // + "\n:" + Integer.parseInt(ctx.Get(base))
  91. // + "\n:" + label
  92. // + "\n:" + value
  93. // + "\n:" + FormatNum(value, format)
  94. // + "\n}}");
  95. // System.out.println("Tranalate>>>>>>>>> I:[" + address + "]" + ctx);
  96. // address -= Integer.parseInt(ctx.Get("block.position"));
  97. break;
  98. case "J": // (j | jal) Salta para o endereco de destino
  99. // System.out.println(">>>>" + label + ":" + format + ":" + value + ":" + FormatNum(value, format));
  100. // address = valueint;
  101. break;
  102. default:
  103. System.out.println(String.format("Label type '%s' not defined", ctx.Get("type")));
  104. }
  105. // address = address * this.AddressGap;
  106. // value = value * this.AddressGap;
  107. // System.out.println("============LaBEL:" + label + ":" + format + ":" + value + ":" + base);
  108. // System.out.println("============LaBEL:" + value + ":" + address);
  109. return laddress.trim().equals("")
  110. ? ""
  111. : (Utils.FormatNum(value, format)
  112. + " <" + base + "+0x"
  113. + Utils.FormatNum(fromStart, format)
  114. + ">");
  115. });
  116. }
  117. };
  118. }
  119. // Shared functions
  120. protected static String ConvertBin(String inst, RegistroBase ctx, ArrayList<String> args) {
  121. try {
  122. Integer size;
  123. String bin = MipsTemplate.INSTBIN(ctx, args);
  124. if (Utils.Empty(bin)) {
  125. return "";
  126. }
  127. Long bin2long = Long.parseLong(bin, 2);
  128. // System.out.println("CONVERTbin:" + bin + "\nbin2long:" + bin2long);
  129. switch (inst) {
  130. case "hex":
  131. size = 8;
  132. bin = Long.toString(bin2long, 16);
  133. break;
  134. case "dec":
  135. size = 10;
  136. bin = "" + bin2long;
  137. break;
  138. default:
  139. throw new Exception(String.format("Conversão para o tipo '%s' não definida.", inst));
  140. }
  141. String result = Utils.Pad(bin, "0", "", size);
  142. ctx.Set("inst." + inst, result);
  143. return result;
  144. } catch (Exception e) {
  145. if (ctx.Get("inst").equals("bgtz")) {
  146. System.out.println("hex-Error:" + e.getMessage());
  147. }
  148. System.out.println(e.getMessage());
  149. e.printStackTrace();
  150. return "error!";
  151. }
  152. }
  153. protected static String INSTBIN(RegistroBase ctx, ArrayList<String> args) throws Exception {
  154. String type = ctx.Get("type");
  155. if (type.equals("label")) {
  156. return "";
  157. }
  158. long shift;
  159. //
  160. switch (type) {
  161. case "S": // (j | jal) Salta para o endereco de destino
  162. // System.out.println("instbin:" + ctx);
  163. break;
  164. case "J": // (j | jal) Salta para o endereco de destino
  165. // System.out.println("JUMP:" + ctx.Get(ctx.Get("label")) + ":"
  166. // + (Long.parseLong(ctx.Get(ctx.Get("label")), 10) >> 2));
  167. shift = (Long.parseLong(ctx.Get("G." + ctx.Get("label")), 10) >> 2);
  168. ctx.Set("target", Functions.BIN("" + shift, 26));
  169. break;
  170. case "I": // (bne...) Salta para posicao em relacao ao pc atual + 4
  171. // System.out.println("BIN iiiiii" + ctx);
  172. // shift = (Long.parseLong(ctx.Get(ctx.Get("label")), 10) >> 2);
  173. // ctx.Set("target", BIN("" + shift, 26));
  174. // if (ctx.Get("offset").equals("") && !ctx.Get("label").equals("")) {
  175. //
  176. // int target = Integer.parseInt(ctx.Get(ctx.Get("label")));
  177. // int position = Integer.parseInt(ctx.Get("global.position")) + 1;
  178. //
  179. //// System.out.println("INSTBIN:->" + FormatNum(target - position, "hex"));
  180. // ctx.Set("offset", (target - position) + "");
  181. //
  182. // }
  183. // System.out.println("INSTBIN[" + type + ":" + bin + "]:" + ctx);
  184. break;
  185. case "R": // (bne...) Salta para posicao em relacao ao pc atual
  186. // System.out.println("INSTBIN[" + type + "]:" + ctx);
  187. break;
  188. default:
  189. }
  190. String bin = templates.Template.FormatRender("bin." + type).Render(ctx);
  191. ctx.Set("inst.bin", bin);
  192. // System.out.println("bin." + type + "::" + bin);
  193. return bin;
  194. }
  195. }
  196. // @Override
  197. // public String Render(RegistroBase ctx) {
  198. //
  199. // if (tpls.isEmpty()) {
  200. // tpls.put("bin.R", New("{[codop]}{[RBIN(rs,5)]}{[RBIN(rt,5)]}{[RBIN(rd,5)]}{[BIN(sa,5)]}{[func]}", ctx));
  201. // tpls.put("bin.I", New("{[codop]}{[RBIN(rs,5)]}{[RBIN(rt,5)]}{[BIN(offset,16)]}", ctx));
  202. // tpls.put("bin.J", New("{[codop]}{[target]}", ctx));
  203. // tpls.put("bin.S", New("{[txt]}", ctx));
  204. // }
  205. //
  206. // TemplateFunctions listener = new TemplateFunctions(ctx, ctx);
  207. //// listener.AddressGap = 4;
  208. // listener.templates = tpls;
  209. //
  210. // walker.walk(listener, tree);
  211. //
  212. // return listener.toString();
  213. // }
  214. // @Override
  215. // public TPLInterface New(String tpl, RegistroBase ctx) {
  216. // return new Template(tpl, ctx);
  217. // }