[Update 2] Switch Compiler - Milestone 1

Developed or developing a new product in PureBasic? Tell the world about it.
Killswitch
Enthusiast
Enthusiast
Posts: 731
Joined: Wed Apr 21, 2004 7:12 pm

[Update 2] Switch Compiler - Milestone 1

Post by Killswitch »

[Update]

I've updated the compiler a little bit. Now exponentials (i.e. 2^10) are fully supported! That's right you can use variables as operands (that feature was missing before). Exponential code is fairly fast (faster than PBs Pow() function in my tests) but if 0 or 1 is used as the second operand (b out of a^b) then the code gets reduced to one or two instructions!

More useful updates (IFs, loops and actual commands) will be added as and when I have time - which is a bit scarce at the moment! Note: this is the only link in this post that points to the newsest version!

DOWNLOAD

[Old news]

Ever since I hung up development on my scripting language I've been planning, and working on, a fully fleged compiler. This release is the first fruits of my labour, and I thought I'd share it with you all.

At the moment the compiler can only handle numeric expressions (floats and intergets, as well as variables). Comments (lines starting with ' or as a comment after a line .ie. "1+1 'Comment") are also supported. There's two .exe's in the zip and a text file. Write your 'code' in Code.txt then run Compiler.exe - it's that easy. A file called ASM.txt will be created in that directory containing the outputted ASM.

My overall goal is to create a working modular compiler which is losely based around BASIC rules. I've decided on having quite a long pipeline (each element of which can be swapped with another user creation, so the language is very user modifiable) but I will, eventually, add some switches to the compiler which means you can skip unessacary stages.

For example:

In the .exe you have 'Compiler.exe' and 'Optimizer.exe'. The code produced by 'Compiler.exe' is fuly working but 'Optimizer.exe' speeds it up by a fair amount. So, you could skip the second stage in order to speed up compile time but reduce the eventual .exe's speed (and increase it's size).

I imagine the final pipeline will be something like this (with skippable bits between *s):

*Front end* > *Preprocessor* > Compiler > *Optimizer* > FASM

Anyway, I hope you enjoy! Suggestions, comments and quieres are, as always, welcome! Big thanks to Fred and all the other guys on here who've helped me along the way! Xombie also deserves a special mention for inspiring to speed up most of my code (and making it work properly :oops:) :P.

DOWNLOAD (Latest version - see below)

As a closing note: I haven't included the source with this release. Why? Well to be honest there's not much actual content - most of the code revolves around my RPN/PostfixToASM procedures which are already floating about on the forums. If you'd like to see the source I can upload it!
Last edited by Killswitch on Wed Mar 22, 2006 11:39 pm, edited 2 times in total.
~I see one problem with your reasoning: the fact is thats not a chicken~
Inf0Byt3
PureBasic Fanatic
PureBasic Fanatic
Posts: 2236
Joined: Fri Dec 09, 2005 12:15 pm
Location: Elbonia

Post by Inf0Byt3 »

Brilliant! Congratulations for this compiler. I can't wait for the final release. Nice optimizations :shock: !
None are more hopelessly enslaved than those who falsely believe they are free. (Goethe)
Killswitch
Enthusiast
Enthusiast
Posts: 731
Joined: Wed Apr 21, 2004 7:12 pm

Post by Killswitch »

Ah, that's a point. The optimizer deletes the original code, so you only ever see the optimized version. Essentially it replaces a lot of Push/Pop statments with Mov statments - which I tested and are faster.
~I see one problem with your reasoning: the fact is thats not a chicken~
Inf0Byt3
PureBasic Fanatic
PureBasic Fanatic
Posts: 2236
Joined: Fri Dec 09, 2005 12:15 pm
Location: Elbonia

Post by Inf0Byt3 »

BTW, nice idea to make an optimizer included. Any chances on making it cross-platform?
None are more hopelessly enslaved than those who falsely believe they are free. (Goethe)
Killswitch
Enthusiast
Enthusiast
Posts: 731
Joined: Wed Apr 21, 2004 7:12 pm

Post by Killswitch »

Well, I'm using PureBasic for the compiler, and FASM as the backend so cross platformness (is that even a word?) is always possible, however I don't have a Linux or OS X box to work on so it's quite unlikely.
~I see one problem with your reasoning: the fact is thats not a chicken~
Trond
Always Here
Always Here
Posts: 7446
Joined: Mon Sep 22, 2003 6:45 pm
Location: Norway

Post by Trond »

No subroutines yet? (Very good generated code, by the way.)
Nik
Addict
Addict
Posts: 1017
Joined: Fri May 13, 2005 11:45 pm
Location: Germany
Contact:

Post by Nik »

Hmm your compiler does work with wine I had to copied the ASM output of the example code to purebasic and inlined it works very good
remi_meier
Enthusiast
Enthusiast
Posts: 468
Joined: Sat Dec 20, 2003 6:19 pm
Location: Switzerland

Post by remi_meier »

Nice one!
Are parentheses supported?

Code: Select all

;C=(B*(2+C))
Push dword [vB]
Mov edx, 2
Mov ebx, dword [vC]
Add ebx, edx
Mov ebx, ebx
Pop edx
imul ebx, edx
Push ebx
Mov dword [vC], dword [v(]
Athlon64 3700+, 1024MB Ram, Radeon X1600
Killswitch
Enthusiast
Enthusiast
Posts: 731
Joined: Wed Apr 21, 2004 7:12 pm

Post by Killswitch »

@remi_meier

Thanks mate you pointed out a bug there - nothing major I just forgot to pop '(' off the stack when I was converting the equation to RPN format.

I've uploaded a new version which squashes this bug.

Yep, the following operaters are supported:

( ) + - * /

Expontials (i.e. 2^2=4) are also supported BUT only if you're using it in a line with a constant value:

Code: Select all

  A=2^10 ;Fine
  B=A^10 ;Not good because of the use of a variable
Leading negative numbers also work (i.e. -1*10=-10).

There's no commands (that's what you mean by subroutines?) as of yet because I'm building this project up a piece at a time. Byte and word will be added, then strings then commands lastly once all the other pieces are in place.

The outputted ASM is fairly good thanks to the optimizer - the stuff the compiler actually produces is a bit kack (but works perfectly well, it's just slooooww :P).

Update:


DOWNLOAD
~I see one problem with your reasoning: the fact is thats not a chicken~
Nik
Addict
Addict
Posts: 1017
Joined: Fri May 13, 2005 11:45 pm
Location: Germany
Contact:

Post by Nik »

I would implement strings after the first commands so one can get something working^^
Trond
Always Here
Always Here
Posts: 7446
Joined: Mon Sep 22, 2003 6:45 pm
Location: Norway

Post by Trond »

Switch Compiler
What should I switch to?
Nik
Addict
Addict
Posts: 1017
Joined: Fri May 13, 2005 11:45 pm
Location: Germany
Contact:

Post by Nik »

See name of Developer
Killswitch
Enthusiast
Enthusiast
Posts: 731
Joined: Wed Apr 21, 2004 7:12 pm

Post by Killswitch »

It's a horrible name, but I really couldn't think of anything better - and I hate things being so generic that they may as well be the materilsation of begie.

I'll think of a better name before the next release :D
~I see one problem with your reasoning: the fact is thats not a chicken~
Inf0Byt3
PureBasic Fanatic
PureBasic Fanatic
Posts: 2236
Joined: Fri Dec 09, 2005 12:15 pm
Location: Elbonia

Post by Inf0Byt3 »

Switch is cool with me 8) .

[Edit]

It could mean something:
S(uper)W(Wonderfull)I(Intelligent)T(True)C(Compiler), etc.. :D
None are more hopelessly enslaved than those who falsely believe they are free. (Goethe)
Dare2
Moderator
Moderator
Posts: 3321
Joined: Sat Dec 27, 2003 3:55 am
Location: Great Southern Land

Post by Dare2 »

Trond wrote:
Switch Compiler
What should I switch to?
:D


(Sometimes these things fall flat ..)
@}--`--,-- A rose by any other name ..
Post Reply