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

📄 constantprop.java

📁 用Java实现的编译器。把源代码编译成SPARC汇编程序
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/**
*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 ConstantProp implements Visitor{

//public static boolean optFlag;
//public static HashSet loopKillSet;==========big mistake!!!!!!
public static ConstantTable curConstantTable;
public static int count;
public static boolean newOptDone;

	/*public ConstantProp(){
		curConstantTable = new LinkedHashMap();
	}*/



	public void visit(Ir n){        } //abstract class
	public void visit(IrProg n){

		newOptDone = false;
		if(n.fdDeclList_!=null)
			n.fdDeclList_.accept(this);
		if(n.mdDeclList_ == null) System.out.println("ERROR!!!!!!!!!!!!!!!!!!!!!!!!!!");
			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){
		curConstantTable = new ConstantTable();curConstantTable.optFlag = true;
		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


/************************************************************************************************/
// You may take the above code as a reference
/***********************************************************************************************/
/*
	public void visit(StmtLocation n){
		if(n.location_ instanceof LocationId && (n.exp_ instanceof LtrInt || n.exp_ instanceof LtrBoolTrue || n.exp_ instanceof LtrBoolFalse )){
			LocationId lid = (LocationId)n.location_;
			Exp e = n.exp_;
			//check whether idd already there and whther value changed
			Object o=null;
			boolean valueUnchanged = true;
			if ((o = curConstantTable.lookupValue(lid.idd)) != null){
				if(loopKillSet == null || !loopKillSet.contains(lid.idd)){
					//n.constantResult = (Exp) o;
					//clone a new object here is always safe so that no conflict in register allocation
					if(o instanceof LtrInt) valueUnchanged = ((LtrInt)o).ltrInt_ == ((LtrInt)e).ltrInt_ ;
					else if(o instanceof LtrBoolFalse) valueUnchanged = e instanceof LtrBoolFalse;
					else if(o instanceof LtrBoolTrue) valueUnchanged = e instanceof LtrBoolTrue;
				}
			}
			if (o!=null && valueUnchanged ) {//idd already there, check whether new assignment gives a different value
				//idd there and same, keep it
				return;
			}else{//idd not there or different then kill
				curConstantTable.table.put(lid.idd, n.exp_);
				if(curConstantTable.checkIDDInParents(lid.idd)) curConstantTable.killSet.add(lid.idd);	// to be killed by this block's parent
			}
		}else{
			n.exp_.accept(this);
			if(n.exp_.constantResult != null) n.exp_ = n.exp_.constantResult;
			if(n.location_ instanceof LocationId){
				LocationId lid = (LocationId)n.location_;
				if(n.exp_ instanceof LtrInt || n.exp_ instanceof LtrBoolTrue || n.exp_ instanceof LtrBoolFalse )	{
					Exp e = n.exp_;
					//check whether idd already there and whther value changed
					Object o=null;
					boolean valueUnchanged = true;
					if ((o = curConstantTable.lookupValue(lid.idd)) != null){
						if(loopKillSet == null || !loopKillSet.contains(lid.idd)){
							//n.constantResult = (Exp) o;
							//clone a new object here is always safe so that no conflict in register allocation
							if(o instanceof LtrInt) valueUnchanged = ((LtrInt)o).ltrInt_ == ((LtrInt)e).ltrInt_ ;
							else if(o instanceof LtrBoolFalse) valueUnchanged = e instanceof LtrBoolFalse;
							else if(o instanceof LtrBoolTrue) valueUnchanged = e instanceof LtrBoolTrue;
						}
					}
					if (o!=null && valueUnchanged ) {//idd already there, check whether new assignment gives a different value
						//idd there and same, keep it
						return;
					}else{//idd not there or different then kill
						curConstantTable.table.put(lid.idd, n.exp_);
						if(curConstantTable.checkIDDInParents(lid.idd)) curConstantTable.killSet.add(lid.idd);	// to be killed by this block's parent
					}
				}
				else{	//remove from constant table
					curConstantTable.table.remove(lid.idd); //no more a constant
					if(curConstantTable.checkIDDInParents(lid.idd)) curConstantTable.killSet.add(lid.idd);  //to be kicked out in parent's constant table
				}
			}
		}


	}

/************************************************************************************************/
// You may take the above code as a reference
/***********************************************************************************************/


	public void visit(StmtLocation n){
		/*if(n.location_ instanceof LocationId && (n.exp_ instanceof LtrInt || n.exp_ instanceof LtrBoolTrue || n.exp_ instanceof LtrBoolFalse )){
			LocationId lid = (LocationId)n.location_;

			if(curConstantTable.table.containsKey(lid.idd)) curConstantTable.table.put(lid.idd, n.exp_);//must overwrite
			if(!curConstantTable.checkIDDInParents(lid.idd, n.exp_)){
				curConstantTable.table.put(lid.idd, n.exp_);
				if(curConstantTable.loopKillSet != null && curConstantTable.loopKillSet.contains(lid.idd))
					curConstantTable.loopKillSet.remove(lid.idd);

				if(curConstantTable.checkIDDInParents(lid.idd)) curConstantTable.killSet.add(lid.idd);	// to be killed by this block's parent
			}
		}else{*/
			n.exp_.accept(this);
			if(n.exp_.constantResult != null) n.exp_ = n.exp_.constantResult;
			if(n.location_ instanceof LocationId){
				LocationId lid = (LocationId)n.location_;
				if(n.exp_ instanceof LtrInt || n.exp_ instanceof LtrBoolTrue || n.exp_ instanceof LtrBoolFalse ){
					if(curConstantTable.table.containsKey(lid.idd)) curConstantTable.table.put(lid.idd, n.exp_);//must overwrite
					if(!curConstantTable.checkIDDInParents(lid.idd, n.exp_)){
						curConstantTable.table.put(lid.idd, n.exp_);

						if(curConstantTable.loopKillSet != null && curConstantTable.loopKillSet.contains(lid.idd))
							curConstantTable.loopKillSet.remove(lid.idd);

						if(curConstantTable.checkIDDInParents(lid.idd)) curConstantTable.killSet.add(lid.idd);  //to be kicked out in parent's constant table
					}
				}
				else{	//remove from constant table
					curConstantTable.table.remove(lid.idd); //no more a constant
					if(curConstantTable.checkIDDInParents(lid.idd)) curConstantTable.killSet.add(lid.idd);  //to be kicked out in parent's constant table
				}
			}
		//}


	}

	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){
		if(curConstantTable.optFlag){
			Object o;
			if((o = curConstantTable.lookupValue(n.idd)) != null){
				if(curConstantTable.loopKillSet == null || !curConstantTable.loopKillSet.contains(n.idd)){
					//n.constantResult = (Exp) o;
					//clone a new object here is always safe so that no conflict in register allocation
					if(o instanceof LtrInt) n.constantResult = new LtrInt(((LtrInt)o).ltrInt_);
					else if(o instanceof LtrBoolFalse) n.constantResult = new LtrBoolFalse();
					else if(o instanceof LtrBoolTrue) n.constantResult = new LtrBoolTrue();
					newOptDone = true;
				}
			}
		}

	}
	public void visit(LocationArray n){

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

	}
	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_.constantResult != null) n.exp_ = n.exp_.constantResult;
	}
	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_.constantResult != null) n.exp_ = n.exp_.constantResult;
			n = n.expList_;

⌨️ 快捷键说明

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