afrog33k/SharpNative

语言: C#

git: https://github.com/afrog33k/SharpNative

C#到本机代码(D,C ++等...)基于Microsoft Roslyn的Transpiler
A C# to Native Code (D, C++ etc...) Transpiler based on Microsoft Roslyn
README.md (中文)

什么是SharpNative?

Join the chat at https://gitter.im/afrog33k/SharpNative

SharpNative是一个从C#生成本机代码(D,Soon C ++ 11,Java和Swift)的工具,利用Microsoft Roslyn生成几乎手写的目标语言代码。

我们的想法是最大化C#的跨平台功能,而不受任何供应商或平台的束缚。这里的重点是生成的D代码的性能和可读性。评论也会保留。


支持的输出语言

处于当前状态的编译器仅支持D作为输出语言。 (这是由于DMD是一个非常快速的编译器,所以测试功能很有趣)


表现 - 结果非常不科学

以下是从CrossNet(第一个C#到Native编译工作之一)的测试

机:

- Macbook Pro Retina(2012年中)

- 2.6Ghz Intel Core i7

- 16GB 1600 MHz DDR3

我的Parallels Windows 8 VM上的一些基准测试:(3GB Ram,3核心)使用带有选项的DMD -inline -release -m64 -O 和.Net在发布模式下

Type Of Test C# Time (ms) D Time (ms) Speed Ratio (C#/D)
NSieveTest 18859 5450 3.46x
MatrixTest(MultiDimensional) 12359 22606 0.56x
MatrixTest(Jagged) 10156 2580 3.98x
GCTest 10657 57288 0.19x
UnsafeTest 32375 4752 6.81x
HeapSortTest 8671 3906 2.21x
BinaryTreesTest 12359 22635 0.55x
CastsTest (using auto) 6282 1784 3.52x
CastsTest (using object) 125 3211 0.04x
EventsTest 5985 6121 0.98x
FannkuchTest 1218 529 2.3x
ForeachOnArrayTest 34359 29358 1.17x
InterfaceTest 8297 4044 2.05x
ListTest 22422 5761 3.89x
NestedLoopsTest 33593 6466 5.19x
PartialSumsTest 4531 5518 0.82x
RecursiveTest 11406 3014 3.78x
StringConcatenationTest 63 147 0.43x
VirtualTest 8078 2784 2.9x
Average 2.34x

由于生成的二进制文件是DMD中的原生二进制文件和更好的优化,因此生成的二进制文件通常比它们的C#对应文件快得多。除了涉及垃圾收集之外,D GC比.Net慢得多(也许我们可以将它移植到D)。此外,当前的多维数组实现似乎缺乏性能。


生成代码的示例

C#:

```C ++ 使用系统;

班级Primes {     public static void Main()     {         var len = 1000000; //这是评论         var primes = AddPrimes(len);         Console.Write(素数);     }

private static int AddPrimes(int len)
{
    var primes = 0;
    for (var i = 2; i < len; i++)
    {
        if (i%2 == 0)
            continue;
        var isPrime = true;
        for (var j = 2; j*j <= i; j++)
        {
            if (i%j == 0)
            {
                isPrime = false;
                break;
            }
        }
        if (isPrime)
            primes++;
    }
    return primes;
}

}

D:

```d
module CsRoot.Primes;
import System.Namespace;
import CsRoot.Namespace;

class Primes : NObject
{

    public static void Main()
    {
      int len = 1000000;
      // This is a comment
      int primes = AddPrimes(len);
      Console.Write(primes);
    }

    final static int AddPrimes(int len)
    {
      int primes = 0;

      for (int i = 2;i<len;i++)
      {
        if(i%2==0)
        {
          continue;
        }
        bool isPrime = true;

        for (int j = 2;j*j<=i;j++)
        {
          if(i%j==0)
          {
            isPrime=false;
            break;
          }
        }
        if(isPrime)
        {
          primes++;
        }
      }
      return primes;
    }

  public override String ToString()
  {
    return GetType().FullName;
  }

  public override Type GetType()
  {
    return __TypeOf!(typeof(this));
  }
}

文档

不幸的是,这是转换器目前的所有文档。


功能列表(不完整): - 什么有效: - 包括编组在内的基本PInvoke - 包括初始化器的数组 - 具有正确隐藏语义的字段/属性/方法 - 属性 - 字符串 - Int / Double / Bool - 类和多态...我们遵循C#模型 - 一些基准 - 基本的linpack,fannkuch,nbody - 模块/命名空间 - Enum - 没有enum.Parse支持但是 - 迭代器是.Net使用枚举器等...切换数组使用简单的for循环来提高性能 - 构造函数/重载/基类调用 - 静态变量/成员/属性 - Basic System.Math,但需要更多实现 - 扩展方法 - 运算符重载 - 索引器 - 匿名类 - 泛型......所有当前的测试用例都有效 - 盒装结构和界面铸造 - OuterClass_InnerClass形式的内部类 - 静态构造函数 - 显式接口 - 隐式和显式转换运算符 - 字符串切换... dlang原生支持:) - String.Format ..虽然实现是非常基本的 - C#多维数组工作正常(即使使用多暗的语法:)) - 代表工作包括多播(通过P / Invoke工作的本地代表) - 事件按预期工作......虽然比C#(单声道)慢一点 - 对象初始化器用作lambda表达式链 - 通用虚拟方法 - 方法,字段和属性的基本反映,目前尚不支持IndexerProperties和通用方法...... - 使用WootzJS代码的迭代器和产量,光纤实现存在,但它在DMD 2.066 Windows 64位上崩溃


要求 - 测试

- Microsoft .Net 4.0 / Mono 3.6及更高版本。

- Windows 7或更高版本(CLI界面适用于Linux和OSX)

- 工作D安装(LDC,DMD或GDC)

要求 - 发展

上述所有要求和:

- Visual Studio 2013或更高版本

- 视觉D.

用法

如果您使用的是GUI界面(windows),请注意DMD应安装在“C:\\ D \\ dmd2 \\ windows \\ bin \\”中 对于CLI界面,可以通过以下方式调用驱动程序:

mono ./SharpNative.exe /compiler:pathtodcompiler /dcorlib:/**pathtodcorlib** /source:"pathtosourcefile" /outputtype:exe /dstdlib:pathtophobos /compileroptions:"compileroptions"

哪里:

- pathtodcompiler是d编译器的路径,例如mac osx上的/ usr / local / bin / ldc

- pathtodcorlib是包含的基本corlib的路径,例如/项目/ SharpNative / DCorl​​ib

- pathtosourcefile是C#中测试源文件的路径

--pathtophobos是安装中的phobos的位置,例如/usr/local/Cellar/ldc/0.15.0/include/d

--compileroptions是传递给dmd / ldc / gdc的编译器选项,例如-inline -release -m64 -O5 -oq

什么行不通:(也是不完整的) - 现在编译器不做任何优化,如final,in,out @pure等...... - 异步/等待 - 处理实现 - 查询LINQ和表达式树的表达式语法 - 结构FieldLayout实现目前是错误的...模仿C#需要新的设计。

本文使用googletrans自动翻译,仅供参考, 原文来自github.com

en_README.md

What is SharpNative ?

Join the chat at https://gitter.im/afrog33k/SharpNative

SharpNative is a tool that generates Native Code (D, Soon C++11, Java and Swift) from C# leveraging Microsoft Roslyn to generate almost hand-written code the the target languages.

The idea is to maximize the cross-platform capabilities of C# without being tied to any vendor or platform. The Emphasis here is on Performance and Readability of the generated D Code. Comments are preserved as well.


Supported Output Languages

The Compiler in its current state only supports D as the output language. (This is due to DMD being an extremely fast compiler, so testing features is fun)


Performance -- Results are very unscientific

The following are tests taken from CrossNet (one of the first C# to Native compiler efforts)

Machine:

-- Macbook Pro Retina (Mid 2012)

-- 2.6Ghz Intel Core i7

-- 16GB 1600 MHz DDR3

Some benchmarks on my Parallels Windows 8 VM: (3GB Ram, 3 Cores) using DMD with options -inline -release -m64 -O
and .Net in release mode

Type Of Test C# Time (ms) D Time (ms) Speed Ratio (C#/D)
NSieveTest 18859 5450 3.46x
MatrixTest(MultiDimensional) 12359 22606 0.56x
MatrixTest(Jagged) 10156 2580 3.98x
GCTest 10657 57288 0.19x
UnsafeTest 32375 4752 6.81x
HeapSortTest 8671 3906 2.21x
BinaryTreesTest 12359 22635 0.55x
CastsTest (using auto) 6282 1784 3.52x
CastsTest (using object) 125 3211 0.04x
EventsTest 5985 6121 0.98x
FannkuchTest 1218 529 2.3x
ForeachOnArrayTest 34359 29358 1.17x
InterfaceTest 8297 4044 2.05x
ListTest 22422 5761 3.89x
NestedLoopsTest 33593 6466 5.19x
PartialSumsTest 4531 5518 0.82x
RecursiveTest 11406 3014 3.78x
StringConcatenationTest 63 147 0.43x
VirtualTest 8078 2784 2.9x
Average 2.34x

Due to the produced binaries being native and better optimizations in the DMD, the generated binaries are generally much faster than their C# counterparts. Except when Garbage Collection is concerned, the D GC is much slower than that of .Net (Maybe we can port it to D). Also the current multidimensional array implementation seems lacking in performance.


Example of Generated Code

C#:

```c++
using System;

class Primes
{
public static void Main()
{
var len = 1000000; // This is a comment
var primes = AddPrimes(len);
Console.Write(primes);
}

private static int AddPrimes(int len)
{
    var primes = 0;
    for (var i = 2; i < len; i++)
    {
        if (i%2 == 0)
            continue;
        var isPrime = true;
        for (var j = 2; j*j <= i; j++)
        {
            if (i%j == 0)
            {
                isPrime = false;
                break;
            }
        }
        if (isPrime)
            primes++;
    }
    return primes;
}

}

D:

```d
module CsRoot.Primes;
import System.Namespace;
import CsRoot.Namespace;

class Primes : NObject
{

    public static void Main()
    {
      int len = 1000000;
      // This is a comment
      int primes = AddPrimes(len);
      Console.Write(primes);
    }

    final static int AddPrimes(int len)
    {
      int primes = 0;

      for (int i = 2;i<len;i++)
      {
        if(i%2==0)
        {
          continue;
        }
        bool isPrime = true;

        for (int j = 2;j*j<=i;j++)
        {
          if(i%j==0)
          {
            isPrime=false;
            break;
          }
        }
        if(isPrime)
        {
          primes++;
        }
      }
      return primes;
    }

  public override String ToString()
  {
    return GetType().FullName;
  }

  public override Type GetType()
  {
    return __TypeOf!(typeof(this));
  }
}

Documentation

Unfortunately this is all the documentation the transpiler has at the moment.


Feature List (Incomplete):
- What works:
- Basic PInvoke including marshalling
- Arrays including initializers
- Fields/ Properties/Methods with correct hiding semantics
- Properties
- String
- Int/Double/Bool
- Classes and Polymorphism … we follow C# model
- Some benchmarks - basic linpack, fannkuch, nbody
- Modules/Namespaces
- Enum - no enum.Parse support yet though
- Iterators are as .Net use enumerators etc … switching arrays to use simple for loop though for performance improvement
- Constructors/Overloads/Base Class calls
- Static Variables/Members/Properties
- Basic System.Math, more implementations required though
- Extension Methods
- Operator Overloading
- Indexers
- Anonymous Classes
- Generics … All current test cases work
- Boxed structs and interface casting for them
- Inner Classes in the form of OuterClass_InnerClass
- Static Constructors
- Explicit Interfaces
- Implicit and Explicit Cast Operators
- String switch … dlang supports this natively :)
- String.Format .. though implementation is very basic
- C# multi dimensional arrays work correctly (even with multi dim syntax :) )
- Delegates work including multicast (Native delegates through P/Invoke work too)
- Events work as expected … though a bit slower than C#(mono)
- Object initializers work as a chain of lambda expressions
- Generic Virtual Methods
- Basic Reflection of Methods, Fields and Properties, IndexerProperties are not yet supported as are Generic Methods ... for now.
- Iterators and Yield using code from WootzJS, a fiber implementation exists but it crashes on DMD 2.066 Windows 64-bit


Requirements -- Testing

-- Microsoft .Net 4.0 / Mono 3.6 and above.

-- Windows 7 or Later ( The CLI Interface works on Linux and OSX)

-- A Working D Installation (LDC,DMD or GDC)

Requirements -- Development

All requirements mentioned above and:

-- Visual Studio 2013 or above

-- Visual D

Usage

If you are using the GUI interface(windows) note that DMD should be installed in "C:\\D\\dmd2\\windows\\bin\\"
For the CLI interface the driver can be invoked in the following manner:

mono ./SharpNative.exe /compiler:pathtodcompiler /dcorlib:/**pathtodcorlib** /source:"pathtosourcefile" /outputtype:exe /dstdlib:pathtophobos /compileroptions:"compileroptions"

where:

-- pathtodcompiler is the path to a d compiler e.g. /usr/local/bin/ldc on mac osx

-- pathtodcorlib is the path to the included basic corlib e.g. /Projects/SharpNative/DCorlib

-- pathtosourcefile is the path to the test source file in C#

--pathtophobos is the location of phobos in your installation e.g. /usr/local/Cellar/ldc/0.15.0/include/d

--compileroptions are the compiler options to pass to dmd/ldc/gdc e.g. -inline -release -m64 -O5 -oq

What Doesn’t Work: (Also Incomplete)
- Right now compiler does not do any optimizations like final, in, out @pure etc …
- Async/Await - Working on an implementation
- Query Expression Syntax for LINQ and Expression Trees
- Structs FieldLayout implementation is currently wrong ... to mimic C# a new design is needed.