Progress of development

Coordinator
Nov 13, 2014 at 8:52 AM
Edited Nov 13, 2014 at 8:56 AM
1) support of generics is improved in latest version. Now the following example can be compiled and executed

//-- ex-gen-class-polynomial

using System;

// A type implements AddMul<A,R> if one can add an A to it, giving an R:

interface AddMul<A,R> {
  R Add(A e);                   // Addition with A, giving R
  R Mul(A e);                   // Multiplication with A, giving R
}

// Polynomials over E, Polynomial<E>:

// The base type E of the polynomial must support addition,
// multiplication and zero (via the nullary constructor).  That's what
// the type parameter constraint on E says.

// In return, one can add an E or a polynomial over E to a polynomial
// over E.  Similarly, a polynomial over E can be multiplied by an E
// or by a polynomial over E.  That's what the interface clauses say.

class Polynomial<E> : AddMul<E,Polynomial<E>>,
                      AddMul<Polynomial<E>,Polynomial<E>>
  where E : AddMul<E,E>, new() {
  // cs contains coefficients of x^0, x^1, ...; absent coefficients are zero.
  // Invariant: cs != null && cs.Length >= 0; cs.Length==0 represents zero.
  private readonly E[] cs;  

  public Polynomial() { 
    this.cs = new E[0];
  }

  public Polynomial(E[] cs) { 
    this.cs = cs;
  }

  public Polynomial<E> Add(Polynomial<E> that) {
    int newlen = Math.Max(this.cs.Length, that.cs.Length);
    int minlen = Math.Min(this.cs.Length, that.cs.Length);
    E[] newcs = new E[newlen];
    if (this.cs.Length <= that.cs.Length) {
      for (int i=0; i<minlen; i++)
        newcs[i] = this.cs[i].Add(that.cs[i]);
      for (int i=minlen; i<newlen; i++)
        newcs[i] = that.cs[i];
    } else {
      for (int i=0; i<minlen; i++)
        newcs[i] = this.cs[i].Add(that.cs[i]);
      for (int i=minlen; i<newlen; i++)
        newcs[i] = this.cs[i];
    }
    return new Polynomial<E>(newcs);
  }

  public Polynomial<E> Add(E that) {
    return this.Add(new Polynomial<E>(new E[] { that }));
  } 

  public Polynomial<E> Mul(E that) {
    E[] newcs = new E[cs.Length];
    for (int i=0; i<cs.Length; i++)
      newcs[i] = that.Mul(cs[i]);
    return new Polynomial<E>(newcs);
  }

  public Polynomial<E> Mul(Polynomial<E> that) {
    int newlen = Math.Max(1, this.cs.Length + that.cs.Length - 1);
    E[] newcs = new E[newlen];
    for (int i=0; i<newlen; i++) {
      E sum = new E();                     // Permitted by constraint E : new()
      int start = Math.Max(0, i-that.cs.Length+1);
      int stop  = Math.Min(i, this.cs.Length-1);
      for (int j=start; j<=stop; j++) {
        // assert 0<=j && j<this.cs.Length && 0<=i-j && i-j<that.cs.Length;
        sum = sum.Add(this.cs[j].Mul(that.cs[i-j]));
      }
      newcs[i] = sum;
    }
    return new Polynomial<E>(newcs);
  }

  public E Eval(E x) {
    E res = new E();                       // Permitted by constraint E : new()
    for (int j=cs.Length-1; j>=0; j--) 
      res = res.Mul(x).Add(cs[j]);
    return res;
  }
}  

struct Int : AddMul<Int,Int> {
  private readonly int i;
  public Int(int i) {
    this.i = i;
  }
  public Int Add(Int that) {
    return new Int(this.i + that.i);
  }
  public Int Mul(Int that) {
    return new Int(this.i * that.i);
  }
  public override String ToString() {
    return i.ToString();
  }
}

class TestPolynomial {
  public static void Main(String[] args) {
    // The integer polynomial 2 + 5x + x^2
    Polynomial<Int> ip = 
      new Polynomial<Int>(new Int[] { new Int(2), new Int(5), new Int(1) });
    Console.WriteLine(ip.Eval(new Int(10)));            // 152
    Console.WriteLine(ip.Add(ip).Eval(new Int(10)));    // 304 = 152 + 152
    Console.WriteLine(ip.Mul(ip).Eval(new Int(10)));    // 23104 = 152 * 152
  }
}



Output result:
152
304
23104
Nov 16, 2014 at 3:17 PM
Thats great, but where is the support for Android? You said you were adding support for compiling with different toolchains.
Coordinator
Nov 16, 2014 at 9:15 PM
Edited Nov 16, 2014 at 9:20 PM
it is already there (you need to have a bit of experiance how to compile native app for android)

have a look into downloading area of Babylon3d project https://babylon.codeplex.com/

here is the example how to compile it (I need to upload project to Git to show how to compile it)

Android.mk file
LOCAL_PATH := $(subst //,/,$(call my-dir))
include $(CLEAR_VARS)

LOCAL_MODULE := native-activity-cs

#LOCAL_SRC_FILES := $(wildcard *.cpp)
#LOCAL_SRC_FILES += $(wildcard *.c)
#LOCAL_SRC_FILES += $(wildcard *.ll)

# APP
LOCAL_SRC_FILES := dummy.cpp main.c BabylonAndroid.ll BabylonNativeCsLibraryForIl.ll CoreLib.ll

LOCAL_ARM_MODE  := arm

LOCAL_LDLIBS := -lgc-lib -lstdc++ -lc -lm -llog -landroid -ldl -lGLESv2 -lEGL -lOpenSLES
LOCAL_STATIC_LIBRARIES := android_native_app_glue

LOCAL_LDFLAGS := -L $(LOCAL_PATH)/../../../Deps/GC/lib/armeabi-v7a

LOCAL_CFLAGS := -DANDROID_NDK \
                -DDISABLE_IMPORTGL

LOCAL_LLFLAGS := -enable-pie -relocation-model=pic

include $(BUILD_SHARED_LIBRARY)

#$(call import-add-path, ../libs/jni)

$(call import-module,android/native_app_glue)
Application.mk
APP_ABI := armeabi-v7a
APP_PLATFORM := android-15
APP_STL := stlport_static
#NDK_TOOLCHAIN := clang3.4
APP_CPPFLAGS += -Wno-error=format-security -fexceptions -frtti -std=c++11 -fPIC
you need to fix some files in NDK to be able to compile .ll files (file build-binary.mk and definitions.mk) you can download them from Babylon 3D project

before all of it you need to generate .ll files. you need to run cmd.lines
Il2Bc.exe "BabylonAndroid.dll" /corelib:"<PATH>\CoreLib.dll" /gctors- /target:armv7-none-linux-androideabi
Il2Bc.exe "BabylonNativeCsLibraryForIl.dll" /corelib:"<PATH>\CoreLib.dll" /gctors- /target:armv7-none-linux-androideabi
Il2Bc.exe "CoreLib.dll" /gctors- /target:armv7-none-linux-androideabi
Coordinator
Nov 16, 2014 at 9:23 PM
so steps to do

1) compile DLLs (using CoreLib in visual studio)

2) generate .ll files for each Dll

3) copy them into source folder of Android Native App

4) compile android native app

done.
Nov 19, 2014 at 11:09 AM
Thank you,
Would be great to see support for emscripten now, as it already has LLVM integrated.
Nov 21, 2014 at 11:50 AM
Can you give a try and see if emscripten would be supported?
What about iOS deploy?

Thanks
Coordinator
Nov 24, 2014 at 12:34 PM
Edited Nov 24, 2014 at 1:51 PM
Yes, I can compile with emscripten, Seems it is using LLVM 3.4 I need to do some cosmetic changes to .LL file

iOS I can't try as I do not have any iOS device

PS. seems emscripten not properly supporting wprintf
Coordinator
Nov 26, 2014 at 11:43 AM
in the next update there will be option to compile for emscripten
Nov 26, 2014 at 3:54 PM
Thank you very much, really appreciated, can't wait to try this out.

Thanks
Dec 4, 2014 at 7:28 AM
Can you commit your code about emscripten and some instruction?
Coordinator
Dec 4, 2014 at 10:18 AM
Edited Dec 4, 2014 at 10:24 AM
I sill commit source code only, is it ok? I think it is not very difficult to build it (if yes, I have committed it)

There is an issue with emscripten, emscripten using UTF32 instead of UTF16, so wprintf is not working for me.
Dec 4, 2014 at 1:47 PM
Which version of emscripten you've been using?
Coordinator
Dec 4, 2014 at 7:57 PM
emcc (Emscripten GCC-like replacement) 1.25.0 ()
Feb 13, 2015 at 12:55 PM
Does anybody have working on Emscripten example?

I took "Hello World" from C# Native example and translate it to .ll file. Next I created .js file with Emscripten. But when I tried it, I got an exception from _Void_System_Guid__cctor__(). Then I commented this line in _Global_Ctors_for_CoreLib() and now example works, but it prints two east asian characters instead of "Hello, world" string. Also I get a few warnings about unexpected number of arguments in call to 'wprintf'.

This is commands for building the example:
Il2Bc.exe ..\CSNativeTest\Program.cs /corelib:CoreLib.dll /llvm34 /emscripten
Il2Bc.exe CoreLib.dll /llvm34 /emscripten
emcc Program.ll -o Program.bc
emcc CoreLib.ll -o CoreLib.bc
emcc Program.bc CoreLib.bc -o CSNativeTest.js
I use Emscripten 1.27.0. Version of clang is 3.3, but I also tried to build the example with clang 3.7 and got the same result.
Feb 13, 2015 at 3:41 PM
I told AlexDev about this problem, probably he need to add some kind of native interop during ll generation to marshal C# string to emscripten one.
Coordinator
Feb 13, 2015 at 4:44 PM
this is because C# unicode is 2 bytes but Emscriptem is using 4 bytes unicode. so while I am using printf to simulate console output it will not work.
Coordinator
Feb 18, 2015 at 1:35 PM
I have added Array functionality to support casting to IList


Test case
using System;
using System.Collections;

class X
{
    public unsafe static int Main(string[] args)
    {
        var a = new double [] { 1.0, 2.0, 3.0, 4.0 };

        var l = (IList)a;

        Console.WriteLine("a");
        foreach (var i in a)
        {
            Console.WriteLine(i);
        }

    l[1] = 10.0;    

        Console.WriteLine("as List");
        foreach (var i in l)
        {
            Console.WriteLine(i);
        }

        return 0;
    }
}
Output
a
1.000000
2.000000
3.000000
4.000000
as List
1
10
3
4
Coordinator
Apr 21, 2015 at 1:16 PM
Edited Apr 21, 2015 at 1:16 PM
Finalizers now working and GC is utilized properly (wait for an update)

To test finalizers
using System;

class Foo
{
    ~Foo()
    {
    Console.WriteLine("~Foo");
    }
    
    public static void Main ()
    {
        while (true) new Foo ();
    }
}
Apr 23, 2015 at 1:18 PM
Sorry cannot compile the generated cpp files in VisualStudio, could you please support this too?
Would be good to have lib-gc for VS2013 too.

Thank you
Coordinator
Apr 25, 2015 at 11:44 AM
amerthebest wrote:
Sorry cannot compile the generated cpp files in VisualStudio, could you please support this too?
Would be good to have lib-gc for VS2013 too.

Thank you
I will do it, but can't way when