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

📄 main.c

📁 < 虚拟机设计与实现>>的 windows版本
💻 C
字号:
/************************************************************
2007.04.11 chenyong 根据bill blunton的源程序改定。
已知不足:
<1> 对文件格式的严谨性校验不足.
<1.1> .PB 后应该后接.PE, 即.PB和.PE应配对出现,但现在不写.PE也可以编译通过。
      (改进想法:在main.c 中增加一个currentProcPass1是否等于OUTSIDE_PROC_PASS1的判断。
<1.2> 每条指令都应当在一个(.PB, .PE)之间。
      (改进想法:在pass1_parseLine 中的 processInstruction() 之前增加一个对currentProcPass1
	   的判断。

<2> 应当设定一个关于本系统是little-endian还是big-endian的标志。
    如果本系统是little-endian,则需要进行little-endian 到 big-endian的转换。
	否则就不要进行转换了。

*************************************************************/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "common.h"
#include "error.h"
#include "win32.h"

#include "cmdline.h"
#include "strtbl.h"
#include "lnscan.h"

#include "linetok.h"
#include "pass1.h"
#include "pass2.h"
#include "bldfile.h"

#include "hashtbl.h"
#include "symtbl.h"

#define MAIN_DEBUG		1

#ifdef	MAIN_DEBUG
#define	MAIN_DEBUG0(arg);				printf("main(): ");printf(arg);
#define MAIN_DEBUG1(arg1,arg2);			printf("main(): ");printf(arg1,arg2);
#define MAIN_DEBUG2(arg1,arg2,arg3);	printf("main(): ");printf(arg1,arg2,arg3);
#else
#define	MAIN_DEBUG0(arg);
#define MAIN_DEBUG1(arg1,arg2);
#define MAIN_DEBUG2(arg1,arg2,arg3);
#endif

int main(int argc, char *argv[])
{
	int bRet;
	struct Line line;

	maxErrors = 10;
	nErrors = 0;

	/*1) handle command line arguments */
	
	MAIN_DEBUG0("handling arguments\n");

	cmdline_init();
	if (handleArgs(argc,argv) == FALSE)
	{
		shutDown(SHUTDOWN_ERROR); 
    }

	/*2) PASS1 -  scan, parse, populate symbol table*/

	MAIN_DEBUG0("initiating first pass\n");

	bRet = strtbl_init();
	if (bRet == FALSE)
    {
		shutDown(SHUTDOWN_ERROR);
    }
	
	bRet = SymbolTable_init();
	if (bRet == FALSE)
	{
        strtbl_free();
        
		shutDown(SHUTDOWN_ERROR);
	}
    HashTable_init();
	
	bRet = LineScanner_init();
	if (bRet == FALSE)
	{
		LineScanner_free();
		HashTable_free();
        SymbolTable_free();
	    strtbl_free();
        
		shutDown(SHUTDOWN_ERROR);
    }
	pass1_init();

	line = getInputLine();
	while (line.end != TRUE)
	{
        pass1_parseLine(&line);
		line = getInputLine();
	}
    pass1_parseLine(&line);

	LineScanner_free();

	/*if errors exist, shutdown before generate anything*/
	if (nErrors > 0)
	{
		printf("%d Error(s) during first pass\n", nErrors);
		printf("build failed\n");

		HashTable_free();
        SymbolTable_free();
	    strtbl_free();

		shutDown(SHUTDOWN_ERROR);
	}

	/*3) PASS2 - create bytecode temp file and listing file */

    MAIN_DEBUG0("\ninitiating second pass\n");

	bRet = LineScanner_init();
	if (bRet == FALSE)
	{
		LineScanner_free();
		HashTable_free();
        SymbolTable_free();
	    strtbl_free();
        
		shutDown(SHUTDOWN_ERROR);
    }

	bRet = pass2_init();
	if (bRet == FALSE)
	{
		pass2_free();
		LineScanner_free();
		HashTable_free();
        SymbolTable_free();
	    strtbl_free();
        
		shutDown(SHUTDOWN_ERROR);
    }

	line = getInputLine();
	while (line.end != TRUE)
	{
        pass2_parseLine(&line);
		if (nErrors > 0)
		{
		    printf("main(): %d Error(s) during second pass\n", nErrors);
		    printf("main(): build failed\n");
		    
		    pass2_free();
		    LineScanner_free();
		    HashTable_free();
            SymbolTable_free();
	        strtbl_free();
        
		    shutDown(SHUTDOWN_ERROR);
		}
		line = getInputLine();
	}
    pass2_parseLine(&line);
    if (nErrors > 0)
	{
        printf("main(): %d Error(s) during second pass\n", nErrors);
		printf("main(): build failed\n");
		    
		pass2_free();
		LineScanner_free();
		HashTable_free();
        SymbolTable_free();
	    strtbl_free();
        
		shutDown(SHUTDOWN_ERROR);
	}
	
	if (listing == TRUE)
	    generateSymbolSummary();

	pass2_free();
	LineScanner_free();

	MAIN_DEBUG1("%lu bytes written to tempfile\n", bytePosPass2);

	/*4) build compilation unit */

	MAIN_DEBUG0("building bytecode executable\n");

	bRet = BuildFile_init();
	if (bRet == FALSE)
	{
		BuildFile_free();
		HashTable_free();
        SymbolTable_free();
	    strtbl_free();
        
		shutDown(SHUTDOWN_ERROR);
    }
	buildFileFormat();

	BuildFile_free();
	SymbolTable_free();
	HashTable_free();
	strtbl_free();

	printf("main(): exiting with (%d) errors\n", nErrors);
	return 0;

}



⌨️ 快捷键说明

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