⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 algebraicsimplification.java

📁 用Java实现的编译器。把源代码编译成SPARC汇编程序
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
/**
*This class is used for expressions using the AND operation.
*This class extends the Exp abstract class
**/
package CatDecaf.Optimizer;
import CatDecaf.IR.*;
import CatDecaf.Utilities.*;
import java6035.tools.ASM.*;
import java.io.*;
import java.util.*;
import CatDecaf.SymTable.*;
import parser.*;

public class AlgebraicSimplification implements Visitor{
public static boolean newOptDone;
boolean searchFunction;
boolean functionPresent;


	public void visit(Ir n){        } //abstract class
	public void visit(IrProg n){
		newOptDone = false;
		searchFunction = false;
		functionPresent = false;
		if(n.fdDeclList_!=null)
			n.fdDeclList_.accept(this);

		if(n.mdDeclList_ != null)
			n.mdDeclList_.accept(this);  //this should never be empty
	}
	public void visit(FdDeclList n){ //not printing List classes
		while(n!= null)
		{
			n.fdDecl_.accept(this);
			n = n.fdDeclList_;
		}
	}
	public void visit(FdDecl n){
		if(n.glbVarList_==null) System.out.println("ERROR!!!!!!!!!!!!!!!");
		n.typ_.accept(this);
		n.glbVarList_.accept(this);
	}
	public void visit(GlbVar n){ } //abstract class
	public void visit(GlbVarArray n){
		n.identifier_.accept(this);
		n.ltrInt_.accept(this);
	}
	public void visit(GlbVarId n){
		n.identifier_.accept(this);
	}
	public void visit(GlbVarList n){ //not printing List classes
		while(n!= null)
		{
			n.glbVar_.accept(this);
			n = n.glbVarList_;
		}
	}
	public void visit(Typ n){	}	//abstract class
	public void visit(TypInt n){
	}
	public void visit(TypBool n){
	}
	public void visit(TypVoid n){
	}
	public void visit(MdDeclList n){ //not printing List classes
		while(n!= null)
		{
			n.mdDecl_.accept(this);
			n = n.mdDeclList_;
		}
	}
	public void visit(MdDecl n){
		n.typ_.accept(this);
		n.identifier_.accept(this);
		if(n.mdParaList_ !=null){ //method parameter list could be NULL
			n.mdParaList_.accept(this);
		}
		n.block_.accept(this);
	}
	public void visit(MdParaList n){ //not printing List classes
		while(n!= null)
		{
			n.mdPara_.accept(this);
			n = n.mdParaList_;
		}
	}
	public void visit(MdPara n){
		n.typ_.accept(this);
		n.identifier_.accept(this);
	}
	public void visit(Block n){
		if(n.varDeclList_!=null) //var declaration list could be empty
			n.varDeclList_.accept(this);
		if(n.stmtList_!=null)//list can be empty
			n.stmtList_.accept(this);
	}
	public void visit(VarDecl n){
		n.typ_.accept(this);
		if(n.locVarList_==null) System.out.println("ERROR!!!!!!!!!!!!!!!");
		n.locVarList_.accept(this);
	}
	public void visit(VarDeclList n){ //not printing List classes
		while(n!= null)
		{
			n.varDecl_.accept(this);
			n = n.varDeclList_;
		}
	}
	public void visit(LocVar n){
		n.identifier_.accept(this);
	}
	public void visit(LocVarList n){ //not printing List classes
		while(n!= null)
		{
			n.locVar_.accept(this);
			n = n.locVarList_;
		}
	}
	public void visit(StmtList n){ //not printing List classes
		while(n!= null)
		{
			n.stmt_.accept(this);
			n = n.stmtList_;
		}
	}
	public void visit(Stmt n){	}	 //abstract class
	public void visit(StmtLocation n){
		n.location_.accept(this);

		n.exp_.accept(this);
		if(n.exp_.algebraicResult != null) n.exp_ = n.exp_.algebraicResult;

	}
	public void visit(StmtBlock n){
		n.block_.accept(this);
	}
	public void visit(StmtMdCall n){
		n.mdCall_.accept(this);
	}
	public void visit(Location n){	}	//abstract class
	public void visit(LocationId n){
		n.identifier_.accept(this);
	}
	public void visit(LocationArray n){
		n.identifier_.accept(this);
		n.exp_.accept(this);
		if(n.exp_.algebraicResult != null) n.exp_ = n.exp_.algebraicResult;
	}
	public void visit(CallOutArgList n){ //not printing List classes
		while(n!= null)
		{
			n.callOutArg_.accept(this);
			n = n.callOutArgList_;
		}
	}
	public void visit(CallOutArg n){	}	//abstract class
	public void visit(CallOutArgExp n){
		n.exp_.accept(this);
		if(n.exp_.algebraicResult != null) n.exp_ = n.exp_.algebraicResult;
	}
	public void visit(CallOutArgStr n){
		n.ltrString_.accept(this);
	}
	public void visit(MdCall n){	}	//abstract class
	public void visit(MdCallOut n){
		n.ltrString_.accept(this);
		if(n.callOutArgList_!=null) //could be empty
			n.callOutArgList_.accept(this);
	}
	public void visit(MdCallIn n){
		n.identifier_.accept(this);
		if(n.expList_!=null) //could be empty
			n.expList_.accept(this);
	}
	public void visit(ExpList n){ //not printing List classes
		while(n!= null)
		{
			n.exp_.accept(this);
			if(n.exp_.algebraicResult != null) n.exp_ = n.exp_.algebraicResult;
			n = n.expList_;
		}
	}
	public void visit(StmtForLoop n){
		n.identifier1_.accept(this);
		n.exp1_.accept(this);
		if(n.exp1_.algebraicResult != null) n.exp1_ = n.exp1_.algebraicResult;
		n.exp2_.accept(this);
		if(n.exp2_.algebraicResult != null) n.exp2_ = n.exp2_.algebraicResult;
		n.identifier3_.accept(this);
		n.exp3_.accept(this);
		if(n.exp3_.algebraicResult != null) n.exp3_ = n.exp3_.algebraicResult;
		n.block_.accept(this);
	}
	public void visit(StmtIfElse n){
		n.exp_.accept(this);
		if(n.exp_.algebraicResult != null) n.exp_ = n.exp_.algebraicResult;
		n.ifBlock_.accept(this);
		if(n.elseBlock_!=null) {  //else block could be empty
			n.elseBlock_.accept(this);
		}
	}
	public void visit(StmtReturn n){
		if(n.exp_ != null){ //could return nothing
			n.exp_.accept(this);
			if(n.exp_.algebraicResult != null) n.exp_ = n.exp_.algebraicResult;
		}
	}
	public void visit(StmtWhileLoop n){

		n.exp_.accept(this);
		if(n.exp_.algebraicResult != null) n.exp_ = n.exp_.algebraicResult;

		n.block_.accept(this);
	}
   	public void visit(Literal n){}//abstract
	public void visit(Exp n){}//abstract
	public void visit(ExpPlus n){
		//accept the 2 sub expressions
		n.exp1_.accept(this);
		if(n.exp1_.algebraicResult != null) n.exp1_ = n.exp1_.algebraicResult;
		n.exp2_.accept(this);
		if(n.exp2_.algebraicResult != null) n.exp2_ = n.exp2_.algebraicResult;
                // check if the traversal is for just searching for a funciton
                if(searchFunction){
                  if(n.exp1_ instanceof MdCall || n.exp2_ instanceof MdCall){
                    functionPresent = true;
                    return;
                  }else{
                    n.exp1_.accept(this);
                    n.exp2_.accept(this);
                    return;
                  }
                }
		/*
		------------------------------------------------------------------
		adding a 0
			a + 0; //return a
			0 + a; // return a
			here "a" could be a) function
					  b) expression
					  c) constant
		-------------------------------------------------------------------
		*/
		if(n.exp1_ instanceof LtrInt){// e1 is a constant
			if (( (LtrInt) n.exp1_).ltrInt_ == 0){//e1 == 0
				n.algebraicResult = n.exp2_;
				n.exp2_ = null;
				n.exp1_ = null;
				newOptDone = true;
			}
		}else if(n.exp2_ instanceof LtrInt){// e2 is a constant
			if (( (LtrInt) n.exp2_).ltrInt_ == 0){//e2 == 0
				n.algebraicResult = n.exp1_;
				n.exp1_ = null;
				n.exp2_ = null;
				newOptDone = true;
			}
		}
		/*
		------------------------------------------------------------------
		operations such as:

		<anything1> * a + <anything2> * a => (<anything1> + <anything2>) * a

		here we are considering for cases where a = LocationID only

		-------------------------------------------------------------------
		*/
		else{
			if(n.exp1_ instanceof ExpTimes && n.exp2_ instanceof ExpTimes){
				ExpTimes newExp1 = (ExpTimes)n.exp1_;
				ExpTimes newExp2 = (ExpTimes)n.exp2_;
				//case 1: exp1.exp1 is the location id
				if(newExp1.exp1_ instanceof LocationId){
					//case 1: exp2.exp1 is the location id
					if(newExp2.exp1_ instanceof LocationId){
						LocationId exp1LocId = (LocationId) newExp1.exp1_;
						LocationId exp2LocId = (LocationId) newExp2.exp1_;
						if(exp1LocId.idd == exp2LocId.idd){
							ExpPlus newExpPlus = new ExpPlus(newExp1.exp2_, newExp2.exp2_);
							ExpTimes newExpTimes = new ExpTimes(newExpPlus, newExp1.exp1_);
                                                        n.algebraicResult = new ExpPlus(newExpTimes , new LtrInt(0));
                                                        newExpTimes = null;
							n.exp1_ = null;
							n.exp2_ = null;
							newExpPlus = null;
							newOptDone = true;
                                                        
						}
					}else
					//case 2: exp2.exp2 is the location id
					if(newExp2.exp2_ instanceof LocationId){
						LocationId exp1LocId = (LocationId) newExp1.exp1_;
						LocationId exp2LocId = (LocationId) newExp2.exp2_;
						if(exp1LocId.idd == exp2LocId.idd){
							ExpPlus newExpPlus = new ExpPlus(newExp1.exp2_, newExp2.exp1_);
							ExpTimes newExpTimes = new ExpTimes(newExpPlus, newExp1.exp1_);
                                                        n.algebraicResult = new ExpPlus(newExpTimes , new LtrInt(0));
                                                        newExpTimes = null;
							n.exp1_ = null;
							n.exp2_ = null;
							newExpPlus = null;
							newOptDone = true;
                                                        
						}
					}
				}
				//case 2: exp1.exp2 is the location id
				if(newExp1.exp2_ instanceof LocationId){
					//case 1: exp2.exp1 is the location id
					if(newExp2.exp1_ instanceof LocationId){
						LocationId exp1LocId = (LocationId) newExp1.exp2_;
						LocationId exp2LocId = (LocationId) newExp2.exp1_;
						if(exp1LocId.idd == exp2LocId.idd){
							ExpPlus newExpPlus = new ExpPlus(newExp1.exp1_, newExp2.exp2_);
							ExpTimes newExpTimes = new ExpTimes(newExpPlus, newExp1.exp2_);
                                                        n.algebraicResult = new ExpPlus(newExpTimes , new LtrInt(0));
                                                        newExpTimes = null;
							n.exp1_ = null;
							n.exp2_ = null;
							newExpPlus = null;
							newOptDone = true;
                                                        
						}

					}
					//case 2: exp2.exp2 is the location id
					if(newExp2.exp2_ instanceof LocationId){
						LocationId exp1LocId = (LocationId) newExp1.exp2_;
						LocationId exp2LocId = (LocationId) newExp2.exp2_;
						if(exp1LocId.idd == exp2LocId.idd){
							ExpPlus newExpPlus = new ExpPlus(newExp1.exp1_, newExp2.exp1_);
							ExpTimes newExpTimes = new ExpTimes(newExpPlus, newExp1.exp2_);
                                                        n.algebraicResult = new ExpPlus(newExpTimes , new LtrInt(0));
                                                        newExpTimes = null;
							n.exp1_ = null;
							n.exp2_ = null;
							newExpPlus = null;
							newOptDone = true;
                                                       
						}
					}
				}
                                newExp1 = null;
                                newExp2 = null;
			}
			/*
			------------------------------------------------------------------
			operations such as:

			<anything1> / a + <anything2> / a => (<anything1> + <anything2>) / a

			here we are considering for cases where a = LocationID only

			-------------------------------------------------------------------
			*/
			else if(n.exp1_ instanceof ExpDivide && n.exp2_ instanceof ExpDivide) {
				ExpDivide newExp1 = (ExpDivide)n.exp1_;
				ExpDivide newExp2 = (ExpDivide)n.exp2_;
				//case 1: exp1.exp2 is the location id and exp2.exp2 is the location id
				if(newExp1.exp2_ instanceof LocationId && newExp2.exp2_ instanceof LocationId){
					LocationId exp1LocId = (LocationId) newExp1.exp2_;
					LocationId exp2LocId = (LocationId) newExp2.exp2_;
					if(exp1LocId.idd == exp2LocId.idd){
						ExpPlus newExpPlus = new ExpPlus(newExp1.exp1_, newExp2.exp1_);
						ExpDivide newExpDivide = new ExpDivide(newExpPlus, newExp1.exp2_);

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -