Search in sources :

Example 66 with Xnode

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

the class Xmod method updateSignature.

/**
 * Update the function signature in the module file to reflects local changes.
 *
 * @param moduleName    Xmod name to update.
 * @param xcodeml       Current XcodeML file unit.
 * @param fctDef        Function definition that has been changed.
 * @param fctType       Function type that has been changed.
 * @param importFctType If true, import the functionType.
 * @throws IllegalTransformationException If the module file or the function
 *                                        cannot be located
 */
public static void updateSignature(String moduleName, XcodeProgram xcodeml, FfunctionDefinition fctDef, FfunctionType fctType, boolean importFctType) throws IllegalTransformationException {
    final Context context = xcodeml.context();
    FortranModule mod;
    if (context.getModuleCache().isModuleLoaded(moduleName)) {
        mod = context.getModuleCache().get(moduleName);
    } else {
        mod = fctDef.findContainingXmod(context);
        if (mod == null) {
            throw new IllegalTransformationException("Unable to locate module file for: " + moduleName);
        }
        context.getModuleCache().add(moduleName, mod);
    }
    FfunctionType fctTypeMod;
    if (importFctType) {
        // TODO should be part of XcodeML
        Xnode importedNode = mod.importNode(fctType);
        mod.getTypeTable().append(importedNode);
        FfunctionType importedFctType = new FfunctionType(importedNode);
        Xid importedFctTypeId = mod.createId(importedFctType.getType(), XstorageClass.F_FUNC, fctDef.getName());
        mod.getIdentifiers().add(importedFctTypeId);
        // check if params need to be imported as well
        if (!importedFctType.getParameters().isEmpty()) {
            for (Xnode param : importedFctType.getParameters()) {
                mod.importType(xcodeml, param.getType());
            }
        }
        return;
    } else {
        fctTypeMod = mod.findFunctionType(fctDef.getName());
    }
    if (fctTypeMod == null) {
        /*
             * Workaround for a bug in OMNI Compiler. Look at test case claw/abstraction12.
             * In this test case, the XcodeML/F intermediate representation for the function
             * call points to a FfunctionType element with no parameters. Thus, we have to
             * matchSeq the correct FfunctionType for the same function/subroutine with the
             * same name in the module symbol table.
             */
        String errorMsg = "Unable to locate fct " + fctDef.getName() + " in module " + moduleName;
        /*
             * If not, try to matchSeq the correct FfunctionType in the module definitions
             */
        fctTypeMod = mod.findFunctionType(fctDef.getName());
        if (fctTypeMod == null) {
            throw new IllegalTransformationException(errorMsg);
        }
    }
    FbasicType modIntTypeIntentIn = mod.createBasicType(FortranType.INTEGER, Intent.IN);
    mod.getTypeTable().add(modIntTypeIntentIn);
    List<Xnode> paramsLocal = fctType.getParameters();
    List<Xnode> paramsMod = fctTypeMod.getParameters();
    if (paramsLocal.size() < paramsMod.size()) {
        throw new IllegalTransformationException("Local function has more parameters than module counterpart.");
    }
    for (int i = 0; i < paramsLocal.size(); ++i) {
        Xnode pLocal = paramsLocal.get(i);
        // Number of parameters in the module function as been
        if (pLocal.getBooleanAttribute(Xattr.IS_INSERTED)) {
            // new parameter
            Xnode param = mod.createAndAddParamIfNotExists(pLocal.value(), modIntTypeIntentIn.getType(), fctTypeMod);
            if (param != null) {
                param.setBooleanAttribute(Xattr.IS_INSERTED, true);
            }
        } else {
            Xnode pMod = paramsMod.get(i);
            String localType = pLocal.getType();
            String modType = pMod.getType();
            if (!localType.equals(modType)) {
                // Param has been updated so have to replicate the change to mod file
                FbasicType lType = xcodeml.getTypeTable().getBasicType(pLocal);
                FbasicType crtType = mod.getTypeTable().getBasicType(pMod);
                if (pLocal.hasAttribute(Xattr.PROMOTION_INFO)) {
                    PromotionInfo promotionInfo = new PromotionInfo();
                    promotionInfo.readDimensionsFromString(pLocal.getAttribute(Xattr.PROMOTION_INFO));
                    if (lType.isArray()) {
                        FbasicType newType = Type.duplicateWithDimension(lType, crtType, xcodeml, mod, promotionInfo.getDimensions());
                        pMod.setType(newType);
                    }
                }
            }
            // Copy the promotion information
            pLocal.copyAttribute(pMod, Xattr.PROMOTION_INFO);
        }
    }
    // Sync attribute between local fct type and module fct type.
    for (Xattr attr : SYNCABLE_ATTR) {
        fctType.syncBooleanAttribute(fctTypeMod, attr);
    }
}
Also used : Context(claw.tatsu.common.Context) Xnode(claw.tatsu.xcodeml.xnode.common.Xnode) FortranModule(claw.tatsu.xcodeml.xnode.fortran.FortranModule) Xid(claw.tatsu.xcodeml.xnode.common.Xid) FfunctionType(claw.tatsu.xcodeml.xnode.fortran.FfunctionType) IllegalTransformationException(claw.tatsu.xcodeml.exception.IllegalTransformationException) PromotionInfo(claw.tatsu.xcodeml.abstraction.PromotionInfo) Xattr(claw.tatsu.xcodeml.xnode.common.Xattr) FbasicType(claw.tatsu.xcodeml.xnode.fortran.FbasicType)

Example 67 with Xnode

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

the class Loop method swapIterationRange.

/**
 * Swap the iteration range information of two do statement.
 *
 * @param e1 First do statement.
 * @param e2 Second do statement.
 * @throws IllegalTransformationException if necessary elements are missing to
 *                                        apply the transformation.
 */
private static void swapIterationRange(Xnode e1, Xnode e2) throws IllegalTransformationException {
    // The two nodes must be do statement
    if (!Xnode.isOfCode(e1, Xcode.F_DO_STATEMENT) || !Xnode.isOfCode(e2, Xcode.F_DO_STATEMENT)) {
        throw new IllegalTransformationException("Only two do statement can be " + "swap iteration ranges.");
    }
    Xnode inductionVar1 = e1.matchDirectDescendant(Xcode.VAR);
    Xnode inductionVar2 = e2.matchDirectDescendant(Xcode.VAR);
    Xnode indexRange1 = e1.matchDirectDescendant(Xcode.INDEX_RANGE);
    Xnode indexRange2 = e2.matchDirectDescendant(Xcode.INDEX_RANGE);
    if (inductionVar1 == null || inductionVar2 == null || indexRange1 == null || indexRange2 == null) {
        throw new IllegalTransformationException("Induction variable or index " + "range missing.");
    }
    Xnode low1 = indexRange1.matchSeq(Xcode.LOWER_BOUND).child(0);
    Xnode up1 = indexRange1.matchSeq(Xcode.UPPER_BOUND).child(0);
    Xnode s1 = indexRange1.matchSeq(Xcode.STEP).child(0);
    Xnode low2 = indexRange2.matchSeq(Xcode.LOWER_BOUND).child(0);
    Xnode up2 = indexRange2.matchSeq(Xcode.UPPER_BOUND).child(0);
    Xnode s2 = indexRange2.matchSeq(Xcode.STEP).child(0);
    // Set the range of loop2 to loop1
    inductionVar2.insertAfter(inductionVar1.cloneNode());
    low2.insertAfter(low1.cloneNode());
    up2.insertAfter(up1.cloneNode());
    s2.insertAfter(s1.cloneNode());
    inductionVar1.insertAfter(inductionVar2.cloneNode());
    low1.insertAfter(low2.cloneNode());
    up1.insertAfter(up2.cloneNode());
    s1.insertAfter(s2.cloneNode());
    inductionVar1.delete();
    inductionVar2.delete();
    low1.delete();
    up1.delete();
    s1.delete();
    low2.delete();
    up2.delete();
    s2.delete();
}
Also used : Xnode(claw.tatsu.xcodeml.xnode.common.Xnode) IllegalTransformationException(claw.tatsu.xcodeml.exception.IllegalTransformationException)

Example 68 with Xnode

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

the class Loop method createDoStmtOverAssumedShapeArray.

/**
 * Create a do statement to iterate over an array from 1 to size.
 *
 * @param fbt          FbasicType of the array.
 * @param arrayName    Array name.
 * @param inductionVar Identifier of a the induction variable.
 * @param dimId        Dimension on which size is called.
 * @param xcodeml      Current translation unit.
 * @return Newly created node.
 */
public static Xnode createDoStmtOverAssumedShapeArray(FbasicType fbt, String arrayName, String inductionVar, int dimId, XcodeML xcodeml) {
    // Induction variable
    Xnode induction = xcodeml.createVar(FortranType.INTEGER, inductionVar, Xscope.LOCAL);
    // Lower bound
    Xnode lb = xcodeml.createNode(Xcode.LOWER_BOUND);
    lb.append(xcodeml.createIntConstant(1));
    // upper bound
    Xnode up = xcodeml.createNode(Xcode.UPPER_BOUND);
    FunctionCall sizeCall = xcodeml.createIntrinsicFctCall(FortranType.INTEGER, Xintrinsic.SIZE);
    sizeCall.addArguments(xcodeml.createVar(fbt.getType(), arrayName, Xscope.LOCAL));
    sizeCall.addArguments(xcodeml.createIntConstant(dimId));
    up.append(sizeCall);
    // step
    Xnode step = xcodeml.createNode(Xcode.STEP);
    step.append(xcodeml.createIntConstant(1));
    Xnode range = xcodeml.createNode(Xcode.INDEX_RANGE);
    range.append(lb);
    range.append(up);
    range.append(step);
    return xcodeml.createDoStmt(induction, range);
}
Also used : Xnode(claw.tatsu.xcodeml.xnode.common.Xnode) FunctionCall(claw.tatsu.xcodeml.abstraction.FunctionCall)

Example 69 with Xnode

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

the class Loop method hoist.

/**
 * Perform a loop hoisting on the given nested do statements.
 *
 * @param hoistedGroups List of groups that will be hoisted.
 * @param start         Starting point of the hoisted loop.
 * @param end           Ending point of the hoisted loop.
 * @param xcodeml       Current XcodeML translation unit for node creation.
 * @return Hoisted nested do statement group.
 * @throws IllegalTransformationException If underlying methods throw exception.
 */
public static HoistedNestedDoStatement hoist(List<HoistedNestedDoStatement> hoistedGroups, Xnode start, Xnode end, XcodeML xcodeml) throws IllegalTransformationException {
    // Perform IF extraction and IF creation for lower-bound
    for (HoistedNestedDoStatement g : hoistedGroups) {
        if (g.needIfStatement()) {
            createIfStatementForLowerBound(xcodeml, g);
        }
        Loop.extractBody(g.getInnerStatement(), g.getOuterStatement());
        g.getOuterStatement().delete();
    }
    // Do the hoisting
    HoistedNestedDoStatement hoisted = hoistedGroups.get(0).cloneNestedGroup();
    hoisted.getInnerStatement().body().delete();
    Xnode newBody = xcodeml.createNode(Xcode.BODY);
    hoisted.getInnerStatement().append(newBody);
    Body.shiftIn(start, end, newBody, false);
    start.insertAfter(hoisted.getOuterStatement());
    return hoisted;
}
Also used : Xnode(claw.tatsu.xcodeml.xnode.common.Xnode) HoistedNestedDoStatement(claw.tatsu.xcodeml.abstraction.HoistedNestedDoStatement)

Example 70 with Xnode

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

the class Loop method extractBody.

/**
 * Extract the body of a do statement and place it after the reference node.
 *
 * @param loop The do statement containing the body to be extracted.
 * @param ref  Element after which statement are shifted.
 * @throws IllegalTransformationException If node passed as arguments are
 *                                        incompatible with the transformation.
 */
private static void extractBody(Xnode loop, Xnode ref) throws IllegalTransformationException {
    if (ref == null || !Xnode.isOfCode(loop, Xcode.F_DO_STATEMENT)) {
        throw new IllegalTransformationException(String.format("%s for Loop.extractBody. opcode: %s", TatsuConstant.ERROR_INCOMPATIBLE, loop == null ? "null" : loop.opcode()));
    }
    Xnode body = loop.body();
    if (body == null) {
        return;
    }
    Xnode refNode = ref;
    for (Xnode child : body.children()) {
        refNode.insertAfter(child);
        refNode = child;
    }
}
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