/**
* <copyright>
* Copyright (c) 2009-2012, IETR/INSA of Rennes
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* * Neither the name of the IETR/INSA of Rennes nor the names of its
* contributors may be used to endorse or promote products derived from this
* software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY
* WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
* </copyright>
*/
package net.sf.orcc.ir.impl;
import java.util.List;
import java.util.Map;
import net.sf.orcc.graph.Edge;
import net.sf.orcc.graph.Vertex;
import net.sf.orcc.graph.impl.GraphImpl;
import net.sf.orcc.graph.visit.DominatorComputer;
import net.sf.orcc.ir.Cfg;
import net.sf.orcc.ir.CfgNode;
import net.sf.orcc.ir.IrPackage;
import org.eclipse.emf.common.notify.Notification;
import org.eclipse.emf.common.util.EList;
import org.eclipse.emf.ecore.EClass;
import org.eclipse.emf.ecore.InternalEObject;
import org.eclipse.emf.ecore.impl.ENotificationImpl;
/**
* <!-- begin-user-doc --> An implementation of the model object '
* <em><b>Cfg</b></em>'. <!-- end-user-doc -->
* <p>
* The following features are implemented:
* <ul>
* <li>{@link net.sf.orcc.ir.impl.CfgImpl#getEntry <em>Entry</em>}</li>
* <li>{@link net.sf.orcc.ir.impl.CfgImpl#getExit <em>Exit</em>}</li>
* </ul>
* </p>
*
* @generated
*/
public class CfgImpl extends GraphImpl implements Cfg {
private Map<Vertex, Vertex> doms;
private Map<Vertex, Vertex> idoms;
/**
* The cached value of the '{@link #getEntry() <em>Entry</em>}' reference.
* <!-- begin-user-doc --> <!-- end-user-doc -->
* @see #getEntry()
* @generated
* @ordered
*/
protected CfgNode entry;
/**
* The cached value of the '{@link #getExit() <em>Exit</em>}' reference.
* <!-- begin-user-doc --> <!-- end-user-doc -->
* @see #getExit()
* @generated
* @ordered
*/
protected CfgNode exit;
/**
* <!-- begin-user-doc --> <!-- end-user-doc -->
* @generated
*/
protected CfgImpl() {
super();
}
/**
* <!-- begin-user-doc --> <!-- end-user-doc -->
* @generated
*/
public CfgNode basicGetEntry() {
return entry;
}
/**
* <!-- begin-user-doc --> <!-- end-user-doc -->
* @generated
*/
public CfgNode basicGetExit() {
return exit;
}
@Override
public void computeDominance() {
idoms = new DominatorComputer(this, exit, true).computeDominance();
// compute dominators last so we get the usual post-order numbering
// easier to debug
doms = new DominatorComputer(this, entry, false).computeDominance();
}
/**
* <!-- begin-user-doc --> <!-- end-user-doc -->
* @generated
*/
@Override
public Object eGet(int featureID, boolean resolve, boolean coreType) {
switch (featureID) {
case IrPackage.CFG__ENTRY:
if (resolve)
return getEntry();
return basicGetEntry();
case IrPackage.CFG__EXIT:
if (resolve)
return getExit();
return basicGetExit();
}
return super.eGet(featureID, resolve, coreType);
}
/**
* <!-- begin-user-doc --> <!-- end-user-doc -->
* @generated
*/
@Override
public boolean eIsSet(int featureID) {
switch (featureID) {
case IrPackage.CFG__ENTRY:
return entry != null;
case IrPackage.CFG__EXIT:
return exit != null;
}
return super.eIsSet(featureID);
}
/**
* <!-- begin-user-doc --> <!-- end-user-doc -->
* @generated
*/
@Override
public void eSet(int featureID, Object newValue) {
switch (featureID) {
case IrPackage.CFG__ENTRY:
setEntry((CfgNode) newValue);
return;
case IrPackage.CFG__EXIT:
setExit((CfgNode) newValue);
return;
}
super.eSet(featureID, newValue);
}
/**
* <!-- begin-user-doc --> <!-- end-user-doc -->
* @generated
*/
@Override
protected EClass eStaticClass() {
return IrPackage.Literals.CFG;
}
/**
* <!-- begin-user-doc --> <!-- end-user-doc -->
* @generated
*/
@Override
public void eUnset(int featureID) {
switch (featureID) {
case IrPackage.CFG__ENTRY:
setEntry((CfgNode) null);
return;
case IrPackage.CFG__EXIT:
setExit((CfgNode) null);
return;
}
super.eUnset(featureID);
}
/**
* <!-- begin-user-doc --> <!-- end-user-doc -->
* @generated
*/
public CfgNode getEntry() {
if (entry != null && entry.eIsProxy()) {
InternalEObject oldEntry = (InternalEObject) entry;
entry = (CfgNode) eResolveProxy(oldEntry);
if (entry != oldEntry) {
if (eNotificationRequired())
eNotify(new ENotificationImpl(this, Notification.RESOLVE,
IrPackage.CFG__ENTRY, oldEntry, entry));
}
}
return entry;
}
/**
* <!-- begin-user-doc --> <!-- end-user-doc -->
* @generated
*/
public CfgNode getExit() {
if (exit != null && exit.eIsProxy()) {
InternalEObject oldExit = (InternalEObject) exit;
exit = (CfgNode) eResolveProxy(oldExit);
if (exit != oldExit) {
if (eNotificationRequired())
eNotify(new ENotificationImpl(this, Notification.RESOLVE,
IrPackage.CFG__EXIT, oldExit, exit));
}
}
return exit;
}
@Override
@SuppressWarnings("unchecked")
public EList<CfgNode> getNodes() {
return (EList<CfgNode>) (EList<?>) getVertices();
}
@Override
public boolean immediatelyDominates(Vertex m, Vertex n) {
if (doms == null) {
throw new IllegalStateException(
"computeDominance must be called first");
}
return m == doms.get(n);
}
@Override
public boolean immediatelyPostDominates(Vertex m, Vertex n) {
if (idoms == null) {
throw new IllegalStateException(
"computeDominance must be called first");
}
return m == idoms.get(n);
}
@Override
public boolean isLoop(Vertex node) {
List<Edge> edges = node.getIncoming();
if (edges.size() >= 2) {
for (Edge edge : edges) {
if (immediatelyDominates(edge.getSource(), node)) {
return true;
}
}
}
return false;
}
/**
* <!-- begin-user-doc --> <!-- end-user-doc -->
* @generated
*/
public void setEntry(CfgNode newEntry) {
CfgNode oldEntry = entry;
entry = newEntry;
if (eNotificationRequired())
eNotify(new ENotificationImpl(this, Notification.SET,
IrPackage.CFG__ENTRY, oldEntry, entry));
}
/**
* <!-- begin-user-doc --> <!-- end-user-doc -->
* @generated
*/
public void setExit(CfgNode newExit) {
CfgNode oldExit = exit;
exit = newExit;
if (eNotificationRequired())
eNotify(new ENotificationImpl(this, Notification.SET,
IrPackage.CFG__EXIT, oldExit, exit));
}
} // CfgImpl