IRTemplate.java 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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 IntermediaryCode;
  7. import java.util.HashMap;
  8. import templates.FunctionInterface;
  9. /**
  10. *
  11. * @author EUGENIO CARVALHO
  12. */
  13. public class IRTemplate implements templates.TemplateDescription {
  14. public HashMap<String, String> Formats() throws Exception {
  15. return new HashMap<String, String>() {
  16. {
  17. String base = "{[PAD(global.position,G.addressLen,' ')]':'}"
  18. + "{[T(1)]}"
  19. + "{[PAD(block.position,G.addressLen,' ')]':'}"
  20. + "{[T(2)]}";
  21. String end = " {' --'[comment]}"
  22. + "{[T(2)]}"
  23. + "{' T< '[type]' >'} "
  24. + "{[T(2)]}"
  25. + "{[basicBlock]}";
  26. put("label", "{[PAD(global.position,G.addressLen,' ')]':'}{[T(2)]}{'<'[label]'>'}':'");
  27. put("assign", base + "{[dst]}' := '{[TYPE('p1')]' '}{[p1]' '}{[op]' '}{[TYPE('p2')]' '}{[p2]}" + end);
  28. put("pointer_assign", base + "{[dst.pointer]}{[dst]' := '}{[op]' '}{[TYPE('p1')]' '}{[p1]}" + end);
  29. put("unary", base + " {[dst]} ' := '{[op]' '}{[TYPE('p1')]' '}{[p1]}" + end);
  30. put("copy", base + " {[dst]} ' := '{[TYPE('p1')]' '}{[p1]}" + end);
  31. put("memory", base + " {[type]' '}{[TYPE('p1')]' '}{[p1]}" + end);
  32. put("jump", base + " 'goto ' {'<'[label]'>'}" + end);
  33. put("branch", base + " 'if '{[TYPE('p1')]' '}{[p1]} {' '[op]' '}{[TYPE('p2')]' '}{[p2]} ' goto ' {'<'[label]'>'}" + end);
  34. // put("call", base + " {[dst]' := '} 'call <' {[funcname]} '>, ' {[nump]}" + end);
  35. put("call", base + "'call <' {[funcname]} '>, ' {[nump]}" + end);
  36. put("return", base + " 'return '{[TYPE('p1')]' '}{[p1]}" + end);
  37. put("push_param", base + " 'push_param '{[TYPE('p1')]' '}{[p1]}" + end);
  38. put("push_return", base + " 'push_return '{[TYPE('p1')]' '}{[p1]}" + end);
  39. put("pop_param", base + " 'pop_param '{[TYPE('p1')]' '}{[p1]}" + end);
  40. put("pop_return", base + " 'pop_return '{[TYPE('p1')]' '}{[p1]}" + end);
  41. }
  42. };
  43. }
  44. public HashMap<String, String> aliases = new HashMap<String, String>() {
  45. {
  46. put("char", "i8");
  47. put("bool", "i8");
  48. put("byte", "i8");
  49. put("int8", "i8");
  50. put("int16", "i16");
  51. put("int32", "i32");
  52. put("int64", "i64");
  53. put("float32", "f32");
  54. put("float64", "f64");
  55. }
  56. };
  57. public HashMap<String, FunctionInterface> Functions() throws Exception {
  58. return new HashMap<String, FunctionInterface>() {
  59. {
  60. put("TYPE", (ctx, args) -> {
  61. String attr = args.get(0) + ".type";
  62. if (!ctx.Has(attr)) {
  63. return "";
  64. }
  65. String type = ctx.Get(attr);
  66. if (!aliases.containsKey(type)) {
  67. throw new Exception(String.format("Nenhum alias foi definido para o tipo '%s'", type));
  68. }
  69. return "(" + aliases.get(type) + ")";
  70. });
  71. }
  72. };
  73. }
  74. }