Search in sources :

Example 71 with Xnode

use of claw.tatsu.xcodeml.xnode.common.Xnode in project claw-compiler by C2SM-RCM.

the class Range method compare.

/**
 * Compare if two indexRange nodes are identical.
 *
 * @param idx1           First indexRange node.
 * @param idx2           Second indexRange node.
 * @param withLowerBound If true, compare lower bound. If false, lower bound is
 *                       not compared.
 * @return True if the index range are identical.
 */
public static boolean compare(Xnode idx1, Xnode idx2, boolean withLowerBound) {
    if (!Xnode.isOfCode(idx1, Xcode.INDEX_RANGE) || !Xnode.isOfCode(idx2, Xcode.INDEX_RANGE)) {
        return false;
    }
    if (idx1.getBooleanAttribute(Xattr.IS_ASSUMED_SHAPE) && idx2.getBooleanAttribute(Xattr.IS_ASSUMED_SHAPE)) {
        return true;
    }
    Xnode low1 = idx1.matchSeq(Xcode.LOWER_BOUND);
    Xnode up1 = idx1.matchSeq(Xcode.UPPER_BOUND);
    Xnode low2 = idx2.matchSeq(Xcode.LOWER_BOUND);
    Xnode up2 = idx2.matchSeq(Xcode.UPPER_BOUND);
    Xnode s1 = idx1.matchSeq(Xcode.STEP);
    Xnode s2 = idx2.matchSeq(Xcode.STEP);
    if (s1 != null) {
        s1 = s1.firstChild();
    }
    if (s2 != null) {
        s2 = s2.firstChild();
    }
    if (withLowerBound) {
        return low1.compareFirstChildValues(low2) && up1.compareFirstChildValues(up2) && (s1 == null || s1.compareOptionalValues(s2));
    } else {
        return up1.compareFirstChildValues(up2) && (s1 == null || s1.compareOptionalValues(s2));
    }
}
Also used : Xnode(claw.tatsu.xcodeml.xnode.common.Xnode)

Example 72 with Xnode

use of claw.tatsu.xcodeml.xnode.common.Xnode in project claw-compiler by C2SM-RCM.

the class FfunctionDefinition method getVariables.

/**
 * Get variables declared in the function.
 *
 * @param xcodeml    Current translation unit.
 * @param parameters If true, parameters are returned.
 * @param temporary  If true, local variables are returned.
 * @param onlyArray  If true, only arrays are returned.
 * @return List of variables names.
 */
private List<String> getVariables(XcodeProgram xcodeml, boolean parameters, boolean temporary, boolean onlyArray) {
    List<String> variables = new ArrayList<>();
    List<Xnode> declarations = getDeclarationTable().values();
    for (Xnode decl : declarations) {
        if (decl.is(Xcode.VAR_DECL)) {
            Xnode name = decl.matchSeq(Xcode.NAME);
            if (!(xcodeml.getTypeTable().isBasicType(decl))) {
                // Only check basic type
                continue;
            }
            FbasicType bt = xcodeml.getTypeTable().getBasicType(decl);
            if ((parameters && isParameterVariable(bt, onlyArray)) || (temporary && isTemporaryVariable(bt, onlyArray))) {
                variables.add(name.value());
            }
        }
    }
    return variables;
}
Also used : Xnode(claw.tatsu.xcodeml.xnode.common.Xnode) ArrayList(java.util.ArrayList)

Example 73 with Xnode

use of claw.tatsu.xcodeml.xnode.common.Xnode in project claw-compiler by C2SM-RCM.

the class Pragma method splitByLength.

/**
 * Split the line by its length and add continuation symbols.
 *
 * @param pragma  Pragma statement to be splitted.
 * @param xcodeml The XcodeML on which the transformations are applied.
 * @param prefix  Prefix of the directive.
 * @throws IllegalTransformationException If the given element is not a
 *                                        FpragmaStatement.
 */
public static void splitByLength(Xnode pragma, XcodeProgram xcodeml, String prefix) throws IllegalTransformationException {
    if (!Xnode.isOfCode(pragma, Xcode.F_PRAGMA_STATEMENT)) {
        throw new IllegalTransformationException(TatsuConstant.ERROR_INCOMPATIBLE);
    }
    String allPragma = pragma.value().toLowerCase();
    if (allPragma.length() > xcodeml.context().getMaxColumns()) {
        allPragma = Pragma.dropEndingComment(allPragma);
        Xnode newlyInserted = pragma;
        List<String> splittedPragmas = Pragma.split(allPragma, xcodeml.context().getMaxColumns(), prefix);
        for (int i = 0; i < splittedPragmas.size(); ++i) {
            // Create pragma with continuation symbol unless for the last item.
            newlyInserted = createAndInsertPragma(xcodeml, newlyInserted, pragma.filename(), pragma.lineNo(), splittedPragmas.get(i), i != splittedPragmas.size() - 1);
        }
        // Delete original not splitted pragma.
        pragma.delete();
    }
}
Also used : Xnode(claw.tatsu.xcodeml.xnode.common.Xnode) IllegalTransformationException(claw.tatsu.xcodeml.exception.IllegalTransformationException)

Example 74 with Xnode

use of claw.tatsu.xcodeml.xnode.common.Xnode in project claw-compiler by C2SM-RCM.

the class Pragma method createAndInsertPragma.

/**
 * Create a new pragma node and insert it after the hook.
 *
 * @param xcodeml   Current XcodeML file unit.
 * @param hook      Hook node. New node will be inserted after this one.
 * @param filename  Filename set to the enhanced info.
 * @param lineNo    Line index specify the offset of the line number for the new
 *                  node from the original pragma node.
 * @param value     Value of the pragma node.
 * @param continued If true, continuation symbol is added at the end of the
 *                  line.
 * @return The newly created node to be able to insert after it.
 */
private static Xnode createAndInsertPragma(XcodeProgram xcodeml, Xnode hook, String filename, int lineNo, String value, boolean continued) {
    if (value == null || value.isEmpty()) {
        return null;
    }
    Xnode p = xcodeml.createNode(Xcode.F_PRAGMA_STATEMENT);
    if (filename != null && !filename.isEmpty()) {
        p.setFilename(filename);
    }
    if (lineNo > 0) {
        p.setLine(lineNo);
    }
    String prefix = xcodeml.context().getGenerator().getPrefix();
    value = value.trim().toLowerCase();
    boolean notStartWithPrefix = !value.startsWith(prefix);
    if (continued) {
        if (notStartWithPrefix) {
            p.setValue(prefix + " " + value + " " + TatsuConstant.CONTINUATION_LINE_SYMBOL);
        } else {
            p.setValue(value + " " + TatsuConstant.CONTINUATION_LINE_SYMBOL);
        }
    } else {
        if (notStartWithPrefix) {
            p.setValue(prefix + " " + value);
        } else {
            p.setValue(value);
        }
    }
    hook.insertAfter(p);
    return p;
}
Also used : Xnode(claw.tatsu.xcodeml.xnode.common.Xnode)

Example 75 with Xnode

use of claw.tatsu.xcodeml.xnode.common.Xnode in project claw-compiler by C2SM-RCM.

the class Type method duplicateWithDimension.

/**
 * Duplicates the type to update and add extra dimensions to match the base
 * type.
 *
 * @param base       Base type.
 * @param toUpdate   Type to update.
 * @param xcodemlSrc Source XcodeML unit. Contains base dimension.
 * @param xcodemlDst Destination XcodeML unit. Duplicate will be created here.
 * @param dimensions List of dimensions definitions to be used.
 * @return The new type hash generated.
 * @throws IllegalTransformationException If action is not supported.
 */
public static FbasicType duplicateWithDimension(FbasicType base, FbasicType toUpdate, XcodeML xcodemlSrc, XcodeML xcodemlDst, List<DimensionDefinition> dimensions) throws IllegalTransformationException {
    FbasicType newType = toUpdate.cloneNode();
    String type = xcodemlDst.getTypeTable().generateHash(FortranType.ARRAY);
    newType.setType(type);
    if (base.isAllAssumedShape() && toUpdate.isAllAssumedShape()) {
        int additionalDimensions = base.getDimensions() - toUpdate.getDimensions();
        for (int i = 0; i < additionalDimensions; ++i) {
            Xnode index = xcodemlDst.createEmptyAssumedShaped();
            newType.addDimension(index, 0);
        }
    } else if (base.isAllAssumedShape() && !toUpdate.isAllAssumedShape()) {
        for (DimensionDefinition dim : dimensions) {
            switch(dim.getInsertionPosition()) {
                case BEFORE:
                    // TODO control and validate the before/after
                    newType.addDimension(dim.generateIndexRange(xcodemlDst, false, false));
                    break;
                case AFTER:
                    newType.addDimension(dim.generateIndexRange(xcodemlDst, false, false), 0);
                    break;
                case IN_MIDDLE:
                    throw new IllegalTransformationException("Not supported yet. " + "Insertion in middle for duplicated array type.", 0);
            }
        }
    } else {
        newType.resetDimension();
        for (int i = 0; i < base.getDimensions(); ++i) {
            Xnode newDim = xcodemlDst.createNode(Xcode.INDEX_RANGE);
            newType.append(newDim);
            Xnode baseDim = base.getDimensions(i);
            Xnode lowerBound = baseDim.matchSeq(Xcode.LOWER_BOUND);
            Xnode upperBound = baseDim.matchSeq(Xcode.UPPER_BOUND);
            if (lowerBound != null) {
                Xnode newLowerBound = Type.duplicateBound(lowerBound, xcodemlSrc, xcodemlDst);
                newDim.append(newLowerBound);
            }
            if (upperBound != null) {
                Xnode newUpperBound = Type.duplicateBound(upperBound, xcodemlSrc, xcodemlDst);
                newDim.append(newUpperBound);
            }
            newType.addDimension(newDim);
        }
    }
    xcodemlDst.getTypeTable().add(newType);
    return newType;
}
Also used : Xnode(claw.tatsu.xcodeml.xnode.common.Xnode) IllegalTransformationException(claw.tatsu.xcodeml.exception.IllegalTransformationException) DimensionDefinition(claw.tatsu.xcodeml.abstraction.DimensionDefinition) FbasicType(claw.tatsu.xcodeml.xnode.fortran.FbasicType)

Aggregations

Xnode (claw.tatsu.xcodeml.xnode.common.Xnode)124 Context (claw.tatsu.common.Context)29 IllegalTransformationException (claw.tatsu.xcodeml.exception.IllegalTransformationException)24 Test (org.junit.Test)24 XcodeProgram (claw.tatsu.xcodeml.xnode.common.XcodeProgram)20 TestContext (helper.Utils.TestContext)20 ArrayList (java.util.ArrayList)18 FfunctionDefinition (claw.tatsu.xcodeml.xnode.fortran.FfunctionDefinition)17 Xblock (claw.tatsu.xcodeml.abstraction.Xblock)9 FunctionCall (claw.tatsu.xcodeml.abstraction.FunctionCall)8 Xid (claw.tatsu.xcodeml.xnode.common.Xid)8 FbasicType (claw.tatsu.xcodeml.xnode.fortran.FbasicType)8 HashSet (java.util.HashSet)7 PromotionInfo (claw.tatsu.xcodeml.abstraction.PromotionInfo)6 FfunctionType (claw.tatsu.xcodeml.xnode.fortran.FfunctionType)6 NestedDoStatement (claw.tatsu.xcodeml.abstraction.NestedDoStatement)5 ClawPragma (claw.wani.language.ClawPragma)5 ClawTranslator (claw.wani.x2t.translator.ClawTranslator)5 NodeList (org.w3c.dom.NodeList)5 DirectiveGenerator (claw.tatsu.directive.generator.DirectiveGenerator)4