Skip to content

Commit

Permalink
Merge branch 'master' of github.com:ballerina-platform/ballerina-lang…
Browse files Browse the repository at this point in the history
… into worker_change
  • Loading branch information
HindujaB committed Nov 21, 2023
2 parents 7fd8965 + 88bb122 commit a0469f6
Show file tree
Hide file tree
Showing 60 changed files with 17,098 additions and 421 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 @@ -28,10 +28,6 @@
import org.wso2.ballerinalang.compiler.tree.BLangPackage;
import org.wso2.ballerinalang.compiler.util.CompilerContext;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.HashMap;

/**
Expand Down Expand Up @@ -92,8 +88,6 @@ private CompiledJarFile generate(BPackageSymbol packageSymbol) {
dlog.setCurrentPackageId(packageSymbol.pkgID);
final JvmPackageGen jvmPackageGen = new JvmPackageGen(symbolTable, packageCache, dlog, types);

populateExternalMap(jvmPackageGen);

//Rewrite identifier names with encoding special characters
HashMap<String, String> originalIdentifierMap = JvmDesugarPhase.encodeModuleIdentifiers(packageSymbol.bir);

Expand All @@ -104,30 +98,4 @@ private CompiledJarFile generate(BPackageSymbol packageSymbol) {
JvmDesugarPhase.replaceEncodedModuleIdentifiers(packageSymbol.bir, originalIdentifierMap);
return compiledJarFile;
}

private void populateExternalMap(JvmPackageGen jvmPackageGen) {

String nativeMap = System.getenv("BALLERINA_NATIVE_MAP");
if (nativeMap == null) {
return;
}
File mapFile = new File(nativeMap);
if (!mapFile.exists()) {
return;
}

try (BufferedReader br = new BufferedReader(new FileReader(mapFile))) {
String line;
while ((line = br.readLine()) != null) {
if (line.startsWith("\"")) {
int firstQuote = line.indexOf('"', 1);
String key = line.substring(1, firstQuote);
String value = line.substring(line.indexOf('"', firstQuote + 1) + 1, line.lastIndexOf('"'));
jvmPackageGen.addExternClassMapping(key, value);
}
}
} catch (IOException e) {
//ignore because this is only important in langlibs users shouldn't see this error
}
}
}
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 @@ -143,7 +144,6 @@ public class JvmPackageGen {
private final InitMethodGen initMethodGen;
private final ConfigMethodGen configMethodGen;
private final Map<String, BIRFunctionWrapper> birFunctionMap;
private final Map<String, String> externClassMap;
private final Map<String, String> globalVarClassMap;
private final Set<PackageID> dependentModules;
private final BLangDiagnosticLog dlog;
Expand All @@ -152,7 +152,6 @@ public class JvmPackageGen {
JvmPackageGen(SymbolTable symbolTable, PackageCache packageCache, BLangDiagnosticLog dlog, Types types) {
birFunctionMap = new HashMap<>();
globalVarClassMap = new HashMap<>();
externClassMap = new HashMap<>();
dependentModules = new LinkedHashSet<>();
this.symbolTable = symbolTable;
this.packageCache = packageCache;
Expand Down Expand Up @@ -243,7 +242,7 @@ private static void generateLockForVariable(ClassWriter cw) {
private static void generateStaticInitializer(ClassWriter cw, String className, BIRPackage birPackage,
boolean isInitClass, boolean serviceEPAvailable,
AsyncDataCollector asyncDataCollector,
JvmConstantsGen jvmConstantsGen, boolean isTestablePackage) {
JvmConstantsGen jvmConstantsGen) {
if (!isInitClass && asyncDataCollector.getStrandMetadata().isEmpty()) {
return;
}
Expand Down Expand Up @@ -320,15 +319,6 @@ static String computeLockNameFromString(String varName) {
return "$lock" + varName;
}

public static String cleanupPackageName(String pkgName) {
int index = pkgName.lastIndexOf("/");
if (index > 0) {
return pkgName.substring(0, index);
} else {
return pkgName;
}
}

public static BIRFunctionWrapper getFunctionWrapper(BIRFunction currentFunc, PackageID packageID,
String moduleClass) {
BInvokableType functionTypeDesc = currentFunc.type;
Expand Down Expand Up @@ -432,11 +422,11 @@ private void generateModuleClasses(BIRPackage module, Map<String, byte[]> jarEnt
}
JvmCodeGenUtil.visitStrandMetadataFields(cw, asyncDataCollector.getStrandMetadata());
generateStaticInitializer(cw, moduleClass, module, isInitClass, serviceEPAvailable,
asyncDataCollector, jvmConstantsGen, isTestable);
asyncDataCollector, jvmConstantsGen);
cw.visitEnd();

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

Expand Down Expand Up @@ -518,7 +508,7 @@ private void linkTypeDefinitions(BIRPackage module, boolean isEntry) {
String className = JvmValueGen.getTypeValueClassName(pkgName, typeName);
try {
BIRFunctionWrapper birFuncWrapperOrError =
getBirFunctionWrapper(isEntry, module.packageID, func, className, lookupKey);
getBirFunctionWrapper(isEntry, module.packageID, func, className);
birFunctionMap.put(pkgName + lookupKey, birFuncWrapperOrError);
} catch (JInteropException e) {
dlog.error(func.pos, e.getCode(), e.getMessage());
Expand Down Expand Up @@ -577,9 +567,7 @@ private void linkModuleFunctions(BIRPackage birPackage, String initClass, boolea
count = count + 1;
// link the bir function for lookup
String birFuncName = birFunc.name.value;

String balFileName;

if (birFunc.pos == null) {
balFileName = MODULE_INIT_CLASS_NAME;
} else {
Expand All @@ -606,8 +594,7 @@ private void linkModuleFunctions(BIRPackage birPackage, String initClass, boolea
}
try {
BIRFunctionWrapper birFuncWrapperOrError = getBirFunctionWrapper(isEntry, packageID, birFunc,
birModuleClassName,
birFuncName);
birModuleClassName);
birFunctionMap.put(pkgName + birFuncName, birFuncWrapperOrError);
} catch (JInteropException e) {
dlog.error(birFunc.pos, e.getCode(), e.getMessage());
Expand All @@ -616,11 +603,11 @@ private void linkModuleFunctions(BIRPackage birPackage, String initClass, boolea
}

private BIRFunctionWrapper getBirFunctionWrapper(boolean isEntry, PackageID packageID,
BIRFunction birFunc, String birModuleClassName, String lookupKey) {
BIRFunction birFunc, String birModuleClassName) {
BIRFunctionWrapper birFuncWrapperOrError;
if (isExternFunc(birFunc) && isEntry) {
birFuncWrapperOrError = createExternalFunctionWrapper(isEntry, birFunc, packageID,
birModuleClassName, lookupKey, this);
birModuleClassName, this);
} else {
if (isEntry && birFunc.receiver == null) {
addDefaultableBooleanVarsToSignature(birFunc, symbolTable.booleanType);
Expand All @@ -630,10 +617,6 @@ private BIRFunctionWrapper getBirFunctionWrapper(boolean isEntry, PackageID pack
return birFuncWrapperOrError;
}

public String lookupExternClassName(String pkgName, String functionName) {
return externClassMap.get(pkgName + "/" + functionName);
}

public byte[] getBytes(ClassWriter cw, BIRNode node) {

byte[] result;
Expand Down Expand Up @@ -664,18 +647,13 @@ public byte[] getBytes(ClassWriter cw, BIRNode node) {
private void clearPackageGenInfo() {
birFunctionMap.clear();
globalVarClassMap.clear();
externClassMap.clear();
dependentModules.clear();
}

public BIRFunctionWrapper lookupBIRFunctionWrapper(String lookupKey) {
return this.birFunctionMap.get(lookupKey);
}

void addExternClassMapping(String key, String value) {
this.externClassMap.put(key, value);
}

BType lookupTypeDef(NewInstance objectNewIns) {

if (!objectNewIns.isExternalDef) {
Expand Down Expand Up @@ -753,7 +731,7 @@ CompiledJarFile generate(BIRPackage module, boolean isEntry) {
final Map<String, byte[]> jarEntries = new HashMap<>();

// desugar parameter initialization
injectDefaultParamInits(module, initMethodGen, this);
injectDefaultParamInits(module, initMethodGen);
injectDefaultParamInitsToAttachedFuncs(module, initMethodGen, this);

// create imported modules flat list
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 a0469f6

Please sign in to comment.