[HN Gopher] Implementing a LLVM Micro C compiler in Haskell
       ___________________________________________________________________
        
       Implementing a LLVM Micro C compiler in Haskell
        
       Author : pcr910303
       Score  : 108 points
       Date   : 2021-06-04 15:50 UTC (7 hours ago)
        
 (HTM) web link (blog.josephmorag.com)
 (TXT) w3m dump (blog.josephmorag.com)
        
       | kubb wrote:
       | This reminds me of a project that I did in my university. It was
       | a compiler of my programming language similar to C straight to
       | x86 assembly. It had typechecking, basic primitive types, local
       | and global variables, arrays and function calls. My professor
       | recommended Haskell saying that it'll make the job easier for me,
       | and he was right. The thing basically worked as soon as I got it
       | to compile. The code is still on my github. I recently took a
       | look at it and I was surprised how readable it was.
       | 
       | A shame that in my professional career I didn't get the chance to
       | use neither Haskell nor any of the PL skills that I picked up in
       | uni, because I really had fun with that project. Though maybe
       | that's for the best. If you have to do it for work, you sometimes
       | end up hating it.
        
       | winter_blue wrote:
       | This is awesome. This essentially will serve as a more up-to-date
       | tutorial on the LLVM bindings for Haskell. So programming
       | language designers could use this tutorial to build a new
       | language using Haskell. Currently, the state of LLVM
       | documentations, and its tutorial leaves much wanting. This work
       | remedies things on the Haskell side of things by quite a bit.
       | 
       | I especially love the fact that instead of implementing a novel
       | language like Kaleidoscope (which the official LLVM does), it
       | implements a subset of C (a language that's low-level enough and
       | widely understood). C has all of the basic constructs, which if
       | you understand how to write a compiler (to LLVM) for, would allow
       | you to implement far more advanced languages.
       | 
       | Thank you Joseph Morag for this wok, and Theophile Choutri,
       | Moritz Kiefer, et al for making it possible.
        
         | pjmlp wrote:
         | Kaleidoscope seems to only focus on C++ nowadays, the OCaml
         | version is no longer there.
         | 
         | https://llvm.org/docs/tutorial/
        
       | andi999 wrote:
       | Why not go for the full C language. C was made to make compilers
       | easy to build. Also it might expose/help to see difficulties in
       | the chosen approach.
        
         | pjmlp wrote:
         | Unless you are talking about something like "A Retargetable C
         | Compiler: Design and Implementation", it is definitly not easy.
         | 
         | https://www.amazon.com/Retargetable-Compiler-Design-Implemen...
         | 
         | During the early 80's, the best home computers could get was
         | Small-C.
         | 
         | https://en.wikipedia.org/wiki/Small-C
        
         | [deleted]
        
         | ash_gti wrote:
         | They went with a subset of C, its enough to write a working
         | executable. Generally for a introduction tutorial to something
         | as complex as writing a compiler a subset like what they
         | presented in the article is enough to show how all the pieces
         | work together to produce a working compiler.
        
           | mikepurvis wrote:
           | Yup, for the purposes of a demo like this, a simplified top-
           | to-bottom "vertical slice" is of far more value than total
           | feature coverage.
        
         | [deleted]
        
         | reikonomusha wrote:
         | A conforming ISO C compiler seems extremely difficulty to build
         | and I don't think ease of writing a compiler is a part of the
         | design these days.
        
           | andi999 wrote:
           | They could stick to ANSI C.
        
             | woodruffw wrote:
             | ANSI C and ISO C are the same thing, at least for the
             | 1989/90 revision. It's still not a particularly easy
             | language to implement, at any stage.
        
       | helltone wrote:
       | This looks great, are there any similar tutorials written for
       | languages other than Haskell?
        
       ___________________________________________________________________
       (page generated 2021-06-04 23:01 UTC)