Compiler writing

Programming the 6502 microprocessor and its relatives in assembly and other languages.
BillG
Posts: 710
Joined: 12 Mar 2020
Location: North Tejas

Re: Compiler writing

Post by BillG »

OK, the integration is done and the compiler can handle FOR loops now, even the monstrosity in the following program:

Code: Select all

100 n = 1
110 m = 3
120 for i = n+1 to m+1 step m-2
130 print i
140 next i
The FOR statement compiles into:

Code: Select all

                          00088 * 120 for i = n+1 to m+1 step m-2
 0125                     00089 L00120
                          00090          ifdef  __TRACE
                          00091          ldx    #120
                          00092          jsr    Trace
                          00093          endif
                          00094          ifdef  __ATLIN
 0125 CE 0125         [3] 00095          ldx    #L00120
 0128 FF 0663         [6] 00096          stx    ResLn_
                          00097          endif
                          00098
 012B BD 0296         [9] 00099          jsr    ForEnter
                          00100
 012E 86 01           [2] 00101          ldaa   #T00000>>8
 0130 A7 04           [6] 00102          staa   4,X
 0132 86 49           [2] 00103          ldaa   #T00000&$FF
 0134 A7 05           [6] 00104          staa   5,X
                          00105
 0136 86 06           [2] 00106          ldaa   #I_>>8
 0138 A7 06           [6] 00107          staa   6,X
 013A 86 75           [2] 00108          ldaa   #I_&$FF
 013C A7 07           [6] 00109          staa   7,X
                          00110
 013E C6 01           [2] 00111          ldab   #1
 0140 4F              [2] 00112          clra
 0141 FB 0672         [4] 00113          addb   N_+1
 0144 B9 0671         [4] 00114          adca   N_
                          00115
 0147 20 46 (018F)    [4] 00116          bra    T00003
                          00117
 0149                     00118 T00000
 0149 C6 01           [2] 00119          ldab   #1
 014B 4F              [2] 00120          clra
 014C FB 0674         [4] 00121          addb   M_+1
 014F B9 0673         [4] 00122          adca   M_
 0152 F7 066E         [5] 00123          stab   ITp00_+1
 0155 B7 066D         [5] 00124          staa   ITp00_
                          00125
 0158 F6 0674         [4] 00126          ldab   M_+1
 015B B6 0673         [4] 00127          ldaa   M_
 015E C0 02           [2] 00128          subb   #2
 0160 82 00           [2] 00129          sbca   #0
 0162 B7 066F         [5] 00130          staa   ITp01_
                          00131
 0165 FB 0676         [4] 00132          addb   I_+1
 0168 B9 0675         [4] 00133          adca   I_
                          00134
 016B 7D 066F         [6] 00135          tst    ITp01_
 016E 2B 0E (017E)    [4] 00136          bmi    T00004
                          00137
 0170 B1 066D         [4] 00138          cmpa   ITp00_
 0173 22 15 (018A)    [4] 00139          bhi    T00001
 0175 25 16 (018D)    [4] 00140          blo    T00002
 0177 F1 066E         [4] 00141          cmpb   ITp00_+1
 017A 23 11 (018D)    [4] 00142          bls    T00002
 017C 20 0C (018A)    [4] 00143          bra    T00001
                          00144
 017E                     00145 T00004
 017E B1 066D         [4] 00146          cmpa   ITp00_
 0181 25 07 (018A)    [4] 00147          blo    T00001
 0183 22 08 (018D)    [4] 00148          bhi    T00002
 0185 F1 066E         [4] 00149          cmpb   ITp00_+1
 0188 24 03 (018D)    [4] 00150          bhs    T00002
                          00151
 018A                     00152 T00001
 018A 7E 02D3         [3] 00153          jmp    ForExit
                          00154
 018D                     00155 T00002
 018D 31              [4] 00156          ins
 018E 31              [4] 00157          ins
                          00158
 018F                     00159 T00003
 018F B7 0675         [5] 00160          staa   I_
 0192 F7 0676         [5] 00161          stab   I_+1
User avatar
BigEd
Posts: 11464
Joined: 11 Dec 2008
Location: England
Contact:

Re: Compiler writing

Post by BigEd »

(Thanks for the VBCC link! Added to the Compilers and Languages page.)
User avatar
BigEd
Posts: 11464
Joined: 11 Dec 2008
Location: England
Contact:

Re: Compiler writing

Post by BigEd »

Another small and carefully constructed c compiler is discussed and linked here
https://news.ycombinator.com/item?id=33581704
https://github.com/rui314/chibicc
petewilson
Posts: 1
Joined: 21 Nov 2022

Re: Compiler writing

Post by petewilson »

One path that can be explored is to own a BBC microcomputer.
You don't need to own a real one - a simulation should do. Google for 'BBC micro emulator horizon', as one example.

The BASIC interpreter was pretty powerful, and included a 6502 assembler. The language was sort of 'bcpl with BASIC syntax' - so named procedures with arguments existed.

One of the projects outlined in books published for the machine was a compiler, written in BBC Basic. It leveraged the built-in assembler.

Don't have my hands on the relevant book right now, alas. If there's interest, I'll keep an eye open for it. It should be somewhere around the house...

The resulting compiler was NOT the world's finest example, either in capability (from memory, integers could be any size you wanted as long as they ere 8 bits, for example); and the compiler itself was a straight recursive-descent simple thing that generated code on the fly. But it got me interested.
User avatar
BigEd
Posts: 11464
Joined: 11 Dec 2008
Location: England
Contact:

Re: Compiler writing

Post by BigEd »

Welcome! Might the book be Mastering Interpreters And Compilers by Bruce Smith?
User avatar
drogon
Posts: 1671
Joined: 14 Feb 2018
Location: Scotland
Contact:

Re: Compiler writing

Post by drogon »

petewilson wrote:
One of the projects outlined in books published for the machine was a compiler, written in BBC Basic. It leveraged the built-in assembler.
I'm suspecting the banned book "The BBC Micro Compendium"

It was banned as it had disassembled listings of the BASIC ROM - technically it was withdrawn the day before shops were supposed to sell it, but somehow I managed to get a copy... In any case it's now online... e.g. https://archive.org/details/BBCMicroCompendium it has listings for 2 languages written in bbc basic and asm - Froth - a forth-like language and the "SLUG" language - a sort of high level structured Pascal-like thing (from memory)

It was fascinating reading back at the time!

-Gordon
--
Gordon Henderson.
See my Ruby 6502 and 65816 SBC projects here: https://projects.drogon.net/ruby/
User avatar
barrym95838
Posts: 2056
Joined: 30 Jun 2013
Location: Sacramento, CA, USA

Re: Compiler writing

Post by barrym95838 »

drogon wrote:
It was fascinating reading back at the time!
Wow! I only have time for a brief skim before work, but it looks fascinating to me right now. That Jeremy Ruston character was a rock star! Bookmarked ...
Got a kilobyte lying fallow in your 65xx's memory map? Sprinkle some VTL02C on it and see how it grows on you!

Mike B. (about me) (learning how to github)
Post Reply