Search in sources :

Example 1 with StructureRefCPEntry

use of org.wso2.ballerinalang.programfile.cpentries.StructureRefCPEntry in project ballerina by ballerina-lang.

the class PackageInfoWriter method writeCP.

public static void writeCP(DataOutputStream dataOutStream, ConstantPoolEntry[] constPool) throws IOException {
    dataOutStream.writeInt(constPool.length);
    for (ConstantPoolEntry cpEntry : constPool) {
        // Emitting the kind of the constant pool entry.
        dataOutStream.writeByte(cpEntry.getEntryType().getValue());
        int nameCPIndex;
        switch(cpEntry.getEntryType()) {
            case CP_ENTRY_UTF8:
                String stringVal = ((UTF8CPEntry) cpEntry).getValue();
                if (stringVal != null) {
                    byte[] bytes = toUTF(stringVal);
                    dataOutStream.writeShort(bytes.length);
                    dataOutStream.write(bytes);
                } else {
                    // If the string value is null, we write the size as -1.
                    // This marks that the value followed by -1 size is a null value.
                    dataOutStream.writeShort(NULL_VALUE_FIELD_SIZE_TAG);
                }
                break;
            case CP_ENTRY_INTEGER:
                long longVal = ((IntegerCPEntry) cpEntry).getValue();
                dataOutStream.writeLong(longVal);
                break;
            case CP_ENTRY_FLOAT:
                double doubleVal = ((FloatCPEntry) cpEntry).getValue();
                dataOutStream.writeDouble(doubleVal);
                break;
            case CP_ENTRY_STRING:
                nameCPIndex = ((StringCPEntry) cpEntry).getStringCPIndex();
                dataOutStream.writeInt(nameCPIndex);
                break;
            case CP_ENTRY_PACKAGE:
                nameCPIndex = ((PackageRefCPEntry) cpEntry).nameCPIndex;
                dataOutStream.writeInt(nameCPIndex);
                break;
            case CP_ENTRY_FUNCTION_REF:
                FunctionRefCPEntry funcRefEntry = (FunctionRefCPEntry) cpEntry;
                dataOutStream.writeInt(funcRefEntry.packageCPIndex);
                dataOutStream.writeInt(funcRefEntry.nameCPIndex);
                break;
            case CP_ENTRY_ACTION_REF:
                ActionRefCPEntry actionRefEntry = (ActionRefCPEntry) cpEntry;
                dataOutStream.writeInt(actionRefEntry.getPackageCPIndex());
                dataOutStream.writeInt(actionRefEntry.getNameCPIndex());
                break;
            case CP_ENTRY_STRUCTURE_REF:
                StructureRefCPEntry structureRefCPEntry = (StructureRefCPEntry) cpEntry;
                dataOutStream.writeInt(structureRefCPEntry.packageCPIndex);
                dataOutStream.writeInt(structureRefCPEntry.nameCPIndex);
                break;
            case CP_ENTRY_TYPE_REF:
                TypeRefCPEntry typeRefCPEntry = (TypeRefCPEntry) cpEntry;
                dataOutStream.writeInt(typeRefCPEntry.typeSigCPIndex);
                break;
            case CP_ENTRY_FORK_JOIN:
                ForkJoinCPEntry forkJoinCPEntry = (ForkJoinCPEntry) cpEntry;
                dataOutStream.writeInt(forkJoinCPEntry.forkJoinInfoIndex);
                break;
            case CP_ENTRY_WRKR_DATA_CHNL_REF:
                WorkerDataChannelRefCPEntry workerDataChannelCPEntry = (WorkerDataChannelRefCPEntry) cpEntry;
                dataOutStream.writeInt(workerDataChannelCPEntry.getUniqueNameCPIndex());
                break;
            case CP_ENTRY_TRANSFORMER_REF:
                TransformerRefCPEntry transformerRefEntry = (TransformerRefCPEntry) cpEntry;
                dataOutStream.writeInt(transformerRefEntry.packageCPIndex);
                dataOutStream.writeInt(transformerRefEntry.nameCPIndex);
                break;
        }
    }
}
Also used : TransformerRefCPEntry(org.wso2.ballerinalang.programfile.cpentries.TransformerRefCPEntry) TypeRefCPEntry(org.wso2.ballerinalang.programfile.cpentries.TypeRefCPEntry) ActionRefCPEntry(org.wso2.ballerinalang.programfile.cpentries.ActionRefCPEntry) FloatCPEntry(org.wso2.ballerinalang.programfile.cpentries.FloatCPEntry) WorkerDataChannelRefCPEntry(org.wso2.ballerinalang.programfile.cpentries.WorkerDataChannelRefCPEntry) ConstantPoolEntry(org.wso2.ballerinalang.programfile.cpentries.ConstantPoolEntry) UTF8CPEntry(org.wso2.ballerinalang.programfile.cpentries.UTF8CPEntry) FunctionRefCPEntry(org.wso2.ballerinalang.programfile.cpentries.FunctionRefCPEntry) ForkJoinCPEntry(org.wso2.ballerinalang.programfile.cpentries.ForkJoinCPEntry) StructureRefCPEntry(org.wso2.ballerinalang.programfile.cpentries.StructureRefCPEntry) IntegerCPEntry(org.wso2.ballerinalang.programfile.cpentries.IntegerCPEntry)

Example 2 with StructureRefCPEntry

use of org.wso2.ballerinalang.programfile.cpentries.StructureRefCPEntry in project ballerina by ballerina-lang.

the class ErrorTableEntry method setPackageInfo.

public void setPackageInfo(PackageInfo packageInfo) {
    this.packageInfo = packageInfo;
    // Load Cache values.
    if (errorStructCPIndex < 0) {
        return;
    }
    StructureRefCPEntry structureRefCPEntry = (StructureRefCPEntry) packageInfo.getCPEntry(errorStructCPIndex);
// this.error = (StructInfo) structureRefCPEntry.getStructureTypeInfo();
}
Also used : StructureRefCPEntry(org.wso2.ballerinalang.programfile.cpentries.StructureRefCPEntry)

Example 3 with StructureRefCPEntry

use of org.wso2.ballerinalang.programfile.cpentries.StructureRefCPEntry in project ballerina by ballerina-lang.

the class CodeGenerator method visit.

public void visit(BLangTryCatchFinally tryNode) {
    Operand gotoTryCatchEndAddr = getOperand(-1);
    Instruction instructGotoTryCatchEnd = InstructionFactory.get(InstructionCodes.GOTO, gotoTryCatchEndAddr);
    List<int[]> unhandledErrorRangeList = new ArrayList<>();
    ErrorTableAttributeInfo errorTable = createErrorTableIfAbsent(currentPkgInfo);
    // Handle try block.
    int fromIP = nextIP();
    genNode(tryNode.tryBody, env);
    int toIP = nextIP() - 1;
    // Append finally block instructions.
    if (tryNode.finallyBody != null) {
        genNode(tryNode.finallyBody, env);
    }
    emit(instructGotoTryCatchEnd);
    unhandledErrorRangeList.add(new int[] { fromIP, toIP });
    // Handle catch blocks.
    int order = 0;
    for (BLangCatch bLangCatch : tryNode.getCatchBlocks()) {
        addLineNumberInfo(bLangCatch.pos);
        int targetIP = nextIP();
        genNode(bLangCatch, env);
        unhandledErrorRangeList.add(new int[] { targetIP, nextIP() - 1 });
        // Append finally block instructions.
        if (tryNode.finallyBody != null) {
            genNode(tryNode.finallyBody, env);
        }
        emit(instructGotoTryCatchEnd);
        // Create Error table entry for this catch block
        BTypeSymbol structSymbol = bLangCatch.param.symbol.type.tsymbol;
        BPackageSymbol packageSymbol = (BPackageSymbol) bLangCatch.param.symbol.type.tsymbol.owner;
        int pkgCPIndex = addPackageRefCPEntry(currentPkgInfo, packageSymbol.pkgID);
        int structNameCPIndex = addUTF8CPEntry(currentPkgInfo, structSymbol.name.value);
        StructureRefCPEntry structureRefCPEntry = new StructureRefCPEntry(pkgCPIndex, structNameCPIndex);
        int structCPEntryIndex = currentPkgInfo.addCPEntry(structureRefCPEntry);
        StructInfo errorStructInfo = this.programFile.packageInfoMap.get(packageSymbol.pkgID.bvmAlias()).getStructInfo(structSymbol.name.value);
        ErrorTableEntry errorTableEntry = new ErrorTableEntry(fromIP, toIP, targetIP, order++, structCPEntryIndex);
        errorTableEntry.setError(errorStructInfo);
        errorTable.addErrorTableEntry(errorTableEntry);
    }
    if (tryNode.finallyBody != null) {
        // Create Error table entry for unhandled errors in try and catch(s) blocks
        for (int[] range : unhandledErrorRangeList) {
            ErrorTableEntry errorTableEntry = new ErrorTableEntry(range[0], range[1], nextIP(), order++, -1);
            errorTable.addErrorTableEntry(errorTableEntry);
        }
        // Append finally block instruction.
        genNode(tryNode.finallyBody, env);
        emit(InstructionFactory.get(InstructionCodes.THROW, getOperand(-1)));
    }
    gotoTryCatchEndAddr.value = nextIP();
}
Also used : StructInfo(org.wso2.ballerinalang.programfile.StructInfo) Operand(org.wso2.ballerinalang.programfile.Instruction.Operand) ArrayList(java.util.ArrayList) BLangCatch(org.wso2.ballerinalang.compiler.tree.statements.BLangCatch) BTypeSymbol(org.wso2.ballerinalang.compiler.semantics.model.symbols.BTypeSymbol) Instruction(org.wso2.ballerinalang.programfile.Instruction) BLangEndpoint(org.wso2.ballerinalang.compiler.tree.BLangEndpoint) BPackageSymbol(org.wso2.ballerinalang.compiler.semantics.model.symbols.BPackageSymbol) ErrorTableAttributeInfo(org.wso2.ballerinalang.programfile.attributes.ErrorTableAttributeInfo) StructureRefCPEntry(org.wso2.ballerinalang.programfile.cpentries.StructureRefCPEntry) ErrorTableEntry(org.wso2.ballerinalang.programfile.ErrorTableEntry)

Example 4 with StructureRefCPEntry

use of org.wso2.ballerinalang.programfile.cpentries.StructureRefCPEntry in project ballerina by ballerina-lang.

the class CodeGenerator method visit.

@Override
public void visit(BLangStructLiteral structLiteral) {
    BStructSymbol structSymbol = (BStructSymbol) structLiteral.type.tsymbol;
    int pkgCPIndex = addPackageRefCPEntry(currentPkgInfo, structSymbol.pkgID);
    int structNameCPIndex = addUTF8CPEntry(currentPkgInfo, structSymbol.name.value);
    StructureRefCPEntry structureRefCPEntry = new StructureRefCPEntry(pkgCPIndex, structNameCPIndex);
    Operand structCPIndex = getOperand(currentPkgInfo.addCPEntry(structureRefCPEntry));
    // Emit an instruction to create a new struct.
    RegIndex structRegIndex = calcAndGetExprRegIndex(structLiteral);
    emit(InstructionCodes.NEWSTRUCT, structCPIndex, structRegIndex);
    // Invoke the struct default values init function here.
    if (structSymbol.defaultsValuesInitFunc != null) {
        int funcRefCPIndex = getFuncRefCPIndex(structSymbol.defaultsValuesInitFunc.symbol);
        // call funcRefCPIndex 1 structRegIndex 0
        Operand[] operands = new Operand[5];
        operands[0] = getOperand(funcRefCPIndex);
        operands[1] = getOperand(false);
        operands[2] = getOperand(1);
        operands[3] = structRegIndex;
        operands[4] = getOperand(0);
        emit(InstructionCodes.CALL, operands);
    }
    // Invoke the struct initializer here.
    if (structLiteral.initializer != null) {
        int funcRefCPIndex = getFuncRefCPIndex(structLiteral.initializer.symbol);
        // call funcRefCPIndex 1 structRegIndex 0
        Operand[] operands = new Operand[5];
        operands[0] = getOperand(funcRefCPIndex);
        operands[1] = getOperand(false);
        operands[2] = getOperand(1);
        operands[3] = structRegIndex;
        operands[4] = getOperand(0);
        emit(InstructionCodes.CALL, operands);
    }
    // Generate code the struct literal.
    for (BLangRecordKeyValue keyValue : structLiteral.keyValuePairs) {
        BLangRecordKey key = keyValue.key;
        Operand fieldIndex = key.fieldSymbol.varIndex;
        genNode(keyValue.valueExpr, this.env);
        int opcode = getOpcode(key.fieldSymbol.type.tag, InstructionCodes.IFIELDSTORE);
        emit(opcode, structRegIndex, fieldIndex, keyValue.valueExpr.regIndex);
    }
}
Also used : BLangRecordKey(org.wso2.ballerinalang.compiler.tree.expressions.BLangRecordLiteral.BLangRecordKey) Operand(org.wso2.ballerinalang.programfile.Instruction.Operand) StructureRefCPEntry(org.wso2.ballerinalang.programfile.cpentries.StructureRefCPEntry) BLangRecordKeyValue(org.wso2.ballerinalang.compiler.tree.expressions.BLangRecordLiteral.BLangRecordKeyValue) BStructSymbol(org.wso2.ballerinalang.compiler.semantics.model.symbols.BStructSymbol) BLangEndpoint(org.wso2.ballerinalang.compiler.tree.BLangEndpoint) RegIndex(org.wso2.ballerinalang.programfile.Instruction.RegIndex)

Example 5 with StructureRefCPEntry

use of org.wso2.ballerinalang.programfile.cpentries.StructureRefCPEntry in project ballerina by ballerina-lang.

the class CodeGenerator method visit.

public void visit(BLangTypeInit cIExpr) {
    BSymbol structSymbol = cIExpr.type.tsymbol;
    int pkgCPIndex = addPackageRefCPEntry(currentPkgInfo, structSymbol.pkgID);
    int structNameCPIndex = addUTF8CPEntry(currentPkgInfo, structSymbol.name.value);
    StructureRefCPEntry structureRefCPEntry = new StructureRefCPEntry(pkgCPIndex, structNameCPIndex);
    Operand structCPIndex = getOperand(currentPkgInfo.addCPEntry(structureRefCPEntry));
    // Emit an instruction to create a new struct.
    RegIndex structRegIndex = calcAndGetExprRegIndex(cIExpr);
    emit(InstructionCodes.NEWSTRUCT, structCPIndex, structRegIndex);
    // Invoke the struct initializer here.
    Operand[] operands = getFuncOperands(cIExpr.objectInitInvocation);
    Operand[] callOperands = new Operand[operands.length + 1];
    callOperands[0] = operands[0];
    callOperands[1] = operands[1];
    callOperands[2] = getOperand(operands[2].value + 1);
    callOperands[3] = structRegIndex;
    System.arraycopy(operands, 3, callOperands, 4, operands.length - 3);
    emit(InstructionCodes.CALL, callOperands);
}
Also used : BSymbol(org.wso2.ballerinalang.compiler.semantics.model.symbols.BSymbol) Operand(org.wso2.ballerinalang.programfile.Instruction.Operand) StructureRefCPEntry(org.wso2.ballerinalang.programfile.cpentries.StructureRefCPEntry) BLangEndpoint(org.wso2.ballerinalang.compiler.tree.BLangEndpoint) RegIndex(org.wso2.ballerinalang.programfile.Instruction.RegIndex)

Aggregations

StructureRefCPEntry (org.wso2.ballerinalang.programfile.cpentries.StructureRefCPEntry)5 BLangEndpoint (org.wso2.ballerinalang.compiler.tree.BLangEndpoint)3 Operand (org.wso2.ballerinalang.programfile.Instruction.Operand)3 RegIndex (org.wso2.ballerinalang.programfile.Instruction.RegIndex)2 ArrayList (java.util.ArrayList)1 BPackageSymbol (org.wso2.ballerinalang.compiler.semantics.model.symbols.BPackageSymbol)1 BStructSymbol (org.wso2.ballerinalang.compiler.semantics.model.symbols.BStructSymbol)1 BSymbol (org.wso2.ballerinalang.compiler.semantics.model.symbols.BSymbol)1 BTypeSymbol (org.wso2.ballerinalang.compiler.semantics.model.symbols.BTypeSymbol)1 BLangRecordKey (org.wso2.ballerinalang.compiler.tree.expressions.BLangRecordLiteral.BLangRecordKey)1 BLangRecordKeyValue (org.wso2.ballerinalang.compiler.tree.expressions.BLangRecordLiteral.BLangRecordKeyValue)1 BLangCatch (org.wso2.ballerinalang.compiler.tree.statements.BLangCatch)1 ErrorTableEntry (org.wso2.ballerinalang.programfile.ErrorTableEntry)1 Instruction (org.wso2.ballerinalang.programfile.Instruction)1 StructInfo (org.wso2.ballerinalang.programfile.StructInfo)1 ErrorTableAttributeInfo (org.wso2.ballerinalang.programfile.attributes.ErrorTableAttributeInfo)1 ActionRefCPEntry (org.wso2.ballerinalang.programfile.cpentries.ActionRefCPEntry)1 ConstantPoolEntry (org.wso2.ballerinalang.programfile.cpentries.ConstantPoolEntry)1 FloatCPEntry (org.wso2.ballerinalang.programfile.cpentries.FloatCPEntry)1 ForkJoinCPEntry (org.wso2.ballerinalang.programfile.cpentries.ForkJoinCPEntry)1