4

For example, a language that I was looking at recently- Terra. You can address the question in the context of any language, I'm just most comfortable with Lua so I'm starting from there.

The Terra Language can be metaprogrammed with Lua and is backwards compatible with C. C code can be used inside of it and its code can be used inside of C. I'll give some code examples from the main page.

terra hello(argc : int, argv : &rawstring)
    -- Here we call a C function from Terra
    C.printf("Hello, Terra!\n")
    return 0
end

-- You can call Terra functions directly from Lua, they are JIT compiled 
-- using LLVM to create machine code
hello(0,nil)

hello:disas()
--[[ output:
    assembly for function at address 0x60e6010
    0x60e6010(+0):      push    rax
    0x60e6011(+1):      movabs  rdi, 102129664
    0x60e601b(+11):     movabs  rax, 140735712154681
    0x60e6025(+21):     call    rax
    0x60e6027(+23):     xor eax, eax
    0x60e6029(+25):     pop rdx
    0x60e602a(+26):     ret
]]

Save Terra code to an external representation such as an object file, or executable. filetype can be one of "object" (an object file *.o), "asm" (an assembly file *.s), "bitcode" (LLVM bitcode *.bc), "llvmir" (LLVM textual IR *.ll), or "executable" (no extension).

Now, what I want to know (in relation to Terra and/or in general):

  • Does this mean that I can write an operating system in Lua? (Is this the level of criteria I should be looking for when examing frameworks and languages?)
  • If so, can I also implement Lua libraries such as Torch? (All languages have libraries, so I'm curious about what defines how much they can make use of them through OS development).
  • Is there an objectively better way or language to do this? (As in, when does a language fully meet the critera yet not hold viability when it comes to operating system development?)

2 Answers2

2

First, you need to understand and define precisely what exactly is an operating system. And the definition is not that simple, and is ambiguous in the details. I suggest to spend days to read Operating Systems : Three Easy Pieces.

Keep in mind that the definition of an OS may vary. For some people, it is what is installed in a (consumer) computer when you buy it brand new. For other people, it could be just the operating system kernel (but they recognize that a kernel alone is completely useless: you need some user space programs and some application software). Read also about unikernels (such as MirageOS) and about projects like Singularity OS or Safe. Of course read about microkernels & monolithic kernels. The OSDev wiki has useful information. The tunes.org site is archiving interesting discussions (from the previous century) related to OS design.

Notice also that Terra is not Lua. It is a semantically C-like language which uses Lua for metaprogramming.

Does this mean that I can write an operating system in Lua?

You won't write all your OS in Lua (or in Terra). Likewise, those coding an OS in C (which is the case for most OSes today) don't code all the OS in C (some parts need to be coded in assembler).

Your question is ambiguous: do you want a language with which most of the OS can be coded (but then you need to code missing parts otherwise, perhaps in assembler), or do your require a language with which all the OS will be coded?

Low level things (scheduler, MMU and virtual space management, interrupt handling, hardware related stuff) are difficult (and probably impossible) to code "portably" in a pure high-level programming language. Because details matter.

See also this answer to a relevant and related question (but not the same one).

2

Er, it depends.

Operating Systems can be written in any language. JavaOS was written almost entirely in Java as a bet, ran slowly, and then had to be sold as a product. Most requirements, like performance, matter less than one expects because low level assembly or C code are dealing with the innermost loops. Real speed-ups, like a smarter scheduler or LRU cache, can be coded anywhere.

Some languages, like Python, make terrible choices because the large virtual machine architecture requires a slow start up time. Some are tightly tied to ideal architectures and can't deal with GPUs, managing layers of cache, etc. Some ideas are improvements in the small area. There are a lot of languages.

Importantly, making your own language is common. In Silicon Valley, there is at least one MeetUp where people design languages, usually compiling with LLVM or the like. In my opinion, start by writing a list of principles equivalent to the "Zen of Python". Contemplate how your language will be a major improvement over all others.

Go forth and code.