Search in sources :

Example 56 with Xnode

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

the class Directive method findParallelRegionEnd.

/**
 * Skip elements in epilogue and find the last element that will be included in
 * the parallel region.
 *
 * @param functionDefinition Function definition in which body checked.
 * @param from               Optional element to start from. If null, starts
 *                           from last element in function's body.
 * @return Last element for the parallel region.
 */
public static Xnode findParallelRegionEnd(Context context, Xnode functionDefinition, Xnode from) {
    DirectiveGenerator dg = context.getGenerator();
    if (!Xnode.isOfCode(functionDefinition, Xcode.F_FUNCTION_DEFINITION)) {
        return null;
    }
    Xnode last = functionDefinition.body().lastChild();
    if (last == null) {
        return null;
    }
    if (from != null) {
        // Start from given element
        last = from;
        if (last.is(Xcode.F_CONTAINS_STATEMENT)) {
            last = last.prevSibling();
        }
    }
    if (dg.getSkippedStatementsInEpilogue().isEmpty() || !last.matchAll(Xcode.F_ASSIGN_STATEMENT).isEmpty()) {
        return last;
    } else {
        while (last.prevSibling() != null && dg.getSkippedStatementsInEpilogue().contains(last.opcode())) {
            if (last.hasBody() || last.is(Xcode.F_IF_STATEMENT)) {
                List<Xnode> children = (last.hasBody()) ? last.body().children() : last.matchDirectDescendant(Xcode.THEN).body().children();
                for (Xnode child : children) {
                    if (!dg.getSkippedStatementsInEpilogue().contains(child.opcode())) {
                        return last;
                    }
                }
            }
            last = last.prevSibling();
        }
    }
    return last;
}
Also used : Xnode(claw.tatsu.xcodeml.xnode.common.Xnode) DirectiveGenerator(claw.tatsu.directive.generator.DirectiveGenerator)

Example 57 with Xnode

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

the class Body method append.

/**
 * Append the slave body to the master body.
 *
 * @param masterBody Master body node.
 * @param slaveBody  Slave body bode.
 * @throws IllegalTransformationException If given nodes are null or not body
 *                                        nodes.
 */
public static void append(Xnode masterBody, Xnode slaveBody) throws IllegalTransformationException {
    if (!Xnode.isOfCode(masterBody, Xcode.BODY) || !Xnode.isOfCode(slaveBody, Xcode.BODY)) {
        throw new IllegalTransformationException(String.format("%s for Body.append. opcode: %s - %s", TatsuConstant.ERROR_INCOMPATIBLE, masterBody == null ? "null" : masterBody.opcode(), slaveBody == null ? "null" : slaveBody.opcode()));
    }
    // Move all nodes to master body
    Xnode crtNode = slaveBody.firstChild();
    while (crtNode != null) {
        Xnode nextSibling = crtNode.nextSibling();
        masterBody.append(crtNode);
        crtNode = nextSibling;
    }
}
Also used : Xnode(claw.tatsu.xcodeml.xnode.common.Xnode) IllegalTransformationException(claw.tatsu.xcodeml.exception.IllegalTransformationException)

Example 58 with Xnode

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

the class Pragma method findPrevious.

/**
 * Find a pragma element in the previous nodes containing a given keyword.
 *
 * @param from    Element to start from.
 * @param keyword Keyword to be found in the pragma.
 * @return The pragma if found. Null otherwise.
 */
public static Xnode findPrevious(Xnode from, String keyword) {
    if (from == null || from.element() == null) {
        return null;
    }
    Xnode prev = from.prevSibling();
    Xnode parent = from;
    do {
        while (prev != null) {
            if (prev.is(Xcode.F_PRAGMA_STATEMENT) && prev.value().toLowerCase().contains(keyword.toLowerCase())) {
                return prev;
            }
            prev = prev.prevSibling();
        }
        parent = parent.ancestor();
        prev = parent;
    } while (parent != null);
    return null;
}
Also used : Xnode(claw.tatsu.xcodeml.xnode.common.Xnode)

Example 59 with Xnode

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

the class Pragma method moveInExecution.

/**
 * If the first pragma statement is located as the first statement of the
 * execution block, the OMNI Compiler front-end places it with the declaration
 * block. If this doesn't make sense for a specific pragma, this method will
 * move it to the execution block.
 *
 * @param pragma The pragma to be moved.
 */
public static void moveInExecution(Xnode pragma) {
    if (!Xnode.isOfCode(pragma, Xcode.F_PRAGMA_STATEMENT)) {
        return;
    }
    if (Xnode.isOfCode(pragma.ancestor(), Xcode.DECLARATIONS)) {
        FfunctionDefinition fdef = pragma.findParentFunction();
        if (fdef != null) {
            if (Xnode.isOfCode(fdef.body().firstChild(), Xcode.F_PRAGMA_STATEMENT)) {
                Xnode hook = null;
                Xnode crtNode = fdef.body().firstChild();
                while (Xnode.isOfCode(crtNode, Xcode.F_PRAGMA_STATEMENT) && pragma.lineNo() > crtNode.lineNo()) {
                    hook = crtNode;
                    crtNode = crtNode.nextSibling();
                }
                if (hook != null) {
                    hook.insertAfter(pragma);
                } else {
                    fdef.body().append(pragma);
                }
            } else {
                fdef.body().insert(pragma);
            }
        }
    }
}
Also used : Xnode(claw.tatsu.xcodeml.xnode.common.Xnode) FfunctionDefinition(claw.tatsu.xcodeml.xnode.fortran.FfunctionDefinition)

Example 60 with Xnode

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

the class Pragma method splitByCont.

/**
 * Split the line by its previous continuation mark.
 *
 * @param pragma  Pragma node.
 * @param prefix  Pragma prefix.
 * @param xcodeml Current XcodeML translation unit.
 * @throws IllegalTransformationException If the given element is not a
 *                                        FpragmaStatement.
 */
public static void splitByCont(Xnode pragma, String prefix, XcodeProgram xcodeml) throws IllegalTransformationException {
    if (!Xnode.isOfCode(pragma, Xcode.F_PRAGMA_STATEMENT)) {
        throw new IllegalTransformationException(TatsuConstant.ERROR_INCOMPATIBLE);
    }
    String allPragma = pragma.value();
    int lineIndex = pragma.lineNo();
    String splitter = prefix.trim();
    if (allPragma.contains(prefix.trim() + TatsuConstant.CONTINUATION_LINE_SYMBOL)) {
        splitter = prefix.trim() + TatsuConstant.CONTINUATION_LINE_SYMBOL;
    }
    Xnode newlyInserted = pragma;
    String[] lines = allPragma.split(splitter);
    for (int i = 0; i < lines.length - 1; ++i) {
        if (!lines[i].isEmpty()) {
            newlyInserted = createAndInsertPragma(xcodeml, newlyInserted, pragma.filename(), lineIndex, lines[i], true);
            ++lineIndex;
        }
    }
    createAndInsertPragma(xcodeml, newlyInserted, pragma.filename(), lineIndex, lines[lines.length - 1], false);
    pragma.delete();
}
Also used : Xnode(claw.tatsu.xcodeml.xnode.common.Xnode) IllegalTransformationException(claw.tatsu.xcodeml.exception.IllegalTransformationException)

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