shinh / elvm

EsoLangVM Compiler Infrastructure
MIT License
1.13k stars 143 forks source link

Multi-file compilation? #56

Open adrianparvino opened 5 years ago

adrianparvino commented 5 years ago

Is there a way to build a single output from 2 files?

shinh commented 5 years ago

There is no linker. However, because EIR is a very simple text-based format, just concatenating EIR files may just work. Suppose you have these two files:

$ cat main.c
void foo();
int main() {
    foo();
}
$ cat foo.c
#include <stdio.h>
void foo() {
    puts("Hello, world!");
}

and you can do

$ out/8cc -S -I. -Ilibc -Iout foo.c -o foo.eir
$ out/8cc -S -I. -Ilibc -Iout main.c -o main.eir
$ cat foo.eir main.eir > linked.eir
$ out/elc linked.eir -rb linked.eir > linked.rb
$ ruby linked.rb
Hello, world!

Note that, as cat is not a linker, it cannot raise an error for symbols defined multiple times.

shinh commented 5 years ago

I realized the above wouldn't work for most cases since symbols generated by the compiler (e.g., .L1 for loops) will conflict with each other. Probably, the best workaround we can do today would be just concatenating all C source code to a single C source code before using the compiler. For example, see out/8cc.c after running make test-rb-full

adrianparvino commented 5 years ago

@shinh I am currently working on using clang as a linker, producing LLVM, and https://github.com/JuliaComputing/llvm-cbe to output C from LLVM, then compiling with ELVM.