Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ public class NameGenerator {
*/
private final String charmapFirst = "wurstiScoOlbypeqandfRTYGghFkjxvmQWEZUIPADHJKLXCVBNM";
private final String charmap = charmapFirst + "3142567890";
private final String charmapMid = charmap + "_";

/**
* A counter
Expand All @@ -28,7 +27,6 @@ public class NameGenerator {
*/
private final int lengthFirst;
private final int length;
private final int lengthMid;


/**
Expand All @@ -39,9 +37,7 @@ public class NameGenerator {
public NameGenerator() {
checkCharmap(charmapFirst);
checkCharmap(charmap);
checkCharmap(charmapMid);
length = charmap.length();
lengthMid = charmapMid.length();
lengthFirst = charmapFirst.length();
}

Expand All @@ -66,12 +62,11 @@ public String getToken() {
StringBuilder b = new StringBuilder();
b.append(charmapFirst.charAt(id % lengthFirst));
if ((id /= lengthFirst) != 0) {
// One radix for every position after the first. Choosing the alphabet per digit while
// always dividing by the same length made distinct ids collide, and the compressor
// relies on this stream being unique.
do {
if (id > lengthMid) {
b.append(charmapMid.charAt((id - 1) % lengthMid));
} else {
b.append(charmap.charAt((id - 1) % length));
}
b.append(charmap.charAt((id - 1) % length));
} while ((id /= length) != 0);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import de.peeeq.wurstscript.jassIm.*;
import de.peeeq.wurstscript.translation.imoptimizer.ImInliner;
import de.peeeq.wurstscript.translation.imoptimizer.ImOptimizer;
import de.peeeq.wurstscript.translation.imoptimizer.NameGenerator;
import de.peeeq.wurstscript.translation.imoptimizer.SideEffectFreeNatives;
import de.peeeq.wurstscript.translation.imtranslation.CallType;
import de.peeeq.wurstscript.translation.imtranslation.ImTranslator;
Expand Down Expand Up @@ -840,6 +841,48 @@ public void nativeNamesAreReservedBeforeCompression() throws IOException {
"Compression must not reuse a native API name.\n" + output);
}

@Test
public void compressedNamesAreUnique() {
// globals, functions and locals all draw from this one stream
NameGenerator generator = new NameGenerator();
java.util.Set<String> seen = new java.util.HashSet<>();
for (int i = 0; i < 200000; i++) {
String token = generator.getUniqueToken();
assertTrue(seen.add(token), "Token " + token + " was handed out twice, the second time as token " + i);
}
}

@Test
public void compressionGivesThousandsOfGlobalsDistinctNames() throws IOException {
// more compressed names than the 3213 at which the generator used to repeat a token
int count = 3300;
List<String> lines = new ArrayList<>();
lines.add("package test");
lines.add(" native consume(int i)");
for (int i = 0; i < count; i++) {
lines.add(" int g" + i + " = " + i);
}
lines.add(" init");
for (int i = 0; i < count; i++) {
lines.add(" consume(g" + i + ")");
}
lines.add("endpackage");
test().optimize().lines(lines.toArray(new String[0]));

String output = Files.toString(
new File("./test-output/OptimizerTests_compressionGivesThousandsOfGlobalsDistinctNames_no_opts.j"),
Charsets.UTF_8);
// the backend appends a suffix such as _1 to a name it has already declared
java.util.regex.Matcher global = java.util.regex.Pattern.compile("(?m)^integer (\\w+)=").matcher(output);
int globals = 0;
while (global.find()) {
globals++;
assertFalse(global.group(1).contains("_"),
"Compression handed out the name of global " + global.group(1) + " twice.");
}
assertEquals(globals, count);
}

@Test
public void reforged301NativesAreClassified() {
for (String name : java.util.Set.of(
Expand Down
Loading