Transpilers(Source-to-Source Compilers)

shivang_4 5,050 views 17 slides Oct 15, 2014
Slide 1
Slide 1 of 17
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13
Slide 14
14
Slide 15
15
Slide 16
16
Slide 17
17

About This Presentation

It's a PPT showing the basics of Transpilers.


Slide Content

Transpilers (source-to-source compilers,transcompilers)

Difference b/w Compiler and Transpiler: A compiler translates source code to machine code that can be understood by the CPU hardware . A transpiler translates source code to another form of source code that can be understood by another compiler.

In other words… The difference between a compiler and a transpiler is that the compiler usually converts from a high to a low level language (C -> Assembler, Java -> Bytecode) while the transpiler converts between languages at (roughly) the same level. Examples: (C++ -> C, Pascal -> C).

Compiler Working

Transpiler Working

Difference b/w cross compilers and transpilers: A  cross compiler  is a compiler capable of creating executable code for a platform other than the one on which the compiler is running. For example, a compiler that runs on a Windows 7 PC but generates code that runs on Android smartphone is a cross compiler . whereas A   transpiler  is a type of compiler that takes the source code of a programming language as its input and outputs the source code into another programming language

How It Works?

An automatic compiler frequently takes a high level program as input and transforms the code and annotate it with parallel code annotations or language constructs. Another purpose of transpiling is translating legacy code to use the next version of the underlying programming language or an API that breaks backward compatibility . i.e. It will perform automatic code refactoring which is useful when the programs to refactor are outside the control of the original implementer (for example, converting programs from Python 2 to Python 3, or converting programs from an old API to the new API) or when the size of the program makes it impractical or time consuming to refactor it by hand.

Transpiler may either keep translated code as close to the source code as possible to ease development and debugging of the original source code, or else they may change the structure of the original code so much, that the translated code does not look like the source code.

History of Transpilers One of the earliest programs of this kind was Digital Research's XLT86 in 1981, a program written by Gary Kildall, which translated .ASM source code for the Intel 8080 processor into .A86 source code for the Intel 8086. Using global data flow analysis on 8080 register usage, the translator would also optimize the output for code size and take care of calling conventions, so that CP/M-80 and MP/M-80 programs could be ported to the CP/M-86 and MP/M-86 platforms automatically.

Examples of Transpilers Source Language Target Language Transpiler C++ C CFRONT C# JavaScript SCRIPTSHARP PHP C++ HIPHOP FOR PHP COBOL C Open COBOL

Porting a codebase When developers want to switch to a different language while retaining most of an existing codebase, it might be better to use a transcompiler compared to rewriting the whole software by hand. In this case, the code often needs manual correction because the automated translation might not work in all cases.

Tools Tool Source Language Target Language Eranea’s COBOL to JAVA Converter COBOL JAVA Google Web Toolkit JAVA program that uses specific API JavaScript And many more…….

Why Transpiler?? It turns that compilers are complex tools. Complex tools because they have to deal with several processes including, but not limiting to: parsing the input, checking the semantics, implement these semantics, finding out opportunities of optimization under the semantics, generating binary code, etc. So, that is why the source-to-source compiler is useful. A source-to-source compiler performs a parsing phase but the generated AST must be much more detailed than the one of a binary generating compiler. The reason is that the output of the source-to-source compiler must be valid as an input for another compiler.

Examples LLVM  can translate from any language supported by gcc 4.2.1 (Ada,  C,  C++, Fortran, Java, Objective-C, or Objective-C++) or by clang to any of: C, C++, or MSIL by way of the "arch" command in llvm-gcc . i.e. %llvm-g++ x.cpp –o program.bc %llc –march= msil program.bc –o program.msil

Contd… ROSE: It is an open source compiler infrastructure to build source-to-source program transformation and analysis tools for large-scale Fortran 77/95/2003, C, C++, OpenMP, and UPC applications. DMS Software Reengineering Toolkit: It is a source-to-source program transformation tool. It can be used for translating from one computer language to another, for compiling domain-specific languages to a general purpose language, or for carrying out optimizations or massive modifications within a specific language .

Contd…. OpenMP: In this implementation is source-to-source, meaning that it converts the input program, in Fortran source, which contains OpenMP directives into an output program, also in Fortran source. The output program explicitly creates and synchronizes parallel threads to implement parallelism, as indicated by the OpenMP directives in the input program. Cetus : It is a compiler infrastructure for the source-to-source transformation of software programs. It currently supports ANSI C. Since its creation in 2004, it has grown to over 80,000 lines of Java code, has been made available publicly on the web, and has become a basis for several research projects.