From 5324e70167a4e83bfeacbb1e7041eb25691ded2b Mon Sep 17 00:00:00 2001 From: "Hassan @Ubuntu" Date: Sun, 27 Jun 2021 00:46:15 +0200 Subject: [PATCH 1/3] Add tests for Bitcast and Call --- test/Common_Instruction_gtest.cc | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/test/Common_Instruction_gtest.cc b/test/Common_Instruction_gtest.cc index 8760329..ec7c936 100644 --- a/test/Common_Instruction_gtest.cc +++ b/test/Common_Instruction_gtest.cc @@ -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); +} From c8816ac292558b6591d16ea9f11892d8f91f6e37 Mon Sep 17 00:00:00 2001 From: "Hassan @Ubuntu" Date: Sun, 27 Jun 2021 00:48:13 +0200 Subject: [PATCH 2/3] Fix typo --- test/Common_Instruction_gtest.cc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/Common_Instruction_gtest.cc b/test/Common_Instruction_gtest.cc index ec7c936..14b48a2 100644 --- a/test/Common_Instruction_gtest.cc +++ b/test/Common_Instruction_gtest.cc @@ -60,7 +60,7 @@ TEST(InstructionTests, CheckInstructionForBitcast) { EXPECT_EQ(BITCAST, instr.oper); EXPECT_EQ("%1", instr.destination); EXPECT_EQ("%my_struct", instr.operand1); - EXPECT_EQ("%my_struct", instr.operand2) + EXPECT_EQ("%my_struct", instr.operand2); } TEST(InstructionTests, CheckInstructionForCall) { From 2113204a3f537bd74dc0ed79bcc174ae92986868 Mon Sep 17 00:00:00 2001 From: "Hassan @Ubuntu" Date: Sun, 27 Jun 2021 01:05:41 +0200 Subject: [PATCH 3/3] Add case for returning function calls --- src/common/instruction.h | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/src/common/instruction.h b/src/common/instruction.h index 9d8fccc..06db49b 100644 --- a/src/common/instruction.h +++ b/src/common/instruction.h @@ -30,6 +30,9 @@ class Instruction { // raw representation of instruction std::string raw; + const std::unordered_map operation_map{ + {"add", ADD}, {"sub", SUB}, {"mul", MUL}, {"shl", SHL}, }; + // Default constructor Instruction() {} @@ -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]); // = add nuw nsw , ; yields {ty}:result if (contents[3] == "nuw" && contents[4] == "nsw") { type = contents[5]; @@ -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; }