IRTemplate.java 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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 = "{[T(3)]}"
  22. + "{' T< '[type]' >'} "
  23. + "{' --'[comment]}"
  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("load", base + "'load '{[TYPE('p1')]' '}{[p1]', '}{[dst]}" + end);
  34. put("store", base + "'store '{[TYPE('dst')]' '}{[dst]', '}{[p1]}" + end);
  35. put("jump", base + " 'goto ' {'<'[label]'>'}" + end);
  36. put("branch", base + " 'if '{[p1]} {' '[op]' '}{[p2]} ' goto ' {'<'[label]'>'}" + end);
  37. // put("call", base + " {[dst]' := '} 'call <' {[funcname]} '>, ' {[nump]}" + end);
  38. put("call", base + "'call' {' <'[funcname]'>'} {', '[nump]}" + end);
  39. put("return", base + " 'return '{[p1]}" + end);
  40. put("push_param", base + " 'push_param '{[p1]}" + end);
  41. put("push_return", base + " 'push_return '{[p1]}" + end);
  42. put("pop_param", base + " 'pop_param '{[p1]}" + end);
  43. put("pop_return", base + " 'pop_return '{[p1]}" + end);
  44. }
  45. };
  46. }
  47. public HashMap<String, FunctionInterface> Functions() throws Exception {
  48. return new HashMap<String, FunctionInterface>() {
  49. {
  50. put("TYPE", (ctx, args) -> {
  51. String attr = args.get(0) + ".type";
  52. if (!ctx.Has(attr)) {
  53. return "";
  54. }
  55. return "(" + ctx.Get(args.get(0) + ".type") + ")";
  56. });
  57. }
  58. };
  59. }
  60. }