virgil icon indicating copy to clipboard operation
virgil copied to clipboard

Feature: Compile to standalone x86_64 code for bare metal.

Open ApplePieCodes opened this issue 1 year ago • 4 comments

If possible, could you attempt to implement this feature. It would make Operating System Development much easier.

ApplePieCodes avatar Jun 16 '24 13:06 ApplePieCodes

Interesting, and not too far-fetched. Currently the compiler can produce ELF and Mach-O x86-64 binaries. In both cases, the compiler inserts only a little bit of startup asm code, and the rest of the runtime system and GC is written in Virgil. There are a handful of syscalls in the compiler-inserted asm code to set up signal handling and the stack, but the rest is in Virgil using Linux.syscall or Darwin.syscall. So switches to turn off that would allow generating a standalone ELF file, which a bootloader could load into memory.

titzer avatar Jun 16 '24 14:06 titzer

Maybe add an asm running command like rust's asm!().

ApplePieCodes avatar Oct 25 '24 11:10 ApplePieCodes

I'm interested in this as well. I've been developing a kernel in Nim, but I'm always on the lookout for interesting system programming languages. I'd love to give Virgil a try, but without official bare metal support it would be hard.

Things I look for in a language to support kernel programming:

  • Freestanding (no stdlib, no OS) output
  • Customizable virtual memory page allocator
  • Deterministic memory management (no random GC pauses)
  • Unsafe pointers
  • Custom data layout (Virgil seems to have nice support here, but it's byte-level, would be nice if there's bit-level layout support as well)
  • Naked functions (no prolog/epilog) for interrupt handlers

Nice to have:

  • Customizing executable layout
  • Inline assembly, or at least a stable ABI
  • UEFI target (for the bootloader)

I'm pretty sure I forgot other things, but that would be a good start.

khaledh avatar Nov 08 '24 02:11 khaledh

I'm interested in this as well. I've been developing a kernel in Nim, but I'm always on the lookout for interesting system programming languages. I'd love to give Virgil a try, but without official bare metal support it would be hard.

Cool! Hacking up a hobby kernel was one of my favorite things after learning C.

Things I look for in a language to support kernel programming:

* Freestanding (no stdlib, no OS) output

I think this one is a ✅; the Virgil compiler can generate standalone ELF files, and you can adjust the base addresses.

* Customizable virtual memory page allocator

This one is below the level of the language; you should be able to poke page tables raw with Pointer.

* Deterministic memory management (no random GC pauses)

Virgil isn't ideal here; objects and arrays are still allocated on the heap. But all allocations are syntactically visible, so it's possible to write a significant amount of tidy code without allocating (e.g. even using ranges, tuples, layouts, with wild abandon).

* Unsafe pointers

Have it :-) :-(

* Custom data layout (Virgil seems to have nice support here, but it's byte-level, would be nice if there's bit-level layout support as well)

There is some bit-level support with packed ADTs, but it's not stable yet. Layouts could probably use an extension to express sub-word quantities.

* Naked functions (no prolog/epilog) for interrupt handlers

This is tricky; probably need to have little asm stubs that get into/out of Virgil code, kind of like Wiard.

Nice to have:

* Customizing executable layout

Can do this with command-line flags.

* Inline assembly, or at least a stable ABI

ABI is pretty stable; it's basically System-V.

* UEFI target (for the bootloader)

Not sure what that entails. Is it enough to have a naked ELF?

I'm pretty sure I forgot other things, but that would be a good start.

titzer avatar May 03 '25 15:05 titzer