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

📄 copyprop.java

📁 用Java实现的编译器。把源代码编译成SPARC汇编程序
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
			if(n.exp1_ instanceof LocationId ){				
				if(curCopyTable.table.containsKey(n.idd1_)) curCopyTable.table.put(n.idd1_, ((LocationId)n.exp1_).idd);//must overwrite
				if(!curCopyTable.checkIDDInParents(n.idd1_, ((LocationId)n.exp1_).idd)){
					curCopyTable.table.put(n.idd1_, ((LocationId)n.exp1_).idd);					
					if(curCopyTable.loopKillSet != null && curCopyTable.loopKillSet.contains(n.idd1_))
						curCopyTable.loopKillSet.remove(n.idd1_);
					if(curCopyTable.checkIDDInParents(n.idd1_)) curCopyTable.killSet.add(n.idd1_);  //to be kicked out in parent's constant table
				}
			}else{	//remove from constant table				
				curCopyTable.table.remove(n.idd1_); //no more a constant
				if(curCopyTable.checkIDDInParents(n.idd1_)) curCopyTable.killSet.add(n.idd1_);  //to be kicked out in parent's constant table
			}
			
			 
				//handle it's an assignment to the right, ie, the value is modifified
				//if lid is a value, then find all keys and remove them all;
				//curCopyTable.removeAllKeysFromAllTablesContainingValue(n.idd1_);
				//instead of doing the above, we remove from current table, and add to killset
				//get a list of the keys that contains those values.
				
				Set keys = new HashSet();
				curCopyTable.getAllKeysFromAllTablesContainingValue(n.idd1_, keys);
				for(Iterator i = keys.iterator(); i.hasNext();){
					IDDescriptor idd = (IDDescriptor)i.next();
					curCopyTable.table.remove(idd);
					if(curCopyTable.checkIDDInParents(idd)) curCopyTable.killSet.add(idd);

				}			 
		}
		//=========================
		
		CopyTable forCT = new CopyTable(); forCT.optFlag = false; forCT.parent = curCopyTable; curCopyTable.child1 = forCT;
		if(curCopyTable.loopKillSet != null){
			forCT.loopKillSet = new HashSet();
			forCT.loopKillSet.addAll(curCopyTable.loopKillSet); //preserve loopkillSet
		}

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

		forCT.loopKillSet = loopKill; curCopyTable = forCT; 
		if(curCopyTable.checkIDDInParents(n.idd3_)) curCopyTable.killSet.add(n.idd3_);
		
		LinkedHashMap temp = null;

		//special case, x=x, completely ignored, nothing to optimize or to change
		if(n.exp3_ instanceof LocationId &&  n.idd3_ == ((LocationId)n.exp3_).idd){
			//do nothing, no optimization
		}else 
		{

			if(n.exp3_ instanceof LocationId){
				temp = curCopyTable.checkIDDInParentsAndGetTable(n.idd3_, ((LocationId)n.exp3_).idd);				
			}
			
			n.block_.accept(this);
							
			if(temp != null){
				curCopyTable.killSet.remove(n.idd3_);			
				temp.put(n.idd3_, ((LocationId)n.exp3_).idd);			
			}
			
			
				//handle it's an assignment to the right, ie, the value is modifified
				//if lid is a value, then find all keys and remove them all;
				//curCopyTable.removeAllKeysFromAllTablesContainingValue(n.idd3_);
				//instead of doing the above, we remove from current table, and add to killset
				//get a list of the keys that contains those values.
				
				Set keys = new HashSet();
				curCopyTable.getAllKeysFromAllTablesContainingValue(n.idd3_, keys);
				for(Iterator i = keys.iterator(); i.hasNext();){
					IDDescriptor idd = (IDDescriptor)i.next();
					curCopyTable.table.remove(idd);
					if(curCopyTable.checkIDDInParents(idd)) curCopyTable.killSet.add(idd);

				}				
		}
		curCopyTable = curCopyTable.parent;
		for(Iterator it = forCT.killSet.iterator(); it.hasNext();){
			Object o = it.next();
			if(curCopyTable.checkIDDInParents((IDDescriptor)o)) curCopyTable.killSet.add(o);  //to be kicked out in parent's constant table
		}
		
		
		if(curCopyTable.optFlag) curCopyTable.kill(forCT.killSet);
		
		
	}	
	public void visit(StmtIfElse n){
		
		n.exp_.accept(this);
		if(n.exp_.copyResult != null) n.exp_ = n.exp_.copyResult;
		
		CopyTable ifCT = new CopyTable(); ifCT.optFlag = curCopyTable.optFlag; ifCT.parent = curCopyTable;		
		CopyTable elseCT = null;
		if(n.elseBlock_ != null) {elseCT = new CopyTable(); elseCT.optFlag = curCopyTable.optFlag; elseCT.parent = curCopyTable; }
		
		if(curCopyTable.loopKillSet != null){
			ifCT.loopKillSet = new HashSet();
			ifCT.loopKillSet.addAll(curCopyTable.loopKillSet);							
			if(elseCT != null) {
				elseCT.loopKillSet = new HashSet();
				elseCT.loopKillSet.addAll(curCopyTable.loopKillSet);
			}
		}

		
		curCopyTable.child1 = ifCT; curCopyTable.child2 = elseCT;
		
		curCopyTable = ifCT;
		n.ifBlock_.accept(this);
		
		if(n.elseBlock_!=null) {  //else block could be empty
			curCopyTable = elseCT;
			n.elseBlock_.accept(this);		
		}
		curCopyTable = curCopyTable.parent;		

		for(Iterator it = ifCT.killSet.iterator(); it.hasNext();){
			Object o = it.next();
			if(curCopyTable.checkIDDInParents((IDDescriptor)o)) curCopyTable.killSet.add(o);  //to be kicked out in parent's constant table
			curCopyTable.table.remove((IDDescriptor)o);
		}
		if(elseCT != null)
		{
			for(Iterator it = elseCT.killSet.iterator(); it.hasNext();){
				Object o = it.next();
				if(curCopyTable.checkIDDInParents((IDDescriptor)o)) curCopyTable.killSet.add(o);  //to be kicked out in parent's constant table
				curCopyTable.table.remove((IDDescriptor)o);
			}
		}	
			
		if(curCopyTable.optFlag) curCopyTable.kill(ifCT.killSet);
		if(elseCT != null) {
			if(curCopyTable.optFlag) curCopyTable.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;
					valueUnchanged = o1==o2;
					
					if(valueUnchanged) {
						//System.out.println("saved back!!");
						curCopyTable.table.put(o, o1);
						if(curCopyTable.loopKillSet != null && curCopyTable.loopKillSet.contains(o))
							curCopyTable.loopKillSet.remove(o);
					}
				}
			}
		}
		
			
	}		
	public void visit(StmtReturn n){
		if(n.exp_ != null){ //could return nothing
			n.exp_.accept(this);	
			if(n.exp_.copyResult != null) n.exp_ = n.exp_.copyResult;
		}
	}		
	public void visit(StmtWhileLoop n){
		
		
		n.exp_.accept(this);
		
		CopyTable whileCT = new CopyTable(); whileCT.optFlag = false; whileCT.parent = curCopyTable; curCopyTable.child1 = whileCT;
		if(curCopyTable.loopKillSet != null){
			whileCT.loopKillSet = new HashSet();
			whileCT.loopKillSet.addAll(curCopyTable.loopKillSet); //preserve loopkillSet
		}

		curCopyTable = whileCT;
		
		n.block_.accept(this);
		curCopyTable = curCopyTable.parent;
		HashSet loopKill = whileCT.killSet;
		
		
		n.exp_.accept(this);
		if(n.exp_.copyResult != null) n.exp_ = n.exp_.copyResult;
		int c = count++;
		whileCT = new CopyTable(); whileCT.optFlag = curCopyTable.optFlag; whileCT.parent = curCopyTable; curCopyTable.child1 = whileCT;
		if(curCopyTable.loopKillSet != null){
			whileCT.loopKillSet = new HashSet();
			whileCT.loopKillSet.addAll(curCopyTable.loopKillSet); //preserve loopkillSet
		}		
		
		whileCT.loopKillSet = loopKill; curCopyTable = whileCT;
				
		n.block_.accept(this);	
		curCopyTable = curCopyTable.parent;
		for(Iterator it = whileCT.killSet.iterator(); it.hasNext();){
			Object o = it.next();
			if(curCopyTable.checkIDDInParents((IDDescriptor)o)) curCopyTable.killSet.add(o);  //to be kicked out in parent's constant table
		}
		
		if(curCopyTable.optFlag) curCopyTable.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_.copyResult != null) n.exp1_ = n.exp1_.copyResult; 		
		if(n.exp2_.copyResult != null) n.exp2_ = n.exp2_.copyResult;
        }
	public void visit(ExpMinus n){
		n.exp1_.accept(this);
		n.exp2_.accept(this);
		if(n.exp1_.copyResult != null) n.exp1_ = n.exp1_.copyResult; 		
		if(n.exp2_.copyResult != null) n.exp2_ = n.exp2_.copyResult;
	}
	public void visit(ExpTimes n){
		n.exp1_.accept(this);
		n.exp2_.accept(this);
		if(n.exp1_.copyResult != null) n.exp1_ = n.exp1_.copyResult; 		
		if(n.exp2_.copyResult != null) n.exp2_ = n.exp2_.copyResult;
	}
	public void visit(ExpDivide n){
		n.exp1_.accept(this);
		n.exp2_.accept(this);
		if(n.exp1_.copyResult != null) n.exp1_ = n.exp1_.copyResult; 		
		if(n.exp2_.copyResult != null) n.exp2_ = n.exp2_.copyResult;
	}
	public void visit(ExpMod n){
		n.exp1_.accept(this);
		n.exp2_.accept(this);
		if(n.exp1_.copyResult != null) n.exp1_ = n.exp1_.copyResult; 		
		if(n.exp2_.copyResult != null) n.exp2_ = n.exp2_.copyResult;
	}
	public void visit(ExpUMinus n){
		n.exp_.accept(this);
		if(n.exp_.copyResult != null) n.exp_ = n.exp_.copyResult; 		
		
	}
	public void visit(ExpShiftLeft n){
		n.exp1_.accept(this);
		n.exp2_.accept(this);
		if(n.exp1_.copyResult != null) n.exp1_ = n.exp1_.copyResult; 		
		if(n.exp2_.copyResult != null) n.exp2_ = n.exp2_.copyResult;
	}
	public void visit(ExpShiftRight n){
		n.exp1_.accept(this);
		n.exp2_.accept(this);
		if(n.exp1_.copyResult != null) n.exp1_ = n.exp1_.copyResult; 		
		if(n.exp2_.copyResult != null) n.exp2_ = n.exp2_.copyResult;
	}
	public void visit(ExpLessThan n){
		n.exp1_.accept(this);
		n.exp2_.accept(this);
		if(n.exp1_.copyResult != null) n.exp1_ = n.exp1_.copyResult; 		
		if(n.exp2_.copyResult != null) n.exp2_ = n.exp2_.copyResult;
	}
	public void visit(ExpGreaterThan n){
		n.exp1_.accept(this);
		n.exp2_.accept(this);
		if(n.exp1_.copyResult != null) n.exp1_ = n.exp1_.copyResult; 		
		if(n.exp2_.copyResult != null) n.exp2_ = n.exp2_.copyResult;
	}
	public void visit(ExpLessThEql n){
		n.exp1_.accept(this);
		n.exp2_.accept(this);
		if(n.exp1_.copyResult != null) n.exp1_ = n.exp1_.copyResult; 		
		if(n.exp2_.copyResult != null) n.exp2_ = n.exp2_.copyResult;
	}
	public void visit(ExpGreaterThEql n){
		n.exp1_.accept(this);
		n.exp2_.accept(this);
		if(n.exp1_.copyResult != null) n.exp1_ = n.exp1_.copyResult; 		
		if(n.exp2_.copyResult != null) n.exp2_ = n.exp2_.copyResult;
	}
	public void visit(ExpEqualTo n){
		n.exp1_.accept(this);
		n.exp2_.accept(this);
		if(n.exp1_.copyResult != null) n.exp1_ = n.exp1_.copyResult; 		
		if(n.exp2_.copyResult != null) n.exp2_ = n.exp2_.copyResult;
	}
	public void visit(ExpNotEqualTo n){
		n.exp1_.accept(this);
		n.exp2_.accept(this);
		if(n.exp1_.copyResult != null) n.exp1_ = n.exp1_.copyResult; 		
		if(n.exp2_.copyResult != null) n.exp2_ = n.exp2_.copyResult;
	}
	public void visit(ExpAndOp n){
		n.exp1_.accept(this);
		n.exp2_.accept(this);
		if(n.exp1_.copyResult != null) n.exp1_ = n.exp1_.copyResult; 		
		if(n.exp2_.copyResult != null) n.exp2_ = n.exp2_.copyResult;
	}
	public void visit(ExpOrOp n){
		n.exp1_.accept(this);
		n.exp2_.accept(this);
		if(n.exp1_.copyResult != null) n.exp1_ = n.exp1_.copyResult; 		
		if(n.exp2_.copyResult != null) n.exp2_ = n.exp2_.copyResult;
	}
	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 + -