Advertisement
Guest User

Untitled

a guest
Dec 18th, 2018
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.40 KB | None | 0 0
  1. import java.util.ArrayList;
  2. import java.util.List;
  3.  
  4.  
  5.  
  6. /*
  7.  * To change this license header, choose License Headers in Project Properties.
  8.  * To change this template file, choose Tools | Templates
  9.  * and open the template in the editor.
  10.  */
  11.  
  12. /**
  13.  *
  14.  * @author tymoteuszilczyszyn
  15.  */
  16. public class T1CodeGen implements Codegen {
  17.    
  18.    
  19.     public static String concat(String s1, String s2) {
  20.         return s1 + "\n" + s2;
  21.     }
  22.  
  23.     @Override
  24.     public String codegen(Program p) throws CodegenException {
  25.        
  26.         String output = "";
  27.         output = "jal " + p.decls.get(0).id + "_entry";
  28.         output = concat(output,"li $v0 10");
  29.         output = concat(output,"syscall");
  30.        
  31.         for (int i=0;i<p.decls.size();i++) {
  32.             output = concat(output,this.genDecl(p.decls.get(i)));
  33.         }
  34.        
  35.        
  36.         return output;
  37.     }
  38.    
  39.     public String genExp (Exp e) {
  40.         String output = "";
  41.        
  42.         if (e instanceof IntLiteral) {
  43.             output = "li $a0 " + Integer.toString(((IntLiteral) e).n);
  44.        
  45.         } else if (e instanceof Variable) {
  46.            
  47.         } else if (e instanceof If) {
  48.            
  49.         } else if (e instanceof Binexp) {
  50.            
  51.         } else if (e instanceof Invoke) {
  52.            
  53.         }
  54.  
  55.        
  56.         return output;
  57.     }
  58.    
  59.     public String genDecl (Declaration d) {
  60.         String output = "";
  61.         int sizeAR = (0 + d.numOfArgs) * 4;
  62.        
  63.         output = d.id + "_entry:";
  64.         output = concat(output,"move $fp $sp");
  65.         output = concat(output,"sw $ra 0($sp)");
  66.         output = concat(output,"addiu $sp $sp -4");
  67.         output = concat(output,this.genExp(d.body));
  68.         output = concat(output,"lw $ra 4($sp)");
  69.         output = concat(output,"addiu $sp $sp " + Integer.toString(sizeAR));
  70.         output = concat(output,"lw $fp 0($sp)");
  71.         output = concat(output,"jr $ra");
  72.        
  73.         return output;
  74.     }
  75.    
  76.     public static void main (String args[]) throws CodegenException {
  77.        
  78.         T1CodeGen t1 = new T1CodeGen();
  79.        
  80.         IntLiteral s = new IntLiteral(5);
  81.         Declaration d = new Declaration("tymek1",2,s);
  82.         List<Declaration> l = new ArrayList<Declaration>();
  83.         l.add(d);
  84.        
  85.         Program p = new Program(l);
  86.        
  87.         System.out.println(t1.codegen(p));
  88.     }
  89.    
  90. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement