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

📄 constantprop.java

📁 用Java实现的编译器。把源代码编译成SPARC汇编程序
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
		}
	}
	public void visit(StmtForLoop n){
		//========id1 = expr1========n.exp1_.accept(this);

		n.exp1_.accept(this);
		if(n.exp1_.constantResult != null) n.exp1_ = n.exp1_.constantResult;
		if(n.exp1_ instanceof LtrInt || n.exp1_ instanceof LtrBoolTrue || n.exp1_ instanceof LtrBoolFalse ){
			if(curConstantTable.table.containsKey(n.idd1_)) curConstantTable.table.put(n.idd1_, n.exp1_);//must overwrite
			if(!curConstantTable.checkIDDInParents(n.idd1_, n.exp1_)){
				curConstantTable.table.put(n.idd1_, n.exp1_);
				if(curConstantTable.loopKillSet != null && curConstantTable.loopKillSet.contains(n.idd1_))
					curConstantTable.loopKillSet.remove(n.idd1_);
				if(curConstantTable.checkIDDInParents(n.idd1_)) curConstantTable.killSet.add(n.idd1_);  //to be kicked out in parent's constant table
			}
		}else{	//remove from constant table
			curConstantTable.table.remove(n.idd1_); //no more a constant
			if(curConstantTable.checkIDDInParents(n.idd1_)) curConstantTable.killSet.add(n.idd1_);  //to be kicked out in parent's constant table
		}

		//=========================mock run=========

		ConstantTable forCT = new ConstantTable(); forCT.optFlag = false; forCT.parent = curConstantTable; curConstantTable.child1 = forCT;
		if(curConstantTable.loopKillSet != null){
			forCT.loopKillSet = new HashSet();
			forCT.loopKillSet.addAll(curConstantTable.loopKillSet); //preserve loopkillSet
		}
		curConstantTable = forCT;
		n.exp2_.accept(this);
		n.exp3_.accept(this);
		curConstantTable.table.remove(n.idd3_);
		if(curConstantTable.checkIDDInParents(n.idd3_)) curConstantTable.killSet.add(n.idd3_);
		n.block_.accept(this);
		curConstantTable = curConstantTable.parent;
		HashSet loopKill = forCT.killSet;

		//=========================real run=========
		n.exp2_.accept(this);
		if(n.exp2_.constantResult != null) n.exp2_ = n.exp2_.constantResult;
		n.exp3_.accept(this);
		if(n.exp3_.constantResult != null) n.exp3_ = n.exp3_.constantResult;


		forCT = new ConstantTable(); forCT.optFlag = curConstantTable.optFlag; forCT.parent = curConstantTable; curConstantTable.child1 = forCT;
		if(curConstantTable.loopKillSet != null){
			forCT.loopKillSet = new HashSet();
			forCT.loopKillSet.addAll(curConstantTable.loopKillSet); //preserve loopkillSet
		}
		forCT.loopKillSet = loopKill; curConstantTable = forCT;
		if(curConstantTable.checkIDDInParents(n.idd3_)) curConstantTable.killSet.add(n.idd3_);

		LinkedHashMap temp = null;
		if(n.exp3_ instanceof LtrInt || n.exp3_ instanceof LtrBoolTrue || n.exp3_ instanceof LtrBoolFalse ){
			temp = curConstantTable.checkIDDInParentsAndGetTable(n.idd3_, n.exp3_);
		}

		n.block_.accept(this);

		if(temp != null){
			curConstantTable.killSet.remove(n.idd3_);
			temp.put(n.idd3_, n.exp3_);
		}
		curConstantTable = curConstantTable.parent;
		for(Iterator it = forCT.killSet.iterator(); it.hasNext();){
			Object o = it.next();
			if(curConstantTable.checkIDDInParents((IDDescriptor)o)) curConstantTable.killSet.add(o);  //to be kicked out in parent's constant table
		}

		if(curConstantTable.optFlag) curConstantTable.kill(forCT.killSet);


	}
	public void visit(StmtIfElse n){

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

		ConstantTable ifCT = new ConstantTable(); ifCT.optFlag = curConstantTable.optFlag; ifCT.parent = curConstantTable;
		ConstantTable elseCT = null;
		if(n.elseBlock_ != null) {elseCT = new ConstantTable(); elseCT.optFlag = curConstantTable.optFlag; elseCT.parent = curConstantTable; }

		if(curConstantTable.loopKillSet != null){
			ifCT.loopKillSet = new HashSet();
			ifCT.loopKillSet.addAll(curConstantTable.loopKillSet);
			if(elseCT != null) {
				elseCT.loopKillSet = new HashSet();
				elseCT.loopKillSet.addAll(curConstantTable.loopKillSet);
			}
		}

		curConstantTable.child1 = ifCT; curConstantTable.child2 = elseCT;

		curConstantTable = ifCT;
		n.ifBlock_.accept(this);

		if(n.elseBlock_!=null) {  //else block could be empty
			curConstantTable = elseCT;
			n.elseBlock_.accept(this);
		}
		curConstantTable = curConstantTable.parent;

		for(Iterator it = ifCT.killSet.iterator(); it.hasNext();){
			Object o = it.next();
			if(curConstantTable.checkIDDInParents((IDDescriptor)o)) curConstantTable.killSet.add(o);  //to be kicked out in parent's constant table
			curConstantTable.table.remove((IDDescriptor)o);
		}
		if(elseCT != null)
		{
			for(Iterator it = elseCT.killSet.iterator(); it.hasNext();){
				Object o = it.next();
				if(curConstantTable.checkIDDInParents((IDDescriptor)o)) curConstantTable.killSet.add(o);  //to be kicked out in parent's constant table
				curConstantTable.table.remove((IDDescriptor)o);
			}
		}

		if(curConstantTable.optFlag) curConstantTable.kill(ifCT.killSet);
		if(elseCT != null) {
			if(curConstantTable.optFlag) curConstantTable.kill(elseCT.killSet);

			//comparing ifCT.table and elseCT.table to pick out common assignment
			for(Iterator it = elseCT.table.keySet().iterator(); it.hasNext();){
				Object o = it.next();
				//System.out.println("In else");
				//((IDDescriptor)o).pPrint();
				if(ifCT.table.containsKey(o)){
					Object o1 = ifCT.table.get(o);
					Object o2 = elseCT.table.get(o);
					boolean valueUnchanged = false;
					if(o1 instanceof LtrInt && o2 instanceof LtrInt) valueUnchanged = (((LtrInt)o1).ltrInt_ == ((LtrInt)o2).ltrInt_ );
					else if(o1 instanceof LtrBoolFalse) valueUnchanged = (o2 instanceof LtrBoolFalse);
					else if(o1 instanceof LtrBoolTrue) valueUnchanged = (o2 instanceof LtrBoolTrue);

					if(valueUnchanged) {
						//System.out.println("saved back!!");
						curConstantTable.table.put(o, o1);
						if(curConstantTable.loopKillSet != null && curConstantTable.loopKillSet.contains(o))
							curConstantTable.loopKillSet.remove(o);
					}
				}
			}
		}


	}
	public void visit(StmtReturn n){
		if(n.exp_ != null){ //could return nothing
			n.exp_.accept(this);
			if(n.exp_.constantResult != null) n.exp_ = n.exp_.constantResult;
		}
	}
	public void visit(StmtWhileLoop n){


		n.exp_.accept(this);
		//============mock run
		ConstantTable whileCT = new ConstantTable();
                whileCT.optFlag = false;
                whileCT.parent = curConstantTable;
                curConstantTable.child1 = whileCT;
		if(curConstantTable.loopKillSet != null){
			whileCT.loopKillSet = new HashSet();
			whileCT.loopKillSet.addAll(curConstantTable.loopKillSet); //preserve loopkillSet
		}
		curConstantTable = whileCT;

		n.block_.accept(this);
		curConstantTable = curConstantTable.parent;
		HashSet loopKill = whileCT.killSet;

		//============real run
		n.exp_.accept(this);
		if(n.exp_.constantResult != null) n.exp_ = n.exp_.constantResult;
		int c = count++;
		whileCT = new ConstantTable(); whileCT.optFlag = curConstantTable.optFlag; whileCT.parent = curConstantTable; curConstantTable.child1 = whileCT;
		if(curConstantTable.loopKillSet != null){
			whileCT.loopKillSet = new HashSet();
			whileCT.loopKillSet.addAll(curConstantTable.loopKillSet); //preserve loopkillSet
		}
		whileCT.loopKillSet = loopKill; curConstantTable = whileCT;

		n.block_.accept(this);
		curConstantTable = curConstantTable.parent;

		for(Iterator it = whileCT.killSet.iterator(); it.hasNext();){
			Object o = it.next();
			if(curConstantTable.checkIDDInParents((IDDescriptor)o)) curConstantTable.killSet.add(o);  //to be kicked out in parent's constant table
		}
		if(curConstantTable.optFlag) curConstantTable.kill(whileCT.killSet);


	}
   	public void visit(Literal n){}//abstract
	public void visit(Exp n){}//abstract
	public void visit(ExpPlus n){
		n.exp1_.accept(this);
		n.exp2_.accept(this);
		if(n.exp1_.constantResult != null) n.exp1_ = n.exp1_.constantResult;
		if(n.exp2_.constantResult != null) n.exp2_ = n.exp2_.constantResult;
        }
	public void visit(ExpMinus n){
		n.exp1_.accept(this);
		n.exp2_.accept(this);
		if(n.exp1_.constantResult != null) n.exp1_ = n.exp1_.constantResult;
		if(n.exp2_.constantResult != null) n.exp2_ = n.exp2_.constantResult;
	}
	public void visit(ExpTimes n){
		n.exp1_.accept(this);
		n.exp2_.accept(this);
		if(n.exp1_.constantResult != null) n.exp1_ = n.exp1_.constantResult;
		if(n.exp2_.constantResult != null) n.exp2_ = n.exp2_.constantResult;
	}
	public void visit(ExpDivide n){
		n.exp1_.accept(this);
		n.exp2_.accept(this);
		if(n.exp1_.constantResult != null) n.exp1_ = n.exp1_.constantResult;
		if(n.exp2_.constantResult != null) n.exp2_ = n.exp2_.constantResult;
	}
	public void visit(ExpMod n){
		n.exp1_.accept(this);
		n.exp2_.accept(this);
		if(n.exp1_.constantResult != null) n.exp1_ = n.exp1_.constantResult;
		if(n.exp2_.constantResult != null) n.exp2_ = n.exp2_.constantResult;
	}
	public void visit(ExpUMinus n){
		n.exp_.accept(this);
		if(n.exp_.constantResult != null) n.exp_ = n.exp_.constantResult;

	}
	public void visit(ExpShiftLeft n){
		n.exp1_.accept(this);
		n.exp2_.accept(this);
		if(n.exp1_.constantResult != null) n.exp1_ = n.exp1_.constantResult;
		if(n.exp2_.constantResult != null) n.exp2_ = n.exp2_.constantResult;
	}
	public void visit(ExpShiftRight n){
		n.exp1_.accept(this);
		n.exp2_.accept(this);
		if(n.exp1_.constantResult != null) n.exp1_ = n.exp1_.constantResult;
		if(n.exp2_.constantResult != null) n.exp2_ = n.exp2_.constantResult;
	}
	public void visit(ExpLessThan n){
		n.exp1_.accept(this);
		n.exp2_.accept(this);
		if(n.exp1_.constantResult != null) n.exp1_ = n.exp1_.constantResult;
		if(n.exp2_.constantResult != null) n.exp2_ = n.exp2_.constantResult;
	}
	public void visit(ExpGreaterThan n){
		n.exp1_.accept(this);
		n.exp2_.accept(this);
		if(n.exp1_.constantResult != null) n.exp1_ = n.exp1_.constantResult;
		if(n.exp2_.constantResult != null) n.exp2_ = n.exp2_.constantResult;
	}
	public void visit(ExpLessThEql n){
		n.exp1_.accept(this);
		n.exp2_.accept(this);
		if(n.exp1_.constantResult != null) n.exp1_ = n.exp1_.constantResult;
		if(n.exp2_.constantResult != null) n.exp2_ = n.exp2_.constantResult;
	}
	public void visit(ExpGreaterThEql n){
		n.exp1_.accept(this);
		n.exp2_.accept(this);
		if(n.exp1_.constantResult != null) n.exp1_ = n.exp1_.constantResult;
		if(n.exp2_.constantResult != null) n.exp2_ = n.exp2_.constantResult;
	}
	public void visit(ExpEqualTo n){
		n.exp1_.accept(this);
		n.exp2_.accept(this);
		if(n.exp1_.constantResult != null) n.exp1_ = n.exp1_.constantResult;
		if(n.exp2_.constantResult != null) n.exp2_ = n.exp2_.constantResult;
	}
	public void visit(ExpNotEqualTo n){
		n.exp1_.accept(this);
		n.exp2_.accept(this);
		if(n.exp1_.constantResult != null) n.exp1_ = n.exp1_.constantResult;
		if(n.exp2_.constantResult != null) n.exp2_ = n.exp2_.constantResult;
	}
	public void visit(ExpAndOp n){
		n.exp1_.accept(this);
		n.exp2_.accept(this);
		if(n.exp1_.constantResult != null) n.exp1_ = n.exp1_.constantResult;
		if(n.exp2_.constantResult != null) n.exp2_ = n.exp2_.constantResult;
	}
	public void visit(ExpOrOp n){
		n.exp1_.accept(this);
		n.exp2_.accept(this);
		if(n.exp1_.constantResult != null) n.exp1_ = n.exp1_.constantResult;
		if(n.exp2_.constantResult != null) n.exp2_ = n.exp2_.constantResult;
	}
	public void visit(LtrBoolFalse n){
	}
	public void visit(LtrBoolTrue n){
	}
	public void visit(LtrChar n){
	}
	public void visit(LtrInt n){
	}
	public void visit(LtrString n){
	}
	public void visit(Identifier n){
	}
}

⌨️ 快捷键说明

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