我正在编写用于汇编代码生成的java代码,但是我收到以下错误:
CodeGenerator.java:77:错误:缺少return语句 } ^ 1错误
我检查了所有if-else语句,每个case返回一些值。问题是什么?
import java.util.*;
class CodeGenerator
{
static String[] code = new String[100];
static String[] reg = new String[100];
public static void main(String[] args)
{
String[] arg1 = new String[] {"b","d","t","t","w"};
String[] arg2 = new String[] {"c","c","v","u",""};
String[] op = new String[] {"+","+","-","+",""};
String[] result = new String[] {"t","v","u","w","a"};
int temp1, temp2, tempReg, temp;
int i=0;
String opcode;
for(i=0;i<=4;i++)
{
if(!(op[i].equals("=")))
{
temp1 = findReg(arg1[i]);
temp2 = findReg(arg2[i]);
if(temp1==-1 && temp2==-1)
{
tempReg = genReg();
opcode = findInst(op[i]);
code[i] = "MOV "+arg1[i]+", R"+tempReg+"\n"+opcode+" "+arg2[i]+", R"+tempReg;
reg[tempReg]=result[i];
}
else if(temp1!=-1 && temp2!=-1)
{
opcode = findInst(op[i]);
code[i]=opcode+" R"+temp1+", R"+temp2;
reg[temp2]=result[i];
}
else
{
temp=findReg(arg1[i]);
if(temp!=-1)
code[i]="MOV R"+temp+", "+result[i];
}
}
}
for(i=0;i<=4;i++)
System.out.println(code[i]);
}
static int genReg()
{
int i=0;
while(reg[i]!=null)
i++;
return i;
}
static int findReg(String a)
{
int i=0;
while(reg[i]!=null)
{
if(reg[i].equals(a))
return i;
else
i++;
}
return -1;
}
static String findInst(String a)
{
if(a.equals("+"))
return "ADD";
else if(a.equals("-"))
return "SUB";
}
}