Search in sources :

Example 1 with VarTypeCountAttributeInfo

use of org.ballerinalang.util.codegen.attributes.VarTypeCountAttributeInfo in project ballerina by ballerina-lang.

the class ProgramFile method addAttributeInfo.

@Override
public void addAttributeInfo(AttributeInfo.Kind attributeKind, AttributeInfo attributeInfo) {
    attributeInfoMap.put(attributeKind, attributeInfo);
    if (attributeKind == AttributeInfo.Kind.VARIABLE_TYPE_COUNT_ATTRIBUTE) {
        // TODO Move this out of the program file to a program context.. Runtime representation of a program.
        // TODO ProgramFile is the static program data.
        VarTypeCountAttributeInfo varTypeCountAttribInfo = (VarTypeCountAttributeInfo) attributeInfo;
        int[] globalVarCount = varTypeCountAttribInfo.getVarTypeCount();
        // TODO Introduce an abstraction for memory blocks
        // Initialize global memory block
        BStructType dummyType = new BStructType(null, "", "", 0);
        dummyType.setFieldTypeCount(globalVarCount);
        this.globalMemoryBlock = new BStruct(dummyType);
    }
}
Also used : VarTypeCountAttributeInfo(org.ballerinalang.util.codegen.attributes.VarTypeCountAttributeInfo) BStructType(org.ballerinalang.model.types.BStructType) BStruct(org.ballerinalang.model.values.BStruct)

Example 2 with VarTypeCountAttributeInfo

use of org.ballerinalang.util.codegen.attributes.VarTypeCountAttributeInfo in project ballerina by ballerina-lang.

the class ProgramFileReader method resolveUserDefinedTypes.

private void resolveUserDefinedTypes(PackageInfo packageInfo) {
    // TODO Improve this. We should be able to this in a single pass.
    StructInfo[] structInfoEntries = packageInfo.getStructInfoEntries();
    for (StructInfo structInfo : structInfoEntries) {
        StructFieldInfo[] fieldInfoEntries = structInfo.getFieldInfoEntries();
        BStructType structType = structInfo.getType();
        BStructType.StructField[] structFields = new BStructType.StructField[fieldInfoEntries.length];
        for (int i = 0; i < fieldInfoEntries.length; i++) {
            // Get the BType from the type descriptor
            StructFieldInfo fieldInfo = fieldInfoEntries[i];
            String typeDesc = fieldInfo.getTypeDescriptor();
            BType fieldType = getBTypeFromDescriptor(typeDesc);
            fieldInfo.setFieldType(fieldType);
            // Create the StructField in the BStructType. This is required for the type equivalence algorithm
            BStructType.StructField structField = new BStructType.StructField(fieldType, fieldInfo.getName(), fieldInfo.flags);
            structFields[i] = structField;
        }
        VarTypeCountAttributeInfo attributeInfo = (VarTypeCountAttributeInfo) structInfo.getAttributeInfo(AttributeInfo.Kind.VARIABLE_TYPE_COUNT_ATTRIBUTE);
        structType.setFieldTypeCount(attributeInfo.getVarTypeCount());
        structType.setStructFields(structFields);
        // Resolve attached function signature
        int attachedFuncCount = structInfo.funcInfoEntries.size();
        BStructType.AttachedFunction[] attachedFunctions = new BStructType.AttachedFunction[attachedFuncCount];
        int count = 0;
        for (AttachedFunctionInfo attachedFuncInfo : structInfo.funcInfoEntries.values()) {
            BFunctionType funcType = getFunctionType(attachedFuncInfo.typeSignature, packageInfo);
            BStructType.AttachedFunction attachedFunction = new BStructType.AttachedFunction(attachedFuncInfo.name, funcType, attachedFuncInfo.flags);
            attachedFunctions[count++] = attachedFunction;
            if (structInfo.initializer == attachedFuncInfo) {
                structType.initializer = attachedFunction;
            } else if (structInfo.defaultsValuesInitFunc == attachedFuncInfo) {
                structType.defaultsValuesInitFunc = attachedFunction;
            }
        }
        structType.setAttachedFunctions(attachedFunctions);
    }
    for (ConstantPoolEntry cpEntry : unresolvedCPEntries) {
        switch(cpEntry.getEntryType()) {
            case CP_ENTRY_TYPE_REF:
                TypeRefCPEntry typeRefCPEntry = (TypeRefCPEntry) cpEntry;
                String typeSig = typeRefCPEntry.getTypeSig();
                BType bType = getBTypeFromDescriptor(typeSig);
                typeRefCPEntry.setType(bType);
                break;
            default:
                break;
        }
    }
}
Also used : VarTypeCountAttributeInfo(org.ballerinalang.util.codegen.attributes.VarTypeCountAttributeInfo) TypeRefCPEntry(org.ballerinalang.util.codegen.cpentries.TypeRefCPEntry) BFunctionType(org.ballerinalang.model.types.BFunctionType) ConstantPoolEntry(org.ballerinalang.util.codegen.cpentries.ConstantPoolEntry) BStructType(org.ballerinalang.model.types.BStructType) BType(org.ballerinalang.model.types.BType)

Example 3 with VarTypeCountAttributeInfo

use of org.ballerinalang.util.codegen.attributes.VarTypeCountAttributeInfo 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());
    }
}
Also used : VarTypeCountAttributeInfo(org.ballerinalang.util.codegen.attributes.VarTypeCountAttributeInfo) LineNumberTableAttributeInfo(org.ballerinalang.util.codegen.attributes.LineNumberTableAttributeInfo) ProgramFileFormatException(org.ballerinalang.util.exceptions.ProgramFileFormatException) UTF8CPEntry(org.ballerinalang.util.codegen.cpentries.UTF8CPEntry) ParamDefaultValueAttributeInfo(org.ballerinalang.util.codegen.attributes.ParamDefaultValueAttributeInfo) LocalVariableAttributeInfo(org.ballerinalang.util.codegen.attributes.LocalVariableAttributeInfo) ErrorTableAttributeInfo(org.ballerinalang.util.codegen.attributes.ErrorTableAttributeInfo) LineNumberTableAttributeInfo(org.ballerinalang.util.codegen.attributes.LineNumberTableAttributeInfo) CodeAttributeInfo(org.ballerinalang.util.codegen.attributes.CodeAttributeInfo) DefaultValueAttributeInfo(org.ballerinalang.util.codegen.attributes.DefaultValueAttributeInfo) VarTypeCountAttributeInfo(org.ballerinalang.util.codegen.attributes.VarTypeCountAttributeInfo) AttributeInfo(org.ballerinalang.util.codegen.attributes.AttributeInfo) ParamDefaultValueAttributeInfo(org.ballerinalang.util.codegen.attributes.ParamDefaultValueAttributeInfo) LocalVariableAttributeInfo(org.ballerinalang.util.codegen.attributes.LocalVariableAttributeInfo) ParamDefaultValueAttributeInfo(org.ballerinalang.util.codegen.attributes.ParamDefaultValueAttributeInfo) DefaultValueAttributeInfo(org.ballerinalang.util.codegen.attributes.DefaultValueAttributeInfo) ErrorTableAttributeInfo(org.ballerinalang.util.codegen.attributes.ErrorTableAttributeInfo) CodeAttributeInfo(org.ballerinalang.util.codegen.attributes.CodeAttributeInfo) StructureRefCPEntry(org.ballerinalang.util.codegen.cpentries.StructureRefCPEntry)

Example 4 with VarTypeCountAttributeInfo

use of org.ballerinalang.util.codegen.attributes.VarTypeCountAttributeInfo in project ballerina by ballerina-lang.

the class ProgramFileReader method resolveConnectorMethodTables.

private void resolveConnectorMethodTables(PackageInfo packageInfo) {
    ConnectorInfo[] connectorInfoEntries = packageInfo.getConnectorInfoEntries();
    for (ConnectorInfo connectorInfo : connectorInfoEntries) {
        BConnectorType connectorType = connectorInfo.getType();
        VarTypeCountAttributeInfo attributeInfo = (VarTypeCountAttributeInfo) connectorInfo.getAttributeInfo(AttributeInfo.Kind.VARIABLE_TYPE_COUNT_ATTRIBUTE);
        connectorType.setFieldTypeCount(attributeInfo.getVarTypeCount());
        Map<Integer, Integer> methodTableInteger = connectorInfo.getMethodTableIndex();
        Map<BConnectorType, ConnectorInfo> methodTableType = new HashMap<>();
        for (Integer key : methodTableInteger.keySet()) {
            int keyType = methodTableInteger.get(key);
            TypeRefCPEntry typeRefCPEntry = (TypeRefCPEntry) packageInfo.getCPEntry(key);
            StructureRefCPEntry structureRefCPEntry = (StructureRefCPEntry) packageInfo.getCPEntry(keyType);
            ConnectorInfo connectorInfoType = (ConnectorInfo) structureRefCPEntry.getStructureTypeInfo();
            methodTableType.put((BConnectorType) typeRefCPEntry.getType(), connectorInfoType);
        }
        connectorInfo.setMethodTableType(methodTableType);
        for (ActionInfo actionInfo : connectorInfo.getActionInfoEntries()) {
            setCallableUnitSignature(actionInfo, actionInfo.getSignature(), packageInfo);
        }
    }
}
Also used : VarTypeCountAttributeInfo(org.ballerinalang.util.codegen.attributes.VarTypeCountAttributeInfo) HashMap(java.util.HashMap) TypeRefCPEntry(org.ballerinalang.util.codegen.cpentries.TypeRefCPEntry) BConnectorType(org.ballerinalang.model.types.BConnectorType) StructureRefCPEntry(org.ballerinalang.util.codegen.cpentries.StructureRefCPEntry)

Aggregations

VarTypeCountAttributeInfo (org.ballerinalang.util.codegen.attributes.VarTypeCountAttributeInfo)4 BStructType (org.ballerinalang.model.types.BStructType)2 StructureRefCPEntry (org.ballerinalang.util.codegen.cpentries.StructureRefCPEntry)2 TypeRefCPEntry (org.ballerinalang.util.codegen.cpentries.TypeRefCPEntry)2 HashMap (java.util.HashMap)1 BConnectorType (org.ballerinalang.model.types.BConnectorType)1 BFunctionType (org.ballerinalang.model.types.BFunctionType)1 BType (org.ballerinalang.model.types.BType)1 BStruct (org.ballerinalang.model.values.BStruct)1 AttributeInfo (org.ballerinalang.util.codegen.attributes.AttributeInfo)1 CodeAttributeInfo (org.ballerinalang.util.codegen.attributes.CodeAttributeInfo)1 DefaultValueAttributeInfo (org.ballerinalang.util.codegen.attributes.DefaultValueAttributeInfo)1 ErrorTableAttributeInfo (org.ballerinalang.util.codegen.attributes.ErrorTableAttributeInfo)1 LineNumberTableAttributeInfo (org.ballerinalang.util.codegen.attributes.LineNumberTableAttributeInfo)1 LocalVariableAttributeInfo (org.ballerinalang.util.codegen.attributes.LocalVariableAttributeInfo)1 ParamDefaultValueAttributeInfo (org.ballerinalang.util.codegen.attributes.ParamDefaultValueAttributeInfo)1 ConstantPoolEntry (org.ballerinalang.util.codegen.cpentries.ConstantPoolEntry)1 UTF8CPEntry (org.ballerinalang.util.codegen.cpentries.UTF8CPEntry)1 ProgramFileFormatException (org.ballerinalang.util.exceptions.ProgramFileFormatException)1