-
Notifications
You must be signed in to change notification settings - Fork 29
Stop a merged cycle allocating a parameter slot it already has #1252
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
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
185 changes: 185 additions & 0 deletions
185
de.peeeq.wurstscript/src/test/java/tests/wurstscript/tests/CyclicFunctionTests.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,185 @@ | ||
| package tests.wurstscript.tests; | ||
|
|
||
| import com.google.common.base.Charsets; | ||
| import com.google.common.io.Files; | ||
| import org.testng.annotations.Test; | ||
|
|
||
| import java.io.File; | ||
| import java.io.IOException; | ||
| import java.util.regex.Matcher; | ||
| import java.util.regex.Pattern; | ||
|
|
||
| /** | ||
| * Jass has no forward declaration, so mutually recursive functions cannot be emitted as themselves: | ||
| * a cycle is merged into one function which takes a choice of which body to run plus the parameters | ||
| * of all of them. That merged signature has to fit in a Jass function, and nothing used to check that | ||
| * it did. | ||
| */ | ||
| public class CyclicFunctionTests extends WurstScriptTest { | ||
|
|
||
| /** A Jass function takes at most 31 parameters, and a tuple counts as one per component. */ | ||
| private static final int JASS_MAX_PARAMETERS = 31; | ||
|
|
||
| /** | ||
| * Two functions in a cycle taking the same parameters in a different order. | ||
| * <p> | ||
| * Sharing a slot used to be looked for from a position which only moved forwards, so a parameter | ||
| * matching a slot late in the union pushed every parameter after it past everything already there: | ||
| * {@code pong} matched its {@code int} against the last slot and then had five tuples left with | ||
| * nowhere behind it to go. Eleven slots for six parameters, 32 Jass parameters for a signature | ||
| * which needs 17, and the emitted function was over the limit while the IM function looked fine. | ||
| * <p> | ||
| * Reordering arguments between two functions which call each other is ordinary, so this is not a | ||
| * corner: it is two functions and one swap. | ||
| */ | ||
| @Test | ||
| public void mutuallyRecursiveFunctionsSharingParametersFitTheJassLimit() throws IOException { | ||
| test().executeProg().lines( | ||
| "package test", | ||
| "native testSuccess()", | ||
| "tuple vec3(real x, real y, real z)", | ||
| "function ping(vec3 a, vec3 b, vec3 c, vec3 d, vec3 e, int n) returns int", | ||
| " if n <= 0", | ||
| " return 0", | ||
| " return pong(n - 1, a, b, c, d, e)", | ||
| "function pong(int n, vec3 a, vec3 b, vec3 c, vec3 d, vec3 e) returns int", | ||
| " if n <= 0", | ||
| " return 1", | ||
| " return ping(a, b, c, d, e, n - 1)", | ||
| "init", | ||
| " vec3 v = vec3(1., 2., 3.)", | ||
| " if ping(v, v, v, v, v, 3) == 1", | ||
| " testSuccess()" | ||
| ); | ||
| String jass = compiledJass("mutuallyRecursiveFunctionsSharingParametersFitTheJassLimit"); | ||
| assertNoFunctionExceedsTheJassLimit(jass); | ||
| // choice, one int, and five tuples of three reals: what the wider of the two needs. | ||
| assertMergedCycleTakes(jass, 17); | ||
| } | ||
|
|
||
| /** | ||
| * The same shape across more of the cycle, since the union is built one function at a time and a | ||
| * slot the next function cannot reach is a slot every function after it cannot reach either. | ||
| */ | ||
| @Test | ||
| public void aLongerCycleReusesSlotsRatherThanAccumulatingThem() throws IOException { | ||
| test().executeProg().lines( | ||
| "package test", | ||
| "native testSuccess()", | ||
| "tuple pair(real x, real y)", | ||
| "function first(pair a, pair b, pair c, int n, string s) returns int", | ||
| " if n <= 0", | ||
| " return 7", | ||
| " return second(s, a, n - 1, b, c)", | ||
| "function second(string s, pair a, int n, pair b, pair c) returns int", | ||
| " if n <= 0", | ||
| " return 7", | ||
| " return third(a, n - 1, b, s, c)", | ||
| "function third(pair a, int n, pair b, string s, pair c) returns int", | ||
| " if n <= 0", | ||
| " return 7", | ||
| " return first(a, b, c, n - 1, s)", | ||
| "init", | ||
| " pair p = pair(1., 2.)", | ||
| " if first(p, p, p, 6, \"x\") == 7", | ||
| " testSuccess()" | ||
| ); | ||
| String jass = compiledJass("aLongerCycleReusesSlotsRatherThanAccumulatingThem"); | ||
| assertNoFunctionExceedsTheJassLimit(jass); | ||
| // choice, three tuples of two reals, one int and one string. It took fifteen before. | ||
| assertMergedCycleTakes(jass, 9); | ||
| } | ||
|
|
||
| /** | ||
| * Audit probe for the sharing this got more aggressive about. Slots are shared by Jass type, so a | ||
| * class reference and an {@code int} are interchangeable and so are two tuples with the same | ||
| * components - the slot keeps the IM type of whichever parameter claimed it first, and the other | ||
| * function's body then reads a variable typed as something else. Searching backwards reaches those | ||
| * slots far more often than searching forwards did, so what used to be rare is now the common case. | ||
| * <p> | ||
| * Every parameter here is a mismatch of that kind: {@code down} takes an int where the slot is a | ||
| * class, a point where the slot is a pair, and a class where the slot is an int. | ||
| */ | ||
| @Test | ||
| public void slotsSharedBetweenUnlikeTypesWithTheSameJassTypeStillCarryTheirValues() throws IOException { | ||
| test().executeProg().lines( | ||
| "package test", | ||
| "native testSuccess()", | ||
| "tuple pair(real x, real y)", | ||
| "tuple point(real a, real b)", | ||
| "class Foo", | ||
| " int v", | ||
| " construct(int v)", | ||
| " this.v = v", | ||
| "function up(Foo f, pair p, int n) returns int", | ||
| " if n <= 0", | ||
| " return f.v", | ||
| " return down(n - 1, point(p.x, p.y), f)", | ||
| "function down(int n, point q, Foo f) returns int", | ||
| " if n <= 0", | ||
| " return f.v", | ||
| " return up(f, pair(q.a, q.b), n - 1)", | ||
| "init", | ||
| " Foo f = new Foo(5)", | ||
| " if up(f, pair(1., 2.), 3) == 5", | ||
| " testSuccess()" | ||
| ); | ||
| String jass = compiledJass("slotsSharedBetweenUnlikeTypesWithTheSameJassTypeStillCarryTheirValues"); | ||
| assertNoFunctionExceedsTheJassLimit(jass); | ||
| // Three slots for six parameters, every one of them shared across a type mismatch: an integer | ||
| // for the class or the count, a tuple, and an integer for the other. Five Jass parameters with | ||
| // the choice, since the tuple is two of them. | ||
| assertMergedCycleTakes(jass, 5); | ||
| } | ||
|
|
||
| private String compiledJass(String testName) throws IOException { | ||
| return Files.toString( | ||
| new File(TEST_OUTPUT_PATH + "CyclicFunctionTests_" + testName + "_no_opts.j"), | ||
| Charsets.UTF_8); | ||
| } | ||
|
|
||
| private static final Pattern FUNCTION_HEADER = | ||
| Pattern.compile("(?m)^function\\s+(\\w+)\\s+takes\\s+(.*?)\\s+returns\\s"); | ||
|
|
||
| /** | ||
| * Counts the parameters the emitted Jass actually declares, which is the number the game applies | ||
| * its limit to - the IM function's parameter count is smaller wherever a tuple is passed. | ||
| */ | ||
| private static void assertNoFunctionExceedsTheJassLimit(String jass) { | ||
| Matcher header = FUNCTION_HEADER.matcher(jass); | ||
| while (header.find()) { | ||
| String parameters = header.group(2); | ||
| if (parameters.equals("nothing")) { | ||
| continue; | ||
| } | ||
| int count = parameters.split(",").length; | ||
| if (count > JASS_MAX_PARAMETERS) { | ||
| throw new AssertionError("function '" + header.group(1) + "' takes " + count | ||
| + " parameters, and Jass allows " + JASS_MAX_PARAMETERS | ||
| + "\n" + header.group()); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| private static final Pattern MERGED_CYCLE = | ||
| Pattern.compile("(?m)^function\\s+(cyc_\\w+)\\s+takes\\s+(.*?)\\s+returns\\s"); | ||
|
|
||
| /** | ||
| * The merged function takes exactly the parameters the cycle needs live at once. | ||
| * <p> | ||
| * Staying under the limit is the requirement, but a signature which is merely under it is how this | ||
| * got here: the slack was being spent one duplicate slot at a time, per function, and only a long | ||
| * enough cycle made it visible. The count is asserted so that spending starts failing again. | ||
| */ | ||
| private static void assertMergedCycleTakes(String jass, int expectedParameters) { | ||
| Matcher merged = MERGED_CYCLE.matcher(jass); | ||
| if (!merged.find()) { | ||
| throw new AssertionError("no merged cycle function in the emitted Jass"); | ||
| } | ||
| int count = merged.group(2).equals("nothing") ? 0 : merged.group(2).split(",").length; | ||
| if (count != expectedParameters) { | ||
| throw new AssertionError("merged cycle '" + merged.group(1) + "' takes " + count | ||
| + " parameters, expected " + expectedParameters + "\n" + merged.group()); | ||
| } | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When a cycle member's parameter order differs from the merged slot order and its call arguments have side effects, this loop moves the expressions themselves into slot order. For example, with merged slots
[int, string], a call to a member declared(string, int)asmember(nextString(), nextInt())becomesmerged(..., nextInt(), nextString()); the subsequent flattening pass therefore evaluates the mutations in the opposite order and can change program results. Evaluate the original arguments in their original order into temporaries before scattering those values into merged slots.Useful? React with 👍 / 👎.