Package org.openquark.cal.internal.javamodel

Examples of org.openquark.cal.internal.javamodel.JavaExpression


        if (arity < 0) {
            throw new CodeGenerationException("Invalid constructor operator arity: " + arity);
        } else
        if (arity == 0) {
            JavaExpression dcInstance;
            JavaTypeName typeName = CALToJavaNames.createTypeNameFromDC(dc, module);
            if (LECCMachineConfiguration.generateStatistics()) {
                // If this is a TagDC we want to pass the ordinal as an argument
                if (isTagDC(dc, getModule())) {
                    dcInstance = new ClassInstanceCreationExpression(typeName, JavaExpression.LiteralWrapper.make(Integer.valueOf(dc.getOrdinal())), JavaTypeName.INT);
                } else {
                    dcInstance = new ClassInstanceCreationExpression(typeName, SCJavaDefn.EXECUTION_CONTEXT_VAR, JavaTypeNames.RTEXECUTION_CONTEXT);
                }
            } else {
                dcInstance = getReferencedDC(typeName, dc);
            }
            return new ExpressionContextPair(dcInstance, argContext);
        } else{
            if (scheme == Scheme.C_SCHEME) {
                // This is a fully saturated DC application in a lazy context.
                // If there are no strict fields in the data constructor we can
                // simply create an instance of it.  Otherwise create an
                // appropriate application node.

                // First generate the java expressions for the CAL expressions.
                ExpressionContextPair[] ecp = new ExpressionContextPair[dc.getArity()];

                for (int i = 0; i < dc.getArity(); ++i) {
                    ecp[i] = genS_C(constructorOpExpressions.getArgument(i), variableContext);
                }

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

                JavaExpression dcExpr = expressionVarToJavaDef(constructorOpExpressions.getDataConstructorExpression(), scheme, variableContext);

                if (dc.getArity() <= LECCMachineConfiguration.OPTIMIZED_APP_CHAIN_LENGTH) {
                    JavaExpression nodeArgs[] = new JavaExpression[dc.getArity() + 1];
                    JavaTypeName nodeArgTypes[] = new JavaTypeName[dc.getArity() + 1];
                    nodeArgs[0] = dcExpr;
                    nodeArgTypes[0] = JavaTypeNames.RTSUPERCOMBINATOR;
                    for (int i = 0; i < dc.getArity(); ++i) {
                        nodeArgs[i+1] = ecp[i].getJavaExpression();
                        nodeArgTypes[i+1] = JavaTypeNames.RTVALUE;
                    }

                    JavaTypeName appNodeType = getTypeNameForApplicationNode(dc.getArity(), false);

                    return new ExpressionContextPair(new JavaExpression.ClassInstanceCreationExpression(appNodeType, nodeArgs, nodeArgTypes), newContext);
                } else {
                    JavaExpression target = dcExpr;
                    for (int i = 0; i < dc.getArity(); ++i) {
                        target = createInvocation(target, APPLY, ecp[i].getJavaExpression());
                    }
                    return new ExpressionContextPair (target, newContext);
                }
View Full Code Here


        final BasicOpTuple basicOpExpressions = BasicOpTuple.isBasicOp(e);

        final ForeignFunctionInfo foreignFunctionInfo = basicOpExpressions.getForeignFunctionInfo();
        final ForeignFunctionInfo.JavaKind kind = foreignFunctionInfo.getJavaKind();

        JavaExpression returnExpression = null;
        Block returnContext = new Block();

        if (kind.isMethod()) {

            final ForeignFunctionInfo.Invocation invocationInfo = (ForeignFunctionInfo.Invocation)foreignFunctionInfo;

            final Method method = (Method)SCJavaDefn.getJavaProxy(invocationInfo);
            if (method.getReturnType() == void.class && (scheme != Scheme.R_SCHEME)) {
                ExpressionContextPair ecp = genS_C (e, variableContext);
                JavaExpression expr = ecp.getJavaExpression();
                JavaStatement block = ecp.getContextBlock();
                JavaExpression.MethodInvocation eval = createInvocation(expr, EVALUATE, EXECUTION_CONTEXT_VAR);
                return new ExpressionContextPair(eval, block);
            }

            int nJavaArgs = SCJavaDefn.getNArguments(foreignFunctionInfo);
            final Class<?> invocationClass = SCJavaDefn.getInvocationClass(invocationInfo);
            final JavaTypeName invocationClassName = JavaTypeName.make(invocationClass);
            String methodName = method.getName();
            int startArg = 0;

            Class<?>[] exceptions = method.getExceptionTypes();
            for (int i = 0; i < exceptions.length; ++i) {
                if (!exceptionHandled(exceptions [i])) {
                    addExceptionHandler(exceptions[i]);
                }
            }

            if (!LECCMachineConfiguration.generateDirectPrimOpCalls()) {
                if (!exceptionHandled(Throwable.class)) {
                    addExceptionHandler(Throwable.class);
                }
            }

            JavaExpression target;
            InvocationType invocationType;      // static, virtual, or interface.  Can't be special (ie. private method).
            if (kind.isStatic()){
                target = null;
                invocationType = InvocationType.STATIC;
            } else {
                ExpressionContextPair pair = generateUnboxedForeignFunctionArgument(invocationClassName, basicOpExpressions.getArgument(0), variableContext);
                target = pair.getJavaExpression();
                invocationType = invocationClass.isInterface() ? InvocationType.INTERFACE : InvocationType.VIRTUAL;
                returnContext.addStatement(pair.getContextBlock());
                startArg = 1;
            }

            Class<?>[] argTypes = method.getParameterTypes();
            JavaExpression[] args = new JavaExpression[argTypes.length];
            JavaTypeName[] argTypeNames = new JavaTypeName[argTypes.length];

            for (int i = startArg, j = 0; i < nJavaArgs; ++i, ++j) {
                int index = i - startArg;
                final JavaTypeName argTypeName = JavaTypeName.make(argTypes[index]);
                ExpressionContextPair pair = generateUnboxedForeignFunctionArgument(argTypeName, basicOpExpressions.getArgument(i), variableContext);
                args[index] = pair.getJavaExpression();
                returnContext.addStatement(pair.getContextBlock());

                argTypeNames[index] = argTypeName;
            }

            JavaTypeName returnType = JavaTypeName.make(method.getReturnType());

            if (kind.isStatic()) {
                returnExpression = new MethodInvocation.Static(invocationClassName, methodName, args, argTypeNames, returnType);
            } else {
                returnExpression = new MethodInvocation.Instance(target, methodName, invocationClassName, args, argTypeNames, returnType, invocationType);
            }

            if (boxResult) {
                ExpressionContextPair pair = returnTypeToCal(method.getReturnType(), returnExpression);
                returnExpression = pair.getJavaExpression();
                returnContext.addStatement(pair.getContextBlock());
            }

            return new ExpressionContextPair(returnExpression, returnContext);

        } else if (kind.isField()) {

            final ForeignFunctionInfo.Invocation invocationInfo = (ForeignFunctionInfo.Invocation)foreignFunctionInfo;

            final Field field = (Field)SCJavaDefn.getJavaProxy(invocationInfo);

            JavaTypeName fieldType = JavaTypeName.make(field.getType());
            String fieldName = field.getName();

            final JavaTypeName invocationClassName = JavaTypeName.make(SCJavaDefn.getInvocationClass(invocationInfo));
            if (kind.isStatic()) {
                returnExpression = new JavaField.Static(invocationClassName, fieldName, fieldType);
            } else {
                ExpressionContextPair pair = generateUnboxedForeignFunctionArgument(invocationClassName, basicOpExpressions.getArgument(0), variableContext);
                JavaExpression instance = pair.getJavaExpression();
                returnContext.addStatement(pair.getContextBlock());
                returnExpression = new JavaField.Instance(instance, fieldName, fieldType);
            }

            if (boxResult) {
                ExpressionContextPair pair = returnTypeToCal (field.getType(), returnExpression);
                returnExpression = pair.getJavaExpression();
                returnContext.addStatement(pair.getContextBlock());
            }

            return new ExpressionContextPair(returnExpression, returnContext);

        } else if (kind.isConstructor()) {

            final ForeignFunctionInfo.Invocation invocationInfo = (ForeignFunctionInfo.Invocation)foreignFunctionInfo;

            Constructor<?> constructor = (Constructor<?>)SCJavaDefn.getJavaProxy(invocationInfo);
            int nJavaArgs = SCJavaDefn.getNArguments(foreignFunctionInfo);
            Class<?> clazz = constructor.getDeclaringClass();
            Class<?>[] argTypes = constructor.getParameterTypes();

            Class<?>[] exceptions = constructor.getExceptionTypes();
            for (int i = 0; i < exceptions.length; ++i) {
                if (!exceptionHandled(exceptions [i])) {
                    addExceptionHandler(exceptions[i]);
                }
            }

            JavaExpression[] args = new JavaExpression[argTypes.length];
            JavaTypeName[] argTypeNames = new JavaTypeName[argTypes.length];
            for (int i = 0; i < nJavaArgs; i++) {
                final JavaTypeName argTypeName = JavaTypeName.make(argTypes[i]);
                ExpressionContextPair pair = generateUnboxedForeignFunctionArgument(argTypeName, basicOpExpressions.getArgument(i), variableContext);
                args[i] = pair.getJavaExpression();
                returnContext.addStatement(pair.getContextBlock());
                argTypeNames[i] = argTypeName;
            }

            returnExpression = new ClassInstanceCreationExpression(JavaTypeName.make(clazz), args, argTypeNames);

            if (boxResult) {
                ExpressionContextPair pair = returnTypeToCal(constructor.getDeclaringClass(), returnExpression);
                returnExpression = pair.getJavaExpression();
                returnContext.addStatement(pair.getContextBlock());
            }

            return new ExpressionContextPair(returnExpression, returnContext);

        } else if (kind.isCast()) {

            final Class<?> argType = SCJavaDefn.getJavaArgumentType(foreignFunctionInfo, 0);
            final Class<?> resultType = SCJavaDefn.getJavaReturnType(foreignFunctionInfo);

            final ExpressionContextPair argExprPair = generateUnboxedForeignFunctionArgument(JavaTypeName.make(argType), basicOpExpressions.getArgument(0), variableContext);
            final JavaExpression argExpr = argExprPair.getJavaExpression();
            returnContext.addStatement(argExprPair.getContextBlock());

            if (kind == ForeignFunctionInfo.JavaKind.IDENTITY_CAST ||
                kind == ForeignFunctionInfo.JavaKind.WIDENING_REFERENCE_CAST) {

                //it is important to do nothing for a widening reference cast (except for evaluating)
                //this is because at the JavaTypeName level, where the inheritance hierarchy is not available, it is not possible for
                //the bytecode generator to determine if this is truly a widening reference cast i.e. a no-op. Hence we must make
                //this optimization at this point.
                returnExpression = argExpr;

            } else if (kind == ForeignFunctionInfo.JavaKind.NARROWING_PRIMITIVE_CAST ||
                       kind == ForeignFunctionInfo.JavaKind.WIDENING_PRIMITIVE_CAST ||
                       kind == ForeignFunctionInfo.JavaKind.NARROWING_REFERENCE_CAST) {

                returnExpression = new JavaExpression.CastExpression(JavaTypeName.make(resultType), argExpr);

            } else {
                throw new CodeGenerationException("Unrecognized foreign function cast kind: " + kind);
            }

            if (boxResult) {
                ExpressionContextPair pair = returnTypeToCal(resultType, returnExpression);
                returnExpression = pair.getJavaExpression();
                returnContext.addStatement(pair.getContextBlock());
            }

            return new ExpressionContextPair(returnExpression, returnContext);

        } else if (kind.isInstanceOf()) {

            final Class<?> argType = SCJavaDefn.getJavaArgumentType(foreignFunctionInfo, 0);
            final ForeignFunctionInfo.InstanceOf instanceOfInfo = (ForeignFunctionInfo.InstanceOf)foreignFunctionInfo;
            final Class<?> instanceOfType = SCJavaDefn.getInstanceOfType(instanceOfInfo);

            final ExpressionContextPair argExprPair = generateUnboxedForeignFunctionArgument(JavaTypeName.make(argType), basicOpExpressions.getArgument(0), variableContext);
            final JavaExpression argExpr = argExprPair.getJavaExpression();
            returnContext.addStatement(argExprPair.getContextBlock());

            returnExpression = new JavaExpression.InstanceOf(argExpr, JavaTypeName.make(instanceOfType));

            if (boxResult) {
                ExpressionContextPair pair = returnTypeToCal(boolean.class, returnExpression);
                returnExpression = pair.getJavaExpression();
                returnContext.addStatement(pair.getContextBlock());
            }

            return new ExpressionContextPair(returnExpression, returnContext);

        } else if (kind.isClassLiteral()) {

            final ForeignFunctionInfo.ClassLiteral classLiteralInfo = (ForeignFunctionInfo.ClassLiteral)foreignFunctionInfo;
            final Class<?> referentType = SCJavaDefn.getReferentType(classLiteralInfo);

            returnExpression = new JavaExpression.ClassLiteral(JavaTypeName.make(referentType));

            if (boxResult) {
                ExpressionContextPair pair = returnTypeToCal(Class.class, returnExpression);
                returnExpression = pair.getJavaExpression();
                returnContext.addStatement(pair.getContextBlock());
            }

            return new ExpressionContextPair(returnExpression, returnContext);

        } else if (kind.isNullLiteral()) {

            returnExpression = JavaExpression.LiteralWrapper.NULL;
            if (boxResult) {
                final ExpressionContextPair pair = returnTypeToCal(SCJavaDefn.getJavaReturnType(foreignFunctionInfo), returnExpression);
                returnExpression = pair.getJavaExpression();
                returnContext.addStatement(pair.getContextBlock());
            }

            return new ExpressionContextPair(returnExpression, returnContext);

        } else if (kind.isNullCheck()) {

            final ForeignFunctionInfo.NullCheck nullCheckInfo = (ForeignFunctionInfo.NullCheck)foreignFunctionInfo;

            final Class<?> argType = SCJavaDefn.getJavaArgumentType(foreignFunctionInfo, 0);

            final ExpressionContextPair argExprPair = generateUnboxedForeignFunctionArgument(JavaTypeName.make(argType), basicOpExpressions.getArgument(0), variableContext);
            final JavaExpression argExpr = argExprPair.getJavaExpression();
            returnContext.addStatement(argExprPair.getContextBlock());

            final JavaOperator javaOp = nullCheckInfo.checkIsNull() ? JavaOperator.EQUALS_OBJECT : JavaOperator.NOT_EQUALS_OBJECT;

            returnExpression = new JavaExpression.OperatorExpression.Binary(javaOp, argExpr, LiteralWrapper.NULL);

            if (boxResult) {
                final ExpressionContextPair pair = returnTypeToCal(boolean.class, returnExpression);
                returnExpression = pair.getJavaExpression();
                returnContext.addStatement(pair.getContextBlock());
            }

            return new ExpressionContextPair(returnExpression, returnContext);

        } else if (kind == ForeignFunctionInfo.JavaKind.NEW_ARRAY) {

            //e.g. newString3Array :: Int -> Int -> JString3Array; (for new String[d1][d2][])

            //note, this may be less than the dimension of the array e.g. for new String[10][7][] this is 2.
            final int nJavaArgs = SCJavaDefn.getNArguments(foreignFunctionInfo);
            final Class<?> newArrayType = SCJavaDefn.getJavaReturnType(foreignFunctionInfo);

            final JavaExpression[] args = new JavaExpression[nJavaArgs];
            final JavaTypeName[] argTypeNames = new JavaTypeName[nJavaArgs];
            for (int i = 0; i < nJavaArgs; i++) {
                final ExpressionContextPair pair = generateUnboxedForeignFunctionArgument(JavaTypeName.INT, basicOpExpressions.getArgument(i), variableContext);
                args[i] = pair.getJavaExpression();
                returnContext.addStatement(pair.getContextBlock());
                argTypeNames[i] = JavaTypeName.INT;
            }

            returnExpression = new ClassInstanceCreationExpression(JavaTypeName.make(newArrayType), args, argTypeNames);

            if (boxResult) {
                final ExpressionContextPair pair = returnTypeToCal(newArrayType, returnExpression);
                returnExpression = pair.getJavaExpression();
                returnContext.addStatement(pair.getContextBlock());
            }

            return new ExpressionContextPair(returnExpression, returnContext);

        } else if (kind == ForeignFunctionInfo.JavaKind.LENGTH_ARRAY) {

            final Class<?> argType = SCJavaDefn.getJavaArgumentType(foreignFunctionInfo, 0);

            final ExpressionContextPair argExprPair = generateUnboxedForeignFunctionArgument(JavaTypeName.make(argType), basicOpExpressions.getArgument(0), variableContext);
            final JavaExpression argExpr = argExprPair.getJavaExpression();
            returnContext.addStatement(argExprPair.getContextBlock());

            returnExpression = new JavaExpression.ArrayLength(argExpr);

            if (boxResult) {
View Full Code Here

        // These are set up as PlaceHolder instances, since the nature of
        // the reference will be determined by how the variable is used.
        // i.e. single vs. multiple use, boxed vs. unboxed, strict vs. lazy
        if (canIgnoreLaziness) {
            if (varInfo.getUnboxedType() != null) {
                JavaExpression localVar = new LocalVariable(varInfo.getJavaName()+"$U", varInfo.getUnboxedType());
                varInfo.updateUnboxedReference(localVar);
                JavaExpression boxedDef = SCJavaDefn.boxExpression(varInfo.getUnboxedType(), localVar);
                varInfo.updateStrictReference(boxedDef);
                varInfo.updateLazyReference(boxedDef);
            } else {
                JavaExpression localVar = new LocalVariable(varInfo.getJavaName(), JavaTypeNames.RTVALUE);
                varInfo.updateStrictReference(localVar);
                varInfo.updateLazyReference(localVar);
            }
        } else {
            JavaExpression localVar = new JavaExpression.LocalVariable(varInfo.getJavaName(), JavaTypeNames.RTVALUE);
            varInfo.updateLazyReference(localVar);
            JavaExpression evaluatedVar = SCJavaDefn.createInvocation(localVar, SCJavaDefn.EVALUATE, SCJavaDefn.EXECUTION_CONTEXT_VAR);
            varInfo.updateStrictReference(evaluatedVar);
            if (varInfo.getUnboxedType() != null) {
                varInfo.updateUnboxedReference(SCJavaDefn.unboxValue(varInfo.getUnboxedType(), evaluatedVar));
            }
        }
View Full Code Here

    private ExpressionContextPair generateNot (Expression e, boolean boxResult, VariableContext variableContext) throws CodeGenerationException {
        // Get the argument.
        Expression arg = e.asAppl().getE2();
        ExpressionContextPair ecp = generateUnboxedArgument(JavaTypeName.BOOLEAN, arg, variableContext);
        JavaExpression not = new JavaExpression.OperatorExpression.Unary(JavaOperator.LOGICAL_NEGATE, ecp.getJavaExpression());

        if (boxResult) {
            not = createMakeKernelBooleanInvocation(not);
        }
View Full Code Here

        }

        // 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();
        JavaExpression args[] = new JavaExpression[nArgs];
        for (int i = 0; i < nArgs; ++i) {
            int argIndex = nArgs - i - 1;
            ExpressionContextPair pair = generateUnboxedPrimOpArgument(basicOpExpressions, basicOpExpressions.getArgument(argIndex), argIndex, variableContext);
            args[argIndex] = pair.getJavaExpression();
            generatedContext.addStatement(pair.getContextBlock());
        }

        JavaExpression javaExpression = PrimOp.getPrimOpDefinition(op, args);

        if (boxResult) {
            javaExpression = boxPrimitiveOpResult(op, javaExpression);
        }
View Full Code Here

        }

        // 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();
        JavaExpression args[] = new JavaExpression[nArgs];
        for (int i = 0; i < nArgs; ++i) {
            int argIndex = nArgs - i - 1;
            ExpressionContextPair pair;
            if (op == PrimOps.PRIMOP_CAL_VALUE_TO_OBJECT) {
                //Prelude.calValueToObject is an unusual primitive op in that it is non-strict in its argument
                pair = genS_C(basicOpExpressions.getArgument(argIndex), variableContext);
            } else
            {
                pair = generateUnboxedPrimOpArgument(basicOpExpressions, basicOpExpressions.getArgument(argIndex), argIndex, variableContext);
            }

            args[argIndex] = pair.getJavaExpression();
            generatedContext.addStatement(pair.getContextBlock());
        }

        JavaExpression javaExpression = PrimOp.getPrimOpDefinition(op, args);

        if (boxResult) {
            javaExpression = boxPrimitiveOpResult(op, javaExpression);
        }
View Full Code Here

        // '.' in the module name gets mapped to '_'
        // '_' in the module name gets mapped to "__"
        String errorVarName = topLevelFunctionName.getModuleName().toSourceText().replaceAll("_", "__").replace('.', '_') + "_" + topLevelFunctionName.getUnqualifiedName() + "_" + line + "_" + column;

        JavaExpression newErrorInfo = sharedValues.getStaticError(errorVarName);

        if (newErrorInfo == null) {
            // Initialize newErrorInfo
            JavaExpression args[] = new JavaExpression[4];
            args[0] = LiteralWrapper.make (topLevelFunctionName.getModuleName().toSourceText());
            args[1] = LiteralWrapper.make (topLevelFunctionName.getUnqualifiedName());
            args[2] = LiteralWrapper.make( Integer.valueOf(line) );
            args[3] = LiteralWrapper.make( Integer.valueOf(column) );
View Full Code Here

     *
     * @param errorInfo
     * @return the call
     */
    private JavaExpression getBadSwitchIndexCall (Expression.ErrorInfo errorInfo) {
        final JavaExpression jErrorInfo;
        if (errorInfo != null) {
            jErrorInfo = getErrorInfo (errorInfo);
        } else {
            jErrorInfo = LiteralWrapper.NULL;
        }
View Full Code Here

     * @param errorInfo
     * @return the call
     */
    private JavaExpression getUnhandledSwitchIndexForIntPatternCall(Expression.ErrorInfo errorInfo) {

        final JavaExpression jErrorInfo;
        if (errorInfo != null) {
            jErrorInfo = getErrorInfo (errorInfo);
        } else {
            jErrorInfo = LiteralWrapper.NULL;
        }
View Full Code Here

     * @param errorInfo
     * @return the call
     */
    private JavaExpression getUnhandledSwitchIndexForCharPatternCall(Expression.ErrorInfo errorInfo) {

        final JavaExpression jErrorInfo;
        if (errorInfo != null) {
            jErrorInfo = getErrorInfo (errorInfo);
        } else {
            jErrorInfo = LiteralWrapper.NULL;
        }
View Full Code Here

TOP

Related Classes of org.openquark.cal.internal.javamodel.JavaExpression

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.