Skip to content
LLVM bindings for .NET Standard written in C# using ClangSharp
C# Other
  1. C# 98.3%
  2. Other 1.7%
Branch: master
Clone or download

Files

Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
Tests/LLVMSharp.UnitTests Add a proper managed wrapper for LLVMSharp (#137) Apr 18, 2020
packages/libLLVM Update to LLVM 10.0.0 (#131) Apr 14, 2020
samples Update to LLVM 10.0.0 (#131) Apr 14, 2020
scripts Update to LLVM 10.0.0 (#131) Apr 14, 2020
sources String version of BuildMul(...) calls ReadOnlySpan<char> overload now ( Apr 24, 2020
tests Update to LLVM 10.0.0 (#131) Apr 14, 2020
.editorconfig Setup some infrastructure for the repository. (#100) Jun 1, 2019
.gitattributes Setup some infrastructure for the repository. (#100) Jun 1, 2019
.gitignore Fixing up some infrastructure so we can consume the libLLVM 8.0.0 pac… Jun 8, 2019
Directory.Build.props Update to LLVM 10.0.0 (#131) Apr 14, 2020
Directory.Build.targets Update to LLVM 10.0.0 (#131) Apr 14, 2020
LICENSE.txt Setup some infrastructure for the repository. (#100) Jun 1, 2019
LLVMSharp.sln Update to LLVM 10.0.0 (#131) Apr 14, 2020
NOTICE.txt Updating dependencies to stable versions and adding a NOTICE.TXT (#119) Nov 23, 2019
README.md Fixing build status links and moving version suffix to beta. (#110) Aug 10, 2019
build.cmd Setup some infrastructure for the repository. (#100) Jun 1, 2019
build.sh Setup some infrastructure for the repository. (#100) Jun 1, 2019
pack.cmd Setup some infrastructure for the repository. (#100) Jun 1, 2019
pack.sh Setup some infrastructure for the repository. (#100) Jun 1, 2019
restore.cmd Setup some infrastructure for the repository. (#100) Jun 1, 2019
restore.sh Setup some infrastructure for the repository. (#100) Jun 1, 2019
test.cmd Setup some infrastructure for the repository. (#100) Jun 1, 2019
test.sh Setup some infrastructure for the repository. (#100) Jun 1, 2019

README.md

LLVMSharp

LLVMSharp is a multi-platform .NET Standard library for accessing the LLVM infrastructure. The bindings are auto-generated using ClangSharp parsing LLVM-C header files.

Job Debug Status Release Status
Windows x86 Build Status Build Status
Windows x64 Build Status Build Status
Ubuntu 16.04 x64 Build Status Build Status
MacOS x64 Build Status Build Status

Join the chat at https://gitter.im/mjsabby/LLVMSharp

LLVMSharp 5.0 NuGet Package for .NET Core 2.0+ (Linux, macOS, Windows) and .NET Framework 4+ - built from the LLVM 5.0.0 Release.

Building LLVMSharp

On Linux using .NET Core:

 $ git clone http://github.com/Microsoft/LLVMSharp
 $ cd LLVMSharp/src
 $ dotnet build

On Windows using .NET Core

Note: - you need to run these commands from the Visual Studio Developer Command Prompt.

 :> git clone http://github.com/mjsabby/LLVMSharp
 :> cd LLVMSharp\src
 :> dotnet build

Features

  • Auto-generated using LLVM C headers files, and supports all functionality exposed by them (more than enough to build a full compiler)
  • Type safe (LLVMValueRef and LLVMTypeRef are different types, despite being pointers internally)
  • Nearly identical to LLVM C APIs, e.g. LLVMModuleCreateWithName in C, vs. LLVM.ModuleCreateWithName (notice the . in the C# API)

Kaleidoscope Tutorials

There's a C# translation of the LLVM official Kaleidoscope Tutorial.

Much of the tutorial is already implemented here, and has some nice improvements like the Visitor pattern for code generation to make the LLVM code stand out and help you bootstrap your compiler.

The tutorials have been tested to run on Windows and Linux, however the build (using MSBuild) uses the Nuget packages, hence require some editing to run on Linux.

Chapter 3

Chapter 4

Chapter 5

Conventions

  • Types are exactly how they are defined in the C bindings, for example: LLVMTypeRef

  • Functions are put in a C# class called LLVM and the LLVM prefix is removed from the functions, for example: LLVM.ModuleCreateWithName("LLVMSharpIntro");

Example application

    using System;
    using System.Runtime.InteropServices;
    using LLVMSharp;

    internal sealed class Program
    {
        [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
        public delegate int Add(int a, int b);

        private static void Main(string[] args)
        {
            LLVMBool Success = new LLVMBool(0);
            LLVMModuleRef mod = LLVM.ModuleCreateWithName("LLVMSharpIntro");

            LLVMTypeRef[] param_types = { LLVM.Int32Type(), LLVM.Int32Type() };
            LLVMTypeRef ret_type = LLVM.FunctionType(LLVM.Int32Type(), param_types, false);
            LLVMValueRef sum = LLVM.AddFunction(mod, "sum", ret_type);

            LLVMBasicBlockRef entry = LLVM.AppendBasicBlock(sum, "entry");

            LLVMBuilderRef builder = LLVM.CreateBuilder();
            LLVM.PositionBuilderAtEnd(builder, entry);
            LLVMValueRef tmp = LLVM.BuildAdd(builder, LLVM.GetParam(sum, 0), LLVM.GetParam(sum, 1), "tmp");
            LLVM.BuildRet(builder, tmp);

            if (LLVM.VerifyModule(mod, LLVMVerifierFailureAction.LLVMPrintMessageAction, out var error) != Success)
            {
                Console.WriteLine($"Error: {error}");
            }

            LLVM.LinkInMCJIT();

            LLVM.InitializeX86TargetMC();
            LLVM.InitializeX86Target();
            LLVM.InitializeX86TargetInfo();
            LLVM.InitializeX86AsmParser();
            LLVM.InitializeX86AsmPrinter();

            LLVMMCJITCompilerOptions options = new LLVMMCJITCompilerOptions { NoFramePointerElim = 1 };
            LLVM.InitializeMCJITCompilerOptions(options);
            if (LLVM.CreateMCJITCompilerForModule(out var engine, mod, options, out error) != Success)
            {
                Console.WriteLine($"Error: {error}");
            }

            var addMethod = (Add)Marshal.GetDelegateForFunctionPointer(LLVM.GetPointerToGlobal(engine, sum), typeof(Add));
            int result = addMethod(10, 10);

            Console.WriteLine("Result of sum is: " + result);

            if (LLVM.WriteBitcodeToFile(mod, "sum.bc") != 0)
            {
                Console.WriteLine("error writing bitcode to file, skipping");
            }

            LLVM.DumpModule(mod);

            LLVM.DisposeBuilder(builder);
            LLVM.DisposeExecutionEngine(engine);
        }
    }

Microsoft Open Source Code of Conduct

This project has adopted the Microsoft Open Source Code of Conduct. For more information see the Code of Conduct FAQ or contact opencode@microsoft.com with any additional questions or comments.

You can’t perform that action at this time.