Search in sources :

Example 1 with TypeRefCPEntry

use of org.ballerinalang.util.codegen.cpentries.TypeRefCPEntry in project ballerina by ballerina-lang.

the class CPU method calculateLength.

@SuppressWarnings("rawtypes")
private static void calculateLength(WorkerExecutionContext ctx, int[] operands, WorkerData sf) {
    int i = operands[0];
    int cpIndex = operands[1];
    int j = operands[2];
    TypeRefCPEntry typeRefCPEntry = (TypeRefCPEntry) ctx.constPool[cpIndex];
    int typeTag = typeRefCPEntry.getType().getTag();
    if (typeTag == TypeTags.STRING_TAG) {
        String value = sf.stringRegs[i];
        if (value == null) {
            handleNullRefError(ctx);
        } else {
            sf.longRegs[j] = value.length();
        }
        return;
    } else if (typeTag == TypeTags.BLOB_TAG) {
        // Here it is assumed null is not supported for blob type
        sf.longRegs[j] = sf.byteRegs[i].length;
        return;
    }
    BValue entity = sf.refRegs[i];
    if (entity == null) {
        handleNullRefError(ctx);
        return;
    }
    if (typeTag == TypeTags.XML_TAG) {
        sf.longRegs[j] = ((BXML) entity).length();
        return;
    } else if (entity instanceof BJSON) {
        if (JSONUtils.isJSONArray((BJSON) entity)) {
            sf.longRegs[j] = JSONUtils.getJSONArrayLength((BJSON) sf.refRegs[i]);
        } else {
            sf.longRegs[j] = -1;
        }
        return;
    } else if (typeTag == TypeTags.MAP_TAG) {
        sf.longRegs[j] = ((BMap) entity).size();
        return;
    }
    BNewArray newArray = (BNewArray) entity;
    sf.longRegs[j] = newArray.size();
    return;
}
Also used : BNewArray(org.ballerinalang.model.values.BNewArray) TypeRefCPEntry(org.ballerinalang.util.codegen.cpentries.TypeRefCPEntry) BValue(org.ballerinalang.model.values.BValue) BString(org.ballerinalang.model.values.BString) BJSON(org.ballerinalang.model.values.BJSON)

Example 2 with TypeRefCPEntry

use of org.ballerinalang.util.codegen.cpentries.TypeRefCPEntry in project ballerina by ballerina-lang.

the class CPU method convertJSONToStruct.

private static void convertJSONToStruct(WorkerExecutionContext ctx, int[] operands, WorkerData sf) {
    int i = operands[0];
    int cpIndex = operands[1];
    int j = operands[2];
    TypeRefCPEntry typeRefCPEntry = (TypeRefCPEntry) ctx.constPool[cpIndex];
    BJSON bjson = (BJSON) sf.refRegs[i];
    if (bjson == null) {
        handleNullRefError(ctx);
        return;
    }
    try {
        sf.refRegs[j] = JSONUtils.convertJSONToStruct(bjson, (BStructType) typeRefCPEntry.getType());
    } catch (Exception e) {
        String errorMsg = "cannot convert '" + TypeConstants.JSON_TNAME + "' to type '" + typeRefCPEntry.getType() + "': " + e.getMessage();
        handleTypeConversionError(ctx, sf, j, errorMsg);
    }
}
Also used : BStructType(org.ballerinalang.model.types.BStructType) TypeRefCPEntry(org.ballerinalang.util.codegen.cpentries.TypeRefCPEntry) BString(org.ballerinalang.model.values.BString) BJSON(org.ballerinalang.model.values.BJSON) BallerinaException(org.ballerinalang.util.exceptions.BallerinaException)

Example 3 with TypeRefCPEntry

use of org.ballerinalang.util.codegen.cpentries.TypeRefCPEntry in project ballerina by ballerina-lang.

the class CPU method execLoadOpcodes.

@SuppressWarnings({ "rawtypes", "unchecked" })
private static void execLoadOpcodes(WorkerExecutionContext ctx, WorkerData sf, int opcode, int[] operands) {
    int i;
    int j;
    int k;
    // Index of the local variable
    int lvIndex;
    int fieldIndex;
    BIntArray bIntArray;
    BFloatArray bFloatArray;
    BStringArray bStringArray;
    BBooleanArray bBooleanArray;
    BBlobArray bBlobArray;
    BRefValueArray bArray;
    StructureType structureType;
    BMap<String, BRefType> bMap;
    BJSON jsonVal;
    switch(opcode) {
        case InstructionCodes.IMOVE:
            lvIndex = operands[0];
            i = operands[1];
            sf.longRegs[i] = sf.longRegs[lvIndex];
            break;
        case InstructionCodes.FMOVE:
            lvIndex = operands[0];
            i = operands[1];
            sf.doubleRegs[i] = sf.doubleRegs[lvIndex];
            break;
        case InstructionCodes.SMOVE:
            lvIndex = operands[0];
            i = operands[1];
            sf.stringRegs[i] = sf.stringRegs[lvIndex];
            break;
        case InstructionCodes.BMOVE:
            lvIndex = operands[0];
            i = operands[1];
            sf.intRegs[i] = sf.intRegs[lvIndex];
            break;
        case InstructionCodes.LMOVE:
            lvIndex = operands[0];
            i = operands[1];
            sf.byteRegs[i] = sf.byteRegs[lvIndex];
            break;
        case InstructionCodes.RMOVE:
            lvIndex = operands[0];
            i = operands[1];
            sf.refRegs[i] = sf.refRegs[lvIndex];
            break;
        case InstructionCodes.IALOAD:
            i = operands[0];
            j = operands[1];
            k = operands[2];
            bIntArray = (BIntArray) sf.refRegs[i];
            if (bIntArray == null) {
                handleNullRefError(ctx);
                break;
            }
            try {
                sf.longRegs[k] = bIntArray.get(sf.longRegs[j]);
            } catch (Exception e) {
                ctx.setError(BLangVMErrors.createError(ctx, e.getMessage()));
                handleError(ctx);
            }
            break;
        case InstructionCodes.FALOAD:
            i = operands[0];
            j = operands[1];
            k = operands[2];
            bFloatArray = (BFloatArray) sf.refRegs[i];
            if (bFloatArray == null) {
                handleNullRefError(ctx);
                break;
            }
            try {
                sf.doubleRegs[k] = bFloatArray.get(sf.longRegs[j]);
            } catch (Exception e) {
                ctx.setError(BLangVMErrors.createError(ctx, e.getMessage()));
                handleError(ctx);
            }
            break;
        case InstructionCodes.SALOAD:
            i = operands[0];
            j = operands[1];
            k = operands[2];
            bStringArray = (BStringArray) sf.refRegs[i];
            if (bStringArray == null) {
                handleNullRefError(ctx);
                break;
            }
            try {
                sf.stringRegs[k] = bStringArray.get(sf.longRegs[j]);
            } catch (Exception e) {
                ctx.setError(BLangVMErrors.createError(ctx, e.getMessage()));
                handleError(ctx);
            }
            break;
        case InstructionCodes.BALOAD:
            i = operands[0];
            j = operands[1];
            k = operands[2];
            bBooleanArray = (BBooleanArray) sf.refRegs[i];
            if (bBooleanArray == null) {
                handleNullRefError(ctx);
                break;
            }
            try {
                sf.intRegs[k] = bBooleanArray.get(sf.longRegs[j]);
            } catch (Exception e) {
                ctx.setError(BLangVMErrors.createError(ctx, e.getMessage()));
                handleError(ctx);
            }
            break;
        case InstructionCodes.LALOAD:
            i = operands[0];
            j = operands[1];
            k = operands[2];
            bBlobArray = (BBlobArray) sf.refRegs[i];
            if (bBlobArray == null) {
                handleNullRefError(ctx);
                break;
            }
            try {
                sf.byteRegs[k] = bBlobArray.get(sf.longRegs[j]);
            } catch (Exception e) {
                ctx.setError(BLangVMErrors.createError(ctx, e.getMessage()));
                handleError(ctx);
            }
            break;
        case InstructionCodes.RALOAD:
            i = operands[0];
            j = operands[1];
            k = operands[2];
            bArray = (BRefValueArray) sf.refRegs[i];
            if (bArray == null) {
                handleNullRefError(ctx);
                break;
            }
            try {
                sf.refRegs[k] = bArray.get(sf.longRegs[j]);
            } catch (Exception e) {
                ctx.setError(BLangVMErrors.createError(ctx, e.getMessage()));
                handleError(ctx);
            }
            break;
        case InstructionCodes.JSONALOAD:
            i = operands[0];
            j = operands[1];
            k = operands[2];
            jsonVal = (BJSON) sf.refRegs[i];
            if (jsonVal == null) {
                handleNullRefError(ctx);
                break;
            }
            try {
                sf.refRegs[k] = JSONUtils.getArrayElement(jsonVal, sf.longRegs[j]);
            } catch (Exception e) {
                ctx.setError(BLangVMErrors.createError(ctx, e.getMessage()));
                handleError(ctx);
            }
            break;
        case InstructionCodes.IGLOAD:
            // Global variable index
            i = operands[0];
            // Stack registry index
            j = operands[1];
            sf.longRegs[j] = ctx.programFile.getGlobalMemoryBlock().getIntField(i);
            break;
        case InstructionCodes.FGLOAD:
            i = operands[0];
            j = operands[1];
            sf.doubleRegs[j] = ctx.programFile.getGlobalMemoryBlock().getFloatField(i);
            break;
        case InstructionCodes.SGLOAD:
            i = operands[0];
            j = operands[1];
            sf.stringRegs[j] = ctx.programFile.getGlobalMemoryBlock().getStringField(i);
            break;
        case InstructionCodes.BGLOAD:
            i = operands[0];
            j = operands[1];
            sf.intRegs[j] = ctx.programFile.getGlobalMemoryBlock().getBooleanField(i);
            break;
        case InstructionCodes.LGLOAD:
            i = operands[0];
            j = operands[1];
            sf.byteRegs[j] = ctx.programFile.getGlobalMemoryBlock().getBlobField(i);
            break;
        case InstructionCodes.RGLOAD:
            i = operands[0];
            j = operands[1];
            sf.refRegs[j] = ctx.programFile.getGlobalMemoryBlock().getRefField(i);
            break;
        case InstructionCodes.IFIELDLOAD:
            i = operands[0];
            fieldIndex = operands[1];
            j = operands[2];
            structureType = (StructureType) sf.refRegs[i];
            if (structureType == null) {
                handleNullRefError(ctx);
                break;
            }
            sf.longRegs[j] = structureType.getIntField(fieldIndex);
            break;
        case InstructionCodes.FFIELDLOAD:
            i = operands[0];
            fieldIndex = operands[1];
            j = operands[2];
            structureType = (StructureType) sf.refRegs[i];
            if (structureType == null) {
                handleNullRefError(ctx);
                break;
            }
            sf.doubleRegs[j] = structureType.getFloatField(fieldIndex);
            break;
        case InstructionCodes.SFIELDLOAD:
            i = operands[0];
            fieldIndex = operands[1];
            j = operands[2];
            structureType = (StructureType) sf.refRegs[i];
            if (structureType == null) {
                handleNullRefError(ctx);
                break;
            }
            sf.stringRegs[j] = structureType.getStringField(fieldIndex);
            break;
        case InstructionCodes.BFIELDLOAD:
            i = operands[0];
            fieldIndex = operands[1];
            j = operands[2];
            structureType = (StructureType) sf.refRegs[i];
            if (structureType == null) {
                handleNullRefError(ctx);
                break;
            }
            sf.intRegs[j] = structureType.getBooleanField(fieldIndex);
            break;
        case InstructionCodes.LFIELDLOAD:
            i = operands[0];
            fieldIndex = operands[1];
            j = operands[2];
            structureType = (StructureType) sf.refRegs[i];
            if (structureType == null) {
                handleNullRefError(ctx);
                break;
            }
            sf.byteRegs[j] = structureType.getBlobField(fieldIndex);
            break;
        case InstructionCodes.RFIELDLOAD:
            i = operands[0];
            fieldIndex = operands[1];
            j = operands[2];
            structureType = (StructureType) sf.refRegs[i];
            if (structureType == null) {
                handleNullRefError(ctx);
                break;
            }
            sf.refRegs[j] = structureType.getRefField(fieldIndex);
            break;
        case InstructionCodes.MAPLOAD:
            i = operands[0];
            j = operands[1];
            k = operands[2];
            bMap = (BMap<String, BRefType>) sf.refRegs[i];
            if (bMap == null) {
                handleNullRefError(ctx);
                break;
            }
            sf.refRegs[k] = bMap.get(sf.stringRegs[j]);
            break;
        case InstructionCodes.JSONLOAD:
            i = operands[0];
            j = operands[1];
            k = operands[2];
            jsonVal = (BJSON) sf.refRegs[i];
            if (jsonVal == null) {
                handleNullRefError(ctx);
                break;
            }
            sf.refRegs[k] = JSONUtils.getElement(jsonVal, sf.stringRegs[j]);
            break;
        case InstructionCodes.ENUMERATORLOAD:
            i = operands[0];
            j = operands[1];
            k = operands[2];
            TypeRefCPEntry typeRefCPEntry = (TypeRefCPEntry) ctx.constPool[i];
            BEnumType enumType = (BEnumType) typeRefCPEntry.getType();
            sf.refRegs[k] = enumType.getEnumerator(j);
            break;
        default:
            throw new UnsupportedOperationException();
    }
}
Also used : BRefType(org.ballerinalang.model.values.BRefType) TypeRefCPEntry(org.ballerinalang.util.codegen.cpentries.TypeRefCPEntry) BRefValueArray(org.ballerinalang.model.values.BRefValueArray) BString(org.ballerinalang.model.values.BString) BStringArray(org.ballerinalang.model.values.BStringArray) BJSON(org.ballerinalang.model.values.BJSON) BallerinaException(org.ballerinalang.util.exceptions.BallerinaException) BIntArray(org.ballerinalang.model.values.BIntArray) BEnumType(org.ballerinalang.model.types.BEnumType) BBlobArray(org.ballerinalang.model.values.BBlobArray) BBooleanArray(org.ballerinalang.model.values.BBooleanArray) StructureType(org.ballerinalang.model.values.StructureType) BFloatArray(org.ballerinalang.model.values.BFloatArray)

Example 4 with TypeRefCPEntry

use of org.ballerinalang.util.codegen.cpentries.TypeRefCPEntry in project ballerina by ballerina-lang.

the class CPU method convertMapToStruct.

@SuppressWarnings({ "unchecked", "rawtypes" })
private static void convertMapToStruct(WorkerExecutionContext ctx, int[] operands, WorkerData sf) {
    int i = operands[0];
    int cpIndex = operands[1];
    int j = operands[2];
    TypeRefCPEntry typeRefCPEntry = (TypeRefCPEntry) ctx.constPool[cpIndex];
    BMap<String, BValue> bMap = (BMap<String, BValue>) sf.refRegs[i];
    if (bMap == null) {
        handleNullRefError(ctx);
        return;
    }
    int longRegIndex = -1;
    int doubleRegIndex = -1;
    int stringRegIndex = -1;
    int booleanRegIndex = -1;
    int blobRegIndex = -1;
    int refRegIndex = -1;
    BStructType structType = (BStructType) typeRefCPEntry.getType();
    BStruct bStruct = new BStruct(structType);
    StructInfo structInfo = ctx.callableUnitInfo.getPackageInfo().getStructInfo(structType.getName());
    Set<String> keys = bMap.keySet();
    for (StructFieldInfo fieldInfo : structInfo.getFieldInfoEntries()) {
        String key = fieldInfo.getName();
        BType fieldType = fieldInfo.getFieldType();
        BValue mapVal = null;
        try {
            boolean containsField = keys.contains(key);
            DefaultValueAttributeInfo defaultValAttrInfo = null;
            if (containsField) {
                mapVal = bMap.get(key);
                if (mapVal == null && BTypes.isValueType(fieldType)) {
                    throw BLangExceptionHelper.getRuntimeException(RuntimeErrors.INCOMPATIBLE_FIELD_TYPE_FOR_CASTING, key, fieldType, null);
                }
                if (mapVal != null && !checkCast(mapVal, fieldType)) {
                    throw BLangExceptionHelper.getRuntimeException(RuntimeErrors.INCOMPATIBLE_FIELD_TYPE_FOR_CASTING, key, fieldType, mapVal.getType());
                }
            } else {
                defaultValAttrInfo = (DefaultValueAttributeInfo) getAttributeInfo(fieldInfo, AttributeInfo.Kind.DEFAULT_VALUE_ATTRIBUTE);
            }
            switch(fieldType.getTag()) {
                case TypeTags.INT_TAG:
                    longRegIndex++;
                    if (containsField) {
                        bStruct.setIntField(longRegIndex, ((BInteger) mapVal).intValue());
                    } else if (defaultValAttrInfo != null) {
                        bStruct.setIntField(longRegIndex, defaultValAttrInfo.getDefaultValue().getIntValue());
                    }
                    break;
                case TypeTags.FLOAT_TAG:
                    doubleRegIndex++;
                    if (containsField) {
                        bStruct.setFloatField(doubleRegIndex, ((BFloat) mapVal).floatValue());
                    } else if (defaultValAttrInfo != null) {
                        bStruct.setFloatField(doubleRegIndex, defaultValAttrInfo.getDefaultValue().getFloatValue());
                    }
                    break;
                case TypeTags.STRING_TAG:
                    stringRegIndex++;
                    if (containsField) {
                        bStruct.setStringField(stringRegIndex, ((BString) mapVal).stringValue());
                    } else if (defaultValAttrInfo != null) {
                        bStruct.setStringField(stringRegIndex, defaultValAttrInfo.getDefaultValue().getStringValue());
                    }
                    break;
                case TypeTags.BOOLEAN_TAG:
                    booleanRegIndex++;
                    if (containsField) {
                        bStruct.setBooleanField(booleanRegIndex, ((BBoolean) mapVal).booleanValue() ? 1 : 0);
                    } else if (defaultValAttrInfo != null) {
                        bStruct.setBooleanField(booleanRegIndex, defaultValAttrInfo.getDefaultValue().getBooleanValue() ? 1 : 0);
                    }
                    break;
                case TypeTags.BLOB_TAG:
                    blobRegIndex++;
                    if (containsField && mapVal != null) {
                        bStruct.setBlobField(blobRegIndex, ((BBlob) mapVal).blobValue());
                    }
                    break;
                default:
                    bStruct.setRefField(++refRegIndex, (BRefType) mapVal);
            }
        } catch (BallerinaException e) {
            sf.refRegs[j] = null;
            String errorMsg = "cannot convert '" + bMap.getType() + "' to type '" + structType + ": " + e.getMessage();
            handleTypeConversionError(ctx, sf, j, errorMsg);
            return;
        }
    }
    sf.refRegs[j] = bStruct;
}
Also used : BStruct(org.ballerinalang.model.values.BStruct) BMap(org.ballerinalang.model.values.BMap) StructInfo(org.ballerinalang.util.codegen.StructInfo) TypeRefCPEntry(org.ballerinalang.util.codegen.cpentries.TypeRefCPEntry) BValue(org.ballerinalang.model.values.BValue) BBoolean(org.ballerinalang.model.values.BBoolean) StructFieldInfo(org.ballerinalang.util.codegen.StructFieldInfo) BString(org.ballerinalang.model.values.BString) BStructType(org.ballerinalang.model.types.BStructType) DefaultValueAttributeInfo(org.ballerinalang.util.codegen.attributes.DefaultValueAttributeInfo) BType(org.ballerinalang.model.types.BType) BallerinaException(org.ballerinalang.util.exceptions.BallerinaException)

Example 5 with TypeRefCPEntry

use of org.ballerinalang.util.codegen.cpentries.TypeRefCPEntry in project ballerina by ballerina-lang.

the class ProgramFileReader method readInstructions.

private void readInstructions(DataInputStream dataInStream, PackageInfo packageInfo) throws IOException {
    int codeLength = dataInStream.readInt();
    byte[] code = new byte[codeLength];
    // Ignore bytes read should be same as the code length.
    dataInStream.read(code);
    DataInputStream codeStream = new DataInputStream(new ByteArrayInputStream(code));
    while (codeStream.available() > 0) {
        int i, j, k, h;
        int funcRefCPIndex;
        FunctionRefCPEntry funcRefCPEntry;
        int flags;
        int[] argRegs;
        int[] retRegs;
        int opcode = codeStream.readUnsignedByte();
        switch(opcode) {
            case InstructionCodes.HALT:
            case InstructionCodes.RET:
                packageInfo.addInstruction(InstructionFactory.get(opcode));
                break;
            case InstructionCodes.ICONST_0:
            case InstructionCodes.ICONST_1:
            case InstructionCodes.ICONST_2:
            case InstructionCodes.ICONST_3:
            case InstructionCodes.ICONST_4:
            case InstructionCodes.ICONST_5:
            case InstructionCodes.FCONST_0:
            case InstructionCodes.FCONST_1:
            case InstructionCodes.FCONST_2:
            case InstructionCodes.FCONST_3:
            case InstructionCodes.FCONST_4:
            case InstructionCodes.FCONST_5:
            case InstructionCodes.BCONST_0:
            case InstructionCodes.BCONST_1:
            case InstructionCodes.RCONST_NULL:
            case InstructionCodes.GOTO:
            case InstructionCodes.THROW:
            case InstructionCodes.ERRSTORE:
            case InstructionCodes.NEWXMLSEQ:
                i = codeStream.readInt();
                packageInfo.addInstruction(InstructionFactory.get(opcode, i));
                break;
            case InstructionCodes.ICONST:
            case InstructionCodes.FCONST:
            case InstructionCodes.SCONST:
            case InstructionCodes.IMOVE:
            case InstructionCodes.FMOVE:
            case InstructionCodes.SMOVE:
            case InstructionCodes.BMOVE:
            case InstructionCodes.LMOVE:
            case InstructionCodes.RMOVE:
            case InstructionCodes.IGLOAD:
            case InstructionCodes.FGLOAD:
            case InstructionCodes.SGLOAD:
            case InstructionCodes.BGLOAD:
            case InstructionCodes.LGLOAD:
            case InstructionCodes.RGLOAD:
            case InstructionCodes.IGSTORE:
            case InstructionCodes.FGSTORE:
            case InstructionCodes.SGSTORE:
            case InstructionCodes.BGSTORE:
            case InstructionCodes.LGSTORE:
            case InstructionCodes.RGSTORE:
            case InstructionCodes.INEG:
            case InstructionCodes.FNEG:
            case InstructionCodes.BNOT:
            case InstructionCodes.REQ_NULL:
            case InstructionCodes.RNE_NULL:
            case InstructionCodes.BR_TRUE:
            case InstructionCodes.BR_FALSE:
            case InstructionCodes.TR_END:
            case InstructionCodes.FPLOAD:
            case InstructionCodes.ARRAYLEN:
            case InstructionCodes.INEWARRAY:
            case InstructionCodes.FNEWARRAY:
            case InstructionCodes.SNEWARRAY:
            case InstructionCodes.BNEWARRAY:
            case InstructionCodes.LNEWARRAY:
            case InstructionCodes.RNEWARRAY:
            case InstructionCodes.JSONNEWARRAY:
            case InstructionCodes.NEWSTRUCT:
            case InstructionCodes.NEWCONNECTOR:
            case InstructionCodes.ITR_NEW:
            case InstructionCodes.ITR_HAS_NEXT:
            case InstructionCodes.IRET:
            case InstructionCodes.FRET:
            case InstructionCodes.SRET:
            case InstructionCodes.BRET:
            case InstructionCodes.LRET:
            case InstructionCodes.RRET:
            case InstructionCodes.XML2XMLATTRS:
            case InstructionCodes.NEWXMLCOMMENT:
            case InstructionCodes.NEWXMLTEXT:
            case InstructionCodes.XMLSEQSTORE:
            case InstructionCodes.TYPEOF:
            case InstructionCodes.TYPELOAD:
            case InstructionCodes.SEQ_NULL:
            case InstructionCodes.SNE_NULL:
            case InstructionCodes.NEWJSON:
            case InstructionCodes.NEWMAP:
            case InstructionCodes.NEWTABLE:
            case InstructionCodes.I2ANY:
            case InstructionCodes.F2ANY:
            case InstructionCodes.S2ANY:
            case InstructionCodes.B2ANY:
            case InstructionCodes.L2ANY:
            case InstructionCodes.ANY2I:
            case InstructionCodes.ANY2F:
            case InstructionCodes.ANY2S:
            case InstructionCodes.ANY2B:
            case InstructionCodes.ANY2L:
            case InstructionCodes.ANY2JSON:
            case InstructionCodes.ANY2XML:
            case InstructionCodes.ANY2MAP:
            case InstructionCodes.ANY2TYPE:
            case InstructionCodes.ANY2DT:
            case InstructionCodes.NULL2JSON:
            case InstructionCodes.I2F:
            case InstructionCodes.I2S:
            case InstructionCodes.I2B:
            case InstructionCodes.I2JSON:
            case InstructionCodes.F2I:
            case InstructionCodes.F2S:
            case InstructionCodes.F2B:
            case InstructionCodes.F2JSON:
            case InstructionCodes.S2I:
            case InstructionCodes.S2F:
            case InstructionCodes.S2B:
            case InstructionCodes.S2JSON:
            case InstructionCodes.B2I:
            case InstructionCodes.B2F:
            case InstructionCodes.B2S:
            case InstructionCodes.B2JSON:
            case InstructionCodes.JSON2I:
            case InstructionCodes.JSON2F:
            case InstructionCodes.JSON2S:
            case InstructionCodes.JSON2B:
            case InstructionCodes.DT2XML:
            case InstructionCodes.DT2JSON:
            case InstructionCodes.T2MAP:
            case InstructionCodes.T2JSON:
            case InstructionCodes.XML2JSON:
            case InstructionCodes.JSON2XML:
            case InstructionCodes.XMLATTRS2MAP:
            case InstructionCodes.ANY2SCONV:
            case InstructionCodes.S2XML:
            case InstructionCodes.XML2S:
            case InstructionCodes.S2JSONX:
            case InstructionCodes.NULL2S:
            case InstructionCodes.AWAIT:
            case InstructionCodes.CHECK_CONVERSION:
            case InstructionCodes.XMLLOADALL:
                i = codeStream.readInt();
                j = codeStream.readInt();
                packageInfo.addInstruction(InstructionFactory.get(opcode, i, j));
                break;
            case InstructionCodes.IALOAD:
            case InstructionCodes.FALOAD:
            case InstructionCodes.SALOAD:
            case InstructionCodes.BALOAD:
            case InstructionCodes.LALOAD:
            case InstructionCodes.RALOAD:
            case InstructionCodes.JSONALOAD:
            case InstructionCodes.IASTORE:
            case InstructionCodes.FASTORE:
            case InstructionCodes.SASTORE:
            case InstructionCodes.BASTORE:
            case InstructionCodes.LASTORE:
            case InstructionCodes.RASTORE:
            case InstructionCodes.JSONASTORE:
            case InstructionCodes.IFIELDLOAD:
            case InstructionCodes.FFIELDLOAD:
            case InstructionCodes.SFIELDLOAD:
            case InstructionCodes.BFIELDLOAD:
            case InstructionCodes.LFIELDLOAD:
            case InstructionCodes.RFIELDLOAD:
            case InstructionCodes.IFIELDSTORE:
            case InstructionCodes.FFIELDSTORE:
            case InstructionCodes.SFIELDSTORE:
            case InstructionCodes.BFIELDSTORE:
            case InstructionCodes.LFIELDSTORE:
            case InstructionCodes.RFIELDSTORE:
            case InstructionCodes.MAPLOAD:
            case InstructionCodes.MAPSTORE:
            case InstructionCodes.JSONLOAD:
            case InstructionCodes.JSONSTORE:
            case InstructionCodes.ENUMERATORLOAD:
            case InstructionCodes.IADD:
            case InstructionCodes.FADD:
            case InstructionCodes.SADD:
            case InstructionCodes.XMLADD:
            case InstructionCodes.ISUB:
            case InstructionCodes.FSUB:
            case InstructionCodes.IMUL:
            case InstructionCodes.FMUL:
            case InstructionCodes.IDIV:
            case InstructionCodes.FDIV:
            case InstructionCodes.IMOD:
            case InstructionCodes.FMOD:
            case InstructionCodes.IEQ:
            case InstructionCodes.FEQ:
            case InstructionCodes.SEQ:
            case InstructionCodes.BEQ:
            case InstructionCodes.REQ:
            case InstructionCodes.INE:
            case InstructionCodes.FNE:
            case InstructionCodes.SNE:
            case InstructionCodes.BNE:
            case InstructionCodes.RNE:
            case InstructionCodes.IGT:
            case InstructionCodes.FGT:
            case InstructionCodes.IGE:
            case InstructionCodes.FGE:
            case InstructionCodes.ILT:
            case InstructionCodes.FLT:
            case InstructionCodes.ILE:
            case InstructionCodes.FLE:
            case InstructionCodes.XMLATTRLOAD:
            case InstructionCodes.XMLATTRSTORE:
            case InstructionCodes.S2QNAME:
            case InstructionCodes.NEWXMLPI:
            case InstructionCodes.TEQ:
            case InstructionCodes.TNE:
            case InstructionCodes.XMLLOAD:
            case InstructionCodes.NEW_INT_RANGE:
            case InstructionCodes.LENGTHOF:
            case InstructionCodes.NEWSTREAM:
            case InstructionCodes.CHECKCAST:
            case InstructionCodes.MAP2T:
            case InstructionCodes.JSON2T:
            case InstructionCodes.ANY2T:
            case InstructionCodes.ANY2C:
            case InstructionCodes.ANY2E:
            case InstructionCodes.IS_ASSIGNABLE:
            case InstructionCodes.TR_RETRY:
            case InstructionCodes.XMLSEQLOAD:
                i = codeStream.readInt();
                j = codeStream.readInt();
                k = codeStream.readInt();
                packageInfo.addInstruction(InstructionFactory.get(opcode, i, j, k));
                break;
            case InstructionCodes.NEWQNAME:
            case InstructionCodes.NEWXMLELEMENT:
            case InstructionCodes.TR_BEGIN:
                i = codeStream.readInt();
                j = codeStream.readInt();
                k = codeStream.readInt();
                h = codeStream.readInt();
                packageInfo.addInstruction(InstructionFactory.get(opcode, i, j, k, h));
                break;
            case InstructionCodes.CALL:
                funcRefCPIndex = codeStream.readInt();
                flags = codeStream.readInt();
                funcRefCPEntry = (FunctionRefCPEntry) packageInfo.getCPEntry(funcRefCPIndex);
                packageInfo.addInstruction(new InstructionCALL(opcode, funcRefCPIndex, funcRefCPEntry.getFunctionInfo(), flags, getArgRegs(codeStream), getArgRegs(codeStream)));
                break;
            case InstructionCodes.VCALL:
                int receiverRegIndex = codeStream.readInt();
                funcRefCPIndex = codeStream.readInt();
                flags = codeStream.readInt();
                funcRefCPEntry = (FunctionRefCPEntry) packageInfo.getCPEntry(funcRefCPIndex);
                packageInfo.addInstruction(new InstructionVCALL(opcode, receiverRegIndex, funcRefCPIndex, funcRefCPEntry.getFunctionInfo(), flags, getArgRegs(codeStream), getArgRegs(codeStream)));
                break;
            case InstructionCodes.ACALL:
                int actionRefCPIndex = codeStream.readInt();
                flags = codeStream.readInt();
                ActionRefCPEntry actionRefCPEntry = (ActionRefCPEntry) packageInfo.getCPEntry(actionRefCPIndex);
                packageInfo.addInstruction(new InstructionACALL(opcode, actionRefCPIndex, actionRefCPEntry.getActionName(), flags, getArgRegs(codeStream), getArgRegs(codeStream)));
                break;
            case InstructionCodes.FPCALL:
                funcRefCPIndex = codeStream.readInt();
                flags = codeStream.readInt();
                argRegs = getArgRegs(codeStream);
                retRegs = getArgRegs(codeStream);
                FunctionCallCPEntry funcCallCPEntry = new FunctionCallCPEntry(flags, argRegs, retRegs);
                int funcCallCPIndex = packageInfo.addCPEntry(funcCallCPEntry);
                packageInfo.addInstruction(InstructionFactory.get(opcode, funcRefCPIndex, funcCallCPIndex));
                break;
            case InstructionCodes.TCALL:
                int transformCPIndex = codeStream.readInt();
                flags = codeStream.readInt();
                TransformerRefCPEntry transformerRefCPEntry = (TransformerRefCPEntry) packageInfo.getCPEntry(transformCPIndex);
                packageInfo.addInstruction(new InstructionTCALL(opcode, transformCPIndex, transformerRefCPEntry.getTransformerInfo(), flags, getArgRegs(codeStream), getArgRegs(codeStream)));
                break;
            case InstructionCodes.WRKSEND:
            case InstructionCodes.WRKRECEIVE:
                int channelRefCPIndex = codeStream.readInt();
                WorkerDataChannelRefCPEntry channelRefCPEntry = (WorkerDataChannelRefCPEntry) packageInfo.getCPEntry(channelRefCPIndex);
                int sigCPIndex = codeStream.readInt();
                UTF8CPEntry sigCPEntry = (UTF8CPEntry) packageInfo.getCPEntry(sigCPIndex);
                BType[] bTypes = getParamTypes(sigCPEntry.getValue(), packageInfo);
                packageInfo.addInstruction(new InstructionWRKSendReceive(opcode, channelRefCPIndex, channelRefCPEntry.getWorkerDataChannelInfo(), sigCPIndex, bTypes, getArgRegs(codeStream)));
                break;
            case InstructionCodes.FORKJOIN:
                int forkJoinIndexCPIndex = codeStream.readInt();
                ForkJoinCPEntry forkJoinIndexCPEntry = (ForkJoinCPEntry) packageInfo.getCPEntry(forkJoinIndexCPIndex);
                int timeoutRegIndex = codeStream.readInt();
                int joinVarRegIndex = codeStream.readInt();
                int joinBlockAddr = codeStream.readInt();
                int timeoutVarRegIndex = codeStream.readInt();
                int timeoutBlockAddr = codeStream.readInt();
                packageInfo.addInstruction(new InstructionFORKJOIN(opcode, forkJoinIndexCPIndex, forkJoinIndexCPEntry, timeoutRegIndex, joinVarRegIndex, joinBlockAddr, timeoutVarRegIndex, timeoutBlockAddr));
                break;
            case InstructionCodes.ITR_NEXT:
                int iteratorIndex = codeStream.readInt();
                int[] typeTags = getArgRegs(codeStream);
                retRegs = getArgRegs(codeStream);
                packageInfo.addInstruction(new InstructionIteratorNext(opcode, iteratorIndex, retRegs.length, typeTags, retRegs));
                break;
            case InstructionCodes.LOCK:
            case InstructionCodes.UNLOCK:
                int varCount = codeStream.readInt();
                BType[] varTypes = new BType[varCount];
                int[] varRegs = new int[varCount];
                for (int m = 0; m < varCount; m++) {
                    int varSigCPIndex = codeStream.readInt();
                    TypeRefCPEntry typeRefCPEntry = (TypeRefCPEntry) packageInfo.getCPEntry(varSigCPIndex);
                    varTypes[m] = typeRefCPEntry.getType();
                    varRegs[m] = codeStream.readInt();
                }
                packageInfo.addInstruction(new InstructionLock(opcode, varTypes, varRegs));
                break;
            default:
                throw new ProgramFileFormatException("unknown opcode " + opcode + " in package " + packageInfo.getPkgPath());
        }
    }
}
Also used : TransformerRefCPEntry(org.ballerinalang.util.codegen.cpentries.TransformerRefCPEntry) TypeRefCPEntry(org.ballerinalang.util.codegen.cpentries.TypeRefCPEntry) ActionRefCPEntry(org.ballerinalang.util.codegen.cpentries.ActionRefCPEntry) InstructionIteratorNext(org.ballerinalang.util.codegen.Instruction.InstructionIteratorNext) ProgramFileFormatException(org.ballerinalang.util.exceptions.ProgramFileFormatException) InstructionCALL(org.ballerinalang.util.codegen.Instruction.InstructionCALL) InstructionTCALL(org.ballerinalang.util.codegen.Instruction.InstructionTCALL) WorkerDataChannelRefCPEntry(org.ballerinalang.util.codegen.cpentries.WorkerDataChannelRefCPEntry) InstructionWRKSendReceive(org.ballerinalang.util.codegen.Instruction.InstructionWRKSendReceive) DataInputStream(java.io.DataInputStream) InstructionVCALL(org.ballerinalang.util.codegen.Instruction.InstructionVCALL) FunctionCallCPEntry(org.ballerinalang.util.codegen.cpentries.FunctionCallCPEntry) UTF8CPEntry(org.ballerinalang.util.codegen.cpentries.UTF8CPEntry) FunctionRefCPEntry(org.ballerinalang.util.codegen.cpentries.FunctionRefCPEntry) InstructionACALL(org.ballerinalang.util.codegen.Instruction.InstructionACALL) InstructionLock(org.ballerinalang.util.codegen.Instruction.InstructionLock) ByteArrayInputStream(java.io.ByteArrayInputStream) BType(org.ballerinalang.model.types.BType) ForkJoinCPEntry(org.ballerinalang.util.codegen.cpentries.ForkJoinCPEntry) InstructionFORKJOIN(org.ballerinalang.util.codegen.Instruction.InstructionFORKJOIN)

Aggregations

TypeRefCPEntry (org.ballerinalang.util.codegen.cpentries.TypeRefCPEntry)12 BString (org.ballerinalang.model.values.BString)6 BJSON (org.ballerinalang.model.values.BJSON)5 BStructType (org.ballerinalang.model.types.BStructType)4 BType (org.ballerinalang.model.types.BType)4 FunctionRefCPEntry (org.ballerinalang.util.codegen.cpentries.FunctionRefCPEntry)4 BRefType (org.ballerinalang.model.values.BRefType)3 BValue (org.ballerinalang.model.values.BValue)3 StructureRefCPEntry (org.ballerinalang.util.codegen.cpentries.StructureRefCPEntry)3 BallerinaException (org.ballerinalang.util.exceptions.BallerinaException)3 BBlobArray (org.ballerinalang.model.values.BBlobArray)2 BBoolean (org.ballerinalang.model.values.BBoolean)2 BBooleanArray (org.ballerinalang.model.values.BBooleanArray)2 BFloatArray (org.ballerinalang.model.values.BFloatArray)2 BIntArray (org.ballerinalang.model.values.BIntArray)2 BRefValueArray (org.ballerinalang.model.values.BRefValueArray)2 BStringArray (org.ballerinalang.model.values.BStringArray)2 BStruct (org.ballerinalang.model.values.BStruct)2 InstructionACALL (org.ballerinalang.util.codegen.Instruction.InstructionACALL)2 InstructionCALL (org.ballerinalang.util.codegen.Instruction.InstructionCALL)2