transpilers(source-to-source compilers)

17
Transpilers (source-to-source compilers,transcompilers)

Upload: shivang-bajaniya

Post on 06-Jul-2015

574 views

Category:

Engineering


5 download

DESCRIPTION

It's a PPT showing the basics of Transpilers.

TRANSCRIPT

Page 1: Transpilers(Source-to-Source Compilers)

Transpilers(source-to-source compilers,transcompilers)

Page 2: Transpilers(Source-to-Source Compilers)

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.

Page 3: Transpilers(Source-to-Source Compilers)

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).

Page 4: Transpilers(Source-to-Source Compilers)

Compiler Working

Source

CodeCompiler Machine

Code

Page 5: Transpilers(Source-to-Source Compilers)

Transpiler Working

Source Code Transpiler Source Code

Page 6: Transpilers(Source-to-Source Compilers)

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

Page 7: Transpilers(Source-to-Source Compilers)

How It Works?

Page 8: Transpilers(Source-to-Source Compilers)

• 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.

Page 9: Transpilers(Source-to-Source Compilers)

•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.

Page 10: Transpilers(Source-to-Source Compilers)

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.

Page 11: Transpilers(Source-to-Source Compilers)

Examples of Transpilers

Source Language Target Language TranspilerC++ C CFRONTC# JavaScript SCRIPTSHARPPHP C++ HIPHOP FOR PHP

COBOL C Open COBOL

Page 12: Transpilers(Source-to-Source Compilers)

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.

Page 13: Transpilers(Source-to-Source Compilers)

Tools

Tool Source Language Target LanguageEranea’s COBOL to JAVA Converter

COBOL JAVA

Google Web Toolkit JAVA program that uses specific API

JavaScript

And many more…….

Page 14: Transpilers(Source-to-Source Compilers)

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.

Page 15: Transpilers(Source-to-Source Compilers)

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

Page 16: Transpilers(Source-to-Source Compilers)

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.

Page 17: Transpilers(Source-to-Source Compilers)

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.