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

📄 xunh.java

📁 一个java编译器
💻 JAVA
📖 第 1 页 / 共 5 页
字号:
}
/********************************************************/
/* 函数名  PrintCotent  				*/
/* 功  能  打印ARG结构的内容				*/
/* 说  明  由函数PrintOneCode调用			*/
/********************************************************/
void PrintContent(ArgRecord arg)
{
             if (arg.form.equals("LabelForm"))
                 midcode=midcode+String.valueOf(arg.midAttr.label);
             else if (arg.form.equals("ValueForm")) 
                 midcode=midcode+String.valueOf(arg.midAttr.value);
             else if (arg.form.equals("AddrForm")) 
	     {   
		 if (arg.midAttr.addr.dataLevel!=-1)
		     midcode=midcode+arg.midAttr.addr.name;
		 else  
		     midcode=midcode+"temp"+String.valueOf(arg.midAttr.addr.dataOff);
	     }
}
/********************************************************/
/* 函数名  PrintOneCode 				*/
/* 功  能  打印一条中间代码				*/
/* 说  明  由函数PrintMidCode调用			*/
/********************************************************/
void PrintOneCode(CodeFile code)
{ 
             PrintCodeName(code.codeR.codekind);
             midcode=midcode+"    ";
             if (code.codeR.arg1!=null)
	         PrintContent(code.codeR.arg1);
             else  
                 midcode=midcode+"    ";
             midcode=midcode+"    ";
             if (code.codeR.arg2!=null)
	         PrintContent(code.codeR.arg2);
             else  
                 midcode=midcode+"    ";
             midcode=midcode+"    ";
             if (code.codeR.arg3!=null)
	         PrintContent(code.codeR.arg3);
             else                   
                 midcode=midcode+"    ";  
}
/********************************************************/
/* 函数名  PrintMidCode 				*/
/* 功  能  打印中间代码序列				*/
/* 说  明						*/
/********************************************************/
void PrintMidCode(CodeFile firstCode)
{   
    int i = 0;
    CodeFile code = firstCode;
    midcode="\n";
    while (code!=null)
    { 
         midcode=midcode+String.valueOf(i)+":  ";
         PrintOneCode(code);
	 midcode=midcode+"\n";
	 code = code.next;
	 i++;
    }
}

/****************************************************/
/* 函数名  LoopOpti				    */
/* 功  能  循环不变式优化主函数		            */
/* 说  明					    */
/****************************************************/
CodeFile LoopOpti()
{   
    /*从第一条代码开始优化过程*/
    CodeFile currentCode = firstCode;

    /*循环处理每条代码,直到中间代码结束*/
    while (currentCode!=null)
    {   
	if ((currentCode.codeR.codekind.equals("AADD"))||(currentCode.codeR.codekind.equals("ADD"))||(currentCode.codeR.codekind.equals("SUB"))||(currentCode.codeR.codekind.equals("MULT"))||(currentCode.codeR.codekind.equals("DIV"))||(currentCode.codeR.codekind.equals("LTC"))||(currentCode.codeR.codekind.equals("EQC")))	
	    /*将被定值的变量名填入变量定值表中*/
	    AddTable(currentCode.codeR.arg3);
        else if (currentCode.codeR.codekind.equals("ASSIG"))
	    /*将被定值的变量填入变量定值表中*/
	    AddTable(currentCode.codeR.arg2);
        else if (currentCode.codeR.codekind.equals("WHILESTART"))
	    /*循环入口*/
	    whileEntry(currentCode);
        else if (currentCode.codeR.codekind.equals("ENDWHILE"))
	    /*循环出口*/
	    whileEnd(currentCode);
        else if (currentCode.codeR.codekind.equals("CALL"))
	    /*过程调用语句*/
	    call(currentCode);

        /*令指针指向下一条代码*/
	currentCode = currentCode.next;	 
    }
    return firstCode;
}
/****************************************************/
/* 函数名  whileEntry				    */
/* 功  能  循环入口部分的处理函数		    */
/* 说  明					    */
/****************************************************/
void whileEntry(CodeFile code)
{
    LoopInfo infoItem = new LoopInfo();

    /*外提标志初始化为可以外提标志1*/
    infoItem.state = 1;
    /*此循环在变量定值表的入口*/
    infoItem.varDef = TotalNum;
    /*循环入口指针*/
    infoItem.whileEntry = code;
    /*循环出口此处不能确定*/
    infoItem.whileEnd = null;
    /*循环信息表压栈*/
    PushLoop(infoItem);
}
/****************************************************/
/* 函数名  call					    */
/* 功  能  遇到过程调用语句的特别处理		    */
/* 说  明  所有包含此调用语句的循环都不能做不变式   */
/*	   外提					    */
/****************************************************/
void call(CodeFile code)
{ 
    /*所有打开着的循环均为不可外提状态,是这些循环信息中的
      State取0*/
    LoopStack Item = loopTop;
  
    while (Item!=null)
    { 
        Item.loopInfo.state = 0;
        Item = Item.under;
    }
}
/****************************************************/
/* 函数名  whileEnd				    */
/* 功  能  循环出口部分的处理函数		    */
/* 说  明					    */
/****************************************************/
void whileEnd(CodeFile code)
{
    /*循环信息栈的栈顶*/
    LoopStack Item = loopTop;

    /*可以外提*/
    if (Item.loopInfo.state==1)
    {   
	/*填写循环出口位置的引用*/
	loopTop.loopInfo.whileEnd = code;
	/*找到循环入口*/
        CodeFile entry  = loopTop.loopInfo.whileEntry;
        /*循环外提处理部分*/
        LoopOutside(entry);
    }

    /*弹循环信息栈,此层循环处理结束*/
    PopLoop();
}

/****************************************************/
/* 函数名  LoopOutside				    */
/* 功  能  循环外提处理函数			    */
/* 说  明					    */
/****************************************************/
void LoopOutside(CodeFile entry)
{
    /*外提的位置,为循环入口位置*/
    CodeFile place = entry;
    /*当前处理代码,注:跳过循环开始标号语句*/
    CodeFile code =  entry.next;
   
    /*取循环信息栈顶指针*/
    LoopStack Item = loopTop;
    /*取得本层循环的出口位置*/
    CodeFile end = Item.loopInfo.whileEnd;
    /*取得本层循环的变量信息表*/
    int head = Item.loopInfo.varDef;
    int present1, present2;

    /*用于跳过内层循环*/
    int Level = 0;

    /*循环检查每条代码是否可以外提,直到此层循环结束*/
    while (code!=end)
    {   
	if (code.codeR.codekind.equals("WHILESTART"))
	    Level++;	
	else if (code.codeR.codekind.equals("ENDWHILE"))  
	    Level--;
	else if ((code.codeR.codekind.equals("ADD"))||(code.codeR.codekind.equals("SUB"))||(code.codeR.codekind.equals("MULT"))||(code.codeR.codekind.equals("AADD")))	
        {
	    /*跳过内层循环*/
	    if (Level==0)
	    {
		present1 = SearchTable(code.codeR.arg1,head);
		present2 = SearchTable(code.codeR.arg2,head);
		/*两个分量都不在变量定值标号中,可以外提*/
		if ((present1<0)&&(present2<0))
		{  
                    /*操作结果也是不变量,故若在表中,从表中删除*/
		    DelItem(code.codeR.arg3,head);
		    /*外提*/
		    /*在当前位置,删除此代码*/
		    CodeFile formerCode = code.former;
		    CodeFile nextCode = code.next;
		    formerCode.next = nextCode;
		    nextCode.former = formerCode;

		    /*将代码加入到应外提的位置*/
		    CodeFile fplace = place.former;
		    fplace.next  = code;
		    code.former = fplace;
  		    code.next = place;
		    place.former = code;

		    /*回到当前位置处,准备处理下一条语句*/
		    code = formerCode;					
		}
		else
		    /*否则,将变量定值加入当前变量定值表中*/ 
		    AddTable(code.codeR.arg3);
	    }
	}
        /*检查下一条语句*/
	code = code.next;
    }
}
/****************************************************/
/* 函数名  SearchTable				    */
/* 功  能  循环变量定值表查找函数		    */
/* 说  明  参数head指明了本层循环的变量定值在表中的 */
/*         起始位置,arg表示要查找的变量,返回变量   */
/*	   在表中的位置,若不存在返回值为-1	    */
/****************************************************/
int SearchTable(ArgRecord arg,int head)
{
    /*初始化为负数,不再表中*/
    int present = -1 ;

    if (arg.form.equals("AddrForm"))
    {   
	int level = arg.midAttr.addr.dataLevel;
	int off = arg.midAttr.addr.dataOff;
	/*注:临时变量和源变量都可以通过比较层数和偏移看是否存在
	  于表中*/
	for (int i=head;i<TotalNum;i++)
        {
	    if ((varTable[i].midAttr.addr.dataLevel==level)
&&(varTable[i].midAttr.addr.dataOff==off))
	    {	
                present = i;
		break;
	    }
        }
    }   
    return present;
}
/****************************************************/
/* 函数名  DelItem				    */
/* 功  能  删除变量定值表中此项			    */
/* 说  明					    */
/****************************************************/
void DelItem(ArgRecord arg,int head)
{
    /*调用函数查找变量定值表*/
    int present = SearchTable(arg,head);
    /*若在表中,则删除*/
    if (present!=-1)
    {   
        for (int i=present;i<TotalNum;i++)
            varTable[i] =varTable[i+1];
        TotalNum--;
    }
}
/****************************************************/
/* 函数名  AddTable			            */
/* 功  能  将被定值的变量填入变量定值表		    */
/* 说  明					    */
/****************************************************/
void AddTable(ArgRecord arg)
{  
    /*若不在循环中,则从头查表,以免表中重复填入相同的变量*/
    int head = 0;
    /*若在循环中,则只要在当前循环层没有重复定义即可*/
    if (loopTop!=null)
	head = loopTop.loopInfo.varDef;

    int present = SearchTable(arg,head);
    /*表中没有,则添加*/
    if (present==-1)
    {
       varTable[TotalNum] = arg;
       TotalNum = TotalNum+1;
    }
}
/****************************************************/
/* 函数名  PushLoop			            */
/* 功  能  循环信息栈的压栈实现过程		    */
/* 说  明					    */
/****************************************************/
void PushLoop(LoopInfo t)
{
    LoopStack p = new LoopStack();
    p.loopInfo = t;
    p.under=loopTop;
    loopTop = p;
    loopStackEmpty = false;
}
/****************************************************/
/* 函数名  PopLoop			            */
/* 功  能  循环信息栈的弹栈实现过程		    */
/* 说  明					    */
/****************************************************/
LoopInfo PopLoop()
{     
    LoopStack p;
    LoopInfo backpointer;
    p = loopTop;
    backpointer = p.loopInfo;
    loopTop=loopTop.under;
    if (loopTop==null)
	loopStackEmpty = true;

    return backpointer;
}
}


/********************************************************************/
/* 类  名 AnalYuyi	                                            */
/* 功  能 总程序的处理					            */
/* 说  明 建立一个类,处理总程序                                    */
/********************************************************************/
class AnalYuyi
{
/* SCOPESIZE为符号表scope栈的大小*/
int SCOPESIZE = 1000;

/*scope栈*/
SymbTable scope[]=new SymbTable[SCOPESIZE];

/*记录当前层数*/
int  Level=-1;
/*记录当前偏移;*/
int  Off;
/*记录主程序的displayOff*/
int mainOff;
/*记录当前层的displayOff*/
int savedOff;

/*注:域成员的偏移量从0开始。*/
int fieldOff = 0;

/*记录主程序display表的偏移量*/
int StoreNoff;

/*根据目标代码生成需要,initOff应为AR首地址sp到形参变量区的偏移7*/	
int initOff=7;

/*分别指向整型,字符型,bool类型的内部表示*/
TypeIR  intptr = new TypeIR();
TypeIR  charptr = new TypeIR();
TypeIR  boolptr = new TypeIR();

/*错误追踪标志*/
boolean Error=false;
boolean Error1=false;
String yerror;
String serror;
TreeNode yuyiTree;

AnalYuyi(String s)
{
    Recursion r=new Recursion(s);
    Error1=r.Error;
    if (Error1)
        serror=r.serror;
    else
    {
        yuyiTree=r.yufaTree;
        Analyze(yuyiTree);
    }
}
/****************************************************/
/****************************************************/
/****************************************************/
/* 函数名  Analyze				    */
/* 功  能  语义分析主函数        		    */
/* 说  明  从语法树的根节点开始,进行语义分析       */	
/****************************************************/
void Analyze(TreeNode t)	
{ 
    TreeNode p = null;
    TreeNode pp = t;

    /*建立一个新的符号表,开始语义分析*/
    CreatSymbTable();

    /*调用类型内部表示初始化函数*/
    initiate();

    /*语法树的声明节点*/
    p=t.child[1];
    while (p!=null) 
    {
        if(p.nodekind.equals("TypeK") ) 
	    TypeDecPart(p.child[0]); 
        else if(p.nodekind.equals("VarK") )  
	    VarDecPart(p.child[0]);  

⌨️ 快捷键说明

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