Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add tests for Bitcast and Call #11

Merged
merged 3 commits into from
Jun 26, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions src/common/instruction.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ class Instruction {
// raw representation of instruction
std::string raw;

const std::unordered_map<std::string, OPERATION> operation_map{
{"add", ADD}, {"sub", SUB}, {"mul", MUL}, {"shl", SHL}, };

// Default constructor
Instruction() {}

Expand Down Expand Up @@ -67,9 +70,7 @@ class Instruction {
} else if (contents[2] == "add" || contents[2] == "sub" ||
contents[2] == "mul" || contents[2] == "shl") {
destination = contents[0];
oper = (contents[2] == "add") ? ADD :
(oper = (contents[2] == "sub") ? SUB :
(oper = (contents[2] == "mul") ? MUL : SHL));
oper = operation_map.at(contents[2]);
// <result> = add nuw nsw <ty> <op1>, <op2> ; yields {ty}:result
if (contents[3] == "nuw" && contents[4] == "nsw") {
type = contents[5];
Expand All @@ -96,7 +97,7 @@ class Instruction {
oper = BITCAST;
operand1 = contents[4];
operand2 = contents[4];
} else if (contents[0] == "call") {
} else if (contents[0] == "call" || contents[2] == "call") {
oper = CALL;
}

Expand Down
15 changes: 15 additions & 0 deletions test/Common_Instruction_gtest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -53,3 +53,18 @@ TEST(InstructionTests, CheckInstructionForAlloca) {
EXPECT_EQ("%balance", instr.destination);
EXPECT_EQ("i32", instr.type);
}

TEST(InstructionTests, CheckInstructionForBitcast) {
std::string raw_instruction("%1 = bitcast %struct.Foo* %my_struct to void (%struct. Foo*)***");
Instruction instr(raw_instruction);
EXPECT_EQ(BITCAST, instr.oper);
EXPECT_EQ("%1", instr.destination);
EXPECT_EQ("%my_struct", instr.operand1);
EXPECT_EQ("%my_struct", instr.operand2);
}

TEST(InstructionTests, CheckInstructionForCall) {
std::string raw_instruction("%x = call i32 @someFunction()");
Instruction instr(raw_instruction);
EXPECT_EQ(CALL, instr.oper);
}