6502.org Forum  Projects  Code  Documents  Tools  Forum
It is currently Fri Nov 22, 2024 9:44 pm

All times are UTC




Post new topic Reply to topic  [ 18 posts ]  Go to page 1, 2  Next
Author Message
PostPosted: Sat Nov 17, 2018 12:39 am 
Offline

Joined: Wed Jan 19, 2011 10:20 pm
Posts: 42
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:
a := -(x=0)


or the absolute value function (definitely tricky code!):

Code:
x := x*(1+2*(x<0))


Which currently produces this code:
Code:
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:
if SomeNumericVar then
// do something if SomeNumericVar <> 0
...


I am also posting on twitter:
https://twitter.com/SyntaxErrorSoft/status/1063554525586321409

Image
cheers,
Paul

_________________
"The plastic veneer of civilization is easily melted in the heat of the moment" - Paul Nicholls


Top
 Profile  
Reply with quote  
PostPosted: Sat Nov 17, 2018 4:00 am 
Offline
User avatar

Joined: Sun Jun 30, 2013 10:26 pm
Posts: 1949
Location: Sacramento, CA, USA
What does
Code:
x := (x<0)*x*2+x
compile to?

_________________
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)


Top
 Profile  
Reply with quote  
PostPosted: Sun Nov 18, 2018 5:53 am 
Offline

Joined: Wed Jan 19, 2011 10:20 pm
Posts: 42
barrym95838 wrote:
What does
Code:
x := (x<0)*x*2+x
compile to?


That currently compiles to:
Code:
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


:)

_________________
"The plastic veneer of civilization is easily melted in the heat of the moment" - Paul Nicholls


Top
 Profile  
Reply with quote  
PostPosted: Sun Nov 18, 2018 9:24 am 
Offline

Joined: Wed Jan 19, 2011 10:20 pm
Posts: 42
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.

_________________
"The plastic veneer of civilization is easily melted in the heat of the moment" - Paul Nicholls


Top
 Profile  
Reply with quote  
PostPosted: Sun Nov 18, 2018 4:52 pm 
Offline
User avatar

Joined: Sun Jun 30, 2013 10:26 pm
Posts: 1949
Location: Sacramento, CA, USA
paul_nicholls wrote:
... and multiplication to shifts & additions ...

How were you doing it before your optimization?

_________________
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)


Top
 Profile  
Reply with quote  
PostPosted: Mon Nov 19, 2018 2:28 am 
Offline

Joined: Wed Jan 19, 2011 10:20 pm
Posts: 42
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

_________________
"The plastic veneer of civilization is easily melted in the heat of the moment" - Paul Nicholls


Top
 Profile  
Reply with quote  
PostPosted: Mon Nov 19, 2018 11:27 pm 
Offline

Joined: Sat Dec 13, 2003 3:37 pm
Posts: 1004
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:
a := -(x=0)


or the absolute value function (definitely tricky code!):

Code:
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.


Top
 Profile  
Reply with quote  
PostPosted: Tue Nov 20, 2018 8:19 am 
Offline

Joined: Wed Jan 19, 2011 10:20 pm
Posts: 42
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:
a := -(x=0)


or the absolute value function (definitely tricky code!):

Code:
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

_________________
"The plastic veneer of civilization is easily melted in the heat of the moment" - Paul Nicholls


Top
 Profile  
Reply with quote  
PostPosted: Wed Nov 28, 2018 7:20 am 
Offline

Joined: Wed Jan 19, 2011 10:20 pm
Posts: 42
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

_________________
"The plastic veneer of civilization is easily melted in the heat of the moment" - Paul Nicholls


Top
 Profile  
Reply with quote  
PostPosted: Fri Nov 30, 2018 8:11 am 
Offline

Joined: Wed Jan 19, 2011 10:20 pm
Posts: 42
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_build_a_compiler.pdf

Image

Image

cheers,
Paul

_________________
"The plastic veneer of civilization is easily melted in the heat of the moment" - Paul Nicholls


Top
 Profile  
Reply with quote  
PostPosted: Fri Nov 30, 2018 9:14 am 
Offline
User avatar

Joined: Wed Feb 14, 2018 2:33 pm
Posts: 1488
Location: Scotland
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

_________________
--
Gordon Henderson.
See my Ruby 6502 and 65816 SBC projects here: https://projects.drogon.net/ruby/


Top
 Profile  
Reply with quote  
PostPosted: Sat Dec 01, 2018 3:53 am 
Offline

Joined: Wed Jan 19, 2011 10:20 pm
Posts: 42
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

_________________
"The plastic veneer of civilization is easily melted in the heat of the moment" - Paul Nicholls


Top
 Profile  
Reply with quote  
PostPosted: Sat Mar 30, 2024 8:47 am 
Online
User avatar

Joined: Thu Dec 11, 2008 1:28 pm
Posts: 10986
Location: England
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.


Top
 Profile  
Reply with quote  
PostPosted: Sat Mar 30, 2024 9:16 am 
Offline
User avatar

Joined: Fri Aug 03, 2018 8:52 am
Posts: 746
Location: Germany
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


Top
 Profile  
Reply with quote  
PostPosted: Mon Apr 01, 2024 2:33 am 
Offline

Joined: Wed Jan 19, 2011 10:20 pm
Posts: 42
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

_________________
"The plastic veneer of civilization is easily melted in the heat of the moment" - Paul Nicholls


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 18 posts ]  Go to page 1, 2  Next

All times are UTC


Who is online

Users browsing this forum: No registered users and 46 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to: