Home

Awesome

language-cil: Manipulating Common Intermediate Language AST

Language-Cil is a Haskell library for manipulating CIL abstract syntax and generating .il files using the concrete syntax of CIL.

Common Intermediate Language (CIL), formerly known as Microsoft Intermediate Language (MSIL), is the lowest level language that runs on the Microsoft .NET and Mono platforms.

The examples directory has a couple of files showing how to construct different CIL ASTs.

Read more on the Wikipedia page on Common Intermediate Language.

Current status

This library is still very much under development. Only a subset of the full CIL AST is implemented.

A parser for concrete syntax has not yet been implemented.

Module overview

Getting started

  1. Make sure you have Mono or .NET installed.

  2. Install the library.

  3. Create an AST, eg: module Example where

    import Language.Cil
    
    main :: IO ()
    main = writeAssembly "Test.il" ass
    
    ass :: Assembly
    ass = simpleAssembly
      [ nop
         -- Print message to stdout
      , ldstr "Enter an integer and press return."
      , call [] Void "mscorlib" "System.Console" "WriteLine" [String]
      
        -- Reads an integer from stdin, add one to it, print result to stdout.
      , call [] String "mscorlib" "System.Console" "ReadLine" []
      , call [] Int32 "" "int32" "Parse" [String]
      
      , ldc_i4 1
      , add
      , call [] Void "mscorlib" "System.Console" "WriteLine" [Int32]
      
      , ret
      ]
    
  4. Write the AST to a file by running main.

  5. Run the IL assembler: ilasm Test.il

  6. Run the generated executable: mono Test.exe (or Test.exe on Windows)