Search in sources :

Example 1 with FortranModule

use of claw.tatsu.xcodeml.xnode.fortran.FortranModule in project claw-compiler by C2SM-RCM.

the class ModuleCache method write.

/**
 * Write all modules in the cache to files.
 *
 * @param ident Number of spaces used to indent the XML file.
 * @throws IllegalTransformationException If module cannot be written.
 * @throws IOException
 */
public void write(Context context, int ident) throws IllegalTransformationException, IOException {
    String suffix = Xmod.getSuffix(context);
    for (Map.Entry<String, FortranModule> pair : _moduleCache.entrySet()) {
        FortranModule module = pair.getValue();
        String newModuleName = module.getPath() + module.getName() + suffix;
        try (OutputStream out = Files.newOutputStream(Paths.get(newModuleName))) {
            module.write(out, ident);
        }
    }
}
Also used : FortranModule(claw.tatsu.xcodeml.xnode.fortran.FortranModule) OutputStream(java.io.OutputStream) Map(java.util.Map) HashMap(java.util.HashMap)

Example 2 with FortranModule

use of claw.tatsu.xcodeml.xnode.fortran.FortranModule 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)

Aggregations

FortranModule (claw.tatsu.xcodeml.xnode.fortran.FortranModule)2 Context (claw.tatsu.common.Context)1 PromotionInfo (claw.tatsu.xcodeml.abstraction.PromotionInfo)1 IllegalTransformationException (claw.tatsu.xcodeml.exception.IllegalTransformationException)1 Xattr (claw.tatsu.xcodeml.xnode.common.Xattr)1 Xid (claw.tatsu.xcodeml.xnode.common.Xid)1 Xnode (claw.tatsu.xcodeml.xnode.common.Xnode)1 FbasicType (claw.tatsu.xcodeml.xnode.fortran.FbasicType)1 FfunctionType (claw.tatsu.xcodeml.xnode.fortran.FfunctionType)1 OutputStream (java.io.OutputStream)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1