Error on "Jump Around" Statements

Questions about the BASICtools and MakeItC
Post Reply
joshklod
Posts: 6
Joined: Tue Feb 27, 2018 7:52 pm

Error on "Jump Around" Statements

Post by joshklod » Fri Mar 13, 2020 8:16 pm

As of ARMbasic v9.47, the compiler sometimes throws errors on included library files.
Specifically, whenever a `goto` is encountered after a subroutine, but before any labels, an "unreachable statement" error is thrown.
Capture.PNG
Capture.PNG (18.51 KiB) Viewed 1193 times
The following minimal example demonstrates this behavior:

Code: Select all

sub a
endsub

goto jumpAround
jumpAround:

MAIN:
And this hypothetical program demonstrates why one might want to do this:

Code: Select all

// main.bas
#include "a.bas"
#include "b.bas"
MAIN:
a
b

Code: Select all

// a.bas
sub a
    print "sub a"
endsub

Code: Select all

// b.bas
#include <c.bas>
sub b
    print "sub b"
    c
endsub

Code: Select all

// c.bas
goto jumpAroundC
sub c
    print "sub c"
endsub
jumpAroundC:
This issue did not occur in ARMbasic v9.37.



basicchip
Posts: 996
Joined: Fri Oct 19, 2012 2:39 am
Location: Lake Tahoe, CA
Contact:

Re: Error on "Jump Around" Statements

Post by basicchip » Sat Mar 14, 2020 6:41 pm

Thank you for isolating and explaining the issue.

The compiler now flags as an error, code that would not be executed, which in this case is the GOTO Jumparound in the libraries.

A better fix is to remove those GOTO's, which I will do in the 3 libraries I see them in.

The compiler also now flags a program with SUB / FUNCTION without a MAIN, which is the reason for the original GOTO's

joshklod
Posts: 6
Joined: Tue Feb 27, 2018 7:52 pm

Re: Error on "Jump Around" Statements

Post by joshklod » Mon Mar 16, 2020 2:59 pm

Thanks.
The compiler now flags as an error, code that would not be executed
Interestingly, the error is not thrown unless a sub/function is encountered before the statement, even when `MAIN` is used. Shouldn't it always be flagged as unreachable as long as there is a `MAIN`?
A better fix is to remove those GOTO's, which I will do in the 3 libraries I see them in.
I ran a quick search and found jumparounds in the following library files:
AB_Math.bas DATA.bas FREQOUT.bas HWPWM.bas HWPWM11.bas HWPWM17.bas HWPWM8.bas I2C.bas ONEWIRE.bas PULSE.bas RTC.bas SERIAL.bas SHIFT.bas SPI.bas STRING.bas VBSTRING.bas

basicchip
Posts: 996
Joined: Fri Oct 19, 2012 2:39 am
Location: Lake Tahoe, CA
Contact:

Re: Error on "Jump Around" Statements

Post by basicchip » Mon Mar 16, 2020 4:33 pm

OK, I did not look hard enough, and will remove the other jumpAround's

The compiler is a single pass compiler so it does not know whether there is a MAIN or not. It does allow a GOTO as the first statement in a program, but if there is a SUB /FUNCTION before that, that GOTO would never be executed so it is flagged as an error.

A MAIN is effectively a GOTO MAIN as the first statement of the program, though not literally done that way in the BASIC source. But if you look at the emitted code there is a BL at the beginning of the code, which either jumps to the first code or the MAIN depending on whether there is a MAIN or not.

Also when you type in a program line by line a User__Typing: label is inserted before your code, which also keeps the compiler happy, as it assumes that label may be called or goto'd somewhere else in the code if there is a MAIN.

basicchip
Posts: 996
Joined: Fri Oct 19, 2012 2:39 am
Location: Lake Tahoe, CA
Contact:

Re: Error on "Jump Around" Statements

Post by basicchip » Mon Mar 16, 2020 7:14 pm

Those jumpAround's have now been removed, and the setupBASIC.exe has been updated at the web

Post Reply