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

📄 pass2.c

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

}/*end IDC*/

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

void IDR(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("IDR(): 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("IDR(): 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("IDR(): line %d, invalid operand (%s)\n", (*tptr).line, (*tptr).text);
        return;  
	}

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

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

void ID(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_DBL_REG);
	if (bRet == TRUE)
	{
		strcat(listingfile_line, t.text);
		encoded[1] = (U1)t.val;
	}
	else
	{ 
        ERROR2("ID(): line %d, invalid operand (%s)\n", (*tptr).line, (*tptr).text);
        return;  
	}

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

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

void I2D(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("I2D(): 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("I2D(): line %d, invalid opcode (%s)\n", (*tptr).line, (*tptr).text);
        return; 
	}
					
	bRet = match(&t, TOK_DBL_REG);
	if (bRet == TRUE)
	{
		strcat(listingfile_line, t.text);
		encoded[2] = (U1)t.val;
	}
	else
	{ 
        ERROR2("I2D(): line %d, invalid operand (%s)\n", (*tptr).line, (*tptr).text);
        return;  
	}

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


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

void I3D(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_DBL_REG);
	if (bRet == TRUE)
	{
		strcat(listingfile_line, t.text);
		encoded[1] = (U1)t.val;
	}
	else
	{ 
        ERROR2("I3D(): 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("I3D(): line %d, invalid opcode (%s)\n", (*tptr).line, (*tptr).text);
        return;  
	}
					
	bRet = match(&t, TOK_DBL_REG);
	if (bRet == TRUE)
	{
		strcat(listingfile_line, t.text);
		encoded[2] = (U1)t.val;
	}
	else
	{ 
        ERROR2("I3D(): 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("I3D(): line %d, invalid opcode (%s)\n", (*tptr).line, (*tptr).text);
        return;   
	}

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

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

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

void IRD(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("IRD(): 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("IRD(): line %d, invalid opcode (%s)\n", (*tptr).line, (*tptr).text);
        return;   
	}
					
	bRet = match(&t, TOK_DBL_REG);
	if (bRet == TRUE)
	{
		strcat(listingfile_line, t.text);
		encoded[2] = (U1)t.val;
	}
	else
	{ 
        ERROR2("IRD(): line %d, invalid operand (%s)\n", (*tptr).line, (*tptr).text);
        return;   
	}

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

}/*end IRD*/

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

void IFD(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("IFD(): 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("IFD(): line %d, invalid opcode (%s)\n", (*tptr).line, (*tptr).text);
        return;   
	}
					
	bRet = match(&t, TOK_DBL_REG);
	if (bRet == TRUE)
	{
		strcat(listingfile_line, t.text);
		encoded[2] = (U1)t.val;
	}
	else
	{ 
        ERROR2("IFD(): line %d, invalid operand (%s)\n", (*tptr).line, (*tptr).text);
        return;   
	}

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

}/*end IFD*/

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

void commitToFiles(U1 len)
{
	U4 i;

	/*populate list file*/

	if (listing == TRUE)
	{
		/*line number*/

		putStrLstBuff(lineNumber);
		putByteLstBuff(')');

		/*address*/
		putByteLstBuff('[');
		sprintf(lineNumber, "%lu", bytePosPass2);
		putStrLstBuff(lineNumber);
		putByteLstBuff(']');

		/*instruction*/
		putByteLstBuff('\t'); 
		putByteLstBuff('\t'); 
		putStrLstBuff(listingfile_line);						
		i = strlen(listingfile_line);
		while (i <= 30)
		{ 
			putByteLstBuff(' '); 
			i++; 
		}

		/*binary version*/

		putByteLstBuff('\t');
		for (i = 0; i < len; i++)
		{
			putByteLstBuff('[');
			sprintf(lineNumber, "%lu", encoded[i]);
			putStrLstBuff(lineNumber);
			putByteLstBuff(']');
		}
		putByteLstBuff('\n');	
	}

	/*populate temp file*/

	for (i = 0; i < len; i++)
	{ 
		putByteTempBuff(encoded[i]); 
	}
	return;

}/*end commitToFiles*/

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

void generateSymbolSummary()
{
	U4 i;
	U8 nstr;

	/* 1) print out symbol table to listing file*/

	putByteLstBuff('\n');
	sprintf(listingfile_line, "%s", "#++++SYMBOL TABLE++++++");
	putStrLstBuff(listingfile_line);
	putByteLstBuff('\n');

	for (i = 0; i < iGlobVar; i++)
	{
		sprintf(listingfile_line, "->GLOBAL VARIABLE %d)", i); 
		putStrLstBuff(listingfile_line);
		printGlobVarToLst(&(globVar[i]));
		putByteLstBuff('\n');
	}

	for (i = 0; i < iProc; i++)
	{
		sprintf(listingfile_line, "->PROCEDURE %d)", i);
		putStrLstBuff(listingfile_line);
		printProcToLst(&(proc[i]));
		putByteLstBuff('\n');
	}

	/* 2) print out hash table to listing file*/

	putByteLstBuff('\n');
	sprintf(listingfile_line, "%s", "#++++HASH TABLE++++++");
	putStrLstBuff(listingfile_line);
	putByteLstBuff('\n');

	for (i = 0; i < PRIME; i++)
	{ 
		if (hashTbl[i].empty == FALSE)
		{
			sprintf(listingfile_line, "Hash Slot %d)\n", i);
			putStrLstBuff(listingfile_line);

			printTreeToLst(&(hashTbl[i]), 0);
			putByteLstBuff('\n');
		}
	}

	/* 3) print out string table to listing file*/

	putByteLstBuff('\n');
	sprintf(listingfile_line, "%s", "#++++STRING TABLE++++++");
	putStrLstBuff(listingfile_line);
	putByteLstBuff('\n');

	nstr = 1;

	for (i = 0; i < strtbl_iStr; i++)
	{
		if (i == 0)
		{
			putByteLstBuff('0');
			putByteLstBuff(')');
		}
		if (strtbl_text[i] != '\0')
		{ 
			putByteLstBuff(strtbl_text[i]); 
		}
		else if (i < strtbl_iStr - 1)
		{ 
			putByteLstBuff('\n');
			sprintf(lineNumber, "%d)", nstr); 
			putStrLstBuff(lineNumber);
			nstr++; 
		}
		else if (i == strtbl_iStr - 1)
		{ 
			putByteLstBuff('\n');
		}
	}
	return;

}/*end generateSymbolSummary*/

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

void printGlobVarToLst(struct GlobalVariable *ptr)
{	
	sprintf(listingfile_line, "identifier=%s\n", &(strtbl_text[(*ptr).text]));
	putStrLstBuff(listingfile_line);

	sprintf(listingfile_line, "data type=%s\n", globSz[(*ptr).dType]);
	putStrLstBuff(listingfile_line);

	sprintf(listingfile_line, "array length=%lu\n", (*ptr).len);
	putStrLstBuff(listingfile_line);

	sprintf(listingfile_line, "total size=%lu\n", (*ptr).size);
	putStrLstBuff(listingfile_line);

	sprintf(listingfile_line, "line=%lu\n", (*ptr).line);
	putStrLstBuff(listingfile_line);

	sprintf(listingfile_line, "offset_$TOP=%lu\n", (*ptr).offset);
	putStrLstBuff(listingfile_line);

	return;

}/*end printGlobVarToLst*/ 

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

void printProcToLst(struct Procedure * ptr)
{
	U2 i;

	sprintf(listingfile_line, "identifier=%s\n", &(strtbl_text[(*ptr).text]));
	putStrLstBuff(listingfile_line);

	sprintf(listingfile_line, "address=%lu\n", (*ptr).address);
	putStrLstBuff(listingfile_line);

	sprintf(listingfile_line, "source file line=%lu\n", (*ptr).line);
	putStrLstBuff(listingfile_line);

	if ((*ptr).nRet)
	{
		sprintf(listingfile_line, "RET\n");
		putStrLstBuff(listingfile_line);
		
		putByteLstBuff('\t');
		sprintf(listingfile_line, "identifier=%s\n", &(strtbl_text[((*ptr).ret).text]));
		putStrLstBuff(listingfile_line);

		putByteLstBuff('\t');
		sprintf(listingfile_line, "fpOffset=%d\n", ((*ptr).ret).fpOffset);
		putStrLstBuff(listingfile_line);

		putByteLstBuff('\t');
		sprintf(listingfile_line, "line=%lu\n", ((*ptr).ret).line);
		putStrLstBuff(listingfile_line);
	}

	sprintf(listingfile_line, "ARGS\n");
	putStrLstBuff(listingfile_line);
	for (i = 0; i < (*ptr).iArg; i++)
	{
		sprintf(listingfile_line, "%d)", i);
		putStrLstBuff(listingfile_line);

		sprintf(listingfile_line, " identifier=%s\n", &(strtbl_text[((*ptr).arg[i]).text]));
		putStrLstBuff(listingfile_line);

		putByteLstBuff('\t');
		sprintf(listingfile_line, "fpOffset=%d\n", ((*ptr).arg[i]).fpOffset);
		putStrLstBuff(listingfile_line);

		putByteLstBuff('\t');
		sprintf(listingfile_line, "line=%lu\n", ((*ptr).arg[i]).line);
		putStrLstBuff(listingfile_line);
	}

	sprintf(listingfile_line, "LOCALS\n");
	putStrLstBuff(listingfile_line);
	for (i = 0; i < (*ptr).iLocal; i++)
	{
		sprintf(listingfile_line, "%d)", i);
		putStrLstBuff(listingfile_line);

		putByteLstBuff('\t');
		sprintf(listingfile_line, " identifier=%s\n", &(strtbl_text[((*ptr).local[i]).text]));
		putStrLstBuff(listingfile_line);

		putByteLstBuff('\t');
		sprintf(listingfile_line, "fpOffset=%d\n", ((*ptr).local[i]).fpOffset);
		putStrLstBuff(listingfile_line);

		putByteLstBuff('\t');
		sprintf(listingfile_line, "line=%lu\n", ((*ptr).local[i]).line);
		putStrLstBuff(listingfile_line);
	}

	sprintf(listingfile_line, "LABELS\n");
	putStrLstBuff(listingfile_line);
	for (i = 0; i < (*ptr).iLabel; i++)
	{
		sprintf(listingfile_line, "%d)", i);
		putStrLstBuff(listingfile_line);

		sprintf(listingfile_line, " identifier=%s\n", &(strtbl_text[((*ptr).label[i]).text]));
		putStrLstBuff(listingfile_line);

		putByteLstBuff('\t');
		sprintf(listingfile_line, "address=%lu\n", ((*ptr).label[i]).address);
		putStrLstBuff(listingfile_line);

		putByteLstBuff('\t');
		sprintf(listingfile_line, "line=%lu\n", ((*ptr).label[i]).line);
		putStrLstBuff(listingfile_line);
	}
	return;

}/*end printProcToLst*/

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

void printTreeToLst(struct HashTbl * link, int level)
{
	int i;
	int size;

    if (link == NULL)
        return;
    
	printTreeToLst((*link).left, level + 1);
    for (i = 0; i < level; i++)
	{ 
		putByteLstBuff('-'); 
	}

    sprintf(listingfile_line, "id =%s", &(strtbl_text[(*link).text]));
	size = strlen(listingfile_line);
	for (i = 0; i < size; i++)
	{ 
		putByteLstBuff(listingfile_line[i]);
	}
	while (i < 20)
	{ 
		i++; 
		putByteLstBuff(' '); 
	}

	sprintf(listingfile_line, " type=%s", SymTypeStr[(*link).type]);
	size = strlen(listingfile_line);
	for (i = 0; i < size; i++)
	{ 
		putByteLstBuff(listingfile_line[i]);
	}
	while ( i < 20)
	{ 
		i++; 
		putByteLstBuff(' '); 
	}

	sprintf(listingfile_line, "(i,si)=(%d,%d)\n", (*link).index, (*link).subIndex);
	putStrLstBuff(listingfile_line);

    printTreeToLst((*link).right, level + 1);
	return;

}

⌨️ 快捷键说明

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