@@ -10008,6 +10008,153 @@ private static Type getElementType(BinaryExpression be)
1000810008 }
1000910009
1001010010 // Check completeness: if post updates v but not w when w data depends on v
10011+
10012+ public Statement selfCalls2Loops (Statement act )
10013+ { // if there are simple calls to itself, replace by
10014+ // continue/break in a loop.
10015+
10016+ // ... ; self.nme() ; ***
10017+ // is while true do (...)
10018+
10019+ // ... ; if E then self.nme() else skip ; ***
10020+ // is while true do (... ; if E then continue else
10021+ // skip) ; ***
10022+
10023+ Statement oldact = act ;
10024+ if (oldact == null )
10025+ { oldact = activity ; }
10026+ if (oldact == null )
10027+ { return act ; }
10028+
10029+ String nme = getName ();
10030+
10031+ Vector contexts = new Vector ();
10032+ Vector remainders = new Vector ();
10033+
10034+ Vector opcalls = Statement .getOperationCallsContexts (
10035+ nme ,oldact ,contexts ,remainders );
10036+
10037+ System .out .println (opcalls );
10038+ System .out .println (contexts );
10039+ System .out .println (remainders );
10040+
10041+ int selfcalls = 0 ;
10042+ Vector branches = new Vector ();
10043+ Vector rems = new Vector ();
10044+
10045+ for (int i = 0 ; i < opcalls .size (); i ++)
10046+ { InvocationStatement opcall = (InvocationStatement ) opcalls .get (i );
10047+ Vector cntx = (Vector ) contexts .get (i );
10048+ Vector rem = (Vector ) remainders .get (i );
10049+ Expression expr = opcall .getCallExp ();
10050+
10051+ if (("self." + nme + "()" ).equals (expr + "" ))
10052+ { selfcalls ++;
10053+ branches .add (cntx );
10054+ rems .add (rem );
10055+ // System.out.println(cntx);
10056+ }
10057+ }
10058+
10059+ System .out .println (">>> There are " + selfcalls + " calls of self." + nme + "() in " + oldact );
10060+ System .out .println (">>> Branches to calls: " + branches );
10061+ System .out .println (">>> Remainders: " + rems );
10062+
10063+ if (selfcalls <= 0 )
10064+ { return act ; }
10065+
10066+ if (selfcalls == 1 )
10067+ { // simple case. The call is the last item in
10068+ // branches[0]. Either a direct call or conditional.
10069+
10070+ SequenceStatement loopBody = new SequenceStatement ();
10071+
10072+ Vector branch = (Vector ) branches .get (0 );
10073+ int blen = branch .size ();
10074+ for (int i = 0 ; i < blen - 1 ; i ++)
10075+ { Statement sx = (Statement ) branch .get (i );
10076+ loopBody .addStatement (sx );
10077+ }
10078+
10079+ Vector remainder = (Vector ) rems .get (0 );
10080+ Object selfcall = branch .get (blen -1 );
10081+
10082+ if (selfcall instanceof Statement )
10083+ {
10084+ WhileStatement ws = new WhileStatement (
10085+ new BasicExpression (true ),
10086+ loopBody );
10087+
10088+ System .out .println (">>> Restructured code: " + ws );
10089+ System .out .println ();
10090+
10091+ return ws ;
10092+ }
10093+ else if (selfcall instanceof Vector )
10094+ { // conditional cases
10095+ Vector selfcallv = (Vector ) selfcall ;
10096+ if (selfcallv .size () == 4 &&
10097+ "if" .equals (selfcallv .get (0 ) + "" ))
10098+ { Expression tst = (Expression ) selfcallv .get (1 );
10099+ Vector sts = (Vector ) selfcallv .get (2 );
10100+ Statement cde =
10101+ Statement .replaceSelfCallByContinue (nme ,sts );
10102+ Statement elsePart = (Statement ) selfcallv .get (3 );
10103+
10104+ Statement newelse =
10105+ SequenceStatement .combineSequenceStatements (
10106+ elsePart ,new BreakStatement ());
10107+ ConditionalStatement cs =
10108+ new ConditionalStatement (tst ,
10109+ cde ,
10110+ newelse );
10111+ loopBody .addStatement (cs );
10112+
10113+ WhileStatement ws = new WhileStatement (
10114+ new BasicExpression (true ),
10115+ loopBody );
10116+ SequenceStatement res = new SequenceStatement ();
10117+ res .addStatement (ws );
10118+ res .addStatements (remainder );
10119+
10120+ System .out .println (">>> Restructured code: " + res );
10121+ System .out .println ();
10122+
10123+ return res ;
10124+ }
10125+ else if (selfcallv .size () == 4 &&
10126+ "else" .equals (selfcallv .get (0 ) + "" ))
10127+ { Expression tst = (Expression ) selfcallv .get (1 );
10128+ Statement ifpart = (Statement ) selfcallv .get (2 );
10129+
10130+ Vector sts = (Vector ) selfcallv .get (3 );
10131+ Statement cde =
10132+ Statement .replaceSelfCallByContinue (nme ,sts );
10133+ Statement newif =
10134+ SequenceStatement .combineSequenceStatements (
10135+ ifpart ,new BreakStatement ());
10136+ ConditionalStatement cs =
10137+ new ConditionalStatement (tst ,
10138+ newif , cde );
10139+ loopBody .addStatement (cs );
10140+
10141+ WhileStatement ws = new WhileStatement (
10142+ new BasicExpression (true ),
10143+ loopBody );
10144+ SequenceStatement res = new SequenceStatement ();
10145+ res .addStatement (ws );
10146+ res .addStatements (remainder );
10147+
10148+ System .out .println (">>> Restructured code: " + res );
10149+ System .out .println ();
10150+
10151+ return res ;
10152+ }
10153+ }
10154+ }
10155+
10156+ return act ;
10157+ }
1001110158}
1001210159
1001310160
0 commit comments