Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.ArrayList;
- import java.util.List;
- /*
- * To change this license header, choose License Headers in Project Properties.
- * To change this template file, choose Tools | Templates
- * and open the template in the editor.
- */
- /**
- *
- * @author tymoteuszilczyszyn
- */
- public class T1CodeGen implements Codegen {
- public static String concat(String s1, String s2) {
- return s1 + "\n" + s2;
- }
- @Override
- public String codegen(Program p) throws CodegenException {
- String output = "";
- output = "jal " + p.decls.get(0).id + "_entry";
- output = concat(output,"li $v0 10");
- output = concat(output,"syscall");
- for (int i=0;i<p.decls.size();i++) {
- output = concat(output,this.genDecl(p.decls.get(i)));
- }
- return output;
- }
- public String genExp (Exp e) {
- String output = "";
- if (e instanceof IntLiteral) {
- output = "li $a0 " + Integer.toString(((IntLiteral) e).n);
- } else if (e instanceof Variable) {
- } else if (e instanceof If) {
- } else if (e instanceof Binexp) {
- } else if (e instanceof Invoke) {
- }
- return output;
- }
- public String genDecl (Declaration d) {
- String output = "";
- int sizeAR = (0 + d.numOfArgs) * 4;
- output = d.id + "_entry:";
- output = concat(output,"move $fp $sp");
- output = concat(output,"sw $ra 0($sp)");
- output = concat(output,"addiu $sp $sp -4");
- output = concat(output,this.genExp(d.body));
- output = concat(output,"lw $ra 4($sp)");
- output = concat(output,"addiu $sp $sp " + Integer.toString(sizeAR));
- output = concat(output,"lw $fp 0($sp)");
- output = concat(output,"jr $ra");
- return output;
- }
- public static void main (String args[]) throws CodegenException {
- T1CodeGen t1 = new T1CodeGen();
- IntLiteral s = new IntLiteral(5);
- Declaration d = new Declaration("tymek1",2,s);
- List<Declaration> l = new ArrayList<Declaration>();
- l.add(d);
- Program p = new Program(l);
- System.out.println(t1.codegen(p));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement