I am following the kaleidoscope tutorial. Emitting object code is very simple, but now I would like to implement linking step so that my toy programming language could compile directly into a binary (so there is no clang
usage necessary). How can I achieve this with LLVM?
Asked
Active
Viewed 1,131 times
2

Bhargav Rao
- 50,140
- 28
- 121
- 140

gruszczy
- 40,948
- 31
- 128
- 181
-
I'm a bit confused as to what is desired. Is it [this](https://llvm.org/docs/tutorial/LangImpl08.html#putting-it-all-together) bit of the tutorial that you're referring to? Also, you write that "no `clang` usage" should be necessary, but then you ask how to "achieve this with LLVM". What's the case? – compor Jan 03 '18 at 07:39
1 Answers
2
As far as the "no clang
necessary": LLVM has a linker called LLD
that is part of the LLVM project. Depending on how you installed LLVM it should be part of the distro.
Refer to your installed version for LLD as well as usage strategies. You will be able to then define your make
or cmake
recipes.
With reference to your core question, here is the general make flow I go through with my own language:
- Compile source -> output.ll (LLVM assembly)
- Optimize assembly -> output.oll (using
opt
) - Generate target assembly -> output.s
- Assemble to object (using
as
) -> output.o - Link (I am using
clang
but this could be swapped withlld
)
-
Thanks a lot for the answer, Frank! Is your language on github, so I could take a look at the source? I tried looking at clang, but even finding the main function was a challenge ;-) – gruszczy Jan 03 '18 at 16:11
-
Also - optimize assembly, is that running the optimization passes, as expained here (https://llvm.org/docs/tutorial/LangImpl04.html#llvm-optimization-passes). And generate target assembly - Is that the object code generation (https://llvm.org/docs/tutorial/LangImpl08.html)? And after that I would just invoke run lld from my compiler process or is there some linking API that I could just use that would be an equivalent? – gruszczy Jan 03 '18 at 16:13
-
It would be the equivalent (re; opt). I do a separate to compare what is the result of the optimization compared to what i emitted (I do not use the C++ LLVM API). Also, I forgot the step "3.5" which assembles the `output.s` file before linking. The `lld` link step, for me, is done outside of an API call. Updated answer to include the forgotten step. – Frank C. Jan 03 '18 at 17:34
-
My language is not ready for github public and the compiler is self hosted so you'd need to understand it's syntax or have a general appreciation for functional programming languages. – Frank C. Jan 03 '18 at 17:36
-
Thanks a lot, Frank! I'm curious, this is not really a question for stack overflow: what do you think is a state for a language to be ready for github, since you obviously have some criteria? – gruszczy Jan 03 '18 at 23:28
-
Ha! I have started projects right from the git-go (no pun intended) in public repositories. For this compiler adventure it is a matter of it is just being consumable. I still need to implement a garbage collector, shared library support and there are many "stub" spots in the run-time library support. I guess it is just a subjective and sophomoric vanity line. – Frank C. Jan 03 '18 at 23:39
-
Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/162466/discussion-between-frank-c-and-gruszczy). – Frank C. Jan 03 '18 at 23:49
-
@FrankC. Is it possible to use `lld` as a library linked to compiler binary instead of spawning a process to do linkage? – Yury Solovyov Aug 14 '19 at 12:07
-
1@YurySolovyov I think it is possible, there is a CMakeLists.txt in the lld linker, so you are gonna have to link that with your code, and include it's headers. But the documentation on how to use it is non-existent or very minimal. I think this function is quite important though: `elf::link(args, exitEarly, stdoutOS, stderrOS)` – lxr196 Feb 17 '21 at 01:19