Menu

Code review please - top urgent

Anobium
2021-10-07
2021-10-08
  • Anobium

    Anobium - 2021-10-07

    I need a code review. I have changed a fundamental part of the compiler to resolve an issue.

    This has take two long days to resolve. :-(

    If I get this incorrect the compiler with fall over with bad asm.

    If you can do code review - ping me.

    Evan


    The issue reported: The ASM code generated was different when a comment is the last line of code before an END SUB, and the code is the same when comments are removed from the program.

    Code Example 1

    #chip 16f88, 4
    #option explicit
    
    Sub _FN2
    End Sub
    
    Sub _FN1
      _FN2
    
    End Sub
    
    _FN1
    

    Code Example 2

    #chip 16f88, 4
    #option explicit
    
    Sub _FN2
    End Sub
    
    Sub _FN1
       _FN2
    '~
    End Sub
    
    _FN1
    

    The only difference is the comment at the end of _FN1 before the End Sub.

    Code example 1 generated a call _fn1 & return and Code example 1 generated a goto _fn1 with no return (which is the intent).


    With assmebly.bi line 235

    Was

    'Check for subs that have a call as the last instruction
      'Turn call into a goto
      'If call is last instruction and always runs, no need for a return at the end either
      'Get last line of sub
      CompSub->HasFinalGoto = 0
      CompSub->FinalGotoDest = ""
      CurrLine = CompSub->CodeStart
      Do While CurrLine->Next <> 0
        CurrLine = CurrLine->Next
      Loop
      LastLine = CurrLine
      'Get line before last
      CheckLine = LastLine->Prev
      If CheckLine <> 0 Then
        Do While (Left(CheckLine->Value, 1) = ";" Or Left(CheckLine->Value, 8) = "PRESERVE") AND CheckLine->Prev <> 0
          CheckLine = CheckLine->Prev
        Loop
      End If
    

    now

     'Check for subs that have a call as the last instruction
      'Turn call into a goto
      'If call is last instruction and always runs, no need for a return at the end either
      'Get last line of sub
      CompSub->HasFinalGoto = 0
      CompSub->FinalGotoDest = ""
      'Restart the code list
      CurrLine = CompSub->CodeStart
      'Do while has data in code list
      Do While CurrLine->Next <> 0
        'While has code in code list, set to next code line
        CurrLine = CurrLine->Next
      Loop
    
      'Handle last line... it could be a comment therefore PRESERVEd
      'Do while the current code line is PRESERVEd
      Do while Left(CurrLine->Value, 8) = "PRESERVE"
       'if PREVious code is NOT a PRESERVEd line then get the PREVious line as the current line
        If Left(CurrLine->Prev->Value, 8) <> "PRESERVE" then
          CurrLine = CurrLine->Prev
        End if
      Loop
    
      LastLine = CurrLine
      'Get line before last
      CheckLine = LastLine->Prev
      If CheckLine <> 0 Then
        Do While (Left(CheckLine->Value, 1) = ";" Or Left(CheckLine->Value, 8) = "PRESERVE") AND CheckLine->Prev <> 0
          CheckLine = CheckLine->Prev
        Loop
      End If
    
     
  • Anobium

    Anobium - 2021-10-08

    My initial attempt did break the compiler. It caused an indefinite loop, so, the compiler never completed.

    New attempt with update code. With an additional test to check that the list is not pass the top extent of list.

      'Handle last line... it could be a comment therefore PRESERVEd
      'Do while the current code line is PRESERVEd
      Do while Left(CurrLine->Value, 8) = "PRESERVE"
       'if PREVious code is NOT a PRESERVEd line then get the PREVious line as the current line, and, check that we are not at the top of the list
        If Left(CurrLine->Prev->Value, 8) <> "PRESERVE" and CurrLine->Prev <> 0 then
          CurrLine = CurrLine->Prev
        Else
          Exit Do
        End if
        print CurrLine->Value
      Loop
    
     

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.