Page tree
Skip to end of metadata
Go to start of metadata

The following gmSL routine moves a declaration to the top of a function.  This may be required when a function making aggressive use of GO TOs with error handling is migrated to try-catch leading to code that uses a variable before it is initialized. (See C# Compiler Error CS0165)

<gmBasic>
   <Storage Action="Create" Identifier="App" />

... options etc. ...

   <gmSL class="Refactoring">
   void MoveDeclToTop(string identifier)
   {
/// moves the declaration of the specified identifier to the top of the declaring subprogram 

      int        iRoot;   // address of identifier to move
      int        subRoot; // subroutine containing the identifier to move
      tVbSub     subInfo; // information vector for subroutine
      tCodeBlock codPtr;  // code stream
      int        nCode;   // length of code stream
      int        lastNew; // last Statement start
      int        iCode;   // code pointer for code scan
      int        opcd;    // opcode for code scan 
      int        subc;    // subcode for code scan
      int        subLen;  // length of subroutine 
      int        topAddr;

      iRoot = Symbol.FindIdentifier(identifier,0);
      if(iRoot == 0)
      {
         System.LogMessage("MoveDeclToTop failed: could not locate " + identifier);
         return;
      }
      subRoot = Store.GetParent(iRoot);
      nCode = Store.GetLength(subRoot);
      subInfo = Store.DeltaVector(subRoot);
      codPtr = Opcode.GetCode();
      nCode = Store.ReadInfo(codPtr,subInfo.anaCodeStart);
      Opcode.SetLength(nCode);
      subLen = nCode;  
      lastNew = 0;
      topAddr = 0;
      for(iCode = 0; iCode >= 0; iCode = Opcode.GetNext(codPtr,iCode,nCode))
      {
         opcd = Opcode.GetOperation(codPtr,iCode,subc);
         if(opcd == OPC.NEW)
         {
            lastNew = iCode;
            continue;
         }
         if(opcd == OPC.DCL)
         {
            if(subc == iRoot) 
            {
               if((icode - lastNew) != 3)
               {
                  System.LogMessage("MoveDeclToTop failed:" + identifier + " Declaration at " + icode + " Not immediately preceded by a NEW" );
                  Opcode.DumpCode(lastNew,lastNew+20);
                  break;
               }
               topAddr = 0;
               Opcode.MoveCode(topAddr,lastNew,nCode,8);
               subInfo.anaCodeStart = Store.WriteInfo(codPtr,subLen);
               System.LogMessage("Reauthored: " + identifier + " by moving declaration to " + topAddr);         
               break;
            }
            else
            {
               topAddr = iCode;
            }
         }
      }
   }
   </gmSL>
   <Compile Project="\fkgtest\Aptora\src\TOM-Trunk.vbp" />
   <Analyse />

<!-- this RunCommand element invokes the gmSL method above and moves the specified local variable declaration -->
   <RunCommand id="Refactoring.MoveDeclToTop" Prams="App.frmDDProcessingList.delete.xTemp"/>

  • No labels