MipsTemplateDescription.java 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  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 MipsTemplateDescription 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(2)]}{'.'[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) -> MipsTemplateDescription.ConvertBin("hex", ctx, args));
  54. put("INSTDEC", (ctx, args) -> MipsTemplateDescription.ConvertBin("dec", ctx, args));
  55. put("INSTBIN", (ctx, args) -> MipsTemplateDescription.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 = Descriprion.Register(reg);
  63. }
  64. return Api.num2bin(reg, Integer.parseInt(args.get(1)));
  65. });
  66. put("LM", (ctx, args) -> {
  67. String label = args.get(0).trim();
  68. if (label.equals("")) {
  69. return label;
  70. }
  71. // System.out.println("RUN LM: " + args + ctx);
  72. String prefix = "G.";
  73. String format = (args.size() > 1) ? args.get(1) : "dec",
  74. // Corresponde ao nome do label que marca o inicio do bloco
  75. base = ctx.Get(prefix + "label." + label).split("\\+")[0],
  76. // Corresponde ao endereco do label base (decimal)
  77. baseAddress = ctx.Get(prefix + base),
  78. // Corresponde a posicao do label destino (decimal)
  79. laddress = ctx.Get(prefix + label);
  80. // System.out.printf("TemplateListener:{%s\n %s\n %s\n %s\n}\n", base, baseAddress, label, laddress);
  81. Long value = Long.parseLong(laddress);
  82. Long fromStart = value - Long.parseLong(baseAddress);
  83. switch (ctx.Get("type")) {
  84. case "I": // (bne...) Salta para posicao em relacao ao pc atual
  85. case "J": // (j | jal) Salta para o endereco de destino
  86. break;
  87. default:
  88. System.out.println(String.format("Label type '%s' not defined", ctx.Get("type")));
  89. }
  90. return laddress.trim().equals("")
  91. ? ""
  92. : (Utils.FormatNum(value, format)
  93. + " <" + base + "+0x"
  94. + Utils.FormatNum(fromStart, format)
  95. + ">");
  96. });
  97. }
  98. };
  99. }
  100. // Shared functions
  101. protected static String ConvertBin(String inst, RegistroBase ctx, ArrayList<String> args) {
  102. try {
  103. Integer size;
  104. String bin = MipsTemplateDescription.INSTBIN(ctx, args);
  105. if (Utils.Empty(bin)) {
  106. return "";
  107. }
  108. Long bin2long = Long.parseLong(bin, 2);
  109. // System.out.println("CONVERTbin:" + bin + "\nbin2long:" + bin2long);
  110. switch (inst) {
  111. case "hex":
  112. size = 8;
  113. bin = Long.toString(bin2long, 16);
  114. break;
  115. case "dec":
  116. size = 10;
  117. bin = "" + bin2long;
  118. break;
  119. default:
  120. throw new Exception(String.format("Conversão para o tipo '%s' não definida.", inst));
  121. }
  122. String result = Utils.Pad(bin, "0", "", size);
  123. ctx.Set("inst." + inst, result);
  124. return result;
  125. } catch (Exception e) {
  126. if (ctx.Get("inst").equals("bgtz")) {
  127. System.out.println("hex-Error:" + e.getMessage());
  128. }
  129. System.out.println(e.getMessage());
  130. e.printStackTrace();
  131. return "error!";
  132. }
  133. }
  134. protected static String INSTBIN(RegistroBase ctx, ArrayList<String> args) throws Exception {
  135. String type = ctx.Get("type");
  136. if (type.equals("label")) {
  137. return "";
  138. }
  139. long shift;
  140. //
  141. switch (type) {
  142. case "S": // (j | jal) Salta para o endereco de destino
  143. // System.out.println("instbin:" + ctx);
  144. break;
  145. case "J": // (j | jal) Salta para o endereco de destino
  146. // System.out.println("JUMP:" + ctx.Get(ctx.Get("label")) + ":"
  147. // + (Long.parseLong(ctx.Get(ctx.Get("label")), 10) >> 2));
  148. shift = (Long.parseLong(ctx.Get("G." + ctx.Get("label")), 10) >> 2);
  149. ctx.Set("target", Functions.BIN("" + shift, 26));
  150. break;
  151. case "I": // (bne...) Salta para posicao em relacao ao pc atual + 4
  152. // System.out.println("BIN iiiiii" + ctx);
  153. // shift = (Long.parseLong(ctx.Get(ctx.Get("label")), 10) >> 2);
  154. // ctx.Set("target", BIN("" + shift, 26));
  155. // if (ctx.Get("offset").equals("") && !ctx.Get("label").equals("")) {
  156. //
  157. // int target = Integer.parseInt(ctx.Get(ctx.Get("label")));
  158. // int position = Integer.parseInt(ctx.Get("global.position")) + 1;
  159. //
  160. //// System.out.println("INSTBIN:->" + FormatNum(target - position, "hex"));
  161. // ctx.Set("offset", (target - position) + "");
  162. //
  163. // }
  164. // System.out.println("INSTBIN[" + type + ":" + bin + "]:" + ctx);
  165. break;
  166. case "R": // (bne...) Salta para posicao em relacao ao pc atual
  167. // System.out.println("INSTBIN[" + type + "]:" + ctx);
  168. break;
  169. default:
  170. }
  171. String bin = templates.Template.FormatRender("bin." + type).Render(ctx);
  172. ctx.Set("inst.bin", bin);
  173. // System.out.println("bin." + type + "::" + bin);
  174. return bin;
  175. }
  176. }
  177. // @Override
  178. // public String Render(RegistroBase ctx) {
  179. //
  180. // if (tpls.isEmpty()) {
  181. // tpls.put("bin.R", New("{[codop]}{[RBIN(rs,5)]}{[RBIN(rt,5)]}{[RBIN(rd,5)]}{[BIN(sa,5)]}{[func]}", ctx));
  182. // tpls.put("bin.I", New("{[codop]}{[RBIN(rs,5)]}{[RBIN(rt,5)]}{[BIN(offset,16)]}", ctx));
  183. // tpls.put("bin.J", New("{[codop]}{[target]}", ctx));
  184. // tpls.put("bin.S", New("{[txt]}", ctx));
  185. // }
  186. //
  187. // TemplateFunctions listener = new TemplateFunctions(ctx, ctx);
  188. //// listener.AddressGap = 4;
  189. // listener.templates = tpls;
  190. //
  191. // walker.walk(listener, tree);
  192. //
  193. // return listener.toString();
  194. // }
  195. // @Override
  196. // public TPLInterface New(String tpl, RegistroBase ctx) {
  197. // return new Template(tpl, ctx);
  198. // }