📄 codegeneration.java
字号:
else{
n.exp2_.accept(this);
//compare
asm.appendEntry(SPARCOpcode.cmp_instr(n.exp2_.getReg(), new SPARCImmed(0)));
//branch
asm.appendEntry(SPARCOpcode.bne_instr(".DenominatorNotZero"+count));
asm.appendEntry(SPARCOpcode.nop_instr());
//.False:
asm.appendEntry(SPARCOpcode.sethi_instr(new SPARCHiOp(new ASMLabel(".DIVISION_ZERO")), SPARCRegister.o0));
asm.appendEntry(SPARCOpcode.or_instr(SPARCRegister.o0, new SPARCLoOp(new ASMLabel(".DIVISION_ZERO")), SPARCRegister.o0));
int lineno = n.exp2_.getExpLineNo();
if(lineno <= 4095 && lineno >= -4096){
asm.appendEntry(SPARCOpcode.mov_instr(new SPARCImmed(lineno), SPARCRegister.o1));
}else{
asm.appendEntry(SPARCOpcode.sethi_instr(new SPARCHiOp(lineno), SPARCRegister.o1));
asm.appendEntry(SPARCOpcode.or_instr(SPARCRegister.o1, new SPARCLoOp(lineno), SPARCRegister.o1));
}
asm.appendEntry(SPARCOpcode.call_instr("printf"));
asm.appendEntry(SPARCOpcode.nop_instr());
asm.appendEntry(SPARCOpcode.call_instr("exit"));
asm.appendEntry(SPARCOpcode.nop_instr());
//.True:
asm.appendEntry(new ASMLabel(".DenominatorNotZero"+count));
n.setReg(SPARCRegister.getNextRegLocal());
asm.appendEntry(SPARCOpcode.sdiv_instr(n.exp1_.getReg(),n.exp2_.getReg(), n.getReg()));
n.exp1_.releaseReg();
n.exp2_.releaseReg();
}
}
public void visit(ExpMod n){
int count = ++labelCnt;
n.exp1_.accept(this);
if(SPARCRegister.getLocalAvailable() == 2){
//before calling the second expression, store the first expression value in the stack
//decrement stack pointer by 8
asm.appendEntry(SPARCOpcode.sub_instr(SPARCRegister.sp, new SPARCImmed(8), SPARCRegister.sp));
//load exp1 register to address sp + size
asm.appendEntry(SPARCOpcode.st_instr(n.exp1_.getReg(), new SPARCAddress(SPARCRegister.sp, 92)));
//release exp1 register
n.exp1_.releaseReg();
//now accept the second expression
n.exp2_.accept(this);
asm.appendEntry(SPARCOpcode.cmp_instr(n.exp2_.getReg(), new SPARCImmed(0)));
//branch
asm.appendEntry(SPARCOpcode.bne_instr(".DenominatorNotZero"+count));
asm.appendEntry(SPARCOpcode.nop_instr());
//.False:
asm.appendEntry(SPARCOpcode.sethi_instr(new SPARCHiOp(new ASMLabel(".DIVISION_ZERO")), SPARCRegister.o0));
asm.appendEntry(SPARCOpcode.or_instr(SPARCRegister.o0, new SPARCLoOp(new ASMLabel(".DIVISION_ZERO")), SPARCRegister.o0));
int lineno = n.exp2_.getExpLineNo();
if(lineno <= 4095 && lineno >= -4096){
asm.appendEntry(SPARCOpcode.mov_instr(new SPARCImmed(lineno), SPARCRegister.o1));
}else{
asm.appendEntry(SPARCOpcode.sethi_instr(new SPARCHiOp(lineno), SPARCRegister.o1));
asm.appendEntry(SPARCOpcode.or_instr(SPARCRegister.o1, new SPARCLoOp(lineno), SPARCRegister.o1));
}
asm.appendEntry(SPARCOpcode.call_instr("printf"));
asm.appendEntry(SPARCOpcode.nop_instr());
asm.appendEntry(SPARCOpcode.call_instr("exit"));
asm.appendEntry(SPARCOpcode.nop_instr());
//.True:
asm.appendEntry(new ASMLabel(".DenominatorNotZero"+count));
asm.appendEntry(SPARCOpcode.ld_instr(new SPARCAddress(SPARCRegister.sp, 92), SPARCRegister.g1));
asm.appendEntry(SPARCOpcode.add_instr(SPARCRegister.sp, new SPARCImmed(8), SPARCRegister.sp));
n.setReg(SPARCRegister.getNextRegLocal());
asm.appendEntry(SPARCOpcode.sdiv_instr(SPARCRegister.g1,n.exp2_.getReg(), n.getReg()));
asm.appendEntry(SPARCOpcode.smul_instr(n.getReg(), n.exp2_.getReg(),n.getReg()));
asm.appendEntry(SPARCOpcode.sub_instr(SPARCRegister.g1, n.getReg(),n.getReg()));
n.exp2_.releaseReg();
}
else{
n.exp2_.accept(this);
//compare
asm.appendEntry(SPARCOpcode.cmp_instr(n.exp2_.getReg(), new SPARCImmed(0)));
//branch
asm.appendEntry(SPARCOpcode.bne_instr(".DenominatorNotZero"+count));
asm.appendEntry(SPARCOpcode.nop_instr());
//.False:
asm.appendEntry(SPARCOpcode.sethi_instr(new SPARCHiOp(new ASMLabel(".DIVISION_ZERO")), SPARCRegister.o0));
asm.appendEntry(SPARCOpcode.or_instr(SPARCRegister.o0, new SPARCLoOp(new ASMLabel(".DIVISION_ZERO")), SPARCRegister.o0));
int lineno = n.exp2_.getExpLineNo();
if(lineno <= 4095 && lineno >= -4096){
asm.appendEntry(SPARCOpcode.mov_instr(new SPARCImmed(lineno), SPARCRegister.o1));
}else{
asm.appendEntry(SPARCOpcode.sethi_instr(new SPARCHiOp(lineno), SPARCRegister.o1));
asm.appendEntry(SPARCOpcode.or_instr(SPARCRegister.o1, new SPARCLoOp(lineno), SPARCRegister.o1));
}
asm.appendEntry(SPARCOpcode.call_instr("printf"));
asm.appendEntry(SPARCOpcode.nop_instr());
asm.appendEntry(SPARCOpcode.call_instr("exit"));
asm.appendEntry(SPARCOpcode.nop_instr());
//.True:
asm.appendEntry(new ASMLabel(".DenominatorNotZero"+count));
n.setReg(SPARCRegister.getNextRegLocal());
asm.appendEntry(SPARCOpcode.sdiv_instr(n.exp1_.getReg(),n.exp2_.getReg(), n.getReg()));
asm.appendEntry(SPARCOpcode.smul_instr( n.getReg(), n.exp2_.getReg(), n.getReg()));
asm.appendEntry(SPARCOpcode.sub_instr(n.exp1_.getReg(), n.getReg(),n.getReg()));
n.exp1_.releaseReg();
n.exp2_.releaseReg();
}
}
public void visit(ExpUMinus n){
n.exp_.accept(this);
n.setReg(SPARCRegister.getNextRegLocal());
asm.appendEntry(new ASMComment(" ExpUminus encountered: subtract 0 from the expression value:"));
asm.appendEntry(SPARCOpcode.sub_instr(SPARCRegister.g0, n.exp_.getReg(), n.getReg()));
n.exp_.releaseReg();
}
public void visit(ExpShiftLeft n){
n.exp1_.accept(this);
//check if the register is the last register
if(SPARCRegister.getLocalAvailable() == 2){
//before calling the second expression, store the first expression value in the stack
//decrement stack pointer by 8
asm.appendEntry(SPARCOpcode.sub_instr(SPARCRegister.sp, new SPARCImmed(8), SPARCRegister.sp));
//load exp1 register to address sp + size
asm.appendEntry(SPARCOpcode.st_instr(n.exp1_.getReg(), new SPARCAddress(SPARCRegister.sp, 92)));
//release exp1 register
n.exp1_.releaseReg();
//now accept the second expression
n.exp2_.accept(this);
asm.appendEntry(SPARCOpcode.ld_instr(new SPARCAddress(SPARCRegister.sp, 92), SPARCRegister.g1));
asm.appendEntry(SPARCOpcode.add_instr(SPARCRegister.sp, new SPARCImmed(8), SPARCRegister.sp));
n.setReg(SPARCRegister.getNextRegLocal());
asm.appendEntry(SPARCOpcode.sll_instr(SPARCRegister.g1,n.exp2_.getReg(), n.getReg()));
n.exp2_.releaseReg();
}else{
n.exp2_.accept(this);
n.setReg(SPARCRegister.getNextRegLocal());
asm.appendEntry(SPARCOpcode.sll_instr(n.exp1_.getReg(),n.exp2_.getReg(), n.getReg()));
n.exp1_.releaseReg();
n.exp2_.releaseReg();
}
}
public void visit(ExpShiftRight n){
n.exp1_.accept(this);
//check if the register is the last register
if(SPARCRegister.getLocalAvailable() == 2){
//before calling the second expression, store the first expression value in the stack
//decrement stack pointer by 8
asm.appendEntry(SPARCOpcode.sub_instr(SPARCRegister.sp, new SPARCImmed(8), SPARCRegister.sp));
//load exp1 register to address sp + size
asm.appendEntry(SPARCOpcode.st_instr(n.exp1_.getReg(), new SPARCAddress(SPARCRegister.sp, 92)));
//release exp1 register
n.exp1_.releaseReg();
//now accept the second expression
n.exp2_.accept(this);
asm.appendEntry(SPARCOpcode.ld_instr(new SPARCAddress(SPARCRegister.sp, 92), SPARCRegister.g1));
asm.appendEntry(SPARCOpcode.add_instr(SPARCRegister.sp, new SPARCImmed(8), SPARCRegister.sp));
n.setReg(SPARCRegister.getNextRegLocal());
if(n.logicalFlag){
asm.appendEntry(SPARCOpcode.srl_instr(SPARCRegister.g1,n.exp2_.getReg(), n.getReg()));
}else{
asm.appendEntry(SPARCOpcode.sra_instr(SPARCRegister.g1,n.exp2_.getReg(), n.getReg()));
}
n.exp2_.releaseReg();
}else{
n.exp2_.accept(this);
n.setReg(SPARCRegister.getNextRegLocal());
if(n.logicalFlag){
asm.appendEntry(SPARCOpcode.srl_instr(n.exp1_.getReg(),n.exp2_.getReg(), n.getReg()));
}else{
asm.appendEntry(SPARCOpcode.sra_instr(n.exp1_.getReg(),n.exp2_.getReg(), n.getReg()));
}
n.exp1_.releaseReg();
n.exp2_.releaseReg();
}
}
public void visit(ExpLessThan n){
int cnt = ++labelCnt;
n.exp1_.accept(this);
//check if the register is the last register
if(SPARCRegister.getLocalAvailable() == 2){
//before calling the second expression, store the first expression value in the stack
//decrement stack pointer by 8
asm.appendEntry(SPARCOpcode.sub_instr(SPARCRegister.sp, new SPARCImmed(8), SPARCRegister.sp));
//load exp1 register to address sp + size
asm.appendEntry(SPARCOpcode.st_instr(n.exp1_.getReg(), new SPARCAddress(SPARCRegister.sp, 92)));
//release exp1 register
n.exp1_.releaseReg();
//now accept the second expression
n.exp2_.accept(this);
asm.appendEntry(SPARCOpcode.ld_instr(new SPARCAddress(SPARCRegister.sp, 92), SPARCRegister.g1));
asm.appendEntry(SPARCOpcode.add_instr(SPARCRegister.sp, new SPARCImmed(8), SPARCRegister.sp));
n.setReg(SPARCRegister.getNextRegLocal());
//compare
asm.appendEntry(SPARCOpcode.cmp_instr(SPARCRegister.g1, n.exp2_.getReg()));
//branch
asm.appendEntry(SPARCOpcode.bl_instr(".True"+cnt));
asm.appendEntry(SPARCOpcode.nop_instr());
//.False:
asm.appendEntry(SPARCOpcode.mov_instr(new SPARCImmed(0), n.getReg()));
asm.appendEntry(SPARCOpcode.b_instr(".End"+cnt));
asm.appendEntry(SPARCOpcode.nop_instr());
//.True:
asm.appendEntry(new ASMLabel(".True"+cnt));
asm.appendEntry(SPARCOpcode.mov_instr(new SPARCImmed(1), n.getReg()));
//.End:
asm.appendEntry(new ASMLabel(".End"+cnt));
n.exp2_.releaseReg();
}else{
n.exp2_.accept(this);
n.setReg(SPARCRegister.getNextRegLocal());
//compare
asm.appendEntry(SPARCOpcode.cmp_instr(n.exp1_.getReg(), n.exp2_.getReg()));
//branch
asm.appendEntry(SPARCOpcode.bl_instr(".True"+cnt));
asm.appendEntry(SPARCOpcode.nop_instr());
//.False:
asm.appendEntry(SPARCOpcode.mov_instr(new SPARCImmed(0), n.getReg()));
asm.appendEntry(SPARCOpcode.b_instr(".End"+cnt));
asm.appendEntry(SPARCOpcode.nop_instr());
//.True:
asm.appendEntry(new ASMLabel(".True"+cnt));
asm.appendEntry(SPARCOpcode.mov_instr(new SPARCImmed(1), n.getReg()));
//.End:
asm.appendEntry(new ASMLabel(".End"+cnt));
n.exp1_.releaseReg();
n.exp2_.releaseReg();
}
}
public void visit(ExpGreaterThan n){
int cnt = ++labelCnt;
n.exp1_.accept(this);
//check if the register is the last register
if(SPARCRegister.getLocalAvailable() == 2){
//before calling the second expression, store the first expression value in the stack
//decrement stack pointer by 8
asm.appendEntry(SPARCOpcode.sub_instr(SPARCRegister.sp, new SPARCImmed(8), SPARCRegister.sp));
//load exp1 register to address sp + size
asm.appendEntry(SPARCOpcode.st_instr(n.exp1_.getReg(), new SPARCAddress(SPARCRegister.sp, 92)));
//release exp1 register
n.exp1_.releaseReg();
//now accept the second expression
n.exp2_.accept(this);
asm.appendEntry(SPARCOpcode.ld_instr(new SPARCAddress(SPARCRegister.sp, 92), SPARCRegister.g1));
asm.appendEntry(SPARCOpcode.add_instr(SPARCRegister.sp, new SPARCImmed(8), SPARCRegister.sp));
n.setReg(SPARCRegister.getNextRegLocal());
//compare
asm.appendEntry(SPARCOpcode.cmp_instr(SPARCRegister.g1, n.exp2_.getReg()));
//branch
asm.appendEntry(SPARCOpcode.bg_instr(".True"+cnt));
asm.appendEntry(SPARCOpcode.nop_instr());
//.False:
asm.appendEntry(SPARCOpcode.mov_instr(new SPARCImmed(0), n.getReg()));
asm.appendEntry(SPARCOpcode.b_instr(".End"+cnt));
asm.appendEntry(SPARCOpcode.nop_instr());
//.True:
asm.appendEntry(new ASMLabel(".True"+cnt));
asm.appendEntry(SPARCOpcode.mov_instr(new SPARCImmed(1), n.getReg()));
//.End:
asm.appendEntry(new ASMLabel(".End"+cnt));
n.exp2_.releaseReg();
}else{
n.exp2_.accept(this);
n.setReg(SPARCRegister.getNextRegLocal());
//compare
asm.appendEntry(SPARCOpcode.cmp_instr(n.exp1_.getReg(), n.exp2_.getReg()));
//branch
asm.appendEntry(SPAR
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -