对于运行部分 采用多个寄存器和carry函数实现运行
本程序能够实现直接、间接递归调用
//static memory space
#define FUNCTION_MAX_SIZE 10
#define STATICSPACE_MAX_SIZE 10
#define DIMENTION_MAX_SIZE 10
int staticSpace[FUNCTION_MAX_SIZE][DIMENTION_MAX_SIZE][STATICSPACE_MAX_SIZE];
int functionIndex=0;
int dimentionIndex=0;
#define STACK_MAX_SIZE 50
int stack[STACK_MAX_SIZE];
int top = 0;//栈顶寄存器
int callCode[DIMENTION_MAX_SIZE];
int callCodeIndex=0;
int callStack[DIMENTION_MAX_SIZE];
int callStackIndex=0;
int callFunction[DIMENTION_MAX_SIZE];
int callFunctionIndex=0;
int functionAddress[FUNCTION_MAX_SIZE];
int functionAddressIndex=0;
int getFunctionAddress(int address){
if(functionAddressIndex!=0)//是旧的
for(int i=0;i<functionAddressIndex;i++)
if(functionAddress[i]==address) return i;
//是新的
functionAddress[functionAddressIndex]=functionAddressIndex;
functionAddressIndex++;
return(functionAddressIndex-1);
}
void carry(){
codeIndex=0;
functionIndex=0;
//程序第一步是跳转到main
codeIndex=code[codeIndex].operand;
// functionIndex=lookup(codeIndex);
functionIndex=getFunctionAddress(codeIndex);
callFunction[callFunctionIndex]=functionIndex;
while(true){
if(strcmp(code[codeIndex].opt,"BR")==0){//check
codeIndex=code[codeIndex].operand;
}
else if(strcmp(code[codeIndex].opt,"LOAD")==0){//需要配合变量表
int address=code[codeIndex].operand;
stack[top]=staticSpace[functionIndex][dimentionIndex][address];
top++;
codeIndex++;
}
else if(strcmp(code[codeIndex].opt,"LOADI")==0){//check
stack[top]=code[codeIndex].operand;
top++;
codeIndex++;
}
else if(strcmp(code[codeIndex].opt,"STO")==0){//需要配合变量表 STO指令中内部需要出栈
int address=code[codeIndex].operand;
staticSpace[functionIndex][dimentionIndex][address]=stack[top-1];
codeIndex++;
}
//四则运算可以改成switch case语句
else if(strcmp(code[codeIndex].opt,"ADD")==0){//check
stack[top-2]=stack[top-2]+stack[top-1];
stack[top-1]=0;
top--;
codeIndex++;
}
else if(strcmp(code[codeIndex].opt,"SUB")==0){//check
stack[top-2]=stack[top-2]-stack[top-1];
stack[top-1]=0;
top--;
codeIndex++;
}
else if(strcmp(code[codeIndex].opt,"MUL")==0){//check
stack[top-2]=stack[top-2]*stack[top-1];
stack[top-1]=0;
top--;
codeIndex++;
}
else if(strcmp(code[codeIndex].opt,"DIV")==0){//check
stack[top-2]=stack[top-2]/stack[top-1];
stack[top-1]=0;
top--;
codeIndex++;
}
else if(strcmp(code[codeIndex].opt,"BRF")==0){//check
if(stack[top-1]==0){
top--;
codeIndex=code[codeIndex].operand;
}
else{
codeIndex++;
top--;
}
}
else if(strcmp(code[codeIndex].opt,"EQ")==0){//check
stack[top-2]=stack[top-2]==stack[top-1];
stack[top-1]=0;
top--;
codeIndex++;
}
else if(strcmp(code[codeIndex].opt,"NOTEQ")==0){//check
stack[top-2]=stack[top-2]!=stack[top-1];
stack[top-1]=0;
top--;
codeIndex++;
}
else if(strcmp(code[codeIndex].opt,"GT")==0){//check
stack[top-2]=stack[top-2]>stack[top-1];
stack[top-1]=0;
top--;
codeIndex++;
}
else if(strcmp(code[codeIndex].opt,"LES")==0){//check
stack[top-2]=stack[top-2]<stack[top-1];
stack[top-1]=0;
top--;
codeIndex++;
}
else if(strcmp(code[codeIndex].opt,"GE")==0){//check
stack[top-2]=stack[top-2]>=stack[top-1];
stack[top-1]=0;
top--;
codeIndex++;
}
else if(strcmp(code[codeIndex].opt,"LE")==0){//check
stack[top-2]=stack[top-2]<=stack[top-1];
stack[top-1]=0;
top--;
codeIndex++;
}
else if(strcmp(code[codeIndex].opt,"IN")==0){//check
printf("输入一个整数值:");
scanf("%d",&stack[top]);
top++;
codeIndex++;
}
else if(strcmp(code[codeIndex].opt,"OUT")==0){//check
printf("输出了一个整数值:%d\n",stack[top-1]);
top--;
codeIndex++;
}
else if(strcmp(code[codeIndex].opt,"CAL")==0){
callCode[callCodeIndex]=codeIndex;//记录中间代码在调用时的位置
callCodeIndex++;
codeIndex=code[codeIndex].operand;
callStack[callStackIndex]=top;//记录栈在调用时的位置
callStackIndex++;
dimentionIndex++;
// functionIndex=lookup(codeIndex);
functionIndex=getFunctionAddress(codeIndex);
callFunctionIndex++;
callFunction[callFunctionIndex]=functionIndex;
}
else if(strcmp(code[codeIndex].opt,"RETURN")==0){
codeIndex=callCode[--callCodeIndex];//回到中间代码的下一条代码
codeIndex++;
top=callStack[--callStackIndex];//回到栈在调用前的位置
dimentionIndex--;
functionIndex=callFunction[--callFunctionIndex];
}
//结束收尾工作
if(strcmp(code[codeIndex].opt,"")==0) return;
}
}
中间代码的输入需要继承语义分析的输出结果
struct Code{ //中间代码
char opt[10]; //操作码
int operand;//操作数
};
Code code[200]; //指令寄存器
void attain(){
//获取中间代码
// printf("请输入中间代码文件名(包括路径):");
// scanf("%s", codeName);
strcpy(codeName,"intermediate_code.txt");
if((getCode = fopen(codeName, "r")) == NULL){
printf("\n打开%s错误!\n",codeName);
return;
}
getTuples(getCode);
char firsttoken=atoi(token);
do{
firsttoken=atoi(token);
strcpy(code[codeIndex].opt,token1);
code[codeIndex].operand=atoi(token2);
codeIndex++;
// printf("%s %s %s\n", token, token1,token2);
getTuples(getCode);
}while(firsttoken!=atoi(token));
}