Examples of MachineFunction


Examples of org.openquark.cal.machine.MachineFunction

            if (variableContext.isVarPreEvaluated(var.getName())) {
                return true;
            }
            if (!variableContext.isLocalVariable(var.getName())) {
                // This is an SC
                MachineFunction mf = module.getFunction(var.getName());
                if (mf != null && mf.getArity() > 0) {
                    return true;
                }

            }
        }

        // We can shortcut a basic op if it is marked as eager
        // 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 = 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) {
View Full Code Here

Examples of org.openquark.cal.machine.MachineFunction

                // this is either a data constructor or an SC.
                if (var.getDataConstructor() != null || (var.getForeignFunctionInfo() != null && SCJavaDefn.getNArguments(var.getForeignFunctionInfo()) > 0)) {
                    return new ExpressionContextPair(varExpression);
                }

                MachineFunction varFunction = module.getFunction(var.getName());
                if (varFunction != null && varFunction.getArity() > 0) {
                    // This is an non-CAF SC and can't be resolved any further.
                    return new ExpressionContextPair(varExpression);
                } else {
                    // We are in a strict context so we want to evaluate the variable
                    return new ExpressionContextPair(createInvocation(varExpression, EVALUATE, EXECUTION_CONTEXT_VAR));
View Full Code Here

Examples of org.openquark.cal.machine.MachineFunction

        if (eVar == null) {
            return null;
        }

        MachineFunction mf = module.getFunction(eVar.getName());
        if (mf == null ||
            !(mf instanceof LECCLiftedLetVarMachineFunction) ||
            mf.getArity() != nArgs) {
            return null;
        }

        if (scheme == Scheme.UNBOX_INTERNAL_SCHEME && !canTypeBeUnboxed(mf.getResultType())) {
            return null;
        }

        boolean calledArgStrictness[] = mf.getParameterStrictness();
        TypeExpr calledArgTypes[] = mf.getParameterTypes();

        // First generate the java expressions for the argument CAL expressions.
        ExpressionContextPair[] ecp = new ExpressionContextPair[chain.length-1];
        for (int i = 1; i < chain.length; ++i) {
            if (calledArgStrictness[i-1]) {
                if (SCJavaDefn.canTypeBeUnboxed(calledArgTypes[i-1])) {
                    ecp[i-1] = generateUnboxedArgument(typeExprToTypeName(calledArgTypes[i-1]), chain[i], variableContext);
                } else {
                    ecp[i-1] = genS_E(chain[i], variableContext);
                }
            } else {
                ecp[i-1] = genS_C(chain[i], variableContext);
            }
        }

        Block newContext;
        if (ecp.length > 0) {
            newContext = ecp[0].getContextBlock();
            for (int i = 1; i < ecp.length; ++i) {
                newContext.addStatement(ecp[i].getContextBlock());
            }
        } else {
            newContext = new Block();
        }

        // This is a fully saturated supercombinator application
        // for which we can generate a direct call instead of building a suspension.
        int calledArity = mf.getArity();
        JavaExpression args[] = new JavaExpression[ecp.length + 1];
        for (int i = 0; i < calledArity; ++i) {
            args[i] = ecp[i].getJavaExpression();
        }
        args[calledArity] = EXECUTION_CONTEXT_VAR;

        JavaTypeName[] argTypes = new JavaTypeName[args.length];
        for (int i = 0; i < calledArgTypes.length; ++i) {
            if (calledArgStrictness[i] && SCJavaDefn.canTypeBeUnboxed(calledArgTypes[i])) {
                argTypes[i] = SCJavaDefn.typeExprToTypeName(calledArgTypes[i]);
            } else {
                argTypes[i] = JavaTypeNames.RTVALUE;
            }
        }
        argTypes[calledArity] = JavaTypeNames.RTEXECUTION_CONTEXT;

        JavaTypeName returnType = JavaTypeNames.RTVALUE;

        // Get the method name to call.
        String functionName = CALToJavaNames.makeLetVarDefFunctionJavaName(mf.getQualifiedName(), module);
        if (scheme == Scheme.E_SCHEME) {
            functionName = functionName + "_Strict";
        } else
        if (scheme == Scheme.C_SCHEME) {
            functionName = functionName + "_Lazy";
        } else
        if (scheme == Scheme.UNBOX_INTERNAL_SCHEME) {
            functionName = functionName + "_Unboxed";
            returnType = SCJavaDefn.typeExprToTypeName(mf.getResultType());
        }

        JavaExpression root = new MethodInvocation.Static(thisTypeName, functionName, args, argTypes, returnType);

        return new ExpressionContextPair(root, newContext);
View Full Code Here

Examples of org.openquark.cal.machine.MachineFunction

            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) {
                    canIgnoreLaziness = ((LECCLiftedLetVarMachineFunction)mf).canIgnoreLaziness();
                }
            }
        }
View Full Code Here

Examples of org.openquark.cal.machine.MachineFunction

        //   potential to grow in this fashion then we can safely do a direct call to the tail recursive
        //   function.

        // Try to collect information about the called function.
        // This is used to decide how to encode the function call.
        MachineFunction mf = module.getFunction(calledFunction.getName());
        if (mf == null) {
            return false;
        }

        // Start of by checking if the called function is a closely connected component of this function.
        boolean safeToCallOnJavaStack =
            (!mf.getQualifiedName().getModuleName().equals(this.getModuleName()) ||!connectedComponents.contains(calledFunction.getName().getUnqualifiedName()));

        if (safeToCallOnJavaStack && mf.isTailRecursive()) {
            // Since the called function is tail recursive we need to
            // check the types of its arguments.
            // At the moment we do the simple check of allowing the call if
            // all arguments are primitive/foreign or an enum data type.
            // This way we know that holding the roots of the arguments can safely be held on the
View Full Code Here

Examples of org.openquark.cal.machine.MachineFunction

                useTypeSpecificAppNode = true;
                break;
            }
        }

        MachineFunction mf = module.getFunction(var.getName());
        if (mf != null && mf.isTailRecursive()) {
            useTypeSpecificAppNode = true;
        }


        // We want to use the SC specific application node that takes unboxed
View Full Code Here

Examples of org.openquark.cal.machine.MachineFunction

           
            @Override
            Expression transform (Expression e) {
                if (e.asVar() != null) {
                    Expression.Var var = e.asVar();
                    MachineFunction mf = module.getFunction(var.getName());
                    // Determine if the var is an alias for another function.
                    if (mf != null) {
                        if (mf.getAliasOf() != null) {
                            QualifiedName aliasOf = mf.getAliasOf();
                            Module aliasModule = module.findModule(aliasOf.getModuleName());
                           
                            if (aliasModule != null) {
                                ModuleTypeInfo aliasModuleTypeInfo = aliasModule.getModuleTypeInfo();
                               
                                // Need to create a new Expression.Var to substitute.
                                // NOTE: We have to handle Prelude.if as a special case because there is no entity for it
                                // in the type info.
                                if (aliasModuleTypeInfo != null) {
                                    FunctionalAgent functionalAgent = aliasModuleTypeInfo.getFunctionalAgent(aliasOf.getUnqualifiedName());
                                    if (functionalAgent != null) {
                                        //System.out.println ("substituting " + aliasOf + " for " + var.getName());                       
                                        return new Expression.Var (functionalAgent);
                                    } else {
                                        return new Expression.Var (aliasOf);
                                    }
                                } else {
                                    logger.logMessage(new CompilerMessage(new MessageKind.Error.UnableToResolveFunctionAlias(function.getQualifiedName().getQualifiedName(), var.getName().getQualifiedName(), aliasOf.getQualifiedName())));
                                }
                            } else {
                                logger.logMessage(new CompilerMessage(new MessageKind.Error.UnableToResolveFunctionAlias(function.getQualifiedName().getQualifiedName(), var.getName().getQualifiedName(), aliasOf.getQualifiedName())));
                            }
                        } else
                        if (mf.getLiteralValue() != null) {
                            return new Expression.Literal(mf.getLiteralValue());
                        }
                    }
                }
                return e;
            }
View Full Code Here

Examples of org.openquark.cal.machine.MachineFunction

           
            @Override
            Expression transform (Expression e) {
                if (e.asVar() != null) {
                    Expression.Var var = e.asVar();
                    MachineFunction mf = module.getFunction(var.getName());
                    // Determine if the var is an alias for another function.
                    if (mf != null) {
                        if (mf.getAliasOf() != null) {
                            QualifiedName aliasOf = mf.getAliasOf();
                            Module aliasModule = module.findModule(aliasOf.getModuleName());
                           
                            if (aliasModule != null) {
                                ModuleTypeInfo aliasModuleTypeInfo = aliasModule.getModuleTypeInfo();
                               
                                // Need to create a new Expression.Var to substitute.
                                // NOTE: We have to handle Prelude.if as a special case because there is no entity for it
                                // in the type info.
                                if (aliasModuleTypeInfo != null) {
                                    FunctionalAgent functionalAgent = aliasModuleTypeInfo.getFunctionalAgent(aliasOf.getUnqualifiedName());
                                    if (functionalAgent != null) {
                                        //System.out.println ("substituting " + aliasOf + " for " + var.getName());                       
                                        return new Expression.Var (functionalAgent);
                                    } else {
                                        return new Expression.Var (aliasOf);
                                    }
                                } else {
                                    logger.logMessage(new CompilerMessage(new MessageKind.Error.UnableToResolveFunctionAlias(function.getQualifiedName().getQualifiedName(), var.getName().getQualifiedName(), aliasOf.getQualifiedName())));
                                }
                            } else {
                                logger.logMessage(new CompilerMessage(new MessageKind.Error.UnableToResolveFunctionAlias(function.getQualifiedName().getQualifiedName(), var.getName().getQualifiedName(), aliasOf.getQualifiedName())));
                            }
                        } else
                        if (mf.getLiteralValue() != null) {
                            return new Expression.Literal(mf.getLiteralValue());
                        }
                    }
                }
                return e;
            }
View Full Code Here

Examples of org.openquark.cal.machine.MachineFunction

                                                            TypeExpr[] calledArgTypes,
                                                            VariableContext variableContext) throws CodeGenerationException {


        Expression.Var var = (Expression.Var)chain[0];
        MachineFunction mf = module.getFunction(var.getName());
        if (mf == null) {
            // Simply return null and let a different compilation path handle
            // the application.
            return null;
        }

        // At this point we need to do a check to make sure that we can
        // build a lazy application using a special purpose node.
        // The function must have an arity small enough to use a predefined
        // lazy app node or be tail recursive.  Special purpose lazy app nodes
        // are generated for tail recursive functions with arity greather than
        // that accomodated by the predefined lazy app nodes.
        if (calledArity > LECCMachineConfiguration.OPTIMIZED_APP_CHAIN_LENGTH &&
            !mf.isTailRecursive()) {
            // Simply return null and let a different compilation path handle
            // the application.
            return null;
        }

        // First generate the java expressions for the CAL expressions.
        ExpressionContextPair[] ecp = new ExpressionContextPair[chain.length];
        for (int i = 0; i < chain.length; ++i) {
            ecp[i] = genS_C(chain[i], variableContext);
        }

        Block newContext = ecp[0].getContextBlock();
        for (int i = 1; i < chain.length; ++i) {
            newContext.addStatement(ecp[i].getContextBlock());
        }

        if (codeGenerationStats != null) {
            if (var.getForeignFunctionInfo() != null) {
                codeGenerationStats.incrementLazyForeignNodeCount();
            } else {
                codeGenerationStats.incrementLazySCNodeCount();
            }
        }

        // This is a fully saturated supercombinator application that can be represented
        // using an special purpose graph node.
        JavaExpression args[] = new JavaExpression[calledArity];
        for (int i = 0; i < calledArity; ++i) {
            args[i] = ecp[i + 1].getJavaExpression();
        }

        JavaExpression scInstance = expressionVarToJavaDef(var, Scheme.C_SCHEME, variableContext);

        JavaExpression constructorArgs[] = new JavaExpression [calledArity + 1];
        constructorArgs[0] = scInstance;
        for (int i = 0; i < args.length; ++i) {
            constructorArgs[i+1] = args[i];
        }
        JavaTypeName constructorArgTypes[] = new JavaTypeName[calledArity + 1];
        Arrays.fill(constructorArgTypes, JavaTypeNames.RTVALUE);
        constructorArgTypes[0] = JavaTypeNames.RTSUPERCOMBINATOR;

        JavaTypeName appNodeType;
        // We want to use a type specific app node if the called function
        // is a tail recursive loop with arity greater than that supported
        // by the predefined lazy app nodes.
        if (mf.isTailRecursive() && calledArity > LECCMachineConfiguration.OPTIMIZED_APP_CHAIN_LENGTH) {
            appNodeType = CALToJavaNames.createLazyInnerTypeNameFromSC(var.getName(), module);
            constructorArgTypes[0] = CALToJavaNames.createTypeNameFromSC(var.getName(), module);
        } else {
            appNodeType = getTypeNameForApplicationNode(chain.length-1, false);
        }
View Full Code Here

Examples of org.openquark.cal.machine.MachineFunction

            }
        }

        // Try to collect information about the called function.
        // This is used to decide how to encode the function call.
        MachineFunction mf = module.getFunction(var.getName());
        if (mf == null) {
            return null;
        }

        // arity of the called function
        int calledArity = mf.getArity();

        if (calledArity >= chain.length) {
            // This is an undersaturated SC application.
            if (codeGenerationStats != null) {
                codeGenerationStats.incrementUnderSaturation(calledArity, chain.length-1);
            }
            ExpressionContextPair ecp = buildUndersaturatedAppNode(chain, mf, variableContext);
            if (ecp != null) {
                return ecp;
            } else {
                return buildGeneralApplicationChain (chain, scheme, variableContext);
            }
        }

        // argument strictness of the called function
        boolean[] calledArgStrictness;
        if (LECCMachineConfiguration.IGNORE_STRICTNESS_ANNOTATIONS) {
            Arrays.fill (calledArgStrictness, false);
        } else {
            calledArgStrictness = mf.getParameterStrictness();
        }

        // argument types of the called function
        TypeExpr[] calledArgTypes = mf.getParameterTypes();

        // does the called function have strict primitive arguments
        boolean calledHasStrictPrimitiveArgs = false;
        for (int i = 0; i < calledArity; ++i) {
            if (calledArgStrictness[i] && SCJavaDefn.canTypeBeUnboxed(calledArgTypes[i])) {
                calledHasStrictPrimitiveArgs = true;
                break;
            }
        }

        // is the called function tail recursive
        boolean calledIsTailRecursive = mf.isTailRecursive();


        // If this is a fully saturated function call we can optimize it if:
        // 1) The number of arguments is > zero and <= OPTIMIZED_APP_CHAIN_LENGTH, because the runtime has special purpose application
        //    nodes for these size of functions.
View Full Code Here
TOP
Copyright © 2018 www.massapi.com. 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.