[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Basic compiler



In article <irudnYTuKL81IqPYnZ2dnUVZ_oednZ2d@comcast.com>,
Michael J. Mahon <mjmahon@aol.com> wrote:

>> I have a basic compiler for the TRS-80 that does a strange hybrid thing; 
>> ACCEL2. I bought it back in the old days. It will put assembly into 
>> comments within the basic program and there is a small runtime library 
>> too. It will compile only what is advantageous. The output is smaller 
>> and a lot faster.
> 
> An interesting approach...so it must "patch" the BASIC program so that
> it gets control to the machine code?

Microsoft's MBASIC (runs under CP/M) does that too: integers which are
line number targets (such as in GOTO, GOSUB, etc) are initially stored
as the line number itself, as a 16-bit binary integer.  The first time
the GOTO or GOSUB is executed, the interpreter searches for that line
number as expected.  But when the line number is found, something
interesting happens: the token for GOTO is changed into another token
which also means GOTO, and the line number is replaced by the offset
for the start of the line in the tokenized Basic program.  If the
Basic program is saved in tokenized rather than ASCII format, this
information is preserved so that the interpreter doesn't have to look
for the line at all the next time that program is loaded and run, the
GOTO/GOSUB statement already knows where it is!

But what happens then in an MBASIC program which have "compiled" its
GOTO/GOSUB statements if you modify the program, by adding, removing
or modifying some line?  That can potentially invalidate any offset to
the target line after a GOTO/GOSUB.  In that case the interpreter,
before modifying the program, scans all through it and replaces all
GOTO/GOSUBS's having machine addresses with GOTO/GOSUB's with
corresponding line numbers instead.

This applies to any statements containing target line numbers: not
just GOT and GOSUB but also ON N GOTO <list of line numbers>, ON ERROR
GOTO <line number> (error handling), RESUME <line number> (determines
where an error handler should resume execution after handling the
error) and RESTORE <line number> (determines where the next READ
statement should start reading its DATA lines).

In Applesoft, it is recommended that frequently called subroutines
should be put near the beginning of the program, since then the
GOSUB statement will find the target line faster.  In MBASIC, this
doesn't matter, since the GOSUB will have to scan for the target
line just once - after that it "remembers" where the target line is.

-- 
----------------------------------------------------------------
Paul Schlyter,  Grev Turegatan 40,  SE-114 38 Stockholm,  SWEDEN
e-mail:  pausch at stockholm dot bostream dot se
WWW:     http://stjarnhimlen.se/