Skip to content

Commit

Permalink
Added test for jump priority queue ordering
Browse files Browse the repository at this point in the history
Signed-off-by: Stefan Marr <[email protected]>
  • Loading branch information
smarr committed Jul 31, 2024
1 parent 1b4cbf2 commit 7d6aa4e
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 4 deletions.
40 changes: 39 additions & 1 deletion src/unitTests/BytecodeGenerationTest.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
#include <cassert>
#include <cppunit/TestAssert.h>
#include <cstddef>
#include <cstdint>
#include <cstdio>
#include <queue>
#include <sstream>
#include <string>
#include <vector>
Expand All @@ -11,10 +13,11 @@
#include "../compiler/Disassembler.h"
#include "../compiler/MethodGenerationContext.h"
#include "../compiler/Parser.h"
#include "../interpreter/bytecodes.h"
#include "../misc/StringUtil.h"
#include "../misc/debug.h"
#include "../interpreter/bytecodes.h"
#include "../vm/Symbols.h"
#include "../vmobjects/VMMethod.h"
#include "BytecodeGenerationTest.h"

void BytecodeGenerationTest::dump(MethodGenerationContext* mgenc) {
Expand Down Expand Up @@ -1504,3 +1507,38 @@ void BytecodeGenerationTest::testInliningWhileLoopsWithContractingBranches() {
*/

void BytecodeGenerationTest::testJumpQueuesOrdering() {
std::priority_queue<Jump> jumps;

jumps.emplace(Jump(1, BC_JUMP, 0));
jumps.emplace(Jump(5, BC_JUMP, 0));
jumps.emplace(Jump(8, BC_JUMP, 0));
jumps.emplace(Jump(2, BC_JUMP, 0));

CPPUNIT_ASSERT_EQUAL((size_t) 1, jumps.top().originalJumpTargetIdx); jumps.pop();
CPPUNIT_ASSERT_EQUAL((size_t) 2, jumps.top().originalJumpTargetIdx); jumps.pop();
CPPUNIT_ASSERT_EQUAL((size_t) 5, jumps.top().originalJumpTargetIdx); jumps.pop();
CPPUNIT_ASSERT_EQUAL((size_t) 8, jumps.top().originalJumpTargetIdx); jumps.pop();


std::priority_queue<BackJump> backJumps;
backJumps.emplace(BackJump(13, 9));
backJumps.emplace(BackJump(3, 12));
backJumps.emplace(BackJump(54, 54));

CPPUNIT_ASSERT_EQUAL((size_t) 3, backJumps.top().loopBeginIdx); backJumps.pop();
CPPUNIT_ASSERT_EQUAL((size_t) 13, backJumps.top().loopBeginIdx); backJumps.pop();
CPPUNIT_ASSERT_EQUAL((size_t) 54, backJumps.top().loopBeginIdx); backJumps.pop();

std::priority_queue<BackJumpPatch> backJumpsToPatch;
backJumpsToPatch.emplace(BackJumpPatch(3, 2));
backJumpsToPatch.emplace(BackJumpPatch(32, 44));
backJumpsToPatch.emplace(BackJumpPatch(12, 55));


CPPUNIT_ASSERT_EQUAL((size_t) 3, backJumpsToPatch.top().backwardsJumpIdx); backJumpsToPatch.pop();
CPPUNIT_ASSERT_EQUAL((size_t) 12, backJumpsToPatch.top().backwardsJumpIdx); backJumpsToPatch.pop();
CPPUNIT_ASSERT_EQUAL((size_t) 32, backJumpsToPatch.top().backwardsJumpIdx); backJumpsToPatch.pop();

}
4 changes: 4 additions & 0 deletions src/unitTests/BytecodeGenerationTest.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ class BytecodeGenerationTest: public CPPUNIT_NS::TestCase {
CPPUNIT_TEST(testInliningWhileLoopsWithExpandingBranches);
CPPUNIT_TEST(testInliningWhileLoopsWithContractingBranches);

CPPUNIT_TEST(testJumpQueuesOrdering);

CPPUNIT_TEST_SUITE_END();

public:
Expand Down Expand Up @@ -116,6 +118,8 @@ class BytecodeGenerationTest: public CPPUNIT_NS::TestCase {
void testInliningWhileLoopsWithExpandingBranches();
void testInliningWhileLoopsWithContractingBranches();

void testJumpQueuesOrdering();

void dump(MethodGenerationContext* mgenc);

void check(std::vector<uint8_t> actual, std::vector<uint8_t> expected);
Expand Down
6 changes: 3 additions & 3 deletions src/vmobjects/VMMethod.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -547,13 +547,13 @@ void VMMethod::AdaptAfterOuterInlined(uint8_t removedCtxLevel, MethodGenerationC
}

bool operator<(const Jump& a, const Jump& b) {
return a.originalJumpTargetIdx < b.originalJumpTargetIdx;
return a.originalJumpTargetIdx > b.originalJumpTargetIdx;
}

bool operator<(const BackJump& a, const BackJump& b) {
return a.loopBeginIdx < b.loopBeginIdx;
return a.loopBeginIdx > b.loopBeginIdx;
}

bool operator<(const BackJumpPatch& a, const BackJumpPatch& b) {
return a.backwardsJumpIdx < b.backwardsJumpIdx;
return a.backwardsJumpIdx > b.backwardsJumpIdx;
}

0 comments on commit 7d6aa4e

Please sign in to comment.