Examples of MachineFunctionImpl


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

            int skipped_hasBadStructure = 0;
            int skipped_tooDeep = 0;
            int skipped_tooMuchTime = 0;
            int changedCounter = 0;
            while (functions.hasNext()) {
                MachineFunctionImpl cl = (MachineFunctionImpl) functions.next();
                CoreFunction coreFunction = cl.getCoreFunction();
               
                if (cl.getExpressionForm() instanceof Expression.PackCons) {
                    continue; // can't be optimized so skip these
                }
                if (cl.getName().startsWith("$")) {
                    continue; // skip compiler generated helper functions
                }

                try {
                    Expression newBody = applyCALOptimizer(cl.getTimeStamp(), coreFunction, moreFunctions);
                    if (!newBody.toString().equals(cl.getExpressionForm().toString())) {
                        ++changedCounter;
                    }
                    modules(newBody, moreModules);
                    cl.setExpression(newBody);
                } catch(TooManyHelperFunctionsException e){
                    skipped_tooManyHelperFunctionsCounter++;
                } catch (OptimizerHelper.UnsupportedExpressionTypeException e) {
                    skipped_unsupportedExpressionType++;
                } catch (HasBadStructureException e){
                    skipped_hasBadStructure++;
                } catch (TooDeepException e){
                    skipped_tooDeep++;
                } catch (TooMuchTimeException e){
                    skipped_tooMuchTime++;
                } catch (IllegalStateException e) {
                    // TODO fix this so all the types of expressions are
                    // handled.
                    skipped_otherCounter++;
                } catch (UnsupportedOperationException e){
                    skipped_unsupportedExpressionType++;
                } catch (IllegalArgumentException e) {
                    // TODO figure out why this is happening and fix it.
                    skipped_otherCounter++;
                } catch (Throwable e) {
                    // TODO figure out why this is happening and fix it.
                    skipped_otherCounter++;
                }
            }

            if (moreFunctions.size() > 0 ||
                    skipped_otherCounter > 0 ||
                    skipped_hasBadStructure > 0 ||
                    skipped_tooDeep > 0 ||
                    skipped_tooMuchTime > 0 ||
                    skipped_tooManyHelperFunctionsCounter > 0 ||
                    skipped_unsupportedExpressionType > 0 ||
                    changedCounter > 0) {
                System.out.print(m.getName());
                System.out.println(": ");
                if (moreFunctions.size() > 0) {
                    System.out.println("    Added " + moreFunctions.size()
                            + " more functions.");
                }
                if (changedCounter > 0) {
                    System.out.println("    Changed " + changedCounter + " of "
                            + functionsList.size() + " expressions.");
                }
                if (
                        skipped_unsupportedExpressionType > 0 ||
                        skipped_tooManyHelperFunctionsCounter > 0 ||
                        skipped_hasBadStructure > 0 ||
                        skipped_tooDeep > 0 ||
                        skipped_tooMuchTime > 0 ||
                        skipped_otherCounter > 0) {
                    System.out.println("    Skipped " + (skipped_hasBadStructure + skipped_otherCounter + skipped_tooManyHelperFunctionsCounter + skipped_unsupportedExpressionType) + " of " + functionsList.size() + " expressions.");
                    if (skipped_tooManyHelperFunctionsCounter > 0) {
                        System.out.println("        Too many helpers " + skipped_tooManyHelperFunctionsCounter + " of " + functionsList.size() + " expressions.");
                    }
                    if (skipped_unsupportedExpressionType > 0) {
                        System.out.println("        Unsupported expression types " + skipped_unsupportedExpressionType + " of " + functionsList.size() + " expressions.");
                    }
                    if (skipped_hasBadStructure > 0) {
                        System.out.println("        Incomplete optimization " + skipped_hasBadStructure + " of " + functionsList.size() + " expressions.");
                    }
                    if (skipped_tooDeep > 0) {
                        System.out.println("        Optimization too deep " + skipped_tooDeep + " of " + functionsList.size() + " expressions.");
                    }
                    if (skipped_tooMuchTime > 0) {
                        System.out.println("        Too much time " + skipped_tooMuchTime + " of " + functionsList.size() + " expressions.");
                    }                   
                    if (skipped_otherCounter > 0) {
                        System.out.println("        Other " + skipped_otherCounter + " of " + functionsList.size() + " expressions.");
                    }                   
                }
            }

            // add the helper functions to the list of functions for the module.
            {
                for (final MachineFunction mf : moreFunctions) {
                    m.addFunction(mf);
                    functionsList.add(mf);
                }
            }
           
        }
       
        Iterator<MachineFunction> functions = functionsList.iterator();
        Map<String, Expression> nameToExpressionMap = new HashMap<String, Expression>();
        while (functions.hasNext()) {
            MachineFunction cl = functions.next ();
            nameToExpressionMap.put (cl.getName(), cl.getExpressionForm());
        }
       
        // Determine the strongly connected components and put the information into the code labels.
        List<Set<String>> sets = ExpressionAnalyzer.determineStronglyConnectedComponents(m.getName(), nameToExpressionMap);
        for (int i = 0; i < sets.size(); ++i) {
            Set<String> set = sets.get(i);
            Iterator<String> items = set.iterator();
            while (items.hasNext()) {
                String name = items.next();
                MachineFunctionImpl mf = (MachineFunctionImpl)m.getFunction(name);
                if (mf != null) {
                    mf.setStronglyConnectedComponents(set);
                }
            }
        }

        // Now that we've determined the closely connected components we can use
        // an ExpressionAnalyzer to do optimizing transformations to the expression.
        functions = functionsList.iterator();
        while (functions.hasNext()) {
           
            MachineFunction cl = functions.next ();
            ExpressionAnalyzer ea = new ExpressionAnalyzer (m.getModuleTypeInfo(), cl);
           
            cl.setExpression(ea.transformExpression(cl.getExpressionForm()));
            if (ea.getHadUnsafeCoerce()){
                cl.setHadUnsafeCoerce();
            }
            cl.setIsTailRecursive(ExpressionAnalyzer.isTailRecursive(cl.getExpressionForm()));
        }       
       
       
        // At this point we want to figure out whether any functions are
        // simply an alias for another function and put the name of the aliased function
        //in the MachineFunction.
        determineFunctionAliases(functionsList);
       
        functions = functionsList.iterator();
        while (functions.hasNext ()) {
            MachineFunction mf = functions.next ();
            ExpressionAnalyzer.antiAlias (mf, m, logger);
            mf.setOptimized();
        }
       
    }
View Full Code Here

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

        synchronized (lock) {
            s.writeInt(functionNameToFunctionMap.size());
          
            for (final Map.Entry<String, MachineFunction> entry : functionNameToFunctionMap.entrySet()) {
               
                MachineFunctionImpl mf = (MachineFunctionImpl)entry.getValue();
                mf.write (s);
            }
        }
        s.endRecord ();
    }
View Full Code Here

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

            // Mark labels as adjuncts, and whether they will have code generated.
            for (final MachineFunction machineFunction : module.getFunctions()) {

                // Get the label.
                MachineFunctionImpl label = (MachineFunctionImpl)machineFunction;

                if (label.isCodeGenerated()) {
                    continue;
                }

                if (label.getAliasOf() != null || label.getLiteralValue() != null) {
                    label.setCodeGenerated(true);
                    //System.out.println("**** Skipping: " + label.getQualifiedName() + "  -> alias of: " + label.getCoreFunction().getAliasOf());
                    continue;
                }

                /*
                 * Mark adjuncts.
                 */
                if (isForAdjunct()) {
                    // Check to see if this is a primitive function.
                    // Functions marked as primitive can be one of two things:
                    // 1) a primitive function implemented as a machine specific operator
                    // 2) a primitive function for which a hard coded machine specific implementation is provided.
                    // If the function falls into category two we don't want to generate anything for it.
                    if (label.isPrimitiveFunction()) {
                        // Check to see if this is considered a primitiv op.
                        if (PrimOps.fetchInfo(label.getQualifiedName()) == null && !label.getName().equals("unsafeCoerce")) {
                            // don't need to generate code.
                            continue;
                        }
                    }

                    // The class to mark as an adjunct.
                    String fullClassName;

                    // This can either be a supercombinator or a data declaration
                    PackCons packCons = label.getExpressionForm().asPackCons();
                    if (packCons != null) {
                        // This is a data declaration
                        TypeConstructor typeCons = packCons.getDataConstructor().getTypeConstructor();

                        if (LECCMachineConfiguration.TREAT_ENUMS_AS_INTS && TypeExpr.isEnumType(typeCons)) {
                            //System.out.println ("**** skipping data type because it is enum: " + typeConsName);
                            continue;
                        }

                        fullClassName = CALToJavaNames.createFullClassNameFromType (typeCons, module);

                    } else {
                        // This is a supercombinator.
                        fullClassName = CALToJavaNames.createFullClassNameFromSC(label.getQualifiedName(), module);
                    }

                    // Mark the adjunct.
                    calLoader.markAdjunctClass(fullClassName);
                }

                // Clear the expression form, since we're finished with it.
                label.setCodeGenerated(true);
            }

            informStatusListeners(StatusListener.SM_GENCODE_DONE, module.getName());

            // Write out the compiled module definition if needed.
View Full Code Here

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

            // Emit supercombinator symbols and collate data definitions
            // For every symbol, we generate its code, which determines what it is, and builds auxiliary entities
            for (final MachineFunction machineFunction : module.getFunctions()) {

                // Get the label.
                MachineFunctionImpl label = (MachineFunctionImpl)machineFunction;

                if (label.isCodeGenerated()) {
                    continue;
                }

                if (label.getAliasOf() != null || label.getLiteralValue() != null) {
                    label.setCodeGenerated(true);
                    //System.out.println("**** Skipping: " + label.getQualifiedName() + "  -> alias of: " + label.getCoreFunction().getAliasOf());
                    continue;
                }

                // Check to see if this is a primitive function.
                // Functions marked as primitive can be one of two things:
                // 1) a primitive function implemented as a machine specific operator
                // 2) a primitive function for which a hard coded machine specific implementation is provided.
                // If the function falls into category two we don't want to generate anything for it.
                if (label.isPrimitiveFunction()) {
                    // Check to see if this is considered a primitiv op.
                    if (PrimOps.fetchInfo(label.getQualifiedName()) == null && !label.getName().equals("unsafeCoerce")) {
                        // don't need to generate code.
                        continue;
                    }
                }

                // This can either be a supercombinator or a data declaration
                if (label.getExpressionForm().asPackCons() != null) {
                    // This is a data declaration
                    // Add the associated TypeConstructor to the set for later code generation.
                    DataConstructor dc = label.getExpressionForm().asPackCons().getDataConstructor();
                    TypeConstructor typeCons = dc.getTypeConstructor();
                    typeConsSet.add(typeCons);
                } else {
                    // This is a supercombinator.  Emit the definition if necessary.
                    try {
                        LECCModule.FunctionGroupInfo fgi = module.getFunctionGroupInfo(label);

                        javaGenerator.createFunction(fgi, generateAll, logger);
                        fgi.setCodeGenerated(true);
                    } catch (CodeGenerationException e) {
                        try {
                            // Note: The code generation could potentially have failed because a foreign type or a foreign function's corresponding Java entity
                            // could not be resolved. (In this case the CodeGenerationException would be wrapping an UnableToResolveForeignEntityException)

                            final Throwable cause = e.getCause();
                            if (cause instanceof UnableToResolveForeignEntityException) {
                                generateLogger.logMessage(((UnableToResolveForeignEntityException)cause).getCompilerMessage());
                            }

                            generateLogger.logMessage(new CompilerMessage(new MessageKind.Error.CodeGenerationAborted(label.getQualifiedName().getQualifiedName()), e));

                        } catch (CompilerMessage.AbortCompilation e2) {/* Ignore and continue. */}

                        return CompilerMessage.Severity.ERROR;

                    } catch (CompilerMessage.AbortCompilation e) {
                        try {
                            generateLogger.logMessage(new CompilerMessage(new MessageKind.Error.CodeGenerationAborted(label.getQualifiedName().getQualifiedName())));
                        } catch (CompilerMessage.AbortCompilation e2) {/* Ignore and continue. */}

                        return CompilerMessage.Severity.ERROR;
                    }

                    if (isForAdjunct()) {
                        String fullClassName = CALToJavaNames.createFullClassNameFromSC(label.getQualifiedName(), module);
                        calLoader.markAdjunctClass(fullClassName);
                    }
                }

                // Clear the expression form, since we're finished with it.
                label.setCodeGenerated(true);
            }


            // Emit any data definitions as necessary.
            for (final TypeConstructor typeCons : typeConsSet) {
View Full Code Here

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

            return nZeroArityFunctions > 0;
        }
       
        void setCodeGenerated (boolean b) {
            for (final MachineFunction machineFunction : machineFunctions.values()) {
                MachineFunctionImpl mf = (MachineFunctionImpl)machineFunction;
                mf.setCodeGenerated(b);
            }           
        }
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.