Compiler tutorial: suggest more stuff

Applications, Games, Tools, User libs and useful stuff coded in PureBasic
Teng
User
User
Posts: 21
Joined: Thu Aug 27, 2009 12:13 pm

Post by Teng »

Trond wrote:What I did was to look a lot on PB's assembly output. When I was unsure of how to translate a certain expression into assembly, I just typed the expression into PB and compiled it to asm (I have a batch file to do it from the tools menu). Very handy.

And again (I mentioned this in the tutorial, but I mention it again), read fasm.pdf on the relevant instructions. It has quite easy to understand info (at least compared to intel's manuals). The book "The art of assembly", I can't recommend, because it uses a different syntax.
How do you look into PB's assembly output? I'm guessing you used the command line compiler right?. From the PB helpfile :
/COMMENTED : create a commented '.asm' output file when creating an executable. This file can be re-assembled later when the needed modifications have been made. This option is for advanced programmers only.
Neat trick. Thanks for sharing that piece of information. :wink:
Trond
Always Here
Always Here
Posts: 7446
Joined: Mon Sep 22, 2003 6:45 pm
Location: Norway

Post by Trond »

Teng wrote:How do you look into PB's assembly output? I'm guessing you used the command line compiler right?. From the PB helpfile :
Yes, I have a batch file that calls the command line compiler, like this:

Code: Select all

rem @echo off
cd c:\
"c:\program files\purebasic 4.40\compilers\pbcompiler" %1 %2 /COMMENTED
if errorlevel 1 goto wait
cd %1
start PureBasic.asm
exit
:wait
pause
Then I have added this tool:
Image
If you substitute your own paths and assign a program to open asm files, you only have to select a menu item and suddenly you have the asm code.
Trond
Always Here
Always Here
Posts: 7446
Joined: Mon Sep 22, 2003 6:45 pm
Location: Norway

Re: Compiler tutorial: suggest more stuff

Post by Trond »

I added a new chapter on IF statements. It's very long because some support code is needed to allow for multiple lines, but it should be easy enough to understand.
User avatar
fsw
Addict
Addict
Posts: 1603
Joined: Tue Apr 29, 2003 9:18 pm
Location: North by Northwest

Re:

Post by fsw »

Trond wrote:No, the parameter is a character, not a number. ...
Yeah sorry, treated your function the wrong way (used "normal" numbers...)

Keep up the good work :mrgreen:
Sabour
User
User
Posts: 31
Joined: Sun Apr 02, 2006 7:41 am

Re: Compiler tutorial: suggest more stuff

Post by Sabour »

still following the tutorial!,keep it up
Trond
Always Here
Always Here
Posts: 7446
Joined: Mon Sep 22, 2003 6:45 pm
Location: Norway

Re: Compiler tutorial: suggest more stuff

Post by Trond »

I'm working on the symbol table, it's a bit hard to write a fully functional symbol table in a small snippet that is easy to explain. But it's coming!
Trond
Always Here
Always Here
Posts: 7446
Joined: Mon Sep 22, 2003 6:45 pm
Location: Norway

Re: Compiler tutorial: suggest more stuff

Post by Trond »

The symbol table is done. I didn't explain all parts of the code completely, so don't hesitate to ask if something is hard to understand.
Teng
User
User
Posts: 21
Joined: Thu Aug 27, 2009 12:13 pm

Re: Compiler tutorial: suggest more stuff

Post by Teng »

Hi Trond, came across a good free book you could use as a supplement to your tutorial :
'Understanding and Writing Compilers : A do-it-yourself-guide by Richard Bornat', nice book rated highly by others. Hope it helps. :)
http://www.cs.mdx.ac.uk/staffpages/r_bo ... piling.pdf
Trond
Always Here
Always Here
Posts: 7446
Joined: Mon Sep 22, 2003 6:45 pm
Location: Norway

Re: Compiler tutorial: suggest more stuff

Post by Trond »

Hi
Most compiler books sucks for one or more reasons:
0. Most books shows how to write a compiler in the way industrial-quality, optimizing compilers are written by teams to 50 people working every day for several years. They're not suitable for normal people.
1. They're too long winded and never get to the meat. Your book doesn't even start with covering the compilation of aritmetic expressions until page 81.
2. Most books use tree structures. Compiling a program consists of translating a list of instructions into a new list of instructions. Books use tree structures because "it's intuitive". We want to translate a list to a list. Tree structures aren't intuitive here.
4. Too many unnecessary and complicated algorithms.
Teng
User
User
Posts: 21
Joined: Thu Aug 27, 2009 12:13 pm

Re: Compiler tutorial: suggest more stuff

Post by Teng »

Heh, yeah have to agree with you but for me most books on compilers suck because I can't understand them :? (probably more likely that I suck). Surely you could recommend some books on the subject that you have referenced from because you didn't just wake up one day and found out that you knew how to make a compiler. Surely you must have read some good books on compilers. I'm actually more interested in making an interpreter, I reason that if I follow your tutorial and emulate the stack as well as other fasm opcodes in any language (not just PureBasic), that this would be possible. Is my reasoning wrong? I'm not going to debate with you on the pros and cons of using tree structures because this subject is way out of my depth, so I'll take your word for it that a tree structure is less intuitive.
Trond
Always Here
Always Here
Posts: 7446
Joined: Mon Sep 22, 2003 6:45 pm
Location: Norway

Re: Compiler tutorial: suggest more stuff

Post by Trond »

Heh, yeah have to agree with you but for me most books on compilers suck because I can't understand them (probably more likely that I suck)
Right, I don't understand them either.
Surely you could recommend some books on the subject that you have referenced from because you didn't just wake up one day and found out that you knew how to make a compiler. Surely you must have read some good books on compilers.
I read Jack Crenshaw's let's write a compiler. I have the dragon book but it's pure nonsense to normal people like us.
I'm actually more interested in making an interpreter, I reason that if I follow your tutorial and emulate the stack as well as other fasm opcodes in any language (not just PureBasic), that this would be possible. Is my reasoning wrong?
There's two ways to make an interpreter. On way is to compile the program like I show, but instead generate a different set of instructions (like I do, only you could make the instruction set simpler). Then you make another program to interpret these instructions.
The other way is to interpret the program directly. In this case you will have to return the return the the actual value from functions such as AddOp(). You will learn a lot from following this tutorial in either case.
I'm not going to debate with you on the pros and cons of using tree structures because this subject is way out of my depth, so I'll take your word for it that a tree structure is less intuitive.
This is my point precisely. If we a have a complicated structure that we don't intuitively understand how to process, and have a simple structure that we do now how to process, because it's a simple list, why should we do it the hard way? In our case we don't even use the list, we just output asm instructions directly. For more optimization, they can be stored to a linked list and optimizations be performed on that list.
User avatar
DoubleDutch
Addict
Addict
Posts: 3220
Joined: Thu Aug 07, 2003 7:01 pm
Location: United Kingdom
Contact:

Re: Compiler tutorial: suggest more stuff

Post by DoubleDutch »

Just looked at the tutorials, they are great. Please keep them up. :)

Whats coming next?
https://deluxepixel.com <- My Business website
https://reportcomplete.com <- School end of term reports system
Trond
Always Here
Always Here
Posts: 7446
Joined: Mon Sep 22, 2003 6:45 pm
Location: Norway

Re: Compiler tutorial: suggest more stuff

Post by Trond »

DoubleDutch wrote:Whats coming next?
Function calls to externally defined functions.
User avatar
DoubleDutch
Addict
Addict
Posts: 3220
Joined: Thu Aug 07, 2003 7:01 pm
Location: United Kingdom
Contact:

Re: Compiler tutorial: suggest more stuff

Post by DoubleDutch »

Like API calls? That would be good.
https://deluxepixel.com <- My Business website
https://reportcomplete.com <- School end of term reports system
Trond
Always Here
Always Here
Posts: 7446
Joined: Mon Sep 22, 2003 6:45 pm
Location: Norway

Re: Compiler tutorial: suggest more stuff

Post by Trond »

DoubleDutch wrote:Like API calls? That would be good.
Function calls in general, except function definition wouldn't be covered yet.
Post Reply