AlterNative
Human-Like translations from .NET assemblies to C++. Experience the power and performance of native applications right now!
Human-Like translations from .NET assemblies to C++. Experience the power and performance of native applications right now!
AlterNative is a tool made by Developers for Developers. Its concept is to maximize the idea of happy-coding. It provides a tool to easy port applications from high-level languages such as .NET to native languages like C++. It is a research project and it is under development with the collaboration of UPC - BarcelonaTech and AlterAid S.L.
This software has been developed in the project Cloud ARM funded by the spanish Ministry of Science and Innovation (IPT-2011-1834-430000).
More information about CloudARM here.
Most of the actual systems are C++ compatible, thus if the application is ported to this language, it can be executed in several platforms (i.e. smartphones, tablets, embedded systems and computers).
AlterNative's philosophy is to provide the user with a system for taking the advantage of the .NET fast development and also take the advantage of performance of low-level languages. But also, exploit the possibility to run this native code in many systems or in other words, follow the WORA (Write Once, Run Everywhere) philosophy.
AlterNative is made with two assumptions:
AlterNative uses the following open-sourced libraries to do it's job:
AlterNative uses AST transformations to do its magic and a set of C++ libraries that turn that AST translations into real compilable codes.
With the release of AlterNative it also has been released a public set of libraries that are referenced at compile time. This libraries are the C++ implementation of the C# runtime classes. For example, the C# class Console is included in this library which implements the same methods but using the C++ syntax.
This means that all the hard work is made by us, so you will only need to enjoy the performance of C++AlterNative Library can be found here.
The provided library uses the following open-sourced libraries to do it's job:
As an example we show a demo version of the class System::Console of the library. The methods are implemented following the naming of the .NET class (System.Console).
In this example we show you how is translated a simple Hello World program. The program is writed in C# and can be compiled in Windows with Visual Studio (msbuild compiler), or in Linux with the mono compiler "mcs".
The code used for this example is described in the listing below:
We run AlterNative targeting the above code compiled in the HelloWorld.exe assembly. The result obtained are the files listed below:
In addition to these files, it is provided an CMakeLists.txt auto-generated by AlterNative in order to provide you with a cross-platform tool for easy compiling the code. Also will be generated a main.cpp as an entry point of the program. This main.cpp redirects to your original entry point.
In this example AlterNative will translate a C# .NET feature that C++ does not support: the foreach statement (C++11 release offers a foreach support but the syntax is different from the C# syntax). The proposed code to be translated is shown in the following listing:
It is important to highlight the challenge of translate an envioronment in which all the classes are "Objects" to an environment in wihch this behaviour is different. In this example, the classes "float" and "A", in C# can be treated as Objects (i.e. int.ToString() exists). In C++ the types int, float, char... are Primitive Types and no classes, for this reason the translated code will try to workaround these drawbacks and get a compilable code at the Output.
Referencing the described above, the management of the variables in the case of the loop of "floats" is different to the case of loop of "A", being the second a pointer to an object while the first is value type.
One important key point of this example is the translation of the foreach statement. In the process of translation, AlterNative takes into account all the features the library offers. In this case, the library provides a "FOREACH" macro which internally calls the methods of end(), begin() and the internal methods of the iterators.
The following listings of code show the resulting code for the decompilation of the class A. In AlterNative each type decompiled is writen in a different file (pair of *.h and *.cpp files).
In this tutorial we will explain you how to translate your application. First of all make sure to follow the instructions explained in the README of the GitHub Page. If you have done these steps you should:
$ alternative new test
$ alternative make test
$ cd test/build
$ ./Blank
After the installation of AlterNative we will write our C# code to be translated. We propose a simple program with one class and two methods
Copy and paste this code and compile it. In Windows with msbuild (or Visual Studio), in Linux with Mono compiler "mcs".
Once the program is compiled, run:
$ alternative myProgram.exe CppOutput
You should see in the output folder these files:
Now we will compile the output code with the command:
$ alternative make CppOutput
After running this command, it should appear a folder "build" in the project folder. CD into it and run the executable. Don't worry if you notice an extremely increase of speed in the execution, you are in native world :) .
Here the involved modules in this tutorial are two:
For example, if we have to convert the statement byte[] b = new byte[10];
The flow of the AST transformations is processed like a tree pattern. Each node denotes a construction in the source code. The major part of the nodes are composed by other nodes which represent more specific code. As a simplified example, a node representing a = b + c, is a combination of:
Every transformation has to be identified inside the AST tree. We have to specify which node we want to translate (i.e. IfElseStatement node). In this tutorial we will see all the process of how to implement a simple transformation.
The code we will transform in this tutorial is a .NET feature, the "as" casting: Person p = myObject as Person;
In fact, the node we will transform is "myObject as Person" which we can identify that is an "As Expression" node.
We have to provide a new feature in C++ that is not available in the standard library. The new feature will be in charge of cast an object to another object. For maintaining the readability of the code, we propose implement a feature in the AlterNative Library for providing easy "as casting" in c++, for example: Person* p = as_cast<Person*>(myObject);
In other words, we have to convert an "As Expression" node into a "Call to Method Expression" node.
We have to locate where is processed the "As Expression" node in order to translate it to a "Call to Method Expression" pointing to the method as_cast (that later we will implement in the AlterNative Library). These nodes are called in the AST "AsExpression" and "InvocationExpression". We have to find the method in charge of processing this node:
The process of our transformation is easy, we have to create an InvocationExpression node and fill it with the correct elements from AsExpression node.
The code above is a process that we call AST transformation. These AST transformations allows AlterNative to perform its work. In this tutorial, the transformation we have made can be described with this AST transformation:
If we assume that the nodes InvocationExpression and IdentifierExpression are implemented, it is not necessary to edit the module CppOutputVisitor because there is no new node created, and the nodes participating in this transformation already have been implemented and will output a well-formed C++ code. You may want to check these nodes in the CppOutputVisitor only for get familiarize with the code
Now we have to implement the method as_cast
This feature supports a standard feature of C#, so we will implement it in the file System/support/asis.h which will be loaded always in our programs.
This software has been developed in the project Cloud ARM funded by the spanish Ministry of Science and Innovation (IPT-2011-1834-430000). The objective of the Cloud ARM project is the development of innovative augmented reality and image retrieval applications focused on the tourist sector.
In the scope of this project, AlterNative provides a mechanism for implementing complex computational algorithms on the mobile device in a cross platform way and with native performance. The Cloud ARM apps are HTML5 based applications that access the device hardware through Cordova plugins and do intensive calculations through AlterNative.
This work is also part of Alex Albalà Díaz’s PhD. directed by Juan López Rubio. These activities has been funded by the grant EMPLEA, within the Programa Estatal de Promoción del Talento y su Empleabilidad, included in the Plan Estatal de Investigación Científica, Técnica y de Innovación 2013-2016 of Ministerio de Economía y Competitividad.
You can contact us using the form on the left, in our Google groups but also you can send an email to the following address: