Page 1 of 2

Writing an OS in a custom language

Posted: Tue Sep 04, 2007 5:49 pm
by KillerX
Hi All,

This query is based on both OS theory and Compilers. Let's say I want to write a new OS in language X. Language X is strongly typed and has all the features that are generally necessary to do systems programming in, but has intrinsic support for concurrency a.k.a CSP (i.e. via channels and a 'spawn' keyword).

The trouble is that multi-threading (or running concurrent processes) is something that is highly dependent on the OS. However, given that the language's purpose itself is to write an OS, I've reached a Catch-22 of sorts.

I want to write an OS in the language, but I can't write a compiler for it until I write the OS itself. Any suggestions?

Cheers,
--
Anant

Posted: Tue Sep 04, 2007 5:53 pm
by Alboin
Write a compiler with spawn disabled?

Posted: Tue Sep 04, 2007 6:05 pm
by KillerX
Alboin wrote:Write a compiler with spawn disabled?
Wow, that was notoriously simple. I guess this means developing a tiny kernel using a subset of the language, and then extend it after spawn has been enabled :)

Many Thanks!

--
Anant

Re: Writing an OS in a custom language

Posted: Tue Sep 04, 2007 9:08 pm
by AndrewAPrice
KillerX wrote:I want to write an OS in the language, but I can't write a compiler for it until I write the OS itself. Any suggestions?
Write a compiler for Linux/Windows to compile the kernel with. That's like writing a compiler for a new language in the language itself.

Posted: Wed Sep 05, 2007 2:45 am
by JamesM
I guess this means developing a tiny kernel using a subset of the language, and then extend it after spawn has been enabled
That's how the java compiler was made. The java compiler is written in java - they made a small, crappy compiler in C, compiled the compiler in that then used the new java compiler to recompile nicely :P

Posted: Wed Sep 05, 2007 5:25 am
by Solar
...called "bootstrapping".

It's like that magic moment when your OS is capable of building itself. ;)

bootstrapping

Posted: Wed Sep 05, 2007 6:07 pm
by kubeos
I wrote my ZC compiler in QBASIC first. Then once I had it working good enough I re-wrote it in ZC. Although if you're planning on doing something like this... maybe QBASIC isn't quite the best place to start... :lol:

Posted: Tue Sep 11, 2007 3:41 pm
by binutils

Solar is correct

Posted: Mon Oct 01, 2007 2:28 am
by DeletedAccount
... bootstrapping ... ,Take a look at project Oberon .. Also i feel that
bootstrapping is sometimes not needed ... Eg when you write C
compiler that implements a subset of standard C using a good C++
compiler .. U rewrite ur compiler with this C compiler .. The readability
of the orginal code is lost ... But it assures that your compiler is
pretty well implemented .... Am i right ???

Posted: Sat Nov 03, 2007 11:00 am
by crazygray
How would you go about writing a compiler? Do you need to know machine language first.

Posted: Sat Nov 03, 2007 1:10 pm
by Crazed123
If you asked that question, you probably can't write a very good compiler.

Take a compilers class.

Posted: Sat Nov 03, 2007 3:59 pm
by AndrewAPrice
crazygray wrote:How would you go about writing a compiler? Do you need to know machine language first.
99%+ of compilers output to assembly, and then call an assembler, then your linker. Of course you could manually output directly to a binary, just get Volume 3 of the Intel docs and it contains a listing of every instruction including their opcode and usage.

Posted: Fri Nov 09, 2007 4:23 pm
by crazygray
I was more looking for the most effective way to write a compiler.

Posted: Fri Nov 09, 2007 5:18 pm
by Brynet-Inc
crazygray wrote:I was more looking for the most effective way to write a compiler.
Heavily intoxicated? :lol:

Posted: Fri Nov 09, 2007 9:17 pm
by babernat
crazygray wrote:I was more looking for the most effective way to write a compiler.
Effective? Dunno about that, the easiest way I can think of is first pick up a compiler book. I'd suggest the dragon book by Aho. Writing a compiler is a very satisfying project. I was required to write one from scratch in school. If it didn't work you failed and you weren't allowed to use any of the compiler tools. It's difficult in, but very rewarding when you do it. So, um get ready for the fire hose.

Your compiler will basically follow this pattern:

1) Lexical Analysis (develop tokens from the source it is compiling). You can use one of the many compiler tools to generate this code. If you don't want to do that, then develop a FSM that can detect the lexemes.

2) Syntax Analysis. If the language is of a certain form and you are smart in writing your grammar, then you can develop a LL(1) grammar which requires a look ahead of only 1. Then you can develop a recursive descent parser that can parse the language. This is where you will spend A LOT of time on paper before you even begin coding. Design before you implement! It can be painful if you don't.

3) Intermediate code generation. A lot of people use separate syntax trees in order to generate code, but if you do a recursive descent parser, you're really walking the syntax tree already so just go ahead and do your code gen at that point while parsing. This can be accomplished by smartly passing parameters and returning values from the methods in your parser.

4) From your intermediate code you can gen the asm or do the machine code directly.


To do it right is no small project, but it is still one of the most loved and hated things I have ever programmed.