I am absolutely fascinated with Crystal's use of LLVM. I have been studying /src/compiler/ and piecing together what I can on how Crystal targets LLVM. Between the Brainf*ck compiler example and the codegen sections in the Crystal compiler, I have learned a lot that I was simply struggling to grok from the LLVM Kaleidoscope tutorial. Now I am using Crystal to build my own toy front-end compiler on LLVM to better understand the ecosystem. I must say, the experience is making me appreciate all the hard work that was put into Crystal.
As someone with only basic C++ skills I find I miss a lot of information in the LLVM documentation. Aside from the LLVM programmers manual / source code did you have any helpful references when you were learning the API? The Brainf*ck example in particular was very helpful for me to see how to use the builder API but I still feel unsure of how the LLVM module system works, and how it handles scope in the grand scheme of program execution.
--
You received this message because you are subscribed to the Google Groups "Crystal" group.
To unsubscribe from this group and stop receiving emails from it, send an email to crystal-lang+unsubscribe@googlegroups.com.
To post to this group, send email to crysta...@googlegroups.com.
Visit this group at https://groups.google.com/group/crystal-lang.
To view this discussion on the web visit https://groups.google.com/d/msgid/crystal-lang/ca8974eb-7ca6-41c0-9ce0-d35ad0708529%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Thank you for the kind words! :-)Yes, LLVM can be hard to get at first. The main doc we followed was, as you mentioned, the language reference. Since we needed to bind to the C API, that was useful too (there are other files too, that's the main one).As for LLVM modules, they are independent unit of compilations. They can use functions or globals defined in other modules if you declare them as an external declaration. In Crystal we create one module per type, so all methods of, say, String, will be generated in a String module. If another module uses methods from String they will declare them as external functions. At link time everything will be found. There isn't any other scope than that: local "variables" inside a function (which are always immutable), then functions which can be interla/private to the module, public, or defined externally, and the same applies to globals.Another great source to check if you are generating good/optimal code is to take a piece of C /C++ code and run it through clang and generate llvm-ir:$ clang file.c -S -emit-llvm # generates file.llSince clang is the main project that uses LLVM, if you generate code that more or less looks like what clang generates then you are probably doing things right :-)Back to LLVM, we also checked the performance tips. Of those, the most important is to generate all alloca instructions in the entry block. Well, at least that's important if you want to generate efficient code.
On Tue, Dec 6, 2016 at 2:38 PM, <virtualma...@gmail.com> wrote:
I am absolutely fascinated with Crystal's use of LLVM. I have been studying /src/compiler/ and piecing together what I can on how Crystal targets LLVM. Between the Brainf*ck compiler example and the codegen sections in the Crystal compiler, I have learned a lot that I was simply struggling to grok from the LLVM Kaleidoscope tutorial. Now I am using Crystal to build my own toy front-end compiler on LLVM to better understand the ecosystem. I must say, the experience is making me appreciate all the hard work that was put into Crystal.
As someone with only basic C++ skills I find I miss a lot of information in the LLVM documentation. Aside from the LLVM programmers manual / source code did you have any helpful references when you were learning the API? The Brainf*ck example in particular was very helpful for me to see how to use the builder API but I still feel unsure of how the LLVM module system works, and how it handles scope in the grand scheme of program execution.
--
You received this message because you are subscribed to the Google Groups "Crystal" group.
To unsubscribe from this group and stop receiving emails from it, send an email to crystal-lang...@googlegroups.com.
To post to this group, send email to crysta...@googlegroups.com.
Visit this group at https://groups.google.com/group/crystal-lang.
To view this discussion on the web visit https://groups.google.com/d/msgid/crystal-lang/ca8974eb-7ca6-41c0-9ce0-d35ad0708529%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.