📄 compiler.cpp
字号:
newNode=new FlagsMap();
strcpy(newNode->strFlagName,flag);
newNode->uFlagAddr=uAddr;
newNode->next=NULL;
_flagsMap=newNode;
}
else
{
while(p!=NULL)
{
if(strcmp(flag,p->strFlagName)==0)
{
m_uErrorNum++;
m_strError.Format("fatal error:\'%s\' redefine",flag);
ReportState(m_strError);
return;
}
else
{
q=p;
p=p->next;
}
}
newNode=new FlagsMap();
strcpy(newNode->strFlagName,flag);
newNode->uFlagAddr=uAddr;
newNode->next=NULL;
q->next=newNode;
}
}
void CCompiler::AddVariable(char *varName, UNSHORT uAddr,WORD wVar)
{
VariableInfo* p=_varInfo;
VariableInfo* newNode=NULL;
VariableInfo* q=NULL;
if(p==NULL)
{
newNode=new VariableInfo();
strcpy(newNode->strVarName,varName);
newNode->uVarAddr=uAddr;
newNode->wValue=wVar;
newNode->next=NULL;
_varInfo=newNode;
}
else
{
while(p!=NULL)
{
if(strcmp(varName,p->strVarName)==0)
{
m_uErrorNum++;
m_strError.Format("fatal error:\'%s\' redefine",varName);
ReportState(m_strError);
return;
}
else
{
q=p;
p=p->next;
}
}
newNode=new VariableInfo();
strcpy(newNode->strVarName,varName);
newNode->uVarAddr=uAddr;
newNode->wValue=wVar;
newNode->next=NULL;
q->next=newNode;
}
}
BOOL CCompiler::SecondScan(char* asmSourceName,char *DFYexeFile)
{
DWORD dwCode=0;
DWORD dwOperandNum=0;
DWORD dwInstructionCode=0;
DWORD dwSrcRegCode=0;
DWORD dwDstRegCode=0;
DWORD dwImmNum=0;
BOOL bStart=FALSE;
BOOL bData=FALSE;
BOOL bTemp=0;
BOOL bTemp2=0;
m_instructionCode=new WORD[m_wDFYexeFileSize];
m_LineToAddr=new MapFileBody[m_uCodeLineNum];
ReportState("second scaning...");
char* word=new char[20];
UNSHORT uAddr=0;//keep the current instruction address
UNSHORT uLine=0;//the line number
CString str;
ifstream asmFile;
char* pBuf=new char[200];;
asmFile.open(asmSourceName);
if(!asmFile)
{
str.Format("fatal error:can't open the file %s or the file is not exist",asmSourceName);
m_uErrorNum+=1;
ReportState(str);
delete[] word;
delete[] pBuf;
return FALSE;
}
while(!asmFile.eof())
{
//initlizing Code...
dwCode=0;
dwOperandNum=0;
dwInstructionCode=0;
dwSrcRegCode=0;
dwDstRegCode=0;
dwImmNum=0;
uLine+=1;//line number +1
CString str;
str.Format("anylize line:%d...",uLine);
ReportState(str);
asmFile.getline(pBuf,200);
word=GetWordFromLine(0,pBuf);
if(IsEmpty(word))//if the line is empty
{
m_LineToAddr[uLine-1].bIsCode=FALSE;
m_LineToAddr[uLine-1].uAddr=-1;
continue;
}//end:if the line is empty
if(IsComment(word))//if the line is comment
{
m_LineToAddr[uLine-1].bIsCode=FALSE;
m_LineToAddr[uLine-1].uAddr=-1;
continue;
}//end:if the line is comment
//after the firstscan the first word of evey line are legal
if(!bStart)//if haven't find the 'START',//the first
{ //the first legal word must be "START"
if(strcmp(word,"START")==0)
{
m_LineToAddr[uLine-1].bIsCode=FALSE;
m_LineToAddr[uLine-1].uAddr=-1;
bStart=TRUE;
word=GetWordFromLine(1,pBuf);
if(IsComment(word)||IsEmpty(word))
{
continue;
}//if have any code but comment
else
{
str.Format("fatal error:Line %d, after 'START' should be comment or NULL" ,uLine);
m_uErrorNum+=1;
ReportState(str);
}
}
else//if the first legal word is not "START"
{
m_uErrorNum+=1;
ReportState("fatal error:cant't find 'START' ");
delete[] word;
delete[] pBuf;
return FALSE;
}
}//end:have find 'START'
else//have been find the 'START'
{
if(strcmp(word,"END")==0)//if go to the end
{
word=GetWordFromLine(1,pBuf);
if(IsComment(word)||IsEmpty(word))
{
break;
}
else
{
m_uErrorNum+=1;
str.Format("fatal error:Line %d,the word behind 'END' should be comment or NULL",uLine);
ReportState(str);
delete[] word;
delete[] pBuf;
return FALSE;
}
}//end: if go to the end
if(strcmp(word,"DATA")==0)//if into the DATA segment
{
bData=TRUE;
word=GetWordFromLine(1,pBuf);
m_LineToAddr[uLine-1].bIsCode=FALSE;
m_LineToAddr[uLine-1].uAddr=uAddr;
if(IsComment(word)||IsEmpty(word))
{
continue;
}
else
{
str.Format("fatal error:Line %d,the word after 'DATA' should be comment or NULL",uLine);
m_uErrorNum+=1;
ReportState(str);
continue;
}
}
if(!bData)//if haven't into the segment
{
m_LineToAddr[uLine-1].bIsCode=TRUE;
m_LineToAddr[uLine-1].uAddr=uAddr;
if(!IsInstruction(word))//the first word is flag
{
word=GetWordFromLine(1,pBuf);
if(!IsInstruction(word))
{
str.Format("fatal error:Line %d,syntax error,the second word should be Instruction",uLine);
m_uErrorNum+=1;
ReportState(str);
continue;
}
else//if the second word is instruction
{
bTemp=IsInstruction(word);//get the instruction code
dwInstructionCode=(DWORD)((bTemp&0x0000007F)<<25);
//if the instrudction is LD-ST ADD-CPL or INPORT,OUTPORT
//may have three operands
if(bTemp>0 && bTemp<=15&&bTemp!=3||(bTemp==25)||(bTemp==26))
{
word=GetWordFromLine(2,pBuf);//anylize the third word
if(bTemp2=IsRegister(word))
{
dwSrcRegCode=DWORD(((bTemp2-1)&0X00000007)<<16);
word=GetWordFromLine(3,pBuf);
if(IsNumber(word))//imm data, 3 operand
{
dwOperandNum=0x00C00000;//3 operands
dwImmNum=DWORD(0X0000FFFF&atoi(word));//get the imm data
word=GetWordFromLine(4,pBuf);//anylize the fifth word
if(bTemp2=IsRegister(word))//if it is a register
{
dwDstRegCode=DWORD(((bTemp2-1)&0X00000007)<<19);
word=GetWordFromLine(5,pBuf);
if(!IsComment(word)&&!IsEmpty(word))
{
str.Format("fatal error: Line %d, the sixth word should be comment or NULL",uLine);
m_uErrorNum+=1;
ReportState(str);
}
}//end: the third operand is a register
else//if the third operand is not a register
{
str.Format("fatal error: Line %d, the third operand should be register",uLine);
m_uErrorNum+=1;
ReportState(str);
}
}//end:3 operands
else if(IsLegal(word))//variable, 2 operand
{
dwOperandNum=0x00800000;//2 operands
UNSHORT uVarAddr;
WORD wValue;
if(!FindVariable(word,uVarAddr,wValue))
{
str.Format("fatal error: Line %d, \'%s\'is not define or is a key word",uLine,word);
m_uErrorNum++;
ReportState(str);
}
else
{
dwImmNum=DWORD(uVarAddr&0x0000FFFF);
}
}//end:the second operand is a variable
else
{
str.Format("fatal error: Line %d,illegal identifier",uLine);
m_uErrorNum++;
ReportState(str);
}
}//end:the first operand is register
else//the first operand is not register
{
str.Format("fatal error: Line %d,the first operand should be register",uLine);
m_uErrorNum++;
ReportState(str);
}//end:the firs operand is not register
}//end:the instruction is LD-ST,ADD-CPL,INPORT OUTPORT
else if(bTemp==3)//the instruction is LEA
{
word=GetWordFromLine(2,pBuf);
if(bTemp2=IsRegister(word))
{
dwSrcRegCode=DWORD(((bTemp2-1)&0X00000007)<<16);
word=GetWordFromLine(3,pBuf);
if(IsNumber(word))
{
dwImmNum=0X0000FFFF&atoi(word);
word=GetWordFromLine(4,pBuf);
if(bTemp=IsRegister(word))
{
dwOperandNum=0X00C00000;
dwDstRegCode=DWORD(((bTemp-1)&0x00000007)<<16);
word=GetWordFromLine(5,pBuf);
if(!IsComment(word)&&!IsEmpty(word))
{ str.Format("fatal error: Line %d ,the sixth word should be a commet or NULL",uLine);
m_uErrorNum++;
ReportState(str);
}
}//end:if the fifth word is a register
else//if the fifth word is not a register
{
dwOperandNum=0X00800000;
if((!IsComment(word))&&(!IsEmpty(word)))
{
str.Format("fatal error: Line %d,the forth third operand should be a register or NULL",uLine);
m_uErrorNum++;
ReportState(str);
}
}//end:if the fifth word is not a register
}//end:if is a number
else
{
//error hadle:should be a number
str.Format("fatal error: Line %d,the second operand should be a imm number",uLine);
m_uErrorNum++;
ReportState(str);
}//end:if is not a number
}//end:if is a register
else//if is not a register
{
//error handle:should be a register
str.Format("fatal error: Line %d,the first operand should be a register",uLine);
m_uErrorNum++;
ReportState(str);
}//end:is not a register
}//end:the instrucion is LEA
else if(bTemp>=16&&bTemp<=20||bTemp==23)//the instruction is JPZ-JZE
{
word=GetWordFromLine(2,pBuf);
if(IsLegal(word))
{
UNSHORT uFlagAddr=0;
if(FindFlag(word,uFlagAddr))
{
dwImmNum=uFlagAddr;
word=GetWordFromLine(3,pBuf);
if(bTemp2=IsRegister(word))
{
dwOperandNum=0X00800000;//2 operands
word=GetWordFromLine(4,pBuf);
dwSrcRegCode=DWORD(((bTemp2-1)&0X00000007)<<16);
if(!IsComment(word)&&!IsEmpty(word))
{
str.Format("fatal error: Line %d ,the fifth word should be a comment or NULL",uLine);
m_uErrorNum++;
ReportState(str);
}
}//end if the second operand is a register
else//if the second operand is not a register
{
dwOperandNum=0X00400000;//1 operand
if(!IsComment(word)&&!IsEmpty(word))
{
str.Format("fatal error: Line %d ,the third word should be a comment or NULL",uLine);
m_uErrorNum++;
ReportState(str);
}//end:if is not a comment and also not NULL
}//end:if the second operand is not a register
}//end:find flag succeed
else//find flag failed
{
//error hadle: find flag failed
str.Format("fatal error: Line %d ,indentifier \'%s\' not defined",uLine,word);
m_uErrorNum++;
ReportState(str);
}//end:find flag failed
}//end:if is a legal word
else//is not a legal word
{
//error hadle:the word is illegal
str.Format("fatal error: Line %d ,\'%s\' is illegal identifier",uLine,word);
m_uErrorNum++;
ReportState(str);
}//end:is not a legal word
}//end:the instruction is JPZ-JZE,CALL
else if(bTemp==21)//if the instruction is PUSH
{
word=GetWordFromLine(2,pBuf);
if(IsLegal(word))
{
UNSHORT uVarAddr=0;
WORD wData=0;
if(FindVariable(word,uVarAddr,wData))
{
dwImmNum=uVarAddr;
word=GetWordFromLine(3,pBuf);
if(bTemp2=IsRegister(word))
{
dwOperandNum=0X00800000;//2 operands
word=GetWordFromLine(4,pBuf);
dwSrcRegCode=DWORD(((bTemp2-1)&0X00000007)<<16);
if(!IsComment(word)&&!IsEmpty(word))
{
str.Format("fatal error: Line %d ,the fifth word should be a comment or NULL",uLine);
m_uErrorNum++;
ReportState(str);
}
}//end if the second operand is a register
else//if the second operand is not a register
{
dwOperandNum=0X00400000;//1 operand
if(!IsComment(word)&&!IsEmpty(word))
{
str.Format("fatal error: Line %d ,the third word should be a comment or NULL",uLine);
m_uErrorNum++;
ReportState(str);
}//end:if is not a comment and also not NULL
}//end:if the second operand is not a register
}//end:find variable succeed
else//find variable failed
{
//error hadle: find flag failed
str.Format("fatal error: Line %d ,indentifier \'%s\' not defined",uLine,word);
m_uErrorNum++;
ReportState(str);
}//end:find variable failed
}//end:if is a legal word
else//is not a legal word
{
//error hadle:the word is illegal
str.Format("fatal error: Line %d ,\'%s\' is illegal identifier",uLine,word);
m_uErrorNum++;
ReportState(str);
}//end:is not a legal word
}//end:the instruciton is PUSH
else if(bTemp==22)//the instruction is POP
{
dwOperandNum=0X00400000;//1 operand
word=GetWordFromLine(2,pBuf);
if(bTemp2=IsRegister(word))
{
dwSrcRegCode=DWORD(((bTemp2-1)&0X00000007)<<16);
word=GetWordFromLine(3,pBuf);
if(!IsComment(word)&&!IsEmpty(word))
{
str.Format("fatal error: Line %d ,the forth word should be comment or NULL",uLine);
m_uErrorNum++;
ReportState(str);
}
}//end:if the first operand is a register
else
{
//error hanle: the first operand should be register
str.Format("fatal error: Line %d,the first operand should be register",uLine);
m_uErrorNum++;
ReportState(str);
}//end:the first operand is not register
}//end:the instruction is POP
else if(bTemp==24||bTemp==27||bTemp>=29&&bTemp<=31)//the instuction is EXIT,CLI,STI,IRET,RET
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -