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
    The question in your title and the questions in the question text don't seem to match up. – immibis Mar 6 '17 at 21:34
  • @immibis The question in the title is more general and the details in the body are more specific. It's setting a range of scope. I should have specified it more as an example of what I'm talking about, sorry. My mind is fragmented. (Off-topic: Aren't you a Minecraft modder? The operating system's written on ComputerCraft's computers are somewhat an inspiration for my train of thought.) – Zachary Johnson Mar 6 '17 at 21:43
  • 1
    The ultimate criterion that must be met for a language to be usable for operating system development is that someone must be able to write an operating system in it. If nobody's tried writing an operating system in Terra before, then you could always find out. – immibis Mar 6 '17 at 22:16
  • @immibis I found something called eLua, that you can boot straight into and run on the "bare metal". It's for embedded systems and has a few operating system features built in "like different File Systems, a command Shell, a remote console for Terminal access, ...". You can also upload Lua files via FTP and run them straight on the board, as well as running them through the file system. I'm just going to hazard a guess and say that this is what I'm looking for, but I might need a bit more of an explanation (also on using other Lua libraries/Terra). – Zachary Johnson Mar 6 '17 at 22:34
  • 1
    @NicolBolas Well ALL software has assembly at its base, so you could argue that anything and everything you see on a computer is "written" in assembly. I tried answering my own second question, "How to meet missing criteria", and was too dumb to state that. I guess, more accurately, eLua is supposedly a Lua interface to the hardware, and that is the kind of thing I am looking for. And the Terra language is metaprogrammed by Lua and can be saved as assembly code, so you could technically access the hardware through Lua that way as well. – Zachary Johnson Mar 7 '17 at 18:15
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).

  • I define an operating system as whatever is on the machine right now. Just to add one more way to define it. – Clearer Apr 17 '18 at 10:24
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.