Skip to content

Commit

Permalink
feat: Deconflict var names when two vars of incompatible types share …
Browse files Browse the repository at this point in the history
…the same name

This is needed because our current system doesn't offer proper scope to variables
  • Loading branch information
Col-E committed Jun 25, 2024
1 parent ca02dbb commit 86eb0aa
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 10 deletions.
2 changes: 1 addition & 1 deletion jasm-composition-jvm/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ repositories {
}

dependencies {
api 'com.github.jumanji144:blw:d475899661'
api 'com.github.jumanji144:blw:7a749bdfbd'
implementation 'org.ow2.asm:asm:9.7'
implementation 'org.ow2.asm:asm-util:9.7'

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package me.darknet.assembler.printer;

import dev.xdark.blw.code.Code;
import dev.xdark.blw.type.Type;
import dev.xdark.blw.type.Types;
import me.darknet.assembler.compile.analysis.jvm.IndexedStraightforwardSimulation;
import me.darknet.assembler.helper.Names;
Expand Down Expand Up @@ -43,19 +44,32 @@ public void setLabelPrefix(String labelPrefix) {
}

public Names localNames() {
Map<String, Type> nameToType = new HashMap<>();
List<Names.Local> locals = new ArrayList<>();
boolean isStatic = (method.accessFlags() & AccessFlag.ACC_STATIC) != 0;
Code code = method.code();
if (code != null) {
for (Local localVariable : code.localVariables()) {
// transform local name
String name = escapeName(localVariable.name(), localVariable.index(), isStatic);
locals.add(
new Names.Local(
localVariable.index(), localVariable.start().getIndex(), localVariable.end().getIndex(),
name, localVariable.type().descriptor()
)
);
for (Local local : code.localVariables()) {
// Transform local name to be legal
int index = local.index();
String name = escapeName(local.name(), index, isStatic);
String descriptor = local.type().descriptor();
Type varType = Types.typeFromDescriptor(descriptor);

// De-conflict variable names if two names of incompatible types occupy the same name.
// int foo = 0 ---> foo
// String foo = "" ---> foo2
// byte[] foo = ... ---> foo3
Type existingVarType = nameToType.get(name);
if (existingVarType != null && varType.getClass() != existingVarType.getClass()) {
int i = 2;
String prefix = name;
while (nameToType.get(name) != null)
name = prefix + (i++);
}

locals.add(new Names.Local(index, local.start().getIndex(), local.end().getIndex(), name, descriptor));
nameToType.put(name, varType);
}
}
Map<Integer, String> parameterNames = new HashMap<>();
Expand Down

0 comments on commit 86eb0aa

Please sign in to comment.