Search in sources :

Example 1 with WidthSpec

use of org.lflang.lf.WidthSpec in project lingua-franca by lf-lang.

the class ASTUtils method getDelayInstance.

/**
 * Create a new instance delay instances using the given reactor class.
 * The supplied time value is used to override the default interval (which
 * is zero).
 * If the target supports parametric polymorphism, then a single class may
 * be used for each instantiation, in which case a non-empty string must
 * be supplied to parameterize the instance.
 * A default name ("delay") is assigned to the instantiation, but this
 * name must be overridden at the call site, where checks can be done to
 * avoid name collisions in the container in which the instantiation is
 * to be placed. Such checks (or modifications of the AST) are not
 * performed in this method in order to avoid causing concurrent
 * modification exceptions.
 * @param delayClass The class to create an instantiation for
 * @param connection The connection to create a delay instantiation foe
 * @param generic A string that denotes the appropriate type parameter,
 *  which should be null or empty if the target does not support generics.
 * @param defineWidthFromConnection If this is true and if the connection
 *  is a wide connection, then instantiate a bank of delays where the width
 *  is given by ports involved in the connection. Otherwise, the width will
 *  be  unspecified indicating a variable length.
 */
private static Instantiation getDelayInstance(Reactor delayClass, Connection connection, String generic, Boolean defineWidthFromConnection) {
    Delay delay = connection.getDelay();
    Instantiation delayInstance = factory.createInstantiation();
    delayInstance.setReactorClass(delayClass);
    if (!StringExtensions.isNullOrEmpty(generic)) {
        TypeParm typeParm = factory.createTypeParm();
        typeParm.setLiteral(generic);
        delayInstance.getTypeParms().add(typeParm);
    }
    if (hasMultipleConnections(connection)) {
        WidthSpec widthSpec = factory.createWidthSpec();
        if (defineWidthFromConnection) {
            // to delay the ports first, and then broadcast the output of the delays.
            for (VarRef port : connection.getLeftPorts()) {
                WidthTerm term = factory.createWidthTerm();
                term.setPort(EcoreUtil.copy(port));
                widthSpec.getTerms().add(term);
            }
        } else {
            widthSpec.setOfVariableLength(true);
        }
        delayInstance.setWidthSpec(widthSpec);
    }
    Assignment assignment = factory.createAssignment();
    assignment.setLhs(delayClass.getParameters().get(0));
    Value value = factory.createValue();
    if (delay.getParameter() != null) {
        value.setParameter(delay.getParameter());
    } else {
        value.setTime(delay.getTime());
    }
    assignment.getRhs().add(value);
    delayInstance.getParameters().add(assignment);
    // This has to be overridden.
    delayInstance.setName("delay");
    return delayInstance;
}
Also used : VarRef(org.lflang.lf.VarRef) Assignment(org.lflang.lf.Assignment) Value(org.lflang.lf.Value) Instantiation(org.lflang.lf.Instantiation) WidthTerm(org.lflang.lf.WidthTerm) Delay(org.lflang.lf.Delay) TypeParm(org.lflang.lf.TypeParm) WidthSpec(org.lflang.lf.WidthSpec)

Example 2 with WidthSpec

use of org.lflang.lf.WidthSpec in project lingua-franca by lf-lang.

the class PortInstance method setInitialWidth.

/**
 * Set the initial multiport width, if this is a multiport, from the widthSpec
 * in the definition. This will be set to -1 if the width cannot be determined.
 * @param errorReporter For reporting errors.
 */
private void setInitialWidth(ErrorReporter errorReporter) {
    // If this is a multiport, determine the width.
    WidthSpec widthSpec = definition.getWidthSpec();
    if (widthSpec != null) {
        if (widthSpec.isOfVariableLength()) {
            errorReporter.reportError(definition, "Variable-width multiports not supported (yet): " + definition.getName());
        } else {
            isMultiport = true;
            // Determine the initial width, if possible.
            // The width may be given by a parameter or even sum of parameters.
            width = 0;
            for (WidthTerm term : widthSpec.getTerms()) {
                Parameter parameter = term.getParameter();
                if (parameter != null) {
                    Integer parameterValue = parent.initialIntParameterValue(parameter);
                    // Only a Literal is supported.
                    if (parameterValue != null) {
                        width += parameterValue;
                    } else {
                        width = -1;
                        return;
                    }
                } else if (term.getWidth() != 0) {
                    width += term.getWidth();
                } else {
                    width = -1;
                    return;
                }
            }
        }
    }
}
Also used : Parameter(org.lflang.lf.Parameter) WidthTerm(org.lflang.lf.WidthTerm) WidthSpec(org.lflang.lf.WidthSpec)

Aggregations

WidthSpec (org.lflang.lf.WidthSpec)2 WidthTerm (org.lflang.lf.WidthTerm)2 Assignment (org.lflang.lf.Assignment)1 Delay (org.lflang.lf.Delay)1 Instantiation (org.lflang.lf.Instantiation)1 Parameter (org.lflang.lf.Parameter)1 TypeParm (org.lflang.lf.TypeParm)1 Value (org.lflang.lf.Value)1 VarRef (org.lflang.lf.VarRef)1