Skip to content

Commit

Permalink
Merge pull request #41409 from ballerina-platform/fix-file-too-large-…
Browse files Browse the repository at this point in the history
…issue-41279

Move object method bodies into a separate class when original class is too large
  • Loading branch information
warunalakshitha authored Nov 21, 2023
2 parents 03b5fb1 + 35bc64f commit 88bb122
Show file tree
Hide file tree
Showing 37 changed files with 16,536 additions and 83 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ public class ErrorValue extends BError implements RefValue {
private final Object details;

private static final String GENERATE_OBJECT_CLASS_PREFIX = "$value$";
private static final String SPLIT_CLASS_SUFFIX_REGEX = "\\$split\\$\\d";
private static final String GENERATE_PKG_INIT = "___init_";
private static final String GENERATE_PKG_START = "___start_";
private static final String GENERATE_PKG_STOP = "___stop_";
Expand Down Expand Up @@ -441,7 +442,7 @@ private Optional<StackTraceElement> filterStackTraceElement(StackTraceElement st
}

private String cleanupClassName(String className) {
return className.replace(GENERATE_OBJECT_CLASS_PREFIX, "");
return className.replace(GENERATE_OBJECT_CLASS_PREFIX, "").replaceAll(SPLIT_CLASS_SUFFIX_REGEX, "");
}

private boolean isCompilerAddedName(String name) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@
import static io.ballerina.projects.util.ProjectConstants.BIN_DIR_NAME;
import static io.ballerina.projects.util.ProjectConstants.DOT;
import static io.ballerina.projects.util.ProjectUtils.getThinJarFileName;
import static org.wso2.ballerinalang.compiler.bir.codegen.JvmConstants.CLASS_FILE_SUFFIX;

/**
* This class represents the Ballerina compiler backend that produces executables that runs on the JVM.
Expand Down Expand Up @@ -756,7 +757,7 @@ public String getWarning(boolean listClasses) {
}

private void addConflictedJars(JarLibrary jarLibrary, HashMap<String, JarLibrary> copiedEntries, String entryName) {
if (entryName.endsWith(".class") && !entryName.endsWith("module-info.class")) {
if (entryName.endsWith(CLASS_FILE_SUFFIX) && !entryName.endsWith("module-info.class")) {
JarLibrary conflictingJar = copiedEntries.get(entryName);

// Ignore if conflicting jars has same name
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
*/
public class ProjectConstants {

private ProjectConstants() {}

public static final String BLANG_SOURCE_EXT = ".bal";
public static final String BALA_EXTENSION = ".bala";
public static final String PLATFORM = "platform";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -376,15 +376,20 @@ private static String cleanupSourceFileName(String name) {
}

public static String getMethodDesc(List<BType> paramTypes, BType retType) {
return INITIAL_METHOD_DESC + populateMethodDesc(paramTypes) + generateReturnType(retType);
return INITIAL_METHOD_DESC + getMethodDescParams(paramTypes) + generateReturnType(retType);
}

public static String getMethodDesc(List<BType> paramTypes, BType retType, BType attachedType) {
return INITIAL_METHOD_DESC + getArgTypeSignature(attachedType) + populateMethodDesc(paramTypes) +
return INITIAL_METHOD_DESC + getArgTypeSignature(attachedType) + getMethodDescParams(paramTypes) +
generateReturnType(retType);
}

public static String populateMethodDesc(List<BType> paramTypes) {
public static String getMethodDesc(List<BType> paramTypes, BType retType, String attachedTypeClassName) {
return INITIAL_METHOD_DESC + "L" + attachedTypeClassName + ";" + getMethodDescParams(paramTypes) +
generateReturnType(retType);
}

public static String getMethodDescParams(List<BType> paramTypes) {
StringBuilder descBuilder = new StringBuilder();
for (BType type : paramTypes) {
descBuilder.append(getArgTypeSignature(type));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,7 @@ public class JvmConstants {

// code generation related constants.
public static final String MODULE_INIT_CLASS_NAME = "$_init";
public static final String OBJECT_SELF_INSTANCE = "self";
public static final String UNION_TYPE_CONSTANT_CLASS_NAME = "constants/$_union_type_constants";
public static final String ERROR_TYPE_CONSTANT_CLASS_NAME = "constants/$_error_type_constants";
public static final String TUPLE_TYPE_CONSTANT_CLASS_NAME = "constants/$_tuple_type_constants";
Expand Down Expand Up @@ -380,10 +381,13 @@ public class JvmConstants {
public static final String START_OF_HEADING_WITH_SEMICOLON = ":\u0001";
public static final String CREATE_INTEROP_ERROR_METHOD = "createInteropError";
public static final String LAMBDA_PREFIX = "$lambda$";
public static final String SPLIT_CLASS_SUFFIX = "$split$";
public static final String POPULATE_METHOD_PREFIX = "$populate";
public static final String ADD_METHOD = "add";
public static final String TEST_EXECUTION_STATE = "__gH7W16nQmp0TestExecState__";
public static final String GET_TEST_EXECUTION_STATE = "$getTestExecutionState";
public static final String STRAND_LOCAL_VARIABLE_NAME = "__strand";
public static final String CLASS_FILE_SUFFIX = ".class";

// scheduler related constants
public static final String SCHEDULE_FUNCTION_METHOD = "scheduleFunction";
Expand Down Expand Up @@ -446,6 +450,7 @@ public class JvmConstants {
public static final int MAX_CALLS_PER_CLIENT_METHOD = 100;
public static final int MAX_CONSTANTS_PER_METHOD = 100;
public static final int MAX_CALLS_PER_FUNCTION_CALL_METHOD = 100;
public static final int MAX_METHOD_COUNT_PER_BALLERINA_OBJECT = 100;
/*
MAX_STRINGS_PER_METHOD is calculated as below.
No of instructions required for create ballerina string constant object = 12
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,7 @@
import static org.wso2.ballerinalang.compiler.bir.codegen.JvmConstants.MAP_VALUE;
import static org.wso2.ballerinalang.compiler.bir.codegen.JvmConstants.MATH_UTILS;
import static org.wso2.ballerinalang.compiler.bir.codegen.JvmConstants.MODULE_INIT_CLASS_NAME;
import static org.wso2.ballerinalang.compiler.bir.codegen.JvmConstants.OBJECT_SELF_INSTANCE;
import static org.wso2.ballerinalang.compiler.bir.codegen.JvmConstants.OBJECT_TYPE_IMPL;
import static org.wso2.ballerinalang.compiler.bir.codegen.JvmConstants.REG_EXP_FACTORY;
import static org.wso2.ballerinalang.compiler.bir.codegen.JvmConstants.SHORT_VALUE;
Expand Down Expand Up @@ -432,7 +433,7 @@ public void generateVarLoad(MethodVisitor mv, BIRNode.BIRVariableDcl varDcl, int

switch (varDcl.kind) {
case SELF:
mv.visitVarInsn(ALOAD, 0);
mv.visitVarInsn(ALOAD, this.indexMap.get(OBJECT_SELF_INSTANCE));
return;
case CONSTANT:
case GLOBAL:
Expand Down Expand Up @@ -1517,7 +1518,7 @@ void generateObjectStoreIns(BIRNonTerminator.FieldAccess objectStoreIns) {
this.mv.visitTypeInsn(CHECKCAST, className);
visitKeyValueExpressions(objectStoreIns);
// invoke setOnInitialization() method
this.mv.visitMethodInsn(INVOKESPECIAL, className, "setOnInitialization",
this.mv.visitMethodInsn(INVOKEVIRTUAL, className, "setOnInitialization",
SET_ON_INIT, false);
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@
import static org.wso2.ballerinalang.compiler.bir.codegen.JvmCodeGenUtil.isExternFunc;
import static org.wso2.ballerinalang.compiler.bir.codegen.JvmCodeGenUtil.toNameString;
import static org.wso2.ballerinalang.compiler.bir.codegen.JvmConstants.BALLERINA;
import static org.wso2.ballerinalang.compiler.bir.codegen.JvmConstants.CLASS_FILE_SUFFIX;
import static org.wso2.ballerinalang.compiler.bir.codegen.JvmConstants.CONSTANT_INIT_METHOD_PREFIX;
import static org.wso2.ballerinalang.compiler.bir.codegen.JvmConstants.CURRENT_MODULE_VAR_NAME;
import static org.wso2.ballerinalang.compiler.bir.codegen.JvmConstants.ENCODED_DOT_CHARACTER;
Expand Down Expand Up @@ -425,7 +426,7 @@ private void generateModuleClasses(BIRPackage module, Map<String, byte[]> jarEnt
cw.visitEnd();

byte[] bytes = getBytes(cw, module);
jarEntries.put(moduleClass + ".class", bytes);
jarEntries.put(moduleClass + CLASS_FILE_SUFFIX, bytes);
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1274,7 +1274,6 @@ static String getStrandMetadataVarName(String typeName, String parentFunction) {
}

private void loadFpReturnType(BIROperand lhsOp) {

BType futureType = JvmCodeGenUtil.getImpliedType(lhsOp.variableDcl.type);
BType returnType = symbolTable.anyType;
if (futureType.tag == TypeTags.FUTURE) {
Expand All @@ -1289,12 +1288,10 @@ private int getJVMIndexOfVarRef(BIRNode.BIRVariableDcl varDcl) {
}

private void loadVar(BIRNode.BIRVariableDcl varDcl) {

jvmInstructionGen.generateVarLoad(this.mv, varDcl, this.getJVMIndexOfVarRef(varDcl));
}

private void storeToVar(BIRNode.BIRVariableDcl varDcl) {

jvmInstructionGen.generateVarStore(this.mv, varDcl, this.getJVMIndexOfVarRef(varDcl));
}

Expand Down
Loading

0 comments on commit 88bb122

Please sign in to comment.