Writing an OS in a custom language

Programming, for all ages and all languages.
KillerX
Posts: 2
Joined: Tue Sep 04, 2007 5:21 pm
Location: India
Contact:

Writing an OS in a custom language

Post 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
User avatar
Alboin
Member
Member
Posts: 1466
Joined: Thu Jan 04, 2007 3:29 pm
Location: Noricum and Pannonia

Post by Alboin »

Write a compiler with spawn disabled?
C8H10N4O2 | #446691 | Trust the nodes.
KillerX
Posts: 2
Joined: Tue Sep 04, 2007 5:21 pm
Location: India
Contact:

Post 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
User avatar
AndrewAPrice
Member
Member
Posts: 2299
Joined: Mon Jun 05, 2006 11:00 pm
Location: USA (and Australia)

Re: Writing an OS in a custom language

Post 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.
My OS is Perception.
User avatar
JamesM
Member
Member
Posts: 2935
Joined: Tue Jul 10, 2007 5:27 am
Location: York, United Kingdom
Contact:

Post 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
User avatar
Solar
Member
Member
Posts: 7615
Joined: Thu Nov 16, 2006 12:01 pm
Location: Germany
Contact:

Post by Solar »

...called "bootstrapping".

It's like that magic moment when your OS is capable of building itself. ;)
Every good solution is obvious once you've found it.
kubeos
Member
Member
Posts: 138
Joined: Tue Jan 30, 2007 2:31 pm
Location: Kamloops BC, CANADA
Contact:

bootstrapping

Post 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:
User avatar
binutils
Member
Member
Posts: 214
Joined: Thu Apr 05, 2007 6:07 am

Post by binutils »

DeletedAccount
Member
Member
Posts: 566
Joined: Tue Jun 20, 2006 9:17 am

Solar is correct

Post 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 ???
User avatar
crazygray
Member
Member
Posts: 73
Joined: Sat Nov 03, 2007 10:17 am
Location: Toky,Japan

Post by crazygray »

How would you go about writing a compiler? Do you need to know machine language first.
Imagine if a creature came from a 4 dimensional world, would he think you to be flat?
Crazed123
Member
Member
Posts: 248
Joined: Thu Oct 21, 2004 11:00 pm

Post by Crazed123 »

If you asked that question, you probably can't write a very good compiler.

Take a compilers class.
User avatar
AndrewAPrice
Member
Member
Posts: 2299
Joined: Mon Jun 05, 2006 11:00 pm
Location: USA (and Australia)

Post 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.
My OS is Perception.
User avatar
crazygray
Member
Member
Posts: 73
Joined: Sat Nov 03, 2007 10:17 am
Location: Toky,Japan

Post by crazygray »

I was more looking for the most effective way to write a compiler.
Imagine if a creature came from a 4 dimensional world, would he think you to be flat?
User avatar
Brynet-Inc
Member
Member
Posts: 2426
Joined: Tue Oct 17, 2006 9:29 pm
Libera.chat IRC: brynet
Location: Canada
Contact:

Post by Brynet-Inc »

crazygray wrote:I was more looking for the most effective way to write a compiler.
Heavily intoxicated? :lol:
Image
Twitter: @canadianbryan. Award by smcerm, I stole it. Original was larger.
User avatar
babernat
Member
Member
Posts: 42
Joined: Tue Jul 03, 2007 6:53 am
Location: Colorado USA

Post 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.
Thanks for all the fish.
Post Reply