Package org.openquark.cal.internal.javamodel.JavaStatement

Examples of org.openquark.cal.internal.javamodel.JavaStatement.Block


        for (int i = 1; i < chain.length; ++i) {
            ecp[i] = genS_C(chain[i], variableContext);
        }

        Block newContext = ecp[0].getContextBlock();
        JavaExpression[] je = new JavaExpression [chain.length];
        je[0] = ecp[0].getJavaExpression();
        for (int i = 1; i < chain.length; ++i) {
            newContext.addStatement(ecp[i].getContextBlock());
            je[i] = ecp[i].getJavaExpression();
        }

        // If we are in a strict context we want to generate something like:
        // arg.f3L(a, b, c).evaluate($ec);
View Full Code Here


        Expression expressions[] = flattenTopLevelSeq(applChain);
        if (expressions == null) {
            return null;
        }

        Block block = new Block();

        // For all but the last expression we want to generate a Java statement
        // which will evaluate to WHNF.
        for (int i = 0, n = expressions.length - 1; i < n; ++i) {
            Expression e = expressions[i];
            JavaStatement js = makeStrictStatementFromSeqArg(e, variableContext);
            block.addStatement(js);
        }

        // For the last expression we simply generate a return expression using the
        // usual top level compilation scheme.
        block.addStatement (genS_R (expressions[expressions.length - 1], variableContext));

        return block;
    }
View Full Code Here

        // If e is a local variable which is known to have been evaluated we don't have to do
        // anything.
        if (e.asVar() != null) {
            VarInfo vi = variableContext.getVariableInfo(e.asVar().getName());
            if (vi != null && vi.isEvaluated()) {
                return new Block();
            }
        }

        ExpressionContextPair pair;

        // Since we're going to ignore the result we can save ourselves a boxing step on
        // the result.  First check to see if the expression is a primitive operation.  If
        // it is we want to call generatePrimitiveOp directly with the R scheme.  This will
        // correctly handle primitive operations and foreign functions, including foreign
        // functions that return void.
        // If the expression is not a primitive op call generateUnboxedArgument() with an
        // unbox type of null (indicating there is no desired type).  This will handle
        // things like unboxed arguments, let variables, etc.

        //we don't directly call primitive operators when doing function operator tracing.
        //This will have the effect of ensuring that they get traced when called.

        if (LECCMachineConfiguration.generateDirectPrimOpCalls() &&
            BasicOpTuple.isBasicOp(e) != null) {
            //there is an assumption here that the primitive op will return a value in weak-head normal form
            //The purpose of this optimization is to avoid an unnecessary boxing and unboxing
            pair = generatePrimitiveOp (e, false, Scheme.R_SCHEME, variableContext);
        } else {
            pair = generateUnboxedArgument(null, e, variableContext);
        }

        Block block = pair.getContextBlock();
        JavaExpression firstArg = pair.getJavaExpression();
        block.addStatement(new ExpressionStatement(forceSafeForStatement(firstArg)));

        return block;
    }
View Full Code Here

            fsRootECP = genS_E(fsRoot, variableContext);
        } else {
            fsRootECP = genS_C(fsRoot, variableContext);
        }

        Block b = fsRootECP.getContextBlock();
        JavaExpression rootExpression = fsRootECP.getJavaExpression();

        JavaExpression extraArgs[] = new JavaExpression[diff];
        for (int i = calledArity + 1; i < appChain.length; ++i) {
            ExpressionContextPair argECP = genS_C(appChain[i], variableContext);
            b.addStatement(argECP.getContextBlock());
            extraArgs[i-calledArity-1] = argECP.getJavaExpression();
        }

        // If we are in a strict context we want to generate something like:
        // root.f3L(a, b, c).evaluate($ec);
View Full Code Here

                ExpressionContextPair[] ecp = new ExpressionContextPair[chain.length];
                for (int i = 0; i < chain.length; ++i) {
                    ecp[i] = genS_C(chain[i], variableContext);
                }

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

                JavaExpression root = ecp[0].getJavaExpression();

                JavaExpression args[] = new JavaExpression[chain.length];
View Full Code Here

        FieldName fieldName = recordSelectionExpr.getFieldName();

        ExpressionContextPair recordExprContextPair = genS_C(recordExpr, variableContext);

        JavaExpression javaRecordExpr = recordExprContextPair.getJavaExpression();
        Block recordSelectionBlock = new Block();
        recordSelectionBlock.addStatement(recordExprContextPair.getContextBlock());

        JavaExpression createLazyRecordSelection;
        if (fieldName instanceof FieldName.Textual) {

            createLazyRecordSelection = new ClassInstanceCreationExpression(JavaTypeNames.RTRECORD_SELECTION_TEXTUAL_FIELD,
View Full Code Here

        Expression conditionExpr = recordCaseExpr.getConditionExpr();

        ExpressionContextPair conditionExprContextPair = genS_E(conditionExpr, variableContext);

        Block recordCaseBlock = new Block();

        JavaExpression javaConditionExpr = conditionExprContextPair.getJavaExpression();
        recordCaseBlock.addStatement(conditionExprContextPair.getContextBlock());

        //the compiler ensures that evaluating conditionExpr will result in a RTRecordValue.
        javaConditionExpr = new CastExpression(JavaTypeNames.RTRECORD_VALUE, javaConditionExpr);

        LocalVariable conditionVar = new LocalVariable("$recordCase" + nestedCaseLevel, JavaTypeNames.RTRECORD_VALUE);

        LocalVariableDeclaration conditionVarDeclaration = new LocalVariableDeclaration(conditionVar, javaConditionExpr);
        recordCaseBlock.addStatement(conditionVarDeclaration);

        //now encode the extraction of the pattern bound variables from the condition record expr.

        // Also need to push a let variable block.  This is separate from the variable scope because the two
        // do not always coincide.  The let variable block is popped by calling i_VariableScope.genS_Vars().
        variableContext.pushJavaScope();

        //FieldName -> String
        SortedMap<FieldName, String> fieldBindingVarMap = recordCaseExpr.getFieldBindingVarMap();

        String baseRecordPatternVarName = recordCaseExpr.getBaseRecordPatternVarName();
        if (baseRecordPatternVarName != null &&
            !baseRecordPatternVarName.equals(Expression.RecordCase.WILDCARD_VAR)) {

            QualifiedName qn = QualifiedName.make(currentModuleName, baseRecordPatternVarName);
            VarInfo.RecordField varInfo = variableContext.addRecordField(qn, null);
            String javaBaseRecordPatternVarName = varInfo.getJavaName();
            LocalName lazyRef = new LocalName(varInfo.getJavaName(), JavaTypeNames.RTVALUE);
            varInfo.updateLazyReference(lazyRef);
            varInfo.updateStrictReference(SCJavaDefn.createInvocation(lazyRef, SCJavaDefn.EVALUATE, SCJavaDefn.EXECUTION_CONTEXT_VAR));

            //generate the Java code:
            //(in the case of both ordinal and textual field names
            //javaBaseRecordPatternVarName = conditionVar.makeMixedRecordRetraction(new int[] {ordinalFieldName1, ..., ordinalFieldNameN},
            //  new String[] {textualFieldName1, ..., textualFieldNameN}

            LocalVariable baseRecordPatternVar = new LocalVariable(javaBaseRecordPatternVarName, JavaTypeNames.RTRECORD_VALUE);

            JavaExpression javaExtractBaseRecordExpr;

            Expression.RecordCase.FieldData fieldData = recordCaseExpr.getBindingFieldsData();
            //todoBI there could be some more optimizations here to handle the cases
            //a. where the ordinal names are in tuple form, then only the tuple size needs to be passed.
            //b. where only a single ordinal or single textual field is being retracted, then we don't
            //   need to form the array.
            //Note however, that if the record pattern var is not used in the expression on the right hand side
            //of the ->, then it will not be created (earlier analysis replaces it by a _), so in fact this
            //code is not called that often anyways.
            int[] retractedOrdinalFields = fieldData.getOrdinalNames();
            String[] retractedTextualFields = fieldData.getTextualNames();

            if (retractedOrdinalFields.length > 0) {

                if (retractedTextualFields.length > 0) {

                    if (fieldData.hasTupleOrdinalPart()) {

                        javaExtractBaseRecordExpr = new MethodInvocation.Instance(
                                conditionVar,
                            "makeTupleMixedRecordRetraction",
                            new JavaExpression[] {
                                LiteralWrapper.make(Integer.valueOf(retractedOrdinalFields.length)),
                                createTextualNamesArray(retractedTextualFields)},
                            new JavaTypeName[] {JavaTypeName.INT, JavaTypeName.STRING_ARRAY},
                            JavaTypeNames.RTRECORD_VALUE,
                            InvocationType.VIRTUAL);
                    } else {

                        javaExtractBaseRecordExpr = new MethodInvocation.Instance(
                                conditionVar,
                            "makeMixedRecordRetraction",
                            new JavaExpression[] {
                                createOrdinalNamesArray(retractedOrdinalFields),
                                createTextualNamesArray(retractedTextualFields)},
                            new JavaTypeName[] {JavaTypeName.INT_ARRAY, JavaTypeName.STRING_ARRAY},
                            JavaTypeNames.RTRECORD_VALUE,
                            InvocationType.VIRTUAL);
                    }
                } else {

                    if (fieldData.hasTupleOrdinalPart()) {

                        javaExtractBaseRecordExpr = new MethodInvocation.Instance(
                                conditionVar,
                            "makeTupleRecordRetraction",
                            new JavaExpression[] {
                                LiteralWrapper.make(Integer.valueOf(retractedOrdinalFields.length))},
                            new JavaTypeName[] {JavaTypeName.INT},
                            JavaTypeNames.RTRECORD_VALUE,
                            InvocationType.VIRTUAL);
                    } else {

                        javaExtractBaseRecordExpr = new MethodInvocation.Instance(
                                conditionVar,
                            "makeOrdinalRecordRetraction",
                            new JavaExpression[] {
                                createOrdinalNamesArray(retractedOrdinalFields)},
                            new JavaTypeName[] {JavaTypeName.INT_ARRAY},
                            JavaTypeNames.RTRECORD_VALUE,
                            InvocationType.VIRTUAL);
                    }
                }

            } else if (retractedTextualFields.length > 0) {

                    javaExtractBaseRecordExpr = new MethodInvocation.Instance(
                            conditionVar,
                        "makeTextualRecordRetraction",
                        new JavaExpression[] {
                            createTextualNamesArray(retractedTextualFields)},
                        new JavaTypeName[] {JavaTypeName.STRING_ARRAY},
                        JavaTypeNames.RTRECORD_VALUE,
                        InvocationType.VIRTUAL);

            } else {
                javaExtractBaseRecordExpr = conditionVar;
            }

            LocalVariableDeclaration extractBaseRecordDeclaration =
                new LocalVariableDeclaration(baseRecordPatternVar, javaExtractBaseRecordExpr);
            recordCaseBlock.addStatement(extractBaseRecordDeclaration);
        }

        for (final Map.Entry<FieldName, String> entry : fieldBindingVarMap.entrySet()) {

            FieldName fieldName = entry.getKey();
            String bindingVarName = entry.getValue();

            //ignore anonymous pattern variables. These are guaranteed not to be used
            //by the result expression, and so don't need to be extracted from the condition record.
            if (!bindingVarName.equals(Expression.RecordCase.WILDCARD_VAR)) {

                QualifiedName qn = QualifiedName.make(currentModuleName, bindingVarName);
                VarInfo.RecordField varInfo = variableContext.addRecordField(qn, null);
                String javaBindingVarName = varInfo.getJavaName();
                LocalName lazyRef = new LocalName(varInfo.getJavaName(), JavaTypeNames.RTVALUE);
                varInfo.updateLazyReference(lazyRef);
                varInfo.updateStrictReference(SCJavaDefn.createInvocation(lazyRef, SCJavaDefn.EVALUATE, SCJavaDefn.EXECUTION_CONTEXT_VAR));

                LocalVariable bindingVar = new LocalVariable(javaBindingVarName, JavaTypeNames.RTVALUE);

                JavaExpression javaExtractValueExpr;
                if (fieldName instanceof FieldName.Textual) {
                    //javaBindingVarName = $recordCase.getTextualFieldValue(fieldName);
                    javaExtractValueExpr = new MethodInvocation.Instance(conditionVar, "getTextualFieldValue",
                        LiteralWrapper.make(fieldName.getCalSourceForm()), JavaTypeName.STRING,
                        JavaTypeNames.RTVALUE, InvocationType.VIRTUAL);
                } else {
                    int ordinal = ((FieldName.Ordinal)fieldName).getOrdinal();
                    javaExtractValueExpr = new MethodInvocation.Instance(conditionVar, "getOrdinalFieldValue",
                        LiteralWrapper.make(Integer.valueOf(ordinal)), JavaTypeName.INT,
                        JavaTypeNames.RTVALUE, InvocationType.VIRTUAL);
                }

                LocalVariableDeclaration extractValueDeclaration = new LocalVariableDeclaration(bindingVar, javaExtractValueExpr);
                recordCaseBlock.addStatement(extractValueDeclaration);
            }
        }


        //encode the result expression in the context of the extended variable scope.
        Expression resultExpr = recordCaseExpr.getResultExpr();
        JavaStatement resultJavaStatement = genS_R(resultExpr, variableContext);

        // Generate any let variables in this block and add them to the recordCaseBlock.
        recordCaseBlock.addStatement(variableContext.popJavaScope());

        // Add the body of the record case.
        recordCaseBlock.addStatement(resultJavaStatement);

        return recordCaseBlock;
    }
View Full Code Here

     */
    private ExpressionContextPair returnTypeToCal(Class<?> argClass, JavaExpression definition) {

        // Variables from which to construct the ExpressionContextPair
        JavaExpression javaExpression = null;
        Block context = new Block();

        if (argClass == void.class) {
            // hopefully definition will be a method invocation, rather than object creation or a field..
            context.addStatement(new ExpressionStatement(definition));

            if (LECCMachineConfiguration.TREAT_ENUMS_AS_INTS) {
                javaExpression = createMakeKernelIntInvocation(LiteralWrapper.make (Integer.valueOf(0)));
            } else {
                JavaTypeName calUnitClassName = JavaTypeName.make(CALToJavaNames.createFullPackageName(CALToJavaNames.createPackageNameFromModule(CAL_Prelude.MODULE_NAME)) + ".TYPE_Unit$CAL_Unit", false);
View Full Code Here

        if (addEC) {
            constructorArgTypes[dc.getArity()] = JavaTypeNames.RTEXECUTION_CONTEXT;
        }

        JavaExpression listCreation = null;
        Block contextBlock = null;
        boolean moreListElements = true;
        do {
            moreListElements = false;
            ExpressionContextPair leftArg = genS_C(constructorOpExpressions.getArgument(0), variableContext);
            if (contextBlock == null) {
                contextBlock = leftArg.getContextBlock();
            } else {
                contextBlock.addStatement(leftArg.getContextBlock());
            }

            constructorArgs = new JavaExpression [nArgs];
            if (addEC) {
                constructorArgs[dc.getArity()] = EXECUTION_CONTEXT_VAR;
            }

            constructorArgs[0] = leftArg.getJavaExpression();

            ClassInstanceCreationExpression cc =
                new ClassInstanceCreationExpression (dcTypeName, constructorArgs, constructorArgTypes);
            if (listCreation == null) {
                listCreation = cc;
            }

            if (lastConstructorArgs != null) {
                lastConstructorArgs[1] = cc;
            }

            lastConstructorArgs = constructorArgs;

            // The second argument will always be a data constructor, either cons or Nil.
            ConstructorOpTuple constructorOpExpressions2 = ConstructorOpTuple.isConstructorOp(constructorOpExpressions.getArgument(1), false);
            if (constructorOpExpressions2 != null && constructorOpExpressions2.getDataConstructor().getName().equals(CAL_Prelude.DataConstructors.Cons)) {
                constructorOpExpressions = constructorOpExpressions2;
                moreListElements = true;
            }
        } while (moreListElements);

        ExpressionContextPair terminator = genS_C(constructorOpExpressions.getArgument(1), variableContext);
        contextBlock.addStatement(terminator.getContextBlock());
        lastConstructorArgs[1] = terminator.getJavaExpression();

        return new ExpressionContextPair (listCreation, contextBlock);
    }
View Full Code Here

        }

        int arity = dc.getArity();

        JavaTypeName dcTypeName = CALToJavaNames.createTypeNameFromDC(dc, module);
        Block argContext = new Block();

        boolean[] fieldStrictness = new boolean [dc.getArity()];
        boolean dcHasStrictFields = false;
        if (LECCMachineConfiguration.IGNORE_STRICTNESS_ANNOTATIONS) {
            Arrays.fill(fieldStrictness, false);
        } else {
            for (int i = 0; i < dc.getArity(); ++i) {
                fieldStrictness[i] = dc.isArgStrict(i);
                if (fieldStrictness[i]) {
                    dcHasStrictFields = true;
                }
            }
        }

        // If there are no strict arguments we can simply create an instance of the DC class.
        // The simplest way to do this is to treat this DC application as if it were in a strict context.
        if (!dcHasStrictFields) {
            scheme = Scheme.E_SCHEME;
        } else {
            // If all strict arguments are already evaluated, or we consider them safe to evaluate (i.e. cheap and
            // with no side effects) we can treat this as strict.
            boolean allOK = true;
            for (int i = 0; i < dc.getArity(); ++i) {
                if (dc.getArgStrictness()[i] && !canIgnoreLaziness(constructorOpExpressions.getArgument(i), variableContext)) {
                    allOK = false;
                    break;
                }
            }

            if (allOK) {
                scheme = Scheme.E_SCHEME;
            }
        }

        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);
                }
            } else {
                // This is a fully saturated DC application in a strict context.  Create a
                // new DC class instance.

                // First generate the java expressions for the members.
                ExpressionContextPair[] ecp = new ExpressionContextPair[dc.getArity()];
                TypeExpr[] fieldTypes = SCJavaDefn.getFieldTypesForDC(dc);

                for (int i = 0; i < dc.getArity(); ++i) {
                    if (fieldStrictness[i]) {
                        if (SCJavaDefn.canTypeBeUnboxed(fieldTypes[i])) {
                            ecp[i] = generateUnboxedArgument(typeExprToTypeName(fieldTypes[i]), constructorOpExpressions.getArgument(i), variableContext);
                        } else {
                            ecp[i] = genS_E(constructorOpExpressions.getArgument(i), variableContext);
                        }
                    } else {
                        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());
                }

                int nArgs = dc.getArity();
                boolean addEC = LECCMachineConfiguration.passExecContextToDataConstructors();
                if (addEC) {
View Full Code Here

TOP

Related Classes of org.openquark.cal.internal.javamodel.JavaStatement.Block

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.