Page 1 of 2

[Update 2] Switch Compiler - Milestone 1

Posted: Sun Mar 19, 2006 11:26 pm
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!

Posted: Sun Mar 19, 2006 11:37 pm
by Inf0Byt3
Brilliant! Congratulations for this compiler. I can't wait for the final release. Nice optimizations :shock: !

Posted: Sun Mar 19, 2006 11:43 pm
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.

Posted: Sun Mar 19, 2006 11:47 pm
by Inf0Byt3
BTW, nice idea to make an optimizer included. Any chances on making it cross-platform?

Posted: Sun Mar 19, 2006 11:54 pm
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.

Posted: Mon Mar 20, 2006 3:08 pm
by Trond
No subroutines yet? (Very good generated code, by the way.)

Posted: Mon Mar 20, 2006 3:41 pm
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

Posted: Mon Mar 20, 2006 4:47 pm
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(]

Posted: Mon Mar 20, 2006 6:21 pm
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

Posted: Mon Mar 20, 2006 9:19 pm
by Nik
I would implement strings after the first commands so one can get something working^^

Posted: Mon Mar 20, 2006 9:21 pm
by Trond
Switch Compiler
What should I switch to?

Posted: Mon Mar 20, 2006 9:40 pm
by Nik
See name of Developer

Posted: Mon Mar 20, 2006 9:43 pm
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

Posted: Mon Mar 20, 2006 9:46 pm
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

Posted: Tue Mar 21, 2006 12:46 am
by Dare2
Trond wrote:
Switch Compiler
What should I switch to?
:D


(Sometimes these things fall flat ..)