All Projects → microsoft → Llvmsharp

microsoft / Llvmsharp

Licence: other
LLVM bindings for .NET Standard written in C# using ClangSharp

Labels

Projects that are alternatives of or similar to Llvmsharp

Sericum
(Toy) Compiler Infrastructure influenced by LLVM written in Rust
Stars: ✭ 366 (-33.82%)
Mutual labels:  llvm
Enzyme
High-performance automatic differentiation of LLVM.
Stars: ✭ 418 (-24.41%)
Mutual labels:  llvm
Phasar
A LLVM-based static analysis framework.
Stars: ✭ 503 (-9.04%)
Mutual labels:  llvm
Nlvm
LLVM-based compiler for the Nim language
Stars: ✭ 380 (-31.28%)
Mutual labels:  llvm
Tagua Vm
Tagua VM is an experimental PHP Virtual Machine that guarantees safety and quality by removing large classes of vulnerabilities thanks to the Rust language and the LLVM Compiler Infrastructure.
Stars: ✭ 419 (-24.23%)
Mutual labels:  llvm
Staticscript
🚀 TypeScript compiler on top of TypeScript as frontend and LLVM as backend
Stars: ✭ 447 (-19.17%)
Mutual labels:  llvm
Easy Just In Time
LLVM Optimization to extract a function, embedded in its intermediate representation in the binary, and execute it using the LLVM Just-In-Time compiler.
Stars: ✭ 361 (-34.72%)
Mutual labels:  llvm
Svf
Static Value-Flow Analysis Framework for Source Code
Stars: ✭ 540 (-2.35%)
Mutual labels:  llvm
Llvm
Intel staging area for llvm.org contribution. Home for Intel LLVM-based projects.
Stars: ✭ 424 (-23.33%)
Mutual labels:  llvm
Langcraft
Compiler from LLVM IR to Minecraft datapacks.
Stars: ✭ 495 (-10.49%)
Mutual labels:  llvm
Rapidus
A toy JavaScript engine implemented in Rust from scratch
Stars: ✭ 390 (-29.48%)
Mutual labels:  llvm
Scala Native
Your favorite language gets closer to bare metal.
Stars: ✭ 4,053 (+632.91%)
Mutual labels:  llvm
Llvm
Project moved to: https://github.com/llvm/llvm-project
Stars: ✭ 4,461 (+706.69%)
Mutual labels:  llvm
Llvm Hs
Haskell bindings for LLVM
Stars: ✭ 370 (-33.09%)
Mutual labels:  llvm
Multi Os Engine
Multi-OS Engine: Create iOS Apps in Java (or Kotlin ... etc.)
Stars: ✭ 529 (-4.34%)
Mutual labels:  llvm
Codegen
Experimental wrapper over LLVM for generating and compiling code at run-time.
Stars: ✭ 362 (-34.54%)
Mutual labels:  llvm
Simit
A language for computing on sparse systems
Stars: ✭ 439 (-20.61%)
Mutual labels:  llvm
Tigress protection
Playing with the Tigress binary protection. Break some of its protections and solve some of its challenges. Automatic deobfuscation using symbolic execution, taint analysis and LLVM.
Stars: ✭ 550 (-0.54%)
Mutual labels:  llvm
Mull
Practical mutation testing tool for C and C++
Stars: ✭ 536 (-3.07%)
Mutual labels:  llvm
Circt
Circuit IR Compilers and Tools
Stars: ✭ 491 (-11.21%)
Mutual labels:  llvm

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 NuGet Package for .NET Core 2.0+ (Linux, macOS, Windows) and .NET Framework 4+ - each version is built from the corresponding LLVM Release.

Building LLVMSharp

On Linux using .NET Core:

 $ git clone http://github.com/Microsoft/LLVMSharp
 $ cd LLVMSharp
 $ 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
 :> 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 [email protected] with any additional questions or comments.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].