Merge pull request #13451 from vyuuui/numeric_labels

Add numeric label support to assembler
This commit is contained in:
Tilka
2025-04-25 01:56:43 +01:00
committed by GitHub
7 changed files with 224 additions and 9 deletions

View File

@ -2029,7 +2029,7 @@ TEST(Assembler, RangeTest)
EXPECT_TRUE(!IsFailure(Assemble(uimm_range_3, 0)));
}
TEST(Assembly, MalformedExpressions)
TEST(Assembler, MalformedExpressions)
{
constexpr char missing_arg[] = "add 0, 1";
constexpr char missing_paren_0[] = ".4byte (1 + 2), ((3 * 6) + 7";
@ -2065,7 +2065,7 @@ TEST(Assembly, MalformedExpressions)
// Modified listing of a subroutine, listing generated by IDA
// Expect bytes are based on disc contents
TEST(Assembly, RealAssembly)
TEST(Assembler, RealAssembly)
{
constexpr char real_assembly[] = ".locate 0x8046A690\n"
".defvar back_chain, -0x30\n"
@ -2223,3 +2223,44 @@ TEST(Assembly, RealAssembly)
EXPECT_EQ(code_blocks[0].instructions[i], real_expect[i]) << " -> i=" << i;
}
}
TEST(Assembler, NumericLabels)
{
constexpr char assembly[] = "0:b 0b\nb 0f\n0:.4byte 0\n0:\n1:.4byte 1b";
constexpr u8 expect[] = {0x48, 0x00, 0x00, 0x00, 0x48, 0x00, 0x00, 0x04,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0c};
auto res = Assemble(assembly, 0);
ASSERT_TRUE(!IsFailure(res));
auto&& code_blocks = GetT(res);
ASSERT_EQ(code_blocks.size(), 1);
ASSERT_EQ(code_blocks[0].instructions.size(), sizeof(expect));
for (size_t i = 0; i < code_blocks[0].instructions.size(); i++)
{
EXPECT_EQ(code_blocks[0].instructions[i], expect[i]) << " -> i=" << i;
}
}
TEST(Assembler, InvalidNumericLabels)
{
constexpr char missing_forward[] = "0:b 0f";
constexpr char missing_backward[] = "b 0b\n0:";
constexpr char forward_directive[] = ".4byte 0f\n0:";
constexpr char missing_backward_directive[] = ".4byte 0b\n0:";
auto res = Assemble(missing_forward, 0);
EXPECT_TRUE(IsFailure(res) && GetFailure(res).message == "No numeric label '0' found below here")
<< GetFailure(res).message;
res = Assemble(missing_backward, 0);
EXPECT_TRUE(IsFailure(res) && GetFailure(res).message == "No numeric label '0' found above here")
<< GetFailure(res).message;
res = Assemble(forward_directive, 0);
EXPECT_TRUE(IsFailure(res) &&
GetFailure(res).message ==
"Forward label references not supported in fully resolved expressons")
<< GetFailure(res).message;
res = Assemble(missing_backward_directive, 0);
EXPECT_TRUE(IsFailure(res) && GetFailure(res).message == "No numeric label '0' found above here")
<< GetFailure(res).message;
}