Skip to content

Commit de84a79

Browse files
committed
Reserve preserved names during compression
1 parent 1ed1157 commit de84a79

3 files changed

Lines changed: 51 additions & 3 deletions

File tree

de.peeeq.wurstscript/src/main/java/de/peeeq/wurstscript/translation/imoptimizer/ImCompressor.java

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,30 @@
77
import de.peeeq.wurstscript.translation.imtranslation.ImTranslator;
88
import de.peeeq.wurstscript.validation.NamePreservation;
99

10+
import java.util.HashSet;
11+
import java.util.Set;
12+
1013
public class ImCompressor {
1114

1215
private final ImTranslator trans;
1316
private final ImProg prog;
1417
private final NameGenerator ng;
18+
private final Set<String> preservedNames = new HashSet<>();
1519

1620
public ImCompressor(ImTranslator translator) {
1721
this.trans = translator;
1822
this.prog = translator.getImProg();
1923
ng = new NameGenerator();
24+
for (ImVar global : prog.getGlobals()) {
25+
if (NamePreservation.isPreserved(global)) {
26+
preservedNames.add(global.getName());
27+
}
28+
}
29+
for (ImFunction function : ImHelper.calculateFunctionsOfProg(prog)) {
30+
if (NamePreservation.isPreserved(function)) {
31+
preservedNames.add(function.getName());
32+
}
33+
}
2034
}
2135

2236
public void compressNames() {
@@ -32,7 +46,7 @@ public void compressGlobals() {
3246
continue;
3347
}
3448

35-
String replacement = ng.getUniqueToken();
49+
String replacement = nextCompressedName();
3650

3751
global.setName(replacement);
3852
}
@@ -50,12 +64,20 @@ public void compressFunctions() {
5064
// do not rename main and config functions
5165
continue;
5266
}
53-
String rname = ng.getUniqueToken();
67+
String rname = nextCompressedName();
5468
func.setName(rname);
5569
}
5670

5771
}
5872

73+
private String nextCompressedName() {
74+
String replacement;
75+
do {
76+
replacement = ng.getUniqueToken();
77+
} while (preservedNames.contains(replacement));
78+
return replacement;
79+
}
80+
5981
private void compressLocals(ImFunction func) {
6082
// TODO compressing locals should not use the global name pool but use a own pool
6183
for (ImVar local : func.getParameters()) {

de.peeeq.wurstscript/src/main/java/de/peeeq/wurstscript/translation/lua/translation/LuaTranslator.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -492,7 +492,8 @@ private boolean isFixedEntryPoint(ImFunction function) {
492492

493493
private void collectPredefinedNames() {
494494
for (ImFunction function : prog.getFunctions()) {
495-
if (function.isBj() || function.isExtern() || function.isNative()) {
495+
if (function.isBj() || function.isExtern() || function.isNative()
496+
|| NamePreservation.isPreserved(function)) {
496497
// Don't rename Wurst-internal stubs (names starting with __wurst_)
497498
// since their names are intentionally different from their trace's source name.
498499
if (!function.getName().startsWith("__wurst_")) {
@@ -506,6 +507,8 @@ private void collectPredefinedNames() {
506507
if (global.getIsBJ()) {
507508
setNameFromTrace(global);
508509
usedNames.add(global.getName());
510+
} else if (NamePreservation.isPreserved(global)) {
511+
usedNames.add(global.getName());
509512
}
510513
}
511514
}

de.peeeq.wurstscript/src/test/java/tests/wurstscript/tests/OptimizerTests.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -504,6 +504,29 @@ public void preserveNameAnnotationKeepsExternallyCalledFunctionReachable() throw
504504
"Expected an externally-called @preserveName function to survive garbage collection.\n" + output);
505505
}
506506

507+
@Test
508+
public void preservedNamesAreReservedBeforeCompression() throws IOException {
509+
test().optimize().lines(
510+
"package test",
511+
" native testSuccess()",
512+
" function ordinary()",
513+
" testSuccess()",
514+
" @preserveName function w()",
515+
" testSuccess()",
516+
" init",
517+
" ordinary()",
518+
" w()",
519+
"endpackage");
520+
521+
String output = Files.toString(
522+
new File("./test-output/OptimizerTests_preservedNamesAreReservedBeforeCompression_opt.j"),
523+
Charsets.UTF_8);
524+
assertTrue(output.contains("function w"),
525+
"Expected the preserved function name to remain available.\n" + output);
526+
assertFalse(output.contains("function w_1"),
527+
"Expected compression to reserve the preserved name.\n" + output);
528+
}
529+
507530
@Test
508531
public void trvePreservesGlobalDespiteLexicalShadow() throws IOException {
509532
test().optimize().lines(

0 commit comments

Comments
 (0)