Page 1 of 2

Pas6502 - Pascal dialect compiler -> 6502 assembly code

Posted: Sat Nov 17, 2018 12:39 am
by paul_nicholls
Hi all,
I'd thought I'd mention that I'm working on a project (part fun/learning/useful) called Pas6502 which will compile a dialect of Pascal into 6502 assembly code (using Kick Assembler macros and asm code). I will be starting with the C64.

I know there have been others, but I wanted to do this myself too :)

I have made a start where I can parse expressions using standard Pascal syntax, or expressions including mixed maths & boolean logic for tricky equations like the Dirac delta function (one line):

Code: Select all

a := -(x=0)
or the absolute value function (definitely tricky code!):

Code: Select all

x := x*(1+2*(x<0))
Which currently produces this code:

Code: Select all

main:
  :loadIntRegMem(x,0)
  :loadIntRegIm(1,1)
  :loadIntRegIm(2,2)
  :loadIntRegMem(x,3)
  :loadIntRegIm(0,4)
  :cmpIntRegLss(3,4,3)
  :mulIntReg(2,3,2)
  :addIntReg(1,2,1)
  :mulIntReg(0,1,0)
  :storeIntRegMem(0,x)
  rts
I will be adding in expression simplification too which will include power of 2 div/mull -> shifts/shifts+adds.

Internally, I will be representing False = 0 and True = -1, but any value <> 0 will also equate to True so I will be able to things like;

Code: Select all

if SomeNumericVar then
// do something if SomeNumericVar <> 0
...
I am also posting on twitter:
https://twitter.com/SyntaxErrorSoft/sta ... 5586321409

Image
cheers,
Paul

Re: Pas6502 - Pascal dialect compiler -> 6502 assembly code

Posted: Sat Nov 17, 2018 4:00 am
by barrym95838
What does

Code: Select all

x := (x<0)*x*2+x
compile to?

Re: Pas6502 - Pascal dialect compiler -> 6502 assembly code

Posted: Sun Nov 18, 2018 5:53 am
by paul_nicholls
barrym95838 wrote:
What does

Code: Select all

x := (x<0)*x*2+x
compile to?
That currently compiles to:

Code: Select all

main:
  :loadIntRegMem(x,0)
  :loadIntRegIm(0,1)
  :cmpIntRegLss(0,1,0)
  :loadIntRegMem(x,1)
  :mulIntReg(0,1,0)
  :loadIntRegIm(2,1)
  :mulIntReg(0,1,0)
  :loadIntRegMem(x,1)
  :addIntReg(0,1,0)
  :storeIntRegMem(0,x)
  rts
:)

Re: Pas6502 - Pascal dialect compiler -> 6502 assembly code

Posted: Sun Nov 18, 2018 9:24 am
by paul_nicholls
I've now added some optimizations like simplifying to a numerical value where possible, division with a power of 2 to a shift, and multiplication to shifts & additions. Will do * 0, * 1, + 0 optimizations too amongst others.

Re: Pas6502 - Pascal dialect compiler -> 6502 assembly code

Posted: Sun Nov 18, 2018 4:52 pm
by barrym95838
paul_nicholls wrote:
... and multiplication to shifts & additions ...
How were you doing it before your optimization?

Re: Pas6502 - Pascal dialect compiler -> 6502 assembly code

Posted: Mon Nov 19, 2018 2:28 am
by paul_nicholls
I was just parsing the expression into postfix format using an expression tree and not optimising it before...

Is that what you meant?
cheers,
Paul

Re: Pas6502 - Pascal dialect compiler -> 6502 assembly code

Posted: Mon Nov 19, 2018 11:27 pm
by whartung
paul_nicholls wrote:
I have made a start where I can parse expressions using standard Pascal syntax, or expressions including mixed maths & boolean logic for tricky equations like the Dirac delta function (one line):

Code: Select all

a := -(x=0)
or the absolute value function (definitely tricky code!):

Code: Select all

x := x*(1+2*(x<0))
Just so ya know, in Pascal, those aren't legal expressions.

Pascal is not C where boolean expressions are actually just integer expression checking for zero, boolean is a "real" type in Pascal, and there's no language construct that casts booleans to integers. You could write one of course, but it's not in the stock language.

Re: Pas6502 - Pascal dialect compiler -> 6502 assembly code

Posted: Tue Nov 20, 2018 8:19 am
by paul_nicholls
whartung wrote:
paul_nicholls wrote:
I have made a start where I can parse expressions using standard Pascal syntax, or expressions including mixed maths & boolean logic for tricky equations like the Dirac delta function (one line):

Code: Select all

a := -(x=0)
or the absolute value function (definitely tricky code!):

Code: Select all

x := x*(1+2*(x<0))
Just so ya know, in Pascal, those aren't legal expressions.

Pascal is not C where boolean expressions are actually just integer expression checking for zero, boolean is a "real" type in Pascal, and there's no language construct that casts booleans to integers. You could write one of course, but it's not in the stock language.
I know, that's why I wrote this:
paul_nicholls wrote:
I can parse expressions using standard Pascal syntax, or expressions including mixed maths & boolean logic
I wanted to give the programmer the freedom of using both Standard Pascal and the mixed version :)
:D
cheers,
Paul

Re: Pas6502 - Pascal dialect compiler -> 6502 assembly code

Posted: Wed Nov 28, 2018 7:20 am
by paul_nicholls
More #Pas6502 compiler progress...can now do basic math, store values into variables and absolute addresses (1 or 2 byte) [including arrays], and Poke() command added. More optimisation needed, but am very happy :) <committing to version control now lol>

Input example:
Image

Output example (copied from the ASM window):
Image
cheers,
Paul

Re: Pas6502 - Pascal dialect compiler -> 6502 assembly code

Posted: Fri Nov 30, 2018 8:11 am
by paul_nicholls
I've now added if and while statements to #Pas6502, and here is the obligatory infinite loop incrementing the border color lol

If anyone is wondering, I've been basing my code on the classic "Let's build a compiler" tutorial series by Jack W. Crenshaw here:
http://www.penguin.cz/~radek/book/lets_ ... mpiler.pdf

Image

Image

cheers,
Paul

Re: Pas6502 - Pascal dialect compiler -> 6502 assembly code

Posted: Fri Nov 30, 2018 9:14 am
by drogon
What are you writing in in and in what environment? e.g. would I be able to run it under Linux using vim to edit files? Or do you think it would ever work on a 6502 hosted system?

Cheers,

-Gordon

Re: Pas6502 - Pascal dialect compiler -> 6502 assembly code

Posted: Sat Dec 01, 2018 3:53 am
by paul_nicholls
I'm writing it using Embarcadero Delphi.

I guess it would run under Wine in Linux, and you could use any text editor to create the source code files, but currently, you would have to load it into the compiler window (or copy/paste) prior to compiling it to a .prg file.

It uses kick assembler (runs using Java) to assemble the generated assembly source code to a C64 .prg file.

cheers,
Paul

Re: Pas6502 - Pascal dialect compiler -> 6502 assembly code

Posted: Sat Mar 30, 2024 8:47 am
by BigEd
Just to note, it looks like there's an updated version:
Quote:
I've uploaded a new version of Pas6502, my Pascal to 6502 compiler for the C64/C128/BBC Micro. This includes fixes, new C128 support.

Re: Pas6502 - Pascal dialect compiler -> 6502 assembly code

Posted: Sat Mar 30, 2024 9:16 am
by Proxy
damn if this worked for the 65816 then it could maybe be possible to port ORCA-C to a more generic system instead of being Apple IIgs specific

Re: Pas6502 - Pascal dialect compiler -> 6502 assembly code

Posted: Mon Apr 01, 2024 2:33 am
by paul_nicholls
Proxy wrote:
damn if this worked for the 65816 then it could maybe be possible to port ORCA-C to a more generic system instead of being Apple IIgs specific
Interesting, I'll have a look at the 65816 CPU to see what that is like for possibly adding to Pas6502 :)

cheers,
Paul