Skip to content

Commit

Permalink
Comparison machine code for cmp reg, bool and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
belijzajac committed May 3, 2024
1 parent 065c289 commit d565985
Show file tree
Hide file tree
Showing 2 changed files with 124 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/backend/codegen/CodeGenerator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -349,6 +349,14 @@ void CodeGenerator::emitCmp(const InstructionPtr &instruction) {
return;
}

// cmp reg, bool
if (argOne->getType() == TType::REGISTER && (argTwo->getType() == TType::KW_TRUE || argTwo->getType() == TType::KW_FALSE)) {
const auto bytes = MachineCodeTable<uint32_t>::getCmpMachineCode(argOne->getValue<Basic::register_t>());
m_textSection.putBytes(bytes);
m_textSection.putValue<uint32_t>(argTwo->getValue<bool>() ? 1 : 0);
return;
}

// cmp reg1, reg2
if (argOne->getType() == TType::REGISTER && argTwo->getType() == TType::REGISTER) {
const auto [dst, src] = assignRegisters(argOne->getValue<Basic::register_t>(), argTwo->getValue<Basic::register_t>());
Expand Down
116 changes: 116 additions & 0 deletions tests/programs/ProgramTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1123,6 +1123,20 @@ TEST_F(ProgramTest, ConditionalIntEqualTrue) {
EXPECT_PROGRAM_OUTPUT(exec("./a.out"), "true");
}

TEST_F(ProgramTest, ConditionalBooleanEqualTrue) {
constexpr auto program = R"(
fn main() {
bool value = true;
if (value == true) {
print("true");
} else {
print("false");
}
})"sv;
SetUp(program);
EXPECT_PROGRAM_OUTPUT(exec("./a.out"), "true");
}

TEST_F(ProgramTest, ConditionalIntVariablesEqualTrue) {
constexpr auto program = R"(
fn main() {
Expand All @@ -1138,6 +1152,21 @@ TEST_F(ProgramTest, ConditionalIntVariablesEqualTrue) {
EXPECT_PROGRAM_OUTPUT(exec("./a.out"), "true");
}

TEST_F(ProgramTest, ConditionalBooleanVariablesEqualTrue) {
constexpr auto program = R"(
fn main() {
bool value1 = true;
bool value2 = true;
if (value1 == value2) {
print("true");
} else {
print("false");
}
})"sv;
SetUp(program);
EXPECT_PROGRAM_OUTPUT(exec("./a.out"), "true");
}

TEST_F(ProgramTest, ConditionalIntEqualFalse) {
constexpr auto program = R"(
fn main() {
Expand All @@ -1152,6 +1181,20 @@ TEST_F(ProgramTest, ConditionalIntEqualFalse) {
EXPECT_PROGRAM_OUTPUT(exec("./a.out"), "false");
}

TEST_F(ProgramTest, ConditionalBooleanEqualFalse) {
constexpr auto program = R"(
fn main() {
bool value = true;
if (value == false) {
print("true");
} else {
print("false");
}
})"sv;
SetUp(program);
EXPECT_PROGRAM_OUTPUT(exec("./a.out"), "false");
}

TEST_F(ProgramTest, ConditionalIntVariablesEqualFalse) {
constexpr auto program = R"(
fn main() {
Expand All @@ -1167,6 +1210,21 @@ TEST_F(ProgramTest, ConditionalIntVariablesEqualFalse) {
EXPECT_PROGRAM_OUTPUT(exec("./a.out"), "false");
}

TEST_F(ProgramTest, ConditionalBooleanVariablesEqualFalse) {
constexpr auto program = R"(
fn main() {
bool value1 = true;
bool value2 = false;
if (value1 == value2) {
print("true");
} else {
print("false");
}
})"sv;
SetUp(program);
EXPECT_PROGRAM_OUTPUT(exec("./a.out"), "false");
}

TEST_F(ProgramTest, ConditionalIntNotEqualTrue) {
constexpr auto program = R"(
fn main() {
Expand All @@ -1181,6 +1239,20 @@ TEST_F(ProgramTest, ConditionalIntNotEqualTrue) {
EXPECT_PROGRAM_OUTPUT(exec("./a.out"), "true");
}

TEST_F(ProgramTest, ConditionalBooleanNotEqualTrue) {
constexpr auto program = R"(
fn main() {
bool value = true;
if (value != false) {
print("true");
} else {
print("false");
}
})"sv;
SetUp(program);
EXPECT_PROGRAM_OUTPUT(exec("./a.out"), "true");
}

TEST_F(ProgramTest, ConditionalIntVariablesNotEqualTrue) {
constexpr auto program = R"(
fn main() {
Expand All @@ -1196,6 +1268,21 @@ TEST_F(ProgramTest, ConditionalIntVariablesNotEqualTrue) {
EXPECT_PROGRAM_OUTPUT(exec("./a.out"), "true");
}

TEST_F(ProgramTest, ConditionalBooleanVariablesNotEqualTrue) {
constexpr auto program = R"(
fn main() {
bool value1 = true;
bool value2 = false;
if (value1 != value2) {
print("true");
} else {
print("false");
}
})"sv;
SetUp(program);
EXPECT_PROGRAM_OUTPUT(exec("./a.out"), "true");
}

TEST_F(ProgramTest, ConditionalIntNotEqualFalse) {
constexpr auto program = R"(
fn main() {
Expand All @@ -1210,6 +1297,20 @@ TEST_F(ProgramTest, ConditionalIntNotEqualFalse) {
EXPECT_PROGRAM_OUTPUT(exec("./a.out"), "false");
}

TEST_F(ProgramTest, ConditionalBooleanNotEqualFalse) {
constexpr auto program = R"(
fn main() {
bool value = true;
if (value != true) {
print("true");
} else {
print("false");
}
})"sv;
SetUp(program);
EXPECT_PROGRAM_OUTPUT(exec("./a.out"), "false");
}

TEST_F(ProgramTest, ConditionalIntVariablesNotEqualFalse) {
constexpr auto program = R"(
fn main() {
Expand All @@ -1225,6 +1326,21 @@ TEST_F(ProgramTest, ConditionalIntVariablesNotEqualFalse) {
EXPECT_PROGRAM_OUTPUT(exec("./a.out"), "false");
}

TEST_F(ProgramTest, ConditionalBooleanVariablesNotEqualFalse) {
constexpr auto program = R"(
fn main() {
bool value1 = true;
bool value2 = true;
if (value1 != value2) {
print("true");
} else {
print("false");
}
})"sv;
SetUp(program);
EXPECT_PROGRAM_OUTPUT(exec("./a.out"), "false");
}

TEST_F(ProgramTest, ConditionalBooleanFollowup) {
constexpr auto program = R"(
fn main() {
Expand Down

0 comments on commit d565985

Please sign in to comment.