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

📄 algebraicsimplification.java

📁 用Java实现的编译器。把源代码编译成SPARC汇编程序
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
                                                n.algebraicResult = new ExpPlus(newExpDivide , new LtrInt(0));
                                                newExpDivide = 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 ExpMod && n.exp2_ instanceof ExpMod) {
                                ExpMod newExp1 = (ExpMod)n.exp1_;
                                ExpMod newExp2 = (ExpMod)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_);
                                                ExpMod newExpMod = new ExpMod(newExpPlus, newExp1.exp2_);
                                                n.algebraicResult = new ExpPlus(newExpMod , new LtrInt(0));
                                                newExpMod = null;
                                                n.exp1_ = null;
                                                n.exp2_ = null;
                                                newExpPlus = null;
                                                newOptDone = true;
                                               
                                        }
                                }
                                newExp1 = null;
                                newExp2 = null;
                        }
		}
        }
	public void visit(ExpMinus 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;
                  }

                }
		/*
		-------------------------------------------------------------------
		subtraction by the same value
		a - a
		-------------------------------------------------------------------
		*/
		if(n.exp1_ instanceof LtrInt && n.exp2_ instanceof LtrInt){// both e1 and e2 are constants
			if (( (LtrInt) n.exp1_).ltrInt_ == ( (LtrInt) n.exp2_).ltrInt_){//e1 == e2
				n.algebraicResult = new LtrInt(0);
				n.exp1_ = null;
				n.exp2_ = null;
				newOptDone = true;
			}
		}else{
			/*
			-------------------------------------------------------------------
			subtraction of 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
					// convert e2 to -e2
					ExpUMinus negExp = new ExpUMinus(n.exp2_);
					n.algebraicResult = negExp;
					n.exp2_ = null;
					n.exp1_ = null;
					negExp = 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){
                                                        ExpMinus newExpMinus = new ExpMinus(newExp1.exp2_, newExp2.exp2_);
                                                        ExpTimes newExpTimes = new ExpTimes(newExpMinus, newExp1.exp1_);
                                                        n.algebraicResult = new ExpMinus(newExpTimes , new LtrInt(0));
                                                        newExpTimes = null;
                                                        n.exp1_ = null;
                                                        n.exp2_ = null;
                                                        newExpMinus = 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){
                                                        ExpMinus newExpMinus = new ExpMinus(newExp1.exp2_, newExp2.exp1_);
                                                        ExpTimes newExpTimes = new ExpTimes(newExpMinus, newExp1.exp1_);
                                                        n.algebraicResult = new ExpMinus(newExpTimes , new LtrInt(0));
                                                        newExpTimes = null;
                                                        n.exp1_ = null;
                                                        n.exp2_ = null;
                                                        newExpMinus = 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){
                                                        ExpMinus newExpMinus = new ExpMinus(newExp1.exp1_, newExp2.exp2_);
                                                        ExpTimes newExpTimes = new ExpTimes(newExpMinus, newExp1.exp2_);
                                                        n.algebraicResult = new ExpMinus(newExpTimes , new LtrInt(0));
                                                        newExpTimes = null;
                                                        n.exp1_ = null;
                                                        n.exp2_ = null;
                                                        newExpMinus = 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){
                                                        ExpMinus newExpMinus = new ExpMinus(newExp1.exp1_, newExp2.exp1_);
                                                        ExpTimes newExpTimes = new ExpTimes(newExpMinus, newExp1.exp2_);
                                                        n.algebraicResult = new ExpMinus(newExpTimes , new LtrInt(0));
                                                        newExpTimes = null;
                                                        n.exp1_ = null;
                                                        n.exp2_ = null;
                                                        newExpMinus = 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){
                                                ExpMinus newExpMinus = new ExpMinus(newExp1.exp1_, newExp2.exp1_);
                                                ExpDivide newExpDivide = new ExpDivide(newExpMinus, newExp1.exp2_);
                                                n.algebraicResult = new ExpMinus(newExpDivide , new LtrInt(0));
                                                newExpDivide = null;
                                                n.exp1_ = null;
                                                n.exp2_ = null;
                                                newExpMinus = 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 ExpMod && n.exp2_ instanceof ExpMod) {
                                ExpMod newExp1 = (ExpMod)n.exp1_;
                                ExpMod newExp2 = (ExpMod)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){
                                                ExpMinus newExpMinus = new ExpMinus(newExp1.exp1_, newExp2.exp1_);
                                                ExpMod newExpMod = new ExpMod(newExpMinus, newExp1.exp2_);
                                                n.algebraicResult = new ExpMinus(newExpMod , new LtrInt(0));
                                                newExpMod = null;
                                                n.exp1_ = null;
                                                n.exp2_ = null;
                                                newExpMinus = null;
                                                newOptDone = true;
                                                
                                        }
                                }
                                newExp1 = null;
                                newExp2 = null;
                        }
                }
		}
	}
	public void visit(ExpTimes 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;
                  }

                }
		/*
		-------------------------------------------------------------------
		multiplication operation by 0
			a * 0;
			0 * a;
		-------------------------------------------------------------------
		-------------------------------------------------------------------
		multiplication operation by 1
			a * 1;
			1 * a;
		-------------------------------------------------------------------
		-------------------------------------------------------------------
		multiplication operation by 3
			a * 3; // should convert to a + a + a
		-------------------------------------------------------------------
		-------------------------------------------------------------------
		multiplication operation by powers of 2
			a * (2 power n); // shift by the power value n
			(2 power n) * a; // shift by the power value n
		-------------------------------------------------------------------
		*/
               if(n.exp1_ instanceof LtrInt){// e1 is a constant
			if (( (LtrInt) n.exp1_).ltrInt_ == 0){//e1 == 0
				// check if e2 has a function
				searchFunction = true;
				n.exp2_.accept(this);
				if(!functionPresent){
					n.algebraicResult = new LtrInt(0);
					n.exp2_ = null;
					n.exp1_ = null;
					newOptDone = true;
				}
				searchFunction = false;
			}else if (( (LtrInt) n.exp1_).ltrInt_ == 1){//e1 == 1
				n.algebraicResult = n.exp2_;
				n.exp2_ = null;
				n.exp1_ = null;
				newOptDone = true;
			}else {
				int expvalue = ( (LtrInt) n.exp1_).ltrInt_;
	                     int lgValExp = (new Double(Math.log(expvalue) / Math.log(2))).intValue();
	                     int powerValue = (new Double(Math.pow(2, lgValExp))).intValue();
	                     if (powerValue == expvalue){// e1 is a power of 2
					ExpShiftLeft newExp = new ExpShiftLeft(n.exp2_, new LtrInt(lgValExp));
					n.algebraicResult = new ExpTimes(newExp, new LtrInt(1));
					newExp = null;
					n.exp1_ = null;
					n.exp2_ = null;
					newOptDone = true;
				}
			}
		}else if(n.exp2_ instanceof LtrInt){// e2 is a constant
			if (( (LtrInt) n.exp2_).ltrInt_ == 0){//e2 == 0
				// check if e1 has a function
				searchFunction = true;
				n.exp1_.accept(this);
				if(!functionPresent){
					n.algebraicResult = new LtrInt(0);
					n.exp2_ = null;
					n.exp1_ = null;
					newOptDone = true;
				}
				searchFunction = false;
			}else if (( (LtrInt) n.exp2_).ltrInt_ == 1){//e2 == 1
				n.algebraicResult = n.exp1_;
				n.exp2_ = null;
				n.exp1_ = null;
				newOptDone = true;
			}else {
				int expvalue = ( (LtrInt) n.exp2_).ltrInt_;
				int lgValExp = (new Double(Math.log(expvalue) / Math.log(2))).intValue();
				int powerValue = (new Double(Math.pow(2, lgValExp))).intValue();
				if (powerValue == expvalue){// e2 is a power of 2
					ExpShiftLeft newExp = new ExpShiftLeft(n.exp1_, new LtrInt(lgValExp));
					n.algebraicResult = new ExpTimes(newExp, new LtrInt(1));
					newExp = null;
					n.exp1_ = null;
					n.exp2_ = null;
					newOptDone = true;
				}
			}
		}

	}
	public void visit(ExpDivide n){
		//accept the 2 sub expressions

⌨️ 快捷键说明

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