Advertisement
Guest User

Untitled

a guest
May 22nd, 2018
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.99 KB | None | 0 0
  1. package AST.Visitor;
  2.  
  3. import java.util.List;
  4. import java.util.Map;
  5.  
  6. import AST.*;
  7.  
  8. public class CodeGenVisitor implements Visitor {
  9. public int memory;
  10. public Map<String, ClassTable> info;
  11. public String currentClass;
  12. public String currentMethod;
  13. public String[] args = {"%rsi", "%rdx", "%rcx", "%r8", "%r9"};
  14.  
  15.  
  16. public CodeGenVisitor(){
  17.  
  18. }
  19.  
  20. public CodeGenVisitor(Map<String, ClassTable> info){
  21. this.info = info;
  22. this.memory = 0;
  23. }
  24.  
  25.  
  26. // MainClass m;
  27. // ClassDeclList cl;
  28. public void visit(Program n) {
  29. gen("\t.text");
  30. gen("\t.globl asm_main");
  31. n.m.accept(this);
  32.  
  33. for(int i = 0; i < n.cl.size(); i++){
  34. n.cl.get(i).accept(this);
  35. }
  36. }
  37.  
  38. // Identifier i1,i2;
  39. // Statement s;
  40. public void visit(MainClass n) {
  41. currentClass = n.i1.s;
  42. currentMethod = null;
  43.  
  44. genLabel("asm_main");
  45.  
  46. //Prologue
  47. //Allocate stack frame
  48. push("%rbp");
  49. genbin("movq", "%rsp","%rbp");
  50.  
  51. n.s.accept(this);
  52.  
  53. //Epilogue
  54. genbin("movq","%rbp", "%rsp");
  55. pop("%rbp");
  56. gen("\tret");
  57.  
  58. System.out.println();
  59. gen("\t.data");
  60. gen(n.i1.s + "$$:\t.quad\t0");
  61.  
  62. }
  63.  
  64. // Identifier i;
  65. // VarDeclList vl;
  66. // MethodDeclList ml;
  67. public void visit(ClassDeclSimple n) {
  68. currentClass = n.i.s;
  69. currentMethod = null;
  70.  
  71. for(int i = 0; i < n.ml.size(); i++){
  72. currentMethod = n.ml.get(i).i.s;
  73. n.ml.get(i).accept(this);
  74. }
  75.  
  76. //Constructor
  77. genLabel(n.i.s + "$" + n.i.s);
  78.  
  79. push("%rbp");
  80. genbin("movq", "%rsp","%rbp");
  81.  
  82. push("%rdi");
  83.  
  84. genbin("movq", "$" + (8 + (info.get(n.i.s).fields.size() * 8)), "%rdi");
  85. genbin("call", "mjcalloc");
  86.  
  87. pop("%rdi");
  88. genbin("leaq", n.i.s + "$$", "%rdx");
  89. genbin("movq", "%rdx", "0(%rax)");
  90.  
  91. for(int i = 8; i < (8 + info.get(n.i.s).fields.size() * 8); i += 8){
  92. genbin("movq", "$0", i + "(%rax)");
  93. }
  94.  
  95. genbin("movq", "%rbp", "%rsp");
  96. pop("%rbp");
  97. gen("\tret");
  98. gen("");
  99.  
  100. //Vtable
  101.  
  102. gen("\t.data");
  103. gen(n.i.s + "$$:\t.quad\t0");
  104. gen( "\t\t.quad\t" + currentClass + "$" + currentClass);
  105. for(int i = 0; i < n.ml.size(); i++){
  106. gen( "\t\t.quad\t" + currentClass + "$" + n.ml.get(i).i.s);
  107. }
  108. }
  109.  
  110. // Identifier i;
  111. // Identifier j;
  112. // VarDeclList vl;
  113. // MethodDeclList ml;
  114. public void visit(ClassDeclExtends n) {
  115. }
  116.  
  117. // Type t;
  118. // Identifier i;
  119. public void visit(VarDecl n) {
  120. }
  121.  
  122. // Type t;
  123. // Identifier i;
  124. // FormalList fl;
  125. // VarDeclList vl;
  126. // StatementList sl;
  127. // Exp e;
  128. public void visit(MethodDecl n) {
  129. currentMethod = n.i.s;
  130.  
  131. genLabel(currentClass + "$" + n.i.s);
  132.  
  133. push("%rbp");
  134. genbin("movq", "%rsp","%rbp");
  135.  
  136. if(n.vl.size() != 0){
  137. genbin("subq", 8*n.vl.size() + "", "%rsp");
  138. }
  139.  
  140. for (int j = 0; j < n.fl.size(); j++) {
  141. push(args[j]);
  142. }
  143.  
  144. for(int i = 0; i < n.sl.size(); i++){
  145. n.sl.get(i).accept(this);
  146. }
  147.  
  148. n.e.accept(this);
  149.  
  150. for (int j = n.fl.size() - 1; j >= 0 ; j--) {
  151. pop(args[j]);
  152. }
  153.  
  154.  
  155. genbin("movq","%rbp", "%rsp");
  156. pop("%rbp");
  157. gen("\tret");
  158. }
  159.  
  160. // Type t;
  161. // Identifier i;
  162. public void visit(Formal n) {
  163. }
  164.  
  165. public void visit(IntArrayType n) {
  166. }
  167.  
  168. public void visit(BooleanType n) {
  169. }
  170.  
  171. public void visit(IntegerType n) {
  172. }
  173.  
  174. // String s;
  175. public void visit(IdentifierType n) {
  176.  
  177. }
  178.  
  179. // StatementList sl;
  180. public void visit(Block n) {
  181. for(int i = 0; i < n.sl.size(); i++){
  182. n.sl.get(i).accept(this);
  183. }
  184. }
  185.  
  186. // Exp e;
  187. // StatementList s1,s2;
  188. public void visit(If n) {
  189.  
  190. }
  191.  
  192. // Exp e;
  193. // StatementList sl;
  194. public void visit(While n) {
  195.  
  196. }
  197.  
  198. // Exp e;
  199. public void visit(Print n) {
  200. n.e.accept(this);
  201. genbin("movq", "%rax", "%rdi");
  202. genbin("call", "put");
  203. }
  204.  
  205. // Identifier i;
  206. // Exp e;
  207. public void visit(Assign n) {
  208.  
  209. }
  210.  
  211. // Identifier i;
  212. // Exp e1,e2;
  213. public void visit(ArrayAssign n) {
  214.  
  215. }
  216.  
  217. // Exp e1,e2;
  218. public void visit(And n) {
  219.  
  220. }
  221.  
  222. // Exp e1,e2;
  223. public void visit(LessThan n) {
  224.  
  225. }
  226.  
  227. // Exp e1,e2;
  228. public void visit(Plus n) {
  229. n.e1.accept(this);
  230. push("%rax");
  231. n.e2.accept(this);
  232. pop("%rdx");
  233. genbin("addq", "%rdx", "%rax");
  234. }
  235.  
  236. // Exp e1,e2;
  237. public void visit(Minus n) {
  238. n.e2.accept(this);
  239. push("%rax");
  240. n.e1.accept(this);
  241. pop("%rdx");
  242.  
  243. genbin("subq", "%rdx", "%rax");
  244.  
  245. }
  246.  
  247. // Exp e1,e2;
  248. public void visit(Times n) {
  249. n.e1.accept(this);
  250. push("%rax");
  251. n.e2.accept(this);
  252. pop("%rdx");
  253. genbin("imulq", "%rdx", "%rax");
  254. }
  255.  
  256. // Exp e1,e2;
  257. public void visit(ArrayLookup n) {
  258.  
  259. }
  260.  
  261. // Exp e;
  262. public void visit(ArrayLength n) {
  263.  
  264. }
  265.  
  266. // Exp e;
  267. // Identifier i;
  268. // ExpList el;
  269. public void visit(Call n) {
  270. n.e.accept(this);
  271. if(!(n.e instanceof This)) {
  272. genbin("movq", "%rax", "%rdi");
  273. }
  274.  
  275.  
  276. genbin("movq", "%rdi", "%rbx");
  277. for (int i = 0; i < n.el.size(); i++) {
  278. n.el.get(i).accept(this);
  279.  
  280. if (n.el.get(i) instanceof This) {
  281. genbin("movq", "%rdi", args[i]);
  282. } else {
  283. genbin("movq", "%rax", args[i]);
  284. }
  285.  
  286. }
  287.  
  288. genbin("call", n.e.expType + "$" + n.i.s);
  289. }
  290.  
  291. // int i;
  292. public void visit(IntegerLiteral n) {
  293. if(n.i == 0){
  294. genbin("xorq", "%rax", "%rax");
  295. }
  296. else{
  297. genbin("movq", "$" + n.i, "%rax");
  298. }
  299. }
  300.  
  301. public void visit(True n) {
  302.  
  303. }
  304.  
  305. public void visit(False n) {
  306.  
  307. }
  308.  
  309. // String s;
  310. public void visit(IdentifierExp n) {
  311. List<ParameterTuple> methodArgs = info.get(currentClass).methods.get(currentMethod).argsOrder;
  312. for (int i = 0; i < methodArgs.size(); i++) {
  313. if (n.s.equals(methodArgs.get(i).name)) {
  314. genbin("movq", "-" + ((i + 1) * 8) + "(%rbp)", "%rax");
  315. return;
  316. } else {
  317. //save for later when we include field variables/local declarations as well
  318.  
  319. }
  320. }
  321. }
  322.  
  323. public void visit(This n) {
  324. }
  325.  
  326. // Exp e;
  327. public void visit(NewArray n) {
  328.  
  329. }
  330.  
  331. // Identifier i;
  332. public void visit(NewObject n) {
  333. genbin("call", n.i.s + "$" + n.i.s);
  334. // genbin("movq", "$" + (info.get(n.i.s).fields.size() * 8), "%rax");
  335. // genbin("call", "mjcalloc");
  336. // genbin("leaq", n.i.s + "$$", "%rdx");
  337. // genbin("movq", "%rdx", "0(%rax)");
  338. // push("%rax");
  339. // genbin("call", n.i.s + "$" + n.i.s);
  340. // pop("%rax");
  341. }
  342.  
  343. // Exp e;
  344. public void visit(Not n) {
  345.  
  346. }
  347.  
  348. // String s;
  349. public void visit(Identifier n) {
  350.  
  351. }
  352.  
  353. // Exp e;
  354. public void visit(Display n) {
  355. }
  356.  
  357.  
  358. public void gen(String s){
  359. System.out.println(s);
  360. }
  361.  
  362. public void genbin(String op, String src, String dst){
  363. gen("\t" + op + "\t" + src + "," + dst);
  364. }
  365.  
  366. public void genbin(String op, String r){
  367. gen("\t" + op + "\t" + r);
  368. }
  369.  
  370. public void push(String s){
  371. memory += 8;
  372. genbin("pushq", s);
  373. }
  374.  
  375. public void pop(String s){
  376. memory += 8;
  377. genbin("popq", s);
  378. }
  379.  
  380. public void genLabel(String L){
  381. gen(L + ":");
  382. }
  383. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement