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

📄 pass2.c

📁 < 虚拟机设计与实现> 的source code, linux版本
💻 C
📖 第 1 页 / 共 4 页
字号:

/*------------------------------------------------------------------*/

/* Instruction 3Integer Registers */

void I3R(U1 opcode, struct Token * tptr) 
{
	struct Token t;
	U1 bRet;
	U1 nBYTES;

	nBYTES = 4;

	sprintf(lineNumber, "%lu", (*tptr).line);
	strcpy(listingfile_line, (*tptr).text);
	strcat(listingfile_line, " ");
	encoded[0] = opcode;

	bRet = match(&t, TOK_INT_REG);
	if (bRet == TRUE)
	{
		strcat(listingfile_line, t.text);
		encoded[1] = (U1)t.val;
	}
	else
	{ 
        ERROR2("I3R(): line %d, invalid operand (%s)\n", (*tptr).line, (*tptr).text);
        return;
	}
					
	bRet = match(&t, TOK_COMMA);
	if (bRet == TRUE)
	{ 
		strcat(listingfile_line, t.text); 
	}
	else
	{ 
        ERROR2("I3R(): line %d, invalid opcode (%s)\n", (*tptr).line, (*tptr).text);
        return;
	}
					
	bRet = match(&t, TOK_INT_REG);
	if (bRet == TRUE)
	{
		strcat(listingfile_line, t.text);
		encoded[2] = (U1)t.val;
	}
	else
	{ 
        ERROR2("I3R(): line %d, invalid operand (%s)\n", (*tptr).line, (*tptr).text);
        return;
	}
	
	bRet = match(&t, TOK_COMMA);
	if (bRet == TRUE)
	{ 
		strcat(listingfile_line, t.text); 
	}
	else
	{ 
        ERROR2("I3R(): line %d, invalid opcode (%s)\n", (*tptr).line, (*tptr).text);
        return;
	}

	bRet = match(&t, TOK_INT_REG);
	if (bRet == TRUE)
	{
		strcat(listingfile_line, t.text);
		encoded[3] = (U1)t.val;
	}
	else
	{ 
        ERROR2("I3R(): line %d, invalid operand (%s)\n", (*tptr).line, (*tptr).text);
        return;
	}

	bRet = match(&t, TOK_NO_MORE);
	if (bRet != TRUE)
	{ 
        ERROR2("I3R(): line %d, invalid opcode (%s)\n", (*tptr).line, (*tptr).text);
        return;
	}
	commitToFiles(nBYTES);
	bytePosPass2 = bytePosPass2 + nBYTES;
	return;

}/*end I3R*/

/*------------------------------------------------------------------*/

void I4R(U1 opcode, struct Token * tptr) /* Instruction 4Integer Registers */
{
	struct Token t;
	U1 bRet;
	U1 nBYTES;
	
	nBYTES = 5;
	sprintf(lineNumber, "%lu", (*tptr).line);
	strcpy(listingfile_line, (*tptr).text);
	strcat(listingfile_line, " ");
	encoded[0] = opcode;

	bRet = match(&t, TOK_INT_REG);
	if (bRet == TRUE)
	{
		strcat(listingfile_line, t.text);
		encoded[1] = (U1)t.val;
	}
	else
	{ 
        ERROR2("I4R(): line %d, invalid operand (%s)\n", (*tptr).line, (*tptr).text);
        return;
	}
					
	bRet = match(&t, TOK_COMMA);
	if (bRet == TRUE)
	{ 
		strcat(listingfile_line, t.text); 
	}
	else
	{ 
        ERROR2("I4R(): line %d, invalid opcode (%s)\n", (*tptr).line, (*tptr).text);
        return;
	}
					
	bRet = match(&t,TOK_INT_REG);
	if (bRet == TRUE)
	{
		strcat(listingfile_line, t.text);
		encoded[2] = (U1)t.val;
	}
	else
	{ 
        ERROR2("I4R(): line %d, invalid operand (%s)\n", (*tptr).line, (*tptr).text);
        return; 
	}
	
	bRet = match(&t, TOK_COMMA);
	if (bRet == TRUE)
	{ 
		strcat(listingfile_line, t.text); 
	}
	else
	{ 
        ERROR2("I4R(): line %d, invalid opcode (%s)\n", (*tptr).line, (*tptr).text);
        return;
	}

	bRet = match(&t, TOK_INT_REG);
	if (bRet == TRUE)
	{
		strcat(listingfile_line, t.text);
		encoded[3] = (U1)t.val;
	}
	else
	{ 
        ERROR2("I4R(): line %d, invalid operand (%s)\n", (*tptr).line, (*tptr).text);
        return;
	}

	bRet = match(&t, TOK_COMMA);
	if (bRet == TRUE)
	{ 
		strcat(listingfile_line, t.text); 
	}
	else
	{ 
        ERROR2("I4R(): line %d, invalid opcode (%s)\n", (*tptr).line, (*tptr).text);
        return;
	}

	bRet = match(&t, TOK_INT_REG);
	if (bRet == TRUE)
	{
		strcat(listingfile_line, t.text);
		encoded[4] = (U1)t.val;
	}
	else
	{ 
        ERROR2("I4R(): line %d, invalid operand (%s)\n", (*tptr).line, (*tptr).text);
        return;
	}

	bRet = match(&t, TOK_NO_MORE);
	if (bRet != TRUE)
	{ 
        ERROR2("I4R(): line %d, invalid opcode (%s)\n", (*tptr).line, (*tptr).text);
        return; 
	}
	commitToFiles(nBYTES);
	bytePosPass2 = bytePosPass2 + nBYTES;
	return;
	
}/*end I4R*/

/*-----------------------------------------------------------------*/

void IFC(U1 opcode, struct Token *tptr)
{
	struct Token t;
	U1 bRet;
	U1 nBYTES;
	
	nBYTES = 6; /* float = 4 bytes */
	sprintf(lineNumber, "%lu", (*tptr).line);
	strcpy(listingfile_line, (*tptr).text);
	strcat(listingfile_line, " ");
	encoded[0] = opcode;

	bRet = match(&t, TOK_FLT_REG);
	if (bRet == TRUE)
	{
		strcat(listingfile_line, t.text);
		encoded[1] = (U1)t.val;
	}
	else
	{ 
        ERROR2("IFC(): line %d, invalid operand (%s)\n", (*tptr).line, (*tptr).text);
        return; 
	}

	bRet = match(&t, TOK_COMMA);
	if (bRet == TRUE)
	{ 
		strcat(listingfile_line, t.text); 
	}
	else
	{ 
        ERROR2("IFC(): line %d, invalid opcode (%s)\n", (*tptr).line, (*tptr).text);
        return; 
	}

	t = getNextLineToken();
	if (t.type == TOK_FLT_CONST)
	{
		strcat(listingfile_line, t.text);
		floatToBytecode((F4)t.fval, &encoded[2]); 
	}
	else
	{ 
		ERROR2("IFC(): line %lu, expecting float constant %s\n", t.line, t.text);
		return; 
	}

	bRet = match(&t,TOK_NO_MORE);
	if (bRet != TRUE)
	{ 
        ERROR2("IFC(): line %d, invalid opcode (%s)\n", (*tptr).line, (*tptr).text);
        return; 
	}
	commitToFiles(nBYTES);
	bytePosPass2 = bytePosPass2 + nBYTES;
	return;
	
}/*end IFC*/

/*-----------------------------------------------------------------*/

void IFR(U1 opcode, struct Token *tptr)
{
	struct Token t;
	U1 bRet;
	U1 nBYTES;
	
	nBYTES = 3;
	sprintf(lineNumber, "%lu", (*tptr).line);
	strcpy(listingfile_line, (*tptr).text);
	strcat(listingfile_line, " ");
	encoded[0] = opcode;

	bRet = match(&t, TOK_FLT_REG);
	if (bRet == TRUE)
	{
		strcat(listingfile_line, t.text);
		encoded[1] = (U1)t.val;
	}
	else
	{ 
        ERROR2("IFR(): line %d, invalid operand (%s)\n", (*tptr).line, (*tptr).text);
        return; 
	}
					
	bRet = match(&t, TOK_COMMA);
	if (bRet == TRUE)
	{ 
		strcat(listingfile_line, t.text); 
	}
	else
	{ 
        ERROR2("IFR(): line %d, invalid opcode (%s)\n", (*tptr).line, (*tptr).text);
        return; 
	}
					
	bRet = match(&t, TOK_INT_REG);
	if (bRet == TRUE)
	{
		strcat(listingfile_line, t.text);
		encoded[2] = (U1)t.val;
	}
	else
	{ 
        ERROR2("IFR(): line %d, invalid operand (%s)\n", (*tptr).line, (*tptr).text);
        return; 
	}

	bRet = match(&t, TOK_NO_MORE);
	if (bRet != TRUE)
	{ 
        ERROR2("IFR(): line %d, invalid opcode (%s)\n", (*tptr).line, (*tptr).text);
        return;  
	}
	commitToFiles(nBYTES);
	bytePosPass2 = bytePosPass2 + nBYTES;
	return;
	
}/*end IFR*/

/*-----------------------------------------------------------------*/

void IF(U1 opcode, struct Token *tptr)
{
	struct Token t;
	U1 bRet;
	U1 nBYTES;
	
	nBYTES = 2;
	sprintf(lineNumber, "%lu", (*tptr).line);
	strcpy(listingfile_line, (*tptr).text);
	strcat(listingfile_line, " ");
	encoded[0] = opcode;

	bRet = match(&t, TOK_FLT_REG);
	if (bRet == TRUE)
	{
		strcat(listingfile_line, t.text);
		encoded[1] = (U1)t.val;
	}
	else
	{ 
        ERROR2("IF(): line %d, invalid operand (%s)\n", (*tptr).line, (*tptr).text);
        return;  
	}

	bRet = match(&t, TOK_NO_MORE);
	if (bRet != TRUE)
	{ 
        ERROR2("IF(): line %d, invalid opcode (%s)\n", (*tptr).line, (*tptr).text);
        return;  
	}
	commitToFiles(nBYTES);
	bytePosPass2 = bytePosPass2 + nBYTES;
	return;

}/*end IF*/

/*-----------------------------------------------------------------*/

void I2F(U1 opcode, struct Token *tptr)
{
	struct Token t;
	U1 bRet;
	U1 nBYTES;
	
	nBYTES = 3;
	sprintf(lineNumber, "%lu", (*tptr).line);
	strcpy(listingfile_line, (*tptr).text);
	strcat(listingfile_line, " ");
	encoded[0] = opcode;

	bRet = match(&t, TOK_FLT_REG);
	if (bRet == TRUE)
	{
		strcat(listingfile_line, t.text);
		encoded[1] = (U1)t.val;
	}
	else
	{ 
        ERROR2("I2F(): line %d, invalid operand (%s)\n", (*tptr).line, (*tptr).text);
        return;   
	}
					
	bRet = match(&t, TOK_COMMA);
	if (bRet == TRUE)
	{ 
		strcat(listingfile_line, t.text); 
	}
	else
	{ 
        ERROR2("I2F(): line %d, invalid opcode (%s)\n", (*tptr).line, (*tptr).text);
        return;   
	}
					
	bRet = match(&t, TOK_FLT_REG);
	if (bRet == TRUE)
	{
		strcat(listingfile_line, t.text);
		encoded[2] = (U1)t.val;
	}
	else
	{ 
        ERROR2("I2F(): line %d, invalid operand (%s)\n", (*tptr).line, (*tptr).text);
        return;  
	}

	bRet = match(&t, TOK_NO_MORE);
	if (bRet != TRUE)
	{ 
        ERROR2("I2F(): line %d, invalid opcode (%s)\n", (*tptr).line, (*tptr).text);
        return;   
	}
	commitToFiles(nBYTES);
	bytePosPass2 = bytePosPass2 + nBYTES;
	return;

}/*end I2F*/

/*-----------------------------------------------------------------*/

void I3F(U1 opcode, struct Token *tptr)
{
	struct Token t;
	U1 bRet;
	U1 nBYTES;
	
	nBYTES = 4;
	sprintf(lineNumber, "%lu", (*tptr).line);
	strcpy(listingfile_line, (*tptr).text);
	strcat(listingfile_line, " ");
	encoded[0] = opcode;

	bRet = match(&t, TOK_FLT_REG);
	if (bRet == TRUE)
	{
		strcat(listingfile_line, t.text);
		encoded[1] = (U1)t.val;
	}
	else
	{ 
        ERROR2("I3F(): line %d, invalid operand (%s)\n", (*tptr).line, (*tptr).text);
        return;  
	}
					
	bRet = match(&t, TOK_COMMA);
	if (bRet == TRUE)
	{ 
		strcat(listingfile_line, t.text); 
	}
	else
	{ 
        ERROR2("I3F(): line %d, invalid opcode (%s)\n", (*tptr).line, (*tptr).text);
        return; 
	}
					
	bRet = match(&t, TOK_FLT_REG);
	if (bRet == TRUE)
	{
		strcat(listingfile_line, t.text);
		encoded[2] = (U1)t.val;
	}
	else
	{ 
        ERROR2("I3F(): line %d, invalid operand (%s)\n", (*tptr).line, (*tptr).text);
        return;  
	}
	
	bRet = match(&t, TOK_COMMA);
	if (bRet == TRUE)
	{ 
		strcat(listingfile_line, t.text); 
	}
	else
	{ 
        ERROR2("I3F(): line %d, invalid opcode (%s)\n", (*tptr).line, (*tptr).text);
        return;  
	}

	bRet = match(&t, TOK_FLT_REG);
	if (bRet == TRUE)
	{
		strcat(listingfile_line, t.text);
		encoded[3] = (U1)t.val;
	}
	else
	{ 
        ERROR2("I3F(): line %d, invalid operand (%s)\n", (*tptr).line, (*tptr).text);
        return;  
	}

	bRet = match(&t, TOK_NO_MORE);
	if (bRet != TRUE)
	{ 
        ERROR2("I3F(): line %d, invalid opcode (%s)\n", (*tptr).line, (*tptr).text);
        return; 
	}
	commitToFiles(nBYTES);
	bytePosPass2 = bytePosPass2 + nBYTES;
	return;

}/*end I3F*/

/*-----------------------------------------------------------------*/

void IRF(U1 opcode, struct Token *tptr)
{
	struct Token t;
	U1 bRet;
	U1 nBYTES;
	
	nBYTES = 3;
	sprintf(lineNumber, "%lu", (*tptr).line);
	strcpy(listingfile_line, (*tptr).text);
	strcat(listingfile_line, " ");
	encoded[0] = opcode;

	bRet = match(&t, TOK_INT_REG);
	if (bRet == TRUE)
	{
		strcat(listingfile_line, t.text);
		encoded[1] = (U1)t.val;
	}
	else
	{ 
        ERROR2("IRF(): line %d, invalid operand (%s)\n", (*tptr).line, (*tptr).text);
        return; 
	}
					
	bRet = match(&t, TOK_COMMA);
	if (bRet == TRUE)
	{ 
		strcat(listingfile_line, t.text); 
	}
	else
	{ 
        ERROR2("IRF(): line %d, invalid opcode (%s)\n", (*tptr).line, (*tptr).text);
        return;  
	}
	
	bRet = match(&t, TOK_FLT_REG);
	if (bRet == TRUE)
	{
		strcat(listingfile_line, t.text);
		encoded[2] = (U1)t.val;
	}
	else
	{ 
        ERROR2("IRF(): line %d, invalid operand (%s)\n", (*tptr).line, (*tptr).text);
        return; 
	}

	bRet = match(&t, TOK_NO_MORE);
	if (bRet != TRUE)
	{ 
        ERROR2("IRF(): line %d, invalid opcode (%s)\n", (*tptr).line, (*tptr).text);
        return;  
	}
	commitToFiles(nBYTES);
	bytePosPass2 = bytePosPass2 + nBYTES;
	return;

}/*end IRF*/

/*-----------------------------------------------------------------*/

void IDF(U1 opcode, struct Token *tptr)
{
	struct Token t;
	U1 bRet;
	U1 nBYTES;
	
	nBYTES = 3;
	sprintf(lineNumber, "%lu", (*tptr).line);
	strcpy(listingfile_line, (*tptr).text);
	strcat(listingfile_line, " ");
	encoded[0] = opcode;

	bRet = match(&t, TOK_DBL_REG);
	if (bRet == TRUE)
	{
		strcat(listingfile_line, t.text);
		encoded[1] = (U1)t.val;
	}
	else
	{ 
        ERROR2("IDF(): line %d, invalid operand (%s)\n", (*tptr).line, (*tptr).text);
        return; 
	}
					
	bRet = match(&t,TOK_COMMA);
	if (bRet == TRUE)
	{ 
		strcat(listingfile_line, t.text); 
	}
	else
	{ 
        ERROR2("IDF(): line %d, invalid opcode (%s)\n", (*tptr).line, (*tptr).text);
        return; 
	}
					
	bRet = match(&t, TOK_FLT_REG);
	if (bRet == TRUE)
	{
		strcat(listingfile_line, t.text);
		encoded[2] = (U1)t.val;
	}
	else
	{ 
        ERROR2("IDF(): line %d, invalid operand (%s)\n", (*tptr).line, (*tptr).text);
        return; 
	}

	bRet = match(&t, TOK_NO_MORE);
	if (bRet != TRUE)
	{ 
        ERROR2("IDF(): line %d, invalid opcode (%s)\n", (*tptr).line, (*tptr).text);
        return;  
	}
	commitToFiles(nBYTES);
	bytePosPass2 = bytePosPass2 + nBYTES;
	return;
	
}/*end IDF*/

/*-----------------------------------------------------------------*/

void IDC(U1 opcode, struct Token *tptr)
{
	struct Token t;
	U1 bRet;
	U1 nBYTES;
	
	nBYTES = 10; /* double = 8 bytes */
	sprintf(lineNumber, "%lu", (*tptr).line);
	strcpy(listingfile_line, (*tptr).text);
	strcat(listingfile_line, " ");
	encoded[0] = opcode;

	bRet = match(&t, TOK_DBL_REG);
	if (bRet == TRUE)
	{
		strcat(listingfile_line, t.text);
		encoded[1] = (U1)t.val;
	}
	else
	{ 
        ERROR2("IDC(): line %d, invalid operand (%s)\n", (*tptr).line, (*tptr).text);
        return; 
	}

	bRet = match(&t, TOK_COMMA);
	if (bRet == TRUE)
	{ 
		strcat(listingfile_line, t.text); 
	}
	else
	{ 
        ERROR2("IDC(): line %d, invalid opcode (%s)\n", (*tptr).line, (*tptr).text);
        return;  
	}

	t = getNextLineToken();
	if (t.type == TOK_FLT_CONST)
	{
		strcat(listingfile_line, t.text);
		doubleToBytecode(t.fval, &encoded[2]); 
	}
	else
	{
		ERROR2("IDC(): line %lu, expecting double constant %s\n", t.line, t.text);
		return; 
	}

	bRet = match(&t, TOK_NO_MORE);
	if (bRet != TRUE)
	{ 
        ERROR2("IDC(): line %d, invalid opcode (%s)\n", (*tptr).line, (*tptr).text);
        return; 
	}
	commitToFiles(nBYTES);
	bytePosPass2 = bytePosPass2 + nBYTES;
	return;

⌨️ 快捷键说明

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