From 0faf5965328a2355adb4fa68acadcafab31aa3d0 Mon Sep 17 00:00:00 2001 From: apvrille <ludovic.apvrille@eurecom.fr> Date: Tue, 13 Feb 2018 14:22:54 +0100 Subject: [PATCH] Update on TTool debug traces --- .../tomappingsystemc2/LiveVariableNode.java | 1462 +++++++++-------- .../tomappingsystemc2/StaticAnalysis.java | 928 +++++------ 2 files changed, 1200 insertions(+), 1190 deletions(-) diff --git a/src/main/java/tmltranslator/tomappingsystemc2/LiveVariableNode.java b/src/main/java/tmltranslator/tomappingsystemc2/LiveVariableNode.java index f9533d4250..13245f1d8a 100644 --- a/src/main/java/tmltranslator/tomappingsystemc2/LiveVariableNode.java +++ b/src/main/java/tmltranslator/tomappingsystemc2/LiveVariableNode.java @@ -37,7 +37,6 @@ */ - package tmltranslator.tomappingsystemc2; import myutil.TraceManager; @@ -48,759 +47,766 @@ import javax.script.ScriptEngineManager; import javax.script.ScriptException; import java.util.LinkedList; -public class LiveVariableNode{ - private LinkedList<LiveVariableNode> _succList = new LinkedList<LiveVariableNode>(), _predList = new LinkedList<LiveVariableNode>(); - private boolean _valid=false; - StaticAnalysis _analysis; - //** Live Variable Analysis - private int[] _useVars; - private int[] _defVars; - private int[] _initialDefVars; - private int[] _inVars; - private int[] _outVars; - //** Reachable Definition analysis - private int[] _killDefs; - private int[] _generateDefs; - private int[] _killCandidates; - private int[] _inDefs; - private int[] _outDefs; - //** others - private int _myDefID = -2; - private boolean _isConstant=false; - private boolean _constantStuckToFalse=false; - private boolean _infected=false; - //private int _checkpoint=0; - private CheckpointInfo _checkpoint=null; - private boolean _guard=false; - private TMLActivityElement _linkedElem = null; - private LiveVariableNode _superiorBranchNode = null; - //private int _exprValue = 0; - private String _exprValue = ""; - private String _lhs = ""; - private String _rhs = ""; - private String _unrolledExpr = ""; - private boolean _varDepSource = false; - private String _nodeInfo=""; - - - LiveVariableNode(StaticAnalysis iAnalysis, int[] iUseVars, int[] iDefVars, TMLActivityElement iLinkedElem, LiveVariableNode iSuperiorNode, boolean iConstantStuckToFalse, String iLhs, String iRhs){ - this(iAnalysis, iUseVars, iDefVars, iLinkedElem, iSuperiorNode, iConstantStuckToFalse); - _lhs=iLhs; - _rhs= iRhs; - //TraceManager.addDev("lhs in init:*" + _lhs + "* rhs in init:*" + _rhs); - } - - LiveVariableNode(StaticAnalysis iAnalysis, int[] iUseVars, int[] iDefVars, TMLActivityElement iLinkedElem, LiveVariableNode iSuperiorNode, boolean iConstantStuckToFalse){ - this(iAnalysis, iUseVars, iDefVars, iLinkedElem, iSuperiorNode); - _constantStuckToFalse=iConstantStuckToFalse; - - } - - LiveVariableNode(StaticAnalysis iAnalysis, int[] iUseVars, int[] iDefVars, TMLActivityElement iLinkedElem, LiveVariableNode iSuperiorNode){ - _useVars=iUseVars; - _defVars=iDefVars; - _initialDefVars = new int[_defVars.length]; - for (int i=0; i<_defVars.length; i++) - _initialDefVars[i]=_defVars[i]; - _superiorBranchNode = iSuperiorNode; - _linkedElem = iLinkedElem; - _analysis = iAnalysis; - //_succList = ; - //_predList = new LinkedList<LiveVariableNode>(); - } - - public void liveVariableInit(){ - boolean isADefinition=false; - _inVars = new int[_defVars.length]; - _outVars = new int[_defVars.length]; - for (int i=0; i<_defVars.length;i++){ - //_defNegVars[i] = ~_defVars[i]; - //_inVars[i] = _useVars[i] & _defNegVars[i]; - _inVars[i] = _useVars[i] & (~_defVars[i]); - isADefinition |= (_defVars[i]!=0); - } - if (isADefinition) - _myDefID = _analysis.getNextDefID(); - else - _myDefID=-1; - } - - public int varStatistics(int[] ioStatistics){ - if(_checkpoint!=null){ - for (int bytes=0; bytes < _outVars.length; bytes++){ - for (int bits=0; bits<32;bits++){ - //TraceManager.addDev("bytes: " + bytes + " stat index: " + (bytes << 5 |bits)); - //if((_outVars[bytes] & (1 << bits))!=0) - if((_outVars[bytes] & (1 << bits))!=0 || (_useVars[bytes] & (1 << bits))!=0) - ioStatistics[bytes << 5 |bits]++; - } - } - return 3; - } - return (_linkedElem!=null && (_linkedElem instanceof TMLReadChannel || _linkedElem instanceof TMLWriteChannel|| _linkedElem instanceof TMLSendEvent || _linkedElem instanceof TMLWaitEvent || _linkedElem instanceof TMLSendRequest))? 1:0; - } - - private boolean atLeast1DepDefForVars(int[] iConcernedVars, boolean iVarGoOutOfScope, int iMinNbOfDefs, CheckpointInfo iCheckInfo){ - //following a command where a variable goes out of scope which is reached by at least two definitions, at least one of them being dependent - //following a command which kills more than one definition of a variable and at least one of these definitions is dependent - int[] aDefsForVar = new int[_analysis.getBytesForDefs()]; - for (int bytes=0; bytes<iConcernedVars.length; bytes++){ - for (int bits=0; bits<32;bits++){ - if ((iConcernedVars[bytes] & (1<< bits))!=0 && (!iVarGoOutOfScope || (_outVars[bytes] & (1<< bits))==0)){ - //for all variables which are used in this statement and go out of scope - //reached by dependent definition? - //find all definitions for var - for (int i=0; i<_analysis.getBytesForDefs();i++) - aDefsForVar[i] = _inDefs[i] & _analysis.getDefsForVar()[bytes << 5 |bits][i]; - //if (defsInfected(aDefsForVar)<-1) return true; - //if (defsInfected(aDefsForVar)<0) return true; //NEW!!!!!!!!! - if (defsInfected(aDefsForVar) < 1 - iMinNbOfDefs){ - iCheckInfo.setDefinitions(aDefsForVar); - iCheckInfo.setVariableID(bytes << 5 |bits, iConcernedVars.length); - return true; - } - } - } - } - return false; - } - - private int isCheckpointCandidate(CheckpointInfo iCheckInfo){ - //being dependent or having at least one dependent inverse operator in the task the communication is established with - int aReasonCode=0; - //if (_linkedElem!=null) TraceManager.addDev("--- " + _linkedElem.getID() + " start isCheckpointCandidate"); else - //TraceManager.addDev("--- start isCheckpointCandidate"); - if(_linkedElem instanceof TMLReadChannel || _linkedElem instanceof TMLWriteChannel){ - for (int i=0; i<((TMLActivityElementChannel)_linkedElem).getNbOfChannels(); i++){ - TMLChannel aChannel = ((TMLActivityElementChannel)_linkedElem).getChannel(i); - //TraceManager.addDev("get Infected for channel " + aChannel.getName() + "=" + aChannel.getInfected()); - //if (aChannel.getInfected()) aReasonCode = CheckpointInfo.CHANNEL_DEP; - if (_analysis.isChannelDep(aChannel.getID())) aReasonCode = CheckpointInfo.CHANNEL_DEP; - } - }else if(_linkedElem instanceof TMLSendEvent || _linkedElem instanceof TMLWaitEvent){ - //if (((TMLActivityElementEvent)_linkedElem).getEvent().getInfected()) aReasonCode = CheckpointInfo.CHANNEL_DEP; - if (_analysis.isChannelDep(((TMLActivityElementEvent)_linkedElem).getEvent().getID())) aReasonCode = CheckpointInfo.CHANNEL_DEP; - }else if (_linkedElem instanceof TMLSendRequest){ - //if (((TMLSendRequest)_linkedElem).getRequest().getInfected()) aReasonCode = CheckpointInfo.CHANNEL_DEP; - if (_analysis.isChannelDep(((TMLSendRequest)_linkedElem).getRequest().getID())) aReasonCode = CheckpointInfo.CHANNEL_DEP; - } - //consider receive event - if (_predList.size()>1){ - //TraceManager.addDev("more than one pred found"); - aReasonCode += CheckpointInfo.CTRL_FLOW_JOIN; - } - iCheckInfo.setOperationMode(CheckpointInfo.KILLED_DEF); - if (atLeast1DepDefForVars(_defVars, false, 1, iCheckInfo)){ - //TraceManager.addDev(getStatementDescr() + " kills dependent definitions!!!"); - aReasonCode += CheckpointInfo.KILLED_DEF; - } - iCheckInfo.setOperationMode(CheckpointInfo.VAR_OUT_OF_SCOPE); - if (atLeast1DepDefForVars(_useVars, true, 1, iCheckInfo)){ - //TraceManager.addDev(getStatementDescr()+ " variable went out of scope!!! "); - aReasonCode += CheckpointInfo.VAR_OUT_OF_SCOPE; - } - if(_varDepSource){ - if (canBeRemoved()) - _varDepSource=false; - else{ - //_analysis.setLastVarDepSource(_generateDefs); - iCheckInfo._depSrcDef = _generateDefs; - //TraceManager.addDev("Stored defs: "); printDefList(_generateDefs); - aReasonCode += CheckpointInfo.DEP_SRC; - } - } - //TraceManager.addDev("--- end isCheckpointCandidate: " + aReasonCode); - return aReasonCode; - - } - - //public static int evaluate(String string) throws IllegalArgumentException{ - public String evaluate(String expression) throws IllegalArgumentException{ - ScriptEngine engine = new ScriptEngineManager().getEngineByName("JavaScript"); - try{ - //Object object = engine.eval("eval("+string+")"); - TraceManager.addDev("Expression=" + expression); - engine.eval(expression); - Object object = engine.get("xx_xx"); - if (object != null){ - String result=""; - if(object instanceof Number){ - result = Integer.toString(((Number)(object)).intValue()); - }else if (object instanceof Boolean){ - result = object.toString(); - }else{ - throw new IllegalArgumentException("Input is neither Integer nor Boolean: '"+expression+"'"); - } - return result; - }else{ - throw new IllegalArgumentException("Invalid expression: '"+expression+"'"); - } - } catch (ScriptException e){ - TraceManager.addDev("expression=" + expression); - throw new IllegalArgumentException("Invalid input: '"+expression+"'", e); - } - } - - public void prepareReachingDefinitions(){ - invalidate(); - _generateDefs = new int[_analysis.getBytesForDefs()]; - _killDefs= new int[_analysis.getBytesForDefs()]; - _killCandidates = new int[_analysis.getBytesForDefs()]; - _inDefs = new int[_analysis.getBytesForDefs()]; - _outDefs = new int[_analysis.getBytesForDefs()]; - //TraceManager.addDev("<> varToStatements asso: " + getStatementDescr()); - if (_myDefID!=-1){ - //if (_myDefID!=-1 && !canBeRemoved()){ //NEW!!!!!!!!!!! - for (int bytes=0; bytes<_defVars.length;bytes++){ - for (int bits=0; bits<32;bits++){ - //if ((_defVars[bytes] & (1 << bits))!=0){ - //only consider live variables= defined + significant - if ((_defVars[bytes] & (1 << bits))!=0){ //NEW!!!!!!!!!! - if((_outVars[bytes] & (1 << bits))==0){ - _defVars[bytes] ^= (1 << bits); - //TraceManager.addDev("del"); - }else - _analysis.getDefsForVar()[(bytes << 5)|bits][_myDefID >>> 5] |= 1 << (_myDefID & 0x1F); - //TraceManager.addDev("var found:" + ((bytes << 5)|bits)); - } - } - } - if (!canBeRemoved()){ - _generateDefs[_myDefID >>> 5] = 1 << (_myDefID & 0x1F); - _analysis.getDefLookUp()[_myDefID]=this; - } - } - //TraceManager.addDev("<> END varToStatements asso: " + getStatementDescr()); - } - - public void printKillEntries(){ - //TraceManager.addDev("++++++++++ Kill definitions list ++++++++++"); - System.out.print(getStatementDescr() + " kills definitions: " + printDefList(_killDefs)); - TraceManager.addDev(""); - } - - public void printReachingEntries(){ - //TraceManager.addDev("++++++++++ Reaching definitions list ++++++++++"); - //if(_linkedElem!=null && (_linkedElem instanceof TMLReadChannel || _linkedElem instanceof TMLWriteChannel|| _linkedElem instanceof TMLSendEvent || _linkedElem instanceof TMLWaitEvent || _linkedElem instanceof TMLSendRequest)){ - System.out.print(getStatementDescr()); - /*printDefList(_inVars); +public class LiveVariableNode { + private LinkedList<LiveVariableNode> _succList = new LinkedList<LiveVariableNode>(), _predList = new LinkedList<LiveVariableNode>(); + private boolean _valid = false; + StaticAnalysis _analysis; + //** Live Variable Analysis + private int[] _useVars; + private int[] _defVars; + private int[] _initialDefVars; + private int[] _inVars; + private int[] _outVars; + //** Reachable Definition analysis + private int[] _killDefs; + private int[] _generateDefs; + private int[] _killCandidates; + private int[] _inDefs; + private int[] _outDefs; + //** others + private int _myDefID = -2; + private boolean _isConstant = false; + private boolean _constantStuckToFalse = false; + private boolean _infected = false; + //private int _checkpoint=0; + private CheckpointInfo _checkpoint = null; + private boolean _guard = false; + private TMLActivityElement _linkedElem = null; + private LiveVariableNode _superiorBranchNode = null; + //private int _exprValue = 0; + private String _exprValue = ""; + private String _lhs = ""; + private String _rhs = ""; + private String _unrolledExpr = ""; + private boolean _varDepSource = false; + private String _nodeInfo = ""; + + + LiveVariableNode(StaticAnalysis iAnalysis, int[] iUseVars, int[] iDefVars, TMLActivityElement iLinkedElem, LiveVariableNode iSuperiorNode, boolean iConstantStuckToFalse, String iLhs, String iRhs) { + this(iAnalysis, iUseVars, iDefVars, iLinkedElem, iSuperiorNode, iConstantStuckToFalse); + _lhs = iLhs; + _rhs = iRhs; + //TraceManager.addDev("lhs in init:*" + _lhs + "* rhs in init:*" + _rhs); + } + + LiveVariableNode(StaticAnalysis iAnalysis, int[] iUseVars, int[] iDefVars, TMLActivityElement iLinkedElem, LiveVariableNode iSuperiorNode, boolean iConstantStuckToFalse) { + this(iAnalysis, iUseVars, iDefVars, iLinkedElem, iSuperiorNode); + _constantStuckToFalse = iConstantStuckToFalse; + + } + + LiveVariableNode(StaticAnalysis iAnalysis, int[] iUseVars, int[] iDefVars, TMLActivityElement iLinkedElem, LiveVariableNode iSuperiorNode) { + _useVars = iUseVars; + _defVars = iDefVars; + _initialDefVars = new int[_defVars.length]; + for (int i = 0; i < _defVars.length; i++) + _initialDefVars[i] = _defVars[i]; + _superiorBranchNode = iSuperiorNode; + _linkedElem = iLinkedElem; + _analysis = iAnalysis; + //_succList = ; + //_predList = new LinkedList<LiveVariableNode>(); + } + + public void liveVariableInit() { + boolean isADefinition = false; + _inVars = new int[_defVars.length]; + _outVars = new int[_defVars.length]; + for (int i = 0; i < _defVars.length; i++) { + //_defNegVars[i] = ~_defVars[i]; + //_inVars[i] = _useVars[i] & _defNegVars[i]; + _inVars[i] = _useVars[i] & (~_defVars[i]); + isADefinition |= (_defVars[i] != 0); + } + if (isADefinition) + _myDefID = _analysis.getNextDefID(); + else + _myDefID = -1; + } + + public int varStatistics(int[] ioStatistics) { + if (_checkpoint != null) { + for (int bytes = 0; bytes < _outVars.length; bytes++) { + for (int bits = 0; bits < 32; bits++) { + //TraceManager.addDev("bytes: " + bytes + " stat index: " + (bytes << 5 |bits)); + //if((_outVars[bytes] & (1 << bits))!=0) + if ((_outVars[bytes] & (1 << bits)) != 0 || (_useVars[bytes] & (1 << bits)) != 0) + ioStatistics[bytes << 5 | bits]++; + } + } + return 3; + } + return (_linkedElem != null && (_linkedElem instanceof TMLReadChannel || _linkedElem instanceof TMLWriteChannel || _linkedElem instanceof TMLSendEvent || _linkedElem instanceof TMLWaitEvent || _linkedElem instanceof TMLSendRequest)) ? 1 : 0; + } + + private boolean atLeast1DepDefForVars(int[] iConcernedVars, boolean iVarGoOutOfScope, int iMinNbOfDefs, CheckpointInfo iCheckInfo) { + //following a command where a variable goes out of scope which is reached by at least two definitions, at least one of them being dependent + //following a command which kills more than one definition of a variable and at least one of these definitions is dependent + int[] aDefsForVar = new int[_analysis.getBytesForDefs()]; + for (int bytes = 0; bytes < iConcernedVars.length; bytes++) { + for (int bits = 0; bits < 32; bits++) { + if ((iConcernedVars[bytes] & (1 << bits)) != 0 && (!iVarGoOutOfScope || (_outVars[bytes] & (1 << bits)) == 0)) { + //for all variables which are used in this statement and go out of scope + //reached by dependent definition? + //find all definitions for var + for (int i = 0; i < _analysis.getBytesForDefs(); i++) + aDefsForVar[i] = _inDefs[i] & _analysis.getDefsForVar()[bytes << 5 | bits][i]; + //if (defsInfected(aDefsForVar)<-1) return true; + //if (defsInfected(aDefsForVar)<0) return true; //NEW!!!!!!!!! + if (defsInfected(aDefsForVar) < 1 - iMinNbOfDefs) { + iCheckInfo.setDefinitions(aDefsForVar); + iCheckInfo.setVariableID(bytes << 5 | bits, iConcernedVars.length); + return true; + } + } + } + } + return false; + } + + private int isCheckpointCandidate(CheckpointInfo iCheckInfo) { + //being dependent or having at least one dependent inverse operator in the task the communication is established with + int aReasonCode = 0; + //if (_linkedElem!=null) TraceManager.addDev("--- " + _linkedElem.getID() + " start isCheckpointCandidate"); else + //TraceManager.addDev("--- start isCheckpointCandidate"); + if (_linkedElem instanceof TMLReadChannel || _linkedElem instanceof TMLWriteChannel) { + for (int i = 0; i < ((TMLActivityElementChannel) _linkedElem).getNbOfChannels(); i++) { + TMLChannel aChannel = ((TMLActivityElementChannel) _linkedElem).getChannel(i); + //TraceManager.addDev("get Infected for channel " + aChannel.getName() + "=" + aChannel.getInfected()); + //if (aChannel.getInfected()) aReasonCode = CheckpointInfo.CHANNEL_DEP; + if (_analysis.isChannelDep(aChannel.getID())) aReasonCode = CheckpointInfo.CHANNEL_DEP; + } + } else if (_linkedElem instanceof TMLSendEvent || _linkedElem instanceof TMLWaitEvent) { + //if (((TMLActivityElementEvent)_linkedElem).getEvent().getInfected()) aReasonCode = CheckpointInfo.CHANNEL_DEP; + if (_analysis.isChannelDep(((TMLActivityElementEvent) _linkedElem).getEvent().getID())) + aReasonCode = CheckpointInfo.CHANNEL_DEP; + } else if (_linkedElem instanceof TMLSendRequest) { + //if (((TMLSendRequest)_linkedElem).getRequest().getInfected()) aReasonCode = CheckpointInfo.CHANNEL_DEP; + if (_analysis.isChannelDep(((TMLSendRequest) _linkedElem).getRequest().getID())) + aReasonCode = CheckpointInfo.CHANNEL_DEP; + } + //consider receive event + if (_predList.size() > 1) { + //TraceManager.addDev("more than one pred found"); + aReasonCode += CheckpointInfo.CTRL_FLOW_JOIN; + } + iCheckInfo.setOperationMode(CheckpointInfo.KILLED_DEF); + if (atLeast1DepDefForVars(_defVars, false, 1, iCheckInfo)) { + //TraceManager.addDev(getStatementDescr() + " kills dependent definitions!!!"); + aReasonCode += CheckpointInfo.KILLED_DEF; + } + iCheckInfo.setOperationMode(CheckpointInfo.VAR_OUT_OF_SCOPE); + if (atLeast1DepDefForVars(_useVars, true, 1, iCheckInfo)) { + //TraceManager.addDev(getStatementDescr()+ " variable went out of scope!!! "); + aReasonCode += CheckpointInfo.VAR_OUT_OF_SCOPE; + } + if (_varDepSource) { + if (canBeRemoved()) + _varDepSource = false; + else { + //_analysis.setLastVarDepSource(_generateDefs); + iCheckInfo._depSrcDef = _generateDefs; + //TraceManager.addDev("Stored defs: "); printDefList(_generateDefs); + aReasonCode += CheckpointInfo.DEP_SRC; + } + } + //TraceManager.addDev("--- end isCheckpointCandidate: " + aReasonCode); + return aReasonCode; + + } + + //public static int evaluate(String string) throws IllegalArgumentException{ + public String evaluate(String expression) throws IllegalArgumentException { + ScriptEngine engine = new ScriptEngineManager().getEngineByName("JavaScript"); + try { + //Object object = engine.eval("eval("+string+")"); + //TraceManager.addDev("Expression=" + expression); + engine.eval(expression); + Object object = engine.get("xx_xx"); + if (object != null) { + String result = ""; + if (object instanceof Number) { + result = Integer.toString(((Number) (object)).intValue()); + } else if (object instanceof Boolean) { + result = object.toString(); + } else { + throw new IllegalArgumentException("Input is neither Integer nor Boolean: '" + expression + "'"); + } + return result; + } else { + throw new IllegalArgumentException("Invalid expression: '" + expression + "'"); + } + } catch (ScriptException e) { + //TraceManager.addDev("expression=" + expression); + throw new IllegalArgumentException("Invalid input: '" + expression + "'", e); + } + } + + public void prepareReachingDefinitions() { + invalidate(); + _generateDefs = new int[_analysis.getBytesForDefs()]; + _killDefs = new int[_analysis.getBytesForDefs()]; + _killCandidates = new int[_analysis.getBytesForDefs()]; + _inDefs = new int[_analysis.getBytesForDefs()]; + _outDefs = new int[_analysis.getBytesForDefs()]; + //TraceManager.addDev("<> varToStatements asso: " + getStatementDescr()); + if (_myDefID != -1) { + //if (_myDefID!=-1 && !canBeRemoved()){ //NEW!!!!!!!!!!! + for (int bytes = 0; bytes < _defVars.length; bytes++) { + for (int bits = 0; bits < 32; bits++) { + //if ((_defVars[bytes] & (1 << bits))!=0){ + //only consider live variables= defined + significant + if ((_defVars[bytes] & (1 << bits)) != 0) { //NEW!!!!!!!!!! + if ((_outVars[bytes] & (1 << bits)) == 0) { + _defVars[bytes] ^= (1 << bits); + //TraceManager.addDev("del"); + } else + _analysis.getDefsForVar()[(bytes << 5) | bits][_myDefID >>> 5] |= 1 << (_myDefID & 0x1F); + //TraceManager.addDev("var found:" + ((bytes << 5)|bits)); + } + } + } + if (!canBeRemoved()) { + _generateDefs[_myDefID >>> 5] = 1 << (_myDefID & 0x1F); + _analysis.getDefLookUp()[_myDefID] = this; + } + } + //TraceManager.addDev("<> END varToStatements asso: " + getStatementDescr()); + } + + public void printKillEntries() { + //TraceManager.addDev("++++++++++ Kill definitions list ++++++++++"); + System.out.print(getStatementDescr() + " kills definitions: " + printDefList(_killDefs)); + TraceManager.addDev(""); + } + + public void printReachingEntries() { + //TraceManager.addDev("++++++++++ Reaching definitions list ++++++++++"); + //if(_linkedElem!=null && (_linkedElem instanceof TMLReadChannel || _linkedElem instanceof TMLWriteChannel|| _linkedElem instanceof TMLSendEvent || _linkedElem instanceof TMLWaitEvent || _linkedElem instanceof TMLSendRequest)){ + System.out.print(getStatementDescr()); + /*printDefList(_inVars); System.out.print(" outvars: "); printDefList(_outVars); System.out.print(" defVars: "); printDefList(_defVars); System.out.print(" useVars: "); printDefList(_useVars);*/ - //+ " reached by definitions: "); - //printDefList(_inDefs); - TraceManager.addDev(""); - //} - } - - private String printDefList(int[] iList){ - String aResult=""; - for (int bytes=0; bytes<iList.length; bytes++){ - for (int bits=0; bits<32;bits++){ - if ((iList[bytes]& (1 << bits))!=0) aResult += ((bytes << 5)|bits) + ", "; - } - } - return aResult; - } - - private String printDefCmdIDs(int[] iList){ - String aResult=""; - if (iList!=null){ - for (int bytes=0; bytes<iList.length; bytes++){ - for (int bits=0; bits<32;bits++){ - if ((iList[bytes] & (1 << bits))!=0){ - TMLActivityElement anElem = _analysis.getDefLookUp()[(bytes << 5)|bits]._linkedElem; - aResult += _analysis.getDefLookUp()[(bytes << 5)|bits].getNodeInfo(); - if (anElem!=null) aResult += "(" + anElem.getID() + "),"; else aResult += ","; - } - } - } - } - return aResult; - } - - private boolean allDefsConstantAndEqual(int[] iDefinitions){ - boolean aIsConst=true, aFirstTime=true, aDefFound=false; - //int aLastExprVal=0; - String aLastExprVal=""; - //TraceManager.addDev("******* allDefsConstantAndEqual"); - for (int bytes=0; bytes<iDefinitions.length && aIsConst; bytes++){ - for (int bits=0; bits<32 && aIsConst; bits++){ - int anIndex = (bytes << 5)|bits; - if ((iDefinitions[bytes] & (1<< bits))!=0){ //what if there are no definitions???? - aDefFound=true; - if (_analysis.getDefLookUp()[anIndex].isConstant()){ - //TraceManager.addDev(_analysis.getDefLookUp()[anIndex].getStatementDescr() + " said to be constant"); - //if (aFirstTime || _analysis.getDefLookUp()[anIndex].getExpressionValue() == aLastExprVal){ - if (aFirstTime || _analysis.getDefLookUp()[anIndex].getExpressionValue().equals(aLastExprVal)){ - aLastExprVal = _analysis.getDefLookUp()[anIndex].getExpressionValue(); - _unrolledExpr+= _analysis.getDefLookUp()[anIndex].getExpressionString() + ";"; - aFirstTime=false; - }else - aIsConst=false; - }else{ - //TraceManager.addDev(_analysis.getDefLookUp()[anIndex].getStatementDescr() + " said to be NOT constant"); - aIsConst=false; - } - }//else - //if (_analysis.getDefLookUp()[anIndex]!=null) TraceManager.addDev(_analysis.getDefLookUp()[anIndex].getStatementDescr() + " not concerned"); - } - } - //if (aIsConst) - //TraceManager.addDev(aIsConst + " lhs:" + _lhs + " rhs:" + _rhs); - return (aIsConst && aDefFound); - } - - public int determineIfConstant(){ - //not constant: random, notified event, receive event, select event - //if (_linkedElem!=null && (_linkedElem instanceof TMLRandom || _linkedElem instanceof TMLWaitEvent || _linkedElem instanceof TMLNotifiedEvent || _linkedElem instanceof TMLSelectEvt)) return false; - if (_isConstant) return 0; - _valid=false; - //boolean aIsConst=true; - int aResult=0; - if (!_constantStuckToFalse){ - int[] aReachingDefForVar= new int[_analysis.getBytesForDefs()]; - aResult=2; - //TraceManager.addDev("!!!!Checking if " + getStatementDescr() + " is constant"); - for (int bytes=0; bytes<_useVars.length; bytes++){ - for (int bits=0; bits<32;bits++){ - if ((_useVars[bytes] & (1<< bits))!=0){ - //TraceManager.addDev("var " + ((bytes << 5)|bits) + " is used usevars:" + _useVars[bytes]); - //TraceManager.addDev("Reached by definitions: "); - //printDefList(_inDefs); - for (int i=0; i<_analysis.getBytesForDefs();i++){ - aReachingDefForVar[i] = _analysis.getDefsForVar()[(bytes << 5)|bits][i] & _inDefs[i]; - } - //aIsConst &= allDefsConstant(aReachingDefForVar); - if(allDefsConstantAndEqual(aReachingDefForVar)){ - //toggle bit no [bits] = set it to zero as it is one - _useVars[bytes]^= (1<< bits); - //TraceManager.addDev("*** " + getStatementDescr() + "toggle bit " + ((bytes << 5)|bits) + "***"); - //_inVars[bytes] = _useVars[bytes] & _defNegVars[bytes]; - //_inVars[bytes] = _useVars[bytes] & (~_defVars[bytes]); NEW!!!!!!! - aResult |= 1; //set bit 2^0 - }else{ - //TraceManager.addDev("not all defs constant for variable " + ((bytes << 5)|bits)); - //aIsConst=false; - aResult &= 1; //del bit 2^1 - } - } - } - } - _isConstant = ((aResult & 2)!=0); - if (_isConstant && !(_lhs==null ||_lhs.isEmpty())){ - _unrolledExpr+= "xx_xx=" + _rhs + ";"; - //try{ - _exprValue = evaluate(_unrolledExpr); - //TraceManager.addDev("Expr: *" + _unrolledExpr + "* evaluated to: " + _exprValue); - //}catch(IllegalArgumentException e){ - //TraceManager.addDev("At lest one variable of the expression remains undefined: " + _rhs); - //} - } - } - //boolean aChangeInResult = (_constantStuckToFalse)? false : aIsConst!= _isConstant; - //boolean aChangeInResult = (_constantStuckToFalse)? false : aIsConst; - //_isConstant = aIsConst; - //return aChangeInResult; - return aResult; - //} - } - - public boolean isConstant(){ - return !_constantStuckToFalse && _isConstant; - } - - public boolean isInfected(){ - return _infected; - } - - public String getExpressionString(){ - return _lhs + "=" + _exprValue; - } - - //public int getExpressionValue(){ - public String getExpressionValue(){ - return _exprValue; - } - - public int[] getKillCandidates(){ - return _killCandidates; - } - - public void setInfected(boolean iInfected){ - _infected = iInfected; - } - - public void setVarDepSource(boolean iVarDepSource){ - _infected = iVarDepSource; - _varDepSource = iVarDepSource; - } - - public String getNodeInfo(){ - return _nodeInfo; - } - - public void setNodeInfo(String iInfo){ - _nodeInfo=iInfo; - } - - public boolean determineKilledSets(){ - boolean aChange=false; - int [] aKillOut = new int[_analysis.getBytesForDefs()]; - int [] aKillIn = new int[_analysis.getBytesForDefs()]; - if (!_valid){ - _valid=true; - //TraceManager.addDev("determineKilledSets for " + getStatementDescr() ); - for(LiveVariableNode aPred: _predList) { - int[] aKillCandidatesPred = aPred.getKillCandidates(); - for (int i=0; i<_analysis.getBytesForDefs();i++){ - aKillOut[i] |= aKillCandidatesPred[i]; - aKillIn[i] |= aKillCandidatesPred[i]; - } - } - //TraceManager.addDev( "aLocalKillCandidates[0] " + aLocalKillCandidates[0]); - //TraceManager.addDev( "killDefs[0] before" + _killDefs[0]); - if (_myDefID!=-1){ - //TraceManager.addDev("Statement " + _myDefID + " (" + getStatementDescr() + ") defines: " + _generateDefs[0] + " could kill input: " + aLocalKillCandidates[0]); - for (int bytes=0; bytes<_defVars.length; bytes++){ - for (int bits=0; bits<32;bits++){ - if ((_defVars[bytes] & (1<< bits))!=0){ //for all variables which are defined in this statement - for (int i=0; i<_analysis.getBytesForDefs();i++){ - //killDefs = definitions representing kill candidates in program flow & all statements affecting the current variable targeted in this definition - //all kill candidates -> for a specific variable -> variable determined by defVars - //_killDefs[i] |= (aLocalKillCandidates[i] & _varToStatements[(bytes << 5)|bits][i]); - _killDefs[i] |= (aKillIn[i] & _analysis.getDefsForVar()[(bytes << 5)|bits][i]); - //TraceManager.addDev( "killDefs[i] " + _killDefs[i] + " i" + i); - //int oldCandidates = aLocalKillCandidates[i]; - //delete all kill candidates for the variable yielded by that definition - //aLocalKillCandidates[i] &= ~ _varToStatements[(bytes << 5)|bits][i]; - aKillOut[i] &= ~ _analysis.getDefsForVar()[(bytes << 5)|bits][i]; - //add this definition as killer for the variable - //aLocalKillCandidates[i] |= _generateDefs[i]; - aKillOut[i] |= _generateDefs[i]; - //aChange |= (oldCandidates!=aLocalKillCandidates[i]); - } - } - } - } - } - //TraceManager.addDev( "killDefs[0] " + _killDefs[0]); - for (int i=0; i<_analysis.getBytesForDefs();i++){ - //aChange |= (_killCandidates[i]!=aLocalKillCandidates[i]); - aChange |= (_killCandidates[i]!=aKillOut[i]); - //_killCandidates[i]=aLocalKillCandidates[i]; - _killCandidates[i]=aKillOut[i]; - } - if (aChange){ - for(LiveVariableNode aSucc: _succList) aSucc.invalidate(); - } - } - return aChange; - } - - public String getStatementDescr(){ - //String aName = "| Live ID: " + _myDefID + " | "; - String aName = "| "; - if (_linkedElem==null) - aName += "cmd type: unknown | cmd ID: unknown"; - else - aName += "cmd type: " + _linkedElem.getName() + " | cmd ID: " + _linkedElem.getID(); - aName+= " | infected: " + _infected; - if (!(_lhs==null || _rhs==null || _lhs.isEmpty() || _rhs.isEmpty())) - aName += " | action: " + _lhs + "=" + _rhs; - aName += " | const: "; - if (_isConstant){ - if (_lhs==null || _lhs.isEmpty()) - aName+= "yes"; - else - aName+= _exprValue; - }else - aName += "no"; - aName += " | to be removed: " + canBeRemoved() + " | checkp:"; - if (_checkpoint==null) - aName += "0"; - else{ - if ((_checkpoint._reasonCode & CheckpointInfo.CHANNEL_DEP)!=0) aName += " CH_DEP"; - if ((_checkpoint._reasonCode & CheckpointInfo.CTRL_FLOW_JOIN)!=0) aName += " CTRL(" + printDefCmdIDs(_checkpoint._ctrlFlowJoinDefs) + ")"; - if ((_checkpoint._reasonCode & CheckpointInfo.KILLED_DEF)!=0) aName += " KILL(" + printDefCmdIDs(_checkpoint._killedDefs) + ")"; - if ((_checkpoint._reasonCode & CheckpointInfo.VAR_OUT_OF_SCOPE)!=0) aName += " VAR_SCP(" + _analysis.printVariables(_checkpoint._varsOutOfScope) + ")"; - if ((_checkpoint._reasonCode & CheckpointInfo.DEP_SRC)!=0) aName += " SRC(" + printDefCmdIDs(_checkpoint._depSrcDef) + ")"; - } - aName += " | "; - return aName; - } - - public void invalidate(){ - _valid=false; - } - - private boolean chOfCmdAllNBRNBW(TMLActivityElementChannel iElem){ - for (int i=0; i<iElem.getNbOfChannels(); i++) - if (iElem.getChannel(i).getType()!=TMLChannel.NBRNBW) return false; - return true; - } - - //public void determineCheckpoints(int iPropagate, CheckpointInfo iCheckInfo){ - public void determineCheckpoints(CheckpointInfo iCheckInfo){ - if (!_guard){ - //if (_checkpoint==0){ - if (_checkpoint==null){ - //int aCandidate = iCheckInfo.__reasonCode | isCheckpointCandidate(iCheckInfo); - iCheckInfo._reasonCode |= isCheckpointCandidate(iCheckInfo); - //if (_linkedElem!=null) TraceManager.addDev(_linkedElem.getID() + " aCandidate: " + aCandidate); - iCheckInfo.setOperationMode(CheckpointInfo.CTRL_FLOW_JOIN); - //if ((aCandidate & CheckpointInfo.CTRL_FLOW_JOIN)!=0 && !atLeast1DepDefForVars(_inVars, false, 2, iCheckInfo)){ - if ((iCheckInfo._reasonCode & CheckpointInfo.CTRL_FLOW_JOIN)!=0 && !atLeast1DepDefForVars(_inVars, false, 2, iCheckInfo)){ - //aCandidate ^= CheckpointInfo.CTRL_FLOW_JOIN; - iCheckInfo._reasonCode ^= CheckpointInfo.CTRL_FLOW_JOIN; - //TraceManager.addDev("aCandidate reset control flow join"); - } - //if ((aCandidate & CheckpointInfo.DEP_SRC)!=0){ - if ((iCheckInfo._reasonCode & CheckpointInfo.DEP_SRC)!=0){ - boolean aDefReachable=false; - for (int i=0; i<_analysis.getBytesForDefs() && !aDefReachable; i++) - //aDefReachable |= ((_analysis.getLastVarDepSource()[i] & _outDefs[i]) !=0); - aDefReachable |= ((iCheckInfo._depSrcDef[i] & _outDefs[i]) !=0); - if (!aDefReachable){ - //aCandidate ^= CheckpointInfo.DEP_SRC; - iCheckInfo._reasonCode ^= CheckpointInfo.DEP_SRC; + //+ " reached by definitions: "); + //printDefList(_inDefs); + TraceManager.addDev(""); + //} + } + + private String printDefList(int[] iList) { + String aResult = ""; + for (int bytes = 0; bytes < iList.length; bytes++) { + for (int bits = 0; bits < 32; bits++) { + if ((iList[bytes] & (1 << bits)) != 0) aResult += ((bytes << 5) | bits) + ", "; + } + } + return aResult; + } + + private String printDefCmdIDs(int[] iList) { + String aResult = ""; + if (iList != null) { + for (int bytes = 0; bytes < iList.length; bytes++) { + for (int bits = 0; bits < 32; bits++) { + if ((iList[bytes] & (1 << bits)) != 0) { + TMLActivityElement anElem = _analysis.getDefLookUp()[(bytes << 5) | bits]._linkedElem; + aResult += _analysis.getDefLookUp()[(bytes << 5) | bits].getNodeInfo(); + if (anElem != null) aResult += "(" + anElem.getID() + "),"; + else aResult += ","; + } + } + } + } + return aResult; + } + + private boolean allDefsConstantAndEqual(int[] iDefinitions) { + boolean aIsConst = true, aFirstTime = true, aDefFound = false; + //int aLastExprVal=0; + String aLastExprVal = ""; + //TraceManager.addDev("******* allDefsConstantAndEqual"); + for (int bytes = 0; bytes < iDefinitions.length && aIsConst; bytes++) { + for (int bits = 0; bits < 32 && aIsConst; bits++) { + int anIndex = (bytes << 5) | bits; + if ((iDefinitions[bytes] & (1 << bits)) != 0) { //what if there are no definitions???? + aDefFound = true; + if (_analysis.getDefLookUp()[anIndex].isConstant()) { + //TraceManager.addDev(_analysis.getDefLookUp()[anIndex].getStatementDescr() + " said to be constant"); + //if (aFirstTime || _analysis.getDefLookUp()[anIndex].getExpressionValue() == aLastExprVal){ + if (aFirstTime || _analysis.getDefLookUp()[anIndex].getExpressionValue().equals(aLastExprVal)) { + aLastExprVal = _analysis.getDefLookUp()[anIndex].getExpressionValue(); + _unrolledExpr += _analysis.getDefLookUp()[anIndex].getExpressionString() + ";"; + aFirstTime = false; + } else + aIsConst = false; + } else { + //TraceManager.addDev(_analysis.getDefLookUp()[anIndex].getStatementDescr() + " said to be NOT constant"); + aIsConst = false; + } + }//else + //if (_analysis.getDefLookUp()[anIndex]!=null) TraceManager.addDev(_analysis.getDefLookUp()[anIndex].getStatementDescr() + " not concerned"); + } + } + //if (aIsConst) + //TraceManager.addDev(aIsConst + " lhs:" + _lhs + " rhs:" + _rhs); + return (aIsConst && aDefFound); + } + + public int determineIfConstant() { + //not constant: random, notified event, receive event, select event + //if (_linkedElem!=null && (_linkedElem instanceof TMLRandom || _linkedElem instanceof TMLWaitEvent || _linkedElem instanceof TMLNotifiedEvent || _linkedElem instanceof TMLSelectEvt)) return false; + if (_isConstant) return 0; + _valid = false; + //boolean aIsConst=true; + int aResult = 0; + if (!_constantStuckToFalse) { + int[] aReachingDefForVar = new int[_analysis.getBytesForDefs()]; + aResult = 2; + //TraceManager.addDev("!!!!Checking if " + getStatementDescr() + " is constant"); + for (int bytes = 0; bytes < _useVars.length; bytes++) { + for (int bits = 0; bits < 32; bits++) { + if ((_useVars[bytes] & (1 << bits)) != 0) { + //TraceManager.addDev("var " + ((bytes << 5)|bits) + " is used usevars:" + _useVars[bytes]); + //TraceManager.addDev("Reached by definitions: "); + //printDefList(_inDefs); + for (int i = 0; i < _analysis.getBytesForDefs(); i++) { + aReachingDefForVar[i] = _analysis.getDefsForVar()[(bytes << 5) | bits][i] & _inDefs[i]; + } + //aIsConst &= allDefsConstant(aReachingDefForVar); + if (allDefsConstantAndEqual(aReachingDefForVar)) { + //toggle bit no [bits] = set it to zero as it is one + _useVars[bytes] ^= (1 << bits); + //TraceManager.addDev("*** " + getStatementDescr() + "toggle bit " + ((bytes << 5)|bits) + "***"); + //_inVars[bytes] = _useVars[bytes] & _defNegVars[bytes]; + //_inVars[bytes] = _useVars[bytes] & (~_defVars[bytes]); NEW!!!!!!! + aResult |= 1; //set bit 2^0 + } else { + //TraceManager.addDev("not all defs constant for variable " + ((bytes << 5)|bits)); + //aIsConst=false; + aResult &= 1; //del bit 2^1 + } + } + } + } + _isConstant = ((aResult & 2) != 0); + if (_isConstant && !(_lhs == null || _lhs.isEmpty())) { + _unrolledExpr += "xx_xx=" + _rhs + ";"; + //try{ + _exprValue = evaluate(_unrolledExpr); + //TraceManager.addDev("Expr: *" + _unrolledExpr + "* evaluated to: " + _exprValue); + //}catch(IllegalArgumentException e){ + //TraceManager.addDev("At lest one variable of the expression remains undefined: " + _rhs); + //} + } + } + //boolean aChangeInResult = (_constantStuckToFalse)? false : aIsConst!= _isConstant; + //boolean aChangeInResult = (_constantStuckToFalse)? false : aIsConst; + //_isConstant = aIsConst; + //return aChangeInResult; + return aResult; + //} + } + + public boolean isConstant() { + return !_constantStuckToFalse && _isConstant; + } + + public boolean isInfected() { + return _infected; + } + + public String getExpressionString() { + return _lhs + "=" + _exprValue; + } + + //public int getExpressionValue(){ + public String getExpressionValue() { + return _exprValue; + } + + public int[] getKillCandidates() { + return _killCandidates; + } + + public void setInfected(boolean iInfected) { + _infected = iInfected; + } + + public void setVarDepSource(boolean iVarDepSource) { + _infected = iVarDepSource; + _varDepSource = iVarDepSource; + } + + public String getNodeInfo() { + return _nodeInfo; + } + + public void setNodeInfo(String iInfo) { + _nodeInfo = iInfo; + } + + public boolean determineKilledSets() { + boolean aChange = false; + int[] aKillOut = new int[_analysis.getBytesForDefs()]; + int[] aKillIn = new int[_analysis.getBytesForDefs()]; + if (!_valid) { + _valid = true; + //TraceManager.addDev("determineKilledSets for " + getStatementDescr() ); + for (LiveVariableNode aPred : _predList) { + int[] aKillCandidatesPred = aPred.getKillCandidates(); + for (int i = 0; i < _analysis.getBytesForDefs(); i++) { + aKillOut[i] |= aKillCandidatesPred[i]; + aKillIn[i] |= aKillCandidatesPred[i]; + } + } + //TraceManager.addDev( "aLocalKillCandidates[0] " + aLocalKillCandidates[0]); + //TraceManager.addDev( "killDefs[0] before" + _killDefs[0]); + if (_myDefID != -1) { + //TraceManager.addDev("Statement " + _myDefID + " (" + getStatementDescr() + ") defines: " + _generateDefs[0] + " could kill input: " + aLocalKillCandidates[0]); + for (int bytes = 0; bytes < _defVars.length; bytes++) { + for (int bits = 0; bits < 32; bits++) { + if ((_defVars[bytes] & (1 << bits)) != 0) { //for all variables which are defined in this statement + for (int i = 0; i < _analysis.getBytesForDefs(); i++) { + //killDefs = definitions representing kill candidates in program flow & all statements affecting the current variable targeted in this definition + //all kill candidates -> for a specific variable -> variable determined by defVars + //_killDefs[i] |= (aLocalKillCandidates[i] & _varToStatements[(bytes << 5)|bits][i]); + _killDefs[i] |= (aKillIn[i] & _analysis.getDefsForVar()[(bytes << 5) | bits][i]); + //TraceManager.addDev( "killDefs[i] " + _killDefs[i] + " i" + i); + //int oldCandidates = aLocalKillCandidates[i]; + //delete all kill candidates for the variable yielded by that definition + //aLocalKillCandidates[i] &= ~ _varToStatements[(bytes << 5)|bits][i]; + aKillOut[i] &= ~_analysis.getDefsForVar()[(bytes << 5) | bits][i]; + //add this definition as killer for the variable + //aLocalKillCandidates[i] |= _generateDefs[i]; + aKillOut[i] |= _generateDefs[i]; + //aChange |= (oldCandidates!=aLocalKillCandidates[i]); + } + } + } + } + } + //TraceManager.addDev( "killDefs[0] " + _killDefs[0]); + for (int i = 0; i < _analysis.getBytesForDefs(); i++) { + //aChange |= (_killCandidates[i]!=aLocalKillCandidates[i]); + aChange |= (_killCandidates[i] != aKillOut[i]); + //_killCandidates[i]=aLocalKillCandidates[i]; + _killCandidates[i] = aKillOut[i]; + } + if (aChange) { + for (LiveVariableNode aSucc : _succList) aSucc.invalidate(); + } + } + return aChange; + } + + public String getStatementDescr() { + //String aName = "| Live ID: " + _myDefID + " | "; + String aName = "| "; + if (_linkedElem == null) + aName += "cmd type: unknown | cmd ID: unknown"; + else + aName += "cmd type: " + _linkedElem.getName() + " | cmd ID: " + _linkedElem.getID(); + aName += " | infected: " + _infected; + if (!(_lhs == null || _rhs == null || _lhs.isEmpty() || _rhs.isEmpty())) + aName += " | action: " + _lhs + "=" + _rhs; + aName += " | const: "; + if (_isConstant) { + if (_lhs == null || _lhs.isEmpty()) + aName += "yes"; + else + aName += _exprValue; + } else + aName += "no"; + aName += " | to be removed: " + canBeRemoved() + " | checkp:"; + if (_checkpoint == null) + aName += "0"; + else { + if ((_checkpoint._reasonCode & CheckpointInfo.CHANNEL_DEP) != 0) aName += " CH_DEP"; + if ((_checkpoint._reasonCode & CheckpointInfo.CTRL_FLOW_JOIN) != 0) + aName += " CTRL(" + printDefCmdIDs(_checkpoint._ctrlFlowJoinDefs) + ")"; + if ((_checkpoint._reasonCode & CheckpointInfo.KILLED_DEF) != 0) + aName += " KILL(" + printDefCmdIDs(_checkpoint._killedDefs) + ")"; + if ((_checkpoint._reasonCode & CheckpointInfo.VAR_OUT_OF_SCOPE) != 0) + aName += " VAR_SCP(" + _analysis.printVariables(_checkpoint._varsOutOfScope) + ")"; + if ((_checkpoint._reasonCode & CheckpointInfo.DEP_SRC) != 0) + aName += " SRC(" + printDefCmdIDs(_checkpoint._depSrcDef) + ")"; + } + aName += " | "; + return aName; + } + + public void invalidate() { + _valid = false; + } + + private boolean chOfCmdAllNBRNBW(TMLActivityElementChannel iElem) { + for (int i = 0; i < iElem.getNbOfChannels(); i++) + if (iElem.getChannel(i).getType() != TMLChannel.NBRNBW) return false; + return true; + } + + //public void determineCheckpoints(int iPropagate, CheckpointInfo iCheckInfo){ + public void determineCheckpoints(CheckpointInfo iCheckInfo) { + if (!_guard) { + //if (_checkpoint==0){ + if (_checkpoint == null) { + //int aCandidate = iCheckInfo.__reasonCode | isCheckpointCandidate(iCheckInfo); + iCheckInfo._reasonCode |= isCheckpointCandidate(iCheckInfo); + //if (_linkedElem!=null) TraceManager.addDev(_linkedElem.getID() + " aCandidate: " + aCandidate); + iCheckInfo.setOperationMode(CheckpointInfo.CTRL_FLOW_JOIN); + //if ((aCandidate & CheckpointInfo.CTRL_FLOW_JOIN)!=0 && !atLeast1DepDefForVars(_inVars, false, 2, iCheckInfo)){ + if ((iCheckInfo._reasonCode & CheckpointInfo.CTRL_FLOW_JOIN) != 0 && !atLeast1DepDefForVars(_inVars, false, 2, iCheckInfo)) { + //aCandidate ^= CheckpointInfo.CTRL_FLOW_JOIN; + iCheckInfo._reasonCode ^= CheckpointInfo.CTRL_FLOW_JOIN; + //TraceManager.addDev("aCandidate reset control flow join"); + } + //if ((aCandidate & CheckpointInfo.DEP_SRC)!=0){ + if ((iCheckInfo._reasonCode & CheckpointInfo.DEP_SRC) != 0) { + boolean aDefReachable = false; + for (int i = 0; i < _analysis.getBytesForDefs() && !aDefReachable; i++) + //aDefReachable |= ((_analysis.getLastVarDepSource()[i] & _outDefs[i]) !=0); + aDefReachable |= ((iCheckInfo._depSrcDef[i] & _outDefs[i]) != 0); + if (!aDefReachable) { + //aCandidate ^= CheckpointInfo.DEP_SRC; + iCheckInfo._reasonCode ^= CheckpointInfo.DEP_SRC; /*TraceManager.addDev("aCandidate reset dep src"); TraceManager.addDev("comp1 in defs: "); printDefList(_outDefs); TraceManager.addDev("comp2 stored defs: "); printDefList(_analysis.getLastVarDepSource());*/ - } - } - //if(aCandidate>0){ - if(iCheckInfo._reasonCode > 0){ - if(((_linkedElem instanceof TMLReadChannel || _linkedElem instanceof TMLWriteChannel) && !chOfCmdAllNBRNBW((TMLActivityElementChannel)_linkedElem)) || _linkedElem instanceof TMLSendEvent || _linkedElem instanceof TMLWaitEvent || _linkedElem instanceof TMLSendRequest){ - //_checkpoint=aCandidate; - _checkpoint = iCheckInfo; - }else{ - _guard=true; - for(LiveVariableNode aSucc: _succList) - aSucc.determineCheckpoints(iCheckInfo); - _guard = false; - } - } - }else - //_checkpoint |= iPropagate; - _checkpoint.incorporate(iCheckInfo); - } - } - - public void reachingDefinitionsInit(){ - invalidate(); - for (int i=0; i<_analysis.getBytesForDefs();i++){ - _outDefs[i] = _generateDefs[i] & (~_killDefs[i]); - } - //TraceManager.addDev("out Defs " + getStatementDescr() + " " + _outDefs[0] + " generate Defs: " + _generateDefs[0] + " kill Defs: " + _killDefs[0]); - } - - public boolean reachingDefinitionAnalysis(){ - boolean aSuccInvalidated=false; - if (!_valid){ - _valid=true; - for(LiveVariableNode aPred: _predList) { - int[] aPredOutDefs = aPred.getOutDefs(); - for (int i=0; i<_analysis.getBytesForDefs();i++){ - _inDefs[i] |= aPredOutDefs[i]; - int aOutDefsOld=_outDefs[i]; - _outDefs[i] = _generateDefs[i] | _inDefs[i] & (~_killDefs[i]); - if (aOutDefsOld!=_outDefs[i]) aSuccInvalidated=true; - } - } - if(aSuccInvalidated){ - for(LiveVariableNode aSucc: _succList) - aSucc.invalidate(); - } - } - return aSuccInvalidated; - } - - public boolean liveVariableAnalysis(){ - boolean aPredInvalidated=false; - if (!_valid){ - _valid=true; + } + } + //if(aCandidate>0){ + if (iCheckInfo._reasonCode > 0) { + if (((_linkedElem instanceof TMLReadChannel || _linkedElem instanceof TMLWriteChannel) && !chOfCmdAllNBRNBW((TMLActivityElementChannel) _linkedElem)) || _linkedElem instanceof TMLSendEvent || _linkedElem instanceof TMLWaitEvent || _linkedElem instanceof TMLSendRequest) { + //_checkpoint=aCandidate; + _checkpoint = iCheckInfo; + } else { + _guard = true; + for (LiveVariableNode aSucc : _succList) + aSucc.determineCheckpoints(iCheckInfo); + _guard = false; + } + } + } else + //_checkpoint |= iPropagate; + _checkpoint.incorporate(iCheckInfo); + } + } + + public void reachingDefinitionsInit() { + invalidate(); + for (int i = 0; i < _analysis.getBytesForDefs(); i++) { + _outDefs[i] = _generateDefs[i] & (~_killDefs[i]); + } + //TraceManager.addDev("out Defs " + getStatementDescr() + " " + _outDefs[0] + " generate Defs: " + _generateDefs[0] + " kill Defs: " + _killDefs[0]); + } + + public boolean reachingDefinitionAnalysis() { + boolean aSuccInvalidated = false; + if (!_valid) { + _valid = true; + for (LiveVariableNode aPred : _predList) { + int[] aPredOutDefs = aPred.getOutDefs(); + for (int i = 0; i < _analysis.getBytesForDefs(); i++) { + _inDefs[i] |= aPredOutDefs[i]; + int aOutDefsOld = _outDefs[i]; + _outDefs[i] = _generateDefs[i] | _inDefs[i] & (~_killDefs[i]); + if (aOutDefsOld != _outDefs[i]) aSuccInvalidated = true; + } + } + if (aSuccInvalidated) { + for (LiveVariableNode aSucc : _succList) + aSucc.invalidate(); + } + } + return aSuccInvalidated; + } + + public boolean liveVariableAnalysis() { + boolean aPredInvalidated = false; + if (!_valid) { + _valid = true; /*if (_linkedElem==null) System.out.print("recalculate waitRequest invalidate: "); else System.out.print("recalculate " + _linkedElem.getName() + " " + _linkedElem.getID() + "invalidate: ");*/ - for(LiveVariableNode aSucc: _succList) { - int[] aSuccInVars = aSucc.getInVars(); - for (int i=0; i<aSuccInVars.length;i++){ - _outVars[i] |= aSuccInVars[i]; - int aInVarsOld=_inVars[i]; - //_inVars[i] = _useVars[i] | _outVars[i] & _defNegVars[i]; - _inVars[i] = _useVars[i] | _outVars[i] & (~_defVars[i]); - if (aInVarsOld!=_inVars[i]) aPredInvalidated=true; - } - } - if(aPredInvalidated){ - for(LiveVariableNode aPred: _predList){ - aPred.invalidate(); + for (LiveVariableNode aSucc : _succList) { + int[] aSuccInVars = aSucc.getInVars(); + for (int i = 0; i < aSuccInVars.length; i++) { + _outVars[i] |= aSuccInVars[i]; + int aInVarsOld = _inVars[i]; + //_inVars[i] = _useVars[i] | _outVars[i] & _defNegVars[i]; + _inVars[i] = _useVars[i] | _outVars[i] & (~_defVars[i]); + if (aInVarsOld != _inVars[i]) aPredInvalidated = true; + } + } + if (aPredInvalidated) { + for (LiveVariableNode aPred : _predList) { + aPred.invalidate(); /*if (aPred.getLinkedElement()==null) System.out.print("waitRequest, "); else System.out.print(aPred.getLinkedElement().getID() + ", ");*/ - } - } - //TraceManager.addDev(); - } - return aPredInvalidated; - } - - public boolean infectionAnalysis(){ - //if (_linkedElem!=null) TraceManager.addDev("Infection Analysis: "+ _linkedElem.getID()); - if (_infected){ - //TraceManager.addDev("cancelled"); - return false; - }else{ - if (_superiorBranchNode!=null && _superiorBranchNode.isInfected()){ - //control dependency - _infected=true; - }else{ - //data dependency - int[] aRelevantDefs = new int[_analysis.getBytesForDefs()]; - for (int bytes=0; bytes<_useVars.length; bytes++){ - for (int bits=0; bits<32;bits++){ - if ((_useVars[bytes] & (1<< bits))!=0){ //for all variables which are used in this statement - for (int i=0; i<_analysis.getBytesForDefs();i++){ - aRelevantDefs[i] |=_inDefs[i] & _analysis.getDefsForVar()[(bytes << 5)|bits][i]; - } - } - } - } - _infected |= (defsInfected(aRelevantDefs)<0); - } - } - //synchronization dependency - if (_infected && _linkedElem!=null){ - //TraceManager.addDev("in if 1"); - if(_linkedElem instanceof TMLReadChannel || _linkedElem instanceof TMLWriteChannel){ - //TraceManager.addDev("in if 2"); - for (int i=0; i<((TMLActivityElementChannel)_linkedElem).getNbOfChannels(); i++){ - //TraceManager.addDev("in loop"); - TMLChannel aChannel = ((TMLActivityElementChannel)_linkedElem).getChannel(i); - //TraceManager.addDev("Set Infected for channel " + aChannel.getName()); - //if (aChannel.getType()!=TMLChannel.NBRNBW) aChannel.setInfected(true); - if (aChannel.getType()!=TMLChannel.NBRNBW) _analysis.addDepChannel(aChannel.getID()); - } - }else if(_linkedElem instanceof TMLSendEvent || _linkedElem instanceof TMLWaitEvent){ - //((TMLActivityElementEvent)_linkedElem).getEvent().setInfected(true); - _analysis.addDepChannel(((TMLActivityElementEvent)_linkedElem).getEvent().getID()); - }else if (_linkedElem instanceof TMLSendRequest){ - //((TMLSendRequest)_linkedElem).getRequest().setInfected(true); - _analysis.addDepChannel(((TMLSendRequest)_linkedElem).getRequest().getID()); - } - } - return _infected; - } - - private int defsInfected(int[] iDefs){ - boolean aInfected=false; - int aNbOfDefs=0; - for (int bytes=0; bytes<iDefs.length; bytes++){ - for (int bits=0; bits<32;bits++){ - if ((iDefs[bytes] & (1<< bits))!=0){ - aNbOfDefs++; - aInfected |= _analysis.getDefLookUp()[(bytes << 5)|bits].isInfected(); - } - } - } - return (aInfected)? -aNbOfDefs : aNbOfDefs; - } - - - public TMLActivityElement getLinkedElement(){ - return _linkedElem; - } - - public boolean isEmptyNode(){ - return false; + } + } + //TraceManager.addDev(); + } + return aPredInvalidated; + } + + public boolean infectionAnalysis() { + //if (_linkedElem!=null) TraceManager.addDev("Infection Analysis: "+ _linkedElem.getID()); + if (_infected) { + //TraceManager.addDev("cancelled"); + return false; + } else { + if (_superiorBranchNode != null && _superiorBranchNode.isInfected()) { + //control dependency + _infected = true; + } else { + //data dependency + int[] aRelevantDefs = new int[_analysis.getBytesForDefs()]; + for (int bytes = 0; bytes < _useVars.length; bytes++) { + for (int bits = 0; bits < 32; bits++) { + if ((_useVars[bytes] & (1 << bits)) != 0) { //for all variables which are used in this statement + for (int i = 0; i < _analysis.getBytesForDefs(); i++) { + aRelevantDefs[i] |= _inDefs[i] & _analysis.getDefsForVar()[(bytes << 5) | bits][i]; + } + } + } + } + _infected |= (defsInfected(aRelevantDefs) < 0); + } + } + //synchronization dependency + if (_infected && _linkedElem != null) { + //TraceManager.addDev("in if 1"); + if (_linkedElem instanceof TMLReadChannel || _linkedElem instanceof TMLWriteChannel) { + //TraceManager.addDev("in if 2"); + for (int i = 0; i < ((TMLActivityElementChannel) _linkedElem).getNbOfChannels(); i++) { + //TraceManager.addDev("in loop"); + TMLChannel aChannel = ((TMLActivityElementChannel) _linkedElem).getChannel(i); + //TraceManager.addDev("Set Infected for channel " + aChannel.getName()); + //if (aChannel.getType()!=TMLChannel.NBRNBW) aChannel.setInfected(true); + if (aChannel.getType() != TMLChannel.NBRNBW) _analysis.addDepChannel(aChannel.getID()); + } + } else if (_linkedElem instanceof TMLSendEvent || _linkedElem instanceof TMLWaitEvent) { + //((TMLActivityElementEvent)_linkedElem).getEvent().setInfected(true); + _analysis.addDepChannel(((TMLActivityElementEvent) _linkedElem).getEvent().getID()); + } else if (_linkedElem instanceof TMLSendRequest) { + //((TMLSendRequest)_linkedElem).getRequest().setInfected(true); + _analysis.addDepChannel(((TMLSendRequest) _linkedElem).getRequest().getID()); + } + } + return _infected; + } + + private int defsInfected(int[] iDefs) { + boolean aInfected = false; + int aNbOfDefs = 0; + for (int bytes = 0; bytes < iDefs.length; bytes++) { + for (int bits = 0; bits < 32; bits++) { + if ((iDefs[bytes] & (1 << bits)) != 0) { + aNbOfDefs++; + aInfected |= _analysis.getDefLookUp()[(bytes << 5) | bits].isInfected(); + } + } + } + return (aInfected) ? -aNbOfDefs : aNbOfDefs; + } + + + public TMLActivityElement getLinkedElement() { + return _linkedElem; + } + + public boolean isEmptyNode() { + return false; /*if (_succList.size()>1) return false; for (int i=0; i<_defVars.length;i++) if (_defVars[i]!=0) return false; for (int i=0; i<_useVars.length;i++) if (_useVars[i]!=0) return false; return true;*/ - } - - public void setSuccessor(LiveVariableNode iSucc){ - if(iSucc!=null){ - _succList.add(iSucc); - iSucc.setPredecessor(this); - } - } - - private void setPredecessor(LiveVariableNode iPred){ - _predList.add(iPred); - //if (_linkedElem!=null) TraceManager.addDev(_linkedElem.getID() + " adds pred " + iPred._myDefID); - } - - public int[] getInVars(){ - return _inVars; - } - - public int[] getOutVars(){ - return _outVars; - } - - public int[] getInDefs(){ - return _inDefs; - } - - public int[] getOutDefs(){ - return _outDefs; - } - - public int[] getInitialDefVars(){ - return _initialDefVars; - } - - private boolean canBeRemoved(){ - //if ((_lhs==null || _lhs.isEmpty()) && (_rhs==null || _rhs.isEmpty()) && !(_linkedElem==null || _linkedElem instanceof TMLNotifiedEvent || _linkedElem instanceof TMLRandom)) return false; - for (int i=0; i<_defVars.length;i++){ - if ((_defVars[i] & _outVars[i]) !=0) return false; - } - return true; - } - - public String getLiveVariableString(){ + } + + public void setSuccessor(LiveVariableNode iSucc) { + if (iSucc != null) { + _succList.add(iSucc); + iSucc.setPredecessor(this); + } + } + + private void setPredecessor(LiveVariableNode iPred) { + _predList.add(iPred); + //if (_linkedElem!=null) TraceManager.addDev(_linkedElem.getID() + " adds pred " + iPred._myDefID); + } + + public int[] getInVars() { + return _inVars; + } + + public int[] getOutVars() { + return _outVars; + } + + public int[] getInDefs() { + return _inDefs; + } + + public int[] getOutDefs() { + return _outDefs; + } + + public int[] getInitialDefVars() { + return _initialDefVars; + } + + private boolean canBeRemoved() { + //if ((_lhs==null || _lhs.isEmpty()) && (_rhs==null || _rhs.isEmpty()) && !(_linkedElem==null || _linkedElem instanceof TMLNotifiedEvent || _linkedElem instanceof TMLRandom)) return false; + for (int i = 0; i < _defVars.length; i++) { + if ((_defVars[i] & _outVars[i]) != 0) return false; + } + return true; + } + + public String getLiveVariableString() { /*System.out.print("Numbers: "); for (int aPos=0; aPos<_outVars.length; aPos++){ System.out.print(Integer.toHexString(_outVars[aPos]) + ", "); } TraceManager.addDev();*/ - return intArrayToHexString(_outVars); - } - - public String getStartLiveVariableString(){ - return intArrayToHexString(_inVars); - } - - public String getStateModificationString(){ - return intArrayToHexString(_defVars); - } - - private String intArrayToHexString(int[] iArray){ - String aResult = "\""; - //\\x" + Integer.toHexString(iArray.length); - for (int aPos=0; aPos<iArray.length; aPos++){ - int anItem = iArray[aPos]; - for (int bytes=0; bytes<4; bytes++){ - //for (int bits=0; bits<32; bits+=8) - aResult+= "\\x" + Integer.toHexString(anItem & 0xFF); - anItem >>>= 8; - } - } - aResult+="\""; - //TraceManager.addDev("String: " + aResult); - return aResult; - } - - public boolean isCheckpoint(){ - return (_checkpoint!=null); - } + return intArrayToHexString(_outVars); + } + + public String getStartLiveVariableString() { + return intArrayToHexString(_inVars); + } + + public String getStateModificationString() { + return intArrayToHexString(_defVars); + } + + private String intArrayToHexString(int[] iArray) { + String aResult = "\""; + //\\x" + Integer.toHexString(iArray.length); + for (int aPos = 0; aPos < iArray.length; aPos++) { + int anItem = iArray[aPos]; + for (int bytes = 0; bytes < 4; bytes++) { + //for (int bits=0; bits<32; bits+=8) + aResult += "\\x" + Integer.toHexString(anItem & 0xFF); + anItem >>>= 8; + } + } + aResult += "\""; + //TraceManager.addDev("String: " + aResult); + return aResult; + } + + public boolean isCheckpoint() { + return (_checkpoint != null); + } } diff --git a/src/main/java/tmltranslator/tomappingsystemc2/StaticAnalysis.java b/src/main/java/tmltranslator/tomappingsystemc2/StaticAnalysis.java index 8b1d4770a9..27c40bcb8a 100644 --- a/src/main/java/tmltranslator/tomappingsystemc2/StaticAnalysis.java +++ b/src/main/java/tmltranslator/tomappingsystemc2/StaticAnalysis.java @@ -37,7 +37,6 @@ */ - package tmltranslator.tomappingsystemc2; import myutil.TraceManager; @@ -51,112 +50,112 @@ import java.util.regex.Matcher; import java.util.regex.Pattern; //import java.util.HashSet; -public class StaticAnalysis{ - private List<LiveVariableNode> liveNodes= new ArrayList<LiveVariableNode>(); - private final static Pattern _varPattern = Pattern.compile("[\\w&&\\D]+[\\w]*"); - private TMLTask _task; - private List<TMLChannel> _channels; - private List<TMLEvent> _events; - private List<TMLRequest> _requests; - - private int _nextDefID=0; - private int[][] _varToDefs=null; - private int _bytesForDefs; - private int _bytesForVars; - private LiveVariableNode[] _defLookup=null; - private Set<Integer> _depChannels; - - public StaticAnalysis(TMLTask iTask, List<TMLChannel>iChannels, List<TMLEvent> iEvents, List<TMLRequest> iRequests, Set<Integer> iDepChannels){ - _task = iTask; - _channels=iChannels; - _events=iEvents; - _requests=iRequests; - _depChannels = iDepChannels; - } - - public int getNextDefID(){ - return _nextDefID++; - } - +public class StaticAnalysis { + private List<LiveVariableNode> liveNodes = new ArrayList<LiveVariableNode>(); + private final static Pattern _varPattern = Pattern.compile("[\\w&&\\D]+[\\w]*"); + private TMLTask _task; + private List<TMLChannel> _channels; + private List<TMLEvent> _events; + private List<TMLRequest> _requests; + + private int _nextDefID = 0; + private int[][] _varToDefs = null; + private int _bytesForDefs; + private int _bytesForVars; + private LiveVariableNode[] _defLookup = null; + private Set<Integer> _depChannels; + + public StaticAnalysis(TMLTask iTask, List<TMLChannel> iChannels, List<TMLEvent> iEvents, List<TMLRequest> iRequests, Set<Integer> iDepChannels) { + _task = iTask; + _channels = iChannels; + _events = iEvents; + _requests = iRequests; + _depChannels = iDepChannels; + } + + public int getNextDefID() { + return _nextDefID++; + } + /*public int getNbOfDefs(){ return _nextDefID; }*/ - - public int getBytesForDefs(){ - return _bytesForDefs; - } - - public int[][] getDefsForVar(){ - return _varToDefs; - } - - public LiveVariableNode[] getDefLookUp(){ - return _defLookup; - } - - public void addDepChannel(int iID){ - //TraceManager.addDev("Add Dependent Channel: " + iID); - _depChannels.add(iID); - } - - public boolean isChannelDep(int iID){ - //TraceManager.addDev("Check if Channel dep: " + iID + " answer: " + _depChannels.contains(iID)); - return _depChannels.contains(iID); - } - - private int getVarSeqNoByName(String attName){ - attName=attName.trim(); - int aSeq=0; - for(TMLAttribute att: _task.getAttributes()) { - if (att.name.equals(attName)) return aSeq; - aSeq++; - } - return -1; - } - - private int[] parseExprToVariableMap(String iExpr, int[] iMap){ - //byte[] aResMap; - int[] aResMap; - if(iMap==null) - aResMap=new int[_bytesForVars]; - else - aResMap=iMap; - if(!(iExpr==null || iExpr.isEmpty())){ - //TraceManager.addDev("Examine expression: " + iExpr); - Matcher matcher = _varPattern.matcher(iExpr); - //System.out.print("Found tokens: "); - while (matcher.find()){ - String token = iExpr.substring(matcher.start(), matcher.end()); - //System.out.print(token + ", "); - int aVarSeqNo = getVarSeqNoByName(token); - if (aVarSeqNo>=0) aResMap[aVarSeqNo >>> 5] |= 1 << (aVarSeqNo & 0x1F); - } - //TraceManager.addDev(); - //TraceManager.addDev("Byte sequence: "); - } + + public int getBytesForDefs() { + return _bytesForDefs; + } + + public int[][] getDefsForVar() { + return _varToDefs; + } + + public LiveVariableNode[] getDefLookUp() { + return _defLookup; + } + + public void addDepChannel(int iID) { + //TraceManager.addDev("Add Dependent Channel: " + iID); + _depChannels.add(iID); + } + + public boolean isChannelDep(int iID) { + //TraceManager.addDev("Check if Channel dep: " + iID + " answer: " + _depChannels.contains(iID)); + return _depChannels.contains(iID); + } + + private int getVarSeqNoByName(String attName) { + attName = attName.trim(); + int aSeq = 0; + for (TMLAttribute att : _task.getAttributes()) { + if (att.name.equals(attName)) return aSeq; + aSeq++; + } + return -1; + } + + private int[] parseExprToVariableMap(String iExpr, int[] iMap) { + //byte[] aResMap; + int[] aResMap; + if (iMap == null) + aResMap = new int[_bytesForVars]; + else + aResMap = iMap; + if (!(iExpr == null || iExpr.isEmpty())) { + //TraceManager.addDev("Examine expression: " + iExpr); + Matcher matcher = _varPattern.matcher(iExpr); + //System.out.print("Found tokens: "); + while (matcher.find()) { + String token = iExpr.substring(matcher.start(), matcher.end()); + //System.out.print(token + ", "); + int aVarSeqNo = getVarSeqNoByName(token); + if (aVarSeqNo >= 0) aResMap[aVarSeqNo >>> 5] |= 1 << (aVarSeqNo & 0x1F); + } + //TraceManager.addDev(); + //TraceManager.addDev("Byte sequence: "); + } /*for(int i=0; i<_bytesForVars; i++) System.out.print(((int)aResMap[i]) + ", "); TraceManager.addDev();*/ - return aResMap; - } - - private int[] parseChannelToVariableMap(TMLChannel iChannel, int[] iMap){ - int[] aResMap; - if(iMap==null) - aResMap=new int[_bytesForVars]; - else - aResMap=iMap; - int anIndex = _task.getAttributes().size() + _channels.indexOf(iChannel); - aResMap[anIndex >>> 5] |= 1 << (anIndex & 0x1F); - return aResMap; - } - - private int[] parseEventToVariableMap(TMLEvent iEvent){ - int[] aResMap = new int[_bytesForVars]; - int anIndex = _task.getAttributes().size() + _channels.size() + _events.indexOf(iEvent); - aResMap[anIndex >>> 5] |= 1 << (anIndex & 0x1F); - return aResMap; - } + return aResMap; + } + + private int[] parseChannelToVariableMap(TMLChannel iChannel, int[] iMap) { + int[] aResMap; + if (iMap == null) + aResMap = new int[_bytesForVars]; + else + aResMap = iMap; + int anIndex = _task.getAttributes().size() + _channels.indexOf(iChannel); + aResMap[anIndex >>> 5] |= 1 << (anIndex & 0x1F); + return aResMap; + } + + private int[] parseEventToVariableMap(TMLEvent iEvent) { + int[] aResMap = new int[_bytesForVars]; + int anIndex = _task.getAttributes().size() + _channels.size() + _events.indexOf(iEvent); + aResMap[anIndex >>> 5] |= 1 << (anIndex & 0x1F); + return aResMap; + } /*public String makeLiveVarEvalFunc(){ String anEvalFunc=""; @@ -201,109 +200,112 @@ public class StaticAnalysis{ // TraceManager.addDev("reqChannel: significant"); // } - public String printVariables(int[] iVariables){ - String aResult=""; - int aSeq=0; - for(TMLAttribute att: _task.getAttributes()) { - if ((iVariables[aSeq >>> 5] & (1 << (aSeq & 0x1F)))!=0) - aResult+= att.getName() + ","; - aSeq++; - } - for(TMLChannel ch: _channels) { - if ((iVariables[aSeq >>> 5] & (1 << (aSeq & 0x1F)))!=0) - aResult+= ch.getName() + ","; - aSeq++; - } - for(TMLEvent evt: _events) { - if ((iVariables[aSeq >>> 5] & (1 << (aSeq & 0x1F)))!=0) - aResult+= evt.getName() + ","; - aSeq++; - } - if (_task.isRequested() && (iVariables[aSeq >>> 5] & (1 << (aSeq & 0x1F)))!=0) - aResult += "reqCh"; - return aResult; - } - - public LiveVariableNode startAnalysis(){ - int aNbOfLiveElements = _task.getAttributes().size() + _channels.size() + _events.size(); - //int aNumberOfBytes=0; - //LiveVariableNode.reset(); - liveNodes = new ArrayList<LiveVariableNode>(); - if (_task.isRequested()) aNbOfLiveElements++; - _bytesForVars = aNbOfLiveElements >>> 5; - if ((aNbOfLiveElements & 0x1F)!=0) _bytesForVars++; - LiveVariableNode aStartNode=null, aLastNode=null; - //_depChannels.clear(); - for(TMLAttribute att: _task.getAttributes()) { - if (att.hasInitialValue()) - aStartNode = new LiveVariableNode(this, new int[_bytesForVars], parseExprToVariableMap(att.name, null), null, null, - false, att.name, att.initialValue); - else - aStartNode = new LiveVariableNode(this, new int[_bytesForVars], parseExprToVariableMap(att.name, null), null, null, - false, att.name, "0"); - liveNodes.add(aStartNode); - aStartNode.setNodeInfo("init " + att.getName()); - if (aLastNode!=null) aLastNode.setSuccessor(aStartNode); - aLastNode=aStartNode; - } - if (_task.isRequested()){ - int[] aReqVars = parseExprToVariableMap("arg1__req", null); - for (int i=2; i<=_task.getRequest().getNbOfParams(); i++){ - parseExprToVariableMap("arg" + i + "__req", aReqVars); - } - //parseExprToVariableMap("arg2__req", aReqVars); - //parseExprToVariableMap("arg3__req", aReqVars); - int[] aReqChannelVar = new int[_bytesForVars]; - aReqChannelVar[_bytesForVars-1] = 1 << ((aNbOfLiveElements-1) & 0x1F); - aStartNode = new LiveVariableNode(this, aReqChannelVar, aReqVars, null, null, true); - for(TMLRequest aReq: _requests) - if (aReq.getNbOfParams()>0){ aStartNode.setVarDepSource(true); break;} - liveNodes.add(aStartNode); - aStartNode.setNodeInfo("rec req"); - aStartNode.setSuccessor(buildLiveAnalysisTree(_task.getActivityDiagram().getFirst(), aStartNode, null)); - }else{ - aStartNode = buildLiveAnalysisTree(_task.getActivityDiagram().getFirst(), null, null); - } - if (aLastNode!=null) aLastNode.setSuccessor(aStartNode); - boolean aChange; - int aConstChange; - do{ - //TraceManager.addDev("******************* one analysis round"); - _nextDefID=0; - for(LiveVariableNode aLiveNode: liveNodes) - aLiveNode.liveVariableInit(); - do{ - aChange=false; - for(LiveVariableNode aLiveNode: liveNodes) - aChange |= aLiveNode.liveVariableAnalysis(); - }while(aChange); - _bytesForDefs = _nextDefID >>> 5; - if ((_nextDefID & 0x1F)!=0 || _nextDefID==0) _bytesForDefs++; - _varToDefs = new int[aNbOfLiveElements][_bytesForDefs]; - _defLookup = new LiveVariableNode[_nextDefID]; - for(LiveVariableNode aLiveNode: liveNodes){ - aLiveNode.prepareReachingDefinitions(); - } - do{ - aChange=false; - for(LiveVariableNode aLiveNode: liveNodes) - aChange |= aLiveNode.determineKilledSets(); - }while(aChange); - for(LiveVariableNode aLiveNode: liveNodes){ - //aLiveNode.printKillEntries(); - aLiveNode.reachingDefinitionsInit(); - } - do{ - aChange=false; - for(LiveVariableNode aLiveNode: liveNodes) - aChange |= aLiveNode.reachingDefinitionAnalysis(); - }while(aChange); - aConstChange=0; - do{ - aConstChange &= 1; - for(LiveVariableNode aLiveNode: liveNodes) - aConstChange |= aLiveNode.determineIfConstant(); - }while(aConstChange>1); + public String printVariables(int[] iVariables) { + String aResult = ""; + int aSeq = 0; + for (TMLAttribute att : _task.getAttributes()) { + if ((iVariables[aSeq >>> 5] & (1 << (aSeq & 0x1F))) != 0) + aResult += att.getName() + ","; + aSeq++; + } + for (TMLChannel ch : _channels) { + if ((iVariables[aSeq >>> 5] & (1 << (aSeq & 0x1F))) != 0) + aResult += ch.getName() + ","; + aSeq++; + } + for (TMLEvent evt : _events) { + if ((iVariables[aSeq >>> 5] & (1 << (aSeq & 0x1F))) != 0) + aResult += evt.getName() + ","; + aSeq++; + } + if (_task.isRequested() && (iVariables[aSeq >>> 5] & (1 << (aSeq & 0x1F))) != 0) + aResult += "reqCh"; + return aResult; + } + + public LiveVariableNode startAnalysis() { + int aNbOfLiveElements = _task.getAttributes().size() + _channels.size() + _events.size(); + //int aNumberOfBytes=0; + //LiveVariableNode.reset(); + liveNodes = new ArrayList<LiveVariableNode>(); + if (_task.isRequested()) aNbOfLiveElements++; + _bytesForVars = aNbOfLiveElements >>> 5; + if ((aNbOfLiveElements & 0x1F) != 0) _bytesForVars++; + LiveVariableNode aStartNode = null, aLastNode = null; + //_depChannels.clear(); + for (TMLAttribute att : _task.getAttributes()) { + if (att.hasInitialValue()) + aStartNode = new LiveVariableNode(this, new int[_bytesForVars], parseExprToVariableMap(att.name, null), null, null, + false, att.name, att.initialValue); + else + aStartNode = new LiveVariableNode(this, new int[_bytesForVars], parseExprToVariableMap(att.name, null), null, null, + false, att.name, "0"); + liveNodes.add(aStartNode); + aStartNode.setNodeInfo("init " + att.getName()); + if (aLastNode != null) aLastNode.setSuccessor(aStartNode); + aLastNode = aStartNode; + } + if (_task.isRequested()) { + int[] aReqVars = parseExprToVariableMap("arg1__req", null); + for (int i = 2; i <= _task.getRequest().getNbOfParams(); i++) { + parseExprToVariableMap("arg" + i + "__req", aReqVars); + } + //parseExprToVariableMap("arg2__req", aReqVars); + //parseExprToVariableMap("arg3__req", aReqVars); + int[] aReqChannelVar = new int[_bytesForVars]; + aReqChannelVar[_bytesForVars - 1] = 1 << ((aNbOfLiveElements - 1) & 0x1F); + aStartNode = new LiveVariableNode(this, aReqChannelVar, aReqVars, null, null, true); + for (TMLRequest aReq : _requests) + if (aReq.getNbOfParams() > 0) { + aStartNode.setVarDepSource(true); + break; + } + liveNodes.add(aStartNode); + aStartNode.setNodeInfo("rec req"); + aStartNode.setSuccessor(buildLiveAnalysisTree(_task.getActivityDiagram().getFirst(), aStartNode, null)); + } else { + aStartNode = buildLiveAnalysisTree(_task.getActivityDiagram().getFirst(), null, null); + } + if (aLastNode != null) aLastNode.setSuccessor(aStartNode); + boolean aChange; + int aConstChange; + do { + //TraceManager.addDev("******************* one analysis round"); + _nextDefID = 0; + for (LiveVariableNode aLiveNode : liveNodes) + aLiveNode.liveVariableInit(); + do { + aChange = false; + for (LiveVariableNode aLiveNode : liveNodes) + aChange |= aLiveNode.liveVariableAnalysis(); + } while (aChange); + _bytesForDefs = _nextDefID >>> 5; + if ((_nextDefID & 0x1F) != 0 || _nextDefID == 0) _bytesForDefs++; + _varToDefs = new int[aNbOfLiveElements][_bytesForDefs]; + _defLookup = new LiveVariableNode[_nextDefID]; + for (LiveVariableNode aLiveNode : liveNodes) { + aLiveNode.prepareReachingDefinitions(); + } + do { + aChange = false; + for (LiveVariableNode aLiveNode : liveNodes) + aChange |= aLiveNode.determineKilledSets(); + } while (aChange); + for (LiveVariableNode aLiveNode : liveNodes) { + //aLiveNode.printKillEntries(); + aLiveNode.reachingDefinitionsInit(); + } + do { + aChange = false; + for (LiveVariableNode aLiveNode : liveNodes) + aChange |= aLiveNode.reachingDefinitionAnalysis(); + } while (aChange); + aConstChange = 0; + do { + aConstChange &= 1; + for (LiveVariableNode aLiveNode : liveNodes) + aConstChange |= aLiveNode.determineIfConstant(); + } while (aConstChange > 1); /*TraceManager.addDev("-----------------new step------------"); for(LiveVariableNode aLiveNode: liveNodes){ @@ -312,268 +314,270 @@ public class StaticAnalysis{ }*/ - - }while(aConstChange>0); - - do{ - aChange=false; - for(LiveVariableNode aLiveNode: liveNodes) - aChange |= aLiveNode.infectionAnalysis(); - }while(aChange); - return aStartNode; - } - - public void determineCheckpoints(int[] iStatistics){ - TraceManager.addDev("*** Static Analysis for task " + _task.getName()); - for(LiveVariableNode aLiveNode: liveNodes) - aLiveNode.determineCheckpoints(new CheckpointInfo()); - //TraceManager.addDev("Create array size " + (_bytesForVars << 5)); - int[] aStatistics = new int[_bytesForVars << 5]; - int aNbOfCheckPoints=0, aNbOfCandidates=0; - for(LiveVariableNode aLiveNode: liveNodes){ - int aStatResult = aLiveNode.varStatistics(aStatistics); - if((aStatResult & 1)!=0){ - //aLiveNode.printReachingEntries(); - //printLiveVarNode(aLiveNode); - aNbOfCandidates++; - if((aStatResult & 2)!=0) aNbOfCheckPoints++; - } - } - if (aNbOfCandidates==0) - TraceManager.addDev("No checkpoint candidates"); - else{ - TraceManager.addDev("a: " + _task.getAttributes().size() + " c: " + _channels.size() + " e: " + _events.size()); - //int aNbOfLiveElements = _task.getAttributeList().size() + _channels.size() + _events.size(); - int nbOfVars=0, nbOfChannels=0, nbOfEvents=0; - for(int i=0; i<_task.getAttributes().size(); i++) - nbOfVars += aStatistics[i]; - TraceManager.addDev("Variables Checks: " + nbOfVars + " Candidates: " + (_task.getAttributes().size() * aNbOfCandidates)); - if (!_task.getAttributes().isEmpty()){ - iStatistics[0] += _task.getAttributes().size() * aNbOfCandidates; - int aVarGain = (100 * (_task.getAttributes().size() * aNbOfCandidates - nbOfVars) / (_task.getAttributes().size() * aNbOfCandidates)); - iStatistics[1] += _task.getAttributes().size() * aNbOfCandidates - nbOfVars; - TraceManager.addDev("Variables Gain: " + aVarGain); - } - for(int i=0; i<_channels.size(); i++) - nbOfChannels += aStatistics[i + _task.getAttributes().size()]; - TraceManager.addDev("Channel Checks: " + nbOfChannels + " Candidates: " + (_channels.size() * aNbOfCandidates)); - if (!_channels.isEmpty()){ - iStatistics[2] += _channels.size() * aNbOfCandidates; - int aChGain = (100 * (_channels.size() * aNbOfCandidates - nbOfChannels) / (_channels.size() * aNbOfCandidates)); - iStatistics[3] += _channels.size() * aNbOfCandidates - nbOfChannels; - TraceManager.addDev("Channels Gain: " + aChGain); - } - for(int i=0; i<_events.size(); i++) - nbOfEvents += aStatistics[i + _task.getAttributes().size() + _channels.size()]; - TraceManager.addDev("Event Checks: " + nbOfEvents + " Candidates: " + (_events.size() * aNbOfCandidates)); - if (!_events.isEmpty()){ - iStatistics[4] += _events.size() * aNbOfCandidates; - int aEvtGain = (100 * (_events.size() * aNbOfCandidates - nbOfEvents) / (_events.size() * aNbOfCandidates)); - iStatistics[5] += _events.size() * aNbOfCandidates - nbOfEvents; - TraceManager.addDev("Events Gain: " + aEvtGain); - } - //TraceManager.addDev("Request Checks: " + aStatistics[_task.getAttributeList().size() + _channels.size() + _events.size()] + " Candidates: " + aNbOfCandidates); - //if (_task.isRequested()) TraceManager.addDev("Saved Requests: " + (100* (aNbOfCandidates - aStatistics[_task.getAttributeList().size() + _channels.size() + _events.size()]) / aNbOfCandidates)); - //TraceManager.addDev("Checkpoints: " + aNbOfCheckPoints + " Candidates: " + aNbOfCandidates); - iStatistics[6] += aNbOfCandidates; - iStatistics[7] += aNbOfCandidates - aNbOfCheckPoints; - TraceManager.addDev("Checkpoint Gain: " + (100 * (aNbOfCandidates - aNbOfCheckPoints) / aNbOfCandidates)); - } - TraceManager.addDev("*** End of Static Analysis for task " + _task.getName()); - } - - private LiveVariableNode buildLiveAnalysisTree(TMLActivityElement iCurrElem, LiveVariableNode iReturnNode, LiveVariableNode iSuperiorNode){ - LiveVariableNode aResNode=null; - - if (iCurrElem instanceof TMLStartState) { - iCurrElem.getNextElement(0); - } - - if (iCurrElem==null || iCurrElem instanceof TMLStopState){ - return iReturnNode; - - } else if (iCurrElem instanceof TMLActionState){ - String[] aTokens=MappedSystemCTask.formatAction(((TMLActionState)iCurrElem).getAction()).split("=",2); - if (aTokens.length<2) aTokens=new String[2]; - aResNode = new LiveVariableNode(this, parseExprToVariableMap(aTokens[1], null), parseExprToVariableMap(aTokens[0], null), iCurrElem, iSuperiorNode, - false, aTokens[0], aTokens[1]); - - } else if (iCurrElem instanceof TMLRandom){ - TMLRandom aRndCmd=(TMLRandom)iCurrElem; - int[] aRandomVars = parseExprToVariableMap(aRndCmd.getMinValue(), null); - parseExprToVariableMap(aRndCmd.getMaxValue(), aRandomVars); - aResNode = new LiveVariableNode(this, aRandomVars, parseExprToVariableMap(aRndCmd.getVariable(), null), iCurrElem, iSuperiorNode, true); - aResNode.setVarDepSource(true); - - } else if (iCurrElem instanceof TMLDelay){ - int[] aDelayVars = parseExprToVariableMap(((TMLDelay)iCurrElem).getMinDelay(), null); - parseExprToVariableMap(((TMLDelay)iCurrElem).getMaxDelay(), aDelayVars); - aResNode = new LiveVariableNode(this, aDelayVars, new int[_bytesForVars], iCurrElem, iSuperiorNode); - - } else if (iCurrElem instanceof TMLExecI){ - aResNode = new LiveVariableNode(this, parseExprToVariableMap(((TMLExecI)iCurrElem).getAction(), null), new int[_bytesForVars], iCurrElem, iSuperiorNode); - - } else if (iCurrElem instanceof TMLExecC){ - aResNode = new LiveVariableNode(this, parseExprToVariableMap(((TMLExecC)iCurrElem).getAction(), null), new int[_bytesForVars], iCurrElem, iSuperiorNode); - - } else if (iCurrElem instanceof TMLExecIInterval){ - int[] aExecVars = parseExprToVariableMap(((TMLExecIInterval)iCurrElem).getMinDelay(), null); - parseExprToVariableMap(((TMLExecIInterval)iCurrElem).getMaxDelay(), aExecVars); - aResNode = new LiveVariableNode(this, aExecVars, new int[_bytesForVars], iCurrElem, iSuperiorNode); - - } else if (iCurrElem instanceof TMLExecCInterval){ - int[] aExecVars = parseExprToVariableMap(((TMLExecCInterval)iCurrElem).getMinDelay(), null); - parseExprToVariableMap(((TMLExecCInterval)iCurrElem).getMaxDelay(), aExecVars); - aResNode = new LiveVariableNode(this, aExecVars, new int[_bytesForVars],iCurrElem, iSuperiorNode); - - } else if (iCurrElem instanceof TMLForLoop){ - TMLForLoop aLoop = (TMLForLoop) iCurrElem; - String[] aTokens=aLoop.getInit().split("=",2); - if (aTokens.length<2) aTokens=new String[2]; - LiveVariableNode aInit = new LiveVariableNode(this, parseExprToVariableMap(aTokens[1], null), parseExprToVariableMap(aTokens[0], null), iCurrElem, iSuperiorNode, false, aTokens[0], aTokens[1]); - aInit.setNodeInfo("init"); - LiveVariableNode aCondition = new LiveVariableNode(this, parseExprToVariableMap(aLoop.getCondition(), null), new int[_bytesForVars], iCurrElem, iSuperiorNode); - aCondition.setNodeInfo("cond"); - aTokens=aLoop.getIncrement().split("=",2); - if (aTokens.length<2) aTokens=new String[2]; - LiveVariableNode anIncrement = new LiveVariableNode(this, parseExprToVariableMap(aTokens[1], null), parseExprToVariableMap(aTokens[0],null), iCurrElem, aCondition, false, aTokens[0], aTokens[1]); - anIncrement.setNodeInfo("inc"); - aInit.setSuccessor(aCondition); - anIncrement.setSuccessor(aCondition); - aCondition.setSuccessor(buildLiveAnalysisTree(iCurrElem.getNextElement(0), anIncrement, aCondition)); //in loop - aCondition.setSuccessor(buildLiveAnalysisTree(iCurrElem.getNextElement(1), iReturnNode, aCondition)); //outside loop - liveNodes.add(aInit); - liveNodes.add(aCondition); - liveNodes.add(anIncrement); - return aInit; - - } else if (iCurrElem instanceof TMLReadChannel){ - TMLReadChannel aReadCmd = (TMLReadChannel)iCurrElem; - int[] aReadVars=null; - if (aReadCmd.getChannel(0).getType()==TMLChannel.BRBW || aReadCmd.getChannel(0).getType()==TMLChannel.BRNBW) - aReadVars = parseChannelToVariableMap(aReadCmd.getChannel(0), null); - else - aReadVars = new int[_bytesForVars]; - parseExprToVariableMap(aReadCmd.getNbOfSamples(), aReadVars); - aResNode = new LiveVariableNode(this, aReadVars, new int[_bytesForVars], iCurrElem, iSuperiorNode); - - } else if (iCurrElem instanceof TMLWriteChannel){ - TMLWriteChannel aWriteCmd=(TMLWriteChannel)iCurrElem; - int[] aWriteVars = new int[_bytesForVars]; - for(int i=0; i< aWriteCmd.getNbOfChannels(); i++){ - if (aWriteCmd.getChannel(i).getType()==TMLChannel.BRBW) parseChannelToVariableMap(aWriteCmd.getChannel(i), aWriteVars); - } - parseExprToVariableMap(aWriteCmd.getNbOfSamples(), aWriteVars); - aResNode = new LiveVariableNode(this, aWriteVars, new int[_bytesForVars], iCurrElem, iSuperiorNode); - - } else if (iCurrElem instanceof TMLSendEvent){ - TMLSendEvent aSendCmd=(TMLSendEvent)iCurrElem; - int[] aSendVars=null; - //if (aSendCmd.getEvent().isBlocking()) - aSendVars = parseEventToVariableMap(aSendCmd.getEvent()); - //else - //aSendVars = new int[_bytesForVars]; - //parseExprToVariableMap(aSendCmd.getParam(0), aSendVars); - //parseExprToVariableMap(aSendCmd.getParam(1), aSendVars); - //parseExprToVariableMap(aSendCmd.getParam(2), aSendVars); - for (int i=0; i<aSendCmd.getNbOfParams(); i++){ - parseExprToVariableMap(aSendCmd.getParam(i), aSendVars); - } - aResNode = new LiveVariableNode(this, aSendVars, new int[_bytesForVars], iCurrElem, iSuperiorNode); - - } else if (iCurrElem instanceof TMLSendRequest){ - TMLSendRequest aSendReqCmd=(TMLSendRequest)iCurrElem; - int[] aSendReqVars = parseExprToVariableMap(aSendReqCmd.getParam(0), null); - //parseExprToVariableMap(aSendReqCmd.getParam(1), aSendReqVars); - //parseExprToVariableMap(aSendReqCmd.getParam(2), aSendReqVars); - for (int i=1; i<aSendReqCmd.getNbOfParams(); i++){ - parseExprToVariableMap(aSendReqCmd.getParam(i), aSendReqVars); - } - aResNode = new LiveVariableNode(this, aSendReqVars, new int[_bytesForVars], iCurrElem, iSuperiorNode); - - } else if (iCurrElem instanceof TMLWaitEvent){ - TMLWaitEvent aWaitCmd=(TMLWaitEvent)iCurrElem; - int[] aWaitVars = parseExprToVariableMap(aWaitCmd.getParam(0), null); - //parseExprToVariableMap(aWaitCmd.getParam(1), aWaitVars); - //parseExprToVariableMap(aWaitCmd.getParam(2), aWaitVars); - for (int i=1; i<aWaitCmd.getNbOfParams(); i++){ - parseExprToVariableMap(aWaitCmd.getParam(i), aWaitVars); - } - aResNode = new LiveVariableNode(this, parseEventToVariableMap(aWaitCmd.getEvent()), aWaitVars, iCurrElem, iSuperiorNode, true); - if (aWaitCmd.getEvent().getNbOfParams()>0) aResNode.setVarDepSource(true); - - } else if (iCurrElem instanceof TMLNotifiedEvent){ - aResNode = new LiveVariableNode(this, parseEventToVariableMap(((TMLNotifiedEvent)iCurrElem).getEvent()), parseExprToVariableMap(((TMLNotifiedEvent)iCurrElem).getVariable(), null), iCurrElem, iSuperiorNode, true); - aResNode.setVarDepSource(true); - - } else if (iCurrElem instanceof TMLSequence){ - ((TMLSequence)iCurrElem).sortNexts(); - for(int i = iCurrElem.getNbNext()-1; i>=0; i--){ - iReturnNode = buildLiveAnalysisTree(iCurrElem.getNextElement(i), iReturnNode, iSuperiorNode); - } - return iReturnNode; - - } else if (iCurrElem instanceof TMLChoice){ - int[] aChoiceVars = new int[_bytesForVars]; - TMLChoice aChoiceCmd = (TMLChoice)iCurrElem; - for(int i=0; i<aChoiceCmd.getNbGuard(); i++) - if (!(aChoiceCmd.isNonDeterministicGuard(i) || aChoiceCmd.isStochasticGuard(i) || aChoiceCmd.getElseGuard()==i)) parseExprToVariableMap(aChoiceCmd.getGuard(i), aChoiceVars); - aResNode = new LiveVariableNode(this, aChoiceVars, new int[_bytesForVars], iCurrElem, iSuperiorNode); - if (aChoiceCmd.nbOfNonDeterministicGuard()>0 || aChoiceCmd.nbOfStochasticGuard()>0) aResNode.setInfected(true); - //TraceManager.addDev("checl: " + aChoiceCmd.nbOfNonDeterministicGuard() + " ** "+ aChoiceCmd.nbOfStochasticGuard()); - for(int i=0; i<aChoiceCmd.getNbNext(); i++) - aResNode.setSuccessor(buildLiveAnalysisTree(iCurrElem.getNextElement(i), iReturnNode, aResNode)); - liveNodes.add(aResNode); - return aResNode; - - } else if (iCurrElem instanceof TMLSelectEvt){ - aResNode = new LiveVariableNode(this, new int[_bytesForVars], new int[_bytesForVars], iCurrElem, iSuperiorNode); - if (iCurrElem.getNbNext()>1) aResNode.setInfected(true); - for(int i=0; i<iCurrElem.getNbNext(); i++){ - aResNode.setSuccessor(buildLiveAnalysisTree(iCurrElem.getNextElement(i), iReturnNode, aResNode)); - } - liveNodes.add(aResNode); - return aResNode; - } - LiveVariableNode aSucc = buildLiveAnalysisTree(iCurrElem.getNextElement(0), iReturnNode, iSuperiorNode); - if (aResNode==null || aResNode.isEmptyNode()){ - aResNode=aSucc; - }else{ - liveNodes.add(aResNode); - aResNode.setSuccessor(aSucc); - } - return aResNode; - } - - public LiveVariableNode getLiveVarNodeByCommand(TMLActivityElement iCmd){ - for(LiveVariableNode aLiveNode: liveNodes) - if (aLiveNode.getLinkedElement()==iCmd) return aLiveNode; - return null; - } - - public void getCommandsImpactingVar(String iVarName, HashSet<Integer> oList){ - //int[] aVarMap=new int[_bytesForVars]; - int aVarSeqNo = getVarSeqNoByName(iVarName); - if (aVarSeqNo>=0){ - //aVarMap[aVarSeqNo >>> 5] |= 1 << (aVarSeqNo & 0x1F); - int intPos = aVarSeqNo >>> 5; - int bitMask = 1 << (aVarSeqNo & 0x1F); - for(LiveVariableNode aLiveNode: liveNodes){ + } while (aConstChange > 0); + + do { + aChange = false; + for (LiveVariableNode aLiveNode : liveNodes) + aChange |= aLiveNode.infectionAnalysis(); + } while (aChange); + return aStartNode; + } + + public void determineCheckpoints(int[] iStatistics) { + //TraceManager.addDev("*** Static Analysis for task " + _task.getName()); + for (LiveVariableNode aLiveNode : liveNodes) + aLiveNode.determineCheckpoints(new CheckpointInfo()); + //TraceManager.addDev("Create array size " + (_bytesForVars << 5)); + int[] aStatistics = new int[_bytesForVars << 5]; + int aNbOfCheckPoints = 0, aNbOfCandidates = 0; + for (LiveVariableNode aLiveNode : liveNodes) { + int aStatResult = aLiveNode.varStatistics(aStatistics); + if ((aStatResult & 1) != 0) { + //aLiveNode.printReachingEntries(); + //printLiveVarNode(aLiveNode); + aNbOfCandidates++; + if ((aStatResult & 2) != 0) aNbOfCheckPoints++; + } + } + if (aNbOfCandidates == 0) { + //TraceManager.addDev("No checkpoint candidates"); + } + else { + //TraceManager.addDev("a: " + _task.getAttributes().size() + " c: " + _channels.size() + " e: " + _events.size()); + //int aNbOfLiveElements = _task.getAttributeList().size() + _channels.size() + _events.size(); + int nbOfVars = 0, nbOfChannels = 0, nbOfEvents = 0; + for (int i = 0; i < _task.getAttributes().size(); i++) + nbOfVars += aStatistics[i]; + //TraceManager.addDev("Variables Checks: " + nbOfVars + " Candidates: " + (_task.getAttributes().size() * aNbOfCandidates)); + if (!_task.getAttributes().isEmpty()) { + iStatistics[0] += _task.getAttributes().size() * aNbOfCandidates; + int aVarGain = (100 * (_task.getAttributes().size() * aNbOfCandidates - nbOfVars) / (_task.getAttributes().size() * aNbOfCandidates)); + iStatistics[1] += _task.getAttributes().size() * aNbOfCandidates - nbOfVars; + //TraceManager.addDev("Variables Gain: " + aVarGain); + } + for (int i = 0; i < _channels.size(); i++) + nbOfChannels += aStatistics[i + _task.getAttributes().size()]; + //TraceManager.addDev("Channel Checks: " + nbOfChannels + " Candidates: " + (_channels.size() * aNbOfCandidates)); + if (!_channels.isEmpty()) { + iStatistics[2] += _channels.size() * aNbOfCandidates; + int aChGain = (100 * (_channels.size() * aNbOfCandidates - nbOfChannels) / (_channels.size() * aNbOfCandidates)); + iStatistics[3] += _channels.size() * aNbOfCandidates - nbOfChannels; + //TraceManager.addDev("Channels Gain: " + aChGain); + } + for (int i = 0; i < _events.size(); i++) + nbOfEvents += aStatistics[i + _task.getAttributes().size() + _channels.size()]; + //TraceManager.addDev("Event Checks: " + nbOfEvents + " Candidates: " + (_events.size() * aNbOfCandidates)); + if (!_events.isEmpty()) { + iStatistics[4] += _events.size() * aNbOfCandidates; + int aEvtGain = (100 * (_events.size() * aNbOfCandidates - nbOfEvents) / (_events.size() * aNbOfCandidates)); + iStatistics[5] += _events.size() * aNbOfCandidates - nbOfEvents; + //TraceManager.addDev("Events Gain: " + aEvtGain); + } + //TraceManager.addDev("Request Checks: " + aStatistics[_task.getAttributeList().size() + _channels.size() + _events.size()] + " Candidates: " + aNbOfCandidates); + //if (_task.isRequested()) TraceManager.addDev("Saved Requests: " + (100* (aNbOfCandidates - aStatistics[_task.getAttributeList().size() + _channels.size() + _events.size()]) / aNbOfCandidates)); + //TraceManager.addDev("Checkpoints: " + aNbOfCheckPoints + " Candidates: " + aNbOfCandidates); + iStatistics[6] += aNbOfCandidates; + iStatistics[7] += aNbOfCandidates - aNbOfCheckPoints; + //TraceManager.addDev("Checkpoint Gain: " + (100 * (aNbOfCandidates - aNbOfCheckPoints) / aNbOfCandidates)); + } + //TraceManager.addDev("*** End of Static Analysis for task " + _task.getName()); + } + + private LiveVariableNode buildLiveAnalysisTree(TMLActivityElement iCurrElem, LiveVariableNode iReturnNode, LiveVariableNode iSuperiorNode) { + LiveVariableNode aResNode = null; + + if (iCurrElem instanceof TMLStartState) { + iCurrElem.getNextElement(0); + } + + if (iCurrElem == null || iCurrElem instanceof TMLStopState) { + return iReturnNode; + + } else if (iCurrElem instanceof TMLActionState) { + String[] aTokens = MappedSystemCTask.formatAction(((TMLActionState) iCurrElem).getAction()).split("=", 2); + if (aTokens.length < 2) aTokens = new String[2]; + aResNode = new LiveVariableNode(this, parseExprToVariableMap(aTokens[1], null), parseExprToVariableMap(aTokens[0], null), iCurrElem, iSuperiorNode, + false, aTokens[0], aTokens[1]); + + } else if (iCurrElem instanceof TMLRandom) { + TMLRandom aRndCmd = (TMLRandom) iCurrElem; + int[] aRandomVars = parseExprToVariableMap(aRndCmd.getMinValue(), null); + parseExprToVariableMap(aRndCmd.getMaxValue(), aRandomVars); + aResNode = new LiveVariableNode(this, aRandomVars, parseExprToVariableMap(aRndCmd.getVariable(), null), iCurrElem, iSuperiorNode, true); + aResNode.setVarDepSource(true); + + } else if (iCurrElem instanceof TMLDelay) { + int[] aDelayVars = parseExprToVariableMap(((TMLDelay) iCurrElem).getMinDelay(), null); + parseExprToVariableMap(((TMLDelay) iCurrElem).getMaxDelay(), aDelayVars); + aResNode = new LiveVariableNode(this, aDelayVars, new int[_bytesForVars], iCurrElem, iSuperiorNode); + + } else if (iCurrElem instanceof TMLExecI) { + aResNode = new LiveVariableNode(this, parseExprToVariableMap(((TMLExecI) iCurrElem).getAction(), null), new int[_bytesForVars], iCurrElem, iSuperiorNode); + + } else if (iCurrElem instanceof TMLExecC) { + aResNode = new LiveVariableNode(this, parseExprToVariableMap(((TMLExecC) iCurrElem).getAction(), null), new int[_bytesForVars], iCurrElem, iSuperiorNode); + + } else if (iCurrElem instanceof TMLExecIInterval) { + int[] aExecVars = parseExprToVariableMap(((TMLExecIInterval) iCurrElem).getMinDelay(), null); + parseExprToVariableMap(((TMLExecIInterval) iCurrElem).getMaxDelay(), aExecVars); + aResNode = new LiveVariableNode(this, aExecVars, new int[_bytesForVars], iCurrElem, iSuperiorNode); + + } else if (iCurrElem instanceof TMLExecCInterval) { + int[] aExecVars = parseExprToVariableMap(((TMLExecCInterval) iCurrElem).getMinDelay(), null); + parseExprToVariableMap(((TMLExecCInterval) iCurrElem).getMaxDelay(), aExecVars); + aResNode = new LiveVariableNode(this, aExecVars, new int[_bytesForVars], iCurrElem, iSuperiorNode); + + } else if (iCurrElem instanceof TMLForLoop) { + TMLForLoop aLoop = (TMLForLoop) iCurrElem; + String[] aTokens = aLoop.getInit().split("=", 2); + if (aTokens.length < 2) aTokens = new String[2]; + LiveVariableNode aInit = new LiveVariableNode(this, parseExprToVariableMap(aTokens[1], null), parseExprToVariableMap(aTokens[0], null), iCurrElem, iSuperiorNode, false, aTokens[0], aTokens[1]); + aInit.setNodeInfo("init"); + LiveVariableNode aCondition = new LiveVariableNode(this, parseExprToVariableMap(aLoop.getCondition(), null), new int[_bytesForVars], iCurrElem, iSuperiorNode); + aCondition.setNodeInfo("cond"); + aTokens = aLoop.getIncrement().split("=", 2); + if (aTokens.length < 2) aTokens = new String[2]; + LiveVariableNode anIncrement = new LiveVariableNode(this, parseExprToVariableMap(aTokens[1], null), parseExprToVariableMap(aTokens[0], null), iCurrElem, aCondition, false, aTokens[0], aTokens[1]); + anIncrement.setNodeInfo("inc"); + aInit.setSuccessor(aCondition); + anIncrement.setSuccessor(aCondition); + aCondition.setSuccessor(buildLiveAnalysisTree(iCurrElem.getNextElement(0), anIncrement, aCondition)); //in loop + aCondition.setSuccessor(buildLiveAnalysisTree(iCurrElem.getNextElement(1), iReturnNode, aCondition)); //outside loop + liveNodes.add(aInit); + liveNodes.add(aCondition); + liveNodes.add(anIncrement); + return aInit; + + } else if (iCurrElem instanceof TMLReadChannel) { + TMLReadChannel aReadCmd = (TMLReadChannel) iCurrElem; + int[] aReadVars = null; + if (aReadCmd.getChannel(0).getType() == TMLChannel.BRBW || aReadCmd.getChannel(0).getType() == TMLChannel.BRNBW) + aReadVars = parseChannelToVariableMap(aReadCmd.getChannel(0), null); + else + aReadVars = new int[_bytesForVars]; + parseExprToVariableMap(aReadCmd.getNbOfSamples(), aReadVars); + aResNode = new LiveVariableNode(this, aReadVars, new int[_bytesForVars], iCurrElem, iSuperiorNode); + + } else if (iCurrElem instanceof TMLWriteChannel) { + TMLWriteChannel aWriteCmd = (TMLWriteChannel) iCurrElem; + int[] aWriteVars = new int[_bytesForVars]; + for (int i = 0; i < aWriteCmd.getNbOfChannels(); i++) { + if (aWriteCmd.getChannel(i).getType() == TMLChannel.BRBW) + parseChannelToVariableMap(aWriteCmd.getChannel(i), aWriteVars); + } + parseExprToVariableMap(aWriteCmd.getNbOfSamples(), aWriteVars); + aResNode = new LiveVariableNode(this, aWriteVars, new int[_bytesForVars], iCurrElem, iSuperiorNode); + + } else if (iCurrElem instanceof TMLSendEvent) { + TMLSendEvent aSendCmd = (TMLSendEvent) iCurrElem; + int[] aSendVars = null; + //if (aSendCmd.getEvent().isBlocking()) + aSendVars = parseEventToVariableMap(aSendCmd.getEvent()); + //else + //aSendVars = new int[_bytesForVars]; + //parseExprToVariableMap(aSendCmd.getParam(0), aSendVars); + //parseExprToVariableMap(aSendCmd.getParam(1), aSendVars); + //parseExprToVariableMap(aSendCmd.getParam(2), aSendVars); + for (int i = 0; i < aSendCmd.getNbOfParams(); i++) { + parseExprToVariableMap(aSendCmd.getParam(i), aSendVars); + } + aResNode = new LiveVariableNode(this, aSendVars, new int[_bytesForVars], iCurrElem, iSuperiorNode); + + } else if (iCurrElem instanceof TMLSendRequest) { + TMLSendRequest aSendReqCmd = (TMLSendRequest) iCurrElem; + int[] aSendReqVars = parseExprToVariableMap(aSendReqCmd.getParam(0), null); + //parseExprToVariableMap(aSendReqCmd.getParam(1), aSendReqVars); + //parseExprToVariableMap(aSendReqCmd.getParam(2), aSendReqVars); + for (int i = 1; i < aSendReqCmd.getNbOfParams(); i++) { + parseExprToVariableMap(aSendReqCmd.getParam(i), aSendReqVars); + } + aResNode = new LiveVariableNode(this, aSendReqVars, new int[_bytesForVars], iCurrElem, iSuperiorNode); + + } else if (iCurrElem instanceof TMLWaitEvent) { + TMLWaitEvent aWaitCmd = (TMLWaitEvent) iCurrElem; + int[] aWaitVars = parseExprToVariableMap(aWaitCmd.getParam(0), null); + //parseExprToVariableMap(aWaitCmd.getParam(1), aWaitVars); + //parseExprToVariableMap(aWaitCmd.getParam(2), aWaitVars); + for (int i = 1; i < aWaitCmd.getNbOfParams(); i++) { + parseExprToVariableMap(aWaitCmd.getParam(i), aWaitVars); + } + aResNode = new LiveVariableNode(this, parseEventToVariableMap(aWaitCmd.getEvent()), aWaitVars, iCurrElem, iSuperiorNode, true); + if (aWaitCmd.getEvent().getNbOfParams() > 0) aResNode.setVarDepSource(true); + + } else if (iCurrElem instanceof TMLNotifiedEvent) { + aResNode = new LiveVariableNode(this, parseEventToVariableMap(((TMLNotifiedEvent) iCurrElem).getEvent()), parseExprToVariableMap(((TMLNotifiedEvent) iCurrElem).getVariable(), null), iCurrElem, iSuperiorNode, true); + aResNode.setVarDepSource(true); + + } else if (iCurrElem instanceof TMLSequence) { + ((TMLSequence) iCurrElem).sortNexts(); + for (int i = iCurrElem.getNbNext() - 1; i >= 0; i--) { + iReturnNode = buildLiveAnalysisTree(iCurrElem.getNextElement(i), iReturnNode, iSuperiorNode); + } + return iReturnNode; + + } else if (iCurrElem instanceof TMLChoice) { + int[] aChoiceVars = new int[_bytesForVars]; + TMLChoice aChoiceCmd = (TMLChoice) iCurrElem; + for (int i = 0; i < aChoiceCmd.getNbGuard(); i++) + if (!(aChoiceCmd.isNonDeterministicGuard(i) || aChoiceCmd.isStochasticGuard(i) || aChoiceCmd.getElseGuard() == i)) + parseExprToVariableMap(aChoiceCmd.getGuard(i), aChoiceVars); + aResNode = new LiveVariableNode(this, aChoiceVars, new int[_bytesForVars], iCurrElem, iSuperiorNode); + if (aChoiceCmd.nbOfNonDeterministicGuard() > 0 || aChoiceCmd.nbOfStochasticGuard() > 0) + aResNode.setInfected(true); + //TraceManager.addDev("checl: " + aChoiceCmd.nbOfNonDeterministicGuard() + " ** "+ aChoiceCmd.nbOfStochasticGuard()); + for (int i = 0; i < aChoiceCmd.getNbNext(); i++) + aResNode.setSuccessor(buildLiveAnalysisTree(iCurrElem.getNextElement(i), iReturnNode, aResNode)); + liveNodes.add(aResNode); + return aResNode; + + } else if (iCurrElem instanceof TMLSelectEvt) { + aResNode = new LiveVariableNode(this, new int[_bytesForVars], new int[_bytesForVars], iCurrElem, iSuperiorNode); + if (iCurrElem.getNbNext() > 1) aResNode.setInfected(true); + for (int i = 0; i < iCurrElem.getNbNext(); i++) { + aResNode.setSuccessor(buildLiveAnalysisTree(iCurrElem.getNextElement(i), iReturnNode, aResNode)); + } + liveNodes.add(aResNode); + return aResNode; + } + + LiveVariableNode aSucc = buildLiveAnalysisTree(iCurrElem.getNextElement(0), iReturnNode, iSuperiorNode); + if (aResNode == null || aResNode.isEmptyNode()) { + aResNode = aSucc; + } else { + liveNodes.add(aResNode); + aResNode.setSuccessor(aSucc); + } + return aResNode; + } + + public LiveVariableNode getLiveVarNodeByCommand(TMLActivityElement iCmd) { + for (LiveVariableNode aLiveNode : liveNodes) + if (aLiveNode.getLinkedElement() == iCmd) return aLiveNode; + return null; + } + + public void getCommandsImpactingVar(String iVarName, HashSet<Integer> oList) { + //int[] aVarMap=new int[_bytesForVars]; + int aVarSeqNo = getVarSeqNoByName(iVarName); + if (aVarSeqNo >= 0) { + //aVarMap[aVarSeqNo >>> 5] |= 1 << (aVarSeqNo & 0x1F); + int intPos = aVarSeqNo >>> 5; + int bitMask = 1 << (aVarSeqNo & 0x1F); + for (LiveVariableNode aLiveNode : liveNodes) { /*int[] aDefVars = aLiveNode.getDefVars(); boolean aMatchFound=false; for (int i=0; i<aDefVars.length && !aMatchFound;i++){ aMatchFound |= ((aDefVars[i] & aVarMap[i]) !=0); }*/ - - //if (aMatchFound && aLiveNode.getLinkedElement()!=null) oList.add(aLiveNode.getLinkedElement().getID()); - if ((aLiveNode.getInitialDefVars()[intPos] & bitMask)!=0){ - if (aLiveNode.getLinkedElement()!=null) oList.add(aLiveNode.getLinkedElement().getID()); - else TraceManager.addDev("No linked elem"); - }else - TraceManager.addDev("Bit for var not set"); - } - }else - TraceManager.addDev("Variable " + iVarName + " not found in getCommandsImpactingVar"); - } + + //if (aMatchFound && aLiveNode.getLinkedElement()!=null) oList.add(aLiveNode.getLinkedElement().getID()); + if ((aLiveNode.getInitialDefVars()[intPos] & bitMask) != 0) { + if (aLiveNode.getLinkedElement() != null) oList.add(aLiveNode.getLinkedElement().getID()); + //else TraceManager.addDev("No linked elem"); + } //else TraceManager.addDev("Bit for var not set"); + } + } //else TraceManager.addDev("Variable " + iVarName + " not found in getCommandsImpactingVar"); + } } -- GitLab