Package org.openquark.cal.internal.machine

Examples of org.openquark.cal.internal.machine.ConstructorOpTuple


            // Unpack the basic op into subexpressions
            if (CODEGEN_DIAG) {
                MACHINE_LOGGER.log(Level.FINE, "    basic:");
            }

            ConstructorOpTuple  constructorOpExpressions = ConstructorOpTuple.isConstructorOp(e, false);

            DataConstructor dc = constructorOpExpressions.getDataConstructor ();

            Instruction instruction = Instruction.I_PackCons.makePackCons(dc);

            int nArgs = constructorOpExpressions.getNArguments ();

            if (nArgs < 0) {
                throw new CodeGenerationException ("Internal Coding Error: Invalid constructor operator arity");  
            }

            for (int i = 0; i < nArgs; ++i) {
                gp.code (schemeC (constructorOpExpressions.getArgument(nArgs - i - 1), p, d + i));
            }

            // Force the evaluation of any strict arguments.
            if (dc.hasStrictArgs()) {
                for (int i = 0; i < dc.getArity(); ++i) {
View Full Code Here


            // Unpack the basic op into subexpressions
            if (CODEGEN_DIAG) {
                MACHINE_LOGGER.log(Level.FINE, "    basic:");
            }

            ConstructorOpTuple  constructorOpExpressions = ConstructorOpTuple.isConstructorOp(e, false);

            DataConstructor dc = constructorOpExpressions.getDataConstructor ();

            Instruction instruction = Instruction.I_PackCons.makePackCons(dc);

            int nArgs = constructorOpExpressions.getNArguments ();

            if (nArgs < 0) {
                throw new CodeGenerationException ("Internal Coding Error: Invalid constructor operator arity");  
            }

            for (int i = 0; i < nArgs; ++i) {
                gp.code (schemeC (constructorOpExpressions.getArgument(nArgs - i - 1), p, d + i));
            }

            // Force the evaluation of any strict arguments.
            if (dc.hasStrictArgs()) {
                for (int i = 0; i < dc.getArity(); ++i) {
View Full Code Here

            }
        }

        // Is e an application of a saturated constructor?
        ConstructorOpTuple constructorOpTuple = ConstructorOpTuple.isConstructorOp(e, false);
        if (constructorOpTuple != null) {
            DataConstructor dc = constructorOpTuple.getDataConstructor ();

            boolean[] fieldStrictness = new boolean [dc.getArity()];
            boolean dcHasStrictFields = false;
            for (int i = 0; i < dc.getArity(); ++i) {
                fieldStrictness[i] = dc.isArgStrict(i);
                if (fieldStrictness[i]) {
                    dcHasStrictFields = true;
                }
            }

            // If there are no strict arguments we can simply create an instance of the DC class.
            // The simplest way to do this is to treat this DC application as if it were in a strict context.
            if (!dcHasStrictFields) {
                return true;
            } else {
                // If all strict arguments are already evaluated, or we consider them safe to evaluate (i.e. cheap and
                // with no side effects) we can treat this as strict.
                boolean allOK = true;
                for (int i = 0; i < dc.getArity(); ++i) {
                    if (dc.getArgStrictness()[i] && !canIgnoreLaziness(constructorOpTuple.getArgument(i), env)) {
                        allOK = false;
                        break;
                    }
                }

                if (allOK) {
                    return true;
                }
            }       
        }

        // We can shortcut a basic op if it is marked as allowed to
        // be eagerly evaluated and all arguments can all be shortcut.
        // Also if the op is Prelude.eager we can shortcut.
        BasicOpTuple basicOpExpressions = BasicOpTuple.isBasicOp(e);
        if (basicOpExpressions != null) {
            if (basicOpExpressions.getPrimitiveOp() == PrimOps.PRIMOP_EAGER) {
                return true;
            }

            QualifiedName opName = basicOpExpressions.getName();
            MachineFunction mf = currentModule.getFunction(opName);
            if (mf == null) {
                return false;
            }

            if (mf.canFunctionBeEagerlyEvaluated()) {
                int nArgs = basicOpExpressions.getNArguments();
                int nWHNFArgs = 0;
                for (int i = 0; i < nArgs; ++i) {
                    Expression eArg = basicOpExpressions.getArgument(i);
                    if (canIgnoreLaziness(eArg, env)) {
                        nWHNFArgs++;
                    }
                }
                if (nArgs == nWHNFArgs) {
                    // All the args are in WHNF so ideally we can ignore laziness for
                    // this primitive operation.  However, there are some primitive
                    // ops where an additional condition, that the second argument is
                    // known to not be zero, is required.
                    String unqualifiedOpName = opName.getUnqualifiedName();
                    if (opName.getModuleName().equals(CAL_Prelude.MODULE_NAME) &&
                            (unqualifiedOpName.equals("divideLong") ||
                                    unqualifiedOpName.equals("remainderLong") ||
                                    unqualifiedOpName.equals("divideInt") ||
                                    unqualifiedOpName.equals("remainderInt") ||
                                    unqualifiedOpName.equals("divideShort") ||
                                    unqualifiedOpName.equals("remainderShort") ||
                                    unqualifiedOpName.equals("divideByte") ||
                                    unqualifiedOpName.equals("remainderByte"))) {

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

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

                            if (unqualifiedOpName.equals("divideLong") || unqualifiedOpName.equals("remainderLong")) {

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

                            } else if (unqualifiedOpName.equals("divideInt") || unqualifiedOpName.equals("remainderInt")) {

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

                            } else if (unqualifiedOpName.equals("divideShort") || unqualifiedOpName.equals("remainderShort")) {

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

                            } else if (unqualifiedOpName.equals("divideByte") || unqualifiedOpName.equals("remainderByte")) {

                                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, env)) {
                    nWHNFArgs++;
                }
            }
            if (nArgs == nWHNFArgs) {
                return true;
            }
        }

        // If e is a fully saturated application of a function tagged for optimization and
        // all the arguments are in WHNF or can have laziness ignored we can
        // ignore laziness for 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 = currentModule.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 ignore laziness for all the arguments.
                        if (chain.length - 1 == calledArity) {
                            int nWHNFArgs = 0;
                            for (int i = 0; i < calledArity; ++i) {
                                if (canIgnoreLaziness(chain[i+1], env)) {
                                    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;
            }
        }
View Full Code Here

            }
        }

        // 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;
            }
        }
View Full Code Here

                }
            }
        }

        // Is e an application of a saturated constructor?
        ConstructorOpTuple constructorOpTuple = ConstructorOpTuple.isConstructorOp(e, false);
        if (constructorOpTuple != null) {
            if (codeGenerationStats != null) {
                codeGenerationStats.incrementFullySaturatedDCInLazyContext(constructorOpTuple.getDataConstructor().getName());
            }

            return generateDataConstructor(constructorOpTuple, Scheme.C_SCHEME, variableContext);
        }
View Full Code Here

        if (BasicOpTuple.isAndOr(e) != null) {
            return generateAndOr(e, true, variableContext);
        }

        // Is e an application of a saturated constructor?
        ConstructorOpTuple constructorOpTuple = ConstructorOpTuple.isConstructorOp(e, false);
        if (constructorOpTuple != null) {
            if (codeGenerationStats != null) {
                codeGenerationStats.incrementFullySaturatedDCInStrictContext(constructorOpTuple.getDataConstructor().getName());
            }

            return generateDataConstructor(constructorOpTuple, Scheme.E_SCHEME, variableContext);
        }
View Full Code Here

            ExpressionContextPair ecPair = generateAndOr(e, true, variableContext);
            return generateReturn(ecPair, variableContext);
        }

        // Is e an application of a saturated constructor?
        ConstructorOpTuple constructorOpTuple = ConstructorOpTuple.isConstructorOp(e, false);
        if (constructorOpTuple != null) {
            if (codeGenerationStats != null) {
                codeGenerationStats.incrementFullySaturatedDCInTopLevelContext(constructorOpTuple.getDataConstructor().getName());
            }
            ExpressionContextPair ecPair = generateDataConstructor(constructorOpTuple, Scheme.R_SCHEME, variableContext);
            return generateReturn(ecPair, variableContext);
        }
View Full Code Here

            }

            lastConstructorArgs = constructorArgs;

            // The second argument will always be a data constructor, either cons or Nil.
            ConstructorOpTuple constructorOpExpressions2 = ConstructorOpTuple.isConstructorOp(constructorOpExpressions.getArgument(1), false);
            if (constructorOpExpressions2 != null && constructorOpExpressions2.getDataConstructor().getName().equals(CAL_Prelude.DataConstructors.Cons)) {
                constructorOpExpressions = constructorOpExpressions2;
                moreListElements = true;
            }
        } while (moreListElements);
View Full Code Here

                }
            }

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

                DataConstructor dc = constructorOpExpressions.getDataConstructor ();

                // If we are dealing with a data constructor for the CAL type boolean we want to optimize
                // by substituting the literal boolean values.
                if (isTrueOrFalseDataCons(dc)) {
                    verifyUnboxType(unboxType, JavaTypeName.BOOLEAN);
View Full Code Here

                }
            }

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

                DataConstructor dc = constructorOpExpressions.getDataConstructor ();

                // If we are dealing with a data constructor for the CAL type boolean we want to optimize
                // by substituting and instance of the literal RTKernel.CAL_Boolean.
                if (isTrueOrFalseDataCons(dc)) {
                    verifyUnboxType(unboxType, JavaTypeName.BOOLEAN);
View Full Code Here

TOP

Related Classes of org.openquark.cal.internal.machine.ConstructorOpTuple

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.