Package org.openquark.cal.internal.machine

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


        } else if (scheme == Scheme.C_SCHEME) {
            compiledBody = genS_C(let.getBody(), variableContext);

        } else {
            throw new CodeGenerationException("Unrecognized let type: " + scheme);
        }


        if (canIgnoreLaziness) {
            if(varInfo.getUnboxedType() != null) {
View Full Code Here


        JavaTypeName tn = PrimOp.getTypeNameForPrimOpArgument(bot.getPrimitiveOp(), argNum);
        if (tn == null) {
            if (bot.getPrimitiveOp() == PrimOps.PRIMOP_FOREIGN_FUNCTION) {
                tn = getTypeNameForForeignFunctionArg (bot.getForeignFunctionInfo(), argNum);
            } else {
                throw new CodeGenerationException ("Attempt to retrieve argument type for " + bot.getName() + ", argNum = " + argNum + ".");
            }
        }
        return tn;
    }
View Full Code Here

        } else if (scheme == Scheme.C_SCHEME) {
            returnValue = genS_C (let.getBody(), variableContext);

        } else {
            throw new CodeGenerationException("Unrecognized let type: " + scheme);
        }

        return returnValue;
    }
View Full Code Here

        // Code a basic operation
        int op = basicOpExpressions.getPrimitiveOp ();
        int nArgs = basicOpExpressions.getNArguments ();

        if (nArgs < 0 || nArgs > 2) {
            throw new CodeGenerationException("Invalid basic operator arity: " + nArgs);
        }

        // The arguments for primitive ops are handled as a special case.
        // We want to avoid boxing/unboxing of primitive values wherever possible.
        Block generatedContext = new Block();
View Full Code Here

            //other than the special case of generating code for the function that calls the primitive op
            //e.g. org.openquark.cal.internal.runtime.lecc.cal_Math.Sin will directly call java.lang.Math.sin but
            //we will not directly call java.lang.Math.sin anywhere else.

            if (!basicOpExpressions.getName().equals(getQualifiedName())) {
                throw new CodeGenerationException("Should not directly call a primitive operator when GEN_DIRECT_PRIMOP_CALLS is false.");
            }
        }

        // Code a basic operation
        int op = basicOpExpressions.getPrimitiveOp ();
        int nArgs = basicOpExpressions.getNArguments ();

        if (nArgs < 0) {
            throw new CodeGenerationException("Invalid basic operator arity: " + nArgs);
        }

        if (op == PrimOps.PRIMOP_FOREIGN_FUNCTION) {
            // This is a foreign function
            return generateForeignCall (e, boxResult, scheme, variableContext);
View Full Code Here

            return;
        }

        if (desiredType instanceof JavaTypeName.Primitive != expressionType instanceof JavaTypeName.Primitive) {
            // Trying to treat an object as a primitive or vice versa.  This is not allowed.
            throw new CodeGenerationException ("Unboxed type mismatch in " + getModuleName() + "." + getFunctionName() + " : expression type = " + expressionType + ", desiredType = " + desiredType);
        }

        if (desiredType instanceof JavaTypeName.Primitive && !desiredType.equals(expressionType)) {
            // The desired and expression types are both primitives but not the same primitive.  This is not allowed.
            throw new CodeGenerationException ("Unboxed type mismatch in " + getModuleName() + "." + getFunctionName() + " : expression type = " + expressionType + ", desiredType = " + desiredType);
        }

        if (desiredType instanceof JavaTypeName.Reference.Object != expressionType instanceof JavaTypeName.Reference.Object) {
            // This means that we are trying to treat to equate an array and a non-array.
            // This is valid if we are treating an array as an object.
            if (!desiredType.equals(JavaTypeName.OBJECT)) {
                throw new CodeGenerationException ("Unboxed type mismatch in " + getModuleName() + "." + getFunctionName() + " : expression type = " + expressionType + ", desiredType = " + desiredType);
            }
        }

        if (desiredType instanceof JavaTypeName.Reference.Object && expressionType instanceof JavaTypeName.Reference.Object) {
            // Check to see that objects types are compatible.
            // i.e. it is valid to treat a String as an Object, but not vice versa.
            String dtn = ((JavaTypeName.Reference.Object)desiredType).getName();
            String etn = ((JavaTypeName.Reference.Object)expressionType).getName();
            try {
                ClassLoader foreignClassLoader = module.getForeignClassLoader();
                Class<?> dtc = Class.forName(dtn, true, foreignClassLoader);
                Class<?> etc = Class.forName(etn, true, foreignClassLoader);
                if (!dtc.isAssignableFrom(etc)) {
                    throw new CodeGenerationException ("Unboxed type mismatch in " + getModuleName() + "." + getFunctionName() + " : expression type = " + expressionType + ", desiredType = " + desiredType);
                }
            } catch (ClassNotFoundException e) {
                throw new CodeGenerationException ("Unable to determine unboxed type compatability in  " + getModuleName() + "." + getFunctionName() + " : expression type = " + expressionType + ", desiredType = " + desiredType);
            }
        }

        // If both types are arrays verify that the element types are compatible.
        if (desiredType instanceof JavaTypeName.Reference.Array && expressionType instanceof JavaTypeName.Reference.Array) {
View Full Code Here

                 e.asLiteral().getLiteral() instanceof String ||
                 e.asLiteral().getLiteral() instanceof BigInteger) {
                    KernelLiteral kernelLit = getKernelLiteral (e.asLiteral().getLiteral());
                    return new ExpressionContextPair(kernelLit.getUnboxedReference());
                } else {
                    throw new CodeGenerationException ("Unboxed type mismatch in " + getModuleName() + "." + getFunctionName() + " : expression type = " + e.asLiteral().getLiteral().getClass().getName() + ", desiredType = " + unboxType);
                }
            }

            // If the expression is a primitive op compile it as an unboxed op.
            //We would not want to directly call primitive operators when doing function tracing.
            //This will have the effect of ensuring that they get traced when called.
            BasicOpTuple bot;
            if (LECCMachineConfiguration.generateDirectPrimOpCalls() &&
                (bot = BasicOpTuple.isBasicOp(e)) != null) {

                // We need to be sure that the primitiveness of the operation/foreign function and the
                // desired unbox type match.  For example an operation that produces an int can either unbox to
                // an int or an Object.

                if (bot.getPrimitiveOp() == PrimOps.PRIMOP_FOREIGN_FUNCTION) {
                    // We know that this is an unboxed argument to a CAL function.
                    // The unboxed value cannot be void.  If we are calling a foreign
                    // function with return type of void we can't do a direct call.  We
                    // need to call the CAL wrapper function which will return the CAL
                    // equivalent to void. i.e. Unit
                    ForeignFunctionInfo ffi = bot.getForeignFunctionInfo();
                    Class<?> foreignReturnType = SCJavaDefn.getJavaReturnType(ffi);

                    verifyUnboxType(unboxType, JavaTypeName.make(foreignReturnType));


                    if(foreignReturnType.equals(void.class)) {
                        // This is the special case of a void foreign function.  The return type needs to
                        // be converted to the CAL Unit type.
                        // Compile the expression strictly, this will result in an RTValue which can be unboxed to the int equivalent
                        // of the unit type.
                        ExpressionContextPair argResult = genS_E (e, variableContext);
                        return new ExpressionContextPair(SCJavaDefn.unboxValue (unboxType, argResult.getJavaExpression()), argResult.getContextBlock());

                    } else if (bot.getPrimitiveOp() == PrimOps.PRIMOP_EAGER) {

                        throw new CodeGenerationException ("PRIMOP_EAGER encountered in generateUnboxedArgument.");

                    } else if (foreignReturnType.isPrimitive() == primitiveUnboxType){
                        // Using unsafeCoerce and input/output we can get CAL code that corresponds to
                        // treating primitives as objects and vice versa.  For example the Java primitive int and
                        // the Java class Integer get equated.
                        // We need to check that the foreign type of the expression being compiled has the same
                        // primitiveness as the unboxed type we are trying to generate.

                        return generatePrimitiveOp(e, false, Scheme.E_SCHEME, variableContext);
                    else {
                        throw new CodeGenerationException ("Primitivenes mismatch in " + getModuleName() + "." + getFunctionName() + " in generateUnboxedArgument.  Expression is " + foreignReturnType.toString() + " expected type is " + unboxType);
                    }
                } else {
                    if (bot.getPrimitiveOp() != PrimOps.PRIMOP_OBJECT_TO_CAL_VALUE &&
                        bot.getPrimitiveOp() != PrimOps.PRIMOP_CAL_VALUE_TO_OBJECT) {
                        verifyUnboxType (unboxType, PrimOp.getTypeNameForPrimOp(bot.getPrimitiveOp()));
View Full Code Here

                 e.asLiteral().getLiteral() instanceof String ||
                 e.asLiteral().getLiteral() instanceof BigInteger) {
                    KernelLiteral kernelLit = getKernelLiteral (e.asLiteral().getLiteral());
                    return new ExpressionContextPair(kernelLit.getUnboxedReference());
                } else {
                    throw new CodeGenerationException ("Unboxed type mismatch in " + getModuleName() + "." + getFunctionName() + " : expression type = " + e.asLiteral().getLiteral().getClass().getName() + ", desiredType = " + unboxType);
                }
            }

            // If the expression is a primitive op compile it as an unboxed op.
            //we don't directly call primitive operators if doing function tracing.
            //This will have the effect of ensuring that they get traced when called.
            BasicOpTuple bot;
            if (LECCMachineConfiguration.generateDirectPrimOpCalls() &&
                (bot = BasicOpTuple.isBasicOp(e)) != null) {

                // Generally speaking if an argument to a foreign function is expressed as a primitive op/foreign function we can
                // assume that the unboxed return value of the argument expression is of the correct type to pass.
                // One exception is when dealing with an argument expression of type Prelude.CalValue.  In this case we need to
                // get the Prelude.CalValue, evaluate, and then unbox the actual type needed by the foreign function.
                // Another is when dealing with Prelude.eager
                // Finally we need to be sure that the primitiveness of the operation/foreign function and the
                // desired unbox type match.  For example an operation that produces an int can either unbox to
                // an int or an Object.
                if (bot.getPrimitiveOp() == PrimOps.PRIMOP_FOREIGN_FUNCTION) {
                    // If this foreign SC is of type internal value we can't just get the unboxed value and pass
                    final ForeignFunctionInfo ffi = bot.getForeignFunctionInfo();
                    final Class<?> foreignReturnType = SCJavaDefn.getJavaReturnType(ffi);

                    // If this foreign SC is of type Prelude.CalValue we can't just get the unboxed value and pass
                    final boolean hasCalValueReturnType = isCalValueClass(foreignReturnType);
                    if (hasCalValueReturnType){
                        ExpressionContextPair argResult = genS_E (e, variableContext);
                        return new ExpressionContextPair(unboxValue(unboxType, SCJavaDefn.createInvocation (argResult.getJavaExpression(), SCJavaDefn.EVALUATE, SCJavaDefn.EXECUTION_CONTEXT_VAR)), argResult.getContextBlock());
                    }
                    verifyUnboxType(unboxType, JavaTypeName.make(foreignReturnType));
                    return generatePrimitiveOp (e, false, Scheme.E_SCHEME, variableContext);
                } else
                if (bot.getPrimitiveOp() == PrimOps.PRIMOP_EAGER) {
                    throw new CodeGenerationException ("PRIMOP_EAGER encountered in generateUnboxedForeignFunctionArgument.");
                } else
                if (bot.getPrimitiveOp() != PrimOps.PRIMOP_OBJECT_TO_CAL_VALUE &&
                    bot.getPrimitiveOp() != PrimOps.PRIMOP_CAL_VALUE_TO_OBJECT) {
                    verifyUnboxType(unboxType, PrimOp.getTypeNameForPrimOp(bot.getPrimitiveOp()));
                    return generatePrimitiveOp(e, false, Scheme.E_SCHEME, variableContext);
                }
            }

            if (isNot(e)) {
                verifyUnboxType(unboxType, JavaTypeName.BOOLEAN);
                   return generateNot (e, false, variableContext);
            }

            if (BasicOpTuple.isAndOr(e) != null) {
                verifyUnboxType(unboxType, JavaTypeName.BOOLEAN);
                return generateAndOr(e, false, variableContext);
            }

            if (e.asVar() != null) {
                Expression.Var var = e.asVar();
                if (variableContext.isLocalVariable(var.getName())) {
                    final JavaTypeName unboxedTypeOfVar = variableContext.getUnboxedType(var.getName());
                    if (unboxedTypeOfVar != null) {
                        verifyUnboxType(unboxType, unboxedTypeOfVar);
                    }

                    // At this point we know that the desired unboxType is compatible with the
                    // unboxed type of the variable (since we called verifyUnboxtype).
                    // If the desired unbox type is not null and is equal to the variables unboxed type
                    // we can simply use the unboxed reference.
                    // If the desired unbox type is Object we can simply use the variables unboxed reference
                    // since we know the variable can't be primitive (because we called verifyUnboxType) and
                    // all non-primitives in Java are an instance of Object.
                    if(unboxType.equals(unboxedTypeOfVar) ||
                       unboxType.equals(JavaTypeName.OBJECT)) {
                        JavaExpression unboxedRef = variableContext.getUnboxedReference(var.getName());
                        if (unboxedRef != null) {
                            return new ExpressionContextPair (unboxedRef);
                        }
                    } else {
                        if (unboxedTypeOfVar != null) {
                            // If the variable has an unboxed type, then in fact it should be the same as the desired unboxed type
                            // or should be java.lang.Object
                            throw new CodeGenerationException("The code generator should not be generating code where the desired unboxed type is not java.lang.Object nor the unboxed type of the variable.");
                        }
                    }
                }
            }
View Full Code Here

        }

        default:
        {
            throw new CodeGenerationException("Unable to return name for primitive type: " + typeName.getName());
        }

        }

    }
View Full Code Here

     * @return boxed expression
     * @throws CodeGenerationException
     */
    static JavaExpression boxExpression (TypeExpr boxType, JavaExpression e) throws CodeGenerationException {
        if (boxType == null || e == null) {
            throw new CodeGenerationException ("Attempt to box null type. ");
        }

        TypeConsApp typeConsApp = boxType.rootTypeConsApp();

        if (typeConsApp != null) {

            if (typeConsApp.isNonParametricType(CAL_Prelude.TypeConstructors.Boolean)) {
                return boxExpression (JavaTypeName.BOOLEAN, e);
            }

            if (typeConsApp.getForeignTypeInfo() != null) {
                return boxExpression (JavaTypeName.make(SCJavaDefn.getForeignType(typeConsApp.getForeignTypeInfo())), e);
            }

            if (LECCMachineConfiguration.TREAT_ENUMS_AS_INTS) {
                if (SCJavaDefn.isEnumDataType (typeConsApp)) {
                    return boxExpression (JavaTypeName.INT, e);
                }
            }
        }

        throw new CodeGenerationException ("Attempt to box unhandled type: " + boxType.toString());
    }
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.