carp-lang / carp-lang/Carp

Separate Compilation

Open
#889 6 comments 0 reactions 0 assignees View on GitHub
difficult nice-to-have
Dominant language
Haskell
Stars
6k
Forks
187
Avg merge
8d 22h
Merged PRs (30d)
4

Description

##### Disclaimer : maybe I've missed something and this is already in the work, but I haven't been able to find anything in the issues / PRs. Sorry if this issue is stupid/redundant

As I explore and write more with the language, one drawback of the current compiler is apparent : it does not support separate compilation and linking, which mainly means that any used library are compiled from source at the same time as the program. This makes the compilation of any program slower (both for the *carp* compiler and the *c* compiler which has to handle a big C file), and I imagine it will get slower as the size of the standard library increases.

A solution to this problem is to allow for separate compilation : that is allowing to compile a unit of code (let's say a file) to an intermediary representation that can be linked to the complete program. This will allow to compile the core library (or any other) once, and being able to link it when using it into other programs.

Here is my precise proposal for carp: when compiling a project, add an option `--as-lib` to compile as a linkable library. The compiler would then generate three artifacts:
* a *.o* file containing the implementation of the library (compiled as normal, the only difference beeing asking the c compiler to not link it)
* a *.h* interface file that will be used by the C compiler when treating a program using the library
* a *.carpi* (?) interface file that will be used by the Carp compiler when treating a program using the library. This last one will mainly contain the types of the values defined in the source file. I'm not yet sure what format this one would have (text or binary). The second one would probably be more efficient

These artifacts could then be used when loading a compiled library, with a directive such as `(static-load "lib.carpi")` which would tell the compiler to:
* use the *lib.carpi* interface when type/borrow-checking the current program
* `#include "lib.h"` in the generated c file
* ask the C compiler to link *lib.o* when linking the final executable

Now there may be something I'm missing (in particular regarding the interactions between the repl and a statically compiled library), but this seems doable (although it would need a bit of work). Please tell me what you think ^^

Contributor guide

Open the contributing guide

Research direction

Start by tracing the compiler entry point for project compilation and the C compiler/linking path described in the issue. Then investigate how a proposed --as-lib option and static-load directive would interact with type checking, generated .h/.o files, .carpi interfaces, and the REPL; done means the design and required artifacts are agreed and separate libraries can be compiled and linked.

Written by the indexing model from the issue text.

Assessment

Tech stack
c, haskell
Domain
build-system, compilers
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.