[HN Gopher] CIL: C Intermediate Language
       ___________________________________________________________________
        
       CIL: C Intermediate Language
        
       Author : tosh
       Score  : 74 points
       Date   : 2024-10-28 09:57 UTC (4 days ago)
        
 (HTM) web link (github.com)
 (TXT) w3m dump (github.com)
        
       | saagarjha wrote:
       | Is anyone using this? Perhaps for transpiring to a more limited
       | compiler?
        
         | nikki93 wrote:
         | Frama-C uses CIL.
        
           | KsassPeuk wrote:
           | Well, Frama-C uses a quite modified version of CIL.
        
         | mingodad wrote:
         | See also https://melt.cs.umn.edu/ableC/ that has a similar
         | goal.
        
         | Tiberium wrote:
         | One thing that CIL allows is creating amalgamations -
         | converting the whole program into a single C file that can be
         | then compiled by GCC or other compilers.
        
       | HexDecOctBin wrote:
       | This reminds me, is there any C compilers that allows injecting
       | custom parsing rules? Say, if it is parsing or type checking and
       | hits upon an error, it calls a callback instead of bailing. This
       | would be very useful for adding simple extensions to the
       | language. I tried to make LLVM do this but couldn't figure it
       | out.
        
         | tester756 wrote:
         | It seems like you're interested in frontend - Clang, not
         | backend - LLVM.
        
         | Gibbon1 wrote:
         | I saw someones tool that allows you to embed python code to
         | generate C code.
         | 
         | https://nedbatchelder.com/code/cog/
         | 
         | Never used it.
        
         | fuhsnn wrote:
         | If you are ok with unoptimized codegen, chibicc[0] is very easy
         | to modify. For an example checkout the patch set for adding
         | defer in my fork[1].
         | 
         | [0] https://github.com/rui314/chibicc [1]
         | https://github.com/fuhsnn/slimcc-defer/commits
        
       | neonsunset wrote:
       | A bit unfortunate choice of the name since there is another CIL
       | too: https://ecma-international.org/publications-and-
       | standards/st...
        
         | someothherguyy wrote:
         | That is CLI (Common Language Infrastructure)?
         | 
         | https://en.wikipedia.org/wiki/Common_Language_Infrastructure
        
           | tubs wrote:
           | Partition III: CIL Instruction Set - Describes the Common
           | Intermediate Language (CIL) instruction set.
        
         | almost wrote:
         | There are a finite number of names, and an even smaller number
         | of good names. But apparently an infinite number of hacker news
         | posters who comment on every new project to complain that the
         | name has been used before for something else.
         | 
         | And even the fact that this is a fork of an earlier project and
         | the name comes from that doesn't stop it!
         | 
         | (I'm aware I have chosen a very weird thing to be getting
         | annoyed at over my breakfast crumpets this morning)
        
           | Philpax wrote:
           | I would normally agree with you on this, but these projects
           | are in the same realm (language compiler infrastructure), so
           | it's a bit more unfortunate than in the usual case
        
           | fuhsnn wrote:
           | People are more triggered by "this is improper in my culture"
           | type of comments, those tend to actually derail the thread.
        
         | PeterWhittaker wrote:
         | Not to mention SELinux CIL, Common Intermediate Language,
         | https://selinuxproject.org/page/PolicyLanguage
        
         | pkhuong wrote:
         | https://github.com/goblint/cil/blob/develop/LICENSE Copyright
         | (c) 2001-2020
         | 
         | (I first learned about CIL in
         | https://people.eecs.berkeley.edu/~necula/Papers/cil_cc02.pdf)
         | 
         | I guess ECMA should have done better research.
        
           | fluoridation wrote:
           | https://en.wikipedia.org/wiki/Common_Language_Infrastructure
           | 
           | 2012 is just the latest version. It was first published in
           | 2001, and started in 2000. They're so close together that
           | it's difficult to say if one would have been able to find the
           | other.
        
       | riperoni wrote:
       | Can someone explain to a C-noob how this differs from and
       | compares to LLVM?
        
         | Philpax wrote:
         | LLVM is a backend: it takes LLVM IR (intermediate
         | representation) and generates machine code.
         | 
         | This is a frontend: it takes C and generates its own IR (a
         | simplified version of C).
         | 
         | You could glue these together with an adapter from CIL to LLVM
         | IR to get a complete C compiler.
         | 
         | Clang is both a frontend and a complete compiler in this
         | respect: the Clang frontend compiles C to LLVM IR, and these
         | are bundled together to produce the Clang compiler.
         | 
         | (Note: I'm simplifying things here. Clang and LLVM are more
         | intertwined than these, and there are several nuances I'm not
         | covering; I'm going for a high-level perspective here)
        
           | riperoni wrote:
           | High-level is good enough for me for the beginning, thanks!
        
         | pizlonator wrote:
         | Great question.
         | 
         | First of all, llvm has clang, which means that llvm as a whole
         | is equipped to understand C (and C++ and Objective-C) both at a
         | high level (abstract syntax tree, all types as declared by the
         | programmer) and low level (SSA form, only the types that are
         | meaningful for sound analysis and optimization).
         | 
         | I think that CIL was a really big deal before llvm and clang.
         | Back then, it was a more approachable alternative to trying to
         | fiddle with C than using GCC, since GCC has a steep learning
         | curve. But in the last 15 years or so, most of the research
         | that would have been done in CIL before is now done in llvm.
         | That's because llvm is much more complete _and_ it's designed
         | for ergonomics, specifically in the case where you just want to
         | mess around and even if you're a newcomer to the compiler. The
         | docs are great and the APIs are top notch.
         | 
         | I think that LLVM's SSA form is especially good for doing
         | sophisticated analysis and instrumentation of C. I've used that
         | a lot for my C experiments. Clang's AST is really great, too -
         | and it's amazing for doing higher level stuff where you want to
         | see the original C types and declarations before lowering.
         | 
         | I suspect that there is very little that CIL can do for you
         | that can't be done in llvm more straightforwardly. And
         | llvm+clang support all of C, plus the adjacent languages (C++
         | and others).
         | 
         | So, it's cool that CIL is still around (having alternatives is
         | good, generally) but in my opinion as someone who does
         | experimental work in C compilers, C language extensions, and
         | static/dynamic analysis of C, llvm completely subsumes CIL.
        
       | WalterBright wrote:
       | I considered developing such a language, but could not justify
       | it. The intermediate code I developed works just fine for this,
       | and would be trivial to convert to/from a text based form, but I
       | couldn't see the point.
        
       | ranger_danger wrote:
       | I just want a way to add simple inheritable classes to C without
       | bringing in the entirety of C++ and all its differences and
       | baggage. But I guess things like constructors/destructors are not
       | possible to implement in a language standard-compliant way (not
       | relying on compiler extensions).
        
         | mingodad wrote:
         | Have a look at http://ecere.org/ they already have it working.
        
       ___________________________________________________________________
       (page generated 2024-11-01 23:01 UTC)