Package org.openquark.cal.internal.machine

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


     */
    private JavaExpression boxPrimitiveOpResult (int op, JavaExpression arg) throws CodeGenerationException {

        switch (op) {
            case PrimOps.PRIMOP_NOP:
                throw new CodeGenerationException ("Attemp to box argument for PRIMOP_NOP.");

            // Booleans
            case PrimOps.PRIMOP_OR:
            case PrimOps.PRIMOP_AND:
            case PrimOps.PRIMOP_EQUALS_INT:
            case PrimOps.PRIMOP_NOT_EQUALS_INT:
            case PrimOps.PRIMOP_GREATER_THAN_INT:
            case PrimOps.PRIMOP_GREATER_THAN_EQUALS_INT:
            case PrimOps.PRIMOP_LESS_THAN_INT:
            case PrimOps.PRIMOP_LESS_THAN_EQUALS_INT:
            case PrimOps.PRIMOP_EQUALS_DOUBLE:
            case PrimOps.PRIMOP_NOT_EQUALS_DOUBLE:
            case PrimOps.PRIMOP_GREATER_THAN_DOUBLE:
            case PrimOps.PRIMOP_GREATER_THAN_EQUALS_DOUBLE:
            case PrimOps.PRIMOP_LESS_THAN_DOUBLE:
            case PrimOps.PRIMOP_LESS_THAN_EQUALS_DOUBLE:
            case PrimOps.PRIMOP_EQUALS_FLOAT:
            case PrimOps.PRIMOP_NOT_EQUALS_FLOAT:
            case PrimOps.PRIMOP_GREATER_THAN_FLOAT:
            case PrimOps.PRIMOP_GREATER_THAN_EQUALS_FLOAT:
            case PrimOps.PRIMOP_LESS_THAN_FLOAT:
            case PrimOps.PRIMOP_LESS_THAN_EQUALS_FLOAT:
            case PrimOps.PRIMOP_EQUALS_LONG:
            case PrimOps.PRIMOP_NOT_EQUALS_LONG:
            case PrimOps.PRIMOP_GREATER_THAN_LONG:
            case PrimOps.PRIMOP_GREATER_THAN_EQUALS_LONG:
            case PrimOps.PRIMOP_LESS_THAN_LONG:
            case PrimOps.PRIMOP_LESS_THAN_EQUALS_LONG:
            case PrimOps.PRIMOP_EQUALS_SHORT:
            case PrimOps.PRIMOP_NOT_EQUALS_SHORT:
            case PrimOps.PRIMOP_GREATER_THAN_SHORT:
            case PrimOps.PRIMOP_GREATER_THAN_EQUALS_SHORT:
            case PrimOps.PRIMOP_LESS_THAN_SHORT:
            case PrimOps.PRIMOP_LESS_THAN_EQUALS_SHORT:
            case PrimOps.PRIMOP_EQUALS_BYTE:
            case PrimOps.PRIMOP_NOT_EQUALS_BYTE:
            case PrimOps.PRIMOP_GREATER_THAN_BYTE:
            case PrimOps.PRIMOP_GREATER_THAN_EQUALS_BYTE:
            case PrimOps.PRIMOP_LESS_THAN_BYTE:
            case PrimOps.PRIMOP_LESS_THAN_EQUALS_BYTE:
            case PrimOps.PRIMOP_EQUALS_CHAR:
            case PrimOps.PRIMOP_NOT_EQUALS_CHAR:
            case PrimOps.PRIMOP_GREATER_THAN_CHAR:
            case PrimOps.PRIMOP_GREATER_THAN_EQUALS_CHAR:
            case PrimOps.PRIMOP_LESS_THAN_CHAR:
            case PrimOps.PRIMOP_LESS_THAN_EQUALS_CHAR:
                return createMakeKernelBooleanInvocation(arg);

            // Integers
            case PrimOps.PRIMOP_ADD_INT:
            case PrimOps.PRIMOP_SUBTRACT_INT:
            case PrimOps.PRIMOP_MULTIPLY_INT:
            case PrimOps.PRIMOP_NEGATE_INT:
            case PrimOps.PRIMOP_DIVIDE_INT:
            case PrimOps.PRIMOP_REMAINDER_INT:
            case PrimOps.PRIMOP_BITWISE_AND_INT:
            case PrimOps.PRIMOP_BITWISE_OR_INT:
            case PrimOps.PRIMOP_BITWISE_XOR_INT:
            case PrimOps.PRIMOP_COMPLEMENT_INT:
            case PrimOps.PRIMOP_SHIFTL_INT:
            case PrimOps.PRIMOP_SHIFTR_INT:
            case PrimOps.PRIMOP_SHIFTR_UNSIGNED_INT:
                return createMakeKernelIntInvocation(arg);

            // Doubles
            case PrimOps.PRIMOP_ADD_DOUBLE:
            case PrimOps.PRIMOP_SUBTRACT_DOUBLE:
            case PrimOps.PRIMOP_MULTIPLY_DOUBLE:
            case PrimOps.PRIMOP_DIVIDE_DOUBLE:
            case PrimOps.PRIMOP_NEGATE_DOUBLE:
            case PrimOps.PRIMOP_REMAINDER_DOUBLE:
                return createMakeKernelDoubleInvocation(arg);

            // Longs
            case PrimOps.PRIMOP_ADD_LONG:
            case PrimOps.PRIMOP_SUBTRACT_LONG:
            case PrimOps.PRIMOP_MULTIPLY_LONG:
            case PrimOps.PRIMOP_DIVIDE_LONG:
            case PrimOps.PRIMOP_NEGATE_LONG:
            case PrimOps.PRIMOP_REMAINDER_LONG:
            case PrimOps.PRIMOP_BITWISE_AND_LONG:
            case PrimOps.PRIMOP_BITWISE_OR_LONG:
            case PrimOps.PRIMOP_BITWISE_XOR_LONG:
            case PrimOps.PRIMOP_COMPLEMENT_LONG:
            case PrimOps.PRIMOP_SHIFTL_LONG:
            case PrimOps.PRIMOP_SHIFTR_LONG:
            case PrimOps.PRIMOP_SHIFTR_UNSIGNED_LONG:
                return createMakeKernelLongInvocation(arg);

            // Floats
            case PrimOps.PRIMOP_ADD_FLOAT:
            case PrimOps.PRIMOP_SUBTRACT_FLOAT:
            case PrimOps.PRIMOP_MULTIPLY_FLOAT:
            case PrimOps.PRIMOP_DIVIDE_FLOAT:
            case PrimOps.PRIMOP_NEGATE_FLOAT:
            case PrimOps.PRIMOP_REMAINDER_FLOAT:
                return createMakeKernelFloatInvocation(arg);


            case PrimOps.PRIMOP_FIELD_NAMES:
            case PrimOps.PRIMOP_FIELD_VALUES:
            case PrimOps.PRIMOP_MAKE_ITERATOR:
            case PrimOps.PRIMOP_MAKE_COMPARATOR:
            case PrimOps.PRIMOP_MAKE_EQUIVALENCE_RELATION:
            case PrimOps.PRIMOP_MAKE_CAL_FUNCTION:
            case PrimOps.PRIMOP_EXECUTION_CONTEXT:
                return createMakeKernelOpaqueInvocation(arg);

            case PrimOps.PRIMOP_HAS_FIELD:
                return createMakeKernelBooleanInvocation(arg);

            case PrimOps.PRIMOP_RECORD_FIELD_INDEX:
                return createMakeKernelIntInvocation(arg);

            case PrimOps.PRIMOP_CAL_VALUE_TO_OBJECT:
            case PrimOps.PRIMOP_OBJECT_TO_CAL_VALUE:
                return arg;

            default:
            {
                throw new CodeGenerationException("Unrecognized primop " + op + ".");
            }
        }
    }
View Full Code Here


                    return generateSwitchOnInteger (eswitch, variableContext);
                } else
                if (firstAltTag instanceof Character) {
                    return generateSwitchOnCharacter (eswitch, variableContext);
                } else {
                    throw new CodeGenerationException ("Unexpected tag type encountered in switch: " + firstAltTag.getClass ().getName ());
                }
            }
        }

        throw new CodeGenerationException ("Invalid switch expression encountered in: " + getFunctionName());
    }
View Full Code Here

                break;
            }
        }

        if (typeCons == null) {
            throw new CodeGenerationException ("Unable to retrieve TypeConstructor for switch in " + getFunctionName() + ".");
        }



        int nDataConstructorsForType = typeCons.getNDataConstructors();
        if (nDataConstructorsForType == 0) {
            throw new CodeGenerationException ("Encountered a data type with zero data constructors in a switch in " + getFunctionName() + ".");
        }

        DataConstructor[] allDCs = new DataConstructor [nDataConstructorsForType];
        for (int i = 0; i < nDataConstructorsForType; ++i ) {
            DataConstructor dc = typeCons.getNthDataConstructor(i);
            allDCs[dc.getOrdinal()] = dc;
        }

        // If all the case alternatives return a boolean literal we may
        // be able to optimize this.
        boolean isa = true;
        for (int i = 0; i < alts.length; ++i) {
            SwitchAlt switchAlt = alts[i];
            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);

            // Add the local variable declaration.
            caseVarDeclaration = new LocalVariableDeclaration(caseVar);
            switchBlock.addStatement(caseVarDeclaration);

            // Compile the expression we are switching on strictly.
            ExpressionContextPair pair = genS_E(switchExpression, variableContext);
            switchBlock.addStatement(pair.getContextBlock());

            JavaExpression caseExpression = pair.getJavaExpression();
            //caseExpression = releaseVarsInSwitchCondition(eswitch, caseExpression, variableContext);

            // We may need to cast the result of the case expression to the type of the local variable.
            caseExpression = (isEnumDataType || caseExpression instanceof ClassInstanceCreationExpression) ? caseExpression : new CastExpression(typeClassName, caseExpression);

            // Assign the result of the switch expression to the local an then get the ordinal value.
            JavaExpression assignLocal = new JavaExpression.Assignment(caseVar, caseExpression);
            JavaExpression getOrdinal = SCJavaDefn.createInvocation(assignLocal, SCJavaDefn.GETORDINALVALUE);

            switchStatement = new SwitchStatement(getOrdinal);
            switchBlock.addStatement(switchStatement);
        }

        // Populate the switch statement with case statement groups.
        for (final SwitchAlt alt : alts) {
            List<Object> altTags = alt.getAltTags();

            // If no variables are used, we can share the code among all data constructors for this alt.
            if (!alt.hasVars()) {

                Block caseBlock = new Block();

                // Add a comment for the data constructors in the group if any (ie. if not the default alt).
                if (alt.getFirstAltTag() instanceof DataConstructor) {
                    StringBuilder commentSB = new StringBuilder();

                    boolean firstDC = true;
                    for (final Object tag : altTags) {
                        DataConstructor tagDC = (DataConstructor)tag;
                        if (firstDC) {
                            firstDC = false;
                        } else {
                            commentSB.append(", ");
                        }
                        commentSB.append(tagDC.getName().getQualifiedName());

                    }
                    caseBlock.addStatement(new LineComment(commentSB.toString()));
                }

                // Create a new child variable scope to handle the alternate and any let variables it contains.
                variableContext.pushJavaScope();

                // Compile the body of the alternate.
                JavaStatement altStatement = genS_R(alt.getAltExpr(), variableContext);
                caseBlock.addStatement(variableContext.popJavaScope());
                caseBlock.addStatement(altStatement);

                if (alt.isDefaultAlt()) {
                    switchStatement.addCase(new SwitchStatement.DefaultCase(caseBlock));

                } else {
                    int[] caseLabels = new int[altTags.size()];
                    int index = 0;
                    for (final Object tag : altTags) {
                        if (!(tag instanceof DataConstructor)) {
                            throw new CodeGenerationException ("Unknown tag type in DC case statement in " + getFunctionName() + ": " + tag.getClass().getName());
                        }

                        caseLabels[index] = ((DataConstructor)tag).getOrdinal();
                        index++;
                    }
View Full Code Here

            Block caseBlock = new Block();

            // If we are switching on an integer we should never have alt variables.
            if (alt.hasVars()) {
                throw new CodeGenerationException ("Alt vars encountered in integer switch in " + getFunctionName() + ".");
            }

            JavaStatement altStatement = genS_R(alt.getAltExpr(), variableContext);
            caseBlock.addStatement(variableContext.popJavaScope());
            caseBlock.addStatement(altStatement);

            if (alt.isDefaultAlt()) {
                switchStatement.addCase(new SwitchStatement.DefaultCase(caseBlock));

            } else {
                List<Object> altTags = alt.getAltTags();
                int[] caseLabels = new int[altTags.size()];

                int index = 0;
                for (final Object tag : altTags) {
                    if (!(tag instanceof Integer)) {
                        throw new CodeGenerationException ("Unknown tag type in case statement in " + getFunctionName() + ":" + tag.getClass().getName() + ".");
                    }

                    caseLabels[index] = ((Integer)tag).intValue();
                    index++;
                }
View Full Code Here

            Block caseBlock = new Block();

            // If we are switching on an character we should never have alt variables.
            if (alt.hasVars()) {
                throw new CodeGenerationException ("Alt vars encountered in character switch in " + getFunctionName() + ".");
            }

            JavaStatement altStatement = genS_R(alt.getAltExpr(), variableContext);
            caseBlock.addStatement(variableContext.popJavaScope());
            caseBlock.addStatement(altStatement);

            if (alt.isDefaultAlt()) {
                switchStatement.addCase(new SwitchStatement.DefaultCase(caseBlock));

            } else {
                List<Object> altTags = alt.getAltTags();
                int[] caseLabels = new int[altTags.size()];

                int index = 0;
                for (final Object tag : altTags) {
                    if (!(tag instanceof Character)) {
                        throw new CodeGenerationException ("Unknown tag type in case statement in " + getFunctionName() + ":" + tag.getClass().getName() + ".");
                    }

                    caseLabels[index] = ((Character)tag).charValue();
                    index++;
                }
View Full Code Here

                    } 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()) {
View Full Code Here

        // Get this alternative's variables.  These have to be added to the active list of scope variables
        if (alt.hasVars()) {
            caseBlock.addStatement(new LineComment("Decompose data type to access members."));

            if (caseVar == null) {
                throw new CodeGenerationException ("Null case variable encountered in single alternate switch.");
            }

            // Must be a data constructor tag, since there are field names (see Expression.Switch.SwitchAlt).
            DataConstructor tagDC = (DataConstructor)altTag;
View Full Code Here

                return buildStrictApplicationChain(chain, calledArity, calledArgStrictness, calledArgTypes, variableContext);
            } else
            if (scheme == Scheme.C_SCHEME) {
                return buildLazyApplicationChain(chain, calledArity, calledArgStrictness, calledArgTypes, variableContext);
            } else {
                throw new CodeGenerationException ("Attempt to build application chain from unknown scheme: " + scheme);
            }
        }

        if (chain.length - 1 > calledArity) {
            return buildOversaturatedApplicationChain(applChain, chain, calledArity, scheme, variableContext);
View Full Code Here

                if (lazyRef != null) {
                    return lazyRef;
                }
            }

            throw new CodeGenerationException ("Unable to obtain reference to local variable " + qualName);
        } else {
            // This is not a local symbol (i.e. it is a supercombinator, foreign function, or constructor)
            // Kernel may claim the symbol if its defined in the Prelude module
            // First determine what kind of symbol this is
            if (dc != null) {
                // Constructor
                // Emit symbol with factory invocation

                JavaTypeName typeName = CALToJavaNames.createTypeNameFromDC(dc, module);
                JavaExpression dcInstance;
                dcInstance = getReferencedDC(typeName, dc);

                return dcInstance;
            } else {
                MachineFunction mf = module.getFunction(qualName);
                if (mf == null) {
                    throw new CodeGenerationException("Unable to retrieve MachineFunction for " + qualName.getQualifiedName());
                }

                LECCModule.FunctionGroupInfo fgi = module.getFunctionGroupInfo(qualName);
                if (fgi == null) {
                    throw new CodeGenerationException("Unable to retrieve FunctionGroupInfo for " + qualName.getQualifiedName());
                }

                // Function
                JavaTypeName fullClass = CALToJavaNames.createTypeNameFromSC(qualName, module);
View Full Code Here

     */
    private static Class<?> getForeignType(final ForeignTypeInfo foreignTypeInfo) throws CodeGenerationException {
        try {
            return foreignTypeInfo.getForeignType();
        } catch (UnableToResolveForeignEntityException e) {
            throw new CodeGenerationException("Failed to resolve foreign type.", e);
        }
    }
View Full Code Here

TOP

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

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.