Package org.openquark.cal.compiler

Examples of org.openquark.cal.compiler.Expression$Let$LetDefn


        //for textual field names
        //new RTRecordSelection.Textual(codeForRecordExpr, textualFieldName);
        //for ordinal field names
        //new RTRecordSelection.Ordinal(codeForRecordExpr, ordinal);

        Expression recordExpr = recordSelectionExpr.getRecordExpr();

        // If we can ignore laziness for the record expression then we can generate code
        // that directly extracts the field value, but doesn't force the field value to
        // WHNF.
        if (canIgnoreLaziness(recordExpr, variableContext)) {
View Full Code Here


        // Increment the nested case level.  This is used to disambiguate the name of the variable created
        // to hold the record value of the case expression.
        nestedCaseLevel++;

        Expression conditionExpr = recordCaseExpr.getConditionExpr();

        ExpressionContextPair conditionExprContextPair = genS_E(conditionExpr, variableContext);

        Block recordCaseBlock = new Block();

        JavaExpression javaConditionExpr = conditionExprContextPair.getJavaExpression();
        recordCaseBlock.addStatement(conditionExprContextPair.getContextBlock());

        //the compiler ensures that evaluating conditionExpr will result in a RTRecordValue.
        javaConditionExpr = new CastExpression(JavaTypeNames.RTRECORD_VALUE, javaConditionExpr);

        LocalVariable conditionVar = new LocalVariable("$recordCase" + nestedCaseLevel, JavaTypeNames.RTRECORD_VALUE);

        LocalVariableDeclaration conditionVarDeclaration = new LocalVariableDeclaration(conditionVar, javaConditionExpr);
        recordCaseBlock.addStatement(conditionVarDeclaration);

        //now encode the extraction of the pattern bound variables from the condition record expr.

        // Also need to push a let variable block.  This is separate from the variable scope because the two
        // do not always coincide.  The let variable block is popped by calling i_VariableScope.genS_Vars().
        variableContext.pushJavaScope();

        //FieldName -> String
        SortedMap<FieldName, String> fieldBindingVarMap = recordCaseExpr.getFieldBindingVarMap();

        String baseRecordPatternVarName = recordCaseExpr.getBaseRecordPatternVarName();
        if (baseRecordPatternVarName != null &&
            !baseRecordPatternVarName.equals(Expression.RecordCase.WILDCARD_VAR)) {

            QualifiedName qn = QualifiedName.make(currentModuleName, baseRecordPatternVarName);
            VarInfo.RecordField varInfo = variableContext.addRecordField(qn, null);
            String javaBaseRecordPatternVarName = varInfo.getJavaName();
            LocalName lazyRef = new LocalName(varInfo.getJavaName(), JavaTypeNames.RTVALUE);
            varInfo.updateLazyReference(lazyRef);
            varInfo.updateStrictReference(SCJavaDefn.createInvocation(lazyRef, SCJavaDefn.EVALUATE, SCJavaDefn.EXECUTION_CONTEXT_VAR));

            //generate the Java code:
            //(in the case of both ordinal and textual field names
            //javaBaseRecordPatternVarName = conditionVar.makeMixedRecordRetraction(new int[] {ordinalFieldName1, ..., ordinalFieldNameN},
            //  new String[] {textualFieldName1, ..., textualFieldNameN}

            LocalVariable baseRecordPatternVar = new LocalVariable(javaBaseRecordPatternVarName, JavaTypeNames.RTRECORD_VALUE);

            JavaExpression javaExtractBaseRecordExpr;

            Expression.RecordCase.FieldData fieldData = recordCaseExpr.getBindingFieldsData();
            //todoBI there could be some more optimizations here to handle the cases
            //a. where the ordinal names are in tuple form, then only the tuple size needs to be passed.
            //b. where only a single ordinal or single textual field is being retracted, then we don't
            //   need to form the array.
            //Note however, that if the record pattern var is not used in the expression on the right hand side
            //of the ->, then it will not be created (earlier analysis replaces it by a _), so in fact this
            //code is not called that often anyways.
            int[] retractedOrdinalFields = fieldData.getOrdinalNames();
            String[] retractedTextualFields = fieldData.getTextualNames();

            if (retractedOrdinalFields.length > 0) {

                if (retractedTextualFields.length > 0) {

                    if (fieldData.hasTupleOrdinalPart()) {

                        javaExtractBaseRecordExpr = new MethodInvocation.Instance(
                                conditionVar,
                            "makeTupleMixedRecordRetraction",
                            new JavaExpression[] {
                                LiteralWrapper.make(Integer.valueOf(retractedOrdinalFields.length)),
                                createTextualNamesArray(retractedTextualFields)},
                            new JavaTypeName[] {JavaTypeName.INT, JavaTypeName.STRING_ARRAY},
                            JavaTypeNames.RTRECORD_VALUE,
                            InvocationType.VIRTUAL);
                    } else {

                        javaExtractBaseRecordExpr = new MethodInvocation.Instance(
                                conditionVar,
                            "makeMixedRecordRetraction",
                            new JavaExpression[] {
                                createOrdinalNamesArray(retractedOrdinalFields),
                                createTextualNamesArray(retractedTextualFields)},
                            new JavaTypeName[] {JavaTypeName.INT_ARRAY, JavaTypeName.STRING_ARRAY},
                            JavaTypeNames.RTRECORD_VALUE,
                            InvocationType.VIRTUAL);
                    }
                } else {

                    if (fieldData.hasTupleOrdinalPart()) {

                        javaExtractBaseRecordExpr = new MethodInvocation.Instance(
                                conditionVar,
                            "makeTupleRecordRetraction",
                            new JavaExpression[] {
                                LiteralWrapper.make(Integer.valueOf(retractedOrdinalFields.length))},
                            new JavaTypeName[] {JavaTypeName.INT},
                            JavaTypeNames.RTRECORD_VALUE,
                            InvocationType.VIRTUAL);
                    } else {

                        javaExtractBaseRecordExpr = new MethodInvocation.Instance(
                                conditionVar,
                            "makeOrdinalRecordRetraction",
                            new JavaExpression[] {
                                createOrdinalNamesArray(retractedOrdinalFields)},
                            new JavaTypeName[] {JavaTypeName.INT_ARRAY},
                            JavaTypeNames.RTRECORD_VALUE,
                            InvocationType.VIRTUAL);
                    }
                }

            } else if (retractedTextualFields.length > 0) {

                    javaExtractBaseRecordExpr = new MethodInvocation.Instance(
                            conditionVar,
                        "makeTextualRecordRetraction",
                        new JavaExpression[] {
                            createTextualNamesArray(retractedTextualFields)},
                        new JavaTypeName[] {JavaTypeName.STRING_ARRAY},
                        JavaTypeNames.RTRECORD_VALUE,
                        InvocationType.VIRTUAL);

            } else {
                javaExtractBaseRecordExpr = conditionVar;
            }

            LocalVariableDeclaration extractBaseRecordDeclaration =
                new LocalVariableDeclaration(baseRecordPatternVar, javaExtractBaseRecordExpr);
            recordCaseBlock.addStatement(extractBaseRecordDeclaration);
        }

        for (final Map.Entry<FieldName, String> entry : fieldBindingVarMap.entrySet()) {

            FieldName fieldName = entry.getKey();
            String bindingVarName = entry.getValue();

            //ignore anonymous pattern variables. These are guaranteed not to be used
            //by the result expression, and so don't need to be extracted from the condition record.
            if (!bindingVarName.equals(Expression.RecordCase.WILDCARD_VAR)) {

                QualifiedName qn = QualifiedName.make(currentModuleName, bindingVarName);
                VarInfo.RecordField varInfo = variableContext.addRecordField(qn, null);
                String javaBindingVarName = varInfo.getJavaName();
                LocalName lazyRef = new LocalName(varInfo.getJavaName(), JavaTypeNames.RTVALUE);
                varInfo.updateLazyReference(lazyRef);
                varInfo.updateStrictReference(SCJavaDefn.createInvocation(lazyRef, SCJavaDefn.EVALUATE, SCJavaDefn.EXECUTION_CONTEXT_VAR));

                LocalVariable bindingVar = new LocalVariable(javaBindingVarName, JavaTypeNames.RTVALUE);

                JavaExpression javaExtractValueExpr;
                if (fieldName instanceof FieldName.Textual) {
                    //javaBindingVarName = $recordCase.getTextualFieldValue(fieldName);
                    javaExtractValueExpr = new MethodInvocation.Instance(conditionVar, "getTextualFieldValue",
                        LiteralWrapper.make(fieldName.getCalSourceForm()), JavaTypeName.STRING,
                        JavaTypeNames.RTVALUE, InvocationType.VIRTUAL);
                } else {
                    int ordinal = ((FieldName.Ordinal)fieldName).getOrdinal();
                    javaExtractValueExpr = new MethodInvocation.Instance(conditionVar, "getOrdinalFieldValue",
                        LiteralWrapper.make(Integer.valueOf(ordinal)), JavaTypeName.INT,
                        JavaTypeNames.RTVALUE, InvocationType.VIRTUAL);
                }

                LocalVariableDeclaration extractValueDeclaration = new LocalVariableDeclaration(bindingVar, javaExtractValueExpr);
                recordCaseBlock.addStatement(extractValueDeclaration);
            }
        }


        //encode the result expression in the context of the extended variable scope.
        Expression resultExpr = recordCaseExpr.getResultExpr();
        JavaStatement resultJavaStatement = genS_R(resultExpr, variableContext);

        // Generate any let variables in this block and add them to the recordCaseBlock.
        recordCaseBlock.addStatement(variableContext.popJavaScope());
View Full Code Here

        // Determine if for the letvar definition can ignore laziness.
        boolean canIgnoreLaziness = canIgnoreLaziness(let.getDefn().getExpr(), variableContext);
        if (!canIgnoreLaziness) {
            // Check to see if the def is a call to a lifted var definition
            Expression defExpr = let.getDefn().getExpr();
            Expression.Var defVar = defExpr.asVar();
            if (defVar == null && defExpr.asAppl() != null) {
                Expression[] chain = appChain(defExpr.asAppl());
                defVar = chain[0].asVar();
            }
            if (defVar != null) {
                MachineFunction mf = module.getFunction(defVar.getName());
                if (mf != null && mf instanceof LECCLiftedLetVarMachineFunction) {
View Full Code Here

        return false;
    }

    private ExpressionContextPair generateNot (Expression e, boolean boxResult, VariableContext variableContext) throws CodeGenerationException {
        // Get the argument.
        Expression arg = e.asAppl().getE2();
        ExpressionContextPair ecp = generateUnboxedArgument(JavaTypeName.BOOLEAN, arg, variableContext);
        JavaExpression not = new JavaExpression.OperatorExpression.Unary(JavaOperator.LOGICAL_NEGATE, ecp.getJavaExpression());

        if (boxResult) {
            not = createMakeKernelBooleanInvocation(not);
View Full Code Here

            if (!switchAlt.isDefaultAlt() &&
                !(switchAlt.getFirstAltTag() instanceof DataConstructor)) {
                isa = false;
                break;
            }
            Expression altExpr = switchAlt.getAltExpr();
            if (altExpr.asLiteral() != null) {
                if (!(altExpr.asLiteral().getLiteral() instanceof Boolean)) {
                    isa = false;
                    break;
                }
            } else if (altExpr.asVar() != null) {
                DataConstructor dcv = altExpr.asVar().getDataConstructor();
                if (dcv == null || !isTrueOrFalseDataCons(dcv)) {
                    isa = false;
                    break;
                }
            } else {
                isa = false;
                break;
            }
        }

        // We either need to have a default alt or an alt for every data
        // constructor for the type.
        if (isa && (eswitch.hasDefaultAlt() || nDataConstructorsForType == alts.length)) {
            return generateIsAFunctionFromSwitch (eswitch, variableContext);
        }

        // Determining if any of the alternates have alt vars that need to be extracted from the
        // switch value.
        boolean noAltVars = true;
        for (int i = 0; i < alts.length; ++i) {
            if (alts[i].hasVars()) {
                noAltVars = false;
                break;
            }
        }

        if (LECCMachineConfiguration.OPTIMIZE_SINGLE_DC_CASES && nDataConstructorsForType == 1) {
            // If there is only one DataConstructor we can eliminate the switch.
            if (codeGenerationStats != null) {
                codeGenerationStats.incrementSingleDCCases();
            }
            if (codeGenerationStats != null) {
                codeGenerationStats.incrementSingleDCCases();
            }

            return generateSingleAltSwitch(eswitch, variableContext);
        }

        // Create a boolean array to determine which cases we have.
        boolean[] caseExistsArray = new boolean[nDataConstructorsForType]// false by default.
        for (int i = 0; i < alts.length; ++i) {
            if (!alts[i].isDefaultAlt()) {
                List<Object> tags = alts[i].getAltTags();
                for (final Object tag : tags) {
                    DataConstructor dc = (DataConstructor)tag;
                    caseExistsArray[dc.getOrdinal()] = true;
                }
            }
        }

        // Generate the switch conditional.
        LocalVariable caseVar = null;
        SwitchStatement switchStatement;
        if (noAltVars /*&& (defaultAltProvided || !missingCases)*/) {
            // If there are no alt vars and we don't have to fill in any missing cases we don't need a local
            // variable holding the switchexpression.  This means we can generate something like:
            // switch (expression.evaluate().getOrdinal())
            ExpressionContextPair ecp = generateUnboxedArgument(JavaTypeName.INT, eswitch.getSwitchExpr(), variableContext);
            switchBlock.addStatement(ecp.getContextBlock());
            JavaExpression conditionExpression = ecp.getJavaExpression();

            switchStatement = new SwitchStatement(conditionExpression);
            switchBlock.addStatement(switchStatement);

        } else {
            // If there are alternates that have alt vars we generate something like:
            // RTValue caseVar;
            // switch ((caseVar = expression.evaluate()).getIntValue())
            // We do the assignment of the local in the actual switch statement
            // because analysis of the generated bytecode has shown this to be
            // slightly more efficient than initializing the local as part of the
            // declaration.
            JavaStatement caseVarDeclaration = null;
            Expression switchExpression = eswitch.getSwitchExpr();

            // Generate a local variable and assign the evaluated value of the expression
            // we are switching on.
            JavaTypeName typeClassName = isEnumDataType ? JavaTypeNames.RTVALUE : CALToJavaNames.createTypeNameFromType(typeCons, module);
            caseVar = new LocalVariable("$case" + nestedCaseLevel, typeClassName);
View Full Code Here

        boolean lookingForTrue = true;
        int defaultIndex = -1;
        int trueCount = 0;
        // Need to determine if there is a default case and how many true and false cases.
        for (int i = 0; i < alts.length; ++i) {
            Expression altExpr = alts[i].getAltExpr();
            Expression.Literal lit = altExpr.asLiteral();
            Expression.Var var = altExpr.asVar();
            boolean isTrue = false;
            if (lit != null && lit.getLiteral() instanceof Boolean && ((Boolean)lit.getLiteral()).booleanValue()) {
                isTrue = true;
                trueCount++;
            } else
            if (var != null && var.getDataConstructor() != null && isTrueDataCons(var.getDataConstructor())) {
                isTrue = true;
                trueCount++;
            }

            if(alts[i].isDefaultAlt()) {
                defaultIndex = i;
                // If the default case returns true we want to build up our expression based
                // on the cases that return false.
                if (isTrue) {
                    lookingForTrue = false;
                }
            }
        }

        if (trueCount == alts.length) {
            isABlock.addStatement(
                    generateReturn(
                            boxPrimitiveOpResult(
                                    PrimOps.PRIMOP_OR,
                                    JavaExpression.LiteralWrapper.make(Boolean.TRUE)), variableContext));
            return isABlock;
        }

        if (trueCount == 0) {
            isABlock.addStatement(generateReturn(boxPrimitiveOpResult(PrimOps.PRIMOP_OR, JavaExpression.LiteralWrapper.make(Boolean.FALSE)), variableContext));
            return isABlock;
        }

        int falseCount = alts.length - trueCount;

        // Generate the switch conditional.
        ExpressionContextPair pair = genS_E (eswitch.getSwitchExpr(), variableContext);

        isABlock.addStatement(pair.getContextBlock());

        JavaExpression isAExpr = null;
        JavaExpression tagValue = SCJavaDefn.createInvocation (pair.getJavaExpression(), SCJavaDefn.GETORDINALVALUE);
        if ((lookingForTrue && trueCount > 1) || (!lookingForTrue && falseCount > 1)) {
            LocalVariable intVal = new LocalVariable ("$intVal", JavaTypeName.INT);
            LocalVariableDeclaration intValDeclaration = new LocalVariableDeclaration(intVal, tagValue);
            isABlock.addStatement (intValDeclaration);
            tagValue = intVal;
        }

        for (int i = 0; i < alts.length; ++i) {
            // We arrange things so that we don't have to explicitly handle the default case.
            if (i == defaultIndex) {
                continue;
            }

            Expression altExpr = alts[i].getAltExpr();
            Expression.Literal lit = altExpr.asLiteral();
            Expression.Var var = altExpr.asVar();

            boolean isTrue = false;
            if (lit != null && lit.getLiteral() instanceof Boolean && ((Boolean)lit.getLiteral()).booleanValue()) {
                isTrue = true;
            } else
View Full Code Here

    private JavaStatement generateIfThenElseFromSwitch (Expression.Switch eSwitch, VariableContext variableContext) throws CodeGenerationException {

        // Extract the alternatives
        Expression.Switch.SwitchAlt[] alts = eSwitch.getAlts();

        Expression trueAlt = null;
        Expression falseAlt = null;
        Expression defaultAlt = null;

        for (int i = 0; i < alts.length; ++i) {
            SwitchAlt switchAlt = alts[i];
            if (switchAlt.isDefaultAlt()) {
                defaultAlt = switchAlt.getAltExpr();

            } else {
                if (switchAlt.getAltTags().size() > 1) {
                    // (True | False) - ie. always.
                    return genS_R(switchAlt.getAltExpr(), variableContext);
                }

                Object altTag = switchAlt.getFirstAltTag();

                if (altTag instanceof DataConstructor) {
                    // This is either Prelude.True or Prelude.False
                    DataConstructor dc = (DataConstructor)altTag;
                    if (dc.getName().equals(CAL_Prelude.DataConstructors.True)) {
                        altTag = Boolean.TRUE;
                    } else
                    if (dc.getName().equals(CAL_Prelude.DataConstructors.False)) {
                        altTag = Boolean.FALSE;
                    } else {
                        // We should never get here.
                        throw new CodeGenerationException ("Trying to generate if-then-else from data constructor: " + dc.getName().getQualifiedName());
                    }
                }

                // Tag is a boolean.
                if (((Boolean)altTag).booleanValue()) {
                    trueAlt = switchAlt.getAltExpr();
                } else {
                    falseAlt = switchAlt.getAltExpr();
                }
            }
        }

        if (trueAlt == null) {
            trueAlt = defaultAlt;
        }
        if (falseAlt == null) {
            falseAlt = defaultAlt;
        }

        Expression condExpression = eSwitch.getSwitchExpr();
        ExpressionContextPair pair = generateUnboxedArgument(JavaTypeName.BOOLEAN, condExpression, variableContext);

        // Then
        Block thenBlock;
        if (trueAlt == null) {
View Full Code Here

     * @return Expression[]
     */
    private Expression[] appChain (Expression.Appl root) {

        // Walk down the left branch.
        Expression c = root;
        int nArgs = 0;
        while (c instanceof Expression.Appl) {
            nArgs++;
            c = ((Expression.Appl)c).getE1();
        }
View Full Code Here

     */
    private JavaStatement buildTopLevelSeq (Expression.Appl applChain,
                                                      boolean isNested,
                                                      VariableContext variableContext) throws CodeGenerationException {

        Expression expressions[] = flattenTopLevelSeq(applChain);
        if (expressions == null) {
            return null;
        }

        Block block = new Block();

        // For all but the last expression we want to generate a Java statement
        // which will evaluate to WHNF.
        for (int i = 0, n = expressions.length - 1; i < n; ++i) {
            Expression e = expressions[i];
            JavaStatement js = makeStrictStatementFromSeqArg(e, variableContext);
            block.addStatement(js);
        }

        // For the last expression we simply generate a return expression using the
View Full Code Here

    private ExpressionContextPair buildOversaturatedApplicationChain (Expression.Appl e,
                                                                      Expression[] appChain,
                                                                      int calledArity,
                                                                      Scheme scheme,
                                                                      VariableContext variableContext) throws CodeGenerationException {
        Expression fsRoot = e;
        int diff = appChain.length - 1 - calledArity;
        for (int i = 0; i < diff; ++i) {
            fsRoot = fsRoot.asAppl().getE1();
        }

        ExpressionContextPair fsRootECP = null;
        if (scheme == Scheme.E_SCHEME || scheme == Scheme.R_SCHEME) {
            fsRootECP = genS_E(fsRoot, variableContext);
View Full Code Here

TOP

Related Classes of org.openquark.cal.compiler.Expression$Let$LetDefn

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.