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 @@ -566,48 +566,138 @@ private void emitCompiletimeObjectAllocs() {

objs.sort(Comparator.comparingInt(o -> o.object.getObjectId()));

// Lua has one allocator for every class; its intrinsic empties the free list along with each
// new maximum, so it is called before the first object even when that one needs no gap.
@Nullable ClassManagementVars mVars = entry.getKey() instanceof ClassManagementVars v ? v : null;
if (mVars != null) {
// Ensure replayed allocations are driven by maxIndex and not by stale free-list state.
objectInits.add(JassIm.ImSet(objs.get(0).object.getTrace(),
JassIm.ImVarAccess(mVars.freeCount), JassIm.ImIntVal(0)));
// The ids are unique in the interpreter's id space: the inheritance group of the erased classes on Jass
// (ProgramState.classKey), which the groups after the generics divide, and one space on Lua.
int finalMax = globalState.getMaxAllocatedId(objs.get(0).object.getImClass());
@Nullable ClassManagementVars erasedGroup = entry.getKey() instanceof ClassManagementVars v ? v : null;
if (erasedGroup == null) {
emitLuaObjectAllocs(objs, finalMax, objectInits);
} else {
emitJassObjectAllocs(objs, finalMax, erasedGroup, objectInits);
}
}

int currentMax = 0;
int finalMax = globalState.getMaxAllocatedId(objs.get(0).object.getImClass());
getCompiletimeStateInitFunction().getBody().addAll(0, objectInits);
}

for (CompiletimeObjectInit init : objs) {
int desiredId = init.object.getObjectId();
int targetMax = desiredId - 1;
if (targetMax > currentMax || (mVars == null && init == objs.get(0))) {
objectInits.add(setMaxObjectId(init.object.getTrace(), mVars, targetMax));
currentMax = targetMax;
}
/**
* Lua has one allocator for every class; its intrinsic empties the free list along with each new maximum, so it
* is called before the first object even when that one needs no gap.
*/
private void emitLuaObjectAllocs(List<CompiletimeObjectInit> objs, int finalMax, List<ImStmt> objectInits) {
int currentMax = 0;
for (CompiletimeObjectInit init : objs) {
int desiredId = init.object.getObjectId();
int targetMax = desiredId - 1;
if (targetMax > currentMax || init == objs.get(0)) {
objectInits.add(JassIm.ImFunctionCall(init.object.getTrace(), translator.luaResumeObjectIdsFunc(),
JassIm.ImTypeArguments(), JassIm.ImExprs(JassIm.ImIntVal(targetMax)), false, CallType.NORMAL));
}
objectInits.add(allocate(init));
currentMax = desiredId;
}
if (finalMax > currentMax) {
objectInits.add(JassIm.ImFunctionCall(objs.get(0).object.getTrace(), translator.luaResumeObjectIdsFunc(),
JassIm.ImTypeArguments(), JassIm.ImExprs(JassIm.ImIntVal(finalMax)), false, CallType.NORMAL));
}
}

ImAlloc alloc = JassIm.ImAlloc(init.object.getTrace(), init.object.getType());
ImSet assign = JassIm.ImSet(init.object.getTrace(), JassIm.ImVarAccess(init.targetVar), alloc);
objectInits.add(assign);
imProg.getGlobalInits().put(init.targetVar, Collections.singletonList(assign));
currentMax = desiredId;
/**
* Jass has an allocator per inheritance group of the classes after the generics are eliminated, which make a class
* of each instantiation of a new-generic class. Those groups divide the group of the erased classes the objects
* were numbered in, and are known only then, so the replay names the class of each write
* ({@link ImTranslator#jassSetObjectIdMaxFunc}) and the class elimination writes that group's allocator.
* <p>
* Each allocator empties its free list before its first object, and is set to an object's id - 1 before the
* object unless it already is: the objects come in the order of their ids, so the allocator's highest id is below
* the object's. With a new-generic class in the erased group, that is tracked for each instantiation, since the
* generics may put each into a group of its own; instantiations which share a group then write it more often
* than needed.
* <p>
* Each allocator ends at the highest id of the erased group, so the ids of the objects dropped or destroyed at
* compile time are not handed out again, as for a plain class, and neither are the ids another instantiation took:
* a compile-time value may hold any of them. With a new-generic class in the erased group that holds for every
* class the generics make of it, those without a replayed object and those only the run time allocates included,
* which only the class elimination knows ({@link ImTranslator#jassAdvanceObjectIds}).
*/
private void emitJassObjectAllocs(List<CompiletimeObjectInit> objs, int finalMax, ClassManagementVars erasedGroup,
List<ImStmt> objectInits) {
boolean perInstantiation = groupHasNewGenericClass(erasedGroup);
Map<Object, Integer> lastId = new LinkedHashMap<>();
Map<Object, CompiletimeObjectInit> firstObject = new LinkedHashMap<>();
for (CompiletimeObjectInit init : objs) {
ImClassType type = init.object.getType();
Object allocator = perInstantiation
? new Instantiation(type.getClassDef(), new GenericTypes(type.getTypeArguments()))
: SHARED_ERASED_GROUP;
int desiredId = init.object.getObjectId();
Integer last = lastId.get(allocator);
if (last == null) {
// Ensure replayed allocations are driven by maxIndex and not by stale free-list state.
objectInits.add(objectIdIntrinsicCall(init, translator.jassClearObjectFreeListFunc()));
firstObject.put(allocator, init);
last = 0;
}
if (desiredId - 1 > last) {
objectInits.add(objectIdIntrinsicCall(init, translator.jassSetObjectIdMaxFunc(),
JassIm.ImIntVal(desiredId - 1)));
}
objectInits.add(allocate(init));
lastId.put(allocator, desiredId);
}
if (perInstantiation) {
objectInits.add(translator.jassAdvanceObjectIds(objs.get(0).object.getTrace(),
classesOfGroup(erasedGroup), finalMax));
return;
}
for (Map.Entry<Object, Integer> e : lastId.entrySet()) {
if (finalMax > e.getValue()) {
objectInits.add(objectIdIntrinsicCall(firstObject.get(e.getKey()), translator.jassSetObjectIdMaxFunc(),
Comment thread
Frotty marked this conversation as resolved.
Resolved
JassIm.ImIntVal(finalMax)));
}
}
}

if (finalMax > currentMax) {
objectInits.add(setMaxObjectId(objs.get(0).object.getTrace(), mVars, finalMax));
/** The classes of the erased group, in the order of the program. */
private List<ImClass> classesOfGroup(ClassManagementVars erasedGroup) {
List<ImClass> result = new ArrayList<>();
for (ImClass c : imProg.getClasses()) {
if (translator.getClassManagementVarsFor(c) == erasedGroup) {
result.add(c);
}
}
return result;
}

getCompiletimeStateInitFunction().getBody().addAll(0, objectInits);
/** The key of the one allocator of an erased group which has no new-generic class. */
private static final Object SHARED_ERASED_GROUP = new Object();

/** A class with its type arguments, compared as the specialisation tells instantiations apart. */
private record Instantiation(ImClass clazz, GenericTypes typeArguments) {
}

/** The next allocation is maxId + 1: Jass sets the group's maxIndex, Lua resumes its one allocator. */
private ImStmt setMaxObjectId(Element trace, @Nullable ClassManagementVars mVars, int maxId) {
if (mVars != null) {
return JassIm.ImSet(trace, JassIm.ImVarAccess(mVars.maxIndex), JassIm.ImIntVal(maxId));
/** Whether the generics can divide the group: whether a class in it is a new-generic class. */
private boolean groupHasNewGenericClass(ClassManagementVars erasedGroup) {
for (ImClass c : imProg.getClasses()) {
if (!c.getTypeVariables().isEmpty() && translator.getClassManagementVarsFor(c) == erasedGroup) {
return true;
}
}
return JassIm.ImFunctionCall(trace, translator.luaResumeObjectIdsFunc(), JassIm.ImTypeArguments(),
JassIm.ImExprs(JassIm.ImIntVal(maxId)), false, CallType.NORMAL);
return false;
}

private ImStmt objectIdIntrinsicCall(CompiletimeObjectInit init, ImFunction intrinsic, ImExpr... values) {
ImExprs arguments = JassIm.ImExprs(JassIm.ImTypeIdOfClass((ImClassType) init.object.getType().copy()));
arguments.addAll(Arrays.asList(values));
return JassIm.ImFunctionCall(init.object.getTrace(), intrinsic, JassIm.ImTypeArguments(), arguments, false,
CallType.NORMAL);
}

private ImStmt allocate(CompiletimeObjectInit init) {
ImAlloc alloc = JassIm.ImAlloc(init.object.getTrace(), init.object.getType());
ImSet assign = JassIm.ImSet(init.object.getTrace(), JassIm.ImVarAccess(init.targetVar), alloc);
imProg.getGlobalInits().put(init.targetVar, Collections.singletonList(assign));
return assign;
}

private static class CompiletimeObjectInit {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ public void eliminateClasses() {
for (ImFunction f : new ArrayList<>(prog.getFunctions())) {
eliminateClassRelatedExprs(f.getBody());
}
translator.removeJassObjectIdIntrinsics();

prog.getClasses().clear();
prog.getMethods().clear();
Expand Down Expand Up @@ -580,6 +581,7 @@ private void eliminateClassRelatedExprs(de.peeeq.wurstscript.jassIm.Element body
final List<ImTypeIdOfObj> typeIdObjs = Lists.newArrayList();
final List<ImTypeIdOfClass> typeIdClasses = Lists.newArrayList();
final List<ImFunctionCall> nativeCalls = Lists.newArrayList();
final List<ImFunctionCall> objectIdCalls = Lists.newArrayList();
body.accept(new de.peeeq.wurstscript.jassIm.Element.DefaultVisitor() {
@Override
public void visit(ImMemberAccess e) {
Expand Down Expand Up @@ -625,8 +627,14 @@ public void visit(ImTypeIdOfObj e) {

@Override
public void visit(ImFunctionCall fc) {
super.visit(fc);
ImFunction func = fc.getFunc();
if (func == translator.jassSetObjectIdMaxFunc || func == translator.jassClearObjectFreeListFunc
|| translator.objectIdAdvanceGroup(func) != null) {
// its class argument names the allocator and is replaced along with the call
objectIdCalls.add(fc);
return;
}
super.visit(fc);
if (!func.hasFlag(FunctionFlagEnum.IS_NATIVE)) {
return;
}
Expand All @@ -635,6 +643,9 @@ public void visit(ImFunctionCall fc) {
}
}
});
for (ImFunctionCall call : objectIdCalls) {
replaceObjectIdCall(call);
}
for (ImMemberAccess ma : mas) {
replaceMemberAccess(ma);
}
Expand Down Expand Up @@ -681,6 +692,52 @@ private void rewriteObjectNative(ImFunctionCall nativeCall) {
}
}

/**
* A call of {@link ImTranslator#jassSetObjectIdMaxFunc} or {@link ImTranslator#jassClearObjectFreeListFunc} as the
* write to the allocator of its class: the variables of the class's group, which exist only now that the generics
* are eliminated.
*/
private void replaceObjectIdCall(ImFunctionCall call) {
List<ImClass> erasedGroup = translator.objectIdAdvanceGroup(call.getFunc());
if (erasedGroup != null) {
replaceAdvanceObjectIds(call, erasedGroup);
return;
}
if (!(call.getArguments().get(0) instanceof ImTypeIdOfClass typeIdOfClass)) {
throw new CompileError(call.attrTrace().attrSource(),
call.getFunc().getName() + " expects the type id of a class, not " + call.getArguments().get(0));
}
ClassManagementVars mVars = translator.getClassManagementVarsFor(typeIdOfClass.getClazz().getClassDef());
ImSet set;
if (call.getFunc() == translator.jassSetObjectIdMaxFunc) {
ImExpr maxId = call.getArguments().get(1);
maxId.setParent(null);
set = JassIm.ImSet(call.attrTrace(), JassIm.ImVarAccess(mVars.maxIndex), maxId);
} else {
set = JassIm.ImSet(call.attrTrace(), JassIm.ImVarAccess(mVars.freeCount), JassIm.ImIntVal(0));
}
call.replaceBy(set);
}

/**
* A call of a {@code __wurst_advanceObjectIds} intrinsic ({@link ImTranslator#jassAdvanceObjectIds}) as the write to the maxIndex of every group which holds a
* class of its erased group or a class the generics made of one, in the order of the program's classes.
*/
private void replaceAdvanceObjectIds(ImFunctionCall call, List<ImClass> erasedGroup) {
Set<ClassManagementVars> groups = new LinkedHashSet<>();
for (ImClass c : prog.getClasses()) {
if (erasedGroup.contains(translator.canonical(c))) {
groups.add(translator.getClassManagementVarsFor(c));
}
}
ImExpr maxId = call.getArguments().get(0);
List<ImStmt> sets = new ArrayList<>();
for (ClassManagementVars mVars : groups) {
sets.add(JassIm.ImSet(call.attrTrace(), JassIm.ImVarAccess(mVars.maxIndex), maxId.copy()));
}
call.replaceByAll(sets);
}

private void replaceTypeIdOfObj(ImTypeIdOfObj e) {
ImVar typeIdVar = translator.getClassManagementVarsFor(e.getClazz().getClassDef()).typeId;
ImExpr obj = e.getObj();
Expand Down
Loading
Loading