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

📄 compiler.cpp

📁 这个也是我们的毕业设计课题
💻 CPP
📖 第 1 页 / 共 3 页
字号:
						{
							dwOperandNum=0X00000000;
							word=GetWordFromLine(2,pBuf);
							if(!IsComment(word)&&!IsEmpty(word))
							{
								//error hadle: the third word should be comment or NULL
								str.Format("fatal error: Line %d ,the third word should be comment or NULL",uLine);
								m_uErrorNum++;
								ReportState(str);
							}

						}//end:the instruction is EXIT,CLI,STI,IRET,RET
						else if(bTemp==28)//the instruction is INTR
						{
							dwOperandNum=0X00400000;
							word=GetWordFromLine(2,pBuf);
							if(IsNumber(word))
							{
								dwImmNum=atoi(word)&0X0000FFFF;
								word=GetWordFromLine(3,pBuf);
								if(!IsComment(word)&&!IsEmpty(word))
								{
									str.Format("fatal error: Line %d ,the first operand should be a number",uLine);
									m_uErrorNum++;
									ReportState(str);
								}
							}//end:if the first operand is a number
							else//the first operand is not a number
							{
								//error handle:the first operand should be a number
								str.Format("fatal Error: Line %d ,the first operand should be a number",uLine);
								m_uErrorNum++;
								ReportState(str);
							}//end:the first operand should be a number
						}//end:the instruction is INTR
					}//end:if the second word is a instruction
				}//end:the first word is flag
				else//the first 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(1,pBuf);//anylize the third word
						if(bTemp=IsRegister(word))
						{
							dwSrcRegCode=DWORD(((bTemp-1)&0X00000007)<<16);
							word=GetWordFromLine(2,pBuf);
							if(IsNumber(word))//imm data, 3 operand
							{
								dwOperandNum=0x00C00000;//3 operands
								dwImmNum=DWORD(0X0000FFFF&atoi(word));//get the imm data
								word=GetWordFromLine(3,pBuf);//anylize the fifth word
								if(bTemp2=IsRegister(word))//if it is a register
								{
									dwDstRegCode=DWORD(((bTemp2-1)&0X00000007)<<19);	
									word=GetWordFromLine(4,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(1,pBuf);//get the first operand
						if(bTemp2=IsRegister(word))
						{
							dwSrcRegCode=DWORD(((bTemp2-1)&0X00000007)<<16);
							word=GetWordFromLine(2,pBuf);
							if(IsNumber(word))
							{
								dwImmNum=0X0000FFFF&atoi(word);
								word=GetWordFromLine(3,pBuf);
								if(bTemp=IsRegister(word))
								{
									dwDstRegCode=DWORD(((bTemp-1)&0x00000007)<<16);
									dwOperandNum=0X00C00000;
									word=GetWordFromLine(4,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 forth word is a register
								else//if the forth word is not a register
								{
									dwOperandNum=0X00800000;//2 operands
									if((!IsComment(word))&&(!IsEmpty(word)))
									{
										str.Format("fatal error: Line %d,the 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 the first operandis 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(1,pBuf);
						if(IsLegal(word))
						{
							UNSHORT uFlagAddr=0;
							if(FindFlag(word,uFlagAddr))
							{
								dwImmNum=uFlagAddr;
								word=GetWordFromLine(2,pBuf);
								if(bTemp2=IsRegister(word))
								{
									dwSrcRegCode=DWORD(((bTemp2-1)&0X00000007)<<16);
									dwOperandNum=0X00800000;//2 operands
									word=GetWordFromLine(3,pBuf);
									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)//the instruction is PUSH
					{
						word=GetWordFromLine(1,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:if the instruction is PUSH
					else if(bTemp==22)//the instruction is POP
					{
						dwOperandNum=0X00400000;//1 operand
						word=GetWordFromLine(1,pBuf);
						if(bTemp2=IsRegister(word))
						{
							dwSrcRegCode=DWORD(((bTemp2-1)&0X00000007)<<16);
							word=GetWordFromLine(2,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:thei 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
					{
						dwOperandNum=0X00000000;
						word=GetWordFromLine(1,pBuf);
						if(!IsComment(word)&&!IsEmpty(word))
						{
							//error hadle: the third word should be comment or NULL
							str.Format("fatal error: Line %d ,the third word should be comment or NULL",uLine);
							m_uErrorNum++;
							ReportState(str);
						}
					}//end:the instruction is EXIT,CLI,STI,IRET,RET
					else if(bTemp==28)//the instruction is INTR
					{
						dwOperandNum=0X00400000;//1 operand
						word=GetWordFromLine(2,pBuf);
						if(IsNumber(word))
						{
							dwImmNum=atoi(word)&0X0000FFFF;
							word=GetWordFromLine(2,pBuf);
							if(!IsComment(word)&&!IsEmpty(word))
							{
							str.Format("fatal error: Line %d ,the first operand should be a number",uLine);
							m_uErrorNum++;
							ReportState(str);
							}
						}//end:if the first operand is a number
						else//the first operand is not a number
						{
							//error handle:the first operand should be a number
							str.Format("fatal Error: Line %d ,the first operand should be a number",uLine);
							m_uErrorNum++;
							ReportState(str);
						}//end:the first operand should be a number
					}//end:the instruction is INTR				
				}//end:the first word is instruction
				dwCode=dwInstructionCode|dwSrcRegCode|dwSrcRegCode|dwImmNum|dwOperandNum;
				//save the instruction's higt 16 bit
				m_instructionCode[uAddr]=WORD((dwCode&0XFFFF0000)>>16);
				//save the instruction's low 16 bit
				m_instructionCode[uAddr+1]=WORD(dwCode&0X0000FFFF);
				uAddr+=2;
				continue;
			}//end: haven't into the data segment
			else//haven into the data segment
			{
				//if into the data segment
				word=GetWordFromLine(1,pBuf);
				if(strcmp(word,"DC")==0)
				{
					word=GetWordFromLine(2,pBuf);
					if(IsNumber(word))
					{
						m_LineToAddr[uLine-1].bIsCode=FALSE;
						m_LineToAddr[uLine-1].uAddr=uAddr;
						m_instructionCode[uAddr]=atoi(word);
						uAddr+=1;
						word=GetWordFromLine(3,pBuf);
						if(!IsComment(word)&&!IsEmpty(word))
						{
							str.Format("fatal error: Line %d, the forth word should comment or NULL",uLine);
							m_uErrorNum++;
							ReportState(str);
						}
					}
					else
					{
						str.Format("fatal error: Line %d,the variable value haven't initialized",uLine);
						m_uErrorNum+=1;
						ReportState(str);
					}//end:IsNumber
				}//end:strcmp
				else
				{
					str.Format("fatal error: Line %d,the second word should be \'DC\'",uLine);
					m_uErrorNum+=1;
					delete[] word;
					delete[] pBuf;
					return FALSE;
				}
			}//end have into the segment
		}//end: the 'START' have been found
	}//end:while
	asmFile.close();
	if(m_uErrorNum==0)
	{
		//generating map file...
		ReportState("generatint .map file...");
		int nSize=strlen(asmSourceName);
		char* mapFileName=new char[nSize+1];
		for(int ii=0;ii<nSize-3;ii++)
			mapFileName[ii]=asmSourceName[ii];
		mapFileName[nSize-3]='m';
		mapFileName[nSize-2]='a';
		mapFileName[nSize-1]='p';
		mapFileName[nSize]='\0';
		ofstream mapfile;
		mapfile.open(mapFileName,ios::out|ios::binary);
		if(!mapfile)//can't create map file
		{
			str.Format("can't create the file:\'%s'" ,mapFileName);
			ReportState(str);
		}//end:can't create map file
		else//can create map file
		{
			str.Format("outport information to file\'%s\'",mapFileName);
			ReportState(str);
			//outport the header to map file
			MapFileHeader header;
			header.wFileVersion=1;
			header.wRecordNum=m_uCodeLineNum;
			mapfile<<' '<<header.wFileVersion<<' '<<header.wRecordNum<<' ';
			//outport the body to map file
			for(int jj=0;jj<m_uCodeLineNum;jj++)
				mapfile<<' '<<m_LineToAddr[jj].bIsCode<<' '<<m_LineToAddr[jj].uAddr;
			mapfile.close();
			delete[] m_LineToAddr;
		}//end:can create map file
		
		//generating code file...
		str.Format("generating code...");
		ReportState(str);
		ofstream exefile(DFYexeFile,ios::out|ios::binary);
		if(!exefile)
		{
			str.Format("fatal error:can't create the file \'%s\'",DFYexeFile);
			m_uErrorNum++;
			ReportState(str);
		}
		else
		{
			str.Format("outport code to file: %s",DFYexeFile);
			ReportState(str);
			DFYexeFileHeader header;
			header.wFileVersion=1;
			header.wFileSize=m_wDFYexeFileSize;
			header.wCodeSegSize=m_uCodeSegSize;
			header.wDataSegSize=m_uDataSegSize;
			header.wDS=0;
			header.wES=0;
			header.wSP=m_wDFYexeFileSize+10;
			header.wReserved=0;
			exefile<<hex<<' '<<header.wFileVersion<<' '<<header.wFileSize<<' '
				<<header.wCodeSegSize<<' '<<header.wDataSegSize<<' '
				<<header.wDS<<' '<<header.wES<<' '<<header.wSP<<' '<<header.wReserved;
			for(int i=0;i<m_wDFYexeFileSize;i++)
			{

				exefile<<hex<<' '<<m_instructionCode[i]<<' ';
			}
			exefile.close();
		}
		
		delete[] m_instructionCode;
		delete[] word;
		delete[] pBuf;
		ReportState("second scan succeed");
		return TRUE;
		
	}
	else
	{
		str.Format("%d errors",m_uErrorNum);
		ReportState(str);
		delete[] word;
		delete[] pBuf;
		return FALSE;
	}

}


⌨️ 快捷键说明

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