LEP-001: Language Target


Created:Wednesday, May 4, 2022
Author:David Delassus
Category:Compiler Architecture
Status:FINAL

Abstract

This LEP specifies what is the targetted language of Letlang as well as its compilation unit.

Rationale

Letlang is a compiled language, therefore the compiler does not produce bytecode to be interpreted by a virtual machine, and it does not interpret the Abstract Syntax Tree directly.

The compiler produces code in a targetted compiled language that is then assembled into a final executable.

Specification

Targetted language

The Letlang compiler produces Rust[1] code.

It was chosen because:

  • Rust’s type system and language features matches well the goals of Letlang
  • The Rust compiler targets LLVM IR[2], allowing Letlang to run on all platforms supported by LLVM[3]
  • Rust has no garbage collector, instead it relies on the borrow checker to keep track of objects and automatically calls their destructor when they go out of scope

Compilation unit

Rust’s compilation unit is the crate.

A crate is a tree of modules:

// root module

mod child_module {
  // ...
}

mod other_child; // located in other_child.rs or other_child/mod.rs

It is either:

  • a library crate: compiled to a .rlib file (an AR archive of machine code)
  • a binary crate: compiled to a final executable

Letlang’s compilation unit is a module. Each Letlang file is a module:

module hello::world;

# ...

Each module is compiled to a single Rust library crate, and linked together in a final binary crate.

NB: Each Letlang module can then be used as-is in a Rust project.

Runtime

Each Letlang module requires the Rust crate letlang_runtime, providing the core features of the language.

Rejected Ideas

Assembly Language

There are too many CPU architecture to target, it would restrict Letlang’s availability to only one platform and operating system during development.

From a security point of view, this would be too much work to ensure memory safety, thread safety, etc… on all targets.

LLVM IR

While this allow the compiler to target many platform at once, this target is still too low-level. A lot of work would be needed to ensure memory safety, thread safety, etc…

References

ReferenceTitleLink
1Rusthttps://www.rust-lang.org/
2LLVM IRhttps://en.wikipedia.org/wiki/LLVM#Intermediate_representation
3LLVMhttps://llvm.org/