Skip to content

Commit e7b5693

Browse files
committed
[fix](mips): fix
1 parent 21af90b commit e7b5693

File tree

3 files changed

+24
-13
lines changed

3 files changed

+24
-13
lines changed

libs/mips/src/mips_printer.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ void ICode::PrintCode(std::ostream &out) {
160160
rs->PrintReg(out);
161161
out << ", " << label << std::endl;
162162
} else if (op <= BC1T && op >= BC1F) {
163-
Ope = op == BC1F ? "bc1f" : op == BC1T ? "bc1f" : "";
163+
Ope = op == BC1F ? "bc1f" : op == BC1T ? "bc1t" : "";
164164
out << Ope << " " << label << std::endl;
165165
} else {
166166
TOLANG_DIE("ICode not supported.");

libs/mips/src/translator.cpp

+19-8
Original file line numberDiff line numberDiff line change
@@ -133,9 +133,17 @@ void Translator::translate(UnaryOperatorPtr unaryOperatorPtr) {
133133
manager->occupy(operandRegPtr, unaryOperatorPtr);
134134
} else if (unaryOperatorPtr->OpType() == UnaryOpType::Neg) { // -
135135
auto result = manager->allocReg(unaryOperatorPtr);
136+
if (unaryOperatorPtr->GetType()->IsIntegerTy()) {
137+
manager->addCode(
138+
new RCode(Subu, result, manager->zero, operandRegPtr));
139+
} else if (unaryOperatorPtr->GetType()->IsIntegerTy()) {
140+
auto reg0 = manager->getFreeFloat();
141+
std::string name0 = manager->addFloat(0);
142+
manager->addCode(new ICode(LS, reg0, name0));
143+
manager->addCode(new RCode(SubS, result, reg0, operandRegPtr));
144+
}
136145
auto optype = unaryOperatorPtr->GetType()->IsFloatTy() ? SubS : Subu;
137-
manager->addCode(
138-
new RCode(optype, result, manager->zero, operandRegPtr));
146+
139147
} else { // not !
140148
assert(unaryOperatorPtr->OpType() == UnaryOpType::Not &&
141149
"invalid unary operator");
@@ -211,11 +219,11 @@ void Translator::translate(CompareInstructionPtr compareInstructionPtr) {
211219
doOpposite = true;
212220
break;
213221
case CompareOpType::GreaterThan:
214-
op = CLtS;
222+
op = CLeS;
215223
doOpposite = true;
216224
break;
217225
case CompareOpType::GreaterThanOrEqual:
218-
op = CLeS;
226+
op = CLtS;
219227
doOpposite = true;
220228
break;
221229
case CompareOpType::LessThan:
@@ -255,10 +263,10 @@ void Translator::translate(BranchInstPtr branchInstPtr) {
255263
std::string falseLabel =
256264
*manager->getLabelName(branchInstPtr->FalseBlock());
257265

258-
manager->addCode(new ICode(Bnez, cond, falseLabel));
266+
manager->addCode(new ICode(Bnez, cond, trueLabel));
259267
manager->addCode(new RCode(Nop));
260268

261-
manager->addCode(new JCode(J, trueLabel));
269+
manager->addCode(new JCode(J, falseLabel));
262270
manager->addCode(new RCode(Nop));
263271
}
264272

@@ -269,8 +277,12 @@ void Translator::translate(CallInstPtr callInstPtr) {
269277
pushSet.insert(occ.first);
270278
}
271279
}
280+
for (UsePtr use : *(callInstPtr->GetUseList())) {
281+
pushSet.erase(use->GetValue());
282+
}
283+
272284
int pos = manager->currentOffset - 4 -
273-
4 * (pushSet.size() - callInstPtr->GetUseList()->size());
285+
4 * pushSet.size();
274286
for (UsePtr use : *(callInstPtr->GetUseList())) {
275287
MipsCodeType codeType =
276288
use->GetValue()->GetType()->IsFloatTy() ? SS : SW;
@@ -279,7 +291,6 @@ void Translator::translate(CallInstPtr callInstPtr) {
279291
use->GetValue()->GetType()->IsFloatTy() ? FloatRegTy : TmpRegTy);
280292
manager->addCode(new ICode(codeType, reg, manager->sp, pos));
281293
pos -= 4;
282-
pushSet.erase(use->GetValue());
283294
}
284295

285296
for (auto valuePtr : pushSet) {

tests/test_mips.cpp

+4-4
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ add.s $f8, $f0, $f9
124124
s.s $f8, 0($sp)
125125
l.s $f10, 0($sp)
126126
l.s $f11, flt4
127-
c.lt.s $f10, $f11
127+
c.le.s $f10, $f11
128128
bc1f main_1
129129
nop
130130
addiu $t0, $zero, 0
@@ -135,9 +135,9 @@ nop
135135
addiu $t0, $zero, 1
136136
137137
main_2:
138-
bnez $t0, main_4
138+
bnez $t0, main_3
139139
nop
140-
j main_3
140+
j main_4
141141
nop
142142
143143
main_3:
@@ -177,7 +177,7 @@ TEST_CASE("testing mips") {
177177
translator.print(ss);
178178

179179
auto ir = ss.str();
180-
// std::cout << ir;
180+
// std::cout << ir;
181181
CHECK_EQ(ir, EXPECTED);
182182
}
183183

0 commit comments

Comments
 (0)