Package org.openquark.cal.compiler

Examples of org.openquark.cal.compiler.Expression


     * @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


            if (codeGenerationStats != null) {
                codeGenerationStats.incrementSCArity(nArguments);
            }

            Expression methodBody = body;

            // If the body of this 'function' is an application of Prelude.eager' we want
            // to strip it off and treat this as strict.
            Expression eagerStripped = stripEager(methodBody);
            if (eagerStripped != methodBody) {
                methodBody = eagerStripped;
                if (scheme == Scheme.C_SCHEME) {
                    scheme = Scheme.E_SCHEME;
                }
            }

            bodyCode = new Block(exceptionInfo);

            // Generate the root (SC) variable scope
            VariableContext variableContext = new VariableContext();

            // Add the arguments to the variable scope and deal with strictness, boxing, etc.
            for (int i = 0; i < nArguments; ++i) {
                // Add each of the arguments to the variable scope.
                VarInfo vi = variableContext.addArgument(QualifiedName.make(currentModuleName, argumentNames[i]), isArgStrict(i), getArgumentTypeName(i), getArgumentType(i));
                this.javaArgumentNames[i] = vi.getJavaName();
            }

            if (canIgnoreLaziness(body, variableContext)) {
                mf.setIgnoreLaziness(true);
            }


            Expression expressions[] = flattenTopLevelSeq(methodBody);
            if (expressions != null && (scheme == Scheme.E_SCHEME || scheme == Scheme.UNBOX_INTERNAL_SCHEME)) {
                for (int i = 0, n = expressions.length - 1; i < n; ++i) {
                    JavaStatement js = makeStrictStatementFromSeqArg(expressions[i], variableContext);
                    bodyCode.addStatement(js);
                }
View Full Code Here

            MachineFunction mf = module.getFunction(opName);
            if (mf != null && mf.canFunctionBeEagerlyEvaluated()) {
                int nArgs = basicOpExpressions.getNArguments();
                int nWHNFArgs = 0;
                for (int i = 0; i < nArgs; ++i) {
                    Expression eArg = basicOpExpressions.getArgument(i);
                    if (canIgnoreLaziness(eArg, variableContext)) {
                        nWHNFArgs++;
                    }
                }
                if (nArgs == nWHNFArgs) {

                    String unqualifiedOpName = opName.getUnqualifiedName();
                    if (opName.getModuleName().equals(CAL_Prelude.MODULE_NAME) &&
                        (unqualifiedOpName.equals(CAL_Prelude_internal.Functions.divideLong.getUnqualifiedName()) ||
                         unqualifiedOpName.equals(CAL_Prelude_internal.Functions.remainderLong.getUnqualifiedName()) ||
                         unqualifiedOpName.equals(CAL_Prelude_internal.Functions.divideInt.getUnqualifiedName()) ||
                         unqualifiedOpName.equals(CAL_Prelude_internal.Functions.remainderInt.getUnqualifiedName()) ||
                         unqualifiedOpName.equals(CAL_Prelude_internal.Functions.divideShort.getUnqualifiedName()) ||
                         unqualifiedOpName.equals(CAL_Prelude_internal.Functions.remainderShort.getUnqualifiedName()) ||
                         unqualifiedOpName.equals(CAL_Prelude_internal.Functions.divideByte.getUnqualifiedName()) ||
                         unqualifiedOpName.equals(CAL_Prelude_internal.Functions.remainderByte.getUnqualifiedName()))) {

                        // Check that the second argument is a non zero literal.

                        Expression arg = basicOpExpressions.getArgument(1);
                        if (arg.asLiteral() != null) {

                            if (unqualifiedOpName.equals(CAL_Prelude_internal.Functions.divideLong.getUnqualifiedName()) ||
                                unqualifiedOpName.equals(CAL_Prelude_internal.Functions.remainderLong.getUnqualifiedName())) {

                                Long l = (Long)arg.asLiteral().getLiteral();
                                return l.longValue() != 0;

                            } else if (unqualifiedOpName.equals(CAL_Prelude_internal.Functions.divideInt.getUnqualifiedName()) ||
                                unqualifiedOpName.equals(CAL_Prelude_internal.Functions.remainderInt.getUnqualifiedName())) {

                                Integer i = (Integer)arg.asLiteral().getLiteral();
                                return i.intValue() != 0;

                            } else if (unqualifiedOpName.equals(CAL_Prelude_internal.Functions.divideShort.getUnqualifiedName()) ||
                                unqualifiedOpName.equals(CAL_Prelude_internal.Functions.remainderShort.getUnqualifiedName())) {

                                Short shortValue = (Short)arg.asLiteral().getLiteral();
                                return shortValue.shortValue() != 0;

                            } else if (unqualifiedOpName.equals(CAL_Prelude_internal.Functions.divideByte.getUnqualifiedName()) ||
                                unqualifiedOpName.equals(CAL_Prelude_internal.Functions.remainderByte.getUnqualifiedName())) {

                                Byte byteValue = (Byte)arg.asLiteral().getLiteral();
                                return byteValue.byteValue() != 0;

                            } else {
                                throw new IllegalStateException();
                            }
                        } else {
                            return false;
                        }
                    } else {
                        return true;
                    }
                } else {
                    return false;
                }
            }
        }

        basicOpExpressions = BasicOpTuple.isAndOr (e);
        if (basicOpExpressions != null) {

            // Code a basic operation
            int nArgs = basicOpExpressions.getNArguments ();
            int nWHNFArgs = 0;
            for (int i = 0; i < nArgs; ++i) {
                Expression eArg = basicOpExpressions.getArgument(i);
                if (canIgnoreLaziness(eArg, variableContext)) {
                    nWHNFArgs++;
                }
            }
            if (nArgs == nWHNFArgs) {
                return true;
            }
        }

        // If e is a fully saturated application of a function tagged for optimization and
        // all the arguments can be shortcutted we can shortcut the application.
        if (e.asAppl() != null) {
            Expression[] chain = appChain(e.asAppl());
            if (chain[0].asVar() != null) {
                // Get the supercombinator on the left end of the chain.
                Expression.Var scVar = chain[0].asVar();
                if (scVar != null) {
                    // Check if this supercombinator is one we should try to optimize.
                    MachineFunction mf = module.getFunction(scVar.getName());
                    if (mf != null && mf.canFunctionBeEagerlyEvaluated()) {

                        // Now determine the arity of the SC.
                        int calledArity = mf.getArity();

                        // Check to see if we can shortcut all the arguments.
                        if (chain.length - 1 == calledArity) {
                            int nWHNFArgs = 0;
                            for (int i = 0; i < calledArity; ++i) {
                                if (canIgnoreLaziness(chain[i+1], variableContext)) {
                                    nWHNFArgs++;
                                }
                            }
                            if (nWHNFArgs == calledArity) {
                                return true;
                            }
                        }
                    }
                }
            }
        }

        // Is e an application of a zero arity constructor.
        if (ConstructorOpTuple.isConstructorOp(e, true) != null) {
            ConstructorOpTuple  constructorOpExpressions = ConstructorOpTuple.isConstructorOp(e, false);

            DataConstructor dc = constructorOpExpressions.getDataConstructor ();

            if (dc.getArity() == 0){
                return true;
            }
        }

        // Is e a DataConsFieldSelection where the data constructor expression can be shortcutted and
        // the field is strict.
        if (e.asDataConsSelection() != null) {
            Expression.DataConsSelection dcs = (Expression.DataConsSelection)e;
            if (dcs.getDataConstructor().isArgStrict(dcs.getFieldIndex()) && canIgnoreLaziness(dcs.getDCValueExpr(), variableContext)) {
                return true;
            }
        }

        // 'if a then b else c' where a, b, and c can all be shortcut.
        CondTuple conditionExpressions = CondTuple.isCondOp(e);
        if (conditionExpressions != null) {
            Expression condExpr = conditionExpressions.getConditionExpression();
            Expression thenExpr = conditionExpressions.getThenExpression();
            Expression elseExpr = conditionExpressions.getElseExpression();
            if (canIgnoreLaziness(condExpr, variableContext) &&
                canIgnoreLaziness(thenExpr, variableContext) &&
                canIgnoreLaziness(elseExpr, variableContext)) {
                return true;
            }
View Full Code Here

            }

            // This is a conditional op.  The conditionExpressions tuple holds (kCond, kThen, kElse) expressions
            // Generate the code for kThen and kElse, as arguments to a new I_Cond instruction

            Expression condExpression = conditionExpressions.getConditionExpression();
            ExpressionContextPair pair;
            pair = generateUnboxedArgument(JavaTypeName.BOOLEAN, condExpression, variableContext);

            Block contextBlock = pair.getContextBlock();
            JavaExpression condJavaExpression = pair.getJavaExpression();
View Full Code Here

            }

            // This is a conditional op.  The conditionExpressions tuple holds (kCond, kThen, kElse) expressions
            // Generate the code for kThen and kElse, as arguments to a new I_Cond instruction

            Expression condExpression = conditionExpressions.getConditionExpression();
            ExpressionContextPair pair = generateUnboxedArgument(JavaTypeName.BOOLEAN, condExpression, variableContext);

            // Then
            variableContext.pushJavaScope();
            JavaStatement thenPart = genS_R (conditionExpressions.getThenExpression(), variableContext);
View Full Code Here

     * @return ExpressionContextPair
     * @throws CodeGenerationException
     */
    private ExpressionContextPair generateLazyRecordUpdate(Expression.RecordUpdate recordUpdateExpr, VariableContext variableContext) throws CodeGenerationException {

        Expression baseRecordExpr = recordUpdateExpr.getBaseRecordExpr();
        ExpressionContextPair baseRecordExprContextPair = genS_C(baseRecordExpr, variableContext);

        JavaExpression javaBaseRecordExpr = baseRecordExprContextPair.getJavaExpression();
        //holds the variable declarations introduced in subexpressions of the record update.
        //for example, in the expression {e | field1 := e1, field2 := e2}, these could come from e, e1 or e2.
View Full Code Here

     * @return ExpressionContextPair
     * @throws CodeGenerationException
     */
    private ExpressionContextPair generateStrictRecordUpdate(Expression.RecordUpdate recordUpdateExpr, VariableContext variableContext) throws CodeGenerationException {

        Expression baseRecordExpr = recordUpdateExpr.getBaseRecordExpr();

        //holds the variable declarations introduced in subexpressions of the record update expression.
        //for example, in the expression {e | field1 := e1, field2 := e2}, these could come from e, e1 or e2.
        Block recordUpdateBlock = new Block();

        JavaExpression invocationTarget;
        {

            ExpressionContextPair baseRecordExprContextPair = genS_E(baseRecordExpr, variableContext);

            JavaExpression javaBaseRecordExpr = baseRecordExprContextPair.getJavaExpression();
            recordUpdateBlock.addStatement(baseRecordExprContextPair.getContextBlock());

            //the compiler ensures that evaluating baseRecordExpr will result in a RTRecordValue.
            invocationTarget = new CastExpression(JavaTypeNames.RTRECORD_VALUE, javaBaseRecordExpr);
        }

        //the expression {e | field1 := e1, field2 := e2, field3 := e3} is encoded as
        //{{{e | field1 := e1} | field2 := e2} | field3 := e3}

        // We want to copy the base record for the first update.  Subsequent updates can just
        // mutate the copy.
        // The copy is created by a call to:
        //    1) updateOrdinalField - if only ordinal fields are being updated
        //    2) updateTextualField - if only textual fields are being updated
        //    3) updateMixedOrdinalField - if both ordinal and textual fields are being updated
        FieldValueData fieldValueData = recordUpdateExpr.getUpdateFieldsData();

        SortedMap<FieldName, Expression> updateFieldValuesMap = recordUpdateExpr.getUpdateFieldValuesMap();
        int fieldN = 0;
        for (final Map.Entry<FieldName, Expression> entry : updateFieldValuesMap.entrySet()) {

            FieldName fieldName = entry.getKey();
            Expression updateExpr = entry.getValue();

            //the actual updated values are not strictly evaluated, so we use the C scheme.
            ExpressionContextPair updateExprContextPair = genS_C(updateExpr, variableContext);
            JavaExpression javaUpdateExpr = updateExprContextPair.getJavaExpression();
            recordUpdateBlock.addStatement(updateExprContextPair.getContextBlock());
View Full Code Here

     * @return ExpressionContextPair
     * @throws CodeGenerationException
     */
    private ExpressionContextPair generateStrictRecordExtension(Expression.RecordExtension recordExtensionExpr, VariableContext variableContext) throws CodeGenerationException {

        Expression baseRecordExpr = recordExtensionExpr.getBaseRecordExpr();

        //holds the variable declarations introduced in subexpressions of the record extension.
        //for example, in the expression {e | field1 = e1, field2 = e2}, these could come from e, e1 or e2.
        Block recordExtensionBlock = new Block();

View Full Code Here

     * @return Expression.RecordExtension
     * @throws CodeGenerationException
     */
    private ExpressionContextPair generateLazyRecordExtension(Expression.RecordExtension recordExtensionExpr, VariableContext variableContext) throws CodeGenerationException {

        Expression baseRecordExpr = recordExtensionExpr.getBaseRecordExpr();
        if (baseRecordExpr == null) {
            //this is a non record-polymorphic record extension so it is already in reduced form.
            return generateStrictRecordExtension(recordExtensionExpr, variableContext);
        }

View Full Code Here

        //for ordinal fields
        //((RTRecordValue) (codeForRecordExpr.evaluate($ec))).getOrdinalFieldValue(ordinal)
        //for textual fields
        //((RTRecordValue) (codeForRecordExpr.evaluate($ec))).getTextualFieldValue(textualFieldName)

        Expression recordExpr = recordSelectionExpr.getRecordExpr();
        FieldName fieldName = recordSelectionExpr.getFieldName();


        ExpressionContextPair recordExprContextPair = genS_E(recordExpr, variableContext);
View Full Code Here

TOP

Related Classes of org.openquark.cal.compiler.Expression

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.