Skip to content

Commit

Permalink
Add branch.unaligned to allow users to branch to locations with lower…
Browse files Browse the repository at this point in the history
… stack pointers
  • Loading branch information
DSouzaM committed May 29, 2024
1 parent f676e4f commit a0befe1
Show file tree
Hide file tree
Showing 8 changed files with 225 additions and 173 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -563,7 +563,7 @@ public void testUnreachableBranch() {
}).getRootNode();

assertInstructions(node,
"branch",
"branch.unaligned",
"load.constant",
"return");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1248,7 +1248,7 @@ public void testImplicitJumpAfterReturn() {
});

assertInstructions(node,
"branch",
"branch.unaligned",
"load.constant",
"return");

Expand All @@ -1259,7 +1259,7 @@ public void testImplicitJumpAfterReturn() {
"tag.enter",
"tag.enter",
"tag.leaveVoid",
"branch",
"branch.unaligned",
"tag.leaveVoid",
"load.constant",
"tag.leave",
Expand All @@ -1271,10 +1271,10 @@ public void testImplicitJumpAfterReturn() {
// instrumentation events should be correct even if we hit a trap
assertEvents(node,
events,
new Event(EventKind.ENTER, 0x0000, 0x0012, null, RootTag.class),
new Event(EventKind.ENTER, 0x0002, 0x0008, null, ExpressionTag.class),
new Event(EventKind.RETURN_VALUE, 0x0002, 0x0008, null, ExpressionTag.class),
new Event(EventKind.RETURN_VALUE, 0x0000, 0x0012, null, RootTag.class));
new Event(EventKind.ENTER, 0x0000, 0x0013, null, RootTag.class),
new Event(EventKind.ENTER, 0x0002, 0x0009, null, ExpressionTag.class),
new Event(EventKind.RETURN_VALUE, 0x0002, 0x0009, null, ExpressionTag.class),
new Event(EventKind.RETURN_VALUE, 0x0000, 0x0013, null, RootTag.class));

}

Expand Down Expand Up @@ -1363,7 +1363,7 @@ public void testImplicitJump() {
});

assertInstructions(node,
"branch",
"branch.unaligned",
"load.constant",
"return");

Expand All @@ -1376,7 +1376,7 @@ public void testImplicitJump() {
"tag.enter",
"tag.enter",
"tag.leaveVoid",
"branch",
"branch.unaligned",
"tag.leaveVoid",
"load.constant",
"tag.leave",
Expand All @@ -1388,10 +1388,10 @@ public void testImplicitJump() {

assertEvents(node,
events,
new Event(EventKind.ENTER, 0x0000, 0x0010, null, RootTag.class),
new Event(EventKind.ENTER, 0x0002, 0x0008, null, ExpressionTag.class),
new Event(EventKind.RETURN_VALUE, 0x0002, 0x0008, null, ExpressionTag.class),
new Event(EventKind.RETURN_VALUE, 0x0000, 0x0010, 42, RootTag.class));
new Event(EventKind.ENTER, 0x0000, 0x0011, null, RootTag.class),
new Event(EventKind.ENTER, 0x0002, 0x0009, null, ExpressionTag.class),
new Event(EventKind.RETURN_VALUE, 0x0002, 0x0009, null, ExpressionTag.class),
new Event(EventKind.RETURN_VALUE, 0x0000, 0x0011, 42, RootTag.class));
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@

import static org.junit.Assert.assertEquals;

import org.junit.Ignore;
import org.junit.Test;

import com.oracle.truffle.api.RootCallTarget;
Expand Down Expand Up @@ -123,15 +124,15 @@ public void testBranchBackward() {
}

@Test
public void testBranchOutwardValid() {
public void testBranchOutwardBalanced() {
// {
// if(arg0 < 0) goto lbl;
// return 123;
// }
// lbl:
// return 42;

RootCallTarget root = parse("branchOutwardValid", b -> {
RootCallTarget root = parse("branchOutwardBalanced", b -> {
b.beginRoot(LANGUAGE);

BytecodeLabel lbl = b.createLabel();
Expand All @@ -153,7 +154,7 @@ public void testBranchOutwardValid() {

b.emitLabel(lbl);

emitReturn(b, 42);
emitReturn(b, 42L);

b.endRoot();
});
Expand All @@ -163,14 +164,12 @@ public void testBranchOutwardValid() {
}

@Test
public void testBranchOutwardInvalid() {
public void testBranchOutwardUnbalanced() {
// return 1 + { goto lbl; 2 }
// lbl:
// return 0;
// return 42;

thrown.expect(IllegalStateException.class);
thrown.expectMessage("BytecodeLabel was emitted at a position with a different stack height than a branch instruction that targets it. Expected stack height 1, but was 0. Branches must be balanced.");
parse("branchOutwardInvalid", b -> {
RootCallTarget root = parse("branchOutwardUnbalanced", b -> {
b.beginRoot(LANGUAGE);

BytecodeLabel lbl = b.createLabel();
Expand All @@ -187,11 +186,12 @@ public void testBranchOutwardInvalid() {

b.emitLabel(lbl);

emitReturn(b, 0);
emitReturn(b, 42L);

b.endRoot();
});

assertEquals(42L, root.call());
}

@Test
Expand Down Expand Up @@ -234,7 +234,7 @@ public void testBranchBalancedStack() {
// result
// };

RootCallTarget root = parse("branchInvalidStack", b -> {
RootCallTarget root = parse("branchBalancedStack", b -> {
b.beginRoot(LANGUAGE);
b.beginReturn();
b.beginAddOperation();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -352,7 +352,6 @@ public void testFinallyTryBranchForwardOutOfHandler() {
}

@Test
@Ignore("unbalanced branches not yet supported")
public void testFinallyTryBranchForwardOutOfHandlerUnbalanced() {
/**
* This test is the same as the previous, but because of the "return 0",
Expand Down Expand Up @@ -1074,7 +1073,6 @@ public void testFinallyTryBranchIntoOuterFinally() {


@Test
@Ignore("unbalanced branches not yet supported")
public void testFinallyTryBranchIntoOuterFinallyUnbalanced() {
/**
* This test is the same as the previous, but because of the "return 0"'s,
Expand Down Expand Up @@ -1215,7 +1213,6 @@ public void testFinallyTryBranchIntoOuterFinallyNestedInAnotherFinally() {
}

@Test
@Ignore("unbalanced branches not yet supported")
public void testFinallyTryBranchIntoOuterFinallyNestedInAnotherFinallyUnbalanced() {
/**
* This test is the same as the previous, but because of the "return 0"'s in handlers b and c,
Expand Down Expand Up @@ -1299,7 +1296,7 @@ public void testFinallyTryBranchIntoOuterFinallyNestedInAnotherFinallyUnbalanced
b.endRoot();
});

testOrdering(false, root, 1L, 3L, 5L, 6L, 8L, 11L, 12L);
testOrdering(false, root, 1L, 3L, 5L, 6L, 8L, 11L);
}

@Test
Expand Down
Loading

0 comments on commit a0befe1

Please sign in to comment.