Search in sources :

Example 31 with RegIndex

use of org.wso2.ballerinalang.programfile.Instruction.RegIndex in project ballerina by ballerina-lang.

the class CodeGenerator method visit.

@Override
public void visit(BLangBracedOrTupleExpr bracedOrTupleExpr) {
    // Emit create array instruction
    RegIndex exprRegIndex = calcAndGetExprRegIndex(bracedOrTupleExpr);
    Operand typeCPIndex = getTypeCPIndex(bracedOrTupleExpr.type);
    emit(InstructionCodes.RNEWARRAY, exprRegIndex, typeCPIndex);
    // Emit instructions populate initial array values;
    for (int i = 0; i < bracedOrTupleExpr.expressions.size(); i++) {
        BLangExpression argExpr = bracedOrTupleExpr.expressions.get(i);
        genNode(argExpr, this.env);
        BLangLiteral indexLiteral = new BLangLiteral();
        indexLiteral.pos = argExpr.pos;
        indexLiteral.value = (long) i;
        indexLiteral.type = symTable.intType;
        genNode(indexLiteral, this.env);
        emit(InstructionCodes.RASTORE, exprRegIndex, indexLiteral.regIndex, argExpr.regIndex);
    }
}
Also used : BLangLiteral(org.wso2.ballerinalang.compiler.tree.expressions.BLangLiteral) Operand(org.wso2.ballerinalang.programfile.Instruction.Operand) BLangExpression(org.wso2.ballerinalang.compiler.tree.expressions.BLangExpression) BLangEndpoint(org.wso2.ballerinalang.compiler.tree.BLangEndpoint) RegIndex(org.wso2.ballerinalang.programfile.Instruction.RegIndex)

Example 32 with RegIndex

use of org.wso2.ballerinalang.programfile.Instruction.RegIndex in project ballerina by ballerina-lang.

the class CodeGenerator method visit.

public void visit(BLangWorkerReceive workerReceiveStmt) {
    WorkerDataChannelInfo workerDataChannelInfo = this.getWorkerDataChannelInfo(this.currentCallableUnitInfo, workerReceiveStmt.workerIdentifier.value, this.currentWorkerInfo.getWorkerName());
    WorkerDataChannelRefCPEntry wrkrChnlRefCPEntry = new WorkerDataChannelRefCPEntry(workerDataChannelInfo.getUniqueNameCPIndex(), workerDataChannelInfo.getUniqueName());
    wrkrChnlRefCPEntry.setWorkerDataChannelInfo(workerDataChannelInfo);
    Operand wrkrRplyRefCPIndex = getOperand(currentPkgInfo.addCPEntry(wrkrChnlRefCPEntry));
    workerDataChannelInfo.setDataChannelRefIndex(wrkrRplyRefCPIndex.value);
    List<BLangExpression> lhsExprs = workerReceiveStmt.exprs;
    int nLHSExprs = lhsExprs.size();
    RegIndex[] regIndexes = new RegIndex[nLHSExprs];
    BType[] bTypes = new BType[nLHSExprs];
    for (int i = 0; i < nLHSExprs; i++) {
        BLangExpression lExpr = lhsExprs.get(i);
        if (lExpr.getKind() == NodeKind.SIMPLE_VARIABLE_REF && lExpr instanceof BLangLocalVarRef) {
            lExpr.regIndex = ((BLangLocalVarRef) lExpr).symbol.varIndex;
            regIndexes[i] = lExpr.regIndex;
        } else {
            lExpr.regIndex = getRegIndex(lExpr.type.tag);
            lExpr.regIndex.isLHSIndex = true;
            regIndexes[i] = lExpr.regIndex;
        }
        bTypes[i] = lExpr.type;
    }
    UTF8CPEntry sigCPEntry = new UTF8CPEntry(this.generateSig(bTypes));
    Operand sigCPIndex = getOperand(currentPkgInfo.addCPEntry(sigCPEntry));
    // WRKRECEIVE wrkrRplyRefCPIndex typesCPIndex nRegIndexes, regIndexes[nRegIndexes]
    Operand[] wrkReceiveArgRegs = new Operand[nLHSExprs + 3];
    wrkReceiveArgRegs[0] = wrkrRplyRefCPIndex;
    wrkReceiveArgRegs[1] = sigCPIndex;
    wrkReceiveArgRegs[2] = getOperand(nLHSExprs);
    System.arraycopy(regIndexes, 0, wrkReceiveArgRegs, 3, regIndexes.length);
    emit(InstructionCodes.WRKRECEIVE, wrkReceiveArgRegs);
    for (BLangExpression lExpr : lhsExprs) {
        if (lExpr.getKind() == NodeKind.SIMPLE_VARIABLE_REF && lExpr instanceof BLangLocalVarRef) {
            continue;
        }
        this.varAssignment = true;
        this.genNode(lExpr, this.env);
        this.varAssignment = false;
    }
}
Also used : BLangLocalVarRef(org.wso2.ballerinalang.compiler.tree.expressions.BLangSimpleVarRef.BLangLocalVarRef) Operand(org.wso2.ballerinalang.programfile.Instruction.Operand) WorkerDataChannelRefCPEntry(org.wso2.ballerinalang.programfile.cpentries.WorkerDataChannelRefCPEntry) BLangEndpoint(org.wso2.ballerinalang.compiler.tree.BLangEndpoint) RegIndex(org.wso2.ballerinalang.programfile.Instruction.RegIndex) UTF8CPEntry(org.wso2.ballerinalang.programfile.cpentries.UTF8CPEntry) BType(org.wso2.ballerinalang.compiler.semantics.model.types.BType) WorkerDataChannelInfo(org.wso2.ballerinalang.programfile.WorkerDataChannelInfo) BLangExpression(org.wso2.ballerinalang.compiler.tree.expressions.BLangExpression)

Example 33 with RegIndex

use of org.wso2.ballerinalang.programfile.Instruction.RegIndex in project ballerina by ballerina-lang.

the class CodeGenerator method visit.

@Override
public void visit(BLangMapAccessExpr mapKeyAccessExpr) {
    boolean variableStore = this.varAssignment;
    this.varAssignment = false;
    genNode(mapKeyAccessExpr.expr, this.env);
    Operand varRefRegIndex = mapKeyAccessExpr.expr.regIndex;
    genNode(mapKeyAccessExpr.indexExpr, this.env);
    Operand keyRegIndex = mapKeyAccessExpr.indexExpr.regIndex;
    BMapType mapType = (BMapType) mapKeyAccessExpr.expr.type;
    if (variableStore) {
        int opcode = getValueToRefTypeCastOpcode(mapType.constraint.tag);
        if (opcode == InstructionCodes.NOP) {
            emit(InstructionCodes.MAPSTORE, varRefRegIndex, keyRegIndex, mapKeyAccessExpr.regIndex);
        } else {
            RegIndex refRegMapValue = getRegIndex(TypeTags.ANY);
            emit(opcode, mapKeyAccessExpr.regIndex, refRegMapValue);
            emit(InstructionCodes.MAPSTORE, varRefRegIndex, keyRegIndex, refRegMapValue);
        }
    } else {
        int opcode = getRefToValueTypeCastOpcode(mapType.constraint.tag);
        if (opcode == InstructionCodes.NOP) {
            emit(InstructionCodes.MAPLOAD, varRefRegIndex, keyRegIndex, calcAndGetExprRegIndex(mapKeyAccessExpr));
        } else {
            RegIndex refRegMapValue = getRegIndex(TypeTags.ANY);
            emit(InstructionCodes.MAPLOAD, varRefRegIndex, keyRegIndex, refRegMapValue);
            emit(opcode, refRegMapValue, calcAndGetExprRegIndex(mapKeyAccessExpr));
        }
    }
    this.varAssignment = variableStore;
}
Also used : BMapType(org.wso2.ballerinalang.compiler.semantics.model.types.BMapType) Operand(org.wso2.ballerinalang.programfile.Instruction.Operand) BLangEndpoint(org.wso2.ballerinalang.compiler.tree.BLangEndpoint) RegIndex(org.wso2.ballerinalang.programfile.Instruction.RegIndex)

Example 34 with RegIndex

use of org.wso2.ballerinalang.programfile.Instruction.RegIndex in project ballerina by ballerina-lang.

the class CodeGenerator method visit.

@Override
public void visit(BLangXMLAttributeAccess xmlAttributeAccessExpr) {
    boolean variableStore = this.varAssignment;
    this.varAssignment = false;
    genNode(xmlAttributeAccessExpr.expr, this.env);
    RegIndex varRefRegIndex = xmlAttributeAccessExpr.expr.regIndex;
    if (xmlAttributeAccessExpr.indexExpr == null) {
        RegIndex xmlValueRegIndex = calcAndGetExprRegIndex(xmlAttributeAccessExpr);
        emit(InstructionCodes.XML2XMLATTRS, varRefRegIndex, xmlValueRegIndex);
        return;
    }
    BLangExpression indexExpr = xmlAttributeAccessExpr.indexExpr;
    genNode(xmlAttributeAccessExpr.indexExpr, this.env);
    RegIndex qnameRegIndex = xmlAttributeAccessExpr.indexExpr.regIndex;
    // If this is a string representation of qname
    if (indexExpr.getKind() != NodeKind.XML_QNAME) {
        RegIndex localNameRegIndex = getRegIndex(TypeTags.STRING);
        RegIndex uriRegIndex = getRegIndex(TypeTags.STRING);
        emit(InstructionCodes.S2QNAME, qnameRegIndex, localNameRegIndex, uriRegIndex);
        qnameRegIndex = getRegIndex(TypeTags.XML);
        generateURILookupInstructions(xmlAttributeAccessExpr.namespaces, localNameRegIndex, uriRegIndex, qnameRegIndex, indexExpr.pos, env);
    }
    if (variableStore) {
        emit(InstructionCodes.XMLATTRSTORE, varRefRegIndex, qnameRegIndex, xmlAttributeAccessExpr.regIndex);
    } else {
        RegIndex xmlValueRegIndex = calcAndGetExprRegIndex(xmlAttributeAccessExpr);
        emit(InstructionCodes.XMLATTRLOAD, varRefRegIndex, qnameRegIndex, xmlValueRegIndex);
    }
}
Also used : BLangExpression(org.wso2.ballerinalang.compiler.tree.expressions.BLangExpression) RegIndex(org.wso2.ballerinalang.programfile.Instruction.RegIndex)

Example 35 with RegIndex

use of org.wso2.ballerinalang.programfile.Instruction.RegIndex in project ballerina by ballerina-lang.

the class CodeGenerator method generateNamedArgs.

private int generateNamedArgs(BLangInvocation iExpr, Operand[] operands, int currentIndex) {
    if (iExpr.namedArgs.isEmpty()) {
        return currentIndex;
    }
    PackageInfo pkgInfo = programFile.packageInfoMap.get(iExpr.symbol.pkgID.bvmAlias());
    CallableUnitInfo callableUnitInfo;
    if (iExpr.symbol.kind == SymbolKind.FUNCTION) {
        callableUnitInfo = pkgInfo.functionInfoMap.get(iExpr.symbol.name.value);
    } else if (iExpr.symbol.kind == SymbolKind.ACTION) {
        ConnectorInfo connectorInfo = pkgInfo.connectorInfoMap.get(iExpr.symbol.owner.name.value);
        callableUnitInfo = connectorInfo.actionInfoMap.get(iExpr.symbol.name.value);
    } else {
        throw new IllegalStateException("Unsupported callable unit");
    }
    ParamDefaultValueAttributeInfo defaultValAttrInfo = (ParamDefaultValueAttributeInfo) callableUnitInfo.getAttributeInfo(AttributeInfo.Kind.PARAMETER_DEFAULTS_ATTRIBUTE);
    for (int i = 0; i < iExpr.namedArgs.size(); i++) {
        BLangExpression argExpr = iExpr.namedArgs.get(i);
        // at this point. If so, get the default value for that parameter from the function info.
        if (argExpr == null) {
            DefaultValue defaultVal = defaultValAttrInfo.getDefaultValueInfo()[i];
            argExpr = getDefaultValExpr(defaultVal);
        }
        operands[currentIndex++] = genNode(argExpr, this.env).regIndex;
    }
    return currentIndex;
}
Also used : DefaultValue(org.wso2.ballerinalang.programfile.DefaultValue) ParamDefaultValueAttributeInfo(org.wso2.ballerinalang.programfile.attributes.ParamDefaultValueAttributeInfo) ConnectorInfo(org.wso2.ballerinalang.programfile.ConnectorInfo) ImportPackageInfo(org.wso2.ballerinalang.programfile.ImportPackageInfo) PackageInfo(org.wso2.ballerinalang.programfile.PackageInfo) CallableUnitInfo(org.wso2.ballerinalang.programfile.CallableUnitInfo) BLangExpression(org.wso2.ballerinalang.compiler.tree.expressions.BLangExpression) BLangEndpoint(org.wso2.ballerinalang.compiler.tree.BLangEndpoint)

Aggregations

RegIndex (org.wso2.ballerinalang.programfile.Instruction.RegIndex)37 BLangEndpoint (org.wso2.ballerinalang.compiler.tree.BLangEndpoint)22 Operand (org.wso2.ballerinalang.programfile.Instruction.Operand)20 BLangExpression (org.wso2.ballerinalang.compiler.tree.expressions.BLangExpression)13 UTF8CPEntry (org.wso2.ballerinalang.programfile.cpentries.UTF8CPEntry)6 BLangXMLQuotedString (org.wso2.ballerinalang.compiler.tree.expressions.BLangXMLQuotedString)5 SymbolEnv (org.wso2.ballerinalang.compiler.semantics.model.SymbolEnv)4 BType (org.wso2.ballerinalang.compiler.semantics.model.types.BType)4 BXMLNSSymbol (org.wso2.ballerinalang.compiler.semantics.model.symbols.BXMLNSSymbol)3 BLangXMLQName (org.wso2.ballerinalang.compiler.tree.expressions.BLangXMLQName)3 Stack (java.util.Stack)2 Name (org.ballerinalang.model.Name)2 BInvokableSymbol (org.wso2.ballerinalang.compiler.semantics.model.symbols.BInvokableSymbol)2 BTypeSymbol (org.wso2.ballerinalang.compiler.semantics.model.symbols.BTypeSymbol)2 BMapType (org.wso2.ballerinalang.compiler.semantics.model.types.BMapType)2 BLangEnumerator (org.wso2.ballerinalang.compiler.tree.BLangEnum.BLangEnumerator)2 BLangIdentifier (org.wso2.ballerinalang.compiler.tree.BLangIdentifier)2 BLangLiteral (org.wso2.ballerinalang.compiler.tree.expressions.BLangLiteral)2 BLangRecordKeyValue (org.wso2.ballerinalang.compiler.tree.expressions.BLangRecordLiteral.BLangRecordKeyValue)2 EnumInfo (org.wso2.ballerinalang.programfile.EnumInfo)2