Package org.openquark.cal.internal.javamodel

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


               
                // For each data constructor create a constant int field for the ordinal.
                for (int i = 0, n = typeConstructor.getNDataConstructors(); i < n; ++i) {
                    DataConstructor dc = typeConstructor.getNthDataConstructor(i);
                    String ordinalFieldName = createEnumFieldName(dc.getName().getUnqualifiedName()) + "_ORDINAL";
                    JavaExpression initializer = LiteralWrapper.make(new Integer(dc.getOrdinal()));
                    JavaFieldDeclaration fieldDec =
                        new JavaFieldDeclaration (
                                Modifier.PUBLIC | Modifier.STATIC | Modifier.FINAL,
                                JavaTypeName.INT, ordinalFieldName, initializer);
                    dataTypeClass.addFieldDeclaration(fieldDec);
                   
                    JavaDocComment jdc =
                        new JavaDocComment ("Ordinal value corresponding to the " + dc.getName().getUnqualifiedName() + " data constructor.");
                    fieldDec.setJavaDoc(jdc);
                }
               
                // For each data constructor create a static field
                for (int i = 0, n = typeConstructor.getNDataConstructors(); i < n; ++i) {
                    DataConstructor dc = typeConstructor.getNthDataConstructor(i);
                   
                    String staticFieldName = createEnumFieldName(dc.getName().getUnqualifiedName());
                   
                    JavaExpression initializer =
                        new JavaExpression.ClassInstanceCreationExpression(
                                dataType_TypeName,
                                new JavaExpression[]{
                                        new JavaField.Static(
                                                dataType_TypeName,
View Full Code Here


                    argTypes[i] = type;

                    if (!typeConstructorInfo.commonFieldNames.contains(fn)) {
                        JavaExpression.JavaField.Instance field =
                            new JavaExpression.JavaField.Instance(null, fieldName, type);
                        JavaExpression assign = new Assignment (field, new MethodVariable(argName));
                        constructorBody.addStatement(new ExpressionStatement(assign));
                    }
                }
               
                String constructorName = dcClassTypeName.getUnqualifiedJavaSourceName();
                int index = constructorName.lastIndexOf('.');
                if (index > -1) {
                    constructorName = constructorName.substring(index + 1);
                }
               
                JavaConstructor constructor;
                if (typeConstructorInfo.commonFieldNames.size() > 0) {
                    JavaExpression superArgValues[] = new JavaExpression[typeConstructorInfo.commonFieldNames.size()];
                    JavaTypeName superArgTypes[] = new JavaTypeName[superArgValues.length];
                    int i = 0;
                    for (FieldName superFieldName : typeConstructorInfo.commonFieldNames) {
                        JavaTypeName fieldType = (JavaTypeName)dci.fieldTypeNames.get(superFieldName);
                        String fieldName = (String)typeConstructorInfo.fieldJavaNames.get(superFieldName);
                        String argName = fieldName+"$";
                        JavaExpression superArgValue = new MethodVariable(argName);
                        superArgValues[i] = superArgValue;
                        superArgTypes[i] = fieldType;
                        ++i;
                    }
                   
View Full Code Here

                // Unable to transform function body to directly return an unboxed value.
                // Fall back on calling fNS.

                Block body = new Block();

                JavaExpression argValues[] = new JavaExpression[getArity() + 1];
                JavaTypeName argTypes[] = new JavaTypeName[argValues.length];

                argValues[argValues.length-1] = SCJavaDefn.EXECUTION_CONTEXT_VAR;

                Arrays.fill(argTypes, JavaTypeNames.RTVALUE);
                argTypes[argTypes.length-1] = JavaTypeNames.RTEXECUTION_CONTEXT;

                for (int i = 0, n = getArity(); i < n; ++i) {
                    argValues[i] = new JavaExpression.MethodVariable(getJavaArgumentName(i));
                    if (isArgStrict(i) && SCJavaDefn.canTypeBeUnboxed(getArgumentType(i))) {
                        argTypes[i] = typeExprToTypeName(getArgumentType(i));
                    }
                }

                LECCModule.FunctionGroupInfo fgi = module.getFunctionGroupInfo(getQualifiedName());
                String functionName = fgi.getFnNamePrefix(getFunctionName()) + "f";
                if (getArity() > 0) {
                    functionName = functionName + getArity() + "S";
                }

                MethodInvocation fNS =
                    new MethodInvocation.Instance(
                            null,
                            functionName,
                            argValues,
                            argTypes,
                            JavaTypeNames.RTVALUE,
                            MethodInvocation.InvocationType.VIRTUAL);

                LocalVariable result = new LocalVariable("$result", JavaTypeNames.RTVALUE);
                LocalVariableDeclaration resultDecl = new LocalVariableDeclaration(result, fNS);

                body.addStatement(resultDecl);

                for (int i = 0, n = getArity(); i < n; ++i) {
                    if (!isArgStrict(i) || !SCJavaDefn.canTypeBeUnboxed(getArgumentType(i))) {
                        // Null out the argument value.
                        Assignment nullOut =
                            new Assignment((JavaExpression.Nameable)argValues[i], LiteralWrapper.NULL);
                        body.addStatement(new ExpressionStatement(nullOut));
                    }
                }

                MethodInvocation eval = createInvocation(result, SCJavaDefn.EVALUATE, SCJavaDefn.EXECUTION_CONTEXT_VAR);
                JavaExpression unbox = SCJavaDefn.unboxValue(SCJavaDefn.typeExprToTypeName(resultType), eval);

                body.addStatement(new ReturnStatement(unbox));

                unboxedBodyCode = body;
            }
View Full Code Here

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

        // Get the supercombinator we're calling.
        JavaExpression root = ecp[0].getJavaExpression();

        if (this.codeGenerationStats != null) {
            if (var.getForeignFunctionInfo() != null) {
                this.codeGenerationStats.incrementDirectForeignCalls();
            } else {
                this.codeGenerationStats.incrementDirectSCCalls();
            }
        }

        // Optimize Prelude.not to use the '!' java operator.
        if (var.getName().equals(CAL_Prelude.Functions.not)) {
            JavaExpression not = new JavaExpression.OperatorExpression.Unary(JavaOperator.LOGICAL_NEGATE, ecp[1].getJavaExpression());
            return new ExpressionContextPair (not, newContext);
        }


        JavaExpression[] args = new JavaExpression[calledArity + 1];
View Full Code Here

        }

        // e is a variable?
        if (e.asVar() != null) {
            Expression.Var var = e.asVar();
            JavaExpression varExpression = expressionVarToJavaDef(var, Scheme.C_SCHEME, variableContext);
            return new ExpressionContextPair (varExpression);
        }

        // With changes to the compiler to lift inner case statements to their own function we
        // should never encounter a case at this level.
View Full Code Here

        }

        // e is a variable?
        if (e.asVar() != null) {
            Expression.Var var = e.asVar();
            JavaExpression varExpression = expressionVarToJavaDef(var, Scheme.E_SCHEME, variableContext);

            if (variableContext.isLocalVariable(var.getName())) {
                return new ExpressionContextPair (varExpression );
            } else {
                // 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));
                }
            }
        }

        // Is e a conditional op (if <cond expr> <then expr> <else expr>)?
        CondTuple conditionExpressions = CondTuple.isCondOp(e);
        if (conditionExpressions != null) {
            if (codeGenerationStats != null) {
                codeGenerationStats.incrementOptimizedIfThenElse();
            }

            // This is a conditional op.  The conditionExpressions tuple holds (kCond, kThen, kElse) expressions
            // Generate the code for kThen and kElse, as arguments to a new I_Cond instruction

            Expression condExpression = conditionExpressions.getConditionExpression();
            ExpressionContextPair pair;
            pair = generateUnboxedArgument(JavaTypeName.BOOLEAN, condExpression, variableContext);

            Block contextBlock = pair.getContextBlock();
            JavaExpression condJavaExpression = pair.getJavaExpression();
            ExpressionContextPair thenPart = genS_E (conditionExpressions.getThenExpression(), variableContext);
            ExpressionContextPair elsePart = genS_E (conditionExpressions.getElseExpression(), variableContext);

            // Note: both contexts are evaluated..
            contextBlock.addStatement(thenPart.getContextBlock());
View Full Code Here

     * @param context - current variable context.  May be null.
     * @return the resulting java statement
     */
    private JavaStatement generateReturn(ExpressionContextPair pair, VariableContext context) {
        Block contextBlock = pair.getContextBlock();
        JavaExpression javaExpression = pair.getJavaExpression();

        if (javaExpression != null) {
            ReturnStatement ret = new ReturnStatement(javaExpression);

            if (context != null) {
View Full Code Here

        }

        // 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

        }

        // R is a variable?
        if (e.asVar() != null) {
            Expression.Var var = e.asVar();
            JavaExpression varExpression = expressionVarToJavaDef(var, Scheme.R_SCHEME, variableContext);

            // If the variable is already evaluated we want to call getValue(), which will follow any
            // indirection chains and return the final value.  However, if we are simply returning
            // a strict unboxed function argument this isn't necessary.
            if (variableContext.isLocalVariable(var.getName()) &&
                variableContext.isVarPreEvaluated(var.getName()) &&
                (!variableContext.isFunctionArgument(var.getName()) || !isArgUnboxable(var.getName()))) {
                // Variable is already evaluated so simply do a 'getValue()' on it.
                varExpression = new MethodInvocation.Instance(varExpression, "getValue", JavaTypeNames.RTVALUE, MethodInvocation.InvocationType.VIRTUAL);
            }

            return generateReturn(varExpression, variableContext);
        }

        // Is e a conditional op (if <cond expr> <then expr> <else expr>)?
        // Since this is the top level compilation scheme we can generate an actual 'if' statement.
        CondTuple conditionExpressions = CondTuple.isCondOp(e);
        if (conditionExpressions != null) {
            if (codeGenerationStats != null) {
                codeGenerationStats.incrementOptimizedIfThenElse();
            }

            // This is a conditional op.  The conditionExpressions tuple holds (kCond, kThen, kElse) expressions
            // Generate the code for kThen and kElse, as arguments to a new I_Cond instruction

            Expression condExpression = conditionExpressions.getConditionExpression();
            ExpressionContextPair pair = generateUnboxedArgument(JavaTypeName.BOOLEAN, condExpression, variableContext);

            // Then
            variableContext.pushJavaScope();
            JavaStatement thenPart = genS_R (conditionExpressions.getThenExpression(), variableContext);

            Block thenBlock = variableContext.popJavaScope();
            thenBlock.addStatement(thenPart);

            // Else
            variableContext.pushJavaScope();
            JavaStatement elsePart = genS_R (conditionExpressions.getElseExpression(), variableContext);

            Block elseBlock = variableContext.popJavaScope();
            elseBlock.addStatement(elsePart);

            JavaExpression conditionExpression = pair.getJavaExpression();

            JavaStatement ite = new IfThenElseStatement(conditionExpression, thenBlock, elseBlock);

            Block contextBlock = pair.getContextBlock();
            contextBlock.addStatement(ite);

            // We don't need to call generateReturn() at this point.  Any return statements
            // will have been generated in the sub-parts of the if-then-else.
            return contextBlock;
        }


        // Expression is a case/switch?
        Expression.Switch eswitch = e.asSwitch();
        if (eswitch != null) {
            // We don't need to call generateReturn() at this point.  Any return statements
            // will have been generated in the sub-parts of the switch.
            return generateSwitch (eswitch, variableContext);
        }

        // Expression is a data constructor field selection?
        Expression.DataConsSelection eDCSelection = e.asDataConsSelection();
        if (eDCSelection != null) {
             return generateReturn(generateDCFieldSelection(eDCSelection, Scheme.R_SCHEME, variableContext), variableContext);
        }

        // e is a let var.
        if (e.asLetNonRec() != null) {
            return generateReturn (generateLetNonRec(e.asLetNonRec(), Scheme.R_SCHEME, null, variableContext), variableContext);
        }

        if (e.asLetRec() != null) {
            return generateReturn(generateLetRec(e.asLetRec(), Scheme.R_SCHEME, variableContext), variableContext);
        }

        // e is an application?
        Expression.Appl appl = e.asAppl();
        if (appl != null) {
            JavaStatement topLevelSeq = buildTopLevelSeq (appl, false, variableContext);
            if (topLevelSeq != null) {
                return topLevelSeq;
            }
            ExpressionContextPair ecp = buildApplicationChain (appl, Scheme.R_SCHEME, variableContext);
            if (ecp != null) {
                return generateReturn(ecp, variableContext);
            } else {

                // This is a general application
                ExpressionContextPair target;
                if (appl.getE1().asDataConsSelection() != null) {
                    target = generateDCFieldSelection(appl.getE1().asDataConsSelection(), Scheme.E_SCHEME, variableContext);
                } else {
                    target = genS_C(appl.getE1(), variableContext);
                }
                ExpressionContextPair arg = genS_C(appl.getE2(), variableContext);

                JavaStatement.Block returnBlock = target.getContextBlock();
                returnBlock.addStatement(arg.getContextBlock());

                MethodInvocation mi = createInvocation(target.getJavaExpression(), APPLY, arg.getJavaExpression());
                return generateReturn (new ExpressionContextPair(mi, returnBlock), variableContext);
            }
        }

        //e is a record update
        Expression.RecordUpdate recordUpdate = e.asRecordUpdate();
        if (recordUpdate != null) {
            ExpressionContextPair ecPair = generateStrictRecordUpdate(recordUpdate, variableContext);
            return generateReturn(ecPair, variableContext);
        }

        // e is a record extension
        Expression.RecordExtension recordExtension = e.asRecordExtension();
        if (recordExtension != null) {

            ExpressionContextPair ecPair = generateStrictRecordExtension(recordExtension, variableContext);
            return generateReturn(ecPair, variableContext);
        }

        // e is a record selection
        Expression.RecordSelection recordSelection = e.asRecordSelection();
        if (recordSelection != null) {
            return generateReturn(generateStrictRecordSelection(recordSelection, true, variableContext), variableContext);
        }

        // e is a record case
        Expression.RecordCase recordCase = e.asRecordCase();
        if (recordCase != null) {
            return generateRecordCase (recordCase, variableContext);
        }

        // e is a cast
        Expression.Cast cast = e.asCast();
        if (cast != null) {
            Expression.Var varToCast = cast.getVarToCast();
            if (!variableContext.isFunctionArgument(varToCast.getName())) {
                throw new CodeGenerationException("Expression.Cast is applied to a non-argument variable.");
            }
            if (!isArgStrict(varToCast.getName()) || !isArgUnboxable(varToCast.getName())) {
                throw new CodeGenerationException("Expression.Cast is applied to a non-strict or non-primitive argument.");
            }

            JavaTypeName castType = JavaTypeName.make (getCastType(cast));

            JavaExpression returnVal = boxExpression(castType, new JavaExpression.CastExpression(castType, variableContext.getUnboxedReference(varToCast.getName())));
            return generateReturn(returnVal, variableContext);

        }

        throw new CodeGenerationException("Unrecognized expression: " + e);
View Full Code Here

            }
        }

        if (scheme == Scheme.E_SCHEME) {
            ExpressionContextPair ecp = genS_E (dcSelection.getDCValueExpr(), variableContext);
            JavaExpression target = ecp.getJavaExpression();
            boolean doCast = true;
            if (target instanceof JavaExpression.ClassInstanceCreationExpression) {
                doCast = false;
            }

            if (doCast) {
                target = new JavaExpression.CastExpression(JavaTypeNames.RTCONS, target);
            }
            MethodInvocation mi =
                new MethodInvocation.Instance (target,
                        "getFieldByIndex",
                        new JavaExpression[]{LiteralWrapper.make(new Integer(dcSelection.getDataConstructor().getOrdinal())),
                                             LiteralWrapper.make(new Integer(dcSelection.getFieldIndex())),
                                             getErrorInfo (dcSelection.getErrorInfo())},
                        new JavaTypeName[]{JavaTypeName.INT,
                                           JavaTypeName.INT,
                                           JavaTypeName.ERRORINFO},
                        JavaTypeNames.RTVALUE,
                        MethodInvocation.InvocationType.VIRTUAL);
            return new ExpressionContextPair (createInvocation(mi, EVALUATE, EXECUTION_CONTEXT_VAR), ecp.getContextBlock());
        }

        if (scheme == Scheme.R_SCHEME) {
            ExpressionContextPair ecp = genS_E (dcSelection.getDCValueExpr(), variableContext);
            JavaExpression target = ecp.getJavaExpression();
            if (!(target instanceof JavaExpression.ClassInstanceCreationExpression)) {
                target = new JavaExpression.CastExpression(JavaTypeNames.RTCONS, target);
            }
            MethodInvocation mi =
                new MethodInvocation.Instance (target,
                        "getFieldByIndex",
                        new JavaExpression[]{LiteralWrapper.make(new Integer(dcSelection.getDataConstructor().getOrdinal())),
                                             LiteralWrapper.make(new Integer(dcSelection.getFieldIndex())),
                                             getErrorInfo (dcSelection.getErrorInfo())},
                        new JavaTypeName[]{JavaTypeName.INT,
                                           JavaTypeName.INT,
                                           JavaTypeName.ERRORINFO},
                       JavaTypeNames.RTVALUE,
                       MethodInvocation.InvocationType.VIRTUAL);
            return new ExpressionContextPair (mi, ecp.getContextBlock());
        }

        // This is a DC field selection expression in a lazy scheme.  Do a lazy compilation of the
        // DC expression and then pass the resulting graph as an argument to an RTDataConsFieldSelection
        // sub class.
        ExpressionContextPair ecp = genS_C (dcSelection.getDCValueExpr(), variableContext);

        JavaTypeName fieldSelectionClassTypeName =
            CALToJavaNames.createFieldSelectionClassTypeNameFromDC(dcSelection.getDataConstructor(), module);

        JavaExpression cice =
            new ClassInstanceCreationExpression (fieldSelectionClassTypeName,
                                                 new JavaExpression[]{ecp.getJavaExpression(),
                                                                      LiteralWrapper.make(new Integer(dcSelection.getDataConstructor().getOrdinal())),
                                                                      LiteralWrapper.make(new Integer(dcSelection.getFieldIndex())),
                                                                      getErrorInfo (dcSelection.getErrorInfo())},
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.