flat assembler
Message board for the users of flat assembler.

Index > Heap > On writing & porting compilers

Author
Thread Post new topic Reply to topic
TmX



Joined: 02 Mar 2006
Posts: 822
Location: Jakarta, Indonesia
TmX
I'm still confused how compilers are written, let alone ported to different OSes/CPU architectures.
At the moment, I have 3 questions on my mind.

1. How to write the compiler?
Let's say I've designed a programming language 'XYZ', which is completely new and not based on any existing language before. So how do I write the compiler for this language? I have to use any existing compiler, right?

2. How to make it self-hosting?
Some compilers are written in themeselves (FASM is in FASM, GCC is in C, Free Pascal is in Pascal, etc). Let's say the 'XYZ' compiler is already stable enough. Then you should be able to write 'XYZ' in itself, right?
For example, FASM was initally written in TASM. Then it was able to compile itself.

3. How to port it to different OSes/CPU architectures?
Now the 'XYZ' compiler is mature enough. How do I port it to another OS?
Do I have to repeat step 1 & 2 on that OS?

I already take a look at the Dragon Book (the old one), but the explanation seems vague to me.
Any good websites/papers as references would be much appreciated Smile
Post 31 Mar 2010, 03:51
View user's profile Send private message Reply with quote
bitshifter



Joined: 04 Dec 2007
Posts: 764
Location: Massachusetts, USA
bitshifter
1) Design syntax and parse into opcodes with 'x' language/compiler.
2) Rewrite code in new syntax and self compile.
3) Only file/video/input routines need porting.
Jack Crenshaw wrote a nice series about the process.
http://compilers.iecc.com/crenshaw/
The hard part is to support many architecture.
Post 31 Mar 2010, 04:06
View user's profile Send private message Reply with quote
Tyler



Joined: 19 Nov 2009
Posts: 1216
Location: NC, USA
Tyler
1.Fasm's source would be a simple start. But, if 'XYZ' is an HLL, it would take a lot of parsing and stuff, but your goal would be to turn the directives in the 'XYZ' source file into opcodes that would accomplish the intended purpose. You would have to use an existing language to write your compiler in.

2.Get to the point that it's functional enough to write your compiler in.
TmX wrote:

FASM was initally written in TASM. Then it was able to compile itself.

That's different. Fasm was always in asm, all Tomasz had to do(not that I'm saying it was trivial) to convert it from Tasm dialect to Fasm dialect was convert the few differences. It would be much harder to rewrite the compiler in a completely new language.

3.I don't know.
Post 31 Mar 2010, 04:12
View user's profile Send private message Reply with quote
Artlav



Joined: 23 Dec 2004
Posts: 188
Location: Moscow, Russia
Artlav
1,2,3. Try to write a compiler or interpreter of a simple subset of an existing language to get a feel for it.

After that, spend some quality time thinking what do you want to get.

What's the language, how would each structure work, how will each of them translate to assembler for each target platform/OS. If something doesn't understandeth, try to do it simple way to get a feel for how it works, then keep working on the plan.

Once it all works on paper, just start coding.

Writing a multi-platform compiler of usable quality is not something you'd likely to succeed in making on the first go. I'm making a third attempt on one already, and it still can't compile itself.
Post 31 Mar 2010, 18:20
View user's profile Send private message Visit poster's website Reply with quote
Dex4u



Joined: 08 Feb 2005
Posts: 1601
Location: web
Dex4u
You are best off starting with fasm macro, with them its very easy to code a new language
see here: http://board.flatassembler.net/topic.php?t=7961
Post 31 Mar 2010, 18:34
View user's profile Send private message Reply with quote
roboman



Joined: 03 Dec 2006
Posts: 122
Location: USA
roboman
very easy? Well compared to writing an os or a compiler.... I'll be back at as soon as I finish getting the garden going for the year. Play around with the macros, play around with writing an interpreter. Both will get you along the path and show you a bunch of problems with what you are thinking, limits of doing things that way and open your eyes as to why languages work the way they do. The next step is probably to write an xyz to asm "compiler" that outputs fasm code and then rewrite it to output directly to machine language. At least that's where I'm at and what I've been doing. But I'm not a really dedicated programmer, it's mostly just playing at some thing that's of interest to me.
Post 01 Apr 2010, 03:20
View user's profile Send private message Visit poster's website Reply with quote
cod3b453



Joined: 25 Aug 2004
Posts: 619
cod3b453
There are books that define many stages of a modern compiler but you essentially need to, in general, do:

- Define a grammar (preferably context-free)
- Build a lexer/parser pair for the grammar that give you an abstract syntax tree (AST)
- Build a code generator that converts the AST into machine code (here you produce architecture/platform-specific code)

You can then insert optimisers, type checkers and so on. As for references, the only book I've used is:

Modern Compiler Implementation in {C/Java/ML} by Andrew Appel
(Same book but using various languages)

I'm currently using GOLD (http://www.devincook.com/goldparser/) to write grammars, which includes engines for most languages including ASM, C, Java.
Post 04 Apr 2010, 16:57
View user's profile Send private message Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  


< Last Thread | Next Thread >
Forum Rules:
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 vote in polls in this forum
You can attach files in this forum
You can download files in this forum


Copyright © 1999-2020, Tomasz Grysztar. Also on YouTube, Twitter.

Website powered by rwasa.