IRTemplate.java 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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", "{[T(2)]}{'<'[label]'>'}':'");
  27. put("alloc", "{[T(3)]} 'alloc ' {[TYPE('p1')]', '} {[numElements]' '} {[p1]}" + end);
  28. put("assign", base + "{[dst]}' := '{[p1]' '}{[op]' '}{[p2]}" + end);
  29. put("pointer_assign", base + "{[dst.pointer]}{[dst]' := '}{[op]' '}{[p1]}" + end);
  30. put("unary", base + " {[dst]} ' := '{[op]' '}{[p1]}" + end);
  31. // put("copy", base + "{'('[dst.type]')'} {[dst]} ' := '{[p1]}" + end);
  32. put("copy", base + " {[dst]} ' := '{[p1]}" + end);
  33. put("memory", base + " {[type]' '}{[TYPE('p1')]' '}{[p1]}" + end);
  34. put("jump", base + " 'goto ' {'<'[label]'>'}" + end);
  35. put("branch", base + " 'if '{[p1]} {' '[op]' '}{[p2]} ' goto ' {'<'[label]'>'}" + end);
  36. // put("call", base + " {[dst]' := '} 'call <' {[funcname]} '>, ' {[nump]}" + end);
  37. put("call", base + "'call <' {[funcname]} '>, ' {[nump]}" + end);
  38. put("return", base + " 'return '{[p1]}" + end);
  39. put("push_param", base + " 'push_param '{[p1]}" + end);
  40. put("push_return", base + " 'push_return '{[p1]}" + end);
  41. put("pop_param", base + " 'pop_param '{[p1]}" + end);
  42. put("pop_return", base + " 'pop_return '{[p1]}" + end);
  43. }
  44. };
  45. }
  46. public HashMap<String, FunctionInterface> Functions() throws Exception {
  47. return new HashMap<String, FunctionInterface>() {
  48. {
  49. put("TYPE", (ctx, args) -> {
  50. String attr = args.get(0) + ".type";
  51. if (!ctx.Has(attr)) {
  52. return "";
  53. }
  54. return "(" + ctx.Get(args.get(0) + ".type") + ")";
  55. });
  56. }
  57. };
  58. }
  59. }