use of org.ballerinalang.util.codegen.attributes.ErrorTableAttributeInfo in project ballerina by ballerina-lang.
the class ProgramFileReader method getAttributeInfo.
private AttributeInfo getAttributeInfo(DataInputStream dataInStream, ConstantPool constantPool) throws IOException {
int attribNameCPIndex = dataInStream.readInt();
UTF8CPEntry attribNameCPEntry = (UTF8CPEntry) constantPool.getCPEntry(attribNameCPIndex);
AttributeInfo.Kind attribKind = AttributeInfo.Kind.fromString(attribNameCPEntry.getValue());
if (attribKind == null) {
throw new ProgramFileFormatException("unknown attribute kind " + attribNameCPEntry.getValue());
}
switch(attribKind) {
case CODE_ATTRIBUTE:
CodeAttributeInfo codeAttributeInfo = new CodeAttributeInfo();
codeAttributeInfo.setAttributeNameIndex(attribNameCPIndex);
codeAttributeInfo.setCodeAddrs(dataInStream.readInt());
codeAttributeInfo.setMaxLongLocalVars(dataInStream.readUnsignedShort());
codeAttributeInfo.setMaxDoubleLocalVars(dataInStream.readShort());
codeAttributeInfo.setMaxStringLocalVars(dataInStream.readShort());
codeAttributeInfo.setMaxIntLocalVars(dataInStream.readShort());
codeAttributeInfo.setMaxByteLocalVars(dataInStream.readShort());
codeAttributeInfo.setMaxRefLocalVars(dataInStream.readShort());
codeAttributeInfo.setMaxLongRegs(dataInStream.readShort());
codeAttributeInfo.setMaxDoubleRegs(dataInStream.readShort());
codeAttributeInfo.setMaxStringRegs(dataInStream.readShort());
codeAttributeInfo.setMaxIntRegs(dataInStream.readShort());
codeAttributeInfo.setMaxByteRegs(dataInStream.readShort());
codeAttributeInfo.setMaxRefRegs(dataInStream.readShort());
return codeAttributeInfo;
case VARIABLE_TYPE_COUNT_ATTRIBUTE:
VarTypeCountAttributeInfo varCountAttributeInfo = new VarTypeCountAttributeInfo(attribNameCPIndex);
varCountAttributeInfo.setMaxLongVars(dataInStream.readShort());
varCountAttributeInfo.setMaxDoubleVars(dataInStream.readShort());
varCountAttributeInfo.setMaxStringVars(dataInStream.readShort());
varCountAttributeInfo.setMaxIntVars(dataInStream.readShort());
varCountAttributeInfo.setMaxByteVars(dataInStream.readShort());
varCountAttributeInfo.setMaxRefVars(dataInStream.readShort());
return varCountAttributeInfo;
case ERROR_TABLE:
ErrorTableAttributeInfo tableAttributeInfo = new ErrorTableAttributeInfo(attribNameCPIndex);
int tableEntryCount = dataInStream.readShort();
for (int i = 0; i < tableEntryCount; i++) {
int ipFrom = dataInStream.readInt();
int ipTo = dataInStream.readInt();
int ipTarget = dataInStream.readInt();
int priority = dataInStream.readInt();
int errorStructCPIndex = dataInStream.readInt();
ErrorTableEntry tableEntry = new ErrorTableEntry(ipFrom, ipTo, ipTarget, priority, errorStructCPIndex);
if (errorStructCPIndex != -1) {
StructureRefCPEntry structureRefCPEntry = (StructureRefCPEntry) constantPool.getCPEntry(errorStructCPIndex);
tableEntry.setError((StructInfo) structureRefCPEntry.getStructureTypeInfo());
}
tableAttributeInfo.addErrorTableEntry(tableEntry);
}
return tableAttributeInfo;
case LOCAL_VARIABLES_ATTRIBUTE:
LocalVariableAttributeInfo localVarAttrInfo = new LocalVariableAttributeInfo(attribNameCPIndex);
int localVarInfoCount = dataInStream.readShort();
for (int i = 0; i < localVarInfoCount; i++) {
LocalVariableInfo localVariableInfo = getLocalVariableInfo(dataInStream, constantPool);
localVarAttrInfo.addLocalVarInfo(localVariableInfo);
}
return localVarAttrInfo;
case LINE_NUMBER_TABLE_ATTRIBUTE:
LineNumberTableAttributeInfo lnNoTblAttrInfo = new LineNumberTableAttributeInfo(attribNameCPIndex);
int lineNoInfoCount = dataInStream.readShort();
for (int i = 0; i < lineNoInfoCount; i++) {
LineNumberInfo lineNumberInfo = getLineNumberInfo(dataInStream, constantPool);
lnNoTblAttrInfo.addLineNumberInfo(lineNumberInfo);
}
return lnNoTblAttrInfo;
case DEFAULT_VALUE_ATTRIBUTE:
DefaultValue defaultValue = getDefaultValue(dataInStream, constantPool);
DefaultValueAttributeInfo defaultValAttrInfo = new DefaultValueAttributeInfo(attribNameCPIndex, defaultValue);
return defaultValAttrInfo;
case PARAMETER_DEFAULTS_ATTRIBUTE:
ParamDefaultValueAttributeInfo paramDefaultValAttrInfo = new ParamDefaultValueAttributeInfo(attribNameCPIndex);
int paramDefaultsInfoCount = dataInStream.readShort();
for (int i = 0; i < paramDefaultsInfoCount; i++) {
DefaultValue paramDefaultValue = getDefaultValue(dataInStream, constantPool);
paramDefaultValAttrInfo.addParamDefaultValueInfo(paramDefaultValue);
}
return paramDefaultValAttrInfo;
default:
throw new ProgramFileFormatException("unsupported attribute kind " + attribNameCPEntry.getValue());
}
}
use of org.ballerinalang.util.codegen.attributes.ErrorTableAttributeInfo in project ballerina by ballerina-lang.
the class ErrorTableEntry method getMatch.
public static ErrorTableEntry getMatch(PackageInfo packageInfo, int currentIP, final BStruct error) {
ErrorTableAttributeInfo errorTable = (ErrorTableAttributeInfo) packageInfo.getAttributeInfo(AttributeInfo.Kind.ERROR_TABLE);
List<ErrorTableEntry> errorTableEntries = errorTable != null ? errorTable.getErrorTableEntriesList() : new ArrayList<>();
List<MatchedEntry> rangeMatched = new ArrayList<>();
errorTableEntries.stream().filter(errorTableEntry -> errorTableEntry.matchRange(currentIP)).forEach(errorTableEntry -> {
MatchedEntry entry = new MatchedEntry();
entry.errorTableEntry = errorTableEntry;
entry.ipSize = errorTableEntry.ipTo - errorTableEntry.ipFrom;
if (errorTableEntry.getErrorStructCPIndex() == -1) {
// match any.
entry.status = 2;
rangeMatched.add(entry);
} else if (errorTableEntry.getError().getType().equals(error.getType())) {
// exact match.
entry.status = 0;
rangeMatched.add(entry);
} else if (CPU.checkStructEquivalency((BStructType) error.getType(), (BStructType) errorTableEntry.getError().getType())) {
entry.status = 1;
rangeMatched.add(entry);
}
});
if (rangeMatched.size() == 0) {
return null;
}
if (rangeMatched.size() == 1) {
return rangeMatched.get(0).errorTableEntry;
}
MatchedEntry[] matchedEntries = rangeMatched.stream().sorted(Comparator.comparingInt(o -> o.ipSize)).toArray(MatchedEntry[]::new);
int currentSize = 0;
ErrorTableEntry errorTableEntry = null;
for (int i = 0; i < matchedEntries.length; i++) {
MatchedEntry entry = matchedEntries[i];
if (currentSize < entry.ipSize) {
if (errorTableEntry == null) {
// Expand scope.
currentSize = entry.ipSize;
} else {
// Return best match.
return errorTableEntry;
}
}
if (entry.status == 0) {
// Best case.
return entry.errorTableEntry;
} else {
if (errorTableEntry == null) {
errorTableEntry = entry.errorTableEntry;
} else {
if (errorTableEntry.priority > entry.errorTableEntry.priority) {
// found a high order entry
errorTableEntry = entry.errorTableEntry;
}
}
}
}
return errorTableEntry;
}
Aggregations